diff --git a/psprecomp/games/dissidia/docs/native-build.md b/psprecomp/games/dissidia/docs/native-build.md index d27f4ee..f032318 100644 --- a/psprecomp/games/dissidia/docs/native-build.md +++ b/psprecomp/games/dissidia/docs/native-build.md @@ -116,7 +116,7 @@ wraps the battle camera update (`ovl_battle FUN_08B3CA24`, once per frame) and r around the fighter you control (the fighter nearest to the game's own eye; the whole rig, eye and look-at, turns about him, so he keeps his place on the screen; `PSPRECOMP_CAM_PIVOT=0` orbits the game's look-at point instead, the old way) by an accumulated yaw/pitch offset, so the game's own d-pad camera, lock-on framing, auto-centring and collision pull-in keep working underneath (the stick is an offset on top: when you let go of the stick it eases back to the game's camera after 0.5 s, with a time constant of 1 s; `PSPRECOMP_CAM_RECENTER=` changes the time constant, -`PSPRECOMP_CAM_RECENTER_DELAY=` the pause, `PSPRECOMP_CAM_RECENTER=0` holds the offset until you move it again); R3 (right stick click) or the C key puts the camera back at once. Stick right turns the view right (`PSPRECOMP_CAM_INVERT_X=1` for the other way). While the view is turned, the left stick is turned by the same angle (`PSPRECOMP_CAM_MOVE_REL=0` turns that off), so "up" goes into the screen instead of towards the opponent. When the fighter has no lock-on target (after having had one in the fight, or 10 s into it: `PSPRECOMP_CAM_HOLD_ARM=`) the camera keeps following the fighter but holds its direction and distance: the left stick never turns it and it does not re-centre, only the right stick turns it (`PSPRECOMP_CAM_UNLOCK_HOLD=0` turns this off; `PSPRECOMP_CAM_FORCE_UNLOCK=` is a test aid that reports 'not locked' that many seconds after the first lock; `PSPRECOMP_CAMLOCK_LOG=` logs lock changes). Walls that block the view of your fighter while you move the camera yourself (turned view or hold) fade to see-through, like the game's own wall fade does for its own camera (`PSPRECOMP_CAM_BLOCKER_FADE=0` turns it off): rays from the camera to the fighter's feet, knees, chest, head and shoulders; any non-skinned stage draw (up to 1500 vertices) they pass through is drawn blended with constant factors and no depth writes (`ge_draw_set_view_blocker`). Faded walls still count for the ray-traced shadows (the RT scene is captured separately from drawing). Outside battle (menus) the right stick acts as the +`PSPRECOMP_CAM_RECENTER_DELAY=` the pause, `PSPRECOMP_CAM_RECENTER=0` holds the offset until you move it again); R3 (right stick click) or the C key puts the camera back at once. Stick right turns the view right (`PSPRECOMP_CAM_INVERT_X=1` for the other way). While the view is turned, the left stick is turned by the same angle (`PSPRECOMP_CAM_MOVE_REL=0` turns that off), so "up" goes into the screen instead of towards the opponent. When the fighter has no lock-on target (after having had one in the fight, or 10 s into it: `PSPRECOMP_CAM_HOLD_ARM=`) the camera keeps following the fighter but holds its direction and distance: the left stick never turns it and it does not re-centre, only the right stick turns it (`PSPRECOMP_CAM_UNLOCK_HOLD=0` turns this off; `PSPRECOMP_CAM_FORCE_UNLOCK=` is a test aid that reports 'not locked' that many seconds after the first lock; `PSPRECOMP_CAMLOCK_LOG=` logs lock changes). The turned view is smoothed: the stick drives a turn speed that eases in and out (`PSPRECOMP_CAM_STICK_TC`, s), the view orbits your own fighter (not whichever fighter is nearest the lens) through a pivot that follows him with a short lag (`PSPRECOMP_CAM_PIVOT_TC`), its distance eases after the game's (`PSPRECOMP_CAM_DIST_TC`; the game zooms and pulls in for its own view direction), the held view follows jumps with a lag (`PSPRECOMP_CAM_HOLD_TC`), and re-centring ramps in. Your own view (turned or held) collides with the stage: the runtime keeps the main view's static stage geometry in world space and traces the line from the fighter's chest to the eye (`ge_draw_camera_probe_*`, a frame late); the eye pulls in fast when it is blocked and lets out slowly, so a turned view stays inside a room instead of looking at the back of a wall (`PSPRECOMP_CAM_COLLIDE=0` turns it off). What still blocks the view of your fighter fades to see-through (`PSPRECOMP_CAM_BLOCKER_FADE=0` turns it off, `PSPRECOMP_CAM_BLOCKER_ALPHA` sets how opaque, default 0.25): rays from the camera to the fighter's knees, chest, head and shoulders; each drawn instance of a non-skinned stage draw (up to 1500 vertices, not lower than his knees) they pass through is drawn blended with constant factors and no depth writes (`ge_draw_set_view_blocker`); it fades out in 0.08 s and back in over 0.3 s, also after the view returns to the game's. `PSPRECOMP_CAMF_LOG=` logs the final eye of n camera updates (tests; the test script's `rstick ` holds the right stick). Faded walls still count for the ray-traced shadows (the RT scene is captured separately from drawing). Outside battle (menus) the right stick acts as the D-pad. Options: `PSPRECOMP_CAM_FREE=0` (stick is a plain D-pad everywhere, the game's own camera), `PSPRECOMP_CAM_SENS` (default 1.0 = ~150 deg/s at full deflection), `PSPRECOMP_CAM_INVERT_Y=1`, `PSPRECOMP_CAM_PITCH_MIN/MAX` (eye elevation limits in degrees, default -5..80), `PSPRECOMP_RSTICK_DEADZONE` (default 0.20), `PSPRECOMP_RSTICK_DPAD=0` (right stick diff --git a/psprecomp/games/dissidia/runtime/camera.cpp b/psprecomp/games/dissidia/runtime/camera.cpp index b935ee2..1de0ece 100644 --- a/psprecomp/games/dissidia/runtime/camera.cpp +++ b/psprecomp/games/dissidia/runtime/camera.cpp @@ -88,6 +88,27 @@ bool g_game_valid = false; float g_turn = 0.f, g_relock_w = 0.f; int g_lock_logs = 0; constexpr uint32_t ACT_POS = 0x80u, ACT_TARGET = 0x2FCu; // actor position (vec3), its target actor (roster.cpp) +// Smoothing (all frame-rate independent, time constants in seconds): +// * the stick drives an angular velocity that eases towards the stick (g_stick_tc), so turns start and stop softly; +// * the orbit pivot (the player's chest) follows the fighter with a short lag (g_pivot_tc): with the view turned, eye = +// pivot + R(game eye - pivot), so every jitter of a raw pivot (steps, landing, dashes) was multiplied on screen; +// * the orbit radius follows the game's own distance with a lag (g_dist_tc): the game zooms and pulls in for ITS view +// direction (collision behind its own eye, lock-on framing), which pumped the turned view in and out; +// * the held rig (no lock-on) follows the fighter with a lag, more vertically (jumps no longer bob the view 1:1); +// * re-centring ramps in after the idle delay instead of starting at full speed. +float g_dt = 0.f; +float g_stick_tc = 0.07f, g_pivot_tc = 0.12f, g_dist_tc = 0.35f, g_hold_tc = 0.15f, g_hold_tc_y = 0.4f; +float g_vyaw = 0.f, g_vpitch = 0.f; // angular velocity (rad/s) +float g_piv[3] = {0, 0, 0}; bool g_piv_ok = false; +float g_rad = 0.f; bool g_rad_ok = false; +float g_off_s[3] = {0, 0, 0}; bool g_off_ok = false; +float ease_k(float tc) { return tc > 0.f ? 1.f - std::exp(-g_dt / tc) : 1.f; } +// Collision of our own view (turned or held) with the stage (ge_draw_camera_probe_*): the game pulls its camera in for walls +// behind ITS eye only. The allowed distance pulls in fast and lets out slowly. PSPRECOMP_CAM_COLLIDE=0 turns it off. +bool g_cc_on = true; +float g_col = 0.f; bool g_col_ok = false; +uint32_t g_cc_id = 0; +bool g_cc_done = false; // once per update (the game sets the eye a second time with the shake added) double now_s() { return std::chrono::duration(std::chrono::steady_clock::now().time_since_epoch()).count(); @@ -179,9 +200,10 @@ void dissidia_cam_update(uint8_t* rdram, recomp_context* ctx) { } const double t = now_s(); double dt = t - g_last_t; - if (g_last_t == 0 || dt > 1.0) { g_yaw = g_pitch = 0.f; dt = 0; g_ever_locked = g_held_valid = false; g_locked = true; } // new battle / long pause: reset + if (g_last_t == 0 || dt > 1.0) { g_yaw = g_pitch = g_vyaw = g_vpitch = 0.f; dt = 0; g_ever_locked = g_held_valid = false; g_locked = true; g_piv_ok = g_rad_ok = g_off_ok = false; } // new battle / long pause: reset g_last_t = t; if (dt > 0.1) dt = 0.1; + g_dt = static_cast(dt); g_mgr = static_cast(ctx->r[4]); // Widescreen: the camera object keeps its aspect ratio at +0xCC (+0xC0 = {fov, near, far, aspect}, 480/272 on the PSP). The game // culls its objects / shadow casters with it, so a wider aspect makes it draw everything the wider picture shows (no pop-in at the @@ -227,22 +249,32 @@ void dissidia_cam_update(uint8_t* rdram, recomp_context* ctx) { g_holding = known && !locked && g_held_valid; if (known && locked && g_relock_w <= 0.f) g_held_valid = false; } - if (psp_ctrl_take_camera_recenter()) { g_yaw = g_pitch = 0.f; g_idle = 0.f; } // R3 / C: back to the game's camera now + if (psp_ctrl_take_camera_recenter()) { g_yaw = g_pitch = g_vyaw = g_vpitch = 0.f; g_idle = 0.f; } // R3 / C: back to the game's camera now g_rstick_consumed_until_ms.store(now_ms() + 400, std::memory_order_relaxed); constexpr float kRate = 2.6f; // rad/s at full deflection and sens 1.0 - if (active) { - g_idle = 0.f; - g_yaw -= (g_invert_x ? -sx : sx) * g_sens * kRate * static_cast(dt); // stick right: the view turns right - g_pitch += (g_invert ? sy : -sy) * g_sens * kRate * 0.6f * static_cast(dt); // stick up: the view tilts up - } else { + { // response curve (fine aim near the centre, full speed at the rim), then ease the angular velocity towards it + const float m = std::sqrt(sx * sx + sy * sy); + const float c = m > 1e-4f ? std::pow(std::fmin(m, 1.f), 1.6f) / m : 0.f; + const float wy = active ? -(g_invert_x ? -sx : sx) * c * g_sens * kRate : 0.f; // stick right: the view turns right + const float wp = active ? (g_invert ? sy : -sy) * c * g_sens * kRate * 0.6f : 0.f; // stick up: the view tilts up + const float k = ease_k(g_stick_tc); + g_vyaw += (wy - g_vyaw) * k; g_vpitch += (wp - g_vpitch) * k; + if (!active && std::fabs(g_vyaw) < 1e-3f) g_vyaw = 0.f; + if (!active && std::fabs(g_vpitch) < 1e-3f) g_vpitch = 0.f; + g_yaw += g_vyaw * g_dt; g_pitch += g_vpitch * g_dt; + } + if (active) g_idle = 0.f; + else { g_idle += static_cast(dt); // Let go of the stick: after a short pause (a brief release while adjusting must not snap back) the offset - // eases back to the game's own camera, so a big turn cannot leave the fighter out of sight. + // eases back to the game's own camera, so a big turn cannot leave the fighter out of sight. The pull ramps in + // over half a second instead of starting at full speed. if (g_recenter > 0.f && g_idle > g_recenter_delay && dt > 0 && !(g_lock_known && !g_locked)) { - float k = std::exp(-static_cast(dt) / g_recenter); + const float ramp = smooth01((g_idle - g_recenter_delay) / 0.5f); + float k = std::exp(-static_cast(dt) * ramp / g_recenter); g_yaw *= k; g_pitch *= k; - if (std::fabs(g_yaw) < 0.002f) g_yaw = 0.f; - if (std::fabs(g_pitch) < 0.002f) g_pitch = 0.f; + if (std::fabs(g_yaw) < 0.002f && g_vyaw == 0.f) g_yaw = 0.f; + if (std::fabs(g_pitch) < 0.002f && g_vpitch == 0.f) g_pitch = 0.f; } } // Yaw is unbounded but kept in (-pi, pi]. @@ -252,9 +284,18 @@ void dissidia_cam_update(uint8_t* rdram, recomp_context* ctx) { g_cine_seen = g_dissidia_cine_seq; g_cine_actor = g_dissidia_cine_actor; g_cine_t0 = t; g_cine_len = g_dissidia_cine_ticks / 60.0; g_cine_side = 0.f; } - g_in_update = true; g_eye_calls = 0; + g_in_update = true; g_eye_calls = 0; g_cc_done = false; g_orig_update(rdram, ctx); g_in_update = false; + { // PSPRECOMP_CAMF_LOG=: one line per update for n updates (camera tests: smoothness of the final eye) + static int left = [] { const char* e = std::getenv("PSPRECOMP_CAMF_LOG"); return e ? std::atoi(e) : 0; }(); + float P[3] = {0, 0, 0}; + if (left > 0 && pl_pos(P)) { + left--; + std::fprintf(stderr, "[CAMF] t=%.4f yaw=%.4f pitch=%.4f hold=%d eye=%.4f,%.4f,%.4f game=%.4f,%.4f,%.4f pl=%.4f,%.4f,%.4f\n", t, g_yaw, g_pitch, (int)g_holding, + g_final_eye[0], g_final_eye[1], g_final_eye[2], g_game_e[0], g_game_e[1], g_game_e[2], P[0], P[1], P[2]); + } + } { // our own camera (turned view or hold): the game's wall fade used its eye, so fade what blocks ours float P[3]; const bool ours = (g_yaw != 0.f || g_pitch != 0.f || g_holding) && g_blocker_on && pl_pos(P); @@ -277,14 +318,20 @@ void dissidia_cam_seteye(uint8_t* rdram, recomp_context* ctx) { { // the held rig keeps its orientation and distance but moves with the player's fighter float P[3]; if (g_held_valid && pl_pos(P)) { - if (!g_held_p_set) { std::memcpy(g_held_p, P, sizeof P); g_held_p_set = true; } - for (int i = 0; i < 3; i++) g_off[i] = P[i] - g_held_p[i]; - } else g_off[0] = g_off[1] = g_off[2] = 0.f; + if (!g_held_p_set) { std::memcpy(g_held_p, P, sizeof P); g_held_p_set = true; g_off_ok = false; } + float want[3]; + for (int i = 0; i < 3; i++) want[i] = P[i] - g_held_p[i]; + const float d2 = (want[0] - g_off_s[0]) * (want[0] - g_off_s[0]) + (want[1] - g_off_s[1]) * (want[1] - g_off_s[1]) + (want[2] - g_off_s[2]) * (want[2] - g_off_s[2]); + if (!g_off_ok || d2 > 15.f * 15.f) { std::memcpy(g_off_s, want, sizeof want); g_off_ok = true; } // first frame / teleport: no swoop + const float kh = ease_k(g_hold_tc), kv = ease_k(g_hold_tc_y); + for (int i = 0; i < 3; i++) g_off_s[i] += (want[i] - g_off_s[i]) * (i == 1 ? kv : kh); + std::memcpy(g_off, g_off_s, sizeof g_off); + } else { g_off[0] = g_off[1] = g_off[2] = 0.f; g_off_ok = false; } } float he[3], ht[3]; for (int i = 0; i < 3; i++) { he[i] = g_held_e[i] + g_off[i]; ht[i] = g_held_t[i] + g_off[i]; } if (g_relock_w > 0.f && !g_holding && g_held_valid) { // easing back to the game's camera after a lock - g_relock_w -= 0.05f; + g_relock_w -= g_dt / 0.6f; // 0.6 s at any frame rate (was 20 updates) const float k = smooth01(g_relock_w < 0.f ? 0.f : g_relock_w); if (g_relock_w <= 0.f) g_held_valid = false; for (int i = 0; i < 3; i++) { wrf(eye + 4 * i, ge[i] + (he[i] - ge[i]) * k); wrf(eye + 0x10 + 4 * i, gt[i] + (ht[i] - gt[i]) * k); } @@ -304,15 +351,35 @@ void dissidia_cam_seteye(uint8_t* rdram, recomp_context* ctx) { } } float pivot[3]; + if (g_in_update && cam == g_mgr && g_eye_calls == 0 && g_yaw == 0.f && g_pitch == 0.f) g_piv_ok = g_rad_ok = false; // back on the game's camera: start fresh next time if (g_in_update && cam == g_mgr && g_eye_calls == 0 && (g_yaw != 0.f || g_pitch != 0.f) && g_pivot_on) { // Orbit the fighter, not the game's look-at point (which sits in front of him): the whole rig, eye and look-at, // turns about a vertical axis through the fighter, and tilts about a horizontal one, so he keeps his place on the // screen and can't be lost behind the lens. The pivot is the fighter nearest to the game's own eye. const float ev[3] = {rdf(eye), rdf(eye + 4), rdf(eye + 8)}; - if (held_now ? pl_pos(pivot) : dissidia_nearest_fighter(ev, pivot)) { + // The player's own fighter (the nearest fighter to the lens could switch to the opponent when he came close and + // throw the view across the stage), the nearest fighter only when the player is not known. + if (pl_pos(pivot) || (!held_now && dissidia_nearest_fighter(ev, pivot))) { g_eye_calls++; pivot[1] += 1.0f; // about the chest + { + const float d2 = (pivot[0] - g_piv[0]) * (pivot[0] - g_piv[0]) + (pivot[1] - g_piv[1]) * (pivot[1] - g_piv[1]) + (pivot[2] - g_piv[2]) * (pivot[2] - g_piv[2]); + if (!g_piv_ok || d2 > 15.f * 15.f) { std::memcpy(g_piv, pivot, sizeof pivot); g_piv_ok = true; } // first use / teleport + const float k = ease_k(g_pivot_tc); + for (int i = 0; i < 3; i++) { g_piv[i] += (pivot[i] - g_piv[i]) * k; pivot[i] = g_piv[i]; } + } float E[3] = {ev[0] - pivot[0], ev[1] - pivot[1], ev[2] - pivot[2]}; + if (!held_now) { // the orbit radius eases after the game's distance instead of jumping with it + const float L = std::sqrt(E[0] * E[0] + E[1] * E[1] + E[2] * E[2]); + if (L > 1e-3f) { + if (!g_rad_ok) { g_rad = L; g_rad_ok = true; } + g_rad += (L - g_rad) * ease_k(g_dist_tc); + // full effect once the view is turned a little, none at zero offset (no pop when the offset reaches 0) + const float w = smooth01(std::sqrt(g_yaw * g_yaw + g_pitch * g_pitch) / 0.15f); + const float s = 1.f + (g_rad / L - 1.f) * w; + for (int i = 0; i < 3; i++) E[i] *= s; + } + } float T[3] = {rdf(eye + 0x10) - pivot[0], rdf(eye + 0x14) - pivot[1], rdf(eye + 0x18) - pivot[2]}; const float horiz = std::sqrt(E[0] * E[0] + E[2] * E[2]); if (horiz > 1e-3f) { @@ -363,6 +430,30 @@ void dissidia_cam_seteye(uint8_t* rdram, recomp_context* ctx) { wrf(eye + 8, tz + std::cos(naz) * ch); } } + if (g_in_update && cam == g_mgr && g_cc_on && !g_cc_done) { + g_cc_done = true; + const float wgt = held_now ? 1.f : smooth01(std::sqrt(g_yaw * g_yaw + g_pitch * g_pitch) / 0.15f); + float P[3]; + if (wgt > 0.f && pl_pos(P)) { + P[1] += 1.0f; // chest + const float e[3] = {rdf(eye), rdf(eye + 4), rdf(eye + 8)}; + const float dir[3] = {e[0] - P[0], e[1] - P[1], e[2] - P[2]}; + const float L = std::sqrt(dir[0] * dir[0] + dir[1] * dir[1] + dir[2] * dir[2]); + if (L > 1.2f) { + constexpr float kStart = 0.6f, kMargin = 0.35f, kMin = 1.2f; // the test starts off the body; the eye stays this far off the wall + const float s0[3] = {P[0] + dir[0] / L * kStart, P[1] + dir[1] / L * kStart, P[2] + dir[2] / L * kStart}; + ge_draw_camera_probe_request(s0, e, ++g_cc_id); + uint32_t rid = 0; + const float r = ge_draw_camera_probe_result(&rid); + float want = L; + if (rid && g_cc_id - rid < 8 && r < 1.f) want = std::fmax(kMin, kStart + r * (L - kStart) - kMargin); + if (!g_col_ok) { g_col = L; g_col_ok = true; } + g_col += (want - g_col) * ease_k(want < g_col ? 0.06f : 0.5f); + const float dist = L + (std::fmin(L, g_col) - L) * wgt; + if (dist < L - 1e-3f) for (int i = 0; i < 3; i++) wrf(eye + 4 * i, P[i] + dir[i] / L * dist); + } + } else g_col_ok = false; + } if (held_now) { // the stick is read in the game's own frame: turn it by how far this view is from the game's float d = std::atan2(rdf(eye) - rdf(eye + 0x10), rdf(eye + 8) - rdf(eye + 0x18)) - game_az; const float PI = 3.14159265f; @@ -402,6 +493,11 @@ void dissidia_camera_install(uint8_t* rdram) { if ((e = std::getenv("PSPRECOMP_CAM_RECENTER"))) g_recenter = static_cast(std::atof(e)); if ((e = std::getenv("PSPRECOMP_CAM_RECENTER_DELAY"))) g_recenter_delay = static_cast(std::atof(e)); if ((e = std::getenv("PSPRECOMP_CAMX_LOG"))) g_log_left = std::atoi(e); + if ((e = std::getenv("PSPRECOMP_CAM_COLLIDE"))) g_cc_on = e[0] != '0'; + if ((e = std::getenv("PSPRECOMP_CAM_STICK_TC"))) g_stick_tc = static_cast(std::atof(e)); + if ((e = std::getenv("PSPRECOMP_CAM_PIVOT_TC"))) g_pivot_tc = static_cast(std::atof(e)); + if ((e = std::getenv("PSPRECOMP_CAM_DIST_TC"))) g_dist_tc = static_cast(std::atof(e)); + if ((e = std::getenv("PSPRECOMP_CAM_HOLD_TC"))) { g_hold_tc = static_cast(std::atof(e)); g_hold_tc_y = g_hold_tc * 2.6f; } g_hold_on = !((e = std::getenv("PSPRECOMP_CAM_UNLOCK_HOLD")) && e[0] == '0'); if ((e = std::getenv("PSPRECOMP_CAMLOCK_LOG"))) g_lock_logs = std::atoi(e); if ((e = std::getenv("PSPRECOMP_CAM_HOLD_ARM"))) g_arm_ticks = static_cast(std::atof(e)); diff --git a/psprecomp/games/dissidia/runtime/framerate.cpp b/psprecomp/games/dissidia/runtime/framerate.cpp index f918c2c..0d3a528 100644 --- a/psprecomp/games/dissidia/runtime/framerate.cpp +++ b/psprecomp/games/dissidia/runtime/framerate.cpp @@ -193,6 +193,7 @@ uint64_t g_fighter_seen[4] = {}; // g_tick when the fighter's step was last as // tap [ms] press, release after ms (default 100) // analog stick only // mark [TEST] line in the log +// rstick hold the right stick (128 128 = centre; camera tests) // quit exit the game // sync [timeout] [scr] hold the script until the player's action (script id) changes (or becomes `scr`); that update is T0 // batch hold it until exists, then play (and delete) the lines in it: a driver feeds trials into a running battle @@ -290,6 +291,9 @@ void parse_script(std::istream& in, std::vector& ev) { } else if (cmd == "analog") { unsigned ax = 128, ay = 128; ss >> ax >> ay; ev.push_back({t, 1, 0, (ay & 255u) << 8 | (ax & 255u), ""}); + } else if (cmd == "rstick") { // rstick : hold the right stick there (128 128 = let go); camera tests + unsigned ax = 128, ay = 128; ss >> ax >> ay; + ev.push_back({t, 17, 0, (ay & 255u) << 8 | (ax & 255u), ""}); } else if (cmd == "mark") { std::string rest; std::getline(ss, rest); ev.push_back({t, 2, 0, 0, rest}); @@ -559,6 +563,11 @@ void testdrive_frame(uint8_t* rdram, recomp_context* ctx) { continue; } if (e.kind == 16) { psp_fx_lights_set(e.a != 0); continue; } + if (e.kind == 17) { // rstick: the debug socket's right-stick injection, held until the next rstick + g_injected_ranalog.store(e.b, std::memory_order_relaxed); + g_injected_ranalog_deadline_ms.store(e.b == 0x8080u ? 0 : INT64_MAX, std::memory_order_relaxed); + continue; + } if (e.kind == 15) { const uint32_t a = test_fighter(e.text == "cpu" ? 1u : 0u); if (a) { w32(a + e.a, e.b); std::fprintf(stderr, "[TEST] gt=%.0f poke %s +%X = %d\n", g_gt_ms, e.text.c_str(), e.a, static_cast(e.b)); } diff --git a/psprecomp/runtime/include/psp_ge_draw.h b/psprecomp/runtime/include/psp_ge_draw.h index 12dc34d..2c01da1 100644 --- a/psprecomp/runtime/include/psp_ge_draw.h +++ b/psprecomp/runtime/include/psp_ge_draw.h @@ -98,6 +98,11 @@ void ge_draw_flash(uint32_t rgb, int ms); /// player's chest) fades out to a see-through alpha, like the game's own wall fade does for its camera (the game computes that from its /// own eye, so a camera we move ourselves needs it done here). Call once per camera update (game thread). void ge_draw_set_view_blocker(bool active, const float target[3], const float eye[3]); // eye: the game camera's eye this update; only draws made from that view fade +// Camera collision against the stage geometry the main view has drawn (static, non-skinned, depth-writing draws, cached in world +// space per stage): ask for the segment from -> to (game thread); the answer, the fraction of the segment before its first hit +// (1 = clear), comes from the render thread a frame later. Returns the latest answer and the id of the request it belongs to. +void ge_draw_camera_probe_request(const float from[3], const float to[3], uint32_t id); +float ge_draw_camera_probe_result(uint32_t* id); /// Game layer: a translucent RGBA image (straight alpha, byte order R,G,B,A, row 0 = top) that a game module draws over the game's /// picture, covering the PSP's 480x272 area (w x h = 480*s x 272*s), under dialogs, menus and touch controls. Used to add elements to diff --git a/psprecomp/runtime/src/psp_ge_draw.cpp b/psprecomp/runtime/src/psp_ge_draw.cpp index 947e5a4..72d89bb 100644 --- a/psprecomp/runtime/src/psp_ge_draw.cpp +++ b/psprecomp/runtime/src/psp_ge_draw.cpp @@ -1336,7 +1336,10 @@ static std::atomic g_fps_counter{false}; // simple transparent fps numbe static std::atomic g_perf_hud{false}; // settings menu Debug page / PSPRECOMP_PERF_HUD=1 static Stream g_s2d, g_s3d; static std::atomic g_vb_active{false}; // view blocker fade (ge_draw_set_view_blocker) +static std::atomic g_vb_linger{false}; // some draw is still (partly) faded: keep evaluating so it fades back in instead of popping static float vb_fade_factor(uint8_t* rdram, const GeState& st, int n, int prim); +static std::atomic g_cc_collect{false}; // camera collision: cache the main view's stage geometry (a battle camera is running) +static void cc_collect(uint8_t* rdram, const GeState& st, int n, int prim); // Ring size in vertices. 3D vertices are 68 bytes, 2D 28: 8.9 MB + 3.6 MB. // Sized for several frames of high-poly characters; small enough for an APU // (Steam Deck) where it is carved from shared system memory. A single batch @@ -2148,8 +2151,10 @@ static void fast_prim(uint8_t* rdram, const GeState& state, int prim_type, int c const int path = kc.path; const KeyBuf& key = kc.key; float vb_f = 1.f; - if (g_vb_active.load(std::memory_order_relaxed) && path == 1 && state.depth_test_enable && !state.depth_write_disable) + if ((g_vb_active.load(std::memory_order_relaxed) || g_vb_linger.load(std::memory_order_relaxed)) && path == 1 && state.depth_test_enable && !state.depth_write_disable) vb_f = vb_fade_factor(rdram, state, n, prim_type); + if (path == 1 && state.depth_test_enable && !state.depth_write_disable && g_cc_collect.load(std::memory_order_relaxed)) + cc_collect(rdram, state, n, prim_type); GeState vb_state; KeyBuf vb_key; const bool vb_on = vb_f < 0.999f; if (vb_on) { // drawn blended with no depth writes (the game's own fade does the same), so the player behind it stays visible @@ -4953,11 +4958,17 @@ void ge_draw_toast(const char* text, int ms, int y_px) { static float g_vb_target[3] = {0, 0, 0}; static float g_vb_eye[3] = {0, 0, 0}; // the game camera's eye (the draws of the main view have this eye; screen-space effects use another view and are left alone) void ge_draw_set_view_blocker(bool active, const float target[3], const float eye[3]) { - if (active) for (int i = 0; i < 3; i++) { g_vb_target[i] = target[i]; g_vb_eye[i] = eye[i]; } + for (int i = 0; i < 3; i++) g_vb_eye[i] = eye[i]; // always: a draw still fading back in must keep being matched to the main view + if (active) for (int i = 0; i < 3; i++) g_vb_target[i] = target[i]; g_vb_active.store(active, std::memory_order_relaxed); } -// Fade factor of a draw (1 = opaque), eased per draw so blocks fade in and out instead of popping. Keyed by the vertex data address and count. -static std::unordered_map g_vb_fade; +// Fade state of one drawn instance (1 = opaque), eased in time so blocks fade in and out instead of popping. Keyed by the vertex +// data address and count AND the instance's world position: one mesh drawn at several places (a row of pillars, repeated wall +// pieces) used to share one entry, so one hit pillar faded all of them, and the un-hit copies drawn after it pulled it back. +struct VbFade { float f = 1.f; double t = 0.0; }; +static std::unordered_map g_vb_fade; +static double g_vb_last_faded_t = 0.0; +static double vb_now() { return std::chrono::duration(std::chrono::steady_clock::now().time_since_epoch()).count(); } static bool vb_main_view(const GeState& st, float eye[3]) { const float* V = st.view_matrix; eye[0] = -(V[0] * V[9] + V[1] * V[10] + V[2] * V[11]); eye[1] = -(V[3] * V[9] + V[4] * V[10] + V[5] * V[11]); eye[2] = -(V[6] * V[9] + V[7] * V[10] + V[8] * V[11]); @@ -4968,9 +4979,21 @@ struct VbBox { float lo[3], hi[3]; }; static std::unordered_map g_vb_box; // model-space box of a draw's vertices, by vertex address and count (static geometry: decoded once) static float vb_fade_factor(uint8_t* rdram, const GeState& st, int n, int prim) { if (n < 3) return 1.f; + static const float k_alpha = [] { const char* e = std::getenv("PSPRECOMP_CAM_BLOCKER_ALPHA"); return e ? std::clamp((float)std::atof(e), 0.f, 1.f) : 0.3f; }(); + const bool want = g_vb_active.load(std::memory_order_relaxed); const float* m = st.world_matrix; static thread_local std::vector tmp; const uint64_t key = (uint64_t)st.vertex_addr << 20 ^ (uint64_t)n; + // the instance: its world position, quantised to 25 cm (static pieces keep their entry; moving ones just do not fade) + auto q = [](float v) { return (uint64_t)(uint32_t)(int32_t)std::lround(v * 4.f); }; + const uint64_t ikey = key * 0x9E3779B97F4A7C15ull ^ (q(m[9]) * 0xC2B2AE3D27D4EB4Full) ^ (q(m[10]) * 0x165667B19E3779F9ull) ^ (q(m[11]) * 0x27D4EB2F165667C5ull); + float eye[3]; + if (!vb_main_view(st, eye)) return 1.f; // not drawn from the game camera (screen-space effects, shadow maps, reflections, ...) + const double now = vb_now(); + auto fi = g_vb_fade.find(ikey); + if (!want && fi == g_vb_fade.end()) return 1.f; // lingering: only draws that are still faded need work + bool hit = false; + if (want) { auto bi = g_vb_box.find(key); if (bi == g_vb_box.end()) { VbBox bx{{1e30f, 1e30f, 1e30f}, {-1e30f, -1e30f, -1e30f}}; @@ -4987,28 +5010,28 @@ static float vb_fade_factor(uint8_t* rdram, const GeState& st, int n, int prim) const float w[3] = {m[0] * x + m[3] * y + m[6] * z + m[9], m[1] * x + m[4] * y + m[7] * z + m[10], m[2] * x + m[5] * y + m[8] * z + m[11]}; for (int k = 0; k < 3; ++k) { wlo[k] = std::min(wlo[k], w[k]); whi[k] = std::max(whi[k], w[k]); } } - float eye[3]; - if (!vb_main_view(st, eye)) return 1.f; // not drawn from the game camera (screen-space effects, shadow maps, ...) const float* T = g_vb_target; // the player's chest - bool hit = false; float hx = T[0] - eye[0], hz = T[2] - eye[2]; const float hl = std::sqrt(hx * hx + hz * hz); if (hl > 1e-3f) { hx /= hl; hz /= hl; } else { hx = 0; hz = 1; } - // Rays to the whole body (feet, knee, chest, head, both shoulders), each starting a little behind the eye so a wall the camera touches or sits in - // still counts: the fighter has to stay visible, not just his chest. - const float aim[6][3] = {{0, -0.9f, 0}, {0, -0.4f, 0}, {0, 0, 0}, {0, 0.8f, 0}, {-hz * 0.5f, 0.2f, hx * 0.5f}, {hz * 0.5f, 0.2f, -hx * 0.5f}}; + // Rays to the body (knee, chest, head, both shoulders), each starting a little behind the eye so a wall the camera touches + // still counts. Not to the feet: a ray grazing the floor at the fighter's feet faded floor pieces and kerbs. Floors and + // anything lower than the fighter's knees is never faded (the box top test), and the start is only 35 cm behind the eye + // (it was 2 m: a long wall the camera stood beside, out of the picture's way, faded as a whole). + const float aim[5][3] = {{0, -0.5f, 0}, {0, 0, 0}, {0, 0.75f, 0}, {-hz * 0.4f, 0.25f, hx * 0.4f}, {hz * 0.4f, 0.25f, -hx * 0.4f}}; bool decoded = false; - if (n <= 1500) { // (a block or a wall, not the whole stage: a single mesh of the floor or the sky would fade as one; Pandaemonium's walls are long slabs of a few triangles) - for (int ri = 0; ri < 6 && !hit; ++ri) { + const bool low = whi[1] < T[1] - 0.45f; // top below the knees (feet are ~1 m below the chest) + if (n <= 1500 && !low) { // (a block or a wall, not the whole stage: a single mesh of the floor or the sky would fade as one; Pandaemonium's walls are long slabs of a few triangles) + for (int ri = 0; ri < 5 && !hit; ++ri) { const float tg[3] = {T[0] + aim[ri][0], T[1] + aim[ri][1], T[2] + aim[ri][2]}; float dir[3] = {tg[0] - eye[0], tg[1] - eye[1], tg[2] - eye[2]}; const float dl = std::sqrt(dir[0] * dir[0] + dir[1] * dir[1] + dir[2] * dir[2]); if (dl < 1e-3f) continue; - const float back = 2.0f; // start behind the eye + const float back = 0.35f; // start behind the eye const float o[3] = {eye[0] - dir[0] / dl * back, eye[1] - dir[1] / dl * back, eye[2] - dir[2] / dl * back}; const float d[3] = {tg[0] - o[0], tg[1] - o[1], tg[2] - o[2]}; const float len = dl + back; - const float tmax = std::max(0.f, 1.f - 0.5f / len); // stop a body's width short of the player: only what he is touching does not fade (a wall a step in front of him must) + const float tmax = std::max(0.f, 1.f - 0.6f / len); // stop a body's width short of the player: only what he is touching does not fade (a wall a step in front of him must) float t0 = 0.f, t1 = tmax; for (int k = 0; k < 3 && t0 <= t1; ++k) { const bool thin = whi[k] - wlo[k] < 0.3f; // a wall is often a flat sheet: shrinking it would invert the box, so widen it a little instead @@ -5023,7 +5046,7 @@ static float vb_fade_factor(uint8_t* rdram, const GeState& st, int n, int prim) decoded = true; } const GeGpuVertex* v = tmp.data(); - auto W = [&](int i, float out[3]) { const float* q = v[i].pos; for (int r = 0; r < 3; ++r) out[r] = m[r] * q[0] + m[3 + r] * q[1] + m[6 + r] * q[2] + m[9 + r]; }; + auto W = [&](int i, float out[3]) { const float* qv = v[i].pos; for (int r = 0; r < 3; ++r) out[r] = m[r] * qv[0] + m[3 + r] * qv[1] + m[6 + r] * qv[2] + m[9 + r]; }; auto tri = [&](int ia, int ib, int ic) { float a[3], b[3], c[3]; W(ia, a); W(ib, b); W(ic, c); const float e1[3] = {b[0] - a[0], b[1] - a[1], b[2] - a[2]}, e2[3] = {c[0] - a[0], c[1] - a[1], c[2] - a[2]}; @@ -5033,10 +5056,10 @@ static float vb_fade_factor(uint8_t* rdram, const GeState& st, int n, int prim) const float inv = 1.f / det, tv[3] = {o[0] - a[0], o[1] - a[1], o[2] - a[2]}; const float u = (tv[0] * pv[0] + tv[1] * pv[1] + tv[2] * pv[2]) * inv; if (u < 0.f || u > 1.f) return false; - const float qv[3] = {tv[1] * e1[2] - tv[2] * e1[1], tv[2] * e1[0] - tv[0] * e1[2], tv[0] * e1[1] - tv[1] * e1[0]}; - const float w = (d[0] * qv[0] + d[1] * qv[1] + d[2] * qv[2]) * inv; + const float qq[3] = {tv[1] * e1[2] - tv[2] * e1[1], tv[2] * e1[0] - tv[0] * e1[2], tv[0] * e1[1] - tv[1] * e1[0]}; + const float w = (d[0] * qq[0] + d[1] * qq[1] + d[2] * qq[2]) * inv; if (w < 0.f || u + w > 1.f) return false; - const float t = (e2[0] * qv[0] + e2[1] * qv[1] + e2[2] * qv[2]) * inv; + const float t = (e2[0] * qq[0] + e2[1] * qq[1] + e2[2] * qq[2]) * inv; return t > 0.f && t < tmax; }; if (prim == GE_PRIM_TRIANGLES) { for (int i = 0; i + 2 < n && !hit; i += 3) hit = tri(i, i + 1, i + 2); } @@ -5044,12 +5067,148 @@ static float vb_fade_factor(uint8_t* rdram, const GeState& st, int n, int prim) else if (prim == GE_PRIM_TRIANGLE_FAN) { for (int i = 1; i + 1 < n && !hit; ++i) hit = tri(0, i, i + 1); } } } - float& f = g_vb_fade.try_emplace(key, 1.f).first->second; - const float target = hit ? 0.12f : 1.f; - f += (target - f) * (hit ? 0.5f : 0.2f); // fades out fast (the fighter must not stay hidden), back in gently - if (std::fabs(f - target) < 0.01f) f = target; + } + if (fi == g_vb_fade.end()) { + if (!hit) return 1.f; // only blocking instances get an entry + if (g_vb_fade.size() > 4096) g_vb_fade.clear(); // (moving pieces make new keys) + fi = g_vb_fade.emplace(ikey, VbFade{1.f, now}).first; + } + VbFade& e = fi->second; + const float dt = (float)std::clamp(now - e.t, 0.0, 0.1); // per frame, not per draw: an instance drawn twice must not ease twice as fast + e.t = now; + const float target = hit ? k_alpha : 1.f; + const float tc = hit ? 0.08f : 0.3f; // fades out fast (the fighter must not stay hidden), back in gently + e.f += (target - e.f) * (1.f - std::exp(-dt / tc)); + if (std::fabs(e.f - target) < 0.01f) e.f = target; + const float f = e.f; + if (f >= 1.f) { g_vb_fade.erase(fi); return 1.f; } + g_vb_last_faded_t = now; return f; } +// ---- Camera collision (ge_draw_camera_probe_*) ---- +// The stage geometry the main view draws, in world space, one entry per drawn instance (same key as the fade). Static pieces +// are decoded once; an entry not drawn for 30 s is dropped, and everything is dropped when the total gets large (a new stage). +// The game's own camera pulls in for walls behind ITS eye; a turned or held view needs its own test, or it ends up outside the +// room looking at the back of a wall. +struct CcMesh { float lo[3], hi[3]; std::vector tri; double seen; int frames = 1; }; // frames: how many frames it was drawn in at this spot +static std::unordered_map g_cc; +static size_t g_cc_tris = 0; +static double g_cc_last_on = 0; +static std::mutex g_cc_mu; +static float g_cc_from[3], g_cc_to[3], g_cc_result = 1.f; +static uint32_t g_cc_req_id = 0, g_cc_res_id = 0; +static bool g_cc_pending = false; +void ge_draw_camera_probe_request(const float from[3], const float to[3], uint32_t id) { + std::lock_guard lk(g_cc_mu); + for (int i = 0; i < 3; i++) { g_cc_from[i] = from[i]; g_cc_to[i] = to[i]; } + g_cc_req_id = id; g_cc_pending = true; + g_cc_collect.store(true, std::memory_order_relaxed); + g_cc_last_on = vb_now(); +} +float ge_draw_camera_probe_result(uint32_t* id) { + std::lock_guard lk(g_cc_mu); + if (id) *id = g_cc_res_id; + return g_cc_result; +} +static void cc_collect(uint8_t* rdram, const GeState& st, int n, int prim) { + if (n < 3 || n > 12000) return; + if (prim != GE_PRIM_TRIANGLES && prim != GE_PRIM_TRIANGLE_STRIP && prim != GE_PRIM_TRIANGLE_FAN) return; + float eye[3]; + if (!vb_main_view(st, eye)) return; + const float* m = st.world_matrix; + auto q = [](float v) { return (uint64_t)(uint32_t)(int32_t)std::lround(v * 4.f); }; + const uint64_t key = (uint64_t)st.vertex_addr << 20 ^ (uint64_t)n; + const uint64_t ikey = key * 0x9E3779B97F4A7C15ull ^ (q(m[9]) * 0xC2B2AE3D27D4EB4Full) ^ (q(m[10]) * 0x165667B19E3779F9ull) ^ (q(m[11]) * 0x27D4EB2F165667C5ull); + const double now = vb_now(); + auto it = g_cc.find(ikey); + if (it != g_cc.end()) { if (now - it->second.seen > 0.005) it->second.frames++; it->second.seen = now; return; } + if (g_cc_tris > 400000) { g_cc.clear(); g_cc_tris = 0; } + static thread_local std::vector tmp; + tmp.resize((size_t)n); + if (!ge_decode_vertices_gpu(rdram, st, n, tmp.data())) return; + CcMesh cm{{1e30f, 1e30f, 1e30f}, {-1e30f, -1e30f, -1e30f}, {}, now}; + auto add = [&](int ia, int ib, int ic) { + for (int j : {ia, ib, ic}) { + const float* p = tmp[j].pos; + for (int r = 0; r < 3; ++r) { + const float w = m[r] * p[0] + m[3 + r] * p[1] + m[6 + r] * p[2] + m[9 + r]; + cm.tri.push_back(w); cm.lo[r] = std::min(cm.lo[r], w); cm.hi[r] = std::max(cm.hi[r], w); + } + } + }; + if (prim == GE_PRIM_TRIANGLES) for (int i = 0; i + 2 < n; i += 3) add(i, i + 1, i + 2); + else if (prim == GE_PRIM_TRIANGLE_STRIP) for (int i = 0; i + 2 < n; ++i) add(i, i + 1, i + 2); + else for (int i = 1; i + 1 < n; ++i) add(0, i, i + 1); + if (!std::isfinite(cm.lo[0]) || cm.lo[0] > cm.hi[0]) return; + g_cc_tris += cm.tri.size() / 9; + g_cc.emplace(ikey, std::move(cm)); +} +// First hit of the segment o -> o + d (t in 0..1) against the cached geometry; 1 when clear. +static float cc_trace(const float o[3], const float d[3]) { + float best = 1.f; + const double now = vb_now(); + for (const auto& kv : g_cc) { + const CcMesh& cm = kv.second; + if (cm.frames < 30 && now - cm.seen > 0.25) continue; // something that moved (a prop, a weapon): only where it is now + float t0 = 0.f, t1 = best; + for (int k = 0; k < 3 && t0 <= t1; ++k) { + const float a2 = cm.lo[k] - 0.01f, b2 = cm.hi[k] + 0.01f; + if (std::fabs(d[k]) < 1e-7f) { if (o[k] < a2 || o[k] > b2) t0 = 2.f; } + else { float u0 = (a2 - o[k]) / d[k], u1 = (b2 - o[k]) / d[k]; if (u0 > u1) std::swap(u0, u1); t0 = std::max(t0, u0); t1 = std::min(t1, u1); } + } + if (t0 > t1) continue; + const float* v = cm.tri.data(); + for (size_t i = 0; i + 8 < cm.tri.size(); i += 9) { + const float* a = v + i; const float* b = v + i + 3; const float* c = v + i + 6; + const float e1[3] = {b[0] - a[0], b[1] - a[1], b[2] - a[2]}, e2[3] = {c[0] - a[0], c[1] - a[1], c[2] - a[2]}; + const float pv[3] = {d[1] * e2[2] - d[2] * e2[1], d[2] * e2[0] - d[0] * e2[2], d[0] * e2[1] - d[1] * e2[0]}; + const float det = e1[0] * pv[0] + e1[1] * pv[1] + e1[2] * pv[2]; + if (std::fabs(det) < 1e-9f) continue; + const float inv = 1.f / det, tv[3] = {o[0] - a[0], o[1] - a[1], o[2] - a[2]}; + const float u = (tv[0] * pv[0] + tv[1] * pv[1] + tv[2] * pv[2]) * inv; + if (u < 0.f || u > 1.f) continue; + const float qq[3] = {tv[1] * e1[2] - tv[2] * e1[1], tv[2] * e1[0] - tv[0] * e1[2], tv[0] * e1[1] - tv[1] * e1[0]}; + const float w = (d[0] * qq[0] + d[1] * qq[1] + d[2] * qq[2]) * inv; + if (w < 0.f || u + w > 1.f) continue; + const float t = (e2[0] * qq[0] + e2[1] * qq[1] + e2[2] * qq[2]) * inv; + if (t > 0.f && t < best) best = t; + } + } + return best; +} +static void cc_frame_end() { + const double now = vb_now(); + if (g_cc_collect.load(std::memory_order_relaxed) && now - g_cc_last_on > 2.0) { // no battle camera any more (menus): drop it all + g_cc_collect.store(false, std::memory_order_relaxed); + g_cc.clear(); g_cc_tris = 0; + return; + } + static double last_sweep = 0; + if (now - last_sweep > 5.0) { + last_sweep = now; + for (auto it = g_cc.begin(); it != g_cc.end();) { + if (now - it->second.seen > 30.0) { g_cc_tris -= it->second.tri.size() / 9; it = g_cc.erase(it); } else ++it; + } + } + float o[3], d[3]; uint32_t id; + { + std::lock_guard lk(g_cc_mu); + if (!g_cc_pending) return; + g_cc_pending = false; + for (int i = 0; i < 3; i++) { o[i] = g_cc_from[i]; d[i] = g_cc_to[i] - g_cc_from[i]; } + id = g_cc_req_id; + } + const float r = cc_trace(o, d); + std::lock_guard lk(g_cc_mu); + g_cc_result = r; g_cc_res_id = id; +} + +// Called once per frame (present): keep evaluating draws after the custom camera is back on the game's while some are still faded. +static void vb_frame_end() { + cc_frame_end(); + g_vb_linger.store(!g_vb_fade.empty() && vb_now() - g_vb_last_faded_t < 1.0, std::memory_order_relaxed); + if (!g_vb_linger.load(std::memory_order_relaxed) && !g_vb_active.load(std::memory_order_relaxed)) g_vb_fade.clear(); +} // Flash: four bars along the edges of the picture, cleared to one colour through the scissor box (no shader needed), for a short time. @@ -5454,6 +5613,7 @@ static void present_blit(GLuint fbo, const char* why = "flip") { glBlitFramebuffer(0, 0, spw * srs, h272_of_fbo(fbo) * srs, pr.x0, pr.y0, pr.x1, pr.y1, GL_COLOR_BUFFER_BIT, pf); perf_note_present(); + vb_frame_end(); if (!std::strcmp(why, "flip")) perf_note_flip(); if (psp_post_final_active()) { // copy of the scene for the final pass (grade / HDR), taken before the HUD and overlays int sdw = 0, sdh = 0;