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 | |
| 32 | void copy(GrSurface* src, GrSurfaceOrigin srcOrigin, const SkIRect& srcRect, |
Timothy Liang | e35055f | 2018-07-20 16:53:00 -0400 | [diff] [blame] | 33 | const SkIPoint& dstPoint) override { |
| 34 | fGpu->copySurface(fTexture, fOrigin, src, srcOrigin, srcRect, dstPoint); |
| 35 | } |
Brian Salomon | ab32f65 | 2019-05-10 14:24:50 -0400 | [diff] [blame] | 36 | 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 Liang | e70604e | 2018-07-19 09:49:46 -0400 | [diff] [blame] | 41 | void insertEventMarker(const char* msg) override {} |
| 42 | |
| 43 | private: |
Timothy Liang | e70604e | 2018-07-19 09:49:46 -0400 | [diff] [blame] | 44 | GrMtlGpu* fGpu; |
| 45 | |
| 46 | typedef GrGpuTextureCommandBuffer INHERITED; |
| 47 | }; |
| 48 | |
Timothy Liang | 5422f9a | 2018-08-10 10:57:55 -0400 | [diff] [blame] | 49 | class GrMtlGpuRTCommandBuffer : public GrGpuRTCommandBuffer, private GrMesh::SendToGpuImpl { |
Timothy Liang | e70604e | 2018-07-19 09:49:46 -0400 | [diff] [blame] | 50 | public: |
| 51 | GrMtlGpuRTCommandBuffer(GrMtlGpu* gpu, GrRenderTarget* rt, GrSurfaceOrigin origin, |
Ethan Nicholas | 56d19a5 | 2018-10-15 11:26:20 -0400 | [diff] [blame] | 52 | const SkRect& bounds, |
Timothy Liang | e70604e | 2018-07-19 09:49:46 -0400 | [diff] [blame] | 53 | const GrGpuRTCommandBuffer::LoadAndStoreInfo& colorInfo, |
Timothy Liang | 5422f9a | 2018-08-10 10:57:55 -0400 | [diff] [blame] | 54 | const GrGpuRTCommandBuffer::StencilLoadAndStoreInfo& stencilInfo); |
| 55 | |
| 56 | ~GrMtlGpuRTCommandBuffer() override; |
Timothy Liang | e70604e | 2018-07-19 09:49:46 -0400 | [diff] [blame] | 57 | |
Timothy Liang | e70604e | 2018-07-19 09:49:46 -0400 | [diff] [blame] | 58 | void begin() override {} |
| 59 | void end() override {} |
| 60 | |
| 61 | void discard() override {} |
| 62 | |
| 63 | void insertEventMarker(const char* msg) override {} |
| 64 | |
Jim Van Verth | 6f8bfba | 2019-04-15 15:28:41 -0400 | [diff] [blame] | 65 | void inlineUpload(GrOpFlushState* state, GrDeferredTextureUploadFn& upload) override { |
| 66 | // TODO: this could be more efficient |
| 67 | state->doUpload(upload); |
| 68 | } |
Brian Salomon | ab32f65 | 2019-05-10 14:24:50 -0400 | [diff] [blame] | 69 | void transferFrom(const SkIRect& srcRect, GrColorType bufferColorType, |
| 70 | GrGpuBuffer* transferBuffer, size_t offset) override; |
Timothy Liang | e70604e | 2018-07-19 09:49:46 -0400 | [diff] [blame] | 71 | void copy(GrSurface* src, GrSurfaceOrigin srcOrigin, const SkIRect& srcRect, |
Timothy Liang | 5422f9a | 2018-08-10 10:57:55 -0400 | [diff] [blame] | 72 | const SkIPoint& dstPoint) override; |
| 73 | |
| 74 | void submit(); |
Timothy Liang | e70604e | 2018-07-19 09:49:46 -0400 | [diff] [blame] | 75 | |
Timothy Liang | e70604e | 2018-07-19 09:49:46 -0400 | [diff] [blame] | 76 | private: |
Jim Van Verth | ae336bd | 2019-03-06 13:50:56 -0500 | [diff] [blame] | 77 | void addNullCommand(); |
Timothy Liang | 5422f9a | 2018-08-10 10:57:55 -0400 | [diff] [blame] | 78 | |
Timothy Liang | e70604e | 2018-07-19 09:49:46 -0400 | [diff] [blame] | 79 | GrGpu* gpu() override { return fGpu; } |
| 80 | |
Timothy Liang | 5422f9a | 2018-08-10 10:57:55 -0400 | [diff] [blame] | 81 | GrMtlPipelineState* prepareDrawState( |
| 82 | const GrPrimitiveProcessor& primProc, |
| 83 | const GrPipeline& pipeline, |
| 84 | const GrPipeline::FixedDynamicState* fixedDynamicState, |
Jim Van Verth | 1223e7f | 2019-02-28 17:38:35 -0500 | [diff] [blame] | 85 | GrPrimitiveType primType); |
Timothy Liang | 5422f9a | 2018-08-10 10:57:55 -0400 | [diff] [blame] | 86 | |
Timothy Liang | e70604e | 2018-07-19 09:49:46 -0400 | [diff] [blame] | 87 | 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 Liang | 5422f9a | 2018-08-10 10:57:55 -0400 | [diff] [blame] | 93 | const SkRect& bounds) override; |
Timothy Liang | e70604e | 2018-07-19 09:49:46 -0400 | [diff] [blame] | 94 | |
Brian Osman | 9a9baae | 2018-11-05 15:06:26 -0500 | [diff] [blame] | 95 | void onClear(const GrFixedClip& clip, const SkPMColor4f& color) override; |
Timothy Liang | e70604e | 2018-07-19 09:49:46 -0400 | [diff] [blame] | 96 | |
Ethan Nicholas | 0106351 | 2018-10-08 16:58:25 -0400 | [diff] [blame] | 97 | void onClearStencilClip(const GrFixedClip& clip, bool insideStencilMask) override; |
Timothy Liang | e70604e | 2018-07-19 09:49:46 -0400 | [diff] [blame] | 98 | |
Timothy Liang | 5422f9a | 2018-08-10 10:57:55 -0400 | [diff] [blame] | 99 | MTLRenderPassDescriptor* createRenderPassDesc() const; |
| 100 | |
Jim Van Verth | 03464f1 | 2019-06-03 11:09:40 -0400 | [diff] [blame^] | 101 | void bindGeometry(const GrBuffer* vertexBuffer, size_t vertexOffset, |
| 102 | const GrBuffer* instanceBuffer); |
Timothy Liang | 5422f9a | 2018-08-10 10:57:55 -0400 | [diff] [blame] | 103 | |
| 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 Verth | 03464f1 | 2019-06-03 11:09:40 -0400 | [diff] [blame^] | 107 | int baseVertex) final; |
Timothy Liang | 5422f9a | 2018-08-10 10:57:55 -0400 | [diff] [blame] | 108 | |
| 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 Verth | 03464f1 | 2019-06-03 11:09:40 -0400 | [diff] [blame^] | 112 | GrPrimitiveRestart restart) final; |
Timothy Liang | 5422f9a | 2018-08-10 10:57:55 -0400 | [diff] [blame] | 113 | |
| 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 Verth | 03464f1 | 2019-06-03 11:09:40 -0400 | [diff] [blame^] | 123 | void setVertexBuffer(id<MTLRenderCommandEncoder>, const GrMtlBuffer*, size_t offset, |
| 124 | size_t index); |
Jim Van Verth | 35a67eb | 2019-05-03 10:58:40 -0400 | [diff] [blame] | 125 | void resetBufferBindings(); |
| 126 | |
Timothy Liang | e70604e | 2018-07-19 09:49:46 -0400 | [diff] [blame] | 127 | GrMtlGpu* fGpu; |
Ethan Nicholas | 56d19a5 | 2018-10-15 11:26:20 -0400 | [diff] [blame] | 128 | // GrRenderTargetProxy bounds |
Ethan Nicholas | 391d344 | 2018-10-15 13:00:59 -0400 | [diff] [blame] | 129 | #ifdef SK_DEBUG |
Ethan Nicholas | 56d19a5 | 2018-10-15 11:26:20 -0400 | [diff] [blame] | 130 | SkRect fBounds; |
Ethan Nicholas | 391d344 | 2018-10-15 13:00:59 -0400 | [diff] [blame] | 131 | #endif |
Timothy Liang | e70604e | 2018-07-19 09:49:46 -0400 | [diff] [blame] | 132 | GrGpuRTCommandBuffer::LoadAndStoreInfo fColorLoadAndStoreInfo; |
| 133 | GrGpuRTCommandBuffer::StencilLoadAndStoreInfo fStencilLoadAndStoreInfo; |
| 134 | |
Jim Van Verth | 61610be | 2019-03-07 15:33:49 -0500 | [diff] [blame] | 135 | id<MTLRenderCommandEncoder> fActiveRenderCmdEncoder; |
| 136 | MTLRenderPassDescriptor* fRenderPassDesc; |
Timothy Liang | 5422f9a | 2018-08-10 10:57:55 -0400 | [diff] [blame] | 137 | |
| 138 | struct CommandBufferInfo { |
| 139 | SkRect fBounds; |
Jim Van Verth | 03464f1 | 2019-06-03 11:09:40 -0400 | [diff] [blame^] | 140 | size_t fCurrentVertexStride; |
Timothy Liang | 5422f9a | 2018-08-10 10:57:55 -0400 | [diff] [blame] | 141 | }; |
| 142 | |
| 143 | CommandBufferInfo fCommandBufferInfo; |
| 144 | |
Jim Van Verth | 35a67eb | 2019-05-03 10:58:40 -0400 | [diff] [blame] | 145 | static constexpr size_t kNumBindings = GrMtlUniformHandler::kLastUniformBinding + 3; |
| 146 | id<MTLBuffer> fBufferBindings[kNumBindings]; |
| 147 | |
Timothy Liang | e70604e | 2018-07-19 09:49:46 -0400 | [diff] [blame] | 148 | typedef GrGpuRTCommandBuffer INHERITED; |
| 149 | }; |
| 150 | |
| 151 | #endif |
| 152 | |