openxr_composition_layer_extension.cpp 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494
  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. #ifdef ANDROID_ENABLED
  32. #include <openxr/openxr.h>
  33. #include <openxr/openxr_platform.h>
  34. #endif
  35. #include "platform/android/api/java_class_wrapper.h"
  36. #include "servers/rendering/rendering_server_globals.h"
  37. ////////////////////////////////////////////////////////////////////////////
  38. // OpenXRCompositionLayerExtension
  39. OpenXRCompositionLayerExtension *OpenXRCompositionLayerExtension::singleton = nullptr;
  40. OpenXRCompositionLayerExtension *OpenXRCompositionLayerExtension::get_singleton() {
  41. return singleton;
  42. }
  43. OpenXRCompositionLayerExtension::OpenXRCompositionLayerExtension() {
  44. singleton = this;
  45. }
  46. OpenXRCompositionLayerExtension::~OpenXRCompositionLayerExtension() {
  47. singleton = nullptr;
  48. }
  49. HashMap<String, bool *> OpenXRCompositionLayerExtension::get_requested_extensions() {
  50. HashMap<String, bool *> request_extensions;
  51. request_extensions[XR_KHR_COMPOSITION_LAYER_CYLINDER_EXTENSION_NAME] = &cylinder_ext_available;
  52. request_extensions[XR_KHR_COMPOSITION_LAYER_EQUIRECT2_EXTENSION_NAME] = &equirect_ext_available;
  53. #ifdef ANDROID_ENABLED
  54. request_extensions[XR_KHR_ANDROID_SURFACE_SWAPCHAIN_EXTENSION_NAME] = &android_surface_ext_available;
  55. #endif
  56. return request_extensions;
  57. }
  58. void OpenXRCompositionLayerExtension::on_instance_created(const XrInstance p_instance) {
  59. #ifdef ANDROID_ENABLED
  60. EXT_INIT_XR_FUNC(xrDestroySwapchain);
  61. EXT_INIT_XR_FUNC(xrCreateSwapchainAndroidSurfaceKHR);
  62. #endif
  63. }
  64. void OpenXRCompositionLayerExtension::on_session_created(const XrSession p_session) {
  65. OpenXRAPI::get_singleton()->register_composition_layer_provider(this);
  66. }
  67. void OpenXRCompositionLayerExtension::on_session_destroyed() {
  68. OpenXRAPI::get_singleton()->unregister_composition_layer_provider(this);
  69. #ifdef ANDROID_ENABLED
  70. free_queued_android_surface_swapchains();
  71. #endif
  72. }
  73. void OpenXRCompositionLayerExtension::on_pre_render() {
  74. #ifdef ANDROID_ENABLED
  75. free_queued_android_surface_swapchains();
  76. #endif
  77. for (OpenXRViewportCompositionLayerProvider *composition_layer : composition_layers) {
  78. composition_layer->on_pre_render();
  79. }
  80. }
  81. int OpenXRCompositionLayerExtension::get_composition_layer_count() {
  82. return composition_layers.size();
  83. }
  84. XrCompositionLayerBaseHeader *OpenXRCompositionLayerExtension::get_composition_layer(int p_index) {
  85. ERR_FAIL_INDEX_V(p_index, composition_layers.size(), nullptr);
  86. return composition_layers[p_index]->get_composition_layer();
  87. }
  88. int OpenXRCompositionLayerExtension::get_composition_layer_order(int p_index) {
  89. ERR_FAIL_INDEX_V(p_index, composition_layers.size(), 1);
  90. return composition_layers[p_index]->get_sort_order();
  91. }
  92. void OpenXRCompositionLayerExtension::register_viewport_composition_layer_provider(OpenXRViewportCompositionLayerProvider *p_composition_layer) {
  93. composition_layers.push_back(p_composition_layer);
  94. }
  95. void OpenXRCompositionLayerExtension::unregister_viewport_composition_layer_provider(OpenXRViewportCompositionLayerProvider *p_composition_layer) {
  96. composition_layers.erase(p_composition_layer);
  97. }
  98. bool OpenXRCompositionLayerExtension::is_available(XrStructureType p_which) {
  99. switch (p_which) {
  100. case XR_TYPE_COMPOSITION_LAYER_QUAD: {
  101. // Doesn't require an extension.
  102. return true;
  103. } break;
  104. case XR_TYPE_COMPOSITION_LAYER_CYLINDER_KHR: {
  105. return cylinder_ext_available;
  106. } break;
  107. case XR_TYPE_COMPOSITION_LAYER_EQUIRECT2_KHR: {
  108. return equirect_ext_available;
  109. } break;
  110. default: {
  111. ERR_PRINT(vformat("Unsupported composition layer type: %s", p_which));
  112. return false;
  113. }
  114. }
  115. }
  116. #ifdef ANDROID_ENABLED
  117. bool OpenXRCompositionLayerExtension::create_android_surface_swapchain(XrSwapchainCreateInfo *p_info, XrSwapchain *r_swapchain, jobject *r_surface) {
  118. if (android_surface_ext_available) {
  119. OpenXRAPI *openxr_api = OpenXRAPI::get_singleton();
  120. ERR_FAIL_NULL_V(openxr_api, false);
  121. XrResult result = xrCreateSwapchainAndroidSurfaceKHR(openxr_api->get_session(), p_info, r_swapchain, r_surface);
  122. if (XR_FAILED(result)) {
  123. print_line("OpenXR: Failed to create Android surface swapchain [", openxr_api->get_error_string(result), "]");
  124. return false;
  125. }
  126. return true;
  127. }
  128. return false;
  129. }
  130. void OpenXRCompositionLayerExtension::free_android_surface_swapchain(XrSwapchain p_swapchain) {
  131. android_surface_swapchain_free_queue.push_back(p_swapchain);
  132. }
  133. void OpenXRCompositionLayerExtension::free_queued_android_surface_swapchains() {
  134. for (XrSwapchain swapchain : android_surface_swapchain_free_queue) {
  135. xrDestroySwapchain(swapchain);
  136. }
  137. android_surface_swapchain_free_queue.clear();
  138. }
  139. #endif
  140. ////////////////////////////////////////////////////////////////////////////
  141. // OpenXRViewportCompositionLayerProvider
  142. OpenXRViewportCompositionLayerProvider::OpenXRViewportCompositionLayerProvider(XrCompositionLayerBaseHeader *p_composition_layer) {
  143. composition_layer = p_composition_layer;
  144. openxr_api = OpenXRAPI::get_singleton();
  145. composition_layer_extension = OpenXRCompositionLayerExtension::get_singleton();
  146. }
  147. OpenXRViewportCompositionLayerProvider::~OpenXRViewportCompositionLayerProvider() {
  148. for (OpenXRExtensionWrapper *extension : OpenXRAPI::get_registered_extension_wrappers()) {
  149. extension->on_viewport_composition_layer_destroyed(composition_layer);
  150. }
  151. if (use_android_surface) {
  152. free_swapchain();
  153. } else {
  154. // This will reset the viewport and free the swapchain too.
  155. set_viewport(RID(), Size2i());
  156. }
  157. }
  158. void OpenXRViewportCompositionLayerProvider::set_alpha_blend(bool p_alpha_blend) {
  159. if (alpha_blend != p_alpha_blend) {
  160. alpha_blend = p_alpha_blend;
  161. if (alpha_blend) {
  162. composition_layer->layerFlags |= XR_COMPOSITION_LAYER_BLEND_TEXTURE_SOURCE_ALPHA_BIT;
  163. } else {
  164. composition_layer->layerFlags &= ~XR_COMPOSITION_LAYER_BLEND_TEXTURE_SOURCE_ALPHA_BIT;
  165. }
  166. }
  167. }
  168. void OpenXRViewportCompositionLayerProvider::set_viewport(RID p_viewport, Size2i p_size) {
  169. ERR_FAIL_COND(use_android_surface);
  170. RenderingServer *rs = RenderingServer::get_singleton();
  171. ERR_FAIL_NULL(rs);
  172. if (subviewport.viewport != p_viewport) {
  173. if (subviewport.viewport.is_valid()) {
  174. RID rt = rs->viewport_get_render_target(subviewport.viewport);
  175. RSG::texture_storage->render_target_set_override(rt, RID(), RID(), RID());
  176. }
  177. subviewport.viewport = p_viewport;
  178. if (subviewport.viewport.is_valid()) {
  179. subviewport.viewport_size = p_size;
  180. } else {
  181. free_swapchain();
  182. subviewport.viewport_size = Size2i();
  183. }
  184. }
  185. }
  186. void OpenXRViewportCompositionLayerProvider::set_use_android_surface(bool p_use_android_surface, Size2i p_size) {
  187. #ifdef ANDROID_ENABLED
  188. if (p_use_android_surface == use_android_surface) {
  189. return;
  190. }
  191. use_android_surface = p_use_android_surface;
  192. if (use_android_surface) {
  193. if (!composition_layer_extension->is_android_surface_swapchain_available()) {
  194. ERR_PRINT_ONCE("OpenXR: Cannot use Android surface for composition layer because the extension isn't available");
  195. }
  196. if (subviewport.viewport.is_valid()) {
  197. set_viewport(RID(), Size2i());
  198. }
  199. swapchain_size = p_size;
  200. } else {
  201. free_swapchain();
  202. }
  203. #endif
  204. }
  205. #ifdef ANDROID_ENABLED
  206. void OpenXRViewportCompositionLayerProvider::create_android_surface() {
  207. ERR_FAIL_COND(android_surface.swapchain != XR_NULL_HANDLE || android_surface.surface.is_valid());
  208. ERR_FAIL_COND(!openxr_api || !openxr_api->is_running());
  209. void *next_pointer = nullptr;
  210. for (OpenXRExtensionWrapper *wrapper : openxr_api->get_registered_extension_wrappers()) {
  211. void *np = wrapper->set_android_surface_swapchain_create_info_and_get_next_pointer(extension_property_values, next_pointer);
  212. if (np != nullptr) {
  213. next_pointer = np;
  214. }
  215. }
  216. // The XR_FB_android_surface_swapchain_create extension mandates that format, sampleCount,
  217. // faceCount, arraySize, and mipCount must be zero.
  218. XrSwapchainCreateInfo info = {
  219. XR_TYPE_SWAPCHAIN_CREATE_INFO, // type
  220. next_pointer, // next
  221. 0, // createFlags
  222. XR_SWAPCHAIN_USAGE_SAMPLED_BIT | XR_SWAPCHAIN_USAGE_COLOR_ATTACHMENT_BIT | XR_SWAPCHAIN_USAGE_MUTABLE_FORMAT_BIT, // usageFlags
  223. 0, // format
  224. 0, // sampleCount
  225. (uint32_t)swapchain_size.x, // width
  226. (uint32_t)swapchain_size.y, // height
  227. 0, // faceCount
  228. 0, // arraySize
  229. 0, // mipCount
  230. };
  231. jobject surface;
  232. composition_layer_extension->create_android_surface_swapchain(&info, &android_surface.swapchain, &surface);
  233. if (surface) {
  234. android_surface.surface.instantiate(JavaClassWrapper::get_singleton()->wrap("android.view.Surface"), surface);
  235. }
  236. }
  237. #endif
  238. Ref<JavaObject> OpenXRViewportCompositionLayerProvider::get_android_surface() {
  239. #ifdef ANDROID_ENABLED
  240. if (use_android_surface) {
  241. if (android_surface.surface.is_null()) {
  242. create_android_surface();
  243. }
  244. return android_surface.surface;
  245. }
  246. #endif
  247. return Ref<JavaObject>();
  248. }
  249. void OpenXRViewportCompositionLayerProvider::set_extension_property_values(const Dictionary &p_extension_property_values) {
  250. extension_property_values = p_extension_property_values;
  251. extension_property_values_changed = true;
  252. }
  253. void OpenXRViewportCompositionLayerProvider::on_pre_render() {
  254. #ifdef ANDROID_ENABLED
  255. if (use_android_surface) {
  256. if (android_surface.surface.is_null()) {
  257. create_android_surface();
  258. }
  259. return;
  260. }
  261. #endif
  262. RenderingServer *rs = RenderingServer::get_singleton();
  263. ERR_FAIL_NULL(rs);
  264. if (subviewport.viewport.is_valid() && openxr_api && openxr_api->is_running()) {
  265. RS::ViewportUpdateMode update_mode = rs->viewport_get_update_mode(subviewport.viewport);
  266. if (update_mode == RS::VIEWPORT_UPDATE_ONCE || update_mode == RS::VIEWPORT_UPDATE_ALWAYS) {
  267. // Update our XR swapchain
  268. if (update_and_acquire_swapchain(update_mode == RS::VIEWPORT_UPDATE_ONCE)) {
  269. // Render to our XR swapchain image.
  270. RID rt = rs->viewport_get_render_target(subviewport.viewport);
  271. RSG::texture_storage->render_target_set_override(rt, get_current_swapchain_texture(), RID(), RID());
  272. }
  273. }
  274. }
  275. }
  276. XrCompositionLayerBaseHeader *OpenXRViewportCompositionLayerProvider::get_composition_layer() {
  277. if (openxr_api == nullptr || composition_layer_extension == nullptr) {
  278. // OpenXR not initialized or we're in the editor?
  279. return nullptr;
  280. }
  281. if (!composition_layer_extension->is_available(composition_layer->type)) {
  282. // Selected type is not supported, ignore our layer.
  283. return nullptr;
  284. }
  285. XrSwapchainSubImage subimage = {
  286. 0, // swapchain // NOLINT(modernize-use-nullptr) - 32-bit uses non-pointer uint64
  287. { { 0, 0 }, { 0, 0 } }, // imageRect
  288. 0, // imageArrayIndex
  289. };
  290. update_swapchain_sub_image(subimage);
  291. if (subimage.swapchain == XR_NULL_HANDLE) {
  292. // Don't have a swapchain to display? Ignore our layer.
  293. return nullptr;
  294. }
  295. // Update the layer struct for the swapchain.
  296. switch (composition_layer->type) {
  297. case XR_TYPE_COMPOSITION_LAYER_QUAD: {
  298. XrCompositionLayerQuad *quad_layer = (XrCompositionLayerQuad *)composition_layer;
  299. quad_layer->space = openxr_api->get_play_space();
  300. quad_layer->subImage = subimage;
  301. } break;
  302. case XR_TYPE_COMPOSITION_LAYER_CYLINDER_KHR: {
  303. XrCompositionLayerCylinderKHR *cylinder_layer = (XrCompositionLayerCylinderKHR *)composition_layer;
  304. cylinder_layer->space = openxr_api->get_play_space();
  305. cylinder_layer->subImage = subimage;
  306. } break;
  307. case XR_TYPE_COMPOSITION_LAYER_EQUIRECT2_KHR: {
  308. XrCompositionLayerEquirect2KHR *equirect_layer = (XrCompositionLayerEquirect2KHR *)composition_layer;
  309. equirect_layer->space = openxr_api->get_play_space();
  310. equirect_layer->subImage = subimage;
  311. } break;
  312. default: {
  313. return nullptr;
  314. } break;
  315. }
  316. if (extension_property_values_changed) {
  317. extension_property_values_changed = false;
  318. void *next_pointer = nullptr;
  319. for (OpenXRExtensionWrapper *extension : OpenXRAPI::get_registered_extension_wrappers()) {
  320. void *np = extension->set_viewport_composition_layer_and_get_next_pointer(composition_layer, extension_property_values, next_pointer);
  321. if (np) {
  322. next_pointer = np;
  323. }
  324. }
  325. composition_layer->next = next_pointer;
  326. }
  327. return composition_layer;
  328. }
  329. void OpenXRViewportCompositionLayerProvider::update_swapchain_sub_image(XrSwapchainSubImage &r_subimage) {
  330. #ifdef ANDROID_ENABLED
  331. if (use_android_surface) {
  332. r_subimage.swapchain = android_surface.swapchain;
  333. } else
  334. #endif
  335. {
  336. XrSwapchain swapchain = subviewport.swapchain_info.get_swapchain();
  337. if (swapchain && subviewport.swapchain_info.is_image_acquired()) {
  338. subviewport.swapchain_info.release();
  339. }
  340. r_subimage.swapchain = swapchain;
  341. }
  342. r_subimage.imageRect.extent.width = swapchain_size.width;
  343. r_subimage.imageRect.extent.height = swapchain_size.height;
  344. }
  345. bool OpenXRViewportCompositionLayerProvider::update_and_acquire_swapchain(bool p_static_image) {
  346. ERR_FAIL_COND_V(use_android_surface, false);
  347. if (openxr_api == nullptr || composition_layer_extension == nullptr) {
  348. // OpenXR not initialized or we're in the editor?
  349. return false;
  350. }
  351. if (!composition_layer_extension->is_available(get_openxr_type())) {
  352. // Selected type is not supported?
  353. return false;
  354. }
  355. // See if our current swapchain is outdated.
  356. if (subviewport.swapchain_info.get_swapchain() != XR_NULL_HANDLE) {
  357. // If this swap chain, or the previous one, were static, then we can't reuse it.
  358. if (swapchain_size == subviewport.viewport_size && !p_static_image && !subviewport.static_image) {
  359. // We're all good! Just acquire it.
  360. // We can ignore should_render here, return will be false.
  361. bool should_render = true;
  362. return subviewport.swapchain_info.acquire(should_render);
  363. }
  364. subviewport.swapchain_info.queue_free();
  365. }
  366. // Create our new swap chain
  367. int64_t swapchain_format = openxr_api->get_color_swapchain_format();
  368. const uint32_t sample_count = 1;
  369. const uint32_t array_size = 1;
  370. XrSwapchainCreateFlags create_flags = 0;
  371. if (p_static_image) {
  372. create_flags |= XR_SWAPCHAIN_CREATE_STATIC_IMAGE_BIT;
  373. }
  374. if (!subviewport.swapchain_info.create(create_flags, XR_SWAPCHAIN_USAGE_SAMPLED_BIT | XR_SWAPCHAIN_USAGE_COLOR_ATTACHMENT_BIT | XR_SWAPCHAIN_USAGE_MUTABLE_FORMAT_BIT, swapchain_format, subviewport.viewport_size.width, subviewport.viewport_size.height, sample_count, array_size)) {
  375. swapchain_size = Size2i();
  376. return false;
  377. }
  378. // Acquire our image so we can start rendering into it,
  379. // we can ignore should_render here, ret will be false.
  380. bool should_render = true;
  381. bool ret = subviewport.swapchain_info.acquire(should_render);
  382. swapchain_size = subviewport.viewport_size;
  383. subviewport.static_image = p_static_image;
  384. return ret;
  385. }
  386. void OpenXRViewportCompositionLayerProvider::free_swapchain() {
  387. #ifdef ANDROID_ENABLED
  388. if (use_android_surface) {
  389. if (android_surface.swapchain != XR_NULL_HANDLE) {
  390. composition_layer_extension->free_android_surface_swapchain(android_surface.swapchain);
  391. android_surface.swapchain = XR_NULL_HANDLE;
  392. android_surface.surface.unref();
  393. }
  394. } else
  395. #endif
  396. {
  397. if (subviewport.swapchain_info.get_swapchain() != XR_NULL_HANDLE) {
  398. subviewport.swapchain_info.queue_free();
  399. }
  400. subviewport.static_image = false;
  401. }
  402. swapchain_size = Size2i();
  403. }
  404. RID OpenXRViewportCompositionLayerProvider::get_current_swapchain_texture() {
  405. ERR_FAIL_COND_V(use_android_surface, RID());
  406. if (openxr_api == nullptr) {
  407. return RID();
  408. }
  409. return subviewport.swapchain_info.get_image();
  410. }