renderer_viewport.cpp 52 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416
  1. /**************************************************************************/
  2. /* renderer_viewport.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. #include "renderer_viewport.h"
  31. #include "core/config/project_settings.h"
  32. #include "core/object/worker_thread_pool.h"
  33. #include "renderer_canvas_cull.h"
  34. #include "renderer_scene_cull.h"
  35. #include "rendering_server_globals.h"
  36. #include "storage/texture_storage.h"
  37. static Transform2D _canvas_get_transform(RendererViewport::Viewport *p_viewport, RendererCanvasCull::Canvas *p_canvas, RendererViewport::Viewport::CanvasData *p_canvas_data, const Vector2 &p_vp_size) {
  38. Transform2D xf = p_viewport->global_transform;
  39. float scale = 1.0;
  40. if (p_viewport->canvas_map.has(p_canvas->parent)) {
  41. Transform2D c_xform = p_viewport->canvas_map[p_canvas->parent].transform;
  42. if (p_viewport->snap_2d_transforms_to_pixel) {
  43. c_xform.columns[2] = c_xform.columns[2].floor();
  44. }
  45. xf = xf * c_xform;
  46. scale = p_canvas->parent_scale;
  47. }
  48. Transform2D c_xform = p_canvas_data->transform;
  49. if (p_viewport->snap_2d_transforms_to_pixel) {
  50. c_xform.columns[2] = c_xform.columns[2].floor();
  51. }
  52. xf = xf * c_xform;
  53. if (scale != 1.0 && !RSG::canvas->disable_scale) {
  54. Vector2 pivot = p_vp_size * 0.5;
  55. Transform2D xfpivot;
  56. xfpivot.set_origin(pivot);
  57. Transform2D xfscale;
  58. xfscale.scale(Vector2(scale, scale));
  59. xf = xfpivot.affine_inverse() * xf;
  60. xf = xfscale * xf;
  61. xf = xfpivot * xf;
  62. }
  63. return xf;
  64. }
  65. Vector<RendererViewport::Viewport *> RendererViewport::_sort_active_viewports() {
  66. // We need to sort the viewports in a "topological order", children first and
  67. // parents last. We also need to keep sibling viewports in the original order
  68. // from top to bottom.
  69. Vector<Viewport *> result;
  70. List<Viewport *> nodes;
  71. for (int i = active_viewports.size() - 1; i >= 0; --i) {
  72. Viewport *viewport = active_viewports[i];
  73. if (viewport->parent.is_valid()) {
  74. continue;
  75. }
  76. nodes.push_back(viewport);
  77. result.insert(0, viewport);
  78. }
  79. while (!nodes.is_empty()) {
  80. const Viewport *node = nodes[0];
  81. nodes.pop_front();
  82. for (int i = active_viewports.size() - 1; i >= 0; --i) {
  83. Viewport *child = active_viewports[i];
  84. if (child->parent != node->self) {
  85. continue;
  86. }
  87. if (!nodes.find(child)) {
  88. nodes.push_back(child);
  89. result.insert(0, child);
  90. }
  91. }
  92. }
  93. return result;
  94. }
  95. void RendererViewport::_configure_3d_render_buffers(Viewport *p_viewport) {
  96. if (p_viewport->render_buffers.is_valid()) {
  97. if (p_viewport->size.width == 0 || p_viewport->size.height == 0) {
  98. p_viewport->render_buffers.unref();
  99. } else {
  100. float scaling_3d_scale = p_viewport->scaling_3d_scale;
  101. RS::ViewportScaling3DMode scaling_3d_mode = p_viewport->scaling_3d_mode;
  102. if ((scaling_3d_mode == RS::VIEWPORT_SCALING_3D_MODE_FSR) && (scaling_3d_scale > 1.0)) {
  103. // FSR is not designed for downsampling.
  104. // Fall back to bilinear scaling.
  105. scaling_3d_mode = RS::VIEWPORT_SCALING_3D_MODE_BILINEAR;
  106. }
  107. if ((scaling_3d_mode == RS::VIEWPORT_SCALING_3D_MODE_FSR) && !p_viewport->fsr_enabled) {
  108. // FSR is not actually available.
  109. // Fall back to bilinear scaling.
  110. WARN_PRINT_ONCE("FSR 1.0 3D resolution scaling is not available. Falling back to bilinear 3D resolution scaling.");
  111. scaling_3d_mode = RS::VIEWPORT_SCALING_3D_MODE_BILINEAR;
  112. }
  113. if (scaling_3d_scale == 1.0) {
  114. scaling_3d_mode = RS::VIEWPORT_SCALING_3D_MODE_OFF;
  115. }
  116. int width;
  117. int height;
  118. int render_width;
  119. int render_height;
  120. switch (scaling_3d_mode) {
  121. case RS::VIEWPORT_SCALING_3D_MODE_BILINEAR:
  122. // Clamp 3D rendering resolution to reasonable values supported on most hardware.
  123. // This prevents freezing the engine or outright crashing on lower-end GPUs.
  124. width = CLAMP(p_viewport->size.width * scaling_3d_scale, 1, 16384);
  125. height = CLAMP(p_viewport->size.height * scaling_3d_scale, 1, 16384);
  126. render_width = width;
  127. render_height = height;
  128. break;
  129. case RS::VIEWPORT_SCALING_3D_MODE_FSR:
  130. width = p_viewport->size.width;
  131. height = p_viewport->size.height;
  132. render_width = MAX(width * scaling_3d_scale, 1.0); // width / (width * scaling)
  133. render_height = MAX(height * scaling_3d_scale, 1.0);
  134. break;
  135. case RS::VIEWPORT_SCALING_3D_MODE_OFF:
  136. width = p_viewport->size.width;
  137. height = p_viewport->size.height;
  138. render_width = width;
  139. render_height = height;
  140. break;
  141. default:
  142. // This is an unknown mode.
  143. WARN_PRINT_ONCE(vformat("Unknown scaling mode: %d. Disabling 3D resolution scaling.", scaling_3d_mode));
  144. scaling_3d_mode = RS::VIEWPORT_SCALING_3D_MODE_OFF;
  145. scaling_3d_scale = 1.0;
  146. width = p_viewport->size.width;
  147. height = p_viewport->size.height;
  148. render_width = width;
  149. render_height = height;
  150. break;
  151. }
  152. p_viewport->internal_size = Size2(render_width, render_height);
  153. // At resolution scales lower than 1.0, use negative texture mipmap bias
  154. // to compensate for the loss of sharpness.
  155. const float texture_mipmap_bias = log2f(MIN(scaling_3d_scale, 1.0)) + p_viewport->texture_mipmap_bias;
  156. p_viewport->render_buffers->configure(p_viewport->render_target, Size2i(render_width, render_height), Size2(width, height), scaling_3d_mode, p_viewport->fsr_sharpness, texture_mipmap_bias, p_viewport->msaa_3d, p_viewport->screen_space_aa, p_viewport->use_taa, p_viewport->use_debanding, p_viewport->view_count);
  157. }
  158. }
  159. }
  160. void RendererViewport::_draw_3d(Viewport *p_viewport) {
  161. RENDER_TIMESTAMP("> Render 3D Scene");
  162. Ref<XRInterface> xr_interface;
  163. if (p_viewport->use_xr && XRServer::get_singleton() != nullptr) {
  164. xr_interface = XRServer::get_singleton()->get_primary_interface();
  165. }
  166. if (p_viewport->use_occlusion_culling) {
  167. if (p_viewport->occlusion_buffer_dirty) {
  168. float aspect = p_viewport->size.aspect();
  169. int max_size = occlusion_rays_per_thread * WorkerThreadPool::get_singleton()->get_thread_count();
  170. int viewport_size = p_viewport->size.width * p_viewport->size.height;
  171. max_size = CLAMP(max_size, viewport_size / (32 * 32), viewport_size / (2 * 2)); // At least one depth pixel for every 16x16 region. At most one depth pixel for every 2x2 region.
  172. float height = Math::sqrt(max_size / aspect);
  173. Size2i new_size = Size2i(height * aspect, height);
  174. RendererSceneOcclusionCull::get_singleton()->buffer_set_size(p_viewport->self, new_size);
  175. p_viewport->occlusion_buffer_dirty = false;
  176. }
  177. }
  178. float screen_mesh_lod_threshold = p_viewport->mesh_lod_threshold / float(p_viewport->size.width);
  179. RSG::scene->render_camera(p_viewport->render_buffers, p_viewport->camera, p_viewport->scenario, p_viewport->self, p_viewport->internal_size, p_viewport->use_taa, screen_mesh_lod_threshold, p_viewport->shadow_atlas, xr_interface, &p_viewport->render_info);
  180. RENDER_TIMESTAMP("< Render 3D Scene");
  181. }
  182. void RendererViewport::_draw_viewport(Viewport *p_viewport) {
  183. if (p_viewport->measure_render_time) {
  184. String rt_id = "vp_begin_" + itos(p_viewport->self.get_id());
  185. RSG::utilities->capture_timestamp(rt_id);
  186. timestamp_vp_map[rt_id] = p_viewport->self;
  187. }
  188. if (OS::get_singleton()->get_current_rendering_method() == "gl_compatibility") {
  189. // This is currently needed for GLES to keep the current window being rendered to up to date
  190. DisplayServer::get_singleton()->gl_window_make_current(p_viewport->viewport_to_screen);
  191. }
  192. /* Camera should always be BEFORE any other 3D */
  193. bool scenario_draw_canvas_bg = false; //draw canvas, or some layer of it, as BG for 3D instead of in front
  194. int scenario_canvas_max_layer = 0;
  195. bool force_clear_render_target = false;
  196. for (int i = 0; i < RS::VIEWPORT_RENDER_INFO_TYPE_MAX; i++) {
  197. for (int j = 0; j < RS::VIEWPORT_RENDER_INFO_MAX; j++) {
  198. p_viewport->render_info.info[i][j] = 0;
  199. }
  200. }
  201. if (RSG::scene->is_scenario(p_viewport->scenario)) {
  202. RID environment = RSG::scene->scenario_get_environment(p_viewport->scenario);
  203. if (RSG::scene->is_environment(environment)) {
  204. if (!p_viewport->disable_2d && !viewport_is_environment_disabled(p_viewport)) {
  205. scenario_draw_canvas_bg = RSG::scene->environment_get_background(environment) == RS::ENV_BG_CANVAS;
  206. scenario_canvas_max_layer = RSG::scene->environment_get_canvas_max_layer(environment);
  207. } else if (RSG::scene->environment_get_background(environment) == RS::ENV_BG_CANVAS) {
  208. // The scene renderer will still copy over the last frame, so we need to clear the render target.
  209. force_clear_render_target = true;
  210. }
  211. }
  212. }
  213. bool can_draw_3d = RSG::scene->is_camera(p_viewport->camera) && !p_viewport->disable_3d;
  214. if ((scenario_draw_canvas_bg || can_draw_3d) && !p_viewport->render_buffers.is_valid()) {
  215. //wants to draw 3D but there is no render buffer, create
  216. p_viewport->render_buffers = RSG::scene->render_buffers_create();
  217. _configure_3d_render_buffers(p_viewport);
  218. }
  219. Color bgcolor = p_viewport->transparent_bg ? Color(0, 0, 0, 0) : RSG::texture_storage->get_default_clear_color();
  220. if (p_viewport->clear_mode != RS::VIEWPORT_CLEAR_NEVER) {
  221. RSG::texture_storage->render_target_request_clear(p_viewport->render_target, bgcolor);
  222. if (p_viewport->clear_mode == RS::VIEWPORT_CLEAR_ONLY_NEXT_FRAME) {
  223. p_viewport->clear_mode = RS::VIEWPORT_CLEAR_NEVER;
  224. }
  225. }
  226. if (!scenario_draw_canvas_bg && can_draw_3d) {
  227. if (force_clear_render_target) {
  228. RSG::texture_storage->render_target_do_clear_request(p_viewport->render_target);
  229. }
  230. _draw_3d(p_viewport);
  231. }
  232. if (!p_viewport->disable_2d) {
  233. RBMap<Viewport::CanvasKey, Viewport::CanvasData *> canvas_map;
  234. Rect2 clip_rect(0, 0, p_viewport->size.x, p_viewport->size.y);
  235. RendererCanvasRender::Light *lights = nullptr;
  236. RendererCanvasRender::Light *lights_with_shadow = nullptr;
  237. RendererCanvasRender::Light *directional_lights = nullptr;
  238. RendererCanvasRender::Light *directional_lights_with_shadow = nullptr;
  239. if (p_viewport->sdf_active) {
  240. // Process SDF.
  241. Rect2 sdf_rect = RSG::texture_storage->render_target_get_sdf_rect(p_viewport->render_target);
  242. RendererCanvasRender::LightOccluderInstance *occluders = nullptr;
  243. // Make list of occluders.
  244. for (KeyValue<RID, Viewport::CanvasData> &E : p_viewport->canvas_map) {
  245. RendererCanvasCull::Canvas *canvas = static_cast<RendererCanvasCull::Canvas *>(E.value.canvas);
  246. Transform2D xf = _canvas_get_transform(p_viewport, canvas, &E.value, clip_rect.size);
  247. for (RendererCanvasRender::LightOccluderInstance *F : canvas->occluders) {
  248. if (!F->enabled) {
  249. continue;
  250. }
  251. F->xform_cache = xf * F->xform;
  252. if (sdf_rect.intersects_transformed(F->xform_cache, F->aabb_cache)) {
  253. F->next = occluders;
  254. occluders = F;
  255. }
  256. }
  257. }
  258. RSG::canvas_render->render_sdf(p_viewport->render_target, occluders);
  259. RSG::texture_storage->render_target_mark_sdf_enabled(p_viewport->render_target, true);
  260. p_viewport->sdf_active = false; // If used, gets set active again.
  261. } else {
  262. RSG::texture_storage->render_target_mark_sdf_enabled(p_viewport->render_target, false);
  263. }
  264. Rect2 shadow_rect;
  265. int shadow_count = 0;
  266. int directional_light_count = 0;
  267. RENDER_TIMESTAMP("Cull 2D Lights");
  268. for (KeyValue<RID, Viewport::CanvasData> &E : p_viewport->canvas_map) {
  269. RendererCanvasCull::Canvas *canvas = static_cast<RendererCanvasCull::Canvas *>(E.value.canvas);
  270. Transform2D xf = _canvas_get_transform(p_viewport, canvas, &E.value, clip_rect.size);
  271. // Find lights in canvas.
  272. for (RendererCanvasRender::Light *F : canvas->lights) {
  273. RendererCanvasRender::Light *cl = F;
  274. if (cl->enabled && cl->texture.is_valid()) {
  275. //not super efficient..
  276. Size2 tsize = RSG::texture_storage->texture_size_with_proxy(cl->texture);
  277. tsize *= cl->scale;
  278. Vector2 offset = tsize / 2.0;
  279. cl->rect_cache = Rect2(-offset + cl->texture_offset, tsize);
  280. cl->xform_cache = xf * cl->xform;
  281. if (clip_rect.intersects_transformed(cl->xform_cache, cl->rect_cache)) {
  282. cl->filter_next_ptr = lights;
  283. lights = cl;
  284. Transform2D scale;
  285. scale.scale(cl->rect_cache.size);
  286. scale.columns[2] = cl->rect_cache.position;
  287. cl->light_shader_xform = xf * cl->xform * scale;
  288. if (cl->use_shadow) {
  289. cl->shadows_next_ptr = lights_with_shadow;
  290. if (lights_with_shadow == nullptr) {
  291. shadow_rect = cl->xform_cache.xform(cl->rect_cache);
  292. } else {
  293. shadow_rect = shadow_rect.merge(cl->xform_cache.xform(cl->rect_cache));
  294. }
  295. lights_with_shadow = cl;
  296. cl->radius_cache = cl->rect_cache.size.length();
  297. }
  298. }
  299. }
  300. }
  301. for (RendererCanvasRender::Light *F : canvas->directional_lights) {
  302. RendererCanvasRender::Light *cl = F;
  303. if (cl->enabled) {
  304. cl->filter_next_ptr = directional_lights;
  305. directional_lights = cl;
  306. cl->xform_cache = xf * cl->xform;
  307. cl->xform_cache.columns[2] = Vector2(); //translation is pointless
  308. if (cl->use_shadow) {
  309. cl->shadows_next_ptr = directional_lights_with_shadow;
  310. directional_lights_with_shadow = cl;
  311. }
  312. directional_light_count++;
  313. if (directional_light_count == RS::MAX_2D_DIRECTIONAL_LIGHTS) {
  314. break;
  315. }
  316. }
  317. }
  318. canvas_map[Viewport::CanvasKey(E.key, E.value.layer, E.value.sublayer)] = &E.value;
  319. }
  320. if (lights_with_shadow) {
  321. //update shadows if any
  322. RendererCanvasRender::LightOccluderInstance *occluders = nullptr;
  323. RENDER_TIMESTAMP("> Render PointLight2D Shadows");
  324. RENDER_TIMESTAMP("Cull LightOccluder2Ds");
  325. //make list of occluders
  326. for (KeyValue<RID, Viewport::CanvasData> &E : p_viewport->canvas_map) {
  327. RendererCanvasCull::Canvas *canvas = static_cast<RendererCanvasCull::Canvas *>(E.value.canvas);
  328. Transform2D xf = _canvas_get_transform(p_viewport, canvas, &E.value, clip_rect.size);
  329. for (RendererCanvasRender::LightOccluderInstance *F : canvas->occluders) {
  330. if (!F->enabled) {
  331. continue;
  332. }
  333. F->xform_cache = xf * F->xform;
  334. if (shadow_rect.intersects_transformed(F->xform_cache, F->aabb_cache)) {
  335. F->next = occluders;
  336. occluders = F;
  337. }
  338. }
  339. }
  340. //update the light shadowmaps with them
  341. RendererCanvasRender::Light *light = lights_with_shadow;
  342. while (light) {
  343. RENDER_TIMESTAMP("Render PointLight2D Shadow");
  344. RSG::canvas_render->light_update_shadow(light->light_internal, shadow_count++, light->xform_cache.affine_inverse(), light->item_shadow_mask, light->radius_cache / 1000.0, light->radius_cache * 1.1, occluders);
  345. light = light->shadows_next_ptr;
  346. }
  347. RENDER_TIMESTAMP("< Render PointLight2D Shadows");
  348. }
  349. if (directional_lights_with_shadow) {
  350. //update shadows if any
  351. RendererCanvasRender::Light *light = directional_lights_with_shadow;
  352. while (light) {
  353. Vector2 light_dir = -light->xform_cache.columns[1].normalized(); // Y is light direction
  354. float cull_distance = light->directional_distance;
  355. Vector2 light_dir_sign;
  356. light_dir_sign.x = (ABS(light_dir.x) < CMP_EPSILON) ? 0.0 : ((light_dir.x > 0.0) ? 1.0 : -1.0);
  357. light_dir_sign.y = (ABS(light_dir.y) < CMP_EPSILON) ? 0.0 : ((light_dir.y > 0.0) ? 1.0 : -1.0);
  358. Vector2 points[6];
  359. int point_count = 0;
  360. for (int j = 0; j < 4; j++) {
  361. static const Vector2 signs[4] = { Vector2(1, 1), Vector2(1, 0), Vector2(0, 0), Vector2(0, 1) };
  362. Vector2 sign_cmp = signs[j] * 2.0 - Vector2(1.0, 1.0);
  363. Vector2 point = clip_rect.position + clip_rect.size * signs[j];
  364. if (sign_cmp == light_dir_sign) {
  365. //both point in same direction, plot offsetted
  366. points[point_count++] = point + light_dir * cull_distance;
  367. } else if (sign_cmp.x == light_dir_sign.x || sign_cmp.y == light_dir_sign.y) {
  368. int next_j = (j + 1) % 4;
  369. Vector2 next_sign_cmp = signs[next_j] * 2.0 - Vector2(1.0, 1.0);
  370. //one point in the same direction, plot segment
  371. if (next_sign_cmp.x == light_dir_sign.x || next_sign_cmp.y == light_dir_sign.y) {
  372. if (light_dir_sign.x != 0.0 || light_dir_sign.y != 0.0) {
  373. points[point_count++] = point;
  374. }
  375. points[point_count++] = point + light_dir * cull_distance;
  376. } else {
  377. points[point_count++] = point + light_dir * cull_distance;
  378. if (light_dir_sign.x != 0.0 || light_dir_sign.y != 0.0) {
  379. points[point_count++] = point;
  380. }
  381. }
  382. } else {
  383. //plot normally
  384. points[point_count++] = point;
  385. }
  386. }
  387. Vector2 xf_points[6];
  388. RendererCanvasRender::LightOccluderInstance *occluders = nullptr;
  389. RENDER_TIMESTAMP("> Render DirectionalLight2D Shadows");
  390. // Make list of occluders.
  391. for (KeyValue<RID, Viewport::CanvasData> &E : p_viewport->canvas_map) {
  392. RendererCanvasCull::Canvas *canvas = static_cast<RendererCanvasCull::Canvas *>(E.value.canvas);
  393. Transform2D xf = _canvas_get_transform(p_viewport, canvas, &E.value, clip_rect.size);
  394. for (RendererCanvasRender::LightOccluderInstance *F : canvas->occluders) {
  395. if (!F->enabled) {
  396. continue;
  397. }
  398. F->xform_cache = xf * F->xform;
  399. Transform2D localizer = F->xform_cache.affine_inverse();
  400. for (int j = 0; j < point_count; j++) {
  401. xf_points[j] = localizer.xform(points[j]);
  402. }
  403. if (F->aabb_cache.intersects_filled_polygon(xf_points, point_count)) {
  404. F->next = occluders;
  405. occluders = F;
  406. }
  407. }
  408. }
  409. RSG::canvas_render->light_update_directional_shadow(light->light_internal, shadow_count++, light->xform_cache, light->item_shadow_mask, cull_distance, clip_rect, occluders);
  410. light = light->shadows_next_ptr;
  411. }
  412. RENDER_TIMESTAMP("< Render DirectionalLight2D Shadows");
  413. }
  414. if (scenario_draw_canvas_bg && canvas_map.begin() && canvas_map.begin()->key.get_layer() > scenario_canvas_max_layer) {
  415. // There may be an outstanding clear request if a clear was requested, but no 2D elements were drawn.
  416. // Clear now otherwise we copy over garbage from the render target.
  417. RSG::texture_storage->render_target_do_clear_request(p_viewport->render_target);
  418. if (!can_draw_3d) {
  419. RSG::scene->render_empty_scene(p_viewport->render_buffers, p_viewport->scenario, p_viewport->shadow_atlas);
  420. } else {
  421. _draw_3d(p_viewport);
  422. }
  423. scenario_draw_canvas_bg = false;
  424. }
  425. for (const KeyValue<Viewport::CanvasKey, Viewport::CanvasData *> &E : canvas_map) {
  426. RendererCanvasCull::Canvas *canvas = static_cast<RendererCanvasCull::Canvas *>(E.value->canvas);
  427. Transform2D xform = _canvas_get_transform(p_viewport, canvas, E.value, clip_rect.size);
  428. RendererCanvasRender::Light *canvas_lights = nullptr;
  429. RendererCanvasRender::Light *canvas_directional_lights = nullptr;
  430. RendererCanvasRender::Light *ptr = lights;
  431. while (ptr) {
  432. if (E.value->layer >= ptr->layer_min && E.value->layer <= ptr->layer_max) {
  433. ptr->next_ptr = canvas_lights;
  434. canvas_lights = ptr;
  435. }
  436. ptr = ptr->filter_next_ptr;
  437. }
  438. ptr = directional_lights;
  439. while (ptr) {
  440. if (E.value->layer >= ptr->layer_min && E.value->layer <= ptr->layer_max) {
  441. ptr->next_ptr = canvas_directional_lights;
  442. canvas_directional_lights = ptr;
  443. }
  444. ptr = ptr->filter_next_ptr;
  445. }
  446. RSG::canvas->render_canvas(p_viewport->render_target, canvas, xform, canvas_lights, canvas_directional_lights, clip_rect, p_viewport->texture_filter, p_viewport->texture_repeat, p_viewport->snap_2d_transforms_to_pixel, p_viewport->snap_2d_vertices_to_pixel, p_viewport->canvas_cull_mask);
  447. if (RSG::canvas->was_sdf_used()) {
  448. p_viewport->sdf_active = true;
  449. }
  450. if (scenario_draw_canvas_bg && E.key.get_layer() >= scenario_canvas_max_layer) {
  451. // There may be an outstanding clear request if a clear was requested, but no 2D elements were drawn.
  452. // Clear now otherwise we copy over garbage from the render target.
  453. RSG::texture_storage->render_target_do_clear_request(p_viewport->render_target);
  454. if (!can_draw_3d) {
  455. RSG::scene->render_empty_scene(p_viewport->render_buffers, p_viewport->scenario, p_viewport->shadow_atlas);
  456. } else {
  457. _draw_3d(p_viewport);
  458. }
  459. scenario_draw_canvas_bg = false;
  460. }
  461. }
  462. if (scenario_draw_canvas_bg) {
  463. // There may be an outstanding clear request if a clear was requested, but no 2D elements were drawn.
  464. // Clear now otherwise we copy over garbage from the render target.
  465. RSG::texture_storage->render_target_do_clear_request(p_viewport->render_target);
  466. if (!can_draw_3d) {
  467. RSG::scene->render_empty_scene(p_viewport->render_buffers, p_viewport->scenario, p_viewport->shadow_atlas);
  468. } else {
  469. _draw_3d(p_viewport);
  470. }
  471. }
  472. }
  473. if (RSG::texture_storage->render_target_is_clear_requested(p_viewport->render_target)) {
  474. //was never cleared in the end, force clear it
  475. RSG::texture_storage->render_target_do_clear_request(p_viewport->render_target);
  476. }
  477. if (p_viewport->measure_render_time) {
  478. String rt_id = "vp_end_" + itos(p_viewport->self.get_id());
  479. RSG::utilities->capture_timestamp(rt_id);
  480. timestamp_vp_map[rt_id] = p_viewport->self;
  481. }
  482. }
  483. void RendererViewport::draw_viewports() {
  484. timestamp_vp_map.clear();
  485. // get our xr interface in case we need it
  486. Ref<XRInterface> xr_interface;
  487. XRServer *xr_server = XRServer::get_singleton();
  488. if (xr_server != nullptr) {
  489. // let our XR server know we're about to render our frames so we can get our frame timing
  490. xr_server->pre_render();
  491. // retrieve the interface responsible for rendering
  492. xr_interface = xr_server->get_primary_interface();
  493. }
  494. if (Engine::get_singleton()->is_editor_hint()) {
  495. set_default_clear_color(GLOBAL_GET("rendering/environment/defaults/default_clear_color"));
  496. }
  497. if (sorted_active_viewports_dirty) {
  498. sorted_active_viewports = _sort_active_viewports();
  499. sorted_active_viewports_dirty = false;
  500. }
  501. HashMap<DisplayServer::WindowID, Vector<BlitToScreen>> blit_to_screen_list;
  502. //draw viewports
  503. RENDER_TIMESTAMP("> Render Viewports");
  504. //determine what is visible
  505. draw_viewports_pass++;
  506. for (int i = sorted_active_viewports.size() - 1; i >= 0; i--) { //to compute parent dependency, must go in reverse draw order
  507. Viewport *vp = sorted_active_viewports[i];
  508. if (vp->update_mode == RS::VIEWPORT_UPDATE_DISABLED) {
  509. continue;
  510. }
  511. if (!vp->render_target.is_valid()) {
  512. continue;
  513. }
  514. //ERR_CONTINUE(!vp->render_target.is_valid());
  515. bool visible = vp->viewport_to_screen_rect != Rect2();
  516. if (vp->use_xr) {
  517. if (xr_interface.is_valid()) {
  518. // Ignore update mode we have to commit frames to our XR interface
  519. visible = true;
  520. // Override our size, make sure it matches our required size and is created as a stereo target
  521. Size2 xr_size = xr_interface->get_render_target_size();
  522. _viewport_set_size(vp, xr_size.width, xr_size.height, xr_interface->get_view_count());
  523. } else {
  524. // don't render anything
  525. visible = false;
  526. vp->size = Size2();
  527. }
  528. } else {
  529. if (vp->update_mode == RS::VIEWPORT_UPDATE_ALWAYS || vp->update_mode == RS::VIEWPORT_UPDATE_ONCE) {
  530. visible = true;
  531. }
  532. if (vp->update_mode == RS::VIEWPORT_UPDATE_WHEN_VISIBLE && RSG::texture_storage->render_target_was_used(vp->render_target)) {
  533. visible = true;
  534. }
  535. if (vp->update_mode == RS::VIEWPORT_UPDATE_WHEN_PARENT_VISIBLE) {
  536. Viewport *parent = viewport_owner.get_or_null(vp->parent);
  537. if (parent && parent->last_pass == draw_viewports_pass) {
  538. visible = true;
  539. }
  540. }
  541. }
  542. visible = visible && vp->size.x > 1 && vp->size.y > 1;
  543. if (visible) {
  544. vp->last_pass = draw_viewports_pass;
  545. }
  546. }
  547. int vertices_drawn = 0;
  548. int objects_drawn = 0;
  549. int draw_calls_used = 0;
  550. for (int i = 0; i < sorted_active_viewports.size(); i++) {
  551. Viewport *vp = sorted_active_viewports[i];
  552. if (vp->last_pass != draw_viewports_pass) {
  553. continue; //should not draw
  554. }
  555. RENDER_TIMESTAMP("> Render Viewport " + itos(i));
  556. RSG::texture_storage->render_target_set_as_unused(vp->render_target);
  557. if (vp->use_xr && xr_interface.is_valid()) {
  558. // Inform XR interface we're about to render its viewport,
  559. // if this returns false we don't render.
  560. // This usually is a result of the player taking off their headset and OpenXR telling us to skip
  561. // rendering frames.
  562. if (xr_interface->pre_draw_viewport(vp->render_target)) {
  563. RSG::texture_storage->render_target_set_override(vp->render_target,
  564. xr_interface->get_color_texture(),
  565. xr_interface->get_depth_texture(),
  566. xr_interface->get_velocity_texture());
  567. // render...
  568. RSG::scene->set_debug_draw_mode(vp->debug_draw);
  569. // and draw viewport
  570. _draw_viewport(vp);
  571. // commit our eyes
  572. Vector<BlitToScreen> blits = xr_interface->post_draw_viewport(vp->render_target, vp->viewport_to_screen_rect);
  573. if (vp->viewport_to_screen != DisplayServer::INVALID_WINDOW_ID) {
  574. if (OS::get_singleton()->get_current_rendering_driver_name() == "opengl3") {
  575. if (blits.size() > 0) {
  576. RSG::rasterizer->blit_render_targets_to_screen(vp->viewport_to_screen, blits.ptr(), blits.size());
  577. }
  578. RSG::rasterizer->end_frame(true);
  579. } else if (blits.size() > 0) {
  580. if (!blit_to_screen_list.has(vp->viewport_to_screen)) {
  581. blit_to_screen_list[vp->viewport_to_screen] = Vector<BlitToScreen>();
  582. }
  583. for (int b = 0; b < blits.size(); b++) {
  584. blit_to_screen_list[vp->viewport_to_screen].push_back(blits[b]);
  585. }
  586. }
  587. }
  588. }
  589. } else {
  590. RSG::texture_storage->render_target_set_override(vp->render_target, RID(), RID(), RID());
  591. RSG::scene->set_debug_draw_mode(vp->debug_draw);
  592. // render standard mono camera
  593. _draw_viewport(vp);
  594. if (vp->viewport_to_screen != DisplayServer::INVALID_WINDOW_ID && (!vp->viewport_render_direct_to_screen || !RSG::rasterizer->is_low_end())) {
  595. //copy to screen if set as such
  596. BlitToScreen blit;
  597. blit.render_target = vp->render_target;
  598. if (vp->viewport_to_screen_rect != Rect2()) {
  599. blit.dst_rect = vp->viewport_to_screen_rect;
  600. } else {
  601. blit.dst_rect.position = Vector2();
  602. blit.dst_rect.size = vp->size;
  603. }
  604. if (!blit_to_screen_list.has(vp->viewport_to_screen)) {
  605. blit_to_screen_list[vp->viewport_to_screen] = Vector<BlitToScreen>();
  606. }
  607. if (OS::get_singleton()->get_current_rendering_driver_name() == "opengl3") {
  608. Vector<BlitToScreen> blit_to_screen_vec;
  609. blit_to_screen_vec.push_back(blit);
  610. RSG::rasterizer->blit_render_targets_to_screen(vp->viewport_to_screen, blit_to_screen_vec.ptr(), 1);
  611. RSG::rasterizer->end_frame(true);
  612. } else {
  613. blit_to_screen_list[vp->viewport_to_screen].push_back(blit);
  614. }
  615. }
  616. }
  617. if (vp->update_mode == RS::VIEWPORT_UPDATE_ONCE) {
  618. vp->update_mode = RS::VIEWPORT_UPDATE_DISABLED;
  619. }
  620. RENDER_TIMESTAMP("< Render Viewport " + itos(i));
  621. objects_drawn += vp->render_info.info[RS::VIEWPORT_RENDER_INFO_TYPE_VISIBLE][RS::VIEWPORT_RENDER_INFO_OBJECTS_IN_FRAME] + vp->render_info.info[RS::VIEWPORT_RENDER_INFO_TYPE_SHADOW][RS::VIEWPORT_RENDER_INFO_OBJECTS_IN_FRAME];
  622. vertices_drawn += vp->render_info.info[RS::VIEWPORT_RENDER_INFO_TYPE_VISIBLE][RS::VIEWPORT_RENDER_INFO_PRIMITIVES_IN_FRAME] + vp->render_info.info[RS::VIEWPORT_RENDER_INFO_TYPE_SHADOW][RS::VIEWPORT_RENDER_INFO_PRIMITIVES_IN_FRAME];
  623. draw_calls_used += vp->render_info.info[RS::VIEWPORT_RENDER_INFO_TYPE_VISIBLE][RS::VIEWPORT_RENDER_INFO_DRAW_CALLS_IN_FRAME] + vp->render_info.info[RS::VIEWPORT_RENDER_INFO_TYPE_SHADOW][RS::VIEWPORT_RENDER_INFO_DRAW_CALLS_IN_FRAME];
  624. }
  625. RSG::scene->set_debug_draw_mode(RS::VIEWPORT_DEBUG_DRAW_DISABLED);
  626. total_objects_drawn = objects_drawn;
  627. total_vertices_drawn = vertices_drawn;
  628. total_draw_calls_used = draw_calls_used;
  629. RENDER_TIMESTAMP("< Render Viewports");
  630. //this needs to be called to make screen swapping more efficient
  631. RSG::rasterizer->prepare_for_blitting_render_targets();
  632. for (const KeyValue<int, Vector<BlitToScreen>> &E : blit_to_screen_list) {
  633. RSG::rasterizer->blit_render_targets_to_screen(E.key, E.value.ptr(), E.value.size());
  634. }
  635. }
  636. RID RendererViewport::viewport_allocate() {
  637. return viewport_owner.allocate_rid();
  638. }
  639. void RendererViewport::viewport_initialize(RID p_rid) {
  640. viewport_owner.initialize_rid(p_rid);
  641. Viewport *viewport = viewport_owner.get_or_null(p_rid);
  642. viewport->self = p_rid;
  643. viewport->render_target = RSG::texture_storage->render_target_create();
  644. viewport->shadow_atlas = RSG::light_storage->shadow_atlas_create();
  645. viewport->viewport_render_direct_to_screen = false;
  646. viewport->fsr_enabled = !RSG::rasterizer->is_low_end() && !viewport->disable_3d;
  647. }
  648. void RendererViewport::viewport_set_use_xr(RID p_viewport, bool p_use_xr) {
  649. Viewport *viewport = viewport_owner.get_or_null(p_viewport);
  650. ERR_FAIL_COND(!viewport);
  651. if (viewport->use_xr == p_use_xr) {
  652. return;
  653. }
  654. viewport->use_xr = p_use_xr;
  655. // Re-configure the 3D render buffers when disabling XR. They'll get
  656. // re-configured when enabling XR in draw_viewports().
  657. if (!p_use_xr) {
  658. viewport->view_count = 1;
  659. _configure_3d_render_buffers(viewport);
  660. }
  661. }
  662. void RendererViewport::viewport_set_scaling_3d_mode(RID p_viewport, RS::ViewportScaling3DMode p_mode) {
  663. Viewport *viewport = viewport_owner.get_or_null(p_viewport);
  664. ERR_FAIL_COND(!viewport);
  665. viewport->scaling_3d_mode = p_mode;
  666. _configure_3d_render_buffers(viewport);
  667. }
  668. void RendererViewport::viewport_set_fsr_sharpness(RID p_viewport, float p_sharpness) {
  669. Viewport *viewport = viewport_owner.get_or_null(p_viewport);
  670. ERR_FAIL_COND(!viewport);
  671. viewport->fsr_sharpness = p_sharpness;
  672. if (viewport->render_buffers.is_valid()) {
  673. viewport->render_buffers->set_fsr_sharpness(p_sharpness);
  674. }
  675. }
  676. void RendererViewport::viewport_set_texture_mipmap_bias(RID p_viewport, float p_mipmap_bias) {
  677. Viewport *viewport = viewport_owner.get_or_null(p_viewport);
  678. ERR_FAIL_COND(!viewport);
  679. viewport->texture_mipmap_bias = p_mipmap_bias;
  680. if (viewport->render_buffers.is_valid()) {
  681. viewport->render_buffers->set_texture_mipmap_bias(p_mipmap_bias);
  682. }
  683. }
  684. void RendererViewport::viewport_set_scaling_3d_scale(RID p_viewport, float p_scaling_3d_scale) {
  685. Viewport *viewport = viewport_owner.get_or_null(p_viewport);
  686. ERR_FAIL_COND(!viewport);
  687. // Clamp to reasonable values that are actually useful.
  688. // Values above 2.0 don't serve a practical purpose since the viewport
  689. // isn't displayed with mipmaps.
  690. if (viewport->scaling_3d_scale == CLAMP(p_scaling_3d_scale, 0.1, 2.0)) {
  691. return;
  692. }
  693. viewport->scaling_3d_scale = CLAMP(p_scaling_3d_scale, 0.1, 2.0);
  694. _configure_3d_render_buffers(viewport);
  695. }
  696. void RendererViewport::viewport_set_size(RID p_viewport, int p_width, int p_height) {
  697. ERR_FAIL_COND(p_width < 0 || p_height < 0);
  698. Viewport *viewport = viewport_owner.get_or_null(p_viewport);
  699. ERR_FAIL_COND(!viewport);
  700. ERR_FAIL_COND_MSG(viewport->use_xr, "Cannot set viewport size when using XR");
  701. _viewport_set_size(viewport, p_width, p_height, 1);
  702. }
  703. void RendererViewport::_viewport_set_size(Viewport *p_viewport, int p_width, int p_height, uint32_t p_view_count) {
  704. Size2i new_size(p_width, p_height);
  705. if (p_viewport->size != new_size || p_viewport->view_count != p_view_count) {
  706. p_viewport->size = new_size;
  707. p_viewport->view_count = p_view_count;
  708. RSG::texture_storage->render_target_set_size(p_viewport->render_target, p_width, p_height, p_view_count);
  709. _configure_3d_render_buffers(p_viewport);
  710. p_viewport->occlusion_buffer_dirty = true;
  711. }
  712. }
  713. void RendererViewport::viewport_set_active(RID p_viewport, bool p_active) {
  714. Viewport *viewport = viewport_owner.get_or_null(p_viewport);
  715. ERR_FAIL_COND(!viewport);
  716. if (p_active) {
  717. ERR_FAIL_COND_MSG(active_viewports.has(viewport), "Can't make active a Viewport that is already active.");
  718. viewport->occlusion_buffer_dirty = true;
  719. active_viewports.push_back(viewport);
  720. } else {
  721. active_viewports.erase(viewport);
  722. }
  723. sorted_active_viewports_dirty = true;
  724. }
  725. void RendererViewport::viewport_set_parent_viewport(RID p_viewport, RID p_parent_viewport) {
  726. Viewport *viewport = viewport_owner.get_or_null(p_viewport);
  727. ERR_FAIL_COND(!viewport);
  728. viewport->parent = p_parent_viewport;
  729. }
  730. void RendererViewport::viewport_set_clear_mode(RID p_viewport, RS::ViewportClearMode p_clear_mode) {
  731. Viewport *viewport = viewport_owner.get_or_null(p_viewport);
  732. ERR_FAIL_COND(!viewport);
  733. viewport->clear_mode = p_clear_mode;
  734. }
  735. void RendererViewport::viewport_attach_to_screen(RID p_viewport, const Rect2 &p_rect, DisplayServer::WindowID p_screen) {
  736. Viewport *viewport = viewport_owner.get_or_null(p_viewport);
  737. ERR_FAIL_COND(!viewport);
  738. if (p_screen != DisplayServer::INVALID_WINDOW_ID) {
  739. // If using OpenGL we can optimize this operation by rendering directly to system_fbo
  740. // instead of rendering to fbo and copying to system_fbo after
  741. if (RSG::rasterizer->is_low_end() && viewport->viewport_render_direct_to_screen) {
  742. RSG::texture_storage->render_target_set_size(viewport->render_target, p_rect.size.x, p_rect.size.y, viewport->view_count);
  743. RSG::texture_storage->render_target_set_position(viewport->render_target, p_rect.position.x, p_rect.position.y);
  744. }
  745. viewport->viewport_to_screen_rect = p_rect;
  746. viewport->viewport_to_screen = p_screen;
  747. } else {
  748. // if render_direct_to_screen was used, reset size and position
  749. if (RSG::rasterizer->is_low_end() && viewport->viewport_render_direct_to_screen) {
  750. RSG::texture_storage->render_target_set_position(viewport->render_target, 0, 0);
  751. RSG::texture_storage->render_target_set_size(viewport->render_target, viewport->size.x, viewport->size.y, viewport->view_count);
  752. }
  753. viewport->viewport_to_screen_rect = Rect2();
  754. viewport->viewport_to_screen = DisplayServer::INVALID_WINDOW_ID;
  755. }
  756. }
  757. void RendererViewport::viewport_set_render_direct_to_screen(RID p_viewport, bool p_enable) {
  758. Viewport *viewport = viewport_owner.get_or_null(p_viewport);
  759. ERR_FAIL_COND(!viewport);
  760. if (p_enable == viewport->viewport_render_direct_to_screen) {
  761. return;
  762. }
  763. // if disabled, reset render_target size and position
  764. if (!p_enable) {
  765. RSG::texture_storage->render_target_set_position(viewport->render_target, 0, 0);
  766. RSG::texture_storage->render_target_set_size(viewport->render_target, viewport->size.x, viewport->size.y, viewport->view_count);
  767. }
  768. RSG::texture_storage->render_target_set_direct_to_screen(viewport->render_target, p_enable);
  769. viewport->viewport_render_direct_to_screen = p_enable;
  770. // if attached to screen already, setup screen size and position, this needs to happen after setting flag to avoid an unnecessary buffer allocation
  771. if (RSG::rasterizer->is_low_end() && viewport->viewport_to_screen_rect != Rect2() && p_enable) {
  772. RSG::texture_storage->render_target_set_size(viewport->render_target, viewport->viewport_to_screen_rect.size.x, viewport->viewport_to_screen_rect.size.y, viewport->view_count);
  773. RSG::texture_storage->render_target_set_position(viewport->render_target, viewport->viewport_to_screen_rect.position.x, viewport->viewport_to_screen_rect.position.y);
  774. }
  775. }
  776. void RendererViewport::viewport_set_update_mode(RID p_viewport, RS::ViewportUpdateMode p_mode) {
  777. Viewport *viewport = viewport_owner.get_or_null(p_viewport);
  778. ERR_FAIL_COND(!viewport);
  779. viewport->update_mode = p_mode;
  780. }
  781. RID RendererViewport::viewport_get_render_target(RID p_viewport) const {
  782. const Viewport *viewport = viewport_owner.get_or_null(p_viewport);
  783. ERR_FAIL_COND_V(!viewport, RID());
  784. return viewport->render_target;
  785. }
  786. RID RendererViewport::viewport_get_texture(RID p_viewport) const {
  787. const Viewport *viewport = viewport_owner.get_or_null(p_viewport);
  788. ERR_FAIL_COND_V(!viewport, RID());
  789. return RSG::texture_storage->render_target_get_texture(viewport->render_target);
  790. }
  791. RID RendererViewport::viewport_get_occluder_debug_texture(RID p_viewport) const {
  792. const Viewport *viewport = viewport_owner.get_or_null(p_viewport);
  793. ERR_FAIL_COND_V(!viewport, RID());
  794. if (viewport->use_occlusion_culling && viewport->debug_draw == RenderingServer::VIEWPORT_DEBUG_DRAW_OCCLUDERS) {
  795. return RendererSceneOcclusionCull::get_singleton()->buffer_get_debug_texture(p_viewport);
  796. }
  797. return RID();
  798. }
  799. void RendererViewport::viewport_set_prev_camera_data(RID p_viewport, const RendererSceneRender::CameraData *p_camera_data) {
  800. Viewport *viewport = viewport_owner.get_or_null(p_viewport);
  801. ERR_FAIL_COND(!viewport);
  802. uint64_t frame = RSG::rasterizer->get_frame_number();
  803. if (viewport->prev_camera_data_frame != frame) {
  804. viewport->prev_camera_data = *p_camera_data;
  805. viewport->prev_camera_data_frame = frame;
  806. }
  807. }
  808. const RendererSceneRender::CameraData *RendererViewport::viewport_get_prev_camera_data(RID p_viewport) {
  809. const Viewport *viewport = viewport_owner.get_or_null(p_viewport);
  810. ERR_FAIL_COND_V(!viewport, nullptr);
  811. return &viewport->prev_camera_data;
  812. }
  813. void RendererViewport::viewport_set_disable_2d(RID p_viewport, bool p_disable) {
  814. Viewport *viewport = viewport_owner.get_or_null(p_viewport);
  815. ERR_FAIL_COND(!viewport);
  816. viewport->disable_2d = p_disable;
  817. }
  818. void RendererViewport::viewport_set_environment_mode(RID p_viewport, RS::ViewportEnvironmentMode p_mode) {
  819. Viewport *viewport = viewport_owner.get_or_null(p_viewport);
  820. ERR_FAIL_COND(!viewport);
  821. viewport->disable_environment = p_mode;
  822. }
  823. bool RendererViewport::viewport_is_environment_disabled(Viewport *viewport) {
  824. ERR_FAIL_COND_V(!viewport, false);
  825. if (viewport->parent.is_valid() && viewport->disable_environment == RS::VIEWPORT_ENVIRONMENT_INHERIT) {
  826. Viewport *parent = viewport_owner.get_or_null(viewport->parent);
  827. return viewport_is_environment_disabled(parent);
  828. }
  829. return viewport->disable_environment == RS::VIEWPORT_ENVIRONMENT_DISABLED;
  830. }
  831. void RendererViewport::viewport_set_disable_3d(RID p_viewport, bool p_disable) {
  832. Viewport *viewport = viewport_owner.get_or_null(p_viewport);
  833. ERR_FAIL_COND(!viewport);
  834. viewport->disable_3d = p_disable;
  835. }
  836. void RendererViewport::viewport_attach_camera(RID p_viewport, RID p_camera) {
  837. Viewport *viewport = viewport_owner.get_or_null(p_viewport);
  838. ERR_FAIL_COND(!viewport);
  839. viewport->camera = p_camera;
  840. }
  841. void RendererViewport::viewport_set_scenario(RID p_viewport, RID p_scenario) {
  842. Viewport *viewport = viewport_owner.get_or_null(p_viewport);
  843. ERR_FAIL_COND(!viewport);
  844. if (viewport->scenario.is_valid()) {
  845. RSG::scene->scenario_remove_viewport_visibility_mask(viewport->scenario, p_viewport);
  846. }
  847. viewport->scenario = p_scenario;
  848. if (viewport->use_occlusion_culling) {
  849. RendererSceneOcclusionCull::get_singleton()->buffer_set_scenario(p_viewport, p_scenario);
  850. }
  851. }
  852. void RendererViewport::viewport_attach_canvas(RID p_viewport, RID p_canvas) {
  853. Viewport *viewport = viewport_owner.get_or_null(p_viewport);
  854. ERR_FAIL_COND(!viewport);
  855. ERR_FAIL_COND(viewport->canvas_map.has(p_canvas));
  856. RendererCanvasCull::Canvas *canvas = RSG::canvas->canvas_owner.get_or_null(p_canvas);
  857. ERR_FAIL_COND(!canvas);
  858. canvas->viewports.insert(p_viewport);
  859. viewport->canvas_map[p_canvas] = Viewport::CanvasData();
  860. viewport->canvas_map[p_canvas].layer = 0;
  861. viewport->canvas_map[p_canvas].sublayer = 0;
  862. viewport->canvas_map[p_canvas].canvas = canvas;
  863. }
  864. void RendererViewport::viewport_remove_canvas(RID p_viewport, RID p_canvas) {
  865. Viewport *viewport = viewport_owner.get_or_null(p_viewport);
  866. ERR_FAIL_COND(!viewport);
  867. RendererCanvasCull::Canvas *canvas = RSG::canvas->canvas_owner.get_or_null(p_canvas);
  868. ERR_FAIL_COND(!canvas);
  869. viewport->canvas_map.erase(p_canvas);
  870. canvas->viewports.erase(p_viewport);
  871. }
  872. void RendererViewport::viewport_set_canvas_transform(RID p_viewport, RID p_canvas, const Transform2D &p_offset) {
  873. Viewport *viewport = viewport_owner.get_or_null(p_viewport);
  874. ERR_FAIL_COND(!viewport);
  875. ERR_FAIL_COND(!viewport->canvas_map.has(p_canvas));
  876. viewport->canvas_map[p_canvas].transform = p_offset;
  877. }
  878. void RendererViewport::viewport_set_transparent_background(RID p_viewport, bool p_enabled) {
  879. Viewport *viewport = viewport_owner.get_or_null(p_viewport);
  880. ERR_FAIL_COND(!viewport);
  881. RSG::texture_storage->render_target_set_transparent(viewport->render_target, p_enabled);
  882. viewport->transparent_bg = p_enabled;
  883. }
  884. void RendererViewport::viewport_set_global_canvas_transform(RID p_viewport, const Transform2D &p_transform) {
  885. Viewport *viewport = viewport_owner.get_or_null(p_viewport);
  886. ERR_FAIL_COND(!viewport);
  887. viewport->global_transform = p_transform;
  888. }
  889. void RendererViewport::viewport_set_canvas_stacking(RID p_viewport, RID p_canvas, int p_layer, int p_sublayer) {
  890. Viewport *viewport = viewport_owner.get_or_null(p_viewport);
  891. ERR_FAIL_COND(!viewport);
  892. ERR_FAIL_COND(!viewport->canvas_map.has(p_canvas));
  893. viewport->canvas_map[p_canvas].layer = p_layer;
  894. viewport->canvas_map[p_canvas].sublayer = p_sublayer;
  895. }
  896. void RendererViewport::viewport_set_positional_shadow_atlas_size(RID p_viewport, int p_size, bool p_16_bits) {
  897. Viewport *viewport = viewport_owner.get_or_null(p_viewport);
  898. ERR_FAIL_COND(!viewport);
  899. viewport->shadow_atlas_size = p_size;
  900. viewport->shadow_atlas_16_bits = p_16_bits;
  901. RSG::light_storage->shadow_atlas_set_size(viewport->shadow_atlas, viewport->shadow_atlas_size, viewport->shadow_atlas_16_bits);
  902. }
  903. void RendererViewport::viewport_set_positional_shadow_atlas_quadrant_subdivision(RID p_viewport, int p_quadrant, int p_subdiv) {
  904. Viewport *viewport = viewport_owner.get_or_null(p_viewport);
  905. ERR_FAIL_COND(!viewport);
  906. RSG::light_storage->shadow_atlas_set_quadrant_subdivision(viewport->shadow_atlas, p_quadrant, p_subdiv);
  907. }
  908. void RendererViewport::viewport_set_msaa_2d(RID p_viewport, RS::ViewportMSAA p_msaa) {
  909. Viewport *viewport = viewport_owner.get_or_null(p_viewport);
  910. ERR_FAIL_COND(!viewport);
  911. if (viewport->msaa_2d == p_msaa) {
  912. return;
  913. }
  914. viewport->msaa_2d = p_msaa;
  915. RSG::texture_storage->render_target_set_msaa(viewport->render_target, p_msaa);
  916. }
  917. void RendererViewport::viewport_set_msaa_3d(RID p_viewport, RS::ViewportMSAA p_msaa) {
  918. Viewport *viewport = viewport_owner.get_or_null(p_viewport);
  919. ERR_FAIL_COND(!viewport);
  920. if (viewport->msaa_3d == p_msaa) {
  921. return;
  922. }
  923. viewport->msaa_3d = p_msaa;
  924. _configure_3d_render_buffers(viewport);
  925. }
  926. void RendererViewport::viewport_set_screen_space_aa(RID p_viewport, RS::ViewportScreenSpaceAA p_mode) {
  927. Viewport *viewport = viewport_owner.get_or_null(p_viewport);
  928. ERR_FAIL_COND(!viewport);
  929. if (viewport->screen_space_aa == p_mode) {
  930. return;
  931. }
  932. viewport->screen_space_aa = p_mode;
  933. _configure_3d_render_buffers(viewport);
  934. }
  935. void RendererViewport::viewport_set_use_taa(RID p_viewport, bool p_use_taa) {
  936. Viewport *viewport = viewport_owner.get_or_null(p_viewport);
  937. ERR_FAIL_COND(!viewport);
  938. ERR_FAIL_COND_EDMSG(OS::get_singleton()->get_current_rendering_method() != "forward_plus", "TAA is only available when using the Forward+ renderer.");
  939. if (viewport->use_taa == p_use_taa) {
  940. return;
  941. }
  942. viewport->use_taa = p_use_taa;
  943. _configure_3d_render_buffers(viewport);
  944. }
  945. void RendererViewport::viewport_set_use_debanding(RID p_viewport, bool p_use_debanding) {
  946. Viewport *viewport = viewport_owner.get_or_null(p_viewport);
  947. ERR_FAIL_COND(!viewport);
  948. if (viewport->use_debanding == p_use_debanding) {
  949. return;
  950. }
  951. viewport->use_debanding = p_use_debanding;
  952. if (viewport->render_buffers.is_valid()) {
  953. viewport->render_buffers->set_use_debanding(p_use_debanding);
  954. }
  955. }
  956. void RendererViewport::viewport_set_use_occlusion_culling(RID p_viewport, bool p_use_occlusion_culling) {
  957. Viewport *viewport = viewport_owner.get_or_null(p_viewport);
  958. ERR_FAIL_COND(!viewport);
  959. if (viewport->use_occlusion_culling == p_use_occlusion_culling) {
  960. return;
  961. }
  962. viewport->use_occlusion_culling = p_use_occlusion_culling;
  963. if (viewport->use_occlusion_culling) {
  964. RendererSceneOcclusionCull::get_singleton()->add_buffer(p_viewport);
  965. RendererSceneOcclusionCull::get_singleton()->buffer_set_scenario(p_viewport, viewport->scenario);
  966. } else {
  967. RendererSceneOcclusionCull::get_singleton()->remove_buffer(p_viewport);
  968. }
  969. viewport->occlusion_buffer_dirty = true;
  970. }
  971. void RendererViewport::viewport_set_occlusion_rays_per_thread(int p_rays_per_thread) {
  972. if (occlusion_rays_per_thread == p_rays_per_thread) {
  973. return;
  974. }
  975. occlusion_rays_per_thread = p_rays_per_thread;
  976. for (int i = 0; i < active_viewports.size(); i++) {
  977. active_viewports[i]->occlusion_buffer_dirty = true;
  978. }
  979. }
  980. void RendererViewport::viewport_set_occlusion_culling_build_quality(RS::ViewportOcclusionCullingBuildQuality p_quality) {
  981. RendererSceneOcclusionCull::get_singleton()->set_build_quality(p_quality);
  982. }
  983. void RendererViewport::viewport_set_mesh_lod_threshold(RID p_viewport, float p_pixels) {
  984. Viewport *viewport = viewport_owner.get_or_null(p_viewport);
  985. ERR_FAIL_COND(!viewport);
  986. viewport->mesh_lod_threshold = p_pixels;
  987. }
  988. int RendererViewport::viewport_get_render_info(RID p_viewport, RS::ViewportRenderInfoType p_type, RS::ViewportRenderInfo p_info) {
  989. ERR_FAIL_INDEX_V(p_type, RS::VIEWPORT_RENDER_INFO_TYPE_MAX, -1);
  990. ERR_FAIL_INDEX_V(p_info, RS::VIEWPORT_RENDER_INFO_MAX, -1);
  991. Viewport *viewport = viewport_owner.get_or_null(p_viewport);
  992. if (!viewport) {
  993. return 0; //there should be a lock here..
  994. }
  995. return viewport->render_info.info[p_type][p_info];
  996. }
  997. void RendererViewport::viewport_set_debug_draw(RID p_viewport, RS::ViewportDebugDraw p_draw) {
  998. Viewport *viewport = viewport_owner.get_or_null(p_viewport);
  999. ERR_FAIL_COND(!viewport);
  1000. viewport->debug_draw = p_draw;
  1001. }
  1002. void RendererViewport::viewport_set_measure_render_time(RID p_viewport, bool p_enable) {
  1003. Viewport *viewport = viewport_owner.get_or_null(p_viewport);
  1004. ERR_FAIL_COND(!viewport);
  1005. viewport->measure_render_time = p_enable;
  1006. }
  1007. float RendererViewport::viewport_get_measured_render_time_cpu(RID p_viewport) const {
  1008. Viewport *viewport = viewport_owner.get_or_null(p_viewport);
  1009. ERR_FAIL_COND_V(!viewport, 0);
  1010. return double(viewport->time_cpu_end - viewport->time_cpu_begin) / 1000.0;
  1011. }
  1012. float RendererViewport::viewport_get_measured_render_time_gpu(RID p_viewport) const {
  1013. Viewport *viewport = viewport_owner.get_or_null(p_viewport);
  1014. ERR_FAIL_COND_V(!viewport, 0);
  1015. return double((viewport->time_gpu_end - viewport->time_gpu_begin) / 1000) / 1000.0;
  1016. }
  1017. void RendererViewport::viewport_set_snap_2d_transforms_to_pixel(RID p_viewport, bool p_enabled) {
  1018. Viewport *viewport = viewport_owner.get_or_null(p_viewport);
  1019. ERR_FAIL_COND(!viewport);
  1020. viewport->snap_2d_transforms_to_pixel = p_enabled;
  1021. }
  1022. void RendererViewport::viewport_set_snap_2d_vertices_to_pixel(RID p_viewport, bool p_enabled) {
  1023. Viewport *viewport = viewport_owner.get_or_null(p_viewport);
  1024. ERR_FAIL_COND(!viewport);
  1025. viewport->snap_2d_vertices_to_pixel = p_enabled;
  1026. }
  1027. void RendererViewport::viewport_set_default_canvas_item_texture_filter(RID p_viewport, RS::CanvasItemTextureFilter p_filter) {
  1028. ERR_FAIL_COND_MSG(p_filter == RS::CANVAS_ITEM_TEXTURE_FILTER_DEFAULT, "Viewport does not accept DEFAULT as texture filter (it's the topmost choice already).)");
  1029. Viewport *viewport = viewport_owner.get_or_null(p_viewport);
  1030. ERR_FAIL_COND(!viewport);
  1031. viewport->texture_filter = p_filter;
  1032. }
  1033. void RendererViewport::viewport_set_default_canvas_item_texture_repeat(RID p_viewport, RS::CanvasItemTextureRepeat p_repeat) {
  1034. ERR_FAIL_COND_MSG(p_repeat == RS::CANVAS_ITEM_TEXTURE_REPEAT_DEFAULT, "Viewport does not accept DEFAULT as texture repeat (it's the topmost choice already).)");
  1035. Viewport *viewport = viewport_owner.get_or_null(p_viewport);
  1036. ERR_FAIL_COND(!viewport);
  1037. viewport->texture_repeat = p_repeat;
  1038. }
  1039. void RendererViewport::viewport_set_sdf_oversize_and_scale(RID p_viewport, RS::ViewportSDFOversize p_size, RS::ViewportSDFScale p_scale) {
  1040. Viewport *viewport = viewport_owner.get_or_null(p_viewport);
  1041. ERR_FAIL_COND(!viewport);
  1042. RSG::texture_storage->render_target_set_sdf_size_and_scale(viewport->render_target, p_size, p_scale);
  1043. }
  1044. RID RendererViewport::viewport_find_from_screen_attachment(DisplayServer::WindowID p_id) const {
  1045. RID *rids = nullptr;
  1046. uint32_t rid_count = viewport_owner.get_rid_count();
  1047. rids = (RID *)alloca(sizeof(RID *) * rid_count);
  1048. viewport_owner.fill_owned_buffer(rids);
  1049. for (uint32_t i = 0; i < rid_count; i++) {
  1050. Viewport *viewport = viewport_owner.get_or_null(rids[i]);
  1051. if (viewport->viewport_to_screen == p_id) {
  1052. return rids[i];
  1053. }
  1054. }
  1055. return RID();
  1056. }
  1057. void RendererViewport::viewport_set_vrs_mode(RID p_viewport, RS::ViewportVRSMode p_mode) {
  1058. Viewport *viewport = viewport_owner.get_or_null(p_viewport);
  1059. ERR_FAIL_COND(!viewport);
  1060. RSG::texture_storage->render_target_set_vrs_mode(viewport->render_target, p_mode);
  1061. _configure_3d_render_buffers(viewport);
  1062. }
  1063. void RendererViewport::viewport_set_vrs_texture(RID p_viewport, RID p_texture) {
  1064. Viewport *viewport = viewport_owner.get_or_null(p_viewport);
  1065. ERR_FAIL_COND(!viewport);
  1066. RSG::texture_storage->render_target_set_vrs_texture(viewport->render_target, p_texture);
  1067. _configure_3d_render_buffers(viewport);
  1068. }
  1069. bool RendererViewport::free(RID p_rid) {
  1070. if (viewport_owner.owns(p_rid)) {
  1071. Viewport *viewport = viewport_owner.get_or_null(p_rid);
  1072. RSG::texture_storage->render_target_free(viewport->render_target);
  1073. RSG::light_storage->shadow_atlas_free(viewport->shadow_atlas);
  1074. if (viewport->render_buffers.is_valid()) {
  1075. viewport->render_buffers.unref();
  1076. }
  1077. while (viewport->canvas_map.begin()) {
  1078. viewport_remove_canvas(p_rid, viewport->canvas_map.begin()->key);
  1079. }
  1080. viewport_set_scenario(p_rid, RID());
  1081. active_viewports.erase(viewport);
  1082. sorted_active_viewports_dirty = true;
  1083. if (viewport->use_occlusion_culling) {
  1084. RendererSceneOcclusionCull::get_singleton()->remove_buffer(p_rid);
  1085. }
  1086. viewport_owner.free(p_rid);
  1087. return true;
  1088. }
  1089. return false;
  1090. }
  1091. void RendererViewport::handle_timestamp(String p_timestamp, uint64_t p_cpu_time, uint64_t p_gpu_time) {
  1092. RID *vp = timestamp_vp_map.getptr(p_timestamp);
  1093. if (!vp) {
  1094. return;
  1095. }
  1096. Viewport *viewport = viewport_owner.get_or_null(*vp);
  1097. if (!viewport) {
  1098. return;
  1099. }
  1100. if (p_timestamp.begins_with("vp_begin")) {
  1101. viewport->time_cpu_begin = p_cpu_time;
  1102. viewport->time_gpu_begin = p_gpu_time;
  1103. }
  1104. if (p_timestamp.begins_with("vp_end")) {
  1105. viewport->time_cpu_end = p_cpu_time;
  1106. viewport->time_gpu_end = p_gpu_time;
  1107. }
  1108. }
  1109. void RendererViewport::set_default_clear_color(const Color &p_color) {
  1110. RSG::texture_storage->set_default_clear_color(p_color);
  1111. }
  1112. void RendererViewport::viewport_set_canvas_cull_mask(RID p_viewport, uint32_t p_canvas_cull_mask) {
  1113. Viewport *viewport = viewport_owner.get_or_null(p_viewport);
  1114. ERR_FAIL_COND(!viewport);
  1115. viewport->canvas_cull_mask = p_canvas_cull_mask;
  1116. }
  1117. // Workaround for setting this on thread.
  1118. void RendererViewport::call_set_vsync_mode(DisplayServer::VSyncMode p_mode, DisplayServer::WindowID p_window) {
  1119. DisplayServer::get_singleton()->window_set_vsync_mode(p_mode, p_window);
  1120. }
  1121. int RendererViewport::get_total_objects_drawn() const {
  1122. return total_objects_drawn;
  1123. }
  1124. int RendererViewport::get_total_vertices_drawn() const {
  1125. return total_vertices_drawn;
  1126. }
  1127. int RendererViewport::get_total_draw_calls_used() const {
  1128. return total_draw_calls_used;
  1129. }
  1130. RendererViewport::RendererViewport() {
  1131. occlusion_rays_per_thread = GLOBAL_GET("rendering/occlusion_culling/occlusion_rays_per_thread");
  1132. }