Files
ArzarothandClaude Opus 5.5 c24b3a17e8 [sc3/texture-upscale] feat(render): texture upscaling of a port's assets, off by default
Renderer::setTextureUpscale draws a Texture marked asset (decoded from the game's
own data: packs::decodeRecord sets it) from a copy 2x or 4x as large, made by
worker threads and kept by content; until it is made the texture is drawn as it
is. Two filters written from the published method descriptions: edge-directed
(xBR family, exact coverage per output texel) and Catmull-Rom with no ringing,
both premultiplied by alpha. A repeating texture is scaled with the opposite
edge as its neighbour. The game's mip levels sit k levels down and a level
chosen from Q or fixed moves with them. Off: the same GL calls as before.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
2026-11-10 07:37:52 +01:00

119 lines
4.3 KiB
C++

// SPDX-License-Identifier: MIT
// Copyright (c) 2026 The Bozos (boubou666 and Arzaroth). MIT licence (see LICENSE at the repository root).
//
// Moved from the team's code in the PS2Recomp fork (native_texture_pack.cpp; phase4-inventory.md section 2.1).
// The texture pack's loader and decoders (runtime/native/native_texture_pack.h, docs/native_packs.md).
#include "bozo/render/texture_pack.h"
#include <cstring>
#include <fstream>
namespace bozo::render::packs
{
bool TexturePack::load(const std::string &path)
{
m_files.clear();
m_records = 0;
m_ok = false;
std::ifstream in(path, std::ios::binary);
char magic[8] = {};
uint32_t files = 0;
if (!in.read(magic, 8) || std::memcmp(magic, kMagic, 8) != 0 || !in.read(reinterpret_cast<char *>(&files), 4))
return false;
for (uint32_t f = 0; f < files && in; ++f)
{
uint16_t length = 0;
in.read(reinterpret_cast<char *>(&length), 2);
std::string name(length, '\0');
in.read(name.data(), length);
uint32_t count = 0;
in.read(reinterpret_cast<char *>(&count), 4);
std::vector<TextureRecord> &list = m_files[name];
list.resize(count);
for (TextureRecord &r : list)
{
uint32_t header[4] = {};
in.read(reinterpret_cast<char *>(header), 16);
r.psm = header[0];
r.width = header[1];
r.height = header[2];
r.texels.resize(header[3]);
in.read(reinterpret_cast<char *>(r.texels.data()), header[3]);
}
m_records += count;
}
m_ok = true;
return true;
}
const std::vector<TextureRecord> *TexturePack::file(const std::string &name) const
{
const auto it = m_files.find(name);
return it != m_files.end() ? &it->second : nullptr;
}
const TextureRecord *TexturePack::nth(const std::vector<std::string> &files, uint32_t n) const
{
for (const std::string &name : files)
{
const auto it = m_files.find(name);
if (it == m_files.end())
return nullptr;
if (n < it->second.size())
return &it->second[n];
n -= static_cast<uint32_t>(it->second.size());
}
return nullptr;
}
uint32_t ct16ToRgba(uint32_t c, uint64_t texa)
{
const uint32_t r = (c & 0x1fu) << 3, g = ((c >> 5) & 0x1fu) << 3, b = ((c >> 10) & 0x1fu) << 3;
const uint32_t ta0 = texa & 0xffu, ta1 = (texa >> 32) & 0xffu;
const bool aem = (texa >> 15) & 1u;
const uint32_t a = (c & 0x8000u) ? ta1 : ((aem && (c & 0x7fffu) == 0u) ? 0u : ta0);
return r | (g << 8) | (b << 16) | (a << 24);
}
uint32_t recordColour(const TextureRecord &r, uint32_t index, uint64_t texa)
{
if (r.psm == PSMCT16 || r.psm == PSMCT16S)
{
uint16_t c;
std::memcpy(&c, r.texels.data() + index * 2u, 2);
return ct16ToRgba(c, texa);
}
uint32_t c;
std::memcpy(&c, r.texels.data() + index * 4u, 4);
if (r.psm == PSMCT24)
{
const bool aem = (texa >> 15) & 1u;
const uint32_t a = (aem && (c & 0xffffffu) == 0u) ? 0u : static_cast<uint32_t>(texa & 0xffu);
return (c & 0xffffffu) | (a << 24);
}
return c;
}
void decodeRecord(Texture &out, uint32_t width, uint32_t height, uint32_t psm, const TextureRecord &texels,
const uint32_t palette[256], uint64_t texa)
{
const bool indexed = psmIndexed(psm);
out.width = width;
out.height = height;
out.asset = true;
out.rgba.resize(static_cast<size_t>(width) * height);
for (uint32_t y = 0; y < height; ++y)
for (uint32_t x = 0; x < width; ++x)
{
uint32_t c = 0;
if (x < texels.width && y < texels.height)
{
const uint32_t at = y * texels.width + x;
c = indexed ? palette[texels.texels[at]] : recordColour(texels, at, texa);
}
out.rgba[static_cast<size_t>(y) * width + x] = c;
}
}
}