Crafter.Graphics/tests/UploadInFlight/main.cpp
Jorijn van der Graaf 079b6b0e1f
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 20:20:29 +02:00

213 lines
9.9 KiB
C++

//SPDX-License-Identifier: LGPL-3.0-only
//SPDX-FileCopyrightText: Copyright (C) 2026 Catcrafts®
// A re-upload into a buffer the GPU may still be reading lands on the GPU
// timeline, after the reads already submitted, instead of being written from
// the host at once. On ReBAR hardware UploadDeviceLocal (same size) and
// UploadDeviceLocalRange mapped the buffer and wrote it in place while a frame
// in flight could still read it: a mesh refitted every frame had the previous
// frame's BLAS update read this frame's vertices, or half of them.
//
// The test records a re-upload, then, before submitting it, submits and waits
// for a copy of the buffer: a frame in flight that runs after the upload was
// recorded. That copy must still see the old contents, and a copy recorded
// after the upload the new ones. Both upload calls are checked, and two
// ranged uploads to one buffer in one frame, which share that frame's staging.
//
// An empty re-upload records nothing (a copy or barrier of no bytes is
// invalid usage). And without a window no frame passes, so nothing ever
// reclaimed the staging: each re-upload added to it. DrainDeletions, after
// waiting for the queue, now lets the staging start over.
//
// Needs a Vulkan device and no window: the test steps Device::frameCounter
// itself. Run with CRAFTER_GRAPHICS_VALIDATION=1 and
// VK_KHRONOS_VALIDATION_VALIDATE_SYNC=1 to check the uploads' barriers too.
#include <cstdlib>
#include "vulkan/vulkan.h"
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 std::uint32_t kCount = 256;
constexpr VkBufferUsageFlags2 kUsage = VK_BUFFER_USAGE_STORAGE_BUFFER_BIT | VK_BUFFER_USAGE_SHADER_DEVICE_ADDRESS_BIT
| VK_BUFFER_USAGE_TRANSFER_SRC_BIT;
VkCommandBuffer BeginCmd() {
const VkCommandBufferAllocateInfo allocation {
.sType = VK_STRUCTURE_TYPE_COMMAND_BUFFER_ALLOCATE_INFO,
.commandPool = Device::commandPool,
.level = VK_COMMAND_BUFFER_LEVEL_PRIMARY,
.commandBufferCount = 1,
};
VkCommandBuffer cmd = VK_NULL_HANDLE;
Device::CheckVkResult(vkAllocateCommandBuffers(Device::device, &allocation, &cmd));
const VkCommandBufferBeginInfo begin {
.sType = VK_STRUCTURE_TYPE_COMMAND_BUFFER_BEGIN_INFO,
.flags = VK_COMMAND_BUFFER_USAGE_ONE_TIME_SUBMIT_BIT,
};
Device::CheckVkResult(vkBeginCommandBuffer(cmd, &begin));
return cmd;
}
void Submit(VkCommandBuffer cmd) {
Device::CheckVkResult(vkEndCommandBuffer(cmd));
const VkSubmitInfo submit { .sType = VK_STRUCTURE_TYPE_SUBMIT_INFO, .commandBufferCount = 1, .pCommandBuffers = &cmd };
Device::CheckVkResult(vkQueueSubmit(Device::queue, 1, &submit, VK_NULL_HANDLE));
}
// Copies `source` into the host-visible `out`, after the uploads' consumers
// (the transfer stage here) and readable by the host once the queue is idle.
void CopyOut(VkCommandBuffer cmd, VulkanBuffer<std::uint32_t, false>& source, VulkanBuffer<std::uint32_t, true>& out) {
const VkBufferCopy region { .srcOffset = 0, .dstOffset = 0, .size = kCount * sizeof(std::uint32_t) };
vkCmdCopyBuffer(cmd, source.buffer, out.buffer, 1, &region);
const VkBufferMemoryBarrier toHost {
.sType = VK_STRUCTURE_TYPE_BUFFER_MEMORY_BARRIER,
.srcAccessMask = VK_ACCESS_TRANSFER_WRITE_BIT,
.dstAccessMask = VK_ACCESS_HOST_READ_BIT,
.srcQueueFamilyIndex = VK_QUEUE_FAMILY_IGNORED,
.dstQueueFamilyIndex = VK_QUEUE_FAMILY_IGNORED,
.buffer = out.buffer,
.offset = 0,
.size = VK_WHOLE_SIZE,
};
vkCmdPipelineBarrier(cmd, VK_PIPELINE_STAGE_TRANSFER_BIT, VK_PIPELINE_STAGE_HOST_BIT, 0, 0, nullptr, 1, &toHost, 0, nullptr);
}
std::vector<std::uint32_t> Contents(VulkanBuffer<std::uint32_t, true>& out) {
out.FlushHost();
return std::vector<std::uint32_t>(out.value, out.value + kCount);
}
std::vector<std::uint32_t> Filled(std::uint32_t base) {
std::vector<std::uint32_t> values(kCount);
for (std::uint32_t i = 0; i < kCount; ++i) values[i] = base + i;
return values;
}
// One round: upload `before`, record a re-upload with `reupload`, and run a
// copy of the buffer before that re-upload's submission.
void Round(std::string_view name, const std::function<void(VulkanBuffer<std::uint32_t, false>&, VkCommandBuffer)>& reupload,
const std::vector<std::uint32_t>& after) {
const std::vector<std::uint32_t> before = Filled(1000);
VulkanBuffer<std::uint32_t, false> data;
VulkanBuffer<std::uint32_t, true> seenInFlight;
VulkanBuffer<std::uint32_t, true> seenAfter;
seenInFlight.Create(VK_BUFFER_USAGE_TRANSFER_DST_BIT, VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT, kCount);
seenAfter.Create(VK_BUFFER_USAGE_TRANSFER_DST_BIT, VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT, kCount);
VkCommandBuffer setup = BeginCmd();
data.UploadDeviceLocal(kUsage, before.data(), kCount, setup, VK_ACCESS_TRANSFER_READ_BIT, VK_PIPELINE_STAGE_TRANSFER_BIT);
Submit(setup);
Device::CheckVkResult(vkQueueWaitIdle(Device::queue));
++Device::frameCounter;
// The next frame records its re-upload...
VkCommandBuffer next = BeginCmd();
reupload(data, next);
CopyOut(next, data, seenAfter);
// ...while a frame submitted before it still reads the buffer.
VkCommandBuffer inFlight = BeginCmd();
CopyOut(inFlight, data, seenInFlight);
Submit(inFlight);
Device::CheckVkResult(vkQueueWaitIdle(Device::queue));
Submit(next);
Device::CheckVkResult(vkQueueWaitIdle(Device::queue));
Check(Contents(seenInFlight) == before, std::format("{}: a frame submitted before the upload still reads the old contents", name));
Check(Contents(seenAfter) == after, std::format("{}: a read recorded after the upload sees the new contents", name));
data.Clear();
seenInFlight.Clear();
seenAfter.Clear();
Device::DrainDeletions();
}
} // namespace
int main() {
Device::Initialize();
Device::validationErrorCount = 0;
Device::framesInFlight = Window::numFrames;
Device::frameCounter = 0;
const std::vector<std::uint32_t> fresh = Filled(5000);
Round("UploadDeviceLocal of the same size", [&](VulkanBuffer<std::uint32_t, false>& data, VkCommandBuffer cmd) {
data.UploadDeviceLocal(kUsage, fresh.data(), kCount, cmd, VK_ACCESS_TRANSFER_READ_BIT, VK_PIPELINE_STAGE_TRANSFER_BIT);
}, fresh);
Round("UploadDeviceLocalRange", [&](VulkanBuffer<std::uint32_t, false>& data, VkCommandBuffer cmd) {
data.UploadDeviceLocalRange(fresh.data(), 0, kCount, cmd, VK_ACCESS_TRANSFER_READ_BIT, VK_PIPELINE_STAGE_TRANSFER_BIT);
}, fresh);
// Two ranged uploads in one frame: the second must not overwrite the
// first's staged bytes before the first copy has run.
std::vector<std::uint32_t> halves = Filled(1000);
for (std::uint32_t i = 0; i < kCount / 2; ++i) halves[i] = 7000 + i;
for (std::uint32_t i = kCount / 2; i < kCount; ++i) halves[i] = 9000 + i;
Round("two UploadDeviceLocalRange calls in one frame", [&](VulkanBuffer<std::uint32_t, false>& data, VkCommandBuffer cmd) {
data.UploadDeviceLocalRange(halves.data(), 0, kCount / 2, cmd, VK_ACCESS_TRANSFER_READ_BIT, VK_PIPELINE_STAGE_TRANSFER_BIT);
data.UploadDeviceLocalRange(halves.data() + kCount / 2, kCount / 2, kCount / 2, cmd, VK_ACCESS_TRANSFER_READ_BIT, VK_PIPELINE_STAGE_TRANSFER_BIT);
}, halves);
{
VulkanBuffer<std::uint32_t, false> data;
VkCommandBuffer cmd = BeginCmd();
data.UploadDeviceLocal(kUsage, fresh.data(), kCount, cmd, VK_ACCESS_TRANSFER_READ_BIT, VK_PIPELINE_STAGE_TRANSFER_BIT);
Submit(cmd);
Device::CheckVkResult(vkQueueWaitIdle(Device::queue));
++Device::frameCounter;
const std::uint32_t errorsBefore = Device::validationErrorCount;
cmd = BeginCmd();
data.UploadDeviceLocal(kUsage, fresh.data(), 0, cmd, VK_ACCESS_TRANSFER_READ_BIT, VK_PIPELINE_STAGE_TRANSFER_BIT);
data.UploadDeviceLocalRange(fresh.data(), 0, 0, cmd, VK_ACCESS_TRANSFER_READ_BIT, VK_PIPELINE_STAGE_TRANSFER_BIT);
Submit(cmd);
Device::CheckVkResult(vkQueueWaitIdle(Device::queue));
Check(Device::validationErrorCount == errorsBefore, "an empty re-upload records no copy or barrier (validation stays quiet)");
data.Clear();
Device::DrainDeletions();
}
{
// As before any window: no frames in flight, and the frame count
// stands still.
Device::framesInFlight = 0;
VulkanBuffer<std::uint32_t, false> data;
VkCommandBuffer cmd = BeginCmd();
data.UploadDeviceLocal(kUsage, fresh.data(), kCount, cmd, VK_ACCESS_TRANSFER_READ_BIT, VK_PIPELINE_STAGE_TRANSFER_BIT);
Submit(cmd);
Device::CheckVkResult(vkQueueWaitIdle(Device::queue));
for (int round = 0; round < 8; ++round) {
cmd = BeginCmd();
data.UploadDeviceLocalRange(fresh.data(), 0, kCount, cmd, VK_ACCESS_TRANSFER_READ_BIT, VK_PIPELINE_STAGE_TRANSFER_BIT);
Submit(cmd);
Device::CheckVkResult(vkQueueWaitIdle(Device::queue));
Device::DrainDeletions();
}
const VkDeviceSize staged = data.stagingRing.empty() ? 0 : data.stagingRing[0].capacity;
Check(staged <= kCount * sizeof(std::uint32_t),
std::format("without a window, re-uploads between drains hold one upload's staging ({} bytes for {})", staged, kCount * sizeof(std::uint32_t)));
data.Clear();
Device::DrainDeletions();
Device::framesInFlight = Window::numFrames;
}
if (std::getenv("CRAFTER_GRAPHICS_VALIDATION") != nullptr) {
Check(Device::validationErrorCount == 0,
std::format("no Vulkan validation errors across the run ({} seen)", Device::validationErrorCount));
}
std::println("{}", failures == 0 ? "all checks passed" : "check(s) failed");
return failures == 0 ? EXIT_SUCCESS : EXIT_FAILURE;
}