Skip to content

Commit e81d634

Browse files
m4gr3dmaunvz
andauthored
Enable depth test for quad layers (#286)
Co-authored-by: Mauricio Narvaez <maunvz@gmail.com>
1 parent 287b8c1 commit e81d634

3 files changed

+198
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,121 @@
1+
/**************************************************************************/
2+
/* openxr_fb_composition_layer_depth_test_extension_wrapper.cpp */
3+
/**************************************************************************/
4+
/* This file is part of: */
5+
/* GODOT XR */
6+
/* https://godotengine.org */
7+
/**************************************************************************/
8+
/* Copyright (c) 2022-present Godot XR contributors (see CONTRIBUTORS.md) */
9+
/* */
10+
/* Permission is hereby granted, free of charge, to any person obtaining */
11+
/* a copy of this software and associated documentation files (the */
12+
/* "Software"), to deal in the Software without restriction, including */
13+
/* without limitation the rights to use, copy, modify, merge, publish, */
14+
/* distribute, sublicense, and/or sell copies of the Software, and to */
15+
/* permit persons to whom the Software is furnished to do so, subject to */
16+
/* the following conditions: */
17+
/* */
18+
/* The above copyright notice and this permission notice shall be */
19+
/* included in all copies or substantial portions of the Software. */
20+
/* */
21+
/* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
22+
/* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
23+
/* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. */
24+
/* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
25+
/* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
26+
/* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
27+
/* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
28+
/**************************************************************************/
29+
30+
#include "extensions/openxr_fb_composition_layer_depth_test_extension_wrapper.h"
31+
32+
#include <godot_cpp/classes/open_xrapi_extension.hpp>
33+
#include <godot_cpp/variant/utility_functions.hpp>
34+
35+
using namespace godot;
36+
37+
static const char *ENABLE_PROPERTY_NAME = "XR_FB_composition_layer_depth_test/enable";
38+
39+
OpenXRFbCompositionLayerDepthTestExtensionWrapper *OpenXRFbCompositionLayerDepthTestExtensionWrapper::singleton = nullptr;
40+
41+
OpenXRFbCompositionLayerDepthTestExtensionWrapper *OpenXRFbCompositionLayerDepthTestExtensionWrapper::get_singleton() {
42+
if (singleton == nullptr) {
43+
singleton = memnew(OpenXRFbCompositionLayerDepthTestExtensionWrapper());
44+
}
45+
return singleton;
46+
}
47+
48+
OpenXRFbCompositionLayerDepthTestExtensionWrapper::OpenXRFbCompositionLayerDepthTestExtensionWrapper() :
49+
OpenXRExtensionWrapperExtension() {
50+
ERR_FAIL_COND_MSG(singleton != nullptr, "An OpenXRFbCompositionLayerDepthTestExtensionWrapper singleton already exists.");
51+
52+
request_extensions[XR_FB_COMPOSITION_LAYER_DEPTH_TEST_EXTENSION_NAME] = &fb_composition_layer_depth_test_ext;
53+
singleton = this;
54+
}
55+
56+
OpenXRFbCompositionLayerDepthTestExtensionWrapper::~OpenXRFbCompositionLayerDepthTestExtensionWrapper() {
57+
cleanup();
58+
}
59+
60+
void OpenXRFbCompositionLayerDepthTestExtensionWrapper::_bind_methods() {
61+
}
62+
63+
void OpenXRFbCompositionLayerDepthTestExtensionWrapper::cleanup() {
64+
fb_composition_layer_depth_test_ext = false;
65+
}
66+
67+
Dictionary OpenXRFbCompositionLayerDepthTestExtensionWrapper::_get_requested_extensions() {
68+
Dictionary result;
69+
for (auto ext : request_extensions) {
70+
uint64_t value = reinterpret_cast<uint64_t>(ext.value);
71+
result[ext.key] = (Variant)value;
72+
}
73+
return result;
74+
}
75+
76+
uint64_t OpenXRFbCompositionLayerDepthTestExtensionWrapper::_set_viewport_composition_layer_and_get_next_pointer(const void *p_layer, const Dictionary &p_property_values, void *p_next_pointer) {
77+
if (!fb_composition_layer_depth_test_ext || !(bool)p_property_values.get(ENABLE_PROPERTY_NAME, false)) {
78+
return reinterpret_cast<uint64_t>(p_next_pointer);
79+
}
80+
81+
const XrCompositionLayerBaseHeader *layer = reinterpret_cast<const XrCompositionLayerBaseHeader *>(p_layer);
82+
83+
if (!layer_structs.has(layer)) {
84+
layer_structs[layer] = {
85+
XR_TYPE_COMPOSITION_LAYER_DEPTH_TEST_FB, // type
86+
p_next_pointer, // next
87+
true, // depthMask
88+
XR_COMPARE_OP_LESS_FB // compareOp - Less depth = closer to the screen = keep this fragment
89+
};
90+
}
91+
92+
XrCompositionLayerDepthTestFB *depth_test = layer_structs.getptr(layer);
93+
return reinterpret_cast<uint64_t>(depth_test);
94+
}
95+
96+
void OpenXRFbCompositionLayerDepthTestExtensionWrapper::_on_viewport_composition_layer_destroyed(const void *p_layer) {
97+
if (fb_composition_layer_depth_test_ext) {
98+
const XrCompositionLayerBaseHeader *layer = reinterpret_cast<const XrCompositionLayerBaseHeader *>(p_layer);
99+
layer_structs.erase(layer);
100+
}
101+
}
102+
103+
TypedArray<Dictionary> OpenXRFbCompositionLayerDepthTestExtensionWrapper::_get_viewport_composition_layer_extension_properties() {
104+
TypedArray<Dictionary> properties;
105+
106+
{
107+
Dictionary depth_enabled;
108+
depth_enabled["name"] = ENABLE_PROPERTY_NAME;
109+
depth_enabled["type"] = Variant::BOOL;
110+
depth_enabled["hint"] = PROPERTY_HINT_NONE;
111+
properties.push_back(depth_enabled);
112+
}
113+
114+
return properties;
115+
}
116+
117+
Dictionary OpenXRFbCompositionLayerDepthTestExtensionWrapper::_get_viewport_composition_layer_extension_property_defaults() {
118+
Dictionary defaults;
119+
defaults[ENABLE_PROPERTY_NAME] = false;
120+
return defaults;
121+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
/**************************************************************************/
2+
/* openxr_fb_composition_layer_depth_test_extension_wrapper.h */
3+
/**************************************************************************/
4+
/* This file is part of: */
5+
/* GODOT XR */
6+
/* https://godotengine.org */
7+
/**************************************************************************/
8+
/* Copyright (c) 2022-present Godot XR contributors (see CONTRIBUTORS.md) */
9+
/* */
10+
/* Permission is hereby granted, free of charge, to any person obtaining */
11+
/* a copy of this software and associated documentation files (the */
12+
/* "Software"), to deal in the Software without restriction, including */
13+
/* without limitation the rights to use, copy, modify, merge, publish, */
14+
/* distribute, sublicense, and/or sell copies of the Software, and to */
15+
/* permit persons to whom the Software is furnished to do so, subject to */
16+
/* the following conditions: */
17+
/* */
18+
/* The above copyright notice and this permission notice shall be */
19+
/* included in all copies or substantial portions of the Software. */
20+
/* */
21+
/* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
22+
/* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
23+
/* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. */
24+
/* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
25+
/* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
26+
/* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
27+
/* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
28+
/**************************************************************************/
29+
30+
#pragma once
31+
32+
#include <openxr/openxr.h>
33+
34+
#include <godot_cpp/classes/open_xr_extension_wrapper_extension.hpp>
35+
#include <godot_cpp/templates/hash_map.hpp>
36+
37+
using namespace godot;
38+
39+
// Wrapper for the XR_FB_composition_layer_depth_test extension. This asks the compositor to
40+
// perform a depth test on the submitted layers to provide occlusion among them
41+
class OpenXRFbCompositionLayerDepthTestExtensionWrapper : public OpenXRExtensionWrapperExtension {
42+
GDCLASS(OpenXRFbCompositionLayerDepthTestExtensionWrapper, OpenXRExtensionWrapperExtension);
43+
44+
public:
45+
Dictionary _get_requested_extensions() override;
46+
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;
47+
virtual TypedArray<Dictionary> _get_viewport_composition_layer_extension_properties() override;
48+
virtual Dictionary _get_viewport_composition_layer_extension_property_defaults() override;
49+
virtual void _on_viewport_composition_layer_destroyed(const void *p_layer) override;
50+
51+
bool is_composition_layer_depth_test_supported() {
52+
return fb_composition_layer_depth_test_ext;
53+
}
54+
55+
static OpenXRFbCompositionLayerDepthTestExtensionWrapper *get_singleton();
56+
57+
OpenXRFbCompositionLayerDepthTestExtensionWrapper();
58+
~OpenXRFbCompositionLayerDepthTestExtensionWrapper();
59+
60+
protected:
61+
static void _bind_methods();
62+
63+
private:
64+
HashMap<String, bool *> request_extensions;
65+
HashMap<const XrCompositionLayerBaseHeader *, XrCompositionLayerDepthTestFB> layer_structs;
66+
67+
void cleanup();
68+
69+
static OpenXRFbCompositionLayerDepthTestExtensionWrapper *singleton;
70+
71+
bool fb_composition_layer_depth_test_ext = false;
72+
};

plugin/src/main/cpp/register_types.cpp

+5
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,7 @@
4848
#include "extensions/openxr_fb_android_surface_swapchain_create_extension_wrapper.h"
4949
#include "extensions/openxr_fb_body_tracking_extension_wrapper.h"
5050
#include "extensions/openxr_fb_composition_layer_alpha_blend_extension_wrapper.h"
51+
#include "extensions/openxr_fb_composition_layer_depth_test_extension_wrapper.h"
5152
#include "extensions/openxr_fb_composition_layer_image_layout_extension_wrapper.h"
5253
#include "extensions/openxr_fb_composition_layer_secure_content_extension_wrapper.h"
5354
#include "extensions/openxr_fb_composition_layer_settings_extension_wrapper.h"
@@ -145,6 +146,9 @@ void initialize_plugin_module(ModuleInitializationLevel p_level) {
145146
ClassDB::register_class<OpenXRFbCompositionLayerSecureContentExtensionWrapper>();
146147
OpenXRFbCompositionLayerSecureContentExtensionWrapper::get_singleton()->register_extension_wrapper();
147148

149+
ClassDB::register_class<OpenXRFbCompositionLayerDepthTestExtensionWrapper>();
150+
OpenXRFbCompositionLayerDepthTestExtensionWrapper::get_singleton()->register_extension_wrapper();
151+
148152
ClassDB::register_class<OpenXRFbCompositionLayerAlphaBlendExtensionWrapper>();
149153
OpenXRFbCompositionLayerAlphaBlendExtensionWrapper::get_singleton()->register_extension_wrapper();
150154

@@ -181,6 +185,7 @@ void initialize_plugin_module(ModuleInitializationLevel p_level) {
181185
Engine::get_singleton()->register_singleton("OpenXRFbSceneExtensionWrapper", OpenXRFbSceneExtensionWrapper::get_singleton());
182186
Engine::get_singleton()->register_singleton("OpenXRFbHandTrackingAimExtensionWrapper", OpenXRFbHandTrackingAimExtensionWrapper::get_singleton());
183187
Engine::get_singleton()->register_singleton("OpenXRFbHandTrackingCapsulesExtensionWrapper", OpenXRFbHandTrackingCapsulesExtensionWrapper::get_singleton());
188+
Engine::get_singleton()->register_singleton("OpenXRFbCompositionLayerDepthTestExtensionWrapper", OpenXRFbCompositionLayerSettingsExtensionWrapper::get_singleton());
184189
Engine::get_singleton()->register_singleton("OpenXRFbCompositionLayerSettingsExtensionWrapper", OpenXRFbCompositionLayerSettingsExtensionWrapper::get_singleton());
185190
Engine::get_singleton()->register_singleton("OpenXRHtcFacialTrackingExtensionWrapper", OpenXRHtcFacialTrackingExtensionWrapper::get_singleton());
186191
Engine::get_singleton()->register_singleton("OpenXRHtcPassthroughExtensionWrapper", OpenXRHtcPassthroughExtensionWrapper::get_singleton());

0 commit comments

Comments
 (0)