Advertisement
snake5

TBAA - HLSL pixel shader v1.0

Nov 9th, 2011
246
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.68 KB | None | 0 0
  1. /// The HLSL (D3D9) pixel shader of TBAA
  2. // Copyright (c) 2011 Arvīds Kokins (snake5)
  3. //
  4. // Licensing: http://creativecommons.org/licenses/by/3.0/
  5. //
  6.  
  7.  
  8. sampler2D Texture0; // the input texture
  9. float2 InvTexSize; // inverse size of the input texture
  10.  
  11.  
  12. const float3 D3 = float3( 1.0/3.0, 1.0/3.0, 1.0/3.0 );
  13. const float BW[ 4 ] = { 1.0 / 16.0, // blend weights
  14.    2.0 / 16.0, 3.0 / 16.0, 4.0 / 16.0 };
  15.  
  16.  
  17. float4 TBAA( float2 texCoord : TEXCOORD0 ) : COLOR
  18. {
  19.    // calculate the normal
  20.    float4 C0 = tex2D( Texture0, texCoord );
  21.    float4 C1 = tex2D( Texture0, texCoord + float2( InvTexSize.x, 0 ) );
  22.    float4 C2 = tex2D( Texture0, texCoord - float2( InvTexSize.x, 0 ) );
  23.    float4 C3 = tex2D( Texture0, texCoord + float2( 0, InvTexSize.y ) );
  24.    float4 C4 = tex2D( Texture0, texCoord - float2( 0, InvTexSize.y ) );
  25.    float Q0 = dot( C0.rgb, D3 );
  26.    float Q1 = dot( C1.rgb, D3 );
  27.    float Q2 = dot( C2.rgb, D3 );
  28.    float Q3 = dot( C3.rgb, D3 );
  29.    float Q4 = dot( C4.rgb, D3 );
  30.    float2 N = float2( Q1 - Q2, Q3 - Q4 );
  31.    // get perp, scaled
  32.    float dst = 3.14159; // the more imprecise, the better result
  33.    float2 pN = float2( -N.y, N.x ) * dst;
  34.    // apply TBAA
  35.    float4 Cp1 = tex2D( Texture0, texCoord + InvTexSize * pN );
  36.    float4 Cp2 = tex2D( Texture0, texCoord + InvTexSize * pN * 2 );
  37.    float4 Cp3 = tex2D( Texture0, texCoord + InvTexSize * pN * 3 );
  38.    float4 Cn1 = tex2D( Texture0, texCoord - InvTexSize * pN );
  39.    float4 Cn2 = tex2D( Texture0, texCoord - InvTexSize * pN * 2 );
  40.    float4 Cn3 = tex2D( Texture0, texCoord - InvTexSize * pN * 3 );
  41.    return C0 * BW[ 3 ] + ( Cp1 + Cn1 ) * BW[ 2 ] +
  42.       ( Cp2 + Cn2 ) * BW[ 1 ] + ( Cp3 + Cn3 ) * BW[ 0 ];
  43. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement