diff --git a/experiments/2024-10-30/src/render/pipeline.rs b/experiments/2024-10-30/src/render/pipeline.rs index c64481845..5b681c9f4 100644 --- a/experiments/2024-10-30/src/render/pipeline.rs +++ b/experiments/2024-10-30/src/render/pipeline.rs @@ -1,4 +1,104 @@ +use super::renderer::Vertex; + pub struct Pipeline { pub pipeline: wgpu::RenderPipeline, pub bind_group: wgpu::BindGroup, } + +impl Pipeline { + pub fn new( + device: &wgpu::Device, + config: &wgpu::SurfaceConfiguration, + transform_buffer: &wgpu::Buffer, + ) -> Self { + let bind_group_layout = + device.create_bind_group_layout(&wgpu::BindGroupLayoutDescriptor { + label: None, + entries: &[wgpu::BindGroupLayoutEntry { + binding: 0, + visibility: wgpu::ShaderStages::VERTEX, + ty: wgpu::BindingType::Buffer { + ty: wgpu::BufferBindingType::Uniform, + has_dynamic_offset: false, + min_binding_size: None, + }, + count: None, + }], + }); + + let layout = + device.create_pipeline_layout(&wgpu::PipelineLayoutDescriptor { + label: None, + bind_group_layouts: &[&bind_group_layout], + push_constant_ranges: &[], + }); + + let shader = + device.create_shader_module(wgpu::include_wgsl!("shader.wgsl")); + + let pipeline = + device.create_render_pipeline(&wgpu::RenderPipelineDescriptor { + label: None, + layout: Some(&layout), + vertex: wgpu::VertexState { + module: &shader, + entry_point: Some("vertex"), + compilation_options: + wgpu::PipelineCompilationOptions::default(), + buffers: &[wgpu::VertexBufferLayout { + array_stride: size_of::() + as wgpu::BufferAddress, + step_mode: wgpu::VertexStepMode::Vertex, + attributes: &wgpu::vertex_attr_array![ + 0 => Float32x3, + 1 => Float32x3, + ], + }], + }, + fragment: Some(wgpu::FragmentState { + module: &shader, + entry_point: Some("fragment"), + compilation_options: + wgpu::PipelineCompilationOptions::default(), + 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: None, + unclipped_depth: false, + polygon_mode: wgpu::PolygonMode::Fill, + conservative: false, + }, + depth_stencil: Some(wgpu::DepthStencilState { + format: wgpu::TextureFormat::Depth32Float, + depth_write_enabled: true, + depth_compare: wgpu::CompareFunction::Less, + stencil: wgpu::StencilState::default(), + bias: wgpu::DepthBiasState::default(), + }), + multisample: wgpu::MultisampleState::default(), + multiview: None, + cache: None, + }); + + let bind_group = device.create_bind_group(&wgpu::BindGroupDescriptor { + label: None, + layout: &bind_group_layout, + entries: &[wgpu::BindGroupEntry { + binding: 0, + resource: transform_buffer.as_entire_binding(), + }], + }); + + Pipeline { + pipeline, + bind_group, + } + } +} diff --git a/experiments/2024-10-30/src/render/renderer.rs b/experiments/2024-10-30/src/render/renderer.rs index e1845bc70..41e81369a 100644 --- a/experiments/2024-10-30/src/render/renderer.rs +++ b/experiments/2024-10-30/src/render/renderer.rs @@ -56,101 +56,7 @@ impl Renderer { usage: wgpu::BufferUsages::UNIFORM, }); - let pipeline = { - let bind_group_layout = device.create_bind_group_layout( - &wgpu::BindGroupLayoutDescriptor { - label: None, - entries: &[wgpu::BindGroupLayoutEntry { - binding: 0, - visibility: wgpu::ShaderStages::VERTEX, - ty: wgpu::BindingType::Buffer { - ty: wgpu::BufferBindingType::Uniform, - has_dynamic_offset: false, - min_binding_size: None, - }, - count: None, - }], - }, - ); - - let layout = device.create_pipeline_layout( - &wgpu::PipelineLayoutDescriptor { - label: None, - bind_group_layouts: &[&bind_group_layout], - push_constant_ranges: &[], - }, - ); - - let shader = - device.create_shader_module(wgpu::include_wgsl!("shader.wgsl")); - - let pipeline = device.create_render_pipeline( - &wgpu::RenderPipelineDescriptor { - label: None, - layout: Some(&layout), - vertex: wgpu::VertexState { - module: &shader, - entry_point: Some("vertex"), - compilation_options: - wgpu::PipelineCompilationOptions::default(), - buffers: &[wgpu::VertexBufferLayout { - array_stride: size_of::() - as wgpu::BufferAddress, - step_mode: wgpu::VertexStepMode::Vertex, - attributes: &wgpu::vertex_attr_array![ - 0 => Float32x3, - 1 => Float32x3, - ], - }], - }, - fragment: Some(wgpu::FragmentState { - module: &shader, - entry_point: Some("fragment"), - compilation_options: - wgpu::PipelineCompilationOptions::default(), - 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: None, - unclipped_depth: false, - polygon_mode: wgpu::PolygonMode::Fill, - conservative: false, - }, - depth_stencil: Some(wgpu::DepthStencilState { - format: wgpu::TextureFormat::Depth32Float, - depth_write_enabled: true, - depth_compare: wgpu::CompareFunction::Less, - stencil: wgpu::StencilState::default(), - bias: wgpu::DepthBiasState::default(), - }), - multisample: wgpu::MultisampleState::default(), - multiview: None, - cache: None, - }, - ); - - let bind_group = - device.create_bind_group(&wgpu::BindGroupDescriptor { - label: None, - layout: &bind_group_layout, - entries: &[wgpu::BindGroupEntry { - binding: 0, - resource: transform_buffer.as_entire_binding(), - }], - }); - - Pipeline { - pipeline, - bind_group, - } - }; + let pipeline = Pipeline::new(&device, &config, &transform_buffer); let depth_view = { let depth_texture =