Jump to content
  • Advertisement

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 this post


Link to post
Share on other sites
Advertisement
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 this post


Link to post
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 this post


Link to post
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 this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!