From 0353b30889b4876fd8c069a36377c53e9a37b458 Mon Sep 17 00:00:00 2001 From: Jonathan Flueren Date: Thu, 16 Mar 2023 18:04:35 +0100 Subject: [PATCH] Add wgsl --- src/main.rs | 58 +++++++++++++++++++++++++++++++++++++++++++++++-- src/shader.wgsl | 27 +++++++++++++++++++++++ 2 files changed, 83 insertions(+), 2 deletions(-) create mode 100644 src/shader.wgsl diff --git a/src/main.rs b/src/main.rs index 0578814..eeac73c 100644 --- a/src/main.rs +++ b/src/main.rs @@ -16,6 +16,7 @@ struct State { queue: wgpu::Queue, config: wgpu::SurfaceConfiguration, size: winit::dpi::PhysicalSize, + render_pipeline: wgpu::RenderPipeline, window: Window, } @@ -63,6 +64,55 @@ impl State { }; surface.configure(&device, &config); + let shader = device.create_shader_module(wgpu::include_wgsl!("shader.wgsl")); + + 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", // 1. + buffers: &[], // 2. + }, + fragment: Some(wgpu::FragmentState { + // 3. + module: &shader, + entry_point: "fs_main", + targets: &[Some(wgpu::ColorTargetState { + // 4. + format: config.format, + blend: Some(wgpu::BlendState::REPLACE), + write_mask: wgpu::ColorWrites::ALL, + })], + }), + primitive: wgpu::PrimitiveState { + topology: wgpu::PrimitiveTopology::TriangleList, // 1. + strip_index_format: None, + front_face: wgpu::FrontFace::Ccw, // 2. + 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, // 1. + multisample: wgpu::MultisampleState { + count: 1, // 2. + mask: !0, // 3. + alpha_to_coverage_enabled: false, // 4. + }, + multiview: None, // 5. + }); + Self { window, surface, @@ -70,6 +120,7 @@ impl State { queue, config, size, + render_pipeline, } } @@ -102,7 +153,7 @@ 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: &[Some(wgpu::RenderPassColorAttachment { view: &view, @@ -111,7 +162,7 @@ impl State { load: wgpu::LoadOp::Clear(wgpu::Color { r: 0.1, g: 0.2, - b: 0.6, + b: 0.3, a: 1.0, }), store: true, @@ -119,6 +170,9 @@ impl State { })], depth_stencil_attachment: None, }); + + render_pass.set_pipeline(&self.render_pipeline); + render_pass.draw(0..3, 0..1); } // submit will accept anything that implements IntoIter diff --git a/src/shader.wgsl b/src/shader.wgsl new file mode 100644 index 0000000..bce42c1 --- /dev/null +++ b/src/shader.wgsl @@ -0,0 +1,27 @@ +// Vertex shader + +struct VertexOutput { + @builtin(position) clip_position: vec4, + @location(0) vert_pos: vec3, +}; + +@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); + out.vert_pos = out.clip_position.xyz; + return out; +} + + + +// Fragment shader + +@fragment +fn fs_main(in: VertexOutput) -> @location(0) vec4 { + return vec4(0.3, 0.2, 0.1, 1.0); +} \ No newline at end of file