So, I'm implementing SSAO as part of my rendering pipeline using OpenGL/GLSL. It works pretty well when I have a camera that takes up the entire screen. However, when my camera is smaller than the full screen size, the SSAO texture doesn't get sampled correctly. Here is the relevant GLSL shader code:

// Convert from clip-space
vec2 fragCoords = (fragPos.xy/ fragPos.w); // fragPos is MVP * worldPosition
vec2 screenCoords = fragCoords * 0.5 + 0.5; // Convert from [-1, 1] to [0, 1] to sample UV coordinates

// Sample texture
float ssaoFactor = texture2D(ssaoTexture, screenCoords).r;

I know that there is some funkiness going on with the viewport, but the fixes that I've tried haven't worked. My first thought was to scale fragCoords by normalized size of my viewport (e.g. vec2(0.5, 0.5) for a viewport with half of the width and height of the screen), but that just produced a very strange result. Any thoughts?

  • \$\begingroup\$ Why do Camera and Screen sizes mismatch in your case? Typically they are the same and your SSAO/Deferred buffers sizes match Camera size as well. \$\endgroup\$
    – Kromster
    Nov 13, 2020 at 7:18
  • \$\begingroup\$ I've already solved this, but that's because I have multiple cameras with various screen sizes. \$\endgroup\$
    – Danny
    Nov 15, 2020 at 0:48

1 Answer 1


Figured this out a while ago. My shader math was fine, but I wasn't setting my viewport size correctly before drawing into my buffer.


You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .