Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include "../Common.hlsli"
- #include "../Normals.hlsli"
- /// <summary>Constants at Constant Buffer View (CBV) register 0</summary>
- cbuffer RootConstants : register(b0)
- {
- /// <summary>Render node ID</summary>
- uint ID;
- }
- /// <summary>Constant buffer at Constant Buffer View (CBV) register 1</summary>
- cbuffer CameraBuffer : register(b1)
- {
- /// <summary>Camera view matrix (world space->view space)</summary>
- float4x4 ViewMatrix;
- /// <summary>Camera projection matrix (view space->clip space)</summary>
- float4x4 ProjectionMatrix;
- /// <summary>Padding</summary>
- float4 pad[8];
- };
- /// <summary>Render nodes buffer</summary>
- StructuredBuffer<RenderNode> RenderNodes : register(t0);
- /// <summary>Materials buffer (bindless)</summary>
- StructuredBuffer<Material> Materials: register(t1);
- /// <summary>Textures buffer (bindless)</summary>
- Texture2D Textures[]: register(t2);
- /// <summary>Anisotropic texture sampler</summary>
- SamplerState AnisoSampler : register(s0);
- /// <summary>Structure holding data passed from vertex shader</summary>
- struct VSOut
- {
- /// <summary>Clip space position</summary>
- float4 position : SV_POSITION;
- /// <summary>Texture coordinates for maps</summary>
- float2 texCoord : TEXCOORD0;
- /// <summary>View space vertex normals</summary>
- float3 normal : TEXCOORD1;
- /// <summary>View space S-tangent vector</summary>
- float3 tangent : TEXCOORD2;
- /// <summary>View space T-tangent vector</summary>
- float3 bitangent : TEXCOORD3;
- /// <summary>Clip space position passed to fragment shader</summary>
- float4 clipPos : TEXCOORD4;
- };
- /// <summary>Vertex shader entry point</summary>
- /// <param name="position">Vertex position (object space)</param>
- /// <param name="normal">Vertex normal</param>
- /// <param name="texCoord">Vertex texture coordinates</param>
- /// <param name="tangent">Vertex S-tangent vector</param>
- /// <param name="bitangent">Vertex T-tangent vector</param>
- /// <return>Filled structure VSOut</return>
- VSOut VS(float3 position : POSITION,
- float3 normal : NORMAL,
- float2 texCoord : TEXCOORD0,
- float3 tangent : TEXCOORD2,
- float3 bitangent : TEXCOORD3)
- {
- VSOut result;
- // Transform vertex positions into world space
- float4 positionWS = mul(float4(position, 1.0f), RenderNodes[ID].WorldMatrix);
- // Transform vertex positions into clip space and set into output
- result.position = mul(ProjectionMatrix, mul(ViewMatrix, positionWS));
- // Set vertex texture coordinates into output
- result.texCoord = texCoord;
- // Transform vertex normal into view space normals
- result.normal = mul(ViewMatrix, float4(mul(float4(normal.xyz, 1.0f), RenderNodes[ID].WorldMatrixInverseTranspose).xyz, 0.0f)).xyz;
- // Transform S-tangent vector into view space normals
- result.tangent = mul(ViewMatrix, float4(mul(float4(tangent.xyz, 1.0f), RenderNodes[ID].WorldMatrixInverseTranspose).xyz, 0.0f)).xyz;
- // Transform T-tangent vector into view space normals
- result.bitangent = mul(ViewMatrix, float4(mul(float4(bitangent.xyz, 1.0f), RenderNodes[ID].WorldMatrixInverseTranspose).xyz, 0.0f)).xyz;
- // Calculate depth
- result.clipPos = result.position;
- return result;
- }
- /// <summary>Structure holding data output from pixel shader (i.e. Multiple Render Targets data)</summary>
- struct PSOut
- {
- /// <summary>Color stored in first render target</summary>
- float4 color : SV_TARGET0;
- /// <summary>Normal - encoded to 2 channels (along with metallic and roughness) stored in second render target</summary>
- float4 normal : SV_TARGET1;
- /// <summary>Depth stored in third render target</summary>
- float depth : SV_TARGET2;
- };
- /// <summary>Pixel shader entry point</summary>
- /// <param name="input">VSOut structure holding data passed from vertex shader</param>
- /// <return>Filled structure PSOut (Multiple Render Targets)</return>
- PSOut PS(VSOut input)
- {
- PSOut result;
- // Read diffuse texture (based on MaterialID for current render node), and set into color output
- result.color = pow(Textures[Materials[RenderNodes[ID].MaterialID].DiffuseMap].Sample(AnisoSampler, input.texCoord.xy), 2.2);
- // Calculate tangent frame for applying normal map to vertex normals
- float3x3 tangentFrame = float3x3(input.tangent, -input.bitangent, input.normal);
- // Read normal map texture (based on MaterialID for current render node), apply it to vertex normals (through tangent frame)
- float3 normal = normalize(mul(Textures[Materials[RenderNodes[ID].MaterialID].NormalsMap].Sample(AnisoSampler, input.texCoord.xy).xyz * 2.0f - 1.0f, tangentFrame));
- // Read metallic texture (based on MaterialID for current render node)
- float metallicMap = Textures[Materials[RenderNodes[ID].MaterialID].MetallicMap].Sample(AnisoSampler, input.texCoord.xy).x;
- // Read roughness texture (based on MaterialID for current render node)
- float roughnessMap = Textures[Materials[RenderNodes[ID].MaterialID].RoughnessMap].Sample(AnisoSampler, input.texCoord.xy).x;
- // Set normal output to contain encoded normal (first 2 channels - see Normals.hlsli for details), metallic (1 channel) and roughness (1 channel)
- result.normal = float4(EncodeNormal(normal), metallicMap, roughnessMap);
- // Write depth
- result.depth = input.clipPos.z / input.clipPos.w;
- return result;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement