1 /* 2 * Copyright 2016 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 GrVkGpuCommandBuffer_DEFINED 9 #define GrVkGpuCommandBuffer_DEFINED 10 11 #include "GrGpuCommandBuffer.h" 12 13 #include "GrColor.h" 14 #include "GrMesh.h" 15 #include "GrTypes.h" 16 #include "GrVkPipelineState.h" 17 #include "vk/GrVkTypes.h" 18 19 class GrVkGpu; 20 class GrVkImage; 21 class GrVkRenderPass; 22 class GrVkRenderTarget; 23 class GrVkSecondaryCommandBuffer; 24 25 class GrVkGpuTextureCommandBuffer : public GrGpuTextureCommandBuffer { 26 public: GrVkGpuTextureCommandBuffer(GrVkGpu * gpu)27 GrVkGpuTextureCommandBuffer(GrVkGpu* gpu) : fGpu(gpu) {} 28 29 ~GrVkGpuTextureCommandBuffer() override; 30 31 void copy(GrSurface* src, GrSurfaceOrigin srcOrigin, const SkIRect& srcRect, 32 const SkIPoint& dstPoint) override; 33 34 void insertEventMarker(const char*) override; 35 reset()36 void reset() { 37 fCopies.reset(); 38 fTexture = nullptr; 39 } 40 41 void submit(); 42 43 private: 44 struct CopyInfo { CopyInfoCopyInfo45 CopyInfo(GrSurface* src, GrSurfaceOrigin srcOrigin, const SkIRect& srcRect, 46 const SkIPoint& dstPoint) 47 : fSrc(sk_ref_sp(src)), fSrcOrigin(srcOrigin), fSrcRect(srcRect), fDstPoint(dstPoint) {} 48 49 sk_sp<GrSurface> fSrc; 50 GrSurfaceOrigin fSrcOrigin; 51 SkIRect fSrcRect; 52 SkIPoint fDstPoint; 53 }; 54 55 GrVkGpu* fGpu; 56 SkTArray<CopyInfo> fCopies; 57 58 typedef GrGpuTextureCommandBuffer INHERITED; 59 }; 60 61 class GrVkGpuRTCommandBuffer : public GrGpuRTCommandBuffer, private GrMesh::SendToGpuImpl { 62 public: 63 GrVkGpuRTCommandBuffer(GrVkGpu*); 64 65 ~GrVkGpuRTCommandBuffer() override; 66 begin()67 void begin() override { } 68 void end() override; 69 70 void discard() override; 71 void insertEventMarker(const char*) override; 72 73 void inlineUpload(GrOpFlushState* state, GrDeferredTextureUploadFn& upload) override; 74 75 void copy(GrSurface* src, GrSurfaceOrigin srcOrigin, const SkIRect& srcRect, 76 const SkIPoint& dstPoint) override; 77 78 void executeDrawable(std::unique_ptr<SkDrawable::GpuDrawHandler>) override; 79 80 void set(GrRenderTarget*, GrSurfaceOrigin, 81 const GrGpuRTCommandBuffer::LoadAndStoreInfo&, 82 const GrGpuRTCommandBuffer::StencilLoadAndStoreInfo&); 83 void reset(); 84 85 void submit(); 86 87 private: 88 void init(); 89 90 // Called instead of init when we are drawing to a render target that already wraps a secondary 91 // command buffer. 92 void initWrapped(); 93 94 bool wrapsSecondaryCommandBuffer() const; 95 96 GrGpu* gpu() override; 97 98 // Bind vertex and index buffers 99 void bindGeometry(const GrGpuBuffer* indexBuffer, 100 const GrGpuBuffer* vertexBuffer, 101 const GrGpuBuffer* instanceBuffer); 102 103 GrVkPipelineState* prepareDrawState(const GrPrimitiveProcessor&, 104 const GrPipeline&, 105 const GrPipeline::FixedDynamicState*, 106 const GrPipeline::DynamicStateArrays*, 107 GrPrimitiveType); 108 109 void onDraw(const GrPrimitiveProcessor&, 110 const GrPipeline&, 111 const GrPipeline::FixedDynamicState*, 112 const GrPipeline::DynamicStateArrays*, 113 const GrMesh[], 114 int meshCount, 115 const SkRect& bounds) override; 116 117 // GrMesh::SendToGpuImpl methods. These issue the actual Vulkan draw commands. 118 // Marked final as a hint to the compiler to not use virtual dispatch. sendMeshToGpu(GrPrimitiveType primType,const GrBuffer * vertexBuffer,int vertexCount,int baseVertex)119 void sendMeshToGpu(GrPrimitiveType primType, const GrBuffer* vertexBuffer, int vertexCount, 120 int baseVertex) final { 121 this->sendInstancedMeshToGpu(primType, vertexBuffer, vertexCount, baseVertex, nullptr, 1, 122 0); 123 } 124 sendIndexedMeshToGpu(GrPrimitiveType primType,const GrBuffer * indexBuffer,int indexCount,int baseIndex,uint16_t,uint16_t,const GrBuffer * vertexBuffer,int baseVertex,GrPrimitiveRestart restart)125 void sendIndexedMeshToGpu(GrPrimitiveType primType, const GrBuffer* indexBuffer, int indexCount, 126 int baseIndex, uint16_t /*minIndexValue*/, uint16_t /*maxIndexValue*/, 127 const GrBuffer* vertexBuffer, int baseVertex, 128 GrPrimitiveRestart restart) final { 129 SkASSERT(restart == GrPrimitiveRestart::kNo); 130 this->sendIndexedInstancedMeshToGpu(primType, indexBuffer, indexCount, baseIndex, 131 vertexBuffer, baseVertex, nullptr, 1, 0, 132 GrPrimitiveRestart::kNo); 133 } 134 135 void sendInstancedMeshToGpu(GrPrimitiveType, const GrBuffer* vertexBuffer, int vertexCount, 136 int baseVertex, const GrBuffer* instanceBuffer, int instanceCount, 137 int baseInstance) final; 138 139 void sendIndexedInstancedMeshToGpu(GrPrimitiveType, const GrBuffer* indexBuffer, int indexCount, 140 int baseIndex, const GrBuffer* vertexBuffer, int baseVertex, 141 const GrBuffer* instanceBuffer, int instanceCount, 142 int baseInstance, GrPrimitiveRestart) final; 143 144 void onClear(const GrFixedClip&, const SkPMColor4f& color) override; 145 146 void onClearStencilClip(const GrFixedClip&, bool insideStencilMask) override; 147 148 void addAdditionalCommandBuffer(); 149 void addAdditionalRenderPass(); 150 151 struct InlineUploadInfo { InlineUploadInfoInlineUploadInfo152 InlineUploadInfo(GrOpFlushState* state, const GrDeferredTextureUploadFn& upload) 153 : fFlushState(state), fUpload(upload) {} 154 155 GrOpFlushState* fFlushState; 156 GrDeferredTextureUploadFn fUpload; 157 }; 158 159 struct CopyInfo { CopyInfoCopyInfo160 CopyInfo(GrSurface* src, GrSurfaceOrigin srcOrigin, const SkIRect& srcRect, 161 const SkIPoint& dstPoint, bool shouldDiscardDst) 162 : fSrc(sk_ref_sp(src)) 163 , fSrcOrigin(srcOrigin) 164 , fSrcRect(srcRect) 165 , fDstPoint(dstPoint) 166 , fShouldDiscardDst(shouldDiscardDst) {} 167 168 sk_sp<GrSurface> fSrc; 169 GrSurfaceOrigin fSrcOrigin; 170 SkIRect fSrcRect; 171 SkIPoint fDstPoint; 172 bool fShouldDiscardDst; 173 }; 174 175 enum class LoadStoreState { 176 kUnknown, 177 kStartsWithClear, 178 kStartsWithDiscard, 179 kLoadAndStore, 180 }; 181 182 struct CommandBufferInfo { 183 const GrVkRenderPass* fRenderPass; 184 SkTArray<GrVkSecondaryCommandBuffer*> fCommandBuffers; 185 VkClearValue fColorClearValue; 186 SkRect fBounds; 187 bool fIsEmpty = true; 188 LoadStoreState fLoadStoreState = LoadStoreState::kUnknown; 189 // The PreDrawUploads and PreCopies are sent to the GPU before submitting the secondary 190 // command buffer. 191 SkTArray<InlineUploadInfo> fPreDrawUploads; 192 SkTArray<CopyInfo> fPreCopies; 193 // Array of images that will be sampled and thus need to be transfered to sampled layout 194 // before submitting the secondary command buffers. This must happen after we do any predraw 195 // uploads or copies. 196 SkTArray<sk_sp<GrVkTexture>> fSampledTextures; 197 currentCmdBufCommandBufferInfo198 GrVkSecondaryCommandBuffer* currentCmdBuf() { 199 return fCommandBuffers.back(); 200 } 201 }; 202 203 SkTArray<CommandBufferInfo> fCommandBufferInfos; 204 int fCurrentCmdInfo; 205 206 GrVkGpu* fGpu; 207 VkAttachmentLoadOp fVkColorLoadOp; 208 VkAttachmentStoreOp fVkColorStoreOp; 209 VkAttachmentLoadOp fVkStencilLoadOp; 210 VkAttachmentStoreOp fVkStencilStoreOp; 211 SkPMColor4f fClearColor; 212 GrVkPipelineState* fLastPipelineState; 213 214 typedef GrGpuRTCommandBuffer INHERITED; 215 }; 216 217 #endif 218