From bbd2ef7b532773daf9231676f59df89b510adb05 Mon Sep 17 00:00:00 2001 From: Florian RICHER Date: Tue, 21 Jun 2022 13:32:23 +0200 Subject: [PATCH] Begin refactor --- src/lib.rs | 66 +++-------------------------------- src/render/mod.rs | 3 ++ src/render/pipelines/light.rs | 7 ++++ src/render/pipelines/mod.rs | 7 ++++ src/render/pipelines/model.rs | 3 ++ src/render/pipelines/utils.rs | 59 +++++++++++++++++++++++++++++++ src/render/renderer/mod.rs | 0 src/render/renderer/vertex.rs | 0 8 files changed, 83 insertions(+), 62 deletions(-) create mode 100644 src/render/mod.rs create mode 100644 src/render/pipelines/light.rs create mode 100644 src/render/pipelines/mod.rs create mode 100644 src/render/pipelines/model.rs create mode 100644 src/render/pipelines/utils.rs create mode 100644 src/render/renderer/mod.rs create mode 100644 src/render/renderer/vertex.rs diff --git a/src/lib.rs b/src/lib.rs index 66b7da2..0ba8e9d 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -16,6 +16,8 @@ mod model; mod resources; mod texture; +mod render; + use model::{DrawLight, DrawModel, Vertex}; const NUM_INSTANCES_PER_ROW: u32 = 10; @@ -156,66 +158,6 @@ struct State { mouse_pressed: bool, } -fn create_render_pipeline( - device: &wgpu::Device, - layout: &wgpu::PipelineLayout, - color_format: wgpu::TextureFormat, - depth_format: Option, - vertex_layouts: &[wgpu::VertexBufferLayout], - shader: wgpu::ShaderModuleDescriptor, -) -> wgpu::RenderPipeline { - let shader = device.create_shader_module(&shader); - - device.create_render_pipeline(&wgpu::RenderPipelineDescriptor { - label: Some(&format!("{:?}", shader)), - layout: Some(layout), - vertex: wgpu::VertexState { - module: &shader, - entry_point: "vs_main", - buffers: vertex_layouts, - }, - fragment: Some(wgpu::FragmentState { - module: &shader, - entry_point: "fs_main", - targets: &[wgpu::ColorTargetState { - format: color_format, - blend: Some(wgpu::BlendState { - alpha: wgpu::BlendComponent::REPLACE, - color: wgpu::BlendComponent::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: depth_format.map(|format| wgpu::DepthStencilState { - format, - depth_write_enabled: true, - depth_compare: wgpu::CompareFunction::Less, - stencil: wgpu::StencilState::default(), - bias: wgpu::DepthBiasState::default(), - }), - multisample: wgpu::MultisampleState { - count: 1, - mask: !0, - alpha_to_coverage_enabled: false, - }, - // If the pipeline will be used with a multiview render pass, this - // indicates how many array layers the attachments will have. - multiview: None, - }) -} - impl State { async fn new(window: &Window) -> Self { let size = window.inner_size(); @@ -443,7 +385,7 @@ impl State { label: Some("Normal Shader"), source: wgpu::ShaderSource::Wgsl(include_str!("shader.wgsl").into()), }; - create_render_pipeline( + render::create_render_pipeline( &device, &render_pipeline_layout, config.format, @@ -463,7 +405,7 @@ impl State { label: Some("Light Shader"), source: wgpu::ShaderSource::Wgsl(include_str!("light.wgsl").into()), }; - create_render_pipeline( + render::create_render_pipeline( &device, &layout, config.format, diff --git a/src/render/mod.rs b/src/render/mod.rs new file mode 100644 index 0000000..a1da8b4 --- /dev/null +++ b/src/render/mod.rs @@ -0,0 +1,3 @@ +mod pipelines; + +pub use pipelines::utils::create_render_pipeline; \ No newline at end of file diff --git a/src/render/pipelines/light.rs b/src/render/pipelines/light.rs new file mode 100644 index 0000000..3d2362d --- /dev/null +++ b/src/render/pipelines/light.rs @@ -0,0 +1,7 @@ +pub struct LightPipeline { + pipeline: wgpu::RenderPipeline +} + +impl LightPipeline { + +} \ No newline at end of file diff --git a/src/render/pipelines/mod.rs b/src/render/pipelines/mod.rs new file mode 100644 index 0000000..70fa1bd --- /dev/null +++ b/src/render/pipelines/mod.rs @@ -0,0 +1,7 @@ +pub mod utils; + +pub struct GlobalBindLayout { + texture: wgpu::BindGroupLayout, + light: wgpu::BindGroupLayout, + camera: wgpu::BindGroupLayout +} \ No newline at end of file diff --git a/src/render/pipelines/model.rs b/src/render/pipelines/model.rs new file mode 100644 index 0000000..09e1ce9 --- /dev/null +++ b/src/render/pipelines/model.rs @@ -0,0 +1,3 @@ +pub struct ModelPipeline { + pipeline: wgpu::RenderPipeline +} \ No newline at end of file diff --git a/src/render/pipelines/utils.rs b/src/render/pipelines/utils.rs new file mode 100644 index 0000000..28ac4db --- /dev/null +++ b/src/render/pipelines/utils.rs @@ -0,0 +1,59 @@ +pub fn create_render_pipeline( + device: &wgpu::Device, + layout: &wgpu::PipelineLayout, + color_format: wgpu::TextureFormat, + depth_format: Option, + vertex_layouts: &[wgpu::VertexBufferLayout], + shader: wgpu::ShaderModuleDescriptor, +) -> wgpu::RenderPipeline { + let shader = device.create_shader_module(&shader); + + device.create_render_pipeline(&wgpu::RenderPipelineDescriptor { + label: Some(&format!("{:?}", shader)), + layout: Some(layout), + vertex: wgpu::VertexState { + module: &shader, + entry_point: "vs_main", + buffers: vertex_layouts, + }, + fragment: Some(wgpu::FragmentState { + module: &shader, + entry_point: "fs_main", + targets: &[wgpu::ColorTargetState { + format: color_format, + blend: Some(wgpu::BlendState { + alpha: wgpu::BlendComponent::REPLACE, + color: wgpu::BlendComponent::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: depth_format.map(|format| wgpu::DepthStencilState { + format, + depth_write_enabled: true, + depth_compare: wgpu::CompareFunction::Less, + stencil: wgpu::StencilState::default(), + bias: wgpu::DepthBiasState::default(), + }), + multisample: wgpu::MultisampleState { + count: 1, + mask: !0, + alpha_to_coverage_enabled: false, + }, + // If the pipeline will be used with a multiview render pass, this + // indicates how many array layers the attachments will have. + multiview: None, + }) +} \ No newline at end of file diff --git a/src/render/renderer/mod.rs b/src/render/renderer/mod.rs new file mode 100644 index 0000000..e69de29 diff --git a/src/render/renderer/vertex.rs b/src/render/renderer/vertex.rs new file mode 100644 index 0000000..e69de29