Hi folks, I've been messing around with Deferred Shading (I'm using D3D10), and everything pretty much works till the G-Buffer filling pass and reconstruction. The reconstructed world space position is fine, the normals look fine, the light vector looks fine, etc... (I'm comparing with the forward rendering approach for reference). But somehow, the diffuse and specular terms dont turn out right. There seems to be a glitch in the Normals, though it somehow doesnt appear that way... (There's no problem with the position because when I use a directional light, I still get the same problem).
Here's my RT config:
Depth - R32F
Normals - R11G11B10F
Ambient + Occlusion - R8G8B8A8
Diffuse + MatID - R8G8B8A8
Specular + Emissive Term - R8G8B8A8
I've tried storing normals in a R8G8B8A8 buffer, storing xy components in a R16G16F/R32G32F buffer and recomputing z, but I still get the same problem. Can anybody give me some insight into what's going on? Thanks a lot.
Here's the Deferred Rendering code (from the lighting pass):
And here's the Forward Rendering code:
Here are some images...
Normals - Deferred Rendering
Normals - Forward Rendering
Light Vector - Deferred Rendering
Light Vector - Forward Rendering
Lighting - Deferred Rendering
Lighting - Forward Rendering
Here's my RT config:
Depth - R32F
Normals - R11G11B10F
Ambient + Occlusion - R8G8B8A8
Diffuse + MatID - R8G8B8A8
Specular + Emissive Term - R8G8B8A8
I've tried storing normals in a R8G8B8A8 buffer, storing xy components in a R16G16F/R32G32F buffer and recomputing z, but I still get the same problem. Can anybody give me some insight into what's going on? Thanks a lot.
Here's the Deferred Rendering code (from the lighting pass):
Code:
// Fetch the depth map and compute world space position
float fDepth = g_txRTDepth.Sample(g_sampPointClamp, In.TexCoord);
float4 cPos = float4(In.DepthCoord, fDepth, 1.0f); // Depth coord is (Tex.x * 2 - 1, 1 - 2 * Tex.y) and is computed in the vertex shader
float4 tPos = mul(cPos, g_mViewProjI);
float3 wPos = tPos.xyz / tPos.w;
// Fetch the normal map and compute the normals
float3 vNormal = g_txRTNormal.Sample(g_sampPointClamp, In.TexCoord);
// Fetch the ambient map and occlusion term
float4 vAmbOcc = g_txRTAmbOcc.Sample(g_sampPointClamp, In.TexCoord);
float3 vAmbient = vAmbOcc.rgb * vAmbOcc.a;
// Fetch the diffuse map and material ID
float4 vDiffMatID = g_txRTDiffMatID.Sample(g_sampPointClamp, In.TexCoord);
float3 vDiffuse = vDiffMatID.rgb;
int nMatID = vDiffMatID.a * 255;
// Fetch the specular map and emissive term
float4 vSpecEms = g_txRTSpecEms.Sample(g_sampPointClamp, In.TexCoord);
float3 vSpecular = vSpecEms.rgb;
float3 vEmissive = vDiffuse * vSpecEms.a;
// Compute the light vector and attenuation
float3 vLightDir = g_vLightPos - wPos;
float fLightDist = length(vLightDir);
float fAttenuation = 1 / (g_vLightAtten.x + fLightDist * g_vLightAtten.y + fLightDist * fLightDist * g_vLightAtten.z);
vLightDir = normalize(vLightDir);
// Compute the view direction
float3 vViewDir = normalize(wPos - g_vCamPos);
// Compute the diffuse and specular lighting terms
float NdotL = max(dot(vNormal, vLightDir), 0);
float RdotV = max(dot(normalize(reflect(vLightDir, vNormal)), vViewDir), 0);
// Compute the final lighting
float3 vLighting = vAmbient + (vDiffuse * NdotL + vSpecular * pow(RdotV, 50)) * g_vLightColor * fAttenuation;
return float4(vLightDir, 1.0f);
And here's the Forward Rendering code:
Code:
// Compute the normals
float3 vN = g_txNormal.Sample(g_sampLinearWrap, In.TexCoord) * 2 - 1;
float3 vBinormal = normalize(cross(In.Normal, In.Tangent)); // Should be done in the vertex shader
float3x3 mBTN = {vBinormal, In.Tangent, In.Normal};
float3 vNormal = normalize(mul(vN, mBTN));
// Compute the ambient color
float fOcc = g_txOcclusion.Sample(g_sampLinearWrap, In.TexCoord);
float3 vAmbient = g_vAmbientColor * fOcc;
// Fetch the diffuse map
float3 vDiffuse = g_txDiffuse.Sample(g_sampLinearWrap, In.TexCoord);
// Fetch the specular and emissive maps
float3 vSpecular = g_txSpecular.Sample(g_sampLinearWrap, In.TexCoord);
// Compute the light vector and attenuation
float3 vLightDir = g_vLightPos - In.WPos;
float fLightDist = length(vLightDir);
float fAttenuation = 1 / (g_vLightAtten.x + fLightDist * g_vLightAtten.y + fLightDist * fLightDist * g_vLightAtten.z);
vLightDir = normalize(vLightDir);
// Compute the view direction
float3 vViewDir = normalize(In.WPos - g_vCamPos);
// Compute the diffuse and specular lighting terms
float NdotL = max(dot(vNormal, vLightDir), 0);
float RdotV = max(dot(normalize(reflect(vLightDir, vNormal)), vViewDir), 0);
// Compute the final lighting
float3 vLighting = vAmbient + (vDiffuse * NdotL + vSpecular * pow(RdotV, 50)) * g_vLightColor * fAttenuation;
return float4(vLightDir, 1.0f);
Here are some images...
Normals - Deferred Rendering
Normals - Forward Rendering
Light Vector - Deferred Rendering
Light Vector - Forward Rendering
Lighting - Deferred Rendering
Lighting - Forward Rendering