Skip to content

GPU: mesh shaders emulated as compute + unpack vertex on SM 6.6 cards without a mesh tier - #1648

Open
yoraudev wants to merge 5 commits into
Marvisak:mainfrom
yoraudev:gpu/mesh-emulation
Open

yoraudev wants to merge 5 commits into
Marvisak:mainfrom
yoraudev:gpu/mesh-emulation

Conversation

@yoraudev

Copy link
Copy Markdown

Summary

Cards that support Shader Model 6.6 but report D3D12_MESH_SHADER_TIER_NOT_SUPPORTED (Polaris/Vega/RDNA1, Pascal, Xe-LP, WARP, …) can now run mesh stages. Nothing is vendor-specific: the path is picked from D3D12_FEATURE_SHADER_MODEL and D3D12_OPTIONS7::MeshShaderTier, and can be forced with VICTORY_MESH_EMULATION=1 (or disabled with =0).

A mesh stage is lowered to a cs_6_6 that writes packed vertices and primitives into a shared UAV, followed by a generated vs_6_6 that reads them back by SV_VertexID and drawn as a plain triangle list.

victory-shader

  • MeshMode::Emulate: HwStage::Ms lowers to [numthreads(n,1,1)] cs_6_6 with LDS instead of ms_6_6. Emulation resources live in space 2 (MESH_EMU_SPACE): u0 intermediate RWByteAddressBuffer, b0 base_group / INDIRECT_SLOT_MAP_FLAG, t0 slot map / unpack input.
  • Primitive index words start bit-31 flagged (culled); emit_indices_emu writes packed indices and the cull flag; vertex (OSG1) and per-primitive (PSG1) outputs are stored as contiguous 4-dword groups.
  • Input::MeshGroup reads base_group + local_group, or slot_map[local_group] for indirect dispatches.
  • unpack_vertex_shader builds the vs_6_6 container: culled primitives get a quiet-NaN SV_Position, and the PS input signature / PSV0 linkage (incl. nointerpolation per-primitive data and depth-only passes) is preserved.

victory-gpu

  • MeshSupport::{Native, Emulate, Unsupported} detection in host.rs.
  • New mesh_emu.rs: shared VRAM intermediate buffer (VICTORY_MESH_BUFFER_MB, default 128 MiB with a 16 MiB fallback), slot map, indirect args, and a cs_5_1 prep pass that sums/clamps group counts across ExecuteIndirect(MESH) records and builds the slot map.
  • EmulatedMeshPipeline: cached CS + cached VS/PS graphics PSO pair.
  • draw.rs: dispatch_mesh_emulated chunks direct DispatchMesh by group_cap (CS → UAV→SRV barrier → DrawInstanced → SRV→UAV); execute_indirect_mesh_emulated does clamp → prep → indirect dispatch → indirect draw. Topology and root/pipeline cache state are restored afterwards.
  • Shader cache format bumped to VSH2 with a separate shader.dxil.mesh-emu converter key.

CLI / docs

  • apps/victory translate picks MeshMode::Emulate when forced or when the device lacks mesh shaders, validates both the CS and the unpack VS, and test-builds the pipelines.
  • README and docs/architecture.md document the path and env vars.

Testing

  • cargo clippy --workspace --all-targets --locked -- -D warnings ✅
  • cargo clippy --workspace --all-targets --all-features --locked -- -D warnings ✅
  • cargo test --workspace --locked ✅
  • cargo test -p victory-shader mesh_emu: 5 passed — triangle draws on a hardware GPU and WARP, bit-31 and unexported-primitive culling, base_group / slot-map indexing, PSG1 per-primitive nointerpolation, LDS + s_barrier + ds_swizzle_b32.
  • cargo test -p victory-gpu mesh_emu: indirect prep shader builds and runs on GPU.
  • cargo xtask policy ✅

Note: dxil.dll wasn't available locally, so static validation was skipped; PSO creation and execution were verified on real hardware and WARP. The full in-game draw.rs flow wasn't exercised since game files aren't in the repo.

… without a mesh tier

Cards with Shader Model 6.6 but D3D12_MESH_SHADER_TIER_NOT_SUPPORTED (or VICTORY_MESH_EMULATION=1) now run mesh stages as a cs_6_6 that writes packed vertices and primitives to a shared UAV, followed by a generated vs_6_6 that unpacks them by SV_VertexID and culls flagged primitives with a NaN position. Direct DispatchMesh is chunked by group_cap; ExecuteIndirect(MESH) goes through a GPU prep pass that sums the group counts and builds a slot map. The shader cache moves to VSH2 with a separate mesh-emu converter key.
@EduBertozzi

