Crafter.Graphics/implementations/Crafter.Graphics-UI.cpp
Jorijn van der Graaf 234fcce099
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 18:14:18 +01:00

410 lines
19 KiB
C++
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

//SPDX-License-Identifier: LGPL-3.0-only
//SPDX-FileCopyrightText: Copyright (C) 2026 Catcrafts®
module;
#include "vulkan/vulkan.h"
module Crafter.Graphics:UI_impl;
import :UI;
import :ComputeShader;
import :Device;
import :Window;
import :DescriptorHeapVulkan;
import :ImageVulkan;
import :VulkanBuffer;
import :FontAtlas;
import :Font;
import :GraphicsTypes;
import std;
using namespace Crafter;
// ─── Initialize ─────────────────────────────────────────────────────────
void UIRenderer::Initialize(Window& window, GraphicsDescriptorHeap& heap, GraphicsCommandBuffer initCmd,
std::filesystem::path quadsSpv,
std::filesystem::path circlesSpv,
std::filesystem::path imagesSpv,
std::filesystem::path textSpv,
std::filesystem::path fusedSpv) {
window_ = &window;
heap_ = &heap;
// Load the four standard pipelines plus the fused uber-kernel (issue #47).
drawQuads.Load(quadsSpv, heap);
drawCircles.Load(circlesSpv, heap);
drawImages.Load(imagesSpv, heap);
drawText.Load(textSpv, heap);
drawFused.Load(fusedSpv, heap);
// One image slot for the swapchain output. The window writes the image
// each frame draws into there, in that frame's heap copy (or set), so it
// follows resizes and present-mode changes by itself; called during a
// frame, it is written for that frame too.
auto outRange = heap_->AllocateImageSlots(1);
outImageSlot_ = ImageSlot{heap_, outRange.firstElement};
heap_->AddSwapchainSlot(outRange.firstElement, window);
// Optional font-atlas registration (user must have called atlas->Initialize
// already before reaching here, so atlas->image is live).
if (fontAtlas != nullptr) {
auto atlasImg = heap_->AllocateImageSlots(1);
fontAtlasImageSlot_ = ImageSlot{heap_, atlasImg.firstElement};
fontAtlasSamplerSlot_ = RegisterLinearClampSampler();
WriteFontAtlasDescriptor();
}
// No whole-heap flush here: WriteFontAtlasDescriptor / RegisterSampler each
// flush their own written byte range above, so the GPU already sees
// everything written during setup.
(void)initCmd; // reserved for future image-layout tweaks
}
// ─── per-frame Record ───────────────────────────────────────────────────
void UIRenderer::Record(GraphicsCommandBuffer cmd, std::uint32_t frameIdx, Window& window) {
// Reset per-frame state.
firstDispatchThisFrame_ = true;
// If text is in use, flush any glyphs that user-side ShapeText calls
// produced during a previous frame's onBuild. (Ensure() during the
// current onBuild also marks the atlas dirty; that's flushed on the
// NEXT Record. For v1 this is fine because the current frame's text
// dispatch reads whatever's already been uploaded, and brand-new glyphs
// missing from the atlas this frame will simply render blank for one
// frame and resolve next frame. To get them this frame, the user can
// call atlas->Update(cmd) themselves at the top of onBuild.)
if (fontAtlas != nullptr && fontAtlas->dirty) {
fontAtlas->Update(cmd);
}
onBuild.Invoke({cmd, frameIdx});
(void)window;
}
// ─── group-count helper ────────────────────────────────────────────────
namespace {
// Number of 8-pixel tiles needed to cover `dim` pixels (rounded up).
inline std::uint32_t TilesFor(std::uint32_t dim) {
return (dim + 7u) / 8u;
}
}
// ─── standard-shader convenience dispatches ─────────────────────────────
//
// All four standard shaders use the same pixel-tile dispatch model: one
// workgroup per 8×8 screen tile, each thread iterates every item in order
// inside the workgroup, accumulating into a local register. This guarantees
// "items in the buffer render in order" (later items overdraw earlier ones)
// without inter-workgroup races on imageLoad/imageStore — the bug that the
// per-item dispatch model had.
void UIRenderer::DispatchQuads(GraphicsCommandBuffer cmd, std::uint32_t bufferSlot,
std::uint32_t itemCount,
std::array<float,4> clipRectPx) {
if (itemCount == 0) return;
struct PC { UIDispatchHeader hdr; } pc { FillHeader(bufferSlot, itemCount, clipRectPx) };
Dispatch(cmd, drawQuads, &pc, sizeof(pc),
TilesFor(window_->width), TilesFor(window_->height), 1u);
}
void UIRenderer::DispatchCircles(GraphicsCommandBuffer cmd, std::uint32_t bufferSlot,
std::uint32_t itemCount,
std::array<float,4> clipRectPx) {
if (itemCount == 0) return;
struct PC { UIDispatchHeader hdr; } pc { FillHeader(bufferSlot, itemCount, clipRectPx) };
Dispatch(cmd, drawCircles, &pc, sizeof(pc),
TilesFor(window_->width), TilesFor(window_->height), 1u);
}
void UIRenderer::DispatchImages(GraphicsCommandBuffer cmd, std::uint32_t bufferSlot,
std::uint32_t itemCount,
std::array<float,4> clipRectPx) {
if (itemCount == 0) return;
struct PC { UIDispatchHeader hdr; } pc { FillHeader(bufferSlot, itemCount, clipRectPx) };
Dispatch(cmd, drawImages, &pc, sizeof(pc),
TilesFor(window_->width), TilesFor(window_->height), 1u);
}
void UIRenderer::DispatchText(GraphicsCommandBuffer cmd, std::uint32_t bufferSlot,
std::uint32_t itemCount,
std::array<float,4> clipRectPx) {
if (itemCount == 0) return;
if (!fontAtlasImageSlot_) {
throw std::runtime_error("UIRenderer::DispatchText: no FontAtlas registered (set fontAtlas before Initialize)");
}
// Flush any glyphs that ShapeText calls (during this onBuild) just
// rasterised, so the dispatch below sees them.
if (fontAtlas != nullptr && fontAtlas->dirty) {
fontAtlas->Update(cmd);
}
struct PC {
UIDispatchHeader hdr;
std::uint32_t fontTextureSlot;
std::uint32_t fontSamplerSlot;
std::uint32_t _p0;
std::uint32_t _p1;
} pc {
FillHeader(bufferSlot, itemCount, clipRectPx),
fontAtlasImageSlot_,
fontAtlasSamplerSlot_,
0, 0
};
Dispatch(cmd, drawText, &pc, sizeof(pc),
TilesFor(window_->width), TilesFor(window_->height), 1u);
}
// ─── fused dispatch (issue #47) ─────────────────────────────────────────
//
// One uber-kernel composites quads → circles → images → text into the output
// image with a single load and a single store. Routed through the generic
// Dispatch() so it still gets the standard write-after-write barrier when it
// is not the first dispatch of the frame, and a following dispatch barriers
// against it — the only barriers it removes are the INTRA-fuse ones a run of
// Dispatch* calls would have inserted between its categories.
void UIRenderer::DispatchFused(GraphicsCommandBuffer cmd,
const FusedBatch& quads,
const FusedBatch& circles,
const FusedBatch& images,
const FusedBatch& text) {
// Nothing to draw → no dispatch (and, crucially, no barrier bump).
if (quads.itemCount == 0 && circles.itemCount == 0 &&
images.itemCount == 0 && text.itemCount == 0) {
return;
}
if (text.itemCount != 0 && !fontAtlasImageSlot_) {
throw std::runtime_error("UIRenderer::DispatchFused: text batch given but no FontAtlas registered (set fontAtlas before Initialize)");
}
// Flush any glyphs ShapeText rasterised during this onBuild, mirroring
// DispatchText — only relevant when there is text to draw.
if (text.itemCount != 0 && fontAtlas != nullptr && fontAtlas->dirty) {
fontAtlas->Update(cmd);
}
UIFusedHeader pc{};
pc.itemBuffers[0] = quads.bufferSlot;
pc.itemBuffers[1] = circles.bufferSlot;
pc.itemBuffers[2] = images.bufferSlot;
pc.itemBuffers[3] = text.bufferSlot;
pc.itemCounts[0] = quads.itemCount;
pc.itemCounts[1] = circles.itemCount;
pc.itemCounts[2] = images.itemCount;
pc.itemCounts[3] = text.itemCount;
pc.outImage = outImageSlot_;
pc.fontTexture = fontAtlasImageSlot_;
pc.fontSampler = fontAtlasSamplerSlot_;
// Per-category clip-active bits, reusing the same surface-coverage test the
// standard path uses (ClipFlags). A category whose clip already covers the
// surface clears its bit so the kernel skips that category's per-pixel clip
// compares (the common full-surface case) — pixel-identical either way.
auto clipBit = [&](const FusedBatch& b, std::uint32_t bit) -> std::uint32_t {
return (ClipFlags(b.clipRectPx, window_->width, window_->height, 0) & kUIFlagClip) ? bit : 0u;
};
pc.flags = clipBit(quads, 0x1u) | clipBit(circles, 0x2u)
| clipBit(images, 0x4u) | clipBit(text, 0x8u);
pc.surfaceWidth = window_->width;
pc.surfaceHeight = window_->height;
pc.frameIdx = window_->currentFrame;
pc._pad = 0;
std::copy(quads.clipRectPx.begin(), quads.clipRectPx.end(), pc.clipQuads);
std::copy(circles.clipRectPx.begin(), circles.clipRectPx.end(), pc.clipCircles);
std::copy(images.clipRectPx.begin(), images.clipRectPx.end(), pc.clipImages);
std::copy(text.clipRectPx.begin(), text.clipRectPx.end(), pc.clipText);
Dispatch(cmd, drawFused, &pc, sizeof(pc),
TilesFor(window_->width), TilesFor(window_->height), 1u);
}
// ─── generic Dispatch (with barrier) ────────────────────────────────────
void UIRenderer::Dispatch(GraphicsCommandBuffer cmd, const GraphicsComputeShader& shader,
const void* push, std::uint32_t pushBytes,
std::uint32_t gx, std::uint32_t gy, std::uint32_t gz) {
if (!firstDispatchThisFrame_) {
// Every UI pass read-modify-writes the same swapchain storage image
// (later passes overdraw earlier ones), so each dispatch must observe
// the previous dispatch's writes. The only resource the hazard touches
// is that one image, so scope the dependency to its subresource with a
// VkImageMemoryBarrier rather than a queue-wide VkMemoryBarrier: same
// correctness, but only this image's shader caches are flushed —
// unrelated buffers/images are no longer invalidated. The image stays
// in VK_IMAGE_LAYOUT_GENERAL (bound as a storage image), so this is a
// pure memory dependency, no layout transition.
//
// The execution dependency is still COMPUTE->COMPUTE over the whole
// queue, so the passes do NOT overlap — this is a narrower cache flush
// only. Eliminating the barriers entirely requires fusing the passes
// into one dispatch (tracked in #47); do not attempt that here.
VkImageMemoryBarrier ib {
.sType = VK_STRUCTURE_TYPE_IMAGE_MEMORY_BARRIER,
.srcAccessMask = VK_ACCESS_SHADER_WRITE_BIT,
.dstAccessMask = VK_ACCESS_SHADER_READ_BIT | VK_ACCESS_SHADER_WRITE_BIT,
.oldLayout = VK_IMAGE_LAYOUT_GENERAL,
.newLayout = VK_IMAGE_LAYOUT_GENERAL,
.srcQueueFamilyIndex = VK_QUEUE_FAMILY_IGNORED,
.dstQueueFamilyIndex = VK_QUEUE_FAMILY_IGNORED,
.image = window_->images[window_->currentImage],
.subresourceRange = {
.aspectMask = VK_IMAGE_ASPECT_COLOR_BIT,
.baseMipLevel = 0,
.levelCount = 1,
.baseArrayLayer = 0,
.layerCount = 1,
},
};
vkCmdPipelineBarrier(cmd,
VK_PIPELINE_STAGE_COMPUTE_SHADER_BIT,
VK_PIPELINE_STAGE_COMPUTE_SHADER_BIT,
0, 0, nullptr, 0, nullptr, 1, &ib);
}
firstDispatchThisFrame_ = false;
shader.Dispatch(cmd, push, pushBytes, gx, gy, gz);
}
// ─── descriptor writes ─────────────────────────────────────────────────
void UIRenderer::WriteFontAtlasDescriptor() {
atlasViewCreateInfo_ = {
.sType = VK_STRUCTURE_TYPE_IMAGE_VIEW_CREATE_INFO,
.image = fontAtlas->image.image,
.viewType = VK_IMAGE_VIEW_TYPE_2D,
.format = VK_FORMAT_R8_UNORM,
.components = {
VK_COMPONENT_SWIZZLE_IDENTITY, VK_COMPONENT_SWIZZLE_IDENTITY,
VK_COMPONENT_SWIZZLE_IDENTITY, VK_COMPONENT_SWIZZLE_IDENTITY,
},
.subresourceRange = {
.aspectMask = VK_IMAGE_ASPECT_COLOR_BIT,
.baseMipLevel = 0,
.levelCount = 1,
.baseArrayLayer = 0,
.layerCount = 1,
},
};
WriteSampledImageDescriptor(fontAtlasImageSlot_,
atlasViewCreateInfo_,
VK_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL);
}
void UIRenderer::WriteSampledImageDescriptor(std::uint16_t slot,
const VkImageViewCreateInfo& viewInfo,
VkImageLayout layout) {
if (!Device::descriptorHeapSupported) {
heap_->WriteImageSets(slot, VK_DESCRIPTOR_TYPE_SAMPLED_IMAGE, viewInfo, layout);
return;
}
std::array<VkImageDescriptorInfoEXT, Window::numFrames> infos{};
std::array<VkResourceDescriptorInfoEXT, Window::numFrames> resources{};
std::array<VkHostAddressRangeEXT, Window::numFrames> destinations{};
for (std::uint32_t f = 0; f < Window::numFrames; ++f) {
infos[f] = {
.sType = VK_STRUCTURE_TYPE_IMAGE_DESCRIPTOR_INFO_EXT,
.pView = &viewInfo,
.layout = layout,
};
resources[f] = {
.sType = VK_STRUCTURE_TYPE_RESOURCE_DESCRIPTOR_INFO_EXT,
.type = VK_DESCRIPTOR_TYPE_SAMPLED_IMAGE,
.data = { .pImage = &infos[f] },
};
destinations[f] = {
.address = heap_->resourceHeap[f].value + heap_->ImageByteOffset(slot),
.size = Device::descriptorHeapProperties.imageDescriptorSize,
};
}
Device::vkWriteResourceDescriptorsEXT(
Device::device, Window::numFrames, resources.data(), destinations.data()
);
// One image descriptor written per heap, at the same slot offset — flush
// just that range rather than the whole heap.
const std::uint32_t off = heap_->ImageByteOffset(slot);
const std::uint32_t sz = Device::descriptorHeapProperties.imageDescriptorSize;
for (auto& h : heap_->resourceHeap) h.FlushDevice(off, sz);
}
void UIRenderer::WriteBufferDescriptor(std::uint16_t slot, VkBuffer buffer, VkDeviceAddress address, std::uint32_t size) {
if (!Device::descriptorHeapSupported) {
// A descriptor set names the buffer by handle; the heap below names
// it by device address.
heap_->WriteBufferSets(slot, buffer);
return;
}
std::array<VkDeviceAddressRangeEXT, Window::numFrames> ranges{};
std::array<VkResourceDescriptorInfoEXT, Window::numFrames> resources{};
std::array<VkHostAddressRangeEXT, Window::numFrames> destinations{};
for (std::uint32_t f = 0; f < Window::numFrames; ++f) {
ranges[f] = { .address = address, .size = size };
resources[f] = {
.sType = VK_STRUCTURE_TYPE_RESOURCE_DESCRIPTOR_INFO_EXT,
.type = VK_DESCRIPTOR_TYPE_STORAGE_BUFFER,
.data = { .pAddressRange = &ranges[f] },
};
destinations[f] = {
.address = heap_->resourceHeap[f].value + heap_->BufferByteOffset(slot),
.size = Device::descriptorHeapProperties.bufferDescriptorSize,
};
}
Device::vkWriteResourceDescriptorsEXT(
Device::device, Window::numFrames, resources.data(), destinations.data()
);
// One buffer descriptor written per heap, at the same slot offset — flush
// just that range rather than the whole heap.
const std::uint32_t off = heap_->BufferByteOffset(slot);
const std::uint32_t sz = Device::descriptorHeapProperties.bufferDescriptorSize;
for (auto& h : heap_->resourceHeap) h.FlushDevice(off, sz);
}
SamplerSlot UIRenderer::RegisterSampler(const VkSamplerCreateInfo& info) {
auto range = heap_->AllocateSamplerSlots(1);
if (!Device::descriptorHeapSupported) {
// The handle owns the slot before the sampler is created, so a
// failed vkCreateSampler returns it to the freelist.
SamplerSlot slot{heap_, range.firstElement};
heap_->WriteSamplerSets(range.firstElement, info);
return slot;
}
std::array<VkSamplerCreateInfo, Window::numFrames> infos{};
std::array<VkHostAddressRangeEXT, Window::numFrames> destinations{};
for (std::uint32_t f = 0; f < Window::numFrames; ++f) {
infos[f] = info;
destinations[f] = {
.address = heap_->samplerHeap[f].value + heap_->SamplerByteOffset(range.firstElement),
.size = Device::descriptorHeapProperties.samplerDescriptorSize,
};
}
Device::vkWriteSamplerDescriptorsEXT(
Device::device, Window::numFrames, infos.data(), destinations.data()
);
// One sampler descriptor written per heap, at the same slot offset — flush
// just that range rather than the whole sampler heap.
const std::uint32_t off = heap_->SamplerByteOffset(range.firstElement);
const std::uint32_t sz = Device::descriptorHeapProperties.samplerDescriptorSize;
for (auto& h : heap_->samplerHeap) h.FlushDevice(off, sz);
return SamplerSlot{heap_, range.firstElement};
}
SamplerSlot UIRenderer::RegisterLinearClampSampler() {
VkSamplerCreateInfo s {
.sType = VK_STRUCTURE_TYPE_SAMPLER_CREATE_INFO,
.magFilter = VK_FILTER_LINEAR,
.minFilter = VK_FILTER_LINEAR,
.mipmapMode = VK_SAMPLER_MIPMAP_MODE_LINEAR,
.addressModeU = VK_SAMPLER_ADDRESS_MODE_CLAMP_TO_EDGE,
.addressModeV = VK_SAMPLER_ADDRESS_MODE_CLAMP_TO_EDGE,
.addressModeW = VK_SAMPLER_ADDRESS_MODE_CLAMP_TO_EDGE,
.maxAnisotropy = 1.0f,
.minLod = 0.0f,
.maxLod = VK_LOD_CLAMP_NONE,
};
return RegisterSampler(s);
}