/**************************************************************************/1/* xr_interface.h */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#pragma once3132#include "core/math/projection.h"33#include "core/os/thread_safe.h"34#include "servers/xr_server.h"35#include "xr_vrs.h"3637// forward declaration38struct BlitToScreen;3940/**41The XR interface is a template class on top of which we build interface to different AR, VR and tracking SDKs.42The idea is that we subclass this class, implement the logic, and then instantiate a singleton of each interface43when Godot starts. These instances do not initialize themselves but register themselves with the AR/VR server.4445If the user wants to enable AR/VR, they can choose the interface they want to use and initialize it.4647Note that we may make this into a fully instantiable class for GDExtension support.48*/4950class XRInterface : public RefCounted {51GDCLASS(XRInterface, RefCounted);5253public:54enum Capabilities { /* purely metadata, provides some info about what this interface supports */55XR_NONE = 0, /* no capabilities */56XR_MONO = 1, /* can be used with mono output */57XR_STEREO = 2, /* can be used with stereo output */58XR_QUAD = 4, /* can be used with quad output (not currently supported) */59XR_VR = 8, /* offers VR support */60XR_AR = 16, /* offers AR support */61XR_EXTERNAL = 32 /* renders to external device */62};6364enum TrackingStatus { /* tracking status currently based on AR but we can start doing more with this for VR as well */65XR_NORMAL_TRACKING,66XR_EXCESSIVE_MOTION,67XR_INSUFFICIENT_FEATURES,68XR_UNKNOWN_TRACKING,69XR_NOT_TRACKING70};7172enum PlayAreaMode { /* defines the mode used by the XR interface for tracking */73XR_PLAY_AREA_UNKNOWN, /* Area mode not set or not available */74XR_PLAY_AREA_3DOF, /* Only support orientation tracking, no positional tracking, area will center around player */75XR_PLAY_AREA_SITTING, /* Player is in seated position, limited positional tracking, fixed guardian around player */76XR_PLAY_AREA_ROOMSCALE, /* Player is free to move around, full positional tracking */77XR_PLAY_AREA_STAGE, /* Same as roomscale but origin point is fixed to the center of the physical space */78XR_PLAY_AREA_CUSTOM = 0x7FFFFFFF, /* Used to denote that a custom, possibly non-standard, play area is being used */79};8081enum EnvironmentBlendMode {82XR_ENV_BLEND_MODE_OPAQUE, /* You cannot see the real world, VR like */83XR_ENV_BLEND_MODE_ADDITIVE, /* You can see the real world, AR like */84XR_ENV_BLEND_MODE_ALPHA_BLEND, /* Real world is passed through where alpha channel is 0.0 and gradually blends to opaque for value 1.0. */85};8687enum VRSTextureFormat {88XR_VRS_TEXTURE_FORMAT_UNIFIED,89XR_VRS_TEXTURE_FORMAT_FRAGMENT_SHADING_RATE,90XR_VRS_TEXTURE_FORMAT_FRAGMENT_DENSITY_MAP,91};9293protected:94_THREAD_SAFE_CLASS_9596static void _bind_methods();9798public:99/** general interface information **/100virtual StringName get_name() const = 0;101virtual uint32_t get_capabilities() const = 0;102103bool is_primary();104void set_primary(bool p_is_primary);105106virtual bool is_initialized() const = 0; /* returns true if we've initialized this interface */107virtual bool initialize() = 0; /* initialize this interface, if this has an HMD it becomes the primary interface */108virtual void uninitialize() = 0; /* deinitialize this interface */109virtual Dictionary get_system_info() = 0; /* return a dictionary with info about our system */110111/** input and output **/112113virtual PackedStringArray get_suggested_tracker_names() const; /* return a list of likely/suggested tracker names */114virtual PackedStringArray get_suggested_pose_names(const StringName &p_tracker_name) const; /* return a list of likely/suggested action names for this tracker */115virtual TrackingStatus get_tracking_status() const; /* get the status of our current tracking */116virtual void 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 = 0); /* trigger a haptic pulse */117118/** specific to VR **/119virtual bool supports_play_area_mode(XRInterface::PlayAreaMode p_mode); /* query if this interface supports this play area mode */120virtual XRInterface::PlayAreaMode get_play_area_mode() const; /* get the current play area mode */121virtual bool set_play_area_mode(XRInterface::PlayAreaMode p_mode); /* change the play area mode, note that this should return false if the mode is not available */122virtual PackedVector3Array get_play_area() const; /* if available, returns an array of vectors denoting the play area the player can move around in */123124/** specific to AR **/125virtual bool get_anchor_detection_is_enabled() const;126virtual void set_anchor_detection_is_enabled(bool p_enable);127virtual int get_camera_feed_id();128129/** rendering and internal **/130131// These methods are called from the main thread.132virtual Transform3D get_camera_transform() = 0; /* returns the position of our camera, only used for updating reference frame. For monoscopic this is equal to the views transform, for stereoscopic this should be an average */133virtual void process() = 0;134135// These methods can be called from both main and render thread.136virtual Size2 get_render_target_size() = 0; /* returns the recommended render target size per eye for this device */137virtual uint32_t get_view_count() = 0; /* returns the view count we need (1 is monoscopic, 2 is stereoscopic but can be more) */138139// These methods are called from the rendering thread.140virtual Transform3D get_transform_for_view(uint32_t p_view, const Transform3D &p_cam_transform) = 0; /* get each views transform */141virtual Projection get_projection_for_view(uint32_t p_view, double p_aspect, double p_z_near, double p_z_far) = 0; /* get each view projection matrix */142virtual RID get_color_texture(); /* obtain color output texture (if applicable) */143virtual RID get_depth_texture(); /* obtain depth output texture (if applicable, used for reprojection) */144virtual RID get_velocity_texture(); /* obtain velocity output texture (if applicable, used for spacewarp) */145virtual RID get_velocity_depth_texture();146virtual Size2i get_velocity_target_size();147virtual Rect2i get_render_region();148virtual void pre_render() {}149virtual bool pre_draw_viewport(RID p_render_target) { return true; } /* inform XR interface we are about to start our viewport draw process */150virtual Vector<BlitToScreen> post_draw_viewport(RID p_render_target, const Rect2 &p_screen_rect) = 0; /* inform XR interface we finished our viewport draw process */151virtual void end_frame() {}152153/** passthrough **/154155virtual bool is_passthrough_supported() { return false; }156virtual bool is_passthrough_enabled() { return false; }157virtual bool start_passthrough() { return false; }158virtual void stop_passthrough() {}159160/** environment blend mode **/161virtual Array get_supported_environment_blend_modes();162virtual XRInterface::EnvironmentBlendMode get_environment_blend_mode() const { return XR_ENV_BLEND_MODE_OPAQUE; }163virtual bool set_environment_blend_mode(EnvironmentBlendMode mode) { return false; }164165/** VRS **/166virtual RID get_vrs_texture(); /* obtain VRS texture */167virtual VRSTextureFormat get_vrs_texture_format() { return XR_VRS_TEXTURE_FORMAT_UNIFIED; }168169XRInterface();170~XRInterface();171};172173VARIANT_ENUM_CAST(XRInterface::Capabilities);174VARIANT_ENUM_CAST(XRInterface::TrackingStatus);175VARIANT_ENUM_CAST(XRInterface::PlayAreaMode);176VARIANT_ENUM_CAST(XRInterface::EnvironmentBlendMode);177VARIANT_ENUM_CAST(XRInterface::VRSTextureFormat);178179180