[oden] Cleaner textures
This commit is contained in:
parent
f5da9ed0fb
commit
aa70df41a3
4 changed files with 93 additions and 70 deletions
7
Cargo.lock
generated
7
Cargo.lock
generated
|
|
@ -86,6 +86,12 @@ dependencies = [
|
|||
"libc",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "anyhow"
|
||||
version = "1.0.71"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "9c7d0618f0e0b7e8ff11427422b64564d5fb0be1940354bfe2e0529b18a9d9b8"
|
||||
|
||||
[[package]]
|
||||
name = "arrayref"
|
||||
version = "0.3.7"
|
||||
|
|
@ -966,6 +972,7 @@ dependencies = [
|
|||
name = "oden"
|
||||
version = "0.1.0"
|
||||
dependencies = [
|
||||
"anyhow",
|
||||
"bytemuck",
|
||||
"env_logger",
|
||||
"image",
|
||||
|
|
|
|||
|
|
@ -6,6 +6,7 @@ edition = "2021"
|
|||
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
|
||||
|
||||
[dependencies]
|
||||
anyhow = "1.0"
|
||||
bytemuck = { version = "1.13", features = ["derive"] }
|
||||
env_logger = "0.10"
|
||||
image = { version = "0.24", default-features = false, features = ["png"] }
|
||||
|
|
|
|||
76
src/lib.rs
76
src/lib.rs
|
|
@ -1,5 +1,4 @@
|
|||
use bytemuck;
|
||||
use image;
|
||||
use wgpu::util::DeviceExt;
|
||||
use winit::{
|
||||
event::*,
|
||||
|
|
@ -8,6 +7,8 @@ use winit::{
|
|||
window::WindowBuilder,
|
||||
};
|
||||
|
||||
mod texture;
|
||||
|
||||
#[repr(C)]
|
||||
#[derive(Copy, Clone, Debug, bytemuck::Pod, bytemuck::Zeroable)]
|
||||
struct Vertex {
|
||||
|
|
@ -15,7 +16,6 @@ struct Vertex {
|
|||
tex_coords: [f32; 2],
|
||||
}
|
||||
|
||||
// lib.rs
|
||||
impl Vertex {
|
||||
fn desc() -> wgpu::VertexBufferLayout<'static> {
|
||||
wgpu::VertexBufferLayout {
|
||||
|
|
@ -153,72 +153,8 @@ impl State {
|
|||
surface.configure(&device, &config);
|
||||
|
||||
let diffuse_bytes = include_bytes!("happy-tree.png");
|
||||
let diffuse_image = image::load_from_memory(diffuse_bytes).unwrap();
|
||||
let diffuse_rgba = diffuse_image.to_rgba8();
|
||||
|
||||
use image::GenericImageView;
|
||||
let dimensions = diffuse_image.dimensions();
|
||||
|
||||
let texture_size = wgpu::Extent3d {
|
||||
width: dimensions.0,
|
||||
height: dimensions.1,
|
||||
depth_or_array_layers: 1,
|
||||
};
|
||||
let diffuse_texture = device.create_texture(&wgpu::TextureDescriptor {
|
||||
// All textures are stored as 3D, we represent our 2D texture
|
||||
// by setting depth to 1.
|
||||
size: texture_size,
|
||||
mip_level_count: 1,
|
||||
sample_count: 1,
|
||||
dimension: wgpu::TextureDimension::D2,
|
||||
// Most images are stored using sRGB so we need to reflect that here.
|
||||
format: wgpu::TextureFormat::Rgba8UnormSrgb,
|
||||
// TEXTURE_BINDING tells wgpu that we want to use this texture in shaders
|
||||
// COPY_DST means that we want to copy data to this texture
|
||||
usage: wgpu::TextureUsages::TEXTURE_BINDING | wgpu::TextureUsages::COPY_DST,
|
||||
label: Some("diffuse_texture"),
|
||||
|
||||
// This is the same as with the SurfaceConfig. It specifies what
|
||||
// texture formats can be used to create TextureViews for this
|
||||
// texture. The base texture format (Rgba8UnormSrgb in this case)
|
||||
// is always supported. Note that using a different texture
|
||||
// format is not supported on the WebGL2 backend.
|
||||
view_formats: &[],
|
||||
});
|
||||
|
||||
// Jam the bits into the texture
|
||||
queue.write_texture(
|
||||
// Tells wgpu where to copy the pixel data
|
||||
wgpu::ImageCopyTexture {
|
||||
texture: &diffuse_texture,
|
||||
mip_level: 0,
|
||||
origin: wgpu::Origin3d::ZERO,
|
||||
aspect: wgpu::TextureAspect::All,
|
||||
},
|
||||
// The actual pixel data
|
||||
&diffuse_rgba,
|
||||
// The layout of the texture
|
||||
wgpu::ImageDataLayout {
|
||||
offset: 0,
|
||||
bytes_per_row: Some(4 * dimensions.0),
|
||||
rows_per_image: Some(dimensions.1),
|
||||
},
|
||||
texture_size,
|
||||
);
|
||||
|
||||
// We don't need to configure the texture view much, so let's
|
||||
// let wgpu define it.
|
||||
let diffuse_texture_view =
|
||||
diffuse_texture.create_view(&wgpu::TextureViewDescriptor::default());
|
||||
let diffuse_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()
|
||||
});
|
||||
let diffuse_texture =
|
||||
texture::Texture::from_bytes(&device, &queue, diffuse_bytes, "happy-tree.png").unwrap();
|
||||
|
||||
let texture_bind_group_layout =
|
||||
device.create_bind_group_layout(&wgpu::BindGroupLayoutDescriptor {
|
||||
|
|
@ -250,11 +186,11 @@ impl State {
|
|||
entries: &[
|
||||
wgpu::BindGroupEntry {
|
||||
binding: 0,
|
||||
resource: wgpu::BindingResource::TextureView(&diffuse_texture_view),
|
||||
resource: wgpu::BindingResource::TextureView(&diffuse_texture.view),
|
||||
},
|
||||
wgpu::BindGroupEntry {
|
||||
binding: 1,
|
||||
resource: wgpu::BindingResource::Sampler(&diffuse_sampler),
|
||||
resource: wgpu::BindingResource::Sampler(&diffuse_texture.sampler),
|
||||
},
|
||||
],
|
||||
label: Some("diffuse_bind_group"),
|
||||
|
|
|
|||
79
src/texture.rs
Normal file
79
src/texture.rs
Normal file
|
|
@ -0,0 +1,79 @@
|
|||
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,
|
||||
})
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue