gpu, metal: bind cube/3D views, fix UAV mip words, and wire heap shadow samplers #577

Open
Sheffy007 wants to merge 1 commit from upstream-pr/577 into main
Sheffy007 commented 2026-11-28 06:16:01 +00:00 (Migrated from github.com)

Imported from LakshanSivakumar/Native-Victory-macOS. Originally posted by Sheffy007 on 2026-11-28T06:16:01Z.


Closes #573
Refs #258, #521
cc @Olima34

Summary

Fixes four native GPU/Metal view and sampler bugs affecting world lighting, shadow comparisons, cubemap probes, and UAV size queries:

  1. Cube, CubeArray, 3D, and 2D/Array MTLTextureType reinterpretation (victory-backend, victory-gpu::native, victory-metal):
    • descriptors::decode / texture_view now decode TEXTURE_1D..TEXTURE_CUBE_ARRAY (2..=10) with explicit ViewDim (D2, D2Array, D3, Cube, CubeArray), mip ranges (MostDetailedMip, MipLevels / MipSlice), and face/layer ranges ((0, 6) for TEXTURE_CUBE, (First2DArrayFace, 6 * NumCubes) for TEXTURE_CUBE_ARRAY).
    • Metal::create_texture_view reinterprets 2D/array backing textures to MTLTextureType::TypeCube, TypeCubeArray, Type2D, or Type2DArray (and Type3D mip ranges), while validating dimensions, slice counts, multisampling, and buffer-backed textures before calling newTextureView so stale or mismatched descriptors return Err instead of tripping Metal ObjC assertions.
  2. Dynamic shadow comparison samplers, LOD clamps, and D3D12_SAMPLER_DESC (victory-gpu::sampler, victory-gpu::native::descriptors, victory-metal):
    • MTLSamplerDescriptor defaults compareFunction to MTLCompareFunction::Never, and SamplerDesc previously omitted compare, lod_min, lod_max, and Address::MirrorOnce. Every heap comparison sampler (CreateSamplerX with words[16] in LESS..=ALWAYS or CreateSampler with D3D12_FILTER_COMPARISON_*) evaluated Never (0.0, 100% shadowed) on image_sample_c* / image_gather4_c*.
    • Wired compare, lod_min, lod_max (including mip == 0 clamping to min_lod), Address::MirrorOnce (MirrorClampToEdge), and 13-dword View::Sampler (d3d12_fields) into SamplerDesc and dynamic_sampler_descriptor.
  3. UAV MipSlice in descriptor::image_words (victory-gpu::descriptor):
    • In D3D12_UNORDERED_ACCESS_VIEW_DESC, MipSlice is at d[2] (d[4] holds ArraySize / WSize / 0). image_words previously read d[4] on unordered views, corrupting image_get_resinfo dimensions on RWTexture2D (mip > 0), RWTexture2DArray, and RWTexture3D (WSize = u32::MAX).

Verification

  • cargo test -p victory-metal --locked -- --test-threads=1
  • cargo test -p victory-gpu --lib descriptor::tests:: --locked -- --test-threads=1
  • cargo test -p victory-gpu --lib native::descriptors::tests:: --locked -- --test-threads=1
  • cargo test -p victory-gpu --lib native::tests::cube_tests:: --locked -- --test-threads=1 (end-to-end GFX10 -> MSC -> Metal GPU tests sampling all 6 TextureCube faces and evaluating image_sample_c_lz with both ConsoleSampler and D3D12_SAMPLER_DESC comparison samplers)
  • cargo clippy --workspace --all-targets --locked --target x86_64-pc-windows-gnullvm -- -D warnings (plain and --all-features)
  • cargo xtask policy
