xr_interface_extension.cpp 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335
  1. /*************************************************************************/
  2. /* xr_interface_extension.cpp */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2021 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2021 Godot Engine contributors (cf. AUTHORS.md). */
  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 "xr_interface_extension.h"
  31. #include "servers/rendering/renderer_rd/renderer_storage_rd.h"
  32. #include "servers/rendering/renderer_storage.h"
  33. #include "servers/rendering/rendering_server_globals.h"
  34. void XRInterfaceExtension::_bind_methods() {
  35. GDVIRTUAL_BIND(_get_name);
  36. GDVIRTUAL_BIND(_get_capabilities);
  37. GDVIRTUAL_BIND(_is_initialized);
  38. GDVIRTUAL_BIND(_initialize);
  39. GDVIRTUAL_BIND(_uninitialize);
  40. GDVIRTUAL_BIND(_supports_play_area_mode, "mode");
  41. GDVIRTUAL_BIND(_get_play_area_mode);
  42. GDVIRTUAL_BIND(_set_play_area_mode, "mode");
  43. GDVIRTUAL_BIND(_get_play_area);
  44. GDVIRTUAL_BIND(_get_render_target_size);
  45. GDVIRTUAL_BIND(_get_view_count);
  46. GDVIRTUAL_BIND(_get_camera_transform);
  47. GDVIRTUAL_BIND(_get_transform_for_view, "view", "cam_transform");
  48. GDVIRTUAL_BIND(_get_projection_for_view, "view", "aspect", "z_near", "z_far");
  49. GDVIRTUAL_BIND(_commit_views, "render_target", "screen_rect");
  50. GDVIRTUAL_BIND(_process);
  51. GDVIRTUAL_BIND(_notification, "what");
  52. /** input and output **/
  53. GDVIRTUAL_BIND(_get_suggested_tracker_names);
  54. GDVIRTUAL_BIND(_get_suggested_pose_names, "tracker_name");
  55. GDVIRTUAL_BIND(_get_tracking_status);
  56. GDVIRTUAL_BIND(_trigger_haptic_pulse, "action_name", "tracker_name", "frequency", "amplitude", "duration_sec", "delay_sec");
  57. // we don't have any properties specific to VR yet....
  58. // but we do have properties specific to AR....
  59. GDVIRTUAL_BIND(_get_anchor_detection_is_enabled);
  60. GDVIRTUAL_BIND(_set_anchor_detection_is_enabled, "enabled");
  61. GDVIRTUAL_BIND(_get_camera_feed_id);
  62. // helper methods
  63. ClassDB::bind_method(D_METHOD("add_blit", "render_target", "src_rect", "dst_rect", "use_layer", "layer", "apply_lens_distortion", "eye_center", "k1", "k2", "upscale", "aspect_ratio"), &XRInterfaceExtension::add_blit);
  64. ClassDB::bind_method(D_METHOD("get_render_target_texture", "render_target"), &XRInterfaceExtension::get_render_target_texture);
  65. // ClassDB::bind_method(D_METHOD("get_render_target_depth", "render_target"), &XRInterfaceExtension::get_render_target_depth);
  66. }
  67. StringName XRInterfaceExtension::get_name() const {
  68. StringName name;
  69. if (GDVIRTUAL_CALL(_get_name, name)) {
  70. return name;
  71. }
  72. return "Unknown";
  73. }
  74. uint32_t XRInterfaceExtension::get_capabilities() const {
  75. uint32_t capabilities;
  76. if (GDVIRTUAL_CALL(_get_capabilities, capabilities)) {
  77. return capabilities;
  78. }
  79. return 0;
  80. }
  81. bool XRInterfaceExtension::is_initialized() const {
  82. bool initialised = false;
  83. if (GDVIRTUAL_CALL(_is_initialized, initialised)) {
  84. return initialised;
  85. }
  86. return false;
  87. }
  88. bool XRInterfaceExtension::initialize() {
  89. bool initialised = false;
  90. if (GDVIRTUAL_CALL(_initialize, initialised)) {
  91. return initialised;
  92. }
  93. return false;
  94. }
  95. void XRInterfaceExtension::uninitialize() {
  96. GDVIRTUAL_CALL(_uninitialize);
  97. }
  98. PackedStringArray XRInterfaceExtension::get_suggested_tracker_names() const {
  99. PackedStringArray arr;
  100. GDVIRTUAL_CALL(_get_suggested_tracker_names, arr);
  101. return arr;
  102. }
  103. PackedStringArray XRInterfaceExtension::get_suggested_pose_names(const StringName &p_tracker_name) const {
  104. PackedStringArray arr;
  105. GDVIRTUAL_CALL(_get_suggested_pose_names, p_tracker_name, arr);
  106. return arr;
  107. }
  108. XRInterface::TrackingStatus XRInterfaceExtension::get_tracking_status() const {
  109. uint32_t status;
  110. if (GDVIRTUAL_CALL(_get_tracking_status, status)) {
  111. return TrackingStatus(status);
  112. }
  113. return XR_UNKNOWN_TRACKING;
  114. }
  115. void XRInterfaceExtension::trigger_haptic_pulse(const String &p_action_name, const StringName &p_tracker_name, double p_frequency, double p_amplitude, double p_duration_sec, double p_delay_sec) {
  116. GDVIRTUAL_CALL(_trigger_haptic_pulse, p_action_name, p_tracker_name, p_frequency, p_amplitude, p_duration_sec, p_delay_sec);
  117. }
  118. bool XRInterfaceExtension::supports_play_area_mode(XRInterface::PlayAreaMode p_mode) {
  119. bool is_supported;
  120. if (GDVIRTUAL_CALL(_supports_play_area_mode, p_mode, is_supported)) {
  121. return is_supported;
  122. }
  123. return false;
  124. }
  125. XRInterface::PlayAreaMode XRInterfaceExtension::get_play_area_mode() const {
  126. uint32_t mode;
  127. if (GDVIRTUAL_CALL(_get_play_area_mode, mode)) {
  128. return XRInterface::PlayAreaMode(mode);
  129. }
  130. return XRInterface::XR_PLAY_AREA_UNKNOWN;
  131. }
  132. bool XRInterfaceExtension::set_play_area_mode(XRInterface::PlayAreaMode p_mode) {
  133. bool success;
  134. if (GDVIRTUAL_CALL(_set_play_area_mode, p_mode, success)) {
  135. return success;
  136. }
  137. return false;
  138. }
  139. PackedVector3Array XRInterfaceExtension::get_play_area() const {
  140. PackedVector3Array arr;
  141. GDVIRTUAL_CALL(_get_play_area, arr);
  142. return arr;
  143. }
  144. /** these will only be implemented on AR interfaces, so we want dummies for VR **/
  145. bool XRInterfaceExtension::get_anchor_detection_is_enabled() const {
  146. bool enabled;
  147. if (GDVIRTUAL_CALL(_get_anchor_detection_is_enabled, enabled)) {
  148. return enabled;
  149. }
  150. return false;
  151. }
  152. void XRInterfaceExtension::set_anchor_detection_is_enabled(bool p_enable) {
  153. // don't do anything here, this needs to be implemented on AR interface to enable/disable things like plane detection etc.
  154. GDVIRTUAL_CALL(_set_anchor_detection_is_enabled, p_enable);
  155. }
  156. int XRInterfaceExtension::get_camera_feed_id() {
  157. int feed_id;
  158. if (GDVIRTUAL_CALL(_get_camera_feed_id, feed_id)) {
  159. return feed_id;
  160. }
  161. return 0;
  162. }
  163. Size2 XRInterfaceExtension::get_render_target_size() {
  164. Size2 size;
  165. if (GDVIRTUAL_CALL(_get_render_target_size, size)) {
  166. return size;
  167. }
  168. return Size2(0, 0);
  169. }
  170. uint32_t XRInterfaceExtension::get_view_count() {
  171. uint32_t view_count;
  172. if (GDVIRTUAL_CALL(_get_view_count, view_count)) {
  173. return view_count;
  174. }
  175. return 1;
  176. }
  177. Transform3D XRInterfaceExtension::get_camera_transform() {
  178. Transform3D transform;
  179. if (GDVIRTUAL_CALL(_get_camera_transform, transform)) {
  180. return transform;
  181. }
  182. return Transform3D();
  183. }
  184. Transform3D XRInterfaceExtension::get_transform_for_view(uint32_t p_view, const Transform3D &p_cam_transform) {
  185. Transform3D transform;
  186. if (GDVIRTUAL_CALL(_get_transform_for_view, p_view, p_cam_transform, transform)) {
  187. return transform;
  188. }
  189. return Transform3D();
  190. }
  191. CameraMatrix XRInterfaceExtension::get_projection_for_view(uint32_t p_view, double p_aspect, double p_z_near, double p_z_far) {
  192. CameraMatrix cm;
  193. PackedFloat64Array arr;
  194. if (GDVIRTUAL_CALL(_get_projection_for_view, p_view, p_aspect, p_z_near, p_z_far, arr)) {
  195. ERR_FAIL_COND_V_MSG(arr.size() != 16, CameraMatrix(), "Projection matrix must contain 16 floats");
  196. real_t *m = (real_t *)cm.matrix;
  197. for (int i = 0; i < 16; i++) {
  198. m[i] = arr[i];
  199. }
  200. return cm;
  201. }
  202. return CameraMatrix();
  203. }
  204. void XRInterfaceExtension::add_blit(RID p_render_target, Rect2 p_src_rect, Rect2i p_dst_rect, bool p_use_layer, uint32_t p_layer, bool p_apply_lens_distortion, Vector2 p_eye_center, double p_k1, double p_k2, double p_upscale, double p_aspect_ratio) {
  205. BlitToScreen blit;
  206. ERR_FAIL_COND_MSG(!can_add_blits, "add_blit can only be called from an XR plugin from within _commit_views!");
  207. blit.render_target = p_render_target;
  208. blit.src_rect = p_src_rect;
  209. blit.dst_rect = p_dst_rect;
  210. blit.multi_view.use_layer = p_use_layer;
  211. blit.multi_view.layer = p_layer;
  212. blit.lens_distortion.apply = p_apply_lens_distortion;
  213. blit.lens_distortion.eye_center = p_eye_center;
  214. blit.lens_distortion.k1 = p_k1;
  215. blit.lens_distortion.k2 = p_k2;
  216. blit.lens_distortion.upscale = p_upscale;
  217. blit.lens_distortion.aspect_ratio = p_aspect_ratio;
  218. blits.push_back(blit);
  219. }
  220. Vector<BlitToScreen> XRInterfaceExtension::commit_views(RID p_render_target, const Rect2 &p_screen_rect) {
  221. // This is just so our XR plugin can add blits...
  222. blits.clear();
  223. can_add_blits = true;
  224. if (GDVIRTUAL_CALL(_commit_views, p_render_target, p_screen_rect)) {
  225. return blits;
  226. }
  227. can_add_blits = false;
  228. return blits;
  229. }
  230. void XRInterfaceExtension::process() {
  231. GDVIRTUAL_CALL(_process);
  232. }
  233. void XRInterfaceExtension::notification(int p_what) {
  234. GDVIRTUAL_CALL(_notification, p_what);
  235. }
  236. RID XRInterfaceExtension::get_render_target_texture(RID p_render_target) {
  237. // In due time this will need to be enhance to return the correct INTERNAL RID for the chosen rendering engine.
  238. // So once a GLES driver is implemented we'll return that and the implemented plugin needs to handle this correctly too.
  239. RendererStorageRD *rd_storage = RendererStorageRD::base_singleton;
  240. ERR_FAIL_NULL_V_MSG(rd_storage, RID(), "Renderer storage not setup");
  241. return rd_storage->render_target_get_rd_texture(p_render_target);
  242. }
  243. /*
  244. RID XRInterfaceExtension::get_render_target_depth(RID p_render_target) {
  245. // TODO implement this, the problem is that our depth texture isn't part of our render target as it is used for 3D rendering only
  246. // but we don't have access to our render buffers from here....
  247. RendererSceneRenderRD * rd_scene = ?????;
  248. ERR_FAIL_NULL_V_MSG(rd_scene, RID(), "Renderer scene render not setup");
  249. return rd_scene->render_buffers_get_depth_texture(????????????);
  250. }
  251. */