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