2026-07-22 18:09:06 +02:00
|
|
|
//SPDX-License-Identifier: LGPL-3.0-only
|
|
|
|
|
//SPDX-FileCopyrightText: Copyright (C) 2026 Catcrafts®
|
fix(device): fence-keyed deferred resource-deletion queue (#101)
Since #40 dropped the per-frame vkQueueWaitIdle, frames are pipelined:
a resource the CPU is done with may still be read by the GPU for up to
numFrames-1 more frames. VulkanBuffer::Resize's destroy-and-recreate
path was therefore a live use-after-free (#63), no longer masked by the
wait-idle.
Device gains a monotonic, frame-counter-keyed deletion queue:
EnqueueDeletion tags {buffer, memory} with the current frameCounter;
ReclaimDeletions (called per frame after the fence wait) frees entries
once framesInFlight frames have elapsed; DrainDeletions frees everything
after a wait-idle. VulkanBuffer::DeferredClear hands handles to the queue
and nulls the handle, and Resize uses it instead of immediate Clear().
Window::Render sets framesInFlight at init, reclaims after the per-image
fence wait, bumps Device::frameCounter once per frame, and drains on the
resize / OUT_OF_DATE wait-idle paths. The destructor keeps immediate
Clear() (callers destroying mid-flight remain responsible, unchanged).
Adds the DeferredDeletion test: drives the retire timing on a real
headless device with real buffers, stepping Device::frameCounter to pin
the exact reclaim frame, asserting validation stays silent.
Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
2026-06-17 13:21:12 +00:00
|
|
|
|
|
|
|
|
// Issue #101: fence-keyed deferred resource-deletion queue. Since #40 dropped
|
|
|
|
|
// the per-frame vkQueueWaitIdle, a buffer the CPU is done with may still be
|
|
|
|
|
// read by the GPU for up to framesInFlight-1 more frames — so destroying it
|
|
|
|
|
// immediately (VulkanBuffer::Clear / the old Resize path) is a use-after-free.
|
feat: windows on their own clock, deferred deletion for every handle owner, staged re-uploads, and RTPass push data
Window
- updateInterval runs Update on the window's own clock while the
compositor sends no frame callbacks (hidden, minimized): the Wayland loop
is a poll() loop, and the Win32 loop sleeps until the next update. Both
read the interval each pass.
- SetPresentMode (FIFO, Mailbox, Immediate). A mode the surface lacks, or
whose swapchain has other than numFrames images (Mesa's Mailbox has four),
falls back to FIFO until the mode is set again.
- AddPollFd / RemovePollFd wake the Wayland loop on the app's descriptors.
- The swapchain has TRANSFER_DST where the surface allows it.
- CaptureNextFrame replaces SaveFrame: it records the copy in the frame,
before the present, where SaveFrame read an image already presented. A
frame unwound past before the write still frees the readback buffer.
- Inter-pass barriers on the swapchain image carry the access masks of the
stages the passes use.
Deferred deletion (issue #101)
- VulkanBuffer, Mesh, VulkanShader, ComputeShader and PipelineRTVulkan hand
their handles to the deletion queue from their destructors and moves, as
a frame in flight may still use them; a static owner goes after the
driver has torn down. VulkanShader and PipelineRTVulkan move and can't be
copied; re-Load and re-Init queue the old pipeline.
- The queue and its mutex are never destroyed, and the queue takes handles
from any thread; Device::frameCounter is atomic. Both the queue and the
staging rings count frames as one window renders them (documented).
- DrainDeletions also lets every buffer's staging ring start over: without
a Window nothing else reclaims either, so an app without one calls it
after waiting for its work.
Meshes and buffers
- A Mesh rebuild queues the old acceleration structure and builds into the
same storage after a barrier, so the BLAS address stays put when it fits.
The refit and rebuild barriers cover compute ray queries too.
- RTBuildOptions::noDuplicateAnyHit. Mesh::geometryFlags replaces opaque,
and the refit and rebuild fallbacks keep the flags.
- A re-upload into a buffer a frame in flight may read (UploadDeviceLocal
of the same size, UploadDeviceLocalRange) copies on the GPU, after those
reads, through a per-frame staging ring; on ReBAR it used to write the
buffer in place. Empty uploads record nothing.
- A compressed Mesh::Build that keeps its buffers decompresses into them
after the reads of frames in flight, and the hit shaders and ray queries
wait for the decompress as well as the BLAS build. Its vertex buffer
takes a ranged refit's copies.
- Mesh move-assigns (queueing the BLAS it replaces), so it can be replaced
in place or erased from a vector.
- VulkanBuffer::Create asks for a device address only for buffers with
SHADER_DEVICE_ADDRESS usage, as vkGetBufferDeviceAddress requires; the
others have address 0. UIRenderer::RegisterBuffer refuses those.
- Build inputs are synchronized as shader reads.
Other
- RTPass pushes the app's own push data and traces at an explicit size.
- DescriptorHeapVulkan hands freed slot ranges out again (they leaked), and
keeps its free lists sorted.
- ImageVulkan's mip chain clamps each level to 1 texel: a non-square
image's shorter side went to 0.
- VulkanShader throws on a missing file instead of failing in bad_alloc.
- CRAFTER_GRAPHICS_DEVICE picks the GPU by its number among the distinct
GPUs Vulkan lists; anything but such a number is an error.
- The debug messenger prints info messages only with
CRAFTER_GRAPHICS_VALIDATION set.
- Windows: the WinRT gamepad code moved to a C file (mingw-w64's WinRT
headers don't compile as C++), -lcombase dropped, a reference leak fixed.
- project.cpp honours --local for Crafter.Event, Math and Asset, as Asset
does for Math, and builds every test through one helper, projectArgs
included.
- Tests: DebugMessengerSeverity, DeletionQueueThreads, DeviceSelection,
HeapSlotReuse, MeshLifetime, MipChainNonSquare, PipelineLifetime,
RTPassPushData, SwapchainTransferDst, UploadInFlight, VulkanBufferAddress,
VulkanShaderLifetime, WindowUpdateClock; others extended.
Breaking: SaveFrame is CaptureNextFrame; Mesh::opaque is geometryFlags; a
buffer without SHADER_DEVICE_ADDRESS usage has no address; frameCounter is
std::atomic.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-20 19:07:54 +02:00
|
|
|
// VulkanBuffer::DeferredClear() / Resize() and the VulkanBuffer destructor now
|
|
|
|
|
// hand the handles to Device's
|
fix(device): fence-keyed deferred resource-deletion queue (#101)
Since #40 dropped the per-frame vkQueueWaitIdle, frames are pipelined:
a resource the CPU is done with may still be read by the GPU for up to
numFrames-1 more frames. VulkanBuffer::Resize's destroy-and-recreate
path was therefore a live use-after-free (#63), no longer masked by the
wait-idle.
Device gains a monotonic, frame-counter-keyed deletion queue:
EnqueueDeletion tags {buffer, memory} with the current frameCounter;
ReclaimDeletions (called per frame after the fence wait) frees entries
once framesInFlight frames have elapsed; DrainDeletions frees everything
after a wait-idle. VulkanBuffer::DeferredClear hands handles to the queue
and nulls the handle, and Resize uses it instead of immediate Clear().
Window::Render sets framesInFlight at init, reclaims after the per-image
fence wait, bumps Device::frameCounter once per frame, and drains on the
resize / OUT_OF_DATE wait-idle paths. The destructor keeps immediate
Clear() (callers destroying mid-flight remain responsible, unchanged).
Adds the DeferredDeletion test: drives the retire timing on a real
headless device with real buffers, stepping Device::frameCounter to pin
the exact reclaim frame, asserting validation stays silent.
Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
2026-06-17 13:21:12 +00:00
|
|
|
// deletion queue, tagged with the current frameCounter; ReclaimDeletions frees
|
|
|
|
|
// an entry only once framesInFlight frames have elapsed, and DrainDeletions
|
|
|
|
|
// (called after a wait-idle) frees everything unconditionally.
|
|
|
|
|
//
|
|
|
|
|
// The retire-timing is the load-bearing logic: free too early and it's the
|
|
|
|
|
// very UAF the queue exists to prevent; free too late and resources leak. This
|
|
|
|
|
// test drives it on a real headless Vulkan device (real VkBuffer/VkDeviceMemory
|
|
|
|
|
// so the frees actually execute and the validation layer can object), manually
|
|
|
|
|
// stepping Device::frameCounter to pin down the exact frame an entry is
|
|
|
|
|
// reclaimed on. It needs no swapchain/window — the queue itself is window-free.
|
|
|
|
|
|
|
|
|
|
#include "vulkan/vulkan.h"
|
|
|
|
|
#include <cstdlib>
|
|
|
|
|
|
|
|
|
|
import Crafter.Graphics;
|
|
|
|
|
import std;
|
|
|
|
|
|
|
|
|
|
using namespace Crafter;
|
|
|
|
|
|
|
|
|
|
namespace {
|
|
|
|
|
|
|
|
|
|
int failures = 0;
|
|
|
|
|
|
|
|
|
|
void Check(bool ok, std::string_view what) {
|
|
|
|
|
std::println("{} {}", ok ? "PASS" : "FAIL", what);
|
|
|
|
|
if (!ok) ++failures;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
constexpr VkBufferUsageFlags2 USAGE =
|
|
|
|
|
VK_BUFFER_USAGE_2_TRANSFER_SRC_BIT | VK_BUFFER_USAGE_2_SHADER_DEVICE_ADDRESS_BIT;
|
|
|
|
|
constexpr auto DEVICE_LOCAL = VK_MEMORY_PROPERTY_DEVICE_LOCAL_BIT;
|
|
|
|
|
|
|
|
|
|
} // namespace
|
|
|
|
|
|
|
|
|
|
int main() {
|
|
|
|
|
Device::Initialize();
|
|
|
|
|
|
|
|
|
|
// No Window here, so set the in-flight depth ourselves (Window::Render
|
|
|
|
|
// normally does this at init). Use the real window depth so the timing
|
|
|
|
|
// matches production.
|
|
|
|
|
Device::framesInFlight = Window::numFrames;
|
|
|
|
|
Device::frameCounter = 0;
|
|
|
|
|
Device::deletionQueue.clear();
|
|
|
|
|
|
|
|
|
|
// ── DeferredClear enqueues, nulls the handle, and retires after exactly
|
|
|
|
|
// framesInFlight frames ──────────────────────────────────────────────
|
|
|
|
|
{
|
|
|
|
|
VulkanBuffer<float, false> buf;
|
|
|
|
|
buf.Create(USAGE, DEVICE_LOCAL, 16);
|
|
|
|
|
Check(buf.buffer != VK_NULL_HANDLE, "Create produced a real buffer handle");
|
|
|
|
|
|
|
|
|
|
buf.DeferredClear();
|
|
|
|
|
Check(buf.buffer == VK_NULL_HANDLE,
|
|
|
|
|
"DeferredClear nulls the handle so the buffer no longer owns it");
|
|
|
|
|
Check(Device::deletionQueue.size() == 1,
|
|
|
|
|
"DeferredClear enqueues exactly one pending deletion");
|
|
|
|
|
|
|
|
|
|
// Walk right up to the retire frame: still queued at every frame before
|
|
|
|
|
// frameCounter reaches retireAfter + framesInFlight.
|
|
|
|
|
for (std::uint64_t f = 0; f < Window::numFrames; ++f) {
|
|
|
|
|
Device::frameCounter = f;
|
|
|
|
|
Device::ReclaimDeletions();
|
|
|
|
|
Check(Device::deletionQueue.size() == 1,
|
|
|
|
|
std::format("entry survives ReclaimDeletions at frame {} "
|
|
|
|
|
"(< retire frame {})", f, Window::numFrames));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// At retireAfter(0) + framesInFlight it is finally freed.
|
|
|
|
|
Device::frameCounter = Window::numFrames;
|
|
|
|
|
Device::ReclaimDeletions();
|
|
|
|
|
Check(Device::deletionQueue.empty(),
|
|
|
|
|
std::format("entry is reclaimed once frameCounter reaches {}",
|
|
|
|
|
Window::numFrames));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// ── Resize's reallocate path defers the OLD allocation, not the new one ──
|
|
|
|
|
{
|
|
|
|
|
Device::frameCounter = 100;
|
|
|
|
|
Device::deletionQueue.clear();
|
|
|
|
|
|
|
|
|
|
VulkanBuffer<float, false> buf;
|
|
|
|
|
buf.Create(USAGE, DEVICE_LOCAL, 4);
|
|
|
|
|
VkBuffer original = buf.buffer;
|
|
|
|
|
|
|
|
|
|
// Grow past capacity → forces destroy+recreate, which must DEFER the old
|
|
|
|
|
// buffer (the #63 UAF this whole queue exists to close).
|
|
|
|
|
buf.Resize(USAGE, DEVICE_LOCAL, 64);
|
|
|
|
|
Check(buf.buffer != VK_NULL_HANDLE && buf.buffer != original,
|
|
|
|
|
"Resize past capacity allocates a fresh buffer");
|
|
|
|
|
Check(Device::deletionQueue.size() == 1
|
|
|
|
|
&& Device::deletionQueue.front().buffer == original,
|
|
|
|
|
"Resize defers the OLD allocation to the deletion queue");
|
|
|
|
|
Check(Device::deletionQueue.front().retireAfter == 100,
|
|
|
|
|
"deferred entry is tagged with the frameCounter at enqueue time");
|
|
|
|
|
|
|
|
|
|
// Not yet due (tagged at 100, retires at 100 + framesInFlight).
|
|
|
|
|
Device::frameCounter = 100 + Window::numFrames - 1;
|
|
|
|
|
Device::ReclaimDeletions();
|
|
|
|
|
Check(Device::deletionQueue.size() == 1,
|
|
|
|
|
"old allocation is not freed before its retire frame");
|
|
|
|
|
|
|
|
|
|
Device::frameCounter = 100 + Window::numFrames;
|
|
|
|
|
Device::ReclaimDeletions();
|
|
|
|
|
Check(Device::deletionQueue.empty(),
|
|
|
|
|
"old allocation is freed on its retire frame");
|
|
|
|
|
|
|
|
|
|
buf.Clear(); // immediate cleanup of the live buffer (no GPU work pending)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// ── DrainDeletions frees everything regardless of retire frame ───────────
|
|
|
|
|
{
|
|
|
|
|
Device::frameCounter = 0;
|
|
|
|
|
Device::deletionQueue.clear();
|
|
|
|
|
|
|
|
|
|
VulkanBuffer<float, false> a;
|
|
|
|
|
VulkanBuffer<float, false> b;
|
|
|
|
|
a.Create(USAGE, DEVICE_LOCAL, 8);
|
|
|
|
|
b.Create(USAGE, DEVICE_LOCAL, 8);
|
|
|
|
|
a.DeferredClear();
|
|
|
|
|
b.DeferredClear();
|
|
|
|
|
Check(Device::deletionQueue.size() == 2, "two entries queued");
|
|
|
|
|
|
|
|
|
|
// Far from any retire frame — ReclaimDeletions would free nothing here.
|
|
|
|
|
Device::DrainDeletions();
|
|
|
|
|
Check(Device::deletionQueue.empty(),
|
|
|
|
|
"DrainDeletions frees every queued entry unconditionally");
|
|
|
|
|
}
|
|
|
|
|
|
feat: windows on their own clock, deferred deletion for every handle owner, staged re-uploads, and RTPass push data
Window
- updateInterval runs Update on the window's own clock while the
compositor sends no frame callbacks (hidden, minimized): the Wayland loop
is a poll() loop, and the Win32 loop sleeps until the next update. Both
read the interval each pass.
- SetPresentMode (FIFO, Mailbox, Immediate). A mode the surface lacks, or
whose swapchain has other than numFrames images (Mesa's Mailbox has four),
falls back to FIFO until the mode is set again.
- AddPollFd / RemovePollFd wake the Wayland loop on the app's descriptors.
- The swapchain has TRANSFER_DST where the surface allows it.
- CaptureNextFrame replaces SaveFrame: it records the copy in the frame,
before the present, where SaveFrame read an image already presented. A
frame unwound past before the write still frees the readback buffer.
- Inter-pass barriers on the swapchain image carry the access masks of the
stages the passes use.
Deferred deletion (issue #101)
- VulkanBuffer, Mesh, VulkanShader, ComputeShader and PipelineRTVulkan hand
their handles to the deletion queue from their destructors and moves, as
a frame in flight may still use them; a static owner goes after the
driver has torn down. VulkanShader and PipelineRTVulkan move and can't be
copied; re-Load and re-Init queue the old pipeline.
- The queue and its mutex are never destroyed, and the queue takes handles
from any thread; Device::frameCounter is atomic. Both the queue and the
staging rings count frames as one window renders them (documented).
- DrainDeletions also lets every buffer's staging ring start over: without
a Window nothing else reclaims either, so an app without one calls it
after waiting for its work.
Meshes and buffers
- A Mesh rebuild queues the old acceleration structure and builds into the
same storage after a barrier, so the BLAS address stays put when it fits.
The refit and rebuild barriers cover compute ray queries too.
- RTBuildOptions::noDuplicateAnyHit. Mesh::geometryFlags replaces opaque,
and the refit and rebuild fallbacks keep the flags.
- A re-upload into a buffer a frame in flight may read (UploadDeviceLocal
of the same size, UploadDeviceLocalRange) copies on the GPU, after those
reads, through a per-frame staging ring; on ReBAR it used to write the
buffer in place. Empty uploads record nothing.
- A compressed Mesh::Build that keeps its buffers decompresses into them
after the reads of frames in flight, and the hit shaders and ray queries
wait for the decompress as well as the BLAS build. Its vertex buffer
takes a ranged refit's copies.
- Mesh move-assigns (queueing the BLAS it replaces), so it can be replaced
in place or erased from a vector.
- VulkanBuffer::Create asks for a device address only for buffers with
SHADER_DEVICE_ADDRESS usage, as vkGetBufferDeviceAddress requires; the
others have address 0. UIRenderer::RegisterBuffer refuses those.
- Build inputs are synchronized as shader reads.
Other
- RTPass pushes the app's own push data and traces at an explicit size.
- DescriptorHeapVulkan hands freed slot ranges out again (they leaked), and
keeps its free lists sorted.
- ImageVulkan's mip chain clamps each level to 1 texel: a non-square
image's shorter side went to 0.
- VulkanShader throws on a missing file instead of failing in bad_alloc.
- CRAFTER_GRAPHICS_DEVICE picks the GPU by its number among the distinct
GPUs Vulkan lists; anything but such a number is an error.
- The debug messenger prints info messages only with
CRAFTER_GRAPHICS_VALIDATION set.
- Windows: the WinRT gamepad code moved to a C file (mingw-w64's WinRT
headers don't compile as C++), -lcombase dropped, a reference leak fixed.
- project.cpp honours --local for Crafter.Event, Math and Asset, as Asset
does for Math, and builds every test through one helper, projectArgs
included.
- Tests: DebugMessengerSeverity, DeletionQueueThreads, DeviceSelection,
HeapSlotReuse, MeshLifetime, MipChainNonSquare, PipelineLifetime,
RTPassPushData, SwapchainTransferDst, UploadInFlight, VulkanBufferAddress,
VulkanShaderLifetime, WindowUpdateClock; others extended.
Breaking: SaveFrame is CaptureNextFrame; Mesh::opaque is geometryFlags; a
buffer without SHADER_DEVICE_ADDRESS usage has no address; frameCounter is
std::atomic.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-20 19:07:54 +02:00
|
|
|
// ── The destructor defers too: an owner can be destroyed while a frame in
|
|
|
|
|
// flight still reads the buffer, so it can't free it at once ──────────
|
|
|
|
|
{
|
|
|
|
|
Device::frameCounter = 0;
|
|
|
|
|
Device::deletionQueue.clear();
|
|
|
|
|
|
|
|
|
|
VkBuffer plain = VK_NULL_HANDLE;
|
|
|
|
|
VkBuffer mapped = VK_NULL_HANDLE;
|
|
|
|
|
{
|
|
|
|
|
VulkanBuffer<float, false> a;
|
|
|
|
|
VulkanBuffer<float, true> b;
|
|
|
|
|
a.Create(USAGE, DEVICE_LOCAL, 8);
|
|
|
|
|
b.Create(USAGE, VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT, 8);
|
|
|
|
|
plain = a.buffer;
|
|
|
|
|
mapped = b.buffer;
|
|
|
|
|
}
|
|
|
|
|
const bool bothQueued = Device::deletionQueue.size() == 2
|
|
|
|
|
&& std::ranges::any_of(Device::deletionQueue, [&](const Device::PendingDeletion& e) { return e.buffer == plain; })
|
|
|
|
|
&& std::ranges::any_of(Device::deletionQueue, [&](const Device::PendingDeletion& e) { return e.buffer == mapped; });
|
|
|
|
|
Check(bothQueued, "destroying a VulkanBuffer that owns an allocation queues it, mapped or not");
|
|
|
|
|
|
|
|
|
|
Device::frameCounter = Window::numFrames - 1;
|
|
|
|
|
Device::ReclaimDeletions();
|
|
|
|
|
Check(Device::deletionQueue.size() == 2, "a destroyed buffer is not freed before its retire frame");
|
|
|
|
|
Device::frameCounter = Window::numFrames;
|
|
|
|
|
Device::ReclaimDeletions();
|
|
|
|
|
Check(Device::deletionQueue.empty(), "a destroyed buffer is freed on its retire frame");
|
|
|
|
|
}
|
|
|
|
|
|
fix(device): fence-keyed deferred resource-deletion queue (#101)
Since #40 dropped the per-frame vkQueueWaitIdle, frames are pipelined:
a resource the CPU is done with may still be read by the GPU for up to
numFrames-1 more frames. VulkanBuffer::Resize's destroy-and-recreate
path was therefore a live use-after-free (#63), no longer masked by the
wait-idle.
Device gains a monotonic, frame-counter-keyed deletion queue:
EnqueueDeletion tags {buffer, memory} with the current frameCounter;
ReclaimDeletions (called per frame after the fence wait) frees entries
once framesInFlight frames have elapsed; DrainDeletions frees everything
after a wait-idle. VulkanBuffer::DeferredClear hands handles to the queue
and nulls the handle, and Resize uses it instead of immediate Clear().
Window::Render sets framesInFlight at init, reclaims after the per-image
fence wait, bumps Device::frameCounter once per frame, and drains on the
resize / OUT_OF_DATE wait-idle paths. The destructor keeps immediate
Clear() (callers destroying mid-flight remain responsible, unchanged).
Adds the DeferredDeletion test: drives the retire timing on a real
headless device with real buffers, stepping Device::frameCounter to pin
the exact reclaim frame, asserting validation stays silent.
Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
2026-06-17 13:21:12 +00:00
|
|
|
// ── EnqueueDeletion ignores a null handle (nothing to free) ──────────────
|
|
|
|
|
{
|
|
|
|
|
Device::deletionQueue.clear();
|
|
|
|
|
Device::EnqueueDeletion(VK_NULL_HANDLE, VK_NULL_HANDLE);
|
|
|
|
|
Check(Device::deletionQueue.empty(),
|
|
|
|
|
"EnqueueDeletion drops a VK_NULL_HANDLE buffer");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
Check(Device::validationErrorCount == 0,
|
|
|
|
|
std::format("no Vulkan validation errors across the run ({} seen)",
|
|
|
|
|
Device::validationErrorCount));
|
|
|
|
|
|
|
|
|
|
if (failures != 0) {
|
|
|
|
|
std::println("{} check(s) failed", failures);
|
|
|
|
|
return EXIT_FAILURE;
|
|
|
|
|
}
|
|
|
|
|
std::println("all checks passed");
|
|
|
|
|
return EXIT_SUCCESS;
|
|
|
|
|
}
|