Common.h 27 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124
  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. #include <AnKi/Util/Allocator.h>
  7. #include <AnKi/Util/Ptr.h>
  8. #include <AnKi/Util/String.h>
  9. #include <AnKi/Util/Enum.h>
  10. #include <AnKi/Shaders/Include/Common.h>
  11. namespace anki {
  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. class GrUpscalerInitInfo;
  27. class PipelineQueryInitInfo;
  28. /// @addtogroup graphics
  29. /// @{
  30. #define ANKI_GR_LOGI(...) ANKI_LOG("GR", kNormal, __VA_ARGS__)
  31. #define ANKI_GR_LOGE(...) ANKI_LOG("GR", kError, __VA_ARGS__)
  32. #define ANKI_GR_LOGW(...) ANKI_LOG("GR", kWarning, __VA_ARGS__)
  33. #define ANKI_GR_LOGF(...) ANKI_LOG("GR", kFatal, __VA_ARGS__)
  34. #define ANKI_GR_LOGV(...) ANKI_LOG("GR", kVerbose, __VA_ARGS__)
  35. class GrMemoryPool : public HeapMemoryPool, public MakeSingleton<GrMemoryPool>
  36. {
  37. template<typename>
  38. friend class MakeSingleton;
  39. private:
  40. GrMemoryPool(AllocAlignedCallback allocCb, void* allocCbUserData)
  41. : HeapMemoryPool(allocCb, allocCbUserData, "GrMemPool")
  42. {
  43. }
  44. ~GrMemoryPool() = default;
  45. };
  46. ANKI_DEFINE_SUBMODULE_UTIL_CONTAINERS(Gr, GrMemoryPool)
  47. // Some constants
  48. constexpr U32 kMaxColorRenderTargets = 4;
  49. constexpr U32 kMaxDescriptorSets = 3; ///< Groups that can be bound at the same time.
  50. constexpr U32 kMaxBindingsPerDescriptorSet = 32;
  51. constexpr U32 kMaxFramesInFlight = 3; ///< Triple buffering.
  52. constexpr U32 kMaxGrObjectNameLength = 61;
  53. constexpr U32 kMaxBindlessTextures = 512;
  54. constexpr U32 kMaxBindlessReadonlyTextureBuffers = 512;
  55. constexpr U32 kMaxPushConstantSize = 128; ///< Thanks AMD!!
  56. /// The number of commands in a command buffer that make it a small batch command buffer.
  57. constexpr U32 kCommandBufferSmallBatchMaxCommands = 100;
  58. class GrObjectDeleter
  59. {
  60. public:
  61. void operator()(GrObject* ptr);
  62. };
  63. /// Smart pointer for resources.
  64. template<typename T>
  65. using GrObjectPtrT = IntrusivePtr<T, GrObjectDeleter>;
  66. using GrObjectPtr = GrObjectPtrT<GrObject>;
  67. #define ANKI_GR_CLASS(x_) \
  68. class x_##Impl; \
  69. class x_; \
  70. using x_##Ptr = GrObjectPtrT<x_>;
  71. ANKI_GR_CLASS(Buffer)
  72. ANKI_GR_CLASS(Texture)
  73. ANKI_GR_CLASS(Sampler)
  74. ANKI_GR_CLASS(CommandBuffer)
  75. ANKI_GR_CLASS(Shader)
  76. ANKI_GR_CLASS(Framebuffer)
  77. ANKI_GR_CLASS(OcclusionQuery)
  78. ANKI_GR_CLASS(TimestampQuery)
  79. ANKI_GR_CLASS(PipelineQuery)
  80. ANKI_GR_CLASS(ShaderProgram)
  81. ANKI_GR_CLASS(Fence)
  82. ANKI_GR_CLASS(RenderGraph)
  83. ANKI_GR_CLASS(AccelerationStructure)
  84. ANKI_GR_CLASS(GrUpscaler)
  85. #undef ANKI_GR_CLASS
  86. #define ANKI_GR_OBJECT \
  87. friend class GrManager; \
  88. template<typename, typename> \
  89. friend class IntrusivePtr; \
  90. template<typename T, typename... TArgs> \
  91. friend void callConstructor(T& p, TArgs&&... args);
  92. /// Shader block information.
  93. class ShaderVariableBlockInfo
  94. {
  95. public:
  96. I16 m_offset = -1; ///< Offset inside the block
  97. I16 m_arraySize = -1; ///< Number of elements.
  98. /// Stride between the each array element if the variable is array.
  99. I16 m_arrayStride = -1;
  100. /// Identifying the stride between columns of a column-major matrix or rows of a row-major matrix.
  101. I16 m_matrixStride = -1;
  102. };
  103. /// Knowing the vendor allows some optimizations
  104. enum class GpuVendor : U8
  105. {
  106. kUnknown,
  107. kArm,
  108. kNvidia,
  109. kAMD,
  110. kIntel,
  111. kQualcomm,
  112. kCount
  113. };
  114. inline constexpr Array<CString, U(GpuVendor::kCount)> kGPUVendorStrings = {"unknown", "ARM", "nVidia", "AMD", "Intel", "Qualcomm"};
  115. /// Device capabilities.
  116. ANKI_BEGIN_PACKED_STRUCT
  117. class GpuDeviceCapabilities
  118. {
  119. public:
  120. /// The alignment of offsets when bounding constant buffers.
  121. U32 m_uniformBufferBindOffsetAlignment = kMaxU32;
  122. /// The max visible range of constant buffers inside the shaders.
  123. PtrSize m_uniformBufferMaxRange = 0;
  124. /// The alignment of offsets when bounding storage buffers.
  125. U32 m_storageBufferBindOffsetAlignment = kMaxU32;
  126. /// The max visible range of storage buffers inside the shaders.
  127. PtrSize m_storageBufferMaxRange = 0;
  128. /// The alignment of offsets when bounding texture buffers.
  129. U32 m_texelBufferBindOffsetAlignment = kMaxU32;
  130. /// The max visible range of texture buffers inside the shaders.
  131. PtrSize m_textureBufferMaxRange = 0;
  132. /// Max push constant size.
  133. PtrSize m_pushConstantsSize = 128;
  134. /// The max combined size of shared variables (with paddings) in compute shaders.
  135. PtrSize m_computeSharedMemorySize = 16_KB;
  136. /// Alignment of the scratch buffer used in AS building.
  137. U32 m_accelerationStructureBuildScratchOffsetAlignment = 0;
  138. /// Each SBT record should be a multiple of this.
  139. U32 m_sbtRecordAlignment = kMaxU32;
  140. /// The size of a shader group handle that will be placed inside an SBT record.
  141. U32 m_shaderGroupHandleSize = 0;
  142. /// Min subgroup size of the GPU.
  143. U32 m_minSubgroupSize = 0;
  144. /// Max subgroup size of the GPU.
  145. U32 m_maxSubgroupSize = 0;
  146. /// Min size of a texel in the shading rate image.
  147. U32 m_minShadingRateImageTexelSize = 0;
  148. /// The max number of drawcalls in draw indirect count calls.
  149. U32 m_maxDrawIndirectCount = 0;
  150. /// GPU vendor.
  151. GpuVendor m_gpuVendor = GpuVendor::kUnknown;
  152. /// Descrete or integrated GPU.
  153. Bool m_discreteGpu = false;
  154. /// API version.
  155. U8 m_minorApiVersion = 0;
  156. /// API version.
  157. U8 m_majorApiVersion = 0;
  158. /// Align structured buffers using the structure's size and not the m_storageBufferBindOffsetAlignment.
  159. Bool m_structuredBufferNaturalAlignment = false;
  160. /// RT.
  161. Bool m_rayTracingEnabled = false;
  162. /// 64 bit atomics.
  163. Bool m_64bitAtomics = false;
  164. /// VRS.
  165. Bool m_vrs = false;
  166. /// Supports min/max texture filtering.
  167. Bool m_samplingFilterMinMax = false;
  168. /// Supports or not 24bit, 48bit or 96bit texture formats.
  169. Bool m_unalignedBbpTextureFormats = false;
  170. /// DLSS.
  171. Bool m_dlss = false;
  172. /// Mesh shaders.
  173. Bool m_meshShaders = false;
  174. /// Can create PipelineQuery objects.
  175. Bool m_pipelineQuery = false;
  176. /// Has access to barycentrics.
  177. Bool m_barycentrics = false;
  178. /// WorkGraphs
  179. Bool m_workGraphs = false;
  180. };
  181. ANKI_END_PACKED_STRUCT
  182. /// The type of the allocator for heap allocations
  183. template<typename T>
  184. using GrAllocator = HeapAllocator<T>;
  185. /// The base of all init infos for GR.
  186. class GrBaseInitInfo
  187. {
  188. public:
  189. /// @name The name of the object.
  190. GrBaseInitInfo(CString name)
  191. {
  192. setName(name);
  193. }
  194. GrBaseInitInfo()
  195. : GrBaseInitInfo(CString())
  196. {
  197. }
  198. GrBaseInitInfo(const GrBaseInitInfo& b)
  199. {
  200. m_name = b.m_name;
  201. }
  202. GrBaseInitInfo& operator=(const GrBaseInitInfo& b)
  203. {
  204. m_name = b.m_name;
  205. return *this;
  206. }
  207. CString getName() const
  208. {
  209. return (m_name[0] != '\0') ? CString(&m_name[0]) : CString();
  210. }
  211. void setName(CString name)
  212. {
  213. // Zero it because the derived classes may be hashed.
  214. zeroMemory(m_name);
  215. U32 len;
  216. if(name && (len = name.getLength()) > 0)
  217. {
  218. len = min(len, kMaxGrObjectNameLength);
  219. memcpy(&m_name[0], &name[0], len);
  220. }
  221. }
  222. private:
  223. Array<char, kMaxGrObjectNameLength + 1> m_name;
  224. };
  225. enum class ColorBit : U8
  226. {
  227. kNone = 0,
  228. kRed = 1 << 0,
  229. kGreen = 1 << 1,
  230. kBlue = 1 << 2,
  231. kAlpha = 1 << 3,
  232. kAll = kRed | kGreen | kBlue | kAlpha
  233. };
  234. ANKI_ENUM_ALLOW_NUMERIC_OPERATIONS(ColorBit)
  235. enum class PrimitiveTopology : U8
  236. {
  237. kPoints,
  238. kLines,
  239. kLineStip,
  240. kTriangles,
  241. kTriangleStrip,
  242. kPatches
  243. };
  244. enum class FillMode : U8
  245. {
  246. kPoints,
  247. kWireframe,
  248. kSolid,
  249. kCount
  250. };
  251. enum class FaceSelectionBit : U8
  252. {
  253. kNone = 0,
  254. kFront = 1 << 0,
  255. kBack = 1 << 1,
  256. kFrontAndBack = kFront | kBack
  257. };
  258. ANKI_ENUM_ALLOW_NUMERIC_OPERATIONS(FaceSelectionBit)
  259. enum class CompareOperation : U8
  260. {
  261. kAlways,
  262. kLess,
  263. kEqual,
  264. kLessEqual,
  265. kGreater,
  266. kGreaterEqual,
  267. kNotEqual,
  268. kNever,
  269. kCount
  270. };
  271. enum class StencilOperation : U8
  272. {
  273. kKeep,
  274. kZero,
  275. kReplace,
  276. kIncrementAndClamp,
  277. kDecrementAndClamp,
  278. kInvert,
  279. kIncrementAndWrap,
  280. kDecrementAndWrap,
  281. kCount
  282. };
  283. enum class BlendFactor : U8
  284. {
  285. kZero,
  286. kOne,
  287. kSrcColor,
  288. kOneMinusSrcColor,
  289. kDstColor,
  290. kOneMinusDstColor,
  291. kSrcAlpha,
  292. kOneMinusSrcAlpha,
  293. kDstAlpha,
  294. kOneMinusDstAlpha,
  295. kConstantColor,
  296. kOneMinusConstantColor,
  297. kConstantAlpha,
  298. kOneMinusConstantAlpha,
  299. kSrcAlphaSaturate,
  300. kSrc1Color,
  301. kOneMinusSrc1Color,
  302. kSrc1Alpha,
  303. kOneMinusSrc1Alpha,
  304. kCount
  305. };
  306. enum class BlendOperation : U8
  307. {
  308. kAdd,
  309. kSubtract,
  310. kReverseSubtract,
  311. kMin,
  312. kMax,
  313. kCount
  314. };
  315. enum class VertexStepRate : U8
  316. {
  317. kVertex,
  318. kInstance,
  319. kCount
  320. };
  321. /// A way to distinguish the aspect of a depth stencil texture.
  322. enum class DepthStencilAspectBit : U8
  323. {
  324. kNone = 0,
  325. kDepth = 1 << 0,
  326. kStencil = 1 << 1,
  327. kDepthStencil = kDepth | kStencil
  328. };
  329. ANKI_ENUM_ALLOW_NUMERIC_OPERATIONS(DepthStencilAspectBit)
  330. /// Pixel or vertex format.
  331. /// WARNING: Keep it the same as vulkan (one conversion less).
  332. enum class Format : U32
  333. {
  334. kNone = 0,
  335. #define ANKI_FORMAT_DEF(type, vk, d3d, componentCount, texelSize, blockWidth, blockHeight, blockSize, shaderType, depthStencil) k##type = vk,
  336. #include <AnKi/Gr/BackendCommon/Format.def.h>
  337. #undef ANKI_FORMAT_DEF
  338. };
  339. ANKI_ENUM_ALLOW_NUMERIC_OPERATIONS(Format)
  340. /// Contains info for a specific Format.
  341. class FormatInfo
  342. {
  343. public:
  344. U8 m_componentCount; ///< The number of components.
  345. U8 m_texelSize; ///< The size of the texel. Only for incompressed, zero for compressed.
  346. U8 m_blockWidth; ///< The width of the block size of compressed formats. Zero otherwise.
  347. U8 m_blockHeight; ///< The height of the block size of compressed formats. Zero otherwise.
  348. U8 m_blockSize; ///< The size of the block of a compressed format. Zero otherwise.
  349. U8 m_shaderType; ///< It's 0 if the shader sees it as float, 1 if uint and 2 if signed int.
  350. DepthStencilAspectBit m_depthStencil; ///< Depth/stencil mask.
  351. const char* m_name;
  352. Bool isDepthStencil() const
  353. {
  354. return m_depthStencil != DepthStencilAspectBit::kNone;
  355. }
  356. Bool isDepth() const
  357. {
  358. return !!(m_depthStencil & DepthStencilAspectBit::kDepth);
  359. }
  360. Bool isStencil() const
  361. {
  362. return !!(m_depthStencil & DepthStencilAspectBit::kStencil);
  363. }
  364. Bool isCompressed() const
  365. {
  366. return m_blockSize > 0;
  367. }
  368. };
  369. /// Get info for a specific Format.
  370. ANKI_PURE FormatInfo getFormatInfo(Format fmt);
  371. /// Compute the size in bytes of a texture surface surface.
  372. ANKI_PURE PtrSize computeSurfaceSize(U32 width, U32 height, Format fmt);
  373. /// Compute the size in bytes of the texture volume.
  374. ANKI_PURE PtrSize computeVolumeSize(U32 width, U32 height, U32 depth, Format fmt);
  375. /// Texture type.
  376. enum class TextureType : U8
  377. {
  378. k1D,
  379. k2D,
  380. k3D,
  381. k2DArray,
  382. kCube,
  383. kCubeArray,
  384. kCount
  385. };
  386. inline Bool textureTypeIsCube(const TextureType t)
  387. {
  388. return t == TextureType::kCube || t == TextureType::kCubeArray;
  389. }
  390. /// Texture usage hints. They are very important.
  391. enum class TextureUsageBit : U32
  392. {
  393. kNone = 0,
  394. kSampledGeometry = 1 << 0,
  395. kSampledFragment = 1 << 1,
  396. kSampledCompute = 1 << 2,
  397. kSampledTraceRays = 1 << 3,
  398. kStorageGeometryRead = 1 << 4,
  399. kStorageGeometryWrite = 1 << 5,
  400. kStorageFragmentRead = 1 << 6,
  401. kStorageFragmentWrite = 1 << 7,
  402. kStorageComputeRead = 1 << 8,
  403. kStorageComputeWrite = 1 << 9,
  404. kStorageTraceRaysRead = 1 << 10,
  405. kStorageTraceRaysWrite = 1 << 11,
  406. kFramebufferRead = 1 << 12,
  407. kFramebufferWrite = 1 << 13,
  408. kFramebufferShadingRate = 1 << 14,
  409. kTransferDestination = 1 << 15,
  410. kPresent = 1 << 17,
  411. // Derived
  412. kAllSampled = kSampledGeometry | kSampledFragment | kSampledCompute | kSampledTraceRays,
  413. kAllStorage = kStorageGeometryRead | kStorageGeometryWrite | kStorageFragmentRead | kStorageFragmentWrite | kStorageComputeRead
  414. | kStorageComputeWrite | kStorageTraceRaysRead | kStorageTraceRaysWrite,
  415. kAllFramebuffer = kFramebufferRead | kFramebufferWrite,
  416. kAllGeometry = kSampledGeometry | kStorageGeometryRead | kStorageGeometryWrite,
  417. kAllFragment = kSampledFragment | kStorageFragmentRead | kStorageFragmentWrite,
  418. kAllGraphics = kSampledGeometry | kSampledFragment | kStorageGeometryRead | kStorageGeometryWrite | kStorageFragmentRead | kStorageFragmentWrite
  419. | kFramebufferRead | kFramebufferWrite | kFramebufferShadingRate,
  420. kAllCompute = kSampledCompute | kStorageComputeRead | kStorageComputeWrite,
  421. kAllTransfer = kTransferDestination,
  422. kAllRead = kAllSampled | kStorageGeometryRead | kStorageFragmentRead | kStorageComputeRead | kStorageTraceRaysRead | kFramebufferRead
  423. | kFramebufferShadingRate | kPresent,
  424. kAllWrite =
  425. kStorageGeometryWrite | kStorageFragmentWrite | kStorageComputeWrite | kStorageTraceRaysWrite | kFramebufferWrite | kTransferDestination,
  426. kAll = kAllRead | kAllWrite,
  427. kAllShaderResource = kAllSampled | kAllStorage,
  428. kAllSrv = (kAllSampled | kAllStorage) & kAllRead,
  429. kAllUav = kAllStorage & kAllWrite,
  430. };
  431. ANKI_ENUM_ALLOW_NUMERIC_OPERATIONS(TextureUsageBit)
  432. enum class SamplingFilter : U8
  433. {
  434. kNearest,
  435. kLinear,
  436. kMin, ///< It calculates the min of a 2x2 quad. Only if GpuDeviceCapabilities::m_samplingFilterMinMax is supported.
  437. kMax, ///< It calculates the max of a 2x2 quad. Only if GpuDeviceCapabilities::m_samplingFilterMinMax is supported.
  438. };
  439. enum class SamplingAddressing : U8
  440. {
  441. kClamp,
  442. kRepeat,
  443. kBlack,
  444. kWhite,
  445. kCount,
  446. kFirst = 0,
  447. kLast = kCount - 1,
  448. };
  449. enum class ShaderType : U16
  450. {
  451. kVertex,
  452. kTessellationControl,
  453. kTessellationEvaluation,
  454. kGeometry,
  455. kTask,
  456. kMesh,
  457. kFragment,
  458. kCompute,
  459. kRayGen,
  460. kAnyHit,
  461. kClosestHit,
  462. kMiss,
  463. kIntersection,
  464. kCallable,
  465. kWorkGraph,
  466. kCount,
  467. kFirst = 0,
  468. kLast = kCount - 1,
  469. kFirstGraphics = kVertex,
  470. kLastGraphics = kFragment,
  471. kFirstRayTracing = kRayGen,
  472. kLastRayTracing = kCallable,
  473. };
  474. ANKI_ENUM_ALLOW_NUMERIC_OPERATIONS(ShaderType)
  475. enum class ShaderTypeBit : U16
  476. {
  477. kVertex = 1 << 0,
  478. kTessellationControl = 1 << 1,
  479. kTessellationEvaluation = 1 << 2,
  480. kGeometry = 1 << 3,
  481. kTask = 1 << 4,
  482. kMesh = 1 << 5,
  483. kFragment = 1 << 6,
  484. kCompute = 1 << 7,
  485. kRayGen = 1 << 8,
  486. kAnyHit = 1 << 9,
  487. kClosestHit = 1 << 10,
  488. kMiss = 1 << 11,
  489. kIntersection = 1 << 12,
  490. kCallable = 1 << 13,
  491. kWorkGraph = 1 << 14,
  492. kNone = 0,
  493. kAllGraphics = kVertex | kTessellationControl | kTessellationEvaluation | kGeometry | kTask | kMesh | kFragment,
  494. kAllLegacyGeometry = kVertex | kTessellationControl | kTessellationEvaluation | kGeometry,
  495. kAllModernGeometry = kTask | kMesh,
  496. kAllRayTracing = kRayGen | kAnyHit | kClosestHit | kMiss | kIntersection | kCallable,
  497. kAllHit = kAnyHit | kClosestHit,
  498. kAll = kAllGraphics | kCompute | kAllRayTracing | kWorkGraph,
  499. };
  500. ANKI_ENUM_ALLOW_NUMERIC_OPERATIONS(ShaderTypeBit)
  501. enum class ShaderVariableDataType : U8
  502. {
  503. kNone,
  504. #define ANKI_SVDT_MACRO(type, baseType, rowCount, columnCount, isIntagralType) k##type,
  505. #define ANKI_SVDT_MACRO_OPAQUE(constant, type) k##constant,
  506. #include <AnKi/Gr/ShaderVariableDataType.def.h>
  507. #undef ANKI_SVDT_MACRO
  508. #undef ANKI_SVDT_MACRO_OPAQUE
  509. // Derived
  510. kCount,
  511. kNumericsFirst = kI32,
  512. kNumericsLast = kMat4,
  513. kNumeric1ComponentFirst = kI32,
  514. kNumeric1ComponentLast = kF32,
  515. kNumeric2ComponentFirst = kIVec2,
  516. kNumeric2ComponentLast = kVec2,
  517. kNumeric3ComponentFirst = kIVec3,
  518. kNumeric3ComponentLast = kVec3,
  519. kNumeric4ComponentFirst = kIVec4,
  520. kNumeric4ComponentLast = kVec4,
  521. kMatrixFirst = kMat3,
  522. kMatrixLast = kMat4,
  523. kTextureFirst = kTexture1D,
  524. kTextureLast = kTextureCubeArray,
  525. kImageFirst = kImage1D,
  526. kImageLast = kImageCubeArray,
  527. };
  528. ANKI_ENUM_ALLOW_NUMERIC_OPERATIONS(ShaderVariableDataType)
  529. class ShaderVariableDataTypeInfo
  530. {
  531. public:
  532. const Char* m_name;
  533. U32 m_size; ///< Size of the type.
  534. Bool m_opaque;
  535. Bool m_isIntegral; ///< If true is integral type. Else it's float.
  536. };
  537. ANKI_PURE const ShaderVariableDataTypeInfo& getShaderVariableDataTypeInfo(ShaderVariableDataType type);
  538. /// Occlusion query result bit.
  539. enum class OcclusionQueryResultBit : U8
  540. {
  541. kNotAvailable = 1 << 0,
  542. kVisible = 1 << 1,
  543. kNotVisible = 1 << 2
  544. };
  545. ANKI_ENUM_ALLOW_NUMERIC_OPERATIONS(OcclusionQueryResultBit)
  546. /// Occlusion query result.
  547. enum class OcclusionQueryResult : U8
  548. {
  549. kNotAvailable,
  550. kVisible,
  551. kNotVisible
  552. };
  553. /// Timestamp query result.
  554. enum class TimestampQueryResult : U8
  555. {
  556. kNotAvailable,
  557. kAvailable
  558. };
  559. /// Pipeline query result.
  560. enum class PipelineQueryResult : U8
  561. {
  562. kNotAvailable,
  563. kAvailable
  564. };
  565. enum class PipelineQueryType : U8
  566. {
  567. kPrimitivesPassedClipping,
  568. kCount
  569. };
  570. /// Attachment load operation.
  571. enum class RenderTargetLoadOperation : U8
  572. {
  573. kLoad,
  574. kClear,
  575. kDontCare
  576. };
  577. /// Attachment store operation.
  578. enum class RenderTargetStoreOperation : U8
  579. {
  580. kStore,
  581. kDontCare
  582. };
  583. /// Buffer usage modes.
  584. /// The graphics work consists of the following pipes: indirect, geometry (all programmable and fixed function geometry stages) and finaly fragment.
  585. /// The compute from the consists of the following: indirect and compute.
  586. /// The trace rays from the: indirect and trace_rays
  587. /// !!WARNING!! If you change this remember to change PrivateBufferUsageBit.
  588. enum class BufferUsageBit : U64
  589. {
  590. kNone = 0,
  591. kUniformGeometry = 1ull << 0ull,
  592. kUniformFragment = 1ull << 1ull,
  593. kUniformCompute = 1ull << 2ull,
  594. kUniformTraceRays = 1ull << 3ull,
  595. kStorageGeometryRead = 1ull << 4ull,
  596. kStorageGeometryWrite = 1ull << 5ull,
  597. kStorageFragmentRead = 1ull << 6ull,
  598. kStorageFragmentWrite = 1ull << 7ull,
  599. kStorageComputeRead = 1ull << 8ull,
  600. kStorageComputeWrite = 1ull << 9ull,
  601. kStorageTraceRaysRead = 1ull << 10ull,
  602. kStorageTraceRaysWrite = 1ull << 11ull,
  603. kTexelGeometryRead = 1ull << 12ull,
  604. kTexelGeometryWrite = 1ull << 13ull,
  605. kTexelFragmentRead = 1ull << 14ull,
  606. kTexelFragmentWrite = 1ull << 15ull,
  607. kTexelComputeRead = 1ull << 16ull,
  608. kTexelComputeWrite = 1ull << 17ull,
  609. kTexelTraceRaysRead = 1ull << 18ull,
  610. kTexelTraceRaysWrite = 1ull << 19ull,
  611. kIndex = 1ull << 20ull,
  612. kVertex = 1ull << 21ull,
  613. kIndirectCompute = 1ull << 22ll,
  614. kIndirectDraw = 1ull << 23ull,
  615. kIndirectTraceRays = 1ull << 24ull,
  616. kTransferSource = 1ull << 25ull,
  617. kTransferDestination = 1ull << 26ull,
  618. kAccelerationStructureBuild = 1ull << 27ull, ///< Will be used as a position or index buffer in a BLAS build.
  619. kShaderBindingTable = 1ull << 28ull, ///< Will be used as SBT in a traceRays() command.
  620. kAccelerationStructureBuildScratch = 1ull << 29ull, ///< Used in buildAccelerationStructureXXX commands.
  621. // Derived
  622. kAllUniform = kUniformGeometry | kUniformFragment | kUniformCompute | kUniformTraceRays,
  623. kAllStorage = kStorageGeometryRead | kStorageGeometryWrite | kStorageFragmentRead | kStorageFragmentWrite | kStorageComputeRead
  624. | kStorageComputeWrite | kStorageTraceRaysRead | kStorageTraceRaysWrite,
  625. kAllTexel = kTexelGeometryRead | kTexelGeometryWrite | kTexelFragmentRead | kTexelFragmentWrite | kTexelComputeRead | kTexelComputeWrite
  626. | kTexelTraceRaysRead | kTexelTraceRaysWrite,
  627. kAllIndirect = kIndirectCompute | kIndirectDraw | kIndirectTraceRays,
  628. kAllTransfer = kTransferSource | kTransferDestination,
  629. kAllGeometry = kUniformGeometry | kStorageGeometryRead | kStorageGeometryWrite | kTexelGeometryRead | kTexelGeometryWrite | kIndex | kVertex,
  630. kAllFragment = kUniformFragment | kStorageFragmentRead | kStorageFragmentWrite | kTexelFragmentRead | kTexelFragmentWrite,
  631. kAllGraphics = kAllGeometry | kAllFragment | kIndirectDraw,
  632. kAllCompute = kUniformCompute | kStorageComputeRead | kStorageComputeWrite | kTexelComputeRead | kTexelComputeWrite | kIndirectCompute,
  633. kAllTraceRays = kUniformTraceRays | kStorageTraceRaysRead | kStorageTraceRaysWrite | kTexelTraceRaysRead | kTexelTraceRaysWrite
  634. | kIndirectTraceRays | kShaderBindingTable,
  635. kAllRayTracing = kAllTraceRays | kAccelerationStructureBuild | kAccelerationStructureBuildScratch,
  636. kAllRead = kAllUniform | kStorageGeometryRead | kStorageFragmentRead | kStorageComputeRead | kStorageTraceRaysRead | kTexelGeometryRead
  637. | kTexelFragmentRead | kTexelComputeRead | kTexelTraceRaysRead | kIndex | kVertex | kIndirectCompute | kIndirectDraw
  638. | kIndirectTraceRays | kTransferSource | kAccelerationStructureBuild | kShaderBindingTable,
  639. kAllWrite = kStorageGeometryWrite | kStorageFragmentWrite | kStorageComputeWrite | kStorageTraceRaysWrite | kTexelGeometryWrite
  640. | kTexelFragmentWrite | kTexelComputeWrite | kTexelTraceRaysWrite | kTransferDestination | kAccelerationStructureBuildScratch,
  641. kAll = kAllRead | kAllWrite,
  642. };
  643. ANKI_ENUM_ALLOW_NUMERIC_OPERATIONS(BufferUsageBit)
  644. /// Buffer access when mapped.
  645. enum class BufferMapAccessBit : U8
  646. {
  647. kNone = 0,
  648. kRead = 1 << 0,
  649. kWrite = 1 << 1,
  650. kReadWrite = kRead | kWrite
  651. };
  652. ANKI_ENUM_ALLOW_NUMERIC_OPERATIONS(BufferMapAccessBit)
  653. /// Index buffer's index type.
  654. enum class IndexType : U8
  655. {
  656. kU16,
  657. kU32,
  658. kCount
  659. };
  660. inline U32 getIndexSize(IndexType type)
  661. {
  662. ANKI_ASSERT(type < IndexType::kCount);
  663. return 2u << U32(type);
  664. }
  665. /// Acceleration structure type.
  666. enum class AccelerationStructureType : U8
  667. {
  668. kTopLevel,
  669. kBottomLevel,
  670. kCount
  671. };
  672. enum class AccelerationStructureUsageBit : U8
  673. {
  674. kNone = 0,
  675. kBuild = 1 << 0,
  676. kAttach = 1 << 1, ///< Attached to a TLAS. Only for BLAS.
  677. kGeometryRead = 1 << 2,
  678. kFragmentRead = 1 << 3,
  679. kComputeRead = 1 << 4,
  680. kTraceRaysRead = 1 << 5,
  681. // Derived
  682. kAllGraphics = kGeometryRead | kFragmentRead,
  683. kAllRead = kAttach | kGeometryRead | kFragmentRead | kComputeRead | kTraceRaysRead,
  684. kAllWrite = kBuild
  685. };
  686. ANKI_ENUM_ALLOW_NUMERIC_OPERATIONS(AccelerationStructureUsageBit)
  687. /// VRS rates.
  688. enum class VrsRate : U8
  689. {
  690. k1x1, ///< Disable VRS. Always supported.
  691. k2x1, ///< Always supported.
  692. k1x2,
  693. k2x2, ///< Always supported.
  694. k4x2,
  695. k2x4,
  696. k4x4,
  697. kCount,
  698. kFirst = 0
  699. };
  700. ANKI_ENUM_ALLOW_NUMERIC_OPERATIONS(VrsRate)
  701. enum class GpuQueueType : U8
  702. {
  703. kGeneral,
  704. kCompute,
  705. kCount,
  706. kFirst = 0
  707. };
  708. ANKI_ENUM_ALLOW_NUMERIC_OPERATIONS(GpuQueueType)
  709. enum class HlslResourceType : U8
  710. {
  711. kCbv,
  712. kSrv,
  713. kUav,
  714. kSampler, // !!!!WARNING!!! Keep it last
  715. kCount,
  716. kFirst = 0
  717. };
  718. ANKI_ENUM_ALLOW_NUMERIC_OPERATIONS(HlslResourceType)
  719. enum class VertexAttributeSemantic : U8
  720. {
  721. kPosition,
  722. kNormal,
  723. kTexCoord,
  724. kColor,
  725. kMisc0,
  726. kMisc1,
  727. kMisc2,
  728. kMisc3,
  729. kCount,
  730. kFirst = 0
  731. };
  732. ANKI_ENUM_ALLOW_NUMERIC_OPERATIONS(VertexAttributeSemantic)
  733. /// This doesn't match Vulkan or D3D.
  734. enum class DescriptorType : U8
  735. {
  736. kTexture, ///< Vulkan: Image (sampled or storage). D3D: Textures
  737. kSampler,
  738. kUniformBuffer,
  739. kStorageBuffer, ///< Vulkan: Storage buffers. D3D: Structured, ByteAddressBuffer
  740. kTexelBuffer,
  741. kAccelerationStructure,
  742. kCount,
  743. kFirst = 0
  744. };
  745. ANKI_ENUM_ALLOW_NUMERIC_OPERATIONS(DescriptorType)
  746. enum class DescriptorFlag : U8
  747. {
  748. kNone,
  749. kRead = 1 << 0,
  750. kWrite = 1 << 1,
  751. kReadWrite = kRead | kWrite,
  752. kByteAddressBuffer = 1 << 2
  753. };
  754. ANKI_ENUM_ALLOW_NUMERIC_OPERATIONS(DescriptorFlag)
  755. inline HlslResourceType descriptorTypeToHlslResourceType(DescriptorType type, DescriptorFlag flag)
  756. {
  757. ANKI_ASSERT(type < DescriptorType::kCount && flag != DescriptorFlag::kNone);
  758. if(type == DescriptorType::kSampler)
  759. {
  760. return HlslResourceType::kSampler;
  761. }
  762. else if(type == DescriptorType::kUniformBuffer)
  763. {
  764. return HlslResourceType::kCbv;
  765. }
  766. else if(!!(flag & DescriptorFlag::kWrite))
  767. {
  768. return HlslResourceType::kUav;
  769. }
  770. else
  771. {
  772. return HlslResourceType::kSrv;
  773. }
  774. }
  775. ANKI_BEGIN_PACKED_STRUCT
  776. class ShaderReflectionBinding
  777. {
  778. public:
  779. U32 m_registerBindingPoint = kMaxU32;
  780. U16 m_arraySize = 0;
  781. union
  782. {
  783. U16 m_vkBinding = kMaxU16; ///< Filled by the VK backend.
  784. U16 m_d3dStructuredBufferStride;
  785. };
  786. DescriptorType m_type = DescriptorType::kCount;
  787. DescriptorFlag m_flags = DescriptorFlag::kNone;
  788. Bool operator<(const ShaderReflectionBinding& b) const
  789. {
  790. #define ANKI_LESS(member) \
  791. if(member != b.member) \
  792. { \
  793. return member < b.member; \
  794. }
  795. const HlslResourceType hlslType = descriptorTypeToHlslResourceType(m_type, m_flags);
  796. const HlslResourceType bhlslType = descriptorTypeToHlslResourceType(b.m_type, b.m_flags);
  797. if(hlslType != bhlslType)
  798. {
  799. return hlslType < bhlslType;
  800. }
  801. ANKI_LESS(m_registerBindingPoint)
  802. ANKI_LESS(m_arraySize)
  803. ANKI_LESS(m_d3dStructuredBufferStride)
  804. #undef ANKI_LESS
  805. return false;
  806. }
  807. Bool operator==(const ShaderReflectionBinding& b) const
  808. {
  809. return memcmp(this, &b, sizeof(*this)) == 0;
  810. }
  811. void validate() const
  812. {
  813. ANKI_ASSERT(m_registerBindingPoint < kMaxU32);
  814. ANKI_ASSERT(m_type < DescriptorType::kCount);
  815. ANKI_ASSERT(m_flags != DescriptorFlag::kNone);
  816. ANKI_ASSERT(m_arraySize > 0);
  817. ANKI_ASSERT(ANKI_GR_BACKEND_VULKAN || m_type != DescriptorType::kStorageBuffer || m_d3dStructuredBufferStride != 0);
  818. }
  819. };
  820. ANKI_END_PACKED_STRUCT
  821. ANKI_BEGIN_PACKED_STRUCT
  822. class ShaderReflectionDescriptorRelated
  823. {
  824. public:
  825. Array2d<ShaderReflectionBinding, kMaxDescriptorSets, kMaxBindingsPerDescriptorSet> m_bindings;
  826. Array<U8, kMaxDescriptorSets> m_bindingCounts = {};
  827. U32 m_pushConstantsSize = 0;
  828. Bool m_hasVkBindlessDescriptorSet = false; ///< Filled by the shader compiler.
  829. U8 m_vkBindlessDescriptorSet = kMaxU8; ///< Filled by the VK backend.
  830. void validate() const
  831. {
  832. for(U32 set = 0; set < kMaxDescriptorSets; ++set)
  833. {
  834. for(U32 ibinding = 0; ibinding < kMaxBindingsPerDescriptorSet; ++ibinding)
  835. {
  836. const ShaderReflectionBinding& binding = m_bindings[set][ibinding];
  837. if(binding.m_type != DescriptorType::kCount)
  838. {
  839. ANKI_ASSERT(ibinding < m_bindingCounts[set]);
  840. binding.validate();
  841. }
  842. else
  843. {
  844. ANKI_ASSERT(ibinding >= m_bindingCounts[set]);
  845. }
  846. }
  847. }
  848. }
  849. };
  850. ANKI_END_PACKED_STRUCT
  851. class ShaderReflection
  852. {
  853. public:
  854. ShaderReflectionDescriptorRelated m_descriptor;
  855. class
  856. {
  857. public:
  858. Array<U8, U32(VertexAttributeSemantic::kCount)> m_vkVertexAttributeLocations;
  859. BitSet<U32(VertexAttributeSemantic::kCount), U8> m_vertexAttributeMask = {false};
  860. } m_vertex;
  861. class
  862. {
  863. public:
  864. BitSet<kMaxColorRenderTargets, U8> m_colorAttachmentWritemask = {false};
  865. Bool m_discards = false;
  866. } m_fragment;
  867. ShaderReflection()
  868. {
  869. m_vertex.m_vkVertexAttributeLocations.fill(kMaxU8);
  870. }
  871. void validate() const
  872. {
  873. m_descriptor.validate();
  874. for([[maybe_unused]] VertexAttributeSemantic semantic : EnumIterable<VertexAttributeSemantic>())
  875. {
  876. ANKI_ASSERT(!m_vertex.m_vertexAttributeMask.get(semantic) || m_vertex.m_vkVertexAttributeLocations[semantic] != kMaxU8);
  877. }
  878. const U32 attachmentCount = m_fragment.m_colorAttachmentWritemask.getSetBitCount();
  879. for(U32 i = 0; i < attachmentCount; ++i)
  880. {
  881. ANKI_ASSERT(m_fragment.m_colorAttachmentWritemask.get(i) && "Should write to all attachments");
  882. }
  883. }
  884. /// Combine shader reflection.
  885. static Error linkShaderReflection(const ShaderReflection& a, const ShaderReflection& b, ShaderReflection& c);
  886. };
  887. /// Clear values for textures or attachments.
  888. class ClearValue
  889. {
  890. private:
  891. class Ds
  892. {
  893. public:
  894. F32 m_depth;
  895. I32 m_stencil;
  896. };
  897. public:
  898. union
  899. {
  900. Array<F32, 4> m_colorf;
  901. Array<I32, 4> m_colori;
  902. Array<U32, 4> m_coloru;
  903. Ds m_depthStencil;
  904. };
  905. ClearValue()
  906. {
  907. zeroMemory(*this);
  908. }
  909. ClearValue(const ClearValue& b)
  910. {
  911. operator=(b);
  912. }
  913. ClearValue& operator=(const ClearValue& b)
  914. {
  915. memcpy(this, &b, sizeof(*this));
  916. return *this;
  917. }
  918. };
  919. /// Compute max number of mipmaps for a 2D texture.
  920. U32 computeMaxMipmapCount2d(U32 w, U32 h, U32 minSizeOfLastMip = 1);
  921. /// Compute max number of mipmaps for a 3D texture.
  922. U32 computeMaxMipmapCount3d(U32 w, U32 h, U32 d, U32 minSizeOfLastMip = 1);
  923. /// Visit a SPIR-V binary.
  924. template<typename TArray, typename TFunc>
  925. static void visitSpirv(TArray spv, TFunc func)
  926. {
  927. ANKI_ASSERT(spv.getSize() > 5);
  928. auto it = &spv[5];
  929. do
  930. {
  931. const U32 instructionCount = *it >> 16u;
  932. const U32 opcode = *it & 0xFFFFu;
  933. TArray instructions(it + 1, instructionCount - 1);
  934. func(opcode, instructions);
  935. it += instructionCount;
  936. } while(it < spv.getEnd());
  937. ANKI_ASSERT(it == spv.getEnd());
  938. }
  939. /// @}
  940. } // end namespace anki