renderer_viewport.h 12 KB

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