• Announcements

    • khawk

      Download the Game Design and Indie Game Marketing Freebook   07/19/17

      GameDev.net and CRC Press have teamed up to bring a free ebook of content curated from top titles published by CRC Press. The freebook, Practices of Game Design & Indie Game Marketing, includes chapters from The Art of Game Design: A Book of Lenses, A Practical Guide to Indie Game Marketing, and An Architectural Approach to Level Design. The GameDev.net FreeBook is relevant to game designers, developers, and those interested in learning more about the challenges in game development. We know game development can be a tough discipline and business, so we picked several chapters from CRC Press titles that we thought would be of interest to you, the GameDev.net audience, in your journey to design, develop, and market your next game. The free ebook is available through CRC Press by clicking here. The Curated Books The Art of Game Design: A Book of Lenses, Second Edition, by Jesse Schell Presents 100+ sets of questions, or different lenses, for viewing a game’s design, encompassing diverse fields such as psychology, architecture, music, film, software engineering, theme park design, mathematics, anthropology, and more. Written by one of the world's top game designers, this book describes the deepest and most fundamental principles of game design, demonstrating how tactics used in board, card, and athletic games also work in video games. It provides practical instruction on creating world-class games that will be played again and again. View it here. A Practical Guide to Indie Game Marketing, by Joel Dreskin Marketing is an essential but too frequently overlooked or minimized component of the release plan for indie games. A Practical Guide to Indie Game Marketing provides you with the tools needed to build visibility and sell your indie games. With special focus on those developers with small budgets and limited staff and resources, this book is packed with tangible recommendations and techniques that you can put to use immediately. As a seasoned professional of the indie game arena, author Joel Dreskin gives you insight into practical, real-world experiences of marketing numerous successful games and also provides stories of the failures. View it here. An Architectural Approach to Level Design This is one of the first books to integrate architectural and spatial design theory with the field of level design. The book presents architectural techniques and theories for level designers to use in their own work. It connects architecture and level design in different ways that address the practical elements of how designers construct space and the experiential elements of how and why humans interact with this space. Throughout the text, readers learn skills for spatial layout, evoking emotion through gamespaces, and creating better levels through architectural theory. View it here. Learn more and download the ebook by clicking here. Did you know? GameDev.net and CRC Press also recently teamed up to bring GDNet+ Members up to a 20% discount on all CRC Press books. Learn more about this and other benefits here.
Sign in to follow this  
Followers 0
kazemi

SSAO Problem

6 posts in this topic

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.
0

Share this post


Link to post
Share on other sites
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: [url="http://www.john-chapman.net/content.php?id=8"]http://www.john-chapman.net/content.php?id=8[/url]
2

Share this post


Link to post
Share on other sites
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.
[quote name='johnchapman' timestamp='1312813612' post='4846174']
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: [url="http://www.john-chapman.net/content.php?id=8"]http://www.john-chap...ontent.php?id=8[/url]
[/quote]
0

Share this post


Link to post
Share on other sites
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.
1

Share this post


Link to post
Share on other sites
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).
1

Share this post


Link to post
Share on other sites
[quote name='johnchapman' timestamp='1312978097' post='4847139']
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).
[/quote]

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:
[source]
float2 rotationTC = pIn.pos.xy/4;
float3 vRotation = gNoiseVecMap.Sample(gPointSam, rotationTC);
[/source]

Then I use this vector to create a rotation matrix for each pixel:
[source]
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);
[/source]
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.

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

Share this post


Link to post
Share on other sites
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:
[attachment=4839: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).
2

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!


Register a new account

Sign in

Already have an account? Sign in here.


Sign In Now
Sign in to follow this  
Followers 0