• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 #include "src/gpu/graphite/dawn/DawnResourceProvider.h"
18 
19 #include "webgpu/webgpu_cpp.h"  // NO_G3_REWRITE
20 
21 #include <optional>
22 
23 namespace skgpu::graphite {
24 class ComputePipeline;
25 class DawnBuffer;
26 class DawnComputePipeline;
27 class DawnQueueManager;
28 class DawnSharedContext;
29 class DawnTexture;
30 class DispatchGroup;
31 struct WorkgroupSize;
32 
33 class DawnCommandBuffer final : public CommandBuffer {
34 public:
35     static std::unique_ptr<DawnCommandBuffer> Make(const DawnSharedContext*, DawnResourceProvider*);
36     ~DawnCommandBuffer() override;
37 
38     wgpu::CommandBuffer finishEncoding();
39 
40 #if defined(SK_DEBUG)
hasActivePassEncoder()41     bool hasActivePassEncoder() const {
42         return fActiveRenderPassEncoder || fActiveComputePassEncoder;
43     }
44 #endif
45 
46     bool startTimerQuery() override;
47     void endTimerQuery() override;
48     std::optional<GpuStats> gpuStats() override;
49 
50 private:
51     DawnCommandBuffer(const DawnSharedContext* sharedContext,
52                       DawnResourceProvider* resourceProvider);
53 
resourceProvider()54     ResourceProvider* resourceProvider() const override { return fResourceProvider; }
55 
56     void onResetCommandBuffer() override;
57     bool setNewCommandBufferResources() override;
58 
59     bool onAddRenderPass(const RenderPassDesc&,
60                          SkIRect renderPassBounds,
61                          const Texture* colorTexture,
62                          const Texture* resolveTexture,
63                          const Texture* depthStencilTexture,
64                          SkIRect viewport,
65                          const DrawPassList&) override;
66     bool onAddComputePass(DispatchGroupSpan) override;
67 
68     // Methods for populating a Dawn RenderPassEncoder:
69     bool beginRenderPass(const RenderPassDesc&,
70                          SkIRect renderPassBounds,
71                          const Texture* colorTexture,
72                          const Texture* resolveTexture,
73                          const Texture* depthStencilTexture);
74     bool emulateLoadMSAAFromResolveAndBeginRenderPassEncoder(
75             const RenderPassDesc& intendedRenderPassDesc,
76             const wgpu::RenderPassDescriptor& intendedDawnRenderPassDesc,
77             const SkIRect& renderPassBounds,
78             const DawnTexture* msaaTexture,
79             const DawnTexture* resolveTexture);
80     bool doBlitWithDraw(const wgpu::RenderPassEncoder& renderEncoder,
81                         const RenderPassDesc& frontendRenderPassDescKey,
82                         const wgpu::TextureView& srcTextureView,
83                         bool srcIsMSAA,
84                         const SkIRect& bounds);
85     bool endRenderPass();
86 
87     bool addDrawPass(const DrawPass*);
88 
89     bool bindGraphicsPipeline(const GraphicsPipeline*);
90     void setBlendConstants(float* blendConstants);
91 
92     void bindUniformBuffer(const BindBufferInfo& info, UniformSlot);
93     void bindDrawBuffers(const BindBufferInfo& vertices,
94                          const BindBufferInfo& instances,
95                          const BindBufferInfo& indices,
96                          const BindBufferInfo& indirect);
97 
98     void bindTextureAndSamplers(const DrawPass& drawPass,
99                                 const DrawPassCommands::BindTexturesAndSamplers& command);
100 
101     void setScissor(const Scissor&);
102     bool updateIntrinsicUniforms(SkIRect viewport);
103     void setViewport(SkIRect viewport);
104 
105     void draw(PrimitiveType type, unsigned int baseVertex, unsigned int vertexCount);
106     void drawIndexed(PrimitiveType type,
107                      unsigned int baseIndex,
108                      unsigned int indexCount,
109                      unsigned int baseVertex);
110     void drawInstanced(PrimitiveType type,
111                        unsigned int baseVertex,
112                        unsigned int vertexCount,
113                        unsigned int baseInstance,
114                        unsigned int instanceCount);
115     void drawIndexedInstanced(PrimitiveType type,
116                               unsigned int baseIndex,
117                               unsigned int indexCount,
118                               unsigned int baseVertex,
119                               unsigned int baseInstance,
120                               unsigned int instanceCount);
121     void drawIndirect(PrimitiveType type);
122     void drawIndexedIndirect(PrimitiveType type);
123 
124     // Methods for populating a Dawn ComputePassEncoder:
125     void beginComputePass();
126     void bindComputePipeline(const ComputePipeline*);
127     void bindDispatchResources(const DispatchGroup&, const DispatchGroup::Dispatch&);
128     void dispatchWorkgroups(const WorkgroupSize& globalSize);
129     void dispatchWorkgroupsIndirect(const Buffer* indirectBuffer, size_t indirectBufferOffset);
130     void endComputePass();
131 
132     // Methods for doing texture/buffer to texture/buffer copying:
133     bool onCopyBufferToBuffer(const Buffer* srcBuffer,
134                               size_t srcOffset,
135                               const Buffer* dstBuffer,
136                               size_t dstOffset,
137                               size_t size) override;
138     bool onCopyTextureToBuffer(const Texture*,
139                                SkIRect srcRect,
140                                const Buffer*,
141                                size_t bufferOffset,
142                                size_t bufferRowBytes) override;
143     bool onCopyBufferToTexture(const Buffer*,
144                                const Texture*,
145                                const BufferTextureCopyData* copyData,
146                                int count) override;
147     bool onCopyTextureToTexture(const Texture* src,
148                                 SkIRect srcRect,
149                                 const Texture* dst,
150                                 SkIPoint dstPoint,
151                                 int mipLevel) override;
152     bool onSynchronizeBufferToCpu(const Buffer*, bool* outDidResultInWork) override;
153     bool onClearBuffer(const Buffer*, size_t offset, size_t size) override;
154 
155     // Commiting uniform buffers' changes if any before drawing
156     void syncUniformBuffers();
157 
158     bool fBoundUniformBuffersDirty = false;
159 
160     std::array<BindBufferInfo, DawnGraphicsPipeline::kNumUniformBuffers> fBoundUniforms;
161 
162     wgpu::CommandEncoder fCommandEncoder;
163     wgpu::RenderPassEncoder fActiveRenderPassEncoder;
164     wgpu::ComputePassEncoder fActiveComputePassEncoder;
165 
166     struct ResolveStepEmulationInfo {
167         const DawnTexture* fMSAATexture;
168         const DawnTexture* fResolveTexture;
169         SkIRect fResolveArea;
170     };
171     std::optional<ResolveStepEmulationInfo> fResolveStepEmulationInfo;
172 
173     wgpu::Buffer fCurrentIndirectBuffer;
174     size_t fCurrentIndirectBufferOffset = 0;
175 
176     bool fWroteFirstPassTimestamps = false;
177     wgpu::QuerySet fTimestampQuerySet;
178     sk_sp<DawnBuffer> fTimestampQueryBuffer;
179     sk_sp<DawnBuffer> fTimestampQueryXferBuffer;
180 
181     const DawnGraphicsPipeline* fActiveGraphicsPipeline = nullptr;
182     const DawnComputePipeline* fActiveComputePipeline = nullptr;
183     const DawnSharedContext* fSharedContext;
184     DawnResourceProvider* fResourceProvider;
185 };
186 
187 }  // namespace skgpu::graphite
188 
189 #endif  // skgpu_graphite_DawnCommandBuffer_DEFINED
190