home *** CD-ROM | disk | FTP | other *** search
/ Introduction to 3D Game …ogramming with DirectX 12 / Introduction-to-3D-Game-Programming-with-DirectX-12.ISO / Code.Textures / Chapter 20 Shadow Mapping / Shadows / Shaders / Common.hlsl next >
Encoding:
Text File  |  2016-03-02  |  4.4 KB  |  147 lines

  1. //***************************************************************************************
  2. // Common.hlsl by Frank Luna (C) 2015 All Rights Reserved.
  3. //***************************************************************************************
  4.  
  5. // Defaults for number of lights.
  6. #ifndef NUM_DIR_LIGHTS
  7.     #define NUM_DIR_LIGHTS 3
  8. #endif
  9.  
  10. #ifndef NUM_POINT_LIGHTS
  11.     #define NUM_POINT_LIGHTS 0
  12. #endif
  13.  
  14. #ifndef NUM_SPOT_LIGHTS
  15.     #define NUM_SPOT_LIGHTS 0
  16. #endif
  17.  
  18. // Include structures and functions for lighting.
  19. #include "LightingUtil.hlsl"
  20.  
  21. struct MaterialData
  22. {
  23.     float4   DiffuseAlbedo;
  24.     float3   FresnelR0;
  25.     float    Roughness;
  26.     float4x4 MatTransform;
  27.     uint     DiffuseMapIndex;
  28.     uint     NormalMapIndex;
  29.     uint     MatPad1;
  30.     uint     MatPad2;
  31. };
  32.  
  33. TextureCube gCubeMap : register(t0);
  34. Texture2D gShadowMap : register(t1);
  35.  
  36. // An array of textures, which is only supported in shader model 5.1+.  Unlike Texture2DArray, the textures
  37. // in this array can be different sizes and formats, making it more flexible than texture arrays.
  38. Texture2D gTextureMaps[10] : register(t2);
  39.  
  40. // Put in space1, so the texture array does not overlap with these resources.  
  41. // The texture array will occupy registers t0, t1, ..., t3 in space0. 
  42. StructuredBuffer<MaterialData> gMaterialData : register(t0, space1);
  43.  
  44.  
  45. SamplerState gsamPointWrap        : register(s0);
  46. SamplerState gsamPointClamp       : register(s1);
  47. SamplerState gsamLinearWrap       : register(s2);
  48. SamplerState gsamLinearClamp      : register(s3);
  49. SamplerState gsamAnisotropicWrap  : register(s4);
  50. SamplerState gsamAnisotropicClamp : register(s5);
  51. SamplerComparisonState gsamShadow : register(s6);
  52.  
  53. // Constant data that varies per frame.
  54. cbuffer cbPerObject : register(b0)
  55. {
  56.     float4x4 gWorld;
  57.     float4x4 gTexTransform;
  58.     uint gMaterialIndex;
  59.     uint gObjPad0;
  60.     uint gObjPad1;
  61.     uint gObjPad2;
  62. };
  63.  
  64. cbuffer cbPass : register(b1)
  65. {
  66.     float4x4 gView;
  67.     float4x4 gInvView;
  68.     float4x4 gProj;
  69.     float4x4 gInvProj;
  70.     float4x4 gViewProj;
  71.     float4x4 gInvViewProj;
  72.     float4x4 gShadowTransform;
  73.     float3 gEyePosW;
  74.     float cbPerObjectPad1;
  75.     float2 gRenderTargetSize;
  76.     float2 gInvRenderTargetSize;
  77.     float gNearZ;
  78.     float gFarZ;
  79.     float gTotalTime;
  80.     float gDeltaTime;
  81.     float4 gAmbientLight;
  82.  
  83.     // Indices [0, NUM_DIR_LIGHTS) are directional lights;
  84.     // indices [NUM_DIR_LIGHTS, NUM_DIR_LIGHTS+NUM_POINT_LIGHTS) are point lights;
  85.     // indices [NUM_DIR_LIGHTS+NUM_POINT_LIGHTS, NUM_DIR_LIGHTS+NUM_POINT_LIGHT+NUM_SPOT_LIGHTS)
  86.     // are spot lights for a maximum of MaxLights per object.
  87.     Light gLights[MaxLights];
  88. };
  89.  
  90. //---------------------------------------------------------------------------------------
  91. // Transforms a normal map sample to world space.
  92. //---------------------------------------------------------------------------------------
  93. float3 NormalSampleToWorldSpace(float3 normalMapSample, float3 unitNormalW, float3 tangentW)
  94. {
  95.     // Uncompress each component from [0,1] to [-1,1].
  96.     float3 normalT = 2.0f*normalMapSample - 1.0f;
  97.  
  98.     // Build orthonormal basis.
  99.     float3 N = unitNormalW;
  100.     float3 T = normalize(tangentW - dot(tangentW, N)*N);
  101.     float3 B = cross(N, T);
  102.  
  103.     float3x3 TBN = float3x3(T, B, N);
  104.  
  105.     // Transform from tangent space to world space.
  106.     float3 bumpedNormalW = mul(normalT, TBN);
  107.  
  108.     return bumpedNormalW;
  109. }
  110.  
  111. //---------------------------------------------------------------------------------------
  112. // PCF for shadow mapping.
  113. //---------------------------------------------------------------------------------------
  114.  
  115. float CalcShadowFactor(float4 shadowPosH)
  116. {
  117.     // Complete projection by doing division by w.
  118.     shadowPosH.xyz /= shadowPosH.w;
  119.  
  120.     // Depth in NDC space.
  121.     float depth = shadowPosH.z;
  122.  
  123.     uint width, height, numMips;
  124.     gShadowMap.GetDimensions(0, width, height, numMips);
  125.  
  126.     // Texel size.
  127.     float dx = 1.0f / (float)width;
  128.  
  129.     float percentLit = 0.0f;
  130.     const float2 offsets[9] =
  131.     {
  132.         float2(-dx,  -dx), float2(0.0f,  -dx), float2(dx,  -dx),
  133.         float2(-dx, 0.0f), float2(0.0f, 0.0f), float2(dx, 0.0f),
  134.         float2(-dx,  +dx), float2(0.0f,  +dx), float2(dx,  +dx)
  135.     };
  136.  
  137.     [unroll]
  138.     for(int i = 0; i < 9; ++i)
  139.     {
  140.         percentLit += gShadowMap.SampleCmpLevelZero(gsamShadow,
  141.             shadowPosH.xy + offsets[i], depth).r;
  142.     }
  143.     
  144.     return percentLit / 9.0f;
  145. }
  146.  
  147.