Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 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
3
Question by paytonleather · Mar 26, 2021 at 09:11 PM · compute shader

Sending struct containing array to compute shader

So I have been making an octree to accelerate a ray tracing compute shader, but I have hit a major block on the last hurdle to testing it, actually sending it to the compute shader. What I am trying to do is send a List(with OctreeData being a struct with 2 arrays in it among other things) to a compute shader via a structured buffer, but turns out this is not blittable(so cant be sent). Is there any way for me to be able to send this at all, or do I have to unroll the arrays into individual elements(and then unroll 512 loops worth of code in the shader)? alternatively, can I send an array within a list easily to a compute shader? Thank you for any help, as this is driving me nuts, as ive come so close to testing it

Incase it matters, extra information: I do know the size of the arrays in the struct, I just dont know how to tell it that and make it blittable, and one array is for storing children nodes(will have 8 integers always in it), and the other one is to index which triangles in a mesh a leaf node(final nodes) contains(will have 16 integers always in it)

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 Genghis42 · May 19 at 11:43 PM

I'm sure you've either solved this or given up by now, but I found your question while working on my own GPU octree code, and others might do the same.


The below struct definition (1) allows the HLSL compute shader to define it's own struct using an array; (2) is blittable when you put your data in a ComputeBuffer and send it to the shader; and (3) has array-like syntax if you need to access your data on the C# side. (In my case, I have a line-for-line C# prototype for all my HLSL code.) It admittedly only works if you know the size of your arrays ahead of time, but at least you only have to unroll a single getter and a setter.


 [Serializable]
 public struct Node
 {
     public float width;
     public Vector3 originPosition; //float3 in HLSL
     public uint bitFlags;
     // ... etc for all per-Node data
 
     // The HLSL uses a fixed-size array, like so:
     //int Children[8];
 
     // ...but C# doesn't allow fixed-size arrays in blittable structs!
     // Yet we still want our prototype code to use array syntax, i.e., WITHOUT
     // having to access each of these individually each time:
     //public int children0;
     //public int children1;
     //public int children2;
     //public int children3;
     //public int children4;
     //public int children5;
     //public int children6;
     //public int children7;
 
     // ...so, we have to use a "pretend" array:
     public ChildrenArray children;
 
     // ...defined locally, like so:
     [Serializable]
     public struct ChildrenArray
     {
         private int children0;
         private int children1;
         private int children2;
         private int children3;
         private int children4;
         private int children5;
         private int children6;
         private int children7;
 
         // The key ingredient is this indexer, which makes the "children"
         // field's API look like an array of ints:
         public int this[int i]
         {
             get { return GetChildFromNode(i); } 
             set { SetChildOnNode(i, value); }
         }
 
         private int GetChildFromNode(int octantIndex)
         {
             switch (octantIndex)
             {
                 case 0: return children0;
                 case 1: return children1;
                 case 2: return children2;
                 case 3: return children3;
                 case 4: return children4;
                 case 5: return children5;
                 case 6: return children6;
                 case 7: return children7;
                 default:
                     throw new IndexOutOfRangeException($"Children array index i must be 0 <= i < 7; got {octantIndex}");
             }
         }
 
         private void SetChildOnNode(int octantIndex, int newValue)
         {
             switch (octantIndex)
             {
                 case 0: children0 = newValue;
                     break;
                 case 1:
                     children1 = newValue;
                     break;
                 case 2:
                     children2 = newValue;
                     break;
                 case 3:
                     children3 = newValue;
                     break;
                 case 4:
                     children4 = newValue;
                     break;
                 case 5:
                     children5 = newValue;
                     break;
                 case 6:
                     children6 = newValue;
                     break;
                 case 7:
                     children7 = newValue;
                     break;
                 default:
                     throw new IndexOutOfRangeException($"Children array index i must be 0 <= i < 7; got {octantIndex}");
             }
         }
     }
 }

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

114 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

Related Questions

Shader / Compute Shader in Unity 1 Answer

DepthNormalsTexture access in Compute Shader 0 Answers

How to disable Warnings in Compute Shaders ? 1 Answer

Unity compute shader is blocked or not after Dispatch kernel? 0 Answers

Replacing a geometry shader with a compute shader. How do I output an arbitrary number of triangles? 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