ShadowProject.bsl 8.8 KB

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