<!-- greyfinch-sync:LakshanSivakumar/Native-Victory-macOS:issue:5537379336 --> Imported from [LakshanSivakumar/Native-Victory-macOS](https://github.com/LakshanSivakumar/Native-Victory-macOS/pull/577). Originally posted by [Sheffy007](https://github.com/Sheffy007) on 2026-11-28T06:16:01Z. --- Closes #573 Refs #258, #521 cc @Olima34 ### Summary Fixes four native GPU/Metal view and sampler bugs affecting world lighting, shadow comparisons, cubemap probes, and UAV size queries: 1. **Cube, CubeArray, 3D, and 2D/Array `MTLTextureType` reinterpretation (`victory-backend`, `victory-gpu::native`, `victory-metal`):** - `descriptors::decode` / `texture_view` now decode `TEXTURE_1D`..`TEXTURE_CUBE_ARRAY` (`2..=10`) with explicit `ViewDim` (`D2`, `D2Array`, `D3`, `Cube`, `CubeArray`), mip ranges (`MostDetailedMip`, `MipLevels` / `MipSlice`), and face/layer ranges (`(0, 6)` for `TEXTURE_CUBE`, `(First2DArrayFace, 6 * NumCubes)` for `TEXTURE_CUBE_ARRAY`). - `Metal::create_texture_view` reinterprets 2D/array backing textures to `MTLTextureType::TypeCube`, `TypeCubeArray`, `Type2D`, or `Type2DArray` (and `Type3D` mip ranges), while validating dimensions, slice counts, multisampling, and buffer-backed textures before calling `newTextureView` so stale or mismatched descriptors return `Err` instead of tripping Metal ObjC assertions. 2. **Dynamic shadow comparison samplers, LOD clamps, and `D3D12_SAMPLER_DESC` (`victory-gpu::sampler`, `victory-gpu::native::descriptors`, `victory-metal`):** - `MTLSamplerDescriptor` defaults `compareFunction` to `MTLCompareFunction::Never`, and `SamplerDesc` previously omitted `compare`, `lod_min`, `lod_max`, and `Address::MirrorOnce`. Every heap comparison sampler (`CreateSamplerX` with `words[16]` in `LESS..=ALWAYS` or `CreateSampler` with `D3D12_FILTER_COMPARISON_*`) evaluated `Never` (`0.0`, 100% shadowed) on `image_sample_c*` / `image_gather4_c*`. - Wired `compare`, `lod_min`, `lod_max` (including `mip == 0` clamping to `min_lod`), `Address::MirrorOnce` (`MirrorClampToEdge`), and 13-dword `View::Sampler` (`d3d12_fields`) into `SamplerDesc` and `dynamic_sampler_descriptor`. 3. **UAV `MipSlice` in `descriptor::image_words` (`victory-gpu::descriptor`):** - In `D3D12_UNORDERED_ACCESS_VIEW_DESC`, `MipSlice` is at `d[2]` (`d[4]` holds `ArraySize` / `WSize` / `0`). `image_words` previously read `d[4]` on unordered views, corrupting `image_get_resinfo` dimensions on `RWTexture2D` (`mip > 0`), `RWTexture2DArray`, and `RWTexture3D` (`WSize = u32::MAX`). ### Verification - `cargo test -p victory-metal --locked -- --test-threads=1` - `cargo test -p victory-gpu --lib descriptor::tests:: --locked -- --test-threads=1` - `cargo test -p victory-gpu --lib native::descriptors::tests:: --locked -- --test-threads=1` - `cargo test -p victory-gpu --lib native::tests::cube_tests:: --locked -- --test-threads=1` (end-to-end GFX10 -> MSC -> Metal GPU tests sampling all 6 `TextureCube` faces and evaluating `image_sample_c_lz` with both `ConsoleSampler` and `D3D12_SAMPLER_DESC` comparison samplers) - `cargo clippy --workspace --all-targets --locked --target x86_64-pc-windows-gnullvm -- -D warnings` (plain and `--all-features`) - `cargo xtask policy`
AnastasisKon commented 2026-11-28 18:08:57 +00:00 (Migrated from github.com)

Imported from LakshanSivakumar/Native-Victory-macOS. Originally posted by AnastasisKon on 2026-11-28T18:08:57Z.

Review: APPROVED


Reviewed and verified locally on head c734a4fd (worktree /private/tmp/review-577):

  • cargo test -p victory-backend -p victory-gpu -p victory-metal --locked -- --test-threads=1: all green (454 passed in victory-gpu lib, 48+13+21 in victory-metal, 10 in victory-backend), including the end-to-end Metal GPU tests: cube_srv_and_d3d12_sampler_sample_all_six_faces_on_metal, shadow_comparison_samplers_evaluate_depth_comparisons_on_metal, texture_views_reinterpret_cube_and_array_dimensions_and_refuse_invalid_shapes.
  • cargo clippy --workspace --all-targets --locked --target x86_64-pc-windows-gnullvm -- -D warnings: exit 0.
  • cargo xtask policy: 405 files ok, exit 0.

Checked the layouts against the D3D12 structs: UAV MipSlice at word 2 (image_words fix is right — d[4] holds ArraySize/WSize), SRV/UAV dimension decoding for 1D..CubeArray with the correct per-dimension mip/layer words, cube = 6 faces and CubeArray = 6*NumCubes with u32::MAX passthrough, comparison funcs 1..=8 mapped to Never..Always with mode==0 gate, D3D12_SAMPLER_DESC 13-dword filter bit decode (aniso/reduction bits, ComparisonFunc at word 6), Address 5 = MirrorOnce -> MirrorClampToEdge. The Metal side validates buffer-backed, 3D, multisampled, and non-square/short cube backing before newTextureView, so bad descriptors now return Err instead of tripping ObjC assertions, and the dim: None case reinterprets a cube as a 2D array when the view asks for faces individually. Approving — this unblocks #573 and the shadow/cubemap lighting paths behind #258/#521.

<!-- greyfinch-sync:LakshanSivakumar/Native-Victory-macOS:review:6099486656 --> Imported from [LakshanSivakumar/Native-Victory-macOS](https://github.com/LakshanSivakumar/Native-Victory-macOS/pull/577#pullrequestreview-6099486656). Originally posted by [AnastasisKon](https://github.com/AnastasisKon) on 2026-11-28T18:08:57Z. **Review:** APPROVED --- Reviewed and verified locally on head c734a4fd (worktree /private/tmp/review-577): - cargo test -p victory-backend -p victory-gpu -p victory-metal --locked -- --test-threads=1: all green (454 passed in victory-gpu lib, 48+13+21 in victory-metal, 10 in victory-backend), including the end-to-end Metal GPU tests: cube_srv_and_d3d12_sampler_sample_all_six_faces_on_metal, shadow_comparison_samplers_evaluate_depth_comparisons_on_metal, texture_views_reinterpret_cube_and_array_dimensions_and_refuse_invalid_shapes. - cargo clippy --workspace --all-targets --locked --target x86_64-pc-windows-gnullvm -- -D warnings: exit 0. - cargo xtask policy: 405 files ok, exit 0. Checked the layouts against the D3D12 structs: UAV MipSlice at word 2 (image_words fix is right — d[4] holds ArraySize/WSize), SRV/UAV dimension decoding for 1D..CubeArray with the correct per-dimension mip/layer words, cube = 6 faces and CubeArray = 6*NumCubes with u32::MAX passthrough, comparison funcs 1..=8 mapped to Never..Always with mode==0 gate, D3D12_SAMPLER_DESC 13-dword filter bit decode (aniso/reduction bits, ComparisonFunc at word 6), Address 5 = MirrorOnce -> MirrorClampToEdge. The Metal side validates buffer-backed, 3D, multisampled, and non-square/short cube backing before newTextureView, so bad descriptors now return Err instead of tripping ObjC assertions, and the dim: None case reinterprets a cube as a 2D array when the view asks for faces individually. Approving — this unblocks #573 and the shadow/cubemap lighting paths behind #258/#521.
This pull request cannot be merged automatically due to conflicts.
Merge manually to resolve the conflicts.
View command line instructions

Checkout

From your project repository, check out a new branch and test the changes.
git fetch -u origin 'upstream-pr/577:upstream-pr/577'
git switch 'upstream-pr/577'

Merge

Merge the changes and update on Forgejo.

Warning: The "Autodetect manual merge" setting is not enabled for this repository, you will have to mark this pull request as manually merged afterwards.

git switch 'main'
git merge --no-ff 'upstream-pr/577'
git switch 'upstream-pr/577'
git rebase 'main'
git switch 'main'
git merge --ff-only 'upstream-pr/577'
git switch 'upstream-pr/577'
git rebase 'main'
git switch 'main'
git merge --no-ff 'upstream-pr/577'
git switch 'main'
git merge --squash 'upstream-pr/577'
git switch 'main'
git merge --ff-only 'upstream-pr/577'
git switch 'main'
git merge 'upstream-pr/577'
git push origin 'main'
Sign in to join this conversation.
No reviewers
No labels
No milestone
No assignees
1 participant
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Reference
LakshanSivakumar/Native-Victory-macOS!577
No description provided.