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 the rendering pipeline. I have grabbed some code for the shader as a starting point, but I don't think it's written in a way that best suites my needs. The output does not look right. I believe the shader code is written for world space ambient occlusion, while my renderer utilises deferred shading and will apply the SSAO in screen space. Can you look at the shader code and let me know whether this is likely the issue and, if so, make the necessary changes to the shader code? It is worth noting that there is a subsequent step that handles the blurring of the SSAO output, which seems to be working correctly.
Here is the shader code for the SSAO fragment shader:
#version 450
layout(location = 0) out vec4 FragColor;
layout(location = 0) in vec2 TexCoords;
layout(set = 0, binding = 0) uniform sampler2D gPosition; 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 { //vec3 samples[]; int kernelSize; float radius; float bias; vec2 noiseScale; } inputsUBO;
layout(binding = 5) uniform ProjBufferObject { mat4 proj; } projUBO;
void main() { // get input for SSAO algorithm vec3 fragPos = texture(gPosition, TexCoords).xyz; vec3 normal = normalize(texture(gNormal, TexCoords).rgb); vec3 randomVec = normalize(texture(texNoise, TexCoords * inputsUBO.noiseScale).xyz); // create TBN change-of-basis matrix: from tangent-space to view-space vec3 tangent = normalize(randomVec - normal * dot(randomVec, normal)); vec3 bitangent = cross(normal, tangent); mat3 TBN = mat3(tangent, bitangent, normal); // iterate over the sample kernel and calculate occlusion factor float occlusion = 0.0; for(int i = 0; i < inputsUBO.kernelSize; ++i) { // get sample position vec3 samplePos = TBN * samplesUBO.samples[i]; // from tangent to view-space samplePos = fragPos + samplePos * inputsUBO.radius;
// project sample position (to sample texture) (to get position on screen/texture) vec4 offset = vec4(samplePos, 1.0); offset = projUBO.proj * offset; // from view to clip-space offset.xyz /= offset.w; // perspective divide offset.xyz = offset.xyz * 0.5 + 0.5; // transform to range 0.0 - 1.0 // get sample depth float sampleDepth = texture(gPosition, offset.xy).z; // get depth value of kernel sample // range check & accumulate float rangeCheck = smoothstep(0.0, 1.0, inputsUBO.radius / abs(fragPos.z - sampleDepth)); occlusion += (sampleDepth >= samplePos.z + inputsUBO.bias ? 1.0 : 0.0) * rangeCheck; } occlusion = 1.0 - (occlusion / inputsUBO.kernelSize); FragColor = vec4(occlusion,occlusion,occlusion, 1.0);}
- Assistant
- User
I don't believe the modifications you have made have really changed anything. I am aware that the current fragment shader uses proj, or the projection from the MVP matrix, as part of it's transformations. I believe this indicates that the code expects the input images to be in view space and it is attempting to transform it to screen space. As the previous geometry stage in my deferred staging setup has already transformed the resulting images to screen space, I don't believe this is necessary. Can you take another look at the shader code and suggest any adjustments to make this work in screen space?
- Assistant
- User
It seems I am still getting some issues. The problem presents itself as though the SSAO is being duplicated in a tiled fashion across the screen. I believe that the input images are in screen space and that the shader is now operating in screen space. Now I am wondering whether there could be issues in the input UBOs to the shader. Below are the UBO inputs to the shader. Can you look at them to see if anything might be causing this duplicated tiling?
ssaoUBO.kernelSize = 64; ssaoUBO.bias = 0.025; ssaoUBO.noiseScale = glm::vec2(swapChainExtent.width / 4.0, swapChainExtent.height / 4.0); ssaoUBO.radius = 0.5f; - Assistant
- User
I have visually checked all of the vec3 variables in the shader before the For loop and they all seem to be behaving properly. I am now wondering whether there is an issue with sampling within the For loop. The only part I can see that might be causing this is the code to generate samplePos. Given I have checked all the input vec3s I wonder whether it could be related to the samplesUBO. Below is the section of code to generate the data for this UBO. Can you look at it and confirm it is correct? Note that the output data that feeds into the UBO is ssaoKernel and the ssaoSamples is the total number of samples, currently set to 64 in this case.
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); } - Assistant
- User
I believe I have fixed the duplication issue. It was an error with the use of the wrong texture sampler on some of the textures. I am now facing a new issue related to the fragPos. Does this texture need to be normalized similar to the other textures? I believe the range of values in this texture is causing issues with the ouput.
- Assistant
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.