Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Enable depth test for quad layers #286

Merged
merged 1 commit into from
Mar 26, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,121 @@
/**************************************************************************/
/* openxr_fb_composition_layer_depth_test_extension_wrapper.cpp */
/**************************************************************************/
/* This file is part of: */
/* GODOT XR */
/* https://godotengine.org */
/**************************************************************************/
/* Copyright (c) 2022-present Godot XR contributors (see CONTRIBUTORS.md) */
/* */
/* 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 "extensions/openxr_fb_composition_layer_depth_test_extension_wrapper.h"

#include <godot_cpp/classes/open_xrapi_extension.hpp>
#include <godot_cpp/variant/utility_functions.hpp>

using namespace godot;

static const char *ENABLE_PROPERTY_NAME = "XR_FB_composition_layer_depth_test/enable";

OpenXRFbCompositionLayerDepthTestExtensionWrapper *OpenXRFbCompositionLayerDepthTestExtensionWrapper::singleton = nullptr;

OpenXRFbCompositionLayerDepthTestExtensionWrapper *OpenXRFbCompositionLayerDepthTestExtensionWrapper::get_singleton() {
if (singleton == nullptr) {
singleton = memnew(OpenXRFbCompositionLayerDepthTestExtensionWrapper());
}
return singleton;
}

OpenXRFbCompositionLayerDepthTestExtensionWrapper::OpenXRFbCompositionLayerDepthTestExtensionWrapper() :
OpenXRExtensionWrapperExtension() {
ERR_FAIL_COND_MSG(singleton != nullptr, "An OpenXRFbCompositionLayerDepthTestExtensionWrapper singleton already exists.");

request_extensions[XR_FB_COMPOSITION_LAYER_DEPTH_TEST_EXTENSION_NAME] = &fb_composition_layer_depth_test_ext;
singleton = this;
}

OpenXRFbCompositionLayerDepthTestExtensionWrapper::~OpenXRFbCompositionLayerDepthTestExtensionWrapper() {
cleanup();
}

void OpenXRFbCompositionLayerDepthTestExtensionWrapper::_bind_methods() {
}

void OpenXRFbCompositionLayerDepthTestExtensionWrapper::cleanup() {
fb_composition_layer_depth_test_ext = false;
}

Dictionary OpenXRFbCompositionLayerDepthTestExtensionWrapper::_get_requested_extensions() {
Dictionary result;
for (auto ext : request_extensions) {
uint64_t value = reinterpret_cast<uint64_t>(ext.value);
result[ext.key] = (Variant)value;
}
return result;
}

uint64_t OpenXRFbCompositionLayerDepthTestExtensionWrapper::_set_viewport_composition_layer_and_get_next_pointer(const void *p_layer, const Dictionary &p_property_values, void *p_next_pointer) {
if (!fb_composition_layer_depth_test_ext || !(bool)p_property_values.get(ENABLE_PROPERTY_NAME, false)) {
return reinterpret_cast<uint64_t>(p_next_pointer);
}

const XrCompositionLayerBaseHeader *layer = reinterpret_cast<const XrCompositionLayerBaseHeader *>(p_layer);

if (!layer_structs.has(layer)) {
layer_structs[layer] = {
XR_TYPE_COMPOSITION_LAYER_DEPTH_TEST_FB, // type
p_next_pointer, // next
true, // depthMask
XR_COMPARE_OP_LESS_FB // compareOp - Less depth = closer to the screen = keep this fragment
};
}

XrCompositionLayerDepthTestFB *depth_test = layer_structs.getptr(layer);
return reinterpret_cast<uint64_t>(depth_test);
}

void OpenXRFbCompositionLayerDepthTestExtensionWrapper::_on_viewport_composition_layer_destroyed(const void *p_layer) {
if (fb_composition_layer_depth_test_ext) {
const XrCompositionLayerBaseHeader *layer = reinterpret_cast<const XrCompositionLayerBaseHeader *>(p_layer);
layer_structs.erase(layer);
}
}

TypedArray<Dictionary> OpenXRFbCompositionLayerDepthTestExtensionWrapper::_get_viewport_composition_layer_extension_properties() {
TypedArray<Dictionary> properties;

{
Dictionary depth_enabled;
depth_enabled["name"] = ENABLE_PROPERTY_NAME;
depth_enabled["type"] = Variant::BOOL;
depth_enabled["hint"] = PROPERTY_HINT_NONE;
properties.push_back(depth_enabled);
}

return properties;
}

Dictionary OpenXRFbCompositionLayerDepthTestExtensionWrapper::_get_viewport_composition_layer_extension_property_defaults() {
Dictionary defaults;
defaults[ENABLE_PROPERTY_NAME] = false;
return defaults;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
/**************************************************************************/
/* openxr_fb_composition_layer_depth_test_extension_wrapper.h */
/**************************************************************************/
/* This file is part of: */
/* GODOT XR */
/* https://godotengine.org */
/**************************************************************************/
/* Copyright (c) 2022-present Godot XR contributors (see CONTRIBUTORS.md) */
/* */
/* 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/openxr.h>

#include <godot_cpp/classes/open_xr_extension_wrapper_extension.hpp>
#include <godot_cpp/templates/hash_map.hpp>

using namespace godot;

// Wrapper for the XR_FB_composition_layer_depth_test extension. This asks the compositor to
// perform a depth test on the submitted layers to provide occlusion among them
class OpenXRFbCompositionLayerDepthTestExtensionWrapper : public OpenXRExtensionWrapperExtension {
GDCLASS(OpenXRFbCompositionLayerDepthTestExtensionWrapper, OpenXRExtensionWrapperExtension);

public:
Dictionary _get_requested_extensions() override;
virtual uint64_t _set_viewport_composition_layer_and_get_next_pointer(const void *p_layer, const Dictionary &p_property_values, void *p_next_pointer) override;
virtual TypedArray<Dictionary> _get_viewport_composition_layer_extension_properties() override;
virtual Dictionary _get_viewport_composition_layer_extension_property_defaults() override;
virtual void _on_viewport_composition_layer_destroyed(const void *p_layer) override;

bool is_composition_layer_depth_test_supported() {
return fb_composition_layer_depth_test_ext;
}

static OpenXRFbCompositionLayerDepthTestExtensionWrapper *get_singleton();

OpenXRFbCompositionLayerDepthTestExtensionWrapper();
~OpenXRFbCompositionLayerDepthTestExtensionWrapper();

protected:
static void _bind_methods();

private:
HashMap<String, bool *> request_extensions;
HashMap<const XrCompositionLayerBaseHeader *, XrCompositionLayerDepthTestFB> layer_structs;

void cleanup();

static OpenXRFbCompositionLayerDepthTestExtensionWrapper *singleton;

bool fb_composition_layer_depth_test_ext = false;
};
5 changes: 5 additions & 0 deletions plugin/src/main/cpp/register_types.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@
#include "extensions/openxr_fb_android_surface_swapchain_create_extension_wrapper.h"
#include "extensions/openxr_fb_body_tracking_extension_wrapper.h"
#include "extensions/openxr_fb_composition_layer_alpha_blend_extension_wrapper.h"
#include "extensions/openxr_fb_composition_layer_depth_test_extension_wrapper.h"
#include "extensions/openxr_fb_composition_layer_image_layout_extension_wrapper.h"
#include "extensions/openxr_fb_composition_layer_secure_content_extension_wrapper.h"
#include "extensions/openxr_fb_composition_layer_settings_extension_wrapper.h"
Expand Down Expand Up @@ -145,6 +146,9 @@ void initialize_plugin_module(ModuleInitializationLevel p_level) {
ClassDB::register_class<OpenXRFbCompositionLayerSecureContentExtensionWrapper>();
OpenXRFbCompositionLayerSecureContentExtensionWrapper::get_singleton()->register_extension_wrapper();

ClassDB::register_class<OpenXRFbCompositionLayerDepthTestExtensionWrapper>();
OpenXRFbCompositionLayerDepthTestExtensionWrapper::get_singleton()->register_extension_wrapper();

ClassDB::register_class<OpenXRFbCompositionLayerAlphaBlendExtensionWrapper>();
OpenXRFbCompositionLayerAlphaBlendExtensionWrapper::get_singleton()->register_extension_wrapper();

Expand Down Expand Up @@ -181,6 +185,7 @@ void initialize_plugin_module(ModuleInitializationLevel p_level) {
Engine::get_singleton()->register_singleton("OpenXRFbSceneExtensionWrapper", OpenXRFbSceneExtensionWrapper::get_singleton());
Engine::get_singleton()->register_singleton("OpenXRFbHandTrackingAimExtensionWrapper", OpenXRFbHandTrackingAimExtensionWrapper::get_singleton());
Engine::get_singleton()->register_singleton("OpenXRFbHandTrackingCapsulesExtensionWrapper", OpenXRFbHandTrackingCapsulesExtensionWrapper::get_singleton());
Engine::get_singleton()->register_singleton("OpenXRFbCompositionLayerDepthTestExtensionWrapper", OpenXRFbCompositionLayerSettingsExtensionWrapper::get_singleton());
Engine::get_singleton()->register_singleton("OpenXRFbCompositionLayerSettingsExtensionWrapper", OpenXRFbCompositionLayerSettingsExtensionWrapper::get_singleton());
Engine::get_singleton()->register_singleton("OpenXRHtcFacialTrackingExtensionWrapper", OpenXRHtcFacialTrackingExtensionWrapper::get_singleton());
Engine::get_singleton()->register_singleton("OpenXRHtcPassthroughExtensionWrapper", OpenXRHtcPassthroughExtensionWrapper::get_singleton());
Expand Down
Loading