summaryrefslogtreecommitdiff
path: root/modules/openxr/extensions/openxr_fb_passthrough_extension_wrapper.cpp
blob: 259b1236a3c7395cb68a137e33ac7e2d3512c769 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
/*************************************************************************/
/*  openxr_fb_passthrough_extension_wrapper.cpp                          */
/*************************************************************************/
/*                       This file is part of:                           */
/*                           GODOT ENGINE                                */
/*                      https://godotengine.org                          */
/*************************************************************************/
/* Copyright (c) 2007-2022 Juan Linietsky, Ariel Manzur.                 */
/* Copyright (c) 2014-2022 Godot Engine contributors (cf. AUTHORS.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 "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(OpenXRAPI *p_openxr_api) :
		OpenXRExtensionWrapper(p_openxr_api) {
	request_extensions[XR_FB_PASSTHROUGH_EXTENSION_NAME] = &fb_passthrough_ext;
	request_extensions[XR_FB_TRIANGLE_MESH_EXTENSION_NAME] = &fb_triangle_mesh_ext;
	singleton = this;
}

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

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;
	}

	auto *scene_tree = Object::cast_to<SceneTree>(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) {
		openxr_api->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::is_composition_passthrough_layer_ready() {
	return fb_passthrough_ext && passthrough_handle != XR_NULL_HANDLE && composition_passthrough_layer.layerHandle != 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
	result = xrCreatePassthroughLayerFB(openxr_api->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.");
	}

	composition_passthrough_layer.layerHandle = passthrough_layer;

	return true;
}

void OpenXRFbPassthroughExtensionWrapper::on_session_created(const XrSession session) {
	if (fb_passthrough_ext) {
		// Create the passthrough feature and start it.
		XrResult result = xrCreatePassthroughFB(openxr_api->get_session(), &passthrough_create_info, &passthrough_handle);
		if (!openxr_api->xr_result(result, "Failed to create passthrough")) {
			passthrough_handle = XR_NULL_HANDLE;
			return;
		}
	}
}

XrCompositionLayerBaseHeader *OpenXRFbPassthroughExtensionWrapper::get_composition_layer() {
	if (is_composition_passthrough_layer_ready()) {
		return (XrCompositionLayerBaseHeader *)&composition_passthrough_layer;
	} else {
		return nullptr;
	}
}

void OpenXRFbPassthroughExtensionWrapper::stop_passthrough() {
	if (!fb_passthrough_ext) {
		return;
	}

	composition_passthrough_layer.layerHandle = XR_NULL_HANDLE;

	XrResult result;
	if (passthrough_layer != XR_NULL_HANDLE) {
		// Destroy the layer
		result = xrDestroyPassthroughLayerFB(passthrough_layer);
		openxr_api->xr_result(result, "Unable to destroy passthrough layer");
		passthrough_layer = XR_NULL_HANDLE;
	}

	if (passthrough_handle != XR_NULL_HANDLE) {
		result = xrPassthroughPauseFB(passthrough_handle);
		openxr_api->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);
			openxr_api->xr_result(result, "Unable to destroy passthrough feature");
			passthrough_handle = XR_NULL_HANDLE;
		}
	}
}

void OpenXRFbPassthroughExtensionWrapper::on_instance_destroyed() {
	if (fb_passthrough_ext) {
		openxr_api->unregister_composition_layer_provider(this);
	}
	cleanup();
}

bool OpenXRFbPassthroughExtensionWrapper::initialize_fb_passthrough_extension(const XrInstance p_instance) {
	ERR_FAIL_NULL_V(openxr_api, 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(openxr_api, 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;
}