I'm learning how to use a compute shader in Vulkan, so I'm just using this as the shader:
#version 450 core
layout(std140, binding = 0) readonly buffer InputSSBO {
int InputBuffer[256];
};
layout(std140, binding = 1) buffer OutputSSBO {
int OutputBuffer[256];
};
layout(local_size_x = 256, local_size_y = 1, local_size_z = 1) in;
void main()
{
uint index = gl_GlobalInvocationID.x;
OutputBuffer[index] = int(index);
}
When I dispatch the shader, (using vkCmdDispatch(commandBuffer, 1, 1, 1)
, although other groupCountX values don't make a difference)
the output buffer reads the numbers
0, 0, 0, 0, 1, 0, 0, 0, 2, ... 62, 0, 0, 0, 63, 0, 0, 0
It seems like the buffer size has been divided by 4 somewhere, so I tried multiplying all the buffer sizes on the C++ side by 4, without changing the compute shader. The output was the same as above, but all the way to 255.
What could cause the buffer to be written to like this?
This problem was due to alignment issues, as std140
uses a byte alignment of 16 bytes, and integers are 4 bytes, creating a padding of three bytes, hence the three 0s.
Changing std140
to std430
fixed the problem as std430
is 4 byte aligned.