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>
This commit is contained in:
Jorijn van der Graaf 2026-10-20 19:07:54 +02:00
commit 079b6b0e1f
Signed by: jorijnvdgraaf
GPG key ID: 2937E59CDCC1BCFB
47 changed files with 3572 additions and 1430 deletions

View file

@ -25,6 +25,7 @@ module;
#include <wayland-client-protocol.h>
#include <sys/stat.h>
#include <time.h>
#include <poll.h>
#endif
#ifdef CRAFTER_GRAPHICS_WINDOW_WIN32
#include <windows.h>
@ -615,6 +616,19 @@ void Window::RecreateSwapchainAndImages() {
CreateSwapchain();
}
void Window::SetPresentMode(PresentMode mode) {
presentMode = mode;
presentModeUnfit = false;
// A window without a swapchain yet picks the mode up when it makes one.
if (swapChain == VK_NULL_HANDLE) return;
// The mode is fixed when a swapchain is made, so make a new one, as
// Resize does: nothing in flight may still use the old images.
Device::CheckVkResult(vkQueueWaitIdle(Device::queue));
Device::DrainDeletions();
RecreateSwapchainAndImages();
onResize.Invoke();
}
void Window::SetTitle(const std::string_view title) {
#ifdef CRAFTER_GRAPHICS_WINDOW_WAYLAND
xdg_toplevel_set_title(xdgToplevel, title.data());
@ -750,11 +764,100 @@ void Window::SetDefaultCursor() {
#endif
}
#ifdef CRAFTER_GRAPHICS_WINDOW_WAYLAND
namespace {
// Milliseconds until the first window that updates on its own clock is
// due, rounded up so the wait doesn't end just short of it; -1 when no
// window does, so the loop waits for events alone.
int MillisecondsToNextClockedUpdate() {
const auto now = std::chrono::high_resolution_clock::now();
std::optional<std::chrono::high_resolution_clock::duration> soonest;
for (const Window* window : Device::windows) {
if (!window->updating || window->updateInterval <= std::chrono::nanoseconds::zero()) continue;
const auto wait = window->lastFrameBegin + window->updateInterval - now;
if (!soonest || wait < *soonest) soonest = wait;
}
if (!soonest) return -1;
if (*soonest <= std::chrono::high_resolution_clock::duration::zero()) return 0;
return static_cast<int>(std::chrono::ceil<std::chrono::milliseconds>(*soonest).count());
}
}
#endif
void Window::StartSync() {
#ifdef CRAFTER_GRAPHICS_WINDOW_WAYLAND
while (open && wl_display_dispatch(Device::display) != -1) {
// libwayland's read sequence, as wl_display_dispatch runs it, but the
// poll also watches the descriptors windows registered with AddPollFd,
// and times out when a window that updates on its own clock is due.
std::vector<pollfd> fds;
std::vector<int> ready;
bool flushPending = false;
while (open) {
while (wl_display_prepare_read(Device::display) != 0) {
if (wl_display_dispatch_pending(Device::display) == -1) return;
}
// A full socket (EAGAIN) is flushed again once poll says it is
// writable. EPIPE goes on to the read, which reports the
// compositor's error.
flushPending = false;
if (wl_display_flush(Device::display) == -1) {
if (errno == EAGAIN) {
flushPending = true;
} else if (errno != EPIPE) {
wl_display_cancel_read(Device::display);
return;
}
}
fds.clear();
fds.push_back({ .fd = wl_display_get_fd(Device::display), .events = static_cast<short>(POLLIN | (flushPending ? POLLOUT : 0)), .revents = 0 });
for (const Window* window : Device::windows) {
for (const PollFd& entry : window->pollFds) {
fds.push_back({ .fd = entry.fd, .events = POLLIN, .revents = 0 });
}
}
if (poll(fds.data(), fds.size(), MillisecondsToNextClockedUpdate()) == -1) {
wl_display_cancel_read(Device::display);
if (errno == EINTR) continue;
return;
}
if (fds[0].revents & (POLLIN | POLLERR | POLLHUP)) {
if (wl_display_read_events(Device::display) == -1) return;
} else {
wl_display_cancel_read(Device::display);
}
// Frame callbacks run Update here for windows on the compositor's clock.
if (wl_display_dispatch_pending(Device::display) == -1) return;
// A callback may add or remove descriptors, so each ready one is
// looked up again, and its callback copied, before it is called.
ready.clear();
for (std::size_t i = 1; i < fds.size(); ++i) {
if (fds[i].revents != 0) ready.push_back(fds[i].fd);
}
for (int fd : ready) {
for (const Window* window : Device::windows) {
auto entry = std::ranges::find(window->pollFds, fd, &PollFd::fd);
if (entry != window->pollFds.end()) {
std::function<void()> onReadable = entry->onReadable;
onReadable();
break;
}
}
}
Gamepad::Tick();
onBeforeUpdate.Invoke();
// By index: an Update may open or close a window.
const auto now = std::chrono::high_resolution_clock::now();
for (std::size_t i = 0; i < Device::windows.size(); ++i) {
Window* window = Device::windows[i];
if (window->updating && window->updateInterval > std::chrono::nanoseconds::zero()
&& now >= window->lastFrameBegin + window->updateInterval) {
window->Update();
}
}
}
#endif
#ifdef CRAFTER_GRAPHICS_WINDOW_WIN32
@ -766,11 +869,23 @@ void Window::StartSync() {
// iteration. Block on the message queue instead, waking on input or after a
// short timeout so gamepad polling / onBeforeUpdate still tick at a sane
// rate (e.g. to detect a controller button asking to resume).
//
// A window on its own clock (updateInterval) sleeps the same way until
// its next update is due, instead of updating every pass.
constexpr DWORD kPausedPollIntervalMs = 16;
while(open) {
// Read each pass, as the Wayland loop does: the clock may be set or
// changed from inside the loop.
const bool clocked = updateInterval > std::chrono::nanoseconds::zero();
if (!updating) {
MsgWaitForMultipleObjectsEx(0, nullptr, kPausedPollIntervalMs,
QS_ALLINPUT, MWMO_INPUTAVAILABLE);
} else if (clocked) {
const auto wait = lastFrameBegin + updateInterval - std::chrono::high_resolution_clock::now();
if (wait > std::chrono::high_resolution_clock::duration::zero()) {
MsgWaitForMultipleObjectsEx(0, nullptr, static_cast<DWORD>(std::chrono::ceil<std::chrono::milliseconds>(wait).count()),
QS_ALLINPUT, MWMO_INPUTAVAILABLE);
}
}
MSG msg;
while (PeekMessage(&msg, NULL, 0, 0, PM_REMOVE)) {
@ -779,7 +894,7 @@ void Window::StartSync() {
}
Gamepad::Tick();
onBeforeUpdate.Invoke();
if(updating) {
if(updating && (!clocked || std::chrono::high_resolution_clock::now() >= lastFrameBegin + updateInterval)) {
Update();
}
}
@ -801,8 +916,11 @@ void Window::StartUpdate() {
lastFrameBegin = std::chrono::high_resolution_clock::now();
updating = true;
#ifdef CRAFTER_GRAPHICS_WINDOW_WAYLAND
cb = wl_surface_frame(surface);
wl_callback_add_listener(cb, &wl_callback_listener, this);
// A window on its own clock is updated by StartSync's loop instead.
if (updateInterval == std::chrono::nanoseconds::zero()) {
cb = wl_surface_frame(surface);
wl_callback_add_listener(cb, &wl_callback_listener, this);
}
#endif
}
@ -810,6 +928,16 @@ void Window::StopUpdate() {
updating = false;
}
#ifdef CRAFTER_GRAPHICS_WINDOW_WAYLAND
void Window::AddPollFd(int fd, std::function<void()> onReadable) {
pollFds.push_back({ .fd = fd, .onReadable = std::move(onReadable) });
}
void Window::RemovePollFd(int fd) {
std::erase_if(pollFds, [fd](const PollFd& entry) { return entry.fd == fd; });
}
#endif
std::chrono::time_point<std::chrono::high_resolution_clock> startTime;
void Window::Update() {
@ -846,6 +974,133 @@ void Window::Update() {
lastFrameBegin = startTime;
}
namespace {
// A frame's readback for CaptureNextFrame: the buffer the swapchain image
// is copied into, recorded into the frame's own command buffer.
// It owns the buffer and its memory, which go through the deletion queue
// however the capture ends: written, or unwound past (a failed
// allocation, a lost device), as the frame may be copying into it.
struct FrameCapture {
VkBuffer buffer = VK_NULL_HANDLE;
VkDeviceMemory memory = VK_NULL_HANDLE;
VkMemoryPropertyFlags memoryFlags = 0;
std::uint32_t width = 0;
std::uint32_t height = 0;
// Where it goes: taken from Window::pendingCapture as the copy is
// recorded, so a capture asked for meanwhile (from onResize, say) is
// the next frame's.
std::filesystem::path path;
FrameCapture(std::uint32_t width, std::uint32_t height) : width(width), height(height) {}
FrameCapture(FrameCapture&& other) noexcept
: buffer(std::exchange(other.buffer, VK_NULL_HANDLE)), memory(std::exchange(other.memory, VK_NULL_HANDLE)),
memoryFlags(other.memoryFlags), width(other.width), height(other.height), path(std::move(other.path)) {}
FrameCapture& operator=(FrameCapture&&) = delete;
~FrameCapture() {
Device::EnqueueDeletion(buffer, memory);
}
};
// Records the copy of `image`, in GENERAL and last written at `writers`
// with `writerAccess`, into a new host-visible buffer.
FrameCapture RecordFrameCapture(VkCommandBuffer cmd, VkImage image, std::uint32_t width, std::uint32_t height, VkPipelineStageFlags writers, VkAccessFlags writerAccess) {
FrameCapture capture(width, height);
const VkBufferCreateInfo bufferInfo {
.sType = VK_STRUCTURE_TYPE_BUFFER_CREATE_INFO,
.size = static_cast<VkDeviceSize>(width) * height * 4,
.usage = VK_BUFFER_USAGE_TRANSFER_DST_BIT,
.sharingMode = VK_SHARING_MODE_EXCLUSIVE,
};
// Into a local first: a call that fails leaves its output undefined.
VkBuffer buffer = VK_NULL_HANDLE;
Device::CheckVkResult(vkCreateBuffer(Device::device, &bufferInfo, nullptr, &buffer));
capture.buffer = buffer;
VkMemoryRequirements requirements;
vkGetBufferMemoryRequirements(Device::device, capture.buffer, &requirements);
// The CPU reads all of it: cached memory where there is some, as
// write-combined memory is very slow to read (#59, #89).
std::uint32_t memoryType;
try {
memoryType = Device::GetMemoryType(requirements.memoryTypeBits,
VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT | VK_MEMORY_PROPERTY_HOST_CACHED_BIT, VK_MEMORY_PROPERTY_HOST_COHERENT_BIT);
} catch (const std::runtime_error&) {
memoryType = Device::GetMemoryType(requirements.memoryTypeBits, VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT);
}
capture.memoryFlags = Device::memoryProperties.memoryTypes[memoryType].propertyFlags;
const VkMemoryAllocateInfo allocation {
.sType = VK_STRUCTURE_TYPE_MEMORY_ALLOCATE_INFO,
.allocationSize = requirements.size,
.memoryTypeIndex = memoryType,
};
VkDeviceMemory memory = VK_NULL_HANDLE;
Device::CheckVkResult(vkAllocateMemory(Device::device, &allocation, nullptr, &memory));
capture.memory = memory;
Device::CheckVkResult(vkBindBufferMemory(Device::device, capture.buffer, capture.memory, 0));
// The passes' writes, before the copy reads them.
const VkImageMemoryBarrier written {
.sType = VK_STRUCTURE_TYPE_IMAGE_MEMORY_BARRIER,
.srcAccessMask = writerAccess,
.dstAccessMask = VK_ACCESS_TRANSFER_READ_BIT,
.oldLayout = VK_IMAGE_LAYOUT_GENERAL,
.newLayout = VK_IMAGE_LAYOUT_GENERAL,
.srcQueueFamilyIndex = VK_QUEUE_FAMILY_IGNORED,
.dstQueueFamilyIndex = VK_QUEUE_FAMILY_IGNORED,
.image = image,
.subresourceRange = { VK_IMAGE_ASPECT_COLOR_BIT, 0, 1, 0, 1 },
};
vkCmdPipelineBarrier(cmd, writers, VK_PIPELINE_STAGE_TRANSFER_BIT, 0, 0, nullptr, 0, nullptr, 1, &written);
const VkBufferImageCopy region {
.imageSubresource = { VK_IMAGE_ASPECT_COLOR_BIT, 0, 0, 1 },
.imageExtent = { width, height, 1 },
};
vkCmdCopyImageToBuffer(cmd, image, VK_IMAGE_LAYOUT_GENERAL, capture.buffer, 1, &region);
const VkBufferMemoryBarrier copied {
.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 = capture.buffer,
.offset = 0,
.size = VK_WHOLE_SIZE,
};
vkCmdPipelineBarrier(cmd, VK_PIPELINE_STAGE_TRANSFER_BIT, VK_PIPELINE_STAGE_HOST_BIT, 0, 0, nullptr, 1, &copied, 0, nullptr);
return capture;
}
// Once the frame has finished: the readback as an RGBA PNG at `path`.
void WriteFrameCapture(const FrameCapture& capture, VkFormat format) {
const std::filesystem::path& path = capture.path;
void* mapped = nullptr;
Device::CheckVkResult(vkMapMemory(Device::device, capture.memory, 0, VK_WHOLE_SIZE, 0, &mapped));
// Cached but not coherent: the copy's writes have to be pulled in.
if ((capture.memoryFlags & VK_MEMORY_PROPERTY_HOST_COHERENT_BIT) == 0) {
const VkMappedMemoryRange range {
.sType = VK_STRUCTURE_TYPE_MAPPED_MEMORY_RANGE,
.memory = capture.memory,
.offset = 0,
.size = VK_WHOLE_SIZE,
};
Device::CheckVkResult(vkInvalidateMappedMemoryRanges(Device::device, 1, &range));
}
const std::uint8_t* src = static_cast<const std::uint8_t*>(mapped);
const std::size_t texels = static_cast<std::size_t>(capture.width) * capture.height;
std::vector<std::uint8_t> rgba(texels * 4);
const bool bgr = format == VK_FORMAT_B8G8R8A8_UNORM;
for (std::size_t i = 0; i < texels; ++i) {
rgba[i * 4 + 0] = src[i * 4 + (bgr ? 2 : 0)];
rgba[i * 4 + 1] = src[i * 4 + 1];
rgba[i * 4 + 2] = src[i * 4 + (bgr ? 0 : 2)];
rgba[i * 4 + 3] = src[i * 4 + 3];
}
vkUnmapMemory(Device::device, capture.memory);
if (stbi_write_png(path.string().c_str(), static_cast<int>(capture.width), static_cast<int>(capture.height), 4, rgba.data(), static_cast<int>(capture.width) * 4) == 0) {
throw std::runtime_error(std::format("CaptureNextFrame: could not write {}", path.string()));
}
}
}
void Window::Render() {
// Acquire the next image from the swap chain. If the surface has
// changed size out from under us (compositor/Win32 resize delivered
@ -981,10 +1236,10 @@ void Window::Render() {
}
#endif
// Note: vkCmdClearColorImage is unavailable here — the swapchain is
// created with VK_IMAGE_USAGE_STORAGE_BIT only (no TRANSFER_DST_BIT).
// Passes that need a background should write one explicitly (UIScene
// exposes a `background()` setter for this purpose).
// The window doesn't clear the image: not every surface allows the
// swapchain TRANSFER_DST (swapchainUsage says whether this one does), so
// passes that need a background write one themselves, by a clear where
// the usage allows it or from a shader (UIScene's `background()`).
(void)clearColor;
for (std::size_t i = 0; i < passes.size(); ++i) {
@ -1000,12 +1255,12 @@ void Window::Render() {
// each pass's own SwapchainStage(), so a compute->compute edge only
// serialises COMPUTE_SHADER while an RT pass on either side pulls in
// RAY_TRACING_SHADER — never the ALL_COMMANDS full-pipeline stall.
VkImageMemoryBarrier ib = BuildSwapchainInterPassBarrier(images[currentBuffer]);
// Masked like swapWriterStages above: a pass that keeps the
// default stage reports a union that names RAY_TRACING_SHADER.
vkCmdPipelineBarrier(drawCmdBuffers[currentBuffer],
passes[i]->SwapchainStage() & Device::stageMask,
passes[i + 1]->SwapchainStage() & Device::stageMask,
const VkPipelineStageFlags srcStages = passes[i]->SwapchainStage() & Device::stageMask;
const VkPipelineStageFlags dstStages = passes[i + 1]->SwapchainStage() & Device::stageMask;
VkImageMemoryBarrier ib = BuildSwapchainInterPassBarrier(images[currentBuffer], srcStages, dstStages);
vkCmdPipelineBarrier(drawCmdBuffers[currentBuffer], srcStages, dstStages,
0, 0, nullptr, 0, nullptr, 1, &ib);
}
}
@ -1018,10 +1273,21 @@ void Window::Render() {
// TRANSFER_WRITE here too — derive both from the same union for symmetry.
presentBarrier.srcAccessMask = SwapchainWriterAccess(swapWriterStages);
// CaptureNextFrame: the copy goes here, while the image is still this
// frame's, and the transition to present waits for it.
std::optional<FrameCapture> capture;
VkPipelineStageFlags presentWaitStages = swapWriterStages;
if (pendingCapture) {
capture.emplace(RecordFrameCapture(drawCmdBuffers[currentBuffer], images[currentBuffer], width, height, swapWriterStages, presentBarrier.srcAccessMask));
presentWaitStages |= VK_PIPELINE_STAGE_TRANSFER_BIT;
capture->path = std::move(*pendingCapture);
pendingCapture.reset();
}
// The ->PRESENT_SRC transition only needs to wait on the stages that
// actually wrote the image (the per-pass union), not ALL_COMMANDS; present
// itself is gated by the render-finished semaphore, so dst stays BOTTOM.
vkCmdPipelineBarrier(drawCmdBuffers[currentBuffer], swapWriterStages, VK_PIPELINE_STAGE_BOTTOM_OF_PIPE_BIT, 0, 0, nullptr, 0, nullptr, 1, &presentBarrier);
vkCmdPipelineBarrier(drawCmdBuffers[currentBuffer], presentWaitStages, VK_PIPELINE_STAGE_BOTTOM_OF_PIPE_BIT, 0, 0, nullptr, 0, nullptr, 1, &presentBarrier);
Device::CheckVkResult(vkEndCommandBuffer(drawCmdBuffers[currentBuffer]));
@ -1062,6 +1328,13 @@ void Window::Render() {
// separate from the per-window frameCounter above (which also keys the
// acquire-semaphore slot) so non-window code can enqueue against it.
Device::frameCounter++;
if (capture) {
// A capture is for tests and screenshots: waiting for the frame here
// is fine.
Device::CheckVkResult(vkQueueWaitIdle(Device::queue));
WriteFrameCapture(*capture, colorFormat);
}
}
#ifdef CRAFTER_TIMING
@ -1148,30 +1421,37 @@ void Window::CreateSwapchain()
swapchainCI.imageFormat = colorFormat;
swapchainCI.imageColorSpace = colorSpace;
swapchainCI.imageExtent = { swapchainExtent.width, swapchainExtent.height };
swapchainCI.imageUsage = VK_IMAGE_USAGE_STORAGE_BIT | VK_IMAGE_USAGE_TRANSFER_SRC_BIT;
swapchainUsage = VK_IMAGE_USAGE_STORAGE_BIT | VK_IMAGE_USAGE_TRANSFER_SRC_BIT
| (surfCaps.supportedUsageFlags & VK_IMAGE_USAGE_TRANSFER_DST_BIT);
swapchainCI.imageUsage = swapchainUsage;
swapchainCI.preTransform = (VkSurfaceTransformFlagBitsKHR)preTransform;
swapchainCI.imageArrayLayers = 1;
swapchainCI.imageSharingMode = VK_SHARING_MODE_EXCLUSIVE;
swapchainCI.queueFamilyIndexCount = 0;
// VK_PRESENT_MODE_FIFO_KHR ("v-sync") is guaranteed available per spec, so
// it is the default. For benchmarking we want frame pacing uncapped from
// the display refresh, so when CRAFTER_PRESENT_IMMEDIATE is set in the
// environment we enumerate the surface's present modes and prefer
// IMMEDIATE (then MAILBOX) when offered, falling back to FIFO otherwise.
swapchainCI.presentMode = VK_PRESENT_MODE_FIFO_KHR;
if (std::getenv("CRAFTER_PRESENT_IMMEDIATE")) {
// it is the fallback for a mode the surface lacks. For benchmarking we
// want frame pacing uncapped from the display refresh, so when
// CRAFTER_PRESENT_IMMEDIATE is set in the environment we prefer IMMEDIATE
// (then MAILBOX) whatever presentMode asks for.
{
std::uint32_t presentModeCount = 0;
vkGetPhysicalDeviceSurfacePresentModesKHR(Device::physDevice, vulkanSurface, &presentModeCount, nullptr);
std::vector<VkPresentModeKHR> presentModes(presentModeCount);
vkGetPhysicalDeviceSurfacePresentModesKHR(Device::physDevice, vulkanSurface, &presentModeCount, presentModes.data());
bool haveImmediate = false, haveMailbox = false;
for (VkPresentModeKHR m : presentModes) {
if (m == VK_PRESENT_MODE_IMMEDIATE_KHR) haveImmediate = true;
if (m == VK_PRESENT_MODE_MAILBOX_KHR) haveMailbox = true;
auto offered = [&](VkPresentModeKHR mode) { return std::ranges::find(presentModes, mode) != presentModes.end(); };
swapchainPresentMode = VK_PRESENT_MODE_FIFO_KHR;
if (presentModeUnfit) {
// Stays FIFO; see below.
} else if (std::getenv("CRAFTER_PRESENT_IMMEDIATE")) {
if (offered(VK_PRESENT_MODE_IMMEDIATE_KHR)) swapchainPresentMode = VK_PRESENT_MODE_IMMEDIATE_KHR;
else if (offered(VK_PRESENT_MODE_MAILBOX_KHR)) swapchainPresentMode = VK_PRESENT_MODE_MAILBOX_KHR;
} else if (presentMode == PresentMode::Mailbox && offered(VK_PRESENT_MODE_MAILBOX_KHR)) {
swapchainPresentMode = VK_PRESENT_MODE_MAILBOX_KHR;
} else if (presentMode == PresentMode::Immediate && offered(VK_PRESENT_MODE_IMMEDIATE_KHR)) {
swapchainPresentMode = VK_PRESENT_MODE_IMMEDIATE_KHR;
}
if (haveImmediate) swapchainCI.presentMode = VK_PRESENT_MODE_IMMEDIATE_KHR;
else if (haveMailbox) swapchainCI.presentMode = VK_PRESENT_MODE_MAILBOX_KHR;
}
swapchainCI.presentMode = swapchainPresentMode;
// Setting oldSwapChain to the saved handle of the previous swapchain aids in resource reuse and makes sure that we can still present already acquired images
swapchainCI.oldSwapchain = oldSwapchain;
// Setting clipped to VK_TRUE allows the implementation to discard rendering outside of the surface area
@ -1188,8 +1468,21 @@ void Window::CreateSwapchain()
Device::CheckVkResult(vkGetSwapchainImagesKHR(Device::device, swapChain, &imageCount, nullptr));
// images[] and every other per-frame array hold exactly numFrames
// entries. A driver may create more images than minImageCount asked for
// (a surface whose minimum is above numFrames must); writing them into
// images[] would run off its end, so refuse instead.
// (a surface whose minimum is above numFrames must, and Mesa gives
// MAILBOX at least four). MAILBOX and IMMEDIATE are wishes: FIFO, with
// numFrames, is the frame loop's own. If that has other than numFrames
// too, writing them into images[] would run off its end, so refuse.
if (imageCount != numFrames && swapchainPresentMode != VK_PRESENT_MODE_FIFO_KHR) {
std::println(std::cerr, "Window: the {} swapchain has {} images, not the frame loop's {}; using FIFO",
swapchainPresentMode == VK_PRESENT_MODE_MAILBOX_KHR ? "MAILBOX" : "IMMEDIATE", imageCount, numFrames);
vkDestroySwapchainKHR(Device::device, swapChain, nullptr);
presentModeUnfit = true;
swapchainPresentMode = VK_PRESENT_MODE_FIFO_KHR;
swapchainCI.presentMode = swapchainPresentMode;
swapchainCI.oldSwapchain = VK_NULL_HANDLE;
Device::CheckVkResult(vkCreateSwapchainKHR(Device::device, &swapchainCI, nullptr, &swapChain));
Device::CheckVkResult(vkGetSwapchainImagesKHR(Device::device, swapChain, &imageCount, nullptr));
}
if (imageCount != numFrames) {
throw std::runtime_error(std::format("Window: the swapchain has {} images, but the frame loop is built for exactly {}", imageCount, numFrames));
}
@ -1301,7 +1594,8 @@ void Window::wl_surface_frame_done(void* data, struct wl_callback *cb, uint32_t
Window* window = reinterpret_cast<Window*>(data);
// The member names the live callback, so the destructor can cancel it.
if(window->updating) {
// A window that has since moved to its own clock lets the chain end.
if(window->updating && window->updateInterval == std::chrono::nanoseconds::zero()) {
window->cb = wl_surface_frame(window->surface);
wl_callback_add_listener(window->cb, &Window::wl_callback_listener, window);
window->Update();
@ -1359,171 +1653,8 @@ void Window::xdg_surface_handle_preferred_scale(void* data, wp_fractional_scale_
#endif
void Window::SaveFrame(const std::filesystem::path& path) {
// Staging buffer big enough for one RGBA frame.
VkDeviceSize bufSize = static_cast<VkDeviceSize>(width) * height * 4;
VkBufferCreateInfo bci{
.sType = VK_STRUCTURE_TYPE_BUFFER_CREATE_INFO,
.size = bufSize,
.usage = VK_BUFFER_USAGE_TRANSFER_DST_BIT,
.sharingMode = VK_SHARING_MODE_EXCLUSIVE,
};
VkBuffer stagingBuf = VK_NULL_HANDLE;
Device::CheckVkResult(vkCreateBuffer(Device::device, &bci, nullptr, &stagingBuf));
VkMemoryRequirements memReqs;
vkGetBufferMemoryRequirements(Device::device, stagingBuf, &memReqs);
// Readback path: the CPU *reads* this whole buffer back. The first
// HOST_VISIBLE|HOST_COHERENT type is usually write-combined (uncached) on
// discrete GPUs, where CPU reads are pathologically slow. Ask for
// HOST_CACHED so reads hit the CPU cache, preferring a cached-coherent type
// when one exists. HOST_CACHED is near-universal but not spec-guaranteed,
// so fall back to any HOST_VISIBLE type (#59) if absent — that path then
// relies on the coherency-gated invalidate below. (Deliberately NOT routed
// through the upload-strategy helper, which steers toward write-combined
// DEVICE_LOCAL|HOST_VISIBLE — the worst type for readback. See issue #89.)
std::uint32_t memTypeIndex;
try {
memTypeIndex = Device::GetMemoryType(memReqs.memoryTypeBits,
VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT | VK_MEMORY_PROPERTY_HOST_CACHED_BIT,
VK_MEMORY_PROPERTY_HOST_COHERENT_BIT);
} catch (const std::runtime_error&) {
memTypeIndex = Device::GetMemoryType(memReqs.memoryTypeBits,
VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT);
}
VkMemoryAllocateInfo mai{
.sType = VK_STRUCTURE_TYPE_MEMORY_ALLOCATE_INFO,
.allocationSize = memReqs.size,
.memoryTypeIndex = memTypeIndex,
};
VkDeviceMemory stagingMem = VK_NULL_HANDLE;
Device::CheckVkResult(vkAllocateMemory(Device::device, &mai, nullptr, &stagingMem));
Device::CheckVkResult(vkBindBufferMemory(Device::device, stagingBuf, stagingMem, 0));
// One-shot command buffer so we don't trash the per-frame ones.
VkCommandBufferAllocateInfo cba{
.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, &cba, &cmd));
VkCommandBufferBeginInfo cbi{
.sType = VK_STRUCTURE_TYPE_COMMAND_BUFFER_BEGIN_INFO,
.flags = VK_COMMAND_BUFFER_USAGE_ONE_TIME_SUBMIT_BIT,
};
Device::CheckVkResult(vkBeginCommandBuffer(cmd, &cbi));
VkImageSubresourceRange range{
.aspectMask = VK_IMAGE_ASPECT_COLOR_BIT,
.baseMipLevel = 0,
.levelCount = 1,
.baseArrayLayer = 0,
.layerCount = 1,
};
// Render() leaves the image in PRESENT_SRC_KHR.
VkImageMemoryBarrier toSrc{
.sType = VK_STRUCTURE_TYPE_IMAGE_MEMORY_BARRIER,
.srcAccessMask = 0,
.dstAccessMask = VK_ACCESS_TRANSFER_READ_BIT,
.oldLayout = VK_IMAGE_LAYOUT_PRESENT_SRC_KHR,
.newLayout = VK_IMAGE_LAYOUT_TRANSFER_SRC_OPTIMAL,
.srcQueueFamilyIndex = VK_QUEUE_FAMILY_IGNORED,
.dstQueueFamilyIndex = VK_QUEUE_FAMILY_IGNORED,
.image = images[currentBuffer],
.subresourceRange = range,
};
vkCmdPipelineBarrier(cmd,
VK_PIPELINE_STAGE_BOTTOM_OF_PIPE_BIT, VK_PIPELINE_STAGE_TRANSFER_BIT,
0, 0, nullptr, 0, nullptr, 1, &toSrc);
VkBufferImageCopy region{
.bufferOffset = 0,
.bufferRowLength = 0,
.bufferImageHeight = 0,
.imageSubresource = { VK_IMAGE_ASPECT_COLOR_BIT, 0, 0, 1 },
.imageOffset = { 0, 0, 0 },
.imageExtent = { width, height, 1 },
};
vkCmdCopyImageToBuffer(cmd, images[currentBuffer],
VK_IMAGE_LAYOUT_TRANSFER_SRC_OPTIMAL, stagingBuf, 1, &region);
VkImageMemoryBarrier back{
.sType = VK_STRUCTURE_TYPE_IMAGE_MEMORY_BARRIER,
.srcAccessMask = VK_ACCESS_TRANSFER_READ_BIT,
.dstAccessMask = 0,
.oldLayout = VK_IMAGE_LAYOUT_TRANSFER_SRC_OPTIMAL,
.newLayout = VK_IMAGE_LAYOUT_PRESENT_SRC_KHR,
.srcQueueFamilyIndex = VK_QUEUE_FAMILY_IGNORED,
.dstQueueFamilyIndex = VK_QUEUE_FAMILY_IGNORED,
.image = images[currentBuffer],
.subresourceRange = range,
};
vkCmdPipelineBarrier(cmd,
VK_PIPELINE_STAGE_TRANSFER_BIT, VK_PIPELINE_STAGE_BOTTOM_OF_PIPE_BIT,
0, 0, nullptr, 0, nullptr, 1, &back);
Device::CheckVkResult(vkEndCommandBuffer(cmd));
VkSubmitInfo si{
.sType = VK_STRUCTURE_TYPE_SUBMIT_INFO,
.commandBufferCount = 1,
.pCommandBuffers = &cmd,
};
Device::CheckVkResult(vkQueueSubmit(Device::queue, 1, &si, VK_NULL_HANDLE));
Device::CheckVkResult(vkQueueWaitIdle(Device::queue));
// Read back, swizzle BGRA → RGBA if needed, write PNG.
void* mapped = nullptr;
Device::CheckVkResult(vkMapMemory(Device::device, stagingMem, 0, VK_WHOLE_SIZE, 0, &mapped));
// If the chosen type is cached-but-not-coherent, the transfer's writes may
// not yet be visible to the CPU read; invalidate to pull them in. Gate on
// the chosen type's actual coherency (not the requested flags) — a coherent
// type needs no invalidate. Whole-range invalidate sidesteps
// nonCoherentAtomSize. This is the read-path counterpart of FlushDevice (#60).
if (!(Device::memoryProperties.memoryTypes[memTypeIndex].propertyFlags &
VK_MEMORY_PROPERTY_HOST_COHERENT_BIT)) {
VkMappedMemoryRange invalidateRange{
.sType = VK_STRUCTURE_TYPE_MAPPED_MEMORY_RANGE,
.memory = stagingMem,
.offset = 0,
.size = VK_WHOLE_SIZE,
};
Device::CheckVkResult(
vkInvalidateMappedMemoryRanges(Device::device, 1, &invalidateRange));
}
const std::uint8_t* src = static_cast<const std::uint8_t*>(mapped);
std::vector<std::uint8_t> rgba(static_cast<std::size_t>(width) * height * 4);
bool bgr = (colorFormat == VK_FORMAT_B8G8R8A8_UNORM);
for (std::uint32_t i = 0; i < width * height; ++i) {
if (bgr) {
rgba[i*4+0] = src[i*4+2];
rgba[i*4+1] = src[i*4+1];
rgba[i*4+2] = src[i*4+0];
rgba[i*4+3] = src[i*4+3];
} else {
rgba[i*4+0] = src[i*4+0];
rgba[i*4+1] = src[i*4+1];
rgba[i*4+2] = src[i*4+2];
rgba[i*4+3] = src[i*4+3];
}
}
vkUnmapMemory(Device::device, stagingMem);
stbi_write_png(path.string().c_str(), static_cast<int>(width), static_cast<int>(height),
4, rgba.data(), static_cast<int>(width) * 4);
vkFreeCommandBuffers(Device::device, Device::commandPool, 1, &cmd);
vkDestroyBuffer(Device::device, stagingBuf, nullptr);
vkFreeMemory(Device::device, stagingMem, nullptr);
void Window::CaptureNextFrame(std::filesystem::path path) {
pendingCapture = std::move(path);
}
#endif // !CRAFTER_GRAPHICS_WINDOW_DOM
@ -1707,6 +1838,11 @@ void Window::StopUpdate() {
Crafter::DomEnv::domStopFrameLoop();
}
void Window::SetPresentMode(PresentMode mode) {
// The page presents the canvas; there is no mode to pick.
presentMode = mode;
}
void Window::Update() {
auto now = std::chrono::high_resolution_clock::now();
mouseDelta = {currentMousePos.x - lastMousePos.x,