CommandBuffer.cpp 12 KB

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