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

45
.vscode/launch.json vendored Normal file
View file

@ -0,0 +1,45 @@
{
// Utilisez IntelliSense pour en savoir plus sur les attributs possibles.
// Pointez pour afficher la description des attributs existants.
// Pour plus d'informations, visitez : https://go.microsoft.com/fwlink/?linkid=830387
"version": "0.2.0",
"configurations": [
{
"type": "lldb",
"request": "launch",
"name": "Debug executable 'tuto1'",
"cargo": {
"args": [
"build",
"--bin=tuto1",
"--package=tuto1"
],
"filter": {
"name": "tuto1",
"kind": "bin"
}
},
"args": [],
"cwd": "${workspaceFolder}"
},
{
"type": "lldb",
"request": "launch",
"name": "Debug unit tests in executable 'tuto1'",
"cargo": {
"args": [
"test",
"--no-run",
"--bin=tuto1",
"--package=tuto1"
],
"filter": {
"name": "tuto1",
"kind": "bin"
}
},
"args": [],
"cwd": "${workspaceFolder}"
}
]
}

44
Cargo.lock generated
View file

@ -91,9 +91,23 @@ checksum = "9c59e7af012c713f529e7a3ee57ce9b31ddd858d4b512923602f74608b009631"
[[package]]
name = "bytemuck"
version = "1.7.2"
version = "1.9.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "72957246c41db82b8ef88a5486143830adeb8227ef9837740bdec67724cf2c5b"
checksum = "cdead85bdec19c194affaeeb670c0e41fe23de31459efd1c174d049269cf02cc"
dependencies = [
"bytemuck_derive",
]
[[package]]
name = "bytemuck_derive"
version = "1.1.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "562e382481975bc61d11275ac5e62a19abd00b0547d99516a415336f183dcd0e"
dependencies = [
"proc-macro2",
"quote",
"syn",
]
[[package]]
name = "byteorder"
@ -192,6 +206,26 @@ version = "1.1.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "3d7b894f5411737b7867f4827955924d7c254fc9f4d91a6aad6b097804b1018b"
[[package]]
name = "console_error_panic_hook"
version = "0.1.7"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "a06aeb73f470f66dcdbf7223caeebb85984942f22f1adb2a088cf9668146bbbc"
dependencies = [
"cfg-if 1.0.0",
"wasm-bindgen",
]
[[package]]
name = "console_log"
version = "0.2.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "501a375961cef1a0d44767200e66e4a559283097e91d0730b1d75dfb2f8a1494"
dependencies = [
"log",
"web-sys",
]
[[package]]
name = "copyless"
version = "0.1.5"
@ -436,9 +470,15 @@ checksum = "e78d4f1cc4ae33bbfc157ed5d5a5ef3bc29227303d595861deb238fcec4e9457"
name = "engine_core"
version = "0.0.1"
dependencies = [
"bytemuck",
"cgmath",
"console_error_panic_hook",
"console_log",
"image",
"log",
"wasm-bindgen",
"wasm-bindgen-futures",
"web-sys",
"wgpu",
"winit",
]

3622
engine_core/Cargo.lock generated

File diff suppressed because it is too large Load diff

View file

@ -11,4 +11,17 @@ log = "0.4.17"
image = "0.24.2"
winit = "0.26.1"
cgmath = "0.18"
wgpu = "0.12.0"
wgpu = "0.12.0"
bytemuck = { version = "1.9.1", features = [ "derive" ] }
[target.'cfg(target_arch = "wasm32")'.dependencies]
console_error_panic_hook = "0.1.6"
console_log = "0.2.0"
wgpu = { version = "0.12", features = ["webgl"]}
wasm-bindgen = "0.2"
wasm-bindgen-futures = "0.4"
web-sys = { version = "0.3", features = [
"Document",
"Window",
"Element",
]}

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,
// }
// ]
// }
// }
}