Skip to content

Tests: a draw from an upload-heap vertex buffer and an upload-backed descriptor table entry (D3D-54) - #648

Closed
woreksami wants to merge 9 commits into
ItsNotRexed:mainfrom
woreksami:d3d-54-upload-heap-draw
Closed

woreksami wants to merge 9 commits into
ItsNotRexed:mainfrom
woreksami:d3d-54-upload-heap-draw

Conversation

@woreksami

@woreksami woreksami commented Nov 22, 2026 •

Copy link
Copy Markdown

D3D-54 (open question 17 in docs/d3d12_vulkan.md §8.2).

Depends on: #291

What it does

d3d12_draw_test already read its index buffer and root CBVs from an upload heap, and d3d12_compute_test reads an upload buffer through a descriptor table. What no test covered: a vertex buffer in an upload heap, and an upload-backed descriptor read by a graphics stage. This adds both:

  • A fourth draw (top-right quadrant): the same quad, its vertex buffer view pointing into the upload heap, where its vertices are coloured (64, 0, 255) instead of red.
  • The root signature gains a one-CBV descriptor table (s4:s5). A shader-visible heap's two slots hold upload-heap VAs, written as raw bytes; the pixel shader multiplies its colour by a "filter" read through the table: pass-through for draws 1-3, one that drops green for draw 4. A stale vertex buffer would make the quadrant red and a stale table (64, 255, 255); only both rebinds give (64, 0, 255). Checked by removing each rebind in turn (thanks @Vonsek, whose review found that the first version couldn't tell).
  • ps_draw.frag reads the table entry as words, not floats: a biased upload VA's high dword is usually 0xffffffff, a NaN pattern a float load is allowed to change.
  • docs/d3d12_vulkan.md: row 17 and the upload/readback row record what the draw shows.

Result: the -heap_base bias works for a vertex buffer, an index buffer, a root CBV and a descriptor-table entry in an upload heap, read with full 64-bit addresses. Still open: whether the game does this at all, and any shader that forces the high dword of an address to 0 (the docs say some descriptor-table reads do) can't reach an upload buffer this way, because the biased VA's high dword is 0xffffffff.

Tested on Windows 11 Pro, NVIDIA GeForce RTX 3060, driver 610.88, MSVC (VS 18 Build Tools), RelWithDebInfo, glslang 16.6.0:

  • d3d12_draw_test: all 4096 pixels match, including the new top-right quadrant; fails as expected with either draw-4 rebind removed.
  • ctest 15/15; d3d12_compute_test, d3d12_vulkan_device_test, d3d12_descriptor_test, d3d12_present_test still pass.
  • Not run with LEONIDA_VK_VALIDATION=1 (no validation layer here).

AI-assisted (Claude Code).

🤖 Generated with Claude Code

Ryx69 and others added 8 commits November 21, 2026 23:44
- vulkan_present.h: VulkanPresenter blits a back buffer (an ordinary texture
  in D3D12's PRESENT state, layout tracked like any other) to an acquired
  swapchain image and presents it on the same VkQueue, after the
  application's work. Two frames in flight, a present semaphore per image,
  FIFO for vsync else MAILBOX/IMMEDIATE, swapchain recreated on resize or
  out-of-date/suboptimal, minimised windows skip presents.
- rt::d3d12::create_window_presenter / WindowPresenter: Win32 surface for a
  Vulkan device created with presentation; holds the device. This is the back
  end a DXGI shim or PresentX would call; the game's own front end is still
  open (docs question 6), so neither is invented here.
- Standalone device enables VK_KHR_surface/win32_surface and VK_KHR_swapchain
  when present; the test helper can too.

Test: d3d12_present_test opens a window and presents 60 frames through the
Xbox-ABI proxy (reset, PRESENT->RENDER_TARGET, clear, ->PRESENT, execute,
present, fence), resizes the window (swapchain recreated, scaled blit),
switches to unsynchronised presents, and reads the screen with GDI:
51,153,229 for a 51,153,230 clear. Clean under the Khronos validation layer,
as are the other GPU tests; 6/6 ctest.
The debug messenger in test/vk_test_device.h counted every error-severity
message, so a broken third-party implicit layer (ReShade's, reported on an
RTX 5090) failed every GPU test. Only VALIDATION-type errors are counted now;
general loader and layer messages are still printed, marked "not counted".
A real validation error still fails the test (checked by replaying part B of
vk_command_replay_test without the layout tracker). runtime/README.md says
so and mentions VK_LOADER_LAYERS_DISABLE.
First pieces of D3D-3 (compute path), no pipelines wired up yet:
- shader_library.h: FSSM extraction from the DXBC container (DXIL, XDAT
  0x0D, XBCH, zlib), SHA-256, a minimal JSON reader and the
  spirv/index.json lookup by fssm_sha256. Self-contained inflate, no
  dependencies.
- IR Dispatch with user data captured at record time; replay binds the
  pipeline, pushes the 144-byte block (s0..s31, heap base, code base) and
  dispatches. Arena buffers get a buffer_offset that copies honour.
- Root signatures use main's model (runtime/src/root_signature.h); the docs
  say so.

Tests: shader_library_test (FIPS 180-4 vectors; stored, fixed and dynamic
deflate blocks; corrupt and truncated streams; FSSM from a synthetic
container) joins ctest; command_list_test covers dispatch recording.
… compute PSOs, Dispatch)

