rename uniforms in SSAO shader

This commit is contained in:
James Seibel
2025-10-04 13:45:18 -05:00
parent 32b3eac589
commit b323b7e52d
3 changed files with 65 additions and 59 deletions
@@ -50,14 +50,14 @@ public class SSAOShader extends AbstractShaderRenderer
// uniforms
public int gProjUniform;
public int gInvProjUniform;
public int gSampleCountUniform;
public int gRadiusUniform;
public int gStrengthUniform;
public int gMinLightUniform;
public int gBiasUniform;
public int gDepthMapUniform;
public int uProj;
public int uInvProj;
public int uSampleCount;
public int uRadius;
public int uStrength;
public int uMinLight;
public int uBias;
public int uDepthMap;
@@ -73,14 +73,14 @@ public class SSAOShader extends AbstractShaderRenderer
);
// uniform setup
this.gProjUniform = this.shader.getUniformLocation("gProj");
this.gInvProjUniform = this.shader.getUniformLocation("gInvProj");
this.gSampleCountUniform = this.shader.getUniformLocation("gSampleCount");
this.gRadiusUniform = this.shader.getUniformLocation("gRadius");
this.gStrengthUniform = this.shader.getUniformLocation("gStrength");
this.gMinLightUniform = this.shader.getUniformLocation("gMinLight");
this.gBiasUniform = this.shader.getUniformLocation("gBias");
this.gDepthMapUniform = this.shader.getUniformLocation("gDepthMap");
this.uProj = this.shader.getUniformLocation("uProj");
this.uInvProj = this.shader.getUniformLocation("uInvProj");
this.uSampleCount = this.shader.getUniformLocation("uSampleCount");
this.uRadius = this.shader.getUniformLocation("uRadius");
this.uStrength = this.shader.getUniformLocation("uStrength");
this.uMinLight = this.shader.getUniformLocation("uMinLight");
this.uBias = this.shader.getUniformLocation("uBias");
this.uDepthMap = this.shader.getUniformLocation("uDepthMap");
}
@@ -100,28 +100,26 @@ public class SSAOShader extends AbstractShaderRenderer
@Override
protected void onApplyUniforms(float partialTicks)
{
this.shader.setUniform(this.gProjUniform, this.projection);
this.shader.setUniform(this.uProj, this.projection);
this.shader.setUniform(this.gInvProjUniform, this.invertedProjection);
this.shader.setUniform(this.uInvProj, this.invertedProjection);
this.shader.setUniform(this.gSampleCountUniform,
Config.Client.Advanced.Graphics.Ssao.sampleCount.get());
this.shader.setUniform(this.uSampleCount, Config.Client.Advanced.Graphics.Ssao.sampleCount.get());
// Implicit Number cast needs to be done to prevent issues with the default value being a int
// Explicit Number casts need to be done to prevent issues with the default value being an int
Number radius = Config.Client.Advanced.Graphics.Ssao.radius.get();
this.shader.setUniform(this.gRadiusUniform, radius.floatValue());
this.shader.setUniform(this.uRadius, radius.floatValue());
Number strength = Config.Client.Advanced.Graphics.Ssao.strength.get();
this.shader.setUniform(this.gStrengthUniform, strength.floatValue());
this.shader.setUniform(this.uStrength, strength.floatValue());
Number minLight = Config.Client.Advanced.Graphics.Ssao.minLight.get();
this.shader.setUniform(this.gMinLightUniform, minLight.floatValue());
this.shader.setUniform(this.uMinLight, minLight.floatValue());
Number bias = Config.Client.Advanced.Graphics.Ssao.bias.get();
this.shader.setUniform(this.gBiasUniform, bias.floatValue());
this.shader.setUniform(this.uBias, bias.floatValue());
GL32.glUniform1i(this.gDepthMapUniform, 0);
GL32.glUniform1i(this.uDepthMap, 0);
}
+34 -29
View File
@@ -9,14 +9,14 @@ in vec2 TexCoord;
out vec4 fragColor;
uniform sampler2D gDepthMap;
uniform int gSampleCount;
uniform float gRadius;
uniform float gStrength;
uniform float gMinLight;
uniform float gBias;
uniform mat4 gInvProj;
uniform mat4 gProj;
uniform sampler2D uDepthMap;
uniform int uSampleCount;
uniform float uRadius;
uniform float uStrength;
uniform float uMinLight;
uniform float uBias;
uniform mat4 uInvProj;
uniform mat4 uProj;
const float EPSILON = 1.e-6;
const float GOLDEN_ANGLE = 2.39996323;
@@ -25,32 +25,35 @@ const float PI = 3.1415926538;
const float TAU = PI * 2.0;
vec3 unproject(vec4 pos) {
vec3 unproject(vec4 pos)
{
return pos.xyz / pos.w;
}
float InterleavedGradientNoise(const in vec2 pixel) {
float InterleavedGradientNoise(const in vec2 pixel)
{
float x = dot(pixel, MAGIC.xy);
return fract(MAGIC.z * fract(x));
}
vec3 calcViewPosition(const in vec3 clipPos) {
vec4 viewPos = gInvProj * vec4(clipPos * 2.0 - 1.0, 1.0);
vec3 calcViewPosition(const in vec3 clipPos)
{
vec4 viewPos = uInvProj * vec4(clipPos * 2.0 - 1.0, 1.0);
return viewPos.xyz / viewPos.w;
}
float GetSpiralOcclusion(const in vec2 uv, const in vec3 viewPos, const in vec3 viewNormal) {
float GetSpiralOcclusion(const in vec2 uv, const in vec3 viewPos, const in vec3 viewNormal)
{
float dither = InterleavedGradientNoise(gl_FragCoord.xy);
float rotatePhase = dither * TAU;
float rStep = gRadius / gSampleCount;
float rStep = uRadius / uSampleCount;
vec2 offset;
float ao = 0.0;
int sampleCount = 0;
float radius = rStep;
for (int i = 0; i < clamp(gSampleCount, 1, SAMPLE_MAX); i++) {
for (int i = 0; i < clamp(uSampleCount, 1, SAMPLE_MAX); i++) {
vec2 offset = vec2(
sin(rotatePhase),
cos(rotatePhase)
@@ -60,45 +63,47 @@ float GetSpiralOcclusion(const in vec2 uv, const in vec3 viewPos, const in vec3
rotatePhase += GOLDEN_ANGLE;
vec3 sampleViewPos = viewPos + vec3(offset, -0.1);
vec3 sampleClipPos = unproject(gProj * vec4(sampleViewPos, 1.0)) * 0.5 + 0.5;
vec3 sampleClipPos = unproject(uProj * vec4(sampleViewPos, 1.0)) * 0.5 + 0.5;
sampleClipPos = saturate(sampleClipPos);
float sampleClipDepth = textureLod(gDepthMap, sampleClipPos.xy, 0.0).r;
float sampleClipDepth = textureLod(uDepthMap, sampleClipPos.xy, 0.0).r;
if (sampleClipDepth >= 1.0 - EPSILON) continue;
sampleClipPos.z = sampleClipDepth;
sampleViewPos = unproject(gInvProj * vec4(sampleClipPos * 2.0 - 1.0, 1.0));
sampleViewPos = unproject(uInvProj * vec4(sampleClipPos * 2.0 - 1.0, 1.0));
vec3 diff = sampleViewPos - viewPos;
float sampleDist = length(diff);
vec3 sampleNormal = diff / sampleDist;
float sampleNoLm = max(dot(viewNormal, sampleNormal) - gBias, 0.0);
float aoF = 1.0 - saturate(sampleDist / gRadius);
float sampleNoLm = max(dot(viewNormal, sampleNormal) - uBias, 0.0);
float aoF = 1.0 - saturate(sampleDist / uRadius);
ao += sampleNoLm * aoF;
sampleCount++;
}
ao /= max(sampleCount, 1);
ao = smoothstep(0.0, gStrength, ao);
ao = smoothstep(0.0, uStrength, ao);
return ao * (1.0 - gMinLight);
return ao * (1.0 - uMinLight);
}
void main() {
float fragmentDepth = textureLod(gDepthMap, TexCoord, 0).r;
void main()
{
float fragmentDepth = textureLod(uDepthMap, TexCoord, 0).r;
float occlusion = 0.0;
// Do not apply to sky
if (fragmentDepth < 1.0) {
if (fragmentDepth < 1.0)
{
vec3 viewPos = calcViewPosition(vec3(TexCoord, fragmentDepth));
#ifdef GL_ARB_derivative_control
// Get higher precision derivatives when available
vec3 viewNormal = cross(dFdxFine(viewPos.xyz), dFdyFine(viewPos.xyz));
// Get higher precision derivatives when available
vec3 viewNormal = cross(dFdxFine(viewPos.xyz), dFdyFine(viewPos.xyz));
#else
vec3 viewNormal = cross(dFdx(viewPos.xyz), dFdy(viewPos.xyz));
vec3 viewNormal = cross(dFdx(viewPos.xyz), dFdy(viewPos.xyz));
#endif
viewNormal = normalize(viewNormal);
@@ -63,12 +63,15 @@ void main()
// a fragment depth of "1" means the fragment wasn't drawn to,
// we only want to apply SSAO to LODs, not to the sky outside the LODs
if (fragmentDepth < 1) {
if (gBlurRadius > 0) {
if (fragmentDepth < 1)
{
if (gBlurRadius > 0)
{
float fragmentDepthLinear = linearizeDepth(fragmentDepth);
fragColor.a = BilateralGaussianBlur(TexCoord, fragmentDepthLinear, 1.6);
}
else {
else
{
fragColor.a = texelFetch(gSSAOMap, ivec2(gl_FragCoord.xy), 0).r;
}
}