Skip to content

Commit

Permalink
Simplify vertex attributes (#228)
Browse files Browse the repository at this point in the history
- 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.
  • Loading branch information
parasyte authored Nov 20, 2021
1 parent fa03a61 commit c2454b0
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 38 deletions.
3 changes: 1 addition & 2 deletions examples/custom-shader/shaders/noise.wgsl
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,9 @@ struct VertexOutput {
[[stage(vertex)]]
fn vs_main(
[[location(0)]] position: vec2<f32>,
[[location(1)]] tex_coord: vec2<f32>,
) -> 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);
return out;
}
Expand Down
25 changes: 9 additions & 16 deletions examples/custom-shader/src/renderers.rs
Original file line number Diff line number Diff line change
Expand Up @@ -37,12 +37,12 @@ impl NoiseRenderer {
});

// 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
// See: https://github.com/parasyte/pixels/issues/180
[[-1.0, -1.0], [0.0, 0.0]],
[[3.0, -1.0], [2.0, 0.0]],
[[-1.0, 3.0], [0.0, 2.0]],
[-1.0, -1.0],
[3.0, -1.0],
[-1.0, 3.0],
];
let vertex_data_slice = bytemuck::cast_slice(&vertex_data);
let vertex_buffer = device.create_buffer_init(&wgpu::util::BufferInitDescriptor {
Expand All @@ -53,18 +53,11 @@ impl NoiseRenderer {
let vertex_buffer_layout = wgpu::VertexBufferLayout {
array_stride: (vertex_data_slice.len() / vertex_data.len()) as wgpu::BufferAddress,
step_mode: wgpu::VertexStepMode::Vertex,
attributes: &[
wgpu::VertexAttribute {
format: wgpu::VertexFormat::Float32x2,
offset: 0,
shader_location: 0,
},
wgpu::VertexAttribute {
format: wgpu::VertexFormat::Float32x2,
offset: 4 * 2,
shader_location: 1,
},
],
attributes: &[wgpu::VertexAttribute {
format: wgpu::VertexFormat::Float32x2,
offset: 0,
shader_location: 0,
}],
};

// Create uniform buffer
Expand Down
3 changes: 1 addition & 2 deletions shaders/scale.wgsl
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,9 @@ struct VertexOutput {
[[stage(vertex)]]
fn vs_main(
[[location(0)]] position: vec2<f32>,
[[location(1)]] tex_coord: vec2<f32>,
) -> 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);
return out;
}
Expand Down
29 changes: 11 additions & 18 deletions src/renderers.rs
Original file line number Diff line number Diff line change
Expand Up @@ -42,12 +42,12 @@ impl ScalingRenderer {
});

// 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
// See: https://github.com/parasyte/pixels/issues/180
[[-1.0, -1.0], [0.0, 0.0]],
[[3.0, -1.0], [2.0, 0.0]],
[[-1.0, 3.0], [0.0, 2.0]],
[-1.0, -1.0],
[3.0, -1.0],
[-1.0, 3.0],
];
let vertex_data_slice = bytemuck::cast_slice(&vertex_data);
let vertex_buffer = device.create_buffer_init(&wgpu::util::BufferInitDescriptor {
Expand All @@ -58,18 +58,11 @@ impl ScalingRenderer {
let vertex_buffer_layout = wgpu::VertexBufferLayout {
array_stride: (vertex_data_slice.len() / vertex_data.len()) as wgpu::BufferAddress,
step_mode: wgpu::VertexStepMode::Vertex,
attributes: &[
wgpu::VertexAttribute {
format: wgpu::VertexFormat::Float32x2,
offset: 0,
shader_location: 0,
},
wgpu::VertexAttribute {
format: wgpu::VertexFormat::Float32x2,
offset: 4 * 2,
shader_location: 1,
},
],
attributes: &[wgpu::VertexAttribute {
format: wgpu::VertexFormat::Float32x2,
offset: 0,
shader_location: 0,
}],
};

// Create uniform buffer
Expand Down Expand Up @@ -255,9 +248,9 @@ impl ScalingMatrix {
#[rustfmt::skip]
let transform: [f32; 16] = [
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,
tx, ty, 0.0, 1.0,
tx, ty, 0.0, 1.0,
];

// Create a clipping rectangle
Expand Down

0 comments on commit c2454b0

Please sign in to comment.