Skip to content

feat(gpu): hold incomplete BVH builds, back structured SRVs with raw views, and sync GPU-written texture aliases - #402

Open
gdemontalivet wants to merge 3 commits into
pragyaangaur:mainfrom
gdemontalivet:feat/macos-bvh-hold-visual-fixes
Open

gdemontalivet wants to merge 3 commits into
pragyaangaur:mainfrom
gdemontalivet:feat/macos-bvh-hold-visual-fixes

Conversation

@gdemontalivet

Copy link
Copy Markdown
Contributor

Three rendering and GPU-stability issues affect Wave32 / D3DMetal hosts when acceleration-structure builders, structured-buffer descriptors, or aliased render targets run in world passes:

  1. Incomplete or Wave32-split BVH builds and downstream inline ray-query passes: Executing only a subset of the acceleration-structure builder compute pipelines—or running 64-lane builder kernels split across two 32-lane waves before full cross-lane builder support is enabled—leaves partially initialized BVH nodes in memory. Downstream inline-ray-query resolve and probe ray-activation dispatches then walk invalid node links, stalling traversal loops or writing NaN / black indirect lighting over valid direct lighting.
  2. Raw byte-offset loads against StructureByteStride > 0 SRVs: Translated scalar (s_buffer_load_*) and unindexed MUBUF loads emit rawBufferLoad on a raw descriptor handle (victory_shader::emit), whereas the title binds many constant/parameter tables as structured buffer SRVs (StructureByteStride > 0). D3D12 leaves rawBufferLoad on a structured SRV undefined, reading garbage or hanging simulation loops; conversely, D3DMetal serves both rawBufferLoad byte offsets and structured element * stride + field byte offsets from a DXGI_FORMAT_R32_TYPELESS D3D12_BUFFER_SRV_FLAG_RAW SRV at 4-byte (FirstElement) granularity.
  3. Card-written texture aliases & bound-RTV format mismatches: When a render target or UAV writes a host texture on the GPU and a later pass samples an overlapping alias texture at another format or mip layout whose host resource could not share the same placed heap, skipping the GPU-to-GPU alias copy or overwriting it with stale CPU bytes leaves the sampled alias blank. Likewise, binding RTVs/DSVs whose format differs from the graphics PSO's creation descriptor drops or corrupts draws unless a matching PSO variant is used.

What changed

  1. Atomic BVH builder hold & ray-resolve gating (pipeline/builder.rs, pipeline/make.rs, pipeline.rs, draw.rs):
    • Identify acceleration-structure builder compute shaders structurally (builder::register / builder::held) and hold all builder dispatches back atomically until every registered builder pipeline has a valid host PSO and 64-lane waves run whole (VICTORY_BVH_BUILDER=1 opts in on Wave32 hosts).
    • Gate inline-ray-query resolve and probe ray-activation compute dispatches while the BVH builder is held (VICTORY_SKIP_RT_RESOLVE=0 overrides) so unbuilt acceleration structures are neither compiled nor traversed.
  2. Raw backing views for structured buffer SRVs (descriptor/host.rs):
    • Convert StructureByteStride > 0 buffer SRVs whose byte offset satisfies host alignment (4-byte dword alignment under Wine/D3DMetal; 16-byte alignment on native D3D12) into DXGI_FORMAT_R32_TYPELESS D3D12_BUFFER_SRV_FLAG_RAW views (VICTORY_RAW_STRUCTURED=0 restores structured SRVs).
  3. GPU-written texture alias copies, RTV/DSV format PSO variants, and opt-in screen-space bounce stand-in (resource.rs, texture.rs, pipeline/host.rs, draw.rs, ambient.rs, apps/victory/src/run.rs):
    • Copy card-written source textures into unshared overlapping alias textures ahead of SRV reads (texture::alias_copy, deduplicated per command list via HostList::alias_source_changed) and suppress stale CPU staging uploads over GPU-authored textures.
    • Cache graphics PSO variants keyed by bound RTV/DSV formats (host_pipeline_for, VICTORY_TARGET_VARIANTS=0 disables), coexisting cleanly with perf(gpu): cache render targets, bound pipelines, and unaligned CBV scratch per list #268's host_targets bind cache.
    • Add an opt-in screen-space bounce and crease stand-in (VICTORY_AMBIENT=1, off by default) for runs where ray-traced indirect probes are held back.

How it was tested

  • Apple M5 Max, CrossOver Preview with D3DMetal (wave32=true), isolated test bottle:
    • cargo test --workspace (native macOS): all passed (0 failed).
    • victory-gpu --lib under Wine + D3DMetal (--test-threads=1): all passed, including live D3DMetal GPU tests for:
      • structured_buffer_srv_made_raw_reads_byte_and_element_offsets (StructureByteStride = 680, FirstElement = 1 +680 B 8 mod 16, verifying MUBUF raw byte load, SMEM scalar byte load, and MUBUF structured [2] + 12 load in one dispatch),
      • pipeline::builder atomic hold/release state machine and ray-resolve skip gating,
      • resource::tests card-written cross-family texture alias copy (R32G32_UINT -> BC1_UNORM),
      • ambient::tests::the_passes_build (all 6 runtime HLSL compute PSOs built under Wine vkd3d-shader + D3DMetal).
    • cargo xtask policy, git diff --check, and cargo clippy --workspace --all-targets --target x86_64-pc-windows-gnullvm -- -D warnings (with and without --features diagnostics): clean. All test inputs are synthetic.

Live M5 Max Verification (Combined Build on CrossOver Preview + D3DMetal)

  • In a combined warm-cache 720p opening-sequence run on Apple M5 Max (CrossOver Preview + D3DMetal, wave32=true), the runtime reached and sustained the 3D world through +8, +24, +48, +90 at 1.5–1.8 FPS (549.7–672.1 ms/frame on 1,021–1,258 draws/frame, 1.275 FPS overall across +8..+90) with 0 2.48 s allocation stalls, 7.2–9.0 ms/frame GUARDS lock hold time (down from 4,887 ms/frame before unlocking GUARDS), 149–154 MB/frame buffer copy volume, 0 DirectStorage decompression or access-violation failures, and 38/38 over-32 KiB aligned mesh/pixel pairs linked with 0 host mesh refusals.

@evan-kolberg

Copy link
Copy Markdown
Contributor

On an Apple M4 Pro with D3DMetal 4.0 beta 3, #402’s alias selector chose a linear 256×4 D32_FLOAT_S8X24_UINT source for a same-address 2048×4 R8_UNORM view. Both have a nominal 2,048-byte guest row, while the measured host pitches are 1,024 and 2,048 bytes. Two requests on the same unsubmitted list returned false, true with zero copies recorded. The second true skips the upload fallback in draw::readable.

Clearing the cached entry before a refresh and recording it only after emitting the copy commands gave false, false. A compatible control still returned true, true with one recorded copy. gdemontalivet’s #402 head · andrewpozdnakov7-jpg’s canonical record-after-copy ordering

Prepared and tested with Codex.

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