blob: 955fe31dba065bd60e152f4436947205638dc270 [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
11#include "GrGpuCommandBuffer.h"
Timothy Liang7ac582e2018-08-06 09:47:23 -040012#include "GrMtlGpu.h"
Timothy Liang5422f9a2018-08-10 10:57:55 -040013#include "GrMesh.h"
Jim Van Verth6f8bfba2019-04-15 15:28:41 -040014#include "GrOpFlushState.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;
19class GrMtlPipelineState;
Timothy Liange70604e2018-07-19 09:49:46 -040020class GrMtlRenderTarget;
21
22class GrMtlGpuTextureCommandBuffer : public GrGpuTextureCommandBuffer {
23public:
24 GrMtlGpuTextureCommandBuffer(GrMtlGpu* gpu, GrTexture* texture, GrSurfaceOrigin origin)
25 : INHERITED(texture, origin)
26 , fGpu(gpu) {
Timothy Liange70604e2018-07-19 09:49:46 -040027 }
28
29 ~GrMtlGpuTextureCommandBuffer() override {}
30
31 void copy(GrSurface* src, GrSurfaceOrigin srcOrigin, const SkIRect& srcRect,
Timothy Liange35055f2018-07-20 16:53:00 -040032 const SkIPoint& dstPoint) override {
33 fGpu->copySurface(fTexture, fOrigin, src, srcOrigin, srcRect, dstPoint);
34 }
Timothy Liange70604e2018-07-19 09:49:46 -040035
36 void insertEventMarker(const char* msg) override {}
37
38private:
Timothy Liange70604e2018-07-19 09:49:46 -040039 GrMtlGpu* fGpu;
40
41 typedef GrGpuTextureCommandBuffer INHERITED;
42};
43
Timothy Liang5422f9a2018-08-10 10:57:55 -040044class GrMtlGpuRTCommandBuffer : public GrGpuRTCommandBuffer, private GrMesh::SendToGpuImpl {
Timothy Liange70604e2018-07-19 09:49:46 -040045public:
46 GrMtlGpuRTCommandBuffer(GrMtlGpu* gpu, GrRenderTarget* rt, GrSurfaceOrigin origin,
Ethan Nicholas56d19a52018-10-15 11:26:20 -040047 const SkRect& bounds,
Timothy Liange70604e2018-07-19 09:49:46 -040048 const GrGpuRTCommandBuffer::LoadAndStoreInfo& colorInfo,
Timothy Liang5422f9a2018-08-10 10:57:55 -040049 const GrGpuRTCommandBuffer::StencilLoadAndStoreInfo& stencilInfo);
50
51 ~GrMtlGpuRTCommandBuffer() override;
Timothy Liange70604e2018-07-19 09:49:46 -040052
Timothy Liange70604e2018-07-19 09:49:46 -040053 void begin() override {}
54 void end() override {}
55
56 void discard() override {}
57
58 void insertEventMarker(const char* msg) override {}
59
Jim Van Verth6f8bfba2019-04-15 15:28:41 -040060 void inlineUpload(GrOpFlushState* state, GrDeferredTextureUploadFn& upload) override {
61 // TODO: this could be more efficient
62 state->doUpload(upload);
63 }
Timothy Liange70604e2018-07-19 09:49:46 -040064
65 void copy(GrSurface* src, GrSurfaceOrigin srcOrigin, const SkIRect& srcRect,
Timothy Liang5422f9a2018-08-10 10:57:55 -040066 const SkIPoint& dstPoint) override;
67
68 void submit();
Timothy Liange70604e2018-07-19 09:49:46 -040069
Timothy Liange70604e2018-07-19 09:49:46 -040070private:
Jim Van Verthae336bd2019-03-06 13:50:56 -050071 void addNullCommand();
Timothy Liang5422f9a2018-08-10 10:57:55 -040072
Timothy Liange70604e2018-07-19 09:49:46 -040073 GrGpu* gpu() override { return fGpu; }
74
Timothy Liang5422f9a2018-08-10 10:57:55 -040075 GrMtlPipelineState* prepareDrawState(
76 const GrPrimitiveProcessor& primProc,
77 const GrPipeline& pipeline,
78 const GrPipeline::FixedDynamicState* fixedDynamicState,
Jim Van Verth1223e7f2019-02-28 17:38:35 -050079 GrPrimitiveType primType);
Timothy Liang5422f9a2018-08-10 10:57:55 -040080
Timothy Liange70604e2018-07-19 09:49:46 -040081 void onDraw(const GrPrimitiveProcessor& primProc,
82 const GrPipeline& pipeline,
83 const GrPipeline::FixedDynamicState* fixedDynamicState,
84 const GrPipeline::DynamicStateArrays* dynamicStateArrays,
85 const GrMesh mesh[],
86 int meshCount,
Timothy Liang5422f9a2018-08-10 10:57:55 -040087 const SkRect& bounds) override;
Timothy Liange70604e2018-07-19 09:49:46 -040088
Brian Osman9a9baae2018-11-05 15:06:26 -050089 void onClear(const GrFixedClip& clip, const SkPMColor4f& color) override;
Timothy Liange70604e2018-07-19 09:49:46 -040090
Ethan Nicholas01063512018-10-08 16:58:25 -040091 void onClearStencilClip(const GrFixedClip& clip, bool insideStencilMask) override;
Timothy Liange70604e2018-07-19 09:49:46 -040092
Timothy Liang5422f9a2018-08-10 10:57:55 -040093 MTLRenderPassDescriptor* createRenderPassDesc() const;
94
95 void bindGeometry(const GrBuffer* vertexBuffer, const GrBuffer* instanceBuffer);
96
97 // GrMesh::SendToGpuImpl methods. These issue the actual Metal draw commands.
98 // Marked final as a hint to the compiler to not use virtual dispatch.
99 void sendMeshToGpu(GrPrimitiveType primType, const GrBuffer* vertexBuffer, int vertexCount,
100 int baseVertex) final {
101 this->sendInstancedMeshToGpu(primType, vertexBuffer, vertexCount, baseVertex, nullptr, 1,
102 0);
103 }
104
105 void sendIndexedMeshToGpu(GrPrimitiveType primType, const GrBuffer* indexBuffer, int indexCount,
106 int baseIndex, uint16_t /*minIndexValue*/, uint16_t /*maxIndexValue*/,
107 const GrBuffer* vertexBuffer, int baseVertex,
108 GrPrimitiveRestart restart) final {
109 SkASSERT(restart == GrPrimitiveRestart::kNo);
110 this->sendIndexedInstancedMeshToGpu(primType, indexBuffer, indexCount, baseIndex,
111 vertexBuffer, baseVertex, nullptr, 1, 0,
112 GrPrimitiveRestart::kNo);
113 }
114
115 void sendInstancedMeshToGpu(GrPrimitiveType, const GrBuffer* vertexBuffer, int vertexCount,
116 int baseVertex, const GrBuffer* instanceBuffer, int instanceCount,
117 int baseInstance) final;
118
119 void sendIndexedInstancedMeshToGpu(GrPrimitiveType, const GrBuffer* indexBuffer, int indexCount,
120 int baseIndex, const GrBuffer* vertexBuffer, int baseVertex,
121 const GrBuffer* instanceBuffer, int instanceCount,
122 int baseInstance, GrPrimitiveRestart) final;
123
Timothy Liange70604e2018-07-19 09:49:46 -0400124 GrMtlGpu* fGpu;
Ethan Nicholas56d19a52018-10-15 11:26:20 -0400125 // GrRenderTargetProxy bounds
Ethan Nicholas391d3442018-10-15 13:00:59 -0400126#ifdef SK_DEBUG
Ethan Nicholas56d19a52018-10-15 11:26:20 -0400127 SkRect fBounds;
Ethan Nicholas391d3442018-10-15 13:00:59 -0400128#endif
Timothy Liange70604e2018-07-19 09:49:46 -0400129 GrGpuRTCommandBuffer::LoadAndStoreInfo fColorLoadAndStoreInfo;
130 GrGpuRTCommandBuffer::StencilLoadAndStoreInfo fStencilLoadAndStoreInfo;
131
Jim Van Verth61610be2019-03-07 15:33:49 -0500132 id<MTLRenderCommandEncoder> fActiveRenderCmdEncoder;
133 MTLRenderPassDescriptor* fRenderPassDesc;
Timothy Liang5422f9a2018-08-10 10:57:55 -0400134
135 struct CommandBufferInfo {
136 SkRect fBounds;
137 };
138
139 CommandBufferInfo fCommandBufferInfo;
140
Timothy Liange70604e2018-07-19 09:49:46 -0400141 typedef GrGpuRTCommandBuffer INHERITED;
142};
143
144#endif
145