Move Win32 async context ownership and trim canvas draw setup
This commit is contained in:
@@ -494,6 +494,45 @@ inline void execute_legacy_canvas_draw_merge_layer_texture(
|
||||
execution.unbind_layer_texture();
|
||||
}
|
||||
|
||||
template <typename LayerMergeT, typename SamplerT, typename FacePlaneT, typename SetActiveTextureUnit, typename PlaneTransform>
|
||||
[[nodiscard]] inline auto make_legacy_canvas_draw_merge_layer_texture_draw(
|
||||
LayerMergeT* layer_merge,
|
||||
SamplerT* sampler,
|
||||
FacePlaneT* face_plane,
|
||||
SetActiveTextureUnit set_active_texture_unit,
|
||||
glm::mat4 proj,
|
||||
glm::mat4 camera,
|
||||
PlaneTransform plane_transform)
|
||||
{
|
||||
return [layer_merge, sampler, face_plane, set_active_texture_unit, proj, camera, plane_transform](int plane_index) {
|
||||
const auto mvp = proj * camera *
|
||||
plane_transform[plane_index] *
|
||||
glm::translate(glm::vec3(0, 0, -1));
|
||||
execute_legacy_canvas_draw_merge_layer_texture(
|
||||
{
|
||||
.mvp = mvp,
|
||||
.texture_slot = 0,
|
||||
.alpha = 1.f,
|
||||
.highlight = false,
|
||||
},
|
||||
{
|
||||
.bind_sampler = [sampler, set_active_texture_unit] {
|
||||
sampler->bind(0);
|
||||
set_active_texture_unit(0);
|
||||
},
|
||||
.bind_layer_texture = [layer_merge, plane_index] {
|
||||
layer_merge->rtt(plane_index).bindTexture();
|
||||
},
|
||||
.draw = [face_plane] {
|
||||
face_plane->draw_fill();
|
||||
},
|
||||
.unbind_layer_texture = [layer_merge, plane_index] {
|
||||
layer_merge->rtt(plane_index).unbindTexture();
|
||||
},
|
||||
});
|
||||
};
|
||||
}
|
||||
|
||||
inline void execute_legacy_canvas_draw_merge_layer_composite(
|
||||
bool is_temporary_erase,
|
||||
bool is_temporary_paint,
|
||||
|
||||
82
src/main.cpp
82
src/main.cpp
@@ -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
|
||||
{
|
||||
|
||||
@@ -385,32 +385,14 @@ void NodeCanvas::draw()
|
||||
}),
|
||||
});
|
||||
|
||||
const auto draw_merged_texture_plane = [&](int plane_index) {
|
||||
pp::panopainter::execute_legacy_canvas_draw_merge_layer_texture(
|
||||
{
|
||||
.mvp = proj * camera *
|
||||
m_canvas->m_plane_transform[plane_index] *
|
||||
glm::translate(glm::vec3(0, 0, -1)),
|
||||
.texture_slot = 0,
|
||||
.alpha = 1.f,
|
||||
.highlight = false,
|
||||
},
|
||||
{
|
||||
.bind_sampler = [&] {
|
||||
m_sampler.bind(0);
|
||||
set_active_texture_unit(0);
|
||||
},
|
||||
.bind_layer_texture = [&] {
|
||||
m_canvas->m_layers_merge.rtt(plane_index).bindTexture();
|
||||
},
|
||||
.draw = [&] {
|
||||
m_face_plane.draw_fill();
|
||||
},
|
||||
.unbind_layer_texture = [&] {
|
||||
m_canvas->m_layers_merge.rtt(plane_index).unbindTexture();
|
||||
},
|
||||
});
|
||||
};
|
||||
const auto draw_merged_texture_plane = pp::panopainter::make_legacy_canvas_draw_merge_layer_texture_draw(
|
||||
&m_canvas->m_layers_merge,
|
||||
&m_sampler,
|
||||
&m_face_plane,
|
||||
set_active_texture_unit,
|
||||
proj,
|
||||
camera,
|
||||
m_canvas->m_plane_transform);
|
||||
|
||||
for (int plane_index = 0; plane_index < 6; plane_index++)
|
||||
{
|
||||
|
||||
@@ -26,6 +26,84 @@ void win32_update_stylus(float dt);
|
||||
void win32_save_window_state();
|
||||
bool win32_vr_start();
|
||||
void win32_vr_stop();
|
||||
std::string GetLastErrorAsString();
|
||||
|
||||
namespace pp::platform::windows {
|
||||
|
||||
namespace {
|
||||
|
||||
struct RetainedWin32AsyncRenderContextState final {
|
||||
HDC hdc{};
|
||||
HGLRC hrc{};
|
||||
std::mutex gl_mutex;
|
||||
std::thread::id gl_thread{};
|
||||
int gl_count = 0;
|
||||
};
|
||||
|
||||
[[nodiscard]] RetainedWin32AsyncRenderContextState& retained_win32_async_render_context_state()
|
||||
{
|
||||
static RetainedWin32AsyncRenderContextState state;
|
||||
return state;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
void set_async_render_context(HDC hdc, HGLRC hrc)
|
||||
{
|
||||
auto& state = retained_win32_async_render_context_state();
|
||||
state.hdc = hdc;
|
||||
state.hrc = hrc;
|
||||
state.gl_thread = {};
|
||||
state.gl_count = 0;
|
||||
}
|
||||
|
||||
void lock_async_render_context()
|
||||
{
|
||||
auto& state = retained_win32_async_render_context_state();
|
||||
if (state.gl_count == 0 || state.gl_thread != std::this_thread::get_id())
|
||||
{
|
||||
state.gl_mutex.lock();
|
||||
const bool ret = wglMakeCurrent(state.hdc, state.hrc);
|
||||
if (!ret)
|
||||
LOG("FAILED wglMakeCurrent: %s", GetLastErrorAsString().c_str());
|
||||
state.gl_thread = std::this_thread::get_id();
|
||||
}
|
||||
state.gl_count++;
|
||||
}
|
||||
|
||||
bool try_lock_async_render_context()
|
||||
{
|
||||
auto& state = retained_win32_async_render_context_state();
|
||||
if (state.gl_count == 0 || state.gl_thread != std::this_thread::get_id())
|
||||
{
|
||||
if (!state.gl_mutex.try_lock())
|
||||
return false;
|
||||
const bool ret = wglMakeCurrent(state.hdc, state.hrc);
|
||||
if (!ret)
|
||||
LOG("FAILED wglMakeCurrent: %s", GetLastErrorAsString().c_str());
|
||||
state.gl_thread = std::this_thread::get_id();
|
||||
}
|
||||
state.gl_count++;
|
||||
return true;
|
||||
}
|
||||
|
||||
void unlock_async_render_context()
|
||||
{
|
||||
auto& state = retained_win32_async_render_context_state();
|
||||
state.gl_count--;
|
||||
if (state.gl_count == 0)
|
||||
{
|
||||
wglMakeCurrent(0, 0);
|
||||
state.gl_mutex.unlock();
|
||||
}
|
||||
}
|
||||
|
||||
void swap_async_render_context()
|
||||
{
|
||||
SwapBuffers(retained_win32_async_render_context_state().hdc);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
namespace {
|
||||
|
||||
|
||||
Reference in New Issue
Block a user