Crafter.Graphics/interfaces/Crafter.Graphics-Device.cppm

600 lines
38 KiB
Text
Raw Normal View History

2026-07-22 18:09:06 +02:00
//SPDX-License-Identifier: LGPL-3.0-only
//SPDX-FileCopyrightText: Copyright (C) 2026 Catcrafts®
2026-03-09 20:10:19 +01:00
module;
2026-05-18 02:07:48 +02:00
#ifndef CRAFTER_GRAPHICS_WINDOW_DOM
2026-03-09 20:10:19 +01:00
#include "vulkan/vulkan.h"
2026-05-18 02:07:48 +02:00
#endif
2026-03-09 20:10:19 +01:00
#ifdef CRAFTER_GRAPHICS_WINDOW_WAYLAND
#include <wayland-client.h>
#include <wayland-client-protocol.h>
#include "../lib/xdg-shell-client-protocol.h"
#include "../lib/wayland-xdg-decoration-unstable-v1-client-protocol.h"
#include "../lib/fractional-scale-v1.h"
#include "../lib/viewporter.h"
#include "../lib/text-input-unstable-v3-client-protocol.h"
2026-03-09 20:10:19 +01:00
#include <xkbcommon/xkbcommon.h>
#endif
export module Crafter.Graphics:Device;
import std;
2026-05-12 00:24:48 +02:00
import :Keys; // KeyCode for keyboard repeat state
2026-03-09 20:10:19 +01:00
export namespace Crafter {
struct Window;
2026-05-02 00:03:24 +02:00
#ifdef CRAFTER_GRAPHICS_WINDOW_WAYLAND
// Wayland's wl_keyboard.key only fires on real press/release — the
// compositor expects the application to synthesize repeat events
// itself using the rate/delay it advertises via wl_keyboard.repeat_info.
struct KeyRepeatState {
int rate = 25; // chars/sec
int delay = 500; // ms before first repeat
bool active = false;
2026-05-12 00:24:48 +02:00
KeyCode key = 0;
2026-05-02 00:03:24 +02:00
std::string utf8; // UTF-8 to re-emit as onTextInput, if any
std::chrono::time_point<std::chrono::steady_clock> pressTime;
std::chrono::time_point<std::chrono::steady_clock> lastFireTime;
bool fired = false; // a repeat has fired since the press: before it, lastFireTime is the press itself
2026-05-02 00:03:24 +02:00
};
// Repeats one tick may emit at once when frames stalled; the rest of the
// stall is dropped, so a held key is never a burst of edits.
inline constexpr int kKeyRepeatCatchUp = 3;
2026-05-02 00:03:24 +02:00
#endif
feat(device): applications ask for the device features their shaders use Device - Device::Initialize(required, optional) takes DeviceFeatures, the five core feature structs vulkan10 to vulkan14. The library enables only what its own shaders and code use. Device::enabledFeatures says what was enabled, supportedFeatures what the GPU has, apiVersion the version in use. - shaderInt16, shaderFloat16, storageBuffer16BitAccess, scalarBlockLayout and the uniform-buffer, storage-buffer and storage-image non-uniform indexing bits are no longer enabled for everyone. An application whose shaders take uint16_t spec constants passes {.vulkan10 = {.shaderInt16 = VK_TRUE}}. scalarBlockLayout stays on with the NVIDIA workaround, whose TLAS address may sit in a struct's tail padding. - Every GPU is weighed before one is chosen: Vulkan 1.3, the swapchain and, below 1.4, maintenance5 extensions, the library's needs, the required features. One that can't run the application is skipped, saying why when it ranks higher. A CPU device runs it only when nothing else is listed, and CRAFTER_GRAPHICS_DEVICE never falls back. A GPU two drivers both list is tried through each. - When no GPU qualifies, Initialize throws naming each GPU's gaps. It destroys the instance it made, before any Wayland connection, so it can be called again asking for less. - maintenance5 is vulkan14's on a 1.4 GPU and VK_KHR_maintenance5's below. CRAFTER_GRAPHICS_NO_VULKAN_1_4 runs as a 1.3 application, to exercise that path on a 1.4 GPU. - VK_KHR_spirv_1_4 and VK_KHR_shader_float_controls, core since 1.2, are no longer enabled, nor VK_EXT_shader_atomic_float, which was enabled without its feature struct. memoryDecompression's feature bit is queried before it is enabled. Shaders - VulkanShader checks a module's capabilities, declared directly or implicitly, against the enabled core features, the GPU's subgroup operations and the optional groups, and throws naming the field to pass, or saying the GPU lacks it. The tables are generated from vk.xml and SPIR-V's grammar. - WorkaroundNvidiaAS::Patch sized a row-major matrix push member by its columns, so the TLAS address could land inside it. Window - The swapchain format is one whose own feature flags allow storage reads and writes without a format, which since Vulkan 1.3 each format says for itself; shaderStorageImageRead/WriteWithoutFormat are no longer required. Tests - DeviceFeatures: ResolveFeatures, ChooseDevice and CheckShaderCapabilities, without a GPU. - FeatureRequests: required and optional features end to end, the 1.3 path, and a retry after a refusal. - PushConstantRewrite: a row-major mat3x4 push member. - HeapASRead, HeapDynamicIndexHit, RTPassPushData and the ray-tracing examples ask for the features their shaders use. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-31 06:40:02 +01:00
#ifndef CRAFTER_GRAPHICS_WINDOW_DOM
// The core feature structs, one per Vulkan version: every feature promoted
// to core is in exactly one of them, and Vulkan forbids naming a feature
// through both its version's struct and the struct it was promoted from.
// The features an application's own shaders and code use go to
// Device::Initialize in these. Only the VkBool32 members are read, so sType
// and pNext may be left zero, as `{.vulkan12 = {.shaderFloat16 = VK_TRUE}}`
// leaves them. At namespace scope, like KeyRepeatState: a nested type with
// member initializers can't be the type of an inline static inside Device.
struct DeviceFeatures {
VkPhysicalDeviceFeatures vulkan10 {};
VkPhysicalDeviceVulkan11Features vulkan11 { .sType = VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_VULKAN_1_1_FEATURES };
VkPhysicalDeviceVulkan12Features vulkan12 { .sType = VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_VULKAN_1_2_FEATURES };
VkPhysicalDeviceVulkan13Features vulkan13 { .sType = VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_VULKAN_1_3_FEATURES };
// Needs a Vulkan 1.4 GPU. Below 1.4 only maintenance5 can be on, which
// the library enables through VK_KHR_maintenance5 there.
VkPhysicalDeviceVulkan14Features vulkan14 { .sType = VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_VULKAN_1_4_FEATURES };
};
// The features a GPU lacks, named the way an application writes them
// ("vulkan12.shaderFloat16"), split by who needs them.
struct FeatureGaps {
std::vector<std::string> library;
std::vector<std::string> application;
};
// One GPU as Device::ChooseDevice weighs it: no gaps means it can run the
// application.
struct DeviceChoice {
VkPhysicalDeviceType type;
std::string name;
std::vector<std::string> gaps;
};
#endif
2026-03-09 20:10:19 +01:00
struct Device {
feat(device): applications ask for the device features their shaders use Device - Device::Initialize(required, optional) takes DeviceFeatures, the five core feature structs vulkan10 to vulkan14. The library enables only what its own shaders and code use. Device::enabledFeatures says what was enabled, supportedFeatures what the GPU has, apiVersion the version in use. - shaderInt16, shaderFloat16, storageBuffer16BitAccess, scalarBlockLayout and the uniform-buffer, storage-buffer and storage-image non-uniform indexing bits are no longer enabled for everyone. An application whose shaders take uint16_t spec constants passes {.vulkan10 = {.shaderInt16 = VK_TRUE}}. scalarBlockLayout stays on with the NVIDIA workaround, whose TLAS address may sit in a struct's tail padding. - Every GPU is weighed before one is chosen: Vulkan 1.3, the swapchain and, below 1.4, maintenance5 extensions, the library's needs, the required features. One that can't run the application is skipped, saying why when it ranks higher. A CPU device runs it only when nothing else is listed, and CRAFTER_GRAPHICS_DEVICE never falls back. A GPU two drivers both list is tried through each. - When no GPU qualifies, Initialize throws naming each GPU's gaps. It destroys the instance it made, before any Wayland connection, so it can be called again asking for less. - maintenance5 is vulkan14's on a 1.4 GPU and VK_KHR_maintenance5's below. CRAFTER_GRAPHICS_NO_VULKAN_1_4 runs as a 1.3 application, to exercise that path on a 1.4 GPU. - VK_KHR_spirv_1_4 and VK_KHR_shader_float_controls, core since 1.2, are no longer enabled, nor VK_EXT_shader_atomic_float, which was enabled without its feature struct. memoryDecompression's feature bit is queried before it is enabled. Shaders - VulkanShader checks a module's capabilities, declared directly or implicitly, against the enabled core features, the GPU's subgroup operations and the optional groups, and throws naming the field to pass, or saying the GPU lacks it. The tables are generated from vk.xml and SPIR-V's grammar. - WorkaroundNvidiaAS::Patch sized a row-major matrix push member by its columns, so the TLAS address could land inside it. Window - The swapchain format is one whose own feature flags allow storage reads and writes without a format, which since Vulkan 1.3 each format says for itself; shaderStorageImageRead/WriteWithoutFormat are no longer required. Tests - DeviceFeatures: ResolveFeatures, ChooseDevice and CheckShaderCapabilities, without a GPU. - FeatureRequests: required and optional features end to end, the 1.3 path, and a retry after a refusal. - PushConstantRewrite: a row-major mat3x4 push member. - HeapASRead, HeapDynamicIndexHit, RTPassPushData and the ray-tracing examples ask for the features their shaders use. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-31 06:40:02 +01:00
#ifndef CRAFTER_GRAPHICS_WINDOW_DOM
// Creates the instance, picks a GPU and creates the device. `required`
// and `optional` are what the application's own shaders and code use;
// the library adds what it needs itself. A GPU without Vulkan 1.3, a
// required extension, the library's needs or a required feature is
// skipped (saying why, when it ranks above the one chosen); when none is
// left this throws, naming each GPU's gaps. Optional features are enabled
// where the chosen GPU has them: enabledFeatures says which.
static void Initialize(const DeviceFeatures& required = {}, const DeviceFeatures& optional = {});
#else
2026-03-09 20:10:19 +01:00
static void Initialize();
feat(device): applications ask for the device features their shaders use Device - Device::Initialize(required, optional) takes DeviceFeatures, the five core feature structs vulkan10 to vulkan14. The library enables only what its own shaders and code use. Device::enabledFeatures says what was enabled, supportedFeatures what the GPU has, apiVersion the version in use. - shaderInt16, shaderFloat16, storageBuffer16BitAccess, scalarBlockLayout and the uniform-buffer, storage-buffer and storage-image non-uniform indexing bits are no longer enabled for everyone. An application whose shaders take uint16_t spec constants passes {.vulkan10 = {.shaderInt16 = VK_TRUE}}. scalarBlockLayout stays on with the NVIDIA workaround, whose TLAS address may sit in a struct's tail padding. - Every GPU is weighed before one is chosen: Vulkan 1.3, the swapchain and, below 1.4, maintenance5 extensions, the library's needs, the required features. One that can't run the application is skipped, saying why when it ranks higher. A CPU device runs it only when nothing else is listed, and CRAFTER_GRAPHICS_DEVICE never falls back. A GPU two drivers both list is tried through each. - When no GPU qualifies, Initialize throws naming each GPU's gaps. It destroys the instance it made, before any Wayland connection, so it can be called again asking for less. - maintenance5 is vulkan14's on a 1.4 GPU and VK_KHR_maintenance5's below. CRAFTER_GRAPHICS_NO_VULKAN_1_4 runs as a 1.3 application, to exercise that path on a 1.4 GPU. - VK_KHR_spirv_1_4 and VK_KHR_shader_float_controls, core since 1.2, are no longer enabled, nor VK_EXT_shader_atomic_float, which was enabled without its feature struct. memoryDecompression's feature bit is queried before it is enabled. Shaders - VulkanShader checks a module's capabilities, declared directly or implicitly, against the enabled core features, the GPU's subgroup operations and the optional groups, and throws naming the field to pass, or saying the GPU lacks it. The tables are generated from vk.xml and SPIR-V's grammar. - WorkaroundNvidiaAS::Patch sized a row-major matrix push member by its columns, so the TLAS address could land inside it. Window - The swapchain format is one whose own feature flags allow storage reads and writes without a format, which since Vulkan 1.3 each format says for itself; shaderStorageImageRead/WriteWithoutFormat are no longer required. Tests - DeviceFeatures: ResolveFeatures, ChooseDevice and CheckShaderCapabilities, without a GPU. - FeatureRequests: required and optional features end to end, the 1.3 path, and a retry after a refusal. - PushConstantRewrite: a row-major mat3x4 push member. - HeapASRead, HeapDynamicIndexHit, RTPassPushData and the ray-tracing examples ask for the features their shaders use. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-31 06:40:02 +01:00
#endif
2026-03-09 20:10:19 +01:00
2026-05-18 02:07:48 +02:00
#ifndef CRAFTER_GRAPHICS_WINDOW_DOM
2026-03-09 20:10:19 +01:00
#ifdef CRAFTER_GRAPHICS_WINDOW_WAYLAND
inline static wl_display* display = nullptr;
inline static wl_seat* seat = nullptr;
inline static xdg_wm_base* xdgWmBase = nullptr;
fix(wayland): open windows on compositors without xdg-decoration, viewporter or fractional scale Window - The constructor made a fractional scale, a toplevel decoration and a viewport from Device::fractionalScaleManager, Device::manager and Device::wpViewporter without looking at them. Device::handle_global binds those globals only when the compositor advertises them, and Initialize requires only wl_shm, wl_compositor and xdg_wm_base, so on a compositor without one of the three (GNOME's Mutter has no xdg-decoration) the constructor handed libwayland a null proxy and crashed before the window existed (#162). Each object is now made only when its global is there, which Resize and the destructor already assumed. - The fractional scale needs the viewporter too: the preferred scale is applied through the viewport, which shows the buffer's physical pixels at width/scale x height/scale. A surface without a viewport is as large as its buffer, so there the scale stays 1, which keeps pointer and touch positions, configure sizes and the text-input caret, all converted by it, in physical pixels; the compositor scales the window up on a HiDPI output. A scale that changes after the constructor is still applied only by the next resize, which is #165. - Without xdg-decoration the window has no title bar, since the library draws no client-side decorations. Device - The optional globals say they are optional, and that a request on a null proxy crashes. Tests - WindowOptionalGlobals: hides xdg-decoration, wp_viewporter and wp_fractional_scale_v1 from the Window one at a time and all at once, by nulling the Device statics after Initialize, and renders, resizes and closes a window each time without a protocol error. Before the fix it crashed on the window without xdg-decoration. A global the compositor does not offer is reported as a SKIP line, since the path that uses it goes untested there. README - Which Wayland protocols the window needs, and what it does without the optional ones. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-31 08:28:48 +01:00
// Optional protocols, like wpViewporter below: null on a compositor
// that does not advertise them (GNOME has no xdg-decoration), and a
// request on a null proxy crashes in libwayland, so check first.
2026-03-09 20:10:19 +01:00
inline static wp_fractional_scale_manager_v1* fractionalScaleManager = nullptr;
inline static zxdg_decoration_manager_v1* manager = nullptr;
inline static xkb_keymap* xkb_keymap;
inline static wl_compositor* compositor = nullptr;
inline static Window* focusedWindow = nullptr;
// The window with keyboard focus (wl_keyboard.enter/leave), apart from
// the pointer's: a key arrives without the pointer ever having entered
// (a touch phone, a key typed while the pointer hovers elsewhere), so the
// keyboard handlers route by this and never by focusedWindow.
inline static Window* keyboardWindow = nullptr;
2026-03-09 20:10:19 +01:00
inline static wl_shm* shm = nullptr;
inline static wp_viewporter* wpViewporter = nullptr;
inline static xkb_context* xkb_context = xkb_context_new(XKB_CONTEXT_NO_FLAGS);
inline static xkb_state* xkb_state;
inline static std::vector<Window*> windows;
2026-03-12 01:07:46 +01:00
inline static wl_pointer* wlPointer;
inline static wl_pointer* seatPointer = nullptr;
inline static wl_keyboard* seatKeyboard = nullptr;
inline static wl_touch* wlTouch = nullptr;
inline static Window* touchWindow = nullptr; // the window under the fingers
// The fingers down, by the compositor's id, in physical pixels. Plain
// data: the statics here are constant-initialised on purpose.
struct TouchSlot {
std::int32_t id;
float x;
float y;
bool used;
Window* window;
};
inline static std::array<TouchSlot, 10> touchSlots{};
static void ForgetTouches();
2026-05-12 00:24:48 +02:00
// wl_data_device_manager + wl_data_device drive copy/paste. Bound
// lazily in handle_global; the data device is created once both
// the manager and the seat are present (registry binding order
// isn't guaranteed). nullptr on compositors that don't expose
// the manager — Clipboard::SetText silently no-ops there.
inline static wl_data_device_manager* dataDeviceManager = nullptr;
inline static wl_data_device* dataDevice = nullptr;
// zwp_text_input_v3: one object per seat; the compositor says which
// surface it follows (enter/leave), the events are state that done()
// applies as a whole.
inline static zwp_text_input_manager_v3* textInputManager = nullptr;
inline static zwp_text_input_v3* textInput = nullptr;
inline static Window* textInputWindow = nullptr;
inline static std::uint32_t textInputDeleteBefore = 0;
inline static std::uint32_t textInputDeleteAfter = 0;
inline static bool textInputHasPreedit = false;
inline static bool textInputHasCommit = false;
inline static bool textInputHasDelete = false;
// Sends the window's wanted text-input state when it has the focus.
static void ApplyTextInput(Window* window);
2026-03-09 20:10:19 +01:00
static void seat_handle_capabilities(void* data, wl_seat* seat, uint32_t capabilities);
static void xdg_surface_handle_preferred_scale(void* data, wp_fractional_scale_v1*, std::uint32_t scale);
static void xdg_wm_base_handle_ping(void* data, xdg_wm_base* xdg_wm_base, std::uint32_t serial);
static void handle_global(void* data, wl_registry* registry, std::uint32_t name, const char* interface, std::uint32_t version);
static void handle_global_remove(void* data, wl_registry* registry, uint32_t name);
static void keyboard_keymap(void* data, wl_keyboard* keyboard, uint32_t format, int fd, uint32_t size);
static void keyboard_enter(void *data, wl_keyboard *keyboard, uint32_t serial, wl_surface *surface, wl_array *keys);
static void keyboard_leave(void *data, wl_keyboard *keyboard, uint32_t serial, wl_surface *surface);
static void keyboard_key(void *data, wl_keyboard *keyboard, uint32_t serial, uint32_t time, uint32_t key, uint32_t state);
static void keyboard_modifiers(void *data, wl_keyboard *keyboard, uint32_t serial, uint32_t mods_depressed, uint32_t mods_latched, uint32_t mods_locked, uint32_t group);
static void keyboard_repeat_info(void *data, wl_keyboard *keyboard, int32_t rate, int32_t delay);
static void pointer_handle_button(void* data, wl_pointer* pointer, std::uint32_t serial, std::uint32_t time, std::uint32_t button, std::uint32_t state);
static void PointerListenerHandleMotion(void* data, wl_pointer* wl_pointer, std::uint32_t time, wl_fixed_t surface_x, wl_fixed_t surface_y);
static void PointerListenerHandleAxis(void*, wl_pointer*, std::uint32_t time, std::uint32_t axis, wl_fixed_t value);
2026-03-09 20:10:19 +01:00
static void PointerListenerHandleEnter(void* data, wl_pointer* wl_pointer, std::uint32_t serial, wl_surface* surface, wl_fixed_t surface_x, wl_fixed_t surface_y);
static void PointerListenerHandleLeave(void*, wl_pointer*, std::uint32_t, wl_surface*);
static void touch_down(void* data, wl_touch* touch, std::uint32_t serial, std::uint32_t time, wl_surface* surface, std::int32_t id, wl_fixed_t x, wl_fixed_t y);
static void touch_up(void* data, wl_touch* touch, std::uint32_t serial, std::uint32_t time, std::int32_t id);
static void touch_motion(void* data, wl_touch* touch, std::uint32_t time, std::int32_t id, wl_fixed_t x, wl_fixed_t y);
static void touch_frame(void* data, wl_touch* touch);
static void touch_cancel(void* data, wl_touch* touch);
static void text_input_enter(void* data, zwp_text_input_v3* textInput, wl_surface* surface);
static void text_input_leave(void* data, zwp_text_input_v3* textInput, wl_surface* surface);
static void text_input_preedit(void* data, zwp_text_input_v3* textInput, const char* text, std::int32_t cursorBegin, std::int32_t cursorEnd);
static void text_input_commit(void* data, zwp_text_input_v3* textInput, const char* text);
static void text_input_delete(void* data, zwp_text_input_v3* textInput, std::uint32_t before, std::uint32_t after);
static void text_input_done(void* data, zwp_text_input_v3* textInput, std::uint32_t serial);
2026-03-09 20:10:19 +01:00
constexpr static wl_pointer_listener pointer_listener = {
.enter = PointerListenerHandleEnter,
.leave = PointerListenerHandleLeave,
.motion = PointerListenerHandleMotion,
.button = pointer_handle_button,
.axis = PointerListenerHandleAxis,
};
constexpr static wl_touch_listener touch_listener = {
.down = touch_down,
.up = touch_up,
.motion = touch_motion,
.frame = touch_frame,
.cancel = touch_cancel,
};
constexpr static zwp_text_input_v3_listener text_input_listener = {
.enter = text_input_enter,
.leave = text_input_leave,
.preedit_string = text_input_preedit,
.commit_string = text_input_commit,
.delete_surrounding_text = text_input_delete,
.done = text_input_done,
};
2026-03-09 20:10:19 +01:00
constexpr static wl_keyboard_listener keyboard_listener = {
.keymap = keyboard_keymap,
.enter = keyboard_enter,
.leave = keyboard_leave,
.key = keyboard_key,
.modifiers = keyboard_modifiers,
.repeat_info = keyboard_repeat_info,
};
constexpr static wl_seat_listener seat_listener = {
.capabilities = seat_handle_capabilities,
};
constexpr static wl_registry_listener registry_listener = {
.global = handle_global,
.global_remove = handle_global_remove,
};
constexpr static xdg_wm_base_listener xdgWmBaseListener = {
.ping = xdg_wm_base_handle_ping,
};
#endif
inline static VkInstance instance = VK_NULL_HANDLE;
inline static VkDebugUtilsMessengerEXT debugMessenger = VK_NULL_HANDLE;
inline static VkPhysicalDevice physDevice = VK_NULL_HANDLE;
inline static VkDevice device = VK_NULL_HANDLE;
inline static std::uint32_t queueFamilyIndex = 0;
inline static VkQueue queue = VK_NULL_HANDLE;
inline static VkCommandPool commandPool = VK_NULL_HANDLE;
inline static VkSwapchainKHR swapchain = VK_NULL_HANDLE;
inline static PFN_vkGetAccelerationStructureBuildSizesKHR vkGetAccelerationStructureBuildSizesKHR;
inline static PFN_vkCreateAccelerationStructureKHR vkCreateAccelerationStructureKHR;
2026-05-05 23:49:29 +02:00
inline static PFN_vkDestroyAccelerationStructureKHR vkDestroyAccelerationStructureKHR;
2026-03-09 20:10:19 +01:00
inline static PFN_vkCmdBuildAccelerationStructuresKHR vkCmdBuildAccelerationStructuresKHR;
inline static PFN_vkGetAccelerationStructureDeviceAddressKHR vkGetAccelerationStructureDeviceAddressKHR;
inline static PFN_vkCreateRayTracingPipelinesKHR vkCreateRayTracingPipelinesKHR;
inline static PFN_vkGetRayTracingShaderGroupHandlesKHR vkGetRayTracingShaderGroupHandlesKHR;
inline static PFN_vkCmdTraceRaysKHR vkCmdTraceRaysKHR;
2026-04-05 22:53:59 +02:00
inline static PFN_vkCmdBindResourceHeapEXT vkCmdBindResourceHeapEXT;
inline static PFN_vkCmdBindSamplerHeapEXT vkCmdBindSamplerHeapEXT;
inline static PFN_vkWriteResourceDescriptorsEXT vkWriteResourceDescriptorsEXT;
2026-05-01 23:35:37 +02:00
inline static PFN_vkWriteSamplerDescriptorsEXT vkWriteSamplerDescriptorsEXT;
inline static PFN_vkCmdPushDataEXT vkCmdPushDataEXT;
2026-04-05 22:53:59 +02:00
inline static PFN_vkGetPhysicalDeviceDescriptorSizeEXT vkGetPhysicalDeviceDescriptorSizeEXT;
2026-04-09 00:15:09 +02:00
inline static PFN_vkGetDeviceFaultInfoEXT vkGetDeviceFaultInfoEXT;
2026-04-05 22:53:59 +02:00
2026-05-12 00:24:48 +02:00
// VK_EXT_memory_decompression — opt-in. When the driver advertises it
// and exposes the GDeflate 1.0 method, GPU asset decompression is
// available; otherwise consumers fall back to CPU decode.
inline static bool memoryDecompressionSupported = false;
inline static PFN_vkCmdDecompressMemoryEXT vkCmdDecompressMemoryEXT = nullptr;
2026-03-09 20:10:19 +01:00
inline static VkPhysicalDeviceMemoryProperties memoryProperties;
2026-04-05 22:53:59 +02:00
// VkPhysicalDeviceLimits::nonCoherentAtomSize — the alignment (in bytes,
// a power of two) that vkFlushMappedMemoryRanges / vkInvalidateMappedMemoryRanges
// require for the offset and size of a sub-buffer range. Whole-buffer
// (VK_WHOLE_SIZE) flushes sidestep it; ranged flushes must round outward
// to it (see VulkanBuffer::FlushDevice(offset,bytes)). Populated at device
// creation; defaults to 1 so the rounding math is well-defined before then.
inline static VkDeviceSize nonCoherentAtomSize = 1;
// Core physical-device properties, captured once at Initialize from the
// VkPhysicalDeviceProperties2 query. Kept because the pipeline-cache
// persistence path needs vendorID / deviceID / pipelineCacheUUID to
// decide whether an on-disk blob was written by this exact device.
inline static VkPhysicalDeviceProperties deviceProperties = {};
// One process-wide pipeline cache fed to every vkCreate*Pipelines call
// (compute UI/user shaders + RT pipelines). Pipeline compilation is a
// one-time cold-start cost, not a per-frame one; a single shared cache
// lets the driver reuse compiled shader binaries across the several
// pipelines built at startup and — once persisted to disk (see
// LoadPipelineCache / SavePipelineCache) — across runs. VK_NULL_HANDLE
// until LoadPipelineCache runs; passing VK_NULL_HANDLE to a create call
// is valid and simply means "no cache", so the create sites need no
// null check.
inline static VkPipelineCache pipelineCache = VK_NULL_HANDLE;
// Where the serialized cache lives. Relative to the working directory,
// matching the gpu_crash_dump-* convention. Set before Initialize to
// relocate it.
inline static std::filesystem::path pipelineCachePath = "pipeline_cache.bin";
feat(vulkan): run the compute/UI path without VK_EXT_descriptor_heap Device::Initialize required twelve device extensions with no capability check. Mesa Turnip (Adreno) lacks six of them: VK_EXT_descriptor_heap, VK_KHR_shader_untyped_pointers, VK_EXT_device_fault and the three ray tracing extensions. vkCreateDevice failed there, so not even a UI-only program could start, although the UI path is compute-only and needs nothing else that Turnip is missing. The descriptor heap, ray tracing and device fault are now optional groups, detected at start-up from the extension list and the feature bits (Device::descriptorHeapSupported, rayTracingSupported, deviceFaultSupported). Ray tracing needs the heap; on a device without it the ray tracing entry points throw a message naming the cause. Without the heap the same bindless slots live in one update-after-bind descriptor set per frame, owned by DescriptorHeapVulkan: binding 0 storage images, 1 sampled images, 2 samplers, 3 storage buffers. Slot allocation, the slot handles, UIDispatchHeader and the UIRenderer API are unchanged; buffer slots simply stop being offset. Window::Render binds that frame's set where it binds the heap, every compute pipeline shares the heap object's pipeline layout, and push data travels as push constants. Each UI kernel is built twice from one source. ui-shared.glsl declares its resources through UI_HEAP_* macros that expand to descriptor_heap or, with CRAFTER_GRAPHICS_DESCRIPTOR_SETS defined, to set/binding. <name>.comp.spv and <name>.comp.sets.spv ship side by side and ComputeShader::Load picks one. The heap build of every shader is byte-identical to before. Breaking: - ComputeShader::Load takes the heap as a second argument. - DescriptorHeapVulkan is no longer copyable or movable. - Custom UI shaders declare heap resources with the UI_HEAP_* macros and are registered twice in project.cpp (see the README). - project.cpp uses Shader::defines and Shader::variant, so it needs a crafter-build that has them (Crafter.Build master, fe38cc1 or later). Also: - maintenance5 is enabled as a feature, not only as an extension: every VulkanBuffer already relied on VkBufferUsageFlags2CreateInfo. - Barrier stage masks drop RAY_TRACING_SHADER on devices without ray tracing, and ImageVulkan::Create resolves its default consumer stage to COMPUTE_SHADER there. - Window refuses a swapchain whose image count is not numFrames instead of writing past images[]. The limitation itself is #160. - CRAFTER_GRAPHICS_NO_DESCRIPTOR_HEAP forces the fallback on a driver that has the heap, so it can be exercised on a desktop GPU. Verified on an RTX 4090 in both modes under the validation layer, and on a Fairphone 6 (Turnip, Adreno 810, postmarketOS), where the fallback is the only path. Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com>
2026-10-06 17:28:46 +02:00
// ─── Optional capability groups ────────────────────────────────
// Decided once by Initialize from the extensions the driver
// advertises; false until then. Not every driver has the descriptor
// heap (Mesa Turnip on Adreno has neither it nor ray tracing), so the
// compute/UI path runs in one of two binding models:
// descriptorHeapSupported -> VK_EXT_descriptor_heap, the *.spv shaders
// !descriptorHeapSupported -> one bindless descriptor set per frame
// (DescriptorHeapVulkan sets mode), the
// *.sets.spv shaders, vkCmdPushConstants
// Setting the environment variable CRAFTER_GRAPHICS_NO_DESCRIPTOR_HEAP
feat(device): applications ask for the device features their shaders use Device - Device::Initialize(required, optional) takes DeviceFeatures, the five core feature structs vulkan10 to vulkan14. The library enables only what its own shaders and code use. Device::enabledFeatures says what was enabled, supportedFeatures what the GPU has, apiVersion the version in use. - shaderInt16, shaderFloat16, storageBuffer16BitAccess, scalarBlockLayout and the uniform-buffer, storage-buffer and storage-image non-uniform indexing bits are no longer enabled for everyone. An application whose shaders take uint16_t spec constants passes {.vulkan10 = {.shaderInt16 = VK_TRUE}}. scalarBlockLayout stays on with the NVIDIA workaround, whose TLAS address may sit in a struct's tail padding. - Every GPU is weighed before one is chosen: Vulkan 1.3, the swapchain and, below 1.4, maintenance5 extensions, the library's needs, the required features. One that can't run the application is skipped, saying why when it ranks higher. A CPU device runs it only when nothing else is listed, and CRAFTER_GRAPHICS_DEVICE never falls back. A GPU two drivers both list is tried through each. - When no GPU qualifies, Initialize throws naming each GPU's gaps. It destroys the instance it made, before any Wayland connection, so it can be called again asking for less. - maintenance5 is vulkan14's on a 1.4 GPU and VK_KHR_maintenance5's below. CRAFTER_GRAPHICS_NO_VULKAN_1_4 runs as a 1.3 application, to exercise that path on a 1.4 GPU. - VK_KHR_spirv_1_4 and VK_KHR_shader_float_controls, core since 1.2, are no longer enabled, nor VK_EXT_shader_atomic_float, which was enabled without its feature struct. memoryDecompression's feature bit is queried before it is enabled. Shaders - VulkanShader checks a module's capabilities, declared directly or implicitly, against the enabled core features, the GPU's subgroup operations and the optional groups, and throws naming the field to pass, or saying the GPU lacks it. The tables are generated from vk.xml and SPIR-V's grammar. - WorkaroundNvidiaAS::Patch sized a row-major matrix push member by its columns, so the TLAS address could land inside it. Window - The swapchain format is one whose own feature flags allow storage reads and writes without a format, which since Vulkan 1.3 each format says for itself; shaderStorageImageRead/WriteWithoutFormat are no longer required. Tests - DeviceFeatures: ResolveFeatures, ChooseDevice and CheckShaderCapabilities, without a GPU. - FeatureRequests: required and optional features end to end, the 1.3 path, and a retry after a refusal. - PushConstantRewrite: a row-major mat3x4 push member. - HeapASRead, HeapDynamicIndexHit, RTPassPushData and the ray-tracing examples ask for the features their shaders use. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-31 06:40:02 +01:00
// forces the second model on any driver, for testing. In the same way
// CRAFTER_GRAPHICS_NO_VULKAN_1_4 creates the instance as a Vulkan 1.3
// application, so the 1.3 path (maintenance5 through
// VK_KHR_maintenance5, no vulkan14 features) runs on a 1.4 GPU.
feat(vulkan): run the compute/UI path without VK_EXT_descriptor_heap Device::Initialize required twelve device extensions with no capability check. Mesa Turnip (Adreno) lacks six of them: VK_EXT_descriptor_heap, VK_KHR_shader_untyped_pointers, VK_EXT_device_fault and the three ray tracing extensions. vkCreateDevice failed there, so not even a UI-only program could start, although the UI path is compute-only and needs nothing else that Turnip is missing. The descriptor heap, ray tracing and device fault are now optional groups, detected at start-up from the extension list and the feature bits (Device::descriptorHeapSupported, rayTracingSupported, deviceFaultSupported). Ray tracing needs the heap; on a device without it the ray tracing entry points throw a message naming the cause. Without the heap the same bindless slots live in one update-after-bind descriptor set per frame, owned by DescriptorHeapVulkan: binding 0 storage images, 1 sampled images, 2 samplers, 3 storage buffers. Slot allocation, the slot handles, UIDispatchHeader and the UIRenderer API are unchanged; buffer slots simply stop being offset. Window::Render binds that frame's set where it binds the heap, every compute pipeline shares the heap object's pipeline layout, and push data travels as push constants. Each UI kernel is built twice from one source. ui-shared.glsl declares its resources through UI_HEAP_* macros that expand to descriptor_heap or, with CRAFTER_GRAPHICS_DESCRIPTOR_SETS defined, to set/binding. <name>.comp.spv and <name>.comp.sets.spv ship side by side and ComputeShader::Load picks one. The heap build of every shader is byte-identical to before. Breaking: - ComputeShader::Load takes the heap as a second argument. - DescriptorHeapVulkan is no longer copyable or movable. - Custom UI shaders declare heap resources with the UI_HEAP_* macros and are registered twice in project.cpp (see the README). - project.cpp uses Shader::defines and Shader::variant, so it needs a crafter-build that has them (Crafter.Build master, fe38cc1 or later). Also: - maintenance5 is enabled as a feature, not only as an extension: every VulkanBuffer already relied on VkBufferUsageFlags2CreateInfo. - Barrier stage masks drop RAY_TRACING_SHADER on devices without ray tracing, and ImageVulkan::Create resolves its default consumer stage to COMPUTE_SHADER there. - Window refuses a swapchain whose image count is not numFrames instead of writing past images[]. The limitation itself is #160. - CRAFTER_GRAPHICS_NO_DESCRIPTOR_HEAP forces the fallback on a driver that has the heap, so it can be exercised on a desktop GPU. Verified on an RTX 4090 in both modes under the validation layer, and on a Fairphone 6 (Turnip, Adreno 810, postmarketOS), where the fallback is the only path. Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com>
2026-10-06 17:28:46 +02:00
inline static bool descriptorHeapSupported = false;
// Ray tracing also needs the descriptor heap (RT pipelines and RT
// shaders are heap-only), so this is never true without it.
inline static bool rayTracingSupported = false;
// VK_EXT_device_fault: the VK_ERROR_DEVICE_LOST report in CheckVkResult.
inline static bool deviceFaultSupported = false;
fix(window): size the swapchain for its present mode, and key frames in flight by frame instead of image Window - CreateSwapchain asked for exactly numFrames (3) images whatever the surface said, read them into a three-element array, and keyed the command buffers, the fences and the descriptor-heap copy holding "its" swapchain image by the acquired image index. A swapchain of another size fell back to FIFO (asked for MAILBOX or IMMEDIATE) or threw, and a surface whose minimum is above three got a create info below it (#160). Mesa's MAILBOX needs four images: Turnip on phoc, llvmpipe on any Wayland compositor. - The present mode is picked first, then the swapchain is sized by SwapchainCapabilities: the surface's counts for that mode where the instance has surface_maintenance1. With swapchain_maintenance1 the swapchain names its mode (VkSwapchainPresentModesCreateInfoKHR), which is what makes the mode's own count the one that applies, at creation and to the forward progress of a UINT64_MAX acquire; without it the stricter of the mode's and the surface's counts for all modes. It asks for numFrames or that minimum where more, never past the maximum (SwapchainMinImageCount). Turnip's FIFO keeps its three images and MAILBOX gets four. The FIFO fallback and presentModeUnfit are gone. - images, imageViewInfos (was imageViews: they are create-infos), the made imageViews that descriptor sets take, imageInitialised and the render-finished semaphores are per image, as many as the driver made. The views are made once per swapchain. The semaphores only grow, since a present of the old swapchain may still wait on one. - Frames in flight stay numFrames, keyed by currentFrame, frameCounter % numFrames: the command buffer, the fence, the acquire semaphore, the heap copy (or set), RenderingElement3D::tlases, the frameIdx a pass is recorded with. currentImage is the acquired image. currentBuffer is gone, so code that used it stops compiling instead of picking the wrong one; RenderPass::Record's frameIdx keeps its name and is now the frame slot, as RenderPass.cppm says. - The frame's fence is waited before the acquire. The acquire semaphores need no spare then, and a frame waits for exactly the one numFrames before it, which the deletion queue and the staging rings count on and fences keyed by image did not promise once images come back out of order. The swapchain slots are written before the fence is reset. - From the acquire to the present the window is recording: a Resize or SetPresentMode from a listener (a VSync key ticked in onUpdate) takes effect after the present, through RebuildSwapchain, together with a present that found the swapchain out of date. Rebuilding mid-frame used to destroy the image the frame was drawing into; with images sized per swapchain it would also index past the new one's end. DescriptorHeapVulkan - Frames and images pair up differently from frame to frame, so no heap copy can keep one swapchain image. AddSwapchainSlot(slot, window) lists an image slot as that window's swapchain image, and Window::Render writes the image a frame acquired into its window's listed slots in that frame's copy, or its set, before onUpdate, skipping a slot that holds it already. A slot is one window's, so two windows can share a heap without writing each other's slots under each other's frames in flight. A slot listed during a frame its window records is written at once. A slot past the heap's image slots, an empty ImageSlot's 0xFFFF among them, is refused. Freeing a slot unlists it, as does initialising the heap in either binding model. WriteImageSet, the one use of which this replaces, is gone, and with it the per-frame dimension of setImageViews. Device - VK_KHR_surface_maintenance1 (or EXT) and VK_KHR_get_surface_capabilities2 are enabled where the loader has them (surfaceMaintenance1Supported, vkGetPhysicalDeviceSurfaceCapabilities2KHR), and the matching swapchain_maintenance1 with its feature where the device has it (swapchainMaintenance1Supported). - DistinctGpus groups the physical devices by GPU as CRAFTER_GRAPHICS_DEVICE numbers them, for ChooseGpu and tests. UI - UIRenderer lists its output slot instead of writing every copy itself, and again on every resize; the resize listener is gone on both backends. The headers' frameIdx is currentFrame. Examples - VulkanTriangle, Sponza, SponzaBench and RTVolume list their output slot and write only the per-frame TLASes. They wrote imageViews[f] into copy f once, which a resize left pointing at destroyed images. Tests - SwapchainImageCount: SwapchainMinImageCount on the numbers drivers report; every offered mode keeps its mode and gets what the window asks for; every frame presents the colour it drew, read back a texel per frame; a switch and a resize asked for during a frame wait for its present; a slot listed during a frame holds its image at once; two windows on one heap draw their own colours. The frame checks run again in child processes, through CRAFTER_GRAPHICS_DEVICE, on a GPU whose swapchains have more images than frames (this one, else llvmpipe), in each binding model the parent didn't cover; a child skips only for a GPU that can't run the library, and checks it runs the model it was asked for. - FrameLoopSync checks the frame slot follows the frame counter and counts the swapchain's own images. HeapASRead, HeapDynamicIndexHit and RTPassPushData list their swapchain slot and drop their resize rewrites. Docs - README, AGENTS.md and AGENTS.consumer.md: frames and images, the swapchain slots, rebuilds held to the present, the new optional groups. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-31 08:51:18 +01:00
// VK_KHR_surface_maintenance1 (or its EXT form) with
// VK_KHR_get_surface_capabilities2, instance extensions enabled where
// the loader has them: a surface's capabilities for one present mode.
// Its image counts can differ from the surface's answer for all modes,
// and Window::CreateSwapchain asks for the mode it uses (#160).
inline static bool surfaceMaintenance1Supported = false;
inline static PFN_vkGetPhysicalDeviceSurfaceCapabilities2KHR vkGetPhysicalDeviceSurfaceCapabilities2KHR = nullptr;
// VK_KHR_swapchain_maintenance1 (or its EXT form) with its
// swapchainMaintenance1 feature, a device group enabled where the
// driver has it. With it a swapchain is made naming its present mode
// (VkSwapchainPresentModesCreateInfoKHR), which is what lets it have
// that mode's image count where the surface's count for all modes is
// higher (Turnip's FIFO: 3 where MAILBOX needs 4).
inline static bool swapchainMaintenance1Supported = false;
feat(vulkan): run the compute/UI path without VK_EXT_descriptor_heap Device::Initialize required twelve device extensions with no capability check. Mesa Turnip (Adreno) lacks six of them: VK_EXT_descriptor_heap, VK_KHR_shader_untyped_pointers, VK_EXT_device_fault and the three ray tracing extensions. vkCreateDevice failed there, so not even a UI-only program could start, although the UI path is compute-only and needs nothing else that Turnip is missing. The descriptor heap, ray tracing and device fault are now optional groups, detected at start-up from the extension list and the feature bits (Device::descriptorHeapSupported, rayTracingSupported, deviceFaultSupported). Ray tracing needs the heap; on a device without it the ray tracing entry points throw a message naming the cause. Without the heap the same bindless slots live in one update-after-bind descriptor set per frame, owned by DescriptorHeapVulkan: binding 0 storage images, 1 sampled images, 2 samplers, 3 storage buffers. Slot allocation, the slot handles, UIDispatchHeader and the UIRenderer API are unchanged; buffer slots simply stop being offset. Window::Render binds that frame's set where it binds the heap, every compute pipeline shares the heap object's pipeline layout, and push data travels as push constants. Each UI kernel is built twice from one source. ui-shared.glsl declares its resources through UI_HEAP_* macros that expand to descriptor_heap or, with CRAFTER_GRAPHICS_DESCRIPTOR_SETS defined, to set/binding. <name>.comp.spv and <name>.comp.sets.spv ship side by side and ComputeShader::Load picks one. The heap build of every shader is byte-identical to before. Breaking: - ComputeShader::Load takes the heap as a second argument. - DescriptorHeapVulkan is no longer copyable or movable. - Custom UI shaders declare heap resources with the UI_HEAP_* macros and are registered twice in project.cpp (see the README). - project.cpp uses Shader::defines and Shader::variant, so it needs a crafter-build that has them (Crafter.Build master, fe38cc1 or later). Also: - maintenance5 is enabled as a feature, not only as an extension: every VulkanBuffer already relied on VkBufferUsageFlags2CreateInfo. - Barrier stage masks drop RAY_TRACING_SHADER on devices without ray tracing, and ImageVulkan::Create resolves its default consumer stage to COMPUTE_SHADER there. - Window refuses a swapchain whose image count is not numFrames instead of writing past images[]. The limitation itself is #160. - CRAFTER_GRAPHICS_NO_DESCRIPTOR_HEAP forces the fallback on a driver that has the heap, so it can be exercised on a desktop GPU. Verified on an RTX 4090 in both modes under the validation layer, and on a Fairphone 6 (Turnip, Adreno 810, postmarketOS), where the fallback is the only path. Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com>
2026-10-06 17:28:46 +02:00
// Pipeline stages that are valid on this device: everything, minus
// RAY_TRACING_SHADER when ray tracing is unsupported. AND it into any
// stage mask built from a constant that names the RT stage.
inline static VkPipelineStageFlags stageMask = ~VkPipelineStageFlags{0};
// Throws std::runtime_error naming the missing capability when ray
// tracing is unsupported. Called at every ray tracing entry point.
static void RequireRayTracing();
feat(device): applications ask for the device features their shaders use Device - Device::Initialize(required, optional) takes DeviceFeatures, the five core feature structs vulkan10 to vulkan14. The library enables only what its own shaders and code use. Device::enabledFeatures says what was enabled, supportedFeatures what the GPU has, apiVersion the version in use. - shaderInt16, shaderFloat16, storageBuffer16BitAccess, scalarBlockLayout and the uniform-buffer, storage-buffer and storage-image non-uniform indexing bits are no longer enabled for everyone. An application whose shaders take uint16_t spec constants passes {.vulkan10 = {.shaderInt16 = VK_TRUE}}. scalarBlockLayout stays on with the NVIDIA workaround, whose TLAS address may sit in a struct's tail padding. - Every GPU is weighed before one is chosen: Vulkan 1.3, the swapchain and, below 1.4, maintenance5 extensions, the library's needs, the required features. One that can't run the application is skipped, saying why when it ranks higher. A CPU device runs it only when nothing else is listed, and CRAFTER_GRAPHICS_DEVICE never falls back. A GPU two drivers both list is tried through each. - When no GPU qualifies, Initialize throws naming each GPU's gaps. It destroys the instance it made, before any Wayland connection, so it can be called again asking for less. - maintenance5 is vulkan14's on a 1.4 GPU and VK_KHR_maintenance5's below. CRAFTER_GRAPHICS_NO_VULKAN_1_4 runs as a 1.3 application, to exercise that path on a 1.4 GPU. - VK_KHR_spirv_1_4 and VK_KHR_shader_float_controls, core since 1.2, are no longer enabled, nor VK_EXT_shader_atomic_float, which was enabled without its feature struct. memoryDecompression's feature bit is queried before it is enabled. Shaders - VulkanShader checks a module's capabilities, declared directly or implicitly, against the enabled core features, the GPU's subgroup operations and the optional groups, and throws naming the field to pass, or saying the GPU lacks it. The tables are generated from vk.xml and SPIR-V's grammar. - WorkaroundNvidiaAS::Patch sized a row-major matrix push member by its columns, so the TLAS address could land inside it. Window - The swapchain format is one whose own feature flags allow storage reads and writes without a format, which since Vulkan 1.3 each format says for itself; shaderStorageImageRead/WriteWithoutFormat are no longer required. Tests - DeviceFeatures: ResolveFeatures, ChooseDevice and CheckShaderCapabilities, without a GPU. - FeatureRequests: required and optional features end to end, the 1.3 path, and a retry after a refusal. - PushConstantRewrite: a row-major mat3x4 push member. - HeapASRead, HeapDynamicIndexHit, RTPassPushData and the ray-tracing examples ask for the features their shaders use. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-31 06:40:02 +01:00
// ─── Device features ───────────────────────────────────────────
// Every feature Initialize enabled: the library's own, the
// application's required ones, and whichever optional ones the GPU has.
// pNext is null. Read only the fields you asked for: the library's own
// may change. All false before Initialize.
inline static DeviceFeatures enabledFeatures {};
// Every feature the chosen GPU supports, enabled or not. Below Vulkan
// 1.4 vulkan14 holds maintenance5 alone, from VK_KHR_maintenance5.
inline static DeviceFeatures supportedFeatures {};
// The Vulkan version the application may use on the chosen GPU:
// min(the instance's, the GPU's). Zero before Initialize.
inline static std::uint32_t apiVersion = 0;
// The GPU's subgroup size, stages and operations, which
// CheckShaderCapabilities holds GroupNonUniform* capabilities to.
inline static VkPhysicalDeviceVulkan11Properties vulkan11Properties = {
.sType = VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_VULKAN_1_1_PROPERTIES
};
// True when shaderInt64 is on only for the NVIDIA workaround's SPIR-V
// rewrite (below): an application shader that declares Int64 still
// needs the application to ask for it, or it would run only on NVIDIA.
inline static bool shaderInt64ForWorkaroundOnly = false;
// The features to enable on a GPU that supports `supported`: library ∪
// required ∪ (optional ∩ supported), never one the GPU lacks. Each one
// in `library` or `required` that it lacks goes to `gaps`. Below an
// apiVersion of 1.4 a vulkan14 one is marked "(Vulkan 1.4 only)", but
// maintenance5, which VK_KHR_maintenance5 provides there, is marked as
// that extension's. Pure logic, driven by tests/DeviceFeatures without
// a GPU.
static DeviceFeatures ResolveFeatures(const DeviceFeatures& library, const DeviceFeatures& required,
const DeviceFeatures& optional, const DeviceFeatures& supported,
std::uint32_t apiVersion, FeatureGaps& gaps);
// The GPU to use: the best device type among those without gaps
// (discrete, integrated, virtual, other), a CPU device only when no
// other kind is listed. Prints why each better-ranked GPU was passed
// over, and throws listing every GPU's gaps when none qualifies. Pure
// logic apart from that print, driven by tests/DeviceFeatures.
static std::size_t ChooseDevice(std::span<const DeviceChoice> choices);
fix(window): size the swapchain for its present mode, and key frames in flight by frame instead of image Window - CreateSwapchain asked for exactly numFrames (3) images whatever the surface said, read them into a three-element array, and keyed the command buffers, the fences and the descriptor-heap copy holding "its" swapchain image by the acquired image index. A swapchain of another size fell back to FIFO (asked for MAILBOX or IMMEDIATE) or threw, and a surface whose minimum is above three got a create info below it (#160). Mesa's MAILBOX needs four images: Turnip on phoc, llvmpipe on any Wayland compositor. - The present mode is picked first, then the swapchain is sized by SwapchainCapabilities: the surface's counts for that mode where the instance has surface_maintenance1. With swapchain_maintenance1 the swapchain names its mode (VkSwapchainPresentModesCreateInfoKHR), which is what makes the mode's own count the one that applies, at creation and to the forward progress of a UINT64_MAX acquire; without it the stricter of the mode's and the surface's counts for all modes. It asks for numFrames or that minimum where more, never past the maximum (SwapchainMinImageCount). Turnip's FIFO keeps its three images and MAILBOX gets four. The FIFO fallback and presentModeUnfit are gone. - images, imageViewInfos (was imageViews: they are create-infos), the made imageViews that descriptor sets take, imageInitialised and the render-finished semaphores are per image, as many as the driver made. The views are made once per swapchain. The semaphores only grow, since a present of the old swapchain may still wait on one. - Frames in flight stay numFrames, keyed by currentFrame, frameCounter % numFrames: the command buffer, the fence, the acquire semaphore, the heap copy (or set), RenderingElement3D::tlases, the frameIdx a pass is recorded with. currentImage is the acquired image. currentBuffer is gone, so code that used it stops compiling instead of picking the wrong one; RenderPass::Record's frameIdx keeps its name and is now the frame slot, as RenderPass.cppm says. - The frame's fence is waited before the acquire. The acquire semaphores need no spare then, and a frame waits for exactly the one numFrames before it, which the deletion queue and the staging rings count on and fences keyed by image did not promise once images come back out of order. The swapchain slots are written before the fence is reset. - From the acquire to the present the window is recording: a Resize or SetPresentMode from a listener (a VSync key ticked in onUpdate) takes effect after the present, through RebuildSwapchain, together with a present that found the swapchain out of date. Rebuilding mid-frame used to destroy the image the frame was drawing into; with images sized per swapchain it would also index past the new one's end. DescriptorHeapVulkan - Frames and images pair up differently from frame to frame, so no heap copy can keep one swapchain image. AddSwapchainSlot(slot, window) lists an image slot as that window's swapchain image, and Window::Render writes the image a frame acquired into its window's listed slots in that frame's copy, or its set, before onUpdate, skipping a slot that holds it already. A slot is one window's, so two windows can share a heap without writing each other's slots under each other's frames in flight. A slot listed during a frame its window records is written at once. A slot past the heap's image slots, an empty ImageSlot's 0xFFFF among them, is refused. Freeing a slot unlists it, as does initialising the heap in either binding model. WriteImageSet, the one use of which this replaces, is gone, and with it the per-frame dimension of setImageViews. Device - VK_KHR_surface_maintenance1 (or EXT) and VK_KHR_get_surface_capabilities2 are enabled where the loader has them (surfaceMaintenance1Supported, vkGetPhysicalDeviceSurfaceCapabilities2KHR), and the matching swapchain_maintenance1 with its feature where the device has it (swapchainMaintenance1Supported). - DistinctGpus groups the physical devices by GPU as CRAFTER_GRAPHICS_DEVICE numbers them, for ChooseGpu and tests. UI - UIRenderer lists its output slot instead of writing every copy itself, and again on every resize; the resize listener is gone on both backends. The headers' frameIdx is currentFrame. Examples - VulkanTriangle, Sponza, SponzaBench and RTVolume list their output slot and write only the per-frame TLASes. They wrote imageViews[f] into copy f once, which a resize left pointing at destroyed images. Tests - SwapchainImageCount: SwapchainMinImageCount on the numbers drivers report; every offered mode keeps its mode and gets what the window asks for; every frame presents the colour it drew, read back a texel per frame; a switch and a resize asked for during a frame wait for its present; a slot listed during a frame holds its image at once; two windows on one heap draw their own colours. The frame checks run again in child processes, through CRAFTER_GRAPHICS_DEVICE, on a GPU whose swapchains have more images than frames (this one, else llvmpipe), in each binding model the parent didn't cover; a child skips only for a GPU that can't run the library, and checks it runs the model it was asked for. - FrameLoopSync checks the frame slot follows the frame counter and counts the swapchain's own images. HeapASRead, HeapDynamicIndexHit and RTPassPushData list their swapchain slot and drop their resize rewrites. Docs - README, AGENTS.md and AGENTS.consumer.md: frames and images, the swapchain slots, rebuilds held to the present, the new optional groups. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-31 08:51:18 +01:00
// `instance`'s physical devices grouped by GPU, in the loader's order:
// a GPU two drivers both list (a Flatpak sees the runtime's and the GL
// extension's manifests; RADV beside AMDVLK) has one device UUID and
// two handles. CRAFTER_GRAPHICS_DEVICE numbers these groups, so one
// number means the same GPU inside a sandbox and out.
static std::vector<std::vector<VkPhysicalDevice>> DistinctGpus(VkInstance instance);
feat(device): applications ask for the device features their shaders use Device - Device::Initialize(required, optional) takes DeviceFeatures, the five core feature structs vulkan10 to vulkan14. The library enables only what its own shaders and code use. Device::enabledFeatures says what was enabled, supportedFeatures what the GPU has, apiVersion the version in use. - shaderInt16, shaderFloat16, storageBuffer16BitAccess, scalarBlockLayout and the uniform-buffer, storage-buffer and storage-image non-uniform indexing bits are no longer enabled for everyone. An application whose shaders take uint16_t spec constants passes {.vulkan10 = {.shaderInt16 = VK_TRUE}}. scalarBlockLayout stays on with the NVIDIA workaround, whose TLAS address may sit in a struct's tail padding. - Every GPU is weighed before one is chosen: Vulkan 1.3, the swapchain and, below 1.4, maintenance5 extensions, the library's needs, the required features. One that can't run the application is skipped, saying why when it ranks higher. A CPU device runs it only when nothing else is listed, and CRAFTER_GRAPHICS_DEVICE never falls back. A GPU two drivers both list is tried through each. - When no GPU qualifies, Initialize throws naming each GPU's gaps. It destroys the instance it made, before any Wayland connection, so it can be called again asking for less. - maintenance5 is vulkan14's on a 1.4 GPU and VK_KHR_maintenance5's below. CRAFTER_GRAPHICS_NO_VULKAN_1_4 runs as a 1.3 application, to exercise that path on a 1.4 GPU. - VK_KHR_spirv_1_4 and VK_KHR_shader_float_controls, core since 1.2, are no longer enabled, nor VK_EXT_shader_atomic_float, which was enabled without its feature struct. memoryDecompression's feature bit is queried before it is enabled. Shaders - VulkanShader checks a module's capabilities, declared directly or implicitly, against the enabled core features, the GPU's subgroup operations and the optional groups, and throws naming the field to pass, or saying the GPU lacks it. The tables are generated from vk.xml and SPIR-V's grammar. - WorkaroundNvidiaAS::Patch sized a row-major matrix push member by its columns, so the TLAS address could land inside it. Window - The swapchain format is one whose own feature flags allow storage reads and writes without a format, which since Vulkan 1.3 each format says for itself; shaderStorageImageRead/WriteWithoutFormat are no longer required. Tests - DeviceFeatures: ResolveFeatures, ChooseDevice and CheckShaderCapabilities, without a GPU. - FeatureRequests: required and optional features end to end, the 1.3 path, and a retry after a refusal. - PushConstantRewrite: a row-major mat3x4 push member. - HeapASRead, HeapDynamicIndexHit, RTPassPushData and the ray-tracing examples ask for the features their shaders use. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-31 06:40:02 +01:00
// Throws when `spirv` declares, directly or implicitly (Int64Atomics
// declares Int64), a SPIR-V capability that nothing enabled: a core
// feature that is off, a subgroup operation the GPU lacks, or one of
// the library's optional groups (ray tracing, the descriptor heap) that
// is off. Most drivers accept such a module silently. The message names
// `file`, the capability, and either the field to pass to Initialize or
// that the GPU lacks it. Capabilities a core version enables, and ones
// no enabled extension can, are left to the driver and the validation
// layer. VulkanShader calls it on every module, before the NVIDIA
// rewrite. Reads enabledFeatures, supportedFeatures, apiVersion,
// vulkan11Properties and the group flags, so tests can stage those.
static void CheckShaderCapabilities(std::span<const std::uint32_t> spirv, const std::filesystem::path& file);
feat(vulkan): run the compute/UI path without VK_EXT_descriptor_heap Device::Initialize required twelve device extensions with no capability check. Mesa Turnip (Adreno) lacks six of them: VK_EXT_descriptor_heap, VK_KHR_shader_untyped_pointers, VK_EXT_device_fault and the three ray tracing extensions. vkCreateDevice failed there, so not even a UI-only program could start, although the UI path is compute-only and needs nothing else that Turnip is missing. The descriptor heap, ray tracing and device fault are now optional groups, detected at start-up from the extension list and the feature bits (Device::descriptorHeapSupported, rayTracingSupported, deviceFaultSupported). Ray tracing needs the heap; on a device without it the ray tracing entry points throw a message naming the cause. Without the heap the same bindless slots live in one update-after-bind descriptor set per frame, owned by DescriptorHeapVulkan: binding 0 storage images, 1 sampled images, 2 samplers, 3 storage buffers. Slot allocation, the slot handles, UIDispatchHeader and the UIRenderer API are unchanged; buffer slots simply stop being offset. Window::Render binds that frame's set where it binds the heap, every compute pipeline shares the heap object's pipeline layout, and push data travels as push constants. Each UI kernel is built twice from one source. ui-shared.glsl declares its resources through UI_HEAP_* macros that expand to descriptor_heap or, with CRAFTER_GRAPHICS_DESCRIPTOR_SETS defined, to set/binding. <name>.comp.spv and <name>.comp.sets.spv ship side by side and ComputeShader::Load picks one. The heap build of every shader is byte-identical to before. Breaking: - ComputeShader::Load takes the heap as a second argument. - DescriptorHeapVulkan is no longer copyable or movable. - Custom UI shaders declare heap resources with the UI_HEAP_* macros and are registered twice in project.cpp (see the README). - project.cpp uses Shader::defines and Shader::variant, so it needs a crafter-build that has them (Crafter.Build master, fe38cc1 or later). Also: - maintenance5 is enabled as a feature, not only as an extension: every VulkanBuffer already relied on VkBufferUsageFlags2CreateInfo. - Barrier stage masks drop RAY_TRACING_SHADER on devices without ray tracing, and ImageVulkan::Create resolves its default consumer stage to COMPUTE_SHADER there. - Window refuses a swapchain whose image count is not numFrames instead of writing past images[]. The limitation itself is #160. - CRAFTER_GRAPHICS_NO_DESCRIPTOR_HEAP forces the fallback on a driver that has the heap, so it can be exercised on a desktop GPU. Verified on an RTX 4090 in both modes under the validation layer, and on a Fairphone 6 (Turnip, Adreno 810, postmarketOS), where the fallback is the only path. Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com>
2026-10-06 17:28:46 +02:00
// The two property structs below are only filled in (and only chained
// into the properties query) when their capability group is supported;
// otherwise they stay zero.
2026-04-05 22:53:59 +02:00
inline static VkPhysicalDeviceDescriptorHeapPropertiesEXT descriptorHeapProperties = {
.sType = VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_DESCRIPTOR_HEAP_PROPERTIES_EXT
};
2026-03-09 20:10:19 +01:00
inline static VkPhysicalDeviceRayTracingPipelinePropertiesKHR rayTracingProperties = {
feat(vulkan): run the compute/UI path without VK_EXT_descriptor_heap Device::Initialize required twelve device extensions with no capability check. Mesa Turnip (Adreno) lacks six of them: VK_EXT_descriptor_heap, VK_KHR_shader_untyped_pointers, VK_EXT_device_fault and the three ray tracing extensions. vkCreateDevice failed there, so not even a UI-only program could start, although the UI path is compute-only and needs nothing else that Turnip is missing. The descriptor heap, ray tracing and device fault are now optional groups, detected at start-up from the extension list and the feature bits (Device::descriptorHeapSupported, rayTracingSupported, deviceFaultSupported). Ray tracing needs the heap; on a device without it the ray tracing entry points throw a message naming the cause. Without the heap the same bindless slots live in one update-after-bind descriptor set per frame, owned by DescriptorHeapVulkan: binding 0 storage images, 1 sampled images, 2 samplers, 3 storage buffers. Slot allocation, the slot handles, UIDispatchHeader and the UIRenderer API are unchanged; buffer slots simply stop being offset. Window::Render binds that frame's set where it binds the heap, every compute pipeline shares the heap object's pipeline layout, and push data travels as push constants. Each UI kernel is built twice from one source. ui-shared.glsl declares its resources through UI_HEAP_* macros that expand to descriptor_heap or, with CRAFTER_GRAPHICS_DESCRIPTOR_SETS defined, to set/binding. <name>.comp.spv and <name>.comp.sets.spv ship side by side and ComputeShader::Load picks one. The heap build of every shader is byte-identical to before. Breaking: - ComputeShader::Load takes the heap as a second argument. - DescriptorHeapVulkan is no longer copyable or movable. - Custom UI shaders declare heap resources with the UI_HEAP_* macros and are registered twice in project.cpp (see the README). - project.cpp uses Shader::defines and Shader::variant, so it needs a crafter-build that has them (Crafter.Build master, fe38cc1 or later). Also: - maintenance5 is enabled as a feature, not only as an extension: every VulkanBuffer already relied on VkBufferUsageFlags2CreateInfo. - Barrier stage masks drop RAY_TRACING_SHADER on devices without ray tracing, and ImageVulkan::Create resolves its default consumer stage to COMPUTE_SHADER there. - Window refuses a swapchain whose image count is not numFrames instead of writing past images[]. The limitation itself is #160. - CRAFTER_GRAPHICS_NO_DESCRIPTOR_HEAP forces the fallback on a driver that has the heap, so it can be exercised on a desktop GPU. Verified on an RTX 4090 in both modes under the validation layer, and on a Fairphone 6 (Turnip, Adreno 810, postmarketOS), where the fallback is the only path. Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com>
2026-10-06 17:28:46 +02:00
.sType = VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_RAY_TRACING_PIPELINE_PROPERTIES_KHR
2026-03-09 20:10:19 +01:00
};
2026-05-12 00:24:48 +02:00
inline static VkPhysicalDeviceMemoryDecompressionPropertiesEXT memoryDecompressionProperties = {
.sType = VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_MEMORY_DECOMPRESSION_PROPERTIES_EXT
};
fix(vulkan-rt): work around NVIDIA descriptor-heap AS-read device-loss (#15) Reading an acceleration structure through VK_EXT_descriptor_heap aborts with VK_ERROR_DEVICE_LOST on NVIDIA 610.43.02 — a brand-new-extension driver fault isolated in #7 (engine setup is correct and validation-clean; images/buffers through the same heap work, and both traceRayEXT and inline rayQuery fault identically on the AS read). An acceleration structure can equally be reached by its device address via OpConvertUToAccelerationStructureKHR, which reads no descriptor and so never touches the faulting heap path. glslang has no GLSL spelling for that conversion, so VulkanShader rewrites the compiled SPIR-V at module-load time: every `OpLoad %accelStruct <heap-ptr>` becomes a load of the TLAS device address from a synthesized push-constant block followed by the convert. RTPass pushes the active frame's TLAS address into that push constant. User GLSL and example code are unchanged; acceleration structures still bind into the heap normally. The workaround is gated on Device::workaroundDescriptorHeapAS (true only on the NVIDIA proprietary driver) and confined to one fenced block in Crafter.Graphics-ShaderVulkan.cppm plus the RTPass push and the shaderInt64 feature toggle — delete those once a fixed NVIDIA driver ships and the heap AS read becomes the direct path again. Verified: VulkanTriangle ray-traces correctly on native NVIDIA (RTX 4090), validation-layer-clean, no device loss. The SPIR-V rewrite was independently validated with spirv-val on both the VulkanTriangle and Sponza raygen modules. Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
2026-06-03 01:59:54 +00:00
inline static VkPhysicalDeviceDriverProperties driverProperties = {
.sType = VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_DRIVER_PROPERTIES
};
// ─── NVIDIA descriptor-heap AS-read workaround (issue #15 / #7) ──
// True only on the NVIDIA proprietary driver, where reading an
// acceleration structure through VK_EXT_descriptor_heap aborts with
// VK_ERROR_DEVICE_LOST (a brand-new-extension driver fault, verified
// engine-clean in #7). When set, VulkanShader rewrites heap AS reads
// into a TLAS-device-address + OpConvertUToAccelerationStructureKHR
// path and RTPass pushes the active TLAS address as push data. Still
// needed on 615.78.08. CRAFTER_GRAPHICS_NO_AS_WORKAROUND, set to any
// value, turns it off to check a newer driver (tests/HeapASRead
// --no-workaround). Delete this flag and everything keyed on it once a
// fixed driver ships.
fix(vulkan-rt): work around NVIDIA descriptor-heap AS-read device-loss (#15) Reading an acceleration structure through VK_EXT_descriptor_heap aborts with VK_ERROR_DEVICE_LOST on NVIDIA 610.43.02 — a brand-new-extension driver fault isolated in #7 (engine setup is correct and validation-clean; images/buffers through the same heap work, and both traceRayEXT and inline rayQuery fault identically on the AS read). An acceleration structure can equally be reached by its device address via OpConvertUToAccelerationStructureKHR, which reads no descriptor and so never touches the faulting heap path. glslang has no GLSL spelling for that conversion, so VulkanShader rewrites the compiled SPIR-V at module-load time: every `OpLoad %accelStruct <heap-ptr>` becomes a load of the TLAS device address from a synthesized push-constant block followed by the convert. RTPass pushes the active frame's TLAS address into that push constant. User GLSL and example code are unchanged; acceleration structures still bind into the heap normally. The workaround is gated on Device::workaroundDescriptorHeapAS (true only on the NVIDIA proprietary driver) and confined to one fenced block in Crafter.Graphics-ShaderVulkan.cppm plus the RTPass push and the shaderInt64 feature toggle — delete those once a fixed NVIDIA driver ships and the heap AS read becomes the direct path again. Verified: VulkanTriangle ray-traces correctly on native NVIDIA (RTX 4090), validation-layer-clean, no device loss. The SPIR-V rewrite was independently validated with spirv-val on both the VulkanTriangle and Sponza raygen modules. Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
2026-06-03 01:59:54 +00:00
inline static bool workaroundDescriptorHeapAS = false;
// Count of ERROR-severity validation messages seen by the debug
// messenger callback since instance creation. The callback only
// prints (it never aborts), so tests that want to fail on a
// validation error — e.g. a malformed acceleration-structure build —
// assert this stays zero across the operation under test.
inline static std::uint32_t validationErrorCount = 0;
fix(vulkan-rt): configurable recursion depth + per-shader TLAS push for compute (#21) Two gaps in the Vulkan RT path that fault the device on the NVIDIA proprietary driver with a non-trivial pipeline (simple VulkanTriangle never hit them): 1. maxPipelineRayRecursionDepth was hardcoded to 1, so any closest-hit shader that traces a secondary ray (shadow ray — a very common pattern) recursed past the pipeline limit (UB → device fault). PipelineRTVulkan::Init now takes a maxRecursionDepth parameter (default 1, clamped to the device's maxRayRecursionDepth). 2. The NVIDIA descriptor-heap AS-read workaround rewrites every shader that reads an accelerationStructureEXT from the heap — including compute shaders — to read the TLAS device address from a push constant, but only RTPass pushed that address. A compute shader that ray-queries the TLAS (rayQueryEXT) therefore ran against an unwritten push slot → garbage AS handle → VK_ERROR_DEVICE_LOST. WorkaroundNvidiaAS::Patch now returns a per-shader PatchResult {patched, tlasPushOffset} instead of writing the clobber-prone global Device::workaroundTlasPushOffset (removed). VulkanShader stores it; ShaderBindingTableVulkan/PipelineRTVulkan carry it for RTPass, and ComputeShader tracks its own offset and pushes the caller-supplied TLAS address in Dispatch (new defaulted tlasAddress parameter), mirroring RTPass::Record. The PushConstantRewrite regression test now asserts Patch's returned patched/offset and adds two ray-querying compute-shader cases, proving the rewrite is stage-agnostic and the per-shader offset is correct. Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
2026-06-03 18:35:39 +00:00
// The byte offset of the TLAS-address member inside a patched shader's
// push-constant block is tracked per-shader (VulkanShader::tlasPushOffset),
// not here: a single global is clobbered by whichever shader was patched
// last and so cannot serve several shaders with differing push layouts
// (e.g. an RT raygen and a ray-querying compute shader). RTPass and
// ComputeShader read the offset off the pipeline they record.
2026-03-09 20:10:19 +01:00
static void CheckVkResult(VkResult result);
// ─── Pipeline cache persistence (issue #69) ─────────────────────
// LoadPipelineCache creates `pipelineCache`, seeding it from
// pipelineCachePath when the file exists *and* its header matches this
// device (PipelineCacheDataCompatible) — a stale or foreign blob is
// discarded so the driver never rejects it. Called once from
// Initialize. SavePipelineCache writes the driver's current cache blob
// back out; Initialize registers it with std::atexit so the cache is
// serialized at process shutdown without any explicit teardown call.
static void LoadPipelineCache();
static void SavePipelineCache();
// True when `data` is a VkPipelineCache blob whose header was written by
// a device with the same vendorID / deviceID / pipelineCacheUUID as
// `deviceProperties`. Pure logic over the standard 32-byte cache header,
// so it is driven directly by the PipelineCacheValidation test with no
// GPU device. A blob too short to hold the header is incompatible.
static bool PipelineCacheDataCompatible(std::span<const std::byte> data);
// Selects a memory type index from typeBits that satisfies `required`.
// When `preferred` bits are also given, a type satisfying both is
// chosen first; if none exists we fall back to required-only rather
// than throwing. Throws only when even `required` cannot be met (no
// valid memory exists for the allocation).
static std::uint32_t GetMemoryType(std::uint32_t typeBits, VkMemoryPropertyFlags required, VkMemoryPropertyFlags preferred = 0);
2026-05-02 00:03:24 +02:00
// Upload-strategy helper for a CPU-written, GPU-read buffer of `size`
// bytes. Picks *where* the buffer should live, complementing
// GetMemoryType (which picks the memory type once the strategy is set):
// true -> allocate HOST_VISIBLE | DEVICE_LOCAL, map, write straight
// into device memory (no staging buffer, no copy).
// false -> allocate pure DEVICE_LOCAL and feed it from a HOST_VISIBLE
// staging buffer + vkCmdCopyBuffer.
//
// The decision is platform-dependent and made at runtime, never
// hardcoded:
// 1. No DEVICE_LOCAL | HOST_VISIBLE type exists (no resizable BAR) ->
// false: direct writes are impossible, staging is mandatory.
// 2. ReBAR / UMA — the host-visible device-local heap is essentially
// the whole VRAM heap (>= 90% of the largest DEVICE_LOCAL heap) ->
// true: a staging copy would be pure overhead.
// 3. Small BAR window — the host-visible device-local heap is a tiny
// window on a separate heap (<< VRAM) -> true only for buffers
// small enough to fit the window's per-buffer budget; large
// buffers stage so they don't exhaust the window (issue #58).
//
// This is the *upload* path (CPU-write -> GPU-read). Readback wants
// HOST_CACHED instead (the ReBAR type is coherent-not-cached, so CPU
// reads from it are slow) — keep that path separate. A more robust
// small-window budget would consult VK_EXT_memory_budget for the
// window's *remaining* space rather than its total size; that needs the
// extension enabled and is a follow-up.
2026-06-16 20:10:23 +02:00
//
// Everything except the final size comparison depends only on
// memoryProperties, so it is derived once by CacheUploadStrategy (called
// at device creation) into the directWrite* members below; this leaves
// the per-allocation call a single bounds check.
static bool PreferDirectDeviceWrite(VkDeviceSize size);
2026-06-16 20:10:23 +02:00
// Size-independent half of PreferDirectDeviceWrite, cached from
// memoryProperties. directWriteBudget is the largest buffer that may be
// written directly: 0 when no resizable BAR exists (always stage),
// VkDeviceSize max for ReBAR/UMA (always map), or the small-window
// per-buffer cap otherwise. Recompute via CacheUploadStrategy whenever
// memoryProperties changes (device creation does this once).
inline static VkDeviceSize directWriteBudget = 0;
static void CacheUploadStrategy();
// ─── Fence-keyed deferred resource deletion (issue #101) ────────
// Since #40 dropped the per-frame vkQueueWaitIdle, frames are
// pipelined: a buffer the CPU is "done" with may still be read by the
// GPU for up to framesInFlight-1 more frames. Destroying it
// immediately (as Clear() does) is then a use-after-free. Callers that
// free a buffer which may still be in flight — VulkanBuffer::Resize's
// reallocate path, VulkanBuffer::DeferredClear — hand the handles here
// instead, tagged with the current frameCounter; ReclaimDeletions frees
// an entry only once framesInFlight frames have elapsed, by which point
// single-queue submission order guarantees all GPU work that could
feat: windows on their own clock, deferred deletion for every handle owner, staged re-uploads, and RTPass push data Window - updateInterval runs Update on the window's own clock while the compositor sends no frame callbacks (hidden, minimized): the Wayland loop is a poll() loop, and the Win32 loop sleeps until the next update. Both read the interval each pass. - SetPresentMode (FIFO, Mailbox, Immediate). A mode the surface lacks, or whose swapchain has other than numFrames images (Mesa's Mailbox has four), falls back to FIFO until the mode is set again. - AddPollFd / RemovePollFd wake the Wayland loop on the app's descriptors. - The swapchain has TRANSFER_DST where the surface allows it. - CaptureNextFrame replaces SaveFrame: it records the copy in the frame, before the present, where SaveFrame read an image already presented. A frame unwound past before the write still frees the readback buffer. - Inter-pass barriers on the swapchain image carry the access masks of the stages the passes use. Deferred deletion (issue #101) - VulkanBuffer, Mesh, VulkanShader, ComputeShader and PipelineRTVulkan hand their handles to the deletion queue from their destructors and moves, as a frame in flight may still use them; a static owner goes after the driver has torn down. VulkanShader and PipelineRTVulkan move and can't be copied; re-Load and re-Init queue the old pipeline. - The queue and its mutex are never destroyed, and the queue takes handles from any thread; Device::frameCounter is atomic. Both the queue and the staging rings count frames as one window renders them (documented). - DrainDeletions also lets every buffer's staging ring start over: without a Window nothing else reclaims either, so an app without one calls it after waiting for its work. Meshes and buffers - A Mesh rebuild queues the old acceleration structure and builds into the same storage after a barrier, so the BLAS address stays put when it fits. The refit and rebuild barriers cover compute ray queries too. - RTBuildOptions::noDuplicateAnyHit. Mesh::geometryFlags replaces opaque, and the refit and rebuild fallbacks keep the flags. - A re-upload into a buffer a frame in flight may read (UploadDeviceLocal of the same size, UploadDeviceLocalRange) copies on the GPU, after those reads, through a per-frame staging ring; on ReBAR it used to write the buffer in place. Empty uploads record nothing. - A compressed Mesh::Build that keeps its buffers decompresses into them after the reads of frames in flight, and the hit shaders and ray queries wait for the decompress as well as the BLAS build. Its vertex buffer takes a ranged refit's copies. - Mesh move-assigns (queueing the BLAS it replaces), so it can be replaced in place or erased from a vector. - VulkanBuffer::Create asks for a device address only for buffers with SHADER_DEVICE_ADDRESS usage, as vkGetBufferDeviceAddress requires; the others have address 0. UIRenderer::RegisterBuffer refuses those. - Build inputs are synchronized as shader reads. Other - RTPass pushes the app's own push data and traces at an explicit size. - DescriptorHeapVulkan hands freed slot ranges out again (they leaked), and keeps its free lists sorted. - ImageVulkan's mip chain clamps each level to 1 texel: a non-square image's shorter side went to 0. - VulkanShader throws on a missing file instead of failing in bad_alloc. - CRAFTER_GRAPHICS_DEVICE picks the GPU by its number among the distinct GPUs Vulkan lists; anything but such a number is an error. - The debug messenger prints info messages only with CRAFTER_GRAPHICS_VALIDATION set. - Windows: the WinRT gamepad code moved to a C file (mingw-w64's WinRT headers don't compile as C++), -lcombase dropped, a reference leak fixed. - project.cpp honours --local for Crafter.Event, Math and Asset, as Asset does for Math, and builds every test through one helper, projectArgs included. - Tests: DebugMessengerSeverity, DeletionQueueThreads, DeviceSelection, HeapSlotReuse, MeshLifetime, MipChainNonSquare, PipelineLifetime, RTPassPushData, SwapchainTransferDst, UploadInFlight, VulkanBufferAddress, VulkanShaderLifetime, WindowUpdateClock; others extended. Breaking: SaveFrame is CaptureNextFrame; Mesh::opaque is geometryFlags; a buffer without SHADER_DEVICE_ADDRESS usage has no address; frameCounter is std::atomic. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-20 19:07:54 +02:00
// reference it has completed. Destructors that own Vulkan handles
// (VulkanBuffer, Mesh, VulkanShader, ComputeShader, PipelineRTVulkan)
// queue them too: they can't know
// whether a frame in flight still uses the handle, and a static owner
// is destroyed after the driver and its layers have torn down, when no
// Vulkan call may run. An entry holds one kind of handle; the others
// stay VK_NULL_HANDLE.
struct PendingDeletion {
std::uint64_t retireAfter; // frameCounter value at enqueue time
feat: windows on their own clock, deferred deletion for every handle owner, staged re-uploads, and RTPass push data Window - updateInterval runs Update on the window's own clock while the compositor sends no frame callbacks (hidden, minimized): the Wayland loop is a poll() loop, and the Win32 loop sleeps until the next update. Both read the interval each pass. - SetPresentMode (FIFO, Mailbox, Immediate). A mode the surface lacks, or whose swapchain has other than numFrames images (Mesa's Mailbox has four), falls back to FIFO until the mode is set again. - AddPollFd / RemovePollFd wake the Wayland loop on the app's descriptors. - The swapchain has TRANSFER_DST where the surface allows it. - CaptureNextFrame replaces SaveFrame: it records the copy in the frame, before the present, where SaveFrame read an image already presented. A frame unwound past before the write still frees the readback buffer. - Inter-pass barriers on the swapchain image carry the access masks of the stages the passes use. Deferred deletion (issue #101) - VulkanBuffer, Mesh, VulkanShader, ComputeShader and PipelineRTVulkan hand their handles to the deletion queue from their destructors and moves, as a frame in flight may still use them; a static owner goes after the driver has torn down. VulkanShader and PipelineRTVulkan move and can't be copied; re-Load and re-Init queue the old pipeline. - The queue and its mutex are never destroyed, and the queue takes handles from any thread; Device::frameCounter is atomic. Both the queue and the staging rings count frames as one window renders them (documented). - DrainDeletions also lets every buffer's staging ring start over: without a Window nothing else reclaims either, so an app without one calls it after waiting for its work. Meshes and buffers - A Mesh rebuild queues the old acceleration structure and builds into the same storage after a barrier, so the BLAS address stays put when it fits. The refit and rebuild barriers cover compute ray queries too. - RTBuildOptions::noDuplicateAnyHit. Mesh::geometryFlags replaces opaque, and the refit and rebuild fallbacks keep the flags. - A re-upload into a buffer a frame in flight may read (UploadDeviceLocal of the same size, UploadDeviceLocalRange) copies on the GPU, after those reads, through a per-frame staging ring; on ReBAR it used to write the buffer in place. Empty uploads record nothing. - A compressed Mesh::Build that keeps its buffers decompresses into them after the reads of frames in flight, and the hit shaders and ray queries wait for the decompress as well as the BLAS build. Its vertex buffer takes a ranged refit's copies. - Mesh move-assigns (queueing the BLAS it replaces), so it can be replaced in place or erased from a vector. - VulkanBuffer::Create asks for a device address only for buffers with SHADER_DEVICE_ADDRESS usage, as vkGetBufferDeviceAddress requires; the others have address 0. UIRenderer::RegisterBuffer refuses those. - Build inputs are synchronized as shader reads. Other - RTPass pushes the app's own push data and traces at an explicit size. - DescriptorHeapVulkan hands freed slot ranges out again (they leaked), and keeps its free lists sorted. - ImageVulkan's mip chain clamps each level to 1 texel: a non-square image's shorter side went to 0. - VulkanShader throws on a missing file instead of failing in bad_alloc. - CRAFTER_GRAPHICS_DEVICE picks the GPU by its number among the distinct GPUs Vulkan lists; anything but such a number is an error. - The debug messenger prints info messages only with CRAFTER_GRAPHICS_VALIDATION set. - Windows: the WinRT gamepad code moved to a C file (mingw-w64's WinRT headers don't compile as C++), -lcombase dropped, a reference leak fixed. - project.cpp honours --local for Crafter.Event, Math and Asset, as Asset does for Math, and builds every test through one helper, projectArgs included. - Tests: DebugMessengerSeverity, DeletionQueueThreads, DeviceSelection, HeapSlotReuse, MeshLifetime, MipChainNonSquare, PipelineLifetime, RTPassPushData, SwapchainTransferDst, UploadInFlight, VulkanBufferAddress, VulkanShaderLifetime, WindowUpdateClock; others extended. Breaking: SaveFrame is CaptureNextFrame; Mesh::opaque is geometryFlags; a buffer without SHADER_DEVICE_ADDRESS usage has no address; frameCounter is std::atomic. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-20 19:07:54 +02:00
VkBuffer buffer = VK_NULL_HANDLE;
VkDeviceMemory memory = VK_NULL_HANDLE;
VkAccelerationStructureKHR accelerationStructure = VK_NULL_HANDLE;
VkShaderModule shaderModule = VK_NULL_HANDLE;
VkPipeline pipeline = VK_NULL_HANDLE;
};
// Monotonic recorded-frame index, bumped once per Window::Render. Not
// the same as Window::frameCounter (which is per-window and also drives
// the acquire-semaphore slot) — this one is global so Mesh/Device code
feat: windows on their own clock, deferred deletion for every handle owner, staged re-uploads, and RTPass push data Window - updateInterval runs Update on the window's own clock while the compositor sends no frame callbacks (hidden, minimized): the Wayland loop is a poll() loop, and the Win32 loop sleeps until the next update. Both read the interval each pass. - SetPresentMode (FIFO, Mailbox, Immediate). A mode the surface lacks, or whose swapchain has other than numFrames images (Mesa's Mailbox has four), falls back to FIFO until the mode is set again. - AddPollFd / RemovePollFd wake the Wayland loop on the app's descriptors. - The swapchain has TRANSFER_DST where the surface allows it. - CaptureNextFrame replaces SaveFrame: it records the copy in the frame, before the present, where SaveFrame read an image already presented. A frame unwound past before the write still frees the readback buffer. - Inter-pass barriers on the swapchain image carry the access masks of the stages the passes use. Deferred deletion (issue #101) - VulkanBuffer, Mesh, VulkanShader, ComputeShader and PipelineRTVulkan hand their handles to the deletion queue from their destructors and moves, as a frame in flight may still use them; a static owner goes after the driver has torn down. VulkanShader and PipelineRTVulkan move and can't be copied; re-Load and re-Init queue the old pipeline. - The queue and its mutex are never destroyed, and the queue takes handles from any thread; Device::frameCounter is atomic. Both the queue and the staging rings count frames as one window renders them (documented). - DrainDeletions also lets every buffer's staging ring start over: without a Window nothing else reclaims either, so an app without one calls it after waiting for its work. Meshes and buffers - A Mesh rebuild queues the old acceleration structure and builds into the same storage after a barrier, so the BLAS address stays put when it fits. The refit and rebuild barriers cover compute ray queries too. - RTBuildOptions::noDuplicateAnyHit. Mesh::geometryFlags replaces opaque, and the refit and rebuild fallbacks keep the flags. - A re-upload into a buffer a frame in flight may read (UploadDeviceLocal of the same size, UploadDeviceLocalRange) copies on the GPU, after those reads, through a per-frame staging ring; on ReBAR it used to write the buffer in place. Empty uploads record nothing. - A compressed Mesh::Build that keeps its buffers decompresses into them after the reads of frames in flight, and the hit shaders and ray queries wait for the decompress as well as the BLAS build. Its vertex buffer takes a ranged refit's copies. - Mesh move-assigns (queueing the BLAS it replaces), so it can be replaced in place or erased from a vector. - VulkanBuffer::Create asks for a device address only for buffers with SHADER_DEVICE_ADDRESS usage, as vkGetBufferDeviceAddress requires; the others have address 0. UIRenderer::RegisterBuffer refuses those. - Build inputs are synchronized as shader reads. Other - RTPass pushes the app's own push data and traces at an explicit size. - DescriptorHeapVulkan hands freed slot ranges out again (they leaked), and keeps its free lists sorted. - ImageVulkan's mip chain clamps each level to 1 texel: a non-square image's shorter side went to 0. - VulkanShader throws on a missing file instead of failing in bad_alloc. - CRAFTER_GRAPHICS_DEVICE picks the GPU by its number among the distinct GPUs Vulkan lists; anything but such a number is an error. - The debug messenger prints info messages only with CRAFTER_GRAPHICS_VALIDATION set. - Windows: the WinRT gamepad code moved to a C file (mingw-w64's WinRT headers don't compile as C++), -lcombase dropped, a reference leak fixed. - project.cpp honours --local for Crafter.Event, Math and Asset, as Asset does for Math, and builds every test through one helper, projectArgs included. - Tests: DebugMessengerSeverity, DeletionQueueThreads, DeviceSelection, HeapSlotReuse, MeshLifetime, MipChainNonSquare, PipelineLifetime, RTPassPushData, SwapchainTransferDst, UploadInFlight, VulkanBufferAddress, VulkanShaderLifetime, WindowUpdateClock; others extended. Breaking: SaveFrame is CaptureNextFrame; Mesh::opaque is geometryFlags; a buffer without SHADER_DEVICE_ADDRESS usage has no address; frameCounter is std::atomic. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-20 19:07:54 +02:00
// can enqueue too, not just the window loop. Atomic: a destructor on
// another thread reads it as it queues. It counts frames as one
// window renders them: with several windows rendering, framesInFlight
// of its counts pass sooner than each window's frames in flight, so
// the deletion queue and the staging rings assume one window.
inline static std::atomic<std::uint64_t> frameCounter = 0;
// = Window::numFrames; set at window init. The number of frames that
// must elapse after an enqueue before the resource is safe to free.
// Zero until set, which only makes ReclaimDeletions more eager — and it
// is never called before a Window (which sets this) starts rendering.
inline static std::uint8_t framesInFlight = 0;
// A deque, not a vector: frameCounter is monotonic, so retireAfter is
// non-decreasing down the queue and the entries ready to reclaim are
// always a prefix. ReclaimDeletions pops that prefix off the front in
feat: windows on their own clock, deferred deletion for every handle owner, staged re-uploads, and RTPass push data Window - updateInterval runs Update on the window's own clock while the compositor sends no frame callbacks (hidden, minimized): the Wayland loop is a poll() loop, and the Win32 loop sleeps until the next update. Both read the interval each pass. - SetPresentMode (FIFO, Mailbox, Immediate). A mode the surface lacks, or whose swapchain has other than numFrames images (Mesa's Mailbox has four), falls back to FIFO until the mode is set again. - AddPollFd / RemovePollFd wake the Wayland loop on the app's descriptors. - The swapchain has TRANSFER_DST where the surface allows it. - CaptureNextFrame replaces SaveFrame: it records the copy in the frame, before the present, where SaveFrame read an image already presented. A frame unwound past before the write still frees the readback buffer. - Inter-pass barriers on the swapchain image carry the access masks of the stages the passes use. Deferred deletion (issue #101) - VulkanBuffer, Mesh, VulkanShader, ComputeShader and PipelineRTVulkan hand their handles to the deletion queue from their destructors and moves, as a frame in flight may still use them; a static owner goes after the driver has torn down. VulkanShader and PipelineRTVulkan move and can't be copied; re-Load and re-Init queue the old pipeline. - The queue and its mutex are never destroyed, and the queue takes handles from any thread; Device::frameCounter is atomic. Both the queue and the staging rings count frames as one window renders them (documented). - DrainDeletions also lets every buffer's staging ring start over: without a Window nothing else reclaims either, so an app without one calls it after waiting for its work. Meshes and buffers - A Mesh rebuild queues the old acceleration structure and builds into the same storage after a barrier, so the BLAS address stays put when it fits. The refit and rebuild barriers cover compute ray queries too. - RTBuildOptions::noDuplicateAnyHit. Mesh::geometryFlags replaces opaque, and the refit and rebuild fallbacks keep the flags. - A re-upload into a buffer a frame in flight may read (UploadDeviceLocal of the same size, UploadDeviceLocalRange) copies on the GPU, after those reads, through a per-frame staging ring; on ReBAR it used to write the buffer in place. Empty uploads record nothing. - A compressed Mesh::Build that keeps its buffers decompresses into them after the reads of frames in flight, and the hit shaders and ray queries wait for the decompress as well as the BLAS build. Its vertex buffer takes a ranged refit's copies. - Mesh move-assigns (queueing the BLAS it replaces), so it can be replaced in place or erased from a vector. - VulkanBuffer::Create asks for a device address only for buffers with SHADER_DEVICE_ADDRESS usage, as vkGetBufferDeviceAddress requires; the others have address 0. UIRenderer::RegisterBuffer refuses those. - Build inputs are synchronized as shader reads. Other - RTPass pushes the app's own push data and traces at an explicit size. - DescriptorHeapVulkan hands freed slot ranges out again (they leaked), and keeps its free lists sorted. - ImageVulkan's mip chain clamps each level to 1 texel: a non-square image's shorter side went to 0. - VulkanShader throws on a missing file instead of failing in bad_alloc. - CRAFTER_GRAPHICS_DEVICE picks the GPU by its number among the distinct GPUs Vulkan lists; anything but such a number is an error. - The debug messenger prints info messages only with CRAFTER_GRAPHICS_VALIDATION set. - Windows: the WinRT gamepad code moved to a C file (mingw-w64's WinRT headers don't compile as C++), -lcombase dropped, a reference leak fixed. - project.cpp honours --local for Crafter.Event, Math and Asset, as Asset does for Math, and builds every test through one helper, projectArgs included. - Tests: DebugMessengerSeverity, DeletionQueueThreads, DeviceSelection, HeapSlotReuse, MeshLifetime, MipChainNonSquare, PipelineLifetime, RTPassPushData, SwapchainTransferDst, UploadInFlight, VulkanBufferAddress, VulkanShaderLifetime, WindowUpdateClock; others extended. Breaking: SaveFrame is CaptureNextFrame; Mesh::opaque is geometryFlags; a buffer without SHADER_DEVICE_ADDRESS usage has no address; frameCounter is std::atomic. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-20 19:07:54 +02:00
// O(ready) instead of walking and compacting the whole queue. Never
// destroyed: static owners queue their handles from their destructors
// at exit, and those may run after this queue's own destructor would.
inline static std::deque<PendingDeletion>& deletionQueue = *new std::deque<PendingDeletion>();
// Held by the calls below: handle owners go on whichever thread drops
// them (a loader's, say) while the render loop reclaims. Never
// destroyed, for the same reason as the queue.
inline static std::mutex& deletionMutex = *new std::mutex();
// Tag {buffer, memory} for deletion after framesInFlight more frames.
// A VK_NULL_HANDLE buffer is ignored (nothing to free).
static void EnqueueDeletion(VkBuffer buffer, VkDeviceMemory memory);
feat: windows on their own clock, deferred deletion for every handle owner, staged re-uploads, and RTPass push data Window - updateInterval runs Update on the window's own clock while the compositor sends no frame callbacks (hidden, minimized): the Wayland loop is a poll() loop, and the Win32 loop sleeps until the next update. Both read the interval each pass. - SetPresentMode (FIFO, Mailbox, Immediate). A mode the surface lacks, or whose swapchain has other than numFrames images (Mesa's Mailbox has four), falls back to FIFO until the mode is set again. - AddPollFd / RemovePollFd wake the Wayland loop on the app's descriptors. - The swapchain has TRANSFER_DST where the surface allows it. - CaptureNextFrame replaces SaveFrame: it records the copy in the frame, before the present, where SaveFrame read an image already presented. A frame unwound past before the write still frees the readback buffer. - Inter-pass barriers on the swapchain image carry the access masks of the stages the passes use. Deferred deletion (issue #101) - VulkanBuffer, Mesh, VulkanShader, ComputeShader and PipelineRTVulkan hand their handles to the deletion queue from their destructors and moves, as a frame in flight may still use them; a static owner goes after the driver has torn down. VulkanShader and PipelineRTVulkan move and can't be copied; re-Load and re-Init queue the old pipeline. - The queue and its mutex are never destroyed, and the queue takes handles from any thread; Device::frameCounter is atomic. Both the queue and the staging rings count frames as one window renders them (documented). - DrainDeletions also lets every buffer's staging ring start over: without a Window nothing else reclaims either, so an app without one calls it after waiting for its work. Meshes and buffers - A Mesh rebuild queues the old acceleration structure and builds into the same storage after a barrier, so the BLAS address stays put when it fits. The refit and rebuild barriers cover compute ray queries too. - RTBuildOptions::noDuplicateAnyHit. Mesh::geometryFlags replaces opaque, and the refit and rebuild fallbacks keep the flags. - A re-upload into a buffer a frame in flight may read (UploadDeviceLocal of the same size, UploadDeviceLocalRange) copies on the GPU, after those reads, through a per-frame staging ring; on ReBAR it used to write the buffer in place. Empty uploads record nothing. - A compressed Mesh::Build that keeps its buffers decompresses into them after the reads of frames in flight, and the hit shaders and ray queries wait for the decompress as well as the BLAS build. Its vertex buffer takes a ranged refit's copies. - Mesh move-assigns (queueing the BLAS it replaces), so it can be replaced in place or erased from a vector. - VulkanBuffer::Create asks for a device address only for buffers with SHADER_DEVICE_ADDRESS usage, as vkGetBufferDeviceAddress requires; the others have address 0. UIRenderer::RegisterBuffer refuses those. - Build inputs are synchronized as shader reads. Other - RTPass pushes the app's own push data and traces at an explicit size. - DescriptorHeapVulkan hands freed slot ranges out again (they leaked), and keeps its free lists sorted. - ImageVulkan's mip chain clamps each level to 1 texel: a non-square image's shorter side went to 0. - VulkanShader throws on a missing file instead of failing in bad_alloc. - CRAFTER_GRAPHICS_DEVICE picks the GPU by its number among the distinct GPUs Vulkan lists; anything but such a number is an error. - The debug messenger prints info messages only with CRAFTER_GRAPHICS_VALIDATION set. - Windows: the WinRT gamepad code moved to a C file (mingw-w64's WinRT headers don't compile as C++), -lcombase dropped, a reference leak fixed. - project.cpp honours --local for Crafter.Event, Math and Asset, as Asset does for Math, and builds every test through one helper, projectArgs included. - Tests: DebugMessengerSeverity, DeletionQueueThreads, DeviceSelection, HeapSlotReuse, MeshLifetime, MipChainNonSquare, PipelineLifetime, RTPassPushData, SwapchainTransferDst, UploadInFlight, VulkanBufferAddress, VulkanShaderLifetime, WindowUpdateClock; others extended. Breaking: SaveFrame is CaptureNextFrame; Mesh::opaque is geometryFlags; a buffer without SHADER_DEVICE_ADDRESS usage has no address; frameCounter is std::atomic. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-20 19:07:54 +02:00
// The same for an acceleration structure, a shader module and a
// pipeline. Queue an acceleration structure before the buffer it lives
// in: entries are freed in queue order. A VK_NULL_HANDLE is ignored.
static void EnqueueDeletion(VkAccelerationStructureKHR accelerationStructure);
static void EnqueueDeletion(VkShaderModule shaderModule);
static void EnqueueDeletion(VkPipeline pipeline);
// Free every entry whose retire frame has been reached
// (retireAfter + framesInFlight <= frameCounter). Call once per frame
// after waiting that frame's fence.
static void ReclaimDeletions();
feat: windows on their own clock, deferred deletion for every handle owner, staged re-uploads, and RTPass push data Window - updateInterval runs Update on the window's own clock while the compositor sends no frame callbacks (hidden, minimized): the Wayland loop is a poll() loop, and the Win32 loop sleeps until the next update. Both read the interval each pass. - SetPresentMode (FIFO, Mailbox, Immediate). A mode the surface lacks, or whose swapchain has other than numFrames images (Mesa's Mailbox has four), falls back to FIFO until the mode is set again. - AddPollFd / RemovePollFd wake the Wayland loop on the app's descriptors. - The swapchain has TRANSFER_DST where the surface allows it. - CaptureNextFrame replaces SaveFrame: it records the copy in the frame, before the present, where SaveFrame read an image already presented. A frame unwound past before the write still frees the readback buffer. - Inter-pass barriers on the swapchain image carry the access masks of the stages the passes use. Deferred deletion (issue #101) - VulkanBuffer, Mesh, VulkanShader, ComputeShader and PipelineRTVulkan hand their handles to the deletion queue from their destructors and moves, as a frame in flight may still use them; a static owner goes after the driver has torn down. VulkanShader and PipelineRTVulkan move and can't be copied; re-Load and re-Init queue the old pipeline. - The queue and its mutex are never destroyed, and the queue takes handles from any thread; Device::frameCounter is atomic. Both the queue and the staging rings count frames as one window renders them (documented). - DrainDeletions also lets every buffer's staging ring start over: without a Window nothing else reclaims either, so an app without one calls it after waiting for its work. Meshes and buffers - A Mesh rebuild queues the old acceleration structure and builds into the same storage after a barrier, so the BLAS address stays put when it fits. The refit and rebuild barriers cover compute ray queries too. - RTBuildOptions::noDuplicateAnyHit. Mesh::geometryFlags replaces opaque, and the refit and rebuild fallbacks keep the flags. - A re-upload into a buffer a frame in flight may read (UploadDeviceLocal of the same size, UploadDeviceLocalRange) copies on the GPU, after those reads, through a per-frame staging ring; on ReBAR it used to write the buffer in place. Empty uploads record nothing. - A compressed Mesh::Build that keeps its buffers decompresses into them after the reads of frames in flight, and the hit shaders and ray queries wait for the decompress as well as the BLAS build. Its vertex buffer takes a ranged refit's copies. - Mesh move-assigns (queueing the BLAS it replaces), so it can be replaced in place or erased from a vector. - VulkanBuffer::Create asks for a device address only for buffers with SHADER_DEVICE_ADDRESS usage, as vkGetBufferDeviceAddress requires; the others have address 0. UIRenderer::RegisterBuffer refuses those. - Build inputs are synchronized as shader reads. Other - RTPass pushes the app's own push data and traces at an explicit size. - DescriptorHeapVulkan hands freed slot ranges out again (they leaked), and keeps its free lists sorted. - ImageVulkan's mip chain clamps each level to 1 texel: a non-square image's shorter side went to 0. - VulkanShader throws on a missing file instead of failing in bad_alloc. - CRAFTER_GRAPHICS_DEVICE picks the GPU by its number among the distinct GPUs Vulkan lists; anything but such a number is an error. - The debug messenger prints info messages only with CRAFTER_GRAPHICS_VALIDATION set. - Windows: the WinRT gamepad code moved to a C file (mingw-w64's WinRT headers don't compile as C++), -lcombase dropped, a reference leak fixed. - project.cpp honours --local for Crafter.Event, Math and Asset, as Asset does for Math, and builds every test through one helper, projectArgs included. - Tests: DebugMessengerSeverity, DeletionQueueThreads, DeviceSelection, HeapSlotReuse, MeshLifetime, MipChainNonSquare, PipelineLifetime, RTPassPushData, SwapchainTransferDst, UploadInFlight, VulkanBufferAddress, VulkanShaderLifetime, WindowUpdateClock; others extended. Breaking: SaveFrame is CaptureNextFrame; Mesh::opaque is geometryFlags; a buffer without SHADER_DEVICE_ADDRESS usage has no address; frameCounter is std::atomic. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-20 19:07:54 +02:00
// Free every queued entry unconditionally, and let every buffer's
// staging ring start over. Call only after a wait-idle (resize /
// teardown), when no in-flight GPU work can reference them. Without a
// Window nothing else reclaims them: an app that has none calls this
// after waiting for its work, or both only grow.
static void DrainDeletions();
feat: windows on their own clock, deferred deletion for every handle owner, staged re-uploads, and RTPass push data Window - updateInterval runs Update on the window's own clock while the compositor sends no frame callbacks (hidden, minimized): the Wayland loop is a poll() loop, and the Win32 loop sleeps until the next update. Both read the interval each pass. - SetPresentMode (FIFO, Mailbox, Immediate). A mode the surface lacks, or whose swapchain has other than numFrames images (Mesa's Mailbox has four), falls back to FIFO until the mode is set again. - AddPollFd / RemovePollFd wake the Wayland loop on the app's descriptors. - The swapchain has TRANSFER_DST where the surface allows it. - CaptureNextFrame replaces SaveFrame: it records the copy in the frame, before the present, where SaveFrame read an image already presented. A frame unwound past before the write still frees the readback buffer. - Inter-pass barriers on the swapchain image carry the access masks of the stages the passes use. Deferred deletion (issue #101) - VulkanBuffer, Mesh, VulkanShader, ComputeShader and PipelineRTVulkan hand their handles to the deletion queue from their destructors and moves, as a frame in flight may still use them; a static owner goes after the driver has torn down. VulkanShader and PipelineRTVulkan move and can't be copied; re-Load and re-Init queue the old pipeline. - The queue and its mutex are never destroyed, and the queue takes handles from any thread; Device::frameCounter is atomic. Both the queue and the staging rings count frames as one window renders them (documented). - DrainDeletions also lets every buffer's staging ring start over: without a Window nothing else reclaims either, so an app without one calls it after waiting for its work. Meshes and buffers - A Mesh rebuild queues the old acceleration structure and builds into the same storage after a barrier, so the BLAS address stays put when it fits. The refit and rebuild barriers cover compute ray queries too. - RTBuildOptions::noDuplicateAnyHit. Mesh::geometryFlags replaces opaque, and the refit and rebuild fallbacks keep the flags. - A re-upload into a buffer a frame in flight may read (UploadDeviceLocal of the same size, UploadDeviceLocalRange) copies on the GPU, after those reads, through a per-frame staging ring; on ReBAR it used to write the buffer in place. Empty uploads record nothing. - A compressed Mesh::Build that keeps its buffers decompresses into them after the reads of frames in flight, and the hit shaders and ray queries wait for the decompress as well as the BLAS build. Its vertex buffer takes a ranged refit's copies. - Mesh move-assigns (queueing the BLAS it replaces), so it can be replaced in place or erased from a vector. - VulkanBuffer::Create asks for a device address only for buffers with SHADER_DEVICE_ADDRESS usage, as vkGetBufferDeviceAddress requires; the others have address 0. UIRenderer::RegisterBuffer refuses those. - Build inputs are synchronized as shader reads. Other - RTPass pushes the app's own push data and traces at an explicit size. - DescriptorHeapVulkan hands freed slot ranges out again (they leaked), and keeps its free lists sorted. - ImageVulkan's mip chain clamps each level to 1 texel: a non-square image's shorter side went to 0. - VulkanShader throws on a missing file instead of failing in bad_alloc. - CRAFTER_GRAPHICS_DEVICE picks the GPU by its number among the distinct GPUs Vulkan lists; anything but such a number is an error. - The debug messenger prints info messages only with CRAFTER_GRAPHICS_VALIDATION set. - Windows: the WinRT gamepad code moved to a C file (mingw-w64's WinRT headers don't compile as C++), -lcombase dropped, a reference leak fixed. - project.cpp honours --local for Crafter.Event, Math and Asset, as Asset does for Math, and builds every test through one helper, projectArgs included. - Tests: DebugMessengerSeverity, DeletionQueueThreads, DeviceSelection, HeapSlotReuse, MeshLifetime, MipChainNonSquare, PipelineLifetime, RTPassPushData, SwapchainTransferDst, UploadInFlight, VulkanBufferAddress, VulkanShaderLifetime, WindowUpdateClock; others extended. Breaking: SaveFrame is CaptureNextFrame; Mesh::opaque is geometryFlags; a buffer without SHADER_DEVICE_ADDRESS usage has no address; frameCounter is std::atomic. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-20 19:07:54 +02:00
// How many DrainDeletions have run: the staging rings start over
// when it changes.
inline static std::atomic<std::uint64_t> drains = 0;
2026-05-02 00:03:24 +02:00
// ─── Wayland key repeat ────────────────────────────────────────
2026-05-12 00:24:48 +02:00
// TickKeyRepeats fires onRawKeyDown / onRawKeyHold / onTextInput on
// the focused window for whichever key is currently repeating.
// Called once per frame from Window::Render. KeyRepeatState lives
// at namespace scope so its member initializers don't trip C++'s
// "complete-type-needed" rule for the inline static below.
2026-05-02 00:03:24 +02:00
#ifdef CRAFTER_GRAPHICS_WINDOW_WAYLAND
inline static KeyRepeatState keyRepeat;
static void TickKeyRepeats();
static void TickKeyRepeats(std::chrono::steady_clock::time_point now); // the clock injected, for the tests
2026-05-02 00:03:24 +02:00
#else
static void TickKeyRepeats() {}
#endif
2026-05-18 02:07:48 +02:00
#else // CRAFTER_GRAPHICS_WINDOW_DOM
// DOM mode: Device collapses to just `Initialize()` (currently a
// no-op since the JS runtime initializes itself). The function is
// kept so user code calling `Device::Initialize()` still compiles
// cross-platform. Browser key repeat is delivered through the
// KeyboardEvent.repeat flag directly — no manual synthesis.
static void TickKeyRepeats() {}
fix(font-atlas): give glyphs that find no room a cell when the next frame begins, evicting the least recently drawn The atlas packed glyphs onto shelves and never gave a place back (#55). Once its 1024x1024 image was full, EnsureAndGet returned nullptr for each new glyph before caching anything, so every new string holding one rasterised it again, and ShapeText dropped it together with its advance: the rest of the line closed up over the gap, and stayed that way. FontAtlas - A glyph is measured, not rasterised, until it gets a cell. One that finds no free room keeps its advance and box and draws blank for a frame. EnsureAndGet always returns the glyph, now a reference; Glyph::Resident() says whether it has a cell, Glyph::handle which one (slot and generation). - Evictions wait for the next frame: its first EnsureAndGet, Touch or Update places the glyphs that waited, once every glyph the frame before drew has been marked. Evicting while a frame is still asking for glyphs took ones it drew further down, which then took others in turn: a page switched back to could rasterise most of itself again rather than what it was missing, and on the DOM backend an over-full view could do so every frame. - A glyph keeps its cell for framesInFlight + 1 frames after it was drawn. The frame loop waits frame u's fence at the start of frame u + framesInFlight, but Device::frameCounter reads that from the end of the frame before, and input callbacks and onBeforeUpdate run in between. - FontAtlas::Packer places glyphs. Until the rows run out it fills shelves exactly as before. Then a glyph takes the same columns of one shelf at least as tall, or of several in a row as tall together, the free rows below the last shelf counting as one more: free texels, and, when the frame begins, glyphs nothing in flight draws, the least recently drawn first. A glyph spanning shelves lets in one taller than every shelf, and a few glyphs drawn every frame pin only their columns. Merging whole shelves instead, tried first, left a random workload without room for a glyph while a median 43% of the atlas was in flight; spanning, at 65%. - A box that found no room in a frame is remembered, with every box at least as big, as a small set per mode, until the frame ends or (without eviction) something is evicted: a saturated atlas no longer searches every shelf for each of its missing glyphs. - An evicted glyph is forgotten, and its texels are zeroed and uploaded with the glyph that takes its place: a neighbour's filtering at its edge must read padding or zero, not a stale glyph. - Touch(handles) marks every glyph whose handle still holds its cell, and says whether all did. InvalidateFont(font) forgets the font's glyphs and those waiting for a cell, whose Font* the next frame would read. The glyph index is looked up at rasterisation, so a Font replaced in place never gets one past its glyphs. GlyphsRasterized() counts, for tests. UIRenderer - A shaped run keeps a handle per glyph it draws. A hit touches them all, so the atlas keeps what is drawn every frame, and shapes the run again when one was evicted or one had no room. A glyph without room draws blank but advances the pen. - InvalidateFont clears the atlas's glyphs of the font too, as its comment and AGENTS.md always said it did. - Record calls the atlas's Update before onBuild, every frame, so the waiting glyphs are placed and uploaded first. Dispatch uploads the atlas before any kernel, a Tier 1 one sampling FontAtlasImageSlot() included: a glyph placed earlier in onBuild can sit in a cell another glyph had. DispatchText and DispatchFused no longer do it themselves. Device, Window (DOM) - Device::frameCounter and Device::framesInFlight exist on the DOM backend too, the counter bumped at the end of Window::Render, with one frame in flight: a frame's writeTexture lands before that frame's submit but after every earlier one. HelloUI - One set of item buffers and slots per frame in flight, by frameIdx. A frame still on the GPU read the items the next frame's onBuild was rewriting; with cells changing hands, those can name cells it drew other glyphs in. Tests - FontAtlasPacker: the packer with made-up boxes and no device: the fill order, least recently drawn first, framesInFlight + 1 frames held, nothing without evict, wide glyphs, tall glyphs spanning shelves and the free rows, Touch past a stale handle, stale handles, dirty spans, and the invariants over a random workload placed the way the atlas places. - FontAtlasEviction: fills the atlas through ShapeText frame by frame. The line that no longer fits keeps its advance and its glyphs' places; its missing glyphs aren't rasterised while nothing can go; when only line 0 is cold, only its glyphs give way; a frame's own misses evict nothing in it; a frame later the line and twenty more are whole, pixel for pixel, line 0 losing a larger share than the glyphs drawn after it; a stale cached run is shaped again; no stale texel is left; InvalidateFont reaches the atlas. Against master, with its two calls to the new API stubbed out, an earlier version failed ten checks. - UIDispatchAtlasFlush: a custom ui.Dispatch after ShapeText leaves the atlas uploaded. Needs a compositor. - ShapeTextCache: a hit rasterises nothing; after InvalidateFont the re-shaped run draws the same, from new cells. Docs - README and AGENTS.consumer.md: the atlas's eviction, the frame a glyph waits, shaping text every frame into per-frame item buffers, InvalidateFont, EnsureAndGet's signature and Lookup's lifetime, and that the atlas, like the deletion queue, assumes one rendering window and evicts nothing without one. - AGENTS.md: FontAtlas::Packer among the pure-logic helpers, eviction without a Window, and InvalidateFont. Breaking: FontAtlas::EnsureAndGet returns const Glyph& and never fails; check Glyph::Resident() before drawing from its UVs. FontAtlas::Touch is no longer noexcept. A GlyphItem is good for the frame it was shaped for, once the atlas has filled up, and item buffers must be per frame. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-31 09:10:42 +01:00
// The frame clock, as on native: bumped at the end of every
// Window::Render. The font atlas evicts by it (#55). One frame in
// flight: a frame's queue.writeTexture runs before that frame's own
// submit but after every earlier one, so only the frame being built
// can still sample a texel the atlas writes.
inline static std::atomic<std::uint64_t> frameCounter = 0;
inline static std::uint8_t framesInFlight = 1;
2026-05-18 02:07:48 +02:00
#endif
2026-03-09 20:10:19 +01:00
};
feat(device): applications ask for the device features their shaders use Device - Device::Initialize(required, optional) takes DeviceFeatures, the five core feature structs vulkan10 to vulkan14. The library enables only what its own shaders and code use. Device::enabledFeatures says what was enabled, supportedFeatures what the GPU has, apiVersion the version in use. - shaderInt16, shaderFloat16, storageBuffer16BitAccess, scalarBlockLayout and the uniform-buffer, storage-buffer and storage-image non-uniform indexing bits are no longer enabled for everyone. An application whose shaders take uint16_t spec constants passes {.vulkan10 = {.shaderInt16 = VK_TRUE}}. scalarBlockLayout stays on with the NVIDIA workaround, whose TLAS address may sit in a struct's tail padding. - Every GPU is weighed before one is chosen: Vulkan 1.3, the swapchain and, below 1.4, maintenance5 extensions, the library's needs, the required features. One that can't run the application is skipped, saying why when it ranks higher. A CPU device runs it only when nothing else is listed, and CRAFTER_GRAPHICS_DEVICE never falls back. A GPU two drivers both list is tried through each. - When no GPU qualifies, Initialize throws naming each GPU's gaps. It destroys the instance it made, before any Wayland connection, so it can be called again asking for less. - maintenance5 is vulkan14's on a 1.4 GPU and VK_KHR_maintenance5's below. CRAFTER_GRAPHICS_NO_VULKAN_1_4 runs as a 1.3 application, to exercise that path on a 1.4 GPU. - VK_KHR_spirv_1_4 and VK_KHR_shader_float_controls, core since 1.2, are no longer enabled, nor VK_EXT_shader_atomic_float, which was enabled without its feature struct. memoryDecompression's feature bit is queried before it is enabled. Shaders - VulkanShader checks a module's capabilities, declared directly or implicitly, against the enabled core features, the GPU's subgroup operations and the optional groups, and throws naming the field to pass, or saying the GPU lacks it. The tables are generated from vk.xml and SPIR-V's grammar. - WorkaroundNvidiaAS::Patch sized a row-major matrix push member by its columns, so the TLAS address could land inside it. Window - The swapchain format is one whose own feature flags allow storage reads and writes without a format, which since Vulkan 1.3 each format says for itself; shaderStorageImageRead/WriteWithoutFormat are no longer required. Tests - DeviceFeatures: ResolveFeatures, ChooseDevice and CheckShaderCapabilities, without a GPU. - FeatureRequests: required and optional features end to end, the 1.3 path, and a retry after a refusal. - PushConstantRewrite: a row-major mat3x4 push member. - HeapASRead, HeapDynamicIndexHit, RTPassPushData and the ray-tracing examples ask for the features their shaders use. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-31 06:40:02 +01:00
}
#ifndef CRAFTER_GRAPHICS_WINDOW_DOM
// Not exported: shared by Device's implementation units.
namespace Crafter::DeviceDetail {
// `parts` one after another, `separator` between them, for gap lists.
std::string Join(std::span<const std::string> parts, std::string_view separator);
}
#endif