Horde3D
http://www.horde3d.org/forums/

Depth of field
http://www.horde3d.org/forums/viewtopic.php?f=4&t=508
Page 1 of 1

Author:  Mikmacer [ 21.09.2008, 21:16 ]
Post subject:  Depth of field

Ive been working on a DOF shader for my game project some times ago. Ive decided to post an exemple demo here, so that it could be useful for someone else!

For this shader, I'm using a 5X5 gaussian blur shader with two pass, then I mix the blurred image with the original rendered image depending on the distance. The depth buffer is a custom linear depth buffer, but you could change this to the original one. Also, this shader is only doing the far blur. The near blur could be added easily.

Some screenshots :
Image

Image

You can download the demo here: http://www.mediafire.com/download.php?omjrdzgk3lm

Author:  kal [ 21.09.2008, 22:00 ]
Post subject:  Re: Depth of field

Code:
0.813    Loading resource 'utilityLib/fragPostProcess.glsl'
0.813    Shader resource 'DOFBlur.shader.xml': Compiling shader context 'DOFBLUR'
0.813    Shader resource 'DOFBlur.shader.xml': Failed to compile shader context 'DOFBLUR'
0.813    Shader resource 'DOFBlur.shader.xml': ShaderLog: [Fragment Shader]

Fragment shader failed to compile with the following errors:
ERROR: 0:2020: 'uniform' : no qualifiers allowed for function return
ERROR: 1 compilation errors.  No code generated.

Author:  Mikmacer [ 21.09.2008, 22:19 ]
Post subject:  Re: Depth of field

kal wrote:
Code:
0.813    Loading resource 'utilityLib/fragPostProcess.glsl'
0.813    Shader resource 'DOFBlur.shader.xml': Compiling shader context 'DOFBLUR'
0.813    Shader resource 'DOFBlur.shader.xml': Failed to compile shader context 'DOFBLUR'
0.813    Shader resource 'DOFBlur.shader.xml': ShaderLog: [Fragment Shader]

Fragment shader failed to compile with the following errors:
ERROR: 0:2020: 'uniform' : no qualifiers allowed for function return
ERROR: 1 compilation errors.  No code generated.


Really strange. I don't have any errors...

EDIT: I found that the shader had a uniform keyword without anything before the main, I am solving this! I don't know why it work with my computer...

EDIT: It should work now : http://www.mediafire.com/download.php?omjrdzgk3lm

Author:  kal [ 21.09.2008, 22:45 ]
Post subject:  Re: Depth of field

evil insertcode :D
It looks great!

Author:  DarkAngel [ 22.09.2008, 02:37 ]
Post subject:  Re: Depth of field

Mikmacer wrote:
Ive been working on a DOF shader for my game project some times ago. Ive decided to post an exemple demo here, so that it could be useful for someone else!
Thanks! I hope this makes it's way into the Wiki eventually.

Quote:
For this shader, I'm using a 5X5 gaussian blur shader with two pass, then I mix the blurred image with the original rendered image depending on the distance.
I made some tweaks to your shader/pipeline to make it more 'blurry', hope this is useful too

First, I dropped down the resolution of the blur buffer. This naturally makes it more blurry (which isn't a problem in this case!) and greatly reduces the cost of the blur shader. I've also enabled bilinear filtering, to get some more samples:
Code:
      <RenderTarget id="DOFBLUR" depthBuf="true" numColBufs="1" format="RGBA16F"  scale="0.25" bilinear="true" />


Second, I tweaked your sample offsets to be at 0, 1.5 and 3.5 texels.
I've hard-coded in 256 & 192 here (which correspond to 1024 & 768 * 0.25), in a robust solution these should come from the application so that other resolutions also work.
Code:
               if (passNumber.r == 1.0)
               {
                  samples[0] = -3.5/256.0;
                  samples[1] = -1.5/256.0;
                  samples[2] = 0.0;
                  samples[3] = 1.5/256.0;
                  samples[4] = 3.5/256.0;
               }
               else
               {
                  samples[0] = -3.5/192.0;
                  samples[1] = -1.5/192.0;
                  samples[2] = 0.0;
                  samples[3] = 1.5/192.0;
                  samples[4] = 3.5/192.0;
               }
Sampling at half-pixel offsets allows us to sample two pixels at once (bilinear must be enabled in the pipeline for this to work).
samples[2] is the same as before, samples[3]/samples[1] is the average of the +/- 1st & 2nd offset pixel, and samples[4]/samples[0] is the average of the +/- 3rd & 4th offset pixel.
Basically we get 9 texels with only 5 samples.

Quote:
The depth buffer is a custom linear depth buffer, but you could change this to the original one.
It would probably be much faster if the actual depth buffer from the GBuffer pass was used, maybe I'll do some work on this later :wink:

Author:  Mikmacer [ 22.09.2008, 03:00 ]
Post subject:  Re: Depth of field

Thanks a lot for those tips! I'm currently learning a lot about shaders, I always appreciate to see how my work could be improved! :D

BTW, my work is based on a custom depth buffer because I need it for some shaders in my project.

EDIT: Ive tried some of those tweaks, it can be downloaded here:

http://www.mediafire.com/download.php?tj0wnmy3lzo.

Author:  mb108 [ 23.09.2008, 19:11 ]
Post subject:  Re: Depth of field

Very nice!
Thanks for sharing

Author:  AcidFaucet [ 23.09.2008, 23:39 ]
Post subject:  Re: Depth of field

Super! I'm curious though why you chose not to have a too close factor though?

Author:  Mikmacer [ 24.09.2008, 01:18 ]
Post subject:  Re: Depth of field

AcidFaucet wrote:
Super! I'm curious though why you chose not to have a too close factor though?


The reason is simply because I didn't needed this in my project. I will work again on this effect soon since I want to make a better blur shader, and I will try to add the near blur at the same time :D

Feel free to modify this effect if you want! The near blur shouldn't be too difficult to add.

Author:  Siavash [ 24.09.2008, 06:06 ]
Post subject:  Re: Depth of field

Great shader Mikmacer ! Now we can use Call Of Duty 4 : Warfare like effects in our games.

Author:  kal [ 24.09.2008, 15:05 ]
Post subject:  Re: Depth of field

v1 looks better but v2 is faster
confused :)

Page 1 of 1 All times are UTC + 1 hour
Powered by phpBB® Forum Software © phpBB Group
https://www.phpbb.com/