Writing a Compute Shader to be used in Unity 4. I'm attempting to get 3d noise.
The goal is to get a multidiminsional float3 array into my compute shader from my C# code. Is this possible in a straightforward manner (using some kind of declaration) or can it only be achieved using Texture3D objects?
I currently have an implementation of simplex noise working on individual float3 points, outputting a single float -1 to 1. I ported the code found here for the compute shader.
I would like to extend this to work on a 3D array of float3's (I suppose the closest comparison in C# would be Vector3[,,]
) by applying the noise operation to each float3 point in the array.
I've tried a few other things, but they feel bizarre and completely miss the point of using a parallel approach. The above is what I imagine it should look like.
I also managed to get Scrawk's Implemenation working as Vertex Shaders. Scrawk got a 3D float4 array into the shader using a Texture3D. But I wasn't able to extract the floats from the texture. Is that how Compute Shaders work as well? Relying on Textures? I probably have overlooked something concerning getting the values out of the Texture. This seems to be how this user was getting data in in this post. Similar question to mine, but not quite what I'm looking for.
New to shaders in general, and I feel like I'm missing something pretty fundamental about Compute Shaders and how they work. The goal is to (as I'm sure you've guessed) get noise generation and mesh computation using marching cubes onto the GPU using Compute Shaders (or whatever shader is best suited to this kind of work).
Constraints are the Free Trial Edition of Unity 4.
Here's a skeleton of the C# code I'm using:
int volumeSize = 16;
compute.SetInt ("simplexSeed", 10);
// This will be a float[,,] array with our density values.
ComputeBuffer output = new ComputeBuffer (/*s ize goes here, no idea */, 16);
compute.SetBuffer (compute.FindKernel ("CSMain"), "Output", output);
// Buffer filled with float3[,,] equivalent, what ever that is in C#. Also what is 'Stride'?
// Haven't found anything exactly clear. I think it's the size of basic datatype we're using in the buffer?
ComputeBuffer voxelPositions = new ComputeBuffer (/* size goes here, no idea */, 16);
compute.SetBuffer (compute.FindKernel ("CSMain"), "VoxelPos", voxelPositions);
compute.Dispatch(0,16,16,16);
float[,,] res = new float[volumeSize, volumeSize, volumeSize];
output.GetData(res); // <=== populated with float density values
MarchingCubes.DoStuff(res); // <=== The goal (Obviously not implemented yet)
And here's the Compute Shader
#pragma kernel CSMain
uniform int simplexSeed;
RWStructuredBuffer<float3[,,]> VoxelPos; // I know these won't work, but it's what I'm trying
RWStructuredBuffer<float[,,]> Output; // to get in there.
float simplexNoise(float3 input)
{
/* ... A bunch of awesome stuff the pastebin guy did ...*/
return noise;
}
/** A bunch of other awesome stuff to support the simplexNoise function **/
/* .... */
/* Here's the entry point, with my (supposedly) supplied input kicking things off */
[numthreads(16,16,16)] // <== Not sure if this thread count is correct?
void CSMain (uint3 id : SV_DispatchThreadID)
{
Output[id.xyz] = simplexNoise(VoxelPos.xyz); // Where the action starts.
}