title: Compute Shaders Input 3d array of floats
body: <p>Writing a Compute Shader to be used in Unity 4. I'm attempting to get 3d noise. </p>
  
  <p><strong>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?</strong></p>
  
  <p>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 <a href="http://pastebin.com/BycaHKEP" rel="nofollow noreferrer">here</a> for the compute shader. </p>
  
  <p>I would like to extend this to work on a 3D array of float3's (I suppose the closest comparison in C# would be <code>Vector3[,,]</code>) by applying the noise operation to each float3 point in the array. </p>
  
  <p>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. </p>
  
  <p>I also managed to get <a href="http://scrawkblog.com/2013/05/18/gpu-gems-to-unity-improved-perlin-noise/" rel="nofollow noreferrer">Scrawk's</a> Implemenation working as Vertex Shaders. <strong>Scrawk got a 3D float4 array into the shader using a Texture3D</strong>. 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 <a href="https://stackoverflow.com/questions/16046293/do-directx-compute-shaders-support-2d-arrays-in-shared-memory">this post</a>. Similar question to mine, but not quite what I'm looking for. </p>
  
  <p>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).</p>
  
  <p>Constraints are the Free Trial Edition of Unity 4. </p>
  
  <p>Here's a skeleton of the C# code I'm using:</p>
  
  <pre><code>    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); // &lt;=== populated with float density values
  
      MarchingCubes.DoStuff(res); // &lt;=== The goal (Obviously not implemented yet)
  </code></pre>
  
  <p>And here's the Compute Shader</p>
  
  <pre><code>#pragma kernel CSMain
  
  uniform int simplexSeed;
  RWStructuredBuffer&lt;float3[,,]&gt; VoxelPos;  // I know these won't work, but it's what I'm trying
  RWStructuredBuffer&lt;float[,,]&gt; 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)] // &lt;== Not sure if this thread count is correct? 
  void CSMain (uint3 id : SV_DispatchThreadID)
  {
      Output[id.xyz] = simplexNoise(VoxelPos.xyz); // Where the action starts.     
  }
  </code></pre>