Skip to content

D3D12.X: opt-in staged GPU compute detiling for swizzled 2D textures - #3651

Open
Kalmarv wants to merge 1 commit into
xcl-dev:mainfrom
Kalmarv:pr/uma-direct-gpu-detile
Open

Kalmarv wants to merge 1 commit into
xcl-dev:mainfrom
Kalmarv:pr/uma-direct-gpu-detile

Conversation

@Kalmarv

@Kalmarv Kalmarv commented Dec 2, 2026 •

Copy link
Copy Markdown
Contributor

Adds an opt-in (XCL_GPU_DETILE=1, default off) staged GPU compute detiling path in texture_upload::submit() using cs_5_0 shaders (compiled via D3DCompile, no runtime DXC required) and the GF(2) offset tables from gpu::tiling across 1-, 2-, 4-, 8-, and 16-byte elements (R8, R16, RG8, RGBA8, BC1, BC7).

When enabled, 2D swizzled non-tail pieces at or above XCL_GPU_DETILE_MIN_BYTES (default 256 KiB) copy raw tiled blocks into the upload buffer and record known page hashes on the CPU before detiling into a pooled DEFAULT-heap UAV scratch buffer ahead of CopyTextureRegion. This preserves CPU detiling's source-read timing and unchanged-page deduplication. The default stays off until end-to-end GPU-time benefits are measured on heavy streaming workloads; the direct-UMA read path is split out for a separate follow-up PR.

Total diff vs main: 5 files changed, +781 / -34 (+595 / -33 runtime, +186 / -1 unit/GPU tests).

What is in it

  • crates/xcl/src/gpu/tiling.rs (+7): Layout::gpu_detile_tables exposing the existing ox, oy, hx, hy GF(2) tables and fixed XOR bits.
  • crates/xcl/src/hle/d3d12x/gpu_detile.rs (+429, mod gpu_detile in mod.rs):
    • Opt-in toggle XCL_GPU_DETILE=1 (default off) and XCL_GPU_DETILE_MIN_BYTES (default 256 KiB).
    • Five cs_5_0 compute shaders (1, 2, 4, 8, 16 B elements), TablePack layout deduplication, and eligible() filter (2D swizzled non-tail pieces meeting the size threshold).
    • Non-blocking off-thread initialization (xcl-gpu-detile-init): both device-independent D3DCompile + D3D12SerializeRootSignature (cached in OnceLock) and per-queue CreateRootSignature + 5× CreateComputePipelineState run off the submitting thread; uploads fall back cleanly to CPU detiling until ready.
    • Per-queue lifetime: Slot is owned directly by title_textures::QueueUploads and dropped on title_textures::forget_queue, with no global device cache and no ID3D12Device::Release hook (real.rs untouched).
    • Unit tests covering non-blocking Slot::poll state transitions, env parsing, table deduplication, and eligible() filtering.
  • crates/xcl/src/hle/d3d12x/texture_upload.rs (+338 / -29) and crates/xcl/src/hle/d3d12x/title_textures.rs (+6 / -5):
    • Work::RawBlocks staging, pooled DEFAULT-heap UAV detile scratch buffer (ensure_detile_buf), UAV barrier and COMMON/COPY_SOURCE transitions, and Job::in_flight_bytes() accounted against IN_FLIGHT_BYTES / IN_FLIGHT_MAX.
    • take() ranks pooled jobs by smallest fitting upload buffer first (pool_rank), tie-breaking on detile scratch fit, so small GPU batches do not grab the largest pooled upload buffer.
    • Unit and GPU tests: take_ranks_upload_size_before_scratch_fit and gpu_detile_matches_xg_for_all_element_sizes_and_64k_modes (R8, R16, RG8, RGBA8, BC1, BC7 × all four 64 KiB modes, whole textures, array slices, partial dirty-tile updates, known page hashing, in_flight_bytes() accounting, and Slot::Failed CPU fallback).

Tested

  • Hardware & toolchain: NVIDIA GeForce RTX 5070 Ti (driver 32.0.16.1074), AMD Ryzen 9 9950X 16-Core, Windows 11 x64, rustc 1.99.0 (b940084d7 2026-09-28), DXC 1.9.2609.
  • cargo build --release -p xcl: ok (39 pre-existing xcl lib warnings, 0 new vs main, 0 in changed code).
  • .\scripts\test.ps1 (with DXC 1.9.2609): 3010 passed, 0 failed, 52 ignored; 24 SKIPPED (22 no wave64, 2 no heap windows; 0 DXC skips; all 6 new gpu_detile and texture_upload tests passed).
  • Targeted RTX 5070 Ti and WARP runs (cargo test -p xcl --lib gpu_detile -- --test-threads=1, cargo test -p xcl --lib texture_upload::tests -- --test-threads=1, and XCL_ADAPTER=warp cargo test -p xcl --lib texture_upload::tests::gpu_detile -- --test-threads=1): all passed, 0 skipped.

Regression run

main:

xcl-smoke v1
commit: 5eaadb54c14e0fc29597b2a1689d7dbd83491303
gpu: NVIDIA GeForce RTX 5070 Ti
title: ea3b5e4d
seconds: 60
exit: 0x0
frames shown: 1698 (PresentX 1698)
presents per 2 s (last 3): 60 61 60
device removals: 0
crashes: 0
peak commit: 15798 MB

PR (default, XCL_GPU_DETILE unset):

xcl-smoke v1
commit: 72a12a04f7d5c76375a715b865f640d9a8abdcf7
gpu: NVIDIA GeForce RTX 5070 Ti
title: ea3b5e4d
seconds: 60
exit: 0x0
frames shown: 1696 (PresentX 1696)
presents per 2 s (last 3): 60 60 60
device removals: 0
crashes: 0
peak commit: 17246 MB

PR (opt-in, XCL_GPU_DETILE=1):

xcl-smoke v1
commit: 72a12a04f7d5c76375a715b865f640d9a8abdcf7
gpu: NVIDIA GeForce RTX 5070 Ti
title: ea3b5e4d
seconds: 60
exit: 0x0
frames shown: 1697 (PresentX 1697)
presents per 2 s (last 3): 60 61 60
device removals: 0
crashes: 0
peak commit: 17256 MB
  • Default (XCL_GPU_DETILE unset) vs main: 1696 vs 1698 frames shown (99.9%), 60 60 60 vs 60 61 60 last presents per 2 s, 0 device removals, 0 crashes, 17246 vs 15798 MB peak commit (109.2%); GPU detiling stays inactive.
  • Opt-in (XCL_GPU_DETILE=1) vs main: 1697 vs 1698 frames shown (99.9%), 60 61 60 vs 60 61 60 last presents per 2 s, 0 device removals, 0 crashes, 17256 vs 15798 MB peak commit (109.2%, +10 MB vs default off). texture upload: GPU compute detiling active (min_bytes=262144) logged at 5.2 s with 0 Present stalls and 0 errors.

Not tested

  • AMD / Intel discrete GPUs, Wine / vkd3d-proton, and macOS / D3DMetal.
  • 3D volume textures, linear textures, and mip-tail levels (remain on the existing CPU detiling path).
  • End-to-end GPU-time vs CPU-time measurements under sustained in-world streaming (left for a follow-up before considering turning XCL_GPU_DETILE on by default).
  • Wave64-only GPU tests (22) and opt-in heap-windows tests (2) skipped on this RTX 5070 Ti adapter.

AI use

  • Antigravity (Gemini 4 Neon): rebased onto origin/main (817eb1fc9), stripped the direct-UMA path out for a separate follow-up PR, defaulted XCL_GPU_DETILE off, moved GpuDetile ownership into per-queue QueueUploads (removing the global cache and real.rs hook), moved both D3DCompile and D3D12 PSO creation onto a background init thread, fixed take() pool ranking with a unit test, and ran the release build, DXC 1.9 test suite, WARP test, and paired 60 s smokes.
  • Claude Opus 5.5: authored the initial staged GPU compute detiling shaders, upload integration, and bit-exactness tests.

