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.

166 lines
4.7 KiB

  1. #ifndef __COMMON__
  2. #define __COMMON__
  3. #include "UnityCG.cginc"
  4. // Mobile: use RGBM instead of float/half RGB
  5. #define USE_RGBM defined(SHADER_API_MOBILE)
  6. #define MOBILE_OR_CONSOLE (defined(SHADER_API_MOBILE) || defined(SHADER_API_PSSL) || defined(SHADER_API_XBOXONE) || defined(SHADER_API_WIIU))
  7. #if defined(SHADER_API_PSSL)
  8. // No support for sampler2D_half on PS4 in 5.4
  9. #define sampler2D_half sampler2D_float
  10. #endif
  11. // -----------------------------------------------------------------------------
  12. // Uniforms
  13. #if defined(SEPARATE_TEXTURE_SAMPLER)
  14. Texture2D _MainTex;
  15. SamplerState sampler_MainTex;
  16. #else
  17. sampler2D _MainTex;
  18. #endif
  19. float4 _MainTex_TexelSize;
  20. float4 _MainTex_ST;
  21. // -----------------------------------------------------------------------------
  22. // Vertex shaders
  23. struct AttributesDefault
  24. {
  25. float4 vertex : POSITION;
  26. float4 texcoord : TEXCOORD0;
  27. };
  28. struct VaryingsDefault
  29. {
  30. float4 pos : SV_POSITION;
  31. float2 uv : TEXCOORD0;
  32. float2 uvSPR : TEXCOORD1; // Single Pass Stereo UVs
  33. };
  34. VaryingsDefault VertDefault(AttributesDefault v)
  35. {
  36. VaryingsDefault o;
  37. o.pos = UnityObjectToClipPos(v.vertex);
  38. o.uv = v.texcoord.xy;
  39. o.uvSPR = UnityStereoScreenSpaceUVAdjust(v.texcoord.xy, _MainTex_ST);
  40. return o;
  41. }
  42. // -----------------------------------------------------------------------------
  43. // Maths stuff
  44. #define HALF_MAX 65504.0
  45. #define EPSILON 1.0e-4
  46. #define UNITY_PI_2 (UNITY_PI * 2.0)
  47. inline half Min3(half3 x) { return min(x.x, min(x.y, x.z)); }
  48. inline half Min3(half x, half y, half z) { return min(x, min(y, z)); }
  49. inline half Max3(half3 x) { return max(x.x, max(x.y, x.z)); }
  50. inline half Max3(half x, half y, half z) { return max(x, max(y, z)); }
  51. inline half Min4(half4 x) { return min(x.x, min(x.y, min(x.z, x.w))); }
  52. inline half Min4(half x, half y, half z, half w) { return min(x, min(y, min(z, w))); }
  53. inline half Max4(half4 x) { return max(x.x, max(x.y, max(x.z, x.w))); }
  54. inline half Max4(half x, half y, half z, half w) { return max(x, max(y, min(z, w))); }
  55. inline half Pow2(half x) { return x * x; }
  56. inline half2 Pow2(half2 x) { return x * x; }
  57. inline half3 Pow2(half3 x) { return x * x; }
  58. inline half4 Pow2(half4 x) { return x * x; }
  59. inline half Pow3(half x) { return x * x * x; }
  60. inline half2 Pow3(half2 x) { return x * x * x; }
  61. inline half3 Pow3(half3 x) { return x * x * x; }
  62. inline half4 Pow3(half4 x) { return x * x * x; }
  63. #ifndef UNITY_STANDARD_BRDF_INCLUDED
  64. inline half Pow4(half x) { return x * x * x * x; }
  65. inline half2 Pow4(half2 x) { return x * x * x * x; }
  66. inline half3 Pow4(half3 x) { return x * x * x * x; }
  67. inline half4 Pow4(half4 x) { return x * x * x * x; }
  68. #endif
  69. // Returns the largest vector of v1 and v2
  70. inline half2 MaxV(half2 v1, half2 v2) { return dot(v1, v1) < dot(v2, v2) ? v2 : v1; }
  71. inline half3 MaxV(half3 v1, half3 v2) { return dot(v1, v1) < dot(v2, v2) ? v2 : v1; }
  72. inline half4 MaxV(half4 v1, half4 v2) { return dot(v1, v1) < dot(v2, v2) ? v2 : v1; }
  73. // Clamp HDR value within a safe range
  74. inline half SafeHDR(half c) { return min(c, HALF_MAX); }
  75. inline half2 SafeHDR(half2 c) { return min(c, HALF_MAX); }
  76. inline half3 SafeHDR(half3 c) { return min(c, HALF_MAX); }
  77. inline half4 SafeHDR(half4 c) { return min(c, HALF_MAX); }
  78. // Compatibility function
  79. #if (SHADER_TARGET < 50 && !defined(SHADER_API_PSSL))
  80. float rcp(float value)
  81. {
  82. return 1.0 / value;
  83. }
  84. #endif
  85. // Tonemapper from http://gpuopen.com/optimized-reversible-tonemapper-for-resolve/
  86. float4 FastToneMap(in float4 color)
  87. {
  88. return float4(color.rgb * rcp(Max3(color.rgb) + 1.), color.a);
  89. }
  90. float4 FastToneMap(in float4 color, in float weight)
  91. {
  92. return float4(color.rgb * rcp(weight * Max3(color.rgb) + 1.), color.a);
  93. }
  94. float4 FastToneUnmap(in float4 color)
  95. {
  96. return float4(color.rgb * rcp(1. - Max3(color.rgb)), color.a);
  97. }
  98. // Interleaved gradient function from Jimenez 2014 http://goo.gl/eomGso
  99. float GradientNoise(float2 uv)
  100. {
  101. uv = floor(uv * _ScreenParams.xy);
  102. float f = dot(float2(0.06711056, 0.00583715), uv);
  103. return frac(52.9829189 * frac(f));
  104. }
  105. // Z buffer depth to linear 0-1 depth
  106. // Handles orthographic projection correctly
  107. float LinearizeDepth(float z)
  108. {
  109. float isOrtho = unity_OrthoParams.w;
  110. float isPers = 1.0 - unity_OrthoParams.w;
  111. z *= _ZBufferParams.x;
  112. return (1.0 - isOrtho * z) / (isPers * z + _ZBufferParams.y);
  113. }
  114. // -----------------------------------------------------------------------------
  115. // RGBM encoding/decoding
  116. half4 EncodeHDR(float3 rgb)
  117. {
  118. #if USE_RGBM
  119. rgb *= 1.0 / 8.0;
  120. float m = max(max(rgb.r, rgb.g), max(rgb.b, 1e-6));
  121. m = ceil(m * 255.0) / 255.0;
  122. return half4(rgb / m, m);
  123. #else
  124. return half4(rgb, 0.0);
  125. #endif
  126. }
  127. float3 DecodeHDR(half4 rgba)
  128. {
  129. #if USE_RGBM
  130. return rgba.rgb * rgba.a * 8.0;
  131. #else
  132. return rgba.rgb;
  133. #endif
  134. }
  135. #endif // __COMMON__