diff --git a/runtime/CMakeLists.txt b/runtime/CMakeLists.txt index c1d0e7cb53..295166a0bd 100644 --- a/runtime/CMakeLists.txt +++ b/runtime/CMakeLists.txt @@ -755,6 +755,9 @@ if(BUILD_TESTING) add_executable(xbox_present_readback_size_test test/xbox_present_readback_size_test.cpp) add_test(NAME xbox_present_readback_size COMMAND xbox_present_readback_size_test) + add_executable(rgba4_fragment_test test/rgba4_fragment_test.cpp) + add_test(NAME rgba4_fragment COMMAND rgba4_fragment_test) + # The title's own window, where PresentX shows its frames (shims/title_window.h): user32 only, no device. if(WIN32) add_executable(title_window_test test/title_window_test.cpp) diff --git a/runtime/src/rgba4_fragment.h b/runtime/src/rgba4_fragment.h index b621628482..dc695f2f2f 100644 --- a/runtime/src/rgba4_fragment.h +++ b/runtime/src/rgba4_fragment.h @@ -1,81 +1,235 @@ #pragma once + +#include #include #include +#include #include #include -#include -#include +#include #include + namespace leonida::rgba4 { -using Inst=std::vector; -inline Inst instruction(std::uint32_t op,std::initializer_list args) { - Inst w{(std::uint32_t(args.size()+1)<<16)|op};w.insert(w.end(),args);return w; + +using Inst = std::vector; + +inline Inst instruction(std::uint32_t op, std::initializer_list args) { + Inst w; + w.reserve(args.size() + 1); + w.push_back((std::uint32_t(args.size() + 1) << 16) | op); + w.insert(w.end(), args); + return w; } + // Quantize only written color outputs at the requested locations. For a wider fallback attachment // this reproduces four-bit UNORM writes for pipelines whose affected attachments use no blending. -inline bool quantize(Inst& words,unsigned mask) { - if(!mask)return true; - if(words.size()<5 || words[0]!=0x07230203)return false; - std::vector code,declarations; - std::map locations, pointer_type, width, scalar, variables; - unsigned import_id=0,next=words[3],float_type=0; - for(std::size_t at=5;at>16,op=words[at]&65535u; - if(!n || n>words.size()-at)return false; - Inst i(words.begin()+at,words.begin()+at+n);at+=n; - if(op==11 && n>=3) { - const char* s=reinterpret_cast(i.data()+2);const auto bytes=(n-2)*4; - if(bytes>=13 && std::memcmp(s,"GLSL.std.450",13)==0)import_id=i[1]; - } else if(op==71 && n==4 && i[2]==30)locations[i[1]]=i[3]; - else if(op==22 && n==3 && i[2]==32){float_type=i[1];width[i[1]]=1;scalar[i[1]]=i[1];} - else if(op==23 && n==4){width[i[1]]=i[3];scalar[i[1]]=i[2];} - else if(op==32 && n==4 && i[2]==3)pointer_type[i[1]]=i[3]; - else if(op==59 && n>=4 && i[3]==3)variables[i[2]]=i[1]; - code.push_back(std::move(i)); +inline bool quantize(Inst& words, unsigned mask) { + if (!mask) return true; + if (words.size() < 5 || words[0] != 0x07230203u) return false; + + constexpr std::uint32_t kOpExtInstImport = 11, kOpExtInst = 12, kOpMemoryModel = 14; + constexpr std::uint32_t kOpTypeFloat = 22, kOpTypeVector = 23, kOpTypePointer = 32; + constexpr std::uint32_t kOpConstant = 43, kOpConstantComposite = 44; + constexpr std::uint32_t kOpFunction = 54, kOpVariable = 59, kOpStore = 62, kOpCopyMemory = 63; + constexpr std::uint32_t kOpAccessChain = 65, kOpInBoundsAccessChain = 66, kOpPtrAccessChain = 83; + constexpr std::uint32_t kOpDecorate = 71, kOpFMul = 133; + constexpr std::uint32_t kDecoLocation = 30, kStorageOutput = 3; + constexpr std::uint32_t kGlslRoundEven = 2, kGlslFClamp = 43; + + struct Kv { std::uint32_t key, value; }; + struct TypeInfo { std::uint32_t id, width, scalar; }; + + std::vector locations, pointer_type, variables; + std::vector types; + std::uint32_t import_id = 0, float_type = 0; + std::size_t mem_model_at = 0; + std::size_t first_func_at = words.size(); + + // Pass 1: flat preamble scan up to the first OpFunction (no per-instruction vector allocations). + for (std::size_t at = 5; at < words.size();) { + const std::uint32_t n = words[at] >> 16, op = words[at] & 0xffffu; + if (!n || n > words.size() - at) return false; + if (op == kOpFunction) { + first_func_at = at; + break; + } + const std::uint32_t* w = &words[at]; + if (op == kOpMemoryModel && !mem_model_at) { + mem_model_at = at; + } else if (op == kOpExtInstImport && n >= 3) { + const auto bytes = std::size_t(n - 2) * 4; + if (bytes >= 13 && std::memcmp(w + 2, "GLSL.std.450", 13) == 0) import_id = w[1]; + } else if (op == kOpDecorate && n == 4 && w[2] == kDecoLocation) { + locations.push_back({w[1], w[3]}); + } else if (op == kOpTypeFloat && n == 3 && w[2] == 32u) { + float_type = w[1]; + types.push_back({w[1], 1u, w[1]}); + } else if (op == kOpTypeVector && n == 4) { + types.push_back({w[1], w[3], w[2]}); + } else if (op == kOpTypePointer && n == 4 && w[2] == kStorageOutput) { + pointer_type.push_back({w[1], w[3]}); + } else if (op == kOpVariable && n >= 4 && w[3] == kStorageOutput) { + variables.push_back({w[2], w[1]}); + } + at += n; + } + + if (!float_type) return false; + + const auto sort_kv = [](std::vector& v) { + std::stable_sort(v.begin(), v.end(), [](const Kv& a, const Kv& b) { return a.key < b.key; }); + }; + const auto find_kv = [](const std::vector& v, std::uint32_t key) -> const Kv* { + const auto it = std::upper_bound(v.begin(), v.end(), key, + [](std::uint32_t k, const Kv& e) { return k < e.key; }); + return (it != v.begin() && (it - 1)->key == key) ? &*(it - 1) : nullptr; + }; + sort_kv(locations); + sort_kv(pointer_type); + sort_kv(variables); + std::stable_sort(types.begin(), types.end(), [](const TypeInfo& a, const TypeInfo& b) { return a.id < b.id; }); + const auto find_type = [&](std::uint32_t id) -> const TypeInfo* { + const auto it = std::upper_bound(types.begin(), types.end(), id, + [](std::uint32_t k, const TypeInfo& e) { return k < e.id; }); + return (it != types.begin() && (it - 1)->id == id) ? &*(it - 1) : nullptr; + }; + + // Outputs is small (<= 8 color targets plus any derived access-chain pointers): a flat list fits in one cache line. + std::vector outputs; + const auto find_output = [&](std::uint32_t id) -> const Kv* { + for (std::size_t i = outputs.size(); i-- > 0;) + if (outputs[i].key == id) return &outputs[i]; + return nullptr; + }; + for (std::size_t idx = 0; idx < variables.size(); ++idx) { + if (idx + 1 < variables.size() && variables[idx + 1].key == variables[idx].key) continue; + const std::uint32_t id = variables[idx].key; + const std::uint32_t ptr = variables[idx].value; + const Kv* loc = find_kv(locations, id); + if (!loc || loc->value >= 8u || !(mask & (1u << loc->value))) continue; + const Kv* pointee = find_kv(pointer_type, ptr); + if (pointee) outputs.push_back({id, pointee->value}); } - if(!float_type)return false; - std::map outputs; - for(auto [id,ptr]:variables)if(locations.count(id)&&locations[id]<8&&(mask&(1u<> constants; - auto components=[&](unsigned type) -> const std::array& { - auto [it,inserted]=constants.try_emplace(type); - if(!inserted)return it->second; - constexpr std::array vals{0.f,1.f,15.f,1.f/15.f}; - std::array base{}; - for(unsigned j=0;j<4;++j){base[j]=next++;declarations.push_back(instruction(43,{scalar[type],base[j],std::bit_cast(vals[j])}));} - if(width[type]==1){it->second=base;return it->second;} - for(unsigned j=0;j<4;++j){const auto id=next++;Inst c=instruction(44,{type,id});c.resize(3+width[type],base[j]);c[0]=(unsigned(c.size())<<16)|44u;declarations.push_back(std::move(c));it->second[j]=id;} - return it->second; + if (outputs.empty()) return true; + if (first_func_at == words.size()) return false; + + const bool add_import = !import_id; + if (add_import && !mem_model_at) return false; + + // Pass 2: validate body framing, track derived Output pointers, and record OpStore sites without mutating `words`. + struct StorePatch { + std::size_t at; + std::uint32_t type; }; - std::vector rewritten;bool changed=false; - for(auto& i:code) { - const auto op=i[0]&65535u; - if((op==65||op==66||op==83) && i.size()>=4 && outputs.count(i[3]) && pointer_type.count(i[1])) - outputs[i[2]]=pointer_type[i[1]]; - if(op==63 && i.size()>=3 && outputs.count(i[1]))return false; - if(op==62 && i.size()>=3 && outputs.count(i[1])) { - const auto type=outputs[i[1]]; - if(!width.count(type)||width[type]<1||width[type]>4||scalar[type]!=float_type)return false; - const auto& c=components(type); - const auto clamped=next++,scaled=next++,rounded=next++,value=next++; - rewritten.push_back(instruction(12,{type,clamped,import_id,43,i[2],c[0],c[1]})); // GLSL FClamp - rewritten.push_back(instruction(133,{type,scaled,clamped,c[2]})); - rewritten.push_back(instruction(12,{type,rounded,import_id,2,scaled})); // GLSL RoundEven - rewritten.push_back(instruction(133,{type,value,rounded,c[3]}));i[2]=value;changed=true; + std::vector patches; + for (std::size_t at = first_func_at; at < words.size();) { + const std::uint32_t n = words[at] >> 16, op = words[at] & 0xffffu; + if (!n || n > words.size() - at) return false; + const std::uint32_t* w = &words[at]; + if ((op == kOpAccessChain || op == kOpInBoundsAccessChain || op == kOpPtrAccessChain) && n >= 4) { + if (find_output(w[3])) { + if (const Kv* pointee = find_kv(pointer_type, w[1])) outputs.push_back({w[2], pointee->value}); + } + } else if (op == kOpCopyMemory && n >= 3) { + if (find_output(w[1])) return false; + } else if (op == kOpStore && n >= 3) { + if (const Kv* out_var = find_output(w[1])) { + const TypeInfo* ti = find_type(out_var->value); + if (!ti || ti->width < 1u || ti->width > 4u || ti->scalar != float_type) return false; + patches.push_back({at, out_var->value}); + } } - rewritten.push_back(std::move(i)); + at += n; } - if(!changed)return false; - Inst result(words.begin(),words.begin()+5);result[3]=next;bool declared=false; - for(const auto& i:rewritten) { - const auto op=i[0]&65535u; - if(op==14 && add_import){Inst imp=instruction(11,{import_id,0,0,0,0});std::memcpy(imp.data()+2,"GLSL.std.450",13);result.insert(result.end(),imp.begin(),imp.end());} - if(op==54&&!declared){for(const auto& dec:declarations)result.insert(result.end(),dec.begin(),dec.end());declared=true;} - result.insert(result.end(),i.begin(),i.end()); + if (patches.empty()) return false; + + // Build shared scalar constants (emitted once) and per-vector-type composite constants. + std::uint32_t next = words[3]; + if (add_import) import_id = next++; + + std::vector declarations; + declarations.reserve(32); + const auto emit_words = [](std::vector& dst, std::uint32_t op, + std::initializer_list args) { + dst.push_back((std::uint32_t(args.size() + 1) << 16) | op); + dst.insert(dst.end(), args.begin(), args.end()); + }; + + constexpr std::array kVals{0.f, 1.f, 15.f, 1.f / 15.f}; + std::array scalar_consts{}; + for (unsigned j = 0; j < 4; ++j) { + scalar_consts[j] = next++; + emit_words(declarations, kOpConstant, {float_type, scalar_consts[j], std::bit_cast(kVals[j])}); } - if(!declared)return false;words=std::move(result);return true; -} + + struct ConstSet { + std::uint32_t type; + std::array ids; + }; + std::vector type_consts; + type_consts.push_back({float_type, scalar_consts}); + const auto constants_for = [&](std::uint32_t type) -> const std::array& { + for (const ConstSet& cs : type_consts) + if (cs.type == type) return cs.ids; + const TypeInfo* ti = find_type(type); + const std::uint32_t width = ti ? ti->width : 1u; + std::array ids{}; + for (unsigned j = 0; j < 4; ++j) { + ids[j] = next++; + declarations.push_back(((3u + width) << 16) | kOpConstantComposite); + declarations.push_back(type); + declarations.push_back(ids[j]); + for (std::uint32_t k = 0; k < width; ++k) declarations.push_back(scalar_consts[j]); + } + type_consts.push_back({type, ids}); + return type_consts.back().ids; + }; + for (const StorePatch& p : patches) (void)constants_for(p.type); + + // Single-pass assembly into a pre-reserved buffer. + Inst result; + result.reserve(words.size() + (add_import ? 6u : 0u) + declarations.size() + patches.size() * 24u); + result.insert(result.end(), words.begin(), words.begin() + 5); + if (add_import) { + result.insert(result.end(), words.begin() + 5, words.begin() + static_cast(mem_model_at)); + const std::size_t imp_at = result.size(); + result.resize(imp_at + 6, 0u); + result[imp_at] = (6u << 16) | kOpExtInstImport; + result[imp_at + 1] = import_id; + std::memcpy(&result[imp_at + 2], "GLSL.std.450", 13); + result.insert(result.end(), words.begin() + static_cast(mem_model_at), + words.begin() + static_cast(first_func_at)); + } else { + result.insert(result.end(), words.begin() + 5, words.begin() + static_cast(first_func_at)); + } + result.insert(result.end(), declarations.begin(), declarations.end()); + + std::size_t cursor = first_func_at; + for (const StorePatch& p : patches) { + result.insert(result.end(), words.begin() + static_cast(cursor), + words.begin() + static_cast(p.at)); + const auto& c = constants_for(p.type); + const std::uint32_t clamped = next++, scaled = next++, rounded = next++, value = next++; + const std::uint32_t orig_val = words[p.at + 2]; + emit_words(result, kOpExtInst, {p.type, clamped, import_id, kGlslFClamp, orig_val, c[0], c[1]}); + emit_words(result, kOpFMul, {p.type, scaled, clamped, c[2]}); + emit_words(result, kOpExtInst, {p.type, rounded, import_id, kGlslRoundEven, scaled}); + emit_words(result, kOpFMul, {p.type, value, rounded, c[3]}); + + const std::uint32_t store_n = words[p.at] >> 16; + result.push_back(words[p.at]); + result.push_back(words[p.at + 1]); + result.push_back(value); + if (store_n > 3) { + result.insert(result.end(), words.begin() + static_cast(p.at + 3), + words.begin() + static_cast(p.at + store_n)); + } + cursor = p.at + store_n; + } + result.insert(result.end(), words.begin() + static_cast(cursor), words.end()); + result[3] = next; + words = std::move(result); + return true; } + +} // namespace leonida::rgba4 diff --git a/runtime/test/rgba4_fragment_test.cpp b/runtime/test/rgba4_fragment_test.cpp new file mode 100644 index 0000000000..cb7c5299bf --- /dev/null +++ b/runtime/test/rgba4_fragment_test.cpp @@ -0,0 +1,228 @@ +// Portable CPU-only unit tests for runtime/src/rgba4_fragment.h (RT-RGBA4-FRAGMENT-FLAT-SCAN-AND-TESTS): +// 4-bit UNORM output quantization rewrite, GLSL.std.450 import injection/reuse, access-chain component stores, +// shared scalar constants across scalar+vector targets, and refusal rules. Hand-built SPIR-V words only; no GPU. +#include "../src/rgba4_fragment.h" + +#include +#include +#include +#include +#include + +#define CHECK(cond) do { \ + if (!(cond)) { \ + std::fprintf(stderr, "line %d: CHECK failed: %s\n", __LINE__, #cond); \ + return 1; \ + } \ +} while (0) + +namespace { + +using Words = leonida::rgba4::Inst; + +constexpr std::uint32_t kOpExtInstImport = 11, kOpExtInst = 12, kOpMemoryModel = 14, kOpCapability = 17; +constexpr std::uint32_t kOpTypeInt = 21, kOpTypeFloat = 22, kOpTypeVector = 23, kOpTypePointer = 32; +constexpr std::uint32_t kOpConstant = 43, kOpConstantComposite = 44; +constexpr std::uint32_t kOpFunction = 54, kOpFunctionEnd = 56, kOpVariable = 59, kOpStore = 62, kOpCopyMemory = 63; +constexpr std::uint32_t kOpAccessChain = 65, kOpDecorate = 71, kOpFMul = 133, kOpLabel = 248, kOpReturn = 253; +constexpr std::uint32_t kDecoLocation = 30, kStorageOutput = 3; + +void emit(Words& w, std::uint32_t op, std::initializer_list args) { + w.push_back((std::uint32_t(args.size() + 1) << 16) | op); + w.insert(w.end(), args.begin(), args.end()); +} + +void emit_glsl_import(Words& w, std::uint32_t id) { + const std::size_t at = w.size(); + w.resize(at + 6, 0u); + w[at] = (6u << 16) | kOpExtInstImport; + w[at + 1] = id; + std::memcpy(&w[at + 2], "GLSL.std.450", 13); +} + +struct ModuleStats { + bool valid_framing = true; + std::uint32_t glsl_imports = 0; + std::uint32_t scalar_consts = 0; + std::uint32_t composite_consts = 0; + std::uint32_t fclamp_ops = 0; + std::uint32_t round_ops = 0; + std::uint32_t fmul_ops = 0; + std::uint32_t stores = 0; + bool import_before_mem_model = true; + bool consts_before_function = true; +}; + +ModuleStats inspect(const Words& w) { + ModuleStats s{}; + if (w.size() < 5 || w[0] != 0x07230203u) { + s.valid_framing = false; + return s; + } + bool saw_mem_model = false, saw_func = false; + for (std::size_t at = 5; at < w.size();) { + const std::uint32_t n = w[at] >> 16, op = w[at] & 0xffffu; + if (!n || n > w.size() - at) { + s.valid_framing = false; + return s; + } + const std::uint32_t* p = &w[at]; + if (op == kOpMemoryModel) saw_mem_model = true; + else if (op == kOpFunction) saw_func = true; + else if (op == kOpExtInstImport && n >= 3 && (n - 2) * 4 >= 13 && std::memcmp(p + 2, "GLSL.std.450", 13) == 0) { + ++s.glsl_imports; + if (saw_mem_model) s.import_before_mem_model = false; + } else if (op == kOpConstant) { + ++s.scalar_consts; + if (saw_func) s.consts_before_function = false; + } else if (op == kOpConstantComposite) { + ++s.composite_consts; + if (saw_func) s.consts_before_function = false; + } else if (op == kOpExtInst && n >= 5) { + if (p[4] == 43u) ++s.fclamp_ops; + else if (p[4] == 2u) ++s.round_ops; + } else if (op == kOpFMul) { + ++s.fmul_ops; + } else if (op == kOpStore) { + ++s.stores; + } + at += n; + } + return s; +} + +Words make_ps(bool with_glsl_import, bool with_loc1_store, bool with_access_chain, bool with_copy_mem, + bool int_output = false, bool with_extra_store_operands = false) { + Words w{0x07230203u, 0x00010600u, 0, 40u, 0}; + emit(w, kOpCapability, {1}); + if (with_glsl_import) emit_glsl_import(w, 1u); + emit(w, kOpMemoryModel, {0, 1}); + emit(w, kOpDecorate, {10, kDecoLocation, 0}); + emit(w, kOpDecorate, {11, kDecoLocation, 1}); + emit(w, kOpTypeFloat, {2, 32}); + emit(w, kOpTypeInt, {3, 32, 0}); + emit(w, kOpTypeVector, {4, int_output ? 3u : 2u, 4}); + emit(w, kOpTypePointer, {5, kStorageOutput, 4}); // ptr Output vec4 + emit(w, kOpTypePointer, {6, kStorageOutput, 2}); // ptr Output float + emit(w, kOpVariable, {5, 10, kStorageOutput}); // Location 0 + emit(w, kOpVariable, {5, 11, kStorageOutput}); // Location 1 + emit(w, kOpFunction, {1, 20, 0, 1}); + emit(w, kOpLabel, {21}); + if (with_copy_mem) { + emit(w, kOpCopyMemory, {10, 11}); + } else if (with_access_chain) { + emit(w, kOpAccessChain, {6, 25, 10, 22}); + emit(w, kOpStore, {25, 26}); // scalar store via derived access-chain pointer + emit(w, kOpStore, {11, 27}); // vec4 store at Location 1 + } else { + if (with_extra_store_operands) { + emit(w, kOpStore, {10, 27, 2, 16}); // Aligned 16 memory operands + } else { + emit(w, kOpStore, {10, 27}); + } + if (with_loc1_store) emit(w, kOpStore, {11, 28}); + } + emit(w, kOpReturn, {}); + emit(w, kOpFunctionEnd, {}); + return w; +} + +} // namespace + +int main() { + // 1. Zero mask returns true without modifying words. + { + Words w = make_ps(false, true, false, false); + const Words orig = w; + CHECK(leonida::rgba4::quantize(w, 0u) && w == orig); + } + + // 2. Mask targeting an unused Location returns true unchanged (fast preamble-only path). + { + Words w = make_ps(false, true, false, false); + const Words orig = w; + CHECK(leonida::rgba4::quantize(w, 1u << 3) && w == orig); + } + + // 3. Rewrite Location 0 only (injects GLSL.std.450 before OpMemoryModel, preserves extra OpStore operands). + { + Words w = make_ps(false, true, false, false, false, true); + CHECK(leonida::rgba4::quantize(w, 1u << 0)); + const ModuleStats s = inspect(w); + CHECK(s.valid_framing && s.glsl_imports == 1 && s.import_before_mem_model); + CHECK(s.consts_before_function && s.scalar_consts == 4 && s.composite_consts == 4); + CHECK(s.fclamp_ops == 1 && s.round_ops == 1 && s.fmul_ops == 2 && s.stores == 2); + // Verify extra OpStore operands (Aligned 16) were preserved on the patched Location 0 store. + bool found_aligned_store = false; + for (std::size_t at = 5; at < w.size(); at += w[at] >> 16) { + if ((w[at] & 0xffffu) == kOpStore && (w[at] >> 16) == 5 && w[at + 1] == 10 && + w[at + 3] == 2 && w[at + 4] == 16 && w[at + 2] != 27) { + found_aligned_store = true; + } + } + CHECK(found_aligned_store); + } + + // 4. Existing GLSL.std.450 import is reused, and scalar + vec4 stores share a single set of 4 scalar OpConstants. + { + Words w = make_ps(true, true, true, false); + CHECK(leonida::rgba4::quantize(w, (1u << 0) | (1u << 1))); + const ModuleStats s = inspect(w); + CHECK(s.valid_framing && s.glsl_imports == 1 && s.import_before_mem_model); + CHECK(s.consts_before_function && s.scalar_consts == 4 && s.composite_consts == 4); + CHECK(s.fclamp_ops == 2 && s.round_ops == 2 && s.fmul_ops == 4 && s.stores == 2); + } + + // 5. Refusal cases leave `words` untouched: OpCopyMemory, non-float output, no matching store, bad header, + // truncated preamble/body, zero-word instruction, missing OpMemoryModel when import is needed. + { + Words copy_mem = make_ps(true, false, false, true); + const Words cm_orig = copy_mem; + CHECK(!leonida::rgba4::quantize(copy_mem, 1u << 0) && copy_mem == cm_orig); + + Words int_out = make_ps(true, false, false, false, true); + const Words io_orig = int_out; + CHECK(!leonida::rgba4::quantize(int_out, 1u << 0) && int_out == io_orig); + + Words no_store = make_ps(true, false, false, false); + const Words ns_orig = no_store; + CHECK(!leonida::rgba4::quantize(no_store, 1u << 1) && no_store == ns_orig); + + Words bad_magic{1, 2, 3, 4, 5, 6}; + CHECK(!leonida::rgba4::quantize(bad_magic, 1u)); + + Words short_hdr{0x07230203u, 0x00010600u, 0, 10}; + CHECK(!leonida::rgba4::quantize(short_hdr, 1u)); + + Words trunc_pre = make_ps(true, true, false, false); + trunc_pre[5] = (999u << 16) | kOpCapability; + CHECK(!leonida::rgba4::quantize(trunc_pre, 1u)); + + Words zero_op = make_ps(true, true, false, false); + zero_op[5] = kOpCapability; + CHECK(!leonida::rgba4::quantize(zero_op, 1u)); + + Words trunc_body = make_ps(true, true, false, false); + trunc_body.push_back((50u << 16) | kOpReturn); + const Words tb_orig = trunc_body; + CHECK(!leonida::rgba4::quantize(trunc_body, 1u) && trunc_body == tb_orig); + + // Missing OpMemoryModel when GLSL.std.450 must be injected refuses cleanly. + Words no_mm{0x07230203u, 0x00010600u, 0, 40u, 0}; + emit(no_mm, kOpDecorate, {10, kDecoLocation, 0}); + emit(no_mm, kOpTypeFloat, {2, 32}); + emit(no_mm, kOpTypeVector, {4, 2, 4}); + emit(no_mm, kOpTypePointer, {5, kStorageOutput, 4}); + emit(no_mm, kOpVariable, {5, 10, kStorageOutput}); + emit(no_mm, kOpFunction, {1, 20, 0, 1}); + emit(no_mm, kOpLabel, {21}); + emit(no_mm, kOpStore, {10, 27}); + emit(no_mm, kOpReturn, {}); + emit(no_mm, kOpFunctionEnd, {}); + const Words nm_orig = no_mm; + CHECK(!leonida::rgba4::quantize(no_mm, 1u) && no_mm == nm_orig); + } + + std::printf("rgba4_fragment_test: all checks passed\n"); + return 0; +}