Hey Guys!
I have some trouble with the freaking old deferred renderer from Ziggyware/ XNA 4 version from Catalin Zima …
While rendering my gbuffer my depth buffer seems to look weird. Furthermore the lbuffer generated by the directional light is completely black.
I’ve been fumbling this issue for a week now with no progress … Anyone got a suggestion what is wrong here?!
The shaders…
Color-Shader (render geometry without a texture)
float4x4 World;
float4x4 View;
float4x4 Projection;
float specularIntensity = 0.8f;
float specularPower = 0.5f;
float FarPlane;
float3 Color;
// Define VS input
struct VSIn
{
float4 Position : POSITION0;
float3 Normal : NORMAL0;
float2 TextureCoords : TEXCOORD0;
};
// Define VS output and therefor PS input
struct VSOut
{
float4 Position : POSITION0;
float2 TextureCoords : TEXCOORD0;
float3 Normal : TEXCOORD1;
float3 Depth : TEXCOORD2;
};
struct PSOut
{
float4 Color : COLOR0;
float4 Normal : COLOR1;
float4 Depth : COLOR2;
};
/**************************************************
Vertex shader.
**************************************************/
VSOut MainVS(VSIn input)
{
VSOut output;
// Just pass the tex coords
output.TextureCoords = input.TextureCoords;
float4 worldPosition = mul(input.Position, World);
float4 viewPosition = mul(worldPosition, View);
// apply projection
output.Position = mul(viewPosition, Projection);
// Calc normal in world space
output.Normal = mul(input.Normal, World);
// Pass depth
output.Depth.x = output.Position.z; // screen space
output.Depth.y = output.Position.w; // screen space
output.Depth.z = viewPosition.z; // view space
return output;
}
/**************************************************
Pixel shader.
**************************************************/
PSOut MainPS(VSOut input)
{
PSOut output;
// Clamp normal from [-1,1] to [0,1]
output.Normal.rgb = 0.5f * (normalize(input.Normal) + 1.0f);
// Specular is stored in alpha chanel
output.Color.rgb = Color;
output.Color.a = specularIntensity;
output.Normal.a = specularPower;
// Depth is Position.z/Position.w
output.Depth = input.Depth.x / input.Depth.y; // ScreenSpace!
//output.Depth = -input.Depth.z / FarPlane; // ViewSpace!
return output;
}
/**************************************************
Techniques
- RenderGBuffer
**************************************************/
technique ClearGBuffer
{
pass Pass1
{
VertexShader = compile vs_4_0_level_9_1 MainVS();
PixelShader = compile ps_4_0_level_9_1 MainPS();
}
}
Directional light shader
float3 Color;
float3 cameraPosition;
float4x4 InvertViewProjection;
float4x4 InvertProjection;
texture colorMap;
texture normalMap;
texture depthMap;
float3 lightDirection;
texture occlusionMap;
sampler colorSampler = sampler_state
{
Texture = (colorMap);
AddressU = CLAMP;
AddressV = CLAMP;
MagFilter = POINT;
MinFilter = POINT;
Mipfilter = POINT;
};
sampler depthSampler = sampler_state
{
Texture = (depthMap);
AddressU = CLAMP;
AddressV = CLAMP;
MagFilter = POINT;
MinFilter = POINT;
Mipfilter = POINT;
};
sampler normalSampler = sampler_state
{
Texture = (normalMap);
AddressU = CLAMP;
AddressV = CLAMP;
MagFilter = POINT;
MinFilter = POINT;
Mipfilter = POINT;
};
sampler occlusionSampler = sampler_state
{
Texture = (occlusionMap);
AddressU = CLAMP;
AddressV = CLAMP;
MagFilter = POINT;
MinFilter = POINT;
Mipfilter = POINT;
};
// Define VS input
struct VSIn
{
float3 Position : POSITION0;
float3 TexCoord : TEXCOORD0;
};
// Define VS output and therefor PS input
struct VSOut
{
float4 Position : POSITION0;
float2 TexCoord : TEXCOORD0;
float3 PositionVS : TEXCOORD1;
};
// Define PS output
struct PSOut
{
float4 Color: COLOR0;
};
/**************************************************
Vertex shader.
**************************************************/
VSOut MainVS(VSIn input)
{
VSOut output;
output.Position = float4(input.Position, 1);
output.TexCoord = input.TexCoord;
output.PositionVS = mul(input.Position, InvertProjection);
return output;
}
/**************************************************
Pixel shader.
**************************************************/
PSOut MainPS(VSOut input)
{
PSOut output;
float4 normalData = tex2D(normalSampler,input.TexCoord);
float3 normal = 2.0f * normalData.xyz - 1.0f;
float specularPower = normalData.a * 255;
float specularIntensity = tex2D(colorSampler, input.TexCoord).a;
//read depth
float depthVal = tex2D(depthSampler,input.TexCoord).r;
float3 position = input.PositionVS.xyz * depthVal;
float3 lightVector = -(lightDirection);
float NdL = max(0,dot(normal,lightVector));
float3 diffuseLight = NdL * Color.rgb;
float3 reflectionVector = normalize(reflect(-lightVector, normal));
float3 directionToCamera = normalize(cameraPosition - position);
float specularLight = specularIntensity * pow(saturate(dot(reflectionVector, directionToCamera)), specularPower);
//output the light
// float occ = tex2D(occlusionSampler, input.TexCoord);
output.Color = float4(diffuseLight.rgb, specularLight); // *occ;
return output;
}
/**************************************************
Techniques
- RenderDirectionalLight
**************************************************/
technique RenderDirectionalLight
{
pass Pass0
{
CullMode = None;
ZEnable = false;
ZWriteEnable = false;
AlphaBlendEnable = true;
VertexShader = compile vs_4_0_level_9_1 MainVS();
PixelShader = compile ps_4_0_level_9_1 MainPS();
}
}
G- and L-Buffer setup::
// setup GBuffer
_gBuffer.Color = new RenderTarget2D(_graphicsDevice, bbWidth, bbHeight, false, SurfaceFormat.Color, DepthFormat.Depth24);
_gBuffer.Normal = new RenderTarget2D(_graphicsDevice, bbWidth, bbHeight, false, SurfaceFormat.Color, DepthFormat.None);
_gBuffer.Depth = new RenderTarget2D(_graphicsDevice, bbWidth, bbHeight, false, SurfaceFormat.Single, DepthFormat.None);
// setup LBuffer
_lBuffer.LightMap = new RenderTarget2D(_graphicsDevice, bbWidth, bbHeight, false, SurfaceFormat.Color, DepthFormat.None);
The commented screenspace/viewspace stuff is from my old plain XNA engine and will be needed for SSAO. Inside XNA the whole rendering worked with no problem, so I think this may be a mono related shader compiling issue?
THX a lot in advance!