xr_interface_extension.cpp 11 KB

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