migrated intermediate tutorial docs

pull/140/head
Ben Hansen 3 years ago
parent e3592e2809
commit 87d0a97404

@ -119,69 +119,51 @@ fn create_render_pipeline(
color_format: wgpu::TextureFormat, color_format: wgpu::TextureFormat,
depth_format: Option<wgpu::TextureFormat>, depth_format: Option<wgpu::TextureFormat>,
vertex_layouts: &[wgpu::VertexBufferLayout], vertex_layouts: &[wgpu::VertexBufferLayout],
vs_src: &str, vs_src: wgpu::ShaderModuleDescriptor,
fs_src: &str, fs_src: wgpu::ShaderModuleDescriptor,
) -> wgpu::RenderPipeline { ) -> wgpu::RenderPipeline {
let mut compiler = shaderc::Compiler::new().unwrap(); let vs_module = device.create_shader_module(&vs_src);
let vs_spirv = compiler.compile_into_spirv(vs_src, shaderc::ShaderKind::Vertex, "shader.vert", "main", None).unwrap(); let fs_module = device.create_shader_module(&fs_src);
let fs_spirv = compiler.compile_into_spirv(fs_src, shaderc::ShaderKind::Fragment, "shader.frag", "main", None).unwrap();
let vs_data = wgpu::read_spirv(std::io::Cursor::new(vs_spirv.as_binary_u8())).unwrap();
let fs_data = wgpu::read_spirv(std::io::Cursor::new(fs_spirv.as_binary_u8())).unwrap();
let vs_module = device.create_shader_module(&vs_data);
let fs_module = device.create_shader_module(&fs_data);
device.create_render_pipeline(&wgpu::RenderPipelineDescriptor { device.create_render_pipeline(&wgpu::RenderPipelineDescriptor {
layout: &layout, label: Some("Render Pipeline"),
layout: Some(&layout),
vertex: wgpu::VertexState { vertex: wgpu::VertexState {
module: &vs_module, module: &vs_module,
entry_point: "main", entry_point: "main",
buffers: vertex_layouts,
}, },
fragment: Some(wgpu::FragmentState { fragment: Some(wgpu::FragmentState {
module: &fs_module, module: &fs_module,
entry_point: "main", entry_point: "main",
targets: &[wgpu::ColorTargetState {
format: color_format,
alpha_blend: wgpu::BlendState::REPLACE,
color_blend: wgpu::BlendState::REPLACE,
write_mask: wgpu::ColorWrite::ALL,
}],
}), }),
rasterization_state: Some(wgpu::RasterizationStateDescriptor { primitive: wgpu::PrimitiveState {
topology: wgpu::PrimitiveTopology::TriangleList,
strip_index_format: None,
front_face: wgpu::FrontFace::Ccw, front_face: wgpu::FrontFace::Ccw,
cull_mode: wgpu::CullMode::Back, cull_mode: wgpu::CullMode::Back,
depth_bias: 0, // Setting this to anything other than Fill requires Features::NON_FILL_POLYGON_MODE
depth_bias_slope_scale: 0.0, polygon_mode: wgpu::PolygonMode::Fill,
depth_bias_clamp: 0.0, },
}), depth_stencil: depth_format.map(|format| wgpu::DepthStencilState {
primitive: wgpu::PrimitiveState { format,
topology: wgpu::PrimitiveTopology::TriangleList, depth_write_enabled: true,
strip_index_format: None, depth_compare: wgpu::CompareFunction::Less,
front_face: wgpu::FrontFace::Ccw, stencil: wgpu::StencilState::default(),
cull_mode: wgpu::CullMode::Back, bias: wgpu::DepthBiasState::default(),
// Setting this to anything other than Fill requires Features::NON_FILL_POLYGON_MODE // Setting this to true requires Features::DEPTH_CLAMPING
polygon_mode: wgpu::PolygonMode::Fill, clamp_depth: false,
},
color_states: &[
wgpu::ColorStateDescriptor {
format: color_format,
color_blend: wgpu::BlendDescriptor::REPLACE,
alpha_blend: wgpu::BlendDescriptor::REPLACE,
write_mask: wgpu::ColorWrite::ALL,
},
],
depth_stencil: depth_format.map(|format| {
wgpu::DepthStencilState {
format,
depth_write_enabled: true,
depth_compare: wgpu::CompareFunction::Less,
stencil_front: wgpu::StencilStateFaceDescriptor::IGNORE,
stencil_back: wgpu::StencilStateFaceDescriptor::IGNORE,
stencil_read_mask: 0,
stencil_write_mask: 0,
}
}), }),
sample_count: 1, multisample: wgpu::MultisampleState {
sample_mask: !0, count: 1,
alpha_to_coverage_enabled: false, mask: !0,
vertex_state: wgpu::VertexStateDescriptor { alpha_to_coverage_enabled: false,
index_format: wgpu::IndexFormat::Uint32,
vertex_buffers: vertex_layouts,
}, },
}) })
} }
@ -190,20 +172,15 @@ fn create_render_pipeline(
We also need to change `State::new()` to use this function. We also need to change `State::new()` to use this function.
```rust ```rust
let render_pipeline = { let render_pipeline = create_render_pipeline(
let vs_src = include_str!("shader.vert"); &device,
let fs_src = include_str!("shader.frag"); &render_pipeline_layout,
sc_desc.format,
create_render_pipeline( Some(texture::Texture::DEPTH_FORMAT),
&device, &[model::ModelVertex::desc(), InstanceRaw::desc()],
&render_pipeline_layout, wgpu::include_spirv!("shader.vert.spv"),
sc_desc.format, wgpu::include_spirv!("shader.frag.spv"),
Some(texture::Texture::DEPTH_FORMAT), );
&[model::ModelVertex::desc()],
vs_src,
fs_src
)
};
``` ```
We're going to need to modify `model::DrawModel` to use our `light_bind_group`. We're going to need to modify `model::DrawModel` to use our `light_bind_group`.
@ -303,23 +280,19 @@ With that done we can create another render pipeline for our light.
```rust ```rust
let light_render_pipeline = { let light_render_pipeline = {
let layout = device.create_pipeline_layout(&wgpu::PipelineLayoutDescriptor { let layout = device.create_pipeline_layout(&wgpu::PipelineLayoutDescriptor {
bind_group_layouts: &[ label: Some("Light Pipeline Layout"),
&uniform_bind_group_layout, bind_group_layouts: &[&uniform_bind_group_layout, &light_bind_group_layout],
&light_bind_group_layout, push_constant_ranges: &[],
]
}); });
let vs_src = include_str!("light.vert");
let fs_src = include_str!("light.frag");
create_render_pipeline( create_render_pipeline(
&device, &device,
&layout, &layout,
sc_desc.format, sc_desc.format,
Some(texture::Texture::DEPTH_FORMAT), Some(texture::Texture::DEPTH_FORMAT),
&[model::ModelVertex::desc()], &[model::ModelVertex::desc()],
vs_src, wgpu::include_spirv!("light.vert.spv"),
fs_src, wgpu::include_spirv!("light.frag.spv"),
) )
}; };
``` ```

@ -31,10 +31,10 @@ let texture_bind_group_layout = device.create_bind_group_layout(&wgpu::BindGroup
wgpu::BindGroupLayoutEntry { wgpu::BindGroupLayoutEntry {
binding: 2, binding: 2,
visibility: wgpu::ShaderStage::FRAGMENT, visibility: wgpu::ShaderStage::FRAGMENT,
ty: wgpu::BindingType::SampledTexture { ty: wgpu::BindingType::Texture {
multisampled: false, multisampled: false,
component_type: wgpu::TextureSampleType::Float { filterable: true }, sample_type: wgpu::TextureSampleType::Float { filterable: true },
dimension: wgpu::TextureViewDimension::D2, view_dimension: wgpu::TextureViewDimension::D2,
}, },
count: None, count: None,
}, },
@ -42,9 +42,9 @@ let texture_bind_group_layout = device.create_bind_group_layout(&wgpu::BindGroup
binding: 3, binding: 3,
visibility: wgpu::ShaderStage::FRAGMENT, visibility: wgpu::ShaderStage::FRAGMENT,
ty: wgpu::BindingType::Sampler { ty: wgpu::BindingType::Sampler {
comparison: false, comparison: false,
filtering: true, filtering: true,
}, },
count: None, count: None,
}, },
], ],

Loading…
Cancel
Save