openxr_fb_foveation_extension.cpp 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184
  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. OpenXRFBFoveationExtension *OpenXRFBFoveationExtension::singleton = nullptr;
  33. OpenXRFBFoveationExtension *OpenXRFBFoveationExtension::get_singleton() {
  34. return singleton;
  35. }
  36. OpenXRFBFoveationExtension::OpenXRFBFoveationExtension(const String &p_rendering_driver) {
  37. singleton = this;
  38. rendering_driver = p_rendering_driver;
  39. swapchain_update_state_ext = OpenXRFBUpdateSwapchainExtension::get_singleton();
  40. int fov_level = GLOBAL_GET("xr/openxr/foveation_level");
  41. if (fov_level >= 0 && fov_level < 4) {
  42. foveation_level = XrFoveationLevelFB(fov_level);
  43. }
  44. bool fov_dyn = GLOBAL_GET("xr/openxr/foveation_dynamic");
  45. foveation_dynamic = fov_dyn ? XR_FOVEATION_DYNAMIC_LEVEL_ENABLED_FB : XR_FOVEATION_DYNAMIC_DISABLED_FB;
  46. swapchain_create_info_foveation_fb.type = XR_TYPE_SWAPCHAIN_CREATE_INFO_FOVEATION_FB;
  47. swapchain_create_info_foveation_fb.next = nullptr;
  48. swapchain_create_info_foveation_fb.flags = 0;
  49. }
  50. OpenXRFBFoveationExtension::~OpenXRFBFoveationExtension() {
  51. singleton = nullptr;
  52. swapchain_update_state_ext = nullptr;
  53. }
  54. HashMap<String, bool *> OpenXRFBFoveationExtension::get_requested_extensions() {
  55. HashMap<String, bool *> request_extensions;
  56. if (rendering_driver == "vulkan") {
  57. // This is currently only supported on OpenGL, but we may add Vulkan support in the future...
  58. } else if (rendering_driver == "opengl3") {
  59. request_extensions[XR_FB_FOVEATION_EXTENSION_NAME] = &fb_foveation_ext;
  60. request_extensions[XR_FB_FOVEATION_CONFIGURATION_EXTENSION_NAME] = &fb_foveation_configuration_ext;
  61. }
  62. return request_extensions;
  63. }
  64. void OpenXRFBFoveationExtension::on_instance_created(const XrInstance p_instance) {
  65. if (fb_foveation_ext) {
  66. EXT_INIT_XR_FUNC(xrCreateFoveationProfileFB);
  67. EXT_INIT_XR_FUNC(xrDestroyFoveationProfileFB);
  68. }
  69. if (fb_foveation_configuration_ext) {
  70. // nothing to register here...
  71. }
  72. }
  73. void OpenXRFBFoveationExtension::on_instance_destroyed() {
  74. fb_foveation_ext = false;
  75. fb_foveation_configuration_ext = false;
  76. }
  77. bool OpenXRFBFoveationExtension::is_enabled() const {
  78. return swapchain_update_state_ext != nullptr && swapchain_update_state_ext->is_enabled() && fb_foveation_ext && fb_foveation_configuration_ext;
  79. }
  80. void *OpenXRFBFoveationExtension::set_swapchain_create_info_and_get_next_pointer(void *p_next_pointer) {
  81. if (is_enabled()) {
  82. swapchain_create_info_foveation_fb.next = p_next_pointer;
  83. return &swapchain_create_info_foveation_fb;
  84. } else {
  85. return p_next_pointer;
  86. }
  87. }
  88. void OpenXRFBFoveationExtension::on_main_swapchains_created() {
  89. update_profile();
  90. }
  91. XrFoveationLevelFB OpenXRFBFoveationExtension::get_foveation_level() const {
  92. return foveation_level;
  93. }
  94. void OpenXRFBFoveationExtension::set_foveation_level(XrFoveationLevelFB p_foveation_level) {
  95. foveation_level = p_foveation_level;
  96. // Update profile will do nothing if we're not yet initialized.
  97. update_profile();
  98. }
  99. XrFoveationDynamicFB OpenXRFBFoveationExtension::get_foveation_dynamic() const {
  100. return foveation_dynamic;
  101. }
  102. void OpenXRFBFoveationExtension::set_foveation_dynamic(XrFoveationDynamicFB p_foveation_dynamic) {
  103. foveation_dynamic = p_foveation_dynamic;
  104. // Update profile will do nothing if we're not yet initialized.
  105. update_profile();
  106. }
  107. void OpenXRFBFoveationExtension::_update_profile() {
  108. // Must be called from rendering thread!
  109. ERR_NOT_ON_RENDER_THREAD;
  110. OpenXRFBFoveationExtension *fov_ext = OpenXRFBFoveationExtension::get_singleton();
  111. ERR_FAIL_NULL(fov_ext);
  112. if (!fov_ext->is_enabled()) {
  113. return;
  114. }
  115. OpenXRAPI *openxr_api = OpenXRAPI::get_singleton();
  116. ERR_FAIL_NULL(openxr_api);
  117. XrSwapchain main_color_swapchain = openxr_api->get_color_swapchain();
  118. if (main_color_swapchain == XR_NULL_HANDLE) {
  119. // Our swapchain hasn't been created yet, we'll call this again once it has.
  120. return;
  121. }
  122. XrFoveationLevelProfileCreateInfoFB level_profile_create_info;
  123. level_profile_create_info.type = XR_TYPE_FOVEATION_LEVEL_PROFILE_CREATE_INFO_FB;
  124. level_profile_create_info.next = nullptr;
  125. level_profile_create_info.level = fov_ext->foveation_level;
  126. level_profile_create_info.verticalOffset = 0.0f;
  127. level_profile_create_info.dynamic = fov_ext->foveation_dynamic;
  128. XrFoveationProfileCreateInfoFB profile_create_info;
  129. profile_create_info.type = XR_TYPE_FOVEATION_PROFILE_CREATE_INFO_FB;
  130. profile_create_info.next = &level_profile_create_info;
  131. XrFoveationProfileFB foveation_profile;
  132. XrResult result = fov_ext->xrCreateFoveationProfileFB(openxr_api->get_session(), &profile_create_info, &foveation_profile);
  133. if (XR_FAILED(result)) {
  134. print_line("OpenXR: Unable to create the foveation profile [", openxr_api->get_error_string(result), "]");
  135. return;
  136. }
  137. XrSwapchainStateFoveationFB foveation_update_state;
  138. foveation_update_state.type = XR_TYPE_SWAPCHAIN_STATE_FOVEATION_FB;
  139. foveation_update_state.profile = foveation_profile;
  140. result = fov_ext->swapchain_update_state_ext->xrUpdateSwapchainFB(main_color_swapchain, (XrSwapchainStateBaseHeaderFB *)&foveation_update_state);
  141. if (XR_FAILED(result)) {
  142. print_line("OpenXR: Unable to update the swapchain [", openxr_api->get_error_string(result), "]");
  143. // We still want to destroy our profile so keep going...
  144. }
  145. result = fov_ext->xrDestroyFoveationProfileFB(foveation_profile);
  146. if (XR_FAILED(result)) {
  147. print_line("OpenXR: Unable to destroy the foveation profile [", openxr_api->get_error_string(result), "]");
  148. }
  149. }