Skip to content

Runtime: M1.5 — the first draw whose VS and PS are translator output, PSO found by hash (D3D-53, Depends on #291) - #699

Closed
AlxcBTW wants to merge 14 commits into
ItsNotRexed:mainfrom
AlxcBTW:m15-translated-draw
Closed

AlxcBTW wants to merge 14 commits into
ItsNotRexed:mainfrom
AlxcBTW:m15-translated-draw

Conversation

@AlxcBTW

@AlxcBTW AlxcBTW commented Nov 22, 2026

Copy link
Copy Markdown
Contributor

Depends on: #291 (graphics draws). This is a draft. Until #291 lands, the diff also shows #291 and its merges of main, and the branch inherits #291's conflicts with main. The new commits are by AlxcBTW; 0ab1ee2 and part of 5e43f3b are fixes to #291 itself, and their hunks can move there. I'll rebuild this on main once #291 is in.

What and why

M1.5 / D3D-53, claimed in #374 after @SuarezPM released it. It is the first draw where both the VS and the PS are translator output. #335 and #291 draw with hand-written GLSL, so the contract between translator and layer had never been exercised by a draw: user SGPRs, V# vertex fetch, root constants, code base and PS exports.

  • runtime/test/shaders/vs_m15.asm (RDNA2 gfx1013, hand-written): s_loads a V# from the descriptor-table root argument in s0:s1, fetches position and colour with buffer_load_dwordx4 idxen, offsets the position by root constants and exports pos0 + param0.
  • runtime/test/shaders/ps_m15.asm (RDNA2 gfx1013, hand-written): interpolates param0, reads four floats through the code base (s_getpc_b64 → program + 0x100) and a root-constant bias, and exports mrt0.
  • Production PSO lookup:
    • build.py writes the synthetic FSSM program that a shader header asks for (; fssm-at / ; fssm-data), and the translator hashes it into index.json.
    • The test creates the PSO from that bytecode and the layer finds it by hash, the same way as for the game. A program with one byte flipped is refused with E_INVALIDARG.
    • Output for every shader without the header is byte-identical.
  • d3d12_translated_draw_test:
    • A root signature with a descriptor table and four root constants.
    • The V# is built with gfx10::encode(Buffer{…, OobSelect::structured}). It points at a DEFAULT-heap vertex buffer copied from an upload heap.
    • Three draws go into an RGBA32F target, compared bit for bit: 4064/4096 pixels are exact.
    • The 32 pixels whose centres lie on a right edge must stay the clear colour (top-left rule).
    • The expected values are derived in the .asm headers.
  • Translator: zero-based VS v0/v3 (VertexIndex - BaseVertex, InstanceIndex - BaseInstance); VS is legacy, not NGG (SHADER-33) #436 compatibility: all draws use StartVertexLocation/StartInstanceLocation 0. With Translator: zero-based VS v0/v3 (VertexIndex - BaseVertex, InstanceIndex - BaseInstance); VS is legacy, not NGG (SHADER-33) #436's translator change applied, the result is identical on both GPUs (checked, not committed). The standalone device now enables shaderDrawParameters when supported, using the same lines as Translator: zero-based VS v0/v3 (VertexIndex - BaseVertex, InstanceIndex - BaseInstance); VS is legacy, not NGG (SHADER-33) #436.

Layer bugs the draw found (both reported on #291):

  1. The dynamic vertex-stride state was declared without vertex buffers, giving VUID-vkCmdDraw-pStrides-04913 for any V#-fetching VS.
  2. shaderInt64 was never enabled. All translated shaders declare Int64, and the queue was lost on the first draw. The standalone device now logs once when the GPU lacks it.

Findings, not fixed here:

  • A V# can't point at an upload buffer as allocated today (open question 17, answered for the V# case). The V# base is 48 bits, and host_addr adds heap_base. On both GPUs here, the upload buffer's device address lay below the arena base. That gave a negative console VA (0xffffffffffce0000 on AMD, 0xffffffffffedc000 on NVIDIA), and the draw faulted. Root views and table pointers are 64-bit and fine. The safe rule is that anything a V# can address must be an arena range. This is written down in docs/d3d12_vulkan.md §3.3.
  • The translator ignores V# OOB_SELECT. A first version of this test used raw mode by mistake and still passed. It will get its own finding: and a hardware measurement.

Tested

Windows 11, MSVC 17.14 RelWithDebInfo, Vulkan SDK 1.4.363.0, LEONIDA_VK_VALIDATION=1:

AMD Radeon 880M (26.11.2) NVIDIA RTX 5070 Laptop (617.42)
d3d12_translated_draw_test pass, 4064/4096 exact + 32 edge = clear pass, same
validation messages 0 0
d3d12_draw/compute/descriptor/present/method_variants_test pass pass
ctest 28/28 28/28

An independent review re-derived every expected pixel. It also checked the asm with llvm-mc, the production lookup path, the byte-identity of build.py, and the stride fix. Its findings are fixed:

AI-assisted (Claude Code).

Checklist

  • No game data, keys or decryption (CONTRIBUTING.md), including in logs or reports quoted here.
  • A new translator rule is in the contract table of docs/design.md, with how it was confirmed. (No translator rule changes.)

🤖 Generated with Claude Code

Ryx69 and others added 14 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.
Conflicts resolved by keeping both sides: the bindless set 0 and conditional
rendering (main) next to the GPU address arena, pipelines, draws and the
presenter (ItsNotRexed#291). Device creation enables VK_EXT_conditional_rendering and
VK_KHR_swapchain from one extension list; the Vulkan function table, the
command variant (Predication, Dispatch, Draw), VulkanDeviceInfo and the test
device carry both sets of members.

ExecuteBundle's stencil_ref_set_ flag is cleared in reset_state and set in
OMSetStencilRef and OMSetFrontAndBackStencilRef. docs/d3d12_vulkan.md and the
README status rows describe both.

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
…ws), second round

Conflicts kept both sides: main's host image copy (VK_EXT_host_image_copy) and its
PresentX/frame-event docs, method-variant and sampler2 test rows next to ItsNotRexed#291's
arena, presenter, pipelines and draw tests. Device creation (the layer and the test
device) collects conditional rendering, host image copy and swapchain in one list.

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

A vertex shader that fetches its own data through a V# has no vertex buffer, and a
pipeline that declares VK_DYNAMIC_STATE_VERTEX_INPUT_BINDING_STRIDE then breaks
VUID-vkCmdDraw-pStrides-04913 on every draw (no vkCmdBindVertexBuffers2 was ever
recorded). The stride is dynamic only when there is a binding. Found by
d3d12_translated_draw_test; the draw path is PR 291's.

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

Every translated shader declares Int64 (64-bit addresses) and lists shaderInt64 in
its index metadata, but neither the standalone device nor the test device enabled
it: vkCreateShaderModule was a validation error and the first translated draw
lost the queue. The test device also enables shaderDrawParameters, which the
translator's zero-based VS ids (PR 436) will need. Two names in the test device
no longer shadow each other after the merge with main. The standalone-device
change is PR 291's.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
vs_m15.asm and ps_m15.asm are hand-written RDNA2 (gfx1013), built by
test/shaders/build.py through llvm-mc and tools/rdna2spirv.py. build.py now also
writes the synthetic FSSM program a shader's header asks for (fssm-at, fssm-data)
to fssm/, so the translator records its fssm_sha256 in index.json.
d3d12_translated_draw_test creates a graphics PSO from containers carrying those
programs; the layer finds both stages by hash in the translator's own index. It
covers a descriptor-table root argument and root constants (s0..s5), vertex fetch
through a V# the application wrote into a descriptor slot (s_load, then
buffer_load_dwordx4 idxen), the PS reading constants through the code base
(s_getpc_b64), and compares a float render target bit for bit. All draws use
StartVertexLocation 0, so the result is the same with and without PR 436's
zero-based ids; verified both ways. docs/d3d12_vulkan.md records the milestone and
the measurement that a V# cannot point at an upload buffer (48-bit base, negative
console VA).

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
… log a missing shaderInt64

PR 436's translated vertex shaders read BaseVertex/BaseInstance, so the production device
must enable shaderDrawParameters before 436 lands (same lines as 436, so they merge). A GPU
without shaderInt64 cannot load any translated shader (all declare Int64): say so once.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
…ter edge assert, softer 436 and V# docs

make_vsharp used OOB_SELECT raw with stride 32, which on hardware checks the byte offset
against num_records; the test only passed because the translator ignores OOB_SELECT. It now
encodes a gfx10::Buffer with OobSelect::structured. The 32 diagonal pixels must be the clear
colour (a right edge under the top-left rule). Docs: 436 is not in the tree (identical result
with its translator change applied); the V# failure follows from the upload buffer lying below
the arena, which is allocation order, so the rule stays: anything behind a V# is an arena range.

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

AlxcBTW commented Nov 22, 2026

Copy link
Copy Markdown
Contributor Author

Superseded by #907: the same work, rebuilt on the rebased head of the PR it depends on (the old base's commits would have shown twice here). Tested on the Radeon 880M and the RTX 5070; the results are in #907.

@AlxcBTW AlxcBTW closed this Nov 22, 2026
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.

2 participants