In an effort to teach myself OpenGL, I am working my way trough the 5th edition of the Superbible.
I am currently trying to figure out how to combine HDR and MSAA (as de
I don't have a copy of the Superbible, so I don't know their exact proposition, but this approach seems very inefficient, and imprecise : your 5x5 filter is only accessing the 'i'th sample of each texel, and totally misses the other samples.
For the filtering phase, I'd go, as kvark already suggested, for a resolve in another texture using glBlitFramebuffer
to have all samples accumulated in HDR. After that, doing the filter in another HDR texture, probably using a separable filter to gain performance, or even using GPU hardware to help increasing further the performance, using bilinear filtering.
This would given you a blurred texture that you could then sample in your tone mapping shader. This should vastly improve performance, but use more memory.
Note that other tone mapping operators exist, and that there is no 'ground truth' in this domain. You could choose to use more a performant approach by not using a such fine grained luminosity estimate.
You could look at Matt Pettineo's recent blog post about tone mapping, this could give you hints about how to improve things, perhaps by using glGenerateMipMaps
to create the luminosity texture.
Regarding the specific issues about tone mapping with MSAA, the only thing I'm aware of is that it's recommended to tone map individual samples before the MSAA resolve, to prevent aliasing artifacts from appearing.
As far as I see from your GLSL code the weight for all samples of a pixel are equal. From that I conclude that the code is interested in the sum of those samples for each pixel. The sum is an average multiplied by the number of samples. From here at least two optimization techniques reveal. Both are using an intermediate single-sampled texture, from which your code is supposed to sample instead of the original multi-sampled one:
(doing it precise to what you are doing). Produce an intermediate texture with a shader that writes average of the samples for each pixel.
(approximating quickly). Let the intermediate texture to be just the resolved original one. Can be done effectively by calling glBlitFramebuffer()
. This will produce slightly different result (because the sample locations are not on a grid), but for you task - HDR - it shouldn't matter, as it's all pretty much an approximation :)
Good luck!