rasterizer_canvas_gles2.cpp 129 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572
  1. /*************************************************************************/
  2. /* rasterizer_canvas_gles2.cpp */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2020 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2020 Godot Engine contributors (cf. AUTHORS.md). */
  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_gles2.h"
  31. #include "core/os/os.h"
  32. #include "core/project_settings.h"
  33. #include "rasterizer_scene_gles2.h"
  34. #include "servers/visual/visual_server_raster.h"
  35. static const GLenum gl_primitive[] = {
  36. GL_POINTS,
  37. GL_LINES,
  38. GL_LINE_STRIP,
  39. GL_LINE_LOOP,
  40. GL_TRIANGLES,
  41. GL_TRIANGLE_STRIP,
  42. GL_TRIANGLE_FAN
  43. };
  44. RasterizerCanvasGLES2::BatchData::BatchData() {
  45. reset_flush();
  46. gl_vertex_buffer = 0;
  47. gl_index_buffer = 0;
  48. max_quads = 0;
  49. vertex_buffer_size_units = 0;
  50. vertex_buffer_size_bytes = 0;
  51. index_buffer_size_units = 0;
  52. index_buffer_size_bytes = 0;
  53. use_colored_vertices = false;
  54. settings_use_batching = false;
  55. settings_max_join_item_commands = 0;
  56. settings_colored_vertex_format_threshold = 0.0f;
  57. settings_batch_buffer_num_verts = 0;
  58. scissor_threshold_area = 0.0f;
  59. joined_item_batch_flags = 0;
  60. diagnose_frame = false;
  61. next_diagnose_tick = 10000;
  62. diagnose_frame_number = 9999999999; // some high number
  63. join_across_z_indices = true;
  64. settings_item_reordering_lookahead = 0;
  65. settings_use_batching_original_choice = false;
  66. settings_flash_batching = false;
  67. settings_diagnose_frame = false;
  68. settings_scissor_lights = false;
  69. settings_scissor_threshold = -1.0f;
  70. settings_use_single_rect_fallback = false;
  71. settings_light_max_join_items = 16;
  72. settings_uv_contract = false;
  73. settings_uv_contract_amount = 0.0f;
  74. stats_items_sorted = 0;
  75. stats_light_items_joined = 0;
  76. }
  77. void RasterizerCanvasGLES2::RenderItemState::reset() {
  78. current_clip = nullptr;
  79. shader_cache = nullptr;
  80. rebind_shader = true;
  81. prev_use_skeleton = false;
  82. last_blend_mode = -1;
  83. canvas_last_material = RID();
  84. item_group_z = 0;
  85. item_group_light = nullptr;
  86. final_modulate = Color(-1.0, -1.0, -1.0, -1.0); // just something unlikely
  87. joined_item = nullptr;
  88. }
  89. // just translate the color into something easily readable and not too verbose
  90. String RasterizerCanvasGLES2::BatchColor::to_string() const {
  91. String sz = "{";
  92. const float *data = get_data();
  93. for (int c = 0; c < 4; c++) {
  94. float f = data[c];
  95. int val = ((f * 255.0f) + 0.5f);
  96. sz += String(Variant(val)) + " ";
  97. }
  98. sz += "}";
  99. return sz;
  100. }
  101. RasterizerStorageGLES2::Texture *RasterizerCanvasGLES2::_get_canvas_texture(const RID &p_texture) const {
  102. if (p_texture.is_valid()) {
  103. RasterizerStorageGLES2::Texture *texture = storage->texture_owner.getornull(p_texture);
  104. if (texture) {
  105. return texture->get_ptr();
  106. }
  107. }
  108. return 0;
  109. }
  110. int RasterizerCanvasGLES2::_batch_find_or_create_tex(const RID &p_texture, const RID &p_normal, bool p_tile, int p_previous_match) {
  111. // optimization .. in 99% cases the last matched value will be the same, so no need to traverse the list
  112. if (p_previous_match > 0) // if it is zero, it will get hit first in the linear search anyway
  113. {
  114. const BatchTex &batch_texture = bdata.batch_textures[p_previous_match];
  115. // note for future reference, if RID implementation changes, this could become more expensive
  116. if ((batch_texture.RID_texture == p_texture) && (batch_texture.RID_normal == p_normal)) {
  117. // tiling mode must also match
  118. bool tiles = batch_texture.tile_mode != BatchTex::TILE_OFF;
  119. if (tiles == p_tile)
  120. // match!
  121. return p_previous_match;
  122. }
  123. }
  124. // not the previous match .. we will do a linear search ... slower, but should happen
  125. // not very often except with non-batchable runs, which are going to be slow anyway
  126. // n.b. could possibly be replaced later by a fast hash table
  127. for (int n = 0; n < bdata.batch_textures.size(); n++) {
  128. const BatchTex &batch_texture = bdata.batch_textures[n];
  129. if ((batch_texture.RID_texture == p_texture) && (batch_texture.RID_normal == p_normal)) {
  130. // tiling mode must also match
  131. bool tiles = batch_texture.tile_mode != BatchTex::TILE_OFF;
  132. if (tiles == p_tile)
  133. // match!
  134. return n;
  135. }
  136. }
  137. // pushing back from local variable .. not ideal but has to use a Vector because non pod
  138. // due to RIDs
  139. BatchTex new_batch_tex;
  140. new_batch_tex.RID_texture = p_texture;
  141. new_batch_tex.RID_normal = p_normal;
  142. // get the texture
  143. RasterizerStorageGLES2::Texture *texture = _get_canvas_texture(p_texture);
  144. if (texture) {
  145. new_batch_tex.tex_pixel_size.x = 1.0 / texture->width;
  146. new_batch_tex.tex_pixel_size.y = 1.0 / texture->height;
  147. new_batch_tex.flags = texture->flags;
  148. } else {
  149. // maybe doesn't need doing...
  150. new_batch_tex.tex_pixel_size.x = 1.0;
  151. new_batch_tex.tex_pixel_size.y = 1.0;
  152. new_batch_tex.flags = 0;
  153. }
  154. if (p_tile) {
  155. if (texture) {
  156. // default
  157. new_batch_tex.tile_mode = BatchTex::TILE_NORMAL;
  158. // no hardware support for non power of 2 tiling
  159. if (!storage->config.support_npot_repeat_mipmap) {
  160. if (next_power_of_2(texture->alloc_width) != (unsigned int)texture->alloc_width && next_power_of_2(texture->alloc_height) != (unsigned int)texture->alloc_height) {
  161. new_batch_tex.tile_mode = BatchTex::TILE_FORCE_REPEAT;
  162. }
  163. }
  164. } else {
  165. // this should not happen?
  166. new_batch_tex.tile_mode = BatchTex::TILE_OFF;
  167. }
  168. } else {
  169. new_batch_tex.tile_mode = BatchTex::TILE_OFF;
  170. }
  171. // push back
  172. bdata.batch_textures.push_back(new_batch_tex);
  173. return bdata.batch_textures.size() - 1;
  174. }
  175. void RasterizerCanvasGLES2::_batch_upload_buffers() {
  176. // noop?
  177. if (!bdata.vertices.size())
  178. return;
  179. glBindBuffer(GL_ARRAY_BUFFER, bdata.gl_vertex_buffer);
  180. // orphan the old (for now)
  181. glBufferData(GL_ARRAY_BUFFER, 0, 0, GL_DYNAMIC_DRAW);
  182. if (!bdata.use_colored_vertices) {
  183. glBufferData(GL_ARRAY_BUFFER, sizeof(BatchVertex) * bdata.vertices.size(), bdata.vertices.get_data(), GL_DYNAMIC_DRAW);
  184. } else {
  185. glBufferData(GL_ARRAY_BUFFER, sizeof(BatchVertexColored) * bdata.vertices_colored.size(), bdata.vertices_colored.get_data(), GL_DYNAMIC_DRAW);
  186. }
  187. // might not be necessary
  188. glBindBuffer(GL_ARRAY_BUFFER, 0);
  189. }
  190. RasterizerCanvasGLES2::Batch *RasterizerCanvasGLES2::_batch_request_new(bool p_blank) {
  191. Batch *batch = bdata.batches.request();
  192. if (!batch) {
  193. // grow the batches
  194. bdata.batches.grow();
  195. // and the temporary batches (used for color verts)
  196. bdata.batches_temp.reset();
  197. bdata.batches_temp.grow();
  198. // this should always succeed after growing
  199. batch = bdata.batches.request();
  200. #ifdef DEBUG_ENABLED
  201. CRASH_COND(!batch);
  202. #endif
  203. }
  204. if (p_blank)
  205. memset(batch, 0, sizeof(Batch));
  206. return batch;
  207. }
  208. // This function may be called MULTIPLE TIMES for each item, so needs to record how far it has got
  209. bool RasterizerCanvasGLES2::prefill_joined_item(FillState &r_fill_state, int &r_command_start, Item *p_item, Item *p_current_clip, bool &r_reclip, RasterizerStorageGLES2::Material *p_material) {
  210. // we will prefill batches and vertices ready for sending in one go to the vertex buffer
  211. int command_count = p_item->commands.size();
  212. Item::Command *const *commands = p_item->commands.ptr();
  213. // locals, might be more efficient in a register (check)
  214. Vector2 texpixel_size = r_fill_state.texpixel_size;
  215. const float uv_epsilon = bdata.settings_uv_contract_amount;
  216. // checking the color for not being white makes it 92/90 times faster in the case where it is white
  217. bool multiply_final_modulate = false;
  218. if (!r_fill_state.use_hardware_transform && (r_fill_state.final_modulate != Color(1, 1, 1, 1))) {
  219. multiply_final_modulate = true;
  220. }
  221. // start batch is a dummy batch (tex id -1) .. could be made more efficient
  222. if (!r_fill_state.curr_batch) {
  223. r_fill_state.curr_batch = _batch_request_new();
  224. r_fill_state.curr_batch->type = Batch::BT_DEFAULT;
  225. r_fill_state.curr_batch->first_command = r_command_start;
  226. // should tex_id be set to -1? check this
  227. }
  228. // we need to return which command we got up to, so
  229. // store this outside the loop
  230. int command_num;
  231. // do as many commands as possible until the vertex buffer will be full up
  232. for (command_num = r_command_start; command_num < command_count; command_num++) {
  233. Item::Command *command = commands[command_num];
  234. switch (command->type) {
  235. default: {
  236. _prefill_default_batch(r_fill_state, command_num, *p_item);
  237. } break;
  238. case Item::Command::TYPE_TRANSFORM: {
  239. // if the extra matrix has been sent already,
  240. // break this extra matrix software path (as we don't want to unset it on the GPU etc)
  241. if (r_fill_state.extra_matrix_sent) {
  242. _prefill_default_batch(r_fill_state, command_num, *p_item);
  243. } else {
  244. // Extra matrix fast path.
  245. // Instead of sending the command immediately, we store the modified transform (in combined)
  246. // for software transform, and only flush this transform command if we NEED to (i.e. we want to
  247. // render some default commands)
  248. Item::CommandTransform *transform = static_cast<Item::CommandTransform *>(command);
  249. const Transform2D &extra_matrix = transform->xform;
  250. if (r_fill_state.use_hardware_transform) {
  251. // if we are using hardware transform mode, we have already sent the final transform,
  252. // so we only want to software transform the extra matrix
  253. r_fill_state.transform_combined = extra_matrix;
  254. } else {
  255. r_fill_state.transform_combined = p_item->final_transform * extra_matrix;
  256. }
  257. // after a transform command, always use some form of software transform (either the combined final + extra, or just the extra)
  258. // until we flush this dirty extra matrix because we need to render default commands.
  259. r_fill_state.transform_mode = _find_transform_mode(r_fill_state.transform_combined);
  260. // make a note of which command the dirty extra matrix is store in, so we can send it later
  261. // if necessary
  262. r_fill_state.transform_extra_command_number_p1 = command_num + 1; // plus 1 so we can test against zero
  263. }
  264. } break;
  265. case Item::Command::TYPE_RECT: {
  266. Item::CommandRect *rect = static_cast<Item::CommandRect *>(command);
  267. bool change_batch = false;
  268. // conditions for creating a new batch
  269. if (r_fill_state.curr_batch->type != Batch::BT_RECT) {
  270. change_batch = true;
  271. // check for special case if there is only a single or small number of rects,
  272. // in which case we will use the legacy default rect renderer
  273. // because it is faster for single rects
  274. // we only want to do this if not a joined item with more than 1 item,
  275. // because joined items with more than 1, the command * will be incorrect
  276. // NOTE - this is assuming that use_hardware_transform means that it is a non-joined item!!
  277. // If that assumption is incorrect this will go horribly wrong.
  278. if (bdata.settings_use_single_rect_fallback && r_fill_state.use_hardware_transform) {
  279. bool is_single_rect = false;
  280. int command_num_next = command_num + 1;
  281. if (command_num_next < command_count) {
  282. Item::Command *command_next = commands[command_num_next];
  283. if ((command_next->type != Item::Command::TYPE_RECT) && (command_next->type != Item::Command::TYPE_TRANSFORM)) {
  284. is_single_rect = true;
  285. }
  286. } else {
  287. is_single_rect = true;
  288. }
  289. // if it is a rect on its own, do exactly the same as the default routine
  290. if (is_single_rect) {
  291. _prefill_default_batch(r_fill_state, command_num, *p_item);
  292. break;
  293. }
  294. } // if use hardware transform
  295. }
  296. Color col = rect->modulate;
  297. if (multiply_final_modulate) {
  298. col *= r_fill_state.final_modulate;
  299. }
  300. // instead of doing all the texture preparation for EVERY rect,
  301. // we build a list of texture combinations and do this once off.
  302. // This means we have a potentially rather slow step to identify which texture combo
  303. // using the RIDs.
  304. int old_batch_tex_id = r_fill_state.batch_tex_id;
  305. r_fill_state.batch_tex_id = _batch_find_or_create_tex(rect->texture, rect->normal_map, rect->flags & CANVAS_RECT_TILE, old_batch_tex_id);
  306. // try to create vertices BEFORE creating a batch,
  307. // because if the vertex buffer is full, we need to finish this
  308. // function, draw what we have so far, and then start a new set of batches
  309. // request FOUR vertices at a time, this is more efficient
  310. BatchVertex *bvs = bdata.vertices.request(4);
  311. if (!bvs) {
  312. // run out of space in the vertex buffer .. finish this function and draw what we have so far
  313. // return where we got to
  314. r_command_start = command_num;
  315. return true;
  316. }
  317. // conditions for creating a new batch
  318. if (old_batch_tex_id != r_fill_state.batch_tex_id) {
  319. change_batch = true;
  320. }
  321. // we need to treat color change separately because we need to count these
  322. // to decide whether to switch on the fly to colored vertices.
  323. if (!r_fill_state.curr_batch->color.equals(col)) {
  324. change_batch = true;
  325. bdata.total_color_changes++;
  326. }
  327. if (change_batch) {
  328. // put the tex pixel size in a local (less verbose and can be a register)
  329. const BatchTex &batchtex = bdata.batch_textures[r_fill_state.batch_tex_id];
  330. batchtex.tex_pixel_size.to(texpixel_size);
  331. if (bdata.settings_uv_contract) {
  332. r_fill_state.contract_uvs = (batchtex.flags & VS::TEXTURE_FLAG_FILTER) == 0;
  333. }
  334. // need to preserve texpixel_size between items
  335. r_fill_state.texpixel_size = texpixel_size;
  336. // open new batch (this should never fail, it dynamically grows)
  337. r_fill_state.curr_batch = _batch_request_new(false);
  338. r_fill_state.curr_batch->type = Batch::BT_RECT;
  339. r_fill_state.curr_batch->color.set(col);
  340. r_fill_state.curr_batch->batch_texture_id = r_fill_state.batch_tex_id;
  341. r_fill_state.curr_batch->first_command = command_num;
  342. r_fill_state.curr_batch->num_commands = 1;
  343. r_fill_state.curr_batch->first_quad = bdata.total_quads;
  344. } else {
  345. // we could alternatively do the count when closing a batch .. perhaps more efficient
  346. r_fill_state.curr_batch->num_commands++;
  347. }
  348. // fill the quad geometry
  349. Vector2 mins = rect->rect.position;
  350. if (r_fill_state.transform_mode == TM_TRANSLATE) {
  351. _software_transform_vertex(mins, r_fill_state.transform_combined);
  352. }
  353. Vector2 maxs = mins + rect->rect.size;
  354. // just aliases
  355. BatchVertex *bA = &bvs[0];
  356. BatchVertex *bB = &bvs[1];
  357. BatchVertex *bC = &bvs[2];
  358. BatchVertex *bD = &bvs[3];
  359. bA->pos.x = mins.x;
  360. bA->pos.y = mins.y;
  361. bB->pos.x = maxs.x;
  362. bB->pos.y = mins.y;
  363. bC->pos.x = maxs.x;
  364. bC->pos.y = maxs.y;
  365. bD->pos.x = mins.x;
  366. bD->pos.y = maxs.y;
  367. if (rect->rect.size.x < 0) {
  368. SWAP(bA->pos, bB->pos);
  369. SWAP(bC->pos, bD->pos);
  370. }
  371. if (rect->rect.size.y < 0) {
  372. SWAP(bA->pos, bD->pos);
  373. SWAP(bB->pos, bC->pos);
  374. }
  375. if (r_fill_state.transform_mode == TM_ALL) {
  376. _software_transform_vertex(bA->pos, r_fill_state.transform_combined);
  377. _software_transform_vertex(bB->pos, r_fill_state.transform_combined);
  378. _software_transform_vertex(bC->pos, r_fill_state.transform_combined);
  379. _software_transform_vertex(bD->pos, r_fill_state.transform_combined);
  380. }
  381. // uvs
  382. Vector2 src_min;
  383. Vector2 src_max;
  384. if (rect->flags & CANVAS_RECT_REGION) {
  385. src_min = rect->source.position;
  386. src_max = src_min + rect->source.size;
  387. src_min *= texpixel_size;
  388. src_max *= texpixel_size;
  389. // nudge offset for the maximum to prevent precision error on GPU reading into line outside the source rect
  390. // this is very difficult to get right.
  391. if (r_fill_state.contract_uvs) {
  392. src_min.x += uv_epsilon;
  393. src_min.y += uv_epsilon;
  394. src_max.x -= uv_epsilon;
  395. src_max.y -= uv_epsilon;
  396. }
  397. } else {
  398. src_min = Vector2(0, 0);
  399. src_max = Vector2(1, 1);
  400. }
  401. // 10% faster calculating the max first
  402. Vector2 uvs[4] = {
  403. src_min,
  404. Vector2(src_max.x, src_min.y),
  405. src_max,
  406. Vector2(src_min.x, src_max.y),
  407. };
  408. if (rect->flags & CANVAS_RECT_TRANSPOSE) {
  409. SWAP(uvs[1], uvs[3]);
  410. }
  411. if (rect->flags & CANVAS_RECT_FLIP_H) {
  412. SWAP(uvs[0], uvs[1]);
  413. SWAP(uvs[2], uvs[3]);
  414. }
  415. if (rect->flags & CANVAS_RECT_FLIP_V) {
  416. SWAP(uvs[0], uvs[3]);
  417. SWAP(uvs[1], uvs[2]);
  418. }
  419. bA->uv.set(uvs[0]);
  420. bB->uv.set(uvs[1]);
  421. bC->uv.set(uvs[2]);
  422. bD->uv.set(uvs[3]);
  423. // increment quad count
  424. bdata.total_quads++;
  425. } break;
  426. }
  427. }
  428. // VERY IMPORTANT to return where we got to, because this func may be called multiple
  429. // times per item.
  430. // Don't miss out on this step by calling return earlier in the function without setting r_command_start.
  431. r_command_start = command_num;
  432. return false;
  433. }
  434. // convert the stupidly high amount of batches (each with its own color)
  435. // to larger batches where the color is stored in the verts instead...
  436. // There is a trade off. Non colored verts are smaller so work faster, but
  437. // there comes a point where it is better to just use colored verts to avoid lots of
  438. // batches.
  439. void RasterizerCanvasGLES2::_batch_translate_to_colored() {
  440. bdata.vertices_colored.reset();
  441. bdata.batches_temp.reset();
  442. // As the vertices_colored and batches_temp are 'mirrors' of the non-colored version,
  443. // the sizes should be equal, and allocations should never fail. Hence the use of debug
  444. // asserts to check program flow, these should not occur at runtime unless the allocation
  445. // code has been altered.
  446. #ifdef DEBUG_ENABLED
  447. CRASH_COND(bdata.vertices_colored.max_size() != bdata.vertices.max_size());
  448. CRASH_COND(bdata.batches_temp.max_size() != bdata.batches.max_size());
  449. #endif
  450. Color curr_col(-1.0, -1.0, -1.0, -1.0);
  451. Batch *dest_batch = 0;
  452. // translate the batches into vertex colored batches
  453. for (int n = 0; n < bdata.batches.size(); n++) {
  454. const Batch &source_batch = bdata.batches[n];
  455. bool needs_new_batch = true;
  456. if (dest_batch) {
  457. if (dest_batch->type == source_batch.type) {
  458. if (source_batch.type == Batch::BT_RECT) {
  459. if (dest_batch->batch_texture_id == source_batch.batch_texture_id) {
  460. // add to previous batch
  461. dest_batch->num_commands += source_batch.num_commands;
  462. needs_new_batch = false;
  463. // create the colored verts (only if not default)
  464. int first_vert = source_batch.first_quad * 4;
  465. int end_vert = 4 * (source_batch.first_quad + source_batch.num_commands);
  466. for (int v = first_vert; v < end_vert; v++) {
  467. const BatchVertex &bv = bdata.vertices[v];
  468. BatchVertexColored *cv = bdata.vertices_colored.request();
  469. #ifdef DEBUG_ENABLED
  470. CRASH_COND(!cv);
  471. #endif
  472. cv->pos = bv.pos;
  473. cv->uv = bv.uv;
  474. cv->col = source_batch.color;
  475. }
  476. } // textures match
  477. } else {
  478. // default
  479. // we can still join, but only under special circumstances
  480. // does this ever happen? not sure at this stage, but left for future expansion
  481. uint32_t source_last_command = source_batch.first_command + source_batch.num_commands;
  482. if (source_last_command == dest_batch->first_command) {
  483. dest_batch->num_commands += source_batch.num_commands;
  484. needs_new_batch = false;
  485. } // if the commands line up exactly
  486. }
  487. } // if both batches are the same type
  488. } // if dest batch is valid
  489. if (needs_new_batch) {
  490. dest_batch = bdata.batches_temp.request();
  491. #ifdef DEBUG_ENABLED
  492. CRASH_COND(!dest_batch);
  493. #endif
  494. *dest_batch = source_batch;
  495. // create the colored verts (only if not default)
  496. if (source_batch.type != Batch::BT_DEFAULT) {
  497. int first_vert = source_batch.first_quad * 4;
  498. int end_vert = 4 * (source_batch.first_quad + source_batch.num_commands);
  499. for (int v = first_vert; v < end_vert; v++) {
  500. const BatchVertex &bv = bdata.vertices[v];
  501. BatchVertexColored *cv = bdata.vertices_colored.request();
  502. #ifdef DEBUG_ENABLED
  503. CRASH_COND(!cv);
  504. #endif
  505. cv->pos = bv.pos;
  506. cv->uv = bv.uv;
  507. cv->col = source_batch.color;
  508. }
  509. }
  510. }
  511. }
  512. // copy the temporary batches to the master batch list (this could be avoided but it makes the code cleaner)
  513. bdata.batches.copy_from(bdata.batches_temp);
  514. }
  515. void RasterizerCanvasGLES2::_batch_render_rects(const Batch &p_batch, RasterizerStorageGLES2::Material *p_material) {
  516. ERR_FAIL_COND(p_batch.num_commands <= 0);
  517. const bool &colored_verts = bdata.use_colored_vertices;
  518. int sizeof_vert;
  519. if (!colored_verts) {
  520. sizeof_vert = sizeof(BatchVertex);
  521. } else {
  522. sizeof_vert = sizeof(BatchVertexColored);
  523. }
  524. // batch tex
  525. const BatchTex &tex = bdata.batch_textures[p_batch.batch_texture_id];
  526. // make sure to set all conditionals BEFORE binding the shader
  527. state.canvas_shader.set_conditional(CanvasShaderGLES2::USE_TEXTURE_RECT, false);
  528. // force repeat is set if non power of 2 texture, and repeat is needed if hardware doesn't support npot
  529. if (tex.tile_mode == BatchTex::TILE_FORCE_REPEAT) {
  530. state.canvas_shader.set_conditional(CanvasShaderGLES2::USE_FORCE_REPEAT, true);
  531. }
  532. if (state.canvas_shader.bind()) {
  533. _set_uniforms();
  534. state.canvas_shader.use_material((void *)p_material);
  535. }
  536. _bind_canvas_texture(tex.RID_texture, tex.RID_normal);
  537. // bind the index and vertex buffer
  538. glBindBuffer(GL_ARRAY_BUFFER, bdata.gl_vertex_buffer);
  539. glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, bdata.gl_index_buffer);
  540. uint64_t pointer = 0;
  541. glVertexAttribPointer(VS::ARRAY_VERTEX, 2, GL_FLOAT, GL_FALSE, sizeof_vert, (const void *)pointer);
  542. // always send UVs, even within a texture specified because a shader can still use UVs
  543. glVertexAttribPointer(VS::ARRAY_TEX_UV, 2, GL_FLOAT, GL_FALSE, sizeof_vert, CAST_INT_TO_UCHAR_PTR(pointer + (2 * 4)));
  544. glEnableVertexAttribArray(VS::ARRAY_TEX_UV);
  545. // color
  546. if (!colored_verts) {
  547. glDisableVertexAttribArray(VS::ARRAY_COLOR);
  548. glVertexAttrib4fv(VS::ARRAY_COLOR, p_batch.color.get_data());
  549. } else {
  550. glVertexAttribPointer(VS::ARRAY_COLOR, 4, GL_FLOAT, GL_FALSE, sizeof_vert, CAST_INT_TO_UCHAR_PTR(pointer + (4 * 4)));
  551. glEnableVertexAttribArray(VS::ARRAY_COLOR);
  552. }
  553. // We only want to set the GL wrapping mode if the texture is not already tiled (i.e. set in Import).
  554. // This is an optimization left over from the legacy renderer.
  555. // If we DID set tiling in the API, and reverted to clamped, then the next draw using this texture
  556. // may use clamped mode incorrectly.
  557. bool tex_is_already_tiled = tex.flags & VS::TEXTURE_FLAG_REPEAT;
  558. if (tex.tile_mode == BatchTex::TILE_NORMAL) {
  559. // if the texture is imported as tiled, no need to set GL state, as it will already be bound with repeat
  560. if (!tex_is_already_tiled) {
  561. glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
  562. glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
  563. }
  564. }
  565. // we need to convert explicitly from pod Vec2 to Vector2 ...
  566. // could use a cast but this might be unsafe in future
  567. Vector2 tps;
  568. tex.tex_pixel_size.to(tps);
  569. state.canvas_shader.set_uniform(CanvasShaderGLES2::COLOR_TEXPIXEL_SIZE, tps);
  570. int64_t offset = p_batch.first_quad * 6 * 2; // 6 inds per quad at 2 bytes each
  571. int num_elements = p_batch.num_commands * 6;
  572. glDrawElements(GL_TRIANGLES, num_elements, GL_UNSIGNED_SHORT, (void *)offset);
  573. storage->info.render._2d_draw_call_count++;
  574. switch (tex.tile_mode) {
  575. case BatchTex::TILE_FORCE_REPEAT: {
  576. state.canvas_shader.set_conditional(CanvasShaderGLES2::USE_FORCE_REPEAT, false);
  577. } break;
  578. case BatchTex::TILE_NORMAL: {
  579. // if the texture is imported as tiled, no need to revert GL state
  580. if (!tex_is_already_tiled) {
  581. glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
  582. glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
  583. }
  584. } break;
  585. default: {
  586. } break;
  587. }
  588. glDisableVertexAttribArray(VS::ARRAY_TEX_UV);
  589. glDisableVertexAttribArray(VS::ARRAY_COLOR);
  590. // may not be necessary .. state change optimization still TODO
  591. glBindBuffer(GL_ARRAY_BUFFER, 0);
  592. glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0);
  593. }
  594. #ifdef DEBUG_ENABLED
  595. String RasterizerCanvasGLES2::get_command_type_string(const Item::Command &p_command) const {
  596. String sz = "";
  597. switch (p_command.type) {
  598. default:
  599. break;
  600. case Item::Command::TYPE_LINE: {
  601. sz = "l";
  602. } break;
  603. case Item::Command::TYPE_POLYLINE: {
  604. sz = "PL";
  605. } break;
  606. case Item::Command::TYPE_RECT: {
  607. sz = "r";
  608. } break;
  609. case Item::Command::TYPE_NINEPATCH: {
  610. sz = "n";
  611. } break;
  612. case Item::Command::TYPE_PRIMITIVE: {
  613. sz = "PR";
  614. } break;
  615. case Item::Command::TYPE_POLYGON: {
  616. sz = "p";
  617. } break;
  618. case Item::Command::TYPE_MESH: {
  619. sz = "m";
  620. } break;
  621. case Item::Command::TYPE_MULTIMESH: {
  622. sz = "MM";
  623. } break;
  624. case Item::Command::TYPE_PARTICLES: {
  625. sz = "PA";
  626. } break;
  627. case Item::Command::TYPE_CIRCLE: {
  628. sz = "c";
  629. } break;
  630. case Item::Command::TYPE_TRANSFORM: {
  631. sz = "t";
  632. // add a bit more info in debug build
  633. const Item::CommandTransform *transform = static_cast<const Item::CommandTransform *>(&p_command);
  634. const Transform2D &mat = transform->xform;
  635. sz += " ";
  636. sz += String(Variant(mat.elements[2]));
  637. sz += " ";
  638. } break;
  639. case Item::Command::TYPE_CLIP_IGNORE: {
  640. sz = "CI";
  641. } break;
  642. } // switch
  643. return sz;
  644. }
  645. void RasterizerCanvasGLES2::diagnose_batches(Item::Command *const *p_commands) {
  646. int num_batches = bdata.batches.size();
  647. BatchColor curr_color;
  648. curr_color.set(Color(-1, -1, -1, -1));
  649. bool first_color_change = true;
  650. for (int batch_num = 0; batch_num < num_batches; batch_num++) {
  651. const Batch &batch = bdata.batches[batch_num];
  652. bdata.frame_string += "\t\t\tbatch ";
  653. switch (batch.type) {
  654. case Batch::BT_RECT: {
  655. bdata.frame_string += "R ";
  656. bdata.frame_string += itos(batch.first_command) + "-";
  657. bdata.frame_string += itos(batch.num_commands);
  658. int tex_id = (int)bdata.batch_textures[batch.batch_texture_id].RID_texture.get_id();
  659. bdata.frame_string += " [" + itos(batch.batch_texture_id) + " - " + itos(tex_id) + "]";
  660. bdata.frame_string += " " + batch.color.to_string();
  661. if (batch.num_commands > 1) {
  662. bdata.frame_string += " MULTI";
  663. }
  664. if (curr_color != batch.color) {
  665. curr_color = batch.color;
  666. if (!first_color_change) {
  667. bdata.frame_string += " color";
  668. } else {
  669. first_color_change = false;
  670. }
  671. }
  672. bdata.frame_string += "\n";
  673. } break;
  674. default: {
  675. bdata.frame_string += "D ";
  676. bdata.frame_string += itos(batch.first_command) + "-";
  677. bdata.frame_string += itos(batch.num_commands) + " ";
  678. int num_show = MIN(batch.num_commands, 16);
  679. for (int n = 0; n < num_show; n++) {
  680. const Item::Command &comm = *p_commands[batch.first_command + n];
  681. bdata.frame_string += get_command_type_string(comm) + " ";
  682. }
  683. bdata.frame_string += "\n";
  684. } break;
  685. }
  686. }
  687. }
  688. #endif
  689. void RasterizerCanvasGLES2::render_batches(Item::Command *const *p_commands, Item *p_current_clip, bool &r_reclip, RasterizerStorageGLES2::Material *p_material) {
  690. int num_batches = bdata.batches.size();
  691. for (int batch_num = 0; batch_num < num_batches; batch_num++) {
  692. const Batch &batch = bdata.batches[batch_num];
  693. switch (batch.type) {
  694. case Batch::BT_RECT: {
  695. _batch_render_rects(batch, p_material);
  696. } break;
  697. default: {
  698. int end_command = batch.first_command + batch.num_commands;
  699. for (int i = batch.first_command; i < end_command; i++) {
  700. Item::Command *command = p_commands[i];
  701. switch (command->type) {
  702. case Item::Command::TYPE_LINE: {
  703. Item::CommandLine *line = static_cast<Item::CommandLine *>(command);
  704. state.canvas_shader.set_conditional(CanvasShaderGLES2::USE_TEXTURE_RECT, false);
  705. if (state.canvas_shader.bind()) {
  706. _set_uniforms();
  707. state.canvas_shader.use_material((void *)p_material);
  708. }
  709. _bind_canvas_texture(RID(), RID());
  710. glDisableVertexAttribArray(VS::ARRAY_COLOR);
  711. glVertexAttrib4fv(VS::ARRAY_COLOR, line->color.components);
  712. state.canvas_shader.set_uniform(CanvasShaderGLES2::MODELVIEW_MATRIX, state.uniforms.modelview_matrix);
  713. if (line->width <= 1) {
  714. Vector2 verts[2] = {
  715. Vector2(line->from.x, line->from.y),
  716. Vector2(line->to.x, line->to.y)
  717. };
  718. #ifdef GLES_OVER_GL
  719. if (line->antialiased)
  720. glEnable(GL_LINE_SMOOTH);
  721. #endif
  722. _draw_gui_primitive(2, verts, NULL, NULL);
  723. #ifdef GLES_OVER_GL
  724. if (line->antialiased)
  725. glDisable(GL_LINE_SMOOTH);
  726. #endif
  727. } else {
  728. Vector2 t = (line->from - line->to).normalized().tangent() * line->width * 0.5;
  729. Vector2 verts[4] = {
  730. line->from - t,
  731. line->from + t,
  732. line->to + t,
  733. line->to - t
  734. };
  735. _draw_gui_primitive(4, verts, NULL, NULL);
  736. #ifdef GLES_OVER_GL
  737. if (line->antialiased) {
  738. glEnable(GL_LINE_SMOOTH);
  739. for (int j = 0; j < 4; j++) {
  740. Vector2 vertsl[2] = {
  741. verts[j],
  742. verts[(j + 1) % 4],
  743. };
  744. _draw_gui_primitive(2, vertsl, NULL, NULL);
  745. }
  746. glDisable(GL_LINE_SMOOTH);
  747. }
  748. #endif
  749. }
  750. } break;
  751. case Item::Command::TYPE_RECT: {
  752. Item::CommandRect *r = static_cast<Item::CommandRect *>(command);
  753. glDisableVertexAttribArray(VS::ARRAY_COLOR);
  754. glVertexAttrib4fv(VS::ARRAY_COLOR, r->modulate.components);
  755. bool can_tile = true;
  756. if (r->texture.is_valid() && r->flags & CANVAS_RECT_TILE && !storage->config.support_npot_repeat_mipmap) {
  757. // workaround for when setting tiling does not work due to hardware limitation
  758. RasterizerStorageGLES2::Texture *texture = storage->texture_owner.getornull(r->texture);
  759. if (texture) {
  760. texture = texture->get_ptr();
  761. if (next_power_of_2(texture->alloc_width) != (unsigned int)texture->alloc_width && next_power_of_2(texture->alloc_height) != (unsigned int)texture->alloc_height) {
  762. state.canvas_shader.set_conditional(CanvasShaderGLES2::USE_FORCE_REPEAT, true);
  763. can_tile = false;
  764. }
  765. }
  766. }
  767. // On some widespread Nvidia cards, the normal draw method can produce some
  768. // flickering in draw_rect and especially TileMap rendering (tiles randomly flicker).
  769. // See GH-9913.
  770. // To work it around, we use a simpler draw method which does not flicker, but gives
  771. // a non negligible performance hit, so it's opt-in (GH-24466).
  772. if (use_nvidia_rect_workaround) {
  773. state.canvas_shader.set_conditional(CanvasShaderGLES2::USE_TEXTURE_RECT, false);
  774. if (state.canvas_shader.bind()) {
  775. _set_uniforms();
  776. state.canvas_shader.use_material((void *)p_material);
  777. }
  778. Vector2 points[4] = {
  779. r->rect.position,
  780. r->rect.position + Vector2(r->rect.size.x, 0.0),
  781. r->rect.position + r->rect.size,
  782. r->rect.position + Vector2(0.0, r->rect.size.y),
  783. };
  784. if (r->rect.size.x < 0) {
  785. SWAP(points[0], points[1]);
  786. SWAP(points[2], points[3]);
  787. }
  788. if (r->rect.size.y < 0) {
  789. SWAP(points[0], points[3]);
  790. SWAP(points[1], points[2]);
  791. }
  792. RasterizerStorageGLES2::Texture *texture = _bind_canvas_texture(r->texture, r->normal_map);
  793. if (texture) {
  794. Size2 texpixel_size(1.0 / texture->width, 1.0 / texture->height);
  795. Rect2 src_rect = (r->flags & CANVAS_RECT_REGION) ? Rect2(r->source.position * texpixel_size, r->source.size * texpixel_size) : Rect2(0, 0, 1, 1);
  796. Vector2 uvs[4] = {
  797. src_rect.position,
  798. src_rect.position + Vector2(src_rect.size.x, 0.0),
  799. src_rect.position + src_rect.size,
  800. src_rect.position + Vector2(0.0, src_rect.size.y),
  801. };
  802. if (r->flags & CANVAS_RECT_TRANSPOSE) {
  803. SWAP(uvs[1], uvs[3]);
  804. }
  805. if (r->flags & CANVAS_RECT_FLIP_H) {
  806. SWAP(uvs[0], uvs[1]);
  807. SWAP(uvs[2], uvs[3]);
  808. }
  809. if (r->flags & CANVAS_RECT_FLIP_V) {
  810. SWAP(uvs[0], uvs[3]);
  811. SWAP(uvs[1], uvs[2]);
  812. }
  813. state.canvas_shader.set_uniform(CanvasShaderGLES2::COLOR_TEXPIXEL_SIZE, texpixel_size);
  814. bool untile = false;
  815. if (can_tile && r->flags & CANVAS_RECT_TILE && !(texture->flags & VS::TEXTURE_FLAG_REPEAT)) {
  816. glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
  817. glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
  818. untile = true;
  819. }
  820. _draw_gui_primitive(4, points, NULL, uvs);
  821. if (untile) {
  822. glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
  823. glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
  824. }
  825. } else {
  826. static const Vector2 uvs[4] = {
  827. Vector2(0.0, 0.0),
  828. Vector2(0.0, 1.0),
  829. Vector2(1.0, 1.0),
  830. Vector2(1.0, 0.0),
  831. };
  832. state.canvas_shader.set_uniform(CanvasShaderGLES2::COLOR_TEXPIXEL_SIZE, Vector2());
  833. _draw_gui_primitive(4, points, NULL, uvs);
  834. }
  835. } else {
  836. // This branch is better for performance, but can produce flicker on Nvidia, see above comment.
  837. _bind_quad_buffer();
  838. state.canvas_shader.set_conditional(CanvasShaderGLES2::USE_TEXTURE_RECT, true);
  839. if (state.canvas_shader.bind()) {
  840. _set_uniforms();
  841. state.canvas_shader.use_material((void *)p_material);
  842. }
  843. RasterizerStorageGLES2::Texture *tex = _bind_canvas_texture(r->texture, r->normal_map);
  844. if (!tex) {
  845. Rect2 dst_rect = Rect2(r->rect.position, r->rect.size);
  846. if (dst_rect.size.width < 0) {
  847. dst_rect.position.x += dst_rect.size.width;
  848. dst_rect.size.width *= -1;
  849. }
  850. if (dst_rect.size.height < 0) {
  851. dst_rect.position.y += dst_rect.size.height;
  852. dst_rect.size.height *= -1;
  853. }
  854. state.canvas_shader.set_uniform(CanvasShaderGLES2::DST_RECT, Color(dst_rect.position.x, dst_rect.position.y, dst_rect.size.x, dst_rect.size.y));
  855. state.canvas_shader.set_uniform(CanvasShaderGLES2::SRC_RECT, Color(0, 0, 1, 1));
  856. glDrawArrays(GL_TRIANGLE_FAN, 0, 4);
  857. storage->info.render._2d_draw_call_count++;
  858. } else {
  859. bool untile = false;
  860. if (can_tile && r->flags & CANVAS_RECT_TILE && !(tex->flags & VS::TEXTURE_FLAG_REPEAT)) {
  861. glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
  862. glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
  863. untile = true;
  864. }
  865. Size2 texpixel_size(1.0 / tex->width, 1.0 / tex->height);
  866. Rect2 src_rect = (r->flags & CANVAS_RECT_REGION) ? Rect2(r->source.position * texpixel_size, r->source.size * texpixel_size) : Rect2(0, 0, 1, 1);
  867. Rect2 dst_rect = Rect2(r->rect.position, r->rect.size);
  868. if (dst_rect.size.width < 0) {
  869. dst_rect.position.x += dst_rect.size.width;
  870. dst_rect.size.width *= -1;
  871. }
  872. if (dst_rect.size.height < 0) {
  873. dst_rect.position.y += dst_rect.size.height;
  874. dst_rect.size.height *= -1;
  875. }
  876. if (r->flags & CANVAS_RECT_FLIP_H) {
  877. src_rect.size.x *= -1;
  878. }
  879. if (r->flags & CANVAS_RECT_FLIP_V) {
  880. src_rect.size.y *= -1;
  881. }
  882. if (r->flags & CANVAS_RECT_TRANSPOSE) {
  883. dst_rect.size.x *= -1; // Encoding in the dst_rect.z uniform
  884. }
  885. state.canvas_shader.set_uniform(CanvasShaderGLES2::COLOR_TEXPIXEL_SIZE, texpixel_size);
  886. state.canvas_shader.set_uniform(CanvasShaderGLES2::DST_RECT, Color(dst_rect.position.x, dst_rect.position.y, dst_rect.size.x, dst_rect.size.y));
  887. state.canvas_shader.set_uniform(CanvasShaderGLES2::SRC_RECT, Color(src_rect.position.x, src_rect.position.y, src_rect.size.x, src_rect.size.y));
  888. glDrawArrays(GL_TRIANGLE_FAN, 0, 4);
  889. storage->info.render._2d_draw_call_count++;
  890. if (untile) {
  891. glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
  892. glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
  893. }
  894. }
  895. glBindBuffer(GL_ARRAY_BUFFER, 0);
  896. glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0);
  897. }
  898. state.canvas_shader.set_conditional(CanvasShaderGLES2::USE_FORCE_REPEAT, false);
  899. } break;
  900. case Item::Command::TYPE_NINEPATCH: {
  901. Item::CommandNinePatch *np = static_cast<Item::CommandNinePatch *>(command);
  902. state.canvas_shader.set_conditional(CanvasShaderGLES2::USE_TEXTURE_RECT, false);
  903. if (state.canvas_shader.bind()) {
  904. _set_uniforms();
  905. state.canvas_shader.use_material((void *)p_material);
  906. }
  907. glDisableVertexAttribArray(VS::ARRAY_COLOR);
  908. glVertexAttrib4fv(VS::ARRAY_COLOR, np->color.components);
  909. RasterizerStorageGLES2::Texture *tex = _bind_canvas_texture(np->texture, np->normal_map);
  910. if (!tex) {
  911. // FIXME: Handle textureless ninepatch gracefully
  912. WARN_PRINT("NinePatch without texture not supported yet in GLES2 backend, skipping.");
  913. continue;
  914. }
  915. if (tex->width == 0 || tex->height == 0) {
  916. WARN_PRINT("Cannot set empty texture to NinePatch.");
  917. continue;
  918. }
  919. Size2 texpixel_size(1.0 / tex->width, 1.0 / tex->height);
  920. // state.canvas_shader.set_uniform(CanvasShaderGLES2::MODELVIEW_MATRIX, state.uniforms.modelview_matrix);
  921. state.canvas_shader.set_uniform(CanvasShaderGLES2::COLOR_TEXPIXEL_SIZE, texpixel_size);
  922. Rect2 source = np->source;
  923. if (source.size.x == 0 && source.size.y == 0) {
  924. source.size.x = tex->width;
  925. source.size.y = tex->height;
  926. }
  927. float screen_scale = 1.0;
  928. if (source.size.x != 0 && source.size.y != 0) {
  929. screen_scale = MIN(np->rect.size.x / source.size.x, np->rect.size.y / source.size.y);
  930. screen_scale = MIN(1.0, screen_scale);
  931. }
  932. // prepare vertex buffer
  933. // this buffer contains [ POS POS UV UV ] *
  934. float buffer[16 * 2 + 16 * 2];
  935. {
  936. // first row
  937. buffer[(0 * 4 * 4) + 0] = np->rect.position.x;
  938. buffer[(0 * 4 * 4) + 1] = np->rect.position.y;
  939. buffer[(0 * 4 * 4) + 2] = source.position.x * texpixel_size.x;
  940. buffer[(0 * 4 * 4) + 3] = source.position.y * texpixel_size.y;
  941. buffer[(0 * 4 * 4) + 4] = np->rect.position.x + np->margin[MARGIN_LEFT] * screen_scale;
  942. buffer[(0 * 4 * 4) + 5] = np->rect.position.y;
  943. buffer[(0 * 4 * 4) + 6] = (source.position.x + np->margin[MARGIN_LEFT]) * texpixel_size.x;
  944. buffer[(0 * 4 * 4) + 7] = source.position.y * texpixel_size.y;
  945. buffer[(0 * 4 * 4) + 8] = np->rect.position.x + np->rect.size.x - np->margin[MARGIN_RIGHT] * screen_scale;
  946. buffer[(0 * 4 * 4) + 9] = np->rect.position.y;
  947. buffer[(0 * 4 * 4) + 10] = (source.position.x + source.size.x - np->margin[MARGIN_RIGHT]) * texpixel_size.x;
  948. buffer[(0 * 4 * 4) + 11] = source.position.y * texpixel_size.y;
  949. buffer[(0 * 4 * 4) + 12] = np->rect.position.x + np->rect.size.x;
  950. buffer[(0 * 4 * 4) + 13] = np->rect.position.y;
  951. buffer[(0 * 4 * 4) + 14] = (source.position.x + source.size.x) * texpixel_size.x;
  952. buffer[(0 * 4 * 4) + 15] = source.position.y * texpixel_size.y;
  953. // second row
  954. buffer[(1 * 4 * 4) + 0] = np->rect.position.x;
  955. buffer[(1 * 4 * 4) + 1] = np->rect.position.y + np->margin[MARGIN_TOP] * screen_scale;
  956. buffer[(1 * 4 * 4) + 2] = source.position.x * texpixel_size.x;
  957. buffer[(1 * 4 * 4) + 3] = (source.position.y + np->margin[MARGIN_TOP]) * texpixel_size.y;
  958. buffer[(1 * 4 * 4) + 4] = np->rect.position.x + np->margin[MARGIN_LEFT] * screen_scale;
  959. buffer[(1 * 4 * 4) + 5] = np->rect.position.y + np->margin[MARGIN_TOP] * screen_scale;
  960. buffer[(1 * 4 * 4) + 6] = (source.position.x + np->margin[MARGIN_LEFT]) * texpixel_size.x;
  961. buffer[(1 * 4 * 4) + 7] = (source.position.y + np->margin[MARGIN_TOP]) * texpixel_size.y;
  962. buffer[(1 * 4 * 4) + 8] = np->rect.position.x + np->rect.size.x - np->margin[MARGIN_RIGHT] * screen_scale;
  963. buffer[(1 * 4 * 4) + 9] = np->rect.position.y + np->margin[MARGIN_TOP] * screen_scale;
  964. buffer[(1 * 4 * 4) + 10] = (source.position.x + source.size.x - np->margin[MARGIN_RIGHT]) * texpixel_size.x;
  965. buffer[(1 * 4 * 4) + 11] = (source.position.y + np->margin[MARGIN_TOP]) * texpixel_size.y;
  966. buffer[(1 * 4 * 4) + 12] = np->rect.position.x + np->rect.size.x;
  967. buffer[(1 * 4 * 4) + 13] = np->rect.position.y + np->margin[MARGIN_TOP] * screen_scale;
  968. buffer[(1 * 4 * 4) + 14] = (source.position.x + source.size.x) * texpixel_size.x;
  969. buffer[(1 * 4 * 4) + 15] = (source.position.y + np->margin[MARGIN_TOP]) * texpixel_size.y;
  970. // third row
  971. buffer[(2 * 4 * 4) + 0] = np->rect.position.x;
  972. buffer[(2 * 4 * 4) + 1] = np->rect.position.y + np->rect.size.y - np->margin[MARGIN_BOTTOM] * screen_scale;
  973. buffer[(2 * 4 * 4) + 2] = source.position.x * texpixel_size.x;
  974. buffer[(2 * 4 * 4) + 3] = (source.position.y + source.size.y - np->margin[MARGIN_BOTTOM]) * texpixel_size.y;
  975. buffer[(2 * 4 * 4) + 4] = np->rect.position.x + np->margin[MARGIN_LEFT] * screen_scale;
  976. buffer[(2 * 4 * 4) + 5] = np->rect.position.y + np->rect.size.y - np->margin[MARGIN_BOTTOM] * screen_scale;
  977. buffer[(2 * 4 * 4) + 6] = (source.position.x + np->margin[MARGIN_LEFT]) * texpixel_size.x;
  978. buffer[(2 * 4 * 4) + 7] = (source.position.y + source.size.y - np->margin[MARGIN_BOTTOM]) * texpixel_size.y;
  979. buffer[(2 * 4 * 4) + 8] = np->rect.position.x + np->rect.size.x - np->margin[MARGIN_RIGHT] * screen_scale;
  980. buffer[(2 * 4 * 4) + 9] = np->rect.position.y + np->rect.size.y - np->margin[MARGIN_BOTTOM] * screen_scale;
  981. buffer[(2 * 4 * 4) + 10] = (source.position.x + source.size.x - np->margin[MARGIN_RIGHT]) * texpixel_size.x;
  982. buffer[(2 * 4 * 4) + 11] = (source.position.y + source.size.y - np->margin[MARGIN_BOTTOM]) * texpixel_size.y;
  983. buffer[(2 * 4 * 4) + 12] = np->rect.position.x + np->rect.size.x;
  984. buffer[(2 * 4 * 4) + 13] = np->rect.position.y + np->rect.size.y - np->margin[MARGIN_BOTTOM] * screen_scale;
  985. buffer[(2 * 4 * 4) + 14] = (source.position.x + source.size.x) * texpixel_size.x;
  986. buffer[(2 * 4 * 4) + 15] = (source.position.y + source.size.y - np->margin[MARGIN_BOTTOM]) * texpixel_size.y;
  987. // fourth row
  988. buffer[(3 * 4 * 4) + 0] = np->rect.position.x;
  989. buffer[(3 * 4 * 4) + 1] = np->rect.position.y + np->rect.size.y;
  990. buffer[(3 * 4 * 4) + 2] = source.position.x * texpixel_size.x;
  991. buffer[(3 * 4 * 4) + 3] = (source.position.y + source.size.y) * texpixel_size.y;
  992. buffer[(3 * 4 * 4) + 4] = np->rect.position.x + np->margin[MARGIN_LEFT] * screen_scale;
  993. buffer[(3 * 4 * 4) + 5] = np->rect.position.y + np->rect.size.y;
  994. buffer[(3 * 4 * 4) + 6] = (source.position.x + np->margin[MARGIN_LEFT]) * texpixel_size.x;
  995. buffer[(3 * 4 * 4) + 7] = (source.position.y + source.size.y) * texpixel_size.y;
  996. buffer[(3 * 4 * 4) + 8] = np->rect.position.x + np->rect.size.x - np->margin[MARGIN_RIGHT] * screen_scale;
  997. buffer[(3 * 4 * 4) + 9] = np->rect.position.y + np->rect.size.y;
  998. buffer[(3 * 4 * 4) + 10] = (source.position.x + source.size.x - np->margin[MARGIN_RIGHT]) * texpixel_size.x;
  999. buffer[(3 * 4 * 4) + 11] = (source.position.y + source.size.y) * texpixel_size.y;
  1000. buffer[(3 * 4 * 4) + 12] = np->rect.position.x + np->rect.size.x;
  1001. buffer[(3 * 4 * 4) + 13] = np->rect.position.y + np->rect.size.y;
  1002. buffer[(3 * 4 * 4) + 14] = (source.position.x + source.size.x) * texpixel_size.x;
  1003. buffer[(3 * 4 * 4) + 15] = (source.position.y + source.size.y) * texpixel_size.y;
  1004. }
  1005. glBindBuffer(GL_ARRAY_BUFFER, data.ninepatch_vertices);
  1006. glBufferData(GL_ARRAY_BUFFER, sizeof(float) * (16 + 16) * 2, buffer, GL_DYNAMIC_DRAW);
  1007. glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, data.ninepatch_elements);
  1008. glEnableVertexAttribArray(VS::ARRAY_VERTEX);
  1009. glEnableVertexAttribArray(VS::ARRAY_TEX_UV);
  1010. glVertexAttribPointer(VS::ARRAY_VERTEX, 2, GL_FLOAT, GL_FALSE, 4 * sizeof(float), NULL);
  1011. glVertexAttribPointer(VS::ARRAY_TEX_UV, 2, GL_FLOAT, GL_FALSE, 4 * sizeof(float), CAST_INT_TO_UCHAR_PTR((sizeof(float) * 2)));
  1012. glDrawElements(GL_TRIANGLES, 18 * 3 - (np->draw_center ? 0 : 6), GL_UNSIGNED_BYTE, NULL);
  1013. glBindBuffer(GL_ARRAY_BUFFER, 0);
  1014. glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0);
  1015. storage->info.render._2d_draw_call_count++;
  1016. } break;
  1017. case Item::Command::TYPE_CIRCLE: {
  1018. Item::CommandCircle *circle = static_cast<Item::CommandCircle *>(command);
  1019. state.canvas_shader.set_conditional(CanvasShaderGLES2::USE_TEXTURE_RECT, false);
  1020. if (state.canvas_shader.bind()) {
  1021. _set_uniforms();
  1022. state.canvas_shader.use_material((void *)p_material);
  1023. }
  1024. static const int num_points = 32;
  1025. Vector2 points[num_points + 1];
  1026. points[num_points] = circle->pos;
  1027. int indices[num_points * 3];
  1028. for (int j = 0; j < num_points; j++) {
  1029. points[j] = circle->pos + Vector2(Math::sin(j * Math_PI * 2.0 / num_points), Math::cos(j * Math_PI * 2.0 / num_points)) * circle->radius;
  1030. indices[j * 3 + 0] = j;
  1031. indices[j * 3 + 1] = (j + 1) % num_points;
  1032. indices[j * 3 + 2] = num_points;
  1033. }
  1034. _bind_canvas_texture(RID(), RID());
  1035. _draw_polygon(indices, num_points * 3, num_points + 1, points, NULL, &circle->color, true);
  1036. } break;
  1037. case Item::Command::TYPE_POLYGON: {
  1038. Item::CommandPolygon *polygon = static_cast<Item::CommandPolygon *>(command);
  1039. state.canvas_shader.set_conditional(CanvasShaderGLES2::USE_TEXTURE_RECT, false);
  1040. if (state.canvas_shader.bind()) {
  1041. _set_uniforms();
  1042. state.canvas_shader.use_material((void *)p_material);
  1043. }
  1044. RasterizerStorageGLES2::Texture *texture = _bind_canvas_texture(polygon->texture, polygon->normal_map);
  1045. if (texture) {
  1046. Size2 texpixel_size(1.0 / texture->width, 1.0 / texture->height);
  1047. state.canvas_shader.set_uniform(CanvasShaderGLES2::COLOR_TEXPIXEL_SIZE, texpixel_size);
  1048. }
  1049. _draw_polygon(polygon->indices.ptr(), polygon->count, polygon->points.size(), polygon->points.ptr(), polygon->uvs.ptr(), polygon->colors.ptr(), polygon->colors.size() == 1, polygon->weights.ptr(), polygon->bones.ptr());
  1050. #ifdef GLES_OVER_GL
  1051. if (polygon->antialiased) {
  1052. glEnable(GL_LINE_SMOOTH);
  1053. if (polygon->antialiasing_use_indices) {
  1054. _draw_generic_indices(GL_LINE_STRIP, polygon->indices.ptr(), polygon->count, polygon->points.size(), polygon->points.ptr(), polygon->uvs.ptr(), polygon->colors.ptr(), polygon->colors.size() == 1);
  1055. } else {
  1056. _draw_generic(GL_LINE_LOOP, polygon->points.size(), polygon->points.ptr(), polygon->uvs.ptr(), polygon->colors.ptr(), polygon->colors.size() == 1);
  1057. }
  1058. glDisable(GL_LINE_SMOOTH);
  1059. }
  1060. #endif
  1061. } break;
  1062. case Item::Command::TYPE_MESH: {
  1063. Item::CommandMesh *mesh = static_cast<Item::CommandMesh *>(command);
  1064. state.canvas_shader.set_conditional(CanvasShaderGLES2::USE_TEXTURE_RECT, false);
  1065. if (state.canvas_shader.bind()) {
  1066. _set_uniforms();
  1067. state.canvas_shader.use_material((void *)p_material);
  1068. }
  1069. RasterizerStorageGLES2::Texture *texture = _bind_canvas_texture(mesh->texture, mesh->normal_map);
  1070. if (texture) {
  1071. Size2 texpixel_size(1.0 / texture->width, 1.0 / texture->height);
  1072. state.canvas_shader.set_uniform(CanvasShaderGLES2::COLOR_TEXPIXEL_SIZE, texpixel_size);
  1073. }
  1074. RasterizerStorageGLES2::Mesh *mesh_data = storage->mesh_owner.getornull(mesh->mesh);
  1075. if (mesh_data) {
  1076. for (int j = 0; j < mesh_data->surfaces.size(); j++) {
  1077. RasterizerStorageGLES2::Surface *s = mesh_data->surfaces[j];
  1078. // materials are ignored in 2D meshes, could be added but many things (ie, lighting mode, reading from screen, etc) would break as they are not meant be set up at this point of drawing
  1079. glBindBuffer(GL_ARRAY_BUFFER, s->vertex_id);
  1080. if (s->index_array_len > 0) {
  1081. glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, s->index_id);
  1082. }
  1083. for (int k = 0; k < VS::ARRAY_MAX - 1; k++) {
  1084. if (s->attribs[k].enabled) {
  1085. glEnableVertexAttribArray(k);
  1086. glVertexAttribPointer(s->attribs[k].index, s->attribs[k].size, s->attribs[k].type, s->attribs[k].normalized, s->attribs[k].stride, CAST_INT_TO_UCHAR_PTR(s->attribs[k].offset));
  1087. } else {
  1088. glDisableVertexAttribArray(k);
  1089. switch (k) {
  1090. case VS::ARRAY_NORMAL: {
  1091. glVertexAttrib4f(VS::ARRAY_NORMAL, 0.0, 0.0, 1, 1);
  1092. } break;
  1093. case VS::ARRAY_COLOR: {
  1094. glVertexAttrib4f(VS::ARRAY_COLOR, 1, 1, 1, 1);
  1095. } break;
  1096. default: {
  1097. }
  1098. }
  1099. }
  1100. }
  1101. if (s->index_array_len > 0) {
  1102. glDrawElements(gl_primitive[s->primitive], s->index_array_len, (s->array_len >= (1 << 16)) ? GL_UNSIGNED_INT : GL_UNSIGNED_SHORT, 0);
  1103. } else {
  1104. glDrawArrays(gl_primitive[s->primitive], 0, s->array_len);
  1105. }
  1106. }
  1107. for (int j = 1; j < VS::ARRAY_MAX - 1; j++) {
  1108. glDisableVertexAttribArray(j);
  1109. }
  1110. }
  1111. storage->info.render._2d_draw_call_count++;
  1112. } break;
  1113. case Item::Command::TYPE_MULTIMESH: {
  1114. Item::CommandMultiMesh *mmesh = static_cast<Item::CommandMultiMesh *>(command);
  1115. RasterizerStorageGLES2::MultiMesh *multi_mesh = storage->multimesh_owner.getornull(mmesh->multimesh);
  1116. if (!multi_mesh)
  1117. break;
  1118. RasterizerStorageGLES2::Mesh *mesh_data = storage->mesh_owner.getornull(multi_mesh->mesh);
  1119. if (!mesh_data)
  1120. break;
  1121. state.canvas_shader.set_conditional(CanvasShaderGLES2::USE_INSTANCE_CUSTOM, multi_mesh->custom_data_format != VS::MULTIMESH_CUSTOM_DATA_NONE);
  1122. state.canvas_shader.set_conditional(CanvasShaderGLES2::USE_INSTANCING, true);
  1123. state.canvas_shader.set_conditional(CanvasShaderGLES2::USE_TEXTURE_RECT, false);
  1124. if (state.canvas_shader.bind()) {
  1125. _set_uniforms();
  1126. state.canvas_shader.use_material((void *)p_material);
  1127. }
  1128. RasterizerStorageGLES2::Texture *texture = _bind_canvas_texture(mmesh->texture, mmesh->normal_map);
  1129. if (texture) {
  1130. Size2 texpixel_size(1.0 / texture->width, 1.0 / texture->height);
  1131. state.canvas_shader.set_uniform(CanvasShaderGLES2::COLOR_TEXPIXEL_SIZE, texpixel_size);
  1132. }
  1133. //reset shader and force rebind
  1134. int amount = MIN(multi_mesh->size, multi_mesh->visible_instances);
  1135. if (amount == -1) {
  1136. amount = multi_mesh->size;
  1137. }
  1138. int stride = multi_mesh->color_floats + multi_mesh->custom_data_floats + multi_mesh->xform_floats;
  1139. int color_ofs = multi_mesh->xform_floats;
  1140. int custom_data_ofs = color_ofs + multi_mesh->color_floats;
  1141. // drawing
  1142. const float *base_buffer = multi_mesh->data.ptr();
  1143. for (int j = 0; j < mesh_data->surfaces.size(); j++) {
  1144. RasterizerStorageGLES2::Surface *s = mesh_data->surfaces[j];
  1145. // materials are ignored in 2D meshes, could be added but many things (ie, lighting mode, reading from screen, etc) would break as they are not meant be set up at this point of drawing
  1146. //bind buffers for mesh surface
  1147. glBindBuffer(GL_ARRAY_BUFFER, s->vertex_id);
  1148. if (s->index_array_len > 0) {
  1149. glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, s->index_id);
  1150. }
  1151. for (int k = 0; k < VS::ARRAY_MAX - 1; k++) {
  1152. if (s->attribs[k].enabled) {
  1153. glEnableVertexAttribArray(k);
  1154. glVertexAttribPointer(s->attribs[k].index, s->attribs[k].size, s->attribs[k].type, s->attribs[k].normalized, s->attribs[k].stride, CAST_INT_TO_UCHAR_PTR(s->attribs[k].offset));
  1155. } else {
  1156. glDisableVertexAttribArray(k);
  1157. switch (k) {
  1158. case VS::ARRAY_NORMAL: {
  1159. glVertexAttrib4f(VS::ARRAY_NORMAL, 0.0, 0.0, 1, 1);
  1160. } break;
  1161. case VS::ARRAY_COLOR: {
  1162. glVertexAttrib4f(VS::ARRAY_COLOR, 1, 1, 1, 1);
  1163. } break;
  1164. default: {
  1165. }
  1166. }
  1167. }
  1168. }
  1169. for (int k = 0; k < amount; k++) {
  1170. const float *buffer = base_buffer + k * stride;
  1171. {
  1172. glVertexAttrib4fv(INSTANCE_ATTRIB_BASE + 0, &buffer[0]);
  1173. glVertexAttrib4fv(INSTANCE_ATTRIB_BASE + 1, &buffer[4]);
  1174. if (multi_mesh->transform_format == VS::MULTIMESH_TRANSFORM_3D) {
  1175. glVertexAttrib4fv(INSTANCE_ATTRIB_BASE + 2, &buffer[8]);
  1176. } else {
  1177. glVertexAttrib4f(INSTANCE_ATTRIB_BASE + 2, 0.0, 0.0, 1.0, 0.0);
  1178. }
  1179. }
  1180. if (multi_mesh->color_floats) {
  1181. if (multi_mesh->color_format == VS::MULTIMESH_COLOR_8BIT) {
  1182. uint8_t *color_data = (uint8_t *)(buffer + color_ofs);
  1183. glVertexAttrib4f(INSTANCE_ATTRIB_BASE + 3, color_data[0] / 255.0, color_data[1] / 255.0, color_data[2] / 255.0, color_data[3] / 255.0);
  1184. } else {
  1185. glVertexAttrib4fv(INSTANCE_ATTRIB_BASE + 3, buffer + color_ofs);
  1186. }
  1187. } else {
  1188. glVertexAttrib4f(INSTANCE_ATTRIB_BASE + 3, 1.0, 1.0, 1.0, 1.0);
  1189. }
  1190. if (multi_mesh->custom_data_floats) {
  1191. if (multi_mesh->custom_data_format == VS::MULTIMESH_CUSTOM_DATA_8BIT) {
  1192. uint8_t *custom_data = (uint8_t *)(buffer + custom_data_ofs);
  1193. glVertexAttrib4f(INSTANCE_ATTRIB_BASE + 4, custom_data[0] / 255.0, custom_data[1] / 255.0, custom_data[2] / 255.0, custom_data[3] / 255.0);
  1194. } else {
  1195. glVertexAttrib4fv(INSTANCE_ATTRIB_BASE + 4, buffer + custom_data_ofs);
  1196. }
  1197. }
  1198. if (s->index_array_len > 0) {
  1199. glDrawElements(gl_primitive[s->primitive], s->index_array_len, (s->array_len >= (1 << 16)) ? GL_UNSIGNED_INT : GL_UNSIGNED_SHORT, 0);
  1200. } else {
  1201. glDrawArrays(gl_primitive[s->primitive], 0, s->array_len);
  1202. }
  1203. }
  1204. }
  1205. state.canvas_shader.set_conditional(CanvasShaderGLES2::USE_INSTANCE_CUSTOM, false);
  1206. state.canvas_shader.set_conditional(CanvasShaderGLES2::USE_INSTANCING, false);
  1207. storage->info.render._2d_draw_call_count++;
  1208. } break;
  1209. case Item::Command::TYPE_POLYLINE: {
  1210. Item::CommandPolyLine *pline = static_cast<Item::CommandPolyLine *>(command);
  1211. state.canvas_shader.set_conditional(CanvasShaderGLES2::USE_TEXTURE_RECT, false);
  1212. if (state.canvas_shader.bind()) {
  1213. _set_uniforms();
  1214. state.canvas_shader.use_material((void *)p_material);
  1215. }
  1216. _bind_canvas_texture(RID(), RID());
  1217. if (pline->triangles.size()) {
  1218. _draw_generic(GL_TRIANGLE_STRIP, pline->triangles.size(), pline->triangles.ptr(), NULL, pline->triangle_colors.ptr(), pline->triangle_colors.size() == 1);
  1219. #ifdef GLES_OVER_GL
  1220. glEnable(GL_LINE_SMOOTH);
  1221. if (pline->multiline) {
  1222. //needs to be different
  1223. } else {
  1224. _draw_generic(GL_LINE_LOOP, pline->lines.size(), pline->lines.ptr(), NULL, pline->line_colors.ptr(), pline->line_colors.size() == 1);
  1225. }
  1226. glDisable(GL_LINE_SMOOTH);
  1227. #endif
  1228. } else {
  1229. #ifdef GLES_OVER_GL
  1230. if (pline->antialiased)
  1231. glEnable(GL_LINE_SMOOTH);
  1232. #endif
  1233. if (pline->multiline) {
  1234. int todo = pline->lines.size() / 2;
  1235. int max_per_call = data.polygon_buffer_size / (sizeof(real_t) * 4);
  1236. int offset = 0;
  1237. while (todo) {
  1238. int to_draw = MIN(max_per_call, todo);
  1239. _draw_generic(GL_LINES, to_draw * 2, &pline->lines.ptr()[offset], NULL, pline->line_colors.size() == 1 ? pline->line_colors.ptr() : &pline->line_colors.ptr()[offset], pline->line_colors.size() == 1);
  1240. todo -= to_draw;
  1241. offset += to_draw * 2;
  1242. }
  1243. } else {
  1244. _draw_generic(GL_LINE_STRIP, pline->lines.size(), pline->lines.ptr(), NULL, pline->line_colors.ptr(), pline->line_colors.size() == 1);
  1245. }
  1246. #ifdef GLES_OVER_GL
  1247. if (pline->antialiased)
  1248. glDisable(GL_LINE_SMOOTH);
  1249. #endif
  1250. }
  1251. } break;
  1252. case Item::Command::TYPE_PRIMITIVE: {
  1253. Item::CommandPrimitive *primitive = static_cast<Item::CommandPrimitive *>(command);
  1254. state.canvas_shader.set_conditional(CanvasShaderGLES2::USE_TEXTURE_RECT, false);
  1255. if (state.canvas_shader.bind()) {
  1256. _set_uniforms();
  1257. state.canvas_shader.use_material((void *)p_material);
  1258. }
  1259. ERR_CONTINUE(primitive->points.size() < 1);
  1260. RasterizerStorageGLES2::Texture *texture = _bind_canvas_texture(primitive->texture, primitive->normal_map);
  1261. if (texture) {
  1262. Size2 texpixel_size(1.0 / texture->width, 1.0 / texture->height);
  1263. state.canvas_shader.set_uniform(CanvasShaderGLES2::COLOR_TEXPIXEL_SIZE, texpixel_size);
  1264. }
  1265. if (primitive->colors.size() == 1 && primitive->points.size() > 1) {
  1266. Color c = primitive->colors[0];
  1267. glVertexAttrib4f(VS::ARRAY_COLOR, c.r, c.g, c.b, c.a);
  1268. } else if (primitive->colors.empty()) {
  1269. glVertexAttrib4f(VS::ARRAY_COLOR, 1, 1, 1, 1);
  1270. }
  1271. _draw_gui_primitive(primitive->points.size(), primitive->points.ptr(), primitive->colors.ptr(), primitive->uvs.ptr());
  1272. } break;
  1273. case Item::Command::TYPE_TRANSFORM: {
  1274. Item::CommandTransform *transform = static_cast<Item::CommandTransform *>(command);
  1275. state.uniforms.extra_matrix = transform->xform;
  1276. state.canvas_shader.set_uniform(CanvasShaderGLES2::EXTRA_MATRIX, state.uniforms.extra_matrix);
  1277. } break;
  1278. case Item::Command::TYPE_PARTICLES: {
  1279. } break;
  1280. case Item::Command::TYPE_CLIP_IGNORE: {
  1281. Item::CommandClipIgnore *ci = static_cast<Item::CommandClipIgnore *>(command);
  1282. if (p_current_clip) {
  1283. if (ci->ignore != r_reclip) {
  1284. if (ci->ignore) {
  1285. glDisable(GL_SCISSOR_TEST);
  1286. r_reclip = true;
  1287. } else {
  1288. glEnable(GL_SCISSOR_TEST);
  1289. int x = p_current_clip->final_clip_rect.position.x;
  1290. int y = storage->frame.current_rt->height - (p_current_clip->final_clip_rect.position.y + p_current_clip->final_clip_rect.size.y);
  1291. int w = p_current_clip->final_clip_rect.size.x;
  1292. int h = p_current_clip->final_clip_rect.size.y;
  1293. if (storage->frame.current_rt->flags[RasterizerStorage::RENDER_TARGET_VFLIP])
  1294. y = p_current_clip->final_clip_rect.position.y;
  1295. glScissor(x, y, w, h);
  1296. r_reclip = false;
  1297. }
  1298. }
  1299. }
  1300. } break;
  1301. default: {
  1302. // FIXME: Proper error handling if relevant
  1303. //print_line("other");
  1304. } break;
  1305. }
  1306. }
  1307. } // default
  1308. break;
  1309. }
  1310. }
  1311. // zero all the batch data ready for a new run
  1312. bdata.reset_flush();
  1313. }
  1314. void RasterizerCanvasGLES2::render_joined_item_commands(const BItemJoined &p_bij, Item *p_current_clip, bool &r_reclip, RasterizerStorageGLES2::Material *p_material, bool p_lit) {
  1315. Item *item = 0;
  1316. Item *first_item = bdata.item_refs[p_bij.first_item_ref].item;
  1317. FillState fill_state;
  1318. fill_state.reset();
  1319. fill_state.use_hardware_transform = p_bij.use_hardware_transform();
  1320. fill_state.extra_matrix_sent = false;
  1321. // in the special case of custom shaders that read from VERTEX (i.e. vertex position)
  1322. // we want to disable software transform of extra matrix
  1323. if (bdata.joined_item_batch_flags & RasterizerStorageGLES2::Shader::CanvasItem::PREVENT_VERTEX_BAKING) {
  1324. fill_state.extra_matrix_sent = true;
  1325. }
  1326. for (unsigned int i = 0; i < p_bij.num_item_refs; i++) {
  1327. const BItemRef &ref = bdata.item_refs[p_bij.first_item_ref + i];
  1328. item = ref.item;
  1329. if (!p_lit) {
  1330. // if not lit we use the complex calculated final modulate
  1331. fill_state.final_modulate = ref.final_modulate;
  1332. } else {
  1333. // if lit we ignore canvas modulate and just use the item modulate
  1334. fill_state.final_modulate = item->final_modulate;
  1335. }
  1336. int command_count = item->commands.size();
  1337. int command_start = 0;
  1338. // ONCE OFF fill state setup, that will be retained over multiple calls to
  1339. // prefill_joined_item()
  1340. fill_state.transform_combined = item->final_transform;
  1341. // decide the initial transform mode, and make a backup
  1342. // in orig_transform_mode in case we need to switch back
  1343. if (!fill_state.use_hardware_transform) {
  1344. fill_state.transform_mode = _find_transform_mode(fill_state.transform_combined);
  1345. } else {
  1346. fill_state.transform_mode = TM_NONE;
  1347. }
  1348. fill_state.orig_transform_mode = fill_state.transform_mode;
  1349. // keep track of when we added an extra matrix
  1350. // so we can defer sending until we see a default command
  1351. fill_state.transform_extra_command_number_p1 = 0;
  1352. while (command_start < command_count) {
  1353. // fill as many batches as possible (until all done, or the vertex buffer is full)
  1354. bool bFull = prefill_joined_item(fill_state, command_start, item, p_current_clip, r_reclip, p_material);
  1355. if (bFull) {
  1356. // always pass first item (commands for default are always first item)
  1357. flush_render_batches(first_item, p_current_clip, r_reclip, p_material);
  1358. fill_state.reset();
  1359. }
  1360. }
  1361. }
  1362. // flush if any left
  1363. flush_render_batches(first_item, p_current_clip, r_reclip, p_material);
  1364. }
  1365. void RasterizerCanvasGLES2::flush_render_batches(Item *p_first_item, Item *p_current_clip, bool &r_reclip, RasterizerStorageGLES2::Material *p_material) {
  1366. // some heuristic to decide whether to use colored verts.
  1367. // feel free to tweak this.
  1368. // this could use hysteresis, to prevent jumping between methods
  1369. // .. however probably not necessary
  1370. bdata.use_colored_vertices = false;
  1371. // only check whether to convert if there are quads (prevent divide by zero)
  1372. // and we haven't decided to prevent color baking (due to e.g. MODULATE
  1373. // being used in a shader)
  1374. if (bdata.total_quads && !(bdata.joined_item_batch_flags & RasterizerStorageGLES2::Shader::CanvasItem::PREVENT_COLOR_BAKING)) {
  1375. // minus 1 to prevent single primitives (ratio 1.0) always being converted to colored..
  1376. // in that case it is slightly cheaper to just have the color as part of the batch
  1377. float ratio = (float)(bdata.total_color_changes - 1) / (float)bdata.total_quads;
  1378. // use bigger than or equal so that 0.0 threshold can force always using colored verts
  1379. if (ratio >= bdata.settings_colored_vertex_format_threshold) {
  1380. bdata.use_colored_vertices = true;
  1381. // small perf cost versus going straight to colored verts (maybe around 10%)
  1382. // however more straightforward
  1383. _batch_translate_to_colored();
  1384. }
  1385. }
  1386. // send buffers to opengl
  1387. _batch_upload_buffers();
  1388. Item::Command *const *commands = p_first_item->commands.ptr();
  1389. #ifdef DEBUG_ENABLED
  1390. if (bdata.diagnose_frame) {
  1391. diagnose_batches(commands);
  1392. }
  1393. #endif
  1394. render_batches(commands, p_current_clip, r_reclip, p_material);
  1395. }
  1396. void RasterizerCanvasGLES2::_canvas_item_render_commands(Item *p_item, Item *p_current_clip, bool &r_reclip, RasterizerStorageGLES2::Material *p_material) {
  1397. int command_count = p_item->commands.size();
  1398. Item::Command *const *commands = p_item->commands.ptr();
  1399. // legacy .. just create one massive batch and render everything as before
  1400. bdata.batches.reset();
  1401. Batch *batch = _batch_request_new();
  1402. batch->type = Batch::BT_DEFAULT;
  1403. batch->num_commands = command_count;
  1404. render_batches(commands, p_current_clip, r_reclip, p_material);
  1405. }
  1406. void RasterizerCanvasGLES2::record_items(Item *p_item_list, int p_z) {
  1407. while (p_item_list) {
  1408. BSortItem *s = bdata.sort_items.request_with_grow();
  1409. s->item = p_item_list;
  1410. s->z_index = p_z;
  1411. p_item_list = p_item_list->next;
  1412. }
  1413. }
  1414. void RasterizerCanvasGLES2::sort_items() {
  1415. // turned off?
  1416. if (!bdata.settings_item_reordering_lookahead) {
  1417. return;
  1418. }
  1419. for (int s = 0; s < bdata.sort_items.size() - 2; s++) {
  1420. if (sort_items_from(s)) {
  1421. #ifdef DEBUG_ENABLED
  1422. bdata.stats_items_sorted++;
  1423. #endif
  1424. }
  1425. }
  1426. }
  1427. bool RasterizerCanvasGLES2::sort_items_from(int p_start) {
  1428. #ifdef DEBUG_ENABLED
  1429. ERR_FAIL_COND_V((p_start + 1) >= bdata.sort_items.size(), false)
  1430. #endif
  1431. const BSortItem &start = bdata.sort_items[p_start];
  1432. int start_z = start.z_index;
  1433. // check start is the right type for sorting
  1434. if (start.item->commands.size() != 1) {
  1435. return false;
  1436. }
  1437. const Item::Command &command_start = *start.item->commands[0];
  1438. if (command_start.type != Item::Command::TYPE_RECT) {
  1439. return false;
  1440. }
  1441. BSortItem &second = bdata.sort_items[p_start + 1];
  1442. if (second.z_index != start_z) {
  1443. // no sorting across z indices (for now)
  1444. return false;
  1445. }
  1446. // if the neighbours are already a good match
  1447. if (_sort_items_match(start, second)) // order is crucial, start first
  1448. {
  1449. return false;
  1450. }
  1451. // local cached aabb
  1452. Rect2 second_AABB = second.item->global_rect_cache;
  1453. // if the start and 2nd items overlap, can do no more
  1454. if (start.item->global_rect_cache.intersects(second_AABB)) {
  1455. return false;
  1456. }
  1457. // which neighbour to test
  1458. int test_last = 2 + bdata.settings_item_reordering_lookahead;
  1459. for (int test = 2; test < test_last; test++) {
  1460. int test_sort_item_id = p_start + test;
  1461. // if we've got to the end of the list, can't sort any more, give up
  1462. if (test_sort_item_id >= bdata.sort_items.size()) {
  1463. return false;
  1464. }
  1465. BSortItem *test_sort_item = &bdata.sort_items[test_sort_item_id];
  1466. // across z indices?
  1467. if (test_sort_item->z_index != start_z) {
  1468. return false;
  1469. }
  1470. Item *test_item = test_sort_item->item;
  1471. // if the test item overlaps the second item, we can't swap, AT ALL
  1472. // because swapping an item OVER this one would cause artefacts
  1473. if (second_AABB.intersects(test_item->global_rect_cache)) {
  1474. return false;
  1475. }
  1476. // do they match?
  1477. if (!_sort_items_match(start, *test_sort_item)) // order is crucial, start first
  1478. {
  1479. continue;
  1480. }
  1481. // we can only swap if there are no AABB overlaps with sandwiched neighbours
  1482. bool ok = true;
  1483. // start from 2, no need to check 1 as the second has already been checked against this item
  1484. // in the intersection test above
  1485. for (int sn = 2; sn < test; sn++) {
  1486. BSortItem *sandwich_neighbour = &bdata.sort_items[p_start + sn];
  1487. if (test_item->global_rect_cache.intersects(sandwich_neighbour->item->global_rect_cache)) {
  1488. ok = false;
  1489. break;
  1490. }
  1491. }
  1492. if (!ok) {
  1493. continue;
  1494. }
  1495. // it is ok to exchange them!
  1496. BSortItem temp;
  1497. temp.assign(second);
  1498. second.assign(*test_sort_item);
  1499. test_sort_item->assign(temp);
  1500. return true;
  1501. } // for test
  1502. return false;
  1503. }
  1504. void RasterizerCanvasGLES2::join_sorted_items() {
  1505. sort_items();
  1506. int z = VS::CANVAS_ITEM_Z_MIN;
  1507. _render_item_state.item_group_z = z;
  1508. for (int s = 0; s < bdata.sort_items.size(); s++) {
  1509. const BSortItem &si = bdata.sort_items[s];
  1510. Item *ci = si.item;
  1511. // change z?
  1512. if (si.z_index != z) {
  1513. z = si.z_index;
  1514. // may not be required
  1515. _render_item_state.item_group_z = z;
  1516. // if z ranged lights are present, sometimes we have to disable joining over z_indices.
  1517. // we do this here.
  1518. // Note this restriction may be able to be relaxed with light bitfields, investigate!
  1519. if (!bdata.join_across_z_indices) {
  1520. _render_item_state.join_batch_break = true;
  1521. }
  1522. }
  1523. bool join;
  1524. if (_render_item_state.join_batch_break) {
  1525. // always start a new batch for this item
  1526. join = false;
  1527. // could be another batch break (i.e. prevent NEXT item from joining this)
  1528. // so we still need to run try_join_item
  1529. // even though we know join is false.
  1530. // also we need to run try_join_item for every item because it keeps the state up to date,
  1531. // if we didn't run it the state would be out of date.
  1532. try_join_item(ci, _render_item_state, _render_item_state.join_batch_break);
  1533. } else {
  1534. join = try_join_item(ci, _render_item_state, _render_item_state.join_batch_break);
  1535. }
  1536. // assume the first item will always return no join
  1537. if (!join) {
  1538. _render_item_state.joined_item = bdata.items_joined.request_with_grow();
  1539. _render_item_state.joined_item->first_item_ref = bdata.item_refs.size();
  1540. _render_item_state.joined_item->num_item_refs = 1;
  1541. _render_item_state.joined_item->bounding_rect = ci->global_rect_cache;
  1542. _render_item_state.joined_item->z_index = z;
  1543. _render_item_state.joined_item->flags = bdata.joined_item_batch_flags;
  1544. // add the reference
  1545. BItemRef *r = bdata.item_refs.request_with_grow();
  1546. r->item = ci;
  1547. // we are storing final_modulate in advance per item reference
  1548. // for baking into vertex colors.
  1549. // this may not be ideal... as we are increasing the size of item reference,
  1550. // but it is stupidly complex to calculate later, which would probably be slower.
  1551. r->final_modulate = _render_item_state.final_modulate;
  1552. } else {
  1553. CRASH_COND(_render_item_state.joined_item == 0);
  1554. _render_item_state.joined_item->num_item_refs += 1;
  1555. _render_item_state.joined_item->bounding_rect = _render_item_state.joined_item->bounding_rect.merge(ci->global_rect_cache);
  1556. BItemRef *r = bdata.item_refs.request_with_grow();
  1557. r->item = ci;
  1558. r->final_modulate = _render_item_state.final_modulate;
  1559. }
  1560. } // for s through sort items
  1561. }
  1562. void RasterizerCanvasGLES2::join_items(Item *p_item_list, int p_z) {
  1563. _render_item_state.item_group_z = p_z;
  1564. // join is whether to join to the previous batch.
  1565. // batch_break is whether to PREVENT the next batch from joining with us
  1566. // batch_break must be preserved over z_indices,
  1567. // so is stored in _render_item_state.join_batch_break
  1568. // if z ranged lights are present, sometimes we have to disable joining over z_indices.
  1569. // we do this here
  1570. if (!bdata.join_across_z_indices) {
  1571. _render_item_state.join_batch_break = true;
  1572. }
  1573. while (p_item_list) {
  1574. Item *ci = p_item_list;
  1575. bool join;
  1576. if (_render_item_state.join_batch_break) {
  1577. // always start a new batch for this item
  1578. join = false;
  1579. // could be another batch break (i.e. prevent NEXT item from joining this)
  1580. // so we still need to run try_join_item
  1581. // even though we know join is false.
  1582. // also we need to run try_join_item for every item because it keeps the state up to date,
  1583. // if we didn't run it the state would be out of date.
  1584. try_join_item(ci, _render_item_state, _render_item_state.join_batch_break);
  1585. } else {
  1586. join = try_join_item(ci, _render_item_state, _render_item_state.join_batch_break);
  1587. }
  1588. // assume the first item will always return no join
  1589. if (!join) {
  1590. _render_item_state.joined_item = bdata.items_joined.request_with_grow();
  1591. _render_item_state.joined_item->first_item_ref = bdata.item_refs.size();
  1592. _render_item_state.joined_item->num_item_refs = 1;
  1593. _render_item_state.joined_item->bounding_rect = ci->global_rect_cache;
  1594. _render_item_state.joined_item->z_index = p_z;
  1595. // add the reference
  1596. BItemRef *r = bdata.item_refs.request_with_grow();
  1597. r->item = ci;
  1598. // we are storing final_modulate in advance per item reference
  1599. // for baking into vertex colors.
  1600. // this may not be ideal... as we are increasing the size of item reference,
  1601. // but it is stupidly complex to calculate later, which would probably be slower.
  1602. r->final_modulate = _render_item_state.final_modulate;
  1603. } else {
  1604. CRASH_COND(_render_item_state.joined_item == 0);
  1605. _render_item_state.joined_item->num_item_refs += 1;
  1606. _render_item_state.joined_item->bounding_rect = _render_item_state.joined_item->bounding_rect.merge(ci->global_rect_cache);
  1607. BItemRef *r = bdata.item_refs.request_with_grow();
  1608. r->item = ci;
  1609. r->final_modulate = _render_item_state.final_modulate;
  1610. }
  1611. p_item_list = p_item_list->next;
  1612. }
  1613. }
  1614. void RasterizerCanvasGLES2::canvas_end() {
  1615. #ifdef DEBUG_ENABLED
  1616. if (bdata.diagnose_frame) {
  1617. bdata.frame_string += "canvas_end\n";
  1618. if (bdata.stats_items_sorted) {
  1619. bdata.frame_string += "\titems reordered: " + itos(bdata.stats_items_sorted) + "\n";
  1620. }
  1621. if (bdata.stats_light_items_joined) {
  1622. bdata.frame_string += "\tlight items joined: " + itos(bdata.stats_light_items_joined) + "\n";
  1623. }
  1624. print_line(bdata.frame_string);
  1625. }
  1626. #endif
  1627. RasterizerCanvasBaseGLES2::canvas_end();
  1628. }
  1629. void RasterizerCanvasGLES2::canvas_begin() {
  1630. // diagnose_frame?
  1631. bdata.frame_string = ""; // just in case, always set this as we don't want a string leak in release...
  1632. #ifdef DEBUG_ENABLED
  1633. if (bdata.settings_diagnose_frame) {
  1634. bdata.diagnose_frame = false;
  1635. uint32_t tick = OS::get_singleton()->get_ticks_msec();
  1636. uint64_t frame = Engine::get_singleton()->get_frames_drawn();
  1637. if (tick >= bdata.next_diagnose_tick) {
  1638. bdata.next_diagnose_tick = tick + 10000;
  1639. // the plus one is prevent starting diagnosis half way through frame
  1640. bdata.diagnose_frame_number = frame + 1;
  1641. }
  1642. if (frame == bdata.diagnose_frame_number) {
  1643. bdata.diagnose_frame = true;
  1644. bdata.reset_stats();
  1645. }
  1646. if (bdata.diagnose_frame) {
  1647. bdata.frame_string = "canvas_begin FRAME " + itos(frame) + "\n";
  1648. }
  1649. }
  1650. #endif
  1651. RasterizerCanvasBaseGLES2::canvas_begin();
  1652. }
  1653. void RasterizerCanvasGLES2::canvas_render_items_begin(const Color &p_modulate, Light *p_light, const Transform2D &p_base_transform) {
  1654. // if we are debugging, flash each frame between batching renderer and old version to compare for regressions
  1655. if (bdata.settings_flash_batching) {
  1656. if ((Engine::get_singleton()->get_frames_drawn() % 2) == 0)
  1657. bdata.settings_use_batching = true;
  1658. else
  1659. bdata.settings_use_batching = false;
  1660. }
  1661. if (!bdata.settings_use_batching) {
  1662. return;
  1663. }
  1664. // this only needs to be done when screen size changes, but this should be
  1665. // infrequent enough
  1666. _calculate_scissor_threshold_area();
  1667. // set up render item state for all the z_indexes (this is common to all z_indexes)
  1668. _render_item_state.reset();
  1669. _render_item_state.item_group_modulate = p_modulate;
  1670. _render_item_state.item_group_light = p_light;
  1671. _render_item_state.item_group_base_transform = p_base_transform;
  1672. _render_item_state.light_region.reset();
  1673. // batch break must be preserved over the different z indices,
  1674. // to prevent joining to an item on a previous index if not allowed
  1675. _render_item_state.join_batch_break = false;
  1676. // whether to join across z indices depends on whether there are z ranged lights.
  1677. // joined z_index items can be wrongly classified with z ranged lights.
  1678. bdata.join_across_z_indices = true;
  1679. int light_count = 0;
  1680. while (p_light) {
  1681. light_count++;
  1682. if ((p_light->z_min != VS::CANVAS_ITEM_Z_MIN) || (p_light->z_max != VS::CANVAS_ITEM_Z_MAX)) {
  1683. // prevent joining across z indices. This would have caused visual regressions
  1684. bdata.join_across_z_indices = false;
  1685. }
  1686. p_light = p_light->next_ptr;
  1687. }
  1688. // can't use the light region bitfield if there are too many lights
  1689. // hopefully most games won't blow this limit..
  1690. // if they do they will work but it won't batch join items just in case
  1691. if (light_count > 64) {
  1692. _render_item_state.light_region.too_many_lights = true;
  1693. }
  1694. }
  1695. void RasterizerCanvasGLES2::canvas_render_items_end() {
  1696. if (!bdata.settings_use_batching) {
  1697. return;
  1698. }
  1699. join_sorted_items();
  1700. #ifdef DEBUG_ENABLED
  1701. if (bdata.diagnose_frame) {
  1702. bdata.frame_string += "items\n";
  1703. }
  1704. #endif
  1705. // batching render is deferred until after going through all the z_indices, joining all the items
  1706. canvas_render_items_implementation(0, 0, _render_item_state.item_group_modulate,
  1707. _render_item_state.item_group_light,
  1708. _render_item_state.item_group_base_transform);
  1709. bdata.items_joined.reset();
  1710. bdata.item_refs.reset();
  1711. bdata.sort_items.reset();
  1712. }
  1713. void RasterizerCanvasGLES2::canvas_render_items(Item *p_item_list, int p_z, const Color &p_modulate, Light *p_light, const Transform2D &p_base_transform) {
  1714. // stage 1 : join similar items, so that their state changes are not repeated,
  1715. // and commands from joined items can be batched together
  1716. if (bdata.settings_use_batching) {
  1717. record_items(p_item_list, p_z);
  1718. return;
  1719. }
  1720. // only legacy renders at this stage, batched renderer doesn't render until canvas_render_items_end()
  1721. canvas_render_items_implementation(p_item_list, p_z, p_modulate, p_light, p_base_transform);
  1722. }
  1723. void RasterizerCanvasGLES2::canvas_render_items_implementation(Item *p_item_list, int p_z, const Color &p_modulate, Light *p_light, const Transform2D &p_base_transform) {
  1724. // parameters are easier to pass around in a structure
  1725. RenderItemState ris;
  1726. ris.item_group_z = p_z;
  1727. ris.item_group_modulate = p_modulate;
  1728. ris.item_group_light = p_light;
  1729. ris.item_group_base_transform = p_base_transform;
  1730. state.canvas_shader.set_conditional(CanvasShaderGLES2::USE_SKELETON, false);
  1731. state.current_tex = RID();
  1732. state.current_tex_ptr = NULL;
  1733. state.current_normal = RID();
  1734. state.canvas_texscreen_used = false;
  1735. glActiveTexture(GL_TEXTURE0);
  1736. glBindTexture(GL_TEXTURE_2D, storage->resources.white_tex);
  1737. if (bdata.settings_use_batching) {
  1738. for (int j = 0; j < bdata.items_joined.size(); j++) {
  1739. render_joined_item(bdata.items_joined[j], ris);
  1740. }
  1741. } else {
  1742. while (p_item_list) {
  1743. Item *ci = p_item_list;
  1744. _canvas_render_item(ci, ris);
  1745. p_item_list = p_item_list->next;
  1746. }
  1747. }
  1748. if (ris.current_clip) {
  1749. glDisable(GL_SCISSOR_TEST);
  1750. }
  1751. state.canvas_shader.set_conditional(CanvasShaderGLES2::USE_SKELETON, false);
  1752. }
  1753. // This function is a dry run of the state changes when drawing the item.
  1754. // It should duplicate the logic in _canvas_render_item,
  1755. // to decide whether items are similar enough to join
  1756. // i.e. no state differences between the 2 items.
  1757. bool RasterizerCanvasGLES2::try_join_item(Item *p_ci, RenderItemState &r_ris, bool &r_batch_break) {
  1758. // if we set max join items to zero we can effectively prevent any joining, so
  1759. // none of the other logic needs to run. Good for testing regression bugs, and
  1760. // could conceivably be faster in some games.
  1761. if (!bdata.settings_max_join_item_commands) {
  1762. return false;
  1763. }
  1764. // if there are any state changes we change join to false
  1765. // we also set r_batch_break to true if we don't want this item joined to the next
  1766. // (e.g. an item that must not be joined at all)
  1767. r_batch_break = false;
  1768. bool join = true;
  1769. // light_masked may possibly need state checking here. Check for regressions!
  1770. // we will now allow joining even if final modulate is different
  1771. // we will instead bake the final modulate into the vertex colors
  1772. // if (p_ci->final_modulate != r_ris.final_modulate) {
  1773. // join = false;
  1774. // r_ris.final_modulate = p_ci->final_modulate;
  1775. // }
  1776. if (r_ris.current_clip != p_ci->final_clip_owner) {
  1777. r_ris.current_clip = p_ci->final_clip_owner;
  1778. join = false;
  1779. }
  1780. // TODO: copy back buffer
  1781. if (p_ci->copy_back_buffer) {
  1782. join = false;
  1783. }
  1784. RasterizerStorageGLES2::Skeleton *skeleton = NULL;
  1785. {
  1786. //skeleton handling
  1787. if (p_ci->skeleton.is_valid() && storage->skeleton_owner.owns(p_ci->skeleton)) {
  1788. skeleton = storage->skeleton_owner.get(p_ci->skeleton);
  1789. if (!skeleton->use_2d) {
  1790. skeleton = NULL;
  1791. }
  1792. }
  1793. bool use_skeleton = skeleton != NULL;
  1794. if (r_ris.prev_use_skeleton != use_skeleton) {
  1795. r_ris.rebind_shader = true;
  1796. r_ris.prev_use_skeleton = use_skeleton;
  1797. join = false;
  1798. }
  1799. if (skeleton) {
  1800. join = false;
  1801. state.using_skeleton = true;
  1802. } else {
  1803. state.using_skeleton = false;
  1804. }
  1805. }
  1806. Item *material_owner = p_ci->material_owner ? p_ci->material_owner : p_ci;
  1807. RID material = material_owner->material;
  1808. RasterizerStorageGLES2::Material *material_ptr = storage->material_owner.getornull(material);
  1809. if (material != r_ris.canvas_last_material || r_ris.rebind_shader) {
  1810. join = false;
  1811. RasterizerStorageGLES2::Shader *shader_ptr = NULL;
  1812. if (material_ptr) {
  1813. shader_ptr = material_ptr->shader;
  1814. if (shader_ptr && shader_ptr->mode != VS::SHADER_CANVAS_ITEM) {
  1815. shader_ptr = NULL; // not a canvas item shader, don't use.
  1816. }
  1817. }
  1818. if (shader_ptr) {
  1819. if (shader_ptr->canvas_item.uses_screen_texture) {
  1820. if (!state.canvas_texscreen_used) {
  1821. join = false;
  1822. }
  1823. }
  1824. }
  1825. r_ris.shader_cache = shader_ptr;
  1826. r_ris.canvas_last_material = material;
  1827. r_ris.rebind_shader = false;
  1828. }
  1829. int blend_mode = r_ris.shader_cache ? r_ris.shader_cache->canvas_item.blend_mode : RasterizerStorageGLES2::Shader::CanvasItem::BLEND_MODE_MIX;
  1830. bool unshaded = r_ris.shader_cache && (r_ris.shader_cache->canvas_item.light_mode == RasterizerStorageGLES2::Shader::CanvasItem::LIGHT_MODE_UNSHADED || (blend_mode != RasterizerStorageGLES2::Shader::CanvasItem::BLEND_MODE_MIX && blend_mode != RasterizerStorageGLES2::Shader::CanvasItem::BLEND_MODE_PMALPHA));
  1831. bool reclip = false;
  1832. // we are precalculating the final_modulate ahead of time because we need this for baking of final modulate into vertex colors
  1833. // (only in software transform mode)
  1834. // This maybe inefficient storing it...
  1835. r_ris.final_modulate = unshaded ? p_ci->final_modulate : (p_ci->final_modulate * r_ris.item_group_modulate);
  1836. if (r_ris.last_blend_mode != blend_mode) {
  1837. join = false;
  1838. r_ris.last_blend_mode = blend_mode;
  1839. }
  1840. // does the shader contain BUILTINs which should break the batching?
  1841. bdata.joined_item_batch_flags = 0;
  1842. if (r_ris.shader_cache) {
  1843. unsigned int and_flags = r_ris.shader_cache->canvas_item.batch_flags & (RasterizerStorageGLES2::Shader::CanvasItem::PREVENT_COLOR_BAKING | RasterizerStorageGLES2::Shader::CanvasItem::PREVENT_VERTEX_BAKING);
  1844. if (and_flags) {
  1845. bool break_batching = true;
  1846. if (and_flags == RasterizerStorageGLES2::Shader::CanvasItem::PREVENT_COLOR_BAKING) {
  1847. // in some circumstances, if the modulate is identity, we still allow baking because reading modulate / color
  1848. // will still be okay to do in the shader with no ill effects
  1849. if (r_ris.final_modulate == Color(1, 1, 1, 1)) {
  1850. break_batching = false;
  1851. }
  1852. }
  1853. if (break_batching) {
  1854. join = false;
  1855. r_batch_break = true;
  1856. // save the flags so that they don't need to be recalculated in the 2nd pass
  1857. bdata.joined_item_batch_flags |= r_ris.shader_cache->canvas_item.batch_flags;
  1858. }
  1859. }
  1860. }
  1861. if ((blend_mode == RasterizerStorageGLES2::Shader::CanvasItem::BLEND_MODE_MIX || blend_mode == RasterizerStorageGLES2::Shader::CanvasItem::BLEND_MODE_PMALPHA) && r_ris.item_group_light && !unshaded) {
  1862. // we cannot join lit items easily.
  1863. // it is possible, but not if they overlap, because
  1864. // a + light_blend + b + light_blend IS NOT THE SAME AS
  1865. // a + b + light_blend
  1866. bool light_allow_join = true;
  1867. // this is a quick getout if we have turned off light joining
  1868. if ((bdata.settings_light_max_join_items == 0) || r_ris.light_region.too_many_lights) {
  1869. light_allow_join = false;
  1870. } else {
  1871. // do light joining...
  1872. // first calculate the light bitfield
  1873. uint64_t light_bitfield = 0;
  1874. uint64_t shadow_bitfield = 0;
  1875. Light *light = r_ris.item_group_light;
  1876. int light_count = -1;
  1877. while (light) {
  1878. light_count++;
  1879. uint64_t light_bit = 1ULL << light_count;
  1880. // note that as a cost of batching, the light culling will be less effective
  1881. if (p_ci->light_mask & light->item_mask && r_ris.item_group_z >= light->z_min && r_ris.item_group_z <= light->z_max) {
  1882. // Note that with the above test, it is possible to also include a bound check.
  1883. // Tests so far have indicated better performance without it, but there may be reason to change this at a later stage,
  1884. // so I leave the line here for reference:
  1885. // && p_ci->global_rect_cache.intersects_transformed(light->xform_cache, light->rect_cache)) {
  1886. light_bitfield |= light_bit;
  1887. bool has_shadow = light->shadow_buffer.is_valid() && p_ci->light_mask & light->item_shadow_mask;
  1888. if (has_shadow) {
  1889. shadow_bitfield |= light_bit;
  1890. }
  1891. }
  1892. light = light->next_ptr;
  1893. }
  1894. // now compare to previous
  1895. if ((r_ris.light_region.light_bitfield != light_bitfield) || (r_ris.light_region.shadow_bitfield != shadow_bitfield)) {
  1896. light_allow_join = false;
  1897. r_ris.light_region.light_bitfield = light_bitfield;
  1898. r_ris.light_region.shadow_bitfield = shadow_bitfield;
  1899. } else {
  1900. // only do these checks if necessary
  1901. if (join && (!r_batch_break)) {
  1902. // we still can't join, even if the lights are exactly the same, if there is overlap between the previous and this item
  1903. if (r_ris.joined_item && light_bitfield) {
  1904. if ((int)r_ris.joined_item->num_item_refs <= bdata.settings_light_max_join_items) {
  1905. for (uint32_t r = 0; r < r_ris.joined_item->num_item_refs; r++) {
  1906. Item *pRefItem = bdata.item_refs[r_ris.joined_item->first_item_ref + r].item;
  1907. if (p_ci->global_rect_cache.intersects(pRefItem->global_rect_cache)) {
  1908. light_allow_join = false;
  1909. break;
  1910. }
  1911. }
  1912. #ifdef DEBUG_ENABLED
  1913. if (light_allow_join) {
  1914. bdata.stats_light_items_joined++;
  1915. }
  1916. #endif
  1917. } // if below max join items
  1918. else {
  1919. // just don't allow joining if above overlap check max items
  1920. light_allow_join = false;
  1921. }
  1922. }
  1923. } // if not batch broken already (no point in doing expensive overlap tests if not needed)
  1924. } // if bitfields don't match
  1925. } // if do light joining
  1926. if (!light_allow_join) {
  1927. // can't join
  1928. join = false;
  1929. }
  1930. } else {
  1931. // if the last item had lights, we should not join it to this one (which has no lights)
  1932. if (r_ris.light_region.light_bitfield || r_ris.light_region.shadow_bitfield) {
  1933. join = false;
  1934. // setting these to zero ensures that any following item with lights will, by definition,
  1935. // be affected by a different set of lights, and thus prevent a join
  1936. r_ris.light_region.light_bitfield = 0;
  1937. r_ris.light_region.shadow_bitfield = 0;
  1938. }
  1939. }
  1940. if (reclip) {
  1941. join = false;
  1942. }
  1943. // non rects will break the batching anyway, we don't want to record item changes, detect this
  1944. if (!r_batch_break && _detect_batch_break(p_ci)) {
  1945. join = false;
  1946. r_batch_break = true;
  1947. }
  1948. return join;
  1949. }
  1950. bool RasterizerCanvasGLES2::_detect_batch_break(Item *p_ci) {
  1951. int command_count = p_ci->commands.size();
  1952. // Any item that contains commands that are default
  1953. // (i.e. not handled by software transform and the batching renderer) should not be joined.
  1954. // In order to work this out, it does a lookahead through the commands,
  1955. // which could potentially be very expensive. As such it makes sense to put a limit on this
  1956. // to some small number, which will catch nearly all cases which need joining,
  1957. // but not be overly expensive in the case of items with large numbers of commands.
  1958. // It is hard to know what this number should be, empirically,
  1959. // and this has not been fully investigated. It works to join single sprite items when set to 1 or above.
  1960. // Note that there is a cost to increasing this because it has to look in advance through
  1961. // the commands.
  1962. // On the other hand joining items where possible will usually be better up to a certain
  1963. // number where the cost of software transform is higher than separate drawcalls with hardware
  1964. // transform.
  1965. // if there are more than this number of commands in the item, we
  1966. // don't allow joining (separate state changes, and hardware transform)
  1967. // This is set to quite a conservative (low) number until investigated properly.
  1968. // const int MAX_JOIN_ITEM_COMMANDS = 16;
  1969. if (command_count > bdata.settings_max_join_item_commands) {
  1970. return true;
  1971. } else {
  1972. Item::Command *const *commands = p_ci->commands.ptr();
  1973. // do as many commands as possible until the vertex buffer will be full up
  1974. for (int command_num = 0; command_num < command_count; command_num++) {
  1975. Item::Command *command = commands[command_num];
  1976. CRASH_COND(!command);
  1977. switch (command->type) {
  1978. default: {
  1979. return true;
  1980. } break;
  1981. case Item::Command::TYPE_RECT:
  1982. case Item::Command::TYPE_TRANSFORM: {
  1983. } break;
  1984. } // switch
  1985. } // for through commands
  1986. } // else
  1987. return false;
  1988. }
  1989. // Legacy non-batched implementation for regression testing.
  1990. // Should be removed after testing phase to avoid duplicate codepaths.
  1991. void RasterizerCanvasGLES2::_canvas_render_item(Item *p_ci, RenderItemState &r_ris) {
  1992. storage->info.render._2d_item_count++;
  1993. if (r_ris.current_clip != p_ci->final_clip_owner) {
  1994. r_ris.current_clip = p_ci->final_clip_owner;
  1995. if (r_ris.current_clip) {
  1996. glEnable(GL_SCISSOR_TEST);
  1997. int y = storage->frame.current_rt->height - (r_ris.current_clip->final_clip_rect.position.y + r_ris.current_clip->final_clip_rect.size.y);
  1998. if (storage->frame.current_rt->flags[RasterizerStorage::RENDER_TARGET_VFLIP])
  1999. y = r_ris.current_clip->final_clip_rect.position.y;
  2000. glScissor(r_ris.current_clip->final_clip_rect.position.x, y, r_ris.current_clip->final_clip_rect.size.width, r_ris.current_clip->final_clip_rect.size.height);
  2001. } else {
  2002. glDisable(GL_SCISSOR_TEST);
  2003. }
  2004. }
  2005. // TODO: copy back buffer
  2006. if (p_ci->copy_back_buffer) {
  2007. if (p_ci->copy_back_buffer->full) {
  2008. _copy_texscreen(Rect2());
  2009. } else {
  2010. _copy_texscreen(p_ci->copy_back_buffer->rect);
  2011. }
  2012. }
  2013. RasterizerStorageGLES2::Skeleton *skeleton = NULL;
  2014. {
  2015. //skeleton handling
  2016. if (p_ci->skeleton.is_valid() && storage->skeleton_owner.owns(p_ci->skeleton)) {
  2017. skeleton = storage->skeleton_owner.get(p_ci->skeleton);
  2018. if (!skeleton->use_2d) {
  2019. skeleton = NULL;
  2020. } else {
  2021. state.skeleton_transform = r_ris.item_group_base_transform * skeleton->base_transform_2d;
  2022. state.skeleton_transform_inverse = state.skeleton_transform.affine_inverse();
  2023. state.skeleton_texture_size = Vector2(skeleton->size * 2, 0);
  2024. }
  2025. }
  2026. bool use_skeleton = skeleton != NULL;
  2027. if (r_ris.prev_use_skeleton != use_skeleton) {
  2028. r_ris.rebind_shader = true;
  2029. state.canvas_shader.set_conditional(CanvasShaderGLES2::USE_SKELETON, use_skeleton);
  2030. r_ris.prev_use_skeleton = use_skeleton;
  2031. }
  2032. if (skeleton) {
  2033. glActiveTexture(GL_TEXTURE0 + storage->config.max_texture_image_units - 3);
  2034. glBindTexture(GL_TEXTURE_2D, skeleton->tex_id);
  2035. state.using_skeleton = true;
  2036. } else {
  2037. state.using_skeleton = false;
  2038. }
  2039. }
  2040. Item *material_owner = p_ci->material_owner ? p_ci->material_owner : p_ci;
  2041. RID material = material_owner->material;
  2042. RasterizerStorageGLES2::Material *material_ptr = storage->material_owner.getornull(material);
  2043. if (material != r_ris.canvas_last_material || r_ris.rebind_shader) {
  2044. RasterizerStorageGLES2::Shader *shader_ptr = NULL;
  2045. if (material_ptr) {
  2046. shader_ptr = material_ptr->shader;
  2047. if (shader_ptr && shader_ptr->mode != VS::SHADER_CANVAS_ITEM) {
  2048. shader_ptr = NULL; // not a canvas item shader, don't use.
  2049. }
  2050. }
  2051. if (shader_ptr) {
  2052. if (shader_ptr->canvas_item.uses_screen_texture) {
  2053. if (!state.canvas_texscreen_used) {
  2054. //copy if not copied before
  2055. _copy_texscreen(Rect2());
  2056. // blend mode will have been enabled so make sure we disable it again later on
  2057. //last_blend_mode = last_blend_mode != RasterizerStorageGLES2::Shader::CanvasItem::BLEND_MODE_DISABLED ? last_blend_mode : -1;
  2058. }
  2059. if (storage->frame.current_rt->copy_screen_effect.color) {
  2060. glActiveTexture(GL_TEXTURE0 + storage->config.max_texture_image_units - 4);
  2061. glBindTexture(GL_TEXTURE_2D, storage->frame.current_rt->copy_screen_effect.color);
  2062. }
  2063. }
  2064. if (shader_ptr != r_ris.shader_cache) {
  2065. if (shader_ptr->canvas_item.uses_time) {
  2066. VisualServerRaster::redraw_request();
  2067. }
  2068. state.canvas_shader.set_custom_shader(shader_ptr->custom_code_id);
  2069. state.canvas_shader.bind();
  2070. }
  2071. int tc = material_ptr->textures.size();
  2072. Pair<StringName, RID> *textures = material_ptr->textures.ptrw();
  2073. ShaderLanguage::ShaderNode::Uniform::Hint *texture_hints = shader_ptr->texture_hints.ptrw();
  2074. for (int i = 0; i < tc; i++) {
  2075. glActiveTexture(GL_TEXTURE0 + i);
  2076. RasterizerStorageGLES2::Texture *t = storage->texture_owner.getornull(textures[i].second);
  2077. if (!t) {
  2078. switch (texture_hints[i]) {
  2079. case ShaderLanguage::ShaderNode::Uniform::HINT_BLACK_ALBEDO:
  2080. case ShaderLanguage::ShaderNode::Uniform::HINT_BLACK: {
  2081. glBindTexture(GL_TEXTURE_2D, storage->resources.black_tex);
  2082. } break;
  2083. case ShaderLanguage::ShaderNode::Uniform::HINT_ANISO: {
  2084. glBindTexture(GL_TEXTURE_2D, storage->resources.aniso_tex);
  2085. } break;
  2086. case ShaderLanguage::ShaderNode::Uniform::HINT_NORMAL: {
  2087. glBindTexture(GL_TEXTURE_2D, storage->resources.normal_tex);
  2088. } break;
  2089. default: {
  2090. glBindTexture(GL_TEXTURE_2D, storage->resources.white_tex);
  2091. } break;
  2092. }
  2093. continue;
  2094. }
  2095. if (t->redraw_if_visible) {
  2096. VisualServerRaster::redraw_request();
  2097. }
  2098. t = t->get_ptr();
  2099. #ifdef TOOLS_ENABLED
  2100. if (t->detect_normal && texture_hints[i] == ShaderLanguage::ShaderNode::Uniform::HINT_NORMAL) {
  2101. t->detect_normal(t->detect_normal_ud);
  2102. }
  2103. #endif
  2104. if (t->render_target)
  2105. t->render_target->used_in_frame = true;
  2106. glBindTexture(t->target, t->tex_id);
  2107. }
  2108. } else {
  2109. state.canvas_shader.set_custom_shader(0);
  2110. state.canvas_shader.bind();
  2111. }
  2112. state.canvas_shader.use_material((void *)material_ptr);
  2113. r_ris.shader_cache = shader_ptr;
  2114. r_ris.canvas_last_material = material;
  2115. r_ris.rebind_shader = false;
  2116. }
  2117. int blend_mode = r_ris.shader_cache ? r_ris.shader_cache->canvas_item.blend_mode : RasterizerStorageGLES2::Shader::CanvasItem::BLEND_MODE_MIX;
  2118. bool unshaded = r_ris.shader_cache && (r_ris.shader_cache->canvas_item.light_mode == RasterizerStorageGLES2::Shader::CanvasItem::LIGHT_MODE_UNSHADED || (blend_mode != RasterizerStorageGLES2::Shader::CanvasItem::BLEND_MODE_MIX && blend_mode != RasterizerStorageGLES2::Shader::CanvasItem::BLEND_MODE_PMALPHA));
  2119. bool reclip = false;
  2120. if (r_ris.last_blend_mode != blend_mode) {
  2121. switch (blend_mode) {
  2122. case RasterizerStorageGLES2::Shader::CanvasItem::BLEND_MODE_MIX: {
  2123. glBlendEquation(GL_FUNC_ADD);
  2124. if (storage->frame.current_rt && storage->frame.current_rt->flags[RasterizerStorage::RENDER_TARGET_TRANSPARENT]) {
  2125. glBlendFuncSeparate(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA, GL_ONE, GL_ONE_MINUS_SRC_ALPHA);
  2126. } else {
  2127. glBlendFuncSeparate(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA, GL_ZERO, GL_ONE);
  2128. }
  2129. } break;
  2130. case RasterizerStorageGLES2::Shader::CanvasItem::BLEND_MODE_ADD: {
  2131. glBlendEquation(GL_FUNC_ADD);
  2132. if (storage->frame.current_rt && storage->frame.current_rt->flags[RasterizerStorage::RENDER_TARGET_TRANSPARENT]) {
  2133. glBlendFuncSeparate(GL_SRC_ALPHA, GL_ONE, GL_SRC_ALPHA, GL_ONE);
  2134. } else {
  2135. glBlendFuncSeparate(GL_SRC_ALPHA, GL_ONE, GL_ZERO, GL_ONE);
  2136. }
  2137. } break;
  2138. case RasterizerStorageGLES2::Shader::CanvasItem::BLEND_MODE_SUB: {
  2139. glBlendEquation(GL_FUNC_REVERSE_SUBTRACT);
  2140. if (storage->frame.current_rt && storage->frame.current_rt->flags[RasterizerStorage::RENDER_TARGET_TRANSPARENT]) {
  2141. glBlendFuncSeparate(GL_SRC_ALPHA, GL_ONE, GL_SRC_ALPHA, GL_ONE);
  2142. } else {
  2143. glBlendFuncSeparate(GL_SRC_ALPHA, GL_ONE, GL_ZERO, GL_ONE);
  2144. }
  2145. } break;
  2146. case RasterizerStorageGLES2::Shader::CanvasItem::BLEND_MODE_MUL: {
  2147. glBlendEquation(GL_FUNC_ADD);
  2148. if (storage->frame.current_rt && storage->frame.current_rt->flags[RasterizerStorage::RENDER_TARGET_TRANSPARENT]) {
  2149. glBlendFuncSeparate(GL_DST_COLOR, GL_ZERO, GL_DST_ALPHA, GL_ZERO);
  2150. } else {
  2151. glBlendFuncSeparate(GL_DST_COLOR, GL_ZERO, GL_ZERO, GL_ONE);
  2152. }
  2153. } break;
  2154. case RasterizerStorageGLES2::Shader::CanvasItem::BLEND_MODE_PMALPHA: {
  2155. glBlendEquation(GL_FUNC_ADD);
  2156. if (storage->frame.current_rt && storage->frame.current_rt->flags[RasterizerStorage::RENDER_TARGET_TRANSPARENT]) {
  2157. glBlendFuncSeparate(GL_ONE, GL_ONE_MINUS_SRC_ALPHA, GL_ONE, GL_ONE_MINUS_SRC_ALPHA);
  2158. } else {
  2159. glBlendFuncSeparate(GL_ONE, GL_ONE_MINUS_SRC_ALPHA, GL_ZERO, GL_ONE);
  2160. }
  2161. } break;
  2162. }
  2163. }
  2164. state.uniforms.final_modulate = unshaded ? p_ci->final_modulate : Color(p_ci->final_modulate.r * r_ris.item_group_modulate.r, p_ci->final_modulate.g * r_ris.item_group_modulate.g, p_ci->final_modulate.b * r_ris.item_group_modulate.b, p_ci->final_modulate.a * r_ris.item_group_modulate.a);
  2165. state.uniforms.modelview_matrix = p_ci->final_transform;
  2166. state.uniforms.extra_matrix = Transform2D();
  2167. _set_uniforms();
  2168. if (unshaded || (state.uniforms.final_modulate.a > 0.001 && (!r_ris.shader_cache || r_ris.shader_cache->canvas_item.light_mode != RasterizerStorageGLES2::Shader::CanvasItem::LIGHT_MODE_LIGHT_ONLY) && !p_ci->light_masked))
  2169. _canvas_item_render_commands(p_ci, NULL, reclip, material_ptr);
  2170. r_ris.rebind_shader = true; // hacked in for now.
  2171. if ((blend_mode == RasterizerStorageGLES2::Shader::CanvasItem::BLEND_MODE_MIX || blend_mode == RasterizerStorageGLES2::Shader::CanvasItem::BLEND_MODE_PMALPHA) && r_ris.item_group_light && !unshaded) {
  2172. Light *light = r_ris.item_group_light;
  2173. bool light_used = false;
  2174. VS::CanvasLightMode mode = VS::CANVAS_LIGHT_MODE_ADD;
  2175. state.uniforms.final_modulate = p_ci->final_modulate; // remove the canvas modulate
  2176. while (light) {
  2177. if (p_ci->light_mask & light->item_mask && r_ris.item_group_z >= light->z_min && r_ris.item_group_z <= light->z_max && p_ci->global_rect_cache.intersects_transformed(light->xform_cache, light->rect_cache)) {
  2178. //intersects this light
  2179. if (!light_used || mode != light->mode) {
  2180. mode = light->mode;
  2181. switch (mode) {
  2182. case VS::CANVAS_LIGHT_MODE_ADD: {
  2183. glBlendEquation(GL_FUNC_ADD);
  2184. glBlendFunc(GL_SRC_ALPHA, GL_ONE);
  2185. } break;
  2186. case VS::CANVAS_LIGHT_MODE_SUB: {
  2187. glBlendEquation(GL_FUNC_REVERSE_SUBTRACT);
  2188. glBlendFunc(GL_SRC_ALPHA, GL_ONE);
  2189. } break;
  2190. case VS::CANVAS_LIGHT_MODE_MIX:
  2191. case VS::CANVAS_LIGHT_MODE_MASK: {
  2192. glBlendEquation(GL_FUNC_ADD);
  2193. glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
  2194. } break;
  2195. }
  2196. }
  2197. if (!light_used) {
  2198. state.canvas_shader.set_conditional(CanvasShaderGLES2::USE_LIGHTING, true);
  2199. light_used = true;
  2200. }
  2201. bool has_shadow = light->shadow_buffer.is_valid() && p_ci->light_mask & light->item_shadow_mask;
  2202. state.canvas_shader.set_conditional(CanvasShaderGLES2::USE_SHADOWS, has_shadow);
  2203. if (has_shadow) {
  2204. state.canvas_shader.set_conditional(CanvasShaderGLES2::SHADOW_USE_GRADIENT, light->shadow_gradient_length > 0);
  2205. state.canvas_shader.set_conditional(CanvasShaderGLES2::SHADOW_FILTER_NEAREST, light->shadow_filter == VS::CANVAS_LIGHT_FILTER_NONE);
  2206. state.canvas_shader.set_conditional(CanvasShaderGLES2::SHADOW_FILTER_PCF3, light->shadow_filter == VS::CANVAS_LIGHT_FILTER_PCF3);
  2207. state.canvas_shader.set_conditional(CanvasShaderGLES2::SHADOW_FILTER_PCF5, light->shadow_filter == VS::CANVAS_LIGHT_FILTER_PCF5);
  2208. state.canvas_shader.set_conditional(CanvasShaderGLES2::SHADOW_FILTER_PCF7, light->shadow_filter == VS::CANVAS_LIGHT_FILTER_PCF7);
  2209. state.canvas_shader.set_conditional(CanvasShaderGLES2::SHADOW_FILTER_PCF9, light->shadow_filter == VS::CANVAS_LIGHT_FILTER_PCF9);
  2210. state.canvas_shader.set_conditional(CanvasShaderGLES2::SHADOW_FILTER_PCF13, light->shadow_filter == VS::CANVAS_LIGHT_FILTER_PCF13);
  2211. }
  2212. state.canvas_shader.bind();
  2213. state.using_light = light;
  2214. state.using_shadow = has_shadow;
  2215. //always re-set uniforms, since light parameters changed
  2216. _set_uniforms();
  2217. state.canvas_shader.use_material((void *)material_ptr);
  2218. glActiveTexture(GL_TEXTURE0 + storage->config.max_texture_image_units - 4);
  2219. RasterizerStorageGLES2::Texture *t = storage->texture_owner.getornull(light->texture);
  2220. if (!t) {
  2221. glBindTexture(GL_TEXTURE_2D, storage->resources.white_tex);
  2222. } else {
  2223. t = t->get_ptr();
  2224. glBindTexture(t->target, t->tex_id);
  2225. }
  2226. glActiveTexture(GL_TEXTURE0);
  2227. _canvas_item_render_commands(p_ci, NULL, reclip, material_ptr); //redraw using light
  2228. state.using_light = NULL;
  2229. }
  2230. light = light->next_ptr;
  2231. }
  2232. if (light_used) {
  2233. state.canvas_shader.set_conditional(CanvasShaderGLES2::USE_LIGHTING, false);
  2234. state.canvas_shader.set_conditional(CanvasShaderGLES2::USE_SHADOWS, false);
  2235. state.canvas_shader.set_conditional(CanvasShaderGLES2::SHADOW_FILTER_NEAREST, false);
  2236. state.canvas_shader.set_conditional(CanvasShaderGLES2::SHADOW_FILTER_PCF3, false);
  2237. state.canvas_shader.set_conditional(CanvasShaderGLES2::SHADOW_FILTER_PCF5, false);
  2238. state.canvas_shader.set_conditional(CanvasShaderGLES2::SHADOW_FILTER_PCF7, false);
  2239. state.canvas_shader.set_conditional(CanvasShaderGLES2::SHADOW_FILTER_PCF9, false);
  2240. state.canvas_shader.set_conditional(CanvasShaderGLES2::SHADOW_FILTER_PCF13, false);
  2241. state.canvas_shader.bind();
  2242. r_ris.last_blend_mode = -1;
  2243. /*
  2244. //this is set again, so it should not be needed anyway?
  2245. state.canvas_item_modulate = unshaded ? ci->final_modulate : Color(
  2246. ci->final_modulate.r * p_modulate.r,
  2247. ci->final_modulate.g * p_modulate.g,
  2248. ci->final_modulate.b * p_modulate.b,
  2249. ci->final_modulate.a * p_modulate.a );
  2250. state.canvas_shader.set_uniform(CanvasShaderGLES2::MODELVIEW_MATRIX,state.final_transform);
  2251. state.canvas_shader.set_uniform(CanvasShaderGLES2::EXTRA_MATRIX,Transform2D());
  2252. state.canvas_shader.set_uniform(CanvasShaderGLES2::FINAL_MODULATE,state.canvas_item_modulate);
  2253. glBlendEquation(GL_FUNC_ADD);
  2254. if (storage->frame.current_rt->flags[RasterizerStorage::RENDER_TARGET_TRANSPARENT]) {
  2255. glBlendFuncSeparate(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA, GL_ONE, GL_ONE_MINUS_SRC_ALPHA);
  2256. } else {
  2257. glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
  2258. }
  2259. //@TODO RESET canvas_blend_mode
  2260. */
  2261. }
  2262. }
  2263. if (reclip) {
  2264. glEnable(GL_SCISSOR_TEST);
  2265. int y = storage->frame.current_rt->height - (r_ris.current_clip->final_clip_rect.position.y + r_ris.current_clip->final_clip_rect.size.y);
  2266. if (storage->frame.current_rt->flags[RasterizerStorage::RENDER_TARGET_VFLIP])
  2267. y = r_ris.current_clip->final_clip_rect.position.y;
  2268. glScissor(r_ris.current_clip->final_clip_rect.position.x, y, r_ris.current_clip->final_clip_rect.size.width, r_ris.current_clip->final_clip_rect.size.height);
  2269. }
  2270. }
  2271. void RasterizerCanvasGLES2::render_joined_item(const BItemJoined &p_bij, RenderItemState &r_ris) {
  2272. storage->info.render._2d_item_count++;
  2273. #ifdef DEBUG_ENABLED
  2274. if (bdata.diagnose_frame) {
  2275. bdata.frame_string += "\tjoined_item " + itos(p_bij.num_item_refs) + " refs\n";
  2276. if (p_bij.z_index != 0) {
  2277. bdata.frame_string += "\t\t(z " + itos(p_bij.z_index) + ")\n";
  2278. }
  2279. }
  2280. #endif
  2281. // this must be reset for each joined item,
  2282. // it only exists to prevent capturing the screen more than once per item
  2283. state.canvas_texscreen_used = false;
  2284. // all the joined items will share the same state with the first item
  2285. Item *ci = bdata.item_refs[p_bij.first_item_ref].item;
  2286. if (r_ris.current_clip != ci->final_clip_owner) {
  2287. r_ris.current_clip = ci->final_clip_owner;
  2288. if (r_ris.current_clip) {
  2289. glEnable(GL_SCISSOR_TEST);
  2290. int y = storage->frame.current_rt->height - (r_ris.current_clip->final_clip_rect.position.y + r_ris.current_clip->final_clip_rect.size.y);
  2291. if (storage->frame.current_rt->flags[RasterizerStorage::RENDER_TARGET_VFLIP])
  2292. y = r_ris.current_clip->final_clip_rect.position.y;
  2293. glScissor(r_ris.current_clip->final_clip_rect.position.x, y, r_ris.current_clip->final_clip_rect.size.width, r_ris.current_clip->final_clip_rect.size.height);
  2294. } else {
  2295. glDisable(GL_SCISSOR_TEST);
  2296. }
  2297. }
  2298. // TODO: copy back buffer
  2299. if (ci->copy_back_buffer) {
  2300. if (ci->copy_back_buffer->full) {
  2301. _copy_texscreen(Rect2());
  2302. } else {
  2303. _copy_texscreen(ci->copy_back_buffer->rect);
  2304. }
  2305. }
  2306. RasterizerStorageGLES2::Skeleton *skeleton = NULL;
  2307. {
  2308. //skeleton handling
  2309. if (ci->skeleton.is_valid() && storage->skeleton_owner.owns(ci->skeleton)) {
  2310. skeleton = storage->skeleton_owner.get(ci->skeleton);
  2311. if (!skeleton->use_2d) {
  2312. skeleton = NULL;
  2313. } else {
  2314. state.skeleton_transform = r_ris.item_group_base_transform * skeleton->base_transform_2d;
  2315. state.skeleton_transform_inverse = state.skeleton_transform.affine_inverse();
  2316. state.skeleton_texture_size = Vector2(skeleton->size * 2, 0);
  2317. }
  2318. }
  2319. bool use_skeleton = skeleton != NULL;
  2320. if (r_ris.prev_use_skeleton != use_skeleton) {
  2321. r_ris.rebind_shader = true;
  2322. state.canvas_shader.set_conditional(CanvasShaderGLES2::USE_SKELETON, use_skeleton);
  2323. r_ris.prev_use_skeleton = use_skeleton;
  2324. }
  2325. if (skeleton) {
  2326. glActiveTexture(GL_TEXTURE0 + storage->config.max_texture_image_units - 3);
  2327. glBindTexture(GL_TEXTURE_2D, skeleton->tex_id);
  2328. state.using_skeleton = true;
  2329. } else {
  2330. state.using_skeleton = false;
  2331. }
  2332. }
  2333. Item *material_owner = ci->material_owner ? ci->material_owner : ci;
  2334. RID material = material_owner->material;
  2335. RasterizerStorageGLES2::Material *material_ptr = storage->material_owner.getornull(material);
  2336. if (material != r_ris.canvas_last_material || r_ris.rebind_shader) {
  2337. RasterizerStorageGLES2::Shader *shader_ptr = NULL;
  2338. if (material_ptr) {
  2339. shader_ptr = material_ptr->shader;
  2340. if (shader_ptr && shader_ptr->mode != VS::SHADER_CANVAS_ITEM) {
  2341. shader_ptr = NULL; // not a canvas item shader, don't use.
  2342. }
  2343. }
  2344. if (shader_ptr) {
  2345. if (shader_ptr->canvas_item.uses_screen_texture) {
  2346. if (!state.canvas_texscreen_used) {
  2347. //copy if not copied before
  2348. _copy_texscreen(Rect2());
  2349. // blend mode will have been enabled so make sure we disable it again later on
  2350. //last_blend_mode = last_blend_mode != RasterizerStorageGLES2::Shader::CanvasItem::BLEND_MODE_DISABLED ? last_blend_mode : -1;
  2351. }
  2352. if (storage->frame.current_rt->copy_screen_effect.color) {
  2353. glActiveTexture(GL_TEXTURE0 + storage->config.max_texture_image_units - 4);
  2354. glBindTexture(GL_TEXTURE_2D, storage->frame.current_rt->copy_screen_effect.color);
  2355. }
  2356. }
  2357. if (shader_ptr != r_ris.shader_cache) {
  2358. if (shader_ptr->canvas_item.uses_time) {
  2359. VisualServerRaster::redraw_request();
  2360. }
  2361. state.canvas_shader.set_custom_shader(shader_ptr->custom_code_id);
  2362. state.canvas_shader.bind();
  2363. }
  2364. int tc = material_ptr->textures.size();
  2365. Pair<StringName, RID> *textures = material_ptr->textures.ptrw();
  2366. ShaderLanguage::ShaderNode::Uniform::Hint *texture_hints = shader_ptr->texture_hints.ptrw();
  2367. for (int i = 0; i < tc; i++) {
  2368. glActiveTexture(GL_TEXTURE0 + i);
  2369. RasterizerStorageGLES2::Texture *t = storage->texture_owner.getornull(textures[i].second);
  2370. if (!t) {
  2371. switch (texture_hints[i]) {
  2372. case ShaderLanguage::ShaderNode::Uniform::HINT_BLACK_ALBEDO:
  2373. case ShaderLanguage::ShaderNode::Uniform::HINT_BLACK: {
  2374. glBindTexture(GL_TEXTURE_2D, storage->resources.black_tex);
  2375. } break;
  2376. case ShaderLanguage::ShaderNode::Uniform::HINT_ANISO: {
  2377. glBindTexture(GL_TEXTURE_2D, storage->resources.aniso_tex);
  2378. } break;
  2379. case ShaderLanguage::ShaderNode::Uniform::HINT_NORMAL: {
  2380. glBindTexture(GL_TEXTURE_2D, storage->resources.normal_tex);
  2381. } break;
  2382. default: {
  2383. glBindTexture(GL_TEXTURE_2D, storage->resources.white_tex);
  2384. } break;
  2385. }
  2386. continue;
  2387. }
  2388. if (t->redraw_if_visible) {
  2389. VisualServerRaster::redraw_request();
  2390. }
  2391. t = t->get_ptr();
  2392. #ifdef TOOLS_ENABLED
  2393. if (t->detect_normal && texture_hints[i] == ShaderLanguage::ShaderNode::Uniform::HINT_NORMAL) {
  2394. t->detect_normal(t->detect_normal_ud);
  2395. }
  2396. #endif
  2397. if (t->render_target)
  2398. t->render_target->used_in_frame = true;
  2399. glBindTexture(t->target, t->tex_id);
  2400. }
  2401. } else {
  2402. state.canvas_shader.set_custom_shader(0);
  2403. state.canvas_shader.bind();
  2404. }
  2405. state.canvas_shader.use_material((void *)material_ptr);
  2406. r_ris.shader_cache = shader_ptr;
  2407. r_ris.canvas_last_material = material;
  2408. r_ris.rebind_shader = false;
  2409. }
  2410. int blend_mode = r_ris.shader_cache ? r_ris.shader_cache->canvas_item.blend_mode : RasterizerStorageGLES2::Shader::CanvasItem::BLEND_MODE_MIX;
  2411. bool unshaded = r_ris.shader_cache && (r_ris.shader_cache->canvas_item.light_mode == RasterizerStorageGLES2::Shader::CanvasItem::LIGHT_MODE_UNSHADED || (blend_mode != RasterizerStorageGLES2::Shader::CanvasItem::BLEND_MODE_MIX && blend_mode != RasterizerStorageGLES2::Shader::CanvasItem::BLEND_MODE_PMALPHA));
  2412. bool reclip = false;
  2413. if (r_ris.last_blend_mode != blend_mode) {
  2414. switch (blend_mode) {
  2415. case RasterizerStorageGLES2::Shader::CanvasItem::BLEND_MODE_MIX: {
  2416. glBlendEquation(GL_FUNC_ADD);
  2417. if (storage->frame.current_rt && storage->frame.current_rt->flags[RasterizerStorage::RENDER_TARGET_TRANSPARENT]) {
  2418. glBlendFuncSeparate(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA, GL_ONE, GL_ONE_MINUS_SRC_ALPHA);
  2419. } else {
  2420. glBlendFuncSeparate(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA, GL_ZERO, GL_ONE);
  2421. }
  2422. } break;
  2423. case RasterizerStorageGLES2::Shader::CanvasItem::BLEND_MODE_ADD: {
  2424. glBlendEquation(GL_FUNC_ADD);
  2425. if (storage->frame.current_rt && storage->frame.current_rt->flags[RasterizerStorage::RENDER_TARGET_TRANSPARENT]) {
  2426. glBlendFuncSeparate(GL_SRC_ALPHA, GL_ONE, GL_SRC_ALPHA, GL_ONE);
  2427. } else {
  2428. glBlendFuncSeparate(GL_SRC_ALPHA, GL_ONE, GL_ZERO, GL_ONE);
  2429. }
  2430. } break;
  2431. case RasterizerStorageGLES2::Shader::CanvasItem::BLEND_MODE_SUB: {
  2432. glBlendEquation(GL_FUNC_REVERSE_SUBTRACT);
  2433. if (storage->frame.current_rt && storage->frame.current_rt->flags[RasterizerStorage::RENDER_TARGET_TRANSPARENT]) {
  2434. glBlendFuncSeparate(GL_SRC_ALPHA, GL_ONE, GL_SRC_ALPHA, GL_ONE);
  2435. } else {
  2436. glBlendFuncSeparate(GL_SRC_ALPHA, GL_ONE, GL_ZERO, GL_ONE);
  2437. }
  2438. } break;
  2439. case RasterizerStorageGLES2::Shader::CanvasItem::BLEND_MODE_MUL: {
  2440. glBlendEquation(GL_FUNC_ADD);
  2441. if (storage->frame.current_rt && storage->frame.current_rt->flags[RasterizerStorage::RENDER_TARGET_TRANSPARENT]) {
  2442. glBlendFuncSeparate(GL_DST_COLOR, GL_ZERO, GL_DST_ALPHA, GL_ZERO);
  2443. } else {
  2444. glBlendFuncSeparate(GL_DST_COLOR, GL_ZERO, GL_ZERO, GL_ONE);
  2445. }
  2446. } break;
  2447. case RasterizerStorageGLES2::Shader::CanvasItem::BLEND_MODE_PMALPHA: {
  2448. glBlendEquation(GL_FUNC_ADD);
  2449. if (storage->frame.current_rt && storage->frame.current_rt->flags[RasterizerStorage::RENDER_TARGET_TRANSPARENT]) {
  2450. glBlendFuncSeparate(GL_ONE, GL_ONE_MINUS_SRC_ALPHA, GL_ONE, GL_ONE_MINUS_SRC_ALPHA);
  2451. } else {
  2452. glBlendFuncSeparate(GL_ONE, GL_ONE_MINUS_SRC_ALPHA, GL_ZERO, GL_ONE);
  2453. }
  2454. } break;
  2455. }
  2456. }
  2457. // using software transform
  2458. if (!p_bij.use_hardware_transform()) {
  2459. state.uniforms.modelview_matrix = Transform2D();
  2460. // final_modulate will be baked per item ref so the final_modulate can be an identity color
  2461. state.uniforms.final_modulate = Color(1, 1, 1, 1);
  2462. } else {
  2463. state.uniforms.modelview_matrix = ci->final_transform;
  2464. // could use the stored version of final_modulate in item ref? Test which is faster NYI
  2465. state.uniforms.final_modulate = unshaded ? ci->final_modulate : (ci->final_modulate * r_ris.item_group_modulate);
  2466. }
  2467. state.uniforms.extra_matrix = Transform2D();
  2468. _set_uniforms();
  2469. if (unshaded || (state.uniforms.final_modulate.a > 0.001 && (!r_ris.shader_cache || r_ris.shader_cache->canvas_item.light_mode != RasterizerStorageGLES2::Shader::CanvasItem::LIGHT_MODE_LIGHT_ONLY) && !ci->light_masked))
  2470. render_joined_item_commands(p_bij, NULL, reclip, material_ptr, false);
  2471. r_ris.rebind_shader = true; // hacked in for now.
  2472. if ((blend_mode == RasterizerStorageGLES2::Shader::CanvasItem::BLEND_MODE_MIX || blend_mode == RasterizerStorageGLES2::Shader::CanvasItem::BLEND_MODE_PMALPHA) && r_ris.item_group_light && !unshaded) {
  2473. Light *light = r_ris.item_group_light;
  2474. bool light_used = false;
  2475. VS::CanvasLightMode mode = VS::CANVAS_LIGHT_MODE_ADD;
  2476. // we leave this set to 1, 1, 1, 1 if using software because the colors are baked into the vertices
  2477. if (p_bij.use_hardware_transform()) {
  2478. state.uniforms.final_modulate = ci->final_modulate; // remove the canvas modulate
  2479. }
  2480. while (light) {
  2481. // use the bounding rect of the joined items, NOT only the bounding rect of the first item.
  2482. // note this is a cost of batching, the light culling will be less effective
  2483. // note that the r_ris.item_group_z will be out of date because we are using deferred rendering till canvas_render_items_end()
  2484. // so we have to test z against the stored value in the joined item
  2485. if (ci->light_mask & light->item_mask && p_bij.z_index >= light->z_min && p_bij.z_index <= light->z_max && p_bij.bounding_rect.intersects_transformed(light->xform_cache, light->rect_cache)) {
  2486. //intersects this light
  2487. if (!light_used || mode != light->mode) {
  2488. mode = light->mode;
  2489. switch (mode) {
  2490. case VS::CANVAS_LIGHT_MODE_ADD: {
  2491. glBlendEquation(GL_FUNC_ADD);
  2492. glBlendFunc(GL_SRC_ALPHA, GL_ONE);
  2493. } break;
  2494. case VS::CANVAS_LIGHT_MODE_SUB: {
  2495. glBlendEquation(GL_FUNC_REVERSE_SUBTRACT);
  2496. glBlendFunc(GL_SRC_ALPHA, GL_ONE);
  2497. } break;
  2498. case VS::CANVAS_LIGHT_MODE_MIX:
  2499. case VS::CANVAS_LIGHT_MODE_MASK: {
  2500. glBlendEquation(GL_FUNC_ADD);
  2501. glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
  2502. } break;
  2503. }
  2504. }
  2505. if (!light_used) {
  2506. state.canvas_shader.set_conditional(CanvasShaderGLES2::USE_LIGHTING, true);
  2507. light_used = true;
  2508. }
  2509. bool has_shadow = light->shadow_buffer.is_valid() && ci->light_mask & light->item_shadow_mask;
  2510. state.canvas_shader.set_conditional(CanvasShaderGLES2::USE_SHADOWS, has_shadow);
  2511. if (has_shadow) {
  2512. state.canvas_shader.set_conditional(CanvasShaderGLES2::SHADOW_USE_GRADIENT, light->shadow_gradient_length > 0);
  2513. state.canvas_shader.set_conditional(CanvasShaderGLES2::SHADOW_FILTER_NEAREST, light->shadow_filter == VS::CANVAS_LIGHT_FILTER_NONE);
  2514. state.canvas_shader.set_conditional(CanvasShaderGLES2::SHADOW_FILTER_PCF3, light->shadow_filter == VS::CANVAS_LIGHT_FILTER_PCF3);
  2515. state.canvas_shader.set_conditional(CanvasShaderGLES2::SHADOW_FILTER_PCF5, light->shadow_filter == VS::CANVAS_LIGHT_FILTER_PCF5);
  2516. state.canvas_shader.set_conditional(CanvasShaderGLES2::SHADOW_FILTER_PCF7, light->shadow_filter == VS::CANVAS_LIGHT_FILTER_PCF7);
  2517. state.canvas_shader.set_conditional(CanvasShaderGLES2::SHADOW_FILTER_PCF9, light->shadow_filter == VS::CANVAS_LIGHT_FILTER_PCF9);
  2518. state.canvas_shader.set_conditional(CanvasShaderGLES2::SHADOW_FILTER_PCF13, light->shadow_filter == VS::CANVAS_LIGHT_FILTER_PCF13);
  2519. }
  2520. state.canvas_shader.bind();
  2521. state.using_light = light;
  2522. state.using_shadow = has_shadow;
  2523. //always re-set uniforms, since light parameters changed
  2524. _set_uniforms();
  2525. state.canvas_shader.use_material((void *)material_ptr);
  2526. glActiveTexture(GL_TEXTURE0 + storage->config.max_texture_image_units - 4);
  2527. RasterizerStorageGLES2::Texture *t = storage->texture_owner.getornull(light->texture);
  2528. if (!t) {
  2529. glBindTexture(GL_TEXTURE_2D, storage->resources.white_tex);
  2530. } else {
  2531. t = t->get_ptr();
  2532. glBindTexture(t->target, t->tex_id);
  2533. }
  2534. glActiveTexture(GL_TEXTURE0);
  2535. // redraw using light.
  2536. // if there is no clip item, we can consider scissoring to the intersection area between the light and the item
  2537. // this can greatly reduce fill rate ..
  2538. // at the cost of glScissor commands, so is optional
  2539. if (!bdata.settings_scissor_lights || r_ris.current_clip) {
  2540. render_joined_item_commands(p_bij, NULL, reclip, material_ptr, true);
  2541. } else {
  2542. bool scissor = _light_scissor_begin(p_bij.bounding_rect, light->xform_cache, light->rect_cache);
  2543. render_joined_item_commands(p_bij, NULL, reclip, material_ptr, true);
  2544. if (scissor) {
  2545. glDisable(GL_SCISSOR_TEST);
  2546. }
  2547. }
  2548. state.using_light = NULL;
  2549. }
  2550. light = light->next_ptr;
  2551. }
  2552. if (light_used) {
  2553. state.canvas_shader.set_conditional(CanvasShaderGLES2::USE_LIGHTING, false);
  2554. state.canvas_shader.set_conditional(CanvasShaderGLES2::USE_SHADOWS, false);
  2555. state.canvas_shader.set_conditional(CanvasShaderGLES2::SHADOW_FILTER_NEAREST, false);
  2556. state.canvas_shader.set_conditional(CanvasShaderGLES2::SHADOW_FILTER_PCF3, false);
  2557. state.canvas_shader.set_conditional(CanvasShaderGLES2::SHADOW_FILTER_PCF5, false);
  2558. state.canvas_shader.set_conditional(CanvasShaderGLES2::SHADOW_FILTER_PCF7, false);
  2559. state.canvas_shader.set_conditional(CanvasShaderGLES2::SHADOW_FILTER_PCF9, false);
  2560. state.canvas_shader.set_conditional(CanvasShaderGLES2::SHADOW_FILTER_PCF13, false);
  2561. state.canvas_shader.bind();
  2562. r_ris.last_blend_mode = -1;
  2563. /*
  2564. //this is set again, so it should not be needed anyway?
  2565. state.canvas_item_modulate = unshaded ? ci->final_modulate : Color(
  2566. ci->final_modulate.r * p_modulate.r,
  2567. ci->final_modulate.g * p_modulate.g,
  2568. ci->final_modulate.b * p_modulate.b,
  2569. ci->final_modulate.a * p_modulate.a );
  2570. state.canvas_shader.set_uniform(CanvasShaderGLES2::MODELVIEW_MATRIX,state.final_transform);
  2571. state.canvas_shader.set_uniform(CanvasShaderGLES2::EXTRA_MATRIX,Transform2D());
  2572. state.canvas_shader.set_uniform(CanvasShaderGLES2::FINAL_MODULATE,state.canvas_item_modulate);
  2573. glBlendEquation(GL_FUNC_ADD);
  2574. if (storage->frame.current_rt->flags[RasterizerStorage::RENDER_TARGET_TRANSPARENT]) {
  2575. glBlendFuncSeparate(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA, GL_ONE, GL_ONE_MINUS_SRC_ALPHA);
  2576. } else {
  2577. glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
  2578. }
  2579. //@TODO RESET canvas_blend_mode
  2580. */
  2581. }
  2582. }
  2583. if (reclip) {
  2584. glEnable(GL_SCISSOR_TEST);
  2585. int y = storage->frame.current_rt->height - (r_ris.current_clip->final_clip_rect.position.y + r_ris.current_clip->final_clip_rect.size.y);
  2586. if (storage->frame.current_rt->flags[RasterizerStorage::RENDER_TARGET_VFLIP])
  2587. y = r_ris.current_clip->final_clip_rect.position.y;
  2588. glScissor(r_ris.current_clip->final_clip_rect.position.x, y, r_ris.current_clip->final_clip_rect.size.width, r_ris.current_clip->final_clip_rect.size.height);
  2589. }
  2590. }
  2591. bool RasterizerCanvasGLES2::_light_find_intersection(const Rect2 &p_item_rect, const Transform2D &p_light_xform, const Rect2 &p_light_rect, Rect2 &r_cliprect) const {
  2592. // transform light to world space (note this is done in the earlier intersection test, so could
  2593. // be made more efficient)
  2594. Vector2 pts[4] = {
  2595. p_light_xform.xform(p_light_rect.position),
  2596. p_light_xform.xform(Vector2(p_light_rect.position.x + p_light_rect.size.x, p_light_rect.position.y)),
  2597. p_light_xform.xform(Vector2(p_light_rect.position.x, p_light_rect.position.y + p_light_rect.size.y)),
  2598. p_light_xform.xform(Vector2(p_light_rect.position.x + p_light_rect.size.x, p_light_rect.position.y + p_light_rect.size.y)),
  2599. };
  2600. // calculate the light bound rect in world space
  2601. Rect2 lrect(pts[0].x, pts[0].y, 0, 0);
  2602. for (int n = 1; n < 4; n++) {
  2603. lrect.expand_to(pts[n]);
  2604. }
  2605. // intersection between the 2 rects
  2606. // they should probably always intersect, because of earlier check, but just in case...
  2607. if (!p_item_rect.intersects(lrect))
  2608. return false;
  2609. // note this does almost the same as Rect2.clip but slightly more efficient for our use case
  2610. r_cliprect.position.x = MAX(p_item_rect.position.x, lrect.position.x);
  2611. r_cliprect.position.y = MAX(p_item_rect.position.y, lrect.position.y);
  2612. Point2 item_rect_end = p_item_rect.position + p_item_rect.size;
  2613. Point2 lrect_end = lrect.position + lrect.size;
  2614. r_cliprect.size.x = MIN(item_rect_end.x, lrect_end.x) - r_cliprect.position.x;
  2615. r_cliprect.size.y = MIN(item_rect_end.y, lrect_end.y) - r_cliprect.position.y;
  2616. return true;
  2617. }
  2618. bool RasterizerCanvasGLES2::_light_scissor_begin(const Rect2 &p_item_rect, const Transform2D &p_light_xform, const Rect2 &p_light_rect) const {
  2619. float area_item = p_item_rect.size.x * p_item_rect.size.y; // double check these are always positive
  2620. // quick reject .. the area of pixels saved can never be more than the area of the item
  2621. if (area_item < bdata.scissor_threshold_area) {
  2622. return false;
  2623. }
  2624. Rect2 cliprect;
  2625. if (!_light_find_intersection(p_item_rect, p_light_xform, p_light_rect, cliprect)) {
  2626. // should not really occur .. but just in case
  2627. cliprect = Rect2(0, 0, 0, 0);
  2628. } else {
  2629. // some conditions not to scissor
  2630. // determine the area (fill rate) that will be saved
  2631. float area_cliprect = cliprect.size.x * cliprect.size.y;
  2632. float area_saved = area_item - area_cliprect;
  2633. // if area saved is too small, don't scissor
  2634. if (area_saved < bdata.scissor_threshold_area) {
  2635. return false;
  2636. }
  2637. }
  2638. glEnable(GL_SCISSOR_TEST);
  2639. int y = storage->frame.current_rt->height - (cliprect.position.y + cliprect.size.y);
  2640. if (storage->frame.current_rt->flags[RasterizerStorage::RENDER_TARGET_VFLIP])
  2641. y = cliprect.position.y;
  2642. glScissor(cliprect.position.x, y, cliprect.size.width, cliprect.size.height);
  2643. return true;
  2644. }
  2645. void RasterizerCanvasGLES2::_calculate_scissor_threshold_area() {
  2646. if (!bdata.settings_scissor_lights) {
  2647. return;
  2648. }
  2649. // scissor area threshold is 0.0 to 1.0 in the settings for ease of use.
  2650. // we need to translate to an absolute area to determine quickly whether
  2651. // to scissor.
  2652. if (bdata.settings_scissor_threshold < 0.0001f) {
  2653. bdata.scissor_threshold_area = -1.0f; // will always pass
  2654. } else {
  2655. // in pixels
  2656. int w = storage->frame.current_rt->width;
  2657. int h = storage->frame.current_rt->height;
  2658. int screen_area = w * h;
  2659. bdata.scissor_threshold_area = bdata.settings_scissor_threshold * screen_area;
  2660. }
  2661. }
  2662. void RasterizerCanvasGLES2::initialize() {
  2663. RasterizerCanvasBaseGLES2::initialize();
  2664. bdata.settings_use_batching = GLOBAL_GET("rendering/batching/options/use_batching");
  2665. bdata.settings_max_join_item_commands = GLOBAL_GET("rendering/batching/parameters/max_join_item_commands");
  2666. bdata.settings_colored_vertex_format_threshold = GLOBAL_GET("rendering/batching/parameters/colored_vertex_format_threshold");
  2667. bdata.settings_item_reordering_lookahead = GLOBAL_GET("rendering/batching/parameters/item_reordering_lookahead");
  2668. bdata.settings_light_max_join_items = GLOBAL_GET("rendering/batching/lights/max_join_items");
  2669. bdata.settings_use_single_rect_fallback = GLOBAL_GET("rendering/batching/options/single_rect_fallback");
  2670. // alternatively only enable uv contract if pixel snap in use,
  2671. // but with this enable bool, it should not be necessary
  2672. bdata.settings_uv_contract = GLOBAL_GET("rendering/batching/precision/uv_contract");
  2673. bdata.settings_uv_contract_amount = (float)GLOBAL_GET("rendering/batching/precision/uv_contract_amount") / 1000000.0f;
  2674. // we can use the threshold to determine whether to turn scissoring off or on
  2675. bdata.settings_scissor_threshold = GLOBAL_GET("rendering/batching/lights/scissor_area_threshold");
  2676. if (bdata.settings_scissor_threshold > 0.999f) {
  2677. bdata.settings_scissor_lights = false;
  2678. } else {
  2679. bdata.settings_scissor_lights = true;
  2680. // apply power of 4 relationship for the area, as most of the important changes
  2681. // will be happening at low values of scissor threshold
  2682. bdata.settings_scissor_threshold *= bdata.settings_scissor_threshold;
  2683. bdata.settings_scissor_threshold *= bdata.settings_scissor_threshold;
  2684. }
  2685. // The sweet spot on my desktop for cache is actually smaller than the max, and this
  2686. // is the default. This saves memory too so we will use it for now, needs testing to see whether this varies according
  2687. // to device / platform.
  2688. bdata.settings_batch_buffer_num_verts = GLOBAL_GET("rendering/batching/parameters/batch_buffer_size");
  2689. // override the use_batching setting in the editor
  2690. // (note that if the editor can't start, you can't change the use_batching project setting!)
  2691. if (Engine::get_singleton()->is_editor_hint()) {
  2692. bool use_in_editor = GLOBAL_GET("rendering/batching/options/use_batching_in_editor");
  2693. bdata.settings_use_batching = use_in_editor;
  2694. // fix some settings in the editor, as the performance not worth the risk
  2695. bdata.settings_use_single_rect_fallback = false;
  2696. }
  2697. // if we are using batching, we will purposefully disable the nvidia workaround.
  2698. // This is because the only reason to use the single rect fallback is the approx 2x speed
  2699. // of the uniform drawing technique. If we used nvidia workaround, speed would be
  2700. // approx equal to the batcher drawing technique (indexed primitive + VB).
  2701. if (bdata.settings_use_batching) {
  2702. use_nvidia_rect_workaround = false;
  2703. }
  2704. // For debugging, if flash is set in project settings, it will flash on alternate frames
  2705. // between the non-batched renderer and the batched renderer,
  2706. // in order to find regressions.
  2707. // This should not be used except during development.
  2708. // make a note of the original choice in case we are flashing on and off the batching
  2709. bdata.settings_use_batching_original_choice = bdata.settings_use_batching;
  2710. bdata.settings_flash_batching = GLOBAL_GET("rendering/batching/debug/flash_batching");
  2711. if (!bdata.settings_use_batching) {
  2712. // no flash when batching turned off
  2713. bdata.settings_flash_batching = false;
  2714. }
  2715. // frame diagnosis. print out the batches every nth frame
  2716. bdata.settings_diagnose_frame = false;
  2717. if (!Engine::get_singleton()->is_editor_hint() && bdata.settings_use_batching) {
  2718. bdata.settings_diagnose_frame = GLOBAL_GET("rendering/batching/debug/diagnose_frame");
  2719. }
  2720. // the maximum num quads in a batch is limited by GLES2. We can have only 16 bit indices,
  2721. // which means we can address a vertex buffer of max size 65535. 4 vertices are needed per quad.
  2722. // Note this determines the memory use by the vertex buffer vector. max quads (65536/4)-1
  2723. // but can be reduced to save memory if really required (will result in more batches though)
  2724. const int max_possible_quads = (65536 / 4) - 1;
  2725. const int min_possible_quads = 8; // some reasonable small value
  2726. // value from project settings
  2727. int max_quads = bdata.settings_batch_buffer_num_verts / 4;
  2728. // sanity checks
  2729. max_quads = CLAMP(max_quads, min_possible_quads, max_possible_quads);
  2730. bdata.settings_max_join_item_commands = CLAMP(bdata.settings_max_join_item_commands, 0, 65535);
  2731. bdata.settings_colored_vertex_format_threshold = CLAMP(bdata.settings_colored_vertex_format_threshold, 0.0f, 1.0f);
  2732. bdata.settings_scissor_threshold = CLAMP(bdata.settings_scissor_threshold, 0.0f, 1.0f);
  2733. bdata.settings_light_max_join_items = CLAMP(bdata.settings_light_max_join_items, 0, 65535);
  2734. bdata.settings_item_reordering_lookahead = CLAMP(bdata.settings_item_reordering_lookahead, 0, 65535);
  2735. // for debug purposes, output a string with the batching options
  2736. String batching_options_string = "OpenGL ES 2.0 Batching: ";
  2737. if (bdata.settings_use_batching) {
  2738. batching_options_string += "ON";
  2739. if (OS::get_singleton()->is_stdout_verbose()) {
  2740. batching_options_string += "\n\tOPTIONS\n";
  2741. batching_options_string += "\tmax_join_item_commands " + itos(bdata.settings_max_join_item_commands) + "\n";
  2742. batching_options_string += "\tcolored_vertex_format_threshold " + String(Variant(bdata.settings_colored_vertex_format_threshold)) + "\n";
  2743. batching_options_string += "\tbatch_buffer_size " + itos(bdata.settings_batch_buffer_num_verts) + "\n";
  2744. batching_options_string += "\tlight_scissor_area_threshold " + String(Variant(bdata.settings_scissor_threshold)) + "\n";
  2745. batching_options_string += "\titem_reordering_lookahead " + itos(bdata.settings_item_reordering_lookahead) + "\n";
  2746. batching_options_string += "\tlight_max_join_items " + itos(bdata.settings_light_max_join_items) + "\n";
  2747. batching_options_string += "\tsingle_rect_fallback " + String(Variant(bdata.settings_use_single_rect_fallback)) + "\n";
  2748. batching_options_string += "\tdebug_flash " + String(Variant(bdata.settings_flash_batching)) + "\n";
  2749. batching_options_string += "\tdiagnose_frame " + String(Variant(bdata.settings_diagnose_frame));
  2750. }
  2751. print_line(batching_options_string);
  2752. }
  2753. // special case, for colored vertex format threshold.
  2754. // as the comparison is >=, we want to be able to totally turn on or off
  2755. // conversion to colored vertex format at the extremes, so we will force
  2756. // 1.0 to be just above 1.0
  2757. if (bdata.settings_colored_vertex_format_threshold > 0.995f) {
  2758. bdata.settings_colored_vertex_format_threshold = 1.01f;
  2759. }
  2760. // save memory when batching off
  2761. if (!bdata.settings_use_batching) {
  2762. max_quads = 0;
  2763. }
  2764. uint32_t sizeof_batch_vert = sizeof(BatchVertex);
  2765. bdata.max_quads = max_quads;
  2766. // 4 verts per quad
  2767. bdata.vertex_buffer_size_units = max_quads * 4;
  2768. // the index buffer can be longer than 65535, only the indices need to be within this range
  2769. bdata.index_buffer_size_units = max_quads * 6;
  2770. // this comes out at approx 64K for non-colored vertex buffer, and 128K for colored vertex buffer
  2771. bdata.vertex_buffer_size_bytes = bdata.vertex_buffer_size_units * sizeof_batch_vert;
  2772. bdata.index_buffer_size_bytes = bdata.index_buffer_size_units * 2; // 16 bit inds
  2773. // create equal number of norma and colored verts (as the normal may need to be translated to colored)
  2774. bdata.vertices.create(bdata.vertex_buffer_size_units); // 512k
  2775. bdata.vertices_colored.create(bdata.vertices.max_size()); // 1024k
  2776. // num batches will be auto increased dynamically if required
  2777. bdata.batches.create(1024);
  2778. bdata.batches_temp.create(bdata.batches.max_size());
  2779. // batch textures can also be increased dynamically
  2780. bdata.batch_textures.create(32);
  2781. // just reserve some space (may not be needed as we are orphaning, but hey ho)
  2782. glGenBuffers(1, &bdata.gl_vertex_buffer);
  2783. if (bdata.vertex_buffer_size_bytes) {
  2784. glBindBuffer(GL_ARRAY_BUFFER, bdata.gl_vertex_buffer);
  2785. glBufferData(GL_ARRAY_BUFFER, bdata.vertex_buffer_size_bytes, NULL, GL_DYNAMIC_DRAW);
  2786. glBindBuffer(GL_ARRAY_BUFFER, 0);
  2787. // pre fill index buffer, the indices never need to change so can be static
  2788. glGenBuffers(1, &bdata.gl_index_buffer);
  2789. glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, bdata.gl_index_buffer);
  2790. Vector<uint16_t> indices;
  2791. indices.resize(bdata.index_buffer_size_units);
  2792. for (int q = 0; q < max_quads; q++) {
  2793. int i_pos = q * 6; // 6 inds per quad
  2794. int q_pos = q * 4; // 4 verts per quad
  2795. indices.set(i_pos, q_pos);
  2796. indices.set(i_pos + 1, q_pos + 1);
  2797. indices.set(i_pos + 2, q_pos + 2);
  2798. indices.set(i_pos + 3, q_pos);
  2799. indices.set(i_pos + 4, q_pos + 2);
  2800. indices.set(i_pos + 5, q_pos + 3);
  2801. // we can only use 16 bit indices in GLES2!
  2802. #ifdef DEBUG_ENABLED
  2803. CRASH_COND((q_pos + 3) > 65535);
  2804. #endif
  2805. }
  2806. glBufferData(GL_ELEMENT_ARRAY_BUFFER, bdata.index_buffer_size_bytes, &indices[0], GL_STATIC_DRAW);
  2807. glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0);
  2808. } // only if there is a vertex buffer (batching is on)
  2809. }
  2810. RasterizerCanvasGLES2::RasterizerCanvasGLES2() {
  2811. bdata.settings_use_batching = false;
  2812. }