1
0
Fork 0

[PIPELINES] Refactor

This commit is contained in:
Florian RICHER 2022-06-21 13:58:56 +02:00
parent cb5af515df
commit 2efbe96845
7 changed files with 131 additions and 61 deletions

View file

@ -18,7 +18,7 @@ mod texture;
mod render; mod render;
use model::{DrawLight, DrawModel, Vertex}; use model::{DrawLight, DrawModel};
const NUM_INSTANCES_PER_ROW: u32 = 10; const NUM_INSTANCES_PER_ROW: u32 = 10;
@ -136,7 +136,6 @@ struct State {
device: wgpu::Device, device: wgpu::Device,
queue: wgpu::Queue, queue: wgpu::Queue,
config: wgpu::SurfaceConfiguration, config: wgpu::SurfaceConfiguration,
render_pipeline: wgpu::RenderPipeline,
obj_model: model::Model, obj_model: model::Model,
camera: camera::Camera, camera: camera::Camera,
projection: camera::Projection, projection: camera::Projection,
@ -152,10 +151,10 @@ struct State {
light_uniform: LightUniform, light_uniform: LightUniform,
light_buffer: wgpu::Buffer, light_buffer: wgpu::Buffer,
light_bind_group: wgpu::BindGroup, light_bind_group: wgpu::BindGroup,
light_render_pipeline: wgpu::RenderPipeline,
#[allow(dead_code)] #[allow(dead_code)]
debug_material: model::Material, debug_material: model::Material,
mouse_pressed: bool, mouse_pressed: bool,
pipelines: render::Pipelines
} }
impl State { impl State {
@ -203,6 +202,7 @@ impl State {
surface.configure(&device, &config); surface.configure(&device, &config);
let global_bind_layout = render::GlobalBindLayout::new(&device); let global_bind_layout = render::GlobalBindLayout::new(&device);
let pipelines = render::Pipelines::new(&global_bind_layout, &device, &config);
// UPDATED! // UPDATED!
let camera = camera::Camera::new((0.0, 5.0, 10.0), cgmath::Deg(-90.0), cgmath::Deg(-20.0)); 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 = let depth_texture =
texture::Texture::create_depth_texture(&device, &config, "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 debug_material = {
let diffuse_bytes = include_bytes!("../res/cobble-diffuse.png"); let diffuse_bytes = include_bytes!("../res/cobble-diffuse.png");
let normal_bytes = include_bytes!("../res/cobble-normal.png"); let normal_bytes = include_bytes!("../res/cobble-normal.png");
@ -382,7 +336,6 @@ impl State {
device, device,
queue, queue,
config, config,
render_pipeline,
obj_model, obj_model,
camera, camera,
projection, projection,
@ -397,10 +350,10 @@ impl State {
light_uniform, light_uniform,
light_buffer, light_buffer,
light_bind_group, light_bind_group,
light_render_pipeline,
#[allow(dead_code)] #[allow(dead_code)]
debug_material, debug_material,
mouse_pressed: false, mouse_pressed: false,
pipelines
} }
} }
@ -505,14 +458,14 @@ impl State {
}); });
render_pass.set_vertex_buffer(1, self.instance_buffer.slice(..)); 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( render_pass.draw_light_model(
&self.obj_model, &self.obj_model,
&self.camera_bind_group, &self.camera_bind_group,
&self.light_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( render_pass.draw_model_instanced(
&self.obj_model, &self.obj_model,
0..self.instances.len() as u32, 0..self.instances.len() as u32,

View file

@ -1,4 +1,4 @@
mod pipelines; mod pipelines;
pub use pipelines::utils::create_render_pipeline; pub use pipelines::utils::create_render_pipeline;
pub use pipelines::GlobalBindLayout; pub use pipelines::{GlobalBindLayout, Pipelines};

View file

@ -1,7 +1,47 @@
use crate::{
model::{self, Vertex},
render, texture,
};
use super::GlobalBindLayout;
pub struct LightPipeline { pub struct LightPipeline {
pipeline: wgpu::RenderPipeline pipeline: wgpu::RenderPipeline,
} }
impl LightPipeline { impl LightPipeline {
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
}
}

View file

@ -1,5 +1,10 @@
mod light;
mod model;
pub mod utils; pub mod utils;
pub use light::LightPipeline;
pub use model::ModelPipeline;
pub struct GlobalBindLayout { pub struct GlobalBindLayout {
texture: wgpu::BindGroupLayout, texture: wgpu::BindGroupLayout,
light: wgpu::BindGroupLayout, light: wgpu::BindGroupLayout,
@ -77,7 +82,6 @@ impl GlobalBindLayout {
label: None, label: None,
}); });
Self { Self {
texture: texture_bind_group_layout, texture: texture_bind_group_layout,
light: light_bind_group_layout, light: light_bind_group_layout,
@ -96,4 +100,30 @@ impl GlobalBindLayout {
pub fn get_camera_bind_layout(&self) -> &wgpu::BindGroupLayout { pub fn get_camera_bind_layout(&self) -> &wgpu::BindGroupLayout {
&self.camera &self.camera
} }
} }
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()
}
}

View file

@ -1,3 +1,50 @@
use crate::{
model::{self, Vertex},
render, texture, InstanceRaw,
};
use super::GlobalBindLayout;
pub struct ModelPipeline { pub struct ModelPipeline {
pipeline: wgpu::RenderPipeline 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
}
}