Path: blob/master/servers/xr/xr_interface_extension.cpp
10277 views
/**************************************************************************/1/* xr_interface_extension.cpp */2/**************************************************************************/3/* This file is part of: */4/* GODOT ENGINE */5/* https://godotengine.org */6/**************************************************************************/7/* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */8/* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */9/* */10/* Permission is hereby granted, free of charge, to any person obtaining */11/* a copy of this software and associated documentation files (the */12/* "Software"), to deal in the Software without restriction, including */13/* without limitation the rights to use, copy, modify, merge, publish, */14/* distribute, sublicense, and/or sell copies of the Software, and to */15/* permit persons to whom the Software is furnished to do so, subject to */16/* the following conditions: */17/* */18/* The above copyright notice and this permission notice shall be */19/* included in all copies or substantial portions of the Software. */20/* */21/* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */22/* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */23/* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. */24/* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */25/* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */26/* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */27/* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */28/**************************************************************************/2930#include "xr_interface_extension.h"31#include "servers/rendering/renderer_rd/storage_rd/texture_storage.h"3233void XRInterfaceExtension::_bind_methods() {34GDVIRTUAL_BIND(_get_name);35GDVIRTUAL_BIND(_get_capabilities);3637GDVIRTUAL_BIND(_is_initialized);38GDVIRTUAL_BIND(_initialize);39GDVIRTUAL_BIND(_uninitialize);40GDVIRTUAL_BIND(_get_system_info);4142GDVIRTUAL_BIND(_supports_play_area_mode, "mode");43GDVIRTUAL_BIND(_get_play_area_mode);44GDVIRTUAL_BIND(_set_play_area_mode, "mode");45GDVIRTUAL_BIND(_get_play_area);4647GDVIRTUAL_BIND(_get_render_target_size);48GDVIRTUAL_BIND(_get_view_count);49GDVIRTUAL_BIND(_get_camera_transform);50GDVIRTUAL_BIND(_get_transform_for_view, "view", "cam_transform");51GDVIRTUAL_BIND(_get_projection_for_view, "view", "aspect", "z_near", "z_far");52GDVIRTUAL_BIND(_get_vrs_texture);53GDVIRTUAL_BIND(_get_vrs_texture_format);5455GDVIRTUAL_BIND(_process);56GDVIRTUAL_BIND(_pre_render);57GDVIRTUAL_BIND(_pre_draw_viewport, "render_target");58GDVIRTUAL_BIND(_post_draw_viewport, "render_target", "screen_rect");59GDVIRTUAL_BIND(_end_frame);6061/** input and output **/6263GDVIRTUAL_BIND(_get_suggested_tracker_names);64GDVIRTUAL_BIND(_get_suggested_pose_names, "tracker_name");65GDVIRTUAL_BIND(_get_tracking_status);66GDVIRTUAL_BIND(_trigger_haptic_pulse, "action_name", "tracker_name", "frequency", "amplitude", "duration_sec", "delay_sec");6768// we don't have any properties specific to VR yet....6970// but we do have properties specific to AR....71GDVIRTUAL_BIND(_get_anchor_detection_is_enabled);72GDVIRTUAL_BIND(_set_anchor_detection_is_enabled, "enabled");73GDVIRTUAL_BIND(_get_camera_feed_id);7475// override output methods76GDVIRTUAL_BIND(_get_color_texture);77GDVIRTUAL_BIND(_get_depth_texture);78GDVIRTUAL_BIND(_get_velocity_texture);7980ClassDB::bind_method(D_METHOD("get_color_texture"), &XRInterfaceExtension::get_color_texture);81ClassDB::bind_method(D_METHOD("get_depth_texture"), &XRInterfaceExtension::get_depth_texture);82ClassDB::bind_method(D_METHOD("get_velocity_texture"), &XRInterfaceExtension::get_velocity_texture);8384// helper methods85ClassDB::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);86ClassDB::bind_method(D_METHOD("get_render_target_texture", "render_target"), &XRInterfaceExtension::get_render_target_texture);87// ClassDB::bind_method(D_METHOD("get_render_target_depth", "render_target"), &XRInterfaceExtension::get_render_target_depth);88}8990StringName XRInterfaceExtension::get_name() const {91StringName name;9293if (GDVIRTUAL_CALL(_get_name, name)) {94return name;95}9697return "Unknown";98}99100uint32_t XRInterfaceExtension::get_capabilities() const {101uint32_t capabilities = 0;102GDVIRTUAL_CALL(_get_capabilities, capabilities);103return capabilities;104}105106bool XRInterfaceExtension::is_initialized() const {107bool initialized = false;108GDVIRTUAL_CALL(_is_initialized, initialized);109return initialized;110}111112bool XRInterfaceExtension::initialize() {113bool initialized = false;114GDVIRTUAL_CALL(_initialize, initialized);115return initialized;116}117118void XRInterfaceExtension::uninitialize() {119GDVIRTUAL_CALL(_uninitialize);120}121122Dictionary XRInterfaceExtension::get_system_info() {123Dictionary dict;124GDVIRTUAL_CALL(_get_system_info, dict);125return dict;126}127128PackedStringArray XRInterfaceExtension::get_suggested_tracker_names() const {129PackedStringArray arr;130131GDVIRTUAL_CALL(_get_suggested_tracker_names, arr);132133return arr;134}135136PackedStringArray XRInterfaceExtension::get_suggested_pose_names(const StringName &p_tracker_name) const {137PackedStringArray arr;138139GDVIRTUAL_CALL(_get_suggested_pose_names, p_tracker_name, arr);140141return arr;142}143144XRInterface::TrackingStatus XRInterfaceExtension::get_tracking_status() const {145XRInterface::TrackingStatus status = XR_UNKNOWN_TRACKING;146GDVIRTUAL_CALL(_get_tracking_status, status);147return status;148}149150void 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) {151GDVIRTUAL_CALL(_trigger_haptic_pulse, p_action_name, p_tracker_name, p_frequency, p_amplitude, p_duration_sec, p_delay_sec);152}153154bool XRInterfaceExtension::supports_play_area_mode(XRInterface::PlayAreaMode p_mode) {155bool is_supported = false;156GDVIRTUAL_CALL(_supports_play_area_mode, p_mode, is_supported);157return is_supported;158}159160XRInterface::PlayAreaMode XRInterfaceExtension::get_play_area_mode() const {161XRInterface::PlayAreaMode mode = XR_PLAY_AREA_UNKNOWN;162GDVIRTUAL_CALL(_get_play_area_mode, mode);163return mode;164}165166bool XRInterfaceExtension::set_play_area_mode(XRInterface::PlayAreaMode p_mode) {167bool success = false;168GDVIRTUAL_CALL(_set_play_area_mode, p_mode, success);169return success;170}171172PackedVector3Array XRInterfaceExtension::get_play_area() const {173PackedVector3Array arr;174GDVIRTUAL_CALL(_get_play_area, arr);175return arr;176}177178/** these will only be implemented on AR interfaces, so we want dummies for VR **/179bool XRInterfaceExtension::get_anchor_detection_is_enabled() const {180bool enabled = false;181GDVIRTUAL_CALL(_get_anchor_detection_is_enabled, enabled);182return enabled;183}184185void XRInterfaceExtension::set_anchor_detection_is_enabled(bool p_enable) {186// don't do anything here, this needs to be implemented on AR interface to enable/disable things like plane detection etc.187GDVIRTUAL_CALL(_set_anchor_detection_is_enabled, p_enable);188}189190int XRInterfaceExtension::get_camera_feed_id() {191int feed_id = 0;192GDVIRTUAL_CALL(_get_camera_feed_id, feed_id);193return feed_id;194}195196Size2 XRInterfaceExtension::get_render_target_size() {197Size2 size;198GDVIRTUAL_CALL(_get_render_target_size, size);199return size;200}201202uint32_t XRInterfaceExtension::get_view_count() {203uint32_t view_count = 1;204GDVIRTUAL_CALL(_get_view_count, view_count);205return view_count;206}207208Transform3D XRInterfaceExtension::get_camera_transform() {209Transform3D transform;210GDVIRTUAL_CALL(_get_camera_transform, transform);211return transform;212}213214Transform3D XRInterfaceExtension::get_transform_for_view(uint32_t p_view, const Transform3D &p_cam_transform) {215Transform3D transform;216GDVIRTUAL_CALL(_get_transform_for_view, p_view, p_cam_transform, transform);217return transform;218}219220Projection XRInterfaceExtension::get_projection_for_view(uint32_t p_view, double p_aspect, double p_z_near, double p_z_far) {221Projection cm;222PackedFloat64Array arr;223224if (GDVIRTUAL_CALL(_get_projection_for_view, p_view, p_aspect, p_z_near, p_z_far, arr)) {225ERR_FAIL_COND_V_MSG(arr.size() != 16, Projection(), "Projection matrix must contain 16 floats");226real_t *m = (real_t *)cm.columns;227for (int i = 0; i < 16; i++) {228m[i] = arr[i];229}230return cm;231}232233return Projection();234}235236RID XRInterfaceExtension::get_vrs_texture() {237RID vrs_texture;238if (GDVIRTUAL_CALL(_get_vrs_texture, vrs_texture)) {239return vrs_texture;240} else {241return XRInterface::get_vrs_texture();242}243}244245XRInterface::VRSTextureFormat XRInterfaceExtension::get_vrs_texture_format() {246VRSTextureFormat vrs_texture_format = XR_VRS_TEXTURE_FORMAT_UNIFIED;247if (GDVIRTUAL_CALL(_get_vrs_texture_format, vrs_texture_format)) {248return vrs_texture_format;249}250return vrs_texture_format;251}252253RID XRInterfaceExtension::get_color_texture() {254RID texture;255GDVIRTUAL_CALL(_get_color_texture, texture);256return texture;257}258259RID XRInterfaceExtension::get_depth_texture() {260RID texture;261GDVIRTUAL_CALL(_get_depth_texture, texture);262return texture;263}264265RID XRInterfaceExtension::get_velocity_texture() {266RID texture;267GDVIRTUAL_CALL(_get_velocity_texture, texture);268return texture;269}270271void 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) {272BlitToScreen blit;273274ERR_FAIL_COND_MSG(!can_add_blits, "add_blit can only be called from an XR plugin from within _post_draw_viewport!");275276blit.render_target = p_render_target;277blit.src_rect = p_src_rect;278blit.dst_rect = p_dst_rect;279280blit.multi_view.use_layer = p_use_layer;281blit.multi_view.layer = p_layer;282283blit.lens_distortion.apply = p_apply_lens_distortion;284blit.lens_distortion.eye_center = p_eye_center;285blit.lens_distortion.k1 = p_k1;286blit.lens_distortion.k2 = p_k2;287blit.lens_distortion.upscale = p_upscale;288blit.lens_distortion.aspect_ratio = p_aspect_ratio;289290blits.push_back(blit);291}292293void XRInterfaceExtension::process() {294GDVIRTUAL_CALL(_process);295}296297void XRInterfaceExtension::pre_render() {298GDVIRTUAL_CALL(_pre_render);299}300301bool XRInterfaceExtension::pre_draw_viewport(RID p_render_target) {302bool do_render = true;303GDVIRTUAL_CALL(_pre_draw_viewport, p_render_target, do_render);304return do_render; // If not implemented we're returning true.305}306307Vector<BlitToScreen> XRInterfaceExtension::post_draw_viewport(RID p_render_target, const Rect2 &p_screen_rect) {308// This is just so our XR plugin can add blits...309blits.clear();310can_add_blits = true;311312if (GDVIRTUAL_CALL(_post_draw_viewport, p_render_target, p_screen_rect)) {313return blits;314}315316can_add_blits = false;317return blits;318}319320void XRInterfaceExtension::end_frame() {321GDVIRTUAL_CALL(_end_frame);322}323324RID XRInterfaceExtension::get_render_target_texture(RID p_render_target) {325// In due time this will need to be enhance to return the correct INTERNAL RID for the chosen rendering engine.326// So once a GLES driver is implemented we'll return that and the implemented plugin needs to handle this correctly too.327RendererRD::TextureStorage *texture_storage = RendererRD::TextureStorage::get_singleton();328ERR_FAIL_NULL_V_MSG(texture_storage, RID(), "Texture storage not setup");329330return texture_storage->render_target_get_rd_texture(p_render_target);331}332333/*334RID XRInterfaceExtension::get_render_target_depth(RID p_render_target) {335// TODO implement this, the problem is that our depth texture isn't part of our render target as it is used for 3D rendering only336// but we don't have access to our render buffers from here....337}338*/339340341