Common.h 27 KB

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