migrated beginner tutorials

0.10
Ben Hansen 3 years ago
parent 358ecb55af
commit 7c90669f89

@ -168,7 +168,7 @@ fn main() {
state.update();
match state.render() {
Ok(_) => {}
// Recreate the swap_chain if lost
// Reconfigure the surface if lost
Err(wgpu::SurfaceError::Lost) => state.resize(state.size),
// The system is out of memory, we should probably quit
Err(wgpu::SurfaceError::OutOfMemory) => *control_flow = ControlFlow::Exit,

@ -155,7 +155,7 @@ fn main() {
state.update();
match state.render() {
Ok(_) => {}
// Recreate the swap_chain if lost
// Reconfigure the surface if lost
Err(wgpu::SurfaceError::Lost) => state.resize(state.size),
// The system is out of memory, we should probably quit
Err(wgpu::SurfaceError::OutOfMemory) => *control_flow = ControlFlow::Exit,

@ -273,7 +273,7 @@ fn main() {
state.update();
match state.render() {
Ok(_) => {}
// Recreate the swap_chain if lost
// Reconfigure the surface if lost
Err(wgpu::SurfaceError::Lost) => state.resize(state.size),
// The system is out of memory, we should probably quit
Err(wgpu::SurfaceError::OutOfMemory) => *control_flow = ControlFlow::Exit,

@ -211,7 +211,7 @@ fn main() {
state.update();
match state.render() {
Ok(_) => {}
// Recreate the swap_chain if lost
// Reconfigure the surface if lost
Err(wgpu::SurfaceError::Lost) => state.resize(state.size),
// The system is out of memory, we should probably quit
Err(wgpu::SurfaceError::OutOfMemory) => *control_flow = ControlFlow::Exit,

@ -353,7 +353,7 @@ fn main() {
state.update();
match state.render() {
Ok(_) => {}
// Recreate the swap_chain if lost
// Reconfigure the surface if lost
Err(wgpu::SurfaceError::Lost) => state.resize(state.size),
// The system is out of memory, we should probably quit
Err(wgpu::SurfaceError::OutOfMemory) => *control_flow = ControlFlow::Exit,

@ -285,7 +285,7 @@ fn main() {
state.update();
match state.render() {
Ok(_) => {}
// Recreate the swap_chain if lost
// Reconfigure the surface if lost
Err(wgpu::SurfaceError::Lost) => state.resize(state.size),
// The system is out of memory, we should probably quit
Err(wgpu::SurfaceError::OutOfMemory) => *control_flow = ControlFlow::Exit,

@ -383,7 +383,7 @@ fn main() {
state.update();
match state.render() {
Ok(_) => {}
// Recreate the swap_chain if lost
// Reconfigure the surface if lost
Err(wgpu::SurfaceError::Lost) => state.resize(state.size),
// The system is out of memory, we should probably quit
Err(wgpu::SurfaceError::OutOfMemory) => *control_flow = ControlFlow::Exit,

@ -338,7 +338,7 @@ fn main() {
state.update();
match state.render() {
Ok(_) => {}
// Recreate the swap_chain if lost
// Reconfigure the surface if lost
Err(wgpu::SurfaceError::Lost) => state.resize(state.size),
// The system is out of memory, we should probably quit
Err(wgpu::SurfaceError::OutOfMemory) => *control_flow = ControlFlow::Exit,

@ -566,7 +566,7 @@ fn main() {
state.update();
match state.render() {
Ok(_) => {}
// Recreate the swap_chain if lost
// Reconfigure the surface if lost
Err(wgpu::SurfaceError::Lost) => state.resize(state.size),
// The system is out of memory, we should probably quit
Err(wgpu::SurfaceError::OutOfMemory) => *control_flow = ControlFlow::Exit,

@ -547,7 +547,7 @@ fn main() {
state.update();
match state.render() {
Ok(_) => {}
// Recreate the swap_chain if lost
// Reconfigure the surface if lost
Err(wgpu::SurfaceError::Lost) => state.resize(state.size),
// The system is out of memory, we should probably quit
Err(wgpu::SurfaceError::OutOfMemory) => *control_flow = ControlFlow::Exit,

@ -695,7 +695,7 @@ fn main() {
state.update();
match state.render() {
Ok(_) => {}
// Recreate the swap_chain if lost
// Reconfigure the surface if lost
Err(wgpu::SurfaceError::Lost) => state.resize(state.size),
// The system is out of memory, we should probably quit
Err(wgpu::SurfaceError::OutOfMemory) => *control_flow = ControlFlow::Exit,

@ -645,7 +645,7 @@ fn main() {
state.update();
match state.render() {
Ok(_) => {}
// Recreate the swap_chain if lost
// Reconfigure the surface if lost
Err(wgpu::SurfaceError::Lost) => state.resize(state.size),
// The system is out of memory, we should probably quit
Err(wgpu::SurfaceError::OutOfMemory) => *control_flow = ControlFlow::Exit,

@ -442,7 +442,7 @@ impl DepthPass {
});
}
fn render(&self, frame: &wgpu::SwapChainTexture, encoder: &mut wgpu::CommandEncoder) {
fn render(&self, view: &wgpu::TextureView, encoder: &mut wgpu::CommandEncoder) {
let mut render_pass = encoder.begin_render_pass(&wgpu::RenderPassDescriptor {
label: Some("Depth Visual Render Pass"),
color_attachments: &[wgpu::RenderPassColorAttachment {
@ -814,7 +814,7 @@ impl State {
render_pass.draw_indexed(0..self.num_indices, 0, 0..self.instances.len() as u32);
}
self.depth_pass.render(&frame, &mut encoder);
self.depth_pass.render(&view, &mut encoder);
self.queue.submit(iter::once(encoder.finish()));
@ -863,7 +863,7 @@ fn main() {
state.update();
match state.render() {
Ok(_) => {}
// Recreate the swap_chain if lost
// Reconfigure the surface if lost
Err(wgpu::SurfaceError::Lost) => state.resize(state.size),
// The system is out of memory, we should probably quit
Err(wgpu::SurfaceError::OutOfMemory) => *control_flow = ControlFlow::Exit,

@ -674,7 +674,7 @@ fn main() {
state.update();
match state.render() {
Ok(_) => {}
// Recreate the swap_chain if lost
// Reconfigure the surface if lost
Err(wgpu::SurfaceError::Lost) => state.resize(state.size),
// The system is out of memory, we should probably quit
Err(wgpu::SurfaceError::OutOfMemory) => *control_flow = ControlFlow::Exit,

@ -385,12 +385,9 @@ impl State {
)
.unwrap();
// let vs_module = device.create_shader_module(&wgpu::include_spirv!("shader.vert.spv"));
// let fs_module = device.create_shader_module(&wgpu::include_spirv!("shader.frag.spv"));
let shader = device.create_shader_module(&wgpu::ShaderModuleDescriptor {
label: Some("shader.wgsl"),
source: wgpu::ShaderSource::Wgsl(include_str!("shader.wgsl").into()),
flags: wgpu::ShaderFlags::VALIDATION,
});
let depth_texture =
@ -593,7 +590,7 @@ fn main() {
state.update();
match state.render() {
Ok(_) => {}
// Recreate the swap_chain if lost
// Reconfigure the surface if lost
Err(wgpu::SurfaceError::Lost) => state.resize(state.size),
// The system is out of memory, we should probably quit
Err(wgpu::SurfaceError::OutOfMemory) => *control_flow = ControlFlow::Exit,

@ -83,7 +83,7 @@ impl Texture {
label: Option<&str>,
) -> Result<Self> {
let dimensions = img.dimensions();
let rgba = img.to_rgba();
let rgba = img.to_rgba8();
let size = wgpu::Extent3d {
width: dimensions.0,

@ -728,7 +728,7 @@ fn main() {
state.update();
match state.render() {
Ok(_) => {}
// Recreate the swap_chain if lost
// Reconfigure the surface if lost
Err(wgpu::SurfaceError::Lost) => state.resize(state.size),
// The system is out of memory, we should probably quit
Err(wgpu::SurfaceError::OutOfMemory) => *control_flow = ControlFlow::Exit,

@ -83,7 +83,7 @@ impl Texture {
label: Option<&str>,
) -> Result<Self> {
let dimensions = img.dimensions();
let rgba = img.to_rgba();
let rgba = img.to_rgba8();
let size = wgpu::Extent3d {
width: dimensions.0,

@ -781,7 +781,7 @@ fn main() {
state.update();
match state.render() {
Ok(_) => {}
// Recreate the swap_chain if lost
// Reconfigure the surface if lost
Err(wgpu::SurfaceError::Lost) => state.resize(state.size),
// The system is out of memory, we should probably quit
Err(wgpu::SurfaceError::OutOfMemory) => *control_flow = ControlFlow::Exit,

@ -86,7 +86,7 @@ impl Texture {
is_normal_map: bool,
) -> Result<Self> {
let dimensions = img.dimensions();
let rgba = img.to_rgba();
let rgba = img.to_rgba8();
let size = wgpu::Extent3d {
width: dimensions.0,

@ -698,7 +698,7 @@ fn main() {
state.update(dt);
match state.render() {
Ok(_) => {}
// Recreate the swap_chain if lost
// Reconfigure the surface if lost
Err(wgpu::SurfaceError::Lost) => state.resize(state.size),
// The system is out of memory, we should probably quit
Err(wgpu::SurfaceError::OutOfMemory) => *control_flow = ControlFlow::Exit,

@ -86,7 +86,7 @@ impl Texture {
is_normal_map: bool,
) -> Result<Self> {
let dimensions = img.dimensions();
let rgba = img.to_rgba();
let rgba = img.to_rgba8();
let size = wgpu::Extent3d {
width: dimensions.0,

@ -691,7 +691,7 @@ fn main() {
state.update(dt);
match state.render() {
Ok(_) => {}
// Recreate the swap_chain if lost
// Reconfigure the surface if lost
Err(wgpu::SurfaceError::Lost) => state.resize(state.size),
// The system is out of memory, we should probably quit
Err(wgpu::SurfaceError::OutOfMemory) => *control_flow = ControlFlow::Exit,

@ -86,7 +86,7 @@ impl Texture {
is_normal_map: bool,
) -> Result<Self> {
let dimensions = img.dimensions();
let rgba = img.to_rgba();
let rgba = img.to_rgba8();
let size = wgpu::Extent3d {
width: dimensions.0,

@ -86,7 +86,7 @@ impl Texture {
is_normal_map: bool,
) -> Result<Self> {
let dimensions = img.dimensions();
let rgba = img.to_rgba();
let rgba = img.to_rgba8();
let size = wgpu::Extent3d {
width: dimensions.0,

@ -70,7 +70,7 @@ impl<'a> Texture<'a> {
_label: Option<&str>,
is_normal_map: bool,
) -> Result<Self> {
let rgba = img.to_rgba();
let rgba = img.to_rgba8();
let dimensions = img.dimensions();
let size = wgpu::Extent3d {

@ -66,7 +66,7 @@ impl Texture {
```
1. We need the DEPTH_FORMAT for when we create the depth stage of the `render_pipeline` and creating the depth texture itself.
2. Our depth texture needs to be the same size as our screen if we want things to render correctly. We can use our `config` to make sure that our depth texture is the same size as our swap chain images.
2. Our depth texture needs to be the same size as our screen if we want things to render correctly. We can use our `config` to make sure that our depth texture is the same size as our surface textures.
3. Since we are rendering to this texture, we need to add the `RENDER_ATTACHMENT` flag to it.
4. We technically don't *need* a sampler for a depth texture, but our `Texture` struct requires it, and we need one if we ever want to sample it.
5. If we do decide to render our depth texture, we need to use `CompareFunction::LessEqual`. This is due to how the `samplerShadow` and `sampler2DShadow()` interacts with the `texture()` function in GLSL.
@ -135,7 +135,7 @@ fn resize(&mut self, new_size: winit::dpi::PhysicalSize<u32>) {
}
```
Make sure you update the `depth_texture` *after* you update `config`. If you don't, your program will crash as the `depth_texture` will be a different size than the `swap_chain` texture.
Make sure you update the `depth_texture` *after* you update `config`. If you don't, your program will crash as the `depth_texture` will be a different size than the `surface` texture.
The last change we need to make is in the `render()` function. We've created the `depth_texture`, but we're not currently using it. We use it by attaching it to the `depth_stencil_attachment` of a render pass.

@ -126,7 +126,15 @@ glob = "0.3"
## Loading models with TOBJ
We're going to use the [tobj](https://docs.rs/tobj/0.1.12/tobj/) library to load our model. Before we can load our model though, we need somewhere to put it.
We're going to use the [tobj](https://docs.rs/tobj/3.0/tobj/) library to load our model. Let's add it to our `Cargo.toml`.
```toml
[dependencies]
# other dependencies...
tobj = "3.0"
```
Before we can load our model though, we need somewhere to put it.
```rust
// model.rs

Loading…
Cancel
Save