| 1 |
/* |
| 2 |
* Copyright 2023 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_compute_VelloComputeSteps_DEFINED |
| 9 |
#define skgpu_graphite_compute_VelloComputeSteps_DEFINED |
| 10 |
|
| 11 |
#include "include/core/SkColorType.h" |
| 12 |
#include "include/core/SkSize.h" |
| 13 |
#include "include/core/SkSpan.h" |
| 14 |
#include "include/private/base/SkTArray.h" |
| 15 |
#include "src/gpu/BufferWriter.h" |
| 16 |
#include "src/gpu/graphite/ComputeTypes.h" |
| 17 |
#include "src/gpu/graphite/compute/ComputeStep.h" |
| 18 |
|
| 19 |
#include "third_party/vello/cpp/vello.h" |
| 20 |
|
| 21 |
#include <string_view> |
| 22 |
#include <tuple> |
| 23 |
|
| 24 |
namespace skgpu::graphite { |
| 25 |
|
| 26 |
// This file defines ComputeSteps for all Vello compute stages and their permutations. The |
| 27 |
// declaration of each ComputeStep subclass mirrors the name of the pipeline stage as defined in the |
| 28 |
// shader metadata. |
| 29 |
// |
| 30 |
// The compute stages all operate over a shared set of buffer and image resources. The |
| 31 |
// `kVelloSlot_*` constant definitions below each uniquely identify a shared resource that must be |
| 32 |
// instantiated when assembling the ComputeSteps into a DispatchGroup. |
| 33 |
// |
| 34 |
// === Monoids and Prefix Sums === |
| 35 |
// |
| 36 |
// Vello's GPU algorithms make repeated use of parallel prefix sums techniques. These occur |
| 37 |
// frequently in path rasterization (e.g. winding number accummulation across a scanline can be |
| 38 |
// thought of as per-pixel prefix sums) but Vello also uses them to calculate buffer offsets for |
| 39 |
// associated entries across its variable length encoding streams. |
| 40 |
// |
| 41 |
// For instance, given a scene that contains Bézier paths, each path gets encoded as a transform, |
| 42 |
// a sequence of path tags (verbs), and zero or more 2-D points associated with each |
| 43 |
// tag. N paths will often map to N transforms, N + M tags, and N + M + L points (where N > 0, M > |
| 44 |
// 0, L >= 0). These entries are stored in separate parallel transform, path tag, and path data |
| 45 |
// streams. The correspondence between entries of these independent streams is implicit. To keep |
| 46 |
// CPU encoding of these streams fast, the offsets into each buffer for a given "path object" is |
| 47 |
// computed dynamically and in parallel on the GPU. Since the offsets for each object build |
| 48 |
// additively on offsets that appear before it in the stream, parallel computation of |
| 49 |
// offsets can be treated as a dynamic programming problem that maps well to parallel prefix sums |
| 50 |
// where each object is a "monoid" (https://en.wikipedia.org/wiki/Monoid) that supports algebraic |
| 51 |
// addition/subtraction over data encoded in the path tags themselves. |
| 52 |
// |
| 53 |
// Once computed, a monoid contains the offsets into the input (and sometimes output) buffers for a |
| 54 |
// given object. The parallel prefix sums operation is defined as a monoidal reduce + pre-scan pair. |
| 55 |
// (Prefix Sums and Their Applications, Blelloch, G., https://www.cs.cmu.edu/~guyb/papers/Ble93.pdf) |
| 56 |
// |
| 57 |
// While these concepts are an implementation detail they are core to the Vello algorithm and are |
| 58 |
// reflected in the pipeline names and data slot definitions. |
| 59 |
// |
| 60 |
// === Full Pipeline === |
| 61 |
// |
| 62 |
// The full Vello pipeline stages are as follows and should be dispatched in the following order: |
| 63 |
// |
| 64 |
// I. Build the path monoid stream: |
| 65 |
// If the input fits within the workgroup size: |
| 66 |
// pathtag_reduce, pathtag_scan_small |
| 67 |
// else |
| 68 |
// pathtag_reduce, pathtag_reduce2, pathtag_scan1, pathtag_scan_large |
| 69 |
// |
| 70 |
// II. Compute path bounding boxes, convert path segments into cubics: |
| 71 |
// bbox_clear, pathseg |
| 72 |
// |
| 73 |
// III. Process the draw object stream to build the draw monoids and inputs to the clip stage: |
| 74 |
// draw_reduce, draw_leaf |
| 75 |
// |
| 76 |
// IV. Compute the bounding boxes for the clip stack from the input stream, if the scene contains |
| 77 |
// clips: |
| 78 |
// clip_reduce, clip_leaf |
| 79 |
// |
| 80 |
// V. Allocate tile and segment buffers for the individual bins and prepare for coarse rasterization |
| 81 |
// binning, tile_alloc, path_coarse |
| 82 |
// |
| 83 |
// VI. Coarse rasterization |
| 84 |
// backdrop_dyn, coarse |
| 85 |
// |
| 86 |
// VII. Fine rasterization |
| 87 |
// fine |
| 88 |
// |
| 89 |
// TODO: Document the coverage mask pipeline once it has been re-implemented. |
| 90 |
|
| 91 |
// *** |
| 92 |
// Shared buffers that are accessed by various stages. |
| 93 |
// |
| 94 |
// The render configration uniform buffer. |
| 95 |
constexpr int kVelloSlot_ConfigUniform = 0; |
| 96 |
|
| 97 |
// The scene encoding buffer. |
| 98 |
constexpr int kVelloSlot_Scene = 1; |
| 99 |
|
| 100 |
// *** |
| 101 |
// Buffers used during the element processing stage. This stage converts the stream of variable |
| 102 |
// length path tags, transforms, brushes into a "path monoid" stream containing buffer offsets for |
| 103 |
// the subsequent stages that associate the input streams with individual draw elements. This stage |
| 104 |
// performs a parallel prefix sum (reduce + scan) which can be performed in two dispatches if the |
| 105 |
// entire input can be processed by a single workgroup per dispatch. Otherwise, the algorithm |
| 106 |
// requires two additional dispatches to continue the traversal (this is due to a lack of primitives |
| 107 |
// to synchronize execution across workgroups in MSL and WGSL). |
| 108 |
// |
| 109 |
// Single pass variant pipelines: pathtag_reduce, pathtag_scan_small |
| 110 |
// Multi-pass variant pipelines: pathtag_reduce, pathtag_reduce2, pathtag_scan1, pathtag_scan_large |
| 111 |
constexpr int kVelloSlot_TagMonoid = 2; |
| 112 |
|
| 113 |
// Single pass variant slots: |
| 114 |
constexpr int kVelloSlot_PathtagReduceOutput = 3; |
| 115 |
|
| 116 |
// Multi pass variant slots: |
| 117 |
constexpr int kVelloSlot_LargePathtagReduceFirstPassOutput = kVelloSlot_PathtagReduceOutput; |
| 118 |
constexpr int kVelloSlot_LargePathtagReduceSecondPassOutput = 4; |
| 119 |
constexpr int kVelloSlot_LargePathtagScanFirstPassOutput = 5; |
| 120 |
|
| 121 |
// *** |
| 122 |
// The second part of element processing flattens path elements (moveTo, lineTo, quadTo, etc) into |
| 123 |
// an unordered line soup buffer and computes their bounding boxes. This stage is where strokes get |
| 124 |
// expanded to fills and stroke styles get applied. The output is an unordered "line soup" buffer |
| 125 |
// and the tight device-space bounding box of each path. |
| 126 |
// |
| 127 |
// Pipelines: bbox_clear, flatten |
| 128 |
constexpr int kVelloSlot_PathBBoxes = 6; |
| 129 |
constexpr int kVelloSlot_Lines = 7; |
| 130 |
|
| 131 |
// *** |
| 132 |
// The next part prepares the draw object stream (entries in the per-tile command list aka PTCL) |
| 133 |
// and additional metadata for the subsequent clipping and binning stages. |
| 134 |
// |
| 135 |
// Pipelines: draw_reduce, draw_leaf |
| 136 |
constexpr int kVelloSlot_DrawReduceOutput = 8; |
| 137 |
constexpr int kVelloSlot_DrawMonoid = 9; |
| 138 |
constexpr int kVelloSlot_InfoBinData = 10; |
| 139 |
constexpr int kVelloSlot_ClipInput = 11; |
| 140 |
|
| 141 |
// *** |
| 142 |
// Clipping. The outputs of this stage are the finalized draw monoid and the clip bounding-boxes. |
| 143 |
// Clipping involves evaluating the stack monoid: refer to the following paper for the meaning of |
| 144 |
// these buffers: https://arxiv.org/pdf/2205.11659.pdf, |
| 145 |
// https://en.wikipedia.org/wiki/Bicyclic_semigroup |
| 146 |
// |
| 147 |
// Pipelines: clip_reduce, clip_leaf |
| 148 |
constexpr int kVelloSlot_ClipBicyclic = 12; |
| 149 |
constexpr int kVelloSlot_ClipElement = 13; |
| 150 |
constexpr int kVelloSlot_ClipBBoxes = 14; |
| 151 |
|
| 152 |
// *** |
| 153 |
// Buffers containing bump allocated data, the inputs and outputs to the binning, coarse raster, and |
| 154 |
// per-tile segment assembly stages. |
| 155 |
// |
| 156 |
// Pipelines: binning, tile_alloc, path_count, backdrop, coarse, path_tiling |
| 157 |
constexpr int kVelloSlot_DrawBBoxes = 15; |
| 158 |
constexpr int kVelloSlot_BumpAlloc = 16; |
| 159 |
constexpr int kVelloSlot_BinHeader = 17; |
| 160 |
|
| 161 |
constexpr int kVelloSlot_Path = 18; |
| 162 |
constexpr int kVelloSlot_Tile = 19; |
| 163 |
constexpr int kVelloSlot_SegmentCounts = 20; |
| 164 |
constexpr int kVelloSlot_Segments = 21; |
| 165 |
constexpr int kVelloSlot_PTCL = 22; |
| 166 |
|
| 167 |
// *** |
| 168 |
// Texture resources used by the fine rasterization stage. The gradient image needs to get populated |
| 169 |
// on the CPU with pre-computed gradient ramps. The image atlas is intended to hold pre-uploaded |
| 170 |
// images that are composited into the scene. |
| 171 |
// |
| 172 |
// The output image contains the final render. |
| 173 |
constexpr int kVelloSlot_OutputImage = 23; |
| 174 |
constexpr int kVelloSlot_GradientImage = 24; |
| 175 |
constexpr int kVelloSlot_ImageAtlas = 25; |
| 176 |
|
| 177 |
// *** |
| 178 |
// The indirect count buffer is used to issue an indirect dispatch of the path count and path tiling |
| 179 |
// stages. |
| 180 |
constexpr int kVelloSlot_IndirectCount = 26; |
| 181 |
|
| 182 |
// *** |
| 183 |
// The sample mask lookup table used in MSAA modes of the fine rasterization stage. |
| 184 |
constexpr int kVelloSlot_MaskLUT = 27; |
| 185 |
|
| 186 |
std::string_view VelloStageName(vello_cpp::ShaderStage); |
| 187 |
WorkgroupSize VelloStageLocalSize(vello_cpp::ShaderStage); |
| 188 |
skia_private::TArray<ComputeStep::WorkgroupBufferDesc> VelloWorkgroupBuffers( |
| 189 |
vello_cpp::ShaderStage); |
| 190 |
ComputeStep::NativeShaderSource VelloNativeShaderSource(vello_cpp::ShaderStage, |
| 191 |
ComputeStep::NativeShaderFormat); |
| 192 |
|
| 193 |
template <vello_cpp::ShaderStage S> |
| 194 |
class VelloStep : public ComputeStep { |
| 195 |
public: |
| 196 |
~VelloStep() override = default; |
| 197 |
|
| 198 |
NativeShaderSource nativeShaderSource(NativeShaderFormat format) const override { |
| 199 |
return VelloNativeShaderSource(S, format); |
| 200 |
} |
| 201 |
|
| 202 |
protected: |
| 203 |
explicit VelloStep(SkSpan<const ResourceDesc> resources) |
| 204 |
: ComputeStep(VelloStageName(S), |
| 205 |
VelloStageLocalSize(S), |
| 206 |
resources, |
| 207 |
AsSpan<ComputeStep::WorkgroupBufferDesc>(VelloWorkgroupBuffers(S)), |
| 208 |
Flags::kSupportsNativeShader) {} |
| 209 |
|
| 210 |
private: |
| 211 |
// Helper that creates a SkSpan from a universal reference to a container. Generally, creating a |
| 212 |
// SkSpan from an rvalue reference is not safe since the pointer stored in the SkSpan will |
| 213 |
// dangle beyond the constructor expression. In our usage in the constructor above, |
| 214 |
// the lifetime of the temporary TArray should match that of the SkSpan, both of which should |
| 215 |
// live through the constructor call expression. |
| 216 |
// |
| 217 |
// From https://en.cppreference.com/w/cpp/language/reference_initialization#Lifetime_of_a_temporary: |
| 218 |
// |
| 219 |
// a temporary bound to a reference parameter in a function call exists until the end of the |
| 220 |
// full expression containing that function call |
| 221 |
// |
| 222 |
template <typename T, typename C> |
| 223 |
static SkSpan<const T> AsSpan(C&& container) { |
| 224 |
return SkSpan(std::data(container), std::size(container)); |
| 225 |
} |
| 226 |
}; |
| 227 |
|
| 228 |
#define VELLO_COMPUTE_STEP(stage) \ |
| 229 |
class Vello##stage##Step final : public VelloStep<vello_cpp::ShaderStage::stage> { \ |
| 230 |
public: \ |
| 231 |
Vello##stage##Step(); \ |
| 232 |
}; |
| 233 |
|
| 234 |
VELLO_COMPUTE_STEP(BackdropDyn) |
| 235 |
VELLO_COMPUTE_STEP(BboxClear) |
| 236 |
VELLO_COMPUTE_STEP(Binning) |
| 237 |
VELLO_COMPUTE_STEP(ClipLeaf) |
| 238 |
VELLO_COMPUTE_STEP(ClipReduce) |
| 239 |
VELLO_COMPUTE_STEP(Coarse) |
| 240 |
VELLO_COMPUTE_STEP(Flatten) |
| 241 |
VELLO_COMPUTE_STEP(DrawLeaf) |
| 242 |
VELLO_COMPUTE_STEP(DrawReduce) |
| 243 |
VELLO_COMPUTE_STEP(PathCount) |
| 244 |
VELLO_COMPUTE_STEP(PathCountSetup) |
| 245 |
VELLO_COMPUTE_STEP(PathTiling) |
| 246 |
VELLO_COMPUTE_STEP(PathTilingSetup) |
| 247 |
VELLO_COMPUTE_STEP(PathtagReduce) |
| 248 |
VELLO_COMPUTE_STEP(PathtagReduce2) |
| 249 |
VELLO_COMPUTE_STEP(PathtagScan1) |
| 250 |
VELLO_COMPUTE_STEP(PathtagScanLarge) |
| 251 |
VELLO_COMPUTE_STEP(PathtagScanSmall) |
| 252 |
VELLO_COMPUTE_STEP(TileAlloc) |
| 253 |
|
| 254 |
#undef VELLO_COMPUTE_STEP |
| 255 |
|
| 256 |
template <vello_cpp::ShaderStage S, SkColorType T> class VelloFineStepBase : public VelloStep<S> { |
| 257 |
public: |
| 258 |
// We need to return a texture format for the bound textures. |
| 259 |
std::tuple<SkISize, SkColorType> calculateTextureParameters( |
| 260 |
int index, const ComputeStep::ResourceDesc&) const override { |
| 261 |
SkASSERT(index == 4); |
| 262 |
// TODO: The texture dimensions are unknown here so this method returns 0 for the texture |
| 263 |
// size. In this case this field is unused since VelloRenderer assigns texture resources |
| 264 |
// directly to the DispatchGroupBuilder. The format must still be queried to describe the |
| 265 |
// ComputeStep's binding layout. This method could be improved to enable conditional |
| 266 |
// querying of optional/dynamic parameters. |
| 267 |
return {SkISize{}, T}; |
| 268 |
} |
| 269 |
|
| 270 |
protected: |
| 271 |
explicit VelloFineStepBase(SkSpan<const ComputeStep::ResourceDesc> resources) |
| 272 |
: VelloStep<S>(resources) {} |
| 273 |
}; |
| 274 |
|
| 275 |
template <vello_cpp::ShaderStage S, SkColorType T, ::rust::Vec<uint8_t> (*MaskLutBuilder)()> |
| 276 |
class VelloFineMsaaStepBase : public VelloFineStepBase<S, T> { |
| 277 |
public: |
| 278 |
size_t calculateBufferSize(int resourceIndex, const ComputeStep::ResourceDesc&) const override { |
| 279 |
SkASSERT(resourceIndex == 5); |
| 280 |
return fMaskLut.size(); |
| 281 |
} |
| 282 |
|
| 283 |
void prepareStorageBuffer(int resourceIndex, |
| 284 |
const ComputeStep::ResourceDesc&, |
| 285 |
BufferWriter&& writer) const override { |
| 286 |
SkASSERT(resourceIndex == 5); |
| 287 |
writer.write(fMaskLut.data(), fMaskLut.size()); |
| 288 |
} |
| 289 |
|
| 290 |
protected: |
| 291 |
explicit VelloFineMsaaStepBase(SkSpan<const ComputeStep::ResourceDesc> resources) |
| 292 |
: VelloFineStepBase<S, T>(resources), fMaskLut(MaskLutBuilder()) {} |
| 293 |
|
| 294 |
private: |
| 295 |
::rust::Vec<uint8_t> fMaskLut; |
| 296 |
}; |
| 297 |
|
| 298 |
class VelloFineAreaStep final |
| 299 |
: public VelloFineStepBase<vello_cpp::ShaderStage::FineArea, kRGBA_8888_SkColorType> { |
| 300 |
public: |
| 301 |
VelloFineAreaStep(); |
| 302 |
}; |
| 303 |
|
| 304 |
class VelloFineAreaAlpha8Step final |
| 305 |
: public VelloFineStepBase<vello_cpp::ShaderStage::FineAreaR8, kAlpha_8_SkColorType> { |
| 306 |
public: |
| 307 |
VelloFineAreaAlpha8Step(); |
| 308 |
}; |
| 309 |
|
| 310 |
class VelloFineMsaa16Step final : public VelloFineMsaaStepBase<vello_cpp::ShaderStage::FineMsaa16, |
| 311 |
kRGBA_8888_SkColorType, |
| 312 |
vello_cpp::build_mask_lut_16> { |
| 313 |
public: |
| 314 |
VelloFineMsaa16Step(); |
| 315 |
}; |
| 316 |
|
| 317 |
class VelloFineMsaa16Alpha8Step final |
| 318 |
: public VelloFineMsaaStepBase<vello_cpp::ShaderStage::FineMsaa16R8, |
| 319 |
kAlpha_8_SkColorType, |
| 320 |
vello_cpp::build_mask_lut_16> { |
| 321 |
public: |
| 322 |
VelloFineMsaa16Alpha8Step(); |
| 323 |
}; |
| 324 |
|
| 325 |
class VelloFineMsaa8Step final : public VelloFineMsaaStepBase<vello_cpp::ShaderStage::FineMsaa8, |
| 326 |
kRGBA_8888_SkColorType, |
| 327 |
vello_cpp::build_mask_lut_8> { |
| 328 |
public: |
| 329 |
VelloFineMsaa8Step(); |
| 330 |
}; |
| 331 |
|
| 332 |
class VelloFineMsaa8Alpha8Step final |
| 333 |
: public VelloFineMsaaStepBase<vello_cpp::ShaderStage::FineMsaa8R8, |
| 334 |
kAlpha_8_SkColorType, |
| 335 |
vello_cpp::build_mask_lut_8> { |
| 336 |
public: |
| 337 |
VelloFineMsaa8Alpha8Step(); |
| 338 |
}; |
| 339 |
|
| 340 |
} // namespace skgpu::graphite |
| 341 |
|
| 342 |
#endif // skgpu_graphite_compute_VelloComputeSteps_DEFINED |