Macros.hlsl 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237
  1. #ifndef UNITY_MACROS_INCLUDED
  2. #define UNITY_MACROS_INCLUDED
  3. // Some shader compiler don't support to do multiple ## for concatenation inside the same macro, it require an indirection.
  4. // This is the purpose of this macro
  5. #define MERGE_NAME(X, Y) X##Y
  6. #define CALL_MERGE_NAME(X, Y) MERGE_NAME(X, Y)
  7. // These define are use to abstract the way we sample into a cubemap array.
  8. // Some platform don't support cubemap array so we fallback on 2D latlong
  9. #ifdef UNITY_NO_CUBEMAP_ARRAY
  10. #define TEXTURECUBE_ARRAY_ABSTRACT TEXTURE2D_ARRAY
  11. #define TEXTURECUBE_ARRAY_PARAM_ABSTRACT TEXTURE2D_ARRAY_PARAM
  12. #define TEXTURECUBE_ARRAY_ARGS_ABSTRACT TEXTURE2D_ARRAY_ARGS
  13. #define SAMPLE_TEXTURECUBE_ARRAY_LOD_ABSTRACT(textureName, samplerName, coord3, index, lod) SAMPLE_TEXTURE2D_ARRAY_LOD(textureName, samplerName, DirectionToLatLongCoordinate(coord3), index, lod)
  14. #else
  15. #define TEXTURECUBE_ARRAY_ABSTRACT TEXTURECUBE_ARRAY
  16. #define TEXTURECUBE_ARRAY_PARAM_ABSTRACT TEXTURECUBE_ARRAY_PARAM
  17. #define TEXTURECUBE_ARRAY_ARGS_ABSTRACT TEXTURECUBE_ARRAY_ARGS
  18. #define SAMPLE_TEXTURECUBE_ARRAY_LOD_ABSTRACT(textureName, samplerName, coord3, index, lod) SAMPLE_TEXTURECUBE_ARRAY_LOD(textureName, samplerName, coord3, index, lod)
  19. #endif
  20. #define PI 3.14159265358979323846
  21. #define TWO_PI 6.28318530717958647693
  22. #define FOUR_PI 12.5663706143591729538
  23. #define INV_PI 0.31830988618379067154
  24. #define INV_TWO_PI 0.15915494309189533577
  25. #define INV_FOUR_PI 0.07957747154594766788
  26. #define HALF_PI 1.57079632679489661923
  27. #define INV_HALF_PI 0.63661977236758134308
  28. #define LOG2_E 1.44269504088896340736
  29. #define MILLIMETERS_PER_METER 1000
  30. #define METERS_PER_MILLIMETER rcp(MILLIMETERS_PER_METER)
  31. #define CENTIMETERS_PER_METER 100
  32. #define METERS_PER_CENTIMETER rcp(CENTIMETERS_PER_METER)
  33. #define FLT_INF asfloat(0x7F800000)
  34. #define FLT_EPS 5.960464478e-8 // 2^-24, machine epsilon: 1 + EPS = 1 (half of the ULP for 1.0f)
  35. #define FLT_MIN 1.175494351e-38 // Minimum normalized positive floating-point number
  36. #define FLT_MAX 3.402823466e+38 // Maximum representable floating-point number
  37. #define HALF_EPS 4.8828125e-4 // 2^-11, machine epsilon: 1 + EPS = 1 (half of the ULP for 1.0f)
  38. #define HALF_MIN 6.103515625e-5 // 2^-14, the same value for 10, 11 and 16-bit: https://www.khronos.org/opengl/wiki/Small_Float_Formats
  39. #define HALF_MAX 65504.0
  40. #define UINT_MAX 0xFFFFFFFFu
  41. #ifdef SHADER_API_GLES
  42. #define GENERATE_INT_FLOAT_1_ARG(FunctionName, Parameter1, FunctionBody) \
  43. float FunctionName(float Parameter1) { FunctionBody; } \
  44. int FunctionName(int Parameter1) { FunctionBody; }
  45. #else
  46. #define GENERATE_INT_FLOAT_1_ARG(FunctionName, Parameter1, FunctionBody) \
  47. float FunctionName(float Parameter1) { FunctionBody; } \
  48. uint FunctionName(uint Parameter1) { FunctionBody; } \
  49. int FunctionName(int Parameter1) { FunctionBody; }
  50. #endif
  51. #define TEMPLATE_1_FLT(FunctionName, Parameter1, FunctionBody) \
  52. float FunctionName(float Parameter1) { FunctionBody; } \
  53. float2 FunctionName(float2 Parameter1) { FunctionBody; } \
  54. float3 FunctionName(float3 Parameter1) { FunctionBody; } \
  55. float4 FunctionName(float4 Parameter1) { FunctionBody; }
  56. #define TEMPLATE_1_HALF(FunctionName, Parameter1, FunctionBody) \
  57. half FunctionName(half Parameter1) { FunctionBody; } \
  58. half2 FunctionName(half2 Parameter1) { FunctionBody; } \
  59. half3 FunctionName(half3 Parameter1) { FunctionBody; } \
  60. half4 FunctionName(half4 Parameter1) { FunctionBody; } \
  61. float FunctionName(float Parameter1) { FunctionBody; } \
  62. float2 FunctionName(float2 Parameter1) { FunctionBody; } \
  63. float3 FunctionName(float3 Parameter1) { FunctionBody; } \
  64. float4 FunctionName(float4 Parameter1) { FunctionBody; }
  65. #ifdef SHADER_API_GLES
  66. #define TEMPLATE_1_INT(FunctionName, Parameter1, FunctionBody) \
  67. int FunctionName(int Parameter1) { FunctionBody; } \
  68. int2 FunctionName(int2 Parameter1) { FunctionBody; } \
  69. int3 FunctionName(int3 Parameter1) { FunctionBody; } \
  70. int4 FunctionName(int4 Parameter1) { FunctionBody; }
  71. #else
  72. #define TEMPLATE_1_INT(FunctionName, Parameter1, FunctionBody) \
  73. int FunctionName(int Parameter1) { FunctionBody; } \
  74. int2 FunctionName(int2 Parameter1) { FunctionBody; } \
  75. int3 FunctionName(int3 Parameter1) { FunctionBody; } \
  76. int4 FunctionName(int4 Parameter1) { FunctionBody; } \
  77. uint FunctionName(uint Parameter1) { FunctionBody; } \
  78. uint2 FunctionName(uint2 Parameter1) { FunctionBody; } \
  79. uint3 FunctionName(uint3 Parameter1) { FunctionBody; } \
  80. uint4 FunctionName(uint4 Parameter1) { FunctionBody; }
  81. #endif
  82. #define TEMPLATE_2_FLT(FunctionName, Parameter1, Parameter2, FunctionBody) \
  83. float FunctionName(float Parameter1, float Parameter2) { FunctionBody; } \
  84. float2 FunctionName(float2 Parameter1, float2 Parameter2) { FunctionBody; } \
  85. float3 FunctionName(float3 Parameter1, float3 Parameter2) { FunctionBody; } \
  86. float4 FunctionName(float4 Parameter1, float4 Parameter2) { FunctionBody; }
  87. #define TEMPLATE_2_HALF(FunctionName, Parameter1, Parameter2, FunctionBody) \
  88. half FunctionName(half Parameter1, half Parameter2) { FunctionBody; } \
  89. half2 FunctionName(half2 Parameter1, half2 Parameter2) { FunctionBody; } \
  90. half3 FunctionName(half3 Parameter1, half3 Parameter2) { FunctionBody; } \
  91. half4 FunctionName(half4 Parameter1, half4 Parameter2) { FunctionBody; } \
  92. float FunctionName(float Parameter1, float Parameter2) { FunctionBody; } \
  93. float2 FunctionName(float2 Parameter1, float2 Parameter2) { FunctionBody; } \
  94. float3 FunctionName(float3 Parameter1, float3 Parameter2) { FunctionBody; } \
  95. float4 FunctionName(float4 Parameter1, float4 Parameter2) { FunctionBody; }
  96. #ifdef SHADER_API_GLES
  97. #define TEMPLATE_2_INT(FunctionName, Parameter1, Parameter2, FunctionBody) \
  98. int FunctionName(int Parameter1, int Parameter2) { FunctionBody; } \
  99. int2 FunctionName(int2 Parameter1, int2 Parameter2) { FunctionBody; } \
  100. int3 FunctionName(int3 Parameter1, int3 Parameter2) { FunctionBody; } \
  101. int4 FunctionName(int4 Parameter1, int4 Parameter2) { FunctionBody; }
  102. #else
  103. #define TEMPLATE_2_INT(FunctionName, Parameter1, Parameter2, FunctionBody) \
  104. int FunctionName(int Parameter1, int Parameter2) { FunctionBody; } \
  105. int2 FunctionName(int2 Parameter1, int2 Parameter2) { FunctionBody; } \
  106. int3 FunctionName(int3 Parameter1, int3 Parameter2) { FunctionBody; } \
  107. int4 FunctionName(int4 Parameter1, int4 Parameter2) { FunctionBody; } \
  108. uint FunctionName(uint Parameter1, uint Parameter2) { FunctionBody; } \
  109. uint2 FunctionName(uint2 Parameter1, uint2 Parameter2) { FunctionBody; } \
  110. uint3 FunctionName(uint3 Parameter1, uint3 Parameter2) { FunctionBody; } \
  111. uint4 FunctionName(uint4 Parameter1, uint4 Parameter2) { FunctionBody; }
  112. #endif
  113. #define TEMPLATE_3_FLT(FunctionName, Parameter1, Parameter2, Parameter3, FunctionBody) \
  114. float FunctionName(float Parameter1, float Parameter2, float Parameter3) { FunctionBody; } \
  115. float2 FunctionName(float2 Parameter1, float2 Parameter2, float2 Parameter3) { FunctionBody; } \
  116. float3 FunctionName(float3 Parameter1, float3 Parameter2, float3 Parameter3) { FunctionBody; } \
  117. float4 FunctionName(float4 Parameter1, float4 Parameter2, float4 Parameter3) { FunctionBody; }
  118. #define TEMPLATE_3_HALF(FunctionName, Parameter1, Parameter2, Parameter3, FunctionBody) \
  119. half FunctionName(half Parameter1, half Parameter2, half Parameter3) { FunctionBody; } \
  120. half2 FunctionName(half2 Parameter1, half2 Parameter2, half2 Parameter3) { FunctionBody; } \
  121. half3 FunctionName(half3 Parameter1, half3 Parameter2, half3 Parameter3) { FunctionBody; } \
  122. half4 FunctionName(half4 Parameter1, half4 Parameter2, half4 Parameter3) { FunctionBody; } \
  123. float FunctionName(float Parameter1, float Parameter2, float Parameter3) { FunctionBody; } \
  124. float2 FunctionName(float2 Parameter1, float2 Parameter2, float2 Parameter3) { FunctionBody; } \
  125. float3 FunctionName(float3 Parameter1, float3 Parameter2, float3 Parameter3) { FunctionBody; } \
  126. float4 FunctionName(float4 Parameter1, float4 Parameter2, float4 Parameter3) { FunctionBody; }
  127. #ifdef SHADER_API_GLES
  128. #define TEMPLATE_3_INT(FunctionName, Parameter1, Parameter2, Parameter3, FunctionBody) \
  129. int FunctionName(int Parameter1, int Parameter2, int Parameter3) { FunctionBody; } \
  130. int2 FunctionName(int2 Parameter1, int2 Parameter2, int2 Parameter3) { FunctionBody; } \
  131. int3 FunctionName(int3 Parameter1, int3 Parameter2, int3 Parameter3) { FunctionBody; } \
  132. int4 FunctionName(int4 Parameter1, int4 Parameter2, int4 Parameter3) { FunctionBody; }
  133. #else
  134. #define TEMPLATE_3_INT(FunctionName, Parameter1, Parameter2, Parameter3, FunctionBody) \
  135. int FunctionName(int Parameter1, int Parameter2, int Parameter3) { FunctionBody; } \
  136. int2 FunctionName(int2 Parameter1, int2 Parameter2, int2 Parameter3) { FunctionBody; } \
  137. int3 FunctionName(int3 Parameter1, int3 Parameter2, int3 Parameter3) { FunctionBody; } \
  138. int4 FunctionName(int4 Parameter1, int4 Parameter2, int4 Parameter3) { FunctionBody; } \
  139. uint FunctionName(uint Parameter1, uint Parameter2, uint Parameter3) { FunctionBody; } \
  140. uint2 FunctionName(uint2 Parameter1, uint2 Parameter2, uint2 Parameter3) { FunctionBody; } \
  141. uint3 FunctionName(uint3 Parameter1, uint3 Parameter2, uint3 Parameter3) { FunctionBody; } \
  142. uint4 FunctionName(uint4 Parameter1, uint4 Parameter2, uint4 Parameter3) { FunctionBody; }
  143. #endif
  144. #ifdef SHADER_API_GLES
  145. #define TEMPLATE_SWAP(FunctionName) \
  146. void FunctionName(inout real a, inout real b) { real t = a; a = b; b = t; } \
  147. void FunctionName(inout real2 a, inout real2 b) { real2 t = a; a = b; b = t; } \
  148. void FunctionName(inout real3 a, inout real3 b) { real3 t = a; a = b; b = t; } \
  149. void FunctionName(inout real4 a, inout real4 b) { real4 t = a; a = b; b = t; } \
  150. void FunctionName(inout int a, inout int b) { int t = a; a = b; b = t; } \
  151. void FunctionName(inout int2 a, inout int2 b) { int2 t = a; a = b; b = t; } \
  152. void FunctionName(inout int3 a, inout int3 b) { int3 t = a; a = b; b = t; } \
  153. void FunctionName(inout int4 a, inout int4 b) { int4 t = a; a = b; b = t; } \
  154. void FunctionName(inout bool a, inout bool b) { bool t = a; a = b; b = t; } \
  155. void FunctionName(inout bool2 a, inout bool2 b) { bool2 t = a; a = b; b = t; } \
  156. void FunctionName(inout bool3 a, inout bool3 b) { bool3 t = a; a = b; b = t; } \
  157. void FunctionName(inout bool4 a, inout bool4 b) { bool4 t = a; a = b; b = t; }
  158. #else
  159. #if REAL_IS_HALF
  160. #define TEMPLATE_SWAP(FunctionName) \
  161. void FunctionName(inout real a, inout real b) { real t = a; a = b; b = t; } \
  162. void FunctionName(inout real2 a, inout real2 b) { real2 t = a; a = b; b = t; } \
  163. void FunctionName(inout real3 a, inout real3 b) { real3 t = a; a = b; b = t; } \
  164. void FunctionName(inout real4 a, inout real4 b) { real4 t = a; a = b; b = t; } \
  165. void FunctionName(inout float a, inout float b) { float t = a; a = b; b = t; } \
  166. void FunctionName(inout float2 a, inout float2 b) { float2 t = a; a = b; b = t; } \
  167. void FunctionName(inout float3 a, inout float3 b) { float3 t = a; a = b; b = t; } \
  168. void FunctionName(inout float4 a, inout float4 b) { float4 t = a; a = b; b = t; } \
  169. void FunctionName(inout int a, inout int b) { int t = a; a = b; b = t; } \
  170. void FunctionName(inout int2 a, inout int2 b) { int2 t = a; a = b; b = t; } \
  171. void FunctionName(inout int3 a, inout int3 b) { int3 t = a; a = b; b = t; } \
  172. void FunctionName(inout int4 a, inout int4 b) { int4 t = a; a = b; b = t; } \
  173. void FunctionName(inout uint a, inout uint b) { uint t = a; a = b; b = t; } \
  174. void FunctionName(inout uint2 a, inout uint2 b) { uint2 t = a; a = b; b = t; } \
  175. void FunctionName(inout uint3 a, inout uint3 b) { uint3 t = a; a = b; b = t; } \
  176. void FunctionName(inout uint4 a, inout uint4 b) { uint4 t = a; a = b; b = t; } \
  177. void FunctionName(inout bool a, inout bool b) { bool t = a; a = b; b = t; } \
  178. void FunctionName(inout bool2 a, inout bool2 b) { bool2 t = a; a = b; b = t; } \
  179. void FunctionName(inout bool3 a, inout bool3 b) { bool3 t = a; a = b; b = t; } \
  180. void FunctionName(inout bool4 a, inout bool4 b) { bool4 t = a; a = b; b = t; }
  181. #else
  182. #define TEMPLATE_SWAP(FunctionName) \
  183. void FunctionName(inout real a, inout real b) { real t = a; a = b; b = t; } \
  184. void FunctionName(inout real2 a, inout real2 b) { real2 t = a; a = b; b = t; } \
  185. void FunctionName(inout real3 a, inout real3 b) { real3 t = a; a = b; b = t; } \
  186. void FunctionName(inout real4 a, inout real4 b) { real4 t = a; a = b; b = t; } \
  187. void FunctionName(inout int a, inout int b) { int t = a; a = b; b = t; } \
  188. void FunctionName(inout int2 a, inout int2 b) { int2 t = a; a = b; b = t; } \
  189. void FunctionName(inout int3 a, inout int3 b) { int3 t = a; a = b; b = t; } \
  190. void FunctionName(inout int4 a, inout int4 b) { int4 t = a; a = b; b = t; } \
  191. void FunctionName(inout uint a, inout uint b) { uint t = a; a = b; b = t; } \
  192. void FunctionName(inout uint2 a, inout uint2 b) { uint2 t = a; a = b; b = t; } \
  193. void FunctionName(inout uint3 a, inout uint3 b) { uint3 t = a; a = b; b = t; } \
  194. void FunctionName(inout uint4 a, inout uint4 b) { uint4 t = a; a = b; b = t; } \
  195. void FunctionName(inout bool a, inout bool b) { bool t = a; a = b; b = t; } \
  196. void FunctionName(inout bool2 a, inout bool2 b) { bool2 t = a; a = b; b = t; } \
  197. void FunctionName(inout bool3 a, inout bool3 b) { bool3 t = a; a = b; b = t; } \
  198. void FunctionName(inout bool4 a, inout bool4 b) { bool4 t = a; a = b; b = t; }
  199. #endif
  200. #endif
  201. // MACRO from Legacy Untiy
  202. // Transforms 2D UV by scale/bias property
  203. #define TRANSFORM_TEX(tex, name) ((tex.xy) * name##_ST.xy + name##_ST.zw)
  204. #define GET_TEXELSIZE_NAME(name) (name##_TexelSize)
  205. #if UNITY_REVERSED_Z
  206. # define COMPARE_DEVICE_DEPTH_CLOSER(shadowMapDepth, zDevice) (shadowMapDepth > zDevice)
  207. # define COMPARE_DEVICE_DEPTH_CLOSEREQUAL(shadowMapDepth, zDevice) (shadowMapDepth >= zDevice)
  208. #else
  209. # define COMPARE_DEVICE_DEPTH_CLOSER(shadowMapDepth, zDevice) (shadowMapDepth < zDevice)
  210. # define COMPARE_DEVICE_DEPTH_CLOSEREQUAL(shadowMapDepth, zDevice) (shadowMapDepth <= zDevice)
  211. #endif
  212. #endif // UNITY_MACROS_INCLUDED