From e9c4e9e681db911efadc571ac54f96259753ec19 Mon Sep 17 00:00:00 2001 From: Florian RICHER Date: Thu, 9 Jun 2022 13:38:07 +0200 Subject: [PATCH] Add shader and pipeline --- engine_core/src/shader.wgsl | 19 ++++++++ engine_core/src/state.rs | 90 ++++++++++++++++++++++++++++++------- 2 files changed, 94 insertions(+), 15 deletions(-) create mode 100644 engine_core/src/shader.wgsl diff --git a/engine_core/src/shader.wgsl b/engine_core/src/shader.wgsl new file mode 100644 index 0000000..01557fb --- /dev/null +++ b/engine_core/src/shader.wgsl @@ -0,0 +1,19 @@ +struct VertexOutput { + [[builtin(position)]] clip_position: vec4; +}; + +[[stage(vertex)]] +fn vs_main( + [[builtin(vertex_index)]] in_vertex_index: u32, +) -> 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(x, y, 0.0, 1.0); + return out; +} + +[[stage(fragment)]] +fn fs_main(in: VertexOutput) -> [[location(0)]] vec4 { + return vec4(0.3, 0.2, 0.1, 1.0); +} diff --git a/engine_core/src/state.rs b/engine_core/src/state.rs index e0aeffc..1b74e2b 100644 --- a/engine_core/src/state.rs +++ b/engine_core/src/state.rs @@ -6,6 +6,7 @@ pub struct State { pub queue: wgpu::Queue, pub config: wgpu::SurfaceConfiguration, pub size: winit::dpi::PhysicalSize, + render_pipeline: wgpu::RenderPipeline, } impl State { @@ -59,12 +60,63 @@ impl State { }; surface.configure(&device, &config); + let shader = device.create_shader_module(&wgpu::ShaderModuleDescriptor { + label: Some("Shader"), + source: wgpu::ShaderSource::Wgsl(include_str!("shader.wgsl").into()), + }); + + let render_pipeline_layout = + device.create_pipeline_layout(&wgpu::PipelineLayoutDescriptor { + label: Some("Render Pipeline Layout"), + bind_group_layouts: &[], + push_constant_ranges: &[], + }); + + let render_pipeline = device.create_render_pipeline(&wgpu::RenderPipelineDescriptor { + label: Some("Render Pipeline"), + layout: Some(&render_pipeline_layout), + vertex: wgpu::VertexState { + module: &shader, + entry_point: "vs_main", + buffers: &[], + }, + fragment: Some(wgpu::FragmentState { + module: &shader, + entry_point: "fs_main", + targets: &[wgpu::ColorTargetState { + format: config.format, + blend: Some(wgpu::BlendState::REPLACE), + write_mask: wgpu::ColorWrites::ALL, + }], + }), + primitive: wgpu::PrimitiveState { + topology: wgpu::PrimitiveTopology::TriangleList, + strip_index_format: None, + front_face: wgpu::FrontFace::Ccw, + cull_mode: Some(wgpu::Face::Back), + // Setting this to anything other than Fill requires Features::NON_FILL_POLYGON_MODE + polygon_mode: wgpu::PolygonMode::Fill, + // Requires Features::DEPTH_CLIP_CONTROL + unclipped_depth: false, + // Requires Features::CONSERVATIVE_RASTERIZATION + conservative: false, + }, + depth_stencil: None, + multisample: wgpu::MultisampleState { + count: 1, + mask: !0, + alpha_to_coverage_enabled: false, + }, + multiview: None, + }); + Self { surface, device, queue, config, size, + render_pipeline } } @@ -78,7 +130,7 @@ impl State { } pub fn input(&mut self, event: &WindowEvent) -> bool { - log::info!("{:#?}", event); + // log::info!("{:#?}", event); false } @@ -94,23 +146,31 @@ impl State { }); { - let _render_pass = encoder.begin_render_pass(&wgpu::RenderPassDescriptor { + let mut render_pass = encoder.begin_render_pass(&wgpu::RenderPassDescriptor { label: Some("Render Pass"), - color_attachments: &[wgpu::RenderPassColorAttachment { - view: &view, - resolve_target: None, - ops: wgpu::Operations { - load: wgpu::LoadOp::Clear(wgpu::Color { - r: 0.1, - g: 0.2, - b: 0.3, - a: 1.0, - }), - store: true, - }, - }], + color_attachments: &[ + // This is what [[location(0)]] in the fragment shader targets + wgpu::RenderPassColorAttachment { + view: &view, + resolve_target: None, + ops: wgpu::Operations { + load: wgpu::LoadOp::Clear( + wgpu::Color { + r: 0.1, + g: 0.2, + b: 0.3, + a: 1.0, + } + ), + store: true, + } + } + ], depth_stencil_attachment: None, }); + + render_pass.set_pipeline(&self.render_pipeline); // 2. + render_pass.draw(0..3, 0..1); } // submit will accept anything that implements IntoIter