/trunk/Hieroglyph3/Applications/Data/Shaders/GBuffer.hlsl

# · Unknown · 204 lines · 165 code · 39 blank · 0 comment · 0 complexity · 586fe36736888d2f60fa4ce9391a1053 MD5 · raw file

  1. //--------------------------------------------------------------------------------
  2. // GBuffer
  3. //
  4. // Vertex shader and pixel shader for filling the G-Buffer of a classic
  5. // deferred renderer
  6. //--------------------------------------------------------------------------------
  7. cbuffer Transforms
  8. {
  9. matrix WorldMatrix;
  10. matrix WorldViewMatrix;
  11. matrix WorldViewProjMatrix;
  12. };
  13. struct VSInput
  14. {
  15. float4 Position : POSITION;
  16. float2 TexCoord : TEXCOORDS0;
  17. float3 Normal : NORMAL;
  18. float4 Tangent : TANGENT;
  19. };
  20. struct VSOutput
  21. {
  22. float4 PositionCS : SV_Position;
  23. float2 TexCoord : TEXCOORD;
  24. float3 NormalWS : NORMALWS;
  25. float3 PositionWS : POSITIONWS;
  26. float3 TangentWS : TANGENTWS;
  27. float3 BitangentWS : BITANGENTWS;
  28. };
  29. struct VSOutputOptimized
  30. {
  31. float4 PositionCS : SV_Position;
  32. float2 TexCoord : TEXCOORD;
  33. float3 NormalVS : NORMALVS;
  34. float3 TangentVS : TANGENTVS;
  35. float3 BitangentVS : BITANGENTVS;
  36. };
  37. struct PSInput
  38. {
  39. float4 PositionSS : SV_Position;
  40. float2 TexCoord : TEXCOORD;
  41. float3 NormalWS : NORMALWS;
  42. float3 PositionWS : POSITIONWS;
  43. float3 TangentWS : TANGENTWS;
  44. float3 BitangentWS : BITANGENTWS;
  45. };
  46. struct PSOutput
  47. {
  48. float4 Normal : SV_Target0;
  49. float4 DiffuseAlbedo : SV_Target1;
  50. float4 SpecularAlbedo : SV_Target2;
  51. float4 Position : SV_Target3;
  52. };
  53. struct PSInputOptimized
  54. {
  55. float4 PositionSS : SV_Position;
  56. float2 TexCoord : TEXCOORD;
  57. float3 NormalVS : NORMALVS;
  58. float3 TangentVS : TANGENTVS;
  59. float3 BitangentVS : BITANGENTVS;
  60. };
  61. struct PSOutputOptimized
  62. {
  63. float4 Normal : SV_Target0;
  64. float4 DiffuseAlbedo : SV_Target1;
  65. float4 SpecularAlbedo : SV_Target2;
  66. };
  67. //-------------------------------------------------------------------------------------------------
  68. // Textures
  69. //-------------------------------------------------------------------------------------------------
  70. Texture2D DiffuseMap : register( t0 );
  71. Texture2D NormalMap : register( t1 );
  72. SamplerState AnisoSampler : register( s0 );
  73. //-------------------------------------------------------------------------------------------------
  74. // Basic vertex shader, no optimizations
  75. //-------------------------------------------------------------------------------------------------
  76. VSOutput VSMain( in VSInput input )
  77. {
  78. VSOutput output;
  79. // Convert position and normals to world space
  80. output.PositionWS = mul( input.Position, WorldMatrix ).xyz;
  81. float3 normalWS = normalize( mul( input.Normal, (float3x3)WorldMatrix ) );
  82. output.NormalWS = normalWS;
  83. // Reconstruct the rest of the tangent frame
  84. float3 tangentWS = normalize( mul( input.Tangent.xyz, (float3x3)WorldMatrix ) );
  85. float3 bitangentWS = normalize( cross( normalWS, tangentWS ) ) * input.Tangent.w;
  86. output.TangentWS = tangentWS;
  87. output.BitangentWS = bitangentWS;
  88. // Calculate the clip-space position
  89. output.PositionCS = mul( input.Position, WorldViewProjMatrix );
  90. // Pass along the texture coordinate
  91. output.TexCoord = input.TexCoord;
  92. return output;
  93. }
  94. //-------------------------------------------------------------------------------------------------
  95. // Vertex shader, with optimizations
  96. //-------------------------------------------------------------------------------------------------
  97. VSOutputOptimized VSMainOptimized( in VSInput input )
  98. {
  99. VSOutputOptimized output;
  100. // Convert normals to view space
  101. float3 normalVS = normalize( mul( input.Normal, (float3x3)WorldViewMatrix ) );
  102. output.NormalVS = normalVS;
  103. // Reconstruct the rest of the tangent frame
  104. float3 tangentVS = normalize( mul( input.Tangent.xyz, (float3x3)WorldViewMatrix ) );
  105. float3 bitangentVS = normalize( cross( normalVS, tangentVS ) ) * input.Tangent.w;
  106. output.TangentVS = tangentVS;
  107. output.BitangentVS = bitangentVS;
  108. // Calculate the clip-space position
  109. output.PositionCS = mul( input.Position, WorldViewProjMatrix );
  110. // Pass along the texture coordinate
  111. output.TexCoord = input.TexCoord;
  112. return output;
  113. }
  114. //-------------------------------------------------------------------------------------------------
  115. // Basic pixel shader, no optimizations
  116. //-------------------------------------------------------------------------------------------------
  117. PSOutput PSMain( in PSInput input )
  118. {
  119. PSOutput output;
  120. // Sample the diffuse map
  121. float3 diffuseAlbedo = DiffuseMap.Sample( AnisoSampler, input.TexCoord ).rgb;
  122. // Normalize the tangent frame after interpolation
  123. float3x3 tangentFrameWS = float3x3( normalize( input.TangentWS ),
  124. normalize( input.BitangentWS ),
  125. normalize( input.NormalWS ) );
  126. // Sample the tangent-space normal map and decompress
  127. float3 normalTS = normalize( NormalMap.Sample( AnisoSampler, input.TexCoord ).rgb * 2.0f - 1.0f );
  128. // Convert to world space
  129. float3 normalWS = mul( normalTS, tangentFrameWS );
  130. // Output our G-Buffer values
  131. output.Normal = float4( normalWS, 1.0f );
  132. output.DiffuseAlbedo = float4( diffuseAlbedo, 1.0f );
  133. output.SpecularAlbedo = float4( 0.7f, 0.7f, 0.7f, 64.0f );
  134. output.Position = float4( input.PositionWS, 1.0f );
  135. return output;
  136. }
  137. //-------------------------------------------------------------------------------------------------
  138. // Function for encoding normals using a spheremap transform
  139. //-------------------------------------------------------------------------------------------------
  140. float2 SpheremapEncode( float3 normalVS )
  141. {
  142. return normalize( normalVS.xy ) * ( sqrt( -normalVS.z * 0.5f + 0.5f ) );
  143. }
  144. //-------------------------------------------------------------------------------------------------
  145. // Pixel shader, with optimizations
  146. //-------------------------------------------------------------------------------------------------
  147. PSOutputOptimized PSMainOptimized( in PSInputOptimized input )
  148. {
  149. PSOutputOptimized output;
  150. // Sample the diffuse map
  151. float3 diffuseAlbedo = DiffuseMap.Sample( AnisoSampler, input.TexCoord ).rgb;
  152. // Normalize the tangent frame after interpolation
  153. float3x3 tangentFrameVS = float3x3( normalize( input.TangentVS ),
  154. normalize( input.BitangentVS ),
  155. normalize( input.NormalVS ) );
  156. // Sample the tangent-space normal map and decompress
  157. float3 normalTS = normalize( NormalMap.Sample( AnisoSampler, input.TexCoord ).rgb * 2.0f - 1.0f );
  158. // Convert to view space
  159. float3 normalVS = mul( normalTS, tangentFrameVS );
  160. // Output our G-Buffer values
  161. output.Normal = float4( SpheremapEncode( normalVS ), 1.0f, 1.0f );
  162. output.DiffuseAlbedo = float4( diffuseAlbedo, 1.0f );
  163. output.SpecularAlbedo = float4( 0.7f, 0.7f, 0.7f, 64.0f / 255.0f );
  164. return output;
  165. }