Skip to content
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

WebGPU uniforms and alignment (?) #16

Open
tommhuth opened this issue Dec 26, 2024 · 1 comment
Open

WebGPU uniforms and alignment (?) #16

tommhuth opened this issue Dec 26, 2024 · 1 comment
Labels
bug Something isn't working

Comments

@tommhuth
Copy link

I'm using the webgpu cube example and added a few more uniforms to test stuff out (sorry, I couldn't get this to work on Codesandbox):

 struct Uniforms {
    projectionMatrix: mat4x4f,
    modelViewMatrix: mat4x4f,
    normalMatrix: mat4x4f, 
    color: vec3f,
    time: f32, 
    darkness: f32,
};

@group(0) @binding(0) var<uniform> uniforms: Uniforms;

struct VertexIn {
    @builtin(vertex_index) vertexIndex: u32,
    @location(0) position: vec3<f32>,
    @location(1) normal: vec3<f32>,
};

struct VertexOut {
    @builtin(position) position: vec4<f32>,
    @location(0) color: vec3<f32>,
    @location(1) normal: vec3<f32>,
};

@vertex
fn main(input: VertexIn) -> VertexOut {
    var out: VertexOut;
    var transformed = input.position + vec3f(0, sin(uniforms.time * 2), 0);

    out.position = uniforms.projectionMatrix * uniforms.modelViewMatrix * vec4(transformed, 1.0); 
    out.color = uniforms.color * uniforms.darkness;
    out.normal = (uniforms.normalMatrix * vec4f(input.normal, 0.0)).xyz;

    return out;
}
const material = new Material({
    uniforms: {
        color: [.2, .4, .4],
        time: .5,
        darkness: .5,
    },
    vertex,
    fragment,
})

When I do that the time uniform does not update, and darkness seems to pick out the data from time instead. also: the order of the struct Uniform members change the behaviour as this order does work:

 struct Uniforms {
    projectionMatrix: mat4x4f,
    modelViewMatrix: mat4x4f,
    normalMatrix: mat4x4f, 
    time: f32, 
    darkness: f32,
    color: vec3f,
};

or even do color: vec4f it works as expected again. I'm am very new to WebGPU, so I might be missing something obvious here, but from what I can tell there is no need to do manual layout/padding (?) for the struct definition? Also four seems to be handling things on its own with the std140 helper?

@tommhuth tommhuth changed the title WebGPU uniforms and layout (?) WebGPU uniforms and alignment (?) Dec 26, 2024
@CodyJasonBennett CodyJasonBennett added the bug Something isn't working label Jan 15, 2025
@CodyJasonBennett
Copy link
Owner

I'm not able to observe this issue. This is the expected memory layout for the shader (offset calculator):

{DB7FB0AF-F222-4D18-8B84-35B93E8A397B}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

2 participants