diff --git a/src/renderer.rs b/src/renderer.rs index 29213de..58da394 100644 --- a/src/renderer.rs +++ b/src/renderer.rs @@ -6,7 +6,7 @@ use wgpu; use crate::{ sprite::Sprite, - utils::{Pixel, Size}, + utils::Size, }; //--Renderer struct--------------------------------------------------------------------------------- @@ -16,6 +16,7 @@ pub struct WgpuRenderer { queue: wgpu::Queue, config: wgpu::SurfaceConfiguration, size: Size, + render_pipeline: wgpu::RenderPipeline, } impl WgpuRenderer { @@ -59,12 +60,67 @@ impl WgpuRenderer { }; surface.configure(&device, &config); + let render_pipeline = { + let shader = device.create_shader_module(wgpu::ShaderModuleDescriptor { + label: Some("test shader"), + source: wgpu::ShaderSource::Wgsl(include_str!("shaders/test_shader.wgsl").into()), + }); + + let render_pipeline_layout = + device.create_pipeline_layout(&wgpu::PipelineLayoutDescriptor { + label: Some("test render pipeline layout"), + bind_group_layouts: &[], + push_constant_ranges: &[], + }); + + device.create_render_pipeline(&wgpu::RenderPipelineDescriptor { + label: Some("test 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: 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, + // multisampling, we don't need it + multisample: wgpu::MultisampleState { + count: 1, + mask: !0, + alpha_to_coverage_enabled: false, + }, + multiview: None, + }) + }; + Ok(Self { surface, device, queue, config, size, + render_pipeline, }) } @@ -99,7 +155,7 @@ impl WgpuRenderer { }); { - 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, @@ -116,6 +172,9 @@ impl WgpuRenderer { })], depth_stencil_attachment: None, }); + + render_pass.set_pipeline(&self.render_pipeline); + render_pass.draw(0..3, 0..1); } self.queue.submit(std::iter::once(encoder.finish())); diff --git a/src/shaders/test_shader.wgsl b/src/shaders/test_shader.wgsl new file mode 100644 index 0000000..052aa94 --- /dev/null +++ b/src/shaders/test_shader.wgsl @@ -0,0 +1,24 @@ +// Vertex shader + +struct VertexOutput { + @builtin(position) clip_position: vec4, +}; + +@vertex +fn vs_main( + @builtin(vertex_index) in_vertex_index: u32, +) -> VertexOutput { + let x = f32(1 - i32(in_vertex_index)) * 0.5; + let y = f32(i32(in_vertex_index & 1u) * 2 - 1) * 0.5; + var out: VertexOutput; + out.clip_position = vec4(x, y, 0.0, 1.0); + return out; +} + +// Fragment shader + +@fragment +fn fs_main(in: VertexOutput) -> @location(0) vec4 { + return vec4(0.3, 0.2, 0.1, 1.0); +} +