beginner tutorial code done

This commit is contained in:
Ben Hansen 2021-02-12 00:18:27 -07:00
parent 91879d405b
commit 3901419a58
24 changed files with 107 additions and 68 deletions

View File

@ -317,17 +317,20 @@ impl DepthPass {
wgpu::BindGroupLayoutEntry {
binding: 0,
count: None,
ty: wgpu::BindingType::SampledTexture {
component_type: wgpu::TextureComponentType::Float,
ty: wgpu::BindingType::Texture {
sample_type: wgpu::TextureSampleType::Depth,
multisampled: false,
dimension: wgpu::TextureViewDimension::D2,
view_dimension: wgpu::TextureViewDimension::D2,
},
visibility: wgpu::ShaderStage::FRAGMENT,
},
wgpu::BindGroupLayoutEntry {
binding: 1,
count: None,
ty: wgpu::BindingType::Sampler { comparison: true },
ty: wgpu::BindingType::Sampler {
comparison: true,
filtering: true,
},
visibility: wgpu::ShaderStage::FRAGMENT,
},
],
@ -433,6 +436,7 @@ impl DepthPass {
fn render(&self, frame: &wgpu::SwapChainTexture, encoder: &mut wgpu::CommandEncoder) {
let mut render_pass = encoder.begin_render_pass(&wgpu::RenderPassDescriptor {
label: Some("Depth Visual Render Pass"),
color_attachments: &[wgpu::RenderPassColorAttachmentDescriptor {
attachment: &frame.view,
resolve_target: None,
@ -652,7 +656,7 @@ impl State {
vertex: wgpu::VertexState {
module: &vs_module,
entry_point: "main",
buffers: &[Vertex::desc()],
buffers: &[Vertex::desc(), InstanceRaw::desc()],
},
fragment: Some(wgpu::FragmentState {
module: &fs_module,
@ -664,14 +668,6 @@ impl State {
write_mask: wgpu::ColorWrite::ALL,
}],
}),
rasterization_state: Some(wgpu::RasterizationStateDescriptor {
front_face: wgpu::FrontFace::Ccw,
cull_mode: wgpu::CullMode::Back,
depth_bias: 2, // corresponds to bilinear filtering
depth_bias_slope_scale: 2.0,
depth_bias_clamp: 0.0,
clamp_depth: device.features().contains(wgpu::Features::DEPTH_CLAMPING),
}),
primitive: wgpu::PrimitiveState {
topology: wgpu::PrimitiveTopology::TriangleList,
strip_index_format: None,
@ -680,12 +676,18 @@ impl State {
// Setting this to anything other than Fill requires Features::NON_FILL_POLYGON_MODE
polygon_mode: wgpu::PolygonMode::Fill,
},
depth_stencil: Some(wgpu::DepthStencilStateDescriptor {
depth_stencil: Some(wgpu::DepthStencilState {
format: texture::Texture::DEPTH_FORMAT,
depth_write_enabled: true,
depth_compare: wgpu::CompareFunction::Less,
stencil: wgpu::StencilStateDescriptor::default(),
stencil: wgpu::StencilState::default(),
bias: wgpu::DepthBiasState {
constant: 2, // Corresponds to bilinear filtering
slope_scale: 2.0,
clamp: 0.0,
},
// Setting this to true requires Features::DEPTH_CLAMPING
clamp_depth: false,
}),
multisample: wgpu::MultisampleState {
count: 1,
@ -768,6 +770,7 @@ impl State {
{
let mut render_pass = encoder.begin_render_pass(&wgpu::RenderPassDescriptor {
label: Some("Render Pass"),
color_attachments: &[wgpu::RenderPassColorAttachmentDescriptor {
attachment: &frame.view,
resolve_target: None,

View File

@ -480,7 +480,7 @@ impl State {
vertex: wgpu::VertexState {
module: &vs_module,
entry_point: "main",
buffers: &[Vertex::desc()],
buffers: &[Vertex::desc(), InstanceRaw::desc()],
},
fragment: Some(wgpu::FragmentState {
module: &fs_module,
@ -501,11 +501,14 @@ impl State {
polygon_mode: wgpu::PolygonMode::Fill,
},
depth_stencil: Some(wgpu::DepthStencilStateDescriptor {
depth_stencil: Some(wgpu::DepthStencilState {
format: texture::Texture::DEPTH_FORMAT,
depth_write_enabled: true,
depth_compare: wgpu::CompareFunction::Less,
stencil: wgpu::StencilStateDescriptor::default(),
stencil: wgpu::StencilState::default(),
bias: wgpu::DepthBiasState::default(),
// Setting this to true requires Features::DEPTH_CLAMPING
clamp_depth: false,
}),
multisample: wgpu::MultisampleState {
count: 1,
@ -586,6 +589,7 @@ impl State {
{
let mut render_pass = encoder.begin_render_pass(&wgpu::RenderPassDescriptor {
label: Some("Render Pass"),
color_attachments: &[wgpu::RenderPassColorAttachmentDescriptor {
attachment: &frame.view,
resolve_target: None,

View File

@ -16,5 +16,12 @@ layout(location=7) in vec4 model_matrix_2;
layout(location=8) in vec4 model_matrix_3;
void main() {
v_tex_coords = a_tex_coords; gl_Position = u_view_proj * model_matrix * vec4(a_position, 1.0);
mat4 model_matrix = mat4(
model_matrix_0,
model_matrix_1,
model_matrix_2,
model_matrix_3
);
v_tex_coords = a_tex_coords;
gl_Position = u_view_proj * model_matrix * vec4(a_position, 1.0);
}

View File

@ -407,7 +407,7 @@ impl State {
vertex: wgpu::VertexState {
module: &vs_module,
entry_point: "main",
buffers: &[Vertex::desc()],
buffers: &[model::ModelVertex::desc(), InstanceRaw::desc()],
},
fragment: Some(wgpu::FragmentState {
module: &fs_module,
@ -427,16 +427,19 @@ impl State {
// Setting this to anything other than Fill requires Features::NON_FILL_POLYGON_MODE
polygon_mode: wgpu::PolygonMode::Fill,
},
depth_stencil: Some(wgpu::DepthStencilStateDescriptor {
depth_stencil: Some(wgpu::DepthStencilState {
format: texture::Texture::DEPTH_FORMAT,
depth_write_enabled: true,
depth_compare: wgpu::CompareFunction::Less,
stencil: wgpu::StencilStateDescriptor::default(),
stencil: wgpu::StencilState::default(),
bias: wgpu::DepthBiasState::default(),
// Setting this to true requires Features::DEPTH_CLAMPING
clamp_depth: false,
}),
vertex_state: wgpu::VertexStateDescriptor {
index_format: wgpu::IndexFormat::Uint32,
vertex_buffers: &[model::ModelVertex::desc(), InstanceRaw::desc()],
multisample: wgpu::MultisampleState {
count: 1,
mask: !0,
alpha_to_coverage_enabled: false,
},
});
@ -494,6 +497,7 @@ impl State {
{
let mut render_pass = encoder.begin_render_pass(&wgpu::RenderPassDescriptor {
label: Some("Render Pass"),
color_attachments: &[wgpu::RenderPassColorAttachmentDescriptor {
attachment: &frame.view,
resolve_target: None,

View File

@ -184,7 +184,7 @@ where
uniforms: &'b wgpu::BindGroup,
) {
self.set_vertex_buffer(0, mesh.vertex_buffer.slice(..));
self.set_index_buffer(mesh.index_buffer.slice(..));
self.set_index_buffer(mesh.index_buffer.slice(..), wgpu::IndexFormat::Uint32);
self.set_bind_group(0, &material.bind_group, &[]);
self.set_bind_group(1, &uniforms, &[]);
self.draw_indexed(0..mesh.num_elements, 0, instances);

View File

@ -16,5 +16,12 @@ layout(location=7) in vec4 model_matrix_2;
layout(location=8) in vec4 model_matrix_3;
void main() {
v_tex_coords = a_tex_coords; gl_Position = u_view_proj * model_matrix * vec4(a_position, 1.0);
mat4 model_matrix = mat4(
model_matrix_0,
model_matrix_1,
model_matrix_2,
model_matrix_3
);
v_tex_coords = a_tex_coords;
gl_Position = u_view_proj * model_matrix * vec4(a_position, 1.0);
}

View File

@ -305,12 +305,14 @@ fn create_render_pipeline(
alpha_blend: wgpu::BlendDescriptor::REPLACE,
write_mask: wgpu::ColorWrite::ALL,
}],
depth_stencil: depth_format.map(|format| wgpu::DepthStencilStateDescriptor {
depth_stencil: depth_format.map(|format| wgpu::DepthStencilState {
format,
depth_write_enabled: true,
depth_compare: wgpu::CompareFunction::Less,
stencil: wgpu::StencilStateDescriptor::default(),
}),
stencil: wgpu::StencilState::default(),
bias: wgpu::DepthBiasState::default(),
// Setting this to true requires Features::DEPTH_CLAMPING
clamp_depth: false, }),
sample_count: 1,
sample_mask: !0,
alpha_to_coverage_enabled: false,
@ -617,6 +619,7 @@ impl State {
{
let mut render_pass = encoder.begin_render_pass(&wgpu::RenderPassDescriptor {
label: Some("Render Pass"),
color_attachments: &[wgpu::RenderPassColorAttachmentDescriptor {
attachment: &frame.view,
resolve_target: None,

View File

@ -204,7 +204,7 @@ where
light: &'b wgpu::BindGroup,
) {
self.set_vertex_buffer(0, mesh.vertex_buffer.slice(..));
self.set_index_buffer(mesh.index_buffer.slice(..));
self.set_index_buffer(mesh.index_buffer.slice(..), wgpu::IndexFormat::Uint32);
self.set_bind_group(0, &material.bind_group, &[]);
self.set_bind_group(1, &uniforms, &[]);
self.set_bind_group(2, &light, &[]);
@ -289,7 +289,7 @@ where
light: &'b wgpu::BindGroup,
) {
self.set_vertex_buffer(0, mesh.vertex_buffer.slice(..));
self.set_index_buffer(mesh.index_buffer.slice(..));
self.set_index_buffer(mesh.index_buffer.slice(..), wgpu::IndexFormat::Uint32);
self.set_bind_group(0, uniforms, &[]);
self.set_bind_group(1, light, &[]);
self.draw_indexed(0..mesh.num_elements, 0, instances);

View File

@ -302,12 +302,14 @@ fn create_render_pipeline(
alpha_blend: wgpu::BlendDescriptor::REPLACE,
write_mask: wgpu::ColorWrite::ALL,
}],
depth_stencil: depth_format.map(|format| wgpu::DepthStencilStateDescriptor {
depth_stencil: depth_format.map(|format| wgpu::DepthStencilState {
format,
depth_write_enabled: true,
depth_compare: wgpu::CompareFunction::Less,
stencil: wgpu::StencilStateDescriptor::default(),
}),
stencil: wgpu::StencilState::default(),
bias: wgpu::DepthBiasState::default(),
// Setting this to true requires Features::DEPTH_CLAMPING
clamp_depth: false, }),
sample_count: 1,
sample_mask: !0,
alpha_to_coverage_enabled: false,
@ -666,6 +668,7 @@ impl State {
{
let mut render_pass = encoder.begin_render_pass(&wgpu::RenderPassDescriptor {
label: Some("Render Pass"),
color_attachments: &[wgpu::RenderPassColorAttachmentDescriptor {
attachment: &frame.view,
resolve_target: None,

View File

@ -309,7 +309,7 @@ where
light: &'b wgpu::BindGroup,
) {
self.set_vertex_buffer(0, mesh.vertex_buffer.slice(..));
self.set_index_buffer(mesh.index_buffer.slice(..));
self.set_index_buffer(mesh.index_buffer.slice(..), wgpu::IndexFormat::Uint32);
self.set_bind_group(0, &material.bind_group, &[]);
self.set_bind_group(1, &uniforms, &[]);
self.set_bind_group(2, &light, &[]);
@ -407,7 +407,7 @@ where
light: &'b wgpu::BindGroup,
) {
self.set_vertex_buffer(0, mesh.vertex_buffer.slice(..));
self.set_index_buffer(mesh.index_buffer.slice(..));
self.set_index_buffer(mesh.index_buffer.slice(..), wgpu::IndexFormat::Uint32);
self.set_bind_group(0, uniforms, &[]);
self.set_bind_group(1, light, &[]);
self.draw_indexed(0..mesh.num_elements, 0, instances);

View File

@ -183,12 +183,14 @@ fn create_render_pipeline(
alpha_blend: wgpu::BlendDescriptor::REPLACE,
write_mask: wgpu::ColorWrite::ALL,
}],
depth_stencil: depth_format.map(|format| wgpu::DepthStencilStateDescriptor {
depth_stencil: depth_format.map(|format| wgpu::DepthStencilState {
format,
depth_write_enabled: true,
depth_compare: wgpu::CompareFunction::Less,
stencil: wgpu::StencilStateDescriptor::default(),
}),
stencil: wgpu::StencilState::default(),
bias: wgpu::DepthBiasState::default(),
// Setting this to true requires Features::DEPTH_CLAMPING
clamp_depth: false, }),
sample_count: 1,
sample_mask: !0,
alpha_to_coverage_enabled: false,
@ -574,6 +576,7 @@ impl State {
{
let mut render_pass = encoder.begin_render_pass(&wgpu::RenderPassDescriptor {
label: Some("Render Pass"),
color_attachments: &[wgpu::RenderPassColorAttachmentDescriptor {
attachment: &frame.view,
resolve_target: None,

View File

@ -309,7 +309,7 @@ where
light: &'b wgpu::BindGroup,
) {
self.set_vertex_buffer(0, mesh.vertex_buffer.slice(..));
self.set_index_buffer(mesh.index_buffer.slice(..));
self.set_index_buffer(mesh.index_buffer.slice(..), wgpu::IndexFormat::Uint32);
self.set_bind_group(0, &material.bind_group, &[]);
self.set_bind_group(1, &uniforms, &[]);
self.set_bind_group(2, &light, &[]);
@ -407,7 +407,7 @@ where
light: &'b wgpu::BindGroup,
) {
self.set_vertex_buffer(0, mesh.vertex_buffer.slice(..));
self.set_index_buffer(mesh.index_buffer.slice(..));
self.set_index_buffer(mesh.index_buffer.slice(..), wgpu::IndexFormat::Uint32);
self.set_bind_group(0, uniforms, &[]);
self.set_bind_group(1, light, &[]);
self.draw_indexed(0..mesh.num_elements, 0, instances);

View File

@ -183,12 +183,14 @@ fn create_render_pipeline(
alpha_blend: wgpu::BlendDescriptor::REPLACE,
write_mask: wgpu::ColorWrite::ALL,
}],
depth_stencil: depth_format.map(|format| wgpu::DepthStencilStateDescriptor {
depth_stencil: depth_format.map(|format| wgpu::DepthStencilState {
format,
depth_write_enabled: true,
depth_compare: wgpu::CompareFunction::Less,
stencil: wgpu::StencilStateDescriptor::default(),
}),
stencil: wgpu::StencilState::default(),
bias: wgpu::DepthBiasState::default(),
// Setting this to true requires Features::DEPTH_CLAMPING
clamp_depth: false, }),
sample_count: 1,
sample_mask: !0,
alpha_to_coverage_enabled: false,
@ -571,6 +573,7 @@ impl State {
{
let mut render_pass = encoder.begin_render_pass(&wgpu::RenderPassDescriptor {
label: Some("Render Pass"),
color_attachments: &[wgpu::RenderPassColorAttachmentDescriptor {
attachment: &frame.view,
resolve_target: None,

View File

@ -327,7 +327,7 @@ where
light: &'b wgpu::BindGroup,
) {
self.set_vertex_buffer(0, mesh.vertex_buffer.slice(..));
self.set_index_buffer(mesh.index_buffer.slice(..));
self.set_index_buffer(mesh.index_buffer.slice(..), wgpu::IndexFormat::Uint32);
self.set_bind_group(0, &material.bind_group, &[]);
self.set_bind_group(1, &uniforms, &[]);
self.set_bind_group(2, &light, &[]);
@ -425,7 +425,7 @@ where
light: &'b wgpu::BindGroup,
) {
self.set_vertex_buffer(0, mesh.vertex_buffer.slice(..));
self.set_index_buffer(mesh.index_buffer.slice(..));
self.set_index_buffer(mesh.index_buffer.slice(..), wgpu::IndexFormat::Uint32);
self.set_bind_group(0, uniforms, &[]);
self.set_bind_group(1, light, &[]);
self.draw_indexed(0..mesh.num_elements, 0, instances);

View File

@ -11,7 +11,7 @@ pub struct RenderPipelineBuilder<'a> {
depth_bias_clamp: f32,
primitive_topology: wgpu::PrimitiveTopology,
color_states: Vec<wgpu::ColorStateDescriptor>,
depth_stencil: Option<wgpu::DepthStencilStateDescriptor>,
depth_stencil: Option<wgpu::DepthStencilState>,
index_format: wgpu::IndexFormat,
vertex_buffers: Vec<wgpu::VertexBufferLayout<'a>>,
sample_count: u32,
@ -111,7 +111,7 @@ impl<'a> RenderPipelineBuilder<'a> {
})
}
pub fn depth_stencil(&mut self, dss: wgpu::DepthStencilStateDescriptor) -> &mut Self {
pub fn depth_stencil(&mut self, dss: wgpu::DepthStencilState) -> &mut Self {
self.depth_stencil = Some(dss);
self
}
@ -123,7 +123,7 @@ impl<'a> RenderPipelineBuilder<'a> {
depth_write_enabled: bool,
depth_compare: wgpu::CompareFunction,
) -> &mut Self {
self.depth_stencil(wgpu::DepthStencilStateDescriptor {
self.depth_stencil(wgpu::DepthStencilState {
format,
depth_write_enabled,
depth_compare,

View File

@ -446,7 +446,7 @@ where
light: &'b wgpu::BindGroup,
) {
self.set_vertex_buffer(0, mesh.vertex_buffer.slice(..));
self.set_index_buffer(mesh.index_buffer.slice(..));
self.set_index_buffer(mesh.index_buffer.slice(..), wgpu::IndexFormat::Uint32);
self.set_bind_group(0, &material.bind_group, &[]);
self.set_bind_group(1, &uniforms, &[]);
self.set_bind_group(2, &light, &[]);
@ -544,7 +544,7 @@ where
light: &'b wgpu::BindGroup,
) {
self.set_vertex_buffer(0, mesh.vertex_buffer.slice(..));
self.set_index_buffer(mesh.index_buffer.slice(..));
self.set_index_buffer(mesh.index_buffer.slice(..), wgpu::IndexFormat::Uint32);
self.set_bind_group(0, uniforms, &[]);
self.set_bind_group(1, light, &[]);
self.draw_indexed(0..mesh.num_elements, 0, instances);

View File

@ -79,12 +79,14 @@ pub fn create_render_pipeline(
alpha_blend: wgpu::BlendDescriptor::REPLACE,
write_mask: wgpu::ColorWrite::ALL,
}],
depth_stencil: depth_format.map(|format| wgpu::DepthStencilStateDescriptor {
depth_stencil: depth_format.map(|format| wgpu::DepthStencilState {
format,
depth_write_enabled: true,
depth_compare: wgpu::CompareFunction::Less,
stencil: wgpu::StencilStateDescriptor::default(),
}),
stencil: wgpu::StencilState::default(),
bias: wgpu::DepthBiasState::default(),
// Setting this to true requires Features::DEPTH_CLAMPING
clamp_depth: false, }),
sample_count: 1,
sample_mask: !0,
alpha_to_coverage_enabled: false,

View File

@ -309,7 +309,7 @@ where
light: &'b wgpu::BindGroup,
) {
self.set_vertex_buffer(0, mesh.vertex_buffer.slice(..));
self.set_index_buffer(mesh.index_buffer.slice(..));
self.set_index_buffer(mesh.index_buffer.slice(..), wgpu::IndexFormat::Uint32);
self.set_bind_group(0, &material.bind_group, &[]);
self.set_bind_group(1, &uniforms, &[]);
self.set_bind_group(2, &light, &[]);
@ -407,7 +407,7 @@ where
light: &'b wgpu::BindGroup,
) {
self.set_vertex_buffer(0, mesh.vertex_buffer.slice(..));
self.set_index_buffer(mesh.index_buffer.slice(..));
self.set_index_buffer(mesh.index_buffer.slice(..), wgpu::IndexFormat::Uint32);
self.set_bind_group(0, uniforms, &[]);
self.set_bind_group(1, light, &[]);
self.draw_indexed(0..mesh.num_elements, 0, instances);

View File

@ -12,7 +12,7 @@ pub struct RenderPipelineBuilder<'a> {
depth_bias_clamp: f32,
primitive_topology: wgpu::PrimitiveTopology,
color_states: Vec<wgpu::ColorStateDescriptor>,
depth_stencil: Option<wgpu::DepthStencilStateDescriptor>,
depth_stencil: Option<wgpu::DepthStencilState>,
index_format: wgpu::IndexFormat,
vertex_buffers: Vec<wgpu::VertexBufferLayout<'a>>,
sample_count: u32,
@ -112,7 +112,7 @@ impl<'a> RenderPipelineBuilder<'a> {
})
}
pub fn depth_stencil(&mut self, dss: wgpu::DepthStencilStateDescriptor) -> &mut Self {
pub fn depth_stencil(&mut self, dss: wgpu::DepthStencilState) -> &mut Self {
self.depth_stencil = Some(dss);
self
}
@ -124,7 +124,7 @@ impl<'a> RenderPipelineBuilder<'a> {
depth_write_enabled: bool,
depth_compare: wgpu::CompareFunction,
) -> &mut Self {
self.depth_stencil(wgpu::DepthStencilStateDescriptor {
self.depth_stencil(wgpu::DepthStencilState {
format,
depth_write_enabled,
depth_compare,

View File

@ -82,11 +82,11 @@ We need to modify our `render_pipeline` to allow depth testing.
```rust
let render_pipeline = device.create_render_pipeline(&wgpu::RenderPipelineDescriptor {
// ...
depth_stencil: Some(wgpu::DepthStencilStateDescriptor {
depth_stencil: Some(wgpu::DepthStencilState {
format: texture::Texture::DEPTH_FORMAT,
depth_write_enabled: true,
depth_compare: wgpu::CompareFunction::Less, // 1.
stencil: wgpu::StencilStateDescriptor::default(), // 2.
stencil: wgpu::StencilState::default(), // 2.
}),
// ...
});

View File

@ -286,7 +286,7 @@ where
instances: Range<u32>,
){
self.set_vertex_buffer(0, mesh.vertex_buffer.slice(..));
self.set_index_buffer(mesh.index_buffer.slice(..));
self.set_index_buffer(mesh.index_buffer.slice(..), wgpu::IndexFormat::Uint32);
self.draw_indexed(0..mesh.num_elements, 0, instances);
}
}
@ -402,7 +402,7 @@ where
uniforms: &'b wgpu::BindGroup,
) {
self.set_vertex_buffer(0, mesh.vertex_buffer.slice(..));
self.set_index_buffer(mesh.index_buffer.slice(..));
self.set_index_buffer(mesh.index_buffer.slice(..), wgpu::IndexFormat::Uint32);
self.set_bind_group(0, &material.bind_group, &[]);
self.set_bind_group(1, &uniforms, &[]);
self.draw_indexed(0..mesh.num_elements, 0, instances);

View File

@ -166,7 +166,7 @@ fn create_render_pipeline(
},
],
depth_stencil: depth_format.map(|format| {
wgpu::DepthStencilStateDescriptor {
wgpu::DepthStencilState {
format,
depth_write_enabled: true,
depth_compare: wgpu::CompareFunction::Less,
@ -428,7 +428,7 @@ where
light: &'b wgpu::BindGroup,
) {
self.set_vertex_buffer(0, mesh.vertex_buffer.slice(..));
self.set_index_buffer(mesh.index_buffer.slice(..));
self.set_index_buffer(mesh.index_buffer.slice(..), wgpu::IndexFormat::Uint32);
self.set_bind_group(0, uniforms, &[]);
self.set_bind_group(1, light, &[]);
self.draw_indexed(0..mesh.num_elements, 0, instances);