Flutter Engine
The Flutter Engine
Loading...
Searching...
No Matches
MtlCommandBuffer.h
Go to the documentation of this file.
1/*
2 * Copyright 2021 Google LLC
3 *
4 * Use of this source code is governed by a BSD-style license that can be
5 * found in the LICENSE file.
6 */
7
8#ifndef skgpu_graphite_MtlCommandBuffer_DEFINED
9#define skgpu_graphite_MtlCommandBuffer_DEFINED
10
14
15#include <memory>
16
19
20#import <Metal/Metal.h>
21
22namespace skgpu::graphite {
23class ComputePipeline;
24class MtlBlitCommandEncoder;
25class MtlComputeCommandEncoder;
26class MtlRenderCommandEncoder;
27class MtlResourceProvider;
28class MtlSharedContext;
29struct WorkgroupSize;
30
31class MtlCommandBuffer final : public CommandBuffer {
32public:
33 static std::unique_ptr<MtlCommandBuffer> Make(id<MTLCommandQueue>,
34 const MtlSharedContext*,
36 ~MtlCommandBuffer() override;
37
38 bool setNewCommandBufferResources() override;
39
40 void addWaitSemaphores(size_t numWaitSemaphores,
41 const BackendSemaphore* waitSemaphores) override;
42 void addSignalSemaphores(size_t numSignalSemaphores,
43 const BackendSemaphore* signalSemaphores) override;
44
45 bool isFinished() {
46 return (*fCommandBuffer).status == MTLCommandBufferStatusCompleted ||
47 (*fCommandBuffer).status == MTLCommandBufferStatusError;
48
49 }
51 // TODO: it's not clear what do to if status is Enqueued. Commit and then wait?
52 if ((*fCommandBuffer).status == MTLCommandBufferStatusScheduled ||
53 (*fCommandBuffer).status == MTLCommandBufferStatusCommitted) {
54 [(*fCommandBuffer) waitUntilCompleted];
55 }
56 if (!this->isFinished()) {
57 SKGPU_LOG_E("Unfinished command buffer status: %d",
58 (int)(*fCommandBuffer).status);
59 SkASSERT(false);
60 }
61 }
62 bool commit();
63
64private:
65 MtlCommandBuffer(id<MTLCommandQueue>,
66 const MtlSharedContext* sharedContext,
67 MtlResourceProvider* resourceProvider);
68
69 bool createNewMTLCommandBuffer();
70
71 void onResetCommandBuffer() override;
72
74 const Texture* colorTexture,
75 const Texture* resolveTexture,
76 const Texture* depthStencilTexture,
77 SkRect viewport,
78 const DrawPassList&) override;
80
81 // Methods for populating a MTLRenderCommandEncoder:
82 bool beginRenderPass(const RenderPassDesc&,
83 const Texture* colorTexture,
84 const Texture* resolveTexture,
85 const Texture* depthStencilTexture);
86 void endRenderPass();
87
88 void addDrawPass(const DrawPass*);
89
90 void bindGraphicsPipeline(const GraphicsPipeline*);
91 void setBlendConstants(float* blendConstants);
92
93 void bindUniformBuffer(const BindBufferInfo& info, UniformSlot);
94 void bindDrawBuffers(const BindBufferInfo& vertices,
95 const BindBufferInfo& instances,
96 const BindBufferInfo& indices,
97 const BindBufferInfo& indirect);
98 void bindVertexBuffers(const Buffer* vertexBuffer, size_t vertexOffset,
99 const Buffer* instanceBuffer, size_t instanceOffset);
100 void bindIndexBuffer(const Buffer* indexBuffer, size_t offset);
101 void bindIndirectBuffer(const Buffer* indirectBuffer, size_t offset);
102
103 void bindTextureAndSampler(const Texture*, const Sampler*, unsigned int bindIndex);
104
105 void setScissor(unsigned int left, unsigned int top,
106 unsigned int width, unsigned int height);
107 void setViewport(float x, float y, float width, float height,
108 float minDepth, float maxDepth);
109
110 void draw(PrimitiveType type, unsigned int baseVertex, unsigned int vertexCount);
111 void drawIndexed(PrimitiveType type, unsigned int baseIndex, unsigned int indexCount,
112 unsigned int baseVertex);
113 void drawInstanced(PrimitiveType type,
114 unsigned int baseVertex, unsigned int vertexCount,
115 unsigned int baseInstance, unsigned int instanceCount);
116 void drawIndexedInstanced(PrimitiveType type, unsigned int baseIndex,
117 unsigned int indexCount, unsigned int baseVertex,
118 unsigned int baseInstance, unsigned int instanceCount);
119 void drawIndirect(PrimitiveType type);
120 void drawIndexedIndirect(PrimitiveType type);
121
122 // Methods for populating a MTLComputeCommandEncoder:
123 void beginComputePass();
124 void bindComputePipeline(const ComputePipeline*);
125 void bindBuffer(const Buffer* buffer, unsigned int offset, unsigned int index);
126 void bindTexture(const Texture* texture, unsigned int index);
127 void bindSampler(const Sampler* sampler, unsigned int index);
128 void dispatchThreadgroups(const WorkgroupSize& globalSize, const WorkgroupSize& localSize);
129 void dispatchThreadgroupsIndirect(const WorkgroupSize& localSize,
130 const Buffer* indirectBuffer,
131 size_t indirectBufferOffset);
132 void endComputePass();
133
134 // Methods for populating a MTLBlitCommandEncoder:
135 bool onCopyBufferToBuffer(const Buffer* srcBuffer,
136 size_t srcOffset,
137 const Buffer* dstBuffer,
138 size_t dstOffset,
139 size_t size) override;
140 bool onCopyTextureToBuffer(const Texture*,
141 SkIRect srcRect,
142 const Buffer*,
143 size_t bufferOffset,
144 size_t bufferRowBytes) override;
145 bool onCopyBufferToTexture(const Buffer*,
146 const Texture*,
147 const BufferTextureCopyData* copyData,
148 int count) override;
149 bool onCopyTextureToTexture(const Texture* src,
150 SkIRect srcRect,
151 const Texture* dst,
152 SkIPoint dstPoint,
153 int mipLevel) override;
154 bool onSynchronizeBufferToCpu(const Buffer*, bool* outDidResultInWork) override;
155 bool onClearBuffer(const Buffer*, size_t offset, size_t size) override;
156
157 MtlBlitCommandEncoder* getBlitCommandEncoder();
158 void endBlitCommandEncoder();
159
160 sk_cfp<id<MTLCommandBuffer>> fCommandBuffer;
161 sk_sp<MtlRenderCommandEncoder> fActiveRenderCommandEncoder;
162 sk_sp<MtlComputeCommandEncoder> fActiveComputeCommandEncoder;
163 sk_sp<MtlBlitCommandEncoder> fActiveBlitCommandEncoder;
164
165 id<MTLBuffer> fCurrentIndexBuffer;
166 id<MTLBuffer> fCurrentIndirectBuffer;
167 size_t fCurrentIndexBufferOffset = 0;
168 size_t fCurrentIndirectBufferOffset = 0;
169
170 // The command buffer will outlive the MtlQueueManager which owns the MTLCommandQueue.
171 id<MTLCommandQueue> fQueue;
172 const MtlSharedContext* fSharedContext;
173 MtlResourceProvider* fResourceProvider;
174
175 // If true, the draw commands being added are entirely offscreen and can be skipped.
176 // This can happen if a recording is being replayed with a transform that moves the recorded
177 // commands outside of the render target bounds.
178 bool fDrawIsOffscreen = false;
179};
180
181} // namespace skgpu::graphite
182
183#endif // skgpu_graphite_MtlCommandBuffer_DEFINED
static void info(const char *fmt,...) SK_PRINTF_LIKE(1
Definition DM.cpp:213
int count
#define SKGPU_LOG_E(fmt,...)
Definition Log.h:38
#define SkASSERT(cond)
Definition SkAssert.h:116
static bool left(const SkPoint &p0, const SkPoint &p1)
skia_private::TArray< std::unique_ptr< DrawPass > > DrawPassList
SkSpan< const std::unique_ptr< DispatchGroup > > DispatchGroupSpan
void addWaitSemaphores(size_t numWaitSemaphores, const BackendSemaphore *waitSemaphores) override
bool onSynchronizeBufferToCpu(const Buffer *, bool *outDidResultInWork) override
bool onCopyBufferToTexture(const Buffer *, const Texture *, const BufferTextureCopyData *copyData, int count) override
bool onCopyTextureToBuffer(const Texture *, SkIRect srcRect, const Buffer *, size_t bufferOffset, size_t bufferRowBytes) override
void addSignalSemaphores(size_t numSignalSemaphores, const BackendSemaphore *signalSemaphores) override
static std::unique_ptr< MtlCommandBuffer > Make(id< MTLCommandQueue >, const MtlSharedContext *, MtlResourceProvider *)
bool onCopyTextureToTexture(const Texture *src, SkIRect srcRect, const Texture *dst, SkIPoint dstPoint, int mipLevel) override
bool onCopyBufferToBuffer(const Buffer *srcBuffer, size_t srcOffset, const Buffer *dstBuffer, size_t dstOffset, size_t size) override
bool onAddComputePass(DispatchGroupSpan) override
bool onClearBuffer(const Buffer *, size_t offset, size_t size) override
bool onAddRenderPass(const RenderPassDesc &, const Texture *colorTexture, const Texture *resolveTexture, const Texture *depthStencilTexture, SkRect viewport, const DrawPassList &) override
static const uint8_t buffer[]
FlTexture * texture
double y
double x
int32_t height
int32_t width
Point offset