# How to pass a 2D array to a function

How do you allow a function to accept a reference of a double array ? For example:

MyFunc(uint x, uint y, float2[][] myArray) {}


I get unexpected [ error does it allow reference passes like this?

I am trying to pass this to my function:

float2 _switchBuffer[2][128]
float2 _switchBuffer[2][256]


This is declared as one of my properties that the entire shader has access to so i need the kernel to pass the relevant switchbuffer to the functions.

So i have something like:

void FFT128 (uint3 id : SV_DispatchThreadID)  { Test(id.xy, 3, _switchBuffer128); }
void FFT256 (uint3 id : SV_DispatchThreadID)  { FFT1D(id.xy, 3, _swotchBuffer256); }


Unsized arrays aren't implemented in HLSL. You can pass fixed-size arrays into functions like

void foo(float2 x[2][4])
{
...
}


(Note the brackets go after the parameter name, not after the type—just like declaring a variable.)

However, having different array sizes that can be passed into the same function won't work in HLSL the way it would in C/C++. For variable-size data, better to use a buffer resource, such as a constant buffer, structured buffer, etc (depending on what you're doing with this data). Or if it's in groupshared` memory, you'll have to specify a maximum size and allocate the array for that.

• In my case its groupshared, so i guess i'll just have to use max size. Thats kinda annoying we can't pass by reference of an array but have to define the size in the method arguement, feels like the language could do with some feature improvements. – wduk Feb 28 at 8:01