mirror of
https://github.com/boykopovar/AnyPS5.git
synced 2026-10-10 01:28:44 +02:00
perf(agc): pool device staging buffers by size class
The device-local tier of BufferPool only reused a buffer of the exact size and usage it was asked for. Partial storage-image detiles and retiles ask for a new size and the other transfer direction almost every time, so the tier sat at its budget and missed and evicted dozens of buffers per frame, and the vkFreeMemory calls on the release thread held up submits and allocations on the recording threads. Device-tier requests of 1 MiB and more now round up to an eighth of their octave, the tier's buffers are created for storage and both transfer directions, and a request takes the smallest retained buffer from its own class up to twice its size. Host tiers, the budget and eviction are unchanged. agc_buffer_pool covers the classes, the fit and its bound, reuse only after release, the budget and the unchanged lists.
This commit is contained in:
@@ -359,6 +359,20 @@ if(BUILD_TESTING)
|
||||
endif()
|
||||
endif()
|
||||
|
||||
add_test_executable(agc_buffer_pool_tests EXCLUDE_FROM_ALL tests/BufferPool.cpp)
|
||||
target_include_directories(agc_buffer_pool_tests PRIVATE ${LIBS_INCLUDE_DIR} ${CMAKE_SOURCE_DIR}/core/shader/recompiler ${CMAKE_SOURCE_DIR}/3rdparty/Vulkan-Headers/include)
|
||||
target_link_libraries(agc_buffer_pool_tests PRIVATE libSceAgcDriver libc)
|
||||
set_target_properties(agc_buffer_pool_tests PROPERTIES CXX_EXTENSIONS OFF)
|
||||
configure_windows_unwind(agc_buffer_pool_tests)
|
||||
if(BUILD_TESTING)
|
||||
set_target_properties(agc_buffer_pool_tests PROPERTIES EXCLUDE_FROM_ALL OFF)
|
||||
add_test(NAME agc_buffer_pool COMMAND agc_buffer_pool_tests)
|
||||
if(WIN32)
|
||||
set_tests_properties(agc_buffer_pool PROPERTIES ENVIRONMENT_MODIFICATION
|
||||
"PATH=path_list_prepend:$<TARGET_FILE_DIR:libc>")
|
||||
endif()
|
||||
endif()
|
||||
|
||||
add_test_executable(agc_display_buffer_dcc_tests EXCLUDE_FROM_ALL tests/DisplayBufferDcc.cpp)
|
||||
target_include_directories(agc_display_buffer_dcc_tests PRIVATE ${LIBS_INCLUDE_DIR} ${CMAKE_SOURCE_DIR}/core/shader/recompiler ${CMAKE_SOURCE_DIR}/3rdparty/Vulkan-Headers/include)
|
||||
target_link_libraries(agc_display_buffer_dcc_tests PRIVATE libSceAgcDriver libc)
|
||||
|
||||
@@ -55,7 +55,8 @@ public:
|
||||
BufferPool(const BufferPool&) = delete;
|
||||
BufferPool& operator=(const BufferPool&) = delete;
|
||||
// The size a buffer for `bytes` is created with: its size class, or `bytes` itself when large.
|
||||
static std::size_t Capacity(std::size_t bytes);
|
||||
static std::size_t Capacity(std::size_t bytes, VkMemoryPropertyFlags properties);
|
||||
static VkBufferUsageFlags Usage(VkBufferUsageFlags usage, VkMemoryPropertyFlags properties);
|
||||
std::optional<BufferAllocation> Take(std::size_t bytes, VkBufferUsageFlags usage, VkMemoryPropertyFlags properties = VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT | VK_MEMORY_PROPERTY_HOST_COHERENT_BIT);
|
||||
void Put(const BufferAllocation& allocation) noexcept;
|
||||
|
||||
@@ -92,6 +93,8 @@ private:
|
||||
Tier& tierFor(std::size_t capacity, VkMemoryPropertyFlags properties);
|
||||
// The device tier's budget (APS5_STAGING_POOL_MIB), read once.
|
||||
static VkDeviceSize DeviceBudget();
|
||||
static bool DeviceTiered(VkMemoryPropertyFlags properties);
|
||||
static std::size_t NextCapacity(std::size_t capacity);
|
||||
void destroy(const BufferAllocation& allocation) noexcept;
|
||||
// Moves the tier's least recently used slot (the oldest front of its lists) to `evicted`; the
|
||||
// caller destroys those after releasing the mutex, so builds taking buffers on other threads
|
||||
@@ -116,6 +119,7 @@ private:
|
||||
static constexpr VkDeviceSize smallBudget = 64ull * 1024 * 1024;
|
||||
// Requests of this size and more keep their exact size and go to the large tier.
|
||||
static constexpr std::size_t classLimit = std::size_t{1} << 20u;
|
||||
static constexpr unsigned deviceClassBits = 3;
|
||||
static constexpr std::size_t defaultSlots = 4096;
|
||||
};
|
||||
|
||||
|
||||
@@ -5,6 +5,7 @@
|
||||
#include <chrono>
|
||||
#include <cstdio>
|
||||
#include <cstdlib>
|
||||
#include <limits>
|
||||
|
||||
namespace AgcDriver::Graphics {
|
||||
|
||||
@@ -16,6 +17,13 @@ bool sharedTiers() {
|
||||
return shared;
|
||||
}
|
||||
|
||||
bool exactSizes() {
|
||||
static const bool exact = std::getenv("APS5_NO_BUFFER_CLASSES") != nullptr;
|
||||
return exact;
|
||||
}
|
||||
|
||||
constexpr VkBufferUsageFlags deviceUsage = VK_BUFFER_USAGE_STORAGE_BUFFER_BIT | VK_BUFFER_USAGE_TRANSFER_SRC_BIT | VK_BUFFER_USAGE_TRANSFER_DST_BIT;
|
||||
|
||||
}
|
||||
|
||||
BufferPool::BufferPool(const Context& context) : device(context.device), unmap(context.Function<PFN_vkUnmapMemory>("vkUnmapMemory")), destroyBuffer(context.Function<PFN_vkDestroyBuffer>("vkDestroyBuffer")), freeMemory(context.Function<PFN_vkFreeMemory>("vkFreeMemory")), maxSlots(std::max<std::size_t>(1, std::min<std::size_t>(MaxSlots(), context.limits.maxMemoryAllocationCount / 6u))) {
|
||||
@@ -47,21 +55,35 @@ void BufferPool::destroy(const BufferAllocation& allocation) noexcept {
|
||||
freeMemory(device, allocation.memory, nullptr);
|
||||
}
|
||||
|
||||
std::size_t BufferPool::Capacity(std::size_t bytes) {
|
||||
static const bool exact = std::getenv("APS5_NO_BUFFER_CLASSES") != nullptr;
|
||||
bool BufferPool::DeviceTiered(VkMemoryPropertyFlags properties) {
|
||||
return (properties & VK_MEMORY_PROPERTY_DEVICE_LOCAL_BIT) != 0 && (properties & VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT) == 0 && DeviceBudget() != 0;
|
||||
}
|
||||
|
||||
std::size_t BufferPool::Capacity(std::size_t bytes, VkMemoryPropertyFlags properties) {
|
||||
constexpr std::size_t smallest = 256;
|
||||
if (exact || bytes >= classLimit) return bytes;
|
||||
return std::max(smallest, std::bit_ceil(bytes));
|
||||
if (exactSizes()) return bytes;
|
||||
if (bytes < classLimit) return std::max(smallest, std::bit_ceil(bytes));
|
||||
if (!DeviceTiered(properties)) return bytes;
|
||||
const auto step = std::bit_floor(bytes) >> deviceClassBits;
|
||||
return (bytes + step - 1) / step * step;
|
||||
}
|
||||
|
||||
std::size_t BufferPool::NextCapacity(std::size_t capacity) {
|
||||
return capacity < classLimit ? capacity * 2 : capacity + (std::bit_floor(capacity) >> deviceClassBits);
|
||||
}
|
||||
|
||||
VkBufferUsageFlags BufferPool::Usage(VkBufferUsageFlags usage, VkMemoryPropertyFlags properties) {
|
||||
return DeviceTiered(properties) ? usage | deviceUsage : usage;
|
||||
}
|
||||
|
||||
BufferPool::Tier& BufferPool::tierFor(std::size_t capacity, VkMemoryPropertyFlags properties) {
|
||||
if ((properties & VK_MEMORY_PROPERTY_DEVICE_LOCAL_BIT) != 0 && (properties & VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT) == 0 && DeviceBudget() != 0) return deviceTier;
|
||||
if (DeviceTiered(properties)) return deviceTier;
|
||||
return !sharedTiers() && capacity < classLimit ? smallTier : largeTier;
|
||||
}
|
||||
|
||||
std::optional<BufferAllocation> BufferPool::Take(std::size_t bytes, VkBufferUsageFlags usage, VkMemoryPropertyFlags properties) {
|
||||
static const bool profile = std::getenv("APS5_PROFILE_DRAW") != nullptr;
|
||||
const auto capacity = Capacity(bytes);
|
||||
const auto capacity = Capacity(bytes, properties);
|
||||
std::lock_guard lock(mutex);
|
||||
++clock;
|
||||
if (profile) {
|
||||
@@ -73,7 +95,9 @@ std::optional<BufferAllocation> BufferPool::Take(std::size_t bytes, VkBufferUsag
|
||||
}
|
||||
}
|
||||
auto& tier = tierFor(capacity, properties);
|
||||
const auto found = tier.free.find({capacity, usage, properties});
|
||||
const auto largest = &tier == &deviceTier && !exactSizes() && bytes <= std::numeric_limits<std::size_t>::max() / 2 ? std::max(capacity, bytes * 2) : capacity;
|
||||
auto found = tier.free.end();
|
||||
for (auto size = capacity; size <= largest && found == tier.free.end(); size = NextCapacity(size)) found = tier.free.find({size, usage, properties});
|
||||
if (found == tier.free.end() || found->second.empty()) {
|
||||
++tier.misses;
|
||||
return std::nullopt;
|
||||
|
||||
@@ -7,24 +7,25 @@
|
||||
|
||||
namespace AgcDriver::Graphics {
|
||||
|
||||
Buffer::Buffer(const Context& context, std::size_t size, VkBufferUsageFlags usage, VkMemoryPropertyFlags properties) : context(context), size(size), capacity(BufferPool::Capacity(size)), usage(usage), properties(properties) {
|
||||
Buffer::Buffer(const Context& context, std::size_t size, VkBufferUsageFlags usage, VkMemoryPropertyFlags properties) : context(context), size(size), capacity(BufferPool::Capacity(size, properties)), usage(BufferPool::Usage(usage, properties)), properties(properties) {
|
||||
Require(size != 0, "zero-sized GPU buffer");
|
||||
const bool addressable = (usage & VK_BUFFER_USAGE_SHADER_DEVICE_ADDRESS_BIT) != 0;
|
||||
Require(!addressable || context.bufferDeviceAddress, "buffer device address is not enabled");
|
||||
cache = GetBufferPool(context);
|
||||
if (const auto allocation = cache->Take(size, usage, properties)) {
|
||||
if (const auto allocation = cache->Take(size, this->usage, properties)) {
|
||||
buffer = allocation->buffer;
|
||||
memory = allocation->memory;
|
||||
mapping = allocation->mapping;
|
||||
deviceAddress = allocation->address;
|
||||
allocationBytes = allocation->allocationBytes;
|
||||
capacity = allocation->bytes;
|
||||
ready = true;
|
||||
return;
|
||||
}
|
||||
try {
|
||||
VkBufferCreateInfo info{VK_STRUCTURE_TYPE_BUFFER_CREATE_INFO};
|
||||
info.size = capacity;
|
||||
info.usage = usage;
|
||||
info.usage = this->usage;
|
||||
info.sharingMode = VK_SHARING_MODE_EXCLUSIVE;
|
||||
Check(context.Function<PFN_vkCreateBuffer>("vkCreateBuffer")(context.device, &info, nullptr, &buffer), "vkCreateBuffer");
|
||||
VkMemoryRequirements requirements{};
|
||||
@@ -88,19 +89,20 @@ void Buffer::Invalidate() {
|
||||
Check(context.Function<PFN_vkInvalidateMappedMemoryRanges>("vkInvalidateMappedMemoryRanges")(context.device, 1, &range), "vkInvalidateMappedMemoryRanges");
|
||||
}
|
||||
|
||||
DeviceBuffer::DeviceBuffer(const Context& context, std::size_t size, VkBufferUsageFlags usage) : context(context), size(size), capacity(BufferPool::Capacity(size)), usage(usage) {
|
||||
DeviceBuffer::DeviceBuffer(const Context& context, std::size_t size, VkBufferUsageFlags usage) : context(context), size(size), capacity(BufferPool::Capacity(size, VK_MEMORY_PROPERTY_DEVICE_LOCAL_BIT)), usage(BufferPool::Usage(usage, VK_MEMORY_PROPERTY_DEVICE_LOCAL_BIT)) {
|
||||
Require(size != 0, "zero-sized device buffer");
|
||||
cache = GetBufferPool(context);
|
||||
if (const auto allocation = cache->Take(size, usage, VK_MEMORY_PROPERTY_DEVICE_LOCAL_BIT)) {
|
||||
if (const auto allocation = cache->Take(size, this->usage, VK_MEMORY_PROPERTY_DEVICE_LOCAL_BIT)) {
|
||||
buffer = allocation->buffer;
|
||||
memory = allocation->memory;
|
||||
allocationBytes = allocation->allocationBytes;
|
||||
capacity = allocation->bytes;
|
||||
return;
|
||||
}
|
||||
try {
|
||||
VkBufferCreateInfo info{VK_STRUCTURE_TYPE_BUFFER_CREATE_INFO};
|
||||
info.size = capacity;
|
||||
info.usage = usage;
|
||||
info.usage = this->usage;
|
||||
info.sharingMode = VK_SHARING_MODE_EXCLUSIVE;
|
||||
Check(context.Function<PFN_vkCreateBuffer>("vkCreateBuffer")(context.device, &info, nullptr, &buffer), "vkCreateBuffer device");
|
||||
VkMemoryRequirements requirements{};
|
||||
|
||||
@@ -0,0 +1,264 @@
|
||||
#include "prx/libSceAgcDriver/Graphics/include/BufferPool.hpp"
|
||||
#include "prx/libSceAgcDriver/Graphics/include/Resources.hpp"
|
||||
#include <cstdint>
|
||||
#include <cstdio>
|
||||
#include <map>
|
||||
#include <memory>
|
||||
#include <string>
|
||||
#include <string_view>
|
||||
#include <vector>
|
||||
|
||||
using namespace AgcDriver::Graphics;
|
||||
|
||||
namespace {
|
||||
|
||||
int failures = 0;
|
||||
|
||||
void Expect(bool condition, const std::string& what) {
|
||||
if (condition) return;
|
||||
std::fprintf(stderr, "FAIL: %s\n", what.c_str());
|
||||
++failures;
|
||||
}
|
||||
|
||||
constexpr std::size_t MiB = std::size_t{1} << 20u;
|
||||
constexpr VkMemoryPropertyFlags HostVisible = VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT | VK_MEMORY_PROPERTY_HOST_COHERENT_BIT;
|
||||
|
||||
struct MockDevice {
|
||||
std::uint64_t next = 1;
|
||||
std::map<VkBuffer, VkDeviceSize> sizes;
|
||||
std::map<VkBuffer, VkBufferUsageFlags> usages;
|
||||
std::map<VkDeviceMemory, std::vector<std::byte>> hostMemory;
|
||||
std::uint64_t allocations = 0;
|
||||
std::uint64_t frees = 0;
|
||||
std::uint64_t destroyedBuffers = 0;
|
||||
VkDeviceSize liveBytes = 0;
|
||||
std::map<VkDeviceMemory, VkDeviceSize> memoryBytes;
|
||||
};
|
||||
|
||||
MockDevice mock;
|
||||
|
||||
VKAPI_ATTR VkResult VKAPI_CALL mockCreateBuffer(VkDevice, const VkBufferCreateInfo* info, const VkAllocationCallbacks*, VkBuffer* buffer) {
|
||||
*buffer = reinterpret_cast<VkBuffer>(static_cast<std::uintptr_t>(mock.next++));
|
||||
mock.sizes[*buffer] = info->size;
|
||||
mock.usages[*buffer] = info->usage;
|
||||
return VK_SUCCESS;
|
||||
}
|
||||
|
||||
VKAPI_ATTR void VKAPI_CALL mockGetBufferMemoryRequirements(VkDevice, VkBuffer buffer, VkMemoryRequirements* requirements) {
|
||||
*requirements = {mock.sizes.at(buffer), 256, 3};
|
||||
}
|
||||
|
||||
VKAPI_ATTR VkResult VKAPI_CALL mockAllocateMemory(VkDevice, const VkMemoryAllocateInfo* info, const VkAllocationCallbacks*, VkDeviceMemory* memory) {
|
||||
*memory = reinterpret_cast<VkDeviceMemory>(static_cast<std::uintptr_t>(mock.next++));
|
||||
if (info->memoryTypeIndex == 1) mock.hostMemory[*memory].resize(info->allocationSize);
|
||||
mock.memoryBytes[*memory] = info->allocationSize;
|
||||
mock.liveBytes += info->allocationSize;
|
||||
++mock.allocations;
|
||||
return VK_SUCCESS;
|
||||
}
|
||||
|
||||
VKAPI_ATTR VkResult VKAPI_CALL mockBindBufferMemory(VkDevice, VkBuffer, VkDeviceMemory, VkDeviceSize) {
|
||||
return VK_SUCCESS;
|
||||
}
|
||||
|
||||
VKAPI_ATTR VkResult VKAPI_CALL mockMapMemory(VkDevice, VkDeviceMemory memory, VkDeviceSize, VkDeviceSize, VkMemoryMapFlags, void** data) {
|
||||
*data = mock.hostMemory.at(memory).data();
|
||||
return VK_SUCCESS;
|
||||
}
|
||||
|
||||
VKAPI_ATTR void VKAPI_CALL mockUnmapMemory(VkDevice, VkDeviceMemory) {}
|
||||
|
||||
VKAPI_ATTR void VKAPI_CALL mockDestroyBuffer(VkDevice, VkBuffer, const VkAllocationCallbacks*) {
|
||||
++mock.destroyedBuffers;
|
||||
}
|
||||
|
||||
VKAPI_ATTR void VKAPI_CALL mockFreeMemory(VkDevice, VkDeviceMemory memory, const VkAllocationCallbacks*) {
|
||||
mock.liveBytes -= mock.memoryBytes.at(memory);
|
||||
mock.memoryBytes.erase(memory);
|
||||
mock.hostMemory.erase(memory);
|
||||
++mock.frees;
|
||||
}
|
||||
|
||||
VKAPI_ATTR VkDeviceAddress VKAPI_CALL mockGetBufferDeviceAddress(VkDevice, const VkBufferDeviceAddressInfo* info) {
|
||||
return 0x100000000000ULL + reinterpret_cast<std::uintptr_t>(info->buffer) * 0x10000;
|
||||
}
|
||||
|
||||
PFN_vkVoidFunction VKAPI_CALL mockProc(VkDevice, const char* name) {
|
||||
static const std::map<std::string_view, PFN_vkVoidFunction> table{
|
||||
{"vkCreateBuffer", reinterpret_cast<PFN_vkVoidFunction>(mockCreateBuffer)},
|
||||
{"vkGetBufferMemoryRequirements", reinterpret_cast<PFN_vkVoidFunction>(mockGetBufferMemoryRequirements)},
|
||||
{"vkAllocateMemory", reinterpret_cast<PFN_vkVoidFunction>(mockAllocateMemory)},
|
||||
{"vkBindBufferMemory", reinterpret_cast<PFN_vkVoidFunction>(mockBindBufferMemory)},
|
||||
{"vkMapMemory", reinterpret_cast<PFN_vkVoidFunction>(mockMapMemory)},
|
||||
{"vkUnmapMemory", reinterpret_cast<PFN_vkVoidFunction>(mockUnmapMemory)},
|
||||
{"vkDestroyBuffer", reinterpret_cast<PFN_vkVoidFunction>(mockDestroyBuffer)},
|
||||
{"vkFreeMemory", reinterpret_cast<PFN_vkVoidFunction>(mockFreeMemory)},
|
||||
{"vkGetBufferDeviceAddressKHR", reinterpret_cast<PFN_vkVoidFunction>(mockGetBufferDeviceAddress)},
|
||||
};
|
||||
const auto it = table.find(name);
|
||||
return it == table.end() ? nullptr : it->second;
|
||||
}
|
||||
|
||||
Context mockContext() {
|
||||
Context context{};
|
||||
context.deviceProc = mockProc;
|
||||
context.memory.memoryTypeCount = 2;
|
||||
context.memory.memoryTypes[0].propertyFlags = VK_MEMORY_PROPERTY_DEVICE_LOCAL_BIT;
|
||||
context.memory.memoryTypes[1].propertyFlags = HostVisible | VK_MEMORY_PROPERTY_HOST_CACHED_BIT;
|
||||
context.limits.maxMemoryAllocationCount = 1u << 20u;
|
||||
context.bufferDeviceAddress = true;
|
||||
return context;
|
||||
}
|
||||
|
||||
void SizeClassesAndDirections() {
|
||||
mock = MockDevice{};
|
||||
auto context = mockContext();
|
||||
VkBuffer first = VK_NULL_HANDLE;
|
||||
{
|
||||
DeviceBuffer upload(context, 10 * MiB + 4096, VK_BUFFER_USAGE_STORAGE_BUFFER_BIT | VK_BUFFER_USAGE_TRANSFER_SRC_BIT);
|
||||
first = upload.Handle();
|
||||
Expect(mock.sizes.at(first) == 11 * MiB, "a 10 MiB + 4 KiB device buffer was created with " + std::to_string(mock.sizes.at(first)) + " bytes, not its 11 MiB class");
|
||||
constexpr VkBufferUsageFlags all = VK_BUFFER_USAGE_STORAGE_BUFFER_BIT | VK_BUFFER_USAGE_TRANSFER_SRC_BIT | VK_BUFFER_USAGE_TRANSFER_DST_BIT;
|
||||
Expect((mock.usages.at(first) & all) == all, "a device buffer was created without storage and both transfer directions");
|
||||
Expect(upload.Size() == 10 * MiB + 4096, "a device buffer reports its class instead of the requested size");
|
||||
}
|
||||
const auto made = mock.allocations;
|
||||
DeviceBuffer writeBack(context, 10 * MiB + 512 * 1024, VK_BUFFER_USAGE_STORAGE_BUFFER_BIT | VK_BUFFER_USAGE_TRANSFER_DST_BIT);
|
||||
Expect(writeBack.Handle() == first && mock.allocations == made, "a retained 11 MiB device buffer did not serve a write-back of another size and direction in its class");
|
||||
Expect(writeBack.Size() == 10 * MiB + 512 * 1024, "a reused device buffer reports the retained size");
|
||||
}
|
||||
|
||||
void LargerClass() {
|
||||
mock = MockDevice{};
|
||||
auto context = mockContext();
|
||||
constexpr auto usage = VK_BUFFER_USAGE_STORAGE_BUFFER_BIT | VK_BUFFER_USAGE_TRANSFER_DST_BIT;
|
||||
VkBuffer twelve = VK_NULL_HANDLE;
|
||||
{
|
||||
DeviceBuffer large(context, 12 * MiB, usage);
|
||||
twelve = large.Handle();
|
||||
}
|
||||
{
|
||||
DeviceBuffer smaller(context, 7 * MiB, usage);
|
||||
Expect(smaller.Handle() == twelve, "a 7 MiB request did not take the retained 12 MiB buffer");
|
||||
}
|
||||
const auto made = mock.allocations;
|
||||
{
|
||||
DeviceBuffer tooSmall(context, 5 * MiB, usage);
|
||||
Expect(tooSmall.Handle() != twelve && mock.allocations == made + 1, "a 5 MiB request took a retained buffer of more than twice its size");
|
||||
Expect(mock.sizes.at(tooSmall.Handle()) == 5 * MiB, "a 5 MiB device buffer was not created at its own class");
|
||||
}
|
||||
DeviceBuffer again(context, 12 * MiB, usage);
|
||||
Expect(again.Handle() == twelve, "a buffer served to a smaller request did not return to its own 12 MiB class");
|
||||
DeviceBuffer best(context, 4 * MiB + 1, usage);
|
||||
Expect(best.Handle() != twelve && mock.sizes.at(best.Handle()) == 5 * MiB, "a 4 MiB + 1 request did not take the smallest retained class that fits (5 MiB)");
|
||||
}
|
||||
|
||||
void KeptUntilFence() {
|
||||
mock = MockDevice{};
|
||||
auto context = mockContext();
|
||||
constexpr auto usage = VK_BUFFER_USAGE_STORAGE_BUFFER_BIT | VK_BUFFER_USAGE_TRANSFER_SRC_BIT;
|
||||
std::vector<std::shared_ptr<DeviceBuffer>> batch;
|
||||
for (int i = 0; i < 3; ++i) batch.push_back(std::make_shared<DeviceBuffer>(context, 6 * MiB + static_cast<std::size_t>(i + 1) * 4096, usage));
|
||||
for (std::size_t i = 0; i < batch.size(); ++i) {
|
||||
for (std::size_t j = i + 1; j < batch.size(); ++j) Expect(batch[i]->Handle() != batch[j]->Handle(), "two buffers of one unfinished batch share a VkBuffer");
|
||||
}
|
||||
const auto kept = batch.front()->Handle();
|
||||
const auto made = mock.allocations;
|
||||
{
|
||||
DeviceBuffer during(context, 6 * MiB, usage);
|
||||
Expect(during.Handle() != kept && mock.allocations == made + 1, "a buffer kept by an unfinished batch was handed out again");
|
||||
}
|
||||
batch.clear();
|
||||
std::vector<std::unique_ptr<DeviceBuffer>> after;
|
||||
for (int i = 0; i < 3; ++i) after.push_back(std::make_unique<DeviceBuffer>(context, 6 * MiB + 100, usage));
|
||||
after.push_back(std::make_unique<DeviceBuffer>(context, 6 * MiB, usage));
|
||||
Expect(mock.allocations == made + 1, "the buffers released after the batch fence were not reused (" + std::to_string(mock.allocations - made - 1) + " new allocations)");
|
||||
}
|
||||
|
||||
void Budget() {
|
||||
mock = MockDevice{};
|
||||
auto context = mockContext();
|
||||
constexpr auto usage = VK_BUFFER_USAGE_STORAGE_BUFFER_BIT | VK_BUFFER_USAGE_TRANSFER_DST_BIT;
|
||||
std::vector<std::unique_ptr<DeviceBuffer>> live;
|
||||
for (int i = 0; i < 12; ++i) live.push_back(std::make_unique<DeviceBuffer>(context, (48 + static_cast<std::size_t>(i) * 3) * MiB, usage));
|
||||
const auto first = live.front()->Handle();
|
||||
VkDeviceSize madeBytes = 0;
|
||||
for (const auto& buffer : live) madeBytes += mock.sizes.at(buffer->Handle());
|
||||
Expect(madeBytes > 512 * MiB, "the budget case does not exceed the budget");
|
||||
for (auto& buffer : live) buffer.reset();
|
||||
Expect(mock.frees != 0 && mock.liveBytes <= 512 * MiB, "the device tier retains " + std::to_string(mock.liveBytes / MiB) + " MiB after " + std::to_string(mock.frees) + " evictions, over its 512 MiB budget");
|
||||
DeviceBuffer probe(context, 48 * MiB, usage);
|
||||
Expect(probe.Handle() != first, "the least recently used device buffer survived the eviction");
|
||||
}
|
||||
|
||||
void AddressAndHostUnchanged() {
|
||||
mock = MockDevice{};
|
||||
auto context = mockContext();
|
||||
VkBuffer scratch = VK_NULL_HANDLE;
|
||||
{
|
||||
DeviceBuffer buffer(context, 2 * MiB, VK_BUFFER_USAGE_STORAGE_BUFFER_BIT | VK_BUFFER_USAGE_TRANSFER_SRC_BIT);
|
||||
scratch = buffer.Handle();
|
||||
}
|
||||
Buffer addressed(context, 2 * MiB, VK_BUFFER_USAGE_STORAGE_BUFFER_BIT | VK_BUFFER_USAGE_TRANSFER_SRC_BIT | VK_BUFFER_USAGE_TRANSFER_DST_BIT | VK_BUFFER_USAGE_SHADER_DEVICE_ADDRESS_BIT, VK_MEMORY_PROPERTY_DEVICE_LOCAL_BIT);
|
||||
Expect(addressed.Handle() != scratch && addressed.DeviceAddress() != 0, "an addressable device-local buffer took a scratch buffer without a device address");
|
||||
Buffer shadow(context, 2 * MiB - 256, VK_BUFFER_USAGE_STORAGE_BUFFER_BIT | VK_BUFFER_USAGE_TRANSFER_SRC_BIT | VK_BUFFER_USAGE_TRANSFER_DST_BIT, VK_MEMORY_PROPERTY_DEVICE_LOCAL_BIT);
|
||||
Expect(shadow.Handle() == scratch, "a device-local staging buffer did not share the scratch buffers' class");
|
||||
VkBuffer host = VK_NULL_HANDLE;
|
||||
{
|
||||
Buffer copy(context, 10 * MiB + 4096, VK_BUFFER_USAGE_TRANSFER_SRC_BIT, HostVisible);
|
||||
host = copy.Handle();
|
||||
Expect(mock.sizes.at(host) == 10 * MiB + 4096, "a large host buffer was rounded to a class");
|
||||
Expect(mock.usages.at(host) == VK_BUFFER_USAGE_TRANSFER_SRC_BIT, "a host buffer was created with more usages than asked");
|
||||
}
|
||||
Buffer other(context, 10 * MiB + 8192, VK_BUFFER_USAGE_TRANSFER_SRC_BIT, HostVisible);
|
||||
Expect(other.Handle() != host, "a large host buffer of another exact size was reused");
|
||||
Buffer same(context, 10 * MiB + 4096, VK_BUFFER_USAGE_TRANSFER_SRC_BIT, HostVisible);
|
||||
Expect(same.Handle() == host, "a large host buffer of the same exact size was not reused");
|
||||
}
|
||||
|
||||
void SmallDeviceClasses() {
|
||||
mock = MockDevice{};
|
||||
auto context = mockContext();
|
||||
constexpr auto usage = VK_BUFFER_USAGE_STORAGE_BUFFER_BIT | VK_BUFFER_USAGE_TRANSFER_DST_BIT;
|
||||
VkBuffer mebibyte = VK_NULL_HANDLE;
|
||||
VkBuffer half = VK_NULL_HANDLE;
|
||||
{
|
||||
DeviceBuffer one(context, MiB, usage);
|
||||
mebibyte = one.Handle();
|
||||
}
|
||||
{
|
||||
DeviceBuffer smaller(context, 300 * 1024, usage);
|
||||
Expect(smaller.Handle() != mebibyte && mock.sizes.at(smaller.Handle()) == 512 * 1024, "a 300 KiB request took a 1 MiB buffer or skipped its power-of-two class");
|
||||
half = smaller.Handle();
|
||||
}
|
||||
DeviceBuffer again(context, 260 * 1024, usage);
|
||||
Expect(again.Handle() == half, "a 260 KiB request did not take the retained 512 KiB buffer");
|
||||
constexpr auto indirect = VK_BUFFER_USAGE_INDIRECT_BUFFER_BIT | VK_BUFFER_USAGE_TRANSFER_DST_BIT;
|
||||
VkBuffer tiny = VK_NULL_HANDLE;
|
||||
{
|
||||
DeviceBuffer args(context, 20, indirect);
|
||||
tiny = args.Handle();
|
||||
}
|
||||
const auto made = mock.allocations;
|
||||
DeviceBuffer nextArgs(context, 20, indirect);
|
||||
Expect(nextArgs.Handle() == tiny && mock.allocations == made, "a 20-byte device request did not reuse the retained 256-byte buffer of its class");
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
int main() {
|
||||
try {
|
||||
SizeClassesAndDirections();
|
||||
LargerClass();
|
||||
KeptUntilFence();
|
||||
Budget();
|
||||
AddressAndHostUnchanged();
|
||||
SmallDeviceClasses();
|
||||
} catch (const std::exception& error) {
|
||||
std::fprintf(stderr, "FAIL: %s\n", error.what());
|
||||
return 1;
|
||||
}
|
||||
if (failures != 0) return 1;
|
||||
std::puts("buffer pool tests passed");
|
||||
return 0;
|
||||
}
|
||||
Reference in New Issue
Block a user