Search Unity

Setting array of StructuredBuffer from script

Discussion in 'Shaders' started by customphase, Dec 26, 2018.

  1. customphase

    customphase

    Joined:
    Aug 19, 2012
    Posts:
    246
    Is it possible to set an array of structured buffers from script? From this thread (https://forum.unity.com/threads/passing-arrays-to-shaders.178682/#post-1222420) I found that with regular arrays you can set them by adding an index to the end of the array name like this :
    Shader:
    Code (CSharp):
    1. StructuredBuffer<float2> testBuffer[2];
    Script:
    Code (CSharp):
    1. bakeMat.SetBuffer("testBuffer0", cbs[0]);
    2. bakeMat.SetBuffer("testBuffer1", cbs[1]);
    But this doesnt seem to work with buffers, only with regular arrays. And i cant use regular arrays, since theres a limit on their size, 1k items i think, and i need more than that.
     
    Last edited: Dec 26, 2018
  2. customphase

    customphase

    Joined:
    Aug 19, 2012
    Posts:
    246
    Nevermind, worked around this by collapsing the structured buffer array into one big structured buffer and additionally supplying structured buffer of indeces range for each array.
     
  3. bgolus

    bgolus

    Joined:
    Dec 7, 2012
    Posts:
    12,342
    FYI, the hack of setting array values by named individual indices was for material property arrays, not buffers, and hasn't worked for the last 2 years since they added Set*Array() functions in 5.4.

    However this has never worked for buffers since they have always been assumed to be arrays.
     
    customphase likes this.