Triforce v0.2.0

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
ruckus7382sfgsgdf
2026-11-10 19:32:48 -06:00
commit 34b3683441
1730 changed files with 554853 additions and 0 deletions

File diff suppressed because it is too large Load Diff

View File

@@ -0,0 +1,29 @@
// SPDX-FileCopyrightText: Copyright 2026 Eden Emulator Project
// SPDX-License-Identifier: GPL-3.0-or-later
#pragma once
#include <optional>
#include <span>
#include <vector>
#include "common/common_types.h"
namespace Shader::Ampere {
constexpr u64 NativeAddressMapMagic = 0x50414d4e4544454eULL;
inline bool HasNativeAddressMap(std::span<const u64> code) {
return code.size() >= 2 && code.back() == NativeAddressMapMagic;
}
inline void AppendNativeAddressMap(std::vector<u64>& code, std::span<const u32> offsets) {
if (offsets.empty()) return;
for (u32 index : offsets) code.push_back(0x50U + index * 8 + ((index + 1) / 3) * 8);
code.push_back(offsets.size());
code.push_back(NativeAddressMapMagic);
}
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 {};
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 {};
return static_cast<u32>(target);
}
} // namespace Shader::Ampere

View File

@@ -0,0 +1,54 @@
// SPDX-FileCopyrightText: Copyright 2026 Eden Emulator Project
// SPDX-License-Identifier: GPL-3.0-or-later
#pragma once
#include "shader_recompiler/frontend/ampere/lower_to_maxwell.h"
namespace Shader::Ampere {
class ProgramCaptureState {
public:
enum class Result { Continue, Complete, InvalidTarget };
static constexpr u32 MaximumInstructions = 65536;
Result Observe(const Instruction& instruction, u32 index) {
const u32 opcode = instruction.Bits(0, 12);
if (opcode == 0x944 || opcode == 0x947 || opcode == 0x945) {
const u64 raw = instruction.words[1] | (u64{instruction.words[2] & 0x3ffff} << 32);
const s64 relative = opcode == 0x945 ? static_cast<s32>(instruction.words[1])
: static_cast<s64>(raw << 14) >> 14;
const s64 target = s64{index} * 16 + 16 + relative;
if (target < 0 || target % 16 || target >= MaximumInstructions * 16)
return Result::InvalidTarget;
// CALL.REL.NOINC (bit 86) pushes no return address: the compiler uses it as a jump
// (the lowering treats it as one), so it never pairs with a RET.
const bool noinc_call = opcode == 0x944 && (instruction.words[2] & 0x400000) != 0;
if (opcode == 0x944 && !noinc_call) {
last_call_target = std::max(last_call_target, static_cast<u32>(target / 16));
} else {
last_forward_target = std::max(last_forward_target, static_cast<u32>(target / 16));
}
// The compiler ends every program with an unconditional branch to itself; with a jump
// table (whose targets live in a constant buffer) that is the only reliable end.
if (opcode == 0x947 && computed_branch && instruction.Bits(12, 4) == 7 &&
target == s64{index} * 16)
return Result::Complete;
}
if (opcode == 0x949 || opcode == 0x958) computed_branch = true;
if (computed_branch) return Result::Continue;
if (opcode == 0x94d && instruction.Bits(12, 4) == 7 && index >= last_forward_target) {
if (!last_call_target) return Result::Complete;
main_exited = true;
}
if (opcode == 0x950 && instruction.Bits(12, 4) == 7 && main_exited &&
index >= last_call_target && index >= last_forward_target)
return Result::Complete;
return Result::Continue;
}
private:
u32 last_call_target{};
u32 last_forward_target{};
bool main_exited{};
bool computed_branch{};
};
} // namespace Shader::Ampere

View File

