title: XNA and HLSL: Sample entire texture for luminosity average
body: <p>I am trying to make an HDR rendering pass in my 3D app. </p>
  
  <p>I understand that in order to get the average light on a scene, you'd need to downsample the render output down to 1x1 texture. This is where I'm struggling with at the moment. </p>
  
  <p>I have set up a 1x1 resolution render target to which I'm going to draw the previous render output. I have tried drawing the output to that render target using a simple SpriteBatch Draw call and using target rectangle. However it was too much to hope for I'd discover something nobody else thought of, the result of this was not actually entire scene downsampled to 1x1 texture, it appears as if only the top left pixel was being drawn, no matter how much I played with target rectangles or Scale overloads.</p>
  
  <p>Now, I'm trying to do another screen-quad render pass, using a shader technique to sample out the scene, and render a single pixel into the render target. So to be fair, title is a bit misleading, what I'm trying to do is sample a grid of pixels spread evenly across the surface, and average those out. But this is where I'm stumped.</p>
  
  <p>I have come across this tutorial:
  <a href="http://www.xnainfo.com/content.php?content=28" rel="nofollow">http://www.xnainfo.com/content.php?content=28</a></p>
  
  <p>In the file that can be downloaded, there are several examples of downsampling and one that I like most is using loop that goes through 16 pixels, averages them out and returns that. </p>
  
  <p>Nothing I did so far managed to produce viable output. The downsampled texture is rendered to the corner of my screen for debugging purposes. </p>
  
  <p>I have modified the HLSL code to look like this:</p>
  
  <pre><code>pixelShaderStruct vertShader(vertexShaderStruct input)
  {
      pixelShaderStruct output;
  
      output.position = float4(input.pos, 1);
      output.texCoord = input.texCoord + 0.5f;
  
      return output;
  };
  
  float4 PixelShaderFunction(pixelShaderStruct input) : COLOR0
  {
     float4 color = 0;
     float2 position = input.texCoord;
  
     for(int x = 0; x &lt; 4; x++)
     {
          for (int y = 0; y &lt; 4; y++)
          {
              color += tex2D(getscene, position + float2(offsets[x], offsets[y]));
          }
     }
  
     color /= 16;
  
     return color;
  }
  </code></pre>
  
  <p>This particular line here is where I believe I'm making the error:</p>
  
  <pre><code>color += tex2D(getscene, position + float2(offsets[x], offsets[y]));
  </code></pre>
  
  <p>I have never properly understood how the texCoord values, used in <code>tex2D</code> sampling, work.
  When making motion blur effect, I had to forward such infinitismal values that I was afraid it'd get rounded off to zero, in order to produce a normal looking effect, while other times, forwarding large values like 30 and 50 was neccessary to produce effects that occupy maybe one third of the screen.</p>
  
  <p>So anyway, my question is:</p>
  
  <p>Given a screen quad (so, flat surface), how do I increment or modify the <code>texCoord</code> values to have a grid of pixels, evenly spread out, sampled across it?</p>
  
  <p>I have tried using:</p>
  
  <pre><code>color += tex2D(getscene, position + float2(offsets[x] * (1/maxX), offsets[y] * (1/maxY)));
  </code></pre>
  
  <p>Where maxX and maxY are screen resolution, </p>
  
  <pre><code>color += tex2D(getscene, position + float2(offsets[x] * x, offsets[y] * y));
  </code></pre>
  
  <p>...and other "shots in the dark", and all results have ended up being the same: the final resulting pixel appears to be identical to the one in the exact middle of my screen, as if that's the only one being sampled.</p>
  
  <p>How to solve that?</p>
  
  <p>Also, how do those texture coordinates work? Where is (0,0)? What's the maximum?</p>
  
  <p>Thanks all in advance.</p>