ShadowProject.bsl 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222
  1. #include "$ENGINE$/GBufferInput.bslinc"
  2. #include "$ENGINE$/ShadowProjectionCommon.bslinc"
  3. Technique
  4. : inherits("GBufferInput")
  5. : inherits("ShadowProjectionCommon") =
  6. {
  7. Pass =
  8. {
  9. Fragment =
  10. {
  11. Texture2D gShadowTex;
  12. SamplerState gShadowSampler;
  13. cbuffer Params
  14. {
  15. // Transform a point in mixed space (xy - clip space, z - view space) to a point
  16. // in shadow space
  17. float4x4 gMixedToShadowSpace;
  18. float2 gShadowMapSize;
  19. float2 gShadowMapSizeInv;
  20. float gSoftTransitionScale;
  21. float gFadePercent;
  22. float gFadePlaneDepth;
  23. float gInvFadePlaneRange;
  24. };
  25. // Converts a set of shadow depths into occlusion values, where 1 means scene object is occluded and 0
  26. // not occluded. Values between 1 and 0 are used for soft transitions on receivers that are near casters.
  27. float4 getOcclusion(float4 shadowDepth, float sceneDepth)
  28. {
  29. // Offset the shadow a bit to reduce shadow acne and use scale for soft transitions.
  30. // Visualization (Mathematica): Plot[1.0 - Clip[(500 - x)*0.5 + 1, {0, 1}], {x, 480, 520}]
  31. return 1.0f - saturate((shadowDepth - sceneDepth) * gSoftTransitionScale + 1);
  32. }
  33. // Takes UV coordinates as input and returns a location to sample from, and a fraction
  34. // that can be used for bilinear interpolation between the samples. Returned sample
  35. // center is always located on a border between texels, in UV space.
  36. float2 getFilteringInfo(float2 uv, out float2 fraction)
  37. {
  38. // UV to texel position
  39. float2 texelXY = uv * gShadowMapSize;
  40. // -0.5f offset because UV (0, 0) maps to (-0.5, -0.5) texel position
  41. texelXY -= 0.5f;
  42. // Get fraction to use for interpolation
  43. fraction = frac(texelXY);
  44. // Get center location to gather from (in UV coordinates)
  45. return (floor(texelXY) + 0.5f) * gShadowMapSizeInv;
  46. }
  47. float PCF1x1(float2 uv, float sceneDepth)
  48. {
  49. float depthSample = gShadowTex.Sample(gShadowSampler, uv).r;
  50. return getOcclusion(depthSample.rrrr, sceneDepth).r;
  51. }
  52. float PCF2x2(float2 uv, float sceneDepth)
  53. {
  54. float2 fraction;
  55. float2 sampleCenter = getFilteringInfo(uv, fraction);
  56. // Gather four samples. Samples are returned in counter-clockwise order, starting with lower left
  57. float4 depthSamples = gShadowTex.GatherRed(gShadowSampler, sampleCenter);
  58. // Convert samples to occlusion
  59. float4 occlusion = getOcclusion(depthSamples, sceneDepth);
  60. // Perform bilinear interpolation
  61. float2 lerpHorz = lerp(occlusion.wx, occlusion.zy, fraction.xx);
  62. return lerp(lerpHorz.x, lerpHorz.y, fraction.y);
  63. }
  64. float PCF4x4(float2 uv, float sceneDepth)
  65. {
  66. float2 fraction;
  67. float2 sampleCenter = getFilteringInfo(uv, fraction);
  68. // Gather 16 samples in four 2x2 gathers. Samples are returned in counter-clockwise order, starting with lower left.
  69. // Gathers are performed in clockwise order, starting with top left block.
  70. float4 topLeftSamples = gShadowTex.GatherRed(gShadowSampler, sampleCenter, int2(-1, -1));
  71. float4 topRightSamples = gShadowTex.GatherRed(gShadowSampler, sampleCenter, int2(1, -1));
  72. float4 botLeftSamples = gShadowTex.GatherRed(gShadowSampler, sampleCenter, int2(-1, 1));
  73. float4 botRightSamples = gShadowTex.GatherRed(gShadowSampler, sampleCenter, int2(1, 1));
  74. // Convert samples to occlusion
  75. float4 topLeftOcclusion = getOcclusion(topLeftSamples, sceneDepth);
  76. float4 topRightOcclusion = getOcclusion(topRightSamples, sceneDepth);
  77. float4 botLeftOcclusion = getOcclusion(botLeftSamples, sceneDepth);
  78. float4 botRightOcclusion = getOcclusion(botRightSamples, sceneDepth);
  79. // Get the average occusion value. Fraction only needs to be applied to edge samples.
  80. //// Acculate occlusion per row
  81. float4 rowOcclusion;
  82. //// Add column 1, top to bottom
  83. rowOcclusion.x = topLeftOcclusion.w * (1.0f - fraction.x);
  84. rowOcclusion.y = topLeftOcclusion.x * (1.0f - fraction.x);
  85. rowOcclusion.z = botLeftOcclusion.w * (1.0f - fraction.x);
  86. rowOcclusion.w = botLeftOcclusion.x * (1.0f - fraction.x);
  87. //// Add column 2 & 3, top to bottom
  88. rowOcclusion.x += topLeftOcclusion.z + topRightOcclusion.w;
  89. rowOcclusion.y += topLeftOcclusion.y + topRightOcclusion.x;
  90. rowOcclusion.z += botLeftOcclusion.z + botRightOcclusion.w;
  91. rowOcclusion.w += botLeftOcclusion.y + botRightOcclusion.x;
  92. //// Add column 4, top to bottom
  93. rowOcclusion.x += topRightOcclusion.z * fraction.x;
  94. rowOcclusion.y += topRightOcclusion.y * fraction.x;
  95. rowOcclusion.z += botRightOcclusion.z * fraction.x;
  96. rowOcclusion.w += botRightOcclusion.w * fraction.x;
  97. //// Accumulate occlusion per columns
  98. float4 occlusionAccumulator = dot(rowOcclusion, float4(1.0f - fraction.y, 1.0f, 1.0f, fraction.y));
  99. // Calc average occlusion using a 3x3 area and return
  100. return occlusionAccumulator * (1.0f / 9.0f);
  101. }
  102. // Accumulates samples for all columns in a row, for 6x2 samples. Samples are provided in three 2x2
  103. // blocks. Samples in a block are in counter-clockwise order, starting with lower left. Returns two
  104. // rows with their accumulated values, starting with top row.
  105. float2 accumulateRows6x2(float fraction, float4 left, float4 mid, float4 right)
  106. {
  107. float2 row;
  108. // Column 1, top to bottom
  109. row.x = left.w * (1.0f - fraction);
  110. row.y = left.x * (1.0f - fraction);
  111. // Columns 2, 3, 4, 5, top to bottom
  112. row.x += left.z + mid.w + mid.z + right.w;
  113. row.y += left.y + mid.x + mid.y + right.x;
  114. // Column 6, top to bottom
  115. row.x += right.z * fraction;
  116. row.y += right.y * fraction;
  117. return row;
  118. }
  119. float PCF6x6(float2 uv, float sceneDepth)
  120. {
  121. float2 fraction;
  122. float2 sampleCenter = getFilteringInfo(uv, fraction);
  123. // Gather 36 samples in nine 2x2 gathers. Gathers are performed in clockwise order, starting with top left block.
  124. // Every three gathers (one row), the values are accumulated to their corresponding row.
  125. // Samples for individual gather operations are returned in counter-clockwise order, starting with lower left.
  126. float2 rows[3];
  127. [unroll]
  128. for(int i = 0; i < 3; i++)
  129. {
  130. int y = -2 + i * 2;
  131. float4 left = getOcclusion(gShadowTex.GatherRed(gShadowSampler, sampleCenter, int2(-2, y)), sceneDepth);
  132. float4 middle = getOcclusion(gShadowTex.GatherRed(gShadowSampler, sampleCenter, int2(0, y)), sceneDepth);
  133. float4 right = getOcclusion(gShadowTex.GatherRed(gShadowSampler, sampleCenter, int2(2, y)), sceneDepth);
  134. rows[i] = accumulateRows6x2(fraction.x, left, middle, right);
  135. }
  136. // Accumulate all rows
  137. float occlusionAccumulator;
  138. occlusionAccumulator = rows[0].x * (1.0f - fraction.y);
  139. occlusionAccumulator += rows[0].y + rows[1].x + rows[1].y + rows[2].x;
  140. occlusionAccumulator += rows[2].y * fraction.y;
  141. // Calc average occlusion using 5x5 area and return
  142. return occlusionAccumulator * (1.0f / 25.0f);
  143. }
  144. float4 main(VStoFS input, uint sampleIdx : SV_SampleIndex) : SV_Target0
  145. {
  146. // Get depth & calculate world position
  147. #if MSAA_COUNT > 1
  148. uint2 screenPos = NDCToScreen(input.position.xy);
  149. float deviceZ = gDepthBufferTex.Load(screenPos, sampleIdx).r;
  150. #else
  151. float2 screenUV = NDCToUV(input.position.xy);
  152. float deviceZ = gDepthBufferTex.Sample(gDepthBufferSamp, screenUV).r;
  153. #endif
  154. float depth = convertFromDeviceZ(deviceZ);
  155. float4 mixedSpacePos = float4(input.position.xy * -depth, depth, 1);
  156. float4 shadowPosition = mul(gMixedToShadowSpace, mixedSpacePos);
  157. shadowPosition.xy /= shadowPosition.w;
  158. // Clamp depth range because pixels in the shadow map that haven't been rendered to will have a value of 1,
  159. // and we want those to remain unshadowed.
  160. float lightSpaceDepth = min(shadowPosition.z, 0.999999f);
  161. float occlusion = 0.0f;
  162. #if SHADOW_QUALITY <= 1
  163. occlusion = PCF1x1(shadowPosition.xy, lightSpaceDepth);
  164. #elif SHADOW_QUALITY == 2
  165. occlusion = PCF2x2(shadowPosition.xy, lightSpaceDepth);
  166. #elif SHADOW_QUALITY == 3
  167. occlusion = PCF4x4(shadowPosition.xy, lightSpaceDepth);
  168. #else
  169. occlusion = PCF6x6(shadowPosition.xy, lightSpaceDepth);
  170. #endif
  171. float alpha = 1.0f;
  172. #if FADE_PLANE
  173. alpha = 1.0f - saturate((depth - gFadePlaneDepth) * gInvFadePlaneRange);
  174. #endif
  175. occlusion *= gFadePercent;
  176. // Encode to get better precision in the blacks, similar to gamma correction but cheaper to execute
  177. return float4(sqrt(occlusion), 0.0f, 0.0f, alpha);
  178. }
  179. };
  180. };
  181. };