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 11 #include "GrGpuCommandBuffer.h" 12 #include "GrMtlGpu.h" 13 #include "GrMesh.h" 14 15 #import <metal/metal.h> 16 17 typedef uint32_t GrColor; 18 class GrMtlPipelineState; 19 class GrMtlRenderTarget; 20 21 class GrMtlGpuTextureCommandBuffer : public GrGpuTextureCommandBuffer { 22 public: GrMtlGpuTextureCommandBuffer(GrMtlGpu * gpu,GrTexture * texture,GrSurfaceOrigin origin)23 GrMtlGpuTextureCommandBuffer(GrMtlGpu* gpu, GrTexture* texture, GrSurfaceOrigin origin) 24 : INHERITED(texture, origin) 25 , fGpu(gpu) { 26 } 27 ~GrMtlGpuTextureCommandBuffer()28 ~GrMtlGpuTextureCommandBuffer() override {} 29 copy(GrSurface * src,GrSurfaceOrigin srcOrigin,const SkIRect & srcRect,const SkIPoint & dstPoint)30 void copy(GrSurface* src, GrSurfaceOrigin srcOrigin, const SkIRect& srcRect, 31 const SkIPoint& dstPoint) override { 32 fGpu->copySurface(fTexture, fOrigin, src, srcOrigin, srcRect, dstPoint); 33 } 34 insertEventMarker(const char * msg)35 void insertEventMarker(const char* msg) override {} 36 37 private: 38 GrMtlGpu* fGpu; 39 40 typedef GrGpuTextureCommandBuffer INHERITED; 41 }; 42 43 class GrMtlGpuRTCommandBuffer : public GrGpuRTCommandBuffer, private GrMesh::SendToGpuImpl { 44 public: 45 GrMtlGpuRTCommandBuffer(GrMtlGpu* gpu, GrRenderTarget* rt, GrSurfaceOrigin origin, 46 const SkRect& bounds, 47 const GrGpuRTCommandBuffer::LoadAndStoreInfo& colorInfo, 48 const GrGpuRTCommandBuffer::StencilLoadAndStoreInfo& stencilInfo); 49 50 ~GrMtlGpuRTCommandBuffer() override; 51 begin()52 void begin() override {} end()53 void end() override {} 54 discard()55 void discard() override {} 56 insertEventMarker(const char * msg)57 void insertEventMarker(const char* msg) override {} 58 inlineUpload(GrOpFlushState * state,GrDeferredTextureUploadFn & upload)59 void inlineUpload(GrOpFlushState* state, GrDeferredTextureUploadFn& upload) override {} 60 61 void copy(GrSurface* src, GrSurfaceOrigin srcOrigin, const SkIRect& srcRect, 62 const SkIPoint& dstPoint) override; 63 64 void submit(); 65 66 private: 67 void addNullCommand(); 68 gpu()69 GrGpu* gpu() override { return fGpu; } 70 71 GrMtlPipelineState* prepareDrawState( 72 const GrPrimitiveProcessor& primProc, 73 const GrPipeline& pipeline, 74 const GrPipeline::FixedDynamicState* fixedDynamicState, 75 GrPrimitiveType primType); 76 77 void onDraw(const GrPrimitiveProcessor& primProc, 78 const GrPipeline& pipeline, 79 const GrPipeline::FixedDynamicState* fixedDynamicState, 80 const GrPipeline::DynamicStateArrays* dynamicStateArrays, 81 const GrMesh mesh[], 82 int meshCount, 83 const SkRect& bounds) override; 84 85 void onClear(const GrFixedClip& clip, const SkPMColor4f& color) override; 86 87 void onClearStencilClip(const GrFixedClip& clip, bool insideStencilMask) override; 88 89 MTLRenderPassDescriptor* createRenderPassDesc() const; 90 91 void bindGeometry(const GrBuffer* vertexBuffer, const GrBuffer* instanceBuffer); 92 93 // GrMesh::SendToGpuImpl methods. These issue the actual Metal draw commands. 94 // Marked final as a hint to the compiler to not use virtual dispatch. sendMeshToGpu(GrPrimitiveType primType,const GrBuffer * vertexBuffer,int vertexCount,int baseVertex)95 void sendMeshToGpu(GrPrimitiveType primType, const GrBuffer* vertexBuffer, int vertexCount, 96 int baseVertex) final { 97 this->sendInstancedMeshToGpu(primType, vertexBuffer, vertexCount, baseVertex, nullptr, 1, 98 0); 99 } 100 sendIndexedMeshToGpu(GrPrimitiveType primType,const GrBuffer * indexBuffer,int indexCount,int baseIndex,uint16_t,uint16_t,const GrBuffer * vertexBuffer,int baseVertex,GrPrimitiveRestart restart)101 void sendIndexedMeshToGpu(GrPrimitiveType primType, const GrBuffer* indexBuffer, int indexCount, 102 int baseIndex, uint16_t /*minIndexValue*/, uint16_t /*maxIndexValue*/, 103 const GrBuffer* vertexBuffer, int baseVertex, 104 GrPrimitiveRestart restart) final { 105 SkASSERT(restart == GrPrimitiveRestart::kNo); 106 this->sendIndexedInstancedMeshToGpu(primType, indexBuffer, indexCount, baseIndex, 107 vertexBuffer, baseVertex, nullptr, 1, 0, 108 GrPrimitiveRestart::kNo); 109 } 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 120 GrMtlGpu* fGpu; 121 // GrRenderTargetProxy bounds 122 #ifdef SK_DEBUG 123 SkRect fBounds; 124 #endif 125 GrGpuRTCommandBuffer::LoadAndStoreInfo fColorLoadAndStoreInfo; 126 GrGpuRTCommandBuffer::StencilLoadAndStoreInfo fStencilLoadAndStoreInfo; 127 128 id<MTLRenderCommandEncoder> fActiveRenderCmdEncoder; 129 MTLRenderPassDescriptor* fRenderPassDesc; 130 131 struct CommandBufferInfo { 132 SkRect fBounds; 133 }; 134 135 CommandBufferInfo fCommandBufferInfo; 136 137 typedef GrGpuRTCommandBuffer INHERITED; 138 }; 139 140 #endif 141 142