fix(libSceAgc): accept s_swappc_b64 null, s[6:7] as a front half's terminator

sceAgcUnknownGetFusedShaderSize and sceAgcUnknownFuseShaderHalves
require a front half to end with s_setpc_b64 s[6:7] (0xbe802006), the
jump to the back half, before its s_code_end padding. PPSA16064
(Minecraft Dungeons II, Unreal Engine) has front halves that end with
0xbefd2106 instead, and the size query threw "front half does not end
with s_setpc_b64 s[6:7]".

0xbefd2106 is SOP1 S_SWAPPC_B64 (opcode 0x21) with SSRC0 s[6:7] and
SDST 125, the NULL register (RDNA2 ISA, SOP1 and the scalar operand
table): it jumps to s[6:7] and discards the return address, the same
transfer as s_setpc_b64 s[6:7]. Fusion drops this instruction and pads
to the back half with s_nop either way, so both forms are accepted. Any
other last instruction still throws.

agc_shader_fusion runs the full fusion check on a front half ending
with the s_swappc_b64 form; on main it fails with the title's error.
This commit is contained in:
TideOPL
2026-10-07 14:05:52 +01:00
parent e79c971f99
commit 1311bf6567
2 changed files with 8 additions and 5 deletions
@@ -28,6 +28,7 @@ constexpr std::uint32_t SPI_SHADER_PGM_RSRC2_HS = 0x10Bu;
constexpr std::uint32_t SCodeEnd = 0xbf9f0000u;
constexpr std::uint32_t SNop = 0xbf800000u;
constexpr std::uint32_t SSetpcS6 = 0xbe802006u;
constexpr std::uint32_t SSwappcNullS6 = 0xbefd2106u;
constexpr std::uint8_t FusedCodeAlignmentLog2 = 8;
constexpr std::size_t FusedCodeAlignment = std::size_t{1} << FusedCodeAlignmentLog2;
constexpr char TrailerMagic[8] = {'b', 'a', 'r', 'e', 'f', 'o', 'o', 't'};
@@ -114,7 +115,7 @@ std::uint32_t FrontProgramBytes(const char* function, const Shader* front) {
return value;
};
while (end > 0 && word(end - 1) == SCodeEnd) --end;
if (end == 0 || word(end - 1) != SSetpcS6) Fail(function, "front half does not end with s_setpc_b64 s[6:7]");
if (end == 0 || (word(end - 1) != SSetpcS6 && word(end - 1) != SSwappcNullS6)) Fail(function, "front half does not end with s_setpc_b64 s[6:7] or s_swappc_b64 null, s[6:7]");
return static_cast<std::uint32_t>((end - 1) * 4);
}
@@ -15,6 +15,7 @@ namespace {
constexpr std::uint32_t SCodeEnd = 0xbf9f0000u;
constexpr std::uint32_t SNop = 0xbf800000u;
constexpr std::uint32_t SSetpcS6 = 0xbe802006u;
constexpr std::uint32_t SSwappcNullS6 = 0xbefd2106u;
constexpr std::uint32_t SMovS0 = 0xbe800380u;
constexpr std::uint32_t VMovV0 = 0x7e000280u;
constexpr std::uint32_t SEndpgm = 0xbf810000u;
@@ -67,8 +68,8 @@ struct Halves {
Shader front{};
Shader back{};
Halves() {
frontCode = {SMovS0, VMovV0, SSetpcS6, SCodeEnd, SCodeEnd};
explicit Halves(std::uint32_t terminator = SSetpcS6) {
frontCode = {SMovS0, VMovV0, terminator, SCodeEnd, SCodeEnd};
const auto codeBytes = static_cast<std::uint32_t>(frontCode.size() * 4);
std::array<std::uint32_t, 2> magic{};
std::memcpy(magic.data(), "barefoot", 8);
@@ -123,8 +124,8 @@ std::uint8_t* aligned(std::vector<std::uint8_t>& storage, std::size_t alignment)
return storage.data() + ((alignment - address % alignment) % alignment);
}
void testFusion(std::size_t misalignment) {
Halves halves;
void testFusion(std::size_t misalignment, std::uint32_t terminator = SSetpcS6) {
Halves halves(terminator);
SizeAlign size{};
check(sceAgcUnknownGetFusedShaderSize(&size, &halves.front, &halves.back) == 0, "fused size query failed");
check(size.m_align == 8, "fused shaders are not 256-byte aligned");
@@ -247,6 +248,7 @@ int main() {
testFusion(0);
testFusion(0xa0);
testFusion(4);
testFusion(0, SSwappcNullS6);
testRejections();
testHullHalves();
LibcRunShutdown_nid_postfix();