@github-actions github-actions Bot added area:d3d12 D3D12.X runtime, command lists, resources, sparse memory gpu:both Code affects both AMD and NVIDIA GPUs (scope tag; see needs:*-test for remaining checks) labels Dec 2, 2026
@Decodere

Decodere commented Dec 2, 2026

Copy link
Copy Markdown
Contributor

Diff review (head d2a7bdba3, read-only code review — not a #3041 merge-check):

The GF(2) compute-detiling math (gpu_detile_tables ox/oy/hx/hy/oz/hz, partial dirty-rect pitch/cols/span, mip-tail offsets, and the cs_5_0 hlsl_packed/hlsl_direct shaders) checks out cleanly against tiling.rs::offset_z. Two lifetime/concurrency hazards should be resolved before enabling this by default:

  1. [HIGH] Device leak and ABA pointer-reuse UB in gpu_detile::of(dev) (crates/xcl/src/hle/d3d12x/gpu_detile.rs:333-358):
    MADE: Mutex<Vec<(usize, Option<Arc<GpuDetile>>)>> caches up to 8 (dev.as_raw() as usize, GpuDetile) entries permanently with no cleanup hook on device destruction. Each GpuDetile owns an ID3D12RootSignature and 5 ID3D12PipelineState objects, pinning up to 8 ID3D12Device instances alive until process exit; worse, if a device is destroyed/evicted and a new ID3D12Device is allocated at the same raw address (ABA), of(dev) returns PSOs/RS created on the dead device. Wiring a forget_device(dev: usize) cleanup hook on device release (matching other D3D12.X caches) closes both the leak and ABA hazard.

  2. [HIGH] Async direct-UMA read race when XCL_GPU_DETILE_DIRECT=1 (currently default ON) (crates/xcl/src/hle/d3d12x/gpu_detile.rs:193-199, crates/xcl/src/hle/d3d12x/texture_upload.rs:1048-1115, crates/xcl/src/hle/d3d12x/title_textures.rs:2658-2889):
    Previously, texture_upload::submit synchronously copied and detiled texels from the title's UMA memory into the staging upload buffer before returning. With direct_va enabled by default, stage_with_gpu_detile performs zero CPU copy for committed coherent UMA textures and submits an asynchronous compute Dispatch (including on XCL's separate OWN_UPLOADS queue) that reads live UMA memory directly, returning while the Dispatch is still queued/in-flight. Any CPU-thread overwrite or buffer reuse of that coherent UMA memory before the upload fence completes will tear/corrupt the uploaded texture. Either defaulting XCL_GPU_DETILE_DIRECT off (until CPU-write synchronization is guaranteed) or fencing/copying mutable staging sources avoids silent texture corruption.

  3. [Scope] Diff size (+1123/-19) & default-ON cross-GPU coverage:
    Since the PR is >800 lines and turns both XCL_GPU_DETILE and XCL_GPU_DETILE_DIRECT on by default across all vendors while tested only on NVIDIA + WARP so far, splitting direct-UMA reading into a small follow-up PR (after verifying packed GPU detile across NVIDIA + AMD/Wine) would reduce risk and keep each PR under the 800-line guideline.

AI use: Antigravity (Gemini 4 Neon) audited the full diff against main and drafted this review.

@Foniass

Foniass commented Dec 2, 2026

Copy link
Copy Markdown
Collaborator

