Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 KyleJensen72 · Jan 30, 2018 at 05:20 PM · shaderthreadscompute shaderhlsl

ComputeShader: Accessing array elements within a StructuredBuffer

I have been toying with ComputeShaders for a while now, and have been trying to use them to leverage the hair physics calculations I was doing in my game. The hair is based off a series of points that use springs to create hair. Each strand is a list of these points.

I had success moving the physics code into a compute shader for one strand, and would be able to calculate 10,000 hair bodies at solid 60fps on one strand, but multiple strands would crash performance.

So I need to instead run a threadgroup for each hair strand, and dispatch these hair strands x amount of times. So I make a structure of hair strands, fill it with a max-sized array of 'hair body' structs. I would then access the hair strand by the groupID, and the hair bodies by the thread index.

Theoretically this should work, but I am not having these results. Due to limitations with StrucuredBuffer element sizes being max 2048 bytes, I can only store certain lengths of hair bodies per strand. When I try to change these values and read them from my C# script, they are null.

I have provided a test scenario that should duplicate my problem. I have simplified this only to get the base functionality working, which is setting the indexes of the array within the hair strand. The code is below:

ShaderTest.cs:

 public class ShaderTest : MonoBehaviour
 {
     public struct TestStruct1
     {
         public int groupID;
         public int[] threadIDs;
     }
 
     public ComputeShader computeShader;
     private ComputeBuffer computeBuffer;
     private int kernelID;
 
     void Start()
     {
         var stopWatch = new System.Diagnostics.Stopwatch();
         TestStruct1[] data = new TestStruct1[256];
 
         for (int i = 0; i < 256; i++)
         {
             TestStruct1 s1 = data[i];
             s1.threadIDs = new int[50];
         }
 
         this.kernelID = computeShader.FindKernel("CSMain");
         computeBuffer = new ComputeBuffer(256, sizeof(int) + (50 * sizeof(int)));
         computeBuffer.SetData(data);
         computeShader.SetBuffer(kernelID, "Buf", computeBuffer);
         stopWatch.Start();
         computeShader.Dispatch(kernelID, 256, 1, 1);
        
         computeBuffer.GetData(data);
 
         stopWatch.Stop();
 
         Debug.Log("Took: " + stopWatch.ElapsedMilliseconds + " milliseconds to compute");
         foreach(TestStruct1 d in data)
         {
             Debug.Log(d.threadIDs[0]); // Crashes here
         }
 
         computeBuffer.Release();
     }
 }

ShaderTest.compute:

 #pragma kernel CSMain
 
 struct TestStruct1
 {
     int groupID;
     int threadIDs[50];
 };
 
 RWStructuredBuffer<TestStruct1> Buf;
 
 [numthreads(64,1,1)]
 void CSMain(int3 threadID : SV_GroupThreadID, int3 groupID : SV_GroupID)
 {
     if (threadID.x <= 50)
     {
         Buf[groupID.x].threadIDs[threadID.x] = 7;
     }    
 }

All I am trying to do in the above code is properly index through the buffer and set each threadID to 7 for proof that this structure works. For whatever reason, it is not giving me the expected results that were so clear before.

Any help would be greatly appreciated

Comment
Add comment
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

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by mynameisjun · Jun 03, 2018 at 02:36 PM

hey i am having same problem at using struct containing array in it. just curious, did you solve this problem???

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

119 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

Related Questions

fragment shader different color for every vertex? 1 Answer

More than one Kernel in Compute Shader 1 Answer

How do int textures work in ComputeShaders? 2 Answers

How can I get correct lighting on a low poly water shader? 1 Answer

Is there a way to communicate between the gameobject and the shader? 1 Answer


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