fix(agc): allocate oversized descriptor sets from a dedicated pool

DescriptorCache::Allocate returned no set when a set needed more
descriptors of one type than a chain pool holds (4,096 storage
buffers, 1,024 sampled or storage images, 512 samplers). A build's
own set then fell back to a pool of its own, but PrepareDrawBindings
has no fallback and threw "draw snapshot descriptor allocation
failed", so a recorded draw with such a set and a snapshotted input
was skipped.

Such a set now gets a pool sized to it (maxSets 1, no
FREE_DESCRIPTOR_SET_BIT), which Free destroys. Every set's count of
each type is first checked against the device's maxDescriptorSet*
limit; other descriptor types throw. The [descriptors] profile line
counts the dedicated pools.
This commit is contained in:
Celegans12
2026-10-07 16:29:41 +02:00
parent c5e10184a2
commit 14b4edda8d
4 changed files with 347 additions and 6 deletions
@@ -60,9 +60,6 @@ public:
VkDescriptorSet set = VK_NULL_HANDLE;
VkDescriptorPool pool = VK_NULL_HANDLE;
};
// A set of `layout` needing `sizes` descriptors from the pool chain, opening a pool when no pool
// has room; a null set when the needs exceed what one chain pool holds (the caller then makes a
// dedicated pool, as before).
SetAllocation Allocate(VkDescriptorSetLayout layout, std::span<const VkDescriptorPoolSize> sizes);
void Free(const SetAllocation& allocation) noexcept;
// APS5_PROFILE_DRAW counters: layouts served from the map / created, sets allocated, pools opened.
@@ -71,6 +68,7 @@ public:
std::uint64_t layoutMisses = 0;
std::uint64_t sets = 0;
std::uint64_t pools = 0;
std::uint64_t dedicatedPools = 0;
};
Stats Counters() const;
@@ -82,6 +80,7 @@ private:
mutable std::mutex mutex;
std::map<std::vector<std::uint32_t>, VkDescriptorSetLayout> layouts;
std::vector<VkDescriptorPool> pools;
std::vector<VkDescriptorPool> dedicated;
Stats stats;
};
@@ -396,7 +395,6 @@ private:
// Whether the layout is this object's own (no cache) and destroyed with it.
bool ownsLayout = false;
VkDescriptorSet _set = VK_NULL_HANDLE;
// Dedicated pool of this object's set (no cache, or a set too large for a cache pool).
VkDescriptorPool pool = VK_NULL_HANDLE;
// The cache pool the set was allocated from, freed back to it on release.
VkDescriptorPool cachePool = VK_NULL_HANDLE;
@@ -1177,7 +1177,7 @@ void ShaderResources::reportDescriptorCaches() const {
const auto descriptors = context.descriptorCache != nullptr ? context.descriptorCache->Counters() : DescriptorCache::Stats{};
const auto samplerHits = context.samplerCache != nullptr ? context.samplerCache->Hits() : 0;
const auto samplerMisses = context.samplerCache != nullptr ? context.samplerCache->Misses() : 0;
AgcDriver::ProfilePrint_nid_no_patch("[descriptors] layouts %llu hits / %llu created, sets %llu from %llu pools, samplers %llu hits / %llu created\n", static_cast<unsigned long long>(descriptors.layoutHits), static_cast<unsigned long long>(descriptors.layoutMisses), static_cast<unsigned long long>(descriptors.sets), static_cast<unsigned long long>(descriptors.pools), static_cast<unsigned long long>(samplerHits), static_cast<unsigned long long>(samplerMisses));
AgcDriver::ProfilePrint_nid_no_patch("[descriptors] layouts %llu hits / %llu created, sets %llu from %llu pools and %llu dedicated pools, samplers %llu hits / %llu created\n", static_cast<unsigned long long>(descriptors.layoutHits), static_cast<unsigned long long>(descriptors.layoutMisses), static_cast<unsigned long long>(descriptors.sets), static_cast<unsigned long long>(descriptors.pools), static_cast<unsigned long long>(descriptors.dedicatedPools), static_cast<unsigned long long>(samplerHits), static_cast<unsigned long long>(samplerMisses));
}
std::vector<std::uint32_t> ShaderResources::ContentKey(const CompiledShader& shader, bool dataWords, bool movableBuffers) {
@@ -2094,6 +2094,7 @@ DescriptorCache::DescriptorCache(const Context& context) : context(context), des
DescriptorCache::~DescriptorCache() {
for (const auto pool : pools) destroyPool(context.device, pool, nullptr);
for (const auto pool : dedicated) destroyPool(context.device, pool, nullptr);
for (const auto& [key, layout] : layouts) destroyLayout(context.device, layout, nullptr);
}
@@ -2115,21 +2116,53 @@ VkDescriptorSetLayout DescriptorCache::Layout(std::span<const std::uint32_t> key
}
namespace {
std::uint32_t SetDescriptorLimit(const VkPhysicalDeviceLimits& limits, VkDescriptorType type) {
switch (type) {
case VK_DESCRIPTOR_TYPE_STORAGE_BUFFER: return limits.maxDescriptorSetStorageBuffers;
case VK_DESCRIPTOR_TYPE_SAMPLED_IMAGE: return limits.maxDescriptorSetSampledImages;
case VK_DESCRIPTOR_TYPE_STORAGE_IMAGE: return limits.maxDescriptorSetStorageImages;
case VK_DESCRIPTOR_TYPE_SAMPLER: return limits.maxDescriptorSetSamplers;
default: throw std::runtime_error("AGC graphics: descriptor set uses an unsupported descriptor type " + std::to_string(type));
}
}
// What one chain pool holds; a set needing more of any type gets a dedicated pool.
constexpr std::uint32_t ChainPoolSets = 1024;
constexpr std::array<VkDescriptorPoolSize, 4> ChainPoolSizes{{{VK_DESCRIPTOR_TYPE_STORAGE_BUFFER, 4096}, {VK_DESCRIPTOR_TYPE_SAMPLED_IMAGE, 1024}, {VK_DESCRIPTOR_TYPE_STORAGE_IMAGE, 1024}, {VK_DESCRIPTOR_TYPE_SAMPLER, 512}}};
}
DescriptorCache::SetAllocation DescriptorCache::Allocate(VkDescriptorSetLayout layout, std::span<const VkDescriptorPoolSize> sizes) {
bool fits = true;
for (const auto& size : sizes) {
Require(size.descriptorCount <= SetDescriptorLimit(context.limits, size.type), "descriptor set exceeds the device's per-set descriptor limit");
const auto capacity = std::find_if(ChainPoolSizes.begin(), ChainPoolSizes.end(), [&](const auto& item) { return item.type == size.type; });
if (capacity == ChainPoolSizes.end() || size.descriptorCount > capacity->descriptorCount) return {};
if (capacity == ChainPoolSizes.end() || size.descriptorCount > capacity->descriptorCount) fits = false;
}
std::lock_guard lock(mutex);
const auto allocate = context.Resolved(&DeviceFunctions::allocateDescriptorSets, "vkAllocateDescriptorSets");
VkDescriptorSetAllocateInfo allocation{VK_STRUCTURE_TYPE_DESCRIPTOR_SET_ALLOCATE_INFO};
allocation.descriptorSetCount = 1;
allocation.pSetLayouts = &layout;
if (!fits) {
dedicated.reserve(dedicated.size() + 1);
VkDescriptorPoolCreateInfo poolInfo{VK_STRUCTURE_TYPE_DESCRIPTOR_POOL_CREATE_INFO};
poolInfo.maxSets = 1;
poolInfo.poolSizeCount = static_cast<std::uint32_t>(sizes.size());
poolInfo.pPoolSizes = sizes.data();
VkDescriptorPool pool = VK_NULL_HANDLE;
Check(context.Function<PFN_vkCreateDescriptorPool>("vkCreateDescriptorPool")(context.device, &poolInfo, nullptr, &pool), "vkCreateDescriptorPool");
allocation.descriptorPool = pool;
VkDescriptorSet set = VK_NULL_HANDLE;
const auto result = allocate(context.device, &allocation, &set);
if (result != VK_SUCCESS) {
destroyPool(context.device, pool, nullptr);
Check(result, "vkAllocateDescriptorSets");
}
dedicated.push_back(pool);
++stats.dedicatedPools;
++stats.sets;
return {set, pool};
}
// Newest pool first: it has the most room; a full or fragmented pool is left for its sets to
// drain and tried again later.
for (auto it = pools.rbegin(); it != pools.rend(); ++it) {
@@ -2161,6 +2194,11 @@ DescriptorCache::SetAllocation DescriptorCache::Allocate(VkDescriptorSetLayout l
void DescriptorCache::Free(const SetAllocation& allocation) noexcept {
if (allocation.set == VK_NULL_HANDLE || allocation.pool == VK_NULL_HANDLE) return;
std::lock_guard lock(mutex);
if (const auto found = std::find(dedicated.begin(), dedicated.end(), allocation.pool); found != dedicated.end()) {
dedicated.erase(found);
destroyPool(context.device, allocation.pool, nullptr);
return;
}
static_cast<void>(freeSets(context.device, allocation.pool, 1, &allocation.set));
}
@@ -854,6 +854,8 @@ struct MockVulkan {
std::vector<VkDescriptorSetLayoutBinding> layoutBindings;
std::vector<VkDescriptorPoolSize> poolSizes;
std::uint32_t poolMaxSets = 0;
VkDescriptorPoolCreateFlags poolFlags = 0;
std::uint32_t freedSets = 0;
std::vector<MockDescriptorWrite> writes;
std::uint32_t boundSets = 0;
std::uint32_t boundFirst = 0;
@@ -936,6 +938,7 @@ VKAPI_ATTR VkResult VKAPI_CALL mockCreateDescriptorPool(VkDevice, const VkDescri
*pool = makeHandle<VkDescriptorPool>();
mock.poolSizes.assign(info->pPoolSizes, info->pPoolSizes + info->poolSizeCount);
mock.poolMaxSets = info->maxSets;
mock.poolFlags = info->flags;
++mock.live;
return VK_SUCCESS;
}
@@ -950,6 +953,11 @@ VKAPI_ATTR VkResult VKAPI_CALL mockAllocateDescriptorSets(VkDevice, const VkDesc
return VK_SUCCESS;
}
VKAPI_ATTR VkResult VKAPI_CALL mockFreeDescriptorSets(VkDevice, VkDescriptorPool, std::uint32_t count, const VkDescriptorSet*) {
mock.freedSets += count;
return VK_SUCCESS;
}
VKAPI_ATTR void VKAPI_CALL mockUpdateDescriptorSets(VkDevice, std::uint32_t count, const VkWriteDescriptorSet* writes, std::uint32_t copyCount, const VkCopyDescriptorSet*) {
Require(copyCount == 0, "descriptor copies are not expected");
for (std::uint32_t i = 0; i < count; ++i) {
@@ -1047,6 +1055,7 @@ PFN_vkVoidFunction VKAPI_CALL mockProc(VkDevice, const char* name) {
{"vkCreateDescriptorPool", reinterpret_cast<PFN_vkVoidFunction>(mockCreateDescriptorPool)},
{"vkDestroyDescriptorPool", reinterpret_cast<PFN_vkVoidFunction>(mockDestroyDescriptorPool)},
{"vkAllocateDescriptorSets", reinterpret_cast<PFN_vkVoidFunction>(mockAllocateDescriptorSets)},
{"vkFreeDescriptorSets", reinterpret_cast<PFN_vkVoidFunction>(mockFreeDescriptorSets)},
{"vkUpdateDescriptorSets", reinterpret_cast<PFN_vkVoidFunction>(mockUpdateDescriptorSets)},
{"vkCmdBindDescriptorSets", reinterpret_cast<PFN_vkVoidFunction>(mockCmdBindDescriptorSets)},
{"vkCreatePipelineLayout", reinterpret_cast<PFN_vkVoidFunction>(mockCreatePipelineLayout)},
@@ -1336,6 +1345,43 @@ void resourceTests() {
}
}
void descriptorCacheTests() {
mock = MockVulkan{};
auto context = mockContext();
context.limits.maxDescriptorSetStorageBuffers = 8192;
context.limits.maxDescriptorSetSampledImages = 2048;
{
AgcDriver::Graphics::DescriptorCache cache(context);
const auto layout = [&](std::uint32_t count) {
const VkDescriptorSetLayoutBinding binding{0, VK_DESCRIPTOR_TYPE_STORAGE_BUFFER, count, VK_SHADER_STAGE_FRAGMENT_BIT, nullptr};
const std::array<std::uint32_t, 4> key{0, VK_DESCRIPTOR_TYPE_STORAGE_BUFFER, count, VK_SHADER_STAGE_FRAGMENT_BIT};
return cache.Layout(key, std::span(&binding, 1));
};
const auto large = layout(4097);
const auto live = mock.live;
const std::array<VkDescriptorPoolSize, 2> oversized{{{VK_DESCRIPTOR_TYPE_STORAGE_BUFFER, 4097}, {VK_DESCRIPTOR_TYPE_SAMPLED_IMAGE, 3}}};
const auto dedicated = cache.Allocate(large, oversized);
Require(dedicated.set != VK_NULL_HANDLE && dedicated.pool != VK_NULL_HANDLE, "a set above the chain pool's capacity got no set");
Require(mock.live == live + 1 && mock.poolMaxSets == 1 && mock.poolFlags == 0, "an oversized set does not get a pool of its own");
Require(mock.poolSizes.size() == 2 && mock.poolSizes[0].type == VK_DESCRIPTOR_TYPE_STORAGE_BUFFER && mock.poolSizes[0].descriptorCount == 4097 && mock.poolSizes[1].type == VK_DESCRIPTOR_TYPE_SAMPLED_IMAGE && mock.poolSizes[1].descriptorCount == 3, "the dedicated pool is not sized to its set");
Require(cache.Counters().pools == 0 && cache.Counters().sets == 1, "an oversized set was counted in the chain pools");
cache.Free(dedicated);
Require(mock.live == live && mock.freedSets == 0, "freeing an oversized set did not destroy its pool");
const std::array<VkDescriptorPoolSize, 1> fitting{{{VK_DESCRIPTOR_TYPE_STORAGE_BUFFER, 4096}}};
const auto chained = cache.Allocate(layout(4096), fitting);
Require(chained.set != VK_NULL_HANDLE && mock.poolMaxSets == 1024 && mock.poolFlags == VK_DESCRIPTOR_POOL_CREATE_FREE_DESCRIPTOR_SET_BIT && cache.Counters().pools == 1, "a set the chain pool holds left the chain");
cache.Free(chained);
Require(mock.freedSets == 1 && mock.live == live + 2, "a chain set was not freed back to its pool");
const std::array<VkDescriptorPoolSize, 1> beyondDevice{{{VK_DESCRIPTOR_TYPE_STORAGE_BUFFER, 8193}}};
expectFailure([&] { cache.Allocate(large, beyondDevice); }, "descriptor set exceeds the device's per-set descriptor limit");
const std::array<VkDescriptorPoolSize, 1> storageImages{{{VK_DESCRIPTOR_TYPE_STORAGE_IMAGE, 1}}};
expectFailure([&] { cache.Allocate(large, storageImages); }, "descriptor set exceeds the device's per-set descriptor limit");
const std::array<VkDescriptorPoolSize, 1> uniformBuffers{{{VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER, 1}}};
expectFailure([&] { cache.Allocate(large, uniformBuffers); }, "descriptor set uses an unsupported descriptor type 6");
}
Require(mock.live == 0, "the descriptor cache leaked a pool or layout");
}
void misalignedShaderDataTests() {
mock = MockVulkan{};
auto context = mockContext();
@@ -2152,6 +2198,7 @@ int main() {
InitialContextTests();
pushConstantTests();
resourceTests();
descriptorCacheTests();
misalignedShaderDataTests();
debugBranchTests();
meshArgumentTests();
@@ -0,0 +1,258 @@
#include "prx/libSceAgcDriver/Execution/include/GuestMemory.hpp"
#include "prx/libSceAgcDriver/Graphics/include/Resources.hpp"
#include "prx/libSceAgcDriver/Graphics/include/ShaderResources.hpp"
#include "VulkanTestDevice.hpp"
#include <SDL_loadso.h>
#include <array>
#include <cstdint>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <initializer_list>
#include <iostream>
#include <memory>
#include <mutex>
#include <span>
#include <string>
#include <vector>
namespace {
using AgcDriver::Graphics::Check;
using AgcDriver::Graphics::Context;
using AgcDriver::Graphics::DescriptorCache;
using AgcDriver::Graphics::Require;
constexpr std::uint32_t Buffers = 4097;
constexpr std::uint32_t Spacing = 256;
class Device {
public:
Device() {
#ifdef _WIN32
library = SDL_LoadObject("vulkan-1.dll");
#else
library = SDL_LoadObject("libvulkan.so.1");
#endif
Require(library != nullptr, "cannot load Vulkan");
try {
instanceProc = reinterpret_cast<PFN_vkGetInstanceProcAddr>(SDL_LoadFunction(library, "vkGetInstanceProcAddr"));
Require(instanceProc != nullptr, "missing Vulkan instance resolver");
VkApplicationInfo application{VK_STRUCTURE_TYPE_APPLICATION_INFO};
application.apiVersion = VK_API_VERSION_1_1;
VkInstanceCreateInfo info{VK_STRUCTURE_TYPE_INSTANCE_CREATE_INFO};
info.pApplicationInfo = &application;
Check(function<PFN_vkCreateInstance>("vkCreateInstance")(&info, nullptr, &instance), "vkCreateInstance");
std::uint32_t count = 0;
const auto enumerate = function<PFN_vkEnumeratePhysicalDevices>("vkEnumeratePhysicalDevices");
Check(enumerate(instance, &count, nullptr), "vkEnumeratePhysicalDevices");
Require(count != 0, "no Vulkan device");
std::vector<VkPhysicalDevice> devices(count);
Check(enumerate(instance, &count, devices.data()), "vkEnumeratePhysicalDevices");
const auto properties = function<PFN_vkGetPhysicalDeviceProperties>("vkGetPhysicalDeviceProperties");
context.physical = devices.front();
for (const auto candidate : devices) {
VkPhysicalDeviceProperties described{};
properties(candidate, &described);
if (described.deviceType == VK_PHYSICAL_DEVICE_TYPE_DISCRETE_GPU) {
context.physical = candidate;
break;
}
}
VkPhysicalDeviceProperties selected{};
properties(context.physical, &selected);
context.limits = selected.limits;
std::printf("device %s\n", selected.deviceName);
const auto queues = function<PFN_vkGetPhysicalDeviceQueueFamilyProperties>("vkGetPhysicalDeviceQueueFamilyProperties");
queues(context.physical, &count, nullptr);
std::vector<VkQueueFamilyProperties> families(count);
queues(context.physical, &count, families.data());
std::uint32_t family = 0;
while (family < count && (families[family].queueFlags & VK_QUEUE_COMPUTE_BIT) == 0) ++family;
Require(family < count, "no Vulkan compute queue");
const float priority = 1;
VkDeviceQueueCreateInfo queue{VK_STRUCTURE_TYPE_DEVICE_QUEUE_CREATE_INFO};
queue.queueFamilyIndex = family;
queue.queueCount = 1;
queue.pQueuePriorities = &priority;
VkDeviceCreateInfo device{VK_STRUCTURE_TYPE_DEVICE_CREATE_INFO};
device.queueCreateInfoCount = 1;
device.pQueueCreateInfos = &queue;
Check(function<PFN_vkCreateDevice>("vkCreateDevice")(context.physical, &device, nullptr, &context.device), "vkCreateDevice");
context.deviceProc = function<PFN_vkGetDeviceProcAddr>("vkGetDeviceProcAddr");
function<PFN_vkGetPhysicalDeviceMemoryProperties>("vkGetPhysicalDeviceMemoryProperties")(context.physical, &context.memory);
context.formatProperties = function<PFN_vkGetPhysicalDeviceFormatProperties>("vkGetPhysicalDeviceFormatProperties");
context.Function<PFN_vkGetDeviceQueue>("vkGetDeviceQueue")(context.device, family, 0, &context.queue);
VkCommandPoolCreateInfo pool{VK_STRUCTURE_TYPE_COMMAND_POOL_CREATE_INFO};
pool.queueFamilyIndex = family;
Check(context.Function<PFN_vkCreateCommandPool>("vkCreateCommandPool")(context.device, &pool, nullptr, &context.pool), "vkCreateCommandPool");
} catch (...) {
release();
throw;
}
}
~Device() { release(); }
Device(const Device&) = delete;
Device& operator=(const Device&) = delete;
const Context& GetContext() const { return context; }
private:
template<typename TFunction>
TFunction function(const char* name) const {
const auto result = reinterpret_cast<TFunction>(instanceProc(instance, name));
Require(result != nullptr, name);
return result;
}
void release() noexcept {
if (context.pool != VK_NULL_HANDLE) context.Function<PFN_vkDestroyCommandPool>("vkDestroyCommandPool")(context.device, context.pool, nullptr);
context.bufferPool.reset();
if (context.device != VK_NULL_HANDLE) function<PFN_vkDestroyDevice>("vkDestroyDevice")(context.device, nullptr);
if (instance != VK_NULL_HANDLE) function<PFN_vkDestroyInstance>("vkDestroyInstance")(instance, nullptr);
if (library != nullptr) SDL_UnloadObject(library);
}
void* library = nullptr;
PFN_vkGetInstanceProcAddr instanceProc = nullptr;
VkInstance instance = VK_NULL_HANDLE;
Context context{};
};
void Emit(std::vector<std::uint32_t>& words, std::uint32_t opcode, std::initializer_list<std::uint32_t> operands) {
words.push_back(static_cast<std::uint32_t>((operands.size() + 1u) << 16u) | opcode);
words.insert(words.end(), operands);
}
std::vector<std::uint32_t> SumLastTwoModule() {
enum : std::uint32_t { Void = 1, FunctionType, Uint, Block, Length, Array, ArrayPointer, Variable, UintPointer, Zero, Last, BeforeLast, Main, Label, LastPointer, LastValue, BeforeLastPointer, BeforeLastValue, Sum, Destination, Bound };
constexpr std::uint32_t storageBuffer = 12;
std::vector<std::uint32_t> words{0x07230203u, 0x00010300u, 0u, Bound, 0u};
Emit(words, 17, {1});
Emit(words, 14, {0, 1});
Emit(words, 15, {5, Main, 0x6e69616du, 0u});
Emit(words, 16, {Main, 17, 1, 1, 1});
Emit(words, 71, {Block, 2});
Emit(words, 72, {Block, 0, 35, 0});
Emit(words, 71, {Variable, 34, 0});
Emit(words, 71, {Variable, 33, 0});
Emit(words, 19, {Void});
Emit(words, 33, {FunctionType, Void});
Emit(words, 21, {Uint, 32, 0});
Emit(words, 30, {Block, Uint});
Emit(words, 43, {Uint, Length, Buffers});
Emit(words, 28, {Array, Block, Length});
Emit(words, 32, {ArrayPointer, storageBuffer, Array});
Emit(words, 59, {ArrayPointer, Variable, storageBuffer});
Emit(words, 32, {UintPointer, storageBuffer, Uint});
Emit(words, 43, {Uint, Zero, 0});
Emit(words, 43, {Uint, Last, Buffers - 1u});
Emit(words, 43, {Uint, BeforeLast, Buffers - 2u});
Emit(words, 54, {Void, Main, 0, FunctionType});
Emit(words, 248, {Label});
Emit(words, 65, {UintPointer, LastPointer, Variable, Last, Zero});
Emit(words, 61, {Uint, LastValue, LastPointer});
Emit(words, 65, {UintPointer, BeforeLastPointer, Variable, BeforeLast, Zero});
Emit(words, 61, {Uint, BeforeLastValue, BeforeLastPointer});
Emit(words, 128, {Uint, Sum, LastValue, BeforeLastValue});
Emit(words, 65, {UintPointer, Destination, Variable, Zero, Zero});
Emit(words, 62, {Destination, Sum});
Emit(words, 253, {});
Emit(words, 56, {});
return words;
}
std::uint32_t Input(std::uint32_t buffer) {
return buffer * 3u + 1u;
}
void Run(const Context& context) {
DescriptorCache cache(context);
const VkDescriptorSetLayoutBinding binding{0, VK_DESCRIPTOR_TYPE_STORAGE_BUFFER, Buffers, VK_SHADER_STAGE_COMPUTE_BIT, nullptr};
const std::array<std::uint32_t, 4> key{0, VK_DESCRIPTOR_TYPE_STORAGE_BUFFER, Buffers, VK_SHADER_STAGE_COMPUTE_BIT};
auto setLayout = cache.Layout(key, std::span(&binding, 1));
const std::array<VkDescriptorPoolSize, 1> sizes{{{VK_DESCRIPTOR_TYPE_STORAGE_BUFFER, Buffers}}};
const auto first = cache.Allocate(setLayout, sizes);
const auto second = cache.Allocate(setLayout, sizes);
Require(first.set != VK_NULL_HANDLE && second.set != VK_NULL_HANDLE, "a set of " + std::to_string(Buffers) + " storage buffers, above the chain pool's capacity, got no descriptor set");
Require(first.pool != second.pool && cache.Counters().pools == 0, "the oversized sets do not have a pool each");
AgcDriver::Graphics::Buffer storage(context, static_cast<std::size_t>(Buffers) * Spacing, VK_BUFFER_USAGE_STORAGE_BUFFER_BIT);
auto* words = reinterpret_cast<std::uint32_t*>(storage.Bytes().data());
for (std::uint32_t buffer = 0; buffer < Buffers; ++buffer) words[buffer * (Spacing / 4u)] = Input(buffer);
std::vector<VkDescriptorBufferInfo> infos;
for (std::uint32_t buffer = 0; buffer < Buffers; ++buffer) infos.push_back({storage.Handle(), static_cast<VkDeviceSize>(buffer) * Spacing, 4});
VkWriteDescriptorSet write{VK_STRUCTURE_TYPE_WRITE_DESCRIPTOR_SET};
write.dstSet = second.set;
write.descriptorCount = Buffers;
write.descriptorType = VK_DESCRIPTOR_TYPE_STORAGE_BUFFER;
write.pBufferInfo = infos.data();
context.Function<PFN_vkUpdateDescriptorSets>("vkUpdateDescriptorSets")(context.device, 1, &write, 0, nullptr);
const auto module = SumLastTwoModule();
VkShaderModuleCreateInfo moduleInfo{VK_STRUCTURE_TYPE_SHADER_MODULE_CREATE_INFO};
moduleInfo.codeSize = module.size() * sizeof(std::uint32_t);
moduleInfo.pCode = module.data();
VkShaderModule shader = VK_NULL_HANDLE;
Check(context.Function<PFN_vkCreateShaderModule>("vkCreateShaderModule")(context.device, &moduleInfo, nullptr, &shader), "vkCreateShaderModule");
VkPipelineLayoutCreateInfo layoutInfo{VK_STRUCTURE_TYPE_PIPELINE_LAYOUT_CREATE_INFO};
layoutInfo.setLayoutCount = 1;
layoutInfo.pSetLayouts = &setLayout;
VkPipelineLayout pipelineLayout = VK_NULL_HANDLE;
Check(context.Function<PFN_vkCreatePipelineLayout>("vkCreatePipelineLayout")(context.device, &layoutInfo, nullptr, &pipelineLayout), "vkCreatePipelineLayout");
VkComputePipelineCreateInfo pipelineInfo{VK_STRUCTURE_TYPE_COMPUTE_PIPELINE_CREATE_INFO};
pipelineInfo.stage = {VK_STRUCTURE_TYPE_PIPELINE_SHADER_STAGE_CREATE_INFO};
pipelineInfo.stage.stage = VK_SHADER_STAGE_COMPUTE_BIT;
pipelineInfo.stage.module = shader;
pipelineInfo.stage.pName = "main";
pipelineInfo.layout = pipelineLayout;
VkPipeline pipeline = VK_NULL_HANDLE;
Check(context.Function<PFN_vkCreateComputePipelines>("vkCreateComputePipelines")(context.device, VK_NULL_HANDLE, 1, &pipelineInfo, nullptr, &pipeline), "vkCreateComputePipelines");
{
std::lock_guard gpu(AgcDriver::GuestMemory::GpuMutex());
AgcDriver::Graphics::CommandBatch batch(context);
context.Function<PFN_vkCmdBindPipeline>("vkCmdBindPipeline")(batch.Handle(), VK_PIPELINE_BIND_POINT_COMPUTE, pipeline);
context.Function<PFN_vkCmdBindDescriptorSets>("vkCmdBindDescriptorSets")(batch.Handle(), VK_PIPELINE_BIND_POINT_COMPUTE, pipelineLayout, 0, 1, &second.set, 0, nullptr);
context.Function<PFN_vkCmdDispatch>("vkCmdDispatch")(batch.Handle(), 1, 1, 1);
AgcDriver::Graphics::RecordMemoryBarrier(context, batch.Handle(), VK_PIPELINE_STAGE_COMPUTE_SHADER_BIT, VK_PIPELINE_STAGE_HOST_BIT, VK_ACCESS_SHADER_WRITE_BIT, VK_ACCESS_HOST_READ_BIT);
batch.SubmitAndWait();
}
context.Function<PFN_vkDestroyPipeline>("vkDestroyPipeline")(context.device, pipeline, nullptr);
context.Function<PFN_vkDestroyPipelineLayout>("vkDestroyPipelineLayout")(context.device, pipelineLayout, nullptr);
context.Function<PFN_vkDestroyShaderModule>("vkDestroyShaderModule")(context.device, shader, nullptr);
storage.Invalidate();
const auto expected = Input(Buffers - 1u) + Input(Buffers - 2u);
Require(words[0] == expected, "the dispatch through the oversized set wrote " + std::to_string(words[0]) + ", expected " + std::to_string(expected) + " from its last two storage buffers");
cache.Free(first);
cache.Free(second);
const auto again = cache.Allocate(setLayout, sizes);
Require(again.set != VK_NULL_HANDLE && again.pool != VK_NULL_HANDLE && cache.Counters().pools == 0, "an oversized set after the others were freed got no descriptor set");
cache.Free(again);
}
}
int main() {
try {
std::unique_ptr<Device> device;
try {
device = std::make_unique<Device>();
} catch (const std::exception& error) {
if (std::getenv("ANYPS5_REQUIRE_VULKAN") != nullptr) throw;
std::printf("skipped, no usable Vulkan device: %s\n", error.what());
return VulkanTestSkipped;
}
const auto& limits = device->GetContext().limits;
if (limits.maxPerStageDescriptorStorageBuffers < Buffers || limits.maxPerStageResources < Buffers || limits.maxDescriptorSetStorageBuffers < Buffers) {
std::printf("skipped, the device binds fewer than %u storage buffers per stage or set\n", Buffers);
return VulkanTestSkipped;
}
Run(device->GetContext());
std::puts("oversized descriptor set tests passed");
return 0;
} catch (const std::exception& error) {
std::cerr << error.what() << '\n';
return 1;
}
}