openxr_composition_layer_extension.cpp 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326
  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_viewport_composition_layer_provider(OpenXRViewportCompositionLayerProvider *p_composition_layer) {
  73. composition_layers.push_back(p_composition_layer);
  74. }
  75. void OpenXRCompositionLayerExtension::unregister_viewport_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. for (OpenXRExtensionWrapper *extension : OpenXRAPI::get_registered_extension_wrappers()) {
  105. extension->on_viewport_composition_layer_destroyed(composition_layer);
  106. }
  107. // This will reset the viewport and free the swapchain too.
  108. set_viewport(RID(), Size2i());
  109. }
  110. void OpenXRViewportCompositionLayerProvider::set_alpha_blend(bool p_alpha_blend) {
  111. if (alpha_blend != p_alpha_blend) {
  112. alpha_blend = p_alpha_blend;
  113. if (alpha_blend) {
  114. composition_layer->layerFlags |= XR_COMPOSITION_LAYER_BLEND_TEXTURE_SOURCE_ALPHA_BIT;
  115. } else {
  116. composition_layer->layerFlags &= ~XR_COMPOSITION_LAYER_BLEND_TEXTURE_SOURCE_ALPHA_BIT;
  117. }
  118. }
  119. }
  120. void OpenXRViewportCompositionLayerProvider::set_viewport(RID p_viewport, Size2i p_size) {
  121. RenderingServer *rs = RenderingServer::get_singleton();
  122. ERR_FAIL_NULL(rs);
  123. if (viewport != p_viewport) {
  124. if (viewport.is_valid()) {
  125. RID rt = rs->viewport_get_render_target(viewport);
  126. RSG::texture_storage->render_target_set_override(rt, RID(), RID(), RID());
  127. }
  128. viewport = p_viewport;
  129. if (viewport.is_valid()) {
  130. viewport_size = p_size;
  131. } else {
  132. free_swapchain();
  133. viewport_size = Size2i();
  134. }
  135. }
  136. }
  137. void OpenXRViewportCompositionLayerProvider::set_extension_property_values(const Dictionary &p_extension_property_values) {
  138. extension_property_values = p_extension_property_values;
  139. extension_property_values_changed = true;
  140. }
  141. void OpenXRViewportCompositionLayerProvider::on_pre_render() {
  142. RenderingServer *rs = RenderingServer::get_singleton();
  143. ERR_FAIL_NULL(rs);
  144. if (viewport.is_valid() && openxr_api && openxr_api->is_running()) {
  145. RS::ViewportUpdateMode update_mode = rs->viewport_get_update_mode(viewport);
  146. if (update_mode == RS::VIEWPORT_UPDATE_ONCE || update_mode == RS::VIEWPORT_UPDATE_ALWAYS) {
  147. // Update our XR swapchain
  148. if (update_and_acquire_swapchain(update_mode == RS::VIEWPORT_UPDATE_ONCE)) {
  149. // Render to our XR swapchain image.
  150. RID rt = rs->viewport_get_render_target(viewport);
  151. RSG::texture_storage->render_target_set_override(rt, get_current_swapchain_texture(), RID(), RID());
  152. }
  153. }
  154. }
  155. }
  156. XrCompositionLayerBaseHeader *OpenXRViewportCompositionLayerProvider::get_composition_layer() {
  157. if (openxr_api == nullptr || composition_layer_extension == nullptr) {
  158. // OpenXR not initialized or we're in the editor?
  159. return nullptr;
  160. }
  161. if (!composition_layer_extension->is_available(composition_layer->type)) {
  162. // Selected type is not supported, ignore our layer.
  163. return nullptr;
  164. }
  165. if (swapchain_info.get_swapchain() == XR_NULL_HANDLE) {
  166. // Don't have a swapchain to display? Ignore our layer.
  167. return nullptr;
  168. }
  169. if (swapchain_info.is_image_acquired()) {
  170. swapchain_info.release();
  171. }
  172. // Update the layer struct for the swapchain.
  173. switch (composition_layer->type) {
  174. case XR_TYPE_COMPOSITION_LAYER_QUAD: {
  175. XrCompositionLayerQuad *quad_layer = (XrCompositionLayerQuad *)composition_layer;
  176. quad_layer->space = openxr_api->get_play_space();
  177. quad_layer->subImage.swapchain = swapchain_info.get_swapchain();
  178. quad_layer->subImage.imageArrayIndex = 0;
  179. quad_layer->subImage.imageRect.offset.x = 0;
  180. quad_layer->subImage.imageRect.offset.y = 0;
  181. quad_layer->subImage.imageRect.extent.width = swapchain_size.width;
  182. quad_layer->subImage.imageRect.extent.height = swapchain_size.height;
  183. } break;
  184. case XR_TYPE_COMPOSITION_LAYER_CYLINDER_KHR: {
  185. XrCompositionLayerCylinderKHR *cylinder_layer = (XrCompositionLayerCylinderKHR *)composition_layer;
  186. cylinder_layer->space = openxr_api->get_play_space();
  187. cylinder_layer->subImage.swapchain = swapchain_info.get_swapchain();
  188. cylinder_layer->subImage.imageArrayIndex = 0;
  189. cylinder_layer->subImage.imageRect.offset.x = 0;
  190. cylinder_layer->subImage.imageRect.offset.y = 0;
  191. cylinder_layer->subImage.imageRect.extent.width = swapchain_size.width;
  192. cylinder_layer->subImage.imageRect.extent.height = swapchain_size.height;
  193. } break;
  194. case XR_TYPE_COMPOSITION_LAYER_EQUIRECT2_KHR: {
  195. XrCompositionLayerEquirect2KHR *equirect_layer = (XrCompositionLayerEquirect2KHR *)composition_layer;
  196. equirect_layer->space = openxr_api->get_play_space();
  197. equirect_layer->subImage.swapchain = swapchain_info.get_swapchain();
  198. equirect_layer->subImage.imageArrayIndex = 0;
  199. equirect_layer->subImage.imageRect.offset.x = 0;
  200. equirect_layer->subImage.imageRect.offset.y = 0;
  201. equirect_layer->subImage.imageRect.extent.width = swapchain_size.width;
  202. equirect_layer->subImage.imageRect.extent.height = swapchain_size.height;
  203. } break;
  204. default: {
  205. return nullptr;
  206. } break;
  207. }
  208. if (extension_property_values_changed) {
  209. extension_property_values_changed = false;
  210. void *next_pointer = nullptr;
  211. for (OpenXRExtensionWrapper *extension : OpenXRAPI::get_registered_extension_wrappers()) {
  212. void *np = extension->set_viewport_composition_layer_and_get_next_pointer(composition_layer, extension_property_values, next_pointer);
  213. if (np) {
  214. next_pointer = np;
  215. }
  216. }
  217. composition_layer->next = next_pointer;
  218. }
  219. return composition_layer;
  220. }
  221. bool OpenXRViewportCompositionLayerProvider::update_and_acquire_swapchain(bool p_static_image) {
  222. if (openxr_api == nullptr || composition_layer_extension == nullptr) {
  223. // OpenXR not initialized or we're in the editor?
  224. return false;
  225. }
  226. if (!composition_layer_extension->is_available(composition_layer->type)) {
  227. // Selected type is not supported?
  228. return false;
  229. }
  230. // See if our current swapchain is outdated.
  231. if (swapchain_info.get_swapchain() != XR_NULL_HANDLE) {
  232. // If this swap chain, or the previous one, were static, then we can't reuse it.
  233. if (swapchain_size == viewport_size && !p_static_image && !static_image) {
  234. // We're all good! Just acquire it.
  235. // We can ignore should_render here, return will be false.
  236. bool should_render = true;
  237. return swapchain_info.acquire(should_render);
  238. }
  239. swapchain_info.queue_free();
  240. }
  241. // Create our new swap chain
  242. int64_t swapchain_format = openxr_api->get_color_swapchain_format();
  243. const uint32_t sample_count = 1;
  244. const uint32_t array_size = 1;
  245. XrSwapchainCreateFlags create_flags = 0;
  246. if (p_static_image) {
  247. create_flags |= XR_SWAPCHAIN_CREATE_STATIC_IMAGE_BIT;
  248. }
  249. if (!swapchain_info.create(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)) {
  250. swapchain_size = Size2i();
  251. return false;
  252. }
  253. // Acquire our image so we can start rendering into it,
  254. // we can ignore should_render here, ret will be false.
  255. bool should_render = true;
  256. bool ret = swapchain_info.acquire(should_render);
  257. swapchain_size = viewport_size;
  258. static_image = p_static_image;
  259. return ret;
  260. }
  261. void OpenXRViewportCompositionLayerProvider::free_swapchain() {
  262. if (swapchain_info.get_swapchain() != XR_NULL_HANDLE) {
  263. swapchain_info.queue_free();
  264. }
  265. swapchain_size = Size2i();
  266. static_image = false;
  267. }
  268. RID OpenXRViewportCompositionLayerProvider::get_current_swapchain_texture() {
  269. if (openxr_api == nullptr) {
  270. return RID();
  271. }
  272. return swapchain_info.get_image();
  273. }