| 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_ShaderCodeDictionary_DEFINED |
| 9 |
#define skgpu_graphite_ShaderCodeDictionary_DEFINED |
| 10 |
|
| 11 |
#include "include/core/SkSpan.h" |
| 12 |
#include "include/effects/SkRuntimeEffect.h" |
| 13 |
#include "include/private/base/SkTo.h" |
| 14 |
#include "src/base/SkArenaAlloc.h" |
| 15 |
#include "src/base/SkEnumBitMask.h" |
| 16 |
#include "src/base/SkSpinlock.h" |
| 17 |
#include "src/core/SkKnownRuntimeEffects.h" |
| 18 |
#include "src/core/SkSLTypeShared.h" |
| 19 |
#include "src/core/SkTHash.h" |
| 20 |
#include "src/gpu/graphite/Attribute.h" |
| 21 |
#include "src/gpu/graphite/BuiltInCodeSnippetID.h" |
| 22 |
#include "src/gpu/graphite/PaintParamsKey.h" |
| 23 |
#include "src/gpu/graphite/ResourceTypes.h" |
| 24 |
#include "src/gpu/graphite/Uniform.h" |
| 25 |
#include "src/gpu/graphite/UniquePaintParamsID.h" |
| 26 |
|
| 27 |
#include <array> |
| 28 |
#include <cstddef> |
| 29 |
#include <cstdint> |
| 30 |
#include <memory> |
| 31 |
#include <string> |
| 32 |
#include <string_view> |
| 33 |
|
| 34 |
class SkRuntimeEffect; |
| 35 |
|
| 36 |
namespace skgpu::graphite { |
| 37 |
|
| 38 |
// TODO: How to represent the type (e.g., 2D) of texture being sampled? |
| 39 |
class TextureAndSampler { |
| 40 |
public: |
| 41 |
constexpr TextureAndSampler(const char* name) : fName(name) {} |
| 42 |
|
| 43 |
const char* name() const { return fName; } |
| 44 |
|
| 45 |
private: |
| 46 |
const char* fName; |
| 47 |
}; |
| 48 |
|
| 49 |
enum class SnippetRequirementFlags : uint32_t { |
| 50 |
kNone = 0x0, |
| 51 |
|
| 52 |
// Signature of the ShaderNode |
| 53 |
kLocalCoords = 0x1, |
| 54 |
kPriorStageOutput = 0x2, // AKA the "input" color, or the "src" argument for a blender |
| 55 |
kBlenderDstColor = 0x4, // The "dst" argument for a blender |
| 56 |
|
| 57 |
// Special values and/or behaviors required for the snippet |
| 58 |
kPrimitiveColor = 0x8, |
| 59 |
kGradientBuffer = 0x10, |
| 60 |
kStoresSamplerDescData = 0x20, // Indicates that the node stores numerical sampler data |
| 61 |
kPassthroughLocalCoords = 0x40, // Indicates that the node will pass through local coords |
| 62 |
// unmodified to its children. |
| 63 |
kLiftExpression = 0x80, // Indicates that the liftable expression generated by this |
| 64 |
// node will be lifted to the vertex shader and passed to the |
| 65 |
// fragment shader as a varying. |
| 66 |
kOmitExpression = 0x100, // Indicates that the liftable expression generated by this |
| 67 |
// node will be used in a compound expression lifted to the |
| 68 |
// vertex shader and omitted from the fragment shader entirely. |
| 69 |
}; |
| 70 |
SK_MAKE_BITMASK_OPS(SnippetRequirementFlags) |
| 71 |
|
| 72 |
class ShaderInfo; |
| 73 |
class ShaderNode; |
| 74 |
|
| 75 |
// ShaderSnippets define the "ABI" of a SkSL module function and its required uniform data, as |
| 76 |
// well as functions for generating the invoking SkSL. Snippets are composed into an effect tree |
| 77 |
// using ShaderNodes. |
| 78 |
struct ShaderSnippet { |
| 79 |
struct Args { |
| 80 |
std::string fPriorStageOutput; |
| 81 |
std::string fBlenderDstColor; |
| 82 |
std::string fFragCoord; |
| 83 |
}; |
| 84 |
|
| 85 |
using GeneratePreambleForSnippetFn = std::string (*)(const ShaderInfo& shaderInfo, |
| 86 |
const ShaderNode*); |
| 87 |
using GenerateLiftableExpressionFn = std::string (*)(const ShaderInfo& shaderInfo, |
| 88 |
const ShaderNode*, |
| 89 |
const ShaderSnippet::Args& args); |
| 90 |
static const Args kDefaultArgs; |
| 91 |
|
| 92 |
// If this snippet has an expression that can be lifted from the fragment shader to the vertex |
| 93 |
// shader, the LiftableExpressionType specifies how the resolved expression is used by |
| 94 |
// subsequent shader nodes. |
| 95 |
enum class LiftableExpressionType : uint32_t { |
| 96 |
kNone, |
| 97 |
kLocalCoords, |
| 98 |
kPriorStageOutput, |
| 99 |
}; |
| 100 |
|
| 101 |
ShaderSnippet() = default; |
| 102 |
|
| 103 |
ShaderSnippet(const char* name, |
| 104 |
const char* staticFn, |
| 105 |
SkEnumBitMask<SnippetRequirementFlags> snippetRequirementFlags, |
| 106 |
SkSpan<const Uniform> uniforms, |
| 107 |
SkSpan<const TextureAndSampler> texturesAndSamplers = {}, |
| 108 |
GeneratePreambleForSnippetFn preambleGenerator = nullptr, |
| 109 |
int numChildren = 0, |
| 110 |
GenerateLiftableExpressionFn liftableExpression = nullptr, |
| 111 |
LiftableExpressionType liftableExpressionType = LiftableExpressionType::kNone, |
| 112 |
Interpolation liftableExpressionInterpolation = Interpolation::kPerspective) |
| 113 |
: fName(name) |
| 114 |
, fStaticFunctionName(staticFn) |
| 115 |
, fSnippetRequirementFlags(snippetRequirementFlags) |
| 116 |
, fUniforms(uniforms) |
| 117 |
, fTexturesAndSamplers(texturesAndSamplers) |
| 118 |
, fNumChildren(numChildren) |
| 119 |
, fPreambleGenerator(preambleGenerator) |
| 120 |
, fLiftableExpressionInterpolation(liftableExpressionInterpolation) |
| 121 |
, fLiftableExpressionType(liftableExpressionType) |
| 122 |
, fLiftableExpressionGenerator(liftableExpression) { |
| 123 |
// Must always provide a name; static function is not optional if using the default (null) |
| 124 |
// generation logic. |
| 125 |
SkASSERT(name); |
| 126 |
SkASSERT(staticFn || preambleGenerator); |
| 127 |
} |
| 128 |
|
| 129 |
bool needsLocalCoords() const { |
| 130 |
return SkToBool(fSnippetRequirementFlags & SnippetRequirementFlags::kLocalCoords); |
| 131 |
} |
| 132 |
bool needsPriorStageOutput() const { |
| 133 |
return SkToBool(fSnippetRequirementFlags & SnippetRequirementFlags::kPriorStageOutput); |
| 134 |
} |
| 135 |
bool needsBlenderDstColor() const { |
| 136 |
return SkToBool(fSnippetRequirementFlags & SnippetRequirementFlags::kBlenderDstColor); |
| 137 |
} |
| 138 |
bool storesSamplerDescData() const { |
| 139 |
return SkToBool(fSnippetRequirementFlags & SnippetRequirementFlags::kStoresSamplerDescData); |
| 140 |
} |
| 141 |
|
| 142 |
const char* fName = nullptr; |
| 143 |
const char* fStaticFunctionName = nullptr; |
| 144 |
|
| 145 |
// The features and args that this shader snippet requires in order to be invoked |
| 146 |
SkEnumBitMask<SnippetRequirementFlags> fSnippetRequirementFlags{SnippetRequirementFlags::kNone}; |
| 147 |
|
| 148 |
// If not null, the list of uniforms in `fUniforms` describes an existing struct type declared |
| 149 |
// in the Graphite modules with the given name. Instead of inlining the each uniform in the |
| 150 |
// top-level interface block or aggregate struct, there will be a single member of this struct's |
| 151 |
// type. |
| 152 |
const char* fUniformStructName = nullptr; |
| 153 |
// If the uniforms are being embedded as a sub-struct, this is the required starting alignment. |
| 154 |
int fRequiredAlignment = -1; |
| 155 |
|
| 156 |
skia_private::TArray<Uniform> fUniforms; |
| 157 |
skia_private::TArray<TextureAndSampler> fTexturesAndSamplers; |
| 158 |
|
| 159 |
int fNumChildren = 0; |
| 160 |
GeneratePreambleForSnippetFn fPreambleGenerator = nullptr; |
| 161 |
|
| 162 |
Interpolation fLiftableExpressionInterpolation = Interpolation::kPerspective; |
| 163 |
LiftableExpressionType fLiftableExpressionType = LiftableExpressionType::kNone; |
| 164 |
GenerateLiftableExpressionFn fLiftableExpressionGenerator = nullptr; |
| 165 |
}; |
| 166 |
|
| 167 |
// ShaderNodes organize snippets into an effect tree, and provide random access to the dynamically |
| 168 |
// bound child snippets. Each node has a fixed number of children defined by its code ID |
| 169 |
// (either a BuiltInCodeSnippetID or a runtime effect's assigned ID). All children are non-null. |
| 170 |
// A ShaderNode tree represents a decompressed PaintParamsKey. |
| 171 |
class ShaderNode { |
| 172 |
public: |
| 173 |
// ShaderNodes should be created in conjunction with an SkArenaAlloc that owns all nodes. |
| 174 |
ShaderNode(const ShaderSnippet* snippet, |
| 175 |
SkSpan<ShaderNode*> children, |
| 176 |
int codeID, |
| 177 |
int keyIndex, |
| 178 |
SkSpan<const uint32_t> data) |
| 179 |
: fEntry(snippet) |
| 180 |
, fChildren(children) |
| 181 |
, fCodeID(codeID) |
| 182 |
, fKeyIndex(keyIndex) |
| 183 |
, fRequiredFlags(snippet->fSnippetRequirementFlags) |
| 184 |
, fData(data) { |
| 185 |
SkASSERT(children.size() == (size_t) fEntry->fNumChildren); |
| 186 |
|
| 187 |
// Propagate requirement flags from children towards root. |
| 188 |
const bool isRuntimeEffect = codeID >= kBuiltInCodeSnippetIDCount; |
| 189 |
const bool isCompose = codeID == (int) BuiltInCodeSnippetID::kCompose || |
| 190 |
codeID == (int) BuiltInCodeSnippetID::kBlendCompose; |
| 191 |
for (const ShaderNode* child : children) { |
| 192 |
// Mask off flags to not propagate. |
| 193 |
SkEnumBitMask<SnippetRequirementFlags> mask = |
| 194 |
SnippetRequirementFlags::kPassthroughLocalCoords | |
| 195 |
SnippetRequirementFlags::kLiftExpression | |
| 196 |
SnippetRequirementFlags::kOmitExpression; |
| 197 |
// Runtime effects invoke children with explicit parameters so those requirements never |
| 198 |
// need to propagate to the root. Similarly, compose only needs to propagate the |
| 199 |
// variable parameters for the inner children. |
| 200 |
if (isRuntimeEffect || (isCompose && child == children.back())) { |
| 201 |
mask |= SnippetRequirementFlags::kLocalCoords | |
| 202 |
SnippetRequirementFlags::kPriorStageOutput | |
| 203 |
SnippetRequirementFlags::kBlenderDstColor; |
| 204 |
} |
| 205 |
fRequiredFlags |= (child->requiredFlags() & ~mask); |
| 206 |
} |
| 207 |
|
| 208 |
// Data should only be provided if the snippet has the kStoresSamplerDescData flag. |
| 209 |
SkASSERT(fData.empty() || snippet->storesSamplerDescData()); |
| 210 |
} |
| 211 |
|
| 212 |
std::string generateDefaultPreamble(const ShaderInfo& shaderInfo) const; |
| 213 |
std::string invokeAndAssign(const ShaderInfo& shaderInfo, |
| 214 |
const ShaderSnippet::Args& args, |
| 215 |
std::string* funcBody) const; |
| 216 |
|
| 217 |
std::string getExpressionVaryingName() const; |
| 218 |
|
| 219 |
int32_t codeSnippetId() const { return fCodeID; } |
| 220 |
int32_t keyIndex() const { return fKeyIndex; } |
| 221 |
const ShaderSnippet* entry() const { return fEntry; } |
| 222 |
|
| 223 |
SkEnumBitMask<SnippetRequirementFlags> requiredFlags() const { return fRequiredFlags; } |
| 224 |
void setLiftExpressionFlag() { fRequiredFlags |= SnippetRequirementFlags::kLiftExpression; } |
| 225 |
void setOmitExpressionFlag() { fRequiredFlags |= SnippetRequirementFlags::kOmitExpression; } |
| 226 |
void unsetLocalCoordsFlag() { fRequiredFlags &= ~SnippetRequirementFlags::kLocalCoords; } |
| 227 |
|
| 228 |
int numChildren() const { return fEntry->fNumChildren; } |
| 229 |
SkSpan<ShaderNode*> children() { return fChildren; } |
| 230 |
SkSpan<const ShaderNode*> children() const { |
| 231 |
return SkSpan<const ShaderNode*>(const_cast<const ShaderNode**>(fChildren.data()), |
| 232 |
fChildren.size()); |
| 233 |
} |
| 234 |
const ShaderNode* child(int childIndex) const { return fChildren[childIndex]; } |
| 235 |
|
| 236 |
SkSpan<const uint32_t> data() const { return fData; } |
| 237 |
|
| 238 |
private: |
| 239 |
const ShaderSnippet* fEntry; // Owned by the ShaderCodeDictionary |
| 240 |
SkSpan<ShaderNode*> fChildren; // Owned by the ShaderInfo's arena |
| 241 |
|
| 242 |
int32_t fCodeID; |
| 243 |
int32_t fKeyIndex; // index back to PaintParamsKey, unique across nodes within a ShaderInfo |
| 244 |
|
| 245 |
SkEnumBitMask<SnippetRequirementFlags> fRequiredFlags; |
| 246 |
SkSpan<const uint32_t> fData; // Subspan of PaintParamsKey's fData; shares same owner |
| 247 |
}; |
| 248 |
|
| 249 |
// ShaderCodeDictionary is a thread-safe dictionary of ShaderSnippets to code IDs for use with |
| 250 |
// creating PaintParamKeys, as well as assigning unique IDs to each encountered PaintParamKey. |
| 251 |
// It defines ShaderSnippets for every BuiltInCodeSnippetID and maintains records for IDs per |
| 252 |
// SkRuntimeEffect, including de-duplicating equivalent SkRuntimeEffect objects. |
| 253 |
class ShaderCodeDictionary { |
| 254 |
public: |
| 255 |
ShaderCodeDictionary(Layout layout, |
| 256 |
SkSpan<sk_sp<SkRuntimeEffect>> userDefinedKnownRuntimeEffects); |
| 257 |
|
| 258 |
UniquePaintParamsID findOrCreate(const PaintParamsKey&); |
| 259 |
|
| 260 |
UniquePaintParamsID findOrCreate(PaintParamsKeyBuilder*) SK_EXCLUDES(fSpinLock); |
| 261 |
|
| 262 |
PaintParamsKey lookup(UniquePaintParamsID) const SK_EXCLUDES(fSpinLock); |
| 263 |
|
| 264 |
SkString idToString(const Caps* caps, UniquePaintParamsID id) const { |
| 265 |
return this->lookup(id).toString(caps, this); |
| 266 |
} |
| 267 |
|
| 268 |
#if defined(SK_DEBUG) |
| 269 |
bool isValidID(int snippetID) const SK_EXCLUDES(fSpinLock); |
| 270 |
|
| 271 |
void dump(const Caps*, UniquePaintParamsID) const; |
| 272 |
#endif |
| 273 |
|
| 274 |
// This method can return nullptr |
| 275 |
const ShaderSnippet* getEntry(int codeSnippetID) const SK_EXCLUDES(fSpinLock); |
| 276 |
const ShaderSnippet* getEntry(BuiltInCodeSnippetID codeSnippetID) const { |
| 277 |
// Built-in code snippets are initialized once so there is no need to take a lock |
| 278 |
return &fBuiltInCodeSnippets[SkTo<int>(codeSnippetID)]; |
| 279 |
} |
| 280 |
|
| 281 |
// getEntry can be used to retrieve the ShaderSnippet for a user-defined known runtime effect |
| 282 |
// but, since the ShaderCodeDictionary owns those runtime effects, we need another entry |
| 283 |
// point to retrieve the actual effect. For unknown runtime effects this is handled by the |
| 284 |
// RuntimeEffectDictionary which, transiently, holds a ref on the encountered runtime effects. |
| 285 |
const SkRuntimeEffect* getUserDefinedKnownRuntimeEffect(int codeSnippetID) const; |
| 286 |
|
| 287 |
// Returns -1 on failure |
| 288 |
int findOrCreateRuntimeEffectSnippet(const SkRuntimeEffect* effect) SK_EXCLUDES(fSpinLock); |
| 289 |
|
| 290 |
bool isUserDefinedKnownRuntimeEffect(int candidate) const; |
| 291 |
#if defined(GPU_TEST_UTILS) |
| 292 |
int numUserDefinedRuntimeEffects() const SK_EXCLUDES(fSpinLock); |
| 293 |
int numUserDefinedKnownRuntimeEffects() const; |
| 294 |
#endif |
| 295 |
|
| 296 |
private: |
| 297 |
const char* addTextToArena(std::string_view text); |
| 298 |
|
| 299 |
SkSpan<const Uniform> convertUniforms(const SkRuntimeEffect* effect); |
| 300 |
ShaderSnippet convertRuntimeEffect(const SkRuntimeEffect* effect, const char* name); |
| 301 |
|
| 302 |
void registerUserDefinedKnownRuntimeEffects(SkSpan<sk_sp<SkRuntimeEffect>>); |
| 303 |
|
| 304 |
const Layout fLayout; |
| 305 |
|
| 306 |
std::array<ShaderSnippet, kBuiltInCodeSnippetIDCount> fBuiltInCodeSnippets; |
| 307 |
|
| 308 |
using KnownRuntimeEffectArray = std::array<ShaderSnippet, SkKnownRuntimeEffects::kStableKeyCnt>; |
| 309 |
KnownRuntimeEffectArray fKnownRuntimeEffectCodeSnippets SK_GUARDED_BY(fSpinLock); |
| 310 |
|
| 311 |
using ShaderSnippetArray = skia_private::TArray<ShaderSnippet>; |
| 312 |
using RuntimeEffectArray = skia_private::TArray<sk_sp<SkRuntimeEffect>>; |
| 313 |
|
| 314 |
// These two arrays are not guarded by a lock since they are only initialized in the ctor |
| 315 |
ShaderSnippetArray fUserDefinedKnownCodeSnippets; |
| 316 |
RuntimeEffectArray fUserDefinedKnownRuntimeEffects; |
| 317 |
|
| 318 |
// The value returned from 'getEntry' must be stable so, hold the user-defined code snippet |
| 319 |
// entries as pointers. |
| 320 |
ShaderSnippetArray fUserDefinedCodeSnippets SK_GUARDED_BY(fSpinLock); |
| 321 |
|
| 322 |
// TODO: can we do something better given this should have write-seldom/read-often behavior? |
| 323 |
mutable SkSpinlock fSpinLock; |
| 324 |
|
| 325 |
using PaintIDMap = skia_private::THashMap<PaintParamsKey, |
| 326 |
UniquePaintParamsID, |
| 327 |
PaintParamsKey::Hash>; |
| 328 |
|
| 329 |
PaintIDMap fPaintKeyToID SK_GUARDED_BY(fSpinLock); |
| 330 |
skia_private::TArray<PaintParamsKey> fIDToPaintKey SK_GUARDED_BY(fSpinLock); |
| 331 |
|
| 332 |
SK_BEGIN_REQUIRE_DENSE |
| 333 |
struct RuntimeEffectKey { |
| 334 |
uint32_t fHash; |
| 335 |
uint32_t fUniformSize; |
| 336 |
|
| 337 |
bool operator==(RuntimeEffectKey rhs) const { |
| 338 |
return fHash == rhs.fHash && fUniformSize == rhs.fUniformSize; |
| 339 |
} |
| 340 |
}; |
| 341 |
SK_END_REQUIRE_DENSE |
| 342 |
|
| 343 |
// A map from RuntimeEffectKeys (hash plus uniforms) to code-snippet IDs. RuntimeEffectKeys |
| 344 |
// don't track the lifetime of a runtime effect at all; they live forever, and a newly- |
| 345 |
// instantiated runtime effect with the same program as a previously-discarded effect will reuse |
| 346 |
// an existing ID. Entries in the runtime-effect map are never removed; they only disappear when |
| 347 |
// the context is discarded, which takes the ShaderCodeDictionary along with it. However, they |
| 348 |
// are extremely small (< 20 bytes) so the memory footprint should be unnoticeable. |
| 349 |
using RuntimeEffectMap = skia_private::THashMap<RuntimeEffectKey, int32_t>; |
| 350 |
RuntimeEffectMap fRuntimeEffectMap SK_GUARDED_BY(fSpinLock); |
| 351 |
|
| 352 |
// This arena holds: |
| 353 |
// - the backing data for PaintParamsKeys in `fPaintKeyToID` and `fIDToPaintKey` |
| 354 |
// - Uniform data created by `findOrCreateRuntimeEffectSnippet` |
| 355 |
// and in all cases is guarded by `fSpinLock` |
| 356 |
SkArenaAlloc fArena{256}; |
| 357 |
}; |
| 358 |
|
| 359 |
} // namespace skgpu::graphite |
| 360 |
|
| 361 |
#endif // skgpu_graphite_ShaderCodeDictionary_DEFINED |