Search code examples
vulkancompute-shader

Vulkan Compute Shader: gl_GlobalInvocationID unexpected result


I'm new in Vulkan compute shaders, sorry for the silly question. Here is my shader (I'm just trying to debug, this is not the whole logic):

#version 450

layout(local_size_x = 32) in;

struct vertex {
    vec3 pos;
    vec3 color;
};

layout(std430, binding = 0) buffer output_buffer{ 
    vertex bezier_curve[]; 
};

layout(std430, push_constant) uniform pc { 
    int points_size; 
} push; 

void main() {
    const uint id = gl_GlobalInvocationID.x + 3; // first 3 elements are the control points
    if (id > push.points_size) return;

    bezier_curve[id].color = vec3(id, id, id);
}

The dispatch:

command_buffers.front().dispatch(1, 1, 1); // since the amount of points is equal to 13 the only workgroup would be enough (just for test)

The output:

auto out_buffer_ptr = reinterpret_cast<vertex*>(logicalDevice.mapMemory(buffer_memory, 0, buffer_size));

for (int i = 0; i < vertices.size(); ++i)
    std::cout << (out_buffer_ptr + i)->color.x << ' ' << (out_buffer_ptr + i)->color.y << ' ' << (out_buffer_ptr + i)->color.z << '\n';

logicalDevice.unmapMemory(buffer_memory);

So I expect to see few vectors, each filled with the only number (gl_GlobalInvocationID.x), i.e.

4 4 4
6 6 6
5 5 5

But here's what I have got:

1 0 0
0 1 0
0 0 1
0 0 0
0 3 3
0 0 0
0 0 0
5 5 0
0 6 6
0 0 0
0 0 0
8 8 0
0 9 9

Those three

1 0 0
0 1 0
0 0 1 

are fine since I hardcoded control points and colors for them myself, that's the gl_GlobalInvocationID.x + 3; case. But where have the others come from?


Solution

  • Guys I forgot about alignment. Can't believe it happened to me... sorry for wasting your time!