|
@@ -1,7 +1,7 @@
|
|
|
use std::iter;
|
|
use std::iter;
|
|
|
|
|
|
|
|
-use wgpu::util::{DeviceExt, RenderEncoder};
|
|
|
|
|
-
|
|
|
|
|
|
|
+use wgpu::util::DeviceExt;
|
|
|
|
|
+mod texture;
|
|
|
use winit::{
|
|
use winit::{
|
|
|
event::*,
|
|
event::*,
|
|
|
event_loop::EventLoop,
|
|
event_loop::EventLoop,
|
|
@@ -13,7 +13,7 @@ use winit::{
|
|
|
#[derive(Clone, Copy, Debug, bytemuck::Pod, bytemuck::Zeroable)]
|
|
#[derive(Clone, Copy, Debug, bytemuck::Pod, bytemuck::Zeroable)]
|
|
|
struct Vertex {
|
|
struct Vertex {
|
|
|
position: [f32; 3],
|
|
position: [f32; 3],
|
|
|
- color: [f32; 3],
|
|
|
|
|
|
|
+ tex_coords: [f32; 2],
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
@@ -31,7 +31,7 @@ impl Vertex {
|
|
|
wgpu::VertexAttribute {
|
|
wgpu::VertexAttribute {
|
|
|
offset: std::mem::size_of::<[f32; 3]>() as wgpu::BufferAddress,
|
|
offset: std::mem::size_of::<[f32; 3]>() as wgpu::BufferAddress,
|
|
|
shader_location: 1,
|
|
shader_location: 1,
|
|
|
- format: wgpu::VertexFormat::Float32x3,
|
|
|
|
|
|
|
+ format: wgpu::VertexFormat::Float32x2,
|
|
|
},
|
|
},
|
|
|
],
|
|
],
|
|
|
}
|
|
}
|
|
@@ -39,14 +39,18 @@ impl Vertex {
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
+// Changed
|
|
|
const VERTICES: &[Vertex] = &[
|
|
const VERTICES: &[Vertex] = &[
|
|
|
- Vertex { position: [-0.0868241, 0.49240386, 0.0], color: [0.5, 0.0, 0.5] }, // A
|
|
|
|
|
- Vertex { position: [-0.49513406, 0.06958647, 0.0], color: [0.5, 0.0, 0.5] }, // B
|
|
|
|
|
- Vertex { position: [-0.21918549, -0.44939706, 0.0], color: [0.5, 0.0, 0.5] }, // C
|
|
|
|
|
- Vertex { position: [0.35966998, -0.3473291, 0.0], color: [0.5, 0.0, 0.5] }, // D
|
|
|
|
|
- Vertex { position: [0.44147372, 0.2347359, 0.0], color: [0.5, 0.0, 0.5] }, // E
|
|
|
|
|
|
|
+ // Changed
|
|
|
|
|
+ Vertex { position: [-0.0868241, 0.49240386, 0.0], tex_coords: [0.4131759, 0.00759614], }, // A
|
|
|
|
|
+ Vertex { position: [-0.49513406, 0.06958647, 0.0], tex_coords: [0.0048659444, 0.43041354], }, // B
|
|
|
|
|
+ Vertex { position: [-0.21918549, -0.44939706, 0.0], tex_coords: [0.28081453, 0.949397], }, // C
|
|
|
|
|
+ Vertex { position: [0.35966998, -0.3473291, 0.0], tex_coords: [0.85967, 0.84732914], }, // D
|
|
|
|
|
+ Vertex { position: [0.44147372, 0.2347359, 0.0], tex_coords: [0.9414737, 0.2652641], }, // E
|
|
|
];
|
|
];
|
|
|
|
|
|
|
|
|
|
+
|
|
|
|
|
+
|
|
|
const INDICES: &[u16] = &[
|
|
const INDICES: &[u16] = &[
|
|
|
0, 1, 4,
|
|
0, 1, 4,
|
|
|
1, 2, 4,
|
|
1, 2, 4,
|
|
@@ -69,8 +73,11 @@ struct State<'a> {
|
|
|
|
|
|
|
|
vertex_buffer: wgpu::Buffer,
|
|
vertex_buffer: wgpu::Buffer,
|
|
|
index_buffer: wgpu::Buffer,
|
|
index_buffer: wgpu::Buffer,
|
|
|
- num_vertices: u32,
|
|
|
|
|
|
|
+ //num_vertices: u32,
|
|
|
num_indices: u32,
|
|
num_indices: u32,
|
|
|
|
|
+
|
|
|
|
|
+ diffuse_bind_group: wgpu::BindGroup,
|
|
|
|
|
+ diffuse_texture: texture::Texture,
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
impl<'a> State<'a> {
|
|
impl<'a> State<'a> {
|
|
@@ -137,7 +144,59 @@ impl<'a> State<'a> {
|
|
|
};
|
|
};
|
|
|
surface.configure(&device, &config);
|
|
surface.configure(&device, &config);
|
|
|
|
|
|
|
|
|
|
+ //Texture
|
|
|
|
|
+ let diffuse_bytes = include_bytes!("happy-tree.png");
|
|
|
|
|
+ let diffuse_texture = texture::Texture::from_bytes(&device, &queue, diffuse_bytes, "happy-tree.png").unwrap();
|
|
|
|
|
+ //BindGroup
|
|
|
|
|
+
|
|
|
|
|
+ let texture_bind_group_layout =
|
|
|
|
|
+ device.create_bind_group_layout(
|
|
|
|
|
+ &wgpu::BindGroupLayoutDescriptor{
|
|
|
|
|
+ label: Some("Texture Bind Group Layout"),
|
|
|
|
|
+ entries: &[
|
|
|
|
|
+ wgpu::BindGroupLayoutEntry{
|
|
|
|
|
+ binding: 0,
|
|
|
|
|
+ visibility: wgpu::ShaderStages::FRAGMENT,
|
|
|
|
|
+ ty: wgpu::BindingType::Texture{
|
|
|
|
|
+ sample_type: wgpu::TextureSampleType::Float{filterable: true},
|
|
|
|
|
+ view_dimension: wgpu::TextureViewDimension::D2,
|
|
|
|
|
+ multisampled: false,
|
|
|
|
|
+ },
|
|
|
|
|
+ count: None,
|
|
|
|
|
+ },
|
|
|
|
|
+ wgpu::BindGroupLayoutEntry{
|
|
|
|
|
+ binding: 1,
|
|
|
|
|
+ visibility: wgpu::ShaderStages::FRAGMENT,
|
|
|
|
|
+ ty: wgpu::BindingType::Sampler(
|
|
|
|
|
+ wgpu::SamplerBindingType::Filtering,
|
|
|
|
|
+ ),
|
|
|
|
|
+ count: None,
|
|
|
|
|
+ }
|
|
|
|
|
+ ]
|
|
|
|
|
+ }
|
|
|
|
|
+ );
|
|
|
|
|
+
|
|
|
|
|
+ //Creation of the real bind group
|
|
|
|
|
|
|
|
|
|
+ let diffuse_bind_group = device.create_bind_group(
|
|
|
|
|
+ &wgpu::BindGroupDescriptor {
|
|
|
|
|
+ layout: &texture_bind_group_layout,
|
|
|
|
|
+ entries: &[
|
|
|
|
|
+ wgpu::BindGroupEntry {
|
|
|
|
|
+ binding: 0,
|
|
|
|
|
+ resource: wgpu::BindingResource::TextureView(&diffuse_texture.view), // CHANGED!
|
|
|
|
|
+ },
|
|
|
|
|
+ wgpu::BindGroupEntry {
|
|
|
|
|
+ binding: 1,
|
|
|
|
|
+ resource: wgpu::BindingResource::Sampler(&diffuse_texture.sampler), // CHANGED!
|
|
|
|
|
+ }
|
|
|
|
|
+ ],
|
|
|
|
|
+ label: Some("diffuse_bind_group"),
|
|
|
|
|
+ }
|
|
|
|
|
+ );
|
|
|
|
|
+
|
|
|
|
|
+
|
|
|
|
|
+ //Shader and Pipeline
|
|
|
let shader = device.create_shader_module(wgpu::ShaderModuleDescriptor {
|
|
let shader = device.create_shader_module(wgpu::ShaderModuleDescriptor {
|
|
|
label: Some("Shader"),
|
|
label: Some("Shader"),
|
|
|
source: wgpu::ShaderSource::Wgsl(include_str!("shader.wgsl").into()),
|
|
source: wgpu::ShaderSource::Wgsl(include_str!("shader.wgsl").into()),
|
|
@@ -146,7 +205,9 @@ impl<'a> State<'a> {
|
|
|
let render_pipeline_layout =
|
|
let render_pipeline_layout =
|
|
|
device.create_pipeline_layout(&wgpu::PipelineLayoutDescriptor {
|
|
device.create_pipeline_layout(&wgpu::PipelineLayoutDescriptor {
|
|
|
label: Some("Render Pipeline Layout"),
|
|
label: Some("Render Pipeline Layout"),
|
|
|
- bind_group_layouts: &[],
|
|
|
|
|
|
|
+ bind_group_layouts: &[
|
|
|
|
|
+ &texture_bind_group_layout,
|
|
|
|
|
+ ],
|
|
|
push_constant_ranges: &[],
|
|
push_constant_ranges: &[],
|
|
|
});
|
|
});
|
|
|
|
|
|
|
@@ -204,7 +265,7 @@ impl<'a> State<'a> {
|
|
|
}
|
|
}
|
|
|
);
|
|
);
|
|
|
|
|
|
|
|
- let num_vertices = VERTICES.len() as u32;
|
|
|
|
|
|
|
+ //let num_vertices = VERTICES.len() as u32;
|
|
|
let num_indices = INDICES.len() as u32;
|
|
let num_indices = INDICES.len() as u32;
|
|
|
Self {
|
|
Self {
|
|
|
surface,
|
|
surface,
|
|
@@ -216,8 +277,10 @@ impl<'a> State<'a> {
|
|
|
render_pipeline,
|
|
render_pipeline,
|
|
|
vertex_buffer,
|
|
vertex_buffer,
|
|
|
index_buffer,
|
|
index_buffer,
|
|
|
- num_vertices,
|
|
|
|
|
|
|
+ //num_vertices,
|
|
|
num_indices,
|
|
num_indices,
|
|
|
|
|
+ diffuse_bind_group,
|
|
|
|
|
+ diffuse_texture,
|
|
|
}
|
|
}
|
|
|
}
|
|
}
|
|
|
|
|
|
|
@@ -275,6 +338,7 @@ impl<'a> State<'a> {
|
|
|
});
|
|
});
|
|
|
|
|
|
|
|
render_pass.set_pipeline(&self.render_pipeline);
|
|
render_pass.set_pipeline(&self.render_pipeline);
|
|
|
|
|
+ render_pass.set_bind_group(0, &self.diffuse_bind_group, &[]);
|
|
|
|
|
|
|
|
render_pass.set_vertex_buffer(0, self.vertex_buffer.slice(..));
|
|
render_pass.set_vertex_buffer(0, self.vertex_buffer.slice(..));
|
|
|
render_pass.set_index_buffer(self.index_buffer.slice(..), wgpu::IndexFormat::Uint16);
|
|
render_pass.set_index_buffer(self.index_buffer.slice(..), wgpu::IndexFormat::Uint16);
|