Triforce v0.8.0

Evil HD (SMAA Ultra, 16x filtering, full textures, ambient occlusion with bounce light, sunlight shafts, foliage drawn 4x as far, the remastered texture pack's loader and the launcher's download of it), FSR 3.1 frame generation for every GPU, Intel XeSS, FSR 4 model choice, ReShade presets, Auto (this PC), HUD placement, the options menu in 13 languages, a second input for every control, hold to toggle, DualSense speaker and haptics, NFC from the player's own dumps, the above-60 fps game fixes, the Debug tab's flight and Inventory fixes, the launcher's full settings, and over 250 fixes.
This commit is contained in:
ruckus7382sfgsgdf
2026-11-25 10:21:32 -06:00
parent 861702476a
commit 4fa4f9188b
881 changed files with 179428 additions and 15581 deletions

View File

@@ -15,6 +15,7 @@
#include <vector>
#include "common/common_types.h"
#include "shader_recompiler/frontend/ampere/geometry_exit.h"
#include "shader_recompiler/frontend/ampere/program_address_map.h"
#include "shader_recompiler/frontend/ampere/register_pressure.h"
namespace Shader::Ampere {
@@ -104,13 +105,23 @@ inline LoweredProgram LowerToMaxwellWith(std::span<const Instruction> code, u32
const u32 target = relocation.output_target != ~0U ? relocation.output_target :
result.source_instruction_offsets[static_cast<size_t>(relocation.target)];
const s64 relative = s64{address_of(target)} - address_of(relocation.output_index) - 8;
if (relative < -0x800000 || relative > 0x7fffff) return fail(index, "lowered branch distance");
result.instructions[relocation.output_index] |= (static_cast<u64>(relative) & 0xffffff) << 20;
u64& word = result.instructions[relocation.output_index];
if (relative < -0x800000 || relative > 0x7fffff) {
// ((Triforce) past the reach of BRA and CAL (a 24-bit offset, 8 MiB either way: about 786,000 lowered
// instructions): their absolute forms, JMP and JCAL, which hold the target's address in 32 bits (20-51;
// the frontend reads them so, IsAbsoluteJump). The predicate and the flow test stay as they are
const u64 op = word >> 52;
if (op != 0xe24 && op != 0xe26) return fail(index, "lowered branch distance");
word = (word & ~(u64{0xfff} << 52)) | (u64{op == 0xe24 ? 0xe21U : 0xe22U} << 52) |
(u64{address_of(target)} << 20);
continue;
}
word |= (static_cast<u64>(relative) & 0xffffff) << 20;
}
result.source_size = (index + 1) * sizeof(Instruction);
return result;
};
if (code.empty() || code.size() > 65536) return fail(0, "invalid native program size");
if (code.empty() || code.size() > MaximumNativeInstructions) return fail(0, "invalid native program size");
std::optional<GeometryExitEnvelope> geometry_exit;
if (code.front().Bits(0, 12) == 0x34e) {
if (!geometry_stage) return fail(0, "native program counter load outside geometry stage");
@@ -3823,11 +3834,14 @@ inline LoweredProgram LowerToMaxwellWith(std::span<const Instruction> code, u32
const s64 relative = static_cast<s64>(raw << 14) >> 14;
if (s64{index} * 16 + 16 + relative != 0)
return fail(index, "native computed branch relative base");
const s64 offset = -s64{address_of(static_cast<u32>(result.instructions.size()))} - 8;
if (offset < -0x800000) return fail(index, "native computed branch range");
// ((Triforce) BRX goes to the table's value plus its own address plus its offset, which reaches back only 8 MiB
// (24 bits). Past that, JMX: the value plus the offset alone, -8 (the frontend's AnalyzeBRX adds the 8 back)
const s64 back = -s64{address_of(static_cast<u32>(result.instructions.size()))} - 8;
const bool absolute = back < -0x800000;
const s64 offset = absolute ? -8 : back;
const u32 source = opcode == 0x958 ? uniform_register(insn.Bits(24, 8)) : insn.Bits(24, 8);
if (source == ~0U) return fail(index, "native uniform computed branch source");
emit(0xe25000000000000fULL | predicate | (u64{source} << 8) |
emit((absolute ? 0xe20000000000000fULL : 0xe25000000000000fULL) | predicate | (u64{source} << 8) |
((static_cast<u64>(offset) & 0xffffff) << 20));
computed_branch = true;
break;
@@ -3928,7 +3942,17 @@ inline LoweredProgram LowerToMaxwellWith(std::span<const Instruction> code, u32
if (target < 0 || target % 16) return fail(index, "native branch destination alignment");
relocations.push_back({static_cast<u32>(result.instructions.size()), target / 16});
emit(0xe24000000000000fULL | branch_predicate);
if (computed_branch && target == s64{index} * 16 && branch_predicate == (u64{7} << 16))
// ((Triforce) a loop that leaves only by predicated EXITs: the program ends at the closing self-branch after the
// loop's unconditional jump back, which nothing falls into (one of OoT's compute programs, its EXITs all @P0)
const auto unconditional_jump = [&](const Instruction& before) {
const u32 op = before.Bits(0, 12);
return before.Bits(12, 4) == 7 && ((op == 0x947 && before.Bits(87, 4) == 7) || op == 0x94d);
};
// (and only when no branch so far goes past it: code a branch reaches behind it is the program's still)
const bool after_jump = index > 0 && call_targets.empty() && unconditional_jump(code[index - 1]) &&
std::none_of(relocations.begin(), relocations.end(),
[&](const auto& r) { return r.target > s64{index}; });
if ((computed_branch || after_jump) && target == s64{index} * 16 && branch_predicate == (u64{7} << 16))
return finish(index); // the closing self-branch
break;
}
@@ -3963,7 +3987,9 @@ inline LoweredProgram LowerToMaxwellWith(std::span<const Instruction> code, u32
const bool write = opcode == 0x927;
const u32 region = insn.Bits(76, 2);
const bool output = insn.Bits(79, 1) != 0;
if ((insn.words[2] & ~0xf800U) != 0 || (region != 0 && region != 3) || write != output ||
// ((Triforce) a read may be of the output region (.O): the program reading back what it wrote (one of OoT's
// task programs reads its payload so); a write is always to it
if ((insn.words[2] & ~0xf800U) != 0 || (region != 0 && region != 3) || (write && !output) ||
(!write && region != 3) || (!write && dest == 255))
return fail(index, "native stage buffer access mode");
emit(0xefc0000000000000ULL | predicate | (write ? u64{insn.Bits(32, 8)} : dest) |

View File

@@ -16,6 +16,7 @@
#include <string_view>
#include <vector>
#include "common/common_types.h"
#include "shader_recompiler/frontend/ampere/program_address_map.h" // ((Triforce) Shader::Ampere::MaximumNativeInstructions)
namespace Shader::AmpereRegmap {
@@ -166,13 +167,26 @@ inline LoweredProgram LowerToMaxwellWith(std::span<const Instruction> code, u32
if (relocation.target > index) return fail(index, "native branch or call leaves captured program");
const u32 target = result.source_instruction_offsets[static_cast<size_t>(relocation.target)];
const s64 relative = s64{address_of(target)} - address_of(relocation.output_index) - 8;
if (relative < -0x800000 || relative > 0x7fffff) return fail(index, "lowered branch distance");
result.instructions[relocation.output_index] |= (static_cast<u64>(relative) & 0xffffff) << 20;
u64& word = result.instructions[relocation.output_index];
if (relative < -0x800000 || relative > 0x7fffff) {
// ((Triforce) past the reach of BRA and CAL (a 24-bit offset, 8 MiB either way: about 786,000 lowered
// instructions): their absolute forms, JMP and JCAL, which hold the target's address in 32 bits (20-51;
// the frontend reads them so, IsAbsoluteJump). The predicate and the flow test stay as they are
const u64 op = word >> 52;
if (op != 0xe24 && op != 0xe26) return fail(index, "lowered branch distance");
word = (word & ~(u64{0xfff} << 52)) | (u64{op == 0xe24 ? 0xe21U : 0xe22U} << 52) |
(u64{address_of(target)} << 20);
continue;
}
word |= (static_cast<u64>(relative) & 0xffffff) << 20;
}
result.source_size = (index + 1) * sizeof(Instruction);
return result;
};
if (code.empty() || code.size() > 65536) return fail(0, "invalid native program size");
if (code.empty() || code.size() > Shader::Ampere::MaximumNativeInstructions) return fail(0, "invalid native program size");
// ((Triforce) 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<u32>(result.instructions.size()));
@@ -230,7 +244,11 @@ inline LoweredProgram LowerToMaxwellWith(std::span<const Instruction> code, u32
opcode == 0x927 || opcode == 0xb9b || opcode == 0x59f || opcode == 0x983 ||
opcode == 0x98e || opcode == 0xb6c || opcode == 0x9a8 || (renameable & (1U << f));
if (reg >= 255 || reg + 3 < reserved_base) continue; // (a quad below may reach up)
if (register_field && reg >= reserved_base) any_high = true;
// A vector load can write reserved registers even when its first destination is below them.
const bool load_dest = f == 0 && (opcode == 0x381 || opcode == 0x981);
const u32 load_size = insn.Bits(73, 3);
const u32 width = load_dest && load_size == 6 ? 4U : load_dest && load_size == 5 ? 2U : 1U;
if (register_field && reg + width > reserved_base) any_high = true;
}
for (u32 f = 0; f < 4 && any_high; ++f) {
const u32 bit = bits[f], reg = insn.Bits(bit, 8);
@@ -1589,6 +1607,7 @@ inline LoweredProgram LowerToMaxwellWith(std::span<const Instruction> 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;
@@ -1611,6 +1630,22 @@ inline LoweredProgram LowerToMaxwellWith(std::span<const Instruction> code, u32
address = 250;
}
const u32 data = opcode == 0x388 ? insn.Bits(32, 8) : static_cast<u32>(dest);
// ((Triforce) 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). As for LDSM, and as lower_to_maxwell.h has it: 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
static const bool lds_barrier = [] {
const char* const e = std::getenv("RX_VK_SHARED_NO_LDS_BARRIER");
return !(e && *e && *e != '0');
}();
if (opcode == 0x388) {
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((opcode == 0x388 ? 0xef58000000000000ULL : 0xef48000000000000ULL) |
predicate | data | (u64{address} << 8) | (u64{insn.Bits(40, 24)} << 20) |
(u64{width} << 48));
@@ -1723,9 +1758,12 @@ inline LoweredProgram LowerToMaxwellWith(std::span<const Instruction> code, u32
return fail(index, "native matrix load mode");
u32 address = insn.Bits(24, 8);
const u64 offset = insn.Bits(40, 24);
// ((Triforce) An address the results overwrite is copied first, to R254: the paths below use R246-R253, and R253
// (the copy's old place) is where each matrix's row address is shuffled to, so matrices 1-3 of an .x2/.x4 shuffled
// matrix 0's address and read the wrong rows (as lower_to_maxwell.h has it: gpu/vk/tests/ldsm_address_test.cpp)
if (address != 255 && address >= dest && address < dest + count) {
emit(0x5c10000000000000ULL | predicate | 253 | (u64{address} << 8) | (u64{255} << 20));
address = 253;
emit(0x5c10000000000000ULL | predicate | 254 | (u64{address} << 8) | (u64{255} << 20));
address = 254;
}
// (hookshot) A warp stores its tiles (STS) and reads them back with LDSM, often with no BAR 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
@@ -3275,11 +3313,14 @@ inline LoweredProgram LowerToMaxwellWith(std::span<const Instruction> code, u32
const s64 relative = static_cast<s64>(raw << 14) >> 14;
if (s64{index} * 16 + 16 + relative != 0)
return fail(index, "native computed branch relative base");
const s64 offset = -s64{address_of(static_cast<u32>(result.instructions.size()))} - 8;
if (offset < -0x800000) return fail(index, "native computed branch range");
// ((Triforce) BRX goes to the table's value plus its own address plus its offset, which reaches back only 8 MiB
// (24 bits). Past that, JMX: the value plus the offset alone, -8 (the frontend's AnalyzeBRX adds the 8 back)
const s64 back = -s64{address_of(static_cast<u32>(result.instructions.size()))} - 8;
const bool absolute = back < -0x800000;
const s64 offset = absolute ? -8 : back;
const u32 source = opcode == 0x958 ? uniform_register(insn.Bits(24, 8)) : insn.Bits(24, 8);
if (source == ~0U) return fail(index, "native uniform computed branch source");
emit(0xe25000000000000fULL | predicate | (u64{source} << 8) |
emit((absolute ? 0xe20000000000000fULL : 0xe25000000000000fULL) | predicate | (u64{source} << 8) |
((static_cast<u64>(offset) & 0xffffff) << 20));
computed_branch = true;
break;
@@ -3406,7 +3447,17 @@ inline LoweredProgram LowerToMaxwellWith(std::span<const Instruction> code, u32
if (target < 0 || target % 16) return fail(index, "native branch destination alignment");
relocations.push_back({static_cast<u32>(result.instructions.size()), target / 16});
emit(0xe24000000000000fULL | branch_predicate);
if (computed_branch && target == s64{index} * 16 && branch_predicate == (u64{7} << 16))
// ((Triforce) a loop that leaves only by predicated EXITs: the program ends at the closing self-branch after the
// loop's unconditional jump back, which nothing falls into (one of OoT's compute programs, its EXITs all @P0)
const auto unconditional_jump = [&](const Instruction& before) {
const u32 op = before.Bits(0, 12);
return before.Bits(12, 4) == 7 && ((op == 0x947 && before.Bits(87, 4) == 7) || op == 0x94d);
};
// (and only when no branch so far goes past it: code a branch reaches behind it is the program's still)
const bool after_jump = index > 0 && call_targets.empty() && unconditional_jump(code[index - 1]) &&
std::none_of(relocations.begin(), relocations.end(),
[&](const auto& r) { return r.target > s64{index}; });
if ((computed_branch || after_jump) && target == s64{index} * 16 && branch_predicate == (u64{7} << 16))
return finish(index); // the closing self-branch
break;
}
@@ -3419,7 +3470,9 @@ inline LoweredProgram LowerToMaxwellWith(std::span<const Instruction> code, u32
const bool write = opcode == 0x927;
const u32 region = insn.Bits(76, 2);
const bool output = insn.Bits(79, 1) != 0;
if ((insn.words[2] & ~0xf800U) != 0 || (region != 0 && region != 3) || write != output ||
// ((Triforce) a read may be of the output region (.O): the program reading back what it wrote (one of OoT's
// task programs reads its payload so); a write is always to it
if ((insn.words[2] & ~0xf800U) != 0 || (region != 0 && region != 3) || (write && !output) ||
(!write && region != 3) || (!write && dest == 255))
return fail(index, "native stage buffer access mode");
emit(0xefc0000000000000ULL | predicate | (write ? u64{insn.Bits(32, 8)} : dest) |

View File

@@ -8,6 +8,12 @@
namespace Shader::Ampere {
constexpr u64 NativeAddressMapMagic = 0x50414d4e4544454eULL;
// (Triforce) The most native instructions a program may have, for the capture, the lowering and this map: as many as fit
// in the largest code area the renderer reads (a control section whose area ends past 16 MiB is refused: stage_head,
// gpu/vk/vk_shader.cpp), 16 bytes each. The GPU has no instruction count of its own: Maxwell and Ampere address a program
// by a 32-bit offset in its program region, and an Ampere BRA's offset has 50 bits. (65,536, 1 MiB, refused compute
// program 79116b290d46542f, 0x104680 bytes of code, on every platform)
constexpr u32 MaximumNativeInstructions = (16u << 20) / 16;
inline bool HasNativeAddressMap(std::span<const u64> code) {
return code.size() >= 2 && code.back() == NativeAddressMapMagic;
}
@@ -20,7 +26,7 @@ inline void AppendNativeAddressMap(std::vector<u64>& code, std::span<const u32>
inline std::optional<u32> NativeProgramAddress(std::span<const u64> code, u32 address) {
if (!HasNativeAddressMap(code) || address % 16) return {};
const u64 count = code[code.size() - 2];
if (!count || count > 65536 || count + 2 > code.size() || address / 16 >= count) return {};
if (!count || count > MaximumNativeInstructions || count + 2 > code.size() || address / 16 >= count) return {};
const size_t start = code.size() - static_cast<size_t>(count) - 2;
const u64 target = code[start + address / 16];
if (target < 0x50 || target >= start * 8 || target % 8 || target % 32 == 0) return {};

View File

@@ -3,12 +3,14 @@
#pragma once
#include "shader_recompiler/frontend/ampere/lower_to_maxwell.h"
#include "shader_recompiler/frontend/ampere/program_address_map.h"
namespace Shader::Ampere {
class ProgramCaptureState {
public:
enum class Result { Continue, Complete, InvalidTarget };
static constexpr u32 MaximumInstructions = 65536;
// ((Triforce) a target past the largest program, program_address_map.h; it was 65,536 instructions, 1 MiB)
static constexpr u32 MaximumInstructions = MaximumNativeInstructions;
Result Observe(const Instruction& instruction, u32 index) {
const u32 opcode = instruction.Bits(0, 12);