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

@ -1,4 +1,4 @@
mod pipelines;
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 {
pipeline: wgpu::RenderPipeline
pipeline: wgpu::RenderPipeline,
}
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

@ -0,0 +1,42 @@
// Vertex shader
struct Camera {
view_pos: vec4<f32>;
view_proj: mat4x4<f32>;
};
[[group(0), binding(0)]]
var<uniform> camera: Camera;
struct Light {
position: vec3<f32>;
color: vec3<f32>;
};
[[group(1), binding(0)]]
var<uniform> light: Light;
struct VertexInput {
[[location(0)]] position: vec3<f32>;
};
struct VertexOutput {
[[builtin(position)]] clip_position: vec4<f32>;
[[location(0)]] color: vec3<f32>;
};
[[stage(vertex)]]
fn vs_main(
model: VertexInput,
) -> VertexOutput {
let scale = 0.25;
var out: VertexOutput;
out.clip_position = camera.view_proj * vec4<f32>(model.position * scale + light.position, 1.0);
out.color = light.color;
return out;
}
// Fragment shader
[[stage(fragment)]]
fn fs_main(in: VertexOutput) -> [[location(0)]] vec4<f32> {
return vec4<f32>(in.color, 1.0);
}

View file

@ -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
}
}
}
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 {
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
}
}

View file

@ -0,0 +1,115 @@
// Vertex shader
struct Camera {
view_pos: vec4<f32>;
view_proj: mat4x4<f32>;
};
[[group(1), binding(0)]]
var<uniform> camera: Camera;
struct Light {
position: vec3<f32>;
color: vec3<f32>;
};
[[group(2), binding(0)]]
var<uniform> light: Light;
struct VertexInput {
[[location(0)]] position: vec3<f32>;
[[location(1)]] tex_coords: vec2<f32>;
[[location(2)]] normal: vec3<f32>;
[[location(3)]] tangent: vec3<f32>;
[[location(4)]] bitangent: vec3<f32>;
};
struct InstanceInput {
[[location(5)]] model_matrix_0: vec4<f32>;
[[location(6)]] model_matrix_1: vec4<f32>;
[[location(7)]] model_matrix_2: vec4<f32>;
[[location(8)]] model_matrix_3: vec4<f32>;
[[location(9)]] normal_matrix_0: vec3<f32>;
[[location(10)]] normal_matrix_1: vec3<f32>;
[[location(11)]] normal_matrix_2: vec3<f32>;
};
struct VertexOutput {
[[builtin(position)]] clip_position: vec4<f32>;
[[location(0)]] tex_coords: vec2<f32>;
[[location(1)]] tangent_position: vec3<f32>;
[[location(2)]] tangent_light_position: vec3<f32>;
[[location(3)]] tangent_view_position: vec3<f32>;
};
[[stage(vertex)]]
fn vs_main(
model: VertexInput,
instance: InstanceInput,
) -> VertexOutput {
let model_matrix = mat4x4<f32>(
instance.model_matrix_0,
instance.model_matrix_1,
instance.model_matrix_2,
instance.model_matrix_3,
);
let normal_matrix = mat3x3<f32>(
instance.normal_matrix_0,
instance.normal_matrix_1,
instance.normal_matrix_2,
);
// Construct the tangent matrix
let world_normal = normalize(normal_matrix * model.normal);
let world_tangent = normalize(normal_matrix * model.tangent);
let world_bitangent = normalize(normal_matrix * model.bitangent);
let tangent_matrix = transpose(mat3x3<f32>(
world_tangent,
world_bitangent,
world_normal,
));
let world_position = model_matrix * vec4<f32>(model.position, 1.0);
var out: VertexOutput;
out.clip_position = camera.view_proj * world_position;
out.tex_coords = model.tex_coords;
out.tangent_position = tangent_matrix * world_position.xyz;
out.tangent_view_position = tangent_matrix * camera.view_pos.xyz;
out.tangent_light_position = tangent_matrix * light.position;
return out;
}
// Fragment shader
[[group(0), binding(0)]]
var t_diffuse: texture_2d<f32>;
[[group(0), binding(1)]]
var s_diffuse: sampler;
[[group(0), binding(2)]]
var t_normal: texture_2d<f32>;
[[group(0), binding(3)]]
var s_normal: sampler;
[[stage(fragment)]]
fn fs_main(in: VertexOutput) -> [[location(0)]] vec4<f32> {
let object_color: vec4<f32> = textureSample(t_diffuse, s_diffuse, in.tex_coords);
let object_normal: vec4<f32> = textureSample(t_normal, s_normal, in.tex_coords);
// We don't need (or want) much ambient light, so 0.1 is fine
let ambient_strength = 0.1;
let ambient_color = light.color * ambient_strength;
// Create the lighting vectors
let tangent_normal = object_normal.xyz * 2.0 - 1.0;
let light_dir = normalize(in.tangent_light_position - in.tangent_position);
let view_dir = normalize(in.tangent_view_position - in.tangent_position);
let half_dir = normalize(view_dir + light_dir);
let diffuse_strength = max(dot(tangent_normal, light_dir), 0.0);
let diffuse_color = light.color * diffuse_strength;
let specular_strength = pow(max(dot(tangent_normal, half_dir), 0.0), 32.0);
let specular_color = specular_strength * light.color;
let result = (ambient_color + diffuse_color + specular_color) * object_color.xyz;
return vec4<f32>(result, object_color.a);
}