Skip to content
0.2.0-dev Download ZIP
136 lines ·5.4 KB Raw
1/**************************************************************************/
2/* openxr_foveated_inset_viewport.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_foveated_inset_viewport.h"
32
33#include "core/config/engine.h"
34#include "core/object/class_db.h"
35#include "servers/xr/xr_server.h"
36
37#include "modules/openxr/extensions/openxr_foveated_inset_extension.h"
38
39OpenXRFoveatedInsetViewport::OpenXRFoveatedInsetViewport() {
40 set_process_internal(true);
41 set_process_priority(99999);
42 set_update_mode(SubViewport::UPDATE_DISABLED);
43
44 OpenXRFoveatedInsetExtension *fi_ext = OpenXRFoveatedInsetExtension::get_singleton();
45 if (!fi_ext) {
46 // Silently fail, this is likely being called when we instantiate our class for
47 // populating ClassDB.
48 return;
49 }
50
51 fi_ext->register_viewport(get_viewport_rid());
52
53 xr_origin = memnew(XROrigin3D);
54 xr_origin->set_current(false);
55 add_child(xr_origin, false, Node3D::INTERNAL_MODE_BACK);
56
57 xr_camera = memnew(XRCamera3D);
58 xr_camera->set_tracker(XR_TRACKER_INSET);
59 xr_origin->add_child(xr_camera, false, Node3D::INTERNAL_MODE_BACK);
60}
61
62OpenXRFoveatedInsetViewport::~OpenXRFoveatedInsetViewport() {
63 OpenXRFoveatedInsetExtension *fi_ext = OpenXRFoveatedInsetExtension::get_singleton();
64 if (!fi_ext) {
65 // Silently fail, this is likely being called after we instantiated our class for
66 // populating ClassDB.
67 return;
68 }
69
70 fi_ext->unregister_viewport(get_viewport_rid());
71}
72
73void OpenXRFoveatedInsetViewport::_bind_methods() {
74 ClassDB::bind_method(D_METHOD("get_xr_origin3d"), &OpenXRFoveatedInsetViewport::get_xr_origin3d);
75 ClassDB::bind_method(D_METHOD("get_xr_camera3d"), &OpenXRFoveatedInsetViewport::get_xr_camera3d);
76}
77
78void OpenXRFoveatedInsetViewport::_validate_property(PropertyInfo &p_property) const {
79 if (!Engine::get_singleton()->is_editor_hint()) {
80 return;
81 }
82 // Hide properties that are managed by us.
83 if (p_property.name == "size" || p_property.name == "size_2d_override" || p_property.name == "size_2d_override_stretch" || p_property.name == "view_count" || p_property.name == "use_xr" || p_property.name == "render_target_update_mode") {
84 p_property.usage = PROPERTY_USAGE_NO_EDITOR;
85 }
86}
87
88void OpenXRFoveatedInsetViewport::_notification(int p_what) {
89 if (Engine::get_singleton()->is_editor_hint()) {
90 return;
91 }
92
93 switch (p_what) {
94 case NOTIFICATION_INTERNAL_PROCESS: {
95 // Update the size of our viewport
96 _update_size();
97
98 // Apply our world origin
99 XRServer *xr_server = XRServer::get_singleton();
100 if (xr_server) {
101 xr_origin->set_transform(xr_server->get_world_origin());
102 }
103 } break;
104 }
105}
106
107void OpenXRFoveatedInsetViewport::_update_size() {
108 OpenXRAPI *openxr_api = OpenXRAPI::get_singleton();
109 OpenXRFoveatedInsetExtension *fi_ext = OpenXRFoveatedInsetExtension::get_singleton();
110 if (openxr_api == nullptr || fi_ext == nullptr) {
111 set_update_mode(SubViewport::UPDATE_DISABLED);
112 return;
113 }
114
115 if (!openxr_api->is_initialized()) {
116 set_update_mode(SubViewport::UPDATE_DISABLED);
117 return;
118 }
119
120 if (openxr_api->get_view_configuration() != XR_VIEW_CONFIGURATION_TYPE_PRIMARY_STEREO_WITH_FOVEATED_INSET) {
121 WARN_PRINT_ONCE("View configuration is not set to stereo with foveated inset, or this is not supported.");
122 set_update_mode(SubViewport::UPDATE_DISABLED);
123 return;
124 }
125
126 set_update_mode(SubViewport::UPDATE_ALWAYS);
127
128 Size2i new_size = fi_ext->get_render_size();
129 if (get_size() != new_size) {
130 set_size(new_size);
131 }
132
133 if (get_view_count() != 2) {
134 set_view_count(2);
135 }
136}