diff --git a/psprecomp/ARCHITECTURE.md b/psprecomp/ARCHITECTURE.md index a69043b..85810f2 100644 --- a/psprecomp/ARCHITECTURE.md +++ b/psprecomp/ARCHITECTURE.md @@ -162,7 +162,7 @@ All under `runtime/` (headers in `runtime/include/`, sources in `runtime/src/`): | Scheduler | `psp_scheduler.cpp` | Cooperative threading (`PspThread`, yield points); `thread_local PspThread* g_current` | | HLE | `src/hle/psp_hle_*.cpp` | Firmware NID implementations: io, kernel (thread/sema/mutex/lwmutex/eventflag/memory), display, ge, ctrl, power, utility, adhoc (own row below); `sceUtilitySavedata` is PPSSPP-compatible: `psp_savedata_crypto.{h,cpp}` (sceChnnlsv/KIRK CMD4/7 AES scheme, per-file MAC in `SAVEDATA_FILE_LIST`, `SAVEDATA_PARAMS` signing) and `psp_savedata_dialog.{h,cpp}` (LISTLOAD/LISTSAVE list state machine + software-rendered overlay laid out in 480x272 design units but rasterised at an integer scale (1..4, `set_output_scale` set by `present_blit` from the output size) with FreeType text from OFL fonts embedded at build time (`assets/fonts`, `assets/embed_fonts.cmake`; Cinzel + Crimson Text) as the generic look, or, when a game module installs an `SdTheme` (`set_theme` / `set_theme_provider`: background image, 9-slice/mirrored-cap sprites for tab, selection bar, normal bar, corner ornaments, cursor, button glyphs, colours, optional TTF for body text; sprite pixels map 1:1 to design units and are bilinear-scaled by the canvas scale), the same layout drawn from that game's own UI textures — the Dissidia module builds one lazily from `menu/en/menu_lpk_label_gim_0.bin` + `menu/menu_pk_label_gim_0.bin` (`games/dissidia/runtime/savedata_theme.cpp`, `gim_decode.{h,cpp}`, read through `read_asset` so loose-file mods restyle it; `PSPRECOMP_SDTHEME=0` disables), ICON0.PNG/PARAM.SFO/size shown per row, blitted by `present_dialog_overlay` in `psp_ge_draw.cpp`; `PSPRECOMP_SAVEDATA_AUTO=1` keeps the headless auto-select); the list has a Triangle key opening **Manage saves** (`psp_savemgr_ui.{h,cpp}`: import / export of PSP save folders from PPSSPP, memory sticks or a typed path, with region retargeting + re-signing and `.bak` backups outside `SAVEDATA`; the copy logic is `psp_savemgr.{h,cpp}`, shared with `tools/savemgr`; `PSPRECOMP_SAVEMGR=0` disables; see games/dissidia/docs/savedata.md); host tests `runtime/tests/run_savedata_*.sh`, `test_savedata_hle`, `test_savemgr`; name-based registration wired to the generated `syscall_table.cpp` stub addresses via dispatch overrides (issue #40 — no per-game stub addresses in the runtime; unbound stubs get a loud per-NID unimplemented no-op that returns a deterministic `v0 = 0`; NID→stub lookup for runtime code via `psp_hle_stub_addr_for_nid`) | | GE list processor | `psp_ge.cpp` | Display-list interpretation, including SIGNAL flow-control behaviors 0x10–0x12 (JUMP/CALL/RET) | -| Renderer | `psp_ge_draw.cpp`, `psp_ge_vertex.cpp`, `psp_ge_texture.cpp`, `psp_ge_shader.cpp` | Vertex decode/transform (column-major PSP matrices), CLUT/texture decode, shaders, GL draw — deep-dive in [docs/GRAPHICS.md](docs/GRAPHICS.md). Default draw path is the batched GPU-skinning one (state-keyed batches, ring VBO synchronised by orphaning, GL 3.3 core only); `PSPRECOMP_GE_FAST=0` selects the per-PRIM CPU-skinning path. `psp_gl_stats.cpp` wraps glad's function pointers for `PSPRECOMP_GL_STATS` (call counters / per-call `glGetError`); `PSPRECOMP_GE_AB` renders every PRIM on both paths into twin targets and diffs them per present (see DEBUGGING.md) | +| Renderer | `psp_ge_draw.cpp`, `psp_ge_vertex.cpp`, `psp_ge_texture.cpp`, `psp_ge_shader.cpp` | Vertex decode/transform (column-major PSP matrices), CLUT/texture decode, shaders, GL draw — deep-dive in [docs/GRAPHICS.md](docs/GRAPHICS.md). Default draw path is the batched GPU-skinning one (state-keyed batches, ring VBO synchronised by orphaning, GL 3.3 core only); draws of 512+ vertices go through a decode cache (keyed by addresses, vertex type, material colour and a hash of the source bytes: characters are skinned on the GPU, so their vertex data is the same every frame) and are drawn from a resident VBO made once per cache entry instead of being re-decoded and re-uploaded into the ring each frame (`PSPRECOMP_DECODE_CACHE=0`, `PSPRECOMP_GPU_VCACHE=0` turn them off); `PSPRECOMP_GE_FAST=0` selects the per-PRIM CPU-skinning path. `psp_gl_stats.cpp` wraps glad's function pointers for `PSPRECOMP_GL_STATS` (call counters / per-call `glGetError`); `PSPRECOMP_GE_AB` renders every PRIM on both paths into twin targets and diffs them per present (see DEBUGGING.md) | | Present sync | `psp_present_sync.cpp` | Vertical sync (setting / `PSPRECOMP_VSYNC`: swap interval or Vulkan present mode, switched live at the next swap), the Android display refresh request (`ANativeWindow_setFrameRate` with the game's frame rate) and `[PRESENT_STATS]` present timing; called around every swap by `psp_gl_swap` (issue #67) | | Render queue | `psp_render_queue.cpp` | Condvar request queue — the only path by which GL work reaches the main thread | | Event loop | `psp_event_loop.cpp` | SDL2 event pump, quit handling, render-queue drain; routes events to the settings menu first and gates the game's input through it. Controllers are read in the background (`SDL_HINT_JOYSTICK_ALLOW_BACKGROUND_EVENTS`) but `publish_input()` mutes their buttons and sticks while no window of this process has the keyboard focus, so several instances on one machine do not share a pad (`PSPRECOMP_PAD_BACKGROUND=1` restores the old behaviour); pad state is still tracked, so held buttons are right on refocus | diff --git a/psprecomp/runtime/include/psp_ge_vertex.h b/psprecomp/runtime/include/psp_ge_vertex.h index 25afa1c..f448ee5 100644 --- a/psprecomp/runtime/include/psp_ge_vertex.h +++ b/psprecomp/runtime/include/psp_ge_vertex.h @@ -88,6 +88,13 @@ struct GeGpuVertex { }; static_assert(sizeof(GeGpuVertex) == 68, "GeGpuVertex layout is shared with the VAO setup"); +/// The decode cache's own copy of a draw's decoded vertices (decoding and storing them on a miss): nullptr when the draw +/// is not cached (fewer than 512 vertices, PSPRECOMP_DECODE_CACHE=0, memory out of range). *id is the entry's serial +/// (stable while the entry lives); ge_decode_cache_generation() changes whenever entries are dropped, so a caller keeping +/// per-entry data (GPU buffers) drops it too. The pointer is valid until the next call. +const struct GeGpuVertex* ge_decode_vertices_gpu_cached(uint8_t* rdram, const struct GeState& state, int count, uint64_t* id); +uint64_t ge_decode_cache_generation(); + /// Decode `count` NON-through vertices (honouring the index buffer) into /// model-space GeGpuVertex records with no CPU skinning / lighting / /// transform. Weights are written only for weighted vertex types. Returns false diff --git a/psprecomp/runtime/src/psp_ge_draw.cpp b/psprecomp/runtime/src/psp_ge_draw.cpp index 72d89bb..1f5c726 100644 --- a/psprecomp/runtime/src/psp_ge_draw.cpp +++ b/psprecomp/runtime/src/psp_ge_draw.cpp @@ -1559,6 +1559,8 @@ struct Batch { bool fx_timed = false; // FX shader override batch, GPU-timed (PSPRECOMP_FX_SHADER_LOG=2) bool fxl = false; // an effects draw: also drawn into the screen-space effects layer (fxl_*) float refl = 0.f; // > 0: a reflective surface (material byte / 255): its triangles go to the reflectivity mask (g_rml_tris) + GLuint persist_vao = 0; // != 0: one draw from a resident vertex buffer (gpu_vcache): no staging, no upload + size_t persist_n = 0; }; static Batch g_batch; static GLuint g_copy_buf_bound = 0; @@ -1731,10 +1733,13 @@ static void flush_batch() { const bool fxl_draw = g_batch.fxl; g_batch.fxl = false; g_batch.refl = 0.f; - const size_t n = s.stage_n; + const size_t persist_n = g_batch.persist_n; + const GLuint persist_vao = g_batch.persist_vao; + g_batch.persist_n = 0; g_batch.persist_vao = 0; + const size_t n = persist_n ? persist_n : s.stage_n; if (n == 0) { g_batch.firsts.clear(); g_batch.counts.clear(); return; } - { + if (!persist_n) { psp_prof::Scope ps(psp_prof::S_UPLOAD); if (g_copy_buf_bound != s.vbo) { glBindBuffer(GL_COPY_WRITE_BUFFER, s.vbo); @@ -1776,12 +1781,12 @@ static void flush_batch() { glBufferSubData(GL_COPY_WRITE_BUFFER, (GLintptr)(s.write * s.stride), (GLsizeiptr)(n * s.stride), s.stage.data()); } - const GLint first = static_cast(s.write); - s.write += n; + const GLint first = persist_n ? 0 : static_cast(s.write); + if (!persist_n) s.write += n; { psp_prof::Scope ps(psp_prof::S_DRAW); - gl_bind_vao(g_batch.vao); + gl_bind_vao(persist_n ? persist_vao : g_batch.vao); if (fx_timed) psp_fx_timer_begin(); if (g_no_raster != 2) { const size_t nseg = g_batch.firsts.size(); @@ -1796,7 +1801,7 @@ static void flush_batch() { if (g_no_raster != 1) glDisable(GL_RASTERIZER_DISCARD); verify_compare(g_expect.size()); } else { - const bool single = nseg == 1 || m == GL_TRIANGLES || m == GL_LINES || m == GL_POINTS; + const bool single = persist_n || nseg == 1 || m == GL_TRIANGLES || m == GL_LINES || m == GL_POINTS; if (!single) for (size_t i = 0; i < nseg; i++) g_batch.firsts[i] += first; auto issue = [&]() { if (single) glDrawArrays(m, first, static_cast(n)); @@ -1866,6 +1871,66 @@ void ge_draw_set_perf_overlay(bool on) { std::fprintf(stderr, "[DRAW] performance overlay %s\n", on ? "on" : "off"); } +// ---- Resident vertex buffers for cached decodes (PSPRECOMP_GPU_VCACHE=0 turns them off) ---- +// One static VBO + the two 3D layouts per decode-cache entry (psp_ge_vertex.cpp), made on first use and dropped when the +// cache drops its entries (its generation changes). Same attribute layout as the 3D stream (setup_stream). +struct GpuVBuf { GLuint vbo = 0, vao = 0, vao_plain = 0; }; +static std::unordered_map g_vcache; +static uint64_t g_vcache_gen = 0; +static bool gpu_vcache_on() { + static const bool on = [] { const char* e = std::getenv("PSPRECOMP_GPU_VCACHE"); return !(e && e[0] == '0'); }(); + return on; +} +static void vcache_layout(bool weights) { + const GLsizei st = static_cast(sizeof(GeGpuVertex)); + glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, st, reinterpret_cast(0)); + glEnableVertexAttribArray(0); + glVertexAttribPointer(3, 3, GL_FLOAT, GL_FALSE, st, reinterpret_cast(12)); + glEnableVertexAttribArray(3); + glVertexAttribPointer(1, 2, GL_FLOAT, GL_FALSE, st, reinterpret_cast(24)); + glEnableVertexAttribArray(1); + glVertexAttribPointer(2, 4, GL_UNSIGNED_BYTE, GL_TRUE, st, reinterpret_cast(32)); + glEnableVertexAttribArray(2); + if (weights) { + glVertexAttribPointer(4, 4, GL_FLOAT, GL_FALSE, st, reinterpret_cast(36)); + glEnableVertexAttribArray(4); + glVertexAttribPointer(5, 4, GL_FLOAT, GL_FALSE, st, reinterpret_cast(52)); + glEnableVertexAttribArray(5); + } +} +static void gpu_vcache_drop_all() { + for (auto& kv : g_vcache) { + GLuint vaos[2] = {kv.second.vao, kv.second.vao_plain}; + glDeleteVertexArrays(2, vaos); + glDeleteBuffers(1, &kv.second.vbo); + } + g_vcache.clear(); + glc::vao = ~0u; // a deleted array may be the cached binding: bind afresh next time +} +static const GpuVBuf& gpu_vcache_get(uint64_t id, const GeGpuVertex* v, size_t n) { + const uint64_t gen = ge_decode_cache_generation(); + if (gen != g_vcache_gen) { flush_batch(); gpu_vcache_drop_all(); g_vcache_gen = gen; } + auto it = g_vcache.find(id); + if (it != g_vcache.end()) return it->second; + flush_batch(); // the buffer and layout bindings below change GL state + GpuVBuf b; + glGenBuffers(1, &b.vbo); + glBindBuffer(GL_ARRAY_BUFFER, b.vbo); + glBufferData(GL_ARRAY_BUFFER, (GLsizeiptr)(n * sizeof(GeGpuVertex)), v, GL_STATIC_DRAW); + glGenVertexArrays(1, &b.vao); + glBindVertexArray(b.vao); + vcache_layout(true); + glGenVertexArrays(1, &b.vao_plain); + glBindVertexArray(b.vao_plain); + vcache_layout(false); + glBindVertexArray(0); + glc::vao = 0; // the raw binds above bypassed gl_bind_vao's cache + static int made = 0; + if (++made <= 40 || made % 100 == 0) + std::fprintf(stderr, "[GPU_VCACHE] resident buffer %d: %zu vertices (%.1f MB)\n", made, n, n * sizeof(GeGpuVertex) / 1048576.0); + return g_vcache.emplace(id, b).first->second; +} + static void setup_stream(Stream& s, size_t stride, bool is3d) { s.stride = stride; s.cap = RING_VERTS; @@ -2173,6 +2238,28 @@ static void fast_prim(uint8_t* rdram, const GeState& state, int prim_type, int c if (fep_log) psp_fep_log_draw(rdram, state, prim_type, n, ge_texture_bound_pack_id()); if (g_batch.refl > 0.f && !vb_on && path == 1) rml_capture(state, rdram, prim_type, n, g_batch.refl); // reflectivity mask if (path == 2 && fighter_mask_wanted(state)) fighter_capture(state, rdram, prim_type, n); // the fighter mask (no self-shadowing) + if (!g_verify && n >= 512 && gpu_vcache_on()) { + // a big draw whose decoded vertices the decode cache holds (characters: the same every frame): drawn from a + // resident buffer made once, instead of copying ~70 bytes a vertex into the frame's ring and uploading it + uint64_t id = 0; + const GeGpuVertex* cv; + { + psp_prof::Scope ps(psp_prof::S_DECODE); + cv = ge_decode_vertices_gpu_cached(rdram, state, n, &id); + } + if (cv) { + const GpuVBuf& vb = gpu_vcache_get(id, cv, (size_t)n); + flush_batch(); // its own batch: the one before it is drawn first + ensure_batch(vb_on ? vb_key : key, g_s3d, path == 2 ? vb.vao : vb.vao_plain, mode, 0, + rdram, vb_on ? vb_state : state, prim_type, fbo_tex, &xf); + g_batch.persist_vao = path == 2 ? vb.vao : vb.vao_plain; + g_batch.persist_n = (size_t)n; + flush_batch(); + psp_prof::verts += (uint64_t)n; + g_has_drawn_prims = true; + return; + } + } uint8_t* dst = stage_reserve(g_s3d, (size_t)n); bool ok; { diff --git a/psprecomp/runtime/src/psp_ge_vertex.cpp b/psprecomp/runtime/src/psp_ge_vertex.cpp index 0c3a1ac..d63703e 100644 --- a/psprecomp/runtime/src/psp_ge_vertex.cpp +++ b/psprecomp/runtime/src/psp_ge_vertex.cpp @@ -1,4 +1,7 @@ #include "psp_ge_vertex.h" +#include +#include +#include #include "psp_ge.h" #include "psp_ge_constants.h" #include "recomp.h" @@ -904,8 +907,129 @@ static inline T ld(const uint8_t* p) { return v; } +static bool decode_vertices_gpu_uncached(uint8_t* rdram, const GeState& state, int count, GeGpuVertex* out); + +// ---- Decode cache -------------------------------------------------------------------------------------------------------- +// Characters are skinned on the GPU, so the vertex data a model's draws read stays the same from frame to frame, and the +// decode above (3 index reads, 5 attribute conversions per vertex) was redone for every one of them every frame: on a +// phone the largest single item of a battle frame (13 % with a 40k-triangle mod body). Draws of kDecodeCacheMin+ vertices +// keep their decoded vertices, keyed by what the decode reads (addresses, count, vertex type, the material colour that +// colourless vertices take) and a hash of the source bytes, so any change to the indices or vertices decodes afresh. +// PSPRECOMP_DECODE_CACHE=0 turns it off. +namespace { +constexpr int kDecodeCacheMin = 512; +constexpr size_t kDecodeCacheBudget = size_t(64) << 20; // bytes of decoded vertices; cleared when over +struct DecodeKey { + uint32_t vaddr, iaddr, vtype, amb, alpha; + int count; + uint64_t h; + bool operator==(const DecodeKey& o) const { + return vaddr == o.vaddr && iaddr == o.iaddr && vtype == o.vtype && amb == o.amb && alpha == o.alpha && count == o.count && h == o.h; + } +}; +struct DecodeKeyHash { + size_t operator()(const DecodeKey& k) const { + uint64_t x = k.h ^ (uint64_t(k.vaddr) << 32) ^ k.iaddr ^ (uint64_t(k.vtype) << 17) ^ uint64_t(k.count) * 0x9E3779B97F4A7C15ull; + return size_t(x ^ (x >> 31)); + } +}; +struct DecodeEntry { std::vector v; uint64_t serial = 0; }; +struct DecodeCache { + std::mutex mu; + std::unordered_map map; + size_t bytes = 0; + uint64_t hits = 0, misses = 0, serial = 0, generation = 1; +}; +DecodeCache& decode_cache() { static DecodeCache c; return c; } +bool decode_cache_on() { + static const bool on = [] { const char* e = std::getenv("PSPRECOMP_DECODE_CACHE"); return !(e && e[0] == '0'); }(); + return on; +} +inline uint64_t hash_bytes(const uint8_t* p, size_t n, uint64_t h) { + size_t i = 0; + for (; i + 8 <= n; i += 8) { uint64_t w; std::memcpy(&w, p + i, 8); h = (h ^ w) * 0x9E3779B97F4A7C15ull; h ^= h >> 29; } + for (; i < n; i++) { h = (h ^ p[i]) * 0x100000001B3ull; } + return h; +} +} // namespace + +// The cache entry for this draw (decoded and stored on a miss), nullptr when the draw is not cached. Holds c.mu while it +// works; the returned pointer is valid until the next call (an insert may clear the map). +static const DecodeEntry* decode_cache_entry(uint8_t* rdram, const GeState& state, int count) { + if (count < kDecodeCacheMin || !decode_cache_on()) return nullptr; + const uint32_t vtype = state.vertex_type; + const int stride = vtx_layout(vtype).stride; + const int idx_type = ge_vtype_idx(vtype); + if (stride <= 0) return nullptr; + auto in_ram = [](uint32_t addr, uint64_t bytes) { return addr >= 0x00010000U && (uint64_t)(addr & 0x07FFFFFFU) + bytes <= 0x08000000ULL; }; + uint64_t h = 0xCBF29CE484222325ull; + uint32_t nverts = (uint32_t)count; + if (idx_type != GE_VTYPE_IDX_NONE) { + const uint32_t isz = idx_type == GE_VTYPE_IDX_8BIT ? 1u : idx_type == GE_VTYPE_IDX_16BIT ? 2u : 4u; + if (!in_ram(state.index_addr, (uint64_t)count * isz)) return nullptr; + const uint8_t* ip = rdram + (state.index_addr & 0x07FFFFFFU); + h = hash_bytes(ip, (size_t)count * isz, h); + uint32_t mx = 0; + if (isz == 2) { for (int i = 0; i < count; i++) { uint16_t v; std::memcpy(&v, ip + i * 2, 2); mx = std::max(mx, v); } } + else if (isz == 1) { for (int i = 0; i < count; i++) mx = std::max(mx, ip[i]); } + else { for (int i = 0; i < count; i++) { uint32_t v; std::memcpy(&v, ip + i * 4, 4); mx = std::max(mx, v); } } + nverts = mx + 1; + } + if (!in_ram(state.vertex_addr, (uint64_t)nverts * stride)) return nullptr; + h = hash_bytes(rdram + (state.vertex_addr & 0x07FFFFFFU), (size_t)nverts * stride, h); + const DecodeKey key{state.vertex_addr, idx_type != GE_VTYPE_IDX_NONE ? state.index_addr : 0u, vtype, + state.material_ambient, state.material_alpha, count, h}; + DecodeCache& c = decode_cache(); + auto report = [&] { + if (((c.hits + c.misses) & 0xFFF) == 0) + std::fprintf(stderr, "[DECODE_CACHE] %llu hits, %llu misses, %zu entries, %.1f MB\n", (unsigned long long)c.hits, + (unsigned long long)c.misses, c.map.size(), c.bytes / 1048576.0); + }; + auto it = c.map.find(key); + if (it != c.map.end()) { c.hits++; report(); return &it->second; } + DecodeEntry e; + e.v.resize((size_t)count); + if (!decode_vertices_gpu_uncached(rdram, state, count, e.v.data())) return nullptr; + c.misses++; report(); + const size_t b = (size_t)count * sizeof(GeGpuVertex); + if (c.bytes + b > kDecodeCacheBudget) { c.map.clear(); c.bytes = 0; c.generation++; } + e.serial = ++c.serial; + c.bytes += b; + return &c.map.emplace(key, std::move(e)).first->second; +} + bool ge_decode_vertices_gpu( uint8_t* rdram, const GeState& state, int count, GeGpuVertex* out +) { + if (count >= kDecodeCacheMin && decode_cache_on()) { + DecodeCache& c = decode_cache(); + std::lock_guard lk(c.mu); + if (const DecodeEntry* e = decode_cache_entry(rdram, state, count)) { + std::memcpy(out, e->v.data(), (size_t)count * sizeof(GeGpuVertex)); + return true; + } + } + return decode_vertices_gpu_uncached(rdram, state, count, out); +} + +const GeGpuVertex* ge_decode_vertices_gpu_cached(uint8_t* rdram, const GeState& state, int count, uint64_t* id) { + if (count < kDecodeCacheMin || !decode_cache_on()) return nullptr; + DecodeCache& c = decode_cache(); + std::lock_guard lk(c.mu); + const DecodeEntry* e = decode_cache_entry(rdram, state, count); + if (!e) return nullptr; + *id = e->serial; + return e->v.data(); +} + +uint64_t ge_decode_cache_generation() { + DecodeCache& c = decode_cache(); + std::lock_guard lk(c.mu); + return c.generation; +} + +static bool decode_vertices_gpu_uncached( + uint8_t* rdram, const GeState& state, int count, GeGpuVertex* out ) { const uint32_t vtype = state.vertex_type; const VtxLayout L = vtx_layout(vtype);