I am building a 3d renderer in C++ using Vulkan.hpp. I am in the process of implementing…
- User
I am building a 3d renderer in C++ using Vulkan.hpp. I am in the process of implementing a SSAO stage into the rendering pipeline. I have managed to set everything up and have a working SSAO stage. However, I just want someone to check the following code for the SSAO fragment shader and verify everything looks right.
Inputs to the InputsBufferObject:
kernelSize = 64 radius = 0.5 bias = 0.025 noiseScale = swapChainImage / 8
SSAO fragment shader code:
#version 450
layout(location = 0) out vec4 FragColor;
layout(location = 0) in vec2 TexCoords;
layout(set = 0, binding = 0) uniform sampler2D gDepth; layout(set = 0, binding = 1) uniform sampler2D gNormal; layout(set = 0, binding = 2) uniform sampler2D texNoise;
layout(binding = 3) buffer SamplesBufferObject { vec3 samples[]; } samplesUBO;
layout(binding = 4) uniform InputsBufferObject { int kernelSize; float radius; float bias; vec2 noiseScale; } inputsUBO;
layout(binding = 5) uniform ProjBufferObject { mat4 proj; mat4 projInv; } projUBO;
vec3 reconstructVSPosFromDepth(vec2 uv) { float nearPlane = 0.1; float farPlane = 100.0; float depth = texture(gDepth, uv).r; float linearSampleDepth = (2.0 * nearPlane) / (farPlane + nearPlane - depth * (farPlane - nearPlane)); linearSampleDepth = normalize(linearSampleDepth); float x = uv.x * 2.0f - 1.0f; float y = (1.0f - uv.y) * 2.0f - 1.0f; vec4 pos = vec4(x, y, linearSampleDepth, 1.0f); vec4 posVS = projUBO.projInv * pos; vec3 posNDC = posVS.xyz / posVS.w; return posNDC; }
void main() { float nearPlane = 0.1; float farPlane = 100.0;
float depth = texture(gDepth, TexCoords).r; vec3 posVS = reconstructVSPosFromDepth(TexCoords); vec3 normal = normalize(texture(gNormal, TexCoords).rgb); vec3 randomVec = normalize(texture(texNoise, TexCoords * inputsUBO.noiseScale).xyz); vec3 tangent = normalize(randomVec - normal * dot(randomVec, normal)); vec3 bitangent = cross(tangent, normal); mat3 TBN = mat3(tangent, bitangent, normal); float occlusion = 0.0f; int sampleCount = 0; for (uint i = 0; i < inputsUBO.kernelSize; i++) { vec3 samplePos = TBN * samplesUBO.samples[i].xyz; samplePos = posVS + samplePos * inputsUBO.radius; vec4 offset = vec4(samplePos, 1.0f); offset = projUBO.proj * offset; offset.xyz /= offset.w; offset.xyz = offset.xyz * 0.5f + 0.5f; //offset.y = 1.0f - offset.y; vec3 reconstructedPos = reconstructVSPosFromDepth(offset.xy); // range check & accumulate float rangeCheck = smoothstep(0.0, 1.0, inputsUBO.radius / abs(posVS.z - reconstructedPos.z)); occlusion += (reconstructedPos.z >= samplePos.z + inputsUBO.bias ? 1.0 : 0.0) * rangeCheck; } occlusion = (occlusion / inputsUBO.kernelSize); FragColor = vec4(occlusion,occlusion,occlusion,1.0);}
- Assistant
Your SSAO fragment shader code generally looks well-structured and implements the typical steps for Screen-Space Ambient Occlusion. However, there are a few points and considerations to improve or clarify aspects of your implementation:
1. Depth Re-Mapping
In your
reconstructVSPosFromDepthfunction, you have the following line:linearSampleDepth = normalize(linearSampleDepth);This normalization isn't necessary and can lead to unexpected results. It should be:
linearSampleDepth = linearSampleDepth; // or just remove the line2. TBN Matrix Construction
The way you construct your TBN matrix should be double-checked:
vec3 tangent = normalize(randomVec - normal * dot(randomVec, normal)); vec3 bitangent = cross(tangent, normal); mat3 TBN = mat3(tangent, bitangent, normal);Ensure that
randomVecis a perturbed vector properly adjusted based on the normal. If you're generatingrandomVecfrom noise, that's good, but ensure it is still constrained within the surface's tangent space.3. Sampling Loop
In your sampling loop, you're directly accessing
samplesUBO.samples[i].xyz, which should be fine if you have populated thesamplesarray correctly with sample vectors in tangent space. Just make sure they are normalized and uniformly spaced, which is usually needed for SSAO samples.4.
samplePosandreconstructedPosCalculationIn the line:
vec3 reconstructedPos = reconstructVSPosFromDepth(offset.xy);You should verify that
offset.xyis a properly normalized coordinate. You’ve commented out the line that inverts the y-coordinate, which suggests you have considered this, but be aware of your viewport's coordinate system.5. Use of Bias
You're using a bias for determining occlusion. It's a common practice to avoid self-occlusion artifacts, but be cautious with the value you use as it can lead to incorrect occlusion if too high.
6. Range Checking
The
rangeCheckcalculation might be prone to artifacts if yourposVS.zvalues vary too much across the screen. You could consider a more sophisticated method of handling distance comparisons by potentially adapting the range based on scene depth.7. Output Color
The final output color:
FragColor = vec4(occlusion, occlusion, occlusion, 1.0);This will grayscale the output based on occlusion, which is typical for SSAO. You might want to consider multiplying
occlusionby an intensity factor to control the visibility of the effect.8. Performance Considerations
Depending on the target platform, running a loop up to
kernelSizecould be expensive. Consider optimizing this by:- Using a lower kernel size.
- Potentially using a different noise function for deciding sample positions.
Conclusion
Your shader looks fundamentally sound for SSAO, but by reviewing the points above, you can tweak and optimize the implementation further for better quality and performance. Always visualize debug values to ensure each step produces the expected output!