- GPU address arena (docs 3.3), when the device has bufferDeviceAddress: one
  host-visible, device-addressable buffer (LEONIDA_ARENA_MB, default 256,
  max 4095 so VAs fit 32-bit table pointers). Default-heap buffers are
  64 KiB-aligned ranges with VA = offset, zeroed through the mapping;
  shader-visible descriptor heaps live there (CPU handle = mapping, GPU
  handle = VA); upload/readback buffers get VA = device address - heap
  base; GetGPUVirtualAddress returns these.
- CreateRootSignature on main's model (rt::rootsig): parse_rts0, then
  pack() for each parameter's place in the 32 user-data dwords; what pack()
  refuses is refused with its reason. CreateComputePipelineState (PC slot
  11, Xbox slot 62 via the proxy) finds the shader by the SHA-256 of its
  FSSM program in the translator's index (VulkanDeviceInfo::shader_dir or
  LEONIDA_SHADERS); its FSSM blob is copied into the arena as the code base.
  Shaders that declare set-0 bindings are refused with a log until the
  bindless set exists (M2).
- Command list: root signatures (arguments cleared on change), pipeline
  state, constant / root view / descriptor-table setters for compute and
  graphics, SetDescriptorHeaps, Dispatch recording the user data as it is,
  ClearState (which also resets ItsNotRexed#164's stencil references); misuse makes
  Close return E_INVALIDARG.
- Test helper and standalone device enable bufferDeviceAddress.

Test: d3d12_compute_test (through the proxy) creates the root signature
from the PC serializer's blob and the PSO through slot 62 in the measured
(desc, 0, 0, riid, out) shape (28cf235) from a container shaped like the
game's, sets root constants, a root UAV and a table whose
application-written entry points at an upload buffer, runs two dispatches
that each keep their own arguments, and reads the results back: 64/64
twice. Its shader is GLSL written to the translator's push-constant and
address contract, compiled with glslangValidator. Clean under the Khronos
validation layer, as are the other GPU tests; 13/13 ctest.
- CreateGraphicsPipelineState (PC slot 10): VS and PS found by the SHA-256
  of their FSSM programs in the translator's index. Input-layout elements are
  matched to the VS input signature by semantic name and index; location =
  the element's register (the translator's vertex-fetch rule); a layout that
  leaves an input unfed is refused, as D3D12 does. Rasterizer, blend and
  depth-stencil state map one to one; viewport, scissor, topology, vertex
  strides, stencil references and blend factor are dynamic; dynamic
  rendering takes the RTV/DSV formats. Hull/domain/geometry, stream output,
  MSAA, set-0 bindings and two code-base stages (question 14) are refused
  with a log.
- Command list: IASetPrimitiveTopology / VertexBuffers / IndexBuffer,
  RSSetViewports / ScissorRects (first of each), OMSetRenderTargets (views
  read at the call, single-range handles too), OMSetBlendFactor,
  OMSetStencilRef and CommandList8's front/back references (now applied),
  DrawInstanced / DrawIndexedInstanced recording a copy of that state.
  CreateCommandList's initial pipeline state is applied. Reset and
  ClearState share one state reset.
- Replay: vertex/index views resolve to buffers by VA (arena or upload),
  attachments move to attachment layouts through the tracker (first use
  zero-fills), the draw runs in a dynamic-rendering scope with a flipped
  viewport (translated VS write D3D clip coordinates), stencil references
  per face, then vkCmdDraw / vkCmdDrawIndexed. Render-target views take the
  pipeline's formats (the view cache keys on format).
- shader_library.h reads the index's input_signature and PS outputs.

Test: d3d12_draw_test (through the proxy) builds a VS+PS PSO whose input
layout order differs from the VS registers, uploads a vertex buffer into the
arena, reads indices from an upload heap, and draws two indexed quads with
different root constants and root CBVs plus a back-facing triangle: all 4096
read-back pixels match (which also checks the viewport flip and winding).
Shaders are GLSL written to the translator's contract. d3d12_com_test now
uses SetMarker as its unimplemented-method example, since DrawInstanced is
implemented. 13/13 ctest; all six GPU tests clean under the Khronos
validation layer.
…n (arena without Resizable BAR)

Without Resizable BAR the only DEVICE_LOCAL | HOST_VISIBLE memory type sits in a heap of 256 MiB or less
(214 MiB on an RTX 3090, driver 616.56). The 256 MiB default arena preferred that type, so vkAllocateMemory
was asked for more than the heap holds (VUID-vkAllocateMemory-pAllocateInfo-01713). The NVIDIA driver still
returned VK_SUCCESS, so the fallback to plain host-visible memory never ran, and d3d12_compute_test and
d3d12_draw_test failed under validation. Reported by CharlyRien.

