rasterizer_canvas_gles3.cpp 109 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789
  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. state.current_tex = RID();
  542. for (uint32_t i = 0; i <= state.current_batch_index; i++) {
  543. //setup clip
  544. if (current_clip != state.canvas_instance_batches[i].clip) {
  545. current_clip = state.canvas_instance_batches[i].clip;
  546. if (current_clip) {
  547. glEnable(GL_SCISSOR_TEST);
  548. 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);
  549. } else {
  550. glDisable(GL_SCISSOR_TEST);
  551. }
  552. }
  553. GLES3::CanvasMaterialData *material_data = state.canvas_instance_batches[i].material_data;
  554. CanvasShaderGLES3::ShaderVariant variant = state.canvas_instance_batches[i].shader_variant;
  555. uint64_t specialization = 0;
  556. specialization |= uint64_t(state.canvas_instance_batches[i].lights_disabled);
  557. specialization |= uint64_t(!GLES3::Config::get_singleton()->float_texture_supported) << 1;
  558. RID shader_version = data.canvas_shader_default_version;
  559. if (material_data) {
  560. if (material_data->shader_data->version.is_valid() && material_data->shader_data->valid) {
  561. // Bind uniform buffer and textures
  562. material_data->bind_uniforms();
  563. shader_version = material_data->shader_data->version;
  564. }
  565. }
  566. bool success = GLES3::MaterialStorage::get_singleton()->shaders.canvas_shader.version_bind_shader(shader_version, variant, specialization);
  567. if (!success) {
  568. continue;
  569. }
  570. GLES3::CanvasShaderData::BlendMode blend_mode = state.canvas_instance_batches[i].blend_mode;
  571. if (last_blend_mode != blend_mode) {
  572. if (last_blend_mode == GLES3::CanvasShaderData::BLEND_MODE_DISABLED) {
  573. // re-enable it
  574. glEnable(GL_BLEND);
  575. } else if (blend_mode == GLES3::CanvasShaderData::BLEND_MODE_DISABLED) {
  576. // disable it
  577. glDisable(GL_BLEND);
  578. }
  579. switch (blend_mode) {
  580. case GLES3::CanvasShaderData::BLEND_MODE_DISABLED: {
  581. // Nothing to do here.
  582. } break;
  583. case GLES3::CanvasShaderData::BLEND_MODE_LCD: {
  584. glBlendEquation(GL_FUNC_ADD);
  585. if (state.transparent_render_target) {
  586. glBlendFuncSeparate(GL_CONSTANT_COLOR, GL_ONE_MINUS_SRC_COLOR, GL_ONE, GL_ONE_MINUS_SRC_ALPHA);
  587. } else {
  588. glBlendFuncSeparate(GL_CONSTANT_COLOR, GL_ONE_MINUS_SRC_COLOR, GL_ZERO, GL_ONE);
  589. }
  590. Color blend_color = state.canvas_instance_batches[state.current_batch_index].blend_color;
  591. glBlendColor(blend_color.r, blend_color.g, blend_color.b, blend_color.a);
  592. } break;
  593. case GLES3::CanvasShaderData::BLEND_MODE_MIX: {
  594. glBlendEquation(GL_FUNC_ADD);
  595. if (state.transparent_render_target) {
  596. glBlendFuncSeparate(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA, GL_ONE, GL_ONE_MINUS_SRC_ALPHA);
  597. } else {
  598. glBlendFuncSeparate(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA, GL_ZERO, GL_ONE);
  599. }
  600. } break;
  601. case GLES3::CanvasShaderData::BLEND_MODE_ADD: {
  602. glBlendEquation(GL_FUNC_ADD);
  603. if (state.transparent_render_target) {
  604. glBlendFuncSeparate(GL_SRC_ALPHA, GL_ONE, GL_SRC_ALPHA, GL_ONE);
  605. } else {
  606. glBlendFuncSeparate(GL_SRC_ALPHA, GL_ONE, GL_ZERO, GL_ONE);
  607. }
  608. } break;
  609. case GLES3::CanvasShaderData::BLEND_MODE_SUB: {
  610. glBlendEquation(GL_FUNC_REVERSE_SUBTRACT);
  611. if (state.transparent_render_target) {
  612. glBlendFuncSeparate(GL_SRC_ALPHA, GL_ONE, GL_SRC_ALPHA, GL_ONE);
  613. } else {
  614. glBlendFuncSeparate(GL_SRC_ALPHA, GL_ONE, GL_ZERO, GL_ONE);
  615. }
  616. } break;
  617. case GLES3::CanvasShaderData::BLEND_MODE_MUL: {
  618. glBlendEquation(GL_FUNC_ADD);
  619. if (state.transparent_render_target) {
  620. glBlendFuncSeparate(GL_DST_COLOR, GL_ZERO, GL_DST_ALPHA, GL_ZERO);
  621. } else {
  622. glBlendFuncSeparate(GL_DST_COLOR, GL_ZERO, GL_ZERO, GL_ONE);
  623. }
  624. } break;
  625. case GLES3::CanvasShaderData::BLEND_MODE_PMALPHA: {
  626. glBlendEquation(GL_FUNC_ADD);
  627. if (state.transparent_render_target) {
  628. glBlendFuncSeparate(GL_ONE, GL_ONE_MINUS_SRC_ALPHA, GL_ONE, GL_ONE_MINUS_SRC_ALPHA);
  629. } else {
  630. glBlendFuncSeparate(GL_ONE, GL_ONE_MINUS_SRC_ALPHA, GL_ZERO, GL_ONE);
  631. }
  632. } break;
  633. }
  634. last_blend_mode = blend_mode;
  635. }
  636. _render_batch(p_lights, i);
  637. }
  638. state.current_batch_index = 0;
  639. state.canvas_instance_batches.clear();
  640. state.last_item_index += index;
  641. }
  642. 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) {
  643. RenderingServer::CanvasItemTextureFilter texture_filter = p_item->texture_filter == RS::CANVAS_ITEM_TEXTURE_FILTER_DEFAULT ? state.default_filter : p_item->texture_filter;
  644. if (texture_filter != state.canvas_instance_batches[state.current_batch_index].filter) {
  645. _new_batch(r_batch_broken);
  646. state.canvas_instance_batches[state.current_batch_index].filter = texture_filter;
  647. }
  648. RenderingServer::CanvasItemTextureRepeat texture_repeat = p_item->texture_repeat == RS::CANVAS_ITEM_TEXTURE_REPEAT_DEFAULT ? state.default_repeat : p_item->texture_repeat;
  649. if (texture_repeat != state.canvas_instance_batches[state.current_batch_index].repeat) {
  650. _new_batch(r_batch_broken);
  651. state.canvas_instance_batches[state.current_batch_index].repeat = texture_repeat;
  652. }
  653. Transform2D base_transform = p_canvas_transform_inverse * p_item->final_transform;
  654. Transform2D draw_transform; // Used by transform command
  655. Color base_color = p_item->final_modulate;
  656. uint32_t base_flags = 0;
  657. Size2 texpixel_size;
  658. bool reclip = false;
  659. bool skipping = false;
  660. // TODO: consider making lights a per-batch property and then baking light operations in the shader for better performance.
  661. uint32_t lights[4] = { 0, 0, 0, 0 };
  662. uint16_t light_count = 0;
  663. {
  664. Light *light = p_lights;
  665. while (light) {
  666. 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)) {
  667. uint32_t light_index = light->render_index_cache;
  668. lights[light_count >> 2] |= light_index << ((light_count & 3) * 8);
  669. light_count++;
  670. if (light_count == data.max_lights_per_item - 1) {
  671. break;
  672. }
  673. }
  674. light = light->next_ptr;
  675. }
  676. base_flags |= light_count << FLAGS_LIGHT_COUNT_SHIFT;
  677. }
  678. bool lights_disabled = light_count == 0 && !state.using_directional_lights;
  679. if (lights_disabled != state.canvas_instance_batches[state.current_batch_index].lights_disabled) {
  680. _new_batch(r_batch_broken);
  681. state.canvas_instance_batches[state.current_batch_index].lights_disabled = lights_disabled;
  682. }
  683. const Item::Command *c = p_item->commands;
  684. while (c) {
  685. if (skipping && c->type != Item::Command::TYPE_ANIMATION_SLICE) {
  686. c = c->next;
  687. continue;
  688. }
  689. if (c->type != Item::Command::TYPE_MESH) {
  690. // For Meshes, this gets updated below.
  691. _update_transform_2d_to_mat2x3(base_transform * draw_transform, state.instance_data_array[r_index].world);
  692. }
  693. // Zero out most fields.
  694. for (int i = 0; i < 4; i++) {
  695. state.instance_data_array[r_index].modulation[i] = 0.0;
  696. state.instance_data_array[r_index].ninepatch_margins[i] = 0.0;
  697. state.instance_data_array[r_index].src_rect[i] = 0.0;
  698. state.instance_data_array[r_index].dst_rect[i] = 0.0;
  699. state.instance_data_array[r_index].lights[i] = uint32_t(0);
  700. }
  701. state.instance_data_array[r_index].color_texture_pixel_size[0] = 0.0;
  702. state.instance_data_array[r_index].color_texture_pixel_size[1] = 0.0;
  703. state.instance_data_array[r_index].pad[0] = 0.0;
  704. state.instance_data_array[r_index].pad[1] = 0.0;
  705. state.instance_data_array[r_index].lights[0] = lights[0];
  706. state.instance_data_array[r_index].lights[1] = lights[1];
  707. state.instance_data_array[r_index].lights[2] = lights[2];
  708. state.instance_data_array[r_index].lights[3] = lights[3];
  709. 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
  710. Color blend_color = base_color;
  711. GLES3::CanvasShaderData::BlendMode blend_mode = p_blend_mode;
  712. if (c->type == Item::Command::TYPE_RECT) {
  713. const Item::CommandRect *rect = static_cast<const Item::CommandRect *>(c);
  714. if (rect->flags & CANVAS_RECT_LCD) {
  715. blend_mode = GLES3::CanvasShaderData::BLEND_MODE_LCD;
  716. blend_color = rect->modulate * base_color;
  717. }
  718. }
  719. 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) {
  720. _new_batch(r_batch_broken);
  721. state.canvas_instance_batches[state.current_batch_index].blend_mode = blend_mode;
  722. state.canvas_instance_batches[state.current_batch_index].blend_color = blend_color;
  723. }
  724. switch (c->type) {
  725. case Item::Command::TYPE_RECT: {
  726. const Item::CommandRect *rect = static_cast<const Item::CommandRect *>(c);
  727. if (rect->flags & CANVAS_RECT_TILE && state.canvas_instance_batches[state.current_batch_index].repeat != RenderingServer::CanvasItemTextureRepeat::CANVAS_ITEM_TEXTURE_REPEAT_ENABLED) {
  728. _new_batch(r_batch_broken);
  729. state.canvas_instance_batches[state.current_batch_index].repeat = RenderingServer::CanvasItemTextureRepeat::CANVAS_ITEM_TEXTURE_REPEAT_ENABLED;
  730. }
  731. 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) {
  732. _new_batch(r_batch_broken);
  733. state.canvas_instance_batches[state.current_batch_index].tex = rect->texture;
  734. state.canvas_instance_batches[state.current_batch_index].command_type = Item::Command::TYPE_RECT;
  735. state.canvas_instance_batches[state.current_batch_index].command = c;
  736. state.canvas_instance_batches[state.current_batch_index].shader_variant = CanvasShaderGLES3::MODE_QUAD;
  737. }
  738. _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);
  739. Rect2 src_rect;
  740. Rect2 dst_rect;
  741. if (rect->texture != RID()) {
  742. src_rect = (rect->flags & CANVAS_RECT_REGION) ? Rect2(rect->source.position * texpixel_size, rect->source.size * texpixel_size) : Rect2(0, 0, 1, 1);
  743. dst_rect = Rect2(rect->rect.position, rect->rect.size);
  744. if (dst_rect.size.width < 0) {
  745. dst_rect.position.x += dst_rect.size.width;
  746. dst_rect.size.width *= -1;
  747. }
  748. if (dst_rect.size.height < 0) {
  749. dst_rect.position.y += dst_rect.size.height;
  750. dst_rect.size.height *= -1;
  751. }
  752. if (rect->flags & CANVAS_RECT_FLIP_H) {
  753. src_rect.size.x *= -1;
  754. state.instance_data_array[r_index].flags |= FLAGS_FLIP_H;
  755. }
  756. if (rect->flags & CANVAS_RECT_FLIP_V) {
  757. src_rect.size.y *= -1;
  758. state.instance_data_array[r_index].flags |= FLAGS_FLIP_V;
  759. }
  760. if (rect->flags & CANVAS_RECT_TRANSPOSE) {
  761. state.instance_data_array[r_index].flags |= FLAGS_TRANSPOSE_RECT;
  762. }
  763. if (rect->flags & CANVAS_RECT_CLIP_UV) {
  764. state.instance_data_array[r_index].flags |= FLAGS_CLIP_RECT_UV;
  765. }
  766. } else {
  767. dst_rect = Rect2(rect->rect.position, rect->rect.size);
  768. if (dst_rect.size.width < 0) {
  769. dst_rect.position.x += dst_rect.size.width;
  770. dst_rect.size.width *= -1;
  771. }
  772. if (dst_rect.size.height < 0) {
  773. dst_rect.position.y += dst_rect.size.height;
  774. dst_rect.size.height *= -1;
  775. }
  776. src_rect = Rect2(0, 0, 1, 1);
  777. }
  778. if (rect->flags & CANVAS_RECT_MSDF) {
  779. state.instance_data_array[r_index].flags |= FLAGS_USE_MSDF;
  780. state.instance_data_array[r_index].msdf[0] = rect->px_range; // Pixel range.
  781. state.instance_data_array[r_index].msdf[1] = rect->outline; // Outline size.
  782. state.instance_data_array[r_index].msdf[2] = 0.f; // Reserved.
  783. state.instance_data_array[r_index].msdf[3] = 0.f; // Reserved.
  784. } else if (rect->flags & CANVAS_RECT_LCD) {
  785. state.instance_data_array[r_index].flags |= FLAGS_USE_LCD;
  786. }
  787. state.instance_data_array[r_index].modulation[0] = rect->modulate.r * base_color.r;
  788. state.instance_data_array[r_index].modulation[1] = rect->modulate.g * base_color.g;
  789. state.instance_data_array[r_index].modulation[2] = rect->modulate.b * base_color.b;
  790. state.instance_data_array[r_index].modulation[3] = rect->modulate.a * base_color.a;
  791. state.instance_data_array[r_index].src_rect[0] = src_rect.position.x;
  792. state.instance_data_array[r_index].src_rect[1] = src_rect.position.y;
  793. state.instance_data_array[r_index].src_rect[2] = src_rect.size.width;
  794. state.instance_data_array[r_index].src_rect[3] = src_rect.size.height;
  795. state.instance_data_array[r_index].dst_rect[0] = dst_rect.position.x;
  796. state.instance_data_array[r_index].dst_rect[1] = dst_rect.position.y;
  797. state.instance_data_array[r_index].dst_rect[2] = dst_rect.size.width;
  798. state.instance_data_array[r_index].dst_rect[3] = dst_rect.size.height;
  799. _add_to_batch(r_index, r_batch_broken);
  800. } break;
  801. case Item::Command::TYPE_NINEPATCH: {
  802. const Item::CommandNinePatch *np = static_cast<const Item::CommandNinePatch *>(c);
  803. 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) {
  804. _new_batch(r_batch_broken);
  805. state.canvas_instance_batches[state.current_batch_index].tex = np->texture;
  806. state.canvas_instance_batches[state.current_batch_index].command_type = Item::Command::TYPE_NINEPATCH;
  807. state.canvas_instance_batches[state.current_batch_index].command = c;
  808. state.canvas_instance_batches[state.current_batch_index].shader_variant = CanvasShaderGLES3::MODE_NINEPATCH;
  809. }
  810. _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);
  811. Rect2 src_rect;
  812. Rect2 dst_rect(np->rect.position.x, np->rect.position.y, np->rect.size.x, np->rect.size.y);
  813. if (np->texture == RID()) {
  814. texpixel_size = Size2(1, 1);
  815. src_rect = Rect2(0, 0, 1, 1);
  816. } else {
  817. if (np->source != Rect2()) {
  818. 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);
  819. state.instance_data_array[r_index].color_texture_pixel_size[0] = 1.0 / np->source.size.width;
  820. state.instance_data_array[r_index].color_texture_pixel_size[1] = 1.0 / np->source.size.height;
  821. } else {
  822. src_rect = Rect2(0, 0, 1, 1);
  823. }
  824. }
  825. state.instance_data_array[r_index].modulation[0] = np->color.r * base_color.r;
  826. state.instance_data_array[r_index].modulation[1] = np->color.g * base_color.g;
  827. state.instance_data_array[r_index].modulation[2] = np->color.b * base_color.b;
  828. state.instance_data_array[r_index].modulation[3] = np->color.a * base_color.a;
  829. state.instance_data_array[r_index].src_rect[0] = src_rect.position.x;
  830. state.instance_data_array[r_index].src_rect[1] = src_rect.position.y;
  831. state.instance_data_array[r_index].src_rect[2] = src_rect.size.width;
  832. state.instance_data_array[r_index].src_rect[3] = src_rect.size.height;
  833. state.instance_data_array[r_index].dst_rect[0] = dst_rect.position.x;
  834. state.instance_data_array[r_index].dst_rect[1] = dst_rect.position.y;
  835. state.instance_data_array[r_index].dst_rect[2] = dst_rect.size.width;
  836. state.instance_data_array[r_index].dst_rect[3] = dst_rect.size.height;
  837. state.instance_data_array[r_index].flags |= int(np->axis_x) << FLAGS_NINEPATCH_H_MODE_SHIFT;
  838. state.instance_data_array[r_index].flags |= int(np->axis_y) << FLAGS_NINEPATCH_V_MODE_SHIFT;
  839. if (np->draw_center) {
  840. state.instance_data_array[r_index].flags |= FLAGS_NINEPACH_DRAW_CENTER;
  841. }
  842. state.instance_data_array[r_index].ninepatch_margins[0] = np->margin[SIDE_LEFT];
  843. state.instance_data_array[r_index].ninepatch_margins[1] = np->margin[SIDE_TOP];
  844. state.instance_data_array[r_index].ninepatch_margins[2] = np->margin[SIDE_RIGHT];
  845. state.instance_data_array[r_index].ninepatch_margins[3] = np->margin[SIDE_BOTTOM];
  846. _add_to_batch(r_index, r_batch_broken);
  847. // Restore if overridden.
  848. state.instance_data_array[r_index].color_texture_pixel_size[0] = texpixel_size.x;
  849. state.instance_data_array[r_index].color_texture_pixel_size[1] = texpixel_size.y;
  850. } break;
  851. case Item::Command::TYPE_POLYGON: {
  852. const Item::CommandPolygon *polygon = static_cast<const Item::CommandPolygon *>(c);
  853. // Polygon's can't be batched, so always create a new batch
  854. _new_batch(r_batch_broken);
  855. state.canvas_instance_batches[state.current_batch_index].tex = polygon->texture;
  856. state.canvas_instance_batches[state.current_batch_index].command_type = Item::Command::TYPE_POLYGON;
  857. state.canvas_instance_batches[state.current_batch_index].command = c;
  858. state.canvas_instance_batches[state.current_batch_index].shader_variant = CanvasShaderGLES3::MODE_ATTRIBUTES;
  859. _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);
  860. state.instance_data_array[r_index].modulation[0] = base_color.r;
  861. state.instance_data_array[r_index].modulation[1] = base_color.g;
  862. state.instance_data_array[r_index].modulation[2] = base_color.b;
  863. state.instance_data_array[r_index].modulation[3] = base_color.a;
  864. for (int j = 0; j < 4; j++) {
  865. state.instance_data_array[r_index].src_rect[j] = 0;
  866. state.instance_data_array[r_index].dst_rect[j] = 0;
  867. state.instance_data_array[r_index].ninepatch_margins[j] = 0;
  868. }
  869. _add_to_batch(r_index, r_batch_broken);
  870. } break;
  871. case Item::Command::TYPE_PRIMITIVE: {
  872. const Item::CommandPrimitive *primitive = static_cast<const Item::CommandPrimitive *>(c);
  873. 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) {
  874. _new_batch(r_batch_broken);
  875. state.canvas_instance_batches[state.current_batch_index].tex = primitive->texture;
  876. state.canvas_instance_batches[state.current_batch_index].primitive_points = primitive->point_count;
  877. state.canvas_instance_batches[state.current_batch_index].command_type = Item::Command::TYPE_PRIMITIVE;
  878. state.canvas_instance_batches[state.current_batch_index].command = c;
  879. state.canvas_instance_batches[state.current_batch_index].shader_variant = CanvasShaderGLES3::MODE_PRIMITIVE;
  880. }
  881. _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);
  882. for (uint32_t j = 0; j < MIN(3u, primitive->point_count); j++) {
  883. state.instance_data_array[r_index].points[j * 2 + 0] = primitive->points[j].x;
  884. state.instance_data_array[r_index].points[j * 2 + 1] = primitive->points[j].y;
  885. state.instance_data_array[r_index].uvs[j * 2 + 0] = primitive->uvs[j].x;
  886. state.instance_data_array[r_index].uvs[j * 2 + 1] = primitive->uvs[j].y;
  887. Color col = primitive->colors[j] * base_color;
  888. 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);
  889. 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);
  890. }
  891. _add_to_batch(r_index, r_batch_broken);
  892. if (primitive->point_count == 4) {
  893. // Reset base data.
  894. _update_transform_2d_to_mat2x3(base_transform * draw_transform, state.instance_data_array[r_index].world);
  895. _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);
  896. for (uint32_t j = 0; j < 3; j++) {
  897. int offset = j == 0 ? 0 : 1;
  898. // Second triangle in the quad. Uses vertices 0, 2, 3.
  899. state.instance_data_array[r_index].points[j * 2 + 0] = primitive->points[j + offset].x;
  900. state.instance_data_array[r_index].points[j * 2 + 1] = primitive->points[j + offset].y;
  901. state.instance_data_array[r_index].uvs[j * 2 + 0] = primitive->uvs[j + offset].x;
  902. state.instance_data_array[r_index].uvs[j * 2 + 1] = primitive->uvs[j + offset].y;
  903. Color col = primitive->colors[j + offset] * base_color;
  904. 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);
  905. 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);
  906. }
  907. _add_to_batch(r_index, r_batch_broken);
  908. }
  909. } break;
  910. case Item::Command::TYPE_MESH:
  911. case Item::Command::TYPE_MULTIMESH:
  912. case Item::Command::TYPE_PARTICLES: {
  913. // Mesh's can't be batched, so always create a new batch
  914. _new_batch(r_batch_broken);
  915. Color modulate(1, 1, 1, 1);
  916. state.canvas_instance_batches[state.current_batch_index].shader_variant = CanvasShaderGLES3::MODE_ATTRIBUTES;
  917. if (c->type == Item::Command::TYPE_MESH) {
  918. const Item::CommandMesh *m = static_cast<const Item::CommandMesh *>(c);
  919. state.canvas_instance_batches[state.current_batch_index].tex = m->texture;
  920. _update_transform_2d_to_mat2x3(base_transform * draw_transform * m->transform, state.instance_data_array[r_index].world);
  921. modulate = m->modulate;
  922. } else if (c->type == Item::Command::TYPE_MULTIMESH) {
  923. const Item::CommandMultiMesh *mm = static_cast<const Item::CommandMultiMesh *>(c);
  924. state.canvas_instance_batches[state.current_batch_index].tex = mm->texture;
  925. state.canvas_instance_batches[state.current_batch_index].shader_variant = CanvasShaderGLES3::MODE_INSTANCED;
  926. if (GLES3::MeshStorage::get_singleton()->multimesh_uses_colors(mm->multimesh)) {
  927. state.instance_data_array[r_index].flags |= FLAGS_INSTANCING_HAS_COLORS;
  928. }
  929. if (GLES3::MeshStorage::get_singleton()->multimesh_uses_custom_data(mm->multimesh)) {
  930. state.instance_data_array[r_index].flags |= FLAGS_INSTANCING_HAS_CUSTOM_DATA;
  931. }
  932. } else if (c->type == Item::Command::TYPE_PARTICLES) {
  933. GLES3::ParticlesStorage *particles_storage = GLES3::ParticlesStorage::get_singleton();
  934. GLES3::TextureStorage *texture_storage = GLES3::TextureStorage::get_singleton();
  935. const Item::CommandParticles *pt = static_cast<const Item::CommandParticles *>(c);
  936. RID particles = pt->particles;
  937. state.canvas_instance_batches[state.current_batch_index].tex = pt->texture;
  938. state.canvas_instance_batches[state.current_batch_index].shader_variant = CanvasShaderGLES3::MODE_INSTANCED;
  939. state.instance_data_array[r_index].flags |= FLAGS_INSTANCING_HAS_COLORS;
  940. state.instance_data_array[r_index].flags |= FLAGS_INSTANCING_HAS_CUSTOM_DATA;
  941. if (particles_storage->particles_has_collision(particles) && texture_storage->render_target_is_sdf_enabled(p_render_target)) {
  942. // Pass collision information.
  943. Transform2D xform = p_item->final_transform;
  944. GLuint sdf_texture = texture_storage->render_target_get_sdf_texture(p_render_target);
  945. Rect2 to_screen;
  946. {
  947. Rect2 sdf_rect = texture_storage->render_target_get_sdf_rect(p_render_target);
  948. to_screen.size = Vector2(1.0 / sdf_rect.size.width, 1.0 / sdf_rect.size.height);
  949. to_screen.position = -sdf_rect.position * to_screen.size;
  950. }
  951. particles_storage->particles_set_canvas_sdf_collision(pt->particles, true, xform, to_screen, sdf_texture);
  952. } else {
  953. particles_storage->particles_set_canvas_sdf_collision(pt->particles, false, Transform2D(), Rect2(), 0);
  954. }
  955. r_sdf_used |= particles_storage->particles_has_collision(particles);
  956. }
  957. state.canvas_instance_batches[state.current_batch_index].command = c;
  958. state.canvas_instance_batches[state.current_batch_index].command_type = c->type;
  959. _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);
  960. state.instance_data_array[r_index].modulation[0] = base_color.r * modulate.r;
  961. state.instance_data_array[r_index].modulation[1] = base_color.g * modulate.g;
  962. state.instance_data_array[r_index].modulation[2] = base_color.b * modulate.b;
  963. state.instance_data_array[r_index].modulation[3] = base_color.a * modulate.a;
  964. for (int j = 0; j < 4; j++) {
  965. state.instance_data_array[r_index].src_rect[j] = 0;
  966. state.instance_data_array[r_index].dst_rect[j] = 0;
  967. state.instance_data_array[r_index].ninepatch_margins[j] = 0;
  968. }
  969. _add_to_batch(r_index, r_batch_broken);
  970. } break;
  971. case Item::Command::TYPE_TRANSFORM: {
  972. const Item::CommandTransform *transform = static_cast<const Item::CommandTransform *>(c);
  973. draw_transform = transform->xform;
  974. } break;
  975. case Item::Command::TYPE_CLIP_IGNORE: {
  976. const Item::CommandClipIgnore *ci = static_cast<const Item::CommandClipIgnore *>(c);
  977. if (current_clip) {
  978. if (ci->ignore != reclip) {
  979. _new_batch(r_batch_broken);
  980. if (ci->ignore) {
  981. state.canvas_instance_batches[state.current_batch_index].clip = nullptr;
  982. reclip = true;
  983. } else {
  984. state.canvas_instance_batches[state.current_batch_index].clip = current_clip;
  985. reclip = false;
  986. }
  987. }
  988. }
  989. } break;
  990. case Item::Command::TYPE_ANIMATION_SLICE: {
  991. const Item::CommandAnimationSlice *as = static_cast<const Item::CommandAnimationSlice *>(c);
  992. double current_time = RSG::rasterizer->get_total_time();
  993. double local_time = Math::fposmod(current_time - as->offset, as->animation_length);
  994. skipping = !(local_time >= as->slice_begin && local_time < as->slice_end);
  995. RenderingServerDefault::redraw_request(); // animation visible means redraw request
  996. } break;
  997. }
  998. c = c->next;
  999. r_batch_broken = false;
  1000. }
  1001. if (current_clip && reclip) {
  1002. //will make it re-enable clipping if needed afterwards
  1003. current_clip = nullptr;
  1004. }
  1005. }
  1006. void RasterizerCanvasGLES3::_render_batch(Light *p_lights, uint32_t p_index) {
  1007. ERR_FAIL_COND(!state.canvas_instance_batches[state.current_batch_index].command);
  1008. // Used by Polygon and Mesh.
  1009. static const GLenum prim[5] = { GL_POINTS, GL_LINES, GL_LINE_STRIP, GL_TRIANGLES, GL_TRIANGLE_STRIP };
  1010. _bind_canvas_texture(state.canvas_instance_batches[p_index].tex, state.canvas_instance_batches[p_index].filter, state.canvas_instance_batches[p_index].repeat);
  1011. switch (state.canvas_instance_batches[p_index].command_type) {
  1012. case Item::Command::TYPE_RECT:
  1013. case Item::Command::TYPE_NINEPATCH: {
  1014. glBindVertexArray(data.indexed_quad_array);
  1015. 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]);
  1016. uint32_t range_start = state.canvas_instance_batches[p_index].start * sizeof(InstanceData);
  1017. _enable_attributes(range_start, false);
  1018. glDrawElementsInstanced(GL_TRIANGLES, 6, GL_UNSIGNED_INT, 0, state.canvas_instance_batches[p_index].instance_count);
  1019. glBindVertexArray(0);
  1020. } break;
  1021. case Item::Command::TYPE_POLYGON: {
  1022. const Item::CommandPolygon *polygon = static_cast<const Item::CommandPolygon *>(state.canvas_instance_batches[p_index].command);
  1023. PolygonBuffers *pb = polygon_buffers.polygons.getptr(polygon->polygon.polygon_id);
  1024. ERR_FAIL_COND(!pb);
  1025. glBindVertexArray(pb->vertex_array);
  1026. 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]);
  1027. uint32_t range_start = state.canvas_instance_batches[p_index].start * sizeof(InstanceData);
  1028. _enable_attributes(range_start, false);
  1029. if (pb->color_disabled && pb->color != Color(1.0, 1.0, 1.0, 1.0)) {
  1030. glVertexAttrib4f(RS::ARRAY_COLOR, pb->color.r, pb->color.g, pb->color.b, pb->color.a);
  1031. }
  1032. if (pb->index_buffer != 0) {
  1033. glDrawElementsInstanced(prim[polygon->primitive], pb->count, GL_UNSIGNED_INT, nullptr, 1);
  1034. } else {
  1035. glDrawArraysInstanced(prim[polygon->primitive], 0, pb->count, 1);
  1036. }
  1037. glBindVertexArray(0);
  1038. if (pb->color_disabled && pb->color != Color(1.0, 1.0, 1.0, 1.0)) {
  1039. // Reset so this doesn't pollute other draw calls.
  1040. glVertexAttrib4f(RS::ARRAY_COLOR, 1.0, 1.0, 1.0, 1.0);
  1041. }
  1042. } break;
  1043. case Item::Command::TYPE_PRIMITIVE: {
  1044. glBindVertexArray(data.canvas_quad_array);
  1045. 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]);
  1046. uint32_t range_start = state.canvas_instance_batches[p_index].start * sizeof(InstanceData);
  1047. _enable_attributes(range_start, true);
  1048. const GLenum primitive[5] = { GL_POINTS, GL_POINTS, GL_LINES, GL_TRIANGLES, GL_TRIANGLES };
  1049. int instance_count = state.canvas_instance_batches[p_index].instance_count;
  1050. ERR_FAIL_COND(instance_count <= 0);
  1051. if (instance_count >= 1) {
  1052. glDrawArraysInstanced(primitive[state.canvas_instance_batches[p_index].primitive_points], 0, state.canvas_instance_batches[p_index].primitive_points, instance_count);
  1053. }
  1054. } break;
  1055. case Item::Command::TYPE_MESH:
  1056. case Item::Command::TYPE_MULTIMESH:
  1057. case Item::Command::TYPE_PARTICLES: {
  1058. GLES3::MeshStorage *mesh_storage = GLES3::MeshStorage::get_singleton();
  1059. GLES3::ParticlesStorage *particles_storage = GLES3::ParticlesStorage::get_singleton();
  1060. RID mesh;
  1061. RID mesh_instance;
  1062. uint32_t instance_count = 1;
  1063. GLuint instance_buffer = 0;
  1064. uint32_t instance_stride = 0;
  1065. uint32_t instance_color_offset = 0;
  1066. bool instance_uses_color = false;
  1067. bool instance_uses_custom_data = false;
  1068. bool use_instancing = false;
  1069. if (state.canvas_instance_batches[p_index].command_type == Item::Command::TYPE_MESH) {
  1070. const Item::CommandMesh *m = static_cast<const Item::CommandMesh *>(state.canvas_instance_batches[p_index].command);
  1071. mesh = m->mesh;
  1072. mesh_instance = m->mesh_instance;
  1073. } else if (state.canvas_instance_batches[p_index].command_type == Item::Command::TYPE_MULTIMESH) {
  1074. const Item::CommandMultiMesh *mm = static_cast<const Item::CommandMultiMesh *>(state.canvas_instance_batches[p_index].command);
  1075. RID multimesh = mm->multimesh;
  1076. mesh = mesh_storage->multimesh_get_mesh(multimesh);
  1077. if (mesh_storage->multimesh_get_transform_format(multimesh) != RS::MULTIMESH_TRANSFORM_2D) {
  1078. break;
  1079. }
  1080. instance_count = mesh_storage->multimesh_get_instances_to_draw(multimesh);
  1081. if (instance_count == 0) {
  1082. break;
  1083. }
  1084. instance_buffer = mesh_storage->multimesh_get_gl_buffer(multimesh);
  1085. instance_stride = mesh_storage->multimesh_get_stride(multimesh);
  1086. instance_color_offset = mesh_storage->multimesh_get_color_offset(multimesh);
  1087. instance_uses_color = mesh_storage->multimesh_uses_colors(multimesh);
  1088. instance_uses_custom_data = mesh_storage->multimesh_uses_custom_data(multimesh);
  1089. use_instancing = true;
  1090. } else if (state.canvas_instance_batches[p_index].command_type == Item::Command::TYPE_PARTICLES) {
  1091. const Item::CommandParticles *pt = static_cast<const Item::CommandParticles *>(state.canvas_instance_batches[p_index].command);
  1092. RID particles = pt->particles;
  1093. mesh = particles_storage->particles_get_draw_pass_mesh(particles, 0);
  1094. ERR_BREAK(particles_storage->particles_get_mode(particles) != RS::PARTICLES_MODE_2D);
  1095. particles_storage->particles_request_process(particles);
  1096. if (particles_storage->particles_is_inactive(particles)) {
  1097. break;
  1098. }
  1099. RenderingServerDefault::redraw_request(); // Active particles means redraw request.
  1100. int dpc = particles_storage->particles_get_draw_passes(particles);
  1101. if (dpc == 0) {
  1102. break; // Nothing to draw.
  1103. }
  1104. instance_count = particles_storage->particles_get_amount(particles);
  1105. instance_buffer = particles_storage->particles_get_gl_buffer(particles);
  1106. instance_stride = 12; // 8 bytes for instance transform and 4 bytes for packed color and custom.
  1107. instance_color_offset = 8; // 8 bytes for instance transform.
  1108. instance_uses_color = true;
  1109. instance_uses_custom_data = true;
  1110. use_instancing = true;
  1111. }
  1112. ERR_FAIL_COND(mesh.is_null());
  1113. uint32_t surf_count = mesh_storage->mesh_get_surface_count(mesh);
  1114. for (uint32_t j = 0; j < surf_count; j++) {
  1115. void *surface = mesh_storage->mesh_get_surface(mesh, j);
  1116. RS::PrimitiveType primitive = mesh_storage->mesh_surface_get_primitive(surface);
  1117. ERR_CONTINUE(primitive < 0 || primitive >= RS::PRIMITIVE_MAX);
  1118. GLuint vertex_array_gl = 0;
  1119. GLuint index_array_gl = 0;
  1120. uint32_t input_mask = 0; // 2D meshes always use the same vertex format
  1121. if (mesh_instance.is_valid()) {
  1122. mesh_storage->mesh_instance_surface_get_vertex_arrays_and_format(mesh_instance, j, input_mask, vertex_array_gl);
  1123. } else {
  1124. mesh_storage->mesh_surface_get_vertex_arrays_and_format(surface, input_mask, vertex_array_gl);
  1125. }
  1126. index_array_gl = mesh_storage->mesh_surface_get_index_buffer(surface, 0);
  1127. bool use_index_buffer = false;
  1128. glBindVertexArray(vertex_array_gl);
  1129. 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]);
  1130. uint32_t range_start = state.canvas_instance_batches[p_index].start * sizeof(InstanceData);
  1131. _enable_attributes(range_start, false, instance_count);
  1132. if (index_array_gl != 0) {
  1133. glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, index_array_gl);
  1134. use_index_buffer = true;
  1135. }
  1136. if (use_instancing) {
  1137. if (instance_buffer == 0) {
  1138. break;
  1139. }
  1140. // Bind instance buffers.
  1141. glBindBuffer(GL_ARRAY_BUFFER, instance_buffer);
  1142. glEnableVertexAttribArray(1);
  1143. glVertexAttribPointer(1, 4, GL_FLOAT, GL_FALSE, instance_stride * sizeof(float), CAST_INT_TO_UCHAR_PTR(0));
  1144. glVertexAttribDivisor(1, 1);
  1145. glEnableVertexAttribArray(2);
  1146. glVertexAttribPointer(2, 4, GL_FLOAT, GL_FALSE, instance_stride * sizeof(float), CAST_INT_TO_UCHAR_PTR(4 * 4));
  1147. glVertexAttribDivisor(2, 1);
  1148. if (instance_uses_color || instance_uses_custom_data) {
  1149. glEnableVertexAttribArray(5);
  1150. glVertexAttribIPointer(5, 4, GL_UNSIGNED_INT, instance_stride * sizeof(float), CAST_INT_TO_UCHAR_PTR(instance_color_offset * sizeof(float)));
  1151. glVertexAttribDivisor(5, 1);
  1152. }
  1153. }
  1154. GLenum primitive_gl = prim[int(primitive)];
  1155. if (use_index_buffer) {
  1156. glDrawElementsInstanced(primitive_gl, mesh_storage->mesh_surface_get_vertices_drawn_count(surface), mesh_storage->mesh_surface_get_index_type(surface), 0, instance_count);
  1157. } else {
  1158. glDrawArraysInstanced(primitive_gl, 0, mesh_storage->mesh_surface_get_vertices_drawn_count(surface), instance_count);
  1159. }
  1160. glBindBuffer(GL_ARRAY_BUFFER, 0);
  1161. glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0);
  1162. if (use_instancing) {
  1163. glDisableVertexAttribArray(5);
  1164. glDisableVertexAttribArray(6);
  1165. glDisableVertexAttribArray(7);
  1166. glDisableVertexAttribArray(8);
  1167. }
  1168. }
  1169. } break;
  1170. case Item::Command::TYPE_TRANSFORM:
  1171. case Item::Command::TYPE_CLIP_IGNORE:
  1172. case Item::Command::TYPE_ANIMATION_SLICE: {
  1173. // Can ignore these as they only impact batch creation.
  1174. } break;
  1175. }
  1176. }
  1177. void RasterizerCanvasGLES3::_add_to_batch(uint32_t &r_index, bool &r_batch_broken) {
  1178. state.canvas_instance_batches[state.current_batch_index].instance_count++;
  1179. r_index++;
  1180. if (r_index >= data.max_instances_per_buffer) {
  1181. // Copy over all data needed for rendering right away
  1182. // then go back to recording item commands.
  1183. glBindBuffer(GL_ARRAY_BUFFER, state.canvas_instance_data_buffers[state.current_data_buffer_index].instance_buffers[state.current_instance_buffer_index]);
  1184. #ifdef WEB_ENABLED
  1185. glBufferSubData(GL_ARRAY_BUFFER, state.last_item_index * sizeof(InstanceData), sizeof(InstanceData) * r_index, state.instance_data_array);
  1186. #else
  1187. // On Desktop and mobile we map the memory without synchronizing for maximum speed.
  1188. void *buffer = glMapBufferRange(GL_ARRAY_BUFFER, state.last_item_index * sizeof(InstanceData), r_index * sizeof(InstanceData), GL_MAP_WRITE_BIT | GL_MAP_UNSYNCHRONIZED_BIT);
  1189. memcpy(buffer, state.instance_data_array, r_index * sizeof(InstanceData));
  1190. glUnmapBuffer(GL_ARRAY_BUFFER);
  1191. #endif
  1192. _allocate_instance_buffer();
  1193. r_index = 0;
  1194. state.last_item_index = 0;
  1195. r_batch_broken = false; // Force a new batch to be created
  1196. _new_batch(r_batch_broken);
  1197. state.canvas_instance_batches[state.current_batch_index].start = 0;
  1198. }
  1199. }
  1200. void RasterizerCanvasGLES3::_new_batch(bool &r_batch_broken) {
  1201. if (state.canvas_instance_batches.size() == 0) {
  1202. state.canvas_instance_batches.push_back(Batch());
  1203. return;
  1204. }
  1205. if (r_batch_broken || state.canvas_instance_batches[state.current_batch_index].instance_count == 0) {
  1206. return;
  1207. }
  1208. r_batch_broken = true;
  1209. // Copy the properties of the current batch, we will manually update the things that changed.
  1210. Batch new_batch = state.canvas_instance_batches[state.current_batch_index];
  1211. new_batch.instance_count = 0;
  1212. new_batch.start = state.canvas_instance_batches[state.current_batch_index].start + state.canvas_instance_batches[state.current_batch_index].instance_count;
  1213. new_batch.instance_buffer_index = state.current_instance_buffer_index;
  1214. state.current_batch_index++;
  1215. state.canvas_instance_batches.push_back(new_batch);
  1216. }
  1217. void RasterizerCanvasGLES3::_enable_attributes(uint32_t p_start, bool p_primitive, uint32_t p_rate) {
  1218. uint32_t split = p_primitive ? 11 : 12;
  1219. for (uint32_t i = 6; i < split; i++) {
  1220. glEnableVertexAttribArray(i);
  1221. glVertexAttribPointer(i, 4, GL_FLOAT, GL_FALSE, sizeof(InstanceData), CAST_INT_TO_UCHAR_PTR(p_start + (i - 6) * 4 * sizeof(float)));
  1222. glVertexAttribDivisor(i, p_rate);
  1223. }
  1224. for (uint32_t i = split; i <= 13; i++) {
  1225. glEnableVertexAttribArray(i);
  1226. glVertexAttribIPointer(i, 4, GL_UNSIGNED_INT, sizeof(InstanceData), CAST_INT_TO_UCHAR_PTR(p_start + (i - 6) * 4 * sizeof(float)));
  1227. glVertexAttribDivisor(i, p_rate);
  1228. }
  1229. }
  1230. RID RasterizerCanvasGLES3::light_create() {
  1231. CanvasLight canvas_light;
  1232. return canvas_light_owner.make_rid(canvas_light);
  1233. }
  1234. void RasterizerCanvasGLES3::light_set_texture(RID p_rid, RID p_texture) {
  1235. GLES3::TextureStorage *texture_storage = GLES3::TextureStorage::get_singleton();
  1236. CanvasLight *cl = canvas_light_owner.get_or_null(p_rid);
  1237. ERR_FAIL_COND(!cl);
  1238. if (cl->texture == p_texture) {
  1239. return;
  1240. }
  1241. ERR_FAIL_COND(p_texture.is_valid() && !texture_storage->owns_texture(p_texture));
  1242. if (cl->texture.is_valid()) {
  1243. texture_storage->texture_remove_from_texture_atlas(cl->texture);
  1244. }
  1245. cl->texture = p_texture;
  1246. if (cl->texture.is_valid()) {
  1247. texture_storage->texture_add_to_texture_atlas(cl->texture);
  1248. }
  1249. }
  1250. void RasterizerCanvasGLES3::light_set_use_shadow(RID p_rid, bool p_enable) {
  1251. CanvasLight *cl = canvas_light_owner.get_or_null(p_rid);
  1252. ERR_FAIL_COND(!cl);
  1253. cl->shadow.enabled = p_enable;
  1254. }
  1255. 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) {
  1256. GLES3::Config *config = GLES3::Config::get_singleton();
  1257. CanvasLight *cl = canvas_light_owner.get_or_null(p_rid);
  1258. ERR_FAIL_COND(!cl->shadow.enabled);
  1259. _update_shadow_atlas();
  1260. cl->shadow.z_far = p_far;
  1261. cl->shadow.y_offset = float(p_shadow_index * 2 + 1) / float(data.max_lights_per_render * 2);
  1262. glBindFramebuffer(GL_FRAMEBUFFER, state.shadow_fb);
  1263. glViewport(0, p_shadow_index * 2, state.shadow_texture_size, 2);
  1264. glDepthMask(GL_TRUE);
  1265. glEnable(GL_DEPTH_TEST);
  1266. glDepthFunc(GL_LESS);
  1267. glDisable(GL_BLEND);
  1268. glEnable(GL_SCISSOR_TEST);
  1269. glScissor(0, p_shadow_index * 2, state.shadow_texture_size, 2);
  1270. glClearColor(p_far, p_far, p_far, 1.0);
  1271. glClearDepth(1.0);
  1272. glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
  1273. glCullFace(GL_BACK);
  1274. glDisable(GL_CULL_FACE);
  1275. RS::CanvasOccluderPolygonCullMode cull_mode = RS::CANVAS_OCCLUDER_POLYGON_CULL_DISABLED;
  1276. CanvasOcclusionShaderGLES3::ShaderVariant variant = config->float_texture_supported ? CanvasOcclusionShaderGLES3::MODE_SHADOW : CanvasOcclusionShaderGLES3::MODE_SHADOW_RGBA;
  1277. bool success = shadow_render.shader.version_bind_shader(shadow_render.shader_version, variant);
  1278. if (!success) {
  1279. return;
  1280. }
  1281. for (int i = 0; i < 4; i++) {
  1282. glViewport((state.shadow_texture_size / 4) * i, p_shadow_index * 2, (state.shadow_texture_size / 4), 2);
  1283. Projection projection;
  1284. {
  1285. real_t fov = 90;
  1286. real_t nearp = p_near;
  1287. real_t farp = p_far;
  1288. real_t aspect = 1.0;
  1289. real_t ymax = nearp * Math::tan(Math::deg_to_rad(fov * 0.5));
  1290. real_t ymin = -ymax;
  1291. real_t xmin = ymin * aspect;
  1292. real_t xmax = ymax * aspect;
  1293. projection.set_frustum(xmin, xmax, ymin, ymax, nearp, farp);
  1294. }
  1295. Vector3 cam_target = Basis::from_euler(Vector3(0, 0, Math_TAU * ((i + 3) / 4.0))).xform(Vector3(0, 1, 0));
  1296. projection = projection * Projection(Transform3D().looking_at(cam_target, Vector3(0, 0, -1)).affine_inverse());
  1297. shadow_render.shader.version_set_uniform(CanvasOcclusionShaderGLES3::PROJECTION, projection, shadow_render.shader_version, variant);
  1298. static const Vector2 directions[4] = { Vector2(1, 0), Vector2(0, 1), Vector2(-1, 0), Vector2(0, -1) };
  1299. shadow_render.shader.version_set_uniform(CanvasOcclusionShaderGLES3::DIRECTION, directions[i].x, directions[i].y, shadow_render.shader_version, variant);
  1300. shadow_render.shader.version_set_uniform(CanvasOcclusionShaderGLES3::Z_FAR, p_far, shadow_render.shader_version, variant);
  1301. LightOccluderInstance *instance = p_occluders;
  1302. while (instance) {
  1303. OccluderPolygon *co = occluder_polygon_owner.get_or_null(instance->occluder);
  1304. if (!co || co->vertex_array == 0 || !(p_light_mask & instance->light_mask)) {
  1305. instance = instance->next;
  1306. continue;
  1307. }
  1308. Transform2D modelview = p_light_xform * instance->xform_cache;
  1309. 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);
  1310. 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);
  1311. if (co->cull_mode != cull_mode) {
  1312. if (co->cull_mode == RS::CANVAS_OCCLUDER_POLYGON_CULL_DISABLED) {
  1313. glDisable(GL_CULL_FACE);
  1314. } else {
  1315. if (cull_mode == RS::CANVAS_OCCLUDER_POLYGON_CULL_DISABLED) {
  1316. // Last time was disabled, so enable and set proper face.
  1317. glEnable(GL_CULL_FACE);
  1318. }
  1319. glCullFace(co->cull_mode == RS::CANVAS_OCCLUDER_POLYGON_CULL_CLOCKWISE ? GL_FRONT : GL_BACK);
  1320. }
  1321. cull_mode = co->cull_mode;
  1322. }
  1323. glBindVertexArray(co->vertex_array);
  1324. glDrawElements(GL_TRIANGLES, 3 * co->line_point_count, GL_UNSIGNED_SHORT, 0);
  1325. instance = instance->next;
  1326. }
  1327. }
  1328. glBindVertexArray(0);
  1329. glBindFramebuffer(GL_FRAMEBUFFER, 0);
  1330. glDepthMask(GL_FALSE);
  1331. glDisable(GL_DEPTH_TEST);
  1332. glDisable(GL_SCISSOR_TEST);
  1333. }
  1334. 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) {
  1335. GLES3::Config *config = GLES3::Config::get_singleton();
  1336. CanvasLight *cl = canvas_light_owner.get_or_null(p_rid);
  1337. ERR_FAIL_COND(!cl->shadow.enabled);
  1338. _update_shadow_atlas();
  1339. Vector2 light_dir = p_light_xform.columns[1].normalized();
  1340. Vector2 center = p_clip_rect.get_center();
  1341. float to_edge_distance = ABS(light_dir.dot(p_clip_rect.get_support(light_dir)) - light_dir.dot(center));
  1342. Vector2 from_pos = center - light_dir * (to_edge_distance + p_cull_distance);
  1343. float distance = to_edge_distance * 2.0 + p_cull_distance;
  1344. float half_size = p_clip_rect.size.length() * 0.5; //shadow length, must keep this no matter the angle
  1345. cl->shadow.z_far = distance;
  1346. cl->shadow.y_offset = float(p_shadow_index * 2 + 1) / float(data.max_lights_per_render * 2);
  1347. Transform2D to_light_xform;
  1348. to_light_xform[2] = from_pos;
  1349. to_light_xform[1] = light_dir;
  1350. to_light_xform[0] = -light_dir.orthogonal();
  1351. to_light_xform.invert();
  1352. glBindFramebuffer(GL_FRAMEBUFFER, state.shadow_fb);
  1353. glViewport(0, p_shadow_index * 2, state.shadow_texture_size, 2);
  1354. glDepthMask(GL_TRUE);
  1355. glEnable(GL_DEPTH_TEST);
  1356. glDepthFunc(GL_LESS);
  1357. glDisable(GL_BLEND);
  1358. glEnable(GL_SCISSOR_TEST);
  1359. glScissor(0, p_shadow_index * 2, state.shadow_texture_size, 2);
  1360. glClearColor(1.0, 1.0, 1.0, 1.0);
  1361. glClearDepth(1.0);
  1362. glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
  1363. glCullFace(GL_BACK);
  1364. glDisable(GL_CULL_FACE);
  1365. RS::CanvasOccluderPolygonCullMode cull_mode = RS::CANVAS_OCCLUDER_POLYGON_CULL_DISABLED;
  1366. CanvasOcclusionShaderGLES3::ShaderVariant variant = config->float_texture_supported ? CanvasOcclusionShaderGLES3::MODE_SHADOW : CanvasOcclusionShaderGLES3::MODE_SHADOW_RGBA;
  1367. bool success = shadow_render.shader.version_bind_shader(shadow_render.shader_version, variant);
  1368. if (!success) {
  1369. return;
  1370. }
  1371. Projection projection;
  1372. projection.set_orthogonal(-half_size, half_size, -0.5, 0.5, 0.0, distance);
  1373. projection = projection * Projection(Transform3D().looking_at(Vector3(0, 1, 0), Vector3(0, 0, -1)).affine_inverse());
  1374. shadow_render.shader.version_set_uniform(CanvasOcclusionShaderGLES3::PROJECTION, projection, shadow_render.shader_version, variant);
  1375. shadow_render.shader.version_set_uniform(CanvasOcclusionShaderGLES3::DIRECTION, 0.0, 1.0, shadow_render.shader_version, variant);
  1376. shadow_render.shader.version_set_uniform(CanvasOcclusionShaderGLES3::Z_FAR, distance, shadow_render.shader_version, variant);
  1377. LightOccluderInstance *instance = p_occluders;
  1378. while (instance) {
  1379. OccluderPolygon *co = occluder_polygon_owner.get_or_null(instance->occluder);
  1380. if (!co || co->vertex_array == 0 || !(p_light_mask & instance->light_mask)) {
  1381. instance = instance->next;
  1382. continue;
  1383. }
  1384. Transform2D modelview = to_light_xform * instance->xform_cache;
  1385. 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);
  1386. 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);
  1387. if (co->cull_mode != cull_mode) {
  1388. if (co->cull_mode == RS::CANVAS_OCCLUDER_POLYGON_CULL_DISABLED) {
  1389. glDisable(GL_CULL_FACE);
  1390. } else {
  1391. if (cull_mode == RS::CANVAS_OCCLUDER_POLYGON_CULL_DISABLED) {
  1392. // Last time was disabled, so enable and set proper face.
  1393. glEnable(GL_CULL_FACE);
  1394. }
  1395. glCullFace(co->cull_mode == RS::CANVAS_OCCLUDER_POLYGON_CULL_CLOCKWISE ? GL_FRONT : GL_BACK);
  1396. }
  1397. cull_mode = co->cull_mode;
  1398. }
  1399. glBindVertexArray(co->vertex_array);
  1400. glDrawElements(GL_TRIANGLES, 3 * co->line_point_count, GL_UNSIGNED_SHORT, 0);
  1401. instance = instance->next;
  1402. }
  1403. Transform2D to_shadow;
  1404. to_shadow.columns[0].x = 1.0 / -(half_size * 2.0);
  1405. to_shadow.columns[2].x = 0.5;
  1406. cl->shadow.directional_xform = to_shadow * to_light_xform;
  1407. glBindVertexArray(0);
  1408. glBindFramebuffer(GL_FRAMEBUFFER, 0);
  1409. glDepthMask(GL_FALSE);
  1410. glDisable(GL_DEPTH_TEST);
  1411. glDisable(GL_SCISSOR_TEST);
  1412. glDisable(GL_CULL_FACE);
  1413. }
  1414. void RasterizerCanvasGLES3::_update_shadow_atlas() {
  1415. GLES3::Config *config = GLES3::Config::get_singleton();
  1416. if (state.shadow_fb == 0) {
  1417. glActiveTexture(GL_TEXTURE0);
  1418. glGenFramebuffers(1, &state.shadow_fb);
  1419. glBindFramebuffer(GL_FRAMEBUFFER, state.shadow_fb);
  1420. glGenRenderbuffers(1, &state.shadow_depth_buffer);
  1421. glBindRenderbuffer(GL_RENDERBUFFER, state.shadow_depth_buffer);
  1422. glRenderbufferStorage(GL_RENDERBUFFER, GL_DEPTH_COMPONENT24, state.shadow_texture_size, data.max_lights_per_render * 2);
  1423. glFramebufferRenderbuffer(GL_FRAMEBUFFER, GL_DEPTH_ATTACHMENT, GL_RENDERBUFFER, state.shadow_depth_buffer);
  1424. glGenTextures(1, &state.shadow_texture);
  1425. glBindTexture(GL_TEXTURE_2D, state.shadow_texture);
  1426. if (config->float_texture_supported) {
  1427. glTexImage2D(GL_TEXTURE_2D, 0, GL_R32F, state.shadow_texture_size, data.max_lights_per_render * 2, 0, GL_RED, GL_FLOAT, nullptr);
  1428. } else {
  1429. glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA8, state.shadow_texture_size, data.max_lights_per_render * 2, 0, GL_RGBA, GL_UNSIGNED_BYTE, nullptr);
  1430. }
  1431. glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
  1432. glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
  1433. glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
  1434. glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
  1435. glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_BASE_LEVEL, 0);
  1436. glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAX_LEVEL, 1);
  1437. glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, state.shadow_texture, 0);
  1438. GLenum status = glCheckFramebufferStatus(GL_FRAMEBUFFER);
  1439. if (status != GL_FRAMEBUFFER_COMPLETE) {
  1440. glDeleteFramebuffers(1, &state.shadow_fb);
  1441. glDeleteTextures(1, &state.shadow_texture);
  1442. glDeleteRenderbuffers(1, &state.shadow_depth_buffer);
  1443. state.shadow_fb = 0;
  1444. state.shadow_texture = 0;
  1445. state.shadow_depth_buffer = 0;
  1446. WARN_PRINT("Could not create CanvasItem shadow atlas, status: " + GLES3::TextureStorage::get_singleton()->get_framebuffer_error(status));
  1447. }
  1448. glBindFramebuffer(GL_FRAMEBUFFER, GLES3::TextureStorage::system_fbo);
  1449. }
  1450. }
  1451. void RasterizerCanvasGLES3::render_sdf(RID p_render_target, LightOccluderInstance *p_occluders) {
  1452. GLES3::TextureStorage *texture_storage = GLES3::TextureStorage::get_singleton();
  1453. GLuint fb = texture_storage->render_target_get_sdf_framebuffer(p_render_target);
  1454. Rect2i rect = texture_storage->render_target_get_sdf_rect(p_render_target);
  1455. Transform2D to_sdf;
  1456. to_sdf.columns[0] *= rect.size.width;
  1457. to_sdf.columns[1] *= rect.size.height;
  1458. to_sdf.columns[2] = rect.position;
  1459. Transform2D to_clip;
  1460. to_clip.columns[0] *= 2.0;
  1461. to_clip.columns[1] *= 2.0;
  1462. to_clip.columns[2] = -Vector2(1.0, 1.0);
  1463. to_clip = to_clip * to_sdf.affine_inverse();
  1464. glBindFramebuffer(GL_FRAMEBUFFER, fb);
  1465. glViewport(0, 0, rect.size.width, rect.size.height);
  1466. glDepthMask(GL_FALSE);
  1467. glDisable(GL_DEPTH_TEST);
  1468. glDisable(GL_BLEND);
  1469. glDisable(GL_CULL_FACE);
  1470. glDisable(GL_SCISSOR_TEST);
  1471. glClearColor(0.0, 0.0, 0.0, 0.0);
  1472. glClear(GL_COLOR_BUFFER_BIT);
  1473. CanvasOcclusionShaderGLES3::ShaderVariant variant = CanvasOcclusionShaderGLES3::MODE_SDF;
  1474. bool success = shadow_render.shader.version_bind_shader(shadow_render.shader_version, variant);
  1475. if (!success) {
  1476. return;
  1477. }
  1478. shadow_render.shader.version_set_uniform(CanvasOcclusionShaderGLES3::PROJECTION, Projection(), shadow_render.shader_version, variant);
  1479. shadow_render.shader.version_set_uniform(CanvasOcclusionShaderGLES3::DIRECTION, 0.0, 0.0, shadow_render.shader_version, variant);
  1480. shadow_render.shader.version_set_uniform(CanvasOcclusionShaderGLES3::Z_FAR, 0.0, shadow_render.shader_version, variant);
  1481. LightOccluderInstance *instance = p_occluders;
  1482. while (instance) {
  1483. OccluderPolygon *oc = occluder_polygon_owner.get_or_null(instance->occluder);
  1484. if (!oc || oc->sdf_vertex_array == 0) {
  1485. instance = instance->next;
  1486. continue;
  1487. }
  1488. Transform2D modelview = to_clip * instance->xform_cache;
  1489. 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);
  1490. 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);
  1491. glBindVertexArray(oc->sdf_vertex_array);
  1492. glDrawElements(oc->sdf_is_lines ? GL_LINES : GL_TRIANGLES, oc->sdf_index_count, GL_UNSIGNED_INT, 0);
  1493. instance = instance->next;
  1494. }
  1495. texture_storage->render_target_sdf_process(p_render_target); //done rendering, process it
  1496. glBindVertexArray(0);
  1497. glBindFramebuffer(GL_FRAMEBUFFER, 0);
  1498. }
  1499. RID RasterizerCanvasGLES3::occluder_polygon_create() {
  1500. OccluderPolygon occluder;
  1501. return occluder_polygon_owner.make_rid(occluder);
  1502. }
  1503. void RasterizerCanvasGLES3::occluder_polygon_set_shape(RID p_occluder, const Vector<Vector2> &p_points, bool p_closed) {
  1504. OccluderPolygon *oc = occluder_polygon_owner.get_or_null(p_occluder);
  1505. ERR_FAIL_COND(!oc);
  1506. Vector<Vector2> lines;
  1507. if (p_points.size()) {
  1508. int lc = p_points.size() * 2;
  1509. lines.resize(lc - (p_closed ? 0 : 2));
  1510. {
  1511. Vector2 *w = lines.ptrw();
  1512. const Vector2 *r = p_points.ptr();
  1513. int max = lc / 2;
  1514. if (!p_closed) {
  1515. max--;
  1516. }
  1517. for (int i = 0; i < max; i++) {
  1518. Vector2 a = r[i];
  1519. Vector2 b = r[(i + 1) % (lc / 2)];
  1520. w[i * 2 + 0] = a;
  1521. w[i * 2 + 1] = b;
  1522. }
  1523. }
  1524. }
  1525. if (oc->line_point_count != lines.size() && oc->vertex_array != 0) {
  1526. glDeleteVertexArrays(1, &oc->vertex_array);
  1527. glDeleteBuffers(1, &oc->vertex_buffer);
  1528. glDeleteBuffers(1, &oc->index_buffer);
  1529. oc->vertex_array = 0;
  1530. oc->vertex_buffer = 0;
  1531. oc->index_buffer = 0;
  1532. }
  1533. if (lines.size()) {
  1534. Vector<uint8_t> geometry;
  1535. Vector<uint8_t> indices;
  1536. int lc = lines.size();
  1537. geometry.resize(lc * 6 * sizeof(float));
  1538. indices.resize(lc * 3 * sizeof(uint16_t));
  1539. {
  1540. uint8_t *vw = geometry.ptrw();
  1541. float *vwptr = reinterpret_cast<float *>(vw);
  1542. uint8_t *iw = indices.ptrw();
  1543. uint16_t *iwptr = (uint16_t *)iw;
  1544. const Vector2 *lr = lines.ptr();
  1545. const int POLY_HEIGHT = 16384;
  1546. for (int i = 0; i < lc / 2; i++) {
  1547. vwptr[i * 12 + 0] = lr[i * 2 + 0].x;
  1548. vwptr[i * 12 + 1] = lr[i * 2 + 0].y;
  1549. vwptr[i * 12 + 2] = POLY_HEIGHT;
  1550. vwptr[i * 12 + 3] = lr[i * 2 + 1].x;
  1551. vwptr[i * 12 + 4] = lr[i * 2 + 1].y;
  1552. vwptr[i * 12 + 5] = POLY_HEIGHT;
  1553. vwptr[i * 12 + 6] = lr[i * 2 + 1].x;
  1554. vwptr[i * 12 + 7] = lr[i * 2 + 1].y;
  1555. vwptr[i * 12 + 8] = -POLY_HEIGHT;
  1556. vwptr[i * 12 + 9] = lr[i * 2 + 0].x;
  1557. vwptr[i * 12 + 10] = lr[i * 2 + 0].y;
  1558. vwptr[i * 12 + 11] = -POLY_HEIGHT;
  1559. iwptr[i * 6 + 0] = i * 4 + 0;
  1560. iwptr[i * 6 + 1] = i * 4 + 1;
  1561. iwptr[i * 6 + 2] = i * 4 + 2;
  1562. iwptr[i * 6 + 3] = i * 4 + 2;
  1563. iwptr[i * 6 + 4] = i * 4 + 3;
  1564. iwptr[i * 6 + 5] = i * 4 + 0;
  1565. }
  1566. }
  1567. if (oc->vertex_array == 0) {
  1568. oc->line_point_count = lc;
  1569. glGenVertexArrays(1, &oc->vertex_array);
  1570. glBindVertexArray(oc->vertex_array);
  1571. glGenBuffers(1, &oc->vertex_buffer);
  1572. glBindBuffer(GL_ARRAY_BUFFER, oc->vertex_buffer);
  1573. glBufferData(GL_ARRAY_BUFFER, lc * 6 * sizeof(float), geometry.ptr(), GL_STATIC_DRAW);
  1574. glEnableVertexAttribArray(RS::ARRAY_VERTEX);
  1575. glVertexAttribPointer(RS::ARRAY_VERTEX, 3, GL_FLOAT, GL_FALSE, 3 * sizeof(float), nullptr);
  1576. glGenBuffers(1, &oc->index_buffer);
  1577. glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, oc->index_buffer);
  1578. glBufferData(GL_ELEMENT_ARRAY_BUFFER, 3 * lc * sizeof(uint16_t), indices.ptr(), GL_STATIC_DRAW);
  1579. glBindVertexArray(0);
  1580. } else {
  1581. glBindBuffer(GL_ARRAY_BUFFER, oc->vertex_buffer);
  1582. glBufferData(GL_ARRAY_BUFFER, lc * 6 * sizeof(float), geometry.ptr(), GL_STATIC_DRAW);
  1583. glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, oc->index_buffer);
  1584. glBufferData(GL_ELEMENT_ARRAY_BUFFER, 3 * lc * sizeof(uint16_t), indices.ptr(), GL_STATIC_DRAW);
  1585. glBindBuffer(GL_ARRAY_BUFFER, 0);
  1586. glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0);
  1587. }
  1588. }
  1589. // sdf
  1590. Vector<int> sdf_indices;
  1591. if (p_points.size()) {
  1592. if (p_closed) {
  1593. sdf_indices = Geometry2D::triangulate_polygon(p_points);
  1594. oc->sdf_is_lines = false;
  1595. } else {
  1596. int max = p_points.size();
  1597. sdf_indices.resize(max * 2);
  1598. int *iw = sdf_indices.ptrw();
  1599. for (int i = 0; i < max; i++) {
  1600. iw[i * 2 + 0] = i;
  1601. iw[i * 2 + 1] = (i + 1) % max;
  1602. }
  1603. oc->sdf_is_lines = true;
  1604. }
  1605. }
  1606. if (oc->sdf_index_count != sdf_indices.size() && oc->sdf_point_count != p_points.size() && oc->sdf_vertex_array != 0) {
  1607. glDeleteVertexArrays(1, &oc->sdf_vertex_array);
  1608. glDeleteBuffers(1, &oc->sdf_vertex_buffer);
  1609. glDeleteBuffers(1, &oc->sdf_index_buffer);
  1610. oc->sdf_vertex_array = 0;
  1611. oc->sdf_vertex_buffer = 0;
  1612. oc->sdf_index_buffer = 0;
  1613. oc->sdf_index_count = sdf_indices.size();
  1614. oc->sdf_point_count = p_points.size();
  1615. }
  1616. if (sdf_indices.size()) {
  1617. if (oc->sdf_vertex_array == 0) {
  1618. oc->sdf_index_count = sdf_indices.size();
  1619. oc->sdf_point_count = p_points.size();
  1620. glGenVertexArrays(1, &oc->sdf_vertex_array);
  1621. glBindVertexArray(oc->sdf_vertex_array);
  1622. glGenBuffers(1, &oc->sdf_vertex_buffer);
  1623. glBindBuffer(GL_ARRAY_BUFFER, oc->sdf_vertex_buffer);
  1624. glBufferData(GL_ARRAY_BUFFER, p_points.size() * 2 * sizeof(float), p_points.to_byte_array().ptr(), GL_STATIC_DRAW);
  1625. glEnableVertexAttribArray(RS::ARRAY_VERTEX);
  1626. glVertexAttribPointer(RS::ARRAY_VERTEX, 2, GL_FLOAT, GL_FALSE, 2 * sizeof(float), nullptr);
  1627. glGenBuffers(1, &oc->sdf_index_buffer);
  1628. glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, oc->sdf_index_buffer);
  1629. glBufferData(GL_ELEMENT_ARRAY_BUFFER, sdf_indices.size() * sizeof(uint32_t), sdf_indices.to_byte_array().ptr(), GL_STATIC_DRAW);
  1630. glBindVertexArray(0);
  1631. } else {
  1632. glBindBuffer(GL_ARRAY_BUFFER, oc->sdf_vertex_buffer);
  1633. glBufferData(GL_ARRAY_BUFFER, p_points.size() * 2 * sizeof(float), p_points.to_byte_array().ptr(), GL_STATIC_DRAW);
  1634. glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, oc->sdf_index_buffer);
  1635. glBufferData(GL_ELEMENT_ARRAY_BUFFER, sdf_indices.size() * sizeof(uint32_t), sdf_indices.to_byte_array().ptr(), GL_STATIC_DRAW);
  1636. glBindBuffer(GL_ARRAY_BUFFER, 0);
  1637. glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0);
  1638. }
  1639. }
  1640. }
  1641. void RasterizerCanvasGLES3::occluder_polygon_set_cull_mode(RID p_occluder, RS::CanvasOccluderPolygonCullMode p_mode) {
  1642. OccluderPolygon *oc = occluder_polygon_owner.get_or_null(p_occluder);
  1643. ERR_FAIL_COND(!oc);
  1644. oc->cull_mode = p_mode;
  1645. }
  1646. void RasterizerCanvasGLES3::set_shadow_texture_size(int p_size) {
  1647. GLES3::Config *config = GLES3::Config::get_singleton();
  1648. p_size = nearest_power_of_2_templated(p_size);
  1649. if (p_size == state.shadow_texture_size) {
  1650. return;
  1651. }
  1652. if (p_size > config->max_texture_size) {
  1653. p_size = config->max_texture_size;
  1654. 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.");
  1655. }
  1656. state.shadow_texture_size = p_size;
  1657. }
  1658. bool RasterizerCanvasGLES3::free(RID p_rid) {
  1659. if (canvas_light_owner.owns(p_rid)) {
  1660. CanvasLight *cl = canvas_light_owner.get_or_null(p_rid);
  1661. ERR_FAIL_COND_V(!cl, false);
  1662. canvas_light_owner.free(p_rid);
  1663. } else if (occluder_polygon_owner.owns(p_rid)) {
  1664. occluder_polygon_set_shape(p_rid, Vector<Vector2>(), false);
  1665. occluder_polygon_owner.free(p_rid);
  1666. } else {
  1667. return false;
  1668. }
  1669. return true;
  1670. }
  1671. void RasterizerCanvasGLES3::update() {
  1672. }
  1673. void RasterizerCanvasGLES3::canvas_begin(RID p_to_render_target, bool p_to_backbuffer) {
  1674. GLES3::TextureStorage *texture_storage = GLES3::TextureStorage::get_singleton();
  1675. GLES3::Config *config = GLES3::Config::get_singleton();
  1676. GLES3::RenderTarget *render_target = texture_storage->get_render_target(p_to_render_target);
  1677. if (p_to_backbuffer) {
  1678. glBindFramebuffer(GL_FRAMEBUFFER, render_target->backbuffer_fbo);
  1679. glActiveTexture(GL_TEXTURE0 + config->max_texture_image_units - 4);
  1680. GLES3::Texture *tex = texture_storage->get_texture(texture_storage->texture_gl_get_default(GLES3::DEFAULT_GL_TEXTURE_WHITE));
  1681. glBindTexture(GL_TEXTURE_2D, tex->tex_id);
  1682. } else {
  1683. glBindFramebuffer(GL_FRAMEBUFFER, render_target->fbo);
  1684. glActiveTexture(GL_TEXTURE0 + config->max_texture_image_units - 4);
  1685. glBindTexture(GL_TEXTURE_2D, render_target->backbuffer);
  1686. }
  1687. if (render_target->is_transparent || p_to_backbuffer) {
  1688. state.transparent_render_target = true;
  1689. glBlendFuncSeparate(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA, GL_ONE, GL_ONE_MINUS_SRC_ALPHA);
  1690. } else {
  1691. state.transparent_render_target = false;
  1692. glBlendFuncSeparate(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA, GL_ZERO, GL_ONE);
  1693. }
  1694. if (render_target && render_target->clear_requested) {
  1695. const Color &col = render_target->clear_color;
  1696. glClearColor(col.r, col.g, col.b, col.a);
  1697. glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT | GL_STENCIL_BUFFER_BIT);
  1698. render_target->clear_requested = false;
  1699. }
  1700. glActiveTexture(GL_TEXTURE0);
  1701. GLES3::Texture *tex = texture_storage->get_texture(texture_storage->texture_gl_get_default(GLES3::DEFAULT_GL_TEXTURE_WHITE));
  1702. glBindTexture(GL_TEXTURE_2D, tex->tex_id);
  1703. }
  1704. void RasterizerCanvasGLES3::_bind_canvas_texture(RID p_texture, RS::CanvasItemTextureFilter p_base_filter, RS::CanvasItemTextureRepeat p_base_repeat) {
  1705. GLES3::TextureStorage *texture_storage = GLES3::TextureStorage::get_singleton();
  1706. GLES3::Config *config = GLES3::Config::get_singleton();
  1707. if (p_texture == RID()) {
  1708. p_texture = default_canvas_texture;
  1709. }
  1710. if (state.current_tex == p_texture && state.current_filter_mode == p_base_filter && state.current_repeat_mode == p_base_repeat) {
  1711. return;
  1712. }
  1713. state.current_tex = p_texture;
  1714. state.current_filter_mode = p_base_filter;
  1715. state.current_repeat_mode = p_base_repeat;
  1716. GLES3::CanvasTexture *ct = nullptr;
  1717. GLES3::Texture *t = texture_storage->get_texture(p_texture);
  1718. if (t) {
  1719. ERR_FAIL_COND(!t->canvas_texture);
  1720. ct = t->canvas_texture;
  1721. if (t->render_target) {
  1722. t->render_target->used_in_frame = true;
  1723. }
  1724. } else {
  1725. ct = texture_storage->get_canvas_texture(p_texture);
  1726. }
  1727. if (!ct) {
  1728. // Invalid Texture RID.
  1729. _bind_canvas_texture(default_canvas_texture, p_base_filter, p_base_repeat);
  1730. return;
  1731. }
  1732. RS::CanvasItemTextureFilter filter = ct->texture_filter != RS::CANVAS_ITEM_TEXTURE_FILTER_DEFAULT ? ct->texture_filter : p_base_filter;
  1733. ERR_FAIL_COND(filter == RS::CANVAS_ITEM_TEXTURE_FILTER_DEFAULT);
  1734. RS::CanvasItemTextureRepeat repeat = ct->texture_repeat != RS::CANVAS_ITEM_TEXTURE_REPEAT_DEFAULT ? ct->texture_repeat : p_base_repeat;
  1735. ERR_FAIL_COND(repeat == RS::CANVAS_ITEM_TEXTURE_REPEAT_DEFAULT);
  1736. GLES3::Texture *texture = texture_storage->get_texture(ct->diffuse);
  1737. if (!texture) {
  1738. GLES3::Texture *tex = texture_storage->get_texture(texture_storage->texture_gl_get_default(GLES3::DEFAULT_GL_TEXTURE_WHITE));
  1739. glActiveTexture(GL_TEXTURE0);
  1740. glBindTexture(GL_TEXTURE_2D, tex->tex_id);
  1741. } else {
  1742. glActiveTexture(GL_TEXTURE0);
  1743. glBindTexture(GL_TEXTURE_2D, texture->tex_id);
  1744. texture->gl_set_filter(filter);
  1745. texture->gl_set_repeat(repeat);
  1746. if (texture->render_target) {
  1747. texture->render_target->used_in_frame = true;
  1748. }
  1749. }
  1750. GLES3::Texture *normal_map = texture_storage->get_texture(ct->normal_map);
  1751. if (!normal_map) {
  1752. glActiveTexture(GL_TEXTURE0 + config->max_texture_image_units - 6);
  1753. GLES3::Texture *tex = texture_storage->get_texture(texture_storage->texture_gl_get_default(GLES3::DEFAULT_GL_TEXTURE_NORMAL));
  1754. glBindTexture(GL_TEXTURE_2D, tex->tex_id);
  1755. } else {
  1756. glActiveTexture(GL_TEXTURE0 + config->max_texture_image_units - 6);
  1757. glBindTexture(GL_TEXTURE_2D, normal_map->tex_id);
  1758. normal_map->gl_set_filter(filter);
  1759. normal_map->gl_set_repeat(repeat);
  1760. if (normal_map->render_target) {
  1761. normal_map->render_target->used_in_frame = true;
  1762. }
  1763. }
  1764. GLES3::Texture *specular_map = texture_storage->get_texture(ct->specular);
  1765. if (!specular_map) {
  1766. glActiveTexture(GL_TEXTURE0 + config->max_texture_image_units - 7);
  1767. GLES3::Texture *tex = texture_storage->get_texture(texture_storage->texture_gl_get_default(GLES3::DEFAULT_GL_TEXTURE_WHITE));
  1768. glBindTexture(GL_TEXTURE_2D, tex->tex_id);
  1769. } else {
  1770. glActiveTexture(GL_TEXTURE0 + config->max_texture_image_units - 7);
  1771. glBindTexture(GL_TEXTURE_2D, specular_map->tex_id);
  1772. specular_map->gl_set_filter(filter);
  1773. specular_map->gl_set_repeat(repeat);
  1774. if (specular_map->render_target) {
  1775. specular_map->render_target->used_in_frame = true;
  1776. }
  1777. }
  1778. }
  1779. 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) {
  1780. GLES3::TextureStorage *texture_storage = GLES3::TextureStorage::get_singleton();
  1781. if (p_texture == RID()) {
  1782. p_texture = default_canvas_texture;
  1783. }
  1784. GLES3::CanvasTexture *ct = nullptr;
  1785. GLES3::Texture *t = texture_storage->get_texture(p_texture);
  1786. if (t) {
  1787. //regular texture
  1788. if (!t->canvas_texture) {
  1789. t->canvas_texture = memnew(GLES3::CanvasTexture);
  1790. t->canvas_texture->diffuse = p_texture;
  1791. }
  1792. ct = t->canvas_texture;
  1793. } else {
  1794. ct = texture_storage->get_canvas_texture(p_texture);
  1795. }
  1796. if (!ct) {
  1797. // Invalid Texture RID.
  1798. _prepare_canvas_texture(default_canvas_texture, p_base_filter, p_base_repeat, r_index, r_texpixel_size);
  1799. return;
  1800. }
  1801. GLES3::Texture *texture = texture_storage->get_texture(ct->diffuse);
  1802. Size2i size_cache;
  1803. if (!texture) {
  1804. ct->diffuse = texture_storage->texture_gl_get_default(GLES3::DEFAULT_GL_TEXTURE_WHITE);
  1805. GLES3::Texture *tex = texture_storage->get_texture(ct->diffuse);
  1806. size_cache = Size2i(tex->width, tex->height);
  1807. } else {
  1808. size_cache = Size2i(texture->width, texture->height);
  1809. }
  1810. GLES3::Texture *normal_map = texture_storage->get_texture(ct->normal_map);
  1811. if (ct->specular_color.a < 0.999) {
  1812. state.instance_data_array[r_index].flags |= FLAGS_DEFAULT_SPECULAR_MAP_USED;
  1813. } else {
  1814. state.instance_data_array[r_index].flags &= ~FLAGS_DEFAULT_SPECULAR_MAP_USED;
  1815. }
  1816. if (normal_map) {
  1817. state.instance_data_array[r_index].flags |= FLAGS_DEFAULT_NORMAL_MAP_USED;
  1818. } else {
  1819. state.instance_data_array[r_index].flags &= ~FLAGS_DEFAULT_NORMAL_MAP_USED;
  1820. }
  1821. state.instance_data_array[r_index].specular_shininess = uint32_t(CLAMP(ct->specular_color.a * 255.0, 0, 255)) << 24;
  1822. state.instance_data_array[r_index].specular_shininess |= uint32_t(CLAMP(ct->specular_color.b * 255.0, 0, 255)) << 16;
  1823. state.instance_data_array[r_index].specular_shininess |= uint32_t(CLAMP(ct->specular_color.g * 255.0, 0, 255)) << 8;
  1824. state.instance_data_array[r_index].specular_shininess |= uint32_t(CLAMP(ct->specular_color.r * 255.0, 0, 255));
  1825. r_texpixel_size.x = 1.0 / float(size_cache.x);
  1826. r_texpixel_size.y = 1.0 / float(size_cache.y);
  1827. state.instance_data_array[r_index].color_texture_pixel_size[0] = r_texpixel_size.x;
  1828. state.instance_data_array[r_index].color_texture_pixel_size[1] = r_texpixel_size.y;
  1829. }
  1830. void RasterizerCanvasGLES3::reset_canvas() {
  1831. glDisable(GL_CULL_FACE);
  1832. glDisable(GL_DEPTH_TEST);
  1833. glDisable(GL_SCISSOR_TEST);
  1834. glEnable(GL_BLEND);
  1835. glBlendEquation(GL_FUNC_ADD);
  1836. glBlendFuncSeparate(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA, GL_ZERO, GL_ONE);
  1837. glActiveTexture(GL_TEXTURE0 + GLES3::Config::get_singleton()->max_texture_image_units - 2);
  1838. glBindTexture(GL_TEXTURE_2D, 0);
  1839. glActiveTexture(GL_TEXTURE0 + GLES3::Config::get_singleton()->max_texture_image_units - 3);
  1840. glBindTexture(GL_TEXTURE_2D, 0);
  1841. glActiveTexture(GL_TEXTURE0);
  1842. glBindBuffer(GL_ARRAY_BUFFER, 0);
  1843. glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0);
  1844. }
  1845. void RasterizerCanvasGLES3::draw_lens_distortion_rect(const Rect2 &p_rect, float p_k1, float p_k2, const Vector2 &p_eye_center, float p_oversample) {
  1846. }
  1847. 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) {
  1848. // We interleave the vertex data into one big VBO to improve cache coherence
  1849. uint32_t vertex_count = p_points.size();
  1850. uint32_t stride = 2;
  1851. if ((uint32_t)p_colors.size() == vertex_count) {
  1852. stride += 4;
  1853. }
  1854. if ((uint32_t)p_uvs.size() == vertex_count) {
  1855. stride += 2;
  1856. }
  1857. if ((uint32_t)p_bones.size() == vertex_count * 4 && (uint32_t)p_weights.size() == vertex_count * 4) {
  1858. stride += 4;
  1859. }
  1860. PolygonBuffers pb;
  1861. glGenBuffers(1, &pb.vertex_buffer);
  1862. glGenVertexArrays(1, &pb.vertex_array);
  1863. glBindVertexArray(pb.vertex_array);
  1864. pb.count = vertex_count;
  1865. pb.index_buffer = 0;
  1866. uint32_t buffer_size = stride * p_points.size();
  1867. Vector<uint8_t> polygon_buffer;
  1868. polygon_buffer.resize(buffer_size * sizeof(float));
  1869. {
  1870. glBindBuffer(GL_ARRAY_BUFFER, pb.vertex_buffer);
  1871. uint8_t *r = polygon_buffer.ptrw();
  1872. float *fptr = reinterpret_cast<float *>(r);
  1873. uint32_t *uptr = (uint32_t *)r;
  1874. uint32_t base_offset = 0;
  1875. {
  1876. // Always uses vertex positions
  1877. glEnableVertexAttribArray(RS::ARRAY_VERTEX);
  1878. glVertexAttribPointer(RS::ARRAY_VERTEX, 2, GL_FLOAT, GL_FALSE, stride * sizeof(float), nullptr);
  1879. const Vector2 *points_ptr = p_points.ptr();
  1880. for (uint32_t i = 0; i < vertex_count; i++) {
  1881. fptr[base_offset + i * stride + 0] = points_ptr[i].x;
  1882. fptr[base_offset + i * stride + 1] = points_ptr[i].y;
  1883. }
  1884. base_offset += 2;
  1885. }
  1886. // Next add colors
  1887. if ((uint32_t)p_colors.size() == vertex_count) {
  1888. glEnableVertexAttribArray(RS::ARRAY_COLOR);
  1889. glVertexAttribPointer(RS::ARRAY_COLOR, 4, GL_FLOAT, GL_FALSE, stride * sizeof(float), CAST_INT_TO_UCHAR_PTR(base_offset * sizeof(float)));
  1890. const Color *color_ptr = p_colors.ptr();
  1891. for (uint32_t i = 0; i < vertex_count; i++) {
  1892. fptr[base_offset + i * stride + 0] = color_ptr[i].r;
  1893. fptr[base_offset + i * stride + 1] = color_ptr[i].g;
  1894. fptr[base_offset + i * stride + 2] = color_ptr[i].b;
  1895. fptr[base_offset + i * stride + 3] = color_ptr[i].a;
  1896. }
  1897. base_offset += 4;
  1898. } else {
  1899. glDisableVertexAttribArray(RS::ARRAY_COLOR);
  1900. pb.color_disabled = true;
  1901. pb.color = p_colors.size() == 1 ? p_colors[0] : Color(1.0, 1.0, 1.0, 1.0);
  1902. }
  1903. if ((uint32_t)p_uvs.size() == vertex_count) {
  1904. glEnableVertexAttribArray(RS::ARRAY_TEX_UV);
  1905. glVertexAttribPointer(RS::ARRAY_TEX_UV, 2, GL_FLOAT, GL_FALSE, stride * sizeof(float), CAST_INT_TO_UCHAR_PTR(base_offset * sizeof(float)));
  1906. const Vector2 *uv_ptr = p_uvs.ptr();
  1907. for (uint32_t i = 0; i < vertex_count; i++) {
  1908. fptr[base_offset + i * stride + 0] = uv_ptr[i].x;
  1909. fptr[base_offset + i * stride + 1] = uv_ptr[i].y;
  1910. }
  1911. base_offset += 2;
  1912. } else {
  1913. glDisableVertexAttribArray(RS::ARRAY_TEX_UV);
  1914. }
  1915. if ((uint32_t)p_indices.size() == vertex_count * 4 && (uint32_t)p_weights.size() == vertex_count * 4) {
  1916. glEnableVertexAttribArray(RS::ARRAY_BONES);
  1917. glVertexAttribPointer(RS::ARRAY_BONES, 4, GL_UNSIGNED_INT, GL_FALSE, stride * sizeof(float), CAST_INT_TO_UCHAR_PTR(base_offset * sizeof(float)));
  1918. const int *bone_ptr = p_bones.ptr();
  1919. for (uint32_t i = 0; i < vertex_count; i++) {
  1920. uint16_t *bone16w = (uint16_t *)&uptr[base_offset + i * stride];
  1921. bone16w[0] = bone_ptr[i * 4 + 0];
  1922. bone16w[1] = bone_ptr[i * 4 + 1];
  1923. bone16w[2] = bone_ptr[i * 4 + 2];
  1924. bone16w[3] = bone_ptr[i * 4 + 3];
  1925. }
  1926. base_offset += 2;
  1927. } else {
  1928. glDisableVertexAttribArray(RS::ARRAY_BONES);
  1929. }
  1930. if ((uint32_t)p_weights.size() == vertex_count * 4) {
  1931. glEnableVertexAttribArray(RS::ARRAY_WEIGHTS);
  1932. glVertexAttribPointer(RS::ARRAY_WEIGHTS, 4, GL_FLOAT, GL_FALSE, stride * sizeof(float), CAST_INT_TO_UCHAR_PTR(base_offset * sizeof(float)));
  1933. const float *weight_ptr = p_weights.ptr();
  1934. for (uint32_t i = 0; i < vertex_count; i++) {
  1935. uint16_t *weight16w = (uint16_t *)&uptr[base_offset + i * stride];
  1936. weight16w[0] = CLAMP(weight_ptr[i * 4 + 0] * 65535, 0, 65535);
  1937. weight16w[1] = CLAMP(weight_ptr[i * 4 + 1] * 65535, 0, 65535);
  1938. weight16w[2] = CLAMP(weight_ptr[i * 4 + 2] * 65535, 0, 65535);
  1939. weight16w[3] = CLAMP(weight_ptr[i * 4 + 3] * 65535, 0, 65535);
  1940. }
  1941. base_offset += 2;
  1942. } else {
  1943. glDisableVertexAttribArray(RS::ARRAY_WEIGHTS);
  1944. }
  1945. ERR_FAIL_COND_V(base_offset != stride, 0);
  1946. glBufferData(GL_ARRAY_BUFFER, vertex_count * stride * sizeof(float), polygon_buffer.ptr(), GL_STATIC_DRAW);
  1947. }
  1948. if (p_indices.size()) {
  1949. //create indices, as indices were requested
  1950. Vector<uint8_t> index_buffer;
  1951. index_buffer.resize(p_indices.size() * sizeof(int32_t));
  1952. {
  1953. uint8_t *w = index_buffer.ptrw();
  1954. memcpy(w, p_indices.ptr(), sizeof(int32_t) * p_indices.size());
  1955. }
  1956. glGenBuffers(1, &pb.index_buffer);
  1957. glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, pb.index_buffer);
  1958. glBufferData(GL_ELEMENT_ARRAY_BUFFER, p_indices.size() * 4, index_buffer.ptr(), GL_STATIC_DRAW);
  1959. pb.count = p_indices.size();
  1960. }
  1961. glBindVertexArray(0);
  1962. glBindBuffer(GL_ARRAY_BUFFER, 0);
  1963. glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0);
  1964. PolygonID id = polygon_buffers.last_id++;
  1965. polygon_buffers.polygons[id] = pb;
  1966. return id;
  1967. }
  1968. void RasterizerCanvasGLES3::free_polygon(PolygonID p_polygon) {
  1969. PolygonBuffers *pb_ptr = polygon_buffers.polygons.getptr(p_polygon);
  1970. ERR_FAIL_COND(!pb_ptr);
  1971. PolygonBuffers &pb = *pb_ptr;
  1972. if (pb.index_buffer != 0) {
  1973. glDeleteBuffers(1, &pb.index_buffer);
  1974. }
  1975. glDeleteVertexArrays(1, &pb.vertex_array);
  1976. glDeleteBuffers(1, &pb.vertex_buffer);
  1977. polygon_buffers.polygons.erase(p_polygon);
  1978. }
  1979. // Creates a new uniform buffer and uses it right away
  1980. // This expands the instance buffer continually
  1981. // In theory allocations can reach as high as number of windows * 3 frames
  1982. // because OpenGL can start rendering subsequent frames before finishing the current one
  1983. void RasterizerCanvasGLES3::_allocate_instance_data_buffer() {
  1984. GLuint new_buffers[3];
  1985. glGenBuffers(3, new_buffers);
  1986. // Batch UBO.
  1987. glBindBuffer(GL_ARRAY_BUFFER, new_buffers[0]);
  1988. glBufferData(GL_ARRAY_BUFFER, data.max_instance_buffer_size, nullptr, GL_STREAM_DRAW);
  1989. // Light uniform buffer.
  1990. glBindBuffer(GL_UNIFORM_BUFFER, new_buffers[1]);
  1991. glBufferData(GL_UNIFORM_BUFFER, sizeof(LightUniform) * data.max_lights_per_render, nullptr, GL_STREAM_DRAW);
  1992. // State buffer.
  1993. glBindBuffer(GL_UNIFORM_BUFFER, new_buffers[2]);
  1994. glBufferData(GL_UNIFORM_BUFFER, sizeof(StateBuffer), nullptr, GL_STREAM_DRAW);
  1995. state.current_data_buffer_index = (state.current_data_buffer_index + 1);
  1996. DataBuffer db;
  1997. db.instance_buffers.push_back(new_buffers[0]);
  1998. db.light_ubo = new_buffers[1];
  1999. db.state_ubo = new_buffers[2];
  2000. db.last_frame_used = RSG::rasterizer->get_frame_number();
  2001. state.canvas_instance_data_buffers.insert(state.current_data_buffer_index, db);
  2002. state.current_data_buffer_index = state.current_data_buffer_index % state.canvas_instance_data_buffers.size();
  2003. glBindBuffer(GL_ARRAY_BUFFER, 0);
  2004. glBindBuffer(GL_UNIFORM_BUFFER, 0);
  2005. }
  2006. void RasterizerCanvasGLES3::_allocate_instance_buffer() {
  2007. state.current_instance_buffer_index++;
  2008. if (int(state.current_instance_buffer_index) < state.canvas_instance_data_buffers[state.current_data_buffer_index].instance_buffers.size()) {
  2009. // We already allocated another buffer in a previous frame, so we can just use it.
  2010. return;
  2011. }
  2012. GLuint new_buffer;
  2013. glGenBuffers(1, &new_buffer);
  2014. glBindBuffer(GL_ARRAY_BUFFER, new_buffer);
  2015. glBufferData(GL_ARRAY_BUFFER, data.max_instance_buffer_size, nullptr, GL_STREAM_DRAW);
  2016. state.canvas_instance_data_buffers[state.current_data_buffer_index].instance_buffers.push_back(new_buffer);
  2017. glBindBuffer(GL_ARRAY_BUFFER, 0);
  2018. }
  2019. void RasterizerCanvasGLES3::set_time(double p_time) {
  2020. state.time = p_time;
  2021. }
  2022. RasterizerCanvasGLES3 *RasterizerCanvasGLES3::singleton = nullptr;
  2023. RasterizerCanvasGLES3 *RasterizerCanvasGLES3::get_singleton() {
  2024. return singleton;
  2025. }
  2026. RasterizerCanvasGLES3::RasterizerCanvasGLES3() {
  2027. singleton = this;
  2028. GLES3::TextureStorage *texture_storage = GLES3::TextureStorage::get_singleton();
  2029. GLES3::MaterialStorage *material_storage = GLES3::MaterialStorage::get_singleton();
  2030. GLES3::Config *config = GLES3::Config::get_singleton();
  2031. polygon_buffers.last_id = 1;
  2032. // quad buffer
  2033. {
  2034. glGenBuffers(1, &data.canvas_quad_vertices);
  2035. glBindBuffer(GL_ARRAY_BUFFER, data.canvas_quad_vertices);
  2036. const float qv[8] = {
  2037. 0, 0,
  2038. 0, 1,
  2039. 1, 1,
  2040. 1, 0
  2041. };
  2042. glBufferData(GL_ARRAY_BUFFER, sizeof(float) * 8, qv, GL_STATIC_DRAW);
  2043. glBindBuffer(GL_ARRAY_BUFFER, 0);
  2044. glGenVertexArrays(1, &data.canvas_quad_array);
  2045. glBindVertexArray(data.canvas_quad_array);
  2046. glBindBuffer(GL_ARRAY_BUFFER, data.canvas_quad_vertices);
  2047. glVertexAttribPointer(0, 2, GL_FLOAT, GL_FALSE, sizeof(float) * 2, nullptr);
  2048. glEnableVertexAttribArray(0);
  2049. glBindVertexArray(0);
  2050. glBindBuffer(GL_ARRAY_BUFFER, 0); //unbind
  2051. }
  2052. {
  2053. //particle quad buffers
  2054. glGenBuffers(1, &data.particle_quad_vertices);
  2055. glBindBuffer(GL_ARRAY_BUFFER, data.particle_quad_vertices);
  2056. {
  2057. //quad of size 1, with pivot on the center for particles, then regular UVS. Color is general plus fetched from particle
  2058. const float qv[16] = {
  2059. -0.5, -0.5,
  2060. 0.0, 0.0,
  2061. -0.5, 0.5,
  2062. 0.0, 1.0,
  2063. 0.5, 0.5,
  2064. 1.0, 1.0,
  2065. 0.5, -0.5,
  2066. 1.0, 0.0
  2067. };
  2068. glBufferData(GL_ARRAY_BUFFER, sizeof(float) * 16, qv, GL_STATIC_DRAW);
  2069. }
  2070. glBindBuffer(GL_ARRAY_BUFFER, 0); //unbind
  2071. glGenVertexArrays(1, &data.particle_quad_array);
  2072. glBindVertexArray(data.particle_quad_array);
  2073. glBindBuffer(GL_ARRAY_BUFFER, data.particle_quad_vertices);
  2074. glEnableVertexAttribArray(RS::ARRAY_VERTEX);
  2075. glVertexAttribPointer(RS::ARRAY_VERTEX, 2, GL_FLOAT, GL_FALSE, sizeof(float) * 4, nullptr);
  2076. glEnableVertexAttribArray(RS::ARRAY_TEX_UV);
  2077. glVertexAttribPointer(RS::ARRAY_TEX_UV, 2, GL_FLOAT, GL_FALSE, sizeof(float) * 4, CAST_INT_TO_UCHAR_PTR(8));
  2078. glBindVertexArray(0);
  2079. glBindBuffer(GL_ARRAY_BUFFER, 0); //unbind
  2080. }
  2081. // ninepatch buffers
  2082. {
  2083. // array buffer
  2084. glGenBuffers(1, &data.ninepatch_vertices);
  2085. glBindBuffer(GL_ARRAY_BUFFER, data.ninepatch_vertices);
  2086. glBufferData(GL_ARRAY_BUFFER, sizeof(float) * (16 + 16) * 2, nullptr, GL_DYNAMIC_DRAW);
  2087. glBindBuffer(GL_ARRAY_BUFFER, 0);
  2088. // element buffer
  2089. glGenBuffers(1, &data.ninepatch_elements);
  2090. glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, data.ninepatch_elements);
  2091. #define _EIDX(y, x) (y * 4 + x)
  2092. uint8_t elems[3 * 2 * 9] = {
  2093. // first row
  2094. _EIDX(0, 0), _EIDX(0, 1), _EIDX(1, 1),
  2095. _EIDX(1, 1), _EIDX(1, 0), _EIDX(0, 0),
  2096. _EIDX(0, 1), _EIDX(0, 2), _EIDX(1, 2),
  2097. _EIDX(1, 2), _EIDX(1, 1), _EIDX(0, 1),
  2098. _EIDX(0, 2), _EIDX(0, 3), _EIDX(1, 3),
  2099. _EIDX(1, 3), _EIDX(1, 2), _EIDX(0, 2),
  2100. // second row
  2101. _EIDX(1, 0), _EIDX(1, 1), _EIDX(2, 1),
  2102. _EIDX(2, 1), _EIDX(2, 0), _EIDX(1, 0),
  2103. // the center one would be here, but we'll put it at the end
  2104. // so it's easier to disable the center and be able to use
  2105. // one draw call for both
  2106. _EIDX(1, 2), _EIDX(1, 3), _EIDX(2, 3),
  2107. _EIDX(2, 3), _EIDX(2, 2), _EIDX(1, 2),
  2108. // third row
  2109. _EIDX(2, 0), _EIDX(2, 1), _EIDX(3, 1),
  2110. _EIDX(3, 1), _EIDX(3, 0), _EIDX(2, 0),
  2111. _EIDX(2, 1), _EIDX(2, 2), _EIDX(3, 2),
  2112. _EIDX(3, 2), _EIDX(3, 1), _EIDX(2, 1),
  2113. _EIDX(2, 2), _EIDX(2, 3), _EIDX(3, 3),
  2114. _EIDX(3, 3), _EIDX(3, 2), _EIDX(2, 2),
  2115. // center field
  2116. _EIDX(1, 1), _EIDX(1, 2), _EIDX(2, 2),
  2117. _EIDX(2, 2), _EIDX(2, 1), _EIDX(1, 1)
  2118. };
  2119. #undef _EIDX
  2120. glBufferData(GL_ELEMENT_ARRAY_BUFFER, sizeof(elems), elems, GL_STATIC_DRAW);
  2121. glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0);
  2122. }
  2123. int uniform_max_size = config->max_uniform_buffer_size;
  2124. if (uniform_max_size < 65536) {
  2125. data.max_lights_per_render = 64;
  2126. } else {
  2127. data.max_lights_per_render = 256;
  2128. }
  2129. // Reserve 3 Uniform Buffers for instance data Frame N, N+1 and N+2
  2130. data.max_instances_per_buffer = uint32_t(GLOBAL_GET("rendering/gl_compatibility/item_buffer_size"));
  2131. data.max_instance_buffer_size = data.max_instances_per_buffer * sizeof(InstanceData); // 16,384 instances * 128 bytes = 2,097,152 bytes = 2,048 kb
  2132. state.canvas_instance_data_buffers.resize(3);
  2133. state.canvas_instance_batches.reserve(200);
  2134. for (int i = 0; i < 3; i++) {
  2135. GLuint new_buffers[3];
  2136. glGenBuffers(3, new_buffers);
  2137. // Batch UBO.
  2138. glBindBuffer(GL_ARRAY_BUFFER, new_buffers[0]);
  2139. glBufferData(GL_ARRAY_BUFFER, data.max_instance_buffer_size, nullptr, GL_STREAM_DRAW);
  2140. // Light uniform buffer.
  2141. glBindBuffer(GL_UNIFORM_BUFFER, new_buffers[1]);
  2142. glBufferData(GL_UNIFORM_BUFFER, sizeof(LightUniform) * data.max_lights_per_render, nullptr, GL_STREAM_DRAW);
  2143. // State buffer.
  2144. glBindBuffer(GL_UNIFORM_BUFFER, new_buffers[2]);
  2145. glBufferData(GL_UNIFORM_BUFFER, sizeof(StateBuffer), nullptr, GL_STREAM_DRAW);
  2146. DataBuffer db;
  2147. db.instance_buffers.push_back(new_buffers[0]);
  2148. db.light_ubo = new_buffers[1];
  2149. db.state_ubo = new_buffers[2];
  2150. db.last_frame_used = 0;
  2151. db.fence = glFenceSync(GL_SYNC_GPU_COMMANDS_COMPLETE, 0);
  2152. state.canvas_instance_data_buffers[i] = db;
  2153. }
  2154. glBindBuffer(GL_ARRAY_BUFFER, 0);
  2155. glBindBuffer(GL_UNIFORM_BUFFER, 0);
  2156. state.instance_data_array = memnew_arr(InstanceData, data.max_instances_per_buffer);
  2157. state.light_uniforms = memnew_arr(LightUniform, data.max_lights_per_render);
  2158. {
  2159. const uint32_t indices[6] = { 0, 2, 1, 3, 2, 0 };
  2160. glGenVertexArrays(1, &data.indexed_quad_array);
  2161. glBindVertexArray(data.indexed_quad_array);
  2162. glBindBuffer(GL_ARRAY_BUFFER, data.canvas_quad_vertices);
  2163. glGenBuffers(1, &data.indexed_quad_buffer);
  2164. glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, data.indexed_quad_buffer);
  2165. glBufferData(GL_ELEMENT_ARRAY_BUFFER, sizeof(uint32_t) * 6, indices, GL_STATIC_DRAW);
  2166. glBindVertexArray(0);
  2167. }
  2168. String global_defines;
  2169. global_defines += "#define MAX_GLOBAL_SHADER_UNIFORMS 256\n"; // TODO: this is arbitrary for now
  2170. global_defines += "#define MAX_LIGHTS " + itos(data.max_lights_per_render) + "\n";
  2171. GLES3::MaterialStorage::get_singleton()->shaders.canvas_shader.initialize(global_defines, 1);
  2172. data.canvas_shader_default_version = GLES3::MaterialStorage::get_singleton()->shaders.canvas_shader.version_create();
  2173. shadow_render.shader.initialize();
  2174. shadow_render.shader_version = shadow_render.shader.version_create();
  2175. {
  2176. default_canvas_group_shader = material_storage->shader_allocate();
  2177. material_storage->shader_initialize(default_canvas_group_shader);
  2178. material_storage->shader_set_code(default_canvas_group_shader, R"(
  2179. // Default CanvasGroup shader.
  2180. shader_type canvas_item;
  2181. render_mode unshaded;
  2182. uniform sampler2D screen_texture : hint_screen_texture, repeat_disable, filter_nearest;
  2183. void fragment() {
  2184. vec4 c = textureLod(screen_texture, SCREEN_UV, 0.0);
  2185. if (c.a > 0.0001) {
  2186. c.rgb /= c.a;
  2187. }
  2188. COLOR *= c;
  2189. }
  2190. )");
  2191. default_canvas_group_material = material_storage->material_allocate();
  2192. material_storage->material_initialize(default_canvas_group_material);
  2193. material_storage->material_set_shader(default_canvas_group_material, default_canvas_group_shader);
  2194. }
  2195. {
  2196. default_clip_children_shader = material_storage->shader_allocate();
  2197. material_storage->shader_initialize(default_clip_children_shader);
  2198. material_storage->shader_set_code(default_clip_children_shader, R"(
  2199. // Default clip children shader.
  2200. shader_type canvas_item;
  2201. render_mode unshaded;
  2202. uniform sampler2D screen_texture : hint_screen_texture, repeat_disable, filter_nearest;
  2203. void fragment() {
  2204. vec4 c = textureLod(screen_texture, SCREEN_UV, 0.0);
  2205. COLOR.rgb = c.rgb;
  2206. }
  2207. )");
  2208. default_clip_children_material = material_storage->material_allocate();
  2209. material_storage->material_initialize(default_clip_children_material);
  2210. material_storage->material_set_shader(default_clip_children_material, default_clip_children_shader);
  2211. }
  2212. default_canvas_texture = texture_storage->canvas_texture_allocate();
  2213. texture_storage->canvas_texture_initialize(default_canvas_texture);
  2214. state.time = 0.0;
  2215. }
  2216. RasterizerCanvasGLES3::~RasterizerCanvasGLES3() {
  2217. singleton = nullptr;
  2218. GLES3::MaterialStorage *material_storage = GLES3::MaterialStorage::get_singleton();
  2219. material_storage->shaders.canvas_shader.version_free(data.canvas_shader_default_version);
  2220. shadow_render.shader.version_free(shadow_render.shader_version);
  2221. material_storage->material_free(default_canvas_group_material);
  2222. material_storage->shader_free(default_canvas_group_shader);
  2223. material_storage->material_free(default_clip_children_material);
  2224. material_storage->shader_free(default_clip_children_shader);
  2225. singleton = nullptr;
  2226. glDeleteBuffers(1, &data.canvas_quad_vertices);
  2227. glDeleteVertexArrays(1, &data.canvas_quad_array);
  2228. glDeleteBuffers(1, &data.canvas_quad_vertices);
  2229. glDeleteVertexArrays(1, &data.canvas_quad_array);
  2230. GLES3::TextureStorage::get_singleton()->canvas_texture_free(default_canvas_texture);
  2231. memdelete_arr(state.instance_data_array);
  2232. memdelete_arr(state.light_uniforms);
  2233. if (state.shadow_fb != 0) {
  2234. glDeleteFramebuffers(1, &state.shadow_fb);
  2235. glDeleteTextures(1, &state.shadow_texture);
  2236. glDeleteRenderbuffers(1, &state.shadow_depth_buffer);
  2237. state.shadow_fb = 0;
  2238. state.shadow_texture = 0;
  2239. state.shadow_depth_buffer = 0;
  2240. }
  2241. }
  2242. #endif // GLES3_ENABLED