暫無描述
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

StpCommon.hlsl 5.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216
  1. // This is necessary to prevent Unity from deciding that our default config logic is actually an include guard declaration
  2. #ifndef STP_COMMON_UNITY_INCLUDE_GUARD
  3. #define STP_COMMON_UNITY_INCLUDE_GUARD
  4. ///
  5. /// Spatial-Temporal Post-Processing (STP) Common Shader Code
  6. ///
  7. /// This file provides configuration defines and other common utilities associated with STP
  8. /// See STP.cs for more details on how this shader code interacts with C#
  9. ///
  10. /// Usage:
  11. /// - Add control defines
  12. /// - Include this file in a shader pass associated with STP
  13. /// - Call relevant STP function
  14. ///
  15. /// By default, no shader functions are available until they are specifically requested via define.
  16. ///
  17. /// The following defines can be used to enable shader functionality:
  18. /// - STP_PAT
  19. /// - Enables the "Pattern" pass
  20. /// - STP_DIL
  21. /// - Enables the "Dilation" pass
  22. /// - STP_SAA
  23. /// - Enables the "Spatial Anti-Aliasing" pass
  24. /// - STP_TAA
  25. /// - Enables the "TAA" pass
  26. // Indicate that we'll be using the HLSL implementation of STP
  27. #define STP_HLSL 1
  28. #define STP_GPU 1
  29. // Disable grain since we don't currently have a way to integrate this with the rest of Unity's post processing
  30. #define STP_GRAIN 0
  31. // Enable the minimum precision path when supported by the shader environment.
  32. #if REAL_IS_HALF || defined(UNITY_DEVICE_SUPPORTS_NATIVE_16BIT)
  33. #define STP_MEDIUM 1
  34. #endif
  35. // Mobile platforms use a simplified version of STP to reduce runtime overhead.
  36. #if defined(SHADER_API_MOBILE) || defined(SHADER_API_SWITCH)
  37. #define STP_TAA_Q 0
  38. #endif
  39. #if defined(SHADER_API_SWITCH)
  40. #define STP_BUG_SAT_INF 1
  41. #endif
  42. // Enable workarounds that help us avoid issues on Metal
  43. #if defined(SHADER_API_METAL)
  44. // Relying on infinity behavior causes issues in the on-screen inline pass calculations
  45. // We expect this option to be required on Metal because the shading language spec states that the fast-math
  46. // option is on by default which disables support for proper INF handling.
  47. #define STP_BUG_SAT_INF 1
  48. #endif
  49. #if defined(SHADER_API_PSSL)
  50. #define STP_BUG_SAT_INF 1
  51. #endif
  52. #if defined(UNITY_DEVICE_SUPPORTS_NATIVE_16BIT)
  53. #define STP_16BIT 1
  54. #if defined(SHADER_API_PSSL)
  55. #define STP_BUG_PRX 1
  56. #endif
  57. #else
  58. #define STP_32BIT 1
  59. #endif
  60. #if defined(ENABLE_DEBUG_MODE)
  61. #define STP_BUG 1
  62. #endif
  63. // Include the STP HLSL files
  64. #include "Packages/com.unity.render-pipelines.core/Runtime/STP/Stp.hlsl"
  65. #include "Packages/com.unity.render-pipelines.core/Runtime/STP/STP.cs.hlsl"
  66. // Include TextureXR.hlsl for XR macros
  67. #include "Packages/com.unity.render-pipelines.core/ShaderLibrary/TextureXR.hlsl"
  68. //
  69. // Common
  70. //
  71. #if defined(ENABLE_LARGE_KERNEL)
  72. #define STP_GROUP_SIZE 128
  73. #define STP_GROUP_SIZE_SHIFT_X 3
  74. #define STP_GROUP_SIZE_SHIFT_Y 4
  75. #else
  76. #define STP_GROUP_SIZE 64
  77. #define STP_GROUP_SIZE_SHIFT_X 3
  78. #define STP_GROUP_SIZE_SHIFT_Y 3
  79. #endif
  80. #if defined(STP_16BIT)
  81. StpW2 ComputeGroupPos(StpW2 groupId)
  82. {
  83. return StpW2(
  84. groupId.x << StpW1_(STP_GROUP_SIZE_SHIFT_X),
  85. groupId.y << StpW1_(STP_GROUP_SIZE_SHIFT_Y)
  86. );
  87. }
  88. #else
  89. StpMU2 ComputeGroupPos(StpMU2 groupId)
  90. {
  91. return StpMU2(
  92. groupId.x << StpMU1_(STP_GROUP_SIZE_SHIFT_X),
  93. groupId.y << StpMU1_(STP_GROUP_SIZE_SHIFT_Y)
  94. );
  95. }
  96. #endif
  97. #define STP_COMMON_CONSTANT asuint(_StpCommonConstant.x)
  98. #define STP_ZBUFFER_PARAMS_Z _StpCommonConstant.y
  99. #define STP_ZBUFFER_PARAMS_W _StpCommonConstant.z
  100. TEXTURE2D(_StpBlueNoiseIn);
  101. RW_TEXTURE2D_X(float4, _StpDebugOut);
  102. SAMPLER(s_point_clamp_sampler);
  103. SAMPLER(s_linear_clamp_sampler);
  104. SAMPLER(s_linear_repeat_sampler);
  105. #if defined(STP_32BIT)
  106. StpMU1 StpBfeF(StpMU1 data, StpMU1 offset, StpMU1 numBits)
  107. {
  108. StpMU1 mask = (StpMU1(1) << numBits) - StpMU1(1);
  109. return (data >> offset) & mask;
  110. }
  111. StpMU1 StpBfiF(StpMU1 mask, StpMU1 src, StpMU1 dst)
  112. {
  113. return (src & mask) | (dst & ~mask);
  114. }
  115. StpMU2 StpRemapLaneTo8x16F(StpMU1 i)
  116. {
  117. return StpMU2(StpBfiF(StpMU1(1), i, StpBfeF(i, StpMU1(2), StpMU1(3))),
  118. StpBfiF(StpMU1(3), StpBfeF(i, StpMU1(1), StpMU1(2)), StpBfeF(i, StpMU1(3), StpMU1(4))));
  119. }
  120. StpMU1 DecodeNoiseWidthMinusOneF(StpMU1 param)
  121. {
  122. return param & StpMU1(0xFF);
  123. }
  124. #endif
  125. #if defined(STP_16BIT)
  126. StpW1 StpBfeH(StpW1 data, StpW1 offset, StpW1 numBits)
  127. {
  128. StpW1 mask = (StpW1(1) << numBits) - StpW1(1);
  129. return (data >> offset) & mask;
  130. }
  131. StpW1 StpBfiH(StpW1 mask, StpW1 src, StpW1 dst)
  132. {
  133. return (src & mask) | (dst & ~mask);
  134. }
  135. StpW2 StpRemapLaneTo8x16H(StpW1 i)
  136. {
  137. return StpW2(StpBfiH(StpW1(1), i, StpBfeH(i, StpW1(2), StpW1(3))),
  138. StpBfiH(StpW1(3), StpBfeH(i, StpW1(1), StpW1(2)), StpBfeH(i, StpW1(3), StpW1(4))));
  139. }
  140. StpW1 DecodeNoiseWidthMinusOneH(StpW1 param)
  141. {
  142. return param & StpW1(0xFF);
  143. }
  144. #endif
  145. bool DecodeHasValidHistory(uint param)
  146. {
  147. return (param >> 8) & 1;
  148. }
  149. uint DecodeStencilMask(uint param)
  150. {
  151. return (param >> 16) & 0xFF;
  152. }
  153. uint DecodeDebugViewIndex(uint param)
  154. {
  155. return (param >> 24) & 0xFF;
  156. }
  157. #if defined(STP_32BIT)
  158. StpMF1 StpDitF1(StpMU2 o)
  159. {
  160. StpMU1 noiseWidthMinusOne = DecodeNoiseWidthMinusOneF(StpMU1(STP_COMMON_CONSTANT));
  161. return (StpMF1)LOAD_TEXTURE2D_LOD(_StpBlueNoiseIn, o & noiseWidthMinusOne, 0).a;
  162. }
  163. // TODO: Broadcast one value as all three outputs a bug that will effect 'STP_GRAIN=3' output.
  164. StpMF3 StpDitF3(StpMU2 o) { return (StpMF3)StpDitF1(o); }
  165. #endif
  166. // NOTE: This function is used by both the 32-bit path, and the 16-bit path (when various workarounds are active)
  167. void StpBugF(StpU3 p, StpF4 c)
  168. {
  169. if (p.z == DecodeDebugViewIndex(STP_COMMON_CONSTANT))
  170. _StpDebugOut[COORD_TEXTURE2D_X(p.xy)] = c;
  171. }
  172. #if defined(STP_16BIT)
  173. StpH1 StpDitH1(StpW2 o)
  174. {
  175. StpW1 noiseWidthMinusOne = DecodeNoiseWidthMinusOneH(StpW1(STP_COMMON_CONSTANT));
  176. return (StpH1)LOAD_TEXTURE2D_LOD(_StpBlueNoiseIn, o & noiseWidthMinusOne, 0).a;
  177. }
  178. // TODO: Broadcast one value as all three outputs a bug that will effect 'STP_GRAIN=3' output.
  179. StpH3 StpDitH3(StpW2 o) { return (StpH3)StpDitH1(o); }
  180. #endif
  181. #endif // STP_COMMON_UNITY_INCLUDE_GUARD