Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

New SSAO Algorithm #62

Open
wants to merge 19 commits into
base: rend2
Choose a base branch
from
Open
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Removed unnecessary complications
Monsterovich committed May 27, 2018
commit 6ad9c1fb36b19924db3a391211f06f878ab73a86
26 changes: 10 additions & 16 deletions codemp/rd-rend2/glsl/ssao.glsl
Original file line number Diff line number Diff line change
@@ -13,7 +13,7 @@ void main()
/*[Fragment]*/
uniform sampler2D u_ScreenDepthMap;
uniform sampler2D u_ScreenImageMap;
uniform vec4 u_ViewInfo; // zfar / znear, zfar, 0, 0
uniform vec4 u_ViewInfo; // znear, zfar, 0, 0
uniform vec2 u_ScreenInfo; // width, height

in vec2 var_ScreenTex;
@@ -24,25 +24,19 @@ out vec4 out_Color;
// AO Shader by Monsterovich :D
//

float zNear = 1.0 / (u_ViewInfo.x / u_ViewInfo.y);
float zFar = u_ViewInfo.y;
vec2 camerarange = vec2(u_ViewInfo.x, u_ViewInfo.y);
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This seems unnecessary

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Removed.


vec2 camerarange = vec2(zNear, zFar);
vec2 screensize = u_ScreenInfo;
vec2 texCoord = var_ScreenTex;


float readDepth( in vec2 coord ) {
Copy link
Owner

@xycaleth xycaleth Jun 2, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can znear and zfar values be passed in as function parameters? I'd like to avoid using global variables unless it's necessary.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done.

return (2.0 * camerarange.x) / (camerarange.y + camerarange.x - texture2D( u_ScreenDepthMap, coord ).x * (camerarange.y - camerarange.x));
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

All the GLSL shaders in rend2 are compiled with version "GLSL 1.50 Core". This means the texture2D function is unavailable - I tried on my laptop and this shader fails to compile.

texture2D should be replaced with texture instead.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done.

}

void main(void)
{
float depth = readDepth( texCoord );
float depth = readDepth( var_ScreenTex );
float d;

float pw = 1.0 / screensize.x;
float ph = 1.0 / screensize.y;
float pw = 1.0 / u_ScreenInfo.x;
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think you can avoid having pw, ph and u_ScreenInfo by using texelFetch and gl_FragCoord. This lets you read from the texture using texel positions (e.g. 40, 100)

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think you need pw and ph. If you use texelFetch to read from the texture, you can use texel coordinates:

ivec2 screenCoord = ivec2(g_FragCoord.xy);
vec4 d;
d = texelFetch(u_ScreenDepthMap, screenCoord, 0).x;
d = texelFtech(u_ScreenDepthMap, ivec2(screenCord.x, screenCoord.y + 1, 0).x;
etc..

If you really want to continue using texture, then u_ScreenInfo should contain (1.0 / w, 1.0 / h) instead of doing it in the shader.

float ph = 1.0 / u_ScreenInfo.y;

float aoCap = 1.0;
float ao = 0.0;
@@ -52,16 +46,16 @@ void main(void)
int i;
for (i = 0; i < 4; i++)
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Only sampling depths sideways and up/down doesn't look very good - it almost looks kind of pixelated. I think this needs to be changed to sampling in directions all around the currently pixel, not just left/up/down/right.

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is an awful lot of samples now... you're doing 64 samples now. Can you reduce this down to 16 again (like you had before)?

{
d = readDepth( vec2(texCoord.x+pw,texCoord.y+ph));
d = readDepth( vec2(var_ScreenTex.x+pw,var_ScreenTex.y+ph));
ao += min(aoCap,max(0.0,depth-d-depthTolerance) * aoMultiplier);

d = readDepth( vec2(texCoord.x-pw,texCoord.y+ph));
d = readDepth( vec2(var_ScreenTex.x-pw,var_ScreenTex.y+ph));
ao += min(aoCap,max(0.0,depth-d-depthTolerance) * aoMultiplier);

d = readDepth( vec2(texCoord.x+pw,texCoord.y-ph));
d = readDepth( vec2(var_ScreenTex.x+pw,var_ScreenTex.y-ph));
ao += min(aoCap,max(0.0,depth-d-depthTolerance) * aoMultiplier);

d = readDepth( vec2(texCoord.x-pw,texCoord.y-ph));
d = readDepth( vec2(var_ScreenTex.x-pw,var_ScreenTex.y-ph));
ao += min(aoCap,max(0.0,depth-d-depthTolerance) * aoMultiplier);

pw *= 2.0;
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

By multiplying by 2 you're skipping depth samples in later loops... is that intentional? i.e. you read left/right/up/down going from ±1, ±2, ±4, ±8...

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why are you multiplying pw and ph by 2?

@@ -71,7 +65,7 @@ void main(void)

ao /= 16.0;

float orig = texture2D(u_ScreenImageMap,texCoord).x;
float orig = texture2D(u_ScreenImageMap,var_ScreenTex).x;
float done = (1.0 - ao) * orig;
out_Color = vec4(done, done, done, 0.0);
}
2 changes: 1 addition & 1 deletion codemp/rd-rend2/tr_backend.cpp
Original file line number Diff line number Diff line change
@@ -2001,7 +2001,7 @@ static void RB_RenderSSAO()
{
const float zmax = backEnd.viewParms.zFar;
const float zmin = r_znear->value;
const vec4_t viewInfo = { zmax / zmin, zmax, 0.0f, 0.0f };
const vec4_t viewInfo = { zmin, zmax, 0.0f, 0.0f };


FBO_Bind(tr.quarterFbo[0]);