bind group data model
This commit is contained in:
parent
129a9747bb
commit
5975289f4c
@ -14,16 +14,16 @@ struct Light {
|
|||||||
};
|
};
|
||||||
|
|
||||||
layout(set = 0, binding = 0) uniform Globals {
|
layout(set = 0, binding = 0) uniform Globals {
|
||||||
mat4 u_ViewProj;
|
mat4 ViewProj;
|
||||||
uvec4 u_NumLights;
|
uvec4 NumLights;
|
||||||
};
|
};
|
||||||
layout(set = 0, binding = 1) uniform Lights {
|
layout(set = 0, binding = 1) uniform Lights {
|
||||||
Light u_Lights[MAX_LIGHTS];
|
Light SceneLights[MAX_LIGHTS];
|
||||||
};
|
};
|
||||||
|
|
||||||
layout(set = 1, binding = 0) uniform Entity {
|
layout(set = 1, binding = 0) uniform Entity {
|
||||||
mat4 u_World;
|
mat4 World;
|
||||||
vec4 u_Color;
|
vec4 Color;
|
||||||
};
|
};
|
||||||
|
|
||||||
void main() {
|
void main() {
|
||||||
@ -31,8 +31,8 @@ void main() {
|
|||||||
vec3 ambient = vec3(0.05, 0.05, 0.05);
|
vec3 ambient = vec3(0.05, 0.05, 0.05);
|
||||||
// accumulate color
|
// accumulate color
|
||||||
vec3 color = ambient;
|
vec3 color = ambient;
|
||||||
for (int i=0; i<int(u_NumLights.x) && i<MAX_LIGHTS; ++i) {
|
for (int i=0; i<int(NumLights.x) && i<MAX_LIGHTS; ++i) {
|
||||||
Light light = u_Lights[i];
|
Light light = SceneLights[i];
|
||||||
// compute Lambertian diffuse term
|
// compute Lambertian diffuse term
|
||||||
vec3 light_dir = normalize(light.pos.xyz - v_Position.xyz);
|
vec3 light_dir = normalize(light.pos.xyz - v_Position.xyz);
|
||||||
float diffuse = max(0.0, dot(normal, light_dir));
|
float diffuse = max(0.0, dot(normal, light_dir));
|
||||||
@ -40,5 +40,5 @@ void main() {
|
|||||||
color += diffuse * light.color.xyz;
|
color += diffuse * light.color.xyz;
|
||||||
}
|
}
|
||||||
// multiply the light by material color
|
// multiply the light by material color
|
||||||
o_Target = vec4(color, 1.0) * u_Color;
|
o_Target = vec4(color, 1.0) * Color;
|
||||||
}
|
}
|
||||||
|
|||||||
@ -8,16 +8,16 @@ layout(location = 0) out vec3 v_Normal;
|
|||||||
layout(location = 1) out vec4 v_Position;
|
layout(location = 1) out vec4 v_Position;
|
||||||
|
|
||||||
layout(set = 0, binding = 0) uniform Globals {
|
layout(set = 0, binding = 0) uniform Globals {
|
||||||
mat4 u_ViewProj;
|
mat4 ViewProj;
|
||||||
uvec4 u_NumLights;
|
uvec4 NumLights;
|
||||||
};
|
};
|
||||||
layout(set = 1, binding = 0) uniform Entity {
|
layout(set = 1, binding = 0) uniform Entity {
|
||||||
mat4 u_World;
|
mat4 World;
|
||||||
vec4 u_Color;
|
vec4 Color;
|
||||||
};
|
};
|
||||||
|
|
||||||
void main() {
|
void main() {
|
||||||
v_Normal = mat3(u_World) * vec3(a_Normal.xyz);
|
v_Normal = mat3(World) * vec3(a_Normal.xyz);
|
||||||
v_Position = u_World * vec4(a_Pos);
|
v_Position = World * vec4(a_Pos);
|
||||||
gl_Position = u_ViewProj * v_Position;
|
gl_Position = ViewProj * v_Position;
|
||||||
}
|
}
|
||||||
|
|||||||
@ -2,6 +2,7 @@ pub mod pipelines;
|
|||||||
pub mod resource;
|
pub mod resource;
|
||||||
pub mod wgpu_renderer;
|
pub mod wgpu_renderer;
|
||||||
mod pipeline;
|
mod pipeline;
|
||||||
|
mod pipeline_layout;
|
||||||
mod pass;
|
mod pass;
|
||||||
mod renderer;
|
mod renderer;
|
||||||
mod shader;
|
mod shader;
|
||||||
@ -9,6 +10,7 @@ mod render_graph;
|
|||||||
mod draw_target;
|
mod draw_target;
|
||||||
|
|
||||||
pub use pipeline::*;
|
pub use pipeline::*;
|
||||||
|
pub use pipeline_layout::*;
|
||||||
pub use pass::*;
|
pub use pass::*;
|
||||||
pub use renderer::*;
|
pub use renderer::*;
|
||||||
pub use shader::*;
|
pub use shader::*;
|
||||||
|
|||||||
@ -1,5 +1,5 @@
|
|||||||
use crate::render::{
|
use crate::render::{
|
||||||
render_graph_2::DrawTarget,
|
render_graph_2::{DrawTarget, PipelineLayout, BindGroup},
|
||||||
shader::{Shader, ShaderStages},
|
shader::{Shader, ShaderStages},
|
||||||
};
|
};
|
||||||
|
|
||||||
@ -21,6 +21,7 @@ impl<'a> Into<wgpu::VertexBufferDescriptor<'a>> for &'a VertexBufferDescriptor {
|
|||||||
|
|
||||||
pub struct PipelineDescriptor {
|
pub struct PipelineDescriptor {
|
||||||
pub draw_targets: Vec<DrawTarget>,
|
pub draw_targets: Vec<DrawTarget>,
|
||||||
|
pub pipeline_layout: PipelineLayout,
|
||||||
pub shader_stages: ShaderStages,
|
pub shader_stages: ShaderStages,
|
||||||
pub rasterization_state: Option<wgpu::RasterizationStateDescriptor>,
|
pub rasterization_state: Option<wgpu::RasterizationStateDescriptor>,
|
||||||
|
|
||||||
@ -56,6 +57,7 @@ pub struct PipelineDescriptor {
|
|||||||
impl PipelineDescriptor {
|
impl PipelineDescriptor {
|
||||||
fn new(vertex_shader: Shader) -> Self {
|
fn new(vertex_shader: Shader) -> Self {
|
||||||
PipelineDescriptor {
|
PipelineDescriptor {
|
||||||
|
pipeline_layout: PipelineLayout::new(),
|
||||||
color_states: Vec::new(),
|
color_states: Vec::new(),
|
||||||
depth_stencil_state: None,
|
depth_stencil_state: None,
|
||||||
draw_targets: Vec::new(),
|
draw_targets: Vec::new(),
|
||||||
@ -105,7 +107,7 @@ impl PipelineBuilder {
|
|||||||
self
|
self
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn with_color_state(mut self, color_state_descriptor: wgpu::ColorStateDescriptor) -> Self {
|
pub fn add_color_state(mut self, color_state_descriptor: wgpu::ColorStateDescriptor) -> Self {
|
||||||
self.pipeline.color_states.push(color_state_descriptor);
|
self.pipeline.color_states.push(color_state_descriptor);
|
||||||
self
|
self
|
||||||
}
|
}
|
||||||
@ -121,7 +123,12 @@ impl PipelineBuilder {
|
|||||||
self
|
self
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn with_vertex_buffer_descriptor(
|
pub fn add_bind_group(mut self, bind_group: BindGroup) -> Self {
|
||||||
|
self.pipeline.pipeline_layout.bind_groups.push(bind_group);
|
||||||
|
self
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn add_vertex_buffer_descriptor(
|
||||||
mut self,
|
mut self,
|
||||||
mut vertex_buffer_descriptor: VertexBufferDescriptor,
|
mut vertex_buffer_descriptor: VertexBufferDescriptor,
|
||||||
) -> Self {
|
) -> Self {
|
||||||
@ -144,7 +151,7 @@ impl PipelineBuilder {
|
|||||||
self
|
self
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn with_draw_target(mut self, draw_target: DrawTarget) -> Self {
|
pub fn add_draw_target(mut self, draw_target: DrawTarget) -> Self {
|
||||||
self.pipeline.draw_targets.push(draw_target);
|
self.pipeline.draw_targets.push(draw_target);
|
||||||
self
|
self
|
||||||
}
|
}
|
||||||
|
|||||||
65
src/render/render_graph_2/pipeline_layout.rs
Normal file
65
src/render/render_graph_2/pipeline_layout.rs
Normal file
@ -0,0 +1,65 @@
|
|||||||
|
pub struct PipelineLayout {
|
||||||
|
pub bind_groups: Vec<BindGroup>,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl PipelineLayout {
|
||||||
|
pub fn new() -> Self {
|
||||||
|
PipelineLayout {
|
||||||
|
bind_groups: Vec::new(),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
pub struct BindGroup {
|
||||||
|
pub bindings: Vec<Binding>
|
||||||
|
}
|
||||||
|
|
||||||
|
pub struct Binding {
|
||||||
|
pub name: String,
|
||||||
|
pub bind_type: BindType,
|
||||||
|
// TODO: ADD SHADER STAGE VISIBILITY
|
||||||
|
}
|
||||||
|
|
||||||
|
pub enum BindType {
|
||||||
|
Uniform {
|
||||||
|
// dynamic: bool,
|
||||||
|
properties: Vec<UniformProperty>
|
||||||
|
},
|
||||||
|
Buffer {
|
||||||
|
dynamic: bool,
|
||||||
|
readonly: bool,
|
||||||
|
},
|
||||||
|
Sampler,
|
||||||
|
SampledTexture {
|
||||||
|
multisampled: bool,
|
||||||
|
dimension: TextureDimension,
|
||||||
|
},
|
||||||
|
StorageTexture {
|
||||||
|
dimension: TextureDimension,
|
||||||
|
},
|
||||||
|
}
|
||||||
|
|
||||||
|
pub struct UniformProperty {
|
||||||
|
pub name: String,
|
||||||
|
pub property_type: UniformPropertyType,
|
||||||
|
}
|
||||||
|
|
||||||
|
pub enum UniformPropertyType {
|
||||||
|
Int,
|
||||||
|
Float,
|
||||||
|
UVec4,
|
||||||
|
Vec3,
|
||||||
|
Vec4,
|
||||||
|
Mat4,
|
||||||
|
Struct(Vec<UniformPropertyType>),
|
||||||
|
Array(Box<UniformPropertyType>, usize),
|
||||||
|
}
|
||||||
|
|
||||||
|
pub enum TextureDimension {
|
||||||
|
D1,
|
||||||
|
D2,
|
||||||
|
D2Array,
|
||||||
|
Cube,
|
||||||
|
CubeArray,
|
||||||
|
D3,
|
||||||
|
}
|
||||||
@ -14,16 +14,16 @@ struct Light {
|
|||||||
};
|
};
|
||||||
|
|
||||||
layout(set = 0, binding = 0) uniform Globals {
|
layout(set = 0, binding = 0) uniform Globals {
|
||||||
mat4 u_ViewProj;
|
mat4 ViewProj;
|
||||||
uvec4 u_NumLights;
|
uvec4 NumLights;
|
||||||
};
|
};
|
||||||
layout(set = 0, binding = 1) uniform Lights {
|
layout(set = 0, binding = 1) uniform Lights {
|
||||||
Light u_Lights[MAX_LIGHTS];
|
Light SceneLights[MAX_LIGHTS];
|
||||||
};
|
};
|
||||||
|
|
||||||
layout(set = 1, binding = 0) uniform Entity {
|
layout(set = 1, binding = 0) uniform Entity {
|
||||||
mat4 u_World;
|
mat4 World;
|
||||||
vec4 u_Color;
|
vec4 Color;
|
||||||
};
|
};
|
||||||
|
|
||||||
void main() {
|
void main() {
|
||||||
@ -31,8 +31,8 @@ void main() {
|
|||||||
vec3 ambient = vec3(0.05, 0.05, 0.05);
|
vec3 ambient = vec3(0.05, 0.05, 0.05);
|
||||||
// accumulate color
|
// accumulate color
|
||||||
vec3 color = ambient;
|
vec3 color = ambient;
|
||||||
for (int i=0; i<int(u_NumLights.x) && i<MAX_LIGHTS; ++i) {
|
for (int i=0; i<int(NumLights.x) && i<MAX_LIGHTS; ++i) {
|
||||||
Light light = u_Lights[i];
|
Light light = SceneLights[i];
|
||||||
// compute Lambertian diffuse term
|
// compute Lambertian diffuse term
|
||||||
vec3 light_dir = normalize(light.pos.xyz - v_Position.xyz);
|
vec3 light_dir = normalize(light.pos.xyz - v_Position.xyz);
|
||||||
float diffuse = max(0.0, dot(normal, light_dir));
|
float diffuse = max(0.0, dot(normal, light_dir));
|
||||||
@ -40,5 +40,5 @@ void main() {
|
|||||||
color += diffuse * light.color.xyz;
|
color += diffuse * light.color.xyz;
|
||||||
}
|
}
|
||||||
// multiply the light by material color
|
// multiply the light by material color
|
||||||
o_Target = vec4(color, 1.0) * u_Color;
|
o_Target = vec4(color, 1.0) * Color;
|
||||||
}
|
}
|
||||||
|
|||||||
@ -8,16 +8,16 @@ layout(location = 0) out vec3 v_Normal;
|
|||||||
layout(location = 1) out vec4 v_Position;
|
layout(location = 1) out vec4 v_Position;
|
||||||
|
|
||||||
layout(set = 0, binding = 0) uniform Globals {
|
layout(set = 0, binding = 0) uniform Globals {
|
||||||
mat4 u_ViewProj;
|
mat4 ViewProj;
|
||||||
uvec4 u_NumLights;
|
uvec4 NumLights;
|
||||||
};
|
};
|
||||||
layout(set = 1, binding = 0) uniform Entity {
|
layout(set = 1, binding = 0) uniform Entity {
|
||||||
mat4 u_World;
|
mat4 World;
|
||||||
vec4 u_Color;
|
vec4 Color;
|
||||||
};
|
};
|
||||||
|
|
||||||
void main() {
|
void main() {
|
||||||
v_Normal = mat3(u_World) * vec3(a_Normal.xyz);
|
v_Normal = mat3(World) * vec3(a_Normal.xyz);
|
||||||
v_Position = u_World * vec4(a_Pos);
|
v_Position = World * vec4(a_Pos);
|
||||||
gl_Position = u_ViewProj * v_Position;
|
gl_Position = ViewProj * v_Position;
|
||||||
}
|
}
|
||||||
|
|||||||
@ -2,9 +2,8 @@ use crate::render::{
|
|||||||
Vertex,
|
Vertex,
|
||||||
{
|
{
|
||||||
render_graph_2::{
|
render_graph_2::{
|
||||||
mesh_draw_target, PassDescriptor, PipelineDescriptor, RenderGraphBuilder,
|
mesh_draw_target, resource, pipeline_layout::*, PassDescriptor, PipelineDescriptor,
|
||||||
RenderPassColorAttachmentDescriptor,
|
RenderGraphBuilder, RenderPassColorAttachmentDescriptor,
|
||||||
resource,
|
|
||||||
},
|
},
|
||||||
shader::{Shader, ShaderStage},
|
shader::{Shader, ShaderStage},
|
||||||
},
|
},
|
||||||
@ -25,6 +24,64 @@ impl ForwardPipelineBuilder for RenderGraphBuilder {
|
|||||||
include_str!("forward.frag"),
|
include_str!("forward.frag"),
|
||||||
ShaderStage::Fragment,
|
ShaderStage::Fragment,
|
||||||
))
|
))
|
||||||
|
.add_bind_group(BindGroup {
|
||||||
|
bindings: vec![
|
||||||
|
Binding {
|
||||||
|
name: "Globals".to_string(),
|
||||||
|
bind_type: BindType::Uniform {
|
||||||
|
properties: vec![
|
||||||
|
UniformProperty {
|
||||||
|
name: "ViewProj".to_string(),
|
||||||
|
property_type: UniformPropertyType::Mat4,
|
||||||
|
},
|
||||||
|
UniformProperty {
|
||||||
|
name: "NumLights".to_string(),
|
||||||
|
property_type: UniformPropertyType::UVec4,
|
||||||
|
},
|
||||||
|
]
|
||||||
|
}
|
||||||
|
},
|
||||||
|
Binding {
|
||||||
|
name: "Lights".to_string(),
|
||||||
|
bind_type: BindType::Uniform {
|
||||||
|
properties: vec![
|
||||||
|
UniformProperty {
|
||||||
|
name: "SceneLights".to_string(),
|
||||||
|
property_type: UniformPropertyType::Array(
|
||||||
|
Box::new(UniformPropertyType::Struct(
|
||||||
|
vec![
|
||||||
|
UniformPropertyType::Mat4,
|
||||||
|
UniformPropertyType::Vec4,
|
||||||
|
UniformPropertyType::Vec4,
|
||||||
|
]
|
||||||
|
)),
|
||||||
|
10
|
||||||
|
),
|
||||||
|
},
|
||||||
|
]
|
||||||
|
}
|
||||||
|
}
|
||||||
|
]
|
||||||
|
})
|
||||||
|
.add_bind_group(BindGroup {
|
||||||
|
bindings: vec![
|
||||||
|
Binding {
|
||||||
|
name: "Entity".to_string(),
|
||||||
|
bind_type: BindType::Uniform {
|
||||||
|
properties: vec![
|
||||||
|
UniformProperty {
|
||||||
|
name: "World".to_string(),
|
||||||
|
property_type: UniformPropertyType::Mat4,
|
||||||
|
},
|
||||||
|
UniformProperty {
|
||||||
|
name: "Color".to_string(),
|
||||||
|
property_type: UniformPropertyType::Vec4,
|
||||||
|
},
|
||||||
|
]
|
||||||
|
}
|
||||||
|
},
|
||||||
|
]
|
||||||
|
})
|
||||||
.with_rasterization_state(wgpu::RasterizationStateDescriptor {
|
.with_rasterization_state(wgpu::RasterizationStateDescriptor {
|
||||||
front_face: wgpu::FrontFace::Ccw,
|
front_face: wgpu::FrontFace::Ccw,
|
||||||
cull_mode: wgpu::CullMode::Back,
|
cull_mode: wgpu::CullMode::Back,
|
||||||
@ -41,14 +98,14 @@ impl ForwardPipelineBuilder for RenderGraphBuilder {
|
|||||||
stencil_read_mask: 0,
|
stencil_read_mask: 0,
|
||||||
stencil_write_mask: 0,
|
stencil_write_mask: 0,
|
||||||
})
|
})
|
||||||
.with_color_state(wgpu::ColorStateDescriptor {
|
.add_color_state(wgpu::ColorStateDescriptor {
|
||||||
format: wgpu::TextureFormat::Bgra8UnormSrgb,
|
format: wgpu::TextureFormat::Bgra8UnormSrgb,
|
||||||
color_blend: wgpu::BlendDescriptor::REPLACE,
|
color_blend: wgpu::BlendDescriptor::REPLACE,
|
||||||
alpha_blend: wgpu::BlendDescriptor::REPLACE,
|
alpha_blend: wgpu::BlendDescriptor::REPLACE,
|
||||||
write_mask: wgpu::ColorWrite::ALL,
|
write_mask: wgpu::ColorWrite::ALL,
|
||||||
})
|
})
|
||||||
.with_vertex_buffer_descriptor(Vertex::get_vertex_buffer_descriptor())
|
.add_vertex_buffer_descriptor(Vertex::get_vertex_buffer_descriptor())
|
||||||
.with_draw_target(mesh_draw_target)
|
.add_draw_target(mesh_draw_target)
|
||||||
.build(),
|
.build(),
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
|||||||
@ -6,6 +6,7 @@ pub enum ShaderValue<'a> {
|
|||||||
Int(u32),
|
Int(u32),
|
||||||
Float(f32),
|
Float(f32),
|
||||||
Vec4(Vec4),
|
Vec4(Vec4),
|
||||||
|
Uniform(&'a [u8]),
|
||||||
Texture(&'a Handle<Texture>),
|
Texture(&'a Handle<Texture>),
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user