title: Unity Compute Shader Texture Array Sampling
body: <p>I've been struggling with this for while now and it is quite time critical so I have to ask here. I'm quite new to compute shaders but from what I've read, it is what I need for my usecase. I'm trying to find the total score from an array of textures, with the score being the product of each channel and a given weight. Previously, I was using NodeJS to do it but it doesn't scale as well given increasing the dimensions by 4 would increase the area required per texture by 16 and with multiple textures this isn't a good solution.</p>
  <p>This is my compute shader right now:</p>
  <pre><code>// Each #kernel tells which function to compile; you can have many kernels
  #pragma kernel CSMain
  
  // Create a RenderTexture with enableRandomWrite flag and set it
  // with cs.SetTexture
  SamplerState linearClampSampler;
  float4 weights;
  RWStructuredBuffer&lt;Texture2DArray&lt;float4&gt;&gt; scoreInput;
  float output;
  
  [numthreads(8,8,1)]
  void CSMain (uint3 id : SV_DispatchThreadID)
  {
  float4 result_mult = scoreInput[id.z].Sample(id.uv).rgba * weights.xyzw;
  output = result_mult.r + result_mult.g + result_mult.b + result_mult.a;
  
  }
  </code></pre>
  <p>For my C# dispatcher, I am doing:</p>
  <pre><code>string[] paths = new string[sessionData.masks.Length];
  Texture2D[] textures = new Texture2D[sessionData.masks.Length];
  for (int i = 0; i &lt; sessionData.masks.Length; i++)
  {
  paths[i] = sessionData.masks[i].combinedMasks;
  textures[i] = CustomUtility.LoadPNG(paths[i]);
  }
  int colourSize = sizeof(float) * 4;
  ComputeBuffer wallBuffer = new ComputeBuffer(textures.Length, colourSize);
  wallBuffer.SetData(textures);
  CalculateScoreShader.SetBuffer(0, &quot;scoreInput&quot;, wallBuffer);
  CalculateScoreShader.Dispatch(0, 8,8,1);
  </code></pre>
  <p>I can't figure out how to sample the texture properly, and I want to make sure that I am setting up the buffer correctly for the shader to used like this. I also want to retrieve the output, but again I'm unsure how to do this.</p>
  <p>I have looked through a decent amount of tutorials and documentation but I just can't seem to find the solution.</p>