Review (head d2a7bdb, diff read, not a merge-check):

  • The detile scratch is not counted in the in-flight cap. title_textures.rs ~2689 adds f.bytes to the queue's in-flight bytes, and Job.bytes is only the UPLOAD buffer. On the GPU path that buffer holds the tables plus any copied raw blocks, so a batch of direct pieces is about BUFFER_MIN (1 MiB) (texture_upload.rs ~1067, take(dev, plan.upload_bytes)). Each job also holds a DEFAULT-heap detile_buf the size of the batch's linear texels (ensure_detile_buf ~879). So IN_FLIGHT_BYTES / IN_FLIGHT_MAX no longer limit what is in flight: up to about 2048 jobs, each with its own VRAM scratch. take() picks a pooled job by upload size only, and the pool keeps 64 MiB of scratch. A large batch therefore makes a new committed DEFAULT buffer on the submitting thread. The smoke block reports commit, not VRAM.
  • The production direct path is not tested. Both GPU tests pass their own direct closures to stage_with_gpu_detile, so gpu_detile::direct_va (uma_device() check, in_committed_window, uma_va_of onto the real window buffers) never runs with a positive result. The "device guard" assertion (texture_upload.rs ~1774, direct_va(&dev, bc7_src, ..) == None) cannot fail: bc7_src is a VirtualAlloc outside every UMA window, so in_committed_window or existing() returns None before the device is compared.
  • When the source is read changes; the description does not say so. Before, the PC texture got the bytes the CPU copied at submit. Now a direct piece gets whatever the window holds when the GPU runs the list. On XCL's own queue (when the title's queue takes no internal work) that can be well after submit. The pages are then forgotten (Hashes::forget ~633). A later write is still uploaded, but every write-watch hit on those pages now re-uploads with no hash comparison. That is a cost the throughput table cannot show: the table measures CPU staging cycles only, not the GPU time of the up to 118 dispatches.
  • Size: 1123 lines in one commit, and the description gives no reason it cannot be split (CONTRIBUTING: over ~800 lines it must). It splits cleanly: gpu_detile_tables plus the gpu_detile module and its tests, not called yet; staged GPU detiling in submit(); direct UMA reads; and the benches/formats.rs detile_blocks bench. The bench measures the CPU detile_blocks path and is unrelated to the GPU change.

Fine: the swizzle math matches Layout::offset_z / detile_blocks (same ox/oy/hx/hy, slice oz^hz^base_xor, power-of-two masks enforced in eligible). Direct reads stay inside span ((rows-1)*pitch+cols blocks, block stride matching block_at), and committed, non-shadowed windows are required. The scratch barriers (COMMON→UAV→COPY_SOURCE→COMMON) are right. Every failure (no PSO, no scratch, size overflow) falls back to the CPU path. Both switches are on by default. Merges cleanly with #3653 / #3662 / #3665: their new trailing view windows are not committed windows, so textures there keep the copy path.

AI review: Claude Code (Claude Opus 5.5), posted by Foniass without a human check of each point.

@Foniass

Foniass commented Dec 2, 2026

Copy link
Copy Markdown
Collaborator

merge-check
base: 1c8b551
head: d2a7bdb
merge: clean
build: cargo build --workspace -> ok
tests: cargo test --workspace --no-fail-fast -- --test-threads=4 -> 2915 passed, 0 failed, 51 ignored; SKIPPED: 28 (20 no wave64, 2 no d3d12SDKLayers.dll, 1 no D3D12 debug layer, 1 no heap windows, 1 cannot disable the driver cache, 3 others; the same set as on main); failing: none
review: read by one reviewer at this same head; the review is at #3651 (comment) and its findings stand. Most serious: the VRAM scratch for detiling isn't counted in the in-flight cap, and the real direct path has no test.
performance: 60 s smoke of a retail title, main 1c8b551 vs this merge (d2a7bdb): 1652 -> 1426 frames shown (86 %; last presents per 2 s: 60 60 60), peak commit 18228 -> 18099 MB, 0 device removals, 0 crashes. This run is lower than most today: the PC was in use at the time (a browser and Discord running), and main's own runs on this machine today ranged from 1370 to 1652 frames, so the difference isn't clearly from this PR.
verdict: not yet: the VRAM scratch for detiling isn't counted in the in-flight cap, and the real direct path has no test (see the review)
hardware / tool and model: RTX 5070 Ti (driver 621.14), Ryzen 7 7800X3D, Windows 11 26300 / Claude Code with Claude Opus 5.5

