Skip to content

Shaders: support image_sample_b with a16 and _cl LOD-clamp variants - #73

Closed
Yaekai wants to merge 2 commits into
Marvisak:mainfrom
Yaekai:feat/shader-image-sample-a16-cl
Closed

Yaekai wants to merge 2 commits into
Marvisak:mainfrom
Yaekai:feat/shader-image-sample-a16-cl

Conversation

@Yaekai

@Yaekai Yaekai commented Nov 24, 2026

Copy link
Copy Markdown
Contributor

In GFX10/RDNA2 MIMG instructions with a16=1, offset (_o), bias (_b), and z-compare (_c) remain full 32-bit VGPRs at the start of vaddr, while gradients, coordinates, LOD, and clamp are packed in 16-bit pairs starting after them (matching gfx10::fields::image_addr_dwords).

Changes

  • Split image address reading in crates/victory-shader/src/emit/lower.rs into full 32-bit prefix VGPRs (addr_u32) and a16-packed trailing components (addr_packed), removing the a16 + image_sample_b bailout.
  • Added support for _cl LOD-clamp variants (image_sample_cl, image_sample_b_cl, image_sample_d_cl, image_sample_c_cl) by passing the trailing clamp component to DXIL's clamp argument.
  • Moved the unsupported cube sample check (unorm / gradients / get_lod) ahead of coordinate reads so cube gradient samples report the right reason instead of an out-of-bounds address.

Verification

  • cargo test -p victory-shader: 57/57 passed.
  • victory translate --validate on GTA VI shaders.rpf (26,084 programs):
    • a biased image sample with 16-bit coordinates: 108 → 0
    • image_sample_d_cl: 27 → 0
    • Emitted VS/PS DXIL programs: 12,272 → 12,316 (+44 complete shaders, all passing dxil.dll validation with 0 new validator errors).

@DaviTostes

Copy link
Copy Markdown

Tested 6c8af09 on Arch Linux x86_64, rustc 1.98.1: pass, no regression only.

  • cargo test -p victory-shader: 51 passed, 0 failed (2 test binaries). The diff adds no test for the image_sample_b a16 / _cl LOD-clamp variants, so the new paths are unexercised; a fixture per variant would cover them.
  • The full workspace still needs Build: Linux workspace, contributor setup and CI #112 for the victory-gpu E0425.

No game data used. AI-assisted (DeepSeek V4.1 Flash coding agent).

@Sunkiller-qc

Copy link
Copy Markdown

With a16 set, the LOD bias is an f16 in the low half of its own dword, and the upper 16 bits are undefined. See LLVM's SITargetLowering::lowerImage: "Bias is of type half but occupies full 32-bit" (D111754/D116038). Here the whole dword is read as an f32 (crates/victory-shader/src/emit/lower.rs:5555-5556), so an a16 image_sample_b* gets a garbage bias. Converting the low half with legacyF16ToF32 would fix it. #160 does it that way and points out the disagreement. #136 has the same code (lower.rs:5660-5661).

Static review, not built or run.

@r4tedx

r4tedx commented Nov 24, 2026

Copy link
Copy Markdown
Contributor

A note from comparing this with #160, which covers the same a16 bias and _cl forms: the two read the a16 bias differently. Here the bias dword is taken as a full f32 (addr_u32), in #160 as an f16 in the low half of that dword. LLVM's SITargetLowering::lowerImage has:

// Special handling of bias when A16 is on. Bias is of type half but
// occupies full 32-bit.
SDValue Bias = DAG.getBuildVector(
    MVT::v2f16, DL,
    {Op.getOperand(ArgOffset + I), DAG.getPOISON(MVT::f16)});

so with a16 the bias keeps its own dword but holds a half in the low 16 bits (the upper half is undefined); the offset and the comparison reference stay 32-bit. Read as an f32, an f16 bias comes out as a denormal near zero, so the sample would lose its bias without failing.

One related point I got wrong myself and fixed in #160: on GFX10.3 a16 does not make the derivatives 16-bit (only the _g16 opcodes do); LLVM's gfx10_asm_mimg.s has image_sample_d ... 2D a16 with five address dwords. Worth checking here if _d forms with a16 pass through this code.

From the sources only, not run on hardware. No game data used.

@Yaekai
Yaekai force-pushed the feat/shader-image-sample-a16-cl branch from 6c8af09 to 3eab56a Compare November 24, 2026 01:56
@Yaekai

Yaekai commented Nov 24, 2026

Copy link
Copy Markdown
Contributor Author

Thanks @Sunkiller-qc and @r4tedx for the great catches! Updated and rebased onto d2a4bae in 3eab56a:

  • With a16, LOD bias is now masked to its low 16 bits and converted via legacyF16ToF32 instead of being bitcast as f32.
  • Non-_g16 image_sample_d* derivatives now stay 32-bit (addr_u32 + as_float, and image_addr_dwords in gfx10/fields.rs only halves derivative dwords for _g16 opcodes).
  • Re-verified cargo test -p victory-shader (71/71 passed) and victory translate --validate on shaders.rpf (0 new validator errors).

edsuuu referenced this pull request in edsuuu/VI-BR-PORT Nov 24, 2026
…amp variants

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@ghost ghost mentioned this pull request Nov 24, 2026
@slint6614-dev

Copy link
Copy Markdown

Measured on the full retail shader pack (26,503 containers) with victory translate --validate, against main d2a4bae:

VS/PS validated CS validated MS validated cargo test -p victory-shader
main 12172 1670 8129 ok
#569 12208 1670 8129 ok
#73 12208 1670 8129 ok
#318 12208 1670 8129 ok
#306 12316 1693 8138 ok

