ShadowProject.bsl 8.3 KB

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