| 1 |
/* |
| 2 |
* Copyright 2021 Google Inc. |
| 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 |
#include "src/gpu/graphite/BufferManager.h" |
| 8 |
|
| 9 |
#include "include/gpu/GpuTypes.h" |
| 10 |
#include "include/gpu/graphite/Recording.h" |
| 11 |
#include "include/private/base/SkAlign.h" |
| 12 |
#include "include/private/base/SkAssert.h" |
| 13 |
#include "include/private/base/SkMath.h" |
| 14 |
#include "include/private/base/SkTo.h" |
| 15 |
#include "src/gpu/graphite/Caps.h" |
| 16 |
#include "src/gpu/graphite/GlobalCache.h" |
| 17 |
#include "src/gpu/graphite/Log.h" |
| 18 |
#include "src/gpu/graphite/QueueManager.h" |
| 19 |
#include "src/gpu/graphite/RecordingPriv.h" |
| 20 |
#include "src/gpu/graphite/Resource.h" |
| 21 |
#include "src/gpu/graphite/ResourceProvider.h" |
| 22 |
#include "src/gpu/graphite/UploadBufferManager.h" |
| 23 |
#include "src/gpu/graphite/task/ClearBuffersTask.h" |
| 24 |
#include "src/gpu/graphite/task/CopyTask.h" |
| 25 |
#include "src/gpu/graphite/task/Task.h" |
| 26 |
#include "src/gpu/graphite/task/TaskList.h" |
| 27 |
|
| 28 |
#include <algorithm> |
| 29 |
#include <cstddef> |
| 30 |
#include <cstring> |
| 31 |
#include <limits> |
| 32 |
#include <numeric> |
| 33 |
#include <tuple> |
| 34 |
|
| 35 |
namespace skgpu::graphite { |
| 36 |
|
| 37 |
namespace { |
| 38 |
|
| 39 |
// The limit for all data created by the StaticBufferManager. This data remains alive for |
| 40 |
// the entire SharedContext so we want to keep it small and give a concrete upper bound to |
| 41 |
// clients for our steady-state memory usage. |
| 42 |
// FIXME The current usage is 4732 bytes across static vertex and index buffers, but that includes |
| 43 |
// multiple copies of tessellation data, and an unoptimized AnalyticRRect mesh. Once those issues |
| 44 |
// are addressed, we can tighten this and decide on the transfer buffer sizing as well. |
| 45 |
[[maybe_unused]] static constexpr uint32_t kMaxStaticDataSize = 6 << 10; |
| 46 |
|
| 47 |
uint32_t validate_count_and_stride(size_t count, size_t stride, uint32_t alignment) { |
| 48 |
// size_t may just be uint32_t, so this ensures we have enough bits to |
| 49 |
// compute the required byte product. |
| 50 |
uint64_t count64 = SkTo<uint64_t>(count); |
| 51 |
uint64_t stride64 = SkTo<uint64_t>(stride); |
| 52 |
uint64_t bytes64 = count64*stride64; |
| 53 |
if (count64 > std::numeric_limits<uint32_t>::max() || |
| 54 |
stride64 > std::numeric_limits<uint32_t>::max() || |
| 55 |
bytes64 > std::numeric_limits<uint32_t>::max() - (alignment + 1)) { |
| 56 |
// Return 0 to skip further allocation attempts. |
| 57 |
return 0; |
| 58 |
} |
| 59 |
// Since count64 and stride64 fit into 32-bits, their product won't overflow a 64-bit multiply, |
| 60 |
// and we've confirmed product fits into 32-bits with head room to be aligned w/o overflow. |
| 61 |
return SkTo<uint32_t>(bytes64); |
| 62 |
} |
| 63 |
|
| 64 |
// Calculates the LCM of `alignMaybePow2` and `alignProbNonPow2`. Neither value needs to be a |
| 65 |
// power of 2, but this is optimized to check for whether or not `alignMaybePow2` is a power of 2. |
| 66 |
// It assumes the probability of the 2nd alignment value being a power of 2 is low enough to not |
| 67 |
// be worth checking. |
| 68 |
uint32_t lcm_alignment(uint32_t alignMaybePow2, uint32_t alignProbNonPow2) { |
| 69 |
SkASSERT(alignMaybePow2 != 0 && alignProbNonPow2 != 0); |
| 70 |
if (alignMaybePow2 == 1 || |
| 71 |
alignMaybePow2 == alignProbNonPow2 || |
| 72 |
(SkIsPow2(alignMaybePow2) && |
| 73 |
alignProbNonPow2 > alignMaybePow2 && |
| 74 |
(alignProbNonPow2 & (alignMaybePow2 - 1)) == 0)) { |
| 75 |
// Trivial LCM since alignProbNonPow2 is the same or a larger multiple of alignMaybePow2 |
| 76 |
return alignProbNonPow2; |
| 77 |
} else { |
| 78 |
return std::lcm(alignMaybePow2, alignProbNonPow2); |
| 79 |
} |
| 80 |
} |
| 81 |
|
| 82 |
// Helpers for creating a BufferState based on type, options, and caps |
| 83 |
|
| 84 |
AccessPattern get_gpu_access_pattern(bool isGpuOnlyAccess, const DrawBufferManager::Options& opts) { |
| 85 |
if (isGpuOnlyAccess) { |
| 86 |
#if defined(GPU_TEST_UTILS) |
| 87 |
if (opts.fAllowCopyingGpuOnly) { |
| 88 |
return AccessPattern::kGpuOnlyCopySrc; |
| 89 |
} |
| 90 |
#endif |
| 91 |
return AccessPattern::kGpuOnly; |
| 92 |
} else { |
| 93 |
return AccessPattern::kHostVisible; |
| 94 |
} |
| 95 |
} |
| 96 |
|
| 97 |
// This returns the minimum required alignment depending on the type of buffer. This is guaranteed |
| 98 |
// to be a power of two. |
| 99 |
uint32_t minimum_alignment(BufferType type, bool useTransferBuffers, const Caps* caps) { |
| 100 |
uint32_t alignment = 4; |
| 101 |
if (type == BufferType::kUniform) { |
| 102 |
alignment = SkTo<uint32_t>(caps->requiredUniformBufferAlignment()); |
| 103 |
} else if (type == BufferType::kStorage || type == BufferType::kVertexStorage || |
| 104 |
type == BufferType::kIndexStorage || type == BufferType::kIndirect) { |
| 105 |
alignment = SkTo<uint32_t>(caps->requiredStorageBufferAlignment()); |
| 106 |
} |
| 107 |
if (useTransferBuffers) { |
| 108 |
// Both alignment and the requiredTransferBufferAlignment must be powers of two, so max |
| 109 |
// provides the correct alignment semantics |
| 110 |
alignment = std::max(alignment, SkTo<uint32_t>(caps->requiredTransferBufferAlignment())); |
| 111 |
} |
| 112 |
return alignment; |
| 113 |
} |
| 114 |
|
| 115 |
uint32_t min_block_size(BufferType type, |
| 116 |
uint32_t minAlignment, |
| 117 |
const DrawBufferManager::Options& opts) { |
| 118 |
uint32_t size; |
| 119 |
if (type == BufferType::kIndex || type == BufferType::kIndexStorage) { |
| 120 |
size = opts.fIndexBufferSize; |
| 121 |
} else if (type == BufferType::kVertex || type == BufferType::kVertexStorage) { |
| 122 |
size = opts.fVertexBufferMinSize; |
| 123 |
} else { |
| 124 |
size = opts.fStorageBufferMinSize; |
| 125 |
} |
| 126 |
#if defined(GPU_TEST_UTILS) |
| 127 |
if (opts.fUseExactBuffSizes) { |
| 128 |
return size; // No extra alignment |
| 129 |
} |
| 130 |
#endif |
| 131 |
|
| 132 |
return SkAlignTo(size, minAlignment); |
| 133 |
} |
| 134 |
|
| 135 |
uint32_t max_block_size(BufferType type, |
| 136 |
uint32_t minAlignment, |
| 137 |
const DrawBufferManager::Options& opts) { |
| 138 |
#if defined(GPU_TEST_UTILS) |
| 139 |
if (opts.fUseExactBuffSizes) { |
| 140 |
// Clamp to the minimum size |
| 141 |
return min_block_size(type, minAlignment, opts); |
| 142 |
} |
| 143 |
#endif |
| 144 |
|
| 145 |
uint32_t size; |
| 146 |
if (type == BufferType::kIndex || type == BufferType::kIndexStorage) { |
| 147 |
size = opts.fIndexBufferSize; |
| 148 |
} else if (type == BufferType::kVertex || type == BufferType::kVertexStorage) { |
| 149 |
size = opts.fVertexBufferMaxSize; |
| 150 |
} else { |
| 151 |
size = opts.fStorageBufferMaxSize; |
| 152 |
} |
| 153 |
|
| 154 |
return SkAlignTo(size, minAlignment); |
| 155 |
} |
| 156 |
|
| 157 |
} // anonymous namespace |
| 158 |
|
| 159 |
// ------------------------------------------------------------------------------------------------ |
| 160 |
// BufferSubAllocator |
| 161 |
|
| 162 |
BufferSubAllocator::BufferSubAllocator(DrawBufferManager* owner, |
| 163 |
int stateIndex, |
| 164 |
sk_sp<Buffer> buffer, |
| 165 |
BindBufferInfo transferBuffer, |
| 166 |
void* mappedPtr, |
| 167 |
size_t stride) |
| 168 |
: fOwner(owner) |
| 169 |
, fStateIndex(stateIndex) |
| 170 |
, fBuffer(std::move(buffer)) |
| 171 |
, fTransferBuffer(transferBuffer) |
| 172 |
, fMappedPtr(mappedPtr) |
| 173 |
, fOffset(0) |
| 174 |
, fStride(SkTo<uint32_t>(stride)) { |
| 175 |
// A starting offset of 0 means we don't need to explicitly lookup the minimum binding alignment |
| 176 |
// since the first sub-allocation is automatically aligned. |
| 177 |
if (fBuffer && fStride > 0 && fStride <= fBuffer->size()) { |
| 178 |
// This puts the BufferSubAllocator in the same state as prepForStride(stride, *) |
| 179 |
fRemaining = SkTo<uint32_t>(fBuffer->size()) / fStride; |
| 180 |
} else { |
| 181 |
fRemaining = 0; |
| 182 |
} |
| 183 |
|
| 184 |
SkASSERT(fStride != 0); |
| 185 |
} |
| 186 |
|
| 187 |
BufferSubAllocator& BufferSubAllocator::operator=(BufferSubAllocator&& other) { |
| 188 |
if (this == &other) { |
| 189 |
return *this; // no-op moving into itself |
| 190 |
} |
| 191 |
|
| 192 |
// Reset the destination allocator first since other's contents will overwrite whatever came |
| 193 |
// beforehand and that must go back to the manager. |
| 194 |
this->reset(); |
| 195 |
|
| 196 |
// Copy fields |
| 197 |
fOwner = other.fOwner; |
| 198 |
fStateIndex = other.fStateIndex; |
| 199 |
fTransferBuffer = other.fTransferBuffer; |
| 200 |
fMappedPtr = other.fMappedPtr; |
| 201 |
fOffset = other.fOffset; |
| 202 |
fStride = other.fStride; |
| 203 |
fRemaining = other.fRemaining; |
| 204 |
|
| 205 |
// Move buffer (leaving other in an invalid state) |
| 206 |
fBuffer = std::move(other.fBuffer); |
| 207 |
SkASSERT(!other); |
| 208 |
return *this; |
| 209 |
} |
| 210 |
|
| 211 |
void BufferSubAllocator::prepForStride(size_t stride, size_t align, size_t minCount) { |
| 212 |
SkASSERT(stride > 0 && align > 0); // Expect valid inputs |
| 213 |
if (fBuffer) { |
| 214 |
if (fStride == stride && (align == 1 || align == stride)) { |
| 215 |
// Shortcut if we're already aligned with the last call to prepForStride(). |
| 216 |
// Leave fRemaining alone, it's either enough for minCount or not, but reserve() will |
| 217 |
// do the right thing regardless. |
| 218 |
SkASSERT(fOffset % align == 0); |
| 219 |
SkASSERT(fOffset % stride == 0); |
| 220 |
return; |
| 221 |
} |
| 222 |
|
| 223 |
// On re-aligning to a new stride, the offset needs to be aligned to the LCM of `align` and |
| 224 |
// `stride` so that repeated suballocations of `stride` can be performed by simply adding to |
| 225 |
// fOffset without additional instructions. If `fStride == 0`, it's a signal that the first |
| 226 |
// offset also needs to be aligned to the minimum binding requirement. |
| 227 |
uint32_t align32 = lcm_alignment(SkTo<uint32_t>(align), SkTo<uint32_t>(stride)); |
| 228 |
if (fStride == 0) { |
| 229 |
const uint32_t minAlignment = fOwner->fCurrentBuffers[fStateIndex].fMinAlignment; |
| 230 |
align32 = lcm_alignment(minAlignment, align32); |
| 231 |
} |
| 232 |
|
| 233 |
// Ensures we won't overflow fOffset past buffer size once we align it |
| 234 |
if (this->remainingBytes() >= align32 - 1) { |
| 235 |
const uint32_t offset = SkAlignNonPow2(fOffset, align32); |
| 236 |
SkASSERT(offset <= fBuffer->size()); |
| 237 |
fStride = SkTo<uint32_t>(stride); |
| 238 |
fRemaining = (SkTo<uint32_t>(fBuffer->size()) - offset) / fStride; |
| 239 |
if (fRemaining > 0 && fRemaining >= minCount) { |
| 240 |
// Successful prep, so preserve the aligned offset |
| 241 |
fOffset = offset; |
| 242 |
return; |
| 243 |
} |
| 244 |
} |
| 245 |
} |
| 246 |
|
| 247 |
// If we've reached here, there wasn't a buffer or enough room to align, or enough room to |
| 248 |
// satisfy minCount, so set fRemaining=0 to fail subsequent reservations. |
| 249 |
fRemaining = 0; |
| 250 |
} |
| 251 |
|
| 252 |
void BufferSubAllocator::reset() { |
| 253 |
if (fBuffer) { |
| 254 |
SkASSERT(fOwner); |
| 255 |
|
| 256 |
DrawBufferManager::BufferState& state = fOwner->fCurrentBuffers[fStateIndex]; |
| 257 |
if (fBuffer->shareable() == Shareable::kScratch) { |
| 258 |
// TODO: Merge this reuse of scratch resources with the ScratchResourceManager, but |
| 259 |
// currently this is resolved outside of Task::prepareResources(). |
| 260 |
|
| 261 |
// The scratch buffer's availability for reuse (scoped to the owning DrawBufferManager) |
| 262 |
// was tied to this BufferSubAllocator, so when that is reset, we just remove the buffer |
| 263 |
// from the set of unavailable buffers. |
| 264 |
SkASSERT((fOwner->fMappingFailed && state.fUnavailableScratchBuffers.empty()) || |
| 265 |
state.fUnavailableScratchBuffers.contains(fBuffer.get())); |
| 266 |
if (!fOwner->fMappingFailed) { |
| 267 |
state.fUnavailableScratchBuffers.remove(fBuffer.get()); |
| 268 |
} |
| 269 |
|
| 270 |
SkASSERT(!fTransferBuffer); // Scratch buffers shouldn't be using transfer buffers |
| 271 |
fOwner->fUsedBuffers.emplace_back(std::move(fBuffer), BindBufferInfo{}); |
| 272 |
} else if (state.fAvailableBuffer.fBuffer.get() == fBuffer.get() || // can't stash itself |
| 273 |
this->remainingBytes() < state.fAvailableBuffer.remainingBytes() || // too small |
| 274 |
this->remainingBytes() < state.fMinAlignment) { // basically empty |
| 275 |
// Transfer ownership of the buffer (and any transfer buffer) back to the manager, using |
| 276 |
// the current offset as a more restricted limit for copying. |
| 277 |
if (fTransferBuffer) { |
| 278 |
// This alignment ensures we are copying a subset that still respects xfer alignment |
| 279 |
fTransferBuffer.fSize = SkAlignTo(fOffset, state.fMinAlignment); |
| 280 |
} |
| 281 |
fOwner->fUsedBuffers.emplace_back(std::move(fBuffer), fTransferBuffer); |
| 282 |
} else { |
| 283 |
// Save this buffer for later, which leaves this instance empty and resets the prior |
| 284 |
// value of fAvailableBuffer (which then goes through the true branch of this if). |
| 285 |
state.fAvailableBuffer = std::move(*this); |
| 286 |
} |
| 287 |
|
| 288 |
fRemaining = 0; |
| 289 |
SkASSERT(!fBuffer); |
| 290 |
} // else nothing to reset |
| 291 |
} |
| 292 |
|
| 293 |
// ------------------------------------------------------------------------------------------------ |
| 294 |
// DrawBufferManager::BufferState |
| 295 |
|
| 296 |
DrawBufferManager::BufferState::BufferState(BufferType type, |
| 297 |
const char* label, |
| 298 |
bool isGpuOnly, |
| 299 |
const Options& opts, |
| 300 |
const Caps* caps) |
| 301 |
: fType(type) |
| 302 |
// The buffer can be GPU-only if |
| 303 |
// a) the caller does not intend to ever upload CPU data to the buffer; or |
| 304 |
// b) CPU data will get uploaded to fBuffer only via a transfer buffer |
| 305 |
, fAccessPattern(get_gpu_access_pattern(isGpuOnly || !caps->drawBufferCanBeMapped(), opts)) |
| 306 |
, fUseTransferBuffer(!isGpuOnly && !caps->drawBufferCanBeMapped()) |
| 307 |
, fLabel(label) |
| 308 |
, fMinAlignment(minimum_alignment(type, fUseTransferBuffer, caps)) |
| 309 |
, fMinBlockSize(min_block_size(type, fMinAlignment, opts)) |
| 310 |
, fMaxBlockSize(max_block_size(type, fMinAlignment, opts)) { |
| 311 |
SkASSERT(SkIsPow2(fMinAlignment)); |
| 312 |
SkASSERT(fMinBlockSize <= fMaxBlockSize); |
| 313 |
} |
| 314 |
|
| 315 |
sk_sp<Buffer> DrawBufferManager::BufferState::findOrCreateBuffer(ResourceProvider* provider, |
| 316 |
Shareable shareable, |
| 317 |
uint32_t byteCount) { |
| 318 |
if (shareable == Shareable::kScratch) { |
| 319 |
sk_sp<Buffer> scratchBuffer = provider->findOrCreateScratchBuffer( |
| 320 |
byteCount, fType, fAccessPattern, fLabel, fUnavailableScratchBuffers); |
| 321 |
if (scratchBuffer) { |
| 322 |
fUnavailableScratchBuffers.add(scratchBuffer.get()); |
| 323 |
} |
| 324 |
return scratchBuffer; |
| 325 |
} else { |
| 326 |
return provider->findOrCreateNonShareableBuffer(byteCount, fType, fAccessPattern, fLabel); |
| 327 |
} |
| 328 |
} |
| 329 |
|
| 330 |
// ------------------------------------------------------------------------------------------------ |
| 331 |
// DrawBufferManager |
| 332 |
|
| 333 |
DrawBufferManager::DrawBufferManager(ResourceProvider* resourceProvider, |
| 334 |
const Caps* caps, |
| 335 |
UploadBufferManager* uploadManager, |
| 336 |
Options dbmOpts) |
| 337 |
: fResourceProvider(resourceProvider) |
| 338 |
, fCaps(caps) |
| 339 |
, fUploadManager(uploadManager) |
| 340 |
, fCurrentBuffers{{ |
| 341 |
// Mappable buffers |
| 342 |
{BufferType::kVertex, "VertexBuffer", /*isGpuOnly=*/false, dbmOpts, caps}, |
| 343 |
{BufferType::kIndex, "IndexBuffer", /*isGpuOnly=*/false, dbmOpts, caps}, |
| 344 |
{BufferType::kUniform, "UniformBuffer", /*isGpuOnly=*/false, dbmOpts, caps}, |
| 345 |
{BufferType::kStorage, "StorageBuffer", /*isGpuOnly=*/false, dbmOpts, caps}, |
| 346 |
// GPU-only buffers |
| 347 |
{BufferType::kStorage, "GPUOnlyStorageBuffer", /*isGpuOnly=*/true, dbmOpts, caps}, |
| 348 |
{BufferType::kVertexStorage, "VertexStorageBuffer", /*isGpuOnly=*/true, dbmOpts, caps}, |
| 349 |
{BufferType::kIndexStorage, "IndexStorageBuffer", /*isGpuOnly=*/true, dbmOpts, caps}, |
| 350 |
{BufferType::kIndirect, "IndirectStorageBuffer", /*isGpuOnly=*/true, dbmOpts, caps}}} {} |
| 351 |
|
| 352 |
DrawBufferManager::~DrawBufferManager() { |
| 353 |
// Must reset these *before* we are deleted |
| 354 |
for (auto& b : fCurrentBuffers) { |
| 355 |
b.fAvailableBuffer.reset(); |
| 356 |
} |
| 357 |
} |
| 358 |
|
| 359 |
void DrawBufferManager::onFailedBuffer() { |
| 360 |
fMappingFailed = true; |
| 361 |
|
| 362 |
// Clean up and unmap everything now |
| 363 |
fClearList.clear(); |
| 364 |
for (auto& state : fCurrentBuffers) { |
| 365 |
state.fAvailableBuffer.reset(); |
| 366 |
// We aren't allocating anything anymore so don't maintain this list. Their outstanding |
| 367 |
// BufferSubAllocators will have a no-op when they get reset. |
| 368 |
state.fUnavailableScratchBuffers.reset(); |
| 369 |
state.fLastBufferSize = 0; |
| 370 |
} |
| 371 |
|
| 372 |
for (auto& [buffer, _] : fUsedBuffers) { |
| 373 |
if (buffer->isMapped()) { |
| 374 |
buffer->unmap(); |
| 375 |
} |
| 376 |
} |
| 377 |
fUsedBuffers.clear(); |
| 378 |
} |
| 379 |
|
| 380 |
bool DrawBufferManager::transferToRecording(Recording* recording) { |
| 381 |
if (fMappingFailed) { |
| 382 |
// All state should have been reset by onFailedBuffer() except for this error flag. |
| 383 |
SkASSERT(fUsedBuffers.empty() && fClearList.empty()); |
| 384 |
#if defined(SK_DEBUG) |
| 385 |
for (const auto& state : fCurrentBuffers) { |
| 386 |
SkASSERT(!SkToBool(state.fAvailableBuffer)); |
| 387 |
SkASSERT(state.fUnavailableScratchBuffers.empty()); |
| 388 |
} |
| 389 |
#endif |
| 390 |
|
| 391 |
fMappingFailed = false; |
| 392 |
return false; |
| 393 |
} |
| 394 |
|
| 395 |
for (auto& state : fCurrentBuffers) { |
| 396 |
// Reset all available buffer sub allocators since they won't be allocatable anymore. |
| 397 |
// This pushes the underlying resource and transfer range to fUsedBuffers |
| 398 |
state.fAvailableBuffer.reset(); |
| 399 |
// BufferSubAllocators should have gone out of scope well before Recorder::snap() is called. |
| 400 |
SkASSERT(state.fUnavailableScratchBuffers.empty()); |
| 401 |
|
| 402 |
// We reset the last buffer size back to 0 to keep the buffer growth behavior the same |
| 403 |
// across calls to snap(). If we knew every snap() would be approximately the same workload, |
| 404 |
// we could choose to keep the last alloc size as-is so that subsequent frames create |
| 405 |
// fewer buffers. We choose *not* to do this because: |
| 406 |
// - Chrome often snaps Recordings with disparate workloads within a frame (e.g. tile vs |
| 407 |
// canvas2d) and we don't want to overallocate on a small recording. |
| 408 |
// - It obfuscates the performance cost of the first frame if we reach a steady state that |
| 409 |
// requires no additional buffer allocations. |
| 410 |
// We could choose to reduce fLastBufferSize (e.g. halve it) to get a head start and reduce |
| 411 |
// the potential for over-allocation, but in performance measurements on buffer-heavy scenes |
| 412 |
// this did not lead to measurable improvements. Thus, we reset so every frame is the same. |
| 413 |
state.fLastBufferSize = 0; |
| 414 |
} |
| 415 |
|
| 416 |
if (!fClearList.empty()) { |
| 417 |
recording->priv().taskList()->add(ClearBuffersTask::Make(std::move(fClearList))); |
| 418 |
} |
| 419 |
|
| 420 |
for (auto& [buffer, transferBuffer] : fUsedBuffers) { |
| 421 |
if (transferBuffer) { |
| 422 |
SkASSERT(buffer); |
| 423 |
SkASSERT(!fCaps->drawBufferCanBeMapped()); |
| 424 |
// Since the transfer buffer is managed by the UploadManager, we don't manually unmap |
| 425 |
// it here or need to pass a ref into CopyBufferToBufferTask. |
| 426 |
size_t copySize = buffer->size(); |
| 427 |
recording->priv().taskList()->add( |
| 428 |
CopyBufferToBufferTask::Make(transferBuffer.fBuffer, |
| 429 |
transferBuffer.fOffset, |
| 430 |
std::move(buffer), |
| 431 |
/*dstOffset=*/0, |
| 432 |
copySize)); |
| 433 |
} else { |
| 434 |
if (buffer->isMapped()) { |
| 435 |
buffer->unmap(); |
| 436 |
} |
| 437 |
recording->priv().addResourceRef(std::move(buffer)); |
| 438 |
} |
| 439 |
} |
| 440 |
|
| 441 |
fUsedBuffers.clear(); |
| 442 |
|
| 443 |
return true; |
| 444 |
} |
| 445 |
|
| 446 |
BufferSubAllocator DrawBufferManager::getBuffer( |
| 447 |
int stateIndex, |
| 448 |
size_t count, |
| 449 |
size_t stride, |
| 450 |
size_t xtraAlignment, |
| 451 |
ClearBuffer cleared, |
| 452 |
Shareable shareable) { |
| 453 |
BufferState& state = fCurrentBuffers[stateIndex]; |
| 454 |
// The size for a buffer is aligned to the minimum block size for better resource reuse, which |
| 455 |
// is more conservative than fMinAlignment. |
| 456 |
uint32_t requiredBytes32 = validate_count_and_stride(count, stride, state.fMinBlockSize); |
| 457 |
if (fMappingFailed || !requiredBytes32) { |
| 458 |
return {}; |
| 459 |
} |
| 460 |
|
| 461 |
const bool supportCpuUpload = state.fAccessPattern == AccessPattern::kHostVisible || |
| 462 |
state.fUseTransferBuffer; |
| 463 |
// Shareable buffers must be GPU-only to actually share effectively. |
| 464 |
SkASSERT(shareable == Shareable::kNo || !supportCpuUpload); |
| 465 |
|
| 466 |
// For non-shareable buffers, we keep the largest relinquished non-shareable buffer in case it |
| 467 |
// has room leftover to be used by future allocations. Scratch buffer ownership is entirely |
| 468 |
// managed by the caller, so always create a new BufferSubAllocator. |
| 469 |
if (shareable == Shareable::kNo) { |
| 470 |
state.fAvailableBuffer.resetForNewBinding(); // ensure we include min binding alignment |
| 471 |
state.fAvailableBuffer.prepForStride(stride, xtraAlignment, count); |
| 472 |
if (state.fAvailableBuffer.availableWithStride() >= count) { |
| 473 |
SkASSERT(state.fAvailableBuffer.fBuffer); |
| 474 |
SkASSERT(state.fAvailableBuffer.fBuffer->shareable() == shareable); |
| 475 |
SkASSERT(SkToBool(state.fAvailableBuffer.fMappedPtr) == supportCpuUpload); |
| 476 |
return std::move(state.fAvailableBuffer); |
| 477 |
} |
| 478 |
|
| 479 |
// Not enough room in the available buffer so release it and create a new buffer. |
| 480 |
state.fAvailableBuffer.reset(); |
| 481 |
} |
| 482 |
|
| 483 |
// Create the next buffer by doubling the size of the previous buffer and clamping to be within |
| 484 |
// the min and max block sizes if `requiredBytes` is less than the max. Otherwise, create a |
| 485 |
// buffer large enough to satisfy `requiredBytes` but align it to minBlockSize. |
| 486 |
uint32_t bufferSize = SkAlignTo(requiredBytes32, state.fMinBlockSize); |
| 487 |
if (bufferSize < state.fMaxBlockSize) { |
| 488 |
// fMaxBlockSize should be sufficiently small that there's no risk of overflowing here. |
| 489 |
SkASSERT(std::numeric_limits<uint32_t>::max() /2 > state.fLastBufferSize); |
| 490 |
bufferSize = std::max(bufferSize, std::min(state.fLastBufferSize * 2, state.fMaxBlockSize)); |
| 491 |
state.fLastBufferSize = bufferSize; |
| 492 |
SkASSERT(bufferSize <= state.fMaxBlockSize); |
| 493 |
} else { |
| 494 |
// Jump to the max block size for subsequent amortized allocations if we get a really big |
| 495 |
// buffer request. |
| 496 |
state.fLastBufferSize = state.fMaxBlockSize; |
| 497 |
} |
| 498 |
SkASSERT(bufferSize >= requiredBytes32 && bufferSize >= state.fMinBlockSize); |
| 499 |
|
| 500 |
sk_sp<Buffer> buffer = state.findOrCreateBuffer(fResourceProvider, shareable, bufferSize); |
| 501 |
if (!buffer) { |
| 502 |
this->onFailedBuffer(); |
| 503 |
return {}; |
| 504 |
} |
| 505 |
|
| 506 |
BindBufferInfo transferBuffer; |
| 507 |
void* mappedPtr = nullptr; |
| 508 |
if (supportCpuUpload) { |
| 509 |
if (state.fUseTransferBuffer) { |
| 510 |
std::tie(mappedPtr, transferBuffer) = fUploadManager->makeBindInfo(buffer->size(), |
| 511 |
fCaps->requiredTransferBufferAlignment(), "TransferForDataBuffer"); |
| 512 |
} else { |
| 513 |
mappedPtr = buffer->map(); |
| 514 |
} |
| 515 |
|
| 516 |
if (!mappedPtr) { |
| 517 |
this->onFailedBuffer(); // Either transfer buffer failed or direct mapping failed |
| 518 |
return {}; |
| 519 |
} |
| 520 |
} |
| 521 |
|
| 522 |
if (cleared == ClearBuffer::kYes) { |
| 523 |
fClearList.push_back(BindBufferInfo{buffer.get(), 0, bufferSize}); |
| 524 |
} |
| 525 |
|
| 526 |
// The returned buffer is not set to fAvailableBuffer because it is going to be passed up to |
| 527 |
// the caller for their use first. Since a new BufferSubAllocator starts at offset 0, there's no |
| 528 |
// need to account for `xtraAlignment`. |
| 529 |
return BufferSubAllocator(this, stateIndex, std::move(buffer), |
| 530 |
transferBuffer, mappedPtr, stride); |
| 531 |
} |
| 532 |
|
| 533 |
// ------------------------------------------------------------------------------------------------ |
| 534 |
// StaticBufferManager |
| 535 |
|
| 536 |
StaticBufferManager::StaticBufferManager(ResourceProvider* resourceProvider, |
| 537 |
const Caps* caps) |
| 538 |
: fResourceProvider(resourceProvider) |
| 539 |
, fUploadManager(resourceProvider, caps) |
| 540 |
, fRequiredTransferAlignment(SkTo<uint32_t>(caps->requiredTransferBufferAlignment())) |
| 541 |
, fVertexBufferState(BufferType::kVertex, caps) |
| 542 |
, fIndexBufferState(BufferType::kIndex, caps) {} |
| 543 |
StaticBufferManager::~StaticBufferManager() = default; |
| 544 |
|
| 545 |
StaticBufferManager::BufferState::BufferState(BufferType type, const Caps* caps) |
| 546 |
: fBufferType(type) |
| 547 |
, fMinimumAlignment(minimum_alignment(type, /*useTransferBuffers=*/true, caps)) |
| 548 |
, fTotalRequiredBytes(0) {} |
| 549 |
|
| 550 |
// ARM hardware b/399631317 also means that static vertex data must be padded and zeroed out. So we |
| 551 |
// always request a count 4 aligned offset, count 4 aligned amount of space, and zero it. |
| 552 |
VertexWriter StaticBufferManager::getVertexWriter(size_t count, |
| 553 |
size_t stride, |
| 554 |
BindBufferInfo* binding) { |
| 555 |
const size_t size = count * stride; |
| 556 |
const size_t alignedCount = SkAlign4(count); |
| 557 |
void* data = this->prepareStaticData(&fVertexBufferState, size, stride * 4, binding); |
| 558 |
if (alignedCount > count) { |
| 559 |
const uint32_t byteDiff = (alignedCount - count) * stride; |
| 560 |
void* zPtr = SkTAddOffset<void>(data, count * stride); |
| 561 |
memset(zPtr, 0, byteDiff); |
| 562 |
} |
| 563 |
return VertexWriter{data, size}; |
| 564 |
} |
| 565 |
|
| 566 |
VertexWriter StaticBufferManager::getIndexWriter(size_t size, BindBufferInfo* binding) { |
| 567 |
// The index writer does not have the same alignment requirements as a vertex, so we simply pass |
| 568 |
// in the minimum alignment as the required alignment |
| 569 |
void* data = this->prepareStaticData(&fIndexBufferState, |
| 570 |
size, |
| 571 |
fIndexBufferState.fMinimumAlignment, |
| 572 |
binding); |
| 573 |
return VertexWriter{data, size}; |
| 574 |
} |
| 575 |
|
| 576 |
void* StaticBufferManager::prepareStaticData(BufferState* state, |
| 577 |
size_t requiredBytes, |
| 578 |
size_t requiredAlignment, |
| 579 |
BindBufferInfo* target) { |
| 580 |
// Zero-out the target binding in the event of any failure in actually transfering data later. |
| 581 |
// Unlike in BufferSubAllocator::reserve(), we do use SkTo<uint32_t> to check |
| 582 |
// `requiredAlignment`. This is not dynamic data and is fully controlled by Graphite, so if it |
| 583 |
// asserts, then there is a bug in the static data for a Renderer that must be fixed. |
| 584 |
const uint32_t align32 = lcm_alignment(state->fMinimumAlignment, |
| 585 |
SkTo<uint32_t>(requiredAlignment)); |
| 586 |
|
| 587 |
SkASSERT(target); |
| 588 |
*target = {nullptr, 0}; |
| 589 |
uint32_t size32 = validate_count_and_stride(requiredBytes, /*stride=*/1, align32); |
| 590 |
if (!size32 || fMappingFailed) { |
| 591 |
return nullptr; |
| 592 |
} |
| 593 |
|
| 594 |
// Copy data must be aligned to the transfer alignment, so align the reserved size to the LCM |
| 595 |
// of the minimum alignment (already net buffer and transfer alignment) and the required |
| 596 |
// alignment stride. |
| 597 |
size32 = SkAlignNonPow2(size32, align32); |
| 598 |
auto [transferMapPtr, transferBindInfo] = |
| 599 |
fUploadManager.makeBindInfo(size32, |
| 600 |
fRequiredTransferAlignment, |
| 601 |
"TransferForStaticBuffer"); |
| 602 |
if (!transferMapPtr) { |
| 603 |
SKGPU_LOG_E("Failed to create or map transfer buffer that initializes static GPU data."); |
| 604 |
fMappingFailed = true; |
| 605 |
return nullptr; |
| 606 |
} |
| 607 |
|
| 608 |
state->fData.push_back( |
| 609 |
{transferBindInfo, |
| 610 |
target, |
| 611 |
SkTo<uint32_t>(requiredAlignment), |
| 612 |
#if defined(GPU_TEST_UTILS) |
| 613 |
SkTo<uint32_t>(requiredBytes) |
| 614 |
#endif |
| 615 |
}); |
| 616 |
|
| 617 |
state->fTotalRequiredBytes = SkAlignNonPow2(state->fTotalRequiredBytes, align32) + size32; |
| 618 |
|
| 619 |
return transferMapPtr; |
| 620 |
} |
| 621 |
|
| 622 |
bool StaticBufferManager::BufferState::createAndUpdateBindings( |
| 623 |
ResourceProvider* resourceProvider, |
| 624 |
Context* context, |
| 625 |
QueueManager* queueManager, |
| 626 |
GlobalCache* globalCache, |
| 627 |
std::string_view label) const { |
| 628 |
if (!fTotalRequiredBytes) { |
| 629 |
return true; // No buffer needed |
| 630 |
} |
| 631 |
|
| 632 |
// The static buffer is always copyable when testing. |
| 633 |
constexpr AccessPattern gpuAccessPattern = |
| 634 |
#if defined(GPU_TEST_UTILS) |
| 635 |
AccessPattern::kGpuOnlyCopySrc; |
| 636 |
#else |
| 637 |
AccessPattern::kGpuOnly; |
| 638 |
#endif |
| 639 |
|
| 640 |
sk_sp<Buffer> staticBuffer = resourceProvider->findOrCreateNonShareableBuffer( |
| 641 |
fTotalRequiredBytes, |
| 642 |
fBufferType, |
| 643 |
gpuAccessPattern, |
| 644 |
label); |
| 645 |
if (!staticBuffer) { |
| 646 |
SKGPU_LOG_E("Failed to create static buffer for type %d of size %u bytes.\n", |
| 647 |
(int) fBufferType, fTotalRequiredBytes); |
| 648 |
return false; |
| 649 |
} |
| 650 |
|
| 651 |
uint32_t offset = 0; |
| 652 |
for (const CopyRange& data : fData) { |
| 653 |
// Each copy range's size should be aligned to the lcm of the required alignment and minimum |
| 654 |
// alignment so we can increment the offset in the static buffer. |
| 655 |
const uint32_t alignment = lcm_alignment(fMinimumAlignment, data.fRequiredAlignment); |
| 656 |
offset = SkAlignNonPow2(offset, alignment); |
| 657 |
SkASSERT(!(offset % fMinimumAlignment) && !(offset % data.fRequiredAlignment)); |
| 658 |
uint32_t size = data.fSource.fSize; |
| 659 |
data.fTarget->fBuffer = staticBuffer.get(); |
| 660 |
data.fTarget->fOffset = offset; |
| 661 |
data.fTarget->fSize = size; |
| 662 |
|
| 663 |
auto copyTask = CopyBufferToBufferTask::Make( |
| 664 |
data.fSource.fBuffer, data.fSource.fOffset, |
| 665 |
sk_ref_sp(data.fTarget->fBuffer), data.fTarget->fOffset, |
| 666 |
size); |
| 667 |
// For static buffers, we want them all to be optimized as GPU only buffers. If we are in |
| 668 |
// a protected context, this means the buffers must be non-protected since they will be |
| 669 |
// read in the vertex shader which doesn't allow protected memory access. Thus all the |
| 670 |
// uploads to these buffers must be done as non-protected commands. |
| 671 |
if (!queueManager->addTask(copyTask.get(), context, Protected::kNo)) { |
| 672 |
SKGPU_LOG_E("Failed to copy data to static buffer.\n"); |
| 673 |
return false; |
| 674 |
} |
| 675 |
|
| 676 |
offset += size; |
| 677 |
} |
| 678 |
|
| 679 |
SkASSERT(offset == fTotalRequiredBytes); |
| 680 |
globalCache->addStaticResource(std::move(staticBuffer)); |
| 681 |
return true; |
| 682 |
} |
| 683 |
|
| 684 |
StaticBufferManager::FinishResult StaticBufferManager::finalize(Context* context, |
| 685 |
QueueManager* queueManager, |
| 686 |
GlobalCache* globalCache) { |
| 687 |
if (fMappingFailed) { |
| 688 |
return FinishResult::kFailure; |
| 689 |
} |
| 690 |
|
| 691 |
const size_t totalRequiredBytes = fVertexBufferState.fTotalRequiredBytes + |
| 692 |
fIndexBufferState.fTotalRequiredBytes; |
| 693 |
SkASSERT(totalRequiredBytes <= kMaxStaticDataSize); |
| 694 |
if (!totalRequiredBytes) { |
| 695 |
return FinishResult::kNoWork; |
| 696 |
} |
| 697 |
|
| 698 |
if (!fVertexBufferState.createAndUpdateBindings(fResourceProvider, |
| 699 |
context, |
| 700 |
queueManager, |
| 701 |
globalCache, |
| 702 |
"StaticVertexBuffer")) { |
| 703 |
return FinishResult::kFailure; |
| 704 |
} |
| 705 |
|
| 706 |
#if defined(GPU_TEST_UTILS) |
| 707 |
skia_private::TArray<GlobalCache::StaticVertexCopyRanges> statVertCopy; |
| 708 |
for (const CopyRange& data : fVertexBufferState.fData) { |
| 709 |
statVertCopy.push_back({data.fTarget->fOffset, |
| 710 |
data.fUnalignedSize, |
| 711 |
data.fTarget->fSize, |
| 712 |
data.fRequiredAlignment}); |
| 713 |
} |
| 714 |
globalCache->testingOnly_SetStaticVertexInfo( |
| 715 |
statVertCopy, |
| 716 |
fVertexBufferState.fData[0].fTarget->fBuffer); |
| 717 |
#endif |
| 718 |
|
| 719 |
if (!fIndexBufferState.createAndUpdateBindings(fResourceProvider, |
| 720 |
context, |
| 721 |
queueManager, |
| 722 |
globalCache, |
| 723 |
"StaticIndexBuffer")) { |
| 724 |
return FinishResult::kFailure; |
| 725 |
} |
| 726 |
queueManager->addUploadBufferManagerRefs(&fUploadManager); |
| 727 |
|
| 728 |
// Reset the static buffer manager since the Recording's copy tasks now manage ownership of |
| 729 |
// the transfer buffers and the GlobalCache owns the final static buffers. |
| 730 |
fVertexBufferState.reset(); |
| 731 |
fIndexBufferState.reset(); |
| 732 |
|
| 733 |
return FinishResult::kSuccess; |
| 734 |
} |
| 735 |
|
| 736 |
} // namespace skgpu::graphite |