diff options
| author | 2020-03-27 02:56:04 -0300 | |
|---|---|---|
| committer | 2020-03-27 03:21:04 -0300 | |
| commit | 7413b30923c3483f45484728503b774d20f8f5df (patch) | |
| tree | e5f2b8c8b23fd64479f9470f12720e37e8dafb37 /src | |
| parent | renderer_vulkan/wrapper: Add owning handle templated class (diff) | |
| download | yuzu-7413b30923c3483f45484728503b774d20f8f5df.tar.gz yuzu-7413b30923c3483f45484728503b774d20f8f5df.tar.xz yuzu-7413b30923c3483f45484728503b774d20f8f5df.zip | |
renderer_vulkan/wrapper: Add pool allocations owning templated class
Diffstat (limited to 'src')
| -rw-r--r-- | src/video_core/renderer_vulkan/wrapper.h | 81 |
1 files changed, 81 insertions, 0 deletions
diff --git a/src/video_core/renderer_vulkan/wrapper.h b/src/video_core/renderer_vulkan/wrapper.h index 5c6729dbc..b50e1f3c7 100644 --- a/src/video_core/renderer_vulkan/wrapper.h +++ b/src/video_core/renderer_vulkan/wrapper.h | |||
| @@ -422,4 +422,85 @@ private: | |||
| 422 | } | 422 | } |
| 423 | }; | 423 | }; |
| 424 | 424 | ||
| 425 | /// Array of a pool allocation. | ||
| 426 | /// Analogue to std::vector | ||
| 427 | template <typename AllocationType, typename PoolType> | ||
| 428 | class PoolAllocations { | ||
| 429 | public: | ||
| 430 | /// Construct an empty allocation. | ||
| 431 | PoolAllocations() = default; | ||
| 432 | |||
| 433 | /// Construct an allocation. Errors are reported through IsOutOfPoolMemory(). | ||
| 434 | explicit PoolAllocations(std::unique_ptr<AllocationType[]> allocations, std::size_t num, | ||
| 435 | VkDevice device, PoolType pool, const DeviceDispatch& dld) noexcept | ||
| 436 | : allocations{std::move(allocations)}, num{num}, device{device}, pool{pool}, dld{&dld} {} | ||
| 437 | |||
| 438 | /// Copying Vulkan allocations is not supported and will never be. | ||
| 439 | PoolAllocations(const PoolAllocations&) = delete; | ||
| 440 | PoolAllocations& operator=(const PoolAllocations&) = delete; | ||
| 441 | |||
| 442 | /// Construct an allocation transfering ownership from another allocation. | ||
| 443 | PoolAllocations(PoolAllocations&& rhs) noexcept | ||
| 444 | : allocations{std::move(rhs.allocations)}, num{rhs.num}, device{rhs.device}, pool{rhs.pool}, | ||
| 445 | dld{rhs.dld} {} | ||
| 446 | |||
| 447 | /// Assign an allocation transfering ownership from another allocation. | ||
| 448 | /// Releases any previously held allocation. | ||
| 449 | PoolAllocations& operator=(PoolAllocations&& rhs) noexcept { | ||
| 450 | Release(); | ||
| 451 | allocations = std::move(rhs.allocations); | ||
| 452 | num = rhs.num; | ||
| 453 | device = rhs.device; | ||
| 454 | pool = rhs.pool; | ||
| 455 | dld = rhs.dld; | ||
| 456 | return *this; | ||
| 457 | } | ||
| 458 | |||
| 459 | /// Destroys any held allocation. | ||
| 460 | ~PoolAllocations() { | ||
| 461 | Release(); | ||
| 462 | } | ||
| 463 | |||
| 464 | /// Returns the number of allocations. | ||
| 465 | std::size_t size() const noexcept { | ||
| 466 | return num; | ||
| 467 | } | ||
| 468 | |||
| 469 | /// Returns a pointer to the array of allocations. | ||
| 470 | AllocationType const* data() const noexcept { | ||
| 471 | return allocations.get(); | ||
| 472 | } | ||
| 473 | |||
| 474 | /// Returns the allocation in the specified index. | ||
| 475 | /// @pre index < size() | ||
| 476 | AllocationType operator[](std::size_t index) const noexcept { | ||
| 477 | return allocations[index]; | ||
| 478 | } | ||
| 479 | |||
| 480 | /// True when a pool fails to construct. | ||
| 481 | bool IsOutOfPoolMemory() const noexcept { | ||
| 482 | return !device; | ||
| 483 | } | ||
| 484 | |||
| 485 | private: | ||
| 486 | /// Destroys the held allocations if they exist. | ||
| 487 | void Release() noexcept { | ||
| 488 | if (!allocations) { | ||
| 489 | return; | ||
| 490 | } | ||
| 491 | const Span<AllocationType> span(allocations.get(), num); | ||
| 492 | const VkResult result = Free(device, pool, span, *dld); | ||
| 493 | // There's no way to report errors from a destructor. | ||
| 494 | if (result != VK_SUCCESS) { | ||
| 495 | std::terminate(); | ||
| 496 | } | ||
| 497 | } | ||
| 498 | |||
| 499 | std::unique_ptr<AllocationType[]> allocations; | ||
| 500 | std::size_t num = 0; | ||
| 501 | VkDevice device = nullptr; | ||
| 502 | PoolType pool = nullptr; | ||
| 503 | const DeviceDispatch* dld = nullptr; | ||
| 504 | }; | ||
| 505 | |||
| 425 | } // namespace Vulkan::vk | 506 | } // namespace Vulkan::vk |