3
$\begingroup$

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); }
$\endgroup$

1 Answer 1

4
$\begingroup$

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.

$\endgroup$
3
  • $\begingroup$ 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. $\endgroup$
    – wduk
    Feb 28, 2021 at 8:01
  • $\begingroup$ 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)? $\endgroup$
    – jwd
    Feb 14 at 21:50
  • $\begingroup$ 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. $\endgroup$ Feb 15 at 18:18

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.