rgba4_fragment: harden SPIR-V output quantization and add portable unit test #1696

Closed
IlyassRS3 wants to merge 1 commit from upstream-pr/1696 into main
IlyassRS3 commented 2026-11-25 08:36:24 +00:00 (Migrated from github.com)

Imported from ubiquitarianism/leonida. Originally posted by IlyassRS3 on 2026-11-25T08:36:24Z.


Item ID: RGBA4-FRAGMENT-VALIDATION-AND-TESTS
Claim: https://github.com/ubiquitarianism/leonida/issues/1608#issuecomment-7157180890

Summary

Hardens leonida::rgba4::quantize in runtime/src/rgba4_fragment.h and adds a portable CPU-only unit test (runtime/test/rgba4_fragment_test.cpp, registered in runtime/CMakeLists.txt):

  • Allow shaders with no masked outputs and no f32 type: resolve outputs against mask before checking !float_type, so depth-only / integer-output shaders with no outputs in mask return true instead of failing PSO creation with E_INVALIDARG.
  • Reject masked outputs with missing pointer types: if an Output variable matches mask but its OpTypePointer Output is absent, fail conservatively instead of silently skipping quantization.
  • Support OpGroupDecorate locations: propagate Location decorations applied via decoration groups (OpGroupDecorate, opcode 74) onto Output variables.
  • Require valid OpMemoryModel placement before emitting GLSL.std.450: ensure OpExtInstImport is inserted exactly once before OpMemoryModel (and before OpFunction) when needed; otherwise fail without mutating words instead of returning invalid SPIR-V with an undeclared import_id. Also tighten GLSL.std.450 import matching to exact 6-word length and non-zero result ID.
  • Track OpPtrAccessChain/OpInBoundsPtrAccessChain and reject untracked pointer escapes: reject OpCopyMemorySized, unknown result pointer types on access chains / OpCopyObject, and OpBitcast / OpSelect / OpPhi / OpFunctionCall involving a masked output pointer.
  • Guard SPIR-V ID bound (words[3]): reject zero bound and prevent 32-bit ID wraparound when allocating new IDs.

Verification

  • clang++ -std=c++20 -O2 -Wall -Wextra -Werror -pedantic -fsanitize=address,undefined runtime/test/rgba4_fragment_test.cpp -> exit 0, all checks pass, 0 warnings / 0 sanitizer errors.

  • Mutation check against unpatched main (git show main:runtime/src/rgba4_fragment.h with new test) -> exit 1 (15 failures reproducing every fix).

  • python3 -I scripts/check_no_game_data.py runtime/src/rgba4_fragment.h runtime/test/rgba4_fragment_test.cpp runtime/CMakeLists.txt -> OK.

  • python3 -m compileall -q make_game.py tools tests and python3 -I tests/test_text_write_line_endings.py -> OK (3/3).

  • AI-assisted: yes

  • GPUs tested: none (CPU-only portable SPIR-V rewriter + synthetic unit tests; no game files)

