Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
12 Jun 22 - 14 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by Cambesa · Feb 14, 2020 at 04:28 PM · editorloopblurcompute shaderhlsl

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();

Comment
Add comment · Show 3
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image BastianUrbach · Feb 14, 2020 at 10:03 PM 0
Share

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?

avatar image Cambesa BastianUrbach · Feb 14, 2020 at 11:51 PM 0
Share

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.

avatar image Cambesa Cambesa · Feb 18, 2020 at 05:02 PM 0
Share

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?

1 Reply

· Add your reply
  • Sort: 
avatar image
0

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.

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

161 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Persistent Data in Compute Shaders. 0 Answers

fragment shader different color for every vertex? 1 Answer

How do int textures work in ComputeShaders? 2 Answers

Swapping Compute Shaders 1 Answer

Callback when (compute) shader is reloaded 0 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges