Split Apple platform target and move platform state ownership

This commit is contained in:
2026-06-17 01:03:01 +02:00
parent 5fdc9a9dd6
commit 90a55b86fe
18 changed files with 258 additions and 104 deletions

View File

@@ -63,7 +63,7 @@ void update_window_fps(HWND hWnd, const wchar_t* window_title, VrShellState& vr,
{
static wchar_t title_fps[512];
const int vr_fps = current_vr_fps(vr);
if (App::I->vr_active)
if (read_vr_session_snapshot(vr).vr_active)
swprintf_s(title_fps, L"%s - %d fps - %d vr fps", window_title, frames, vr_fps);
else
swprintf_s(title_fps, L"%s - %d fps", window_title, frames);

View File

@@ -793,4 +793,9 @@ PlatformServices& platform_services()
return services;
}
VrSessionSnapshot read_platform_vr_session_snapshot() noexcept
{
return read_vr_session_snapshot(retained_state().vr);
}
}

View File

@@ -4,9 +4,12 @@
#include <future>
struct VrSessionSnapshot;
namespace pp::platform::windows {
[[nodiscard]] PlatformServices& platform_services();
[[nodiscard]] VrSessionSnapshot read_platform_vr_session_snapshot() noexcept;
void enqueue_main_thread_task(std::packaged_task<void()> task);
void drain_main_thread_tasks();

View File

@@ -14,6 +14,8 @@
namespace pp::platform::windows {
struct VrShellState final {
std::mutex snapshot_mutex;
VrSessionSnapshot session;
std::jthread hmd_renderer;
std::mutex hmd_render_mutex;
std::condition_variable hmd_render_cv;
@@ -27,6 +29,35 @@ inline int current_vr_fps(const VrShellState& state)
return state.vr_frames.load(std::memory_order_relaxed);
}
inline VrSessionSnapshot read_vr_session_snapshot(VrShellState& state)
{
std::lock_guard<std::mutex> lock(state.snapshot_mutex);
return state.session;
}
inline void write_vr_session_snapshot(
VrShellState& state,
bool has_vr,
bool vr_active,
const VRController& primary_controller,
const glm::mat4& vr_head)
{
std::lock_guard<std::mutex> lock(state.snapshot_mutex);
state.session.has_vr = has_vr;
state.session.vr_active = vr_active;
state.session.vr_controllers[0] = primary_controller;
state.session.vr_head = vr_head;
}
inline void clear_vr_session_snapshot(VrShellState& state, bool has_vr)
{
std::lock_guard<std::mutex> lock(state.snapshot_mutex);
state.session.has_vr = has_vr;
state.session.vr_active = false;
state.session.vr_controllers[0] = {};
state.session.vr_head = glm::mat4(1.0f);
}
inline void request_stop_and_join_vr_thread(VrShellState& state)
{
if (state.hmd_renderer.joinable())
@@ -64,7 +95,7 @@ inline bool start_vr_shell(VrShellState& state, bool sandboxed, std::atomic<int>
BT_SetTerminate();
LOG("start hmd render thread");
App::I->has_vr = true;
clear_vr_session_snapshot(state, true);
state.vr_running.store(true, std::memory_order_relaxed);
state.vive->on_analog_button = std::bind(&App::vr_analog, App::I, std::placeholders::_1,
@@ -99,9 +130,12 @@ inline bool start_vr_shell(VrShellState& state, bool sandboxed, std::atomic<int>
frames++;
state.vive->Update();
App::I->vr_active = state.vive->m_active;
App::I->vr_controllers[0] = state.vive->m_controllers[0];
App::I->vr_head = state.vive->m_pose;
write_vr_session_snapshot(
state,
true,
state.vive->m_active,
state.vive->m_controllers[0],
state.vive->m_pose);
App::I->vr_update(dt);
if (state.vr_running.load(std::memory_order_relaxed) && state.vive->m_active)
@@ -118,8 +152,7 @@ inline bool start_vr_shell(VrShellState& state, bool sandboxed, std::atomic<int>
t0 = t1;
}
App::I->vr_active = false;
App::I->has_vr = false;
clear_vr_session_snapshot(state, false);
state.vr_running.store(false, std::memory_order_relaxed);
state.vive->Terminate();
LOG("hmd renderer terminated");