• 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 /
  • Help Room /
avatar image
Question by diskhkme · Nov 30, 2015 at 02:28 AM · shadershader programminggpushader writingdirectx 11

Performance of looping compute shader in Update()

Hi. I have a question about the performance of compute shader, when I using it for general purpose calculation.

I faced with the problem during changing my normal CPU-based algorithm into CPU-GPU algorithm using compute shader, since calculation time becomes extremely slower when looping dispatch inside Update().

I've tested simple point-plane distance calculation with and without looping. Below is my shader code.

 #pragma kernel MyVisible
 
 struct MyData
 {
     float3 pt;
     float dist;
 };
 
 float3 normal;
 float3 ptontri;
  
 RWStructuredBuffer<MyData> dataBuffer;
  
 [numthreads(1024,1,1)]
 void MyVisible (int3 dispatchID : SV_DispatchThreadID,int3 groupID : SV_GroupID)
 {
     int id = dispatchID.x + dispatchID.y*1 + groupID.y*1024;
     float3 pl = dataBuffer[id].pt - ptontri;
     float d = dot(pl,normal);
     dataBuffer[id].dist = d;
 }

And below is the looping part, briefly.

  void Update()
     {
         //initialization is done in Start()
 
         for (int i = 0; i < 300; i++)
         {
             buffer = new ComputeBuffer(numgroup * threadcount, single_stride, ComputeBufferType.Default);
             buffer.SetData(subdata);
             int kernel = shader.FindKernel("MyVisible");
             shader.SetBuffer(kernel, "dataBuffer", buffer);
             shader.SetFloats("normal", new float[] { normal.x, normal.y, normal.z });
             shader.SetFloats("ptontri", new float[] { ptontri.x, ptontri.y, ptontri.z });
 
             shader.Dispatch(kernel, numgroup, 1, 1);
 
             MyData[] data = new MyData[numgroup * threadcount];
 
             buffer.GetData(data);
             buffer.Release();
         }
 }


The result, in short, shows about 30 times slower calculation time when looping.

W$$anonymous$$ch means calculating 300 points 300 times per frame are 30 times slower than calculating 90000 points per frame. I understand that the latter is more efficient in the perspective of parallelization, but I cannot avoid looping due to the how algorithm itself was designed, and gap between those two calculation time is strangely wide.

The main bottleneck is GetData() part in looping when I checked using System.Diagnostics.Stopwatch, and similar problem is reported in link text(in t$$anonymous$$s case, no looping was involved). Answer suggests doing some other calculation after dispatch, w$$anonymous$$ch in my case, is not applicable since we need the result value in the next loop.

So, I want to know if there's a way to improve the performance when using compute shader with looping.

Comment

People who like this

0 Show 0
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

2 Replies

· Add your reply
  • Sort: 
avatar image
Best Answer

Answer by smnerat · Jul 11, 2016 at 04:06 AM

T$$anonymous$$s is an older question, but in case anyone comes across t$$anonymous$$s. You need a completely different way of approac$$anonymous$$ng the problem. Using GetData() once or twice pretty much defeats the purpose of hardware acceleration, and your doing 300 times. Once you get your data on the GPU, you really should keep it there.

I don't know what kind of algorithm your working with so I'm going to say t$$anonymous$$s in regards to the test code you posted. Instead of getting your result (very slowly!) and then feeding it back into the compute shader, you should create two separate buffers when your script starts. Then when you run you're loop, set buffer1 as read and buffer2 as write, and then swap them at the end of the loop.

Pseudocode:

 private ComputeBuffer buffer1;
 private ComputeBuffer buffer2;
 
 void Start(){
     buffer1 = new ComputeBuffer(length, stride, type);
     buffer2 = new ComputeBuffer(length, stride, type);
 }
 
 void Update(){
     for(var i = 0; i < 300; i++){
         shader.SetBuffer(kernelID, "readBuffer", buffer1);
         shader.SetBuffer(kernelID, "writeBuffer", buffer2);    
         shader.Dispatch(kernelID, numGroup, 1, 1);
 
         Swap(buffer1, buffer2);
     }
 }
 
 void Swap(ref ComputeBuffer b1, ref ComputeBuffer b2) {
     ComputeBuffer bTemp = b1;
     b1 = b2;
     b2 = bTemp;
 }
Comment

People who like this

0 Show 1 · 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
avatar image diskhkme · Sep 05, 2016 at 03:05 AM 0
Share

I didn't applied his suggestion to my algorithm but it looks promising.

avatar image

Answer by AllMightyNico · Apr 14, 2016 at 09:30 PM

I'd like to know the same.

Comment
starikcetin

People who like this

-1 Show 0 · 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.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

39 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

Related Questions

How do I get the fragment position in world coordinates? 1 Answer

How to write a shader like this? [Screenshots] 0 Answers

Shader - SV_Target 1 Answer

Shader object parts discard by Shadow 0 Answers

Reversed UV Light with 2D PointLight (shader graph) 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