@@ -0,0 +1,85 @@
// SPDX-FileCopyrightText: Copyright 2026 Eden Emulator Project
// SPDX-License-Identifier: GPL-3.0-or-later
#pragma once
#include <cstring>
#include "shader_recompiler/frontend/ampere/lower_to_maxwell.h"
namespace Shader::Ampere {
inline u64 NativeProgramKey(std::span<const Instruction> source) {
u64 hash = 14695981039346656037ULL;
for (const auto& instruction : source) {
for (u32 word : instruction.words) {
for (u32 byte = 0; byte < 4; ++byte) hash = (hash ^ ((word >> (byte * 8)) & 255)) * 1099511628211ULL;
}
}
return hash;
}
// Private iteration format: version, lengths, texture bank, exact original
// instructions, then the newly generated canonical instructions. Source bytes
// are compared directly; the filename hash is only an index.
inline std::vector<u8> EncodeProgramOverride(std::span<const Instruction> source,
const LoweredProgram& program) {
if (!program || program.source_size != source.size_bytes() || program.source_instruction_offsets.size() != source.size()) return {};
const std::array<u32, 6> header{0x4e454445, 0x3230564f,
static_cast<u32>(source.size_bytes()), static_cast<u32>(program.instructions.size()),
program.required_texture_buffer, static_cast<u32>(program.source_instruction_offsets.size())};
std::vector<u8> bytes(sizeof(header) + source.size_bytes() + program.instructions.size() * 8 + program.source_instruction_offsets.size() * 4);
std::memcpy(bytes.data(), header.data(), sizeof(header));
std::memcpy(bytes.data() + sizeof(header), source.data(), source.size_bytes());
std::memcpy(bytes.data() + sizeof(header) + source.size_bytes(), program.instructions.data(), program.instructions.size() * 8);
std::memcpy(bytes.data() + sizeof(header) + source.size_bytes() + program.instructions.size() * 8,
program.source_instruction_offsets.data(), program.source_instruction_offsets.size() * 4);
return bytes;
}
inline u32 ProgramOverrideSourceSize(std::span<const u8> bytes) {
std::array<u32, 6> header{};
if (bytes.size() < sizeof(header)) return 0;
std::memcpy(header.data(), bytes.data(), sizeof(header));
const bool mapped = header[1] == 0x3230564f;
if (header[0] != 0x4e454445 || (!mapped && header[1] != 0x3130564f) ||
(mapped ? header[5] != header[2] / 16 : header[5] != 0) ||
header[2] == 0 || header[2] % sizeof(Instruction) || header[2] > 1048576 ||
header[3] == 0 || header[3] > 1048576 || (header[4] != ~0U && header[4] >= 18)) return 0;
if (bytes.size() != sizeof(header) + size_t{header[2]} + size_t{header[3]} * 8 + size_t{header[5]} * 4) return 0;
return header[2];
}
inline LoweredProgram DecodeProgramOverride(std::span<const u8> bytes,
std::span<const Instruction> source) {
LoweredProgram result;
const auto fail = [&result](std::string_view message) {
result.instructions.clear();
result.source_instruction_offsets.clear();
result.error = message;
return result;
};
std::array<u32, 6> header{};
if (!ProgramOverrideSourceSize(bytes)) return fail("invalid native override header or length");
std::memcpy(header.data(), bytes.data(), sizeof(header));
if (header[2] != source.size_bytes()) return fail("native override source length mismatch");
if (std::memcmp(bytes.data() + sizeof(header), source.data(), source.size_bytes()))
return fail("native override source mismatch");
result.instructions.resize(header[3]);
std::memcpy(result.instructions.data(), bytes.data() + sizeof(header) + source.size_bytes(), header[3] * 8);
if (header[5]) {
result.source_instruction_offsets.resize(header[5]);
std::memcpy(result.source_instruction_offsets.data(),
bytes.data() + sizeof(header) + source.size_bytes() + size_t{header[3]} * 8,
size_t{header[5]} * 4);
u32 previous = 0;
for (u32 offset : result.source_instruction_offsets) {
if (offset >= result.instructions.size() || offset < previous)
return fail("native override address map mismatch");
previous = offset;
}
}
result.source_size = header[2];
result.required_texture_buffer = header[4];
return result;
}
} // namespace Shader::Ampere