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

Deferred Rendering - Questions and Help with Point Light Problems

9 posts in this topic

Note: Code linked in the post will change as I change the files on my computer and Dropbox updates them. :)

 

Hello everyone!  I am creating my very first deferred renderer (.h | .cpp). It has been exciting so far even though I just got done with Directional Lighting and am now trying to get Point Lights working.  I am also using this guide as reference.  Before I talk about the problems,  I have a few questions:

 

G-Buffer Formats:

I have 4 render targets (color, normal, depth, lighting).  Here's the formats I am using:

//Color 
RenderTargets.push_back(NXRenderTarget(DXGI_FORMAT_R16G16B16A16_UNORM, NXGPU::Context->ViewportWidth, NXGPU::Context->ViewportHeight));
//Normal 
RenderTargets.push_back(NXRenderTarget(DXGI_FORMAT_R16G16B16A16_UNORM, NXGPU::Context->ViewportWidth, NXGPU::Context->ViewportHeight));
//Depth
RenderTargets.push_back(NXRenderTarget(DXGI_FORMAT_R32_FLOAT, NXGPU::Context->ViewportWidth, NXGPU::Context->ViewportHeight));
//Lighting Buffer
RenderTargets.push_back(NXRenderTarget(DXGI_FORMAT_R16G16B16A16_UNORM, NXGPU::Context->ViewportWidth, NXGPU::Context->ViewportHeight));

Are those good formats to be using for the G-Buffer?  Also, I know this question may be stupid, but I'm a little confused about R32_FLOAT.  So I know the format contains one 32-bit floating-point red channel.  But in examples I've seen on the internet, when using this format, and drawing it to a quad, they get a White/Black depth buffer image.  When I render it to a quad, I see a Red/Black depth buffer image. Should the depth buffer always be white/black? Here are a few images of the output that I get from all three targets (from the left side of a cube).

 

Color:

f30c.png

 

Normal:

cda4.png

 

Depth:

gb0o.png

 

Point Light Problems:

For my engine I created a light manager (.h | .cpp) to handle lighting.  Implementing Directional Lighting was a breeze and worked the first time I tried it as you can see below:

 

Directional Light (1, 0, 0)

meok.png

 

Directional Light (1, 0, 1)

1fl5.png

 

As you can see, Directional Lights seem to work fine.  But point lights are weird.  First, I can't seem to get a smooth sphere rendered (created in 3DS Max with a radius of 1.0 and 32 segments):

 

3DS Max:

s91l.png

 

My Engine (in forward renderer):

fgej.png

 

As you can see the sphere is not smooth.  When using this sphere as a point light with Deferred Rendering, I get weird results.  It took me a long time to get to the point where anything is even lit.  I experimented for several days and finally got to this point, but something is still wrong. It's driving me crazy.

 

Point Light | Position: (10, 0, -8), Radius: 50, Intensity: 100

d9nl.png
 

Point Light | Position (10, 0, -8), Radius: 100, Intensity: 1000

t4wm.png

 

As you can see, something is certainly wrong.  If I set the intensity lower than 100 (like 4 as shown in the tutorials), nothing is visible.  If the radius is too small or big, nothing is shown. I tried moving it to different positions, disabling/enabling Z-Buffer, changing blend states, and I get the same results, sometimes nothing visible after the changes. I didn't even set my color to green.  It's supposed to be white.  This is the Point Light shader I am using:

 

Pixel Shader:
 

cbuffer LightDataBuffer : register(b0)
{
	float gLightRadius;
	float gLightIntensity;
	float2 gHalfPixel;
	float3 gLightColor;
	float3 gEyePosition;
	float3 gLightPosition;
	float4x4 gInverseVP;
}

Texture2D colorMap : register(t0);
Texture2D normalMap : register(t1);
Texture2D depthMap : register(t2);
SamplerState colorSampler
{
	AddressU = CLAMP;
	AddressV = CLAMP;
	MagFilter = LINEAR;
	MinFilter = LINEAR;
	Mipfilter = LINEAR;
};

SamplerState normalSampler
{
	AddressU = CLAMP;
	AddressV = CLAMP;
	MagFilter = POINT;
	MinFilter = POINT;
	Mipfilter = POINT;
};

struct VertexShaderOutput
{
	float4 Position : POSITION0;
	float4 ScreenPosition : TEXCOORD0;
};

