Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
godotengine
GitHub Repository: godotengine/godot
Path: blob/master/modules/openxr/openxr_api_extension.cpp
20951 views
1
/**************************************************************************/
2
/* openxr_api_extension.cpp */
3
/**************************************************************************/
4
/* This file is part of: */
5
/* GODOT ENGINE */
6
/* https://godotengine.org */
7
/**************************************************************************/
8
/* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */
9
/* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */
10
/* */
11
/* Permission is hereby granted, free of charge, to any person obtaining */
12
/* a copy of this software and associated documentation files (the */
13
/* "Software"), to deal in the Software without restriction, including */
14
/* without limitation the rights to use, copy, modify, merge, publish, */
15
/* distribute, sublicense, and/or sell copies of the Software, and to */
16
/* permit persons to whom the Software is furnished to do so, subject to */
17
/* the following conditions: */
18
/* */
19
/* The above copyright notice and this permission notice shall be */
20
/* included in all copies or substantial portions of the Software. */
21
/* */
22
/* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
23
/* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
24
/* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. */
25
/* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
26
/* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
27
/* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
28
/* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
29
/**************************************************************************/
30
31
#include "openxr_api_extension.h"
32
33
#include "extensions/openxr_extension_wrapper.h"
34
#include "openxr_api_extension.compat.inc"
35
36
void OpenXRAPIExtension::_bind_methods() {
37
ClassDB::bind_method(D_METHOD("get_openxr_version"), &OpenXRAPIExtension::get_openxr_version);
38
ClassDB::bind_method(D_METHOD("get_instance"), &OpenXRAPIExtension::get_instance);
39
ClassDB::bind_method(D_METHOD("get_system_id"), &OpenXRAPIExtension::get_system_id);
40
ClassDB::bind_method(D_METHOD("get_session"), &OpenXRAPIExtension::get_session);
41
42
ClassDB::bind_method(D_METHOD("transform_from_pose", "pose"), &OpenXRAPIExtension::transform_from_pose);
43
ClassDB::bind_method(D_METHOD("xr_result", "result", "format", "args"), &OpenXRAPIExtension::xr_result);
44
ClassDB::bind_static_method("OpenXRAPIExtension", D_METHOD("openxr_is_enabled", "check_run_in_editor"), &OpenXRAPIExtension::openxr_is_enabled);
45
ClassDB::bind_method(D_METHOD("get_instance_proc_addr", "name"), &OpenXRAPIExtension::get_instance_proc_addr);
46
ClassDB::bind_method(D_METHOD("get_error_string", "result"), &OpenXRAPIExtension::get_error_string);
47
ClassDB::bind_method(D_METHOD("get_swapchain_format_name", "swapchain_format"), &OpenXRAPIExtension::get_swapchain_format_name);
48
ClassDB::bind_method(D_METHOD("set_object_name", "object_type", "object_handle", "object_name"), &OpenXRAPIExtension::set_object_name);
49
ClassDB::bind_method(D_METHOD("begin_debug_label_region", "label_name"), &OpenXRAPIExtension::begin_debug_label_region);
50
ClassDB::bind_method(D_METHOD("end_debug_label_region"), &OpenXRAPIExtension::end_debug_label_region);
51
ClassDB::bind_method(D_METHOD("insert_debug_label", "label_name"), &OpenXRAPIExtension::insert_debug_label);
52
53
ClassDB::bind_method(D_METHOD("is_initialized"), &OpenXRAPIExtension::is_initialized);
54
ClassDB::bind_method(D_METHOD("is_running"), &OpenXRAPIExtension::is_running);
55
56
ClassDB::bind_method(D_METHOD("set_custom_play_space", "space"), &OpenXRAPIExtension::set_custom_play_space);
57
ClassDB::bind_method(D_METHOD("get_play_space"), &OpenXRAPIExtension::get_play_space);
58
ClassDB::bind_method(D_METHOD("get_predicted_display_time"), &OpenXRAPIExtension::get_predicted_display_time);
59
ClassDB::bind_method(D_METHOD("get_next_frame_time"), &OpenXRAPIExtension::get_next_frame_time);
60
ClassDB::bind_method(D_METHOD("can_render"), &OpenXRAPIExtension::can_render);
61
62
ClassDB::bind_method(D_METHOD("find_action", "name", "action_set"), &OpenXRAPIExtension::find_action);
63
ClassDB::bind_method(D_METHOD("action_get_handle", "action"), &OpenXRAPIExtension::action_get_handle);
64
65
ClassDB::bind_method(D_METHOD("get_hand_tracker", "hand_index"), &OpenXRAPIExtension::get_hand_tracker);
66
67
ClassDB::bind_method(D_METHOD("register_composition_layer_provider", "extension"), &OpenXRAPIExtension::register_composition_layer_provider);
68
ClassDB::bind_method(D_METHOD("unregister_composition_layer_provider", "extension"), &OpenXRAPIExtension::unregister_composition_layer_provider);
69
70
ClassDB::bind_method(D_METHOD("register_projection_views_extension", "extension"), &OpenXRAPIExtension::register_projection_views_extension);
71
ClassDB::bind_method(D_METHOD("unregister_projection_views_extension", "extension"), &OpenXRAPIExtension::unregister_projection_views_extension);
72
73
ClassDB::bind_method(D_METHOD("register_frame_info_extension", "extension"), &OpenXRAPIExtension::register_frame_info_extension);
74
ClassDB::bind_method(D_METHOD("unregister_frame_info_extension", "extension"), &OpenXRAPIExtension::unregister_frame_info_extension);
75
76
ClassDB::bind_method(D_METHOD("get_render_state_z_near"), &OpenXRAPIExtension::get_render_state_z_near);
77
ClassDB::bind_method(D_METHOD("get_render_state_z_far"), &OpenXRAPIExtension::get_render_state_z_far);
78
79
ClassDB::bind_method(D_METHOD("set_velocity_texture", "render_target"), &OpenXRAPIExtension::set_velocity_texture);
80
ClassDB::bind_method(D_METHOD("set_velocity_depth_texture", "render_target"), &OpenXRAPIExtension::set_velocity_depth_texture);
81
ClassDB::bind_method(D_METHOD("set_velocity_target_size", "target_size"), &OpenXRAPIExtension::set_velocity_target_size);
82
83
ClassDB::bind_method(D_METHOD("get_supported_swapchain_formats"), &OpenXRAPIExtension::get_supported_swapchain_formats);
84
85
ClassDB::bind_method(D_METHOD("openxr_swapchain_create", "create_flags", "usage_flags", "swapchain_format", "width", "height", "sample_count", "array_size"), &OpenXRAPIExtension::openxr_swapchain_create);
86
ClassDB::bind_method(D_METHOD("openxr_swapchain_free", "swapchain"), &OpenXRAPIExtension::openxr_swapchain_free);
87
ClassDB::bind_method(D_METHOD("openxr_swapchain_get_swapchain", "swapchain"), &OpenXRAPIExtension::openxr_swapchain_get_swapchain);
88
ClassDB::bind_method(D_METHOD("openxr_swapchain_acquire", "swapchain"), &OpenXRAPIExtension::openxr_swapchain_acquire);
89
ClassDB::bind_method(D_METHOD("openxr_swapchain_get_image", "swapchain"), &OpenXRAPIExtension::openxr_swapchain_get_image);
90
ClassDB::bind_method(D_METHOD("openxr_swapchain_release", "swapchain"), &OpenXRAPIExtension::openxr_swapchain_release);
91
92
ClassDB::bind_method(D_METHOD("get_projection_layer"), &OpenXRAPIExtension::get_projection_layer);
93
94
ClassDB::bind_method(D_METHOD("set_render_region", "render_region"), &OpenXRAPIExtension::set_render_region);
95
96
ClassDB::bind_method(D_METHOD("set_emulate_environment_blend_mode_alpha_blend", "enabled"), &OpenXRAPIExtension::set_emulate_environment_blend_mode_alpha_blend);
97
ClassDB::bind_method(D_METHOD("is_environment_blend_mode_alpha_supported"), &OpenXRAPIExtension::is_environment_blend_mode_alpha_blend_supported);
98
99
ClassDB::bind_method(D_METHOD("update_main_swapchain_size"), &OpenXRAPIExtension::update_main_swapchain_size);
100
101
BIND_ENUM_CONSTANT(OPENXR_ALPHA_BLEND_MODE_SUPPORT_NONE);
102
BIND_ENUM_CONSTANT(OPENXR_ALPHA_BLEND_MODE_SUPPORT_REAL);
103
BIND_ENUM_CONSTANT(OPENXR_ALPHA_BLEND_MODE_SUPPORT_EMULATING);
104
}
105
106
uint64_t OpenXRAPIExtension::get_openxr_version() {
107
ERR_FAIL_NULL_V(OpenXRAPI::get_singleton(), 0);
108
return (uint64_t)OpenXRAPI::get_singleton()->get_openxr_version();
109
}
110
111
uint64_t OpenXRAPIExtension::get_instance() {
112
ERR_FAIL_NULL_V(OpenXRAPI::get_singleton(), 0);
113
return (uint64_t)OpenXRAPI::get_singleton()->get_instance();
114
}
115
116
uint64_t OpenXRAPIExtension::get_system_id() {
117
ERR_FAIL_NULL_V(OpenXRAPI::get_singleton(), 0);
118
return (uint64_t)OpenXRAPI::get_singleton()->get_system_id();
119
}
120
121
uint64_t OpenXRAPIExtension::get_session() {
122
ERR_FAIL_NULL_V(OpenXRAPI::get_singleton(), 0);
123
return (uint64_t)OpenXRAPI::get_singleton()->get_session();
124
}
125
126
Transform3D OpenXRAPIExtension::transform_from_pose(GDExtensionConstPtr<const void> p_pose) {
127
ERR_FAIL_NULL_V(OpenXRAPI::get_singleton(), Transform3D());
128
return OpenXRAPI::get_singleton()->transform_from_pose(*(XrPosef *)p_pose.data);
129
}
130
131
bool OpenXRAPIExtension::xr_result(uint64_t p_result, const String &p_format, const Array &p_args) {
132
ERR_FAIL_NULL_V(OpenXRAPI::get_singleton(), false);
133
return OpenXRAPI::get_singleton()->xr_result((XrResult)p_result, p_format.utf8().get_data(), p_args);
134
}
135
136
bool OpenXRAPIExtension::openxr_is_enabled(bool p_check_run_in_editor) {
137
ERR_FAIL_NULL_V(OpenXRAPI::get_singleton(), false);
138
return OpenXRAPI::openxr_is_enabled(p_check_run_in_editor);
139
}
140
141
uint64_t OpenXRAPIExtension::get_instance_proc_addr(const String &p_name) {
142
ERR_FAIL_NULL_V(OpenXRAPI::get_singleton(), 0);
143
CharString str = p_name.utf8();
144
PFN_xrVoidFunction addr = nullptr;
145
XrResult result = OpenXRAPI::get_singleton()->get_instance_proc_addr(str.get_data(), &addr);
146
if (result != XR_SUCCESS) {
147
return 0;
148
}
149
return reinterpret_cast<uint64_t>(addr);
150
}
151
152
String OpenXRAPIExtension::get_error_string(uint64_t result) {
153
ERR_FAIL_NULL_V(OpenXRAPI::get_singleton(), String());
154
return OpenXRAPI::get_singleton()->get_error_string((XrResult)result);
155
}
156
157
String OpenXRAPIExtension::get_swapchain_format_name(int64_t p_swapchain_format) {
158
ERR_FAIL_NULL_V(OpenXRAPI::get_singleton(), String());
159
return OpenXRAPI::get_singleton()->get_swapchain_format_name(p_swapchain_format);
160
}
161
162
void OpenXRAPIExtension::set_object_name(int64_t p_object_type, uint64_t p_object_handle, const String &p_object_name) {
163
ERR_FAIL_NULL(OpenXRAPI::get_singleton());
164
165
OpenXRAPI::get_singleton()->set_object_name(XrObjectType(p_object_type), p_object_handle, p_object_name);
166
}
167
168
void OpenXRAPIExtension::begin_debug_label_region(const String &p_label_name) {
169
ERR_FAIL_NULL(OpenXRAPI::get_singleton());
170
171
OpenXRAPI::get_singleton()->begin_debug_label_region(p_label_name);
172
}
173
174
void OpenXRAPIExtension::end_debug_label_region() {
175
ERR_FAIL_NULL(OpenXRAPI::get_singleton());
176
177
OpenXRAPI::get_singleton()->end_debug_label_region();
178
}
179
180
void OpenXRAPIExtension::insert_debug_label(const String &p_label_name) {
181
ERR_FAIL_NULL(OpenXRAPI::get_singleton());
182
183
OpenXRAPI::get_singleton()->insert_debug_label(p_label_name);
184
}
185
186
bool OpenXRAPIExtension::is_initialized() {
187
ERR_FAIL_NULL_V(OpenXRAPI::get_singleton(), false);
188
return OpenXRAPI::get_singleton()->is_initialized();
189
}
190
191
bool OpenXRAPIExtension::is_running() {
192
ERR_FAIL_NULL_V(OpenXRAPI::get_singleton(), false);
193
return OpenXRAPI::get_singleton()->is_running();
194
}
195
196
void OpenXRAPIExtension::set_custom_play_space(GDExtensionConstPtr<const void> p_custom_space) {
197
ERR_FAIL_NULL(OpenXRAPI::get_singleton());
198
OpenXRAPI::get_singleton()->set_custom_play_space(*(XrSpace *)p_custom_space.data);
199
}
200
201
uint64_t OpenXRAPIExtension::get_play_space() {
202
ERR_FAIL_NULL_V(OpenXRAPI::get_singleton(), 0);
203
return (uint64_t)OpenXRAPI::get_singleton()->get_play_space();
204
}
205
206
int64_t OpenXRAPIExtension::get_predicted_display_time() {
207
ERR_FAIL_NULL_V(OpenXRAPI::get_singleton(), 0);
208
return (XrTime)OpenXRAPI::get_singleton()->get_predicted_display_time();
209
}
210
211
int64_t OpenXRAPIExtension::get_next_frame_time() {
212
ERR_FAIL_NULL_V(OpenXRAPI::get_singleton(), 0);
213
214
// In the past we needed to look a frame ahead, may be calling this unintentionally so lets warn the dev.
215
WARN_PRINT_ONCE("OpenXR: Next frame timing called, verify this is intended.");
216
217
return (XrTime)OpenXRAPI::get_singleton()->get_next_frame_time();
218
}
219
220
bool OpenXRAPIExtension::can_render() {
221
ERR_FAIL_NULL_V(OpenXRAPI::get_singleton(), false);
222
return OpenXRAPI::get_singleton()->can_render();
223
}
224
225
RID OpenXRAPIExtension::find_action(const String &p_name, const RID &p_action_set) {
226
ERR_FAIL_NULL_V(OpenXRAPI::get_singleton(), RID());
227
return OpenXRAPI::get_singleton()->find_action(p_name, p_action_set);
228
}
229
230
uint64_t OpenXRAPIExtension::action_get_handle(RID p_action) {
231
ERR_FAIL_NULL_V(OpenXRAPI::get_singleton(), 0);
232
XrAction action_handle = OpenXRAPI::get_singleton()->action_get_handle(p_action);
233
return (uint64_t)action_handle;
234
}
235
236
uint64_t OpenXRAPIExtension::get_hand_tracker(int p_hand_index) {
237
ERR_FAIL_NULL_V(OpenXRAPI::get_singleton(), 0);
238
return (uint64_t)OpenXRAPI::get_singleton()->get_hand_tracker(p_hand_index);
239
}
240
241
void OpenXRAPIExtension::register_composition_layer_provider(OpenXRExtensionWrapper *p_extension) {
242
ERR_FAIL_NULL(OpenXRAPI::get_singleton());
243
OpenXRAPI::get_singleton()->register_composition_layer_provider(p_extension);
244
}
245
246
void OpenXRAPIExtension::unregister_composition_layer_provider(OpenXRExtensionWrapper *p_extension) {
247
ERR_FAIL_NULL(OpenXRAPI::get_singleton());
248
OpenXRAPI::get_singleton()->unregister_composition_layer_provider(p_extension);
249
}
250
251
void OpenXRAPIExtension::register_projection_views_extension(OpenXRExtensionWrapper *p_extension) {
252
ERR_FAIL_NULL(OpenXRAPI::get_singleton());
253
OpenXRAPI::get_singleton()->register_projection_views_extension(p_extension);
254
}
255
256
void OpenXRAPIExtension::unregister_projection_views_extension(OpenXRExtensionWrapper *p_extension) {
257
ERR_FAIL_NULL(OpenXRAPI::get_singleton());
258
OpenXRAPI::get_singleton()->unregister_projection_views_extension(p_extension);
259
}
260
261
void OpenXRAPIExtension::register_frame_info_extension(OpenXRExtensionWrapper *p_extension) {
262
ERR_FAIL_NULL(OpenXRAPI::get_singleton());
263
OpenXRAPI::get_singleton()->register_frame_info_extension(p_extension);
264
}
265
266
void OpenXRAPIExtension::unregister_frame_info_extension(OpenXRExtensionWrapper *p_extension) {
267
ERR_FAIL_NULL(OpenXRAPI::get_singleton());
268
OpenXRAPI::get_singleton()->unregister_frame_info_extension(p_extension);
269
}
270
271
double OpenXRAPIExtension::get_render_state_z_near() {
272
ERR_NOT_ON_RENDER_THREAD_V(0.0);
273
ERR_FAIL_NULL_V(OpenXRAPI::get_singleton(), 0.0);
274
return OpenXRAPI::get_singleton()->get_render_state_z_near();
275
}
276
277
double OpenXRAPIExtension::get_render_state_z_far() {
278
ERR_NOT_ON_RENDER_THREAD_V(0.0);
279
ERR_FAIL_NULL_V(OpenXRAPI::get_singleton(), 0.0);
280
return OpenXRAPI::get_singleton()->get_render_state_z_far();
281
}
282
283
void OpenXRAPIExtension::set_velocity_texture(RID p_render_target) {
284
ERR_FAIL_NULL(OpenXRAPI::get_singleton());
285
OpenXRAPI::get_singleton()->set_velocity_texture(p_render_target);
286
}
287
288
void OpenXRAPIExtension::set_velocity_depth_texture(RID p_render_target) {
289
ERR_FAIL_NULL(OpenXRAPI::get_singleton());
290
OpenXRAPI::get_singleton()->set_velocity_depth_texture(p_render_target);
291
}
292
293
void OpenXRAPIExtension::set_velocity_target_size(const Size2i &p_target_size) {
294
ERR_FAIL_NULL(OpenXRAPI::get_singleton());
295
OpenXRAPI::get_singleton()->set_velocity_target_size(p_target_size);
296
}
297
298
PackedInt64Array OpenXRAPIExtension::get_supported_swapchain_formats() {
299
ERR_FAIL_NULL_V(OpenXRAPI::get_singleton(), PackedInt64Array());
300
return OpenXRAPI::get_singleton()->get_supported_swapchain_formats();
301
}
302
303
uint64_t OpenXRAPIExtension::openxr_swapchain_create(XrSwapchainCreateFlags p_create_flags, XrSwapchainUsageFlags p_usage_flags, int64_t p_swapchain_format, uint32_t p_width, uint32_t p_height, uint32_t p_sample_count, uint32_t p_array_size) {
304
ERR_FAIL_NULL_V(OpenXRAPI::get_singleton(), 0);
305
306
OpenXRAPI::OpenXRSwapChainInfo *new_swapchain_info = memnew(OpenXRAPI::OpenXRSwapChainInfo);
307
new_swapchain_info->create(p_create_flags, p_usage_flags, p_swapchain_format, p_width, p_height, p_sample_count, p_array_size);
308
return reinterpret_cast<uint64_t>(new_swapchain_info);
309
}
310
311
void OpenXRAPIExtension::openxr_swapchain_free(uint64_t p_swapchain_info) {
312
ERR_FAIL_NULL(OpenXRAPI::get_singleton());
313
314
OpenXRAPI::OpenXRSwapChainInfo *swapchain_info = reinterpret_cast<OpenXRAPI::OpenXRSwapChainInfo *>(p_swapchain_info);
315
swapchain_info->free();
316
memfree(swapchain_info);
317
}
318
319
uint64_t OpenXRAPIExtension::openxr_swapchain_get_swapchain(uint64_t p_swapchain_info) {
320
ERR_FAIL_NULL_V(OpenXRAPI::get_singleton(), 0);
321
322
OpenXRAPI::OpenXRSwapChainInfo *swapchain_info = reinterpret_cast<OpenXRAPI::OpenXRSwapChainInfo *>(p_swapchain_info);
323
XrSwapchain swapchain = swapchain_info->get_swapchain();
324
return (uint64_t)swapchain;
325
}
326
327
void OpenXRAPIExtension::openxr_swapchain_acquire(uint64_t p_swapchain_info) {
328
ERR_FAIL_NULL(OpenXRAPI::get_singleton());
329
330
OpenXRAPI::OpenXRSwapChainInfo *swapchain_info = reinterpret_cast<OpenXRAPI::OpenXRSwapChainInfo *>(p_swapchain_info);
331
bool should_render = true; // Can ignore should_render.
332
swapchain_info->acquire(should_render);
333
}
334
335
RID OpenXRAPIExtension::openxr_swapchain_get_image(uint64_t p_swapchain_info) {
336
ERR_FAIL_NULL_V(OpenXRAPI::get_singleton(), RID());
337
338
OpenXRAPI::OpenXRSwapChainInfo *swapchain_info = reinterpret_cast<OpenXRAPI::OpenXRSwapChainInfo *>(p_swapchain_info);
339
return swapchain_info->get_image();
340
}
341
342
void OpenXRAPIExtension::openxr_swapchain_release(uint64_t p_swapchain_info) {
343
ERR_FAIL_NULL(OpenXRAPI::get_singleton());
344
345
OpenXRAPI::OpenXRSwapChainInfo *swapchain_info = reinterpret_cast<OpenXRAPI::OpenXRSwapChainInfo *>(p_swapchain_info);
346
swapchain_info->release();
347
}
348
349
uint64_t OpenXRAPIExtension::get_projection_layer() {
350
ERR_FAIL_NULL_V(OpenXRAPI::get_singleton(), 0);
351
return (uint64_t)OpenXRAPI::get_singleton()->get_projection_layer();
352
}
353
354
void OpenXRAPIExtension::set_render_region(const Rect2i &p_render_region) {
355
ERR_FAIL_NULL(OpenXRAPI::get_singleton());
356
OpenXRAPI::get_singleton()->set_render_region(p_render_region);
357
}
358
359
void OpenXRAPIExtension::update_main_swapchain_size() {
360
ERR_FAIL_NULL(OpenXRAPI::get_singleton());
361
OpenXRAPI::get_singleton()->update_main_swapchain_size();
362
}
363
364
void OpenXRAPIExtension::set_emulate_environment_blend_mode_alpha_blend(bool p_enabled) {
365
ERR_FAIL_NULL(OpenXRAPI::get_singleton());
366
OpenXRAPI::get_singleton()->set_emulate_environment_blend_mode_alpha_blend(p_enabled);
367
}
368
369
OpenXRAPIExtension::OpenXRAlphaBlendModeSupport OpenXRAPIExtension::is_environment_blend_mode_alpha_blend_supported() {
370
ERR_FAIL_NULL_V(OpenXRAPI::get_singleton(), OPENXR_ALPHA_BLEND_MODE_SUPPORT_NONE);
371
return (OpenXRAPIExtension::OpenXRAlphaBlendModeSupport)OpenXRAPI::get_singleton()->is_environment_blend_mode_alpha_blend_supported();
372
}
373
374
OpenXRAPIExtension::OpenXRAPIExtension() {
375
}
376
377