MoltenVK icon indicating copy to clipboard operation
MoltenVK copied to clipboard

Wrong argument buffers generated for a compute shader

Open rokuz opened this issue 1 year ago • 2 comments

The following compute shader

layout(local_size_x = 16, local_size_y = 16, local_size_z = 1) in;

layout (set = 0, binding = 2, rgba8) uniform readonly  image2D kTextures2Din[];
layout (set = 1, binding = 2, rgba8) uniform writeonly image2D kTextures2Dout[];

layout(push_constant) uniform constants {
   uint tex;
   uint width;
   uint height;
} pc;

void main() {
   ivec2 pos = ivec2(gl_GlobalInvocationID.xy);

   if (pos.x < pc.width && pos.y < pc.height) {
     vec4 pixel = imageLoad(kTextures2Din[pc.tex], pos);
     float luminance = dot(pixel, vec4(0.299, 0.587, 0.114, 0.0)); // https://www.w3.org/TR/AERT/#color-contrast
     imageStore(kTextures2Dout[pc.tex], pos, vec4(vec3(luminance), 1.0));
   }
}

leads to the generation of the following argument buffers:

struct spvDescriptorSetBuffer0
{
    array<texture2d<float>, 256> _m0_pad [[id(0)]];
    array<texture2d<float>, 256> _m256_pad [[id(256)]];
    array<texture2d<float>, 256> kTextures2Din [[id(272)]];
};

struct spvDescriptorSetBuffer1
{
    array<texture2d<float>, 256> _m0_pad [[id(0)]];
    array<texture2d<float>, 256> _m256_pad [[id(256)]];
    array<texture2d<float, access::write>, 256> kTextures2Dout [[id(272)]];
};

I believe that the first two fields for missing bindings 0 and 1 (I'm not sure why the size's 256, though). However, the id's offset for the third field is incorrect (it should be 512) and this shader can't be compiled.

If we change bindings like that

layout (set = 0, binding = 0, rgba8) uniform readonly  image2D kTextures2Din[];
layout (set = 1, binding = 0, rgba8) uniform writeonly image2D kTextures2Dout[];

argument buffers are generating correctly.

rokuz avatar Dec 28 '23 08:12 rokuz

@billhollings Any progress here?

rokuz avatar Jul 05 '24 18:07 rokuz

Try it with the latest version of MoltenVK from they repo. PR #2260 add a number of improvements to the use of Metal argument buffers in MoltenVK.

billhollings avatar Jul 06 '24 00:07 billhollings