blob: f7a0eb9eda89e872e213a28bd79656e2db08be29 [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 }
Brian Salomonab32f652019-05-10 14:24:50 -040036 void transferFrom(const SkIRect& srcRect, GrColorType bufferColorType,
37 GrGpuBuffer* transferBuffer, size_t offset) override {
38 fGpu->transferPixelsFrom(fTexture, srcRect.fLeft, srcRect.fTop, srcRect.width(),
39 srcRect.height(), bufferColorType, transferBuffer, offset);
40 }
Timothy Liange70604e2018-07-19 09:49:46 -040041 void insertEventMarker(const char* msg) override {}
42
43private:
Timothy Liange70604e2018-07-19 09:49:46 -040044 GrMtlGpu* fGpu;
45
46 typedef GrGpuTextureCommandBuffer INHERITED;
47};
48
Timothy Liang5422f9a2018-08-10 10:57:55 -040049class GrMtlGpuRTCommandBuffer : public GrGpuRTCommandBuffer, private GrMesh::SendToGpuImpl {
Timothy Liange70604e2018-07-19 09:49:46 -040050public:
51 GrMtlGpuRTCommandBuffer(GrMtlGpu* gpu, GrRenderTarget* rt, GrSurfaceOrigin origin,
Ethan Nicholas56d19a52018-10-15 11:26:20 -040052 const SkRect& bounds,
Timothy Liange70604e2018-07-19 09:49:46 -040053 const GrGpuRTCommandBuffer::LoadAndStoreInfo& colorInfo,
Timothy Liang5422f9a2018-08-10 10:57:55 -040054 const GrGpuRTCommandBuffer::StencilLoadAndStoreInfo& stencilInfo);
55
56 ~GrMtlGpuRTCommandBuffer() override;
Timothy Liange70604e2018-07-19 09:49:46 -040057
Timothy Liange70604e2018-07-19 09:49:46 -040058 void begin() override {}
59 void end() override {}
60
61 void discard() override {}
62
63 void insertEventMarker(const char* msg) override {}
64
Jim Van Verth6f8bfba2019-04-15 15:28:41 -040065 void inlineUpload(GrOpFlushState* state, GrDeferredTextureUploadFn& upload) override {
66 // TODO: this could be more efficient
67 state->doUpload(upload);
68 }
Brian Salomonab32f652019-05-10 14:24:50 -040069 void transferFrom(const SkIRect& srcRect, GrColorType bufferColorType,
70 GrGpuBuffer* transferBuffer, size_t offset) override;
Timothy Liange70604e2018-07-19 09:49:46 -040071 void copy(GrSurface* src, GrSurfaceOrigin srcOrigin, const SkIRect& srcRect,
Timothy Liang5422f9a2018-08-10 10:57:55 -040072 const SkIPoint& dstPoint) override;
73
74 void submit();
Timothy Liange70604e2018-07-19 09:49:46 -040075
Timothy Liange70604e2018-07-19 09:49:46 -040076private:
Jim Van Verthae336bd2019-03-06 13:50:56 -050077 void addNullCommand();
Timothy Liang5422f9a2018-08-10 10:57:55 -040078
Timothy Liange70604e2018-07-19 09:49:46 -040079 GrGpu* gpu() override { return fGpu; }
80
Timothy Liang5422f9a2018-08-10 10:57:55 -040081 GrMtlPipelineState* prepareDrawState(
82 const GrPrimitiveProcessor& primProc,
83 const GrPipeline& pipeline,
84 const GrPipeline::FixedDynamicState* fixedDynamicState,
Jim Van Verth1223e7f2019-02-28 17:38:35 -050085 GrPrimitiveType primType);
Timothy Liang5422f9a2018-08-10 10:57:55 -040086
Timothy Liange70604e2018-07-19 09:49:46 -040087 void onDraw(const GrPrimitiveProcessor& primProc,
88 const GrPipeline& pipeline,
89 const GrPipeline::FixedDynamicState* fixedDynamicState,
90 const GrPipeline::DynamicStateArrays* dynamicStateArrays,
91 const GrMesh mesh[],
92 int meshCount,
Timothy Liang5422f9a2018-08-10 10:57:55 -040093 const SkRect& bounds) override;
Timothy Liange70604e2018-07-19 09:49:46 -040094
Brian Osman9a9baae2018-11-05 15:06:26 -050095 void onClear(const GrFixedClip& clip, const SkPMColor4f& color) override;
Timothy Liange70604e2018-07-19 09:49:46 -040096
Ethan Nicholas01063512018-10-08 16:58:25 -040097 void onClearStencilClip(const GrFixedClip& clip, bool insideStencilMask) override;
Timothy Liange70604e2018-07-19 09:49:46 -040098
Timothy Liang5422f9a2018-08-10 10:57:55 -040099 MTLRenderPassDescriptor* createRenderPassDesc() const;
100
Jim Van Verth03464f12019-06-03 11:09:40 -0400101 void bindGeometry(const GrBuffer* vertexBuffer, size_t vertexOffset,
102 const GrBuffer* instanceBuffer);
Timothy Liang5422f9a2018-08-10 10:57:55 -0400103
104 // GrMesh::SendToGpuImpl methods. These issue the actual Metal draw commands.
105 // Marked final as a hint to the compiler to not use virtual dispatch.
106 void sendMeshToGpu(GrPrimitiveType primType, const GrBuffer* vertexBuffer, int vertexCount,
Jim Van Verth03464f12019-06-03 11:09:40 -0400107 int baseVertex) final;
Timothy Liang5422f9a2018-08-10 10:57:55 -0400108
109 void sendIndexedMeshToGpu(GrPrimitiveType primType, const GrBuffer* indexBuffer, int indexCount,
110 int baseIndex, uint16_t /*minIndexValue*/, uint16_t /*maxIndexValue*/,
111 const GrBuffer* vertexBuffer, int baseVertex,
Jim Van Verth03464f12019-06-03 11:09:40 -0400112 GrPrimitiveRestart restart) final;
Timothy Liang5422f9a2018-08-10 10:57:55 -0400113
114 void sendInstancedMeshToGpu(GrPrimitiveType, const GrBuffer* vertexBuffer, int vertexCount,
115 int baseVertex, const GrBuffer* instanceBuffer, int instanceCount,
116 int baseInstance) final;
117
118 void sendIndexedInstancedMeshToGpu(GrPrimitiveType, const GrBuffer* indexBuffer, int indexCount,
119 int baseIndex, const GrBuffer* vertexBuffer, int baseVertex,
120 const GrBuffer* instanceBuffer, int instanceCount,
121 int baseInstance, GrPrimitiveRestart) final;
122
Jim Van Verth03464f12019-06-03 11:09:40 -0400123 void setVertexBuffer(id<MTLRenderCommandEncoder>, const GrMtlBuffer*, size_t offset,
124 size_t index);
Jim Van Verth35a67eb2019-05-03 10:58:40 -0400125 void resetBufferBindings();
126
Timothy Liange70604e2018-07-19 09:49:46 -0400127 GrMtlGpu* fGpu;
Ethan Nicholas56d19a52018-10-15 11:26:20 -0400128 // GrRenderTargetProxy bounds
Ethan Nicholas391d3442018-10-15 13:00:59 -0400129#ifdef SK_DEBUG
Ethan Nicholas56d19a52018-10-15 11:26:20 -0400130 SkRect fBounds;
Ethan Nicholas391d3442018-10-15 13:00:59 -0400131#endif
Timothy Liange70604e2018-07-19 09:49:46 -0400132 GrGpuRTCommandBuffer::LoadAndStoreInfo fColorLoadAndStoreInfo;
133 GrGpuRTCommandBuffer::StencilLoadAndStoreInfo fStencilLoadAndStoreInfo;
134
Jim Van Verth61610be2019-03-07 15:33:49 -0500135 id<MTLRenderCommandEncoder> fActiveRenderCmdEncoder;
136 MTLRenderPassDescriptor* fRenderPassDesc;
Timothy Liang5422f9a2018-08-10 10:57:55 -0400137
138 struct CommandBufferInfo {
139 SkRect fBounds;
Jim Van Verth03464f12019-06-03 11:09:40 -0400140 size_t fCurrentVertexStride;
Timothy Liang5422f9a2018-08-10 10:57:55 -0400141 };
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