openxr_future_extension.cpp 9.2 KB

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