Skip to content

cpp-migration: pass captured heap view types into the in-runtime native compute route (#2958) #3023

Backlog

Description

@JB2WIN

Target branch cpp-migration. Follow-up to #2908 (#2972), part of #2935.

Problem. The in-process native compute route of #2958 (X2W_NATIVE_SHADERS, src/startup_stubs/d3d12_compute_dispatch.inl) translates slot 67 requests with x2w::translate_compute(read_compute_request(path), 0, true). That call takes no HeapViewContract, so a request that reads a GPU-selected global SRV heap slot is refused on this route, although #2972 can translate it offline when the view types are known.

Plan.

  1. Record each heap slot's view type when the stand-in creates the SRV (classify_global_srv_view from src/startup_stubs/native_global_srv.h).
  2. At Dispatch, resolve the slots the request can select and build the contract from the recorded types.
  3. Pass the contract into the translation of the slot 67 request (today prepare_direct_compute refuses a contract for non-container requests, and translate_compute has no contract parameter).
  4. Before the Dispatch, check that the live heap still matches the contract; otherwise refuse and keep the gfx2hlsl.py fallback.

Test. A source-built compute workload through the stand-in that reads a buffer, a 2D texture and an empty slot through a GPU-selected index, _warp (capability skip where needed) and _hardware, plus a negative control that indexes one slot off.

I'm taking it; a PR follows. Environment: Windows 11 build 26200, NVIDIA GeForce RTX 3080, plus WARP.

AI-assisted.

Activity

  1. JB2WIN commented on Nov 27, 2026

    @JB2WIN
    ContributorAuthor

    Update: the first step is #3025.

    A census over a local start-up capture (compile_dxil --refusals, 2,689 slot 67 requests, counts only) gives 941 first refusals of this kind. In those programs the heap index is read from buffer data per wave, so the runtime cannot resolve it at Dispatch. Behind the contract there are more walls:

    1. The byte-address rewrite of the loaded heap descriptor (stride, byte count, raw out-of-bounds mode): native: read the byte-address rewrite of a GPU-selected heap descriptor #3025, compiler only.
    2. Typed loads (buffer_load_format_*) through the heap descriptor. With 1 done in a local prototype, 906 requests stop here.
    3. Runtime: a shader-visible heap with title slot k at desktop index k as raw views, plus a contract check over all selectable slots at Dispatch (one captured slot is not enough for per-wave selection).

    I'll continue with 2 next unless someone else is on it.

    AI-assisted.

  2. JB2WIN commented on Nov 27, 2026

    @JB2WIN
    ContributorAuthor

    Runtime acceptance criteria for stage 3 (from R0Y00's review of #3025). Before the runtime supplies a raw-view contract for GPU-selected slots, its fixture must check both of these:

    • a selectable slot whose guest descriptor has a stride above 1 (byte count num_records * stride), read through the byte-address rewrite;
    • a slot whose guest byte count does not match the desktop view: the dispatch must refuse it before the shader runs, not read past or short of the view.

    #3025 does not cover either: its descriptor words stay symbolic.

    AI-assisted.

  3. JB2WIN commented on Nov 27, 2026

    @JB2WIN
    ContributorAuthor

    Step 2a is #3034 (builds on #3025): formatted loads (tbuffer_load_format_*) through the byte-address rewritten descriptor, table or GPU-selected heap slot, now read the same raw view as the raw load.

    Additions to the stage-3 runtime acceptance criteria from that change:

    • A formatted load bounds its whole element with the desktop raw view's size (getDimensions), not with the guest window's byte count. The runtime must create the raw view for a GPU-selected heap window with exactly the guest window's byte count (num_records * max(stride, 1)). The startup manifest route already sizes a table window that way.
    • The descriptor's DST_SEL (word 3) is not applied; only the identity channel order is translated. The runtime contract should either check identity DST_SEL on every selectable slot or the translator has to apply it.

    Typed heap loads (buffer_load_format_*) are the next step and need their own view contract.

    AI-assisted.

  4. JB2WIN commented on Nov 27, 2026

    @JB2WIN
    ContributorAuthor

    Typed heap loads: #3042 adds HeapViewContract::typed_dword for one-channel 32-bit typed buffer slots (buffer_load_format_x with an idxen index through the GPU-selected heap descriptor), with .views channel metadata and a fixture section.

    On the local capture (counts only) the 906 typed heap loads all have that one-channel shape, but with typed_dword forced, 895 of those programs then stop at a raw read of another heap slot: they read the heap raw and typed through different selectors. Covering them needs more than one captured selection per dispatch (raw and typed contracts per load class), which I plan as the next step before the runtime contract.

    AI-assisted.

  5. added a commit that references this issue on Nov 27, 2026
  6. 12 remaining items

  7. JB2WIN commented on Nov 29, 2026

    @JB2WIN
    ContributorAuthor

    I'm withdrawing Plan C (#3023 (comment)). I measured it before building it, and it unlocks nothing on its own.

    I ran compile_dxil --refusals on main (1a0120b) over the same local cohort of 2,689 compute requests. For the measurement only, requests without a .views contract were treated as if a heap twin existed:

    Heap contract assumed Requests that translate Next refusal after the heap one
    none (main) 487 1073 direct resource-heap load requires the selected GPU slot and its view type
    raw_buffer (Plan C as posted) 487 1013 a GPU-selected heap slot is read as a raw buffer SRV only, all from a typed_buffer_srv load
    raw_and_typed_dword (#3415) 487 701 direct indexed descriptor load shape, 328 unbound numeric SGPR sN, 164 heap byte window loaded inside a loop

    So in this cohort:

    • most of these requests read their heap slot with a formatted (typed) load, not raw. My earlier "mostly raw" assumption was wrong for them.
    • even with both raw and typed heap reads allowed, not one more request translates. The next blockers are tied to the per-lane waterfall loop around the heap index (v_readfirstlane, then the heap load inside the loop), not to how the heap is bound.

    A heap twin would add runtime work and would not translate a single extra request. The next step is the waterfall loop itself. I'm looking at the 701 direct indexed descriptor load shape cases first and will post what they look like.

    The cohort is local only and was not committed. The measurement switch was a local getenv override and is not part of any branch.

    AI-assisted.

  8. JB2WIN commented on Nov 29, 2026

    @JB2WIN
    ContributorAuthor

    Follow-up on the 701 direct indexed descriptor load shape refusals in the raw_and_typed_dword run: all 701 are s_load_dwordx8 from the resource heap with a GPU-selected SGPR offset. So they fetch an image descriptor by a GPU value, which is bindless texture access, not a buffer read. The direct path only accepts a GPU-selected heap load of one buffer descriptor (s_load_dwordx4).

    So for this bucket the blocker is GPU-selected heap textures, not raw versus typed buffers. For a texture, ResourceDescriptorHeap[i] needs its dimension and format in the shader, and a heap slot can hold any view. A heap twin could still help there, as a texture twin: real views only for slots that match the type the shader declares, null for every other slot. I'll write that up as a design before building anything. The remaining refusals (328 unbound numeric SGPR sN, 164 heap byte window loaded inside a loop) come after it.

    AI-assisted.

  9. JB2WIN commented on Nov 29, 2026

    @JB2WIN
    ContributorAuthor

    Measured the bound view heap for the GPU-selected heap texture reads (the 696 s_load_dwordx8 cases above). One launcher run (Series X preset, NVIDIA), sampled at the first compute dispatch and at dispatch 4096, with a temporary local counter (not committed):

    dispatch 1 dispatch 4096
    logical descriptors in the bound CBV/SRV/UAV heap 1,245,184 1,245,184
    slots holding a registered SRV token: Texture2D 565 2,000
    Texture2DArray 6 2,839
    other texture dimensions 129 213
    buffer SRVs 296 5,176
    other views (CBV/UAV) 558 5,286
    one full scan of the heap records 12.8 ms 10.3 ms

    What follows for a design:

    • A 1:1 mirror of the title heap is not possible. 1,245,184 is above the desktop shader-visible limit of 1,000,000.
    • Any per-dispatch walk over the heap costs ~10 ms, so validation at dispatch time is out.
    • CreateDescriptorHeap in d3d12_xs_device.cpp notes that the title copies descriptors between heaps with its own memory copies, so tracking API writes alone would miss copies.

    The direction I'm writing up (local design, nothing built yet): the translated shader reads the 32-byte record at the GPU-selected index from the title heap memory itself, through a raw SRV over that memory (the zero-copy alias d3d12_memory_srv.inl already uses). If word 0 is the view token magic and word 1 is a known view id, it indexes ResourceDescriptorHeap[base_dim + id]. Otherwise it reads a null slot. The runtime fills one descriptor per view record id of the matching dimension when the record is created (about 5,000 textures in this run, not 1.2M). Copies made by the title are then handled because the shader reads the token that is in memory when it runs. Open points before code: resource lifetime of mirrored ids, the resource state of textures the shader may pick, and whether this belongs in the runtime the way you want it, @R0Y00.

    AI-assisted.

  10. JB2WIN commented on Nov 29, 2026

    @JB2WIN
    ContributorAuthor

    Result of a local prototype of the texture route, before any more design work. For measurement only, compute s_load_dwordx8 heap loads with an SGPR offset were routed like the pixel heap path, and a placeholder ResourceDescriptorHeap handle stood in for the token lookup. I then ran compile_dxil --refusals on the same 2,689-request cohort (main 1a0120b plus the earlier raw_and_typed_dword override):

    • requests that translate: still 487, no change;
    • direct indexed descriptor load shape (the 701): gone;
    • unbound numeric SGPR sN: 328 → 837, so about 500 of them stop there right after the texture load;
    • smaller next stops: gather through the sampler heap is not translated 84, non-contiguous resource descriptor 67, invalid pixel heap texture descriptor address 55.

    So the texture route alone translates nothing extra in this cohort either. Two fresh design reviews of the view-id-token design also found gaps in PR 1 (symbol routing, consumer checks, dimension validity). I'm parking stage 3 here and not opening a PR. The measurements above, the heap-size numbers and the view-id-token direction stay on this issue for whoever takes the unbound numeric SGPR chain next. The prototype was local getenv switches only, nothing pushed.

    AI-assisted.

  11. added a commit that references this issue on Nov 29, 2026
  12. JB2WIN commented on Nov 29, 2026

    @JB2WIN
    ContributorAuthor

    Status of stage 3 (GPU-selected heap textures in compute). I'm now building PR 1, the translator part only.

    The approach changed since my last update. A GPU-selected s_load_dwordx8 from the resource heap reads the 32-byte view token in title heap memory at the guest's own byte address. It checks the magic, the id range and the view dimension per id, and samples ResourceDescriptorHeap[base_dim + id]. A wrong token reads a null slot and bumps a per-wave miss counter. There is no slot mirror and no per-dispatch heap scan. The measured heap has about 1.25 M logical descriptors, of which about 15.5 k hold view tokens. It covers 1D, 2D and 2D array textures with loads, image_sample_l/_lz (literal or fixed sampler-heap samplers), gather4_lz with a literal sampler, and get_resinfo. A load with any other use keeps today's refusal.

    PR 1 publishes manifest lines that the runtime reader still refuses by name (heap token textures are not bound yet), so game runs do not change. The runtime part (a raw view over the bound heap's memory, per-dimension id tables, lifetime, states) is PR 2.

    @R0Y00, before I start PR 2, are you OK with that runtime outline? It is the one from issuecomment-7257080862, plus 1D and a dims table per view id.

    Prerequisites already up: #3500 (image fields), #3524 (fold), #3526 (comparison CLAMP_BORDER). #3076 stage 2a (runtime sampler heap) is in progress.

    AI-assisted.

  13. added a commit that references this issue on Nov 30, 2026
  14. added
    priority: P0
    on Nov 30, 2026
  15. JB2WIN commented on Nov 30, 2026

    @JB2WIN
    ContributorAuthor

    Measurement after #3544, so the next step goes to the right place. This is my local cohort of 2,689 slot-67 compute requests, counts only, with a temporary diagnostic that is not in any PR.

    Why a GPU-selected texture load (s_load_dwordx8 from the heap at an SGPR offset) is still not eligible under #3544's rules. This is the first failing rule per program:

    Rule Programs
    the same program also reads the heap through an s_add_u32 address (the buffer route) 874
    a word of the load's registers is read later with another symbol 289
    a token word read by a non-image instruction 34
    T# above s105 21
    unsupported image op 4

    The 289 case is overly strict: those registers are simply reused by a later table load. As a test I narrowed that rule to an unbound join loss and applied the buffer-route rule per load instead of per program. With both changes, the census barely moves: the heap refusal goes from 1323 to 1320, and 831 compile either way. With the textures handled, these programs stop at their buffer loads through the resource heap (s_load_dwordx4 at a GPU-selected offset, or the s_add_u32 form). That agrees with @Leyukaka's static count above, where 318 of 331 are raw buffer reads.

    A follow-up to #3544 that makes image field reads of token textures eligible moves 0 requests. So I'm not opening it for now.

    That makes the next lever GPU-selected buffer views through the same token route. The token records also cover buffer views (5,176 buffer records in the measured run), so a raw or typed buffer SRV per view id would be the buffer analogue of the per-dimension texture tables. I'll write that up as a design before building anything. @RonLogic, does this overlap the #3063 retarget work you mentioned above?

    AI-assisted.

  16. added a commit that references this issue on Nov 30, 2026
  17. lukaslw-lab commented on Nov 30, 2026

    @lukaslw-lab
    Contributor

    @JB2WIN, slot 57 mesh programs hit the same GPU-selected heap pattern on the buffer side. It is the largest C++ refusal by refused DispatchMesh calls in a 360 s native run (1.13 M; #3416, claimed in #2413). The chain:

    1. s_load_dwordx4 of a buffer SRV from a root descriptor table (an immediate offset);
    2. s_buffer_load_dword of a dword from it, the selector;
    3. s_lshl_b32 … 5, then s_load_dwordx4 from the resource heap at that SGPR offset;
    4. buffer_load_format_x … idxen: a one-channel typed read.

    The selector is not masked. A local prototype recognises the chain behind an opt-in .mslayout contract and emits ResourceDescriptorHeap[selector] as a TypedBuffer<U32> read. A source-built fixture covers it, with six refusal controls. It trusts the caller like the #2908 typed_dword contract, so an unmasked index stays unbounded.

    I would rather use your token guard than open that: read the token at the guest heap address, check the magic, id < count and the view type, read ResourceDescriptorHeap[base + id], otherwise read a null slot. For a typed buffer, the per-id record would hold the view type plus format or element size, not a texture dimension. Is the buffer route (your 874 programs) already planned on your side? If it is, I'll build the mesh variant on your helpers rather than a parallel one. If it isn't, I can take the typed-buffer check for mesh on top of #3544.

  18. lukaslw-lab commented on Nov 30, 2026

    @lukaslw-lab
    Contributor

    @JB2WIN, follow-up to my note above: I went ahead with the mesh side in #3619, translator only, using the same token shape as #3544.

    • Route: a heap descriptor at a GPU-computed byte offset (a value shifted left by 5 or more) is read through its view token: magic 0x56573258, view id, then a per-id kind table. A valid token reads ResourceDescriptorHeap[base + id] as a typed U32 buffer; anything else reads the null slot, and the first lane counts a miss.
    • Kind value 16 marks a one-channel 32-bit typed buffer. Your dimension table uses 1 + image dimension (at most 8), so one table could serve both routes. Could you keep 16 free for this?
    • Bindings: token memory and kind table (raw SRVs), a miss UAV, and base/count/null in cbuffer b1. They're opt-in (heap_tokens in .mslayout), and native preparation still refuses these programs.

    On the two top refused mesh programs (#3416), the refusal moves from the heap slot (+0x2a0 / +0x2a8) to v_readfirstlane_b32 (+0x470 / +0x474).

    When #3544 lands I'll switch to its helpers wherever they fit. The runtime binding for mesh comes next.

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    priority: P0

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions