Skip to content

Improper vs Proper Vertex Allignment when Writing to Vertex Buffer From Compute Shader #351

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 10 commits into from
3 changes: 3 additions & 0 deletions src/pages/samples/[slug].tsx
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,9 @@ export const pages: PageComponentType = {
),
resizeCanvas: dynamic(() => import('../../sample/resizeCanvas/main')),
rotatingCube: dynamic(() => import('../../sample/rotatingCube/main')),
vertComputeAllignment: dynamic(
() => import('../../sample/vertComputeAllignment/main')
),
twoCubes: dynamic(() => import('../../sample/twoCubes/main')),
texturedCube: dynamic(() => import('../../sample/texturedCube/main')),
instancedCube: dynamic(() => import('../../sample/instancedCube/main')),
Expand Down
30 changes: 30 additions & 0 deletions src/sample/vertComputeAllignment/alligned.wgsl
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
struct VertexUniforms {
count: u32,
position_stride: u32,
scale: f32,
padding: f32,
}

@group(0) @binding(0) var<uniform> vertex_uniforms: VertexUniforms;
@group(0) @binding(1) var<storage, read_write> current_positions: array<f32>;
@group(1) @binding(1) var<storage, read> correct_positions: array<f32>;

@compute @workgroup_size(64)
fn passThrough(@builtin(global_invocation_id) global_id : vec3<u32>) {
let index = global_id.x;
// Return early if the vertex index is higher than half the number of vertices
if (index >= vertex_uniforms.count) {
return;
}
let position_offset = index * vertex_uniforms.position_stride;
// Access current position
var current_position = vec3<f32>(
correct_positions[position_offset],
correct_positions[position_offset + 1],
correct_positions[position_offset + 2]
);

current_positions[position_offset] = current_position.x;
current_positions[position_offset + 1] = current_position.y;
current_positions[position_offset + 2] = current_position.z;
}
23 changes: 23 additions & 0 deletions src/sample/vertComputeAllignment/basicMod.vert.wgsl
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
struct Uniforms {
modelViewProjectionMatrix : mat4x4<f32>,
}
@binding(0) @group(0) var<uniform> uniforms : Uniforms;

struct VertexOutput {
@builtin(position) Position : vec4<f32>,
@location(0) fragUV : vec2<f32>,
@location(1) fragPosition: vec4<f32>,
}

@vertex
fn main(
// Note: Render pass is able to properly access vec3s, though compute shader cannot
@location(0) position : vec3<f32>,
@location(1) uv : vec2<f32>
) -> VertexOutput {
var output : VertexOutput;
output.Position = uniforms.modelViewProjectionMatrix * vec4<f32>(position, 1.0);
output.fragUV = uv;
output.fragPosition = 0.5 * (vec4<f32>(position, 1.0) + vec4(1.0, 1.0, 1.0, 1.0));
return output;
}
Loading