From 1e98a40a72ace612f32850b41bb400809376fa75 Mon Sep 17 00:00:00 2001 From: Jarrod Doyle Date: Mon, 16 Oct 2023 10:30:59 +0100 Subject: [PATCH] Basic triangle render pass --- res/shaders/main.wgsl | 19 +++++++++++++++ src/main.rs | 56 +++++++++++++++++++++++++++++++++++++++++-- 2 files changed, 73 insertions(+), 2 deletions(-) create mode 100644 res/shaders/main.wgsl diff --git a/res/shaders/main.wgsl b/res/shaders/main.wgsl new file mode 100644 index 0000000..f5a0eae --- /dev/null +++ b/res/shaders/main.wgsl @@ -0,0 +1,19 @@ +struct VertexOutput { + @builtin(position) clip_position: vec4, +}; + +@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; +} + +@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 diff --git a/src/main.rs b/src/main.rs index 0bf2780..061d050 100644 --- a/src/main.rs +++ b/src/main.rs @@ -17,6 +17,55 @@ pub async fn run() { let mut render_ctx = gfx::Context::new(window, wgpu::Limits::default()).await; + let shader = render_ctx + .device + .create_shader_module(wgpu::include_wgsl!("../res/shaders/main.wgsl")); + let render_pipeline_layout = + render_ctx + .device + .create_pipeline_layout(&wgpu::PipelineLayoutDescriptor { + label: Some("RP Layout"), + bind_group_layouts: &[], + push_constant_ranges: &[], + }); + let render_pipeline = + render_ctx + .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: &[Some(wgpu::ColorTargetState { + format: render_ctx.surface_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), + unclipped_depth: false, + polygon_mode: wgpu::PolygonMode::Fill, + conservative: false, + }, + depth_stencil: None, + multisample: wgpu::MultisampleState { + count: 1, + mask: !0, + alpha_to_coverage_enabled: false, + }, + multiview: None, + }); + event_loop.run(move |event, _, control_flow| match event { Event::WindowEvent { ref event, @@ -57,7 +106,7 @@ pub async fn run() { label: Some("Render encoder"), }); - 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, @@ -69,7 +118,10 @@ pub async fn run() { })], depth_stencil_attachment: None, }); - drop(_render_pass); + render_pass.set_pipeline(&render_pipeline); + render_pass.draw(0..3, 0..1); + + drop(render_pass); render_ctx.queue.submit(std::iter::once(encoder.finish())); output.present();