From 8f1bc7530aa0a60ad0a556703b3f47110cee9639 Mon Sep 17 00:00:00 2001 From: Matthew Stanley <1379tech@gmail.com> Date: Sun, 15 Nov 2026 03:25:32 -0800 Subject: [PATCH 1/6] feat: allow hosts to decline unobserved video per grant --- CMakeLists.txt | 8 +++++++ corelink/core_link.cpp | 9 ++++++- corelink/core_link.hpp | 3 +++ corelink/link_protocol.hpp | 9 +++++-- corelink/link_test_main.cpp | 8 +++++-- docs/CORE_ABI.md | 1 + docs/CORE_LINK.md | 9 +++++++ include/rcore/rcore.h | 12 +++++++++- runner/host_session.cpp | 19 ++++++++++++++- runner/host_session.hpp | 6 +++++ runner/runner_link.cpp | 5 ++++ tests/gpu_host_test.cpp | 25 +++++++++++++++++--- tests/presentation_protocol_test.cpp | 8 +++++++ tests/rcore_fake_core.c | 7 ++++++ tests/video_demand_test.cmake | 35 ++++++++++++++++++++++++++++ 15 files changed, 154 insertions(+), 10 deletions(-) create mode 100644 tests/video_demand_test.cmake diff --git a/CMakeLists.txt b/CMakeLists.txt index c257b09..fcb7d4f 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -277,6 +277,14 @@ if(RETRO_RUNTIME_TOOLS) add_executable(retro-presentation-protocol-test tests/presentation_protocol_test.cpp) target_link_libraries(retro-presentation-protocol-test PRIVATE retro_corelink) add_test(NAME presentation_protocol COMMAND retro-presentation-protocol-test) + add_test(NAME link_video_demand + COMMAND ${CMAKE_COMMAND} + -DLINK_TEST=$ + -DRUNNER=$ + -DCORE=$ + -DROM=${CMAKE_CURRENT_SOURCE_DIR}/LICENSE + -DOUT=${CMAKE_CURRENT_BINARY_DIR}/test-video-demand + -P ${CMAKE_CURRENT_SOURCE_DIR}/tests/video_demand_test.cmake) # Link 2.2: FrameDone carries the game's count and the runner's timing. add_test(NAME link_frame_stats COMMAND retro-core-link-test --runner $ diff --git a/corelink/core_link.cpp b/corelink/core_link.cpp index 47163a9..b1d6abd 100644 --- a/corelink/core_link.cpp +++ b/corelink/core_link.cpp @@ -293,6 +293,11 @@ void CoreLink::handle_packet(const std::vector& buf, } bool CoreLink::grant(const rcore_pad pads[RCORE_MAX_SEATS], std::uint32_t width, std::uint32_t height) { + return grant(pads,width,height,true); +} + +bool CoreLink::grant(const rcore_pad pads[RCORE_MAX_SEATS], std::uint32_t width, std::uint32_t height, + bool video_requested) { if (!can_grant()) return false; GrantMsg g{}; g.h.type = Msg::Grant; @@ -302,7 +307,9 @@ bool CoreLink::grant(const rcore_pad pads[RCORE_MAX_SEATS], std::uint32_t width, g.presentation_width = width; g.presentation_height = height; } - const auto bytes = link_has_presentation_size(identity_.protocol_minor) ? sizeof(g) : kGrantSize22; + if (!video_requested) g.flags |= kGrantSkipVideo; + const auto bytes = link_has_video_demand(identity_.protocol_minor) ? sizeof(g) + : link_has_presentation_size(identity_.protocol_minor) ? kGrantSize25 : kGrantSize22; if (!send_msg_sized(channel_, g, bytes)) return false; ++granted_; ++outstanding_; diff --git a/corelink/core_link.hpp b/corelink/core_link.hpp index 74121dd..f90b091 100644 --- a/corelink/core_link.hpp +++ b/corelink/core_link.hpp @@ -160,6 +160,9 @@ class CoreLink { // Available physical pixels before letterboxing; zeros when unknown. bool grant(const rcore_pad pads[RCORE_MAX_SEATS], std::uint32_t width = 0, std::uint32_t height = 0); + // Presentation-only permission; old runners/cores may still send video. + bool grant(const rcore_pad pads[RCORE_MAX_SEATS], std::uint32_t width, + std::uint32_t height, bool video_requested); std::uint64_t frames_granted() const { return granted_; } std::uint64_t frames_done() const { return done_; } const FrameStats& frame_stats() const { return stats_; } diff --git a/corelink/link_protocol.hpp b/corelink/link_protocol.hpp index 01c9b86..e4dcbb7 100644 --- a/corelink/link_protocol.hpp +++ b/corelink/link_protocol.hpp @@ -46,7 +46,9 @@ constexpr std::uint32_t kProtocolMajor = 2; // 2.3: Grant appends the available drawable pixel area (rcore rev 9). // 2.4: optional core-owned GPU rings, native handle transfer and release events. // 2.5: between-frame replay recording controls and authoritative status. -constexpr std::uint32_t kProtocolMinor = 5; +// 2.6: Grant appends presentation-only permission to omit unobserved video. +constexpr std::uint32_t kProtocolMinor = 6; +constexpr bool link_has_video_demand(std::uint32_t peer_minor) { return peer_minor >= 6; } constexpr bool link_has_replays(std::uint32_t peer_minor) { return peer_minor >= 5; } constexpr bool link_has_gpu_frames(std::uint32_t peer_minor) { return kProtocolMajor > 2 || peer_minor >= 4; @@ -299,8 +301,11 @@ struct GrantMsg { rcore_pad pads[RCORE_MAX_SEATS]; // 2.3: before letterboxing; both zero means unavailable. std::uint32_t presentation_width, presentation_height; + std::uint32_t flags, _pad26; }; constexpr std::size_t kGrantSize22 = 240; +constexpr std::size_t kGrantSize25 = 248; +constexpr std::uint32_t kGrantSkipVideo = 1; struct EmptyMsg { MsgHeader h; @@ -432,7 +437,7 @@ static_assert(offsetof(ExitingMsg, reason) == 12, "ExitingMsg::reason"); static_assert(sizeof(SavesFilledMsg) == 232 && alignof(SavesFilledMsg) == 4, "SavesFilledMsg"); static_assert(offsetof(SavesFilledMsg, h) == 0, "SavesFilledMsg::h"); static_assert(offsetof(SavesFilledMsg, pads) == 8, "SavesFilledMsg::pads"); -static_assert(sizeof(GrantMsg) == 248 && alignof(GrantMsg) == 8, "GrantMsg"); +static_assert(sizeof(GrantMsg) == 256 && alignof(GrantMsg) == 8, "GrantMsg"); static_assert(offsetof(GrantMsg, h) == 0, "GrantMsg::h"); static_assert(offsetof(GrantMsg, frame_number) == 8, "GrantMsg::frame_number"); static_assert(offsetof(GrantMsg, pads) == 16, "GrantMsg::pads"); diff --git a/corelink/link_test_main.cpp b/corelink/link_test_main.cpp index 091fc75..09e153a 100644 --- a/corelink/link_test_main.cpp +++ b/corelink/link_test_main.cpp @@ -86,6 +86,7 @@ int main(int argc, char** argv) { LaunchSpec spec; spec.gl = false; std::uint64_t frames = 60; + std::uint64_t video_every = 1, pictures = 0; bool seat0 = true; bool presentation_test = false; bool expect_route_end = false; @@ -134,6 +135,7 @@ int main(int argc, char** argv) { else if (a == "--title-dir") spec.title_dir = utf8_path(val()); else if (a == "--out") spec.session_dir = utf8_path(val()); else if (a == "--frames") frames = std::strtoull(val().c_str(), nullptr, 10); + else if (a == "--video-every") video_every = std::strtoull(val().c_str(), nullptr, 10); else if (a == "--load-state") spec.load_state = utf8_path(val()); else if (a.size() >= 11 && a.compare(0, 6, "--tpak") == 0 && a[6] >= '1' && a[6] <= '4') { const std::size_t seat = static_cast(a[6] - '1'); @@ -277,7 +279,8 @@ int main(int argc, char** argv) { // Fixture exercises resize, unavailable/minimized, then recovery. const std::uint32_t pw = presentation_test && k != 3 ? (k == 1 ? 1280 : 2560) : 0; const std::uint32_t ph = presentation_test && k != 3 ? (k == 1 ? 720 : 1080) : 0; - if (!link.grant(pads, pw, ph)) { + const bool video_requested = k==frames || (video_every && k%video_every==0); + if (!link.grant(pads, pw, ph, video_requested)) { if (expect_route_end) { link.pump(100); if (link.state()==LinkState::Ended && !link.exit_code()) break; @@ -288,7 +291,7 @@ int main(int argc, char** argv) { print_notifies(); const bool clean_route_end = expect_route_end && link.state()==LinkState::Ended && !link.exit_code(); if (link.state() != LinkState::Ready && !clean_route_end) ok = false; - link.take_frame(); + if (link.take_frame()) ++pictures; std::int16_t sink[4096]; while (link.drain_audio(sink, 2048)) { } @@ -366,6 +369,7 @@ int main(int argc, char** argv) { std::printf("picture: first byte %u\n", unsigned(link.frame_pixels()[0])); } // 2.2: what the overlay's readout is made of. + std::printf("pictures received: %llu\n",static_cast(pictures)); std::printf("frame stats: game %llu work %s\n", static_cast(link.frame_stats().game_frame), link.frame_stats().work_ns ? "measured" : "none"); diff --git a/docs/CORE_ABI.md b/docs/CORE_ABI.md index 29c5488..cd174cb 100644 --- a/docs/CORE_ABI.md +++ b/docs/CORE_ABI.md @@ -12,6 +12,7 @@ only the shape of the contract and why. | Rev | Date | Change | |---|---|---| | 1 | 2026-09-23 | First draft. | +| 12 | 2026-11-15 | Optional appended host `video_requested(ctx, field, game_frame)` permits omitting video on an unobserved field. An absent callback requests every picture. Simulation, audio, input, device completion and correctness observers still run; internal display history may require a scanout despite permission to omit. Presentation state only, excluded from saves and netplay identity. | | 11 | 2026-10-19 | Optional `RCORE_CAP_GPU_FRAMES` and five appended host callbacks for a fixed actual-device offer, image-ring registration, resource status, shared-frame submission and retirement. CPU RGBA8 remains the fallback; no generated game ABI change. See [GPU_FRAMES.md](GPU_FRAMES.md). | | 10 | 2026-10-14 | Optional pre-init `execution_profiles()` capability/qualification query; appended init `execution_profile` index, valid with `RCORE_INIT_PROFILE_AGREED`. The runner exchanges offers/ACKs before init/guest boot, folds the common profile and canonical capability transcript into existing netplay identity, and keeps selection fixed through replay/reset/rollback. Old runners retain the core's conservative startup default. | | 9 | 2026-10-14 | Optional host `presentation_size(ctx, width, height)`: the available drawable physical pixel area before letterboxing, stable for a granted frame. Returns 0 and zeros when unavailable. Core checks host `struct_size` before reading the appended callback. Presentation policy only; never guest input or serialized simulation state. | diff --git a/docs/CORE_LINK.md b/docs/CORE_LINK.md index e1cd1f7..18922cd 100644 --- a/docs/CORE_LINK.md +++ b/docs/CORE_LINK.md @@ -13,6 +13,15 @@ protocol is the same on all three. ## Transport +**2.6 (2026-11-15):** `Grant` appends flags and padding (256 bytes). +`kGrantSkipVideo` permits omitting CPU/GPU video for that field; zero flags +retain every picture. The runner exposes rcore revision 12 `video_requested` +and retains field/game-picture progress even without pixels. Input, audio, +simulation and `FrameDone` still occur on every grant. Recording and replay +routes request every picture. Peers speaking 2.3–2.5 receive the unchanged +248-byte grant; peers through 2.2 receive 240 bytes. Older cores may ignore +the hint. This is local presentation policy, not serialized or netplay input. + **2.4 (2026-10-19):** optional GPU ring/frame/retirement messages 10–12, startup capabilities 70, ownership status 71 and explicit save thumbnail 72. The existing shared frame-region layout is unchanged. These messages require diff --git a/include/rcore/rcore.h b/include/rcore/rcore.h index e576db1..8237275 100644 --- a/include/rcore/rcore.h +++ b/include/rcore/rcore.h @@ -48,7 +48,7 @@ extern "C" { #define RCORE_ABI_MAJOR 0u /* 0 = draft; the first implemented contract is 1 */ #define RCORE_ABI_MINOR 0u -#define RCORE_DRAFT_REVISION 11u /* draft-only counter; see docs/CORE_ABI.md */ +#define RCORE_DRAFT_REVISION 12u /* draft-only counter; see docs/CORE_ABI.md */ #if defined(_WIN32) # define RCORE_EXPORT __declspec(dllexport) @@ -516,6 +516,16 @@ typedef struct rcore_host_api { * the replacement ring. The core keeps memory alive until retirement, * except unload: the host's independent handle references outlive it. */ void (*gpu_frames_unregister)(void* host_ctx, uint64_t generation); + /* --- appended in draft revision 12: optional video demand --- + * Called on the core thread with current field/picture progress. Return + * zero to permit omitting this field's CPU/GPU video output, nonzero to + * request it. Absent means every picture is requested. This is permission, + * not an instruction to skip emulation: input, audio, interrupts, device + * progress and state observers must retain their normal behavior. Internal + * video observers may still require a picture. Stable for the field and + * presentation-only; never guest input, compatibility identity or saved + * simulation state. The host must accept legacy cores that ignore it. */ + uint32_t (*video_requested)(void* host_ctx, uint64_t frame_number, uint64_t game_frame); } rcore_host_api; /* ------------------------------------------------------------------------ */ diff --git a/runner/host_session.cpp b/runner/host_session.cpp index fb26407..bf8d911 100644 --- a/runner/host_session.cpp +++ b/runner/host_session.cpp @@ -17,6 +17,7 @@ HostSession::HostSession(const LoadedCore& core, Sink& sink) : core_(core), sink host_.log = h_log; host_.report = h_report; host_.video_submit = h_video; + host_.video_requested = h_video_requested; host_.audio_push = h_audio; host_.set_audio_rate = h_rate; host_.input_get = h_input; @@ -102,6 +103,10 @@ std::uint32_t HostSession::h_gpu_released(void* ctx, std::uint64_t generation, std::uint32_t HostSession::h_gpu_frame(void* ctx, const rcore_gpu_frame* frame) { if (!frame || frame->struct_size < sizeof *frame || !frame->generation || frame->image_index >= RCORE_GPU_MAX_IMAGES) return 0; + self(ctx)->sink_.video_progress(frame->frame_number, frame->game_frame); + // Legacy cores may ignore demand. Complete the existing GPU ownership + // protocol if they publish: a ready binary semaphore cannot be discarded + // and reused as though a consumer had waited for it. if (self(ctx)->route_) self(ctx)->route_->gpu_video(); return self(ctx)->sink_.gpu_frame(*frame) ? 1u : 0u; } @@ -243,7 +248,19 @@ void HostSession::h_report(void* ctx, const rcore_event* e) { void HostSession::h_video(void* ctx, const rcore_frame* f) { if (f && self(ctx)->route_) self(ctx)->route_->video(*f); - if (f) self(ctx)->sink_.frame(*f); + if (f) { + auto* s = self(ctx); + s->sink_.video_progress(f->frame_number, + f->struct_size >= offsetof(rcore_frame, game_frame) + sizeof f->game_frame ? f->game_frame : 0); + if (s->video_requested_ || s->route_) s->sink_.frame(*f); + } +} + +std::uint32_t HostSession::h_video_requested(void* ctx, std::uint64_t frame, std::uint64_t picture) { + auto* s = self(ctx); + s->sink_.video_progress(frame, picture); + // Routes/recordings observe per-field pictures; never suppress those. + return s->video_requested_ || s->route_ != nullptr; } void HostSession::h_audio(void* ctx, const std::int16_t* s, std::uint32_t n) { diff --git a/runner/host_session.hpp b/runner/host_session.hpp index e926daf..466e3d1 100644 --- a/runner/host_session.hpp +++ b/runner/host_session.hpp @@ -27,6 +27,9 @@ class Sink { virtual void log(std::uint32_t level, const char* msg) = 0; virtual void event(const rcore_event& e) = 0; virtual void frame(const rcore_frame& f) = 0; + // Current progress, including fields whose pixels are not requested. + // A core may repeat the same metadata; this is a value, not a counter tick. + virtual void video_progress(std::uint64_t, std::uint64_t) {} virtual void audio(const std::int16_t* samples, std::uint32_t frames) = 0; virtual void audio_rate(std::uint32_t hz) = 0; // The state of `seat` for the frame being run. Must be identical for every @@ -86,6 +89,7 @@ class HostSession { // The host table the core is given. Stable for the session's lifetime. const rcore_host_api* host_api() const { return &host_; } + void set_video_requested(bool requested) { video_requested_ = requested; } void set_presentation_size(std::uint32_t width, std::uint32_t height) { presentation_width_ = width && height ? width : 0; presentation_height_ = width && height ? height : 0; @@ -151,6 +155,7 @@ class HostSession { static void h_log(void*, std::uint32_t, const char*); static void h_report(void*, const rcore_event*); static void h_video(void*, const rcore_frame*); + static std::uint32_t h_video_requested(void*, std::uint64_t, std::uint64_t); static void h_audio(void*, const std::int16_t*, std::uint32_t); static void h_rate(void*, std::uint32_t); static void h_input(void*, std::uint32_t, rcore_pad*); @@ -177,6 +182,7 @@ class HostSession { Route* route_ = nullptr; Sink& sink_; rcore_host_api host_{}; + bool video_requested_ = true; std::uint32_t presentation_width_ = 0, presentation_height_ = 0; std::map> options_; std::vector regions_; diff --git a/runner/runner_link.cpp b/runner/runner_link.cpp index efda31d..62b3306 100644 --- a/runner/runner_link.cpp +++ b/runner/runner_link.cpp @@ -106,6 +106,9 @@ class LinkSink final : public Sink { for (auto& item : gpu_rings_) close_gpu_handles(item.second); } void set_gpu_capabilities(const rcore_gpu_capabilities& caps) { gpu_caps_ = caps; } + void video_progress(std::uint64_t, std::uint64_t game_frame) override { + last_game_frame = game_frame; + } bool gpu_capabilities(rcore_gpu_capabilities& out) override { out = gpu_caps_; return out.handle_types && out.sync_types; @@ -745,6 +748,8 @@ int run_link_mode(const LoadedCore& core, const CoreManifest& manifest, const Li } } sink.frame_number = g.frame_number; + session.set_video_requested(!link_has_video_demand(session_minor) || buf.size() < sizeof g + || !(g.flags & kGrantSkipVideo)); session.set_presentation_size(g.presentation_width, g.presentation_height); // Everything that arrived before this grant is the frame's accessory // sequence; a message that arrives while the frame runs waits for the diff --git a/tests/gpu_host_test.cpp b/tests/gpu_host_test.cpp index 3a52af2..7f21d17 100644 --- a/tests/gpu_host_test.cpp +++ b/tests/gpu_host_test.cpp @@ -3,19 +3,24 @@ #include using namespace retro::runner; +static_assert(sizeof(void*)!=8 || sizeof(rcore_host_api)==208); +static_assert(sizeof(void*)!=8 || offsetof(rcore_host_api,video_requested)==200); void check(bool ok, const char* what) { if (!ok) { std::cerr << "FAIL " << what << '\n'; std::exit(1); } } struct TestSink : Sink { bool available = false; unsigned samples = 0, registrations = 0, pictures = 0, retires = 0; + unsigned cpu_pictures = 0, audio_frames = 0, input_polls = 0; + std::uint64_t field = 0, game_frame = 0; rcore_gpu_capabilities caps{}; void log(std::uint32_t, const char*) override {} void event(const rcore_event&) override {} - void frame(const rcore_frame&) override {} - void audio(const std::int16_t*, std::uint32_t) override {} + void frame(const rcore_frame&) override { ++cpu_pictures; } + void video_progress(std::uint64_t f, std::uint64_t g) override { field=f;game_frame=g; } + void audio(const std::int16_t*, std::uint32_t n) override { audio_frames+=n; } void audio_rate(std::uint32_t) override {} - void input(std::uint32_t, rcore_pad&) override {} + void input(std::uint32_t, rcore_pad&) override { ++input_polls; } bool gpu_capabilities(rcore_gpu_capabilities& out) override { ++samples; out = caps; return available; } @@ -106,5 +111,19 @@ int main() { check(!h->video_submit_gpu(h->host_ctx, &frame) && sink.pictures == 1, "invalid slot refused"); h->gpu_frames_unregister(h->host_ctx, 0); h->gpu_frames_unregister(h->host_ctx, 42); check(sink.retires == 1, "one retirement forwarded"); + check(h->video_requested && h->video_requested(h->host_ctx, 41, 17), "legacy demand defaults to every picture"); + gpu.set_video_requested(false); + check(!h->video_requested(h->host_ctx, 42, 18) && sink.field==42 && sink.game_frame==18, + "omitted image retains field/picture progress"); + frame.image_index=0; + check(h->video_submit_gpu(h->host_ctx,&frame) && sink.pictures==2, "legacy GPU publication retains ownership protocol despite optional demand"); + rcore_frame cpu_frame{};cpu_frame.struct_size=sizeof cpu_frame;cpu_frame.frame_number=42;cpu_frame.game_frame=18; + h->video_submit(h->host_ctx,&cpu_frame); + check(sink.cpu_pictures==0, "legacy core CPU pixels may be discarded"); + std::int16_t pcm[4]{};h->audio_push(h->host_ctx,pcm,2); + rcore_pad pad{};pad.struct_size=sizeof pad;h->input_get(h->host_ctx,0,&pad); + check(sink.audio_frames==2 && sink.input_polls==1, "video demand never suppresses audio or input"); + gpu.set_video_requested(true);h->video_submit(h->host_ctx,&cpu_frame); + check(sink.cpu_pictures==1 && h->video_requested(h->host_ctx,43,19), "video can resume at next field"); std::cout << "GPU host contract: fixed offer, adapter and geometry refusal, CPU fallback PASS\n"; } diff --git a/tests/presentation_protocol_test.cpp b/tests/presentation_protocol_test.cpp index a0cab55..3732ca6 100644 --- a/tests/presentation_protocol_test.cpp +++ b/tests/presentation_protocol_test.cpp @@ -15,5 +15,13 @@ int main() { got.presentation_width || got.presentation_height) return 3; bytes.resize(kGrantSize22 - 1); if (as_msg(bytes, got, kGrantSize22)) return 4; + if (link_has_video_demand(5) || !link_has_video_demand(6)) return 5; + sent.flags = kGrantSkipVideo; + bytes.resize(sizeof sent); std::memcpy(bytes.data(), &sent, sizeof sent); + if (!as_msg(bytes, got, kGrantSize22) || !(got.flags & kGrantSkipVideo)) return 6; + bytes.resize(kGrantSize25); + if (!as_msg(bytes, got, kGrantSize22) || got.flags || got.presentation_width != 2560) return 7; + bytes.resize(kGrantSize22); + if (!as_msg(bytes, got, kGrantSize22) || got.flags || got.presentation_width) return 8; return 0; } diff --git a/tests/rcore_fake_core.c b/tests/rcore_fake_core.c index 85268fd..e697f53 100644 --- a/tests/rcore_fake_core.c +++ b/tests/rcore_fake_core.c @@ -54,6 +54,9 @@ #include #include +_Static_assert(sizeof(void*)!=8 || sizeof(rcore_host_api)==208,"rev12 host size"); +_Static_assert(sizeof(void*)!=8 || offsetof(rcore_host_api,video_requested)==200,"rev11 prefix unchanged"); + #if defined(_WIN32) # include #else @@ -298,6 +301,10 @@ static rcore_result run_frame(void) { #endif if (!g_battery) g_battery = (unsigned char*)g_host->save_memory(g_host->host_ctx, "battery"); if (g_battery) g_battery[g_frames % 64] = (unsigned char)(g_frames & 0xff); + if (g_host->struct_size >= offsetof(rcore_host_api,video_requested)+sizeof(g_host->video_requested) && + g_host->video_requested && !g_host->video_requested(g_host->host_ctx,g_frames,g_frames/2)) { + return RCORE_OK; + } memset(g_pixels, (int)(g_frames & 0xff), sizeof g_pixels); rcore_frame f; memset(&f, 0, sizeof f); diff --git a/tests/video_demand_test.cmake b/tests/video_demand_test.cmake new file mode 100644 index 0000000..da05b63 --- /dev/null +++ b/tests/video_demand_test.cmake @@ -0,0 +1,35 @@ +execute_process(COMMAND "${LINK_TEST}" --runner "${RUNNER}" --core "${CORE}" + --rom "${ROM}" --out "${OUT}" --frames 10 --video-every 3 + RESULT_VARIABLE rc OUTPUT_VARIABLE output ERROR_VARIABLE errors) +if(NOT rc EQUAL 0) + message(FATAL_ERROR "video-demand link run failed: ${rc}\n${output}\n${errors}") +endif() +foreach(expected "10 frame(s) granted and done, runner exit 0, 0 fault(s)" + "pictures received: 4" "picture: first byte 10" "frame stats: game 5 work measured") + string(FIND "${output}" "${expected}" pos) + if(pos EQUAL -1) + message(FATAL_ERROR "missing result: ${expected}\n${output}") + endif() +endforeach() +file(READ "${OUT}/core.log" log) +string(FIND "${log}" "FAKE_DONE frames=10" pos) +if(pos EQUAL -1) + message(FATAL_ERROR "video demand lost simulation work: ${log}") +endif() + +# Recording observes every field even when its host's grant declines pixels. +file(REMOVE "${OUT}/recording.route" "${OUT}/recording.route.partial") +execute_process(COMMAND "${LINK_TEST}" --runner "${RUNNER}" --core "${CORE}" + --rom "${ROM}" --out "${OUT}/record" --frames 10 --video-every 0 + --record-start-at "1:${OUT}/recording.route" --record-stop-at 9 + RESULT_VARIABLE rc OUTPUT_VARIABLE output ERROR_VARIABLE errors) +if(NOT rc EQUAL 0) + message(FATAL_ERROR "recording demand run failed: ${rc}\n${output}\n${errors}") +endif() +foreach(expected "pictures received: 9" "stop at 9 state 2 fields 8" + "10 frame(s) granted and done, runner exit 0, 0 fault(s)") + string(FIND "${output}" "${expected}" pos) + if(pos EQUAL -1) + message(FATAL_ERROR "recording did not override optional demand: ${expected}\n${output}") + endif() +endforeach() From 7c5ebc4be3a3c8967da16acc17134afcb02acffe Mon Sep 17 00:00:00 2001 From: Matthew Stanley <1379tech@gmail.com> Date: Sun, 15 Nov 2026 03:38:20 -0800 Subject: [PATCH 2/6] test: qualify video demand with complete dual-stick audio routes --- corelink/link_test_main.cpp | 23 +++++++++++++++++++++-- 1 file changed, 21 insertions(+), 2 deletions(-) diff --git a/corelink/link_test_main.cpp b/corelink/link_test_main.cpp index 09e153a..698c6b5 100644 --- a/corelink/link_test_main.cpp +++ b/corelink/link_test_main.cpp @@ -41,7 +41,9 @@ using namespace retro::corelink; namespace { constexpr std::uint32_t kLeft = 1u << 20, kRight = 1u << 21, kUp = 1u << 22, kDown = 1u << 23; -constexpr std::uint32_t kStick = kLeft | kRight | kUp | kDown; +constexpr std::uint32_t kRLeft = 1u << 24, kRRight = 1u << 25, + kRUp = 1u << 26, kRDown = 1u << 27; +constexpr std::uint32_t kStick = kLeft | kRight | kUp | kDown | kRLeft | kRRight | kRUp | kRDown; [[noreturn]] void die(const std::string& m) { std::fprintf(stderr, "retro-core-link-test: %s\n", m.c_str()); @@ -70,6 +72,10 @@ std::uint32_t script_buttons(const std::string& s) { else if (t == "right") b |= kRight; else if (t == "up") b |= kUp; else if (t == "down") b |= kDown; + else if (t == "rleft") b |= kRLeft; + else if (t == "rright") b |= kRRight; + else if (t == "rup") b |= kRUp; + else if (t == "rdown") b |= kRDown; else die("--input-script: unknown button '" + t + "'"); if (plus == std::string::npos) break; start = plus + 1; @@ -87,6 +93,8 @@ int main(int argc, char** argv) { spec.gl = false; std::uint64_t frames = 60; std::uint64_t video_every = 1, pictures = 0; + bool pcm_hash = false; + std::uint64_t pcm = 0xcbf29ce484222325ull, audio_frames = 0; bool seat0 = true; bool presentation_test = false; bool expect_route_end = false; @@ -136,6 +144,7 @@ int main(int argc, char** argv) { else if (a == "--out") spec.session_dir = utf8_path(val()); else if (a == "--frames") frames = std::strtoull(val().c_str(), nullptr, 10); else if (a == "--video-every") video_every = std::strtoull(val().c_str(), nullptr, 10); + else if (a == "--pcm-hash") pcm_hash = true; else if (a == "--load-state") spec.load_state = utf8_path(val()); else if (a.size() >= 11 && a.compare(0, 6, "--tpak") == 0 && a[6] >= '1' && a[6] <= '4') { const std::size_t seat = static_cast(a[6] - '1'); @@ -275,6 +284,8 @@ int main(int argc, char** argv) { pads[0].buttons &= ~kStick; pads[0].axes[RCORE_AXIS_LX] = held & kLeft ? -32767 : held & kRight ? 32767 : 0; pads[0].axes[RCORE_AXIS_LY] = held & kUp ? 32767 : held & kDown ? -32767 : 0; + pads[0].axes[RCORE_AXIS_RX] = held & kRLeft ? -32767 : held & kRRight ? 32767 : 0; + pads[0].axes[RCORE_AXIS_RY] = held & kRUp ? 32767 : held & kRDown ? -32767 : 0; } // Fixture exercises resize, unavailable/minimized, then recovery. const std::uint32_t pw = presentation_test && k != 3 ? (k == 1 ? 1280 : 2560) : 0; @@ -293,7 +304,13 @@ int main(int argc, char** argv) { if (link.state() != LinkState::Ready && !clean_route_end) ok = false; if (link.take_frame()) ++pictures; std::int16_t sink[4096]; - while (link.drain_audio(sink, 2048)) { + while (const auto n = link.drain_audio(sink, 2048)) { + audio_frames += n; + if (pcm_hash) for (std::size_t i=0;i(sink[i]); + pcm=(pcm^(v&255u))*0x100000001b3ull; + pcm=(pcm^(v>>8))*0x100000001b3ull; + } } if (clean_route_end) break; for (const StateOp& op : record_ops) { @@ -370,6 +387,8 @@ int main(int argc, char** argv) { } // 2.2: what the overlay's readout is made of. std::printf("pictures received: %llu\n",static_cast(pictures)); + if (pcm_hash) std::printf("PCM_HASH %016llx over %llu samples\n", + static_cast(pcm),static_cast(audio_frames*2)); std::printf("frame stats: game %llu work %s\n", static_cast(link.frame_stats().game_frame), link.frame_stats().work_ns ? "measured" : "none"); From 52c5a9963f3be08b4601e2a0427ae32db0a56df7 Mon Sep 17 00:00:00 2001 From: Matthew Stanley <1379tech@gmail.com> Date: Sun, 15 Nov 2026 05:15:34 -0800 Subject: [PATCH 3/6] feat: lend optional CPU video targets from the owned frame slot --- CMakeLists.txt | 17 +++++++++++ docs/CORE_ABI.md | 1 + include/rcore/rcore.h | 32 ++++++++++++++++++++- runner/host_session.cpp | 18 ++++++++++++ runner/host_session.hpp | 9 ++++++ runner/runner_link.cpp | 49 +++++++++++++++++++++++++++---- tests/cpu_target_test.cmake | 24 ++++++++++++++++ tests/gpu_host_test.cpp | 27 +++++++++++++++++- tests/rcore_fake_core.c | 57 ++++++++++++++++++++++++++++++++++++- 9 files changed, 226 insertions(+), 8 deletions(-) create mode 100644 tests/cpu_target_test.cmake diff --git a/CMakeLists.txt b/CMakeLists.txt index fcb7d4f..c2f76a7 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -131,6 +131,23 @@ if(RETRO_RUNTIME_TOOLS) -DRUNNER=$ -DCORE=$ -DLINK=$ -DOUT=${CMAKE_CURRENT_BINARY_DIR}/route-integration -P ${CMAKE_CURRENT_SOURCE_DIR}/tests/route_test.cmake) + add_test(NAME cpu_target_routes COMMAND ${CMAKE_COMMAND} + -DRUNNER=$ -DCORE=$ + -DLINK=$ -DOUT=${CMAKE_CURRENT_BINARY_DIR}/cpu-target-routes + -P ${CMAKE_CURRENT_SOURCE_DIR}/tests/route_test.cmake) + set_tests_properties(cpu_target_routes PROPERTIES ENVIRONMENT "FAKE_CORE_CPU_TARGET=2") + add_test(NAME cpu_target_link COMMAND ${CMAKE_COMMAND} + -DRUNNER=$ -DCORE=$ + -DLINK_TEST=$ -DROM=${CMAKE_CURRENT_SOURCE_DIR}/LICENSE + -DOUT=${CMAKE_CURRENT_BINARY_DIR}/cpu-target-link + -P ${CMAKE_CURRENT_SOURCE_DIR}/tests/cpu_target_test.cmake) + set_tests_properties(cpu_target_link PROPERTIES ENVIRONMENT "FAKE_CORE_CPU_TARGET=1") + add_test(NAME cpu_target_refusal COMMAND ${CMAKE_COMMAND} + -DRUNNER=$ -DCORE=$ + -DLINK_TEST=$ -DROM=${CMAKE_CURRENT_SOURCE_DIR}/LICENSE + -DOUT=${CMAKE_CURRENT_BINARY_DIR}/cpu-target-refusal -DREFUSE=ON + -P ${CMAKE_CURRENT_SOURCE_DIR}/tests/cpu_target_test.cmake) + set_tests_properties(cpu_target_refusal PROPERTIES ENVIRONMENT "FAKE_CORE_CPU_TARGET=3") add_test(NAME route_watch COMMAND ${CMAKE_COMMAND} -DRUNNER=$ -DCORE=$ -DLINK=$ -DOUT=${CMAKE_CURRENT_BINARY_DIR}/route-watch diff --git a/docs/CORE_ABI.md b/docs/CORE_ABI.md index cd174cb..28949fa 100644 --- a/docs/CORE_ABI.md +++ b/docs/CORE_ABI.md @@ -12,6 +12,7 @@ only the shape of the contract and why. | Rev | Date | Change | |---|---|---| | 1 | 2026-09-23 | First draft. | +| 13 | 2026-11-15 | Optional appended host `video_acquire_cpu` lends a writable CPU image for the current field. Existing `video_submit` commits the exact active loan without a copy. A committed image remains read-only until the next publication; previous-image geometry is explicit. Missing/refused acquisition retains ordinary submission. Loans expire at submission or field return; cores preserve their own scanout history across fallback, omitted output, resize and restore. No shared frame-region or link layout change. | | 12 | 2026-11-15 | Optional appended host `video_requested(ctx, field, game_frame)` permits omitting video on an unobserved field. An absent callback requests every picture. Simulation, audio, input, device completion and correctness observers still run; internal display history may require a scanout despite permission to omit. Presentation state only, excluded from saves and netplay identity. | | 11 | 2026-10-19 | Optional `RCORE_CAP_GPU_FRAMES` and five appended host callbacks for a fixed actual-device offer, image-ring registration, resource status, shared-frame submission and retirement. CPU RGBA8 remains the fallback; no generated game ABI change. See [GPU_FRAMES.md](GPU_FRAMES.md). | | 10 | 2026-10-14 | Optional pre-init `execution_profiles()` capability/qualification query; appended init `execution_profile` index, valid with `RCORE_INIT_PROFILE_AGREED`. The runner exchanges offers/ACKs before init/guest boot, folds the common profile and canonical capability transcript into existing netplay identity, and keeps selection fixed through replay/reset/rollback. Old runners retain the core's conservative startup default. | diff --git a/include/rcore/rcore.h b/include/rcore/rcore.h index 8237275..bc31b02 100644 --- a/include/rcore/rcore.h +++ b/include/rcore/rcore.h @@ -48,7 +48,7 @@ extern "C" { #define RCORE_ABI_MAJOR 0u /* 0 = draft; the first implemented contract is 1 */ #define RCORE_ABI_MINOR 0u -#define RCORE_DRAFT_REVISION 12u /* draft-only counter; see docs/CORE_ABI.md */ +#define RCORE_DRAFT_REVISION 13u /* draft-only counter; see docs/CORE_ABI.md */ #if defined(_WIN32) # define RCORE_EXPORT __declspec(dllexport) @@ -138,6 +138,21 @@ typedef struct rcore_frame { uint64_t game_frame; } rcore_frame; +/* Rev 13: optional CPU scanout storage lent by the host. The caller sets + * struct_size. On success pixels contains width*height RGBA8 pixels with the + * returned stride (at least width*4); no capacity beyond stride*height is + * implied. previous_pixels, when non-NULL, is the last published CPU image, + * with its own geometry. It is read-only and may differ in size or be absent + * after a GPU picture. A recycled writable slot is NOT previous-image history. */ +typedef struct rcore_cpu_video_target { + uint32_t struct_size; + uint32_t width, height, stride, pixel_format; + uint32_t _pad0; + void* pixels; + const void* previous_pixels; + uint32_t previous_width, previous_height, previous_stride, previous_pixel_format; +} rcore_cpu_video_target; + /* Rev 11: a core-owned export ring, optional on both sides. Capabilities are * sampled once before load; release notifications below concern resource * ownership, never a simulation profile or a capability renegotiation. @@ -526,6 +541,21 @@ typedef struct rcore_host_api { * presentation-only; never guest input, compatibility identity or saved * simulation state. The host must accept legacy cores that ignore it. */ uint32_t (*video_requested)(void* host_ctx, uint64_t frame_number, uint64_t game_frame); + /* --- appended in draft revision 13: optional CPU video target --- + * Check struct_size and this pointer before use. Zero/absent means use + * ordinary video_submit. All calls and pixel access are on the core thread; + * only one loan may be active. On success write only the returned image, + * then video_submit with that exact pointer/geometry to publish without a + * copy. Writable access ends at video_submit or return from run_frame; + * never cache a writable pointer across either boundary. An unsubmitted + * loan publishes nothing. After a successful submit its pixels remain + * readable (never writable) as previous until the next publication or core + * teardown. Before fallback, omitted output, resize or restore, a core + * needing persistent scanout history must synchronize its owned history. + * Borrowed pixels are presentation-only, never saved simulation state. + * No host is required to support this extension. */ + uint32_t (*video_acquire_cpu)(void* host_ctx, uint32_t width, uint32_t height, + uint32_t pixel_format, rcore_cpu_video_target* out); } rcore_host_api; /* ------------------------------------------------------------------------ */ diff --git a/runner/host_session.cpp b/runner/host_session.cpp index bf8d911..e7ca9f9 100644 --- a/runner/host_session.cpp +++ b/runner/host_session.cpp @@ -51,6 +51,24 @@ void HostSession::offer_gpu_frames() { host_.gpu_frames_unregister = h_gpu_unregister; } +void HostSession::offer_cpu_frames() { + host_.video_acquire_cpu = h_video_acquire_cpu; +} + +std::uint32_t HostSession::h_video_acquire_cpu(void* ctx, std::uint32_t width, + std::uint32_t height, std::uint32_t format, + rcore_cpu_video_target* out) { + if (!out || out->struct_size < sizeof *out || !width || !height || + format != RCORE_PIXEL_RGBA8 || width > UINT32_MAX / 4 || + std::size_t(height) > SIZE_MAX / (std::size_t(width) * 4)) return 0; + auto* s = self(ctx); + // Route observers request pictures even when the host does not display. + if (!s->video_requested_ && !s->route_) return 0; + *out = {}; + out->struct_size = sizeof *out; + return s->sink_.acquire_cpu(width, height, format, *out) ? 1u : 0u; +} + std::uint32_t HostSession::h_gpu_caps(void* ctx, rcore_gpu_capabilities* out) { if (!out || out->struct_size < sizeof *out) return 0; *out = self(ctx)->gpu_caps_; diff --git a/runner/host_session.hpp b/runner/host_session.hpp index 466e3d1..1e7dfa8 100644 --- a/runner/host_session.hpp +++ b/runner/host_session.hpp @@ -27,6 +27,9 @@ class Sink { virtual void log(std::uint32_t level, const char* msg) = 0; virtual void event(const rcore_event& e) = 0; virtual void frame(const rcore_frame& f) = 0; + virtual bool acquire_cpu(std::uint32_t, std::uint32_t, std::uint32_t, + rcore_cpu_video_target&) { return false; } + virtual void end_frame() {} // Current progress, including fields whose pixels are not requested. // A core may repeat the same metadata; this is a value, not a counter tick. virtual void video_progress(std::uint64_t, std::uint64_t) {} @@ -86,6 +89,10 @@ class HostSession { // After link negotiation, before init. Import support/adapter identity is // sampled once; callbacks stay absent for CPU-only sinks and older hubs. void offer_gpu_frames(); + // CPU targets use the existing shared frame slots, independently of GPU + // import support. Only a sink owning writable storage offers this API. + void offer_cpu_frames(); + void end_frame() { sink_.end_frame(); } // The host table the core is given. Stable for the session's lifetime. const rcore_host_api* host_api() const { return &host_; } @@ -156,6 +163,8 @@ class HostSession { static void h_report(void*, const rcore_event*); static void h_video(void*, const rcore_frame*); static std::uint32_t h_video_requested(void*, std::uint64_t, std::uint64_t); + static std::uint32_t h_video_acquire_cpu(void*, std::uint32_t, std::uint32_t, + std::uint32_t, rcore_cpu_video_target*); static void h_audio(void*, const std::int16_t*, std::uint32_t); static void h_rate(void*, std::uint32_t); static void h_input(void*, std::uint32_t, rcore_pad*); diff --git a/runner/runner_link.cpp b/runner/runner_link.cpp index 62b3306..0dc7596 100644 --- a/runner/runner_link.cpp +++ b/runner/runner_link.cpp @@ -168,6 +168,7 @@ class LinkSink final : public Sink { ring.in_flight[f.image_index] = frame_number; last_game_frame = f.game_frame; last_gpu_ = true; + cpu_loan_ = false; return true; } void gpu_unregister(std::uint64_t generation) override { @@ -244,18 +245,52 @@ class LinkSink final : public Sink { if (e.kind == RCORE_EVENT_FAULT) ++faults; } + bool acquire_cpu(std::uint32_t width, std::uint32_t height, std::uint32_t format, + rcore_cpu_video_target& out) override { + if (cpu_loan_ || !width || !height || format != RCORE_PIXEL_RGBA8 || + width > kMaxFrameWidth || height > kMaxFrameHeight) return false; + out.width = width; out.height = height; out.stride = width * 4; + out.pixel_format = format; + out.pixels = base_ + shm_->frames_offset + std::size_t(back_) * kFrameSlotBytes; + if (published_ >= 0 && !last_gpu_) { + const FrameInfo& previous = shm_->slot[published_]; + out.previous_pixels = base_ + shm_->frames_offset + + std::size_t(published_) * kFrameSlotBytes; + out.previous_width = previous.width; out.previous_height = previous.height; + out.previous_stride = previous.stride; out.previous_pixel_format = previous.pixel_format; + } + cpu_loan_width_ = width; cpu_loan_height_ = height; + cpu_loan_ = true; + return true; + } + void end_frame() override { cpu_loan_ = false; } + void frame(const rcore_frame& f) override { last_gpu_ = false; - if (f.pixel_format != RCORE_PIXEL_RGBA8 || f.width > kMaxFrameWidth || - f.height > kMaxFrameHeight) { + const bool loan = cpu_loan_; + cpu_loan_ = false; // any submission ends writable access + if (!f.pixels || !f.width || !f.height || f.pixel_format != RCORE_PIXEL_RGBA8 || + f.width > kMaxFrameWidth || f.height > kMaxFrameHeight || f.stride < f.width * 4) { ++bad_frames; // reported at the end of the frame, not mid-core return; } std::uint8_t* dst = base_ + shm_->frames_offset + std::size_t(back_) * kFrameSlotBytes; const auto* src = static_cast(f.pixels); const std::size_t row = std::size_t(f.width) * 4; - for (std::uint32_t y = 0; y < f.height; ++y) { - std::memcpy(dst + y * row, src + std::size_t(y) * f.stride, row); + if (src == dst && loan && f.width == cpu_loan_width_ && f.height == cpu_loan_height_ && + f.stride == row) { + // The core filled our exclusively owned back slot. The metadata + // and atomic ownership exchange below are the publication. + } else { + const auto address = reinterpret_cast(src); + const auto back_address = reinterpret_cast(dst); + if (address >= back_address && address < back_address + kFrameSlotBytes) { + ++bad_frames; // stale/mismatched loan must not self-copy + return; + } + for (std::uint32_t y = 0; y < f.height; ++y) { + std::memcpy(dst + y * row, src + std::size_t(y) * f.stride, row); + } } FrameInfo& info = shm_->slot[back_]; info.width = f.width; @@ -367,6 +402,8 @@ class LinkSink final : public Sink { SharedHeader* shm_; std::uint8_t* base_; std::uint32_t back_ = 2; // the triple buffer's initial back slot + bool cpu_loan_ = false; + std::uint32_t cpu_loan_width_ = 0, cpu_loan_height_ = 0; int published_ = -1; // the slot last swapped into middle std::ofstream log_, events_; }; @@ -442,6 +479,7 @@ int run_link_mode(const LoadedCore& core, const CoreManifest& manifest, const Li sink.set_gpu_capabilities(offered.caps); session.offer_gpu_frames(); } + session.offer_cpu_frames(); for (const DataAccessory& d : a.data_accessories) session.bind_data_accessory(d.seat, d.slot); // 2.1: a hub's AccessoryData, queued for the next granted frame. One // arriving for a (seat, slot) nothing is plugged into is dropped and said @@ -765,6 +803,7 @@ int run_link_mode(const LoadedCore& core, const CoreManifest& manifest, const Li auto timed = [&](rcore_result (*fn)()) { const Clock::time_point t0 = Clock::now(); const rcore_result rc = fn(); + session.end_frame(); done.work_ns += static_cast( std::chrono::duration_cast(Clock::now() - t0).count()); return rc; @@ -828,7 +867,7 @@ int run_link_mode(const LoadedCore& core, const CoreManifest& manifest, const Li } if (sink.bad_frames) { code = exiting(sock, 1, "the core submitted a frame this link cannot carry " - "(format, or larger than 1024x1024)"); + "(invalid format, geometry, or CPU target)"); break; } if (done.result != RCORE_OK) { diff --git a/tests/cpu_target_test.cmake b/tests/cpu_target_test.cmake new file mode 100644 index 0000000..6d71728 --- /dev/null +++ b/tests/cpu_target_test.cmake @@ -0,0 +1,24 @@ +execute_process(COMMAND "${LINK_TEST}" --runner "${RUNNER}" --core "${CORE}" + --rom "${ROM}" --out "${OUT}" --frames 8 + RESULT_VARIABLE rc OUTPUT_VARIABLE output ERROR_VARIABLE errors) +if(REFUSE) + if(rc EQUAL 0 OR NOT "${output}${errors}" MATCHES "frame this link cannot carry") + message(FATAL_ERROR "mismatched CPU loan was not refused: ${rc}\n${output}\n${errors}") + endif() + return() +endif() +if(NOT rc EQUAL 0) + message(FATAL_ERROR "CPU-target link run failed: ${rc}\n${output}\n${errors}") +endif() +foreach(expected "8 frame(s) granted and done, runner exit 0, 0 fault(s)" + "pictures received: 7" "picture: first byte 8") + string(FIND "${output}" "${expected}" pos) + if(pos EQUAL -1) + message(FATAL_ERROR "missing CPU-target result: ${expected}\n${output}") + endif() +endforeach() +file(READ "${OUT}/core.log" log) +string(FIND "${log}" "FAKE_CPU_TARGET acquired=8 previous=7 abandoned=1" pos) +if(pos EQUAL -1) + message(FATAL_ERROR "CPU-target history/expiry checks did not execute: ${log}") +endif() diff --git a/tests/gpu_host_test.cpp b/tests/gpu_host_test.cpp index 7f21d17..ea02ce0 100644 --- a/tests/gpu_host_test.cpp +++ b/tests/gpu_host_test.cpp @@ -3,8 +3,9 @@ #include using namespace retro::runner; -static_assert(sizeof(void*)!=8 || sizeof(rcore_host_api)==208); +static_assert(sizeof(void*)!=8 || sizeof(rcore_host_api)==216); static_assert(sizeof(void*)!=8 || offsetof(rcore_host_api,video_requested)==200); +static_assert(sizeof(void*)!=8 || offsetof(rcore_host_api,video_acquire_cpu)==208); void check(bool ok, const char* what) { if (!ok) { std::cerr << "FAIL " << what << '\n'; std::exit(1); } } @@ -12,11 +13,15 @@ struct TestSink : Sink { bool available = false; unsigned samples = 0, registrations = 0, pictures = 0, retires = 0; unsigned cpu_pictures = 0, audio_frames = 0, input_polls = 0; + unsigned cpu_acquires = 0, field_ends = 0; std::uint64_t field = 0, game_frame = 0; rcore_gpu_capabilities caps{}; void log(std::uint32_t, const char*) override {} void event(const rcore_event&) override {} void frame(const rcore_frame&) override { ++cpu_pictures; } + bool acquire_cpu(std::uint32_t, std::uint32_t, std::uint32_t, + rcore_cpu_video_target&) override { ++cpu_acquires; return false; } + void end_frame() override { ++field_ends; } void video_progress(std::uint64_t f, std::uint64_t g) override { field=f;game_frame=g; } void audio(const std::int16_t*, std::uint32_t n) override { audio_frames+=n; } void audio_rate(std::uint32_t) override {} @@ -35,6 +40,26 @@ int main() { LoadedCore core; TestSink absent; HostSession cpu(core, absent); + check(!cpu.host_api()->video_acquire_cpu, "unsupported sink keeps optional CPU API absent"); + cpu.offer_cpu_frames(); + const auto* ch = cpu.host_api(); + rcore_cpu_video_target target{}; target.struct_size = sizeof target; + check(!ch->video_acquire_cpu(ch->host_ctx,64,48,RCORE_PIXEL_RGBA8,&target) && + absent.cpu_acquires==1 && !target.pixels, "sink refusal retains ordinary submit"); + --target.struct_size; + check(!ch->video_acquire_cpu(ch->host_ctx,64,48,RCORE_PIXEL_RGBA8,&target) && + !ch->video_acquire_cpu(ch->host_ctx,64,48,RCORE_PIXEL_RGBA8,nullptr) && + absent.cpu_acquires==1, "short/null CPU target refused before sink"); + target.struct_size=sizeof target; + check(!ch->video_acquire_cpu(ch->host_ctx,0,48,RCORE_PIXEL_RGBA8,&target) && + !ch->video_acquire_cpu(ch->host_ctx,UINT32_MAX,UINT32_MAX,RCORE_PIXEL_RGBA8,&target) && + !ch->video_acquire_cpu(ch->host_ctx,64,48,99,&target) && absent.cpu_acquires==1, + "empty/overflow/unsupported CPU geometry refused"); + cpu.set_video_requested(false); + check(!ch->video_acquire_cpu(ch->host_ctx,64,48,RCORE_PIXEL_RGBA8,&target) && + absent.cpu_acquires==1, "omitted video declines CPU loan"); + cpu.end_frame(); + check(absent.field_ends==1, "field return ends any outstanding CPU loan"); cpu.offer_gpu_frames(); absent.available = true; cpu.offer_gpu_frames(); diff --git a/tests/rcore_fake_core.c b/tests/rcore_fake_core.c index e697f53..b47b634 100644 --- a/tests/rcore_fake_core.c +++ b/tests/rcore_fake_core.c @@ -54,8 +54,13 @@ #include #include -_Static_assert(sizeof(void*)!=8 || sizeof(rcore_host_api)==208,"rev12 host size"); +_Static_assert(sizeof(void*)!=8 || sizeof(rcore_host_api)==216,"rev13 host size"); _Static_assert(sizeof(void*)!=8 || offsetof(rcore_host_api,video_requested)==200,"rev11 prefix unchanged"); +_Static_assert(sizeof(void*)!=8 || offsetof(rcore_host_api,video_acquire_cpu)==208,"rev12 prefix unchanged"); +_Static_assert(offsetof(rcore_cpu_video_target,pixels)==24,"CPU target pointer alignment"); +_Static_assert(offsetof(rcore_cpu_video_target,previous_pixels)==24+sizeof(void*),"CPU previous pointer"); +_Static_assert(offsetof(rcore_cpu_video_target,previous_width)==24+2*sizeof(void*),"CPU previous geometry"); +_Static_assert(sizeof(rcore_cpu_video_target)==40+2*sizeof(void*),"CPU target C layout"); #if defined(_WIN32) # include @@ -72,6 +77,8 @@ static unsigned long long g_frames; static unsigned char g_pixels[W * H * 4]; static unsigned char* g_battery; static unsigned long long g_crash_at; +static unsigned g_cpu_acquired, g_cpu_previous, g_cpu_abandoned; +static unsigned g_cpu_last_value, g_cpu_last_width, g_cpu_last_height; static rcore_save_region k_regions[1]; @@ -316,6 +323,49 @@ static rcore_result run_frame(void) { f.pixels = g_pixels; f.frame_number = g_frames; f.game_frame = g_frames / 2; /* rev 8: a game drawing every other frame */ + /* Exercise the actual linked runner's owned back slot. Mode1 includes an + * abandoned field, resize, then an ordinary private-buffer submission; + * mode2 keeps ordinary pictures for the existing route/recording oracle. */ + const char* cpu_mode = getenv("FAKE_CORE_CPU_TARGET"); + if (cpu_mode && atoi(cpu_mode) && + g_host->struct_size >= offsetof(rcore_host_api,video_acquire_cpu)+sizeof(g_host->video_acquire_cpu) && + g_host->video_acquire_cpu) { + const int transitions = atoi(cpu_mode)==1; + rcore_cpu_video_target out = {0}, nested = {0}; + const uint32_t width = transitions && g_frames==4 ? W/2 : W; + const uint32_t height = transitions && g_frames==4 ? H/2 : H; + out.struct_size=sizeof out; + if (!g_host->video_acquire_cpu(g_host->host_ctx,width,height,RCORE_PIXEL_RGBA8,&out) || + !out.pixels || out.width!=width || out.height!=height || out.stride!=width*4 || + out.pixel_format!=RCORE_PIXEL_RGBA8) return RCORE_ERR_STATE; + ++g_cpu_acquired; + if (g_cpu_last_width) { + if (!out.previous_pixels || out.previous_pixels==out.pixels || + out.previous_width!=g_cpu_last_width || out.previous_height!=g_cpu_last_height || + out.previous_stride!=g_cpu_last_width*4 || out.previous_pixel_format!=RCORE_PIXEL_RGBA8 || + ((const unsigned char*)out.previous_pixels)[0]!=g_cpu_last_value) + return RCORE_ERR_STATE; + ++g_cpu_previous; + } else if (out.previous_pixels) return RCORE_ERR_STATE; + nested.struct_size=sizeof nested; + if (g_host->video_acquire_cpu(g_host->host_ctx,width,height,RCORE_PIXEL_RGBA8,&nested)) + return RCORE_ERR_STATE; /* two simultaneous writers are forbidden */ + memset(out.pixels,(int)(g_frames&255),(size_t)out.stride*out.height); + if (transitions && g_frames==3) { + ++g_cpu_abandoned; + return RCORE_OK; /* next run must acquire again, not publish this */ + } + if (!(transitions && g_frames==5)) { + f.width=out.width; f.height=out.height; f.stride=out.stride; f.pixels=out.pixels; + } + if (atoi(cpu_mode)==3) f.stride+=4; /* mismatched loan must not self-copy */ + g_cpu_last_value=(unsigned)(g_frames&255); + g_cpu_last_width=f.width; g_cpu_last_height=f.height; + g_host->video_submit(g_host->host_ctx,&f); + if (f.pixels==out.pixels && ((const unsigned char*)f.pixels)[0]!=g_cpu_last_value) + return RCORE_ERR_STATE; /* committed image remains readable */ + return RCORE_OK; + } g_host->video_submit(g_host->host_ctx, &f); return RCORE_OK; } @@ -350,6 +400,11 @@ static void unload(void) { char line[64]; snprintf(line, sizeof line, "FAKE_DONE frames=%llu", g_frames); g_host->log(g_host->host_ctx, RCORE_LOG_INFO, line); + if (getenv("FAKE_CORE_CPU_TARGET")) { + snprintf(line,sizeof line,"FAKE_CPU_TARGET acquired=%u previous=%u abandoned=%u", + g_cpu_acquired,g_cpu_previous,g_cpu_abandoned); + g_host->log(g_host->host_ctx,RCORE_LOG_INFO,line); + } } static void deinit(void) {} From 985a67eee35be2bc9ce5400190e93d14c79a5a68 Mon Sep 17 00:00:00 2001 From: Matthew Stanley <1379tech@gmail.com> Date: Sun, 15 Nov 2026 06:18:16 -0800 Subject: [PATCH 4/6] feat: repeat unpaced CPU pictures without shared-slot publication --- CMakeLists.txt | 6 +++++ corelink/link_test_main.cpp | 12 +++++++++ docs/CORE_ABI.md | 1 + docs/CORE_RUNNER.md | 7 +++++ include/rcore/rcore.h | 12 ++++++++- runner/host_session.cpp | 15 ++++++++--- runner/runner_link.cpp | 31 ++++++++++++++++++---- runner/runner_link.hpp | 1 + runner/runner_main.cpp | 4 ++- tests/cpu_repeat_test.cmake | 53 +++++++++++++++++++++++++++++++++++++ tests/gpu_host_test.cpp | 28 +++++++++++++++++--- tests/rcore_fake_core.c | 32 ++++++++++++++++++---- 12 files changed, 183 insertions(+), 19 deletions(-) create mode 100644 tests/cpu_repeat_test.cmake diff --git a/CMakeLists.txt b/CMakeLists.txt index c2f76a7..da291f5 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -148,6 +148,12 @@ if(RETRO_RUNTIME_TOOLS) -DOUT=${CMAKE_CURRENT_BINARY_DIR}/cpu-target-refusal -DREFUSE=ON -P ${CMAKE_CURRENT_SOURCE_DIR}/tests/cpu_target_test.cmake) set_tests_properties(cpu_target_refusal PROPERTIES ENVIRONMENT "FAKE_CORE_CPU_TARGET=3") + add_test(NAME cpu_repeat_link COMMAND ${CMAKE_COMMAND} + -DRUNNER=$ -DCORE=$ + -DLINK_TEST=$ -DROM=${CMAKE_CURRENT_SOURCE_DIR}/LICENSE + -DOUT=${CMAKE_CURRENT_BINARY_DIR}/cpu-repeat-link + -P ${CMAKE_CURRENT_SOURCE_DIR}/tests/cpu_repeat_test.cmake) + set_tests_properties(cpu_repeat_link PROPERTIES ENVIRONMENT "FAKE_CORE_CPU_TARGET=4") add_test(NAME route_watch COMMAND ${CMAKE_COMMAND} -DRUNNER=$ -DCORE=$ -DLINK=$ -DOUT=${CMAKE_CURRENT_BINARY_DIR}/route-watch diff --git a/corelink/link_test_main.cpp b/corelink/link_test_main.cpp index 698c6b5..bcc03ba 100644 --- a/corelink/link_test_main.cpp +++ b/corelink/link_test_main.cpp @@ -97,6 +97,7 @@ int main(int argc, char** argv) { std::uint64_t pcm = 0xcbf29ce484222325ull, audio_frames = 0; bool seat0 = true; bool presentation_test = false; + bool unpaced = false, cpu_repeat_test = false; bool expect_route_end = false; std::string restart_route; std::vector> script; @@ -145,6 +146,8 @@ int main(int argc, char** argv) { else if (a == "--frames") frames = std::strtoull(val().c_str(), nullptr, 10); else if (a == "--video-every") video_every = std::strtoull(val().c_str(), nullptr, 10); else if (a == "--pcm-hash") pcm_hash = true; + else if (a == "--unpaced") { unpaced = true; spec.extra_args.push_back("--unpaced"); } + else if (a == "--cpu-repeat-test") cpu_repeat_test = true; else if (a == "--load-state") spec.load_state = utf8_path(val()); else if (a.size() >= 11 && a.compare(0, 6, "--tpak") == 0 && a[6] >= '1' && a[6] <= '4') { const std::size_t seat = static_cast(a[6] - '1'); @@ -303,6 +306,15 @@ int main(int argc, char** argv) { const bool clean_route_end = expect_route_end && link.state()==LinkState::Ended && !link.exit_code(); if (link.state() != LinkState::Ready && !clean_route_end) ok = false; if (link.take_frame()) ++pictures; + if (cpu_repeat_test) { + const auto* f = link.frame_info(); + const auto* pixels = link.frame_pixels(); + const auto published = unpaced && !(k & 1) ? k - 1 : k; + if (!f || !pixels || f->frame_number != published || f->width != 64 || f->height != 48 || + link.frame_stats().game_frame != k / 2) die("CPU repeat changed metadata or lost progress"); + for (std::size_t i = 0; i < std::size_t(f->stride) * f->height; ++i) + if (pixels[i] != (k + 1) / 2) die("CPU repeat changed per-field pixels"); + } std::int16_t sink[4096]; while (const auto n = link.drain_audio(sink, 2048)) { audio_frames += n; diff --git a/docs/CORE_ABI.md b/docs/CORE_ABI.md index 28949fa..5d7dece 100644 --- a/docs/CORE_ABI.md +++ b/docs/CORE_ABI.md @@ -12,6 +12,7 @@ only the shape of the contract and why. | Rev | Date | Change | |---|---|---| | 1 | 2026-09-23 | First draft. | +| 14 | 2026-11-15 | CPU targets append `flags` and padding after the rev13 prefix. An explicitly unpaced host may advertise `RCORE_CPU_VIDEO_REPEAT_PREVIOUS`: submitting the exact previous image with unchanged geometry and aspect advances observers/progress without copying or republishing shared storage. Pixels stay read-only at the same address until a non-repeat publication. Hosts clip output to the caller's supplied target size; older targets remain supported. The host callback/table and link layout are unchanged. | | 13 | 2026-11-15 | Optional appended host `video_acquire_cpu` lends a writable CPU image for the current field. Existing `video_submit` commits the exact active loan without a copy. A committed image remains read-only until the next publication; previous-image geometry is explicit. Missing/refused acquisition retains ordinary submission. Loans expire at submission or field return; cores preserve their own scanout history across fallback, omitted output, resize and restore. No shared frame-region or link layout change. | | 12 | 2026-11-15 | Optional appended host `video_requested(ctx, field, game_frame)` permits omitting video on an unobserved field. An absent callback requests every picture. Simulation, audio, input, device completion and correctness observers still run; internal display history may require a scanout despite permission to omit. Presentation state only, excluded from saves and netplay identity. | | 11 | 2026-10-19 | Optional `RCORE_CAP_GPU_FRAMES` and five appended host callbacks for a fixed actual-device offer, image-ring registration, resource status, shared-frame submission and retirement. CPU RGBA8 remains the fallback; no generated game ABI change. See [GPU_FRAMES.md](GPU_FRAMES.md). | diff --git a/docs/CORE_RUNNER.md b/docs/CORE_RUNNER.md index f8b3588..90b5238 100644 --- a/docs/CORE_RUNNER.md +++ b/docs/CORE_RUNNER.md @@ -11,6 +11,13 @@ from the same-process `--replay-at` save/restore probe below. **Status, 2026-09-25:** two modes. **Headless** is described here. **Link** (`--link`) is how the hub runs a core in its own window; see `CORE_LINK.md`. +`--unpaced` explicitly marks a link session that does not pace presentation. +Only such a session offers CPU previous-image repeat without shared-slot +republication. The default keeps ordinary per-picture publication. Hosts pass +this through `LaunchSpec.extra_args`; `retro-core-link-test --unpaced` forwards +it to the runner. Simulation, audio, route recording and frame progress still +advance on every field. + ## What it does today 1. **Loads the core and computes its identity.** It opens the library once, diff --git a/include/rcore/rcore.h b/include/rcore/rcore.h index bc31b02..7e2bee3 100644 --- a/include/rcore/rcore.h +++ b/include/rcore/rcore.h @@ -48,7 +48,7 @@ extern "C" { #define RCORE_ABI_MAJOR 0u /* 0 = draft; the first implemented contract is 1 */ #define RCORE_ABI_MINOR 0u -#define RCORE_DRAFT_REVISION 13u /* draft-only counter; see docs/CORE_ABI.md */ +#define RCORE_DRAFT_REVISION 14u /* draft-only counter; see docs/CORE_ABI.md */ #if defined(_WIN32) # define RCORE_EXPORT __declspec(dllexport) @@ -144,6 +144,7 @@ typedef struct rcore_frame { * implied. previous_pixels, when non-NULL, is the last published CPU image, * with its own geometry. It is read-only and may differ in size or be absent * after a GPU picture. A recycled writable slot is NOT previous-image history. */ +#define RCORE_CPU_VIDEO_REPEAT_PREVIOUS (1u << 0) typedef struct rcore_cpu_video_target { uint32_t struct_size; uint32_t width, height, stride, pixel_format; @@ -151,6 +152,15 @@ typedef struct rcore_cpu_video_target { void* pixels; const void* previous_pixels; uint32_t previous_width, previous_height, previous_stride, previous_pixel_format; + /* Rev 14: read only when struct_size covers flags. An unpaced host may + * advertise REPEAT_PREVIOUS: submit the exact previous_pixels, geometry, + * format and unchanged aspect to repeat without copying or republishing. + * The submission ends the writable loan and still advances frame progress + * and observers. Previous pixels stay read-only at the same address until + * the next non-repeat publication or teardown. Other submissions retain + * the ordinary publication contract. The rev13 prefix remains accepted. */ + uint32_t flags; + uint32_t _pad1; } rcore_cpu_video_target; /* Rev 11: a core-owned export ring, optional on both sides. Capabilities are diff --git a/runner/host_session.cpp b/runner/host_session.cpp index e7ca9f9..ec49913 100644 --- a/runner/host_session.cpp +++ b/runner/host_session.cpp @@ -1,6 +1,7 @@ #include "host_session.hpp" #include "route.hpp" +#include #include #include #include @@ -58,15 +59,21 @@ void HostSession::offer_cpu_frames() { std::uint32_t HostSession::h_video_acquire_cpu(void* ctx, std::uint32_t width, std::uint32_t height, std::uint32_t format, rcore_cpu_video_target* out) { - if (!out || out->struct_size < sizeof *out || !width || !height || + if (!out || out->struct_size < offsetof(rcore_cpu_video_target, flags) || !width || !height || format != RCORE_PIXEL_RGBA8 || width > UINT32_MAX / 4 || std::size_t(height) > SIZE_MAX / (std::size_t(width) * 4)) return 0; auto* s = self(ctx); // Route observers request pictures even when the host does not display. if (!s->video_requested_ && !s->route_) return 0; - *out = {}; - out->struct_size = sizeof *out; - return s->sink_.acquire_cpu(width, height, format, *out) ? 1u : 0u; + // The producer always gets our full local object; an older core lends + // only the rev13 prefix. Never write our appended flags into its stack. + const auto supplied = std::min(out->struct_size, sizeof *out); + rcore_cpu_video_target target{}; + target.struct_size = static_cast(supplied); + if (!s->sink_.acquire_cpu(width, height, format, target)) return 0; + target.struct_size = static_cast(supplied); + std::memcpy(out, &target, supplied); + return 1; } std::uint32_t HostSession::h_gpu_caps(void* ctx, rcore_gpu_capabilities* out) { diff --git a/runner/runner_link.cpp b/runner/runner_link.cpp index 0dc7596..b513298 100644 --- a/runner/runner_link.cpp +++ b/runner/runner_link.cpp @@ -98,8 +98,8 @@ void copy_str(char* dst, std::size_t cap, const char* src) { // dir (everything) and to the hub (what it shows); input from the grant. class LinkSink final : public Sink { public: - LinkSink(Channel& sock, SharedHeader* shm, const fs::path& out) - : sock_(sock), shm_(shm), log_(out / "core.log"), events_(out / "events.tsv") { + LinkSink(Channel& sock, SharedHeader* shm, const fs::path& out, bool unpaced) + : sock_(sock), shm_(shm), unpaced_(unpaced), log_(out / "core.log"), events_(out / "events.tsv") { base_ = reinterpret_cast(shm); } ~LinkSink() override { @@ -169,6 +169,7 @@ class LinkSink final : public Sink { last_game_frame = f.game_frame; last_gpu_ = true; cpu_loan_ = false; + cpu_repeat_ = false; return true; } void gpu_unregister(std::uint64_t generation) override { @@ -258,22 +259,39 @@ class LinkSink final : public Sink { std::size_t(published_) * kFrameSlotBytes; out.previous_width = previous.width; out.previous_height = previous.height; out.previous_stride = previous.stride; out.previous_pixel_format = previous.pixel_format; + if (unpaced_ && out.struct_size >= offsetof(rcore_cpu_video_target, flags) + sizeof out.flags) + out.flags = RCORE_CPU_VIDEO_REPEAT_PREVIOUS; } cpu_loan_width_ = width; cpu_loan_height_ = height; cpu_loan_ = true; + cpu_repeat_ = (out.flags & RCORE_CPU_VIDEO_REPEAT_PREVIOUS) != 0; return true; } - void end_frame() override { cpu_loan_ = false; } + void end_frame() override { cpu_loan_ = cpu_repeat_ = false; } void frame(const rcore_frame& f) override { - last_gpu_ = false; const bool loan = cpu_loan_; + const bool repeat = cpu_repeat_; cpu_loan_ = false; // any submission ends writable access + cpu_repeat_ = false; if (!f.pixels || !f.width || !f.height || f.pixel_format != RCORE_PIXEL_RGBA8 || f.width > kMaxFrameWidth || f.height > kMaxFrameHeight || f.stride < f.width * 4) { ++bad_frames; // reported at the end of the frame, not mid-core return; } + if (loan && repeat && !last_gpu_ && published_ >= 0) { + const FrameInfo& previous = shm_->slot[published_]; + const auto* pixels = base_ + shm_->frames_offset + std::size_t(published_) * kFrameSlotBytes; + if (f.pixels == pixels && f.width == previous.width && f.height == previous.height && + f.stride == previous.stride && f.pixel_format == previous.pixel_format && + f.aspect_num == previous.aspect_num && f.aspect_den == previous.aspect_den) { + // HostSession observed this field before commit. Keep both + // pixels and metadata untouched: this slot may be hub-owned. + last_game_frame = f.struct_size >= offsetof(rcore_frame, game_frame) + sizeof f.game_frame + ? f.game_frame : 0; + return; + } + } std::uint8_t* dst = base_ + shm_->frames_offset + std::size_t(back_) * kFrameSlotBytes; const auto* src = static_cast(f.pixels); const std::size_t row = std::size_t(f.width) * 4; @@ -293,6 +311,7 @@ class LinkSink final : public Sink { } } FrameInfo& info = shm_->slot[back_]; + last_gpu_ = false; info.width = f.width; info.height = f.height; info.stride = static_cast(row); @@ -403,7 +422,9 @@ class LinkSink final : public Sink { std::uint8_t* base_; std::uint32_t back_ = 2; // the triple buffer's initial back slot bool cpu_loan_ = false; + bool cpu_repeat_ = false; std::uint32_t cpu_loan_width_ = 0, cpu_loan_height_ = 0; + const bool unpaced_; int published_ = -1; // the slot last swapped into middle std::ofstream log_, events_; }; @@ -467,7 +488,7 @@ int run_link_mode(const LoadedCore& core, const CoreManifest& manifest, const Li const std::size_t frame_done_size = link_has_frame_stats(session_minor) ? sizeof(FrameDoneMsg) : kFrameDoneSize21; - LinkSink sink(sock, shm, a.out); + LinkSink sink(sock, shm, a.out, a.unpaced); HostSession session(core, sink); if (link_has_gpu_frames(session_minor)) { std::vector packet; diff --git a/runner/runner_link.hpp b/runner/runner_link.hpp index bde67d2..2e5269e 100644 --- a/runner/runner_link.hpp +++ b/runner/runner_link.hpp @@ -62,6 +62,7 @@ struct LinkArgs { fs::path out; // session dir: core.log, events.tsv, the core's cache_dir bool gl = false; bool strict = false; + bool unpaced = false; // explicit host launch policy, never inferred from timing std::map overrides; std::optional load_state; // --tpakN-rom: the Game Boy cartridge in seat N's Transfer Pak (N = 1-4), diff --git a/runner/runner_main.cpp b/runner/runner_main.cpp index b82b24d..a028cf6 100644 --- a/runner/runner_main.cpp +++ b/runner/runner_main.cpp @@ -424,7 +424,7 @@ int main(int argc, char** argv) { std::array vru_seats{}; std::optional accessory_script; std::string link_handles; - bool gl = false, strict = false, seat0 = true, list_options = false, link = false; + bool gl = false, strict = false, seat0 = true, list_options = false, link = false, unpaced = false; bool describe = false; std::optional replay_at; RouteArgs route_args; @@ -482,6 +482,7 @@ int main(int argc, char** argv) { else if (a == "--link") link = true; else if (a == "--link-handles") link_handles = val(); else if (a == "--strict") strict = true; + else if (a == "--unpaced") unpaced = true; else if (a == "--no-seats") seat0 = false; else if (a == "--list-options") list_options = true; else if (a == "--describe") describe = true; @@ -679,6 +680,7 @@ int main(int argc, char** argv) { la.out = out; la.gl = gl; la.strict = strict; + la.unpaced = unpaced; la.overrides = overrides; la.load_state = load_state; la.tpak_roms = tpak_roms; diff --git a/tests/cpu_repeat_test.cmake b/tests/cpu_repeat_test.cmake new file mode 100644 index 0000000..9aae385 --- /dev/null +++ b/tests/cpu_repeat_test.cmake @@ -0,0 +1,53 @@ +foreach(mode paced unpaced) + set(args) + set(pictures 8) + set(repeats 0) + if(mode STREQUAL unpaced) + list(APPEND args --unpaced) + set(pictures 4) + set(repeats 4) + endif() + execute_process(COMMAND "${LINK_TEST}" --runner "${RUNNER}" --core "${CORE}" + --rom "${ROM}" --out "${OUT}/${mode}" --frames 8 --cpu-repeat-test --pcm-hash ${args} + RESULT_VARIABLE rc OUTPUT_VARIABLE output ERROR_VARIABLE errors) + if(NOT rc EQUAL 0) + message(FATAL_ERROR "${mode} CPU repeat failed: ${rc}\n${output}\n${errors}") + endif() + foreach(expected "8 frame(s) granted and done, runner exit 0, 0 fault(s)" + "pictures received: ${pictures}" "picture: first byte 4" + "frame stats: game 4 work measured" "over 32 samples") + string(FIND "${output}" "${expected}" pos) + if(pos EQUAL -1) + message(FATAL_ERROR "${mode} missing result ${expected}\n${output}") + endif() + endforeach() + file(READ "${OUT}/${mode}/core.log" log) + if(NOT log MATCHES "FAKE_CPU_REPEAT repeats=${repeats}") + message(FATAL_ERROR "${mode} repeat branch was not selected correctly: ${log}") + endif() + string(REGEX MATCH "PCM_HASH [0-9a-f]+ over 32 samples" pcm "${output}") + if(mode STREQUAL paced) + set(reference_pcm "${pcm}") + elseif(NOT pcm STREQUAL reference_pcm) + message(FATAL_ERROR "repeats changed PCM: ${reference_pcm} vs ${pcm}") + endif() +endforeach() +# The recorder sees each submitted field before an unchanged picture is +# elided. Playback without lending must match every pixel and audio digest. +execute_process(COMMAND "${CMAKE_COMMAND}" -E env "RETRO_RECORD_ROUTE=${OUT}/repeat.route" + "${LINK_TEST}" --runner "${RUNNER}" --core "${CORE}" --rom "${ROM}" + --out "${OUT}/record" --frames 8 --cpu-repeat-test --unpaced + RESULT_VARIABLE rc OUTPUT_VARIABLE output ERROR_VARIABLE errors) +if(NOT rc EQUAL 0 OR NOT output MATCHES "pictures received: 4") + message(FATAL_ERROR "repeat route record failed: ${rc}\n${output}\n${errors}") +endif() +execute_process(COMMAND "${RUNNER}" --core "${CORE}" --rom "${ROM}" + --play-route "${OUT}/repeat.route" --out "${OUT}/play" + RESULT_VARIABLE rc OUTPUT_VARIABLE output ERROR_VARIABLE errors) +if(NOT rc EQUAL 0) + message(FATAL_ERROR "repeat route playback failed: ${rc}\n${output}\n${errors}") +endif() +file(READ "${OUT}/play/route-verdict.json" verdict) +if(NOT verdict MATCHES "in_sync") + message(FATAL_ERROR "repeat per-field pixels/audio diverged: ${verdict}") +endif() diff --git a/tests/gpu_host_test.cpp b/tests/gpu_host_test.cpp index ea02ce0..f2d8f31 100644 --- a/tests/gpu_host_test.cpp +++ b/tests/gpu_host_test.cpp @@ -6,6 +6,8 @@ using namespace retro::runner; static_assert(sizeof(void*)!=8 || sizeof(rcore_host_api)==216); static_assert(sizeof(void*)!=8 || offsetof(rcore_host_api,video_requested)==200); static_assert(sizeof(void*)!=8 || offsetof(rcore_host_api,video_acquire_cpu)==208); +static_assert(sizeof(void*)!=8 || sizeof(rcore_cpu_video_target)==64); +static_assert(sizeof(void*)!=8 || offsetof(rcore_cpu_video_target,flags)==56); void check(bool ok, const char* what) { if (!ok) { std::cerr << "FAIL " << what << '\n'; std::exit(1); } } @@ -14,13 +16,21 @@ struct TestSink : Sink { unsigned samples = 0, registrations = 0, pictures = 0, retires = 0; unsigned cpu_pictures = 0, audio_frames = 0, input_polls = 0; unsigned cpu_acquires = 0, field_ends = 0; + bool lend_cpu = false; + std::uint8_t cpu_pixels[64*48*4]{}; std::uint64_t field = 0, game_frame = 0; rcore_gpu_capabilities caps{}; void log(std::uint32_t, const char*) override {} void event(const rcore_event&) override {} void frame(const rcore_frame&) override { ++cpu_pictures; } - bool acquire_cpu(std::uint32_t, std::uint32_t, std::uint32_t, - rcore_cpu_video_target&) override { ++cpu_acquires; return false; } + bool acquire_cpu(std::uint32_t w, std::uint32_t h, std::uint32_t format, + rcore_cpu_video_target& out) override { + ++cpu_acquires; + if (!lend_cpu) return false; + out.width=w;out.height=h;out.stride=w*4;out.pixel_format=format; + out.pixels=cpu_pixels;out.flags=RCORE_CPU_VIDEO_REPEAT_PREVIOUS; + return true; + } void end_frame() override { ++field_ends; } void video_progress(std::uint64_t f, std::uint64_t g) override { field=f;game_frame=g; } void audio(const std::int16_t*, std::uint32_t n) override { audio_frames+=n; } @@ -46,7 +56,7 @@ int main() { rcore_cpu_video_target target{}; target.struct_size = sizeof target; check(!ch->video_acquire_cpu(ch->host_ctx,64,48,RCORE_PIXEL_RGBA8,&target) && absent.cpu_acquires==1 && !target.pixels, "sink refusal retains ordinary submit"); - --target.struct_size; + target.struct_size=offsetof(rcore_cpu_video_target,flags)-1; check(!ch->video_acquire_cpu(ch->host_ctx,64,48,RCORE_PIXEL_RGBA8,&target) && !ch->video_acquire_cpu(ch->host_ctx,64,48,RCORE_PIXEL_RGBA8,nullptr) && absent.cpu_acquires==1, "short/null CPU target refused before sink"); @@ -60,6 +70,18 @@ int main() { absent.cpu_acquires==1, "omitted video declines CPU loan"); cpu.end_frame(); check(absent.field_ends==1, "field return ends any outstanding CPU loan"); + cpu.set_video_requested(true); + absent.lend_cpu=true; + target.struct_size=offsetof(rcore_cpu_video_target,flags); + target.flags=0xabcddcba;target._pad1=0x98766543; + check(ch->video_acquire_cpu(ch->host_ctx,64,48,RCORE_PIXEL_RGBA8,&target) && + target.struct_size==offsetof(rcore_cpu_video_target,flags) && target.pixels==absent.cpu_pixels && + target.flags==0xabcddcba && target._pad1==0x98766543, + "older56-byte target prefix has no writes past supplied size"); + target.struct_size=sizeof target; + check(ch->video_acquire_cpu(ch->host_ctx,64,48,RCORE_PIXEL_RGBA8,&target) && + target.struct_size==sizeof target && target.flags==RCORE_CPU_VIDEO_REPEAT_PREVIOUS, + "new64-byte target receives appended flags"); cpu.offer_gpu_frames(); absent.available = true; cpu.offer_gpu_frames(); diff --git a/tests/rcore_fake_core.c b/tests/rcore_fake_core.c index b47b634..e33cb6a 100644 --- a/tests/rcore_fake_core.c +++ b/tests/rcore_fake_core.c @@ -60,7 +60,8 @@ _Static_assert(sizeof(void*)!=8 || offsetof(rcore_host_api,video_acquire_cpu)==2 _Static_assert(offsetof(rcore_cpu_video_target,pixels)==24,"CPU target pointer alignment"); _Static_assert(offsetof(rcore_cpu_video_target,previous_pixels)==24+sizeof(void*),"CPU previous pointer"); _Static_assert(offsetof(rcore_cpu_video_target,previous_width)==24+2*sizeof(void*),"CPU previous geometry"); -_Static_assert(sizeof(rcore_cpu_video_target)==40+2*sizeof(void*),"CPU target C layout"); +_Static_assert(offsetof(rcore_cpu_video_target,flags)==40+2*sizeof(void*),"CPU target rev13 prefix unchanged"); +_Static_assert(sizeof(rcore_cpu_video_target)==48+2*sizeof(void*),"CPU target C layout"); #if defined(_WIN32) # include @@ -79,6 +80,8 @@ static unsigned char* g_battery; static unsigned long long g_crash_at; static unsigned g_cpu_acquired, g_cpu_previous, g_cpu_abandoned; static unsigned g_cpu_last_value, g_cpu_last_width, g_cpu_last_height; +static const void* g_cpu_last_pointer; +static unsigned g_cpu_repeats; static rcore_save_region k_regions[1]; @@ -285,6 +288,13 @@ static void die_now(void) { static rcore_result run_frame(void) { ++g_frames; + const char* cpu_mode = getenv("FAKE_CORE_CPU_TARGET"); + const int repeat_mode = cpu_mode && atoi(cpu_mode)==4; + const unsigned pixel_value = (unsigned)((repeat_mode ? (g_frames+1)/2 : g_frames)&255); + if (repeat_mode) { + int16_t pcm[4]={(int16_t)g_frames,-(int16_t)g_frames,(int16_t)(g_frames*3),-(int16_t)(g_frames*3)}; + g_host->audio_push(g_host->host_ctx,pcm,2); + } if (g_host->struct_size >= offsetof(rcore_host_api, presentation_size) + sizeof(g_host->presentation_size) && g_host->presentation_size) { uint32_t w = 99, h = 99, w2 = 0, h2 = 0; @@ -312,7 +322,7 @@ static rcore_result run_frame(void) { g_host->video_requested && !g_host->video_requested(g_host->host_ctx,g_frames,g_frames/2)) { return RCORE_OK; } - memset(g_pixels, (int)(g_frames & 0xff), sizeof g_pixels); + memset(g_pixels, (int)pixel_value, sizeof g_pixels); rcore_frame f; memset(&f, 0, sizeof f); f.struct_size = sizeof f; @@ -326,7 +336,6 @@ static rcore_result run_frame(void) { /* Exercise the actual linked runner's owned back slot. Mode1 includes an * abandoned field, resize, then an ordinary private-buffer submission; * mode2 keeps ordinary pictures for the existing route/recording oracle. */ - const char* cpu_mode = getenv("FAKE_CORE_CPU_TARGET"); if (cpu_mode && atoi(cpu_mode) && g_host->struct_size >= offsetof(rcore_host_api,video_acquire_cpu)+sizeof(g_host->video_acquire_cpu) && g_host->video_acquire_cpu) { @@ -350,7 +359,17 @@ static rcore_result run_frame(void) { nested.struct_size=sizeof nested; if (g_host->video_acquire_cpu(g_host->host_ctx,width,height,RCORE_PIXEL_RGBA8,&nested)) return RCORE_ERR_STATE; /* two simultaneous writers are forbidden */ - memset(out.pixels,(int)(g_frames&255),(size_t)out.stride*out.height); + if (repeat_mode && out.struct_size>=offsetof(rcore_cpu_video_target,flags)+sizeof out.flags && + (out.flags&RCORE_CPU_VIDEO_REPEAT_PREVIOUS) && out.previous_pixels && + g_cpu_last_value==pixel_value) { + if (out.previous_pixels!=g_cpu_last_pointer) return RCORE_ERR_STATE; + f.pixels=out.previous_pixels; + ++g_cpu_repeats; + g_host->video_submit(g_host->host_ctx,&f); + if (((const unsigned char*)f.pixels)[0]!=pixel_value) return RCORE_ERR_STATE; + return RCORE_OK; + } + memset(out.pixels,(int)pixel_value,(size_t)out.stride*out.height); if (transitions && g_frames==3) { ++g_cpu_abandoned; return RCORE_OK; /* next run must acquire again, not publish this */ @@ -359,9 +378,10 @@ static rcore_result run_frame(void) { f.width=out.width; f.height=out.height; f.stride=out.stride; f.pixels=out.pixels; } if (atoi(cpu_mode)==3) f.stride+=4; /* mismatched loan must not self-copy */ - g_cpu_last_value=(unsigned)(g_frames&255); + g_cpu_last_value=pixel_value; g_cpu_last_width=f.width; g_cpu_last_height=f.height; g_host->video_submit(g_host->host_ctx,&f); + g_cpu_last_pointer=f.pixels; if (f.pixels==out.pixels && ((const unsigned char*)f.pixels)[0]!=g_cpu_last_value) return RCORE_ERR_STATE; /* committed image remains readable */ return RCORE_OK; @@ -404,6 +424,8 @@ static void unload(void) { snprintf(line,sizeof line,"FAKE_CPU_TARGET acquired=%u previous=%u abandoned=%u", g_cpu_acquired,g_cpu_previous,g_cpu_abandoned); g_host->log(g_host->host_ctx,RCORE_LOG_INFO,line); + snprintf(line,sizeof line,"FAKE_CPU_REPEAT repeats=%u",g_cpu_repeats); + g_host->log(g_host->host_ctx,RCORE_LOG_INFO,line); } } From a77b0e6317487b44fcb4460f50058be23f6d05e7 Mon Sep 17 00:00:00 2001 From: Matthew Stanley <1379tech@gmail.com> Date: Sun, 15 Nov 2026 08:15:44 -0800 Subject: [PATCH 5/6] feat(video): negotiate compact horizontal RGBA pixel pairs --- CMakeLists.txt | 6 ++++ corelink/core_link.cpp | 2 ++ corelink/core_link.hpp | 2 ++ corelink/link_protocol.hpp | 5 +++- corelink/link_test_main.cpp | 24 +++++++++++---- include/rcore/rcore.h | 7 ++++- runner/host_session.cpp | 5 +++- runner/runner_link.cpp | 34 +++++++++++++++------ tests/cpu_pixel_pairs_test.cmake | 44 ++++++++++++++++++++++++++++ tests/gpu_host_test.cpp | 3 ++ tests/presentation_protocol_test.cpp | 6 ++++ tests/rcore_fake_core.c | 40 +++++++++++++++++++++++-- 12 files changed, 159 insertions(+), 19 deletions(-) create mode 100644 tests/cpu_pixel_pairs_test.cmake diff --git a/CMakeLists.txt b/CMakeLists.txt index da291f5..c826174 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -154,6 +154,12 @@ if(RETRO_RUNTIME_TOOLS) -DOUT=${CMAKE_CURRENT_BINARY_DIR}/cpu-repeat-link -P ${CMAKE_CURRENT_SOURCE_DIR}/tests/cpu_repeat_test.cmake) set_tests_properties(cpu_repeat_link PROPERTIES ENVIRONMENT "FAKE_CORE_CPU_TARGET=4") + add_test(NAME cpu_pixel_pairs_link COMMAND ${CMAKE_COMMAND} + -DRUNNER=$ -DCORE=$ + -DLINK_TEST=$ -DROM=${CMAKE_CURRENT_SOURCE_DIR}/LICENSE + -DOUT=${CMAKE_CURRENT_BINARY_DIR}/cpu-pixel-pairs-link + -P ${CMAKE_CURRENT_SOURCE_DIR}/tests/cpu_pixel_pairs_test.cmake) + set_tests_properties(cpu_pixel_pairs_link PROPERTIES ENVIRONMENT "FAKE_CORE_CPU_TARGET=5") add_test(NAME route_watch COMMAND ${CMAKE_COMMAND} -DRUNNER=$ -DCORE=$ -DLINK=$ -DOUT=${CMAKE_CURRENT_BINARY_DIR}/route-watch diff --git a/corelink/core_link.cpp b/corelink/core_link.cpp index b1d6abd..9304905 100644 --- a/corelink/core_link.cpp +++ b/corelink/core_link.cpp @@ -308,6 +308,8 @@ bool CoreLink::grant(const rcore_pad pads[RCORE_MAX_SEATS], std::uint32_t width, g.presentation_height = height; } if (!video_requested) g.flags |= kGrantSkipVideo; + if (spec_.cpu_pixel_pairs && link_has_cpu_pixel_pairs(identity_.protocol_minor)) + g.flags |= kGrantCpuPixelPairs; const auto bytes = link_has_video_demand(identity_.protocol_minor) ? sizeof(g) : link_has_presentation_size(identity_.protocol_minor) ? kGrantSize25 : kGrantSize22; if (!send_msg_sized(channel_, g, bytes)) return false; diff --git a/corelink/core_link.hpp b/corelink/core_link.hpp index f90b091..8b1937c 100644 --- a/corelink/core_link.hpp +++ b/corelink/core_link.hpp @@ -33,6 +33,8 @@ struct LaunchSpec { fs::path session_dir; // runner.log, core.log, events.tsv, the core's cache bool gl = true; bool strict = false; + // Actual frontend can reconstruct exact horizontal pairs before filtering. + bool cpu_pixel_pairs = false; rcore_gpu_capabilities gpu_capabilities{}; // actual local import support, sampled once std::map options; std::optional load_state; diff --git a/corelink/link_protocol.hpp b/corelink/link_protocol.hpp index e4dcbb7..348630d 100644 --- a/corelink/link_protocol.hpp +++ b/corelink/link_protocol.hpp @@ -47,7 +47,9 @@ constexpr std::uint32_t kProtocolMajor = 2; // 2.4: optional core-owned GPU rings, native handle transfer and release events. // 2.5: between-frame replay recording controls and authoritative status. // 2.6: Grant appends presentation-only permission to omit unobserved video. -constexpr std::uint32_t kProtocolMinor = 6; +// 2.7: optional Grant flag enables exact RGBA8 horizontal-pair reconstruction. +constexpr std::uint32_t kProtocolMinor = 7; +constexpr bool link_has_cpu_pixel_pairs(std::uint32_t peer_minor) { return peer_minor >= 7; } constexpr bool link_has_video_demand(std::uint32_t peer_minor) { return peer_minor >= 6; } constexpr bool link_has_replays(std::uint32_t peer_minor) { return peer_minor >= 5; } constexpr bool link_has_gpu_frames(std::uint32_t peer_minor) { @@ -306,6 +308,7 @@ struct GrantMsg { constexpr std::size_t kGrantSize22 = 240; constexpr std::size_t kGrantSize25 = 248; constexpr std::uint32_t kGrantSkipVideo = 1; +constexpr std::uint32_t kGrantCpuPixelPairs = 2; // optional logical-width2 RGBA8 reconstruction struct EmptyMsg { MsgHeader h; diff --git a/corelink/link_test_main.cpp b/corelink/link_test_main.cpp index bcc03ba..c7eceb3 100644 --- a/corelink/link_test_main.cpp +++ b/corelink/link_test_main.cpp @@ -97,7 +97,7 @@ int main(int argc, char** argv) { std::uint64_t pcm = 0xcbf29ce484222325ull, audio_frames = 0; bool seat0 = true; bool presentation_test = false; - bool unpaced = false, cpu_repeat_test = false; + bool unpaced = false, cpu_repeat_test = false, cpu_pairs_test = false; bool expect_route_end = false; std::string restart_route; std::vector> script; @@ -146,8 +146,10 @@ int main(int argc, char** argv) { else if (a == "--frames") frames = std::strtoull(val().c_str(), nullptr, 10); else if (a == "--video-every") video_every = std::strtoull(val().c_str(), nullptr, 10); else if (a == "--pcm-hash") pcm_hash = true; + else if (a == "--cpu-pixel-pairs") spec.cpu_pixel_pairs = true; else if (a == "--unpaced") { unpaced = true; spec.extra_args.push_back("--unpaced"); } else if (a == "--cpu-repeat-test") cpu_repeat_test = true; + else if (a == "--cpu-pixel-pairs-test") cpu_pairs_test = true; else if (a == "--load-state") spec.load_state = utf8_path(val()); else if (a.size() >= 11 && a.compare(0, 6, "--tpak") == 0 && a[6] >= '1' && a[6] <= '4') { const std::size_t seat = static_cast(a[6] - '1'); @@ -315,6 +317,17 @@ int main(int argc, char** argv) { for (std::size_t i = 0; i < std::size_t(f->stride) * f->height; ++i) if (pixels[i] != (k + 1) / 2) die("CPU repeat changed per-field pixels"); } + if (cpu_pairs_test) { + const auto* f=link.frame_info(); const auto* px=link.frame_pixels(); + if(!f || !px || f->height!=48 || link.frame_stats().game_frame!=k/2) + die("CPU pixel pairs lost progress/geometry"); + const auto repeat=f->pixel_format==RCORE_PIXEL_RGBA8_X2 ? 2u : 1u; + if(f->width*repeat!=64 || f->stride!=f->width*4) + die("CPU pixel pairs changed logical geometry"); + for(unsigned y=0;y<48;++y) for(unsigned x=0;x<64;++x) for(unsigned c=0;c<4;++c) + if(px[y*f->stride+(x/repeat)*4+c]!=static_cast((k+1)/2+x/2+3*y+c)) + die("CPU pixel pairs changed a logical pixel"); + } std::int16_t sink[4096]; while (const auto n = link.drain_audio(sink, 2048)) { audio_frames += n; @@ -375,11 +388,12 @@ int main(int argc, char** argv) { // ---- the artifacts headless mode writes ---------------------------------- if (const FrameInfo* f = link.frame_info()) { std::ofstream shot(spec.session_dir / "shot.ppm", std::ios::binary); - shot << "P6\n" << f->width << ' ' << f->height << "\n255\n"; + const std::uint32_t repeat = f->pixel_format == RCORE_PIXEL_RGBA8_X2 ? 2 : 1; + shot << "P6\n" << f->width * repeat << ' ' << f->height << "\n255\n"; const std::uint8_t* px = link.frame_pixels(); - for (std::size_t i = 0; i < std::size_t(f->width) * f->height; ++i) { - shot.write(reinterpret_cast(px + i * 4), 3); - } + for (std::uint32_t y = 0; y < f->height; ++y) + for (std::uint32_t x = 0; x < f->width * repeat; ++x) + shot.write(reinterpret_cast(px + std::size_t(y) * f->stride + (x / repeat) * 4), 3); } { std::ifstream log(spec.session_dir / "core.log"); diff --git a/include/rcore/rcore.h b/include/rcore/rcore.h index 7e2bee3..3e28a4e 100644 --- a/include/rcore/rcore.h +++ b/include/rcore/rcore.h @@ -48,7 +48,7 @@ extern "C" { #define RCORE_ABI_MAJOR 0u /* 0 = draft; the first implemented contract is 1 */ #define RCORE_ABI_MINOR 0u -#define RCORE_DRAFT_REVISION 14u /* draft-only counter; see docs/CORE_ABI.md */ +#define RCORE_DRAFT_REVISION 15u /* draft-only counter; see docs/CORE_ABI.md */ #if defined(_WIN32) # define RCORE_EXPORT __declspec(dllexport) @@ -117,6 +117,11 @@ typedef struct rcore_core_info { /* ------------------------------------------------------------------------ */ #define RCORE_PIXEL_RGBA8 1u /* bytes R,G,B,A in memory order */ +/* Rev15: stored RGBA8 pixels, each replicated exactly twice horizontally + * BEFORE display filtering. Logical width=2*width; stride describes stored + * bytes. Submit only after a successful acquire requesting this format; + * unsupported hosts refuse it so the core submits ordinary RGBA8 instead. */ +#define RCORE_PIXEL_RGBA8_X2 2u typedef struct rcore_frame { uint32_t struct_size; diff --git a/runner/host_session.cpp b/runner/host_session.cpp index ec49913..e8d803a 100644 --- a/runner/host_session.cpp +++ b/runner/host_session.cpp @@ -60,9 +60,12 @@ std::uint32_t HostSession::h_video_acquire_cpu(void* ctx, std::uint32_t width, std::uint32_t height, std::uint32_t format, rcore_cpu_video_target* out) { if (!out || out->struct_size < offsetof(rcore_cpu_video_target, flags) || !width || !height || - format != RCORE_PIXEL_RGBA8 || width > UINT32_MAX / 4 || + (format != RCORE_PIXEL_RGBA8 && format != RCORE_PIXEL_RGBA8_X2) || + (format == RCORE_PIXEL_RGBA8_X2 && width > UINT32_MAX / 2) || width > UINT32_MAX / 4 || std::size_t(height) > SIZE_MAX / (std::size_t(width) * 4)) return 0; auto* s = self(ctx); + // Replay observers retain the canonical full-width byte oracle. + if (format == RCORE_PIXEL_RGBA8_X2 && s->route_) return 0; // Route observers request pictures even when the host does not display. if (!s->video_requested_ && !s->route_) return 0; // The producer always gets our full local object; an older core lends diff --git a/runner/runner_link.cpp b/runner/runner_link.cpp index b513298..40b08f5 100644 --- a/runner/runner_link.cpp +++ b/runner/runner_link.cpp @@ -248,7 +248,9 @@ class LinkSink final : public Sink { bool acquire_cpu(std::uint32_t width, std::uint32_t height, std::uint32_t format, rcore_cpu_video_target& out) override { - if (cpu_loan_ || !width || !height || format != RCORE_PIXEL_RGBA8 || + if (cpu_loan_ || !width || !height || + (format != RCORE_PIXEL_RGBA8 && format != RCORE_PIXEL_RGBA8_X2) || + (format == RCORE_PIXEL_RGBA8_X2 && (!cpu_pixel_pairs || width > kMaxFrameWidth / 2)) || width > kMaxFrameWidth || height > kMaxFrameHeight) return false; out.width = width; out.height = height; out.stride = width * 4; out.pixel_format = format; @@ -262,7 +264,7 @@ class LinkSink final : public Sink { if (unpaced_ && out.struct_size >= offsetof(rcore_cpu_video_target, flags) + sizeof out.flags) out.flags = RCORE_CPU_VIDEO_REPEAT_PREVIOUS; } - cpu_loan_width_ = width; cpu_loan_height_ = height; + cpu_loan_width_ = width; cpu_loan_height_ = height; cpu_loan_format_ = format; cpu_loan_ = true; cpu_repeat_ = (out.flags & RCORE_CPU_VIDEO_REPEAT_PREVIOUS) != 0; return true; @@ -274,12 +276,15 @@ class LinkSink final : public Sink { const bool repeat = cpu_repeat_; cpu_loan_ = false; // any submission ends writable access cpu_repeat_ = false; - if (!f.pixels || !f.width || !f.height || f.pixel_format != RCORE_PIXEL_RGBA8 || + if (!f.pixels || !f.width || !f.height || + (f.pixel_format != RCORE_PIXEL_RGBA8 && f.pixel_format != RCORE_PIXEL_RGBA8_X2) || + (f.pixel_format == RCORE_PIXEL_RGBA8_X2 && (!cpu_pixel_pairs || !loan || + cpu_loan_format_ != f.pixel_format || f.width > kMaxFrameWidth / 2)) || f.width > kMaxFrameWidth || f.height > kMaxFrameHeight || f.stride < f.width * 4) { ++bad_frames; // reported at the end of the frame, not mid-core return; } - if (loan && repeat && !last_gpu_ && published_ >= 0) { + if (loan && repeat && f.pixel_format == cpu_loan_format_ && !last_gpu_ && published_ >= 0) { const FrameInfo& previous = shm_->slot[published_]; const auto* pixels = base_ + shm_->frames_offset + std::size_t(published_) * kFrameSlotBytes; if (f.pixels == pixels && f.width == previous.width && f.height == previous.height && @@ -296,7 +301,7 @@ class LinkSink final : public Sink { const auto* src = static_cast(f.pixels); const std::size_t row = std::size_t(f.width) * 4; if (src == dst && loan && f.width == cpu_loan_width_ && f.height == cpu_loan_height_ && - f.stride == row) { + f.stride == row && f.pixel_format == cpu_loan_format_) { // The core filled our exclusively owned back slot. The metadata // and atomic ownership exchange below are the publication. } else { @@ -336,9 +341,17 @@ class LinkSink final : public Sink { if (last_gpu_) return {}; // a CPU slot from an earlier field is stale if (published_ < 0) return {}; const FrameInfo& info = shm_->slot[published_]; - return state::make_thumbnail(base_ + shm_->frames_offset + - std::size_t(published_) * kFrameSlotBytes, - info.width, info.height, info.stride); + const auto* pixels = base_ + shm_->frames_offset + std::size_t(published_) * kFrameSlotBytes; + if (info.pixel_format == RCORE_PIXEL_RGBA8_X2) { + const auto width = info.width * 2; + std::vector expanded(std::size_t(width) * info.height * 4); + for (std::uint32_t y = 0; y < info.height; ++y) + for (std::uint32_t x = 0; x < width; ++x) + std::memcpy(expanded.data() + (std::size_t(y) * width + x) * 4, + pixels + std::size_t(y) * info.stride + (x / 2) * 4, 4); + return state::make_thumbnail(expanded.data(), width, info.height, width * 4); + } + return state::make_thumbnail(pixels, info.width, info.height, info.stride); } void take_thumbnail(const std::vector& packet) { @@ -396,6 +409,7 @@ class LinkSink final : public Sink { std::uint64_t last_game_frame = 0; std::uint32_t faults = 0; std::uint32_t bad_frames = 0; + bool cpu_pixel_pairs = false; // The core's stated frame rate (set_frame_rate), 0/0 until it states one. std::uint32_t rate_num = 0, rate_den = 0; @@ -423,7 +437,7 @@ class LinkSink final : public Sink { std::uint32_t back_ = 2; // the triple buffer's initial back slot bool cpu_loan_ = false; bool cpu_repeat_ = false; - std::uint32_t cpu_loan_width_ = 0, cpu_loan_height_ = 0; + std::uint32_t cpu_loan_width_ = 0, cpu_loan_height_ = 0, cpu_loan_format_ = 0; const bool unpaced_; int published_ = -1; // the slot last swapped into middle std::ofstream log_, events_; @@ -809,6 +823,8 @@ int run_link_mode(const LoadedCore& core, const CoreManifest& manifest, const Li sink.frame_number = g.frame_number; session.set_video_requested(!link_has_video_demand(session_minor) || buf.size() < sizeof g || !(g.flags & kGrantSkipVideo)); + sink.cpu_pixel_pairs = link_has_cpu_pixel_pairs(session_minor) && buf.size() >= sizeof g + && (g.flags & kGrantCpuPixelPairs); session.set_presentation_size(g.presentation_width, g.presentation_height); // Everything that arrived before this grant is the frame's accessory // sequence; a message that arrives while the frame runs waits for the diff --git a/tests/cpu_pixel_pairs_test.cmake b/tests/cpu_pixel_pairs_test.cmake new file mode 100644 index 0000000..d34db5b --- /dev/null +++ b/tests/cpu_pixel_pairs_test.cmake @@ -0,0 +1,44 @@ +# Actual linked runner: compact/repeat, unadvertised fallback, and recorder fallback. +foreach(mode compact fallback record) + set(args --cpu-pixel-pairs-test --unpaced --pcm-hash) + set(envargs) + if(NOT mode STREQUAL fallback) + list(APPEND args --cpu-pixel-pairs) + endif() + if(mode STREQUAL record) + list(APPEND envargs "RETRO_RECORD_ROUTE=${OUT}/pairs.route") + endif() + execute_process(COMMAND "${CMAKE_COMMAND}" -E env ${envargs} + "${LINK_TEST}" --runner "${RUNNER}" --core "${CORE}" --rom "${ROM}" + --out "${OUT}/${mode}" --frames 8 ${args} + RESULT_VARIABLE rc OUTPUT_VARIABLE output ERROR_VARIABLE errors) + if(NOT rc EQUAL 0 OR NOT output MATCHES "8 frame.s. granted and done, runner exit 0, 0 fault.s.") + message(FATAL_ERROR "${mode} pixel-pair fields failed: ${rc}\n${output}\n${errors}") + endif() + file(READ "${OUT}/${mode}/core.log" log) + if(mode STREQUAL compact) + if(NOT log MATCHES "FAKE_CPU_REPEAT repeats=3" OR NOT output MATCHES "pictures received: 5") + message(FATAL_ERROR "compact path/repeat not selected: ${output} ${log}") + endif() + elseif(NOT log MATCHES "FAKE_CPU_REPEAT repeats=0" OR NOT output MATCHES "pictures received: 8") + message(FATAL_ERROR "${mode} fallback did not publish ordinary images: ${output} ${log}") + endif() + file(SHA256 "${OUT}/${mode}/shot.ppm" shot) + string(REGEX MATCH "PCM_HASH [0-9a-f]+ over 32 samples" pcm "${output}") + if(mode STREQUAL compact) + set(reference_shot "${shot}") + set(reference_pcm "${pcm}") + elseif(NOT shot STREQUAL reference_shot OR NOT pcm STREQUAL reference_pcm) + message(FATAL_ERROR "${mode} changed final picture/audio") + endif() +endforeach() +execute_process(COMMAND "${RUNNER}" --core "${CORE}" --rom "${ROM}" + --play-route "${OUT}/pairs.route" --out "${OUT}/play" + RESULT_VARIABLE rc OUTPUT_VARIABLE output ERROR_VARIABLE errors) +if(NOT rc EQUAL 0) + message(FATAL_ERROR "compact recorder fallback replay failed: ${rc} ${output} ${errors}") +endif() +file(READ "${OUT}/play/route-verdict.json" verdict) +if(NOT verdict MATCHES "in_sync") + message(FATAL_ERROR "compact recorder fallback replay diverged: ${verdict}") +endif() diff --git a/tests/gpu_host_test.cpp b/tests/gpu_host_test.cpp index f2d8f31..cf4f20f 100644 --- a/tests/gpu_host_test.cpp +++ b/tests/gpu_host_test.cpp @@ -82,6 +82,9 @@ int main() { check(ch->video_acquire_cpu(ch->host_ctx,64,48,RCORE_PIXEL_RGBA8,&target) && target.struct_size==sizeof target && target.flags==RCORE_CPU_VIDEO_REPEAT_PREVIOUS, "new64-byte target receives appended flags"); + check(ch->video_acquire_cpu(ch->host_ctx,32,48,RCORE_PIXEL_RGBA8_X2,&target) && + target.pixel_format==RCORE_PIXEL_RGBA8_X2 && target.width==32, + "explicit compact format reaches a supporting sink without layout changes"); cpu.offer_gpu_frames(); absent.available = true; cpu.offer_gpu_frames(); diff --git a/tests/presentation_protocol_test.cpp b/tests/presentation_protocol_test.cpp index 3732ca6..57de71b 100644 --- a/tests/presentation_protocol_test.cpp +++ b/tests/presentation_protocol_test.cpp @@ -23,5 +23,11 @@ int main() { if (!as_msg(bytes, got, kGrantSize22) || got.flags || got.presentation_width != 2560) return 7; bytes.resize(kGrantSize22); if (!as_msg(bytes, got, kGrantSize22) || got.flags || got.presentation_width) return 8; + if(link_has_cpu_pixel_pairs(6) || !link_has_cpu_pixel_pairs(7)) return 9; + sent.flags=kGrantSkipVideo|kGrantCpuPixelPairs; + bytes.resize(sizeof sent); std::memcpy(bytes.data(),&sent,sizeof sent); + if(!as_msg(bytes,got,kGrantSize22) || !(got.flags&kGrantCpuPixelPairs) || !(got.flags&kGrantSkipVideo)) return 10; + bytes.resize(kGrantSize25); + if(!as_msg(bytes,got,kGrantSize22) || got.flags) return 11; return 0; } diff --git a/tests/rcore_fake_core.c b/tests/rcore_fake_core.c index e33cb6a..e65a5c7 100644 --- a/tests/rcore_fake_core.c +++ b/tests/rcore_fake_core.c @@ -289,7 +289,8 @@ static void die_now(void) { static rcore_result run_frame(void) { ++g_frames; const char* cpu_mode = getenv("FAKE_CORE_CPU_TARGET"); - const int repeat_mode = cpu_mode && atoi(cpu_mode)==4; + const int pairs_mode = cpu_mode && atoi(cpu_mode)==5; + const int repeat_mode = cpu_mode && (atoi(cpu_mode)==4 || pairs_mode); const unsigned pixel_value = (unsigned)((repeat_mode ? (g_frames+1)/2 : g_frames)&255); if (repeat_mode) { int16_t pcm[4]={(int16_t)g_frames,-(int16_t)g_frames,(int16_t)(g_frames*3),-(int16_t)(g_frames*3)}; @@ -333,10 +334,45 @@ static rcore_result run_frame(void) { f.pixels = g_pixels; f.frame_number = g_frames; f.game_frame = g_frames / 2; /* rev 8: a game drawing every other frame */ + if (pairs_mode) { + unsigned x,y,c; + for(y=0;ystruct_size >= offsetof(rcore_host_api,video_acquire_cpu)+sizeof(g_host->video_acquire_cpu) && + g_host->video_acquire_cpu) { + rcore_cpu_video_target target={0}; target.struct_size=sizeof target; + if(g_host->video_acquire_cpu(g_host->host_ctx,W/2,H,RCORE_PIXEL_RGBA8_X2,&target)) { + if(!target.pixels || target.width!=W/2 || target.height!=H || target.stride!=W*2 || + target.pixel_format!=RCORE_PIXEL_RGBA8_X2) return RCORE_ERR_STATE; + ++g_cpu_acquired; + if(g_frames==3) { + /* Abandon a valid compact loan for an ordinary private + * frame: models full history that cannot be paired. */ + g_cpu_last_value=pixel_value; + g_host->video_submit(g_host->host_ctx,&f); + return RCORE_OK; + } + f.width=W/2; f.stride=W*2; f.pixel_format=RCORE_PIXEL_RGBA8_X2; + if(target.previous_pixels && target.previous_width==W/2 && target.previous_height==H && + target.previous_pixel_format==RCORE_PIXEL_RGBA8_X2 && + (target.flags&RCORE_CPU_VIDEO_REPEAT_PREVIOUS) && g_cpu_last_value==pixel_value) { + f.pixels=target.previous_pixels; ++g_cpu_repeats; + } else { + for(y=0;yvideo_submit(g_host->host_ctx,&f); + return RCORE_OK; + } + } + /* Unsupported/recording hosts receive the canonical full-width image. */ + } /* Exercise the actual linked runner's owned back slot. Mode1 includes an * abandoned field, resize, then an ordinary private-buffer submission; * mode2 keeps ordinary pictures for the existing route/recording oracle. */ - if (cpu_mode && atoi(cpu_mode) && + if (cpu_mode && atoi(cpu_mode) && !pairs_mode && g_host->struct_size >= offsetof(rcore_host_api,video_acquire_cpu)+sizeof(g_host->video_acquire_cpu) && g_host->video_acquire_cpu) { const int transitions = atoi(cpu_mode)==1; From a29a34b29466a6476e168a90464d40eaf79992a0 Mon Sep 17 00:00:00 2001 From: Matthew Stanley <1379tech@gmail.com> Date: Sun, 15 Nov 2026 10:26:52 -0800 Subject: [PATCH 6/6] ci: use supported macOS 15 hosted runners --- .github/workflows/ci.yml | 2 +- .github/workflows/release.yml | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 75be696..8ac8087 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -23,7 +23,7 @@ jobs: run: ctest --test-dir build --output-on-failure macos: - runs-on: macos-14 + runs-on: macos-15 steps: - uses: actions/checkout@v4 - name: Toolchain diff --git a/.github/workflows/release.yml b/.github/workflows/release.yml index 5a4d2b7..466fdcf 100644 --- a/.github/workflows/release.yml +++ b/.github/workflows/release.yml @@ -106,7 +106,7 @@ jobs: runner: ubuntu-22.04-arm # One universal binary; its x86_64 slice is run under Rosetta. - platform: macos-universal - runner: macos-14 + runner: macos-15 - platform: windows-x86_64 runner: windows-2022 runs-on: ${{ matrix.runner }}