CommandBuffer.cpp 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423
  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. #include <AnKi/Gr/CommandBuffer.h>
  6. #include <AnKi/Gr/Vulkan/CommandBufferImpl.h>
  7. #include <AnKi/Gr/Vulkan/GrManagerImpl.h>
  8. #include <AnKi/Gr/AccelerationStructure.h>
  9. #include <AnKi/Gr/Vulkan/FenceImpl.h>
  10. namespace anki {
  11. CommandBuffer* CommandBuffer::newInstance(const CommandBufferInitInfo& init)
  12. {
  13. ANKI_TRACE_SCOPED_EVENT(VkNewCommandBuffer);
  14. CommandBufferImpl* impl = anki::newInstance<CommandBufferImpl>(GrMemoryPool::getSingleton(), init.getName());
  15. const Error err = impl->init(init);
  16. if(err)
  17. {
  18. deleteInstance(GrMemoryPool::getSingleton(), impl);
  19. impl = nullptr;
  20. }
  21. return impl;
  22. }
  23. void CommandBuffer::flush(ConstWeakArray<FencePtr> waitFences, FencePtr* signalFence)
  24. {
  25. ANKI_VK_SELF(CommandBufferImpl);
  26. self.endRecording();
  27. if(!self.isSecondLevel())
  28. {
  29. Array<MicroSemaphorePtr, 8> waitSemaphores;
  30. for(U32 i = 0; i < waitFences.getSize(); ++i)
  31. {
  32. waitSemaphores[i] = static_cast<const FenceImpl&>(*waitFences[i]).m_semaphore;
  33. }
  34. MicroSemaphorePtr signalSemaphore;
  35. getGrManagerImpl().flushCommandBuffer(self.getMicroCommandBuffer(), self.renderedToDefaultFramebuffer(),
  36. WeakArray<MicroSemaphorePtr>(waitSemaphores.getBegin(), waitFences.getSize()),
  37. (signalFence) ? &signalSemaphore : nullptr);
  38. if(signalFence)
  39. {
  40. FenceImpl* fenceImpl = anki::newInstance<FenceImpl>(GrMemoryPool::getSingleton(), "SignalFence");
  41. fenceImpl->m_semaphore = signalSemaphore;
  42. signalFence->reset(fenceImpl);
  43. }
  44. }
  45. else
  46. {
  47. ANKI_ASSERT(signalFence == nullptr);
  48. ANKI_ASSERT(waitFences.getSize() == 0);
  49. }
  50. }
  51. void CommandBuffer::bindVertexBuffer(U32 binding, const BufferPtr& buff, PtrSize offset, PtrSize stride, VertexStepRate stepRate)
  52. {
  53. ANKI_VK_SELF(CommandBufferImpl);
  54. self.bindVertexBufferInternal(binding, buff, offset, stride, stepRate);
  55. }
  56. void CommandBuffer::setVertexAttribute(U32 location, U32 buffBinding, Format fmt, PtrSize relativeOffset)
  57. {
  58. ANKI_VK_SELF(CommandBufferImpl);
  59. self.setVertexAttributeInternal(location, buffBinding, fmt, relativeOffset);
  60. }
  61. void CommandBuffer::bindIndexBuffer(const BufferPtr& buff, PtrSize offset, IndexType type)
  62. {
  63. ANKI_VK_SELF(CommandBufferImpl);
  64. self.bindIndexBufferInternal(buff, offset, type);
  65. }
  66. void CommandBuffer::setPrimitiveRestart(Bool enable)
  67. {
  68. ANKI_VK_SELF(CommandBufferImpl);
  69. self.setPrimitiveRestartInternal(enable);
  70. }
  71. void CommandBuffer::setViewport(U32 minx, U32 miny, U32 width, U32 height)
  72. {
  73. ANKI_VK_SELF(CommandBufferImpl);
  74. self.setViewportInternal(minx, miny, width, height);
  75. }
  76. void CommandBuffer::setScissor(U32 minx, U32 miny, U32 width, U32 height)
  77. {
  78. ANKI_VK_SELF(CommandBufferImpl);
  79. self.setScissorInternal(minx, miny, width, height);
  80. }
  81. void CommandBuffer::setFillMode(FillMode mode)
  82. {
  83. ANKI_VK_SELF(CommandBufferImpl);
  84. self.setFillModeInternal(mode);
  85. }
  86. void CommandBuffer::setCullMode(FaceSelectionBit mode)
  87. {
  88. ANKI_VK_SELF(CommandBufferImpl);
  89. self.setCullModeInternal(mode);
  90. }
  91. void CommandBuffer::setPolygonOffset(F32 factor, F32 units)
  92. {
  93. ANKI_VK_SELF(CommandBufferImpl);
  94. self.setPolygonOffsetInternal(factor, units);
  95. }
  96. void CommandBuffer::setStencilOperations(FaceSelectionBit face, StencilOperation stencilFail, StencilOperation stencilPassDepthFail,
  97. StencilOperation stencilPassDepthPass)
  98. {
  99. ANKI_VK_SELF(CommandBufferImpl);
  100. self.setStencilOperationsInternal(face, stencilFail, stencilPassDepthFail, stencilPassDepthPass);
  101. }
  102. void CommandBuffer::setStencilCompareOperation(FaceSelectionBit face, CompareOperation comp)
  103. {
  104. ANKI_VK_SELF(CommandBufferImpl);
  105. self.setStencilCompareOperationInternal(face, comp);
  106. }
  107. void CommandBuffer::setStencilCompareMask(FaceSelectionBit face, U32 mask)
  108. {
  109. ANKI_VK_SELF(CommandBufferImpl);
  110. self.setStencilCompareMaskInternal(face, mask);
  111. }
  112. void CommandBuffer::setStencilWriteMask(FaceSelectionBit face, U32 mask)
  113. {
  114. ANKI_VK_SELF(CommandBufferImpl);
  115. self.setStencilWriteMaskInternal(face, mask);
  116. }
  117. void CommandBuffer::setStencilReference(FaceSelectionBit face, U32 ref)
  118. {
  119. ANKI_VK_SELF(CommandBufferImpl);
  120. self.setStencilReferenceInternal(face, ref);
  121. }
  122. void CommandBuffer::setDepthWrite(Bool enable)
  123. {
  124. ANKI_VK_SELF(CommandBufferImpl);
  125. self.setDepthWriteInternal(enable);
  126. }
  127. void CommandBuffer::setDepthCompareOperation(CompareOperation op)
  128. {
  129. ANKI_VK_SELF(CommandBufferImpl);
  130. self.setDepthCompareOperationInternal(op);
  131. }
  132. void CommandBuffer::setAlphaToCoverage(Bool enable)
  133. {
  134. ANKI_VK_SELF(CommandBufferImpl);
  135. self.setAlphaToCoverageInternal(enable);
  136. }
  137. void CommandBuffer::setColorChannelWriteMask(U32 attachment, ColorBit mask)
  138. {
  139. ANKI_VK_SELF(CommandBufferImpl);
  140. self.setColorChannelWriteMaskInternal(attachment, mask);
  141. }
  142. void CommandBuffer::setBlendFactors(U32 attachment, BlendFactor srcRgb, BlendFactor dstRgb, BlendFactor srcA, BlendFactor dstA)
  143. {
  144. ANKI_VK_SELF(CommandBufferImpl);
  145. self.setBlendFactorsInternal(attachment, srcRgb, dstRgb, srcA, dstA);
  146. }
  147. void CommandBuffer::setBlendOperation(U32 attachment, BlendOperation funcRgb, BlendOperation funcA)
  148. {
  149. ANKI_VK_SELF(CommandBufferImpl);
  150. self.setBlendOperationInternal(attachment, funcRgb, funcA);
  151. }
  152. void CommandBuffer::bindTextureAndSampler(U32 set, U32 binding, const TextureViewPtr& texView, const SamplerPtr& sampler, U32 arrayIdx)
  153. {
  154. ANKI_VK_SELF(CommandBufferImpl);
  155. self.bindTextureAndSamplerInternal(set, binding, texView, sampler, arrayIdx);
  156. }
  157. void CommandBuffer::bindTexture(U32 set, U32 binding, const TextureViewPtr& texView, U32 arrayIdx)
  158. {
  159. ANKI_VK_SELF(CommandBufferImpl);
  160. self.bindTextureInternal(set, binding, texView, arrayIdx);
  161. }
  162. void CommandBuffer::bindSampler(U32 set, U32 binding, const SamplerPtr& sampler, U32 arrayIdx)
  163. {
  164. ANKI_VK_SELF(CommandBufferImpl);
  165. self.bindSamplerInternal(set, binding, sampler, arrayIdx);
  166. }
  167. void CommandBuffer::bindUniformBuffer(U32 set, U32 binding, const BufferPtr& buff, PtrSize offset, PtrSize range, U32 arrayIdx)
  168. {
  169. ANKI_VK_SELF(CommandBufferImpl);
  170. self.bindUniformBufferInternal(set, binding, buff, offset, range, arrayIdx);
  171. }
  172. void CommandBuffer::bindStorageBuffer(U32 set, U32 binding, const BufferPtr& buff, PtrSize offset, PtrSize range, U32 arrayIdx)
  173. {
  174. ANKI_VK_SELF(CommandBufferImpl);
  175. self.bindStorageBufferInternal(set, binding, buff, offset, range, arrayIdx);
  176. }
  177. void CommandBuffer::bindImage(U32 set, U32 binding, const TextureViewPtr& img, U32 arrayIdx)
  178. {
  179. ANKI_VK_SELF(CommandBufferImpl);
  180. self.bindImageInternal(set, binding, img, arrayIdx);
  181. }
  182. void CommandBuffer::bindAccelerationStructure(U32 set, U32 binding, const AccelerationStructurePtr& as, U32 arrayIdx)
  183. {
  184. ANKI_VK_SELF(CommandBufferImpl);
  185. self.bindAccelerationStructureInternal(set, binding, as, arrayIdx);
  186. }
  187. void CommandBuffer::bindReadOnlyTextureBuffer(U32 set, U32 binding, const BufferPtr& buff, PtrSize offset, PtrSize range, Format fmt, U32 arrayIdx)
  188. {
  189. ANKI_VK_SELF(CommandBufferImpl);
  190. self.bindReadOnlyTextureBufferInternal(set, binding, buff, offset, range, fmt, arrayIdx);
  191. }
  192. void CommandBuffer::bindAllBindless(U32 set)
  193. {
  194. ANKI_VK_SELF(CommandBufferImpl);
  195. self.bindAllBindlessInternal(set);
  196. }
  197. void CommandBuffer::bindShaderProgram(const ShaderProgramPtr& prog)
  198. {
  199. ANKI_VK_SELF(CommandBufferImpl);
  200. self.bindShaderProgramInternal(prog);
  201. }
  202. void CommandBuffer::beginRenderPass(const FramebufferPtr& fb, const Array<TextureUsageBit, kMaxColorRenderTargets>& colorAttachmentUsages,
  203. TextureUsageBit depthStencilAttachmentUsage, U32 minx, U32 miny, U32 width, U32 height)
  204. {
  205. ANKI_VK_SELF(CommandBufferImpl);
  206. self.beginRenderPassInternal(fb, colorAttachmentUsages, depthStencilAttachmentUsage, minx, miny, width, height);
  207. }
  208. void CommandBuffer::endRenderPass()
  209. {
  210. ANKI_VK_SELF(CommandBufferImpl);
  211. self.endRenderPassInternal();
  212. }
  213. void CommandBuffer::setVrsRate(VrsRate rate)
  214. {
  215. ANKI_VK_SELF(CommandBufferImpl);
  216. self.setVrsRateInternal(rate);
  217. }
  218. void CommandBuffer::drawElements(PrimitiveTopology topology, U32 count, U32 instanceCount, U32 firstIndex, U32 baseVertex, U32 baseInstance)
  219. {
  220. ANKI_VK_SELF(CommandBufferImpl);
  221. self.drawElementsInternal(topology, count, instanceCount, firstIndex, baseVertex, baseInstance);
  222. }
  223. void CommandBuffer::drawArrays(PrimitiveTopology topology, U32 count, U32 instanceCount, U32 first, U32 baseInstance)
  224. {
  225. ANKI_VK_SELF(CommandBufferImpl);
  226. self.drawArraysInternal(topology, count, instanceCount, first, baseInstance);
  227. }
  228. void CommandBuffer::drawArraysIndirect(PrimitiveTopology topology, U32 drawCount, PtrSize offset, const BufferPtr& buff)
  229. {
  230. ANKI_VK_SELF(CommandBufferImpl);
  231. self.drawArraysIndirectInternal(topology, drawCount, offset, buff);
  232. }
  233. void CommandBuffer::drawElementsIndirect(PrimitiveTopology topology, U32 drawCount, PtrSize offset, const BufferPtr& buff)
  234. {
  235. ANKI_VK_SELF(CommandBufferImpl);
  236. self.drawElementsIndirectInternal(topology, drawCount, offset, buff);
  237. }
  238. void CommandBuffer::dispatchCompute(U32 groupCountX, U32 groupCountY, U32 groupCountZ)
  239. {
  240. ANKI_VK_SELF(CommandBufferImpl);
  241. self.dispatchComputeInternal(groupCountX, groupCountY, groupCountZ);
  242. }
  243. void CommandBuffer::traceRays(const BufferPtr& sbtBuffer, PtrSize sbtBufferOffset, U32 sbtRecordSize, U32 hitGroupSbtRecordCount, U32 rayTypeCount,
  244. U32 width, U32 height, U32 depth)
  245. {
  246. ANKI_VK_SELF(CommandBufferImpl);
  247. self.traceRaysInternal(sbtBuffer, sbtBufferOffset, sbtRecordSize, hitGroupSbtRecordCount, rayTypeCount, width, height, depth);
  248. }
  249. void CommandBuffer::generateMipmaps2d(const TextureViewPtr& texView)
  250. {
  251. ANKI_VK_SELF(CommandBufferImpl);
  252. self.generateMipmaps2dInternal(texView);
  253. }
  254. void CommandBuffer::generateMipmaps3d([[maybe_unused]] const TextureViewPtr& texView)
  255. {
  256. ANKI_ASSERT(!"TODO");
  257. }
  258. void CommandBuffer::blitTextureViews([[maybe_unused]] const TextureViewPtr& srcView, [[maybe_unused]] const TextureViewPtr& destView)
  259. {
  260. ANKI_ASSERT(!"TODO");
  261. }
  262. void CommandBuffer::clearTextureView(const TextureViewPtr& texView, const ClearValue& clearValue)
  263. {
  264. ANKI_VK_SELF(CommandBufferImpl);
  265. self.clearTextureViewInternal(texView, clearValue);
  266. }
  267. void CommandBuffer::copyBufferToTextureView(const BufferPtr& buff, PtrSize offset, PtrSize range, const TextureViewPtr& texView)
  268. {
  269. ANKI_VK_SELF(CommandBufferImpl);
  270. self.copyBufferToTextureViewInternal(buff, offset, range, texView);
  271. }
  272. void CommandBuffer::fillBuffer(const BufferPtr& buff, PtrSize offset, PtrSize size, U32 value)
  273. {
  274. ANKI_VK_SELF(CommandBufferImpl);
  275. self.fillBufferInternal(buff, offset, size, value);
  276. }
  277. void CommandBuffer::writeOcclusionQueriesResultToBuffer(ConstWeakArray<OcclusionQuery*> queries, PtrSize offset, const BufferPtr& buff)
  278. {
  279. ANKI_VK_SELF(CommandBufferImpl);
  280. self.writeOcclusionQueriesResultToBufferInternal(queries, offset, buff);
  281. }
  282. void CommandBuffer::copyBufferToBuffer(const BufferPtr& src, const BufferPtr& dst, ConstWeakArray<CopyBufferToBufferInfo> copies)
  283. {
  284. ANKI_VK_SELF(CommandBufferImpl);
  285. self.copyBufferToBufferInternal(src, dst, copies);
  286. }
  287. void CommandBuffer::buildAccelerationStructure(const AccelerationStructurePtr& as)
  288. {
  289. ANKI_VK_SELF(CommandBufferImpl);
  290. self.buildAccelerationStructureInternal(as);
  291. }
  292. void CommandBuffer::upscale(const GrUpscalerPtr& upscaler, const TextureViewPtr& inColor, const TextureViewPtr& outUpscaledColor,
  293. const TextureViewPtr& motionVectors, const TextureViewPtr& depth, const TextureViewPtr& exposure,
  294. const Bool resetAccumulation, const Vec2& jitterOffset, const Vec2& motionVectorsScale)
  295. {
  296. ANKI_VK_SELF(CommandBufferImpl);
  297. self.upscaleInternal(upscaler, inColor, outUpscaledColor, motionVectors, depth, exposure, resetAccumulation, jitterOffset, motionVectorsScale);
  298. }
  299. void CommandBuffer::setPipelineBarrier(ConstWeakArray<TextureBarrierInfo> textures, ConstWeakArray<BufferBarrierInfo> buffers,
  300. ConstWeakArray<AccelerationStructureBarrierInfo> accelerationStructures)
  301. {
  302. ANKI_VK_SELF(CommandBufferImpl);
  303. self.setPipelineBarrierInternal(textures, buffers, accelerationStructures);
  304. }
  305. void CommandBuffer::resetOcclusionQueries(ConstWeakArray<OcclusionQuery*> queries)
  306. {
  307. ANKI_VK_SELF(CommandBufferImpl);
  308. self.resetOcclusionQueriesInternal(queries);
  309. }
  310. void CommandBuffer::beginOcclusionQuery(const OcclusionQueryPtr& query)
  311. {
  312. ANKI_VK_SELF(CommandBufferImpl);
  313. self.beginOcclusionQueryInternal(query);
  314. }
  315. void CommandBuffer::endOcclusionQuery(const OcclusionQueryPtr& query)
  316. {
  317. ANKI_VK_SELF(CommandBufferImpl);
  318. self.endOcclusionQueryInternal(query);
  319. }
  320. void CommandBuffer::pushSecondLevelCommandBuffers(ConstWeakArray<CommandBuffer*> cmdbs)
  321. {
  322. ANKI_VK_SELF(CommandBufferImpl);
  323. self.pushSecondLevelCommandBuffersInternal(cmdbs);
  324. }
  325. void CommandBuffer::resetTimestampQueries(ConstWeakArray<TimestampQuery*> queries)
  326. {
  327. ANKI_VK_SELF(CommandBufferImpl);
  328. self.resetTimestampQueriesInternal(queries);
  329. }
  330. void CommandBuffer::writeTimestamp(const TimestampQueryPtr& query)
  331. {
  332. ANKI_VK_SELF(CommandBufferImpl);
  333. self.writeTimestampInternal(query);
  334. }
  335. Bool CommandBuffer::isEmpty() const
  336. {
  337. ANKI_VK_SELF_CONST(CommandBufferImpl);
  338. return self.isEmpty();
  339. }
  340. void CommandBuffer::setPushConstants(const void* data, U32 dataSize)
  341. {
  342. ANKI_VK_SELF(CommandBufferImpl);
  343. self.setPushConstantsInternal(data, dataSize);
  344. }
  345. void CommandBuffer::setRasterizationOrder(RasterizationOrder order)
  346. {
  347. ANKI_VK_SELF(CommandBufferImpl);
  348. self.setRasterizationOrderInternal(order);
  349. }
  350. void CommandBuffer::setLineWidth(F32 width)
  351. {
  352. ANKI_VK_SELF(CommandBufferImpl);
  353. self.setLineWidthInternal(width);
  354. }
  355. } // end namespace anki