renderer_canvas_cull.cpp 91 KB

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