AI-written and posted by Foniass without a human check of each point.

@Kalmarv
Kalmarv force-pushed the pr/uma-direct-gpu-detile branch from d2a7bdb to 5d0b81e Compare December 2, 2026 14:56
@Kalmarv Kalmarv changed the title D3D12.X: GPU compute detiling directly from coherent UMA windows D3D12.X: GPU compute detiling for swizzled 2D textures Dec 2, 2026
@Kalmarv

Kalmarv commented Dec 2, 2026

Copy link
Copy Markdown
Contributor Author

Rebased onto origin/main (5eaadb54c) and updated to 5d0b81e4a addressing all points from @Decodere and @Foniass:

  1. Device leak & ABA eviction in gpu_detile::of (@Decodere Implement XSystemGetDeviceType/PLS thunks and Opus audio carrier parsing #1): Wired gpu_detile::take_device / forget_device into ID3D12Device::Release (real::dev_release via release_taking_first in overrides(Kind::Device)) so cached root signatures and PSOs are evicted before IUnknown::Release can free or reuse the device address. Covered by gpu_detile::tests::forget_device_evicts_cached_entry and real::heap_release_order_tests::device_release_evicts_gpu_detile_before_address_reuse.
  2. In-flight VRAM scratch accounting & pool selection (@Foniass Implement XSystemGetDeviceType/PLS thunks and Opus audio carrier parsing #1): Made Job.bytes private and added Job::in_flight_bytes() (bytes + detile_bytes), used by title_textures.rs (submit_on / reclaim) so IN_FLIGHT_BYTES / IN_FLIGHT_MAX bound both UPLOAD staging and DEFAULT detile scratch. Updated take(dev, bytes, detile_bytes) to prefer pooled jobs whose detile_buf already fits detile_bytes (or jobs without a detile_buf when detile_bytes == 0).
  3. XCL_GPU_DETILE_DIRECT defaulted OFF & read-timing/hash-forget semantics documented (@Decodere #2, @Foniass XAudio2: real Opus decoder via Windows MSOpusDecoder MFT #3): XCL_GPU_DETILE_DIRECT now requires explicit =1 opt-in; default (XCL_GPU_DETILE=1, XCL_GPU_DETILE_DIRECT=0) uses staged packed GPU detiling, copying raw blocks and hashing pages at submit so source-read timing and known-page deduplication match CPU detiling. The PR description and gpu_detile.rs doc comments state the live-read timing and Hashes::forget re-upload tradeoff of opt-in XCL_GPU_DETILE_DIRECT=1.
  4. Real committed UMA test of gpu_detile::direct_va and production submit() (@Foniass #2): Replaced the VirtualAlloc assertion with isolated subprocess test gpu_detile_direct_va_uses_real_uma_window_and_rejects_other_device (XCL_GPU_DETILE_DIRECT=1), which opens a real committed coherent UMA window via real::open_uma_for_test, asserts positive direct_va(&dev, src, len) == Some(uma_va_of(src)), asserts wrong-device rejection on that exact committed UMA address, checks alignment/OOB rejection, and executes production texture_upload::submit() over a 9-mip BC7 chain (incl. mip tail) with DISPATCHES delta == 9, in_flight_bytes() == bytes + detile_bytes, and bit-exact readback.
  5. Scope & unrelated bench hunk (@Foniass Olympus compatibility layer #4, @Decodere XAudio2: real Opus decoder via Windows MSOpusDecoder MFT #3): Dropped the unrelated crates/xcl/benches/formats.rs change and added a size justification to the PR description (~560 lines runtime + ~740 lines tests, with direct UMA opt-in).

Re-verified on RTX 5070 Ti + WARP (cargo build --release -p xcl: 39 lib warnings, 0 new; scripts/test.ps1 with DXC 1.9: 2983 passed / 0 failed / 52 ignored / 24 SKIPPED [22 no wave64, 2 no heap windows]; clean 60 s smoke vs 5eaadb54c: 1657 vs 1698 frames, 61 61 60 presents/2s, 0 removals, 0 crashes, 17593 vs 15798 MB commit).

AI use: Antigravity (Gemini 4 Neon) implemented these review fixes, ran the tests and smoke, and drafted this reply.

@Foniass

Foniass commented Dec 2, 2026

Copy link
Copy Markdown
Collaborator

merge-check
base: 5eaadb5
head: 5d0b81e
merge: clean
build: cargo build --workspace -> ok
tests: cargo test --workspace --no-fail-fast -- --test-threads=4 -> 2983 passed, 0 failed, 52 ignored; SKIPPED: 28 (20 no wave64, 2 no d3d12SDKLayers.dll, 1 no D3D12 debug layer, 1 no heap windows, 1 cannot disable the driver cache, 3 others; the same set as on main); failing: none
review: follow-up on 5d0b81e (reworked and rebased from d2a7bdb, my last review's head). Now one commit of +1298/-26 in 6 files; the benches/formats.rs hunk is gone. Reading straight from the UMA window is now opt-in (XCL_GPU_DETILE_DIRECT=1); the default path copies the raw tiled blocks into the upload buffer (hashing them on the CPU), then detiles them on the GPU for pieces of at least 256 KiB. A pooled DEFAULT-heap scratch buffer is counted through Job::in_flight_bytes(), ID3D12Device::Release gets a hook to evict the per-device pipeline cache, and a child-process test covers direct_va and submit() on a real committed window. Earlier findings: (1) scratch missing from the in-flight cap: fixed (title_textures.rs ~2620 / ~2705 add and subtract in_flight_bytes()); a large batch still creates a committed DEFAULT buffer on the submitting thread (ensure_detile_buf, texture_upload.rs ~886). (2) the direct path untested: fixed (gpu_detile_direct_va_uses_real_uma_window_and_rejects_other_device, ~1735). (3) source-read timing not stated: fixed for the default path. (4) size: partly fixed; the bench is dropped and there's a size note, but it says only that the parts were kept together, its "~560 lines of runtime code" is about 785 by count, and the opt-in direct path (direct_va*, uma_device, open_uma_for_test, Hashes::forget, the direct branches, two GPU tests) is still a separable second PR. New: (a) GPU detiling is on by default for every title with no measured benefit: the throughput harness prints CPU cycles only, the dispatches' GPU time isn't measured, and the PR's own smoke shows 97.6 % frames and peak commit 15798 -> 17593 MB (111.4 %), unexplained; the default path also needs a DEFAULT scratch buffer the size of the linear data on top of the upload buffer. (b) The dev_release hook (real.rs ~4835) runs on every device Release and evicts only when release_taking_first sees the last reference, which device children likely never allow on a real device; the ABA protection is shown only on a fake refcounted object (~15409). (c) The first eligible upload compiles five cs_5_0 shaders with D3DCompile inside own_work on the submitting thread (gpu_detile.rs ~366), a one-time stall on the title's thread. (d) take() with detile_bytes > 0 (texture_upload.rs ~926) ranks by scratch fit before upload size, so a small GPU batch can take the largest pooled upload buffer. Fine: the swizzle math matches offset_z / detile_blocks; RawBlocks reads the same block runs as Work::Blocks and notes their pages the same way; the scratch barriers are right; every failure falls back to the CPU path; the default path is checked bit-exact for R8 / R16 / RG8 / RGBA8 / BC1 / BC7 in the four 64 KiB modes.
performance: 60 s smoke of a retail title, main 5eaadb5 vs this merge (5d0b81e): 1422 -> 1675 frames shown (117 %; last presents per 2 s: 60 60 61), peak commit 17184 -> 16410 MB, 0 device removals, 0 crashes.
verdict: not yet: GPU detiling is now on by default for every title with no measured benefit (its own smoke shows 97.6 % frames and +1.8 GB peak commit), bundled with the separable opt-in direct-UMA path in a 1298-line PR (see the review)
hardware / tool and model: RTX 5070 Ti (driver 621.14), Ryzen 7 7800X3D, Windows 11 26300 / Claude Code with Claude Opus 5.5

AI-written and posted by Foniass without a human check of each point.

@bmjask bmjask left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Review of 5d0b81e4: 1 finding(s) (1 medium), most severe first.

  1. Medium crates/xcl/src/hle/d3d12x/real.rs:4837: Not certain about this one, because it depends on D3D12 device children holding a reference on their device: the new dev_release hook probably never evicts the gpu_detile cache for a real device. (inline)

These findings come from an AI-assisted review (Claude Code) and focus on correctness. Each is a candidate; please check it before acting on it.

Comment thread crates/xcl/src/hle/d3d12x/real.rs Outdated

/// ID3D12Device::Release: evict per-device GPU detile pipelines before the final Release frees the
/// address (preventing both device leaks and ABA reuse).
unsafe extern "system" fn dev_release(this: *mut c_void) -> u32 {

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Medium: Not certain about this one, because it depends on D3D12 device children holding a reference on their device: the new dev_release hook probably never evicts the gpu_detile cache for a real device. The cached root signature and five PSOs are device children, so while the entry exists the device refcount never drops to the value the AddRef probe looks for. After one large GPU-detiled upload, the title's final Release leaves the device, its PSOs and the MADE slot alive for the life of the process (up to 8 devices before FIFO eviction). If children don't hold that reference, the opposite problem appears: release_taking_first drops the taken PSOs in after only after the final Release has already freed the device. The unit test uses a fake object with no child references, so it can't tell the two apart. Evicting from an explicit device-teardown point, or holding the cache by weak/private-data association, would avoid both.

@bmjask

bmjask commented Dec 2, 2026

Copy link
Copy Markdown
Contributor

@Prime1997 could you take a look at this one for review and merge? My automated review notes are above, if any.

Add an opt-in (`XCL_GPU_DETILE=1`, default off) staged GPU compute
detiling path in `texture_upload::submit()` using `cs_5_0` shaders
(compiled via D3DCompile, no runtime DXC) and the GF(2) offset tables
(`ox`, `oy`, `hx`, `hy`) from `gpu::tiling` across 1-, 2-, 4-, 8-, and
16-byte elements (R8, R16, RG8, RGBA8, BC1, BC7).

When enabled, 2D swizzled non-tail pieces at or above
`XCL_GPU_DETILE_MIN_BYTES` (default 256 KiB) copy raw tiled blocks into
the upload buffer and record `known` page hashes on the CPU (preserving
CPU detiling's source-read timing and unchanged-page deduplication),
then detile into a pooled DEFAULT-heap UAV scratch buffer before
`CopyTextureRegion`.

Both device-independent shader compilation (`D3DCompile`) and D3D12
pipeline creation (`CreateRootSignature` + 5x `CreateComputePipelineState`)
run off the submitting thread on a background thread
(`xcl-gpu-detile-init`), falling back cleanly to CPU detiling until ready
so the submitting thread never blocks during startup PSO compilation.
Each upload queue owns its own `gpu_detile::Slot` inside `QueueUploads`
(dropped on `title_textures::forget_queue`), avoiding any global device
cache or device Release hook. `Job::in_flight_bytes()` accounts for both
UPLOAD staging and DEFAULT detile scratch against `IN_FLIGHT_BYTES` /
`IN_FLIGHT_MAX`, and `take()` ranks pooled jobs by smallest fitting
upload buffer before tie-breaking on detile scratch fit.

Includes unit and GPU bit-exactness tests across all 64 KiB modes,
partial updates, `known` page hashing, `in_flight_bytes()` accounting,
`take()` pool ranking, non-blocking off-thread init polling, and CPU
fallback.

Co-Authored-By: Antigravity (Gemini 4 Neon) <noreply@google.com>
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@Kalmarv Kalmarv changed the title D3D12.X: GPU compute detiling for swizzled 2D textures D3D12.X: opt-in staged GPU compute detiling for swizzled 2D textures Dec 2, 2026
@Kalmarv
Kalmarv force-pushed the pr/uma-direct-gpu-detile branch from 5d0b81e to 72a12a0 Compare December 2, 2026 18:41
@Kalmarv

Kalmarv commented Dec 2, 2026

Copy link
Copy Markdown
Contributor Author

Thanks @Foniass and @bmjask for the thorough reviews — reworked and rebased onto origin/main (817eb1fc9) as 72a12a04f (+781 / -34 across 5 files: +595 / -33 runtime, +186 / -1 tests):

  • Split out the direct-UMA path (direct_va*, uma_device, open_uma_for_test, Hashes::forget, direct branches and direct GPU tests) for a separate follow-up PR so this PR covers staged GPU compute detiling only and stays cleanly scoped.
  • Defaulted XCL_GPU_DETILE OFF (opt-in XCL_GPU_DETILE=1) until end-to-end GPU-time vs CPU-time gains are measured on heavy streaming workloads.
  • Fixed device/pipeline lifetime (@Foniass (b) & @bmjask real.rs:4837): removed the global MADE cache and the dev_release hook completely (crates/xcl/src/hle/d3d12x/real.rs is untouched vs main). Each upload queue now owns its gpu_detile::Slot directly inside title_textures::QueueUploads and drops it in title_textures::forget_queue.
  • Fixed submitting-thread init stall (@Foniass (c)): moved both device-independent D3DCompile + D3D12SerializeRootSignature and per-queue CreateRootSignature + 5× CreateComputePipelineState onto background thread xcl-gpu-detile-init, falling back transparently to CPU detiling until ready (slot_poll_is_non_blocking_until_background_init_finishes). (Testing XCL_GPU_DETILE=1 during the initial smoke caught that CreateComputePipelineState alone contended with startup background PSO compiles for 12.3 s when left on the submitting thread; moving PSO creation off-thread too eliminated the stall completely.)
  • Fixed take() pool ranking (@Foniass (d)): pool_rank now ranks pooled jobs by smallest fitting upload buffer first and tie-breaks on detile scratch fit, with unit test take_ranks_upload_size_before_scratch_fit.

Verification on 72a12a04f (RTX 5070 Ti, driver 32.0.16.1074, Ryzen 9 9950X, Windows 11 x64, rustc 1.99.0, DXC 1.9.2609):

  • cargo build --release -p xcl: ok (39 pre-existing warnings, 0 new).
  • .\scripts\test.ps1: 3010 passed, 0 failed, 52 ignored; 24 SKIPPED (22 no wave64, 2 no heap windows; 0 DXC skips), plus targeted WARP gpu_detile_matches_xg_for_all_element_sizes_and_64k_modes pass.
  • Paired 60 s smokes (title: ea3b5e4d, main 5eaadb54c: 1698 frames, 15798 MB peak commit):
    • Default (XCL_GPU_DETILE unset): 1696 frames (99.9%), 60 60 60, 0 removals, 0 crashes, 17246 MB peak commit.
    • Opt-in (XCL_GPU_DETILE=1): 1697 frames (99.9%), 60 61 60, 0 removals, 0 crashes, 17256 MB peak commit (GPU compute detiling active at 5.2 s, 0 Present stalls).

Ready for re-check. (Update authored with Antigravity, Gemini 4 Neon.)

@github-actions github-actions Bot added the needs-1st-check Passes gates; needs first independent merge-check (#3041) label Dec 2, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

area:d3d12 D3D12.X runtime, command lists, resources, sparse memory gpu:both Code affects both AMD and NVIDIA GPUs (scope tag; see needs:*-test for remaining checks) needs-1st-check Passes gates; needs first independent merge-check (#3041)

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants