renderer_canvas_cull.cpp 75 KB

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