Horde3D

Next-Generation Graphics Engine
It is currently 02.10.2014, 05:21

All times are UTC + 1 hour




Post new topic Reply to topic  [ 11 posts ] 
Author Message
 Post subject: Depth of field
PostPosted: 21.09.2008, 21:16 
Offline

Joined: 16.01.2008, 00:24
Posts: 73
Location: Canada/Quebec
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


Last edited by Mikmacer on 21.09.2008, 22:29, edited 1 time in total.

Top
 Profile  
 
 Post subject: Re: Depth of field
PostPosted: 21.09.2008, 22:00 
Offline

Joined: 18.05.2008, 17:47
Posts: 96
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.


Top
 Profile  
 
 Post subject: Re: Depth of field
PostPosted: 21.09.2008, 22:19 
Offline

Joined: 16.01.2008, 00:24
Posts: 73
Location: Canada/Quebec
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


Top
 Profile  
 
 Post subject: Re: Depth of field
PostPosted: 21.09.2008, 22:45 
Offline

Joined: 18.05.2008, 17:47
Posts: 96
evil insertcode :D
It looks great!


Top
 Profile  
 
 Post subject: Re: Depth of field
PostPosted: 22.09.2008, 02:37 
Offline

Joined: 08.11.2006, 03:10
Posts: 384
Location: Australia
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:


Top
 Profile  
 
 Post subject: Re: Depth of field
PostPosted: 22.09.2008, 03:00 
Offline

Joined: 16.01.2008, 00:24
Posts: 73
Location: Canada/Quebec
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.


Top
 Profile  
 
 Post subject: Re: Depth of field
PostPosted: 23.09.2008, 19:11 
Offline

Joined: 08.03.2008, 18:45
Posts: 23
Very nice!
Thanks for sharing


Top
 Profile  
 
 Post subject: Re: Depth of field
PostPosted: 23.09.2008, 23:39 
Offline

Joined: 19.11.2007, 19:35
Posts: 218
Super! I'm curious though why you chose not to have a too close factor though?


Top
 Profile  
 
 Post subject: Re: Depth of field
PostPosted: 24.09.2008, 01:18 
Offline

Joined: 16.01.2008, 00:24
Posts: 73
Location: Canada/Quebec
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.


Top
 Profile  
 
 Post subject: Re: Depth of field
PostPosted: 24.09.2008, 06:06 
Offline

Joined: 21.08.2008, 11:44
Posts: 352
Great shader Mikmacer ! Now we can use Call Of Duty 4 : Warfare like effects in our games.


Top
 Profile  
 
 Post subject: Re: Depth of field
PostPosted: 24.09.2008, 15:05 
Offline

Joined: 18.05.2008, 17:47
Posts: 96
v1 looks better but v2 is faster
confused :)


Top
 Profile  
 
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 11 posts ] 

All times are UTC + 1 hour


Who is online

Users browsing this forum: No registered users and 1 guest


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Search for:
Jump to:  
cron
Powered by phpBB® Forum Software © phpBB Group