1 | /**************************************************************************/ |
2 | /* renderer_viewport.h */ |
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 | #ifndef RENDERER_VIEWPORT_H |
32 | #define RENDERER_VIEWPORT_H |
33 | |
34 | #include "core/templates/local_vector.h" |
35 | #include "core/templates/rid_owner.h" |
36 | #include "core/templates/self_list.h" |
37 | #include "servers/rendering/renderer_scene_render.h" |
38 | #include "servers/rendering/rendering_method.h" |
39 | #include "servers/rendering_server.h" |
40 | #include "servers/xr/xr_interface.h" |
41 | #include "storage/render_scene_buffers.h" |
42 | |
43 | class RendererViewport { |
44 | public: |
45 | struct CanvasBase { |
46 | }; |
47 | |
48 | struct Viewport { |
49 | RID self; |
50 | RID parent; |
51 | |
52 | // use xr interface to override camera positioning and projection matrices and control output |
53 | bool use_xr = false; |
54 | |
55 | Size2i internal_size; |
56 | Size2i size; |
57 | uint32_t view_count; |
58 | RID camera; |
59 | RID scenario; |
60 | |
61 | RS::ViewportScaling3DMode scaling_3d_mode = RenderingServer::VIEWPORT_SCALING_3D_MODE_BILINEAR; |
62 | float scaling_3d_scale = 1.0; |
63 | float fsr_sharpness = 0.2f; |
64 | float texture_mipmap_bias = 0.0f; |
65 | bool fsr_enabled = false; |
66 | RS::ViewportUpdateMode update_mode = RenderingServer::VIEWPORT_UPDATE_WHEN_VISIBLE; |
67 | RID render_target; |
68 | RID render_target_texture; |
69 | Ref<RenderSceneBuffers> render_buffers; |
70 | |
71 | RS::ViewportMSAA msaa_2d = RenderingServer::VIEWPORT_MSAA_DISABLED; |
72 | RS::ViewportMSAA msaa_3d = RenderingServer::VIEWPORT_MSAA_DISABLED; |
73 | RS::ViewportScreenSpaceAA screen_space_aa = RenderingServer::VIEWPORT_SCREEN_SPACE_AA_DISABLED; |
74 | bool use_taa = false; |
75 | bool use_debanding = false; |
76 | |
77 | RendererSceneRender::CameraData prev_camera_data; |
78 | uint64_t prev_camera_data_frame = 0; |
79 | |
80 | bool use_occlusion_culling = false; |
81 | bool occlusion_buffer_dirty = false; |
82 | |
83 | DisplayServer::WindowID viewport_to_screen; |
84 | Rect2 viewport_to_screen_rect; |
85 | bool viewport_render_direct_to_screen; |
86 | |
87 | bool disable_2d = false; |
88 | RS::ViewportEnvironmentMode disable_environment = RS::VIEWPORT_ENVIRONMENT_INHERIT; |
89 | bool disable_3d = false; |
90 | bool measure_render_time = false; |
91 | |
92 | bool snap_2d_transforms_to_pixel = false; |
93 | bool snap_2d_vertices_to_pixel = false; |
94 | |
95 | uint64_t time_cpu_begin; |
96 | uint64_t time_cpu_end; |
97 | |
98 | uint64_t time_gpu_begin; |
99 | uint64_t time_gpu_end; |
100 | |
101 | RID shadow_atlas; |
102 | int shadow_atlas_size = 2048; |
103 | bool shadow_atlas_16_bits = true; |
104 | |
105 | bool sdf_active = false; |
106 | |
107 | float mesh_lod_threshold = 1.0; |
108 | |
109 | uint64_t last_pass = 0; |
110 | |
111 | RS::ViewportDebugDraw debug_draw = RenderingServer::VIEWPORT_DEBUG_DRAW_DISABLED; |
112 | |
113 | RS::ViewportClearMode clear_mode = RenderingServer::VIEWPORT_CLEAR_ALWAYS; |
114 | |
115 | RS::CanvasItemTextureFilter texture_filter = RS::CANVAS_ITEM_TEXTURE_FILTER_LINEAR; |
116 | RS::CanvasItemTextureRepeat texture_repeat = RS::CANVAS_ITEM_TEXTURE_REPEAT_DISABLED; |
117 | |
118 | bool transparent_bg = false; |
119 | bool use_hdr_2d = false; |
120 | |
121 | uint32_t canvas_cull_mask = 0xffffffff; |
122 | |
123 | struct CanvasKey { |
124 | int64_t stacking; |
125 | RID canvas; |
126 | bool operator<(const CanvasKey &p_canvas) const { |
127 | if (stacking == p_canvas.stacking) { |
128 | return canvas < p_canvas.canvas; |
129 | } |
130 | return stacking < p_canvas.stacking; |
131 | } |
132 | CanvasKey() { |
133 | stacking = 0; |
134 | } |
135 | CanvasKey(const RID &p_canvas, int p_layer, int p_sublayer) { |
136 | canvas = p_canvas; |
137 | int64_t sign = p_layer < 0 ? -1 : 1; |
138 | stacking = sign * (((int64_t)ABS(p_layer)) << 32) + p_sublayer; |
139 | } |
140 | int get_layer() const { return stacking >> 32; } |
141 | }; |
142 | |
143 | struct CanvasData { |
144 | CanvasBase *canvas = nullptr; |
145 | Transform2D transform; |
146 | int layer; |
147 | int sublayer; |
148 | }; |
149 | |
150 | Transform2D global_transform; |
151 | |
152 | HashMap<RID, CanvasData> canvas_map; |
153 | |
154 | RenderingMethod::RenderInfo render_info; |
155 | |
156 | Viewport() { |
157 | view_count = 1; |
158 | update_mode = RS::VIEWPORT_UPDATE_WHEN_VISIBLE; |
159 | clear_mode = RS::VIEWPORT_CLEAR_ALWAYS; |
160 | transparent_bg = false; |
161 | use_hdr_2d = false; |
162 | |
163 | viewport_to_screen = DisplayServer::INVALID_WINDOW_ID; |
164 | shadow_atlas_size = 0; |
165 | measure_render_time = false; |
166 | |
167 | debug_draw = RS::VIEWPORT_DEBUG_DRAW_DISABLED; |
168 | screen_space_aa = RS::VIEWPORT_SCREEN_SPACE_AA_DISABLED; |
169 | use_debanding = false; |
170 | use_occlusion_culling = false; |
171 | occlusion_buffer_dirty = true; |
172 | |
173 | snap_2d_transforms_to_pixel = false; |
174 | snap_2d_vertices_to_pixel = false; |
175 | |
176 | use_xr = false; |
177 | sdf_active = false; |
178 | |
179 | time_cpu_begin = 0; |
180 | time_cpu_end = 0; |
181 | |
182 | time_gpu_begin = 0; |
183 | time_gpu_end = 0; |
184 | } |
185 | }; |
186 | |
187 | HashMap<String, RID> timestamp_vp_map; |
188 | |
189 | uint64_t draw_viewports_pass = 0; |
190 | |
191 | mutable RID_Owner<Viewport, true> viewport_owner; |
192 | |
193 | Vector<Viewport *> active_viewports; |
194 | Vector<Viewport *> sorted_active_viewports; |
195 | bool sorted_active_viewports_dirty = false; |
196 | |
197 | int total_objects_drawn = 0; |
198 | int total_vertices_drawn = 0; |
199 | int total_draw_calls_used = 0; |
200 | |
201 | int num_viewports_with_motion_vectors = 0; |
202 | |
203 | private: |
204 | Vector<Viewport *> _sort_active_viewports(); |
205 | void _viewport_set_size(Viewport *p_viewport, int p_width, int p_height, uint32_t p_view_count); |
206 | void _configure_3d_render_buffers(Viewport *p_viewport); |
207 | void _draw_3d(Viewport *p_viewport); |
208 | void _draw_viewport(Viewport *p_viewport); |
209 | |
210 | int occlusion_rays_per_thread = 512; |
211 | |
212 | void _resize_occlusion_culling_buffer(const Size2i &p_size); |
213 | |
214 | public: |
215 | RID viewport_allocate(); |
216 | void viewport_initialize(RID p_rid); |
217 | |
218 | void viewport_set_use_xr(RID p_viewport, bool p_use_xr); |
219 | |
220 | void viewport_set_size(RID p_viewport, int p_width, int p_height); |
221 | |
222 | void viewport_attach_to_screen(RID p_viewport, const Rect2 &p_rect = Rect2(), DisplayServer::WindowID p_screen = DisplayServer::MAIN_WINDOW_ID); |
223 | void viewport_set_render_direct_to_screen(RID p_viewport, bool p_enable); |
224 | |
225 | void viewport_set_active(RID p_viewport, bool p_active); |
226 | void viewport_set_parent_viewport(RID p_viewport, RID p_parent_viewport); |
227 | |
228 | void viewport_set_scaling_3d_mode(RID p_viewport, RS::ViewportScaling3DMode p_mode); |
229 | void viewport_set_scaling_3d_scale(RID p_viewport, float p_scaling_3d_scale); |
230 | void viewport_set_fsr_sharpness(RID p_viewport, float p_sharpness); |
231 | void viewport_set_texture_mipmap_bias(RID p_viewport, float p_mipmap_bias); |
232 | |
233 | void viewport_set_update_mode(RID p_viewport, RS::ViewportUpdateMode p_mode); |
234 | void viewport_set_vflip(RID p_viewport, bool p_enable); |
235 | |
236 | void viewport_set_clear_mode(RID p_viewport, RS::ViewportClearMode p_clear_mode); |
237 | |
238 | RID viewport_get_render_target(RID p_viewport) const; |
239 | RID viewport_get_texture(RID p_viewport) const; |
240 | RID viewport_get_occluder_debug_texture(RID p_viewport) const; |
241 | |
242 | void viewport_set_prev_camera_data(RID p_viewport, const RendererSceneRender::CameraData *p_camera_data); |
243 | const RendererSceneRender::CameraData *viewport_get_prev_camera_data(RID p_viewport); |
244 | |
245 | void viewport_set_disable_2d(RID p_viewport, bool p_disable); |
246 | void viewport_set_environment_mode(RID p_viewport, RS::ViewportEnvironmentMode p_mode); |
247 | void viewport_set_disable_3d(RID p_viewport, bool p_disable); |
248 | |
249 | bool viewport_is_environment_disabled(Viewport *viewport); |
250 | |
251 | void viewport_attach_camera(RID p_viewport, RID p_camera); |
252 | void viewport_set_scenario(RID p_viewport, RID p_scenario); |
253 | void viewport_attach_canvas(RID p_viewport, RID p_canvas); |
254 | void viewport_remove_canvas(RID p_viewport, RID p_canvas); |
255 | void viewport_set_canvas_transform(RID p_viewport, RID p_canvas, const Transform2D &p_offset); |
256 | void viewport_set_transparent_background(RID p_viewport, bool p_enabled); |
257 | void viewport_set_use_hdr_2d(RID p_viewport, bool p_use_hdr_2d); |
258 | |
259 | void viewport_set_global_canvas_transform(RID p_viewport, const Transform2D &p_transform); |
260 | void viewport_set_canvas_stacking(RID p_viewport, RID p_canvas, int p_layer, int p_sublayer); |
261 | |
262 | void viewport_set_canvas_cull_mask(RID p_viewport, uint32_t p_canvas_cull_mask); |
263 | |
264 | void viewport_set_positional_shadow_atlas_size(RID p_viewport, int p_size, bool p_16_bits = true); |
265 | void viewport_set_positional_shadow_atlas_quadrant_subdivision(RID p_viewport, int p_quadrant, int p_subdiv); |
266 | |
267 | void viewport_set_msaa_2d(RID p_viewport, RS::ViewportMSAA p_msaa); |
268 | void viewport_set_msaa_3d(RID p_viewport, RS::ViewportMSAA p_msaa); |
269 | void viewport_set_screen_space_aa(RID p_viewport, RS::ViewportScreenSpaceAA p_mode); |
270 | void viewport_set_use_taa(RID p_viewport, bool p_use_taa); |
271 | void viewport_set_use_debanding(RID p_viewport, bool p_use_debanding); |
272 | void viewport_set_use_occlusion_culling(RID p_viewport, bool p_use_occlusion_culling); |
273 | void viewport_set_occlusion_rays_per_thread(int p_rays_per_thread); |
274 | void viewport_set_occlusion_culling_build_quality(RS::ViewportOcclusionCullingBuildQuality p_quality); |
275 | void viewport_set_mesh_lod_threshold(RID p_viewport, float p_pixels); |
276 | |
277 | virtual int viewport_get_render_info(RID p_viewport, RS::ViewportRenderInfoType p_type, RS::ViewportRenderInfo p_info); |
278 | virtual void viewport_set_debug_draw(RID p_viewport, RS::ViewportDebugDraw p_draw); |
279 | |
280 | void viewport_set_measure_render_time(RID p_viewport, bool p_enable); |
281 | float viewport_get_measured_render_time_cpu(RID p_viewport) const; |
282 | float viewport_get_measured_render_time_gpu(RID p_viewport) const; |
283 | |
284 | void viewport_set_snap_2d_transforms_to_pixel(RID p_viewport, bool p_enabled); |
285 | void viewport_set_snap_2d_vertices_to_pixel(RID p_viewport, bool p_enabled); |
286 | |
287 | void viewport_set_default_canvas_item_texture_filter(RID p_viewport, RS::CanvasItemTextureFilter p_filter); |
288 | void viewport_set_default_canvas_item_texture_repeat(RID p_viewport, RS::CanvasItemTextureRepeat p_repeat); |
289 | |
290 | void viewport_set_sdf_oversize_and_scale(RID p_viewport, RS::ViewportSDFOversize p_over_size, RS::ViewportSDFScale p_scale); |
291 | |
292 | virtual RID viewport_find_from_screen_attachment(DisplayServer::WindowID p_id = DisplayServer::MAIN_WINDOW_ID) const; |
293 | |
294 | void viewport_set_vrs_mode(RID p_viewport, RS::ViewportVRSMode p_mode); |
295 | void viewport_set_vrs_texture(RID p_viewport, RID p_texture); |
296 | |
297 | void handle_timestamp(String p_timestamp, uint64_t p_cpu_time, uint64_t p_gpu_time); |
298 | |
299 | void set_default_clear_color(const Color &p_color); |
300 | void draw_viewports(); |
301 | |
302 | bool free(RID p_rid); |
303 | |
304 | int get_total_objects_drawn() const; |
305 | int get_total_primitives_drawn() const; |
306 | int get_total_draw_calls_used() const; |
307 | int get_num_viewports_with_motion_vectors() const; |
308 | |
309 | // Workaround for setting this on thread. |
310 | void call_set_vsync_mode(DisplayServer::VSyncMode p_mode, DisplayServer::WindowID p_window); |
311 | |
312 | RendererViewport(); |
313 | virtual ~RendererViewport() {} |
314 | }; |
315 | |
316 | #endif // RENDERER_VIEWPORT_H |
317 | |