Common.h 9.3 KB

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