From 6ae129022e816cf49decbd6b22fab3d575f9e562 Mon Sep 17 00:00:00 2001 From: Reuben Dunnington Date: Fri, 8 Sep 2023 21:35:13 -0700 Subject: [PATCH] rework vsync to be manual wait for win32 --- src/app/osx_app.m | 54 +++++------ src/app/win32_app.c | 8 -- src/app/win32_app.h | 1 - src/graphics/graphics.h | 2 +- src/graphics/graphics_surface.c | 3 + src/graphics/win32_vsync.c | 163 +++++++++++++------------------- src/runtime.c | 17 +--- 7 files changed, 99 insertions(+), 149 deletions(-) diff --git a/src/app/osx_app.m b/src/app/osx_app.m index 6449498..145bf65 100644 --- a/src/app/osx_app.m +++ b/src/app/osx_app.m @@ -1132,8 +1132,6 @@ void oc_init() [NSApp run]; [NSApp setActivationPolicy:NSApplicationActivationPolicyRegular]; [NSApp activateIgnoringOtherApps:YES]; - - oc_vsync_init(); } } } @@ -2404,37 +2402,39 @@ void oc_vsync_init(void) { } -void oc_vsync_window(oc_window window) +void oc_vsync_wait(oc_window window) { - oc_window_data* windowData = oc_window_ptr_from_handle(window); - if(!windowData) - { - return; - } + // TODO figure out why this causes stuttering with triple buffering - OCWindow* ocWindow = (OCWindow*)windowData->osx.nsWindow; + // oc_window_data* windowData = oc_window_ptr_from_handle(window); + // if(!windowData) + // { + // return; + // } - CVReturn ret; + // OCWindow* ocWindow = (OCWindow*)windowData->osx.nsWindow; - if((ret = CVDisplayLinkCreateWithActiveCGDisplays(&ocWindow->displayLink)) != kCVReturnSuccess) - { - oc_log_error("CVDisplayLinkCreateWithActiveCGDisplays error: %d\n", ret); - } + // CVReturn ret; - CGDirectDisplayID mainDisplay = CGMainDisplayID(); + // if((ret = CVDisplayLinkCreateWithActiveCGDisplays(&ocWindow->displayLink)) != kCVReturnSuccess) + // { + // oc_log_error("CVDisplayLinkCreateWithActiveCGDisplays error: %d\n", ret); + // } - if((ret = CVDisplayLinkSetCurrentCGDisplay(ocWindow->displayLink, mainDisplay)) != kCVReturnSuccess) - { - oc_log_error("CVDisplayLinkSetCurrentCGDisplay ret: %d\n", ret); - } + // CGDirectDisplayID mainDisplay = CGMainDisplayID(); - if((ret = CVDisplayLinkSetOutputCallback(ocWindow->displayLink, oc_display_link_callback, ocWindow)) != kCVReturnSuccess) - { - oc_log_error("CVDisplayLinkSetOutputCallback ret: %d\n", ret); - } + // if((ret = CVDisplayLinkSetCurrentCGDisplay(ocWindow->displayLink, mainDisplay)) != kCVReturnSuccess) + // { + // oc_log_error("CVDisplayLinkSetCurrentCGDisplay ret: %d\n", ret); + // } - if((ret = CVDisplayLinkStart(ocWindow->displayLink)) != kCVReturnSuccess) - { - oc_log_error("CVDisplayLinkStart ret: %d\n", ret); - } + // if((ret = CVDisplayLinkSetOutputCallback(ocWindow->displayLink, oc_display_link_callback, ocWindow)) != kCVReturnSuccess) + // { + // oc_log_error("CVDisplayLinkSetOutputCallback ret: %d\n", ret); + // } + + // if((ret = CVDisplayLinkStart(ocWindow->displayLink)) != kCVReturnSuccess) + // { + // oc_log_error("CVDisplayLinkStart ret: %d\n", ret); + // } } diff --git a/src/app/win32_app.c b/src/app/win32_app.c index c286bb8..416aaf3 100644 --- a/src/app/win32_app.c +++ b/src/app/win32_app.c @@ -586,14 +586,6 @@ LRESULT oc_win32_win_proc(HWND windowHandle, UINT message, WPARAM wParam, LPARAM } break; - case OC_WM_USER_VBLANK: - { - oc_event event = { 0 }; - event.type = OC_EVENT_FRAME; - oc_queue_event(&event); - } - break; - default: { handled = false; diff --git a/src/app/win32_app.h b/src/app/win32_app.h index 7f682c4..8f41e84 100644 --- a/src/app/win32_app.h +++ b/src/app/win32_app.h @@ -49,7 +49,6 @@ typedef struct oc_win32_app_data enum OC_WM_USER { OC_WM_USER_DISPATCH_PROC = 0x0400, // WM_USER messages are defined from 0x400 to 0x7FFF - OC_WM_USER_VBLANK = 0x0401, }; #endif __WIN32_APP_H_ diff --git a/src/graphics/graphics.h b/src/graphics/graphics.h index ec69537..b9547a8 100644 --- a/src/graphics/graphics.h +++ b/src/graphics/graphics.h @@ -132,7 +132,7 @@ ORCA_API void oc_surface_send_to_back(oc_surface surface); //DOC: puts surface typedef void (*oc_vsync_callback)(void* data); ORCA_API void oc_vsync_init(void); -ORCA_API void oc_vsync_window(oc_window window); +ORCA_API void oc_vsync_wait(oc_window window); //------------------------------------------------------------------------------------------ //SECTION: graphics canvas structs diff --git a/src/graphics/graphics_surface.c b/src/graphics/graphics_surface.c index 246bf53..88175d7 100644 --- a/src/graphics/graphics_surface.c +++ b/src/graphics/graphics_surface.c @@ -148,7 +148,10 @@ oc_surface oc_surface_create_for_window(oc_window window, oc_surface_api api) if(surface) { surfaceHandle = oc_surface_handle_alloc(surface); + // On windows, we do a manual wait for vsync in runtime.c so just turn all the swap intervals off +#if OC_PLATFORM_WINDOWS oc_surface_swap_interval(surfaceHandle, 0); +#endif oc_surface_select(surfaceHandle); } return (surfaceHandle); diff --git a/src/graphics/win32_vsync.c b/src/graphics/win32_vsync.c index 0bfc4c9..7c486c0 100644 --- a/src/graphics/win32_vsync.c +++ b/src/graphics/win32_vsync.c @@ -23,89 +23,6 @@ typedef struct oc_vsync_data oc_vsync_data __oc_vsync_data; -typedef struct oc_vsync_thread_data -{ - IDXGIAdapter1* adapter; - oc_window window; - bool quit; -} oc_vsync_thread_data; - -static i32 oc_window_vysnc_notification_thread(void* userPointer) -{ - oc_vsync_thread_data* threadData = (oc_vsync_thread_data*)userPointer; - - while(!threadData->quit) - { - oc_window_data* windowData = oc_window_ptr_from_handle(threadData->window); - if(!windowData) - { - oc_log_error("Failed to get window ptr - assuming window was closed."); - break; - } - - RECT windowRect = { 0 }; - if(GetWindowRect(windowData->win32.hWnd, &windowRect) == FALSE) - { - oc_log_error("Failed to get window rect with error: %d.", GetLastError()); - return 1; - } - - //NOTE(Reuben): Wait for VBlank on the display device with which the window has the most intersecting area - IDXGIOutput* output = NULL; - IDXGIOutput* selectedOutput = NULL; - uint32_t selected_intersect_area = 0; - for(UINT i = 0; DXGI_ERROR_NOT_FOUND != IDXGIAdapter1_EnumOutputs(threadData->adapter, i, &output); ++i) - { - DXGI_OUTPUT_DESC outputDesc = { 0 }; - HRESULT hr = IDXGIOutput_GetDesc(output, &outputDesc); - if(SUCCEEDED(hr)) - { - RECT intersectRect = { 0 }; - if(IntersectRect(&intersectRect, &windowRect, &outputDesc.DesktopCoordinates)) - { - uint32_t outputIntersectArea = (intersectRect.right - intersectRect.left) * (intersectRect.bottom - intersectRect.top); - - if(selectedOutput == NULL || outputIntersectArea > selected_intersect_area) - { - selectedOutput = output; - selected_intersect_area = outputIntersectArea; - } - } - } - else - { - oc_log_error("Failed to get IDXGIOutput desc with error: %d", hr); - } - - if(selectedOutput != output) - { - IDXGIOutput_Release(output); - } - } - - if(selectedOutput) - { - HRESULT hr = IDXGIOutput_WaitForVBlank(selectedOutput); - - if(FAILED(hr)) - { - // TODO(reuben) - fall back to software timer - oc_log_warning("Failed to wait for vblank with error: %d", hr); - } - - SendMessage(windowData->win32.hWnd, OC_WM_USER_VBLANK, 0, 0); - - IDXGIOutput_Release(selectedOutput); - } - else - { - oc_log_warning("No outputs found. Were all monitors unplugged?"); - } - } - - return 0; -} - void oc_vsync_init(void) { if(__oc_vsync_data.adapter) @@ -116,7 +33,7 @@ void oc_vsync_init(void) IDXGIFactory4* factory = NULL; { UINT flags = 0; - flags |= DXGI_CREATE_FACTORY_DEBUG; // TODO make this optional + // flags |= DXGI_CREATE_FACTORY_DEBUG; // TODO make this optional HRESULT hr = CreateDXGIFactory2(flags, &IID_IDXGIFactory4, (void**)&factory); if(!SUCCEEDED(hr)) @@ -163,25 +80,77 @@ void oc_vsync_init(void) } else { - oc_log_info("Couldn't find any DXGI adapters. Vsync will be unavailable."); + oc_log_info("Couldn't find any DXGI adapters - vsync will be unavailable."); IDXGIFactory_Release(factory); } } -void oc_vsync_window(oc_window window) +void oc_vsync_wait(oc_window window) { if(__oc_vsync_data.adapter) { - oc_vsync_thread_data* data = malloc(sizeof(oc_vsync_thread_data)); - *data = (oc_vsync_thread_data){ - .adapter = __oc_vsync_data.adapter, - .window = window, - .quit = false, - }; - oc_thread_create_with_name(oc_window_vysnc_notification_thread, data, OC_STR8("vblank thread")); - } - else - { - oc_log_error("No DXGI adapter available - unable to start vsync notification thread."); + oc_window_data* windowData = oc_window_ptr_from_handle(window); + if(!windowData) + { + oc_log_error("Failed to get window ptr - assuming window was closed."); + return; + } + + RECT windowRect = { 0 }; + if(GetWindowRect(windowData->win32.hWnd, &windowRect) == FALSE) + { + oc_log_error("Failed to get window rect with error: %d.", GetLastError()); + return; + } + + // Wait for VBlank on the display device with which the window has the most intersecting area + IDXGIOutput* output = NULL; + IDXGIOutput* selectedOutput = NULL; + uint32_t selected_intersect_area = 0; + for(UINT i = 0; DXGI_ERROR_NOT_FOUND != IDXGIAdapter1_EnumOutputs(__oc_vsync_data.adapter, i, &output); ++i) + { + DXGI_OUTPUT_DESC outputDesc = { 0 }; + HRESULT hr = IDXGIOutput_GetDesc(output, &outputDesc); + if(SUCCEEDED(hr)) + { + RECT intersectRect = { 0 }; + if(IntersectRect(&intersectRect, &windowRect, &outputDesc.DesktopCoordinates)) + { + uint32_t outputIntersectArea = (intersectRect.right - intersectRect.left) * (intersectRect.bottom - intersectRect.top); + + if(selectedOutput == NULL || outputIntersectArea > selected_intersect_area) + { + selectedOutput = output; + selected_intersect_area = outputIntersectArea; + } + } + } + else + { + oc_log_error("Failed to get IDXGIOutput desc with error: %d", hr); + } + + if(selectedOutput != output) + { + IDXGIOutput_Release(output); + } + } + + if(selectedOutput) + { + HRESULT hr = IDXGIOutput_WaitForVBlank(selectedOutput); + + if(FAILED(hr)) + { + // TODO(reuben) - fall back to software timer + oc_log_warning("Failed to wait for vblank with error: %d", hr); + } + + IDXGIOutput_Release(selectedOutput); + } + else + { + oc_log_warning("No outputs found. Were all monitors unplugged?"); + } } } diff --git a/src/runtime.c b/src/runtime.c index da67cfe..a6dc482 100644 --- a/src/runtime.c +++ b/src/runtime.c @@ -676,19 +676,6 @@ i32 orca_runloop(void* user) } break; - case OC_EVENT_FRAME: - { - if(exports[OC_EXPORT_FRAME_REFRESH]) - { - M3Result res = m3_Call(exports[OC_EXPORT_FRAME_REFRESH], 0, 0); - if(res) - { - ORCA_WASM3_ABORT(app->runtime.m3Runtime, res, "Runtime error"); - } - } - } - break; - default: break; } @@ -851,6 +838,8 @@ i32 orca_runloop(void* user) oc_surface_present(app->debugOverlay.surface); oc_arena_clear(oc_scratch()); + + oc_vsync_wait(app->window); } if(exports[OC_EXPORT_TERMINATE]) @@ -880,8 +869,6 @@ int main(int argc, char** argv) oc_rect windowRect = { .x = 100, .y = 100, .w = 810, .h = 610 }; app->window = oc_window_create(windowRect, OC_STR8("orca"), 0); - oc_vsync_window(app->window); - app->debugOverlay.show = false; app->debugOverlay.surface = oc_surface_create_for_window(app->window, OC_CANVAS); app->debugOverlay.canvas = oc_canvas_create();