enable resets the input method's state, so sending it with every caret
move (an application keeps the cursor rectangle current while typing)
dropped the composition at each keystroke: enable now goes once per
focus and a moved caret sends its rectangle alone; disable only takes
back an enable. The compositor's leave ends both, and a window still
showing composing text hears onPreedit("") then. A finger's up and motion
go to the window it went down on, not the window of the latest down. The
seat creates its pointer and keyboard once, as it already did the touch:
capabilities come again when a device appears or goes, and a second
keyboard proxy delivered every key twice.
431 lines
No EOL
26 KiB
C++
431 lines
No EOL
26 KiB
C++
//SPDX-License-Identifier: LGPL-3.0-only
|
|
//SPDX-FileCopyrightText: Copyright (C) 2026 Catcrafts®
|
|
|
|
module;
|
|
#ifndef CRAFTER_GRAPHICS_WINDOW_DOM
|
|
#include "vulkan/vulkan.h"
|
|
#endif
|
|
#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"
|
|
#include <xkbcommon/xkbcommon.h>
|
|
#endif
|
|
export module Crafter.Graphics:Device;
|
|
import std;
|
|
import :Keys; // KeyCode for keyboard repeat state
|
|
|
|
export namespace Crafter {
|
|
struct Window;
|
|
|
|
#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;
|
|
KeyCode key = 0;
|
|
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;
|
|
};
|
|
#endif
|
|
|
|
struct Device {
|
|
static void Initialize();
|
|
|
|
#ifndef CRAFTER_GRAPHICS_WINDOW_DOM
|
|
#ifdef CRAFTER_GRAPHICS_WINDOW_WAYLAND
|
|
inline static wl_display* display = nullptr;
|
|
inline static wl_seat* seat = nullptr;
|
|
inline static xdg_wm_base* xdgWmBase = nullptr;
|
|
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;
|
|
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;
|
|
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();
|
|
// 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;
|
|
// Sub-detent scroll accumulator for PointerListenerHandleAxis.
|
|
// wl_pointer.axis values are normalized to wheel detents (15 axis
|
|
// units each, the libinput convention); whatever doesn't make a
|
|
// whole detent yet is carried here so smooth-scroll devices add up
|
|
// across events. Reset on pointer leave.
|
|
inline static double scrollDetentRemainder = 0.0;
|
|
// 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);
|
|
|
|
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);
|
|
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);
|
|
|
|
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,
|
|
};
|
|
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;
|
|
inline static PFN_vkDestroyAccelerationStructureKHR vkDestroyAccelerationStructureKHR;
|
|
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;
|
|
inline static PFN_vkCmdBindResourceHeapEXT vkCmdBindResourceHeapEXT;
|
|
inline static PFN_vkCmdBindSamplerHeapEXT vkCmdBindSamplerHeapEXT;
|
|
inline static PFN_vkWriteResourceDescriptorsEXT vkWriteResourceDescriptorsEXT;
|
|
inline static PFN_vkWriteSamplerDescriptorsEXT vkWriteSamplerDescriptorsEXT;
|
|
inline static PFN_vkCmdPushDataEXT vkCmdPushDataEXT;
|
|
inline static PFN_vkGetPhysicalDeviceDescriptorSizeEXT vkGetPhysicalDeviceDescriptorSizeEXT;
|
|
inline static PFN_vkGetDeviceFaultInfoEXT vkGetDeviceFaultInfoEXT;
|
|
|
|
// 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;
|
|
|
|
inline static VkPhysicalDeviceMemoryProperties memoryProperties;
|
|
|
|
// 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";
|
|
|
|
// ─── 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
|
|
// forces the second model on any driver, for testing.
|
|
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;
|
|
// 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();
|
|
|
|
// 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.
|
|
inline static VkPhysicalDeviceDescriptorHeapPropertiesEXT descriptorHeapProperties = {
|
|
.sType = VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_DESCRIPTOR_HEAP_PROPERTIES_EXT
|
|
};
|
|
inline static VkPhysicalDeviceRayTracingPipelinePropertiesKHR rayTracingProperties = {
|
|
.sType = VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_RAY_TRACING_PIPELINE_PROPERTIES_KHR
|
|
};
|
|
inline static VkPhysicalDeviceMemoryDecompressionPropertiesEXT memoryDecompressionProperties = {
|
|
.sType = VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_MEMORY_DECOMPRESSION_PROPERTIES_EXT
|
|
};
|
|
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. Delete
|
|
// this flag and everything keyed on it once a fixed driver ships.
|
|
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;
|
|
// 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.
|
|
|
|
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);
|
|
|
|
// 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.
|
|
//
|
|
// 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);
|
|
|
|
// 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
|
|
// reference it has completed.
|
|
struct PendingDeletion {
|
|
std::uint64_t retireAfter; // frameCounter value at enqueue time
|
|
VkBuffer buffer;
|
|
VkDeviceMemory memory;
|
|
};
|
|
// 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
|
|
// can enqueue too, not just the window loop.
|
|
inline static 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
|
|
// O(ready) instead of walking and compacting the whole queue.
|
|
inline static std::deque<PendingDeletion> deletionQueue;
|
|
// 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);
|
|
// 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();
|
|
// Free every queued entry unconditionally. Call only after a wait-idle
|
|
// (resize / teardown), when no in-flight GPU work can reference them.
|
|
static void DrainDeletions();
|
|
|
|
// ─── Wayland key repeat ────────────────────────────────────────
|
|
// 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.
|
|
#ifdef CRAFTER_GRAPHICS_WINDOW_WAYLAND
|
|
inline static KeyRepeatState keyRepeat;
|
|
static void TickKeyRepeats();
|
|
#else
|
|
static void TickKeyRepeats() {}
|
|
#endif
|
|
#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() {}
|
|
#endif
|
|
};
|
|
} |