Deferred shading at Direct3D11 (0)
I'm currently writing a test program for deferred shading at Direct3D11.
Direct3D11 で GBuffer を生成して, Deferred shading を行うテストプログラムを作成中です.
Debug draw of normal buffer in world coordinate.
ワールド座標系の法線を書き込んだバッファの可視化.

Debug draw of position buffer in world coordinate.
ワールド座標系の位置座標を書き込んだバッファの可視化.

Result of diffuse lighting using the G-Buffer and a single directional light.
GBuffer とディレクショナルライトでライティングを行った結果.

Debug draw of diffuse albedo texture.
Diffuse Albedo テクスチャを書き込んだバッファの可視化.

Debug draw of specular albedo texture.
Specular Albedo テクスチャを書き込んだバッファの可視化.

//--------------------------------------------------------------------------------------
//
// Dx11GBufferLightPrepass.hlsl ( hanecci 2011/12/30 )
//
//--------------------------------------------------------------------------------------
cbuffer cbTransformMatrix : register( b0 )
{
float4x4 mWorldViewProjectionMatrix : packoffset( c0 );
float4x4 mWorldViewMatrix : packoffset( c4 );
float4x4 mProjectionMatrix : packoffset( c8 );
float4x4 mWorldMatrix : packoffset( c12 );
float4x4 mViewProjectionMatrix : packoffset( c16 );
};
//--------------------------------------------------------------------------------------
struct VS_INPUT
{
float4 vPosition : POSITION;
float3 vNormal : NORMAL;
};
//--------------------------------------------------------------------------------------
struct VS_OUTPUT
{
float4 vPositionCS : SV_Position;
float3 vNormalWS : NORMALWS;
float3 vPositionWS : POSITIONWS;
};
//--------------------------------------------------------------------------------------
struct PS_INPUT
{
float4 vPositionSS : SV_Position;
float3 vNormalWS : NORMALWS;
float3 vPositionWS : POSITIONWS;
};
//--------------------------------------------------------------------------------------
struct PS_OUTPUT
{
float4 vNormal : SV_Target0;
float4 vPosition : SV_Target1;
};
//--------------------------------------------------------------------------------------
VS_OUTPUT MainVS( VS_INPUT input )
{
VS_OUTPUT output;
output.vPositionWS = mul( input.vPosition, mWorldMatrix ).xyz;
output.vNormalWS = normalize( mul( input.vNormal,
( float3x3 ) mWorldMatrix ) );
output.vPositionCS = mul( input.vPosition,
mWorldViewProjectionMatrix );
return output;
}
//--------------------------------------------------------------------------------------
PS_OUTPUT MainPS( PS_INPUT input )
{
PS_OUTPUT output;
output.vPosition = float4( input.vPositionWS, 0.2f );
float3 normalWS = normalize( input.vNormalWS );
output.vNormal = float4( normalWS, 0.4f );
return output;
}
//--------------------------------------------------------------------------------------//--------------------------------------------------------------------------------------
//
// Dx11DeferredShading.hlsl ( hanecci, 2011/12/31 )
//
//--------------------------------------------------------------------------------------
Texture2D mNormalTexture : register( t1 );
Texture2D mPositionTexture : register( t2 );
//--------------------------------------------------------------------------------------
static const float3 cLightDir = float3( 0.0f, -1.0f, 0.0f );
static const float3 cLightDiffuseColor = float3( 1.0f, 1.0f, 1.0f );
static const float3 cLightSpecularColor = float3( 1.0f, 1.0f, 1.0f );
//--------------------------------------------------------------------------------------
struct VS_Input
{
float3 vPosition : POSITION;
float2 vTexcoord0 : TEXCOORD0;
};
//--------------------------------------------------------------------------------------
struct PS_Input
{
float4 vPosition : SV_Position;
};
//--------------------------------------------------------------------------------------
PS_Input MainVS( VS_Input vs_input )
{
PS_Input ps_input = ( PS_Input ) 0;
ps_input.vPosition = float4( vs_input.vPosition, 1.0f );
return ps_input;
}
//--------------------------------------------------------------------------------------
void
getGBufferAttributes( in float2 screenPos,
out float3 normal,
out float3 position )
{
int3 sampleIndices = int3( screenPos.xy, 0 );
normal = mNormalTexture.Load( sampleIndices ).xyz;
position = mPositionTexture.Load( sampleIndices ).xyz;
}
//--------------------------------------------------------------------------------------
float3
calcDirectionalLighting( float3 worldNormal,
float3 worldPosition,
float3 lightWorldDir,
float3 lightDiffuseColor,
float3 lightSpecularColor )
{
float3 color = float3( 0.0f, 0.0f, 0.0f );
float3 lightDir = - lightWorldDir;
float dotValue = max( dot( lightDir, worldNormal ), 0.0f );
color = dotValue * lightDiffuseColor;
return color;
}
//--------------------------------------------------------------------------------------
float4
MainPS( PS_Input ps_input ) : SV_Target
{
float3 worldNormal;
float3 worldPosition;
getGBufferAttributes( ps_input.vPosition.xy,
worldNormal,
worldPosition );
float4 color = float4( 0.0f, 0.0f, 0.0f, 1.0f );
color.xyz = calcDirectionalLighting( worldNormal,
worldPosition,
cLightDir,
cLightDiffuseColor,
cLightSpecularColor );
return color;
}
//--------------------------------------------------------------------------------------