• 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
WFP

SSAO with Deferred Shading Issues

4 posts in this topic

Greetings,

 

In my deferred shading pipeline, I am trying to add screen-space ambient occlusion.  I'm (of course) creating it after my G-Buffer is created so I have the normals in view space and can reconstruct the position in view space from the depth buffer.  I originally implemented it in my forward renderer following the example from Frank Luna's Direct3D 11 book, and it worked well, but I am running into some issues trying to adapt that to my deferred shading approach.

 

My G-Buffer normal render target is a R32G32B32A32_FLOAT format and my depth buffer is D24S8, so precision for calculations isn't an issue.  As mentioned, I'm reconstructing the view space position from depth as described here by MJP.

 

The results I'm getting are shown below.  As you can see in the first image, some of the occlusion looks correct, particularly where the box sits over the ground and the corner of the upper-middle box touches the lower-middle box, as well as the occlusion occurring on the sphere behind the left-most box.  You'll notice in the first image that there is some occlusion being generated in the gap between the upper two boxes, but not as much as I would probably expect.  Furthermore, when I move the camera to the right very slightly (second image), the occlusion value between them basically disappears, which makes me think I'm doing something wrong somewhere in view space.  Notice that for those two boxes, too, the faces facing the screen have nothing else in front of them, so I would imagine they would receive no occlusion at all.

 

The third image is the same scene rendered from a different angle, this time showing that the sphere is somehow getting occlusion on faces that have no other geometry in front of them.

 

Also, the images are intentionally unblurred so we can see them for what they are and hopefully get a better idea of what is happening.  I've tried it with bilateral blur passes enabled and the blurring works fine, but it's still just blurring the same incorrect data.  Any ideas that might help in fixing this issue are very welcome and appreciated.

 

Here is the shader code I am using.

 

Vertex Shader:

struct VertexIn
{
	float3 posL : POSITION;
	float2 tex : TEXCOORD;
};

struct VertexOut
{
	float4 posH : SV_POSITION;
	float3 viewRay : VIEWRAY;
	float2 tex : TEXCOORD;
};

cbuffer cbPerFrame : register(cb0)
{
	float4x4 inverseProjectionMatrix;
};

VertexOut main(VertexIn vIn)
{
	VertexOut vOut;

	// already in NDC space
	vOut.posH = float4(vIn.posL, 1.0f);

	float3 positionV = mul(float4(vIn.posL, 1.0f), inverseProjectionMatrix).xyz;
	vOut.viewRay = float3(positionV.xy / positionV.z, 1.0f);

	// pass to pixel shader
	vOut.tex = vIn.tex;

	return vOut;
}

 

 

Pixel Shader:

 

struct VertexOut
{
	float4 posH : SV_POSITION;
	float3 viewRay : VIEWRAY;
	float2 tex : TEXCOORD;
};

cbuffer cbPerFrame : register(cb0)
{
	float4x4 gViewToTexSpace; // proj * texture
	float4 gOffsetVectors[14];

	float gOcclusionRadius;    //0.5f
	float gOcclusionFadeStart; // 0.2f
	float gOcclusionFadeEnd;   // 2.0f
	float gSurfaceEpsilon;     // 0.05f

	// for reconstructing position from depth
	float projectionA;
	float projectionB;

	float2 _padding;
};

Texture2D normalTexture : register(t0);
Texture2D depthStencilTexture : register(t1);
Texture2D randomVecMap : register(t2);

SamplerState samNormalDepth : register(s0);
SamplerState samRandomVec : register(s1);

// determines how much the sample point q occludes the point p as a function of distZ
float occlusionFunction(float distZ)
{
	float occlusion = 0.0f;
	if(distZ > gSurfaceEpsilon)
	{
		float fadeLength = gOcclusionFadeEnd - gOcclusionFadeStart;

		// linearly decrease occlusion from 1 to 0 as distZ goes from fade start to end
		occlusion = saturate((gOcclusionFadeEnd - distZ) / fadeLength);
	}
	return occlusion;
}

