diff --git a/src/lib.rs b/src/lib.rs index 56d8562..466bd69 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -13,6 +13,7 @@ struct State { queue: wgpu::Queue, config: wgpu::SurfaceConfiguration, size: winit::dpi::PhysicalSize, + render_pipeline: wgpu::RenderPipeline, window: Window, } @@ -74,12 +75,57 @@ 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", + 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, + cull_mode: Some(wgpu::Face::Back), + polygon_mode: wgpu::PolygonMode::Fill, + unclipped_depth: false, + conservative: false, + front_face: wgpu::FrontFace::Ccw, + }, + 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, window, } } @@ -118,18 +164,20 @@ impl State { 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, resolve_target: None, ops: wgpu::Operations { - load: wgpu::LoadOp::Clear(wgpu::Color { - r: 0.1, - g: 0.2, - b: 0.3, - a: 1.0, - }), + load: wgpu::LoadOp::Clear( + wgpu::Color { + r: 0.1, + g: 0.2, + b: 0.3, + a: 1.0, + } + ), store: wgpu::StoreOp::Store, }, })], @@ -138,7 +186,10 @@ impl State { timestamp_writes: None, }); - drop(_render_pass); + render_pass.set_pipeline(&self.render_pipeline); + render_pass.draw(0..3, 0..1); + + drop(render_pass); self.queue.submit(std::iter::once(encoder.finish())); output.present(); diff --git a/src/shader.wgsl b/src/shader.wgsl new file mode 100644 index 0000000..e66e66e --- /dev/null +++ b/src/shader.wgsl @@ -0,0 +1,22 @@ +// Vertex shader + +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 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