mirror of
https://git.triforceproject.com/triforce/source
synced 2026-11-20 08:39:58 -04:00
Triforce v0.5.1
Hotfix for v0.5.0: crashes (NX2navi game folder refused by Setup, heap-full retries, GTX 10 series noise pass and whole VRAM mirrors, code-patch mods kept with their code, no page file warning), graphics (DLSS with frame generation, DLSS coming back, AMD storage buffer alignment, mesh index map, shared memory barrier, shader conversions, integrated GPUs' uniform snapshot, a Mesh shaders row), controls (microphone after restart, button prompts with a mod's size table, sprint vs Hide text, native mouse camera, controller after Restart now), Setup's Preparing shaders step, and the Linux link fix.
This commit is contained in:
@@ -18,7 +18,7 @@ add_executable(f2i_immediate
|
||||
"${SHADER}/frontend/maxwell/decode.cpp"
|
||||
"${SHADER}/frontend/maxwell/opcodes.cpp" "${IMPL}/common_funcs.cpp"
|
||||
"${IMPL}/impl.cpp" "${IMPL}/register_map_nx2.cpp" "${IMPL}/move_register.cpp"
|
||||
"${IMPL}/floating_point_conversion_integer.cpp"
|
||||
"${IMPL}/floating_point_conversion_integer.cpp" "${IMPL}/integer_floating_point_conversion.cpp"
|
||||
"${IMPL}/floating_point_compare_and_set.cpp" "${IMPL}/floating_point_min_max.cpp"
|
||||
"${IMPL}/logic_operation.cpp"
|
||||
${BACKEND_SOURCES} ${IR_SOURCES} ${OPTIMIZATION_SOURCES} ${SIRIT_SOURCES})
|
||||
|
||||
@@ -23,11 +23,17 @@ for module in build-f2i/modules/*.spv; do
|
||||
done
|
||||
```
|
||||
|
||||
It also takes literal inputs through F2I.S32.TRUNC from F32 and I2FP.F32.U32.RZ and
|
||||
evaluates the IR the production passes leave (a small evaluator in the fixture): F2I
|
||||
must saturate 2^31, 2^32, the largest float and +inf to INT32_MAX without handing
|
||||
OpConvertFToS a value out of its range, and I2F must round toward zero (0xffffffff
|
||||
to 0x4f7fffff), not to nearest as OpConvertUToF does.
|
||||
|
||||
The unmodified `232eecb` lowerer fails the first authored immediate case with
|
||||
`unsupported native opcode`. The fixture tests the conversion path and its IR
|
||||
controls; SPIR-V validation does not establish numerical results on a GPU or a
|
||||
fix for any particular visible game symptom. In particular, it does not change
|
||||
or independently certify existing register-path saturation at numeric limits.
|
||||
fix for any particular visible game symptom. The value checks are of the IR as
|
||||
SPIR-V defines its instructions, not of a driver's results.
|
||||
|
||||
Encoding reference: [Mesa NAK, pinned sm70_encode.rs](https://chromium.googlesource.com/external/gitlab.freedesktop.org/mesa/mesa/+/b1fe47e94409d81924c8663f58fac97e66ab4527/src/nouveau/compiler/nak/sm70_encode.rs#2134)
|
||||
(`OpF2I`, `encode_alu_base` and `encode_alu_imm`). The `0x105` base plus immediate
|
||||
|
||||
@@ -1,6 +1,8 @@
|
||||
// Authored instructions only. No dump, native program, GPU or game is read.
|
||||
#include <algorithm>
|
||||
#include <array>
|
||||
#include <bit>
|
||||
#include <cmath>
|
||||
#include <cstdio>
|
||||
#include <cstdlib>
|
||||
#include <filesystem>
|
||||
@@ -62,12 +64,14 @@ static void translate(Maxwell::TranslatorVisitor& visitor, u64 word) {
|
||||
case Maxwell::Opcode::FSET_reg: visitor.FSET_reg(word); break;
|
||||
case Maxwell::Opcode::FMNMX_reg: visitor.FMNMX_reg(word); break;
|
||||
case Maxwell::Opcode::LOP_reg: visitor.LOP_reg(word); break;
|
||||
case Maxwell::Opcode::I2F_reg: visitor.I2F_reg(word); break;
|
||||
case Maxwell::Opcode::EXIT: break;
|
||||
default: CHECK(false);
|
||||
}
|
||||
}
|
||||
|
||||
static unsigned modules = 0;
|
||||
static unsigned value_failures = 0;
|
||||
static void check_pipeline(const std::filesystem::path& output, u32 literal, bool sign,
|
||||
u32 rounding, bool ftz, u32 dst_width = 2) {
|
||||
constexpr u32 dest = 13;
|
||||
@@ -160,6 +164,138 @@ static void check_pipeline(const std::filesystem::path& output, u32 literal, boo
|
||||
CHECK(stream.good());
|
||||
}
|
||||
|
||||
// A small evaluator of the 32-bit IR the conversions leave after the production passes, for literal sources: the stored
|
||||
// value, as the GPU would compute it if each instruction did what SPIR-V defines. A conversion given a value it cannot
|
||||
// represent (OpConvertFToS/U out of range: undefined) is reported in `undefined` instead of being guessed
|
||||
static std::optional<u32> evaluate(const IR::Value& value, bool& undefined) {
|
||||
if (value.IsImmediate()) return value.Type() == IR::Type::U1 ? u32{value.U1()} : value.U32();
|
||||
const IR::Inst& inst = *value.InstRecursive();
|
||||
const auto arg = [&](size_t i) { return evaluate(inst.Arg(i), undefined); };
|
||||
const auto f = [](u32 bits) { return std::bit_cast<f32>(bits); };
|
||||
const auto b = [](f32 x) { return std::bit_cast<u32>(x); };
|
||||
std::array<u32, 3> a{};
|
||||
for (size_t i = 0; i < inst.NumArgs() && i < 3; ++i) {
|
||||
const auto v = arg(i);
|
||||
if (!v) return std::nullopt;
|
||||
a[i] = *v;
|
||||
}
|
||||
switch (inst.GetOpcode()) {
|
||||
case IR::Opcode::Identity: return a[0];
|
||||
case IR::Opcode::SelectU32: case IR::Opcode::SelectF32: return a[0] ? a[1] : a[2];
|
||||
case IR::Opcode::IEqual: return u32{a[0] == a[1]};
|
||||
case IR::Opcode::INotEqual: return u32{a[0] != a[1]};
|
||||
case IR::Opcode::UGreaterThan: return u32{a[0] > a[1]};
|
||||
case IR::Opcode::SGreaterThan: return u32{s32(a[0]) > s32(a[1])};
|
||||
case IR::Opcode::FindUMsb32: return a[0] ? u32(31 - std::countl_zero(a[0])) : ~0U;
|
||||
case IR::Opcode::UMax32: return std::max(a[0], a[1]);
|
||||
case IR::Opcode::IAdd32: return a[0] + a[1];
|
||||
case IR::Opcode::ISub32: return a[0] - a[1];
|
||||
case IR::Opcode::ShiftLeftLogical32: return a[1] < 32 ? a[0] << a[1] : 0U;
|
||||
case IR::Opcode::ShiftRightLogical32: return a[1] < 32 ? a[0] >> a[1] : 0U;
|
||||
case IR::Opcode::BitwiseAnd32: return a[0] & a[1];
|
||||
case IR::Opcode::BitwiseOr32: return a[0] | a[1];
|
||||
case IR::Opcode::BitCastF32U32: case IR::Opcode::BitCastU32F32: return a[0];
|
||||
case IR::Opcode::ConvertF32U32: { // (exact below 2^24: the only inputs the RZ path hands it)
|
||||
if (a[0] >= (1U << 24)) undefined = true;
|
||||
return b(f32(a[0]));
|
||||
}
|
||||
case IR::Opcode::FPIsNan32: return u32{std::isnan(f(a[0]))};
|
||||
case IR::Opcode::FPOrdGreaterThanEqual32: return u32{f(a[0]) >= f(a[1])};
|
||||
case IR::Opcode::FPTrunc32: return b(std::trunc(f(a[0])));
|
||||
case IR::Opcode::FPRoundEven32: return b(std::nearbyint(f(a[0])));
|
||||
case IR::Opcode::FPFloor32: return b(std::floor(f(a[0])));
|
||||
case IR::Opcode::FPCeil32: return b(std::ceil(f(a[0])));
|
||||
case IR::Opcode::FPAbs32: return a[0] & 0x7fffffffU;
|
||||
case IR::Opcode::FPNeg32: return a[0] ^ 0x80000000U;
|
||||
case IR::Opcode::FPClamp32: // (OpNClamp: a NaN gives the other operand)
|
||||
return b(std::fmin(std::fmax(f(a[0]), f(a[1])), f(a[2])));
|
||||
case IR::Opcode::ConvertS32F32: {
|
||||
const f32 x = f(a[0]);
|
||||
if (!(x >= -2147483648.0f && x < 2147483648.0f)) { undefined = true; return 0U; }
|
||||
return u32(s32(x));
|
||||
}
|
||||
case IR::Opcode::ConvertU32F32: {
|
||||
const f32 x = f(a[0]);
|
||||
if (!(x >= 0.0f && x < 4294967296.0f)) { undefined = true; return 0U; }
|
||||
return u32(x);
|
||||
}
|
||||
default:
|
||||
std::fprintf(stderr, "evaluate: no %.*s\n", int(IR::NameOf(inst.GetOpcode()).size()), IR::NameOf(inst.GetOpcode()).data());
|
||||
return std::nullopt;
|
||||
}
|
||||
}
|
||||
|
||||
static std::optional<u32> stored_value(const IR::Block& block, bool& undefined) {
|
||||
for (const IR::Inst& inst : block.Instructions())
|
||||
if (inst.GetOpcode() == IR::Opcode::WriteSharedU32) return evaluate(inst.Arg(1), undefined);
|
||||
return std::nullopt;
|
||||
}
|
||||
|
||||
// A native instruction through the lowering, the Maxwell translation and the production passes, its result evaluated
|
||||
// (above): source, when given, is a Maxwell MOV32I of a literal into R19 first
|
||||
static void check_value(const std::filesystem::path& output, const char* what, Native native, std::optional<u32> source,
|
||||
u32 expected) {
|
||||
constexpr u32 dest = 13;
|
||||
const std::array code{native, instruction(0x94d, 0, 0, 0x03800000)};
|
||||
const auto lowered = Ampere::LowerToMaxwell(code);
|
||||
CHECK(lowered);
|
||||
ObjectPool<IR::Inst> pool;
|
||||
IR::Block block{pool};
|
||||
IR::Program program;
|
||||
program.stage = Stage::Compute;
|
||||
program.workgroup_size = {1, 1, 1};
|
||||
program.shared_memory_size = 4;
|
||||
program.blocks.push_back(&block);
|
||||
program.post_order_blocks.push_back(&block);
|
||||
IR::AbstractSyntaxNode node{};
|
||||
node.type = IR::AbstractSyntaxNode::Type::Block;
|
||||
node.data.block = █
|
||||
program.syntax_list.push_back(node);
|
||||
IR::AbstractSyntaxNode ret{};
|
||||
ret.type = IR::AbstractSyntaxNode::Type::Return;
|
||||
program.syntax_list.push_back(ret);
|
||||
FixtureEnvironment env;
|
||||
Maxwell::TranslatorVisitor visitor{env, block};
|
||||
if (source) translate(visitor, (u64{0x010} << 52) | (u64{*source} << 20) | (u64{0xf} << 12) | 19); // MOV32I R19
|
||||
for (u64 word : lowered.instructions) translate(visitor, word);
|
||||
visitor.ir.WriteShared(32, visitor.ir.Imm32(0), visitor.X(static_cast<IR::Reg>(dest)));
|
||||
visitor.ir.Epilogue();
|
||||
Optimization::SsaRewritePass(program);
|
||||
Optimization::ConstantPropagationPass(env, program);
|
||||
Optimization::DeadCodeEliminationPass(program);
|
||||
Optimization::IdentityRemovalPass(program);
|
||||
bool undefined = false;
|
||||
const auto stored = stored_value(block, undefined);
|
||||
CHECK(stored.has_value());
|
||||
const u32 input = source ? *source : native.words[1];
|
||||
if (*stored != expected || undefined)
|
||||
std::fprintf(stderr, "%s %08x: %08x%s, expected %08x\n", what, input, *stored,
|
||||
undefined ? " (a conversion out of its range)" : "", expected);
|
||||
if (*stored != expected || undefined) value_failures++; // (counted: every case is reported before the exit status)
|
||||
Optimization::CollectShaderInfoPass(env, program);
|
||||
Profile profile{};
|
||||
profile.supported_spirv = 0x00010600;
|
||||
const auto spv = Backend::SPIRV::EmitSPIRV(profile, program);
|
||||
CHECK(spv.size() > 5);
|
||||
const auto path = output / ("case-" + std::to_string(modules++) + ".spv");
|
||||
std::ofstream stream(path, std::ios::binary);
|
||||
stream.write(reinterpret_cast<const char*>(spv.data()), spv.size() * sizeof(u32));
|
||||
CHECK(stream.good());
|
||||
std::printf("%s %08x -> %08x\n", what, input, *stored);
|
||||
}
|
||||
|
||||
// F2I.S32 from F32, toward zero (Fritchii eba45b151): INT32_MAX is 2^31 as an F32, so the clamp let 2^31 and above through
|
||||
// to OpConvertFToS, out of its range (undefined); they saturate to INT32_MAX
|
||||
static void check_f2i_saturation(const std::filesystem::path& output, u32 literal, u32 expected) {
|
||||
check_value(output, "F2I.S32.TRUNC", instruction(0x905, 13, literal, mode(true, 3, false)), std::nullopt, expected);
|
||||
}
|
||||
|
||||
// I2FP.F32.U32.RZ (Ampere 0x245, rounding in bits 78-79): OpConvertUToF rounds to nearest, so the RZ conversion is made
|
||||
// from the top 24 significant bits (Fritchii f6a8f5132)
|
||||
static void check_i2f_rz(const std::filesystem::path& output, u32 literal, u32 expected) {
|
||||
check_value(output, "I2F.F32.U32.RZ", Native{{{0x245U | (7U << 12) | (13U << 16), 19, 0x0020d000U, 0}}}, literal, expected);
|
||||
}
|
||||
|
||||
static void check_source_controls() {
|
||||
// Existing source forms remain accepted with their original source handling.
|
||||
for (u32 op : {0x305U, 0xb05U, 0xd05U}) {
|
||||
@@ -223,5 +359,23 @@ int main(int argc, char** argv) {
|
||||
for (u32 literal : literals) check_pipeline(output, literal, true, 3, true);
|
||||
check_pipeline(output, std::bit_cast<u32>(32768.5f), false, 0, true, 1);
|
||||
check_controls();
|
||||
check_f2i_saturation(output, 0x4f000000U, 0x7fffffffU); // 2^31
|
||||
check_f2i_saturation(output, 0x4f800000U, 0x7fffffffU); // 2^32
|
||||
check_f2i_saturation(output, 0x7f7fffffU, 0x7fffffffU); // the largest float
|
||||
check_f2i_saturation(output, 0x7f800000U, 0x7fffffffU); // +inf
|
||||
check_f2i_saturation(output, 0x4effffffU, 0x7fffff80U); // the largest float below 2^31: exact
|
||||
check_f2i_saturation(output, 0xcf000000U, 0x80000000U); // -2^31: exact
|
||||
check_f2i_saturation(output, 0xff800000U, 0x80000000U); // -inf
|
||||
check_f2i_saturation(output, 0x7fc00000U, 0U); // NaN
|
||||
check_f2i_saturation(output, std::bit_cast<u32>(-13.75f), u32(-13));
|
||||
// U32 -> F32 toward zero: the largest values stay below 2^32, ties and the bits under the 24 kept drop, small values exact
|
||||
check_i2f_rz(output, 0xffffffffU, 0x4f7fffffU);
|
||||
check_i2f_rz(output, 0xffffff80U, 0x4f7fffffU);
|
||||
check_i2f_rz(output, 0x01000001U, 0x4b800000U);
|
||||
check_i2f_rz(output, 0x00ffffffU, 0x4b7fffffU);
|
||||
check_i2f_rz(output, 0x80000000U, 0x4f000000U);
|
||||
check_i2f_rz(output, 0U, 0U);
|
||||
check_i2f_rz(output, 1U, 0x3f800000U);
|
||||
CHECK(value_failures == 0);
|
||||
std::printf("PASS: %u authored native F2I immediate -> Maxwell -> IR -> SPIR-V cases; source/mode/predicate controls pass\n", modules);
|
||||
}
|
||||
|
||||
@@ -86,6 +86,8 @@ void binds_load(void) {
|
||||
/* the native mouse look (no camera found: the motion goes to mouse_stick) and the mouse's sensitivity, which
|
||||
read_input calls */
|
||||
int hos_mouse_native(void) { return -1; }
|
||||
void hos_mouse_native_free(int free) { (void)free; }
|
||||
void hos_mouse_native_stick(int held) { (void)held; }
|
||||
static void mouse_setup(void) {}
|
||||
static void mouse_sens_keys(void) {}
|
||||
int mouse_stick(int32_t *x, int32_t *y, int held) { (void)x; (void)y; (void)held; mouse_queries++; return 0; }
|
||||
|
||||
@@ -221,8 +221,8 @@ def python_check(rel, decl, tools, timeout):
|
||||
|
||||
def cpp_checks(tools, timeout):
|
||||
"""gpu/vk/tests: the presentation rectangle (all of it), the lowering harness built and run over no programs
|
||||
(the programs it reads are dumped from the game, so here it only shows the lowering still compiles), and the
|
||||
lowering's register-marker regression"""
|
||||
(the programs it reads are dumped from the game, so here it only shows the lowering still compiles), the
|
||||
lowering's register-marker regression and LDSM's address copy"""
|
||||
cxx = tools["cxx"]
|
||||
out = []
|
||||
|
||||
@@ -244,6 +244,8 @@ def cpp_checks(tools, timeout):
|
||||
out.append((lower, build_and_run(lower, ["-O1", "-Wno-everything", "-I" + eden, "-I" + compat], ["{tmp}"])))
|
||||
regmap = "gpu/vk/tests/regmap_marker_test.cpp" # (the lowering's renamed-register markers: no number mapped twice)
|
||||
out.append((regmap, build_and_run(regmap, ["-O1", "-Wno-everything", "-I" + eden, "-I" + compat], [])))
|
||||
ldsm = "gpu/vk/tests/ldsm_address_test.cpp" # (LDSM's row addresses kept apart from its shuffles' registers)
|
||||
out.append((ldsm, build_and_run(ldsm, ["-O1", "-Wno-everything", "-I" + eden, "-I" + compat], [])))
|
||||
return out, ([] if cxx else ["a C++ compiler"])
|
||||
|
||||
|
||||
|
||||
@@ -64,7 +64,14 @@ int main(int argc, char** argv) {
|
||||
dev.subgroup_max = number(2);
|
||||
dev.sg32_stages = number(3);
|
||||
dev.driver_id = number(4);
|
||||
// (storage buffers bound and translated at 16 bytes at least: AMD's 4 made the 8- and 16-byte vector views inexact)
|
||||
for (const uint64_t device : {uint64_t{4}, uint64_t{16}, uint64_t{64}}) {
|
||||
dev.props.limits.minStorageBufferOffsetAlignment = device;
|
||||
assert(storage_binding_align() == std::max<uint64_t>(device, 16));
|
||||
}
|
||||
dev.props.limits.minStorageBufferOffsetAlignment = 4;
|
||||
check(profile(), number(5));
|
||||
assert(profile().min_ssbo_alignment == 16);
|
||||
check(profile_graphics(VK_SHADER_STAGE_VERTEX_BIT), number(6));
|
||||
check(profile_graphics(VK_SHADER_STAGE_FRAGMENT_BIT), number(7));
|
||||
check(profile_pinned(), number(8));
|
||||
@@ -101,16 +108,20 @@ def main():
|
||||
void check_stamp(uint32_t size, uint32_t pinned, bool workaround, bool forced) {
|
||||
bool added = false;
|
||||
const auto add = [&](const uint32_t* values, size_t bytes) {
|
||||
assert(bytes == 5 * sizeof(uint32_t));
|
||||
assert(bytes == 6 * sizeof(uint32_t));
|
||||
assert(values[0] == uint32_t(size > 32) && values[1] == size);
|
||||
assert(values[2] == uint32_t(workaround) && values[3] == pinned && values[4] == uint32_t(forced));
|
||||
// (the storage binding alignment the translation is built with: a cache of a 16-aligned GPU is no use on a 64-aligned one)
|
||||
assert(values[5] == uint32_t(storage_binding_align()));
|
||||
added = true;
|
||||
};
|
||||
''' + stamp + r'''
|
||||
assert(added);
|
||||
}
|
||||
'''
|
||||
fixture = (FIXTURE + section(shader, "Shader::Profile make_profile(", "Shader::HostTranslateInfo host_info()")
|
||||
internal = (VK / "vk_internal.h").read_text(encoding="utf-8")
|
||||
fixture = (FIXTURE + section(internal, "inline uint64_t storage_binding_align()", "/// A range of guest memory")
|
||||
+ section(shader, "Shader::Profile make_profile(", "Shader::HostTranslateInfo host_info()")
|
||||
+ section(shader, "const Shader::Profile& profile()", "const std::vector<Code>& codes_of(")
|
||||
+ section(warp, "Id HostThreadId(", "Id GuestLane(")
|
||||
+ section(warp, "bool UseShuffleFallback(", "Id ShuffleAbsolute(") + check_stamp + CASES)
|
||||
|
||||
@@ -344,6 +344,12 @@ int main() {
|
||||
names = {std::string(59, 'x') + "\xc3\xa9" + "yy"}; /* cut at a character's start, not inside the 2-byte one */
|
||||
opts.mic = 2, mic_chosen();
|
||||
CHECK(mic_want == std::string(59, 'x'));
|
||||
names = {"USB Mic", "Webcam"}; /* a saved name read back (the options file's line, with its end) */
|
||||
for (const char* line : {"USB Mic\n", "USB Mic\r\n", "USB Mic \n"}) { /* (the last: as an older build saved it) */
|
||||
mic_want = mic_key(line), opts.mic = 2;
|
||||
refresh_mics();
|
||||
CHECK(mic_want == "USB Mic" && !mic_missing && opts.mic == 2 && !std::strcmp(tx_mic(2), "USB Mic") && told.index == 0);
|
||||
}
|
||||
std::puts("PASS: the Microphone row keeps its choice by name, and tells the host");
|
||||
return 0;
|
||||
}
|
||||
@@ -385,6 +391,8 @@ def main():
|
||||
'the Insert menu has no Microphone row')
|
||||
check(re.search(r'hos_mic_count,\s*hos_mic_name,\s*hos_mic_select', (ROOT / 'host' / 'rx_backend.c').read_text(encoding='utf-8')),
|
||||
'the menu is not given the microphones')
|
||||
check('mic_want = mic_key(eq + 1);' in (ROOT / 'gpu' / 'vk' / 'vk_menu.cpp').read_text(encoding='utf-8'),
|
||||
'the saved microphone is not read through mic_key')
|
||||
check('if (!mic_want.empty()) refresh_mics();' in (ROOT / 'gpu' / 'vk' / 'vk_menu.cpp').read_text(encoding='utf-8'),
|
||||
'the start lists the microphones (and starts SDL audio) even with none chosen')
|
||||
with tempfile.TemporaryDirectory(prefix='audio-in-') as t:
|
||||
|
||||
@@ -66,6 +66,18 @@ class BcRawTargetTests(unittest.TestCase):
|
||||
self.assertNotIn("53509044401d78d2", line)
|
||||
self.assertRegex(self.draw, r"default_list && cp->key == 0x53509044401d78d2ull && !at\.n")
|
||||
|
||||
def test_3d_bc_images_take_no_attachment_usage_on_nvidia_before_turing(self):
|
||||
# (Pascal loses the device on a raw view of a 3D BC image, upstream #2327: no usage there, so no view, and
|
||||
# the noise pass is left out by the rule above)
|
||||
raw = self.tex.index("Raw storage views address one compressed block per texel")
|
||||
branch = self.tex[raw:self.tex.index("CUBE_COMPATIBLE_BIT", raw)]
|
||||
self.assertIn("dev.props.vendorID == 0x10de && dev.props.deviceID < 0x1e00", branch)
|
||||
guard = branch.index("if (x.type == VK_IMAGE_TYPE_3D && !bc3d_target)")
|
||||
usage = branch.index("x.usage |= VK_IMAGE_USAGE_COLOR_ATTACHMENT_BIT;")
|
||||
self.assertLess(guard, usage)
|
||||
self.assertIn("} else if (format_props(raw).optimalTilingFeatures & VK_FORMAT_FEATURE_COLOR_ATTACHMENT_BIT)", branch)
|
||||
self.assertIn('getenv("RX_VK_BC3D_TARGET")', branch)
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
unittest.main()
|
||||
|
||||
@@ -54,14 +54,17 @@ static int MessageBoxA(void *w, const char *text, const char *caption, unsigned
|
||||
int hos_sdl_load(void) { return 0; }
|
||||
@@CODE@@
|
||||
int main(void) {
|
||||
/* ((Triforce, 11-18) no swap at all below 40 GB of memory says so too: 16384 + 0 and 32768 + 0 now; 65536 + 0 not) */
|
||||
static const unsigned long long cases[][3] = {
|
||||
{8192, 0, 1}, {8192, 2047, 1}, {8192, 2048, 0}, {16384, 0, 0}, {10239, 0, 1}, {10240, 0, 0}};
|
||||
{8192, 0, 1}, {8192, 2047, 1}, {8192, 2048, 0}, {16384, 0, 1}, {10239, 0, 1}, {10240, 1, 0}, {32768, 0, 1},
|
||||
{32768, 1024, 0}, {65536, 0, 0}};
|
||||
char text[512];
|
||||
for (size_t i = 0; i < sizeof cases / sizeof *cases; i++) {
|
||||
const int got = memory_notice_text(cases[i][0], cases[i][1], text, sizeof text);
|
||||
printf("case %llu MB + %llu MB swap: %s\n", cases[i][0], cases[i][1], got ? "notice" : "none");
|
||||
if (got != (int)cases[i][2]) return printf("FAIL: wrong answer\n"), 1;
|
||||
if (got && (!strstr(text, "8.0 GB") && cases[i][0] == 8192)) return printf("FAIL: the text has no size: %s\n", text), 1;
|
||||
if (got && cases[i][0] == 32768 && !strstr(text, "page file")) return printf("FAIL: no page file not named: %s\n", text), 1;
|
||||
}
|
||||
setenv("HOS_HEADLESS", "1", 1);
|
||||
memory_notice();
|
||||
|
||||
92
tests/test_lower_lds_after_sts.py
Normal file
92
tests/test_lower_lds_after_sts.py
Normal file
@@ -0,0 +1,92 @@
|
||||
"""A warp's shared memory store (STS) and its next load (LDS) get a subgroup barrier between them.
|
||||
|
||||
The Switch runs a warp's shared memory accesses in order, so OoT's programs store to shared memory and read it back,
|
||||
other lanes' words too, with no BAR between. Vulkan orders another lane's store and this lane's load only through a
|
||||
barrier, and the driver's compiler may move them past each other. OoT's 4/3 gameplay network (fd029a317a9a4b94) does
|
||||
that at its end (8 STS, then 16 LDS): on NVIDIA's cooperative matrix path the picture got flickering bright bars, 16 px
|
||||
wide and 2 rows tall (Hookshot issue 2455). As for LDSM: a subgroup barrier (MEMBAR, bit 20) before the first LDS after
|
||||
an STS, only where the LDS is not predicated (every lane of the warp reaches it); none after a BAR.SYNC.
|
||||
RX_VK_SHARED_NO_LDS_BARRIER=1: none, as before.
|
||||
|
||||
python -I -B tests/test_lower_lds_after_sts.py [--cxx clang++]
|
||||
"""
|
||||
import argparse
|
||||
import os
|
||||
import shutil
|
||||
import subprocess
|
||||
import tempfile
|
||||
from pathlib import Path
|
||||
|
||||
RUN_ALL_ARGS = ["--cxx", "clang++"]
|
||||
|
||||
ROOT = Path(__file__).resolve().parents[1]
|
||||
|
||||
FIXTURE = r'''#include <cstdint>
|
||||
#include <cstdio>
|
||||
#include <cstdlib>
|
||||
#include <cstring>
|
||||
#include <string>
|
||||
#include <vector>
|
||||
#include "shader_recompiler/frontend/ampere/lower_to_maxwell.h"
|
||||
#define CHECK(x) do { if (!(x)) { std::fprintf(stderr, "FAIL:%d %s\n", __LINE__, #x); std::exit(1); } } while (0)
|
||||
using Shader::Ampere::Instruction;
|
||||
static Instruction insn(uint32_t a, uint32_t b, uint32_t c, uint32_t d) { Instruction i{}; i.words[0] = a; i.words[1] = b; i.words[2] = c; i.words[3] = d; return i; }
|
||||
static const Instruction exit_insn = insn(0x0000794d, 0, 0x03800000, 0x000fea00); // EXIT
|
||||
// STS.32 [R2+0x10], R5 and LDS.32 R6, [R3+0x20] (@PT; @P0 for the predicated one); BAR.SYNC 0
|
||||
static const Instruction sts = insn(0x02ff7388, 0x00001005, 0x00000800, 0x000fe800);
|
||||
static const Instruction lds = insn(0x03067984, 0x00002000, 0x00000800, 0x000e2800);
|
||||
static const Instruction lds_p0 = insn(0x03060984, 0x00002000, 0x00000800, 0x000e2800);
|
||||
static const Instruction bar = insn(0x00007b1d, 0, 0x00010000, 0x000fec00);
|
||||
static bool subgroup_barrier(uint64_t i) { return (i & 0xfff8000000000000ULL) == 0xef98000000000000ULL && ((i >> 20) & 1); }
|
||||
static bool maxwell_lds(uint64_t i) { return (i >> 51) == (0xef48000000000000ULL >> 51); } // (the size is in 48..50)
|
||||
static bool maxwell_sts(uint64_t i) { return (i >> 51) == (0xef58000000000000ULL >> 51); }
|
||||
// The lowered program as a string: B a subgroup barrier, L an LDS, S an STS, . anything else
|
||||
static std::string shape(std::vector<Instruction> code) {
|
||||
code.push_back(exit_insn);
|
||||
const auto r = Shader::Ampere::LowerToMaxwell(code);
|
||||
CHECK(r);
|
||||
std::string s;
|
||||
for (const uint64_t i : r.instructions)
|
||||
s += subgroup_barrier(i) ? 'B' : maxwell_lds(i) ? 'L' : maxwell_sts(i) ? 'S' : '.';
|
||||
return s;
|
||||
}
|
||||
static size_t count(const std::string& s, char c) { size_t n = 0; for (char x : s) n += x == c; return n; }
|
||||
int main(int argc, char** argv) {
|
||||
const bool off = argc > 1 && !std::strcmp(argv[1], "off");
|
||||
if (off) { // RX_VK_SHARED_NO_LDS_BARRIER=1: as before, no barrier
|
||||
CHECK(count(shape({sts, lds}), 'B') == 0);
|
||||
std::printf("PASS: RX_VK_SHARED_NO_LDS_BARRIER=1 leaves the LDS after an STS without a barrier\n");
|
||||
return 0;
|
||||
}
|
||||
const std::string one = shape({sts, lds});
|
||||
CHECK(count(one, 'B') == 1);
|
||||
CHECK(one.find('S') < one.find('B') && one.find('B') < one.find('L')); // between the store and the load
|
||||
CHECK(count(shape({sts, lds, lds}), 'B') == 1); // the first load after the store only
|
||||
CHECK(count(shape({sts, lds, sts, lds}), 'B') == 2); // a store again: the next load again
|
||||
CHECK(count(shape({lds}), 'B') == 0); // no store before it
|
||||
CHECK(count(shape({sts, bar, lds}), 'B') == 0); // a BAR.SYNC orders them already
|
||||
CHECK(count(shape({sts, lds_p0}), 'B') == 0); // predicated: not every lane may reach it
|
||||
CHECK(count(shape({sts, lds_p0, lds}), 'B') == 1); // the next unpredicated load takes it
|
||||
std::printf("PASS: a subgroup barrier between a warp's STS and its next LDS\n");
|
||||
}
|
||||
'''
|
||||
|
||||
|
||||
def main():
|
||||
p = argparse.ArgumentParser(description=__doc__)
|
||||
p.add_argument("--cxx", default=shutil.which("clang++") and "clang++" or "g++")
|
||||
a = p.parse_args()
|
||||
with tempfile.TemporaryDirectory(prefix="lower-lds-after-sts-") as tmp:
|
||||
cpp = Path(tmp) / "fixture.cpp"
|
||||
exe = Path(tmp) / "fixture"
|
||||
cpp.write_text(FIXTURE, encoding="utf-8")
|
||||
subprocess.run([a.cxx, "-std=c++20", "-O1", "-Wno-everything" if "clang" in a.cxx else "-w",
|
||||
"-I" + str(ROOT / "gpu" / "vk" / "eden"), "-I" + str(ROOT / "gpu" / "vk" / "compat"),
|
||||
str(cpp), "-o", str(exe)], check=True)
|
||||
env = {k: v for k, v in os.environ.items() if k != "RX_VK_SHARED_NO_LDS_BARRIER"}
|
||||
subprocess.run([str(exe)], check=True, env=env)
|
||||
subprocess.run([str(exe), "off"], check=True, env=dict(env, RX_VK_SHARED_NO_LDS_BARRIER="1"))
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
main()
|
||||
@@ -77,7 +77,7 @@ int main() {
|
||||
CHECK(key2.size() <= kMicNameKept && whole(key2) && key2.size() == 59);
|
||||
CHECK(mic_key(nullptr).empty() && mic_key("USB Mic") == "USB Mic");
|
||||
// (a device's name with a line break would have added lines to hookshot_options.ini)
|
||||
CHECK(mic_key("Mic\noutput_res=2\r") == "Mic output_res=2 ");
|
||||
CHECK(mic_key("Mic\noutput_res=2\r") == "Mic output_res=2"); // (the line break a space, the end trimmed)
|
||||
std::printf("PASS: the menu's text cut and slid by whole UTF-8 characters\n");
|
||||
}
|
||||
'''
|
||||
|
||||
@@ -85,8 +85,9 @@ def main():
|
||||
|
||||
context = CONTEXT.read_text(encoding="utf-8")
|
||||
define = block(context, "void EmitContext::DefineMesh(")
|
||||
if "mesh_map_bytes = 4 + mesh_info.Corners() * mesh_info.max_primitives" not in define:
|
||||
raise SystemExit("the map region is not sized by the index bytes per primitive")
|
||||
# Whole blocks of 32 primitives: the program writes the index bytes of every primitive block, lanes past max_primitives too
|
||||
if "mesh_map_bytes = 4 + mesh_info.Corners() * Common::DivCeil(mesh_info.max_primitives, 32U) * 32" not in define:
|
||||
raise SystemExit("the map region is not sized by the index bytes per primitive, in whole blocks of 32 primitives")
|
||||
if not re.search(r"corners == 1\s*\?\s*spv::BuiltIn::PrimitivePointIndicesEXT", define):
|
||||
raise SystemExit("points do not output PrimitivePointIndicesEXT")
|
||||
if "TypeArray(U32[corners], primitive_count)" not in define:
|
||||
|
||||
@@ -64,7 +64,7 @@ def check_sources():
|
||||
load = block(mods, "static void code_record_load(")
|
||||
check("hos_game_path_a(path, MAX_PATH);" in load and "MOD_CODE_RECORD" in load and "mod_code_parse(text, built, BUILT_MOST)" in load,
|
||||
"the record is not read from the game folder")
|
||||
patches = block(mods, "static void code_patches(")
|
||||
patches = block(mods, "static int code_patches(")
|
||||
check('snprintf(rel, sizeof rel, "%s/exefs/%s", mod->name, fd.cFileName);' in patches and "mod_code_is_patch(fd.cFileName)" in patches
|
||||
and "mod_code_state(built, nbuilt, rel, mod_code_fnv(data, len), len, &why)" in patches,
|
||||
"a mod's patch files are not held against the record by the path the build wrote")
|
||||
@@ -78,6 +78,14 @@ def check_sources():
|
||||
"the record is not read before the mods are walked, or what is gone not said after")
|
||||
scan = block(mods, "static void mods_scan(")
|
||||
check("code_patches(&mods[i]);" in scan and "code_patches_gone" not in scan, "what is gone is only said when there is a mods folder")
|
||||
# ((Triforce, 11-18) a mod whose code waits for a build keeps its RomFS files waiting too: its new files over the old
|
||||
# code crashed the game at its start; HOS_MODS_SPLIT=1 loads them anyway)
|
||||
check("const int waits = code_patches(&mods[i]);" in scan and "const int hold = waits > 0 && !split;" in scan
|
||||
and 'const int files = hold ? 0 : index_romfs(dir, "", 0);' in scan and "HOS_MODS_SPLIT" in scan,
|
||||
"a mod whose code waits for a build still has its RomFS files read")
|
||||
check(scan.index("code_patches(&mods[i]);") < scan.index("index_romfs(dir"), "the RomFS files are read before the mod's code is known")
|
||||
check("waits += state == MOD_CODE_CHANGED || state == MOD_CODE_WAITS;" in patches and "return waits;" in patches,
|
||||
"code_patches does not say how many of a mod's patches wait")
|
||||
check("exefs patches are left out" not in mods, "host/mods.c still says exefs patches are left out")
|
||||
# (the walk both sides make: the path under the mods folder is the mod's name, exefs, the file)
|
||||
check('out.append(("%s/exefs/%s" % (name, entry), path))' in (ROOT / "nx2" / "code_mods.py").read_text(encoding="utf-8"),
|
||||
@@ -166,6 +174,7 @@ void hos_mod_cheats_add(const char *mod, const char *path) {
|
||||
}
|
||||
void hos_mod_cheats_ready(void) { printf("cheats: ready\n"); }
|
||||
int hos_prompts_romfs(const char *key, char *out, size_t cap) { (void)key, (void)out, (void)cap; return 0; }
|
||||
int hos_prompts_font_file(const char *key, char *out, size_t cap) { (void)key, (void)out, (void)cap; return 0; }
|
||||
int main(void) {
|
||||
hos_mods_init(NULL);
|
||||
char found[MAX_PATH];
|
||||
@@ -222,7 +231,12 @@ def mods_c_itself(cc, tmp, out, mods, main_id):
|
||||
check(sum("is off (its name starts with -)" in line for line in log) == 2 and sum("is off (its name starts with _)" in line for line in log) == 1,
|
||||
"the mods that are off:\n" + said)
|
||||
# (the build's walk of the same folder, nx2/code_mods.py find_mods, finds the same mods in the same order)
|
||||
walk_c = [line[len("mods: "):line.rindex(": ")] for line in log if line.endswith(" RomFS files")]
|
||||
held = " its RomFS files wait for the build too, with its code"
|
||||
walk_c = [line[len("mods: "):line.rindex(": ")] if line.endswith(" RomFS files") else line[len("mods: "):line.index(":" + held)]
|
||||
for line in log if line.endswith(" RomFS files") or held in line]
|
||||
# ((Triforce, 11-18) the mods whose code waits for a build (edited, new, retried) keep their RomFS files out until it)
|
||||
check(sorted(line[len("mods: "):line.index(":" + held)] for line in log if held in line) == ["edited", "new", "retried"],
|
||||
"the mods whose RomFS files wait with their code:\n" + said)
|
||||
walk_py = [name for name, _ in code_mods.find_mods(str(mods))]
|
||||
check(walk_c == walk_py and "a/b/c/d" in walk_py and "contents/0400486024DEC000/deep" in walk_py and len(walk_py) == 12
|
||||
and walk_py.index("A-upper") < walk_py.index("a/b/c/d") < walk_py.index("art") < walk_py.index("Zed") < walk_py.index("zz-cheats"),
|
||||
|
||||
@@ -1,14 +1,17 @@
|
||||
"""Native mouse look turns the player camera's own angles (host/mouse_native.c).
|
||||
"""Native mouse look turns the player camera's own angles and holds them there (host/mouse_native.c).
|
||||
|
||||
The file is compiled on its own against a fake guest memory that holds one player camera: its pitch limits at +0x178, its
|
||||
pitch at +0x18c and its yaw at +0x1a4. hos_mouse_camera is what the camera's update (f_80644c80, nx2/oot_patches.py) calls
|
||||
each frame. Until it has run the mouse's motion is the right stick's (hos_mouse_native gives -1); then it turns yaw and
|
||||
pitch by 0.1 degrees a count at sensitivity 1, pitch kept within the limits and yaw wrapped at 180, up and down swapped
|
||||
with the menu's Invert mouse vertical (option 8), and the controller reads leave the motion to the camera. Without the
|
||||
menu's Mouse camera (option 6), or on an object that is not a camera, it does nothing; 150 ms without the camera (the
|
||||
pause menu, another camera) give the motion back to the stick. The camera takes the mouse only in its states 3 and 4 (at
|
||||
+0x204: following Link, turned by the player): aiming in first person leaves it updating with state 0 while another
|
||||
camera draws, and the motion is then the stick's.
|
||||
The file is compiled on its own against a fake guest memory that holds a player camera: its pitch limits at +0x178, its
|
||||
pitch at +0x18c, its yaw at +0x1a4 and its state at +0x204. hos_mouse_camera is what the camera's update (f_80644c80,
|
||||
nx2/oot_patches.py) calls each frame. Until it has run the mouse's motion is the right stick's (hos_mouse_native gives
|
||||
-1); then it turns yaw and pitch by 0.1 degrees a count at sensitivity 1, pitch kept within the limits and yaw wrapped at
|
||||
180, up and down swapped with the menu's Invert mouse vertical (option 8), and the controller reads leave the motion to
|
||||
the camera. The game eases the angles back toward Link's by itself inside a frame (its follow camera): the mouse's angles
|
||||
are put back every frame, so the view does not drift, and a move adds to the mouse's angles, not to the game's. The game's
|
||||
own right stick is let through (hos_mouse_native_stick: the camera's keys, a pad): its angles become the mouse's new
|
||||
start. Without the menu's Mouse camera (option 6), or on an object that is not a camera, it does nothing. The game gives
|
||||
the camera free control only in its states 3 (it follows Link, the right stick takes it over) and 4 (the player's), and
|
||||
while no button it takes the right stick for is held (ZR, the bow's aim; L, the item wheel; R: hos_mouse_native_free);
|
||||
150 ms without the camera (the pause menu, another camera) give the motion back to the stick as well.
|
||||
python -I -B tests/test_mouse_native.py (Linux: host/posix's windows.h)
|
||||
"""
|
||||
import shutil
|
||||
@@ -52,10 +55,14 @@ int rx_option(int id, int otherwise) { return id == 8 ? menu_invert : id == 6 ?
|
||||
int hos_mouse_take(int32_t *dx, int32_t *dy) { *dx = move_x, *dy = move_y; move_x = move_y = 0; return captured; }
|
||||
void hos_mouse_give_back(int32_t dx, int32_t dy) { move_x += dx, move_y += dy; }
|
||||
int hos_mouse_native(void);
|
||||
void hos_mouse_native_free(int free);
|
||||
void hos_mouse_native_stick(int held);
|
||||
void hos_mouse_camera(uint64_t camera);
|
||||
static const uint64_t CAM = HOS_HEAP_BASE + 10 * 4096 + 0x40, OTHER = HOS_HEAP_BASE + 20 * 4096;
|
||||
static const uint64_t CAM = HOS_HEAP_BASE + 10 * 4096 + 0x40, CAM2 = HOS_HEAP_BASE + 30 * 4096 + 0x40,
|
||||
OTHER = HOS_HEAP_BASE + 20 * 4096;
|
||||
static float getf(uint64_t a) { float f; memcpy(&f, GP(a), 4); return f; }
|
||||
static void putf(uint64_t a, float f) { memcpy(GP(a), &f, 4); }
|
||||
static void puti(uint64_t a, int32_t v) { memcpy(GP(a), &v, 4); }
|
||||
static int fails;
|
||||
#define PITCH (CAM + 0x18c)
|
||||
#define YAW (CAM + 0x1a4)
|
||||
@@ -63,9 +70,13 @@ static void state(uint32_t v) { memcpy(GP(CAM + 0x204), &v, 4); }
|
||||
#define CHECK(c) do { if (!(c)) { printf("FAIL line %d: %s (pitch %g yaw %g)\n", __LINE__, #c, getf(PITCH), getf(YAW)); fails++; } } while (0)
|
||||
static int near(float a, float b) { return fabsf(a - b) < 1e-3f; }
|
||||
static void frame(int32_t dx, int32_t dy) { move_x += dx, move_y += dy; clock_ns += 16666667; hos_mouse_camera(CAM); }
|
||||
/* (the camera's angles become the mouse's start: a stick turn of the game's own, or a camera put somewhere by hand) */
|
||||
static void seed(void) { hos_mouse_native_stick(1); frame(0, 0); hos_mouse_native_stick(0); }
|
||||
static void seed_at(float pitch, float yaw) { putf(PITCH, pitch), putf(YAW, yaw); seed(); }
|
||||
int main(void) {
|
||||
fake_heap = (uint8_t *)calloc(64, 4096);
|
||||
putf(CAM + 0x178, -85), putf(CAM + 0x17c, 89);
|
||||
puti(CAM + 0x204, 3);
|
||||
putf(PITCH, 10), putf(YAW, 30);
|
||||
state(4); /* turned by the player */
|
||||
move_x = 5, move_y = 3;
|
||||
@@ -75,19 +86,43 @@ int main(void) {
|
||||
CHECK(near(getf(YAW), 29.0f) && near(getf(PITCH), 10.4f)); /* right: yaw falls; down: pitch rises */
|
||||
move_x = 3;
|
||||
CHECK(hos_mouse_native() == 1 && move_x == 3); /* the camera's now: the motion stays for it */
|
||||
frame(-3, 0);
|
||||
move_x = 0;
|
||||
/* the game's follow camera inside a frame: it eases the pitch and turns the yaw by itself; the mouse's angles are put back */
|
||||
putf(YAW, 12), putf(PITCH, 4);
|
||||
frame(0, 0);
|
||||
CHECK(near(getf(YAW), 29.0f) && near(getf(PITCH), 10.4f)); /* no drift back toward Link */
|
||||
putf(YAW, -40), putf(PITCH, 40);
|
||||
frame(4, 0);
|
||||
CHECK(near(getf(YAW), 28.6f) && near(getf(PITCH), 10.4f)); /* a move adds to the mouse's angles, not the game's */
|
||||
frame(0, 100000);
|
||||
CHECK(near(getf(PITCH), 89.0f)); /* kept within the limits */
|
||||
seed_at(0, 0);
|
||||
frame(0, -100000);
|
||||
CHECK(near(getf(PITCH), -85.0f));
|
||||
putf(YAW, -179.5f);
|
||||
seed_at(0, -179.5f);
|
||||
frame(10, 0);
|
||||
CHECK(near(getf(YAW), 179.5f)); /* wrapped */
|
||||
putf(PITCH, 0);
|
||||
seed_at(0, 0);
|
||||
menu_invert = 1; /* the menu's Invert mouse vertical: down tilts up */
|
||||
frame(0, 10);
|
||||
CHECK(near(getf(PITCH), -1.0f));
|
||||
menu_invert = 0;
|
||||
/* the game's own right stick (the camera's keys, a pad): its angles are the mouse's new start, and it still turns */
|
||||
putf(YAW, 70), putf(PITCH, 30);
|
||||
seed();
|
||||
CHECK(near(getf(YAW), 70.0f) && near(getf(PITCH), 30.0f));
|
||||
frame(-10, 0);
|
||||
CHECK(near(getf(YAW), 71.0f));
|
||||
/* another camera: its own angles are the start, not the last camera's */
|
||||
putf(CAM2 + 0x178, -85), putf(CAM2 + 0x17c, 89), puti(CAM2 + 0x204, 3);
|
||||
putf(CAM2 + 0x18c, -20), putf(CAM2 + 0x1a4, 100);
|
||||
move_x = 0;
|
||||
clock_ns += 16666667;
|
||||
hos_mouse_camera(CAM2);
|
||||
CHECK(near(getf(CAM2 + 0x18c), -20.0f) && near(getf(CAM2 + 0x1a4), 100.0f));
|
||||
clock_ns += 16666667;
|
||||
hos_mouse_camera(CAM2);
|
||||
CHECK(near(getf(CAM2 + 0x18c), -20.0f) && near(getf(CAM2 + 0x1a4), 100.0f));
|
||||
putf(OTHER + 0x18c, NAN); /* (no limits, no angles: not a camera) */
|
||||
move_x = 50;
|
||||
hos_mouse_camera(OTHER);
|
||||
@@ -97,27 +132,47 @@ int main(void) {
|
||||
putf(YAW, 0);
|
||||
frame(10, 0);
|
||||
CHECK(near(getf(YAW), 0.0f));
|
||||
move_x = 0;
|
||||
menu_native = 1;
|
||||
frame(0, 0);
|
||||
seed_at(10, 30);
|
||||
/* the game's own state: only 3 (it follows Link) and 4 (the player's) turn with the mouse */
|
||||
puti(CAM + 0x204, 1);
|
||||
putf(YAW, 0);
|
||||
move_x = 10;
|
||||
hos_mouse_camera(CAM);
|
||||
CHECK(move_x == 10 && near(getf(YAW), 0.0f)); /* (a lock-on, a menu, first person: the right stick's) */
|
||||
CHECK(hos_mouse_native() == -1);
|
||||
move_x = 0;
|
||||
puti(CAM + 0x204, 3);
|
||||
/* a button the game takes the stick for (ZR, the bow's aim; L, the item wheel; R): the game's camera too */
|
||||
hos_mouse_native_free(0);
|
||||
putf(YAW, 5);
|
||||
move_x = 10;
|
||||
hos_mouse_camera(CAM);
|
||||
CHECK(move_x == 10 && near(getf(YAW), 5.0f));
|
||||
CHECK(hos_mouse_native() == -1);
|
||||
hos_mouse_native_free(1);
|
||||
move_x = 0;
|
||||
captured = 0;
|
||||
CHECK(hos_mouse_native() == 0); /* not captured: nothing */
|
||||
captured = 1;
|
||||
clock_ns += 200000000LL; /* no camera for 200 ms (the pause menu): the stick again */
|
||||
CHECK(hos_mouse_native() == -1);
|
||||
/* aiming in first person (the slingshot): the camera keeps updating, state 0, another draws: the stick takes it */
|
||||
state(4);
|
||||
frame(0, 0); /* the camera's a moment ago (the hook ran: hos_mouse_native would give 1 for 150 ms) */
|
||||
state(0);
|
||||
putf(YAW, 0);
|
||||
frame(10, 0);
|
||||
CHECK(near(getf(YAW), 0.0f) && move_x == 10);
|
||||
CHECK(hos_mouse_native() == -1); /* at once, not 150 ms later */
|
||||
move_x = 0;
|
||||
state(3); /* following Link again: the mouse turns it */
|
||||
frame(10, 0);
|
||||
CHECK(near(getf(YAW), -1.0f));
|
||||
CHECK(hos_mouse_native() == 1);
|
||||
for (uint32_t resumed = 3; resumed <= 4; resumed++) {
|
||||
state(4);
|
||||
frame(0, 0); /* the camera's a moment ago (the hook ran: hos_mouse_native would give 1 for 150 ms) */
|
||||
state(0);
|
||||
putf(YAW, 0), putf(PITCH, 20);
|
||||
frame(10, 5);
|
||||
CHECK(near(getf(YAW), 0.0f) && near(getf(PITCH), 20.0f) && move_x == 10 && move_y == 5);
|
||||
CHECK(hos_mouse_native() == -1); /* at once, not 150 ms later */
|
||||
move_x = move_y = 0;
|
||||
state(resumed); /* following Link or turned by the player: start at the game's current angles */
|
||||
frame(10, 5);
|
||||
CHECK(near(getf(YAW), -1.0f));
|
||||
CHECK(near(getf(PITCH), 20.5f));
|
||||
CHECK(hos_mouse_native() == 1);
|
||||
}
|
||||
if (!fails) printf("PASS\n");
|
||||
return fails != 0;
|
||||
}
|
||||
|
||||
302
tests/test_oot_heap_retry.py
Normal file
302
tests/test_oot_heap_retry.py
Normal file
@@ -0,0 +1,302 @@
|
||||
"""The OptimizeChangeBank thread's allocations are tried again when their heap is full (host/oot_heap_retry.c, nx2/oot_patches.py).
|
||||
|
||||
Issue 1697 (thread OptimizeChangeBanc writes guest 0x8 a few frames after loading a save) and TomShafford/Hookshot's crash at
|
||||
backflip + attack: a model's CPU buffer (a heap's tryAlloc) or GPU memory (f_80522050) came back null from a full heap and the
|
||||
game wrote through it. Builds host/oot_heap_retry.c, as it is, over a guest memory of its own with made-up sead heaps (name,
|
||||
size, parent, a vtable with tryAlloc and getFreeSize) and a stand-in for the game's f_80522050, then checks:
|
||||
|
||||
- an allocation that succeeds is the game's alone: one call, nothing logged, nothing changed;
|
||||
- a GPU allocation that fails is tried again in each parent with room, then in the game's default heap (no waits:
|
||||
it never sleeps); the request names its own heap again afterwards; nothing with room: it fails as before;
|
||||
- the CPU buffer is tried in each parent with room, returned in x0, the other registers and the flags as they were;
|
||||
- HOS_GPU_ALLOC_RETRY=0: the game's call alone, as shipped; HOS_GPU_ALLOC_TEST_FAIL=<n>: the first n refused by the named
|
||||
heap without calling it (nothing leaks), and the retry makes them;
|
||||
- nx2/oot_patches.py makes both edits on lifted text shaped as the lift writes it, once, and a second run changes nothing.
|
||||
|
||||
python -I -B tests/test_oot_heap_retry.py [--cc gcc]
|
||||
"""
|
||||
import argparse
|
||||
import os
|
||||
import shutil
|
||||
import subprocess
|
||||
import sys
|
||||
import tempfile
|
||||
from pathlib import Path
|
||||
|
||||
RUN_ALL_ARGS = ["--cc", "gcc"]
|
||||
|
||||
ROOT = Path(__file__).resolve().parents[1]
|
||||
sys.path.insert(0, str(ROOT / "nx2"))
|
||||
import oot_patches # noqa: E402
|
||||
|
||||
HOS_H = r'''#pragma once
|
||||
#include <stdint.h>
|
||||
#include <string.h>
|
||||
typedef struct { uint64_t x[32]; uint8_t n, z, c, v; } Ctx;
|
||||
typedef void (*A64Fn)(Ctx *);
|
||||
#define GUEST 0x80000000ull
|
||||
extern unsigned char mem[0x100000];
|
||||
static inline uint64_t recomp_load8(Ctx *c, uint64_t a) { (void)c; return mem[a - GUEST]; }
|
||||
static inline uint64_t recomp_load64(Ctx *c, uint64_t a) { uint64_t v; (void)c; memcpy(&v, mem + (a - GUEST), 8); return v; }
|
||||
static inline void recomp_store64(Ctx *c, uint64_t a, uint64_t v) { (void)c; memcpy(mem + (a - GUEST), &v, 8); }
|
||||
A64Fn a64_lookup(uint64_t a);
|
||||
void a64_icall(Ctx *c, uint64_t a);
|
||||
void hos_log(const char *fmt, ...);
|
||||
void hos_sleep_ns(int64_t ns);
|
||||
'''
|
||||
|
||||
FIXTURE = r'''#include "hos.h"
|
||||
#include <stdarg.h>
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#define CHECK(x) do { if (!(x)) { fprintf(stderr, "FAIL:%d %s\n", __LINE__, #x); exit(1); } } while (0)
|
||||
unsigned char mem[0x100000];
|
||||
static int logs, sleeps, game_calls;
|
||||
void hos_log(const char *fmt, ...) { va_list a; va_start(a, fmt); vprintf(fmt, a); va_end(a); putchar('\n'); logs++; }
|
||||
void hos_sleep_ns(int64_t ns) { (void)ns; sleeps++; }
|
||||
|
||||
/* heaps: at H(i), +0 the vtable, +0x20 its name, +0x30 its size, +0x38 its parent; the room left in room[i] */
|
||||
#define VT 0x80001000ull
|
||||
#define FN_TRY 0x80002000ull
|
||||
#define FN_FREE 0x80002100ull
|
||||
#define H(i) (0x80010000ull + 0x100ull * (i))
|
||||
#define NAME(i) (0x80020000ull + 0x40ull * (i))
|
||||
static uint64_t room[8], next_block = 0x80080000ull, free_after_calls[8];
|
||||
static uint64_t heap_index(uint64_t h) { return (h - H(0)) / 0x100; }
|
||||
static void try_alloc(Ctx *c) { /* tryAlloc(heap, size, align): the block or 0 */
|
||||
const uint64_t i = heap_index(c->x[0]), size = c->x[1];
|
||||
c->x[1] = c->x[2] = c->x[3] = 0xdead; /* (a callee's scratch registers) */
|
||||
c->n = c->z = c->c = c->v = 1;
|
||||
if (room[i] < size) { c->x[0] = 0; return; }
|
||||
room[i] -= size;
|
||||
c->x[0] = next_block, next_block += (size + 15) & ~15ull;
|
||||
}
|
||||
static void get_free(Ctx *c) { c->x[0] = room[heap_index(c->x[0])]; c->x[5] = 0xdead; }
|
||||
A64Fn a64_lookup(uint64_t a) { return a == FN_TRY ? try_alloc : a == FN_FREE ? get_free : NULL; }
|
||||
void a64_icall(Ctx *c, uint64_t a) { a64_lookup(a)(c); }
|
||||
static void make_heap(int i, int parent, uint64_t free_room, const char *name) {
|
||||
recomp_store64(NULL, H(i), VT);
|
||||
strcpy((char *)mem + (NAME(i) - GUEST), name);
|
||||
recomp_store64(NULL, H(i) + 0x20, NAME(i));
|
||||
recomp_store64(NULL, H(i) + 0x30, 0x100000);
|
||||
recomp_store64(NULL, H(i) + 0x38, parent < 0 ? 0 : H(parent));
|
||||
room[i] = free_room;
|
||||
}
|
||||
|
||||
/* the game's f_80522050: x1 the request (+0 count, +8 the heap, 0: the default, heap 3), x2 the size of one; x0 bit 0 */
|
||||
static uint64_t default_heap = H(3);
|
||||
static void game_gpu_alloc(Ctx *c) {
|
||||
game_calls++;
|
||||
uint64_t heap = recomp_load64(c, c->x[1] + 8);
|
||||
if (!heap) heap = default_heap;
|
||||
const uint64_t i = heap_index(heap), size = recomp_load64(c, c->x[1]) * c->x[2];
|
||||
if (free_after_calls[i] && game_calls >= (int)free_after_calls[i]) room[i] += size; /* (a free landed) */
|
||||
c->x[0] = room[i] >= size ? (room[i] -= size, 1) : 0;
|
||||
c->x[9] = 0xdead;
|
||||
}
|
||||
void hos_oot_gpu_alloc(Ctx *c, void (*game)(Ctx *));
|
||||
int hos_oot_heap_refuse(void);
|
||||
void hos_oot_heap_retry(Ctx *c, uint64_t heap, uint64_t size, uint64_t align, uint64_t lr);
|
||||
|
||||
#define REQ 0x80030000ull
|
||||
static void request(Ctx *c, uint64_t heap, uint64_t count) {
|
||||
memset(c, 0, sizeof *c);
|
||||
recomp_store64(c, REQ, count);
|
||||
recomp_store64(c, REQ + 8, heap);
|
||||
recomp_store64(c, REQ + 0x20, NAME(7));
|
||||
c->x[0] = 0x80040000ull, c->x[1] = REQ, c->x[2] = 0x100, c->x[30] = 0x80456f1cull;
|
||||
game_calls = logs = sleeps = 0;
|
||||
}
|
||||
static void reset(void) {
|
||||
memset(free_after_calls, 0, sizeof free_after_calls);
|
||||
make_heap(0, 1, 0, "OptimizeChangeBank"); /* (the named heap; its parent 1, whose parent is the root 2) */
|
||||
make_heap(1, 2, 0, "Model");
|
||||
make_heap(2, -1, 0, "Root");
|
||||
make_heap(3, -1, 0, "Default");
|
||||
}
|
||||
int main(int argc, char **argv) {
|
||||
Ctx c;
|
||||
recomp_store64(NULL, VT + 0x30, FN_TRY);
|
||||
recomp_store64(NULL, VT + 0x80, FN_FREE);
|
||||
strcpy((char *)mem + (NAME(7) - GUEST), "gsys::ModelResource(MatBlock)");
|
||||
const char *mode = argc > 1 ? argv[1] : "";
|
||||
|
||||
if (!strcmp(mode, "off")) { /* HOS_GPU_ALLOC_RETRY=0: the game's call alone */
|
||||
reset(), room[1] = 1 << 20;
|
||||
request(&c, H(0), 4);
|
||||
hos_oot_gpu_alloc(&c, game_gpu_alloc);
|
||||
CHECK(!(c.x[0] & 1) && game_calls == 1 && !logs);
|
||||
c.x[0] = 0;
|
||||
hos_oot_heap_retry(&c, H(0), 0x4198, 8, 0x80456910ull);
|
||||
CHECK(c.x[0] == 0 && room[1] == 1 << 20);
|
||||
CHECK(!hos_oot_heap_refuse());
|
||||
puts("PASS: HOS_GPU_ALLOC_RETRY=0: the game's own call alone");
|
||||
return 0;
|
||||
}
|
||||
if (!strcmp(mode, "off-refuse")) { /* HOS_GPU_ALLOC_RETRY=0 HOS_GPU_ALLOC_TEST_FAIL=1: the full heap's failure, as shipped */
|
||||
reset(), room[0] = room[1] = 1 << 20;
|
||||
request(&c, H(0), 4);
|
||||
hos_oot_gpu_alloc(&c, game_gpu_alloc);
|
||||
CHECK(!(c.x[0] & 1) && game_calls == 0 && room[0] == 1 << 20 && room[1] == 1 << 20);
|
||||
CHECK(hos_oot_heap_refuse() && !hos_oot_heap_refuse());
|
||||
c.x[0] = 0;
|
||||
hos_oot_heap_retry(&c, H(0), 0x4198, 8, 0x80456910ull);
|
||||
CHECK(c.x[0] == 0 && room[1] == 1 << 20);
|
||||
puts("PASS: HOS_GPU_ALLOC_RETRY=0 with HOS_GPU_ALLOC_TEST_FAIL=1: the failure the game crashes on, not tried again");
|
||||
return 0;
|
||||
}
|
||||
if (!strcmp(mode, "refuse")) { /* HOS_GPU_ALLOC_TEST_FAIL=2 */
|
||||
reset(), room[0] = room[1] = 1 << 20;
|
||||
for (int k = 0; k < 3; k++) {
|
||||
request(&c, H(0), 4);
|
||||
const uint64_t named = room[0], parent = room[1];
|
||||
hos_oot_gpu_alloc(&c, game_gpu_alloc);
|
||||
CHECK(c.x[0] & 1);
|
||||
if (k < 2) CHECK(room[0] == named && room[1] == parent - 0x400 && !sleeps); /* (refused: made in the parent) */
|
||||
else CHECK(room[0] == named - 0x400 && game_calls == 1); /* (then the game's own again) */
|
||||
}
|
||||
CHECK(hos_oot_heap_refuse() && hos_oot_heap_refuse() && !hos_oot_heap_refuse());
|
||||
puts("PASS: HOS_GPU_ALLOC_TEST_FAIL=2: two refused by their heap without a call, made by the retry, then as shipped");
|
||||
return 0;
|
||||
}
|
||||
if (!strcmp(mode, "split")) { /* HOS_GPU_ALLOC_TEST_FAIL=0,1: the model buffer alone */
|
||||
reset(), room[0] = 1 << 20;
|
||||
request(&c, H(0), 4);
|
||||
hos_oot_gpu_alloc(&c, game_gpu_alloc);
|
||||
CHECK((c.x[0] & 1) && game_calls == 1 && !logs);
|
||||
CHECK(hos_oot_heap_refuse() && !hos_oot_heap_refuse());
|
||||
puts("PASS: HOS_GPU_ALLOC_TEST_FAIL=0,1: the GPU memory as shipped, one model buffer refused");
|
||||
return 0;
|
||||
}
|
||||
|
||||
/* made at once: the game's alone */
|
||||
reset(), room[0] = 1 << 20;
|
||||
request(&c, H(0), 4);
|
||||
hos_oot_gpu_alloc(&c, game_gpu_alloc);
|
||||
CHECK((c.x[0] & 1) && game_calls == 1 && !logs && !sleeps && recomp_load64(&c, REQ + 8) == H(0));
|
||||
|
||||
/* full, its parent with room: made there at once, no sleep */
|
||||
reset(), room[1] = 1 << 20;
|
||||
request(&c, H(0), 4);
|
||||
hos_oot_gpu_alloc(&c, game_gpu_alloc);
|
||||
CHECK((c.x[0] & 1) && game_calls == 2 && !sleeps && room[1] == (1 << 20) - 0x400 && recomp_load64(&c, REQ + 8) == H(0));
|
||||
|
||||
/* full, its parent too, the root has room: made there; the request names its heap again */
|
||||
reset(), room[2] = 1 << 20;
|
||||
request(&c, H(0), 4);
|
||||
hos_oot_gpu_alloc(&c, game_gpu_alloc);
|
||||
CHECK((c.x[0] & 1) && room[2] == (1 << 20) - 0x400 && recomp_load64(&c, REQ + 8) == H(0));
|
||||
CHECK(c.x[30] == 0x80456f1cull);
|
||||
|
||||
/* no parent with room: the game's default heap */
|
||||
reset(), room[3] = 1 << 20;
|
||||
request(&c, H(0), 4);
|
||||
hos_oot_gpu_alloc(&c, game_gpu_alloc);
|
||||
CHECK((c.x[0] & 1) && room[3] == (1 << 20) - 0x400 && recomp_load64(&c, REQ + 8) == H(0));
|
||||
|
||||
/* nothing has room: it fails, as before, and says so */
|
||||
reset();
|
||||
request(&c, H(0), 4);
|
||||
hos_oot_gpu_alloc(&c, game_gpu_alloc);
|
||||
CHECK(!(c.x[0] & 1) && logs >= 3 && recomp_load64(&c, REQ + 8) == H(0));
|
||||
CHECK(!sleeps);
|
||||
puts("PASS: GPU memory from a full heap: in a parent with room, in the default heap, or not at all; never a sleep");
|
||||
|
||||
/* the CPU buffer: in the nearest parent with room; x0 the block, everything else as it was */
|
||||
reset(), room[2] = 1 << 20;
|
||||
memset(&c, 0, sizeof c);
|
||||
for (int r = 1; r < 31; r++) c.x[r] = 0x1000 + r;
|
||||
c.n = 1, c.z = 0, c.c = 1, c.v = 0;
|
||||
Ctx before = c;
|
||||
hos_oot_heap_retry(&c, H(0), 0x4198, 8, 0x80456910ull);
|
||||
CHECK(c.x[0] >= 0x80080000ull && room[2] == (1 << 20) - 0x4198);
|
||||
for (int r = 1; r < 31; r++) CHECK(c.x[r] == before.x[r]);
|
||||
CHECK(c.n == 1 && c.z == 0 && c.c == 1 && c.v == 0);
|
||||
reset(), room[1] = 0x4198 + 0x1000; /* (a parent without the margin is passed over) */
|
||||
c = before;
|
||||
hos_oot_heap_retry(&c, H(0), 0x4198, 8, 0x80456910ull);
|
||||
CHECK(c.x[0] == 0 && room[1] == 0x4198 + 0x1000);
|
||||
reset(), room[1] = 1 << 20;
|
||||
c = before;
|
||||
hos_oot_heap_retry(&c, H(0), 0x4198, 8, 0x80456910ull);
|
||||
CHECK(c.x[0] && room[1] == (1 << 20) - 0x4198);
|
||||
reset(), room[1] = 1 << 20; /* (the second site, the +168 buffer, the same way) */
|
||||
c = before;
|
||||
hos_oot_heap_retry(&c, H(0), 0x800, 8, 0x80456da4ull);
|
||||
CHECK(c.x[0] && room[1] == (1 << 20) - 0x800);
|
||||
CHECK(!hos_oot_heap_refuse());
|
||||
puts("PASS: the model buffer from a full heap: made in its nearest parent with room, x0 alone changed");
|
||||
return 0;
|
||||
}
|
||||
'''
|
||||
|
||||
# (lifted text shaped as the lift writes it: the four places' exact lines, and another caller of f_80522050)
|
||||
LIFTED = '''#include "a64.h"
|
||||
void f_80522050(Ctx *A64_NOESC);
|
||||
void f_804566e0(Ctx *__restrict A64_NOESC c) {
|
||||
c->x[8]=recomp_load64(c,c->x[8] + 48);
|
||||
{ uint64_t _t = c->x[8]; c->x[30] = 0x80456910ULL; A64_ICALL(c, _t); }
|
||||
recomp_store64_sp(c,c->x[27] + 200,c->x[0]);
|
||||
{ uint64_t _t = c->x[8]; c->x[30] = 0x80456da4ULL; A64_ICALL(c, _t); }
|
||||
recomp_store64_sp(c,c->x[27] + 168,c->x[0]);
|
||||
c->x[30] = 0x80456f1cULL; f_80522050(c);
|
||||
c->x[30] = 0x80457098ULL; f_80522050(c);
|
||||
}
|
||||
void f_80dfe000(Ctx *__restrict A64_NOESC c) {
|
||||
c->x[30] = 0x80dfe6a4ULL; f_80522050(c);
|
||||
}
|
||||
void f_80522050(Ctx *__restrict A64_NOESC c) {
|
||||
c->x[0] = 0x1ULL;
|
||||
return;
|
||||
}
|
||||
'''
|
||||
|
||||
|
||||
def main():
|
||||
p = argparse.ArgumentParser(description=__doc__)
|
||||
p.add_argument("--cc", default="gcc")
|
||||
a = p.parse_args()
|
||||
|
||||
out, found = oot_patches.patch_heap_retry(LIFTED)
|
||||
assert found == [2, 2], found
|
||||
assert out.count("hos_oot_gpu_alloc(c, f_80522050)") == 2
|
||||
assert "c->x[30] = 0x80dfe6a4ULL; f_80522050(c);" in out, "another caller of f_80522050 was changed"
|
||||
assert "void f_80522050(Ctx *__restrict A64_NOESC c) {" in out, "f_80522050 itself was changed"
|
||||
for lr in ("0x80456910ULL", "0x80456da4ULL"):
|
||||
assert out.count("hos_oot_heap_retry(c, _hh, _hn, _ha, %s)" % lr) == 1, lr
|
||||
assert out.count(oot_patches.GPU_ALLOC_DECL) == 1 and out.count(oot_patches.HEAP_ALLOC_DECL) == 1
|
||||
assert oot_patches.patch_heap_retry(out) == (out, [2, 2]), "a second run changed the text"
|
||||
other = LIFTED.replace("0x80456910ULL", "0x80456914ULL").replace("0x80456da4ULL", "0x80456da8ULL") \
|
||||
.replace("0x80456f1cULL", "0x80456f20ULL").replace("0x80457098ULL", "0x8045709cULL")
|
||||
assert oot_patches.patch_heap_retry(other) == (other, [0, 0]), "another version's text was edited"
|
||||
import inspect
|
||||
assert "patch_heap_retry(out) if protect_hearts else" in inspect.getsource(oot_patches.apply), \
|
||||
"the edits are made on builds other than the supported one"
|
||||
print("PASS: nx2/oot_patches.py: the four places edited once (f_80522050's other callers left), a second run changes "
|
||||
"nothing, other text is left alone, the supported build only")
|
||||
|
||||
cc = shutil.which(a.cc) or shutil.which("gcc") or shutil.which("clang")
|
||||
if not cc:
|
||||
print("SKIP: no C compiler")
|
||||
return
|
||||
with tempfile.TemporaryDirectory(prefix="heap-retry-") as tmp:
|
||||
tmp = Path(tmp)
|
||||
(tmp / "hos.h").write_text(HOS_H, encoding="utf-8")
|
||||
(tmp / "fixture.c").write_text(FIXTURE, encoding="utf-8")
|
||||
(tmp / "oot_heap_retry.c").write_text((ROOT / "host" / "oot_heap_retry.c").read_text(encoding="utf-8"),
|
||||
encoding="utf-8")
|
||||
exe = tmp / ("fixture.exe" if os.name == "nt" else "fixture")
|
||||
subprocess.run([cc, "-std=gnu11", "-O1", "-Wall", "-Wextra", "-Werror", "-I", str(tmp), str(tmp / "fixture.c"),
|
||||
str(tmp / "oot_heap_retry.c"), "-o", str(exe)], check=True)
|
||||
env = {k: v for k, v in os.environ.items() if not k.startswith("HOS_GPU_ALLOC")}
|
||||
for mode, extra in (("", {}), ("off", {"HOS_GPU_ALLOC_RETRY": "0"}), ("refuse", {"HOS_GPU_ALLOC_TEST_FAIL": "2"}),
|
||||
("off-refuse", {"HOS_GPU_ALLOC_RETRY": "0", "HOS_GPU_ALLOC_TEST_FAIL": "1"}),
|
||||
("split", {"HOS_GPU_ALLOC_TEST_FAIL": "0,1"})):
|
||||
r = subprocess.run([str(exe), mode], env=dict(env, **extra), capture_output=True, text=True)
|
||||
print("\n".join(line for line in r.stdout.splitlines() if line.startswith("PASS")))
|
||||
if r.returncode:
|
||||
raise SystemExit("FAIL (%s):\n%s%s" % (mode or "retry", r.stdout, r.stderr))
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
main()
|
||||
40
tests/test_oot_menu_linux_half.py
Normal file
40
tests/test_oot_menu_linux_half.py
Normal file
@@ -0,0 +1,40 @@
|
||||
"""host/oot_menu.c's two halves: what other host files call is defined in both.
|
||||
|
||||
The file is one half on Windows (#if defined(_WIN32)) and another elsewhere (#else). A function the Windows half added for the
|
||||
rest of the host (v0.5.0's hos_oot_mods_down, nn_hid.c) and not the other half stopped every Linux build at the link (11-18).
|
||||
This test lists the non-static functions each half defines and the ones host/*.c outside it call, and checks that every one
|
||||
called from outside is in the non-Windows half too.
|
||||
"""
|
||||
|
||||
import re
|
||||
import unittest
|
||||
from pathlib import Path
|
||||
|
||||
ROOT = Path(__file__).resolve().parents[1]
|
||||
SRC = ROOT / "host" / "oot_menu.c"
|
||||
DEF = re.compile(r"^(?!static\b)[A-Za-z_][\w \t\*]*?\b(hos_\w+)\s*\([^;]*\)\s*\{", re.M)
|
||||
|
||||
|
||||
def halves(text):
|
||||
start = text.index("#if defined(_WIN32)")
|
||||
other = text.index("\n#else", start)
|
||||
end = text.index("\n#endif", other)
|
||||
return text[start:other], text[other:end]
|
||||
|
||||
|
||||
class OotMenuHalvesTest(unittest.TestCase):
|
||||
def test_called_from_outside_defined_in_both(self):
|
||||
text = SRC.read_text(encoding="utf-8")
|
||||
win, other = halves(text)
|
||||
win_defs, other_defs = set(DEF.findall(win)), set(DEF.findall(other))
|
||||
self.assertIn("hos_oot_menu_tick", other_defs) # (the parse found the other half's functions)
|
||||
outside = "\n".join(p.read_text(encoding="utf-8", errors="replace")
|
||||
for p in sorted((ROOT / "host").glob("*.c")) if p.name != "oot_menu.c")
|
||||
called = {name for name in win_defs if re.search(r"\b%s\s*\(" % name, outside)}
|
||||
self.assertIn("hos_oot_mods_down", called)
|
||||
missing = sorted(called - other_defs)
|
||||
self.assertEqual(missing, [], "defined only in oot_menu.c's Windows half, but called from other host files: %s" % missing)
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
raise SystemExit(unittest.main(verbosity=1))
|
||||
76
tests/test_packed_romfs.py
Normal file
76
tests/test_packed_romfs.py
Normal file
@@ -0,0 +1,76 @@
|
||||
"""NX2navi's packed RomFS is refused (nx2/make_game.py refuse_packed_romfs, setup/Shell.cs CheckSource).
|
||||
|
||||
NX2navi keeps the game's RomFS as one image, <its game folder>\\romfs\\romfs.bin (+ index.json). Chosen as Setup's romfs folder it
|
||||
passed (only the exefs was checked), the game was built without its data and crashed at start (two players, 11-18). Now:
|
||||
- a romfs folder holding romfs.bin is refused, with a message that names NX2navi and what to choose instead;
|
||||
- a folder of the game's files is not;
|
||||
- the check, the build from folders and an update each refuse it, and Setup's check passes the romfs folder.
|
||||
"""
|
||||
|
||||
import importlib.util
|
||||
import io
|
||||
import os
|
||||
import sys
|
||||
import tempfile
|
||||
import unittest
|
||||
from contextlib import redirect_stdout
|
||||
from pathlib import Path
|
||||
|
||||
ROOT = Path(__file__).resolve().parents[1]
|
||||
|
||||
|
||||
def load_make_game():
|
||||
sys.path.insert(0, str(ROOT / "nx2"))
|
||||
try:
|
||||
spec = importlib.util.spec_from_file_location("make_game_packed_romfs", ROOT / "nx2" / "make_game.py")
|
||||
module = importlib.util.module_from_spec(spec)
|
||||
spec.loader.exec_module(module)
|
||||
return module
|
||||
finally:
|
||||
sys.path.remove(str(ROOT / "nx2"))
|
||||
|
||||
|
||||
mg = load_make_game()
|
||||
|
||||
|
||||
class PackedRomfsTest(unittest.TestCase):
|
||||
def setUp(self):
|
||||
self.tmp = tempfile.TemporaryDirectory(prefix="packed-romfs-")
|
||||
self.dir = Path(self.tmp.name)
|
||||
|
||||
def tearDown(self):
|
||||
self.tmp.cleanup()
|
||||
|
||||
def test_nx2navi_folder_refused(self):
|
||||
romfs = self.dir / "NX2navi" / "romfs"
|
||||
romfs.mkdir(parents=True)
|
||||
(romfs / "romfs.bin").write_bytes(b"\0" * 64)
|
||||
(romfs / "index.json").write_text("{}")
|
||||
out = io.StringIO()
|
||||
with redirect_stdout(out), self.assertRaises(SystemExit) as e:
|
||||
mg.refuse_packed_romfs(str(romfs))
|
||||
self.assertEqual(e.exception.code, 1)
|
||||
line = out.getvalue().strip()
|
||||
self.assertTrue(line.startswith("@@error "), line) # (what Setup shows under the game files: Setup.cs Check)
|
||||
self.assertIn("NX2navi", line)
|
||||
self.assertIn("romfs.bin", line)
|
||||
self.assertIn(".nup", line)
|
||||
|
||||
def test_unpacked_folder_passes(self):
|
||||
romfs = self.dir / "romfs"
|
||||
(romfs / "System").mkdir(parents=True)
|
||||
(romfs / "System" / "AocMainField.byml").write_bytes(b"x")
|
||||
mg.refuse_packed_romfs(str(romfs)) # (no exit)
|
||||
mg.refuse_packed_romfs(None) # (the .nup route: no folder)
|
||||
|
||||
def test_every_route_refuses(self):
|
||||
src = (ROOT / "nx2" / "make_game.py").read_text(encoding="utf-8")
|
||||
self.assertIn("refuse_packed_romfs(a.romfs) # (Setup passes", src) # the check
|
||||
self.assertIn("refuse_packed_romfs(romfs) # (a folder built from NX2navi's", src) # an update
|
||||
self.assertIn("refuse_packed_romfs(a.romfs)\n refuse_dump_links(a.exefs)", src) # the build from folders
|
||||
shell = (ROOT / "setup" / "Shell.cs").read_text(encoding="utf-8")
|
||||
self.assertIn('"--exefs", ExefsPath.Text, "--romfs", RomfsPath.Text', shell)
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
raise SystemExit(unittest.main(verbosity=1))
|
||||
53
tests/test_prompts_mod_sizetable.py
Normal file
53
tests/test_prompts_mod_sizetable.py
Normal file
@@ -0,0 +1,53 @@
|
||||
"""Button prompts beside a mod that ships its own resource size table (host/prompts.c, host/mods.c; #bug-tracker 11-18).
|
||||
|
||||
The prompts' font is larger than the game's; our prompts/rsizetable.zs has room for it, but a mod's own size table wins over
|
||||
ours, and then the game dropped the font, so prompts.c gave the prompts up ("the game's own icons in texts"). The size hook
|
||||
the lifted code calls for every file (host/mods.c hos_mods_table_size, nx2/oot_patches.py patch_mod_sizes) now gives the
|
||||
font its room too. Checked here, by the source:
|
||||
- the hook answers for the font (hos_prompts_font_file) before it looks the path up among the mods' files' others, and
|
||||
says it once in the log;
|
||||
- prompts.c gives up only when that hook has not run in this game (hos_mods_size_hook_seen), not whenever a mod has a
|
||||
size table;
|
||||
- hos_prompts_font_file answers only for the font's own key, through hos_prompts_romfs (which builds it once).
|
||||
"""
|
||||
|
||||
import re
|
||||
import unittest
|
||||
from pathlib import Path
|
||||
|
||||
ROOT = Path(__file__).resolve().parents[1]
|
||||
|
||||
|
||||
def function(text, head):
|
||||
at = text.index(head)
|
||||
end = text.index("{", at) + 1
|
||||
depth = 1
|
||||
while depth:
|
||||
depth += (text[end] == "{") - (text[end] == "}")
|
||||
end += 1
|
||||
return text[at:end]
|
||||
|
||||
|
||||
class PromptsModSizeTable(unittest.TestCase):
|
||||
def test_hook_sizes_the_font(self):
|
||||
mods = (ROOT / "host" / "mods.c").read_text(encoding="utf-8")
|
||||
hook = function(mods, "uint64_t hos_mods_table_size(uint64_t size, uint64_t path) {")
|
||||
self.assertIn("InterlockedExchange(&hos_mods_size_hook_seen, 1)", hook)
|
||||
self.assertLess(hook.index("hos_mods_size_hook_seen"), hook.index("if (!table_n")) # (seen also with no mods)
|
||||
self.assertIn("hos_prompts_font_file(key, built, sizeof built)", hook)
|
||||
self.assertIn("zs_content_size(built)", hook)
|
||||
self.assertIn("button prompts' font gets", hook)
|
||||
|
||||
def test_prompts_give_up_only_without_the_hook(self):
|
||||
src = (ROOT / "host" / "prompts.c").read_text(encoding="utf-8")
|
||||
build = function(src, "static int build(char *out, size_t cap) {")
|
||||
gate = build[build.index('hos_mods_has(SIZES_PATH, ".rsizetable.zs")'):]
|
||||
self.assertTrue(re.search(r"if \(!hos_mods_size_hook_seen\) \{\s*hos_log\(\"prompts: a mod has the resource size table: "
|
||||
r"the game's own icons in texts\"\);\s*return 0;", gate), "the fallback is not gated on the hook")
|
||||
font = function(src, "int hos_prompts_font_file(const char *key, char *out, size_t cap) {")
|
||||
self.assertIn("if (strcmp(key, FONT_PATH)) return 0;", font)
|
||||
self.assertIn("hos_prompts_romfs(FONT_PATH, out, cap)", font)
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
raise SystemExit(unittest.main(verbosity=1))
|
||||
@@ -120,6 +120,20 @@ int main(void) {
|
||||
got = frame(OOT_SPRINT_MOVE, OOT_SPRINT_LS, 0, 1, ZL);
|
||||
CHECK(!on && got == ZL, "targeting ends it, and ZL is the game's");
|
||||
|
||||
/* ((Triforce, 11-18) a conversation: Link's update goes on in a state no sprint is in, and the click hides the text box
|
||||
there: it is the game's, and starts nothing) */
|
||||
fresh();
|
||||
frame(OOT_SPRINT_STOP, OOT_SPRINT_LS, 0, 1, 0);
|
||||
got = frame(OOT_SPRINT_STOP, OOT_SPRINT_LS, 0, 1, LS);
|
||||
CHECK(got == LS, "in a conversation the click reaches the game");
|
||||
got = frame(OOT_SPRINT_STOP, OOT_SPRINT_LS, 0, 1, LS);
|
||||
CHECK(!on && got == LS, "the whole press stays the game's, no sprint");
|
||||
got = frame(OOT_SPRINT_MOVE, OOT_SPRINT_LS, 0, 1, 0);
|
||||
got = frame(OOT_SPRINT_MOVE, OOT_SPRINT_LS, 0, 1, LS);
|
||||
CHECK(got == 0, "back in play the click is the sprint's again");
|
||||
frame(OOT_SPRINT_MOVE, OOT_SPRINT_LS, 0, 1, 0);
|
||||
CHECK(on, "and starts it");
|
||||
|
||||
/* no update of Link: a menu. The press is the game's and starts nothing */
|
||||
fresh();
|
||||
frame(OOT_SPRINT_MOVE, OOT_SPRINT_LS, 0, 1, 0);
|
||||
|
||||
@@ -176,6 +176,38 @@ class StreamlineSdkTest(unittest.TestCase):
|
||||
self.assertRegex(digest, r"^[0-9a-f]{64}$", name)
|
||||
self.assertEqual(set(sl.GAME_LICENSE_NAMES), {name for name, _ in licenses})
|
||||
|
||||
def test_dlss_beside_streamline(self):
|
||||
"""(11-18) with Streamline up NGX finds nvngx_dlss.dll only in dlssg: make_game.py puts a copy of the ngx one there,
|
||||
and takes it away with Streamline's files (the folder then goes too) or when the build has no DLSS"""
|
||||
sys.path.insert(0, str(ROOT / "nx2"))
|
||||
try:
|
||||
mg = load("make_game_for_test", ROOT / "nx2" / "make_game.py")
|
||||
finally:
|
||||
sys.path.remove(str(ROOT / "nx2"))
|
||||
sdk, game = self.dir / "sdk", self.dir / "game"
|
||||
sl.fetch(str(sdk), say=self.say, archive=zipfile.ZipFile(io.BytesIO(archive_bytes())))
|
||||
(game / "ngx").mkdir(parents=True)
|
||||
(game / "ngx" / "nvngx_dlss.dll").write_bytes(b"dlss")
|
||||
saved = (mg.streamline_script, mg.LINUX, os.environ.get("HOOKSHOT_STREAMLINE_SDK"))
|
||||
mg.streamline_script, mg.LINUX = (lambda: sl), False
|
||||
try:
|
||||
os.environ["HOOKSHOT_STREAMLINE_SDK"] = str(sdk)
|
||||
dst = Path(mg.place_dlssg_runtime(str(game)))
|
||||
self.assertEqual((dst / "nvngx_dlss.dll").read_bytes(), b"dlss")
|
||||
(game / "ngx" / "nvngx_dlss.dll").unlink() # (a build without DLSS: no stale copy)
|
||||
mg.place_dlssg_runtime(str(game))
|
||||
self.assertFalse((dst / "nvngx_dlss.dll").exists())
|
||||
(dst / "nvngx_dlss.dll").write_bytes(b"old") # (a build without Streamline: the folder goes whole)
|
||||
os.environ["HOOKSHOT_STREAMLINE_SDK"] = ""
|
||||
self.assertIsNone(mg.place_dlssg_runtime(str(game)))
|
||||
self.assertFalse(dst.exists())
|
||||
finally:
|
||||
mg.streamline_script, mg.LINUX = saved[0], saved[1]
|
||||
if saved[2] is None:
|
||||
os.environ.pop("HOOKSHOT_STREAMLINE_SDK", None)
|
||||
else:
|
||||
os.environ["HOOKSHOT_STREAMLINE_SDK"] = saved[2]
|
||||
|
||||
def test_make_game(self):
|
||||
src = (ROOT / "nx2" / "make_game.py").read_text(encoding="utf-8")
|
||||
for needle in ("fetch_streamline_sdk(a)", "place_dlssg_runtime(out)", 'os.environ["HOOKSHOT_STREAMLINE_SDK"] = sdk',
|
||||
|
||||
156
tests/test_thread_wake_cleanup.py
Normal file
156
tests/test_thread_wake_cleanup.py
Normal file
@@ -0,0 +1,156 @@
|
||||
"""Exercise thread destruction from hos_thread.c with counted host handles, without a game.
|
||||
|
||||
Removing the wake CloseHandle must fail the repeated-lifecycle and free-order checks.
|
||||
The registry removal is production code; only host locks, handles and free are instrumented.
|
||||
"""
|
||||
import argparse
|
||||
from pathlib import Path
|
||||
import shutil
|
||||
import subprocess
|
||||
import tempfile
|
||||
import unittest
|
||||
|
||||
RUN_ALL_ARGS = ["--cc", "{cxx}"]
|
||||
|
||||
ROOT = Path(__file__).resolve().parents[1]
|
||||
COMPILER = None
|
||||
|
||||
|
||||
def block(text, marker):
|
||||
start = text.index(marker)
|
||||
end = text.index("{", start) + 1
|
||||
depth = 1
|
||||
while depth:
|
||||
depth += (text[end] == "{") - (text[end] == "}")
|
||||
end += 1
|
||||
return text[start:end]
|
||||
|
||||
|
||||
FIXTURE = r'''
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
|
||||
#define CHECK(test, message) do { if (!(test)) { fprintf(stderr, "%s\n", message); exit(1); } } while (0)
|
||||
typedef struct { int closed, kind; } HostHandle;
|
||||
typedef HostHandle *HANDLE;
|
||||
typedef struct { HANDLE handle, wake; } HosThread;
|
||||
static HosThread *threads[1024], *destroying;
|
||||
static int nthreads, threads_lock, lock_held, frees, thread_closes, wake_closes, check_free_order;
|
||||
|
||||
static void AcquireSRWLockExclusive(int *lock) {
|
||||
CHECK(lock == &threads_lock && !lock_held, "invalid registry lock acquisition");
|
||||
lock_held = 1;
|
||||
}
|
||||
static void ReleaseSRWLockExclusive(int *lock) {
|
||||
CHECK(lock == &threads_lock && lock_held, "invalid registry lock release");
|
||||
lock_held = 0;
|
||||
}
|
||||
static void check_unregistered(void) {
|
||||
CHECK(!lock_held, "resource released while registry lock held");
|
||||
for (int i = 0; i < nthreads; i++)
|
||||
CHECK(threads[i] != destroying, "destroyed thread remains registered");
|
||||
}
|
||||
static int CloseHandle(HANDLE handle) {
|
||||
CHECK(handle != NULL, "null handle closed");
|
||||
CHECK(!handle->closed, "handle closed more than once");
|
||||
CHECK(handle == destroying->handle || handle == destroying->wake, "wrong handle closed");
|
||||
check_unregistered();
|
||||
if (handle->kind == 1) thread_closes++;
|
||||
else {
|
||||
CHECK(!destroying->handle || destroying->handle->closed, "wake closed before thread handle");
|
||||
wake_closes++;
|
||||
}
|
||||
handle->closed = 1;
|
||||
return 1;
|
||||
}
|
||||
static void checked_free(void *memory) {
|
||||
CHECK(memory == destroying, "wrong thread freed");
|
||||
check_unregistered();
|
||||
if (check_free_order) {
|
||||
CHECK(!destroying->handle || destroying->handle->closed, "thread handle live at free");
|
||||
CHECK(!destroying->wake || destroying->wake->closed, "wake event live at free");
|
||||
}
|
||||
frees++;
|
||||
free(memory);
|
||||
}
|
||||
#define free checked_free
|
||||
@DESTROY@
|
||||
#undef free
|
||||
|
||||
static void cycle(int handles, int registered) {
|
||||
HostHandle thread = {0, 1}, wake = {0, 2};
|
||||
HosThread before = {NULL, NULL}, after = {NULL, NULL};
|
||||
destroying = (HosThread *)calloc(1, sizeof *destroying);
|
||||
CHECK(destroying != NULL, "allocation failed");
|
||||
if (handles & 1) destroying->handle = &thread;
|
||||
if (handles & 2) destroying->wake = &wake;
|
||||
threads[0] = &before;
|
||||
if (registered) { threads[1] = destroying; threads[2] = &after; nthreads = 3; }
|
||||
else { threads[1] = &after; nthreads = 2; }
|
||||
hos_thread_destroy(destroying);
|
||||
CHECK(nthreads == 2, "wrong registry size after destroy");
|
||||
CHECK(threads[0] == &before && threads[1] == &after, "surviving registry entries changed");
|
||||
destroying = NULL;
|
||||
}
|
||||
int main(int argc, char **argv) {
|
||||
CHECK(argc == 2, "expected test case");
|
||||
if (!strcmp(argv[1], "repeated")) {
|
||||
for (int i = 0; i < 10000; i++) cycle(3, 1);
|
||||
CHECK(frees == 10000 && thread_closes == 10000, "thread lifecycle count mismatch");
|
||||
CHECK(wake_closes == 10000, "wake event leak: expected 10000 closes");
|
||||
} else if (!strcmp(argv[1], "order")) {
|
||||
check_free_order = 1;
|
||||
cycle(3, 1);
|
||||
CHECK(frees == 1 && thread_closes == 1 && wake_closes == 1, "close count mismatch");
|
||||
} else if (!strcmp(argv[1], "nulls")) {
|
||||
check_free_order = 1;
|
||||
for (int registered = 0; registered < 2; registered++)
|
||||
for (int handles = 0; handles < 4; handles++) cycle(handles, registered);
|
||||
CHECK(frees == 8 && thread_closes == 4 && wake_closes == 4, "null-handle lifecycle count mismatch");
|
||||
} else CHECK(0, "unknown test case");
|
||||
puts("PASS: thread resources closed once and registry preserved");
|
||||
return 0;
|
||||
}
|
||||
'''
|
||||
|
||||
|
||||
class ThreadWakeCleanupTests(unittest.TestCase):
|
||||
@classmethod
|
||||
def setUpClass(cls):
|
||||
compiler = COMPILER or shutil.which("gcc") or shutil.which("clang")
|
||||
if not compiler:
|
||||
raise unittest.SkipTest("no C compiler")
|
||||
cls.temporary = tempfile.TemporaryDirectory(prefix="hookshot-thread-cleanup-")
|
||||
cls.addClassCleanup(cls.temporary.cleanup)
|
||||
directory = Path(cls.temporary.name)
|
||||
production = (ROOT / "host/hos_thread.c").read_text(encoding="utf-8")
|
||||
source = directory / "thread_cleanup.c"
|
||||
source.write_text(FIXTURE.replace("@DESTROY@", block(production, "void hos_thread_destroy(")),
|
||||
encoding="utf-8")
|
||||
cls.executable = directory / "check.exe"
|
||||
result = subprocess.run([compiler, "-x", "c", "-std=c11", "-Wall", "-Wextra", "-Werror",
|
||||
str(source), "-o", str(cls.executable)], capture_output=True, text=True)
|
||||
if result.returncode:
|
||||
raise AssertionError(result.stdout + result.stderr)
|
||||
|
||||
def run_case(self, name):
|
||||
result = subprocess.run([str(self.executable), name], capture_output=True, text=True, timeout=30)
|
||||
self.assertEqual(result.returncode, 0, result.stdout + result.stderr)
|
||||
|
||||
def test_repeated_lifecycles_release_all_wake_events(self):
|
||||
self.run_case("repeated")
|
||||
|
||||
def test_handles_close_before_owner_is_freed(self):
|
||||
self.run_case("order")
|
||||
|
||||
def test_null_handles_and_registered_or_unregistered_threads(self):
|
||||
self.run_case("nulls")
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
parser = argparse.ArgumentParser(description=__doc__)
|
||||
parser.add_argument("--cc")
|
||||
args, rest = parser.parse_known_args()
|
||||
COMPILER = args.cc
|
||||
unittest.main(argv=[__file__] + rest)
|
||||
Reference in New Issue
Block a user