mirror of
https://github.com/TheBozos/TMB_Recomp.git
synced 2026-12-01 12:54:41 -10:00
Preserve the original complete caller ABI and legacy extent without emitting hardware tags. Keep unknown, historical and overlapping-record fallbacks. Qualify the native leaf and full texture chain with original comparisons, complete game recordings, memory snapshots and windowed movie/loading/gameplay. Co-Authored-By: Codex <[email protected]>
140 lines
6.4 KiB
C++
140 lines
6.4 KiB
C++
// SPDX-License-Identifier: MIT
|
|
// The traversal's texture consumer reads game records directly; no packet image or address-keyed copy is kept.
|
|
#include "tmb_texture_inputs.h"
|
|
#include "bozo/libs/libraries.h"
|
|
#include <algorithm>
|
|
#include <cstdio>
|
|
#include <cstring>
|
|
#include <iterator>
|
|
|
|
namespace tmb::textureInputs
|
|
{
|
|
namespace
|
|
{
|
|
// bozo:native kTextureInputs
|
|
constexpr uint32_t kTextureInputs[] = {
|
|
0x1f90cc, 0x1f9130, 0x1f9158, 0x1f91b4,
|
|
0x1f93bc, 0x1f93c4, 0x1f93d4, 0x1f9494, 0x1f94a0, 0x1f988c,
|
|
};
|
|
constexpr uint32_t kTextureUpload = 0x1ea5c8;
|
|
static_assert(std::is_sorted(std::begin(kTextureInputs), std::end(kTextureInputs)));
|
|
template <typename T> T read(Runtime *rt, uint32_t address)
|
|
{
|
|
const auto at = bozo::GuestMemory::flatOffset(address, sizeof(T));
|
|
if (at < 0 || uint64_t(at) + sizeof(T) > bozo::kFlatVu0Offset)
|
|
throw bozo::libs::BadAccess{address, sizeof(T)};
|
|
T value;
|
|
std::memcpy(&value, rt->memory.flat() + at, sizeof(value));
|
|
return value;
|
|
}
|
|
struct Statistics
|
|
{
|
|
uint64_t reads{}, patches{}, uploads{};
|
|
~Statistics()
|
|
{
|
|
std::fprintf(stderr, "[native:texture-inputs] direct reads %llu retired patches %llu\n",
|
|
(unsigned long long)reads, (unsigned long long)patches);
|
|
std::fprintf(stderr, "[native:texture-uploads] native completions %llu retired tag stores %llu\n",
|
|
(unsigned long long)uploads, (unsigned long long)(3 * uploads));
|
|
}
|
|
} statistics;
|
|
|
|
void submitTexture(uint8_t *ram, R5900Context *ctx, Runtime *rt)
|
|
{
|
|
const uint32_t caller = getRegU32(ctx, 31);
|
|
if (!bozo::port::nativeGraphics(rt) || (caller != 0x1f947c && caller != 0x1f977c))
|
|
{
|
|
pktAddVu1Tex__FP9Vu1DmaPktPUi_0x1ea5c8(ram, ctx, rt);
|
|
return;
|
|
}
|
|
const uint32_t control = getRegU32(ctx, 4), cursor = read<uint32_t>(rt, control + 8);
|
|
const auto controlAt = bozo::GuestMemory::flatOffset(control, 0x14);
|
|
// The original writes can feed back into a deliberately overlapping control record.
|
|
// Keep that ABI outside the native disjoint-record contract, including aliased RAM views.
|
|
const auto overlaps = [&](uint32_t address, uint32_t size)
|
|
{
|
|
const auto at = bozo::GuestMemory::flatOffset(address, size);
|
|
return controlAt >= 0 && at >= 0 && at < controlAt + 0x14 && controlAt < at + size;
|
|
};
|
|
if (overlaps(cursor & ~15u, 16) || overlaps(cursor + 4, 4) || overlaps(cursor + 8, 4))
|
|
{
|
|
pktAddVu1Tex__FP9Vu1DmaPktPUi_0x1ea5c8(ram, ctx, rt);
|
|
return;
|
|
}
|
|
// Texture data is already available to the native renderer. Preserve only the game's legacy
|
|
// extent and scalar caller ABI; no upload descriptor/tag is read, written or dispatched.
|
|
const uint32_t next = cursor + 16;
|
|
const auto at = bozo::GuestMemory::flatOffset(control + 8, 4);
|
|
std::memcpy(rt->memory.flat() + at, &next, 4);
|
|
setRegU32(ctx, 2, next);
|
|
ctx->r[3] = {};
|
|
setRegU32(ctx, 3, cursor);
|
|
setRegU32(ctx, 5, cursor);
|
|
setRegU32(ctx, 6, cursor);
|
|
setRegU64(ctx, 7, 0x11000000);
|
|
ctx->pc = caller;
|
|
++statistics.uploads;
|
|
}
|
|
|
|
// Both assembly traversals enqueue {object, texture-copy cursor, ...}. The helper preserves the
|
|
// texture index at frame+0. Read the caller from its actual saved frame, so a deferred return/resume
|
|
// needs no scoped binding, persistent packet bank or thread-local context.
|
|
bool source(Runtime *rt, const R5900Context *ctx, uint32_t pc, uint32_t &address)
|
|
{
|
|
const uint32_t sp = getRegU32(ctx, 29);
|
|
uint32_t frame = sp, queueOffset;
|
|
const bool activation = pc >= 0x1f93bc && pc <= 0x1f94a0;
|
|
const bool mip = pc == 0x1f988c;
|
|
if (activation)
|
|
{
|
|
if (read<uint64_t>(rt, sp + 0x50) != 0x1f9100) return false;
|
|
frame += 0x60;
|
|
}
|
|
else if (mip)
|
|
{
|
|
if (read<uint64_t>(rt, sp + 0xa0) != 0x1f9100) return false;
|
|
frame += 0xb0;
|
|
}
|
|
const uint64_t caller = read<uint64_t>(rt, frame + 0x20);
|
|
unsigned queueRegister;
|
|
if (caller == 0x1d330c || caller == 0x1d3340) queueRegister = 18;
|
|
else if (caller == 0x1d2d84 || caller == 0x1d2db8) queueRegister = 19;
|
|
else return false; // C builders and other callers retain their complete original ABI.
|
|
if (activation) queueOffset = read<uint32_t>(rt, sp + (queueRegister == 18 ? 0x20 : 0x30));
|
|
else if (mip) queueOffset = read<uint32_t>(rt, sp + (queueRegister == 18 ? 0x30 : 0x40));
|
|
else queueOffset = getRegU32(ctx, queueRegister);
|
|
const uint32_t index = read<uint32_t>(rt, frame);
|
|
const uint32_t object = read<uint32_t>(rt, 0x4c0a78 + queueOffset);
|
|
// pktAddVu1ObjAsm copied each source qword with an aligned 128-bit load.
|
|
address = (object + 0x70 + 48 * index) & ~15u;
|
|
return true;
|
|
}
|
|
}
|
|
bool owns(uint32_t pc)
|
|
{
|
|
return std::binary_search(std::begin(kTextureInputs), std::end(kTextureInputs), pc);
|
|
}
|
|
bool instruction(uint8_t *, R5900Context *ctx, Runtime *rt, uint32_t pc)
|
|
{
|
|
if (!bozo::port::nativeGraphics(rt) || !owns(pc)) return false;
|
|
uint32_t address;
|
|
if (!source(rt, ctx, pc, address)) return false;
|
|
if (pc == 0x1f90cc || pc == 0x1f9130)
|
|
{
|
|
setRegU64(ctx, pc == 0x1f90cc ? 3 : 5, read<uint64_t>(rt, address + (pc == 0x1f90cc ? 0 : 16)));
|
|
++statistics.reads;
|
|
}
|
|
else if (pc == 0x1f93bc || pc == 0x1f9494)
|
|
{
|
|
setRegU64(ctx, 2, read<uint16_t>(rt, address));
|
|
++statistics.reads;
|
|
}
|
|
else ++statistics.patches; // Scalar results and game texture-manager writes still execute normally.
|
|
return true;
|
|
}
|
|
void install(tmb::Registry &runtime)
|
|
{
|
|
runtime.registerFunction(kTextureUpload, &submitTexture);
|
|
}
|
|
}
|