From e74a0f4b0986bb6054e4ebad05fb793f64a105e1 Mon Sep 17 00:00:00 2001 From: David Snopek Date: Thu, 25 Jan 2024 14:07:50 -0600 Subject: [PATCH] OpenXR: Allow moving vendor passthrough extensions to GDExtension --- doc/classes/XRInterface.xml | 12 +- .../openxr/doc_classes/OpenXRAPIExtension.xml | 38 +++ .../OpenXRExtensionWrapperExtension.xml | 6 + .../openxr_extension_wrapper_extension.cpp | 11 + .../openxr_extension_wrapper_extension.h | 4 +- ...penxr_fb_passthrough_extension_wrapper.cpp | 246 ------------------ .../openxr_fb_passthrough_extension_wrapper.h | 231 ---------------- modules/openxr/openxr_api.cpp | 22 +- modules/openxr/openxr_api.h | 13 +- modules/openxr/openxr_api_extension.cpp | 32 +++ modules/openxr/openxr_api_extension.h | 16 ++ modules/openxr/openxr_interface.cpp | 17 +- modules/openxr/openxr_interface.h | 2 - modules/openxr/register_types.cpp | 2 - 14 files changed, 155 insertions(+), 497 deletions(-) delete mode 100644 modules/openxr/extensions/openxr_fb_passthrough_extension_wrapper.cpp delete mode 100644 modules/openxr/extensions/openxr_fb_passthrough_extension_wrapper.h diff --git a/doc/classes/XRInterface.xml b/doc/classes/XRInterface.xml index 29a5b75672a..1a3be669247 100644 --- a/doc/classes/XRInterface.xml +++ b/doc/classes/XRInterface.xml @@ -102,16 +102,18 @@ Is [code]true[/code] if this interface has been initialized. - + Is [code]true[/code] if passthrough is enabled. + [i]Deprecated.[/i] Check if [member environment_blend_mode] is [constant XRInterface.XR_ENV_BLEND_MODE_ALPHA_BLEND], instead. - + Is [code]true[/code] if this interface supports passthrough. + [i]Deprecated.[/i] Check that [constant XRInterface.XR_ENV_BLEND_MODE_ALPHA_BLEND] is supported using [method get_supported_environment_blend_modes], instead. @@ -143,17 +145,19 @@ Sets the active play area mode, will return [code]false[/code] if the mode can't be used with this interface. - + Starts passthrough, will return [code]false[/code] if passthrough couldn't be started. [b]Note:[/b] The viewport used for XR must have a transparent background, otherwise passthrough may not properly render. + [i]Deprecated.[/i] Set the [member environment_blend_mode] to [constant XRInterface.XR_ENV_BLEND_MODE_ALPHA_BLEND], instead. - + Stops passthrough. + [i]Deprecated.[/i] Set the [member environment_blend_mode] to [constant XRInterface.XR_ENV_BLEND_MODE_OPAQUE], instead. diff --git a/modules/openxr/doc_classes/OpenXRAPIExtension.xml b/modules/openxr/doc_classes/OpenXRAPIExtension.xml index e795311651b..1bc3a1a3fc8 100644 --- a/modules/openxr/doc_classes/OpenXRAPIExtension.xml +++ b/modules/openxr/doc_classes/OpenXRAPIExtension.xml @@ -75,6 +75,12 @@ Returns the id of the system, which is a [url=https://registry.khronos.org/OpenXR/specs/1.0/man/html/XrSystemId.html]XrSystemId[/url] cast to an integer. + + + + Returns [enum OpenXRAPIExtension.OpenXRAlphaBlendModeSupport] denoting if [constant XRInterface.XR_ENV_BLEND_MODE_ALPHA_BLEND] is really support, emulated or not supported at all. + + @@ -94,6 +100,20 @@ Returns [code]true[/code] if OpenXR is enabled. + + + + + Registers the given extension as a composition layer provider. + + + + + + + If set to [code]true[/code], an OpenXR extension is loaded which is capable of emulating the [constant XRInterface.XR_ENV_BLEND_MODE_ALPHA_BLEND] blend mode. + + @@ -101,6 +121,13 @@ Creates a [Transform3D] from an [url=https://registry.khronos.org/OpenXR/specs/1.0/man/html/XrPosef.html]XrPosef[/url]. + + + + + Unregisters the given extension as a composition layer provider. + + @@ -111,4 +138,15 @@ + + + Means that [constant XRInterface.XR_ENV_BLEND_MODE_ALPHA_BLEND] isn't supported at all. + + + Means that [constant XRInterface.XR_ENV_BLEND_MODE_ALPHA_BLEND] is really supported. + + + Means that [constant XRInterface.XR_ENV_BLEND_MODE_ALPHA_BLEND] is emulated. + + diff --git a/modules/openxr/doc_classes/OpenXRExtensionWrapperExtension.xml b/modules/openxr/doc_classes/OpenXRExtensionWrapperExtension.xml index fa93704a0aa..b923d9244d6 100644 --- a/modules/openxr/doc_classes/OpenXRExtensionWrapperExtension.xml +++ b/modules/openxr/doc_classes/OpenXRExtensionWrapperExtension.xml @@ -9,6 +9,12 @@ + + + + Returns a pointer to a [code]XrCompositionLayerBaseHeader[/code] struct to provide a composition layer. This will only be called if the extension previously registered itself with [method OpenXRAPIExtension.register_composition_layer_provider]. + + diff --git a/modules/openxr/extensions/openxr_extension_wrapper_extension.cpp b/modules/openxr/extensions/openxr_extension_wrapper_extension.cpp index 23238cabb97..5ad7a97eca7 100644 --- a/modules/openxr/extensions/openxr_extension_wrapper_extension.cpp +++ b/modules/openxr/extensions/openxr_extension_wrapper_extension.cpp @@ -39,6 +39,7 @@ void OpenXRExtensionWrapperExtension::_bind_methods() { GDVIRTUAL_BIND(_set_session_create_and_get_next_pointer, "next_pointer"); GDVIRTUAL_BIND(_set_swapchain_create_info_and_get_next_pointer, "next_pointer"); GDVIRTUAL_BIND(_set_hand_joint_locations_and_get_next_pointer, "hand_index", "next_pointer"); + GDVIRTUAL_BIND(_get_composition_layer); GDVIRTUAL_BIND(_on_register_metadata); GDVIRTUAL_BIND(_on_before_instance_created); GDVIRTUAL_BIND(_on_instance_created, "instance"); @@ -128,6 +129,16 @@ void *OpenXRExtensionWrapperExtension::set_hand_joint_locations_and_get_next_poi return nullptr; } +XrCompositionLayerBaseHeader *OpenXRExtensionWrapperExtension::get_composition_layer() { + uint64_t pointer; + + if (GDVIRTUAL_CALL(_get_composition_layer, pointer)) { + return reinterpret_cast(pointer); + } + + return nullptr; +} + void OpenXRExtensionWrapperExtension::on_register_metadata() { GDVIRTUAL_CALL(_on_register_metadata); } diff --git a/modules/openxr/extensions/openxr_extension_wrapper_extension.h b/modules/openxr/extensions/openxr_extension_wrapper_extension.h index 6acf229e16c..4d8b19f4fd9 100644 --- a/modules/openxr/extensions/openxr_extension_wrapper_extension.h +++ b/modules/openxr/extensions/openxr_extension_wrapper_extension.h @@ -39,7 +39,7 @@ #include "core/os/thread_safe.h" #include "core/variant/native_ptr.h" -class OpenXRExtensionWrapperExtension : public Object, OpenXRExtensionWrapper { +class OpenXRExtensionWrapperExtension : public Object, public OpenXRExtensionWrapper, public OpenXRCompositionLayerProvider { GDCLASS(OpenXRExtensionWrapperExtension, Object); protected: @@ -59,6 +59,7 @@ public: virtual void *set_session_create_and_get_next_pointer(void *p_next_pointer) override; virtual void *set_swapchain_create_info_and_get_next_pointer(void *p_next_pointer) override; virtual void *set_hand_joint_locations_and_get_next_pointer(int p_hand_index, void *p_next_pointer) override; + virtual XrCompositionLayerBaseHeader *get_composition_layer() override; //TODO workaround as GDExtensionPtr return type results in build error in godot-cpp GDVIRTUAL1R(uint64_t, _set_system_properties_and_get_next_pointer, GDExtensionPtr); @@ -66,6 +67,7 @@ public: GDVIRTUAL1R(uint64_t, _set_session_create_and_get_next_pointer, GDExtensionPtr); GDVIRTUAL1R(uint64_t, _set_swapchain_create_info_and_get_next_pointer, GDExtensionPtr); GDVIRTUAL2R(uint64_t, _set_hand_joint_locations_and_get_next_pointer, int, GDExtensionPtr); + GDVIRTUAL0R(uint64_t, _get_composition_layer); virtual void on_register_metadata() override; virtual void on_before_instance_created() override; diff --git a/modules/openxr/extensions/openxr_fb_passthrough_extension_wrapper.cpp b/modules/openxr/extensions/openxr_fb_passthrough_extension_wrapper.cpp deleted file mode 100644 index 3da0ffd9c73..00000000000 --- a/modules/openxr/extensions/openxr_fb_passthrough_extension_wrapper.cpp +++ /dev/null @@ -1,246 +0,0 @@ -/**************************************************************************/ -/* openxr_fb_passthrough_extension_wrapper.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_fb_passthrough_extension_wrapper.h" - -#include "core/os/os.h" -#include "scene/main/viewport.h" -#include "scene/main/window.h" - -using namespace godot; - -OpenXRFbPassthroughExtensionWrapper *OpenXRFbPassthroughExtensionWrapper::singleton = nullptr; - -OpenXRFbPassthroughExtensionWrapper *OpenXRFbPassthroughExtensionWrapper::get_singleton() { - return singleton; -} - -OpenXRFbPassthroughExtensionWrapper::OpenXRFbPassthroughExtensionWrapper() { - singleton = this; -} - -OpenXRFbPassthroughExtensionWrapper::~OpenXRFbPassthroughExtensionWrapper() { - cleanup(); -} - -HashMap OpenXRFbPassthroughExtensionWrapper::get_requested_extensions() { - HashMap request_extensions; - - request_extensions[XR_FB_PASSTHROUGH_EXTENSION_NAME] = &fb_passthrough_ext; - request_extensions[XR_FB_TRIANGLE_MESH_EXTENSION_NAME] = &fb_triangle_mesh_ext; - - return request_extensions; -} - -void OpenXRFbPassthroughExtensionWrapper::cleanup() { - fb_passthrough_ext = false; - fb_triangle_mesh_ext = false; -} - -Viewport *OpenXRFbPassthroughExtensionWrapper::get_main_viewport() { - MainLoop *main_loop = OS::get_singleton()->get_main_loop(); - if (!main_loop) { - print_error("Unable to retrieve main loop"); - return nullptr; - } - - SceneTree *scene_tree = Object::cast_to(main_loop); - if (!scene_tree) { - print_error("Unable to retrieve scene tree"); - return nullptr; - } - - Viewport *viewport = scene_tree->get_root()->get_viewport(); - return viewport; -} - -void OpenXRFbPassthroughExtensionWrapper::on_instance_created(const XrInstance instance) { - if (fb_passthrough_ext) { - bool result = initialize_fb_passthrough_extension(instance); - if (!result) { - print_error("Failed to initialize fb_passthrough extension"); - fb_passthrough_ext = false; - } - } - - if (fb_triangle_mesh_ext) { - bool result = initialize_fb_triangle_mesh_extension(instance); - if (!result) { - print_error("Failed to initialize fb_triangle_mesh extension"); - fb_triangle_mesh_ext = false; - } - } - - if (fb_passthrough_ext) { - OpenXRAPI::get_singleton()->register_composition_layer_provider(this); - } -} - -bool OpenXRFbPassthroughExtensionWrapper::is_passthrough_enabled() { - return fb_passthrough_ext && passthrough_handle != XR_NULL_HANDLE && passthrough_layer != XR_NULL_HANDLE; -} - -bool OpenXRFbPassthroughExtensionWrapper::start_passthrough() { - if (passthrough_handle == XR_NULL_HANDLE) { - return false; - } - - if (is_passthrough_enabled()) { - return true; - } - - // Start the passthrough feature - XrResult result = xrPassthroughStartFB(passthrough_handle); - if (!is_valid_passthrough_result(result, "Failed to start passthrough")) { - stop_passthrough(); - return false; - } - - // Create the passthrough layer - XrPassthroughLayerCreateInfoFB passthrough_layer_config = { - XR_TYPE_PASSTHROUGH_LAYER_CREATE_INFO_FB, - nullptr, - passthrough_handle, - XR_PASSTHROUGH_IS_RUNNING_AT_CREATION_BIT_FB, - XR_PASSTHROUGH_LAYER_PURPOSE_RECONSTRUCTION_FB, - }; - result = xrCreatePassthroughLayerFB(OpenXRAPI::get_singleton()->get_session(), &passthrough_layer_config, &passthrough_layer); - if (!is_valid_passthrough_result(result, "Failed to create the passthrough layer")) { - stop_passthrough(); - return false; - } - - // Check if the the viewport has transparent background - Viewport *viewport = get_main_viewport(); - if (viewport && !viewport->has_transparent_background()) { - print_error("Main viewport doesn't have transparent background! Passthrough may not properly render."); - } - - return true; -} - -void OpenXRFbPassthroughExtensionWrapper::on_session_created(const XrSession session) { - if (fb_passthrough_ext) { - // Create the passthrough feature and start it. - XrPassthroughCreateInfoFB passthrough_create_info = { - XR_TYPE_PASSTHROUGH_CREATE_INFO_FB, - nullptr, - 0, - }; - - XrResult result = xrCreatePassthroughFB(OpenXRAPI::get_singleton()->get_session(), &passthrough_create_info, &passthrough_handle); - if (!OpenXRAPI::get_singleton()->xr_result(result, "Failed to create passthrough")) { - passthrough_handle = XR_NULL_HANDLE; - return; - } - } -} - -XrCompositionLayerBaseHeader *OpenXRFbPassthroughExtensionWrapper::get_composition_layer() { - if (is_passthrough_enabled()) { - composition_passthrough_layer.layerHandle = passthrough_layer; - return (XrCompositionLayerBaseHeader *)&composition_passthrough_layer; - } else { - return nullptr; - } -} - -void OpenXRFbPassthroughExtensionWrapper::stop_passthrough() { - if (!fb_passthrough_ext) { - return; - } - - XrResult result; - if (passthrough_layer != XR_NULL_HANDLE) { - // Destroy the layer - result = xrDestroyPassthroughLayerFB(passthrough_layer); - OpenXRAPI::get_singleton()->xr_result(result, "Unable to destroy passthrough layer"); - passthrough_layer = XR_NULL_HANDLE; - } - - if (passthrough_handle != XR_NULL_HANDLE) { - result = xrPassthroughPauseFB(passthrough_handle); - OpenXRAPI::get_singleton()->xr_result(result, "Unable to stop passthrough feature"); - } -} - -void OpenXRFbPassthroughExtensionWrapper::on_session_destroyed() { - if (fb_passthrough_ext) { - stop_passthrough(); - - XrResult result; - if (passthrough_handle != XR_NULL_HANDLE) { - result = xrDestroyPassthroughFB(passthrough_handle); - OpenXRAPI::get_singleton()->xr_result(result, "Unable to destroy passthrough feature"); - passthrough_handle = XR_NULL_HANDLE; - } - } -} - -void OpenXRFbPassthroughExtensionWrapper::on_instance_destroyed() { - if (fb_passthrough_ext) { - OpenXRAPI::get_singleton()->unregister_composition_layer_provider(this); - } - cleanup(); -} - -bool OpenXRFbPassthroughExtensionWrapper::initialize_fb_passthrough_extension(const XrInstance p_instance) { - ERR_FAIL_NULL_V(OpenXRAPI::get_singleton(), false); - - EXT_INIT_XR_FUNC_V(xrCreatePassthroughFB); - EXT_INIT_XR_FUNC_V(xrDestroyPassthroughFB); - EXT_INIT_XR_FUNC_V(xrPassthroughStartFB); - EXT_INIT_XR_FUNC_V(xrPassthroughPauseFB); - EXT_INIT_XR_FUNC_V(xrCreatePassthroughLayerFB); - EXT_INIT_XR_FUNC_V(xrDestroyPassthroughLayerFB); - EXT_INIT_XR_FUNC_V(xrPassthroughLayerPauseFB); - EXT_INIT_XR_FUNC_V(xrPassthroughLayerResumeFB); - EXT_INIT_XR_FUNC_V(xrPassthroughLayerSetStyleFB); - EXT_INIT_XR_FUNC_V(xrCreateGeometryInstanceFB); - EXT_INIT_XR_FUNC_V(xrDestroyGeometryInstanceFB); - EXT_INIT_XR_FUNC_V(xrGeometryInstanceSetTransformFB); - - return true; -} - -bool OpenXRFbPassthroughExtensionWrapper::initialize_fb_triangle_mesh_extension(const XrInstance p_instance) { - ERR_FAIL_NULL_V(OpenXRAPI::get_singleton(), false); - - EXT_INIT_XR_FUNC_V(xrCreateTriangleMeshFB); - EXT_INIT_XR_FUNC_V(xrDestroyTriangleMeshFB); - EXT_INIT_XR_FUNC_V(xrTriangleMeshGetVertexBufferFB); - EXT_INIT_XR_FUNC_V(xrTriangleMeshGetIndexBufferFB); - EXT_INIT_XR_FUNC_V(xrTriangleMeshBeginUpdateFB); - EXT_INIT_XR_FUNC_V(xrTriangleMeshEndUpdateFB); - EXT_INIT_XR_FUNC_V(xrTriangleMeshBeginVertexBufferUpdateFB); - EXT_INIT_XR_FUNC_V(xrTriangleMeshEndVertexBufferUpdateFB); - - return true; -} diff --git a/modules/openxr/extensions/openxr_fb_passthrough_extension_wrapper.h b/modules/openxr/extensions/openxr_fb_passthrough_extension_wrapper.h deleted file mode 100644 index 045e4242026..00000000000 --- a/modules/openxr/extensions/openxr_fb_passthrough_extension_wrapper.h +++ /dev/null @@ -1,231 +0,0 @@ -/**************************************************************************/ -/* openxr_fb_passthrough_extension_wrapper.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. */ -/**************************************************************************/ - -#ifndef OPENXR_FB_PASSTHROUGH_EXTENSION_WRAPPER_H -#define OPENXR_FB_PASSTHROUGH_EXTENSION_WRAPPER_H - -#include "../openxr_api.h" -#include "../util.h" -#include "openxr_composition_layer_provider.h" -#include "openxr_extension_wrapper.h" - -#include - -class Viewport; - -// Wrapper for the set of Facebook XR passthrough extensions. -class OpenXRFbPassthroughExtensionWrapper : public OpenXRExtensionWrapper, public OpenXRCompositionLayerProvider { -public: - OpenXRFbPassthroughExtensionWrapper(); - ~OpenXRFbPassthroughExtensionWrapper(); - - virtual HashMap get_requested_extensions() override; - - void on_instance_created(const XrInstance instance) override; - - void on_session_created(const XrSession session) override; - - void on_session_destroyed() override; - - void on_instance_destroyed() override; - - XrCompositionLayerBaseHeader *get_composition_layer() override; - - bool is_passthrough_supported() { - return fb_passthrough_ext; - } - - bool is_passthrough_enabled(); - - bool start_passthrough(); - - void stop_passthrough(); - - static OpenXRFbPassthroughExtensionWrapper *get_singleton(); - -private: - // Create a passthrough feature - EXT_PROTO_XRRESULT_FUNC3(xrCreatePassthroughFB, - (XrSession), session, - (const XrPassthroughCreateInfoFB *), create_info, - (XrPassthroughFB *), feature_out) - - // Destroy a previously created passthrough feature - EXT_PROTO_XRRESULT_FUNC1(xrDestroyPassthroughFB, (XrPassthroughFB), feature) - - //*** Passthrough feature state management functions ********* - // Start the passthrough feature - EXT_PROTO_XRRESULT_FUNC1(xrPassthroughStartFB, (XrPassthroughFB), passthrough) - // Pause the passthrough feature - EXT_PROTO_XRRESULT_FUNC1(xrPassthroughPauseFB, (XrPassthroughFB), passthrough) - - EXT_PROTO_XRRESULT_FUNC3(xrCreatePassthroughLayerFB, (XrSession), session, - (const XrPassthroughLayerCreateInfoFB *), config, - (XrPassthroughLayerFB *), layer_out) - - EXT_PROTO_XRRESULT_FUNC1(xrDestroyPassthroughLayerFB, (XrPassthroughLayerFB), layer) - - EXT_PROTO_XRRESULT_FUNC1(xrPassthroughLayerPauseFB, (XrPassthroughLayerFB), layer) - EXT_PROTO_XRRESULT_FUNC1(xrPassthroughLayerResumeFB, (XrPassthroughLayerFB), layer) - - // Set the style of an existing passthrough layer. If the enabled feature set - // doesn’t change, this is a lightweight operation that can be called in every - // frame to animate the style. Changes that may incur a bigger cost: - // - Enabling/disabling the color mapping, or changing the type of mapping - // (monochromatic to RGBA or back). - // - Changing `textureOpacityFactor` from 0 to non-zero or vice versa - // - Changing `edgeColor[3]` from 0 to non-zero or vice versa - // NOTE: For XR_FB_passthrough, all color values are treated as linear. - EXT_PROTO_XRRESULT_FUNC2(xrPassthroughLayerSetStyleFB, - (XrPassthroughLayerFB), layer, - (const XrPassthroughStyleFB *), style) - - // Create a geometry instance to be used as a projection surface for passthrough. - // A geometry instance assigns a triangle mesh as part of the specified layer's - // projection surface. - // The operation is only valid if the passthrough layer's purpose has been set to - // `XR_PASSTHROUGH_LAYER_PURPOSE_PROJECTED_FB`. Otherwise, the call this function will - // result in an error. In the specified layer, Passthrough will be visible where the view - // is covered by the user-specified geometries. - // - // A triangle mesh object can be instantiated multiple times - in the same or different layers' - // projection surface. Each instantiation has its own transformation, which - // can be updated using `xrGeometryInstanceSetTransformFB`. - EXT_PROTO_XRRESULT_FUNC3(xrCreateGeometryInstanceFB, - (XrSession), session, - (const XrGeometryInstanceCreateInfoFB *), create_info, - (XrGeometryInstanceFB *), out_geometry_instance) - - // Destroys a previously created geometry instance from passthrough rendering. - // This removes the geometry instance from passthrough rendering. - // The operation has no effect on other instances or the underlying mesh. - EXT_PROTO_XRRESULT_FUNC1(xrDestroyGeometryInstanceFB, (XrGeometryInstanceFB), instance) - - // Update the transformation of a passthrough geometry instance. - EXT_PROTO_XRRESULT_FUNC2(xrGeometryInstanceSetTransformFB, - (XrGeometryInstanceFB), instance, - (const XrGeometryInstanceTransformFB *), transformation) - - // Create a triangle mesh geometry object. - // Depending on the behavior flags, the mesh could be created immutable (data is assigned - // at creation and cannot be changed) or mutable (the mesh is created empty and can be updated - // by calling begin/end update functions). - EXT_PROTO_XRRESULT_FUNC3(xrCreateTriangleMeshFB, - (XrSession), session, - (const XrTriangleMeshCreateInfoFB *), create_info, - (XrTriangleMeshFB *), out_triangle_mesh) - - // Destroy an `XrTriangleMeshFB` object along with its data. The mesh buffers must not be - // accessed anymore after their parent mesh object has been destroyed. - EXT_PROTO_XRRESULT_FUNC1(xrDestroyTriangleMeshFB, (XrTriangleMeshFB), mesh) - - // Retrieve a pointer to the vertex buffer. The vertex buffer is structured as an array of 3 floats - // per vertex representing x, y, and z: `[x0, y0, z0, x1, y1, z1, ...]`. The size of the buffer is - // `maxVertexCount * 3` floats. The application must call `xrTriangleMeshBeginUpdateFB` or - // `xrTriangleMeshBeginVertexBufferUpdateFB` before making modifications to the vertex - // buffer. The buffer location is guaranteed to remain constant over the lifecycle of the mesh - // object. - EXT_PROTO_XRRESULT_FUNC2(xrTriangleMeshGetVertexBufferFB, - (XrTriangleMeshFB), mesh, - (XrVector3f **), out_vertex_buffer) - - // Retrieve the index buffer that defines the topology of the triangle mesh. Each triplet of - // consecutive elements point to three vertices in the vertex buffer and thus form a triangle. The - // size of each element is `indexElementSize` bytes, and thus the size of the buffer is - // `maxTriangleCount * 3 * indexElementSize` bytes. The application must call - // `xrTriangleMeshBeginUpdateFB` before making modifications to the index buffer. The buffer - // location is guaranteed to remain constant over the lifecycle of the mesh object. - EXT_PROTO_XRRESULT_FUNC2(xrTriangleMeshGetIndexBufferFB, - (XrTriangleMeshFB), mesh, - (uint32_t **), out_index_buffer) - - // Begin updating the mesh buffer data. The application must call this function before it makes any - // modifications to the buffers retrieved by `xrTriangleMeshGetVertexBufferFB` and - // `xrTriangleMeshGetIndexBufferFB`. If only the vertex buffer needs to be updated, - // `xrTriangleMeshBeginVertexBufferUpdateFB` can be used instead. To commit the - // modifications, the application must call `xrTriangleMeshEndUpdateFB`. - EXT_PROTO_XRRESULT_FUNC1(xrTriangleMeshBeginUpdateFB, (XrTriangleMeshFB), mesh) - - // Signal the API that the application has finished updating the mesh buffers after a call to - // `xrTriangleMeshBeginUpdateFB`. `vertexCount` and `triangleCount` specify the actual - // number of primitives that make up the mesh after the update. They must be larger than zero but - // smaller or equal to the maximum counts defined at create time. Buffer data beyond these counts - // is ignored. - EXT_PROTO_XRRESULT_FUNC3(xrTriangleMeshEndUpdateFB, - (XrTriangleMeshFB), mesh, - (uint32_t), vertexCount, - (uint32_t), triangle_count) - - // Update the vertex positions of a triangle mesh. Can only be called once the mesh topology has - // been set using `xrTriangleMeshBeginUpdateFB`/`xrTriangleMeshEndUpdateFB`. The - // vertex count is defined by the last invocation to `xrTriangleMeshEndUpdateFB`. Once the - // modification is done, `xrTriangleMeshEndVertexBufferUpdateFB` must be called. - EXT_PROTO_XRRESULT_FUNC2(xrTriangleMeshBeginVertexBufferUpdateFB, - (XrTriangleMeshFB), mesh, - (uint32_t *), out_vertex_count) - - // Signal the API that the contents of the vertex buffer data has been updated - // after a call to `xrTriangleMeshBeginVertexBufferUpdateFB`. - EXT_PROTO_XRRESULT_FUNC1(xrTriangleMeshEndVertexBufferUpdateFB, (XrTriangleMeshFB), mesh) - - bool initialize_fb_passthrough_extension(const XrInstance instance); - - bool initialize_fb_triangle_mesh_extension(const XrInstance instance); - - void cleanup(); - - // TODO: Temporary workaround (https://github.com/GodotVR/godot_openxr/issues/138) - // Address a bug in the passthrough api where XR_ERROR_UNEXPECTED_STATE_PASSTHROUGH_FB is - // returned even when the operation is valid on Meta Quest devices. - // The issue should be addressed on that platform in OS release v37. - inline bool is_valid_passthrough_result(XrResult result, const char *format) { - return OpenXRAPI::get_singleton()->xr_result(result, format) || result == XR_ERROR_UNEXPECTED_STATE_PASSTHROUGH_FB; - } - - Viewport *get_main_viewport(); - - static OpenXRFbPassthroughExtensionWrapper *singleton; - - bool fb_passthrough_ext = false; // required for any passthrough functionality - bool fb_triangle_mesh_ext = false; // only use for projected passthrough - - XrPassthroughFB passthrough_handle = XR_NULL_HANDLE; - XrPassthroughLayerFB passthrough_layer = XR_NULL_HANDLE; - - XrCompositionLayerPassthroughFB composition_passthrough_layer = { - XR_TYPE_COMPOSITION_LAYER_PASSTHROUGH_FB, - nullptr, - XR_COMPOSITION_LAYER_BLEND_TEXTURE_SOURCE_ALPHA_BIT, - XR_NULL_HANDLE, - XR_NULL_HANDLE, - }; -}; - -#endif // OPENXR_FB_PASSTHROUGH_EXTENSION_WRAPPER_H diff --git a/modules/openxr/openxr_api.cpp b/modules/openxr/openxr_api.cpp index 7b777a98457..14a1aaa6866 100644 --- a/modules/openxr/openxr_api.cpp +++ b/modules/openxr/openxr_api.cpp @@ -56,7 +56,6 @@ #include "extensions/openxr_composition_layer_depth_extension.h" #include "extensions/openxr_fb_display_refresh_rate_extension.h" #include "extensions/openxr_fb_foveation_extension.h" -#include "extensions/openxr_fb_passthrough_extension_wrapper.h" #include "extensions/openxr_fb_update_swapchain_extension.h" #ifdef ANDROID_ENABLED @@ -3099,11 +3098,30 @@ bool OpenXRAPI::is_environment_blend_mode_supported(XrEnvironmentBlendMode p_ble } bool OpenXRAPI::set_environment_blend_mode(XrEnvironmentBlendMode p_blend_mode) { + if (emulate_environment_blend_mode_alpha_blend && p_blend_mode == XR_ENVIRONMENT_BLEND_MODE_ALPHA_BLEND) { + requested_environment_blend_mode = XR_ENVIRONMENT_BLEND_MODE_ALPHA_BLEND; + environment_blend_mode = XR_ENVIRONMENT_BLEND_MODE_OPAQUE; + return true; + } // We allow setting this when not initialized and will check if it is supported when initializing. // After OpenXR is initialized we verify we're setting a supported blend mode. - if (!is_initialized() || is_environment_blend_mode_supported(p_blend_mode)) { + else if (!is_initialized() || is_environment_blend_mode_supported(p_blend_mode)) { + requested_environment_blend_mode = p_blend_mode; environment_blend_mode = p_blend_mode; return true; } return false; } + +void OpenXRAPI::set_emulate_environment_blend_mode_alpha_blend(bool p_enabled) { + emulate_environment_blend_mode_alpha_blend = p_enabled; +} + +OpenXRAPI::OpenXRAlphaBlendModeSupport OpenXRAPI::is_environment_blend_mode_alpha_blend_supported() { + if (is_environment_blend_mode_supported(XR_ENVIRONMENT_BLEND_MODE_ALPHA_BLEND)) { + return OPENXR_ALPHA_BLEND_MODE_SUPPORT_REAL; + } else if (emulate_environment_blend_mode_alpha_blend) { + return OPENXR_ALPHA_BLEND_MODE_SUPPORT_EMULATING; + } + return OPENXR_ALPHA_BLEND_MODE_SUPPORT_NONE; +} diff --git a/modules/openxr/openxr_api.h b/modules/openxr/openxr_api.h index 8cc6be3a9d9..f3cdcc94c52 100644 --- a/modules/openxr/openxr_api.h +++ b/modules/openxr/openxr_api.h @@ -104,8 +104,10 @@ private: // blend mode XrEnvironmentBlendMode environment_blend_mode = XR_ENVIRONMENT_BLEND_MODE_OPAQUE; + XrEnvironmentBlendMode requested_environment_blend_mode = XR_ENVIRONMENT_BLEND_MODE_OPAQUE; uint32_t num_supported_environment_blend_modes = 0; XrEnvironmentBlendMode *supported_environment_blend_modes = nullptr; + bool emulate_environment_blend_mode_alpha_blend = false; // state XrInstance instance = XR_NULL_HANDLE; @@ -428,7 +430,16 @@ public: const XrEnvironmentBlendMode *get_supported_environment_blend_modes(uint32_t &count); bool is_environment_blend_mode_supported(XrEnvironmentBlendMode p_blend_mode) const; bool set_environment_blend_mode(XrEnvironmentBlendMode p_blend_mode); - XrEnvironmentBlendMode get_environment_blend_mode() const { return environment_blend_mode; } + XrEnvironmentBlendMode get_environment_blend_mode() const { return requested_environment_blend_mode; } + + enum OpenXRAlphaBlendModeSupport { + OPENXR_ALPHA_BLEND_MODE_SUPPORT_NONE = 0, + OPENXR_ALPHA_BLEND_MODE_SUPPORT_REAL = 1, + OPENXR_ALPHA_BLEND_MODE_SUPPORT_EMULATING = 2, + }; + + void set_emulate_environment_blend_mode_alpha_blend(bool p_enabled); + OpenXRAlphaBlendModeSupport is_environment_blend_mode_alpha_blend_supported(); OpenXRAPI(); ~OpenXRAPI(); diff --git a/modules/openxr/openxr_api_extension.cpp b/modules/openxr/openxr_api_extension.cpp index f0f0835f784..d9e282e218c 100644 --- a/modules/openxr/openxr_api_extension.cpp +++ b/modules/openxr/openxr_api_extension.cpp @@ -30,6 +30,8 @@ #include "openxr_api_extension.h" +#include "extensions/openxr_extension_wrapper_extension.h" + void OpenXRAPIExtension::_bind_methods() { ClassDB::bind_method(D_METHOD("get_instance"), &OpenXRAPIExtension::get_instance); ClassDB::bind_method(D_METHOD("get_system_id"), &OpenXRAPIExtension::get_system_id); @@ -48,6 +50,16 @@ void OpenXRAPIExtension::_bind_methods() { ClassDB::bind_method(D_METHOD("get_play_space"), &OpenXRAPIExtension::get_play_space); ClassDB::bind_method(D_METHOD("get_next_frame_time"), &OpenXRAPIExtension::get_next_frame_time); ClassDB::bind_method(D_METHOD("can_render"), &OpenXRAPIExtension::can_render); + + ClassDB::bind_method(D_METHOD("register_composition_layer_provider", "extension"), &OpenXRAPIExtension::register_composition_layer_provider); + ClassDB::bind_method(D_METHOD("unregister_composition_layer_provider", "extension"), &OpenXRAPIExtension::unregister_composition_layer_provider); + + ClassDB::bind_method(D_METHOD("set_emulate_environment_blend_mode_alpha_blend", "enabled"), &OpenXRAPIExtension::set_emulate_environment_blend_mode_alpha_blend); + ClassDB::bind_method(D_METHOD("is_environment_blend_mode_alpha_supported"), &OpenXRAPIExtension::is_environment_blend_mode_alpha_blend_supported); + + BIND_ENUM_CONSTANT(OPENXR_ALPHA_BLEND_MODE_SUPPORT_NONE); + BIND_ENUM_CONSTANT(OPENXR_ALPHA_BLEND_MODE_SUPPORT_REAL); + BIND_ENUM_CONSTANT(OPENXR_ALPHA_BLEND_MODE_SUPPORT_EMULATING); } uint64_t OpenXRAPIExtension::get_instance() { @@ -126,5 +138,25 @@ bool OpenXRAPIExtension::can_render() { return OpenXRAPI::get_singleton()->can_render(); } +void OpenXRAPIExtension::register_composition_layer_provider(OpenXRExtensionWrapperExtension *p_extension) { + ERR_FAIL_NULL(OpenXRAPI::get_singleton()); + OpenXRAPI::get_singleton()->register_composition_layer_provider(p_extension); +} + +void OpenXRAPIExtension::unregister_composition_layer_provider(OpenXRExtensionWrapperExtension *p_extension) { + ERR_FAIL_NULL(OpenXRAPI::get_singleton()); + OpenXRAPI::get_singleton()->unregister_composition_layer_provider(p_extension); +} + +void OpenXRAPIExtension::set_emulate_environment_blend_mode_alpha_blend(bool p_enabled) { + ERR_FAIL_NULL(OpenXRAPI::get_singleton()); + OpenXRAPI::get_singleton()->set_emulate_environment_blend_mode_alpha_blend(p_enabled); +} + +OpenXRAPIExtension::OpenXRAlphaBlendModeSupport OpenXRAPIExtension::is_environment_blend_mode_alpha_blend_supported() { + ERR_FAIL_NULL_V(OpenXRAPI::get_singleton(), OPENXR_ALPHA_BLEND_MODE_SUPPORT_NONE); + return (OpenXRAPIExtension::OpenXRAlphaBlendModeSupport)OpenXRAPI::get_singleton()->is_environment_blend_mode_alpha_blend_supported(); +} + OpenXRAPIExtension::OpenXRAPIExtension() { } diff --git a/modules/openxr/openxr_api_extension.h b/modules/openxr/openxr_api_extension.h index 98f87c7aa17..82344c1d060 100644 --- a/modules/openxr/openxr_api_extension.h +++ b/modules/openxr/openxr_api_extension.h @@ -38,6 +38,8 @@ #include "core/os/thread_safe.h" #include "core/variant/native_ptr.h" +class OpenXRExtensionWrapperExtension; + class OpenXRAPIExtension : public RefCounted { GDCLASS(OpenXRAPIExtension, RefCounted); @@ -70,7 +72,21 @@ public: int64_t get_next_frame_time(); bool can_render(); + void register_composition_layer_provider(OpenXRExtensionWrapperExtension *p_extension); + void unregister_composition_layer_provider(OpenXRExtensionWrapperExtension *p_extension); + + enum OpenXRAlphaBlendModeSupport { + OPENXR_ALPHA_BLEND_MODE_SUPPORT_NONE = 0, + OPENXR_ALPHA_BLEND_MODE_SUPPORT_REAL = 1, + OPENXR_ALPHA_BLEND_MODE_SUPPORT_EMULATING = 2, + }; + + void set_emulate_environment_blend_mode_alpha_blend(bool p_enabled); + OpenXRAlphaBlendModeSupport is_environment_blend_mode_alpha_blend_supported(); + OpenXRAPIExtension(); }; +VARIANT_ENUM_CAST(OpenXRAPIExtension::OpenXRAlphaBlendModeSupport); + #endif // OPENXR_API_EXTENSION_H diff --git a/modules/openxr/openxr_interface.cpp b/modules/openxr/openxr_interface.cpp index 6b311b73a8b..d12d241388f 100644 --- a/modules/openxr/openxr_interface.cpp +++ b/modules/openxr/openxr_interface.cpp @@ -1152,21 +1152,19 @@ void OpenXRInterface::end_frame() { } bool OpenXRInterface::is_passthrough_supported() { - return passthrough_wrapper != nullptr && passthrough_wrapper->is_passthrough_supported(); + return get_supported_environment_blend_modes().find(XR_ENV_BLEND_MODE_ALPHA_BLEND); } bool OpenXRInterface::is_passthrough_enabled() { - return passthrough_wrapper != nullptr && passthrough_wrapper->is_passthrough_enabled(); + return get_environment_blend_mode() == XR_ENV_BLEND_MODE_ALPHA_BLEND; } bool OpenXRInterface::start_passthrough() { - return passthrough_wrapper != nullptr && passthrough_wrapper->start_passthrough(); + return set_environment_blend_mode(XR_ENV_BLEND_MODE_ALPHA_BLEND); } void OpenXRInterface::stop_passthrough() { - if (passthrough_wrapper) { - passthrough_wrapper->stop_passthrough(); - } + set_environment_blend_mode(XR_ENV_BLEND_MODE_OPAQUE); } Array OpenXRInterface::get_supported_environment_blend_modes() { @@ -1198,6 +1196,11 @@ Array OpenXRInterface::get_supported_environment_blend_modes() { WARN_PRINT("Unsupported blend mode found: " + String::num_int64(int64_t(env_blend_modes[i]))); } } + + if (openxr_api->is_environment_blend_mode_alpha_blend_supported() == OpenXRAPI::OPENXR_ALPHA_BLEND_MODE_SUPPORT_EMULATING) { + modes.push_back(XR_ENV_BLEND_MODE_ALPHA_BLEND); + } + return modes; } @@ -1416,8 +1419,6 @@ OpenXRInterface::OpenXRInterface() { _set_default_pos(head_transform, 1.0, 0); _set_default_pos(transform_for_view[0], 1.0, 1); _set_default_pos(transform_for_view[1], 1.0, 2); - - passthrough_wrapper = OpenXRFbPassthroughExtensionWrapper::get_singleton(); } OpenXRInterface::~OpenXRInterface() { diff --git a/modules/openxr/openxr_interface.h b/modules/openxr/openxr_interface.h index ca95fdf04df..aee9751d6b9 100644 --- a/modules/openxr/openxr_interface.h +++ b/modules/openxr/openxr_interface.h @@ -32,7 +32,6 @@ #define OPENXR_INTERFACE_H #include "action_map/openxr_action_map.h" -#include "extensions/openxr_fb_passthrough_extension_wrapper.h" #include "extensions/openxr_hand_tracking_extension.h" #include "openxr_api.h" @@ -49,7 +48,6 @@ private: OpenXRAPI *openxr_api = nullptr; bool initialized = false; XRInterface::TrackingStatus tracking_state; - OpenXRFbPassthroughExtensionWrapper *passthrough_wrapper = nullptr; // At a minimum we need a tracker for our head Ref head; diff --git a/modules/openxr/register_types.cpp b/modules/openxr/register_types.cpp index 04411a0c579..a0a93c8694b 100644 --- a/modules/openxr/register_types.cpp +++ b/modules/openxr/register_types.cpp @@ -44,7 +44,6 @@ #include "extensions/openxr_composition_layer_depth_extension.h" #include "extensions/openxr_eye_gaze_interaction.h" #include "extensions/openxr_fb_display_refresh_rate_extension.h" -#include "extensions/openxr_fb_passthrough_extension_wrapper.h" #include "extensions/openxr_hand_tracking_extension.h" #include "extensions/openxr_htc_controller_extension.h" #include "extensions/openxr_htc_vive_tracker_extension.h" @@ -114,7 +113,6 @@ void initialize_openxr_module(ModuleInitializationLevel p_level) { OpenXRAPI::register_extension_wrapper(memnew(OpenXRHTCControllerExtension)); OpenXRAPI::register_extension_wrapper(memnew(OpenXRHTCViveTrackerExtension)); OpenXRAPI::register_extension_wrapper(memnew(OpenXRHuaweiControllerExtension)); - OpenXRAPI::register_extension_wrapper(memnew(OpenXRFbPassthroughExtensionWrapper)); OpenXRAPI::register_extension_wrapper(memnew(OpenXRDisplayRefreshRateExtension)); OpenXRAPI::register_extension_wrapper(memnew(OpenXRWMRControllerExtension)); OpenXRAPI::register_extension_wrapper(memnew(OpenXRML2ControllerExtension));