blob: 7e820e322523c81f3f1bb7953a5fd276dd9a9963 [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
Greg Daniel46cfbc62019-06-07 11:43:30 -040032 void copy(GrSurface* src, const SkIRect& srcRect, const SkIPoint& dstPoint) override {
33 fGpu->copySurface(fTexture, src, srcRect, dstPoint);
Timothy Liange35055f2018-07-20 16:53:00 -040034 }
Brian Salomonab32f652019-05-10 14:24:50 -040035 void transferFrom(const SkIRect& srcRect, GrColorType bufferColorType,
36 GrGpuBuffer* transferBuffer, size_t offset) override {
37 fGpu->transferPixelsFrom(fTexture, srcRect.fLeft, srcRect.fTop, srcRect.width(),
38 srcRect.height(), bufferColorType, transferBuffer, offset);
39 }
Timothy Liange70604e2018-07-19 09:49:46 -040040 void insertEventMarker(const char* msg) override {}
41
42private:
Timothy Liange70604e2018-07-19 09:49:46 -040043 GrMtlGpu* fGpu;
44
45 typedef GrGpuTextureCommandBuffer INHERITED;
46};
47
Timothy Liang5422f9a2018-08-10 10:57:55 -040048class GrMtlGpuRTCommandBuffer : public GrGpuRTCommandBuffer, private GrMesh::SendToGpuImpl {
Timothy Liange70604e2018-07-19 09:49:46 -040049public:
50 GrMtlGpuRTCommandBuffer(GrMtlGpu* gpu, GrRenderTarget* rt, GrSurfaceOrigin origin,
Ethan Nicholas56d19a52018-10-15 11:26:20 -040051 const SkRect& bounds,
Timothy Liange70604e2018-07-19 09:49:46 -040052 const GrGpuRTCommandBuffer::LoadAndStoreInfo& colorInfo,
Timothy Liang5422f9a2018-08-10 10:57:55 -040053 const GrGpuRTCommandBuffer::StencilLoadAndStoreInfo& stencilInfo);
54
55 ~GrMtlGpuRTCommandBuffer() override;
Timothy Liange70604e2018-07-19 09:49:46 -040056
Timothy Liange70604e2018-07-19 09:49:46 -040057 void begin() override {}
58 void end() override {}
59
60 void discard() override {}
61
62 void insertEventMarker(const char* msg) override {}
63
Jim Van Verth6b6008a2019-06-18 08:38:32 -040064 void initRenderState(id<MTLRenderCommandEncoder>);
65
Jim Van Verth6f8bfba2019-04-15 15:28:41 -040066 void inlineUpload(GrOpFlushState* state, GrDeferredTextureUploadFn& upload) override {
67 // TODO: this could be more efficient
68 state->doUpload(upload);
69 }
Brian Salomonab32f652019-05-10 14:24:50 -040070 void transferFrom(const SkIRect& srcRect, GrColorType bufferColorType,
71 GrGpuBuffer* transferBuffer, size_t offset) override;
Greg Daniel46cfbc62019-06-07 11:43:30 -040072 void copy(GrSurface* src, const SkIRect& srcRect, const SkIPoint& dstPoint) override;
Timothy Liang5422f9a2018-08-10 10:57:55 -040073
74 void submit();
Timothy Liange70604e2018-07-19 09:49:46 -040075
Timothy Liange70604e2018-07-19 09:49:46 -040076private:
77 GrGpu* gpu() override { return fGpu; }
78
Timothy Liang5422f9a2018-08-10 10:57:55 -040079 GrMtlPipelineState* prepareDrawState(
80 const GrPrimitiveProcessor& primProc,
81 const GrPipeline& pipeline,
82 const GrPipeline::FixedDynamicState* fixedDynamicState,
Jim Van Verth1223e7f2019-02-28 17:38:35 -050083 GrPrimitiveType primType);
Timothy Liang5422f9a2018-08-10 10:57:55 -040084
Timothy Liange70604e2018-07-19 09:49:46 -040085 void onDraw(const GrPrimitiveProcessor& primProc,
86 const GrPipeline& pipeline,
87 const GrPipeline::FixedDynamicState* fixedDynamicState,
88 const GrPipeline::DynamicStateArrays* dynamicStateArrays,
89 const GrMesh mesh[],
90 int meshCount,
Timothy Liang5422f9a2018-08-10 10:57:55 -040091 const SkRect& bounds) override;
Timothy Liange70604e2018-07-19 09:49:46 -040092
Brian Osman9a9baae2018-11-05 15:06:26 -050093 void onClear(const GrFixedClip& clip, const SkPMColor4f& color) override;
Timothy Liange70604e2018-07-19 09:49:46 -040094
Ethan Nicholas01063512018-10-08 16:58:25 -040095 void onClearStencilClip(const GrFixedClip& clip, bool insideStencilMask) override;
Timothy Liange70604e2018-07-19 09:49:46 -040096
Jim Van Verthc159f942019-06-10 14:46:51 -040097 void setupRenderPass(const GrGpuRTCommandBuffer::LoadAndStoreInfo& colorInfo,
98 const GrGpuRTCommandBuffer::StencilLoadAndStoreInfo& stencilInfo);
Timothy Liang5422f9a2018-08-10 10:57:55 -040099
Jim Van Verth03464f12019-06-03 11:09:40 -0400100 void bindGeometry(const GrBuffer* vertexBuffer, size_t vertexOffset,
101 const GrBuffer* instanceBuffer);
Timothy Liang5422f9a2018-08-10 10:57:55 -0400102
103 // GrMesh::SendToGpuImpl methods. These issue the actual Metal draw commands.
104 // Marked final as a hint to the compiler to not use virtual dispatch.
105 void sendMeshToGpu(GrPrimitiveType primType, const GrBuffer* vertexBuffer, int vertexCount,
Jim Van Verth03464f12019-06-03 11:09:40 -0400106 int baseVertex) final;
Timothy Liang5422f9a2018-08-10 10:57:55 -0400107
108 void sendIndexedMeshToGpu(GrPrimitiveType primType, const GrBuffer* indexBuffer, int indexCount,
109 int baseIndex, uint16_t /*minIndexValue*/, uint16_t /*maxIndexValue*/,
110 const GrBuffer* vertexBuffer, int baseVertex,
Jim Van Verth03464f12019-06-03 11:09:40 -0400111 GrPrimitiveRestart restart) final;
Timothy Liang5422f9a2018-08-10 10:57:55 -0400112
113 void sendInstancedMeshToGpu(GrPrimitiveType, const GrBuffer* vertexBuffer, int vertexCount,
114 int baseVertex, const GrBuffer* instanceBuffer, int instanceCount,
115 int baseInstance) final;
116
117 void sendIndexedInstancedMeshToGpu(GrPrimitiveType, const GrBuffer* indexBuffer, int indexCount,
118 int baseIndex, const GrBuffer* vertexBuffer, int baseVertex,
119 const GrBuffer* instanceBuffer, int instanceCount,
120 int baseInstance, GrPrimitiveRestart) final;
121
Jim Van Verth03464f12019-06-03 11:09:40 -0400122 void setVertexBuffer(id<MTLRenderCommandEncoder>, const GrMtlBuffer*, size_t offset,
123 size_t index);
Jim Van Verth35a67eb2019-05-03 10:58:40 -0400124 void resetBufferBindings();
Jim Van Verthc159f942019-06-10 14:46:51 -0400125 void precreateCmdEncoder();
Jim Van Verth35a67eb2019-05-03 10:58:40 -0400126
Jim Van Verthc159f942019-06-10 14:46:51 -0400127 GrMtlGpu* fGpu;
Ethan Nicholas56d19a52018-10-15 11:26:20 -0400128 // GrRenderTargetProxy bounds
Ethan Nicholas391d3442018-10-15 13:00:59 -0400129#ifdef SK_DEBUG
Jim Van Verthc159f942019-06-10 14:46:51 -0400130 SkRect fRTBounds;
Ethan Nicholas391d3442018-10-15 13:00:59 -0400131#endif
Timothy Liange70604e2018-07-19 09:49:46 -0400132
Jim Van Verth61610be2019-03-07 15:33:49 -0500133 id<MTLRenderCommandEncoder> fActiveRenderCmdEncoder;
134 MTLRenderPassDescriptor* fRenderPassDesc;
Jim Van Verthc159f942019-06-10 14:46:51 -0400135 SkRect fBounds;
136 size_t fCurrentVertexStride;
Timothy Liang5422f9a2018-08-10 10:57:55 -0400137
Jim Van Verth35a67eb2019-05-03 10:58:40 -0400138 static constexpr size_t kNumBindings = GrMtlUniformHandler::kLastUniformBinding + 3;
Jim Van Verth6b6008a2019-06-18 08:38:32 -0400139 struct {
140 id<MTLBuffer> fBuffer;
141 size_t fOffset;
142 } fBufferBindings[kNumBindings];
Jim Van Verth35a67eb2019-05-03 10:58:40 -0400143
Timothy Liange70604e2018-07-19 09:49:46 -0400144 typedef GrGpuRTCommandBuffer INHERITED;
145};
146
147#endif
148