   Histogram computation on HDR rendering
  Hi All!!!My name is Edu. I'm currently doing a small personal project in XNA and I'm a little stuck in the HDR pipeline.I read the ShaderX6 article of Fran Carucci regarding histogram computation on the CPU to compute the max, min and average luminance values. I'm getting now a bit crazy.As stated in the article I render the scene to a FP16 format render target (I'm not using currently LogLuv encoding, to simplify), create a downsampled version 1/4 of the original size and download this version for CPU analysis. The first step is the histogram creation:Carucci suggests to use 1024 luminance slots for the histogram. The luminance encompassed by each slot will be the absolute_maximum_luminance / 1024. I'm still trying to figura what is he refrring to with 'absolute_max_luminance'Could it be the maximum luminance that could be expressed with 16 bits per channel (The 65535)?It is a parameter to be set manually by an user?The fact is that the value chosen for this parameter affects the final result of the image: if I choose a big value for it and my scene has pixels whose max luminance is relatively small compared to the absolute_max_luminance chosen, then the vast majority of pixels will fall on the same histogram slot (the greater the value of absolute_max_lum is, the greater the luminace range encompassed by each slot) and the final image will be incorrect.I also can't understand very well how histogram equalization is applied in this process. As far as I know the equalization process tries to maximize the image contrast spreading teh original histogram of the image across the entire luminance range, preserving the luminance ratios. What I've tried to do is:1.- Foreach pixel in the image compute luminance and fins its slot. Add 1/Total_Num_Pixels for that slot. This will give us the normalized histogram.2.- Compute the cumulative distribution function for the image, that will be used to equalize the image. As seen in Digital Image Processing book this function can be used to perform the luminance mapping.I store this fucntion in a num_slots x 1 texture.3.- As the image will be equalized to cover all the luminance range this implies that I can compute the max, min and average luinance this way:MinLuminance = Absolute_Max_Luminance * Min_PercentageMaxLuminance = Absolute_Max_Luminance * Max_PercentageAvgLuminance = Absolute_Max_Luminance * Avg_Percentage(I'm not very sure of this step)4.- To perform tonemapping I use a Reinhard operator, using the values computed. Before applying the operator I try to map each pixel luminance to its equalized value:// Calculate the luminance of the current pixelfloat Lw = dot(LUM_CONVERT,vColor);Lw = tex1D(RemapSampler,Lw);float Ls = Lw - g_fMinLuminance;float Ld = 0.0f;if(Ls > 0.0f){float L = (g_fMiddleGrey / g_fAvgLuminance) * Ls;Ld = L * (1.0f + L / (g_fMaxLuminance * g_fMaxLuminance)) /(1.0f + L);}vColor *= (Ld / Lw);return vColor;But this is not giving me good results. I send a capture showing the final rendered image, the render of the downsampled version of the original FP16 render target and the lighting configuration panel with the user configurable values for the algorithmSurely I'm not understandng something well. But I've been thinking on it for  two weeks and my mind is blocked now. I would appreciate very much a little help.Thanks a lot in advance, really.[Edited by - eduwushu on September 6, 2010 4:55:54 AM]
  I think the histogram you make is just needed for the visible range you currently see. so max liminanz is luminanz you get when you take 1.f from the current framebuffer and transform it back to your HDRange with the inverse forular you used the last frame.Yes, of course there might be moments where out of a sudden some extrem birght or dark situation appears and then all the accumulation is done on the first or last segment of the histogram. but then you can easily adapt during the next frames (and the human eye also needs some time to adapt to new ranges).my 2nd suggest is to use a simpler histogram, 1024 slots are kind of an overkill, at least for games. if you'd use 16slots, you could use MRT and generate the histogram easily in one pixelshader pass.just my2cent.   ;  Hi Edu,0) Who is Matt?1) Formatting postsSee you :)   ;Quote:Hi Edu,0) Who is Matt?1) Formatting postsSee you :)Oops, a little mistake hehe.Quote:so max liminanz is luminanz you get when you take 1.f from the current framebuffer and transform it back to your HDRange with the inverse forular you used the last frame.I don't know if I understand you very well... Are you suggesting to use as maximum absolute luminance the maximum pixel luminance for the current frame?Take into account that in the shaderx article it seems that absolute_max_luminance is different from max_luminance. This last one will be corrected with an autoexposure formula to perform the 'eye adaptation' effect.Abs_Max_Luminance is used as maximum luminance value which defines the range on which the luminance slots will  be distributed. Thinking on it, it only makes sense to put a value for abs_max_luminance greater or equal than the max luminance of current frame.I don't know if it makes sense to choose an abs_max_luminance greater than this value.