Array<vec3<f32>>

var createScene = function () {
    var scene = new BABYLON.Scene(engine);
    var camera = new BABYLON.ArcRotateCamera("Camera", Math.PI / 2, Math.PI / 2, 2, BABYLON.Vector3.Zero(), scene);
    camera.attachControl(canvas, true);

    const cs = new BABYLON.ComputeShader("cs", engine,
        { computeSource:
            `
                @group(0) @binding(0) var<storage,read_write> A : array<vec3<f32>>;
                @group(1) @binding(0) var<storage,read_write> B : array<vec3<f32>>;
        
                @compute @workgroup_size(3)
                fn main(@builtin(global_invocation_id) global_id : vec3<u32>) {
                    var x : u32 = global_id.x;
                    B[x] = A[x];
                }
            `
        },
        { bindingsMapping:
            {
                "A": { group: 0, binding: 0 },
                "B": { group: 1, binding: 0 }
            }
        }
    );
    
    const A = new BABYLON.StorageBuffer(engine, 9 * 4);
    const B = new BABYLON.StorageBuffer(engine, 9 * 4);

    A.update(new Float32Array([0,1,2,3,4,5,6,7,8]));

    cs.setStorageBuffer("A", A);
    cs.setStorageBuffer("B", B);
 
    cs.dispatch(1);
    B.read().then((res) => {
        console.log(new Float32Array(res.buffer));
    });

    return scene;
};

EXPECTATION : [0, 1, 2, 3, 4, 5, 6, 7, 8]
CONSOLE : [0, 1, 2, 0, 4, 5, 6, 0, 0]

I suspect vec3 in the 8th and 9th lines is a bug.
When switched to vec2 or vec4, it works as expected.

vec2
EXPECTATION & CONSOLE : [0, 1, 2, 3, 4, 5, 0, 0, 0]
// @workgroup_size is still 3.
// [0, 1], [2, 3], [4, 5]

vec4
EXPECTATION & CONSOLE : [0, 1, 2, 3, 4, 5, 6, 7, 0]
// 8 Missing because alone cannot construct a vector.
// [0, 1, 2, 3], [4, 5, 6, 7], [8…

Welcome aboard!

There are alignment issues that apply, so an array of vec3<f32> will actually use 4 f32 for each element, the 4th being unused.

1 Like

Is it a matter of Babylon or of webgpu?
Is it intended or a bug?
Are you solving it or are you going to solve it?

No, it’s expected, it’s how WebGPU works. You can declare your array as array<f32> if you want to avoid alignment problems.

3 Likes

Thank you for your kindness