Debian Codegraph @

src/gpu/graphite/mtl/MtlCommandBuffer.h

raw
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
11 #include "src/gpu/graphite/CommandBuffer.h"
12 #include "src/gpu/graphite/DrawPass.h"
13 #include "src/gpu/graphite/Log.h"
14 #include "src/gpu/graphite/mtl/MtlResourceProvider.h"
15
16 #include <memory>
17
18 #include "include/core/SkTypes.h"
19 #include "include/ports/SkCFObject.h"
20
21 #import <Metal/Metal.h>
22
23 namespace skgpu::graphite {
24 class ComputePipeline;
25 class MtlBlitCommandEncoder;
26 class MtlComputeCommandEncoder;
27 class MtlRenderCommandEncoder;
28 class MtlSharedContext;
29 struct WorkgroupSize;
30
31 class MtlCommandBuffer final : public CommandBuffer {
32 public:
33 static std::unique_ptr<MtlCommandBuffer> Make(id<MTLCommandQueue>,
34 const MtlSharedContext*,
35 MtlResourceProvider*);
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 }
50 void waitUntilFinished() {
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
64 private:
65 MtlCommandBuffer(id<MTLCommandQueue>,
66 const MtlSharedContext* sharedContext,
67 MtlResourceProvider* resourceProvider);
68
69 ResourceProvider* resourceProvider() const override { return fResourceProvider; }
70
71 bool createNewMTLCommandBuffer();
72
73 void onResetCommandBuffer() override;
74
75 bool onAddRenderPass(const RenderPassDesc&,
76 SkIRect renderPassBounds,
77 const Texture* colorTexture,
78 const Texture* resolveTexture,
79 const Texture* depthStencilTexture,
80 SkIPoint resolveOffset,
81 SkIRect viewport,
82 const DrawPassList&) override;
83 bool onAddComputePass(DispatchGroupSpan) override;
84
85 // Methods for populating a MTLRenderCommandEncoder:
86 bool beginRenderPass(const RenderPassDesc&,
87 const Texture* colorTexture,
88 const Texture* resolveTexture,
89 const Texture* depthStencilTexture);
90 void endRenderPass();
91
92 [[nodiscard]] bool addDrawPass(DrawPass*);
93
94 void updateIntrinsicUniforms(SkIRect viewport);
95
96 void bindGraphicsPipeline(const GraphicsPipeline*);
97 void setBlendConstants(std::array<float, 4> blendConstants);
98
99 void bindUniformBuffer(const BindBufferInfo& info, UniformSlot);
100 void bindInputBuffer(const Buffer* buffer, size_t offset, uint32_t bindingIndex);
101 void bindIndexBuffer(const Buffer* indexBuffer, size_t offset);
102 void bindIndirectBuffer(const Buffer* indirectBuffer, size_t offset);
103
104 void bindTextureAndSampler(const Texture*, const Sampler*, unsigned int bindIndex);
105
106 void setScissor(const Scissor&);
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