Crafter.Graphics/examples/Sponza
Repository files (latest commit first)
Filename Latest commit message Latest commit date
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
..
closesthit.glsl fix(rt): split Sponza's combined image sampler into a heap texture and sampler 2026-10-31 08:25:52 +01:00
closesthit.wgsl SPDX license update 2026-07-22 18:09:06 +02:00
main.cpp fix(window): size the swapchain for its present mode, and key frames in flight by frame instead of image 2026-10-31 18:14:18 +01:00
miss.glsl SPDX license update 2026-07-22 18:09:06 +02:00
miss.wgsl SPDX license update 2026-07-22 18:09:06 +02:00
project.cpp SPDX license update 2026-07-22 18:09:06 +02:00
raygen.glsl SPDX license update 2026-07-22 18:09:06 +02:00
raygen.wgsl SPDX license update 2026-07-22 18:09:06 +02:00
README.md fix(rt): split Sponza's combined image sampler into a heap texture and sampler 2026-10-31 08:25:52 +01:00
resolve.wgsl SPDX license update 2026-07-22 18:09:06 +02:00

Sponza example

Loads the Sponza atrium as a .cmesh + one albedo .ctex and renders it via ray tracing on both Vulkan (native) and WebGPU (wasm). Same main.cpp, #ifdef CRAFTER_GRAPHICS_WINDOW_DOM selects the backend.

What this example proves

  • .cmesh and .ctex decompression round-trip on both backends (GPU via VK_EXT_memory_decompression on Vulkan, CPU via Compression::DecompressCPU on WebGPU).
  • A single texture binding flowing from Image2D<RGBA8> through the RT pipeline's closest-hit on both backends. The closest-hit samples at the barycentric attribs as UVs — proof-of-binding, not visually accurate. Per-vertex UV interpolation is the next step.

The closest-hit reads its texture and its sampler through spec constants (textures[albedoSlot], samplers[samplerSlot]), not a runtime index. That is deliberate — see below.

