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 SSAO into my rendering pipeline using the depth buffer as in input. So far I have managed to get something that looks like an SSAO output. However, it seems the output is still being impacted by the depth buffer. The occlusion at far distances effectively becomes black, while the occlusion close to the camera is muted. I have inverted the input of the depth image after linearization since I'm sure the buffer must be inverted. Without this adjustment the occlusion seems inverted so it seems like the correct approach. Do you have any idea what might be causing the depth weighting on the occlusion output? I have included the code for the SSAO fragment shader below for reference. Also as a note, the UBO inputs for the InputsBufferObject are as follows: kernelSize = 32, radius = 1.5, bias = 0.025. noiseScale = vec2(swapchain.width/ssaoNoiseSize, swapchain.height/ssaoNoiseSize)
#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;
// Function to get the view-space position from depth vec3 getViewPos(vec2 uv, float depth, mat4 projInv) { vec4 clipSpacePos = vec4(uv * 2.0 - 1.0, depth * 2.0 - 1.0, 1.0); // Convert to clip space vec4 viewSpacePos = projInv * clipSpacePos; // Transform to view space using inverse projection matrix return viewSpacePos.xyz / viewSpacePos.w; // Perspective division }
float LinearizeDepth(float depthValue, float nearPlane, float farPlane) { // Back-convert the depth to the non-linear value float z = depthValue * 2.0 - 1.0; // Transform from [0, 1] to [-1, 1] (NDC space)
// Reverse perspective projection to linear view space depth return (2.0 * nearPlane * farPlane) / (farPlane + nearPlane - z * (farPlane - nearPlane));}
void main() { float near = 0.1; float far = 100.0; float depth = texture(gDepth, TexCoords).r; // Fetch depth from depth buffer depth = 1-((LinearizeDepth(depth, near, far)-near)/(far-near)); vec3 normal = normalize(texture(gNormal, TexCoords).rgb); // Fetch and normalize normal vec3 viewPos = getViewPos(TexCoords, depth, projUBO.projInv);
// Sample a random vector for dithering vec3 randomVec = normalize(texture(texNoise, TexCoords * inputsUBO.noiseScale).xyz); vec3 tangent = normalize(randomVec - normal * dot(randomVec, normal)); vec3 bitangent = cross(normal, tangent); mat3 TBN = mat3(tangent, bitangent, normal); float adjustedRadius = inputsUBO.radius; float occlusion = 0.0; for (int i = 0; i < inputsUBO.kernelSize; ++i) { // Transform the sample vector to the view space vec3 samplePos = TBN * samplesUBO.samples[i]; samplePos = viewPos + samplePos * adjustedRadius; // Project the sample position vec4 offset = projUBO.proj * vec4(samplePos, 1.0); offset.xy /= offset.w; // Perspective division offset.xy = offset.xy * 0.5 + 0.5; // Transform to [0,1] // Fetch the depth of the sample position float sampleDepth = texture(gDepth, offset.xy).r; sampleDepth = 1-((LinearizeDepth(sampleDepth, near, far)-near)/(far-near)); float rangeCheck = smoothstep(0.0, 1.0, adjustedRadius / abs(viewPos.z - sampleDepth)); occlusion += (sampleDepth >= samplePos.z + inputsUBO.bias ? 1.0 : 0.0) * rangeCheck; } // Average occlusion value for the fragment occlusion = 1.0 - (occlusion / float(inputsUBO.kernelSize)); FragColor = vec4(vec3(occlusion), 1.0); // Output as greyscale - Assistant
- User
You mentioned a number of times that there may be issues with depth precision. I do not believe this would be the case since the depth buffer uses a 16bit floating point format, which should ensure sufficient precision.
In terms of kernel samples, here is the code used to generate the samples along with the code used to generate the inputs for the noise texture. Does this code require any modification to ensure correct normalization?
for (unsigned int i = 0; i < ssaoSamples; ++i) { glm::vec3 sample( randomFloats(generator) * 2.0 - 1.0, randomFloats(generator) * 2.0 - 1.0, randomFloats(generator) ); sample = glm::normalize(sample); sample *= randomFloats(generator); float scale = (float)i / ssaoSamples; scale = lerp(0.1f, 1.0f, scale * scale); sample *= scale; ssaoKernel.push_back(sample); } /*std::vector<glm::vec3> ssaoNoise;*/ for (unsigned int i = 0; i < pow(ssaoNoiseSize,2); i++) { glm::vec3 noise( randomFloats(generator) * 2.0 - 1.0, randomFloats(generator) * 2.0 - 1.0, 0.0f); ssaoNoise.push_back(noise); } - Assistant
- User
- Assistant
If you are observing unexpected ranges for your
viewPos, this could indeed impact the SSAO calculations, especially concerning the and sample position calculations. Let's review the logic for potential issues. - User
The projection matrices are being calculated as follows:
projectionUBO.proj = camera.GetProjectionMatrix(); projectionUBO.invproj = glm::inverse(camera.GetProjectionMatrix());The near and far values are hardcoded for simplicity, but align with the values used in the projection matrix.
Having followed your suggested debug steps, I can't find anything that would be causing this issue.
- Assistant
- User
The inverse projection matrix is as follows. I'm not completely sure, but something about this doesn't look right. Can you confirm this is a viable matrix for this kind of operation?
{0.55228, 0.00, 0.00, 0.00} {0.00, 0.41421, 0.00, 0.00} {0.00, 0.00, 0.00, -1.00} {0.00, 0.00, -4.995, 5.005}
- Assistant
- User
The calculation used to generate the projection matrix is as follows:
projectionMatrix = glm::perspective(glm::radians(45.0f), aspectRatio, 0.1f, 100.0f);
The aspect ratio is 800/600, based on the window size.
The calculation for the inverse matrix is:
projectionUBO.invproj = glm::inverse(camera.GetProjectionMatrix());
Based on these inputs, does the resulting matrices make sense?
- Assistant
- User
The resulting projection matrix looks like:
{1.81066, 0.00, 0.00, 0.00} {0.00, 2.41421, 0.00, 0.00} {0.00, 0.00, -1.002, -0.2002} {0.00, 0.00, -1.00, 0.00}
The resulting inverse matrix looks like:
{0.55228, 0.00, 0.00, 0.00} {0.00, 0.41421, 0.00, 0.00} {0.00, 0.00, 0.00, -1.00} {0.00, 0.00, -4.995, 5.005}
Based on the inputs and calculations, do these resulting matrices look correct?
- Assistant
- User
Based on this, it seems the inverse projection matrix should be largely correct as an input into SSAO. Would you agree?
- Assistant
Yes, based on the analysis and typical use of projection and inverse projection matrices, your inverse projection matrix seems appropriate for use in an SSAO context, provided that:
-
Correct Structure: The inverse projection matrix is structured to invert the operations of the projection matrix, particularly concerning depth transformations from clip space back to view space. Key elements like the inverse scaling of x and y axes and the recalibration of the depth components appear correctly managed.
-
The values in the inverse matrix indeed align with the expected mathematical operations, serving as a basis for converting screen space positions and normalized device coordinate (NDC) depths back into view space coordinates. Given that the matrices are consistent with typical inverse operations—as through GLM’s matrix functions—the inverse setup should be functional.
-
- User
Given the viewPos calculation in the SSAO fragment shader code produces a wider range of values compared to the depth and normal inputs, would there be a need to consider some kind of normalization of the viewPost value? Is this typical in an SSAO implementation?
- Assistant
- User
If the viewPos is meant to reflect the geometry of the scene, then validation of the values is relatively simple in that I can just compare it to the actual scene geometry image stored in the g-buffer. By comparing the two images it seems the viewPos is a decent approximation of the scene geometry. However, the depth information stored in the z channel of the viewPos seems to be inverted. Given I am inverting the depth buffer information in the SSAO shader, this may not be a problem. Do you think the inverted depth information in viewPos might be leading to depth weighted occlusion outputs?
- Assistant
- User
After extensive debugging, I now believe that the SSAO shader algorithm is working correctly and the issues I'm seeing may simply be related to the radius and bias value. What radius and bias values would you suggest for a SSAO implementation with a fov of 45, a near value of 0.1 and a far value of 100?
- Assistant