float4 main(VertexShaderOutput input) : SV_TARGET
{
	input.ScreenPosition.xy /= input.ScreenPosition.w;
	float2 texCoord = 0.5f * (float2(input.ScreenPosition.x, -input.ScreenPosition.y) + 1);
	texCoord -= gHalfPixel;
	float4 normalData = normalMap.Sample(normalSampler, texCoord);
	float3 normal = 2.0f * normalData.xyz - 1.0f;
	float specularPower = normalData.a * 255;
	float specularIntensity = colorMap.Sample(colorSampler, texCoord).a;
	float depthVal = depthMap.Sample(normalSampler, texCoord).r;
	float4 position;
	position.xy = input.ScreenPosition.xy;
	position.z = depthVal;
	position.w = 1.0f;
	position = mul(position, gInverseVP);
	position /= position.w;
	float3 lightVector = gLightPosition - position;
	float attenuation = saturate(1.0f - length(lightVector) / gLightRadius);
	lightVector = normalize(lightVector);
	float NdL = max(0, dot(normal, lightVector));
	float3 diffuseLight = NdL * gLightColor.rgb;
	float3 reflectionVector = normalize(reflect(-lightVector, normal));
	float3 directionToCamera = normalize(gEyePosition - position.rgb);
	float specularLight = specularIntensity * pow(saturate(dot(reflectionVector, directionToCamera)), specularPower);
	return attenuation * gLightIntensity * float4(diffuseLight.rgb, specularLight);
}

Vertex Shader:
 

cbuffer MatrixBuffer : register(b0)
{
	float4x4 world;
	float4x4 view;
	float4x4 projection;
}

struct VertexShaderInput
{
	float3 Position : POSITION0;
};

struct VertexShaderOutput
{
	float4 Position : SV_POSITION;
	float4 ScreenPosition : TEXCOORD0;
};

VertexShaderOutput main(VertexShaderInput input)
{
	VertexShaderOutput output;
	float4 worldPosition = mul(float4(input.Position, 1), world);
	float4 viewPosition = mul(worldPosition, view);
	output.Position = mul(viewPosition, projection);
	output.ScreenPosition = output.Position;
	return output;
}

Point Light Shader class:

 

~Header:

class NXPointLightShader : public NXShader
			{
				public:
					struct LightDataBuffer
					{
						float Radius;
						float Intensity;
						XMVECTOR HalfPixel;
						XMVECTOR Color;
						XMVECTOR CameraPosition;
						XMVECTOR LightPosition;
						XMMATRIX InverseVP;
					};

					NXPointLightShader();
					bool SetVSConstants(NXCamera3D& camera, XMMATRIX& world);
					bool SetPSConstants(NXCamera3D& camera, XMVECTOR& position, XMVECTOR& color, float radius, float intensity);

				private:
					D3D11_MAPPED_SUBRESOURCE mappedResource;
					ID3D11Buffer* matrixBuffer;
					bool CreateMatrixBuffer();
					void DisposeShader();
			};

~Implementation:

