Timothy Liang | e70604e | 2018-07-19 09:49:46 -0400 | [diff] [blame] | 1 | /* |
| 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 Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 11 | #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 Liang | e70604e | 2018-07-19 09:49:46 -0400 | [diff] [blame] | 15 | |
Timothy Liang | 7ac582e | 2018-08-06 09:47:23 -0400 | [diff] [blame] | 16 | #import <metal/metal.h> |
| 17 | |
Timothy Liang | 5422f9a | 2018-08-10 10:57:55 -0400 | [diff] [blame] | 18 | typedef uint32_t GrColor; |
Jim Van Verth | 35a67eb | 2019-05-03 10:58:40 -0400 | [diff] [blame] | 19 | class GrMtlBuffer; |
Timothy Liang | 5422f9a | 2018-08-10 10:57:55 -0400 | [diff] [blame] | 20 | class GrMtlPipelineState; |
Timothy Liang | e70604e | 2018-07-19 09:49:46 -0400 | [diff] [blame] | 21 | class GrMtlRenderTarget; |
| 22 | |
| 23 | class GrMtlGpuTextureCommandBuffer : public GrGpuTextureCommandBuffer { |
| 24 | public: |
| 25 | GrMtlGpuTextureCommandBuffer(GrMtlGpu* gpu, GrTexture* texture, GrSurfaceOrigin origin) |
| 26 | : INHERITED(texture, origin) |
| 27 | , fGpu(gpu) { |
Timothy Liang | e70604e | 2018-07-19 09:49:46 -0400 | [diff] [blame] | 28 | } |
| 29 | |
| 30 | ~GrMtlGpuTextureCommandBuffer() override {} |
| 31 | |
Greg Daniel | 46cfbc6 | 2019-06-07 11:43:30 -0400 | [diff] [blame] | 32 | void copy(GrSurface* src, const SkIRect& srcRect, const SkIPoint& dstPoint) override { |
| 33 | fGpu->copySurface(fTexture, src, srcRect, dstPoint); |
Timothy Liang | e35055f | 2018-07-20 16:53:00 -0400 | [diff] [blame] | 34 | } |
Brian Salomon | ab32f65 | 2019-05-10 14:24:50 -0400 | [diff] [blame] | 35 | 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 Liang | e70604e | 2018-07-19 09:49:46 -0400 | [diff] [blame] | 40 | void insertEventMarker(const char* msg) override {} |
| 41 | |
| 42 | private: |
Timothy Liang | e70604e | 2018-07-19 09:49:46 -0400 | [diff] [blame] | 43 | GrMtlGpu* fGpu; |
| 44 | |
| 45 | typedef GrGpuTextureCommandBuffer INHERITED; |
| 46 | }; |
| 47 | |
Timothy Liang | 5422f9a | 2018-08-10 10:57:55 -0400 | [diff] [blame] | 48 | class GrMtlGpuRTCommandBuffer : public GrGpuRTCommandBuffer, private GrMesh::SendToGpuImpl { |
Timothy Liang | e70604e | 2018-07-19 09:49:46 -0400 | [diff] [blame] | 49 | public: |
| 50 | GrMtlGpuRTCommandBuffer(GrMtlGpu* gpu, GrRenderTarget* rt, GrSurfaceOrigin origin, |
Ethan Nicholas | 56d19a5 | 2018-10-15 11:26:20 -0400 | [diff] [blame] | 51 | const SkRect& bounds, |
Timothy Liang | e70604e | 2018-07-19 09:49:46 -0400 | [diff] [blame] | 52 | const GrGpuRTCommandBuffer::LoadAndStoreInfo& colorInfo, |
Timothy Liang | 5422f9a | 2018-08-10 10:57:55 -0400 | [diff] [blame] | 53 | const GrGpuRTCommandBuffer::StencilLoadAndStoreInfo& stencilInfo); |
| 54 | |
| 55 | ~GrMtlGpuRTCommandBuffer() override; |
Timothy Liang | e70604e | 2018-07-19 09:49:46 -0400 | [diff] [blame] | 56 | |
Timothy Liang | e70604e | 2018-07-19 09:49:46 -0400 | [diff] [blame] | 57 | void begin() override {} |
| 58 | void end() override {} |
| 59 | |
| 60 | void discard() override {} |
| 61 | |
| 62 | void insertEventMarker(const char* msg) override {} |
| 63 | |
Jim Van Verth | 6f8bfba | 2019-04-15 15:28:41 -0400 | [diff] [blame] | 64 | void inlineUpload(GrOpFlushState* state, GrDeferredTextureUploadFn& upload) override { |
| 65 | // TODO: this could be more efficient |
| 66 | state->doUpload(upload); |
| 67 | } |
Brian Salomon | ab32f65 | 2019-05-10 14:24:50 -0400 | [diff] [blame] | 68 | void transferFrom(const SkIRect& srcRect, GrColorType bufferColorType, |
| 69 | GrGpuBuffer* transferBuffer, size_t offset) override; |
Greg Daniel | 46cfbc6 | 2019-06-07 11:43:30 -0400 | [diff] [blame] | 70 | void copy(GrSurface* src, const SkIRect& srcRect, const SkIPoint& dstPoint) override; |
Timothy Liang | 5422f9a | 2018-08-10 10:57:55 -0400 | [diff] [blame] | 71 | |
| 72 | void submit(); |
Timothy Liang | e70604e | 2018-07-19 09:49:46 -0400 | [diff] [blame] | 73 | |
Timothy Liang | e70604e | 2018-07-19 09:49:46 -0400 | [diff] [blame] | 74 | private: |
| 75 | GrGpu* gpu() override { return fGpu; } |
| 76 | |
Timothy Liang | 5422f9a | 2018-08-10 10:57:55 -0400 | [diff] [blame] | 77 | GrMtlPipelineState* prepareDrawState( |
| 78 | const GrPrimitiveProcessor& primProc, |
| 79 | const GrPipeline& pipeline, |
| 80 | const GrPipeline::FixedDynamicState* fixedDynamicState, |
Jim Van Verth | 1223e7f | 2019-02-28 17:38:35 -0500 | [diff] [blame] | 81 | GrPrimitiveType primType); |
Timothy Liang | 5422f9a | 2018-08-10 10:57:55 -0400 | [diff] [blame] | 82 | |
Timothy Liang | e70604e | 2018-07-19 09:49:46 -0400 | [diff] [blame] | 83 | void onDraw(const GrPrimitiveProcessor& primProc, |
| 84 | const GrPipeline& pipeline, |
| 85 | const GrPipeline::FixedDynamicState* fixedDynamicState, |
| 86 | const GrPipeline::DynamicStateArrays* dynamicStateArrays, |
| 87 | const GrMesh mesh[], |
| 88 | int meshCount, |
Timothy Liang | 5422f9a | 2018-08-10 10:57:55 -0400 | [diff] [blame] | 89 | const SkRect& bounds) override; |
Timothy Liang | e70604e | 2018-07-19 09:49:46 -0400 | [diff] [blame] | 90 | |
Brian Osman | 9a9baae | 2018-11-05 15:06:26 -0500 | [diff] [blame] | 91 | void onClear(const GrFixedClip& clip, const SkPMColor4f& color) override; |
Timothy Liang | e70604e | 2018-07-19 09:49:46 -0400 | [diff] [blame] | 92 | |
Ethan Nicholas | 0106351 | 2018-10-08 16:58:25 -0400 | [diff] [blame] | 93 | void onClearStencilClip(const GrFixedClip& clip, bool insideStencilMask) override; |
Timothy Liang | e70604e | 2018-07-19 09:49:46 -0400 | [diff] [blame] | 94 | |
Jim Van Verth | c159f94 | 2019-06-10 14:46:51 -0400 | [diff] [blame^] | 95 | void setupRenderPass(const GrGpuRTCommandBuffer::LoadAndStoreInfo& colorInfo, |
| 96 | const GrGpuRTCommandBuffer::StencilLoadAndStoreInfo& stencilInfo); |
Timothy Liang | 5422f9a | 2018-08-10 10:57:55 -0400 | [diff] [blame] | 97 | |
Jim Van Verth | 03464f1 | 2019-06-03 11:09:40 -0400 | [diff] [blame] | 98 | void bindGeometry(const GrBuffer* vertexBuffer, size_t vertexOffset, |
| 99 | const GrBuffer* instanceBuffer); |
Timothy Liang | 5422f9a | 2018-08-10 10:57:55 -0400 | [diff] [blame] | 100 | |
| 101 | // GrMesh::SendToGpuImpl methods. These issue the actual Metal draw commands. |
| 102 | // Marked final as a hint to the compiler to not use virtual dispatch. |
| 103 | void sendMeshToGpu(GrPrimitiveType primType, const GrBuffer* vertexBuffer, int vertexCount, |
Jim Van Verth | 03464f1 | 2019-06-03 11:09:40 -0400 | [diff] [blame] | 104 | int baseVertex) final; |
Timothy Liang | 5422f9a | 2018-08-10 10:57:55 -0400 | [diff] [blame] | 105 | |
| 106 | void sendIndexedMeshToGpu(GrPrimitiveType primType, const GrBuffer* indexBuffer, int indexCount, |
| 107 | int baseIndex, uint16_t /*minIndexValue*/, uint16_t /*maxIndexValue*/, |
| 108 | const GrBuffer* vertexBuffer, int baseVertex, |
Jim Van Verth | 03464f1 | 2019-06-03 11:09:40 -0400 | [diff] [blame] | 109 | GrPrimitiveRestart restart) final; |
Timothy Liang | 5422f9a | 2018-08-10 10:57:55 -0400 | [diff] [blame] | 110 | |
| 111 | void sendInstancedMeshToGpu(GrPrimitiveType, const GrBuffer* vertexBuffer, int vertexCount, |
| 112 | int baseVertex, const GrBuffer* instanceBuffer, int instanceCount, |
| 113 | int baseInstance) final; |
| 114 | |
| 115 | void sendIndexedInstancedMeshToGpu(GrPrimitiveType, const GrBuffer* indexBuffer, int indexCount, |
| 116 | int baseIndex, const GrBuffer* vertexBuffer, int baseVertex, |
| 117 | const GrBuffer* instanceBuffer, int instanceCount, |
| 118 | int baseInstance, GrPrimitiveRestart) final; |
| 119 | |
Jim Van Verth | 03464f1 | 2019-06-03 11:09:40 -0400 | [diff] [blame] | 120 | void setVertexBuffer(id<MTLRenderCommandEncoder>, const GrMtlBuffer*, size_t offset, |
| 121 | size_t index); |
Jim Van Verth | 35a67eb | 2019-05-03 10:58:40 -0400 | [diff] [blame] | 122 | void resetBufferBindings(); |
Jim Van Verth | c159f94 | 2019-06-10 14:46:51 -0400 | [diff] [blame^] | 123 | void precreateCmdEncoder(); |
Jim Van Verth | 35a67eb | 2019-05-03 10:58:40 -0400 | [diff] [blame] | 124 | |
Jim Van Verth | c159f94 | 2019-06-10 14:46:51 -0400 | [diff] [blame^] | 125 | GrMtlGpu* fGpu; |
Ethan Nicholas | 56d19a5 | 2018-10-15 11:26:20 -0400 | [diff] [blame] | 126 | // GrRenderTargetProxy bounds |
Ethan Nicholas | 391d344 | 2018-10-15 13:00:59 -0400 | [diff] [blame] | 127 | #ifdef SK_DEBUG |
Jim Van Verth | c159f94 | 2019-06-10 14:46:51 -0400 | [diff] [blame^] | 128 | SkRect fRTBounds; |
Ethan Nicholas | 391d344 | 2018-10-15 13:00:59 -0400 | [diff] [blame] | 129 | #endif |
Timothy Liang | e70604e | 2018-07-19 09:49:46 -0400 | [diff] [blame] | 130 | |
Jim Van Verth | 61610be | 2019-03-07 15:33:49 -0500 | [diff] [blame] | 131 | id<MTLRenderCommandEncoder> fActiveRenderCmdEncoder; |
| 132 | MTLRenderPassDescriptor* fRenderPassDesc; |
Jim Van Verth | c159f94 | 2019-06-10 14:46:51 -0400 | [diff] [blame^] | 133 | SkRect fBounds; |
| 134 | size_t fCurrentVertexStride; |
Timothy Liang | 5422f9a | 2018-08-10 10:57:55 -0400 | [diff] [blame] | 135 | |
Jim Van Verth | 35a67eb | 2019-05-03 10:58:40 -0400 | [diff] [blame] | 136 | static constexpr size_t kNumBindings = GrMtlUniformHandler::kLastUniformBinding + 3; |
| 137 | id<MTLBuffer> fBufferBindings[kNumBindings]; |
| 138 | |
Timothy Liang | e70604e | 2018-07-19 09:49:46 -0400 | [diff] [blame] | 139 | typedef GrGpuRTCommandBuffer INHERITED; |
| 140 | }; |
| 141 | |
| 142 | #endif |
| 143 | |