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>
This commit is contained in:
parent
6f471ca6b1
commit
234fcce099
29 changed files with 1378 additions and 550 deletions
|
|
@ -42,6 +42,12 @@ const char* const instanceExtensionNames[] = {
|
|||
"VK_KHR_wayland_surface"
|
||||
#endif
|
||||
};
|
||||
namespace {
|
||||
// The surface_maintenance1 Initialize enabled on the instance, the KHR
|
||||
// form or the EXT one, or null. A device's swapchain_maintenance1 has to
|
||||
// be the same form: each requires its own.
|
||||
const char* instanceSurfaceMaintenance1 = nullptr;
|
||||
}
|
||||
// Device extensions are split by what breaks without them. Only the first
|
||||
// group is needed to put UI pixels on screen, and a GPU without it is skipped,
|
||||
// as is one below Vulkan 1.4 without VK_KHR_maintenance5 (core from 1.4).
|
||||
|
|
@ -778,6 +784,31 @@ void Device::seat_handle_capabilities(void*, wl_seat* seat, uint32_t capabilitie
|
|||
|
||||
#endif
|
||||
|
||||
std::vector<std::vector<VkPhysicalDevice>> Device::DistinctGpus(VkInstance instance) {
|
||||
std::uint32_t physDeviceCount = 0;
|
||||
CheckVkResult(vkEnumeratePhysicalDevices(instance, &physDeviceCount, nullptr));
|
||||
std::vector<VkPhysicalDevice> physDevices(physDeviceCount);
|
||||
CheckVkResult(vkEnumeratePhysicalDevices(instance, &physDeviceCount, physDevices.data()));
|
||||
|
||||
std::vector<std::vector<VkPhysicalDevice>> gpus;
|
||||
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());
|
||||
const auto at = std::ranges::find(seen, uuid);
|
||||
if (at == seen.end()) {
|
||||
seen.push_back(uuid);
|
||||
gpus.push_back({ device });
|
||||
} else {
|
||||
gpus[at - seen.begin()].push_back(device);
|
||||
}
|
||||
}
|
||||
return gpus;
|
||||
}
|
||||
|
||||
namespace {
|
||||
// What Initialize learns about one GPU before choosing it.
|
||||
struct Candidate {
|
||||
|
|
@ -800,6 +831,10 @@ namespace {
|
|||
bool faultVendorBinary = false;
|
||||
bool decompression = false;
|
||||
bool workaroundAS = false;
|
||||
// VK_KHR_swapchain_maintenance1 or VK_EXT_swapchain_maintenance1,
|
||||
// whichever matches the instance's surface_maintenance1, with its
|
||||
// feature bit; null without. Becomes swapchainMaintenance1Supported.
|
||||
const char* swapchainMaintenance1 = nullptr;
|
||||
// The extensions every device needs, maintenance5 among them below
|
||||
// Vulkan 1.4. Initialize adds the groups'.
|
||||
std::vector<const char*> requiredExtensions;
|
||||
|
|
@ -880,6 +915,14 @@ namespace {
|
|||
const bool rayTracingExtensions = hasAll(rayTracingExtensionNames);
|
||||
const bool faultExtension = availableExtensions.contains(VK_EXT_DEVICE_FAULT_EXTENSION_NAME);
|
||||
const bool decompressionExtension = availableExtensions.contains(VK_EXT_MEMORY_DECOMPRESSION_EXTENSION_NAME);
|
||||
const char* swapchainMaintenance1Extension =
|
||||
instanceSurfaceMaintenance1 == nullptr ? nullptr
|
||||
: std::string_view(instanceSurfaceMaintenance1) == VK_KHR_SURFACE_MAINTENANCE_1_EXTENSION_NAME
|
||||
? VK_KHR_SWAPCHAIN_MAINTENANCE_1_EXTENSION_NAME
|
||||
: VK_EXT_SWAPCHAIN_MAINTENANCE_1_EXTENSION_NAME;
|
||||
if (swapchainMaintenance1Extension != nullptr && !availableExtensions.contains(swapchainMaintenance1Extension)) {
|
||||
swapchainMaintenance1Extension = nullptr;
|
||||
}
|
||||
DeviceFeatures& supported = candidate.supported;
|
||||
VkPhysicalDeviceMaintenance5Features maintenance5Query {
|
||||
.sType = VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_MAINTENANCE_5_FEATURES,
|
||||
|
|
@ -905,6 +948,10 @@ namespace {
|
|||
VkPhysicalDeviceMemoryDecompressionFeaturesEXT decompressionQuery {
|
||||
.sType = VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_MEMORY_DECOMPRESSION_FEATURES_EXT,
|
||||
};
|
||||
// The EXT form's struct is the same one under another name.
|
||||
VkPhysicalDeviceSwapchainMaintenance1FeaturesKHR swapchainMaintenance1Query {
|
||||
.sType = VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_SWAPCHAIN_MAINTENANCE_1_FEATURES_KHR,
|
||||
};
|
||||
VkPhysicalDeviceFeatures2 featureQuery {
|
||||
.sType = VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_FEATURES_2,
|
||||
};
|
||||
|
|
@ -935,6 +982,9 @@ namespace {
|
|||
if (decompressionExtension) {
|
||||
ask(decompressionQuery);
|
||||
}
|
||||
if (swapchainMaintenance1Extension != nullptr) {
|
||||
ask(swapchainMaintenance1Query);
|
||||
}
|
||||
vkGetPhysicalDeviceFeatures2(physical, &featureQuery);
|
||||
supported.vulkan10 = featureQuery.features;
|
||||
// The chain pointed at this function's locals.
|
||||
|
|
@ -959,6 +1009,7 @@ namespace {
|
|||
candidate.fault = faultExtension && faultQuery.deviceFault;
|
||||
candidate.faultVendorBinary = faultQuery.deviceFaultVendorBinary;
|
||||
candidate.decompression = decompressionExtension && decompressionQuery.memoryDecompression;
|
||||
candidate.swapchainMaintenance1 = swapchainMaintenance1Query.swapchainMaintenance1 ? swapchainMaintenance1Extension : nullptr;
|
||||
// NVIDIA's brand-new VK_EXT_descriptor_heap acceleration-structure read
|
||||
// path faults (see #7); enable the SPIR-V rewrite workaround there.
|
||||
// Other drivers (and any future fixed NVIDIA driver, once this gate is
|
||||
|
|
@ -1043,33 +1094,7 @@ namespace {
|
|||
// The GPU Initialize uses, or a throw naming why there is none.
|
||||
Candidate ChooseGpu(VkInstance instance, std::uint32_t instanceVersion,
|
||||
const DeviceFeatures& required, const DeviceFeatures& optional) {
|
||||
std::uint32_t physDeviceCount = 0;
|
||||
Device::CheckVkResult(vkEnumeratePhysicalDevices(instance, &physDeviceCount, nullptr));
|
||||
std::vector<VkPhysicalDevice> physDevices(physDeviceCount);
|
||||
Device::CheckVkResult(vkEnumeratePhysicalDevices(instance, &physDeviceCount, physDevices.data()));
|
||||
|
||||
// The handles, grouped by GPU: 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. The numbers below count
|
||||
// GPUs, so one means the same GPU inside a sandbox and out.
|
||||
std::vector<std::vector<VkPhysicalDevice>> gpus;
|
||||
{
|
||||
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());
|
||||
const auto at = std::ranges::find(seen, uuid);
|
||||
if (at == seen.end()) {
|
||||
seen.push_back(uuid);
|
||||
gpus.push_back({ device });
|
||||
} else {
|
||||
gpus[at - seen.begin()].push_back(device);
|
||||
}
|
||||
}
|
||||
}
|
||||
const std::vector<std::vector<VkPhysicalDevice>> gpus = Device::DistinctGpus(instance);
|
||||
// A GPU's first handle that can run the application, else its first
|
||||
// handle, gaps and all.
|
||||
const auto evaluate = [&](const std::vector<VkPhysicalDevice>& handles) {
|
||||
|
|
@ -1153,8 +1178,36 @@ void Device::Initialize(const DeviceFeatures& required, const DeviceFeatures& op
|
|||
instanceCreateInfo.sType = VK_STRUCTURE_TYPE_INSTANCE_CREATE_INFO;
|
||||
instanceCreateInfo.pNext = enableValidation ? (const void*)&validationFeatures : nullptr;
|
||||
instanceCreateInfo.pApplicationInfo = &app;
|
||||
instanceCreateInfo.enabledExtensionCount = sizeof(instanceExtensionNames) / sizeof(const char*);
|
||||
instanceCreateInfo.ppEnabledExtensionNames = instanceExtensionNames;
|
||||
|
||||
// The extensions above, plus optional ones where the loader has them:
|
||||
// surface_maintenance1 answers a surface's capabilities for one present
|
||||
// mode, through vkGetPhysicalDeviceSurfaceCapabilities2KHR, so it comes
|
||||
// with get_surface_capabilities2 or not at all. The KHR form is the EXT
|
||||
// one promoted; either will do.
|
||||
std::vector<const char*> enabledInstanceExtensions(std::begin(instanceExtensionNames), std::end(instanceExtensionNames));
|
||||
{
|
||||
std::uint32_t count = 0;
|
||||
CheckVkResult(vkEnumerateInstanceExtensionProperties(nullptr, &count, nullptr));
|
||||
std::vector<VkExtensionProperties> available(count);
|
||||
CheckVkResult(vkEnumerateInstanceExtensionProperties(nullptr, &count, available.data()));
|
||||
const auto offered = [&](const char* name) {
|
||||
return std::ranges::any_of(available, [&](const VkExtensionProperties& extension) {
|
||||
return std::strcmp(extension.extensionName, name) == 0;
|
||||
});
|
||||
};
|
||||
const char* surfaceMaintenance1 =
|
||||
offered(VK_KHR_SURFACE_MAINTENANCE_1_EXTENSION_NAME) ? VK_KHR_SURFACE_MAINTENANCE_1_EXTENSION_NAME
|
||||
: offered(VK_EXT_SURFACE_MAINTENANCE_1_EXTENSION_NAME) ? VK_EXT_SURFACE_MAINTENANCE_1_EXTENSION_NAME
|
||||
: nullptr;
|
||||
surfaceMaintenance1Supported = surfaceMaintenance1 != nullptr && offered(VK_KHR_GET_SURFACE_CAPABILITIES_2_EXTENSION_NAME);
|
||||
instanceSurfaceMaintenance1 = surfaceMaintenance1Supported ? surfaceMaintenance1 : nullptr;
|
||||
if (surfaceMaintenance1Supported) {
|
||||
enabledInstanceExtensions.push_back(VK_KHR_GET_SURFACE_CAPABILITIES_2_EXTENSION_NAME);
|
||||
enabledInstanceExtensions.push_back(surfaceMaintenance1);
|
||||
}
|
||||
}
|
||||
instanceCreateInfo.enabledExtensionCount = static_cast<std::uint32_t>(enabledInstanceExtensions.size());
|
||||
instanceCreateInfo.ppEnabledExtensionNames = enabledInstanceExtensions.data();
|
||||
|
||||
size_t foundInstanceLayers = 0;
|
||||
|
||||
|
|
@ -1182,6 +1235,14 @@ void Device::Initialize(const DeviceFeatures& required, const DeviceFeatures& op
|
|||
}
|
||||
|
||||
CheckVkResult(vkCreateInstance(&instanceCreateInfo, NULL, &instance));
|
||||
if (surfaceMaintenance1Supported) {
|
||||
vkGetPhysicalDeviceSurfaceCapabilities2KHR = reinterpret_cast<PFN_vkGetPhysicalDeviceSurfaceCapabilities2KHR>(
|
||||
vkGetInstanceProcAddr(instance, "vkGetPhysicalDeviceSurfaceCapabilities2KHR"));
|
||||
surfaceMaintenance1Supported = vkGetPhysicalDeviceSurfaceCapabilities2KHR != nullptr;
|
||||
if (!surfaceMaintenance1Supported) {
|
||||
instanceSurfaceMaintenance1 = nullptr;
|
||||
}
|
||||
}
|
||||
|
||||
VkDebugUtilsMessengerCreateInfoEXT debugUtilsMessengerCreateInfo = {};
|
||||
debugUtilsMessengerCreateInfo.sType = VK_STRUCTURE_TYPE_DEBUG_UTILS_MESSENGER_CREATE_INFO_EXT;
|
||||
|
|
@ -1239,6 +1300,7 @@ void Device::Initialize(const DeviceFeatures& required, const DeviceFeatures& op
|
|||
rayTracingSupported = chosen.rayTracing;
|
||||
deviceFaultSupported = chosen.fault;
|
||||
memoryDecompressionSupported = chosen.decompression;
|
||||
swapchainMaintenance1Supported = chosen.swapchainMaintenance1 != nullptr;
|
||||
workaroundDescriptorHeapAS = chosen.workaroundAS;
|
||||
shaderInt64ForWorkaroundOnly = chosen.workaroundAS
|
||||
&& required.vulkan10.shaderInt64 == VK_FALSE && optional.vulkan10.shaderInt64 == VK_FALSE;
|
||||
|
|
@ -1344,6 +1406,11 @@ void Device::Initialize(const DeviceFeatures& required, const DeviceFeatures& op
|
|||
.memoryDecompression = VK_TRUE,
|
||||
};
|
||||
|
||||
VkPhysicalDeviceSwapchainMaintenance1FeaturesKHR swapchainMaintenance1Features {
|
||||
.sType = VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_SWAPCHAIN_MAINTENANCE_1_FEATURES_KHR,
|
||||
.swapchainMaintenance1 = VK_TRUE,
|
||||
};
|
||||
|
||||
VkPhysicalDeviceDescriptorHeapFeaturesEXT desciptorHeapFeatures {
|
||||
.sType = VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_DESCRIPTOR_HEAP_FEATURES_EXT,
|
||||
.descriptorHeap = VK_TRUE,
|
||||
|
|
@ -1379,6 +1446,9 @@ void Device::Initialize(const DeviceFeatures& required, const DeviceFeatures& op
|
|||
if (memoryDecompressionSupported) {
|
||||
link(memoryDecompressionFeatures);
|
||||
}
|
||||
if (swapchainMaintenance1Supported) {
|
||||
link(swapchainMaintenance1Features);
|
||||
}
|
||||
if (rayTracingSupported) {
|
||||
link(physicalDeviceRayQueryFeatures);
|
||||
link(physicalDeviceRayTracingPipelineFeatures);
|
||||
|
|
@ -1417,6 +1487,9 @@ void Device::Initialize(const DeviceFeatures& required, const DeviceFeatures& op
|
|||
if (memoryDecompressionSupported) {
|
||||
enabledDeviceExtensions.push_back(VK_EXT_MEMORY_DECOMPRESSION_EXTENSION_NAME);
|
||||
}
|
||||
if (swapchainMaintenance1Supported) {
|
||||
enabledDeviceExtensions.push_back(chosen.swapchainMaintenance1);
|
||||
}
|
||||
|
||||
VkDeviceCreateInfo deviceCreateInfo = {};
|
||||
deviceCreateInfo.sType = VK_STRUCTURE_TYPE_DEVICE_CREATE_INFO;
|
||||
|
|
@ -1610,9 +1683,11 @@ namespace {
|
|||
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
|
||||
// submission order guarantees all GPU work from frame F is complete.
|
||||
// frame F retires at F + framesInFlight: Window::Render calls this after
|
||||
// waiting for its frame slot's fence, and that slot's last submission is
|
||||
// frame F itself once framesInFlight frames have passed, so all GPU work
|
||||
// from frame F is complete (and, by single-queue submission order,
|
||||
// everything before it).
|
||||
//
|
||||
// frameCounter is monotonic, so retireAfter is non-decreasing down the
|
||||
// queue and the ready entries are a contiguous prefix: pop them off the
|
||||
|
|
|
|||
Loading…
Reference in a new issue