/bgfx/3rdparty/spirv-cross/reference/shaders-msl-no-opt/comp/glsl.std450.comp

https://gitlab.com/JFT/Irrlicht_extended · GLSL · 282 lines · 221 code · 35 blank · 26 comment · 8 complexity · b35f32ce59e36e546022750c07a4e791 MD5 · raw file

  1. #pragma clang diagnostic ignored "-Wmissing-prototypes"
  2. #include <metal_stdlib>
  3. #include <simd/simd.h>
  4. using namespace metal;
  5. struct SSBO
  6. {
  7. float res;
  8. int ires;
  9. uint ures;
  10. float4 f32;
  11. int4 s32;
  12. uint4 u32;
  13. float2x2 m2;
  14. float3x3 m3;
  15. float4x4 m4;
  16. };
  17. struct ResType
  18. {
  19. float _m0;
  20. int _m1;
  21. };
  22. // Implementation of the GLSL radians() function
  23. template<typename T>
  24. T radians(T d)
  25. {
  26. return d * T(0.01745329251);
  27. }
  28. // Implementation of the GLSL degrees() function
  29. template<typename T>
  30. T degrees(T r)
  31. {
  32. return r * T(57.2957795131);
  33. }
  34. // Implementation of the GLSL findLSB() function
  35. template<typename T>
  36. T spvFindLSB(T x)
  37. {
  38. return select(ctz(x), T(-1), x == T(0));
  39. }
  40. // Implementation of the signed GLSL findMSB() function
  41. template<typename T>
  42. T spvFindSMSB(T x)
  43. {
  44. T v = select(x, T(-1) - x, x < T(0));
  45. return select(clz(T(0)) - (clz(v) + T(1)), T(-1), v == T(0));
  46. }
  47. // Implementation of the unsigned GLSL findMSB() function
  48. template<typename T>
  49. T spvFindUMSB(T x)
  50. {
  51. return select(clz(T(0)) - (clz(x) + T(1)), T(-1), x == T(0));
  52. }
  53. // Implementation of the GLSL sign() function for integer types
  54. template<typename T, typename E = typename enable_if<is_integral<T>::value>::type>
  55. T sign(T x)
  56. {
  57. return select(select(select(x, T(0), x == T(0)), T(1), x > T(0)), T(-1), x < T(0));
  58. }
  59. // Returns the determinant of a 2x2 matrix.
  60. inline float spvDet2x2(float a1, float a2, float b1, float b2)
  61. {
  62. return a1 * b2 - b1 * a2;
  63. }
  64. // Returns the determinant of a 3x3 matrix.
  65. inline float spvDet3x3(float a1, float a2, float a3, float b1, float b2, float b3, float c1, float c2, float c3)
  66. {
  67. return a1 * spvDet2x2(b2, b3, c2, c3) - b1 * spvDet2x2(a2, a3, c2, c3) + c1 * spvDet2x2(a2, a3, b2, b3);
  68. }
  69. // Returns the inverse of a matrix, by using the algorithm of calculating the classical
  70. // adjoint and dividing by the determinant. The contents of the matrix are changed.
  71. float4x4 spvInverse4x4(float4x4 m)
  72. {
  73. float4x4 adj; // The adjoint matrix (inverse after dividing by determinant)
  74. // Create the transpose of the cofactors, as the classical adjoint of the matrix.
  75. adj[0][0] = spvDet3x3(m[1][1], m[1][2], m[1][3], m[2][1], m[2][2], m[2][3], m[3][1], m[3][2], m[3][3]);
  76. adj[0][1] = -spvDet3x3(m[0][1], m[0][2], m[0][3], m[2][1], m[2][2], m[2][3], m[3][1], m[3][2], m[3][3]);
  77. adj[0][2] = spvDet3x3(m[0][1], m[0][2], m[0][3], m[1][1], m[1][2], m[1][3], m[3][1], m[3][2], m[3][3]);
  78. adj[0][3] = -spvDet3x3(m[0][1], m[0][2], m[0][3], m[1][1], m[1][2], m[1][3], m[2][1], m[2][2], m[2][3]);
  79. adj[1][0] = -spvDet3x3(m[1][0], m[1][2], m[1][3], m[2][0], m[2][2], m[2][3], m[3][0], m[3][2], m[3][3]);
  80. adj[1][1] = spvDet3x3(m[0][0], m[0][2], m[0][3], m[2][0], m[2][2], m[2][3], m[3][0], m[3][2], m[3][3]);
  81. adj[1][2] = -spvDet3x3(m[0][0], m[0][2], m[0][3], m[1][0], m[1][2], m[1][3], m[3][0], m[3][2], m[3][3]);
  82. adj[1][3] = spvDet3x3(m[0][0], m[0][2], m[0][3], m[1][0], m[1][2], m[1][3], m[2][0], m[2][2], m[2][3]);
  83. adj[2][0] = spvDet3x3(m[1][0], m[1][1], m[1][3], m[2][0], m[2][1], m[2][3], m[3][0], m[3][1], m[3][3]);
  84. adj[2][1] = -spvDet3x3(m[0][0], m[0][1], m[0][3], m[2][0], m[2][1], m[2][3], m[3][0], m[3][1], m[3][3]);
  85. adj[2][2] = spvDet3x3(m[0][0], m[0][1], m[0][3], m[1][0], m[1][1], m[1][3], m[3][0], m[3][1], m[3][3]);
  86. adj[2][3] = -spvDet3x3(m[0][0], m[0][1], m[0][3], m[1][0], m[1][1], m[1][3], m[2][0], m[2][1], m[2][3]);
  87. adj[3][0] = -spvDet3x3(m[1][0], m[1][1], m[1][2], m[2][0], m[2][1], m[2][2], m[3][0], m[3][1], m[3][2]);
  88. adj[3][1] = spvDet3x3(m[0][0], m[0][1], m[0][2], m[2][0], m[2][1], m[2][2], m[3][0], m[3][1], m[3][2]);
  89. adj[3][2] = -spvDet3x3(m[0][0], m[0][1], m[0][2], m[1][0], m[1][1], m[1][2], m[3][0], m[3][1], m[3][2]);
  90. adj[3][3] = spvDet3x3(m[0][0], m[0][1], m[0][2], m[1][0], m[1][1], m[1][2], m[2][0], m[2][1], m[2][2]);
  91. // Calculate the determinant as a combination of the cofactors of the first row.
  92. float det = (adj[0][0] * m[0][0]) + (adj[0][1] * m[1][0]) + (adj[0][2] * m[2][0]) + (adj[0][3] * m[3][0]);
  93. // Divide the classical adjoint matrix by the determinant.
  94. // If determinant is zero, matrix is not invertable, so leave it unchanged.
  95. return (det != 0.0f) ? (adj * (1.0f / det)) : m;
  96. }
  97. // Returns the inverse of a matrix, by using the algorithm of calculating the classical
  98. // adjoint and dividing by the determinant. The contents of the matrix are changed.
  99. float3x3 spvInverse3x3(float3x3 m)
  100. {
  101. float3x3 adj; // The adjoint matrix (inverse after dividing by determinant)
  102. // Create the transpose of the cofactors, as the classical adjoint of the matrix.
  103. adj[0][0] = spvDet2x2(m[1][1], m[1][2], m[2][1], m[2][2]);
  104. adj[0][1] = -spvDet2x2(m[0][1], m[0][2], m[2][1], m[2][2]);
  105. adj[0][2] = spvDet2x2(m[0][1], m[0][2], m[1][1], m[1][2]);
  106. adj[1][0] = -spvDet2x2(m[1][0], m[1][2], m[2][0], m[2][2]);
  107. adj[1][1] = spvDet2x2(m[0][0], m[0][2], m[2][0], m[2][2]);
  108. adj[1][2] = -spvDet2x2(m[0][0], m[0][2], m[1][0], m[1][2]);
  109. adj[2][0] = spvDet2x2(m[1][0], m[1][1], m[2][0], m[2][1]);
  110. adj[2][1] = -spvDet2x2(m[0][0], m[0][1], m[2][0], m[2][1]);
  111. adj[2][2] = spvDet2x2(m[0][0], m[0][1], m[1][0], m[1][1]);
  112. // Calculate the determinant as a combination of the cofactors of the first row.
  113. float det = (adj[0][0] * m[0][0]) + (adj[0][1] * m[1][0]) + (adj[0][2] * m[2][0]);
  114. // Divide the classical adjoint matrix by the determinant.
  115. // If determinant is zero, matrix is not invertable, so leave it unchanged.
  116. return (det != 0.0f) ? (adj * (1.0f / det)) : m;
  117. }
  118. // Returns the inverse of a matrix, by using the algorithm of calculating the classical
  119. // adjoint and dividing by the determinant. The contents of the matrix are changed.
  120. float2x2 spvInverse2x2(float2x2 m)
  121. {
  122. float2x2 adj; // The adjoint matrix (inverse after dividing by determinant)
  123. // Create the transpose of the cofactors, as the classical adjoint of the matrix.
  124. adj[0][0] = m[1][1];
  125. adj[0][1] = -m[0][1];
  126. adj[1][0] = -m[1][0];
  127. adj[1][1] = m[0][0];
  128. // Calculate the determinant as a combination of the cofactors of the first row.
  129. float det = (adj[0][0] * m[0][0]) + (adj[0][1] * m[1][0]);
  130. // Divide the classical adjoint matrix by the determinant.
  131. // If determinant is zero, matrix is not invertable, so leave it unchanged.
  132. return (det != 0.0f) ? (adj * (1.0f / det)) : m;
  133. }
  134. template<typename T>
  135. inline T spvReflect(T i, T n)
  136. {
  137. return i - T(2) * i * n * n;
  138. }
  139. template<typename T>
  140. inline T spvRefract(T i, T n, T eta)
  141. {
  142. T NoI = n * i;
  143. T NoI2 = NoI * NoI;
  144. T k = T(1) - eta * eta * (T(1) - NoI2);
  145. if (k < T(0))
  146. {
  147. return T(0);
  148. }
  149. else
  150. {
  151. return eta * i - (eta * NoI + sqrt(k)) * n;
  152. }
  153. }
  154. template<typename T>
  155. inline T spvFaceForward(T n, T i, T nref)
  156. {
  157. return i * nref < T(0) ? n : -n;
  158. }
  159. kernel void main0(device SSBO& _19 [[buffer(0)]])
  160. {
  161. _19.res = round(_19.f32.x);
  162. _19.res = rint(_19.f32.x);
  163. _19.res = trunc(_19.f32.x);
  164. _19.res = abs(_19.f32.x);
  165. _19.ires = abs(_19.s32.x);
  166. _19.res = sign(_19.f32.x);
  167. _19.ires = sign(_19.s32.x);
  168. _19.res = floor(_19.f32.x);
  169. _19.res = ceil(_19.f32.x);
  170. _19.res = fract(_19.f32.x);
  171. _19.res = radians(_19.f32.x);
  172. _19.res = degrees(_19.f32.x);
  173. _19.res = sin(_19.f32.x);
  174. _19.res = cos(_19.f32.x);
  175. _19.res = tan(_19.f32.x);
  176. _19.res = asin(_19.f32.x);
  177. _19.res = acos(_19.f32.x);
  178. _19.res = atan(_19.f32.x);
  179. _19.res = sinh(_19.f32.x);
  180. _19.res = cosh(_19.f32.x);
  181. _19.res = tanh(_19.f32.x);
  182. _19.res = asinh(_19.f32.x);
  183. _19.res = acosh(_19.f32.x);
  184. _19.res = atanh(_19.f32.x);
  185. _19.res = atan2(_19.f32.x, _19.f32.y);
  186. _19.res = pow(_19.f32.x, _19.f32.y);
  187. _19.res = exp(_19.f32.x);
  188. _19.res = log(_19.f32.x);
  189. _19.res = exp2(_19.f32.x);
  190. _19.res = log2(_19.f32.x);
  191. _19.res = sqrt(_19.f32.x);
  192. _19.res = rsqrt(_19.f32.x);
  193. _19.res = abs(_19.f32.x);
  194. _19.res = abs(_19.f32.x - _19.f32.y);
  195. _19.res = sign(_19.f32.x);
  196. _19.res = spvFaceForward(_19.f32.x, _19.f32.y, _19.f32.z);
  197. _19.res = spvReflect(_19.f32.x, _19.f32.y);
  198. _19.res = spvRefract(_19.f32.x, _19.f32.y, _19.f32.z);
  199. _19.res = length(_19.f32.xy);
  200. _19.res = distance(_19.f32.xy, _19.f32.zw);
  201. float2 v2 = normalize(_19.f32.xy);
  202. v2 = faceforward(_19.f32.xy, _19.f32.yz, _19.f32.zw);
  203. v2 = reflect(_19.f32.xy, _19.f32.zw);
  204. v2 = refract(_19.f32.xy, _19.f32.yz, _19.f32.w);
  205. float3 v3 = cross(_19.f32.xyz, _19.f32.yzw);
  206. _19.res = determinant(_19.m2);
  207. _19.res = determinant(_19.m3);
  208. _19.res = determinant(_19.m4);
  209. _19.m2 = spvInverse2x2(_19.m2);
  210. _19.m3 = spvInverse3x3(_19.m3);
  211. _19.m4 = spvInverse4x4(_19.m4);
  212. float tmp;
  213. float _287 = modf(_19.f32.x, tmp);
  214. _19.res = _287;
  215. _19.res = fast::min(_19.f32.x, _19.f32.y);
  216. _19.ures = min(_19.u32.x, _19.u32.y);
  217. _19.ires = min(_19.s32.x, _19.s32.y);
  218. _19.res = fast::max(_19.f32.x, _19.f32.y);
  219. _19.ures = max(_19.u32.x, _19.u32.y);
  220. _19.ires = max(_19.s32.x, _19.s32.y);
  221. _19.res = fast::clamp(_19.f32.x, _19.f32.y, _19.f32.z);
  222. _19.ures = clamp(_19.u32.x, _19.u32.y, _19.u32.z);
  223. _19.ires = clamp(_19.s32.x, _19.s32.y, _19.s32.z);
  224. _19.res = mix(_19.f32.x, _19.f32.y, _19.f32.z);
  225. _19.res = step(_19.f32.x, _19.f32.y);
  226. _19.res = smoothstep(_19.f32.x, _19.f32.y, _19.f32.z);
  227. _19.res = fma(_19.f32.x, _19.f32.y, _19.f32.z);
  228. ResType _387;
  229. _387._m0 = frexp(_19.f32.x, _387._m1);
  230. int itmp = _387._m1;
  231. _19.res = _387._m0;
  232. _19.res = ldexp(_19.f32.x, itmp);
  233. _19.ures = pack_float_to_snorm4x8(_19.f32);
  234. _19.ures = pack_float_to_unorm4x8(_19.f32);
  235. _19.ures = pack_float_to_snorm2x16(_19.f32.xy);
  236. _19.ures = pack_float_to_unorm2x16(_19.f32.xy);
  237. _19.ures = as_type<uint>(half2(_19.f32.xy));
  238. v2 = unpack_snorm2x16_to_float(_19.u32.x);
  239. v2 = unpack_unorm2x16_to_float(_19.u32.x);
  240. v2 = float2(as_type<half2>(_19.u32.x));
  241. float4 v4 = unpack_snorm4x8_to_float(_19.u32.x);
  242. v4 = unpack_unorm4x8_to_float(_19.u32.x);
  243. _19.s32 = spvFindLSB(_19.s32);
  244. _19.s32 = int4(spvFindLSB(_19.u32));
  245. _19.s32 = spvFindSMSB(_19.s32);
  246. _19.s32 = int4(spvFindUMSB(_19.u32));
  247. }