1
0
Fork 0

[TUTORIEL4] Buffers finish

This commit is contained in:
Florian RICHER 2022-06-09 22:49:49 +02:00
parent e9c4e9e681
commit 460dbc5a77
8 changed files with 2019 additions and 1827 deletions

View file

@ -1,4 +1,20 @@
use wgpu::util::DeviceExt;
use winit::{window::Window, event::WindowEvent};
use super::vertex::Vertex;
const VERTICES: &[Vertex] = &[
Vertex { position: [-0.0868241, 0.49240386, 0.0], color: [1.0, 0.0, 0.0] }, // A
Vertex { position: [-0.49513406, 0.06958647, 0.0], color: [0.0, 1.0, 0.0] }, // B
Vertex { position: [-0.21918549, -0.44939706, 0.0], color: [0.0, 1.0, 0.0] }, // C
Vertex { position: [0.35966998, -0.3473291, 0.0], color: [1.0, 0.0, 0.0] }, // D
Vertex { position: [0.44147372, 0.2347359, 0.0], color: [0.0, 0.0, 1.0] }, // E
];
const INDICES: &[u16] = &[
0, 1, 4,
1, 2, 4,
2, 3, 4,
];
pub struct State {
pub surface: wgpu::Surface,
@ -7,6 +23,10 @@ pub struct State {
pub config: wgpu::SurfaceConfiguration,
pub size: winit::dpi::PhysicalSize<u32>,
render_pipeline: wgpu::RenderPipeline,
vertex_buffer: wgpu::Buffer,
// num_vertices: u32,
index_buffer: wgpu::Buffer,
num_indices: u32,
}
impl State {
@ -59,6 +79,25 @@ impl State {
present_mode: wgpu::PresentMode::Fifo,
};
surface.configure(&device, &config);
let vertex_buffer = device.create_buffer_init(
&wgpu::util::BufferInitDescriptor {
label: Some("Vertex Buffer"),
contents: bytemuck::cast_slice(VERTICES),
usage: wgpu::BufferUsages::VERTEX,
}
);
// let num_vertices = VERTICES.len() as u32;
let index_buffer = device.create_buffer_init(
&wgpu::util::BufferInitDescriptor {
label: Some("Index Buffer"),
contents: bytemuck::cast_slice(INDICES),
usage: wgpu::BufferUsages::INDEX,
}
);
let num_indices = INDICES.len() as u32;
let shader = device.create_shader_module(&wgpu::ShaderModuleDescriptor {
label: Some("Shader"),
@ -78,7 +117,9 @@ impl State {
vertex: wgpu::VertexState {
module: &shader,
entry_point: "vs_main",
buffers: &[],
buffers: &[
Vertex::desc(),
],
},
fragment: Some(wgpu::FragmentState {
module: &shader,
@ -116,7 +157,11 @@ impl State {
queue,
config,
size,
render_pipeline
render_pipeline,
vertex_buffer,
// num_vertices,
index_buffer,
num_indices,
}
}
@ -129,7 +174,7 @@ impl State {
}
}
pub fn input(&mut self, event: &WindowEvent) -> bool {
pub fn input(&mut self, _event: &WindowEvent) -> bool {
// log::info!("{:#?}", event);
false
}
@ -164,13 +209,17 @@ impl State {
),
store: true,
}
}
},
],
depth_stencil_attachment: None,
});
render_pass.set_pipeline(&self.render_pipeline); // 2.
render_pass.draw(0..3, 0..1);
render_pass.set_pipeline(&self.render_pipeline);
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(0..self.num_vertices, 0..1);
render_pass.draw_indexed(0..self.num_indices, 0, 0..1);
}
// submit will accept anything that implements IntoIter