Extract dialog, VR, and canvas draw helpers

This commit is contained in:
2026-06-16 11:40:00 +02:00
parent 18665bdffc
commit d2a841f348
9 changed files with 566 additions and 377 deletions

View File

@@ -7,20 +7,18 @@
#include "app.h"
#include "canvas.h"
#include "keymap.h"
#include "hmd.h"
#include "legacy_gl_runtime_dispatch.h"
#include "legacy_preference_storage.h"
#include "renderer_gl/opengl_capabilities.h"
#include "platform_windows/windows_platform_services.h"
#include "platform_windows/windows_splash.h"
#include "platform_windows/windows_stylus_input.h"
#include "platform_windows/windows_vr_shell.h"
#include "../resource.h"
#include <shellscalingapi.h>
#include <WbemCli.h>
#include <deque>
#include <chrono>
#include "wacom.h"
#include "abr.h"
@@ -32,9 +30,7 @@
#include <iomanip>
#include <ctime>
#include <sstream>
#include <memory>
#include <atomic>
#include <stop_token>
#define WM_USER_CLOSE (WM_USER + 1)
#define WM_USER_WAKEUP (WM_USER + 2)
@@ -55,11 +51,8 @@ struct RetainedState
bool keys[256]{};
std::map<kKey, int> vkey_map;
wchar_t window_title[512]{};
std::jthread hmd_renderer;
bool sandboxed = false;
std::mutex hmd_render_mutex;
std::condition_variable hmd_render_cv;
std::unique_ptr<Vive> vive;
pp::platform::windows::VrShellState vr;
};
RetainedState& retained_state()
@@ -125,9 +118,7 @@ void pp_windows_enqueue_main_task(std::packaged_task<void()> task)
enqueue_main_task_bridge(std::move(task));
}
std::atomic<int> vr_frames{0};
std::atomic<int> running{-1};
std::atomic_bool vr_running{false};
#ifdef USE_RENDERDOC
RENDERDOC_API_1_4_0* rdoc_api = NULL;
@@ -230,8 +221,8 @@ void win32_update_stylus(float dt)
void win32_update_fps(int frames)
{
static wchar_t title_fps[512];
const int vr_fps = vr_frames.load(std::memory_order_relaxed);
auto& state = retained_state();
const int vr_fps = pp::platform::windows::current_vr_fps(state.vr);
if (App::I->vr_active)
swprintf_s(title_fps, L"%s - %d fps - %d vr fps", state.window_title, frames, vr_fps);
else
@@ -520,101 +511,13 @@ void init_vk_map()
bool win32_vr_start()
{
auto& state = retained_state();
if (state.sandboxed)
return false;
state.vive = std::make_unique<Vive>();
state.vive->on_draw = [](const glm::mat4& proj, const glm::mat4& view, const glm::mat4& pose) { App::I->vr_draw(proj, view, pose); };
if (!state.vive->Initialize())
{
state.vive.reset();
LOG("VR: failed to initialize vive");
return false;
}
if (state.hmd_renderer.joinable())
{
state.hmd_renderer.request_stop();
state.hmd_renderer.join();
}
state.hmd_renderer = std::jthread([&state](std::stop_token stop_token) {
if (!state.vive)
return;
BT_SetTerminate();
LOG("start hmd render thread");
App::I->has_vr = true;
vr_running.store(true, std::memory_order_relaxed);
state.vive->on_analog_button = std::bind(&App::vr_analog, App::I, std::placeholders::_1,
std::placeholders::_2, std::placeholders::_3, std::placeholders::_4);
state.vive->on_button = std::bind(&App::vr_digital, App::I, std::placeholders::_1,
std::placeholders::_2, std::placeholders::_3, std::placeholders::_4);
App::I->render_task([] {
App::I->vr_draw_ui();
});
const float target_tick_rate = 90;
auto t0 = GetTickCount64();
float one_sec_timer = 0;
int frames = 0;
while (!stop_token.stop_requested() && vr_running.load(std::memory_order_relaxed) && running.load(std::memory_order_relaxed) == 1 && state.vive->Valid())
{
std::unique_lock<std::mutex> lock(state.hmd_render_mutex);
auto t1 = GetTickCount64();
float dt = (float)(t1 - t0) / 1000.0f;
one_sec_timer += dt;
if (one_sec_timer >= 1.f)
{
one_sec_timer = 0;
vr_frames.store(frames, std::memory_order_relaxed);
frames = 0;
}
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;
App::I->vr_update(dt);
if (vr_running.load(std::memory_order_relaxed) && state.vive->m_active)
{
App::I->render_task([&state] {
state.vive->Draw();
});
}
const int framerate = (1.f / target_tick_rate) * 1000;
const int diff = framerate - (t1 - t0);
//state.hmd_render_cv.wait_for(lock, std::chrono::milliseconds(diff));
t0 = t1;
}
App::I->vr_active = false;
App::I->has_vr = false;
vr_running.store(false, std::memory_order_relaxed);
state.vive->Terminate();
LOG("hmd renderer terminated");
});
return true;
return pp::platform::windows::start_vr_shell(state.vr, state.sandboxed, running);
}
void win32_vr_stop()
{
auto& state = retained_state();
if (state.vive)
{
vr_running.store(false, std::memory_order_relaxed);
if (state.hmd_renderer.joinable())
{
state.hmd_renderer.request_stop();
state.hmd_renderer.join();
}
state.vive->Terminate();
state.vive.reset();
}
pp::platform::windows::stop_vr_shell(state.vr);
}
void win32_save_window_state()
@@ -947,11 +850,7 @@ LRESULT CALLBACK WndProc(HWND hWnd, UINT msg, WPARAM wp, LPARAM lp)
{
case WM_USER_CLOSE:
running.store(0, std::memory_order_relaxed);
if (state.hmd_renderer.joinable())
{
state.hmd_renderer.request_stop();
state.hmd_renderer.join();
}
pp::platform::windows::request_stop_and_join_vr_thread(state.vr);
App::I->runtime().ui_thread_stop();
App::I->runtime().render_thread_stop();
App::I->terminate();