<!-- greyfinch-sync:ubiquitarianism/leonida:issue:5487917750 --> Imported from [ubiquitarianism/leonida](https://github.com/ProjectLeonida/mandy/pull/1696). Originally posted by [IlyassRS3](https://github.com/IlyassRS3) on 2026-11-25T08:36:24Z. --- Item ID: `RGBA4-FRAGMENT-VALIDATION-AND-TESTS` Claim: https://github.com/ubiquitarianism/leonida/issues/1608#issuecomment-7157180890 ## Summary Hardens `leonida::rgba4::quantize` in `runtime/src/rgba4_fragment.h` and adds a portable CPU-only unit test (`runtime/test/rgba4_fragment_test.cpp`, registered in `runtime/CMakeLists.txt`): - **Allow shaders with no masked outputs and no `f32` type**: resolve `outputs` against `mask` before checking `!float_type`, so depth-only / integer-output shaders with no outputs in `mask` return `true` instead of failing PSO creation with `E_INVALIDARG`. - **Reject masked outputs with missing pointer types**: if an `Output` variable matches `mask` but its `OpTypePointer Output` is absent, fail conservatively instead of silently skipping quantization. - **Support `OpGroupDecorate` locations**: propagate `Location` decorations applied via decoration groups (`OpGroupDecorate`, opcode 74) onto `Output` variables. - **Require valid `OpMemoryModel` placement before emitting `GLSL.std.450`**: ensure `OpExtInstImport` is inserted exactly once before `OpMemoryModel` (and before `OpFunction`) when needed; otherwise fail without mutating `words` instead of returning invalid SPIR-V with an undeclared `import_id`. Also tighten `GLSL.std.450` import matching to exact 6-word length and non-zero result ID. - **Track `OpPtrAccessChain`/`OpInBoundsPtrAccessChain` and reject untracked pointer escapes**: reject `OpCopyMemorySized`, unknown result pointer types on access chains / `OpCopyObject`, and `OpBitcast` / `OpSelect` / `OpPhi` / `OpFunctionCall` involving a masked output pointer. - **Guard SPIR-V ID bound (`words[3]`)**: reject zero bound and prevent 32-bit ID wraparound when allocating new IDs. ## Verification - `clang++ -std=c++20 -O2 -Wall -Wextra -Werror -pedantic -fsanitize=address,undefined runtime/test/rgba4_fragment_test.cpp` -> exit 0, all checks pass, 0 warnings / 0 sanitizer errors. - Mutation check against unpatched `main` (`git show main:runtime/src/rgba4_fragment.h` with new test) -> exit 1 (`15` failures reproducing every fix). - `python3 -I scripts/check_no_game_data.py runtime/src/rgba4_fragment.h runtime/test/rgba4_fragment_test.cpp runtime/CMakeLists.txt` -> OK. - `python3 -m compileall -q make_game.py tools tests` and `python3 -I tests/test_text_write_line_endings.py` -> OK (3/3). - AI-assisted: yes - GPUs tested: none (CPU-only portable SPIR-V rewriter + synthetic unit tests; no game files)
copilot-pull-request-reviewer[bot] commented 2026-11-25 08:36:39 +00:00 (Migrated from github.com)

Imported from ubiquitarianism/leonida. Originally posted by copilot-pull-request-reviewer[bot] on 2026-11-25T08:36:39Z.

Review: COMMENTED


Copilot was unable to review this pull request because the user who requested the review has insufficient remaining quota. Try requesting a review at a lower effort level.

<!-- greyfinch-sync:ubiquitarianism/leonida:review:6054239038 --> Imported from [ubiquitarianism/leonida](https://github.com/ProjectLeonida/mandy/pull/1696#pullrequestreview-6054239038). Originally posted by [copilot-pull-request-reviewer[bot]](https://github.com/apps/copilot-pull-request-reviewer) on 2026-11-25T08:36:39Z. **Review:** COMMENTED --- Copilot was unable to review this pull request because the user who requested the review has insufficient remaining quota. Try requesting a review at a lower effort level.
MoonEyes commented 2026-11-28 02:50:52 +00:00 (Migrated from github.com)

Imported from ubiquitarianism/leonida. Originally posted by MoonEyes on 2026-11-28T02:50:52Z.


Hi @IlyassRS3, thanks for this pull request. It currently conflicts with main (conflicting file(s): runtime/CMakeLists.txt), so it carries the needs rebase label. CONTRIBUTING.md asks authors to rebase their branch on main; once it merges cleanly the label goes within the hour, and reviews start with the pull requests that do. Could you update the branch when you have a moment? If you no longer plan to continue with it, please say so here.

AI-assisted (Claude Code, Claude Opus 5.5).

<!-- greyfinch-sync:ubiquitarianism/leonida:comment:7232632020 --> Imported from [ubiquitarianism/leonida](https://github.com/ProjectLeonida/mandy/pull/1696#issuecomment-7232632020). Originally posted by [MoonEyes](https://github.com/MoonEyes) on 2026-11-28T02:50:52Z. --- Hi @IlyassRS3, thanks for this pull request. It currently conflicts with `main` (conflicting file(s): `runtime/CMakeLists.txt`), so it carries the `needs rebase` label. CONTRIBUTING.md asks authors to rebase their branch on `main`; once it merges cleanly the label goes within the hour, and reviews start with the pull requests that do. Could you update the branch when you have a moment? If you no longer plan to continue with it, please say so here. AI-assisted (Claude Code, Claude Opus 5.5).
aslan2142 commented 2026-12-01 16:37:00 +00:00 (Migrated from github.com)

Imported from ubiquitarianism/leonida. Originally posted by aslan2142 on 2026-12-01T16:37:00Z.


Closing this PR because the legacy C++ Vulkan runtime (and legacy Python shader compiler) is now considered deprecated in favor of the new Rust Vulkan runtime (in progress) and the Rust DX12 runtime (d3d12/) available now. Thank you for the contribution! If you believe this PR was closed by mistake (for example, if it still applies to active tools, d3d12/, or general docs), please feel free to reopen it.

<!-- greyfinch-sync:ubiquitarianism/leonida:comment:7317901095 --> Imported from [ubiquitarianism/leonida](https://github.com/ProjectLeonida/mandy/pull/1696#issuecomment-7317901095). Originally posted by [aslan2142](https://github.com/aslan2142) on 2026-12-01T16:37:00Z. --- Closing this PR because the legacy C++ Vulkan runtime (and legacy Python shader compiler) is now considered deprecated in favor of the new Rust Vulkan runtime (in progress) and the Rust DX12 runtime (`d3d12/`) available now. Thank you for the contribution! If you believe this PR was closed by mistake (for example, if it still applies to active tools, `d3d12/`, or general docs), please feel free to reopen it.

Pull request closed

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
ProjectLeonida/mandy!1696
No description provided.