bind group data model

This commit is contained in:
Carter Anderson 2020-01-20 23:05:53 -08:00
parent 129a9747bb
commit 5975289f4c
9 changed files with 172 additions and 40 deletions

View File

@ -14,16 +14,16 @@ struct Light {
};
layout(set = 0, binding = 0) uniform Globals {
mat4 u_ViewProj;
uvec4 u_NumLights;
mat4 ViewProj;
uvec4 NumLights;
};
layout(set = 0, binding = 1) uniform Lights {
Light u_Lights[MAX_LIGHTS];
Light SceneLights[MAX_LIGHTS];
};
layout(set = 1, binding = 0) uniform Entity {
mat4 u_World;
vec4 u_Color;
mat4 World;
vec4 Color;
};
void main() {
@ -31,8 +31,8 @@ void main() {
vec3 ambient = vec3(0.05, 0.05, 0.05);
// accumulate color
vec3 color = ambient;
for (int i=0; i<int(u_NumLights.x) && i<MAX_LIGHTS; ++i) {
Light light = u_Lights[i];
for (int i=0; i<int(NumLights.x) && i<MAX_LIGHTS; ++i) {
Light light = SceneLights[i];
// compute Lambertian diffuse term
vec3 light_dir = normalize(light.pos.xyz - v_Position.xyz);
float diffuse = max(0.0, dot(normal, light_dir));
@ -40,5 +40,5 @@ void main() {
color += diffuse * light.color.xyz;
}
// multiply the light by material color
o_Target = vec4(color, 1.0) * u_Color;
o_Target = vec4(color, 1.0) * Color;
}

View File

@ -8,16 +8,16 @@ layout(location = 0) out vec3 v_Normal;
layout(location = 1) out vec4 v_Position;
layout(set = 0, binding = 0) uniform Globals {
mat4 u_ViewProj;
uvec4 u_NumLights;
mat4 ViewProj;
uvec4 NumLights;
};
layout(set = 1, binding = 0) uniform Entity {
mat4 u_World;
vec4 u_Color;
mat4 World;
vec4 Color;
};
void main() {
v_Normal = mat3(u_World) * vec3(a_Normal.xyz);
v_Position = u_World * vec4(a_Pos);
gl_Position = u_ViewProj * v_Position;
v_Normal = mat3(World) * vec3(a_Normal.xyz);
v_Position = World * vec4(a_Pos);
gl_Position = ViewProj * v_Position;
}

View File

@ -2,6 +2,7 @@ pub mod pipelines;
pub mod resource;
pub mod wgpu_renderer;
mod pipeline;
mod pipeline_layout;
mod pass;
mod renderer;
mod shader;
@ -9,6 +10,7 @@ mod render_graph;
mod draw_target;
pub use pipeline::*;
pub use pipeline_layout::*;
pub use pass::*;
pub use renderer::*;
pub use shader::*;

View File

@ -1,5 +1,5 @@
use crate::render::{
render_graph_2::DrawTarget,
render_graph_2::{DrawTarget, PipelineLayout, BindGroup},
shader::{Shader, ShaderStages},
};
@ -21,6 +21,7 @@ impl<'a> Into<wgpu::VertexBufferDescriptor<'a>> for &'a VertexBufferDescriptor {
pub struct PipelineDescriptor {
pub draw_targets: Vec<DrawTarget>,
pub pipeline_layout: PipelineLayout,
pub shader_stages: ShaderStages,
pub rasterization_state: Option<wgpu::RasterizationStateDescriptor>,
@ -56,6 +57,7 @@ pub struct PipelineDescriptor {
impl PipelineDescriptor {
fn new(vertex_shader: Shader) -> Self {
PipelineDescriptor {
pipeline_layout: PipelineLayout::new(),
color_states: Vec::new(),
depth_stencil_state: None,
draw_targets: Vec::new(),
@ -105,7 +107,7 @@ impl PipelineBuilder {
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
}
@ -121,7 +123,12 @@ impl PipelineBuilder {
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 vertex_buffer_descriptor: VertexBufferDescriptor,
) -> Self {
@ -144,7 +151,7 @@ impl PipelineBuilder {
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
}

View 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,
}

View File

@ -14,16 +14,16 @@ struct Light {
};
layout(set = 0, binding = 0) uniform Globals {
mat4 u_ViewProj;
uvec4 u_NumLights;
mat4 ViewProj;
uvec4 NumLights;
};
layout(set = 0, binding = 1) uniform Lights {
Light u_Lights[MAX_LIGHTS];
Light SceneLights[MAX_LIGHTS];
};
layout(set = 1, binding = 0) uniform Entity {
mat4 u_World;
vec4 u_Color;
mat4 World;
vec4 Color;
};
void main() {
@ -31,8 +31,8 @@ void main() {
vec3 ambient = vec3(0.05, 0.05, 0.05);
// accumulate color
vec3 color = ambient;
for (int i=0; i<int(u_NumLights.x) && i<MAX_LIGHTS; ++i) {
Light light = u_Lights[i];
for (int i=0; i<int(NumLights.x) && i<MAX_LIGHTS; ++i) {
Light light = SceneLights[i];
// compute Lambertian diffuse term
vec3 light_dir = normalize(light.pos.xyz - v_Position.xyz);
float diffuse = max(0.0, dot(normal, light_dir));
@ -40,5 +40,5 @@ void main() {
color += diffuse * light.color.xyz;
}
// multiply the light by material color
o_Target = vec4(color, 1.0) * u_Color;
o_Target = vec4(color, 1.0) * Color;
}

View File

@ -8,16 +8,16 @@ layout(location = 0) out vec3 v_Normal;
layout(location = 1) out vec4 v_Position;
layout(set = 0, binding = 0) uniform Globals {
mat4 u_ViewProj;
uvec4 u_NumLights;
mat4 ViewProj;
uvec4 NumLights;
};
layout(set = 1, binding = 0) uniform Entity {
mat4 u_World;
vec4 u_Color;
mat4 World;
vec4 Color;
};
void main() {
v_Normal = mat3(u_World) * vec3(a_Normal.xyz);
v_Position = u_World * vec4(a_Pos);
gl_Position = u_ViewProj * v_Position;
v_Normal = mat3(World) * vec3(a_Normal.xyz);
v_Position = World * vec4(a_Pos);
gl_Position = ViewProj * v_Position;
}

View File

@ -2,9 +2,8 @@ use crate::render::{
Vertex,
{
render_graph_2::{
mesh_draw_target, PassDescriptor, PipelineDescriptor, RenderGraphBuilder,
RenderPassColorAttachmentDescriptor,
resource,
mesh_draw_target, resource, pipeline_layout::*, PassDescriptor, PipelineDescriptor,
RenderGraphBuilder, RenderPassColorAttachmentDescriptor,
},
shader::{Shader, ShaderStage},
},
@ -25,6 +24,64 @@ impl ForwardPipelineBuilder for RenderGraphBuilder {
include_str!("forward.frag"),
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 {
front_face: wgpu::FrontFace::Ccw,
cull_mode: wgpu::CullMode::Back,
@ -41,14 +98,14 @@ impl ForwardPipelineBuilder for RenderGraphBuilder {
stencil_read_mask: 0,
stencil_write_mask: 0,
})
.with_color_state(wgpu::ColorStateDescriptor {
.add_color_state(wgpu::ColorStateDescriptor {
format: wgpu::TextureFormat::Bgra8UnormSrgb,
color_blend: wgpu::BlendDescriptor::REPLACE,
alpha_blend: wgpu::BlendDescriptor::REPLACE,
write_mask: wgpu::ColorWrite::ALL,
})
.with_vertex_buffer_descriptor(Vertex::get_vertex_buffer_descriptor())
.with_draw_target(mesh_draw_target)
.add_vertex_buffer_descriptor(Vertex::get_vertex_buffer_descriptor())
.add_draw_target(mesh_draw_target)
.build(),
)
}

View File

@ -6,6 +6,7 @@ pub enum ShaderValue<'a> {
Int(u32),
Float(f32),
Vec4(Vec4),
Uniform(&'a [u8]),
Texture(&'a Handle<Texture>),
}