blob: 723ac0765d14b4bfb8df60c5b983ff7a0e6962a8 [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
Aaron O'Mullan829b6a02019-07-08 01:31:14 +020016#import <Metal/Metal.h>
Timothy Liang7ac582e2018-08-06 09:47:23 -040017
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 Salomonf77c1462019-08-01 15:19:29 -040035 void transferFrom(const SkIRect& srcRect, GrColorType surfaceColorType,
36 GrColorType bufferColorType, GrGpuBuffer* transferBuffer,
37 size_t offset) override {
Brian Salomonab32f652019-05-10 14:24:50 -040038 fGpu->transferPixelsFrom(fTexture, srcRect.fLeft, srcRect.fTop, srcRect.width(),
Brian Salomonf77c1462019-08-01 15:19:29 -040039 srcRect.height(), surfaceColorType, bufferColorType,
40 transferBuffer, offset);
Brian Salomonab32f652019-05-10 14:24:50 -040041 }
Timothy Liange70604e2018-07-19 09:49:46 -040042 void insertEventMarker(const char* msg) override {}
43
44private:
Timothy Liange70604e2018-07-19 09:49:46 -040045 GrMtlGpu* fGpu;
46
47 typedef GrGpuTextureCommandBuffer INHERITED;
48};
49
Timothy Liang5422f9a2018-08-10 10:57:55 -040050class GrMtlGpuRTCommandBuffer : public GrGpuRTCommandBuffer, private GrMesh::SendToGpuImpl {
Timothy Liange70604e2018-07-19 09:49:46 -040051public:
52 GrMtlGpuRTCommandBuffer(GrMtlGpu* gpu, GrRenderTarget* rt, GrSurfaceOrigin origin,
Ethan Nicholas56d19a52018-10-15 11:26:20 -040053 const SkRect& bounds,
Timothy Liange70604e2018-07-19 09:49:46 -040054 const GrGpuRTCommandBuffer::LoadAndStoreInfo& colorInfo,
Timothy Liang5422f9a2018-08-10 10:57:55 -040055 const GrGpuRTCommandBuffer::StencilLoadAndStoreInfo& stencilInfo);
56
57 ~GrMtlGpuRTCommandBuffer() override;
Timothy Liange70604e2018-07-19 09:49:46 -040058
Timothy Liange70604e2018-07-19 09:49:46 -040059 void begin() override {}
60 void end() override {}
61
Timothy Liange70604e2018-07-19 09:49:46 -040062 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 Salomonf77c1462019-08-01 15:19:29 -040070 void transferFrom(const SkIRect& srcRect, GrColorType textureColorType,
71 GrColorType bufferColorType, GrGpuBuffer* transferBuffer,
72 size_t offset) override;
Greg Daniel46cfbc62019-06-07 11:43:30 -040073 void copy(GrSurface* src, const SkIRect& srcRect, const SkIPoint& dstPoint) override;
Timothy Liang5422f9a2018-08-10 10:57:55 -040074
75 void submit();
Timothy Liange70604e2018-07-19 09:49:46 -040076
Timothy Liange70604e2018-07-19 09:49:46 -040077private:
78 GrGpu* gpu() override { return fGpu; }
79
Timothy Liang5422f9a2018-08-10 10:57:55 -040080 GrMtlPipelineState* prepareDrawState(
81 const GrPrimitiveProcessor& primProc,
82 const GrPipeline& pipeline,
83 const GrPipeline::FixedDynamicState* fixedDynamicState,
Jim Van Verth1223e7f2019-02-28 17:38:35 -050084 GrPrimitiveType primType);
Timothy Liang5422f9a2018-08-10 10:57:55 -040085
Timothy Liange70604e2018-07-19 09:49:46 -040086 void onDraw(const GrPrimitiveProcessor& primProc,
87 const GrPipeline& pipeline,
88 const GrPipeline::FixedDynamicState* fixedDynamicState,
89 const GrPipeline::DynamicStateArrays* dynamicStateArrays,
90 const GrMesh mesh[],
91 int meshCount,
Timothy Liang5422f9a2018-08-10 10:57:55 -040092 const SkRect& bounds) override;
Timothy Liange70604e2018-07-19 09:49:46 -040093
Brian Osman9a9baae2018-11-05 15:06:26 -050094 void onClear(const GrFixedClip& clip, const SkPMColor4f& color) override;
Timothy Liange70604e2018-07-19 09:49:46 -040095
Ethan Nicholas01063512018-10-08 16:58:25 -040096 void onClearStencilClip(const GrFixedClip& clip, bool insideStencilMask) override;
Timothy Liange70604e2018-07-19 09:49:46 -040097
Jim Van Verthc159f942019-06-10 14:46:51 -040098 void setupRenderPass(const GrGpuRTCommandBuffer::LoadAndStoreInfo& colorInfo,
99 const GrGpuRTCommandBuffer::StencilLoadAndStoreInfo& stencilInfo);
Timothy Liang5422f9a2018-08-10 10:57:55 -0400100
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();
Jim Van Verthc159f942019-06-10 14:46:51 -0400126 void precreateCmdEncoder();
Jim Van Verth35a67eb2019-05-03 10:58:40 -0400127
Jim Van Verthc159f942019-06-10 14:46:51 -0400128 GrMtlGpu* fGpu;
Ethan Nicholas56d19a52018-10-15 11:26:20 -0400129 // GrRenderTargetProxy bounds
Ethan Nicholas391d3442018-10-15 13:00:59 -0400130#ifdef SK_DEBUG
Jim Van Verthc159f942019-06-10 14:46:51 -0400131 SkRect fRTBounds;
Ethan Nicholas391d3442018-10-15 13:00:59 -0400132#endif
Timothy Liange70604e2018-07-19 09:49:46 -0400133
Jim Van Verth61610be2019-03-07 15:33:49 -0500134 id<MTLRenderCommandEncoder> fActiveRenderCmdEncoder;
135 MTLRenderPassDescriptor* fRenderPassDesc;
Jim Van Verthc159f942019-06-10 14:46:51 -0400136 SkRect fBounds;
137 size_t fCurrentVertexStride;
Timothy Liang5422f9a2018-08-10 10:57:55 -0400138
Jim Van Verth35a67eb2019-05-03 10:58:40 -0400139 static constexpr size_t kNumBindings = GrMtlUniformHandler::kLastUniformBinding + 3;
Jim Van Verth6b6008a2019-06-18 08:38:32 -0400140 struct {
141 id<MTLBuffer> fBuffer;
142 size_t fOffset;
143 } fBufferBindings[kNumBindings];
Jim Van Verth35a67eb2019-05-03 10:58:40 -0400144
Timothy Liange70604e2018-07-19 09:49:46 -0400145 typedef GrGpuRTCommandBuffer INHERITED;
146};
147
148#endif
149