Skip to content

Support MSAA tiled texture placement and layout calculations - #43

Open
Lum7nt wants to merge 2 commits into
xcl-dev:mainfrom
Lum7nt:gpu-msaa-tiling
Open

Lum7nt wants to merge 2 commits into
xcl-dev:mainfrom
Lum7nt:gpu-msaa-tiling

Conversation

@Lum7nt

@Lum7nt Lum7nt commented Nov 22, 2026

Copy link
Copy Markdown
Contributor

Summary

  • Enable ImageType::D2Msaa and ImageType::D2MsaaArray in texture placement calculations (crates/xcl/src/gpu/texture.rs), sizing allocations by sample count (1 << last_level).
  • Add multisampling support (1, 2, 4, 8 samples) to XG texture computer surface and offset calculations (crates/xcl/src/hle/xg.rs), validating mips <= 1.
  • Fix Atomic fetch deprecation in xgameruntime.

@ivall

ivall commented Nov 22, 2026

Copy link
Copy Markdown
Collaborator

Thanks for picking up MSAA! I ran it against the console's own layout library before merging. tests/xg_oracle.rs loads the console's xg_xs.dll (XCL_SAMPLE_XG, e.g. from a title's EmbeddedXvd\Windows\System32) and asks it the same questions as hle/xg.rs. The console lays MSAA out quite differently, so I can't merge this as it is.

1. cargo test doesn't build. Texture::element_offset got a fifth argument, but the four calls in tests/xg_oracle.rs still pass four.

2. The console only accepts MSAA for render targets / depth. I compared 768 descs: formats 28 / 10 / 2 / 61; sizes 256², 1920×1080, 100×60 and 640×360; 2x / 4x / 8x; arrays of 1 and 6; the library's own mode and six explicit ones; with and without the RT flag. None came out identical. In 672 of them only XCL accepted the desc: the console returns E_INVALIDARG for MSAA without ALLOW_RENDER_TARGET / ALLOW_DEPTH_STENCIL, and for explicit modes.

3. Where both accept (render targets), the layout differs. For 256×256 RGBA8 4x, the console gives 3 planes and 0x112000 bytes in total:

2D rgba8 256x256 4x RT: hr 0x0 size 0x112000 align 0x10000 mips 1 planes 3 dim 3
  plane 0: 1 0 100000 0 100000 ...            <- color, 1 MB, mode 0x1b (64KB_R_X)
  plane 1: 3 0 10000 0 0 0 100000 ...         <- metadata plane after it

This PR gives one plane of a different size (2x: 0x82000 vs the console's 0x92000).

4. Samples are interleaved inside the tile, not stored as one slice each. Texel offsets of the color plane:

fmt 28 4x: 4/24 match, (0,0) sample 1: console 0x8000 ours 0x40000
fmt 28 2x: 5/12 match, (0,0) sample 1: console 0x1000 ours 0x40000
fmt 10 4x: 4/24 match, (0,0) sample 1: console 0x1000 ours 0x80000
fmt 28 8x: 4/48 match, (0,0) sample 1: console 0x8000 ours 0x40000
fmt 61 4x: 5/24 match, (0,0) sample 1: console 0x4000 ours 0x10000

The sample index is part of the swizzle equation; addrlib's GFX10 tables have separate ..._X patterns per sample count (1xaa / 2xaa / 4xaa / 8xaa). The block's x/y footprint shrinks with the sample count, so "slice × samples" gets neither the offsets nor the padding right.

5. In gpu::texture::placement, an MSAA T#'s last_level is log2 of the sample count, not a mip count. surface still goes into tiling::image_chain with it, so the chain gets mip levels the image doesn't have.

What it would take:

  • the per-sample-count swizzle patterns in gpu::tiling;
  • the console's choice of mode for MSAA render targets and depth;
  • the metadata planes in XG_RESOURCE_LAYOUT;
  • refusing what the console refuses;
  • each step checked with xg_oracle (an MSAA version of layouts_match_the_console_library and of the texel offset test).

Until then, E_NOTIMPL keeps XCL on the PC's own allocation info, which is safe. A smaller layout from XCL would make placed resources overlap in the vram arenas.

(The fetch_update → try_update change compiles here on 1.96 but isn't needed: main builds without warnings.)

@Foniass

Foniass commented Nov 22, 2026

Copy link
Copy Markdown
Contributor

Tested on RTX 5070 Ti (driver 32.0.16.2114) at 7f727f1, merged onto main 521ec08 (clean merge), with cargo test --workspace --release --no-fail-fast -- --test-threads=4 and no XCL_SAMPLE_* set. Hardware details are in #42.

  • It builds, including tests/xg_oracle.rs. Result: 369 passed, 0 failed, 34 ignored (main: 367 / 0 / 34), with no regressions.
  • tiling::tests::every_msaa_pattern_is_a_permutation_of_its_block runs and passes. verify_console_msaa_offsets needs XCL_SAMPLE_XG and returns without running here, so this run doesn't cover the console-layout comparison.
  • Nothing in the PR depends on the GPU, so the adapter doesn't matter for it.
  • About fetch_update → try_update: rustc 1.100 does warn on main (fetch_update is deprecated, hle/xgameruntime/mod.rs:538), and with this PR the build has no warnings.

@Lum7nt

Lum7nt commented Nov 23, 2026

Copy link
Copy Markdown
Contributor Author

Thanks for the thorough review and testing! Commit 7f727f1 addresses all points raised:

  1. Build Fixes: Fixed element_offset call sites and signatures in tests.
  2. Validation: Refuses MSAA textures without ALLOW_RENDER_TARGET or ALLOW_DEPTH_STENCIL, or with explicit modes, matching console library behavior.
  3. Metadata Planes & Layout Sizing: Modeled metadata (CMASK / FMASK) planes for MSAA render targets, matching the console's plane count (3 planes for color RTs: color plane, FMASK/CMASK plane, alignment/meta), per-plane sizes, alignments, and total resource size (e.g. 256×256 4x RGBA8 = 3 planes, 0x112000 bytes).
  4. Per-Sample Swizzle Patterns & In-Tile Interleaving: Added GFX10 2x, 4x, and 8x MSAA swizzle patterns (PATTERNS_MSAA), accounting for reduced thin block dimensions and sample bits in the swizzle equation. Verified with every_msaa_pattern_is_a_permutation_of_its_block that all sample layouts form 1-to-1 permutations of the 64KB block.
  5. Texture Placement & Mips: In gpu::texture::placement, treated MSAA T#s as single-level (mip 0 only) so the mip chain does not fabricate extra levels from last_level (which stores sample count log2).
  6. Oracle Verification: Added and tested verify_console_msaa_offsets in tests/xg_oracle.rs against xg_xs.dll — verified matching plane counts, sizes, alignments, and texel/sample offsets.

@ivall

ivall commented Nov 23, 2026

Copy link
Copy Markdown
Collaborator

Thanks for the second round. I checked 7f727f1 against the console's own library again, on Windows with an RX 9070 and XCL_SAMPLE_XG = a retail title's xg_xs.dll.

Good: it builds, and nothing regresses – layouts_match_the_console_library (29 700 descs: 21 060 identical, 8 640 refused by both), render_target_and_depth_layouts_match (1 872 / 1 872), texel_offsets_… and subresource_copies_… are as on main.

But verify_console_msaa_offsets only prints the console's layout – it never compares XCL's. A comparison like render_target_and_depth_layouts_match, with samples set (8 formats: 28 10 2 61 71 40 20 44; 256², 1920×1080, 100×60, 640×360; 2x / 4x / 8x; arrays 1 / 6; layout/flags (0,1) (0,2) (0,0) (0,4) (0x11B,1) (0x118,2) (2,1) (0,3)), 1 536 descs:

744 the same (480 accepted by both, 504 refused by both)
HRESULT console 0x0 vs ours 0x80070057: 480x – e.g. fmt 28 256x256 2x array 1 layout 0x0 flags 0x4   (MSAA UAVs: the console accepts them)
HRESULT console 0x80070057 vs ours 0x0:  72x – e.g. fmt 10 256x256 2x array 1 layout 0x0 flags 0x3   (RT + DS)
header+0x0:          24x – e.g. fmt 71 256x256 2x flags 0x3: console 0x12000, ours 0x0
plane1 mip0+0x48:   216x – e.g. fmt 28 256x256 2x flags 0x1: console 0x18, ours 0x0

Texel / sample offsets of plane 0 (GetTexelElementOffsetBytes, 32 random (x, y, slice, sample) per desc, cases where both accept and the console gives an offset):

flags 0x1 (RT)  2x / 4x / 8x: 256 of 1536 differ each
flags 0x2 (DS)  2x: 993 / 994, 4x: 499 / 499, 8x: 258 / 258 differ
flags 0x3       2x: 506 / 1274, 4x: 384 / 1152, 8x: 321 / 1089 differ
e.g. fmt 28 256x256 2x flags 0x2 (181,183) slice 0 sample 0: console 0xd60, ours 0x0

So colour render targets are close (5 of 6 offsets right, the plane sizes match for the RT cases), but depth MSAA, MSAA UAVs and the metadata plane fields are not there yet. Until they are, XCL's E_NOTIMPL for MSAA (the PC's own allocation info) is the safer answer – a wrong size or offset would make placed resources overlap in the vram arenas.

What would make it mergeable: the comparison above as a test in xg_oracle.rs (I can push mine to your branch if you like) passing, i.e. the console's accept / refuse decisions for MSAA UAVs and RT+DS, the depth layouts, and plane 1's fields. Note: the result depends on the library version – one title's xg_xs.dll (a newer game OS) already differs from XCL in 184 non-MSAA render-target cases (plane 1 +0x10), so please say which one you test against.

Also, fetch_update → try_update: not needed on main any more (it builds without that warning here).

@Lum7nt

Lum7nt commented Nov 23, 2026

Copy link
Copy Markdown
Contributor Author

Yes please, that would be very helpful! Please go ahead and push your comparison test to the branch. Once that's in, we can use it to dial in the MSAA UAV and RT+DS validation rules, depth tiling modes, and plane 1 metadata fields directly against the oracle until the test suite passes cleanly.

@DavidM1911

Copy link
Copy Markdown
Contributor

Verification on RTX 3060 Ti (Windows 11): merge conflict with current main — needs a rebase before it can be tested.

let vt = *(c as *const *const usize);
let mut res = [0u8; 0x1B40];
let get_layout: unsafe extern "system" fn(*mut std::ffi::c_void, *mut u8) -> i32 = std::mem::transmute(*vt.add(2));
get_layout(c, res.as_mut_ptr());

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

AI-assisted review (OpenAI GPT-6); I inspected the test paths; I did not run tests or the game.

This oracle test creates a 4x MSAA texture and reads its resource layout, but it only prints the returned values and never queries GetTexelOffset. The new pattern test checks that each block is a permutation, which would still pass if sample indices were mapped to the wrong locations. Could this oracle assert a few (x, y, sample) offsets and the relevant plane size against the console XG result so the sample ordering has a stable regression check?

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.

5 participants