From 81196fb9ca856f50ffe1d0d131d45248175453e9 Mon Sep 17 00:00:00 2001 From: Piotrek Date: Mon, 12 Apr 2021 14:01:02 +0200 Subject: [PATCH] index buffer --- Cargo.lock | 21 +++++++++++++++++ Cargo.toml | 2 ++ src/.bin/shader.frag.spv | Bin 452 -> 572 bytes src/.bin/shader.vert.spv | Bin 1164 -> 1056 bytes src/app.rs | 49 +++++++++++++++++++++++++++++++++++---- src/main.rs | 2 ++ src/shader.frag | 5 ++-- src/shader.vert | 19 ++++++--------- src/vertex.rs | 31 +++++++++++++++++++++++++ 9 files changed, 110 insertions(+), 19 deletions(-) create mode 100644 src/vertex.rs diff --git a/Cargo.lock b/Cargo.lock index 4b4fda5..0d37d52 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -85,6 +85,26 @@ version = "3.6.1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "63396b8a4b9de3f4fdfb320ab6080762242f66a8ef174c49d8e19b674db4cdbe" +[[package]] +name = "bytemuck" +version = "1.5.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "bed57e2090563b83ba8f83366628ce535a7584c9afa4c9fc0612a03925c6df58" +dependencies = [ + "bytemuck_derive", +] + +[[package]] +name = "bytemuck_derive" +version = "1.0.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "8e215f8c2f9f79cb53c8335e687ffd07d5bfcb6fe5fc80723762d0be46e7cc54" +dependencies = [ + "proc-macro2", + "quote", + "syn", +] + [[package]] name = "byteorder" version = "1.4.3" @@ -1677,6 +1697,7 @@ name = "wgpufun" version = "0.1.0" dependencies = [ "anyhow", + "bytemuck", "fs_extra", "futures", "glob", diff --git a/Cargo.toml b/Cargo.toml index 0135c0f..d321f23 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -10,6 +10,8 @@ edition = "2018" winit = "0.24" wgpu = "0.7" futures = "0.3" +bytemuck = { version = "1.4", features = [ "derive" ] } + [build-dependencies] anyhow = "1.0" diff --git a/src/.bin/shader.frag.spv b/src/.bin/shader.frag.spv index 232ea0e81b049a49f19714079a560760dd52c95b..108354fcf397da1b494f0103f3b3feffc5e9c54e 100644 GIT binary patch literal 572 zcmYk2PfNo<6vUrx)7IAhv!FMrcod2UErO^g$t6(54-iTcB@o+^#)3CLo1e;y;QZD^ zx-fbBX6Nn9rd~U1neAD}dN#JdHL{u*v#vE=^@HbNx+&u6<<$j>zSW%&&A?jLPz7&` zbjiWKa3Bn&gO`DJb=_+ArJ;u0{2=t_b3b?pqxCx66dzd>* z8D;PBuE3LG*Vi-8dB5a~*DzXTtK7tRF3UE_i;D9#MaC0>MNi(UA1f(YPhDzr`y=g} z;wHke^t_24|B1eBRsIV6P1)lChrXkVo*sQyRlmaV=;_G-cdj07UoiW;pXm+c!HdNm z-(oz~Td~~T6PSMz%j}@!RQ|bP$>CAyMkUAnk4jJeLxFkpoRi~DuDpk{AEmq&egTNU BF<<}y delta 210 zcmdnPa)g$A%UKqh1hvSnFWe8GK|&zLV9&sUqHi~8=TY2Dn{5*k6gphy5wMEQNy|$b-6!oQv6FX&LVGwqxn{@Z zu9*oPDfrA<;4aG_-KP}trk7(j<*riKx^|uXIg6A39GfEdmZtx?K>J`f95k{cX>9qP zp(bO8+Yoe!*(WhNZuN#4IN%(Qkp~;(p$`l#o`(;{4nt+A2ajIp@x2#>g71pM8~kN? z72Q?Yiun2mDq`|g#iP3M!B6`!^Z5L@eDwJ|vDZw!DaF)B+me2#bysDa{eCHz6LoAUGuupKptJhdB^d`kI@yiS({oj3_H{x1EqTYi4_nA;;pOi88xw7ns*ymQ{)9XPm=(8pR<9KXcJ{WpDw$aDveXlNP c*_81fK6x}>sxEr@&g_6W(LoIVS5;4Bf34_P5C8xG literal 1164 zcmYk4U279j5Qa~iCatY)t+lNmwWiiDREifWf@l$yCGMSlo-gD+X=cF;aHy^@GSPaYIUZ`v>%z}il6k28MbdS6H<6^M?_{k#~ zE1^*m<*WvlV{`bIUd+vx@oRX4Z6tb4cy>6InD_ZWMNPgP@Mzil zlsWJnnA){E)>w7_zRZ7~y5y;$=9!CmCmlNG@;V^uu)dG4gNpCMagF&VS&P`lx8VDd z*6y{z?*g%nYY!&%IdU~^Q!f(bi|vp(tKP$&-iCLcSnGT~sl6{T=d6^xYG3DAui~1z zE)bog=32)Zx$D(&ktj#ZzT95wAI$UO6}G+CbimXJvYfi_<3Oe4w>h5%~Q_cUHDF# z_#coa(OK^8e@*TSYBg-*&VLEpo|<2=!nti+Opbgt?BZ?w103&j4;RyF>=wD$41UBk d=iSET+Vg%rgLBk>7x&$LTXk5=|H}@~@P8Y1S Self { // Select backend - let backend = wgpu::BackendBit::DX12; + let backend = wgpu::BackendBit::PRIMARY; println!("Backend: {:?}", backend); // Create surface and pick adapter (physical gpu) @@ -57,7 +76,7 @@ impl App { let vert_state = wgpu::VertexState { //vert module: &device.create_shader_module(&wgpu::include_spirv!(".bin\\shader.vert.spv")), entry_point: "main", - buffers: &[], + buffers: &[Vertex::desc()], }; let frag_state = wgpu::FragmentState { // frag module: &device.create_shader_module(&wgpu::include_spirv!(".bin\\shader.frag.spv")), @@ -93,8 +112,26 @@ impl App { multisample: multisample_state, }); + // Vertex buffer + let vertex_buffer = device.create_buffer_init( + &wgpu::util::BufferInitDescriptor { + label: Some("Vertex Buffer"), + contents: bytemuck::cast_slice(VERTICES), + usage: wgpu::BufferUsage::VERTEX, + } + ); + + // Index buffer + let index_buffer = device.create_buffer_init( + &wgpu::util::BufferInitDescriptor { + label: Some("Index Buffer"), + contents: bytemuck::cast_slice(INDICES), + usage: wgpu::BufferUsage::INDEX, + } + ); + // Save values in app - Self { surface, device, queue, swapchain_desc, swapchain, pipeline } + Self { surface, device, queue, swapchain_desc, swapchain, pipeline, vertex_buffer, index_buffer } } pub fn resize(&mut self, new_size: Option>) { @@ -145,7 +182,9 @@ impl App { }); render_pass.set_pipeline(&self.pipeline); - render_pass.draw(0..3, 0..1); + 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); } // Submit encoder (command buffer) diff --git a/src/main.rs b/src/main.rs index 287c5e8..d268365 100644 --- a/src/main.rs +++ b/src/main.rs @@ -5,6 +5,7 @@ use winit::{ dpi::LogicalSize }; +mod vertex; mod app; use app::App; @@ -68,6 +69,7 @@ fn main() { // The system is out of memory, we should probably quit Err(wgpu::SwapChainError::OutOfMemory) => *c = ControlFlow::Exit, // All other errors (Outdated, Timeout) should be resolved by the next frame + Err(wgpu::SwapChainError::Outdated) => (), Err(e) => eprintln!("{:?}", e), } } diff --git a/src/shader.frag b/src/shader.frag index 237a15b..9f09429 100644 --- a/src/shader.frag +++ b/src/shader.frag @@ -1,9 +1,10 @@ // shader.frag #version 450 -layout(location=0) out vec4 f_color; +layout(location=0) in vec3 vert_color; +layout(location=0) out vec4 frag_color; void main() { - f_color = vec4(0.3, 0.2, 0.1, 1.0); + frag_color = vec4(vert_color, 1.0); } \ No newline at end of file diff --git a/src/shader.vert b/src/shader.vert index 189f5c2..ecf2291 100644 --- a/src/shader.vert +++ b/src/shader.vert @@ -1,17 +1,12 @@ -// shader.vert #version 450 -const vec2 positions[3] = vec2[3]( - vec2(0.0, 0.5), - vec2(-0.5, -0.5), - vec2(0.5, -0.5) -); +layout(location=0) in vec3 in_position; +layout(location=1) in vec3 in_color; +layout(location=0) out vec3 vert_color; + void main() { - gl_Position = vec4(positions[gl_VertexIndex], 0.0, 1.0); -} - - - - \ No newline at end of file + vert_color = in_color; + gl_Position = vec4(in_position, 1.0); +} \ No newline at end of file diff --git a/src/vertex.rs b/src/vertex.rs new file mode 100644 index 0000000..8f26feb --- /dev/null +++ b/src/vertex.rs @@ -0,0 +1,31 @@ + + +#[repr(C)] +#[derive(Copy, Clone, Debug, bytemuck::Pod, bytemuck::Zeroable)] +pub struct Vertex { + pub position: [f32; 3], + pub color: [f32; 3], +} + +impl Vertex { + pub fn desc<'a>() -> wgpu::VertexBufferLayout<'a> { + wgpu::VertexBufferLayout { + array_stride: std::mem::size_of::() as wgpu::BufferAddress, + step_mode: wgpu::InputStepMode::Vertex, + attributes: &[ + // position + wgpu::VertexAttribute { + offset: 0, + shader_location: 0, + format: wgpu::VertexFormat::Float3, + }, + // color + wgpu::VertexAttribute { + offset: std::mem::size_of::<[f32; 3]>() as wgpu::BufferAddress, + shader_location: 1, + format: wgpu::VertexFormat::Float3, + } + ] + } + } +} \ No newline at end of file