texture struct
This commit is contained in:
+12
-57
@@ -1,9 +1,8 @@
|
||||
use winit::{window::Window, dpi::PhysicalSize, event::WindowEvent};
|
||||
use wgpu::util::DeviceExt;
|
||||
use std::path::Path;
|
||||
use image::GenericImageView;
|
||||
|
||||
use crate::vertex::Vertex;
|
||||
use crate::texture::Texture;
|
||||
|
||||
const VERTICES: &[Vertex] = &[
|
||||
Vertex { position: [-1.0, 1.0, 0.0], uv: [0.0, 0.0] },
|
||||
@@ -28,7 +27,7 @@ pub struct App {
|
||||
vertex_buffer: wgpu::Buffer,
|
||||
index_buffer: wgpu::Buffer,
|
||||
// Texture
|
||||
diffuse_bind_group: wgpu::BindGroup
|
||||
bind_group: wgpu::BindGroup
|
||||
}
|
||||
|
||||
impl App {
|
||||
@@ -71,53 +70,9 @@ impl App {
|
||||
let swapchain = device.create_swap_chain(&surface, &swapchain_desc);
|
||||
|
||||
// Load image
|
||||
let img_path = Path::new("./assets/rick.png");
|
||||
let img = image::open(img_path).unwrap();
|
||||
let img_dim = img.dimensions();
|
||||
let img_data = img.as_rgba8().unwrap();
|
||||
let tex = Texture::from_file(&device, &queue, "./assets/rick.png");
|
||||
|
||||
let texture_size = wgpu::Extent3d { width: img_dim.0, height: img_dim.1, depth: 1, };
|
||||
let texture_desc = wgpu::TextureDescriptor {
|
||||
size: texture_size,
|
||||
mip_level_count: 1,
|
||||
sample_count: 1,
|
||||
dimension: wgpu::TextureDimension::D2,
|
||||
format: wgpu::TextureFormat::Rgba8UnormSrgb,
|
||||
usage: wgpu::TextureUsage::SAMPLED | wgpu::TextureUsage::COPY_DST,
|
||||
label: Some("diffuse_texture"),
|
||||
};
|
||||
let texture = device.create_texture(&texture_desc);
|
||||
|
||||
queue.write_texture(
|
||||
// Tells wgpu where to copy the pixel data
|
||||
wgpu::TextureCopyView {
|
||||
texture: &texture,
|
||||
mip_level: 0,
|
||||
origin: wgpu::Origin3d::ZERO,
|
||||
},
|
||||
// The actual pixel data
|
||||
img_data,
|
||||
// The layout of the texture
|
||||
wgpu::TextureDataLayout {
|
||||
offset: 0,
|
||||
bytes_per_row: 4 * img_dim.0,
|
||||
rows_per_image: img_dim.1,
|
||||
},
|
||||
texture_size,
|
||||
);
|
||||
|
||||
let diffuse_texture_view = 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 texture_bind_group_layout = device.create_bind_group_layout(
|
||||
let bind_group_layout = device.create_bind_group_layout(
|
||||
&wgpu::BindGroupLayoutDescriptor {
|
||||
entries: &[
|
||||
wgpu::BindGroupLayoutEntry {
|
||||
@@ -140,21 +95,21 @@ impl App {
|
||||
count: None,
|
||||
},
|
||||
],
|
||||
label: Some("texture_bind_group_layout"),
|
||||
label: None,
|
||||
}
|
||||
);
|
||||
|
||||
let diffuse_bind_group = device.create_bind_group(
|
||||
let bind_group = device.create_bind_group(
|
||||
&wgpu::BindGroupDescriptor {
|
||||
layout: &texture_bind_group_layout,
|
||||
layout: &bind_group_layout,
|
||||
entries: &[
|
||||
wgpu::BindGroupEntry {
|
||||
binding: 0,
|
||||
resource: wgpu::BindingResource::TextureView(&diffuse_texture_view),
|
||||
resource: wgpu::BindingResource::TextureView(&tex.view),
|
||||
},
|
||||
wgpu::BindGroupEntry {
|
||||
binding: 1,
|
||||
resource: wgpu::BindingResource::Sampler(&diffuse_sampler),
|
||||
resource: wgpu::BindingResource::Sampler(&tex.sampler),
|
||||
}
|
||||
],
|
||||
label: Some("diffuse_bind_group"),
|
||||
@@ -166,7 +121,7 @@ impl App {
|
||||
// Pipeline stuff
|
||||
let pipeline_layout = device.create_pipeline_layout(&wgpu::PipelineLayoutDescriptor {
|
||||
label: Some("Render Pipeline Layout"),
|
||||
bind_group_layouts: &[&texture_bind_group_layout],
|
||||
bind_group_layouts: &[&bind_group_layout],
|
||||
push_constant_ranges: &[],
|
||||
});
|
||||
let vert_state = wgpu::VertexState { //vert
|
||||
@@ -228,7 +183,7 @@ impl App {
|
||||
|
||||
// Save values in app
|
||||
println!("Initialized");
|
||||
Self { surface, device, queue, swapchain_desc, swapchain, pipeline, vertex_buffer, index_buffer, diffuse_bind_group }
|
||||
Self { surface, device, queue, swapchain_desc, swapchain, pipeline, vertex_buffer, index_buffer, bind_group }
|
||||
}
|
||||
|
||||
pub fn resize(&mut self, new_size: Option<PhysicalSize<u32>>) {
|
||||
@@ -279,7 +234,7 @@ impl App {
|
||||
});
|
||||
|
||||
render_pass.set_pipeline(&self.pipeline);
|
||||
render_pass.set_bind_group(0, &self.diffuse_bind_group, &[]);
|
||||
render_pass.set_bind_group(0, &self.bind_group, &[]);
|
||||
render_pass.set_vertex_buffer(0, self.vertex_buffer.slice(..));
|
||||
render_pass.set_index_buffer(self.index_buffer.slice(..), wgpu::IndexFormat::Uint16);
|
||||
render_pass.draw_indexed(0..(INDICES.len() as u32), 0, 0..1);
|
||||
|
||||
Binary file not shown.
Binary file not shown.
@@ -6,6 +6,7 @@ use winit::{
|
||||
};
|
||||
|
||||
mod vertex;
|
||||
mod texture;
|
||||
mod app;
|
||||
use app::App;
|
||||
|
||||
|
||||
@@ -0,0 +1,60 @@
|
||||
use std::path::Path;
|
||||
use image::GenericImageView;
|
||||
|
||||
pub struct Texture {
|
||||
pub texture: wgpu::Texture,
|
||||
pub view: wgpu::TextureView,
|
||||
pub sampler: wgpu::Sampler,
|
||||
}
|
||||
|
||||
impl Texture {
|
||||
|
||||
pub fn from_file(device: &wgpu::Device, queue: &wgpu::Queue, str_path: &str) -> Self {
|
||||
let img_path = Path::new(str_path);
|
||||
let img = image::open(img_path).unwrap();
|
||||
Self::from_image(&device, &queue, &img)
|
||||
}
|
||||
|
||||
pub fn from_image(device: &wgpu::Device, queue: &wgpu::Queue, img: &image::DynamicImage) -> Self {
|
||||
// Data from image
|
||||
let img_dim = img.dimensions();
|
||||
let img_data = img.as_rgba8().unwrap();
|
||||
|
||||
// Create texture on device
|
||||
let texture_size = wgpu::Extent3d { width: img_dim.0, height: img_dim.1, depth: 1, };
|
||||
let texture = device.create_texture(&wgpu::TextureDescriptor {
|
||||
size: texture_size,
|
||||
mip_level_count: 1,
|
||||
sample_count: 1,
|
||||
dimension: wgpu::TextureDimension::D2,
|
||||
format: wgpu::TextureFormat::Rgba8UnormSrgb,
|
||||
usage: wgpu::TextureUsage::SAMPLED | wgpu::TextureUsage::COPY_DST,
|
||||
label: None,
|
||||
});
|
||||
|
||||
// Write texture to memory
|
||||
queue.write_texture(
|
||||
wgpu::TextureCopyView { texture: &texture, mip_level: 0, origin: wgpu::Origin3d::ZERO },
|
||||
img_data,
|
||||
wgpu::TextureDataLayout { offset: 0, bytes_per_row: 4 * img_dim.0, rows_per_image: img_dim.1 },
|
||||
texture_size,
|
||||
);
|
||||
|
||||
// Create view and sampler
|
||||
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()
|
||||
}
|
||||
);
|
||||
|
||||
Self { texture, view, sampler }
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user