From 570f27d48651f5e8c79e5dd1a79324c585cc48ce Mon Sep 17 00:00:00 2001 From: Ryan Pavlik Date: Fri, 15 Oct 2021 14:16:29 -0500 Subject: [PATCH] st/oxr: Wire up Win32 OpenGL --- src/xrt/state_trackers/oxr/CMakeLists.txt | 8 +++ src/xrt/state_trackers/oxr/oxr_api_verify.h | 6 +++ src/xrt/state_trackers/oxr/oxr_objects.h | 7 +++ src/xrt/state_trackers/oxr/oxr_session.c | 15 ++++++ .../oxr/oxr_session_gfx_gl_win32.c | 51 +++++++++++++++++++ src/xrt/state_trackers/oxr/oxr_verify.c | 31 +++++++++++ 6 files changed, 118 insertions(+) create mode 100644 src/xrt/state_trackers/oxr/oxr_session_gfx_gl_win32.c diff --git a/src/xrt/state_trackers/oxr/CMakeLists.txt b/src/xrt/state_trackers/oxr/CMakeLists.txt index 8291f226d..6749a69d2 100644 --- a/src/xrt/state_trackers/oxr/CMakeLists.txt +++ b/src/xrt/state_trackers/oxr/CMakeLists.txt @@ -91,6 +91,14 @@ if(ANDROID) target_sources(st_oxr PRIVATE oxr_session_gfx_gles_android.c) target_link_libraries(st_oxr PRIVATE aux_android) endif() +if(WIN32) + target_link_libraries(st_oxr PRIVATE kernel32) + target_compile_definitions(st_oxr PRIVATE XR_USE_PLATFORM_WIN32) +endif() + +if(WIN32 AND XRT_HAVE_OPENGL) + target_sources(st_oxr PRIVATE oxr_session_gfx_gl_win32.c) +endif() if(WIN32) target_compile_definitions(st_oxr PRIVATE XR_USE_PLATFORM_WIN32) diff --git a/src/xrt/state_trackers/oxr/oxr_api_verify.h b/src/xrt/state_trackers/oxr/oxr_api_verify.h index b3f65d371..11de7e6de 100644 --- a/src/xrt/state_trackers/oxr/oxr_api_verify.h +++ b/src/xrt/state_trackers/oxr/oxr_api_verify.h @@ -270,6 +270,12 @@ XrResult oxr_verify_XrGraphicsBindingOpenGLXlibKHR(struct oxr_logger * /*log*/, const XrGraphicsBindingOpenGLXlibKHR * /*next*/); #endif // defined(XR_USE_PLATFORM_XLIB) && defined(XR_USE_GRAPHICS_API_OPENGL) +#if defined(XR_USE_PLATFORM_WIN32) && defined(XR_USE_GRAPHICS_API_OPENGL) +XrResult +oxr_verify_XrGraphicsBindingOpenGLWin32KHR(struct oxr_logger * /*log*/, + const XrGraphicsBindingOpenGLWin32KHR * /*next*/); +#endif // defined(XR_USE_PLATFORM_WIN32) && defined(XR_USE_GRAPHICS_API_OPENGL) + #if defined(XR_USE_GRAPHICS_API_VULKAN) XrResult oxr_verify_XrGraphicsBindingVulkanKHR(struct oxr_logger * /*log*/, const XrGraphicsBindingVulkanKHR * /*next*/); diff --git a/src/xrt/state_trackers/oxr/oxr_objects.h b/src/xrt/state_trackers/oxr/oxr_objects.h index 9754de03a..cafeb7091 100644 --- a/src/xrt/state_trackers/oxr/oxr_objects.h +++ b/src/xrt/state_trackers/oxr/oxr_objects.h @@ -1097,6 +1097,13 @@ oxr_session_populate_gl_xlib(struct oxr_logger *log, struct oxr_session *sess); #endif // XR_USE_PLATFORM_XLIB +#ifdef XR_USE_PLATFORM_WIN32 +XrResult +oxr_session_populate_gl_win32(struct oxr_logger *log, + struct oxr_system *sys, + XrGraphicsBindingOpenGLWin32KHR const *next, + struct oxr_session *sess); +#endif // XR_USE_PLATFORM_WIN32 #endif // XR_USE_GRAPHICS_API_OPENGL #if defined(XR_USE_GRAPHICS_API_OPENGL) || defined(XR_USE_GRAPHICS_API_OPENGL_ES) diff --git a/src/xrt/state_trackers/oxr/oxr_session.c b/src/xrt/state_trackers/oxr/oxr_session.c index c1d8f8c0c..38ee2a382 100644 --- a/src/xrt/state_trackers/oxr/oxr_session.c +++ b/src/xrt/state_trackers/oxr/oxr_session.c @@ -722,6 +722,21 @@ oxr_session_create_impl(struct oxr_logger *log, } #endif +#if defined(XR_USE_PLATFORM_WIN32) && defined(XR_USE_GRAPHICS_API_OPENGL) + XrGraphicsBindingOpenGLWin32KHR const *opengl_win32 = OXR_GET_INPUT_FROM_CHAIN( + createInfo, XR_TYPE_GRAPHICS_BINDING_OPENGL_WIN32_KHR, XrGraphicsBindingOpenGLWin32KHR); + if (opengl_win32 != NULL) { + if (!sys->gotten_requirements) { + return oxr_error(log, XR_ERROR_GRAPHICS_REQUIREMENTS_CALL_MISSING, + "Has not called xrGetOpenGLGraphicsRequirementsKHR"); + } + + OXR_SESSION_ALLOCATE_AND_INIT(log, sys, *out_session); + OXR_ALLOCATE_NATIVE_COMPOSITOR(log, xsi, *out_session); + return oxr_session_populate_gl_win32(log, sys, opengl_win32, *out_session); + } +#endif + #ifdef XR_USE_GRAPHICS_API_VULKAN XrGraphicsBindingVulkanKHR const *vulkan = OXR_GET_INPUT_FROM_CHAIN(createInfo, XR_TYPE_GRAPHICS_BINDING_VULKAN_KHR, XrGraphicsBindingVulkanKHR); diff --git a/src/xrt/state_trackers/oxr/oxr_session_gfx_gl_win32.c b/src/xrt/state_trackers/oxr/oxr_session_gfx_gl_win32.c new file mode 100644 index 000000000..6e83bf79e --- /dev/null +++ b/src/xrt/state_trackers/oxr/oxr_session_gfx_gl_win32.c @@ -0,0 +1,51 @@ +// Copyright 2018-2022, Collabora, Ltd. +// SPDX-License-Identifier: BSL-1.0 +/*! + * @file + * @brief Holds OpenGL-specific session functions for Windows + * @author Jakob Bornecrantz + * @author Ryan Pavlik + * @ingroup oxr_main + * @ingroup comp_client + */ + +#ifndef XR_USE_PLATFORM_WIN32 +#error "Must build this file with Win32 enabled!" +#endif + +#ifndef XR_USE_GRAPHICS_API_OPENGL +#error "Must build this file with OpenGL enabled!" +#endif + +#include + +#include "util/u_misc.h" + +#include "oxr_objects.h" +#include "oxr_logger.h" +#include "oxr_two_call.h" +#include "oxr_handle.h" + +#include "xrt/xrt_instance.h" +#include "xrt/xrt_gfx_win32.h" + + +XrResult +oxr_session_populate_gl_win32(struct oxr_logger *log, + struct oxr_system *sys, + XrGraphicsBindingOpenGLWin32KHR const *next, + struct oxr_session *sess) +{ + + struct xrt_compositor_native *xcn = sess->xcn; + struct xrt_compositor_gl *xcgl = xrt_gfx_provider_create_gl_win32(xcn, next->hDC, next->hGLRC); + + if (xcgl == NULL) { + return oxr_error(log, XR_ERROR_INITIALIZATION_FAILED, "Failed to create a Win32 client compositor"); + } + + sess->compositor = &xcgl->base; + sess->create_swapchain = oxr_swapchain_gl_create; + + return XR_SUCCESS; +} diff --git a/src/xrt/state_trackers/oxr/oxr_verify.c b/src/xrt/state_trackers/oxr/oxr_verify.c index 350a7d725..561d27852 100644 --- a/src/xrt/state_trackers/oxr/oxr_verify.c +++ b/src/xrt/state_trackers/oxr/oxr_verify.c @@ -470,6 +470,15 @@ oxr_verify_XrSessionCreateInfo(struct oxr_logger *log, } #endif // defined(OXR_HAVE_KHR_opengl_enable) && defined(XR_USE_PLATFORM_XLIB) +#if defined(OXR_HAVE_KHR_opengl_enable) && defined(XR_USE_PLATFORM_WIN32) + XrGraphicsBindingOpenGLWin32KHR const *opengl_win32 = OXR_GET_INPUT_FROM_CHAIN( + createInfo, XR_TYPE_GRAPHICS_BINDING_OPENGL_WIN32_KHR, XrGraphicsBindingOpenGLWin32KHR); + if (opengl_win32 != NULL) { + OXR_VERIFY_EXTENSION(log, inst, KHR_opengl_enable); + return oxr_verify_XrGraphicsBindingOpenGLWin32KHR(log, opengl_win32); + } +#endif // defined(OXR_HAVE_KHR_opengl_enable) && defined(XR_USE_PLATFORM_WIN32) + #if defined(OXR_HAVE_KHR_vulkan_enable) || defined(OXR_HAVE_KHR_vulkan_enable2) /* XR_TYPE_GRAPHICS_BINDING_VULKAN2_KHR aliased to * XR_TYPE_GRAPHICS_BINDING_VULKAN_KHR */ @@ -567,6 +576,28 @@ oxr_verify_XrGraphicsBindingOpenGLXlibKHR(struct oxr_logger *log, const XrGraphi #endif // defined(XR_USE_PLATFORM_XLIB) && defined(XR_USE_GRAPHICS_API_OPENGL) +#if defined(XR_USE_PLATFORM_WIN32) && defined(XR_USE_GRAPHICS_API_OPENGL) + +XrResult +oxr_verify_XrGraphicsBindingOpenGLWin32KHR(struct oxr_logger *log, const XrGraphicsBindingOpenGLWin32KHR *next) +{ + if (next->type != XR_TYPE_GRAPHICS_BINDING_OPENGL_WIN32_KHR) { + return oxr_error(log, XR_ERROR_VALIDATION_FAILURE, "Graphics binding has invalid type"); + } + + if (next->hDC == NULL) { + return oxr_error(log, XR_ERROR_VALIDATION_FAILURE, "hDC is NULL"); + } + + if (next->hGLRC == NULL) { + return oxr_error(log, XR_ERROR_VALIDATION_FAILURE, "hGLRC is NULL"); + } + + return XR_SUCCESS; +} + +#endif // defined(XR_USE_PLATFORM_WIN32) && defined(XR_USE_GRAPHICS_API_OPENGL) + #ifdef XR_USE_GRAPHICS_API_VULKAN