[PR #1696] rgba4_fragment: harden SPIR-V output quantization and add portable unit test #1710

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

📋 Pull Request Information

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

Base: main ← Head: fix/rgba4-fragment-validation-and-tests


📝 Commits (1)

  • 0dda119 rgba4_fragment: harden SPIR-V rewrite validation and add portable unit test

📊 Changes

3 files changed (+439 additions, -24 deletions)

View changed files

📝 runtime/CMakeLists.txt (+3 -0)
📝 runtime/src/rgba4_fragment.h (+74 -24)
➕ runtime/test/rgba4_fragment_test.cpp (+362 -0)

📄 Description

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)


🔄 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/1696 **Author:** [@IlyassRS3](https://github.com/IlyassRS3) **Created:** 11/25/2026 **Status:** 🔄 Open **Base:** `main` ← **Head:** `fix/rgba4-fragment-validation-and-tests` --- ### 📝 Commits (1) - [`0dda119`](https://github.com/ubiquitarianism/leonida/commit/0dda119e7575d8fda8923cb061155d52a9218adc) rgba4_fragment: harden SPIR-V rewrite validation and add portable unit test ### 📊 Changes **3 files changed** (+439 additions, -24 deletions) <details> <summary>View changed files</summary> 📝 `runtime/CMakeLists.txt` (+3 -0) 📝 `runtime/src/rgba4_fragment.h` (+74 -24) ➕ `runtime/test/rgba4_fragment_test.cpp` (+362 -0) </details> ### 📄 Description 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) --- <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#1710
No description provided.