How to use compute shaders to compute enclosed regions within a texture

As shown in the figure below, a basic line drawing function is realized through the renderFeature of URP.
And when there is an enclosed area, turn it white.

Currently I am getting data through tex.ReadPixels and tex.GetPixels. Use the CPU to traverse each pixel through BFS to determine whether the closed area exists and use tex.SetPixel to cover the pixel.

This is too slow. Since I have almost no experience with compute shaders, I was wondering if it is possible to achieve the same functionality with compute shaders? If possible, can someone give me some keywords for me to search?
8689782--1172202--upload_2022-12-28_10-47-5.gif

search for “compute shader flood fill” or “gpu compute flood fill”

2 Likes

Thank you so much, I think I finally found the right direction to solve the problem.

1 Like

It works! And much faster than the CPU! But I still want to ask greedily, assuming that the texture has a resolution of 1024*1024, it is necessary to dispatch 1024 times to calculate the shader. Is there any way to optimize it?

for (int i = 0; i < splatmapWidth; i++)
    {
        shader.Dispatch(kernel, splatmapWidth / 8, splatmapWidth / 8, 1);
    }

8691624--1172727--upload_2022-12-29_9-46-36.gif

Mind sharing your implementation or the resources you found useful?

Of course, this is a little summary written in Chinese, and there are all the codes in it.
https://www.kuanmi.top/2022/12/26/draw/

1 Like