On Vulkan the texture and the sampler are separate descriptors: a sampled image in the resource heap and a sampler in the sampler heap, joined where the closest-hit samples them, texture(sampler2D(textures[albedoSlot], samplers[samplerSlot]), uv). The descriptor heap has no combined image samplers, and glslang rejects a descriptor_heap sampler2D (#164).

Native RT limitation: dynamic descriptor_heap indexing in hit shaders

Fixed in NVIDIA 615.78.08: tests/HeapDynamicIndexHit selects buffers and textures with a runtime index in a closest-hit, including different descriptors in neighbouring lanes of a wave. This example still uses the spec-constant pattern below, which also runs on the older drivers.

On NVIDIA driver 610.43.02 (Vulkan 1.4), indexing a layout(descriptor_heap) array with a runtime (non-constant) index inside a ray-tracing hit shader aborts the device with VK_ERROR_DEVICE_LOST (an instruction-pointer / READ_INVALID device-fault) with validation off. GPU-Assisted Validation masks it — the scene runs fine under GPU-AV — which is why a validated run doesn't catch it. It is a driver-side fault, the same family as the descriptor-heap AS-read fault (#7 / #15) and the RT recursion / compute TLAS-push issues (#21 / #22), but here for plain SSBO and sampled-image descriptors read with a non-constant heap index (issue #23).

What was isolated (NVIDIA RTX 4090, driver 610.43.02)

Driving a native bindless RT scene headlessly and bisecting the closest-hit:

  • A closest-hit that reads only lightHeap[lightSlot] where lightSlot is a spec constant survives indefinitely. ✅ (This example's textures[albedoSlot] and samplers[samplerSlot] are exactly this case.)
  • Reading indexHeap[assetIndexStart + gl_InstanceCustomIndexEXT] / vertexHeap[...] — a heap index offset by a runtime value — device-losts on the first geometry hit. ❌
  • Reading a texture dynamically, textureHeap[assetColorStart + gl_InstanceCustomIndexEXT], also device-losts. ❌ So it is SSBO and sampled-image descriptors.
  • nonuniformEXT() on the dynamic index does not help.
  • The identical dynamic-heap-index pattern works fine in fragment shaders (the UI renderer indexes uiTextures[] / ui*Heap[] by per-item runtime slots), so this is RT-stage-specific, not a general descriptor_heap problem.
  • Reading a spec-constant-indexed SSBO in raygen works; only the dynamic index in the hit stage faults.

Why there is no transparent engine workaround

The AS-read fault (#15) is worked around transparently because an acceleration structure can be reached two ways: through a descriptor, or through its device address via OpConvertUToAccelerationStructureKHR (which reads no descriptor). There is exactly one TLAS, so the engine rewrites the heap AS read into an address load and feeds the address in as push data.

Neither half of that applies here:

  • Sampled images have no device-address path. A texture must be reached through a descriptor; there is no OpConvertUToImage. A dynamic heap texture index cannot be rewritten into anything that avoids dynamic descriptor selection.
  • There are many buffers, dynamically selected. SSBOs can be reached by address (buffer_reference / OpConvertUToPtr), but a per-mesh array selected by gl_InstanceCustomIndexEXT would need the engine to maintain and bind an address-table buffer and a SPIR-V rewrite far larger than the single-TLAS AS case — and it would still leave the texture half broken.

So the engine cannot paper over this the way it does the AS read. The fix is on the consumer side: avoid dynamically selecting a descriptor in a hit shader.

The fault is dynamic selection of a descriptor. Indexing within a single bound resource — an element offset into one SSBO, a layer into one array texture — is ordinary memory / layer addressing and is unaffected. So bind one resource and index inside it, rather than indexing the heap:

  • Geometry — pack all meshes' vertices/indices into a single SSBO bound at a spec-constant slot and index it by a runtime element offset, or reach each mesh's buffer via buffer_reference (a device address loaded from one bound table). Either way the descriptor is constant; only the offset/address is dynamic.

    // ❌ faults in a hit shader on NVIDIA: dynamic descriptor selection
    layout(descriptor_heap) buffer Verts { Vertex v[]; } vertexHeap[];
    Vertex vtx = vertexHeap[assetVertexStart + gl_InstanceCustomIndexEXT].v[i];
    
    // ✅ one descriptor (spec-constant slot), dynamic element offset
    layout(constant_id = 0) const uint16_t vertexSlot = 0us;
    layout(descriptor_heap) buffer Verts { Vertex v[]; } vertexHeap[];
    uint base = assetVertexStart[gl_InstanceCustomIndexEXT]; // from a bound SSBO
    Vertex vtx = vertexHeap[vertexSlot].v[base + i];
    
  • Materials / textures — put them in one texture2DArray (or a small number of arrays bucketed by format/size) bound at a spec-constant slot and index by layer:

    // ✅ one array texture (spec-constant slots), dynamic layer index
    layout(constant_id = 1) const uint16_t materialArraySlot = 0us;
    layout(constant_id = 2) const uint16_t samplerSlot = 0us;
    layout(descriptor_heap) uniform texture2DArray materials[];
    layout(descriptor_heap) uniform sampler        samplers[];
    uint layer = materialLayer[gl_InstanceCustomIndexEXT]; // from a bound SSBO
    vec3 albedo = texture(sampler2DArray(materials[materialArraySlot], samplers[samplerSlot]),
                          vec3(uv, layer)).rgb;
    

This is precisely what the WebGPU path already does — bucketed texture arrays plus a single geometry buffer — so it is a proven, cross-backend pattern, and it sidesteps the NVIDIA RT fault on the native path.

Asset fetch

project.cpp calls Crafter::GitFetch(...) on https://github.com/jimmiebergmann/Sponza (pinned to commit 222338979d32f4f4818466291bdbc29f192b86ba). The clone lands in the per-user crafter-build cache; first build pulls ~280 MB once, subsequent builds reuse it.

cfg.assets then picks two files out of that clone:

Source Compressed output
sponza.obj sponza.cmesh
textures/sponza_arch_diff.tga sponza_arch_diff.ctex

Both land flat in the example's bin directory.

Building

crafter build                          # native Vulkan
crafter build --target=wasm32-wasip1   # WebGPU / wasm

License & attribution

Sponza geometry, materials, and textures are licensed under CC BY 3.0.

When redistributing builds of this example that bundle the compressed Sponza outputs (*.cmesh, *.ctex), the CC BY 3.0 attribution requirement applies. Quoting the original credit somewhere visible to end users (about-screen, credits page, etc.) is enough.

The Crafter.Graphics library code itself is LGPL-3.0; the two licenses are compatible for data + code distribution.