renderer_viewport.h 12 KB

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