rasterizer_canvas_gles3.cpp 112 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823
  1. /**************************************************************************/
  2. /* rasterizer_canvas_gles3.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 "rasterizer_canvas_gles3.h"
  31. #ifdef GLES3_ENABLED
  32. #include "core/os/os.h"
  33. #include "rasterizer_scene_gles3.h"
  34. #include "core/config/project_settings.h"
  35. #include "core/math/geometry_2d.h"
  36. #include "servers/rendering/rendering_server_default.h"
  37. #include "storage/config.h"
  38. #include "storage/material_storage.h"
  39. #include "storage/mesh_storage.h"
  40. #include "storage/particles_storage.h"
  41. #include "storage/texture_storage.h"
  42. void RasterizerCanvasGLES3::_update_transform_2d_to_mat4(const Transform2D &p_transform, float *p_mat4) {
  43. p_mat4[0] = p_transform.columns[0][0];
  44. p_mat4[1] = p_transform.columns[0][1];
  45. p_mat4[2] = 0;
  46. p_mat4[3] = 0;
  47. p_mat4[4] = p_transform.columns[1][0];
  48. p_mat4[5] = p_transform.columns[1][1];
  49. p_mat4[6] = 0;
  50. p_mat4[7] = 0;
  51. p_mat4[8] = 0;
  52. p_mat4[9] = 0;
  53. p_mat4[10] = 1;
  54. p_mat4[11] = 0;
  55. p_mat4[12] = p_transform.columns[2][0];
  56. p_mat4[13] = p_transform.columns[2][1];
  57. p_mat4[14] = 0;
  58. p_mat4[15] = 1;
  59. }
  60. void RasterizerCanvasGLES3::_update_transform_2d_to_mat2x4(const Transform2D &p_transform, float *p_mat2x4) {
  61. p_mat2x4[0] = p_transform.columns[0][0];
  62. p_mat2x4[1] = p_transform.columns[1][0];
  63. p_mat2x4[2] = 0;
  64. p_mat2x4[3] = p_transform.columns[2][0];
  65. p_mat2x4[4] = p_transform.columns[0][1];
  66. p_mat2x4[5] = p_transform.columns[1][1];
  67. p_mat2x4[6] = 0;
  68. p_mat2x4[7] = p_transform.columns[2][1];
  69. }
  70. void RasterizerCanvasGLES3::_update_transform_2d_to_mat2x3(const Transform2D &p_transform, float *p_mat2x3) {
  71. p_mat2x3[0] = p_transform.columns[0][0];
  72. p_mat2x3[1] = p_transform.columns[0][1];
  73. p_mat2x3[2] = p_transform.columns[1][0];
  74. p_mat2x3[3] = p_transform.columns[1][1];
  75. p_mat2x3[4] = p_transform.columns[2][0];
  76. p_mat2x3[5] = p_transform.columns[2][1];
  77. }
  78. void RasterizerCanvasGLES3::_update_transform_to_mat4(const Transform3D &p_transform, float *p_mat4) {
  79. p_mat4[0] = p_transform.basis.rows[0][0];
  80. p_mat4[1] = p_transform.basis.rows[1][0];
  81. p_mat4[2] = p_transform.basis.rows[2][0];
  82. p_mat4[3] = 0;
  83. p_mat4[4] = p_transform.basis.rows[0][1];
  84. p_mat4[5] = p_transform.basis.rows[1][1];
  85. p_mat4[6] = p_transform.basis.rows[2][1];
  86. p_mat4[7] = 0;
  87. p_mat4[8] = p_transform.basis.rows[0][2];
  88. p_mat4[9] = p_transform.basis.rows[1][2];
  89. p_mat4[10] = p_transform.basis.rows[2][2];
  90. p_mat4[11] = 0;
  91. p_mat4[12] = p_transform.origin.x;
  92. p_mat4[13] = p_transform.origin.y;
  93. p_mat4[14] = p_transform.origin.z;
  94. p_mat4[15] = 1;
  95. }
  96. void RasterizerCanvasGLES3::canvas_render_items(RID p_to_render_target, Item *p_item_list, const Color &p_modulate, Light *p_light_list, Light *p_directional_light_list, const Transform2D &p_canvas_transform, RS::CanvasItemTextureFilter p_default_filter, RS::CanvasItemTextureRepeat p_default_repeat, bool p_snap_2d_vertices_to_pixel, bool &r_sdf_used) {
  97. GLES3::TextureStorage *texture_storage = GLES3::TextureStorage::get_singleton();
  98. GLES3::MaterialStorage *material_storage = GLES3::MaterialStorage::get_singleton();
  99. GLES3::MeshStorage *mesh_storage = GLES3::MeshStorage::get_singleton();
  100. Transform2D canvas_transform_inverse = p_canvas_transform.affine_inverse();
  101. // Clear out any state that may have been left from the 3D pass.
  102. reset_canvas();
  103. if (state.canvas_instance_data_buffers[state.current_data_buffer_index].fence != GLsync()) {
  104. GLint syncStatus;
  105. glGetSynciv(state.canvas_instance_data_buffers[state.current_data_buffer_index].fence, GL_SYNC_STATUS, 1, nullptr, &syncStatus);
  106. if (syncStatus == GL_UNSIGNALED) {
  107. // If older than 2 frames, wait for sync OpenGL can have up to 3 frames in flight, any more and we need to sync anyway.
  108. if (state.canvas_instance_data_buffers[state.current_data_buffer_index].last_frame_used < RSG::rasterizer->get_frame_number() - 2) {
  109. #ifndef WEB_ENABLED
  110. // On web, we do nothing as the glSubBufferData will force a sync anyway and WebGL does not like waiting.
  111. glClientWaitSync(state.canvas_instance_data_buffers[state.current_data_buffer_index].fence, 0, 100000000); // wait for up to 100ms
  112. #endif
  113. state.canvas_instance_data_buffers[state.current_data_buffer_index].last_frame_used = RSG::rasterizer->get_frame_number();
  114. glDeleteSync(state.canvas_instance_data_buffers[state.current_data_buffer_index].fence);
  115. state.canvas_instance_data_buffers[state.current_data_buffer_index].fence = GLsync();
  116. } else {
  117. // Used in last frame or frame before that. OpenGL can get up to two frames behind, so these buffers may still be in use
  118. // Allocate a new buffer and use that.
  119. _allocate_instance_data_buffer();
  120. }
  121. } else {
  122. // Already finished all rendering commands, we can use it.
  123. state.canvas_instance_data_buffers[state.current_data_buffer_index].last_frame_used = RSG::rasterizer->get_frame_number();
  124. glDeleteSync(state.canvas_instance_data_buffers[state.current_data_buffer_index].fence);
  125. state.canvas_instance_data_buffers[state.current_data_buffer_index].fence = GLsync();
  126. }
  127. }
  128. //setup directional lights if exist
  129. uint32_t light_count = 0;
  130. uint32_t directional_light_count = 0;
  131. {
  132. Light *l = p_directional_light_list;
  133. uint32_t index = 0;
  134. while (l) {
  135. if (index == data.max_lights_per_render) {
  136. l->render_index_cache = -1;
  137. l = l->next_ptr;
  138. continue;
  139. }
  140. CanvasLight *clight = canvas_light_owner.get_or_null(l->light_internal);
  141. if (!clight) { //unused or invalid texture
  142. l->render_index_cache = -1;
  143. l = l->next_ptr;
  144. ERR_CONTINUE(!clight);
  145. }
  146. Vector2 canvas_light_dir = l->xform_cache.columns[1].normalized();
  147. state.light_uniforms[index].position[0] = -canvas_light_dir.x;
  148. state.light_uniforms[index].position[1] = -canvas_light_dir.y;
  149. _update_transform_2d_to_mat2x4(clight->shadow.directional_xform, state.light_uniforms[index].shadow_matrix);
  150. state.light_uniforms[index].height = l->height; //0..1 here
  151. for (int i = 0; i < 4; i++) {
  152. state.light_uniforms[index].shadow_color[i] = uint8_t(CLAMP(int32_t(l->shadow_color[i] * 255.0), 0, 255));
  153. state.light_uniforms[index].color[i] = l->color[i];
  154. }
  155. state.light_uniforms[index].color[3] *= l->energy; //use alpha for energy, so base color can go separate
  156. if (state.shadow_fb != 0) {
  157. state.light_uniforms[index].shadow_pixel_size = (1.0 / state.shadow_texture_size) * (1.0 + l->shadow_smooth);
  158. state.light_uniforms[index].shadow_z_far_inv = 1.0 / clight->shadow.z_far;
  159. state.light_uniforms[index].shadow_y_ofs = clight->shadow.y_offset;
  160. } else {
  161. state.light_uniforms[index].shadow_pixel_size = 1.0;
  162. state.light_uniforms[index].shadow_z_far_inv = 1.0;
  163. state.light_uniforms[index].shadow_y_ofs = 0;
  164. }
  165. state.light_uniforms[index].flags = l->blend_mode << LIGHT_FLAGS_BLEND_SHIFT;
  166. state.light_uniforms[index].flags |= l->shadow_filter << LIGHT_FLAGS_FILTER_SHIFT;
  167. if (clight->shadow.enabled) {
  168. state.light_uniforms[index].flags |= LIGHT_FLAGS_HAS_SHADOW;
  169. }
  170. l->render_index_cache = index;
  171. index++;
  172. l = l->next_ptr;
  173. }
  174. light_count = index;
  175. directional_light_count = light_count;
  176. state.using_directional_lights = directional_light_count > 0;
  177. }
  178. //setup lights if exist
  179. {
  180. Light *l = p_light_list;
  181. uint32_t index = light_count;
  182. while (l) {
  183. if (index == data.max_lights_per_render) {
  184. l->render_index_cache = -1;
  185. l = l->next_ptr;
  186. continue;
  187. }
  188. CanvasLight *clight = canvas_light_owner.get_or_null(l->light_internal);
  189. if (!clight) { //unused or invalid texture
  190. l->render_index_cache = -1;
  191. l = l->next_ptr;
  192. ERR_CONTINUE(!clight);
  193. }
  194. Vector2 canvas_light_pos = p_canvas_transform.xform(l->xform.get_origin()); //convert light position to canvas coordinates, as all computation is done in canvas coords to avoid precision loss
  195. state.light_uniforms[index].position[0] = canvas_light_pos.x;
  196. state.light_uniforms[index].position[1] = canvas_light_pos.y;
  197. _update_transform_2d_to_mat2x4(l->light_shader_xform.affine_inverse(), state.light_uniforms[index].matrix);
  198. _update_transform_2d_to_mat2x4(l->xform_cache.affine_inverse(), state.light_uniforms[index].shadow_matrix);
  199. state.light_uniforms[index].height = l->height * (p_canvas_transform.columns[0].length() + p_canvas_transform.columns[1].length()) * 0.5; //approximate height conversion to the canvas size, since all calculations are done in canvas coords to avoid precision loss
  200. for (int i = 0; i < 4; i++) {
  201. state.light_uniforms[index].shadow_color[i] = uint8_t(CLAMP(int32_t(l->shadow_color[i] * 255.0), 0, 255));
  202. state.light_uniforms[index].color[i] = l->color[i];
  203. }
  204. state.light_uniforms[index].color[3] *= l->energy; //use alpha for energy, so base color can go separate
  205. if (state.shadow_fb != 0) {
  206. state.light_uniforms[index].shadow_pixel_size = (1.0 / state.shadow_texture_size) * (1.0 + l->shadow_smooth);
  207. state.light_uniforms[index].shadow_z_far_inv = 1.0 / clight->shadow.z_far;
  208. state.light_uniforms[index].shadow_y_ofs = clight->shadow.y_offset;
  209. } else {
  210. state.light_uniforms[index].shadow_pixel_size = 1.0;
  211. state.light_uniforms[index].shadow_z_far_inv = 1.0;
  212. state.light_uniforms[index].shadow_y_ofs = 0;
  213. }
  214. state.light_uniforms[index].flags = l->blend_mode << LIGHT_FLAGS_BLEND_SHIFT;
  215. state.light_uniforms[index].flags |= l->shadow_filter << LIGHT_FLAGS_FILTER_SHIFT;
  216. if (clight->shadow.enabled) {
  217. state.light_uniforms[index].flags |= LIGHT_FLAGS_HAS_SHADOW;
  218. }
  219. if (clight->texture.is_valid()) {
  220. Rect2 atlas_rect = GLES3::TextureStorage::get_singleton()->texture_atlas_get_texture_rect(clight->texture);
  221. state.light_uniforms[index].atlas_rect[0] = atlas_rect.position.x;
  222. state.light_uniforms[index].atlas_rect[1] = atlas_rect.position.y;
  223. state.light_uniforms[index].atlas_rect[2] = atlas_rect.size.width;
  224. state.light_uniforms[index].atlas_rect[3] = atlas_rect.size.height;
  225. } else {
  226. state.light_uniforms[index].atlas_rect[0] = 0;
  227. state.light_uniforms[index].atlas_rect[1] = 0;
  228. state.light_uniforms[index].atlas_rect[2] = 0;
  229. state.light_uniforms[index].atlas_rect[3] = 0;
  230. }
  231. l->render_index_cache = index;
  232. index++;
  233. l = l->next_ptr;
  234. }
  235. light_count = index;
  236. }
  237. if (light_count > 0) {
  238. glBindBufferBase(GL_UNIFORM_BUFFER, LIGHT_UNIFORM_LOCATION, state.canvas_instance_data_buffers[state.current_data_buffer_index].light_ubo);
  239. #ifdef WEB_ENABLED
  240. glBufferSubData(GL_UNIFORM_BUFFER, 0, sizeof(LightUniform) * light_count, state.light_uniforms);
  241. #else
  242. // On Desktop and mobile we map the memory without synchronizing for maximum speed.
  243. void *ubo = glMapBufferRange(GL_UNIFORM_BUFFER, 0, sizeof(LightUniform) * light_count, GL_MAP_WRITE_BIT | GL_MAP_UNSYNCHRONIZED_BIT);
  244. memcpy(ubo, state.light_uniforms, sizeof(LightUniform) * light_count);
  245. glUnmapBuffer(GL_UNIFORM_BUFFER);
  246. #endif
  247. GLuint texture_atlas = texture_storage->texture_atlas_get_texture();
  248. if (texture_atlas == 0) {
  249. GLES3::Texture *tex = texture_storage->get_texture(texture_storage->texture_gl_get_default(GLES3::DEFAULT_GL_TEXTURE_WHITE));
  250. texture_atlas = tex->tex_id;
  251. }
  252. glActiveTexture(GL_TEXTURE0 + GLES3::Config::get_singleton()->max_texture_image_units - 2);
  253. glBindTexture(GL_TEXTURE_2D, texture_atlas);
  254. GLuint shadow_tex = state.shadow_texture;
  255. if (shadow_tex == 0) {
  256. GLES3::Texture *tex = texture_storage->get_texture(texture_storage->texture_gl_get_default(GLES3::DEFAULT_GL_TEXTURE_WHITE));
  257. shadow_tex = tex->tex_id;
  258. }
  259. glActiveTexture(GL_TEXTURE0 + GLES3::Config::get_singleton()->max_texture_image_units - 3);
  260. glBindTexture(GL_TEXTURE_2D, shadow_tex);
  261. }
  262. {
  263. //update canvas state uniform buffer
  264. StateBuffer state_buffer;
  265. Size2i ssize = texture_storage->render_target_get_size(p_to_render_target);
  266. // If we've overridden the render target's color texture, then we need
  267. // to invert the Y axis, so 2D texture appear right side up.
  268. // We're probably rendering directly to an XR device.
  269. float y_scale = texture_storage->render_target_get_override_color(p_to_render_target).is_valid() ? -2.0f : 2.0f;
  270. Transform3D screen_transform;
  271. screen_transform.translate_local(-(ssize.width / 2.0f), -(ssize.height / 2.0f), 0.0f);
  272. screen_transform.scale(Vector3(2.0f / ssize.width, y_scale / ssize.height, 1.0f));
  273. _update_transform_to_mat4(screen_transform, state_buffer.screen_transform);
  274. _update_transform_2d_to_mat4(p_canvas_transform, state_buffer.canvas_transform);
  275. Transform2D normal_transform = p_canvas_transform;
  276. normal_transform.columns[0].normalize();
  277. normal_transform.columns[1].normalize();
  278. normal_transform.columns[2] = Vector2();
  279. _update_transform_2d_to_mat4(normal_transform, state_buffer.canvas_normal_transform);
  280. state_buffer.canvas_modulate[0] = p_modulate.r;
  281. state_buffer.canvas_modulate[1] = p_modulate.g;
  282. state_buffer.canvas_modulate[2] = p_modulate.b;
  283. state_buffer.canvas_modulate[3] = p_modulate.a;
  284. Size2 render_target_size = texture_storage->render_target_get_size(p_to_render_target);
  285. state_buffer.screen_pixel_size[0] = 1.0 / render_target_size.x;
  286. state_buffer.screen_pixel_size[1] = 1.0 / render_target_size.y;
  287. state_buffer.time = state.time;
  288. state_buffer.use_pixel_snap = p_snap_2d_vertices_to_pixel;
  289. state_buffer.directional_light_count = directional_light_count;
  290. Vector2 canvas_scale = p_canvas_transform.get_scale();
  291. state_buffer.sdf_to_screen[0] = render_target_size.width / canvas_scale.x;
  292. state_buffer.sdf_to_screen[1] = render_target_size.height / canvas_scale.y;
  293. state_buffer.screen_to_sdf[0] = 1.0 / state_buffer.sdf_to_screen[0];
  294. state_buffer.screen_to_sdf[1] = 1.0 / state_buffer.sdf_to_screen[1];
  295. Rect2 sdf_rect = texture_storage->render_target_get_sdf_rect(p_to_render_target);
  296. Rect2 sdf_tex_rect(sdf_rect.position / canvas_scale, sdf_rect.size / canvas_scale);
  297. state_buffer.sdf_to_tex[0] = 1.0 / sdf_tex_rect.size.width;
  298. state_buffer.sdf_to_tex[1] = 1.0 / sdf_tex_rect.size.height;
  299. state_buffer.sdf_to_tex[2] = -sdf_tex_rect.position.x / sdf_tex_rect.size.width;
  300. state_buffer.sdf_to_tex[3] = -sdf_tex_rect.position.y / sdf_tex_rect.size.height;
  301. state_buffer.tex_to_sdf = 1.0 / ((canvas_scale.x + canvas_scale.y) * 0.5);
  302. glBindBufferBase(GL_UNIFORM_BUFFER, BASE_UNIFORM_LOCATION, state.canvas_instance_data_buffers[state.current_data_buffer_index].state_ubo);
  303. glBufferData(GL_UNIFORM_BUFFER, sizeof(StateBuffer), &state_buffer, GL_STREAM_DRAW);
  304. GLuint global_buffer = material_storage->global_shader_parameters_get_uniform_buffer();
  305. glBindBufferBase(GL_UNIFORM_BUFFER, GLOBAL_UNIFORM_LOCATION, global_buffer);
  306. glBindBuffer(GL_UNIFORM_BUFFER, 0);
  307. }
  308. glActiveTexture(GL_TEXTURE0 + GLES3::Config::get_singleton()->max_texture_image_units - 5);
  309. glBindTexture(GL_TEXTURE_2D, texture_storage->render_target_get_sdf_texture(p_to_render_target));
  310. {
  311. state.default_filter = p_default_filter;
  312. state.default_repeat = p_default_repeat;
  313. }
  314. Size2 render_target_size = texture_storage->render_target_get_size(p_to_render_target);
  315. glViewport(0, 0, render_target_size.x, render_target_size.y);
  316. r_sdf_used = false;
  317. int item_count = 0;
  318. bool backbuffer_cleared = false;
  319. bool time_used = false;
  320. bool material_screen_texture_cached = false;
  321. bool material_screen_texture_mipmaps_cached = false;
  322. Rect2 back_buffer_rect;
  323. bool backbuffer_copy = false;
  324. bool backbuffer_gen_mipmaps = false;
  325. bool update_skeletons = false;
  326. Item *ci = p_item_list;
  327. Item *canvas_group_owner = nullptr;
  328. bool skip_item = false;
  329. state.last_item_index = 0;
  330. while (ci) {
  331. if (ci->copy_back_buffer && canvas_group_owner == nullptr) {
  332. backbuffer_copy = true;
  333. if (ci->copy_back_buffer->full) {
  334. back_buffer_rect = Rect2();
  335. } else {
  336. back_buffer_rect = ci->copy_back_buffer->rect;
  337. }
  338. }
  339. // Check material for something that may change flow of rendering, but do not bind for now.
  340. RID material = ci->material_owner == nullptr ? ci->material : ci->material_owner->material;
  341. if (material.is_valid()) {
  342. GLES3::CanvasMaterialData *md = static_cast<GLES3::CanvasMaterialData *>(material_storage->material_get_data(material, RS::SHADER_CANVAS_ITEM));
  343. if (md && md->shader_data->valid) {
  344. if (md->shader_data->uses_screen_texture && canvas_group_owner == nullptr) {
  345. if (!material_screen_texture_cached) {
  346. backbuffer_copy = true;
  347. back_buffer_rect = Rect2();
  348. backbuffer_gen_mipmaps = md->shader_data->uses_screen_texture_mipmaps;
  349. } else if (!material_screen_texture_mipmaps_cached) {
  350. backbuffer_gen_mipmaps = md->shader_data->uses_screen_texture_mipmaps;
  351. }
  352. }
  353. if (md->shader_data->uses_sdf) {
  354. r_sdf_used = true;
  355. }
  356. if (md->shader_data->uses_time) {
  357. time_used = true;
  358. }
  359. }
  360. }
  361. if (ci->skeleton.is_valid()) {
  362. const Item::Command *c = ci->commands;
  363. while (c) {
  364. if (c->type == Item::Command::TYPE_MESH) {
  365. const Item::CommandMesh *cm = static_cast<const Item::CommandMesh *>(c);
  366. if (cm->mesh_instance.is_valid()) {
  367. mesh_storage->mesh_instance_check_for_update(cm->mesh_instance);
  368. mesh_storage->mesh_instance_set_canvas_item_transform(cm->mesh_instance, canvas_transform_inverse * ci->final_transform);
  369. update_skeletons = true;
  370. }
  371. }
  372. c = c->next;
  373. }
  374. }
  375. if (ci->canvas_group_owner != nullptr) {
  376. if (canvas_group_owner == nullptr) {
  377. if (update_skeletons) {
  378. mesh_storage->update_mesh_instances();
  379. update_skeletons = false;
  380. }
  381. // Canvas group begins here, render until before this item
  382. _render_items(p_to_render_target, item_count, canvas_transform_inverse, p_light_list, r_sdf_used);
  383. item_count = 0;
  384. if (ci->canvas_group_owner->canvas_group->mode != RS::CANVAS_GROUP_MODE_TRANSPARENT) {
  385. Rect2i group_rect = ci->canvas_group_owner->global_rect_cache;
  386. texture_storage->render_target_copy_to_back_buffer(p_to_render_target, group_rect, false);
  387. if (ci->canvas_group_owner->canvas_group->mode == RS::CANVAS_GROUP_MODE_CLIP_AND_DRAW) {
  388. ci->canvas_group_owner->use_canvas_group = false;
  389. items[item_count++] = ci->canvas_group_owner;
  390. }
  391. } else if (!backbuffer_cleared) {
  392. texture_storage->render_target_clear_back_buffer(p_to_render_target, Rect2i(), Color(0, 0, 0, 0));
  393. backbuffer_cleared = true;
  394. }
  395. backbuffer_copy = false;
  396. canvas_group_owner = ci->canvas_group_owner; //continue until owner found
  397. }
  398. ci->canvas_group_owner = nullptr; //must be cleared
  399. }
  400. if (canvas_group_owner == nullptr && ci->canvas_group != nullptr && ci->canvas_group->mode != RS::CANVAS_GROUP_MODE_CLIP_AND_DRAW) {
  401. skip_item = true;
  402. }
  403. if (ci == canvas_group_owner) {
  404. if (update_skeletons) {
  405. mesh_storage->update_mesh_instances();
  406. update_skeletons = false;
  407. }
  408. _render_items(p_to_render_target, item_count, canvas_transform_inverse, p_light_list, r_sdf_used, true);
  409. item_count = 0;
  410. if (ci->canvas_group->blur_mipmaps) {
  411. texture_storage->render_target_gen_back_buffer_mipmaps(p_to_render_target, ci->global_rect_cache);
  412. }
  413. canvas_group_owner = nullptr;
  414. // Backbuffer is dirty now and needs to be re-cleared if another CanvasGroup needs it.
  415. backbuffer_cleared = false;
  416. // Tell the renderer to paint this as a canvas group
  417. ci->use_canvas_group = true;
  418. } else {
  419. ci->use_canvas_group = false;
  420. }
  421. if (backbuffer_copy) {
  422. if (update_skeletons) {
  423. mesh_storage->update_mesh_instances();
  424. update_skeletons = false;
  425. }
  426. //render anything pending, including clearing if no items
  427. _render_items(p_to_render_target, item_count, canvas_transform_inverse, p_light_list, r_sdf_used);
  428. item_count = 0;
  429. texture_storage->render_target_copy_to_back_buffer(p_to_render_target, back_buffer_rect, backbuffer_gen_mipmaps);
  430. backbuffer_copy = false;
  431. material_screen_texture_cached = true; // After a backbuffer copy, screen texture makes no further copies.
  432. material_screen_texture_mipmaps_cached = backbuffer_gen_mipmaps;
  433. backbuffer_gen_mipmaps = false;
  434. }
  435. if (backbuffer_gen_mipmaps) {
  436. texture_storage->render_target_gen_back_buffer_mipmaps(p_to_render_target, back_buffer_rect);
  437. backbuffer_gen_mipmaps = false;
  438. material_screen_texture_mipmaps_cached = true;
  439. }
  440. // just add all items for now
  441. if (skip_item) {
  442. skip_item = false;
  443. } else {
  444. items[item_count++] = ci;
  445. }
  446. if (!ci->next || item_count == MAX_RENDER_ITEMS - 1) {
  447. if (update_skeletons) {
  448. mesh_storage->update_mesh_instances();
  449. update_skeletons = false;
  450. }
  451. _render_items(p_to_render_target, item_count, canvas_transform_inverse, p_light_list, r_sdf_used, canvas_group_owner != nullptr);
  452. //then reset
  453. item_count = 0;
  454. }
  455. ci = ci->next;
  456. }
  457. if (time_used) {
  458. RenderingServerDefault::redraw_request();
  459. }
  460. state.canvas_instance_data_buffers[state.current_data_buffer_index].fence = glFenceSync(GL_SYNC_GPU_COMMANDS_COMPLETE, 0);
  461. // Clear out state used in 2D pass
  462. reset_canvas();
  463. state.current_data_buffer_index = (state.current_data_buffer_index + 1) % state.canvas_instance_data_buffers.size();
  464. state.current_instance_buffer_index = 0;
  465. }
  466. void RasterizerCanvasGLES3::_render_items(RID p_to_render_target, int p_item_count, const Transform2D &p_canvas_transform_inverse, Light *p_lights, bool &r_sdf_used, bool p_to_backbuffer) {
  467. GLES3::MaterialStorage *material_storage = GLES3::MaterialStorage::get_singleton();
  468. canvas_begin(p_to_render_target, p_to_backbuffer);
  469. if (p_item_count <= 0) {
  470. // Nothing to draw, just call canvas_begin() to clear the render target and return.
  471. return;
  472. }
  473. uint32_t index = 0;
  474. Item *current_clip = nullptr;
  475. GLES3::CanvasShaderData *shader_data_cache = nullptr;
  476. // Record Batches.
  477. // First item always forms its own batch.
  478. bool batch_broken = false;
  479. _new_batch(batch_broken);
  480. // Override the start position and index as we want to start from where we finished off last time.
  481. state.canvas_instance_batches[state.current_batch_index].start = state.last_item_index;
  482. index = 0;
  483. for (int i = 0; i < p_item_count; i++) {
  484. Item *ci = items[i];
  485. if (ci->final_clip_owner != state.canvas_instance_batches[state.current_batch_index].clip) {
  486. _new_batch(batch_broken);
  487. state.canvas_instance_batches[state.current_batch_index].clip = ci->final_clip_owner;
  488. current_clip = ci->final_clip_owner;
  489. }
  490. RID material = ci->material_owner == nullptr ? ci->material : ci->material_owner->material;
  491. if (ci->use_canvas_group) {
  492. if (ci->canvas_group->mode == RS::CANVAS_GROUP_MODE_CLIP_AND_DRAW) {
  493. material = default_clip_children_material;
  494. } else {
  495. if (material.is_null()) {
  496. if (ci->canvas_group->mode == RS::CANVAS_GROUP_MODE_CLIP_ONLY) {
  497. material = default_clip_children_material;
  498. } else {
  499. material = default_canvas_group_material;
  500. }
  501. }
  502. }
  503. }
  504. if (material != state.canvas_instance_batches[state.current_batch_index].material) {
  505. _new_batch(batch_broken);
  506. GLES3::CanvasMaterialData *material_data = nullptr;
  507. if (material.is_valid()) {
  508. material_data = static_cast<GLES3::CanvasMaterialData *>(material_storage->material_get_data(material, RS::SHADER_CANVAS_ITEM));
  509. }
  510. shader_data_cache = nullptr;
  511. if (material_data) {
  512. if (material_data->shader_data->version.is_valid() && material_data->shader_data->valid) {
  513. shader_data_cache = material_data->shader_data;
  514. }
  515. }
  516. state.canvas_instance_batches[state.current_batch_index].material = material;
  517. state.canvas_instance_batches[state.current_batch_index].material_data = material_data;
  518. }
  519. GLES3::CanvasShaderData::BlendMode blend_mode = shader_data_cache ? shader_data_cache->blend_mode : GLES3::CanvasShaderData::BLEND_MODE_MIX;
  520. _record_item_commands(ci, p_to_render_target, p_canvas_transform_inverse, current_clip, blend_mode, p_lights, index, batch_broken, r_sdf_used);
  521. }
  522. if (index == 0) {
  523. // Nothing to render, just return.
  524. state.current_batch_index = 0;
  525. state.canvas_instance_batches.clear();
  526. return;
  527. }
  528. // Copy over all data needed for rendering.
  529. glBindBuffer(GL_ARRAY_BUFFER, state.canvas_instance_data_buffers[state.current_data_buffer_index].instance_buffers[state.current_instance_buffer_index]);
  530. #ifdef WEB_ENABLED
  531. glBufferSubData(GL_ARRAY_BUFFER, state.last_item_index * sizeof(InstanceData), sizeof(InstanceData) * index, state.instance_data_array);
  532. #else
  533. // On Desktop and mobile we map the memory without synchronizing for maximum speed.
  534. void *buffer = glMapBufferRange(GL_ARRAY_BUFFER, state.last_item_index * sizeof(InstanceData), index * sizeof(InstanceData), GL_MAP_WRITE_BIT | GL_MAP_UNSYNCHRONIZED_BIT);
  535. memcpy(buffer, state.instance_data_array, index * sizeof(InstanceData));
  536. glUnmapBuffer(GL_ARRAY_BUFFER);
  537. #endif
  538. glDisable(GL_SCISSOR_TEST);
  539. current_clip = nullptr;
  540. GLES3::CanvasShaderData::BlendMode last_blend_mode = GLES3::CanvasShaderData::BLEND_MODE_MIX;
  541. Color last_blend_color;
  542. state.current_tex = RID();
  543. for (uint32_t i = 0; i <= state.current_batch_index; i++) {
  544. // Skipping when there is no instances.
  545. if (state.canvas_instance_batches[i].instance_count == 0) {
  546. continue;
  547. }
  548. //setup clip
  549. if (current_clip != state.canvas_instance_batches[i].clip) {
  550. current_clip = state.canvas_instance_batches[i].clip;
  551. if (current_clip) {
  552. glEnable(GL_SCISSOR_TEST);
  553. glScissor(current_clip->final_clip_rect.position.x, current_clip->final_clip_rect.position.y, current_clip->final_clip_rect.size.x, current_clip->final_clip_rect.size.y);
  554. } else {
  555. glDisable(GL_SCISSOR_TEST);
  556. }
  557. }
  558. GLES3::CanvasMaterialData *material_data = state.canvas_instance_batches[i].material_data;
  559. CanvasShaderGLES3::ShaderVariant variant = state.canvas_instance_batches[i].shader_variant;
  560. uint64_t specialization = 0;
  561. specialization |= uint64_t(state.canvas_instance_batches[i].lights_disabled);
  562. specialization |= uint64_t(!GLES3::Config::get_singleton()->float_texture_supported) << 1;
  563. RID shader_version = data.canvas_shader_default_version;
  564. if (material_data) {
  565. if (material_data->shader_data->version.is_valid() && material_data->shader_data->valid) {
  566. // Bind uniform buffer and textures
  567. material_data->bind_uniforms();
  568. shader_version = material_data->shader_data->version;
  569. }
  570. }
  571. bool success = GLES3::MaterialStorage::get_singleton()->shaders.canvas_shader.version_bind_shader(shader_version, variant, specialization);
  572. if (!success) {
  573. continue;
  574. }
  575. GLES3::CanvasShaderData::BlendMode blend_mode = state.canvas_instance_batches[i].blend_mode;
  576. Color blend_color = state.canvas_instance_batches[i].blend_color;
  577. if (last_blend_mode != blend_mode || last_blend_color != blend_color) {
  578. if (last_blend_mode == GLES3::CanvasShaderData::BLEND_MODE_DISABLED) {
  579. // re-enable it
  580. glEnable(GL_BLEND);
  581. } else if (blend_mode == GLES3::CanvasShaderData::BLEND_MODE_DISABLED) {
  582. // disable it
  583. glDisable(GL_BLEND);
  584. }
  585. switch (blend_mode) {
  586. case GLES3::CanvasShaderData::BLEND_MODE_DISABLED: {
  587. // Nothing to do here.
  588. } break;
  589. case GLES3::CanvasShaderData::BLEND_MODE_LCD: {
  590. glBlendEquation(GL_FUNC_ADD);
  591. if (state.transparent_render_target) {
  592. glBlendFuncSeparate(GL_CONSTANT_COLOR, GL_ONE_MINUS_SRC_COLOR, GL_ONE, GL_ONE_MINUS_SRC_ALPHA);
  593. } else {
  594. glBlendFuncSeparate(GL_CONSTANT_COLOR, GL_ONE_MINUS_SRC_COLOR, GL_ZERO, GL_ONE);
  595. }
  596. glBlendColor(blend_color.r, blend_color.g, blend_color.b, blend_color.a);
  597. } break;
  598. case GLES3::CanvasShaderData::BLEND_MODE_MIX: {
  599. glBlendEquation(GL_FUNC_ADD);
  600. if (state.transparent_render_target) {
  601. glBlendFuncSeparate(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA, GL_ONE, GL_ONE_MINUS_SRC_ALPHA);
  602. } else {
  603. glBlendFuncSeparate(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA, GL_ZERO, GL_ONE);
  604. }
  605. } break;
  606. case GLES3::CanvasShaderData::BLEND_MODE_ADD: {
  607. glBlendEquation(GL_FUNC_ADD);
  608. if (state.transparent_render_target) {
  609. glBlendFuncSeparate(GL_SRC_ALPHA, GL_ONE, GL_SRC_ALPHA, GL_ONE);
  610. } else {
  611. glBlendFuncSeparate(GL_SRC_ALPHA, GL_ONE, GL_ZERO, GL_ONE);
  612. }
  613. } break;
  614. case GLES3::CanvasShaderData::BLEND_MODE_SUB: {
  615. glBlendEquation(GL_FUNC_REVERSE_SUBTRACT);
  616. if (state.transparent_render_target) {
  617. glBlendFuncSeparate(GL_SRC_ALPHA, GL_ONE, GL_SRC_ALPHA, GL_ONE);
  618. } else {
  619. glBlendFuncSeparate(GL_SRC_ALPHA, GL_ONE, GL_ZERO, GL_ONE);
  620. }
  621. } break;
  622. case GLES3::CanvasShaderData::BLEND_MODE_MUL: {
  623. glBlendEquation(GL_FUNC_ADD);
  624. if (state.transparent_render_target) {
  625. glBlendFuncSeparate(GL_DST_COLOR, GL_ZERO, GL_DST_ALPHA, GL_ZERO);
  626. } else {
  627. glBlendFuncSeparate(GL_DST_COLOR, GL_ZERO, GL_ZERO, GL_ONE);
  628. }
  629. } break;
  630. case GLES3::CanvasShaderData::BLEND_MODE_PMALPHA: {
  631. glBlendEquation(GL_FUNC_ADD);
  632. if (state.transparent_render_target) {
  633. glBlendFuncSeparate(GL_ONE, GL_ONE_MINUS_SRC_ALPHA, GL_ONE, GL_ONE_MINUS_SRC_ALPHA);
  634. } else {
  635. glBlendFuncSeparate(GL_ONE, GL_ONE_MINUS_SRC_ALPHA, GL_ZERO, GL_ONE);
  636. }
  637. } break;
  638. }
  639. last_blend_mode = blend_mode;
  640. last_blend_color = blend_color;
  641. }
  642. _render_batch(p_lights, i);
  643. }
  644. glDisable(GL_SCISSOR_TEST);
  645. state.current_batch_index = 0;
  646. state.canvas_instance_batches.clear();
  647. state.last_item_index += index;
  648. }
  649. void RasterizerCanvasGLES3::_record_item_commands(const Item *p_item, RID p_render_target, const Transform2D &p_canvas_transform_inverse, Item *&current_clip, GLES3::CanvasShaderData::BlendMode p_blend_mode, Light *p_lights, uint32_t &r_index, bool &r_batch_broken, bool &r_sdf_used) {
  650. RenderingServer::CanvasItemTextureFilter texture_filter = p_item->texture_filter == RS::CANVAS_ITEM_TEXTURE_FILTER_DEFAULT ? state.default_filter : p_item->texture_filter;
  651. if (texture_filter != state.canvas_instance_batches[state.current_batch_index].filter) {
  652. _new_batch(r_batch_broken);
  653. state.canvas_instance_batches[state.current_batch_index].filter = texture_filter;
  654. }
  655. RenderingServer::CanvasItemTextureRepeat texture_repeat = p_item->texture_repeat == RS::CANVAS_ITEM_TEXTURE_REPEAT_DEFAULT ? state.default_repeat : p_item->texture_repeat;
  656. if (texture_repeat != state.canvas_instance_batches[state.current_batch_index].repeat) {
  657. _new_batch(r_batch_broken);
  658. state.canvas_instance_batches[state.current_batch_index].repeat = texture_repeat;
  659. }
  660. Transform2D base_transform = p_canvas_transform_inverse * p_item->final_transform;
  661. Transform2D draw_transform; // Used by transform command
  662. Color base_color = p_item->final_modulate;
  663. uint32_t base_flags = 0;
  664. Size2 texpixel_size;
  665. bool reclip = false;
  666. bool skipping = false;
  667. // TODO: consider making lights a per-batch property and then baking light operations in the shader for better performance.
  668. uint32_t lights[4] = { 0, 0, 0, 0 };
  669. uint16_t light_count = 0;
  670. {
  671. Light *light = p_lights;
  672. while (light) {
  673. if (light->render_index_cache >= 0 && p_item->light_mask & light->item_mask && p_item->z_final >= light->z_min && p_item->z_final <= light->z_max && p_item->global_rect_cache.intersects_transformed(light->xform_cache, light->rect_cache)) {
  674. uint32_t light_index = light->render_index_cache;
  675. lights[light_count >> 2] |= light_index << ((light_count & 3) * 8);
  676. light_count++;
  677. if (light_count == data.max_lights_per_item - 1) {
  678. break;
  679. }
  680. }
  681. light = light->next_ptr;
  682. }
  683. base_flags |= light_count << FLAGS_LIGHT_COUNT_SHIFT;
  684. }
  685. bool lights_disabled = light_count == 0 && !state.using_directional_lights;
  686. if (lights_disabled != state.canvas_instance_batches[state.current_batch_index].lights_disabled) {
  687. _new_batch(r_batch_broken);
  688. state.canvas_instance_batches[state.current_batch_index].lights_disabled = lights_disabled;
  689. }
  690. const Item::Command *c = p_item->commands;
  691. while (c) {
  692. if (skipping && c->type != Item::Command::TYPE_ANIMATION_SLICE) {
  693. c = c->next;
  694. continue;
  695. }
  696. if (c->type != Item::Command::TYPE_MESH) {
  697. // For Meshes, this gets updated below.
  698. _update_transform_2d_to_mat2x3(base_transform * draw_transform, state.instance_data_array[r_index].world);
  699. }
  700. // Zero out most fields.
  701. for (int i = 0; i < 4; i++) {
  702. state.instance_data_array[r_index].modulation[i] = 0.0;
  703. state.instance_data_array[r_index].ninepatch_margins[i] = 0.0;
  704. state.instance_data_array[r_index].src_rect[i] = 0.0;
  705. state.instance_data_array[r_index].dst_rect[i] = 0.0;
  706. state.instance_data_array[r_index].lights[i] = uint32_t(0);
  707. }
  708. state.instance_data_array[r_index].color_texture_pixel_size[0] = 0.0;
  709. state.instance_data_array[r_index].color_texture_pixel_size[1] = 0.0;
  710. state.instance_data_array[r_index].pad[0] = 0.0;
  711. state.instance_data_array[r_index].pad[1] = 0.0;
  712. state.instance_data_array[r_index].lights[0] = lights[0];
  713. state.instance_data_array[r_index].lights[1] = lights[1];
  714. state.instance_data_array[r_index].lights[2] = lights[2];
  715. state.instance_data_array[r_index].lights[3] = lights[3];
  716. state.instance_data_array[r_index].flags = base_flags | (state.instance_data_array[r_index == 0 ? 0 : r_index - 1].flags & (FLAGS_DEFAULT_NORMAL_MAP_USED | FLAGS_DEFAULT_SPECULAR_MAP_USED)); //reset on each command for sanity, keep canvastexture binding config
  717. Color blend_color = base_color;
  718. GLES3::CanvasShaderData::BlendMode blend_mode = p_blend_mode;
  719. if (c->type == Item::Command::TYPE_RECT) {
  720. const Item::CommandRect *rect = static_cast<const Item::CommandRect *>(c);
  721. if (rect->flags & CANVAS_RECT_LCD) {
  722. blend_mode = GLES3::CanvasShaderData::BLEND_MODE_LCD;
  723. blend_color = rect->modulate * base_color;
  724. }
  725. }
  726. if (blend_mode != state.canvas_instance_batches[state.current_batch_index].blend_mode || blend_color != state.canvas_instance_batches[state.current_batch_index].blend_color) {
  727. _new_batch(r_batch_broken);
  728. state.canvas_instance_batches[state.current_batch_index].blend_mode = blend_mode;
  729. state.canvas_instance_batches[state.current_batch_index].blend_color = blend_color;
  730. }
  731. switch (c->type) {
  732. case Item::Command::TYPE_RECT: {
  733. const Item::CommandRect *rect = static_cast<const Item::CommandRect *>(c);
  734. if (rect->flags & CANVAS_RECT_TILE && state.canvas_instance_batches[state.current_batch_index].repeat != RenderingServer::CanvasItemTextureRepeat::CANVAS_ITEM_TEXTURE_REPEAT_ENABLED) {
  735. _new_batch(r_batch_broken);
  736. state.canvas_instance_batches[state.current_batch_index].repeat = RenderingServer::CanvasItemTextureRepeat::CANVAS_ITEM_TEXTURE_REPEAT_ENABLED;
  737. }
  738. if (rect->texture != state.canvas_instance_batches[state.current_batch_index].tex || state.canvas_instance_batches[state.current_batch_index].command_type != Item::Command::TYPE_RECT) {
  739. _new_batch(r_batch_broken);
  740. state.canvas_instance_batches[state.current_batch_index].tex = rect->texture;
  741. state.canvas_instance_batches[state.current_batch_index].command_type = Item::Command::TYPE_RECT;
  742. state.canvas_instance_batches[state.current_batch_index].command = c;
  743. state.canvas_instance_batches[state.current_batch_index].shader_variant = CanvasShaderGLES3::MODE_QUAD;
  744. }
  745. _prepare_canvas_texture(rect->texture, state.canvas_instance_batches[state.current_batch_index].filter, state.canvas_instance_batches[state.current_batch_index].repeat, r_index, texpixel_size);
  746. Rect2 src_rect;
  747. Rect2 dst_rect;
  748. if (rect->texture != RID()) {
  749. src_rect = (rect->flags & CANVAS_RECT_REGION) ? Rect2(rect->source.position * texpixel_size, rect->source.size * texpixel_size) : Rect2(0, 0, 1, 1);
  750. dst_rect = Rect2(rect->rect.position, rect->rect.size);
  751. if (dst_rect.size.width < 0) {
  752. dst_rect.position.x += dst_rect.size.width;
  753. dst_rect.size.width *= -1;
  754. }
  755. if (dst_rect.size.height < 0) {
  756. dst_rect.position.y += dst_rect.size.height;
  757. dst_rect.size.height *= -1;
  758. }
  759. if (rect->flags & CANVAS_RECT_FLIP_H) {
  760. src_rect.size.x *= -1;
  761. state.instance_data_array[r_index].flags |= FLAGS_FLIP_H;
  762. }
  763. if (rect->flags & CANVAS_RECT_FLIP_V) {
  764. src_rect.size.y *= -1;
  765. state.instance_data_array[r_index].flags |= FLAGS_FLIP_V;
  766. }
  767. if (rect->flags & CANVAS_RECT_TRANSPOSE) {
  768. state.instance_data_array[r_index].flags |= FLAGS_TRANSPOSE_RECT;
  769. }
  770. if (rect->flags & CANVAS_RECT_CLIP_UV) {
  771. state.instance_data_array[r_index].flags |= FLAGS_CLIP_RECT_UV;
  772. }
  773. } else {
  774. dst_rect = Rect2(rect->rect.position, rect->rect.size);
  775. if (dst_rect.size.width < 0) {
  776. dst_rect.position.x += dst_rect.size.width;
  777. dst_rect.size.width *= -1;
  778. }
  779. if (dst_rect.size.height < 0) {
  780. dst_rect.position.y += dst_rect.size.height;
  781. dst_rect.size.height *= -1;
  782. }
  783. src_rect = Rect2(0, 0, 1, 1);
  784. }
  785. if (rect->flags & CANVAS_RECT_MSDF) {
  786. state.instance_data_array[r_index].flags |= FLAGS_USE_MSDF;
  787. state.instance_data_array[r_index].msdf[0] = rect->px_range; // Pixel range.
  788. state.instance_data_array[r_index].msdf[1] = rect->outline; // Outline size.
  789. state.instance_data_array[r_index].msdf[2] = 0.f; // Reserved.
  790. state.instance_data_array[r_index].msdf[3] = 0.f; // Reserved.
  791. } else if (rect->flags & CANVAS_RECT_LCD) {
  792. state.instance_data_array[r_index].flags |= FLAGS_USE_LCD;
  793. }
  794. state.instance_data_array[r_index].modulation[0] = rect->modulate.r * base_color.r;
  795. state.instance_data_array[r_index].modulation[1] = rect->modulate.g * base_color.g;
  796. state.instance_data_array[r_index].modulation[2] = rect->modulate.b * base_color.b;
  797. state.instance_data_array[r_index].modulation[3] = rect->modulate.a * base_color.a;
  798. state.instance_data_array[r_index].src_rect[0] = src_rect.position.x;
  799. state.instance_data_array[r_index].src_rect[1] = src_rect.position.y;
  800. state.instance_data_array[r_index].src_rect[2] = src_rect.size.width;
  801. state.instance_data_array[r_index].src_rect[3] = src_rect.size.height;
  802. state.instance_data_array[r_index].dst_rect[0] = dst_rect.position.x;
  803. state.instance_data_array[r_index].dst_rect[1] = dst_rect.position.y;
  804. state.instance_data_array[r_index].dst_rect[2] = dst_rect.size.width;
  805. state.instance_data_array[r_index].dst_rect[3] = dst_rect.size.height;
  806. _add_to_batch(r_index, r_batch_broken);
  807. } break;
  808. case Item::Command::TYPE_NINEPATCH: {
  809. const Item::CommandNinePatch *np = static_cast<const Item::CommandNinePatch *>(c);
  810. if (np->texture != state.canvas_instance_batches[state.current_batch_index].tex || state.canvas_instance_batches[state.current_batch_index].command_type != Item::Command::TYPE_NINEPATCH) {
  811. _new_batch(r_batch_broken);
  812. state.canvas_instance_batches[state.current_batch_index].tex = np->texture;
  813. state.canvas_instance_batches[state.current_batch_index].command_type = Item::Command::TYPE_NINEPATCH;
  814. state.canvas_instance_batches[state.current_batch_index].command = c;
  815. state.canvas_instance_batches[state.current_batch_index].shader_variant = CanvasShaderGLES3::MODE_NINEPATCH;
  816. }
  817. _prepare_canvas_texture(np->texture, state.canvas_instance_batches[state.current_batch_index].filter, state.canvas_instance_batches[state.current_batch_index].repeat, r_index, texpixel_size);
  818. Rect2 src_rect;
  819. Rect2 dst_rect(np->rect.position.x, np->rect.position.y, np->rect.size.x, np->rect.size.y);
  820. if (np->texture == RID()) {
  821. texpixel_size = Size2(1, 1);
  822. src_rect = Rect2(0, 0, 1, 1);
  823. } else {
  824. if (np->source != Rect2()) {
  825. src_rect = Rect2(np->source.position.x * texpixel_size.width, np->source.position.y * texpixel_size.height, np->source.size.x * texpixel_size.width, np->source.size.y * texpixel_size.height);
  826. state.instance_data_array[r_index].color_texture_pixel_size[0] = 1.0 / np->source.size.width;
  827. state.instance_data_array[r_index].color_texture_pixel_size[1] = 1.0 / np->source.size.height;
  828. } else {
  829. src_rect = Rect2(0, 0, 1, 1);
  830. }
  831. }
  832. state.instance_data_array[r_index].modulation[0] = np->color.r * base_color.r;
  833. state.instance_data_array[r_index].modulation[1] = np->color.g * base_color.g;
  834. state.instance_data_array[r_index].modulation[2] = np->color.b * base_color.b;
  835. state.instance_data_array[r_index].modulation[3] = np->color.a * base_color.a;
  836. state.instance_data_array[r_index].src_rect[0] = src_rect.position.x;
  837. state.instance_data_array[r_index].src_rect[1] = src_rect.position.y;
  838. state.instance_data_array[r_index].src_rect[2] = src_rect.size.width;
  839. state.instance_data_array[r_index].src_rect[3] = src_rect.size.height;
  840. state.instance_data_array[r_index].dst_rect[0] = dst_rect.position.x;
  841. state.instance_data_array[r_index].dst_rect[1] = dst_rect.position.y;
  842. state.instance_data_array[r_index].dst_rect[2] = dst_rect.size.width;
  843. state.instance_data_array[r_index].dst_rect[3] = dst_rect.size.height;
  844. state.instance_data_array[r_index].flags |= int(np->axis_x) << FLAGS_NINEPATCH_H_MODE_SHIFT;
  845. state.instance_data_array[r_index].flags |= int(np->axis_y) << FLAGS_NINEPATCH_V_MODE_SHIFT;
  846. if (np->draw_center) {
  847. state.instance_data_array[r_index].flags |= FLAGS_NINEPACH_DRAW_CENTER;
  848. }
  849. state.instance_data_array[r_index].ninepatch_margins[0] = np->margin[SIDE_LEFT];
  850. state.instance_data_array[r_index].ninepatch_margins[1] = np->margin[SIDE_TOP];
  851. state.instance_data_array[r_index].ninepatch_margins[2] = np->margin[SIDE_RIGHT];
  852. state.instance_data_array[r_index].ninepatch_margins[3] = np->margin[SIDE_BOTTOM];
  853. _add_to_batch(r_index, r_batch_broken);
  854. // Restore if overridden.
  855. state.instance_data_array[r_index].color_texture_pixel_size[0] = texpixel_size.x;
  856. state.instance_data_array[r_index].color_texture_pixel_size[1] = texpixel_size.y;
  857. } break;
  858. case Item::Command::TYPE_POLYGON: {
  859. const Item::CommandPolygon *polygon = static_cast<const Item::CommandPolygon *>(c);
  860. // Polygon's can't be batched, so always create a new batch
  861. _new_batch(r_batch_broken);
  862. state.canvas_instance_batches[state.current_batch_index].tex = polygon->texture;
  863. state.canvas_instance_batches[state.current_batch_index].command_type = Item::Command::TYPE_POLYGON;
  864. state.canvas_instance_batches[state.current_batch_index].command = c;
  865. state.canvas_instance_batches[state.current_batch_index].shader_variant = CanvasShaderGLES3::MODE_ATTRIBUTES;
  866. _prepare_canvas_texture(polygon->texture, state.canvas_instance_batches[state.current_batch_index].filter, state.canvas_instance_batches[state.current_batch_index].repeat, r_index, texpixel_size);
  867. state.instance_data_array[r_index].modulation[0] = base_color.r;
  868. state.instance_data_array[r_index].modulation[1] = base_color.g;
  869. state.instance_data_array[r_index].modulation[2] = base_color.b;
  870. state.instance_data_array[r_index].modulation[3] = base_color.a;
  871. for (int j = 0; j < 4; j++) {
  872. state.instance_data_array[r_index].src_rect[j] = 0;
  873. state.instance_data_array[r_index].dst_rect[j] = 0;
  874. state.instance_data_array[r_index].ninepatch_margins[j] = 0;
  875. }
  876. _add_to_batch(r_index, r_batch_broken);
  877. } break;
  878. case Item::Command::TYPE_PRIMITIVE: {
  879. const Item::CommandPrimitive *primitive = static_cast<const Item::CommandPrimitive *>(c);
  880. if (primitive->point_count != state.canvas_instance_batches[state.current_batch_index].primitive_points || state.canvas_instance_batches[state.current_batch_index].command_type != Item::Command::TYPE_PRIMITIVE) {
  881. _new_batch(r_batch_broken);
  882. state.canvas_instance_batches[state.current_batch_index].tex = primitive->texture;
  883. state.canvas_instance_batches[state.current_batch_index].primitive_points = primitive->point_count;
  884. state.canvas_instance_batches[state.current_batch_index].command_type = Item::Command::TYPE_PRIMITIVE;
  885. state.canvas_instance_batches[state.current_batch_index].command = c;
  886. state.canvas_instance_batches[state.current_batch_index].shader_variant = CanvasShaderGLES3::MODE_PRIMITIVE;
  887. }
  888. _prepare_canvas_texture(state.canvas_instance_batches[state.current_batch_index].tex, state.canvas_instance_batches[state.current_batch_index].filter, state.canvas_instance_batches[state.current_batch_index].repeat, r_index, texpixel_size);
  889. for (uint32_t j = 0; j < MIN(3u, primitive->point_count); j++) {
  890. state.instance_data_array[r_index].points[j * 2 + 0] = primitive->points[j].x;
  891. state.instance_data_array[r_index].points[j * 2 + 1] = primitive->points[j].y;
  892. state.instance_data_array[r_index].uvs[j * 2 + 0] = primitive->uvs[j].x;
  893. state.instance_data_array[r_index].uvs[j * 2 + 1] = primitive->uvs[j].y;
  894. Color col = primitive->colors[j] * base_color;
  895. state.instance_data_array[r_index].colors[j * 2 + 0] = (uint32_t(Math::make_half_float(col.g)) << 16) | Math::make_half_float(col.r);
  896. state.instance_data_array[r_index].colors[j * 2 + 1] = (uint32_t(Math::make_half_float(col.a)) << 16) | Math::make_half_float(col.b);
  897. }
  898. _add_to_batch(r_index, r_batch_broken);
  899. if (primitive->point_count == 4) {
  900. // Reset base data.
  901. _update_transform_2d_to_mat2x3(base_transform * draw_transform, state.instance_data_array[r_index].world);
  902. _prepare_canvas_texture(state.canvas_instance_batches[state.current_batch_index].tex, state.canvas_instance_batches[state.current_batch_index].filter, state.canvas_instance_batches[state.current_batch_index].repeat, r_index, texpixel_size);
  903. for (uint32_t j = 0; j < 3; j++) {
  904. int offset = j == 0 ? 0 : 1;
  905. // Second triangle in the quad. Uses vertices 0, 2, 3.
  906. state.instance_data_array[r_index].points[j * 2 + 0] = primitive->points[j + offset].x;
  907. state.instance_data_array[r_index].points[j * 2 + 1] = primitive->points[j + offset].y;
  908. state.instance_data_array[r_index].uvs[j * 2 + 0] = primitive->uvs[j + offset].x;
  909. state.instance_data_array[r_index].uvs[j * 2 + 1] = primitive->uvs[j + offset].y;
  910. Color col = primitive->colors[j + offset] * base_color;
  911. state.instance_data_array[r_index].colors[j * 2 + 0] = (uint32_t(Math::make_half_float(col.g)) << 16) | Math::make_half_float(col.r);
  912. state.instance_data_array[r_index].colors[j * 2 + 1] = (uint32_t(Math::make_half_float(col.a)) << 16) | Math::make_half_float(col.b);
  913. }
  914. _add_to_batch(r_index, r_batch_broken);
  915. }
  916. } break;
  917. case Item::Command::TYPE_MESH:
  918. case Item::Command::TYPE_MULTIMESH:
  919. case Item::Command::TYPE_PARTICLES: {
  920. // Mesh's can't be batched, so always create a new batch
  921. _new_batch(r_batch_broken);
  922. Color modulate(1, 1, 1, 1);
  923. state.canvas_instance_batches[state.current_batch_index].shader_variant = CanvasShaderGLES3::MODE_ATTRIBUTES;
  924. if (c->type == Item::Command::TYPE_MESH) {
  925. const Item::CommandMesh *m = static_cast<const Item::CommandMesh *>(c);
  926. state.canvas_instance_batches[state.current_batch_index].tex = m->texture;
  927. _update_transform_2d_to_mat2x3(base_transform * draw_transform * m->transform, state.instance_data_array[r_index].world);
  928. modulate = m->modulate;
  929. } else if (c->type == Item::Command::TYPE_MULTIMESH) {
  930. const Item::CommandMultiMesh *mm = static_cast<const Item::CommandMultiMesh *>(c);
  931. state.canvas_instance_batches[state.current_batch_index].tex = mm->texture;
  932. state.canvas_instance_batches[state.current_batch_index].shader_variant = CanvasShaderGLES3::MODE_INSTANCED;
  933. if (GLES3::MeshStorage::get_singleton()->multimesh_uses_colors(mm->multimesh)) {
  934. state.instance_data_array[r_index].flags |= FLAGS_INSTANCING_HAS_COLORS;
  935. }
  936. if (GLES3::MeshStorage::get_singleton()->multimesh_uses_custom_data(mm->multimesh)) {
  937. state.instance_data_array[r_index].flags |= FLAGS_INSTANCING_HAS_CUSTOM_DATA;
  938. }
  939. } else if (c->type == Item::Command::TYPE_PARTICLES) {
  940. GLES3::ParticlesStorage *particles_storage = GLES3::ParticlesStorage::get_singleton();
  941. GLES3::TextureStorage *texture_storage = GLES3::TextureStorage::get_singleton();
  942. const Item::CommandParticles *pt = static_cast<const Item::CommandParticles *>(c);
  943. RID particles = pt->particles;
  944. state.canvas_instance_batches[state.current_batch_index].tex = pt->texture;
  945. state.canvas_instance_batches[state.current_batch_index].shader_variant = CanvasShaderGLES3::MODE_INSTANCED;
  946. state.instance_data_array[r_index].flags |= FLAGS_INSTANCING_HAS_COLORS;
  947. state.instance_data_array[r_index].flags |= FLAGS_INSTANCING_HAS_CUSTOM_DATA;
  948. if (particles_storage->particles_has_collision(particles) && texture_storage->render_target_is_sdf_enabled(p_render_target)) {
  949. // Pass collision information.
  950. Transform2D xform = p_item->final_transform;
  951. GLuint sdf_texture = texture_storage->render_target_get_sdf_texture(p_render_target);
  952. Rect2 to_screen;
  953. {
  954. Rect2 sdf_rect = texture_storage->render_target_get_sdf_rect(p_render_target);
  955. to_screen.size = Vector2(1.0 / sdf_rect.size.width, 1.0 / sdf_rect.size.height);
  956. to_screen.position = -sdf_rect.position * to_screen.size;
  957. }
  958. particles_storage->particles_set_canvas_sdf_collision(pt->particles, true, xform, to_screen, sdf_texture);
  959. } else {
  960. particles_storage->particles_set_canvas_sdf_collision(pt->particles, false, Transform2D(), Rect2(), 0);
  961. }
  962. r_sdf_used |= particles_storage->particles_has_collision(particles);
  963. }
  964. state.canvas_instance_batches[state.current_batch_index].command = c;
  965. state.canvas_instance_batches[state.current_batch_index].command_type = c->type;
  966. _prepare_canvas_texture(state.canvas_instance_batches[state.current_batch_index].tex, state.canvas_instance_batches[state.current_batch_index].filter, state.canvas_instance_batches[state.current_batch_index].repeat, r_index, texpixel_size);
  967. state.instance_data_array[r_index].modulation[0] = base_color.r * modulate.r;
  968. state.instance_data_array[r_index].modulation[1] = base_color.g * modulate.g;
  969. state.instance_data_array[r_index].modulation[2] = base_color.b * modulate.b;
  970. state.instance_data_array[r_index].modulation[3] = base_color.a * modulate.a;
  971. for (int j = 0; j < 4; j++) {
  972. state.instance_data_array[r_index].src_rect[j] = 0;
  973. state.instance_data_array[r_index].dst_rect[j] = 0;
  974. state.instance_data_array[r_index].ninepatch_margins[j] = 0;
  975. }
  976. _add_to_batch(r_index, r_batch_broken);
  977. } break;
  978. case Item::Command::TYPE_TRANSFORM: {
  979. const Item::CommandTransform *transform = static_cast<const Item::CommandTransform *>(c);
  980. draw_transform = transform->xform;
  981. } break;
  982. case Item::Command::TYPE_CLIP_IGNORE: {
  983. const Item::CommandClipIgnore *ci = static_cast<const Item::CommandClipIgnore *>(c);
  984. if (current_clip) {
  985. if (ci->ignore != reclip) {
  986. _new_batch(r_batch_broken);
  987. if (ci->ignore) {
  988. state.canvas_instance_batches[state.current_batch_index].clip = nullptr;
  989. reclip = true;
  990. } else {
  991. state.canvas_instance_batches[state.current_batch_index].clip = current_clip;
  992. reclip = false;
  993. }
  994. }
  995. }
  996. } break;
  997. case Item::Command::TYPE_ANIMATION_SLICE: {
  998. const Item::CommandAnimationSlice *as = static_cast<const Item::CommandAnimationSlice *>(c);
  999. double current_time = RSG::rasterizer->get_total_time();
  1000. double local_time = Math::fposmod(current_time - as->offset, as->animation_length);
  1001. skipping = !(local_time >= as->slice_begin && local_time < as->slice_end);
  1002. RenderingServerDefault::redraw_request(); // animation visible means redraw request
  1003. } break;
  1004. }
  1005. c = c->next;
  1006. r_batch_broken = false;
  1007. }
  1008. if (current_clip && reclip) {
  1009. //will make it re-enable clipping if needed afterwards
  1010. current_clip = nullptr;
  1011. }
  1012. }
  1013. void RasterizerCanvasGLES3::_render_batch(Light *p_lights, uint32_t p_index) {
  1014. ERR_FAIL_COND(!state.canvas_instance_batches[state.current_batch_index].command);
  1015. // Used by Polygon and Mesh.
  1016. static const GLenum prim[5] = { GL_POINTS, GL_LINES, GL_LINE_STRIP, GL_TRIANGLES, GL_TRIANGLE_STRIP };
  1017. _bind_canvas_texture(state.canvas_instance_batches[p_index].tex, state.canvas_instance_batches[p_index].filter, state.canvas_instance_batches[p_index].repeat);
  1018. switch (state.canvas_instance_batches[p_index].command_type) {
  1019. case Item::Command::TYPE_RECT:
  1020. case Item::Command::TYPE_NINEPATCH: {
  1021. glBindVertexArray(data.indexed_quad_array);
  1022. glBindBuffer(GL_ARRAY_BUFFER, state.canvas_instance_data_buffers[state.current_data_buffer_index].instance_buffers[state.canvas_instance_batches[p_index].instance_buffer_index]);
  1023. uint32_t range_start = state.canvas_instance_batches[p_index].start * sizeof(InstanceData);
  1024. _enable_attributes(range_start, false);
  1025. glDrawElementsInstanced(GL_TRIANGLES, 6, GL_UNSIGNED_INT, 0, state.canvas_instance_batches[p_index].instance_count);
  1026. glBindVertexArray(0);
  1027. } break;
  1028. case Item::Command::TYPE_POLYGON: {
  1029. const Item::CommandPolygon *polygon = static_cast<const Item::CommandPolygon *>(state.canvas_instance_batches[p_index].command);
  1030. PolygonBuffers *pb = polygon_buffers.polygons.getptr(polygon->polygon.polygon_id);
  1031. ERR_FAIL_COND(!pb);
  1032. glBindVertexArray(pb->vertex_array);
  1033. glBindBuffer(GL_ARRAY_BUFFER, state.canvas_instance_data_buffers[state.current_data_buffer_index].instance_buffers[state.canvas_instance_batches[p_index].instance_buffer_index]);
  1034. uint32_t range_start = state.canvas_instance_batches[p_index].start * sizeof(InstanceData);
  1035. _enable_attributes(range_start, false);
  1036. if (pb->color_disabled && pb->color != Color(1.0, 1.0, 1.0, 1.0)) {
  1037. glVertexAttrib4f(RS::ARRAY_COLOR, pb->color.r, pb->color.g, pb->color.b, pb->color.a);
  1038. }
  1039. if (pb->index_buffer != 0) {
  1040. glDrawElementsInstanced(prim[polygon->primitive], pb->count, GL_UNSIGNED_INT, nullptr, 1);
  1041. } else {
  1042. glDrawArraysInstanced(prim[polygon->primitive], 0, pb->count, 1);
  1043. }
  1044. glBindVertexArray(0);
  1045. if (pb->color_disabled && pb->color != Color(1.0, 1.0, 1.0, 1.0)) {
  1046. // Reset so this doesn't pollute other draw calls.
  1047. glVertexAttrib4f(RS::ARRAY_COLOR, 1.0, 1.0, 1.0, 1.0);
  1048. }
  1049. } break;
  1050. case Item::Command::TYPE_PRIMITIVE: {
  1051. glBindVertexArray(data.canvas_quad_array);
  1052. glBindBuffer(GL_ARRAY_BUFFER, state.canvas_instance_data_buffers[state.current_data_buffer_index].instance_buffers[state.canvas_instance_batches[p_index].instance_buffer_index]);
  1053. uint32_t range_start = state.canvas_instance_batches[p_index].start * sizeof(InstanceData);
  1054. _enable_attributes(range_start, true);
  1055. const GLenum primitive[5] = { GL_POINTS, GL_POINTS, GL_LINES, GL_TRIANGLES, GL_TRIANGLES };
  1056. int instance_count = state.canvas_instance_batches[p_index].instance_count;
  1057. ERR_FAIL_COND(instance_count <= 0);
  1058. if (instance_count >= 1) {
  1059. glDrawArraysInstanced(primitive[state.canvas_instance_batches[p_index].primitive_points], 0, state.canvas_instance_batches[p_index].primitive_points, instance_count);
  1060. }
  1061. } break;
  1062. case Item::Command::TYPE_MESH:
  1063. case Item::Command::TYPE_MULTIMESH:
  1064. case Item::Command::TYPE_PARTICLES: {
  1065. GLES3::MeshStorage *mesh_storage = GLES3::MeshStorage::get_singleton();
  1066. GLES3::ParticlesStorage *particles_storage = GLES3::ParticlesStorage::get_singleton();
  1067. RID mesh;
  1068. RID mesh_instance;
  1069. uint32_t instance_count = 1;
  1070. GLuint instance_buffer = 0;
  1071. uint32_t instance_stride = 0;
  1072. uint32_t instance_color_offset = 0;
  1073. bool instance_uses_color = false;
  1074. bool instance_uses_custom_data = false;
  1075. bool use_instancing = false;
  1076. if (state.canvas_instance_batches[p_index].command_type == Item::Command::TYPE_MESH) {
  1077. const Item::CommandMesh *m = static_cast<const Item::CommandMesh *>(state.canvas_instance_batches[p_index].command);
  1078. mesh = m->mesh;
  1079. mesh_instance = m->mesh_instance;
  1080. } else if (state.canvas_instance_batches[p_index].command_type == Item::Command::TYPE_MULTIMESH) {
  1081. const Item::CommandMultiMesh *mm = static_cast<const Item::CommandMultiMesh *>(state.canvas_instance_batches[p_index].command);
  1082. RID multimesh = mm->multimesh;
  1083. mesh = mesh_storage->multimesh_get_mesh(multimesh);
  1084. if (mesh_storage->multimesh_get_transform_format(multimesh) != RS::MULTIMESH_TRANSFORM_2D) {
  1085. break;
  1086. }
  1087. instance_count = mesh_storage->multimesh_get_instances_to_draw(multimesh);
  1088. if (instance_count == 0) {
  1089. break;
  1090. }
  1091. instance_buffer = mesh_storage->multimesh_get_gl_buffer(multimesh);
  1092. instance_stride = mesh_storage->multimesh_get_stride(multimesh);
  1093. instance_color_offset = mesh_storage->multimesh_get_color_offset(multimesh);
  1094. instance_uses_color = mesh_storage->multimesh_uses_colors(multimesh);
  1095. instance_uses_custom_data = mesh_storage->multimesh_uses_custom_data(multimesh);
  1096. use_instancing = true;
  1097. } else if (state.canvas_instance_batches[p_index].command_type == Item::Command::TYPE_PARTICLES) {
  1098. const Item::CommandParticles *pt = static_cast<const Item::CommandParticles *>(state.canvas_instance_batches[p_index].command);
  1099. RID particles = pt->particles;
  1100. mesh = particles_storage->particles_get_draw_pass_mesh(particles, 0);
  1101. ERR_BREAK(particles_storage->particles_get_mode(particles) != RS::PARTICLES_MODE_2D);
  1102. particles_storage->particles_request_process(particles);
  1103. if (particles_storage->particles_is_inactive(particles)) {
  1104. break;
  1105. }
  1106. RenderingServerDefault::redraw_request(); // Active particles means redraw request.
  1107. int dpc = particles_storage->particles_get_draw_passes(particles);
  1108. if (dpc == 0) {
  1109. break; // Nothing to draw.
  1110. }
  1111. instance_count = particles_storage->particles_get_amount(particles);
  1112. instance_buffer = particles_storage->particles_get_gl_buffer(particles);
  1113. instance_stride = 12; // 8 bytes for instance transform and 4 bytes for packed color and custom.
  1114. instance_color_offset = 8; // 8 bytes for instance transform.
  1115. instance_uses_color = true;
  1116. instance_uses_custom_data = true;
  1117. use_instancing = true;
  1118. }
  1119. ERR_FAIL_COND(mesh.is_null());
  1120. uint32_t surf_count = mesh_storage->mesh_get_surface_count(mesh);
  1121. for (uint32_t j = 0; j < surf_count; j++) {
  1122. void *surface = mesh_storage->mesh_get_surface(mesh, j);
  1123. RS::PrimitiveType primitive = mesh_storage->mesh_surface_get_primitive(surface);
  1124. ERR_CONTINUE(primitive < 0 || primitive >= RS::PRIMITIVE_MAX);
  1125. GLuint vertex_array_gl = 0;
  1126. GLuint index_array_gl = 0;
  1127. uint32_t input_mask = 0; // 2D meshes always use the same vertex format
  1128. if (mesh_instance.is_valid()) {
  1129. mesh_storage->mesh_instance_surface_get_vertex_arrays_and_format(mesh_instance, j, input_mask, vertex_array_gl);
  1130. } else {
  1131. mesh_storage->mesh_surface_get_vertex_arrays_and_format(surface, input_mask, vertex_array_gl);
  1132. }
  1133. index_array_gl = mesh_storage->mesh_surface_get_index_buffer(surface, 0);
  1134. bool use_index_buffer = false;
  1135. glBindVertexArray(vertex_array_gl);
  1136. glBindBuffer(GL_ARRAY_BUFFER, state.canvas_instance_data_buffers[state.current_data_buffer_index].instance_buffers[state.canvas_instance_batches[p_index].instance_buffer_index]);
  1137. uint32_t range_start = state.canvas_instance_batches[p_index].start * sizeof(InstanceData);
  1138. _enable_attributes(range_start, false, instance_count);
  1139. if (index_array_gl != 0) {
  1140. glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, index_array_gl);
  1141. use_index_buffer = true;
  1142. }
  1143. if (use_instancing) {
  1144. if (instance_buffer == 0) {
  1145. break;
  1146. }
  1147. // Bind instance buffers.
  1148. glBindBuffer(GL_ARRAY_BUFFER, instance_buffer);
  1149. glEnableVertexAttribArray(1);
  1150. glVertexAttribPointer(1, 4, GL_FLOAT, GL_FALSE, instance_stride * sizeof(float), CAST_INT_TO_UCHAR_PTR(0));
  1151. glVertexAttribDivisor(1, 1);
  1152. glEnableVertexAttribArray(2);
  1153. glVertexAttribPointer(2, 4, GL_FLOAT, GL_FALSE, instance_stride * sizeof(float), CAST_INT_TO_UCHAR_PTR(4 * 4));
  1154. glVertexAttribDivisor(2, 1);
  1155. if (instance_uses_color || instance_uses_custom_data) {
  1156. glEnableVertexAttribArray(5);
  1157. glVertexAttribIPointer(5, 4, GL_UNSIGNED_INT, instance_stride * sizeof(float), CAST_INT_TO_UCHAR_PTR(instance_color_offset * sizeof(float)));
  1158. glVertexAttribDivisor(5, 1);
  1159. } else {
  1160. // Set all default instance color and custom data values to 1.0 or 0.0 using a compressed format.
  1161. uint16_t zero = Math::make_half_float(0.0f);
  1162. uint16_t one = Math::make_half_float(1.0f);
  1163. GLuint default_color = (uint32_t(one) << 16) | one;
  1164. GLuint default_custom = (uint32_t(zero) << 16) | zero;
  1165. glVertexAttribI4ui(5, default_color, default_color, default_custom, default_custom);
  1166. }
  1167. }
  1168. GLenum primitive_gl = prim[int(primitive)];
  1169. if (use_index_buffer) {
  1170. glDrawElementsInstanced(primitive_gl, mesh_storage->mesh_surface_get_vertices_drawn_count(surface), mesh_storage->mesh_surface_get_index_type(surface), 0, instance_count);
  1171. } else {
  1172. glDrawArraysInstanced(primitive_gl, 0, mesh_storage->mesh_surface_get_vertices_drawn_count(surface), instance_count);
  1173. }
  1174. glBindBuffer(GL_ARRAY_BUFFER, 0);
  1175. glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0);
  1176. if (use_instancing) {
  1177. glDisableVertexAttribArray(5);
  1178. glDisableVertexAttribArray(6);
  1179. glDisableVertexAttribArray(7);
  1180. glDisableVertexAttribArray(8);
  1181. }
  1182. }
  1183. } break;
  1184. case Item::Command::TYPE_TRANSFORM:
  1185. case Item::Command::TYPE_CLIP_IGNORE:
  1186. case Item::Command::TYPE_ANIMATION_SLICE: {
  1187. // Can ignore these as they only impact batch creation.
  1188. } break;
  1189. }
  1190. }
  1191. void RasterizerCanvasGLES3::_add_to_batch(uint32_t &r_index, bool &r_batch_broken) {
  1192. state.canvas_instance_batches[state.current_batch_index].instance_count++;
  1193. r_index++;
  1194. if (r_index + state.last_item_index >= data.max_instances_per_buffer) {
  1195. // Copy over all data needed for rendering right away
  1196. // then go back to recording item commands.
  1197. glBindBuffer(GL_ARRAY_BUFFER, state.canvas_instance_data_buffers[state.current_data_buffer_index].instance_buffers[state.current_instance_buffer_index]);
  1198. #ifdef WEB_ENABLED
  1199. glBufferSubData(GL_ARRAY_BUFFER, state.last_item_index * sizeof(InstanceData), sizeof(InstanceData) * r_index, state.instance_data_array);
  1200. #else
  1201. // On Desktop and mobile we map the memory without synchronizing for maximum speed.
  1202. void *buffer = glMapBufferRange(GL_ARRAY_BUFFER, state.last_item_index * sizeof(InstanceData), r_index * sizeof(InstanceData), GL_MAP_WRITE_BIT | GL_MAP_UNSYNCHRONIZED_BIT);
  1203. memcpy(buffer, state.instance_data_array, r_index * sizeof(InstanceData));
  1204. glUnmapBuffer(GL_ARRAY_BUFFER);
  1205. #endif
  1206. _allocate_instance_buffer();
  1207. r_index = 0;
  1208. state.last_item_index = 0;
  1209. r_batch_broken = false; // Force a new batch to be created
  1210. _new_batch(r_batch_broken);
  1211. state.canvas_instance_batches[state.current_batch_index].start = 0;
  1212. }
  1213. }
  1214. void RasterizerCanvasGLES3::_new_batch(bool &r_batch_broken) {
  1215. if (state.canvas_instance_batches.size() == 0) {
  1216. state.canvas_instance_batches.push_back(Batch());
  1217. return;
  1218. }
  1219. if (r_batch_broken || state.canvas_instance_batches[state.current_batch_index].instance_count == 0) {
  1220. return;
  1221. }
  1222. r_batch_broken = true;
  1223. // Copy the properties of the current batch, we will manually update the things that changed.
  1224. Batch new_batch = state.canvas_instance_batches[state.current_batch_index];
  1225. new_batch.instance_count = 0;
  1226. new_batch.start = state.canvas_instance_batches[state.current_batch_index].start + state.canvas_instance_batches[state.current_batch_index].instance_count;
  1227. new_batch.instance_buffer_index = state.current_instance_buffer_index;
  1228. state.current_batch_index++;
  1229. state.canvas_instance_batches.push_back(new_batch);
  1230. }
  1231. void RasterizerCanvasGLES3::_enable_attributes(uint32_t p_start, bool p_primitive, uint32_t p_rate) {
  1232. uint32_t split = p_primitive ? 11 : 12;
  1233. for (uint32_t i = 6; i < split; i++) {
  1234. glEnableVertexAttribArray(i);
  1235. glVertexAttribPointer(i, 4, GL_FLOAT, GL_FALSE, sizeof(InstanceData), CAST_INT_TO_UCHAR_PTR(p_start + (i - 6) * 4 * sizeof(float)));
  1236. glVertexAttribDivisor(i, p_rate);
  1237. }
  1238. for (uint32_t i = split; i <= 13; i++) {
  1239. glEnableVertexAttribArray(i);
  1240. glVertexAttribIPointer(i, 4, GL_UNSIGNED_INT, sizeof(InstanceData), CAST_INT_TO_UCHAR_PTR(p_start + (i - 6) * 4 * sizeof(float)));
  1241. glVertexAttribDivisor(i, p_rate);
  1242. }
  1243. }
  1244. RID RasterizerCanvasGLES3::light_create() {
  1245. CanvasLight canvas_light;
  1246. return canvas_light_owner.make_rid(canvas_light);
  1247. }
  1248. void RasterizerCanvasGLES3::light_set_texture(RID p_rid, RID p_texture) {
  1249. GLES3::TextureStorage *texture_storage = GLES3::TextureStorage::get_singleton();
  1250. CanvasLight *cl = canvas_light_owner.get_or_null(p_rid);
  1251. ERR_FAIL_COND(!cl);
  1252. if (cl->texture == p_texture) {
  1253. return;
  1254. }
  1255. ERR_FAIL_COND(p_texture.is_valid() && !texture_storage->owns_texture(p_texture));
  1256. if (cl->texture.is_valid()) {
  1257. texture_storage->texture_remove_from_texture_atlas(cl->texture);
  1258. }
  1259. cl->texture = p_texture;
  1260. if (cl->texture.is_valid()) {
  1261. texture_storage->texture_add_to_texture_atlas(cl->texture);
  1262. }
  1263. }
  1264. void RasterizerCanvasGLES3::light_set_use_shadow(RID p_rid, bool p_enable) {
  1265. CanvasLight *cl = canvas_light_owner.get_or_null(p_rid);
  1266. ERR_FAIL_COND(!cl);
  1267. cl->shadow.enabled = p_enable;
  1268. }
  1269. void RasterizerCanvasGLES3::light_update_shadow(RID p_rid, int p_shadow_index, const Transform2D &p_light_xform, int p_light_mask, float p_near, float p_far, LightOccluderInstance *p_occluders) {
  1270. GLES3::Config *config = GLES3::Config::get_singleton();
  1271. CanvasLight *cl = canvas_light_owner.get_or_null(p_rid);
  1272. ERR_FAIL_COND(!cl->shadow.enabled);
  1273. _update_shadow_atlas();
  1274. cl->shadow.z_far = p_far;
  1275. cl->shadow.y_offset = float(p_shadow_index * 2 + 1) / float(data.max_lights_per_render * 2);
  1276. glBindFramebuffer(GL_FRAMEBUFFER, state.shadow_fb);
  1277. glViewport(0, p_shadow_index * 2, state.shadow_texture_size, 2);
  1278. glDepthMask(GL_TRUE);
  1279. glEnable(GL_DEPTH_TEST);
  1280. glDepthFunc(GL_LESS);
  1281. glDisable(GL_BLEND);
  1282. glEnable(GL_SCISSOR_TEST);
  1283. glScissor(0, p_shadow_index * 2, state.shadow_texture_size, 2);
  1284. glClearColor(p_far, p_far, p_far, 1.0);
  1285. glClearDepth(1.0);
  1286. glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
  1287. glCullFace(GL_BACK);
  1288. glDisable(GL_CULL_FACE);
  1289. RS::CanvasOccluderPolygonCullMode cull_mode = RS::CANVAS_OCCLUDER_POLYGON_CULL_DISABLED;
  1290. CanvasOcclusionShaderGLES3::ShaderVariant variant = config->float_texture_supported ? CanvasOcclusionShaderGLES3::MODE_SHADOW : CanvasOcclusionShaderGLES3::MODE_SHADOW_RGBA;
  1291. bool success = shadow_render.shader.version_bind_shader(shadow_render.shader_version, variant);
  1292. if (!success) {
  1293. return;
  1294. }
  1295. for (int i = 0; i < 4; i++) {
  1296. glViewport((state.shadow_texture_size / 4) * i, p_shadow_index * 2, (state.shadow_texture_size / 4), 2);
  1297. Projection projection;
  1298. {
  1299. real_t fov = 90;
  1300. real_t nearp = p_near;
  1301. real_t farp = p_far;
  1302. real_t aspect = 1.0;
  1303. real_t ymax = nearp * Math::tan(Math::deg_to_rad(fov * 0.5));
  1304. real_t ymin = -ymax;
  1305. real_t xmin = ymin * aspect;
  1306. real_t xmax = ymax * aspect;
  1307. projection.set_frustum(xmin, xmax, ymin, ymax, nearp, farp);
  1308. }
  1309. Vector3 cam_target = Basis::from_euler(Vector3(0, 0, Math_TAU * ((i + 3) / 4.0))).xform(Vector3(0, 1, 0));
  1310. projection = projection * Projection(Transform3D().looking_at(cam_target, Vector3(0, 0, -1)).affine_inverse());
  1311. shadow_render.shader.version_set_uniform(CanvasOcclusionShaderGLES3::PROJECTION, projection, shadow_render.shader_version, variant);
  1312. static const Vector2 directions[4] = { Vector2(1, 0), Vector2(0, 1), Vector2(-1, 0), Vector2(0, -1) };
  1313. shadow_render.shader.version_set_uniform(CanvasOcclusionShaderGLES3::DIRECTION, directions[i].x, directions[i].y, shadow_render.shader_version, variant);
  1314. shadow_render.shader.version_set_uniform(CanvasOcclusionShaderGLES3::Z_FAR, p_far, shadow_render.shader_version, variant);
  1315. LightOccluderInstance *instance = p_occluders;
  1316. while (instance) {
  1317. OccluderPolygon *co = occluder_polygon_owner.get_or_null(instance->occluder);
  1318. if (!co || co->vertex_array == 0 || !(p_light_mask & instance->light_mask)) {
  1319. instance = instance->next;
  1320. continue;
  1321. }
  1322. Transform2D modelview = p_light_xform * instance->xform_cache;
  1323. shadow_render.shader.version_set_uniform(CanvasOcclusionShaderGLES3::MODELVIEW1, modelview.columns[0][0], modelview.columns[1][0], 0, modelview.columns[2][0], shadow_render.shader_version, variant);
  1324. shadow_render.shader.version_set_uniform(CanvasOcclusionShaderGLES3::MODELVIEW2, modelview.columns[0][1], modelview.columns[1][1], 0, modelview.columns[2][1], shadow_render.shader_version, variant);
  1325. if (co->cull_mode != cull_mode) {
  1326. if (co->cull_mode == RS::CANVAS_OCCLUDER_POLYGON_CULL_DISABLED) {
  1327. glDisable(GL_CULL_FACE);
  1328. } else {
  1329. if (cull_mode == RS::CANVAS_OCCLUDER_POLYGON_CULL_DISABLED) {
  1330. // Last time was disabled, so enable and set proper face.
  1331. glEnable(GL_CULL_FACE);
  1332. }
  1333. glCullFace(co->cull_mode == RS::CANVAS_OCCLUDER_POLYGON_CULL_CLOCKWISE ? GL_FRONT : GL_BACK);
  1334. }
  1335. cull_mode = co->cull_mode;
  1336. }
  1337. glBindVertexArray(co->vertex_array);
  1338. glDrawElements(GL_TRIANGLES, 3 * co->line_point_count, GL_UNSIGNED_SHORT, 0);
  1339. instance = instance->next;
  1340. }
  1341. }
  1342. glBindVertexArray(0);
  1343. glBindFramebuffer(GL_FRAMEBUFFER, 0);
  1344. glDepthMask(GL_FALSE);
  1345. glDisable(GL_DEPTH_TEST);
  1346. glDisable(GL_SCISSOR_TEST);
  1347. }
  1348. void RasterizerCanvasGLES3::light_update_directional_shadow(RID p_rid, int p_shadow_index, const Transform2D &p_light_xform, int p_light_mask, float p_cull_distance, const Rect2 &p_clip_rect, LightOccluderInstance *p_occluders) {
  1349. GLES3::Config *config = GLES3::Config::get_singleton();
  1350. CanvasLight *cl = canvas_light_owner.get_or_null(p_rid);
  1351. ERR_FAIL_COND(!cl->shadow.enabled);
  1352. _update_shadow_atlas();
  1353. Vector2 light_dir = p_light_xform.columns[1].normalized();
  1354. Vector2 center = p_clip_rect.get_center();
  1355. float to_edge_distance = ABS(light_dir.dot(p_clip_rect.get_support(light_dir)) - light_dir.dot(center));
  1356. Vector2 from_pos = center - light_dir * (to_edge_distance + p_cull_distance);
  1357. float distance = to_edge_distance * 2.0 + p_cull_distance;
  1358. float half_size = p_clip_rect.size.length() * 0.5; //shadow length, must keep this no matter the angle
  1359. cl->shadow.z_far = distance;
  1360. cl->shadow.y_offset = float(p_shadow_index * 2 + 1) / float(data.max_lights_per_render * 2);
  1361. Transform2D to_light_xform;
  1362. to_light_xform[2] = from_pos;
  1363. to_light_xform[1] = light_dir;
  1364. to_light_xform[0] = -light_dir.orthogonal();
  1365. to_light_xform.invert();
  1366. glBindFramebuffer(GL_FRAMEBUFFER, state.shadow_fb);
  1367. glViewport(0, p_shadow_index * 2, state.shadow_texture_size, 2);
  1368. glDepthMask(GL_TRUE);
  1369. glEnable(GL_DEPTH_TEST);
  1370. glDepthFunc(GL_LESS);
  1371. glDisable(GL_BLEND);
  1372. glEnable(GL_SCISSOR_TEST);
  1373. glScissor(0, p_shadow_index * 2, state.shadow_texture_size, 2);
  1374. glClearColor(1.0, 1.0, 1.0, 1.0);
  1375. glClearDepth(1.0);
  1376. glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
  1377. glCullFace(GL_BACK);
  1378. glDisable(GL_CULL_FACE);
  1379. RS::CanvasOccluderPolygonCullMode cull_mode = RS::CANVAS_OCCLUDER_POLYGON_CULL_DISABLED;
  1380. CanvasOcclusionShaderGLES3::ShaderVariant variant = config->float_texture_supported ? CanvasOcclusionShaderGLES3::MODE_SHADOW : CanvasOcclusionShaderGLES3::MODE_SHADOW_RGBA;
  1381. bool success = shadow_render.shader.version_bind_shader(shadow_render.shader_version, variant);
  1382. if (!success) {
  1383. return;
  1384. }
  1385. Projection projection;
  1386. projection.set_orthogonal(-half_size, half_size, -0.5, 0.5, 0.0, distance);
  1387. projection = projection * Projection(Transform3D().looking_at(Vector3(0, 1, 0), Vector3(0, 0, -1)).affine_inverse());
  1388. shadow_render.shader.version_set_uniform(CanvasOcclusionShaderGLES3::PROJECTION, projection, shadow_render.shader_version, variant);
  1389. shadow_render.shader.version_set_uniform(CanvasOcclusionShaderGLES3::DIRECTION, 0.0, 1.0, shadow_render.shader_version, variant);
  1390. shadow_render.shader.version_set_uniform(CanvasOcclusionShaderGLES3::Z_FAR, distance, shadow_render.shader_version, variant);
  1391. LightOccluderInstance *instance = p_occluders;
  1392. while (instance) {
  1393. OccluderPolygon *co = occluder_polygon_owner.get_or_null(instance->occluder);
  1394. if (!co || co->vertex_array == 0 || !(p_light_mask & instance->light_mask)) {
  1395. instance = instance->next;
  1396. continue;
  1397. }
  1398. Transform2D modelview = to_light_xform * instance->xform_cache;
  1399. shadow_render.shader.version_set_uniform(CanvasOcclusionShaderGLES3::MODELVIEW1, modelview.columns[0][0], modelview.columns[1][0], 0, modelview.columns[2][0], shadow_render.shader_version, variant);
  1400. shadow_render.shader.version_set_uniform(CanvasOcclusionShaderGLES3::MODELVIEW2, modelview.columns[0][1], modelview.columns[1][1], 0, modelview.columns[2][1], shadow_render.shader_version, variant);
  1401. if (co->cull_mode != cull_mode) {
  1402. if (co->cull_mode == RS::CANVAS_OCCLUDER_POLYGON_CULL_DISABLED) {
  1403. glDisable(GL_CULL_FACE);
  1404. } else {
  1405. if (cull_mode == RS::CANVAS_OCCLUDER_POLYGON_CULL_DISABLED) {
  1406. // Last time was disabled, so enable and set proper face.
  1407. glEnable(GL_CULL_FACE);
  1408. }
  1409. glCullFace(co->cull_mode == RS::CANVAS_OCCLUDER_POLYGON_CULL_CLOCKWISE ? GL_FRONT : GL_BACK);
  1410. }
  1411. cull_mode = co->cull_mode;
  1412. }
  1413. glBindVertexArray(co->vertex_array);
  1414. glDrawElements(GL_TRIANGLES, 3 * co->line_point_count, GL_UNSIGNED_SHORT, 0);
  1415. instance = instance->next;
  1416. }
  1417. Transform2D to_shadow;
  1418. to_shadow.columns[0].x = 1.0 / -(half_size * 2.0);
  1419. to_shadow.columns[2].x = 0.5;
  1420. cl->shadow.directional_xform = to_shadow * to_light_xform;
  1421. glBindVertexArray(0);
  1422. glBindFramebuffer(GL_FRAMEBUFFER, 0);
  1423. glDepthMask(GL_FALSE);
  1424. glDisable(GL_DEPTH_TEST);
  1425. glDisable(GL_SCISSOR_TEST);
  1426. glDisable(GL_CULL_FACE);
  1427. }
  1428. void RasterizerCanvasGLES3::_update_shadow_atlas() {
  1429. GLES3::Config *config = GLES3::Config::get_singleton();
  1430. if (state.shadow_fb == 0) {
  1431. glActiveTexture(GL_TEXTURE0);
  1432. glGenFramebuffers(1, &state.shadow_fb);
  1433. glBindFramebuffer(GL_FRAMEBUFFER, state.shadow_fb);
  1434. glGenRenderbuffers(1, &state.shadow_depth_buffer);
  1435. glBindRenderbuffer(GL_RENDERBUFFER, state.shadow_depth_buffer);
  1436. glRenderbufferStorage(GL_RENDERBUFFER, GL_DEPTH_COMPONENT24, state.shadow_texture_size, data.max_lights_per_render * 2);
  1437. glFramebufferRenderbuffer(GL_FRAMEBUFFER, GL_DEPTH_ATTACHMENT, GL_RENDERBUFFER, state.shadow_depth_buffer);
  1438. glGenTextures(1, &state.shadow_texture);
  1439. glBindTexture(GL_TEXTURE_2D, state.shadow_texture);
  1440. if (config->float_texture_supported) {
  1441. glTexImage2D(GL_TEXTURE_2D, 0, GL_R32F, state.shadow_texture_size, data.max_lights_per_render * 2, 0, GL_RED, GL_FLOAT, nullptr);
  1442. } else {
  1443. glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA8, state.shadow_texture_size, data.max_lights_per_render * 2, 0, GL_RGBA, GL_UNSIGNED_BYTE, nullptr);
  1444. }
  1445. glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
  1446. glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
  1447. glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
  1448. glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
  1449. glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_BASE_LEVEL, 0);
  1450. glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAX_LEVEL, 1);
  1451. glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, state.shadow_texture, 0);
  1452. GLenum status = glCheckFramebufferStatus(GL_FRAMEBUFFER);
  1453. if (status != GL_FRAMEBUFFER_COMPLETE) {
  1454. glDeleteFramebuffers(1, &state.shadow_fb);
  1455. glDeleteTextures(1, &state.shadow_texture);
  1456. glDeleteRenderbuffers(1, &state.shadow_depth_buffer);
  1457. state.shadow_fb = 0;
  1458. state.shadow_texture = 0;
  1459. state.shadow_depth_buffer = 0;
  1460. WARN_PRINT("Could not create CanvasItem shadow atlas, status: " + GLES3::TextureStorage::get_singleton()->get_framebuffer_error(status));
  1461. }
  1462. GLES3::Utilities::get_singleton()->texture_allocated_data(state.shadow_texture, state.shadow_texture_size * data.max_lights_per_render * 2 * 4, "2D shadow atlas texture");
  1463. glBindFramebuffer(GL_FRAMEBUFFER, GLES3::TextureStorage::system_fbo);
  1464. }
  1465. }
  1466. void RasterizerCanvasGLES3::render_sdf(RID p_render_target, LightOccluderInstance *p_occluders) {
  1467. GLES3::TextureStorage *texture_storage = GLES3::TextureStorage::get_singleton();
  1468. GLuint fb = texture_storage->render_target_get_sdf_framebuffer(p_render_target);
  1469. Rect2i rect = texture_storage->render_target_get_sdf_rect(p_render_target);
  1470. Transform2D to_sdf;
  1471. to_sdf.columns[0] *= rect.size.width;
  1472. to_sdf.columns[1] *= rect.size.height;
  1473. to_sdf.columns[2] = rect.position;
  1474. Transform2D to_clip;
  1475. to_clip.columns[0] *= 2.0;
  1476. to_clip.columns[1] *= 2.0;
  1477. to_clip.columns[2] = -Vector2(1.0, 1.0);
  1478. to_clip = to_clip * to_sdf.affine_inverse();
  1479. glBindFramebuffer(GL_FRAMEBUFFER, fb);
  1480. glViewport(0, 0, rect.size.width, rect.size.height);
  1481. glDepthMask(GL_FALSE);
  1482. glDisable(GL_DEPTH_TEST);
  1483. glDisable(GL_BLEND);
  1484. glDisable(GL_CULL_FACE);
  1485. glDisable(GL_SCISSOR_TEST);
  1486. glClearColor(0.0, 0.0, 0.0, 0.0);
  1487. glClear(GL_COLOR_BUFFER_BIT);
  1488. CanvasOcclusionShaderGLES3::ShaderVariant variant = CanvasOcclusionShaderGLES3::MODE_SDF;
  1489. bool success = shadow_render.shader.version_bind_shader(shadow_render.shader_version, variant);
  1490. if (!success) {
  1491. return;
  1492. }
  1493. shadow_render.shader.version_set_uniform(CanvasOcclusionShaderGLES3::PROJECTION, Projection(), shadow_render.shader_version, variant);
  1494. shadow_render.shader.version_set_uniform(CanvasOcclusionShaderGLES3::DIRECTION, 0.0, 0.0, shadow_render.shader_version, variant);
  1495. shadow_render.shader.version_set_uniform(CanvasOcclusionShaderGLES3::Z_FAR, 0.0, shadow_render.shader_version, variant);
  1496. LightOccluderInstance *instance = p_occluders;
  1497. while (instance) {
  1498. OccluderPolygon *oc = occluder_polygon_owner.get_or_null(instance->occluder);
  1499. if (!oc || oc->sdf_vertex_array == 0) {
  1500. instance = instance->next;
  1501. continue;
  1502. }
  1503. Transform2D modelview = to_clip * instance->xform_cache;
  1504. shadow_render.shader.version_set_uniform(CanvasOcclusionShaderGLES3::MODELVIEW1, modelview.columns[0][0], modelview.columns[1][0], 0, modelview.columns[2][0], shadow_render.shader_version, variant);
  1505. shadow_render.shader.version_set_uniform(CanvasOcclusionShaderGLES3::MODELVIEW2, modelview.columns[0][1], modelview.columns[1][1], 0, modelview.columns[2][1], shadow_render.shader_version, variant);
  1506. glBindVertexArray(oc->sdf_vertex_array);
  1507. glDrawElements(oc->sdf_is_lines ? GL_LINES : GL_TRIANGLES, oc->sdf_index_count, GL_UNSIGNED_INT, 0);
  1508. instance = instance->next;
  1509. }
  1510. texture_storage->render_target_sdf_process(p_render_target); //done rendering, process it
  1511. glBindVertexArray(0);
  1512. glBindFramebuffer(GL_FRAMEBUFFER, 0);
  1513. }
  1514. RID RasterizerCanvasGLES3::occluder_polygon_create() {
  1515. OccluderPolygon occluder;
  1516. return occluder_polygon_owner.make_rid(occluder);
  1517. }
  1518. void RasterizerCanvasGLES3::occluder_polygon_set_shape(RID p_occluder, const Vector<Vector2> &p_points, bool p_closed) {
  1519. OccluderPolygon *oc = occluder_polygon_owner.get_or_null(p_occluder);
  1520. ERR_FAIL_COND(!oc);
  1521. Vector<Vector2> lines;
  1522. if (p_points.size()) {
  1523. int lc = p_points.size() * 2;
  1524. lines.resize(lc - (p_closed ? 0 : 2));
  1525. {
  1526. Vector2 *w = lines.ptrw();
  1527. const Vector2 *r = p_points.ptr();
  1528. int max = lc / 2;
  1529. if (!p_closed) {
  1530. max--;
  1531. }
  1532. for (int i = 0; i < max; i++) {
  1533. Vector2 a = r[i];
  1534. Vector2 b = r[(i + 1) % (lc / 2)];
  1535. w[i * 2 + 0] = a;
  1536. w[i * 2 + 1] = b;
  1537. }
  1538. }
  1539. }
  1540. if (oc->line_point_count != lines.size() && oc->vertex_array != 0) {
  1541. glDeleteVertexArrays(1, &oc->vertex_array);
  1542. GLES3::Utilities::get_singleton()->buffer_free_data(oc->vertex_buffer);
  1543. GLES3::Utilities::get_singleton()->buffer_free_data(oc->index_buffer);
  1544. oc->vertex_array = 0;
  1545. oc->vertex_buffer = 0;
  1546. oc->index_buffer = 0;
  1547. }
  1548. if (lines.size()) {
  1549. Vector<uint8_t> geometry;
  1550. Vector<uint8_t> indices;
  1551. int lc = lines.size();
  1552. geometry.resize(lc * 6 * sizeof(float));
  1553. indices.resize(lc * 3 * sizeof(uint16_t));
  1554. {
  1555. uint8_t *vw = geometry.ptrw();
  1556. float *vwptr = reinterpret_cast<float *>(vw);
  1557. uint8_t *iw = indices.ptrw();
  1558. uint16_t *iwptr = (uint16_t *)iw;
  1559. const Vector2 *lr = lines.ptr();
  1560. const int POLY_HEIGHT = 16384;
  1561. for (int i = 0; i < lc / 2; i++) {
  1562. vwptr[i * 12 + 0] = lr[i * 2 + 0].x;
  1563. vwptr[i * 12 + 1] = lr[i * 2 + 0].y;
  1564. vwptr[i * 12 + 2] = POLY_HEIGHT;
  1565. vwptr[i * 12 + 3] = lr[i * 2 + 1].x;
  1566. vwptr[i * 12 + 4] = lr[i * 2 + 1].y;
  1567. vwptr[i * 12 + 5] = POLY_HEIGHT;
  1568. vwptr[i * 12 + 6] = lr[i * 2 + 1].x;
  1569. vwptr[i * 12 + 7] = lr[i * 2 + 1].y;
  1570. vwptr[i * 12 + 8] = -POLY_HEIGHT;
  1571. vwptr[i * 12 + 9] = lr[i * 2 + 0].x;
  1572. vwptr[i * 12 + 10] = lr[i * 2 + 0].y;
  1573. vwptr[i * 12 + 11] = -POLY_HEIGHT;
  1574. iwptr[i * 6 + 0] = i * 4 + 0;
  1575. iwptr[i * 6 + 1] = i * 4 + 1;
  1576. iwptr[i * 6 + 2] = i * 4 + 2;
  1577. iwptr[i * 6 + 3] = i * 4 + 2;
  1578. iwptr[i * 6 + 4] = i * 4 + 3;
  1579. iwptr[i * 6 + 5] = i * 4 + 0;
  1580. }
  1581. }
  1582. if (oc->vertex_array == 0) {
  1583. oc->line_point_count = lc;
  1584. glGenVertexArrays(1, &oc->vertex_array);
  1585. glBindVertexArray(oc->vertex_array);
  1586. glGenBuffers(1, &oc->vertex_buffer);
  1587. glBindBuffer(GL_ARRAY_BUFFER, oc->vertex_buffer);
  1588. GLES3::Utilities::get_singleton()->buffer_allocate_data(GL_ARRAY_BUFFER, oc->vertex_buffer, lc * 6 * sizeof(float), geometry.ptr(), GL_STATIC_DRAW, "Occluder polygon vertex buffer");
  1589. glEnableVertexAttribArray(RS::ARRAY_VERTEX);
  1590. glVertexAttribPointer(RS::ARRAY_VERTEX, 3, GL_FLOAT, GL_FALSE, 3 * sizeof(float), nullptr);
  1591. glGenBuffers(1, &oc->index_buffer);
  1592. glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, oc->index_buffer);
  1593. GLES3::Utilities::get_singleton()->buffer_allocate_data(GL_ELEMENT_ARRAY_BUFFER, oc->index_buffer, 3 * lc * sizeof(uint16_t), indices.ptr(), GL_STATIC_DRAW, "Occluder polygon index buffer");
  1594. glBindVertexArray(0);
  1595. } else {
  1596. glBindBuffer(GL_ARRAY_BUFFER, oc->vertex_buffer);
  1597. glBufferData(GL_ARRAY_BUFFER, lc * 6 * sizeof(float), geometry.ptr(), GL_STATIC_DRAW);
  1598. glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, oc->index_buffer);
  1599. glBufferData(GL_ELEMENT_ARRAY_BUFFER, 3 * lc * sizeof(uint16_t), indices.ptr(), GL_STATIC_DRAW);
  1600. glBindBuffer(GL_ARRAY_BUFFER, 0);
  1601. glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0);
  1602. }
  1603. }
  1604. // sdf
  1605. Vector<int> sdf_indices;
  1606. if (p_points.size()) {
  1607. if (p_closed) {
  1608. sdf_indices = Geometry2D::triangulate_polygon(p_points);
  1609. oc->sdf_is_lines = false;
  1610. } else {
  1611. int max = p_points.size();
  1612. sdf_indices.resize(max * 2);
  1613. int *iw = sdf_indices.ptrw();
  1614. for (int i = 0; i < max; i++) {
  1615. iw[i * 2 + 0] = i;
  1616. iw[i * 2 + 1] = (i + 1) % max;
  1617. }
  1618. oc->sdf_is_lines = true;
  1619. }
  1620. }
  1621. if (oc->sdf_index_count != sdf_indices.size() && oc->sdf_point_count != p_points.size() && oc->sdf_vertex_array != 0) {
  1622. glDeleteVertexArrays(1, &oc->sdf_vertex_array);
  1623. GLES3::Utilities::get_singleton()->buffer_free_data(oc->sdf_vertex_buffer);
  1624. GLES3::Utilities::get_singleton()->buffer_free_data(oc->sdf_index_buffer);
  1625. oc->sdf_vertex_array = 0;
  1626. oc->sdf_vertex_buffer = 0;
  1627. oc->sdf_index_buffer = 0;
  1628. oc->sdf_index_count = sdf_indices.size();
  1629. oc->sdf_point_count = p_points.size();
  1630. }
  1631. if (sdf_indices.size()) {
  1632. if (oc->sdf_vertex_array == 0) {
  1633. oc->sdf_index_count = sdf_indices.size();
  1634. oc->sdf_point_count = p_points.size();
  1635. glGenVertexArrays(1, &oc->sdf_vertex_array);
  1636. glBindVertexArray(oc->sdf_vertex_array);
  1637. glGenBuffers(1, &oc->sdf_vertex_buffer);
  1638. glBindBuffer(GL_ARRAY_BUFFER, oc->sdf_vertex_buffer);
  1639. GLES3::Utilities::get_singleton()->buffer_allocate_data(GL_ARRAY_BUFFER, oc->sdf_vertex_buffer, oc->sdf_point_count * 2 * sizeof(float), p_points.to_byte_array().ptr(), GL_STATIC_DRAW, "Occluder polygon SDF vertex buffer");
  1640. glEnableVertexAttribArray(RS::ARRAY_VERTEX);
  1641. glVertexAttribPointer(RS::ARRAY_VERTEX, 2, GL_FLOAT, GL_FALSE, 2 * sizeof(float), nullptr);
  1642. glGenBuffers(1, &oc->sdf_index_buffer);
  1643. glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, oc->sdf_index_buffer);
  1644. GLES3::Utilities::get_singleton()->buffer_allocate_data(GL_ELEMENT_ARRAY_BUFFER, oc->sdf_index_buffer, oc->sdf_index_count * sizeof(uint32_t), sdf_indices.to_byte_array().ptr(), GL_STATIC_DRAW, "Occluder polygon SDF index buffer");
  1645. glBindVertexArray(0);
  1646. } else {
  1647. glBindBuffer(GL_ARRAY_BUFFER, oc->sdf_vertex_buffer);
  1648. glBufferData(GL_ARRAY_BUFFER, p_points.size() * 2 * sizeof(float), p_points.to_byte_array().ptr(), GL_STATIC_DRAW);
  1649. glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, oc->sdf_index_buffer);
  1650. glBufferData(GL_ELEMENT_ARRAY_BUFFER, sdf_indices.size() * sizeof(uint32_t), sdf_indices.to_byte_array().ptr(), GL_STATIC_DRAW);
  1651. glBindBuffer(GL_ARRAY_BUFFER, 0);
  1652. glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0);
  1653. }
  1654. }
  1655. }
  1656. void RasterizerCanvasGLES3::occluder_polygon_set_cull_mode(RID p_occluder, RS::CanvasOccluderPolygonCullMode p_mode) {
  1657. OccluderPolygon *oc = occluder_polygon_owner.get_or_null(p_occluder);
  1658. ERR_FAIL_COND(!oc);
  1659. oc->cull_mode = p_mode;
  1660. }
  1661. void RasterizerCanvasGLES3::set_shadow_texture_size(int p_size) {
  1662. GLES3::Config *config = GLES3::Config::get_singleton();
  1663. p_size = nearest_power_of_2_templated(p_size);
  1664. if (p_size == state.shadow_texture_size) {
  1665. return;
  1666. }
  1667. if (p_size > config->max_texture_size) {
  1668. p_size = config->max_texture_size;
  1669. WARN_PRINT("Attempting to set CanvasItem shadow atlas size to " + itos(p_size) + " which is beyond limit of " + itos(config->max_texture_size) + "supported by hardware.");
  1670. }
  1671. state.shadow_texture_size = p_size;
  1672. }
  1673. bool RasterizerCanvasGLES3::free(RID p_rid) {
  1674. if (canvas_light_owner.owns(p_rid)) {
  1675. CanvasLight *cl = canvas_light_owner.get_or_null(p_rid);
  1676. ERR_FAIL_COND_V(!cl, false);
  1677. canvas_light_owner.free(p_rid);
  1678. } else if (occluder_polygon_owner.owns(p_rid)) {
  1679. occluder_polygon_set_shape(p_rid, Vector<Vector2>(), false);
  1680. occluder_polygon_owner.free(p_rid);
  1681. } else {
  1682. return false;
  1683. }
  1684. return true;
  1685. }
  1686. void RasterizerCanvasGLES3::update() {
  1687. }
  1688. void RasterizerCanvasGLES3::canvas_begin(RID p_to_render_target, bool p_to_backbuffer) {
  1689. GLES3::TextureStorage *texture_storage = GLES3::TextureStorage::get_singleton();
  1690. GLES3::Config *config = GLES3::Config::get_singleton();
  1691. GLES3::RenderTarget *render_target = texture_storage->get_render_target(p_to_render_target);
  1692. if (p_to_backbuffer) {
  1693. glBindFramebuffer(GL_FRAMEBUFFER, render_target->backbuffer_fbo);
  1694. glActiveTexture(GL_TEXTURE0 + config->max_texture_image_units - 4);
  1695. GLES3::Texture *tex = texture_storage->get_texture(texture_storage->texture_gl_get_default(GLES3::DEFAULT_GL_TEXTURE_WHITE));
  1696. glBindTexture(GL_TEXTURE_2D, tex->tex_id);
  1697. } else {
  1698. glBindFramebuffer(GL_FRAMEBUFFER, render_target->fbo);
  1699. glActiveTexture(GL_TEXTURE0 + config->max_texture_image_units - 4);
  1700. glBindTexture(GL_TEXTURE_2D, render_target->backbuffer);
  1701. }
  1702. if (render_target->is_transparent || p_to_backbuffer) {
  1703. state.transparent_render_target = true;
  1704. glBlendFuncSeparate(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA, GL_ONE, GL_ONE_MINUS_SRC_ALPHA);
  1705. } else {
  1706. state.transparent_render_target = false;
  1707. glBlendFuncSeparate(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA, GL_ZERO, GL_ONE);
  1708. }
  1709. if (render_target && render_target->clear_requested) {
  1710. const Color &col = render_target->clear_color;
  1711. glClearColor(col.r, col.g, col.b, render_target->is_transparent ? col.a : 1.0f);
  1712. glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT | GL_STENCIL_BUFFER_BIT);
  1713. render_target->clear_requested = false;
  1714. }
  1715. glActiveTexture(GL_TEXTURE0);
  1716. GLES3::Texture *tex = texture_storage->get_texture(texture_storage->texture_gl_get_default(GLES3::DEFAULT_GL_TEXTURE_WHITE));
  1717. glBindTexture(GL_TEXTURE_2D, tex->tex_id);
  1718. }
  1719. void RasterizerCanvasGLES3::_bind_canvas_texture(RID p_texture, RS::CanvasItemTextureFilter p_base_filter, RS::CanvasItemTextureRepeat p_base_repeat) {
  1720. GLES3::TextureStorage *texture_storage = GLES3::TextureStorage::get_singleton();
  1721. GLES3::Config *config = GLES3::Config::get_singleton();
  1722. if (p_texture == RID()) {
  1723. p_texture = default_canvas_texture;
  1724. }
  1725. if (state.current_tex == p_texture && state.current_filter_mode == p_base_filter && state.current_repeat_mode == p_base_repeat) {
  1726. return;
  1727. }
  1728. state.current_tex = p_texture;
  1729. state.current_filter_mode = p_base_filter;
  1730. state.current_repeat_mode = p_base_repeat;
  1731. GLES3::CanvasTexture *ct = nullptr;
  1732. GLES3::Texture *t = texture_storage->get_texture(p_texture);
  1733. if (t) {
  1734. ERR_FAIL_COND(!t->canvas_texture);
  1735. ct = t->canvas_texture;
  1736. if (t->render_target) {
  1737. t->render_target->used_in_frame = true;
  1738. }
  1739. } else {
  1740. ct = texture_storage->get_canvas_texture(p_texture);
  1741. }
  1742. if (!ct) {
  1743. // Invalid Texture RID.
  1744. _bind_canvas_texture(default_canvas_texture, p_base_filter, p_base_repeat);
  1745. return;
  1746. }
  1747. RS::CanvasItemTextureFilter filter = ct->texture_filter != RS::CANVAS_ITEM_TEXTURE_FILTER_DEFAULT ? ct->texture_filter : p_base_filter;
  1748. ERR_FAIL_COND(filter == RS::CANVAS_ITEM_TEXTURE_FILTER_DEFAULT);
  1749. RS::CanvasItemTextureRepeat repeat = ct->texture_repeat != RS::CANVAS_ITEM_TEXTURE_REPEAT_DEFAULT ? ct->texture_repeat : p_base_repeat;
  1750. ERR_FAIL_COND(repeat == RS::CANVAS_ITEM_TEXTURE_REPEAT_DEFAULT);
  1751. GLES3::Texture *texture = texture_storage->get_texture(ct->diffuse);
  1752. if (!texture) {
  1753. GLES3::Texture *tex = texture_storage->get_texture(texture_storage->texture_gl_get_default(GLES3::DEFAULT_GL_TEXTURE_WHITE));
  1754. glActiveTexture(GL_TEXTURE0);
  1755. glBindTexture(GL_TEXTURE_2D, tex->tex_id);
  1756. } else {
  1757. glActiveTexture(GL_TEXTURE0);
  1758. glBindTexture(GL_TEXTURE_2D, texture->tex_id);
  1759. texture->gl_set_filter(filter);
  1760. texture->gl_set_repeat(repeat);
  1761. if (texture->render_target) {
  1762. texture->render_target->used_in_frame = true;
  1763. }
  1764. }
  1765. GLES3::Texture *normal_map = texture_storage->get_texture(ct->normal_map);
  1766. if (!normal_map) {
  1767. glActiveTexture(GL_TEXTURE0 + config->max_texture_image_units - 6);
  1768. GLES3::Texture *tex = texture_storage->get_texture(texture_storage->texture_gl_get_default(GLES3::DEFAULT_GL_TEXTURE_NORMAL));
  1769. glBindTexture(GL_TEXTURE_2D, tex->tex_id);
  1770. } else {
  1771. glActiveTexture(GL_TEXTURE0 + config->max_texture_image_units - 6);
  1772. glBindTexture(GL_TEXTURE_2D, normal_map->tex_id);
  1773. normal_map->gl_set_filter(filter);
  1774. normal_map->gl_set_repeat(repeat);
  1775. if (normal_map->render_target) {
  1776. normal_map->render_target->used_in_frame = true;
  1777. }
  1778. }
  1779. GLES3::Texture *specular_map = texture_storage->get_texture(ct->specular);
  1780. if (!specular_map) {
  1781. glActiveTexture(GL_TEXTURE0 + config->max_texture_image_units - 7);
  1782. GLES3::Texture *tex = texture_storage->get_texture(texture_storage->texture_gl_get_default(GLES3::DEFAULT_GL_TEXTURE_WHITE));
  1783. glBindTexture(GL_TEXTURE_2D, tex->tex_id);
  1784. } else {
  1785. glActiveTexture(GL_TEXTURE0 + config->max_texture_image_units - 7);
  1786. glBindTexture(GL_TEXTURE_2D, specular_map->tex_id);
  1787. specular_map->gl_set_filter(filter);
  1788. specular_map->gl_set_repeat(repeat);
  1789. if (specular_map->render_target) {
  1790. specular_map->render_target->used_in_frame = true;
  1791. }
  1792. }
  1793. }
  1794. void RasterizerCanvasGLES3::_prepare_canvas_texture(RID p_texture, RS::CanvasItemTextureFilter p_base_filter, RS::CanvasItemTextureRepeat p_base_repeat, uint32_t &r_index, Size2 &r_texpixel_size) {
  1795. GLES3::TextureStorage *texture_storage = GLES3::TextureStorage::get_singleton();
  1796. if (p_texture == RID()) {
  1797. p_texture = default_canvas_texture;
  1798. }
  1799. GLES3::CanvasTexture *ct = nullptr;
  1800. GLES3::Texture *t = texture_storage->get_texture(p_texture);
  1801. if (t) {
  1802. //regular texture
  1803. if (!t->canvas_texture) {
  1804. t->canvas_texture = memnew(GLES3::CanvasTexture);
  1805. t->canvas_texture->diffuse = p_texture;
  1806. }
  1807. ct = t->canvas_texture;
  1808. } else {
  1809. ct = texture_storage->get_canvas_texture(p_texture);
  1810. }
  1811. if (!ct) {
  1812. // Invalid Texture RID.
  1813. _prepare_canvas_texture(default_canvas_texture, p_base_filter, p_base_repeat, r_index, r_texpixel_size);
  1814. return;
  1815. }
  1816. GLES3::Texture *texture = texture_storage->get_texture(ct->diffuse);
  1817. Size2i size_cache;
  1818. if (!texture) {
  1819. ct->diffuse = texture_storage->texture_gl_get_default(GLES3::DEFAULT_GL_TEXTURE_WHITE);
  1820. GLES3::Texture *tex = texture_storage->get_texture(ct->diffuse);
  1821. size_cache = Size2i(tex->width, tex->height);
  1822. } else {
  1823. size_cache = Size2i(texture->width, texture->height);
  1824. }
  1825. GLES3::Texture *normal_map = texture_storage->get_texture(ct->normal_map);
  1826. if (ct->specular_color.a < 0.999) {
  1827. state.instance_data_array[r_index].flags |= FLAGS_DEFAULT_SPECULAR_MAP_USED;
  1828. } else {
  1829. state.instance_data_array[r_index].flags &= ~FLAGS_DEFAULT_SPECULAR_MAP_USED;
  1830. }
  1831. if (normal_map) {
  1832. state.instance_data_array[r_index].flags |= FLAGS_DEFAULT_NORMAL_MAP_USED;
  1833. } else {
  1834. state.instance_data_array[r_index].flags &= ~FLAGS_DEFAULT_NORMAL_MAP_USED;
  1835. }
  1836. state.instance_data_array[r_index].specular_shininess = uint32_t(CLAMP(ct->specular_color.a * 255.0, 0, 255)) << 24;
  1837. state.instance_data_array[r_index].specular_shininess |= uint32_t(CLAMP(ct->specular_color.b * 255.0, 0, 255)) << 16;
  1838. state.instance_data_array[r_index].specular_shininess |= uint32_t(CLAMP(ct->specular_color.g * 255.0, 0, 255)) << 8;
  1839. state.instance_data_array[r_index].specular_shininess |= uint32_t(CLAMP(ct->specular_color.r * 255.0, 0, 255));
  1840. r_texpixel_size.x = 1.0 / float(size_cache.x);
  1841. r_texpixel_size.y = 1.0 / float(size_cache.y);
  1842. state.instance_data_array[r_index].color_texture_pixel_size[0] = r_texpixel_size.x;
  1843. state.instance_data_array[r_index].color_texture_pixel_size[1] = r_texpixel_size.y;
  1844. }
  1845. void RasterizerCanvasGLES3::reset_canvas() {
  1846. glDisable(GL_CULL_FACE);
  1847. glDisable(GL_DEPTH_TEST);
  1848. glDisable(GL_SCISSOR_TEST);
  1849. glEnable(GL_BLEND);
  1850. glBlendEquation(GL_FUNC_ADD);
  1851. glBlendFuncSeparate(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA, GL_ZERO, GL_ONE);
  1852. glActiveTexture(GL_TEXTURE0 + GLES3::Config::get_singleton()->max_texture_image_units - 2);
  1853. glBindTexture(GL_TEXTURE_2D, 0);
  1854. glActiveTexture(GL_TEXTURE0 + GLES3::Config::get_singleton()->max_texture_image_units - 3);
  1855. glBindTexture(GL_TEXTURE_2D, 0);
  1856. glActiveTexture(GL_TEXTURE0);
  1857. glBindBuffer(GL_ARRAY_BUFFER, 0);
  1858. glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0);
  1859. }
  1860. void RasterizerCanvasGLES3::draw_lens_distortion_rect(const Rect2 &p_rect, float p_k1, float p_k2, const Vector2 &p_eye_center, float p_oversample) {
  1861. }
  1862. RendererCanvasRender::PolygonID RasterizerCanvasGLES3::request_polygon(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) {
  1863. // We interleave the vertex data into one big VBO to improve cache coherence
  1864. uint32_t vertex_count = p_points.size();
  1865. uint32_t stride = 2;
  1866. if ((uint32_t)p_colors.size() == vertex_count) {
  1867. stride += 4;
  1868. }
  1869. if ((uint32_t)p_uvs.size() == vertex_count) {
  1870. stride += 2;
  1871. }
  1872. if ((uint32_t)p_bones.size() == vertex_count * 4 && (uint32_t)p_weights.size() == vertex_count * 4) {
  1873. stride += 4;
  1874. }
  1875. PolygonBuffers pb;
  1876. glGenBuffers(1, &pb.vertex_buffer);
  1877. glGenVertexArrays(1, &pb.vertex_array);
  1878. glBindVertexArray(pb.vertex_array);
  1879. pb.count = vertex_count;
  1880. pb.index_buffer = 0;
  1881. uint32_t buffer_size = stride * p_points.size();
  1882. Vector<uint8_t> polygon_buffer;
  1883. polygon_buffer.resize(buffer_size * sizeof(float));
  1884. {
  1885. glBindBuffer(GL_ARRAY_BUFFER, pb.vertex_buffer);
  1886. uint8_t *r = polygon_buffer.ptrw();
  1887. float *fptr = reinterpret_cast<float *>(r);
  1888. uint32_t *uptr = (uint32_t *)r;
  1889. uint32_t base_offset = 0;
  1890. {
  1891. // Always uses vertex positions
  1892. glEnableVertexAttribArray(RS::ARRAY_VERTEX);
  1893. glVertexAttribPointer(RS::ARRAY_VERTEX, 2, GL_FLOAT, GL_FALSE, stride * sizeof(float), nullptr);
  1894. const Vector2 *points_ptr = p_points.ptr();
  1895. for (uint32_t i = 0; i < vertex_count; i++) {
  1896. fptr[base_offset + i * stride + 0] = points_ptr[i].x;
  1897. fptr[base_offset + i * stride + 1] = points_ptr[i].y;
  1898. }
  1899. base_offset += 2;
  1900. }
  1901. // Next add colors
  1902. if ((uint32_t)p_colors.size() == vertex_count) {
  1903. glEnableVertexAttribArray(RS::ARRAY_COLOR);
  1904. glVertexAttribPointer(RS::ARRAY_COLOR, 4, GL_FLOAT, GL_FALSE, stride * sizeof(float), CAST_INT_TO_UCHAR_PTR(base_offset * sizeof(float)));
  1905. const Color *color_ptr = p_colors.ptr();
  1906. for (uint32_t i = 0; i < vertex_count; i++) {
  1907. fptr[base_offset + i * stride + 0] = color_ptr[i].r;
  1908. fptr[base_offset + i * stride + 1] = color_ptr[i].g;
  1909. fptr[base_offset + i * stride + 2] = color_ptr[i].b;
  1910. fptr[base_offset + i * stride + 3] = color_ptr[i].a;
  1911. }
  1912. base_offset += 4;
  1913. } else {
  1914. glDisableVertexAttribArray(RS::ARRAY_COLOR);
  1915. pb.color_disabled = true;
  1916. pb.color = p_colors.size() == 1 ? p_colors[0] : Color(1.0, 1.0, 1.0, 1.0);
  1917. }
  1918. if ((uint32_t)p_uvs.size() == vertex_count) {
  1919. glEnableVertexAttribArray(RS::ARRAY_TEX_UV);
  1920. glVertexAttribPointer(RS::ARRAY_TEX_UV, 2, GL_FLOAT, GL_FALSE, stride * sizeof(float), CAST_INT_TO_UCHAR_PTR(base_offset * sizeof(float)));
  1921. const Vector2 *uv_ptr = p_uvs.ptr();
  1922. for (uint32_t i = 0; i < vertex_count; i++) {
  1923. fptr[base_offset + i * stride + 0] = uv_ptr[i].x;
  1924. fptr[base_offset + i * stride + 1] = uv_ptr[i].y;
  1925. }
  1926. base_offset += 2;
  1927. } else {
  1928. glDisableVertexAttribArray(RS::ARRAY_TEX_UV);
  1929. }
  1930. if ((uint32_t)p_indices.size() == vertex_count * 4 && (uint32_t)p_weights.size() == vertex_count * 4) {
  1931. glEnableVertexAttribArray(RS::ARRAY_BONES);
  1932. glVertexAttribPointer(RS::ARRAY_BONES, 4, GL_UNSIGNED_INT, GL_FALSE, stride * sizeof(float), CAST_INT_TO_UCHAR_PTR(base_offset * sizeof(float)));
  1933. const int *bone_ptr = p_bones.ptr();
  1934. for (uint32_t i = 0; i < vertex_count; i++) {
  1935. uint16_t *bone16w = (uint16_t *)&uptr[base_offset + i * stride];
  1936. bone16w[0] = bone_ptr[i * 4 + 0];
  1937. bone16w[1] = bone_ptr[i * 4 + 1];
  1938. bone16w[2] = bone_ptr[i * 4 + 2];
  1939. bone16w[3] = bone_ptr[i * 4 + 3];
  1940. }
  1941. base_offset += 2;
  1942. } else {
  1943. glDisableVertexAttribArray(RS::ARRAY_BONES);
  1944. }
  1945. if ((uint32_t)p_weights.size() == vertex_count * 4) {
  1946. glEnableVertexAttribArray(RS::ARRAY_WEIGHTS);
  1947. glVertexAttribPointer(RS::ARRAY_WEIGHTS, 4, GL_FLOAT, GL_FALSE, stride * sizeof(float), CAST_INT_TO_UCHAR_PTR(base_offset * sizeof(float)));
  1948. const float *weight_ptr = p_weights.ptr();
  1949. for (uint32_t i = 0; i < vertex_count; i++) {
  1950. uint16_t *weight16w = (uint16_t *)&uptr[base_offset + i * stride];
  1951. weight16w[0] = CLAMP(weight_ptr[i * 4 + 0] * 65535, 0, 65535);
  1952. weight16w[1] = CLAMP(weight_ptr[i * 4 + 1] * 65535, 0, 65535);
  1953. weight16w[2] = CLAMP(weight_ptr[i * 4 + 2] * 65535, 0, 65535);
  1954. weight16w[3] = CLAMP(weight_ptr[i * 4 + 3] * 65535, 0, 65535);
  1955. }
  1956. base_offset += 2;
  1957. } else {
  1958. glDisableVertexAttribArray(RS::ARRAY_WEIGHTS);
  1959. }
  1960. ERR_FAIL_COND_V(base_offset != stride, 0);
  1961. GLES3::Utilities::get_singleton()->buffer_allocate_data(GL_ARRAY_BUFFER, pb.vertex_buffer, vertex_count * stride * sizeof(float), polygon_buffer.ptr(), GL_STATIC_DRAW, "Polygon 2D vertex buffer");
  1962. }
  1963. if (p_indices.size()) {
  1964. //create indices, as indices were requested
  1965. Vector<uint8_t> index_buffer;
  1966. index_buffer.resize(p_indices.size() * sizeof(int32_t));
  1967. {
  1968. uint8_t *w = index_buffer.ptrw();
  1969. memcpy(w, p_indices.ptr(), sizeof(int32_t) * p_indices.size());
  1970. }
  1971. glGenBuffers(1, &pb.index_buffer);
  1972. glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, pb.index_buffer);
  1973. GLES3::Utilities::get_singleton()->buffer_allocate_data(GL_ELEMENT_ARRAY_BUFFER, pb.index_buffer, p_indices.size() * 4, index_buffer.ptr(), GL_STATIC_DRAW, "Polygon 2D index buffer");
  1974. pb.count = p_indices.size();
  1975. }
  1976. glBindVertexArray(0);
  1977. glBindBuffer(GL_ARRAY_BUFFER, 0);
  1978. glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0);
  1979. PolygonID id = polygon_buffers.last_id++;
  1980. polygon_buffers.polygons[id] = pb;
  1981. return id;
  1982. }
  1983. void RasterizerCanvasGLES3::free_polygon(PolygonID p_polygon) {
  1984. PolygonBuffers *pb_ptr = polygon_buffers.polygons.getptr(p_polygon);
  1985. ERR_FAIL_COND(!pb_ptr);
  1986. PolygonBuffers &pb = *pb_ptr;
  1987. if (pb.index_buffer != 0) {
  1988. GLES3::Utilities::get_singleton()->buffer_free_data(pb.index_buffer);
  1989. }
  1990. glDeleteVertexArrays(1, &pb.vertex_array);
  1991. GLES3::Utilities::get_singleton()->buffer_free_data(pb.vertex_buffer);
  1992. polygon_buffers.polygons.erase(p_polygon);
  1993. }
  1994. // Creates a new uniform buffer and uses it right away
  1995. // This expands the instance buffer continually
  1996. // In theory allocations can reach as high as number of windows * 3 frames
  1997. // because OpenGL can start rendering subsequent frames before finishing the current one
  1998. void RasterizerCanvasGLES3::_allocate_instance_data_buffer() {
  1999. GLuint new_buffers[3];
  2000. glGenBuffers(3, new_buffers);
  2001. // Batch UBO.
  2002. glBindBuffer(GL_ARRAY_BUFFER, new_buffers[0]);
  2003. GLES3::Utilities::get_singleton()->buffer_allocate_data(GL_ARRAY_BUFFER, new_buffers[0], data.max_instance_buffer_size, nullptr, GL_STREAM_DRAW, "2D Batch UBO[" + itos(state.current_data_buffer_index) + "][0]");
  2004. // Light uniform buffer.
  2005. glBindBuffer(GL_UNIFORM_BUFFER, new_buffers[1]);
  2006. GLES3::Utilities::get_singleton()->buffer_allocate_data(GL_UNIFORM_BUFFER, new_buffers[1], sizeof(LightUniform) * data.max_lights_per_render, nullptr, GL_STREAM_DRAW, "2D Lights UBO[" + itos(state.current_data_buffer_index) + "]");
  2007. // State buffer.
  2008. glBindBuffer(GL_UNIFORM_BUFFER, new_buffers[2]);
  2009. GLES3::Utilities::get_singleton()->buffer_allocate_data(GL_UNIFORM_BUFFER, new_buffers[2], sizeof(StateBuffer), nullptr, GL_STREAM_DRAW, "2D State UBO[" + itos(state.current_data_buffer_index) + "]");
  2010. state.current_data_buffer_index = (state.current_data_buffer_index + 1);
  2011. DataBuffer db;
  2012. db.instance_buffers.push_back(new_buffers[0]);
  2013. db.light_ubo = new_buffers[1];
  2014. db.state_ubo = new_buffers[2];
  2015. db.last_frame_used = RSG::rasterizer->get_frame_number();
  2016. state.canvas_instance_data_buffers.insert(state.current_data_buffer_index, db);
  2017. state.current_data_buffer_index = state.current_data_buffer_index % state.canvas_instance_data_buffers.size();
  2018. glBindBuffer(GL_ARRAY_BUFFER, 0);
  2019. glBindBuffer(GL_UNIFORM_BUFFER, 0);
  2020. }
  2021. void RasterizerCanvasGLES3::_allocate_instance_buffer() {
  2022. state.current_instance_buffer_index++;
  2023. if (int(state.current_instance_buffer_index) < state.canvas_instance_data_buffers[state.current_data_buffer_index].instance_buffers.size()) {
  2024. // We already allocated another buffer in a previous frame, so we can just use it.
  2025. return;
  2026. }
  2027. GLuint new_buffer;
  2028. glGenBuffers(1, &new_buffer);
  2029. glBindBuffer(GL_ARRAY_BUFFER, new_buffer);
  2030. GLES3::Utilities::get_singleton()->buffer_allocate_data(GL_ARRAY_BUFFER, new_buffer, data.max_instance_buffer_size, nullptr, GL_STREAM_DRAW, "Batch UBO[" + itos(state.current_data_buffer_index) + "][" + itos(state.canvas_instance_data_buffers[state.current_data_buffer_index].instance_buffers.size()) + "]");
  2031. state.canvas_instance_data_buffers[state.current_data_buffer_index].instance_buffers.push_back(new_buffer);
  2032. glBindBuffer(GL_ARRAY_BUFFER, 0);
  2033. }
  2034. void RasterizerCanvasGLES3::set_time(double p_time) {
  2035. state.time = p_time;
  2036. }
  2037. RasterizerCanvasGLES3 *RasterizerCanvasGLES3::singleton = nullptr;
  2038. RasterizerCanvasGLES3 *RasterizerCanvasGLES3::get_singleton() {
  2039. return singleton;
  2040. }
  2041. RasterizerCanvasGLES3::RasterizerCanvasGLES3() {
  2042. singleton = this;
  2043. GLES3::TextureStorage *texture_storage = GLES3::TextureStorage::get_singleton();
  2044. GLES3::MaterialStorage *material_storage = GLES3::MaterialStorage::get_singleton();
  2045. GLES3::Config *config = GLES3::Config::get_singleton();
  2046. polygon_buffers.last_id = 1;
  2047. // quad buffer
  2048. {
  2049. glGenBuffers(1, &data.canvas_quad_vertices);
  2050. glBindBuffer(GL_ARRAY_BUFFER, data.canvas_quad_vertices);
  2051. const float qv[8] = {
  2052. 0, 0,
  2053. 0, 1,
  2054. 1, 1,
  2055. 1, 0
  2056. };
  2057. glBufferData(GL_ARRAY_BUFFER, sizeof(float) * 8, qv, GL_STATIC_DRAW);
  2058. glBindBuffer(GL_ARRAY_BUFFER, 0);
  2059. glGenVertexArrays(1, &data.canvas_quad_array);
  2060. glBindVertexArray(data.canvas_quad_array);
  2061. glBindBuffer(GL_ARRAY_BUFFER, data.canvas_quad_vertices);
  2062. glVertexAttribPointer(0, 2, GL_FLOAT, GL_FALSE, sizeof(float) * 2, nullptr);
  2063. glEnableVertexAttribArray(0);
  2064. glBindVertexArray(0);
  2065. glBindBuffer(GL_ARRAY_BUFFER, 0); //unbind
  2066. }
  2067. {
  2068. //particle quad buffers
  2069. glGenBuffers(1, &data.particle_quad_vertices);
  2070. glBindBuffer(GL_ARRAY_BUFFER, data.particle_quad_vertices);
  2071. {
  2072. //quad of size 1, with pivot on the center for particles, then regular UVS. Color is general plus fetched from particle
  2073. const float qv[16] = {
  2074. -0.5, -0.5,
  2075. 0.0, 0.0,
  2076. -0.5, 0.5,
  2077. 0.0, 1.0,
  2078. 0.5, 0.5,
  2079. 1.0, 1.0,
  2080. 0.5, -0.5,
  2081. 1.0, 0.0
  2082. };
  2083. glBufferData(GL_ARRAY_BUFFER, sizeof(float) * 16, qv, GL_STATIC_DRAW);
  2084. }
  2085. glBindBuffer(GL_ARRAY_BUFFER, 0); //unbind
  2086. glGenVertexArrays(1, &data.particle_quad_array);
  2087. glBindVertexArray(data.particle_quad_array);
  2088. glBindBuffer(GL_ARRAY_BUFFER, data.particle_quad_vertices);
  2089. glEnableVertexAttribArray(RS::ARRAY_VERTEX);
  2090. glVertexAttribPointer(RS::ARRAY_VERTEX, 2, GL_FLOAT, GL_FALSE, sizeof(float) * 4, nullptr);
  2091. glEnableVertexAttribArray(RS::ARRAY_TEX_UV);
  2092. glVertexAttribPointer(RS::ARRAY_TEX_UV, 2, GL_FLOAT, GL_FALSE, sizeof(float) * 4, CAST_INT_TO_UCHAR_PTR(8));
  2093. glBindVertexArray(0);
  2094. glBindBuffer(GL_ARRAY_BUFFER, 0); //unbind
  2095. }
  2096. // ninepatch buffers
  2097. {
  2098. // array buffer
  2099. glGenBuffers(1, &data.ninepatch_vertices);
  2100. glBindBuffer(GL_ARRAY_BUFFER, data.ninepatch_vertices);
  2101. glBufferData(GL_ARRAY_BUFFER, sizeof(float) * (16 + 16) * 2, nullptr, GL_DYNAMIC_DRAW);
  2102. glBindBuffer(GL_ARRAY_BUFFER, 0);
  2103. // element buffer
  2104. glGenBuffers(1, &data.ninepatch_elements);
  2105. glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, data.ninepatch_elements);
  2106. #define _EIDX(y, x) (y * 4 + x)
  2107. uint8_t elems[3 * 2 * 9] = {
  2108. // first row
  2109. _EIDX(0, 0), _EIDX(0, 1), _EIDX(1, 1),
  2110. _EIDX(1, 1), _EIDX(1, 0), _EIDX(0, 0),
  2111. _EIDX(0, 1), _EIDX(0, 2), _EIDX(1, 2),
  2112. _EIDX(1, 2), _EIDX(1, 1), _EIDX(0, 1),
  2113. _EIDX(0, 2), _EIDX(0, 3), _EIDX(1, 3),
  2114. _EIDX(1, 3), _EIDX(1, 2), _EIDX(0, 2),
  2115. // second row
  2116. _EIDX(1, 0), _EIDX(1, 1), _EIDX(2, 1),
  2117. _EIDX(2, 1), _EIDX(2, 0), _EIDX(1, 0),
  2118. // the center one would be here, but we'll put it at the end
  2119. // so it's easier to disable the center and be able to use
  2120. // one draw call for both
  2121. _EIDX(1, 2), _EIDX(1, 3), _EIDX(2, 3),
  2122. _EIDX(2, 3), _EIDX(2, 2), _EIDX(1, 2),
  2123. // third row
  2124. _EIDX(2, 0), _EIDX(2, 1), _EIDX(3, 1),
  2125. _EIDX(3, 1), _EIDX(3, 0), _EIDX(2, 0),
  2126. _EIDX(2, 1), _EIDX(2, 2), _EIDX(3, 2),
  2127. _EIDX(3, 2), _EIDX(3, 1), _EIDX(2, 1),
  2128. _EIDX(2, 2), _EIDX(2, 3), _EIDX(3, 3),
  2129. _EIDX(3, 3), _EIDX(3, 2), _EIDX(2, 2),
  2130. // center field
  2131. _EIDX(1, 1), _EIDX(1, 2), _EIDX(2, 2),
  2132. _EIDX(2, 2), _EIDX(2, 1), _EIDX(1, 1)
  2133. };
  2134. #undef _EIDX
  2135. glBufferData(GL_ELEMENT_ARRAY_BUFFER, sizeof(elems), elems, GL_STATIC_DRAW);
  2136. glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0);
  2137. }
  2138. int uniform_max_size = config->max_uniform_buffer_size;
  2139. if (uniform_max_size < 65536) {
  2140. data.max_lights_per_render = 64;
  2141. } else {
  2142. data.max_lights_per_render = 256;
  2143. }
  2144. // Reserve 3 Uniform Buffers for instance data Frame N, N+1 and N+2
  2145. data.max_instances_per_buffer = uint32_t(GLOBAL_GET("rendering/gl_compatibility/item_buffer_size"));
  2146. data.max_instance_buffer_size = data.max_instances_per_buffer * sizeof(InstanceData); // 16,384 instances * 128 bytes = 2,097,152 bytes = 2,048 kb
  2147. state.canvas_instance_data_buffers.resize(3);
  2148. state.canvas_instance_batches.reserve(200);
  2149. for (int i = 0; i < 3; i++) {
  2150. GLuint new_buffers[3];
  2151. glGenBuffers(3, new_buffers);
  2152. // Batch UBO.
  2153. glBindBuffer(GL_ARRAY_BUFFER, new_buffers[0]);
  2154. GLES3::Utilities::get_singleton()->buffer_allocate_data(GL_ARRAY_BUFFER, new_buffers[0], data.max_instance_buffer_size, nullptr, GL_STREAM_DRAW, "Batch UBO[0][0]");
  2155. // Light uniform buffer.
  2156. glBindBuffer(GL_UNIFORM_BUFFER, new_buffers[1]);
  2157. GLES3::Utilities::get_singleton()->buffer_allocate_data(GL_UNIFORM_BUFFER, new_buffers[1], sizeof(LightUniform) * data.max_lights_per_render, nullptr, GL_STREAM_DRAW, "2D lights UBO[0]");
  2158. // State buffer.
  2159. glBindBuffer(GL_UNIFORM_BUFFER, new_buffers[2]);
  2160. GLES3::Utilities::get_singleton()->buffer_allocate_data(GL_UNIFORM_BUFFER, new_buffers[2], sizeof(StateBuffer), nullptr, GL_STREAM_DRAW, "2D state UBO[0]");
  2161. DataBuffer db;
  2162. db.instance_buffers.push_back(new_buffers[0]);
  2163. db.light_ubo = new_buffers[1];
  2164. db.state_ubo = new_buffers[2];
  2165. db.last_frame_used = 0;
  2166. db.fence = glFenceSync(GL_SYNC_GPU_COMMANDS_COMPLETE, 0);
  2167. state.canvas_instance_data_buffers[i] = db;
  2168. }
  2169. glBindBuffer(GL_ARRAY_BUFFER, 0);
  2170. glBindBuffer(GL_UNIFORM_BUFFER, 0);
  2171. state.instance_data_array = memnew_arr(InstanceData, data.max_instances_per_buffer);
  2172. state.light_uniforms = memnew_arr(LightUniform, data.max_lights_per_render);
  2173. {
  2174. const uint32_t indices[6] = { 0, 2, 1, 3, 2, 0 };
  2175. glGenVertexArrays(1, &data.indexed_quad_array);
  2176. glBindVertexArray(data.indexed_quad_array);
  2177. glBindBuffer(GL_ARRAY_BUFFER, data.canvas_quad_vertices);
  2178. glGenBuffers(1, &data.indexed_quad_buffer);
  2179. glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, data.indexed_quad_buffer);
  2180. glBufferData(GL_ELEMENT_ARRAY_BUFFER, sizeof(uint32_t) * 6, indices, GL_STATIC_DRAW);
  2181. glBindVertexArray(0);
  2182. }
  2183. String global_defines;
  2184. global_defines += "#define MAX_GLOBAL_SHADER_UNIFORMS 256\n"; // TODO: this is arbitrary for now
  2185. global_defines += "#define MAX_LIGHTS " + itos(data.max_lights_per_render) + "\n";
  2186. GLES3::MaterialStorage::get_singleton()->shaders.canvas_shader.initialize(global_defines, 1);
  2187. data.canvas_shader_default_version = GLES3::MaterialStorage::get_singleton()->shaders.canvas_shader.version_create();
  2188. shadow_render.shader.initialize();
  2189. shadow_render.shader_version = shadow_render.shader.version_create();
  2190. {
  2191. default_canvas_group_shader = material_storage->shader_allocate();
  2192. material_storage->shader_initialize(default_canvas_group_shader);
  2193. material_storage->shader_set_code(default_canvas_group_shader, R"(
  2194. // Default CanvasGroup shader.
  2195. shader_type canvas_item;
  2196. render_mode unshaded;
  2197. uniform sampler2D screen_texture : hint_screen_texture, repeat_disable, filter_nearest;
  2198. void fragment() {
  2199. vec4 c = textureLod(screen_texture, SCREEN_UV, 0.0);
  2200. if (c.a > 0.0001) {
  2201. c.rgb /= c.a;
  2202. }
  2203. COLOR *= c;
  2204. }
  2205. )");
  2206. default_canvas_group_material = material_storage->material_allocate();
  2207. material_storage->material_initialize(default_canvas_group_material);
  2208. material_storage->material_set_shader(default_canvas_group_material, default_canvas_group_shader);
  2209. }
  2210. {
  2211. default_clip_children_shader = material_storage->shader_allocate();
  2212. material_storage->shader_initialize(default_clip_children_shader);
  2213. material_storage->shader_set_code(default_clip_children_shader, R"(
  2214. // Default clip children shader.
  2215. shader_type canvas_item;
  2216. render_mode unshaded;
  2217. uniform sampler2D screen_texture : hint_screen_texture, repeat_disable, filter_nearest;
  2218. void fragment() {
  2219. vec4 c = textureLod(screen_texture, SCREEN_UV, 0.0);
  2220. COLOR.rgb = c.rgb;
  2221. }
  2222. )");
  2223. default_clip_children_material = material_storage->material_allocate();
  2224. material_storage->material_initialize(default_clip_children_material);
  2225. material_storage->material_set_shader(default_clip_children_material, default_clip_children_shader);
  2226. }
  2227. default_canvas_texture = texture_storage->canvas_texture_allocate();
  2228. texture_storage->canvas_texture_initialize(default_canvas_texture);
  2229. state.time = 0.0;
  2230. }
  2231. RasterizerCanvasGLES3::~RasterizerCanvasGLES3() {
  2232. singleton = nullptr;
  2233. GLES3::MaterialStorage *material_storage = GLES3::MaterialStorage::get_singleton();
  2234. material_storage->shaders.canvas_shader.version_free(data.canvas_shader_default_version);
  2235. shadow_render.shader.version_free(shadow_render.shader_version);
  2236. material_storage->material_free(default_canvas_group_material);
  2237. material_storage->shader_free(default_canvas_group_shader);
  2238. material_storage->material_free(default_clip_children_material);
  2239. material_storage->shader_free(default_clip_children_shader);
  2240. singleton = nullptr;
  2241. glDeleteBuffers(1, &data.canvas_quad_vertices);
  2242. glDeleteVertexArrays(1, &data.canvas_quad_array);
  2243. glDeleteBuffers(1, &data.canvas_quad_vertices);
  2244. glDeleteVertexArrays(1, &data.canvas_quad_array);
  2245. GLES3::TextureStorage::get_singleton()->canvas_texture_free(default_canvas_texture);
  2246. memdelete_arr(state.instance_data_array);
  2247. memdelete_arr(state.light_uniforms);
  2248. if (state.shadow_fb != 0) {
  2249. glDeleteFramebuffers(1, &state.shadow_fb);
  2250. GLES3::Utilities::get_singleton()->texture_free_data(state.shadow_texture);
  2251. glDeleteRenderbuffers(1, &state.shadow_depth_buffer);
  2252. state.shadow_fb = 0;
  2253. state.shadow_texture = 0;
  2254. state.shadow_depth_buffer = 0;
  2255. }
  2256. for (uint32_t i = 0; i < state.canvas_instance_data_buffers.size(); i++) {
  2257. for (int j = 0; j < state.canvas_instance_data_buffers[i].instance_buffers.size(); j++) {
  2258. if (state.canvas_instance_data_buffers[i].instance_buffers[j]) {
  2259. GLES3::Utilities::get_singleton()->buffer_free_data(state.canvas_instance_data_buffers[i].instance_buffers[j]);
  2260. }
  2261. }
  2262. if (state.canvas_instance_data_buffers[i].light_ubo) {
  2263. GLES3::Utilities::get_singleton()->buffer_free_data(state.canvas_instance_data_buffers[i].light_ubo);
  2264. }
  2265. if (state.canvas_instance_data_buffers[i].state_ubo) {
  2266. GLES3::Utilities::get_singleton()->buffer_free_data(state.canvas_instance_data_buffers[i].state_ubo);
  2267. }
  2268. }
  2269. }
  2270. #endif // GLES3_ENABLED