openxr_future_extension.cpp 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254
  1. /**************************************************************************/
  2. /* openxr_future_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_future_extension.h"
  31. #include "../openxr_api.h"
  32. ////////////////////////////////////////////////////////////////////////////
  33. // OpenXRFutureResult
  34. void OpenXRFutureResult::_bind_methods() {
  35. ClassDB::bind_method(D_METHOD("get_status"), &OpenXRFutureResult::get_status);
  36. ClassDB::bind_method(D_METHOD("get_future"), &OpenXRFutureResult::_get_future);
  37. ClassDB::bind_method(D_METHOD("cancel_future"), &OpenXRFutureResult::cancel_future);
  38. ADD_SIGNAL(MethodInfo("completed", PropertyInfo(Variant::OBJECT, "result", PROPERTY_HINT_RESOURCE_TYPE, "OpenXRFutureResult")));
  39. BIND_ENUM_CONSTANT(RESULT_RUNNING);
  40. BIND_ENUM_CONSTANT(RESULT_FINISHED);
  41. BIND_ENUM_CONSTANT(RESULT_CANCELLED);
  42. }
  43. void OpenXRFutureResult::_mark_as_finished() {
  44. // Update our status
  45. status = RESULT_FINISHED;
  46. // Perform our callback
  47. on_success_callback.call((uint64_t)future);
  48. // Emit our signal
  49. emit_signal(SNAME("completed"), this);
  50. }
  51. void OpenXRFutureResult::_mark_as_cancelled() {
  52. // Update our status
  53. status = RESULT_CANCELLED;
  54. // There is no point in doing a callback for cancellation as its always user invoked.
  55. // But we do emit our signal to make sure any await finishes.
  56. emit_signal(SNAME("completed"), this);
  57. }
  58. OpenXRFutureResult::ResultStatus OpenXRFutureResult::get_status() const {
  59. return status;
  60. }
  61. XrFutureEXT OpenXRFutureResult::get_future() const {
  62. return future;
  63. }
  64. uint64_t OpenXRFutureResult::_get_future() const {
  65. return (uint64_t)future;
  66. }
  67. void OpenXRFutureResult::cancel_future() {
  68. ERR_FAIL_COND(status != RESULT_RUNNING);
  69. OpenXRFutureExtension *future_extension = OpenXRFutureExtension::get_singleton();
  70. ERR_FAIL_NULL(future_extension);
  71. future_extension->cancel_future(future);
  72. }
  73. OpenXRFutureResult::OpenXRFutureResult(XrFutureEXT p_future, const Callable &p_on_success) {
  74. future = p_future;
  75. on_success_callback = p_on_success;
  76. }
  77. ////////////////////////////////////////////////////////////////////////////
  78. // OpenXRFutureExtension
  79. OpenXRFutureExtension *OpenXRFutureExtension::singleton = nullptr;
  80. OpenXRFutureExtension *OpenXRFutureExtension::get_singleton() {
  81. return singleton;
  82. }
  83. OpenXRFutureExtension::OpenXRFutureExtension() {
  84. singleton = this;
  85. }
  86. OpenXRFutureExtension::~OpenXRFutureExtension() {
  87. singleton = nullptr;
  88. }
  89. void OpenXRFutureExtension::_bind_methods() {
  90. ClassDB::bind_method(D_METHOD("is_active"), &OpenXRFutureExtension::is_active);
  91. ClassDB::bind_method(D_METHOD("register_future", "future", "on_success"), &OpenXRFutureExtension::_register_future, DEFVAL(Callable()));
  92. ClassDB::bind_method(D_METHOD("cancel_future", "future"), &OpenXRFutureExtension::_cancel_future);
  93. }
  94. HashMap<String, bool *> OpenXRFutureExtension::get_requested_extensions() {
  95. HashMap<String, bool *> request_extensions;
  96. request_extensions[XR_EXT_FUTURE_EXTENSION_NAME] = &future_ext;
  97. return request_extensions;
  98. }
  99. void OpenXRFutureExtension::on_instance_created(const XrInstance p_instance) {
  100. if (future_ext) {
  101. EXT_INIT_XR_FUNC(xrPollFutureEXT);
  102. EXT_INIT_XR_FUNC(xrCancelFutureEXT);
  103. future_ext = xrPollFutureEXT_ptr && xrCancelFutureEXT_ptr;
  104. }
  105. }
  106. void OpenXRFutureExtension::on_instance_destroyed() {
  107. xrPollFutureEXT_ptr = nullptr;
  108. xrCancelFutureEXT_ptr = nullptr;
  109. }
  110. void OpenXRFutureExtension::on_session_destroyed() {
  111. if (!is_active()) {
  112. return;
  113. }
  114. OpenXRAPI *openxr_api = OpenXRAPI::get_singleton();
  115. ERR_FAIL_NULL(openxr_api);
  116. // Cancel any running futures.
  117. for (const KeyValue<XrFutureEXT, Ref<OpenXRFutureResult>> &element : futures) {
  118. XrFutureCancelInfoEXT cancel_info = {
  119. XR_TYPE_FUTURE_CANCEL_INFO_EXT, // type
  120. nullptr, // next
  121. element.key // future
  122. };
  123. XrResult result = xrCancelFutureEXT_ptr(openxr_api->get_instance(), &cancel_info);
  124. if (XR_FAILED(result)) {
  125. WARN_PRINT("OpenXR: Failed to cancel future [" + openxr_api->get_error_string(result) + "]");
  126. }
  127. // Make sure we mark our future result as cancelled
  128. element.value->_mark_as_cancelled();
  129. }
  130. futures.clear();
  131. }
  132. bool OpenXRFutureExtension::is_active() const {
  133. return future_ext;
  134. }
  135. Ref<OpenXRFutureResult> OpenXRFutureExtension::register_future(XrFutureEXT p_future, const Callable &p_on_success) {
  136. ERR_FAIL_COND_V(futures.has(p_future), nullptr);
  137. Ref<OpenXRFutureResult> future_result;
  138. future_result.instantiate(p_future, p_on_success);
  139. futures[p_future] = future_result;
  140. return future_result;
  141. }
  142. Ref<OpenXRFutureResult> OpenXRFutureExtension::_register_future(uint64_t p_future, const Callable &p_on_success) {
  143. return register_future((XrFutureEXT)p_future, p_on_success);
  144. }
  145. void OpenXRFutureExtension::cancel_future(XrFutureEXT p_future) {
  146. ERR_FAIL_COND(!futures.has(p_future));
  147. OpenXRAPI *openxr_api = OpenXRAPI::get_singleton();
  148. ERR_FAIL_NULL(openxr_api);
  149. XrFutureCancelInfoEXT cancel_info = {
  150. XR_TYPE_FUTURE_CANCEL_INFO_EXT, // type
  151. nullptr, // next
  152. p_future // future
  153. };
  154. XrResult result = xrCancelFutureEXT_ptr(openxr_api->get_instance(), &cancel_info);
  155. if (XR_FAILED(result)) {
  156. WARN_PRINT("OpenXR: Failed to cancel future [" + openxr_api->get_error_string(result) + "]");
  157. }
  158. // Make sure we mark our future result as cancelled
  159. futures[p_future]->_mark_as_cancelled();
  160. // And erase it from the futures we track
  161. futures.erase(p_future);
  162. }
  163. void OpenXRFutureExtension::_cancel_future(uint64_t p_future) {
  164. cancel_future((XrFutureEXT)p_future);
  165. }
  166. void OpenXRFutureExtension::on_process() {
  167. if (!is_active()) {
  168. return;
  169. }
  170. OpenXRAPI *openxr_api = OpenXRAPI::get_singleton();
  171. ERR_FAIL_NULL(openxr_api);
  172. // Process futures
  173. Vector<XrFutureEXT> completed;
  174. for (const KeyValue<XrFutureEXT, Ref<OpenXRFutureResult>> &element : futures) {
  175. XrFuturePollInfoEXT poll_info = {
  176. XR_TYPE_FUTURE_POLL_INFO_EXT, // type
  177. nullptr, // next
  178. element.key // future
  179. };
  180. XrFuturePollResultEXT poll_result = {
  181. XR_TYPE_FUTURE_POLL_RESULT_EXT, // type
  182. nullptr, // next
  183. XR_FUTURE_STATE_MAX_ENUM_EXT // state
  184. };
  185. XrResult result = xrPollFutureEXT_ptr(openxr_api->get_instance(), &poll_info, &poll_result);
  186. if (XR_FAILED(result)) {
  187. ERR_PRINT("OpenXR: Failed to obtain future status [" + openxr_api->get_error_string(result) + "]");
  188. // Maybe remove this depending on the error?
  189. continue;
  190. }
  191. if (poll_result.state == XR_FUTURE_STATE_READY_EXT) {
  192. // Mark our future result as finished (this will invoke our callback).
  193. element.value->_mark_as_finished();
  194. // Queue removing this
  195. completed.push_back(element.key);
  196. }
  197. }
  198. // Now clean up completed futures.
  199. for (const XrFutureEXT &future : completed) {
  200. futures.erase(future);
  201. }
  202. }