Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -242,7 +242,7 @@ Modding support and fan translations are first-class architectural requirements:
disc0:/PSP_GAME/USRDIR/umd0.cpk, umd1.cpk (original game archives)
```
On PC, `--mods <dir>` maps `ms0:/PSP/P3P` to a host directory.
- **Texture packs (work in progress, [#23](https://github.com/endercodezz/P3P3DS/issues/23), [#56](https://github.com/endercodezz/P3P3DS/issues/56)):** existing P3P packs are PPSSPP packs (`textures.ini` + PNG). `tools/texpack` (Rust, host only) checks a pack and encodes PNG into New 3DS texture data; the runtime does not load packs yet. From `tools/texpack`: `cargo run --release -- inspect <pack dir>` and `cargo run --release -- encode <image.png> --format rgba8|rgb8|rgb565|rgba5551|rgba4 --out <file>` (the same bytes as devkitPro `tex3ds -r -z none`). The runtime computes PPSSPP-compatible texture keys (`core/include/p3p3ds/ge/texture_replacement.hpp`); to see how much of a pack a PC run would find: `cargo run --release -- keys <pack dir> > keys.txt`, then `p3p_pc_bootstrap ... --texture-keys keys.txt --texture-keys-report keys.csv` (prints `[TEXKEYS]`, the CSV lists every texture key and whether the pack has it). `cargo run --release -- build <pack dir> <out dir> [--max-size 512] [--format auto|rgba8|...]` converts a pack into the New 3DS pack format (`index.bin` + `textures.bin`, documented in `tools/texpack/src/build.rs`): images downscaled to at most `--max-size` texels per side, RGB565 / RGBA5551 / RGBA4 chosen from the alpha channel.
- **Texture packs (work in progress, [#23](https://github.com/endercodezz/P3P3DS/issues/23), [#56](https://github.com/endercodezz/P3P3DS/issues/56)):** existing P3P packs are PPSSPP packs (`textures.ini` + PNG). `tools/texpack` (Rust, host only) checks a pack and encodes PNG into New 3DS texture data; the runtime does not load packs yet. From `tools/texpack`: `cargo run --release -- inspect <pack dir>` and `cargo run --release -- encode <image.png> --format rgba8|rgb8|rgb565|rgba5551|rgba4 --out <file>` (the same bytes as devkitPro `tex3ds -r -z none`). The runtime computes PPSSPP-compatible texture keys (`core/include/p3p3ds/ge/texture_replacement.hpp`); to see how much of a pack a PC run would find: `cargo run --release -- keys <pack dir> > keys.txt`, then `p3p_pc_bootstrap ... --texture-keys keys.txt --texture-keys-report keys.csv` (prints `[TEXKEYS]`, the CSV lists every texture key and whether the pack has it). `cargo run --release -- build <pack dir> <out dir> [--max-size 512] [--format auto|rgba8|...]` converts a pack into the New 3DS pack format (`index.bin` + `textures.bin`, documented in `tools/texpack/src/build.rs`): images downscaled to at most `--max-size` texels per side, RGB565 / RGBA5551 / RGBA4 chosen from the alpha channel. Using a converted pack: copy `index.bin` and `textures.bin` to `sdmc:/p3p3ds/textures/` on the New 3DS (the bottom-screen report's `texpack:` line shows the texture count, replaced uploads, KiB and the milliseconds spent reading them), or run the PC runner with `--texture-pack <out dir>`.
- **Translation & Mod Support (Future Goal):** The architecture aims to support community translations (such as Russian, Spanish, German, French) and mods via VFS redirection. Note that translations are not guaranteed to be drop-in asset-only packages: individual localizations may require custom font sheets, character encoding tables, `.bmd`/`.bf` script handling, or runtime executable hooks (such as glyph-spacing/kerning adjustments).

---
Expand Down
1 change: 1 addition & 0 deletions cmake/p3p3ds_sources.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ set(P3P3DS_CORE_SOURCES
core/src/ge/async_renderer.cpp
core/src/ge/geometry.cpp
core/src/ge/software_renderer.cpp
core/src/ge/texture_pack.cpp
core/src/ge/texture_replacement.cpp
core/src/ge/xxhash.cpp
core/src/hle/sysmem.cpp
Expand Down
14 changes: 14 additions & 0 deletions core/include/p3p3ds/ge/renderer.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
// PPSSPP GPU/ge_constants.h was a behavioural cross-check only.
#include <array>
#include <cstdint>
#include <memory>
#include <vector>

namespace psprecomp { class GuestMemory; }
Expand Down Expand Up @@ -66,15 +67,28 @@ struct VertexLayout {
};
[[nodiscard]] VertexLayout vertex_layout(std::uint32_t vtype);

class TexturePack;

struct ReplacementStats {
std::uint64_t textured_draws{}, replaced_draws{}, loaded{}, read_errors{};
};

class SoftwareRenderer final : public GeRenderer {
public:
SoftwareRenderer();
~SoftwareRenderer() override;
void draw(psprecomp::GuestMemory &memory, const GeRegisters &regs, Prim prim, std::uint32_t count,
std::uint32_t vertex_address, std::uint32_t index_address) override;
void transfer(psprecomp::GuestMemory &memory, const GeRegisters &regs) override;
[[nodiscard]] const DrawStats &stats() const override { return stats_; }
// Texture replacement from a New 3DS pack (texture_pack.hpp); nullptr turns it off.
void set_texture_pack(std::shared_ptr<const TexturePack> pack);
[[nodiscard]] ReplacementStats replacement_stats() const;

private:
struct Replacements;
DrawStats stats_;
std::unique_ptr<Replacements> replacements_;
};

// 24-bit GE float argument -> float.
Expand Down
74 changes: 74 additions & 0 deletions core/include/p3p3ds/ge/texture_pack.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
#pragma once
// New 3DS texture pack written by `p3p-texpack build` (tools/texpack/src/build.rs
// documents the format): index.bin (entries sorted by PPSSPP-compatible key)
// and textures.bin (PICA200 tiled data, rows bottom-up). The index is read
// once; texture data is read on demand.
#include "p3p3ds/ge/texture_replacement.hpp"

#include <cstdint>
#include <cstdio>
#include <filesystem>
#include <memory>
#include <string>
#include <vector>

namespace p3p3ds::ge {

struct PackEntry {
ReplacementKey key;
std::uint32_t offset{}, size{};
std::uint16_t width{}, height{}; // 0 x 0: keep the original texture
std::uint8_t format{}; // citro3d GPU_TEXCOLOR (0 RGBA8, 1 RGB8, 2 RGBA5551, 3 RGB565, 4 RGBA4)
};

class TexturePack {
public:
// nullptr with `error` set when the directory has no valid pack.
static std::unique_ptr<TexturePack> open(const std::filesystem::path &dir, std::string &error);
~TexturePack();
TexturePack(const TexturePack &) = delete;
TexturePack &operator=(const TexturePack &) = delete;

[[nodiscard]] bool ignore_address() const noexcept { return ignore_address_; }
[[nodiscard]] std::size_t size() const noexcept { return entries_.size(); }
// The entry PPSSPP's lookup order finds for `key` (find_replacement), or nullptr.
[[nodiscard]] const PackEntry *find(const ReplacementKey &key) const;
// Texture data of an entry with a size; false on a read error.
bool read(const PackEntry &entry, std::vector<std::uint8_t> &out) const;
// The same into `out` (entry.size bytes, e.g. a C3D_Tex's data).
bool read_into(const PackEntry &entry, void *out) const;

private:
TexturePack() = default;
[[nodiscard]] const PackEntry *exact(const ReplacementKey &key) const;
std::vector<PackEntry> entries_;
bool ignore_address_{};
std::FILE *data_{};
};

// The replacement for the texture a draw binds: keeps the CLUT bytes and the
// per-texture maxSeenV PPSSPP keys depend on (texture_replacement.hpp).
// Used by the PC reference renderer and the New 3DS renderer.
class TextureReplacer {
public:
explicit TextureReplacer(std::shared_ptr<const TexturePack> pack) : pack_(std::move(pack)) {}
// nullptr when the pack has no replacement (or says "keep the original").
// `through` / `max_v`: the draw's mode and largest V texel (through_max_v).
const PackEntry *find(psprecomp::GuestMemory &memory, const GeRegisters &regs, bool through, std::uint16_t max_v);
[[nodiscard]] const TexturePack &pack() const noexcept { return *pack_; }

private:
std::shared_ptr<const TexturePack> pack_;
TextureKeys keys_;
PpssppClut clut_;
std::uint64_t clut_seen_{};
};

// Largest V texel coordinate of a through-mode draw's vertices (0 otherwise).
[[nodiscard]] std::uint16_t through_max_v(psprecomp::GuestMemory &memory, const GeRegisters &regs, std::uint32_t count,
std::uint32_t vertex_address, std::uint32_t index_address);

// Texture data of `entry` -> RGBA texels (r in the low byte), image row 0 first.
void decode_pack_texture(const PackEntry &entry, const std::vector<std::uint8_t> &data, std::vector<std::uint32_t> &out);

} // namespace p3p3ds::ge
2 changes: 2 additions & 0 deletions core/include/p3p3ds/ge/texture_replacement.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,8 @@ struct PpssppClut {
[[nodiscard]] std::uint32_t replacement_data_hash(const psprecomp::GuestMemory &memory, const GeRegisters &regs, std::uint16_t max_seen_v);

// PPSSPP's lookup order with fallbacks to keys with zeroed parts.
[[nodiscard]] std::optional<ReplacementKey> find_replacement(const std::function<bool(const ReplacementKey &)> &contains, ReplacementKey key,
bool ignore_address);
[[nodiscard]] std::optional<ReplacementKey> find_replacement(const ReplacementKeySet &pack, ReplacementKey key, bool ignore_address);

// Per-texture state PPSSPP keeps between draws (maxSeenV of 512-tall textures).
Expand Down
69 changes: 69 additions & 0 deletions core/src/ge/software_renderer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,15 @@
// (primitives with any vertex behind the eye are dropped, no near clipping).
#include "p3p3ds/ge/renderer.hpp"
#include "p3p3ds/ge/geometry.hpp"
#include "p3p3ds/ge/texture_pack.hpp"
#include "p3p3ds/ge/texture_replacement.hpp"

#include "psprecomp/guest_memory.hpp"

#include <algorithm>
#include <cmath>
#include <cstring>
#include <unordered_map>

namespace p3p3ds::ge {
namespace {
Expand All @@ -23,9 +26,17 @@ std::uint32_t pack(const std::array<std::uint8_t, 4> &c, std::uint32_t format) {
}
}

// A replacement image covering the whole PSP texture (sampled with the
// PSP texture's normalised coordinates).
struct ReplacementImage {
std::uint32_t width{}, height{};
std::vector<std::uint32_t> rgba; // r in the low byte, row 0 first
};

struct Context {
psprecomp::GuestMemory &memory;
const GeRegisters &g;
const ReplacementImage *replacement{};
std::uint32_t r(std::uint32_t i) const { return g.reg[i] & 0xFFFFFFu; }
bool enabled(std::uint32_t i) const { return (g.reg[i] & 1u) != 0u; }
};
Expand All @@ -42,6 +53,14 @@ std::array<std::uint8_t, 4> sample(const Context &c, const Texture &t, float u,
if (clamp) return std::clamp<std::int32_t>(i, 0, static_cast<std::int32_t>(size) - 1);
return static_cast<std::int32_t>(static_cast<std::uint32_t>(i) & (size - 1u));
};
if (const ReplacementImage *r = c.replacement) {
const float su = u * static_cast<float>(r->width) / static_cast<float>(t.width);
const float sv = v * static_cast<float>(r->height) / static_cast<float>(t.height);
const std::uint32_t texel = r->rgba[static_cast<std::size_t>(coord(sv, r->height, (wrap >> 8) & 1u)) * r->width +
static_cast<std::size_t>(coord(su, r->width, wrap & 1u))];
return {static_cast<std::uint8_t>(texel), static_cast<std::uint8_t>(texel >> 8), static_cast<std::uint8_t>(texel >> 16),
static_cast<std::uint8_t>(texel >> 24)};
}
return fetch_texel(c.memory, c.g, t, coord(u, t.width, wrap & 1u), coord(v, t.height, (wrap >> 8) & 1u));
}

Expand Down Expand Up @@ -205,6 +224,49 @@ VertexLayout vertex_layout(std::uint32_t vtype) {
return l;
}

struct SoftwareRenderer::Replacements {
explicit Replacements(std::shared_ptr<const TexturePack> pack) : replacer(std::move(pack)) {}
TextureReplacer replacer;
// Pack entry -> decoded image (nullptr: read error).
std::unordered_map<const PackEntry *, std::shared_ptr<const ReplacementImage>> images;
ReplacementStats stats;

const ReplacementImage *lookup(psprecomp::GuestMemory &memory, const GeRegisters &regs, bool through, std::uint16_t max_v) {
++stats.textured_draws;
const PackEntry *entry = replacer.find(memory, regs, through, max_v);
if (!entry) return nullptr;
auto [it, inserted] = images.try_emplace(entry);
if (inserted) {
std::vector<std::uint8_t> data;
if (replacer.pack().read(*entry, data)) {
auto image = std::make_shared<ReplacementImage>();
image->width = entry->width;
image->height = entry->height;
decode_pack_texture(*entry, data, image->rgba);
it->second = std::move(image);
++stats.loaded;
} else {
++stats.read_errors;
}
}
if (it->second) ++stats.replaced_draws;
return it->second.get();
}
};

SoftwareRenderer::SoftwareRenderer() = default;
SoftwareRenderer::~SoftwareRenderer() = default;

void SoftwareRenderer::set_texture_pack(std::shared_ptr<const TexturePack> pack) {
if (!pack) {
replacements_.reset();
return;
}
replacements_ = std::make_unique<Replacements>(std::move(pack));
}

ReplacementStats SoftwareRenderer::replacement_stats() const { return replacements_ ? replacements_->stats : ReplacementStats{}; }

void SoftwareRenderer::draw(psprecomp::GuestMemory &memory, const GeRegisters &regs, Prim prim, std::uint32_t count,
std::uint32_t vertex_address, std::uint32_t index_address) {
Context c{memory, regs};
Expand Down Expand Up @@ -232,6 +294,13 @@ void SoftwareRenderer::draw(psprecomp::GuestMemory &memory, const GeRegisters &r
const bool flat = (c.r(0x50) & 1u) == 0u;
if ((c.r(0xD3) & 1u) != 0u) ++stats_.clears;
const Texture *tex = textured ? &texture : nullptr;
if (textured && replacements_) {
// Largest V of the draw (texels in through mode) feeds PPSSPP's maxSeenV.
std::uint16_t max_v = 0;
if (layout.through)
for (const auto &v : vertices) max_v = std::max<std::uint16_t>(max_v, static_cast<std::uint16_t>(std::clamp(v.v, 0.0f, 65535.0f)));
c.replacement = replacements_->lookup(memory, regs, layout.through, max_v);
}

auto sprite = [&](const Vertex &a, const Vertex &b) {
++stats_.sprites;
Expand Down
Loading