blob: d3f964f82a445d05deaf43b37716476e96c50506 [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"
Timothy Liange70604e2018-07-19 09:49:46 -040014
Timothy Liang7ac582e2018-08-06 09:47:23 -040015#import <metal/metal.h>
16
Timothy Liang5422f9a2018-08-10 10:57:55 -040017typedef uint32_t GrColor;
18class GrMtlPipelineState;
Timothy Liange70604e2018-07-19 09:49:46 -040019class GrMtlRenderTarget;
20
21class GrMtlGpuTextureCommandBuffer : public GrGpuTextureCommandBuffer {
22public:
23 GrMtlGpuTextureCommandBuffer(GrMtlGpu* gpu, GrTexture* texture, GrSurfaceOrigin origin)
24 : INHERITED(texture, origin)
25 , fGpu(gpu) {
Timothy Liange70604e2018-07-19 09:49:46 -040026 }
27
28 ~GrMtlGpuTextureCommandBuffer() override {}
29
30 void copy(GrSurface* src, GrSurfaceOrigin srcOrigin, const SkIRect& srcRect,
Timothy Liange35055f2018-07-20 16:53:00 -040031 const SkIPoint& dstPoint) override {
32 fGpu->copySurface(fTexture, fOrigin, src, srcOrigin, srcRect, dstPoint);
33 }
Timothy Liange70604e2018-07-19 09:49:46 -040034
35 void insertEventMarker(const char* msg) override {}
36
37private:
Timothy Liange70604e2018-07-19 09:49:46 -040038 GrMtlGpu* fGpu;
39
40 typedef GrGpuTextureCommandBuffer INHERITED;
41};
42
Timothy Liang5422f9a2018-08-10 10:57:55 -040043class GrMtlGpuRTCommandBuffer : public GrGpuRTCommandBuffer, private GrMesh::SendToGpuImpl {
Timothy Liange70604e2018-07-19 09:49:46 -040044public:
45 GrMtlGpuRTCommandBuffer(GrMtlGpu* gpu, GrRenderTarget* rt, GrSurfaceOrigin origin,
46 const GrGpuRTCommandBuffer::LoadAndStoreInfo& colorInfo,
Timothy Liang5422f9a2018-08-10 10:57:55 -040047 const GrGpuRTCommandBuffer::StencilLoadAndStoreInfo& stencilInfo);
48
49 ~GrMtlGpuRTCommandBuffer() override;
Timothy Liange70604e2018-07-19 09:49:46 -040050
Timothy Liange70604e2018-07-19 09:49:46 -040051 void begin() override {}
52 void end() override {}
53
54 void discard() override {}
55
56 void insertEventMarker(const char* msg) override {}
57
58 void inlineUpload(GrOpFlushState* state, GrDeferredTextureUploadFn& upload) override {}
59
60 void copy(GrSurface* src, GrSurfaceOrigin srcOrigin, const SkIRect& srcRect,
Timothy Liang5422f9a2018-08-10 10:57:55 -040061 const SkIPoint& dstPoint) override;
62
63 void submit();
Timothy Liange70604e2018-07-19 09:49:46 -040064
Timothy Liange70604e2018-07-19 09:49:46 -040065private:
Timothy Liang5422f9a2018-08-10 10:57:55 -040066 void internalBegin();
67 void internalEnd();
68
Timothy Liange70604e2018-07-19 09:49:46 -040069 GrGpu* gpu() override { return fGpu; }
70
Timothy Liang5422f9a2018-08-10 10:57:55 -040071 GrMtlPipelineState* prepareDrawState(
72 const GrPrimitiveProcessor& primProc,
73 const GrPipeline& pipeline,
74 const GrPipeline::FixedDynamicState* fixedDynamicState,
75 const GrMesh meshes[],
76 int meshCount);
77
Timothy Liange70604e2018-07-19 09:49:46 -040078 void onDraw(const GrPrimitiveProcessor& primProc,
79 const GrPipeline& pipeline,
80 const GrPipeline::FixedDynamicState* fixedDynamicState,
81 const GrPipeline::DynamicStateArrays* dynamicStateArrays,
82 const GrMesh mesh[],
83 int meshCount,
Timothy Liang5422f9a2018-08-10 10:57:55 -040084 const SkRect& bounds) override;
Timothy Liange70604e2018-07-19 09:49:46 -040085
86 void onClear(const GrFixedClip& clip, GrColor color) override {}
87
88 void onClearStencilClip(const GrFixedClip& clip, bool insideStencilMask) override {}
89
Timothy Liang5422f9a2018-08-10 10:57:55 -040090 MTLRenderPassDescriptor* createRenderPassDesc() const;
91
92 void bindGeometry(const GrBuffer* vertexBuffer, const GrBuffer* instanceBuffer);
93
94 // GrMesh::SendToGpuImpl methods. These issue the actual Metal draw commands.
95 // Marked final as a hint to the compiler to not use virtual dispatch.
96 void sendMeshToGpu(GrPrimitiveType primType, const GrBuffer* vertexBuffer, int vertexCount,
97 int baseVertex) final {
98 this->sendInstancedMeshToGpu(primType, vertexBuffer, vertexCount, baseVertex, nullptr, 1,
99 0);
100 }
101
102 void sendIndexedMeshToGpu(GrPrimitiveType primType, const GrBuffer* indexBuffer, int indexCount,
103 int baseIndex, uint16_t /*minIndexValue*/, uint16_t /*maxIndexValue*/,
104 const GrBuffer* vertexBuffer, int baseVertex,
105 GrPrimitiveRestart restart) final {
106 SkASSERT(restart == GrPrimitiveRestart::kNo);
107 this->sendIndexedInstancedMeshToGpu(primType, indexBuffer, indexCount, baseIndex,
108 vertexBuffer, baseVertex, nullptr, 1, 0,
109 GrPrimitiveRestart::kNo);
110 }
111
112 void sendInstancedMeshToGpu(GrPrimitiveType, const GrBuffer* vertexBuffer, int vertexCount,
113 int baseVertex, const GrBuffer* instanceBuffer, int instanceCount,
114 int baseInstance) final;
115
116 void sendIndexedInstancedMeshToGpu(GrPrimitiveType, const GrBuffer* indexBuffer, int indexCount,
117 int baseIndex, const GrBuffer* vertexBuffer, int baseVertex,
118 const GrBuffer* instanceBuffer, int instanceCount,
119 int baseInstance, GrPrimitiveRestart) final;
120
Timothy Liange70604e2018-07-19 09:49:46 -0400121 GrMtlGpu* fGpu;
122 GrGpuRTCommandBuffer::LoadAndStoreInfo fColorLoadAndStoreInfo;
123 GrGpuRTCommandBuffer::StencilLoadAndStoreInfo fStencilLoadAndStoreInfo;
124
Timothy Liang5422f9a2018-08-10 10:57:55 -0400125 id<MTLRenderCommandEncoder> fActiveRenderCmdEncoder;
126 MTLRenderPassDescriptor* fRenderPassDesc;
127
128 struct CommandBufferInfo {
129 SkRect fBounds;
130 };
131
132 CommandBufferInfo fCommandBufferInfo;
133
Timothy Liange70604e2018-07-19 09:49:46 -0400134 typedef GrGpuRTCommandBuffer INHERITED;
135};
136
137#endif
138