ShadowProject.bsl 9.4 KB

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