Godot Engine – Multi-platform 2D and 3D game engine
OpenXR: Add support for Foveated Inset
Bastiaan Olij31314ccb9482
diff --git a/doc/classes/ProjectSettings.xml b/doc/classes/ProjectSettings.xmlindex b921a14be39..7f9c942c783 100644--- a/doc/classes/ProjectSettings.xml+++ b/doc/classes/ProjectSettings.xml@@ -3620,6 +3620,9 @@<member name="xr/openxr/binding_modifiers/dpad_binding" type="bool" setter="" getter="" default="false">If [code]true[/code], enables the D-pad binding modifier if supported by the XR runtime.</member>+ <member name="xr/openxr/create_default_foveated_inset_viewport" type="bool" setter="" getter="" default="true">+ If [code]true[/code], and [member xr/openxr/view_configuration] is set to [code]Stereo with Foveated Inset[/code], and foveated inset is supported, Godot will add and configure an [OpenXRFoveatedInsetViewport] node for rendering the foveated inset.+ </member><member name="xr/openxr/default_action_map" type="String" setter="" getter="" default=""res://openxr_action_map.tres"">Action map configuration to load by default.</member>diff --git a/drivers/gles3/rasterizer_scene_gles3.h b/drivers/gles3/rasterizer_scene_gles3.hindex 127f6776818..451ea562169 100644--- a/drivers/gles3/rasterizer_scene_gles3.h+++ b/drivers/gles3/rasterizer_scene_gles3.h@@ -108,7 +108,6 @@ struct RenderDataGLES3 {Transform3D inv_cam_transform;Projection cam_projection;bool cam_orthogonal = false;- bool cam_asymmetrical = false;uint32_t camera_visible_layers = 0xFFFFFFFF;// For billboards to cast correct shadows.diff --git a/main/main.cpp b/main/main.cppindex 7745eea0fb1..248e288423e 100644--- a/main/main.cpp+++ b/main/main.cpp@@ -2861,13 +2861,14 @@ Error Main::setup(const char *execpath, int argc, char *argv[], bool p_second_phGLOBAL_DEF(PropertyInfo(Variant::STRING, "xr/openxr/target_api_version"), "");GLOBAL_DEF_BASIC(PropertyInfo(Variant::STRING, "xr/openxr/default_action_map", PROPERTY_HINT_FILE, "*.tres"), "res://openxr_action_map.tres");GLOBAL_DEF_BASIC(PropertyInfo(Variant::INT, "xr/openxr/form_factor", PROPERTY_HINT_ENUM, "Head Mounted,Handheld"), "0");- GLOBAL_DEF_BASIC(PropertyInfo(Variant::INT, "xr/openxr/view_configuration", PROPERTY_HINT_ENUM, "Mono,Stereo"), "1"); // "Mono,Stereo,Quad,Observer"+ GLOBAL_DEF_BASIC(PropertyInfo(Variant::INT, "xr/openxr/view_configuration", PROPERTY_HINT_ENUM, "Mono,Stereo,Stereo with Foveated Inset"), "1");GLOBAL_DEF_BASIC(PropertyInfo(Variant::INT, "xr/openxr/reference_space", PROPERTY_HINT_ENUM, "Local,Stage,Local Floor"), "1");GLOBAL_DEF_BASIC(PropertyInfo(Variant::INT, "xr/openxr/environment_blend_mode", PROPERTY_HINT_ENUM, "Opaque,Additive,Alpha"), "0");GLOBAL_DEF_BASIC(PropertyInfo(Variant::INT, "xr/openxr/foveation_level", PROPERTY_HINT_ENUM, "Off,Low,Medium,High"), "0");GLOBAL_DEF_BASIC("xr/openxr/foveation_dynamic", false);GLOBAL_DEF_BASIC("xr/openxr/foveation_eye_tracked", true);GLOBAL_DEF_BASIC("xr/openxr/foveation_with_subsampled_images", true);+ GLOBAL_DEF_BASIC("xr/openxr/create_default_foveated_inset_viewport", true);GLOBAL_DEF_BASIC("xr/openxr/submit_depth_buffer", false);GLOBAL_DEF_BASIC("xr/openxr/startup_alert", true);diff --git a/modules/openxr/config.py b/modules/openxr/config.pyindex d91d239c822..7ac731cb2e8 100644--- a/modules/openxr/config.py+++ b/modules/openxr/config.py@@ -27,6 +27,7 @@ def get_doc_classes():"OpenXRIPBinding","OpenXRHand","OpenXRVisibilityMask",+ "OpenXRFoveatedInsetViewport","OpenXRCompositionLayer","OpenXRCompositionLayerQuad","OpenXRCompositionLayerCylinder",diff --git a/modules/openxr/doc_classes/OpenXRAPIExtension.xml b/modules/openxr/doc_classes/OpenXRAPIExtension.xmlindex 00121d4ecec..8d495d19a9e 100644--- a/modules/openxr/doc_classes/OpenXRAPIExtension.xml+++ b/modules/openxr/doc_classes/OpenXRAPIExtension.xml@@ -103,6 +103,12 @@Returns the predicted display timing for the current frame.</description></method>+ <method name="get_primary_view_count" qualifiers="const">+ <return type="int" />+ <description>+ Returns the view count for the primary viewport.+ </description>+ </method><method name="get_projection_layer"><return type="int" /><description>diff --git a/modules/openxr/doc_classes/OpenXRFoveatedInsetViewport.xml b/modules/openxr/doc_classes/OpenXRFoveatedInsetViewport.xmlnew file mode 100644index 00000000000..9b86924bf0f--- /dev/null+++ b/modules/openxr/doc_classes/OpenXRFoveatedInsetViewport.xml@@ -0,0 +1,29 @@+<?xml version="1.0" encoding="UTF-8" ?>+<class name="OpenXRFoveatedInsetViewport" inherits="SubViewport" api_type="core" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="../../../doc/class.xsd">+ <brief_description>+ This viewport is used for rendering our foveated inset.+ </brief_description>+ <description>+ This viewport is used for rendering our foveated inset. When foveated inset rendering is supported OpenXR will create and configure this viewport correctly and use its output. The viewport will be available after OpenXR reaches the "begun" state. You can access it by calling [code]get_tree().get_root().get_node("OpenXRFoveatedInsetViewport")[/code] at this time.+ </description>+ <tutorials>+ </tutorials>+ <methods>+ <method name="get_xr_camera3d" qualifiers="const">+ <return type="XRCamera3D" />+ <description>+ Returns the [XRCamera3D] node used for rendering the foveated inset.+ </description>+ </method>+ <method name="get_xr_origin3d" qualifiers="const">+ <return type="XROrigin3D" />+ <description>+ Returns the [XROrigin3D] node used for tracking the foveated inset. Our built-in logic handles positioning this node. It will copy the position and orientation of the [XROrigin3D] node marked as current.+ </description>+ </method>+ </methods>+ <members>+ <member name="process_priority" type="int" setter="set_process_priority" getter="get_process_priority" overrides="Node" default="99999" />+ <member name="render_target_update_mode" type="int" setter="set_update_mode" getter="get_update_mode" overrides="SubViewport" enum="SubViewport.UpdateMode" default="0" />+ </members>+</class>diff --git a/modules/openxr/doc_classes/OpenXRInterface.xml b/modules/openxr/doc_classes/OpenXRInterface.xmlindex e9b1938de11..ee7c1194d02 100644--- a/modules/openxr/doc_classes/OpenXRInterface.xml+++ b/modules/openxr/doc_classes/OpenXRInterface.xml@@ -17,6 +17,13 @@Returns a list of action sets registered with Godot (loaded from the action map at runtime).</description></method>+ <method name="get_active_view_configuration" qualifiers="const">+ <return type="int" enum="OpenXRInterface.ViewConfiguration" />+ <description>+ Gets the active view configuration.+ [b]Note:[/b] This will only return a valid value after OpenXR is initialized.+ </description>+ </method><method name="get_available_display_refresh_rates" qualifiers="const"><return type="Array" /><description>@@ -297,6 +304,21 @@<constant name="SESSION_STATE_EXITING" value="8" enum="SessionState">The OpenXR instance is about to be destroyed and we're exiting. [signal instance_exiting] is emitted when we change to this state.</constant>+ <constant name="VIEW_CONFIGURATION_MONO" value="0" enum="ViewConfiguration">+ Our XR output configuration is monoscopic.+ </constant>+ <constant name="VIEW_CONFIGURATION_STEREO" value="1" enum="ViewConfiguration">+ Our XR output configuration is stereoscopic.+ </constant>+ <constant name="VIEW_CONFIGURATION_STEREO_WITH_INSET" value="2" enum="ViewConfiguration">+ Our XR output configuration is stereoscopic with an additional foveated inset render.+ </constant>+ <constant name="VIEW_CONFIGURATION_UNSET" value="254" enum="ViewConfiguration">+ Our XR output configuration has not yet been determined.+ </constant>+ <constant name="VIEW_CONFIGURATION_UNKNOWN" value="255" enum="ViewConfiguration">+ Our XR output configuration is unknown.+ </constant><constant name="HAND_LEFT" value="0" enum="Hand">Left hand.</constant>diff --git a/modules/openxr/extensions/openxr_extension_wrapper.h b/modules/openxr/extensions/openxr_extension_wrapper.hindex a2d7dbf59aa..36fb7c11583 100644--- a/modules/openxr/extensions/openxr_extension_wrapper.h+++ b/modules/openxr/extensions/openxr_extension_wrapper.h@@ -158,6 +158,7 @@ public:virtual void on_main_swapchains_created(); // `on_main_swapchains_created` is called right after our main swapchains are (re)created.virtual void on_pre_draw_viewport(RID p_render_target); // `on_pre_draw_viewport` is called right before we start rendering this viewportvirtual void on_post_draw_viewport(RID p_render_target); // `on_port_draw_viewport` is called right after we start rendering this viewport (note that on Vulkan draw commands may only be queued)+ virtual void on_post_render() {} // `on_post_render` is called right before we submit our XR viewports to the XR compositor.GDVIRTUAL1(_on_register_metadata, OpenXRInteractionProfileMetadata *);GDVIRTUAL0(_on_before_instance_created);diff --git a/modules/openxr/extensions/openxr_foveated_inset_extension.cpp b/modules/openxr/extensions/openxr_foveated_inset_extension.cppnew file mode 100644index 00000000000..bb3b489a4d0--- /dev/null+++ b/modules/openxr/extensions/openxr_foveated_inset_extension.cpp@@ -0,0 +1,344 @@+/**************************************************************************/+/* openxr_foveated_inset_extension.cpp */+/**************************************************************************/+/* This file is part of: */+/* GODOT ENGINE */+/* https://godotengine.org */+/**************************************************************************/+/* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */+/* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */+/* */+/* Permission is hereby granted, free of charge, to any person obtaining */+/* a copy of this software and associated documentation files (the */+/* "Software"), to deal in the Software without restriction, including */+/* without limitation the rights to use, copy, modify, merge, publish, */+/* distribute, sublicense, and/or sell copies of the Software, and to */+/* permit persons to whom the Software is furnished to do so, subject to */+/* the following conditions: */+/* */+/* The above copyright notice and this permission notice shall be */+/* included in all copies or substantial portions of the Software. */+/* */+/* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */+/* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */+/* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. */+/* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */+/* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */+/* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */+/* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */+/**************************************************************************/++#include "openxr_foveated_inset_extension.h"++#include "../scene/openxr_foveated_inset_viewport.h"++#include "core/config/project_settings.h"+#include "core/object/callable_mp.h"+#include "core/os/os.h"+#include "scene/main/scene_tree.h"+#include "scene/main/window.h" // IWYU pragma: keep. Used via `Node::get_window()`.+#include "servers/rendering/rendering_server.h"+#include "servers/rendering/rendering_server_globals.h"+#include "servers/xr/xr_server.h"++OpenXRFoveatedInsetExtension *OpenXRFoveatedInsetExtension::singleton = nullptr;++OpenXRFoveatedInsetExtension *OpenXRFoveatedInsetExtension::get_singleton() {+ return singleton;+}++OpenXRFoveatedInsetExtension::OpenXRFoveatedInsetExtension() {+ singleton = this;+}++OpenXRFoveatedInsetExtension::~OpenXRFoveatedInsetExtension() {+ singleton = nullptr;+}++HashMap<String, bool *> OpenXRFoveatedInsetExtension::get_requested_extensions(XrVersion p_version) {+ HashMap<String, bool *> request_extensions;++ // Only request our extensions if we're attempting to use foveated inset.+ int view_configuration_setting = GLOBAL_GET("xr/openxr/view_configuration");+ if (p_version < XR_API_VERSION_1_1_0 && view_configuration_setting == 2) {+ // Extension was replaced in OpenXR 1.1, use `XR_VARJO_quad_views` in OpenXR 1.0.+ // Note: we currently always include this as there is a dependency with `XR_VARJO_foveated_rendering`.+ request_extensions[XR_VARJO_QUAD_VIEWS_EXTENSION_NAME] = &varjo_ext_available;++ // This enables eye tracked foveated inset support.+ request_extensions[XR_VARJO_FOVEATED_RENDERING_EXTENSION_NAME] = &varjo_foveated_rendering_ext_available;+ }++ return request_extensions;+}++void OpenXRFoveatedInsetExtension::on_session_created(const XrSession p_session) {+ OpenXRAPI *openxr_api = OpenXRAPI::get_singleton();+ ERR_FAIL_NULL(openxr_api);++ // We must create a tracker for our foveated inset if we use the foveated inset rendering.+ if (openxr_api->get_view_configuration() == XR_VIEW_CONFIGURATION_TYPE_PRIMARY_STEREO_WITH_FOVEATED_INSET) {+ XRServer *xr_server = XRServer::get_singleton();+ ERR_FAIL_NULL(xr_server);++ foveated_inset.instantiate();+ foveated_inset->set_tracker_type(XRServer::TRACKER_CAMERA);+ foveated_inset->set_tracker_name(XR_TRACKER_INSET);+ foveated_inset->set_tracker_desc("Foveated Inset View");+ xr_server->add_tracker(foveated_inset);+ }+}++void OpenXRFoveatedInsetExtension::on_state_ready() {+ OpenXRAPI *openxr_api = OpenXRAPI::get_singleton();+ ERR_FAIL_NULL(openxr_api);++ // It's too early in `on_session_created` to create our viewport.+ // It would be nicer if we could do this earlier...+ bool create_inset_viewport = GLOBAL_GET("xr/openxr/create_default_foveated_inset_viewport");+ if (openxr_api->get_view_configuration() == XR_VIEW_CONFIGURATION_TYPE_PRIMARY_STEREO_WITH_FOVEATED_INSET && create_inset_viewport) {+ OS *os = OS::get_singleton();+ ERR_FAIL_NULL(os);++ MainLoop *main_loop = os->get_main_loop();+ ERR_FAIL_NULL(main_loop);++ SceneTree *scene_tree = Object::cast_to<SceneTree>(main_loop);+ ERR_FAIL_NULL(scene_tree);++ Node *root = scene_tree->get_root();+ ERR_FAIL_NULL(root);++ inset_viewport = memnew(OpenXRFoveatedInsetViewport);+ inset_viewport->set_name("OpenXRFoveatedInsetViewport");+ root->add_child(inset_viewport);+ }+}++void OpenXRFoveatedInsetExtension::on_session_destroyed() {+ XRServer *xr_server = XRServer::get_singleton();+ ERR_FAIL_NULL(xr_server);++ if (inset_viewport) {+ Node *parent = inset_viewport->get_parent();+ if (parent) {+ parent->remove_child(inset_viewport);+ }+ inset_viewport->queue_free();+ inset_viewport = nullptr;+ }++ _free_swapchains();++ if (foveated_inset.is_valid()) {+ xr_server->remove_tracker(foveated_inset);+ foveated_inset.unref();+ }+}++void OpenXRFoveatedInsetExtension::on_process() {+ OpenXRAPI *openxr_api = OpenXRAPI::get_singleton();+ ERR_FAIL_NULL(openxr_api);++ if (foveated_inset.is_valid()) {+ // We use the same center transform here as for our head tracker.+ Transform3D t;+ Vector3 linear_velocity;+ Vector3 angular_velocity;++ head_confidence = openxr_api->get_head_center(t, linear_velocity, angular_velocity);+ if (head_confidence != XRPose::XR_TRACKING_CONFIDENCE_NONE) {+ // Only update our transform if we have one to update it with+ // note that poses are stored without world scale and reference frame applied!+ head_transform = t;+ head_linear_velocity = linear_velocity;+ head_angular_velocity = angular_velocity;+ }++ foveated_inset->set_pose("default", head_transform, head_linear_velocity, head_angular_velocity, head_confidence);+ }+}++TypedArray<Projection> OpenXRFoveatedInsetExtension::get_camera_projections(const StringName &p_tracker_name, double p_aspect, double p_z_near, double p_z_far) {+ TypedArray<Projection> camera_projections;++ OpenXRAPI *openxr_api = OpenXRAPI::get_singleton();+ ERR_FAIL_NULL_V(openxr_api, camera_projections);++ if (p_tracker_name == XR_TRACKER_INSET) {+ if (openxr_api->get_view_configuration() != XR_VIEW_CONFIGURATION_TYPE_PRIMARY_STEREO_WITH_FOVEATED_INSET) {+ // It's possible our application was configured to support foveated inset,+ // but the current hardware used does not support it.+ return camera_projections;+ }++ ERR_FAIL_COND_V(openxr_api->get_view_count() != 4, camera_projections);++ for (uint32_t v = 2; v < 4; v++) {+ Projection cm;+ openxr_api->get_view_projection(v, p_z_near, p_z_far, cm);+ camera_projections.push_back(cm);+ }+ }++ return camera_projections;+}++TypedArray<Transform3D> OpenXRFoveatedInsetExtension::get_camera_offsets(const StringName &p_tracker_name) {+ TypedArray<Transform3D> camera_offsets;++ OpenXRAPI *openxr_api = OpenXRAPI::get_singleton();+ ERR_FAIL_NULL_V(openxr_api, camera_offsets);++ if (p_tracker_name == XR_TRACKER_INSET) {+ if (openxr_api->get_view_configuration() != XR_VIEW_CONFIGURATION_TYPE_PRIMARY_STEREO_WITH_FOVEATED_INSET) {+ // It's possible our application was configured to support foveated inset,+ // but the current hardware used does not support it.+ return camera_offsets;+ }++ ERR_FAIL_COND_V(openxr_api->get_view_count() != 4, camera_offsets);++ XRServer *xr_server = XRServer::get_singleton();+ ERR_FAIL_NULL_V(xr_server, camera_offsets);++ double world_scale = xr_server->get_world_scale();++ for (uint32_t v = 2; v < 4; v++) {+ Transform3D t;+ openxr_api->get_view_offset(v, t);++ // Apply our world scale+ t.origin *= world_scale;++ camera_offsets.push_back(t);+ }+ }++ return camera_offsets;+}++Size2i OpenXRFoveatedInsetExtension::get_render_size() {+ OpenXRAPI *openxr_api = OpenXRAPI::get_singleton();+ ERR_FAIL_NULL_V(openxr_api, Size2i());++#ifdef DEBUG_ENABLED+ // Views 2 and 3 should have the same size!+ ERR_FAIL_COND_V(openxr_api->get_recommended_target_size(2) != openxr_api->get_recommended_target_size(3), Size2i());+#endif++ return openxr_api->get_recommended_target_size(2);+}++void OpenXRFoveatedInsetExtension::register_viewport(RID p_viewport) {+ RenderingServer *rendering_server = RenderingServer::get_singleton();+ ERR_FAIL_NULL(rendering_server);++ rendering_server->call_on_render_thread(callable_mp(this, &OpenXRFoveatedInsetExtension::_register_viewport_rt).bind(p_viewport));+}++void OpenXRFoveatedInsetExtension::unregister_viewport(RID p_viewport) {+ RenderingServer *rendering_server = RenderingServer::get_singleton();+ ERR_FAIL_NULL(rendering_server);++ rendering_server->call_on_render_thread(callable_mp(this, &OpenXRFoveatedInsetExtension::_unregister_viewport_rt).bind(p_viewport));+}++void OpenXRFoveatedInsetExtension::_register_viewport_rt(RID p_viewport) {+ render_state.viewports.push_back(p_viewport);+}++void OpenXRFoveatedInsetExtension::_unregister_viewport_rt(RID p_viewport) {+ render_state.viewports.erase(p_viewport);+}++void OpenXRFoveatedInsetExtension::on_pre_render() {+ if (render_state.viewports.size() == 0) {+ return;+ } else if (render_state.viewports.size() > 1) {+ WARN_PRINT("Multiple OpenXRFoveatedInsetViewport nodes are active, using the first.");+ }++ OpenXRAPI *openxr_api = OpenXRAPI::get_singleton();+ ERR_FAIL_NULL(openxr_api);++ if (openxr_api->get_view_configuration() != XR_VIEW_CONFIGURATION_TYPE_PRIMARY_STEREO_WITH_FOVEATED_INSET) {+ return;+ }++ RenderingServer *rendering_server = RenderingServer::get_singleton();+ ERR_FAIL_NULL(rendering_server);++ RID render_target = rendering_server->viewport_get_render_target(render_state.viewports[0]);+ if (!render_target.is_valid()) {+ return;+ }++ // Check if we need to update our swap chains.+ Size2i new_size = openxr_api->get_recommended_target_size(2);+ if (render_state.size != new_size) {+ _free_swapchains();++ int64_t color_swapchain_format = openxr_api->get_color_swapchain_format();+ if (color_swapchain_format != 0) {+ if (!render_state.swapchains[SWAPCHAIN_COLOR].create(0, XR_SWAPCHAIN_USAGE_SAMPLED_BIT | XR_SWAPCHAIN_USAGE_COLOR_ATTACHMENT_BIT | XR_SWAPCHAIN_USAGE_MUTABLE_FORMAT_BIT, color_swapchain_format, new_size.width, new_size.height, 1, 2, true)) {+ return;+ }++ openxr_api->set_object_name(XR_OBJECT_TYPE_SWAPCHAIN, uint64_t(render_state.swapchains[SWAPCHAIN_COLOR].get_swapchain()), "Inset color swapchain");+ }++ // TODO check depth swapchain.++ // Our new size is now applicable.+ render_state.size = new_size;++ // Set projections for views 2 and 3.+ for (uint32_t i = 2; i < 4; i++) {+ openxr_api->set_projection_view_swapchain_rt(i, render_state.swapchains[SWAPCHAIN_COLOR].get_swapchain(), i - 2, new_size);+ }++ // TODO Set depth for views 2 and 3.+ }++ // Check if we need to acquire our swap chains.+ for (int i = 0; i < SWAPCHAIN_MAX; i++) {+ if (!render_state.swapchains[i].is_image_acquired() && render_state.swapchains[i].get_swapchain() != XR_NULL_HANDLE) {+ bool should_render = true;+ if (!render_state.swapchains[i].acquire(should_render)) {+ return;+ }+ }+ }++ // Set our swapchains on our render target.+ RID color_texture = render_state.swapchains[SWAPCHAIN_COLOR].get_image();+ RID depth_texture = render_state.swapchains[SWAPCHAIN_DEPTH].get_image();+ RSG::texture_storage->render_target_set_override(render_target, color_texture, depth_texture, RID(), RID());+}++void OpenXRFoveatedInsetExtension::on_post_render() {+ if (render_state.viewports.size() == 0) {+ return;+ }++ OpenXRAPI *openxr_api = OpenXRAPI::get_singleton();+ ERR_FAIL_NULL(openxr_api);++ if (openxr_api->get_view_configuration() != XR_VIEW_CONFIGURATION_TYPE_PRIMARY_STEREO_WITH_FOVEATED_INSET) {+ return;+ }++ // Release our swapchain image if we acquired it.+ for (int i = 0; i < SWAPCHAIN_MAX; i++) {+ if (render_state.swapchains[i].is_image_acquired()) {+ render_state.swapchains[i].release();+ }+ }+}++void OpenXRFoveatedInsetExtension::_free_swapchains() {+ for (int i = 0; i < SWAPCHAIN_MAX; i++) {+ render_state.swapchains[i].queue_free();+ }+}diff --git a/modules/openxr/extensions/openxr_foveated_inset_extension.h b/modules/openxr/extensions/openxr_foveated_inset_extension.hnew file mode 100644index 00000000000..9b0d2c3dc63--- /dev/null+++ b/modules/openxr/extensions/openxr_foveated_inset_extension.h@@ -0,0 +1,124 @@+/**************************************************************************/+/* openxr_foveated_inset_extension.h */+/**************************************************************************/+/* This file is part of: */+/* GODOT ENGINE */+/* https://godotengine.org */+/**************************************************************************/+/* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */+/* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */+/* */+/* Permission is hereby granted, free of charge, to any person obtaining */+/* a copy of this software and associated documentation files (the */+/* "Software"), to deal in the Software without restriction, including */+/* without limitation the rights to use, copy, modify, merge, publish, */+/* distribute, sublicense, and/or sell copies of the Software, and to */+/* permit persons to whom the Software is furnished to do so, subject to */+/* the following conditions: */+/* */+/* The above copyright notice and this permission notice shall be */+/* included in all copies or substantial portions of the Software. */+/* */+/* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */+/* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */+/* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. */+/* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */+/* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */+/* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */+/* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */+/**************************************************************************/++#pragma once++#include "../openxr_api.h"+#include "openxr_extension_wrapper.h"++#include "servers/xr/xr_positional_tracker.h"++// Foveated inset is a core feature in OpenXR 1.1 but we'll encapsulate+// the functionality in this "extension".+//+// When used we render 4 views instead of 2.+// The first two views are our context view and rendered by our default logic.+// The second two views are our focus view (inset) and handled by the+// implementation here.+// This order is guaranteed by the OpenXR specification.+//+// In OpenXR 1.0 XR_VIEW_CONFIGURATION_TYPE_PRIMARY_STEREO_WITH_FOVEATED_INSET+// was a Varjo only extension implementing this functionality equal to how+// the core support in OpenXR 1.1 defines this.+// We enable it as a fallback if supported.+//+// For more detail on how this works, Varjo has an excellent information page:+// https://varjo.com/blog/make-the-best-out-of-your-varjo-experience-update-on-varjo-quad-view-eye-tracked-foveation-and-varjo-base-settings++#define XR_TRACKER_INSET SNAME("foveated_inset")++class OpenXRFoveatedInsetViewport;++class OpenXRFoveatedInsetExtension : public OpenXRExtensionWrapper {+ GDCLASS(OpenXRFoveatedInsetExtension, OpenXRExtensionWrapper);++protected:+ static void _bind_methods() {}++public:+ static OpenXRFoveatedInsetExtension *get_singleton();++ OpenXRFoveatedInsetExtension();+ virtual ~OpenXRFoveatedInsetExtension() override;++ virtual HashMap<String, bool *> get_requested_extensions(XrVersion p_version) override;++ virtual void on_session_created(const XrSession p_session) override;+ virtual void on_session_destroyed() override;+ virtual void on_state_ready() override;+ virtual void on_process() override;++ // Note, call `OpenXR::is_view_configuration_supported(XR_VIEW_CONFIGURATION_TYPE_PRIMARY_STEREO_WITH_FOVEATED_INSET)`+ // to check if our foveated inset is supported.+ // Even if OpenXR 1.1 is initialized or the Varjo extension is available,+ // this does not guarantee the current hardware configuration actually makes foveated inset available.+ // Hence we do not expose an `is_available` function here.++ virtual TypedArray<Projection> get_camera_projections(const StringName &p_tracker_name, double p_aspect, double p_z_near, double p_z_far) override;+ virtual TypedArray<Transform3D> get_camera_offsets(const StringName &p_tracker_name) override;++ Size2i get_render_size();++ void register_viewport(RID p_viewport);+ void unregister_viewport(RID p_viewport);+ virtual void on_pre_render() override;+ virtual void on_post_render() override;++private:+ static OpenXRFoveatedInsetExtension *singleton;+ OpenXRFoveatedInsetViewport *inset_viewport = nullptr;++ bool varjo_ext_available = false;+ bool varjo_foveated_rendering_ext_available = false;++ Ref<XRPositionalTracker> foveated_inset;+ Transform3D head_transform;+ Vector3 head_linear_velocity;+ Vector3 head_angular_velocity;+ XRPose::TrackingConfidence head_confidence = XRPose::XR_TRACKING_CONFIDENCE_NONE;++ enum SwapChainTypes {+ SWAPCHAIN_COLOR,+ SWAPCHAIN_DEPTH,+ SWAPCHAIN_MAX+ };++ // Render state, Only accessible in rendering thread+ struct RenderState {+ LocalVector<RID> viewports;+ Size2i size;+ OpenXRAPI::OpenXRSwapChainInfo swapchains[SWAPCHAIN_MAX];+ } render_state;++ void _register_viewport_rt(RID p_viewport);+ void _unregister_viewport_rt(RID p_viewport);++ void _free_swapchains();+};diff --git a/modules/openxr/extensions/openxr_visibility_mask_extension.cpp b/modules/openxr/extensions/openxr_visibility_mask_extension.cppindex ac3e4673f4e..5f69424d5e9 100644--- a/modules/openxr/extensions/openxr_visibility_mask_extension.cpp+++ b/modules/openxr/extensions/openxr_visibility_mask_extension.cpp@@ -37,6 +37,12 @@#include "core/variant/variant.h"#include "servers/rendering/rendering_server.h"+// Note: if foveated inset rendering is used, our visibility mask should only+// be applied to our context view.+// This code is correct and will only supply data for the primary view.+// But user will need to setup visual layers accordingly.+// Need to document this!+static const char *VISIBILITY_MASK_SHADER_CODE ="shader_type spatial;\n""render_mode unshaded, shadows_disabled, cull_disabled;\n"@@ -102,7 +108,7 @@ void OpenXRVisibilityMaskExtension::on_session_created(const XrSession p_instancrendering_server->material_set_render_priority(material, 99);// Get our initial mesh data.- mesh_count = openxr_api->get_view_count(); // We need a mesh for each view.+ mesh_count = openxr_api->get_view_count(); // We need a mesh for each primary view.for (uint32_t i = 0; i < mesh_count; i++) {_update_mesh_data(i);}diff --git a/modules/openxr/openxr_api.cpp b/modules/openxr/openxr_api.cppindex a73daa43089..c568a56fd75 100644--- a/modules/openxr/openxr_api.cpp+++ b/modules/openxr/openxr_api.cpp@@ -808,9 +808,15 @@ bool OpenXRAPI::load_supported_view_configuration_types() {// Check value we loaded at startup...if (!is_view_configuration_supported(view_configuration)) {- print_verbose(vformat("OpenXR: %s isn't supported, defaulting to %s.", OpenXRUtil::get_view_configuration_name(view_configuration), OpenXRUtil::get_view_configuration_name(supported_view_configuration_types[0])));-- view_configuration = supported_view_configuration_types[0];+ if (view_configuration == XR_VIEW_CONFIGURATION_TYPE_PRIMARY_STEREO_WITH_FOVEATED_INSET && is_view_configuration_supported(XR_VIEW_CONFIGURATION_TYPE_PRIMARY_STEREO)) {+ // Fall back to stereo+ print_verbose("OpenXR: XR_VIEW_CONFIGURATION_TYPE_PRIMARY_STEREO_WITH_FOVEATED_INSET isn't supported, defaulting to XR_VIEW_CONFIGURATION_TYPE_PRIMARY_STEREO.");+ view_configuration = XR_VIEW_CONFIGURATION_TYPE_PRIMARY_STEREO;+ } else {+ // Fall back to the first supported.+ print_verbose(vformat("OpenXR: %s isn't supported, defaulting to %s.", OpenXRUtil::get_view_configuration_name(view_configuration), OpenXRUtil::get_view_configuration_name(supported_view_configuration_types[0])));+ view_configuration = supported_view_configuration_types[0];+ }}return true;@@ -1320,7 +1326,7 @@ bool OpenXRAPI::create_main_swapchains(const Size2i &p_size) {// We start with our color swapchain...if (color_swapchain_format != 0) {- if (!render_state.main_swapchains[OPENXR_SWAPCHAIN_COLOR].create(0, XR_SWAPCHAIN_USAGE_SAMPLED_BIT | XR_SWAPCHAIN_USAGE_COLOR_ATTACHMENT_BIT | XR_SWAPCHAIN_USAGE_MUTABLE_FORMAT_BIT, color_swapchain_format, render_state.main_swapchain_size.width, render_state.main_swapchain_size.height, sample_count, view_configuration_views.size(), true)) {+ if (!render_state.main_swapchains[OPENXR_SWAPCHAIN_COLOR].create(0, XR_SWAPCHAIN_USAGE_SAMPLED_BIT | XR_SWAPCHAIN_USAGE_COLOR_ATTACHMENT_BIT | XR_SWAPCHAIN_USAGE_MUTABLE_FORMAT_BIT, color_swapchain_format, render_state.main_swapchain_size.width, render_state.main_swapchain_size.height, sample_count, render_state.primary_view_count, true)) {return false;}@@ -1332,14 +1338,14 @@ bool OpenXRAPI::create_main_swapchains(const Size2i &p_size) {// - we support our depth layer extension// Note: Application Space Warp and Frame Synthesis use a separate lower resolution depth buffer.if (depth_swapchain_format != 0 && submit_depth_buffer && OpenXRCompositionLayerDepthExtension::get_singleton()->is_available()) {- if (!render_state.main_swapchains[OPENXR_SWAPCHAIN_DEPTH].create(0, XR_SWAPCHAIN_USAGE_SAMPLED_BIT | XR_SWAPCHAIN_USAGE_DEPTH_STENCIL_ATTACHMENT_BIT, depth_swapchain_format, render_state.main_swapchain_size.width, render_state.main_swapchain_size.height, sample_count, view_configuration_views.size(), true)) {+ if (!render_state.main_swapchains[OPENXR_SWAPCHAIN_DEPTH].create(0, XR_SWAPCHAIN_USAGE_SAMPLED_BIT | XR_SWAPCHAIN_USAGE_DEPTH_STENCIL_ATTACHMENT_BIT, depth_swapchain_format, render_state.main_swapchain_size.width, render_state.main_swapchain_size.height, sample_count, render_state.primary_view_count, true)) {return false;}set_object_name(XR_OBJECT_TYPE_SWAPCHAIN, uint64_t(render_state.main_swapchains[OPENXR_SWAPCHAIN_DEPTH].get_swapchain()), "Main depth swapchain");}- for (uint32_t i = 0; i < render_state.view_count; i++) {+ for (uint32_t i = 0; i < render_state.primary_view_count; i++) {render_state.projection_views[i].subImage.swapchain = render_state.main_swapchains[OPENXR_SWAPCHAIN_COLOR].get_swapchain();render_state.projection_views[i].subImage.imageArrayIndex = i;render_state.projection_views[i].subImage.imageRect.offset.x = 0;@@ -1582,9 +1588,27 @@ void OpenXRAPI::set_form_factor(XrFormFactor p_form_factor) {}uint32_t OpenXRAPI::get_view_count() const {+ // Return the count of all views we have.return view_configuration_views.size();}+uint32_t OpenXRAPI::get_primary_view_count() const {+ // Here we return the view count for our primary view.+ // Additional views will be handled separately.+ switch (view_configuration) {+ case XR_VIEW_CONFIGURATION_TYPE_PRIMARY_MONO:+ return 1;+ case XR_VIEW_CONFIGURATION_TYPE_PRIMARY_STEREO:+ return 2;+ case XR_VIEW_CONFIGURATION_TYPE_PRIMARY_STEREO_WITH_FOVEATED_INSET:+ return 2;+ case XR_VIEW_CONFIGURATION_TYPE_SECONDARY_MONO_FIRST_PERSON_OBSERVER_MSFT:+ return 2;+ default:+ return 1;+ }+}+void OpenXRAPI::set_view_configuration(XrViewConfigurationType p_view_configuration) {ERR_FAIL_COND(is_initialized());@@ -1836,7 +1860,7 @@ bool OpenXRAPI::initialize_session() {return false;}- allocate_view_buffers(view_configuration_views.size(), submit_depth_buffer);+ allocate_view_buffers(view_configuration_views.size(), get_primary_view_count(), submit_depth_buffer);return true;}@@ -1896,6 +1920,10 @@ XrHandTrackerEXT OpenXRAPI::get_hand_tracker(int p_hand_index) {}Size2 OpenXRAPI::get_recommended_target_size() {+ // We return the recommended target size of our primary view here.+ // For foveated inset or additional composition layers,+ // size is handled in those subsystems!+RenderingServer *rendering_server = RenderingServer::get_singleton();ERR_FAIL_COND_V(view_configuration_views.is_empty(), Size2());@@ -1912,6 +1940,12 @@ Size2 OpenXRAPI::get_recommended_target_size() {return target_size;}+Size2i OpenXRAPI::get_recommended_target_size(uint32_t p_view) {+ ERR_FAIL_UNSIGNED_INDEX_V(p_view, view_configuration_views.size(), Size2i());++ return Size2i(view_configuration_views[p_view].recommendedImageRectWidth, view_configuration_views[p_view].recommendedImageRectHeight);+}+void OpenXRAPI::update_head_tracking() {XrResult result;@@ -2093,7 +2127,7 @@ TypedArray<Projection> OpenXRAPI::get_camera_projections(const StringName &p_traXRServer *xr_server = XRServer::get_singleton();ERR_FAIL_NULL_V(xr_server, camera_projections);- for (uint32_t v = 0; v < get_view_count(); v++) {+ for (uint32_t v = 0; v < get_primary_view_count(); v++) {Projection cm;get_view_projection(v, p_z_near, p_z_far, cm);camera_projections.push_back(cm);@@ -2119,7 +2153,7 @@ TypedArray<Transform3D> OpenXRAPI::get_camera_offsets(const StringName &p_trackedouble world_scale = xr_server->get_world_scale();- for (uint32_t v = 0; v < get_view_count(); v++) {+ for (uint32_t v = 0; v < get_primary_view_count(); v++) {Transform3D t;get_view_offset(v, t);@@ -2181,7 +2215,7 @@ bool OpenXRAPI::get_view_projection(uint32_t p_view, double p_z_near, double p_z}// For now we assume these are the same for all views.- set_render_state_near_and_far(p_z_near, p_z_far);+ set_render_state_near_and_far(p_view, p_z_near, p_z_far);// now update our projectionreturn graphics_extension->create_projection_fov(view_fovs[p_view], p_z_near, p_z_far, p_camera_matrix);@@ -2350,7 +2384,7 @@ bool OpenXRAPI::poll_events() {}}-void OpenXRAPI::_allocate_view_buffers_rt(uint32_t p_view_count, bool p_submit_depth_buffer) {+void OpenXRAPI::_allocate_view_buffers_rt(uint32_t p_view_count, uint32_t p_primary_view_count, bool p_submit_depth_buffer) {// Must be called from rendering thread!ERR_NOT_ON_RENDER_THREAD;@@ -2361,6 +2395,7 @@ void OpenXRAPI::_allocate_view_buffers_rt(uint32_t p_view_count, bool p_submit_d// Allocate buffers we'll be populating with view information.openxr_api->render_state.view_count = p_view_count;+ openxr_api->render_state.primary_view_count = p_primary_view_count;openxr_api->render_state.view_poses.resize(p_view_count);openxr_api->render_state.view_fovs.resize(p_view_count);openxr_api->render_state.projection_views.resize(p_view_count);@@ -2483,23 +2518,26 @@ void OpenXRAPI::_set_render_state_view_poses(bool p_is_valid, bool p_should_subm}}-void OpenXRAPI::_set_render_state_near_and_far(double p_z_near, double p_z_far) {+void OpenXRAPI::_set_render_state_near_and_far(uint32_t p_view, double p_z_near, double p_z_far) {ERR_NOT_ON_RENDER_THREAD;OpenXRAPI *openxr_api = OpenXRAPI::get_singleton();ERR_FAIL_NULL(openxr_api);+ // Record our near and far for our first view.+ if (p_view == 0) {+ openxr_api->render_state.z_near = p_z_near;+ openxr_api->render_state.z_far = p_z_far;+ }+// if we're using depth views, make sure we update our near and far there...if (!openxr_api->render_state.depth_views.is_empty()) {- for (XrCompositionLayerDepthInfoKHR &depth_view : openxr_api->render_state.depth_views) {- // As we are using reverse-Z these need to be flipped.- depth_view.nearZ = p_z_far;- depth_view.farZ = p_z_near;- }- }+ ERR_FAIL_UNSIGNED_INDEX(p_view, openxr_api->render_state.depth_views.size());- openxr_api->render_state.z_near = p_z_near;- openxr_api->render_state.z_far = p_z_far;+ // As we are using reverse-Z these need to be flipped.+ openxr_api->render_state.depth_views[p_view].nearZ = p_z_far;+ openxr_api->render_state.depth_views[p_view].farZ = p_z_near;+ }}void OpenXRAPI::_update_main_swapchain_size_rt() {@@ -2523,12 +2561,12 @@ void OpenXRAPI::_update_main_swapchain_size_rt() {#endif}-void OpenXRAPI::allocate_view_buffers(uint32_t p_view_count, bool p_submit_depth_buffer) {+void OpenXRAPI::allocate_view_buffers(uint32_t p_view_count, uint32_t p_primary_view_count, bool p_submit_depth_buffer) {// If we're rendering on a separate thread, we may still be processing the last frame, don't communicate this till we're ready...RenderingServer *rendering_server = RenderingServer::get_singleton();ERR_FAIL_NULL(rendering_server);- rendering_server->call_on_render_thread(callable_mp_static(&OpenXRAPI::_allocate_view_buffers_rt).bind(p_view_count, p_submit_depth_buffer));+ rendering_server->call_on_render_thread(callable_mp_static(&OpenXRAPI::_allocate_view_buffers_rt).bind(p_view_count, p_primary_view_count, p_submit_depth_buffer));}void OpenXRAPI::set_render_session_running(bool p_is_running) {@@ -2585,11 +2623,23 @@ void OpenXRAPI::set_render_state_view_poses(bool p_is_valid, bool p_should_submirendering_server->call_on_render_thread(callable_mp_static(&OpenXRAPI::_set_render_state_view_poses).bind(p_is_valid, p_should_submit_spatial_container_layers, p_orientations, p_positions, p_fovs));}-void OpenXRAPI::set_render_state_near_and_far(double p_z_near, double p_z_far) {+void OpenXRAPI::set_render_state_near_and_far(uint32_t p_view, double p_z_near, double p_z_far) {RenderingServer *rendering_server = RenderingServer::get_singleton();ERR_FAIL_NULL(rendering_server);- rendering_server->call_on_render_thread(callable_mp_static(&OpenXRAPI::_set_render_state_near_and_far).bind(p_z_near, p_z_far));+ rendering_server->call_on_render_thread(callable_mp_static(&OpenXRAPI::_set_render_state_near_and_far).bind(p_view, p_z_near, p_z_far));+}++void OpenXRAPI::set_projection_view_swapchain_rt(uint32_t p_view, XrSwapchain p_swapchain, uint32_t image_array_index, Size2i p_size) {+ ERR_NOT_ON_RENDER_THREAD;+ ERR_FAIL_UNSIGNED_INDEX(p_view, render_state.projection_views.size());++ render_state.projection_views[p_view].subImage.swapchain = p_swapchain;+ render_state.projection_views[p_view].subImage.imageArrayIndex = image_array_index;+ render_state.projection_views[p_view].subImage.imageRect.offset.x = 0;+ render_state.projection_views[p_view].subImage.imageRect.offset.y = 0;+ render_state.projection_views[p_view].subImage.imageRect.extent.width = p_size.width;+ render_state.projection_views[p_view].subImage.imageRect.extent.height = p_size.height;}void OpenXRAPI::update_main_swapchain_size() {@@ -2731,8 +2781,8 @@ bool OpenXRAPI::pre_draw_viewport(RID p_render_target) {return false;}- Size2i swapchain_size = get_recommended_target_size();- bool should_recreate_swapchain = (swapchain_size != render_state.main_swapchain_size);+ Size2i main_swapchain_size = get_recommended_target_size();+ bool should_recreate_swapchain = (main_swapchain_size != render_state.main_swapchain_size);OpenXRFBFoveationExtension *fov_ext = OpenXRFBFoveationExtension::get_singleton();if (fov_ext) {@@ -2761,7 +2811,7 @@ bool OpenXRAPI::pre_draw_viewport(RID p_render_target) {free_main_swapchains();// In with the new.- create_main_swapchains(swapchain_size);+ create_main_swapchains(main_swapchain_size);}// Acquire our images@@ -2877,23 +2927,28 @@ void OpenXRAPI::end_frame() {}}+ // Apply our render region to our primary views.Rect2i new_render_region = (render_state.render_region != Rect2i()) ? render_state.render_region : Rect2i(Point2i(0, 0), render_state.main_swapchain_size);- for (XrCompositionLayerProjectionView &projection_view : render_state.projection_views) {- projection_view.subImage.imageRect.offset.x = new_render_region.position.x;- projection_view.subImage.imageRect.offset.y = new_render_region.position.y;- projection_view.subImage.imageRect.extent.width = new_render_region.size.width;- projection_view.subImage.imageRect.extent.height = new_render_region.size.height;+ for (uint32_t v = 0; v < render_state.primary_view_count; v++) {+ render_state.projection_views[v].subImage.imageRect.offset.x = new_render_region.position.x;+ render_state.projection_views[v].subImage.imageRect.offset.y = new_render_region.position.y;+ render_state.projection_views[v].subImage.imageRect.extent.width = new_render_region.size.width;+ render_state.projection_views[v].subImage.imageRect.extent.height = new_render_region.size.height;}if (render_state.submit_depth_buffer && OpenXRCompositionLayerDepthExtension::get_singleton()->is_available() && !render_state.depth_views.is_empty()) {- for (XrCompositionLayerDepthInfoKHR &depth_view : render_state.depth_views) {- depth_view.subImage.imageRect.offset.x = new_render_region.position.x;- depth_view.subImage.imageRect.offset.y = new_render_region.position.y;- depth_view.subImage.imageRect.extent.width = new_render_region.size.width;- depth_view.subImage.imageRect.extent.height = new_render_region.size.height;+ for (uint32_t v = 0; v < render_state.primary_view_count; v++) {+ render_state.depth_views[v].subImage.imageRect.offset.x = new_render_region.position.x;+ render_state.depth_views[v].subImage.imageRect.offset.y = new_render_region.position.y;+ render_state.depth_views[v].subImage.imageRect.extent.width = new_render_region.size.width;+ render_state.depth_views[v].subImage.imageRect.extent.height = new_render_region.size.height;}}+ for (OpenXRExtensionWrapper *wrapper : registered_extension_wrappers) {+ wrapper->on_post_render();+ }+// must have:// - should_render set to true// - a valid view pose for projection_views[eye].pose to submit layer@@ -3188,10 +3243,10 @@ OpenXRAPI::OpenXRAPI() {case 1: {view_configuration = XR_VIEW_CONFIGURATION_TYPE_PRIMARY_STEREO;} break;- /* we don't support quad and observer configurations (yet)case 2: {- view_configuration = XR_VIEW_CONFIGURATION_TYPE_PRIMARY_QUAD_VARJO;+ view_configuration = XR_VIEW_CONFIGURATION_TYPE_PRIMARY_STEREO_WITH_FOVEATED_INSET; // This also works for XR_VIEW_CONFIGURATION_TYPE_PRIMARY_QUAD_VARJO.} break;+ /* we don't support observer configuration (yet)case 3: {view_configuration = XR_VIEW_CONFIGURATION_TYPE_SECONDARY_MONO_FIRST_PERSON_OBSERVER_MSFT;} break;diff --git a/modules/openxr/openxr_api.h b/modules/openxr/openxr_api.hindex 3823d683285..b28cc423bff 100644--- a/modules/openxr/openxr_api.h+++ b/modules/openxr/openxr_api.h@@ -384,6 +384,7 @@ private:bool use_subsampled_images = true; // We need to default to true for the warning to be shown if we fallback immediately at startup.uint32_t view_count = 0;+ uint32_t primary_view_count = 0;bool view_pose_valid = false;LocalVector<XrPosef> view_poses;LocalVector<XrFovf> view_fovs;@@ -404,7 +405,7 @@ private:OpenXRSwapChainInfo main_swapchains[OPENXR_SWAPCHAIN_MAX];} render_state;- static void _allocate_view_buffers_rt(uint32_t p_view_count, bool p_submit_depth_buffer);+ static void _allocate_view_buffers_rt(uint32_t p_view_count, uint32_t p_primary_view_count, bool p_submit_depth_buffer);static void _set_render_session_running_rt(bool p_is_running);static void _set_render_display_info_rt(XrTime p_predicted_display_time, bool p_should_render);static void _set_render_play_space_rt(uint64_t p_play_space);@@ -412,10 +413,10 @@ private:static void _set_render_state_multiplier_rt(double p_render_target_size_multiplier);static void _set_render_state_render_region_rt(const Rect2i &p_render_region);static void _set_render_state_view_poses(bool p_is_valid, bool p_should_submit_spatial_container_layers, const PackedVector4Array &p_orientations, const PackedVector3Array &p_positions, const PackedVector4Array &p_fovs);- static void _set_render_state_near_and_far(double p_z_near, double p_z_far);+ static void _set_render_state_near_and_far(uint32_t p_view, double p_z_near, double p_z_far);static void _update_main_swapchain_size_rt();- void allocate_view_buffers(uint32_t p_view_count, bool p_submit_depth_buffer);+ void allocate_view_buffers(uint32_t p_view_count, uint32_t p_primary_view_count, bool p_submit_depth_buffer);void set_render_session_running(bool p_is_running);void set_render_display_info(XrTime p_predicted_display_time, bool p_should_render);void set_render_play_space(XrSpace p_play_space);@@ -423,7 +424,7 @@ private:void set_render_state_multiplier(double p_render_target_size_multiplier);void set_render_state_render_region(const Rect2i &p_render_region);void set_render_state_view_poses(bool p_is_valid, bool p_should_submit_spatial_container_layers, const PackedVector4Array &p_orientations, const PackedVector3Array &p_positions, const PackedVector4Array &p_fovs);- void set_render_state_near_and_far(double p_z_near, double p_z_far);+ void set_render_state_near_and_far(uint32_t p_view, double p_z_near, double p_z_far);public:void update_main_swapchain_size();@@ -479,6 +480,7 @@ public:XrFormFactor get_form_factor() const { return form_factor; }uint32_t get_view_count() const;+ uint32_t get_primary_view_count() const;void set_view_configuration(XrViewConfigurationType p_view_configuration);XrViewConfigurationType get_view_configuration() const { return view_configuration; }@@ -507,6 +509,7 @@ public:XrHandTrackerEXT get_hand_tracker(int p_hand_index);Size2 get_recommended_target_size();+ Size2i get_recommended_target_size(uint32_t p_view);XRPose::TrackingConfidence get_head_center(Transform3D &r_transform, Vector3 &r_linear_velocity, Vector3 &r_angular_velocity);TypedArray<Projection> get_camera_projections(const StringName &p_tracker_name, double p_aspect, double p_z_near, double p_z_far);TypedArray<Transform3D> get_camera_offsets(const StringName &p_tracker_name);@@ -534,6 +537,9 @@ public:void post_draw_viewport(RID p_render_target);void end_frame();+ // Rendering+ void set_projection_view_swapchain_rt(uint32_t p_view, XrSwapchain p_swapchain, uint32_t image_array_index, Size2i p_size);+// Display refresh ratefloat get_display_refresh_rate() const;void set_display_refresh_rate(float p_refresh_rate);diff --git a/modules/openxr/openxr_api_extension.cpp b/modules/openxr/openxr_api_extension.cppindex be347474c07..3f819ade881 100644--- a/modules/openxr/openxr_api_extension.cpp+++ b/modules/openxr/openxr_api_extension.cpp@@ -55,6 +55,7 @@ void OpenXRAPIExtension::_bind_methods() {ClassDB::bind_method(D_METHOD("insert_debug_label", "label_name"), &OpenXRAPIExtension::insert_debug_label);ClassDB::bind_method(D_METHOD("get_view_count"), &OpenXRAPIExtension::get_view_count);+ ClassDB::bind_method(D_METHOD("get_primary_view_count"), &OpenXRAPIExtension::get_primary_view_count);ClassDB::bind_method(D_METHOD("get_view_configuration"), &OpenXRAPIExtension::get_view_configuration);ClassDB::bind_method(D_METHOD("is_initialized"), &OpenXRAPIExtension::is_initialized);@@ -198,6 +199,11 @@ uint32_t OpenXRAPIExtension::get_view_count() const {return (uint32_t)OpenXRAPI::get_singleton()->get_view_count();}+uint32_t OpenXRAPIExtension::get_primary_view_count() const {+ ERR_FAIL_NULL_V(OpenXRAPI::get_singleton(), 0);+ return (uint32_t)OpenXRAPI::get_singleton()->get_primary_view_count();+}+uint64_t OpenXRAPIExtension::get_view_configuration() const {ERR_FAIL_NULL_V(OpenXRAPI::get_singleton(), 0);return (uint64_t)OpenXRAPI::get_singleton()->get_view_configuration();diff --git a/modules/openxr/openxr_api_extension.h b/modules/openxr/openxr_api_extension.hindex b8d357ffa37..8bff438c853 100644--- a/modules/openxr/openxr_api_extension.h+++ b/modules/openxr/openxr_api_extension.h@@ -78,6 +78,7 @@ public:void insert_debug_label(const String &p_label_name);uint32_t get_view_count() const;+ uint32_t get_primary_view_count() const;uint64_t get_view_configuration() const;bool is_initialized();diff --git a/modules/openxr/openxr_interface.cpp b/modules/openxr/openxr_interface.cppindex bbee90c634c..f6ccd3d34b8 100644--- a/modules/openxr/openxr_interface.cpp+++ b/modules/openxr/openxr_interface.cpp@@ -69,6 +69,9 @@ void OpenXRInterface::_bind_methods() {ClassDB::bind_method(D_METHOD("is_user_presence_supported"), &OpenXRInterface::is_user_presence_supported);ClassDB::bind_method(D_METHOD("is_user_present"), &OpenXRInterface::is_user_present);+ // View configuration+ ClassDB::bind_method(D_METHOD("get_active_view_configuration"), &OpenXRInterface::get_active_view_configuration);+// Display refresh rateClassDB::bind_method(D_METHOD("get_display_refresh_rate"), &OpenXRInterface::get_display_refresh_rate);ClassDB::bind_method(D_METHOD("set_display_refresh_rate", "refresh_rate"), &OpenXRInterface::set_display_refresh_rate);@@ -145,6 +148,12 @@ void OpenXRInterface::_bind_methods() {BIND_ENUM_CONSTANT(SESSION_STATE_LOSS_PENDING);BIND_ENUM_CONSTANT(SESSION_STATE_EXITING);+ BIND_ENUM_CONSTANT(VIEW_CONFIGURATION_MONO);+ BIND_ENUM_CONSTANT(VIEW_CONFIGURATION_STEREO);+ BIND_ENUM_CONSTANT(VIEW_CONFIGURATION_STEREO_WITH_INSET);+ BIND_ENUM_CONSTANT(VIEW_CONFIGURATION_UNSET);+ BIND_ENUM_CONSTANT(VIEW_CONFIGURATION_UNKNOWN);+BIND_ENUM_CONSTANT(HAND_LEFT);BIND_ENUM_CONSTANT(HAND_RIGHT);BIND_ENUM_CONSTANT(HAND_MAX);@@ -1055,8 +1064,13 @@ Size2 OpenXRInterface::get_render_target_size() {}uint32_t OpenXRInterface::get_view_count() {- // TODO: set this based on our configuration and the active spatial containers.- return 2;+ if (openxr_api == nullptr) {+ return 2;+ } else {+ // We return our primary view count here,+ // this controls our primary view!+ return openxr_api->get_primary_view_count();+ }}void OpenXRInterface::_set_default_pos(Transform3D &r_transform, double p_world_scale, uint64_t p_eye) {@@ -1505,6 +1519,24 @@ bool OpenXRInterface::is_user_present() const {}}+/** View configuration */+OpenXRInterface::ViewConfiguration OpenXRInterface::get_active_view_configuration() const {+ if (!openxr_api || !openxr_api->is_initialized()) {+ return VIEW_CONFIGURATION_UNSET;+ } else {+ switch (openxr_api->get_view_configuration()) {+ case XR_VIEW_CONFIGURATION_TYPE_PRIMARY_MONO:+ return VIEW_CONFIGURATION_MONO;+ case XR_VIEW_CONFIGURATION_TYPE_PRIMARY_STEREO:+ return VIEW_CONFIGURATION_STEREO;+ case XR_VIEW_CONFIGURATION_TYPE_PRIMARY_STEREO_WITH_FOVEATED_INSET:+ return VIEW_CONFIGURATION_STEREO_WITH_INSET;+ default:+ return VIEW_CONFIGURATION_UNKNOWN;+ }+ }+}+/** Hand tracking. */void OpenXRInterface::set_motion_range(const Hand p_hand, const HandMotionRange p_motion_range) {ERR_FAIL_INDEX(p_hand, HAND_MAX);@@ -1649,6 +1681,9 @@ Vector3 OpenXRInterface::get_hand_joint_angular_velocity(Hand p_hand, HandJoints}RID OpenXRInterface::get_vrs_texture() {+ // Note, vrs_mode == VRS_XR should not be used with foveated inset.+ // Not sure where to best check for this (yet).+if (!openxr_api) {return RID();}diff --git a/modules/openxr/openxr_interface.h b/modules/openxr/openxr_interface.hindex 7eead99d032..41e39d6fb3a 100644--- a/modules/openxr/openxr_interface.h+++ b/modules/openxr/openxr_interface.h@@ -243,6 +243,17 @@ public:bool is_user_presence_supported() const;bool is_user_present() const;+ /** View configuration */+ enum ViewConfiguration {+ VIEW_CONFIGURATION_MONO = 0,+ VIEW_CONFIGURATION_STEREO = 1,+ VIEW_CONFIGURATION_STEREO_WITH_INSET = 2,+ VIEW_CONFIGURATION_UNSET = 254,+ VIEW_CONFIGURATION_UNKNOWN = 255,+ };++ ViewConfiguration get_active_view_configuration() const;+/** Hand tracking. */enum Hand {HAND_LEFT,@@ -349,6 +360,7 @@ public:};VARIANT_ENUM_CAST(OpenXRInterface::SessionState)+VARIANT_ENUM_CAST(OpenXRInterface::ViewConfiguration)VARIANT_ENUM_CAST(OpenXRInterface::Hand)VARIANT_ENUM_CAST(OpenXRInterface::HandMotionRange)VARIANT_ENUM_CAST(OpenXRInterface::HandTrackedSource)diff --git a/modules/openxr/register_types.cpp b/modules/openxr/register_types.cppindex 87087279e97..f98581a6724 100644--- a/modules/openxr/register_types.cpp+++ b/modules/openxr/register_types.cpp@@ -59,6 +59,7 @@#include "extensions/openxr_dpad_binding_extension.h"#include "extensions/openxr_eye_gaze_interaction.h"#include "extensions/openxr_fb_display_refresh_rate_extension.h"+#include "extensions/openxr_foveated_inset_extension.h"#include "extensions/openxr_frame_synthesis_extension.h"#include "extensions/openxr_future_extension.h"#include "extensions/openxr_hand_interaction_extension.h"@@ -91,6 +92,7 @@#include "scene/openxr_composition_layer_cylinder.h"#include "scene/openxr_composition_layer_equirect.h"#include "scene/openxr_composition_layer_quad.h"+#include "scene/openxr_foveated_inset_viewport.h"#include "scene/openxr_visibility_mask.h"#ifdef MODULE_GLTF_ENABLED@@ -201,6 +203,7 @@ void initialize_openxr_module(ModuleInitializationLevel p_level) {_register_extension(memnew(OpenXRPerformanceSettingsExtension));_register_extension(memnew(OpenXRValveControllerExtension));_register_extension(memnew(OpenXRKHRGenericController));+ _register_extension(memnew(OpenXRFoveatedInsetExtension));// Futures extension has to be registered as a singleton so extensions can access it._register_extension(memnew(OpenXRFutureExtension), true, true);@@ -306,6 +309,7 @@ void initialize_openxr_module(ModuleInitializationLevel p_level) {GDREGISTER_CLASS(OpenXRRenderModel);GDREGISTER_CLASS(OpenXRRenderModelManager);#endif+ GDREGISTER_CLASS(OpenXRFoveatedInsetViewport);GDREGISTER_CLASS(OpenXRSpatialContainerExtension);GDREGISTER_CLASS(OpenXRSpatialContainerSelfRenderingExtension);diff --git a/modules/openxr/scene/openxr_foveated_inset_viewport.cpp b/modules/openxr/scene/openxr_foveated_inset_viewport.cppnew file mode 100644index 00000000000..a18b5ba14b7--- /dev/null+++ b/modules/openxr/scene/openxr_foveated_inset_viewport.cpp@@ -0,0 +1,136 @@+/**************************************************************************/+/* openxr_foveated_inset_viewport.cpp */+/**************************************************************************/+/* This file is part of: */+/* GODOT ENGINE */+/* https://godotengine.org */+/**************************************************************************/+/* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */+/* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */+/* */+/* Permission is hereby granted, free of charge, to any person obtaining */+/* a copy of this software and associated documentation files (the */+/* "Software"), to deal in the Software without restriction, including */+/* without limitation the rights to use, copy, modify, merge, publish, */+/* distribute, sublicense, and/or sell copies of the Software, and to */+/* permit persons to whom the Software is furnished to do so, subject to */+/* the following conditions: */+/* */+/* The above copyright notice and this permission notice shall be */+/* included in all copies or substantial portions of the Software. */+/* */+/* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */+/* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */+/* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. */+/* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */+/* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */+/* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */+/* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */+/**************************************************************************/++#include "openxr_foveated_inset_viewport.h"++#include "core/config/engine.h"+#include "core/object/class_db.h"+#include "servers/xr/xr_server.h"++#include "modules/openxr/extensions/openxr_foveated_inset_extension.h"++OpenXRFoveatedInsetViewport::OpenXRFoveatedInsetViewport() {+ set_process_internal(true);+ set_process_priority(99999);+ set_update_mode(SubViewport::UPDATE_DISABLED);++ OpenXRFoveatedInsetExtension *fi_ext = OpenXRFoveatedInsetExtension::get_singleton();+ if (!fi_ext) {+ // Silently fail, this is likely being called when we instantiate our class for+ // populating ClassDB.+ return;+ }++ fi_ext->register_viewport(get_viewport_rid());++ xr_origin = memnew(XROrigin3D);+ xr_origin->set_current(false);+ add_child(xr_origin, false, Node3D::INTERNAL_MODE_BACK);++ xr_camera = memnew(XRCamera3D);+ xr_camera->set_tracker(XR_TRACKER_INSET);+ xr_origin->add_child(xr_camera, false, Node3D::INTERNAL_MODE_BACK);+}++OpenXRFoveatedInsetViewport::~OpenXRFoveatedInsetViewport() {+ OpenXRFoveatedInsetExtension *fi_ext = OpenXRFoveatedInsetExtension::get_singleton();+ if (!fi_ext) {+ // Silently fail, this is likely being called after we instantiated our class for+ // populating ClassDB.+ return;+ }++ fi_ext->unregister_viewport(get_viewport_rid());+}++void OpenXRFoveatedInsetViewport::_bind_methods() {+ ClassDB::bind_method(D_METHOD("get_xr_origin3d"), &OpenXRFoveatedInsetViewport::get_xr_origin3d);+ ClassDB::bind_method(D_METHOD("get_xr_camera3d"), &OpenXRFoveatedInsetViewport::get_xr_camera3d);+}++void OpenXRFoveatedInsetViewport::_validate_property(PropertyInfo &p_property) const {+ if (!Engine::get_singleton()->is_editor_hint()) {+ return;+ }+ // Hide properties that are managed by us.+ if (p_property.name == "size" || p_property.name == "size_2d_override" || p_property.name == "size_2d_override_stretch" || p_property.name == "view_count" || p_property.name == "use_xr" || p_property.name == "render_target_update_mode") {+ p_property.usage = PROPERTY_USAGE_NO_EDITOR;+ }+}++void OpenXRFoveatedInsetViewport::_notification(int p_what) {+ if (Engine::get_singleton()->is_editor_hint()) {+ return;+ }++ switch (p_what) {+ case NOTIFICATION_INTERNAL_PROCESS: {+ // Update the size of our viewport+ _update_size();++ // Apply our world origin+ XRServer *xr_server = XRServer::get_singleton();+ if (xr_server) {+ xr_origin->set_transform(xr_server->get_world_origin());+ }+ } break;+ }+}++void OpenXRFoveatedInsetViewport::_update_size() {+ OpenXRAPI *openxr_api = OpenXRAPI::get_singleton();+ OpenXRFoveatedInsetExtension *fi_ext = OpenXRFoveatedInsetExtension::get_singleton();+ if (openxr_api == nullptr || fi_ext == nullptr) {+ set_update_mode(SubViewport::UPDATE_DISABLED);+ return;+ }++ if (!openxr_api->is_initialized()) {+ set_update_mode(SubViewport::UPDATE_DISABLED);+ return;+ }++ if (openxr_api->get_view_configuration() != XR_VIEW_CONFIGURATION_TYPE_PRIMARY_STEREO_WITH_FOVEATED_INSET) {+ WARN_PRINT_ONCE("View configuration is not set to stereo with foveated inset, or this is not supported.");+ set_update_mode(SubViewport::UPDATE_DISABLED);+ return;+ }++ set_update_mode(SubViewport::UPDATE_ALWAYS);++ Size2i new_size = fi_ext->get_render_size();+ if (get_size() != new_size) {+ set_size(new_size);+ }++ if (get_view_count() != 2) {+ set_view_count(2);+ }+}diff --git a/modules/openxr/scene/openxr_foveated_inset_viewport.h b/modules/openxr/scene/openxr_foveated_inset_viewport.hnew file mode 100644index 00000000000..74d95312d5b--- /dev/null+++ b/modules/openxr/scene/openxr_foveated_inset_viewport.h@@ -0,0 +1,56 @@+/**************************************************************************/+/* openxr_foveated_inset_viewport.h */+/**************************************************************************/+/* This file is part of: */+/* GODOT ENGINE */+/* https://godotengine.org */+/**************************************************************************/+/* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */+/* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */+/* */+/* Permission is hereby granted, free of charge, to any person obtaining */+/* a copy of this software and associated documentation files (the */+/* "Software"), to deal in the Software without restriction, including */+/* without limitation the rights to use, copy, modify, merge, publish, */+/* distribute, sublicense, and/or sell copies of the Software, and to */+/* permit persons to whom the Software is furnished to do so, subject to */+/* the following conditions: */+/* */+/* The above copyright notice and this permission notice shall be */+/* included in all copies or substantial portions of the Software. */+/* */+/* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */+/* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */+/* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. */+/* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */+/* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */+/* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */+/* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */+/**************************************************************************/++#pragma once++#include "scene/3d/xr/xr_nodes.h"+#include "scene/main/viewport.h"++class OpenXRFoveatedInsetViewport : public SubViewport {+ GDCLASS(OpenXRFoveatedInsetViewport, SubViewport);++public:+ OpenXRFoveatedInsetViewport();+ ~OpenXRFoveatedInsetViewport();++ XROrigin3D *get_xr_origin3d() const { return xr_origin; }+ XRCamera3D *get_xr_camera3d() const { return xr_camera; }++protected:+ static void _bind_methods();+ void _validate_property(PropertyInfo &p_property) const;+ void _notification(int p_what);++private:+ void _update_size();++ XROrigin3D *xr_origin = nullptr;+ XRCamera3D *xr_camera = nullptr;+};diff --git a/servers/rendering/renderer_rd/storage_rd/render_scene_data_rd.h b/servers/rendering/renderer_rd/storage_rd/render_scene_data_rd.hindex f242aad9354..85a9a1e9beb 100644--- a/servers/rendering/renderer_rd/storage_rd/render_scene_data_rd.h+++ b/servers/rendering/renderer_rd/storage_rd/render_scene_data_rd.h@@ -48,7 +48,6 @@ public:float taa_frame_count = 0.0f;uint32_t camera_visible_layers;bool cam_orthogonal = false;- bool cam_asymmetrical = false;bool flip_y = false;// For billboards to cast correct shadows.