blob: 5a6bc36d789ec1a0699f10d608e2bc43a6b8d6a8 [file] [log] [blame]
Timothy Liange70604e2018-07-19 09:49:46 -04001/*
2 * Copyright 2018 Google Inc.
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 GrMtlGpuCommandBuffer_DEFINED
9#define GrMtlGpuCommandBuffer_DEFINED
10
Mike Kleinc0bd9f92019-04-23 12:05:21 -050011#include "src/gpu/GrGpuCommandBuffer.h"
12#include "src/gpu/GrMesh.h"
13#include "src/gpu/GrOpFlushState.h"
14#include "src/gpu/mtl/GrMtlGpu.h"
Timothy Liange70604e2018-07-19 09:49:46 -040015
Timothy Liang7ac582e2018-08-06 09:47:23 -040016#import <metal/metal.h>
17
Timothy Liang5422f9a2018-08-10 10:57:55 -040018typedef uint32_t GrColor;
Jim Van Verth35a67eb2019-05-03 10:58:40 -040019class GrMtlBuffer;
Timothy Liang5422f9a2018-08-10 10:57:55 -040020class GrMtlPipelineState;
Timothy Liange70604e2018-07-19 09:49:46 -040021class GrMtlRenderTarget;
22
23class GrMtlGpuTextureCommandBuffer : public GrGpuTextureCommandBuffer {
24public:
25 GrMtlGpuTextureCommandBuffer(GrMtlGpu* gpu, GrTexture* texture, GrSurfaceOrigin origin)
26 : INHERITED(texture, origin)
27 , fGpu(gpu) {
Timothy Liange70604e2018-07-19 09:49:46 -040028 }
29
30 ~GrMtlGpuTextureCommandBuffer() override {}
31
32 void copy(GrSurface* src, GrSurfaceOrigin srcOrigin, const SkIRect& srcRect,
Timothy Liange35055f2018-07-20 16:53:00 -040033 const SkIPoint& dstPoint) override {
34 fGpu->copySurface(fTexture, fOrigin, src, srcOrigin, srcRect, dstPoint);
35 }
Timothy Liange70604e2018-07-19 09:49:46 -040036
37 void insertEventMarker(const char* msg) override {}
38
39private:
Timothy Liange70604e2018-07-19 09:49:46 -040040 GrMtlGpu* fGpu;
41
42 typedef GrGpuTextureCommandBuffer INHERITED;
43};
44
Timothy Liang5422f9a2018-08-10 10:57:55 -040045class GrMtlGpuRTCommandBuffer : public GrGpuRTCommandBuffer, private GrMesh::SendToGpuImpl {
Timothy Liange70604e2018-07-19 09:49:46 -040046public:
47 GrMtlGpuRTCommandBuffer(GrMtlGpu* gpu, GrRenderTarget* rt, GrSurfaceOrigin origin,
Ethan Nicholas56d19a52018-10-15 11:26:20 -040048 const SkRect& bounds,
Timothy Liange70604e2018-07-19 09:49:46 -040049 const GrGpuRTCommandBuffer::LoadAndStoreInfo& colorInfo,
Timothy Liang5422f9a2018-08-10 10:57:55 -040050 const GrGpuRTCommandBuffer::StencilLoadAndStoreInfo& stencilInfo);
51
52 ~GrMtlGpuRTCommandBuffer() override;
Timothy Liange70604e2018-07-19 09:49:46 -040053
Timothy Liange70604e2018-07-19 09:49:46 -040054 void begin() override {}
55 void end() override {}
56
57 void discard() override {}
58
59 void insertEventMarker(const char* msg) override {}
60
Jim Van Verth6f8bfba2019-04-15 15:28:41 -040061 void inlineUpload(GrOpFlushState* state, GrDeferredTextureUploadFn& upload) override {
62 // TODO: this could be more efficient
63 state->doUpload(upload);
64 }
Timothy Liange70604e2018-07-19 09:49:46 -040065
66 void copy(GrSurface* src, GrSurfaceOrigin srcOrigin, const SkIRect& srcRect,
Timothy Liang5422f9a2018-08-10 10:57:55 -040067 const SkIPoint& dstPoint) override;
68
69 void submit();
Timothy Liange70604e2018-07-19 09:49:46 -040070
Timothy Liange70604e2018-07-19 09:49:46 -040071private:
Jim Van Verthae336bd2019-03-06 13:50:56 -050072 void addNullCommand();
Timothy Liang5422f9a2018-08-10 10:57:55 -040073
Timothy Liange70604e2018-07-19 09:49:46 -040074 GrGpu* gpu() override { return fGpu; }
75
Timothy Liang5422f9a2018-08-10 10:57:55 -040076 GrMtlPipelineState* prepareDrawState(
77 const GrPrimitiveProcessor& primProc,
78 const GrPipeline& pipeline,
79 const GrPipeline::FixedDynamicState* fixedDynamicState,
Jim Van Verth1223e7f2019-02-28 17:38:35 -050080 GrPrimitiveType primType);
Timothy Liang5422f9a2018-08-10 10:57:55 -040081
Timothy Liange70604e2018-07-19 09:49:46 -040082 void onDraw(const GrPrimitiveProcessor& primProc,
83 const GrPipeline& pipeline,
84 const GrPipeline::FixedDynamicState* fixedDynamicState,
85 const GrPipeline::DynamicStateArrays* dynamicStateArrays,
86 const GrMesh mesh[],
87 int meshCount,
Timothy Liang5422f9a2018-08-10 10:57:55 -040088 const SkRect& bounds) override;
Timothy Liange70604e2018-07-19 09:49:46 -040089
Brian Osman9a9baae2018-11-05 15:06:26 -050090 void onClear(const GrFixedClip& clip, const SkPMColor4f& color) override;
Timothy Liange70604e2018-07-19 09:49:46 -040091
Ethan Nicholas01063512018-10-08 16:58:25 -040092 void onClearStencilClip(const GrFixedClip& clip, bool insideStencilMask) override;
Timothy Liange70604e2018-07-19 09:49:46 -040093
Timothy Liang5422f9a2018-08-10 10:57:55 -040094 MTLRenderPassDescriptor* createRenderPassDesc() const;
95
96 void bindGeometry(const GrBuffer* vertexBuffer, const GrBuffer* instanceBuffer);
97
98 // GrMesh::SendToGpuImpl methods. These issue the actual Metal draw commands.
99 // Marked final as a hint to the compiler to not use virtual dispatch.
100 void sendMeshToGpu(GrPrimitiveType primType, const GrBuffer* vertexBuffer, int vertexCount,
101 int baseVertex) final {
102 this->sendInstancedMeshToGpu(primType, vertexBuffer, vertexCount, baseVertex, nullptr, 1,
103 0);
104 }
105
106 void sendIndexedMeshToGpu(GrPrimitiveType primType, const GrBuffer* indexBuffer, int indexCount,
107 int baseIndex, uint16_t /*minIndexValue*/, uint16_t /*maxIndexValue*/,
108 const GrBuffer* vertexBuffer, int baseVertex,
109 GrPrimitiveRestart restart) final {
110 SkASSERT(restart == GrPrimitiveRestart::kNo);
111 this->sendIndexedInstancedMeshToGpu(primType, indexBuffer, indexCount, baseIndex,
112 vertexBuffer, baseVertex, nullptr, 1, 0,
113 GrPrimitiveRestart::kNo);
114 }
115
116 void sendInstancedMeshToGpu(GrPrimitiveType, const GrBuffer* vertexBuffer, int vertexCount,
117 int baseVertex, const GrBuffer* instanceBuffer, int instanceCount,
118 int baseInstance) final;
119
120 void sendIndexedInstancedMeshToGpu(GrPrimitiveType, const GrBuffer* indexBuffer, int indexCount,
121 int baseIndex, const GrBuffer* vertexBuffer, int baseVertex,
122 const GrBuffer* instanceBuffer, int instanceCount,
123 int baseInstance, GrPrimitiveRestart) final;
124
Jim Van Verth35a67eb2019-05-03 10:58:40 -0400125 void setVertexBuffer(id<MTLRenderCommandEncoder>, const GrMtlBuffer*, size_t index);
126 void resetBufferBindings();
127
Timothy Liange70604e2018-07-19 09:49:46 -0400128 GrMtlGpu* fGpu;
Ethan Nicholas56d19a52018-10-15 11:26:20 -0400129 // GrRenderTargetProxy bounds
Ethan Nicholas391d3442018-10-15 13:00:59 -0400130#ifdef SK_DEBUG
Ethan Nicholas56d19a52018-10-15 11:26:20 -0400131 SkRect fBounds;
Ethan Nicholas391d3442018-10-15 13:00:59 -0400132#endif
Timothy Liange70604e2018-07-19 09:49:46 -0400133 GrGpuRTCommandBuffer::LoadAndStoreInfo fColorLoadAndStoreInfo;
134 GrGpuRTCommandBuffer::StencilLoadAndStoreInfo fStencilLoadAndStoreInfo;
135
Jim Van Verth61610be2019-03-07 15:33:49 -0500136 id<MTLRenderCommandEncoder> fActiveRenderCmdEncoder;
137 MTLRenderPassDescriptor* fRenderPassDesc;
Timothy Liang5422f9a2018-08-10 10:57:55 -0400138
139 struct CommandBufferInfo {
140 SkRect fBounds;
141 };
142
143 CommandBufferInfo fCommandBufferInfo;
144
Jim Van Verth35a67eb2019-05-03 10:58:40 -0400145 static constexpr size_t kNumBindings = GrMtlUniformHandler::kLastUniformBinding + 3;
146 id<MTLBuffer> fBufferBindings[kNumBindings];
147
Timothy Liange70604e2018-07-19 09:49:46 -0400148 typedef GrGpuRTCommandBuffer INHERITED;
149};
150
151#endif
152