|
| 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 | +} |
0 commit comments