   Luminance calc on PS. How to refeed the pipeline with a texture outputed from PS?
  Hi,I want to calculte the average luminance of a texture. I could do this in CPU, calculating the luminance of each pixel and dividing by the number of pixels.But I need it much faster, so I want to do it in GPU with the pixel shader.The basic idea is, imagine a texture 1D texture with 2^n pixels. I would sum the pixel 0 with the pixel n/2 and put it on the pixel 0 of a new texture of size n/2.Then I would sum the pixel 1 with the pixel n/2 +1 and put on the pixel 1 of this new texture.So, the new texture has size n/2. Then I would refeed the pipeline with this new texture, and do the same process on it. So I would get a texture with size n/4.Doing this process log n times finishes it.So, how do I do it on XNA? How do I refeed things etc... is there any sample on i?. I've searched and did not find any info.Thanks   
  All you need to do is create the required render targets for all of the sizes you need, and then you render to one render target while sampling from the previous one. If you'd like to see some code for setting up this sort of pipeline, you can have a look at my HDR sample.   ;  OMG, thats all I needed.Thanks mat for the code for configuring the pipeline!I will try and might have some doubts later, I will reply when I have them.   ;  Hi Matt, i have a doubt.Suppose my initial texture is 512x512.So, my first render target will be 256x256 becauseI want to Output the pixel color on a 256x256 texture(Am I right on this?)But, I want to sample from the 512x512 texture. So how do I do this? Becausethe texture coordinates are going to be interpolated based on the render targetsize(256x256).(Am I right on this? )If I am right on both sentences above, does this work?(Suppose the actual RT is 256x256 and I want to sample based on the 512x512 texture)//PS codefloat dx = 1/512;float dy = 1/512;input.TexCoords = input.TexCoords/2;    float3 pixel = float3(tex2D(texturesS,input.TexCoords).rgb);    float3 pixeldx = float3(tex2D(texturesS,float2(input.TexCoords.x + dx,inputTexCoords.y)).rgb);    float3 pixeldy = float3(tex2D(texturesS,float2(input.TexCoords.x,inputTexCoords.y + dy)).rgb);    float3 pixeldxdy = float3(tex2D(texturesS,float2(input.TexCoords.x + dx,inputTexCoords.y + dy)).rgb);    //luminance calculation    float3 finalColor = (0.299f  * (pixel.R + pixeldx.R + pixeldy.R + pixeldxdy.R))    +(0.587f  * (pixel.G + pixeldx.G + pixeldy.G + pixeldxdy.G))    +(0.114f  * (pixel.B + pixeldx.B + pixeldy.B + pixeldxdy.B));   return float4(finalColor, 1);;  I think my assumption above is wrong.My first render target needs to be 512x512, but I'm really confused, if it needs to be 512x512, how I am going to output an 256x256 texture??   ;As Matt said:Quote:create the required render targets for all of the sizes you needYou basically render your scene to the 512x512 target to get a good initial size. Then you render the result from your 512x512 target (retrieve with GetTexture()) to a 256x256 target, then the 256x256 result to a 128x128 target and so on till you end up with your 1x1 texture you need.I *think* you only need to convert to luminance the first time you render to the 512x512 target, after that you'll typically just want to interpolate/average these luminance values for the smaller targets. I'm not sure where your shader code comes from, but instead of trying to advice you on these parts, I'd suggest you delve through Matt's sample some more to see how he goes about doing these things.;  Yeah you'll need to take 4 samples instead of 1, since your render target is 1/4 the size of your input texture. Or if you're using a surface format that your hardware can filter, you can just take a single sample with bilinear filtering.   