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