Debian Codegraph @

servers/xr/xr_interface_extension.cpp

raw symbols SCIP
1 /**************************************************************************/
2 /* xr_interface_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 "xr_interface_extension.h"
32 #include "servers/rendering/renderer_rd/storage_rd/texture_storage.h"
33
34 void XRInterfaceExtension::_bind_methods() {
35 GDVIRTUAL_BIND(_get_name);
36 GDVIRTUAL_BIND(_get_capabilities);
37
38 GDVIRTUAL_BIND(_is_initialized);
39 GDVIRTUAL_BIND(_initialize);
40 GDVIRTUAL_BIND(_uninitialize);
41 GDVIRTUAL_BIND(_get_system_info);
42
43 GDVIRTUAL_BIND(_supports_play_area_mode, "mode");
44 GDVIRTUAL_BIND(_get_play_area_mode);
45 GDVIRTUAL_BIND(_set_play_area_mode, "mode");
46 GDVIRTUAL_BIND(_get_play_area);
47
48 GDVIRTUAL_BIND(_get_render_target_size);
49 GDVIRTUAL_BIND(_get_view_count);
50 GDVIRTUAL_BIND(_get_camera_transform);
51 GDVIRTUAL_BIND(_get_transform_for_view, "view", "cam_transform");
52 GDVIRTUAL_BIND(_get_projection_for_view, "view", "aspect", "z_near", "z_far");
53 GDVIRTUAL_BIND(_get_vrs_texture);
54 GDVIRTUAL_BIND(_get_vrs_texture_format);
55
56 GDVIRTUAL_BIND(_process);
57 GDVIRTUAL_BIND(_pre_render);
58 GDVIRTUAL_BIND(_pre_draw_viewport, "render_target");
59 GDVIRTUAL_BIND(_post_draw_viewport, "render_target", "screen_rect");
60 GDVIRTUAL_BIND(_end_frame);
61
62 /** input and output **/
63
64 GDVIRTUAL_BIND(_get_suggested_tracker_names);
65 GDVIRTUAL_BIND(_get_suggested_pose_names, "tracker_name");
66 GDVIRTUAL_BIND(_get_tracking_status);
67 GDVIRTUAL_BIND(_trigger_haptic_pulse, "action_name", "tracker_name", "frequency", "amplitude", "duration_sec", "delay_sec");
68
69 // we don't have any properties specific to VR yet....
70
71 // but we do have properties specific to AR....
72 GDVIRTUAL_BIND(_get_anchor_detection_is_enabled);
73 GDVIRTUAL_BIND(_set_anchor_detection_is_enabled, "enabled");
74 GDVIRTUAL_BIND(_get_camera_feed_id);
75
76 // override output methods
77 GDVIRTUAL_BIND(_get_color_texture);
78 GDVIRTUAL_BIND(_get_depth_texture);
79 GDVIRTUAL_BIND(_get_velocity_texture);
80
81 ClassDB::bind_method(D_METHOD("get_color_texture"), &XRInterfaceExtension::get_color_texture);
82 ClassDB::bind_method(D_METHOD("get_depth_texture"), &XRInterfaceExtension::get_depth_texture);
83 ClassDB::bind_method(D_METHOD("get_velocity_texture"), &XRInterfaceExtension::get_velocity_texture);
84
85 // helper methods
86 ClassDB::bind_method(D_METHOD("add_blit", "render_target", "src_rect", "dst_rect", "use_layer", "layer", "apply_lens_distortion", "eye_center", "k1", "k2", "upscale", "aspect_ratio"), &XRInterfaceExtension::add_blit);
87 ClassDB::bind_method(D_METHOD("get_render_target_texture", "render_target"), &XRInterfaceExtension::get_render_target_texture);
88 // ClassDB::bind_method(D_METHOD("get_render_target_depth", "render_target"), &XRInterfaceExtension::get_render_target_depth);
89 }
90
91 StringName XRInterfaceExtension::get_name() const {
92 StringName name;
93
94 if (GDVIRTUAL_CALL(_get_name, name)) {
95 return name;
96 }
97
98 return "Unknown";
99 }
100
101 uint32_t XRInterfaceExtension::get_capabilities() const {
102 uint32_t capabilities = 0;
103 GDVIRTUAL_CALL(_get_capabilities, capabilities);
104 return capabilities;
105 }
106
107 bool XRInterfaceExtension::is_initialized() const {
108 bool initialized = false;
109 GDVIRTUAL_CALL(_is_initialized, initialized);
110 return initialized;
111 }
112
113 bool XRInterfaceExtension::initialize() {
114 bool initialized = false;
115 GDVIRTUAL_CALL(_initialize, initialized);
116 return initialized;
117 }
118
119 void XRInterfaceExtension::uninitialize() {
120 GDVIRTUAL_CALL(_uninitialize);
121 }
122
123 Dictionary XRInterfaceExtension::get_system_info() {
124 Dictionary dict;
125 GDVIRTUAL_CALL(_get_system_info, dict);
126 return dict;
127 }
128
129 PackedStringArray XRInterfaceExtension::get_suggested_tracker_names() const {
130 PackedStringArray arr;
131
132 GDVIRTUAL_CALL(_get_suggested_tracker_names, arr);
133
134 return arr;
135 }
136
137 PackedStringArray XRInterfaceExtension::get_suggested_pose_names(const StringName &p_tracker_name) const {
138 PackedStringArray arr;
139
140 GDVIRTUAL_CALL(_get_suggested_pose_names, p_tracker_name, arr);
141
142 return arr;
143 }
144
145 XRInterface::TrackingStatus XRInterfaceExtension::get_tracking_status() const {
146 XRInterface::TrackingStatus status = XR_UNKNOWN_TRACKING;
147 GDVIRTUAL_CALL(_get_tracking_status, status);
148 return status;
149 }
150
151 void XRInterfaceExtension::trigger_haptic_pulse(const String &p_action_name, const StringName &p_tracker_name, double p_frequency, double p_amplitude, double p_duration_sec, double p_delay_sec) {
152 GDVIRTUAL_CALL(_trigger_haptic_pulse, p_action_name, p_tracker_name, p_frequency, p_amplitude, p_duration_sec, p_delay_sec);
153 }
154
155 bool XRInterfaceExtension::supports_play_area_mode(XRInterface::PlayAreaMode p_mode) {
156 bool is_supported = false;
157 GDVIRTUAL_CALL(_supports_play_area_mode, p_mode, is_supported);
158 return is_supported;
159 }
160
161 XRInterface::PlayAreaMode XRInterfaceExtension::get_play_area_mode() const {
162 XRInterface::PlayAreaMode mode = XR_PLAY_AREA_UNKNOWN;
163 GDVIRTUAL_CALL(_get_play_area_mode, mode);
164 return mode;
165 }
166
167 bool XRInterfaceExtension::set_play_area_mode(XRInterface::PlayAreaMode p_mode) {
168 bool success = false;
169 GDVIRTUAL_CALL(_set_play_area_mode, p_mode, success);
170 return success;
171 }
172
173 PackedVector3Array XRInterfaceExtension::get_play_area() const {
174 PackedVector3Array arr;
175 GDVIRTUAL_CALL(_get_play_area, arr);
176 return arr;
177 }
178
179 /** these will only be implemented on AR interfaces, so we want dummies for VR **/
180 bool XRInterfaceExtension::get_anchor_detection_is_enabled() const {
181 bool enabled = false;
182 GDVIRTUAL_CALL(_get_anchor_detection_is_enabled, enabled);
183 return enabled;
184 }
185
186 void XRInterfaceExtension::set_anchor_detection_is_enabled(bool p_enable) {
187 // don't do anything here, this needs to be implemented on AR interface to enable/disable things like plane detection etc.
188 GDVIRTUAL_CALL(_set_anchor_detection_is_enabled, p_enable);
189 }
190
191 int XRInterfaceExtension::get_camera_feed_id() {
192 int feed_id = 0;
193 GDVIRTUAL_CALL(_get_camera_feed_id, feed_id);
194 return feed_id;
195 }
196
197 Size2 XRInterfaceExtension::get_render_target_size() {
198 Size2 size;
199 GDVIRTUAL_CALL(_get_render_target_size, size);
200 return size;
201 }
202
203 uint32_t XRInterfaceExtension::get_view_count() {
204 uint32_t view_count = 1;
205 GDVIRTUAL_CALL(_get_view_count, view_count);
206 return view_count;
207 }
208
209 Transform3D XRInterfaceExtension::get_camera_transform() {
210 Transform3D transform;
211 GDVIRTUAL_CALL(_get_camera_transform, transform);
212 return transform;
213 }
214
215 Transform3D XRInterfaceExtension::get_transform_for_view(uint32_t p_view, const Transform3D &p_cam_transform) {
216 Transform3D transform;
217 GDVIRTUAL_CALL(_get_transform_for_view, p_view, p_cam_transform, transform);
218 return transform;
219 }
220
221 Projection XRInterfaceExtension::get_projection_for_view(uint32_t p_view, double p_aspect, double p_z_near, double p_z_far) {
222 Projection cm;
223 PackedFloat64Array arr;
224
225 if (GDVIRTUAL_CALL(_get_projection_for_view, p_view, p_aspect, p_z_near, p_z_far, arr)) {
226 ERR_FAIL_COND_V_MSG(arr.size() != 16, Projection(), "Projection matrix must contain 16 floats");
227 real_t *m = (real_t *)cm.columns;
228 for (int i = 0; i < 16; i++) {
229 m[i] = arr[i];
230 }
231 return cm;
232 }
233
234 return Projection();
235 }
236
237 RID XRInterfaceExtension::get_vrs_texture() {
238 RID vrs_texture;
239 if (GDVIRTUAL_CALL(_get_vrs_texture, vrs_texture)) {
240 return vrs_texture;
241 } else {
242 return XRInterface::get_vrs_texture();
243 }
244 }
245
246 XRInterface::VRSTextureFormat XRInterfaceExtension::get_vrs_texture_format() {
247 VRSTextureFormat vrs_texture_format = XR_VRS_TEXTURE_FORMAT_UNIFIED;
248 if (GDVIRTUAL_CALL(_get_vrs_texture_format, vrs_texture_format)) {
249 return vrs_texture_format;
250 }
251 return vrs_texture_format;
252 }
253
254 RID XRInterfaceExtension::get_color_texture() {
255 RID texture;
256 GDVIRTUAL_CALL(_get_color_texture, texture);
257 return texture;
258 }
259
260 RID XRInterfaceExtension::get_depth_texture() {
261 RID texture;
262 GDVIRTUAL_CALL(_get_depth_texture, texture);
263 return texture;
264 }
265
266 RID XRInterfaceExtension::get_velocity_texture() {
267 RID texture;
268 GDVIRTUAL_CALL(_get_velocity_texture, texture);
269 return texture;
270 }
271
272 void XRInterfaceExtension::add_blit(RID p_render_target, Rect2 p_src_rect, Rect2i p_dst_rect, bool p_use_layer, uint32_t p_layer, bool p_apply_lens_distortion, Vector2 p_eye_center, double p_k1, double p_k2, double p_upscale, double p_aspect_ratio) {
273 BlitToScreen blit;
274
275 ERR_FAIL_COND_MSG(!can_add_blits, "add_blit can only be called from an XR plugin from within _post_draw_viewport!");
276
277 blit.render_target = p_render_target;
278 blit.src_rect = p_src_rect;
279 blit.dst_rect = p_dst_rect;
280
281 blit.multi_view.use_layer = p_use_layer;
282 blit.multi_view.layer = p_layer;
283
284 blit.lens_distortion.apply = p_apply_lens_distortion;
285 blit.lens_distortion.eye_center = p_eye_center;
286 blit.lens_distortion.k1 = p_k1;
287 blit.lens_distortion.k2 = p_k2;
288 blit.lens_distortion.upscale = p_upscale;
289 blit.lens_distortion.aspect_ratio = p_aspect_ratio;
290
291 blits.push_back(blit);
292 }
293
294 void XRInterfaceExtension::process() {
295 GDVIRTUAL_CALL(_process);
296 }
297
298 void XRInterfaceExtension::pre_render() {
299 GDVIRTUAL_CALL(_pre_render);
300 }
301
302 bool XRInterfaceExtension::pre_draw_viewport(RID p_render_target) {
303 bool do_render = true;
304 GDVIRTUAL_CALL(_pre_draw_viewport, p_render_target, do_render);
305 return do_render; // If not implemented we're returning true.
306 }
307
308 Vector<BlitToScreen> XRInterfaceExtension::post_draw_viewport(RID p_render_target, const Rect2 &p_screen_rect) {
309 // This is just so our XR plugin can add blits...
310 blits.clear();
311 can_add_blits = true;
312
313 if (GDVIRTUAL_CALL(_post_draw_viewport, p_render_target, p_screen_rect)) {
314 return blits;
315 }
316
317 can_add_blits = false;
318 return blits;
319 }
320
321 void XRInterfaceExtension::end_frame() {
322 GDVIRTUAL_CALL(_end_frame);
323 }
324
325 RID XRInterfaceExtension::get_render_target_texture(RID p_render_target) {
326 // In due time this will need to be enhance to return the correct INTERNAL RID for the chosen rendering engine.
327 // So once a GLES driver is implemented we'll return that and the implemented plugin needs to handle this correctly too.
328 RendererRD::TextureStorage *texture_storage = RendererRD::TextureStorage::get_singleton();
329 ERR_FAIL_NULL_V_MSG(texture_storage, RID(), "Texture storage not setup");
330
331 return texture_storage->render_target_get_rd_texture(p_render_target);
332 }
333
334 /*
335 RID XRInterfaceExtension::get_render_target_depth(RID p_render_target) {
336 // TODO implement this, the problem is that our depth texture isn't part of our render target as it is used for 3D rendering only
337 // but we don't have access to our render buffers from here....
338 }
339 */