diff --git a/core/src/main/resources/assets/distanthorizons/shaders/ssao/apply.fsh b/core/src/main/resources/assets/distanthorizons/shaders/ssao/apply.fsh index 00a404cda..ecc9ba828 100644 --- a/core/src/main/resources/assets/distanthorizons/shaders/ssao/apply.fsh +++ b/core/src/main/resources/assets/distanthorizons/shaders/ssao/apply.fsh @@ -7,39 +7,43 @@ out vec4 fragColor; uniform sampler2D uSourceColorTexture; uniform sampler2D uSourceDepthTexture; -uniform vec2 gViewSize; -uniform int gBlurRadius; -uniform float gNear; -uniform float gFar; +layout (std140) uniform applyFragUniformBlock +{ + vec2 uViewSize; + int uBlurRadius; + float uNearClipPlane; // in blocks + float uFarClipPlane; // in blocks +}; -float linearizeDepth(const in float depth) { - return (gNear * gFar) / (depth * (gNear - gFar) + gFar); -} +float linearizeDepth(const in float depth) { return (uNearClipPlane * uFarClipPlane) / (depth * (uNearClipPlane - uFarClipPlane) + uFarClipPlane); } -float Gaussian(const in float sigma, const in float x) { - return exp(-(x*x) / (2.0 * (sigma*sigma))); -} +float Gaussian(const in float sigma, const in float x) { return exp(-(x*x) / (2.0 * (sigma*sigma))); } -float BilateralGaussianBlur(const in vec2 texcoord, const in float linearDepth, const in float g_sigmaV) { +float BilateralGaussianBlur(const in vec2 texcoord, const in float linearDepth, const in float g_sigmaV) +{ float g_sigmaX = 1.6; float g_sigmaY = 1.6; - int radius = clamp(gBlurRadius, 1, 3); + int radius = clamp(uBlurRadius, 1, 3); - vec2 pixelSize = 1.0 / gViewSize; + vec2 pixelSize = 1.0 / uViewSize; float accum = 0.0; float total = 0.0; - for (int iy = -radius; iy <= radius; iy++) { + for (int iy = -radius; iy <= radius; iy++) + { float fy = Gaussian(g_sigmaY, iy); - for (int ix = -radius; ix <= radius; ix++) { + for (int ix = -radius; ix <= radius; ix++) + { float fx = Gaussian(g_sigmaX, ix); - vec2 sampleTex = texcoord + ivec2(ix, iy) * pixelSize; - float sampleValue = textureLod(uSourceColorTexture, sampleTex, 0).r; - float sampleDepth = textureLod(uSourceDepthTexture, sampleTex, 0).r; + vec2 sampleTexCoord = texcoord + ivec2(ix, iy) * pixelSize; + + float sampleValue = textureLod(uSourceColorTexture, sampleTexCoord, 0).r; + + float sampleDepth = textureLod(uSourceDepthTexture, sampleTexCoord, 0).r; float sampleLinearDepth = linearizeDepth(sampleDepth); float depthDiff = abs(sampleLinearDepth - linearDepth); @@ -51,7 +55,10 @@ float BilateralGaussianBlur(const in vec2 texcoord, const in float linearDepth, } } - if (total <= 1.e-4) return 1.0; + if (total <= 1.e-4) + { + return 1.0; + } return accum / total; } @@ -66,7 +73,7 @@ void main() // we only want to apply SSAO to LODs, not to the sky outside the LODs if (fragmentDepth < 1) { - if (gBlurRadius > 0) + if (uBlurRadius > 0) { float fragmentDepthLinear = linearizeDepth(fragmentDepth); fragColor.a = BilateralGaussianBlur(TexCoord, fragmentDepthLinear, 1.6);