Skip to content

Recompiler: pass spatial-only coords to CalculateLevelOfDetail for array textures - #3362

Open
Kalmarv wants to merge 1 commit into
xcl-dev:mainfrom
Kalmarv:fix/hlsl-array-calculate-lod
Open

Kalmarv wants to merge 1 commit into
xcl-dev:mainfrom
Kalmarv:fix/hlsl-array-calculate-lod

Conversation

@Kalmarv

@Kalmarv Kalmarv commented Nov 29, 2026

Copy link
Copy Markdown
Contributor

What changed

Fixed HLSL CalculateLevelOfDetail / CalculateLevelOfDetailUnclamped coordinate arguments for array textures (DIM 4 Texture1DArray and DIM 5 Texture2DArray) in crates/xcl/src/gpu/recomp/emit.rs:

  • Added lod_coord(dim, c) returning spatial-only coordinates (float for 1D/1DArray, float2 for 2D/2DArray, float3 for 3D, and cube_dir(...) for CubeArray), omitting the array slice index.
  • Used lod_coord for coord0 (used by CalculateLevelOfDetail on implicit-LOD image_sample_*_o) and in image_get_lod (for both CalculateLevelOfDetail and CalculateLevelOfDetailUnclamped), matching the direct DXIL path (crates/xcl/src/gpu/dxil/lower_image.rs).
  • Added gpu::recomp::tests::ps_array_texture_calculate_lod_uses_spatial_coords in crates/xcl/src/gpu/recomp/tests.rs verifying HLSL codegen, DXC ps_6_6 compilation, and GPU pixel-shader draws for IMAGE_GET_LOD and implicit-LOD IMAGE_SAMPLE_O on both Texture1DArray and Texture2DArray.

Why

In HLSL, Texture1DArray::CalculateLevelOfDetail(Unclamped) takes a float location and Texture2DArray::CalculateLevelOfDetail(Unclamped) takes a float2 location (array slices do not participate in derivative/LOD calculation). Previously emit.rs passed float2(u, round(slice)) for Texture1DArray and float3(u, v, round(slice)) for Texture2DArray in both image_get_lod and coord0, causing a DXC compilation error for any pixel shader using image_get_lod or implicit-LOD image_sample_*_o on 1D/2D array textures.

Tested

  • cargo check --workspace --all-targets: 0 errors, 0 new warnings.
  • Targeted CPU + DXC + GPU tests (cargo test -p xcl --lib -- ps_array_texture_calculate_lod_uses_spatial_coords gpu_ps_image_sample_offset_implicit_lod_between_mips ps_get_lod_with_a_static_sampler) on NVIDIA GeForce RTX 5070 Ti (driver 32.0.16.1074, Windows 11 Pro Build 26200, DXC 1.9.2609.5): 3 passed, 0 failed, 0 ignored (0.42 s).
  • Full workspace GPU test suite (cargo test --workspace -- --test-threads=4, XCL_DXC_DIR set to DXC 1.9.2609.5) on NVIDIA GeForce RTX 5070 Ti: exit 0 (96.4 s).
  • Real-title 150 s regression smoke run on PR head (dd7a190a7fa373eb7c94f84410b7107fc8130f46, 0 minidumps, 0 display events):
xcl-smoke v1
commit: dd7a190a7fa373eb7c94f84410b7107fc8130f46
gpu: NVIDIA GeForce RTX 5070 Ti
title: ea3b5e4d
seconds: 150
exit: 0x0
frames shown: 1560 (PresentX 1560)
presents per 2 s (last 3): 0 0 0
device removals: 0
crashes: 0
peak commit: 33597 MB
  • Ancestor main 150 s baseline smoke run (f0e8fdd5c3942ddd399d5ee11384c8595c5aacff):
xcl-smoke v1
commit: f0e8fdd5c3942ddd399d5ee11384c8595c5aacff
gpu: NVIDIA GeForce RTX 5070 Ti
title: ea3b5e4d
seconds: 150
exit: 0x0
frames shown: 1695 (PresentX 1695)
presents per 2 s (last 3): 0 0 0
device removals: 0
crashes: 0
peak commit: 31546 MB

Not tested

  • AMD or Intel GPUs (tested on NVIDIA GeForce RTX 5070 Ti only).
  • Real-title execution under forced HLSL fallback (XCL_DIRECT_DXIL=none); the smoke run used XCL_DIRECT_DXIL=all.

AI use

Implemented and verified with Antigravity (Gemini) assistance; all changes, generated HLSL, DXC compilation, and real-GPU test/smoke results were directly reviewed and run on local hardware.

@github-actions github-actions Bot added area:recompiler RDNA2 decoder, HLSL/direct-DXIL recompiler, wave64/wave32 area:tests Unit, integration, e2e tests, or testtitle gpu:both Code affects both AMD and NVIDIA GPUs (scope tag; see needs:*-test for remaining checks) labels Nov 29, 2026
@Foniass

Foniass commented Dec 1, 2026

Copy link
Copy Markdown
Contributor