All four clear the 108 "biased image sample with 16-bit coordinates" programs identically. #73 and #318 also clear the 27 image_sample_d_cl refusals, but those programs then stop on another reason (emitted count unchanged). #306 additionally clears 15 of the 16 validator error kinds (only the mesh 32 KiB output size remains, see #459); it overlaps #554/#565/#381. Windows 11, dxil.dll from SDK 10.0.26100.

In GFX10/RDNA2 MIMG instructions with a16=1, offset (_o), bias (_b) and z-compare (_c) remain full 32-bit VGPRs at the start of vaddr, while gradients, coordinates, LOD and clamp are packed in 16-bit pairs starting after them (matching gfx10::fields::image_addr_dwords).

- Split address reading into full 32-bit prefix VGPRs and a16-packed trailing components, removing the a16+image_sample_b bailout.
- Support _cl LOD-clamp variants (image_sample_cl, image_sample_b_cl, image_sample_d_cl, image_sample_c_cl) by passing the trailing clamp component to DXIL's clamp parameter.
- Check unsupported cube sample modes before reading coordinates so cube gradient samples report the right error instead of an out-of-bounds address.
edsuuu referenced this pull request in edsuuu/VI-BR-PORT Nov 24, 2026
The tester tree (upstream main ffe310e plus its PRs, update 8; see its
INTEGRATION.md) ran 11-25 minutes in the world on an RTX 5070 without a
device removal. Where both sides carry the same PR (the wave64 stack
#223/#233/#320 with #439 and #180, #454, #336, #259, #331 and others), the
tester tree's newer head is taken; our own work and the PRs only we
merged are kept.

Conflicts, file by file:

- descriptor/host.rs: the tester layout (`spare`, the guard slot after the
  dynamic slots, `free_table`, the capped sampler heap). A capped view
  heap keeps `capped_reserve()` dynamic slots, now 2^18 by default (the
  tester measurement, and our 91fed158), still `VICTORY_DYNAMIC_SLOTS` in
  diagnostics builds; uncapped hosts keep our 2^19. Our slot statistics
  (TAKEN/PEAK/SHORT), the bindless plan cache (`CommittedRegions`,
  `timed_valid_title_address`, `timed_find`, `title_bounds`) and the
  revision triple are kept beside the tester's `guard_slot`.
- Buffer states: the tester tree's buffer-state tracker (#334: every list
  moves host copies of title memory between GENERIC_READ and
  UNORDERED_ACCESS and back to common before it closes) and depth twins
  (#469) are taken. #40's enhanced/legacy indirect barriers (the indirect
  prelude, `use_indirect_buffers`, the title buffer handoff mirrored in
  `host_barrier`) cannot share buffers with that tracker and are dropped;
  #40's argument checks (`byte_len`, exact spans, overflow-safe clamp
  shader, aligned counts) stay. `indirect::bound` and #282's
  `rectangles` go through `buffer_state`, and `indirect::execute` (#334)
  replaces `done`. #178's DXC fallback compiles both of our passes.
  Our segments (#387: `close` ends the segment, `close_native` closes a
  native list) carry the tracker: each native list returns its buffers
  to common and its twins to their depth targets, and each prelude also
  holds our texture uploads.
- shadow.rs: our write flow (`queue_write`, one `snapshot` after the
  batch's refreshes, `finish_writes`, the unchanged-page cache) with
  #261's copy-back that also gives the snapshot the title's bytes; its
  tests run through our flow.
- pipeline/make.rs: #275's per-frame budget and per-pipeline wake pair,
  with our compute waits (a dispatch waits for its pipeline, up to 20 s).
- pipeline/library.rs: the tester version (#347, #275's saver thread);
  our concurrent-write test kept. shader_cache.rs: our byte-returning
  cache API with #358's "translated, only not kept".
- draw.rs: #493's root SRV tables; where no table can stand in, our
  drop of a draw whose root view is unset or outside title buffers. Our
  refusal of a table offset past the capped heap stays.
- resource.rs: `HostTexture` has both our `staged` generation and the
  depth twin.
- Shaders: the tester's lower.rs for the wave64 stack, #160's image
  sampling (it covers our #73 and #136 sample forms) and #453/#587; kept
  from our side: #136's preference for an image or buffer use of a
  descriptor read as data and its `s_buffer_` arm, #422's nearest-even
  narrowing (also for #169's d16 loads and #453's interpolation), #452's
  spin-wait limit, #391's LDS limit, #302's spilled root arguments (now in
  `NativeProgram::translation_ir`), #136's PSV0 interpolation beside the
  tester's row harmonisation. split_tests.rs, lane_read_loop_tests.rs and
  docs/wave64-on-wave32.md are the tester versions (the same PRs at newer
  heads).
- Tests: #483's heap guard dwords filled in our GPU tests of #200
  (v_div_scale) and #437 (scalar loads from a raw view), which read
  through the guard slot otherwise; #334's debug-layer test passes an
  offset to our `indirect::bound`.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
MiguVT pushed a commit to MiguVT/VIctorINUX that referenced this pull request Nov 24, 2026
…ndards

Docs: contributing guide points at the map, code standards and glossary added
github-actions Bot added a commit to KevinPratap/VIctory that referenced this pull request Nov 24, 2026
github-actions Bot added a commit to KevinPratap/VIctory that referenced this pull request Nov 24, 2026
@Marvisak Marvisak closed this Nov 24, 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.

6 participants