openxr_fb_foveation_extension.cpp 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284
  1. /**************************************************************************/
  2. /* openxr_fb_foveation_extension.cpp */
  3. /**************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /**************************************************************************/
  8. /* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */
  9. /* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */
  10. /* */
  11. /* Permission is hereby granted, free of charge, to any person obtaining */
  12. /* a copy of this software and associated documentation files (the */
  13. /* "Software"), to deal in the Software without restriction, including */
  14. /* without limitation the rights to use, copy, modify, merge, publish, */
  15. /* distribute, sublicense, and/or sell copies of the Software, and to */
  16. /* permit persons to whom the Software is furnished to do so, subject to */
  17. /* the following conditions: */
  18. /* */
  19. /* The above copyright notice and this permission notice shall be */
  20. /* included in all copies or substantial portions of the Software. */
  21. /* */
  22. /* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
  23. /* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
  24. /* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. */
  25. /* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
  26. /* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
  27. /* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
  28. /* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
  29. /**************************************************************************/
  30. #include "openxr_fb_foveation_extension.h"
  31. #include "core/config/project_settings.h"
  32. #include "openxr_eye_gaze_interaction.h"
  33. #include "../openxr_platform_inc.h"
  34. OpenXRFBFoveationExtension *OpenXRFBFoveationExtension::singleton = nullptr;
  35. OpenXRFBFoveationExtension *OpenXRFBFoveationExtension::get_singleton() {
  36. return singleton;
  37. }
  38. OpenXRFBFoveationExtension::OpenXRFBFoveationExtension(const String &p_rendering_driver) {
  39. singleton = this;
  40. rendering_driver = p_rendering_driver;
  41. swapchain_update_state_ext = OpenXRFBUpdateSwapchainExtension::get_singleton();
  42. int fov_level = GLOBAL_GET("xr/openxr/foveation_level");
  43. if (fov_level >= 0 && fov_level < 4) {
  44. foveation_level = XrFoveationLevelFB(fov_level);
  45. }
  46. bool fov_dyn = GLOBAL_GET("xr/openxr/foveation_dynamic");
  47. foveation_dynamic = fov_dyn ? XR_FOVEATION_DYNAMIC_LEVEL_ENABLED_FB : XR_FOVEATION_DYNAMIC_DISABLED_FB;
  48. swapchain_create_info_foveation_fb.type = XR_TYPE_SWAPCHAIN_CREATE_INFO_FOVEATION_FB;
  49. swapchain_create_info_foveation_fb.next = nullptr;
  50. swapchain_create_info_foveation_fb.flags = 0;
  51. meta_foveation_eye_tracked_create_info.type = XR_TYPE_FOVEATION_EYE_TRACKED_PROFILE_CREATE_INFO_META;
  52. meta_foveation_eye_tracked_create_info.next = nullptr;
  53. meta_foveation_eye_tracked_create_info.flags = 0;
  54. meta_foveation_eye_tracked_properties.type = XR_TYPE_SYSTEM_FOVEATION_EYE_TRACKED_PROPERTIES_META;
  55. meta_foveation_eye_tracked_properties.next = nullptr;
  56. meta_foveation_eye_tracked_properties.supportsFoveationEyeTracked = XR_FALSE;
  57. #ifdef VULKAN_ENABLED
  58. meta_vulkan_swapchain_create_info.type = XR_TYPE_VULKAN_SWAPCHAIN_CREATE_INFO_META;
  59. meta_vulkan_swapchain_create_info.next = nullptr;
  60. meta_vulkan_swapchain_create_info.additionalCreateFlags = VK_IMAGE_CREATE_FRAGMENT_DENSITY_MAP_OFFSET_BIT_QCOM;
  61. meta_vulkan_swapchain_create_info.additionalUsageFlags = 0;
  62. #endif
  63. if (rendering_driver == "opengl3") {
  64. swapchain_create_info_foveation_fb.flags = XR_SWAPCHAIN_CREATE_FOVEATION_SCALED_BIN_BIT_FB;
  65. } else if (rendering_driver == "vulkan") {
  66. swapchain_create_info_foveation_fb.flags = XR_SWAPCHAIN_CREATE_FOVEATION_FRAGMENT_DENSITY_MAP_BIT_FB;
  67. }
  68. }
  69. OpenXRFBFoveationExtension::~OpenXRFBFoveationExtension() {
  70. singleton = nullptr;
  71. swapchain_update_state_ext = nullptr;
  72. }
  73. HashMap<String, bool *> OpenXRFBFoveationExtension::get_requested_extensions(XrVersion p_version) {
  74. HashMap<String, bool *> request_extensions;
  75. request_extensions[XR_FB_FOVEATION_EXTENSION_NAME] = &fb_foveation_ext;
  76. request_extensions[XR_FB_FOVEATION_CONFIGURATION_EXTENSION_NAME] = &fb_foveation_configuration_ext;
  77. #ifdef XR_USE_GRAPHICS_API_VULKAN
  78. if (rendering_driver == "vulkan") {
  79. request_extensions[XR_FB_FOVEATION_VULKAN_EXTENSION_NAME] = &fb_foveation_vulkan_ext;
  80. request_extensions[XR_META_FOVEATION_EYE_TRACKED_EXTENSION_NAME] = &meta_foveation_eye_tracked_ext;
  81. request_extensions[XR_META_VULKAN_SWAPCHAIN_CREATE_INFO_EXTENSION_NAME] = &meta_vulkan_swapchain_create_info_ext;
  82. }
  83. #endif // XR_USE_GRAPHICS_API_VULKAN
  84. return request_extensions;
  85. }
  86. void OpenXRFBFoveationExtension::on_instance_created(const XrInstance p_instance) {
  87. if (fb_foveation_ext) {
  88. EXT_INIT_XR_FUNC(xrCreateFoveationProfileFB);
  89. EXT_INIT_XR_FUNC(xrDestroyFoveationProfileFB);
  90. }
  91. if (fb_foveation_configuration_ext) {
  92. // nothing to register here...
  93. }
  94. if (meta_foveation_eye_tracked_ext) {
  95. EXT_INIT_XR_FUNC(xrGetFoveationEyeTrackedStateMETA);
  96. }
  97. }
  98. void OpenXRFBFoveationExtension::on_instance_destroyed() {
  99. fb_foveation_ext = false;
  100. fb_foveation_configuration_ext = false;
  101. meta_foveation_eye_tracked_ext = false;
  102. }
  103. bool OpenXRFBFoveationExtension::is_enabled() const {
  104. bool enabled = swapchain_update_state_ext != nullptr && swapchain_update_state_ext->is_enabled() && fb_foveation_ext && fb_foveation_configuration_ext;
  105. #ifdef XR_USE_GRAPHICS_API_VULKAN
  106. if (rendering_driver == "vulkan") {
  107. enabled = enabled && fb_foveation_vulkan_ext;
  108. }
  109. #endif // XR_USE_GRAPHICS_API_VULKAN
  110. return enabled;
  111. }
  112. void *OpenXRFBFoveationExtension::set_system_properties_and_get_next_pointer(void *p_next_pointer) {
  113. #ifdef XR_USE_GRAPHICS_API_VULKAN
  114. if (rendering_driver == "vulkan") {
  115. meta_foveation_eye_tracked_properties.next = p_next_pointer;
  116. return &meta_foveation_eye_tracked_properties;
  117. }
  118. #endif
  119. return p_next_pointer;
  120. }
  121. void *OpenXRFBFoveationExtension::set_swapchain_create_info_and_get_next_pointer(void *p_next_pointer) {
  122. void *next = p_next_pointer;
  123. if (is_enabled()) {
  124. swapchain_create_info_foveation_fb.next = next;
  125. next = &swapchain_create_info_foveation_fb;
  126. #ifdef VULKAN_ENABLED
  127. if (meta_foveation_eye_tracked_ext && meta_vulkan_swapchain_create_info_ext && meta_foveation_eye_tracked_properties.supportsFoveationEyeTracked) {
  128. meta_vulkan_swapchain_create_info.next = next;
  129. next = &meta_vulkan_swapchain_create_info;
  130. }
  131. #endif
  132. }
  133. return next;
  134. }
  135. void OpenXRFBFoveationExtension::on_main_swapchains_created() {
  136. update_profile();
  137. }
  138. XrFoveationLevelFB OpenXRFBFoveationExtension::get_foveation_level() const {
  139. return foveation_level;
  140. }
  141. void OpenXRFBFoveationExtension::set_foveation_level(XrFoveationLevelFB p_foveation_level) {
  142. foveation_level = p_foveation_level;
  143. // Update profile will do nothing if we're not yet initialized.
  144. update_profile();
  145. }
  146. XrFoveationDynamicFB OpenXRFBFoveationExtension::get_foveation_dynamic() const {
  147. return foveation_dynamic;
  148. }
  149. void OpenXRFBFoveationExtension::set_foveation_dynamic(XrFoveationDynamicFB p_foveation_dynamic) {
  150. foveation_dynamic = p_foveation_dynamic;
  151. // Update profile will do nothing if we're not yet initialized.
  152. update_profile();
  153. }
  154. bool OpenXRFBFoveationExtension::is_foveation_eye_tracked_enabled() const {
  155. return is_enabled() && meta_foveation_eye_tracked_ext && meta_vulkan_swapchain_create_info_ext && meta_foveation_eye_tracked_properties.supportsFoveationEyeTracked;
  156. }
  157. void OpenXRFBFoveationExtension::get_fragment_density_offsets(LocalVector<Vector2i> &r_offsets) {
  158. // Must be called from rendering thread!
  159. ERR_NOT_ON_RENDER_THREAD;
  160. if (!is_foveation_eye_tracked_enabled() || !OpenXREyeGazeInteractionExtension::get_singleton()->is_available()) {
  161. return;
  162. }
  163. OpenXRAPI *openxr_api = OpenXRAPI::get_singleton();
  164. ERR_FAIL_NULL(openxr_api);
  165. _update_profile_rt();
  166. XrFoveationEyeTrackedStateMETA state = {
  167. XR_TYPE_FOVEATION_EYE_TRACKED_STATE_META, // type
  168. nullptr, // next
  169. { XrVector2f{}, XrVector2f{} }, // foveationCenter[XR_FOVEATION_CENTER_SIZE_META];
  170. 0, // flags
  171. };
  172. XrResult result = xrGetFoveationEyeTrackedStateMETA(openxr_api->get_session(), &state);
  173. if (XR_FAILED(result)) {
  174. print_line("OpenXR: Unable to get foveation offsets [", openxr_api->get_error_string(result), "]");
  175. return;
  176. }
  177. if (!(state.flags & XR_FOVEATION_EYE_TRACKED_STATE_VALID_BIT_META)) {
  178. return;
  179. }
  180. r_offsets.reserve(XR_FOVEATION_CENTER_SIZE_META);
  181. Size2 dims = openxr_api->get_recommended_target_size() * 0.5;
  182. for (uint32_t i = 0; i < XR_FOVEATION_CENTER_SIZE_META; ++i) {
  183. const XrVector2f &xr_center = state.foveationCenter[i];
  184. r_offsets.push_back(Vector2i((int)(xr_center.x * dims.x), (int)(xr_center.y * dims.y)));
  185. }
  186. }
  187. void OpenXRFBFoveationExtension::_update_profile_rt() {
  188. // Must be called from rendering thread!
  189. ERR_NOT_ON_RENDER_THREAD;
  190. if (!is_enabled()) {
  191. return;
  192. }
  193. OpenXRAPI *openxr_api = OpenXRAPI::get_singleton();
  194. ERR_FAIL_NULL(openxr_api);
  195. XrSwapchain main_color_swapchain = openxr_api->get_color_swapchain();
  196. if (main_color_swapchain == XR_NULL_HANDLE) {
  197. // Our swapchain hasn't been created yet, we'll call this again once it has.
  198. return;
  199. }
  200. void *next = nullptr;
  201. if (meta_foveation_eye_tracked_ext && meta_vulkan_swapchain_create_info_ext && meta_foveation_eye_tracked_properties.supportsFoveationEyeTracked) {
  202. meta_foveation_eye_tracked_create_info.next = next;
  203. next = &meta_foveation_eye_tracked_create_info;
  204. }
  205. XrFoveationLevelProfileCreateInfoFB level_profile_create_info;
  206. level_profile_create_info.type = XR_TYPE_FOVEATION_LEVEL_PROFILE_CREATE_INFO_FB;
  207. level_profile_create_info.next = next;
  208. level_profile_create_info.level = foveation_level;
  209. level_profile_create_info.verticalOffset = 0.0f;
  210. level_profile_create_info.dynamic = foveation_dynamic;
  211. XrFoveationProfileCreateInfoFB profile_create_info;
  212. profile_create_info.type = XR_TYPE_FOVEATION_PROFILE_CREATE_INFO_FB;
  213. profile_create_info.next = &level_profile_create_info;
  214. XrFoveationProfileFB foveation_profile;
  215. XrResult result = xrCreateFoveationProfileFB(openxr_api->get_session(), &profile_create_info, &foveation_profile);
  216. if (XR_FAILED(result)) {
  217. print_line("OpenXR: Unable to create the foveation profile [", openxr_api->get_error_string(result), "]");
  218. return;
  219. }
  220. XrSwapchainStateFoveationFB foveation_update_state;
  221. foveation_update_state.type = XR_TYPE_SWAPCHAIN_STATE_FOVEATION_FB;
  222. foveation_update_state.next = nullptr;
  223. foveation_update_state.flags = 0;
  224. foveation_update_state.profile = foveation_profile;
  225. result = swapchain_update_state_ext->xrUpdateSwapchainFB(main_color_swapchain, (XrSwapchainStateBaseHeaderFB *)&foveation_update_state);
  226. if (XR_FAILED(result)) {
  227. print_line("OpenXR: Unable to update the swapchain [", openxr_api->get_error_string(result), "]");
  228. // We still want to destroy our profile so keep going...
  229. }
  230. result = xrDestroyFoveationProfileFB(foveation_profile);
  231. if (XR_FAILED(result)) {
  232. print_line("OpenXR: Unable to destroy the foveation profile [", openxr_api->get_error_string(result), "]");
  233. }
  234. }