- Home /
Compute shader loop in editor
I'm working on a tool for the editor in which you can take a sprite and it blurs it for you. I'm using a compute shader with buffer approach to blur it but blur requires multiple passes. After using Shader.Dispatch(), I'm trying to use Buffer.GetData() but this returns a black/empty texture because (I think) the buffer will only be done after a frame update which only happens in editor when you change something. I tried using corourines in the editor but that does not work. Using a non-buffer approach (RenderTexture & ReadPixels) only allows me to do one blur pass. Is there another way to Dispatch() and use the result of a compute shader in a loop in the editor? Here's the piece of code I written so far:
SpriteRenderer sr = GetComponent<SpriteRenderer>();
reftex = sr.sprite.texture;
Color[] outputData = new Color[reftex.width * reftex.height];
ComputeBuffer buffer = new ComputeBuffer(reftex.width * reftex.height, 16);
Color[] bufferData = outputData;
buffer.SetData(bufferData);
int kernel = shader.FindKernel("CSMain");
shader.SetTexture(kernel, "Input", reftex);
shader.SetVector("TexelSize", new Vector4(1f / reftex.width, 1f / reftex.height, reftex.width, reftex.height));
shader.SetBuffer(kernel, "Output", buffer);
shader.Dispatch(kernel, buffer.count, 1, 1);
//yield return new WaitForEndOfFrame();
buffer.GetData(outputData);
resultTexture.SetPixels(outputData);
resultTexture.Apply();
Are you sure that's the problem? I used the following code to test it and it worked fine both in edit and play mode (called via a context menu entry):
var buffer = new ComputeBuffer(1, 4);
shader.SetBuffer(0, "buffer", buffer);
var data = new uint[] { 1 };
buffer.SetData(data);
shader.Dispatch(0, 1, 1, 1);
buffer.GetData(data);
print(data[0]);
buffer.Dispose();
#pragma kernel $$anonymous$$ain
RWStructuredBuffer<uint> buffer;
[numthreads(1,1,1)]
void $$anonymous$$ain (uint3 id : SV_DispatchThreadID) {
buffer[id.x] *= 2;
}
$$anonymous$$y best guess is that there is something wrong with the compute shader itself. Have you tried if it works in play mode?
I'm not sure. When I set the output data to: Output[id.xy] = float4(0.0,1.0,0.0,1.0);
and print the first entry in the array, it prints: RGBA(0.000, 0.000, 0.000, 0.000)
In play mode its also (0,0,0,0) when implementing the yield. I'm following a variety of approaches discussed in https://forum.unity.com/threads/check-if-a-computeshader-dispatch-command-is-completed-on-gpu-before-doing-second-kernel-dispatch.369631/ nut none seem to work and the information is incomplete. Perhaps it fails to cast the data types. In your example you're using quite a simple structure uint but i'm using Color. I'll try using a float4 approach ins$$anonymous$$d.
Using Vector4[] ins$$anonymous$$d of Color[] as buffer does not solve the issue and becomes slower because I can't set pixels as SetPixels() but must do it as SetPixel() in a for loop.
SpriteRenderer sr = GetComponent<SpriteRenderer>();
reftex = sr.sprite.texture;
Vector4[] outputData = new Vector4[reftex.width * reftex.height];
ComputeBuffer buffer = new ComputeBuffer(reftex.width * reftex.height, 16);
Vector4[] bufferData = outputData;
buffer.SetData(bufferData);
int kernel = shader.Find$$anonymous$$ernel("CS$$anonymous$$ain");
shader.SetTexture(kernel, "Input", reftex);
shader.SetVector("TexelSize", new Vector4(1f / reftex.width, 1f / reftex.height, reftex.width, reftex.height));
shader.SetBuffer(kernel, "Output", buffer);
shader.Dispatch(kernel, buffer.count, 1, 1); //yield or sleep after this line
buffer.GetData(outputData);
for (int i = 0; i < reftex.width * reftex.height; i++)
{
int x = $$anonymous$$athf.RoundToInt($$anonymous$$athf.Repeat(i, reftex.width));
int y = $$anonymous$$athf.FloorToInt(i / reftex.width);
resultTexture.SetPixel(x, y, outputData[i]);
}
print(outputData[0]);
resultTexture.Apply();
The shader simply sets the color to float4(0.0,1.0,0.0,1.0) like in the comment above so i think it either does not parse through the pixels in the shader, or it can not cast a float4 array to a Vector4 array or there's something else going wrong. According to the reply from Sirshelley linked in my previous comment it is important to wait for the next frame but that does not seem to have any effect in either play mode with coroutine or edit mode with thread sleep. How would I go about doing that in the editor?
Answer by Cambesa · Feb 26, 2020 at 11:08 PM
The biggest problem was with the data structure I used for the output of the shader. By changing the output data type in the shader from RWTexture2D<float4>
or RWTexture2D<float>
to RWStructuredBuffer<float4>
and programming the shader to return a one-dimensional array of float4, (also known as Array[id.x]) instead of an Array[id.xy], I could receive the result from the shader and transfer it into the a Color[] data structure (colorArray) in C#. Then I could assign the texture with Texture.SetPixels(colorArray)
. I would still love to know how to use the RWTexture2D data type so I can simply set the colors by doing Output[id.xy] = resultFloat4
.
In conclusion: It seems that when you use compute buffers, you can not copy data from that buffer to a RWTexture2D data structure in the shader. But using compute buffers(in c#), (and in that extend RWStructuredBuffer in the shader) is the only way I know of, that enables me to use ComputeBuffer.GetData() to wait for the shader to finish.
Your answer
![](https://koobas.hobune.stream/wayback/20220612223328im_/https://answers.unity.com/themes/thub/images/avi.jpg)