| 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_PaintParamsKey_DEFINED |
| 9 |
#define skgpu_graphite_PaintParamsKey_DEFINED |
| 10 |
|
| 11 |
#include "include/core/SkSpan.h" |
| 12 |
#include "include/core/SkTypes.h" |
| 13 |
#include "include/private/base/SkMacros.h" |
| 14 |
#include "include/private/base/SkTArray.h" |
| 15 |
#include "src/core/SkChecksum.h" |
| 16 |
#include "src/gpu/graphite/BuiltInCodeSnippetID.h" |
| 17 |
|
| 18 |
#include <limits> |
| 19 |
#include <cstring> // for memcmp |
| 20 |
|
| 21 |
class SkArenaAlloc; |
| 22 |
struct SkSamplingOptions; |
| 23 |
enum class SkTileMode; |
| 24 |
|
| 25 |
namespace skgpu::graphite { |
| 26 |
|
| 27 |
class Caps; |
| 28 |
class ShaderCodeDictionary; |
| 29 |
class ShaderNode; |
| 30 |
class TextureProxy; |
| 31 |
class UniquePaintParamsID; |
| 32 |
|
| 33 |
/** |
| 34 |
* This class is a compact representation of the shader needed to implement a given |
| 35 |
* PaintParams. Its structure is a series of nodes where each node consists of: |
| 36 |
* 4 bytes: code-snippet ID |
| 37 |
* N child nodes, where N is the constant number of children defined by the ShaderCodeDictionary |
| 38 |
* for the node's snippet ID. |
| 39 |
* |
| 40 |
* Some snippet definitions support embedding data into the PaintParamsKey, used when something |
| 41 |
* external to the generated SkSL needs produce unique pipelines (e.g. immutable samplers). For |
| 42 |
* snippets that store data, the data is stored immediately after the ID as: |
| 43 |
* 4 bytes: code-snippet ID |
| 44 |
* 4 bytes: data length |
| 45 |
* 0-M: variable length data |
| 46 |
* N child nodes |
| 47 |
* |
| 48 |
* All children of a child node are stored in the key before the next child is encoded in the key, |
| 49 |
* e.g. iterating the data in a key is a depth-first traversal of the node tree. |
| 50 |
* |
| 51 |
* The PaintParamsKey stores multiple root nodes, with each root representing an effect tree that |
| 52 |
* affects different parts of the shading pipeline. The key is can only hold 2 or 3 roots: |
| 53 |
* 1. Color root node: produces the "src" color used in final blending with the "dst" color. |
| 54 |
* 2. Final blend node: defines the blend function combining src and dst colors. If this is a |
| 55 |
* FixedBlend snippet the final pipeline may be able to lift it to HW blending. |
| 56 |
* 3. Clipping: optional, produces analytic coverage from a clip shader or shape. |
| 57 |
* |
| 58 |
* Logically the root effects produce a src color and the src coverage (augmenting any other |
| 59 |
* coverage coming from the RenderStep). A single src shading node could be used instead of the |
| 60 |
* two for color and blending, but its structure would always be: |
| 61 |
* |
| 62 |
* [ BlendCompose [ [ color-root-node ] surface-color [ final-blend ] ] ] |
| 63 |
* |
| 64 |
* where "surface-color" would be a special snippet that produces the current dst color value. |
| 65 |
* To keep PaintParamsKeys memory cost lower, the BlendCompose and "surface-color" nodes are implied |
| 66 |
* when generating the SkSL and pipeline. |
| 67 |
*/ |
| 68 |
class PaintParamsKey { |
| 69 |
public: |
| 70 |
// PaintParamsKey can only be created by using a PaintParamsKeyBuilder or by cloning the key |
| 71 |
// data from a Builder-owned key, but they can be passed around by value after that. |
| 72 |
constexpr PaintParamsKey(const PaintParamsKey&) = default; |
| 73 |
|
| 74 |
constexpr PaintParamsKey(SkSpan<const uint32_t> span) : fData(span) {} |
| 75 |
|
| 76 |
~PaintParamsKey() = default; |
| 77 |
PaintParamsKey& operator=(const PaintParamsKey&) = default; |
| 78 |
|
| 79 |
static constexpr PaintParamsKey Invalid() { return PaintParamsKey(SkSpan<const uint32_t>()); } |
| 80 |
bool isValid() const { return !fData.empty(); } |
| 81 |
|
| 82 |
// Return a PaintParamsKey whose data is owned by the provided arena and is not attached to |
| 83 |
// a PaintParamsKeyBuilder. The caller must ensure that the SkArenaAlloc remains alive longer |
| 84 |
// than the returned key. |
| 85 |
PaintParamsKey clone(SkArenaAlloc*) const; |
| 86 |
|
| 87 |
// Converts the key into a forest of ShaderNode trees. If the key is valid this will return at |
| 88 |
// least one root node. If the key contains unknown shader snippet IDs, returns an empty span. |
| 89 |
// All shader nodes, and the returned span's backing data, are owned by the provided arena. |
| 90 |
// |
| 91 |
// A valid key will produce either 2 or 3 root nodes. The first root node represents how the |
| 92 |
// source color is computed. The second node defines the final blender between the calculated |
| 93 |
// source color and the current pixel's dst color. If provided, the third node calculates an |
| 94 |
// additional analytic coverage value to combine with the geometry's coverage. |
| 95 |
// |
| 96 |
// Before returning the ShaderNode trees, this method decides which ShaderNode expressions to |
| 97 |
// lift to the vertex shader, depending on how many varyings are available. |
| 98 |
SkSpan<const ShaderNode*> getRootNodes(const Caps*, |
| 99 |
const ShaderCodeDictionary*, |
| 100 |
SkArenaAlloc*, |
| 101 |
int availableVaryings) const; |
| 102 |
|
| 103 |
// Converts the key to a structured list of snippet information for debugging or labeling |
| 104 |
// purposes. |
| 105 |
SkString toString(const Caps*, const ShaderCodeDictionary*) const; |
| 106 |
|
| 107 |
#ifdef SK_DEBUG |
| 108 |
void dump(const Caps*, const ShaderCodeDictionary*, UniquePaintParamsID) const; |
| 109 |
#endif |
| 110 |
|
| 111 |
bool operator==(const PaintParamsKey& that) const { |
| 112 |
return fData.size() == that.fData.size() && |
| 113 |
!memcmp(fData.data(), that.fData.data(), fData.size()); |
| 114 |
} |
| 115 |
bool operator!=(const PaintParamsKey& that) const { return !(*this == that); } |
| 116 |
|
| 117 |
struct Hash { |
| 118 |
uint32_t operator()(const PaintParamsKey& k) const { |
| 119 |
return SkChecksum::Hash32(k.fData.data(), k.fData.size_bytes()); |
| 120 |
} |
| 121 |
}; |
| 122 |
|
| 123 |
SkSpan<const uint32_t> data() const { return fData; } |
| 124 |
|
| 125 |
// Checks that a given key is viable for serialization and, also, that a deserialized |
| 126 |
// key is, at least, correctly formed. Other than that all the sizes make sense, this method |
| 127 |
// also checks that only Skia-internal shader code snippets appear in the key. |
| 128 |
[[nodiscard]] bool isSerializable(const ShaderCodeDictionary*) const; |
| 129 |
|
| 130 |
private: |
| 131 |
friend class PaintParamsKeyBuilder; // for the parented-data ctor |
| 132 |
|
| 133 |
// Returns null if the node or any of its children have an invalid snippet ID. Recursively |
| 134 |
// creates a node and all of its children, incrementing 'currentIndex' by the total number of |
| 135 |
// nodes created. |
| 136 |
ShaderNode* createNode(const ShaderCodeDictionary*, |
| 137 |
int* currentIndex, |
| 138 |
SkArenaAlloc* arena) const; |
| 139 |
|
| 140 |
// The memory referenced in 'fData' is always owned by someone else. It either shares the span |
| 141 |
// from the Builder, or clone() puts the span in an arena. |
| 142 |
SkSpan<const uint32_t> fData; |
| 143 |
}; |
| 144 |
|
| 145 |
// The PaintParamsKeyBuilder and the PaintParamsKeys snapped from it share the same |
| 146 |
// underlying block of memory. When an PaintParamsKey is snapped from the builder it 'locks' |
| 147 |
// the memory and 'unlocks' it in its destructor. Because of this relationship, the builder |
| 148 |
// can only have one extant key and that key must be destroyed before the builder can be reused |
| 149 |
// to create another one. |
| 150 |
// |
| 151 |
// This arrangement is intended to improve performance in the expected case, where a builder is |
| 152 |
// being used in a tight loop to generate keys which can be recycled once they've been used to |
| 153 |
// find the dictionary's matching uniqueID. We don't expect the cost of copying the key's memory |
| 154 |
// into the dictionary to be prohibitive since that should be infrequent. |
| 155 |
class PaintParamsKeyBuilder { |
| 156 |
public: |
| 157 |
PaintParamsKeyBuilder(const PaintParamsKeyBuilder&) = delete; |
| 158 |
PaintParamsKeyBuilder& operator=(const PaintParamsKeyBuilder&) = delete; |
| 159 |
|
| 160 |
PaintParamsKeyBuilder(PaintParamsKeyBuilder&&) = default; |
| 161 |
PaintParamsKeyBuilder& operator=(PaintParamsKeyBuilder&&) = default; |
| 162 |
|
| 163 |
PaintParamsKeyBuilder(const ShaderCodeDictionary* dict) { |
| 164 |
SkDEBUGCODE(fDict = dict;) |
| 165 |
} |
| 166 |
|
| 167 |
~PaintParamsKeyBuilder() { SkASSERT(!fLocked); } |
| 168 |
|
| 169 |
void beginBlock(BuiltInCodeSnippetID id) { this->beginBlock(static_cast<int32_t>(id)); } |
| 170 |
void beginBlock(int32_t codeSnippetID) { |
| 171 |
SkASSERT(!fLocked); |
| 172 |
SkDEBUGCODE(this->pushStack(codeSnippetID);) |
| 173 |
fData.push_back(codeSnippetID); |
| 174 |
} |
| 175 |
|
| 176 |
// TODO: Have endBlock() be handled automatically with RAII, in which case we could have it |
| 177 |
// validate the snippet ID being popped off the stack frame. |
| 178 |
void endBlock() { |
| 179 |
SkDEBUGCODE(this->popStack();) |
| 180 |
} |
| 181 |
|
| 182 |
#ifdef SK_DEBUG |
| 183 |
// Check that the builder has been reset to its initial state prior to creating a new key. |
| 184 |
void checkReset(); |
| 185 |
#endif |
| 186 |
|
| 187 |
// Helper to add blocks that don't have children |
| 188 |
void addBlock(BuiltInCodeSnippetID id) { |
| 189 |
this->beginBlock(id); |
| 190 |
this->endBlock(); |
| 191 |
} |
| 192 |
|
| 193 |
void addData(SkSpan<const uint32_t> data) { |
| 194 |
// First push the data size followed by the actual data. |
| 195 |
SkDEBUGCODE(this->validateData(data.size())); |
| 196 |
fData.push_back(data.size()); |
| 197 |
fData.push_back_n(data.size(), data.data()); |
| 198 |
} |
| 199 |
|
| 200 |
void addErrorBlock() { |
| 201 |
fHasError = true; |
| 202 |
// Preserve the structure of parent stack, but since fHasError is true, the builder won't |
| 203 |
// produce a valid PaintParamsKey. |
| 204 |
this->addBlock(BuiltInCodeSnippetID::kError); |
| 205 |
} |
| 206 |
|
| 207 |
void tryShrinkCapacity() { |
| 208 |
int halfCapacity = fData.capacity() / 2; |
| 209 |
if (fDataHighWaterMark < halfCapacity) { |
| 210 |
fDataHighWaterMark = 0; |
| 211 |
SkASSERT(fData.empty()); |
| 212 |
fData.reserve_exact(halfCapacity); |
| 213 |
} |
| 214 |
} |
| 215 |
|
| 216 |
private: |
| 217 |
friend class AutoLockBuilderAsKey; // for lockAsKey() and unlock() |
| 218 |
|
| 219 |
// Returns a view of this builder as a PaintParamsKey. The Builder cannot be used until the |
| 220 |
// returned Key goes out of scope. |
| 221 |
PaintParamsKey lockAsKey() { |
| 222 |
SkASSERT(!fLocked); // lockAsKey() is not re-entrant |
| 223 |
SkASSERT(fStack.empty()); // All beginBlocks() had a matching endBlock() |
| 224 |
|
| 225 |
SkDEBUGCODE(fLocked = true;) |
| 226 |
fDataHighWaterMark = std::max(fDataHighWaterMark, fData.size()); |
| 227 |
return fHasError ? PaintParamsKey::Invalid() : PaintParamsKey(fData); |
| 228 |
} |
| 229 |
|
| 230 |
// Invalidates any PaintParamsKey returned by lockAsKey() unless it has been cloned. |
| 231 |
void unlock() { |
| 232 |
SkASSERT(fLocked); |
| 233 |
fData.clear(); |
| 234 |
fHasError = false; |
| 235 |
|
| 236 |
SkDEBUGCODE(fLocked = false;) |
| 237 |
SkDEBUGCODE(fStack.clear();) |
| 238 |
SkDEBUGCODE(this->checkReset();) |
| 239 |
} |
| 240 |
|
| 241 |
// The data array uses clear() on unlock so that it's underlying storage and repeated use of the |
| 242 |
// builder will hit a high-water mark and avoid lots of allocations when recording draws. |
| 243 |
skia_private::TArray<uint32_t> fData; |
| 244 |
bool fHasError = false; // if true, fData may not encode a valid/complete ShaderNode tree. |
| 245 |
int fDataHighWaterMark = 0; |
| 246 |
|
| 247 |
#ifdef SK_DEBUG |
| 248 |
void pushStack(int32_t codeSnippetID); |
| 249 |
void validateData(size_t dataSize); |
| 250 |
void popStack(); |
| 251 |
|
| 252 |
// Information about the current block being written |
| 253 |
struct StackFrame { |
| 254 |
int fCodeSnippetID; |
| 255 |
int fNumExpectedChildren; |
| 256 |
int fNumActualChildren = 0; |
| 257 |
int fDataSize = -1; |
| 258 |
}; |
| 259 |
|
| 260 |
const ShaderCodeDictionary* fDict; |
| 261 |
skia_private::TArray<StackFrame> fStack; |
| 262 |
bool fLocked = false; |
| 263 |
#endif |
| 264 |
}; |
| 265 |
|
| 266 |
class AutoLockBuilderAsKey { |
| 267 |
public: |
| 268 |
AutoLockBuilderAsKey(PaintParamsKeyBuilder* builder) |
| 269 |
: fBuilder(builder) |
| 270 |
, fKey(builder->lockAsKey()) {} |
| 271 |
|
| 272 |
~AutoLockBuilderAsKey() { |
| 273 |
fBuilder->unlock(); |
| 274 |
} |
| 275 |
|
| 276 |
// Use as a PaintParamsKey |
| 277 |
const PaintParamsKey& operator*() const { return fKey; } |
| 278 |
const PaintParamsKey* operator->() const { return &fKey; } |
| 279 |
|
| 280 |
private: |
| 281 |
PaintParamsKeyBuilder* fBuilder; |
| 282 |
PaintParamsKey fKey; |
| 283 |
}; |
| 284 |
|
| 285 |
} // namespace skgpu::graphite |
| 286 |
|
| 287 |
#endif // skgpu_graphite_PaintParamsKey_DEFINED |