xr_interface_extension.cpp 11 KB

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