CommandBufferImpl.h 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655
  1. // Copyright (C) 2009-2023, 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/CommandBuffer.h>
  7. #include <AnKi/Gr/Vulkan/CommandBufferFactory.h>
  8. #include <AnKi/Gr/CommandBuffer.h>
  9. #include <AnKi/Gr/Texture.h>
  10. #include <AnKi/Gr/Buffer.h>
  11. #include <AnKi/Gr/Shader.h>
  12. #include <AnKi/Gr/Vulkan/BufferImpl.h>
  13. #include <AnKi/Gr/Vulkan/TextureImpl.h>
  14. #include <AnKi/Gr/Vulkan/Pipeline.h>
  15. #include <AnKi/Gr/Vulkan/GrManagerImpl.h>
  16. #include <AnKi/Util/List.h>
  17. namespace anki {
  18. #define ANKI_BATCH_COMMANDS 1
  19. // Forward
  20. class CommandBufferInitInfo;
  21. /// @addtogroup vulkan
  22. /// @{
  23. /// Command buffer implementation.
  24. class CommandBufferImpl final : public CommandBuffer
  25. {
  26. public:
  27. /// Default constructor
  28. CommandBufferImpl(CString name)
  29. : CommandBuffer(name)
  30. {
  31. }
  32. ~CommandBufferImpl();
  33. Error init(const CommandBufferInitInfo& init);
  34. void setFence(MicroFence* fence)
  35. {
  36. m_microCmdb->setFence(fence);
  37. }
  38. const MicroCommandBufferPtr& getMicroCommandBuffer()
  39. {
  40. return m_microCmdb;
  41. }
  42. VkCommandBuffer getHandle() const
  43. {
  44. ANKI_ASSERT(m_handle);
  45. return m_handle;
  46. }
  47. Bool renderedToDefaultFramebuffer() const
  48. {
  49. return m_renderedToDefaultFb;
  50. }
  51. Bool isEmpty() const
  52. {
  53. return m_empty;
  54. }
  55. Bool isSecondLevel() const
  56. {
  57. return !!(m_flags & CommandBufferFlag::kSecondLevel);
  58. }
  59. ANKI_FORCE_INLINE void bindVertexBufferInternal(U32 binding, Buffer* buff, PtrSize offset, PtrSize stride, VertexStepRate stepRate)
  60. {
  61. commandCommon();
  62. m_state.bindVertexBuffer(binding, stride, stepRate);
  63. const VkBuffer vkbuff = static_cast<const BufferImpl&>(*buff).getHandle();
  64. vkCmdBindVertexBuffers(m_handle, binding, 1, &vkbuff, &offset);
  65. }
  66. ANKI_FORCE_INLINE void setVertexAttributeInternal(U32 location, U32 buffBinding, const Format fmt, PtrSize relativeOffset)
  67. {
  68. commandCommon();
  69. m_state.setVertexAttribute(location, buffBinding, fmt, relativeOffset);
  70. }
  71. ANKI_FORCE_INLINE void bindIndexBufferInternal(Buffer* buff, PtrSize offset, IndexType type)
  72. {
  73. commandCommon();
  74. vkCmdBindIndexBuffer(m_handle, static_cast<const BufferImpl&>(*buff).getHandle(), offset, convertIndexType(type));
  75. }
  76. ANKI_FORCE_INLINE void setPrimitiveRestartInternal(Bool enable)
  77. {
  78. commandCommon();
  79. m_state.setPrimitiveRestart(enable);
  80. }
  81. ANKI_FORCE_INLINE void setFillModeInternal(FillMode mode)
  82. {
  83. commandCommon();
  84. m_state.setFillMode(mode);
  85. }
  86. ANKI_FORCE_INLINE void setCullModeInternal(FaceSelectionBit mode)
  87. {
  88. commandCommon();
  89. m_state.setCullMode(mode);
  90. }
  91. ANKI_FORCE_INLINE void setViewportInternal(U32 minx, U32 miny, U32 width, U32 height)
  92. {
  93. ANKI_ASSERT(width > 0 && height > 0);
  94. commandCommon();
  95. if(m_viewport[0] != minx || m_viewport[1] != miny || m_viewport[2] != width || m_viewport[3] != height)
  96. {
  97. m_viewportDirty = true;
  98. m_viewport[0] = minx;
  99. m_viewport[1] = miny;
  100. m_viewport[2] = width;
  101. m_viewport[3] = height;
  102. }
  103. }
  104. ANKI_FORCE_INLINE void setScissorInternal(U32 minx, U32 miny, U32 width, U32 height)
  105. {
  106. ANKI_ASSERT(width > 0 && height > 0);
  107. commandCommon();
  108. if(m_scissor[0] != minx || m_scissor[1] != miny || m_scissor[2] != width || m_scissor[3] != height)
  109. {
  110. m_scissorDirty = true;
  111. m_scissor[0] = minx;
  112. m_scissor[1] = miny;
  113. m_scissor[2] = width;
  114. m_scissor[3] = height;
  115. }
  116. }
  117. ANKI_FORCE_INLINE void setPolygonOffsetInternal(F32 factor, F32 units)
  118. {
  119. commandCommon();
  120. m_state.setPolygonOffset(factor, units);
  121. vkCmdSetDepthBias(m_handle, factor, 0.0f, units);
  122. }
  123. ANKI_FORCE_INLINE void setStencilOperationsInternal(FaceSelectionBit face, StencilOperation stencilFail, StencilOperation stencilPassDepthFail,
  124. StencilOperation stencilPassDepthPass)
  125. {
  126. commandCommon();
  127. m_state.setStencilOperations(face, stencilFail, stencilPassDepthFail, stencilPassDepthPass);
  128. }
  129. ANKI_FORCE_INLINE void setStencilCompareOperationInternal(FaceSelectionBit face, CompareOperation comp)
  130. {
  131. commandCommon();
  132. m_state.setStencilCompareOperation(face, comp);
  133. }
  134. void setStencilCompareMaskInternal(FaceSelectionBit face, U32 mask);
  135. void setStencilWriteMaskInternal(FaceSelectionBit face, U32 mask);
  136. void setStencilReferenceInternal(FaceSelectionBit face, U32 ref);
  137. ANKI_FORCE_INLINE void setDepthWriteInternal(Bool enable)
  138. {
  139. commandCommon();
  140. m_state.setDepthWrite(enable);
  141. }
  142. ANKI_FORCE_INLINE void setDepthCompareOperationInternal(CompareOperation op)
  143. {
  144. commandCommon();
  145. m_state.setDepthCompareOperation(op);
  146. }
  147. ANKI_FORCE_INLINE void setAlphaToCoverageInternal(Bool enable)
  148. {
  149. commandCommon();
  150. m_state.setAlphaToCoverage(enable);
  151. }
  152. ANKI_FORCE_INLINE void setColorChannelWriteMaskInternal(U32 attachment, ColorBit mask)
  153. {
  154. commandCommon();
  155. m_state.setColorChannelWriteMask(attachment, mask);
  156. }
  157. ANKI_FORCE_INLINE void setBlendFactorsInternal(U32 attachment, BlendFactor srcRgb, BlendFactor dstRgb, BlendFactor srcA, BlendFactor dstA)
  158. {
  159. commandCommon();
  160. m_state.setBlendFactors(attachment, srcRgb, dstRgb, srcA, dstA);
  161. }
  162. ANKI_FORCE_INLINE void setBlendOperationInternal(U32 attachment, BlendOperation funcRgb, BlendOperation funcA)
  163. {
  164. commandCommon();
  165. m_state.setBlendOperation(attachment, funcRgb, funcA);
  166. }
  167. ANKI_FORCE_INLINE void bindTextureAndSamplerInternal(U32 set, U32 binding, TextureView* texView, Sampler* sampler, U32 arrayIdx)
  168. {
  169. commandCommon();
  170. const TextureViewImpl& view = static_cast<const TextureViewImpl&>(*texView);
  171. const TextureImpl& tex = view.getTextureImpl();
  172. ANKI_ASSERT(tex.isSubresourceGoodForSampling(view.getSubresource()));
  173. const VkImageLayout lay = tex.computeLayout(TextureUsageBit::kAllSampled & tex.getTextureUsage(), 0);
  174. m_dsetState[set].bindTextureAndSampler(binding, arrayIdx, &view, sampler, lay);
  175. m_microCmdb->pushObjectRef(sampler);
  176. }
  177. ANKI_FORCE_INLINE void bindTextureInternal(U32 set, U32 binding, TextureView* texView, U32 arrayIdx)
  178. {
  179. commandCommon();
  180. const TextureViewImpl& view = static_cast<const TextureViewImpl&>(*texView);
  181. const TextureImpl& tex = view.getTextureImpl();
  182. ANKI_ASSERT(tex.isSubresourceGoodForSampling(view.getSubresource()));
  183. const VkImageLayout lay = tex.computeLayout(TextureUsageBit::kAllSampled & tex.getTextureUsage(), 0);
  184. m_dsetState[set].bindTexture(binding, arrayIdx, texView, lay);
  185. }
  186. ANKI_FORCE_INLINE void bindSamplerInternal(U32 set, U32 binding, Sampler* sampler, U32 arrayIdx)
  187. {
  188. commandCommon();
  189. m_dsetState[set].bindSampler(binding, arrayIdx, sampler);
  190. m_microCmdb->pushObjectRef(sampler);
  191. }
  192. ANKI_FORCE_INLINE void bindUavTextureInternal(U32 set, U32 binding, TextureView* img, U32 arrayIdx)
  193. {
  194. commandCommon();
  195. m_dsetState[set].bindUavTexture(binding, arrayIdx, img);
  196. const Bool isPresentable = !!(static_cast<const TextureViewImpl&>(*img).getTextureImpl().getTextureUsage() & TextureUsageBit::kPresent);
  197. if(isPresentable)
  198. {
  199. m_renderedToDefaultFb = true;
  200. }
  201. }
  202. ANKI_FORCE_INLINE void bindAccelerationStructureInternal(U32 set, U32 binding, AccelerationStructure* as, U32 arrayIdx)
  203. {
  204. commandCommon();
  205. m_dsetState[set].bindAccelerationStructure(binding, arrayIdx, as);
  206. m_microCmdb->pushObjectRef(as);
  207. }
  208. ANKI_FORCE_INLINE void bindAllBindlessInternal(U32 set)
  209. {
  210. commandCommon();
  211. m_dsetState[set].bindBindlessDescriptorSet();
  212. }
  213. void beginRenderPassInternal(Framebuffer* fb, const Array<TextureUsageBit, kMaxColorRenderTargets>& colorAttachmentUsages,
  214. TextureUsageBit depthStencilAttachmentUsage, U32 minx, U32 miny, U32 width, U32 height);
  215. void endRenderPassInternal();
  216. ANKI_FORCE_INLINE void setVrsRateInternal(VrsRate rate)
  217. {
  218. ANKI_ASSERT(getGrManagerImpl().getDeviceCapabilities().m_vrs);
  219. ANKI_ASSERT(rate < VrsRate::kCount);
  220. commandCommon();
  221. if(m_vrsRate != rate)
  222. {
  223. m_vrsRate = rate;
  224. m_vrsRateDirty = true;
  225. }
  226. }
  227. ANKI_FORCE_INLINE void drawInternal(PrimitiveTopology topology, U32 count, U32 instanceCount, U32 first, U32 baseInstance)
  228. {
  229. m_state.setPrimitiveTopology(topology);
  230. drawcallCommon();
  231. vkCmdDraw(m_handle, count, instanceCount, first, baseInstance);
  232. }
  233. ANKI_FORCE_INLINE void drawIndexedInternal(PrimitiveTopology topology, U32 count, U32 instanceCount, U32 firstIndex, U32 baseVertex,
  234. U32 baseInstance)
  235. {
  236. m_state.setPrimitiveTopology(topology);
  237. drawcallCommon();
  238. vkCmdDrawIndexed(m_handle, count, instanceCount, firstIndex, baseVertex, baseInstance);
  239. }
  240. ANKI_FORCE_INLINE void drawIndirectInternal(PrimitiveTopology topology, U32 drawCount, PtrSize offset, Buffer* buff)
  241. {
  242. m_state.setPrimitiveTopology(topology);
  243. drawcallCommon();
  244. const BufferImpl& impl = static_cast<const BufferImpl&>(*buff);
  245. ANKI_ASSERT(impl.usageValid(BufferUsageBit::kIndirectDraw));
  246. ANKI_ASSERT((offset % 4) == 0);
  247. ANKI_ASSERT((offset + sizeof(DrawIndirectArgs) * drawCount) <= impl.getSize());
  248. vkCmdDrawIndirect(m_handle, impl.getHandle(), offset, drawCount, sizeof(DrawIndirectArgs));
  249. }
  250. ANKI_FORCE_INLINE void drawIndexedIndirectInternal(PrimitiveTopology topology, U32 drawCount, PtrSize offset, Buffer* buff)
  251. {
  252. m_state.setPrimitiveTopology(topology);
  253. drawcallCommon();
  254. const BufferImpl& impl = static_cast<const BufferImpl&>(*buff);
  255. ANKI_ASSERT(impl.usageValid(BufferUsageBit::kIndirectDraw));
  256. ANKI_ASSERT((offset % 4) == 0);
  257. ANKI_ASSERT((offset + sizeof(DrawIndexedIndirectArgs) * drawCount) <= impl.getSize());
  258. vkCmdDrawIndexedIndirect(m_handle, impl.getHandle(), offset, drawCount, sizeof(DrawIndexedIndirectArgs));
  259. }
  260. ANKI_FORCE_INLINE void drawMeshTasksInternal(U32 groupCountX, U32 groupCountY, U32 groupCountZ)
  261. {
  262. ANKI_ASSERT(!!(getGrManagerImpl().getExtensions() & VulkanExtensions::kEXT_mesh_shader));
  263. drawcallCommon();
  264. vkCmdDrawMeshTasksEXT(m_handle, groupCountX, groupCountY, groupCountZ);
  265. }
  266. ANKI_FORCE_INLINE void drawIndexedIndirectCountInternal(PrimitiveTopology topology, Buffer* argBuffer, PtrSize argBufferOffset,
  267. U32 argBufferStride, Buffer* countBuffer, PtrSize countBufferOffset, U32 maxDrawCount)
  268. {
  269. m_state.setPrimitiveTopology(topology);
  270. drawcallCommon();
  271. const BufferImpl& argBufferImpl = static_cast<const BufferImpl&>(*argBuffer);
  272. ANKI_ASSERT(argBufferImpl.usageValid(BufferUsageBit::kIndirectDraw));
  273. ANKI_ASSERT((argBufferOffset % 4) == 0);
  274. ANKI_ASSERT(argBufferStride >= sizeof(DrawIndexedIndirectArgs));
  275. ANKI_ASSERT(argBufferOffset + maxDrawCount * argBufferStride <= argBuffer->getSize());
  276. const BufferImpl& countBufferImpl = static_cast<const BufferImpl&>(*countBuffer);
  277. ANKI_ASSERT(countBufferImpl.usageValid(BufferUsageBit::kIndirectDraw));
  278. ANKI_ASSERT((countBufferOffset % 4) == 0);
  279. ANKI_ASSERT(countBufferOffset + sizeof(U32) <= countBuffer->getSize());
  280. ANKI_ASSERT(maxDrawCount > 0 && maxDrawCount <= getGrManagerImpl().getDeviceCapabilities().m_maxDrawIndirectCount);
  281. vkCmdDrawIndexedIndirectCountKHR(m_handle, argBufferImpl.getHandle(), argBufferOffset, countBufferImpl.getHandle(), countBufferOffset,
  282. maxDrawCount, argBufferStride);
  283. }
  284. ANKI_FORCE_INLINE void drawIndirectCountInternal(PrimitiveTopology topology, Buffer* argBuffer, PtrSize argBufferOffset, U32 argBufferStride,
  285. Buffer* countBuffer, PtrSize countBufferOffset, U32 maxDrawCount)
  286. {
  287. m_state.setPrimitiveTopology(topology);
  288. drawcallCommon();
  289. const BufferImpl& argBufferImpl = static_cast<const BufferImpl&>(*argBuffer);
  290. ANKI_ASSERT(argBufferImpl.usageValid(BufferUsageBit::kIndirectDraw));
  291. ANKI_ASSERT((argBufferOffset % 4) == 0);
  292. ANKI_ASSERT(argBufferStride >= sizeof(DrawIndirectArgs));
  293. ANKI_ASSERT(argBufferOffset + maxDrawCount * argBufferStride <= argBuffer->getSize());
  294. const BufferImpl& countBufferImpl = static_cast<const BufferImpl&>(*countBuffer);
  295. ANKI_ASSERT(countBufferImpl.usageValid(BufferUsageBit::kIndirectDraw));
  296. ANKI_ASSERT((countBufferOffset % 4) == 0);
  297. ANKI_ASSERT(countBufferOffset + maxDrawCount * sizeof(U32) <= countBuffer->getSize());
  298. ANKI_ASSERT(maxDrawCount > 0 && maxDrawCount <= getGrManagerImpl().getDeviceCapabilities().m_maxDrawIndirectCount);
  299. vkCmdDrawIndirectCountKHR(m_handle, argBufferImpl.getHandle(), argBufferOffset, countBufferImpl.getHandle(), countBufferOffset, maxDrawCount,
  300. argBufferStride);
  301. }
  302. void dispatchComputeInternal(U32 groupCountX, U32 groupCountY, U32 groupCountZ);
  303. void dispatchComputeIndirectInternal(Buffer* argBuffer, PtrSize argBufferOffset);
  304. void traceRaysInternal(Buffer* sbtBuffer, PtrSize sbtBufferOffset, U32 sbtRecordSize, U32 hitGroupSbtRecordCount, U32 rayTypeCount, U32 width,
  305. U32 height, U32 depth);
  306. void resetOcclusionQueriesInternal(ConstWeakArray<OcclusionQuery*> queries);
  307. void beginOcclusionQueryInternal(OcclusionQuery* query);
  308. void endOcclusionQueryInternal(OcclusionQuery* query);
  309. void resetTimestampQueriesInternal(ConstWeakArray<TimestampQuery*> queries);
  310. void writeTimestampInternal(TimestampQuery* query);
  311. void generateMipmaps2dInternal(TextureView* texView);
  312. void clearTextureViewInternal(TextureView* texView, const ClearValue& clearValue);
  313. void pushSecondLevelCommandBuffersInternal(ConstWeakArray<CommandBuffer*> cmdbs);
  314. // To enable using Anki's commandbuffers for external workloads
  315. void beginRecordingExt()
  316. {
  317. commandCommon();
  318. }
  319. void endRecording();
  320. void setPipelineBarrierInternal(ConstWeakArray<TextureBarrierInfo> textures, ConstWeakArray<BufferBarrierInfo> buffers,
  321. ConstWeakArray<AccelerationStructureBarrierInfo> accelerationStructures);
  322. void fillBufferInternal(Buffer* buff, PtrSize offset, PtrSize size, U32 value);
  323. void writeOcclusionQueriesResultToBufferInternal(ConstWeakArray<OcclusionQuery*> queries, PtrSize offset, Buffer* buff);
  324. void bindShaderProgramInternal(ShaderProgram* prog);
  325. ANKI_FORCE_INLINE void bindConstantBufferInternal(U32 set, U32 binding, Buffer* buff, PtrSize offset, PtrSize range, U32 arrayIdx)
  326. {
  327. commandCommon();
  328. m_dsetState[set].bindConstantBuffer(binding, arrayIdx, buff, offset, range);
  329. }
  330. ANKI_FORCE_INLINE void bindUavBufferInternal(U32 set, U32 binding, Buffer* buff, PtrSize offset, PtrSize range, U32 arrayIdx)
  331. {
  332. commandCommon();
  333. m_dsetState[set].bindUavBuffer(binding, arrayIdx, buff, offset, range);
  334. }
  335. ANKI_FORCE_INLINE void bindReadOnlyTextureBufferInternal(U32 set, U32 binding, Buffer* buff, PtrSize offset, PtrSize range, Format fmt,
  336. U32 arrayIdx)
  337. {
  338. commandCommon();
  339. m_dsetState[set].bindReadOnlyTextureBuffer(binding, arrayIdx, buff, offset, range, fmt);
  340. }
  341. void copyBufferToTextureViewInternal(Buffer* buff, PtrSize offset, PtrSize range, TextureView* texView);
  342. void copyBufferToBufferInternal(Buffer* src, Buffer* dst, ConstWeakArray<CopyBufferToBufferInfo> copies);
  343. void buildAccelerationStructureInternal(AccelerationStructure* as, Buffer* scratchBuffer, PtrSize scratchBufferOffset);
  344. void upscaleInternal(GrUpscaler* upscaler, TextureView* inColor, TextureView* outUpscaledColor, TextureView* motionVectors, TextureView* depth,
  345. TextureView* exposure, const Bool resetAccumulation, const Vec2& jitterOffset, const Vec2& motionVectorsScale);
  346. void setPushConstantsInternal(const void* data, U32 dataSize);
  347. ANKI_FORCE_INLINE void setRasterizationOrderInternal(RasterizationOrder order)
  348. {
  349. commandCommon();
  350. if(!!(getGrManagerImpl().getExtensions() & VulkanExtensions::kAMD_rasterization_order))
  351. {
  352. m_state.setRasterizationOrder(order);
  353. }
  354. }
  355. ANKI_FORCE_INLINE void setLineWidthInternal(F32 width)
  356. {
  357. commandCommon();
  358. vkCmdSetLineWidth(m_handle, width);
  359. #if ANKI_ASSERTIONS_ENABLED
  360. m_lineWidthSet = true;
  361. #endif
  362. }
  363. ANKI_FORCE_INLINE void pushDebugMarkerInternal(CString name, Vec3 color)
  364. {
  365. if(m_debugMarkers) [[unlikely]]
  366. {
  367. commandCommon();
  368. VkDebugUtilsLabelEXT label = {};
  369. label.sType = VK_STRUCTURE_TYPE_DEBUG_UTILS_LABEL_EXT;
  370. label.pLabelName = name.cstr();
  371. label.color[0] = color[0];
  372. label.color[1] = color[1];
  373. label.color[2] = color[2];
  374. label.color[3] = 1.0f;
  375. vkCmdBeginDebugUtilsLabelEXT(m_handle, &label);
  376. }
  377. #if ANKI_EXTRA_CHECKS
  378. ++m_debugMarkersPushed;
  379. #endif
  380. }
  381. ANKI_FORCE_INLINE void popDebugMarkerInternal()
  382. {
  383. if(m_debugMarkers) [[unlikely]]
  384. {
  385. commandCommon();
  386. vkCmdEndDebugUtilsLabelEXT(m_handle);
  387. }
  388. #if ANKI_EXTRA_CHECKS
  389. ANKI_ASSERT(m_debugMarkersPushed > 0);
  390. --m_debugMarkersPushed;
  391. #endif
  392. }
  393. private:
  394. StackMemoryPool* m_pool = nullptr;
  395. MicroCommandBufferPtr m_microCmdb;
  396. VkCommandBuffer m_handle = VK_NULL_HANDLE;
  397. ThreadId m_tid = ~ThreadId(0);
  398. CommandBufferFlag m_flags = CommandBufferFlag::kNone;
  399. Bool m_renderedToDefaultFb : 1 = false;
  400. Bool m_finalized : 1 = false;
  401. Bool m_empty : 1 = true;
  402. Bool m_beganRecording : 1 = false;
  403. Bool m_debugMarkers : 1 = false;
  404. #if ANKI_EXTRA_CHECKS
  405. U32 m_commandCount = 0;
  406. U32 m_setPushConstantsSize = 0;
  407. U32 m_debugMarkersPushed = 0;
  408. #endif
  409. Framebuffer* m_activeFb = nullptr;
  410. Array<U32, 4> m_renderArea = {0, 0, kMaxU32, kMaxU32};
  411. Array<U32, 2> m_fbSize = {0, 0};
  412. U32 m_rpCommandCount = 0; ///< Number of drawcalls or pushed cmdbs in rp.
  413. Array<TextureUsageBit, kMaxColorRenderTargets> m_colorAttachmentUsages = {};
  414. TextureUsageBit m_depthStencilAttachmentUsage = TextureUsageBit::kNone;
  415. PipelineStateTracker m_state;
  416. Array<DescriptorSetState, kMaxDescriptorSets> m_dsetState;
  417. ShaderProgramImpl* m_graphicsProg ANKI_DEBUG_CODE(= nullptr); ///< Last bound graphics program
  418. ShaderProgramImpl* m_computeProg ANKI_DEBUG_CODE(= nullptr);
  419. ShaderProgramImpl* m_rtProg ANKI_DEBUG_CODE(= nullptr);
  420. VkSubpassContents m_subpassContents = VK_SUBPASS_CONTENTS_MAX_ENUM;
  421. /// @name state_opts
  422. /// @{
  423. Array<U32, 4> m_viewport = {0, 0, 0, 0};
  424. Array<U32, 4> m_scissor = {0, 0, kMaxU32, kMaxU32};
  425. VkViewport m_lastViewport = {};
  426. Bool m_viewportDirty = true;
  427. Bool m_scissorDirty = true;
  428. VkRect2D m_lastScissor = {{-1, -1}, {kMaxU32, kMaxU32}};
  429. Array<U32, 2> m_stencilCompareMasks = {0x5A5A5A5A, 0x5A5A5A5A}; ///< Use a stupid number to initialize.
  430. Array<U32, 2> m_stencilWriteMasks = {0x5A5A5A5A, 0x5A5A5A5A};
  431. Array<U32, 2> m_stencilReferenceMasks = {0x5A5A5A5A, 0x5A5A5A5A};
  432. #if ANKI_ASSERTIONS_ENABLED
  433. Bool m_lineWidthSet = false;
  434. #endif
  435. Bool m_vrsRateDirty = true;
  436. VrsRate m_vrsRate = VrsRate::k1x1;
  437. /// Rebind the above dynamic state. Needed after pushing secondary command buffers (they dirty the state).
  438. void rebindDynamicState();
  439. /// @}
  440. /// Some common operations per command.
  441. ANKI_FORCE_INLINE void commandCommon()
  442. {
  443. ANKI_ASSERT(!m_finalized);
  444. #if ANKI_EXTRA_CHECKS
  445. ++m_commandCount;
  446. #endif
  447. m_empty = false;
  448. if(!m_beganRecording) [[unlikely]]
  449. {
  450. beginRecording();
  451. m_beganRecording = true;
  452. }
  453. ANKI_ASSERT(Thread::getCurrentThreadId() == m_tid && "Commands must be recorder and flushed by the thread this command buffer was created");
  454. ANKI_ASSERT(m_handle);
  455. }
  456. void drawcallCommon();
  457. void dispatchCommon();
  458. Bool insideRenderPass() const
  459. {
  460. return m_activeFb != nullptr;
  461. }
  462. void beginRenderPassInternal();
  463. Bool secondLevel() const
  464. {
  465. return !!(m_flags & CommandBufferFlag::kSecondLevel);
  466. }
  467. void setImageBarrier(VkPipelineStageFlags srcStage, VkAccessFlags srcAccess, VkImageLayout prevLayout, VkPipelineStageFlags dstStage,
  468. VkAccessFlags dstAccess, VkImageLayout newLayout, VkImage img, const VkImageSubresourceRange& range);
  469. void beginRecording();
  470. Bool flipViewport() const;
  471. static VkViewport computeViewport(U32* viewport, U32 fbWidth, U32 fbHeight, Bool flipvp)
  472. {
  473. const U32 minx = viewport[0];
  474. const U32 miny = viewport[1];
  475. const U32 width = min<U32>(fbWidth, viewport[2]);
  476. const U32 height = min<U32>(fbHeight, viewport[3]);
  477. ANKI_ASSERT(width > 0 && height > 0);
  478. ANKI_ASSERT(minx + width <= fbWidth);
  479. ANKI_ASSERT(miny + height <= fbHeight);
  480. VkViewport s = {};
  481. s.x = F32(minx);
  482. s.y = (flipvp) ? F32(fbHeight - miny) : F32(miny); // Move to the bottom;
  483. s.width = F32(width);
  484. s.height = (flipvp) ? -F32(height) : F32(height);
  485. s.minDepth = 0.0f;
  486. s.maxDepth = 1.0f;
  487. return s;
  488. }
  489. static VkRect2D computeScissor(U32* scissor, U32 fbWidth, U32 fbHeight, Bool flipvp)
  490. {
  491. const U32 minx = scissor[0];
  492. const U32 miny = scissor[1];
  493. const U32 width = min<U32>(fbWidth, scissor[2]);
  494. const U32 height = min<U32>(fbHeight, scissor[3]);
  495. ANKI_ASSERT(minx + width <= fbWidth);
  496. ANKI_ASSERT(miny + height <= fbHeight);
  497. VkRect2D out = {};
  498. out.extent.width = width;
  499. out.extent.height = height;
  500. out.offset.x = minx;
  501. out.offset.y = (flipvp) ? (fbHeight - (miny + height)) : miny;
  502. return out;
  503. }
  504. const ShaderProgramImpl& getBoundProgram()
  505. {
  506. if(m_graphicsProg)
  507. {
  508. ANKI_ASSERT(m_computeProg == nullptr && m_rtProg == nullptr);
  509. return *m_graphicsProg;
  510. }
  511. else if(m_computeProg)
  512. {
  513. ANKI_ASSERT(m_graphicsProg == nullptr && m_rtProg == nullptr);
  514. return *m_computeProg;
  515. }
  516. else
  517. {
  518. ANKI_ASSERT(m_graphicsProg == nullptr && m_computeProg == nullptr && m_rtProg != nullptr);
  519. return *m_rtProg;
  520. }
  521. }
  522. };
  523. /// @}
  524. } // end namespace anki
  525. #include <AnKi/Gr/Vulkan/CommandBufferImpl.inl.h>