I want to understand how to work with compute shaders. I didn't find any details on the Internet. What is workingGroup?
layout (local_size_x = 1, local_size_y = 1, local_size_z = 1) in;
what it meaning?
vkCmdDispatch(cmdBuffer, 1, 1, 1);
Should the values in the Shader and in the function be the same?
For understanding these basic concepts of compute shaders, material for OpenCL, OpenGL, Metal, D3D, and CUDA compute would also be relevant: they all use a similar hierarchical grid subdivision of work.
The hierarchy, from finest to coarsest, in Vulkan terms is: invocation (aka thread) > subgroup > local workgroup > global workgroup (aka dispatch). Subgroups are a more advanced topic; you can ignore them for now as they're mostly implicit. Just to be confusing, people often just say "workgroup" when they mean "local workgroup".
The layout(local_size)
declaration in your shader defines the dimensions of a local workgroup in terms of individual invocations. The parameters to vkCmdDispatch
give the dimensions of the global workgroup, in terms of local workgroups.
So if you call vkCmdDispatch(cmdbuf, M, N, P)
and the compute shader in the current pipeline declared layout (local_size_x=X, local_size_y=Y, local_size_z=Z)
, then Vulkan will run MxNxP local workgroups, each of which consists of XxYxZ invocations of your shader.
Within each invocation you can find out where it is within the local and global grids with the GLSL built-in input variables gl_NumWorkGroups
, gl_WorkGroupID
, gl_LocalInvocationID
, gl_GlobalInvocationID
, and gl_LocalInvocationIndex
.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With