Common.h 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431
  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. /// 64 bit atomics.
  123. Bool m_64bitAtomics = false;
  124. /// Supports min/max texture filtering.
  125. Bool m_samplingFilterMinMax = false;
  126. };
  127. ANKI_END_PACKED_STRUCT
  128. static_assert(sizeof(GpuDeviceCapabilities)
  129. == sizeof(PtrSize) * 4 + sizeof(U32) * 5 + sizeof(U8) * 3 + sizeof(Bool) * 3,
  130. "Should be packed");
  131. /// Bindless related info.
  132. class BindlessLimits
  133. {
  134. public:
  135. U32 m_bindlessTextureCount = 0;
  136. U32 m_bindlessImageCount = 0;
  137. };
  138. /// The type of the allocator for heap allocations
  139. template<typename T>
  140. using GrAllocator = HeapAllocator<T>;
  141. /// Clear values for textures or attachments.
  142. class ClearValue
  143. {
  144. private:
  145. class Ds
  146. {
  147. public:
  148. F32 m_depth;
  149. I32 m_stencil;
  150. };
  151. public:
  152. union
  153. {
  154. Array<F32, 4> m_colorf;
  155. Array<I32, 4> m_colori;
  156. Array<U32, 4> m_coloru;
  157. Ds m_depthStencil;
  158. };
  159. ClearValue()
  160. {
  161. zeroMemory(*this);
  162. }
  163. ClearValue(const ClearValue& b)
  164. {
  165. operator=(b);
  166. }
  167. ClearValue& operator=(const ClearValue& b)
  168. {
  169. memcpy(this, &b, sizeof(*this));
  170. return *this;
  171. }
  172. };
  173. /// A way to identify a surface in a texture.
  174. class TextureSurfaceInfo
  175. {
  176. public:
  177. U32 m_level = 0;
  178. U32 m_depth = 0;
  179. U32 m_face = 0;
  180. U32 m_layer = 0;
  181. TextureSurfaceInfo() = default;
  182. TextureSurfaceInfo(const TextureSurfaceInfo&) = default;
  183. TextureSurfaceInfo(U32 level, U32 depth, U32 face, U32 layer)
  184. : m_level(level)
  185. , m_depth(depth)
  186. , m_face(face)
  187. , m_layer(layer)
  188. {
  189. }
  190. TextureSurfaceInfo& operator=(const TextureSurfaceInfo&) = default;
  191. Bool operator==(const TextureSurfaceInfo& b) const
  192. {
  193. return m_level == b.m_level && m_depth == b.m_depth && m_face == b.m_face && m_layer == b.m_layer;
  194. }
  195. Bool operator!=(const TextureSurfaceInfo& b) const
  196. {
  197. return !(*this == b);
  198. }
  199. U64 computeHash() const
  200. {
  201. return anki::computeHash(this, sizeof(*this), 0x1234567);
  202. }
  203. static TextureSurfaceInfo newZero()
  204. {
  205. return TextureSurfaceInfo();
  206. }
  207. };
  208. /// A way to identify a volume in 3D textures.
  209. class TextureVolumeInfo
  210. {
  211. public:
  212. U32 m_level = 0;
  213. TextureVolumeInfo() = default;
  214. TextureVolumeInfo(const TextureVolumeInfo&) = default;
  215. TextureVolumeInfo(U32 level)
  216. : m_level(level)
  217. {
  218. }
  219. TextureVolumeInfo& operator=(const TextureVolumeInfo&) = default;
  220. };
  221. /// Defines a subset of a texture.
  222. class TextureSubresourceInfo
  223. {
  224. public:
  225. U32 m_firstMipmap = 0;
  226. U32 m_mipmapCount = 1;
  227. U32 m_firstLayer = 0;
  228. U32 m_layerCount = 1;
  229. U8 m_firstFace = 0;
  230. U8 m_faceCount = 1;
  231. DepthStencilAspectBit m_depthStencilAspect = DepthStencilAspectBit::NONE;
  232. U8 _m_padding[1] = {0};
  233. TextureSubresourceInfo(DepthStencilAspectBit aspect = DepthStencilAspectBit::NONE)
  234. : m_depthStencilAspect(aspect)
  235. {
  236. }
  237. TextureSubresourceInfo(const TextureSubresourceInfo&) = default;
  238. TextureSubresourceInfo(const TextureSurfaceInfo& surf, DepthStencilAspectBit aspect = DepthStencilAspectBit::NONE)
  239. : m_firstMipmap(surf.m_level)
  240. , m_mipmapCount(1)
  241. , m_firstLayer(surf.m_layer)
  242. , m_layerCount(1)
  243. , m_firstFace(U8(surf.m_face))
  244. , m_faceCount(1)
  245. , m_depthStencilAspect(aspect)
  246. {
  247. }
  248. TextureSubresourceInfo(const TextureVolumeInfo& vol, DepthStencilAspectBit aspect = DepthStencilAspectBit::NONE)
  249. : m_firstMipmap(vol.m_level)
  250. , m_mipmapCount(1)
  251. , m_firstLayer(0)
  252. , m_layerCount(1)
  253. , m_firstFace(0)
  254. , m_faceCount(1)
  255. , m_depthStencilAspect(aspect)
  256. {
  257. }
  258. TextureSubresourceInfo& operator=(const TextureSubresourceInfo&) = default;
  259. Bool operator==(const TextureSubresourceInfo& b) const
  260. {
  261. ANKI_ASSERT(_m_padding[0] == b._m_padding[0]);
  262. return memcmp(this, &b, sizeof(*this)) == 0;
  263. }
  264. Bool operator!=(const TextureSubresourceInfo& b) const
  265. {
  266. return !(*this == b);
  267. }
  268. U64 computeHash() const
  269. {
  270. static_assert(sizeof(*this) == sizeof(U32) * 4 + sizeof(U8) * 4, "Should be hashable");
  271. ANKI_ASSERT(_m_padding[0] == 0);
  272. return anki::computeHash(this, sizeof(*this));
  273. }
  274. };
  275. /// The base of all init infos for GR.
  276. class GrBaseInitInfo
  277. {
  278. public:
  279. /// @name The name of the object.
  280. GrBaseInitInfo(CString name)
  281. {
  282. setName(name);
  283. }
  284. GrBaseInitInfo()
  285. : GrBaseInitInfo(CString())
  286. {
  287. }
  288. GrBaseInitInfo(const GrBaseInitInfo& b)
  289. {
  290. m_name = b.m_name;
  291. }
  292. GrBaseInitInfo& operator=(const GrBaseInitInfo& b)
  293. {
  294. m_name = b.m_name;
  295. return *this;
  296. }
  297. CString getName() const
  298. {
  299. return (m_name[0] != '\0') ? CString(&m_name[0]) : CString();
  300. }
  301. void setName(CString name)
  302. {
  303. // Zero it because the derived classes may be hashed.
  304. zeroMemory(m_name);
  305. U32 len;
  306. if(name && (len = name.getLength()) > 0)
  307. {
  308. len = min(len, MAX_GR_OBJECT_NAME_LENGTH);
  309. memcpy(&m_name[0], &name[0], len);
  310. }
  311. }
  312. private:
  313. Array<char, MAX_GR_OBJECT_NAME_LENGTH + 1> m_name;
  314. };
  315. /// Compute max number of mipmaps for a 2D texture.
  316. inline U32 computeMaxMipmapCount2d(U32 w, U32 h, U32 minSizeOfLastMip = 1)
  317. {
  318. ANKI_ASSERT(w >= minSizeOfLastMip && h >= minSizeOfLastMip);
  319. U32 s = (w < h) ? w : h;
  320. U32 count = 0;
  321. while(s >= minSizeOfLastMip)
  322. {
  323. s /= 2;
  324. ++count;
  325. }
  326. return count;
  327. }
  328. /// Compute max number of mipmaps for a 3D texture.
  329. inline U32 computeMaxMipmapCount3d(U32 w, U32 h, U32 d, U32 minSizeOfLastMip = 1)
  330. {
  331. U32 s = (w < h) ? w : h;
  332. s = (s < d) ? s : d;
  333. U32 count = 0;
  334. while(s >= minSizeOfLastMip)
  335. {
  336. s /= 2;
  337. ++count;
  338. }
  339. return count;
  340. }
  341. /// Compute the size in bytes of a texture surface surface.
  342. PtrSize computeSurfaceSize(U32 width, U32 height, Format fmt);
  343. /// Compute the size in bytes of the texture volume.
  344. PtrSize computeVolumeSize(U32 width, U32 height, U32 depth, Format fmt);
  345. /// @}
  346. } // end namespace anki