Hash :
c3a1cae4
Author :
Date :
2024-04-15T14:58:55
Use angle::SimpleMutex everywhere in libGLESv2 Only cases left that use std::mutex are: - Share group and the context ErrorSet mutexes as they need try_lock() - Anywhere mutexes are used in conjunction with std::condition_variables (as they explicitly require std::mutex) Bug: angleproject:8667 Change-Id: Ib6d68938b0886f9e7c43e023162557990ecfb300 Reviewed-on: https://chromium-review.googlesource.com/c/angle/angle/+/5453294 Reviewed-by: Roman Lavrov <romanl@google.com> Reviewed-by: Charlie Lao <cclao@google.com> Commit-Queue: Shahbaz Youssefi <syoussefi@chromium.org>
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489
//
// Copyright 2022 The ANGLE Project Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
//
// Suballocation.h:
// Defines class interface for BufferBlock and Suballocation and other related classes.
//
#ifndef LIBANGLE_RENDERER_VULKAN_SUBALLOCATION_H_
#define LIBANGLE_RENDERER_VULKAN_SUBALLOCATION_H_
#include "common/SimpleMutex.h"
#include "common/debug.h"
#include "libANGLE/angletypes.h"
#include "libANGLE/renderer/serial_utils.h"
#include "libANGLE/renderer/vulkan/vk_cache_utils.h"
#include "libANGLE/renderer/vulkan/vk_resource.h"
#include "libANGLE/renderer/vulkan/vk_utils.h"
#include "libANGLE/renderer/vulkan/vk_wrapper.h"
namespace rx
{
enum class MemoryAllocationType;
namespace vk
{
class Context;
// BufferBlock
class BufferBlock final : angle::NonCopyable
{
public:
BufferBlock();
BufferBlock(BufferBlock &&other);
~BufferBlock();
void destroy(Renderer *renderer);
VkResult init(Context *context,
Buffer &buffer,
uint32_t memoryTypeIndex,
vma::VirtualBlockCreateFlags flags,
DeviceMemory &deviceMemory,
VkMemoryPropertyFlags memoryPropertyFlags,
VkDeviceSize size);
void initWithoutVirtualBlock(Context *context,
Buffer &buffer,
MemoryAllocationType memoryAllocationType,
uint32_t memoryTypeIndex,
DeviceMemory &deviceMemory,
VkMemoryPropertyFlags memoryPropertyFlags,
VkDeviceSize size,
VkDeviceSize allocatedBufferSize);
BufferBlock &operator=(BufferBlock &&other);
const Buffer &getBuffer() const { return mBuffer; }
const DeviceMemory &getDeviceMemory() const { return mDeviceMemory; }
DeviceMemory &getDeviceMemory() { return mDeviceMemory; }
BufferSerial getBufferSerial() const { return mSerial; }
VkMemoryPropertyFlags getMemoryPropertyFlags() const;
VkDeviceSize getMemorySize() const;
VkResult allocate(VkDeviceSize size,
VkDeviceSize alignment,
VmaVirtualAllocation *allocationOut,
VkDeviceSize *offsetOut);
void free(VmaVirtualAllocation allocation, VkDeviceSize offset);
VkBool32 isEmpty();
bool hasVirtualBlock() const { return mVirtualBlock.valid(); }
bool isHostVisible() const;
bool isCoherent() const;
bool isCached() const;
bool isMapped() const;
VkResult map(const VkDevice device);
void unmap(const VkDevice device);
uint8_t *getMappedMemory() const;
// This should be called whenever this found to be empty. The total number of count of empty is
// returned.
int32_t getAndIncrementEmptyCounter();
void calculateStats(vma::StatInfo *pStatInfo) const;
void onNewDescriptorSet(const SharedDescriptorSetCacheKey &sharedCacheKey)
{
mDescriptorSetCacheManager.addKey(sharedCacheKey);
}
void releaseAllCachedDescriptorSetCacheKeys(Renderer *renderer)
{
if (!mDescriptorSetCacheManager.empty())
{
mDescriptorSetCacheManager.releaseKeys(renderer);
}
}
private:
mutable angle::SimpleMutex mVirtualBlockMutex;
VirtualBlock mVirtualBlock;
Buffer mBuffer;
DeviceMemory mDeviceMemory;
VkMemoryPropertyFlags mMemoryPropertyFlags;
// Memory size that user of this object thinks we have.
VkDeviceSize mSize;
// Memory size that was actually allocated for this object.
VkDeviceSize mAllocatedBufferSize;
// Memory allocation type used for this object.
MemoryAllocationType mMemoryAllocationType;
// Memory type index used for the allocation. It can be used to determine the heap index.
uint32_t mMemoryTypeIndex;
uint8_t *mMappedMemory;
BufferSerial mSerial;
// Heuristic information for pruneEmptyBuffer. This tracks how many times (consecutively) this
// buffer block is found to be empty when pruneEmptyBuffer is called. This gets reset whenever
// it becomes non-empty.
int32_t mCountRemainsEmpty;
// Manages the descriptorSet cache that created with this BufferBlock.
DescriptorSetCacheManager mDescriptorSetCacheManager;
};
using BufferBlockPointer = std::unique_ptr<BufferBlock>;
using BufferBlockPointerVector = std::vector<BufferBlockPointer>;
class BufferBlockGarbageList final : angle::NonCopyable
{
public:
BufferBlockGarbageList() : mBufferBlockQueue(kInitialQueueCapacity) {}
~BufferBlockGarbageList() { ASSERT(mBufferBlockQueue.empty()); }
void add(BufferBlock *bufferBlock)
{
std::unique_lock<angle::SimpleMutex> lock(mMutex);
if (mBufferBlockQueue.full())
{
size_t newCapacity = mBufferBlockQueue.capacity() << 1;
mBufferBlockQueue.updateCapacity(newCapacity);
}
mBufferBlockQueue.push(bufferBlock);
}
void pruneEmptyBufferBlocks(Renderer *renderer)
{
if (!mBufferBlockQueue.empty())
{
std::unique_lock<angle::SimpleMutex> lock(mMutex);
size_t count = mBufferBlockQueue.size();
for (size_t i = 0; i < count; i++)
{
BufferBlock *block = mBufferBlockQueue.front();
mBufferBlockQueue.pop();
if (block->isEmpty())
{
block->destroy(renderer);
}
else
{
mBufferBlockQueue.push(block);
}
}
}
}
bool empty() const { return mBufferBlockQueue.empty(); }
private:
static constexpr size_t kInitialQueueCapacity = 4;
angle::SimpleMutex mMutex;
angle::FixedQueue<BufferBlock *> mBufferBlockQueue;
};
// BufferSuballocation
class BufferSuballocation final : angle::NonCopyable
{
public:
BufferSuballocation();
BufferSuballocation(BufferSuballocation &&other);
BufferSuballocation &operator=(BufferSuballocation &&other);
void destroy(Renderer *renderer);
void init(BufferBlock *block,
VmaVirtualAllocation allocation,
VkDeviceSize offset,
VkDeviceSize size);
void initWithEntireBuffer(Context *context,
Buffer &buffer,
MemoryAllocationType memoryAllocationType,
uint32_t memoryTypeIndex,
DeviceMemory &deviceMemory,
VkMemoryPropertyFlags memoryPropertyFlags,
VkDeviceSize size,
VkDeviceSize allocatedBufferSize);
const Buffer &getBuffer() const;
VkDeviceSize getSize() const;
const DeviceMemory &getDeviceMemory() const;
VkMemoryMapFlags getMemoryPropertyFlags() const;
bool isHostVisible() const;
bool isCoherent() const;
bool isCached() const;
bool isMapped() const;
uint8_t *getMappedMemory() const;
void flush(const VkDevice &device);
void invalidate(const VkDevice &device);
VkDeviceSize getOffset() const;
bool valid() const;
VkResult map(Context *context);
BufferSerial getBlockSerial() const;
uint8_t *getBlockMemory() const;
VkDeviceSize getBlockMemorySize() const;
bool isSuballocated() const { return mBufferBlock->hasVirtualBlock(); }
BufferBlock *getBufferBlock() const { return mBufferBlock; }
private:
// Only used by DynamicBuffer where DynamicBuffer does the actual suballocation and pass the
// offset/size to this object. Since DynamicBuffer does not have a VMA virtual allocator, they
// will be ignored at destroy time. The offset/size is set here mainly for easy retrieval when
// the BufferHelper object is passed around.
friend class BufferHelper;
void setOffsetAndSize(VkDeviceSize offset, VkDeviceSize size);
BufferBlock *mBufferBlock;
VmaVirtualAllocation mAllocation;
VkDeviceSize mOffset;
VkDeviceSize mSize;
};
class BufferSuballocationGarbage
{
public:
BufferSuballocationGarbage() = default;
BufferSuballocationGarbage(BufferSuballocationGarbage &&other)
: mLifetime(other.mLifetime),
mSuballocation(std::move(other.mSuballocation)),
mBuffer(std::move(other.mBuffer))
{}
BufferSuballocationGarbage &operator=(BufferSuballocationGarbage &&other)
{
mLifetime = other.mLifetime;
mSuballocation = std::move(other.mSuballocation);
mBuffer = std::move(other.mBuffer);
return *this;
}
BufferSuballocationGarbage(const ResourceUse &use,
BufferSuballocation &&suballocation,
Buffer &&buffer)
: mLifetime(use), mSuballocation(std::move(suballocation)), mBuffer(std::move(buffer))
{}
~BufferSuballocationGarbage() = default;
bool destroyIfComplete(Renderer *renderer);
bool hasResourceUseSubmitted(Renderer *renderer) const;
VkDeviceSize getSize() const { return mSuballocation.getSize(); }
bool isSuballocated() const { return mSuballocation.isSuballocated(); }
private:
ResourceUse mLifetime;
BufferSuballocation mSuballocation;
Buffer mBuffer;
};
// BufferBlock implementation.
ANGLE_INLINE VkMemoryPropertyFlags BufferBlock::getMemoryPropertyFlags() const
{
return mMemoryPropertyFlags;
}
ANGLE_INLINE VkDeviceSize BufferBlock::getMemorySize() const
{
return mSize;
}
ANGLE_INLINE VkBool32 BufferBlock::isEmpty()
{
std::unique_lock<angle::SimpleMutex> lock(mVirtualBlockMutex);
return vma::IsVirtualBlockEmpty(mVirtualBlock.getHandle());
}
ANGLE_INLINE bool BufferBlock::isHostVisible() const
{
return (mMemoryPropertyFlags & VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT) != 0;
}
ANGLE_INLINE bool BufferBlock::isCoherent() const
{
return (mMemoryPropertyFlags & VK_MEMORY_PROPERTY_HOST_COHERENT_BIT) != 0;
}
ANGLE_INLINE bool BufferBlock::isCached() const
{
return (mMemoryPropertyFlags & VK_MEMORY_PROPERTY_HOST_CACHED_BIT) != 0;
}
ANGLE_INLINE bool BufferBlock::isMapped() const
{
return mMappedMemory != nullptr;
}
ANGLE_INLINE uint8_t *BufferBlock::getMappedMemory() const
{
ASSERT(mMappedMemory != nullptr);
return mMappedMemory;
}
// BufferSuballocation implementation.
ANGLE_INLINE BufferSuballocation::BufferSuballocation()
: mBufferBlock(nullptr), mAllocation(VK_NULL_HANDLE), mOffset(0), mSize(0)
{}
ANGLE_INLINE BufferSuballocation::BufferSuballocation(BufferSuballocation &&other)
: BufferSuballocation()
{
*this = std::move(other);
}
ANGLE_INLINE BufferSuballocation &BufferSuballocation::operator=(BufferSuballocation &&other)
{
std::swap(mBufferBlock, other.mBufferBlock);
std::swap(mSize, other.mSize);
std::swap(mAllocation, other.mAllocation);
std::swap(mOffset, other.mOffset);
return *this;
}
ANGLE_INLINE bool BufferSuballocation::valid() const
{
return mBufferBlock != nullptr;
}
ANGLE_INLINE void BufferSuballocation::destroy(Renderer *renderer)
{
if (valid())
{
ASSERT(mBufferBlock);
if (mBufferBlock->hasVirtualBlock())
{
mBufferBlock->free(mAllocation, mOffset);
mBufferBlock = nullptr;
}
else
{
// When virtual block is invalid, this is the standalone buffer that are created by
// BufferSuballocation::initWithEntireBuffer call. In this case, vmaBufferSuballocation
// owns block, we must properly delete the block object.
mBufferBlock->destroy(renderer);
SafeDelete(mBufferBlock);
}
mAllocation = VK_NULL_HANDLE;
mOffset = 0;
mSize = 0;
}
}
ANGLE_INLINE void BufferSuballocation::init(BufferBlock *block,
VmaVirtualAllocation allocation,
VkDeviceSize offset,
VkDeviceSize size)
{
ASSERT(!valid());
ASSERT(block != nullptr);
ASSERT(allocation != VK_NULL_HANDLE);
ASSERT(offset != VK_WHOLE_SIZE);
mBufferBlock = block;
mAllocation = allocation;
mOffset = offset;
mSize = size;
}
ANGLE_INLINE void BufferSuballocation::initWithEntireBuffer(
Context *context,
Buffer &buffer,
MemoryAllocationType memoryAllocationType,
uint32_t memoryTypeIndex,
DeviceMemory &deviceMemory,
VkMemoryPropertyFlags memoryPropertyFlags,
VkDeviceSize size,
VkDeviceSize allocatedBufferSize)
{
ASSERT(!valid());
std::unique_ptr<BufferBlock> block = std::make_unique<BufferBlock>();
block->initWithoutVirtualBlock(context, buffer, memoryAllocationType, memoryTypeIndex,
deviceMemory, memoryPropertyFlags, size, allocatedBufferSize);
mBufferBlock = block.release();
mAllocation = VK_NULL_HANDLE;
mOffset = 0;
mSize = mBufferBlock->getMemorySize();
}
ANGLE_INLINE const Buffer &BufferSuballocation::getBuffer() const
{
return mBufferBlock->getBuffer();
}
ANGLE_INLINE VkDeviceSize BufferSuballocation::getSize() const
{
return mSize;
}
ANGLE_INLINE const DeviceMemory &BufferSuballocation::getDeviceMemory() const
{
return mBufferBlock->getDeviceMemory();
}
ANGLE_INLINE VkMemoryMapFlags BufferSuballocation::getMemoryPropertyFlags() const
{
return mBufferBlock->getMemoryPropertyFlags();
}
ANGLE_INLINE bool BufferSuballocation::isHostVisible() const
{
return mBufferBlock->isHostVisible();
}
ANGLE_INLINE bool BufferSuballocation::isCoherent() const
{
return mBufferBlock->isCoherent();
}
ANGLE_INLINE bool BufferSuballocation::isCached() const
{
return mBufferBlock->isCached();
}
ANGLE_INLINE bool BufferSuballocation::isMapped() const
{
return mBufferBlock->isMapped();
}
ANGLE_INLINE uint8_t *BufferSuballocation::getMappedMemory() const
{
return mBufferBlock->getMappedMemory() + getOffset();
}
ANGLE_INLINE void BufferSuballocation::flush(const VkDevice &device)
{
if (!isCoherent())
{
VkMappedMemoryRange mappedRange = {};
mappedRange.sType = VK_STRUCTURE_TYPE_MAPPED_MEMORY_RANGE;
mappedRange.memory = mBufferBlock->getDeviceMemory().getHandle();
mappedRange.offset = getOffset();
mappedRange.size = mSize;
mBufferBlock->getDeviceMemory().flush(device, mappedRange);
}
}
ANGLE_INLINE void BufferSuballocation::invalidate(const VkDevice &device)
{
if (!isCoherent())
{
VkMappedMemoryRange mappedRange = {};
mappedRange.sType = VK_STRUCTURE_TYPE_MAPPED_MEMORY_RANGE;
mappedRange.memory = mBufferBlock->getDeviceMemory().getHandle();
mappedRange.offset = getOffset();
mappedRange.size = mSize;
mBufferBlock->getDeviceMemory().invalidate(device, mappedRange);
}
}
ANGLE_INLINE VkDeviceSize BufferSuballocation::getOffset() const
{
return mOffset;
}
ANGLE_INLINE void BufferSuballocation::setOffsetAndSize(VkDeviceSize offset, VkDeviceSize size)
{
mOffset = offset;
mSize = size;
}
ANGLE_INLINE uint8_t *BufferSuballocation::getBlockMemory() const
{
return mBufferBlock->getMappedMemory();
}
ANGLE_INLINE VkDeviceSize BufferSuballocation::getBlockMemorySize() const
{
return mBufferBlock->getMemorySize();
}
ANGLE_INLINE BufferSerial BufferSuballocation::getBlockSerial() const
{
ASSERT(valid());
return mBufferBlock->getBufferSerial();
}
} // namespace vk
} // namespace rx
#endif // LIBANGLE_RENDERER_VULKAN_SUBALLOCATION_H_