From 32fd49d52bb62f9ddced3ceda8d54d8875e93c36 Mon Sep 17 00:00:00 2001 From: Julian Petrov Date: Tue, 26 Apr 2022 23:06:59 -0400 Subject: [PATCH] st/oxr: Implement XR_KHR_win32_convert_performance_counter_time --- scripts/generate_oxr_ext_support.py | 1 + src/xrt/state_trackers/oxr/CMakeLists.txt | 4 ++ src/xrt/state_trackers/oxr/oxr_api_funcs.h | 12 ++++++ src/xrt/state_trackers/oxr/oxr_api_instance.c | 40 +++++++++++++++++++ .../state_trackers/oxr/oxr_api_negotiate.c | 5 +++ .../oxr/oxr_extension_support.h | 13 ++++++ src/xrt/state_trackers/oxr/oxr_instance.c | 24 +++++++++++ src/xrt/state_trackers/oxr/oxr_objects.h | 22 ++++++++++ 8 files changed, 121 insertions(+) diff --git a/scripts/generate_oxr_ext_support.py b/scripts/generate_oxr_ext_support.py index 38f2f5374..cc322c23c 100755 --- a/scripts/generate_oxr_ext_support.py +++ b/scripts/generate_oxr_ext_support.py @@ -26,6 +26,7 @@ EXTENSIONS = ( ['XR_KHR_swapchain_usage_input_attachment_bit'], ['XR_KHR_vulkan_enable', 'XR_USE_GRAPHICS_API_VULKAN'], ['XR_KHR_vulkan_enable2', 'XR_USE_GRAPHICS_API_VULKAN'], + ['XR_KHR_win32_convert_performance_counter_time', 'XR_USE_PLATFORM_WIN32'], ['XR_EXT_debug_utils', 'XRT_FEATURE_OPENXR_DEBUG_UTILS'], ['XR_EXT_dpad_binding'], ['XR_EXT_hand_tracking'], diff --git a/src/xrt/state_trackers/oxr/CMakeLists.txt b/src/xrt/state_trackers/oxr/CMakeLists.txt index e83994a48..d41238417 100644 --- a/src/xrt/state_trackers/oxr/CMakeLists.txt +++ b/src/xrt/state_trackers/oxr/CMakeLists.txt @@ -86,6 +86,10 @@ if(ANDROID) target_link_libraries(st_oxr PRIVATE aux_android) endif() +if(WIN32) + target_compile_definitions(st_oxr PRIVATE XR_USE_PLATFORM_WIN32) +endif() + target_link_libraries( st_oxr PRIVATE diff --git a/src/xrt/state_trackers/oxr/oxr_api_funcs.h b/src/xrt/state_trackers/oxr/oxr_api_funcs.h index 881c3ecec..6cf2f2ae2 100644 --- a/src/xrt/state_trackers/oxr/oxr_api_funcs.h +++ b/src/xrt/state_trackers/oxr/oxr_api_funcs.h @@ -104,6 +104,18 @@ oxr_xrConvertTimespecTimeToTimeKHR(XrInstance instance, const struct timespec *t XRAPI_ATTR XrResult XRAPI_CALL oxr_xrConvertTimeToTimespecTimeKHR(XrInstance instance, XrTime time, struct timespec *timespecTime); +#ifdef XR_USE_PLATFORM_WIN32 +//! OpenXR API function @ep{xrConvertWin32PerformanceCounterToTimeKHR} +XRAPI_ATTR XrResult XRAPI_CALL +oxr_xrConvertWin32PerformanceCounterToTimeKHR(XrInstance instance, + const LARGE_INTEGER *performanceCounter, + XrTime *time); + +//! OpenXR API function @ep{xrConvertTimeToWin32PerformanceCounterKHR} +XRAPI_ATTR XrResult XRAPI_CALL +oxr_xrConvertTimeToWin32PerformanceCounterKHR(XrInstance instance, XrTime time, LARGE_INTEGER *performanceCounter); +#endif // XR_USE_PLATFORM_WIN32 + /* * * oxr_api_system.c diff --git a/src/xrt/state_trackers/oxr/oxr_api_instance.c b/src/xrt/state_trackers/oxr/oxr_api_instance.c index 5e89cd110..e0417ec2c 100644 --- a/src/xrt/state_trackers/oxr/oxr_api_instance.c +++ b/src/xrt/state_trackers/oxr/oxr_api_instance.c @@ -391,3 +391,43 @@ oxr_xrConvertTimeToTimespecTimeKHR(XrInstance instance, XrTime time, struct time } #endif // XR_USE_TIMESPEC + +// ---- XR_KHR_win32_convert_performance_counter_time extension +#ifdef XR_USE_PLATFORM_WIN32 +XrResult +oxr_xrConvertWin32PerformanceCounterToTimeKHR(XrInstance instance, + const LARGE_INTEGER *performanceCounter, + XrTime *time) +{ + OXR_TRACE_MARKER(); + + //! @todo do we need to check and see if this extension was + //! enabled first? + struct oxr_instance *inst; + struct oxr_logger log; + OXR_VERIFY_INSTANCE_AND_INIT_LOG(&log, instance, inst, "xrConvertWin32PerformanceCounterToTimeKHR"); + OXR_VERIFY_EXTENSION(&log, inst, KHR_win32_convert_performance_counter_time); + OXR_VERIFY_ARG_NOT_NULL(&log, performanceCounter); + OXR_VERIFY_ARG_NOT_NULL(&log, time); + return oxr_instance_convert_win32perfcounter_to_time(&log, inst, performanceCounter, time); +} + +XrResult +oxr_xrConvertTimeToWin32PerformanceCounterKHR(XrInstance instance, XrTime time, LARGE_INTEGER *performanceCounter) +{ + OXR_TRACE_MARKER(); + + struct oxr_instance *inst; + struct oxr_logger log; + OXR_VERIFY_INSTANCE_AND_INIT_LOG(&log, instance, inst, "xrConvertTimeToWin32PerformanceCounterKHR"); + OXR_VERIFY_EXTENSION(&log, inst, KHR_win32_convert_performance_counter_time); + OXR_VERIFY_ARG_NOT_NULL(&log, performanceCounter); + + if (time <= (XrTime)0) { + return oxr_error(&log, XR_ERROR_TIME_INVALID, "(time == %" PRIi64 ") is not a valid time.", time); + } + + return oxr_instance_convert_time_to_win32perfcounter(&log, inst, time, performanceCounter); +} + +#endif // XR_USE_PLATFORM_WIN32 diff --git a/src/xrt/state_trackers/oxr/oxr_api_negotiate.c b/src/xrt/state_trackers/oxr/oxr_api_negotiate.c index 9c4ea45d5..d29c185e4 100644 --- a/src/xrt/state_trackers/oxr/oxr_api_negotiate.c +++ b/src/xrt/state_trackers/oxr/oxr_api_negotiate.c @@ -213,6 +213,11 @@ handle_non_null(struct oxr_instance *inst, struct oxr_logger *log, const char *n ENTRY_IF_EXT(xrConvertTimeToTimespecTimeKHR, KHR_convert_timespec_time); #endif // OXR_HAVE_KHR_convert_timespec_time +#ifdef OXR_HAVE_KHR_win32_convert_performance_counter_time + ENTRY_IF_EXT(xrConvertWin32PerformanceCounterToTimeKHR, KHR_win32_convert_performance_counter_time); + ENTRY_IF_EXT(xrConvertTimeToWin32PerformanceCounterKHR, KHR_win32_convert_performance_counter_time); +#endif // OXR_HAVE_KHR_win32_convert_performance_counter_time + #ifdef OXR_HAVE_EXT_performance_settings ENTRY_IF_EXT(xrPerfSettingsSetPerformanceLevelEXT, EXT_performance_settings); #endif // OXR_HAVE_EXT_performance_settings diff --git a/src/xrt/state_trackers/oxr/oxr_extension_support.h b/src/xrt/state_trackers/oxr/oxr_extension_support.h index 761a18d35..e424560a9 100644 --- a/src/xrt/state_trackers/oxr/oxr_extension_support.h +++ b/src/xrt/state_trackers/oxr/oxr_extension_support.h @@ -201,6 +201,18 @@ #endif +/* + * XR_KHR_win32_convert_performance_counter_time + */ +#if defined(XR_KHR_win32_convert_performance_counter_time) && defined(XR_USE_PLATFORM_WIN32) +#define OXR_HAVE_KHR_win32_convert_performance_counter_time +#define OXR_EXTENSION_SUPPORT_KHR_win32_convert_performance_counter_time(_) \ + _(KHR_win32_convert_performance_counter_time, KHR_WIN32_CONVERT_PERFORMANCE_COUNTER_TIME) +#else +#define OXR_EXTENSION_SUPPORT_KHR_win32_convert_performance_counter_time(_) +#endif + + /* * XR_EXT_debug_utils */ @@ -341,6 +353,7 @@ OXR_EXTENSION_SUPPORT_KHR_swapchain_usage_input_attachment_bit(_) \ OXR_EXTENSION_SUPPORT_KHR_vulkan_enable(_) \ OXR_EXTENSION_SUPPORT_KHR_vulkan_enable2(_) \ + OXR_EXTENSION_SUPPORT_KHR_win32_convert_performance_counter_time(_) \ OXR_EXTENSION_SUPPORT_EXT_debug_utils(_) \ OXR_EXTENSION_SUPPORT_EXT_dpad_binding(_) \ OXR_EXTENSION_SUPPORT_EXT_hand_tracking(_) \ diff --git a/src/xrt/state_trackers/oxr/oxr_instance.c b/src/xrt/state_trackers/oxr/oxr_instance.c index e0071cd53..7ec3a22d7 100644 --- a/src/xrt/state_trackers/oxr/oxr_instance.c +++ b/src/xrt/state_trackers/oxr/oxr_instance.c @@ -416,3 +416,27 @@ oxr_instance_convert_timespec_to_time(struct oxr_logger *log, return XR_SUCCESS; } #endif // XR_USE_TIMESPEC + +#ifdef XR_USE_PLATFORM_WIN32 + +XrResult +oxr_instance_convert_time_to_win32perfcounter(struct oxr_logger *log, + struct oxr_instance *inst, + XrTime time, + LARGE_INTEGER *win32perfcounterTime) +{ + time_state_to_win32perfcounter(inst->timekeeping, time, win32perfcounterTime); + return XR_SUCCESS; +} + +XrResult +oxr_instance_convert_win32perfcounter_to_time(struct oxr_logger *log, + struct oxr_instance *inst, + const LARGE_INTEGER *win32perfcounterTime, + XrTime *time) +{ + *time = time_state_from_win32perfcounter(inst->timekeeping, win32perfcounterTime); + return XR_SUCCESS; +} + +#endif // XR_USE_PLATFORM_WIN32 diff --git a/src/xrt/state_trackers/oxr/oxr_objects.h b/src/xrt/state_trackers/oxr/oxr_objects.h index c7002698e..55b2d520e 100644 --- a/src/xrt/state_trackers/oxr/oxr_objects.h +++ b/src/xrt/state_trackers/oxr/oxr_objects.h @@ -314,6 +314,28 @@ oxr_instance_convert_timespec_to_time(struct oxr_logger *log, XrTime *time); #endif // XR_USE_TIMESPEC +#ifdef XR_USE_PLATFORM_WIN32 + +/*! + * @public @memberof oxr_instance + */ +XrResult +oxr_instance_convert_time_to_win32perfcounter(struct oxr_logger *log, + struct oxr_instance *inst, + XrTime time, + LARGE_INTEGER *win32perfcounterTime); + +/*! + * @public @memberof oxr_instance + */ +XrResult +oxr_instance_convert_win32perfcounter_to_time(struct oxr_logger *log, + struct oxr_instance *inst, + const LARGE_INTEGER *win32perfcounterTime, + XrTime *time); + +#endif // XR_USE_PLATFORM_WIN32 + /*! * @} */