-
Notifications
You must be signed in to change notification settings - Fork 25
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add OpenXR extension wrapper for fb_scene
- Loading branch information
Showing
3 changed files
with
293 additions
and
0 deletions.
There are no files selected for viewing
124 changes: 124 additions & 0 deletions
124
godotopenxrmeta/src/main/cpp/include/openxr_fb_scene_extension_wrapper.h
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,124 @@ | ||
/**************************************************************************/ | ||
/* openxr_fb_scene_extension_wrapper.h */ | ||
/**************************************************************************/ | ||
/* This file is part of: */ | ||
/* GODOT XR */ | ||
/* https://godotengine.org */ | ||
/**************************************************************************/ | ||
/* Copyright (c) 2022-present Godot XR contributors (see CONTRIBUTORS.md) */ | ||
/* */ | ||
/* Original contributed implementation: */ | ||
/* Copyright (c) 2022-2023 MattaKis Consulting Kft. (Migeran) */ | ||
/* */ | ||
/* 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_SCENE_EXTENSION_WRAPPER_H | ||
#define OPENXR_FB_SCENE_EXTENSION_WRAPPER_H | ||
|
||
#include <godot_cpp/classes/open_xr_extension_wrapper_extension.hpp> | ||
#include <openxr/openxr.h> | ||
#include <openxr/fb_scene.h> | ||
#include <godot_cpp/variant/utility_functions.hpp> | ||
|
||
#include "util.h" | ||
|
||
#include <map> | ||
|
||
using namespace godot; | ||
|
||
struct XrSceneObjectInternal { | ||
XrUuidEXT uuid; | ||
XrSpace space; | ||
std::optional<std::string> label; | ||
|
||
// Vertices and lines on a plane, probably use this for a floor / ceiling as they are irregularly shaped | ||
// We store a std::vector instead of XrBoundary2DFB to own the vertex memory | ||
std::optional<std::vector<XrVector2f>> boundary2D; | ||
// A rectangle containing the whole thing, perfect for desks / tables / play surfaces | ||
std::optional<XrRect2Df> boundingBox2D; | ||
// 3D box for the whole thing, better for obstacles and other objects not used as a surface | ||
std::optional<XrRect3DfFB> boundingBox3D; | ||
}; | ||
|
||
// Wrapper for the set of Facebook XR scene extension. | ||
class OpenXRFbSceneExtensionWrapper : public OpenXRExtensionWrapperExtension { | ||
GDCLASS(OpenXRFbSceneExtensionWrapper, OpenXRExtensionWrapperExtension); | ||
|
||
public: | ||
godot::Dictionary _get_requested_extensions() override; | ||
|
||
void _on_instance_created(uint64_t instance) override; | ||
|
||
void _on_instance_destroyed() override; | ||
|
||
bool is_scene_supported() { | ||
return fb_scene_ext; | ||
} | ||
|
||
static OpenXRFbSceneExtensionWrapper *get_singleton(); | ||
|
||
OpenXRFbSceneExtensionWrapper(); | ||
~OpenXRFbSceneExtensionWrapper(); | ||
|
||
std::optional<std::string> get_semantic_labels(const XrSpace space); | ||
void get_shapes(const XrSpace space, XrSceneObjectInternal& object); | ||
|
||
protected: | ||
static void _bind_methods(); | ||
|
||
private: | ||
EXT_PROTO_XRRESULT_FUNC3(xrGetSpaceBoundingBox2DFB, | ||
(XrSession), session, | ||
(XrSpace), space, | ||
(XrRect2Df *), boundingBox2DOutput) | ||
|
||
EXT_PROTO_XRRESULT_FUNC3(xrGetSpaceBoundingBox3DFB, | ||
(XrSession), session, | ||
(XrSpace), space, | ||
(XrRect3DfFB *), boundingBox3DOutput) | ||
|
||
EXT_PROTO_XRRESULT_FUNC3(xrGetSpaceSemanticLabelsFB, | ||
(XrSession), session, | ||
(XrSpace), space, | ||
(XrSemanticLabelsFB *), semanticLabelsOutput) | ||
|
||
EXT_PROTO_XRRESULT_FUNC3(xrGetSpaceBoundary2DFB, | ||
(XrSession), session, | ||
(XrSpace), space, | ||
(XrBoundary2DFB *), boundary2DOutput) | ||
|
||
EXT_PROTO_XRRESULT_FUNC3(xrGetSpaceRoomLayoutFB, | ||
(XrSession), session, | ||
(XrSpace), space, | ||
(XrRoomLayoutFB *), roomLayoutOutput) | ||
|
||
bool initialize_fb_scene_extension(const XrInstance instance); | ||
|
||
std::map<godot::String, bool *> request_extensions; | ||
|
||
void cleanup(); | ||
|
||
static OpenXRFbSceneExtensionWrapper *singleton; | ||
|
||
bool fb_scene_ext = false; | ||
}; | ||
|
||
#endif // OPENXR_FB_SCENE_EXTENSION_WRAPPER_H |
165 changes: 165 additions & 0 deletions
165
godotopenxrmeta/src/main/cpp/openxr_fb_scene_extension_wrapper.cpp
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,165 @@ | ||
/**************************************************************************/ | ||
/* openxr_fb_scene_extension_wrapper.cpp */ | ||
/**************************************************************************/ | ||
/* This file is part of: */ | ||
/* GODOT XR */ | ||
/* https://godotengine.org */ | ||
/**************************************************************************/ | ||
/* Copyright (c) 2022-present Godot XR contributors (see CONTRIBUTORS.md) */ | ||
/* */ | ||
/* Original contributed implementation: */ | ||
/* Copyright (c) 2022-2023 MattaKis Consulting Kft. (Migeran) */ | ||
/* */ | ||
/* 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 "include/openxr_fb_scene_extension_wrapper.h" | ||
|
||
#include <godot_cpp/variant/utility_functions.hpp> | ||
#include <godot_cpp/classes/open_xrapi_extension.hpp> | ||
#include <godot_cpp/classes/object.hpp> | ||
|
||
#include "include/openxr_fb_spatial_entity_extension_wrapper.h" | ||
|
||
using namespace godot; | ||
|
||
OpenXRFbSceneExtensionWrapper *OpenXRFbSceneExtensionWrapper::singleton = nullptr; | ||
|
||
OpenXRFbSceneExtensionWrapper *OpenXRFbSceneExtensionWrapper::get_singleton() { | ||
if (singleton == nullptr) { | ||
singleton = memnew(OpenXRFbSceneExtensionWrapper()); | ||
} | ||
return singleton; | ||
} | ||
|
||
OpenXRFbSceneExtensionWrapper::OpenXRFbSceneExtensionWrapper() : | ||
OpenXRExtensionWrapperExtension() { | ||
|
||
ERR_FAIL_COND_MSG(singleton != nullptr, "An OpenXRFbSceneExtensionWrapper singleton already exists."); | ||
|
||
request_extensions[XR_FB_SCENE_EXTENSION_NAME] = &fb_scene_ext; | ||
singleton = this; | ||
} | ||
|
||
OpenXRFbSceneExtensionWrapper::~OpenXRFbSceneExtensionWrapper() { | ||
cleanup(); | ||
} | ||
|
||
void OpenXRFbSceneExtensionWrapper::_bind_methods() { | ||
ClassDB::bind_static_method("OpenXRFbSceneExtensionWrapper", D_METHOD("get_singleton"), &OpenXRFbSceneExtensionWrapper::get_singleton); | ||
ClassDB::bind_method(D_METHOD("is_scene_supported"), &OpenXRFbSceneExtensionWrapper::is_scene_supported); | ||
} | ||
|
||
void OpenXRFbSceneExtensionWrapper::cleanup() { | ||
fb_scene_ext = false; | ||
} | ||
|
||
godot::Dictionary OpenXRFbSceneExtensionWrapper::_get_requested_extensions() { | ||
godot::Dictionary result; | ||
for (auto ext: request_extensions) { | ||
godot::String key = ext.first; | ||
uint64_t value = reinterpret_cast<uint64_t>(ext.second); | ||
result[key] = (godot::Variant)value; | ||
} | ||
return result; | ||
} | ||
|
||
void OpenXRFbSceneExtensionWrapper::_on_instance_created(uint64_t instance) { | ||
if (fb_scene_ext) { | ||
bool result = initialize_fb_scene_extension((XrInstance)instance); | ||
if (!result) { | ||
UtilityFunctions::print("Failed to initialize fb_scene extension"); | ||
fb_scene_ext = false; | ||
} | ||
} | ||
} | ||
|
||
void OpenXRFbSceneExtensionWrapper::_on_instance_destroyed() { | ||
cleanup(); | ||
} | ||
|
||
std::optional<std::string> OpenXRFbSceneExtensionWrapper::get_semantic_labels(const XrSpace space) { | ||
if (!OpenXRFbSpatialEntityExtensionWrapper::get_singleton()->is_component_enabled(space, XR_SPACE_COMPONENT_TYPE_SEMANTIC_LABELS_FB)) { | ||
return std::nullopt; | ||
} | ||
|
||
static const std::string recognizedLabels = | ||
"TABLE,COUCH,FLOOR,CEILING,WALL_FACE,WINDOW_FRAME,DOOR_FRAME,STORAGE,BED,SCREEN,LAMP,PLANT,WALL_ART,INVISIBLE_WALL_FACE,OTHER"; | ||
const XrSemanticLabelsSupportInfoFB semanticLabelsSupportInfo = { | ||
XR_TYPE_SEMANTIC_LABELS_SUPPORT_INFO_FB, | ||
nullptr, | ||
XR_SEMANTIC_LABELS_SUPPORT_MULTIPLE_SEMANTIC_LABELS_BIT_FB | XR_SEMANTIC_LABELS_SUPPORT_ACCEPT_DESK_TO_TABLE_MIGRATION_BIT_FB | | ||
XR_SEMANTIC_LABELS_SUPPORT_ACCEPT_INVISIBLE_WALL_FACE_BIT_FB, | ||
recognizedLabels.c_str()}; | ||
|
||
XrSemanticLabelsFB labels = {XR_TYPE_SEMANTIC_LABELS_FB, &semanticLabelsSupportInfo, 0}; | ||
|
||
// First call. | ||
xrGetSpaceSemanticLabelsFB(SESSION, space, &labels); | ||
// Second call | ||
std::vector<char> labelData(labels.bufferCountOutput); | ||
labels.bufferCapacityInput = labelData.size(); | ||
labels.buffer = labelData.data(); | ||
xrGetSpaceSemanticLabelsFB(SESSION, space, &labels); | ||
|
||
return std::string(labels.buffer, labels.bufferCountOutput); | ||
} | ||
|
||
void OpenXRFbSceneExtensionWrapper::get_shapes(const XrSpace space, XrSceneObjectInternal& object) { | ||
if (OpenXRFbSpatialEntityExtensionWrapper::get_singleton()->is_component_enabled(space, XR_SPACE_COMPONENT_TYPE_BOUNDED_2D_FB)) { | ||
// Grab both the bounding box 2D and the boundary | ||
XrRect2Df boundingBox2D; | ||
if (XR_SUCCEEDED(xrGetSpaceBoundingBox2DFB(SESSION, space, &boundingBox2D))) { | ||
object.boundingBox2D = boundingBox2D; | ||
} | ||
|
||
XrBoundary2DFB boundary2D = {XR_TYPE_BOUNDARY_2D_FB, nullptr, 0}; | ||
if (XR_SUCCEEDED(xrGetSpaceBoundary2DFB(SESSION, space, &boundary2D))) { | ||
std::vector<XrVector2f> vertices(boundary2D.vertexCountOutput); | ||
boundary2D.vertexCapacityInput = vertices.size(); | ||
boundary2D.vertices = vertices.data(); | ||
if (XR_SUCCEEDED(xrGetSpaceBoundary2DFB(SESSION, space, &boundary2D))) { | ||
object.boundary2D = vertices; | ||
} | ||
} | ||
} | ||
|
||
if (OpenXRFbSpatialEntityExtensionWrapper::get_singleton()->is_component_enabled(space, XR_SPACE_COMPONENT_TYPE_BOUNDED_3D_FB)) { | ||
XrRect3DfFB boundingBox3D; | ||
if (XR_SUCCEEDED(xrGetSpaceBoundingBox3DFB(SESSION, space, &boundingBox3D))) { | ||
object.boundingBox3D = boundingBox3D; | ||
} | ||
} | ||
|
||
// TODO: Need to enable the extension for this | ||
// if (is_component_enabled(space, XR_SPACE_COMPONENT_TYPE_TRIANGLE_MESH_META)) { | ||
// WARN_PRINT("Found component with XR_SPACE_COMPONENT_TYPE_TRIANGLE_MESH_META"); | ||
// } | ||
} | ||
|
||
bool OpenXRFbSceneExtensionWrapper::initialize_fb_scene_extension(const XrInstance p_instance) { | ||
GDEXTENSION_INIT_XR_FUNC_V(xrGetSpaceBoundingBox2DFB); | ||
GDEXTENSION_INIT_XR_FUNC_V(xrGetSpaceBoundingBox3DFB); | ||
GDEXTENSION_INIT_XR_FUNC_V(xrGetSpaceSemanticLabelsFB); | ||
GDEXTENSION_INIT_XR_FUNC_V(xrGetSpaceBoundary2DFB); | ||
GDEXTENSION_INIT_XR_FUNC_V(xrGetSpaceRoomLayoutFB); | ||
|
||
return true; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters