Jump to content

  • Log In with Google      Sign In   
  • Create Account


SSAO Problem


Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

  • You cannot reply to this topic
6 replies to this topic

#1 CodeLord   Members   -  Reputation: 126

Like
0Likes
Like

Posted 07 August 2011 - 04:02 PM

I am trying to integrate the SSAO technique by Vladimir Kajalin described in ShaderX 7 into my game engine. My implementation seems to be working but I noticed there are some weird projective artifacts which is always visible no matter what kind of geometry is being rendered. Here I increased the contrast to make it more visible. Can somebody look at my code and tell me what I'm doing wrong.

Code:
[source lang="cpp"]const float3 xyz[8] = { float3(-0.5774, -0.5774, -0.5774), float3(-0.5774, -0.5774, 0.5774), float3(-0.5774, 0.5774, -0.5774), float3(-0.5774, 0.5774, 0.5774), float3( 0.5774, -0.5774, -0.5774), float3( 0.5774, -0.5774, 0.5774), float3( 0.5774, 0.5774, -0.5774), float3( 0.5774, 0.5774, 0.5774),};Texture2D<float3> gNoiseVecMap;struct VS_IN{ float3 pos : POSITION;};struct VS_OUT{ float4 pos : SV_POSITION;};float GetDepth(const float2 uv) { const float z = LoadTexture(gDepthMap, uv, 0); return z;}VS_OUT VS(VS_IN vIn){ VS_OUT vOut; vOut.pos = float4(vIn.pos, 1.0f); return vOut;}float PS(VS_OUT pIn) : SV_Target{ float2 rotationTC = pIn.pos.xy/4; float3 vRotation = gNoiseVecMap.Sample(gPointSam, rotationTC); float h = 1/(1+vRotation.z); float3x3 rotMat = float3x3( h*vRotation.y*vRotation.y + vRotation.z, -h*vRotation.y*vRotation.x, vRotation.x, -h*vRotation.y*vRotation.x, h*vRotation.x*vRotation.x + vRotation.z, vRotation.y, -vRotation.x, -vRotation.y, vRotation.z); float fSceneDepthP = GetDepth(pIn.pos.xy); const int nSamplesNum = 24; const float offsetScaleStep = 1 + 2.4 / nSamplesNum; float offsetScale = 0.01; float Accessability = 0; for(int i=0; i<nSamplesNum; ++i) { float3 vOffset = xyz[i%8]*(offsetScale*=offsetScaleStep); float3 vRotatedOffset = mul(vOffset, rotMat); float3 vRandVector = float3(vRotatedOffset.xy*gProjParams.screenDimension, vRotatedOffset.z*fSceneDepthP*2); float3 vSamplePos = float3(pIn.pos.xy, fSceneDepthP) + vRandVector; vSamplePos.xy = clamp(vSamplePos.xy, float2(0, 0), gProjParams.screenDimension-1); float fSceneDepthS = GetDepth(vSamplePos.xy); float fRangeIsInvalid = saturate(((fSceneDepthP-fSceneDepthS)/fSceneDepthS)); Accessability += lerp(fSceneDepthS > vSamplePos.z, 0.5, fRangeIsInvalid); } Accessability = Accessability / nSamplesNum; return saturate(Accessability*(Accessability+1));}technique11 Standard{ pass P0 { SetVertexShader( CompileShader( vs_5_0, VS() ) ); SetGeometryShader( NULL ); SetPixelShader( CompileShader( ps_5_0, PS() ) ); }}[/source]

screenDimension is size of the back buffer (1920x1200 here for example), and gDepthMap is set to a linear depth buffer which is filled with the length of view space position of pixels.

Attached Thumbnails

  • ao_problem.jpg


Sponsor:

#2 johnchapman   Members   -  Reputation: 532

Like
1Likes
Like

Posted 08 August 2011 - 08:26 AM

Looking at the screenshot i'd say it's a combination of a large sample kernel radius with a low number of samples; does decreasing the kernel radius/increasing the number of samples improve the problem?

The black "edges" are a result of sampling beyond the edges of the depth texture. The result of this will change depending on the texture wrap mode (repeat, clamp to edge, etc.) or you could try clamping your texture coordinated in [0, 1].

As a side note, you might be interested in a tutorial I wrote a while back that extends the SSAO technique you're implementing: http://www.john-chapman.net/content.php?id=8

#3 CodeLord   Members   -  Reputation: 126

