Merge pull request #76 from jprudil/vertex-use-macro

Use vertex_attr_array macro
pull/88/head
sotrh 4 years ago committed by GitHub
commit b47081d70c
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

@ -123,28 +123,16 @@ For you visually learners, our vertex buffer looks like this.
![A figure of the VertexBufferDescriptor](./vb_desc.png)
Let's create a static method on `Vertex` that returns this descriptor.
Let's create a static method on `Vertex` that returns this descriptor. Thankfully, wgpu provides us with a convenient macro for doing this! `vertex_attr_array!` expands into an array of descriptors with automatically calculated offsets.
```rust
// main.rs
impl Vertex {
fn desc<'a>() -> wgpu::VertexBufferDescriptor<'a> {
use std::mem;
wgpu::VertexBufferDescriptor {
stride: mem::size_of::<Vertex>() as wgpu::BufferAddress,
stride: std::mem::size_of::<Vertex>() as wgpu::BufferAddress,
step_mode: wgpu::InputStepMode::Vertex,
attributes: &[
wgpu::VertexAttributeDescriptor {
offset: 0,
shader_location: 0,
format: wgpu::VertexFormat::Float3,
},
wgpu::VertexAttributeDescriptor {
offset: mem::size_of::<[f32; 3]>() as wgpu::BufferAddress,
shader_location: 1,
format: wgpu::VertexFormat::Float3,
},
]
attributes: &wgpu::vertex_attr_array![0 => Float3, 1 => Float3],
}
}
}

@ -247,7 +247,7 @@ let render_pipeline_layout = device.create_pipeline_layout(&wgpu::PipelineLayout
```
## A change to the VERTICES
There's a few things we need to change about our `Vertex` definition. Up to now we've been using a `color` attribute to dictate the color of our mesh. Now that we're using a texture we want to replace our `color` with `tex_coords`.
There's a few things we need to change about our `Vertex` definition. Up to now we've been using a `color` attribute to dictate the color of our mesh. Now that we're using a texture we want to replace our `color` with `tex_coords`, which is only two floats instead of three.
```rust
#[repr(C)]
@ -263,25 +263,10 @@ We need to reflect these changes in the `VertexBufferDescriptor`.
```rust
impl Vertex {
fn desc<'a>() -> wgpu::VertexBufferDescriptor<'a> {
use std::mem;
wgpu::VertexBufferDescriptor {
stride: mem::size_of::<Vertex>() as wgpu::BufferAddress,
stride: std::mem::size_of::<Vertex>() as wgpu::BufferAddress,
step_mode: wgpu::InputStepMode::Vertex,
attributes: &[
wgpu::VertexAttributeDescriptor {
offset: 0,
shader_location: 0,
format: wgpu::VertexFormat::Float3,
},
wgpu::VertexAttributeDescriptor {
offset: mem::size_of::<[f32; 3]>() as wgpu::BufferAddress,
shader_location: 1,
// We only need to change this to reflect that tex_coords
// is only 2 floats and not 3. It's in the same position
// as color was, so nothing else needs to change
format: wgpu::VertexFormat::Float2,
},
]
attributes: &wgpu::vertex_attr_array![0 => Float3, 1 => Float2],
}
}
}

@ -34,27 +34,14 @@ Let's define our `VertexBufferDescriptor`.
```rust
impl Vertex for ModelVertex {
fn desc<'a>() -> wgpu::VertexBufferDescriptor<'a> {
use std::mem;
wgpu::VertexBufferDescriptor {
stride: mem::size_of::<ModelVertex>() as wgpu::BufferAddress,
stride: std::mem::size_of::<ModelVertex>() as wgpu::BufferAddress,
step_mode: wgpu::InputStepMode::Vertex,
attributes: &[
wgpu::VertexAttributeDescriptor {
offset: 0,
shader_location: 0,
format: wgpu::VertexFormat::Float3,
},
wgpu::VertexAttributeDescriptor {
offset: mem::size_of::<[f32; 3]>() as wgpu::BufferAddress,
shader_location: 1,
format: wgpu::VertexFormat::Float2,
},
wgpu::VertexAttributeDescriptor {
offset: mem::size_of::<[f32; 5]>() as wgpu::BufferAddress,
shader_location: 2,
format: wgpu::VertexFormat::Float3,
},
]
attributes: &wgpu::vertex_attr_array![
0 => Float3,
1 => Float2,
2 => Float3
],
}
}
}

Loading…
Cancel
Save