feat: windows on their own clock, deferred deletion for every handle owner, staged re-uploads, and RTPass push data
Window - updateInterval runs Update on the window's own clock while the compositor sends no frame callbacks (hidden, minimized): the Wayland loop is a poll() loop, and the Win32 loop sleeps until the next update. Both read the interval each pass. - SetPresentMode (FIFO, Mailbox, Immediate). A mode the surface lacks, or whose swapchain has other than numFrames images (Mesa's Mailbox has four), falls back to FIFO until the mode is set again. - AddPollFd / RemovePollFd wake the Wayland loop on the app's descriptors. - The swapchain has TRANSFER_DST where the surface allows it. - CaptureNextFrame replaces SaveFrame: it records the copy in the frame, before the present, where SaveFrame read an image already presented. A frame unwound past before the write still frees the readback buffer. - Inter-pass barriers on the swapchain image carry the access masks of the stages the passes use. Deferred deletion (issue #101) - VulkanBuffer, Mesh, VulkanShader, ComputeShader and PipelineRTVulkan hand their handles to the deletion queue from their destructors and moves, as a frame in flight may still use them; a static owner goes after the driver has torn down. VulkanShader and PipelineRTVulkan move and can't be copied; re-Load and re-Init queue the old pipeline. - The queue and its mutex are never destroyed, and the queue takes handles from any thread; Device::frameCounter is atomic. Both the queue and the staging rings count frames as one window renders them (documented). - DrainDeletions also lets every buffer's staging ring start over: without a Window nothing else reclaims either, so an app without one calls it after waiting for its work. Meshes and buffers - A Mesh rebuild queues the old acceleration structure and builds into the same storage after a barrier, so the BLAS address stays put when it fits. The refit and rebuild barriers cover compute ray queries too. - RTBuildOptions::noDuplicateAnyHit. Mesh::geometryFlags replaces opaque, and the refit and rebuild fallbacks keep the flags. - A re-upload into a buffer a frame in flight may read (UploadDeviceLocal of the same size, UploadDeviceLocalRange) copies on the GPU, after those reads, through a per-frame staging ring; on ReBAR it used to write the buffer in place. Empty uploads record nothing. - A compressed Mesh::Build that keeps its buffers decompresses into them after the reads of frames in flight, and the hit shaders and ray queries wait for the decompress as well as the BLAS build. Its vertex buffer takes a ranged refit's copies. - Mesh move-assigns (queueing the BLAS it replaces), so it can be replaced in place or erased from a vector. - VulkanBuffer::Create asks for a device address only for buffers with SHADER_DEVICE_ADDRESS usage, as vkGetBufferDeviceAddress requires; the others have address 0. UIRenderer::RegisterBuffer refuses those. - Build inputs are synchronized as shader reads. Other - RTPass pushes the app's own push data and traces at an explicit size. - DescriptorHeapVulkan hands freed slot ranges out again (they leaked), and keeps its free lists sorted. - ImageVulkan's mip chain clamps each level to 1 texel: a non-square image's shorter side went to 0. - VulkanShader throws on a missing file instead of failing in bad_alloc. - CRAFTER_GRAPHICS_DEVICE picks the GPU by its number among the distinct GPUs Vulkan lists; anything but such a number is an error. - The debug messenger prints info messages only with CRAFTER_GRAPHICS_VALIDATION set. - Windows: the WinRT gamepad code moved to a C file (mingw-w64's WinRT headers don't compile as C++), -lcombase dropped, a reference leak fixed. - project.cpp honours --local for Crafter.Event, Math and Asset, as Asset does for Math, and builds every test through one helper, projectArgs included. - Tests: DebugMessengerSeverity, DeletionQueueThreads, DeviceSelection, HeapSlotReuse, MeshLifetime, MipChainNonSquare, PipelineLifetime, RTPassPushData, SwapchainTransferDst, UploadInFlight, VulkanBufferAddress, VulkanShaderLifetime, WindowUpdateClock; others extended. Breaking: SaveFrame is CaptureNextFrame; Mesh::opaque is geometryFlags; a buffer without SHADER_DEVICE_ADDRESS usage has no address; frameCounter is std::atomic. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
parent
449d5da553
commit
079b6b0e1f
47 changed files with 3572 additions and 1430 deletions
|
|
@ -871,7 +871,11 @@ void Device::Initialize() {
|
|||
|
||||
VkDebugUtilsMessengerCreateInfoEXT debugUtilsMessengerCreateInfo = {};
|
||||
debugUtilsMessengerCreateInfo.sType = VK_STRUCTURE_TYPE_DEBUG_UTILS_MESSENGER_CREATE_INFO_EXT;
|
||||
debugUtilsMessengerCreateInfo.messageSeverity = VK_DEBUG_UTILS_MESSAGE_SEVERITY_INFO_BIT_EXT | VK_DEBUG_UTILS_MESSAGE_SEVERITY_WARNING_BIT_EXT | VK_DEBUG_UTILS_MESSAGE_SEVERITY_ERROR_BIT_EXT;
|
||||
// Info messages (the loader reports its device sorting, the layers their
|
||||
// settings) are for whoever is debugging with validation on; without it
|
||||
// they would start every application's output.
|
||||
debugUtilsMessengerCreateInfo.messageSeverity = VK_DEBUG_UTILS_MESSAGE_SEVERITY_WARNING_BIT_EXT | VK_DEBUG_UTILS_MESSAGE_SEVERITY_ERROR_BIT_EXT
|
||||
| (enableValidation ? VK_DEBUG_UTILS_MESSAGE_SEVERITY_INFO_BIT_EXT : 0);
|
||||
debugUtilsMessengerCreateInfo.messageType = VK_DEBUG_UTILS_MESSAGE_TYPE_GENERAL_BIT_EXT | VK_DEBUG_UTILS_MESSAGE_TYPE_VALIDATION_BIT_EXT | VK_DEBUG_UTILS_MESSAGE_TYPE_PERFORMANCE_BIT_EXT;
|
||||
debugUtilsMessengerCreateInfo.pfnUserCallback = onError;
|
||||
CheckVkResult(GET_EXTENSION_FUNCTION(vkCreateDebugUtilsMessengerEXT)(instance, &debugUtilsMessengerCreateInfo, NULL, &debugMessenger));
|
||||
|
|
@ -884,7 +888,37 @@ void Device::Initialize() {
|
|||
|
||||
uint32_t bestScore = 0;
|
||||
|
||||
for (uint32_t i = 0; i < physDeviceCount; i++)
|
||||
// CRAFTER_GRAPHICS_DEVICE picks a GPU by its number among the distinct
|
||||
// ones in this list: with two of the same GPU nothing else tells them
|
||||
// apart, and the first may be too busy to use. A GPU two driver manifests
|
||||
// both list (a Flatpak sees the runtime's and the GL extension's) counts
|
||||
// once, so the number means the same GPU inside a sandbox and out.
|
||||
const char* chosenDevice = std::getenv("CRAFTER_GRAPHICS_DEVICE");
|
||||
if (chosenDevice != nullptr) {
|
||||
std::vector<VkPhysicalDevice> distinct;
|
||||
std::vector<std::array<std::uint8_t, VK_UUID_SIZE>> seen;
|
||||
for (VkPhysicalDevice device : physDevices) {
|
||||
VkPhysicalDeviceIDProperties id { .sType = VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_ID_PROPERTIES };
|
||||
VkPhysicalDeviceProperties2 properties { .sType = VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_PROPERTIES_2, .pNext = &id };
|
||||
vkGetPhysicalDeviceProperties2(device, &properties);
|
||||
std::array<std::uint8_t, VK_UUID_SIZE> uuid;
|
||||
std::ranges::copy(id.deviceUUID, uuid.begin());
|
||||
if (std::ranges::find(seen, uuid) == seen.end()) {
|
||||
seen.push_back(uuid);
|
||||
distinct.push_back(device);
|
||||
}
|
||||
}
|
||||
const std::string_view text = chosenDevice;
|
||||
std::size_t index = 0;
|
||||
const auto [end, error] = std::from_chars(text.data(), text.data() + text.size(), index);
|
||||
if (error != std::errc() || end != text.data() + text.size() || index >= distinct.size()) {
|
||||
throw std::runtime_error(std::format("CRAFTER_GRAPHICS_DEVICE={} isn't the number of a GPU: Vulkan lists {} GPU(s), 0 to {}",
|
||||
chosenDevice, distinct.size(), distinct.size() - 1));
|
||||
}
|
||||
physDevice = distinct[index];
|
||||
}
|
||||
|
||||
for (uint32_t i = 0; chosenDevice == nullptr && i < physDeviceCount; i++)
|
||||
{
|
||||
VkPhysicalDevice device = physDevices[i];
|
||||
|
||||
|
|
@ -1408,10 +1442,45 @@ void Device::EnqueueDeletion(VkBuffer buffer, VkDeviceMemory memory) {
|
|||
// Nothing to free for an already-null handle — avoids parking dead
|
||||
// entries that ReclaimDeletions would have to skip over.
|
||||
if (buffer == VK_NULL_HANDLE) return;
|
||||
deletionQueue.push_back({ frameCounter, buffer, memory });
|
||||
std::lock_guard lock(deletionMutex);
|
||||
deletionQueue.push_back({ .retireAfter = frameCounter, .buffer = buffer, .memory = memory });
|
||||
}
|
||||
|
||||
void Device::EnqueueDeletion(VkAccelerationStructureKHR accelerationStructure) {
|
||||
if (accelerationStructure == VK_NULL_HANDLE) return;
|
||||
std::lock_guard lock(deletionMutex);
|
||||
deletionQueue.push_back({ .retireAfter = frameCounter, .accelerationStructure = accelerationStructure });
|
||||
}
|
||||
|
||||
void Device::EnqueueDeletion(VkShaderModule shaderModule) {
|
||||
if (shaderModule == VK_NULL_HANDLE) return;
|
||||
std::lock_guard lock(deletionMutex);
|
||||
deletionQueue.push_back({ .retireAfter = frameCounter, .shaderModule = shaderModule });
|
||||
}
|
||||
|
||||
void Device::EnqueueDeletion(VkPipeline pipeline) {
|
||||
if (pipeline == VK_NULL_HANDLE) return;
|
||||
std::lock_guard lock(deletionMutex);
|
||||
deletionQueue.push_back({ .retireAfter = frameCounter, .pipeline = pipeline });
|
||||
}
|
||||
|
||||
namespace {
|
||||
// Free whatever one queued entry holds. The destroy and free calls accept
|
||||
// VK_NULL_HANDLE for the kinds it doesn't hold; the acceleration-structure
|
||||
// entry point is only loaded with ray tracing, so that one is guarded.
|
||||
void DestroyPending(const Device::PendingDeletion& entry) {
|
||||
if (entry.accelerationStructure != VK_NULL_HANDLE) {
|
||||
Device::vkDestroyAccelerationStructureKHR(Device::device, entry.accelerationStructure, nullptr);
|
||||
}
|
||||
vkDestroyShaderModule(Device::device, entry.shaderModule, nullptr);
|
||||
vkDestroyPipeline(Device::device, entry.pipeline, nullptr);
|
||||
vkDestroyBuffer(Device::device, entry.buffer, nullptr);
|
||||
vkFreeMemory(Device::device, entry.memory, nullptr);
|
||||
}
|
||||
}
|
||||
|
||||
void Device::ReclaimDeletions() {
|
||||
std::lock_guard lock(deletionMutex);
|
||||
// Free every entry whose retire frame has been reached. An entry tagged at
|
||||
// frame F retires at F + framesInFlight: by the time the CPU has begun that
|
||||
// many later frames (each gated by a per-image fence wait), single-queue
|
||||
|
|
@ -1423,19 +1492,18 @@ void Device::ReclaimDeletions() {
|
|||
// newer, hence also in flight). O(ready), not O(queue).
|
||||
while (!deletionQueue.empty() &&
|
||||
deletionQueue.front().retireAfter + framesInFlight <= frameCounter) {
|
||||
const PendingDeletion& entry = deletionQueue.front();
|
||||
vkDestroyBuffer(device, entry.buffer, nullptr);
|
||||
vkFreeMemory(device, entry.memory, nullptr);
|
||||
DestroyPending(deletionQueue.front());
|
||||
deletionQueue.pop_front();
|
||||
}
|
||||
}
|
||||
|
||||
void Device::DrainDeletions() {
|
||||
std::lock_guard lock(deletionMutex);
|
||||
// Unconditional: the caller has just wait-idled, so no in-flight GPU work
|
||||
// can still reference any queued resource.
|
||||
for (PendingDeletion& entry : deletionQueue) {
|
||||
vkDestroyBuffer(device, entry.buffer, nullptr);
|
||||
vkFreeMemory(device, entry.memory, nullptr);
|
||||
for (const PendingDeletion& entry : deletionQueue) {
|
||||
DestroyPending(entry);
|
||||
}
|
||||
deletionQueue.clear();
|
||||
++drains;
|
||||
}
|
||||
Loading…
Reference in a new issue