CommandBuffer.cpp 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429
  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(
  36. U32 binding, BufferPtr buff, PtrSize offset, PtrSize stride, 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,
  82. StencilOperation stencilFail,
  83. StencilOperation stencilPassDepthFail,
  84. StencilOperation stencilPassDepthPass)
  85. {
  86. ANKI_VK_SELF(CommandBufferImpl);
  87. self.setStencilOperations(face, stencilFail, stencilPassDepthFail, stencilPassDepthPass);
  88. }
  89. void CommandBuffer::setStencilCompareOperation(FaceSelectionBit face, CompareOperation comp)
  90. {
  91. ANKI_VK_SELF(CommandBufferImpl);
  92. self.setStencilCompareOperation(face, comp);
  93. }
  94. void CommandBuffer::setStencilCompareMask(FaceSelectionBit face, U32 mask)
  95. {
  96. ANKI_VK_SELF(CommandBufferImpl);
  97. self.setStencilCompareMask(face, mask);
  98. }
  99. void CommandBuffer::setStencilWriteMask(FaceSelectionBit face, U32 mask)
  100. {
  101. ANKI_VK_SELF(CommandBufferImpl);
  102. self.setStencilWriteMask(face, mask);
  103. }
  104. void CommandBuffer::setStencilReference(FaceSelectionBit face, U32 ref)
  105. {
  106. ANKI_VK_SELF(CommandBufferImpl);
  107. self.setStencilReference(face, ref);
  108. }
  109. void CommandBuffer::setDepthWrite(Bool enable)
  110. {
  111. ANKI_VK_SELF(CommandBufferImpl);
  112. self.setDepthWrite(enable);
  113. }
  114. void CommandBuffer::setDepthCompareOperation(CompareOperation op)
  115. {
  116. ANKI_VK_SELF(CommandBufferImpl);
  117. self.setDepthCompareOperation(op);
  118. }
  119. void CommandBuffer::setAlphaToCoverage(Bool enable)
  120. {
  121. ANKI_VK_SELF(CommandBufferImpl);
  122. self.setAlphaToCoverage(enable);
  123. }
  124. void CommandBuffer::setColorChannelWriteMask(U32 attachment, ColorBit mask)
  125. {
  126. ANKI_VK_SELF(CommandBufferImpl);
  127. self.setColorChannelWriteMask(attachment, mask);
  128. }
  129. void CommandBuffer::setBlendFactors(
  130. U32 attachment, BlendFactor srcRgb, BlendFactor dstRgb, BlendFactor srcA, BlendFactor dstA)
  131. {
  132. ANKI_VK_SELF(CommandBufferImpl);
  133. self.setBlendFactors(attachment, srcRgb, dstRgb, srcA, dstA);
  134. }
  135. void CommandBuffer::setBlendOperation(U32 attachment, BlendOperation funcRgb, BlendOperation funcA)
  136. {
  137. ANKI_VK_SELF(CommandBufferImpl);
  138. self.setBlendOperation(attachment, funcRgb, funcA);
  139. }
  140. void CommandBuffer::bindTextureAndSampler(
  141. U32 set, U32 binding, TextureViewPtr texView, SamplerPtr sampler, TextureUsageBit usage, U32 arrayIdx)
  142. {
  143. ANKI_VK_SELF(CommandBufferImpl);
  144. self.bindTextureAndSamplerInternal(set, binding, texView, sampler, usage, arrayIdx);
  145. }
  146. void CommandBuffer::bindTexture(U32 set, U32 binding, TextureViewPtr texView, TextureUsageBit usage, U32 arrayIdx)
  147. {
  148. ANKI_VK_SELF(CommandBufferImpl);
  149. self.bindTextureInternal(set, binding, texView, usage, arrayIdx);
  150. }
  151. void CommandBuffer::bindSampler(U32 set, U32 binding, SamplerPtr sampler, U32 arrayIdx)
  152. {
  153. ANKI_VK_SELF(CommandBufferImpl);
  154. self.bindSamplerInternal(set, binding, sampler, arrayIdx);
  155. }
  156. void CommandBuffer::bindUniformBuffer(U32 set, U32 binding, BufferPtr buff, PtrSize offset, PtrSize range, U32 arrayIdx)
  157. {
  158. ANKI_VK_SELF(CommandBufferImpl);
  159. self.bindUniformBufferInternal(set, binding, buff, offset, range, arrayIdx);
  160. }
  161. void CommandBuffer::bindStorageBuffer(U32 set, U32 binding, BufferPtr buff, PtrSize offset, PtrSize range, U32 arrayIdx)
  162. {
  163. ANKI_VK_SELF(CommandBufferImpl);
  164. self.bindStorageBufferInternal(set, binding, buff, offset, range, arrayIdx);
  165. }
  166. void CommandBuffer::bindImage(U32 set, U32 binding, TextureViewPtr img, U32 arrayIdx)
  167. {
  168. ANKI_VK_SELF(CommandBufferImpl);
  169. self.bindImageInternal(set, binding, img, arrayIdx);
  170. }
  171. void CommandBuffer::bindAccelerationStructure(U32 set, U32 binding, AccelerationStructurePtr as, U32 arrayIdx)
  172. {
  173. ANKI_VK_SELF(CommandBufferImpl);
  174. self.bindAccelerationStructureInternal(set, binding, as, arrayIdx);
  175. }
  176. void CommandBuffer::bindTextureBuffer(
  177. U32 set, U32 binding, BufferPtr buff, PtrSize offset, PtrSize range, Format fmt, U32 arrayIdx)
  178. {
  179. ANKI_ASSERT(!"TODO");
  180. }
  181. void CommandBuffer::bindAllBindless(U32 set)
  182. {
  183. ANKI_VK_SELF(CommandBufferImpl);
  184. self.bindAllBindlessInternal(set);
  185. }
  186. U32 CommandBuffer::bindBindlessTexture(TextureViewPtr tex, TextureUsageBit usage)
  187. {
  188. ANKI_VK_SELF(CommandBufferImpl);
  189. return self.bindBindlessTextureInternal(tex, usage);
  190. }
  191. U32 CommandBuffer::bindBindlessImage(TextureViewPtr img)
  192. {
  193. ANKI_VK_SELF(CommandBufferImpl);
  194. return self.bindBindlessImageInternal(img);
  195. }
  196. void CommandBuffer::bindShaderProgram(ShaderProgramPtr prog)
  197. {
  198. ANKI_VK_SELF(CommandBufferImpl);
  199. self.bindShaderProgram(prog);
  200. }
  201. void CommandBuffer::beginRenderPass(FramebufferPtr fb,
  202. const Array<TextureUsageBit, MAX_COLOR_ATTACHMENTS>& colorAttachmentUsages,
  203. TextureUsageBit depthStencilAttachmentUsage,
  204. U32 minx,
  205. U32 miny,
  206. U32 width,
  207. U32 height)
  208. {
  209. ANKI_VK_SELF(CommandBufferImpl);
  210. self.beginRenderPass(fb, colorAttachmentUsages, depthStencilAttachmentUsage, minx, miny, width, height);
  211. }
  212. void CommandBuffer::endRenderPass()
  213. {
  214. ANKI_VK_SELF(CommandBufferImpl);
  215. self.endRenderPass();
  216. }
  217. void CommandBuffer::drawElements(
  218. PrimitiveTopology topology, U32 count, U32 instanceCount, U32 firstIndex, U32 baseVertex, U32 baseInstance)
  219. {
  220. ANKI_VK_SELF(CommandBufferImpl);
  221. self.drawElements(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.drawArrays(topology, count, instanceCount, first, baseInstance);
  227. }
  228. void CommandBuffer::drawArraysIndirect(PrimitiveTopology topology, U32 drawCount, PtrSize offset, BufferPtr buff)
  229. {
  230. ANKI_VK_SELF(CommandBufferImpl);
  231. self.drawArraysIndirect(topology, drawCount, offset, buff);
  232. }
  233. void CommandBuffer::drawElementsIndirect(PrimitiveTopology topology, U32 drawCount, PtrSize offset, BufferPtr buff)
  234. {
  235. ANKI_VK_SELF(CommandBufferImpl);
  236. self.drawElementsIndirect(topology, drawCount, offset, buff);
  237. }
  238. void CommandBuffer::dispatchCompute(U32 groupCountX, U32 groupCountY, U32 groupCountZ)
  239. {
  240. ANKI_VK_SELF(CommandBufferImpl);
  241. self.dispatchCompute(groupCountX, groupCountY, groupCountZ);
  242. }
  243. void CommandBuffer::generateMipmaps2d(TextureViewPtr texView)
  244. {
  245. ANKI_VK_SELF(CommandBufferImpl);
  246. self.generateMipmaps2d(texView);
  247. }
  248. void CommandBuffer::generateMipmaps3d(TextureViewPtr texView)
  249. {
  250. ANKI_ASSERT(!"TODO");
  251. }
  252. void CommandBuffer::blitTextureViews(TextureViewPtr srcView, TextureViewPtr destView)
  253. {
  254. ANKI_ASSERT(!"TODO");
  255. }
  256. void CommandBuffer::clearTextureView(TextureViewPtr texView, const ClearValue& clearValue)
  257. {
  258. ANKI_VK_SELF(CommandBufferImpl);
  259. self.clearTextureView(texView, clearValue);
  260. }
  261. void CommandBuffer::copyBufferToTextureView(BufferPtr buff, PtrSize offset, PtrSize range, TextureViewPtr texView)
  262. {
  263. ANKI_VK_SELF(CommandBufferImpl);
  264. self.copyBufferToTextureViewInternal(buff, offset, range, texView);
  265. }
  266. void CommandBuffer::fillBuffer(BufferPtr buff, PtrSize offset, PtrSize size, U32 value)
  267. {
  268. ANKI_VK_SELF(CommandBufferImpl);
  269. self.fillBuffer(buff, offset, size, value);
  270. }
  271. void CommandBuffer::writeOcclusionQueryResultToBuffer(OcclusionQueryPtr query, PtrSize offset, BufferPtr buff)
  272. {
  273. ANKI_VK_SELF(CommandBufferImpl);
  274. self.writeOcclusionQueryResultToBuffer(query, offset, buff);
  275. }
  276. void CommandBuffer::copyBufferToBuffer(
  277. BufferPtr src, PtrSize srcOffset, BufferPtr dst, PtrSize dstOffset, PtrSize range)
  278. {
  279. ANKI_VK_SELF(CommandBufferImpl);
  280. self.copyBufferToBuffer(src, srcOffset, dst, dstOffset, range);
  281. }
  282. void CommandBuffer::buildAccelerationStructure(AccelerationStructurePtr as)
  283. {
  284. ANKI_VK_SELF(CommandBufferImpl);
  285. self.buildAccelerationStructureInternal(as);
  286. }
  287. void CommandBuffer::setTextureBarrier(
  288. TexturePtr tex, TextureUsageBit prevUsage, TextureUsageBit nextUsage, const TextureSubresourceInfo& subresource)
  289. {
  290. ANKI_VK_SELF(CommandBufferImpl);
  291. self.setTextureBarrier(tex, prevUsage, nextUsage, subresource);
  292. }
  293. void CommandBuffer::setTextureSurfaceBarrier(
  294. TexturePtr tex, TextureUsageBit prevUsage, TextureUsageBit nextUsage, const TextureSurfaceInfo& surf)
  295. {
  296. ANKI_VK_SELF(CommandBufferImpl);
  297. self.setTextureSurfaceBarrier(tex, prevUsage, nextUsage, surf);
  298. }
  299. void CommandBuffer::setTextureVolumeBarrier(
  300. TexturePtr tex, TextureUsageBit prevUsage, TextureUsageBit nextUsage, const TextureVolumeInfo& vol)
  301. {
  302. ANKI_VK_SELF(CommandBufferImpl);
  303. self.setTextureVolumeBarrier(tex, prevUsage, nextUsage, vol);
  304. }
  305. void CommandBuffer::setBufferBarrier(
  306. BufferPtr buff, BufferUsageBit before, BufferUsageBit after, PtrSize offset, PtrSize size)
  307. {
  308. ANKI_VK_SELF(CommandBufferImpl);
  309. self.setBufferBarrier(buff, before, after, offset, size);
  310. }
  311. void CommandBuffer::resetOcclusionQuery(OcclusionQueryPtr query)
  312. {
  313. ANKI_VK_SELF(CommandBufferImpl);
  314. self.resetOcclusionQuery(query);
  315. }
  316. void CommandBuffer::beginOcclusionQuery(OcclusionQueryPtr query)
  317. {
  318. ANKI_VK_SELF(CommandBufferImpl);
  319. self.beginOcclusionQuery(query);
  320. }
  321. void CommandBuffer::endOcclusionQuery(OcclusionQueryPtr query)
  322. {
  323. ANKI_VK_SELF(CommandBufferImpl);
  324. self.endOcclusionQuery(query);
  325. }
  326. void CommandBuffer::pushSecondLevelCommandBuffer(CommandBufferPtr cmdb)
  327. {
  328. ANKI_VK_SELF(CommandBufferImpl);
  329. self.pushSecondLevelCommandBuffer(cmdb);
  330. }
  331. void CommandBuffer::resetTimestampQuery(TimestampQueryPtr query)
  332. {
  333. ANKI_VK_SELF(CommandBufferImpl);
  334. self.resetTimestampQueryInternal(query);
  335. }
  336. void CommandBuffer::writeTimestamp(TimestampQueryPtr query)
  337. {
  338. ANKI_VK_SELF(CommandBufferImpl);
  339. self.writeTimestampInternal(query);
  340. }
  341. Bool CommandBuffer::isEmpty() const
  342. {
  343. ANKI_VK_SELF_CONST(CommandBufferImpl);
  344. return self.isEmpty();
  345. }
  346. void CommandBuffer::setPushConstants(const void* data, U32 dataSize)
  347. {
  348. ANKI_VK_SELF(CommandBufferImpl);
  349. self.setPushConstants(data, dataSize);
  350. }
  351. void CommandBuffer::setRasterizationOrder(RasterizationOrder order)
  352. {
  353. ANKI_VK_SELF(CommandBufferImpl);
  354. self.setRasterizationOrder(order);
  355. }
  356. void CommandBuffer::setLineWidth(F32 width)
  357. {
  358. ANKI_VK_SELF(CommandBufferImpl);
  359. self.setLineWidth(width);
  360. }
  361. } // end namespace anki