-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
moved the texture struct into it's own file
- Loading branch information
1 parent
75fb844
commit 663ab99
Showing
5 changed files
with
103 additions
and
53 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,20 +1,24 @@ | ||
{ | ||
"cSpell.words": [ | ||
"bindgen", | ||
"bytemuck", | ||
"Clippy", | ||
"Halvar", | ||
"mipmap", | ||
"Multisample", | ||
"multisampled", | ||
"multiview", | ||
"repr", | ||
"srgb", | ||
"swapchain", | ||
"Unorm", | ||
"Vsync", | ||
"vulkano", | ||
"webgl", | ||
"wgpu", | ||
"wgsl", | ||
"winit" | ||
"winit", | ||
"Zeroable" | ||
], | ||
"rust-analyzer.linkedProjects": ["Cargo.toml"] | ||
"rust-analyzer.linkedProjects": ["Cargo.toml", ".\\Cargo.toml"] | ||
} |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,80 @@ | ||
use anyhow::*; | ||
use image::GenericImageView; | ||
|
||
pub struct Texture { | ||
pub texture: wgpu::Texture, | ||
pub view: wgpu::TextureView, | ||
pub sampler: wgpu::Sampler, | ||
} | ||
|
||
impl Texture { | ||
pub fn from_bytes( | ||
device: &wgpu::Device, | ||
queue: &wgpu::Queue, | ||
bytes: &[u8], | ||
label: &str, | ||
) -> Result<Self> { | ||
let img = image::load_from_memory(bytes)?; | ||
Self::from_image(device, queue, &img, Some(label)) | ||
} | ||
|
||
pub fn from_image( | ||
device: &wgpu::Device, | ||
queue: &wgpu::Queue, | ||
img: &image::DynamicImage, | ||
label: Option<&str>, | ||
) -> Result<Self> { | ||
let rgba = img.to_rgba8(); | ||
let dimensions = img.dimensions(); | ||
|
||
let size = wgpu::Extent3d { | ||
width: dimensions.0, | ||
height: dimensions.1, | ||
depth_or_array_layers: 1, | ||
}; | ||
|
||
let texture = device.create_texture(&wgpu::TextureDescriptor { | ||
label, | ||
size, | ||
mip_level_count: 1, | ||
sample_count: 1, | ||
dimension: wgpu::TextureDimension::D2, | ||
format: wgpu::TextureFormat::Rgba8UnormSrgb, | ||
usage: wgpu::TextureUsages::TEXTURE_BINDING | wgpu::TextureUsages::COPY_DST, | ||
view_formats: &[], | ||
}); | ||
|
||
queue.write_texture( | ||
wgpu::ImageCopyTexture { | ||
aspect: wgpu::TextureAspect::All, | ||
texture: &texture, | ||
mip_level: 0, | ||
origin: wgpu::Origin3d::ZERO, | ||
}, | ||
&rgba, | ||
wgpu::ImageDataLayout { | ||
offset: 0, | ||
bytes_per_row: Some(4 * dimensions.0), | ||
rows_per_image: Some(dimensions.1), | ||
}, | ||
size, | ||
); | ||
|
||
let view = texture.create_view(&wgpu::TextureViewDescriptor::default()); | ||
let sampler = device.create_sampler(&wgpu::SamplerDescriptor { | ||
address_mode_u: wgpu::AddressMode::ClampToEdge, | ||
address_mode_v: wgpu::AddressMode::ClampToEdge, | ||
address_mode_w: wgpu::AddressMode::ClampToEdge, | ||
mag_filter: wgpu::FilterMode::Linear, | ||
min_filter: wgpu::FilterMode::Nearest, | ||
mipmap_filter: wgpu::FilterMode::Nearest, | ||
..Default::default() | ||
}); | ||
|
||
Ok(Self { | ||
texture, | ||
view, | ||
sampler, | ||
}) | ||
} | ||
} |