Debian Codegraph @

modules/openxr/extensions/openxr_fb_foveation_extension.cpp

raw symbols SCIP
1 /**************************************************************************/
2 /* openxr_fb_foveation_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_fb_foveation_extension.h"
32 #include "core/config/project_settings.h"
33 #include "openxr_eye_gaze_interaction.h"
34
35 #include "../openxr_platform_inc.h"
36
37 OpenXRFBFoveationExtension *OpenXRFBFoveationExtension::singleton = nullptr;
38
39 OpenXRFBFoveationExtension *OpenXRFBFoveationExtension::get_singleton() {
40 return singleton;
41 }
42
43 OpenXRFBFoveationExtension::OpenXRFBFoveationExtension(const String &p_rendering_driver) {
44 singleton = this;
45 rendering_driver = p_rendering_driver;
46 swapchain_update_state_ext = OpenXRFBUpdateSwapchainExtension::get_singleton();
47 int fov_level = GLOBAL_GET("xr/openxr/foveation_level");
48 if (fov_level >= 0 && fov_level < 4) {
49 foveation_level = XrFoveationLevelFB(fov_level);
50 }
51 bool fov_dyn = GLOBAL_GET("xr/openxr/foveation_dynamic");
52 foveation_dynamic = fov_dyn ? XR_FOVEATION_DYNAMIC_LEVEL_ENABLED_FB : XR_FOVEATION_DYNAMIC_DISABLED_FB;
53
54 swapchain_create_info_foveation_fb.type = XR_TYPE_SWAPCHAIN_CREATE_INFO_FOVEATION_FB;
55 swapchain_create_info_foveation_fb.next = nullptr;
56 swapchain_create_info_foveation_fb.flags = 0;
57
58 meta_foveation_eye_tracked_create_info.type = XR_TYPE_FOVEATION_EYE_TRACKED_PROFILE_CREATE_INFO_META;
59 meta_foveation_eye_tracked_create_info.next = nullptr;
60 meta_foveation_eye_tracked_create_info.flags = 0;
61
62 meta_foveation_eye_tracked_properties.type = XR_TYPE_SYSTEM_FOVEATION_EYE_TRACKED_PROPERTIES_META;
63 meta_foveation_eye_tracked_properties.next = nullptr;
64 meta_foveation_eye_tracked_properties.supportsFoveationEyeTracked = XR_FALSE;
65
66 #ifdef VULKAN_ENABLED
67 meta_vulkan_swapchain_create_info.type = XR_TYPE_VULKAN_SWAPCHAIN_CREATE_INFO_META;
68 meta_vulkan_swapchain_create_info.next = nullptr;
69 meta_vulkan_swapchain_create_info.additionalCreateFlags = VK_IMAGE_CREATE_FRAGMENT_DENSITY_MAP_OFFSET_BIT_QCOM;
70 meta_vulkan_swapchain_create_info.additionalUsageFlags = 0;
71 #endif
72
73 if (rendering_driver == "opengl3") {
74 swapchain_create_info_foveation_fb.flags = XR_SWAPCHAIN_CREATE_FOVEATION_SCALED_BIN_BIT_FB;
75 } else if (rendering_driver == "vulkan") {
76 swapchain_create_info_foveation_fb.flags = XR_SWAPCHAIN_CREATE_FOVEATION_FRAGMENT_DENSITY_MAP_BIT_FB;
77 }
78 }
79
80 OpenXRFBFoveationExtension::~OpenXRFBFoveationExtension() {
81 singleton = nullptr;
82 swapchain_update_state_ext = nullptr;
83 }
84
85 HashMap<String, bool *> OpenXRFBFoveationExtension::get_requested_extensions(XrVersion p_version) {
86 HashMap<String, bool *> request_extensions;
87
88 request_extensions[XR_FB_FOVEATION_EXTENSION_NAME] = &fb_foveation_ext;
89 request_extensions[XR_FB_FOVEATION_CONFIGURATION_EXTENSION_NAME] = &fb_foveation_configuration_ext;
90
91 #ifdef XR_USE_GRAPHICS_API_VULKAN
92 if (rendering_driver == "vulkan") {
93 request_extensions[XR_FB_FOVEATION_VULKAN_EXTENSION_NAME] = &fb_foveation_vulkan_ext;
94 request_extensions[XR_META_FOVEATION_EYE_TRACKED_EXTENSION_NAME] = &meta_foveation_eye_tracked_ext;
95 request_extensions[XR_META_VULKAN_SWAPCHAIN_CREATE_INFO_EXTENSION_NAME] = &meta_vulkan_swapchain_create_info_ext;
96 }
97 #endif // XR_USE_GRAPHICS_API_VULKAN
98
99 return request_extensions;
100 }
101
102 void OpenXRFBFoveationExtension::on_instance_created(const XrInstance p_instance) {
103 if (fb_foveation_ext) {
104 EXT_INIT_XR_FUNC(xrCreateFoveationProfileFB);
105 EXT_INIT_XR_FUNC(xrDestroyFoveationProfileFB);
106 }
107
108 if (fb_foveation_configuration_ext) {
109 // nothing to register here...
110 }
111
112 if (meta_foveation_eye_tracked_ext) {
113 EXT_INIT_XR_FUNC(xrGetFoveationEyeTrackedStateMETA);
114 }
115 }
116
117 void OpenXRFBFoveationExtension::on_instance_destroyed() {
118 fb_foveation_ext = false;
119 fb_foveation_configuration_ext = false;
120 meta_foveation_eye_tracked_ext = false;
121 }
122
123 bool OpenXRFBFoveationExtension::is_enabled() const {
124 bool enabled = swapchain_update_state_ext != nullptr && swapchain_update_state_ext->is_enabled() && fb_foveation_ext && fb_foveation_configuration_ext;
125 #ifdef XR_USE_GRAPHICS_API_VULKAN
126 if (rendering_driver == "vulkan") {
127 enabled = enabled && fb_foveation_vulkan_ext;
128 }
129 #endif // XR_USE_GRAPHICS_API_VULKAN
130 return enabled;
131 }
132
133 void *OpenXRFBFoveationExtension::set_system_properties_and_get_next_pointer(void *p_next_pointer) {
134 #ifdef XR_USE_GRAPHICS_API_VULKAN
135 if (rendering_driver == "vulkan") {
136 meta_foveation_eye_tracked_properties.next = p_next_pointer;
137 return &meta_foveation_eye_tracked_properties;
138 }
139 #endif
140 return p_next_pointer;
141 }
142
143 void *OpenXRFBFoveationExtension::set_swapchain_create_info_and_get_next_pointer(void *p_next_pointer) {
144 void *next = p_next_pointer;
145 if (is_enabled()) {
146 swapchain_create_info_foveation_fb.next = next;
147 next = &swapchain_create_info_foveation_fb;
148
149 #ifdef VULKAN_ENABLED
150 if (meta_foveation_eye_tracked_ext && meta_vulkan_swapchain_create_info_ext && meta_foveation_eye_tracked_properties.supportsFoveationEyeTracked) {
151 meta_vulkan_swapchain_create_info.next = next;
152 next = &meta_vulkan_swapchain_create_info;
153 }
154 #endif
155 }
156
157 return next;
158 }
159
160 void OpenXRFBFoveationExtension::on_main_swapchains_created() {
161 update_profile();
162 }
163
164 XrFoveationLevelFB OpenXRFBFoveationExtension::get_foveation_level() const {
165 return foveation_level;
166 }
167
168 void OpenXRFBFoveationExtension::set_foveation_level(XrFoveationLevelFB p_foveation_level) {
169 foveation_level = p_foveation_level;
170
171 // Update profile will do nothing if we're not yet initialized.
172 update_profile();
173 }
174
175 XrFoveationDynamicFB OpenXRFBFoveationExtension::get_foveation_dynamic() const {
176 return foveation_dynamic;
177 }
178
179 void OpenXRFBFoveationExtension::set_foveation_dynamic(XrFoveationDynamicFB p_foveation_dynamic) {
180 foveation_dynamic = p_foveation_dynamic;
181
182 // Update profile will do nothing if we're not yet initialized.
183 update_profile();
184 }
185
186 bool OpenXRFBFoveationExtension::is_foveation_eye_tracked_enabled() const {
187 return is_enabled() && meta_foveation_eye_tracked_ext && meta_vulkan_swapchain_create_info_ext && meta_foveation_eye_tracked_properties.supportsFoveationEyeTracked;
188 }
189
190 void OpenXRFBFoveationExtension::get_fragment_density_offsets(LocalVector<Vector2i> &r_offsets) {
191 // Must be called from rendering thread!
192 ERR_NOT_ON_RENDER_THREAD;
193
194 if (!is_foveation_eye_tracked_enabled() || !OpenXREyeGazeInteractionExtension::get_singleton()->is_available()) {
195 return;
196 }
197
198 OpenXRAPI *openxr_api = OpenXRAPI::get_singleton();
199 ERR_FAIL_NULL(openxr_api);
200
201 _update_profile_rt();
202
203 XrFoveationEyeTrackedStateMETA state = {
204 XR_TYPE_FOVEATION_EYE_TRACKED_STATE_META, // type
205 nullptr, // next
206 { XrVector2f{}, XrVector2f{} }, // foveationCenter[XR_FOVEATION_CENTER_SIZE_META];
207 0, // flags
208 };
209 XrResult result = xrGetFoveationEyeTrackedStateMETA(openxr_api->get_session(), &state);
210 if (XR_FAILED(result)) {
211 print_line("OpenXR: Unable to get foveation offsets [", openxr_api->get_error_string(result), "]");
212 return;
213 }
214 if (!(state.flags & XR_FOVEATION_EYE_TRACKED_STATE_VALID_BIT_META)) {
215 return;
216 }
217
218 r_offsets.reserve(XR_FOVEATION_CENTER_SIZE_META);
219 Size2 dims = openxr_api->get_recommended_target_size() * 0.5;
220 for (uint32_t i = 0; i < XR_FOVEATION_CENTER_SIZE_META; ++i) {
221 const XrVector2f &xr_center = state.foveationCenter[i];
222 r_offsets.push_back(Vector2i((int)(xr_center.x * dims.x), (int)(xr_center.y * dims.y)));
223 }
224 }
225
226 void OpenXRFBFoveationExtension::_update_profile_rt() {
227 // Must be called from rendering thread!
228 ERR_NOT_ON_RENDER_THREAD;
229
230 if (!is_enabled()) {
231 return;
232 }
233
234 OpenXRAPI *openxr_api = OpenXRAPI::get_singleton();
235 ERR_FAIL_NULL(openxr_api);
236
237 XrSwapchain main_color_swapchain = openxr_api->get_color_swapchain();
238 if (main_color_swapchain == XR_NULL_HANDLE) {
239 // Our swapchain hasn't been created yet, we'll call this again once it has.
240 return;
241 }
242
243 void *next = nullptr;
244 if (meta_foveation_eye_tracked_ext && meta_vulkan_swapchain_create_info_ext && meta_foveation_eye_tracked_properties.supportsFoveationEyeTracked) {
245 meta_foveation_eye_tracked_create_info.next = next;
246 next = &meta_foveation_eye_tracked_create_info;
247 }
248
249 XrFoveationLevelProfileCreateInfoFB level_profile_create_info;
250 level_profile_create_info.type = XR_TYPE_FOVEATION_LEVEL_PROFILE_CREATE_INFO_FB;
251 level_profile_create_info.next = next;
252 level_profile_create_info.level = foveation_level;
253 level_profile_create_info.verticalOffset = 0.0f;
254 level_profile_create_info.dynamic = foveation_dynamic;
255
256 XrFoveationProfileCreateInfoFB profile_create_info;
257 profile_create_info.type = XR_TYPE_FOVEATION_PROFILE_CREATE_INFO_FB;
258 profile_create_info.next = &level_profile_create_info;
259
260 XrFoveationProfileFB foveation_profile;
261 XrResult result = xrCreateFoveationProfileFB(openxr_api->get_session(), &profile_create_info, &foveation_profile);
262 if (XR_FAILED(result)) {
263 print_line("OpenXR: Unable to create the foveation profile [", openxr_api->get_error_string(result), "]");
264 return;
265 }
266
267 XrSwapchainStateFoveationFB foveation_update_state;
268 foveation_update_state.type = XR_TYPE_SWAPCHAIN_STATE_FOVEATION_FB;
269 foveation_update_state.next = nullptr;
270 foveation_update_state.flags = 0;
271 foveation_update_state.profile = foveation_profile;
272
273 result = swapchain_update_state_ext->xrUpdateSwapchainFB(main_color_swapchain, (XrSwapchainStateBaseHeaderFB *)&foveation_update_state);
274 if (XR_FAILED(result)) {
275 print_line("OpenXR: Unable to update the swapchain [", openxr_api->get_error_string(result), "]");
276
277 // We still want to destroy our profile so keep going...
278 }
279
280 result = xrDestroyFoveationProfileFB(foveation_profile);
281 if (XR_FAILED(result)) {
282 print_line("OpenXR: Unable to destroy the foveation profile [", openxr_api->get_error_string(result), "]");
283 }
284 }