float4 main(VertexOut pIn) : SV_TARGET
{
	float3 normal = normalize(normalTexture.SampleLevel(samNormalDepth, pIn.tex, 0.0f).xyz);
	float depth = depthStencilTexture.SampleLevel(samNormalDepth, pIn.tex, 0.0f).r;
	float linearDepth = projectionB / (depth - projectionA);
	float3 position = pIn.viewRay * linearDepth;

	// extract random vector from map from [0,1] to [-1, 1]
	float3 randVec = 2.0f * randomVecMap.SampleLevel(samRandomVec, 4.0f * pIn.tex, 0.0f).rgb - 1.0f;

	float occlusionSum = 0.0f;

	// sample neighboring points about position in the hemisphere oriented by normal
	[unroll]
	for(int i = 0; i < 14; ++i)
	{
		// offset vectors are fixed and uniformly distributed - reflecting them about a random vector gives a random, uniform distribution
		float3 offset = reflect(gOffsetVectors[i].xyz, randVec);

		// flip offset vector if it is behind the plane define by (position, normal)
		float flip = sign(dot(offset, normal));

		// sample a point near position within the occlusion radius
		float3 q = position + flip * gOcclusionRadius * offset;

		// project q and generate projective tex-coords
		float4 projQ = mul(float4(q, 1.0f), gViewToTexSpace);
		projQ.xy /= projQ.w;

		// find nearest depth value along ray from eye to q
		float rz = depthStencilTexture.SampleLevel(samNormalDepth, projQ.xy, 0.0f).r;

		// reconstruct full view space position r = (rx, ry, rz)
		linearDepth = projectionB / (rz - projectionA);
		float3 r = pIn.viewRay * linearDepth;

		// test whether r occludes position
		float distZ = position.z - r.z;
		float dp = max(dot(normal, normalize(r - position)), 0.0f);
		float occlusion = dp * occlusionFunction(distZ);

		occlusionSum += occlusion;
	}

	occlusionSum /= 14;

	float access = 1.0f - occlusionSum;

	// sharpen the contrast of the SSAO map to make the effect more dramatic
	return saturate(pow(access, 4.0f));
}

 

 

 

Here is the application code for setting projectionA and projectionB (from Matt's post).

float clipDiff = farClipDistance - nearClipDistance;
float projectionA = farClipDistance / clipDiff;
float projectionB = (-farClipDistance * nearClipDistance) / clipDiff;

 

 

Thanks!

0

Share this post


Link to post
Share on other sites

so you're doing world space SSAO, but your normals are view space?

 

try to set qp here

    float dp = max(dot(normal, normalize(r - position)), 0.0f);

to 1.f and although it's ignoring the normal, it should look somehow stable, then you know, you have to either use worldspace normals, or to transform the world space vector between the position of the surface and the 'hit' position on the ssao sample into view space.

0

Share this post


Link to post
Share on other sites

Krypt0n:  Just tried your suggestion, and it didn't make a difference besides making the effect darker on the screen (of course, since it was always 1.0f instead of between 0.0f and 1.0f).  It still gave basically the exact same thing we see in the images above, though.

EDIT:  Sorry, meant to answer your first question, too.  I am trying to do everything in view space, not world space.

 

My thinking is that the issue is somewhere in these lines:

 

                // sample a point near position within the occlusion radius
		float3 q = position + flip * gOcclusionRadius * offset;

		// project q and generate projective tex-coords
		float4 projQ = mul(float4(q, 1.0f), gViewToTexSpace);
		projQ.xy /= projQ.w;

		// find nearest depth value along ray from eye to q
		float rz = depthStencilTexture.SampleLevel(samNormalDepth, projQ.xy, 0.0f).r;

 

because I am initially reconstructing the position from linear depth, but am not undoing that linear depth transformation before doing work with q to find the new sampling coordinates.  I have to run to work, but can test this theory this evening, plus any other suggestions anyone may have.

Edited by WFP
0

Share this post


Link to post
Share on other sites

Hm, I'm thinking that possibly in addition or instead of the idea I had earlier, it may actually be that I need to recalculate the view ray for the new texture location:

		// project q and generate projective tex-coords
		float4 projQ = mul(float4(q, 1.0f), gViewToTexSpace);
		projQ.xy /= projQ.w;

		// find nearest depth value along ray from eye to q
		float rz = depthStencilTexture.SampleLevel(samNormalDepth, projQ.xy, 0.0f).r;

		// reconstruct full view space position r = (rx, ry, rz)
		linearDepth = projectionB / (rz - projectionA);
		// this viewRay still points to the originally sampled coordinate
		float3 r = pIn.viewRay * linearDepth;

 

I'll try to figure out a solution for this.  If anyone knows it offhand, that'd be great ;).

 

Thanks!

 

Edit: Update to comment in code sample

Edited by WFP
0

Share this post


Link to post
Share on other sites

My idea above seems to have worked for the most part!  Here's the new code to recalculate the view ray for the random sample:

 

		// sample a point near position within the occlusion radius
		float3 q = position + flip * gOcclusionRadius * offset;
		// new viewRay calculation
		float3 viewRay = float3(q.xy / q.z, 1.0f);

		// project q and generate projective tex-coords
		float4 projQ = mul(float4(q, 1.0f), gViewToTexSpace);
		projQ.xy /= projQ.w;

		// find nearest depth value along ray from eye to q
		float rz = depthStencilTexture.SampleLevel(samNormalDepth, projQ.xy, 0.0f).r;

		// reconstruct full view space position r = (rx, ry, rz)
		linearDepth = projectionB / (rz - projectionA);
		// use newly calculated viewRay
		float3 r = viewRay * linearDepth;

 

I've added a result screenshot below (still pre-blurring).  If there are any other glaring deficiencies you see with this result or the way I'm calculating it please let me know! smile.png

 

Thanks!

0

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