diff --git a/src/lib.rs b/src/lib.rs index f8c470a..638a092 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -18,7 +18,7 @@ mod texture; mod render; -use model::{DrawLight, DrawModel, Vertex}; +use model::{DrawLight, DrawModel}; const NUM_INSTANCES_PER_ROW: u32 = 10; @@ -136,7 +136,6 @@ struct State { device: wgpu::Device, queue: wgpu::Queue, config: wgpu::SurfaceConfiguration, - render_pipeline: wgpu::RenderPipeline, obj_model: model::Model, camera: camera::Camera, projection: camera::Projection, @@ -152,10 +151,10 @@ struct State { light_uniform: LightUniform, light_buffer: wgpu::Buffer, light_bind_group: wgpu::BindGroup, - light_render_pipeline: wgpu::RenderPipeline, #[allow(dead_code)] debug_material: model::Material, mouse_pressed: bool, + pipelines: render::Pipelines } impl State { @@ -203,6 +202,7 @@ impl State { surface.configure(&device, &config); let global_bind_layout = render::GlobalBindLayout::new(&device); + let pipelines = render::Pipelines::new(&global_bind_layout, &device, &config); // UPDATED! let camera = camera::Camera::new((0.0, 5.0, 10.0), cgmath::Deg(-90.0), cgmath::Deg(-20.0)); @@ -301,52 +301,6 @@ impl State { let depth_texture = texture::Texture::create_depth_texture(&device, &config, "depth_texture"); - let render_pipeline_layout = - device.create_pipeline_layout(&wgpu::PipelineLayoutDescriptor { - label: Some("Render Pipeline Layout"), - bind_group_layouts: &[ - global_bind_layout.get_texture_bind_layout(), - global_bind_layout.get_camera_bind_layout(), - global_bind_layout.get_light_bind_layout(), - ], - push_constant_ranges: &[], - }); - - let render_pipeline = { - let shader = wgpu::ShaderModuleDescriptor { - label: Some("Normal Shader"), - source: wgpu::ShaderSource::Wgsl(include_str!("shader.wgsl").into()), - }; - render::create_render_pipeline( - &device, - &render_pipeline_layout, - config.format, - Some(texture::Texture::DEPTH_FORMAT), - &[model::ModelVertex::desc(), InstanceRaw::desc()], - shader, - ) - }; - - let light_render_pipeline = { - let layout = device.create_pipeline_layout(&wgpu::PipelineLayoutDescriptor { - label: Some("Light Pipeline Layout"), - bind_group_layouts: &[global_bind_layout.get_camera_bind_layout(), global_bind_layout.get_light_bind_layout()], - push_constant_ranges: &[], - }); - let shader = wgpu::ShaderModuleDescriptor { - label: Some("Light Shader"), - source: wgpu::ShaderSource::Wgsl(include_str!("light.wgsl").into()), - }; - render::create_render_pipeline( - &device, - &layout, - config.format, - Some(texture::Texture::DEPTH_FORMAT), - &[model::ModelVertex::desc()], - shader, - ) - }; - let debug_material = { let diffuse_bytes = include_bytes!("../res/cobble-diffuse.png"); let normal_bytes = include_bytes!("../res/cobble-normal.png"); @@ -382,7 +336,6 @@ impl State { device, queue, config, - render_pipeline, obj_model, camera, projection, @@ -397,10 +350,10 @@ impl State { light_uniform, light_buffer, light_bind_group, - light_render_pipeline, #[allow(dead_code)] debug_material, mouse_pressed: false, + pipelines } } @@ -505,14 +458,14 @@ impl State { }); render_pass.set_vertex_buffer(1, self.instance_buffer.slice(..)); - render_pass.set_pipeline(&self.light_render_pipeline); + render_pass.set_pipeline(self.pipelines.get_light_pipeline()); render_pass.draw_light_model( &self.obj_model, &self.camera_bind_group, &self.light_bind_group, ); - render_pass.set_pipeline(&self.render_pipeline); + render_pass.set_pipeline(self.pipelines.get_render_pipeline()); render_pass.draw_model_instanced( &self.obj_model, 0..self.instances.len() as u32, diff --git a/src/render/mod.rs b/src/render/mod.rs index bb6bc0d..159b448 100644 --- a/src/render/mod.rs +++ b/src/render/mod.rs @@ -1,4 +1,4 @@ mod pipelines; pub use pipelines::utils::create_render_pipeline; -pub use pipelines::GlobalBindLayout; \ No newline at end of file +pub use pipelines::{GlobalBindLayout, Pipelines}; \ No newline at end of file diff --git a/src/render/pipelines/light.rs b/src/render/pipelines/light.rs index 3d2362d..9bc415f 100644 --- a/src/render/pipelines/light.rs +++ b/src/render/pipelines/light.rs @@ -1,7 +1,47 @@ +use crate::{ + model::{self, Vertex}, + render, texture, +}; + +use super::GlobalBindLayout; + pub struct LightPipeline { - pipeline: wgpu::RenderPipeline + pipeline: wgpu::RenderPipeline, } impl LightPipeline { - -} \ No newline at end of file + pub fn new( + global_bind_layout: &GlobalBindLayout, + device: &wgpu::Device, + config: &wgpu::SurfaceConfiguration, + ) -> Self { + let pipeline = { + let layout = device.create_pipeline_layout(&wgpu::PipelineLayoutDescriptor { + label: Some("Light Pipeline Layout"), + bind_group_layouts: &[ + global_bind_layout.get_camera_bind_layout(), + global_bind_layout.get_light_bind_layout(), + ], + push_constant_ranges: &[], + }); + let shader = wgpu::ShaderModuleDescriptor { + label: Some("Light Shader"), + source: wgpu::ShaderSource::Wgsl(include_str!("light.wgsl").into()), + }; + render::create_render_pipeline( + &device, + &layout, + config.format, + Some(texture::Texture::DEPTH_FORMAT), + &[model::ModelVertex::desc()], + shader, + ) + }; + + Self { pipeline } + } + + pub fn get_pipeline(&self) -> &wgpu::RenderPipeline { + &self.pipeline + } +} diff --git a/src/light.wgsl b/src/render/pipelines/light.wgsl similarity index 100% rename from src/light.wgsl rename to src/render/pipelines/light.wgsl diff --git a/src/render/pipelines/mod.rs b/src/render/pipelines/mod.rs index da4664b..2ae669d 100644 --- a/src/render/pipelines/mod.rs +++ b/src/render/pipelines/mod.rs @@ -1,5 +1,10 @@ +mod light; +mod model; pub mod utils; +pub use light::LightPipeline; +pub use model::ModelPipeline; + pub struct GlobalBindLayout { texture: wgpu::BindGroupLayout, light: wgpu::BindGroupLayout, @@ -77,7 +82,6 @@ impl GlobalBindLayout { label: None, }); - Self { texture: texture_bind_group_layout, light: light_bind_group_layout, @@ -96,4 +100,30 @@ impl GlobalBindLayout { pub fn get_camera_bind_layout(&self) -> &wgpu::BindGroupLayout { &self.camera } -} \ No newline at end of file +} + +pub struct Pipelines { + render: model::ModelPipeline, + light: light::LightPipeline, +} + +impl Pipelines { + pub fn new( + global_bind_layout: &GlobalBindLayout, + device: &wgpu::Device, + config: &wgpu::SurfaceConfiguration, + ) -> Self { + Self { + render: model::ModelPipeline::new(global_bind_layout, device, config), + light: light::LightPipeline::new(global_bind_layout, device, config), + } + } + + pub fn get_render_pipeline(&self) -> &wgpu::RenderPipeline { + self.render.get_pipeline() + } + + pub fn get_light_pipeline(&self) -> &wgpu::RenderPipeline { + self.light.get_pipeline() + } +} diff --git a/src/render/pipelines/model.rs b/src/render/pipelines/model.rs index 09e1ce9..7030f0e 100644 --- a/src/render/pipelines/model.rs +++ b/src/render/pipelines/model.rs @@ -1,3 +1,50 @@ +use crate::{ + model::{self, Vertex}, + render, texture, InstanceRaw, +}; + +use super::GlobalBindLayout; + pub struct ModelPipeline { - pipeline: wgpu::RenderPipeline -} \ No newline at end of file + pipeline: wgpu::RenderPipeline, +} + +impl ModelPipeline { + pub fn new( + global_bind_layout: &GlobalBindLayout, + device: &wgpu::Device, + config: &wgpu::SurfaceConfiguration, + ) -> Self { + let render_pipeline_layout = + device.create_pipeline_layout(&wgpu::PipelineLayoutDescriptor { + label: Some("Render Pipeline Layout"), + bind_group_layouts: &[ + global_bind_layout.get_texture_bind_layout(), + global_bind_layout.get_camera_bind_layout(), + global_bind_layout.get_light_bind_layout(), + ], + push_constant_ranges: &[], + }); + + let pipeline = { + let shader = wgpu::ShaderModuleDescriptor { + label: Some("Normal Shader"), + source: wgpu::ShaderSource::Wgsl(include_str!("model.wgsl").into()), + }; + render::create_render_pipeline( + &device, + &render_pipeline_layout, + config.format, + Some(texture::Texture::DEPTH_FORMAT), + &[model::ModelVertex::desc(), InstanceRaw::desc()], + shader, + ) + }; + + Self { pipeline } + } + + pub fn get_pipeline(&self) -> &wgpu::RenderPipeline { + &self.pipeline + } +} diff --git a/src/shader.wgsl b/src/render/pipelines/model.wgsl similarity index 100% rename from src/shader.wgsl rename to src/render/pipelines/model.wgsl