Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice

[SOLVED] Unity compute shader: cycle through RWStructuredBuffer

Discussion in 'Scripting' started by dima13230, May 29, 2018.

  1. dima13230

    dima13230

    Joined:
    Dec 19, 2016
    Posts:
    6
    Hi all, I'm new in shader programming, so I have question: how to make cycle which goes through RWStructuredBuffer?
    I want to make molecules simulation so I have to make them attract each other, so I decided to make cycle which will do it. My attempt doesn't work, this is error:
    invalid type for index - index must be a scalar, or a vector with the correct number of dimensions at kernel CSMain

    Code (CSharp):
    1. #pragma kernel CSMain
    2.  
    3. #include "Math.compute"
    4.  
    5. struct Molecule
    6. {
    7.     float mass;
    8.     float3 position;
    9.     float3 velocity;
    10.     float diameter;
    11. };
    12.  
    13. RWStructuredBuffer<Molecule> dataBuffer;
    14.  
    15. [numthreads(16,1,1)]
    16. void CSMain (uint3 id : SV_DispatchThreadID)
    17. {
    18.     for( int i = 0; i < dataBuffer.Length; i ++ )
    19.     {
    20.         if (i != id)
    21.         {
    22.             dataBuffer[id].velocity = gravityConstant * (towards(dataBuffer[id].position, dataBuffer[i].position) * dataBuffer[id].mass * dataBuffer[i].mass / distance(dataBuffer[id.position], dataBuffer[i].position) ^ 2;
    23.         }
    24.     }
    25. }
    Contents of Math.compute.
    Code (CSharp):
    1. const float gravityConstant = 0.000000000067385;
    2.  
    3. float3 distance ( float3 a, float3 b)
    4. {
    5.     float3 vec = float3(a.x - b.x, a.y - b.y, a.z - b.z);
    6.     return sqrt(vec.x * vec.x + vec.y * vec.y + vec.z * vec.z);
    7. };
    8.  
    9. float3 towards ( float3 a, float3 b)
    10. {
    11.     return b - a;
    12. };
    After using google I found that id is a vector type variable so I have to list through buffer with some vector. What it should look like?
     
    Last edited: May 29, 2018
  2. Pavlon

    Pavlon

    Joined:
    Apr 15, 2015
    Posts:
    191
    Not sure if this is the problem but at line 22 it looks like you try to make a float3 out of a float without proper casting...

    Also cant see any initializer or some code that adds objects to your buffer.
     
  3. dima13230

    dima13230

    Joined:
    Dec 19, 2016
    Posts:
    6
    In fact variable "i" should be uint3 type, but I don't know how to properly use it. What about initializer or some code that adds objects to buffer -- I made no it yet because I have problem with shader
     
  4. dima13230

    dima13230

    Joined:
    Dec 19, 2016
    Posts:
    6
    I solved my problem -- to access RWStructuredBuffer I defined new uint3 type variable:
    Code (CSharp):
    1. for( int i = 0; i < (int)dataBuffer.Length; i++ )
    2. {
    3.     uint3 id2 = uint3(i,0,0);
    4.     dataBuffer[id2.x] // And then write some code
    5. }
     
    ouzer likes this.