Files
LNXSDK/leenkx/Shaders/ssao_pass/ssao_pass.frag.glsl

130 lines
4.2 KiB
Plaintext
Raw Normal View History

2025-01-22 16:18:30 +01:00
// Alchemy AO / Scalable Ambient Obscurance
#version 450
#include "compiled.inc"
#include "std/gbuffer.glsl"
uniform sampler2D gbufferD;
uniform sampler2D gbuffer0;
uniform vec2 cameraProj;
uniform vec3 eye;
uniform vec3 eyeLook;
uniform vec2 screenSize;
uniform mat4 invVP;
#ifdef _CPostprocess
uniform vec3 PPComp12;
#endif
in vec2 texCoord;
in vec3 viewRay;
out float fragColor;
void main() {
float depth = textureLod(gbufferD, texCoord, 0.0).r * 2.0 - 1.0;
if (depth == 1.0) { fragColor = 1.0; return; }
vec2 enc = textureLod(gbuffer0, texCoord, 0.0).rg;
vec3 n;
n.z = 1.0 - abs(enc.x) - abs(enc.y);
n.xy = n.z >= 0.0 ? enc.xy : octahedronWrap(enc.xy);
n = normalize(n);
vec3 vray = normalize(viewRay);
vec3 currentPos = getPosNoEye(eyeLook, vray, depth, cameraProj);
// vec3 currentPos = getPos2NoEye(eye, invVP, depth, texCoord);
float currentDistance = length(currentPos);
#ifdef _CPostprocess
float currentDistanceA = currentDistance * PPComp12.z * (1.0 / PPComp12.y);
#else
float currentDistanceA = currentDistance * ssaoScale * (1.0 / ssaoRadius);
#endif
float currentDistanceB = currentDistance * 0.0005;
ivec2 px = ivec2(texCoord * screenSize);
float phi = (3 * px.x ^ px.y + px.x * px.y) * 10;
fragColor = 0;
const int samples = 8;
const float samplesInv = PI2 * (1.0 / 8.0);
// Manually unrolled sampling loop for better performance on older hardware
// Sample 0
{
float theta = samplesInv * (0 + 0.5) + phi;
vec2 k = vec2(cos(theta), sin(theta)) / currentDistanceA;
depth = textureLod(gbufferD, texCoord + k, 0.0).r * 2.0 - 1.0;
vec3 pos = getPos2NoEye(eye, invVP, depth, texCoord + k) - currentPos;
fragColor += max(0, dot(pos, n) - currentDistanceB) / (dot(pos, pos) + 0.015);
}
// Sample 1
{
float theta = samplesInv * (1 + 0.5) + phi;
vec2 k = vec2(cos(theta), sin(theta)) / currentDistanceA;
depth = textureLod(gbufferD, texCoord + k, 0.0).r * 2.0 - 1.0;
vec3 pos = getPos2NoEye(eye, invVP, depth, texCoord + k) - currentPos;
fragColor += max(0, dot(pos, n) - currentDistanceB) / (dot(pos, pos) + 0.015);
}
// Sample 2
{
float theta = samplesInv * (2 + 0.5) + phi;
vec2 k = vec2(cos(theta), sin(theta)) / currentDistanceA;
depth = textureLod(gbufferD, texCoord + k, 0.0).r * 2.0 - 1.0;
vec3 pos = getPos2NoEye(eye, invVP, depth, texCoord + k) - currentPos;
fragColor += max(0, dot(pos, n) - currentDistanceB) / (dot(pos, pos) + 0.015);
}
// Sample 3
{
float theta = samplesInv * (3 + 0.5) + phi;
vec2 k = vec2(cos(theta), sin(theta)) / currentDistanceA;
depth = textureLod(gbufferD, texCoord + k, 0.0).r * 2.0 - 1.0;
vec3 pos = getPos2NoEye(eye, invVP, depth, texCoord + k) - currentPos;
fragColor += max(0, dot(pos, n) - currentDistanceB) / (dot(pos, pos) + 0.015);
}
// Sample 4
{
float theta = samplesInv * (4 + 0.5) + phi;
vec2 k = vec2(cos(theta), sin(theta)) / currentDistanceA;
depth = textureLod(gbufferD, texCoord + k, 0.0).r * 2.0 - 1.0;
vec3 pos = getPos2NoEye(eye, invVP, depth, texCoord + k) - currentPos;
fragColor += max(0, dot(pos, n) - currentDistanceB) / (dot(pos, pos) + 0.015);
}
// Sample 5
{
float theta = samplesInv * (5 + 0.5) + phi;
vec2 k = vec2(cos(theta), sin(theta)) / currentDistanceA;
depth = textureLod(gbufferD, texCoord + k, 0.0).r * 2.0 - 1.0;
vec3 pos = getPos2NoEye(eye, invVP, depth, texCoord + k) - currentPos;
fragColor += max(0, dot(pos, n) - currentDistanceB) / (dot(pos, pos) + 0.015);
}
// Sample 6
{
float theta = samplesInv * (6 + 0.5) + phi;
vec2 k = vec2(cos(theta), sin(theta)) / currentDistanceA;
depth = textureLod(gbufferD, texCoord + k, 0.0).r * 2.0 - 1.0;
vec3 pos = getPos2NoEye(eye, invVP, depth, texCoord + k) - currentPos;
fragColor += max(0, dot(pos, n) - currentDistanceB) / (dot(pos, pos) + 0.015);
}
// Sample 7
{
float theta = samplesInv * (7 + 0.5) + phi;
2025-01-22 16:18:30 +01:00
vec2 k = vec2(cos(theta), sin(theta)) / currentDistanceA;
depth = textureLod(gbufferD, texCoord + k, 0.0).r * 2.0 - 1.0;
vec3 pos = getPos2NoEye(eye, invVP, depth, texCoord + k) - currentPos;
fragColor += max(0, dot(pos, n) - currentDistanceB) / (dot(pos, pos) + 0.015);
}
#ifdef _CPostprocess
fragColor *= (PPComp12.x * 0.3) / samples;
#else
fragColor *= (ssaoStrength * 0.3) / samples;
#endif
fragColor = 1.0 - fragColor;
}