Search Unity

Compute Shader Multidimensional Arrays

Discussion in 'Editor & General Support' started by BayernMaik, Oct 8, 2020.

  1. BayernMaik


    Oct 15, 2019
    I am trying to figure out how to define and pass multidimensional arrays in shaders / compute shaders.

    If i have a variable within the shader HLSL code like this:
    Code (CSharp):
    1. float floatAry[17][17][17];
    and try to pass an array by script:
    Code (CSharp):
    1. computeShader.SetFloats("floatAry", new float[17, 17, 17]);
    i recieve an error :error CS1503: Argument 2: cannot convert from 'float[*,*,*]' to 'float'
    as i appearantly only can pass a single float or onedimensional array of floats

    I also tried to use a RWStructuredBuffer in HLSL code:
    Code (CSharp):
    1. RWStructuredBuffer<float> floatAryBuffer[17][17];
    and pass an array by script:
    Code (CSharp):
    2. int[,,] floatAry = new int[17, 17, 17];
    3. ComputeBuffer floatAryBuffer = new ComputeBuffer(floatAry.Length, sizeof(float));
    4. computeShader.SetBuffer(kernel, "floatAryBuffer", floatAryBuffer);
    5. floatAryBuffer.SetData(floatAry);
    what doesnt throw an error at this point, but at:
    Code (CSharp):
    2. computeShader.Dispatch(kernel, 1, 1, 1);
    with this code in shader:
    Code (CSharp):
    1. [numthreads(1, 1, 1)]
    2. void CSMain(uint3 id : SV_DispatchThreadID)
    3. {
    4.     float value = floatAryBuffer[id.x][id.y][id.z];
    5. }
    causes an error "sampler array index must be a literal expression"

    Is there a way to pass multidimensional arrays to ComputeShaders or can i only pass onedimensional arrays?