NXPointLightShader::NXPointLightShader() : NXShader(L"PointLight", "../Debug/PointLightVS.cso", "../Debug/PointLightPS.cso") { if (!CreateMatrixBuffer()){ return; } }
			bool NXPointLightShader::SetVSConstants(NXCamera3D& camera, XMMATRIX& world)
			{
				TransformConstantBuffer* dataPtr = nullptr;

				XMMATRIX proj, view;
				camera.GetViewMatrix(view);
				NXGPU::Context->GetProjectionMatrix(proj);

				world = XMMatrixTranspose(world);
				view = XMMatrixTranspose(view);
				proj = XMMatrixTranspose(proj);

				if (FAILED(NXGPU::Map(matrixBuffer, 0, D3D11_MAP_WRITE_DISCARD, 0, &mappedResource)))
					return false;

				dataPtr = (TransformConstantBuffer*) mappedResource.pData;
				dataPtr->World = world;
				dataPtr->View = view;
				dataPtr->Projection = proj;

				NXGPU::Unmap(matrixBuffer, 0);

				NXGPU::SetVSConstantBuffers(0, 1, &matrixBuffer);

				return true;
			}
			bool NXPointLightShader::SetPSConstants(NXCamera3D& camera, XMVECTOR& position, XMVECTOR& color, float radius, float intensity)
			{
				LightDataBuffer* dataPtr = nullptr;

				XMMATRIX proj, view;
				camera.GetViewMatrix(view);
				NXGPU::Context->GetProjectionMatrix(proj);

				XMFLOAT2 halfPixel;
				halfPixel.x = 0.5f / (float) NXGPU::Context->ViewportWidth;
				halfPixel.y = 0.5f / (float) NXGPU::Context->ViewportHeight;

				if (FAILED(NXGPU::Map(ConstantBuffer, 0, D3D11_MAP_WRITE_DISCARD, 0, &mappedResource)))
					return false;

				dataPtr = (LightDataBuffer*) mappedResource.pData;
				dataPtr->Radius = radius;
				dataPtr->Intensity = intensity;
				dataPtr->HalfPixel = XMLoadFloat2(&halfPixel);
				dataPtr->LightPosition = position;
				dataPtr->Color = color;
				dataPtr->CameraPosition = camera.Position;
				dataPtr->InverseVP = XMMatrixInverse(nullptr, view * proj);

				NXGPU::Unmap(ConstantBuffer, 0);

				NXGPU::SetPSConstantBuffers(0, 1, &ConstantBuffer);

				ID3D11ShaderResourceView* textures[3] = { NXRenderer::Technique->RenderTargets[0].Texture->Texture, NXRenderer::Technique->RenderTargets[1].Texture->Texture, NXRenderer::Technique->RenderTargets[2].Texture->Texture };
				NXGPU::GetDeviceContext()->PSSetShaderResources(0, 3, textures);

				return true;
			}
			bool NXPointLightShader::CreateMatrixBuffer()
			{
				D3D11_BUFFER_DESC lightDataDesc;
				lightDataDesc.Usage = D3D11_USAGE_DYNAMIC;
				lightDataDesc.ByteWidth = sizeof(LightDataBuffer);
				lightDataDesc.BindFlags = D3D11_BIND_CONSTANT_BUFFER;
				lightDataDesc.CPUAccessFlags = D3D11_CPU_ACCESS_WRITE;
				lightDataDesc.MiscFlags = 0;
				lightDataDesc.StructureByteStride = 0;

				if (FAILED(NXGPU::GetDevice()->CreateBuffer(&lightDataDesc, NULL, &ConstantBuffer)))
					return false;

				D3D11_BUFFER_DESC matrixDataDesc;
				matrixDataDesc.Usage = D3D11_USAGE_DYNAMIC;
				matrixDataDesc.ByteWidth = sizeof(TransformConstantBuffer);
				matrixDataDesc.BindFlags = D3D11_BIND_CONSTANT_BUFFER;
				matrixDataDesc.CPUAccessFlags = D3D11_CPU_ACCESS_WRITE;
				matrixDataDesc.MiscFlags = 0;
				matrixDataDesc.StructureByteStride = 0;

				if (FAILED(NXGPU::GetDevice()->CreateBuffer(&matrixDataDesc, NULL, &matrixBuffer)))
					return false;

				return true;
			}
			void NXPointLightShader::DisposeShader(){ if (matrixBuffer){ matrixBuffer->Release(); matrixBuffer = nullptr; } }

I've tried transposing world, view, and projection which helped get me to where I am now.  Here is the full NXShader file (sets up shaders: .h | .cpp).  It doesn't seem to be working correctly at all and it's making frustrated.  Any help?  Also, how does my deferred rendering code look so far?  Any other problems? Thanks! smile.png

Edited by KoldGames
0

Share this post


Link to post
Share on other sites

Well since your directional light works, your problem probably isn't related to the normal.

 

My first suspect would be that your world position reconstruction isn't correct. I use a different technique (the one here: http://mynameismjp.wordpress.com/2009/03/10/reconstructing-position-from-depth/ ). I store view space depth instead of projection space depth. You should be able to get what you have working though. Is the division by position.w really needed? (I forget).

 

Have you tried stepping through the pixel shader in the debugger to see what the values look like?

 

Other tips for debugging: simplify your shader until you get *something* to render (for instance, just use the attenuation value that you calculate as output from the pixel shader).

 

As for your corrupted-looking sphere, I don't know if that's related. To simplify things for debugging, you could just start by rendering a full-screen quad for your point light (even though it would be inefficient).

 

 


Are those good formats to be using for the G-Buffer?

 

They are a little large, perhaps (two 64 bit RTs and one 32 bit RT). But don't worry about that now; get something working, then shrink them if it becomes a performance issue later.

 


So I know the format contains one 32-bit floating-point red channel.  But in examples I've seen on the internet, when using this format, and drawing it to a quad, they get a White/Black depth buffer image.  When I render it to a quad, I see a Red/Black depth buffer image. Should the depth buffer always be white/black?

 

It depends on the pixel shader you're using to render it. In DX9 anyway, when you sample from a texture with just a R channel, you get 0 for the G and B channels. So if your pixel shader just outputs that value, it will be red/black. 

0

Share this post


Link to post
Share on other sites

Have you tried stepping through the pixel shader in the debugger to see what the values look like?

 

Hey! I just tried that out after learning the Graphics Debugger was built into VS2013.  Here are my results:

 

Vertex Shader:

2drd.png

 

"worldPosition" seems to not be right.

 

Pixel Shader:
q9g.png

 

Really weird.  By the end of the Pixel Shader, several values are NaN.  Note I that added a Directional Light and I disabled the blend states before I debugged so I could see where the sphere was. Don't know if that was the cause of the values being NaN, just wanted to be sure.  I am pretty sure I am setting everything correctly.

 

Lighting Buffer

r17k.png
 

Really weird.  The sphere has a radius of 0.1 and intensity of 100.  While the directional light is working,  the point light doesn't seem to have a color. unsure.png huh.png

 

 

 


My first suspect would be that your world position reconstruction isn't correct. I use a different technique (the one here: 

 

I thought it was needed.  It was in the tutorial.  I tried removing it and got the same thing.  How are you storing view space depth?  I thought I was doing that.  Hmmm, lol. smile.png

Edited by KoldGames
0

Share this post


Link to post
Share on other sites


I thought it was needed.  It was in the tutorial.  I tried removing it and got the same thing.  How are you storing view space depth?  I thought I was doing that.  Hmmm, lol. 

 

Well in your pixel shader you're transforming your point from projection space to world space. So presumably you're storing projection space depth.

 

Can you show us the vertex and pixel shader for when you create your depth map?

0

Share this post


Link to post
Share on other sites


Can you show us the vertex and pixel shader for when you create your depth map?

 

Yep! Here it is:
 

Vertex Shader:

cbuffer MatrixBuffer : register(b0)
{
	matrix worldMatrix;
	matrix viewMatrix;
	matrix projectionMatrix;
}

struct VertexShaderInput
{
	float4 Position : POSITION0;
	float3 Normal : NORMAL0;
	float2 TexCoord : TEXCOORD0;
};
struct VertexShaderOutput
{
	float4 Position : SV_POSITION0;
	float2 TexCoord : TEXCOORD0;
	float3 Normal : TEXCOORD1;
	float2 Depth : TEXCOORD2;
};

VertexShaderOutput main(VertexShaderInput input)
{
	VertexShaderOutput output;
	float4 worldPosition = mul(input.Position, worldMatrix);
	float4 viewPosition = mul(worldPosition, viewMatrix);
	output.Position = mul(viewPosition, projectionMatrix);
	output.TexCoord = input.TexCoord;
	output.Normal = mul(input.Normal, worldMatrix);
	output.Depth.x = output.Position.z;
	output.Depth.y = output.Position.w;
	return output;
}

Pixel Shader

float specularIntensity = 0.8f;
float specularPower = 0.5f;

Texture2D shaderTexture : register(t0);
SamplerState SampleType;

struct VertexShaderOutput
{
	float4 Position : SV_POSITION0;
	float2 TexCoord : TEXCOORD0;
	float3 Normal : TEXCOORD1;
	float2 Depth : TEXCOORD2;
};

struct PixelShaderOutput
{
	half4 Color : SV_TARGET0;
	half4 Normal : SV_TARGET1;
	half4 Depth : SV_TARGET2;
};

PixelShaderOutput PS(VertexShaderOutput input)
{
	PixelShaderOutput output;
	output.Color = shaderTexture.Sample(SampleType, input.TexCoord);
	output.Color.a = specularIntensity;                              
	output.Normal.rgb = 0.5f * (normalize(input.Normal) + 1.0f); 
	output.Normal.a = specularPower; 
	output.Depth = input.Depth.x / input.Depth.y;
	return output;
}
0

Share this post


Link to post
Share on other sites
Ok, so I checked out my point light shader values in the debugger and noticed that some of my values weren't being set right so instead of using XMVECTOR for float2s, 3s, or 4s, I used XMFLOATs. Now I get a light that is the right color, but still not working correctly. I'll upload a photo soon.
0

Share this post


Link to post
Share on other sites

Here are some pictures:

 

Point Light | Position (0, 0, 0), White, Radius: 100, Intensity: 9 | InverseVP = XMMatrixTranspose(XMMatrixInverse(nullptr, view * proj));

 

hixc.png

^Point Light fades out when I look to the right and lighting appears to be wrong.

 

~

Point Light | Position (0, 0, 0), White, Radius: 100, Intensity: 9 | InverseVP = XMMatrixInverse(nullptr, view * proj);

89tk.png

 

Cube gets cutoff more and more as I look to the right, and lighting looks wrong.

0

Share this post


Link to post
Share on other sites

Hey! I fixed it! biggrin.png I'm so excited right now.  I decided to erase my point light code and rewrite it.  After getting a similar result to what I had before, I tried transposing the InvertViewProjection matrix and it worked out great! smile.png

 

luqs.png

qln5.png

66rt.png

Edited by KoldGames
0

Share this post


Link to post
Share on other sites

Awesome work! glad you got it sorted out.

 

For your g-buffer layout:  you can use ARGB8 for your diffuse and normals buffers. You can use some packing method for the normals buffer (I like best-fit normals, check out the Crytek ppts for info on that), or if your surfaces are rough enough you can get away with 8bit as it hides the banding. Diffuse doesn't need 16-bit at all unless you're storing some emissive lighting data, and even then I you can probably pack it down. :)

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