“Black border problem” in Compute Shaders

Ein Bild

“Black border problem” in Compute Shaders

Have you ever seen black borders in the resulting images of your compute shaders? Here is the solution!

The following image demonstrates what the problem is. Black pixels at the borders of images created oder altered with a compute shader.

The problem is caused by the division of two integer type numbers. Usually the compute shaders are dispatched by using the dimensions of the image divided by the local_size values of the compute shader (example below).

layout (local_size_x = 16, local_size_y = 16) in;
Dispatch(cmd, image.Width / 16, image.Height / 16, 1);

This calculation is usually a division with a remainder as these are integer values. So if there is a remainder the group counts do not use all pixels of the image for the calculations. The solution is just to add 1 to each of the dimensions.

Dispatch(cmd, image.Width / 16 + 1, image.Height / 16 + 1, 1);
Ein Bild
Compute ShaderVulkan