mirror of
https://git.triforceproject.com/triforce/source
synced 2026-11-21 09:09:58 -04:00
Triforce v0.2.0
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
199
gpu/vk/eden/shader_recompiler/frontend/ampere/geometry_exit.h
Normal file
199
gpu/vk/eden/shader_recompiler/frontend/ampere/geometry_exit.h
Normal file
@@ -0,0 +1,199 @@
|
||||
// SPDX-FileCopyrightText: Copyright 2026 Eden Emulator Project
|
||||
// SPDX-License-Identifier: GPL-3.0-or-later
|
||||
#pragma once
|
||||
|
||||
#include <string_view>
|
||||
#include <vector>
|
||||
#include "common/common_types.h"
|
||||
|
||||
namespace Shader::Ampere {
|
||||
|
||||
struct GeometryExitEnvelope {
|
||||
struct Exit {
|
||||
u32 offset;
|
||||
u32 predicate;
|
||||
};
|
||||
std::vector<Exit> exits;
|
||||
u32 handler{};
|
||||
bool has_returns{};
|
||||
u32 error_offset{};
|
||||
std::string_view error;
|
||||
|
||||
explicit operator bool() const { return error.empty(); }
|
||||
};
|
||||
|
||||
inline constexpr u32 GeometryExitSetupInstructions = 5;
|
||||
|
||||
template <typename Code>
|
||||
GeometryExitEnvelope AnalyzeGeometryExit(const Code& code) {
|
||||
constexpr u32 ModeMask = 0xc00001ffU;
|
||||
constexpr size_t Setup = GeometryExitSetupInstructions;
|
||||
GeometryExitEnvelope result;
|
||||
const auto fail = [&](size_t index, std::string_view reason) {
|
||||
result.exits.clear();
|
||||
result.handler = 0;
|
||||
result.has_returns = false;
|
||||
result.error_offset = static_cast<u32>(index * 16);
|
||||
result.error = reason;
|
||||
return result;
|
||||
};
|
||||
const auto bits = [&](size_t index, u32 offset, u32 count) {
|
||||
const auto& words = code[index].words;
|
||||
u64 value = words[offset / 32];
|
||||
if (offset / 32 + 1 < 4) value |= u64{words[offset / 32 + 1]} << 32;
|
||||
return static_cast<u32>((value >> (offset % 32)) & ((u64{1} << count) - 1));
|
||||
};
|
||||
const auto is = [&](size_t index, u32 word0, u32 word1, u32 word2) {
|
||||
const auto& words = code[index].words;
|
||||
return words[0] == word0 && words[1] == word1 && words[2] == word2 &&
|
||||
(words[3] & ModeMask) == 0;
|
||||
};
|
||||
const auto is_exit = [&](size_t index) {
|
||||
const auto& words = code[index].words;
|
||||
return (words[0] & ~0xf000U) == 0x94d && words[1] == 0 && words[2] == 0x03800000 &&
|
||||
(words[3] & ModeMask) == 0 && bits(index, 12, 4) != 15;
|
||||
};
|
||||
const auto is_return = [&](size_t index) {
|
||||
const auto& words = code[index].words;
|
||||
const u64 raw = words[1] | (u64{words[2] & 0x3ffff} << 32);
|
||||
const s64 relative = static_cast<s64>(raw << 14) >> 14;
|
||||
return (words[0] & 0x00ffffffU) == 0x00007950 && bits(index, 24, 8) < 249 &&
|
||||
(words[2] & ~0x3ffffU) == 0x03c00000 && (words[3] & ModeMask) == 0 &&
|
||||
static_cast<s64>(index + 1) * 16 + relative == 0;
|
||||
};
|
||||
|
||||
const size_t count = code.size();
|
||||
if (count < Setup + 4) return fail(count, "native exit envelope truncated");
|
||||
const u32 target = code[1].words[1];
|
||||
if (!is(0, 0x734e, 0, 0) || !is(1, 0x37802, target, 0xf00) || !is(2, 0x7210, 3, 0x07f1e0ff) ||
|
||||
!(is(3, 0x01017210, 0xff, 0x007fe4ff) || is(3, 0xff017224, 0xff, 0x000e0401)) ||
|
||||
!is(4, 0x7357, 0, 0))
|
||||
return fail(0, "native exit handler installation");
|
||||
const size_t handler = target / 16;
|
||||
if (target % 16 || handler <= Setup + 1 || handler + 3 != count)
|
||||
return fail(1, "native exit handler location");
|
||||
if (!is(handler, 0x00ff7324, 0xff, 0) || !is(handler + 1, 0x7957, 0, 0) ||
|
||||
!is(handler + 2, 0x794d, 0, 0x03800000))
|
||||
return fail(handler, "native exit handler body");
|
||||
if (!(is_exit(handler - 1) && bits(handler - 1, 12, 4) == 7) && !is_return(handler - 1))
|
||||
return fail(handler - 1, "native exit handler reached by fallthrough");
|
||||
|
||||
for (size_t index = Setup; index < handler; ++index) {
|
||||
const u32 opcode = bits(index, 0, 12), family = opcode & 0x1ff;
|
||||
if (family == 0x14e || (family >= 0x154 && family <= 0x157))
|
||||
return fail(index, "native exit handler state access in body");
|
||||
if (opcode == 0x94d) {
|
||||
if (!is_exit(index)) return fail(index, "native body EXIT mode");
|
||||
result.exits.push_back({static_cast<u32>(index * 16), bits(index, 12, 4)});
|
||||
continue;
|
||||
}
|
||||
if (is_return(index)) {
|
||||
result.has_returns = true;
|
||||
continue;
|
||||
}
|
||||
if (family < 0x140 || family >= 0x160 || opcode == 0x941 || opcode == 0x942 ||
|
||||
opcode == 0x948)
|
||||
continue;
|
||||
const auto& words = code[index].words;
|
||||
const bool noinc_call = opcode == 0x944 && (words[2] & 0x400000) != 0;
|
||||
if (opcode != 0x945 && opcode != 0x947 && !noinc_call)
|
||||
return fail(index, "native control flow in exit envelope body");
|
||||
const u64 raw = words[1] | (u64{words[2] & 0x3ffff} << 32);
|
||||
const s64 relative = opcode == 0x945 ? s64{static_cast<s32>(words[1])}
|
||||
: static_cast<s64>(raw << 14) >> 14;
|
||||
const s64 destination = static_cast<s64>(index) * 16 + 16 + relative;
|
||||
if (destination % 16 || destination < static_cast<s64>(Setup * 16) ||
|
||||
destination >= static_cast<s64>(handler * 16))
|
||||
return fail(index, "native branch leaves exit envelope body");
|
||||
}
|
||||
|
||||
u32 live_registers = 0b1011, live_predicates = 0b1;
|
||||
for (size_t index = Setup; live_registers || live_predicates; ++index) {
|
||||
const u32 opcode = bits(index, 0, 12), family = opcode & 0x1ff;
|
||||
const u32 form = (opcode >> 9) & 7, guard = bits(index, 12, 4);
|
||||
u32 reads = 0, read_predicates = 0, writes = 0, write_predicates = 0;
|
||||
const auto read_register = [&](u32 reg) { if (reg < 32) reads |= 1U << reg; };
|
||||
const auto write_register = [&](u32 reg) { if (reg < 32) writes |= 1U << reg; };
|
||||
const auto read_predicate = [&](u32 pred) { if (pred < 7) read_predicates |= 1U << pred; };
|
||||
const auto write_predicate = [&](u32 pred) { if (pred < 7) write_predicates |= 1U << pred; };
|
||||
const auto read_sources = [&](bool has_c) {
|
||||
read_register(bits(index, 24, 8));
|
||||
if (form == 1) read_register(bits(index, 32, 8));
|
||||
if (form == 2 || form == 3 || form == 7) read_register(bits(index, 64, 8));
|
||||
if (has_c && (form == 1 || form == 4 || form == 5 || form == 6)) read_register(bits(index, 64, 8));
|
||||
};
|
||||
read_predicate(guard & 7);
|
||||
if (is_exit(index)) {
|
||||
if (guard != 7)
|
||||
return fail(index, "native instruction before exit setup state is dead");
|
||||
// The handler finalizes the output through R0; the rest of the setup state may stay
|
||||
// live when the body ends without reading it.
|
||||
if ((live_registers & 1) || (read_predicates & live_predicates))
|
||||
return fail(index, "native exit handler reads exit setup state");
|
||||
break;
|
||||
}
|
||||
if (opcode == 0x919 || opcode == 0x923) {
|
||||
if (opcode == 0x923) read_register(bits(index, 24, 8));
|
||||
write_register(bits(index, 16, 8));
|
||||
} else if (opcode == 0x805 || opcode == 0xab9 || opcode == 0x945) {
|
||||
} else if (opcode == 0x381 || opcode == 0x981) {
|
||||
const u32 address = bits(index, 24, 8), size = bits(index, 73, 3);
|
||||
if (address != 255) {
|
||||
read_register(address);
|
||||
read_register(address + 1);
|
||||
}
|
||||
const u32 results = size < 5 ? 1 : size == 5 ? 2 : size == 6 ? 4 : 0;
|
||||
for (u32 i = 0; i < results; ++i) write_register(bits(index, 16, 8) + i);
|
||||
} else if (family == 0x002 && (form == 1 || form == 4 || form == 5 || form == 6)) {
|
||||
if (form == 1) read_register(bits(index, 32, 8));
|
||||
write_register(bits(index, 16, 8));
|
||||
} else if ((family == 0x024 || family == 0x016) && form != 0) {
|
||||
read_sources(true);
|
||||
if (family == 0x024 && bits(index, 74, 1)) read_predicate(bits(index, 87, 3));
|
||||
write_register(bits(index, 16, 8));
|
||||
} else if (family == 0x105 && (form == 1 || form == 5 || form == 6)) {
|
||||
// F2I: register, constant buffer or uniform source in operand b; a is unused.
|
||||
if (form == 1) read_register(bits(index, 32, 8));
|
||||
write_register(bits(index, 16, 8));
|
||||
} else if (family == 0x011 && form != 0) {
|
||||
// LEA (.X reads the carry predicate), optional carry out.
|
||||
read_sources(true);
|
||||
if (bits(index, 74, 1)) read_predicate(bits(index, 87, 3));
|
||||
write_register(bits(index, 16, 8));
|
||||
write_predicate(bits(index, 81, 3));
|
||||
} else if (opcode == 0xb82) {
|
||||
// LDC with a register index; a 64-bit load writes a pair.
|
||||
if (bits(index, 24, 8) != 255) read_register(bits(index, 24, 8));
|
||||
write_register(bits(index, 16, 8));
|
||||
if (bits(index, 73, 3) == 5) write_register(bits(index, 16, 8) + 1);
|
||||
} else if (family == 0x012 && form != 0) {
|
||||
// LOP3.LUT with an optional predicate result.
|
||||
read_sources(true);
|
||||
write_register(bits(index, 16, 8));
|
||||
write_predicate(bits(index, 81, 3));
|
||||
} else if (opcode == 0x322) {
|
||||
// AST: stores 1-4 components from operand b for the vertex in c; writes nothing.
|
||||
const u32 value = bits(index, 32, 8), components = bits(index, 74, 2) + 1;
|
||||
if (value != 255)
|
||||
for (u32 i = 0; i < components; ++i) read_register(value + i);
|
||||
read_register(bits(index, 64, 8));
|
||||
} else if (family == 0x00c && form != 0 && !bits(index, 72, 1)) {
|
||||
read_sources(false);
|
||||
read_predicate(bits(index, 87, 3));
|
||||
write_predicate(bits(index, 81, 3));
|
||||
write_predicate(bits(index, 84, 3));
|
||||
} else {
|
||||
return fail(index, "native instruction before exit setup state is dead");
|
||||
}
|
||||
if ((reads & live_registers) || (read_predicates & live_predicates))
|
||||
return fail(index, "native body reads exit setup state");
|
||||
if (guard == 7) {
|
||||
live_registers &= ~writes;
|
||||
live_predicates &= ~write_predicates;
|
||||
}
|
||||
}
|
||||
result.handler = target;
|
||||
return result;
|
||||
}
|
||||
|
||||
} // namespace Shader::Ampere
|
||||
20
gpu/vk/eden/shader_recompiler/frontend/ampere/instruction.h
Normal file
20
gpu/vk/eden/shader_recompiler/frontend/ampere/instruction.h
Normal file
@@ -0,0 +1,20 @@
|
||||
// SPDX-FileCopyrightText: Copyright 2026 Eden Emulator Project
|
||||
// SPDX-License-Identifier: GPL-3.0-or-later
|
||||
#pragma once
|
||||
|
||||
#include <array>
|
||||
#include "common/common_types.h"
|
||||
|
||||
namespace Shader::Ampere {
|
||||
struct Instruction {
|
||||
std::array<u32, 4> words;
|
||||
|
||||
u32 Bits(u32 offset, u32 count) const {
|
||||
u64 bits = words[offset / 32];
|
||||
if (offset / 32 + 1 < words.size())
|
||||
bits |= u64{words[offset / 32 + 1]} << 32;
|
||||
return static_cast<u32>((bits >> (offset % 32)) & ((u64{1} << count) - 1));
|
||||
}
|
||||
};
|
||||
static_assert(sizeof(Instruction) == 16);
|
||||
} // namespace Shader::Ampere
|
||||
4078
gpu/vk/eden/shader_recompiler/frontend/ampere/lower_to_maxwell.h
Normal file
4078
gpu/vk/eden/shader_recompiler/frontend/ampere/lower_to_maxwell.h
Normal file
File diff suppressed because it is too large
Load Diff
File diff suppressed because it is too large
Load Diff
@@ -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
|
||||
@@ -0,0 +1,81 @@
|
||||
// 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 (index < exit_setup.size()) {
|
||||
exit_setup[index] = instruction;
|
||||
if (index + 1 == exit_setup.size() &&
|
||||
std::all_of(exit_setup.begin(), exit_setup.end(), [](const Instruction& setup) {
|
||||
return (setup.words[3] & 0xc00001ffU) == 0;
|
||||
}) &&
|
||||
exit_setup[0].words[0] == 0x0000734e &&
|
||||
exit_setup[0].words[1] == 0 && exit_setup[0].words[2] == 0 &&
|
||||
exit_setup[1].words[0] == 0x00037802 && exit_setup[1].words[2] == 0xf00 &&
|
||||
exit_setup[2].words[0] == 0x00007210 && exit_setup[2].words[1] == 3 &&
|
||||
exit_setup[2].words[2] == 0x07f1e0ff &&
|
||||
exit_setup[3].words[1] == 0xff &&
|
||||
((exit_setup[3].words[0] == 0x01017210 &&
|
||||
exit_setup[3].words[2] == 0x007fe4ff) ||
|
||||
(exit_setup[3].words[0] == 0xff017224 &&
|
||||
exit_setup[3].words[2] == 0x000e0401)) &&
|
||||
instruction.words[0] == 0x00007357 && instruction.words[1] == 0 &&
|
||||
instruction.words[2] == 0) {
|
||||
const u32 target = exit_setup[1].words[1];
|
||||
if (target < exit_setup.size() * 16 || target % 16 ||
|
||||
target >= MaximumInstructions * 16)
|
||||
return Result::InvalidTarget;
|
||||
last_forward_target = std::max(last_forward_target, target / 16);
|
||||
}
|
||||
}
|
||||
if (opcode == 0x944 || opcode == 0x947 || opcode == 0x945) {
|
||||
// BRA keeps its .DIV/.CONV mode in the offset's two low bits.
|
||||
const u32 low = instruction.words[1] & (opcode == 0x947 ? ~3U : ~0U);
|
||||
const u64 raw = low | (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, but its callee still returns
|
||||
// with RET.REL.NODEC through the link register set ahead of the call.
|
||||
if (opcode == 0x944) {
|
||||
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 (index >= 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:
|
||||
std::array<Instruction, 5> exit_setup{};
|
||||
u32 last_call_target{};
|
||||
u32 last_forward_target{};
|
||||
bool main_exited{};
|
||||
bool computed_branch{};
|
||||
};
|
||||
} // namespace Shader::Ampere
|
||||
@@ -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
|
||||
@@ -0,0 +1,316 @@
|
||||
// SPDX-FileCopyrightText: Copyright 2026 Eden Emulator Project
|
||||
// SPDX-License-Identifier: GPL-3.0-or-later
|
||||
#pragma once
|
||||
#include <array>
|
||||
#include <vector>
|
||||
#include "shader_recompiler/frontend/ampere/instruction.h"
|
||||
|
||||
namespace Shader::Ampere {
|
||||
struct RegisterPressure {
|
||||
Instruction instruction;
|
||||
std::vector<u64> prefix, suffix;
|
||||
bool valid = true;
|
||||
};
|
||||
inline u64 NativeRegisterCopy(u32 destination_bank, u32 destination, u32 source_bank, u32 source) {
|
||||
return 0x5c98000000070000ULL | (u64{10} << 44) | destination | (u64{destination_bank} << 8) |
|
||||
(u64{source_bank} << 10) | (u64{source} << 20);
|
||||
}
|
||||
// Preserve high guest registers in SSA while borrowing aligned low quartets
|
||||
// for one native instruction. The expanded instruction owns its temporary bank.
|
||||
inline RegisterPressure PrepareRegisterPressure(const Instruction& input, u32 reserved) {
|
||||
RegisterPressure result{input};
|
||||
if (reserved < 4 || reserved > 246 || (reserved & 3)) {
|
||||
result.valid = false;
|
||||
return result;
|
||||
}
|
||||
struct Operand {
|
||||
u32 bit, count;
|
||||
};
|
||||
std::vector<Operand> operands;
|
||||
const auto operand = [&](u32 bit, u32 count = 1) { operands.push_back({bit, count}); };
|
||||
const u32 opcode = input.Bits(0, 12);
|
||||
switch (opcode) {
|
||||
case 0xa02:
|
||||
case 0xc3e:
|
||||
case 0x802:
|
||||
case 0xb08:
|
||||
case 0xd08:
|
||||
case 0x908:
|
||||
case 0x919:
|
||||
operand(16);
|
||||
break;
|
||||
// (triforce 11-10) Forms this table lacked: 0x3c2, 0x803, 0x809, 0xb82, 0xc0c, 0xc12, 0xc19, 0xc20, 0xc23, 0xc24, 0xc45.
|
||||
// A mesh program that reached one of them with a high register stopped as "native register shadowing mode"; the old
|
||||
// lowering refused all but two of those too, so their draws were never made (tests/seed_lowering.cpp over the game's
|
||||
// 13,773 shader stages: 26 of its 491 mesh stages lowered by neither, 9 of them the models'; 5 after this). Each form
|
||||
// lists the fields its lowering (lower_to_maxwell.h) reads as general registers: the uniform register of a 0xc.. form
|
||||
// (bits 32-39) and R2UR's destination are not among them.
|
||||
case 0x3c2: // R2UR: its source (the destination is a uniform register)
|
||||
case 0xc0c: // ISETP against a uniform register: its first source (it writes predicates only)
|
||||
case 0xa0c:
|
||||
case 0x804:
|
||||
case 0x80b:
|
||||
case 0x80c:
|
||||
operand(24);
|
||||
break;
|
||||
case 0xb82: { // LDC, indexed: the destination (a pair at width 5) and the index
|
||||
const u32 width = input.Bits(73, 3);
|
||||
operand(16, width == 6 ? 4 : width == 5 ? 2 : 1);
|
||||
operand(24);
|
||||
break;
|
||||
}
|
||||
case 0x20c:
|
||||
case 0x20b:
|
||||
operand(24);
|
||||
operand(32);
|
||||
break;
|
||||
case 0x803: // (triforce) P2R: destination and merge source
|
||||
case 0x809: // (triforce) FMNMX with an immediate
|
||||
case 0xc20: // (triforce) FMUL by a uniform register
|
||||
case 0x807:
|
||||
case 0x808:
|
||||
case 0x923:
|
||||
case 0x820:
|
||||
case 0xa20:
|
||||
case 0x421:
|
||||
operand(16);
|
||||
operand(24);
|
||||
break;
|
||||
case 0x207:
|
||||
case 0x208:
|
||||
case 0x220:
|
||||
case 0x221:
|
||||
case 0x209:
|
||||
operand(16);
|
||||
operand(24);
|
||||
operand(32);
|
||||
break;
|
||||
case 0x805:
|
||||
operand(16, 2);
|
||||
break;
|
||||
case 0xc17:
|
||||
case 0x817:
|
||||
operand(16);
|
||||
operand(24);
|
||||
break;
|
||||
case 0xc23: // (triforce) FFMA with a uniform multiplier: destination, a and the addend
|
||||
case 0xa24:
|
||||
case 0xa23:
|
||||
case 0x623:
|
||||
case 0x423:
|
||||
operand(16);
|
||||
operand(24);
|
||||
operand(64);
|
||||
break;
|
||||
case 0x621:
|
||||
operand(16);
|
||||
operand(24);
|
||||
break;
|
||||
case 0x321:
|
||||
operand(16, input.Bits(74, 2) + 1);
|
||||
operand(24);
|
||||
operand(32);
|
||||
break;
|
||||
case 0x322:
|
||||
operand(24);
|
||||
operand(32, input.Bits(74, 2) + 1);
|
||||
break;
|
||||
case 0x927:
|
||||
operand(24);
|
||||
operand(32);
|
||||
break;
|
||||
case 0x224:
|
||||
case 0x223:
|
||||
operand(16);
|
||||
operand(24);
|
||||
operand(32);
|
||||
operand(64);
|
||||
break;
|
||||
case 0x81a:
|
||||
operand(16);
|
||||
operand(24);
|
||||
break;
|
||||
case 0x308:
|
||||
case 0x202:
|
||||
case 0x243:
|
||||
case 0x245:
|
||||
case 0x305:
|
||||
case 0x306:
|
||||
case 0x307:
|
||||
operand(16);
|
||||
operand(32);
|
||||
break;
|
||||
case 0xc45: // (triforce) I2FP of a uniform register
|
||||
case 0xb05:
|
||||
case 0xd05:
|
||||
operand(16);
|
||||
break;
|
||||
case 0x59f:
|
||||
operand(24, 2);
|
||||
operand(32);
|
||||
break;
|
||||
case 0x98e:
|
||||
operand(24, input.Bits(72, 1) ? 2 : 1);
|
||||
operand(32, input.Bits(73, 3) == 2 || input.Bits(73, 3) == 5 ? 2 : 1);
|
||||
break;
|
||||
case 0x433:
|
||||
case 0x633:
|
||||
operand(16);
|
||||
operand(24);
|
||||
break;
|
||||
case 0x233:
|
||||
operand(16);
|
||||
operand(24);
|
||||
operand(32);
|
||||
break;
|
||||
case 0xc12: // (triforce) LOP3 with a uniform second source
|
||||
case 0xc24: // (triforce) IMAD with a uniform multiplier
|
||||
case 0x810:
|
||||
case 0xa10:
|
||||
case 0xc10:
|
||||
case 0x812:
|
||||
case 0x824:
|
||||
case 0x823:
|
||||
case 0x831:
|
||||
case 0x424:
|
||||
case 0x624:
|
||||
operand(16);
|
||||
operand(24);
|
||||
operand(64);
|
||||
break;
|
||||
case 0x230:
|
||||
case 0x231:
|
||||
operand(16);
|
||||
operand(24);
|
||||
operand(32);
|
||||
operand(64);
|
||||
break;
|
||||
case 0xc19: // (triforce) SHF by a uniform amount: destination, low and high words
|
||||
case 0x419:
|
||||
case 0x819:
|
||||
operand(16);
|
||||
operand(24);
|
||||
operand(64);
|
||||
break;
|
||||
case 0x210:
|
||||
case 0x211:
|
||||
case 0x212:
|
||||
case 0x219:
|
||||
operand(16);
|
||||
operand(24);
|
||||
operand(32);
|
||||
operand(64);
|
||||
break;
|
||||
case 0xa11:
|
||||
operand(16);
|
||||
operand(24);
|
||||
operand(64);
|
||||
break;
|
||||
case 0x225:
|
||||
case 0x227:
|
||||
case 0x625:
|
||||
case 0x825:
|
||||
operand(16, opcode == 0x227 ? 1 : 2);
|
||||
operand(24);
|
||||
if (opcode == 0x225 || opcode == 0x227) {
|
||||
operand(32);
|
||||
operand(64, 2);
|
||||
}
|
||||
if (opcode == 0x625)
|
||||
operand(64);
|
||||
if (opcode == 0x825)
|
||||
operand(64, 2);
|
||||
break;
|
||||
case 0x83b:
|
||||
operand(16, 1U << input.Bits(72, 2));
|
||||
operand(24);
|
||||
break;
|
||||
case 0x23c:
|
||||
operand(16, 2);
|
||||
operand(24, input.Bits(75, 1) ? 4 : 2);
|
||||
operand(32, input.Bits(75, 1) ? 2 : 1);
|
||||
operand(64, 2);
|
||||
break;
|
||||
case 0x388:
|
||||
case 0x984:
|
||||
case 0x988: {
|
||||
const u32 width = input.Bits(73, 3), count = width == 6 ? 4 : width == 5 ? 2 : 1;
|
||||
operand(opcode == 0x984 ? 16 : 32, count);
|
||||
operand(24);
|
||||
break;
|
||||
}
|
||||
case 0x387:
|
||||
case 0x983:
|
||||
case 0x987: {
|
||||
const u32 width = input.Bits(73, 3), count = width == 6 ? 4 : width == 5 ? 2 : 1;
|
||||
operand(opcode == 0x983 ? 16 : 32, count);
|
||||
operand(24);
|
||||
break;
|
||||
}
|
||||
case 0x381:
|
||||
case 0x386:
|
||||
case 0x981:
|
||||
case 0x986: {
|
||||
const u32 width = input.Bits(73, 3), count = width == 6 ? 4 : width == 5 ? 2 : 1;
|
||||
operand(opcode == 0x386 || opcode == 0x986 ? 32 : 16, count);
|
||||
operand(24, opcode == 0x381 || opcode == 0x386 || input.Bits(90, 1) ? 2 : 1);
|
||||
break;
|
||||
}
|
||||
default:
|
||||
result.valid = false;
|
||||
return result;
|
||||
}
|
||||
std::array<bool, 256> used{};
|
||||
std::array<bool, 64> groups{};
|
||||
for (const auto [bit, count] : operands) {
|
||||
const u32 reg = input.Bits(bit, 8);
|
||||
if (reg == 255)
|
||||
continue;
|
||||
if (reg + count > 255 ||
|
||||
(reg < reserved && reg + count > reserved) ||
|
||||
(reg >= reserved && reg / 4 != (reg + count - 1) / 4)) {
|
||||
result.valid = false;
|
||||
return result;
|
||||
}
|
||||
for (u32 component = 0; component < count; ++component) {
|
||||
used[reg + component] = true;
|
||||
if (reg + component >= reserved)
|
||||
groups[(reg + component) / 4] = true;
|
||||
}
|
||||
}
|
||||
std::array<u32, 64> mapped{};
|
||||
for (u32 group = 0; group < groups.size(); ++group) {
|
||||
if (!groups[group])
|
||||
continue;
|
||||
u32 borrow = reserved;
|
||||
while (borrow >= 4) {
|
||||
borrow -= 4;
|
||||
if (!used[borrow] && !used[borrow + 1] && !used[borrow + 2] && !used[borrow + 3])
|
||||
break;
|
||||
}
|
||||
if (used[borrow] || used[borrow + 1] || used[borrow + 2] || used[borrow + 3]) {
|
||||
result.valid = false;
|
||||
return result;
|
||||
}
|
||||
mapped[group] = borrow;
|
||||
for (u32 component = 0; component < 4; ++component) {
|
||||
const u32 low = borrow + component, high = group * 4 + component;
|
||||
used[low] = true;
|
||||
result.prefix.push_back(NativeRegisterCopy(2, low, 0, low));
|
||||
result.prefix.push_back(NativeRegisterCopy(0, low, 1, high));
|
||||
result.suffix.push_back(NativeRegisterCopy(1, high, 0, low));
|
||||
result.suffix.push_back(NativeRegisterCopy(0, low, 2, low));
|
||||
}
|
||||
}
|
||||
for (const auto [bit, count] : operands) {
|
||||
const u32 reg = input.Bits(bit, 8);
|
||||
if (reg == 255 || !groups[reg / 4])
|
||||
continue;
|
||||
const u32 replacement = mapped[reg / 4] + reg % 4;
|
||||
const u32 word = bit / 32, shift = bit % 32;
|
||||
result.instruction.words[word] =
|
||||
(result.instruction.words[word] & ~(255U << shift)) | (replacement << shift);
|
||||
}
|
||||
return result;
|
||||
}
|
||||
} // namespace Shader::Ampere
|
||||
Reference in New Issue
Block a user