First thank you for trying to help me out with my problem. I am a beginner in DirectX so sorry if I don't get it easily ;)
Here is a bit more code of my try :
The PixelShader that generate my texture (the depth texture is generated automaticly by the DephStencilView) :
GBufferPSOUT PSFillGBuffer(GBufferPSIN pIn)
{
// Texture map if any
float4 diffuseColor = MaterialDiffuse;
if (HasDiffuseMap)
diffuseColor = TexDiffuse.Sample(DefaultSampler, pIn.TextureUV);
// Specular map if any
float specIntensity = (MaterialSpecular.x + MaterialSpecular.y + MaterialSpecular.z) / 3;
if (HasSpecularMap)
{
float3 specColor = TexSpecular.Sample(DefaultSampler, pIn.TextureUV).rgb;
specIntensity = (specColor.x + specColor.y + specColor.z) / 3;
}
// Normal map if any
float3 normal = normalize(pIn.WNormal);
if (HasNormalMap)
{
float4 normalPixel = TexNormal.Sample(DefaultSampler, pIn.TextureUV);
pIn.WNormal = normalize((normalPixel.x * pIn.WTangent) + (normalPixel.y * pIn.WBitangent) + (normalPixel.z * pIn.WNormal));
}
// GBuffer Output
GBufferPSOUT result = (GBufferPSOUT)0;
result.Target0.xyz = diffuseColor.rgb;
result.Target0.w = specIntensity;
result.Target1 = float4(normal.xyz * 0.5 + 0.5, 0.0);
result.Target2.xyz = MaterialAmbient.rgb;
result.Target2.w = SpecularPower / 100;
// Return result
return result;
}
Then my Vertex Shader to reconstruct and light the GBuffer textures (I don't use the SV_VertexID in debug mode cause Visual Studio can't debug them) :
QuadPSIN VS_Main(uint vertexID : SV_VertexID)
{
QuadPSIN result;
result.UV = float2((vertexID << 1) & 2, vertexID & 2);
result.Position = float4(result.UV * float2(2.0f, -2.0f) + float2(-1.0f, 1.0f), 0.0f, 1.0f);
return result;
}
// When debugging, we can't use a SV_VertexID quad because Visual Studio Graphics Debugger can't trace pixel history from it.
QuadPSIN VS_Main_DEBUG(QuadVSIN vIn)
{
QuadPSIN result;
// The input quad is expected in device coordinates
// (i.e. 0,0 is center of screen, -1,1 top left, 1,-1
// bottom right). Therefore no transformation!
result.Position = vIn.Position;
result.Position.w = 1.0f;
// The UV coordinates are top-left 0,0 bottom-right 1,1
result.UV.x = result.Position.x * 0.5 + 0.5;
result.UV.y = result.Position.y * -0.5 + 0.5;
return result;
}
Here is the Pixel Shader :
float4 PS_Main(QuadPSIN pIn) : SV_Target
{
GBufferAttributes attrs;
bool processPixel = true;
ExtractGBufferAttributes(pIn.Position.xy, pIn.UV, Texture0, Texture1, Texture2, TextureDepth, attrs, processPixel);
if (!processPixel)
discard;
float3 WPos = attrs.Position;
float3 Vn = normalize(CameraPosition - WPos);
float3 Nn = attrs.Normal;
float3 litColor = float3(0.0f, 0.0f, 0.0f);
#if HEMI
// Calculating Ambient Light
litColor += ComputeHemisphericAmbientLight(Nn, attrs.Diffuse, AmbientUpColor, AmbientDownColor);
#endif
#if DIR
litColor += ComputeParallelLight(normalize(-DL_Direction), Nn, Vn, attrs.Ambient, attrs.Diffuse, float3(attrs.specIntensity, attrs.specIntensity, attrs.specIntensity), attrs.SpecularPower);
#endif
#if POINT
// Calculating Point Light
litColor += ComputePointLight(WPos, Nn, Vn, attrs.Ambient, attrs.Diffuse, float3(attrs.specIntensity, attrs.specIntensity, attrs.specIntensity), attrs.SpecularPower);
#endif
#if SPOT
// Calculating Spot Light
litColor += ComputeSpotLight(WPos, Nn, Vn, attrs.Ambient, attrs.Diffuse, float3(attrs.specIntensity, attrs.specIntensity, attrs.specIntensity), attrs.SpecularPower);
#endif
// Return result
return float4(litColor, 1);
}
And finaly my function supposed to unpack my GBuffer maps to usable data :
void ExtractGBufferAttributes(float2 position, float2 uv, Texture2D<float4> t0, Texture2D<float4> t1, Texture2D<float4> t2, Texture2D<float> t3, out GBufferAttributes attrs, out bool processPixel)
{
int3 screenPos = int3(position, 0);
processPixel = true;
attrs.Diffuse = t0.Load(screenPos).xyz;
attrs.SpecularIntensity = t0.Load(screenPos).w;
attrs.Normal = normalize(t1.Load(screenPos).xyz * 2.0 - 1.0);
attrs.Ambient = t2.Load(screenPos).xyz;
attrs.SpecularPower = t2.Load(screenPos).w * 100;
float depth = t3.Load(screenPos).x;
if (depth == 1.0f)
processPixel = false;
float4 cPos = float4(position, depth, 1);
float4 wPos = mul(ViewProjectionInverse, cPos);
attrs.Position = wPos.xyz / wPos.w;
}
I am actually looking at your code sample, but what I am not sure to get, is if you create your depth map manually inside the shader ? You don't use the one generated by the DepthStencil ?