Advertisement
Zgragselus

GBuffer hlsl

May 31st, 2024
817
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 5.19 KB | None | 0 0
  1. #include "../Common.hlsli"
  2. #include "../Normals.hlsli"
  3.  
  4. /// <summary>Constants at Constant Buffer View (CBV) register 0</summary>
  5. cbuffer RootConstants : register(b0)
  6. {
  7.     /// <summary>Render node ID</summary>
  8.     uint ID;
  9. }
  10.  
  11. /// <summary>Constant buffer at Constant Buffer View (CBV) register 1</summary>
  12. cbuffer CameraBuffer : register(b1)
  13. {
  14.     /// <summary>Camera view matrix (world space->view space)</summary>
  15.     float4x4 ViewMatrix;
  16.  
  17.     /// <summary>Camera projection matrix (view space->clip space)</summary>
  18.     float4x4 ProjectionMatrix;
  19.  
  20.     /// <summary>Padding</summary>
  21.     float4 pad[8];
  22. };
  23.  
  24. /// <summary>Render nodes buffer</summary>
  25. StructuredBuffer<RenderNode> RenderNodes : register(t0);
  26.  
  27. /// <summary>Materials buffer (bindless)</summary>
  28. StructuredBuffer<Material> Materials: register(t1);
  29.  
  30. /// <summary>Textures buffer (bindless)</summary>
  31. Texture2D Textures[]: register(t2);
  32.  
  33. /// <summary>Anisotropic texture sampler</summary>
  34. SamplerState AnisoSampler : register(s0);
  35.  
  36. /// <summary>Structure holding data passed from vertex shader</summary>
  37. struct VSOut
  38. {
  39.     /// <summary>Clip space position</summary>
  40.     float4 position : SV_POSITION;
  41.  
  42.     /// <summary>Texture coordinates for maps</summary>
  43.     float2 texCoord : TEXCOORD0;
  44.  
  45.     /// <summary>View space vertex normals</summary>
  46.     float3 normal : TEXCOORD1;
  47.  
  48.     /// <summary>View space S-tangent vector</summary>
  49.     float3 tangent : TEXCOORD2;
  50.  
  51.     /// <summary>View space T-tangent vector</summary>
  52.     float3 bitangent : TEXCOORD3;
  53.  
  54.     /// <summary>Clip space position passed to fragment shader</summary>
  55.     float4 clipPos : TEXCOORD4;
  56. };
  57.  
  58. /// <summary>Vertex shader entry point</summary>
  59. /// <param name="position">Vertex position (object space)</param>
  60. /// <param name="normal">Vertex normal</param>
  61. /// <param name="texCoord">Vertex texture coordinates</param>
  62. /// <param name="tangent">Vertex S-tangent vector</param>
  63. /// <param name="bitangent">Vertex T-tangent vector</param>
  64. /// <return>Filled structure VSOut</return>
  65. VSOut VS(float3 position : POSITION,
  66.     float3 normal : NORMAL,
  67.     float2 texCoord : TEXCOORD0,
  68.     float3 tangent : TEXCOORD2,
  69.     float3 bitangent : TEXCOORD3)
  70. {
  71.     VSOut result;
  72.  
  73.     // Transform vertex positions into world space
  74.     float4 positionWS = mul(float4(position, 1.0f), RenderNodes[ID].WorldMatrix);
  75.  
  76.     // Transform vertex positions into clip space and set into output
  77.     result.position = mul(ProjectionMatrix, mul(ViewMatrix, positionWS));
  78.     // Set vertex texture coordinates into output
  79.     result.texCoord = texCoord;
  80.     // Transform vertex normal into view space normals
  81.     result.normal = mul(ViewMatrix, float4(mul(float4(normal.xyz, 1.0f), RenderNodes[ID].WorldMatrixInverseTranspose).xyz, 0.0f)).xyz;
  82.     // Transform S-tangent vector into view space normals
  83.     result.tangent = mul(ViewMatrix, float4(mul(float4(tangent.xyz, 1.0f), RenderNodes[ID].WorldMatrixInverseTranspose).xyz, 0.0f)).xyz;
  84.     // Transform T-tangent vector into view space normals
  85.     result.bitangent = mul(ViewMatrix, float4(mul(float4(bitangent.xyz, 1.0f), RenderNodes[ID].WorldMatrixInverseTranspose).xyz, 0.0f)).xyz;
  86.     // Calculate depth
  87.     result.clipPos = result.position;
  88.  
  89.     return result;
  90. }
  91.  
  92. /// <summary>Structure holding data output from pixel shader (i.e. Multiple Render Targets data)</summary>
  93. struct PSOut
  94. {
  95.     /// <summary>Color stored in first render target</summary>
  96.     float4 color : SV_TARGET0;
  97.  
  98.     /// <summary>Normal - encoded to 2 channels (along with metallic and roughness) stored in second render target</summary>
  99.     float4 normal : SV_TARGET1;
  100.  
  101.     /// <summary>Depth stored in third render target</summary>
  102.     float depth : SV_TARGET2;
  103. };
  104.  
  105. /// <summary>Pixel shader entry point</summary>
  106. /// <param name="input">VSOut structure holding data passed from vertex shader</param>
  107. /// <return>Filled structure PSOut (Multiple Render Targets)</return>
  108. PSOut PS(VSOut input)
  109. {
  110.     PSOut result;
  111.  
  112.     // Read diffuse texture (based on MaterialID for current render node), and set into color output
  113.     result.color = pow(Textures[Materials[RenderNodes[ID].MaterialID].DiffuseMap].Sample(AnisoSampler, input.texCoord.xy), 2.2);
  114.  
  115.     // Calculate tangent frame for applying normal map to vertex normals
  116.     float3x3 tangentFrame = float3x3(input.tangent, -input.bitangent, input.normal);
  117.     // Read normal map texture (based on MaterialID for current render node), apply it to vertex normals (through tangent frame)
  118.     float3 normal = normalize(mul(Textures[Materials[RenderNodes[ID].MaterialID].NormalsMap].Sample(AnisoSampler, input.texCoord.xy).xyz * 2.0f - 1.0f, tangentFrame));
  119.  
  120.     // Read metallic texture (based on MaterialID for current render node)
  121.     float metallicMap = Textures[Materials[RenderNodes[ID].MaterialID].MetallicMap].Sample(AnisoSampler, input.texCoord.xy).x;
  122.  
  123.     // Read roughness texture (based on MaterialID for current render node)
  124.     float roughnessMap = Textures[Materials[RenderNodes[ID].MaterialID].RoughnessMap].Sample(AnisoSampler, input.texCoord.xy).x;
  125.  
  126.     // Set normal output to contain encoded normal (first 2 channels - see Normals.hlsli for details), metallic (1 channel) and roughness (1 channel)
  127.     result.normal = float4(EncodeNormal(normal), metallicMap, roughnessMap);
  128.  
  129.     // Write depth
  130.     result.depth = input.clipPos.z / input.clipPos.w;
  131.  
  132.     return result;
  133. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement