Debian Codegraph @

src/gpu/graphite/DrawList.h

raw
1 /*
2 * Copyright 2021 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_DrawList_DEFINED
8 #define skgpu_graphite_DrawList_DEFINED
9
10 #include "src/gpu/graphite/DrawListBase.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 <cstdint>
26 #include <limits>
27 #include <optional>
28
29 namespace skgpu::graphite {
30
31 class DrawPass;
32 class Geometry;
33 class Renderer;
34
35 /**
36 * A DrawList represents a collection of drawing commands (and related clip/shading state) in
37 * a form that closely mirrors what can be rendered efficiently and directly by the GPU backend
38 * (while balancing how much pre-processing to do for draws that might get eliminated later due to
39 * occlusion culling).
40 *
41 * A draw command combines:
42 * - a shape
43 * - a transform
44 * - a primitive clip (not affected by the transform)
45 * - optional shading description (shader, color filter, blend mode, etc)
46 * - a draw ordering (compressed painters index, stencil set, and write/test depth)
47 *
48 * Commands are accumulated in an arbitrary order and then sorted by increasing sort z when the list
49 * is prepared into an actual command buffer. The result of a draw command is the rasterization of
50 * the transformed shape, restricted by its primitive clip (e.g. a scissor rect) and a depth test
51 * of "GREATER" vs. its write/test z. (A test of GREATER, as opposed to GEQUAL, avoids double hits
52 * for draws that may have overlapping geometry, e.g. stroking.) If the command has a shading
53 * description, the color buffer will be modified; if not, it will be a depth-only draw.
54 *
55 * In addition to sorting the collected commands, the command list can be optimized during
56 * preparation. Commands that are fully occluded by later operations can be skipped entirely without
57 * affecting the final results. Adjacent commands (post sort) that would use equivalent GPU
58 * pipelines are merged to produce fewer (but larger) operations on the GPU.
59 *
60 * Other than flush-time optimizations (sort, cull, and merge), the command list does what you tell
61 * it to. Draw-specific simplification, style application, and advanced clipping should be handled
62 * at a higher layer.
63 */
64 class DrawList : public DrawListBase {
65 public:
66 // Add a constructor to prevent default zero initialization of SkTBlockList members' storage.
67 DrawList() {}
68
69 // DrawList requires that all Transforms be valid and asserts as much; invalid transforms should
70 // be detected at the Device level or similar. The provided Renderer must be compatible with the
71 // 'shape' and 'stroke' parameters. If the renderer uses coverage AA, 'ordering' must have a
72 // compressed painters order that reflects that. If the renderer uses stencil, the 'ordering'
73 // must have a valid stencil index as well.
74 void recordDraw(const Renderer* renderer,
75 const Transform& localToDevice,
76 const Geometry& geometry,
77 const Clip& clip,
78 DrawOrder ordering,
79 UniquePaintParamsID paintID,
80 SkEnumBitMask<DstUsage> dstUsage,
81 BarrierType barrierBeforeDraws,
82 PipelineDataGatherer* gatherer,
83 const StrokeStyle* stroke) override;
84
85 std::unique_ptr<DrawPass> snapDrawPass(Recorder* recorder,
86 sk_sp<TextureProxy> target,
87 const SkImageInfo& targetInfo,
88 DstReadStrategy dstReadStrategy) override;
89
90 // Discard all previously recorded draws and set to the requested load op (with optional clear
91 // color).
92 void reset(LoadOp op, SkColor4f clearColor = {0.f, 0.f, 0.f, 0.f}) override;
93
94 private:
95 struct Draw {
96 public:
97 Draw(const Renderer* renderer, const Transform& transform, const Geometry& geometry,
98 const Clip& clip, DrawOrder order, BarrierType barrierBeforeDraws,
99 const StrokeStyle* stroke)
100 : fRenderer(renderer)
101 , fDrawParams(transform, geometry, clip, order, stroke)
102 , fBarrierBeforeDraws(barrierBeforeDraws) {}
103
104 const Renderer* renderer() const { return fRenderer; }
105 const DrawParams& drawParams() const { return fDrawParams; }
106 const BarrierType& barrierBeforeDraws() const { return fBarrierBeforeDraws; }
107
108 private:
109 const Renderer* fRenderer;
110 DrawParams fDrawParams;
111 BarrierType fBarrierBeforeDraws;
112 };
113
114 template <uint64_t Bits, uint64_t Offset>
115 struct Bitfield {
116 static constexpr uint64_t kMask = ((uint64_t) 1 << Bits) - 1;
117 static constexpr uint64_t kOffset = Offset;
118 static constexpr uint64_t kBits = Bits;
119
120 static uint32_t get(uint64_t v) { return static_cast<uint32_t>((v >> kOffset) & kMask); }
121 static uint64_t set(uint32_t v) { return (v & kMask) << kOffset; }
122 };
123
124 /**
125 * Each Draw in a DrawList might be processed by multiple RenderSteps (determined by the Draw's
126 * Renderer), which can be sorted independently. Each (step, draw) pair produces its own
127 * SortKey.
128 *
129 * The goal of sorting draws for the DrawPass is to minimize pipeline transitions and dynamic
130 * binds within a pipeline, while still respecting the overall painter's order. This decreases
131 * the number of low-level draw commands in a command buffer and increases the size of those,
132 * allowing the GPU to operate more efficiently and have fewer bubbles within its own
133 * instruction stream.
134 *
135 * The Draw's CompresssedPaintersOrder and DisjointStencilIndex represent the most significant
136 * bits of the key, and are shared by all SortKeys produced by the same draw. Next, the pipeline
137 * description is encoded in two steps:
138 * 1. The index of the RenderStep packed in the high bits to ensure each step for a draw is
139 * ordered correctly.
140 * 2. An index into a cache of pipeline descriptions is used to encode the identity of the
141 * pipeline (SortKeys that differ in the bits from #1 necessarily would have different
142 * descriptions, but then the specific ordering of the RenderSteps isn't enforced). Last,
143 * the SortKey encodes an index into the set of uniform bindings accumulated for a DrawPass.
144 * This allows the SortKey to cluster draw steps that have both a compatible pipeline and do
145 * not require rebinding uniform data or other state (e.g. scissor). Since the uniform data
146 * index and the pipeline description index are packed into indices and not actual pointers,
147 * a given SortKey is only valid for the a specific DrawList->DrawPass conversion.
148 */
149 class SortKey {
150 public:
151 SortKey(const Draw* draw, int renderStep,
152 GraphicsPipelineCache::Index pipelineIndex,
153 UniformDataCache::Index uniformIndex,
154 TextureDataCache::Index textureBindingIndex)
155 : fPipelineKey(
156 ColorDepthOrderField::set(draw->drawParams().order().paintOrder().bits())
157 | StencilIndexField::set(draw->drawParams().order().stencilIndex().bits())
158 | RenderStepField::set(static_cast<uint32_t>(renderStep))
159 | PipelineField::set(pipelineIndex))
160 , fUniformKey(UniformField::set(uniformIndex) |
161 TextureBindingsField::set(textureBindingIndex))
162 , fDraw(draw) {
163 SkASSERT(pipelineIndex < GraphicsPipelineCache::kInvalidIndex);
164 SkASSERT(renderStep <= draw->renderer()->numRenderSteps());
165 }
166
167 bool operator<(const SortKey& k) const {
168 return fPipelineKey < k.fPipelineKey ||
169 (fPipelineKey == k.fPipelineKey && fUniformKey < k.fUniformKey);
170 }
171
172 const RenderStep& renderStep() const {
173 return fDraw->renderer()->step(RenderStepField::get(fPipelineKey));
174 }
175
176 const Draw& draw() const { return *fDraw; }
177
178 GraphicsPipelineCache::Index pipelineIndex() const {
179 return PipelineField::get(fPipelineKey);
180 }
181 UniformDataCache::Index uniformIndex() const {
182 return UniformField::get(fUniformKey);
183 }
184 TextureDataCache::Index textureBindingIndex() const {
185 return TextureBindingsField::get(fUniformKey);
186 }
187
188 private:
189 // Fields are ordered from most-significant to least when sorting by 128-bit value.
190 // NOTE: We don't use C++ bit fields because field ordering is implementation defined and we
191 // need to sort consistently.
192 using ColorDepthOrderField = Bitfield<16, 48>; // sizeof(CompressedPaintersOrder)
193 using StencilIndexField = Bitfield<16, 32>; // sizeof(DisjointStencilIndex)
194 using RenderStepField = Bitfield<2, 30>; // bits >= log2(Renderer::kMaxRenderSteps)
195 using PipelineField = Bitfield<30, 0>; // bits >= log2(max total steps in draw list)
196 uint64_t fPipelineKey;
197
198 // The uniform/texture index fields need 1 extra bit to encode "no-data". Values that are
199 // greater than or equal to 2^(bits-1) represent "no-data", while values between
200 // [0, 2^(bits-1)-1] can access data arrays without extra logic.
201 using UniformField = Bitfield<34, 30>; // bits >= 1+log2(max total steps)
202 using TextureBindingsField = Bitfield<30, 0>; // bits >= 1+log2(max total steps)
203 uint64_t fUniformKey;
204
205 // Backpointer to the draw that produced the sort key
206 const Draw* fDraw;
207
208 static_assert(ColorDepthOrderField::kBits >= sizeof(CompressedPaintersOrder));
209 static_assert(StencilIndexField::kBits >= sizeof(DisjointStencilIndex));
210 static_assert(RenderStepField::kBits >= SkNextLog2_portable(Renderer::kMaxRenderSteps));
211 static_assert(PipelineField::kBits >= SkNextLog2_portable(DrawListBase::kMaxRenderSteps));
212 static_assert(UniformField::kBits >= 1+SkNextLog2_portable(DrawListBase::kMaxRenderSteps));
213 static_assert(TextureBindingsField::kBits >= 1+SkNextLog2_portable(DrawListBase::kMaxRenderSteps));
214 };
215
216 SkTBlockList<Draw, 4> fDraws{SkBlockAllocator::GrowthPolicy::kFibonacci};
217
218 std::vector<SortKey> fSortKeys;
219 };
220
221 } // namespace skgpu::graphite
222
223 #endif // skgpu_graphite_DrawList_DEFINED