Thanks - I am all for code-sharing, but am also a lazy bugger. If you could do an overview description of your method, it would be much appreciated! :)
Well long story short it's similar to the idea of using a mipmap pyramid to be able to have your image blurred at different radii, but instead of creating the mipmap only going down, it adds a phase that goes the other way as well, from coarse to the top level. Those are called pyramid filters, and well, there's a reference to a paper in the introduction of the post, that explains them pretty well. Adapting that to DOF is kindof natural, but not so easy. The author of the first paper I reference also made a DOF system out of pyramid filtering, but it's more complicated than this one, works better but it's not for realtime
August 26, 2009 at 1:58 PM
I'm back from a short trip to Montreal, a lot of people have exhaustively blogged about Siggraph and such things so I won't.
Instead as promised, here is the source code from my DOF experiment. Works in FX Composer 2.5, I couldn't use 1.x this time because it crashed on my macbook... 2.5 has other bugs I had to work around, but I managed to solve those (see the code).
As with some other previous snippets I published, those are small tests I did at home, I hope they can be inspiring for someone, maybe even in other domains, more than something I'll use right now on a game (the catch is, if they were, I couldn't publish them anyways ;)
A little background:
Pyramid filtering is a very useful technique for implementing a wide class of continuous convolution kernels. They can be used for a wide range of applications, from image upsampling to inpaiting (see: Strengert , 2007. Pyramid Methods in GPU-Based Image Processing. Conference on Computer Graphics Theory and Applications (GRAPP'07), volume GM-R, pp.21-28).
As with separable filtering, they can be computed in linear time, but unlike them, it's easy to simultaneously compute the convolution for different kernel sizes simultaneously.
A pyramid filter works by applying a convolution, with a small kernel, on the source image, and downsampling the result into a smaller texture. This step is called analysis, and it's repeated multiple times. Each new analysis level allows us to compute our kernel over a wider area. After a given number of analysis steps, we perform the same number of synthesis steps, where we start on the smallest level of our image pyramid, and go up by convolving and upsampling the image.
This pyramid lets us vary the size of our filtering kernel in the synthesis step. As the kernel size depends on the depth of the pyramid, deciding on which level a given pixel starts its synthesis process affects the size of the filter applied to that region of the image.
There are a few problems to be solved if you want to implement DOF with this.
The first one is that you need to mask some samples during your blurring pass, and it's not so obvious to choose how to do that, as the filtering is a two-pass process now. Ideally you'd want to mask different thinks during the first and the second pass, but it's not really possible.
The second problem is how to deal with foreground blur, that is, bleeding out the blur outside foreground object borders (see Starcraft II Effects & Techniques. Advances in Real-Time Rendering in 3D Graphics and Games Course - SIGGRAPH 2008).
I found a solution that does not look too bad, it's still improvable (a lot) and tweakable, but I didn't work further on it because it's stil unable to achieve a good bokeh, and I think that's really something we have to improve in our DOF effects now. Probably it's better suited for other effects, where you don't have so many discontinuities in your blur radius. Smoke and fog scattering for example could work very well.
string UIWidget = "none"; // suppress UI for this variable string ScriptClass = "scene"; // this fx will render then scene AND the postprocessing string ScriptOrder = "standard"; string ScriptOutput = "color"; string Script = "Technique=Main;"; > = 0.8; // FX Composer supports SAS .86 (directX 1.0 version does not support scripting)
"Experiment: DOF with Pyramidal Filters"
2 Comments -
Thanks - I am all for code-sharing, but am also a lazy bugger. If you could do an overview description of your method, it would be much appreciated! :)
Thanks again!
August 26, 2009 at 2:01 AM
Well long story short it's similar to the idea of using a mipmap pyramid to be able to have your image blurred at different radii, but instead of creating the mipmap only going down, it adds a phase that goes the other way as well, from coarse to the top level. Those are called pyramid filters, and well, there's a reference to a paper in the introduction of the post, that explains them pretty well. Adapting that to DOF is kindof natural, but not so easy. The author of the first paper I reference also made a DOF system out of pyramid filtering, but it's more complicated than this one, works better but it's not for realtime
August 26, 2009 at 1:58 PM