2026-07-22 18:09:06 +02:00
|
|
|
|
//SPDX-License-Identifier: LGPL-3.0-only
|
|
|
|
|
|
//SPDX-FileCopyrightText: Copyright (C) 2026 Catcrafts®
|
2026-05-02 21:08:20 +02:00
|
|
|
|
|
|
|
|
|
|
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;
|
2026-05-18 04:58:52 +02:00
|
|
|
|
import :GraphicsTypes;
|
2026-05-02 21:08:20 +02:00
|
|
|
|
import std;
|
|
|
|
|
|
|
|
|
|
|
|
using namespace Crafter;
|
|
|
|
|
|
|
|
|
|
|
|
// ─── Initialize ─────────────────────────────────────────────────────────
|
|
|
|
|
|
|
2026-05-18 04:58:52 +02:00
|
|
|
|
void UIRenderer::Initialize(Window& window, GraphicsDescriptorHeap& heap, GraphicsCommandBuffer initCmd,
|
2026-05-02 21:08:20 +02:00
|
|
|
|
std::filesystem::path quadsSpv,
|
|
|
|
|
|
std::filesystem::path circlesSpv,
|
|
|
|
|
|
std::filesystem::path imagesSpv,
|
2026-06-16 17:03:24 +00:00
|
|
|
|
std::filesystem::path textSpv,
|
|
|
|
|
|
std::filesystem::path fusedSpv) {
|
2026-05-02 21:08:20 +02:00
|
|
|
|
window_ = &window;
|
|
|
|
|
|
heap_ = &heap;
|
|
|
|
|
|
|
2026-06-16 17:03:24 +00:00
|
|
|
|
// Load the four standard pipelines plus the fused uber-kernel (issue #47).
|
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
|
|
|
|
drawQuads.Load(quadsSpv, heap);
|
|
|
|
|
|
drawCircles.Load(circlesSpv, heap);
|
|
|
|
|
|
drawImages.Load(imagesSpv, heap);
|
|
|
|
|
|
drawText.Load(textSpv, heap);
|
|
|
|
|
|
drawFused.Load(fusedSpv, heap);
|
2026-05-02 21:08:20 +02:00
|
|
|
|
|
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
|
|
|
|
// 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.
|
2026-05-02 21:08:20 +02:00
|
|
|
|
auto outRange = heap_->AllocateImageSlots(1);
|
2026-05-05 00:02:04 +02:00
|
|
|
|
outImageSlot_ = ImageSlot{heap_, outRange.firstElement};
|
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
|
|
|
|
heap_->AddSwapchainSlot(outRange.firstElement, window);
|
2026-05-02 21:08:20 +02:00
|
|
|
|
|
|
|
|
|
|
// 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);
|
2026-05-05 00:02:04 +02:00
|
|
|
|
fontAtlasImageSlot_ = ImageSlot{heap_, atlasImg.firstElement};
|
2026-05-02 21:08:20 +02:00
|
|
|
|
fontAtlasSamplerSlot_ = RegisterLinearClampSampler();
|
|
|
|
|
|
WriteFontAtlasDescriptor();
|
|
|
|
|
|
}
|
|
|
|
|
|
|
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
|
|
|
|
// No whole-heap flush here: WriteFontAtlasDescriptor / RegisterSampler each
|
|
|
|
|
|
// flush their own written byte range above, so the GPU already sees
|
|
|
|
|
|
// everything written during setup.
|
2026-05-12 00:24:48 +02:00
|
|
|
|
|
2026-05-02 21:08:20 +02:00
|
|
|
|
(void)initCmd; // reserved for future image-layout tweaks
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// ─── per-frame Record ───────────────────────────────────────────────────
|
|
|
|
|
|
|
2026-05-18 04:58:52 +02:00
|
|
|
|
void UIRenderer::Record(GraphicsCommandBuffer cmd, std::uint32_t frameIdx, Window& window) {
|
2026-05-02 21:08:20 +02:00
|
|
|
|
// 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.
|
|
|
|
|
|
|
2026-05-18 04:58:52 +02:00
|
|
|
|
void UIRenderer::DispatchQuads(GraphicsCommandBuffer cmd, std::uint32_t bufferSlot,
|
2026-05-02 21:08:20 +02:00
|
|
|
|
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);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2026-05-18 04:58:52 +02:00
|
|
|
|
void UIRenderer::DispatchCircles(GraphicsCommandBuffer cmd, std::uint32_t bufferSlot,
|
2026-05-02 21:08:20 +02:00
|
|
|
|
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);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2026-05-18 04:58:52 +02:00
|
|
|
|
void UIRenderer::DispatchImages(GraphicsCommandBuffer cmd, std::uint32_t bufferSlot,
|
2026-05-02 21:08:20 +02:00
|
|
|
|
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);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2026-05-18 04:58:52 +02:00
|
|
|
|
void UIRenderer::DispatchText(GraphicsCommandBuffer cmd, std::uint32_t bufferSlot,
|
2026-05-02 21:08:20 +02:00
|
|
|
|
std::uint32_t itemCount,
|
|
|
|
|
|
std::array<float,4> clipRectPx) {
|
|
|
|
|
|
if (itemCount == 0) return;
|
2026-05-05 00:02:04 +02:00
|
|
|
|
if (!fontAtlasImageSlot_) {
|
2026-05-02 21:08:20 +02:00
|
|
|
|
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);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2026-06-16 17:03:24 +00:00
|
|
|
|
// ─── 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_;
|
2026-06-16 17:09:06 +00:00
|
|
|
|
// 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);
|
2026-06-16 17:03:24 +00:00
|
|
|
|
pc.surfaceWidth = window_->width;
|
|
|
|
|
|
pc.surfaceHeight = window_->height;
|
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
|
|
|
|
pc.frameIdx = window_->currentFrame;
|
2026-06-16 17:03:24 +00:00
|
|
|
|
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);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2026-05-02 21:08:20 +02:00
|
|
|
|
// ─── generic Dispatch (with barrier) ────────────────────────────────────
|
|
|
|
|
|
|
2026-05-18 05:39:17 +02:00
|
|
|
|
void UIRenderer::Dispatch(GraphicsCommandBuffer cmd, const GraphicsComputeShader& shader,
|
2026-05-02 21:08:20 +02:00
|
|
|
|
const void* push, std::uint32_t pushBytes,
|
|
|
|
|
|
std::uint32_t gx, std::uint32_t gy, std::uint32_t gz) {
|
|
|
|
|
|
if (!firstDispatchThisFrame_) {
|
2026-06-16 16:53:51 +00:00
|
|
|
|
// 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,
|
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
|
|
|
|
.image = window_->images[window_->currentImage],
|
2026-06-16 16:53:51 +00:00
|
|
|
|
.subresourceRange = {
|
|
|
|
|
|
.aspectMask = VK_IMAGE_ASPECT_COLOR_BIT,
|
|
|
|
|
|
.baseMipLevel = 0,
|
|
|
|
|
|
.levelCount = 1,
|
|
|
|
|
|
.baseArrayLayer = 0,
|
|
|
|
|
|
.layerCount = 1,
|
|
|
|
|
|
},
|
2026-05-02 21:08:20 +02:00
|
|
|
|
};
|
|
|
|
|
|
vkCmdPipelineBarrier(cmd,
|
|
|
|
|
|
VK_PIPELINE_STAGE_COMPUTE_SHADER_BIT,
|
|
|
|
|
|
VK_PIPELINE_STAGE_COMPUTE_SHADER_BIT,
|
2026-06-16 16:53:51 +00:00
|
|
|
|
0, 0, nullptr, 0, nullptr, 1, &ib);
|
2026-05-02 21:08:20 +02:00
|
|
|
|
}
|
|
|
|
|
|
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) {
|
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
|
|
|
|
if (!Device::descriptorHeapSupported) {
|
|
|
|
|
|
heap_->WriteImageSets(slot, VK_DESCRIPTOR_TYPE_SAMPLED_IMAGE, viewInfo, layout);
|
|
|
|
|
|
return;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2026-05-02 21:08:20 +02:00
|
|
|
|
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()
|
|
|
|
|
|
);
|
2026-06-16 18:29:24 +00:00
|
|
|
|
// 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);
|
2026-05-02 21:08:20 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
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
|
|
|
|
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;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2026-05-02 21:08:20 +02:00
|
|
|
|
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()
|
|
|
|
|
|
);
|
2026-06-16 18:29:24 +00:00
|
|
|
|
// 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);
|
2026-05-02 21:08:20 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
2026-05-05 00:02:04 +02:00
|
|
|
|
SamplerSlot UIRenderer::RegisterSampler(const VkSamplerCreateInfo& info) {
|
2026-05-02 21:08:20 +02:00
|
|
|
|
auto range = heap_->AllocateSamplerSlots(1);
|
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
|
|
|
|
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;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2026-05-02 21:08:20 +02:00
|
|
|
|
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()
|
|
|
|
|
|
);
|
2026-06-16 18:29:24 +00:00
|
|
|
|
// 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);
|
2026-05-05 00:02:04 +02:00
|
|
|
|
return SamplerSlot{heap_, range.firstElement};
|
2026-05-02 21:08:20 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
2026-05-05 00:02:04 +02:00
|
|
|
|
SamplerSlot UIRenderer::RegisterLinearClampSampler() {
|
2026-05-02 21:08:20 +02:00
|
|
|
|
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);
|
|
|
|
|
|
}
|
|
|
|
|
|
|