openxr_composition_layer_extension.cpp 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296
  1. /**************************************************************************/
  2. /* openxr_composition_layer_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_composition_layer_extension.h"
  31. #include "servers/rendering/rendering_server_globals.h"
  32. ////////////////////////////////////////////////////////////////////////////
  33. // OpenXRCompositionLayerExtension
  34. OpenXRCompositionLayerExtension *OpenXRCompositionLayerExtension::singleton = nullptr;
  35. OpenXRCompositionLayerExtension *OpenXRCompositionLayerExtension::get_singleton() {
  36. return singleton;
  37. }
  38. OpenXRCompositionLayerExtension::OpenXRCompositionLayerExtension() {
  39. singleton = this;
  40. }
  41. OpenXRCompositionLayerExtension::~OpenXRCompositionLayerExtension() {
  42. singleton = nullptr;
  43. }
  44. HashMap<String, bool *> OpenXRCompositionLayerExtension::get_requested_extensions() {
  45. HashMap<String, bool *> request_extensions;
  46. request_extensions[XR_KHR_COMPOSITION_LAYER_CYLINDER_EXTENSION_NAME] = &cylinder_ext_available;
  47. request_extensions[XR_KHR_COMPOSITION_LAYER_EQUIRECT2_EXTENSION_NAME] = &equirect_ext_available;
  48. return request_extensions;
  49. }
  50. void OpenXRCompositionLayerExtension::on_session_created(const XrSession p_instance) {
  51. OpenXRAPI::get_singleton()->register_composition_layer_provider(this);
  52. }
  53. void OpenXRCompositionLayerExtension::on_session_destroyed() {
  54. OpenXRAPI::get_singleton()->unregister_composition_layer_provider(this);
  55. }
  56. void OpenXRCompositionLayerExtension::on_pre_render() {
  57. for (OpenXRViewportCompositionLayerProvider *composition_layer : composition_layers) {
  58. composition_layer->on_pre_render();
  59. }
  60. }
  61. int OpenXRCompositionLayerExtension::get_composition_layer_count() {
  62. return composition_layers.size();
  63. }
  64. XrCompositionLayerBaseHeader *OpenXRCompositionLayerExtension::get_composition_layer(int p_index) {
  65. ERR_FAIL_INDEX_V(p_index, composition_layers.size(), nullptr);
  66. return composition_layers[p_index]->get_composition_layer();
  67. }
  68. int OpenXRCompositionLayerExtension::get_composition_layer_order(int p_index) {
  69. ERR_FAIL_INDEX_V(p_index, composition_layers.size(), 1);
  70. return composition_layers[p_index]->get_sort_order();
  71. }
  72. void OpenXRCompositionLayerExtension::register_composition_layer_provider(OpenXRViewportCompositionLayerProvider *p_composition_layer) {
  73. composition_layers.push_back(p_composition_layer);
  74. }
  75. void OpenXRCompositionLayerExtension::unregister_composition_layer_provider(OpenXRViewportCompositionLayerProvider *p_composition_layer) {
  76. composition_layers.erase(p_composition_layer);
  77. }
  78. bool OpenXRCompositionLayerExtension::is_available(XrStructureType p_which) {
  79. switch (p_which) {
  80. case XR_TYPE_COMPOSITION_LAYER_QUAD: {
  81. // Doesn't require an extension.
  82. return true;
  83. } break;
  84. case XR_TYPE_COMPOSITION_LAYER_CYLINDER_KHR: {
  85. return cylinder_ext_available;
  86. } break;
  87. case XR_TYPE_COMPOSITION_LAYER_EQUIRECT2_KHR: {
  88. return equirect_ext_available;
  89. } break;
  90. default: {
  91. ERR_PRINT(vformat("Unsupported composition layer type: %s", p_which));
  92. return false;
  93. }
  94. }
  95. }
  96. ////////////////////////////////////////////////////////////////////////////
  97. // OpenXRViewportCompositionLayerProvider
  98. OpenXRViewportCompositionLayerProvider::OpenXRViewportCompositionLayerProvider(XrCompositionLayerBaseHeader *p_composition_layer) {
  99. composition_layer = p_composition_layer;
  100. openxr_api = OpenXRAPI::get_singleton();
  101. composition_layer_extension = OpenXRCompositionLayerExtension::get_singleton();
  102. }
  103. OpenXRViewportCompositionLayerProvider::~OpenXRViewportCompositionLayerProvider() {
  104. // This will reset the viewport and free the swapchain too.
  105. set_viewport(RID(), Size2i());
  106. }
  107. void OpenXRViewportCompositionLayerProvider::set_alpha_blend(bool p_alpha_blend) {
  108. if (alpha_blend != p_alpha_blend) {
  109. alpha_blend = p_alpha_blend;
  110. if (alpha_blend) {
  111. composition_layer->layerFlags |= XR_COMPOSITION_LAYER_BLEND_TEXTURE_SOURCE_ALPHA_BIT;
  112. } else {
  113. composition_layer->layerFlags &= ~XR_COMPOSITION_LAYER_BLEND_TEXTURE_SOURCE_ALPHA_BIT;
  114. }
  115. }
  116. }
  117. void OpenXRViewportCompositionLayerProvider::set_viewport(RID p_viewport, Size2i p_size) {
  118. RenderingServer *rs = RenderingServer::get_singleton();
  119. ERR_FAIL_NULL(rs);
  120. if (viewport != p_viewport) {
  121. if (viewport.is_valid()) {
  122. RID rt = rs->viewport_get_render_target(viewport);
  123. RSG::texture_storage->render_target_set_override(rt, RID(), RID(), RID());
  124. }
  125. viewport = p_viewport;
  126. if (viewport.is_valid()) {
  127. viewport_size = p_size;
  128. } else {
  129. free_swapchain();
  130. viewport_size = Size2i();
  131. }
  132. }
  133. }
  134. void OpenXRViewportCompositionLayerProvider::on_pre_render() {
  135. RenderingServer *rs = RenderingServer::get_singleton();
  136. ERR_FAIL_NULL(rs);
  137. if (viewport.is_valid() && openxr_api && openxr_api->is_running()) {
  138. RS::ViewportUpdateMode update_mode = rs->viewport_get_update_mode(viewport);
  139. if (update_mode == RS::VIEWPORT_UPDATE_ONCE || update_mode == RS::VIEWPORT_UPDATE_ALWAYS) {
  140. // Update our XR swapchain
  141. if (update_and_acquire_swapchain(update_mode == RS::VIEWPORT_UPDATE_ONCE)) {
  142. // Render to our XR swapchain image.
  143. RID rt = rs->viewport_get_render_target(viewport);
  144. RSG::texture_storage->render_target_set_override(rt, get_current_swapchain_texture(), RID(), RID());
  145. }
  146. }
  147. }
  148. }
  149. XrCompositionLayerBaseHeader *OpenXRViewportCompositionLayerProvider::get_composition_layer() {
  150. if (openxr_api == nullptr || composition_layer_extension == nullptr) {
  151. // OpenXR not initialised or we're in the editor?
  152. return nullptr;
  153. }
  154. if (!composition_layer_extension->is_available(composition_layer->type)) {
  155. // Selected type is not supported, ignore our layer.
  156. return nullptr;
  157. }
  158. if (swapchain_info.swapchain == XR_NULL_HANDLE) {
  159. // Don't have a swapchain to display? Ignore our layer.
  160. return nullptr;
  161. }
  162. if (swapchain_info.image_acquired) {
  163. openxr_api->release_image(swapchain_info);
  164. }
  165. // Update the layer struct for the swapchain.
  166. switch (composition_layer->type) {
  167. case XR_TYPE_COMPOSITION_LAYER_QUAD: {
  168. XrCompositionLayerQuad *quad_layer = (XrCompositionLayerQuad *)composition_layer;
  169. quad_layer->subImage.swapchain = swapchain_info.swapchain;
  170. quad_layer->subImage.imageArrayIndex = 0;
  171. quad_layer->subImage.imageRect.offset.x = 0;
  172. quad_layer->subImage.imageRect.offset.y = 0;
  173. quad_layer->subImage.imageRect.extent.width = swapchain_size.width;
  174. quad_layer->subImage.imageRect.extent.height = swapchain_size.height;
  175. } break;
  176. case XR_TYPE_COMPOSITION_LAYER_CYLINDER_KHR: {
  177. XrCompositionLayerCylinderKHR *cylinder_layer = (XrCompositionLayerCylinderKHR *)composition_layer;
  178. cylinder_layer->subImage.swapchain = swapchain_info.swapchain;
  179. cylinder_layer->subImage.imageArrayIndex = 0;
  180. cylinder_layer->subImage.imageRect.offset.x = 0;
  181. cylinder_layer->subImage.imageRect.offset.y = 0;
  182. cylinder_layer->subImage.imageRect.extent.width = swapchain_size.width;
  183. cylinder_layer->subImage.imageRect.extent.height = swapchain_size.height;
  184. } break;
  185. case XR_TYPE_COMPOSITION_LAYER_EQUIRECT2_KHR: {
  186. XrCompositionLayerEquirect2KHR *equirect_layer = (XrCompositionLayerEquirect2KHR *)composition_layer;
  187. equirect_layer->subImage.swapchain = swapchain_info.swapchain;
  188. equirect_layer->subImage.imageArrayIndex = 0;
  189. equirect_layer->subImage.imageRect.offset.x = 0;
  190. equirect_layer->subImage.imageRect.offset.y = 0;
  191. equirect_layer->subImage.imageRect.extent.width = swapchain_size.width;
  192. equirect_layer->subImage.imageRect.extent.height = swapchain_size.height;
  193. } break;
  194. default: {
  195. return nullptr;
  196. } break;
  197. }
  198. return composition_layer;
  199. }
  200. bool OpenXRViewportCompositionLayerProvider::update_and_acquire_swapchain(bool p_static_image) {
  201. if (openxr_api == nullptr || composition_layer_extension == nullptr) {
  202. // OpenXR not initialised or we're in the editor?
  203. return false;
  204. }
  205. if (!composition_layer_extension->is_available(composition_layer->type)) {
  206. // Selected type is not supported?
  207. return false;
  208. }
  209. // See if our current swapchain is outdated.
  210. if (swapchain_info.swapchain != XR_NULL_HANDLE) {
  211. // If this swap chain, or the previous one, were static, then we can't reuse it.
  212. if (swapchain_size == viewport_size && !p_static_image && !static_image) {
  213. // We're all good! Just acquire it.
  214. return openxr_api->acquire_image(swapchain_info);
  215. }
  216. openxr_api->free_swapchain(swapchain_info);
  217. }
  218. // Create our new swap chain
  219. int64_t swapchain_format = openxr_api->get_color_swapchain_format();
  220. const uint32_t sample_count = 3;
  221. const uint32_t array_size = 1;
  222. XrSwapchainCreateFlags create_flags = 0;
  223. if (p_static_image) {
  224. create_flags |= XR_SWAPCHAIN_CREATE_STATIC_IMAGE_BIT;
  225. }
  226. if (!openxr_api->create_swapchain(create_flags, XR_SWAPCHAIN_USAGE_SAMPLED_BIT | XR_SWAPCHAIN_USAGE_COLOR_ATTACHMENT_BIT | XR_SWAPCHAIN_USAGE_MUTABLE_FORMAT_BIT, swapchain_format, viewport_size.width, viewport_size.height, sample_count, array_size, swapchain_info.swapchain, &swapchain_info.swapchain_graphics_data)) {
  227. swapchain_size = Size2i();
  228. return false;
  229. }
  230. // Acquire our image so we can start rendering into it
  231. bool ret = openxr_api->acquire_image(swapchain_info);
  232. swapchain_size = viewport_size;
  233. static_image = p_static_image;
  234. return ret;
  235. }
  236. void OpenXRViewportCompositionLayerProvider::free_swapchain() {
  237. if (swapchain_info.swapchain != XR_NULL_HANDLE) {
  238. openxr_api->free_swapchain(swapchain_info);
  239. }
  240. swapchain_size = Size2i();
  241. static_image = false;
  242. }
  243. RID OpenXRViewportCompositionLayerProvider::get_current_swapchain_texture() {
  244. if (openxr_api == nullptr) {
  245. return RID();
  246. }
  247. return openxr_api->get_image(swapchain_info);
  248. }