renderer_canvas_cull.cpp 92 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650
  1. /**************************************************************************/
  2. /* renderer_canvas_cull.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_canvas_cull.h"
  31. #include "core/config/project_settings.h"
  32. #include "core/math/geometry_2d.h"
  33. #include "core/math/transform_interpolator.h"
  34. #include "renderer_viewport.h"
  35. #include "rendering_server_default.h"
  36. #include "rendering_server_globals.h"
  37. #include "servers/rendering/storage/texture_storage.h"
  38. // Use the same antialiasing feather size as StyleBoxFlat's default
  39. // (but doubled, as it's specified for both sides here).
  40. // This value is empirically determined to provide good antialiasing quality
  41. // while not making lines appear too soft.
  42. const static float FEATHER_SIZE = 1.25f;
  43. void RendererCanvasCull::_render_canvas_item_tree(RID p_to_render_target, Canvas::ChildItem *p_child_items, int p_child_item_count, const Transform2D &p_transform, const Rect2 &p_clip_rect, const Color &p_modulate, RendererCanvasRender::Light *p_lights, RendererCanvasRender::Light *p_directional_lights, RenderingServer::CanvasItemTextureFilter p_default_filter, RenderingServer::CanvasItemTextureRepeat p_default_repeat, bool p_snap_2d_vertices_to_pixel, uint32_t p_canvas_cull_mask, RenderingMethod::RenderInfo *r_render_info) {
  44. RENDER_TIMESTAMP("Cull CanvasItem Tree");
  45. memset(z_list, 0, z_range * sizeof(RendererCanvasRender::Item *));
  46. memset(z_last_list, 0, z_range * sizeof(RendererCanvasRender::Item *));
  47. for (int i = 0; i < p_child_item_count; i++) {
  48. _cull_canvas_item(p_child_items[i].item, p_transform, p_clip_rect, Color(1, 1, 1, 1), 0, z_list, z_last_list, nullptr, nullptr, false, p_canvas_cull_mask, Point2(), 1, nullptr);
  49. }
  50. RendererCanvasRender::Item *list = nullptr;
  51. RendererCanvasRender::Item *list_end = nullptr;
  52. for (int i = 0; i < z_range; i++) {
  53. if (!z_list[i]) {
  54. continue;
  55. }
  56. if (!list) {
  57. list = z_list[i];
  58. list_end = z_last_list[i];
  59. } else {
  60. list_end->next = z_list[i];
  61. list_end = z_last_list[i];
  62. }
  63. }
  64. RENDER_TIMESTAMP("Render CanvasItems");
  65. bool sdf_flag;
  66. RSG::canvas_render->canvas_render_items(p_to_render_target, list, p_modulate, p_lights, p_directional_lights, p_transform, p_default_filter, p_default_repeat, p_snap_2d_vertices_to_pixel, sdf_flag, r_render_info);
  67. if (sdf_flag) {
  68. sdf_used = true;
  69. }
  70. }
  71. void RendererCanvasCull::_collect_ysort_children(RendererCanvasCull::Item *p_canvas_item, RendererCanvasCull::Item *p_material_owner, const Color &p_modulate, RendererCanvasCull::Item **r_items, int &r_index, int p_z) {
  72. int child_item_count = p_canvas_item->child_items.size();
  73. RendererCanvasCull::Item **child_items = p_canvas_item->child_items.ptrw();
  74. for (int i = 0; i < child_item_count; i++) {
  75. if (child_items[i]->visible) {
  76. // To y-sort according to the item's final position, physics interpolation
  77. // and transform snapping need to be applied before y-sorting.
  78. Transform2D child_xform;
  79. if (!_interpolation_data.interpolation_enabled || !child_items[i]->interpolated) {
  80. child_xform = child_items[i]->xform_curr;
  81. } else {
  82. real_t f = Engine::get_singleton()->get_physics_interpolation_fraction();
  83. TransformInterpolator::interpolate_transform_2d(child_items[i]->xform_prev, child_items[i]->xform_curr, child_xform, f);
  84. }
  85. if (snapping_2d_transforms_to_pixel) {
  86. child_xform.columns[2] = (child_xform.columns[2] + Point2(0.5, 0.5)).floor();
  87. }
  88. r_items[r_index] = child_items[i];
  89. child_items[i]->ysort_xform = p_canvas_item->ysort_xform * child_xform;
  90. child_items[i]->material_owner = child_items[i]->use_parent_material ? p_material_owner : nullptr;
  91. child_items[i]->ysort_modulate = p_modulate;
  92. child_items[i]->ysort_index = r_index;
  93. child_items[i]->ysort_parent_abs_z_index = p_z;
  94. if (!child_items[i]->repeat_source) {
  95. child_items[i]->repeat_size = p_canvas_item->repeat_size;
  96. child_items[i]->repeat_times = p_canvas_item->repeat_times;
  97. child_items[i]->repeat_source_item = p_canvas_item->repeat_source_item;
  98. }
  99. // Y sorted canvas items are flattened into r_items. Calculate their absolute z index to use when rendering r_items.
  100. int abs_z = 0;
  101. if (child_items[i]->z_relative) {
  102. abs_z = CLAMP(p_z + child_items[i]->z_index, RS::CANVAS_ITEM_Z_MIN, RS::CANVAS_ITEM_Z_MAX);
  103. } else {
  104. abs_z = child_items[i]->z_index;
  105. }
  106. r_index++;
  107. if (child_items[i]->sort_y) {
  108. _collect_ysort_children(child_items[i], child_items[i]->use_parent_material ? p_material_owner : child_items[i], p_modulate * child_items[i]->modulate, r_items, r_index, abs_z);
  109. }
  110. }
  111. }
  112. }
  113. int RendererCanvasCull::_count_ysort_children(RendererCanvasCull::Item *p_canvas_item) {
  114. int ysort_children_count = 0;
  115. int child_item_count = p_canvas_item->child_items.size();
  116. RendererCanvasCull::Item *const *child_items = p_canvas_item->child_items.ptr();
  117. for (int i = 0; i < child_item_count; i++) {
  118. if (child_items[i]->visible) {
  119. ysort_children_count++;
  120. if (child_items[i]->sort_y) {
  121. ysort_children_count += _count_ysort_children(child_items[i]);
  122. }
  123. }
  124. }
  125. return ysort_children_count;
  126. }
  127. void RendererCanvasCull::_mark_ysort_dirty(RendererCanvasCull::Item *ysort_owner) {
  128. do {
  129. ysort_owner->ysort_children_count = -1;
  130. ysort_owner = canvas_item_owner.owns(ysort_owner->parent) ? canvas_item_owner.get_or_null(ysort_owner->parent) : nullptr;
  131. } while (ysort_owner && ysort_owner->sort_y);
  132. }
  133. void RendererCanvasCull::_attach_canvas_item_for_draw(RendererCanvasCull::Item *ci, RendererCanvasCull::Item *p_canvas_clip, RendererCanvasRender::Item **r_z_list, RendererCanvasRender::Item **r_z_last_list, const Transform2D &p_transform, const Rect2 &p_clip_rect, Rect2 p_global_rect, const Color &p_modulate, int p_z, RendererCanvasCull::Item *p_material_owner, bool p_use_canvas_group, RendererCanvasRender::Item *r_canvas_group_from) {
  134. if (ci->copy_back_buffer) {
  135. ci->copy_back_buffer->screen_rect = p_transform.xform(ci->copy_back_buffer->rect).intersection(p_clip_rect);
  136. }
  137. if (p_use_canvas_group) {
  138. int zidx = p_z - RS::CANVAS_ITEM_Z_MIN;
  139. if (r_canvas_group_from == nullptr) {
  140. // no list before processing this item, means must put stuff in group from the beginning of list.
  141. r_canvas_group_from = r_z_list[zidx];
  142. } else {
  143. // there was a list before processing, so begin group from this one.
  144. r_canvas_group_from = r_canvas_group_from->next;
  145. }
  146. if (r_canvas_group_from) {
  147. // Has a place to begin the group from!
  148. //compute a global rect (in global coords) for children in the same z layer
  149. Rect2 rect_accum;
  150. RendererCanvasRender::Item *c = r_canvas_group_from;
  151. while (c) {
  152. if (c == r_canvas_group_from) {
  153. rect_accum = c->global_rect_cache;
  154. } else {
  155. rect_accum = rect_accum.merge(c->global_rect_cache);
  156. }
  157. c = c->next;
  158. }
  159. // We have two choices now, if user has drawn something, we must assume users wants to draw the "mask", so compute the size based on this.
  160. // If nothing has been drawn, we just take it over and draw it ourselves.
  161. if (ci->canvas_group->fit_empty && (ci->commands == nullptr || (ci->commands->next == nullptr && ci->commands->type == RendererCanvasCull::Item::Command::TYPE_RECT && (static_cast<RendererCanvasCull::Item::CommandRect *>(ci->commands)->flags & RendererCanvasRender::CANVAS_RECT_IS_GROUP)))) {
  162. // No commands, or sole command is the one used to draw, so we (re)create the draw command.
  163. ci->clear();
  164. if (rect_accum == Rect2()) {
  165. rect_accum.size = Size2(1, 1);
  166. }
  167. rect_accum = rect_accum.grow(ci->canvas_group->fit_margin);
  168. //draw it?
  169. RendererCanvasRender::Item::CommandRect *crect = ci->alloc_command<RendererCanvasRender::Item::CommandRect>();
  170. crect->flags = RendererCanvasRender::CANVAS_RECT_IS_GROUP; // so we can recognize it later
  171. crect->rect = p_transform.affine_inverse().xform(rect_accum);
  172. crect->modulate = Color(1, 1, 1, 1);
  173. //the global rect is used to do the copying, so update it
  174. p_global_rect = rect_accum.grow(ci->canvas_group->clear_margin); //grow again by clear margin
  175. p_global_rect.position += p_clip_rect.position;
  176. } else {
  177. p_global_rect.position -= p_clip_rect.position;
  178. p_global_rect = p_global_rect.merge(rect_accum); //must use both rects for this
  179. p_global_rect = p_global_rect.grow(ci->canvas_group->clear_margin); //grow by clear margin
  180. p_global_rect.position += p_clip_rect.position;
  181. }
  182. // Very important that this is cleared after used in RendererCanvasRender to avoid
  183. // potential crashes.
  184. r_canvas_group_from->canvas_group_owner = ci;
  185. }
  186. }
  187. if (((ci->commands != nullptr || ci->visibility_notifier) && p_clip_rect.intersects(p_global_rect, true)) || ci->vp_render || ci->copy_back_buffer) {
  188. //something to draw?
  189. if (ci->update_when_visible) {
  190. RenderingServerDefault::redraw_request();
  191. }
  192. if (ci->commands != nullptr || ci->copy_back_buffer) {
  193. ci->final_transform = p_transform;
  194. ci->final_modulate = p_modulate * ci->self_modulate;
  195. ci->global_rect_cache = p_global_rect;
  196. ci->global_rect_cache.position -= p_clip_rect.position;
  197. ci->light_masked = false;
  198. int zidx = p_z - RS::CANVAS_ITEM_Z_MIN;
  199. if (r_z_last_list[zidx]) {
  200. r_z_last_list[zidx]->next = ci;
  201. r_z_last_list[zidx] = ci;
  202. } else {
  203. r_z_list[zidx] = ci;
  204. r_z_last_list[zidx] = ci;
  205. }
  206. ci->z_final = p_z;
  207. ci->next = nullptr;
  208. }
  209. if (ci->visibility_notifier) {
  210. if (!ci->visibility_notifier->visible_element.in_list()) {
  211. visibility_notifier_list.add(&ci->visibility_notifier->visible_element);
  212. ci->visibility_notifier->just_visible = true;
  213. }
  214. ci->visibility_notifier->visible_in_frame = RSG::rasterizer->get_frame_number();
  215. }
  216. } else if (ci->repeat_source) {
  217. // If repeat source does not draw itself it still needs transform updated as its child items' repeat offsets are relative to it.
  218. ci->final_transform = p_transform;
  219. }
  220. }
  221. void RendererCanvasCull::_cull_canvas_item(Item *p_canvas_item, const Transform2D &p_parent_xform, const Rect2 &p_clip_rect, const Color &p_modulate, int p_z, RendererCanvasRender::Item **r_z_list, RendererCanvasRender::Item **r_z_last_list, Item *p_canvas_clip, Item *p_material_owner, bool p_is_already_y_sorted, uint32_t p_canvas_cull_mask, const Point2 &p_repeat_size, int p_repeat_times, RendererCanvasRender::Item *p_repeat_source_item) {
  222. Item *ci = p_canvas_item;
  223. if (!ci->visible) {
  224. return;
  225. }
  226. if (!(ci->visibility_layer & p_canvas_cull_mask)) {
  227. return;
  228. }
  229. if (ci->children_order_dirty) {
  230. ci->child_items.sort_custom<ItemIndexSort>();
  231. ci->children_order_dirty = false;
  232. }
  233. if (ci->use_parent_material && p_material_owner) {
  234. ci->material_owner = p_material_owner;
  235. } else {
  236. p_material_owner = ci;
  237. ci->material_owner = nullptr;
  238. }
  239. Color modulate = ci->modulate * p_modulate;
  240. if (modulate.a < 0.007) {
  241. return;
  242. }
  243. Rect2 rect = ci->get_rect();
  244. if (ci->visibility_notifier) {
  245. if (ci->visibility_notifier->area.size != Vector2()) {
  246. rect = rect.merge(ci->visibility_notifier->area);
  247. }
  248. }
  249. Transform2D self_xform;
  250. Transform2D final_xform;
  251. if (p_is_already_y_sorted) {
  252. // Y-sorted item's final transform is calculated before y-sorting,
  253. // and is passed as `p_parent_xform` afterwards. No need to recalculate.
  254. final_xform = p_parent_xform;
  255. } else {
  256. if (!_interpolation_data.interpolation_enabled || !ci->interpolated) {
  257. self_xform = ci->xform_curr;
  258. } else {
  259. real_t f = Engine::get_singleton()->get_physics_interpolation_fraction();
  260. TransformInterpolator::interpolate_transform_2d(ci->xform_prev, ci->xform_curr, self_xform, f);
  261. }
  262. Transform2D parent_xform = p_parent_xform;
  263. if (snapping_2d_transforms_to_pixel) {
  264. self_xform.columns[2] = (self_xform.columns[2] + Point2(0.5, 0.5)).floor();
  265. parent_xform.columns[2] = (parent_xform.columns[2] + Point2(0.5, 0.5)).floor();
  266. }
  267. final_xform = parent_xform * self_xform;
  268. }
  269. Point2 repeat_size = p_repeat_size;
  270. int repeat_times = p_repeat_times;
  271. RendererCanvasRender::Item *repeat_source_item = p_repeat_source_item;
  272. if (ci->repeat_source) {
  273. repeat_size = ci->repeat_size;
  274. repeat_times = ci->repeat_times;
  275. repeat_source_item = ci;
  276. } else {
  277. ci->repeat_size = repeat_size;
  278. ci->repeat_times = repeat_times;
  279. ci->repeat_source_item = repeat_source_item;
  280. }
  281. Rect2 global_rect = final_xform.xform(rect);
  282. if (repeat_source_item && (repeat_size.x || repeat_size.y)) {
  283. // Top-left repeated rect.
  284. Rect2 corner_rect = global_rect;
  285. corner_rect.position -= repeat_source_item->final_transform.basis_xform((repeat_times / 2) * repeat_size);
  286. global_rect = corner_rect;
  287. // Plus top-right repeated rect.
  288. Size2 size_x_offset = repeat_source_item->final_transform.basis_xform(repeat_times * Size2(repeat_size.x, 0));
  289. corner_rect.position += size_x_offset;
  290. global_rect = global_rect.merge(corner_rect);
  291. // Plus bottom-right repeated rect.
  292. corner_rect.position += repeat_source_item->final_transform.basis_xform(repeat_times * Size2(0, repeat_size.y));
  293. global_rect = global_rect.merge(corner_rect);
  294. // Plus bottom-left repeated rect.
  295. corner_rect.position -= size_x_offset;
  296. global_rect = global_rect.merge(corner_rect);
  297. }
  298. global_rect.position += p_clip_rect.position;
  299. int child_item_count = ci->child_items.size();
  300. Item **child_items = ci->child_items.ptrw();
  301. if (ci->clip) {
  302. if (p_canvas_clip != nullptr) {
  303. ci->final_clip_rect = p_canvas_clip->final_clip_rect.intersection(global_rect);
  304. } else {
  305. ci->final_clip_rect = p_clip_rect.intersection(global_rect);
  306. }
  307. if (ci->final_clip_rect.size.width < 0.5 || ci->final_clip_rect.size.height < 0.5) {
  308. // The clip rect area is 0, so don't draw the item.
  309. return;
  310. }
  311. ci->final_clip_rect.position = ci->final_clip_rect.position.round();
  312. ci->final_clip_rect.size = ci->final_clip_rect.size.round();
  313. ci->final_clip_owner = ci;
  314. } else {
  315. ci->final_clip_owner = p_canvas_clip;
  316. }
  317. int parent_z = p_z;
  318. if (ci->z_relative) {
  319. p_z = CLAMP(p_z + ci->z_index, RS::CANVAS_ITEM_Z_MIN, RS::CANVAS_ITEM_Z_MAX);
  320. } else {
  321. p_z = ci->z_index;
  322. }
  323. if (ci->sort_y) {
  324. if (!p_is_already_y_sorted) {
  325. if (ci->ysort_children_count == -1) {
  326. ci->ysort_children_count = _count_ysort_children(ci);
  327. }
  328. child_item_count = ci->ysort_children_count + 1;
  329. child_items = (Item **)alloca(child_item_count * sizeof(Item *));
  330. ci->ysort_xform = Transform2D();
  331. ci->ysort_modulate = Color(1, 1, 1, 1);
  332. ci->ysort_index = 0;
  333. ci->ysort_parent_abs_z_index = parent_z;
  334. child_items[0] = ci;
  335. int i = 1;
  336. _collect_ysort_children(ci, p_material_owner, Color(1, 1, 1, 1), child_items, i, p_z);
  337. SortArray<Item *, ItemYSort> sorter;
  338. sorter.sort(child_items, child_item_count);
  339. for (i = 0; i < child_item_count; i++) {
  340. _cull_canvas_item(child_items[i], final_xform * child_items[i]->ysort_xform, p_clip_rect, modulate * child_items[i]->ysort_modulate, child_items[i]->ysort_parent_abs_z_index, r_z_list, r_z_last_list, (Item *)ci->final_clip_owner, (Item *)child_items[i]->material_owner, true, p_canvas_cull_mask, child_items[i]->repeat_size, child_items[i]->repeat_times, child_items[i]->repeat_source_item);
  341. }
  342. } else {
  343. RendererCanvasRender::Item *canvas_group_from = nullptr;
  344. bool use_canvas_group = ci->canvas_group != nullptr && (ci->canvas_group->fit_empty || ci->commands != nullptr);
  345. if (use_canvas_group) {
  346. int zidx = p_z - RS::CANVAS_ITEM_Z_MIN;
  347. canvas_group_from = r_z_last_list[zidx];
  348. }
  349. _attach_canvas_item_for_draw(ci, p_canvas_clip, r_z_list, r_z_last_list, final_xform, p_clip_rect, global_rect, modulate, p_z, p_material_owner, use_canvas_group, canvas_group_from);
  350. }
  351. } else {
  352. RendererCanvasRender::Item *canvas_group_from = nullptr;
  353. bool use_canvas_group = ci->canvas_group != nullptr && (ci->canvas_group->fit_empty || ci->commands != nullptr);
  354. if (use_canvas_group) {
  355. int zidx = p_z - RS::CANVAS_ITEM_Z_MIN;
  356. canvas_group_from = r_z_last_list[zidx];
  357. }
  358. for (int i = 0; i < child_item_count; i++) {
  359. if (!child_items[i]->behind && !use_canvas_group) {
  360. continue;
  361. }
  362. _cull_canvas_item(child_items[i], final_xform, p_clip_rect, modulate, p_z, r_z_list, r_z_last_list, (Item *)ci->final_clip_owner, p_material_owner, false, p_canvas_cull_mask, repeat_size, repeat_times, repeat_source_item);
  363. }
  364. _attach_canvas_item_for_draw(ci, p_canvas_clip, r_z_list, r_z_last_list, final_xform, p_clip_rect, global_rect, modulate, p_z, p_material_owner, use_canvas_group, canvas_group_from);
  365. for (int i = 0; i < child_item_count; i++) {
  366. if (child_items[i]->behind || use_canvas_group) {
  367. continue;
  368. }
  369. _cull_canvas_item(child_items[i], final_xform, p_clip_rect, modulate, p_z, r_z_list, r_z_last_list, (Item *)ci->final_clip_owner, p_material_owner, false, p_canvas_cull_mask, repeat_size, repeat_times, repeat_source_item);
  370. }
  371. }
  372. }
  373. void RendererCanvasCull::render_canvas(RID p_render_target, Canvas *p_canvas, const Transform2D &p_transform, RendererCanvasRender::Light *p_lights, RendererCanvasRender::Light *p_directional_lights, const Rect2 &p_clip_rect, RenderingServer::CanvasItemTextureFilter p_default_filter, RenderingServer::CanvasItemTextureRepeat p_default_repeat, bool p_snap_2d_transforms_to_pixel, bool p_snap_2d_vertices_to_pixel, uint32_t canvas_cull_mask, RenderingMethod::RenderInfo *r_render_info) {
  374. RENDER_TIMESTAMP("> Render Canvas");
  375. sdf_used = false;
  376. snapping_2d_transforms_to_pixel = p_snap_2d_transforms_to_pixel;
  377. if (p_canvas->children_order_dirty) {
  378. p_canvas->child_items.sort();
  379. p_canvas->children_order_dirty = false;
  380. }
  381. int l = p_canvas->child_items.size();
  382. Canvas::ChildItem *ci = p_canvas->child_items.ptrw();
  383. _render_canvas_item_tree(p_render_target, ci, l, p_transform, p_clip_rect, p_canvas->modulate, p_lights, p_directional_lights, p_default_filter, p_default_repeat, p_snap_2d_vertices_to_pixel, canvas_cull_mask, r_render_info);
  384. RENDER_TIMESTAMP("< Render Canvas");
  385. }
  386. bool RendererCanvasCull::was_sdf_used() {
  387. return sdf_used;
  388. }
  389. RID RendererCanvasCull::canvas_allocate() {
  390. return canvas_owner.allocate_rid();
  391. }
  392. void RendererCanvasCull::canvas_initialize(RID p_rid) {
  393. canvas_owner.initialize_rid(p_rid);
  394. }
  395. void RendererCanvasCull::canvas_set_item_mirroring(RID p_canvas, RID p_item, const Point2 &p_mirroring) {
  396. Canvas *canvas = canvas_owner.get_or_null(p_canvas);
  397. ERR_FAIL_NULL(canvas);
  398. Item *canvas_item = canvas_item_owner.get_or_null(p_item);
  399. ERR_FAIL_NULL(canvas_item);
  400. int idx = canvas->find_item(canvas_item);
  401. ERR_FAIL_COND(idx == -1);
  402. bool is_repeat_source = (p_mirroring.x || p_mirroring.y);
  403. canvas_item->repeat_source = is_repeat_source;
  404. canvas_item->repeat_source_item = is_repeat_source ? canvas_item : nullptr;
  405. canvas_item->repeat_size = p_mirroring;
  406. canvas_item->repeat_times = 1;
  407. }
  408. void RendererCanvasCull::canvas_set_item_repeat(RID p_item, const Point2 &p_repeat_size, int p_repeat_times) {
  409. ERR_FAIL_COND(p_repeat_times < 0);
  410. Item *canvas_item = canvas_item_owner.get_or_null(p_item);
  411. ERR_FAIL_NULL(canvas_item);
  412. bool is_repeat_source = (p_repeat_size.x || p_repeat_size.y) && p_repeat_times;
  413. canvas_item->repeat_source = is_repeat_source;
  414. canvas_item->repeat_source_item = is_repeat_source ? canvas_item : nullptr;
  415. canvas_item->repeat_size = p_repeat_size;
  416. canvas_item->repeat_times = p_repeat_times;
  417. }
  418. void RendererCanvasCull::canvas_set_modulate(RID p_canvas, const Color &p_color) {
  419. Canvas *canvas = canvas_owner.get_or_null(p_canvas);
  420. ERR_FAIL_NULL(canvas);
  421. canvas->modulate = p_color;
  422. }
  423. void RendererCanvasCull::canvas_set_disable_scale(bool p_disable) {
  424. disable_scale = p_disable;
  425. }
  426. void RendererCanvasCull::canvas_set_parent(RID p_canvas, RID p_parent, float p_scale) {
  427. Canvas *canvas = canvas_owner.get_or_null(p_canvas);
  428. ERR_FAIL_NULL(canvas);
  429. canvas->parent = p_parent;
  430. canvas->parent_scale = p_scale;
  431. }
  432. RID RendererCanvasCull::canvas_item_allocate() {
  433. return canvas_item_owner.allocate_rid();
  434. }
  435. void RendererCanvasCull::canvas_item_initialize(RID p_rid) {
  436. canvas_item_owner.initialize_rid(p_rid);
  437. }
  438. void RendererCanvasCull::canvas_item_set_parent(RID p_item, RID p_parent) {
  439. Item *canvas_item = canvas_item_owner.get_or_null(p_item);
  440. ERR_FAIL_NULL(canvas_item);
  441. if (canvas_item->parent.is_valid()) {
  442. if (canvas_owner.owns(canvas_item->parent)) {
  443. Canvas *canvas = canvas_owner.get_or_null(canvas_item->parent);
  444. canvas->erase_item(canvas_item);
  445. } else if (canvas_item_owner.owns(canvas_item->parent)) {
  446. Item *item_owner = canvas_item_owner.get_or_null(canvas_item->parent);
  447. item_owner->child_items.erase(canvas_item);
  448. if (item_owner->sort_y) {
  449. _mark_ysort_dirty(item_owner);
  450. }
  451. }
  452. canvas_item->parent = RID();
  453. }
  454. if (p_parent.is_valid()) {
  455. if (canvas_owner.owns(p_parent)) {
  456. Canvas *canvas = canvas_owner.get_or_null(p_parent);
  457. Canvas::ChildItem ci;
  458. ci.item = canvas_item;
  459. canvas->child_items.push_back(ci);
  460. canvas->children_order_dirty = true;
  461. } else if (canvas_item_owner.owns(p_parent)) {
  462. Item *item_owner = canvas_item_owner.get_or_null(p_parent);
  463. item_owner->child_items.push_back(canvas_item);
  464. item_owner->children_order_dirty = true;
  465. if (item_owner->sort_y) {
  466. _mark_ysort_dirty(item_owner);
  467. }
  468. } else {
  469. ERR_FAIL_MSG("Invalid parent.");
  470. }
  471. }
  472. canvas_item->parent = p_parent;
  473. }
  474. void RendererCanvasCull::canvas_item_set_visible(RID p_item, bool p_visible) {
  475. Item *canvas_item = canvas_item_owner.get_or_null(p_item);
  476. ERR_FAIL_NULL(canvas_item);
  477. canvas_item->visible = p_visible;
  478. _mark_ysort_dirty(canvas_item);
  479. }
  480. void RendererCanvasCull::canvas_item_set_light_mask(RID p_item, int p_mask) {
  481. Item *canvas_item = canvas_item_owner.get_or_null(p_item);
  482. ERR_FAIL_NULL(canvas_item);
  483. canvas_item->light_mask = p_mask;
  484. }
  485. void RendererCanvasCull::canvas_item_set_transform(RID p_item, const Transform2D &p_transform) {
  486. Item *canvas_item = canvas_item_owner.get_or_null(p_item);
  487. ERR_FAIL_NULL(canvas_item);
  488. if (_interpolation_data.interpolation_enabled && canvas_item->interpolated) {
  489. if (!canvas_item->on_interpolate_transform_list) {
  490. _interpolation_data.canvas_item_transform_update_list_curr->push_back(p_item);
  491. canvas_item->on_interpolate_transform_list = true;
  492. } else {
  493. DEV_ASSERT(_interpolation_data.canvas_item_transform_update_list_curr->size() > 0);
  494. }
  495. }
  496. canvas_item->xform_curr = p_transform;
  497. }
  498. void RendererCanvasCull::canvas_item_set_visibility_layer(RID p_item, uint32_t p_visibility_layer) {
  499. Item *canvas_item = canvas_item_owner.get_or_null(p_item);
  500. ERR_FAIL_NULL(canvas_item);
  501. canvas_item->visibility_layer = p_visibility_layer;
  502. }
  503. uint32_t RendererCanvasCull::canvas_item_get_visibility_layer(RID p_item) {
  504. Item *canvas_item = canvas_item_owner.get_or_null(p_item);
  505. if (!canvas_item) {
  506. return 0;
  507. }
  508. return canvas_item->visibility_layer;
  509. }
  510. void RendererCanvasCull::canvas_item_set_clip(RID p_item, bool p_clip) {
  511. Item *canvas_item = canvas_item_owner.get_or_null(p_item);
  512. ERR_FAIL_NULL(canvas_item);
  513. canvas_item->clip = p_clip;
  514. }
  515. void RendererCanvasCull::canvas_item_set_distance_field_mode(RID p_item, bool p_enable) {
  516. Item *canvas_item = canvas_item_owner.get_or_null(p_item);
  517. ERR_FAIL_NULL(canvas_item);
  518. canvas_item->distance_field = p_enable;
  519. }
  520. void RendererCanvasCull::canvas_item_set_custom_rect(RID p_item, bool p_custom_rect, const Rect2 &p_rect) {
  521. Item *canvas_item = canvas_item_owner.get_or_null(p_item);
  522. ERR_FAIL_NULL(canvas_item);
  523. canvas_item->custom_rect = p_custom_rect;
  524. canvas_item->rect = p_rect;
  525. }
  526. void RendererCanvasCull::canvas_item_set_modulate(RID p_item, const Color &p_color) {
  527. Item *canvas_item = canvas_item_owner.get_or_null(p_item);
  528. ERR_FAIL_NULL(canvas_item);
  529. canvas_item->modulate = p_color;
  530. }
  531. void RendererCanvasCull::canvas_item_set_self_modulate(RID p_item, const Color &p_color) {
  532. Item *canvas_item = canvas_item_owner.get_or_null(p_item);
  533. ERR_FAIL_NULL(canvas_item);
  534. canvas_item->self_modulate = p_color;
  535. }
  536. void RendererCanvasCull::canvas_item_set_draw_behind_parent(RID p_item, bool p_enable) {
  537. Item *canvas_item = canvas_item_owner.get_or_null(p_item);
  538. ERR_FAIL_NULL(canvas_item);
  539. canvas_item->behind = p_enable;
  540. }
  541. void RendererCanvasCull::canvas_item_set_update_when_visible(RID p_item, bool p_update) {
  542. Item *canvas_item = canvas_item_owner.get_or_null(p_item);
  543. ERR_FAIL_NULL(canvas_item);
  544. canvas_item->update_when_visible = p_update;
  545. }
  546. void RendererCanvasCull::canvas_item_add_line(RID p_item, const Point2 &p_from, const Point2 &p_to, const Color &p_color, float p_width, bool p_antialiased) {
  547. Item *canvas_item = canvas_item_owner.get_or_null(p_item);
  548. ERR_FAIL_NULL(canvas_item);
  549. Item::CommandPrimitive *line = canvas_item->alloc_command<Item::CommandPrimitive>();
  550. ERR_FAIL_NULL(line);
  551. Vector2 diff = (p_from - p_to);
  552. Vector2 dir = diff.orthogonal().normalized();
  553. Vector2 t = dir * p_width * 0.5;
  554. Vector2 begin_left;
  555. Vector2 begin_right;
  556. Vector2 end_left;
  557. Vector2 end_right;
  558. if (p_width >= 0.0) {
  559. begin_left = p_from + t;
  560. begin_right = p_from - t;
  561. end_left = p_to + t;
  562. end_right = p_to - t;
  563. line->points[0] = begin_left;
  564. line->points[1] = begin_right;
  565. line->points[2] = end_right;
  566. line->points[3] = end_left;
  567. line->point_count = 4;
  568. } else {
  569. begin_left = p_from;
  570. begin_right = p_from;
  571. end_left = p_to;
  572. end_right = p_to;
  573. line->points[0] = p_from;
  574. line->points[1] = p_to;
  575. line->point_count = 2;
  576. }
  577. for (uint32_t i = 0; i < line->point_count; i++) {
  578. line->colors[i] = p_color;
  579. }
  580. if (p_antialiased) {
  581. float border_size = FEATHER_SIZE;
  582. if (0.0f <= p_width && p_width < 1.0f) {
  583. border_size *= p_width;
  584. }
  585. Vector2 dir2 = diff.normalized();
  586. Vector2 border = dir * border_size;
  587. Vector2 border2 = dir2 * border_size;
  588. Color transparent = Color(p_color, 0.0);
  589. {
  590. Item::CommandPrimitive *left_border = canvas_item->alloc_command<Item::CommandPrimitive>();
  591. ERR_FAIL_NULL(left_border);
  592. left_border->points[0] = begin_left;
  593. left_border->points[1] = begin_left + border;
  594. left_border->points[2] = end_left + border;
  595. left_border->points[3] = end_left;
  596. left_border->colors[0] = p_color;
  597. left_border->colors[1] = transparent;
  598. left_border->colors[2] = transparent;
  599. left_border->colors[3] = p_color;
  600. left_border->point_count = 4;
  601. }
  602. {
  603. Item::CommandPrimitive *right_border = canvas_item->alloc_command<Item::CommandPrimitive>();
  604. ERR_FAIL_NULL(right_border);
  605. right_border->points[0] = begin_right;
  606. right_border->points[1] = begin_right - border;
  607. right_border->points[2] = end_right - border;
  608. right_border->points[3] = end_right;
  609. right_border->colors[0] = p_color;
  610. right_border->colors[1] = transparent;
  611. right_border->colors[2] = transparent;
  612. right_border->colors[3] = p_color;
  613. right_border->point_count = 4;
  614. }
  615. {
  616. Item::CommandPrimitive *top_border = canvas_item->alloc_command<Item::CommandPrimitive>();
  617. ERR_FAIL_NULL(top_border);
  618. top_border->points[0] = begin_left;
  619. top_border->points[1] = begin_left + border2;
  620. top_border->points[2] = begin_right + border2;
  621. top_border->points[3] = begin_right;
  622. top_border->colors[0] = p_color;
  623. top_border->colors[1] = transparent;
  624. top_border->colors[2] = transparent;
  625. top_border->colors[3] = p_color;
  626. top_border->point_count = 4;
  627. }
  628. {
  629. Item::CommandPrimitive *bottom_border = canvas_item->alloc_command<Item::CommandPrimitive>();
  630. ERR_FAIL_NULL(bottom_border);
  631. bottom_border->points[0] = end_left;
  632. bottom_border->points[1] = end_left - border2;
  633. bottom_border->points[2] = end_right - border2;
  634. bottom_border->points[3] = end_right;
  635. bottom_border->colors[0] = p_color;
  636. bottom_border->colors[1] = transparent;
  637. bottom_border->colors[2] = transparent;
  638. bottom_border->colors[3] = p_color;
  639. bottom_border->point_count = 4;
  640. }
  641. {
  642. Item::CommandPrimitive *top_left_corner = canvas_item->alloc_command<Item::CommandPrimitive>();
  643. ERR_FAIL_NULL(top_left_corner);
  644. top_left_corner->points[0] = begin_left;
  645. top_left_corner->points[1] = begin_left + border2;
  646. top_left_corner->points[2] = begin_left + border + border2;
  647. top_left_corner->points[3] = begin_left + border;
  648. top_left_corner->colors[0] = p_color;
  649. top_left_corner->colors[1] = transparent;
  650. top_left_corner->colors[2] = transparent;
  651. top_left_corner->colors[3] = transparent;
  652. top_left_corner->point_count = 4;
  653. }
  654. {
  655. Item::CommandPrimitive *top_right_corner = canvas_item->alloc_command<Item::CommandPrimitive>();
  656. ERR_FAIL_NULL(top_right_corner);
  657. top_right_corner->points[0] = begin_right;
  658. top_right_corner->points[1] = begin_right + border2;
  659. top_right_corner->points[2] = begin_right - border + border2;
  660. top_right_corner->points[3] = begin_right - border;
  661. top_right_corner->colors[0] = p_color;
  662. top_right_corner->colors[1] = transparent;
  663. top_right_corner->colors[2] = transparent;
  664. top_right_corner->colors[3] = transparent;
  665. top_right_corner->point_count = 4;
  666. }
  667. {
  668. Item::CommandPrimitive *bottom_left_corner = canvas_item->alloc_command<Item::CommandPrimitive>();
  669. ERR_FAIL_NULL(bottom_left_corner);
  670. bottom_left_corner->points[0] = end_left;
  671. bottom_left_corner->points[1] = end_left - border2;
  672. bottom_left_corner->points[2] = end_left + border - border2;
  673. bottom_left_corner->points[3] = end_left + border;
  674. bottom_left_corner->colors[0] = p_color;
  675. bottom_left_corner->colors[1] = transparent;
  676. bottom_left_corner->colors[2] = transparent;
  677. bottom_left_corner->colors[3] = transparent;
  678. bottom_left_corner->point_count = 4;
  679. }
  680. {
  681. Item::CommandPrimitive *bottom_right_corner = canvas_item->alloc_command<Item::CommandPrimitive>();
  682. ERR_FAIL_NULL(bottom_right_corner);
  683. bottom_right_corner->points[0] = end_right;
  684. bottom_right_corner->points[1] = end_right - border2;
  685. bottom_right_corner->points[2] = end_right - border - border2;
  686. bottom_right_corner->points[3] = end_right - border;
  687. bottom_right_corner->colors[0] = p_color;
  688. bottom_right_corner->colors[1] = transparent;
  689. bottom_right_corner->colors[2] = transparent;
  690. bottom_right_corner->colors[3] = transparent;
  691. bottom_right_corner->point_count = 4;
  692. }
  693. }
  694. }
  695. static Vector2 compute_polyline_segment_dir(const Vector<Point2> &p_points, int p_index, const Vector2 &p_prev_segment_dir) {
  696. int point_count = p_points.size();
  697. bool is_last_point = (p_index == point_count - 1);
  698. Vector2 segment_dir;
  699. if (is_last_point) {
  700. segment_dir = p_prev_segment_dir;
  701. } else {
  702. segment_dir = (p_points[p_index + 1] - p_points[p_index]).normalized();
  703. if (segment_dir.is_zero_approx()) {
  704. segment_dir = p_prev_segment_dir;
  705. }
  706. }
  707. return segment_dir;
  708. }
  709. static Vector2 compute_polyline_edge_offset_clamped(const Vector2 &p_segment_dir, const Vector2 &p_prev_segment_dir) {
  710. Vector2 bisector;
  711. float length = 1.0f;
  712. bisector = (p_prev_segment_dir * p_segment_dir.length() - p_segment_dir * p_prev_segment_dir.length()).normalized();
  713. float angle = atan2f(bisector.cross(p_prev_segment_dir), bisector.dot(p_prev_segment_dir));
  714. float sin_angle = sinf(angle);
  715. if (!Math::is_zero_approx(sin_angle) && !p_segment_dir.is_equal_approx(p_prev_segment_dir)) {
  716. length = 1.0f / sin_angle;
  717. length = CLAMP(length, -3.0f, 3.0f);
  718. } else {
  719. bisector = p_segment_dir.orthogonal();
  720. }
  721. if (bisector.is_zero_approx()) {
  722. bisector = p_segment_dir.orthogonal();
  723. }
  724. return bisector * length;
  725. }
  726. void RendererCanvasCull::canvas_item_add_polyline(RID p_item, const Vector<Point2> &p_points, const Vector<Color> &p_colors, float p_width, bool p_antialiased) {
  727. ERR_FAIL_COND(p_points.size() < 2);
  728. Item *canvas_item = canvas_item_owner.get_or_null(p_item);
  729. ERR_FAIL_NULL(canvas_item);
  730. Color color = Color(1, 1, 1, 1);
  731. Vector<int> indices;
  732. int point_count = p_points.size();
  733. Item::CommandPolygon *pline = canvas_item->alloc_command<Item::CommandPolygon>();
  734. ERR_FAIL_NULL(pline);
  735. if (p_width < 0) {
  736. if (p_antialiased) {
  737. WARN_PRINT("Antialiasing is not supported for thin polylines drawn using line strips (`p_width < 0`).");
  738. }
  739. pline->primitive = RS::PRIMITIVE_LINE_STRIP;
  740. if (p_colors.size() == 1 || p_colors.size() == point_count) {
  741. pline->polygon.create(indices, p_points, p_colors);
  742. } else {
  743. Vector<Color> colors;
  744. if (p_colors.is_empty()) {
  745. colors.push_back(color);
  746. } else {
  747. colors.resize(point_count);
  748. Color *colors_ptr = colors.ptrw();
  749. for (int i = 0; i < point_count; i++) {
  750. if (i < p_colors.size()) {
  751. color = p_colors[i];
  752. }
  753. colors_ptr[i] = color;
  754. }
  755. }
  756. pline->polygon.create(indices, p_points, colors);
  757. }
  758. return;
  759. }
  760. int polyline_point_count = point_count * 2;
  761. bool loop = p_points[0].is_equal_approx(p_points[point_count - 1]);
  762. Vector2 first_segment_dir;
  763. Vector2 last_segment_dir;
  764. // Search for first non-zero vector between two segments.
  765. for (int i = 1; i < point_count; i++) {
  766. first_segment_dir = (p_points[i] - p_points[i - 1]).normalized();
  767. if (!first_segment_dir.is_zero_approx()) {
  768. break;
  769. }
  770. }
  771. // Search for last non-zero vector between two segments.
  772. for (int i = point_count - 1; i >= 1; i--) {
  773. last_segment_dir = (p_points[i] - p_points[i - 1]).normalized();
  774. if (!last_segment_dir.is_zero_approx()) {
  775. break;
  776. }
  777. }
  778. PackedColorArray colors;
  779. PackedVector2Array points;
  780. // Additional 2+2 vertices to antialias begin+end of the middle triangle strip.
  781. colors.resize(polyline_point_count + ((p_antialiased && !loop) ? 4 : 0));
  782. points.resize(polyline_point_count + ((p_antialiased && !loop) ? 4 : 0));
  783. Vector2 *points_ptr = points.ptrw();
  784. Color *colors_ptr = colors.ptrw();
  785. if (p_antialiased) {
  786. float border_size = FEATHER_SIZE;
  787. if (p_width < 1.0f) {
  788. border_size *= p_width;
  789. }
  790. Color color2 = Color(1, 1, 1, 0);
  791. Item::CommandPolygon *pline_left = canvas_item->alloc_command<Item::CommandPolygon>();
  792. ERR_FAIL_NULL(pline_left);
  793. Item::CommandPolygon *pline_right = canvas_item->alloc_command<Item::CommandPolygon>();
  794. ERR_FAIL_NULL(pline_right);
  795. PackedColorArray colors_left;
  796. PackedVector2Array points_left;
  797. PackedColorArray colors_right;
  798. PackedVector2Array points_right;
  799. // 2+2 additional vertices for begin+end corners.
  800. // 1 additional vertex to swap the orientation of the triangles within the end corner's quad.
  801. colors_left.resize(polyline_point_count + (loop ? 0 : 5));
  802. points_left.resize(polyline_point_count + (loop ? 0 : 5));
  803. colors_right.resize(polyline_point_count + (loop ? 0 : 5));
  804. points_right.resize(polyline_point_count + (loop ? 0 : 5));
  805. Color *colors_left_ptr = colors_left.ptrw();
  806. Vector2 *points_left_ptr = points_left.ptrw();
  807. Vector2 *points_right_ptr = points_right.ptrw();
  808. Color *colors_right_ptr = colors_right.ptrw();
  809. Vector2 prev_segment_dir;
  810. for (int i = 0; i < point_count; i++) {
  811. bool is_first_point = (i == 0);
  812. bool is_last_point = (i == point_count - 1);
  813. Vector2 segment_dir = compute_polyline_segment_dir(p_points, i, prev_segment_dir);
  814. if (is_first_point && loop) {
  815. prev_segment_dir = last_segment_dir;
  816. } else if (is_last_point && loop) {
  817. prev_segment_dir = first_segment_dir;
  818. }
  819. Vector2 base_edge_offset;
  820. if (is_first_point && !loop) {
  821. base_edge_offset = first_segment_dir.orthogonal();
  822. } else if (is_last_point && !loop) {
  823. base_edge_offset = last_segment_dir.orthogonal();
  824. } else {
  825. base_edge_offset = compute_polyline_edge_offset_clamped(segment_dir, prev_segment_dir);
  826. }
  827. Vector2 edge_offset = base_edge_offset * (p_width * 0.5f);
  828. Vector2 border = base_edge_offset * border_size;
  829. Vector2 pos = p_points[i];
  830. int j = i * 2 + (loop ? 0 : 2);
  831. points_ptr[j + 0] = pos + edge_offset;
  832. points_ptr[j + 1] = pos - edge_offset;
  833. points_left_ptr[j + 0] = pos + edge_offset;
  834. points_left_ptr[j + 1] = pos + edge_offset + border;
  835. points_right_ptr[j + 0] = pos - edge_offset;
  836. points_right_ptr[j + 1] = pos - edge_offset - border;
  837. if (i < p_colors.size()) {
  838. color = p_colors[i];
  839. color2 = Color(color.r, color.g, color.b, 0);
  840. }
  841. colors_ptr[j + 0] = color;
  842. colors_ptr[j + 1] = color;
  843. colors_left_ptr[j + 0] = color;
  844. colors_left_ptr[j + 1] = color2;
  845. colors_right_ptr[j + 0] = color;
  846. colors_right_ptr[j + 1] = color2;
  847. if (is_first_point && !loop) {
  848. Vector2 begin_border = -segment_dir * border_size;
  849. points_ptr[0] = pos + edge_offset + begin_border;
  850. points_ptr[1] = pos - edge_offset + begin_border;
  851. colors_ptr[0] = color2;
  852. colors_ptr[1] = color2;
  853. points_left_ptr[0] = pos + edge_offset + begin_border;
  854. points_left_ptr[1] = pos + edge_offset + begin_border + border;
  855. colors_left_ptr[0] = color2;
  856. colors_left_ptr[1] = color2;
  857. points_right_ptr[0] = pos - edge_offset + begin_border;
  858. points_right_ptr[1] = pos - edge_offset + begin_border - border;
  859. colors_right_ptr[0] = color2;
  860. colors_right_ptr[1] = color2;
  861. }
  862. if (is_last_point && !loop) {
  863. Vector2 end_border = prev_segment_dir * border_size;
  864. int end_index = polyline_point_count + 2;
  865. points_ptr[end_index + 0] = pos + edge_offset + end_border;
  866. points_ptr[end_index + 1] = pos - edge_offset + end_border;
  867. colors_ptr[end_index + 0] = color2;
  868. colors_ptr[end_index + 1] = color2;
  869. // Swap orientation of the triangles within both end corner quads so the visual seams
  870. // between triangles goes from the edge corner. Done by going back to the edge corner
  871. // (1 additional vertex / zero-area triangle per left/right corner).
  872. points_left_ptr[end_index + 0] = pos + edge_offset;
  873. points_left_ptr[end_index + 1] = pos + edge_offset + end_border + border;
  874. points_left_ptr[end_index + 2] = pos + edge_offset + end_border;
  875. colors_left_ptr[end_index + 0] = color;
  876. colors_left_ptr[end_index + 1] = color2;
  877. colors_left_ptr[end_index + 2] = color2;
  878. points_right_ptr[end_index + 0] = pos - edge_offset;
  879. points_right_ptr[end_index + 1] = pos - edge_offset + end_border - border;
  880. points_right_ptr[end_index + 2] = pos - edge_offset + end_border;
  881. colors_right_ptr[end_index + 0] = color;
  882. colors_right_ptr[end_index + 1] = color2;
  883. colors_right_ptr[end_index + 2] = color2;
  884. }
  885. prev_segment_dir = segment_dir;
  886. }
  887. pline_left->primitive = RS::PRIMITIVE_TRIANGLE_STRIP;
  888. pline_left->polygon.create(indices, points_left, colors_left);
  889. pline_right->primitive = RS::PRIMITIVE_TRIANGLE_STRIP;
  890. pline_right->polygon.create(indices, points_right, colors_right);
  891. } else {
  892. // Makes a single triangle strip for drawing the line.
  893. Vector2 prev_segment_dir;
  894. for (int i = 0; i < point_count; i++) {
  895. bool is_first_point = (i == 0);
  896. bool is_last_point = (i == point_count - 1);
  897. Vector2 segment_dir = compute_polyline_segment_dir(p_points, i, prev_segment_dir);
  898. if (is_first_point && loop) {
  899. prev_segment_dir = last_segment_dir;
  900. } else if (is_last_point && loop) {
  901. prev_segment_dir = first_segment_dir;
  902. }
  903. Vector2 base_edge_offset;
  904. if (is_first_point && !loop) {
  905. base_edge_offset = first_segment_dir.orthogonal();
  906. } else if (is_last_point && !loop) {
  907. base_edge_offset = last_segment_dir.orthogonal();
  908. } else {
  909. base_edge_offset = compute_polyline_edge_offset_clamped(segment_dir, prev_segment_dir);
  910. }
  911. Vector2 edge_offset = base_edge_offset * (p_width * 0.5f);
  912. Vector2 pos = p_points[i];
  913. points_ptr[i * 2 + 0] = pos + edge_offset;
  914. points_ptr[i * 2 + 1] = pos - edge_offset;
  915. if (i < p_colors.size()) {
  916. color = p_colors[i];
  917. }
  918. colors_ptr[i * 2 + 0] = color;
  919. colors_ptr[i * 2 + 1] = color;
  920. prev_segment_dir = segment_dir;
  921. }
  922. }
  923. pline->primitive = RS::PRIMITIVE_TRIANGLE_STRIP;
  924. pline->polygon.create(indices, points, colors);
  925. }
  926. void RendererCanvasCull::canvas_item_add_multiline(RID p_item, const Vector<Point2> &p_points, const Vector<Color> &p_colors, float p_width, bool p_antialiased) {
  927. ERR_FAIL_COND(p_points.is_empty() || p_points.size() % 2 != 0);
  928. ERR_FAIL_COND(p_colors.size() != 1 && p_colors.size() * 2 != p_points.size());
  929. // TODO: `canvas_item_add_line`(`multiline`, `polyline`) share logic, should factor out.
  930. if (p_width < 0) {
  931. if (p_antialiased) {
  932. WARN_PRINT("Antialiasing is not supported for thin multilines drawn using line strips (`p_width < 0`).");
  933. }
  934. Item *canvas_item = canvas_item_owner.get_or_null(p_item);
  935. ERR_FAIL_NULL(canvas_item);
  936. Vector<Color> colors;
  937. if (p_colors.size() == 1) {
  938. colors = p_colors;
  939. } else { //} else if (p_colors.size() << 1 == p_points.size()) {
  940. colors.resize(p_points.size());
  941. Color *colors_ptr = colors.ptrw();
  942. for (int i = 0; i < p_colors.size(); i++) {
  943. Color color = p_colors[i];
  944. colors_ptr[i * 2 + 0] = color;
  945. colors_ptr[i * 2 + 1] = color;
  946. }
  947. }
  948. Item::CommandPolygon *pline = canvas_item->alloc_command<Item::CommandPolygon>();
  949. ERR_FAIL_NULL(pline);
  950. pline->primitive = RS::PRIMITIVE_LINES;
  951. pline->polygon.create(Vector<int>(), p_points, colors);
  952. } else {
  953. if (p_colors.size() == 1) {
  954. Color color = p_colors[0];
  955. for (int i = 0; i < p_points.size() >> 1; i++) {
  956. Vector2 from = p_points[i * 2 + 0];
  957. Vector2 to = p_points[i * 2 + 1];
  958. canvas_item_add_line(p_item, from, to, color, p_width, p_antialiased);
  959. }
  960. } else { //} else if (p_colors.size() << 1 == p_points.size()) {
  961. for (int i = 0; i < p_colors.size(); i++) {
  962. Color color = p_colors[i];
  963. Vector2 from = p_points[i * 2 + 0];
  964. Vector2 to = p_points[i * 2 + 1];
  965. canvas_item_add_line(p_item, from, to, color, p_width, p_antialiased);
  966. }
  967. }
  968. }
  969. }
  970. void RendererCanvasCull::canvas_item_add_rect(RID p_item, const Rect2 &p_rect, const Color &p_color, bool p_antialiased) {
  971. Item *canvas_item = canvas_item_owner.get_or_null(p_item);
  972. ERR_FAIL_NULL(canvas_item);
  973. Item::CommandRect *rect = canvas_item->alloc_command<Item::CommandRect>();
  974. ERR_FAIL_NULL(rect);
  975. rect->modulate = p_color;
  976. rect->rect = p_rect;
  977. // Add feathers.
  978. if (p_antialiased) {
  979. float border_size = FEATHER_SIZE;
  980. const real_t size = MIN(p_rect.size.width, p_rect.size.height);
  981. if (0.0f <= size && size < 1.0f) {
  982. border_size *= size;
  983. }
  984. const Vector2 vec_down = Vector2(0.0f, p_rect.size.height);
  985. const Vector2 vec_right = Vector2(p_rect.size.width, 0.0f);
  986. const Vector2 begin_left = p_rect.position;
  987. const Vector2 begin_right = p_rect.position + vec_down;
  988. const Vector2 end_left = p_rect.position + vec_right;
  989. const Vector2 end_right = p_rect.position + p_rect.size;
  990. const Vector2 dir = Vector2(0.0f, -1.0f);
  991. const Vector2 dir2 = Vector2(-1.0f, 0.0f);
  992. const Vector2 border = dir * border_size;
  993. const Vector2 border2 = dir2 * border_size;
  994. Color transparent = Color(p_color, 0.0);
  995. {
  996. Item::CommandPrimitive *left_border = canvas_item->alloc_command<Item::CommandPrimitive>();
  997. ERR_FAIL_NULL(left_border);
  998. left_border->points[0] = begin_left;
  999. left_border->points[1] = begin_left + border;
  1000. left_border->points[2] = end_left + border;
  1001. left_border->points[3] = end_left;
  1002. left_border->colors[0] = p_color;
  1003. left_border->colors[1] = transparent;
  1004. left_border->colors[2] = transparent;
  1005. left_border->colors[3] = p_color;
  1006. left_border->point_count = 4;
  1007. }
  1008. {
  1009. Item::CommandPrimitive *right_border = canvas_item->alloc_command<Item::CommandPrimitive>();
  1010. ERR_FAIL_NULL(right_border);
  1011. right_border->points[0] = begin_right;
  1012. right_border->points[1] = begin_right - border;
  1013. right_border->points[2] = end_right - border;
  1014. right_border->points[3] = end_right;
  1015. right_border->colors[0] = p_color;
  1016. right_border->colors[1] = transparent;
  1017. right_border->colors[2] = transparent;
  1018. right_border->colors[3] = p_color;
  1019. right_border->point_count = 4;
  1020. }
  1021. {
  1022. Item::CommandPrimitive *top_border = canvas_item->alloc_command<Item::CommandPrimitive>();
  1023. ERR_FAIL_NULL(top_border);
  1024. top_border->points[0] = begin_left;
  1025. top_border->points[1] = begin_left + border2;
  1026. top_border->points[2] = begin_right + border2;
  1027. top_border->points[3] = begin_right;
  1028. top_border->colors[0] = p_color;
  1029. top_border->colors[1] = transparent;
  1030. top_border->colors[2] = transparent;
  1031. top_border->colors[3] = p_color;
  1032. top_border->point_count = 4;
  1033. }
  1034. {
  1035. Item::CommandPrimitive *bottom_border = canvas_item->alloc_command<Item::CommandPrimitive>();
  1036. ERR_FAIL_NULL(bottom_border);
  1037. bottom_border->points[0] = end_left;
  1038. bottom_border->points[1] = end_left - border2;
  1039. bottom_border->points[2] = end_right - border2;
  1040. bottom_border->points[3] = end_right;
  1041. bottom_border->colors[0] = p_color;
  1042. bottom_border->colors[1] = transparent;
  1043. bottom_border->colors[2] = transparent;
  1044. bottom_border->colors[3] = p_color;
  1045. bottom_border->point_count = 4;
  1046. }
  1047. {
  1048. Item::CommandPrimitive *top_left_corner = canvas_item->alloc_command<Item::CommandPrimitive>();
  1049. ERR_FAIL_NULL(top_left_corner);
  1050. top_left_corner->points[0] = begin_left;
  1051. top_left_corner->points[1] = begin_left + border2;
  1052. top_left_corner->points[2] = begin_left + border + border2;
  1053. top_left_corner->points[3] = begin_left + border;
  1054. top_left_corner->colors[0] = p_color;
  1055. top_left_corner->colors[1] = transparent;
  1056. top_left_corner->colors[2] = transparent;
  1057. top_left_corner->colors[3] = transparent;
  1058. top_left_corner->point_count = 4;
  1059. }
  1060. {
  1061. Item::CommandPrimitive *top_right_corner = canvas_item->alloc_command<Item::CommandPrimitive>();
  1062. ERR_FAIL_NULL(top_right_corner);
  1063. top_right_corner->points[0] = begin_right;
  1064. top_right_corner->points[1] = begin_right + border2;
  1065. top_right_corner->points[2] = begin_right - border + border2;
  1066. top_right_corner->points[3] = begin_right - border;
  1067. top_right_corner->colors[0] = p_color;
  1068. top_right_corner->colors[1] = transparent;
  1069. top_right_corner->colors[2] = transparent;
  1070. top_right_corner->colors[3] = transparent;
  1071. top_right_corner->point_count = 4;
  1072. }
  1073. {
  1074. Item::CommandPrimitive *bottom_left_corner = canvas_item->alloc_command<Item::CommandPrimitive>();
  1075. ERR_FAIL_NULL(bottom_left_corner);
  1076. bottom_left_corner->points[0] = end_left;
  1077. bottom_left_corner->points[1] = end_left - border2;
  1078. bottom_left_corner->points[2] = end_left + border - border2;
  1079. bottom_left_corner->points[3] = end_left + border;
  1080. bottom_left_corner->colors[0] = p_color;
  1081. bottom_left_corner->colors[1] = transparent;
  1082. bottom_left_corner->colors[2] = transparent;
  1083. bottom_left_corner->colors[3] = transparent;
  1084. bottom_left_corner->point_count = 4;
  1085. }
  1086. {
  1087. Item::CommandPrimitive *bottom_right_corner = canvas_item->alloc_command<Item::CommandPrimitive>();
  1088. ERR_FAIL_NULL(bottom_right_corner);
  1089. bottom_right_corner->points[0] = end_right;
  1090. bottom_right_corner->points[1] = end_right - border2;
  1091. bottom_right_corner->points[2] = end_right - border - border2;
  1092. bottom_right_corner->points[3] = end_right - border;
  1093. bottom_right_corner->colors[0] = p_color;
  1094. bottom_right_corner->colors[1] = transparent;
  1095. bottom_right_corner->colors[2] = transparent;
  1096. bottom_right_corner->colors[3] = transparent;
  1097. bottom_right_corner->point_count = 4;
  1098. }
  1099. }
  1100. }
  1101. void RendererCanvasCull::canvas_item_add_circle(RID p_item, const Point2 &p_pos, float p_radius, const Color &p_color, bool p_antialiased) {
  1102. Item *canvas_item = canvas_item_owner.get_or_null(p_item);
  1103. ERR_FAIL_NULL(canvas_item);
  1104. static const int circle_segments = 64;
  1105. {
  1106. Item::CommandPolygon *circle = canvas_item->alloc_command<Item::CommandPolygon>();
  1107. ERR_FAIL_NULL(circle);
  1108. circle->primitive = RS::PRIMITIVE_TRIANGLES;
  1109. Vector<int> indices;
  1110. Vector<Vector2> points;
  1111. points.resize(circle_segments + 2);
  1112. Vector2 *points_ptr = points.ptrw();
  1113. // Store circle center in the last point.
  1114. points_ptr[circle_segments + 1] = p_pos;
  1115. const real_t circle_point_step = Math_TAU / circle_segments;
  1116. for (int i = 0; i < circle_segments + 1; i++) {
  1117. float angle = i * circle_point_step;
  1118. points_ptr[i].x = Math::cos(angle) * p_radius;
  1119. points_ptr[i].y = Math::sin(angle) * p_radius;
  1120. points_ptr[i] += p_pos;
  1121. }
  1122. indices.resize(circle_segments * 3);
  1123. int *indices_ptr = indices.ptrw();
  1124. for (int i = 0; i < circle_segments; i++) {
  1125. indices_ptr[i * 3 + 0] = circle_segments + 1;
  1126. indices_ptr[i * 3 + 1] = i;
  1127. indices_ptr[i * 3 + 2] = i + 1;
  1128. }
  1129. Vector<Color> color;
  1130. color.push_back(p_color);
  1131. circle->polygon.create(indices, points, color);
  1132. }
  1133. if (p_antialiased) {
  1134. float border_size = FEATHER_SIZE;
  1135. const float diameter = p_radius * 2.0f;
  1136. if (0.0f <= diameter && diameter < 1.0f) {
  1137. border_size *= p_radius;
  1138. }
  1139. Item::CommandPolygon *feather = canvas_item->alloc_command<Item::CommandPolygon>();
  1140. ERR_FAIL_NULL(feather);
  1141. feather->primitive = RS::PRIMITIVE_TRIANGLE_STRIP;
  1142. Color transparent = Color(p_color, 0.0);
  1143. Vector<int> indices;
  1144. Vector<Color> colors;
  1145. Vector<Vector2> points;
  1146. points.resize(2 * circle_segments + 2);
  1147. colors.resize(2 * circle_segments + 2);
  1148. const real_t circle_point_step = Math_TAU / circle_segments;
  1149. Vector2 *points_ptr = points.ptrw();
  1150. Color *colors_ptr = colors.ptrw();
  1151. for (int i = 0; i < circle_segments + 1; i++) {
  1152. const float angle = i * circle_point_step;
  1153. const float c = Math::cos(angle);
  1154. const float s = Math::sin(angle);
  1155. points_ptr[i * 2].x = c * p_radius;
  1156. points_ptr[i * 2].y = s * p_radius;
  1157. points_ptr[i * 2] += p_pos;
  1158. points_ptr[i * 2 + 1].x = c * (p_radius + border_size);
  1159. points_ptr[i * 2 + 1].y = s * (p_radius + border_size);
  1160. points_ptr[i * 2 + 1] += p_pos;
  1161. colors_ptr[i * 2] = p_color;
  1162. colors_ptr[i * 2 + 1] = transparent;
  1163. }
  1164. feather->polygon.create(indices, points, colors);
  1165. }
  1166. }
  1167. void RendererCanvasCull::canvas_item_add_texture_rect(RID p_item, const Rect2 &p_rect, RID p_texture, bool p_tile, const Color &p_modulate, bool p_transpose) {
  1168. Item *canvas_item = canvas_item_owner.get_or_null(p_item);
  1169. ERR_FAIL_NULL(canvas_item);
  1170. Item::CommandRect *rect = canvas_item->alloc_command<Item::CommandRect>();
  1171. ERR_FAIL_NULL(rect);
  1172. rect->modulate = p_modulate;
  1173. rect->rect = p_rect;
  1174. rect->flags = 0;
  1175. if (p_tile) {
  1176. rect->flags |= RendererCanvasRender::CANVAS_RECT_TILE;
  1177. rect->flags |= RendererCanvasRender::CANVAS_RECT_REGION;
  1178. rect->source = Rect2(0, 0, ABS(p_rect.size.width), ABS(p_rect.size.height));
  1179. }
  1180. if (p_rect.size.x < 0) {
  1181. rect->flags |= RendererCanvasRender::CANVAS_RECT_FLIP_H;
  1182. rect->rect.size.x = -rect->rect.size.x;
  1183. }
  1184. if (p_rect.size.y < 0) {
  1185. rect->flags |= RendererCanvasRender::CANVAS_RECT_FLIP_V;
  1186. rect->rect.size.y = -rect->rect.size.y;
  1187. }
  1188. if (p_transpose) {
  1189. rect->flags |= RendererCanvasRender::CANVAS_RECT_TRANSPOSE;
  1190. SWAP(rect->rect.size.x, rect->rect.size.y);
  1191. }
  1192. rect->texture = p_texture;
  1193. }
  1194. void RendererCanvasCull::canvas_item_add_msdf_texture_rect_region(RID p_item, const Rect2 &p_rect, RID p_texture, const Rect2 &p_src_rect, const Color &p_modulate, int p_outline_size, float p_px_range, float p_scale) {
  1195. Item *canvas_item = canvas_item_owner.get_or_null(p_item);
  1196. ERR_FAIL_NULL(canvas_item);
  1197. Item::CommandRect *rect = canvas_item->alloc_command<Item::CommandRect>();
  1198. ERR_FAIL_NULL(rect);
  1199. rect->modulate = p_modulate;
  1200. rect->rect = p_rect;
  1201. rect->texture = p_texture;
  1202. rect->source = p_src_rect;
  1203. rect->flags = RendererCanvasRender::CANVAS_RECT_REGION | RendererCanvasRender::CANVAS_RECT_MSDF;
  1204. if (p_rect.size.x < 0) {
  1205. rect->flags |= RendererCanvasRender::CANVAS_RECT_FLIP_H;
  1206. rect->rect.size.x = -rect->rect.size.x;
  1207. }
  1208. if (p_src_rect.size.x < 0) {
  1209. rect->flags ^= RendererCanvasRender::CANVAS_RECT_FLIP_H;
  1210. rect->source.size.x = -rect->source.size.x;
  1211. }
  1212. if (p_rect.size.y < 0) {
  1213. rect->flags |= RendererCanvasRender::CANVAS_RECT_FLIP_V;
  1214. rect->rect.size.y = -rect->rect.size.y;
  1215. }
  1216. if (p_src_rect.size.y < 0) {
  1217. rect->flags ^= RendererCanvasRender::CANVAS_RECT_FLIP_V;
  1218. rect->source.size.y = -rect->source.size.y;
  1219. }
  1220. rect->outline = (float)p_outline_size / p_scale / 4.0;
  1221. rect->px_range = p_px_range;
  1222. }
  1223. void RendererCanvasCull::canvas_item_add_lcd_texture_rect_region(RID p_item, const Rect2 &p_rect, RID p_texture, const Rect2 &p_src_rect, const Color &p_modulate) {
  1224. Item *canvas_item = canvas_item_owner.get_or_null(p_item);
  1225. ERR_FAIL_NULL(canvas_item);
  1226. Item::CommandRect *rect = canvas_item->alloc_command<Item::CommandRect>();
  1227. ERR_FAIL_NULL(rect);
  1228. rect->modulate = p_modulate;
  1229. rect->rect = p_rect;
  1230. rect->texture = p_texture;
  1231. rect->source = p_src_rect;
  1232. rect->flags = RendererCanvasRender::CANVAS_RECT_REGION | RendererCanvasRender::CANVAS_RECT_LCD;
  1233. if (p_rect.size.x < 0) {
  1234. rect->flags |= RendererCanvasRender::CANVAS_RECT_FLIP_H;
  1235. rect->rect.size.x = -rect->rect.size.x;
  1236. }
  1237. if (p_src_rect.size.x < 0) {
  1238. rect->flags ^= RendererCanvasRender::CANVAS_RECT_FLIP_H;
  1239. rect->source.size.x = -rect->source.size.x;
  1240. }
  1241. if (p_rect.size.y < 0) {
  1242. rect->flags |= RendererCanvasRender::CANVAS_RECT_FLIP_V;
  1243. rect->rect.size.y = -rect->rect.size.y;
  1244. }
  1245. if (p_src_rect.size.y < 0) {
  1246. rect->flags ^= RendererCanvasRender::CANVAS_RECT_FLIP_V;
  1247. rect->source.size.y = -rect->source.size.y;
  1248. }
  1249. }
  1250. void RendererCanvasCull::canvas_item_add_texture_rect_region(RID p_item, const Rect2 &p_rect, RID p_texture, const Rect2 &p_src_rect, const Color &p_modulate, bool p_transpose, bool p_clip_uv) {
  1251. Item *canvas_item = canvas_item_owner.get_or_null(p_item);
  1252. ERR_FAIL_NULL(canvas_item);
  1253. Item::CommandRect *rect = canvas_item->alloc_command<Item::CommandRect>();
  1254. ERR_FAIL_NULL(rect);
  1255. rect->modulate = p_modulate;
  1256. rect->rect = p_rect;
  1257. rect->texture = p_texture;
  1258. rect->source = p_src_rect;
  1259. rect->flags = RendererCanvasRender::CANVAS_RECT_REGION;
  1260. if (p_rect.size.x < 0) {
  1261. rect->flags |= RendererCanvasRender::CANVAS_RECT_FLIP_H;
  1262. rect->rect.size.x = -rect->rect.size.x;
  1263. }
  1264. if (p_src_rect.size.x < 0) {
  1265. rect->flags ^= RendererCanvasRender::CANVAS_RECT_FLIP_H;
  1266. rect->source.size.x = -rect->source.size.x;
  1267. }
  1268. if (p_rect.size.y < 0) {
  1269. rect->flags |= RendererCanvasRender::CANVAS_RECT_FLIP_V;
  1270. rect->rect.size.y = -rect->rect.size.y;
  1271. }
  1272. if (p_src_rect.size.y < 0) {
  1273. rect->flags ^= RendererCanvasRender::CANVAS_RECT_FLIP_V;
  1274. rect->source.size.y = -rect->source.size.y;
  1275. }
  1276. if (p_transpose) {
  1277. rect->flags |= RendererCanvasRender::CANVAS_RECT_TRANSPOSE;
  1278. SWAP(rect->rect.size.x, rect->rect.size.y);
  1279. }
  1280. if (p_clip_uv) {
  1281. rect->flags |= RendererCanvasRender::CANVAS_RECT_CLIP_UV;
  1282. }
  1283. }
  1284. void RendererCanvasCull::canvas_item_add_nine_patch(RID p_item, const Rect2 &p_rect, const Rect2 &p_source, RID p_texture, const Vector2 &p_topleft, const Vector2 &p_bottomright, RS::NinePatchAxisMode p_x_axis_mode, RS::NinePatchAxisMode p_y_axis_mode, bool p_draw_center, const Color &p_modulate) {
  1285. Item *canvas_item = canvas_item_owner.get_or_null(p_item);
  1286. ERR_FAIL_NULL(canvas_item);
  1287. Item::CommandNinePatch *style = canvas_item->alloc_command<Item::CommandNinePatch>();
  1288. ERR_FAIL_NULL(style);
  1289. style->texture = p_texture;
  1290. style->rect = p_rect;
  1291. style->source = p_source;
  1292. style->draw_center = p_draw_center;
  1293. style->color = p_modulate;
  1294. style->margin[SIDE_LEFT] = p_topleft.x;
  1295. style->margin[SIDE_TOP] = p_topleft.y;
  1296. style->margin[SIDE_RIGHT] = p_bottomright.x;
  1297. style->margin[SIDE_BOTTOM] = p_bottomright.y;
  1298. style->axis_x = p_x_axis_mode;
  1299. style->axis_y = p_y_axis_mode;
  1300. }
  1301. void RendererCanvasCull::canvas_item_add_primitive(RID p_item, const Vector<Point2> &p_points, const Vector<Color> &p_colors, const Vector<Point2> &p_uvs, RID p_texture) {
  1302. uint32_t pc = p_points.size();
  1303. ERR_FAIL_COND(pc == 0 || pc > 4);
  1304. Item *canvas_item = canvas_item_owner.get_or_null(p_item);
  1305. ERR_FAIL_NULL(canvas_item);
  1306. Item::CommandPrimitive *prim = canvas_item->alloc_command<Item::CommandPrimitive>();
  1307. ERR_FAIL_NULL(prim);
  1308. for (int i = 0; i < p_points.size(); i++) {
  1309. prim->points[i] = p_points[i];
  1310. if (i < p_uvs.size()) {
  1311. prim->uvs[i] = p_uvs[i];
  1312. }
  1313. if (i < p_colors.size()) {
  1314. prim->colors[i] = p_colors[i];
  1315. } else if (p_colors.size()) {
  1316. prim->colors[i] = p_colors[0];
  1317. } else {
  1318. prim->colors[i] = Color(1, 1, 1, 1);
  1319. }
  1320. }
  1321. prim->point_count = p_points.size();
  1322. prim->texture = p_texture;
  1323. }
  1324. void RendererCanvasCull::canvas_item_add_polygon(RID p_item, const Vector<Point2> &p_points, const Vector<Color> &p_colors, const Vector<Point2> &p_uvs, RID p_texture) {
  1325. Item *canvas_item = canvas_item_owner.get_or_null(p_item);
  1326. ERR_FAIL_NULL(canvas_item);
  1327. #ifdef DEBUG_ENABLED
  1328. int pointcount = p_points.size();
  1329. ERR_FAIL_COND(pointcount < 3);
  1330. int color_size = p_colors.size();
  1331. int uv_size = p_uvs.size();
  1332. ERR_FAIL_COND(color_size != 0 && color_size != 1 && color_size != pointcount);
  1333. ERR_FAIL_COND(uv_size != 0 && (uv_size != pointcount));
  1334. #endif
  1335. Vector<int> indices = Geometry2D::triangulate_polygon(p_points);
  1336. ERR_FAIL_COND_MSG(indices.is_empty(), "Invalid polygon data, triangulation failed.");
  1337. Item::CommandPolygon *polygon = canvas_item->alloc_command<Item::CommandPolygon>();
  1338. ERR_FAIL_NULL(polygon);
  1339. polygon->primitive = RS::PRIMITIVE_TRIANGLES;
  1340. polygon->texture = p_texture;
  1341. polygon->polygon.create(indices, p_points, p_colors, p_uvs);
  1342. }
  1343. void RendererCanvasCull::canvas_item_add_triangle_array(RID p_item, const Vector<int> &p_indices, const Vector<Point2> &p_points, const Vector<Color> &p_colors, const Vector<Point2> &p_uvs, const Vector<int> &p_bones, const Vector<float> &p_weights, RID p_texture, int p_count) {
  1344. Item *canvas_item = canvas_item_owner.get_or_null(p_item);
  1345. ERR_FAIL_NULL(canvas_item);
  1346. int vertex_count = p_points.size();
  1347. ERR_FAIL_COND(vertex_count == 0);
  1348. ERR_FAIL_COND(!p_colors.is_empty() && p_colors.size() != vertex_count && p_colors.size() != 1);
  1349. ERR_FAIL_COND(!p_uvs.is_empty() && p_uvs.size() != vertex_count);
  1350. ERR_FAIL_COND(!p_bones.is_empty() && p_bones.size() != vertex_count * 4);
  1351. ERR_FAIL_COND(!p_weights.is_empty() && p_weights.size() != vertex_count * 4);
  1352. Item::CommandPolygon *polygon = canvas_item->alloc_command<Item::CommandPolygon>();
  1353. ERR_FAIL_NULL(polygon);
  1354. polygon->texture = p_texture;
  1355. polygon->polygon.create(p_indices, p_points, p_colors, p_uvs, p_bones, p_weights);
  1356. polygon->primitive = RS::PRIMITIVE_TRIANGLES;
  1357. }
  1358. void RendererCanvasCull::canvas_item_add_set_transform(RID p_item, const Transform2D &p_transform) {
  1359. Item *canvas_item = canvas_item_owner.get_or_null(p_item);
  1360. ERR_FAIL_NULL(canvas_item);
  1361. Item::CommandTransform *tr = canvas_item->alloc_command<Item::CommandTransform>();
  1362. ERR_FAIL_NULL(tr);
  1363. tr->xform = p_transform;
  1364. }
  1365. void RendererCanvasCull::canvas_item_add_mesh(RID p_item, const RID &p_mesh, const Transform2D &p_transform, const Color &p_modulate, RID p_texture) {
  1366. Item *canvas_item = canvas_item_owner.get_or_null(p_item);
  1367. ERR_FAIL_NULL(canvas_item);
  1368. ERR_FAIL_COND(!p_mesh.is_valid());
  1369. Item::CommandMesh *m = canvas_item->alloc_command<Item::CommandMesh>();
  1370. ERR_FAIL_NULL(m);
  1371. m->mesh = p_mesh;
  1372. if (canvas_item->skeleton.is_valid()) {
  1373. m->mesh_instance = RSG::mesh_storage->mesh_instance_create(p_mesh);
  1374. RSG::mesh_storage->mesh_instance_set_skeleton(m->mesh_instance, canvas_item->skeleton);
  1375. }
  1376. m->texture = p_texture;
  1377. m->transform = p_transform;
  1378. m->modulate = p_modulate;
  1379. }
  1380. void RendererCanvasCull::canvas_item_add_particles(RID p_item, RID p_particles, RID p_texture) {
  1381. Item *canvas_item = canvas_item_owner.get_or_null(p_item);
  1382. ERR_FAIL_NULL(canvas_item);
  1383. Item::CommandParticles *part = canvas_item->alloc_command<Item::CommandParticles>();
  1384. ERR_FAIL_NULL(part);
  1385. part->particles = p_particles;
  1386. part->texture = p_texture;
  1387. //take the chance and request processing for them, at least once until they become visible again
  1388. RSG::particles_storage->particles_request_process(p_particles);
  1389. }
  1390. void RendererCanvasCull::canvas_item_add_multimesh(RID p_item, RID p_mesh, RID p_texture) {
  1391. Item *canvas_item = canvas_item_owner.get_or_null(p_item);
  1392. ERR_FAIL_NULL(canvas_item);
  1393. Item::CommandMultiMesh *mm = canvas_item->alloc_command<Item::CommandMultiMesh>();
  1394. ERR_FAIL_NULL(mm);
  1395. mm->multimesh = p_mesh;
  1396. mm->texture = p_texture;
  1397. }
  1398. void RendererCanvasCull::canvas_item_add_clip_ignore(RID p_item, bool p_ignore) {
  1399. Item *canvas_item = canvas_item_owner.get_or_null(p_item);
  1400. ERR_FAIL_NULL(canvas_item);
  1401. Item::CommandClipIgnore *ci = canvas_item->alloc_command<Item::CommandClipIgnore>();
  1402. ERR_FAIL_NULL(ci);
  1403. ci->ignore = p_ignore;
  1404. }
  1405. void RendererCanvasCull::canvas_item_add_animation_slice(RID p_item, double p_animation_length, double p_slice_begin, double p_slice_end, double p_offset) {
  1406. Item *canvas_item = canvas_item_owner.get_or_null(p_item);
  1407. ERR_FAIL_NULL(canvas_item);
  1408. Item::CommandAnimationSlice *as = canvas_item->alloc_command<Item::CommandAnimationSlice>();
  1409. ERR_FAIL_NULL(as);
  1410. as->animation_length = p_animation_length;
  1411. as->slice_begin = p_slice_begin;
  1412. as->slice_end = p_slice_end;
  1413. as->offset = p_offset;
  1414. }
  1415. void RendererCanvasCull::canvas_item_set_sort_children_by_y(RID p_item, bool p_enable) {
  1416. Item *canvas_item = canvas_item_owner.get_or_null(p_item);
  1417. ERR_FAIL_NULL(canvas_item);
  1418. canvas_item->sort_y = p_enable;
  1419. _mark_ysort_dirty(canvas_item);
  1420. }
  1421. void RendererCanvasCull::canvas_item_set_z_index(RID p_item, int p_z) {
  1422. ERR_FAIL_COND(p_z < RS::CANVAS_ITEM_Z_MIN || p_z > RS::CANVAS_ITEM_Z_MAX);
  1423. Item *canvas_item = canvas_item_owner.get_or_null(p_item);
  1424. ERR_FAIL_NULL(canvas_item);
  1425. canvas_item->z_index = p_z;
  1426. }
  1427. void RendererCanvasCull::canvas_item_set_z_as_relative_to_parent(RID p_item, bool p_enable) {
  1428. Item *canvas_item = canvas_item_owner.get_or_null(p_item);
  1429. ERR_FAIL_NULL(canvas_item);
  1430. canvas_item->z_relative = p_enable;
  1431. }
  1432. void RendererCanvasCull::canvas_item_attach_skeleton(RID p_item, RID p_skeleton) {
  1433. Item *canvas_item = canvas_item_owner.get_or_null(p_item);
  1434. ERR_FAIL_NULL(canvas_item);
  1435. if (canvas_item->skeleton == p_skeleton) {
  1436. return;
  1437. }
  1438. canvas_item->skeleton = p_skeleton;
  1439. Item::Command *c = canvas_item->commands;
  1440. while (c) {
  1441. if (c->type == Item::Command::TYPE_MESH) {
  1442. Item::CommandMesh *cm = static_cast<Item::CommandMesh *>(c);
  1443. if (canvas_item->skeleton.is_valid()) {
  1444. if (cm->mesh_instance.is_null()) {
  1445. cm->mesh_instance = RSG::mesh_storage->mesh_instance_create(cm->mesh);
  1446. }
  1447. RSG::mesh_storage->mesh_instance_set_skeleton(cm->mesh_instance, canvas_item->skeleton);
  1448. } else {
  1449. if (cm->mesh_instance.is_valid()) {
  1450. RSG::mesh_storage->mesh_instance_free(cm->mesh_instance);
  1451. cm->mesh_instance = RID();
  1452. }
  1453. }
  1454. }
  1455. c = c->next;
  1456. }
  1457. }
  1458. void RendererCanvasCull::canvas_item_set_copy_to_backbuffer(RID p_item, bool p_enable, const Rect2 &p_rect) {
  1459. Item *canvas_item = canvas_item_owner.get_or_null(p_item);
  1460. ERR_FAIL_NULL(canvas_item);
  1461. if (p_enable && (canvas_item->copy_back_buffer == nullptr)) {
  1462. canvas_item->copy_back_buffer = memnew(RendererCanvasRender::Item::CopyBackBuffer);
  1463. }
  1464. if (!p_enable && (canvas_item->copy_back_buffer != nullptr)) {
  1465. memdelete(canvas_item->copy_back_buffer);
  1466. canvas_item->copy_back_buffer = nullptr;
  1467. }
  1468. if (p_enable) {
  1469. canvas_item->copy_back_buffer->rect = p_rect;
  1470. canvas_item->copy_back_buffer->full = p_rect == Rect2();
  1471. }
  1472. }
  1473. void RendererCanvasCull::canvas_item_clear(RID p_item) {
  1474. Item *canvas_item = canvas_item_owner.get_or_null(p_item);
  1475. ERR_FAIL_NULL(canvas_item);
  1476. canvas_item->clear();
  1477. #ifdef DEBUG_ENABLED
  1478. if (debug_redraw) {
  1479. canvas_item->debug_redraw_time = debug_redraw_time;
  1480. }
  1481. #endif
  1482. }
  1483. void RendererCanvasCull::canvas_item_set_draw_index(RID p_item, int p_index) {
  1484. Item *canvas_item = canvas_item_owner.get_or_null(p_item);
  1485. ERR_FAIL_NULL(canvas_item);
  1486. canvas_item->index = p_index;
  1487. if (canvas_item_owner.owns(canvas_item->parent)) {
  1488. Item *canvas_item_parent = canvas_item_owner.get_or_null(canvas_item->parent);
  1489. canvas_item_parent->children_order_dirty = true;
  1490. return;
  1491. }
  1492. Canvas *canvas = canvas_owner.get_or_null(canvas_item->parent);
  1493. if (canvas) {
  1494. canvas->children_order_dirty = true;
  1495. return;
  1496. }
  1497. }
  1498. void RendererCanvasCull::canvas_item_set_material(RID p_item, RID p_material) {
  1499. Item *canvas_item = canvas_item_owner.get_or_null(p_item);
  1500. ERR_FAIL_NULL(canvas_item);
  1501. canvas_item->material = p_material;
  1502. }
  1503. void RendererCanvasCull::canvas_item_set_use_parent_material(RID p_item, bool p_enable) {
  1504. Item *canvas_item = canvas_item_owner.get_or_null(p_item);
  1505. ERR_FAIL_NULL(canvas_item);
  1506. canvas_item->use_parent_material = p_enable;
  1507. }
  1508. void RendererCanvasCull::canvas_item_set_visibility_notifier(RID p_item, bool p_enable, const Rect2 &p_area, const Callable &p_enter_callable, const Callable &p_exit_callable) {
  1509. Item *canvas_item = canvas_item_owner.get_or_null(p_item);
  1510. ERR_FAIL_NULL(canvas_item);
  1511. if (p_enable) {
  1512. if (!canvas_item->visibility_notifier) {
  1513. canvas_item->visibility_notifier = visibility_notifier_allocator.alloc();
  1514. }
  1515. canvas_item->visibility_notifier->area = p_area;
  1516. canvas_item->visibility_notifier->enter_callable = p_enter_callable;
  1517. canvas_item->visibility_notifier->exit_callable = p_exit_callable;
  1518. } else {
  1519. if (canvas_item->visibility_notifier) {
  1520. visibility_notifier_allocator.free(canvas_item->visibility_notifier);
  1521. canvas_item->visibility_notifier = nullptr;
  1522. }
  1523. }
  1524. }
  1525. void RendererCanvasCull::canvas_item_set_debug_redraw(bool p_enabled) {
  1526. debug_redraw = p_enabled;
  1527. RSG::canvas_render->set_debug_redraw(p_enabled, debug_redraw_time, debug_redraw_color);
  1528. }
  1529. bool RendererCanvasCull::canvas_item_get_debug_redraw() const {
  1530. return debug_redraw;
  1531. }
  1532. void RendererCanvasCull::canvas_item_set_interpolated(RID p_item, bool p_interpolated) {
  1533. Item *canvas_item = canvas_item_owner.get_or_null(p_item);
  1534. ERR_FAIL_NULL(canvas_item);
  1535. canvas_item->interpolated = p_interpolated;
  1536. }
  1537. void RendererCanvasCull::canvas_item_reset_physics_interpolation(RID p_item) {
  1538. Item *canvas_item = canvas_item_owner.get_or_null(p_item);
  1539. ERR_FAIL_NULL(canvas_item);
  1540. canvas_item->xform_prev = canvas_item->xform_curr;
  1541. }
  1542. // Useful especially for origin shifting.
  1543. void RendererCanvasCull::canvas_item_transform_physics_interpolation(RID p_item, const Transform2D &p_transform) {
  1544. Item *canvas_item = canvas_item_owner.get_or_null(p_item);
  1545. ERR_FAIL_NULL(canvas_item);
  1546. canvas_item->xform_prev = p_transform * canvas_item->xform_prev;
  1547. canvas_item->xform_curr = p_transform * canvas_item->xform_curr;
  1548. }
  1549. void RendererCanvasCull::canvas_item_set_canvas_group_mode(RID p_item, RS::CanvasGroupMode p_mode, float p_clear_margin, bool p_fit_empty, float p_fit_margin, bool p_blur_mipmaps) {
  1550. Item *canvas_item = canvas_item_owner.get_or_null(p_item);
  1551. ERR_FAIL_NULL(canvas_item);
  1552. if (p_mode == RS::CANVAS_GROUP_MODE_DISABLED) {
  1553. if (canvas_item->canvas_group != nullptr) {
  1554. memdelete(canvas_item->canvas_group);
  1555. canvas_item->canvas_group = nullptr;
  1556. }
  1557. } else {
  1558. if (canvas_item->canvas_group == nullptr) {
  1559. canvas_item->canvas_group = memnew(RendererCanvasRender::Item::CanvasGroup);
  1560. }
  1561. canvas_item->canvas_group->mode = p_mode;
  1562. canvas_item->canvas_group->fit_empty = p_fit_empty;
  1563. canvas_item->canvas_group->fit_margin = p_fit_margin;
  1564. canvas_item->canvas_group->blur_mipmaps = p_blur_mipmaps;
  1565. canvas_item->canvas_group->clear_margin = p_clear_margin;
  1566. }
  1567. }
  1568. RID RendererCanvasCull::canvas_light_allocate() {
  1569. return canvas_light_owner.allocate_rid();
  1570. }
  1571. void RendererCanvasCull::canvas_light_initialize(RID p_rid) {
  1572. canvas_light_owner.initialize_rid(p_rid);
  1573. RendererCanvasRender::Light *clight = canvas_light_owner.get_or_null(p_rid);
  1574. clight->light_internal = RSG::canvas_render->light_create();
  1575. }
  1576. void RendererCanvasCull::canvas_light_set_mode(RID p_light, RS::CanvasLightMode p_mode) {
  1577. RendererCanvasRender::Light *clight = canvas_light_owner.get_or_null(p_light);
  1578. ERR_FAIL_NULL(clight);
  1579. if (clight->mode == p_mode) {
  1580. return;
  1581. }
  1582. RID canvas = clight->canvas;
  1583. if (canvas.is_valid()) {
  1584. canvas_light_attach_to_canvas(p_light, RID());
  1585. }
  1586. clight->mode = p_mode;
  1587. if (canvas.is_valid()) {
  1588. canvas_light_attach_to_canvas(p_light, canvas);
  1589. }
  1590. }
  1591. void RendererCanvasCull::canvas_light_attach_to_canvas(RID p_light, RID p_canvas) {
  1592. RendererCanvasRender::Light *clight = canvas_light_owner.get_or_null(p_light);
  1593. ERR_FAIL_NULL(clight);
  1594. if (clight->canvas.is_valid()) {
  1595. Canvas *canvas = canvas_owner.get_or_null(clight->canvas);
  1596. if (clight->mode == RS::CANVAS_LIGHT_MODE_POINT) {
  1597. canvas->lights.erase(clight);
  1598. } else {
  1599. canvas->directional_lights.erase(clight);
  1600. }
  1601. }
  1602. if (!canvas_owner.owns(p_canvas)) {
  1603. p_canvas = RID();
  1604. }
  1605. clight->canvas = p_canvas;
  1606. if (clight->canvas.is_valid()) {
  1607. Canvas *canvas = canvas_owner.get_or_null(clight->canvas);
  1608. if (clight->mode == RS::CANVAS_LIGHT_MODE_POINT) {
  1609. canvas->lights.insert(clight);
  1610. } else {
  1611. canvas->directional_lights.insert(clight);
  1612. }
  1613. }
  1614. }
  1615. void RendererCanvasCull::canvas_light_set_enabled(RID p_light, bool p_enabled) {
  1616. RendererCanvasRender::Light *clight = canvas_light_owner.get_or_null(p_light);
  1617. ERR_FAIL_NULL(clight);
  1618. clight->enabled = p_enabled;
  1619. }
  1620. void RendererCanvasCull::canvas_light_set_texture_scale(RID p_light, float p_scale) {
  1621. RendererCanvasRender::Light *clight = canvas_light_owner.get_or_null(p_light);
  1622. ERR_FAIL_NULL(clight);
  1623. clight->scale = p_scale;
  1624. }
  1625. void RendererCanvasCull::canvas_light_set_transform(RID p_light, const Transform2D &p_transform) {
  1626. RendererCanvasRender::Light *clight = canvas_light_owner.get_or_null(p_light);
  1627. ERR_FAIL_NULL(clight);
  1628. if (_interpolation_data.interpolation_enabled && clight->interpolated) {
  1629. if (!clight->on_interpolate_transform_list) {
  1630. _interpolation_data.canvas_light_transform_update_list_curr->push_back(p_light);
  1631. clight->on_interpolate_transform_list = true;
  1632. } else {
  1633. DEV_ASSERT(_interpolation_data.canvas_light_transform_update_list_curr->size() > 0);
  1634. }
  1635. }
  1636. clight->xform_curr = p_transform;
  1637. }
  1638. void RendererCanvasCull::canvas_light_set_texture(RID p_light, RID p_texture) {
  1639. RendererCanvasRender::Light *clight = canvas_light_owner.get_or_null(p_light);
  1640. ERR_FAIL_NULL(clight);
  1641. if (clight->texture == p_texture) {
  1642. return;
  1643. }
  1644. clight->texture = p_texture;
  1645. clight->version++;
  1646. RSG::canvas_render->light_set_texture(clight->light_internal, p_texture);
  1647. }
  1648. void RendererCanvasCull::canvas_light_set_texture_offset(RID p_light, const Vector2 &p_offset) {
  1649. RendererCanvasRender::Light *clight = canvas_light_owner.get_or_null(p_light);
  1650. ERR_FAIL_NULL(clight);
  1651. clight->texture_offset = p_offset;
  1652. }
  1653. void RendererCanvasCull::canvas_light_set_color(RID p_light, const Color &p_color) {
  1654. RendererCanvasRender::Light *clight = canvas_light_owner.get_or_null(p_light);
  1655. ERR_FAIL_NULL(clight);
  1656. clight->color = p_color;
  1657. }
  1658. void RendererCanvasCull::canvas_light_set_height(RID p_light, float p_height) {
  1659. RendererCanvasRender::Light *clight = canvas_light_owner.get_or_null(p_light);
  1660. ERR_FAIL_NULL(clight);
  1661. clight->height = p_height;
  1662. }
  1663. void RendererCanvasCull::canvas_light_set_energy(RID p_light, float p_energy) {
  1664. RendererCanvasRender::Light *clight = canvas_light_owner.get_or_null(p_light);
  1665. ERR_FAIL_NULL(clight);
  1666. clight->energy = p_energy;
  1667. }
  1668. void RendererCanvasCull::canvas_light_set_z_range(RID p_light, int p_min_z, int p_max_z) {
  1669. RendererCanvasRender::Light *clight = canvas_light_owner.get_or_null(p_light);
  1670. ERR_FAIL_NULL(clight);
  1671. clight->z_min = p_min_z;
  1672. clight->z_max = p_max_z;
  1673. }
  1674. void RendererCanvasCull::canvas_light_set_layer_range(RID p_light, int p_min_layer, int p_max_layer) {
  1675. RendererCanvasRender::Light *clight = canvas_light_owner.get_or_null(p_light);
  1676. ERR_FAIL_NULL(clight);
  1677. clight->layer_max = p_max_layer;
  1678. clight->layer_min = p_min_layer;
  1679. }
  1680. void RendererCanvasCull::canvas_light_set_item_cull_mask(RID p_light, int p_mask) {
  1681. RendererCanvasRender::Light *clight = canvas_light_owner.get_or_null(p_light);
  1682. ERR_FAIL_NULL(clight);
  1683. clight->item_mask = p_mask;
  1684. }
  1685. void RendererCanvasCull::canvas_light_set_item_shadow_cull_mask(RID p_light, int p_mask) {
  1686. RendererCanvasRender::Light *clight = canvas_light_owner.get_or_null(p_light);
  1687. ERR_FAIL_NULL(clight);
  1688. clight->item_shadow_mask = p_mask;
  1689. }
  1690. void RendererCanvasCull::canvas_light_set_directional_distance(RID p_light, float p_distance) {
  1691. RendererCanvasRender::Light *clight = canvas_light_owner.get_or_null(p_light);
  1692. ERR_FAIL_NULL(clight);
  1693. clight->directional_distance = p_distance;
  1694. }
  1695. void RendererCanvasCull::canvas_light_set_blend_mode(RID p_light, RS::CanvasLightBlendMode p_mode) {
  1696. RendererCanvasRender::Light *clight = canvas_light_owner.get_or_null(p_light);
  1697. ERR_FAIL_NULL(clight);
  1698. clight->blend_mode = p_mode;
  1699. }
  1700. void RendererCanvasCull::canvas_light_set_shadow_enabled(RID p_light, bool p_enabled) {
  1701. RendererCanvasRender::Light *clight = canvas_light_owner.get_or_null(p_light);
  1702. ERR_FAIL_NULL(clight);
  1703. if (clight->use_shadow == p_enabled) {
  1704. return;
  1705. }
  1706. clight->use_shadow = p_enabled;
  1707. clight->version++;
  1708. RSG::canvas_render->light_set_use_shadow(clight->light_internal, clight->use_shadow);
  1709. }
  1710. void RendererCanvasCull::canvas_light_set_shadow_filter(RID p_light, RS::CanvasLightShadowFilter p_filter) {
  1711. RendererCanvasRender::Light *clight = canvas_light_owner.get_or_null(p_light);
  1712. ERR_FAIL_NULL(clight);
  1713. clight->shadow_filter = p_filter;
  1714. }
  1715. void RendererCanvasCull::canvas_light_set_shadow_color(RID p_light, const Color &p_color) {
  1716. RendererCanvasRender::Light *clight = canvas_light_owner.get_or_null(p_light);
  1717. ERR_FAIL_NULL(clight);
  1718. clight->shadow_color = p_color;
  1719. }
  1720. void RendererCanvasCull::canvas_light_set_shadow_smooth(RID p_light, float p_smooth) {
  1721. RendererCanvasRender::Light *clight = canvas_light_owner.get_or_null(p_light);
  1722. ERR_FAIL_NULL(clight);
  1723. clight->shadow_smooth = p_smooth;
  1724. }
  1725. void RendererCanvasCull::canvas_light_set_interpolated(RID p_light, bool p_interpolated) {
  1726. RendererCanvasRender::Light *clight = canvas_light_owner.get_or_null(p_light);
  1727. ERR_FAIL_NULL(clight);
  1728. clight->interpolated = p_interpolated;
  1729. }
  1730. void RendererCanvasCull::canvas_light_reset_physics_interpolation(RID p_light) {
  1731. RendererCanvasRender::Light *clight = canvas_light_owner.get_or_null(p_light);
  1732. ERR_FAIL_NULL(clight);
  1733. clight->xform_prev = clight->xform_curr;
  1734. }
  1735. void RendererCanvasCull::canvas_light_transform_physics_interpolation(RID p_light, const Transform2D &p_transform) {
  1736. RendererCanvasRender::Light *clight = canvas_light_owner.get_or_null(p_light);
  1737. ERR_FAIL_NULL(clight);
  1738. clight->xform_prev = p_transform * clight->xform_prev;
  1739. clight->xform_curr = p_transform * clight->xform_curr;
  1740. }
  1741. RID RendererCanvasCull::canvas_light_occluder_allocate() {
  1742. return canvas_light_occluder_owner.allocate_rid();
  1743. }
  1744. void RendererCanvasCull::canvas_light_occluder_initialize(RID p_rid) {
  1745. return canvas_light_occluder_owner.initialize_rid(p_rid);
  1746. }
  1747. void RendererCanvasCull::canvas_light_occluder_attach_to_canvas(RID p_occluder, RID p_canvas) {
  1748. RendererCanvasRender::LightOccluderInstance *occluder = canvas_light_occluder_owner.get_or_null(p_occluder);
  1749. ERR_FAIL_NULL(occluder);
  1750. if (occluder->canvas.is_valid()) {
  1751. Canvas *canvas = canvas_owner.get_or_null(occluder->canvas);
  1752. canvas->occluders.erase(occluder);
  1753. }
  1754. if (!canvas_owner.owns(p_canvas)) {
  1755. p_canvas = RID();
  1756. }
  1757. occluder->canvas = p_canvas;
  1758. if (occluder->canvas.is_valid()) {
  1759. Canvas *canvas = canvas_owner.get_or_null(occluder->canvas);
  1760. canvas->occluders.insert(occluder);
  1761. }
  1762. }
  1763. void RendererCanvasCull::canvas_light_occluder_set_enabled(RID p_occluder, bool p_enabled) {
  1764. RendererCanvasRender::LightOccluderInstance *occluder = canvas_light_occluder_owner.get_or_null(p_occluder);
  1765. ERR_FAIL_NULL(occluder);
  1766. occluder->enabled = p_enabled;
  1767. }
  1768. void RendererCanvasCull::canvas_light_occluder_set_polygon(RID p_occluder, RID p_polygon) {
  1769. RendererCanvasRender::LightOccluderInstance *occluder = canvas_light_occluder_owner.get_or_null(p_occluder);
  1770. ERR_FAIL_NULL(occluder);
  1771. if (occluder->polygon.is_valid()) {
  1772. LightOccluderPolygon *occluder_poly = canvas_light_occluder_polygon_owner.get_or_null(occluder->polygon);
  1773. if (occluder_poly) {
  1774. occluder_poly->owners.erase(occluder);
  1775. }
  1776. }
  1777. occluder->polygon = p_polygon;
  1778. occluder->occluder = RID();
  1779. if (occluder->polygon.is_valid()) {
  1780. LightOccluderPolygon *occluder_poly = canvas_light_occluder_polygon_owner.get_or_null(p_polygon);
  1781. if (!occluder_poly) {
  1782. occluder->polygon = RID();
  1783. ERR_FAIL_NULL(occluder_poly);
  1784. } else {
  1785. occluder_poly->owners.insert(occluder);
  1786. occluder->occluder = occluder_poly->occluder;
  1787. occluder->aabb_cache = occluder_poly->aabb;
  1788. occluder->cull_cache = occluder_poly->cull_mode;
  1789. }
  1790. }
  1791. }
  1792. void RendererCanvasCull::canvas_light_occluder_set_as_sdf_collision(RID p_occluder, bool p_enable) {
  1793. RendererCanvasRender::LightOccluderInstance *occluder = canvas_light_occluder_owner.get_or_null(p_occluder);
  1794. ERR_FAIL_NULL(occluder);
  1795. occluder->sdf_collision = p_enable;
  1796. }
  1797. void RendererCanvasCull::canvas_light_occluder_set_transform(RID p_occluder, const Transform2D &p_xform) {
  1798. RendererCanvasRender::LightOccluderInstance *occluder = canvas_light_occluder_owner.get_or_null(p_occluder);
  1799. ERR_FAIL_NULL(occluder);
  1800. if (_interpolation_data.interpolation_enabled && occluder->interpolated) {
  1801. if (!occluder->on_interpolate_transform_list) {
  1802. _interpolation_data.canvas_light_occluder_transform_update_list_curr->push_back(p_occluder);
  1803. occluder->on_interpolate_transform_list = true;
  1804. } else {
  1805. DEV_ASSERT(_interpolation_data.canvas_light_occluder_transform_update_list_curr->size() > 0);
  1806. }
  1807. }
  1808. occluder->xform_curr = p_xform;
  1809. }
  1810. void RendererCanvasCull::canvas_light_occluder_set_light_mask(RID p_occluder, int p_mask) {
  1811. RendererCanvasRender::LightOccluderInstance *occluder = canvas_light_occluder_owner.get_or_null(p_occluder);
  1812. ERR_FAIL_NULL(occluder);
  1813. occluder->light_mask = p_mask;
  1814. }
  1815. void RendererCanvasCull::canvas_light_occluder_set_interpolated(RID p_occluder, bool p_interpolated) {
  1816. RendererCanvasRender::LightOccluderInstance *occluder = canvas_light_occluder_owner.get_or_null(p_occluder);
  1817. ERR_FAIL_NULL(occluder);
  1818. occluder->interpolated = p_interpolated;
  1819. }
  1820. void RendererCanvasCull::canvas_light_occluder_reset_physics_interpolation(RID p_occluder) {
  1821. RendererCanvasRender::LightOccluderInstance *occluder = canvas_light_occluder_owner.get_or_null(p_occluder);
  1822. ERR_FAIL_NULL(occluder);
  1823. occluder->xform_prev = occluder->xform_curr;
  1824. }
  1825. void RendererCanvasCull::canvas_light_occluder_transform_physics_interpolation(RID p_occluder, const Transform2D &p_transform) {
  1826. RendererCanvasRender::LightOccluderInstance *occluder = canvas_light_occluder_owner.get_or_null(p_occluder);
  1827. ERR_FAIL_NULL(occluder);
  1828. occluder->xform_prev = p_transform * occluder->xform_prev;
  1829. occluder->xform_curr = p_transform * occluder->xform_curr;
  1830. }
  1831. RID RendererCanvasCull::canvas_occluder_polygon_allocate() {
  1832. return canvas_light_occluder_polygon_owner.allocate_rid();
  1833. }
  1834. void RendererCanvasCull::canvas_occluder_polygon_initialize(RID p_rid) {
  1835. canvas_light_occluder_polygon_owner.initialize_rid(p_rid);
  1836. LightOccluderPolygon *occluder_poly = canvas_light_occluder_polygon_owner.get_or_null(p_rid);
  1837. occluder_poly->occluder = RSG::canvas_render->occluder_polygon_create();
  1838. }
  1839. void RendererCanvasCull::canvas_occluder_polygon_set_shape(RID p_occluder_polygon, const Vector<Vector2> &p_shape, bool p_closed) {
  1840. LightOccluderPolygon *occluder_poly = canvas_light_occluder_polygon_owner.get_or_null(p_occluder_polygon);
  1841. ERR_FAIL_NULL(occluder_poly);
  1842. uint32_t pc = p_shape.size();
  1843. ERR_FAIL_COND(pc < 2);
  1844. occluder_poly->aabb = Rect2();
  1845. const Vector2 *r = p_shape.ptr();
  1846. for (uint32_t i = 0; i < pc; i++) {
  1847. if (i == 0) {
  1848. occluder_poly->aabb.position = r[i];
  1849. } else {
  1850. occluder_poly->aabb.expand_to(r[i]);
  1851. }
  1852. }
  1853. RSG::canvas_render->occluder_polygon_set_shape(occluder_poly->occluder, p_shape, p_closed);
  1854. for (RendererCanvasRender::LightOccluderInstance *E : occluder_poly->owners) {
  1855. E->aabb_cache = occluder_poly->aabb;
  1856. }
  1857. }
  1858. void RendererCanvasCull::canvas_occluder_polygon_set_cull_mode(RID p_occluder_polygon, RS::CanvasOccluderPolygonCullMode p_mode) {
  1859. LightOccluderPolygon *occluder_poly = canvas_light_occluder_polygon_owner.get_or_null(p_occluder_polygon);
  1860. ERR_FAIL_NULL(occluder_poly);
  1861. occluder_poly->cull_mode = p_mode;
  1862. RSG::canvas_render->occluder_polygon_set_cull_mode(occluder_poly->occluder, p_mode);
  1863. for (RendererCanvasRender::LightOccluderInstance *E : occluder_poly->owners) {
  1864. E->cull_cache = p_mode;
  1865. }
  1866. }
  1867. void RendererCanvasCull::canvas_set_shadow_texture_size(int p_size) {
  1868. RSG::canvas_render->set_shadow_texture_size(p_size);
  1869. }
  1870. RID RendererCanvasCull::canvas_texture_allocate() {
  1871. return RSG::texture_storage->canvas_texture_allocate();
  1872. }
  1873. void RendererCanvasCull::canvas_texture_initialize(RID p_rid) {
  1874. RSG::texture_storage->canvas_texture_initialize(p_rid);
  1875. }
  1876. void RendererCanvasCull::canvas_texture_set_channel(RID p_canvas_texture, RS::CanvasTextureChannel p_channel, RID p_texture) {
  1877. RSG::texture_storage->canvas_texture_set_channel(p_canvas_texture, p_channel, p_texture);
  1878. }
  1879. void RendererCanvasCull::canvas_texture_set_shading_parameters(RID p_canvas_texture, const Color &p_base_color, float p_shininess) {
  1880. RSG::texture_storage->canvas_texture_set_shading_parameters(p_canvas_texture, p_base_color, p_shininess);
  1881. }
  1882. void RendererCanvasCull::canvas_texture_set_texture_filter(RID p_canvas_texture, RS::CanvasItemTextureFilter p_filter) {
  1883. RSG::texture_storage->canvas_texture_set_texture_filter(p_canvas_texture, p_filter);
  1884. }
  1885. void RendererCanvasCull::canvas_texture_set_texture_repeat(RID p_canvas_texture, RS::CanvasItemTextureRepeat p_repeat) {
  1886. RSG::texture_storage->canvas_texture_set_texture_repeat(p_canvas_texture, p_repeat);
  1887. }
  1888. void RendererCanvasCull::canvas_item_set_default_texture_filter(RID p_item, RS::CanvasItemTextureFilter p_filter) {
  1889. Item *ci = canvas_item_owner.get_or_null(p_item);
  1890. ERR_FAIL_NULL(ci);
  1891. ci->texture_filter = p_filter;
  1892. }
  1893. void RendererCanvasCull::canvas_item_set_default_texture_repeat(RID p_item, RS::CanvasItemTextureRepeat p_repeat) {
  1894. Item *ci = canvas_item_owner.get_or_null(p_item);
  1895. ERR_FAIL_NULL(ci);
  1896. ci->texture_repeat = p_repeat;
  1897. }
  1898. void RendererCanvasCull::update_visibility_notifiers() {
  1899. SelfList<Item::VisibilityNotifierData> *E = visibility_notifier_list.first();
  1900. while (E) {
  1901. SelfList<Item::VisibilityNotifierData> *N = E->next();
  1902. Item::VisibilityNotifierData *visibility_notifier = E->self();
  1903. if (visibility_notifier->just_visible) {
  1904. visibility_notifier->just_visible = false;
  1905. if (visibility_notifier->enter_callable.is_valid()) {
  1906. if (RSG::threaded) {
  1907. visibility_notifier->enter_callable.call_deferred();
  1908. } else {
  1909. visibility_notifier->enter_callable.call();
  1910. }
  1911. }
  1912. } else {
  1913. if (visibility_notifier->visible_in_frame != RSG::rasterizer->get_frame_number()) {
  1914. visibility_notifier_list.remove(E);
  1915. if (visibility_notifier->exit_callable.is_valid()) {
  1916. if (RSG::threaded) {
  1917. visibility_notifier->exit_callable.call_deferred();
  1918. } else {
  1919. visibility_notifier->exit_callable.call();
  1920. }
  1921. }
  1922. }
  1923. }
  1924. E = N;
  1925. }
  1926. }
  1927. Rect2 RendererCanvasCull::_debug_canvas_item_get_rect(RID p_item) {
  1928. Item *canvas_item = canvas_item_owner.get_or_null(p_item);
  1929. ERR_FAIL_NULL_V(canvas_item, Rect2());
  1930. return canvas_item->get_rect();
  1931. }
  1932. bool RendererCanvasCull::free(RID p_rid) {
  1933. if (canvas_owner.owns(p_rid)) {
  1934. Canvas *canvas = canvas_owner.get_or_null(p_rid);
  1935. ERR_FAIL_NULL_V(canvas, false);
  1936. while (canvas->viewports.size()) {
  1937. RendererViewport::Viewport *vp = RSG::viewport->viewport_owner.get_or_null(*canvas->viewports.begin());
  1938. ERR_FAIL_NULL_V(vp, true);
  1939. HashMap<RID, RendererViewport::Viewport::CanvasData>::Iterator E = vp->canvas_map.find(p_rid);
  1940. ERR_FAIL_COND_V(!E, true);
  1941. vp->canvas_map.erase(p_rid);
  1942. canvas->viewports.erase(*canvas->viewports.begin());
  1943. }
  1944. for (int i = 0; i < canvas->child_items.size(); i++) {
  1945. canvas->child_items[i].item->parent = RID();
  1946. }
  1947. for (RendererCanvasRender::Light *E : canvas->lights) {
  1948. E->canvas = RID();
  1949. }
  1950. for (RendererCanvasRender::LightOccluderInstance *E : canvas->occluders) {
  1951. E->canvas = RID();
  1952. }
  1953. canvas_owner.free(p_rid);
  1954. } else if (canvas_item_owner.owns(p_rid)) {
  1955. Item *canvas_item = canvas_item_owner.get_or_null(p_rid);
  1956. ERR_FAIL_NULL_V(canvas_item, true);
  1957. _interpolation_data.notify_free_canvas_item(p_rid, *canvas_item);
  1958. if (canvas_item->parent.is_valid()) {
  1959. if (canvas_owner.owns(canvas_item->parent)) {
  1960. Canvas *canvas = canvas_owner.get_or_null(canvas_item->parent);
  1961. canvas->erase_item(canvas_item);
  1962. } else if (canvas_item_owner.owns(canvas_item->parent)) {
  1963. Item *item_owner = canvas_item_owner.get_or_null(canvas_item->parent);
  1964. item_owner->child_items.erase(canvas_item);
  1965. if (item_owner->sort_y) {
  1966. _mark_ysort_dirty(item_owner);
  1967. }
  1968. }
  1969. }
  1970. for (int i = 0; i < canvas_item->child_items.size(); i++) {
  1971. canvas_item->child_items[i]->parent = RID();
  1972. }
  1973. if (canvas_item->visibility_notifier != nullptr) {
  1974. visibility_notifier_allocator.free(canvas_item->visibility_notifier);
  1975. }
  1976. /*
  1977. if (canvas_item->material) {
  1978. canvas_item->material->owners.erase(canvas_item);
  1979. }
  1980. */
  1981. if (canvas_item->canvas_group != nullptr) {
  1982. memdelete(canvas_item->canvas_group);
  1983. canvas_item->canvas_group = nullptr;
  1984. }
  1985. canvas_item_owner.free(p_rid);
  1986. } else if (canvas_light_owner.owns(p_rid)) {
  1987. RendererCanvasRender::Light *canvas_light = canvas_light_owner.get_or_null(p_rid);
  1988. ERR_FAIL_NULL_V(canvas_light, true);
  1989. _interpolation_data.notify_free_canvas_light(p_rid, *canvas_light);
  1990. if (canvas_light->canvas.is_valid()) {
  1991. Canvas *canvas = canvas_owner.get_or_null(canvas_light->canvas);
  1992. if (canvas) {
  1993. canvas->lights.erase(canvas_light);
  1994. }
  1995. }
  1996. RSG::canvas_render->free(canvas_light->light_internal);
  1997. canvas_light_owner.free(p_rid);
  1998. } else if (canvas_light_occluder_owner.owns(p_rid)) {
  1999. RendererCanvasRender::LightOccluderInstance *occluder = canvas_light_occluder_owner.get_or_null(p_rid);
  2000. ERR_FAIL_NULL_V(occluder, true);
  2001. _interpolation_data.notify_free_canvas_light_occluder(p_rid, *occluder);
  2002. if (occluder->polygon.is_valid()) {
  2003. LightOccluderPolygon *occluder_poly = canvas_light_occluder_polygon_owner.get_or_null(occluder->polygon);
  2004. if (occluder_poly) {
  2005. occluder_poly->owners.erase(occluder);
  2006. }
  2007. }
  2008. if (occluder->canvas.is_valid() && canvas_owner.owns(occluder->canvas)) {
  2009. Canvas *canvas = canvas_owner.get_or_null(occluder->canvas);
  2010. canvas->occluders.erase(occluder);
  2011. }
  2012. canvas_light_occluder_owner.free(p_rid);
  2013. } else if (canvas_light_occluder_polygon_owner.owns(p_rid)) {
  2014. LightOccluderPolygon *occluder_poly = canvas_light_occluder_polygon_owner.get_or_null(p_rid);
  2015. ERR_FAIL_NULL_V(occluder_poly, true);
  2016. RSG::canvas_render->free(occluder_poly->occluder);
  2017. while (occluder_poly->owners.size()) {
  2018. (*occluder_poly->owners.begin())->polygon = RID();
  2019. occluder_poly->owners.remove(occluder_poly->owners.begin());
  2020. }
  2021. canvas_light_occluder_polygon_owner.free(p_rid);
  2022. } else {
  2023. return false;
  2024. }
  2025. return true;
  2026. }
  2027. template <typename T>
  2028. void RendererCanvasCull::_free_rids(T &p_owner, const char *p_type) {
  2029. List<RID> owned;
  2030. p_owner.get_owned_list(&owned);
  2031. if (owned.size()) {
  2032. if (owned.size() == 1) {
  2033. WARN_PRINT(vformat("1 RID of type \"%s\" was leaked.", p_type));
  2034. } else {
  2035. WARN_PRINT(vformat("%d RIDs of type \"%s\" were leaked.", owned.size(), p_type));
  2036. }
  2037. for (const RID &E : owned) {
  2038. free(E);
  2039. }
  2040. }
  2041. }
  2042. void RendererCanvasCull::finalize() {
  2043. _free_rids(canvas_owner, "Canvas");
  2044. _free_rids(canvas_item_owner, "CanvasItem");
  2045. _free_rids(canvas_light_owner, "CanvasLight");
  2046. _free_rids(canvas_light_occluder_owner, "CanvasLightOccluder");
  2047. _free_rids(canvas_light_occluder_polygon_owner, "CanvasLightOccluderPolygon");
  2048. }
  2049. void RendererCanvasCull::tick() {
  2050. if (_interpolation_data.interpolation_enabled) {
  2051. update_interpolation_tick(true);
  2052. }
  2053. }
  2054. void RendererCanvasCull::update_interpolation_tick(bool p_process) {
  2055. #define GODOT_UPDATE_INTERPOLATION_TICK(m_list_prev, m_list_curr, m_type, m_owner_list) \
  2056. /* Detect any that were on the previous transform list that are no longer active. */ \
  2057. for (unsigned int n = 0; n < _interpolation_data.m_list_prev->size(); n++) { \
  2058. const RID &rid = (*_interpolation_data.m_list_prev)[n]; \
  2059. m_type *item = m_owner_list.get_or_null(rid); \
  2060. /* no longer active? (either the instance deleted or no longer being transformed) */ \
  2061. if (item && !item->on_interpolate_transform_list) { \
  2062. item->xform_prev = item->xform_curr; \
  2063. } \
  2064. } \
  2065. /* and now for any in the transform list (being actively interpolated), */ \
  2066. /* keep the previous transform value up to date and ready for next tick */ \
  2067. if (p_process) { \
  2068. for (unsigned int n = 0; n < _interpolation_data.m_list_curr->size(); n++) { \
  2069. const RID &rid = (*_interpolation_data.m_list_curr)[n]; \
  2070. m_type *item = m_owner_list.get_or_null(rid); \
  2071. if (item) { \
  2072. item->xform_prev = item->xform_curr; \
  2073. item->on_interpolate_transform_list = false; \
  2074. } \
  2075. } \
  2076. } \
  2077. SWAP(_interpolation_data.m_list_curr, _interpolation_data.m_list_prev); \
  2078. _interpolation_data.m_list_curr->clear();
  2079. GODOT_UPDATE_INTERPOLATION_TICK(canvas_item_transform_update_list_prev, canvas_item_transform_update_list_curr, Item, canvas_item_owner);
  2080. GODOT_UPDATE_INTERPOLATION_TICK(canvas_light_transform_update_list_prev, canvas_light_transform_update_list_curr, RendererCanvasRender::Light, canvas_light_owner);
  2081. GODOT_UPDATE_INTERPOLATION_TICK(canvas_light_occluder_transform_update_list_prev, canvas_light_occluder_transform_update_list_curr, RendererCanvasRender::LightOccluderInstance, canvas_light_occluder_owner);
  2082. #undef GODOT_UPDATE_INTERPOLATION_TICK
  2083. }
  2084. void RendererCanvasCull::InterpolationData::notify_free_canvas_item(RID p_rid, RendererCanvasCull::Item &r_canvas_item) {
  2085. r_canvas_item.on_interpolate_transform_list = false;
  2086. if (!interpolation_enabled) {
  2087. return;
  2088. }
  2089. // If the instance was on any of the lists, remove.
  2090. canvas_item_transform_update_list_curr->erase_multiple_unordered(p_rid);
  2091. canvas_item_transform_update_list_prev->erase_multiple_unordered(p_rid);
  2092. }
  2093. void RendererCanvasCull::InterpolationData::notify_free_canvas_light(RID p_rid, RendererCanvasRender::Light &r_canvas_light) {
  2094. r_canvas_light.on_interpolate_transform_list = false;
  2095. if (!interpolation_enabled) {
  2096. return;
  2097. }
  2098. // If the instance was on any of the lists, remove.
  2099. canvas_light_transform_update_list_curr->erase_multiple_unordered(p_rid);
  2100. canvas_light_transform_update_list_prev->erase_multiple_unordered(p_rid);
  2101. }
  2102. void RendererCanvasCull::InterpolationData::notify_free_canvas_light_occluder(RID p_rid, RendererCanvasRender::LightOccluderInstance &r_canvas_light_occluder) {
  2103. r_canvas_light_occluder.on_interpolate_transform_list = false;
  2104. if (!interpolation_enabled) {
  2105. return;
  2106. }
  2107. // If the instance was on any of the lists, remove.
  2108. canvas_light_occluder_transform_update_list_curr->erase_multiple_unordered(p_rid);
  2109. canvas_light_occluder_transform_update_list_prev->erase_multiple_unordered(p_rid);
  2110. }
  2111. RendererCanvasCull::RendererCanvasCull() {
  2112. z_list = (RendererCanvasRender::Item **)memalloc(z_range * sizeof(RendererCanvasRender::Item *));
  2113. z_last_list = (RendererCanvasRender::Item **)memalloc(z_range * sizeof(RendererCanvasRender::Item *));
  2114. disable_scale = false;
  2115. debug_redraw_time = GLOBAL_DEF("debug/canvas_items/debug_redraw_time", 1.0);
  2116. debug_redraw_color = GLOBAL_DEF("debug/canvas_items/debug_redraw_color", Color(1.0, 0.2, 0.2, 0.5));
  2117. }
  2118. RendererCanvasCull::~RendererCanvasCull() {
  2119. memfree(z_list);
  2120. memfree(z_last_list);
  2121. }