Like
0Likes
Like

Posted 09 August 2011 - 05:01 PM

Hi John, I have tried both changing the kernel radius by decreasing the offset and step size but it didn't do any good, increasing the sample count also doesn't reduce the artifacts. My guess is I'm not retrieving the depth accurately.

The edges are not a big issue, the problem for me is those weird looking triangular artifacts in the middle of the screen.

Thanks for the tutorial link, I will look into that.

Looking at the screenshot i'd say it's a combination of a large sample kernel radius with a low number of samples; does decreasing the kernel radius/increasing the number of samples improve the problem?

The black "edges" are a result of sampling beyond the edges of the depth texture. The result of this will change depending on the texture wrap mode (repeat, clamp to edge, etc.) or you could try clamping your texture coordinated in [0, 1].

As a side note, you might be interested in a tutorial I wrote a while back that extends the SSAO technique you're implementing: http://www.john-chap...ontent.php?id=8



#4 maxest   Members   -  Reputation: 294

Like
1Likes
Like

Posted 09 August 2011 - 07:30 PM

My simple advice is to visualize each input data to your SSAO separately. Some of it should also exhibit this weird triangular pattern. Visualize kernel vectors, depths, whatever you use for SSAO computation.

#5 johnchapman   Members   -  Reputation: 532

Like
1Likes
Like

Posted 10 August 2011 - 06:08 AM

How are you actually generating the sample kernel? As maxest says if there's any regularity to it that is what will be causing the interference pattern (and why increasing the number samples doesn't seem to reduce the effect).

#6 CodeLord   Members   -  Reputation: 126

Like
0Likes
Like

Posted 10 August 2011 - 03:18 PM

How are you actually generating the sample kernel? As maxest says if there's any regularity to it that is what will be causing the interference pattern (and why increasing the number samples doesn't seem to reduce the effect).


I have to correct my answer, increasing the number of samples does reduce the artifacts slightly, but makes it super slow to run as well. I think 16 was the recommended value by the developer of the algorithm, and I'm using 24.

This is how I generate the random vectors: I create a 4x4 random noise vector:
[source language=cpp] int ncomp = 3; int w = 4, h = 4; int length = w * h; float *data = new float[length*ncomp]; for(int i=0; i<length; ++i) { Vector3 v(frand(), frand(), frand()); v.normalize(); data[i*ncomp] = v.x; data[i*ncomp+1] = v.y; data[i*ncomp+2] = v.z; } Texture* pTexture = Texture::create(SurfaceDesc(DXGI_FORMAT_R32G32B32_FLOAT, w, h, 1, 0), 12, data); [/source]

and then pass to the shader as gNoiseVecMap. I sample from the noise texture:
float2 rotationTC = pIn.pos.xy/4;
float3 vRotation = gNoiseVecMap.Sample(gPointSam, rotationTC);


Then I use this vector to create a rotation matrix for each pixel:
float h = 1/(1+vRotation.z);

float3x3 rotMat = float3x3(	h*vRotation.y*vRotation.y + vRotation.z,	-h*vRotation.y*vRotation.x,					vRotation.x, 
						-h*vRotation.y*vRotation.x,					h*vRotation.x*vRotation.x + vRotation.z,	vRotation.y,
						-vRotation.x,								-vRotation.y,								vRotation.z);

The rotation matrix is then used to rotate a set of vectors with various lengths to create pseudo random vectors, and then are transformed to screen space.

float3 vOffset = xyz[i%8]*(offsetScale*=offsetScaleStep);
float3 vRotatedOffset = mul(vOffset, rotMat);
float3 vRandVector = float3(vRotatedOffset.xy*gProjParams.screenDimension, vRotatedOffset.z*fSceneDepthP*2);


#7 johnchapman   Members   -  Reputation: 532

Like
1Likes
Like

Posted 11 August 2011 - 12:30 AM

The issue stems from the fact that your sampling kernel is basically just the vertices of a cube. Even with semi-random scaling + random rotation, it still retains a regular appearance:
bad_ssao_kernel.jpg

You can see where the interference pattern is coming from.

Instead I'd generate a sample kernel on the CPU where you have more control and can more easilly visualise the kernel seperately.

If you take a look at the tutorial I linked to there's an example of generating a decent kernel (it's for a hemispherical sampling kernel, but you can easilly adapt it to get a spherical one).




Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.



PARTNERS