diff --git a/gpu/vk/eden/shader_recompiler/backend/spirv/emit_spirv_context_get_set.cpp b/gpu/vk/eden/shader_recompiler/backend/spirv/emit_spirv_context_get_set.cpp index ae5d1fe..a2c5d7e 100644 --- a/gpu/vk/eden/shader_recompiler/backend/spirv/emit_spirv_context_get_set.cpp +++ b/gpu/vk/eden/shader_recompiler/backend/spirv/emit_spirv_context_get_set.cpp @@ -440,13 +440,6 @@ void EmitSetAttribute(EmitContext& ctx, IR::Attribute attr, Id value, [[maybe_un value = ctx.OpBitcast(output->type, value); } - static constexpr IR::Attribute cd0 = IR::Attribute::ClipDistance0; - static constexpr IR::Attribute cd7 = IR::Attribute::ClipDistance7; - - if (attr >= cd0 && attr <= cd7) { - const u32 idx = (u32) attr - (u32) cd0; - clip_distance_written.set(idx); - } ctx.OpStore(output->pointer, value); } diff --git a/gpu/vk/eden/shader_recompiler/backend/spirv/emit_spirv_special.cpp b/gpu/vk/eden/shader_recompiler/backend/spirv/emit_spirv_special.cpp index daa83de..a16a975 100644 --- a/gpu/vk/eden/shader_recompiler/backend/spirv/emit_spirv_special.cpp +++ b/gpu/vk/eden/shader_recompiler/backend/spirv/emit_spirv_special.cpp @@ -197,11 +197,9 @@ void EmitPrologue(EmitContext& ctx) { if (Sirit::ValidId(ctx.clip_distances)) { for (u32 i = 0; i < ctx.profile.max_user_clip_distances; ++i) { - if (!clip_distance_written.test(i)) { - const Id idx = ctx.Const(i); - const Id element = ctx.OpAccessChain(ctx.output_f32, ctx.clip_distances, idx); - ctx.OpStore(element, ctx.Const(0.0f)); - } + const Id idx = ctx.Const(i); + const Id element = ctx.OpAccessChain(ctx.output_f32, ctx.clip_distances, idx); + ctx.OpStore(element, ctx.Const(0.0f)); } } } diff --git a/gpu/vk/eden/shader_recompiler/backend/spirv/spirv_emit_context.cpp b/gpu/vk/eden/shader_recompiler/backend/spirv/spirv_emit_context.cpp index d9daec6..98eec2a 100644 --- a/gpu/vk/eden/shader_recompiler/backend/spirv/spirv_emit_context.cpp +++ b/gpu/vk/eden/shader_recompiler/backend/spirv/spirv_emit_context.cpp @@ -2312,7 +2312,11 @@ void EmitContext::DefineMesh(const IR::Program& program) { mesh_attribute_words = (std::max)( mesh_primitive_base + Common::DivCeil(mesh_info.max_primitives, 32U) * primitive_lines * 32, 1U); - mesh_map_bytes = 4 + mesh_info.Corners() * mesh_info.max_primitives; + // Whole blocks of 32 primitives, as the attributes: the program writes the index bytes of every primitive block, lanes past + // max_primitives too (a count of 0 wraps its min(count - 1, lane) to every lane). Sized to max_primitives, those writes were + // clamped onto the last byte, the last primitive's last corner: with 126 primitives (most of OoT's) a stray vertex, flashing + // triangles over Kokiri's foliage and ground (as nxnative found: "primitive index bytes for every primitive block") + mesh_map_bytes = 4 + mesh_info.Corners() * Common::DivCeil(mesh_info.max_primitives, 32U) * 32; mesh_shared_u32 = TypePointer(spv::StorageClass::Workgroup, U32[1]); mesh_attributes = DefineVariable(*this, TypeArray(U32[1], Const(mesh_attribute_words)), std::nullopt, spv::StorageClass::Workgroup); diff --git a/gpu/vk/eden/shader_recompiler/backend/spirv/spirv_emit_context.h b/gpu/vk/eden/shader_recompiler/backend/spirv/spirv_emit_context.h index e7f5915..94098dd 100644 --- a/gpu/vk/eden/shader_recompiler/backend/spirv/spirv_emit_context.h +++ b/gpu/vk/eden/shader_recompiler/backend/spirv/spirv_emit_context.h @@ -19,8 +19,6 @@ namespace Shader::Backend::SPIRV { -static std::bitset<8> clip_distance_written; - using Sirit::Id; class VectorTypes { diff --git a/gpu/vk/eden/shader_recompiler/frontend/ampere/lower_to_maxwell.h b/gpu/vk/eden/shader_recompiler/frontend/ampere/lower_to_maxwell.h index a4b069a..cf5ef9d 100644 --- a/gpu/vk/eden/shader_recompiler/frontend/ampere/lower_to_maxwell.h +++ b/gpu/vk/eden/shader_recompiler/frontend/ampere/lower_to_maxwell.h @@ -284,6 +284,9 @@ inline LoweredProgram LowerToMaxwellWith(std::span code, u32 if (!entered) for (u32 pc = index + 1; pc < target; ++pc) dead_instructions[pc] = true; } + // (hookshot) A shared memory store (STS) not yet followed by a barrier: an LDS after it may read what another lane of the + // warp stored (see LDS below) + bool shared_store_pending = false; for (u32 index = 0; index < code.size(); ++index) { auto insn = code[index]; result.source_instruction_offsets.push_back(static_cast(result.instructions.size())); @@ -1701,6 +1704,7 @@ inline LoweredProgram LowerToMaxwellWith(std::span code, u32 case 0xb1d: // BAR.SYNC 0 (bit 80: .DEFER_BLOCKING), full workgroup. if (insn.words[1] || (insn.words[2] & ~0x10000U) || native_predicate != 7) return fail(index, "native workgroup barrier mode"); + shared_store_pending = false; emit(0xf0a8000000000000ULL | predicate | (u64{1} << 43) | (u64{1} << 44) | (u64{1} << 39) | (u64{7} << 39)); break; @@ -1757,6 +1761,23 @@ inline LoweredProgram LowerToMaxwellWith(std::span code, u32 } const bool store = opcode != 0x984; const u32 data = store ? insn.Bits(32, 8) : static_cast(dest); + // (hookshot) A warp stores to shared memory (STS) and reads it back (LDS) with no barrier between: the Switch runs a + // warp's shared memory accesses in order, 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 on NVIDIA's cooperative + // matrix path: 16x2 bright bars, Hookshot issue 2455). As for LDSM: a subgroup barrier before the first LDS after an + // STS, only where the LDS is not predicated (every lane of the warp reaches it). RX_VK_SHARED_NO_LDS_BARRIER=1: none, as before + // ((Triforce) both our STS forms, 0x388 and 0x988 with a uniform base, count as the store; the switch read here, as + // this file's others are: upstream's env_on is in a header we do not have) + static const bool lds_barrier = [] { + const char* e = std::getenv("RX_VK_SHARED_NO_LDS_BARRIER"); + return !(e && *e && *e != '0'); + }(); + if (store) { + shared_store_pending = true; + } else if (shared_store_pending && lds_barrier && native_predicate == 7) { + emit(0xef98000000000000ULL | predicate | (u64{1} << 20)); // (MEMBAR, bit 20: barrier_operations.cpp) + shared_store_pending = false; + } emit((store ? 0xef58000000000000ULL : 0xef48000000000000ULL) | predicate | data | (u64{address} << 8) | (u64{insn.Bits(40, 24)} << 20) | (u64{width} << 48)); @@ -1877,6 +1898,9 @@ inline LoweredProgram LowerToMaxwellWith(std::span code, u32 return fail(index, "native matrix load mode"); u32 address = insn.Bits(24, 8); const u64 offset = insn.Bits(40, 24); + // (Fritchii f130f0c50; ours had it already) An address the results overwrite is copied first, to R254: the paths below + // use R246-R253, and R253 is where each matrix's row address is shuffled to, so with the copy there matrices 1-3 of + // an .x2/.x4 read matrix 0's rows (OoT has one such LDSM.x4, gpu/vk/tests/ldsm_address_test.cpp) if (uniform_offset) { const u32 uniform = uniform_register(insn.Bits(32, 8)); if (uniform == ~0U) return fail(index, "native matrix load uniform offset"); diff --git a/gpu/vk/eden/shader_recompiler/frontend/maxwell/translate/impl/floating_point_conversion_integer.cpp b/gpu/vk/eden/shader_recompiler/frontend/maxwell/translate/impl/floating_point_conversion_integer.cpp index c8c9e26..ef13b54 100644 --- a/gpu/vk/eden/shader_recompiler/frontend/maxwell/translate/impl/floating_point_conversion_integer.cpp +++ b/gpu/vk/eden/shader_recompiler/frontend/maxwell/translate/impl/floating_point_conversion_integer.cpp @@ -146,6 +146,10 @@ void TranslateF2I(TranslatorVisitor& v, u64 insn, const IR::F16F32F64& src_a) { }()}; const bool is_signed{f2i.is_signed != 0}; const auto [max_bound, min_bound] = ClampBounds(f2i.dest_format, is_signed); + // (Fritchii eba45b151, zortos293/Lichii#1051 by myshadowww) INT32_MAX rounds up to 2^31 as an F32 bound, so an input at or + // above it reached OpConvertFToS out of range, which is undefined. The clamp stops below 2^31 and the exact INT32_MAX is + // selected after the conversion + const bool signed_f32_i32{f2i.dest_format == DestFormat::I32 && f2i.src_format == SrcFormat::F32 && is_signed}; IR::F16F32F64 intermediate; switch (f2i.src_format) { @@ -156,7 +160,7 @@ void TranslateF2I(TranslatorVisitor& v, u64 insn, const IR::F16F32F64& src_a) { break; } case SrcFormat::F32: { - const IR::F32 max_val{v.ir.Imm32(static_cast(max_bound))}; + const IR::F32 max_val{v.ir.Imm32(signed_f32_i32 ? 0x1.fffffep30f : static_cast(max_bound))}; const IR::F32 min_val{v.ir.Imm32(static_cast(min_bound))}; intermediate = v.ir.FPClamp(rounded_value, min_val, max_val); break; @@ -173,6 +177,10 @@ void TranslateF2I(TranslatorVisitor& v, u64 insn, const IR::F16F32F64& src_a) { const size_t bitsize{std::max(32, BitSize(f2i.dest_format))}; IR::U16U32U64 result{v.ir.ConvertFToI(bitsize, is_signed, intermediate)}; + if (signed_f32_i32) { + result = IR::U32{v.ir.Select(v.ir.FPGreaterThanEqual(op_a, v.ir.Imm32(0x1p31f)), + v.ir.Imm32(static_cast((std::numeric_limits::max)())), result)}; + } bool handled_special_case = false; const bool special_nan_cases = diff --git a/gpu/vk/eden/shader_recompiler/frontend/maxwell/translate/impl/integer_floating_point_conversion.cpp b/gpu/vk/eden/shader_recompiler/frontend/maxwell/translate/impl/integer_floating_point_conversion.cpp index 6c09539..ff29e77 100644 --- a/gpu/vk/eden/shader_recompiler/frontend/maxwell/translate/impl/integer_floating_point_conversion.cpp +++ b/gpu/vk/eden/shader_recompiler/frontend/maxwell/translate/impl/integer_floating_point_conversion.cpp @@ -61,6 +61,20 @@ IR::U32 SmallAbs(TranslatorVisitor& v, const IR::U32& value, int bitsize) { return IR::U32{v.ir.Select(is_least, value, absolute)}; } +// (Fritchii, zortos293 f6a8f5132) U32 -> F32 rounded toward zero. OpConvertUToF rounds to nearest whatever the IR's rounding +// asks, so an input of 2^24 or more could round up: 0xffffff80..0xffffffff became 2^32 instead of 0x4f7fffff (a hash times 2^-32 +// reached 1.0). The top 24 significant bits make the float, the rest are dropped; an input below 2^24 converts exactly +IR::F32 ConvertUnsigned32ToFloatRZ(TranslatorVisitor& v, const IR::U32& source) { + auto& ir = v.ir; + const IR::U32 msb{ir.Select(ir.INotEqual(source, ir.Imm32(0)), ir.FindUMsb(source), ir.Imm32(0))}; + const IR::U32 shift{ir.ISub(ir.UMax(msb, ir.Imm32(23)), ir.Imm32(23))}; + const IR::U32 retained{ir.ShiftRightLogical(source, shift)}; + const IR::U32 bits{ir.BitwiseOr(ir.ShiftLeftLogical(ir.IAdd(msb, ir.Imm32(127)), ir.Imm32(23)), + ir.BitwiseAnd(retained, ir.Imm32(0x7fffff)))}; + const IR::F32 exact{ir.ConvertIToF(32, 32, false, ir.BitwiseAnd(source, ir.Imm32(0xffffff)))}; + return IR::F32{ir.Select(ir.IGreaterThan(msb, ir.Imm32(23), false), ir.BitCast(bits), exact)}; +} + void I2F(TranslatorVisitor& v, u64 insn, IR::U32U64 src) { const Encoding i2f{insn}; if (i2f.cc != 0) { @@ -99,7 +113,12 @@ void I2F(TranslatorVisitor& v, u64 insn, IR::U32U64 src) { .rounding = CastFpRounding(i2f.fp_rounding), .fmz_mode = IR::FmzMode::DontCare, }; - auto value{v.ir.ConvertIToF(size_t(dst_bitsize), size_t(conversion_src_bitsize), is_signed, src, fp_control)}; + IR::F16F32F64 value; + if (!is_signed && conversion_src_bitsize == 32 && dst_bitsize == 32 && i2f.fp_rounding == FpRounding::RZ) { + value = ConvertUnsigned32ToFloatRZ(v, IR::U32{src}); + } else { + value = v.ir.ConvertIToF(size_t(dst_bitsize), size_t(conversion_src_bitsize), is_signed, src, fp_control); + } if (i2f.neg != 0) { if (i2f.abs != 0 || !is_signed) { // We know the value is positive diff --git a/gpu/vk/tests/ldsm_address_test.cpp b/gpu/vk/tests/ldsm_address_test.cpp new file mode 100644 index 0000000..db2e3eb --- /dev/null +++ b/gpu/vk/tests/ldsm_address_test.cpp @@ -0,0 +1,67 @@ +// Game-free regression for the Ampere -> Maxwell lowering of LDSM (lower_to_maxwell.h, case 0x83b): when the row address +// register is one of the destination registers it is copied to a scratch register first, and every matrix's SHFL.IDX must +// read the address from that copy. The copy went to R253, which the non-transposed path also uses for each matrix's +// shuffled address: matrices 1-3 of an LDSM.x2/.x4 then shuffled matrix 0's row address and read the wrong rows. Found +// for Lichii (Fritchii f130f0c50, Duck912): the copy now goes to R254. OoT has one such LDSM.x4 (an HMMA compute program). +// clang++ -std=c++20 -I gpu/vk/eden -I gpu/vk/compat gpu/vk/tests/ldsm_address_test.cpp -o ldsm_address_test +#include + +#include "shader_recompiler/frontend/ampere/lower_to_maxwell.h" + +namespace { +using Shader::Ampere::Instruction; + +// LDSM.16.M88.{x1,x2,x4} Rd, [Ra + offset]: count 1 << bits 72-73, transpose bit 78 +Instruction ldsm(u32 dest, u32 address, u32 offset, u32 count_log2, bool transpose) { + return {{{0x83bU | (7U << 12) | (dest << 16) | (address << 24), offset << 8, + (count_log2 << 8) | (transpose ? 0x4000U : 0U), 0}}}; +} + +int check(u32 dest, u32 address, u32 count_log2, bool transpose) { + const Instruction code[2]{ldsm(dest, address, 0x60, count_log2, transpose), {{{0x794d | (7U << 12), 0, 0x03800000, 0}}}}; + const auto lowered = Shader::Ampere::LowerToMaxwell(code); + if (!lowered) { + std::printf("FAIL: LDSM R%u, [R%u] x%u%s did not lower: %.*s\n", dest, address, 1U << count_log2, transpose ? " .T" : "", + int(lowered.error.size()), lowered.error.data()); + return 1; + } + // The register the shuffles read the address from, and whether anything writes it after it is set and before the last + // shuffle (the copy itself is the first write) + u32 from = ~0U, shuffles = 0; + int bad = 0; + bool copied = false; + for (const u64 w : lowered.instructions) { + const u32 written = static_cast(w & 0xff); + if ((w >> 52) == 0xef1) { // SHFL.IDX Rd, Ra(bits 8-15), Rb (bits 48-51: the predicate) + const u32 a = static_cast((w >> 8) & 0xff); + if (from == ~0U) from = a; + if (a != from) bad++; + shuffles++; + } + if ((w >> 52) == 0x5c1 && ((w >> 8) & 0xff) == address && ((w >> 20) & 0xff) == 255 && !copied) { // the copy (IADD Rt, Ra, RZ) + copied = true; + continue; + } + if (copied && from != ~0U && written == from && shuffles < (1U << count_log2) * (transpose ? 2U : 1U)) bad++; + } + const bool overlap = address >= dest && address < dest + (1U << count_log2); + if (overlap && !copied) bad++; + if (!overlap && from != address) bad++; + if (bad) std::printf("FAIL: LDSM R%u, [R%u] x%u%s: the shuffles read R%u, %d bad\n", dest, address, 1U << count_log2, + transpose ? " .T" : "", from, bad); + return bad ? 1 : 0; +} +} // namespace + +int main() { + int fails = 0; + for (const bool transpose : {false, true}) + for (u32 count_log2 = 0; count_log2 < 3; ++count_log2) { + fails += check(68, 68, count_log2, transpose); // the address is Rd itself + fails += check(68, 68 + (1U << count_log2) - 1, count_log2, transpose); // the last destination + fails += check(68, 71, count_log2, transpose); // past the destinations (x4: inside) + fails += check(68, 20, count_log2, transpose); // apart: read in place + } + if (!fails) std::printf("PASS\n"); + return fails ? 1 : 0; +} diff --git a/gpu/vk/vk_dlss.cpp b/gpu/vk/vk_dlss.cpp index 7075f9e..994f215 100644 --- a/gpu/vk/vk_dlss.cpp +++ b/gpu/vk/vk_dlss.cpp @@ -83,6 +83,10 @@ struct State { // up for the run. Each try stalls as a creation does, so they are 600 frames apart) uint64_t retry_frame = 0; int retries = 0; + // ((Triforce, upstream 6770b115 #2550) the preset, output and HDR a creation failed for (not for want of video memory, + // retried above): not tried again each frame, but once any of them changes, so the menu's DLSS rows bring DLSS back + // without a restart. 0: none failed) + uint64_t refused = 0; }; State& st() { static State s; @@ -310,6 +314,8 @@ int prepare(VkCommandBuffer cmd, uint32_t rw, uint32_t rh, uint32_t ow, uint32_t else snprintf(why, sizeof why, "the render size %ux%u left its range, %ux%u to %ux%u", rw, rh, s.min_rw, s.min_rh, s.feature_rw, s.feature_rh); + const uint64_t key = (uint64_t)ow << 44 ^ (uint64_t)oh << 24 ^ (uint64_t)preset << 1 ^ (hdr ? 1 : 0) ^ 1ull << 63; + if (s.refused == key) return *fail = "its feature was not made", 0; // (until a DLSS setting changes: upstream #2550) if (s.feature) { NVSDK_NGX_Handle* old = s.feature; temporal_later([old] { NVSDK_NGX_VULKAN_ReleaseFeature(old); }); @@ -373,9 +379,14 @@ int prepare(VkCommandBuffer cmd, uint32_t rw, uint32_t rh, uint32_t ow, uint32_t (unsigned long long)s.retry_frame, s.retries, kMemoryRetries); return *fail = kNoMemory, 0; } - return *fail = "its feature was not made", -1; + // ((Triforce, upstream 6770b115 #2550) any other failure: the game's upscale until a DLSS setting (preset, output, + // HDR) changes, not for the rest of the run. Quick DLSS model switches left several features alive, and NGX then + // refused the next one (0xbad00002): DLSS was gone until a restart, with no word why) + s.refused = key; + logf("vk: NVIDIA DLSS: the game's upscale until a DLSS setting changes (the DLSS model row, the output, HDR)"); + return *fail = "its feature was not made", 0; } - s.retries = 0, s.retry_frame = 0; + s.retries = 0, s.retry_frame = 0, s.refused = 0; s.feature_rw = max_w, s.feature_rh = max_h, s.feature_ow = ow, s.feature_oh = oh, s.min_rw = min_w, s.min_rh = min_h; s.feature_preset = preset, s.feature_hdr = hdr; s.features_made++; diff --git a/gpu/vk/vk_draw.cpp b/gpu/vk/vk_draw.cpp index 969c86d..f41e23c 100644 --- a/gpu/vk/vk_draw.cpp +++ b/gpu/vk/vk_draw.cpp @@ -3503,7 +3503,19 @@ VkDescriptorBufferInfo uniform_range(const NvnState& s, const Stage& st, uint32_ return {cur->stream, at, n}; } // (RX_VK_UB_SNAPSHOT=1: the contents as they are now, in the stream, not the pool the GPU reads later) - static const bool snapshot = getenv("RX_VK_UB_SNAPSHOT") != nullptr; + // ((Triforce, upstream 018a1d2e #2464) on by default on Intel when the pools have no VRAM mirror (vk_memory.cpp + // mirror_on off: integrated GPUs): the GPU then reads the game's memory itself when it runs the frame, and by then the + // game has written the next frame's uniforms over the ones submitted. Kokiri's depth prepass drew the ground with other + // objects' matrices, and the fog pass painted sky over it (the white forest on Intel Core Ultra iGPUs). Other GPUs with + // no mirror keep it off until it is measured there. RX_VK_UB_SNAPSHOT=0: off; any other value: on) + // ((Triforce, 11-18) and on AMD's integrated GPUs too, which have no mirror either: the same white forest over Kokiri on + // an AMD Radeon iGPU (#bug-tracker "Texture White and Glow"), and the scene and the interface flickering on a ROG Ally X + // (Radeon 890M class, "Screen flicker, UI flicker ... on Ally X"). A discrete GPU without its mirror keeps it off) + static const bool snapshot = [] { + const char* e = getenv("RX_VK_UB_SNAPSHOT"); + if (e) return !(*e == '0' && !e[1]); + return (dev.props.vendorID == 0x8086 || dev.props.deviceType == VK_PHYSICAL_DEVICE_TYPE_INTEGRATED_GPU) && !pools_mirrored(); + }(); BufferRef b = snapshot ? BufferRef{} : gpu_buffer_bind(u.addr, n, false); if (b && b.offset % align == 0) return {b.buffer, b.offset, n}; const uint64_t at = stream_alloc(n, align); // (unaligned or outside the pools: copied into the stream) @@ -3926,7 +3938,7 @@ void build_descriptors(const NvnState& s, const Program& p, bool keep = false) { w.descriptorType = type; return w; }; - const uint64_t salign = std::max(dev.props.limits.minStorageBufferOffsetAlignment, 4); + const uint64_t salign = storage_binding_align(); // (the translator's: vk_internal.h) for (const Stage& st : p.stages) { const Shader::Info& info = st.info; const int ns = st.nvn_stage; diff --git a/gpu/vk/vk_internal.h b/gpu/vk/vk_internal.h index a6bc7dd..7f26a9f 100644 --- a/gpu/vk/vk_internal.h +++ b/gpu/vk/vk_internal.h @@ -100,6 +100,16 @@ struct Device { }; extern Device dev; +/// (hookshot) Where a storage buffer binding starts: the address rounded down to this, the shaders' offsets counted from there +/// (vk_shader.cpp make_profile / host_info, the translator's ssbo_align_mask; vk_draw.cpp's descriptor writer). At least 16 +/// bytes, whatever the device allows: the translator reads 8- and 16-byte accesses through vector views (offset >> 3, >> 4), +/// exact only while a binding starts on a 16-byte boundary. At AMD's 4 bytes an aligned access could start at any word of the +/// binding and the view read the vector before it: OoT's light colours shifted (green lighting, cyan sky). Found for Lichii +/// (zortos293/Lichii#295, Fritchii 57ed438cd, from Matt Artist and sharkiix); NVIDIA's own alignment is 16 already +inline uint64_t storage_binding_align() { + return dev.props.limits.minStorageBufferOffsetAlignment > 16 ? dev.props.limits.minStorageBufferOffsetAlignment : 16; +} + /// A range of guest memory as the GPU sees it: a buffer (an imported pool), the offset in it, and its device address struct BufferRef { VkBuffer buffer = VK_NULL_HANDLE; @@ -110,6 +120,7 @@ struct BufferRef { } }; bool memory_init(); // vk_memory.cpp +bool pools_mirrored(); // ((Triforce, upstream #2464) vk_memory.cpp mirror_on: the buffer pools have VRAM mirrors) // ((Triforce) The PC's RAM and what the process holds (vk_memory.cpp, mem_parts.h). memory_ram_watch: once a present (it asks // every 60 presents, and every 600 has the memory line written on a thread of its own); the answers are those of its last look) void memory_ram_watch(uint64_t presents); diff --git a/gpu/vk/vk_main.cpp b/gpu/vk/vk_main.cpp index d91e03b..a9f4cf2 100644 --- a/gpu/vk/vk_main.cpp +++ b/gpu/vk/vk_main.cpp @@ -486,6 +486,22 @@ bool create_device() { // and 4 (LLPC) draws them natively, as before. tests/test_task_mesh_fallback_devices.py task_mesh_fallback = dev.props.vendorID == 0x1002 && amd_older; #endif + { // ((Triforce, 11-18) the menu's Mesh shaders row, mesh_shaders= in triforce_options.ini: 1 Compute fallback on any GPU with + // mesh shaders (an RX 7700S's glowing walls went with it, #bug-tracker); 0 Auto, the rule above. RX_VK_TASK_MESH_FALLBACK over it) + char exe[MAX_PATH]; + const DWORD n = game_module_path(exe, MAX_PATH); + std::string ini(exe, n); + ini = ini.substr(0, ini.find_last_of("\\/") + 1) + "triforce_options.ini"; + if (FILE* f = fopen(ini.c_str(), "r")) { + char line[80]; + while (fgets(line, sizeof line, f)) + if (!strncmp(line, "mesh_shaders=", 13)) { + if (atoi(line + 13) == 1) task_mesh_fallback = true; + break; + } + fclose(f); + } + } if (const char* e = getenv("RX_VK_TASK_MESH_FALLBACK")) task_mesh_fallback = atoi(e) != 0; dev.task_mesh_fallback = task_mesh_fallback && dev.mesh_shader && f12.bufferDeviceAddress && !getenv("RX_VK_NO_MESH_FALLBACK"); if (dev.task_mesh_fallback) logf("vk: task + mesh programs use the compute fallback (RX_VK_TASK_MESH_FALLBACK=0: native)"); @@ -1102,10 +1118,17 @@ bool started_up() { if (prepare_only()) { // (three parts: the programs, the pipelines the notes predict, then those they name; most of the time is the first) static int part; + // ((Triforce, 11-18) the bar's share of each part as it takes its time: on NVIDIA the programs are most of it (56 s of + // ~70 on an RTX 4070 SUPER); elsewhere the pipelines, which AMD's driver builds in seconds each (an update on an AMD card + // ran over an hour in that last part and sat at 90% and more: Setup's "Preparing shaders" row shows this bar) + static const bool nvidia = dev.props.vendorID == 0x10de; const auto progress = [](size_t done, size_t total) { static size_t last = ~(size_t)0; if (!total) return; - static const size_t from[3] = {0, 700, 900}, span[3] = {700, 200, 100}; + static const size_t from_nv[3] = {0, 700, 900}, span_nv[3] = {700, 200, 100}; + static const size_t from_other[3] = {0, 300, 400}, span_other[3] = {300, 100, 600}; + const size_t* from = nvidia ? from_nv : from_other; + const size_t* span = nvidia ? span_nv : span_other; const size_t at = from[part] + done * span[part] / total; if (at == last) return; last = at; diff --git a/gpu/vk/vk_memory.cpp b/gpu/vk/vk_memory.cpp index 62a78e0..ae0d845 100644 --- a/gpu/vk/vk_memory.cpp +++ b/gpu/vk/vk_memory.cpp @@ -4234,6 +4234,7 @@ bool import_in_background(uint64_t guest, uint64_t size, const char** why) { return true; } } // namespace +bool pools_mirrored() { return mirror_on(); } // ((Triforce, upstream 018a1d2e #2464) vk_draw.cpp uniform_range's default) uint64_t device_vram_bytes() { uint64_t vram = 0; for (uint32_t h = 0; h < dev.memory.memoryHeapCount; h++) @@ -4424,8 +4425,14 @@ bool mirror_pieces_on() { static const bool on = [] { const char* e = getenv("RX_VK_MIRROR_PIECES"); const bool can = dev.sparse_buffers && dev.features.sparseResidencyBuffer; - const bool v = can && (e ? atoi(e) != 0 : memory_trial_on()); - logf("vk: mirrors in pieces: %s%s", v ? "on" : "off", !can ? " (no sparse residency buffers)" : e ? " (RX_VK_MIRROR_PIECES)" : ""); + // ((Triforce, 11-18) not by default on NVIDIA before Turing (device IDs below 0x1e00: GTX 10 series, GTX 900): a GTX + // 1070 lost the GPU after the opening cutscene, the next submission stopped in its waits for a mirror's new pieces + // (VRAM 6.2 of 7 GB, textures being evicted), every time until RX_VK_MIRROR_PIECES=0; with it a new game played + // (#bug-tracker "Crash when starting a new game"). Whole mirrors there, as before the pieces) + const bool old_nvidia = dev.props.vendorID == 0x10de && dev.props.deviceID < 0x1e00; + const bool v = can && (e ? atoi(e) != 0 : !old_nvidia && memory_trial_on()); + logf("vk: mirrors in pieces: %s%s", v ? "on" : "off", !can ? " (no sparse residency buffers)" : e ? " (RX_VK_MIRROR_PIECES)" + : old_nvidia ? " (NVIDIA before Turing: whole mirrors)" : ""); return v; }(); return on; diff --git a/gpu/vk/vk_menu.cpp b/gpu/vk/vk_menu.cpp index 45800bc..8a13f75 100644 --- a/gpu/vk/vk_menu.cpp +++ b/gpu/vk/vk_menu.cpp @@ -623,8 +623,10 @@ std::string mic_key(const char* name) { std::string k(name ? name : ""); for (char& c : k) // (a device's name goes into triforce_options.ini: a line break in it would add lines there) if ((unsigned char)c < 0x20 || c == 0x7f) c = ' '; - if (k.size() <= kMicNameKept) return k; - return k.substr(0, text_char_start(k, kMicNameKept)); + if (k.size() > kMicNameKept) k = k.substr(0, text_char_start(k, kMicNameKept)); + // (and no spaces at the end: a saved line's end, made one above, and those an older build saved with the name) + while (!k.empty() && k.back() == ' ') k.pop_back(); + return k; } /// buf cut back to whole UTF-8 characters (snprintf may have cut one) void utf8_whole(char* buf) { @@ -730,6 +732,7 @@ const char* tx_robust(int v) { static const char* t[] = {"Auto", "Off", "On"}; return (unsigned)v < 3 ? t[v] : t[0]; } +const char* tx_mesh(int v) { return v == 1 ? "Compute fallback" : "Auto"; } // ((Triforce, 11-18) opts.mesh_mode, vk_main.cpp) // (Triforce) The Debug tab's Replay helper threads row (opts.replay_threads; vk_replay.cpp par_threads_max): Off, or the // parallel replay tried on 2 to 8 threads as RX_VK_PARALLEL_AUTO= does. The switches go over the row for a run const char* replay_threads_env() { @@ -897,6 +900,9 @@ MENU_HINTS(hn_wide_menus, 0, "The menus stretched over the whole screen, as befo "The menus in the middle 16:9; beside them, what the game draws there, out of focus.", "The menus in the middle 16:9; black beside them.") MENU_HINTS(hn_robust, 0, "Off, on every card.", "Can be faster on some NVIDIA cards.", "Always on.") +MENU_HINTS(hn_mesh, 0, "As the game finds best for this GPU: the fallback on AMD's RX 6000 driver, native mesh shaders elsewhere.", + "Every mesh draw through compute shaders: a little slower. Try it if walls glow or the GPU stops responding on an AMD " + "card. The shaders are prepared again once.") MENU_HINTS(hn_fps, 0, nullptr, "With frame generation on, the game's own rate too.", "In the counter's place in the top right corner, a see-through panel: the GPU's and the CPU's load, temperatures and memory, " "the frame rate with its lows, the frame times and their graph, the waits per frame and the picture's settings. It " @@ -1141,6 +1147,11 @@ Row rows[] = { {TAB_GRAPHICS, "Robust buffers", &opts.robust, 0, 2, 1, tx_robust, "Bounds-checked buffer reads: a read past a buffer's end is dropped instead of stopping the GPU. Applies after a " "restart.", hn_robust, true}, + // ((Triforce, 11-18) a player's RX 7700S: glowing doors and walls with native mesh shaders, gone with the compute fallback + // (#bug-tracker). The row's key, mesh_shaders, is read where the device is made: vk_main.cpp) + {TAB_GRAPHICS, "Mesh shaders", &opts.mesh_mode, 0, 1, 1, tx_mesh, + "How the game's mesh-shader draws run. Try Compute fallback on an AMD card that shows glowing walls or loses the GPU. " + "Applies after a restart.", hn_mesh, true}, {TAB_SYSTEM, "Game", nullptr, 0, 0, 0, nullptr, nullptr, nullptr, false, true, -1, true}, // ((Triforce) 11-17: a section) // (Triforce: language.txt's choice, not the options file's. Its entries are counted when the file is read: lang_load) @@ -4697,7 +4708,6 @@ void menu_init() { if (!strcmp(line, "dynamic_resolution")) old_dynres = atoi(eq + 1); if (!strcmp(line, "microphone_device")) { // (hookshot: the microphone, by its name) mic_want = mic_key(eq + 1); - while (!mic_want.empty() && (mic_want.back() == '\n' || mic_want.back() == '\r')) mic_want.pop_back(); } } fclose(f); diff --git a/gpu/vk/vk_menu.h b/gpu/vk/vk_menu.h index dd06bcb..dfb1650 100644 --- a/gpu/vk/vk_menu.h +++ b/gpu/vk/vk_menu.h @@ -108,6 +108,7 @@ struct Options { int prompts = 0; // the button icons: 0 Auto (the controller), 1 Switch, 2 Xbox, 3 PS4, 4 PS5, 5 Steam Deck (host/prompts.c), // (Triforce) 6 Keyboard & mouse int robust = 1; // bounds-checked buffer access: 0 Auto (off), 1 Off (the default since 11-17, the owner's), 2 On (gpu/vk/vk_main.cpp; RX_VK_ROBUST) + int mesh_mode = 0; // (Triforce, 11-18) the game's mesh-shader draws: 0 Auto (vk_main.cpp's rule), 1 Compute fallback (RX_VK_TASK_MESH_FALLBACK over it) int descriptor_reuse = 1; // (on by default since options_version 4) a draw keeps its descriptors through other slots' registrations (gpu/vk/vk_draw.cpp; RX_VK_DESC_SLOTGEN) int sampler_cache = 1; // (on by default since options_version 5) sampler ids answer their parameters without the registrations' lock (gpu/vk/vk_draw.cpp; RX_VK_SAMPLER_CACHE) int fast_pipeline_link = 1; // (Triforce, the Debug tab; on by default since options_version 5) Fast pipeline link: a new pipeline is linked from its stage libraries first diff --git a/gpu/vk/vk_shader.cpp b/gpu/vk/vk_shader.cpp index bd47c6e..aae0394 100644 --- a/gpu/vk/vk_shader.cpp +++ b/gpu/vk/vk_shader.cpp @@ -687,7 +687,7 @@ Shader::Profile make_profile() { // (hookshot) gathers nudged by 1/512 of a texel on AMD's and Intel's drivers, as Eden and Lichii have it (vk_vendor.h; in the // cache's stamp) p.need_gather_subpixel_offset = dev.fixes.gather_offset; - p.min_ssbo_alignment = dev.props.limits.minStorageBufferOffsetAlignment; + p.min_ssbo_alignment = storage_binding_align(); // (at least 16: vk_internal.h) return p; } @@ -701,7 +701,7 @@ Shader::HostTranslateInfo host_info() { h.needs_demote_reorder = false; h.support_snorm_render_buffer = true; h.support_viewport_index_layer = true; - h.min_ssbo_alignment = dev.props.limits.minStorageBufferOffsetAlignment; + h.min_ssbo_alignment = storage_binding_align(); h.support_geometry_shader_passthrough = false; h.support_conditional_barrier = true; // (barriers in loops kept: see the WebGPU renderer's 4f5e3e6) return h; @@ -1752,7 +1752,10 @@ uint64_t disk_stamp() { // the pinned graphics profile even if the base profile already has the requested width) const uint32_t lanes[] = {profile().warp_size_potentially_larger_than_guest ? 1u : 0u, profile().max_subgroup_size, profile().has_broken_spirv_fragment_shuffle ? 1u : 0u, (uint32_t)dev.sg32_stages, - getenv("RX_VK_WARP64") ? 1u : 0u}; + getenv("RX_VK_WARP64") ? 1u : 0u, + // (and where the translation starts a storage binding: offsets are counted from there, so a cache + // made on a 16-aligned GPU is no use on a 64-aligned one in the same folder, a laptop's two GPUs) + (uint32_t)storage_binding_align()}; add(lanes, sizeof lanes); // (the records keep Shader::Info as it lies in memory: a cache written by another C++ library (the MSVC build's, then the // llvm-mingw one's libc++) has other layouts, and reading it crashed at start. The library and the sizes, so it starts again) @@ -4111,7 +4114,11 @@ const Program* seed_variant_of(const uint64_t code[3], bool mesh, const uint32_t struct Reset { ~Reset() { questions_in = nullptr, answers_in = nullptr; } } reset; if (mesh) translate_mesh_into(*p, *blank, by_stage[6] ? &code_of[6] : nullptr, code_of[7], nullptr, fs, color_types); else translate_into(*p, *blank, &code_of[0], fs, nullptr, color_types); - if (p->failed) return nullptr; + if (p->failed) { // (upstream #2358: a failed variant's modules went nowhere; ours forget their note too) + for (Stage& st : p->stages) + if (st.module) stage_module_destroy(st.module); + return nullptr; + } std::lock_guard lk(shader_mu); auto& variants = programs[p->key]; for (auto& v : variants) diff --git a/gpu/vk/vk_texture.cpp b/gpu/vk/vk_texture.cpp index 5db0823..0980115 100644 --- a/gpu/vk/vk_texture.cpp +++ b/gpu/vk/vk_texture.cpp @@ -2749,7 +2749,22 @@ Texture* create(const Key& key, const nvn_tex* t, VkFormat vf, bool astc, int bw // stayed zero, and the foliage's dithered LOD crossfade, which reads them, left trees bare at mid distance, #1151. // 3D images take the usage on the driver's word for the 2D one: the query does not cover 2D_ARRAY_COMPATIBLE's // per-slice attachment views) - if (format_props(raw).optimalTilingFeatures & VK_FORMAT_FEATURE_COLOR_ATTACHMENT_BIT) { + // ((Triforce) not for 3D images on NVIDIA's Pascal and older (device IDs below 0x1e00: GTX 10 series, GTX 900): that + // driver takes the usage and the per-slice views, then loses the device on the first draw or even a clear into one + // (upstream #2327: GTX 1080, driver 581.80; here a GTX 1070, 581.80, at the area load after the opening cutscene and + // at every save load). Without the usage the views are not made and the noise pass is left out there (draw(): trees + // bare at mid distance, as before #1151) until upstream's separate-attachment path is ported. RX_VK_BC3D_TARGET=1: + // the usage anyway, =0: never) + static const int bc3d_env = getenv("RX_VK_BC3D_TARGET") ? atoi(getenv("RX_VK_BC3D_TARGET")) : -1; + const bool old_nvidia = dev.props.vendorID == 0x10de && dev.props.deviceID < 0x1e00; + const bool bc3d_target = bc3d_env >= 0 ? bc3d_env != 0 : !old_nvidia; + if (x.type == VK_IMAGE_TYPE_3D && !bc3d_target) { + static std::atomic told{0}; + if (told++ == 0) + logf("vk: 3D BC images are not made render targets (%s; NVIDIA before Turing loses the device on them): the " + "noise pass is left out (RX_VK_BC3D_TARGET=1: made anyway)", + bc3d_env == 0 ? "RX_VK_BC3D_TARGET=0" : "NVIDIA device id below 0x1e00"); + } else if (format_props(raw).optimalTilingFeatures & VK_FORMAT_FEATURE_COLOR_ATTACHMENT_BIT) { // (asked with the flags a 3D image is made with below; a refused image is made again without the usage) const VkImageCreateFlags asked = flags | (x.type != VK_IMAGE_TYPE_3D ? 0u : VK_IMAGE_CREATE_2D_ARRAY_COMPATIBLE_BIT | (dev.view_2d_of_3d ? VK_IMAGE_CREATE_2D_VIEW_COMPATIBLE_BIT_EXT : 0u)); diff --git a/host/display.c b/host/display.c index fa83b58..1d1f479 100644 --- a/host/display.c +++ b/host/display.c @@ -488,8 +488,17 @@ static DWORD WINAPI display_main(void *p) { /* (no activate: shown at the bottom of the z-order, so a game in front keeps its focus) */ if (noactivate) SetWindowPos(hwnd, HWND_BOTTOM, 0, 0, 0, 0, SWP_NOMOVE | SWP_NOSIZE | SWP_NOACTIVATE | SWP_SHOWWINDOW); - else + else { ShowWindow(hwnd, SW_SHOW); + /* ((Triforce, 11-18) brought to the front: after the Insert menu's Restart now the new window opened behind + another one (the run that started it had exited meanwhile, and the window it handed the front to kept it), and + the controllers, which count only while the game is in front (nn_hid.c), did nothing until the game was quit + and started by hand (#bug-tracker "Controller fails to work upon restart"). The run before allows it + (hos_relaunch.c AllowSetForegroundWindow); a start from Setup or an icon is in front already) */ + SetForegroundWindow(hwnd); + if (GetForegroundWindow() != hwnd) + hos_log("display: the window did not come to the front at its start (controllers count once it is in front)"); + } if (getenv("HOS_FULLSCREEN") && atoi(getenv("HOS_FULLSCREEN"))) set_fullscreen(hwnd, 1); RAWINPUTDEVICE rid = {1, 2, 0, hwnd}; /* generic desktop mouse: WM_INPUT while the window is in front */ RegisterRawInputDevices(&rid, 1, sizeof rid); diff --git a/host/hos_main.c b/host/hos_main.c index c783a49..f0c98cf 100644 --- a/host/hos_main.c +++ b/host/hos_main.c @@ -1644,6 +1644,19 @@ static void occlusion_switch(void) { a message says it once at boot; the game starts anyway. The text, or 0 when there is enough */ #define HOS_MEMORY_NEEDED_MB 10240ull static int memory_notice_text(unsigned long long ram_mb, unsigned long long swap_mb, char *out, size_t cap) { + /* ((Triforce, 11-18) and no swap at all below 40 GB of memory: a player's PC of 32 GB with Windows' page file off + crashed as every save loaded, 17 "commit ... failed (error 1455)" before it: the game's own memory and the + renderer's reach about 21 GB there, and without a page file the other programs' share counts against the same + limit. With the page file back on it played (#bug-tracker 11-18) */ + if (swap_mb == 0 && ram_mb < 40960ull) { + snprintf(out, cap, + "This PC has %.1f GB of memory and no swap (the page file on Windows is turned off). Loading a save the game " + "uses about 21 GB, and without a page file Windows can run out and the game closes.\n\nOn Windows: search " + "\"Adjust the appearance and performance of Windows\", Advanced, Virtual memory: Change, tick \"Automatically " + "manage paging file size for all drives\", then restart the PC. It starts now anyway.", + ram_mb / 1024.0); + return 1; + } if (ram_mb + swap_mb >= HOS_MEMORY_NEEDED_MB) return 0; snprintf(out, cap, "This PC has %.1f GB of memory and %.1f GB of swap (the page file on Windows). The game needs about %llu GB of the " @@ -1672,7 +1685,10 @@ static void memory_notice(void) { hos_log("boot: %llu MB of memory, %llu MB of swap", ram, swap); return; } - hos_log("boot: LOW MEMORY: %llu MB of memory, %llu MB of swap, below the ~%llu MB the game needs", ram, swap, HOS_MEMORY_NEEDED_MB); + if (swap == 0 && ram + swap >= HOS_MEMORY_NEEDED_MB) /* ((Triforce, 11-18) memory_notice_text's no-swap case) */ + hos_log("boot: NO PAGE FILE: %llu MB of memory, 0 MB of swap: loading a save can run out of memory (error 1455)", ram); + else + hos_log("boot: LOW MEMORY: %llu MB of memory, %llu MB of swap, below the ~%llu MB the game needs", ram, swap, HOS_MEMORY_NEEDED_MB); if (getenv("HOS_HEADLESS")) return; #ifndef _WIN32 int hos_sdl_load(void); diff --git a/host/hos_relaunch.c b/host/hos_relaunch.c index beb74bb..840a112 100644 --- a/host/hos_relaunch.c +++ b/host/hos_relaunch.c @@ -43,6 +43,9 @@ static void relaunch(void) { swprintf(pid, 16, L"%lu", (unsigned long)GetCurrentProcessId()); SetEnvironmentVariableW(L"HOS_RESTART_OF", pid); if (CreateProcessW(exe, cmd, NULL, NULL, FALSE, 0, NULL, NULL, &si, &pi)) { + /* ((Triforce, 11-18) this run is in front: the new one may take the front for its window (display.c), which it + otherwise opened behind another window once this one had gone, and the controllers did nothing there) */ + AllowSetForegroundWindow(pi.dwProcessId); CloseHandle(pi.hThread); CloseHandle(pi.hProcess); } else { diff --git a/host/hos_thread.c b/host/hos_thread.c index 4fb7358..3672dd7 100644 --- a/host/hos_thread.c +++ b/host/hos_thread.c @@ -600,6 +600,7 @@ void hos_thread_destroy(HosThread *t) { if (threads[i] == t) { threads[i] = threads[--nthreads]; break; } ReleaseSRWLockExclusive(&threads_lock); if (t->handle) CloseHandle(t->handle); + if (t->wake) CloseHandle(t->wake); free(t); } diff --git a/host/mods.c b/host/mods.c index 5ce51ca..4aae486 100644 --- a/host/mods.c +++ b/host/mods.c @@ -204,12 +204,14 @@ static void code_record_load(void) { if (built) nbuilt = mod_code_parse(text, built, BUILT_MOST); free(text); } -static void code_patches(const Mod *mod) { +/* (returns how many of this mod's patches wait for a build: new since it, or changed) */ +static int code_patches(const Mod *mod) { char pat[MAX_PATH]; snprintf(pat, sizeof pat, "%s\\exefs\\*", mod->path); WIN32_FIND_DATAA fd; HANDLE f = FindFirstFileA(pat, &fd); - if (f == INVALID_HANDLE_VALUE) return; + if (f == INVALID_HANDLE_VALUE) return 0; + int waits = 0; do { if ((fd.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) || !mod_code_is_patch(fd.cFileName)) continue; char path[MAX_PATH], rel[MOD_CODE_PATH]; @@ -224,9 +226,11 @@ static void code_patches(const Mod *mod) { else if (state == MOD_CODE_REFUSED) hos_log("mods: %s was not applied when the game was built: %s", rel, why); else if (state == MOD_CODE_CHANGED) hos_log("mods: %s changed since the game was built: the game has it as it was then", rel); else hos_log("mods: %s is not built into the game", rel); - code_waits += state == MOD_CODE_CHANGED || state == MOD_CODE_WAITS; + waits += state == MOD_CODE_CHANGED || state == MOD_CODE_WAITS; } while (FindNextFileA(f, &fd)); FindClose(f); + code_waits += waits; + return waits; } /* (and the patches the game has that the mods folder no longer does, with or without a mods folder) */ static void code_patches_gone(void) { @@ -281,10 +285,20 @@ static void mods_scan(void) { for (int i = 0; i < nmods; i++) { char dir[MAX_PATH]; snprintf(dir, sizeof dir, "%s\\romfs", mods[i].path); - const int files = index_romfs(dir, "", 0); - code_patches(&mods[i]); /* (Triforce: its exefs patches, built in or waiting) */ + /* (Triforce: its exefs patches, built in or waiting, first. (11-18) A mod whose code waits for a build keeps its RomFS + files waiting too: the game crashed at its start with a mod's new files over its old code (Classic Item Slots + put in by the Mods menu, #bug-tracker "Crashing on startup"; a build put them in step). They come with the code, + at the next build. HOS_MODS_SPLIT=1: the files now anyway, as before) */ + const int waits = code_patches(&mods[i]); + static int split = -1; + if (split < 0) split = getenv("HOS_MODS_SPLIT") && atoi(getenv("HOS_MODS_SPLIT")) == 1; + const int hold = waits > 0 && !split; + const int files = hold ? 0 : index_romfs(dir, "", 0); cheat_files(&mods[i]); /* (and its cheat file) */ - hos_log("mods: %s: %d RomFS files", mods[i].name, files); + if (hold) + hos_log("mods: %s: its RomFS files wait for the build too, with its code (%d patch(es) not built in yet)", mods[i].name, waits); + else + hos_log("mods: %s: %d RomFS files", mods[i].name, files); } hos_log("mods: %d mods from %s: %zu RomFS entries", nmods, root, table_n); } @@ -340,7 +354,10 @@ static uint64_t zs_content_size(const char *host_path) { address 0). So a mod's file gets at least its unpacked size and room beside it: an eighth of it, 128 KB at least, and 64 KB more (a model needs about 8 KB, and 3 to 4 KB a material and a shape: 99 of the game's own fitted, 11-17). The game's own files keep the table's figure; a figure is only ever raised. size: the table's, path: the guest's path */ +volatile LONG hos_mods_size_hook_seen; /* ((Triforce, 11-18) the lifted code calls the hook below: prompts.c asks */ +int hos_prompts_font_file(const char *key, char *out, size_t cap); /* prompts.c */ uint64_t hos_mods_table_size(uint64_t size, uint64_t path) { + if (!hos_mods_size_hook_seen) InterlockedExchange(&hos_mods_size_hook_seen, 1); if (!table_n || !path) return size; const char *p = (const char *)GP(path); char rel[MAX_PATH], key[MAX_PATH]; @@ -352,6 +369,20 @@ uint64_t hos_mods_table_size(uint64_t size, uint64_t path) { Entry *e = find(key); int packed = 1; if (!e) { + /* ((Triforce, 11-18) the button prompts' font, larger than the game's: with a mod's own size table served, ours + (prompts/rsizetable.zs, which has room for it) is not, so its room comes from here instead. Before, prompts.c + gave the prompts up whenever a mod had a size table (#bug-tracker 11-18: "Button Prompt UI Broken", with the + Young Link Rides Pony Epona mod) */ + char built[MAX_PATH]; + if (hos_prompts_font_file(key, built, sizeof built)) { + const uint64_t real = zs_content_size(built); + const uint64_t need = real ? (real + (64u << 10) + 0xfff) & ~(uint64_t)0xfff : 0; + static volatile LONG told_font; + if (need > (uint32_t)size && !InterlockedExchange(&told_font, 1)) + hos_log("mods: the button prompts' font gets %llu bytes to load in (the size table's %llu; the font %llu unpacked)", + (unsigned long long)need, (unsigned long long)(uint32_t)size, (unsigned long long)real); + return need > (uint32_t)size ? need : size; + } snprintf(key, sizeof key, "%s", rel); lower_slash(key); e = find(key), packed = 0; diff --git a/host/mouse_native.c b/host/mouse_native.c index cb7d160..cc40c87 100644 --- a/host/mouse_native.c +++ b/host/mouse_native.c @@ -9,15 +9,21 @@ which f_80644c80 calls only while the stick is out of its dead zone (found with hardware write watchpoints on the two angles in Kokiri Forest). Yaw falls as the view turns right and pitch rises as it tilts down. - The camera's state at +0x204 is 3 while it follows Link by itself, and then it eases the pitch back to its rest over a - couple of seconds; the right stick makes it 4 (the player's), where a pitch stays where it was put. The mouse leaves the - state as it is: written there from here the game puts it back and the angles with it the next frame. So a pitch the - mouse chose eases back as one the game chose does, while yaw stays. + The camera's state at +0x204 is 3 while it follows Link by itself, and then it eases the pitch back toward its rest and, + as Link walks, turns the yaw back behind him; the right stick makes it 4 (the player's), where the angles stay. The + mouse does not move the stick, so the game's follow kept easing the view back toward Link's: after a while of playing, + the camera drifted to where Link faced. So the angles the mouse chose are the host's own (target_yaw and target_pitch + below), put back into the camera every frame: the camera's update still runs inside the frame -- its easing, its + follow, its collision -- but what it does to the angles never adds up over the frames. The game's own right stick is + let through: while the camera's keys or a pad turn it (nn_hid.c, hos_mouse_native_stick), its angles become the mouse's + new start. The motion is added before the camera reads its input, so the game's own collision, lock-on and scripted cameras still - follow. While the hook has run in the last 150 ms the mouse's motion is the camera's (hos_mouse_native gives 1: the - right stick is left alone); otherwise (the pause menu, cutscenes with another camera, a build the patch did not find - its place in) it is the right stick as before. + follow. The camera is the mouse's only while the game gives it free control: the hook ran within the last 150 ms, the + camera's state at +0x204 is 3 or 4, and the game holds none of the buttons that take the right stick for its own use + (ZR, the bow's aim; L, the item wheel; R — nn_hid.c says which with hos_mouse_native_free). Otherwise (a menu, an item + wheel, a lock-on, the bow's aim, the pause menu, cutscenes with another camera, a build the patch did not find its + place in) the motion is the right stick as before, so the game's own menus, selections and aiming still take it. The sensitivity is the mouse's (the menu's Mouse sensitivity, - and =): 0.1 degrees a count at 1. HOS_MOUSE_INVERT_Y=1 or the menu's Invert mouse vertical flips up and down. */ @@ -37,13 +43,23 @@ extern double hos_mouse_sens; enum { LIMITS_AT = 0x178, PITCH_AT = 0x18c, YAW_AT = 0x1a4, STATE_AT = 0x204 }; #define LIVE_NS 150000000LL /* (the hook ran this recently: the camera takes the mouse) */ #define DEG_PER_COUNT 0.1 +/* (the camera's state at +0x204: 3 while it follows Link by itself, where the right stick takes it over, 4 the player's) */ +#define STATE_FOLLOWS 3 +#define STATE_PLAYER 4 static volatile LONG64 last_hook_ns; static volatile LONG64 last_camera; +static volatile LONG last_state = -1; /* (the last camera's state at +0x204) */ +static volatile LONG game_free = 1; /* (nn_hid.c: the game leaves the camera to the player this frame) */ +static volatile LONG stick_held; /* (nn_hid.c: the camera's keys or a pad turn the camera this frame) */ +static uint64_t own_camera; /* (the camera target_yaw and target_pitch belong to) */ +static float target_yaw, target_pitch; /* (the mouse's angles: the game's own follow is undone every frame) */ +static int target_ok; static long long applied, cameras_seen; 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 int32_t geti(uint64_t a) { int32_t v; memcpy(&v, GP(a), 4); return v; } /* the menu's Mouse camera (option 6), or HOS_MOUSE_NATIVE */ static int native_on(void) { @@ -66,6 +82,24 @@ static int looks_like_camera(uint64_t cam) { fabsf(y) <= 360.0f; } +/* Whether the game gives the camera to the player now: its update ran within the last 150 ms, its state is one the + right stick turns (3 or 4), and none of the buttons the game takes the stick for is held. Otherwise the mouse is the + right stick, as in As the stick: in a menu, an item wheel, a lock-on, a scripted camera or the bow's aim. */ +static int free_look(void) { + if (hos_now_ns() - InterlockedCompareExchange64(&last_hook_ns, 0, 0) >= LIVE_NS) return 0; + const int state = last_state; + return (state == STATE_FOLLOWS || state == STATE_PLAYER) && game_free != 0; +} + +/* nn_hid.c, with each read of the controller: 1 while the game leaves the camera to the player, 0 while it takes the + right stick for its own use (ZR, the bow's aim; L, the item wheel; R) */ +void hos_mouse_native_free(int free) { InterlockedExchange(&game_free, free ? 1 : 0); } + +/* nn_hid.c, with each read of the controller: 1 while the player's own right stick (the camera's keys, a pad) turns the + camera this frame. The game has then moved the angles itself, so they are the mouse's new start: the stick and the + mouse both turn the camera instead of undoing each other. */ +void hos_mouse_native_stick(int held) { InterlockedExchange(&stick_held, held ? 1 : 0); } + /* f_80644c80's first line (nx2/oot_patches.py): cam is the player camera, the game's thread */ void hos_mouse_camera(uint64_t cam) { if (!cam || !native_on() || !looks_like_camera(cam)) return; @@ -78,6 +112,7 @@ void hos_mouse_camera(uint64_t cam) { if (state != 3 && state != 4) { /* (at once: the stick takes the next motion, not one burst once the 150 ms run out) */ InterlockedExchange64(&last_hook_ns, 0); + target_ok = 0; /* the game's angles may change while another camera owns the view */ /* (each state seen leaving the mouse to the stick, once: what another view, Z-targeting, climbing or swimming, is, to add here if one of them should turn with the mouse) */ static uint32_t said[8]; @@ -91,28 +126,45 @@ void hos_mouse_camera(uint64_t cam) { return; } InterlockedExchange64(&last_hook_ns, hos_now_ns()); + InterlockedExchange(&last_state, geti(cam + STATE_AT)); + if (!free_look()) { /* (a menu, an item wheel, a lock-on, the bow: the game's camera, and the motion is the right stick's, nn_hid.c) */ + target_ok = 0; + return; + } if (InterlockedExchange64(&last_camera, (LONG64)cam) != (LONG64)cam && cameras_seen++ < 8) hos_log("mouse native: the player camera at %#llx turns with the mouse", (unsigned long long)cam); + /* (a new camera, or one the game's own stick turned: its angles now are where the mouse starts from) */ + if (!target_ok || own_camera != cam || stick_held) { + own_camera = cam; + target_yaw = getf(cam + YAW_AT); + target_pitch = getf(cam + PITCH_AT); + target_ok = 1; + } int32_t dx, dy; - if (!hos_mouse_take(&dx, &dy) || (!dx && !dy)) return; - const double k = DEG_PER_COUNT * hos_mouse_sens; - float y = getf(cam + YAW_AT) - (float)(dx * k), p = getf(cam + PITCH_AT) + (float)((invert_y() ? -dy : dy) * k); - const float lo = getf(cam + LIMITS_AT), hi = getf(cam + LIMITS_AT + 4); - while (y > 180.0f) y -= 360.0f; - while (y < -180.0f) y += 360.0f; - if (p < lo) p = lo; - if (p > hi) p = hi; - putf(cam + YAW_AT, y), putf(cam + PITCH_AT, p); - if ((++applied & 4095) == 1) - hos_log("mouse native: %lld moves applied; pitch %.2f yaw %.2f", applied, p, y); + if (hos_mouse_take(&dx, &dy) && (dx || dy)) { + const double k = DEG_PER_COUNT * hos_mouse_sens; + target_yaw -= (float)(dx * k); + target_pitch += (float)((invert_y() ? -dy : dy) * k); + const float lo = getf(cam + LIMITS_AT), hi = getf(cam + LIMITS_AT + 4); + while (target_yaw > 180.0f) target_yaw -= 360.0f; + while (target_yaw < -180.0f) target_yaw += 360.0f; + if (target_pitch < lo) target_pitch = lo; + if (target_pitch > hi) target_pitch = hi; + if ((++applied & 4095) == 1) + hos_log("mouse native: %lld moves applied; pitch %.2f yaw %.2f", applied, target_pitch, target_yaw); + } + /* (put back every frame: the camera eases and follows by itself inside one, but none of that adds up to a view that + drifts back toward Link while the mouse is the camera) */ + putf(cam + YAW_AT, target_yaw), putf(cam + PITCH_AT, target_pitch); } /* With each read of the controller (nn_hid.c, the game's threads): 0 if the mouse is not captured, 1 if the camera takes - its motion (hos_mouse_camera), -1 if no player camera runs now (the right stick takes it) */ + its motion (hos_mouse_camera), -1 if the game does not give the camera free control now (a menu, an item wheel, a + lock-on, the bow, the wheels, no player camera: the right stick takes it) */ int hos_mouse_native(void) { static int said; if (!said) said = 1, hos_log("mouse native: on (the menu's Mouse camera, or HOS_MOUSE_NATIVE)"); - if (hos_now_ns() - InterlockedCompareExchange64(&last_hook_ns, 0, 0) >= LIVE_NS) return -1; + if (!free_look()) return -1; int32_t dx, dy; const int captured = hos_mouse_take(&dx, &dy); /* (only to know the capture: the motion is the camera's) */ if (dx || dy) hos_mouse_give_back(dx, dy); diff --git a/host/nn_hid.c b/host/nn_hid.c index d3c76d0..4ab1c5a 100644 --- a/host/nn_hid.c +++ b/host/nn_hid.c @@ -951,6 +951,7 @@ static int flight_jump_shortcut(uint64_t *buttons, int mode, int menu_open, int static void read_input(uint64_t *buttons, int32_t stick[4]) { *buttons = 0; memset(stick, 0, 4 * sizeof *stick); + void hos_mouse_native_stick(int held); scripted_input(buttons, stick); /* A scripted run (a test) takes no input from the player's devices until 5 s after its last press (a variable left set in a shell no longer leaves the game without controls), and otherwise the pads count only @@ -1014,16 +1015,21 @@ static void read_input(uint64_t *buttons, int32_t stick[4]) { if (bound_down(i, 0)) *buttons |= 1ULL << i, player_used(); int32_t mouse[4] = {0}; /* the mouse's motion: the camera itself (mouse_native.c) with the menu's Mouse camera on Native, else the right - stick; also the stick while the player camera does not run (the pause menu, other cameras: mouse_native.c) */ + stick; also the stick while the game does not give the camera free control (a menu, a lock-on, the pause menu, + other cameras: mouse_native.c) and while a button it takes the stick for is held: ZR (the bow's aim), L (the + item wheel) or R, where the mouse points as it does in As the stick */ static const char *native_env; static int native_looked; if (!native_looked) native_looked = 1, native_env = getenv("HOS_MOUSE_NATIVE"); const int native = native_env ? atoi(native_env) != 0 : rx_option(6, 0); int hos_mouse_native(void); + void hos_mouse_native_free(int free); + const uint64_t game_stick = (1ULL << B_ZR) | (1ULL << B_L) | (1ULL << B_R); int captured = 0, as_stick = !native; if (native) { mouse_setup(); mouse_sens_keys(); + hos_mouse_native_free(!(*buttons & game_stick)); captured = hos_mouse_native(); if (captured < 0) captured = 0, as_stick = 1; } @@ -1045,7 +1051,9 @@ static void read_input(uint64_t *buttons, int32_t stick[4]) { controller's stick and the scripted presses are as they are) */ if (walk_vk && key(walk_vk)) walk_keys(keys, rx_option(HOS_OPT_WALK_SCALE, 51)); stick_max(stick, keys); - if (keys[0] || keys[1] || keys[2] || keys[3]) player_used(); + /* (hookshot, mouse_native.c) the keys and the pads are the right stick too: while they turn the camera, its angles are + the native mouse's new start, so the two do not undo each other */ + hos_mouse_native_stick(stick[2] != 0 || stick[3] != 0); } static void vibration_send(uint64_t handle, uint64_t value); diff --git a/host/oot_heap_retry.c b/host/oot_heap_retry.c new file mode 100644 index 0000000..24e0896 --- /dev/null +++ b/host/oot_heap_retry.c @@ -0,0 +1,210 @@ +/* (hookshot) Allocations of the game's "OptimizeChangeBank" thread tried again when their heap has no room, instead of the + game going on with a null pointer and crashing a few frames later. + + The OptimizeChangeBank thread rebuilds a model's material block (f_804566e0, "gsys::ModelResource(MatBlock)") and takes + memory for it from heaps its caller names (its second argument's +8 for the CPU's parts, +16 for the GPU's): small heaps + of their own per model resource ("Model/....bfres", tens of KB), children of a large one. When such a heap is full none + of these allocations is checked: + - two CPU buffers from the heap's tryAlloc (vtable +0x30), at 0x8045690c (stored at the MatBlock's +200, its bump + allocator's memory) and 0x80456da0 (stored at +168, the same base/used/size layout), each sized for the model. A + null one makes its bump allocator hand out guest address 0: the next store, in f_8044cb30, writes guest 0x8 (issue + 1697: "CRASH: exception 0xc0000005 (write ..., guest 0x8)", thread OptimizeChangeBanc, lr 0x80456d48, every time a few + frames after loading that player's save, since 2.0.12); + - the GPU memory, f_80522050 called at 0x80456f18 and 0x80457094, leaves the material block with no memory pool + (0x80457248), and the block is written to guest address 0 two calls later (TomShafford/Hookshot's crash at + backflip + attack). + Each is tried again in the heap's parents up to the root, the first with room (the GPU memory then in the game's + default heap). Only these call sites are changed: f_80522050's other callers (some 30) are as shipped. + + Freeing: the MatBlock's destructor (f_8093f940) frees +168 and +200 through f_8021dea0, the game's free by address, + which walks the heap tree for the heap whose range holds the block (read from the lifted code, not traced in a run), so + a block from a parent goes back to that parent. The model's own heap does not count it (MatBlock +680's accounting + neither). Every block moved to a parent is counted per call site, and the totals are logged every 16 moves after the + first 60 log lines (a growing total over a session would be a leak). + + The GPU part is ported from TomShafford/Hookshot 55d68e8cd6 and 65c1998222 (GPL-3.0, a Hookshot fork), by TomShafford; + its waits before the parents are left out (its own log showed they did not help). + + A sead heap here (vtable 0x84c172c8, the heap class of these): +0x20 its name, +0x28 its memory, +0x30 its size, +0x38 its + parent, vtable +0x30 tryAlloc(size, alignment), vtable +0x80 its free size (ExpHeap: the free list summed, main+0x834916d0). + + HOS_GPU_ALLOC_RETRY=0: none is tried again (as shipped). + HOS_GPU_ALLOC_TEST_FAIL= or , (a test): the first n allocations of each kind at these sites (or that many of + the GPU memory and of the CPU buffers) are refused by the heap they name, as a full heap would, so the retry runs (with + HOS_GPU_ALLOC_RETRY=0: the game's crash, as shipped). */ +#include "hos.h" +#include +#include +#include + +enum { HEAP_NAME = 0x20, HEAP_START = 0x28, HEAP_SIZE = 0x30, HEAP_PARENT = 0x38, VT_TRY_ALLOC = 0x30, VT_FREE_SIZE = 0x80 }; + +static int retry_on(void) { + static int on = -1; + if (on < 0) on = !getenv("HOS_GPU_ALLOC_RETRY") || atoi(getenv("HOS_GPU_ALLOC_RETRY")) != 0; + return on; +} +/* HOS_GPU_ALLOC_TEST_FAIL: 1 while this kind's first n allocations are refused (kind 0 the GPU memory, 1 the CPU buffer) */ +static int test_fail(int kind) { + static long left[2]; + static volatile int read; /* (set once both counts are in: no thread decrements a count another is still setting) */ + if (!__atomic_load_n(&read, __ATOMIC_ACQUIRE)) { /* ("": both kinds; ",": each its own) */ + static volatile long lock; + while (__sync_lock_test_and_set(&lock, 1)) {} + if (!read) { + const char *e = getenv("HOS_GPU_ALLOC_TEST_FAIL"), *comma = e ? strchr(e, ',') : NULL; + left[0] = e ? atol(e) : 0; + left[1] = !e ? 0 : comma ? atol(comma + 1) : atol(e); + __atomic_store_n(&read, 1, __ATOMIC_RELEASE); + } + __sync_lock_release(&lock); + } + return __atomic_sub_fetch(&left[kind], 1, __ATOMIC_RELAXED) >= 0; +} +static int readable(uint64_t a) { return a >= 0x80000000ULL && a < (1ULL << 39) - 0x100; } +static int guest_string(Ctx *c, uint64_t at, char *out, int n) { /* (a guest C string: 1 if it reads as one) */ + int i = 0, ok = readable(at); + for (; ok && i < n - 1; i++) { + const char ch = (char)recomp_load8(c, at + i); + if (!ch) break; + if (ch < 32 || ch >= 127) ok = 0; + out[i] = ch; + } + if (!ok || i < 2) i = 0, out[i++] = '?', ok = 0; + out[i] = 0; + return ok; +} +/* A guest function called as the game would, every register and flag as before but x0: its result */ +static uint64_t guest_call(Ctx *c, uint64_t fn, uint64_t a0, uint64_t a1, uint64_t a2, uint64_t lr) { + uint64_t x[31]; + const uint8_t n = c->n, z = c->z, cf = c->c, v = c->v; + memcpy(x, c->x, sizeof x); + c->x[0] = a0, c->x[1] = a1, c->x[2] = a2, c->x[30] = lr; + a64_icall(c, fn); + const uint64_t r = c->x[0]; + memcpy(c->x, x, sizeof x); + c->n = n, c->z = z, c->c = cf, c->v = v; + return r; +} +static uint64_t heap_method(Ctx *c, uint64_t heap, unsigned slot) { /* (0: none) */ + if (!readable(heap)) return 0; + const uint64_t vt = recomp_load64(c, heap); + return readable(vt) ? recomp_load64(c, vt + slot) : 0; +} +static uint64_t free_size(Ctx *c, uint64_t heap) { /* (the heap's own getFreeSize; ~0: unknown) */ + const uint64_t fn = heap_method(c, heap, VT_FREE_SIZE); + return fn && a64_lookup(fn) ? guest_call(c, fn, heap, 0, 0, c->x[30]) : ~0ULL; +} +static void describe(Ctx *c, uint64_t heap, char *out, int n) { + char name[48], kb[32]; + if (!readable(heap)) { snprintf(out, n, "heap %#llx", (unsigned long long)heap); return; } + if (!guest_string(c, recomp_load64(c, heap + HEAP_NAME), name, sizeof name)) + guest_string(c, recomp_load64(c, heap + HEAP_START), name, sizeof name); + const uint64_t f = free_size(c, heap); + if (f == ~0ULL) snprintf(kb, sizeof kb, "?"); + else snprintf(kb, sizeof kb, "%llu", (unsigned long long)(f >> 10)); + snprintf(out, n, "heap %#llx \"%s\" (%llu KB, %s KB free)", (unsigned long long)heap, name, + (unsigned long long)(recomp_load64(c, heap + HEAP_SIZE) >> 10), kb); +} +static int tell(void) { /* (the first 60 failures logged) */ + static volatile long told; + return __atomic_fetch_add(&told, 1, __ATOMIC_RELAXED) < 60; +} +/* Blocks moved to a parent heap, per call site (0, 1: the CPU buffers at 0x8045690c and 0x80456da0; 2: the GPU memory), + and their bytes: the totals logged every 16th move after the log budget, and with each move while it lasts */ +static volatile long moved_n[3], moved_bytes[3]; +static void count_moved(int site, uint64_t bytes, int say) { + static const char *const names[] = {"model buffer (0x8045690c)", "model buffer (0x80456da0)", "GPU memory"}; + const long n = __atomic_add_fetch(&moved_n[site], 1, __ATOMIC_RELAXED); + const long b = __atomic_add_fetch(&moved_bytes[site], (long)bytes, __ATOMIC_RELAXED); + if (say || n % 16 == 0) + hos_log("oot: heap-full retries so far: %s %ld blocks, %ld KB in parent heaps", names[site], n, b >> 10); +} + +/* ---- the GPU memory: f_80522050(x0 the memory block, x1 the request: +0 the count, +8 the heap, +0x20 what it is for; x2 + the size of one). nx2/oot_patches.py makes f_804566e0's two calls of it call this, with the game's function ---- */ +static int gpu_alloc_in(Ctx *c, A64Fn game, uint64_t a0, uint64_t a1, uint64_t a2, uint64_t lr, uint64_t heap, + uint64_t in) { + recomp_store64(c, a1 + 8, in); + c->x[0] = a0, c->x[1] = a1, c->x[2] = a2, c->x[30] = lr; + game(c); + recomp_store64(c, a1 + 8, heap); + return (int)(c->x[0] & 1); +} +void hos_oot_gpu_alloc(Ctx *c, A64Fn game) { + const uint64_t a0 = c->x[0], a1 = c->x[1], a2 = c->x[2], lr = c->x[30]; + const int on = retry_on() && readable(a1); + const int refused = readable(a1) && recomp_load64(c, a1 + 8) && test_fail(0); + if (refused && !on) { /* (the failure alone, as a full heap gives it) */ + hos_log("oot: GPU memory refused by HOS_GPU_ALLOC_TEST_FAIL, not tried again (HOS_GPU_ALLOC_RETRY=0) (lr %#llx)", + (unsigned long long)lr); + c->x[0] = 0; + return; + } + if (!refused) { + game(c); + if (!on || (c->x[0] & 1)) return; + } + const uint64_t count = recomp_load64(c, a1), heap = recomp_load64(c, a1 + 8), what = recomp_load64(c, a1 + 0x20); + char name[64], where[160]; + guest_string(c, what, name, sizeof name); + describe(c, heap, where, sizeof where); + const int say = tell(); + if (say) + hos_log("oot: GPU memory for %s, %#llx bytes from %s, %s (lr %#llx)", name, (unsigned long long)(count * a2), where, + refused ? "refused by HOS_GPU_ALLOC_TEST_FAIL" : "failed", (unsigned long long)lr); + int ok = 0; + /* (the heaps it was made in: their parents up to the root, the free left in them after their children) */ + uint64_t h = heap; + for (int up = 0; up < 8 && readable(h) && !ok; up++) { + const uint64_t parent = recomp_load64(c, h + HEAP_PARENT); + if (!readable(parent) || parent == h) break; + h = parent; + const uint64_t f = free_size(c, h); + if (f != ~0ULL && f < count * a2 + 0x2000) continue; + ok = gpu_alloc_in(c, game, a0, a1, a2, lr, heap, h); + if (say) { + describe(c, h, where, sizeof where); + hos_log("oot: GPU memory: %s in its parent %s", ok ? "made" : "not made either", where); + } + } + if (!ok && heap) { /* (the game's own default when no heap is named: the thread's heap, or the root) */ + ok = gpu_alloc_in(c, game, a0, a1, a2, lr, heap, 0); + if (say) hos_log("oot: GPU memory: %s in the game's default heap", ok ? "made" : "not made either"); + } + if (ok) count_moved(2, count * a2, say); + if (!ok && say) hos_log("oot: GPU memory: no heap had room: the game goes on without it (and crashes writing to it)"); +} + +/* ---- the CPU buffers: the heap's tryAlloc at 0x8045690c or 0x80456da0 (lr 0x80456910, 0x80456da4) gave x0 = 0 (or + HOS_GPU_ALLOC_TEST_FAIL refused it: nx2/oot_patches.py then made no call). Tried in each parent with room; x0 the block, + or 0 still ---- */ +int hos_oot_heap_refuse(void) { return test_fail(1); } +void hos_oot_heap_retry(Ctx *c, uint64_t heap, uint64_t size, uint64_t align, uint64_t lr) { + if (!retry_on()) { + hos_log("oot: model buffer, %#llx bytes from heap %#llx, failed: not tried again (HOS_GPU_ALLOC_RETRY=0) (lr %#llx)", + (unsigned long long)size, (unsigned long long)heap, (unsigned long long)lr); + return; + } + char where[160]; + const int say = tell(); + describe(c, heap, where, sizeof where); + if (say) hos_log("oot: model buffer, %#llx bytes from %s, failed (lr %#llx)", (unsigned long long)size, where, + (unsigned long long)lr); + uint64_t h = heap, got = 0; + for (int up = 0; up < 8 && readable(h) && !got; up++) { + const uint64_t parent = recomp_load64(c, h + HEAP_PARENT); + if (!readable(parent) || parent == h) break; + h = parent; + const uint64_t f = free_size(c, h), fn = heap_method(c, h, VT_TRY_ALLOC); + if ((f != ~0ULL && f < size + 0x2000) || !fn || !a64_lookup(fn)) continue; + got = guest_call(c, fn, h, size, align, lr); + if (say) { + describe(c, h, where, sizeof where); + hos_log("oot: model buffer: %s in its parent %s", got ? "made" : "not made either", where); + } + } + if (got) count_moved(lr == 0x80456da4ULL, size, say); + if (!got && say) hos_log("oot: model buffer: no heap had room: the game goes on without it (and crashes writing to it)"); + c->x[0] = got; +} diff --git a/host/oot_menu.c b/host/oot_menu.c index 8bc2a97..234366d 100644 --- a/host/oot_menu.c +++ b/host/oot_menu.c @@ -1093,4 +1093,8 @@ int hos_oot_options_front(void) { const int64_t t = __atomic_load_n(&options_seen_ns, __ATOMIC_RELAXED); return t && hos_now_ns() - t < 200000000LL; } +/* ((Triforce, 11-18) the Mods entry under the game's lists is the Windows half's (mods_list_tick): here no list's last button + has the cursor, so nn_hid.c never keeps a Down from the game. Without it every Linux build of v0.5.0 stopped at the link, + "undefined reference to hos_oot_mods_down" (#bug-tracker, 11-18). tests/test_oot_menu_linux_half.py) */ +int hos_oot_mods_down(void) { return 0; } #endif diff --git a/host/oot_sprint.h b/host/oot_sprint.h index 35cd8bb..de7d81d 100644 --- a/host/oot_sprint.h +++ b/host/oot_sprint.h @@ -4,11 +4,13 @@ right after it loads those flags (nx2/oot_patches.py patch_sprint), and they put the bit in while the button's sprint is on: the game's own sprint then runs, with its speed, animation, camera and every rule of its own that ends it. - The button: the left stick's click (which the game does not use) or B, the bottom face button of the Nintendo layout. + The button: the left stick's click or B, the bottom face button of the Nintendo layout. Toggle: a press starts the sprint, the next press or Link's state ends it. Hold: sprint while it is held. During gameplay the button is kept from the game, so it does not also do its own action; B only for a press that begins while Link moves, and then until it is let go, so B puts the weapon away as usual otherwise and the game never sees - half a press. A press made in a menu, a cutscene or a load (no update of Link within OOT_SPRINT_POLLS polls of the + half a press. ((Triforce, 11-18) The stick's click only for a press that begins while Link is in a state a sprint can + be in (OOT_SPRINT_STOP is not): the game does use it, to hide the text box in a conversation, where Link's update goes + on, and with the click kept from it there that did nothing (#bug-tracker 11-18).) A press made in a menu, a cutscene or a load (no update of Link within OOT_SPRINT_POLLS polls of the controller) starts nothing. With the game's Swap Jump Controls B is the jump: the stick's click is the one to use. Here: the logic alone, for tests/test_sprint.py. host/oot_sprint.c reads Link and the settings. */ @@ -67,8 +69,9 @@ typedef struct { int moved; /* step only: the sprint has moved since it started */ double wait_frames; /* step only: 60 Hz frames stood still */ int last_button; /* step only: the setting at the last update */ + int sprintable; /* Link's state at his last update was one a sprint can be in (not OOT_SPRINT_STOP) */ } OotSprint; -#define OOT_SPRINT_INIT {0, 0, 0, OOT_SPRINT_POLLS, 0, 0, 0, 0, 0, 0.0, -1} +#define OOT_SPRINT_INIT {0, 0, 0, OOT_SPRINT_POLLS, 0, 0, 0, 0, 0, 0.0, -1, 0} #define OOT_SPRINT_GET(f) __atomic_load_n(&(f), __ATOMIC_SEQ_CST) #define OOT_SPRINT_SET(f, v) __atomic_store_n(&(f), (v), __ATOMIC_SEQ_CST) @@ -91,7 +94,7 @@ static inline void oot_sprint_pad(OotSprint *s, int button, uint64_t *buttons) { /* A press is decided at its start, for its whole length: kept from the game, and the sprint's, when it begins in gameplay (B: while Link moves); else the game's alone, and no sprint's */ if (now & ~s->last & bit) { - const int mine = gameplay && (button != OOT_SPRINT_B || OOT_SPRINT_GET(s->moving)); + const int mine = gameplay && (button == OOT_SPRINT_B ? OOT_SPRINT_GET(s->moving) : OOT_SPRINT_GET(s->sprintable)); OOT_SPRINT_SET(s->latched, mine); if (mine) __atomic_fetch_or(&s->pressed, bit, __ATOMIC_SEQ_CST); } else if (!(now & bit)) { @@ -112,6 +115,7 @@ static inline int oot_sprint_step(OotSprint *s, int kind, int button, int hold, int on = OOT_SPRINT_GET(s->active); if (OOT_SPRINT_GET(s->zl_held)) kind = OOT_SPRINT_STOP; /* targeting: the game's own stance */ OOT_SPRINT_SET(s->moving, kind == OOT_SPRINT_MOVE); + OOT_SPRINT_SET(s->sprintable, kind != OOT_SPRINT_STOP); if (button == OOT_SPRINT_OFF) { on = 0; } else if (hold) { diff --git a/host/prompts.c b/host/prompts.c index 471e231..4bbab4d 100644 --- a/host/prompts.c +++ b/host/prompts.c @@ -563,10 +563,16 @@ static int build(char *out, size_t cap) { char dir[MAX_PATH], path[MAX_PATH]; int face; if (!setup(dir, sizeof dir, &face)) return 0; - /* (a mod's size table is served, not ours: the faces' fonts, larger than the game's, do not fit it) */ + /* (a mod's size table is served, not ours: the faces' fonts, larger than the game's, do not fit it. ((Triforce, 11-18) + unless the lifted code asks host/mods.c's hos_mods_table_size for each file's room, which gives the font its own: so + only when that hook has not run in this game, an older build of it) */ + extern volatile LONG hos_mods_size_hook_seen; if (hos_mods_has(SIZES_PATH, ".rsizetable.zs")) { - hos_log("prompts: a mod has the resource size table: the game's own icons in texts"); - return 0; + if (!hos_mods_size_hook_seen) { + hos_log("prompts: a mod has the resource size table: the game's own icons in texts"); + return 0; + } + hos_log("prompts: a mod has the resource size table: the font's room comes from the size hook (host/mods.c)"); } snprintf(path, sizeof path, "%sprompts/%s/External.bfotf", dir, faces[face]); long n; @@ -655,6 +661,15 @@ static int build_camera(char *out, size_t cap) { return ok; } +int hos_prompts_romfs(const char *key, char *out, size_t cap); +/* ((Triforce, 11-18) mods.c's size hook: for the font's key (lower case, '/', packed name), the font built for this run + (built now if it was not), 1 with one; 0 for any other key or with no prompts. A mod's own font is the mod's: the hook + finds that one first) */ +int hos_prompts_font_file(const char *key, char *out, size_t cap) { + if (strcmp(key, FONT_PATH)) return 0; + return hos_prompts_romfs(FONT_PATH, out, cap); +} + /* mods.c: the file for a RomFS path the prompts replace (key: lower case, '/'), 1 with one. Asked only when no mod has the file: a mod's own wins */ int hos_prompts_romfs(const char *key, char *out, size_t cap) { diff --git a/nx2/make_game.py b/nx2/make_game.py index 82bde97..d1dda64 100644 --- a/nx2/make_game.py +++ b/nx2/make_game.py @@ -480,6 +480,7 @@ def check(a, work): bid = build_id(os.path.join(probe, "exefs", "main")) shutil.rmtree(probe, ignore_errors=True) else: + refuse_packed_romfs(a.romfs) # (Setup passes the romfs folder too since 11-18) bid = build_id(os.path.join(a.exefs, "main")) if bid.startswith(BUILD_ID): say("check", "ok", "This is the version Triforce supports (build %s)." % bid[:8]) @@ -1157,6 +1158,19 @@ def refuse_dump_links(folder): % (folder, path)) +PACKED_ROMFS = ("This romfs folder is NX2navi's: it holds the game's data packed in one file (romfs.bin), which Triforce cannot " + "read, so the game would crash at start. Choose your game file (.nup) instead, or romfs and exefs folders with " + "the game's files unpacked in them.") + + +def refuse_packed_romfs(folder): + """(Triforce, 11-18) NX2navi keeps the game's RomFS as one image, \\romfs\\romfs.bin (+ index.json), not as + files: chosen as the romfs folder, it passed (only the exefs is checked) and the game was built without its data. It then + crashed at start in the game's own setup, a 0x268-byte allocation coming back null (f_80c4afa0: two players, 11-18)""" + if folder and os.path.isfile(os.path.join(folder, "romfs.bin")): + fail(PACKED_ROMFS) + + def long_path(p): """(Windows) p as an extended-length path (\\\\?\\...): a game folder whose deepest file passes 260 characters still links and is read (#1468); Python takes these without Windows' long-paths setting. Elsewhere, or one already: as is (nup.py's)""" @@ -1586,15 +1600,28 @@ def fetch_streamline_sdk(a): def place_dlssg_runtime(out): """(NVIDIA DLSS frame generation) a game built with NVIDIA's Streamline (HOOKSHOT_STREAMLINE_SDK, as nx2/build.py takes it) gets Streamline's DLLs, nvngx_dlssg.dll and their license texts in its dlssg folder, where gpu/vk/vk_dlssg.cpp looks. A - folder built with them before and without them now loses them. Returns the folder, or None""" + folder built with them before and without them now loses them. Returns the folder, or None + + (Triforce, 11-18) DLSS's own runtime too, a copy of the one place_ngx_runtime put in ngx (so this runs after it): with + Streamline up, NGX looks for nvngx_dlss.dll only in the dlssg folder, and without it there DLSS upscaling did not start + ("not available on this GPU (0xbad00004)"), so the menu offered no DLSS while frame generation was on""" sl = streamline_script() if sl is None: return None sdk = os.environ.get("HOOKSHOT_STREAMLINE_SDK", "") + dlss_copy = os.path.join(out, "dlssg", "nvngx_dlss.dll") if LINUX or not sl.complete(sdk): + with contextlib.suppress(OSError): # (before Streamline's files: the folder is left empty and goes with them) + os.remove(dlss_copy) sl.remove_runtime(out) return None dst = sl.install_runtime(sdk, out) + dlss = os.path.join(out, "ngx", "nvngx_dlss.dll") + if os.path.isfile(dlss): + copy_writable(dlss, dlss_copy) + else: # (a build without NVIDIA DLSS: no stale copy of an earlier one's) + with contextlib.suppress(OSError): + os.remove(dlss_copy) print("NVIDIA DLSS frame generation: %s (NVIDIA's, under NVIDIA's licenses: never share this game folder)" % dst, flush=True) return dst @@ -1812,6 +1839,7 @@ def main(a=None): fail("This game folder has no copy of the game files to update from: it was built before updates came in. Choose your game files " "in Triforce Setup and build into the same folder once; updates after that work by themselves.") check_cache_paths(out, (kept, romfs)) + refuse_packed_romfs(romfs) # (a folder built from NX2navi's before the check: it never started) shutil.copytree(kept, exefs) check_modules(exefs) stand_in_damaged_files(out, romfs) # (a folder unpacked by an earlier builder from a 23.3 GB dump) @@ -1871,6 +1899,7 @@ def main(a=None): stand_in_damaged_files(out, romfs, unpacked_seen) else: check_cache_paths(out, (kept, romfs)) + refuse_packed_romfs(a.romfs) refuse_dump_links(a.exefs) refuse_dump_links(a.romfs) shutil.copytree(a.exefs, exefs) # (unpacked in place: the user's copy stays as it is) diff --git a/nx2/oot_patches.py b/nx2/oot_patches.py index 42b37f9..1b8b511 100644 --- a/nx2/oot_patches.py +++ b/nx2/oot_patches.py @@ -16,6 +16,9 @@ Each is a runtime switch where it can be, so the game's own path stays reachable before any recompute. So the fixed path kept the dynamic path's last scale (0.75 docked: 1536x864 at every setting). Flag 1 now also asks for the recompute, and on the fixed path the flags +0xe20 bits 3/4 (a hardcoded 0.75 / 0.5 in place of the scale) are not looked at. +- The OptimizeChangeBank thread's allocations tried again when their heap is full (host/oot_heap_retry.c): f_804566e0's two + calls of f_80522050, its GPU memory (GPU_ALLOC_SITES), and its two tryAllocs of CPU buffers (HEAP_ALLOC_SITES). They went on + with a null pointer and crashed writing through it (issue 1697). The supported build only. """ import glob import os @@ -690,6 +693,46 @@ def patch_scene_format(s): return s.replace(RGBA16F_OLD, RGBA16F_NEW, 1).replace('#include "a64.h"\n', '#include "a64.h"\n' + RGBA16F_DECL, 1) +# (from TomShafford/Hookshot 55d68e8cd6 and 65c1998222) f_804566e0's two calls of f_80522050, the GPU memory a model's material +# block is made with, made through host/oot_heap_retry.c hos_oot_gpu_alloc, which calls the game's own and tries a full heap's +# failure again in the heap's parents. f_80522050's other callers stay as they are. +GPU_ALLOC_SITES = (0x80456f1c, 0x80457098) # (the calls' return addresses) +GPU_ALLOC_OLD = "c->x[30] = %#xULL; f_80522050(c);" +GPU_ALLOC_NEW = "c->x[30] = %#xULL; hos_oot_gpu_alloc(c, f_80522050); /* (nx2/oot_patches.py: host/oot_heap_retry.c) */" +GPU_ALLOC_DECL = "void hos_oot_gpu_alloc(Ctx *A64_NOESC c, A64Fn game);\n" +# f_804566e0's tryAllocs of the CPU buffers at its MatBlock's +200 (its bump allocator's memory) and +168, each sized for the +# model (x0 the heap, x1 the size, x2 the alignment): a null result tried in the heap's parents (hos_oot_heap_retry) +HEAP_ALLOC_SITES = (0x80456910, 0x80456da4) +HEAP_ALLOC_OLD = "{ uint64_t _t = c->x[8]; c->x[30] = %#xULL; A64_ICALL(c, _t); }\n" +HEAP_ALLOC_NEW = ("{ const uint64_t _hh = c->x[0], _hn = c->x[1], _ha = c->x[2]; /* (nx2/oot_patches.py: host/oot_heap_retry.c) */\n" + " if (hos_oot_heap_refuse()) c->x[0] = 0;\n" + " else { uint64_t _t = c->x[8]; c->x[30] = %#xULL; A64_ICALL(c, _t); }\n" + " if (!c->x[0]) hos_oot_heap_retry(c, _hh, _hn, _ha, %#xULL); }\n") +HEAP_ALLOC_DECL = "int hos_oot_heap_refuse(void);\nvoid hos_oot_heap_retry(Ctx *c, uint64_t heap, uint64_t size, uint64_t align, uint64_t lr);\n" + + +def patch_heap_retry(s): + """The four places above in one file's text: (text, [GPU sites, CPU sites] made now or found made). apply() wants + [2, 2] over the whole lift and warns otherwise""" + found = [0, 0] + decls = [] + for kind, sites, old, new, decl in ((0, GPU_ALLOC_SITES, GPU_ALLOC_OLD, GPU_ALLOC_NEW, GPU_ALLOC_DECL), + (1, HEAP_ALLOC_SITES, HEAP_ALLOC_OLD, HEAP_ALLOC_NEW, HEAP_ALLOC_DECL)): + for lr in sites: + o, n = old % lr, (new % lr if kind == 0 else new % (lr, lr)) + if n in s: + found[kind] += 1 + elif s.count(o) == 1 and s.count('#include "a64.h"\n') == 1: + s = s.replace(o, n, 1) + found[kind] += 1 + if decl not in decls: + decls.append(decl) + for decl in decls: + if decl not in s: + s = s.replace('#include "a64.h"\n', '#include "a64.h"\n' + decl, 1) + return s, found + + def apply(gen, *, protect_hearts=False, required=False): """required: the lift of the supported build (LIFE_BUILD_ID), whole: a missing output-size edit stops it (apply_output_size). The other patches only warn when their place is not found once.""" @@ -712,6 +755,7 @@ def apply(gen, *, protect_hearts=False, required=False): sprint_sites = 0 # (patch_sprint: its two calls) zstd_sites = 0 # (patch_host_zstd: its two calls, the supported build's alone) mod_size_sites = 0 # ((Triforce) patch_mod_sizes: its one call, the supported build's alone) + heap_sites = [0, 0] # (the GPU memory's sites, the CPU buffers': patch_heap_retry, the supported build only) for path in sorted(glob.glob(os.path.join(glob.escape(gen), "code_*.c"))): with open(path, encoding="utf-8", newline="") as f: s = f.read() @@ -783,6 +827,12 @@ def apply(gen, *, protect_hearts=False, required=False): step_files.append(path) if sites: print("oot_patches: the frame's time at %d step divide(s) in %s" % (sites, os.path.basename(path))) + heap, found = patch_heap_retry(out) if protect_hearts else (out, [0, 0]) + heap_sites[0] += found[0] + heap_sites[1] += found[1] + if heap != out: + print("oot_patches: heap-full retries of the OptimizeChangeBank allocations in", os.path.basename(path)) + out = heap out, whole = patch_whole_frames(out) whole_sites += out.count(WHOLE_MARK) # (made now, or by an earlier run over this file) if whole: @@ -847,6 +897,9 @@ def apply(gen, *, protect_hearts=False, required=False): print("oot_patches: WARNING: the resource size table's lookup was found at %d of 1 place" % mod_size_sites) if protect_hearts and zstd_sites != len(ZSTD_SITES): # (without them HOS_HOST_ZSTD does nothing, or half: host/hos_zstd.c) print("oot_patches: WARNING: the host's zstd decoder's calls were found at %d of %d places" % (zstd_sites, len(ZSTD_SITES))) + if protect_hearts and heap_sites != [len(GPU_ALLOC_SITES), len(HEAP_ALLOC_SITES)]: # (the supported build's functions) + print("oot_patches: WARNING: the OptimizeChangeBank allocations were found %s times, not once each: a full heap is " + "not tried again" % heap_sites) for what, n in done.items(): if n != 1: print("oot_patches: WARNING: the %s was found %d times" % (what, n)) diff --git a/setup/Shell.Test.cs b/setup/Shell.Test.cs index 7412f84..80bb850 100644 --- a/setup/Shell.Test.cs +++ b/setup/Shell.Test.cs @@ -162,6 +162,12 @@ sealed partial class Shell rows["read"].Done(); rows["lift"].Done(); rows["compile"].Begin(); rows["compile"].Progress(0.41); SetButtons(cancel: true); await Shot(dir, "13-build"); + // ((Triforce, 11-18) the shaders' step after the folder: its own row and bar, and the note that says what it is) + rows["compile"].Done(); rows["finish"].Done(); rows["shaders"].Begin(); rows["shaders"].Progress(0.37); + BuildNote.Text = T(S.ShadersNote) + " " + T(S.Elapsed) + ": 1 h 17 min"; + await Shot(dir, "13b-build-shaders"); + foreach (var r in rows.Values) r.Reset(); + rows["read"].Done(); rows["lift"].Done(); rows["compile"].Begin(); rows["compile"].Progress(0.41); Log.Text = string.Join("\n", new[] { diff --git a/setup/Shell.cs b/setup/Shell.cs index 95fbc5d..0afd132 100644 --- a/setup/Shell.cs +++ b/setup/Shell.cs @@ -30,8 +30,12 @@ sealed partial class Shell // to 1.1 GB in 13,230 files, 3.9 times its size) const int ToolsUnpackedTimes = 5; // (the build's steps, and the stone each one lights when it is done; the last one makes the whole crest gleam) + // ((Triforce, 11-18) "shaders": the game's --prepare-shaders after the folder is put together (nx2/make_game.py prepare_shaders; + // its "@@stage shaders" and progress, gpu/vk/vk_main.cpp). Without a row it ran under four finished steps with no bar: an + // update on an AMD card sat there over an hour and looked stuck) static readonly (string Id, S Text, int Stone)[] Stages = - { ("read", S.Read, Art.Emerald), ("lift", S.Lift, Art.Sapphire), ("compile", S.Compile, Art.Ruby), ("finish", S.Finish, -1) }; + { ("read", S.Read, Art.Emerald), ("lift", S.Lift, Art.Sapphire), ("compile", S.Compile, Art.Ruby), ("finish", S.Finish, -1), + ("shaders", S.Shaders, -1) }; // parts of Main.xaml readonly Grid Root, InstallPage, FetchPage, SetupPage, BuildPage, NupRow, AskLayer; @@ -844,7 +848,8 @@ sealed partial class Shell var tools = Tools.Find(); if (tools == null) { Say(T(S.ToolsMissing), "Bad", "Bad"); return; } var args = new List { "--check" }; - args.AddRange(UseNup.IsChecked == true ? new[] { "--nup", NupPath.Text } : new[] { "--exefs", ExefsPath.Text }); + // (the romfs folder too: NX2navi's holds one packed romfs.bin, which the builder refuses, nx2/make_game.py refuse_packed_romfs) + args.AddRange(UseNup.IsChecked == true ? new[] { "--nup", NupPath.Text } : new[] { "--exefs", ExefsPath.Text, "--romfs", RomfsPath.Text }); var (ok, text) = await Runner.Check(tools, args); if (serial != checkSerial) return; sourceOk = ok; @@ -960,6 +965,7 @@ sealed partial class Shell if (current != null && rows.TryGetValue(current, out var was)) was.Done(); current = id; if (rows.TryGetValue(id, out var row)) row.Begin(); + ShowClock(); // (the note under the title is the step's: the shaders' says what that step is) } else if (line.StartsWith("@@progress ")) { @@ -1042,7 +1048,7 @@ sealed partial class Shell void ShowClock() { - if (runner != null) BuildNote.Text = T(S.Patience) + " " + T(S.Elapsed) + ": " + Span(elapsed.Elapsed); + if (runner != null) BuildNote.Text = (current == "shaders" ? T(S.ShadersNote) : T(S.Patience)) + " " + T(S.Elapsed) + ": " + Span(elapsed.Elapsed); } static string Span(TimeSpan t) => t.TotalHours >= 1 ? (int)t.TotalHours + " h " + t.Minutes + " min" : t.TotalMinutes >= 1 ? t.Minutes + " min" : t.Seconds + " s"; diff --git a/setup/Strings.cs b/setup/Strings.cs index a22ef79..77bbd76 100644 --- a/setup/Strings.cs +++ b/setup/Strings.cs @@ -23,6 +23,8 @@ enum S Download, Updating, UpdateFailed, // (the report of a build that stopped, English only as well) CopyReport, ReportCopied, ReportNotCopied, + // ((Triforce, 11-18) the shaders' step after the folder is put together: English only as well) + Shaders, ShadersNote, } static class Strings @@ -38,6 +40,8 @@ static class Strings "The build stopped", "Build cancelled", "You can start again from the same folder.", "Stop the build and close?", "Language", "Stop and close", "Keep building", "Downloading the new version", "Updating your game", "The update could not be downloaded", "Copy report", "Report copied", "The clipboard is in use: try again", + "Preparing shaders", + "Your game is built; its shaders are being prepared once so it doesn't stutter later. On AMD graphics cards this can take an hour or more.", }; static readonly string[] Fr = { diff --git a/tests/f2i_immediate/CMakeLists.txt b/tests/f2i_immediate/CMakeLists.txt index 47e5e71..e525789 100644 --- a/tests/f2i_immediate/CMakeLists.txt +++ b/tests/f2i_immediate/CMakeLists.txt @@ -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}) diff --git a/tests/f2i_immediate/README.md b/tests/f2i_immediate/README.md index 919dfd6..8cf3e39 100644 --- a/tests/f2i_immediate/README.md +++ b/tests/f2i_immediate/README.md @@ -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 diff --git a/tests/f2i_immediate/fixture.cpp b/tests/f2i_immediate/fixture.cpp index dae9d64..103e7c4 100644 --- a/tests/f2i_immediate/fixture.cpp +++ b/tests/f2i_immediate/fixture.cpp @@ -1,6 +1,8 @@ // Authored instructions only. No dump, native program, GPU or game is read. +#include #include #include +#include #include #include #include @@ -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 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(bits); }; + const auto b = [](f32 x) { return std::bit_cast(x); }; + std::array 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 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 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 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(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(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(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(-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); } diff --git a/tests/input_background_test.c b/tests/input_background_test.c index a9425b1..09b15d5 100644 --- a/tests/input_background_test.c +++ b/tests/input_background_test.c @@ -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; } diff --git a/tests/run_all.py b/tests/run_all.py index 088a114..6df5d11 100644 --- a/tests/run_all.py +++ b/tests/run_all.py @@ -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"]) diff --git a/tests/test_amd_subgroup_profiles.py b/tests/test_amd_subgroup_profiles.py index 44d0231..17b4d0a 100644 --- a/tests/test_amd_subgroup_profiles.py +++ b/tests/test_amd_subgroup_profiles.py @@ -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(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& codes_of(") + section(warp, "Id HostThreadId(", "Id GuestLane(") + section(warp, "bool UseShuffleFallback(", "Id ShuffleAbsolute(") + check_stamp + CASES) diff --git a/tests/test_audio_in.py b/tests/test_audio_in.py index a697599..1adfb79 100644 --- a/tests/test_audio_in.py +++ b/tests/test_audio_in.py @@ -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: diff --git a/tests/test_bc_raw_targets.py b/tests/test_bc_raw_targets.py index 5a68164..5eca8b0 100644 --- a/tests/test_bc_raw_targets.py +++ b/tests/test_bc_raw_targets.py @@ -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() diff --git a/tests/test_low_memory_notice.py b/tests/test_low_memory_notice.py index f2ad2bb..aa28bb8 100644 --- a/tests/test_low_memory_notice.py +++ b/tests/test_low_memory_notice.py @@ -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(); diff --git a/tests/test_lower_lds_after_sts.py b/tests/test_lower_lds_after_sts.py new file mode 100644 index 0000000..6da5137 --- /dev/null +++ b/tests/test_lower_lds_after_sts.py @@ -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 +#include +#include +#include +#include +#include +#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 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() diff --git a/tests/test_menu_utf8.py b/tests/test_menu_utf8.py index 67a9455..5294758 100644 --- a/tests/test_menu_utf8.py +++ b/tests/test_menu_utf8.py @@ -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"); } ''' diff --git a/tests/test_mesh_point_topology.py b/tests/test_mesh_point_topology.py index 18fe3ca..384dd0e 100644 --- a/tests/test_mesh_point_topology.py +++ b/tests/test_mesh_point_topology.py @@ -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: diff --git a/tests/test_mod_code.py b/tests/test_mod_code.py index c947b42..ef90570 100644 --- a/tests/test_mod_code.py +++ b/tests/test_mod_code.py @@ -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"), diff --git a/tests/test_mouse_native.py b/tests/test_mouse_native.py index adfd188..05da4e1 100644 --- a/tests/test_mouse_native.py +++ b/tests/test_mouse_native.py @@ -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; } diff --git a/tests/test_oot_heap_retry.py b/tests/test_oot_heap_retry.py new file mode 100644 index 0000000..d307137 --- /dev/null +++ b/tests/test_oot_heap_retry.py @@ -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=: 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 +#include +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 +#include +#include +#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() diff --git a/tests/test_oot_menu_linux_half.py b/tests/test_oot_menu_linux_half.py new file mode 100644 index 0000000..0ba27e2 --- /dev/null +++ b/tests/test_oot_menu_linux_half.py @@ -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)) diff --git a/tests/test_packed_romfs.py b/tests/test_packed_romfs.py new file mode 100644 index 0000000..8655f60 --- /dev/null +++ b/tests/test_packed_romfs.py @@ -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, \\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)) diff --git a/tests/test_prompts_mod_sizetable.py b/tests/test_prompts_mod_sizetable.py new file mode 100644 index 0000000..125775c --- /dev/null +++ b/tests/test_prompts_mod_sizetable.py @@ -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)) diff --git a/tests/test_sprint.py b/tests/test_sprint.py index 9b3ff36..0386a54 100644 --- a/tests/test_sprint.py +++ b/tests/test_sprint.py @@ -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); diff --git a/tests/test_streamline_sdk.py b/tests/test_streamline_sdk.py index 66055de..2727483 100644 --- a/tests/test_streamline_sdk.py +++ b/tests/test_streamline_sdk.py @@ -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', diff --git a/tests/test_thread_wake_cleanup.py b/tests/test_thread_wake_cleanup.py new file mode 100644 index 0000000..2d53c4f --- /dev/null +++ b/tests/test_thread_wake_cleanup.py @@ -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 +#include +#include + +#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)