<p>Unsized arrays aren't implemented in HLSL. You can pass fixed-size arrays into functions like</p>
<pre><code>void foo(float2 x[2][4])
{
    ...
}
</code></pre>
<p>(Note the brackets go after the parameter name, not after the type—just like declaring a variable.)</p>
<p>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 <code>groupshared</code> memory, you'll have to specify a maximum size and allocate the array for that.</p>

CC BY-SA 4.0
2021-02-28T08:01:43.363Z
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
CC BY-SA 4.0
2024-02-14T21:50:09.273Z
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
CC BY-SA 4.0
2024-02-15T18:18:32.727Z
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
CC BY-SA 4.0
2021-02-27T23:48:51.543Z
2021-02-27T23:48:51.543Z
Nathan Reed
<p>How do you allow a function to accept a reference of a double array ? For example:</p>
<pre><code>MyFunc(uint x, uint y, float2[][] myArray) {}
</code></pre>
<p>I get <code>unexpected [</code> error does it allow reference passes like this?</p>
<p>I am trying to pass this to my function:</p>
<pre><code>float2 _switchBuffer[2][128]
float2 _switchBuffer[2][256]`
</code></pre>
<p>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.</p>
<p>So i have something like:</p>
<pre><code>void FFT128 (uint3 id : SV_DispatchThreadID)  { Test(id.xy, 3, _switchBuffer128); }
void FFT256 (uint3 id : SV_DispatchThreadID)  { FFT1D(id.xy, 3, _swotchBuffer256); }
</code></pre>

<hlsl>
How to pass a 2D array to a function
wduk
# How to pass a 2D array to a function

(Asked by: username_0 on 2021-02-27)

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); }

```

**Tags:** hlsl


---

## 1 answers

### Answer by username_1 on 2021-02-27. Score: 4 | Accepted answer

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.

> **Comments:**
> **username_0**: 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.  
> **username_2**: 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)?  
> **username_1**: 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.  

---

