/* * Copyright 2021 Google LLC * * Use of this source code is governed by a BSD-style license that can be * found in the LICENSE file. */ #ifndef skgpu_MtlCommandBuffer_DEFINED #define skgpu_MtlCommandBuffer_DEFINED #include "experimental/graphite/src/CommandBuffer.h" #include "experimental/graphite/src/GpuWorkSubmission.h" #include #include "include/core/SkTypes.h" #include "include/ports/SkCFObject.h" #import namespace skgpu::mtl { class BlitCommandEncoder; class Gpu; class RenderCommandEncoder; class CommandBuffer final : public skgpu::CommandBuffer { public: static sk_sp Make(const Gpu*); ~CommandBuffer() override; bool isFinished() { return (*fCommandBuffer).status == MTLCommandBufferStatusCompleted || (*fCommandBuffer).status == MTLCommandBufferStatusError; } void waitUntilFinished() { // TODO: it's not clear what do to if status is Enqueued. Commit and then wait? if ((*fCommandBuffer).status == MTLCommandBufferStatusScheduled || (*fCommandBuffer).status == MTLCommandBufferStatusCommitted) { [(*fCommandBuffer) waitUntilCompleted]; } if (!this->isFinished()) { SkDebugf("Unfinished command buffer status: %d\n", (int)(*fCommandBuffer).status); SkASSERT(false); } } bool commit(); private: CommandBuffer(sk_cfp> cmdBuffer, const Gpu* gpu); void onBeginRenderPass(const RenderPassDesc&) override; void endRenderPass() override; void onBindGraphicsPipeline(const skgpu::GraphicsPipeline*) override; void onBindUniformBuffer(const skgpu::Buffer*, size_t offset) override; void onBindVertexBuffers(const skgpu::Buffer* vertexBuffer, size_t vertexOffset, const skgpu::Buffer* instanceBuffer, size_t instanceOffset) override; void onBindIndexBuffer(const skgpu::Buffer* indexBuffer, size_t offset) override; void onDraw(PrimitiveType type, unsigned int baseVertex, unsigned int vertexCount) override; void onDrawIndexed(PrimitiveType type, unsigned int baseIndex, unsigned int indexCount, unsigned int baseVertex) override; void onDrawInstanced(PrimitiveType type, unsigned int baseVertex, unsigned int vertexCount, unsigned int baseInstance, unsigned int instanceCount) override; void onDrawIndexedInstanced(PrimitiveType type, unsigned int baseIndex, unsigned int indexCount, unsigned int baseVertex, unsigned int baseInstance, unsigned int instanceCount) override; void onCopyTextureToBuffer(const skgpu::Texture*, SkIRect srcRect, const skgpu::Buffer*, size_t bufferOffset, size_t bufferRowBytes) override; BlitCommandEncoder* getBlitCommandEncoder(); void endBlitCommandEncoder(); sk_cfp> fCommandBuffer; sk_sp fActiveRenderCommandEncoder; sk_sp fActiveBlitCommandEncoder; size_t fCurrentVertexStride = 0; size_t fCurrentInstanceStride = 0; id fCurrentIndexBuffer; size_t fCurrentIndexBufferOffset = 0; const Gpu* fGpu; }; } // namespace skgpu::mtl #endif // skgpu_MtlCommandBuffer_DEFINED