Review (head dd7a190, diff read, not a merge-check: CI is red (the pipeline_library race #3221 fixed), so not built or run here yet):

  • Merged onto current main, this breaks HLSL compilation of biased comparison samples on arrays. Since 7d1c008 (after this PR's base), the image_sample_c_b* path takes its gradients from coord0 (main emit.rs ~2854-2858) and passes them to sample_cmp_grad, which declares them with the per-DIM type (float3 for DIM 5, float2 for DIM 4; ~2531-2535). With coord0 = lod_coord(...), DIM 5 assigns a float2 gradient to a float3, which DXC rejects. DIM 4 splats a scalar into a float2, moving the layer coordinate by du*2^bias. The only test of this path (gpu_ps_compare_mip_bias) uses DIM 1, so CI won't catch it. Keeping the array-shaped coord0 for sample_cmp_grad, and adding a sample_c_b test on DIM 4/5, would fix and cover it.
  • The "Why" overstates it. On main the array CalculateLevelOfDetail(Unclamped) call isn't a compile error: DXC warns about implicit truncation, and the truncated coordinate is exactly (u, v) / u, so main already returns the same LOD. The new test fails on main only through its !hlsl.contains(...) check; its DXC and GPU parts would pass there too. So this removes a warning rather than fixing a wrong LOD.
  • Tested gives "exit 0" without counts, AI use says "Antigravity (Gemini)" without the exact model, and XCL_DIRECT_DXIL=none isn't a real setting (0 is HLSL-only).

Fine: the lod_coord mapping matches direct DXIL's spatial / calculate_lod, cubes are unchanged, and the returned channels (clamped, unclamped, 0, 0) match.

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

@Foniass

Foniass commented Dec 1, 2026

Copy link
Copy Markdown
Contributor

merge-check
base: b2b1f10
head: dd7a190
merge: clean
build: cargo build --workspace -> ok
tests: cargo test --workspace --no-fail-fast -- --test-threads=4 -> 2903 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: re-check of the head I reviewed earlier (dd7a190; my review comment has the details), now run because the PR's only red CI was the pipeline_library race fixed on main. Findings: (1) Merged onto current main, this breaks HLSL compilation of biased comparison samples on arrays (from reading). Since 7d1c008, the image_sample_c_b* path takes its gradients from coord0 (emit.rs ~2854-2858) and passes them to sample_cmp_grad, which declares them with the per-DIM type (float3 for DIM 5, float2 for DIM 4; ~2531-2535). With coord0 = lod_coord(...), DIM 5 assigns a float2 gradient to a float3, which DXC rejects, and DIM 4 splats a scalar into a float2, moving the layer coordinate by du*2^bias. The only test of that path (gpu_ps_compare_mip_bias) uses DIM 1, so the suite below doesn't catch it. Keeping the array-shaped coord0 for sample_cmp_grad, and adding a sample_c_b test on DIM 4 / 5, would fix and cover it. (2) On main the array CalculateLevelOfDetail call already returns the same LOD (DXC truncates the coordinate to (u, v) / u with a warning), so this removes a warning rather than fixing a wrong LOD; the new test fails on main only through its !hlsl.contains(...) check. (3) Tested gives "exit 0" without counts, and AI use names no exact model. Fine: the lod_coord mapping matches direct DXIL's spatial / calculate_lod, cubes are unchanged, and the returned channels match.
performance: 60 s smoke of a retail title, main b2b1f10 vs this merge (dea0406b): 1671 -> 1338 frames shown (80 %; last presents per 2 s: 60 60 60), peak commit 17394 -> 16826 MB, 0 device removals, 0 crashes. The run is at 80 % of main's frames, the 80 % limit; most likely the one-time retranslation, since emit.rs is part of the translator's cache identity (a second run would confirm). No shader compile error in its log.
verdict: not yet: merged onto current main, the biased comparison sample path on array textures (DIM 4 / 5) gets a gradient of the wrong width (finding 1): DXC rejects DIM 5 and DIM 4 moves the layer coordinate. The suite can't see it (its only test of that path is 1D); keep the array-shaped coord0 for sample_cmp_grad and add a DIM 4 / 5 test
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 dd7a190a: 1 finding(s). These are on lines outside the diff:

crates/xcl/src/gpu/recomp/emit.rs:2807: In the function this touches, the mip used to scale texel offsets for implicit-LOD _o samples is round(CalculateLevelOfDetail(coord0)), which ignores the instruction's bias (sample_b_o) and LOD clamp (sample_cl_o). A PS image_sample_b_o with bias +2 and a computed LOD of 0 samples about mip 2 through SampleBias, but texel_offset divides the offset by mip 0's width, so it moves 1/4 texel instead of 1 and differs from the console. The same applies when cl is above the computed LOD.


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.

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

Labels

area:recompiler RDNA2 decoder, HLSL/direct-DXIL recompiler, wave64/wave32 area:tests Unit, integration, e2e tests, or testtitle gpu:both Code affects both AMD and NVIDIA GPUs (scope tag; see needs:*-test for remaining checks)

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants