KhronosGroup / MoltenVK

MoltenVK is a Vulkan Portability implementation. It layers a subset of the high-performance, industry-standard Vulkan graphics and compute API over Apple's Metal graphics framework, enabling Vulkan applications to run on macOS, iOS and tvOS.
Apache License 2.0
4.81k stars 424 forks source link

Wrong argument buffers generated for a compute shader #2106

Open rokuz opened 10 months ago

rokuz commented 10 months ago

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 commented 4 months ago

@billhollings Any progress here?

billhollings commented 3 months ago

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.