# 3D Compute shader return values

## Recommended Posts

Posted (edited)

Hi,

I am trying to move some of my C# code for a noise implementation onto a Unity HLSL compute shader.  Unfortunately some of the functions returns arrays.  I have looked at several resources in an attempt to find out how to do this but I still don't have a working solution.  I did find a similar question on here (see the bottom of this post).  In this case using out' was suggested.  Is this the correct way to handle this?  If so what would the correct syntax be for a function which also has three parameters, such as:

int[] CornerPoints(float x, float y, float z)

In this case the returned array will be a multidimensional array.

int[8,3]

.Any pointers in the right direction would be much appreciated.

Edited by MasterReDWinD
Error correction

##### Share on other sites
Posted (edited)

I don't remember what the syntax is (or if it's possible) to return a statically-sized array like that in HLSL, but you can definitely use an out parameter. Something like:

void CornerPoints(float x, float y, float z, out int[8*3] points)

Note that you will likely need to do the x/y indexing manually for multidimensional arrays because I don't believe HLSL supports that (iirc).

Edited by Valakor

##### Share on other sites

Many thanks for your reply Valakor.  I've had a go at getting this working.  This is what I have so far:

void CornerPoints(float x, float y, float z, out int corners[8*3])
{
...
corners = {
// Front
x0, y0, z0, // front bottom left
x0, y1, z0, // front top left
x1, y1, z0, // front top right
x1, y0, z0, // front bottom right
// Rear
x0, y0, z1, // rear bottom left
x0, y1, z1, // rear top left
x1, y1, z1, // rear top right
x1, y0, z1  // rear bottom right
};

In the calling function I have:

    int corners[8 * 3];
CornerPoints(x, y, z, out corners);

Unfortunately I'm getting a couple of errors:

Shader error in 'VoxelShaderTest.compute': syntax error: unexpected token '{' at kernel CalculateMaterials at VoxelShaderTest.compute(124) (on d3d11)

Shader error in 'VoxelShaderTest.compute': 'CornerPoints': no matching 0 parameter function at kernel CalculateMaterials at VoxelShaderTest.compute(144) (on d3d11)

Shader error in 'VoxelShaderTest.compute': syntax error: unexpected token 'out' at kernel CalculateMaterials at VoxelShaderTest.compute(144) (on d3d11)

Do you have any ideas on these?

I think you're right on the array needing to be flattened.

##### Share on other sites
Posted (edited)

HLSL is a pretty small subset of C, so I'm unsure if it supports aggregate initialization like that. You probably just need to fill in each element like:

corners[0] = x0; corners[1] = y0; corners[2] = z0;
corners[3] = x0; corners[4] = y1; corners[5] = z0;
...

MSDN's pages about HLSL syntax and features might tell you what type of array initialization syntaxes are supported.

As for the last error, you don't specify parameters as 'out' at the callsite like you do in C#, calling the function should just look like:

int corners[8 * 3];
CornerPoints(x, y, z, corners);`

Edited by Valakor

##### Share on other sites

Thanks again.  I managed to get it to compile.

## Create an account

Register a new account

• ### Game Developer Survey

We are looking for qualified game developers to participate in a 10-minute online survey. Qualified participants will be offered a \$15 incentive for your time and insights. Click here to start!

• 12
• 18
• 15
• 9
• 9