3

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); }
wduk
  • 177
  • 1
  • 6

1 Answers1

4

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.

Nathan Reed
  • 25,002
  • 2
  • 68
  • 107
  • 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 '21 at 08:01
  • Does this result in a full copy of the array, or is the compiler smart enough to turn it into something like a pointer (even though the language doesn't expose such things directly)? – jwd Feb 14 '24 at 21:50
  • In shaders code, typically everything is inlined by the compiler and so all local variables and function parameters are ultimately just stored in registers. There is neither a copy nor a pointer. – Nathan Reed Feb 15 '24 at 18:18