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

@ -22,9 +22,8 @@ ComputeShader::ComputeShader(ComputeShader&& other) noexcept
ComputeShader& ComputeShader::operator=(ComputeShader&& other) noexcept {
if (this != &other) {
if (pipeline != VK_NULL_HANDLE) {
vkDestroyPipeline(Device::device, pipeline, nullptr);
}
// A frame in flight may still dispatch the pipeline this replaces.
Device::EnqueueDeletion(pipeline);
pipeline = other.pipeline;
layout = other.layout;
workaroundNeedsTlas = other.workaroundNeedsTlas;
@ -35,10 +34,9 @@ ComputeShader& ComputeShader::operator=(ComputeShader&& other) noexcept {
}
ComputeShader::~ComputeShader() {
if (pipeline != VK_NULL_HANDLE) {
vkDestroyPipeline(Device::device, pipeline, nullptr);
pipeline = VK_NULL_HANDLE;
}
// Deferred, like the other owners of Vulkan handles: a frame in flight
// may still dispatch it, and a static one goes after the driver.
Device::EnqueueDeletion(pipeline);
}
void ComputeShader::Load(const std::filesystem::path& spvPath, DescriptorHeapVulkan& heap) {
@ -48,10 +46,9 @@ void ComputeShader::Load(const std::filesystem::path& spvPath, DescriptorHeapVul
// descriptor-set build compiled from the same source instead.
std::filesystem::path setsPath = spvPath;
setsPath.replace_extension("sets.spv");
// VulkanShader only prints when a file is missing and then creates an
// empty module, which surfaces as an unrelated driver error. A missing
// variant is the likely mistake when a bundle is copied to a device
// without the heap, so name it.
// VulkanShader would throw naming only the file. A missing variant is
// the likely mistake when a bundle is copied to a device without the
// heap, so say how to make it.
if (!std::filesystem::exists(setsPath)) {
throw std::runtime_error(std::format(
"ComputeShader::Load: this device has no VK_EXT_descriptor_heap, so the descriptor-set build "
@ -74,12 +71,12 @@ void ComputeShader::Load(const std::filesystem::path& spvPath, DescriptorHeapVul
},
.layout = layout,
};
const VkResult created = vkCreateComputePipelines(
Device::device, Device::pipelineCache, 1, &info, nullptr, &pipeline);
// The module is only an input to pipeline creation; VulkanShader does
// not destroy it, so it is released here either way.
vkDestroyShaderModule(Device::device, shader.shader, nullptr);
Device::CheckVkResult(created);
VkPipeline created = VK_NULL_HANDLE;
Device::CheckVkResult(vkCreateComputePipelines(
Device::device, Device::pipelineCache, 1, &info, nullptr, &created));
// Loading again replaces the pipeline, which a frame in flight may
// still dispatch.
Device::EnqueueDeletion(std::exchange(pipeline, created));
return;
}
@ -110,10 +107,10 @@ void ComputeShader::Load(const std::filesystem::path& spvPath, DescriptorHeapVul
},
.layout = VK_NULL_HANDLE,
};
const VkResult created = vkCreateComputePipelines(
Device::device, Device::pipelineCache, 1, &info, nullptr, &pipeline);
vkDestroyShaderModule(Device::device, shader.shader, nullptr);
Device::CheckVkResult(created);
VkPipeline created = VK_NULL_HANDLE;
Device::CheckVkResult(vkCreateComputePipelines(
Device::device, Device::pipelineCache, 1, &info, nullptr, &created));
Device::EnqueueDeletion(std::exchange(pipeline, created));
}
void ComputeShader::Dispatch(VkCommandBuffer cmd,

View file

@ -871,7 +871,11 @@ void Device::Initialize() {
VkDebugUtilsMessengerCreateInfoEXT debugUtilsMessengerCreateInfo = {};
debugUtilsMessengerCreateInfo.sType = VK_STRUCTURE_TYPE_DEBUG_UTILS_MESSENGER_CREATE_INFO_EXT;
debugUtilsMessengerCreateInfo.messageSeverity = VK_DEBUG_UTILS_MESSAGE_SEVERITY_INFO_BIT_EXT | VK_DEBUG_UTILS_MESSAGE_SEVERITY_WARNING_BIT_EXT | VK_DEBUG_UTILS_MESSAGE_SEVERITY_ERROR_BIT_EXT;
// Info messages (the loader reports its device sorting, the layers their
// settings) are for whoever is debugging with validation on; without it
// they would start every application's output.
debugUtilsMessengerCreateInfo.messageSeverity = VK_DEBUG_UTILS_MESSAGE_SEVERITY_WARNING_BIT_EXT | VK_DEBUG_UTILS_MESSAGE_SEVERITY_ERROR_BIT_EXT
| (enableValidation ? VK_DEBUG_UTILS_MESSAGE_SEVERITY_INFO_BIT_EXT : 0);
debugUtilsMessengerCreateInfo.messageType = VK_DEBUG_UTILS_MESSAGE_TYPE_GENERAL_BIT_EXT | VK_DEBUG_UTILS_MESSAGE_TYPE_VALIDATION_BIT_EXT | VK_DEBUG_UTILS_MESSAGE_TYPE_PERFORMANCE_BIT_EXT;
debugUtilsMessengerCreateInfo.pfnUserCallback = onError;
CheckVkResult(GET_EXTENSION_FUNCTION(vkCreateDebugUtilsMessengerEXT)(instance, &debugUtilsMessengerCreateInfo, NULL, &debugMessenger));
@ -884,7 +888,37 @@ void Device::Initialize() {
uint32_t bestScore = 0;
for (uint32_t i = 0; i < physDeviceCount; i++)
// CRAFTER_GRAPHICS_DEVICE picks a GPU by its number among the distinct
// ones in this list: with two of the same GPU nothing else tells them
// apart, and the first may be too busy to use. A GPU two driver manifests
// both list (a Flatpak sees the runtime's and the GL extension's) counts
// once, so the number means the same GPU inside a sandbox and out.
const char* chosenDevice = std::getenv("CRAFTER_GRAPHICS_DEVICE");
if (chosenDevice != nullptr) {
std::vector<VkPhysicalDevice> distinct;
std::vector<std::array<std::uint8_t, VK_UUID_SIZE>> seen;
for (VkPhysicalDevice device : physDevices) {
VkPhysicalDeviceIDProperties id { .sType = VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_ID_PROPERTIES };
VkPhysicalDeviceProperties2 properties { .sType = VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_PROPERTIES_2, .pNext = &id };
vkGetPhysicalDeviceProperties2(device, &properties);
std::array<std::uint8_t, VK_UUID_SIZE> uuid;
std::ranges::copy(id.deviceUUID, uuid.begin());
if (std::ranges::find(seen, uuid) == seen.end()) {
seen.push_back(uuid);
distinct.push_back(device);
}
}
const std::string_view text = chosenDevice;
std::size_t index = 0;
const auto [end, error] = std::from_chars(text.data(), text.data() + text.size(), index);
if (error != std::errc() || end != text.data() + text.size() || index >= distinct.size()) {
throw std::runtime_error(std::format("CRAFTER_GRAPHICS_DEVICE={} isn't the number of a GPU: Vulkan lists {} GPU(s), 0 to {}",
chosenDevice, distinct.size(), distinct.size() - 1));
}
physDevice = distinct[index];
}
for (uint32_t i = 0; chosenDevice == nullptr && i < physDeviceCount; i++)
{
VkPhysicalDevice device = physDevices[i];
@ -1408,10 +1442,45 @@ void Device::EnqueueDeletion(VkBuffer buffer, VkDeviceMemory memory) {
// Nothing to free for an already-null handle — avoids parking dead
// entries that ReclaimDeletions would have to skip over.
if (buffer == VK_NULL_HANDLE) return;
deletionQueue.push_back({ frameCounter, buffer, memory });
std::lock_guard lock(deletionMutex);
deletionQueue.push_back({ .retireAfter = frameCounter, .buffer = buffer, .memory = memory });
}
void Device::EnqueueDeletion(VkAccelerationStructureKHR accelerationStructure) {
if (accelerationStructure == VK_NULL_HANDLE) return;
std::lock_guard lock(deletionMutex);
deletionQueue.push_back({ .retireAfter = frameCounter, .accelerationStructure = accelerationStructure });
}
void Device::EnqueueDeletion(VkShaderModule shaderModule) {
if (shaderModule == VK_NULL_HANDLE) return;
std::lock_guard lock(deletionMutex);
deletionQueue.push_back({ .retireAfter = frameCounter, .shaderModule = shaderModule });
}
void Device::EnqueueDeletion(VkPipeline pipeline) {
if (pipeline == VK_NULL_HANDLE) return;
std::lock_guard lock(deletionMutex);
deletionQueue.push_back({ .retireAfter = frameCounter, .pipeline = pipeline });
}
namespace {
// Free whatever one queued entry holds. The destroy and free calls accept
// VK_NULL_HANDLE for the kinds it doesn't hold; the acceleration-structure
// entry point is only loaded with ray tracing, so that one is guarded.
void DestroyPending(const Device::PendingDeletion& entry) {
if (entry.accelerationStructure != VK_NULL_HANDLE) {
Device::vkDestroyAccelerationStructureKHR(Device::device, entry.accelerationStructure, nullptr);
}
vkDestroyShaderModule(Device::device, entry.shaderModule, nullptr);
vkDestroyPipeline(Device::device, entry.pipeline, nullptr);
vkDestroyBuffer(Device::device, entry.buffer, nullptr);
vkFreeMemory(Device::device, entry.memory, nullptr);
}
}
void Device::ReclaimDeletions() {
std::lock_guard lock(deletionMutex);
// Free every entry whose retire frame has been reached. An entry tagged at
// frame F retires at F + framesInFlight: by the time the CPU has begun that
// many later frames (each gated by a per-image fence wait), single-queue
@ -1423,19 +1492,18 @@ void Device::ReclaimDeletions() {
// newer, hence also in flight). O(ready), not O(queue).
while (!deletionQueue.empty() &&
deletionQueue.front().retireAfter + framesInFlight <= frameCounter) {
const PendingDeletion& entry = deletionQueue.front();
vkDestroyBuffer(device, entry.buffer, nullptr);
vkFreeMemory(device, entry.memory, nullptr);
DestroyPending(deletionQueue.front());
deletionQueue.pop_front();
}
}
void Device::DrainDeletions() {
std::lock_guard lock(deletionMutex);
// Unconditional: the caller has just wait-idled, so no in-flight GPU work
// can still reference any queued resource.
for (PendingDeletion& entry : deletionQueue) {
vkDestroyBuffer(device, entry.buffer, nullptr);
vkFreeMemory(device, entry.memory, nullptr);
for (const PendingDeletion& entry : deletionQueue) {
DestroyPending(entry);
}
deletionQueue.clear();
++drains;
}

View file

@ -14,13 +14,7 @@ module;
#include <cstring>
#endif
#ifdef CRAFTER_GRAPHICS_WINDOW_WIN32
#define COBJMACROS
#define WIN32_LEAN_AND_MEAN
#define NOMINMAX
#include <windows.h>
#include <windows.gaming.input.h>
#include <roapi.h>
#include <initguid.h>
#include "Crafter.Graphics-GamepadWin32.h"
#endif
module Crafter.Graphics;
import std;
@ -407,37 +401,15 @@ void Crafter::Gamepad::Rumble(Device& dev, float low, float high,
#endif
// ────────────────────────────────────────────────────────────────────
// Win32 backend (Windows.Gaming.Input via C ABI)
// Win32 backend (Windows.Gaming.Input, through Crafter.Graphics-GamepadWin32.c)
// ────────────────────────────────────────────────────────────────────
#ifdef CRAFTER_GRAPHICS_WINDOW_WIN32
namespace {
using IGamepad = __x_ABI_CWindows_CGaming_CInput_CIGamepad;
using IGamepadStatics = __x_ABI_CWindows_CGaming_CInput_CIGamepadStatics;
using GamepadAddedHandler = __FIEventHandler_1_Windows__CGaming__CInput__CGamepad;
using GamepadRemovedHandler = __FIEventHandler_1_Windows__CGaming__CInput__CGamepad;
using GamepadReading = __x_ABI_CWindows_CGaming_CInput_CGamepadReading;
using GamepadVibration = __x_ABI_CWindows_CGaming_CInput_CGamepadVibration;
using GamepadButtons = __x_ABI_CWindows_CGaming_CInput_CGamepadButtons;
// Maps a connected gamepad to its Crafter::Gamepad::Device. The pointer
// is stable for as long as the gamepad stays connected.
std::unordered_map<CrafterWgiGamepad*, Crafter::Gamepad::Device*> g_byGamepad;
IGamepadStatics* g_statics = nullptr;
EventRegistrationToken g_addedToken {};
EventRegistrationToken g_removedToken {};
bool g_initialized = false;
// Cross-thread queue. WGI fires Added/Removed on a thread pool; the
// event loop drains this on the main thread inside Tick().
struct QueueEntry {
bool added;
IGamepad* gamepad; // AddRef'd
};
std::mutex g_queueMutex;
std::vector<QueueEntry> g_queue;
// Maps a live IGamepad* to its Crafter::Gamepad::Device. Pointer is
// stable from WGI's perspective for the lifetime of the connection.
std::unordered_map<IGamepad*, Crafter::Gamepad::Device*> g_byGamepad;
std::uint32_t PointerToId(IGamepad* g) {
std::uint32_t PointerToId(CrafterWgiGamepad* g) {
// Hash the pointer for a stable-while-connected id. (NonRoamableId
// would be more durable across reconnects but adds another COM
// round-trip; pointer hash is fine for v1.)
@ -449,86 +421,29 @@ namespace {
return out == 0 ? 1 : out;
}
// ── Delegate vtable for GamepadAdded / GamepadRemoved ───────────
struct AddedDelegate {
// The first member matches the C ABI vtable layout. Cast yields
// a valid GamepadAddedHandler*.
void* vtbl;
LONG refCount;
bool isAdded;
};
extern GamepadAddedHandler::__GamepadAddedHandlerVtbl g_addedVtbl;
extern GamepadAddedHandler::__GamepadAddedHandlerVtbl g_removedVtbl;
HRESULT STDMETHODCALLTYPE DelegateQI(GamepadAddedHandler* This, REFIID riid, void** out) {
if (IsEqualIID(riid, &IID_IUnknown) || IsEqualIID(riid, &IID_IAgileObject) ||
IsEqualIID(riid, &IID___FIEventHandler_1_Windows__CGaming__CInput__CGamepad))
{
*out = This;
This->lpVtbl->AddRef(This);
return S_OK;
}
*out = nullptr;
return E_NOINTERFACE;
}
ULONG STDMETHODCALLTYPE DelegateAddRef(GamepadAddedHandler* This) {
AddedDelegate* d = reinterpret_cast<AddedDelegate*>(This);
return (ULONG)InterlockedIncrement(&d->refCount);
}
ULONG STDMETHODCALLTYPE DelegateRelease(GamepadAddedHandler* This) {
AddedDelegate* d = reinterpret_cast<AddedDelegate*>(This);
LONG r = InterlockedDecrement(&d->refCount);
return (ULONG)r;
}
HRESULT STDMETHODCALLTYPE DelegateInvoke(GamepadAddedHandler* This, IInspectable* sender, IGamepad* gamepad) {
AddedDelegate* d = reinterpret_cast<AddedDelegate*>(This);
if (gamepad) IGamepad_AddRef(gamepad);
{
std::lock_guard<std::mutex> lock(g_queueMutex);
g_queue.push_back({ d->isAdded, gamepad });
}
return S_OK;
}
GamepadAddedHandler::__GamepadAddedHandlerVtbl g_addedVtbl = {
DelegateQI, DelegateAddRef, DelegateRelease, DelegateInvoke
};
GamepadAddedHandler::__GamepadAddedHandlerVtbl g_removedVtbl = {
DelegateQI, DelegateAddRef, DelegateRelease, DelegateInvoke
};
AddedDelegate g_addedDelegate { &g_addedVtbl, 1, true };
AddedDelegate g_removedDelegate { &g_removedVtbl, 1, false };
// Translate GamepadReading.Buttons bitfield to our Button enum.
bool ReadingHas(const GamepadReading& r, GamepadButtons b) {
return (r.Buttons & b) != 0;
}
void ApplyReading(Crafter::Gamepad::Device* dev, const GamepadReading& r) {
void ApplyReading(Crafter::Gamepad::Device* dev, const CrafterWgiReading& r) {
using B = Crafter::Gamepad::Button;
using A = Crafter::Gamepad::Axis;
struct Map { B which; GamepadButtons mask; };
struct Map { B which; std::uint32_t mask; };
static const Map buttonMap[] = {
{ B::DPadUp, GamepadButtons_DPadUp },
{ B::DPadDown, GamepadButtons_DPadDown },
{ B::DPadLeft, GamepadButtons_DPadLeft },
{ B::DPadRight, GamepadButtons_DPadRight },
{ B::Start, GamepadButtons_Menu },
{ B::Select, GamepadButtons_View },
{ B::LeftStickClick, GamepadButtons_LeftThumbstick },
{ B::RightStickClick,GamepadButtons_RightThumbstick },
{ B::LeftBumper, GamepadButtons_LeftShoulder },
{ B::RightBumper, GamepadButtons_RightShoulder },
{ B::South, GamepadButtons_A },
{ B::East, GamepadButtons_B },
{ B::West, GamepadButtons_X },
{ B::North, GamepadButtons_Y },
{ B::DPadUp, CRAFTER_WGI_BUTTON_DPAD_UP },
{ B::DPadDown, CRAFTER_WGI_BUTTON_DPAD_DOWN },
{ B::DPadLeft, CRAFTER_WGI_BUTTON_DPAD_LEFT },
{ B::DPadRight, CRAFTER_WGI_BUTTON_DPAD_RIGHT },
{ B::Start, CRAFTER_WGI_BUTTON_MENU },
{ B::Select, CRAFTER_WGI_BUTTON_VIEW },
{ B::LeftStickClick, CRAFTER_WGI_BUTTON_LEFT_THUMBSTICK },
{ B::RightStickClick,CRAFTER_WGI_BUTTON_RIGHT_THUMBSTICK },
{ B::LeftBumper, CRAFTER_WGI_BUTTON_LEFT_SHOULDER },
{ B::RightBumper, CRAFTER_WGI_BUTTON_RIGHT_SHOULDER },
{ B::South, CRAFTER_WGI_BUTTON_A },
{ B::East, CRAFTER_WGI_BUTTON_B },
{ B::West, CRAFTER_WGI_BUTTON_X },
{ B::North, CRAFTER_WGI_BUTTON_Y },
};
for (const Map& m : buttonMap) {
bool now = ReadingHas(r, m.mask);
bool now = (r.buttons & m.mask) != 0;
bool& cur = dev->buttons[(std::size_t)m.which];
if (cur == now) continue;
cur = now;
@ -545,12 +460,12 @@ namespace {
cur = v;
dev->onAxisChanged.Invoke(which);
};
axis(A::LeftStickX, (float)r.LeftThumbstickX);
axis(A::LeftStickY, (float)r.LeftThumbstickY);
axis(A::RightStickX, (float)r.RightThumbstickX);
axis(A::RightStickY, (float)r.RightThumbstickY);
axis(A::LeftTrigger, (float)r.LeftTrigger);
axis(A::RightTrigger, (float)r.RightTrigger);
axis(A::LeftStickX, (float)r.leftStickX);
axis(A::LeftStickY, (float)r.leftStickY);
axis(A::RightStickX, (float)r.rightStickX);
axis(A::RightStickY, (float)r.rightStickY);
axis(A::LeftTrigger, (float)r.leftTrigger);
axis(A::RightTrigger, (float)r.rightTrigger);
// Triggers also expose digital "pressed" buttons at a threshold.
constexpr float kTriggerThreshold = 0.1f;
@ -562,81 +477,52 @@ namespace {
if (now) dev->onButtonDown.Invoke(which);
else dev->onButtonUp .Invoke(which);
};
trigBtn(B::LeftTrigger, r.LeftTrigger);
trigBtn(B::RightTrigger, r.RightTrigger);
}
void EnsureInit() {
if (g_initialized) return;
g_initialized = true;
// RoInitialize is required once per thread; ignore RPC_E_CHANGED_MODE
// (some hosts initialize as STA, that's fine for WGI).
RoInitialize(RO_INIT_MULTITHREADED);
HSTRING_HEADER header;
HSTRING className;
const wchar_t* name = RuntimeClass_Windows_Gaming_Input_Gamepad;
if (FAILED(WindowsCreateStringReference(name,
(UINT32)wcslen(name), &header, &className))) return;
if (FAILED(RoGetActivationFactory(className,
&IID___x_ABI_CWindows_CGaming_CInput_CIGamepadStatics,
(void**)&g_statics))) return;
__x_ABI_CWindows_CGaming_CInput_CIGamepadStatics_add_GamepadAdded(
g_statics,
reinterpret_cast<GamepadAddedHandler*>(&g_addedDelegate),
&g_addedToken);
__x_ABI_CWindows_CGaming_CInput_CIGamepadStatics_add_GamepadRemoved(
g_statics,
reinterpret_cast<GamepadAddedHandler*>(&g_removedDelegate),
&g_removedToken);
trigBtn(B::LeftTrigger, r.leftTrigger);
trigBtn(B::RightTrigger, r.rightTrigger);
}
}
void Crafter::Gamepad::Tick() {
EnsureInit();
if (!g_statics) return;
if (!CrafterWgiStart()) return;
// Drain the cross-thread connect/disconnect queue.
std::vector<QueueEntry> pending;
{
std::lock_guard<std::mutex> lock(g_queueMutex);
pending.swap(g_queue);
}
for (QueueEntry& e : pending) {
if (!e.gamepad) continue;
if (e.added) {
if (g_byGamepad.find(e.gamepad) != g_byGamepad.end()) {
IGamepad_Release(e.gamepad);
continue;
}
auto dev = std::make_unique<Crafter::Gamepad::Device>();
dev->id = PointerToId(e.gamepad);
dev->name = "Gamepad";
auto* raw = dev.get();
g_byGamepad[e.gamepad] = raw;
connected.push_back(std::move(dev));
onConnected.Invoke(raw);
// keep AddRef'd; released on disconnect
} else {
auto it = g_byGamepad.find(e.gamepad);
if (it != g_byGamepad.end()) {
Crafter::Gamepad::Device* raw = it->second;
onDisconnected.Invoke(raw);
for (auto vi = connected.begin(); vi != connected.end(); ++vi) {
if (vi->get() == raw) { connected.erase(vi); break; }
// The connects and disconnects since the last tick.
std::array<CrafterWgiEvent, 16> events;
for (std::size_t count; (count = CrafterWgiTakeEvents(events.data(), events.size())) != 0;) {
for (const CrafterWgiEvent& e : std::span(events.data(), count)) {
if (e.added) {
if (g_byGamepad.find(e.gamepad) != g_byGamepad.end()) {
CrafterWgiRelease(e.gamepad);
continue;
}
g_byGamepad.erase(it);
auto dev = std::make_unique<Crafter::Gamepad::Device>();
dev->id = PointerToId(e.gamepad);
dev->name = "Gamepad";
auto* raw = dev.get();
g_byGamepad[e.gamepad] = raw;
connected.push_back(std::move(dev));
onConnected.Invoke(raw);
// The event's reference is kept until the disconnect.
} else {
auto it = g_byGamepad.find(e.gamepad);
if (it != g_byGamepad.end()) {
Crafter::Gamepad::Device* raw = it->second;
onDisconnected.Invoke(raw);
for (auto vi = connected.begin(); vi != connected.end(); ++vi) {
if (vi->get() == raw) { connected.erase(vi); break; }
}
// The connect's reference, then the disconnect's.
CrafterWgiRelease(it->first);
g_byGamepad.erase(it);
}
CrafterWgiRelease(e.gamepad);
}
IGamepad_Release(e.gamepad);
}
}
// Poll each connected gamepad.
for (auto& [gp, dev] : g_byGamepad) {
GamepadReading reading{};
if (FAILED(__x_ABI_CWindows_CGaming_CInput_CIGamepad_GetCurrentReading(gp, &reading))) continue;
CrafterWgiReading reading {};
if (!CrafterWgiRead(gp, &reading)) continue;
ApplyReading(dev, reading);
}
}
@ -646,16 +532,13 @@ void Crafter::Gamepad::Rumble(Device& dev, float low, float high,
{
// WGI vibration is "set and forget" — no duration. Caller can stop
// by re-calling with zero magnitudes. We honor 0 duration as stop.
IGamepad* gp = nullptr;
CrafterWgiGamepad* gp = nullptr;
for (auto& [g, d] : g_byGamepad) {
if (d == &dev) { gp = g; break; }
}
if (!gp) return;
auto clamp01 = [](float v) { return v < 0 ? 0.0f : v > 1.0f ? 1.0f : v; };
GamepadVibration vib{};
vib.LeftMotor = clamp01(low);
vib.RightMotor = clamp01(high);
__x_ABI_CWindows_CGaming_CInput_CIGamepad_put_Vibration(gp, vib);
CrafterWgiVibrate(gp, clamp01(low), clamp01(high));
}
#endif

View file

@ -0,0 +1,153 @@
//SPDX-License-Identifier: LGPL-3.0-only
//SPDX-FileCopyrightText: Copyright (C) 2026 Catcrafts®
// Windows.Gaming.Input for the Win32 gamepad backend; see
// Crafter.Graphics-GamepadWin32.h for why this part is C.
#define COBJMACROS
#define WIN32_LEAN_AND_MEAN
#define NOMINMAX
#include <windows.h>
#include <roapi.h>
#include <winstring.h>
#include <wchar.h>
#include <stdlib.h>
// Defines the interface IDs the headers below declare, in this file.
#include <initguid.h>
#include <windows.gaming.input.h>
#include "Crafter.Graphics-GamepadWin32.h"
typedef __x_ABI_CWindows_CGaming_CInput_CIGamepad IGamepad;
typedef __x_ABI_CWindows_CGaming_CInput_CIGamepadStatics IGamepadStatics;
typedef __FIEventHandler_1_Windows__CGaming__CInput__CGamepad GamepadHandler;
typedef __FIEventHandler_1_Windows__CGaming__CInput__CGamepadVtbl GamepadHandlerVtbl;
_Static_assert(CRAFTER_WGI_BUTTON_MENU == GamepadButtons_Menu && CRAFTER_WGI_BUTTON_RIGHT_THUMBSTICK == GamepadButtons_RightThumbstick
&& CRAFTER_WGI_BUTTON_DPAD_UP == GamepadButtons_DPadUp && CRAFTER_WGI_BUTTON_A == GamepadButtons_A,
"the CRAFTER_WGI_BUTTON_ values are GamepadButtons'");
static IGamepadStatics* statics = NULL;
static EventRegistrationToken addedToken;
static EventRegistrationToken removedToken;
static int started = 0;
// The connects and disconnects, which Windows.Gaming.Input reports on its
// own threads, until the event loop takes them.
static SRWLOCK queueLock = SRWLOCK_INIT;
static CrafterWgiEvent* queue = NULL;
static size_t queueCount = 0;
static size_t queueCapacity = 0;
// The GamepadAdded and GamepadRemoved handlers. Static objects, so their
// reference counts don't decide their lifetime.
typedef struct Handler {
const GamepadHandlerVtbl* lpVtbl;
int added;
} Handler;
static HRESULT STDMETHODCALLTYPE HandlerQueryInterface(GamepadHandler* This, REFIID riid, void** out) {
if (IsEqualIID(riid, &IID_IUnknown) || IsEqualIID(riid, &IID_IAgileObject)
|| IsEqualIID(riid, &IID___FIEventHandler_1_Windows__CGaming__CInput__CGamepad)) {
*out = This;
return S_OK;
}
*out = NULL;
return E_NOINTERFACE;
}
static ULONG STDMETHODCALLTYPE HandlerAddRef(GamepadHandler* This) {
(void)This;
return 2;
}
static ULONG STDMETHODCALLTYPE HandlerRelease(GamepadHandler* This) {
(void)This;
return 1;
}
static HRESULT STDMETHODCALLTYPE HandlerInvoke(GamepadHandler* This, IInspectable* sender, IGamepad* gamepad) {
(void)sender;
if (gamepad == NULL) return S_OK;
const Handler* handler = (const Handler*)This;
__x_ABI_CWindows_CGaming_CInput_CIGamepad_AddRef(gamepad);
AcquireSRWLockExclusive(&queueLock);
if (queueCount == queueCapacity) {
size_t capacity = queueCapacity == 0 ? 8 : queueCapacity * 2;
CrafterWgiEvent* grown = (CrafterWgiEvent*)realloc(queue, capacity * sizeof(CrafterWgiEvent));
if (grown == NULL) {
ReleaseSRWLockExclusive(&queueLock);
__x_ABI_CWindows_CGaming_CInput_CIGamepad_Release(gamepad);
return E_OUTOFMEMORY;
}
queue = grown;
queueCapacity = capacity;
}
queue[queueCount].added = handler->added;
queue[queueCount].gamepad = (CrafterWgiGamepad*)gamepad;
++queueCount;
ReleaseSRWLockExclusive(&queueLock);
return S_OK;
}
static const GamepadHandlerVtbl handlerVtbl = {
HandlerQueryInterface,
HandlerAddRef,
HandlerRelease,
HandlerInvoke,
};
static Handler addedHandler = { &handlerVtbl, 1 };
static Handler removedHandler = { &handlerVtbl, 0 };
int CrafterWgiStart(void) {
if (started) return statics != NULL;
started = 1;
// Once per thread. RPC_E_CHANGED_MODE (the host made it a single-threaded
// apartment) is fine for Windows.Gaming.Input.
RoInitialize(RO_INIT_MULTITHREADED);
HSTRING_HEADER header;
HSTRING className;
const wchar_t* name = RuntimeClass_Windows_Gaming_Input_Gamepad;
if (FAILED(WindowsCreateStringReference(name, (UINT32)wcslen(name), &header, &className))) return 0;
if (FAILED(RoGetActivationFactory(className, &IID___x_ABI_CWindows_CGaming_CInput_CIGamepadStatics, (void**)&statics))) {
statics = NULL;
return 0;
}
__x_ABI_CWindows_CGaming_CInput_CIGamepadStatics_add_GamepadAdded(statics, (GamepadHandler*)&addedHandler, &addedToken);
__x_ABI_CWindows_CGaming_CInput_CIGamepadStatics_add_GamepadRemoved(statics, (GamepadHandler*)&removedHandler, &removedToken);
return 1;
}
size_t CrafterWgiTakeEvents(CrafterWgiEvent* out, size_t capacity) {
AcquireSRWLockExclusive(&queueLock);
size_t taken = queueCount < capacity ? queueCount : capacity;
for (size_t i = 0; i < taken; ++i) out[i] = queue[i];
for (size_t i = taken; i < queueCount; ++i) queue[i - taken] = queue[i];
queueCount -= taken;
ReleaseSRWLockExclusive(&queueLock);
return taken;
}
int CrafterWgiRead(CrafterWgiGamepad* gamepad, CrafterWgiReading* out) {
__x_ABI_CWindows_CGaming_CInput_CGamepadReading reading;
if (FAILED(__x_ABI_CWindows_CGaming_CInput_CIGamepad_GetCurrentReading((IGamepad*)gamepad, &reading))) return 0;
out->buttons = (uint32_t)reading.Buttons;
out->leftTrigger = reading.LeftTrigger;
out->rightTrigger = reading.RightTrigger;
out->leftStickX = reading.LeftThumbstickX;
out->leftStickY = reading.LeftThumbstickY;
out->rightStickX = reading.RightThumbstickX;
out->rightStickY = reading.RightThumbstickY;
return 1;
}
void CrafterWgiVibrate(CrafterWgiGamepad* gamepad, double low, double high) {
__x_ABI_CWindows_CGaming_CInput_CGamepadVibration vibration = { 0 };
vibration.LeftMotor = low;
vibration.RightMotor = high;
__x_ABI_CWindows_CGaming_CInput_CIGamepad_put_Vibration((IGamepad*)gamepad, vibration);
}
void CrafterWgiRelease(CrafterWgiGamepad* gamepad) {
__x_ABI_CWindows_CGaming_CInput_CIGamepad_Release((IGamepad*)gamepad);
}

View file

@ -0,0 +1,74 @@
//SPDX-License-Identifier: LGPL-3.0-only
//SPDX-FileCopyrightText: Copyright (C) 2026 Catcrafts®
// The C side of the Win32 gamepad backend (Crafter.Graphics-GamepadWin32.c):
// Windows.Gaming.Input, through its C interface. mingw-w64's generated WinRT
// headers declare that interface only to C; in C++ they declare the ABI
// templates instead, and mingw-w64 14's windows.foundation.h doesn't compile
// as C++ (IReference<boolean> and IReference<BYTE> are one type). Gamepad.cpp
// drives it from the event loop's thread; only the connect and disconnect
// notifications arrive on another, and they just queue.
#ifndef CRAFTER_GRAPHICS_GAMEPAD_WIN32_H
#define CRAFTER_GRAPHICS_GAMEPAD_WIN32_H
#include <stddef.h>
#include <stdint.h>
#ifdef __cplusplus
extern "C" {
#endif
// A connected gamepad: Windows.Gaming.Input's IGamepad.
typedef struct CrafterWgiGamepad CrafterWgiGamepad;
typedef struct CrafterWgiEvent {
int added; // nonzero for a connect, zero for a disconnect
CrafterWgiGamepad* gamepad; // holds a reference, which CrafterWgiRelease drops
} CrafterWgiEvent;
// A GamepadReading. `buttons` holds GamepadButtons bits, named below.
typedef struct CrafterWgiReading {
uint32_t buttons;
double leftTrigger;
double rightTrigger;
double leftStickX;
double leftStickY;
double rightStickX;
double rightStickY;
} CrafterWgiReading;
enum {
CRAFTER_WGI_BUTTON_MENU = 0x1,
CRAFTER_WGI_BUTTON_VIEW = 0x2,
CRAFTER_WGI_BUTTON_A = 0x4,
CRAFTER_WGI_BUTTON_B = 0x8,
CRAFTER_WGI_BUTTON_X = 0x10,
CRAFTER_WGI_BUTTON_Y = 0x20,
CRAFTER_WGI_BUTTON_DPAD_UP = 0x40,
CRAFTER_WGI_BUTTON_DPAD_DOWN = 0x80,
CRAFTER_WGI_BUTTON_DPAD_LEFT = 0x100,
CRAFTER_WGI_BUTTON_DPAD_RIGHT = 0x200,
CRAFTER_WGI_BUTTON_LEFT_SHOULDER = 0x400,
CRAFTER_WGI_BUTTON_RIGHT_SHOULDER = 0x800,
CRAFTER_WGI_BUTTON_LEFT_THUMBSTICK = 0x1000,
CRAFTER_WGI_BUTTON_RIGHT_THUMBSTICK = 0x2000,
};
// Starts listening for gamepads the first time it is called. Nonzero when
// Windows.Gaming.Input is available, every time.
int CrafterWgiStart(void);
// Moves up to `capacity` of the connects and disconnects that came in since
// the last call into `out`, oldest first, and returns how many it moved.
size_t CrafterWgiTakeEvents(CrafterWgiEvent* out, size_t capacity);
// The gamepad's current state; nonzero on success.
int CrafterWgiRead(CrafterWgiGamepad* gamepad, CrafterWgiReading* out);
// Sets the two rumble motors, each 0..1, until set again.
void CrafterWgiVibrate(CrafterWgiGamepad* gamepad, double low, double high);
// Drops the reference an event handed over.
void CrafterWgiRelease(CrafterWgiGamepad* gamepad);
#ifdef __cplusplus
}
#endif
#endif

View file

@ -29,6 +29,20 @@ namespace {
constexpr VkBufferUsageFlags2 kIndexUsageBase =
kVertexUsageBase | VK_BUFFER_USAGE_STORAGE_BUFFER_BIT;
// Where the geometry buffers are read: the BLAS build, and hit shaders or
// compute ray queries fetching vertices by address. An upload is ordered
// before these reads, and a re-upload after the earlier ones, which frames
// still in flight may be doing. A build reads its inputs as a shader read.
constexpr VkPipelineStageFlags kGeometryReaders =
VK_PIPELINE_STAGE_ACCELERATION_STRUCTURE_BUILD_BIT_KHR
| VK_PIPELINE_STAGE_RAY_TRACING_SHADER_BIT_KHR
| VK_PIPELINE_STAGE_COMPUTE_SHADER_BIT;
// The same, for synchronization2's barriers.
constexpr VkPipelineStageFlags2 kGeometryReaders2 =
VK_PIPELINE_STAGE_2_ACCELERATION_STRUCTURE_BUILD_BIT_KHR
| VK_PIPELINE_STAGE_2_RAY_TRACING_SHADER_BIT_KHR
| VK_PIPELINE_STAGE_2_COMPUTE_SHADER_BIT;
// Translate the portable RTBuildOptions to Vulkan build flags. The two
// preferences are mutually exclusive; ALLOW_UPDATE is layered on top so
// a later in-place refit (UPDATE mode) is permitted.
@ -42,6 +56,15 @@ namespace {
return flags;
}
// The geometry's flags for a build: opaque or not, and whether any-hit
// may run more than once per primitive.
VkGeometryFlagsKHR GeometryFlags(bool opaque, const RTBuildOptions& options) {
VkGeometryFlagsKHR flags = opaque ? static_cast<VkGeometryFlagsKHR>(VK_GEOMETRY_OPAQUE_BIT_KHR) : VkGeometryFlagsKHR{};
if (options.noDuplicateAnyHit)
flags |= VK_GEOMETRY_NO_DUPLICATE_ANY_HIT_INVOCATION_BIT_KHR;
return flags;
}
// Shared BLAS sizing / creation / build-record tail — geometry-type
// agnostic; both the triangle and the AABB (procedural) paths feed
// their single geometry through here. `update` selects an in-place
@ -61,11 +84,38 @@ namespace {
.pGeometries = &blasGeometry,
};
// A refit writes the BLAS in place, and a rebuild writes the same
// storage and scratch whenever the new BLAS fits. Earlier work, in
// this command buffer or in a frame still in flight, may read the BLAS
// (a TLAS build, a trace, a compute ray query) or write the scratch (a
// build), so order that before this build. A first build touches
// nothing earlier.
if (self.accelerationStructure != VK_NULL_HANDLE) {
VkMemoryBarrier asBarrier {
.sType = VK_STRUCTURE_TYPE_MEMORY_BARRIER,
.srcAccessMask = VK_ACCESS_ACCELERATION_STRUCTURE_READ_BIT_KHR | VK_ACCESS_ACCELERATION_STRUCTURE_WRITE_BIT_KHR,
.dstAccessMask = VK_ACCESS_ACCELERATION_STRUCTURE_READ_BIT_KHR | VK_ACCESS_ACCELERATION_STRUCTURE_WRITE_BIT_KHR
};
vkCmdPipelineBarrier(cmd,
VK_PIPELINE_STAGE_ACCELERATION_STRUCTURE_BUILD_BIT_KHR | VK_PIPELINE_STAGE_RAY_TRACING_SHADER_BIT_KHR | VK_PIPELINE_STAGE_COMPUTE_SHADER_BIT,
VK_PIPELINE_STAGE_ACCELERATION_STRUCTURE_BUILD_BIT_KHR,
0, 1, &asBarrier, 0, nullptr, 0, nullptr);
}
if (!update) {
// Fresh build: query sizes, (re)allocate scratch + AS storage,
// create a new AS handle. The scratch buffer is sized for the
// build (always >= the update scratch requirement), so it also
// covers later in-place refits without resizing.
//
// On a rebuild the old handle may still be referenced by this
// command buffer and by frames in flight, so it goes to the
// deletion queue instead of being destroyed here. It is queued
// before the Resizes below, which queue the old storage when the
// new BLAS doesn't fit, so it is freed before the buffer it lives in.
Device::EnqueueDeletion(self.accelerationStructure);
self.accelerationStructure = VK_NULL_HANDLE;
VkAccelerationStructureBuildSizesInfoKHR blasBuildSizes = {
.sType = VK_STRUCTURE_TYPE_ACCELERATION_STRUCTURE_BUILD_SIZES_INFO_KHR
};
@ -89,13 +139,6 @@ namespace {
VK_MEMORY_PROPERTY_DEVICE_LOCAL_BIT,
blasBuildSizes.accelerationStructureSize);
// Re-Build of an existing Mesh: destroy the stale handle first
// so the previous AS storage isn't leaked.
if (self.accelerationStructure != VK_NULL_HANDLE) {
Device::vkDestroyAccelerationStructureKHR(Device::device, self.accelerationStructure, nullptr);
self.accelerationStructure = VK_NULL_HANDLE;
}
VkAccelerationStructureCreateInfoKHR blasCreateInfo{
.sType = VK_STRUCTURE_TYPE_ACCELERATION_STRUCTURE_CREATE_INFO_KHR,
.buffer = self.blasBuffer.buffer,
@ -110,19 +153,6 @@ namespace {
.accelerationStructure = self.accelerationStructure
};
self.blasAddr = Device::vkGetAccelerationStructureDeviceAddressKHR(Device::device, &addrInfo);
} else {
// In-place refit: the AS may have been read by a prior TLAS
// build / trace, so order that read before the build overwrites
// it. Scratch reuse needs the same write-after-read guard.
VkMemoryBarrier asBarrier {
.sType = VK_STRUCTURE_TYPE_MEMORY_BARRIER,
.srcAccessMask = VK_ACCESS_ACCELERATION_STRUCTURE_READ_BIT_KHR | VK_ACCESS_ACCELERATION_STRUCTURE_WRITE_BIT_KHR,
.dstAccessMask = VK_ACCESS_ACCELERATION_STRUCTURE_READ_BIT_KHR | VK_ACCESS_ACCELERATION_STRUCTURE_WRITE_BIT_KHR
};
vkCmdPipelineBarrier(cmd,
VK_PIPELINE_STAGE_ACCELERATION_STRUCTURE_BUILD_BIT_KHR | VK_PIPELINE_STAGE_RAY_TRACING_SHADER_BIT_KHR,
VK_PIPELINE_STAGE_ACCELERATION_STRUCTURE_BUILD_BIT_KHR,
0, 1, &asBarrier, 0, nullptr, 0, nullptr);
}
blasBuildGeometryInfo.scratchData.deviceAddress = self.scratchBuffer.address;
@ -183,13 +213,56 @@ namespace {
.sType = VK_STRUCTURE_TYPE_ACCELERATION_STRUCTURE_GEOMETRY_KHR,
.geometryType = VK_GEOMETRY_TYPE_TRIANGLES_KHR,
.geometry = geometryData,
.flags = VK_GEOMETRY_OPAQUE_BIT_KHR
.flags = self.geometryFlags
};
RecordBLASBuildFromGeometry(self, blasGeometry, indexCount / 3, flags, update, cmd);
}
}
Mesh::Mesh(Mesh&& other)
: scratchBuffer(std::move(other.scratchBuffer))
, blasBuffer(std::move(other.blasBuffer))
, vertexBuffer(std::move(other.vertexBuffer))
, indexBuffer(std::move(other.indexBuffer))
, aabbBuffer(std::move(other.aabbBuffer))
, compressedStaging(std::move(other.compressedStaging))
, accelerationStructure(std::exchange(other.accelerationStructure, VK_NULL_HANDLE))
, blasAddr(std::exchange(other.blasAddr, 0))
, geometryFlags(other.geometryFlags)
, buildFlags(other.buildFlags)
, builtPrimitiveCount(other.builtPrimitiveCount)
, builtInputCount(other.builtInputCount)
, allowUpdate(other.allowUpdate) {
}
Mesh& Mesh::operator=(Mesh&& other) {
if (this == &other) return *this;
// Ahead of blasBuffer, which it lives in and which queues its own as it
// takes other's over.
Device::EnqueueDeletion(accelerationStructure);
scratchBuffer = std::move(other.scratchBuffer);
blasBuffer = std::move(other.blasBuffer);
vertexBuffer = std::move(other.vertexBuffer);
indexBuffer = std::move(other.indexBuffer);
aabbBuffer = std::move(other.aabbBuffer);
compressedStaging = std::move(other.compressedStaging);
accelerationStructure = std::exchange(other.accelerationStructure, VK_NULL_HANDLE);
blasAddr = std::exchange(other.blasAddr, 0);
geometryFlags = other.geometryFlags;
buildFlags = other.buildFlags;
builtPrimitiveCount = other.builtPrimitiveCount;
builtInputCount = other.builtInputCount;
allowUpdate = other.allowUpdate;
return *this;
}
Mesh::~Mesh() {
// The body runs before the members' destructors, so the handle is queued
// ahead of blasBuffer, which it lives in.
Device::EnqueueDeletion(accelerationStructure);
}
void Mesh::Build(std::span<Vector<float, 3, 3>> verticies, std::span<std::uint32_t> indicies, VkCommandBuffer cmd, RTBuildOptions options) {
// Every Build* records an acceleration-structure build; without ray
// tracing those entry points are not loaded. Fail with the reason.
@ -199,10 +272,11 @@ void Mesh::Build(std::span<Vector<float, 3, 3>> verticies, std::span<std::uint32
// upload before the BLAS build reads them. Replaces the previous
// HOST_VISIBLE allocation that the build (and any hit-shader fetch) would
// read over PCIe every frame (#73).
vertexBuffer.UploadDeviceLocal(kVertexUsageBase, verticies.data(), static_cast<std::uint32_t>(verticies.size()), cmd, VK_ACCESS_MEMORY_READ_BIT, VK_PIPELINE_STAGE_ACCELERATION_STRUCTURE_BUILD_BIT_KHR);
indexBuffer.UploadDeviceLocal(kIndexUsageBase, indicies.data(), static_cast<std::uint32_t>(indicies.size()), cmd, VK_ACCESS_MEMORY_READ_BIT, VK_PIPELINE_STAGE_ACCELERATION_STRUCTURE_BUILD_BIT_KHR);
vertexBuffer.UploadDeviceLocal(kVertexUsageBase, verticies.data(), static_cast<std::uint32_t>(verticies.size()), cmd, VK_ACCESS_SHADER_READ_BIT, kGeometryReaders);
indexBuffer.UploadDeviceLocal(kIndexUsageBase, indicies.data(), static_cast<std::uint32_t>(indicies.size()), cmd, VK_ACCESS_SHADER_READ_BIT, kGeometryReaders);
allowUpdate = options.allowUpdate;
geometryFlags = GeometryFlags(/*opaque*/ true, options);
builtInputCount = static_cast<std::uint32_t>(verticies.size());
RecordBLASBuild(*this, static_cast<std::uint32_t>(verticies.size()), static_cast<std::uint32_t>(indicies.size()), BlasFlags(options), /*update*/ false, cmd);
}
@ -231,9 +305,12 @@ void Mesh::Build(const CompressedMeshAsset& asset, VkCommandBuffer cmd, RTBuildO
// The GPU decompressor writes vertex/index directly (the build input is
// never host-written on this path), so they want pure DEVICE_LOCAL — no
// host visibility, no map. This is where the BLAS build and any hit-shader
// fetch then read them from (#73).
// fetch then read them from (#73). A ranged Refit copies the moved
// vertices in, hence TRANSFER_DST.
const VkBuffer previousVertices = vertexBuffer.buffer;
const VkBuffer previousIndices = indexBuffer.buffer;
vertexBuffer.Resize(
kVertexUsageBase | VK_BUFFER_USAGE_2_MEMORY_DECOMPRESSION_BIT_EXT,
kVertexUsageBase | VK_BUFFER_USAGE_TRANSFER_DST_BIT | VK_BUFFER_USAGE_2_MEMORY_DECOMPRESSION_BIT_EXT,
VK_MEMORY_PROPERTY_DEVICE_LOCAL_BIT,
asset.vertexCount);
indexBuffer.Resize(
@ -269,11 +346,33 @@ void Mesh::Build(const CompressedMeshAsset& asset, VkCommandBuffer cmd, RTBuildO
regions);
}
// A rebuild that keeps its buffers (Resize keeps an allocation that fits)
// decompresses into what frames in flight may still be reading: the
// decompress waits for those reads. Fresh buffers nothing reads yet.
if ((vertexBuffer.buffer != VK_NULL_HANDLE && vertexBuffer.buffer == previousVertices)
|| (indexBuffer.buffer != VK_NULL_HANDLE && indexBuffer.buffer == previousIndices)) {
const VkMemoryBarrier2 readsDone {
.sType = VK_STRUCTURE_TYPE_MEMORY_BARRIER_2,
.srcStageMask = kGeometryReaders2,
.srcAccessMask = 0,
.dstStageMask = VK_PIPELINE_STAGE_2_MEMORY_DECOMPRESSION_BIT_EXT,
.dstAccessMask = VK_ACCESS_2_MEMORY_DECOMPRESSION_WRITE_BIT_EXT,
};
const VkDependencyInfo beforeDecompress {
.sType = VK_STRUCTURE_TYPE_DEPENDENCY_INFO,
.memoryBarrierCount = 1,
.pMemoryBarriers = &readsDone,
};
vkCmdPipelineBarrier2(cmd, &beforeDecompress);
}
// Then the BLAS build reads the decompressed geometry as a shader read,
// as do the hit shaders and ray queries that fetch it by address.
Decompress::DecompressOnGPU(
cmd,
regions,
VK_PIPELINE_STAGE_2_ACCELERATION_STRUCTURE_BUILD_BIT_KHR,
VK_ACCESS_2_ACCELERATION_STRUCTURE_READ_BIT_KHR);
kGeometryReaders2,
VK_ACCESS_2_SHADER_READ_BIT);
// The compressed staging is only read by the decompress recorded above; the
// subsequent BLAS build reads the decompressed vertex/index buffers, never
@ -286,6 +385,7 @@ void Mesh::Build(const CompressedMeshAsset& asset, VkCommandBuffer cmd, RTBuildO
compressedStaging.DeferredClear();
allowUpdate = options.allowUpdate;
geometryFlags = GeometryFlags(/*opaque*/ true, options);
builtInputCount = asset.vertexCount;
RecordBLASBuild(*this, asset.vertexCount, asset.indexCount, BlasFlags(options), /*update*/ false, cmd);
}
@ -296,7 +396,7 @@ namespace {
// Geometry-source agnostic: the host-upload path (RecordProceduralBuild)
// points this at self.aabbBuffer, while the device-buffer overloads
// point it straight at the producer's buffer — no copy involved either
// way. The geometry's opaque bit is read from self.opaque so an UPDATE
// way. The geometry's flags are read from self.geometryFlags so an UPDATE
// keeps the exact geometry description of the original build.
void RecordProceduralBuildFromAddress(Mesh& self, VkDeviceAddress aabbAddress, std::uint32_t count, std::uint32_t stride, VkBuildAccelerationStructureFlagsKHR flags, bool update, VkCommandBuffer cmd) {
VkAccelerationStructureGeometryAabbsDataKHR aabbsData {
@ -312,7 +412,7 @@ namespace {
.geometry = geometryData,
// Non-opaque by default (mirrors the WebGPU path) so any-hit
// shaders run for procedural geometry unless the caller opts out.
.flags = self.opaque ? static_cast<VkGeometryFlagsKHR>(VK_GEOMETRY_OPAQUE_BIT_KHR) : VkGeometryFlagsKHR{}
.flags = self.geometryFlags
};
RecordBLASBuildFromGeometry(self, blasGeometry, count, flags, update, cmd);
@ -330,7 +430,7 @@ namespace {
// re-uploads into the same same-sized buffer: Resize reuses the existing
// allocation (count unchanged), so the device address — and the geometry
// it feeds — stays stable for an in-place UPDATE.
self.aabbBuffer.UploadDeviceLocal(kVertexUsageBase, aabbs.data(), static_cast<std::uint32_t>(aabbs.size()), cmd, VK_ACCESS_MEMORY_READ_BIT, VK_PIPELINE_STAGE_ACCELERATION_STRUCTURE_BUILD_BIT_KHR);
self.aabbBuffer.UploadDeviceLocal(kVertexUsageBase, aabbs.data(), static_cast<std::uint32_t>(aabbs.size()), cmd, VK_ACCESS_SHADER_READ_BIT, kGeometryReaders);
RecordProceduralBuildFromAddress(self, self.aabbBuffer.address, static_cast<std::uint32_t>(aabbs.size()), sizeof(RTAabb), flags, update, cmd);
}
@ -338,7 +438,7 @@ namespace {
void Mesh::BuildProcedural(std::span<const RTAabb> aabbs, bool opaque, VkCommandBuffer cmd, RTBuildOptions options) {
Device::RequireRayTracing();
this->opaque = opaque;
geometryFlags = GeometryFlags(opaque, options);
allowUpdate = options.allowUpdate;
builtInputCount = static_cast<std::uint32_t>(aabbs.size());
@ -353,7 +453,7 @@ void Mesh::BuildProcedural(VkDeviceAddress aabbAddress, std::uint32_t count, boo
// The caller owns that buffer's lifetime + usage flags
// (ACCELERATION_STRUCTURE_BUILD_INPUT_READ_ONLY + SHADER_DEVICE_ADDRESS)
// and must barrier the producing writes before this build reads them.
this->opaque = opaque;
geometryFlags = GeometryFlags(opaque, options);
allowUpdate = options.allowUpdate;
builtInputCount = count;
@ -385,6 +485,7 @@ void Mesh::Refit(std::span<Vector<float, 3, 3>> verticies, std::span<std::uint32
.preference = (buildFlags & VK_BUILD_ACCELERATION_STRUCTURE_PREFER_FAST_BUILD_BIT_KHR)
? RTBuildPreference::FastBuild : RTBuildPreference::FastTrace,
.allowUpdate = allowUpdate,
.noDuplicateAnyHit = (geometryFlags & VK_GEOMETRY_NO_DUPLICATE_ANY_HIT_INVOCATION_BIT_KHR) != 0,
});
return;
}
@ -402,15 +503,14 @@ void Mesh::Refit(std::span<Vector<float, 3, 3>> verticies, std::span<std::uint32
// the rest of the vertex buffer already holds last refit's positions, so a
// ranged upload patches just the vertices that moved (#119). The window is
// clamped to the array — a caller window past the end would otherwise read
// out of bounds / overflow the copy. UploadDeviceLocalRange writes into the
// out of bounds / overflow the copy. UploadDeviceLocalRange copies into the
// existing same-sized allocation (no Resize), so the address the UPDATE
// reads stays stable, then barriers just that sub-range before the build. On
// the staged path this re-stages only the dirty slice (the deforming-mesh
// fallback, #73).
// reads stays stable, after the reads of the frames still in flight and
// before the build, staging only the dirty slice.
std::uint32_t offset = std::min(dirtyVertexOffset, static_cast<std::uint32_t>(verticies.size()));
std::uint32_t count = std::min(dirtyVertexCount, static_cast<std::uint32_t>(verticies.size()) - offset);
if (count != 0) {
vertexBuffer.UploadDeviceLocalRange(verticies.data() + offset, offset, count, cmd, VK_ACCESS_MEMORY_READ_BIT, VK_PIPELINE_STAGE_ACCELERATION_STRUCTURE_BUILD_BIT_KHR);
vertexBuffer.UploadDeviceLocalRange(verticies.data() + offset, offset, count, cmd, VK_ACCESS_SHADER_READ_BIT, kGeometryReaders);
}
RecordBLASBuild(*this, static_cast<std::uint32_t>(verticies.size()), static_cast<std::uint32_t>(indicies.size()), buildFlags, /*update*/ true, cmd);

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,