Files
fs2open.github.com/code/graphics/vulkan/VulkanDescriptorManager.cpp
T
Fabian WoltermannandClaude Opus 5.5 8ef44a411d ImGui frame profiler overlay, and Vulkan per-draw descriptor set churn fix (#7694)
* chore: vendor ImPlot library

Adds ImPlot as a build dependency, to be used for the upcoming ImGui
frame profiler overlay's frametime graph and pie chart.

* feat: add ImGui frame profiler overlay

Replaces the old text-dump frame profile display (-profile_frame_time)
with a runtime-toggleable ImGui/ImPlot overlay showing average/median
frametime, a scrolling frametime graph, and a pie chart of the top
contributors to frame time. Toggled via a new "Frame Profiler Overlay"
option, which also seeds from -profile_frame_time for backward
compatibility and now drives frame profiling collection at runtime
instead of only at startup.

* Add graphics and Vulkan debug stats to ImGui profiler overlay

- Integrated per-backend diagnostic counters into the ImGui frame profiler overlay, triggered by `-gr_debug`.
- Consolidated and cleaned up Vulkan rendering state and descriptor management for compatibility with ImGui's Vulkan pipeline.
- Enabled uniform buffer and draw statistics reporting for enhanced performance insights during debugging.

* Update ImPlot to v1.1 WIP and improve internal consistency

- Adjust copyright notices for extended license coverage (2025-2026).
- Modernize constants with `constexpr` replacements, improving type safety and clarity.
- Enforce `nullptr` usage over `NULL` throughout, aligning with modern C++ practices.
- Add various enhancements to ImPlot legends, markers, and axes behavior.
- Introduce additional helper methods for time handling and timestamp calculations.
- Address UI and typo improvements in comments and documentation, refining accuracy.

* Refactor frame profiler to use per-category self-time accumulation

- Introduced `accumulate_self_times` for single-pass exclusive time computation, improving performance and accuracy.
- Updated `FrameProfiler` and overlay snapshot logic to leverage `Category`'s stable `getId` and `getCount`.
- Replaced pie chart with a stacked bar for frame budget visualization, optimizing rendering cost.
- Cached `get_tid` and `get_pid` with thread-local storage to minimize syscall overhead.
- Added support for unit testing frame profiling functionality and included new test cases.
- Improved code comments and clarified documentation.

* Use dynamic uniform buffers to stop per-draw descriptor set churn in Vulkan

The Material and PerDraw descriptor sets were reallocated and fully rewritten on
essentially every draw call. model_draw_list::render_buffer rebinds ModelData with
a fresh uniform buffer offset for each queued draw, and that offset was part of the
Material set's memoization key, so the cache in VulkanDrawManager::applyMaterial
missed every time - taking a frame-pool allocation, a full template write, the
16-entry material texture resolution and a vkUpdateDescriptorSets with it.
GenericData/Matrices did the same to the PerDraw set. renderShadowDraw was worse
still: all three sets allocated and rewritten per shadow draw, with no memoization
at all.

A large asteroid field (Solaris m21) measured 2566 descriptor sets and 19107
descriptor writes per frame at 1606 model draws, and grew an extra descriptor pool
chunk every frame.

Declare the bindings whose offset moves per draw - MaterialBinding::ModelData,
MaterialBinding::ShadowMapData, PerDrawBinding::GenericData and
PerDrawBinding::Matrices - as eUniformBufferDynamic. DescriptorWriter::setBuffer
now splits a dynamic binding's offset out of the descriptor, writing
{buffer, 0, range} and stashing the offset for vkCmdBindDescriptorSets'
pDynamicOffsets, so an offset-only change leaves the set's contents identical and
the memoization caches start hitting. Shader sources are unchanged.

VulkanStateTracker::bindDescriptorSet now compares the dynamic offsets as well as
the set handle. This is required, not an optimization: the common case is now the
same set rebound with a new offset, and the old handle-only redundancy check would
have skipped that bind and fed every draw the previous draw's uniforms.

renderShadowDraw memoizes its three sets the way applyMaterial does. It does not
invalidate applyMaterial's caches - those record what a set contains, the shadow
pass allocates its own sets rather than overwriting theirs, and it binds through
the state tracker, so the next material cache hit still rebinds correctly.

Also switch allocateFrameSet to the non-allocating allocateDescriptorSets overload;
the vector-returning one heap-allocated once per set, thousands of times a frame.
MAX_SETS_PER_POOL stays at 1024 - the per-frame pool growth it used to log was a
symptom of the churn, and one chunk now covers the scene outright.

Measured on that mission in a Debug build, A/B on the same tree with and without
this change:

  descriptor sets/frame  @ ~1370 draws  2566   -> 250-345
  descriptor writes/frame                19107 -> ~1700-2300
  median frametime                       92.9/83.6 ms -> 72.0/66.5 ms
  Render Buffer (profiler overlay)       24.68 ms (30.8%) -> 12.73 ms (18.4%)
  Build Shadow Map                       5.44 ms -> off the top-5 entirely

Release builds already inlined most of this away and are unchanged, as expected.
Verified with the validation layers (sync validation enabled): no new VUIDs versus
the same build without the change.

Co-Authored-By: Claude Opus 5 <[email protected]>

* Refactor Vulkan descriptor set binding to reduce dynamic offset churn

- Consolidated dynamic descriptor offset logic into `DescriptorWriter::bindSets`, replacing repetitive `vkCmdBindDescriptorSets` patterns across Vulkan modules.
- Eliminated redundant descriptor binding operations for Material and PerDraw sets by centralizing logic in `bindSets`.
- Simplified descriptors by directly splitting offset changes from descriptor contents, avoiding unnecessary updates.
- Updated comments and documentation to align with the new approach while maintaining reference consistency.
- Applied optimizations across Vulkan modules (`VulkanPostProcessing`, `VulkanDescriptorManager`, etc.) to reduce per-draw overhead and improve readability.

* Enable primitive restart for supported topologies in Vulkan pipeline

- Added `topologySupportsPrimitiveRestart` to determine restart eligibility for each topology.
- Updated `primitiveRestartEnable` to be conditionally set based on topology, addressing compatibility issues with Metal/MoltenVK.
- Prevented crash scenarios on macOS by ensuring pipelines are valid for strip/fan topologies requiring primitive restart.

* Enable VK_KHR_portability_subset for compatibility

* Replace exception with sentinel for non-dynamic Vulkan descriptor bindings

- Introduced `DYNAMIC_SLOT_INVALID` sentinel to replace the throw in `dynamic_offset_slot` for MSVC compatibility.
- Added `static_assert` checks to validate binding constants, ensuring compile-time validation for dynamic offsets.
- Updated comments to clarify design rationale and avoid potential misuse.

* Add memory statistics and tracking to profiler overlay

* fix ci issues

* Add OpenGL memory statistics to the profiler overlay

The memory panel showed no texture bytes and no geometry bytes under
OpenGL. Only the depth renderbuffers went into the render target group.
All three groups now give a value.

Textures: GL_texture_bytes_used holds the bytes of every live texture
cache slot. opengl_texture_release_bytes() removes the bytes of a slot at
each site that drops its texture data. The bitmap render targets record
the bytes of their color texture.

Geometry: the unused GL_vertex_data_in global becomes
GL_geometry_bytes_used, which counts only vertex and index buffers. This
matches the geometry group of the Vulkan backend.
gr_opengl_delete_buffer() sets the size of the buffer to 0, because
opengl_destroy_all_buffers() calls it a second time for a buffer that the
game already deleted.

Render targets: three new functions add the scene textures, the
post-processing textures, and the shadow map to the group. They sum the
bytes at the moment of the call from the textures that exist, thus they
need no bookkeeping at the create and delete sites.

Co-Authored-By: Claude Opus 5 <[email protected]>

* Fix profiler overlay crash on a category that is not a static

The category registry kept a Category pointer for each id. The comment said that all categories
are global statics, but that is not correct. The Lua API tracing_category constructs a Category
and copies it into a script-owned object. The registry then kept a pointer to the dead temporary.
build_overlay_snapshot called getName() on that pointer, made an SCP_string from the garbage
char*, and the process stopped with SIGABRT.

The registry now keeps the name by value and never keeps a Category pointer. getById() becomes
getNameById(), which is all that build_overlay_snapshot needs. A copy of a Category keeps the id
of the original, which stays correct because the id only selects a name.

Add two tests: one shows that the name stays available after the Category object is dead, and one
drives the full path that failed, from processEvent to the overlay snapshot.

Co-Authored-By: Claude Opus 5 <[email protected]>

* Replace category pointers with IDs to prevent use-after-free

- Updated `trace_event` to store `category_id` instead of a pointer to prevent dangling references to category objects that may already be destroyed.
- Modified all references to category names to use `Category::getNameById()` instead of dereferencing pointers.
- Expanded memory statistics in the profiler overlay with new sound, Lua, and OS-level memory usage tracking.
- Added new tests to verify proper use of category IDs in frame profiling and event tracing.
- Improved memory safety and stability across tracing, profiling, and overlay systems.

* Refactor profiler overlay to use snapshot-based frame timing

* Address review feedback on the profiler overlay

Skip complete::end for a scope that did not start. This fixes a crash
when Lua turns the overlay on while a scope is open. Track the GPU begin
event so that turning the overlay off mid-scope still closes it.

Use XSTR 1936 and 1937 for the overlay option. Master now uses 1932
and 1933.

Remove the default value of dynamicOffsets in bindDescriptorSet. Use the
PerDraw binding constants in the PerDraw key.

Co-Authored-By: Claude Sonnet 5.5 <[email protected]>

* Use wall-clock frame time and read the history without a copy

The Avg and FPS line used traced self-time, which reads too high.
Measure the time between overlay frames instead. Draw the graph with
PlotLineG so the history deque needs no per-frame copy.

Co-Authored-By: Claude Sonnet 5.5 <[email protected]>

* Address second round of review feedback on the profiler overlay

- Make the category registry thread-safe and give categories with the
  same name one id, so the registry does not grow without limit.
- Add tests that toggle the profiler inside an open trace scope.
- Correct the next free XSTR id note to 1938.
- Use the existing Windows include block for psapi.h in osapi.cpp.
- Use list_range in ship_get_num_ships() and shorten some comments.

Co-Authored-By: Claude Opus 5.5 <[email protected]>

* Return size_t from Category::getCount() and tidy the registry code

- Use structured bindings and std::scoped_lock in the category registry.
- Return size_t from getCount() and update its callers and tests.

Co-Authored-By: Claude Opus 5.5 <[email protected]>

* Address third round of review feedback on the profiler overlay

- Move the overlay option strings to XSTR 1940 and 1941. The touch
  overlay on master uses 1936 to 1939.
- Give FrameProfiler the main thread ID when it is made. Before, it
  used the thread of the first event, which was a race.
- Let mouse motion go to the game while the pointer is over the
  overlay, so that mouse flight does not stop there.
- Find the dynamic bindings of the PerDraw key with
  dynamic_offset_slot(), as the other call sites do.
- Make disable_inside_open_scope count the GPU timestamp queries, so
  that it fails if end() does not close the GPU event.
- Make the copy test copy the category, not bind a reference.

Co-Authored-By: Claude Opus 5.5 <[email protected]>

* apply review suggestions

---------

Co-authored-by: Claude Opus 5 <[email protected]>
2026-11-11 08:16:33 +01:00

534 lines
20 KiB
C++

#include "VulkanDescriptorManager.h"
#include "VulkanBuffer.h"
#include "VulkanTexture.h"
#include "VulkanRaytracing.h"
namespace graphics::vulkan {
// ========== Static set templates ==========
static constexpr DescriptorBindingTemplate s_globalBindings[] = {
{GlobalBinding::Lights, vk::DescriptorType::eUniformBuffer, 1, vk::ShaderStageFlagBits::eVertex | vk::ShaderStageFlagBits::eFragment},
{GlobalBinding::DeferredData, vk::DescriptorType::eUniformBuffer, 1, vk::ShaderStageFlagBits::eVertex | vk::ShaderStageFlagBits::eFragment},
{GlobalBinding::ShadowMap, vk::DescriptorType::eCombinedImageSampler, 1, vk::ShaderStageFlagBits::eFragment, vk::ImageViewType::e2DArray},
{GlobalBinding::EnvMap, vk::DescriptorType::eCombinedImageSampler, 1, vk::ShaderStageFlagBits::eFragment, vk::ImageViewType::eCube},
{GlobalBinding::IrradianceMap, vk::DescriptorType::eCombinedImageSampler, 1, vk::ShaderStageFlagBits::eFragment, vk::ImageViewType::eCube},
{GlobalBinding::ShadowCascadeParams, vk::DescriptorType::eUniformBuffer, 1, vk::ShaderStageFlagBits::eVertex | vk::ShaderStageFlagBits::eFragment},
};
static constexpr DescriptorBindingTemplate s_globalTlasBinding{
GlobalBinding::Tlas, vk::DescriptorType::eAccelerationStructureKHR, 1, vk::ShaderStageFlagBits::eFragment};
// GLOBAL_DYNAMIC_OFFSET_COUNT is stated rather than derived because this set's template is
// assembled at runtime (see below); keep the two in agreement. The optional TLAS binding is not
// a dynamic UBO, so it cannot change the count either.
static_assert(dynamic_offset_count(s_globalBindings) == GLOBAL_DYNAMIC_OFFSET_COUNT,
"The Global set gained a dynamic binding; GLOBAL_DYNAMIC_OFFSET_COUNT must follow.");
// The Global set's layout must omit the TLAS binding entirely on devices without
// VK_KHR_acceleration_structure enabled -- a descriptor type the device didn't
// enable the extension for is invalid in vkCreateDescriptorSetLayout. So, unlike
// the other set templates, the Global template is assembled at runtime in
// createSetLayouts() based on the raytracingSupported flag passed to init(), into
// the per-instance m_globalBindingsRuntime/m_globalTemplateRuntime members
// (see VulkanDescriptorManager.h), rather than compiled in as a fixed constexpr array.
// The binding declarations themselves live in the header (MaterialSetBindings /
// PerDrawSetBindings) because the dynamic-offset layout is derived from them there.
static constexpr DescriptorSetTemplate s_materialTemplate(MaterialSetBindings);
static constexpr DescriptorSetTemplate s_perDrawTemplate(PerDrawSetBindings);
// ========== Static uniform binding mappings ==========
static constexpr VulkanDescriptorManager::UniformBindingEntry s_globalUBOs[] = {
{GlobalBinding::Lights, uniform_block_type::Lights},
{GlobalBinding::DeferredData, uniform_block_type::DeferredGlobals},
{GlobalBinding::ShadowCascadeParams, uniform_block_type::ShadowCascadeParams},
};
static constexpr VulkanDescriptorManager::UniformBindingEntry s_materialUBOs[] = {
{MaterialBinding::ModelData, uniform_block_type::ModelData},
{MaterialBinding::DecalGlobals, uniform_block_type::DecalGlobals},
};
// Note: MaterialBinding::ShadowMapData is intentionally NOT in s_materialUBOs --
// unlike ModelData/DecalGlobals, shadow_render_list passes its UBO handle/offset/size
// directly into gr_render_shadow_draw() rather than through gr_bind_uniform_buffer(),
// so VulkanDrawManager::renderShadowDraw() binds it explicitly instead of relying on
// the generic pending-uniform-binding pass in applyMaterial().
static constexpr VulkanDescriptorManager::UniformBindingEntry s_perDrawUBOs[] = {
{PerDrawBinding::GenericData, uniform_block_type::GenericData},
{PerDrawBinding::Matrices, uniform_block_type::Matrices},
{PerDrawBinding::NanoVGData, uniform_block_type::NanoVGData},
{PerDrawBinding::DecalInfo, uniform_block_type::DecalInfo},
{PerDrawBinding::MovieData, uniform_block_type::MovieData},
};
// ========== DescriptorFallbacks ==========
const vk::DescriptorImageInfo& DescriptorFallbacks::getImage(vk::ImageViewType t) const
{
switch (t) {
case vk::ImageViewType::e2D: return texture2D;
case vk::ImageViewType::e2DArray: return texture2DArray;
case vk::ImageViewType::eCube: return textureCube;
case vk::ImageViewType::e3D: return texture3D;
default:
Assertion(false, "DescriptorFallbacks::getImage: unhandled ImageViewType %d", static_cast<int>(t));
return texture2D;
}
}
// ========== DescriptorWriter template-based methods ==========
void DescriptorWriter::writeSet(DescriptorSetIndex setIndex, vk::DescriptorSet set)
{
Assert(m_fallbacks);
const DescriptorSetTemplate& tmpl = VulkanDescriptorManager::getSetTemplate(setIndex);
// Clear binding slots for this set
m_bindingSlots = {};
m_currentSet = setIndex;
// Dynamic offsets are consumed in binding order, so assign slots as the template is walked
// (templates list bindings in ascending binding number). This must stay the same rule
// dynamic_offset_slot() applies — hence advancing by b.count, not by one binding.
uint32_t nextDynIndex = 0;
m_dynOffsets[static_cast<size_t>(setIndex)] = {};
for (const auto& b : tmpl) {
Assert(m_writeCount < MAX_WRITES);
Assert(b.binding < MAX_BINDINGS_PER_SET);
auto& w = m_writes[m_writeCount++];
w = vk::WriteDescriptorSet();
w.dstSet = set;
w.dstBinding = b.binding;
w.descriptorCount = b.count;
w.descriptorType = b.type;
auto& slot = m_bindingSlots[b.binding];
slot.count = b.count;
slot.viewType = b.viewType;
bool isImage = (b.type == vk::DescriptorType::eCombinedImageSampler);
bool isAccelStruct = (b.type == vk::DescriptorType::eAccelerationStructureKHR);
if (b.type == vk::DescriptorType::eUniformBufferDynamic) {
Assert(nextDynIndex + b.count <= MAX_DYNAMIC_OFFSETS_PER_SET);
slot.dynIndex = static_cast<int>(nextDynIndex);
nextDynIndex += b.count;
}
if (isImage) {
Assert(m_imageInfoCount + b.count <= MAX_IMAGE_INFOS);
auto* dst = &m_imageInfos[m_imageInfoCount];
const auto& fallbackImg = m_fallbacks->getImage(b.viewType);
for (uint32_t j = 0; j < b.count; ++j) {
dst[j] = fallbackImg;
}
w.pImageInfo = dst;
slot.imageInfo = dst;
m_imageInfoCount += b.count;
} else if (isAccelStruct) {
// No setAccelStruct() override step needed: m_fallbacks->shadowTlas IS
// the live value, kept fresh once per frame by setCurrentShadowTlas().
Assert(b.count == 1);
Assert(m_accelStructInfoCount < MAX_ACCEL_STRUCT_INFOS);
auto* dst = &m_accelStructInfos[m_accelStructInfoCount];
*dst = m_fallbacks->shadowTlas;
auto& asInfo = m_asWriteInfos[m_accelStructInfoCount];
asInfo = vk::WriteDescriptorSetAccelerationStructureKHR();
asInfo.accelerationStructureCount = 1;
asInfo.pAccelerationStructures = dst;
w.pNext = &asInfo;
m_accelStructInfoCount++;
} else {
Assert(m_bufferInfoCount < MAX_BUFFER_INFOS);
m_bufferInfos[m_bufferInfoCount] = m_fallbacks->buffer;
// The fallback buffer info starts at offset 0, which is also the base a
// dynamic descriptor needs (its offset lives in the dynamic-offset array,
// left at 0 here until setBuffer supplies a real one).
w.pBufferInfo = &m_bufferInfos[m_bufferInfoCount];
slot.bufferInfo = &m_bufferInfos[m_bufferInfoCount++];
}
}
}
void DescriptorWriter::bindSets(vk::CommandBuffer cmd,
vk::PipelineLayout layout,
DescriptorSetIndex firstSet,
ArrayView<vk::DescriptorSet> sets)
{
size_t out = 0;
for (size_t i = 0; i < sets.size; ++i) {
const auto set = static_cast<DescriptorSetIndex>(static_cast<uint32_t>(firstSet) + i);
Assert(static_cast<uint32_t>(set) < static_cast<uint32_t>(DescriptorSetIndex::Count));
const uint32_t dynCount = VulkanDescriptorManager::getDynamicOffsetCount(set);
Assert(dynCount <= MAX_DYNAMIC_OFFSETS_PER_SET);
for (uint32_t j = 0; j < dynCount; ++j) {
Assert(out < m_dynOffsetScratch.size());
m_dynOffsetScratch[out++] = m_dynOffsets[static_cast<size_t>(set)][j];
}
}
cmd.bindDescriptorSets(vk::PipelineBindPoint::eGraphics,
layout,
static_cast<uint32_t>(firstSet),
vk::ArrayProxy<const vk::DescriptorSet>(static_cast<uint32_t>(sets.size), sets.data),
vk::ArrayProxy<const uint32_t>(static_cast<uint32_t>(out), m_dynOffsetScratch.data()));
}
void DescriptorWriter::flush()
{
if (m_writeCount > 0) {
m_device.updateDescriptorSets(m_writeCount, m_writes.data(), 0, nullptr);
getDescriptorManager()->noteDescriptorWrites(m_writeCount);
}
m_writeCount = 0;
m_bufferInfoCount = 0;
m_imageInfoCount = 0;
m_accelStructInfoCount = 0;
}
void DescriptorWriter::setBuffer(uint32_t binding, const vk::DescriptorBufferInfo& info)
{
Assert(binding < MAX_BINDINGS_PER_SET);
auto& slot = m_bindingSlots[binding];
Assert(slot.bufferInfo);
if (slot.dynIndex >= 0) {
// Dynamic binding: the descriptor holds only {buffer, 0, range}; the caller's
// offset becomes the dynamic offset applied at bind time. Keeping it out of
// the descriptor is what lets the set survive an offset-only change.
auto& dyn = m_dynOffsets[static_cast<size_t>(m_currentSet)][static_cast<size_t>(slot.dynIndex)];
if (info.buffer) {
Assertion(info.offset <= static_cast<vk::DeviceSize>(UINT32_MAX),
"Dynamic uniform buffer offset " SIZE_T_ARG " does not fit in a uint32 dynamic offset!",
static_cast<size_t>(info.offset));
*slot.bufferInfo = vk::DescriptorBufferInfo(info.buffer, 0, info.range);
dyn = static_cast<uint32_t>(info.offset);
} else {
*slot.bufferInfo = m_fallbacks->buffer;
dyn = 0;
}
return;
}
if (info.buffer) {
*slot.bufferInfo = info;
} else {
*slot.bufferInfo = m_fallbacks->buffer;
}
}
void DescriptorWriter::setImage(uint32_t binding, const vk::DescriptorImageInfo& info)
{
Assert(binding < MAX_BINDINGS_PER_SET);
auto& slot = m_bindingSlots[binding];
Assert(slot.imageInfo);
if (info.imageView) {
*slot.imageInfo = info;
} else {
*slot.imageInfo = m_fallbacks->getImage(slot.viewType);
}
}
void DescriptorWriter::setImageArray(uint32_t binding, ArrayView<vk::DescriptorImageInfo> infos)
{
Assert(binding < MAX_BINDINGS_PER_SET);
auto& slot = m_bindingSlots[binding];
Assert(slot.imageInfo);
Assert(infos.size <= slot.count);
memcpy(slot.imageInfo, infos.data, infos.size * sizeof(vk::DescriptorImageInfo));
}
// ========== Global descriptor manager ==========
static VulkanDescriptorManager* g_descriptorManager = nullptr;
VulkanDescriptorManager* getDescriptorManager()
{
Assertion(g_descriptorManager != nullptr, "Vulkan DescriptorManager not initialized!");
return g_descriptorManager;
}
void setDescriptorManager(VulkanDescriptorManager* manager)
{
g_descriptorManager = manager;
}
bool VulkanDescriptorManager::init(vk::Device device, bool raytracingSupported)
{
if (m_initialized) {
return true;
}
m_device = device;
m_raytracingEnabled = raytracingSupported;
createSetLayouts();
createDescriptorPools();
m_initialized = true;
nprintf(("vulkan", "VulkanDescriptorManager: Initialized (raytraced shadows %s)\n",
m_raytracingEnabled ? "enabled" : "disabled"));
return true;
}
void VulkanDescriptorManager::shutdown()
{
if (!m_initialized) {
return;
}
// Wait for device idle before destroying
m_device.waitIdle();
// Destroy pools (automatically frees allocated sets)
for (auto& poolChain : m_framePools) {
poolChain.clear();
}
// Destroy layouts
for (auto& layout : m_setLayouts) {
layout.reset();
}
m_initialized = false;
nprintf(("vulkan", "VulkanDescriptorManager: Shutdown complete\n"));
}
void VulkanDescriptorManager::buildFallbacks(VulkanBufferManager* bufMgr, VulkanTextureManager* texMgr,
VulkanRaytracingManager* rtMgr)
{
m_fallbacks.buffer = bufMgr->getFallbackUniformBufferInfo();
m_fallbacks.texture2D = texMgr->getFallbackTextureInfo2D();
m_fallbacks.texture2DArray = texMgr->getFallbackTextureInfo2DArray();
m_fallbacks.textureCube = texMgr->getFallbackTextureInfoCube();
m_fallbacks.texture3D = texMgr->getFallbackTextureInfo3D();
if (m_raytracingEnabled && rtMgr != nullptr) {
// Seed with the permanent empty TLAS; setCurrentShadowTlas() takes over
// once the first real TLAS is built.
m_fallbacks.shadowTlas = rtMgr->getFallbackTlas();
}
nprintf(("vulkan", "VulkanDescriptorManager: Fallbacks built\n"));
}
const DescriptorSetTemplate& VulkanDescriptorManager::getSetTemplate(DescriptorSetIndex setIndex)
{
switch (setIndex) {
case DescriptorSetIndex::Global: return getDescriptorManager()->m_globalTemplateRuntime;
case DescriptorSetIndex::Material: return s_materialTemplate;
case DescriptorSetIndex::PerDraw: return s_perDrawTemplate;
default:
Assertion(false, "Invalid DescriptorSetIndex!");
return getDescriptorManager()->m_globalTemplateRuntime;
}
}
uint32_t VulkanDescriptorManager::getDynamicOffsetCount(DescriptorSetIndex setIndex)
{
// Constant per set (see the derived *_DYNAMIC_OFFSET_COUNT values), not a scan of the
// template — this runs on every descriptor set bind.
switch (setIndex) {
case DescriptorSetIndex::Global:
return GLOBAL_DYNAMIC_OFFSET_COUNT;
case DescriptorSetIndex::Material:
return MATERIAL_DYNAMIC_OFFSET_COUNT;
case DescriptorSetIndex::PerDraw:
return PERDRAW_DYNAMIC_OFFSET_COUNT;
default:
UNREACHABLE("Invalid descriptor set index %u!", static_cast<uint32_t>(setIndex));
return 0;
}
}
vk::DescriptorSetLayout VulkanDescriptorManager::getSetLayout(DescriptorSetIndex setIndex) const
{
return m_setLayouts[static_cast<size_t>(setIndex)].get();
}
vk::DescriptorSet VulkanDescriptorManager::allocateFrameSet(DescriptorSetIndex setIndex)
{
if (!m_initialized) {
return {};
}
vk::DescriptorSetLayout layout = m_setLayouts[static_cast<size_t>(setIndex)].get();
auto& pools = m_framePools[m_currentFrame];
++m_setsAllocatedThisFrame;
// Allocate through the non-throwing, non-allocating vulkan.hpp overload: this is
// one of the hottest calls in the backend (thousands per frame), and the
// vector-returning overload heap-allocates on every single one.
vk::DescriptorSetAllocateInfo allocInfo;
allocInfo.descriptorSetCount = 1;
allocInfo.pSetLayouts = &layout;
// Try allocating from the last pool in the list
if (!pools.empty()) {
allocInfo.descriptorPool = pools.back().get();
vk::DescriptorSet set;
vk::Result result = m_device.allocateDescriptorSets(&allocInfo, &set);
if (result == vk::Result::eSuccess) {
return set;
}
if (result == vk::Result::eErrorOutOfPoolMemory) {
nprintf(("vulkan", "VulkanDescriptorManager: frame pool exhausted, allocating an additional pool chunk\n"));
} else if (result == vk::Result::eErrorFragmentedPool) {
nprintf(("vulkan", "VulkanDescriptorManager: frame pool fragmented, allocating an additional pool chunk\n"));
} else {
nprintf(("vulkan",
"VulkanDescriptorManager: descriptor set allocation failed (%s)\n",
vk::to_string(result).c_str()));
return {};
}
}
// Create a new pool and retry
pools.push_back(createFramePool());
nprintf(("vulkandescriptor", "VulkanDescriptorManager: Grew frame %u pool count to %zu\n",
m_currentFrame, pools.size()));
allocInfo.descriptorPool = pools.back().get();
vk::DescriptorSet set;
vk::Result result = m_device.allocateDescriptorSets(&allocInfo, &set);
if (result != vk::Result::eSuccess) {
nprintf(("vulkan",
"VulkanDescriptorManager: Failed to allocate frame descriptor set after pool growth: %s\n",
vk::to_string(result).c_str()));
return {};
}
return set;
}
void VulkanDescriptorManager::beginFrame()
{
if (!m_initialized) {
return;
}
auto& pools = m_framePools[m_currentFrame];
// Reset per-frame diagnostic counters
m_setsAllocatedThisFrame = 0;
m_writesThisFrame = 0;
// Reset all pools for the current frame
for (auto& pool : pools) {
m_device.resetDescriptorPool(pool.get());
}
// If we grew beyond the initial pool, shrink back to 1 to reclaim memory
// (the single pool will grow again next frame if needed)
if (pools.size() > 1) {
vk::UniqueDescriptorPool first = std::move(pools[0]);
pools.clear();
pools.push_back(std::move(first));
}
}
void VulkanDescriptorManager::setCurrentFrame(uint32_t frameIndex)
{
// The renderer is the single source of the frame-in-flight index and advances
// it by exactly one (mod MAX_FRAMES_IN_FLIGHT) each flip(); assert that
// invariant so a drift between the renderer and this manager is caught loudly
// instead of silently corrupting descriptor pools / per-draw sets.
Assertion(frameIndex == (m_currentFrame + 1) % MAX_FRAMES_IN_FLIGHT,
"VulkanDescriptorManager::setCurrentFrame: non-monotonic frame index (%u -> %u)",
m_currentFrame, frameIndex);
m_currentFrame = frameIndex;
}
ArrayView<VulkanDescriptorManager::UniformBindingEntry>
VulkanDescriptorManager::getUniformBindings(DescriptorSetIndex setIndex)
{
switch (setIndex) {
case DescriptorSetIndex::Global: return {s_globalUBOs, std::size(s_globalUBOs)};
case DescriptorSetIndex::Material: return {s_materialUBOs, std::size(s_materialUBOs)};
case DescriptorSetIndex::PerDraw: return {s_perDrawUBOs, std::size(s_perDrawUBOs)};
default: return {nullptr, 0};
}
}
void VulkanDescriptorManager::createSetLayouts()
{
m_globalBindingsRuntime.assign(std::begin(s_globalBindings), std::end(s_globalBindings));
if (m_raytracingEnabled) {
m_globalBindingsRuntime.push_back(s_globalTlasBinding);
}
m_globalTemplateRuntime = DescriptorSetTemplate(m_globalBindingsRuntime.data(), m_globalBindingsRuntime.size());
m_setLayouts[static_cast<size_t>(DescriptorSetIndex::Global)] = createSetLayout(m_globalTemplateRuntime);
m_setLayouts[static_cast<size_t>(DescriptorSetIndex::Material)] = createSetLayout(s_materialTemplate);
m_setLayouts[static_cast<size_t>(DescriptorSetIndex::PerDraw)] = createSetLayout(s_perDrawTemplate);
nprintf(("vulkan", "VulkanDescriptorManager: Created %zu descriptor set layouts\n",
static_cast<size_t>(DescriptorSetIndex::Count)));
}
vk::UniqueDescriptorPool VulkanDescriptorManager::createFramePool()
{
// Pool sizing constants (with rationale) live next to the other descriptor
// constants in VulkanDescriptorManager.h. Additional pool chunks are created
// automatically when one is exhausted.
SCP_vector<vk::DescriptorPoolSize> poolSizes = {
{vk::DescriptorType::eUniformBuffer, MAX_UNIFORM_BUFFERS_PER_POOL},
{vk::DescriptorType::eUniformBufferDynamic, MAX_DYNAMIC_UNIFORM_BUFFERS_PER_POOL},
{vk::DescriptorType::eCombinedImageSampler, MAX_SAMPLERS_PER_POOL},
{vk::DescriptorType::eStorageBuffer, MAX_SETS_PER_POOL},
};
if (m_raytracingEnabled) {
poolSizes.emplace_back(vk::DescriptorType::eAccelerationStructureKHR, MAX_SETS_PER_POOL);
}
vk::DescriptorPoolCreateInfo poolInfo;
poolInfo.maxSets = MAX_SETS_PER_POOL;
poolInfo.poolSizeCount = static_cast<uint32_t>(poolSizes.size());
poolInfo.pPoolSizes = poolSizes.data();
return m_device.createDescriptorPoolUnique(poolInfo);
}
void VulkanDescriptorManager::createDescriptorPools()
{
// Create one initial pool per frame (more will be added on demand)
for (uint32_t i = 0; i < MAX_FRAMES_IN_FLIGHT; ++i) {
m_framePools[i].push_back(createFramePool());
}
nprintf(("vulkan", "VulkanDescriptorManager: Created %u frame pool chains\n",
MAX_FRAMES_IN_FLIGHT));
}
vk::UniqueDescriptorSetLayout VulkanDescriptorManager::createSetLayout(
const DescriptorSetTemplate& tmpl)
{
SCP_vector<vk::DescriptorSetLayoutBinding> vkBindings;
vkBindings.reserve(tmpl.size);
for (const auto& b : tmpl) {
vk::DescriptorSetLayoutBinding binding;
binding.binding = b.binding;
binding.descriptorType = b.type;
binding.descriptorCount = b.count;
binding.stageFlags = b.stages;
binding.pImmutableSamplers = nullptr;
vkBindings.push_back(binding);
}
vk::DescriptorSetLayoutCreateInfo layoutInfo;
layoutInfo.bindingCount = static_cast<uint32_t>(vkBindings.size());
layoutInfo.pBindings = vkBindings.data();
return m_device.createDescriptorSetLayoutUnique(layoutInfo);
}
} // namespace graphics::vulkan