openxr_fb_foveation_extension.cpp 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  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_state_ready() {
  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. if (!is_enabled()) {
  109. return;
  110. }
  111. XrFoveationLevelProfileCreateInfoFB level_profile_create_info;
  112. level_profile_create_info.type = XR_TYPE_FOVEATION_LEVEL_PROFILE_CREATE_INFO_FB;
  113. level_profile_create_info.next = nullptr;
  114. level_profile_create_info.level = foveation_level;
  115. level_profile_create_info.verticalOffset = 0.0f;
  116. level_profile_create_info.dynamic = foveation_dynamic;
  117. XrFoveationProfileCreateInfoFB profile_create_info;
  118. profile_create_info.type = XR_TYPE_FOVEATION_PROFILE_CREATE_INFO_FB;
  119. profile_create_info.next = &level_profile_create_info;
  120. XrFoveationProfileFB foveation_profile;
  121. XrResult result = xrCreateFoveationProfileFB(OpenXRAPI::get_singleton()->get_session(), &profile_create_info, &foveation_profile);
  122. if (XR_FAILED(result)) {
  123. print_line("OpenXR: Unable to create the foveation profile [", OpenXRAPI::get_singleton()->get_error_string(result), "]");
  124. return;
  125. }
  126. XrSwapchainStateFoveationFB foveation_update_state;
  127. foveation_update_state.type = XR_TYPE_SWAPCHAIN_STATE_FOVEATION_FB;
  128. foveation_update_state.profile = foveation_profile;
  129. result = swapchain_update_state_ext->xrUpdateSwapchainFB(OpenXRAPI::get_singleton()->get_color_swapchain(), (XrSwapchainStateBaseHeaderFB *)&foveation_update_state);
  130. if (XR_FAILED(result)) {
  131. print_line("OpenXR: Unable to update the swapchain [", OpenXRAPI::get_singleton()->get_error_string(result), "]");
  132. // We still want to destroy our profile so keep going...
  133. }
  134. result = xrDestroyFoveationProfileFB(foveation_profile);
  135. if (XR_FAILED(result)) {
  136. print_line("OpenXR: Unable to destroy the foveation profile [", OpenXRAPI::get_singleton()->get_error_string(result), "]");
  137. }
  138. }