openxr_fb_foveation_extension.cpp 8.0 KB

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