ShadowProject.bsl 8.9 KB

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