Debian Codegraph @

src/gpu/graphite/DrawListBase.h

raw
1 /*
2 * Copyright 2026 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 #ifndef skgpu_graphite_DrawListBase_DEFINED
8 #define skgpu_graphite_DrawListBase_DEFINED
9
10 #include "include/gpu/graphite/GraphiteTypes.h"
11
12 #include "include/private/base/SkDebug.h"
13 #include "src/base/SkBlockAllocator.h"
14 #include "src/base/SkEnumBitMask.h"
15 #include "src/base/SkTBlockList.h"
16 #include "src/gpu/graphite/ContextUtils.h"
17 #include "src/gpu/graphite/DrawCommands.h"
18 #include "src/gpu/graphite/DrawOrder.h"
19 #include "src/gpu/graphite/DrawParams.h"
20 #include "src/gpu/graphite/PaintParams.h"
21 #include "src/gpu/graphite/PipelineData.h"
22 #include "src/gpu/graphite/geom/Rect.h"
23 #include "src/gpu/graphite/geom/Transform.h"
24
25 #include <array>
26 #include <cstdint>
27 #include <limits>
28 #include <memory>
29 #include <optional>
30
31 namespace skgpu::graphite {
32
33 class DrawPass;
34 class Geometry;
35 class Renderer;
36 class Recorder;
37
38 /**
39 * The base interface for recording draw commands. DrawList implements the existing Graphite
40 * drawing behavior. In the next CL, DrawListLayer.h will implement the experimental layer based
41 * method.
42 */
43 class DrawListBase {
44 public:
45 // TODO (thomsmit): Remove this limit when DrawListLayer is used
46 static constexpr int kMaxRenderSteps = 4096;
47 static_assert(kMaxRenderSteps <= std::numeric_limits<uint16_t>::max());
48
49 DrawListBase() {}
50 virtual ~DrawListBase() = default;
51
52 virtual void recordDraw(
53 const Renderer* renderer,
54 const Transform& localToDevice,
55 const Geometry& geometry,
56 const Clip& clip,
57 DrawOrder ordering,
58 UniquePaintParamsID paintID,
59 SkEnumBitMask<DstUsage> dstUsage,
60 BarrierType barrierBeforeDraws,
61 PipelineDataGatherer* gatherer,
62 const StrokeStyle* stroke) = 0;
63
64 virtual std::unique_ptr<DrawPass> snapDrawPass(Recorder* recorder,
65 sk_sp<TextureProxy> target,
66 const SkImageInfo& targetInfo,
67 const DstReadStrategy dstReadStrategy) = 0;
68
69 int renderStepCount() const { return fRenderStepCount; }
70
71 bool modifiesTarget() const {
72 return this->renderStepCount() > 0 || fLoadOp == LoadOp::kClear;
73 }
74
75 bool samplesTexture(const TextureProxy* texture) const {
76 return fTextureDataCache.hasTexture(texture);
77 }
78
79 const Rect& dstReadBounds() const { return fDstReadBounds; }
80 const Rect& passBounds() const { return fPassBounds; }
81 bool drawsReadDst() const { return !fDstReadBounds.isEmptyNegativeOrNaN(); }
82 bool drawsRequireMSAA() const { return fRequiresMSAA; }
83 SkEnumBitMask<DepthStencilFlags> depthStencilFlags() const { return fDepthStencilFlags; }
84
85 SkDEBUGCODE(bool hasCoverageMaskDraws() const { return fCoverageMaskShapeDrawCount > 0; })
86
87 virtual void reset(LoadOp op, SkColor4f clearColor = {0.f, 0.f, 0.f, 0.f}) {
88 fLoadOp = op;
89 fClearColor = clearColor.premul().array();
90 fRenderStepCount = 0;
91 fDstReadBounds = Rect::InfiniteInverted();
92 fPassBounds = Rect::InfiniteInverted();
93 fRequiresMSAA = false;
94 fDepthStencilFlags = DepthStencilFlags::kNone;
95
96 fTransforms.reset();
97 fUniformDataCache.reset();
98 fTextureDataCache.reset();
99 fPipelineCache.reset();
100
101 SkDEBUGCODE(fCoverageMaskShapeDrawCount = 0;)
102 }
103
104 protected:
105 const Transform& deduplicateTransform(const Transform& localToDevice) {
106 if (fTransforms.empty() || fTransforms.back() != localToDevice) {
107 fTransforms.push_back(localToDevice);
108 }
109 return fTransforms.back();
110 }
111
112 // TODO (thomsmit): Once sort-based draw ordering is removed, try putting the transforms onto
113 // the DrawListLayer's arena allocator.
114 SkTBlockList<Transform, 4> fTransforms{SkBlockAllocator::GrowthPolicy::kFibonacci};
115
116 UniformDataCache fUniformDataCache;
117 TextureDataCache fTextureDataCache;
118 GraphicsPipelineCache fPipelineCache;
119
120 int fRenderStepCount = 0;
121
122 Rect fDstReadBounds = Rect::InfiniteInverted();
123 Rect fPassBounds = Rect::InfiniteInverted();
124 bool fRequiresMSAA = false;
125 SkEnumBitMask<DepthStencilFlags> fDepthStencilFlags = DepthStencilFlags::kNone;
126
127 LoadOp fLoadOp = LoadOp::kLoad;
128 std::array<float, 4> fClearColor = {0.f, 0.f, 0.f, 0.f};
129
130 #if defined(SK_DEBUG)
131 int fCoverageMaskShapeDrawCount = 0;
132 #endif
133
134 // Writes uniform data either to uniform buffers or to shared storage buffers, and tracks when
135 // bindings need to change between draws.
136 class UniformTracker {
137 public:
138 UniformTracker(bool useStorageBuffers) : fUseStorageBuffers(useStorageBuffers) {}
139
140 bool writeUniforms(UniformDataCache& uniformCache,
141 DrawBufferManager* bufferMgr,
142 UniformDataCache::Index index) {
143 if (index >= UniformDataCache::kInvalidIndex) {
144 return false;
145 }
146
147 if (index == fLastIndex) {
148 return false;
149 }
150 fLastIndex = index;
151
152 UniformDataCache::Entry& uniformData = uniformCache.lookup(index);
153 const size_t uniformDataSize = uniformData.fCpuData.size();
154
155 // Upload the uniform data if we haven't already.
156 // Alternatively, re-upload the uniform data to avoid a rebind if we're using storage
157 // buffers. This will result in more data uploaded, but the tradeoff seems worthwhile.
158 if (!uniformData.fBufferBinding.fBuffer ||
159 (fUseStorageBuffers &&
160 uniformData.fBufferBinding.fBuffer != fLastBinding.fBuffer)) {
161 BufferWriter writer;
162 std::tie(writer, uniformData.fBufferBinding) =
163 fCurrentBuffer.getMappedSubrange(1, uniformDataSize);
164 if (!writer) {
165 // Allocate a new buffer
166 std::tie(writer, uniformData.fBufferBinding, fCurrentBuffer) =
167 fUseStorageBuffers
168 ? bufferMgr->getMappedStorageBuffer(1, uniformDataSize)
169 : bufferMgr->getMappedUniformBuffer(1, uniformDataSize);
170 if (!writer) {
171 return {}; // Allocation failed so early out
172 }
173 }
174
175 writer.write(uniformData.fCpuData.data(), uniformDataSize);
176
177 if (fUseStorageBuffers) {
178 // When using storage buffers, store the SSBO index in the binding's offset
179 // field and always use the entire buffer's size in the size field.
180 SkASSERT(uniformData.fBufferBinding.fOffset % uniformDataSize == 0);
181 uniformData.fBufferBinding.fOffset /= uniformDataSize;
182 uniformData.fBufferBinding.fSize = uniformData.fBufferBinding.fBuffer->size();
183 } else {
184 // Every new set of uniform data has to be bound, this ensures its aligned
185 // correctly
186 fCurrentBuffer.resetForNewBinding();
187 }
188 }
189
190 const bool needsRebind = uniformData.fBufferBinding.fBuffer != fLastBinding.fBuffer ||
191 (!fUseStorageBuffers &&
192 uniformData.fBufferBinding.fOffset != fLastBinding.fOffset);
193
194 fLastBinding = uniformData.fBufferBinding;
195
196 return needsRebind;
197 }
198
199 void bindUniforms(UniformSlot slot, DrawPassCommands::List* commandList) {
200 BindBufferInfo binding = fLastBinding;
201 if (fUseStorageBuffers) {
202 // Track the SSBO index in fLastBinding, but set offset = 0 in the actual used
203 // binding.
204 binding.fOffset = 0;
205 }
206 commandList->bindUniformBuffer(binding, slot);
207 }
208
209 uint32_t ssboIndex() const {
210 // The SSBO index for the last-bound storage buffer is stored in the binding's offset
211 // field.
212 return fLastBinding.fOffset;
213 }
214
215 private:
216 // The GPU buffer data is being written into; for SSBOs, Graphite will only record a bind
217 // command when this changes. Sub-allocations will be aligned such that they can be randomly
218 // accessed even if the data is heterogenous. UBOs will always have to issue binding
219 // commands when a draw needs to use a different set of uniform values.
220 BufferSubAllocator fCurrentBuffer;
221
222 // Internally track the last binding returned, so that we know whether new uploads or
223 // rebindings are necessary. If we're using SSBOs, this is treated specially -- the fOffset
224 // field holds the index in the storage buffer of the last-written uniforms, and the offsets
225 // used for actual bindings are always zero.
226 BindBufferInfo fLastBinding;
227
228 // This keeps track of the last index used for writing uniforms from a provided uniform
229 // cache. If a provided index matches the last index, the uniforms are assumed to already be
230 // written and no additional uploading is performed. This assumes a UniformTracker will
231 // always be provided with the same uniform cache.
232 UniformDataCache::Index fLastIndex = UniformDataCache::kInvalidIndex;
233
234 const bool fUseStorageBuffers;
235 };
236
237 // Tracks when to issue BindTexturesAndSamplers commands to a command list and converts
238 // TextureDataBlocks to that representation as needed.
239 class TextureTracker {
240 public:
241 TextureTracker(TextureDataCache* textureCache) : fTextureCache(textureCache) {}
242
243 bool setCurrentTextureBindings(TextureDataCache::Index bindingIndex) {
244 if (bindingIndex < TextureDataCache::kInvalidIndex && fLastIndex != bindingIndex) {
245 fLastIndex = bindingIndex;
246 return true;
247 }
248 // No binding change
249 return false;
250 }
251
252 void bindTextures(DrawPassCommands::List* commandList) {
253 SkASSERT(fLastIndex < TextureDataCache::kInvalidIndex);
254 TextureDataBlock binding = fTextureCache->lookup(fLastIndex);
255
256 auto [textures, samplers] =
257 commandList->bindDeferredTexturesAndSamplers(binding.numTextures());
258
259 for (int i = 0; i < binding.numTextures(); ++i) {
260 auto [t, s] = binding.texture(i);
261 textures[i] = t.get();
262 samplers[i] = s;
263 }
264 }
265
266 private:
267 TextureDataCache::Index fLastIndex = TextureDataCache::kInvalidIndex;
268
269 TextureDataCache* const fTextureCache;
270 };
271 };
272
273 } // namespace skgpu::graphite
274
275 #endif // skgpu_graphite_DrawListBase_DEFINED