Common.h 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507
  1. // Copyright (C) 2009-present, Panagiotis Christopoulos Charitos and contributors.
  2. // All rights reserved.
  3. // Code licensed under the BSD License.
  4. // http://www.anki3d.org/LICENSE
  5. #pragma once
  6. //! == C++ =============================================================================================================
  7. #if defined(__cplusplus)
  8. # include <AnKi/Math.h>
  9. # include <AnKi/Util/Enum.h>
  10. # define ANKI_HLSL 0
  11. # define ANKI_GLSL 0
  12. # define ANKI_CPP 1
  13. # define ANKI_BEGIN_NAMESPACE namespace anki {
  14. # define ANKI_END_NAMESPACE }
  15. # define ANKI_ARRAY(type, size, name) Array<type, U32(size)> name
  16. # define ANKI_CPP_CODE(...) __VA_ARGS__
  17. ANKI_BEGIN_NAMESPACE
  18. using Address = U64;
  19. using ScalarVec4 = Array<F32, 4>;
  20. using ScalarMat3x4 = Array<F32, 12>;
  21. using ScalarMat4 = Array<F32, 16>;
  22. using RF32 = F32;
  23. using RVec2 = Vec2;
  24. using RVec3 = Vec3;
  25. using RVec4 = Vec4;
  26. using RMat3 = Mat3;
  27. ANKI_END_NAMESPACE
  28. # define ANKI_RP
  29. //! == HLSL ============================================================================================================
  30. #elif defined(__HLSL_VERSION)
  31. # define ANKI_HLSL 1
  32. # define ANKI_GLSL 0
  33. # define ANKI_CPP 0
  34. # if defined(__spirv__)
  35. # define ANKI_GR_BACKEND_VULKAN 1
  36. # define ANKI_GR_BACKEND_DIRECT3D 0
  37. # else
  38. # define ANKI_GR_BACKEND_VULKAN 0
  39. # define ANKI_GR_BACKEND_DIRECT3D 1
  40. # endif
  41. # define ANKI_BEGIN_NAMESPACE
  42. # define ANKI_END_NAMESPACE
  43. # define inline
  44. # define ANKI_ARRAY(type, size, name) type name[(U32)size]
  45. # define ANKI_CPP_CODE(...)
  46. # define ANKI_ENUM_ALLOW_NUMERIC_OPERATIONS(enum_)
  47. # define constexpr static const
  48. # if defined(ANKI_ASSERTIONS_ENABLED) && ANKI_ASSERTIONS_ENABLED == 1 && ANKI_GR_BACKEND_VULKAN
  49. # define ANKI_ASSERT(x) \
  50. if(!(x)) \
  51. printf("Assertion failed. (" __FILE__ ":%i)", __LINE__)
  52. # else
  53. # define ANKI_ASSERT(x)
  54. # endif
  55. template<typename T>
  56. void maybeUnused(T a)
  57. {
  58. a = a;
  59. }
  60. # define ANKI_MAYBE_UNUSED(x) maybeUnused(x)
  61. # define _ANKI_CONCATENATE(a, b) a##b
  62. # define ANKI_CONCATENATE(a, b) _ANKI_CONCATENATE(a, b)
  63. # define static_assert(x)
  64. # pragma pack_matrix(row_major)
  65. typedef float F32;
  66. typedef float2 Vec2;
  67. typedef float3 Vec3;
  68. typedef float4 Vec4;
  69. typedef float16_t F16;
  70. typedef float16_t2 HVec2;
  71. typedef float16_t3 HVec3;
  72. typedef float16_t4 HVec4;
  73. typedef uint16_t U16;
  74. typedef uint16_t2 U16Vec2;
  75. typedef uint16_t3 U16Vec3;
  76. typedef uint16_t4 U16Vec4;
  77. typedef int16_t I16;
  78. typedef int16_t2 I16Vec2;
  79. typedef int16_t3 I16Vec3;
  80. typedef int16_t4 I16Vec4;
  81. typedef uint U32;
  82. typedef uint32_t2 UVec2;
  83. typedef uint32_t3 UVec3;
  84. typedef uint32_t4 UVec4;
  85. typedef int I32;
  86. typedef int32_t2 IVec2;
  87. typedef int32_t3 IVec3;
  88. typedef int32_t4 IVec4;
  89. typedef uint64_t U64;
  90. typedef uint64_t2 U64Vec2;
  91. typedef uint64_t3 U64Vec3;
  92. typedef uint64_t4 U64Vec4;
  93. typedef int64_t I64;
  94. typedef int64_t2 I64Vec2;
  95. typedef int64_t3 I64Vec3;
  96. typedef int64_t4 I64Vec4;
  97. typedef bool Bool;
  98. # define _ANKI_DEFINE_OPERATOR_SCALAR_ROWS3(matType, scalarType, op) \
  99. matType operator op(scalarType f) \
  100. { \
  101. matType o; \
  102. o.m_row0 = m_row0 op f; \
  103. o.m_row1 = m_row1 op f; \
  104. o.m_row2 = m_row2 op f; \
  105. return o; \
  106. }
  107. # define _ANKI_DEFINE_OPERATOR_SCALAR_ROWS4(matType, scalarType, op) \
  108. matType operator op(scalarType f) \
  109. { \
  110. matType o; \
  111. o.m_row0 = m_row0 op f; \
  112. o.m_row1 = m_row1 op f; \
  113. o.m_row2 = m_row2 op f; \
  114. o.m_row3 = m_row3 op f; \
  115. return o; \
  116. }
  117. # define _ANKI_DEFINE_OPERATOR_SELF_ROWS3(matType, op) \
  118. matType operator op(matType b) \
  119. { \
  120. matType o; \
  121. o.m_row0 = m_row0 op b.m_row0; \
  122. o.m_row1 = m_row1 op b.m_row1; \
  123. o.m_row2 = m_row2 op b.m_row2; \
  124. return o; \
  125. }
  126. # define _ANKI_DEFINE_OPERATOR_SELF_ROWS4(matType, op) \
  127. matType operator op(matType b) \
  128. { \
  129. matType o; \
  130. o.m_row0 = m_row0 op b.m_row0; \
  131. o.m_row1 = m_row1 op b.m_row1; \
  132. o.m_row2 = m_row2 op b.m_row2; \
  133. o.m_row3 = m_row3 op b.m_row3; \
  134. return o; \
  135. }
  136. # define _ANKI_DEFINE_ALL_OPERATORS_ROWS3(matType, scalarType) \
  137. _ANKI_DEFINE_OPERATOR_SCALAR_ROWS3(matType, scalarType, +) \
  138. _ANKI_DEFINE_OPERATOR_SCALAR_ROWS3(matType, scalarType, -) \
  139. _ANKI_DEFINE_OPERATOR_SCALAR_ROWS3(matType, scalarType, *) \
  140. _ANKI_DEFINE_OPERATOR_SCALAR_ROWS3(matType, scalarType, /) \
  141. _ANKI_DEFINE_OPERATOR_SELF_ROWS3(matType, +) \
  142. _ANKI_DEFINE_OPERATOR_SELF_ROWS3(matType, -)
  143. # define _ANKI_DEFINE_ALL_OPERATORS_ROWS4(matType, scalarType) \
  144. _ANKI_DEFINE_OPERATOR_SCALAR_ROWS4(matType, scalarType, +) \
  145. _ANKI_DEFINE_OPERATOR_SCALAR_ROWS4(matType, scalarType, -) \
  146. _ANKI_DEFINE_OPERATOR_SCALAR_ROWS4(matType, scalarType, *) \
  147. _ANKI_DEFINE_OPERATOR_SCALAR_ROWS4(matType, scalarType, /) \
  148. _ANKI_DEFINE_OPERATOR_SELF_ROWS4(matType, +) \
  149. _ANKI_DEFINE_OPERATOR_SELF_ROWS4(matType, -)
  150. struct Mat3
  151. {
  152. Vec3 m_row0;
  153. Vec3 m_row1;
  154. Vec3 m_row2;
  155. _ANKI_DEFINE_ALL_OPERATORS_ROWS3(Mat3, F32)
  156. void setColumns(Vec3 c0, Vec3 c1, Vec3 c2)
  157. {
  158. m_row0 = Vec3(c0.x, c1.x, c2.x);
  159. m_row1 = Vec3(c0.y, c1.y, c2.y);
  160. m_row2 = Vec3(c0.z, c1.z, c2.z);
  161. }
  162. };
  163. Vec3 mul(Mat3 m, Vec3 v)
  164. {
  165. const F32 a = dot(m.m_row0, v);
  166. const F32 b = dot(m.m_row1, v);
  167. const F32 c = dot(m.m_row2, v);
  168. return Vec3(a, b, c);
  169. }
  170. Mat3 transpose(Mat3 m)
  171. {
  172. Mat3 o;
  173. o.setColumns(m.m_row0, m.m_row1, m.m_row2);
  174. return o;
  175. }
  176. struct Mat4
  177. {
  178. Vec4 m_row0;
  179. Vec4 m_row1;
  180. Vec4 m_row2;
  181. Vec4 m_row3;
  182. _ANKI_DEFINE_ALL_OPERATORS_ROWS4(Mat4, F32)
  183. void setColumns(Vec4 c0, Vec4 c1, Vec4 c2, Vec4 c3)
  184. {
  185. m_row0 = Vec4(c0.x, c1.x, c2.x, c3.x);
  186. m_row1 = Vec4(c0.y, c1.y, c2.y, c3.y);
  187. m_row2 = Vec4(c0.z, c1.z, c2.z, c3.z);
  188. m_row3 = Vec4(c0.w, c1.w, c2.w, c3.w);
  189. }
  190. Vec4 getTranslationPart()
  191. {
  192. return Vec4(m_row0.w, m_row1.w, m_row2.w, m_row3.w);
  193. }
  194. };
  195. Vec4 mul(Mat4 m, Vec4 v)
  196. {
  197. const F32 a = dot(m.m_row0, v);
  198. const F32 b = dot(m.m_row1, v);
  199. const F32 c = dot(m.m_row2, v);
  200. const F32 d = dot(m.m_row3, v);
  201. return Vec4(a, b, c, d);
  202. }
  203. Mat4 mul(Mat4 a_, Mat4 b_)
  204. {
  205. const Vec4 a[4] = {a_.m_row0, a_.m_row1, a_.m_row2, a_.m_row3};
  206. const Vec4 b[4] = {b_.m_row0, b_.m_row1, b_.m_row2, b_.m_row3};
  207. Vec4 c[4];
  208. [unroll] for(U32 i = 0; i < 4; i++)
  209. {
  210. Vec4 t1, t2;
  211. t1 = a[i][0];
  212. t2 = b[0] * t1;
  213. t1 = a[i][1];
  214. t2 += b[1] * t1;
  215. t1 = a[i][2];
  216. t2 += b[2] * t1;
  217. t1 = a[i][3];
  218. t2 += b[3] * t1;
  219. c[i] = t2;
  220. }
  221. Mat4 o;
  222. o.m_row0 = c[0];
  223. o.m_row1 = c[1];
  224. o.m_row2 = c[2];
  225. o.m_row3 = c[3];
  226. return o;
  227. }
  228. struct Mat3x4
  229. {
  230. Vec4 m_row0;
  231. Vec4 m_row1;
  232. Vec4 m_row2;
  233. _ANKI_DEFINE_ALL_OPERATORS_ROWS3(Mat3x4, F32)
  234. Vec3 getTranslationPart()
  235. {
  236. return Vec3(m_row0.w, m_row1.w, m_row2.w);
  237. }
  238. void setColumns(Vec3 c0, Vec3 c1, Vec3 c2, Vec3 c3)
  239. {
  240. m_row0 = Vec4(c0.x, c1.x, c2.x, c3.x);
  241. m_row1 = Vec4(c0.y, c1.y, c2.y, c3.y);
  242. m_row2 = Vec4(c0.z, c1.z, c2.z, c3.z);
  243. }
  244. void setColumn(U32 i, Vec3 c)
  245. {
  246. m_row0[i] = c.x;
  247. m_row1[i] = c.y;
  248. m_row2[i] = c.z;
  249. }
  250. Vec3 getColumn(U32 i)
  251. {
  252. return Vec3(m_row0[i], m_row1[i], m_row2[i]);
  253. }
  254. };
  255. Vec3 mul(Mat3x4 m, Vec4 v)
  256. {
  257. const F32 a = dot(m.m_row0, v);
  258. const F32 b = dot(m.m_row1, v);
  259. const F32 c = dot(m.m_row2, v);
  260. return Vec3(a, b, c);
  261. }
  262. Mat3x4 combineTransformations(Mat3x4 a_, Mat3x4 b_)
  263. {
  264. const Vec4 a[3] = {a_.m_row0, a_.m_row1, a_.m_row2};
  265. const Vec4 b[3] = {b_.m_row0, b_.m_row1, b_.m_row2};
  266. Vec4 c[3];
  267. [unroll] for(U32 i = 0; i < 3; i++)
  268. {
  269. Vec4 t2;
  270. t2 = b[0] * a[i][0];
  271. t2 += b[1] * a[i][1];
  272. t2 += b[2] * a[i][2];
  273. const Vec4 v4 = Vec4(0.0f, 0.0f, 0.0f, a[i][3]);
  274. t2 += v4;
  275. c[i] = t2;
  276. }
  277. Mat3x4 o;
  278. o.m_row0 = c[0];
  279. o.m_row1 = c[1];
  280. o.m_row2 = c[2];
  281. return o;
  282. }
  283. template<typename TMat>
  284. Vec3 extractScale(TMat trf)
  285. {
  286. Vec3 scale;
  287. [unroll] for(U32 i = 0; i < 3; ++i)
  288. {
  289. const Vec3 axis = Vec3(trf.m_row0[i], trf.m_row1[i], trf.m_row2[i]);
  290. scale[i] = length(axis);
  291. }
  292. return scale;
  293. }
  294. #endif // defined(__HLSL_VERSION)
  295. //! == Common ==========================================================================================================
  296. ANKI_BEGIN_NAMESPACE
  297. constexpr U32 kMaxLodCount = 3u;
  298. constexpr U32 kMaxShadowCascades = 4u;
  299. constexpr U32 kIndirectDiffuseClipmapCount = 3u;
  300. /// Bias applied to the camera to push the clipmaps further into the looking direction.
  301. constexpr Vec3 kIndirectDiffuseClipmapForwardBias = Vec3(20.0f, 2.0f, 20.0f);
  302. constexpr F32 kShadowsPolygonOffsetFactor = 1.25f;
  303. constexpr F32 kShadowsPolygonOffsetUnits = 2.75f;
  304. #if defined(__HLSL_VERSION)
  305. constexpr U32 kMaxMipsSinglePassDownsamplerCanProduce = 12u;
  306. #else
  307. constexpr U8 kMaxMipsSinglePassDownsamplerCanProduce = 12u;
  308. #endif
  309. constexpr U32 kMaxPrimitivesPerMeshlet = 124; ///< nVidia prefers 126 but meshoptimizer choks with that value.
  310. constexpr U32 kMaxVerticesPerMeshlet = 128;
  311. #define ANKI_TASK_SHADER_THREADGROUP_SIZE 64u
  312. constexpr U32 kMeshletGroupSize = ANKI_TASK_SHADER_THREADGROUP_SIZE;
  313. #define ANKI_MESH_SHADER_THREADGROUP_SIZE 32u
  314. static_assert(kMaxVerticesPerMeshlet % ANKI_MESH_SHADER_THREADGROUP_SIZE == 0);
  315. constexpr F32 kPcfTexelRadius = 4.0f;
  316. constexpr F32 kPcssSearchTexelRadius = 12.0;
  317. constexpr F32 kPcssTexelRadius = 12.0;
  318. constexpr F32 kPcssDirLightMaxPenumbraMeters = 6.0; // If the occluder and the reciever have more than this value then do full penumbra
  319. // Some special spaces or sets for reflection to identify special shader input
  320. #define ANKI_D3D_FAST_CONSTANTS_SPACE 3000
  321. #define ANKI_D3D_SHADER_RECORD_CONSTANTS_SPACE 3001
  322. #define ANKI_D3D_DRAW_ID_CONSTANT_SPACE 3002
  323. #define ANKI_VK_BINDLESS_TEXTURES_DESCRIPTOR_SET 1000000
  324. struct DrawIndirectArgs
  325. {
  326. U32 m_vertexCount;
  327. U32 m_instanceCount;
  328. U32 m_firstVertex;
  329. U32 m_firstInstance;
  330. #if defined(__cplusplus)
  331. DrawIndirectArgs()
  332. : m_vertexCount(kMaxU32)
  333. , m_instanceCount(1)
  334. , m_firstVertex(0)
  335. , m_firstInstance(0)
  336. {
  337. }
  338. DrawIndirectArgs(const DrawIndirectArgs&) = default;
  339. DrawIndirectArgs(U32 count, U32 instanceCount, U32 first, U32 baseInstance)
  340. : m_vertexCount(count)
  341. , m_instanceCount(instanceCount)
  342. , m_firstVertex(first)
  343. , m_firstInstance(baseInstance)
  344. {
  345. }
  346. Bool operator==(const DrawIndirectArgs& b) const
  347. {
  348. return m_vertexCount == b.m_vertexCount && m_instanceCount == b.m_instanceCount && m_firstVertex == b.m_firstVertex
  349. && m_firstInstance == b.m_firstInstance;
  350. }
  351. Bool operator!=(const DrawIndirectArgs& b) const
  352. {
  353. return !(operator==(b));
  354. }
  355. #endif
  356. };
  357. struct DrawIndexedIndirectArgs
  358. {
  359. U32 m_indexCount;
  360. U32 m_instanceCount;
  361. U32 m_firstIndex;
  362. I32 m_vertexOffset;
  363. U32 m_firstInstance;
  364. #if defined(__cplusplus)
  365. DrawIndexedIndirectArgs()
  366. : m_indexCount(kMaxU32)
  367. , m_instanceCount(1)
  368. , m_firstIndex(0)
  369. , m_vertexOffset(0)
  370. , m_firstInstance(0)
  371. {
  372. }
  373. DrawIndexedIndirectArgs(const DrawIndexedIndirectArgs&) = default;
  374. DrawIndexedIndirectArgs(U32 count, U32 instanceCount, U32 firstIndex, U32 baseVertex, U32 baseInstance)
  375. : m_indexCount(count)
  376. , m_instanceCount(instanceCount)
  377. , m_firstIndex(firstIndex)
  378. , m_vertexOffset(baseVertex)
  379. , m_firstInstance(baseInstance)
  380. {
  381. }
  382. Bool operator==(const DrawIndexedIndirectArgs& b) const
  383. {
  384. return m_indexCount == b.m_indexCount && m_instanceCount == b.m_instanceCount && m_firstIndex == b.m_firstIndex
  385. && m_vertexOffset == b.m_vertexOffset && m_firstInstance == b.m_firstInstance;
  386. }
  387. Bool operator!=(const DrawIndexedIndirectArgs& b) const
  388. {
  389. return !(operator==(b));
  390. }
  391. #endif
  392. };
  393. struct DispatchIndirectArgs
  394. {
  395. U32 m_threadGroupCountX;
  396. U32 m_threadGroupCountY;
  397. U32 m_threadGroupCountZ;
  398. };
  399. /// Mirrors VkGeometryInstanceFlagBitsKHR
  400. enum AccellerationStructureFlag : U32
  401. {
  402. kAccellerationStructureFlagTriangleFacingCullDisable = 1 << 0,
  403. kAccellerationStructureFlagFlipFacing = 1 << 1,
  404. kAccellerationStructureFlagForceOpaque = 1 << 2,
  405. kAccellerationStructureFlagForceNoOpaque = 1 << 3,
  406. kAccellerationStructureFlagTriangleFrontCounterlockwise = kAccellerationStructureFlagFlipFacing
  407. };
  408. /// Mirrors VkAccelerationStructureInstanceKHR and D3D12_RAYTRACING_INSTANCE_DESC.
  409. struct AccelerationStructureInstance
  410. {
  411. Mat3x4 m_transform;
  412. U32 m_instanceCustomIndex : 24; ///< Custom value that can be accessed in the shaders.
  413. U32 m_mask : 8;
  414. U32 m_instanceShaderBindingTableRecordOffset : 24;
  415. U32 m_flags : 8; ///< It's AccellerationStructureFlag.
  416. #if defined(__cplusplus)
  417. U64 m_accelerationStructureAddress;
  418. #else
  419. UVec2 m_accelerationStructureAddress;
  420. #endif
  421. };
  422. ANKI_END_NAMESPACE