|
| 1 | +use mygraphics_shaders::ShaderConstants; |
| 2 | +use wgpu::{ |
| 3 | + ColorTargetState, ColorWrites, Device, FragmentState, FrontFace, MultisampleState, |
| 4 | + PipelineLayoutDescriptor, PolygonMode, PrimitiveState, PrimitiveTopology, PushConstantRange, |
| 5 | + RenderPass, RenderPipeline, RenderPipelineDescriptor, ShaderModuleDescriptorPassthrough, |
| 6 | + ShaderRuntimeChecks, ShaderStages, TextureFormat, VertexState, |
| 7 | +}; |
| 8 | + |
| 9 | +pub struct MyRenderPipeline { |
| 10 | + pipeline: RenderPipeline, |
| 11 | +} |
| 12 | + |
| 13 | +impl MyRenderPipeline { |
| 14 | + pub fn new(device: &Device, out_format: TextureFormat) -> anyhow::Result<Self> { |
| 15 | + // Workaround in wgpu 27.0.1 where the macro expansion of `include_spirv_raw!` doesn't compile |
| 16 | + // see https://github.com/gfx-rs/wgpu/pull/8250 |
| 17 | + // let module = unsafe { |
| 18 | + // device.create_shader_module_passthrough(include_spirv_raw!(env!("SHADER_SPV_PATH"))) |
| 19 | + // }; |
| 20 | + let module = unsafe { |
| 21 | + device.create_shader_module_passthrough(ShaderModuleDescriptorPassthrough { |
| 22 | + label: Some(env!("SHADER_SPV_PATH")), |
| 23 | + entry_point: "".to_owned(), |
| 24 | + num_workgroups: (0, 0, 0), |
| 25 | + runtime_checks: ShaderRuntimeChecks::unchecked(), |
| 26 | + spirv: Some(wgpu::util::make_spirv_raw(include_bytes!(env!( |
| 27 | + "SHADER_SPV_PATH" |
| 28 | + )))), |
| 29 | + dxil: None, |
| 30 | + msl: None, |
| 31 | + hlsl: None, |
| 32 | + glsl: None, |
| 33 | + wgsl: None, |
| 34 | + }) |
| 35 | + }; |
| 36 | + |
| 37 | + let layout = device.create_pipeline_layout(&PipelineLayoutDescriptor { |
| 38 | + label: Some("MyRenderPipeline layout"), |
| 39 | + bind_group_layouts: &[], |
| 40 | + push_constant_ranges: &[PushConstantRange { |
| 41 | + stages: ShaderStages::VERTEX_FRAGMENT, |
| 42 | + range: 0..size_of::<ShaderConstants>() as u32, |
| 43 | + }], |
| 44 | + }); |
| 45 | + |
| 46 | + Ok(Self { |
| 47 | + pipeline: device.create_render_pipeline(&RenderPipelineDescriptor { |
| 48 | + label: Some("MyRenderPipeline"), |
| 49 | + layout: Some(&layout), |
| 50 | + vertex: VertexState { |
| 51 | + module: &module, |
| 52 | + entry_point: Some("main_vs"), |
| 53 | + compilation_options: Default::default(), |
| 54 | + buffers: &[], |
| 55 | + }, |
| 56 | + primitive: PrimitiveState { |
| 57 | + topology: PrimitiveTopology::TriangleList, |
| 58 | + strip_index_format: None, |
| 59 | + front_face: FrontFace::Ccw, |
| 60 | + cull_mode: None, |
| 61 | + unclipped_depth: false, |
| 62 | + polygon_mode: PolygonMode::Fill, |
| 63 | + conservative: false, |
| 64 | + }, |
| 65 | + depth_stencil: None, |
| 66 | + multisample: MultisampleState::default(), |
| 67 | + fragment: Some(FragmentState { |
| 68 | + module: &module, |
| 69 | + entry_point: Some("main_fs"), |
| 70 | + compilation_options: Default::default(), |
| 71 | + targets: &[Some(ColorTargetState { |
| 72 | + format: out_format, |
| 73 | + blend: None, |
| 74 | + write_mask: ColorWrites::ALL, |
| 75 | + })], |
| 76 | + }), |
| 77 | + multiview: None, |
| 78 | + cache: None, |
| 79 | + }), |
| 80 | + }) |
| 81 | + } |
| 82 | + |
| 83 | + pub fn draw(&self, rpass: &mut RenderPass<'_>, shader_constants: &ShaderConstants) { |
| 84 | + rpass.set_pipeline(&self.pipeline); |
| 85 | + rpass.set_push_constants( |
| 86 | + ShaderStages::VERTEX_FRAGMENT, |
| 87 | + 0, |
| 88 | + bytemuck::bytes_of(shader_constants), |
| 89 | + ); |
| 90 | + rpass.draw(0..3, 0..1); |
| 91 | + } |
| 92 | +} |
0 commit comments