• 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 loadMSAAFromResolveAndBeginRenderPassEncoder(
75             const RenderPassDesc& frontendRenderPassDesc,
76             const wgpu::RenderPassDescriptor& wgpuRenderPassDesc,
77             const DawnTexture* msaaTexture);
78     bool doBlitWithDraw(const wgpu::RenderPassEncoder& renderEncoder,
79                         const RenderPassDesc& frontendRenderPassDesc,
80                         const wgpu::TextureView& sourceTextureView,
81                         int width,
82                         int height);
83     void endRenderPass();
84 
85     bool addDrawPass(const DrawPass*);
86 
87     bool bindGraphicsPipeline(const GraphicsPipeline*);
88     void setBlendConstants(float* blendConstants);
89 
90     void bindUniformBuffer(const BindBufferInfo& info, UniformSlot);
91     void bindDrawBuffers(const BindBufferInfo& vertices,
92                          const BindBufferInfo& instances,
93                          const BindBufferInfo& indices,
94                          const BindBufferInfo& indirect);
95 
96     void bindTextureAndSamplers(const DrawPass& drawPass,
97                                 const DrawPassCommands::BindTexturesAndSamplers& command);
98 
99     void setScissor(const Scissor&);
100     bool updateIntrinsicUniforms(SkIRect viewport);
101     void setViewport(SkIRect viewport);
102 
103     void draw(PrimitiveType type, unsigned int baseVertex, unsigned int vertexCount);
104     void drawIndexed(PrimitiveType type,
105                      unsigned int baseIndex,
106                      unsigned int indexCount,
107                      unsigned int baseVertex);
108     void drawInstanced(PrimitiveType type,
109                        unsigned int baseVertex,
110                        unsigned int vertexCount,
111                        unsigned int baseInstance,
112                        unsigned int instanceCount);
113     void drawIndexedInstanced(PrimitiveType type,
114                               unsigned int baseIndex,
115                               unsigned int indexCount,
116                               unsigned int baseVertex,
117                               unsigned int baseInstance,
118                               unsigned int instanceCount);
119     void drawIndirect(PrimitiveType type);
120     void drawIndexedIndirect(PrimitiveType type);
121 
122     // Methods for populating a Dawn ComputePassEncoder:
123     void beginComputePass();
124     void bindComputePipeline(const ComputePipeline*);
125     void bindDispatchResources(const DispatchGroup&, const DispatchGroup::Dispatch&);
126     void dispatchWorkgroups(const WorkgroupSize& globalSize);
127     void dispatchWorkgroupsIndirect(const Buffer* indirectBuffer, size_t indirectBufferOffset);
128     void endComputePass();
129 
130     // Methods for doing texture/buffer to texture/buffer copying:
131     bool onCopyBufferToBuffer(const Buffer* srcBuffer,
132                               size_t srcOffset,
133                               const Buffer* dstBuffer,
134                               size_t dstOffset,
135                               size_t size) override;
136     bool onCopyTextureToBuffer(const Texture*,
137                                SkIRect srcRect,
138                                const Buffer*,
139                                size_t bufferOffset,
140                                size_t bufferRowBytes) override;
141     bool onCopyBufferToTexture(const Buffer*,
142                                const Texture*,
143                                const BufferTextureCopyData* copyData,
144                                int count) override;
145     bool onCopyTextureToTexture(const Texture* src,
146                                 SkIRect srcRect,
147                                 const Texture* dst,
148                                 SkIPoint dstPoint,
149                                 int mipLevel) override;
150     bool onSynchronizeBufferToCpu(const Buffer*, bool* outDidResultInWork) override;
151     bool onClearBuffer(const Buffer*, size_t offset, size_t size) override;
152 
153     // Commiting uniform buffers' changes if any before drawing
154     void syncUniformBuffers();
155 
156     bool fBoundUniformBuffersDirty = false;
157 
158     std::array<BindBufferInfo, DawnGraphicsPipeline::kNumUniformBuffers> fBoundUniforms;
159 
160     wgpu::CommandEncoder fCommandEncoder;
161     wgpu::RenderPassEncoder fActiveRenderPassEncoder;
162     wgpu::ComputePassEncoder fActiveComputePassEncoder;
163 
164     wgpu::Buffer fCurrentIndirectBuffer;
165     size_t fCurrentIndirectBufferOffset = 0;
166 
167     bool fWroteFirstPassTimestamps = false;
168     wgpu::QuerySet fTimestampQuerySet;
169     sk_sp<DawnBuffer> fTimestampQueryBuffer;
170     sk_sp<DawnBuffer> fTimestampQueryXferBuffer;
171 
172     const DawnGraphicsPipeline* fActiveGraphicsPipeline = nullptr;
173     const DawnComputePipeline* fActiveComputePipeline = nullptr;
174     const DawnSharedContext* fSharedContext;
175     DawnResourceProvider* fResourceProvider;
176 };
177 
178 }  // namespace skgpu::graphite
179 
180 #endif  // skgpu_graphite_DawnCommandBuffer_DEFINED
181