Copy link
Copy Markdown
Contributor

Ran this on the hardware it targets: GTX 1060 6 GB (Pascal, SM 6.6 through the Agility runtime, no mesh shader tier), Windows 10 19045. I merged 9bfe067 onto main ffe310e (with #486 so the tests get SM 6.6 on Windows 10).

  • victory-shader: 125 passed / 0 failed (main 120).
  • victory-gpu: 51 passed / 0 failed.
  • The mesh_emu tests pass on hardware with no mesh shaders, including emulated_mesh_draws_triangle_and_culls_bit31_and_unexported, …supports_lds_barrier_and_quad_swizzle and prep_shader_builds_and_runs_indirect.

For reviewers: #961 (towards #3) covers the same ground, a mesh program lowered to cs_6_6 plus a generated VS. It has been run against the title on GPUs without mesh shaders, and that turned up problems any such path meets. Here's how they line up with this PR as I read it:

found in-game on #961 here
Records above the last lane uninitialized when max_primitives > threads (stale triangles from reused scratch) handled: index words start bit-31 culled
A mesh stage visible to ALL overlapping the pixel stage's b0/s<n>/root views, so D3D12 refuses the root signature (165 refusals on an RX 470) avoided: separate compute root signature
Scratch sized for the worst case per draw ran an 8 GB card out of memory one shared buffer, 128 MiB default (VICTORY_MESH_BUFFER_MB)
With #189: cache key and lane numbering must follow the Wave64 mode (@L1DRE, @fuzeofficlal on #961) worth checking once #189 lands

Both approaches work. Since they would conflict, it may help to settle on one and fold the other's useful parts into it. I'm glad to help whichever way the maintainers prefer. Disclosure: I wrote #961.

AI-assisted (Claude Code).

@yoraudev

Copy link
Copy Markdown
Author

Thanks for running this on the Pascal card — that's exactly the class of machine it targets, and good to see the suite green there.

Confirming the table from this side:

  • Uninitialized records past the last lane: the emulated CS prologue writes bit 31 (culled) into every primitive index word of the group slice before any lane exports, so a group that lands on scratch an earlier chunk of the shared buffer used cannot leak its triangles — also when max_primitives > threads. Vertex slots past the export are zeroed rather than left stale.
  • Root-signature overlap: the emulated CS is a separate compute root signature (the mesh stage's bindings plus the emulation's u0/b0/t0 in a dedicated register space), and the graphics half carries the pixel stage plus a vertex-visible t0 for the unpack VS. The title's mesh bindings never share a signature with the pixel stage.
  • Scratch memory: one committed buffer with 128/64/32/16 MiB fallbacks (VICTORY_MESH_BUFFER_MB), sliced per group by the program's declared max_vertices/max_primitives, and dispatches are chunked by group_cap (min(fits, 65535) groups) so one draw never depends on a worst-case-size allocation.

On the #189 row — I checked, and there are two concrete interaction points, both real:

  1. Cache key. The mesh-emu translation is a separate converter (shader.dxil.mesh-emu vs shader.dxil) keyed by build + container; that is what keeps an emulated run from serving native entries today. Shaders: emulate Wave64 compute shaders on Wave32 and narrower hosts #189 adds the wave64 mode as a runtime choice, and the build fingerprint only covers rebuilds — so once it lands, the mode has to go into the converter id/input the same way, or switching VICTORY_WAVE64_EMULATE at run time will serve a translation emitted for the other mode.
  2. Lane numbering. lower.rs still sets WaveSize(64) from the console program's wave64 flag when lane numbering or single-wave LDS is involved (console_lanes || one_wave && lds). On a 32-lane host that compute PSO is refused at CreateComputePipelineState, so the emulated mesh pipeline drops with a warning instead of running — the same rejection Shaders: emulate Wave64 compute shaders on Wave32 and narrower hosts #189 removes for plain compute. When the two are combined, emit_with should take both modes and the mesh-emu path should lower through Shaders: emulate Wave64 compute shaders on Wave32 and narrower hosts #189's wave64 handling. That is the "lane numbering follows the mode" case in the table, and it is where the two PRs genuinely need each other on 32-lane cards.

On settling on one approach — that's the maintainers' call, and happy to help either way. From this PR, the parts that would survive a fold-in regardless of direction: the GPU side (group-cap chunking, the ExecuteIndirect prep pass and slot map, the shared output buffer and its UAV/SRV state machine), and the VSH2 cache split. If #189 lands first I'm glad to rebase on it and wire the two modes together.

D3D12's Close returns E_FAIL for an already-closed list as well as for an invalid recorded command, and a world frame closed a sealed list again: the run ended at the first emulated mesh dispatch with 'a host command list did not close (0x80004005)', which read as an invalid command (frame 29347 on an RX 580, mesh d4111a6e6ea49d91 + pixel c0a7fe77b1862e10). The console API tolerates re-closing a sealed list, so the host now tracks closed-ness per list: a second close warns once and does nothing, and reset reopens it.

Reproduced with the real containers from the title's archive through new local-only tests: extract_containers pulls them out of shaders.rpf, emulated_mesh_dispatch_closes drives the direct and indirect emulated dispatches with them end to end (the failure reproduces as a double close and passes with the fix), and dump_cached_mesh_entries prints a run's cached mesh bindings.
@yoraudev

Copy link
Copy Markdown
Author

Root cause found for the world-entry crash on a mesh-less card ("a host command list did not close (Unspecified error (0x80004005)): a command recorded into it was invalid", frame 29347 on an RX 580, this branch's first emulated mesh frame), and fixed in c6ec4ad.

Diagnosis. That message was read as "an invalid command was recorded", but D3D12's Close returns E_FAIL for an already-closed list as well (the docs list both causes; our failure text only named one). The failing frame closes a sealed command list a second time — the title's re-close after its frame accounting sees the first-use dispatch drop, as far as I can tell — and the second Close ended the run.

How it was pinned down. The exact shader pair from the log (mesh d4111a6e6ea49d91 + pixel c0a7fe77b1862e10, MSNC_VS_BakeDecorations / PS_BakeDecorations) was pulled out of the title's shaders.rpf (new extract_containers test, local-only), the cached translation decoded to check its real bindings (new dump_cached_mesh_entries test), and the full emulated pipeline + dispatch replayed on the same GPU (new emulated_mesh_dispatch_closes test): the direct and indirect emulated dispatches record and close cleanly, retail and with the debug layer — closing the list a second time reproduces the run's error verbatim.

Fix. HostList now tracks closed-ness: a second close warns once (a host command list closed twice; the second close does nothing, counted for the frame line) and is a no-op, and reset reopens. The console API tolerates re-closing a sealed list; the host now does too. Genuinely invalid recorded commands still fail as before.

The three new tests are #[ignore]d and need local title files, so CI stays hermetic and the asset policy is untouched (cargo xtask policy: 156 files ok). cargo test -p victory-gpu -p victory and clippy pass. A re-run of the world entry on the RX 580 should now get past this point; the new warn line will tell us if the title re-closes per frame.

Resolve conflicts against main (70564ae): move MESH_EMU_SPACE from 2 to 3 to avoid main's MEMORY_SPACE = 2, wire emulated mesh pipelines through root_uavs/title-memory/depth-twins/atomics64/pipeline-library tag, keep non-mesh shaders on shader.dxil during translate --cache, and preserve VSH2 cache encoding.
Resolve conflicts against latest main: align indirect::clamp_only with single-group tiled clamp (Marvisak#1510), mask emulated mesh primitive vertex indices to 9 bits to drop edge flags (c0228d9 parity), pass OPTIONS19 aniso point-mip support through push_stage_params and emit::host, and keep library/host/draw/translate updates.
Resolve conflicts with view_marks (cd94d35) and hardware_adapter test updates (ccfb029): unify emit::Options with MeshMode, preserve view_marks in shader_cache VSH2 encoding and translate.rs, and wire view_marks root descriptors into both halves of EmulatedMeshPipeline and draw.rs bind_pipeline/bind_stage.

Marvisak commented Nov 25, 2026 •

Copy link
Copy Markdown
Owner

Automated triage for the maintainers.

This pull request no longer merges cleanly into main (conflicts in crates/victory-gpu/src/draw.rs, crates/victory-gpu/src/host.rs, crates/victory-gpu/src/indirect.rs, crates/victory-shader/src/emit/lower.rs, crates/victory-shader/src/emit/mod.rs, crates/victory-shader/src/emit/shader.rs). It will be reviewed once it is rebased onto the current main. The earlier verdict (needs work) was for an older state of main.


Generated by Claude Code

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants