Code Sample – MRT Pixel Shader

[hlsl]
Texture2D diffuseMap : register(t0);
Texture2D normalMap : register(t1);
Texture2D glowMap : register(t2);

SamplerState mySampler : register(s0);

struct VS_OUT
{
float4 position : SV_POSITION;
float4 visual : QUAD_VISUAL; //(u, v, alpha, glow)
};

struct PS_OUT
{
float4 diffuse : SV_TARGET0;
float4 normal : SV_TARGET1;
float4 glow : SV_TARGET2;
};

PS_OUT PS(VS_OUT pixel)
{
float4 diffuse =
diffuseMap.Sample(mySampler, pixel.visual.xy);
float4 normal =
normalMap .Sample(mySampler, pixel.visual.xy);
float4 glow =
glowMap .Sample(mySampler, pixel.visual.xy);

//apply per-vertex alpha
diffuse.a *= pixel.visual.z;

//calculate glow
float4 finalGlow =
float4
(
lerp
(
diffuse.rgb,
float3(0.0, 0.0, 0.0),
1.0 – glow.r * glow.a
* pixel.visual.w
* diffuse.a
),
diffuse.a * pixel.visual.z
);

PS_OUT output;
output.diffuse = diffuse;
output.normal = float4(normal.rgb, normal.a * diffuse.a);
output.glow = finalGlow;

return output;
}

[/hlsl]

Leave a Reply