1 /* 2 * Copyright 2022 Google LLC 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 skgpu_graphite_DawnCommandBuffer_DEFINED 9 #define skgpu_graphite_DawnCommandBuffer_DEFINED 10 11 #include "src/gpu/graphite/CommandBuffer.h" 12 #include "src/gpu/graphite/DrawPass.h" 13 #include "src/gpu/graphite/GpuWorkSubmission.h" 14 #include "src/gpu/graphite/Log.h" 15 #include "src/gpu/graphite/compute/DispatchGroup.h" 16 #include "src/gpu/graphite/dawn/DawnGraphicsPipeline.h" 17 18 #include "webgpu/webgpu_cpp.h" // NO_G3_REWRITE 19 20 namespace skgpu::graphite { 21 class ComputePipeline; 22 class DawnBuffer; 23 class DawnComputePipeline; 24 class DawnQueueManager; 25 class DawnResourceProvider; 26 class DawnSharedContext; 27 class DawnTexture; 28 class DispatchGroup; 29 struct WorkgroupSize; 30 31 class DawnCommandBuffer final : public CommandBuffer { 32 public: 33 static std::unique_ptr<DawnCommandBuffer> Make(const DawnSharedContext*, DawnResourceProvider*); 34 ~DawnCommandBuffer() override; 35 36 wgpu::CommandBuffer finishEncoding(); 37 38 private: 39 DawnCommandBuffer(const DawnSharedContext* sharedContext, 40 DawnResourceProvider* resourceProvider); 41 42 void onResetCommandBuffer() override; 43 bool setNewCommandBufferResources() override; 44 45 bool onAddRenderPass(const RenderPassDesc&, 46 const Texture* colorTexture, 47 const Texture* resolveTexture, 48 const Texture* depthStencilTexture, 49 SkRect viewport, 50 const DrawPassList&) override; 51 bool onAddComputePass(DispatchGroupSpan) override; 52 53 // Methods for populating a Dawn RenderPassEncoder: 54 bool beginRenderPass(const RenderPassDesc&, 55 const Texture* colorTexture, 56 const Texture* resolveTexture, 57 const Texture* depthStencilTexture); 58 bool loadMSAAFromResolveAndBeginRenderPassEncoder( 59 const RenderPassDesc& frontendRenderPassDesc, 60 const wgpu::RenderPassDescriptor& wgpuRenderPassDesc, 61 const DawnTexture* msaaTexture); 62 bool doBlitWithDraw(const wgpu::RenderPassEncoder& renderEncoder, 63 const RenderPassDesc& frontendRenderPassDesc, 64 const wgpu::TextureView& sourceTextureView, 65 int width, 66 int height); 67 void endRenderPass(); 68 69 bool addDrawPass(const DrawPass*); 70 71 bool bindGraphicsPipeline(const GraphicsPipeline*); 72 void setBlendConstants(float* blendConstants); 73 74 void bindUniformBuffer(const BindUniformBufferInfo& info, UniformSlot); 75 void bindDrawBuffers(const BindBufferInfo& vertices, 76 const BindBufferInfo& instances, 77 const BindBufferInfo& indices, 78 const BindBufferInfo& indirect); 79 80 void bindTextureAndSamplers(const DrawPass& drawPass, 81 const DrawPassCommands::BindTexturesAndSamplers& command); 82 83 void setScissor(unsigned int left, unsigned int top, unsigned int width, unsigned int height); 84 void preprocessViewport(const SkRect& viewport); 85 void setViewport(const SkRect& viewport); 86 87 void draw(PrimitiveType type, unsigned int baseVertex, unsigned int vertexCount); 88 void drawIndexed(PrimitiveType type, 89 unsigned int baseIndex, 90 unsigned int indexCount, 91 unsigned int baseVertex); 92 void drawInstanced(PrimitiveType type, 93 unsigned int baseVertex, 94 unsigned int vertexCount, 95 unsigned int baseInstance, 96 unsigned int instanceCount); 97 void drawIndexedInstanced(PrimitiveType type, 98 unsigned int baseIndex, 99 unsigned int indexCount, 100 unsigned int baseVertex, 101 unsigned int baseInstance, 102 unsigned int instanceCount); 103 void drawIndirect(PrimitiveType type); 104 void drawIndexedIndirect(PrimitiveType type); 105 106 // Methods for populating a Dawn ComputePassEncoder: 107 void beginComputePass(); 108 void bindComputePipeline(const ComputePipeline*); 109 void bindDispatchResources(const DispatchGroup&, const DispatchGroup::Dispatch&); 110 void dispatchWorkgroups(const WorkgroupSize& globalSize); 111 void dispatchWorkgroupsIndirect(const Buffer* indirectBuffer, size_t indirectBufferOffset); 112 void endComputePass(); 113 114 // Methods for doing texture/buffer to texture/buffer copying: 115 bool onCopyBufferToBuffer(const Buffer* srcBuffer, 116 size_t srcOffset, 117 const Buffer* dstBuffer, 118 size_t dstOffset, 119 size_t size) override; 120 bool onCopyTextureToBuffer(const Texture*, 121 SkIRect srcRect, 122 const Buffer*, 123 size_t bufferOffset, 124 size_t bufferRowBytes) override; 125 bool onCopyBufferToTexture(const Buffer*, 126 const Texture*, 127 const BufferTextureCopyData* copyData, 128 int count) override; 129 bool onCopyTextureToTexture(const Texture* src, 130 SkIRect srcRect, 131 const Texture* dst, 132 SkIPoint dstPoint, 133 int mipLevel) override; 134 bool onSynchronizeBufferToCpu(const Buffer*, bool* outDidResultInWork) override; 135 bool onClearBuffer(const Buffer*, size_t offset, size_t size) override; 136 137 // Commiting uniform buffers' changes if any before drawing 138 void syncUniformBuffers(); 139 140 bool fBoundUniformBuffersDirty = false; 141 142 std::array<const DawnBuffer*, DawnGraphicsPipeline::kNumUniformBuffers> fBoundUniformBuffers; 143 std::array<uint32_t, DawnGraphicsPipeline::kNumUniformBuffers> fBoundUniformBufferOffsets; 144 std::array<uint32_t, DawnGraphicsPipeline::kNumUniformBuffers> fBoundUniformBufferSizes; 145 146 wgpu::CommandEncoder fCommandEncoder; 147 wgpu::RenderPassEncoder fActiveRenderPassEncoder; 148 wgpu::ComputePassEncoder fActiveComputePassEncoder; 149 150 wgpu::Buffer fCurrentIndirectBuffer; 151 size_t fCurrentIndirectBufferOffset = 0; 152 153 sk_sp<DawnBuffer> fIntrinsicConstantBuffer; 154 int fIntrinsicConstantBufferSlotsUsed = 0; 155 156 const DawnGraphicsPipeline* fActiveGraphicsPipeline = nullptr; 157 const DawnComputePipeline* fActiveComputePipeline = nullptr; 158 const DawnSharedContext* fSharedContext; 159 DawnResourceProvider* fResourceProvider; 160 }; 161 162 } // namespace skgpu::graphite 163 164 #endif // skgpu_graphite_DawnCommandBuffer_DEFINED 165