Common.h 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424
  1. // Copyright (C) 2009-2021, 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. #include <AnKi/Gr/Enums.h>
  7. #include <AnKi/Util/Allocator.h>
  8. #include <AnKi/Util/Ptr.h>
  9. #include <AnKi/Util/String.h>
  10. namespace anki
  11. {
  12. // Forward
  13. class GrObject;
  14. class GrManager;
  15. class GrManagerImpl;
  16. class TextureInitInfo;
  17. class TextureViewInitInfo;
  18. class SamplerInitInfo;
  19. class GrManagerInitInfo;
  20. class FramebufferInitInfo;
  21. class BufferInitInfo;
  22. class ShaderInitInfo;
  23. class ShaderProgramInitInfo;
  24. class CommandBufferInitInfo;
  25. class AccelerationStructureInitInfo;
  26. /// @addtogroup graphics
  27. /// @{
  28. #define ANKI_GR_LOGI(...) ANKI_LOG("GR ", NORMAL, __VA_ARGS__)
  29. #define ANKI_GR_LOGE(...) ANKI_LOG("GR ", ERROR, __VA_ARGS__)
  30. #define ANKI_GR_LOGW(...) ANKI_LOG("GR ", WARNING, __VA_ARGS__)
  31. #define ANKI_GR_LOGF(...) ANKI_LOG("GR ", FATAL, __VA_ARGS__)
  32. // Some constants
  33. constexpr U32 MAX_VERTEX_ATTRIBUTES = 8;
  34. constexpr U32 MAX_COLOR_ATTACHMENTS = 4;
  35. constexpr U32 MAX_DESCRIPTOR_SETS = 2; ///< Groups that can be bound at the same time.
  36. constexpr U32 MAX_BINDINGS_PER_DESCRIPTOR_SET = 32;
  37. constexpr U32 MAX_FRAMES_IN_FLIGHT = 3; ///< Triple buffering.
  38. constexpr U32 MAX_GR_OBJECT_NAME_LENGTH = 31;
  39. /// The number of commands in a command buffer that make it a small batch command buffer.
  40. constexpr U32 COMMAND_BUFFER_SMALL_BATCH_MAX_COMMANDS = 100;
  41. /// Smart pointer for resources.
  42. template<typename T>
  43. using GrObjectPtrT = IntrusivePtr<T, DefaultPtrDeleter<GrObject>>;
  44. using GrObjectPtr = GrObjectPtrT<GrObject>;
  45. #define ANKI_GR_CLASS(x_) \
  46. class x_##Impl; \
  47. class x_; \
  48. using x_##Ptr = GrObjectPtrT<x_>;
  49. ANKI_GR_CLASS(Buffer)
  50. ANKI_GR_CLASS(Texture)
  51. ANKI_GR_CLASS(TextureView)
  52. ANKI_GR_CLASS(Sampler)
  53. ANKI_GR_CLASS(CommandBuffer)
  54. ANKI_GR_CLASS(Shader)
  55. ANKI_GR_CLASS(Framebuffer)
  56. ANKI_GR_CLASS(OcclusionQuery)
  57. ANKI_GR_CLASS(TimestampQuery)
  58. ANKI_GR_CLASS(ShaderProgram)
  59. ANKI_GR_CLASS(Fence)
  60. ANKI_GR_CLASS(RenderGraph)
  61. ANKI_GR_CLASS(AccelerationStructure)
  62. #undef ANKI_GR_CLASS
  63. #define ANKI_GR_OBJECT \
  64. friend class GrManager; \
  65. template<typename, typename> \
  66. friend class IntrusivePtr; \
  67. template<typename, typename> \
  68. friend class GenericPoolAllocator;
  69. /// Shader block information.
  70. class ShaderVariableBlockInfo
  71. {
  72. public:
  73. I16 m_offset = -1; ///< Offset inside the block
  74. I16 m_arraySize = -1; ///< Number of elements.
  75. /// Stride between the each array element if the variable is array.
  76. I16 m_arrayStride = -1;
  77. /// Identifying the stride between columns of a column-major matrix or rows of a row-major matrix.
  78. I16 m_matrixStride = -1;
  79. };
  80. /// Knowing the vendor allows some optimizations
  81. enum class GpuVendor : U8
  82. {
  83. UNKNOWN,
  84. ARM,
  85. NVIDIA,
  86. AMD,
  87. INTEL,
  88. COUNT
  89. };
  90. extern Array<CString, U(GpuVendor::COUNT)> GPU_VENDOR_STR;
  91. /// Device capabilities.
  92. ANKI_BEGIN_PACKED_STRUCT
  93. class GpuDeviceCapabilities
  94. {
  95. public:
  96. /// The alignment of offsets when bounding uniform buffers.
  97. U32 m_uniformBufferBindOffsetAlignment = MAX_U32;
  98. /// The max visible range of uniform buffers inside the shaders.
  99. PtrSize m_uniformBufferMaxRange = 0;
  100. /// The alignment of offsets when bounding storage buffers.
  101. U32 m_storageBufferBindOffsetAlignment = MAX_U32;
  102. /// The max visible range of storage buffers inside the shaders.
  103. PtrSize m_storageBufferMaxRange = 0;
  104. /// The alignment of offsets when bounding texture buffers.
  105. U32 m_textureBufferBindOffsetAlignment = MAX_U32;
  106. /// The max visible range of texture buffers inside the shaders.
  107. PtrSize m_textureBufferMaxRange = 0;
  108. /// Max push constant size.
  109. PtrSize m_pushConstantsSize = 128;
  110. /// Each SBT record should be a multiple of this.
  111. U32 m_sbtRecordAlignment = MAX_U32;
  112. /// The size of a shader group handle that will be placed inside an SBT record.
  113. U32 m_shaderGroupHandleSize = 0;
  114. /// GPU vendor.
  115. GpuVendor m_gpuVendor = GpuVendor::UNKNOWN;
  116. /// API version.
  117. U8 m_minorApiVersion = 0;
  118. /// API version.
  119. U8 m_majorApiVersion = 0;
  120. /// RT.
  121. Bool m_rayTracingEnabled = false;
  122. };
  123. ANKI_END_PACKED_STRUCT
  124. static_assert(sizeof(GpuDeviceCapabilities) == sizeof(PtrSize) * 4 + sizeof(U32) * 5 + sizeof(U8) * 3 + sizeof(Bool),
  125. "Should be packed");
  126. /// Bindless related info.
  127. class BindlessLimits
  128. {
  129. public:
  130. U32 m_bindlessTextureCount = 0;
  131. U32 m_bindlessImageCount = 0;
  132. };
  133. /// The type of the allocator for heap allocations
  134. template<typename T>
  135. using GrAllocator = HeapAllocator<T>;
  136. /// Clear values for textures or attachments.
  137. class ClearValue
  138. {
  139. private:
  140. class Ds
  141. {
  142. public:
  143. F32 m_depth;
  144. I32 m_stencil;
  145. };
  146. public:
  147. union
  148. {
  149. Array<F32, 4> m_colorf;
  150. Array<I32, 4> m_colori;
  151. Array<U32, 4> m_coloru;
  152. Ds m_depthStencil;
  153. };
  154. ClearValue()
  155. {
  156. zeroMemory(*this);
  157. }
  158. ClearValue(const ClearValue& b)
  159. {
  160. operator=(b);
  161. }
  162. ClearValue& operator=(const ClearValue& b)
  163. {
  164. memcpy(this, &b, sizeof(*this));
  165. return *this;
  166. }
  167. };
  168. /// A way to identify a surface in a texture.
  169. class TextureSurfaceInfo
  170. {
  171. public:
  172. U32 m_level = 0;
  173. U32 m_depth = 0;
  174. U32 m_face = 0;
  175. U32 m_layer = 0;
  176. TextureSurfaceInfo() = default;
  177. TextureSurfaceInfo(const TextureSurfaceInfo&) = default;
  178. TextureSurfaceInfo(U32 level, U32 depth, U32 face, U32 layer)
  179. : m_level(level)
  180. , m_depth(depth)
  181. , m_face(face)
  182. , m_layer(layer)
  183. {
  184. }
  185. TextureSurfaceInfo& operator=(const TextureSurfaceInfo&) = default;
  186. Bool operator==(const TextureSurfaceInfo& b) const
  187. {
  188. return m_level == b.m_level && m_depth == b.m_depth && m_face == b.m_face && m_layer == b.m_layer;
  189. }
  190. Bool operator!=(const TextureSurfaceInfo& b) const
  191. {
  192. return !(*this == b);
  193. }
  194. U64 computeHash() const
  195. {
  196. return anki::computeHash(this, sizeof(*this), 0x1234567);
  197. }
  198. static TextureSurfaceInfo newZero()
  199. {
  200. return TextureSurfaceInfo();
  201. }
  202. };
  203. /// A way to identify a volume in 3D textures.
  204. class TextureVolumeInfo
  205. {
  206. public:
  207. U32 m_level = 0;
  208. TextureVolumeInfo() = default;
  209. TextureVolumeInfo(const TextureVolumeInfo&) = default;
  210. TextureVolumeInfo(U32 level)
  211. : m_level(level)
  212. {
  213. }
  214. TextureVolumeInfo& operator=(const TextureVolumeInfo&) = default;
  215. };
  216. /// Defines a subset of a texture.
  217. class TextureSubresourceInfo
  218. {
  219. public:
  220. U32 m_firstMipmap = 0;
  221. U32 m_mipmapCount = 1;
  222. U32 m_firstLayer = 0;
  223. U32 m_layerCount = 1;
  224. U8 m_firstFace = 0;
  225. U8 m_faceCount = 1;
  226. DepthStencilAspectBit m_depthStencilAspect = DepthStencilAspectBit::NONE;
  227. U8 _m_padding[1] = {0};
  228. TextureSubresourceInfo(DepthStencilAspectBit aspect = DepthStencilAspectBit::NONE)
  229. : m_depthStencilAspect(aspect)
  230. {
  231. }
  232. TextureSubresourceInfo(const TextureSubresourceInfo&) = default;
  233. TextureSubresourceInfo(const TextureSurfaceInfo& surf, DepthStencilAspectBit aspect = DepthStencilAspectBit::NONE)
  234. : m_firstMipmap(surf.m_level)
  235. , m_mipmapCount(1)
  236. , m_firstLayer(surf.m_layer)
  237. , m_layerCount(1)
  238. , m_firstFace(U8(surf.m_face))
  239. , m_faceCount(1)
  240. , m_depthStencilAspect(aspect)
  241. {
  242. }
  243. TextureSubresourceInfo(const TextureVolumeInfo& vol, DepthStencilAspectBit aspect = DepthStencilAspectBit::NONE)
  244. : m_firstMipmap(vol.m_level)
  245. , m_mipmapCount(1)
  246. , m_firstLayer(0)
  247. , m_layerCount(1)
  248. , m_firstFace(0)
  249. , m_faceCount(1)
  250. , m_depthStencilAspect(aspect)
  251. {
  252. }
  253. TextureSubresourceInfo& operator=(const TextureSubresourceInfo&) = default;
  254. Bool operator==(const TextureSubresourceInfo& b) const
  255. {
  256. ANKI_ASSERT(_m_padding[0] == b._m_padding[0]);
  257. return memcmp(this, &b, sizeof(*this)) == 0;
  258. }
  259. Bool operator!=(const TextureSubresourceInfo& b) const
  260. {
  261. return !(*this == b);
  262. }
  263. U64 computeHash() const
  264. {
  265. static_assert(sizeof(*this) == sizeof(U32) * 4 + sizeof(U8) * 4, "Should be hashable");
  266. ANKI_ASSERT(_m_padding[0] == 0);
  267. return anki::computeHash(this, sizeof(*this));
  268. }
  269. };
  270. /// The base of all init infos for GR.
  271. class GrBaseInitInfo
  272. {
  273. public:
  274. /// @name The name of the object.
  275. GrBaseInitInfo(CString name)
  276. {
  277. setName(name);
  278. }
  279. GrBaseInitInfo()
  280. : GrBaseInitInfo(CString())
  281. {
  282. }
  283. GrBaseInitInfo(const GrBaseInitInfo& b)
  284. {
  285. m_name = b.m_name;
  286. }
  287. GrBaseInitInfo& operator=(const GrBaseInitInfo& b)
  288. {
  289. m_name = b.m_name;
  290. return *this;
  291. }
  292. CString getName() const
  293. {
  294. return (m_name[0] != '\0') ? CString(&m_name[0]) : CString();
  295. }
  296. void setName(CString name)
  297. {
  298. // Zero it because the derived classes may be hashed.
  299. zeroMemory(m_name);
  300. U32 len;
  301. if(name && (len = name.getLength()) > 0)
  302. {
  303. len = min(len, MAX_GR_OBJECT_NAME_LENGTH);
  304. memcpy(&m_name[0], &name[0], len);
  305. }
  306. }
  307. private:
  308. Array<char, MAX_GR_OBJECT_NAME_LENGTH + 1> m_name;
  309. };
  310. /// Compute max number of mipmaps for a 2D texture.
  311. inline U32 computeMaxMipmapCount2d(U32 w, U32 h, U32 minSizeOfLastMip = 1)
  312. {
  313. ANKI_ASSERT(w >= minSizeOfLastMip && h >= minSizeOfLastMip);
  314. U32 s = (w < h) ? w : h;
  315. U32 count = 0;
  316. while(s >= minSizeOfLastMip)
  317. {
  318. s /= 2;
  319. ++count;
  320. }
  321. return count;
  322. }
  323. /// Compute max number of mipmaps for a 3D texture.
  324. inline U32 computeMaxMipmapCount3d(U32 w, U32 h, U32 d, U32 minSizeOfLastMip = 1)
  325. {
  326. U32 s = (w < h) ? w : h;
  327. s = (s < d) ? s : d;
  328. U32 count = 0;
  329. while(s >= minSizeOfLastMip)
  330. {
  331. s /= 2;
  332. ++count;
  333. }
  334. return count;
  335. }
  336. /// Compute the size in bytes of a texture surface surface.
  337. PtrSize computeSurfaceSize(U32 width, U32 height, Format fmt);
  338. /// Compute the size in bytes of the texture volume.
  339. PtrSize computeVolumeSize(U32 width, U32 height, U32 depth, Format fmt);
  340. /// @}
  341. } // end namespace anki