Simplify vertex attributes (#228)

- Vertex UVs can be computed from the positions, which saves a small amount of code and a small amount of bandwidth (both inconsequential).
- This is mostly for readability.
- Fixes vertically-flipped pixel buffer in the `custom-shader` example.
This commit is contained in:
Jay Oster 2021-11-20 14:06:23 -08:00 committed by GitHub
parent fa03a61fa3
commit c2454b01ab
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
4 changed files with 22 additions and 38 deletions

View file

@ -8,10 +8,9 @@ struct VertexOutput {
[[stage(vertex)]] [[stage(vertex)]]
fn vs_main( fn vs_main(
[[location(0)]] position: vec2<f32>, [[location(0)]] position: vec2<f32>,
[[location(1)]] tex_coord: vec2<f32>,
) -> VertexOutput { ) -> VertexOutput {
var out: VertexOutput; var out: VertexOutput;
out.tex_coord = tex_coord; out.tex_coord = position * vec2<f32>(0.5, -0.5) + 0.5;
out.position = vec4<f32>(position, 0.0, 1.0); out.position = vec4<f32>(position, 0.0, 1.0);
return out; return out;
} }

View file

@ -37,12 +37,12 @@ impl NoiseRenderer {
}); });
// Create vertex buffer; array-of-array of position and texture coordinates // Create vertex buffer; array-of-array of position and texture coordinates
let vertex_data: [[[f32; 2]; 2]; 3] = [ let vertex_data: [[f32; 2]; 3] = [
// One full-screen triangle // One full-screen triangle
// See: https://github.com/parasyte/pixels/issues/180 // See: https://github.com/parasyte/pixels/issues/180
[[-1.0, -1.0], [0.0, 0.0]], [-1.0, -1.0],
[[3.0, -1.0], [2.0, 0.0]], [3.0, -1.0],
[[-1.0, 3.0], [0.0, 2.0]], [-1.0, 3.0],
]; ];
let vertex_data_slice = bytemuck::cast_slice(&vertex_data); let vertex_data_slice = bytemuck::cast_slice(&vertex_data);
let vertex_buffer = device.create_buffer_init(&wgpu::util::BufferInitDescriptor { let vertex_buffer = device.create_buffer_init(&wgpu::util::BufferInitDescriptor {
@ -53,18 +53,11 @@ impl NoiseRenderer {
let vertex_buffer_layout = wgpu::VertexBufferLayout { let vertex_buffer_layout = wgpu::VertexBufferLayout {
array_stride: (vertex_data_slice.len() / vertex_data.len()) as wgpu::BufferAddress, array_stride: (vertex_data_slice.len() / vertex_data.len()) as wgpu::BufferAddress,
step_mode: wgpu::VertexStepMode::Vertex, step_mode: wgpu::VertexStepMode::Vertex,
attributes: &[ attributes: &[wgpu::VertexAttribute {
wgpu::VertexAttribute { format: wgpu::VertexFormat::Float32x2,
format: wgpu::VertexFormat::Float32x2, offset: 0,
offset: 0, shader_location: 0,
shader_location: 0, }],
},
wgpu::VertexAttribute {
format: wgpu::VertexFormat::Float32x2,
offset: 4 * 2,
shader_location: 1,
},
],
}; };
// Create uniform buffer // Create uniform buffer

View file

@ -13,10 +13,9 @@ struct VertexOutput {
[[stage(vertex)]] [[stage(vertex)]]
fn vs_main( fn vs_main(
[[location(0)]] position: vec2<f32>, [[location(0)]] position: vec2<f32>,
[[location(1)]] tex_coord: vec2<f32>,
) -> VertexOutput { ) -> VertexOutput {
var out: VertexOutput; var out: VertexOutput;
out.tex_coord = tex_coord; out.tex_coord = position * vec2<f32>(0.5, -0.5) + 0.5;
out.position = r_locals.transform * vec4<f32>(position, 0.0, 1.0); out.position = r_locals.transform * vec4<f32>(position, 0.0, 1.0);
return out; return out;
} }

View file

@ -42,12 +42,12 @@ impl ScalingRenderer {
}); });
// Create vertex buffer; array-of-array of position and texture coordinates // Create vertex buffer; array-of-array of position and texture coordinates
let vertex_data: [[[f32; 2]; 2]; 3] = [ let vertex_data: [[f32; 2]; 3] = [
// One full-screen triangle // One full-screen triangle
// See: https://github.com/parasyte/pixels/issues/180 // See: https://github.com/parasyte/pixels/issues/180
[[-1.0, -1.0], [0.0, 0.0]], [-1.0, -1.0],
[[3.0, -1.0], [2.0, 0.0]], [3.0, -1.0],
[[-1.0, 3.0], [0.0, 2.0]], [-1.0, 3.0],
]; ];
let vertex_data_slice = bytemuck::cast_slice(&vertex_data); let vertex_data_slice = bytemuck::cast_slice(&vertex_data);
let vertex_buffer = device.create_buffer_init(&wgpu::util::BufferInitDescriptor { let vertex_buffer = device.create_buffer_init(&wgpu::util::BufferInitDescriptor {
@ -58,18 +58,11 @@ impl ScalingRenderer {
let vertex_buffer_layout = wgpu::VertexBufferLayout { let vertex_buffer_layout = wgpu::VertexBufferLayout {
array_stride: (vertex_data_slice.len() / vertex_data.len()) as wgpu::BufferAddress, array_stride: (vertex_data_slice.len() / vertex_data.len()) as wgpu::BufferAddress,
step_mode: wgpu::VertexStepMode::Vertex, step_mode: wgpu::VertexStepMode::Vertex,
attributes: &[ attributes: &[wgpu::VertexAttribute {
wgpu::VertexAttribute { format: wgpu::VertexFormat::Float32x2,
format: wgpu::VertexFormat::Float32x2, offset: 0,
offset: 0, shader_location: 0,
shader_location: 0, }],
},
wgpu::VertexAttribute {
format: wgpu::VertexFormat::Float32x2,
offset: 4 * 2,
shader_location: 1,
},
],
}; };
// Create uniform buffer // Create uniform buffer
@ -255,9 +248,9 @@ impl ScalingMatrix {
#[rustfmt::skip] #[rustfmt::skip]
let transform: [f32; 16] = [ let transform: [f32; 16] = [
sw, 0.0, 0.0, 0.0, sw, 0.0, 0.0, 0.0,
0.0, -sh, 0.0, 0.0, 0.0, sh, 0.0, 0.0,
0.0, 0.0, 1.0, 0.0, 0.0, 0.0, 1.0, 0.0,
tx, ty, 0.0, 1.0, tx, ty, 0.0, 1.0,
]; ];
// Create a clipping rectangle // Create a clipping rectangle