Jarvis, Optimise my shit
This commit is contained in:
parent
d1abde919d
commit
9424b29d0a
50 changed files with 1469 additions and 1232 deletions
|
|
@ -2,7 +2,6 @@
|
|||
|
||||
layout(location = 0) in vec2 inTextCoord;
|
||||
layout(location = 0) out float outAO;
|
||||
layout(location = 1) out vec4 viewPos;
|
||||
|
||||
layout(set = 0, binding = 0) uniform sampler2D posSampler;
|
||||
layout(set = 0, binding = 1) uniform sampler2D normalSampler;
|
||||
|
|
@ -19,65 +18,52 @@ layout(set = 2, binding = 0) uniform SSAOInfo {
|
|||
float radius; //140
|
||||
float bias; //144
|
||||
int kernelSize; //148
|
||||
float ResolutionScale; //152;
|
||||
vec2 Padding; //160;
|
||||
} ssao;
|
||||
|
||||
void main() {
|
||||
|
||||
vec2 ScreenSize = ssao.screenSize * ssao.ResolutionScale;
|
||||
|
||||
int KERNEL_SIZE = ssao.kernelSize;
|
||||
int KERNEL_SIZE = clamp(ssao.kernelSize, 1, 64);
|
||||
vec3 fragPos = texture(posSampler, inTextCoord).xyz;
|
||||
vec3 worldNorm = normalize(texture(normalSampler, inTextCoord).xyz);
|
||||
vec3 worldNorm = texture(normalSampler, inTextCoord).xyz;
|
||||
|
||||
if (dot(worldNorm, worldNorm) < 0.001) {
|
||||
if (dot(worldNorm, worldNorm) < 0.001 || fragPos.z >= 0.0) {
|
||||
outAO = 1.0;
|
||||
return;
|
||||
}
|
||||
|
||||
vec3 normal = normalize(mat3(ssao.view) * worldNorm);
|
||||
|
||||
viewPos = vec4(fragPos, 1);
|
||||
|
||||
vec2 noiseScale = ScreenSize / 4.0;
|
||||
vec3 randomVec = texture(noiseSampler, inTextCoord * noiseScale).xyz;
|
||||
vec3 tangent = normalize(randomVec - normal * dot(randomVec, normal));
|
||||
vec3 randomVec = texelFetch(noiseSampler, ivec2(gl_FragCoord.xy) % 4, 0).xyz;
|
||||
vec3 tangent = randomVec - normal * dot(randomVec, normal);
|
||||
if (dot(tangent, tangent) < 0.0001) {
|
||||
tangent = cross(normal, abs(normal.z) < 0.9 ? vec3(0, 0, 1) : vec3(0, 1, 0));
|
||||
}
|
||||
tangent = normalize(tangent);
|
||||
vec3 bitangent = cross(normal, tangent);
|
||||
mat3 TBN = mat3(tangent, bitangent, normal);
|
||||
float fragDepth = -fragPos.z;
|
||||
|
||||
float minOptimalDistance = 15;
|
||||
float maxKernelSize = float(ssao.kernelSize);
|
||||
|
||||
// Calculate a dynamic loop limit based on proximity
|
||||
// If closer than 1.5 units, smoothly scale down the loop count
|
||||
int dynamicKernelSize = int(KERNEL_SIZE);
|
||||
if (fragDepth < minOptimalDistance) {
|
||||
float proximityFactor = clamp(fragDepth / minOptimalDistance, 0.2, 1.0);
|
||||
dynamicKernelSize = int(maxKernelSize * proximityFactor);
|
||||
}
|
||||
|
||||
// Force a hard minimum baseline of samples so SSAO doesn't completely disappear
|
||||
dynamicKernelSize = max(dynamicKernelSize, 16);
|
||||
|
||||
float occlusion = 0.0;
|
||||
|
||||
float Passes = 0.0f;
|
||||
|
||||
for (int i = 0; i < dynamicKernelSize; i++) {
|
||||
vec3 samplePos = fragPos + (TBN * kernel.samples[i].xyz) * ssao.radius;
|
||||
for (int i = 0; i < KERNEL_SIZE; i++) {
|
||||
// Cover the complete radius distribution, even with a smaller sample budget.
|
||||
int sampleIndex = i * 64 / KERNEL_SIZE;
|
||||
vec3 samplePos = fragPos + (TBN * kernel.samples[sampleIndex].xyz) * ssao.radius;
|
||||
vec4 offset = ssao.projection * vec4(samplePos, 1.0);
|
||||
if (offset.w <= 0.0) continue;
|
||||
offset.xyz /= offset.w;
|
||||
offset.xyz = offset.xyz * 0.5 + 0.5;
|
||||
vec2 sampleUV = clamp(vec2(offset.x, 1.0 - offset.y), 0.0, 1.0);
|
||||
vec2 sampleUV = vec2(offset.x, 1.0 - offset.y);
|
||||
if (any(lessThan(sampleUV, vec2(0))) || any(greaterThan(sampleUV, vec2(1)))) continue;
|
||||
vec3 sampleViewPos = texture(posSampler, sampleUV).xyz;
|
||||
if (sampleViewPos.z >= 0.0) continue;
|
||||
float sampleDepth = -sampleViewPos.z;
|
||||
float depthDelta = abs(fragDepth - sampleDepth);
|
||||
if (depthDelta > ssao.radius * 2.0) {
|
||||
continue;
|
||||
}
|
||||
float rangeCheck = smoothstep(0.0, 1.0, ssao.radius / depthDelta);
|
||||
float rangeCheck = smoothstep(0.0, 1.0, ssao.radius / max(depthDelta, 0.0001));
|
||||
float isOccluded = step(samplePos.z + ssao.bias, sampleViewPos.z);
|
||||
occlusion += isOccluded * rangeCheck;
|
||||
Passes += 1.0;
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue