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,5 @@
mod state;
pub(self) mod vertex;
use winit::{
event::{ElementState, Event, KeyboardInput, VirtualKeyCode, WindowEvent},

View file

@ -1,19 +1,24 @@
struct VertexInput {
[[location(0)]] position: vec3<f32>;
[[location(1)]] color: vec3<f32>;
};
struct VertexOutput {
[[builtin(position)]] clip_position: vec4<f32>;
[[location(0)]] color: vec3<f32>;
};
[[stage(vertex)]]
fn vs_main(
[[builtin(vertex_index)]] in_vertex_index: u32,
model: VertexInput,
) -> VertexOutput {
var out: VertexOutput;
let x = f32(1 - i32(in_vertex_index)) * 0.5;
let y = f32(i32(in_vertex_index & 1u) * 2 - 1) * 0.5;
out.clip_position = vec4<f32>(x, y, 0.0, 1.0);
out.color = model.color;
out.clip_position = vec4<f32>(model.position, 1.0);
return out;
}
[[stage(fragment)]]
fn fs_main(in: VertexOutput) -> [[location(0)]] vec4<f32> {
return vec4<f32>(0.3, 0.2, 0.1, 1.0);
}
return vec4<f32>(in.color, 1.0);
}

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

41
engine_core/src/vertex.rs Normal file
View file

@ -0,0 +1,41 @@
#[repr(C)]
#[derive(Copy, Clone, Debug, bytemuck::Pod, bytemuck::Zeroable)]
pub struct Vertex {
pub position: [f32; 3],
pub color: [f32; 3],
}
impl Vertex {
const ATTRIBS: [wgpu::VertexAttribute; 2] =
wgpu::vertex_attr_array![0 => Float32x3, 1 => Float32x3];
pub fn desc<'a>() -> wgpu::VertexBufferLayout<'a> {
use std::mem;
wgpu::VertexBufferLayout {
array_stride: mem::size_of::<Self>() as wgpu::BufferAddress,
step_mode: wgpu::VertexStepMode::Vertex,
attributes: &Self::ATTRIBS,
}
}
// pub fn desc<'a>() -> wgpu::VertexBufferLayout<'a> {
// wgpu::VertexBufferLayout {
// array_stride: std::mem::size_of::<Vertex>() as wgpu::BufferAddress,
// step_mode: wgpu::VertexStepMode::Vertex,
// attributes: &[
// wgpu::VertexAttribute {
// offset: 0,
// shader_location: 0,
// format: wgpu::VertexFormat::Float32x3,
// },
// wgpu::VertexAttribute {
// offset: std::mem::size_of::<[f32; 3]>() as wgpu::BufferAddress,
// shader_location: 1,
// format: wgpu::VertexFormat::Float32x3,
// }
// ]
// }
// }
}