Move Win32 async context ownership and trim canvas draw setup

This commit is contained in:
2026-06-16 08:49:31 +02:00
parent d5b137c9ff
commit 667589f1f6
7 changed files with 181 additions and 93 deletions

View File

@@ -38,21 +38,22 @@
#define WM_USER_WAKEUP (WM_USER + 2)
LRESULT CALLBACK WndProc(HWND hWnd, UINT msg, WPARAM wp, LPARAM lp);
namespace pp::platform::windows {
void set_async_render_context(HDC hdc, HGLRC hrc);
void lock_async_render_context();
bool try_lock_async_render_context();
void unlock_async_render_context();
void swap_async_render_context();
}
struct RetainedState
{
HINSTANCE hInst{};
HWND hWnd{};
HDC hDC{};
HGLRC hRC{};
const wchar_t* className{};
bool keys[256]{};
std::mutex gl_mutex;
std::mutex async_mutex;
std::thread::id gl_thread{};
std::map<kKey, int> vkey_map;
wchar_t window_title[512]{};
std::jthread hmd_renderer;
int gl_count = 0;
std::deque<std::packaged_task<void()>> main_tasklist;
std::mutex main_task_mutex;
float timer_stylus = 0;
@@ -170,57 +171,22 @@ void destroy_window()
void async_lock()
{
//std::lock_guard<std::mutex> _lock(async_mutex);
auto& state = retained_state();
if (state.gl_count == 0 || state.gl_thread != std::this_thread::get_id())
{
state.gl_mutex.lock();
bool ret = wglMakeCurrent(state.hDC, state.hRC);
if (ret == false)
LOG("FAILED wglMakeCurrent: %s", GetLastErrorAsString().c_str());
state.gl_thread = std::this_thread::get_id();
//LOG("lock");
}
state.gl_count++;
//assert(ret == true);
pp::platform::windows::lock_async_render_context();
}
bool async_lock_try()
{
//std::lock_guard<std::mutex> _lock(async_mutex);
auto& state = retained_state();
if (state.gl_count == 0 || state.gl_thread != std::this_thread::get_id())
{
if (!state.gl_mutex.try_lock())
return false;
bool ret = wglMakeCurrent(state.hDC, state.hRC);
if (ret == false)
LOG("FAILED wglMakeCurrent: %s", GetLastErrorAsString().c_str());
state.gl_thread = std::this_thread::get_id();
//LOG("lock");
}
state.gl_count++;
//assert(ret == true);
return true;
return pp::platform::windows::try_lock_async_render_context();
}
void win32_async_swap()
{
SwapBuffers(retained_state().hDC);
//LOG("swap");
pp::platform::windows::swap_async_render_context();
}
void async_unlock()
{
//std::lock_guard<std::mutex> _lock(async_mutex);
auto& state = retained_state();
state.gl_count--;
if (state.gl_count == 0)
{
//LOG("unlock");
wglMakeCurrent(0, 0);
state.gl_mutex.unlock();
}
pp::platform::windows::unlock_async_render_context();
}
void win32_update_stylus(float dt)
@@ -866,11 +832,12 @@ int main(int argc, char** argv)
pfd.cDepthBits = 24;
pfd.iLayerType = PFD_MAIN_PLANE;
state.hDC = GetDC(state.hWnd);
int pxfmt = ChoosePixelFormat(state.hDC, &pfd);
SetPixelFormat(state.hDC, pxfmt, &pfd);
state.hRC = wglCreateContext(state.hDC); // Create OpenGL 2.1 or less
wglMakeCurrent(state.hDC, state.hRC);
HDC hDC = GetDC(state.hWnd);
int pxfmt = ChoosePixelFormat(hDC, &pfd);
SetPixelFormat(hDC, pxfmt, &pfd);
HGLRC hRC = wglCreateContext(hDC); // Create OpenGL 2.1 or less
wglMakeCurrent(hDC, hRC);
pp::platform::windows::set_async_render_context(hDC, hRC);
// Initialize extensions
if (!gladLoadGL())
@@ -878,7 +845,7 @@ int main(int argc, char** argv)
LOG("gladLoadGL() failed");
return 0;
}
if (!gladLoadWGL(state.hDC))
if (!gladLoadWGL(hDC))
{
LOG("gladLoadWGL() failed");
return 0;
@@ -912,18 +879,19 @@ int main(int argc, char** argv)
UINT numFormat;
wglMakeCurrent(NULL, NULL);
wglDeleteContext(state.hRC);
wglDeleteContext(hRC);
DestroyWindow(state.hWnd);
state.hWnd = CreateWindow(wc.lpszClassName, state.window_title, wnd_style, clientPos.x, clientPos.y,
(float)(clientRect.right - clientRect.left),
(float)(clientRect.bottom - clientRect.top), 0, 0, state.hInst, 0);
state.hDC = GetDC(state.hWnd);
wglChoosePixelFormatARB(state.hDC, wgl_config.pixel_format_attributes.data(), nullptr, 1, &pxfmt, &numFormat);
SetPixelFormat(state.hDC, pxfmt, &pfd);
state.hRC = wglCreateContextAttribsARB(state.hDC, NULL, wgl_config.context_attributes.data());
wglMakeCurrent(state.hDC, state.hRC);
hDC = GetDC(state.hWnd);
wglChoosePixelFormatARB(hDC, wgl_config.pixel_format_attributes.data(), nullptr, 1, &pxfmt, &numFormat);
SetPixelFormat(hDC, pxfmt, &pfd);
hRC = wglCreateContextAttribsARB(hDC, NULL, wgl_config.context_attributes.data());
wglMakeCurrent(hDC, hRC);
pp::platform::windows::set_async_render_context(hDC, hRC);
}
else
{