Skip to content

Commit cea08ec

Browse files
committed
simplify and fix depth of field
1 parent 5e48d74 commit cea08ec

File tree

4 files changed

+58
-80
lines changed

4 files changed

+58
-80
lines changed

data/cage/shader/effects/dofApply.glsl

Lines changed: 31 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,38 @@ $include vertex.glsl
33

44
$define shader fragment
55

6-
$include dofParams.glsl
6+
layout(std140, binding = 2) uniform Dof
7+
{
8+
mat4 projInv;
9+
vec4 dofNear; // near, far
10+
vec4 dofFar; // near, far
11+
};
12+
13+
vec3 s2w(vec2 p, float d)
14+
{
15+
vec4 p4 = vec4(p, d, 1.0);
16+
p4 = projInv * p4;
17+
return p4.xyz / p4.w;
18+
}
19+
20+
float interpFactor(float a, float b, float d)
21+
{
22+
if (a < b)
23+
return smoothstep(a, b, d);
24+
return d < a ? 0.0 : 1.0;
25+
}
26+
27+
void dofContribution(vec2 uv, float depth, out float near, out float far)
28+
{
29+
vec3 wp = s2w(uv * 2.0 - 1.0, depth * 2.0 - 1.0);
30+
float d = length(wp);
31+
near = 1.0 - interpFactor(dofNear[0], dofNear[1], d);
32+
far = interpFactor(dofFar[0], dofFar[1], d);
33+
}
734

835
layout(binding = 0) uniform sampler2D texColor;
936
layout(binding = 1) uniform sampler2D texDepth;
10-
layout(binding = 2) uniform sampler2D texDofNear;
11-
layout(binding = 3) uniform sampler2D texDofFar;
37+
layout(binding = 2) uniform sampler2D texDof;
1238

1339
out vec3 outColor;
1440

@@ -23,10 +49,9 @@ void main()
2349
outColor = color;
2450
return;
2551
}
26-
vec3 colorNear = textureLod(texDofNear, uv, 0).rgb;
27-
vec3 colorFar = textureLod(texDofFar, uv, 0).rgb;
52+
vec3 colorDof = textureLod(texDof, uv, 0).rgb;
2853
float near;
2954
float far;
3055
dofContribution(uv, depth, near, far);
31-
outColor = color * (1 - near - far) + colorNear * near + colorFar * far;
56+
outColor = color * (1 - near - far) + colorDof * (near + far);
3257
}

data/cage/shader/effects/dofCollect.glsl

Lines changed: 1 addition & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,7 @@ $include vertex.glsl
33

44
$define shader fragment
55

6-
$include dofParams.glsl
7-
8-
layout(location = 0) uniform int uniPass;
9-
106
layout(binding = 0) uniform sampler2D texColor;
11-
layout(binding = 1) uniform sampler2D texDepth;
127

138
out vec3 outDof;
149

@@ -24,8 +19,5 @@ void main()
2419
for (int x = 0; x < downscale; x++)
2520
color += texelFetch(texColor, ivec2(gl_FragCoord) * downscale + ivec2(x, y) - downscale / 2, 0).rgb;
2621
color /= float(downscale * downscale);
27-
float depth = textureLod(texDepth, uv, 0).x;
28-
float contribs[2];
29-
dofContribution(uv, depth, contribs[0], contribs[1]);
30-
outDof = color * contribs[uniPass];
22+
outDof = color;
3123
}

data/cage/shader/effects/dofParams.glsl

Lines changed: 0 additions & 29 deletions
This file was deleted.

sources/libengine/graphics/screenSpaceEffects.cpp

Lines changed: 26 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -154,49 +154,39 @@ namespace cage
154154
s.dofFar = Vec4(fd + fr, fd + fr + br, 0, 0);
155155
q->universalUniformStruct(s, 2);
156156

157-
TextureHandle texNear = provTex(config.provisionals, "dofNear", res, 1, GL_RGB16F);
158-
TextureHandle texFar = provTex(config.provisionals, "dofFar", res, 1, GL_RGB16F);
159-
160-
q->bind(config.inColor, 0);
161-
q->bind(config.inDepth, 1);
162-
Holder<ShaderProgram> shader = config.assets->get<AssetSchemeIndexShaderProgram, MultiShaderProgram>(HashString("cage/shader/effects/dofCollect.glsl"))->get(0);
163-
q->bind(shader);
157+
TextureHandle texDof = provTex(config.provisionals, "dofColor", res, 1, GL_RGB16F);
164158
Holder<Model> model = config.assets->get<AssetSchemeIndexModel, Model>(HashString("cage/model/square.obj"));
165-
{ // collect near
166-
q->colorTexture(fb, 0, texNear);
159+
160+
{ // collect
161+
q->bind(config.inColor, 0);
162+
q->bind(config.inDepth, 1);
163+
Holder<ShaderProgram> shader = config.assets->get<AssetSchemeIndexShaderProgram, MultiShaderProgram>(HashString("cage/shader/effects/dofCollect.glsl"))->get(0);
164+
q->bind(shader);
165+
q->colorTexture(fb, 0, texDof);
167166
q->checkFrameBuffer(fb);
168-
q->uniform(shader, 0, 0);
169167
q->draw(model);
170168
}
171-
{ // collect far
172-
q->colorTexture(fb, 0, texFar);
169+
170+
{ // blur
171+
GfGaussianBlurConfig gb;
172+
(ScreenSpaceCommonConfig &)gb = config;
173+
gb.resolution = res;
174+
gb.internalFormat = GL_RGB16F;
175+
gb.texture = texDof;
176+
for (uint32 i = 0; i < config.blurPasses; i++)
177+
gfGaussianBlur(gb);
178+
}
179+
180+
{ // apply
181+
q->viewport(Vec2i(), config.resolution);
182+
q->colorTexture(fb, 0, config.outColor);
173183
q->checkFrameBuffer(fb);
174-
q->uniform(shader, 0, 1);
184+
q->bind(config.inColor, 0);
185+
q->bind(config.inDepth, 1);
186+
q->bind(texDof, 2);
187+
q->bind(config.assets->get<AssetSchemeIndexShaderProgram, MultiShaderProgram>(HashString("cage/shader/effects/dofApply.glsl"))->get(0));
175188
q->draw(model);
176189
}
177-
178-
// blur
179-
GfGaussianBlurConfig gb;
180-
(ScreenSpaceCommonConfig &)gb = config;
181-
gb.resolution = res;
182-
gb.internalFormat = GL_RGB16F;
183-
gb.texture = texNear;
184-
for (uint32 i = 0; i < config.blurPasses; i++)
185-
gfGaussianBlur(gb);
186-
gb.texture = texFar;
187-
for (uint32 i = 0; i < config.blurPasses; i++)
188-
gfGaussianBlur(gb);
189-
190-
// apply
191-
q->viewport(Vec2i(), config.resolution);
192-
q->colorTexture(fb, 0, config.outColor);
193-
q->checkFrameBuffer(fb);
194-
q->bind(config.inColor, 0);
195-
q->bind(config.inDepth, 1);
196-
q->bind(texNear, 2);
197-
q->bind(texFar, 3);
198-
q->bind(config.assets->get<AssetSchemeIndexShaderProgram, MultiShaderProgram>(HashString("cage/shader/effects/dofApply.glsl"))->get(0));
199-
q->draw(model);
200190
}
201191

202192
namespace

0 commit comments

Comments
 (0)