memory_type_order (vulkan_backend.h) now leaves out every type whose heap is smaller than the allocation,
so the preference falls through to system memory there. DeviceState::allocate uses it for every allocation.
d3d12_state_map_test checks the order on that card's heap layout and with Resizable BAR on.
… stages from the stage table

Review feedback from IAmNotSiob:
- Both PSO paths declared a 144-byte push-constant range without consulting ItsNotRexed#224's limit. On a device with
  maxPushConstantsSize 128 (the Vulkan minimum) that breaks VUID-VkPushConstantRange-size-00298 and returns a
  bare E_FAIL. Both paths now refuse with E_NOTIMPL and log the limit until the uniform-buffer tail exists.
  d3d12_compute_test checks this with a device that reports 128 bytes.
- The graphics range's stage flags are now the whole stage table create_graphics accepts, kept in
  GraphicsPipeline::push_stages, and the replay pushes with the same flags. A geometry or mesh stage added to
  the table is covered without another change.

docs/d3d12_vulkan.md section 5.2 now counts code-base readers per stage with the current translator: of the 662
shaders with s_getpc_b64, 53 VS, 92 PS, 246 MS and 263 CS read the code base. VS+PS pairs are only known at
PSO creation.
…descriptor table entry (D3D-54)

d3d12_draw_test gets a fourth draw (top-right, yellow) whose vertex buffer is read straight from an upload heap,
and the pixel shader reads a filter through a descriptor table whose entries are upload-heap VAs. Both go through
the -heap_base bias (open question 17); the table entry is read as words, since a biased VA's high dword is
usually 0xffffffff, a NaN pattern.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@evilclanker9000

Copy link
Copy Markdown
Contributor

The original pixel oracle still passes when either or both draw-4 rebinds are omitted. On f2dc19011928dc38762978b2cd03a82da1175f1f, RTX 5080 / 616.92 with Vulkan validation, the stock executable passes all 4096 pixels. In a local copy of the fixture I omitted draw 4's IASetVertexBuffers, then its SetGraphicsRootDescriptorTable, then both. All three variants still matched the original 4096 pixels. The upload and arena vertices are identical; their blue channel is zero, the only channel on which the two filters differ.

I changed only the synthetic input so the two bindings have independent visible effects: set R=64, B=255 in the upload vertices at +1536, leave the arena vertices red, keep the slot-0 filter (1,1,1), and set the slot-1 filter at +1280 to (1,0,1). Using the same compiled shaders and runtime, all four runs matched these predicted RGB values throughout the 32×32 top-right quadrant (alpha 255); the other 3072 pixels matched the stock reference in every run.

Draw-4 VB Draw-4 table Top-right RGB
previous arena VB previous slot 0 (255,255,0)
previous arena VB slot 1 (255,0,0)
upload VB previous slot 0 (64,255,255)
upload VB slot 1 (64,0,255)

A separate offset control kept both rebind calls active and changed only the address stored in slot 1 from upload VA +1280 to +1024. Exactly those 1024 pixels changed to (64,255,255). All runs produced zero Vulkan validation errors.

This exercises the runtime's graphics PSO, IA vertex-buffer binding, graphics root descriptor table, indexed draw, queue and texture readback. The table entry holds an application-written 64-bit VA, which the fixture's handwritten pixel shader dereferences. A packed view descriptor and a translated game shader would be separate tests. Using the two distinct inputs and (64,0,255) in the checked-in oracle would catch either stale binding.

…view on ItsNotRexed#648)

The upload vertices were the arena vertices and the two filters differed only in blue, which those red vertices
don't have, so the quadrant came out the same with either rebind left out (found by @Vonsek). The upload
vertices are now (64, 0, 255) and the second filter drops green: a stale vertex buffer gives red, a stale table
gives (64, 255, 255), and only both rebinds give (64, 0, 255). Checked by removing each rebind in turn.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@woreksami

woreksami commented Nov 22, 2026 •

Copy link
Copy Markdown
Author

@Vonsek thanks, you're right: the upload vertices were the arena vertices and the filters only differed in blue, so neither rebind was really checked. Fixed in 1591fdd along the lines you suggested: the upload vertices are now (64, 0, 255) and the slot-1 filter is (1, 0, 1), so the top-right quadrant must be (64, 0, 255).

Re-run on an RTX 3060 (driver 610.88): the stock test matches all 4096 pixels. With draw 4's IASetVertexBuffers removed it fails with (255, 0, 0) at (32, 0), and with its SetGraphicsRootDescriptorTable removed it fails with (64, 255, 255), matching your table.

@woreksami

Copy link
Copy Markdown
Author

Superseded by #653 (claimed first); its descriptor-table case is offered there.

@woreksami woreksami closed this Nov 22, 2026
VictorJulianiR added a commit to VictorJulianiR/Leonida that referenced this pull request Nov 22, 2026
Adapt the graphics descriptor-table fixture from woreksami's ItsNotRexed#648
(f2dc190 and
1591fdd). Vonsek's review identified
the need for an observable difference when an earlier binding is reused.
Keep the existing vertex, index and root tint offsets and place both
filters in separate aligned regions of the same upload allocation.

Co-authored-by: woreksami <249861314+woreksami@users.noreply.github.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants