• Create Account

## Deferred Rendering - Questions and Help with Point Light Problems

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.

9 replies to this topic

### #1KoldGames  Members

222
Like
0Likes
Like

Posted 12 February 2014 - 10:34 PM

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:

Normal:

Depth:

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)

Directional Light (1, 0, 1)

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:

My Engine (in forward renderer):

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

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

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:

cbuffer LightDataBuffer : register(b0)
{
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
{
MagFilter = LINEAR;
MinFilter = LINEAR;
Mipfilter = LINEAR;
};

SamplerState normalSampler
{
MagFilter = POINT;
MinFilter = POINT;
Mipfilter = POINT;
};

{
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);
}


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

{
float3 Position : POSITION0;
};

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

{
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:

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

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();
};


~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->Intensity = intensity;
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 };

return true;
}
{

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

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!

Edited by KoldGames, 13 February 2014 - 04:02 PM.

### #2phil_t  Members

7653
Like
0Likes
Like

Posted 12 February 2014 - 11:57 PM

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.

### #3KoldGames  Members

222
Like
0Likes
Like

Posted 13 February 2014 - 12:50 PM

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:

"worldPosition" seems to not be right.

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

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.

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.

Edited by KoldGames, 13 February 2014 - 12:51 PM.

### #4phil_t  Members

7653
Like
0Likes
Like

Posted 14 February 2014 - 12:20 PM

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?

### #5KoldGames  Members

222
Like
0Likes
Like

Posted 14 February 2014 - 01:19 PM

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

Yep! Here it is:

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

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

{
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;
}

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

Texture2D shaderTexture : register(t0);
SamplerState SampleType;

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

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

{
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;
}

### #6KoldGames  Members

222
Like
0Likes
Like

Posted 15 February 2014 - 12:59 AM

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.

### #7KoldGames  Members

222
Like
0Likes
Like

Posted 15 February 2014 - 11:58 AM

Here are some pictures:

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

^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);

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

### #8KoldGames  Members

222
Like
0Likes
Like

Posted 16 February 2014 - 10:38 AM

*bump*

### #9KoldGames  Members

222
Like
0Likes
Like

Posted 16 February 2014 - 03:40 PM

Hey! I fixed it! 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!

Edited by KoldGames, 16 February 2014 - 03:40 PM.

### #10Styves  Members

1632
Like
0Likes
Like

Posted 17 February 2014 - 08:39 AM

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.

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.