StdLib.hlsl 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322
  1. // Because this framework is supposed to work with the legacy render pipelines AND scriptable render
  2. // pipelines we can't use Unity's shader libraries (some scriptable pipelines come with their own
  3. // shader lib). So here goes a minimal shader lib only used for post-processing to ensure good
  4. // compatibility with all pipelines.
  5. #ifndef UNITY_POSTFX_STDLIB
  6. #define UNITY_POSTFX_STDLIB
  7. // -----------------------------------------------------------------------------
  8. // API macros
  9. #if defined(SHADER_API_PSSL)
  10. #include "API/PSSL.hlsl"
  11. #elif defined(SHADER_API_XBOXONE)
  12. #include "API/XboxOne.hlsl"
  13. #elif defined(SHADER_API_D3D11)
  14. #include "API/D3D11.hlsl"
  15. #elif defined(SHADER_API_D3D12)
  16. #include "API/D3D12.hlsl"
  17. #elif defined(SHADER_API_D3D9) || defined(SHADER_API_D3D11_9X)
  18. #include "API/D3D9.hlsl"
  19. #elif defined(SHADER_API_VULKAN)
  20. #include "API/Vulkan.hlsl"
  21. #elif defined(SHADER_API_SWITCH)
  22. #include "API/Switch.hlsl"
  23. #elif defined(SHADER_API_METAL)
  24. #include "API/Metal.hlsl"
  25. #elif defined(SHADER_API_PSP2)
  26. #include "API/PSP2.hlsl"
  27. #else
  28. #include "API/OpenGL.hlsl"
  29. #endif
  30. #if defined(SHADER_API_PSSL) || defined(SHADER_API_XBOXONE) || defined(SHADER_API_SWITCH) || defined(SHADER_API_PSP2)
  31. #define SHADER_API_CONSOLE
  32. #endif
  33. // -----------------------------------------------------------------------------
  34. // Constants
  35. #define HALF_MAX 65504.0 // (2 - 2^-10) * 2^15
  36. #define HALF_MAX_MINUS1 65472.0 // (2 - 2^-9) * 2^15
  37. #define EPSILON 1.0e-4
  38. #define PI 3.14159265359
  39. #define TWO_PI 6.28318530718
  40. #define FOUR_PI 12.56637061436
  41. #define INV_PI 0.31830988618
  42. #define INV_TWO_PI 0.15915494309
  43. #define INV_FOUR_PI 0.07957747155
  44. #define HALF_PI 1.57079632679
  45. #define INV_HALF_PI 0.636619772367
  46. #define FLT_EPSILON 1.192092896e-07 // Smallest positive number, such that 1.0 + FLT_EPSILON != 1.0
  47. #define FLT_MIN 1.175494351e-38 // Minimum representable positive floating-point number
  48. #define FLT_MAX 3.402823466e+38 // Maximum representable floating-point number
  49. // -----------------------------------------------------------------------------
  50. // Compatibility functions
  51. #if (SHADER_TARGET < 50 && !defined(SHADER_API_PSSL))
  52. float rcp(float value)
  53. {
  54. return 1.0 / value;
  55. }
  56. #endif
  57. #if defined(SHADER_API_GLES)
  58. #define mad(a, b, c) (a * b + c)
  59. #endif
  60. #ifndef INTRINSIC_MINMAX3
  61. float Min3(float a, float b, float c)
  62. {
  63. return min(min(a, b), c);
  64. }
  65. float2 Min3(float2 a, float2 b, float2 c)
  66. {
  67. return min(min(a, b), c);
  68. }
  69. float3 Min3(float3 a, float3 b, float3 c)
  70. {
  71. return min(min(a, b), c);
  72. }
  73. float4 Min3(float4 a, float4 b, float4 c)
  74. {
  75. return min(min(a, b), c);
  76. }
  77. float Max3(float a, float b, float c)
  78. {
  79. return max(max(a, b), c);
  80. }
  81. float2 Max3(float2 a, float2 b, float2 c)
  82. {
  83. return max(max(a, b), c);
  84. }
  85. float3 Max3(float3 a, float3 b, float3 c)
  86. {
  87. return max(max(a, b), c);
  88. }
  89. float4 Max3(float4 a, float4 b, float4 c)
  90. {
  91. return max(max(a, b), c);
  92. }
  93. #endif // INTRINSIC_MINMAX3
  94. // https://twitter.com/SebAaltonen/status/878250919879639040
  95. // madd_sat + madd
  96. float FastSign(float x)
  97. {
  98. return saturate(x * FLT_MAX + 0.5) * 2.0 - 1.0;
  99. }
  100. float2 FastSign(float2 x)
  101. {
  102. return saturate(x * FLT_MAX + 0.5) * 2.0 - 1.0;
  103. }
  104. float3 FastSign(float3 x)
  105. {
  106. return saturate(x * FLT_MAX + 0.5) * 2.0 - 1.0;
  107. }
  108. float4 FastSign(float4 x)
  109. {
  110. return saturate(x * FLT_MAX + 0.5) * 2.0 - 1.0;
  111. }
  112. // Using pow often result to a warning like this
  113. // "pow(f, e) will not work for negative f, use abs(f) or conditionally handle negative values if you expect them"
  114. // PositivePow remove this warning when you know the value is positive and avoid inf/NAN.
  115. float PositivePow(float base, float power)
  116. {
  117. return pow(max(abs(base), float(FLT_EPSILON)), power);
  118. }
  119. float2 PositivePow(float2 base, float2 power)
  120. {
  121. return pow(max(abs(base), float2(FLT_EPSILON, FLT_EPSILON)), power);
  122. }
  123. float3 PositivePow(float3 base, float3 power)
  124. {
  125. return pow(max(abs(base), float3(FLT_EPSILON, FLT_EPSILON, FLT_EPSILON)), power);
  126. }
  127. float4 PositivePow(float4 base, float4 power)
  128. {
  129. return pow(max(abs(base), float4(FLT_EPSILON, FLT_EPSILON, FLT_EPSILON, FLT_EPSILON)), power);
  130. }
  131. // NaN checker
  132. // /Gic isn't enabled on fxc so we can't rely on isnan() anymore
  133. bool IsNan(float x)
  134. {
  135. // For some reason the following tests outputs "internal compiler error" randomly on desktop
  136. // so we'll use a safer but slightly slower version instead :/
  137. //return (x <= 0.0 || 0.0 <= x) ? false : true;
  138. return (x < 0.0 || x > 0.0 || x == 0.0) ? false : true;
  139. }
  140. bool AnyIsNan(float2 x)
  141. {
  142. return IsNan(x.x) || IsNan(x.y);
  143. }
  144. bool AnyIsNan(float3 x)
  145. {
  146. return IsNan(x.x) || IsNan(x.y) || IsNan(x.z);
  147. }
  148. bool AnyIsNan(float4 x)
  149. {
  150. return IsNan(x.x) || IsNan(x.y) || IsNan(x.z) || IsNan(x.w);
  151. }
  152. // -----------------------------------------------------------------------------
  153. // Std unity data
  154. float4x4 unity_CameraProjection;
  155. float4x4 unity_MatrixVP;
  156. float4x4 unity_ObjectToWorld;
  157. float4x4 unity_WorldToCamera;
  158. float3 _WorldSpaceCameraPos;
  159. float4 _ProjectionParams; // x: 1 (-1 flipped), y: near, z: far, w: 1/far
  160. float4 unity_ColorSpaceLuminance;
  161. float4 unity_DeltaTime; // x: dt, y: 1/dt, z: smoothDt, w: 1/smoothDt
  162. float4 unity_OrthoParams; // x: width, y: height, z: unused, w: ortho ? 1 : 0
  163. float4 _ZBufferParams; // x: 1-far/near, y: far/near, z: x/far, w: y/far
  164. float4 _ScreenParams; // x: width, y: height, z: 1+1/width, w: 1+1/height
  165. float4 _Time; // x: t/20, y: t, z: t*2, w: t*3
  166. float4 _SinTime; // x: sin(t/20), y: sin(t), z: sin(t*2), w: sin(t*3)
  167. float4 _CosTime; // x: cos(t/20), y: cos(t), z: cos(t*2), w: cos(t*3)
  168. // -----------------------------------------------------------------------------
  169. // Std functions
  170. // Z buffer depth to linear 0-1 depth
  171. // Handles orthographic projection correctly
  172. float Linear01Depth(float z)
  173. {
  174. float isOrtho = unity_OrthoParams.w;
  175. float isPers = 1.0 - unity_OrthoParams.w;
  176. z *= _ZBufferParams.x;
  177. return (1.0 - isOrtho * z) / (isPers * z + _ZBufferParams.y);
  178. }
  179. float LinearEyeDepth(float z)
  180. {
  181. return rcp(_ZBufferParams.z * z + _ZBufferParams.w);
  182. }
  183. // Clamp HDR value within a safe range
  184. half3 SafeHDR(half3 c)
  185. {
  186. return min(c, HALF_MAX);
  187. }
  188. half4 SafeHDR(half4 c)
  189. {
  190. return min(c, HALF_MAX);
  191. }
  192. // Decode normals stored in _CameraDepthNormalsTexture
  193. float3 DecodeViewNormalStereo(float4 enc4)
  194. {
  195. float kScale = 1.7777;
  196. float3 nn = enc4.xyz * float3(2.0 * kScale, 2.0 * kScale, 0) + float3(-kScale, -kScale, 1);
  197. float g = 2.0 / dot(nn.xyz, nn.xyz);
  198. float3 n;
  199. n.xy = g * nn.xy;
  200. n.z = g - 1.0;
  201. return n;
  202. }
  203. // Interleaved gradient function from Jimenez 2014
  204. // http://www.iryoku.com/next-generation-post-processing-in-call-of-duty-advanced-warfare
  205. float GradientNoise(float2 uv)
  206. {
  207. uv = floor(uv * _ScreenParams.xy);
  208. float f = dot(float2(0.06711056, 0.00583715), uv);
  209. return frac(52.9829189 * frac(f));
  210. }
  211. // Vertex manipulation
  212. float2 TransformTriangleVertexToUV(float2 vertex)
  213. {
  214. float2 uv = (vertex + 1.0) * 0.5;
  215. return uv;
  216. }
  217. #include "xRLib.hlsl"
  218. // -----------------------------------------------------------------------------
  219. // Default vertex shaders
  220. struct AttributesDefault
  221. {
  222. float3 vertex : POSITION;
  223. };
  224. struct VaryingsDefault
  225. {
  226. float4 vertex : SV_POSITION;
  227. float2 texcoord : TEXCOORD0;
  228. float2 texcoordStereo : TEXCOORD1;
  229. #if STEREO_INSTANCING_ENABLED
  230. uint stereoTargetEyeIndex : SV_RenderTargetArrayIndex;
  231. #endif
  232. };
  233. #if STEREO_INSTANCING_ENABLED
  234. float _DepthSlice;
  235. #endif
  236. VaryingsDefault VertDefault(AttributesDefault v)
  237. {
  238. VaryingsDefault o;
  239. o.vertex = float4(v.vertex.xy, 0.0, 1.0);
  240. o.texcoord = TransformTriangleVertexToUV(v.vertex.xy);
  241. #if UNITY_UV_STARTS_AT_TOP
  242. o.texcoord = o.texcoord * float2(1.0, -1.0) + float2(0.0, 1.0);
  243. #endif
  244. o.texcoordStereo = TransformStereoScreenSpaceTex(o.texcoord, 1.0);
  245. return o;
  246. }
  247. float4 _UVTransform; // xy: scale, wz: translate
  248. #if STEREO_DOUBLEWIDE_TARGET
  249. float4 _PosScaleOffset; // xy: scale, wz: offset
  250. #endif
  251. VaryingsDefault VertUVTransform(AttributesDefault v)
  252. {
  253. VaryingsDefault o;
  254. #if STEREO_DOUBLEWIDE_TARGET
  255. o.vertex = float4(v.vertex.xy * _PosScaleOffset.xy + _PosScaleOffset.zw, 0.0, 1.0);
  256. #else
  257. o.vertex = float4(v.vertex.xy, 0.0, 1.0);
  258. #endif
  259. o.texcoord = TransformTriangleVertexToUV(v.vertex.xy) * _UVTransform.xy + _UVTransform.zw;
  260. o.texcoordStereo = TransformStereoScreenSpaceTex(o.texcoord, 1.0);
  261. #if STEREO_INSTANCING_ENABLED
  262. o.stereoTargetEyeIndex = (uint)_DepthSlice;
  263. #endif
  264. return o;
  265. }
  266. #define TRANSFORM_TEX(tex,name) (tex.xy * name##_ST.xy + name##_ST.zw)
  267. #endif // UNITY_POSTFX_STDLIB