xr_interface_extension.cpp 12 KB

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