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 | #include "servers/rendering/rendering_server_globals.h" |
34 | |
35 | void XRInterfaceExtension::_bind_methods() { |
36 | GDVIRTUAL_BIND(_get_name); |
37 | GDVIRTUAL_BIND(_get_capabilities); |
38 | |
39 | GDVIRTUAL_BIND(_is_initialized); |
40 | GDVIRTUAL_BIND(_initialize); |
41 | GDVIRTUAL_BIND(_uninitialize); |
42 | GDVIRTUAL_BIND(_get_system_info); |
43 | |
44 | GDVIRTUAL_BIND(_supports_play_area_mode, "mode" ); |
45 | GDVIRTUAL_BIND(_get_play_area_mode); |
46 | GDVIRTUAL_BIND(_set_play_area_mode, "mode" ); |
47 | GDVIRTUAL_BIND(_get_play_area); |
48 | |
49 | GDVIRTUAL_BIND(_get_render_target_size); |
50 | GDVIRTUAL_BIND(_get_view_count); |
51 | GDVIRTUAL_BIND(_get_camera_transform); |
52 | GDVIRTUAL_BIND(_get_transform_for_view, "view" , "cam_transform" ); |
53 | GDVIRTUAL_BIND(_get_projection_for_view, "view" , "aspect" , "z_near" , "z_far" ); |
54 | GDVIRTUAL_BIND(_get_vrs_texture); |
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 | RID XRInterfaceExtension::get_color_texture() { |
247 | RID texture; |
248 | GDVIRTUAL_CALL(_get_color_texture, texture); |
249 | return texture; |
250 | } |
251 | |
252 | RID XRInterfaceExtension::get_depth_texture() { |
253 | RID texture; |
254 | GDVIRTUAL_CALL(_get_depth_texture, texture); |
255 | return texture; |
256 | } |
257 | |
258 | RID XRInterfaceExtension::get_velocity_texture() { |
259 | RID texture; |
260 | GDVIRTUAL_CALL(_get_velocity_texture, texture); |
261 | return texture; |
262 | } |
263 | |
264 | 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) { |
265 | BlitToScreen blit; |
266 | |
267 | ERR_FAIL_COND_MSG(!can_add_blits, "add_blit can only be called from an XR plugin from within _post_draw_viewport!" ); |
268 | |
269 | blit.render_target = p_render_target; |
270 | blit.src_rect = p_src_rect; |
271 | blit.dst_rect = p_dst_rect; |
272 | |
273 | blit.multi_view.use_layer = p_use_layer; |
274 | blit.multi_view.layer = p_layer; |
275 | |
276 | blit.lens_distortion.apply = p_apply_lens_distortion; |
277 | blit.lens_distortion.eye_center = p_eye_center; |
278 | blit.lens_distortion.k1 = p_k1; |
279 | blit.lens_distortion.k2 = p_k2; |
280 | blit.lens_distortion.upscale = p_upscale; |
281 | blit.lens_distortion.aspect_ratio = p_aspect_ratio; |
282 | |
283 | blits.push_back(blit); |
284 | } |
285 | |
286 | void XRInterfaceExtension::process() { |
287 | GDVIRTUAL_CALL(_process); |
288 | } |
289 | |
290 | void XRInterfaceExtension::pre_render() { |
291 | GDVIRTUAL_CALL(_pre_render); |
292 | } |
293 | |
294 | bool XRInterfaceExtension::pre_draw_viewport(RID p_render_target) { |
295 | bool do_render = true; |
296 | GDVIRTUAL_CALL(_pre_draw_viewport, p_render_target, do_render); |
297 | return do_render; // If not implemented we're returning true. |
298 | } |
299 | |
300 | Vector<BlitToScreen> XRInterfaceExtension::post_draw_viewport(RID p_render_target, const Rect2 &p_screen_rect) { |
301 | // This is just so our XR plugin can add blits... |
302 | blits.clear(); |
303 | can_add_blits = true; |
304 | |
305 | if (GDVIRTUAL_CALL(_post_draw_viewport, p_render_target, p_screen_rect)) { |
306 | return blits; |
307 | } |
308 | |
309 | can_add_blits = false; |
310 | return blits; |
311 | } |
312 | |
313 | void XRInterfaceExtension::end_frame() { |
314 | GDVIRTUAL_CALL(_end_frame); |
315 | } |
316 | |
317 | RID XRInterfaceExtension::get_render_target_texture(RID p_render_target) { |
318 | // In due time this will need to be enhance to return the correct INTERNAL RID for the chosen rendering engine. |
319 | // So once a GLES driver is implemented we'll return that and the implemented plugin needs to handle this correctly too. |
320 | RendererRD::TextureStorage *texture_storage = RendererRD::TextureStorage::get_singleton(); |
321 | ERR_FAIL_NULL_V_MSG(texture_storage, RID(), "Texture storage not setup" ); |
322 | |
323 | return texture_storage->render_target_get_rd_texture(p_render_target); |
324 | } |
325 | |
326 | /* |
327 | RID XRInterfaceExtension::get_render_target_depth(RID p_render_target) { |
328 | // 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 |
329 | // but we don't have access to our render buffers from here.... |
330 | } |
331 | */ |
332 | |