CommandBuffer.cpp 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459
  1. // Copyright (C) 2009-2022, 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(GrManager* manager, const CommandBufferInitInfo& init)
  12. {
  13. ANKI_TRACE_SCOPED_EVENT(VK_NEW_CCOMMAND_BUFFER);
  14. CommandBufferImpl* impl = manager->getAllocator().newInstance<CommandBufferImpl>(manager, init.getName());
  15. const Error err = impl->init(init);
  16. if(err)
  17. {
  18. manager->getAllocator().deleteInstance(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. self.getGrManagerImpl().flushCommandBuffer(
  36. self.getMicroCommandBuffer(), self.renderedToDefaultFramebuffer(),
  37. WeakArray<MicroSemaphorePtr>(waitSemaphores.getBegin(), waitFences.getSize()),
  38. (signalFence) ? &signalSemaphore : nullptr);
  39. if(signalFence)
  40. {
  41. FenceImpl* fenceImpl =
  42. self.getGrManagerImpl().getAllocator().newInstance<FenceImpl>(&getManager(), "SignalFence");
  43. fenceImpl->m_semaphore = signalSemaphore;
  44. signalFence->reset(fenceImpl);
  45. }
  46. }
  47. else
  48. {
  49. ANKI_ASSERT(signalFence == nullptr);
  50. ANKI_ASSERT(waitFences.getSize() == 0);
  51. }
  52. }
  53. void CommandBuffer::bindVertexBuffer(U32 binding, const BufferPtr& buff, PtrSize offset, PtrSize stride,
  54. VertexStepRate stepRate)
  55. {
  56. ANKI_VK_SELF(CommandBufferImpl);
  57. self.bindVertexBufferInternal(binding, buff, offset, stride, stepRate);
  58. }
  59. void CommandBuffer::setVertexAttribute(U32 location, U32 buffBinding, Format fmt, PtrSize relativeOffset)
  60. {
  61. ANKI_VK_SELF(CommandBufferImpl);
  62. self.setVertexAttributeInternal(location, buffBinding, fmt, relativeOffset);
  63. }
  64. void CommandBuffer::bindIndexBuffer(const BufferPtr& buff, PtrSize offset, IndexType type)
  65. {
  66. ANKI_VK_SELF(CommandBufferImpl);
  67. self.bindIndexBufferInternal(buff, offset, type);
  68. }
  69. void CommandBuffer::setPrimitiveRestart(Bool enable)
  70. {
  71. ANKI_VK_SELF(CommandBufferImpl);
  72. self.setPrimitiveRestartInternal(enable);
  73. }
  74. void CommandBuffer::setViewport(U32 minx, U32 miny, U32 width, U32 height)
  75. {
  76. ANKI_VK_SELF(CommandBufferImpl);
  77. self.setViewportInternal(minx, miny, width, height);
  78. }
  79. void CommandBuffer::setScissor(U32 minx, U32 miny, U32 width, U32 height)
  80. {
  81. ANKI_VK_SELF(CommandBufferImpl);
  82. self.setScissorInternal(minx, miny, width, height);
  83. }
  84. void CommandBuffer::setFillMode(FillMode mode)
  85. {
  86. ANKI_VK_SELF(CommandBufferImpl);
  87. self.setFillModeInternal(mode);
  88. }
  89. void CommandBuffer::setCullMode(FaceSelectionBit mode)
  90. {
  91. ANKI_VK_SELF(CommandBufferImpl);
  92. self.setCullModeInternal(mode);
  93. }
  94. void CommandBuffer::setPolygonOffset(F32 factor, F32 units)
  95. {
  96. ANKI_VK_SELF(CommandBufferImpl);
  97. self.setPolygonOffsetInternal(factor, units);
  98. }
  99. void CommandBuffer::setStencilOperations(FaceSelectionBit face, StencilOperation stencilFail,
  100. StencilOperation stencilPassDepthFail, StencilOperation stencilPassDepthPass)
  101. {
  102. ANKI_VK_SELF(CommandBufferImpl);
  103. self.setStencilOperationsInternal(face, stencilFail, stencilPassDepthFail, stencilPassDepthPass);
  104. }
  105. void CommandBuffer::setStencilCompareOperation(FaceSelectionBit face, CompareOperation comp)
  106. {
  107. ANKI_VK_SELF(CommandBufferImpl);
  108. self.setStencilCompareOperationInternal(face, comp);
  109. }
  110. void CommandBuffer::setStencilCompareMask(FaceSelectionBit face, U32 mask)
  111. {
  112. ANKI_VK_SELF(CommandBufferImpl);
  113. self.setStencilCompareMaskInternal(face, mask);
  114. }
  115. void CommandBuffer::setStencilWriteMask(FaceSelectionBit face, U32 mask)
  116. {
  117. ANKI_VK_SELF(CommandBufferImpl);
  118. self.setStencilWriteMaskInternal(face, mask);
  119. }
  120. void CommandBuffer::setStencilReference(FaceSelectionBit face, U32 ref)
  121. {
  122. ANKI_VK_SELF(CommandBufferImpl);
  123. self.setStencilReferenceInternal(face, ref);
  124. }
  125. void CommandBuffer::setDepthWrite(Bool enable)
  126. {
  127. ANKI_VK_SELF(CommandBufferImpl);
  128. self.setDepthWriteInternal(enable);
  129. }
  130. void CommandBuffer::setDepthCompareOperation(CompareOperation op)
  131. {
  132. ANKI_VK_SELF(CommandBufferImpl);
  133. self.setDepthCompareOperationInternal(op);
  134. }
  135. void CommandBuffer::setAlphaToCoverage(Bool enable)
  136. {
  137. ANKI_VK_SELF(CommandBufferImpl);
  138. self.setAlphaToCoverageInternal(enable);
  139. }
  140. void CommandBuffer::setColorChannelWriteMask(U32 attachment, ColorBit mask)
  141. {
  142. ANKI_VK_SELF(CommandBufferImpl);
  143. self.setColorChannelWriteMaskInternal(attachment, mask);
  144. }
  145. void CommandBuffer::setBlendFactors(U32 attachment, BlendFactor srcRgb, BlendFactor dstRgb, BlendFactor srcA,
  146. BlendFactor dstA)
  147. {
  148. ANKI_VK_SELF(CommandBufferImpl);
  149. self.setBlendFactorsInternal(attachment, srcRgb, dstRgb, srcA, dstA);
  150. }
  151. void CommandBuffer::setBlendOperation(U32 attachment, BlendOperation funcRgb, BlendOperation funcA)
  152. {
  153. ANKI_VK_SELF(CommandBufferImpl);
  154. self.setBlendOperationInternal(attachment, funcRgb, funcA);
  155. }
  156. void CommandBuffer::bindTextureAndSampler(U32 set, U32 binding, const TextureViewPtr& texView,
  157. const SamplerPtr& sampler, U32 arrayIdx)
  158. {
  159. ANKI_VK_SELF(CommandBufferImpl);
  160. self.bindTextureAndSamplerInternal(set, binding, texView, sampler, arrayIdx);
  161. }
  162. void CommandBuffer::bindTexture(U32 set, U32 binding, const TextureViewPtr& texView, U32 arrayIdx)
  163. {
  164. ANKI_VK_SELF(CommandBufferImpl);
  165. self.bindTextureInternal(set, binding, texView, arrayIdx);
  166. }
  167. void CommandBuffer::bindSampler(U32 set, U32 binding, const SamplerPtr& sampler, U32 arrayIdx)
  168. {
  169. ANKI_VK_SELF(CommandBufferImpl);
  170. self.bindSamplerInternal(set, binding, sampler, arrayIdx);
  171. }
  172. void CommandBuffer::bindUniformBuffer(U32 set, U32 binding, const BufferPtr& buff, PtrSize offset, PtrSize range,
  173. U32 arrayIdx)
  174. {
  175. ANKI_VK_SELF(CommandBufferImpl);
  176. self.bindUniformBufferInternal(set, binding, buff, offset, range, arrayIdx);
  177. }
  178. void CommandBuffer::bindStorageBuffer(U32 set, U32 binding, const BufferPtr& buff, PtrSize offset, PtrSize range,
  179. U32 arrayIdx)
  180. {
  181. ANKI_VK_SELF(CommandBufferImpl);
  182. self.bindStorageBufferInternal(set, binding, buff, offset, range, arrayIdx);
  183. }
  184. void CommandBuffer::bindImage(U32 set, U32 binding, const TextureViewPtr& img, U32 arrayIdx)
  185. {
  186. ANKI_VK_SELF(CommandBufferImpl);
  187. self.bindImageInternal(set, binding, img, arrayIdx);
  188. }
  189. void CommandBuffer::bindAccelerationStructure(U32 set, U32 binding, const AccelerationStructurePtr& as, U32 arrayIdx)
  190. {
  191. ANKI_VK_SELF(CommandBufferImpl);
  192. self.bindAccelerationStructureInternal(set, binding, as, arrayIdx);
  193. }
  194. void CommandBuffer::bindTextureBuffer(U32 set, U32 binding, const BufferPtr& buff, PtrSize offset, PtrSize range,
  195. Format fmt, U32 arrayIdx)
  196. {
  197. ANKI_ASSERT(!"TODO");
  198. }
  199. void CommandBuffer::bindAllBindless(U32 set)
  200. {
  201. ANKI_VK_SELF(CommandBufferImpl);
  202. self.bindAllBindlessInternal(set);
  203. }
  204. void CommandBuffer::bindShaderProgram(const ShaderProgramPtr& prog)
  205. {
  206. ANKI_VK_SELF(CommandBufferImpl);
  207. self.bindShaderProgramInternal(prog);
  208. }
  209. void CommandBuffer::beginRenderPass(const FramebufferPtr& fb,
  210. const Array<TextureUsageBit, MAX_COLOR_ATTACHMENTS>& colorAttachmentUsages,
  211. TextureUsageBit depthStencilAttachmentUsage, U32 minx, U32 miny, U32 width,
  212. U32 height)
  213. {
  214. ANKI_VK_SELF(CommandBufferImpl);
  215. self.beginRenderPassInternal(fb, colorAttachmentUsages, depthStencilAttachmentUsage, minx, miny, width, height);
  216. }
  217. void CommandBuffer::endRenderPass()
  218. {
  219. ANKI_VK_SELF(CommandBufferImpl);
  220. self.endRenderPassInternal();
  221. }
  222. void CommandBuffer::setVrsRate(VrsRate rate)
  223. {
  224. ANKI_VK_SELF(CommandBufferImpl);
  225. self.setVrsRateInternal(rate);
  226. }
  227. void CommandBuffer::drawElements(PrimitiveTopology topology, U32 count, U32 instanceCount, U32 firstIndex,
  228. U32 baseVertex, U32 baseInstance)
  229. {
  230. ANKI_VK_SELF(CommandBufferImpl);
  231. self.drawElementsInternal(topology, count, instanceCount, firstIndex, baseVertex, baseInstance);
  232. }
  233. void CommandBuffer::drawArrays(PrimitiveTopology topology, U32 count, U32 instanceCount, U32 first, U32 baseInstance)
  234. {
  235. ANKI_VK_SELF(CommandBufferImpl);
  236. self.drawArraysInternal(topology, count, instanceCount, first, baseInstance);
  237. }
  238. void CommandBuffer::drawArraysIndirect(PrimitiveTopology topology, U32 drawCount, PtrSize offset, const BufferPtr& buff)
  239. {
  240. ANKI_VK_SELF(CommandBufferImpl);
  241. self.drawArraysIndirectInternal(topology, drawCount, offset, buff);
  242. }
  243. void CommandBuffer::drawElementsIndirect(PrimitiveTopology topology, U32 drawCount, PtrSize offset,
  244. const BufferPtr& buff)
  245. {
  246. ANKI_VK_SELF(CommandBufferImpl);
  247. self.drawElementsIndirectInternal(topology, drawCount, offset, buff);
  248. }
  249. void CommandBuffer::dispatchCompute(U32 groupCountX, U32 groupCountY, U32 groupCountZ)
  250. {
  251. ANKI_VK_SELF(CommandBufferImpl);
  252. self.dispatchComputeInternal(groupCountX, groupCountY, groupCountZ);
  253. }
  254. void CommandBuffer::traceRays(const BufferPtr& sbtBuffer, PtrSize sbtBufferOffset, U32 sbtRecordSize,
  255. U32 hitGroupSbtRecordCount, U32 rayTypeCount, U32 width, U32 height, U32 depth)
  256. {
  257. ANKI_VK_SELF(CommandBufferImpl);
  258. self.traceRaysInternal(sbtBuffer, sbtBufferOffset, sbtRecordSize, hitGroupSbtRecordCount, rayTypeCount, width,
  259. height, depth);
  260. }
  261. void CommandBuffer::generateMipmaps2d(const TextureViewPtr& texView)
  262. {
  263. ANKI_VK_SELF(CommandBufferImpl);
  264. self.generateMipmaps2dInternal(texView);
  265. }
  266. void CommandBuffer::generateMipmaps3d(const TextureViewPtr& texView)
  267. {
  268. ANKI_ASSERT(!"TODO");
  269. }
  270. void CommandBuffer::blitTextureViews(const TextureViewPtr& srcView, const TextureViewPtr& destView)
  271. {
  272. ANKI_ASSERT(!"TODO");
  273. }
  274. void CommandBuffer::clearTextureView(const TextureViewPtr& texView, const ClearValue& clearValue)
  275. {
  276. ANKI_VK_SELF(CommandBufferImpl);
  277. self.clearTextureViewInternal(texView, clearValue);
  278. }
  279. void CommandBuffer::copyBufferToTextureView(const BufferPtr& buff, PtrSize offset, PtrSize range,
  280. const TextureViewPtr& texView)
  281. {
  282. ANKI_VK_SELF(CommandBufferImpl);
  283. self.copyBufferToTextureViewInternal(buff, offset, range, texView);
  284. }
  285. void CommandBuffer::fillBuffer(const BufferPtr& buff, PtrSize offset, PtrSize size, U32 value)
  286. {
  287. ANKI_VK_SELF(CommandBufferImpl);
  288. self.fillBufferInternal(buff, offset, size, value);
  289. }
  290. void CommandBuffer::writeOcclusionQueryResultToBuffer(const OcclusionQueryPtr& query, PtrSize offset,
  291. const BufferPtr& buff)
  292. {
  293. ANKI_VK_SELF(CommandBufferImpl);
  294. self.writeOcclusionQueryResultToBufferInternal(query, offset, buff);
  295. }
  296. void CommandBuffer::copyBufferToBuffer(const BufferPtr& src, PtrSize srcOffset, const BufferPtr& dst, PtrSize dstOffset,
  297. PtrSize range)
  298. {
  299. ANKI_VK_SELF(CommandBufferImpl);
  300. self.copyBufferToBufferInternal(src, srcOffset, dst, dstOffset, range);
  301. }
  302. void CommandBuffer::buildAccelerationStructure(const AccelerationStructurePtr& as)
  303. {
  304. ANKI_VK_SELF(CommandBufferImpl);
  305. self.buildAccelerationStructureInternal(as);
  306. }
  307. void CommandBuffer::setTextureBarrier(const TexturePtr& tex, TextureUsageBit prevUsage, TextureUsageBit nextUsage,
  308. const TextureSubresourceInfo& subresource)
  309. {
  310. ANKI_VK_SELF(CommandBufferImpl);
  311. self.setTextureBarrierInternal(tex, prevUsage, nextUsage, subresource);
  312. }
  313. void CommandBuffer::setTextureSurfaceBarrier(const TexturePtr& tex, TextureUsageBit prevUsage,
  314. TextureUsageBit nextUsage, const TextureSurfaceInfo& surf)
  315. {
  316. ANKI_VK_SELF(CommandBufferImpl);
  317. self.setTextureSurfaceBarrierInternal(tex, prevUsage, nextUsage, surf);
  318. }
  319. void CommandBuffer::setTextureVolumeBarrier(const TexturePtr& tex, TextureUsageBit prevUsage, TextureUsageBit nextUsage,
  320. const TextureVolumeInfo& vol)
  321. {
  322. ANKI_VK_SELF(CommandBufferImpl);
  323. self.setTextureVolumeBarrierInternal(tex, prevUsage, nextUsage, vol);
  324. }
  325. void CommandBuffer::setBufferBarrier(const BufferPtr& buff, BufferUsageBit before, BufferUsageBit after, PtrSize offset,
  326. PtrSize size)
  327. {
  328. ANKI_VK_SELF(CommandBufferImpl);
  329. self.setBufferBarrierInternal(buff, before, after, offset, size);
  330. }
  331. void CommandBuffer::setAccelerationStructureBarrier(const AccelerationStructurePtr& as,
  332. AccelerationStructureUsageBit prevUsage,
  333. AccelerationStructureUsageBit nextUsage)
  334. {
  335. ANKI_VK_SELF(CommandBufferImpl);
  336. self.setAccelerationStructureBarrierInternal(as, prevUsage, nextUsage);
  337. }
  338. void CommandBuffer::resetOcclusionQuery(const OcclusionQueryPtr& query)
  339. {
  340. ANKI_VK_SELF(CommandBufferImpl);
  341. self.resetOcclusionQueryInternal(query);
  342. }
  343. void CommandBuffer::beginOcclusionQuery(const OcclusionQueryPtr& query)
  344. {
  345. ANKI_VK_SELF(CommandBufferImpl);
  346. self.beginOcclusionQueryInternal(query);
  347. }
  348. void CommandBuffer::endOcclusionQuery(const OcclusionQueryPtr& query)
  349. {
  350. ANKI_VK_SELF(CommandBufferImpl);
  351. self.endOcclusionQueryInternal(query);
  352. }
  353. void CommandBuffer::pushSecondLevelCommandBuffer(const CommandBufferPtr& cmdb)
  354. {
  355. ANKI_VK_SELF(CommandBufferImpl);
  356. self.pushSecondLevelCommandBufferInternal(cmdb);
  357. }
  358. void CommandBuffer::resetTimestampQuery(const TimestampQueryPtr& query)
  359. {
  360. ANKI_VK_SELF(CommandBufferImpl);
  361. self.resetTimestampQueryInternal(query);
  362. }
  363. void CommandBuffer::writeTimestamp(const TimestampQueryPtr& query)
  364. {
  365. ANKI_VK_SELF(CommandBufferImpl);
  366. self.writeTimestampInternal(query);
  367. }
  368. Bool CommandBuffer::isEmpty() const
  369. {
  370. ANKI_VK_SELF_CONST(CommandBufferImpl);
  371. return self.isEmpty();
  372. }
  373. void CommandBuffer::setPushConstants(const void* data, U32 dataSize)
  374. {
  375. ANKI_VK_SELF(CommandBufferImpl);
  376. self.setPushConstantsInternal(data, dataSize);
  377. }
  378. void CommandBuffer::setRasterizationOrder(RasterizationOrder order)
  379. {
  380. ANKI_VK_SELF(CommandBufferImpl);
  381. self.setRasterizationOrderInternal(order);
  382. }
  383. void CommandBuffer::setLineWidth(F32 width)
  384. {
  385. ANKI_VK_SELF(CommandBufferImpl);
  386. self.setLineWidthInternal(width);
  387. }
  388. } // end namespace anki