| 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_PipelineData_DEFINED |
| 9 |
#define skgpu_graphite_PipelineData_DEFINED |
| 10 |
|
| 11 |
#include "include/core/SkM44.h" |
| 12 |
#include "include/core/SkPoint.h" |
| 13 |
#include "include/core/SkRefCnt.h" |
| 14 |
#include "include/core/SkSamplingOptions.h" |
| 15 |
#include "include/core/SkSpan.h" |
| 16 |
#include "include/core/SkTileMode.h" |
| 17 |
#include "include/private/base/SkTDArray.h" |
| 18 |
#include "src/base/SkArenaAlloc.h" |
| 19 |
#include "src/core/SkColorData.h" |
| 20 |
#include "src/core/SkTHash.h" |
| 21 |
#include "src/gpu/graphite/BufferManager.h" |
| 22 |
#include "src/gpu/graphite/DrawTypes.h" |
| 23 |
#include "src/gpu/graphite/GraphicsPipelineDesc.h" |
| 24 |
#include "src/gpu/graphite/TextureProxy.h" |
| 25 |
#include "src/gpu/graphite/UniformManager.h" |
| 26 |
#include "src/shaders/gradients/SkGradientBaseShader.h" |
| 27 |
|
| 28 |
#include <optional> |
| 29 |
#include <type_traits> |
| 30 |
#include <variant> |
| 31 |
|
| 32 |
namespace skgpu::graphite { |
| 33 |
|
| 34 |
class Uniform; |
| 35 |
|
| 36 |
/** |
| 37 |
* Wraps an SkSpan<const char> and provides ==/!= operators and a hash function based on the |
| 38 |
* bit contents of the spans. It is assumed that the bytes are aligned to match some uniform |
| 39 |
* interface declaration that will consume this data once it's copied to the GPU. |
| 40 |
*/ |
| 41 |
class UniformDataBlock { |
| 42 |
public: |
| 43 |
constexpr UniformDataBlock(const UniformDataBlock&) = default; |
| 44 |
constexpr UniformDataBlock() = default; |
| 45 |
|
| 46 |
static UniformDataBlock Make(UniformDataBlock toClone, SkArenaAlloc* arena) { |
| 47 |
const char* copy = arena->makeArrayCopy<char>(toClone.fData); |
| 48 |
return UniformDataBlock(SkSpan(copy, toClone.size())); |
| 49 |
} |
| 50 |
|
| 51 |
// Wraps the finished accumulated uniform data within the manager's underlying storage. |
| 52 |
static UniformDataBlock Wrap(UniformManager* uniforms) { |
| 53 |
return UniformDataBlock(uniforms->finish()); |
| 54 |
} |
| 55 |
|
| 56 |
static UniformDataBlock WrapNonShading(UniformManager* uniforms) { |
| 57 |
return UniformDataBlock(uniforms->finishMarked()); |
| 58 |
} |
| 59 |
|
| 60 |
constexpr UniformDataBlock& operator=(const UniformDataBlock&) = default; |
| 61 |
|
| 62 |
explicit operator bool() const { return !this->empty(); } |
| 63 |
bool empty() const { return fData.empty(); } |
| 64 |
|
| 65 |
const char* data() const { return fData.data(); } |
| 66 |
size_t size() const { return fData.size(); } |
| 67 |
|
| 68 |
bool operator==(UniformDataBlock that) const { |
| 69 |
return this->size() == that.size() && |
| 70 |
(this->data() == that.data() || // Shortcuts the memcmp if the spans are the same |
| 71 |
memcmp(this->data(), that.data(), this->size()) == 0); |
| 72 |
} |
| 73 |
bool operator!=(UniformDataBlock that) const { return !(*this == that); } |
| 74 |
|
| 75 |
struct Hash { |
| 76 |
uint32_t operator()(UniformDataBlock block) const { |
| 77 |
return SkChecksum::Hash32(block.fData.data(), block.fData.size_bytes()); |
| 78 |
} |
| 79 |
}; |
| 80 |
|
| 81 |
private: |
| 82 |
// To ensure that the underlying data is actually aligned properly, UniformDataBlocks can |
| 83 |
// only be created publicly by copying an existing block or wrapping data accumulated by a |
| 84 |
// UniformManager (or transitively a PipelineDataGatherer). |
| 85 |
constexpr UniformDataBlock(SkSpan<const char> data) : fData(data) {} |
| 86 |
|
| 87 |
SkSpan<const char> fData; |
| 88 |
}; |
| 89 |
|
| 90 |
/** |
| 91 |
* Wraps an SkSpan<const SampledTexture> (list of pairs of TextureProxy and SamplerDesc) and |
| 92 |
* provides ==/!= operators and a hash function based on the proxy addresses and sampler desc |
| 93 |
* bit representation. |
| 94 |
*/ |
| 95 |
class TextureDataBlock { |
| 96 |
public: |
| 97 |
using SampledTexture = std::pair<sk_sp<TextureProxy>, SamplerDesc>; |
| 98 |
|
| 99 |
constexpr TextureDataBlock(const TextureDataBlock&) = default; |
| 100 |
constexpr TextureDataBlock() = default; |
| 101 |
|
| 102 |
static TextureDataBlock Make(TextureDataBlock toClone, SkArenaAlloc* arena) { |
| 103 |
SampledTexture* copy = arena->makeArrayCopy<SampledTexture>(toClone.fTextures); |
| 104 |
return TextureDataBlock(SkSpan(copy, toClone.numTextures())); |
| 105 |
} |
| 106 |
|
| 107 |
// TODO(b/330864257): Once Device::drawCoverageMask() can keep its texture proxy alive without |
| 108 |
// creating a temporary TextureDataBlock this constructor can go away. |
| 109 |
explicit TextureDataBlock(const SampledTexture& texture) : fTextures(&texture, 1) {} |
| 110 |
|
| 111 |
constexpr TextureDataBlock& operator=(const TextureDataBlock&) = default; |
| 112 |
|
| 113 |
explicit operator bool() const { return !this->empty(); } |
| 114 |
bool empty() const { return fTextures.empty(); } |
| 115 |
|
| 116 |
int numTextures() const { return SkTo<int>(fTextures.size()); } |
| 117 |
const SampledTexture& texture(int index) const { return fTextures[index]; } |
| 118 |
|
| 119 |
bool operator==(TextureDataBlock other) const { |
| 120 |
if (fTextures.size() != other.fTextures.size()) { |
| 121 |
return false; |
| 122 |
} |
| 123 |
if (fTextures.data() == other.fTextures.data()) { |
| 124 |
return true; // shortcut for the same span |
| 125 |
} |
| 126 |
|
| 127 |
for (size_t i = 0; i < fTextures.size(); ++i) { |
| 128 |
if (fTextures[i] != other.fTextures[i]) { |
| 129 |
return false; |
| 130 |
} |
| 131 |
} |
| 132 |
|
| 133 |
return true; |
| 134 |
} |
| 135 |
bool operator!=(TextureDataBlock other) const { return !(*this == other); } |
| 136 |
|
| 137 |
struct Hash { |
| 138 |
uint32_t operator()(TextureDataBlock block) const { |
| 139 |
uint32_t hash = 0; |
| 140 |
|
| 141 |
for (auto& d : block.fTextures) { |
| 142 |
SamplerDesc samplerKey = std::get<1>(d); |
| 143 |
hash = SkChecksum::Hash32(&samplerKey, sizeof(samplerKey), hash); |
| 144 |
|
| 145 |
// Because the lifetime of the TextureDataCache is for just one Recording and the |
| 146 |
// TextureDataBlocks hold refs on their proxies, we can just use the proxy's pointer |
| 147 |
// for the hash here. |
| 148 |
uintptr_t proxy = reinterpret_cast<uintptr_t>(std::get<0>(d).get()); |
| 149 |
hash = SkChecksum::Hash32(&proxy, sizeof(proxy), hash); |
| 150 |
} |
| 151 |
|
| 152 |
return hash; |
| 153 |
} |
| 154 |
}; |
| 155 |
|
| 156 |
private: |
| 157 |
friend class PipelineDataGatherer; |
| 158 |
|
| 159 |
// Initial TextureDataBlocks must come from a PipelineDataGatherer |
| 160 |
constexpr TextureDataBlock(SkSpan<const SampledTexture> textures) : fTextures(textures) {} |
| 161 |
|
| 162 |
SkSpan<const SampledTexture> fTextures; |
| 163 |
}; |
| 164 |
|
| 165 |
template <typename K, // Initial type inserted into the map from K->Index |
| 166 |
typename V = K, // Value type stored in contiguous memory (map from Index->V) |
| 167 |
typename S = std::monostate, // Storage with persist(K) -> K function if not monostate |
| 168 |
typename H = SkGoodHash> // Hash function applied to K |
| 169 |
class DenseBiMap { |
| 170 |
public: |
| 171 |
using Index = uint32_t; |
| 172 |
// 1 << SkNextLog2_portable(DrawList::kMaxRenderSteps); |
| 173 |
static constexpr Index kInvalidIndex = 4096; |
| 174 |
|
| 175 |
Index insert(K data) { |
| 176 |
Index* index = fDataToIndex.find(data); |
| 177 |
if (!index) { |
| 178 |
// First time we've seen this piece of data. |
| 179 |
SkASSERT(SkToU32(fIndexToData.size()) < kInvalidIndex); |
| 180 |
// Persist it in storage if S is not monostate. |
| 181 |
if constexpr (!std::is_same_v<S, std::monostate>) { |
| 182 |
data = fStorage.persist(data); |
| 183 |
} else { |
| 184 |
static_assert(std::is_trivially_copyable<K>::value); |
| 185 |
} |
| 186 |
|
| 187 |
index = fDataToIndex.set(data, static_cast<Index>(fIndexToData.size())); |
| 188 |
fIndexToData.emplace_back(data); // constructs V in place from single K argument |
| 189 |
} |
| 190 |
return *index; |
| 191 |
} |
| 192 |
|
| 193 |
bool contains(K data) const { return SkToBool(fDataToIndex.find(data)); } |
| 194 |
|
| 195 |
const V& lookup(Index index) const { return fIndexToData[index]; } |
| 196 |
|
| 197 |
V& lookup(Index index) { return fIndexToData[index]; } |
| 198 |
|
| 199 |
skia_private::TArray<V>&& detach() { return std::move(fIndexToData); } |
| 200 |
|
| 201 |
const skia_private::TArray<V>& get() const { return fIndexToData; } |
| 202 |
|
| 203 |
void reset() { |
| 204 |
fIndexToData.clear(); |
| 205 |
fDataToIndex.reset(); |
| 206 |
if constexpr (!std::is_same_v<S, std::monostate>) { |
| 207 |
fStorage.~S(); |
| 208 |
new (&fStorage) S(); |
| 209 |
} |
| 210 |
} |
| 211 |
|
| 212 |
int count() const { return fIndexToData.size(); } |
| 213 |
|
| 214 |
template <typename SV = S> |
| 215 |
typename std::enable_if<!std::is_same_v<SV, std::monostate>, const SV&>::type |
| 216 |
storage() const { return fStorage; } |
| 217 |
|
| 218 |
template <typename SV = S> |
| 219 |
typename std::enable_if<!std::is_same_v<SV, std::monostate>, SV&>::type |
| 220 |
storage() { return fStorage; } |
| 221 |
|
| 222 |
private: |
| 223 |
skia_private::THashMap<K, Index, H> fDataToIndex; |
| 224 |
skia_private::TArray<V> fIndexToData; |
| 225 |
|
| 226 |
S fStorage; |
| 227 |
}; |
| 228 |
|
| 229 |
// A GraphicsPipelineCache is used to de-duplicate GraphicsPipelineDescs when they will all be |
| 230 |
// used with the same as-yet-undeterminied RenderPassDesc. Once collected the indices can then be |
| 231 |
// used to map to the resolved GraphicsPipelines after resources have been prepared. |
| 232 |
using GraphicsPipelineCache = DenseBiMap<GraphicsPipelineDesc>; |
| 233 |
|
| 234 |
// A UniformDataCache is used to deduplicate uniform data blocks uploaded to uniform / storage |
| 235 |
// buffers for a DrawPass pipeline. |
| 236 |
class UniformDataCache { |
| 237 |
public: |
| 238 |
// Tracks uniform data on the CPU and then its transition to storage in a GPU buffer (UBO or |
| 239 |
// SSBO). |
| 240 |
struct Entry { |
| 241 |
UniformDataBlock fCpuData; |
| 242 |
BindBufferInfo fBufferBinding; |
| 243 |
|
| 244 |
// Can only be initialized with CPU data. |
| 245 |
Entry(UniformDataBlock cpuData) : fCpuData(cpuData) {} |
| 246 |
}; |
| 247 |
|
| 248 |
private: |
| 249 |
struct UniformCopier { |
| 250 |
UniformDataBlock persist(UniformDataBlock data) { |
| 251 |
return UniformDataBlock::Make(data, &fArena); |
| 252 |
} |
| 253 |
SkArenaAlloc fArena{0}; |
| 254 |
}; |
| 255 |
using UniformDataMap = |
| 256 |
DenseBiMap<UniformDataBlock, Entry, UniformCopier, UniformDataBlock::Hash>; |
| 257 |
|
| 258 |
UniformDataMap fUniforms; |
| 259 |
|
| 260 |
public: |
| 261 |
using Index = UniformDataMap::Index; |
| 262 |
static constexpr Index kInvalidIndex = UniformDataMap::kInvalidIndex; |
| 263 |
|
| 264 |
UniformDataCache() = default; |
| 265 |
|
| 266 |
void reset() { fUniforms.reset(); } |
| 267 |
|
| 268 |
Index insert(UniformDataBlock dataBlock) { return fUniforms.insert(dataBlock); } |
| 269 |
|
| 270 |
const Entry& lookup(Index index) const { return fUniforms.lookup(index); } |
| 271 |
|
| 272 |
Entry& lookup(Index index) { return fUniforms.lookup(index); } |
| 273 |
|
| 274 |
#if defined(GPU_TEST_UTILS) |
| 275 |
int count() { return fUniforms.count(); } |
| 276 |
#endif |
| 277 |
}; |
| 278 |
|
| 279 |
// A TextureDataCache is used to deduplicate sets of texture bindings and collect the list of |
| 280 |
// unique texture proxies that are referenced by all inserted bindings. |
| 281 |
class TextureDataCache { |
| 282 |
struct TakeTextureRef { |
| 283 |
TextureProxy* persist(TextureProxy* proxy) { |
| 284 |
// This ref will be adopted by the sk_sp() value stored in the TextureProxyCache. |
| 285 |
proxy->ref(); |
| 286 |
return proxy; |
| 287 |
} |
| 288 |
}; |
| 289 |
using TextureProxyCache = DenseBiMap<TextureProxy*, sk_sp<TextureProxy>, TakeTextureRef>; |
| 290 |
|
| 291 |
struct TextureCopier { |
| 292 |
TextureDataBlock persist(TextureDataBlock textures) { |
| 293 |
// Insert every referenced texture into fUniqueTextures to hand off to DrawPass. |
| 294 |
for (int i = 0; i < textures.numTextures(); ++i) { |
| 295 |
(void) fUniqueTextures.insert(textures.texture(i).first.get()); |
| 296 |
} |
| 297 |
|
| 298 |
// Confirm that we're getting the right value back. |
| 299 |
#if defined(SK_DEBUG) |
| 300 |
auto t = TextureDataBlock::Make(textures, &fArena); |
| 301 |
SkASSERT(textures == t); |
| 302 |
return t; |
| 303 |
#else |
| 304 |
return TextureDataBlock::Make(textures, &fArena); |
| 305 |
#endif |
| 306 |
} |
| 307 |
|
| 308 |
SkArenaAlloc fArena{0}; |
| 309 |
TextureProxyCache fUniqueTextures; |
| 310 |
}; |
| 311 |
using TextureDataMap = DenseBiMap<TextureDataBlock, |
| 312 |
TextureDataBlock, |
| 313 |
TextureCopier, |
| 314 |
TextureDataBlock::Hash>; |
| 315 |
TextureDataMap fTextures; |
| 316 |
|
| 317 |
public: |
| 318 |
using Index = TextureDataMap::Index; |
| 319 |
static constexpr Index kInvalidIndex = TextureDataMap::kInvalidIndex; |
| 320 |
|
| 321 |
TextureDataCache() = default; |
| 322 |
|
| 323 |
void reset() { fTextures.reset(); } |
| 324 |
|
| 325 |
Index insert(TextureDataBlock dataBlock) { return fTextures.insert(dataBlock); } |
| 326 |
|
| 327 |
TextureDataBlock lookup(Index index) const { return fTextures.lookup(index); } |
| 328 |
|
| 329 |
bool hasTexture(const TextureProxy* texture) const { |
| 330 |
// The template for TextureProxyCache uses `TextureProxy*` because `sk_sp` does not |
| 331 |
// take a const pointer; this contains() check just uses the address and doesn't do |
| 332 |
// anything that actually requires it to be non-const. |
| 333 |
return fTextures.storage().fUniqueTextures.contains( |
| 334 |
const_cast<TextureProxy*>(texture)); |
| 335 |
} |
| 336 |
|
| 337 |
skia_private::TArray<sk_sp<TextureProxy>> detachTextures() { |
| 338 |
return fTextures.storage().fUniqueTextures.detach(); |
| 339 |
} |
| 340 |
|
| 341 |
const skia_private::TArray<TextureDataBlock>& getBindings() const { |
| 342 |
return fTextures.get(); |
| 343 |
} |
| 344 |
|
| 345 |
#if defined(GPU_TEST_UTILS) |
| 346 |
int bindingCount() { return fTextures.count(); } |
| 347 |
int uniqueTextureCount() { return fTextures.storage().fUniqueTextures.count(); } |
| 348 |
#endif |
| 349 |
}; |
| 350 |
|
| 351 |
class PipelineDataGatherer { |
| 352 |
public: |
| 353 |
PipelineDataGatherer(Layout layout) : fUniformManager(layout) {} |
| 354 |
|
| 355 |
// Fully resets both uniforms (paint and renderstep)and textures |
| 356 |
void resetForDraw() { |
| 357 |
fUniformManager.reset(); |
| 358 |
fTextures.clear(); |
| 359 |
fPaintTextureCount = 0; |
| 360 |
} |
| 361 |
|
| 362 |
#if defined(SK_DEBUG) |
| 363 |
// Check that the gatherer has been reset to its initial state prior to collecting new data. |
| 364 |
void checkReset() const { |
| 365 |
SkASSERT(fTextures.empty()); |
| 366 |
SkASSERT(fUniformManager.isReset()); |
| 367 |
SkASSERT(fPaintTextureCount == 0); |
| 368 |
} |
| 369 |
|
| 370 |
void checkRewind() const { |
| 371 |
SkASSERT(fTextures.size() == fPaintTextureCount); |
| 372 |
} |
| 373 |
#endif // SK_DEBUG |
| 374 |
|
| 375 |
// If a renderstep performs shading, then alignment should occur on the combined |
| 376 |
// paint+renderstep, so no alignment is required and we simply mark the end of the paints. Else |
| 377 |
// we need to align whatever is cuurrently stored to the renderstep's uniform alignment. |
| 378 |
void markOffsetAndAlign(bool performsShading, int requiredAlignment) { |
| 379 |
fPaintTextureCount = fTextures.size(); |
| 380 |
fUniformManager.markOffset(); |
| 381 |
if (!performsShading) { |
| 382 |
fUniformManager.alignForNonShading(requiredAlignment); |
| 383 |
} |
| 384 |
} |
| 385 |
|
| 386 |
// Rewind to collect data for another RenderStep using the same paint data. |
| 387 |
void rewindForRenderStep() { |
| 388 |
fTextures.resize_back(fPaintTextureCount); |
| 389 |
fUniformManager.rewindToMark(); |
| 390 |
} |
| 391 |
|
| 392 |
// Mark the end of extracting uniforms and textures from a RenderStep. |
| 393 |
std::pair<UniformDataBlock, TextureDataBlock> endCombinedData(bool performsShading) { |
| 394 |
SkSpan<const TextureDataBlock::SampledTexture> textures{fTextures}; |
| 395 |
if (performsShading) { |
| 396 |
// Return paint AND renderstep uniforms written since the last resetForDraw. |
| 397 |
return {UniformDataBlock::Wrap(&fUniformManager), TextureDataBlock(textures)}; |
| 398 |
} else { |
| 399 |
textures = textures.subspan(fPaintTextureCount); |
| 400 |
return {UniformDataBlock::WrapNonShading(&fUniformManager), TextureDataBlock(textures)}; |
| 401 |
} |
| 402 |
} |
| 403 |
|
| 404 |
// Append a sampled texture. |
| 405 |
void add(sk_sp<TextureProxy> proxy, const SamplerDesc& samplerDesc) { |
| 406 |
fTextures.push_back({std::move(proxy), samplerDesc}); |
| 407 |
} |
| 408 |
|
| 409 |
void tryShrinkCapacity() { |
| 410 |
SkDEBUGCODE(this->checkReset()); |
| 411 |
fUniformManager.tryShrinkCapacity(); |
| 412 |
} |
| 413 |
|
| 414 |
// Mimic the type-safe API available in UniformManager |
| 415 |
template <typename T> void write(const T& t) { fUniformManager.write(t); } |
| 416 |
template <typename T> void writeHalf(const T& t) { fUniformManager.writeHalf(t); } |
| 417 |
template <typename T> void writeArray(SkSpan<const T> t) { fUniformManager.writeArray(t); } |
| 418 |
template <typename T> void writeHalfArray(SkSpan<const T> t) { |
| 419 |
fUniformManager.writeHalfArray(t); |
| 420 |
} |
| 421 |
void write(const Uniform& u, const void* data) { fUniformManager.write(u, data); } |
| 422 |
void writePaintColor(const SkPMColor4f& color) { fUniformManager.writePaintColor(color); } |
| 423 |
void beginStruct(int baseAligment) { fUniformManager.beginStruct(baseAligment); } |
| 424 |
void endStruct() { fUniformManager.endStruct(); } |
| 425 |
|
| 426 |
SkDEBUGCODE(UniformManager* uniformManager() { return &fUniformManager; }) |
| 427 |
private: |
| 428 |
SkDEBUGCODE(friend class UniformExpectationsValidator;) |
| 429 |
|
| 430 |
UniformManager fUniformManager; |
| 431 |
skia_private::TArray<TextureDataBlock::SampledTexture> fTextures; |
| 432 |
|
| 433 |
int fPaintTextureCount = 0; |
| 434 |
}; |
| 435 |
|
| 436 |
#ifdef SK_DEBUG |
| 437 |
class UniformExpectationsValidator { |
| 438 |
public: |
| 439 |
UniformExpectationsValidator(PipelineDataGatherer* gatherer, |
| 440 |
SkSpan<const Uniform> expectedUniforms, |
| 441 |
bool isSubstruct = false) |
| 442 |
: fManager(gatherer->uniformManager()) { |
| 443 |
fManager->setExpectedUniforms(expectedUniforms, isSubstruct); |
| 444 |
} |
| 445 |
|
| 446 |
~UniformExpectationsValidator() { |
| 447 |
fManager->doneWithExpectedUniforms(); |
| 448 |
} |
| 449 |
|
| 450 |
private: |
| 451 |
UniformManager* fManager; |
| 452 |
|
| 453 |
UniformExpectationsValidator(UniformExpectationsValidator&&) = delete; |
| 454 |
UniformExpectationsValidator(const UniformExpectationsValidator&) = delete; |
| 455 |
UniformExpectationsValidator& operator=(UniformExpectationsValidator&&) = delete; |
| 456 |
UniformExpectationsValidator& operator=(const UniformExpectationsValidator&) = delete; |
| 457 |
}; |
| 458 |
#endif // SK_DEBUG |
| 459 |
|
| 460 |
/** |
| 461 |
* Aggregates gradient color and stop information into a single buffer to be bound once for a |
| 462 |
* DrawPass. It de-duplicates gradient data by caching based on the SkGradientBaseShader pointer. |
| 463 |
*/ |
| 464 |
class FloatStorageManager : public SkRefCnt { |
| •465 |
// Size limit for individual gradients (anything larger will be dropped) |
| 466 |
static constexpr int kMaxGradientStops = 1024 * 1024; // ~5MB of data in the shader |
| 467 |
|
| 468 |
// Size limit for the max buffer size. If a new draw would exceed this limit, we drop the draw. |
| 469 |
// The float storage manager is used by all Devices in a Recorder and is reset at snap(), |
| 470 |
// requiring a global flush to otherwise get a new buffer (which is undesirable). Instead, we |
| 471 |
// assume that exceeding this limit happens in two situations: |
| 472 |
// 1. Adversarial content, at which point correctness is not critical. |
| 473 |
// 2. A truly bespoke application requiring 4GB of gradient color data should be having its |
| 474 |
// workload managed at the application level where it can snap Recordings. |
| 475 |
// |
| 476 |
// It is also likely that even if we accumulate this much CPU data, a GPU driver will fail to |
| 477 |
// create a buffer for us to copy to, causing the snap() to fail. |
| 478 |
static constexpr int kMaxStorageFloats = |
| 479 |
static_cast<int>(std::numeric_limits<uint32_t>::max() / sizeof(float)); |
| 480 |
static_assert(std::numeric_limits<uint32_t>::max() / sizeof(float) |
| 481 |
<= (uint32_t) std::numeric_limits<int>::max()); |
| 482 |
public: |
| •483 |
FloatStorageManager() { this->reset(); } |
| 484 |
|
| 485 |
void reset() { |
| •486 |
// Remove the manually added refs on the keys in fGradientOffsetCache |
| 487 |
for (auto [k, _] : fGradientOffsetCache) { |
| 488 |
k->unref(); // k might be deleted at this point but we won't use it anymore |
| 489 |
} |
| 490 |
fGradientStorage.clear(); |
| 491 |
fGradientOffsetCache.reset(); |
| 492 |
} |
| 493 |
|
| 494 |
// Checks if data already exists for the requested gradient shader. If so, it returns |
| 495 |
// a nullptr and the existing offset. If not, it allocates space, caches the offset, |
| 496 |
// and returns a pointer to the start of the new data and the calculated offset. |
| •497 |
// |
| 498 |
// If it was not possible to store the gradient data, a nullptr and negative offset |
| 499 |
// are returned to signal the error state. |
| 500 |
std::pair<float*, int> allocateGradientData(int numStops, const SkGradientBaseShader* shader) { |
| 501 |
SkASSERT(!this->isFinalized()); |
| •502 |
if (numStops > kMaxGradientStops) { |
| 503 |
return {nullptr, -1}; |
| 504 |
} |
| 505 |
|
| •506 |
int* existingOffset = fGradientOffsetCache.find(shader); |
| 507 |
if (existingOffset) { |
| 508 |
return {nullptr, *existingOffset}; |
| 509 |
} |
| 510 |
auto [ptr, offset] = this->allocateFloatData(numStops * 5); // 4 for color, 1 for offset |
| •511 |
|
| 512 |
// Only cache the storage if it was allocated successfully. |
| 513 |
if (ptr) { |
| 514 |
SkASSERT(offset >= 0); |
| •515 |
// Since FloatStorageManager is single threaded, adding a ref and then storing in the |
| 516 |
// map should be fine. |
| 517 |
shader->ref(); |
| 518 |
fGradientOffsetCache.set(shader, offset); |
| 519 |
} |
| 520 |
|
| 521 |
return {ptr, offset}; |
| 522 |
} |
| 523 |
|
| 524 |
bool finalize(DrawBufferManager* bufferMgr) { |
| 525 |
SkASSERT(!this->isFinalized()); |
| 526 |
if (!fGradientStorage.empty()) { |
| •527 |
SkASSERT(fGradientStorage.size() <= kMaxStorageFloats); |
| 528 |
auto [writer, bufferInfo, _] = |
| 529 |
bufferMgr->getMappedStorageBuffer(fGradientStorage.size(), sizeof(float)); |
| 530 |
if (writer) { |
| 531 |
writer.write(fGradientStorage.data(), fGradientStorage.size_bytes()); |
| 532 |
fBufferInfo = bufferInfo; |
| 533 |
this->reset(); |
| 534 |
} else { |
| 535 |
return false; |
| 536 |
} |
| 537 |
} else { |
| 538 |
fBufferInfo = BindBufferInfo(); |
| 539 |
} |
| 540 |
return true; |
| 541 |
} |
| 542 |
|
| 543 |
BindBufferInfo getBufferInfo() { return fBufferInfo.value(); } |
| 544 |
bool hasData() const { return fBufferInfo.has_value() && |
| 545 |
fBufferInfo.value().fBuffer != nullptr; } |
| 546 |
SkDEBUGCODE(bool isFinalized() const { return fBufferInfo.has_value(); }) |
| 547 |
private: |
| 548 |
// Allocates space for a given number of floats and returns a pointer to the start |
| 549 |
// of the new allocation and its offset from the beginning of the buffer. |
| 550 |
std::pair<float*, int> allocateFloatData(int floatCount) { |
| 551 |
int currentSize = fGradientStorage.size(); |
| •552 |
if (kMaxStorageFloats - floatCount < currentSize) { |
| 553 |
return {nullptr, -1}; // We've accumulated too much |
| 554 |
} |
| 555 |
fGradientStorage.resize(currentSize + floatCount); |
| 556 |
float* startPtr = fGradientStorage.begin() + currentSize; |
| 557 |
|
| 558 |
return {startPtr, currentSize}; |
| 559 |
} |
| 560 |
|
| 561 |
// NOTE: This storage aggregates all data required by all draws within a DrawPass so that its |
| 562 |
// storage buffer can be bound once and accessed at random. |
| 563 |
SkTDArray<float> fGradientStorage; |
| 564 |
|
| •565 |
// We use the shader's address as a key to de-duplicate gradient data. Each key has a ref added |
| 566 |
// when it's first put in the map. The map does not key off of sk_sp<SkGradientBaseShader> to as |
| 567 |
// that is not compatible with SkGoodHash. These refs are dropped in reset(). While this extends |
| 568 |
// the lifetime of the SkShaders, it only applies to large gradients. Hopefully clients are |
| 569 |
// trying to reuse such shaders across frames already. |
| 570 |
// |
| 571 |
// If we didn't keep the shaders alive, we'd have to worry about cache collisions from |
| 572 |
// re-allocations using the same address. Using a unique ID can wrap, leading to potential |
| 573 |
// mismatches. Adding sufficient data to eliminate this risk (e.g. keying off the unique ID, the |
| 574 |
// address, the number of color stops, AND a hash of the color data) is likely more expensive |
| 575 |
// than taking a ref. |
| 576 |
skia_private::THashMap<const SkGradientBaseShader*, int> fGradientOffsetCache; |
| 577 |
|
| 578 |
std::optional<BindBufferInfo> fBufferInfo = std::nullopt; |
| 579 |
}; |
| 580 |
|
| 581 |
} // namespace skgpu::graphite |
| 582 |
|
| 583 |
#endif // skgpu_graphite_PipelineData_DEFINED |