October 26, 2011

GLSL depth of field with bokeh v2

This is my second attempt to create a depth of field shader with bokeh. the first one is here:
And I am really glad that it got popular quite fast.

This one is much more flexible and I have added few new features.

• variable sample count to increase quality/performance
• option to blur depth buffer to reduce hard edges
• option to dither the samples with noise or pattern
• bokeh chromatic aberration/fringing
• bokeh bias to bring out bokeh edges
• image thresholding to bring out highlights when image is out of focus

yet to do

• add multi-shape bokeh (does anyone knows how to make procedural pentagon or hexagon?)
• bokeh vignetting at screen edges
• some minor fixes


fragment shader HERE


  1. Looking great, I'd love to hear what you had to change to accomplish this! What kind of hw do you run this on and what kind of framerate do you get?

    Keep up the great work, like your posts!

  2. Hello Tobias,
    I am glad you like it.
    I will publish the code soon.. I just have to deal with other stuff first.
    Basically what does the trick is the texture sampling and sampling weight distribution. Right now I am on an old 2006 iMac with Mobility Radeon and the framerate is dependant on the sample count and texture size.

  3. Awesome, looking forward to reading the code and trying it out myself. Nice to hear that you are able to run it on that kind of hardware while still looking that good!

  4. Hey, I published the code. It should be really easy to implement yourself. It only requires a depth buffer as input. Have fun :)

  5. Oh awesome! Thanks will have a play with it soon! Lots of fun in your blog, have bookmarked it now so I can come back to it more often!

    Thanks for the awesome code snippets!

  6. are these blender shaders portable to apps out side of the blender game engine? Awesome work by the way.

  7. Jeremy - there shaders are application and platform independent. So any app supporting OpenGL should run them nicely without any modification.