Skip to content

Commit

Permalink
Reduce uniform size
Browse files Browse the repository at this point in the history
  • Loading branch information
CodingJellyfish committed Feb 17, 2025
1 parent 864d8c1 commit 50c5afa
Show file tree
Hide file tree
Showing 3 changed files with 64 additions and 56 deletions.
28 changes: 14 additions & 14 deletions data/shaders/sunlightshadow.frag
Original file line number Diff line number Diff line change
Expand Up @@ -10,16 +10,16 @@ uniform float split0;
uniform float split1;
uniform float split2;
uniform float splitmax;

uniform vec3 sundirection;
uniform float shadow_res;
uniform vec3 sun_color;
uniform float overlap_proportion;

uniform vec3 box0;
uniform vec3 box1;
uniform vec3 box2;
uniform vec3 box3;

uniform vec3 sundirection;
uniform vec3 sun_color;
uniform float texel0;
uniform float texel1;
uniform float texel2;
uniform float texel3;

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

if (xpos.z < split0) {
factor = getShadowFactor(xpos.xyz + lbias + nbias * max(box0.x, box0.y), 0);
factor = getShadowFactor(xpos.xyz + lbias + nbias * texel0, 0);
if (xpos.z > blend_start(split0)) {
factor = mix(factor, getShadowFactor(xpos.xyz + lbias + nbias * max(box1.x, box1.y), 1),
factor = mix(factor, getShadowFactor(xpos.xyz + lbias + nbias * texel1, 1),
(xpos.z - blend_start(split0)) / split0 / overlap_proportion);
}
} else if (xpos.z < split1) {
factor = getShadowFactor(xpos.xyz + lbias + nbias * max(box1.x, box1.y), 1);
factor = getShadowFactor(xpos.xyz + lbias + nbias * texel1, 1);
if (xpos.z > blend_start(split1)) {
factor = mix(factor, getShadowFactor(xpos.xyz + lbias + nbias * max(box2.x, box2.y), 2),
factor = mix(factor, getShadowFactor(xpos.xyz + lbias + nbias * texel2, 2),
(xpos.z - blend_start(split1)) / split1 / overlap_proportion);
}
} else if (xpos.z < split2) {
factor = getShadowFactor(xpos.xyz + lbias + nbias * max(box2.x, box2.y), 2);
factor = getShadowFactor(xpos.xyz + lbias + nbias * texel2, 2);
if (xpos.z > blend_start(split2)) {
factor = mix(factor, getShadowFactor(xpos.xyz + lbias + nbias * max(box3.x, box3.y), 3),
factor = mix(factor, getShadowFactor(xpos.xyz + lbias + nbias * texel3, 3),
(xpos.z - blend_start(split2)) / split2 / overlap_proportion);
}
} else if (xpos.z < splitmax) {
factor = getShadowFactor(xpos.xyz + lbias + nbias * max(box3.x, box3.y), 3);
factor = getShadowFactor(xpos.xyz + lbias + nbias * texel3, 3);
if (xpos.z > blend_start(splitmax)) {
factor = mix(factor, 1.0, (xpos.z - blend_start(splitmax)) / splitmax / overlap_proportion);
}
Expand Down
34 changes: 16 additions & 18 deletions data/shaders/sunlightshadowpcss.frag
Original file line number Diff line number Diff line change
Expand Up @@ -12,16 +12,16 @@ uniform float split0;
uniform float split1;
uniform float split2;
uniform float splitmax;

uniform vec3 sundirection;
uniform float shadow_res;
uniform vec3 sun_color;
uniform float overlap_proportion;

uniform vec3 box0;
uniform vec3 box1;
uniform vec3 box2;
uniform vec3 box3;

uniform vec3 sundirection;
uniform vec3 sun_color;
uniform vec2 penumbra0;
uniform vec2 penumbra1;
uniform vec2 penumbra2;
uniform vec2 penumbra3;

in vec2 uv;
#ifdef GL_ES
Expand Down Expand Up @@ -139,25 +139,23 @@ float filterPCSS(vec2 uv, float z_rec, uint layer,
return occludedCount * (1.0 / 16.0);
}

float getShadowFactor(vec3 position, vec3 bbox, vec2 dz_duv, uint layer)
float getShadowFactor(vec3 position, vec2 penumbra, vec2 dz_duv, uint layer)
{
float penumbra = tan(3.14 * sun_angle / 360.) * bbox.z * position.z;

// rotate the poisson disk randomly
mat2 R = getRandomRotationMatrix(gl_FragCoord.xy);

float occludedCount = 0.0;
float z_occSum = 0.0;

blockerSearchAndFilter(occludedCount, z_occSum, position.xy, position.z, layer, penumbra / bbox.xy, dz_duv);
blockerSearchAndFilter(occludedCount, z_occSum, position.xy, position.z, layer, penumbra * position.z, dz_duv);

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

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

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

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

vec3 norm = DecodeNormal(texture(ntex, uv).xy);
float roughness =texture(ntex, uv).z;
float roughness = texture(ntex, uv).z;
vec3 eyedir = -normalize(xpos.xyz);

vec3 Lightdir = SunMRP(norm, eyedir);
Expand Down Expand Up @@ -206,27 +204,27 @@ void main() {

float factor;
if (xpos.z < split0) {
float factor2 = getShadowFactor(position1, box0, dz_duv1, 0);
float factor2 = getShadowFactor(position1, penumbra0, dz_duv1, 0);
factor = factor2;
}
if (blend_start(split0) < xpos.z && xpos.z < split1) {
float factor2 = getShadowFactor(position2, box1, dz_duv2, 1);
float factor2 = getShadowFactor(position2, penumbra1, dz_duv2, 1);
if (xpos.z < split0) {
factor = mix(factor, factor2, (xpos.z - blend_start(split0)) / split0 / overlap_proportion);
} else {
factor = factor2;
}
}
if (blend_start(split1) < xpos.z && xpos.z < split2) {
float factor2 = getShadowFactor(position3, box2, dz_duv3, 2);
float factor2 = getShadowFactor(position3, penumbra2, dz_duv3, 2);
if (xpos.z < split1) {
factor = mix(factor, factor2, (xpos.z - blend_start(split1)) / split1 / overlap_proportion);
} else {
factor = factor2;
}
}
if (blend_start(split2) < xpos.z && xpos.z < splitmax) {
float factor2 = getShadowFactor(position4, box3, dz_duv4, 3);
float factor2 = getShadowFactor(position4, penumbra3, dz_duv4, 3);
if (xpos.z < split2) {
factor = mix(factor, factor2, (xpos.z - blend_start(split2)) / split2 / overlap_proportion);
} else {
Expand Down
58 changes: 34 additions & 24 deletions src/graphics/lighting_passes.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -205,8 +205,7 @@ class DegradedIBLShader : public TextureShader<DegradedIBLShader, 2>
class ShadowedSunLightShaderPCF : public TextureShader<ShadowedSunLightShaderPCF,
3, float, float, float,
float, float, float,
core::vector3df, core::vector3df,
core::vector3df, core::vector3df,
float, float, float, float,
core::vector3df, video::SColorf>
{
public:
Expand All @@ -220,7 +219,8 @@ class ShadowedSunLightShaderPCF : public TextureShader<ShadowedSunLightShaderPCF
1, "dtex", ST_NEAREST_FILTERED,
8, "shadowtex", ST_SHADOW_SAMPLER);
assignUniforms("split0", "split1", "split2", "splitmax", "shadow_res",
"overlap_proportion", "box0", "box1", "box2", "box3", "sundirection", "sun_color");
"overlap_proportion", "texel0", "texel1", "texel2", "texel3",
"sundirection", "sun_color");
} // ShadowedSunLightShaderPCF
// ------------------------------------------------------------------------
void render(GLuint normal_depth_texture,
Expand All @@ -233,15 +233,19 @@ class ShadowedSunLightShaderPCF : public TextureShader<ShadowedSunLightShaderPCF
setTextureUnits(normal_depth_texture,
depth_stencil_texture,
shadow_framebuffer->getDepthTexture() );
drawFullScreenEffect(ShadowMatrices::m_shadow_split[1],
ShadowMatrices::m_shadow_split[2],
ShadowMatrices::m_shadow_split[3],
ShadowMatrices::m_shadow_split[4],
float(UserConfigParams::m_shadows_resolution),
ShadowMatrices::m_shadow_overlap_proportion,
shadow_box_extents[0], shadow_box_extents[1],
shadow_box_extents[2], shadow_box_extents[3],
direction, col);
std::array<float, 4> texel;
for (int i = 0; i < 4; i++)
{
texel[i] = std::max(shadow_box_extents[i].X, shadow_box_extents[i].Y);
}
drawFullScreenEffect(ShadowMatrices::m_shadow_split[1],
ShadowMatrices::m_shadow_split[2],
ShadowMatrices::m_shadow_split[3],
ShadowMatrices::m_shadow_split[4],
float(UserConfigParams::m_shadows_resolution),
ShadowMatrices::m_shadow_overlap_proportion,
texel[0], texel[1], texel[2], texel[3],
direction, col);

} // render
}; // ShadowedSunLightShaderPCF
Expand All @@ -250,8 +254,8 @@ class ShadowedSunLightShaderPCF : public TextureShader<ShadowedSunLightShaderPCF
class ShadowedSunLightShaderPCSS : public TextureShader<ShadowedSunLightShaderPCSS,
4, float, float, float,
float, float, float,
core::vector3df, core::vector3df,
core::vector3df, core::vector3df,
core::vector2df, core::vector2df,
core::vector2df, core::vector2df,
core::vector3df, video::SColorf>
{
public:
Expand All @@ -266,7 +270,8 @@ class ShadowedSunLightShaderPCSS : public TextureShader<ShadowedSunLightShaderPC
8, "shadowtexdepth", ST_NEAREST_FILTERED_ARRAY2D,
16, "shadowtex", ST_SHADOW_SAMPLER);
assignUniforms("split0", "split1", "split2", "splitmax", "shadow_res",
"overlap_proportion", "box0", "box1", "box2", "box3", "sundirection", "sun_color");
"overlap_proportion", "penumbra0", "penumbra1", "penumbra2", "penumbra3",
"sundirection", "sun_color");
} // ShadowedSunLightShaderPCF
// ------------------------------------------------------------------------
void render(GLuint normal_depth_texture,
Expand All @@ -280,15 +285,20 @@ class ShadowedSunLightShaderPCSS : public TextureShader<ShadowedSunLightShaderPC
depth_stencil_texture,
shadow_framebuffer->getDepthTexture(),
shadow_framebuffer->getDepthTexture() );
drawFullScreenEffect(ShadowMatrices::m_shadow_split[1],
ShadowMatrices::m_shadow_split[2],
ShadowMatrices::m_shadow_split[3],
ShadowMatrices::m_shadow_split[4],
float(UserConfigParams::m_shadows_resolution),
ShadowMatrices::m_shadow_overlap_proportion,
shadow_box_extents[0], shadow_box_extents[1],
shadow_box_extents[2], shadow_box_extents[3],
direction, col);
std::array<core::vector2df, 4> penumbra;
for (int i = 0; i < 4; i++)
{
float size = tan(core::PI64 * 0.54 / 360.) * shadow_box_extents[i].Z;
penumbra[i] = core::vector2df(size / shadow_box_extents[i].X, size / shadow_box_extents[i].Y);
}
drawFullScreenEffect(ShadowMatrices::m_shadow_split[1],
ShadowMatrices::m_shadow_split[2],
ShadowMatrices::m_shadow_split[3],
ShadowMatrices::m_shadow_split[4],
float(UserConfigParams::m_shadows_resolution),
ShadowMatrices::m_shadow_overlap_proportion,
penumbra[0], penumbra[1], penumbra[2], penumbra[3],
direction, col);

} // render
}; // ShadowedSunLightShaderPCSS
Expand Down

0 comments on commit 50c5afa

Please sign in to comment.