// CPU-only unit test for runtime/src/sample_mask_fragment.h: // 1. quad_pixels(uint64_t) bit extraction (PA_SC_AA_MASK_X0Y0_X1Y0 / _X0Y1_X1Y1). // 2. sample_mask_patch_fragment() on fragment shaders: // - WITH BuiltIn FragCoord + EarlyFragmentTests + 144-byte PushConstant block (SPIR-V 1.0 and 1.4), // - WITHOUT FragCoord and WITHOUT PushConstant (SPIR-V 1.0 and 1.4), // - WITH Function-storage OpVariable in entry block and OpPhi referencing the entry label, // - Error cases (invalid magic, no fragment entry, PostDepthCoverage, overlapping offset). // 3. Structural SPIR-V invariants + external `spirv-val --target-env vulkan1.3` validation when available. // No GPU or game data required. #include "../src/sample_mask_fragment.h" #include #include #include #include #include #include #include #include #include #define EXPECT(cond) \ do { \ if (!(cond)) { \ std::fprintf(stderr, "sample_mask_fragment_test: line %d failed: %s\n", __LINE__, #cond); \ return 1; \ } \ } while (0) namespace { using namespace leonida; using namespace leonida::spirv_sample_mask; void append_string(Inst& i, const char* s) { const std::size_t n = std::strlen(s) + 1; const std::size_t words = (n + 3) / 4; const std::size_t at = i.size(); i.resize(at + words, 0); std::memcpy(&i[at], s, n); fix_length(i); } // Synthetic fragment shader matching the translator's PushConstant layout (144 bytes: // uint[32] + 4 uints at 128..140), optionally with FragCoord, EarlyFragmentTests, // PostDepthCoverage, Function OpVariable, and an OpPhi referencing the entry block. std::vector make_fragment(std::uint32_t version, bool has_frag_coord, bool early_tests, bool has_push_constant, bool with_local_and_phi, bool post_depth_coverage = false) { constexpr std::uint32_t op_capability = 17, op_memory_model = 14, op_type_array = 28, op_type_function = 33, op_store = 62, op_branch = 249, op_return = 253, op_f_add = 129; constexpr std::uint32_t mode_origin_upper_left = 7, storage_output = 3, dec_location = 30, dec_array_stride = 6; std::uint32_t next = 1; const auto id = [&] { return next++; }; const std::uint32_t id_main = id(), id_void = id(), id_fn = id(), id_f32 = id(), id_v4f = id(); const std::uint32_t id_ptr_out = id(), id_out_color = id(); const std::uint32_t id_ptr_in = has_frag_coord ? id() : 0; const std::uint32_t id_frag_coord = has_frag_coord ? id() : 0; const std::uint32_t id_u32 = has_push_constant ? id() : 0; const std::uint32_t id_c32 = has_push_constant ? id() : 0; const std::uint32_t id_c0 = has_push_constant ? id() : 0; const std::uint32_t id_arr = has_push_constant ? id() : 0; const std::uint32_t id_pc_struct = has_push_constant ? id() : 0; const std::uint32_t id_ptr_pc = has_push_constant ? id() : 0; const std::uint32_t id_ptr_pc_u32 = has_push_constant ? id() : 0; const std::uint32_t id_pc_var = has_push_constant ? id() : 0; const std::uint32_t id_f0 = id(), id_f1 = id(), id_v4_init = id(); const std::uint32_t id_ptr_fn_f32 = with_local_and_phi ? id() : 0; const std::uint32_t id_local = with_local_and_phi ? id() : 0; const std::uint32_t id_l0 = id(); const std::uint32_t id_l1 = with_local_and_phi ? id() : 0; const std::uint32_t id_phi = with_local_and_phi ? id() : 0; std::vector insts; insts.push_back(make(op_capability, {1})); // Shader if (post_depth_coverage) insts.push_back(make(op_capability, {4447})); // SampleMaskPostDepthCoverage insts.push_back(make(op_memory_model, {0, 1})); // Logical GLSL450 Inst ep = make(op_entry_point, {model_fragment, id_main}); append_string(ep, "main"); ep.push_back(id_out_color); if (has_frag_coord) ep.push_back(id_frag_coord); if (has_push_constant && version >= 0x00010400u) ep.push_back(id_pc_var); fix_length(ep); insts.push_back(std::move(ep)); insts.push_back(make(op_execution_mode, {id_main, mode_origin_upper_left})); if (early_tests) insts.push_back(make(op_execution_mode, {id_main, mode_early_fragment_tests})); if (post_depth_coverage) insts.push_back(make(op_execution_mode, {id_main, mode_post_depth_coverage})); insts.push_back(make(op_decorate, {id_out_color, dec_location, 0})); if (has_frag_coord) insts.push_back(make(op_decorate, {id_frag_coord, decoration_builtin, builtin_frag_coord})); if (has_push_constant) { insts.push_back(make(op_decorate, {id_arr, dec_array_stride, 4})); insts.push_back(make(op_decorate, {id_pc_struct, decoration_block})); insts.push_back(make(op_member_decorate, {id_pc_struct, 0, decoration_offset, 0})); insts.push_back(make(op_member_decorate, {id_pc_struct, 1, decoration_offset, 128})); insts.push_back(make(op_member_decorate, {id_pc_struct, 2, decoration_offset, 132})); insts.push_back(make(op_member_decorate, {id_pc_struct, 3, decoration_offset, 136})); insts.push_back(make(op_member_decorate, {id_pc_struct, 4, decoration_offset, 140})); } insts.push_back(make(op_type_void, {id_void})); insts.push_back(make(op_type_function, {id_fn, id_void})); insts.push_back(make(op_type_float, {id_f32, 32})); insts.push_back(make(op_type_vector, {id_v4f, id_f32, 4})); insts.push_back(make(op_type_pointer, {id_ptr_out, storage_output, id_v4f})); if (has_frag_coord) insts.push_back(make(op_type_pointer, {id_ptr_in, storage_input, id_v4f})); if (with_local_and_phi) insts.push_back(make(op_type_pointer, {id_ptr_fn_f32, storage_function, id_f32})); if (has_push_constant) { insts.push_back(make(op_type_int, {id_u32, 32, 0})); insts.push_back(make(op_constant, {id_u32, id_c32, 32})); insts.push_back(make(op_constant, {id_u32, id_c0, 0})); insts.push_back(make(op_type_array, {id_arr, id_u32, id_c32})); insts.push_back(make(op_type_struct, {id_pc_struct, id_arr, id_u32, id_u32, id_u32, id_u32})); insts.push_back(make(op_type_pointer, {id_ptr_pc, storage_push_constant, id_pc_struct})); insts.push_back(make(op_type_pointer, {id_ptr_pc_u32, storage_push_constant, id_u32})); } insts.push_back(make(op_constant, {id_f32, id_f0, 0x00000000u})); insts.push_back(make(op_constant, {id_f32, id_f1, 0x3f800000u})); insts.push_back(make(44 /* OpConstantComposite */, {id_v4f, id_v4_init, id_f1, id_f0, id_f0, id_f1})); insts.push_back(make(op_variable, {id_ptr_out, id_out_color, storage_output})); if (has_frag_coord) insts.push_back(make(op_variable, {id_ptr_in, id_frag_coord, storage_input})); if (has_push_constant) insts.push_back(make(op_variable, {id_ptr_pc, id_pc_var, storage_push_constant})); insts.push_back(make(op_function, {id_void, id_main, 0, id_fn})); insts.push_back(make(op_label, {id_l0})); if (with_local_and_phi) { insts.push_back(make(op_variable, {id_ptr_fn_f32, id_local, storage_function})); insts.push_back(make(op_store, {id_local, id_f1})); } if (has_push_constant) { const std::uint32_t ptr = id(), val = id(); insts.push_back(make(op_access_chain, {id_ptr_pc_u32, ptr, id_pc_var, id_c0, id_c0})); insts.push_back(make(op_load, {id_u32, val, ptr})); } if (has_frag_coord) { const std::uint32_t fc = id(); insts.push_back(make(op_load, {id_v4f, fc, id_frag_coord})); } if (with_local_and_phi) { const std::uint32_t lv = id(); insts.push_back(make(op_load, {id_f32, lv, id_local})); insts.push_back(make(op_branch, {id_l1})); insts.push_back(make(op_label, {id_l1})); insts.push_back(make(op_phi, {id_f32, id_phi, lv, id_l0})); const std::uint32_t sum = id(); insts.push_back(make(op_f_add, {id_f32, sum, id_phi, id_f0})); } insts.push_back(make(op_store, {id_out_color, id_v4_init})); insts.push_back(make(op_return, {})); insts.push_back(make(op_function_end, {})); std::vector words = {0x07230203u, version, 0, next, 0}; for (const Inst& i : insts) words.insert(words.end(), i.begin(), i.end()); return words; } struct Inspect { bool valid = false; bool has_early_fragment_tests = false; bool has_kill = false; int frag_coord_var_count = 0; int push_constant_var_count = 0; bool frag_coord_in_interface = false; bool push_constant_in_interface = false; bool has_offset_144 = false; bool entry_phi_uses_entry_label = false; bool local_var_before_branch = true; }; Inspect inspect_module(const std::vector& words) { Inspect out; if (words.size() < 5 || words[0] != 0x07230203u) return out; const std::uint32_t bound = words[3]; std::vector insts; for (std::size_t w = 5; w < words.size();) { const std::uint32_t n = words[w] >> 16; if (n == 0 || w + n > words.size()) return out; insts.emplace_back(words.begin() + static_cast(w), words.begin() + static_cast(w + n)); w += n; } std::uint32_t entry = 0; std::unordered_set iface; std::unordered_set fc_dec; std::unordered_set fc_vars; std::unordered_set pc_vars; std::uint32_t entry_label = 0; bool in_entry = false; bool seen_entry_terminator = false; bool in_global = true; for (const Inst& i : insts) { const std::uint32_t op = opcode(i); if (op == op_function) in_global = false; if (op == op_entry_point && i.size() > 2 && i[1] == model_fragment) { entry = i[2]; const std::size_t s = 3 + string_words(i, 3); for (std::size_t w = s; w < i.size(); ++w) iface.insert(i[w]); } else if ((op == op_execution_mode || op == op_execution_mode_id) && i.size() >= 3 && i[1] == entry && i[2] == mode_early_fragment_tests) { out.has_early_fragment_tests = true; } else if (op == op_decorate && i.size() == 4 && i[2] == decoration_builtin && i[3] == builtin_frag_coord) { fc_dec.insert(i[1]); } else if (op == op_member_decorate && i.size() == 5 && i[3] == decoration_offset && i[4] == kPixelMaskPushOffset) { out.has_offset_144 = true; } else if (in_global && op == op_variable && i.size() >= 4) { if (i[2] >= bound) return out; if (i[3] == storage_input && fc_dec.count(i[2])) fc_vars.insert(i[2]); if (i[3] == storage_push_constant) pc_vars.insert(i[2]); } else if (op == op_function && i.size() >= 3 && i[2] == entry) { in_entry = true; } else if (in_entry) { if (op == op_function_end) { in_entry = false; } else if (op == op_label && !entry_label) { entry_label = i[1]; } else if (op == op_variable && seen_entry_terminator) { out.local_var_before_branch = false; } else if (is_terminator(op)) { seen_entry_terminator = true; if (op == op_kill) out.has_kill = true; } else if (op == op_phi) { for (std::size_t w = 4; w < i.size(); w += 2) { if (i[w] == entry_label) out.entry_phi_uses_entry_label = true; } } } } out.frag_coord_var_count = static_cast(fc_vars.size()); out.push_constant_var_count = static_cast(pc_vars.size()); for (std::uint32_t v : fc_vars) out.frag_coord_in_interface |= iface.count(v) != 0; for (std::uint32_t v : pc_vars) out.push_constant_in_interface |= iface.count(v) != 0; out.valid = true; return out; } bool have_spirv_val() { #ifdef _WIN32 return std::system("where spirv-val >NUL 2>&1") == 0; #else return std::system("command -v spirv-val >/dev/null 2>&1") == 0; #endif } bool run_spirv_val(const std::vector& words, const char* tag) { const auto path = std::filesystem::temp_directory_path() / (std::string("leonida_sm_") + tag + ".spv"); { std::ofstream f(path, std::ios::binary | std::ios::trunc); if (!f) return false; f.write(reinterpret_cast(words.data()), static_cast(words.size() * sizeof(std::uint32_t))); } const std::string cmd = "spirv-val --target-env vulkan1.3 \"" + path.string() + "\""; const int rc = std::system(cmd.c_str()); std::error_code ec; std::filesystem::remove(path, ec); return rc == 0; } } // namespace int main() { // 1. quad_pixels(uint64_t) bit extraction: 16 bits per pixel in 2x2 quad, sample 0 at bits 0, 16, 32, 48. EXPECT(quad_pixels(0x0000000000000000ull) == 0x0u); EXPECT(quad_pixels(0xFFFFFFFFFFFFFFFFull) == 0xFu); EXPECT(quad_pixels(0x0001000100010001ull) == 0xFu); EXPECT(quad_pixels(0xFFFEFFFEFFFEFFFEull) == 0x0u); // high sample bits 1..15 ignored EXPECT(quad_pixels(0x0000000000000001ull) == 0x1u); // X0Y0 EXPECT(quad_pixels(0x0000000000010000ull) == 0x2u); // X1Y0 EXPECT(quad_pixels(0x0000000100000000ull) == 0x4u); // X0Y1 EXPECT(quad_pixels(0x0001000000000000ull) == 0x8u); // X1Y1 EXPECT(quad_pixels(0x0000000100000001ull) == 0x5u); // even X EXPECT(quad_pixels(0x0001000000010000ull) == 0xAu); // odd X for (std::uint32_t p = 0; p < 16; ++p) { const std::uint64_t mask = (std::uint64_t((p >> 0) & 1u) << 0) | (std::uint64_t((p >> 1) & 1u) << 16) | (std::uint64_t((p >> 2) & 1u) << 32) | (std::uint64_t((p >> 3) & 1u) << 48) | 0xAAA85554CCC23332ull; // arbitrary bits in 1..15 of each pixel EXPECT(quad_pixels(mask) == p); } const bool validate_external = have_spirv_val(); // 2. Patching fragment shaders across combinations of FragCoord, EarlyFragmentTests, // PushConstant block, local OpVariable + entry-block OpPhi, and SPIR-V 1.0 vs 1.4. struct Case { const char* name; std::uint32_t version; bool has_fc; bool early_tests; bool has_pc; bool local_phi; }; const Case cases[] = { {"v10_fc_early_pc", 0x00010000u, true, true, true, false}, {"v14_fc_early_pc", 0x00010400u, true, true, true, false}, {"v10_nofc_nopc", 0x00010000u, false, false, false, false}, {"v14_nofc_nopc", 0x00010400u, false, false, false, false}, {"v10_nofc_early_pc", 0x00010000u, false, true, true, false}, {"v14_nofc_early_pc", 0x00010400u, false, true, true, false}, {"v10_local_phi", 0x00010000u, true, true, true, true}, {"v14_local_phi", 0x00010400u, false, true, false, true}, }; for (const Case& c : cases) { const auto input = make_fragment(c.version, c.has_fc, c.early_tests, c.has_pc, c.local_phi); if (validate_external) EXPECT(run_spirv_val(input, "in")); std::string why; const auto patched = sample_mask_patch_fragment(input, why); EXPECT(!patched.empty()); EXPECT(why.empty()); const Inspect info = inspect_module(patched); EXPECT(info.valid); EXPECT(!info.has_early_fragment_tests); EXPECT(info.has_kill); EXPECT(info.frag_coord_var_count == 1); EXPECT(info.push_constant_var_count == 1); EXPECT(info.frag_coord_in_interface); EXPECT(info.push_constant_in_interface == (c.version >= 0x00010400u)); EXPECT(info.has_offset_144); EXPECT(!info.entry_phi_uses_entry_label); EXPECT(info.local_var_before_branch); if (validate_external) EXPECT(run_spirv_val(patched, c.name)); } // 3. Error cases & hardened SPIR-V edge cases. { std::string why; EXPECT(sample_mask_patch_fragment({}, why).empty() && !why.empty()); EXPECT(sample_mask_patch_fragment({0x12345678u, 0, 0, 1, 0}, why).empty() && !why.empty()); const auto pdc = make_fragment(0x00010000u, true, true, true, false, true); EXPECT(sample_mask_patch_fragment(pdc, why).empty() && !why.empty()); const auto ok_in = make_fragment(0x00010000u, true, true, true, false); EXPECT(sample_mask_patch_fragment(ok_in, why, 142u).empty() && !why.empty()); // unaligned offset EXPECT(sample_mask_patch_fragment(ok_in, why, 0u).empty() && !why.empty()); // overlaps array member at 0 // Member starting at off < push_offset whose byte span crosses push_offset: // member 0 is uint[32] (ArrayStride 4 -> [0, 128)), so offset 124 MUST be rejected! EXPECT(sample_mask_patch_fragment(ok_in, why, 124u).empty() && !why.empty()); EXPECT(sample_mask_patch_fragment(ok_in, why, 64u).empty() && !why.empty()); // OpMemberDecorate %blk 0 BuiltIn FragCoord (Input block): cleanly rejected rather than duplicating FragCoord. { std::vector insts; insts.push_back(make(17 /* OpCapability */, {1})); insts.push_back(make(14 /* OpMemoryModel */, {0, 1})); Inst ep = make(op_entry_point, {model_fragment, 1}); append_string(ep, "main"); ep.push_back(8); fix_length(ep); insts.push_back(std::move(ep)); insts.push_back(make(op_execution_mode, {1, 7})); insts.push_back(make(op_decorate, {6, decoration_block})); insts.push_back(make(op_member_decorate, {6, 0, decoration_builtin, builtin_frag_coord})); insts.push_back(make(op_type_void, {2})); insts.push_back(make(33 /* OpTypeFunction */, {3, 2})); insts.push_back(make(op_type_float, {4, 32})); insts.push_back(make(op_type_vector, {5, 4, 4})); insts.push_back(make(op_type_struct, {6, 5})); insts.push_back(make(op_type_pointer, {7, storage_input, 6})); insts.push_back(make(op_variable, {7, 8, storage_input})); insts.push_back(make(op_function, {2, 1, 0, 3})); insts.push_back(make(op_label, {9})); insts.push_back(make(253 /* OpReturn */, {})); insts.push_back(make(op_function_end, {})); std::vector blk = {0x07230203u, 0x00010000u, 0, 10, 0}; for (const Inst& i : insts) blk.insert(blk.end(), i.begin(), i.end()); EXPECT(sample_mask_patch_fragment(blk, why).empty() && !why.empty()); } // OpLine preceding the first OpType*: new_decorations must still land in section 8 (before OpLine). { std::vector insts; insts.push_back(make(17 /* OpCapability */, {1})); insts.push_back(make(14 /* OpMemoryModel */, {0, 1})); Inst ep = make(op_entry_point, {model_fragment, 1}); append_string(ep, "main"); fix_length(ep); insts.push_back(std::move(ep)); insts.push_back(make(op_execution_mode, {1, 7})); Inst file_str = make(7 /* OpString */, {5}); append_string(file_str, "test.glsl"); insts.push_back(std::move(file_str)); insts.push_back(make(op_line, {5, 1, 0})); // OpLine before first OpType* insts.push_back(make(op_type_void, {2})); insts.push_back(make(33 /* OpTypeFunction */, {3, 2})); insts.push_back(make(op_function, {2, 1, 0, 3})); insts.push_back(make(op_label, {4})); insts.push_back(make(253 /* OpReturn */, {})); insts.push_back(make(op_function_end, {})); std::vector with_line = {0x07230203u, 0x00010000u, 0, 6, 0}; for (const Inst& i : insts) with_line.insert(with_line.end(), i.begin(), i.end()); if (validate_external) EXPECT(run_spirv_val(with_line, "line_in")); const auto patched = sample_mask_patch_fragment(with_line, why); EXPECT(!patched.empty() && why.empty()); if (validate_external) EXPECT(run_spirv_val(patched, "line_out")); } } std::printf("sample_mask_fragment_test: PASS (spirv-val=%s)\n", validate_external ? "verified" : "unavailable"); return 0; }