Skip to content

Commit 50c5afa

Browse files
Reduce uniform size
1 parent 864d8c1 commit 50c5afa

File tree

3 files changed

+64
-56
lines changed

3 files changed

+64
-56
lines changed

data/shaders/sunlightshadow.frag

+14-14
Original file line numberDiff line numberDiff line change
@@ -10,16 +10,16 @@ uniform float split0;
1010
uniform float split1;
1111
uniform float split2;
1212
uniform float splitmax;
13+
14+
uniform vec3 sundirection;
1315
uniform float shadow_res;
16+
uniform vec3 sun_color;
1417
uniform float overlap_proportion;
1518

16-
uniform vec3 box0;
17-
uniform vec3 box1;
18-
uniform vec3 box2;
19-
uniform vec3 box3;
20-
21-
uniform vec3 sundirection;
22-
uniform vec3 sun_color;
19+
uniform float texel0;
20+
uniform float texel1;
21+
uniform float texel2;
22+
uniform float texel3;
2323

2424
in vec2 uv;
2525
#ifdef GL_ES
@@ -123,25 +123,25 @@ void main() {
123123
nbias -= Lightdir * dot(nbias, Lightdir); // Slope-scaled normal bias
124124

125125
if (xpos.z < split0) {
126-
factor = getShadowFactor(xpos.xyz + lbias + nbias * max(box0.x, box0.y), 0);
126+
factor = getShadowFactor(xpos.xyz + lbias + nbias * texel0, 0);
127127
if (xpos.z > blend_start(split0)) {
128-
factor = mix(factor, getShadowFactor(xpos.xyz + lbias + nbias * max(box1.x, box1.y), 1),
128+
factor = mix(factor, getShadowFactor(xpos.xyz + lbias + nbias * texel1, 1),
129129
(xpos.z - blend_start(split0)) / split0 / overlap_proportion);
130130
}
131131
} else if (xpos.z < split1) {
132-
factor = getShadowFactor(xpos.xyz + lbias + nbias * max(box1.x, box1.y), 1);
132+
factor = getShadowFactor(xpos.xyz + lbias + nbias * texel1, 1);
133133
if (xpos.z > blend_start(split1)) {
134-
factor = mix(factor, getShadowFactor(xpos.xyz + lbias + nbias * max(box2.x, box2.y), 2),
134+
factor = mix(factor, getShadowFactor(xpos.xyz + lbias + nbias * texel2, 2),
135135
(xpos.z - blend_start(split1)) / split1 / overlap_proportion);
136136
}
137137
} else if (xpos.z < split2) {
138-
factor = getShadowFactor(xpos.xyz + lbias + nbias * max(box2.x, box2.y), 2);
138+
factor = getShadowFactor(xpos.xyz + lbias + nbias * texel2, 2);
139139
if (xpos.z > blend_start(split2)) {
140-
factor = mix(factor, getShadowFactor(xpos.xyz + lbias + nbias * max(box3.x, box3.y), 3),
140+
factor = mix(factor, getShadowFactor(xpos.xyz + lbias + nbias * texel3, 3),
141141
(xpos.z - blend_start(split2)) / split2 / overlap_proportion);
142142
}
143143
} else if (xpos.z < splitmax) {
144-
factor = getShadowFactor(xpos.xyz + lbias + nbias * max(box3.x, box3.y), 3);
144+
factor = getShadowFactor(xpos.xyz + lbias + nbias * texel3, 3);
145145
if (xpos.z > blend_start(splitmax)) {
146146
factor = mix(factor, 1.0, (xpos.z - blend_start(splitmax)) / splitmax / overlap_proportion);
147147
}

data/shaders/sunlightshadowpcss.frag

+16-18
Original file line numberDiff line numberDiff line change
@@ -12,16 +12,16 @@ uniform float split0;
1212
uniform float split1;
1313
uniform float split2;
1414
uniform float splitmax;
15+
16+
uniform vec3 sundirection;
1517
uniform float shadow_res;
18+
uniform vec3 sun_color;
1619
uniform float overlap_proportion;
1720

18-
uniform vec3 box0;
19-
uniform vec3 box1;
20-
uniform vec3 box2;
21-
uniform vec3 box3;
22-
23-
uniform vec3 sundirection;
24-
uniform vec3 sun_color;
21+
uniform vec2 penumbra0;
22+
uniform vec2 penumbra1;
23+
uniform vec2 penumbra2;
24+
uniform vec2 penumbra3;
2525

2626
in vec2 uv;
2727
#ifdef GL_ES
@@ -139,25 +139,23 @@ float filterPCSS(vec2 uv, float z_rec, uint layer,
139139
return occludedCount * (1.0 / 16.0);
140140
}
141141

142-
float getShadowFactor(vec3 position, vec3 bbox, vec2 dz_duv, uint layer)
142+
float getShadowFactor(vec3 position, vec2 penumbra, vec2 dz_duv, uint layer)
143143
{
144-
float penumbra = tan(3.14 * sun_angle / 360.) * bbox.z * position.z;
145-
146144
// rotate the poisson disk randomly
147145
mat2 R = getRandomRotationMatrix(gl_FragCoord.xy);
148146

149147
float occludedCount = 0.0;
150148
float z_occSum = 0.0;
151149

152-
blockerSearchAndFilter(occludedCount, z_occSum, position.xy, position.z, layer, penumbra / bbox.xy, dz_duv);
150+
blockerSearchAndFilter(occludedCount, z_occSum, position.xy, position.z, layer, penumbra * position.z, dz_duv);
153151

154152
// early exit if there is no occluders at all, also avoids a divide-by-zero below.
155153
if (z_occSum == 0.0) {
156154
return 1.0;
157155
}
158156

159-
float penumbraRatio = 1.0 - z_occSum / occludedCount / position.z;
160-
vec2 radius = max(penumbra / bbox.xy * penumbraRatio, vec2(0.5 / shadow_res));
157+
float penumbraRatio = position.z - z_occSum / occludedCount;
158+
vec2 radius = max(penumbra * penumbraRatio, vec2(0.5 / shadow_res));
161159

162160
float percentageOccluded = filterPCSS(position.xy, position.z, layer, radius, R, dz_duv);
163161

@@ -174,7 +172,7 @@ void main() {
174172
vec4 xpos = getPosFromUVDepth(vec3(uv, z), u_inverse_projection_matrix);
175173

176174
vec3 norm = DecodeNormal(texture(ntex, uv).xy);
177-
float roughness =texture(ntex, uv).z;
175+
float roughness = texture(ntex, uv).z;
178176
vec3 eyedir = -normalize(xpos.xyz);
179177

180178
vec3 Lightdir = SunMRP(norm, eyedir);
@@ -206,27 +204,27 @@ void main() {
206204

207205
float factor;
208206
if (xpos.z < split0) {
209-
float factor2 = getShadowFactor(position1, box0, dz_duv1, 0);
207+
float factor2 = getShadowFactor(position1, penumbra0, dz_duv1, 0);
210208
factor = factor2;
211209
}
212210
if (blend_start(split0) < xpos.z && xpos.z < split1) {
213-
float factor2 = getShadowFactor(position2, box1, dz_duv2, 1);
211+
float factor2 = getShadowFactor(position2, penumbra1, dz_duv2, 1);
214212
if (xpos.z < split0) {
215213
factor = mix(factor, factor2, (xpos.z - blend_start(split0)) / split0 / overlap_proportion);
216214
} else {
217215
factor = factor2;
218216
}
219217
}
220218
if (blend_start(split1) < xpos.z && xpos.z < split2) {
221-
float factor2 = getShadowFactor(position3, box2, dz_duv3, 2);
219+
float factor2 = getShadowFactor(position3, penumbra2, dz_duv3, 2);
222220
if (xpos.z < split1) {
223221
factor = mix(factor, factor2, (xpos.z - blend_start(split1)) / split1 / overlap_proportion);
224222
} else {
225223
factor = factor2;
226224
}
227225
}
228226
if (blend_start(split2) < xpos.z && xpos.z < splitmax) {
229-
float factor2 = getShadowFactor(position4, box3, dz_duv4, 3);
227+
float factor2 = getShadowFactor(position4, penumbra3, dz_duv4, 3);
230228
if (xpos.z < split2) {
231229
factor = mix(factor, factor2, (xpos.z - blend_start(split2)) / split2 / overlap_proportion);
232230
} else {

src/graphics/lighting_passes.cpp

+34-24
Original file line numberDiff line numberDiff line change
@@ -205,8 +205,7 @@ class DegradedIBLShader : public TextureShader<DegradedIBLShader, 2>
205205
class ShadowedSunLightShaderPCF : public TextureShader<ShadowedSunLightShaderPCF,
206206
3, float, float, float,
207207
float, float, float,
208-
core::vector3df, core::vector3df,
209-
core::vector3df, core::vector3df,
208+
float, float, float, float,
210209
core::vector3df, video::SColorf>
211210
{
212211
public:
@@ -220,7 +219,8 @@ class ShadowedSunLightShaderPCF : public TextureShader<ShadowedSunLightShaderPCF
220219
1, "dtex", ST_NEAREST_FILTERED,
221220
8, "shadowtex", ST_SHADOW_SAMPLER);
222221
assignUniforms("split0", "split1", "split2", "splitmax", "shadow_res",
223-
"overlap_proportion", "box0", "box1", "box2", "box3", "sundirection", "sun_color");
222+
"overlap_proportion", "texel0", "texel1", "texel2", "texel3",
223+
"sundirection", "sun_color");
224224
} // ShadowedSunLightShaderPCF
225225
// ------------------------------------------------------------------------
226226
void render(GLuint normal_depth_texture,
@@ -233,15 +233,19 @@ class ShadowedSunLightShaderPCF : public TextureShader<ShadowedSunLightShaderPCF
233233
setTextureUnits(normal_depth_texture,
234234
depth_stencil_texture,
235235
shadow_framebuffer->getDepthTexture() );
236-
drawFullScreenEffect(ShadowMatrices::m_shadow_split[1],
237-
ShadowMatrices::m_shadow_split[2],
238-
ShadowMatrices::m_shadow_split[3],
239-
ShadowMatrices::m_shadow_split[4],
240-
float(UserConfigParams::m_shadows_resolution),
241-
ShadowMatrices::m_shadow_overlap_proportion,
242-
shadow_box_extents[0], shadow_box_extents[1],
243-
shadow_box_extents[2], shadow_box_extents[3],
244-
direction, col);
236+
std::array<float, 4> texel;
237+
for (int i = 0; i < 4; i++)
238+
{
239+
texel[i] = std::max(shadow_box_extents[i].X, shadow_box_extents[i].Y);
240+
}
241+
drawFullScreenEffect(ShadowMatrices::m_shadow_split[1],
242+
ShadowMatrices::m_shadow_split[2],
243+
ShadowMatrices::m_shadow_split[3],
244+
ShadowMatrices::m_shadow_split[4],
245+
float(UserConfigParams::m_shadows_resolution),
246+
ShadowMatrices::m_shadow_overlap_proportion,
247+
texel[0], texel[1], texel[2], texel[3],
248+
direction, col);
245249

246250
} // render
247251
}; // ShadowedSunLightShaderPCF
@@ -250,8 +254,8 @@ class ShadowedSunLightShaderPCF : public TextureShader<ShadowedSunLightShaderPCF
250254
class ShadowedSunLightShaderPCSS : public TextureShader<ShadowedSunLightShaderPCSS,
251255
4, float, float, float,
252256
float, float, float,
253-
core::vector3df, core::vector3df,
254-
core::vector3df, core::vector3df,
257+
core::vector2df, core::vector2df,
258+
core::vector2df, core::vector2df,
255259
core::vector3df, video::SColorf>
256260
{
257261
public:
@@ -266,7 +270,8 @@ class ShadowedSunLightShaderPCSS : public TextureShader<ShadowedSunLightShaderPC
266270
8, "shadowtexdepth", ST_NEAREST_FILTERED_ARRAY2D,
267271
16, "shadowtex", ST_SHADOW_SAMPLER);
268272
assignUniforms("split0", "split1", "split2", "splitmax", "shadow_res",
269-
"overlap_proportion", "box0", "box1", "box2", "box3", "sundirection", "sun_color");
273+
"overlap_proportion", "penumbra0", "penumbra1", "penumbra2", "penumbra3",
274+
"sundirection", "sun_color");
270275
} // ShadowedSunLightShaderPCF
271276
// ------------------------------------------------------------------------
272277
void render(GLuint normal_depth_texture,
@@ -280,15 +285,20 @@ class ShadowedSunLightShaderPCSS : public TextureShader<ShadowedSunLightShaderPC
280285
depth_stencil_texture,
281286
shadow_framebuffer->getDepthTexture(),
282287
shadow_framebuffer->getDepthTexture() );
283-
drawFullScreenEffect(ShadowMatrices::m_shadow_split[1],
284-
ShadowMatrices::m_shadow_split[2],
285-
ShadowMatrices::m_shadow_split[3],
286-
ShadowMatrices::m_shadow_split[4],
287-
float(UserConfigParams::m_shadows_resolution),
288-
ShadowMatrices::m_shadow_overlap_proportion,
289-
shadow_box_extents[0], shadow_box_extents[1],
290-
shadow_box_extents[2], shadow_box_extents[3],
291-
direction, col);
288+
std::array<core::vector2df, 4> penumbra;
289+
for (int i = 0; i < 4; i++)
290+
{
291+
float size = tan(core::PI64 * 0.54 / 360.) * shadow_box_extents[i].Z;
292+
penumbra[i] = core::vector2df(size / shadow_box_extents[i].X, size / shadow_box_extents[i].Y);
293+
}
294+
drawFullScreenEffect(ShadowMatrices::m_shadow_split[1],
295+
ShadowMatrices::m_shadow_split[2],
296+
ShadowMatrices::m_shadow_split[3],
297+
ShadowMatrices::m_shadow_split[4],
298+
float(UserConfigParams::m_shadows_resolution),
299+
ShadowMatrices::m_shadow_overlap_proportion,
300+
penumbra[0], penumbra[1], penumbra[2], penumbra[3],
301+
direction, col);
292302

293303
} // render
294304
}; // ShadowedSunLightShaderPCSS

0 commit comments

Comments
 (0)