Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

[Compute] Sending a struct with some data

Discussion in 'Shaders' started by Testfor, Jul 7, 2017.

  1. Testfor

    Testfor

    Joined:
    Jan 22, 2015
    Posts:
    55
    Good morning/night everyone,

    This week I´ve been working on Gerstner simulation (ocean waves). I have the basic algorithm working on the CPU but I would like to spice things up moving it to the GPU using compute (it is the first time I use compute xD).

    The idea is to send a struct to the GPU containing some data like time,waves properties etc

    The following is the declaration of the structs in the shader:
    Code (csharp):
    1.  
    2. // Size 24
    3. struct WaveProperties
    4. {
    5.     float chopy;
    6.     float A;
    7.     float2 D;
    8.     float L;
    9.     float s;
    10. };
    11.  
    12. // Size 24 * 8 + 8 = 200
    13. struct WaveSimulation
    14. {
    15.     WaveProperties waves[8];
    16.     float time;
    17.     float numWaves;
    18. };
    19. //...
    20. RWStructuredBuffer<WaveSimulation> WavesConstants;
    21. RWTexture2D<float4> HeightOutput;
    22.  
    ... this is in the CPU
    Code (csharp):
    1.  
    2. public struct Vec2
    3. {
    4.     public float x;
    5.     public float y;
    6.     public Vec2(float x_,float y_)
    7.     {
    8.         x = x_;
    9.         y = y_;
    10.     }
    11. };
    12.  
    13. public struct WaveProperties
    14. {
    15.     public float chopy;    // The actual parametizable value
    16.     public float A;        // Amplitude
    17.     public Vec2 D;         // Wave direction
    18.     public float L;        // Distance crest to crest
    19.     public float s;        // speed m/s
    20. };
    21.  
    22. struct WaveSimulation
    23. {
    24.     public WaveProperties[] waves;
    25.     public float time;
    26.     public float numWaves;
    27. };
    28.  
    I did my own vec2 class just in case unity hides some extra data. I also don´t know if the WaveProperties[] array in C# can cause me trouble.

    I set the buffer like this:

    Code (csharp):
    1.  
    2.         mWaveSimulation.time = 0.5f;
    3.         mWaveSimulation.numWaves = mWaves.Count;
    4.         mWaveSimulation.waves = new WaveProperties[mWaves.Count];
    5.         mWaves.CopyTo(mWaveSimulation.waves);
    6.         mWavesBuffer = new ComputeBuffer(1, 200);
    7.         mWavesBuffer.SetData(new[] { mWaveSimulation });
    8.  
    Well, the problem is that the values that I´m getting are all 0/NaN

    Any ideas? Thanks!
     
  2. Testfor

    Testfor

    Joined:
    Jan 22, 2015
    Posts:
    55
    Alright, so instead of trying to mix both the "general info" buffer and the "waves" buffer, now I have two buffers. I did some testing and checks because previously I was misunderstanding some concepts. So far so good, now I think I have all the data I need in the shader! I'll do some tests and actually contrast the GPU result with the CPU one.

    I will keep updating this post for other people in the future ;)

    Thanks.