[PR #2089] gfx10_descriptors: clamp sampler maxLod >= minLod and guard DERIV_ADJUST_EN integer border (VK-SAMPLER-LOD-ORDER-AND-DERIV-ADJUST) #2095

Open
opened 2026-11-26 21:20:01 +01:00 by 4ld0 · 0 comments
Owner

📋 Pull Request Information

Original PR: https://github.com/ubiquitarianism/leonida/pull/2089
Author: @KushGene
Created: 11/25/2026
Status: 🔄 Open

Base: main ← Head: fix/vk-sampler-lod-order-and-deriv-adjust


📝 Commits (1)

  • c182b7a gfx10_descriptors: clamp sampler maxLod >= minLod and guard DERIV_ADJUST_EN integer border (VK-SAMPLER-LOD-ORDER-AND-DERIV-ADJUST)

📊 Changes

2 files changed (+45 additions, -4 deletions)

View changed files

📝 runtime/src/gfx10_descriptors.h (+13 -4)
📝 runtime/test/gfx10_descriptors_test.cpp (+32 -0)

📄 Description

Claimed in the coordination issue as: VK-SAMPLER-LOD-ORDER-AND-DERIV-ADJUST (https://github.com/ubiquitarianism/leonida/issues/1608#issuecomment-7172871387)

What and why

runtime/src/gfx10_descriptors.h decodes console GFX10 S# descriptors (decode(SamplerWords)) and translates them to VkSamplerCreateInfo (vulkan_sampler()) for DeviceState::bind_sampler() and synthetic GPU tests:

  1. Enforce maxLod >= minLod and finite LOD values (VUID-VkSamplerCreateInfo-maxLod-01973): vulkan_sampler() previously copied sampler.min_lod and sampler.max_lod verbatim when mip_filter != MipFilter::none && !sampler.unnormalized. Any title S# carrying MIN_LOD > MAX_LOD or a D3D12_SAMPLER_DESC / caller input with min_lod > max_lod (or NaN) produced an invalid VkSamplerCreateInfo. Raise info.maxLod to info.minLod when info.maxLod < info.minLod and normalize NaN LOD inputs (minLod -> 0.0f, maxLod -> info.minLod).
  2. Guard mipLodBias against NaN and negative/NaN max_lod_bias (VUID-VkSamplerCreateInfo-mipLodBias-01069): std::clamp(sampler.lod_bias, -max_lod_bias, max_lod_bias) triggers undefined behavior when max_lod_bias < 0 or NaN, and propagates NaN lod_bias. Normalize negative/NaN limits to 0.0f and NaN lod_bias to 0.0f using detail::clamped().
  3. Respect DERIV_ADJUST_EN when checking kIntegerBorderPtr in decode(SamplerWords): Line 582 already checks !deriv_adjust before interpreting WORD3 bits as kCompareNever, because DERIV_ADJUST_EN (WORD2 bit 31) repurposes WORD3. Apply the same !deriv_adjust guard to kIntegerBorderPtr (0x800) so descriptors with DERIV_ADJUST_EN = 1 are not misdecoded as integer_border = true (VK_BORDER_COLOR_INT_*).

Follows the pattern of e9f521ceb (VK-SAMPLER-UNNORM-BIAS).

Tested

  • Fail on main / pass with fix (runtime/test/gfx10_descriptors_test.cpp): Built gfx10_descriptors_test against the pre-fix header with the new tests: failed on main at line 386: info.minLod == 3.5f && info.maxLod == 3.5f (exit 1); passes with the fix (GFX10 descriptors: T#, S# and V# fields, round trips, 95 format rows and the Vulkan mappings OK).
  • Strict compiler check: Compiled and ran runtime/test/gfx10_descriptors_test.cpp with /usr/bin/g++ -std=c++20 -O2 -Wall -Wextra -Werror -Wno-missing-field-initializers (0 warnings, exit 0).
  • Affected CMake targets & ctest: Rebuilt gfx10_descriptors_test, vk_sampler_test, vk_harness, vk_sandbox, vk_store_test, vk_texture_test, indirect_clamp_safety_test, indirect_topology_and_stats_test, and ran ctest --test-dir runtime/build/linux-ctest -E "^(shader_library_corpus|vk_sync_validation)$" --output-on-failure -j8 -> 97/97 passed (2 skipped in full suite).
  • Real GPU (ps_ssharp + inverted-LOD vk_sampler_test):
    • LEONIDA_VK_DEVICE=6700 python3 -I scripts/run_gpu_tests.py --build runtime/build/linux-ctest --filter ps_ssharp --require-pass -> 2 shaders translated and valid (spirv-val), PASS ps_ssharp 8 S#/T# cases, 4096 pixels each exact (1/1 passed).
    • LEONIDA_VK_DEVICE=6700 ./runtime/build/linux-ctest/vk_sampler_test .../vs_ids.spv .../ps_ssharp.spv .../inv.raw 0 00000100 04000000 0 0 1 (MIN_LOD = 1.0, MAX_LOD = 0.0, point mip filter) -> mipmapMode 0, minLod 1, maxLod 1, mipLodBias 0; view levels 0..1 and ok -> .../inv.raw (65536 bytes, vkCreateSampler and render succeeded on RADV).
  • Hygiene & no-game-data: python3 -I scripts/check_no_game_data.py runtime/src/gfx10_descriptors.h runtime/test/gfx10_descriptors_test.cpp and git diff --check passed.

GPUs tested: AMD Radeon RX 6700 XT (RADV NAVI22, Mesa 26.2.7, Vulkan 1.4.354, Fedora Linux 45).

AI-assisted: yes (Antigravity)

Checklist

  • Based on current main (db1a22d29) and merges cleanly.
  • One topic; follow-up work goes in a new pull request.
  • No game data, keys or decryption (CONTRIBUTING.md), including in logs or reports quoted here.

🔄 This issue represents a GitHub Pull Request. It cannot be merged through Gitea due to API limitations.

## 📋 Pull Request Information **Original PR:** https://github.com/ubiquitarianism/leonida/pull/2089 **Author:** [@KushGene](https://github.com/KushGene) **Created:** 11/25/2026 **Status:** 🔄 Open **Base:** `main` ← **Head:** `fix/vk-sampler-lod-order-and-deriv-adjust` --- ### 📝 Commits (1) - [`c182b7a`](https://github.com/ubiquitarianism/leonida/commit/c182b7ad31f628aca90b86458426cf0a1e325132) gfx10_descriptors: clamp sampler maxLod >= minLod and guard DERIV_ADJUST_EN integer border (VK-SAMPLER-LOD-ORDER-AND-DERIV-ADJUST) ### 📊 Changes **2 files changed** (+45 additions, -4 deletions) <details> <summary>View changed files</summary> 📝 `runtime/src/gfx10_descriptors.h` (+13 -4) 📝 `runtime/test/gfx10_descriptors_test.cpp` (+32 -0) </details> ### 📄 Description Claimed in the coordination issue as: VK-SAMPLER-LOD-ORDER-AND-DERIV-ADJUST (https://github.com/ubiquitarianism/leonida/issues/1608#issuecomment-7172871387) ### What and why `runtime/src/gfx10_descriptors.h` decodes console GFX10 S# descriptors (`decode(SamplerWords)`) and translates them to `VkSamplerCreateInfo` (`vulkan_sampler()`) for `DeviceState::bind_sampler()` and synthetic GPU tests: 1. **Enforce `maxLod >= minLod` and finite LOD values (`VUID-VkSamplerCreateInfo-maxLod-01973`):** `vulkan_sampler()` previously copied `sampler.min_lod` and `sampler.max_lod` verbatim when `mip_filter != MipFilter::none && !sampler.unnormalized`. Any title S# carrying `MIN_LOD > MAX_LOD` or a `D3D12_SAMPLER_DESC` / caller input with `min_lod > max_lod` (or NaN) produced an invalid `VkSamplerCreateInfo`. Raise `info.maxLod` to `info.minLod` when `info.maxLod < info.minLod` and normalize NaN LOD inputs (`minLod` -> `0.0f`, `maxLod` -> `info.minLod`). 2. **Guard `mipLodBias` against NaN and negative/NaN `max_lod_bias` (`VUID-VkSamplerCreateInfo-mipLodBias-01069`):** `std::clamp(sampler.lod_bias, -max_lod_bias, max_lod_bias)` triggers undefined behavior when `max_lod_bias < 0` or NaN, and propagates NaN `lod_bias`. Normalize negative/NaN limits to `0.0f` and NaN `lod_bias` to `0.0f` using `detail::clamped()`. 3. **Respect `DERIV_ADJUST_EN` when checking `kIntegerBorderPtr` in `decode(SamplerWords)`:** Line 582 already checks `!deriv_adjust` before interpreting `WORD3` bits as `kCompareNever`, because `DERIV_ADJUST_EN` (WORD2 bit 31) repurposes `WORD3`. Apply the same `!deriv_adjust` guard to `kIntegerBorderPtr` (`0x800`) so descriptors with `DERIV_ADJUST_EN = 1` are not misdecoded as `integer_border = true` (`VK_BORDER_COLOR_INT_*`). Follows the pattern of `e9f521ceb` (`VK-SAMPLER-UNNORM-BIAS`). ### Tested - **Fail on `main` / pass with fix (`runtime/test/gfx10_descriptors_test.cpp`):** Built `gfx10_descriptors_test` against the pre-fix header with the new tests: failed on `main` at `line 386: info.minLod == 3.5f && info.maxLod == 3.5f` (exit 1); passes with the fix (`GFX10 descriptors: T#, S# and V# fields, round trips, 95 format rows and the Vulkan mappings OK`). - **Strict compiler check:** Compiled and ran `runtime/test/gfx10_descriptors_test.cpp` with `/usr/bin/g++ -std=c++20 -O2 -Wall -Wextra -Werror -Wno-missing-field-initializers` (0 warnings, exit 0). - **Affected CMake targets & `ctest`:** Rebuilt `gfx10_descriptors_test`, `vk_sampler_test`, `vk_harness`, `vk_sandbox`, `vk_store_test`, `vk_texture_test`, `indirect_clamp_safety_test`, `indirect_topology_and_stats_test`, and ran `ctest --test-dir runtime/build/linux-ctest -E "^(shader_library_corpus|vk_sync_validation)$" --output-on-failure -j8` -> 97/97 passed (2 skipped in full suite). - **Real GPU (`ps_ssharp` + inverted-LOD `vk_sampler_test`):** - `LEONIDA_VK_DEVICE=6700 python3 -I scripts/run_gpu_tests.py --build runtime/build/linux-ctest --filter ps_ssharp --require-pass` -> 2 shaders translated and valid (`spirv-val`), `PASS ps_ssharp 8 S#/T# cases, 4096 pixels each exact` (1/1 passed). - `LEONIDA_VK_DEVICE=6700 ./runtime/build/linux-ctest/vk_sampler_test .../vs_ids.spv .../ps_ssharp.spv .../inv.raw 0 00000100 04000000 0 0 1` (`MIN_LOD = 1.0`, `MAX_LOD = 0.0`, point mip filter) -> `mipmapMode 0, minLod 1, maxLod 1, mipLodBias 0; view levels 0..1` and `ok -> .../inv.raw` (`65536` bytes, `vkCreateSampler` and render succeeded on RADV). - **Hygiene & no-game-data:** `python3 -I scripts/check_no_game_data.py runtime/src/gfx10_descriptors.h runtime/test/gfx10_descriptors_test.cpp` and `git diff --check` passed. GPUs tested: AMD Radeon RX 6700 XT (RADV NAVI22, Mesa 26.2.7, Vulkan 1.4.354, Fedora Linux 45). AI-assisted: yes (Antigravity) ### Checklist - [x] Based on current `main` (`db1a22d29`) and merges cleanly. - [x] One topic; follow-up work goes in a new pull request. - [x] No game data, keys or decryption (`CONTRIBUTING.md`), including in logs or reports quoted here. --- <sub>🔄 This issue represents a GitHub Pull Request. It cannot be merged through Gitea due to API limitations.</sub>
Sign in to join this conversation.
No labels
bug
pull-request
No milestone
No project
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.

Dependencies

No dependencies set

Reference
starred/leonida#2095
No description provided.