rasterizer_canvas_gles2.cpp 89 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414
  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-2021 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2021 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 "drivers/gles2/rasterizer_gles2.h"
  34. #include "drivers/gles_common/rasterizer_asserts.h"
  35. #include "rasterizer_scene_gles2.h"
  36. #include "servers/visual/visual_server_raster.h"
  37. static const GLenum gl_primitive[] = {
  38. GL_POINTS,
  39. GL_LINES,
  40. GL_LINE_STRIP,
  41. GL_LINE_LOOP,
  42. GL_TRIANGLES,
  43. GL_TRIANGLE_STRIP,
  44. GL_TRIANGLE_FAN
  45. };
  46. void RasterizerCanvasGLES2::_batch_upload_buffers() {
  47. // noop?
  48. if (!bdata.vertices.size())
  49. return;
  50. glBindBuffer(GL_ARRAY_BUFFER, bdata.gl_vertex_buffer);
  51. // usage flag is a project setting
  52. GLenum buffer_usage_flag = GL_DYNAMIC_DRAW;
  53. if (bdata.buffer_mode_batch_upload_flag_stream) {
  54. buffer_usage_flag = GL_STREAM_DRAW;
  55. }
  56. // orphan the old (for now)
  57. if (bdata.buffer_mode_batch_upload_send_null) {
  58. glBufferData(GL_ARRAY_BUFFER, 0, 0, buffer_usage_flag); // GL_DYNAMIC_DRAW);
  59. }
  60. switch (bdata.fvf) {
  61. case RasterizerStorageCommon::FVF_UNBATCHED: // should not happen
  62. break;
  63. case RasterizerStorageCommon::FVF_REGULAR: // no change
  64. glBufferData(GL_ARRAY_BUFFER, sizeof(BatchVertex) * bdata.vertices.size(), bdata.vertices.get_data(), buffer_usage_flag);
  65. break;
  66. case RasterizerStorageCommon::FVF_COLOR:
  67. glBufferData(GL_ARRAY_BUFFER, sizeof(BatchVertexColored) * bdata.unit_vertices.size(), bdata.unit_vertices.get_unit(0), buffer_usage_flag);
  68. break;
  69. case RasterizerStorageCommon::FVF_LIGHT_ANGLE:
  70. glBufferData(GL_ARRAY_BUFFER, sizeof(BatchVertexLightAngled) * bdata.unit_vertices.size(), bdata.unit_vertices.get_unit(0), buffer_usage_flag);
  71. break;
  72. case RasterizerStorageCommon::FVF_MODULATED:
  73. glBufferData(GL_ARRAY_BUFFER, sizeof(BatchVertexModulated) * bdata.unit_vertices.size(), bdata.unit_vertices.get_unit(0), buffer_usage_flag);
  74. break;
  75. case RasterizerStorageCommon::FVF_LARGE:
  76. glBufferData(GL_ARRAY_BUFFER, sizeof(BatchVertexLarge) * bdata.unit_vertices.size(), bdata.unit_vertices.get_unit(0), buffer_usage_flag);
  77. break;
  78. }
  79. // might not be necessary
  80. glBindBuffer(GL_ARRAY_BUFFER, 0);
  81. }
  82. void RasterizerCanvasGLES2::_batch_render_lines(const Batch &p_batch, RasterizerStorageGLES2::Material *p_material, bool p_anti_alias) {
  83. _set_texture_rect_mode(false);
  84. if (state.canvas_shader.bind()) {
  85. _set_uniforms();
  86. state.canvas_shader.use_material((void *)p_material);
  87. }
  88. _bind_canvas_texture(RID(), RID());
  89. glDisableVertexAttribArray(VS::ARRAY_COLOR);
  90. glVertexAttrib4fv(VS::ARRAY_COLOR, (float *)&p_batch.color);
  91. #ifdef GLES_OVER_GL
  92. if (p_anti_alias)
  93. glEnable(GL_LINE_SMOOTH);
  94. #endif
  95. int sizeof_vert = sizeof(BatchVertex);
  96. // bind the index and vertex buffer
  97. glBindBuffer(GL_ARRAY_BUFFER, bdata.gl_vertex_buffer);
  98. glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, bdata.gl_index_buffer);
  99. uint64_t pointer = 0;
  100. glVertexAttribPointer(VS::ARRAY_VERTEX, 2, GL_FLOAT, GL_FALSE, sizeof_vert, (const void *)pointer);
  101. glDisableVertexAttribArray(VS::ARRAY_TEX_UV);
  102. int64_t offset = p_batch.first_vert; // 6 inds per quad at 2 bytes each
  103. int num_elements = p_batch.num_commands * 2;
  104. glDrawArrays(GL_LINES, offset, num_elements);
  105. storage->info.render._2d_draw_call_count++;
  106. // may not be necessary .. state change optimization still TODO
  107. glBindBuffer(GL_ARRAY_BUFFER, 0);
  108. glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0);
  109. #ifdef GLES_OVER_GL
  110. if (p_anti_alias)
  111. glDisable(GL_LINE_SMOOTH);
  112. #endif
  113. }
  114. void RasterizerCanvasGLES2::_batch_render_generic(const Batch &p_batch, RasterizerStorageGLES2::Material *p_material) {
  115. ERR_FAIL_COND(p_batch.num_commands <= 0);
  116. const bool &use_light_angles = bdata.use_light_angles;
  117. const bool &use_modulate = bdata.use_modulate;
  118. const bool &use_large_verts = bdata.use_large_verts;
  119. const bool &colored_verts = bdata.use_colored_vertices | use_light_angles | use_modulate | use_large_verts;
  120. int sizeof_vert;
  121. switch (bdata.fvf) {
  122. default:
  123. sizeof_vert = 0; // prevent compiler warning - this should never happen
  124. break;
  125. case RasterizerStorageCommon::FVF_UNBATCHED: {
  126. sizeof_vert = 0; // prevent compiler warning - this should never happen
  127. return;
  128. } break;
  129. case RasterizerStorageCommon::FVF_REGULAR: // no change
  130. sizeof_vert = sizeof(BatchVertex);
  131. break;
  132. case RasterizerStorageCommon::FVF_COLOR:
  133. sizeof_vert = sizeof(BatchVertexColored);
  134. break;
  135. case RasterizerStorageCommon::FVF_LIGHT_ANGLE:
  136. sizeof_vert = sizeof(BatchVertexLightAngled);
  137. break;
  138. case RasterizerStorageCommon::FVF_MODULATED:
  139. sizeof_vert = sizeof(BatchVertexModulated);
  140. break;
  141. case RasterizerStorageCommon::FVF_LARGE:
  142. sizeof_vert = sizeof(BatchVertexLarge);
  143. break;
  144. }
  145. // make sure to set all conditionals BEFORE binding the shader
  146. _set_texture_rect_mode(false, use_light_angles, use_modulate, use_large_verts);
  147. // batch tex
  148. const BatchTex &tex = bdata.batch_textures[p_batch.batch_texture_id];
  149. //VSG::rasterizer->gl_check_for_error();
  150. // force repeat is set if non power of 2 texture, and repeat is needed if hardware doesn't support npot
  151. if (tex.tile_mode == BatchTex::TILE_FORCE_REPEAT) {
  152. state.canvas_shader.set_conditional(CanvasShaderGLES2::USE_FORCE_REPEAT, true);
  153. }
  154. if (state.canvas_shader.bind()) {
  155. _set_uniforms();
  156. state.canvas_shader.use_material((void *)p_material);
  157. }
  158. _bind_canvas_texture(tex.RID_texture, tex.RID_normal);
  159. // bind the index and vertex buffer
  160. glBindBuffer(GL_ARRAY_BUFFER, bdata.gl_vertex_buffer);
  161. glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, bdata.gl_index_buffer);
  162. uint64_t pointer = 0;
  163. glVertexAttribPointer(VS::ARRAY_VERTEX, 2, GL_FLOAT, GL_FALSE, sizeof_vert, (const void *)pointer);
  164. // always send UVs, even within a texture specified because a shader can still use UVs
  165. glEnableVertexAttribArray(VS::ARRAY_TEX_UV);
  166. glVertexAttribPointer(VS::ARRAY_TEX_UV, 2, GL_FLOAT, GL_FALSE, sizeof_vert, CAST_INT_TO_UCHAR_PTR(pointer + (2 * 4)));
  167. // color
  168. if (!colored_verts) {
  169. glDisableVertexAttribArray(VS::ARRAY_COLOR);
  170. glVertexAttrib4fv(VS::ARRAY_COLOR, p_batch.color.get_data());
  171. } else {
  172. glEnableVertexAttribArray(VS::ARRAY_COLOR);
  173. glVertexAttribPointer(VS::ARRAY_COLOR, 4, GL_FLOAT, GL_FALSE, sizeof_vert, CAST_INT_TO_UCHAR_PTR(pointer + (4 * 4)));
  174. }
  175. if (use_light_angles) {
  176. glEnableVertexAttribArray(VS::ARRAY_TANGENT);
  177. glVertexAttribPointer(VS::ARRAY_TANGENT, 1, GL_FLOAT, GL_FALSE, sizeof_vert, CAST_INT_TO_UCHAR_PTR(pointer + (8 * 4)));
  178. }
  179. if (use_modulate) {
  180. glEnableVertexAttribArray(VS::ARRAY_TEX_UV2);
  181. glVertexAttribPointer(VS::ARRAY_TEX_UV2, 4, GL_FLOAT, GL_FALSE, sizeof_vert, CAST_INT_TO_UCHAR_PTR(pointer + (9 * 4)));
  182. }
  183. if (use_large_verts) {
  184. glEnableVertexAttribArray(VS::ARRAY_BONES);
  185. glVertexAttribPointer(VS::ARRAY_BONES, 2, GL_FLOAT, GL_FALSE, sizeof_vert, CAST_INT_TO_UCHAR_PTR(pointer + (13 * 4)));
  186. glEnableVertexAttribArray(VS::ARRAY_WEIGHTS);
  187. glVertexAttribPointer(VS::ARRAY_WEIGHTS, 4, GL_FLOAT, GL_FALSE, sizeof_vert, CAST_INT_TO_UCHAR_PTR(pointer + (15 * 4)));
  188. }
  189. // We only want to set the GL wrapping mode if the texture is not already tiled (i.e. set in Import).
  190. // This is an optimization left over from the legacy renderer.
  191. // If we DID set tiling in the API, and reverted to clamped, then the next draw using this texture
  192. // may use clamped mode incorrectly.
  193. bool tex_is_already_tiled = tex.flags & VS::TEXTURE_FLAG_REPEAT;
  194. if (tex.tile_mode == BatchTex::TILE_NORMAL) {
  195. // if the texture is imported as tiled, no need to set GL state, as it will already be bound with repeat
  196. if (!tex_is_already_tiled) {
  197. glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
  198. glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
  199. }
  200. }
  201. // we need to convert explicitly from pod Vec2 to Vector2 ...
  202. // could use a cast but this might be unsafe in future
  203. Vector2 tps;
  204. tex.tex_pixel_size.to(tps);
  205. state.canvas_shader.set_uniform(CanvasShaderGLES2::COLOR_TEXPIXEL_SIZE, tps);
  206. switch (p_batch.type) {
  207. default: {
  208. // prevent compiler warning
  209. } break;
  210. case RasterizerStorageCommon::BT_RECT: {
  211. int64_t offset = p_batch.first_vert * 3;
  212. int num_elements = p_batch.num_commands * 6;
  213. glDrawElements(GL_TRIANGLES, num_elements, GL_UNSIGNED_SHORT, (void *)offset);
  214. } break;
  215. case RasterizerStorageCommon::BT_POLY: {
  216. int64_t offset = p_batch.first_vert;
  217. int num_elements = p_batch.num_commands;
  218. glDrawArrays(GL_TRIANGLES, offset, num_elements);
  219. } break;
  220. }
  221. storage->info.render._2d_draw_call_count++;
  222. switch (tex.tile_mode) {
  223. case BatchTex::TILE_FORCE_REPEAT: {
  224. state.canvas_shader.set_conditional(CanvasShaderGLES2::USE_FORCE_REPEAT, false);
  225. } break;
  226. case BatchTex::TILE_NORMAL: {
  227. // if the texture is imported as tiled, no need to revert GL state
  228. if (!tex_is_already_tiled) {
  229. glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
  230. glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
  231. }
  232. } break;
  233. default: {
  234. } break;
  235. }
  236. // could these have ifs?
  237. glDisableVertexAttribArray(VS::ARRAY_TEX_UV);
  238. glDisableVertexAttribArray(VS::ARRAY_COLOR);
  239. glDisableVertexAttribArray(VS::ARRAY_TANGENT);
  240. glDisableVertexAttribArray(VS::ARRAY_TEX_UV2);
  241. glDisableVertexAttribArray(VS::ARRAY_BONES);
  242. glDisableVertexAttribArray(VS::ARRAY_WEIGHTS);
  243. // may not be necessary .. state change optimization still TODO
  244. glBindBuffer(GL_ARRAY_BUFFER, 0);
  245. glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0);
  246. }
  247. void RasterizerCanvasGLES2::render_batches(Item *p_current_clip, bool &r_reclip, RasterizerStorageGLES2::Material *p_material) {
  248. int num_batches = bdata.batches.size();
  249. for (int batch_num = 0; batch_num < num_batches; batch_num++) {
  250. const Batch &batch = bdata.batches[batch_num];
  251. switch (batch.type) {
  252. case RasterizerStorageCommon::BT_RECT: {
  253. _batch_render_generic(batch, p_material);
  254. } break;
  255. case RasterizerStorageCommon::BT_POLY: {
  256. _batch_render_generic(batch, p_material);
  257. } break;
  258. case RasterizerStorageCommon::BT_LINE: {
  259. _batch_render_lines(batch, p_material, false);
  260. } break;
  261. case RasterizerStorageCommon::BT_LINE_AA: {
  262. _batch_render_lines(batch, p_material, true);
  263. } break;
  264. default: {
  265. int end_command = batch.first_command + batch.num_commands;
  266. RAST_DEV_DEBUG_ASSERT(batch.item);
  267. RasterizerCanvas::Item::Command *const *commands = batch.item->commands.ptr();
  268. for (int i = batch.first_command; i < end_command; i++) {
  269. Item::Command *command = commands[i];
  270. switch (command->type) {
  271. case Item::Command::TYPE_LINE: {
  272. Item::CommandLine *line = static_cast<Item::CommandLine *>(command);
  273. _set_texture_rect_mode(false);
  274. if (state.canvas_shader.bind()) {
  275. _set_uniforms();
  276. state.canvas_shader.use_material((void *)p_material);
  277. }
  278. _bind_canvas_texture(RID(), RID());
  279. glDisableVertexAttribArray(VS::ARRAY_COLOR);
  280. glVertexAttrib4fv(VS::ARRAY_COLOR, line->color.components);
  281. state.canvas_shader.set_uniform(CanvasShaderGLES2::MODELVIEW_MATRIX, state.uniforms.modelview_matrix);
  282. if (line->width <= 1) {
  283. Vector2 verts[2] = {
  284. Vector2(line->from.x, line->from.y),
  285. Vector2(line->to.x, line->to.y)
  286. };
  287. #ifdef GLES_OVER_GL
  288. if (line->antialiased)
  289. glEnable(GL_LINE_SMOOTH);
  290. #endif
  291. _draw_gui_primitive(2, verts, NULL, NULL);
  292. #ifdef GLES_OVER_GL
  293. if (line->antialiased)
  294. glDisable(GL_LINE_SMOOTH);
  295. #endif
  296. } else {
  297. Vector2 t = (line->from - line->to).normalized().tangent() * line->width * 0.5;
  298. Vector2 verts[4] = {
  299. line->from - t,
  300. line->from + t,
  301. line->to + t,
  302. line->to - t
  303. };
  304. _draw_gui_primitive(4, verts, NULL, NULL);
  305. #ifdef GLES_OVER_GL
  306. if (line->antialiased) {
  307. glEnable(GL_LINE_SMOOTH);
  308. for (int j = 0; j < 4; j++) {
  309. Vector2 vertsl[2] = {
  310. verts[j],
  311. verts[(j + 1) % 4],
  312. };
  313. _draw_gui_primitive(2, vertsl, NULL, NULL);
  314. }
  315. glDisable(GL_LINE_SMOOTH);
  316. }
  317. #endif
  318. }
  319. } break;
  320. case Item::Command::TYPE_RECT: {
  321. Item::CommandRect *r = static_cast<Item::CommandRect *>(command);
  322. glDisableVertexAttribArray(VS::ARRAY_COLOR);
  323. glVertexAttrib4fv(VS::ARRAY_COLOR, r->modulate.components);
  324. bool can_tile = true;
  325. if (r->texture.is_valid() && r->flags & CANVAS_RECT_TILE && !storage->config.support_npot_repeat_mipmap) {
  326. // workaround for when setting tiling does not work due to hardware limitation
  327. RasterizerStorageGLES2::Texture *texture = storage->texture_owner.getornull(r->texture);
  328. if (texture) {
  329. texture = texture->get_ptr();
  330. 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) {
  331. state.canvas_shader.set_conditional(CanvasShaderGLES2::USE_FORCE_REPEAT, true);
  332. can_tile = false;
  333. }
  334. }
  335. }
  336. // On some widespread Nvidia cards, the normal draw method can produce some
  337. // flickering in draw_rect and especially TileMap rendering (tiles randomly flicker).
  338. // See GH-9913.
  339. // To work it around, we use a simpler draw method which does not flicker, but gives
  340. // a non negligible performance hit, so it's opt-in (GH-24466).
  341. if (use_nvidia_rect_workaround) {
  342. // are we using normal maps, if so we want to use light angle
  343. bool send_light_angles = false;
  344. // only need to use light angles when normal mapping
  345. // otherwise we can use the default shader
  346. if (state.current_normal != RID()) {
  347. send_light_angles = true;
  348. }
  349. _set_texture_rect_mode(false, send_light_angles);
  350. if (state.canvas_shader.bind()) {
  351. _set_uniforms();
  352. state.canvas_shader.use_material((void *)p_material);
  353. }
  354. Vector2 points[4] = {
  355. r->rect.position,
  356. r->rect.position + Vector2(r->rect.size.x, 0.0),
  357. r->rect.position + r->rect.size,
  358. r->rect.position + Vector2(0.0, r->rect.size.y),
  359. };
  360. if (r->rect.size.x < 0) {
  361. SWAP(points[0], points[1]);
  362. SWAP(points[2], points[3]);
  363. }
  364. if (r->rect.size.y < 0) {
  365. SWAP(points[0], points[3]);
  366. SWAP(points[1], points[2]);
  367. }
  368. RasterizerStorageGLES2::Texture *texture = _bind_canvas_texture(r->texture, r->normal_map);
  369. if (texture) {
  370. Size2 texpixel_size(1.0 / texture->width, 1.0 / texture->height);
  371. Rect2 src_rect = (r->flags & CANVAS_RECT_REGION) ? Rect2(r->source.position * texpixel_size, r->source.size * texpixel_size) : Rect2(0, 0, 1, 1);
  372. Vector2 uvs[4] = {
  373. src_rect.position,
  374. src_rect.position + Vector2(src_rect.size.x, 0.0),
  375. src_rect.position + src_rect.size,
  376. src_rect.position + Vector2(0.0, src_rect.size.y),
  377. };
  378. // for encoding in light angle
  379. bool flip_h = false;
  380. bool flip_v = false;
  381. if (r->flags & CANVAS_RECT_TRANSPOSE) {
  382. SWAP(uvs[1], uvs[3]);
  383. }
  384. if (r->flags & CANVAS_RECT_FLIP_H) {
  385. SWAP(uvs[0], uvs[1]);
  386. SWAP(uvs[2], uvs[3]);
  387. flip_h = true;
  388. flip_v = !flip_v;
  389. }
  390. if (r->flags & CANVAS_RECT_FLIP_V) {
  391. SWAP(uvs[0], uvs[3]);
  392. SWAP(uvs[1], uvs[2]);
  393. flip_v = !flip_v;
  394. }
  395. state.canvas_shader.set_uniform(CanvasShaderGLES2::COLOR_TEXPIXEL_SIZE, texpixel_size);
  396. bool untile = false;
  397. if (can_tile && r->flags & CANVAS_RECT_TILE && !(texture->flags & VS::TEXTURE_FLAG_REPEAT)) {
  398. glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
  399. glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
  400. untile = true;
  401. }
  402. if (send_light_angles) {
  403. // for single rects, there is no need to fully utilize the light angle,
  404. // we only need it to encode flips (horz and vert). But the shader can be reused with
  405. // batching in which case the angle encodes the transform as well as
  406. // the flips.
  407. // Note transpose is NYI. I don't think it worked either with the non-nvidia method.
  408. // if horizontal flip, angle is 180
  409. float angle = 0.0f;
  410. if (flip_h)
  411. angle = Math_PI;
  412. // add 1 (to take care of zero floating point error with sign)
  413. angle += 1.0f;
  414. // flip if necessary
  415. if (flip_v)
  416. angle *= -1.0f;
  417. // light angle must be sent for each vert, instead as a single uniform in the uniform draw method
  418. // this has the benefit of enabling batching with light angles.
  419. float light_angles[4] = { angle, angle, angle, angle };
  420. _draw_gui_primitive(4, points, NULL, uvs, light_angles);
  421. } else {
  422. _draw_gui_primitive(4, points, NULL, uvs);
  423. }
  424. if (untile) {
  425. glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
  426. glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
  427. }
  428. } else {
  429. static const Vector2 uvs[4] = {
  430. Vector2(0.0, 0.0),
  431. Vector2(0.0, 1.0),
  432. Vector2(1.0, 1.0),
  433. Vector2(1.0, 0.0),
  434. };
  435. state.canvas_shader.set_uniform(CanvasShaderGLES2::COLOR_TEXPIXEL_SIZE, Vector2());
  436. _draw_gui_primitive(4, points, NULL, uvs);
  437. }
  438. } else {
  439. // This branch is better for performance, but can produce flicker on Nvidia, see above comment.
  440. _bind_quad_buffer();
  441. _set_texture_rect_mode(true);
  442. if (state.canvas_shader.bind()) {
  443. _set_uniforms();
  444. state.canvas_shader.use_material((void *)p_material);
  445. }
  446. RasterizerStorageGLES2::Texture *tex = _bind_canvas_texture(r->texture, r->normal_map);
  447. if (!tex) {
  448. Rect2 dst_rect = Rect2(r->rect.position, r->rect.size);
  449. if (dst_rect.size.width < 0) {
  450. dst_rect.position.x += dst_rect.size.width;
  451. dst_rect.size.width *= -1;
  452. }
  453. if (dst_rect.size.height < 0) {
  454. dst_rect.position.y += dst_rect.size.height;
  455. dst_rect.size.height *= -1;
  456. }
  457. 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));
  458. state.canvas_shader.set_uniform(CanvasShaderGLES2::SRC_RECT, Color(0, 0, 1, 1));
  459. glDrawArrays(GL_TRIANGLE_FAN, 0, 4);
  460. storage->info.render._2d_draw_call_count++;
  461. } else {
  462. bool untile = false;
  463. if (can_tile && r->flags & CANVAS_RECT_TILE && !(tex->flags & VS::TEXTURE_FLAG_REPEAT)) {
  464. glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
  465. glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
  466. untile = true;
  467. }
  468. Size2 texpixel_size(1.0 / tex->width, 1.0 / tex->height);
  469. Rect2 src_rect = (r->flags & CANVAS_RECT_REGION) ? Rect2(r->source.position * texpixel_size, r->source.size * texpixel_size) : Rect2(0, 0, 1, 1);
  470. Rect2 dst_rect = Rect2(r->rect.position, r->rect.size);
  471. if (dst_rect.size.width < 0) {
  472. dst_rect.position.x += dst_rect.size.width;
  473. dst_rect.size.width *= -1;
  474. }
  475. if (dst_rect.size.height < 0) {
  476. dst_rect.position.y += dst_rect.size.height;
  477. dst_rect.size.height *= -1;
  478. }
  479. if (r->flags & CANVAS_RECT_FLIP_H) {
  480. src_rect.size.x *= -1;
  481. }
  482. if (r->flags & CANVAS_RECT_FLIP_V) {
  483. src_rect.size.y *= -1;
  484. }
  485. if (r->flags & CANVAS_RECT_TRANSPOSE) {
  486. dst_rect.size.x *= -1; // Encoding in the dst_rect.z uniform
  487. }
  488. state.canvas_shader.set_uniform(CanvasShaderGLES2::COLOR_TEXPIXEL_SIZE, texpixel_size);
  489. 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));
  490. 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));
  491. glDrawArrays(GL_TRIANGLE_FAN, 0, 4);
  492. storage->info.render._2d_draw_call_count++;
  493. if (untile) {
  494. glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
  495. glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
  496. }
  497. }
  498. glBindBuffer(GL_ARRAY_BUFFER, 0);
  499. glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0);
  500. }
  501. state.canvas_shader.set_conditional(CanvasShaderGLES2::USE_FORCE_REPEAT, false);
  502. } break;
  503. case Item::Command::TYPE_NINEPATCH: {
  504. Item::CommandNinePatch *np = static_cast<Item::CommandNinePatch *>(command);
  505. _set_texture_rect_mode(false);
  506. if (state.canvas_shader.bind()) {
  507. _set_uniforms();
  508. state.canvas_shader.use_material((void *)p_material);
  509. }
  510. glDisableVertexAttribArray(VS::ARRAY_COLOR);
  511. glVertexAttrib4fv(VS::ARRAY_COLOR, np->color.components);
  512. RasterizerStorageGLES2::Texture *tex = _bind_canvas_texture(np->texture, np->normal_map);
  513. if (!tex) {
  514. // FIXME: Handle textureless ninepatch gracefully
  515. WARN_PRINT("NinePatch without texture not supported yet in GLES2 backend, skipping.");
  516. continue;
  517. }
  518. if (tex->width == 0 || tex->height == 0) {
  519. WARN_PRINT("Cannot set empty texture to NinePatch.");
  520. continue;
  521. }
  522. Size2 texpixel_size(1.0 / tex->width, 1.0 / tex->height);
  523. // state.canvas_shader.set_uniform(CanvasShaderGLES2::MODELVIEW_MATRIX, state.uniforms.modelview_matrix);
  524. state.canvas_shader.set_uniform(CanvasShaderGLES2::COLOR_TEXPIXEL_SIZE, texpixel_size);
  525. Rect2 source = np->source;
  526. if (source.size.x == 0 && source.size.y == 0) {
  527. source.size.x = tex->width;
  528. source.size.y = tex->height;
  529. }
  530. float screen_scale = 1.0;
  531. if ((bdata.settings_ninepatch_mode == 1) && (source.size.x != 0) && (source.size.y != 0)) {
  532. screen_scale = MIN(np->rect.size.x / source.size.x, np->rect.size.y / source.size.y);
  533. screen_scale = MIN(1.0, screen_scale);
  534. }
  535. // prepare vertex buffer
  536. // this buffer contains [ POS POS UV UV ] *
  537. float buffer[16 * 2 + 16 * 2];
  538. {
  539. // first row
  540. buffer[(0 * 4 * 4) + 0] = np->rect.position.x;
  541. buffer[(0 * 4 * 4) + 1] = np->rect.position.y;
  542. buffer[(0 * 4 * 4) + 2] = source.position.x * texpixel_size.x;
  543. buffer[(0 * 4 * 4) + 3] = source.position.y * texpixel_size.y;
  544. buffer[(0 * 4 * 4) + 4] = np->rect.position.x + np->margin[MARGIN_LEFT] * screen_scale;
  545. buffer[(0 * 4 * 4) + 5] = np->rect.position.y;
  546. buffer[(0 * 4 * 4) + 6] = (source.position.x + np->margin[MARGIN_LEFT]) * texpixel_size.x;
  547. buffer[(0 * 4 * 4) + 7] = source.position.y * texpixel_size.y;
  548. buffer[(0 * 4 * 4) + 8] = np->rect.position.x + np->rect.size.x - np->margin[MARGIN_RIGHT] * screen_scale;
  549. buffer[(0 * 4 * 4) + 9] = np->rect.position.y;
  550. buffer[(0 * 4 * 4) + 10] = (source.position.x + source.size.x - np->margin[MARGIN_RIGHT]) * texpixel_size.x;
  551. buffer[(0 * 4 * 4) + 11] = source.position.y * texpixel_size.y;
  552. buffer[(0 * 4 * 4) + 12] = np->rect.position.x + np->rect.size.x;
  553. buffer[(0 * 4 * 4) + 13] = np->rect.position.y;
  554. buffer[(0 * 4 * 4) + 14] = (source.position.x + source.size.x) * texpixel_size.x;
  555. buffer[(0 * 4 * 4) + 15] = source.position.y * texpixel_size.y;
  556. // second row
  557. buffer[(1 * 4 * 4) + 0] = np->rect.position.x;
  558. buffer[(1 * 4 * 4) + 1] = np->rect.position.y + np->margin[MARGIN_TOP] * screen_scale;
  559. buffer[(1 * 4 * 4) + 2] = source.position.x * texpixel_size.x;
  560. buffer[(1 * 4 * 4) + 3] = (source.position.y + np->margin[MARGIN_TOP]) * texpixel_size.y;
  561. buffer[(1 * 4 * 4) + 4] = np->rect.position.x + np->margin[MARGIN_LEFT] * screen_scale;
  562. buffer[(1 * 4 * 4) + 5] = np->rect.position.y + np->margin[MARGIN_TOP] * screen_scale;
  563. buffer[(1 * 4 * 4) + 6] = (source.position.x + np->margin[MARGIN_LEFT]) * texpixel_size.x;
  564. buffer[(1 * 4 * 4) + 7] = (source.position.y + np->margin[MARGIN_TOP]) * texpixel_size.y;
  565. buffer[(1 * 4 * 4) + 8] = np->rect.position.x + np->rect.size.x - np->margin[MARGIN_RIGHT] * screen_scale;
  566. buffer[(1 * 4 * 4) + 9] = np->rect.position.y + np->margin[MARGIN_TOP] * screen_scale;
  567. buffer[(1 * 4 * 4) + 10] = (source.position.x + source.size.x - np->margin[MARGIN_RIGHT]) * texpixel_size.x;
  568. buffer[(1 * 4 * 4) + 11] = (source.position.y + np->margin[MARGIN_TOP]) * texpixel_size.y;
  569. buffer[(1 * 4 * 4) + 12] = np->rect.position.x + np->rect.size.x;
  570. buffer[(1 * 4 * 4) + 13] = np->rect.position.y + np->margin[MARGIN_TOP] * screen_scale;
  571. buffer[(1 * 4 * 4) + 14] = (source.position.x + source.size.x) * texpixel_size.x;
  572. buffer[(1 * 4 * 4) + 15] = (source.position.y + np->margin[MARGIN_TOP]) * texpixel_size.y;
  573. // third row
  574. buffer[(2 * 4 * 4) + 0] = np->rect.position.x;
  575. buffer[(2 * 4 * 4) + 1] = np->rect.position.y + np->rect.size.y - np->margin[MARGIN_BOTTOM] * screen_scale;
  576. buffer[(2 * 4 * 4) + 2] = source.position.x * texpixel_size.x;
  577. buffer[(2 * 4 * 4) + 3] = (source.position.y + source.size.y - np->margin[MARGIN_BOTTOM]) * texpixel_size.y;
  578. buffer[(2 * 4 * 4) + 4] = np->rect.position.x + np->margin[MARGIN_LEFT] * screen_scale;
  579. buffer[(2 * 4 * 4) + 5] = np->rect.position.y + np->rect.size.y - np->margin[MARGIN_BOTTOM] * screen_scale;
  580. buffer[(2 * 4 * 4) + 6] = (source.position.x + np->margin[MARGIN_LEFT]) * texpixel_size.x;
  581. buffer[(2 * 4 * 4) + 7] = (source.position.y + source.size.y - np->margin[MARGIN_BOTTOM]) * texpixel_size.y;
  582. buffer[(2 * 4 * 4) + 8] = np->rect.position.x + np->rect.size.x - np->margin[MARGIN_RIGHT] * screen_scale;
  583. buffer[(2 * 4 * 4) + 9] = np->rect.position.y + np->rect.size.y - np->margin[MARGIN_BOTTOM] * screen_scale;
  584. buffer[(2 * 4 * 4) + 10] = (source.position.x + source.size.x - np->margin[MARGIN_RIGHT]) * texpixel_size.x;
  585. buffer[(2 * 4 * 4) + 11] = (source.position.y + source.size.y - np->margin[MARGIN_BOTTOM]) * texpixel_size.y;
  586. buffer[(2 * 4 * 4) + 12] = np->rect.position.x + np->rect.size.x;
  587. buffer[(2 * 4 * 4) + 13] = np->rect.position.y + np->rect.size.y - np->margin[MARGIN_BOTTOM] * screen_scale;
  588. buffer[(2 * 4 * 4) + 14] = (source.position.x + source.size.x) * texpixel_size.x;
  589. buffer[(2 * 4 * 4) + 15] = (source.position.y + source.size.y - np->margin[MARGIN_BOTTOM]) * texpixel_size.y;
  590. // fourth row
  591. buffer[(3 * 4 * 4) + 0] = np->rect.position.x;
  592. buffer[(3 * 4 * 4) + 1] = np->rect.position.y + np->rect.size.y;
  593. buffer[(3 * 4 * 4) + 2] = source.position.x * texpixel_size.x;
  594. buffer[(3 * 4 * 4) + 3] = (source.position.y + source.size.y) * texpixel_size.y;
  595. buffer[(3 * 4 * 4) + 4] = np->rect.position.x + np->margin[MARGIN_LEFT] * screen_scale;
  596. buffer[(3 * 4 * 4) + 5] = np->rect.position.y + np->rect.size.y;
  597. buffer[(3 * 4 * 4) + 6] = (source.position.x + np->margin[MARGIN_LEFT]) * texpixel_size.x;
  598. buffer[(3 * 4 * 4) + 7] = (source.position.y + source.size.y) * texpixel_size.y;
  599. buffer[(3 * 4 * 4) + 8] = np->rect.position.x + np->rect.size.x - np->margin[MARGIN_RIGHT] * screen_scale;
  600. buffer[(3 * 4 * 4) + 9] = np->rect.position.y + np->rect.size.y;
  601. buffer[(3 * 4 * 4) + 10] = (source.position.x + source.size.x - np->margin[MARGIN_RIGHT]) * texpixel_size.x;
  602. buffer[(3 * 4 * 4) + 11] = (source.position.y + source.size.y) * texpixel_size.y;
  603. buffer[(3 * 4 * 4) + 12] = np->rect.position.x + np->rect.size.x;
  604. buffer[(3 * 4 * 4) + 13] = np->rect.position.y + np->rect.size.y;
  605. buffer[(3 * 4 * 4) + 14] = (source.position.x + source.size.x) * texpixel_size.x;
  606. buffer[(3 * 4 * 4) + 15] = (source.position.y + source.size.y) * texpixel_size.y;
  607. }
  608. glBindBuffer(GL_ARRAY_BUFFER, data.ninepatch_vertices);
  609. glBufferData(GL_ARRAY_BUFFER, sizeof(float) * (16 + 16) * 2, buffer, _buffer_upload_usage_flag);
  610. glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, data.ninepatch_elements);
  611. glEnableVertexAttribArray(VS::ARRAY_VERTEX);
  612. glEnableVertexAttribArray(VS::ARRAY_TEX_UV);
  613. glVertexAttribPointer(VS::ARRAY_VERTEX, 2, GL_FLOAT, GL_FALSE, 4 * sizeof(float), NULL);
  614. glVertexAttribPointer(VS::ARRAY_TEX_UV, 2, GL_FLOAT, GL_FALSE, 4 * sizeof(float), CAST_INT_TO_UCHAR_PTR((sizeof(float) * 2)));
  615. glDrawElements(GL_TRIANGLES, 18 * 3 - (np->draw_center ? 0 : 6), GL_UNSIGNED_BYTE, NULL);
  616. glBindBuffer(GL_ARRAY_BUFFER, 0);
  617. glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0);
  618. storage->info.render._2d_draw_call_count++;
  619. } break;
  620. case Item::Command::TYPE_CIRCLE: {
  621. Item::CommandCircle *circle = static_cast<Item::CommandCircle *>(command);
  622. _set_texture_rect_mode(false);
  623. if (state.canvas_shader.bind()) {
  624. _set_uniforms();
  625. state.canvas_shader.use_material((void *)p_material);
  626. }
  627. static const int num_points = 32;
  628. Vector2 points[num_points + 1];
  629. points[num_points] = circle->pos;
  630. int indices[num_points * 3];
  631. for (int j = 0; j < num_points; j++) {
  632. 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;
  633. indices[j * 3 + 0] = j;
  634. indices[j * 3 + 1] = (j + 1) % num_points;
  635. indices[j * 3 + 2] = num_points;
  636. }
  637. _bind_canvas_texture(RID(), RID());
  638. _draw_polygon(indices, num_points * 3, num_points + 1, points, NULL, &circle->color, true);
  639. } break;
  640. case Item::Command::TYPE_POLYGON: {
  641. Item::CommandPolygon *polygon = static_cast<Item::CommandPolygon *>(command);
  642. _set_texture_rect_mode(false);
  643. if (state.canvas_shader.bind()) {
  644. _set_uniforms();
  645. state.canvas_shader.use_material((void *)p_material);
  646. }
  647. RasterizerStorageGLES2::Texture *texture = _bind_canvas_texture(polygon->texture, polygon->normal_map);
  648. if (texture) {
  649. Size2 texpixel_size(1.0 / texture->width, 1.0 / texture->height);
  650. state.canvas_shader.set_uniform(CanvasShaderGLES2::COLOR_TEXPIXEL_SIZE, texpixel_size);
  651. }
  652. _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());
  653. #ifdef GLES_OVER_GL
  654. if (polygon->antialiased) {
  655. glEnable(GL_LINE_SMOOTH);
  656. if (polygon->antialiasing_use_indices) {
  657. _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);
  658. } else {
  659. _draw_generic(GL_LINE_LOOP, polygon->points.size(), polygon->points.ptr(), polygon->uvs.ptr(), polygon->colors.ptr(), polygon->colors.size() == 1);
  660. }
  661. glDisable(GL_LINE_SMOOTH);
  662. }
  663. #endif
  664. } break;
  665. case Item::Command::TYPE_MESH: {
  666. Item::CommandMesh *mesh = static_cast<Item::CommandMesh *>(command);
  667. _set_texture_rect_mode(false);
  668. if (state.canvas_shader.bind()) {
  669. _set_uniforms();
  670. state.canvas_shader.use_material((void *)p_material);
  671. }
  672. RasterizerStorageGLES2::Texture *texture = _bind_canvas_texture(mesh->texture, mesh->normal_map);
  673. if (texture) {
  674. Size2 texpixel_size(1.0 / texture->width, 1.0 / texture->height);
  675. state.canvas_shader.set_uniform(CanvasShaderGLES2::COLOR_TEXPIXEL_SIZE, texpixel_size);
  676. }
  677. RasterizerStorageGLES2::Mesh *mesh_data = storage->mesh_owner.getornull(mesh->mesh);
  678. if (mesh_data) {
  679. for (int j = 0; j < mesh_data->surfaces.size(); j++) {
  680. RasterizerStorageGLES2::Surface *s = mesh_data->surfaces[j];
  681. // 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
  682. glBindBuffer(GL_ARRAY_BUFFER, s->vertex_id);
  683. if (s->index_array_len > 0) {
  684. glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, s->index_id);
  685. }
  686. for (int k = 0; k < VS::ARRAY_MAX - 1; k++) {
  687. if (s->attribs[k].enabled) {
  688. glEnableVertexAttribArray(k);
  689. 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));
  690. } else {
  691. glDisableVertexAttribArray(k);
  692. switch (k) {
  693. case VS::ARRAY_NORMAL: {
  694. glVertexAttrib4f(VS::ARRAY_NORMAL, 0.0, 0.0, 1, 1);
  695. } break;
  696. case VS::ARRAY_COLOR: {
  697. glVertexAttrib4f(VS::ARRAY_COLOR, 1, 1, 1, 1);
  698. } break;
  699. default: {
  700. }
  701. }
  702. }
  703. }
  704. if (s->index_array_len > 0) {
  705. glDrawElements(gl_primitive[s->primitive], s->index_array_len, (s->array_len >= (1 << 16)) ? GL_UNSIGNED_INT : GL_UNSIGNED_SHORT, 0);
  706. } else {
  707. glDrawArrays(gl_primitive[s->primitive], 0, s->array_len);
  708. }
  709. }
  710. for (int j = 1; j < VS::ARRAY_MAX - 1; j++) {
  711. glDisableVertexAttribArray(j);
  712. }
  713. }
  714. storage->info.render._2d_draw_call_count++;
  715. } break;
  716. case Item::Command::TYPE_MULTIMESH: {
  717. Item::CommandMultiMesh *mmesh = static_cast<Item::CommandMultiMesh *>(command);
  718. RasterizerStorageGLES2::MultiMesh *multi_mesh = storage->multimesh_owner.getornull(mmesh->multimesh);
  719. if (!multi_mesh)
  720. break;
  721. RasterizerStorageGLES2::Mesh *mesh_data = storage->mesh_owner.getornull(multi_mesh->mesh);
  722. if (!mesh_data)
  723. break;
  724. state.canvas_shader.set_conditional(CanvasShaderGLES2::USE_INSTANCE_CUSTOM, multi_mesh->custom_data_format != VS::MULTIMESH_CUSTOM_DATA_NONE);
  725. state.canvas_shader.set_conditional(CanvasShaderGLES2::USE_INSTANCING, true);
  726. _set_texture_rect_mode(false);
  727. if (state.canvas_shader.bind()) {
  728. _set_uniforms();
  729. state.canvas_shader.use_material((void *)p_material);
  730. }
  731. RasterizerStorageGLES2::Texture *texture = _bind_canvas_texture(mmesh->texture, mmesh->normal_map);
  732. if (texture) {
  733. Size2 texpixel_size(1.0 / texture->width, 1.0 / texture->height);
  734. state.canvas_shader.set_uniform(CanvasShaderGLES2::COLOR_TEXPIXEL_SIZE, texpixel_size);
  735. }
  736. //reset shader and force rebind
  737. int amount = MIN(multi_mesh->size, multi_mesh->visible_instances);
  738. if (amount == -1) {
  739. amount = multi_mesh->size;
  740. }
  741. int stride = multi_mesh->color_floats + multi_mesh->custom_data_floats + multi_mesh->xform_floats;
  742. int color_ofs = multi_mesh->xform_floats;
  743. int custom_data_ofs = color_ofs + multi_mesh->color_floats;
  744. // drawing
  745. const float *base_buffer = multi_mesh->data.ptr();
  746. for (int j = 0; j < mesh_data->surfaces.size(); j++) {
  747. RasterizerStorageGLES2::Surface *s = mesh_data->surfaces[j];
  748. // 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
  749. //bind buffers for mesh surface
  750. glBindBuffer(GL_ARRAY_BUFFER, s->vertex_id);
  751. if (s->index_array_len > 0) {
  752. glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, s->index_id);
  753. }
  754. for (int k = 0; k < VS::ARRAY_MAX - 1; k++) {
  755. if (s->attribs[k].enabled) {
  756. glEnableVertexAttribArray(k);
  757. 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));
  758. } else {
  759. glDisableVertexAttribArray(k);
  760. switch (k) {
  761. case VS::ARRAY_NORMAL: {
  762. glVertexAttrib4f(VS::ARRAY_NORMAL, 0.0, 0.0, 1, 1);
  763. } break;
  764. case VS::ARRAY_COLOR: {
  765. glVertexAttrib4f(VS::ARRAY_COLOR, 1, 1, 1, 1);
  766. } break;
  767. default: {
  768. }
  769. }
  770. }
  771. }
  772. for (int k = 0; k < amount; k++) {
  773. const float *buffer = base_buffer + k * stride;
  774. {
  775. glVertexAttrib4fv(INSTANCE_ATTRIB_BASE + 0, &buffer[0]);
  776. glVertexAttrib4fv(INSTANCE_ATTRIB_BASE + 1, &buffer[4]);
  777. if (multi_mesh->transform_format == VS::MULTIMESH_TRANSFORM_3D) {
  778. glVertexAttrib4fv(INSTANCE_ATTRIB_BASE + 2, &buffer[8]);
  779. } else {
  780. glVertexAttrib4f(INSTANCE_ATTRIB_BASE + 2, 0.0, 0.0, 1.0, 0.0);
  781. }
  782. }
  783. if (multi_mesh->color_floats) {
  784. if (multi_mesh->color_format == VS::MULTIMESH_COLOR_8BIT) {
  785. uint8_t *color_data = (uint8_t *)(buffer + color_ofs);
  786. 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);
  787. } else {
  788. glVertexAttrib4fv(INSTANCE_ATTRIB_BASE + 3, buffer + color_ofs);
  789. }
  790. } else {
  791. glVertexAttrib4f(INSTANCE_ATTRIB_BASE + 3, 1.0, 1.0, 1.0, 1.0);
  792. }
  793. if (multi_mesh->custom_data_floats) {
  794. if (multi_mesh->custom_data_format == VS::MULTIMESH_CUSTOM_DATA_8BIT) {
  795. uint8_t *custom_data = (uint8_t *)(buffer + custom_data_ofs);
  796. 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);
  797. } else {
  798. glVertexAttrib4fv(INSTANCE_ATTRIB_BASE + 4, buffer + custom_data_ofs);
  799. }
  800. }
  801. if (s->index_array_len > 0) {
  802. glDrawElements(gl_primitive[s->primitive], s->index_array_len, (s->array_len >= (1 << 16)) ? GL_UNSIGNED_INT : GL_UNSIGNED_SHORT, 0);
  803. } else {
  804. glDrawArrays(gl_primitive[s->primitive], 0, s->array_len);
  805. }
  806. }
  807. }
  808. // LIGHT ANGLE PR replaced USE_INSTANCE_CUSTOM line with below .. think it was a typo,
  809. // but just in case, made this note.
  810. //_set_texture_rect_mode(false);
  811. state.canvas_shader.set_conditional(CanvasShaderGLES2::USE_INSTANCE_CUSTOM, false);
  812. state.canvas_shader.set_conditional(CanvasShaderGLES2::USE_INSTANCING, false);
  813. storage->info.render._2d_draw_call_count++;
  814. } break;
  815. case Item::Command::TYPE_POLYLINE: {
  816. Item::CommandPolyLine *pline = static_cast<Item::CommandPolyLine *>(command);
  817. _set_texture_rect_mode(false);
  818. if (state.canvas_shader.bind()) {
  819. _set_uniforms();
  820. state.canvas_shader.use_material((void *)p_material);
  821. }
  822. _bind_canvas_texture(RID(), RID());
  823. if (pline->triangles.size()) {
  824. _draw_generic(GL_TRIANGLE_STRIP, pline->triangles.size(), pline->triangles.ptr(), NULL, pline->triangle_colors.ptr(), pline->triangle_colors.size() == 1);
  825. #ifdef GLES_OVER_GL
  826. glEnable(GL_LINE_SMOOTH);
  827. if (pline->multiline) {
  828. //needs to be different
  829. } else {
  830. _draw_generic(GL_LINE_LOOP, pline->lines.size(), pline->lines.ptr(), NULL, pline->line_colors.ptr(), pline->line_colors.size() == 1);
  831. }
  832. glDisable(GL_LINE_SMOOTH);
  833. #endif
  834. } else {
  835. #ifdef GLES_OVER_GL
  836. if (pline->antialiased)
  837. glEnable(GL_LINE_SMOOTH);
  838. #endif
  839. if (pline->multiline) {
  840. int todo = pline->lines.size() / 2;
  841. int max_per_call = data.polygon_buffer_size / (sizeof(real_t) * 4);
  842. int offset = 0;
  843. while (todo) {
  844. int to_draw = MIN(max_per_call, todo);
  845. _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);
  846. todo -= to_draw;
  847. offset += to_draw * 2;
  848. }
  849. } else {
  850. _draw_generic(GL_LINE_STRIP, pline->lines.size(), pline->lines.ptr(), NULL, pline->line_colors.ptr(), pline->line_colors.size() == 1);
  851. }
  852. #ifdef GLES_OVER_GL
  853. if (pline->antialiased)
  854. glDisable(GL_LINE_SMOOTH);
  855. #endif
  856. }
  857. } break;
  858. case Item::Command::TYPE_PRIMITIVE: {
  859. Item::CommandPrimitive *primitive = static_cast<Item::CommandPrimitive *>(command);
  860. _set_texture_rect_mode(false);
  861. if (state.canvas_shader.bind()) {
  862. _set_uniforms();
  863. state.canvas_shader.use_material((void *)p_material);
  864. }
  865. ERR_CONTINUE(primitive->points.size() < 1);
  866. RasterizerStorageGLES2::Texture *texture = _bind_canvas_texture(primitive->texture, primitive->normal_map);
  867. if (texture) {
  868. Size2 texpixel_size(1.0 / texture->width, 1.0 / texture->height);
  869. state.canvas_shader.set_uniform(CanvasShaderGLES2::COLOR_TEXPIXEL_SIZE, texpixel_size);
  870. }
  871. // we need a temporary because this must be nulled out
  872. // if only a single color specified
  873. const Color *colors = primitive->colors.ptr();
  874. if (primitive->colors.size() == 1 && primitive->points.size() > 1) {
  875. Color c = primitive->colors[0];
  876. glVertexAttrib4f(VS::ARRAY_COLOR, c.r, c.g, c.b, c.a);
  877. colors = nullptr;
  878. } else if (primitive->colors.empty()) {
  879. glVertexAttrib4f(VS::ARRAY_COLOR, 1, 1, 1, 1);
  880. }
  881. #ifdef RASTERIZER_EXTRA_CHECKS
  882. else {
  883. RAST_DEV_DEBUG_ASSERT(primitive->colors.size() == primitive->points.size());
  884. }
  885. if (primitive->uvs.ptr()) {
  886. RAST_DEV_DEBUG_ASSERT(primitive->uvs.size() == primitive->points.size());
  887. }
  888. #endif
  889. _draw_gui_primitive(primitive->points.size(), primitive->points.ptr(), colors, primitive->uvs.ptr());
  890. } break;
  891. case Item::Command::TYPE_TRANSFORM: {
  892. Item::CommandTransform *transform = static_cast<Item::CommandTransform *>(command);
  893. state.uniforms.extra_matrix = transform->xform;
  894. state.canvas_shader.set_uniform(CanvasShaderGLES2::EXTRA_MATRIX, state.uniforms.extra_matrix);
  895. } break;
  896. case Item::Command::TYPE_PARTICLES: {
  897. } break;
  898. case Item::Command::TYPE_CLIP_IGNORE: {
  899. Item::CommandClipIgnore *ci = static_cast<Item::CommandClipIgnore *>(command);
  900. if (p_current_clip) {
  901. if (ci->ignore != r_reclip) {
  902. if (ci->ignore) {
  903. glDisable(GL_SCISSOR_TEST);
  904. r_reclip = true;
  905. } else {
  906. glEnable(GL_SCISSOR_TEST);
  907. int x = p_current_clip->final_clip_rect.position.x;
  908. int y = storage->frame.current_rt->height - (p_current_clip->final_clip_rect.position.y + p_current_clip->final_clip_rect.size.y);
  909. int w = p_current_clip->final_clip_rect.size.x;
  910. int h = p_current_clip->final_clip_rect.size.y;
  911. if (storage->frame.current_rt->flags[RasterizerStorage::RENDER_TARGET_VFLIP])
  912. y = p_current_clip->final_clip_rect.position.y;
  913. glScissor(x, y, w, h);
  914. r_reclip = false;
  915. }
  916. }
  917. }
  918. } break;
  919. default: {
  920. // FIXME: Proper error handling if relevant
  921. //print_line("other");
  922. } break;
  923. }
  924. }
  925. } // default
  926. break;
  927. }
  928. }
  929. }
  930. void RasterizerCanvasGLES2::canvas_end() {
  931. batch_canvas_end();
  932. RasterizerCanvasBaseGLES2::canvas_end();
  933. }
  934. void RasterizerCanvasGLES2::canvas_begin() {
  935. batch_canvas_begin();
  936. RasterizerCanvasBaseGLES2::canvas_begin();
  937. }
  938. void RasterizerCanvasGLES2::canvas_render_items_begin(const Color &p_modulate, Light *p_light, const Transform2D &p_base_transform) {
  939. batch_canvas_render_items_begin(p_modulate, p_light, p_base_transform);
  940. }
  941. void RasterizerCanvasGLES2::canvas_render_items_end() {
  942. batch_canvas_render_items_end();
  943. }
  944. void RasterizerCanvasGLES2::canvas_render_items(Item *p_item_list, int p_z, const Color &p_modulate, Light *p_light, const Transform2D &p_base_transform) {
  945. batch_canvas_render_items(p_item_list, p_z, p_modulate, p_light, p_base_transform);
  946. }
  947. 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) {
  948. // parameters are easier to pass around in a structure
  949. RenderItemState ris;
  950. ris.item_group_z = p_z;
  951. ris.item_group_modulate = p_modulate;
  952. ris.item_group_light = p_light;
  953. ris.item_group_base_transform = p_base_transform;
  954. state.canvas_shader.set_conditional(CanvasShaderGLES2::USE_SKELETON, false);
  955. state.current_tex = RID();
  956. state.current_tex_ptr = NULL;
  957. state.current_normal = RID();
  958. state.canvas_texscreen_used = false;
  959. glActiveTexture(GL_TEXTURE0);
  960. glBindTexture(GL_TEXTURE_2D, storage->resources.white_tex);
  961. if (bdata.settings_use_batching) {
  962. for (int j = 0; j < bdata.items_joined.size(); j++) {
  963. render_joined_item(bdata.items_joined[j], ris);
  964. }
  965. } else {
  966. while (p_item_list) {
  967. Item *ci = p_item_list;
  968. _legacy_canvas_render_item(ci, ris);
  969. p_item_list = p_item_list->next;
  970. }
  971. }
  972. if (ris.current_clip) {
  973. glDisable(GL_SCISSOR_TEST);
  974. }
  975. state.canvas_shader.set_conditional(CanvasShaderGLES2::USE_SKELETON, false);
  976. }
  977. // This function is a dry run of the state changes when drawing the item.
  978. // It should duplicate the logic in _canvas_render_item,
  979. // to decide whether items are similar enough to join
  980. // i.e. no state differences between the 2 items.
  981. bool RasterizerCanvasGLES2::try_join_item(Item *p_ci, RenderItemState &r_ris, bool &r_batch_break) {
  982. // if we set max join items to zero we can effectively prevent any joining, so
  983. // none of the other logic needs to run. Good for testing regression bugs, and
  984. // could conceivably be faster in some games.
  985. if (!bdata.settings_max_join_item_commands) {
  986. return false;
  987. }
  988. // if there are any state changes we change join to false
  989. // we also set r_batch_break to true if we don't want this item joined to the next
  990. // (e.g. an item that must not be joined at all)
  991. r_batch_break = false;
  992. bool join = true;
  993. // light_masked objects we just don't currently support for joining
  994. // (this could possibly be improved at a later date)
  995. if (p_ci->light_masked) {
  996. join = false;
  997. r_batch_break = true;
  998. }
  999. // we will now allow joining even if final modulate is different
  1000. // we will instead bake the final modulate into the vertex colors
  1001. // if (p_ci->final_modulate != r_ris.final_modulate) {
  1002. // join = false;
  1003. // r_ris.final_modulate = p_ci->final_modulate;
  1004. // }
  1005. if (r_ris.current_clip != p_ci->final_clip_owner) {
  1006. r_ris.current_clip = p_ci->final_clip_owner;
  1007. join = false;
  1008. }
  1009. // TODO: copy back buffer
  1010. if (p_ci->copy_back_buffer) {
  1011. join = false;
  1012. }
  1013. RasterizerStorageGLES2::Skeleton *skeleton = NULL;
  1014. {
  1015. //skeleton handling
  1016. if (p_ci->skeleton.is_valid() && storage->skeleton_owner.owns(p_ci->skeleton)) {
  1017. skeleton = storage->skeleton_owner.get(p_ci->skeleton);
  1018. if (!skeleton->use_2d) {
  1019. skeleton = NULL;
  1020. }
  1021. }
  1022. bool skeleton_prevent_join = false;
  1023. bool use_skeleton = skeleton != NULL;
  1024. if (r_ris.prev_use_skeleton != use_skeleton) {
  1025. if (!bdata.settings_use_software_skinning)
  1026. r_ris.rebind_shader = true;
  1027. r_ris.prev_use_skeleton = use_skeleton;
  1028. // join = false;
  1029. skeleton_prevent_join = true;
  1030. }
  1031. if (skeleton) {
  1032. // join = false;
  1033. skeleton_prevent_join = true;
  1034. state.using_skeleton = true;
  1035. } else {
  1036. state.using_skeleton = false;
  1037. }
  1038. if (skeleton_prevent_join) {
  1039. if (!bdata.settings_use_software_skinning)
  1040. join = false;
  1041. }
  1042. }
  1043. Item *material_owner = p_ci->material_owner ? p_ci->material_owner : p_ci;
  1044. RID material = material_owner->material;
  1045. RasterizerStorageGLES2::Material *material_ptr = storage->material_owner.getornull(material);
  1046. if (material != r_ris.canvas_last_material || r_ris.rebind_shader) {
  1047. join = false;
  1048. RasterizerStorageGLES2::Shader *shader_ptr = NULL;
  1049. if (material_ptr) {
  1050. shader_ptr = material_ptr->shader;
  1051. // special case, if the user has made an error in the shader code
  1052. if (shader_ptr && !shader_ptr->valid) {
  1053. join = false;
  1054. r_batch_break = true;
  1055. }
  1056. if (shader_ptr && shader_ptr->mode != VS::SHADER_CANVAS_ITEM) {
  1057. shader_ptr = NULL; // not a canvas item shader, don't use.
  1058. }
  1059. }
  1060. if (shader_ptr) {
  1061. if (shader_ptr->canvas_item.uses_screen_texture) {
  1062. if (!state.canvas_texscreen_used) {
  1063. join = false;
  1064. }
  1065. }
  1066. }
  1067. r_ris.shader_cache = shader_ptr;
  1068. r_ris.canvas_last_material = material;
  1069. r_ris.rebind_shader = false;
  1070. }
  1071. int blend_mode = r_ris.shader_cache ? r_ris.shader_cache->canvas_item.blend_mode : RasterizerStorageGLES2::Shader::CanvasItem::BLEND_MODE_MIX;
  1072. 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));
  1073. bool reclip = false;
  1074. // we are precalculating the final_modulate ahead of time because we need this for baking of final modulate into vertex colors
  1075. // (only in software transform mode)
  1076. // This maybe inefficient storing it...
  1077. r_ris.final_modulate = unshaded ? p_ci->final_modulate : (p_ci->final_modulate * r_ris.item_group_modulate);
  1078. if (r_ris.last_blend_mode != blend_mode) {
  1079. join = false;
  1080. r_ris.last_blend_mode = blend_mode;
  1081. }
  1082. // does the shader contain BUILTINs which should break the batching?
  1083. bdata.joined_item_batch_flags = 0;
  1084. if (r_ris.shader_cache) {
  1085. unsigned int and_flags = r_ris.shader_cache->canvas_item.batch_flags & (RasterizerStorageCommon::PREVENT_COLOR_BAKING | RasterizerStorageCommon::PREVENT_VERTEX_BAKING | RasterizerStorageCommon::PREVENT_ITEM_JOINING);
  1086. if (and_flags) {
  1087. // special case for preventing item joining altogether
  1088. if (and_flags & RasterizerStorageCommon::PREVENT_ITEM_JOINING) {
  1089. join = false;
  1090. //r_batch_break = true; // don't think we need a batch break
  1091. // save the flags so that they don't need to be recalculated in the 2nd pass
  1092. bdata.joined_item_batch_flags |= r_ris.shader_cache->canvas_item.batch_flags;
  1093. } else {
  1094. bool use_larger_fvfs = true;
  1095. if (and_flags == RasterizerStorageCommon::PREVENT_COLOR_BAKING) {
  1096. // in some circumstances, if the modulate is identity, we still allow baking because reading modulate / color
  1097. // will still be okay to do in the shader with no ill effects
  1098. if (r_ris.final_modulate == Color(1, 1, 1, 1)) {
  1099. use_larger_fvfs = false;
  1100. }
  1101. }
  1102. // new .. always use large FVF
  1103. if (use_larger_fvfs) {
  1104. if (and_flags == RasterizerStorageCommon::PREVENT_COLOR_BAKING) {
  1105. bdata.joined_item_batch_flags |= RasterizerStorageCommon::USE_MODULATE_FVF;
  1106. } else {
  1107. // we need to save on the joined item that it should use large fvf.
  1108. // This info will then be used in filling and rendering
  1109. bdata.joined_item_batch_flags |= RasterizerStorageCommon::USE_LARGE_FVF;
  1110. }
  1111. bdata.joined_item_batch_flags |= r_ris.shader_cache->canvas_item.batch_flags;
  1112. }
  1113. /*
  1114. if (and_flags == RasterizerStorageCommon::PREVENT_COLOR_BAKING) {
  1115. // in some circumstances, if the modulate is identity, we still allow baking because reading modulate / color
  1116. // will still be okay to do in the shader with no ill effects
  1117. if (r_ris.final_modulate == Color(1, 1, 1, 1)) {
  1118. break_batching = false;
  1119. }
  1120. else
  1121. {
  1122. // new .. large FVF
  1123. break_batching = false;
  1124. // we need to save on the joined item that it should use large fvf.
  1125. // This info will then be used in filling and rendering
  1126. bdata.joined_item_batch_flags |= RasterizerStorageCommon::USE_LARGE_FVF;
  1127. }
  1128. }
  1129. if (break_batching) {
  1130. join = false;
  1131. r_batch_break = true;
  1132. // save the flags so that they don't need to be recalculated in the 2nd pass
  1133. bdata.joined_item_batch_flags |= r_ris.shader_cache->canvas_item.batch_flags;
  1134. }
  1135. */
  1136. } // if not prevent item joining
  1137. }
  1138. }
  1139. if ((blend_mode == RasterizerStorageGLES2::Shader::CanvasItem::BLEND_MODE_MIX || blend_mode == RasterizerStorageGLES2::Shader::CanvasItem::BLEND_MODE_PMALPHA) && r_ris.item_group_light && !unshaded) {
  1140. // we cannot join lit items easily.
  1141. // it is possible, but not if they overlap, because
  1142. // a + light_blend + b + light_blend IS NOT THE SAME AS
  1143. // a + b + light_blend
  1144. bool light_allow_join = true;
  1145. // this is a quick getout if we have turned off light joining
  1146. if ((bdata.settings_light_max_join_items == 0) || r_ris.light_region.too_many_lights) {
  1147. light_allow_join = false;
  1148. } else {
  1149. // do light joining...
  1150. // first calculate the light bitfield
  1151. uint64_t light_bitfield = 0;
  1152. uint64_t shadow_bitfield = 0;
  1153. Light *light = r_ris.item_group_light;
  1154. int light_count = -1;
  1155. while (light) {
  1156. light_count++;
  1157. uint64_t light_bit = 1ULL << light_count;
  1158. // note that as a cost of batching, the light culling will be less effective
  1159. if (p_ci->light_mask & light->item_mask && r_ris.item_group_z >= light->z_min && r_ris.item_group_z <= light->z_max) {
  1160. // Note that with the above test, it is possible to also include a bound check.
  1161. // Tests so far have indicated better performance without it, but there may be reason to change this at a later stage,
  1162. // so I leave the line here for reference:
  1163. // && p_ci->global_rect_cache.intersects_transformed(light->xform_cache, light->rect_cache)) {
  1164. light_bitfield |= light_bit;
  1165. bool has_shadow = light->shadow_buffer.is_valid() && p_ci->light_mask & light->item_shadow_mask;
  1166. if (has_shadow) {
  1167. shadow_bitfield |= light_bit;
  1168. }
  1169. }
  1170. light = light->next_ptr;
  1171. }
  1172. // now compare to previous
  1173. if ((r_ris.light_region.light_bitfield != light_bitfield) || (r_ris.light_region.shadow_bitfield != shadow_bitfield)) {
  1174. light_allow_join = false;
  1175. r_ris.light_region.light_bitfield = light_bitfield;
  1176. r_ris.light_region.shadow_bitfield = shadow_bitfield;
  1177. } else {
  1178. // only do these checks if necessary
  1179. if (join && (!r_batch_break)) {
  1180. // we still can't join, even if the lights are exactly the same, if there is overlap between the previous and this item
  1181. if (r_ris.joined_item && light_bitfield) {
  1182. if ((int)r_ris.joined_item->num_item_refs <= bdata.settings_light_max_join_items) {
  1183. for (uint32_t r = 0; r < r_ris.joined_item->num_item_refs; r++) {
  1184. Item *pRefItem = bdata.item_refs[r_ris.joined_item->first_item_ref + r].item;
  1185. if (p_ci->global_rect_cache.intersects(pRefItem->global_rect_cache)) {
  1186. light_allow_join = false;
  1187. break;
  1188. }
  1189. }
  1190. #ifdef DEBUG_ENABLED
  1191. if (light_allow_join) {
  1192. bdata.stats_light_items_joined++;
  1193. }
  1194. #endif
  1195. } // if below max join items
  1196. else {
  1197. // just don't allow joining if above overlap check max items
  1198. light_allow_join = false;
  1199. }
  1200. }
  1201. } // if not batch broken already (no point in doing expensive overlap tests if not needed)
  1202. } // if bitfields don't match
  1203. } // if do light joining
  1204. if (!light_allow_join) {
  1205. // can't join
  1206. join = false;
  1207. // we also dont want to allow joining this item with the next item, because the next item could have no lights!
  1208. r_batch_break = true;
  1209. }
  1210. } else {
  1211. // if the last item had lights, we should not join it to this one (which has no lights)
  1212. if (r_ris.light_region.light_bitfield || r_ris.light_region.shadow_bitfield) {
  1213. join = false;
  1214. // setting these to zero ensures that any following item with lights will, by definition,
  1215. // be affected by a different set of lights, and thus prevent a join
  1216. r_ris.light_region.light_bitfield = 0;
  1217. r_ris.light_region.shadow_bitfield = 0;
  1218. }
  1219. }
  1220. if (reclip) {
  1221. join = false;
  1222. }
  1223. // non rects will break the batching anyway, we don't want to record item changes, detect this
  1224. if (!r_batch_break && _detect_item_batch_break(r_ris, p_ci, r_batch_break)) {
  1225. join = false;
  1226. r_batch_break = true;
  1227. }
  1228. return join;
  1229. }
  1230. // Legacy non-batched implementation for regression testing.
  1231. // Should be removed after testing phase to avoid duplicate codepaths.
  1232. void RasterizerCanvasGLES2::_legacy_canvas_render_item(Item *p_ci, RenderItemState &r_ris) {
  1233. storage->info.render._2d_item_count++;
  1234. if (r_ris.current_clip != p_ci->final_clip_owner) {
  1235. r_ris.current_clip = p_ci->final_clip_owner;
  1236. if (r_ris.current_clip) {
  1237. glEnable(GL_SCISSOR_TEST);
  1238. 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);
  1239. if (storage->frame.current_rt->flags[RasterizerStorage::RENDER_TARGET_VFLIP])
  1240. y = r_ris.current_clip->final_clip_rect.position.y;
  1241. 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);
  1242. } else {
  1243. glDisable(GL_SCISSOR_TEST);
  1244. }
  1245. }
  1246. // TODO: copy back buffer
  1247. if (p_ci->copy_back_buffer) {
  1248. if (p_ci->copy_back_buffer->full) {
  1249. _copy_texscreen(Rect2());
  1250. } else {
  1251. _copy_texscreen(p_ci->copy_back_buffer->rect);
  1252. }
  1253. }
  1254. RasterizerStorageGLES2::Skeleton *skeleton = NULL;
  1255. {
  1256. //skeleton handling
  1257. if (p_ci->skeleton.is_valid() && storage->skeleton_owner.owns(p_ci->skeleton)) {
  1258. skeleton = storage->skeleton_owner.get(p_ci->skeleton);
  1259. if (!skeleton->use_2d) {
  1260. skeleton = NULL;
  1261. } else {
  1262. state.skeleton_transform = r_ris.item_group_base_transform * skeleton->base_transform_2d;
  1263. state.skeleton_transform_inverse = state.skeleton_transform.affine_inverse();
  1264. state.skeleton_texture_size = Vector2(skeleton->size * 2, 0);
  1265. }
  1266. }
  1267. bool use_skeleton = skeleton != NULL;
  1268. if (r_ris.prev_use_skeleton != use_skeleton) {
  1269. r_ris.rebind_shader = true;
  1270. state.canvas_shader.set_conditional(CanvasShaderGLES2::USE_SKELETON, use_skeleton);
  1271. r_ris.prev_use_skeleton = use_skeleton;
  1272. }
  1273. if (skeleton) {
  1274. glActiveTexture(GL_TEXTURE0 + storage->config.max_texture_image_units - 3);
  1275. glBindTexture(GL_TEXTURE_2D, skeleton->tex_id);
  1276. state.using_skeleton = true;
  1277. } else {
  1278. state.using_skeleton = false;
  1279. }
  1280. }
  1281. Item *material_owner = p_ci->material_owner ? p_ci->material_owner : p_ci;
  1282. RID material = material_owner->material;
  1283. RasterizerStorageGLES2::Material *material_ptr = storage->material_owner.getornull(material);
  1284. if (material != r_ris.canvas_last_material || r_ris.rebind_shader) {
  1285. RasterizerStorageGLES2::Shader *shader_ptr = NULL;
  1286. if (material_ptr) {
  1287. shader_ptr = material_ptr->shader;
  1288. if (shader_ptr && shader_ptr->mode != VS::SHADER_CANVAS_ITEM) {
  1289. shader_ptr = NULL; // not a canvas item shader, don't use.
  1290. }
  1291. }
  1292. if (shader_ptr) {
  1293. if (shader_ptr->canvas_item.uses_screen_texture) {
  1294. if (!state.canvas_texscreen_used) {
  1295. //copy if not copied before
  1296. _copy_texscreen(Rect2());
  1297. // blend mode will have been enabled so make sure we disable it again later on
  1298. //last_blend_mode = last_blend_mode != RasterizerStorageGLES2::Shader::CanvasItem::BLEND_MODE_DISABLED ? last_blend_mode : -1;
  1299. }
  1300. if (storage->frame.current_rt->copy_screen_effect.color) {
  1301. glActiveTexture(GL_TEXTURE0 + storage->config.max_texture_image_units - 4);
  1302. glBindTexture(GL_TEXTURE_2D, storage->frame.current_rt->copy_screen_effect.color);
  1303. }
  1304. }
  1305. if (shader_ptr != r_ris.shader_cache) {
  1306. if (shader_ptr->canvas_item.uses_time) {
  1307. VisualServerRaster::redraw_request();
  1308. }
  1309. state.canvas_shader.set_custom_shader(shader_ptr->custom_code_id);
  1310. state.canvas_shader.bind();
  1311. }
  1312. int tc = material_ptr->textures.size();
  1313. Pair<StringName, RID> *textures = material_ptr->textures.ptrw();
  1314. ShaderLanguage::ShaderNode::Uniform::Hint *texture_hints = shader_ptr->texture_hints.ptrw();
  1315. for (int i = 0; i < tc; i++) {
  1316. glActiveTexture(GL_TEXTURE0 + i);
  1317. RasterizerStorageGLES2::Texture *t = storage->texture_owner.getornull(textures[i].second);
  1318. if (!t) {
  1319. switch (texture_hints[i]) {
  1320. case ShaderLanguage::ShaderNode::Uniform::HINT_BLACK_ALBEDO:
  1321. case ShaderLanguage::ShaderNode::Uniform::HINT_BLACK: {
  1322. glBindTexture(GL_TEXTURE_2D, storage->resources.black_tex);
  1323. } break;
  1324. case ShaderLanguage::ShaderNode::Uniform::HINT_ANISO: {
  1325. glBindTexture(GL_TEXTURE_2D, storage->resources.aniso_tex);
  1326. } break;
  1327. case ShaderLanguage::ShaderNode::Uniform::HINT_NORMAL: {
  1328. glBindTexture(GL_TEXTURE_2D, storage->resources.normal_tex);
  1329. } break;
  1330. default: {
  1331. glBindTexture(GL_TEXTURE_2D, storage->resources.white_tex);
  1332. } break;
  1333. }
  1334. continue;
  1335. }
  1336. if (t->redraw_if_visible) {
  1337. VisualServerRaster::redraw_request();
  1338. }
  1339. t = t->get_ptr();
  1340. #ifdef TOOLS_ENABLED
  1341. if (t->detect_normal && texture_hints[i] == ShaderLanguage::ShaderNode::Uniform::HINT_NORMAL) {
  1342. t->detect_normal(t->detect_normal_ud);
  1343. }
  1344. #endif
  1345. if (t->render_target)
  1346. t->render_target->used_in_frame = true;
  1347. glBindTexture(t->target, t->tex_id);
  1348. }
  1349. } else {
  1350. state.canvas_shader.set_custom_shader(0);
  1351. state.canvas_shader.bind();
  1352. }
  1353. state.canvas_shader.use_material((void *)material_ptr);
  1354. r_ris.shader_cache = shader_ptr;
  1355. r_ris.canvas_last_material = material;
  1356. r_ris.rebind_shader = false;
  1357. }
  1358. int blend_mode = r_ris.shader_cache ? r_ris.shader_cache->canvas_item.blend_mode : RasterizerStorageGLES2::Shader::CanvasItem::BLEND_MODE_MIX;
  1359. 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));
  1360. bool reclip = false;
  1361. if (r_ris.last_blend_mode != blend_mode) {
  1362. switch (blend_mode) {
  1363. case RasterizerStorageGLES2::Shader::CanvasItem::BLEND_MODE_MIX: {
  1364. glBlendEquation(GL_FUNC_ADD);
  1365. if (storage->frame.current_rt && storage->frame.current_rt->flags[RasterizerStorage::RENDER_TARGET_TRANSPARENT]) {
  1366. glBlendFuncSeparate(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA, GL_ONE, GL_ONE_MINUS_SRC_ALPHA);
  1367. } else {
  1368. glBlendFuncSeparate(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA, GL_ZERO, GL_ONE);
  1369. }
  1370. } break;
  1371. case RasterizerStorageGLES2::Shader::CanvasItem::BLEND_MODE_ADD: {
  1372. glBlendEquation(GL_FUNC_ADD);
  1373. if (storage->frame.current_rt && storage->frame.current_rt->flags[RasterizerStorage::RENDER_TARGET_TRANSPARENT]) {
  1374. glBlendFuncSeparate(GL_SRC_ALPHA, GL_ONE, GL_SRC_ALPHA, GL_ONE);
  1375. } else {
  1376. glBlendFuncSeparate(GL_SRC_ALPHA, GL_ONE, GL_ZERO, GL_ONE);
  1377. }
  1378. } break;
  1379. case RasterizerStorageGLES2::Shader::CanvasItem::BLEND_MODE_SUB: {
  1380. glBlendEquation(GL_FUNC_REVERSE_SUBTRACT);
  1381. if (storage->frame.current_rt && storage->frame.current_rt->flags[RasterizerStorage::RENDER_TARGET_TRANSPARENT]) {
  1382. glBlendFuncSeparate(GL_SRC_ALPHA, GL_ONE, GL_SRC_ALPHA, GL_ONE);
  1383. } else {
  1384. glBlendFuncSeparate(GL_SRC_ALPHA, GL_ONE, GL_ZERO, GL_ONE);
  1385. }
  1386. } break;
  1387. case RasterizerStorageGLES2::Shader::CanvasItem::BLEND_MODE_MUL: {
  1388. glBlendEquation(GL_FUNC_ADD);
  1389. if (storage->frame.current_rt && storage->frame.current_rt->flags[RasterizerStorage::RENDER_TARGET_TRANSPARENT]) {
  1390. glBlendFuncSeparate(GL_DST_COLOR, GL_ZERO, GL_DST_ALPHA, GL_ZERO);
  1391. } else {
  1392. glBlendFuncSeparate(GL_DST_COLOR, GL_ZERO, GL_ZERO, GL_ONE);
  1393. }
  1394. } break;
  1395. case RasterizerStorageGLES2::Shader::CanvasItem::BLEND_MODE_PMALPHA: {
  1396. glBlendEquation(GL_FUNC_ADD);
  1397. if (storage->frame.current_rt && storage->frame.current_rt->flags[RasterizerStorage::RENDER_TARGET_TRANSPARENT]) {
  1398. glBlendFuncSeparate(GL_ONE, GL_ONE_MINUS_SRC_ALPHA, GL_ONE, GL_ONE_MINUS_SRC_ALPHA);
  1399. } else {
  1400. glBlendFuncSeparate(GL_ONE, GL_ONE_MINUS_SRC_ALPHA, GL_ZERO, GL_ONE);
  1401. }
  1402. } break;
  1403. }
  1404. }
  1405. 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);
  1406. state.uniforms.modelview_matrix = p_ci->final_transform;
  1407. state.uniforms.extra_matrix = Transform2D();
  1408. _set_uniforms();
  1409. 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))
  1410. _legacy_canvas_item_render_commands(p_ci, NULL, reclip, material_ptr);
  1411. r_ris.rebind_shader = true; // hacked in for now.
  1412. if ((blend_mode == RasterizerStorageGLES2::Shader::CanvasItem::BLEND_MODE_MIX || blend_mode == RasterizerStorageGLES2::Shader::CanvasItem::BLEND_MODE_PMALPHA) && r_ris.item_group_light && !unshaded) {
  1413. Light *light = r_ris.item_group_light;
  1414. bool light_used = false;
  1415. VS::CanvasLightMode mode = VS::CANVAS_LIGHT_MODE_ADD;
  1416. state.uniforms.final_modulate = p_ci->final_modulate; // remove the canvas modulate
  1417. while (light) {
  1418. 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)) {
  1419. //intersects this light
  1420. if (!light_used || mode != light->mode) {
  1421. mode = light->mode;
  1422. switch (mode) {
  1423. case VS::CANVAS_LIGHT_MODE_ADD: {
  1424. glBlendEquation(GL_FUNC_ADD);
  1425. glBlendFunc(GL_SRC_ALPHA, GL_ONE);
  1426. } break;
  1427. case VS::CANVAS_LIGHT_MODE_SUB: {
  1428. glBlendEquation(GL_FUNC_REVERSE_SUBTRACT);
  1429. glBlendFunc(GL_SRC_ALPHA, GL_ONE);
  1430. } break;
  1431. case VS::CANVAS_LIGHT_MODE_MIX:
  1432. case VS::CANVAS_LIGHT_MODE_MASK: {
  1433. glBlendEquation(GL_FUNC_ADD);
  1434. glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
  1435. } break;
  1436. }
  1437. }
  1438. if (!light_used) {
  1439. state.canvas_shader.set_conditional(CanvasShaderGLES2::USE_LIGHTING, true);
  1440. light_used = true;
  1441. }
  1442. bool has_shadow = light->shadow_buffer.is_valid() && p_ci->light_mask & light->item_shadow_mask;
  1443. state.canvas_shader.set_conditional(CanvasShaderGLES2::USE_SHADOWS, has_shadow);
  1444. if (has_shadow) {
  1445. state.canvas_shader.set_conditional(CanvasShaderGLES2::SHADOW_USE_GRADIENT, light->shadow_gradient_length > 0);
  1446. state.canvas_shader.set_conditional(CanvasShaderGLES2::SHADOW_FILTER_NEAREST, light->shadow_filter == VS::CANVAS_LIGHT_FILTER_NONE);
  1447. state.canvas_shader.set_conditional(CanvasShaderGLES2::SHADOW_FILTER_PCF3, light->shadow_filter == VS::CANVAS_LIGHT_FILTER_PCF3);
  1448. state.canvas_shader.set_conditional(CanvasShaderGLES2::SHADOW_FILTER_PCF5, light->shadow_filter == VS::CANVAS_LIGHT_FILTER_PCF5);
  1449. state.canvas_shader.set_conditional(CanvasShaderGLES2::SHADOW_FILTER_PCF7, light->shadow_filter == VS::CANVAS_LIGHT_FILTER_PCF7);
  1450. state.canvas_shader.set_conditional(CanvasShaderGLES2::SHADOW_FILTER_PCF9, light->shadow_filter == VS::CANVAS_LIGHT_FILTER_PCF9);
  1451. state.canvas_shader.set_conditional(CanvasShaderGLES2::SHADOW_FILTER_PCF13, light->shadow_filter == VS::CANVAS_LIGHT_FILTER_PCF13);
  1452. }
  1453. state.canvas_shader.bind();
  1454. state.using_light = light;
  1455. state.using_shadow = has_shadow;
  1456. //always re-set uniforms, since light parameters changed
  1457. _set_uniforms();
  1458. state.canvas_shader.use_material((void *)material_ptr);
  1459. glActiveTexture(GL_TEXTURE0 + storage->config.max_texture_image_units - 6);
  1460. RasterizerStorageGLES2::Texture *t = storage->texture_owner.getornull(light->texture);
  1461. if (!t) {
  1462. glBindTexture(GL_TEXTURE_2D, storage->resources.white_tex);
  1463. } else {
  1464. t = t->get_ptr();
  1465. glBindTexture(t->target, t->tex_id);
  1466. }
  1467. glActiveTexture(GL_TEXTURE0);
  1468. _legacy_canvas_item_render_commands(p_ci, NULL, reclip, material_ptr); //redraw using light
  1469. state.using_light = NULL;
  1470. }
  1471. light = light->next_ptr;
  1472. }
  1473. if (light_used) {
  1474. state.canvas_shader.set_conditional(CanvasShaderGLES2::USE_LIGHTING, false);
  1475. state.canvas_shader.set_conditional(CanvasShaderGLES2::USE_SHADOWS, false);
  1476. state.canvas_shader.set_conditional(CanvasShaderGLES2::SHADOW_FILTER_NEAREST, false);
  1477. state.canvas_shader.set_conditional(CanvasShaderGLES2::SHADOW_FILTER_PCF3, false);
  1478. state.canvas_shader.set_conditional(CanvasShaderGLES2::SHADOW_FILTER_PCF5, false);
  1479. state.canvas_shader.set_conditional(CanvasShaderGLES2::SHADOW_FILTER_PCF7, false);
  1480. state.canvas_shader.set_conditional(CanvasShaderGLES2::SHADOW_FILTER_PCF9, false);
  1481. state.canvas_shader.set_conditional(CanvasShaderGLES2::SHADOW_FILTER_PCF13, false);
  1482. state.canvas_shader.bind();
  1483. r_ris.last_blend_mode = -1;
  1484. /*
  1485. //this is set again, so it should not be needed anyway?
  1486. state.canvas_item_modulate = unshaded ? ci->final_modulate : Color(
  1487. ci->final_modulate.r * p_modulate.r,
  1488. ci->final_modulate.g * p_modulate.g,
  1489. ci->final_modulate.b * p_modulate.b,
  1490. ci->final_modulate.a * p_modulate.a );
  1491. state.canvas_shader.set_uniform(CanvasShaderGLES2::MODELVIEW_MATRIX,state.final_transform);
  1492. state.canvas_shader.set_uniform(CanvasShaderGLES2::EXTRA_MATRIX,Transform2D());
  1493. state.canvas_shader.set_uniform(CanvasShaderGLES2::FINAL_MODULATE,state.canvas_item_modulate);
  1494. glBlendEquation(GL_FUNC_ADD);
  1495. if (storage->frame.current_rt->flags[RasterizerStorage::RENDER_TARGET_TRANSPARENT]) {
  1496. glBlendFuncSeparate(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA, GL_ONE, GL_ONE_MINUS_SRC_ALPHA);
  1497. } else {
  1498. glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
  1499. }
  1500. //@TODO RESET canvas_blend_mode
  1501. */
  1502. }
  1503. }
  1504. if (reclip) {
  1505. glEnable(GL_SCISSOR_TEST);
  1506. 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);
  1507. if (storage->frame.current_rt->flags[RasterizerStorage::RENDER_TARGET_VFLIP])
  1508. y = r_ris.current_clip->final_clip_rect.position.y;
  1509. 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);
  1510. }
  1511. }
  1512. void RasterizerCanvasGLES2::render_joined_item(const BItemJoined &p_bij, RenderItemState &r_ris) {
  1513. storage->info.render._2d_item_count++;
  1514. #if defined(TOOLS_ENABLED) && defined(DEBUG_ENABLED)
  1515. if (bdata.diagnose_frame) {
  1516. bdata.frame_string += _diagnose_make_item_joined_string(p_bij);
  1517. }
  1518. #endif
  1519. // all the joined items will share the same state with the first item
  1520. Item *ci = bdata.item_refs[p_bij.first_item_ref].item;
  1521. if (r_ris.current_clip != ci->final_clip_owner) {
  1522. r_ris.current_clip = ci->final_clip_owner;
  1523. if (r_ris.current_clip) {
  1524. glEnable(GL_SCISSOR_TEST);
  1525. 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);
  1526. if (storage->frame.current_rt->flags[RasterizerStorage::RENDER_TARGET_VFLIP])
  1527. y = r_ris.current_clip->final_clip_rect.position.y;
  1528. 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);
  1529. } else {
  1530. glDisable(GL_SCISSOR_TEST);
  1531. }
  1532. }
  1533. // TODO: copy back buffer
  1534. if (ci->copy_back_buffer) {
  1535. if (ci->copy_back_buffer->full) {
  1536. _copy_texscreen(Rect2());
  1537. } else {
  1538. _copy_texscreen(ci->copy_back_buffer->rect);
  1539. }
  1540. }
  1541. if (!bdata.settings_use_batching || !bdata.settings_use_software_skinning) {
  1542. RasterizerStorageGLES2::Skeleton *skeleton = NULL;
  1543. //skeleton handling
  1544. if (ci->skeleton.is_valid() && storage->skeleton_owner.owns(ci->skeleton)) {
  1545. skeleton = storage->skeleton_owner.get(ci->skeleton);
  1546. if (!skeleton->use_2d) {
  1547. skeleton = NULL;
  1548. } else {
  1549. state.skeleton_transform = r_ris.item_group_base_transform * skeleton->base_transform_2d;
  1550. state.skeleton_transform_inverse = state.skeleton_transform.affine_inverse();
  1551. state.skeleton_texture_size = Vector2(skeleton->size * 2, 0);
  1552. }
  1553. }
  1554. bool use_skeleton = skeleton != NULL;
  1555. if (r_ris.prev_use_skeleton != use_skeleton) {
  1556. r_ris.rebind_shader = true;
  1557. state.canvas_shader.set_conditional(CanvasShaderGLES2::USE_SKELETON, use_skeleton);
  1558. r_ris.prev_use_skeleton = use_skeleton;
  1559. }
  1560. if (skeleton) {
  1561. glActiveTexture(GL_TEXTURE0 + storage->config.max_texture_image_units - 3);
  1562. glBindTexture(GL_TEXTURE_2D, skeleton->tex_id);
  1563. state.using_skeleton = true;
  1564. } else {
  1565. state.using_skeleton = false;
  1566. }
  1567. } // if not using batching
  1568. Item *material_owner = ci->material_owner ? ci->material_owner : ci;
  1569. RID material = material_owner->material;
  1570. RasterizerStorageGLES2::Material *material_ptr = storage->material_owner.getornull(material);
  1571. if (material != r_ris.canvas_last_material || r_ris.rebind_shader) {
  1572. RasterizerStorageGLES2::Shader *shader_ptr = NULL;
  1573. if (material_ptr) {
  1574. shader_ptr = material_ptr->shader;
  1575. if (shader_ptr && shader_ptr->mode != VS::SHADER_CANVAS_ITEM) {
  1576. shader_ptr = NULL; // not a canvas item shader, don't use.
  1577. }
  1578. }
  1579. if (shader_ptr) {
  1580. if (shader_ptr->canvas_item.uses_screen_texture) {
  1581. if (!state.canvas_texscreen_used) {
  1582. //copy if not copied before
  1583. _copy_texscreen(Rect2());
  1584. // blend mode will have been enabled so make sure we disable it again later on
  1585. //last_blend_mode = last_blend_mode != RasterizerStorageGLES2::Shader::CanvasItem::BLEND_MODE_DISABLED ? last_blend_mode : -1;
  1586. }
  1587. if (storage->frame.current_rt->copy_screen_effect.color) {
  1588. glActiveTexture(GL_TEXTURE0 + storage->config.max_texture_image_units - 4);
  1589. glBindTexture(GL_TEXTURE_2D, storage->frame.current_rt->copy_screen_effect.color);
  1590. }
  1591. }
  1592. if (shader_ptr != r_ris.shader_cache) {
  1593. if (shader_ptr->canvas_item.uses_time) {
  1594. VisualServerRaster::redraw_request();
  1595. }
  1596. state.canvas_shader.set_custom_shader(shader_ptr->custom_code_id);
  1597. state.canvas_shader.bind();
  1598. }
  1599. int tc = material_ptr->textures.size();
  1600. Pair<StringName, RID> *textures = material_ptr->textures.ptrw();
  1601. ShaderLanguage::ShaderNode::Uniform::Hint *texture_hints = shader_ptr->texture_hints.ptrw();
  1602. for (int i = 0; i < tc; i++) {
  1603. glActiveTexture(GL_TEXTURE0 + i);
  1604. RasterizerStorageGLES2::Texture *t = storage->texture_owner.getornull(textures[i].second);
  1605. if (!t) {
  1606. switch (texture_hints[i]) {
  1607. case ShaderLanguage::ShaderNode::Uniform::HINT_BLACK_ALBEDO:
  1608. case ShaderLanguage::ShaderNode::Uniform::HINT_BLACK: {
  1609. glBindTexture(GL_TEXTURE_2D, storage->resources.black_tex);
  1610. } break;
  1611. case ShaderLanguage::ShaderNode::Uniform::HINT_ANISO: {
  1612. glBindTexture(GL_TEXTURE_2D, storage->resources.aniso_tex);
  1613. } break;
  1614. case ShaderLanguage::ShaderNode::Uniform::HINT_NORMAL: {
  1615. glBindTexture(GL_TEXTURE_2D, storage->resources.normal_tex);
  1616. } break;
  1617. default: {
  1618. glBindTexture(GL_TEXTURE_2D, storage->resources.white_tex);
  1619. } break;
  1620. }
  1621. continue;
  1622. }
  1623. if (t->redraw_if_visible) {
  1624. VisualServerRaster::redraw_request();
  1625. }
  1626. t = t->get_ptr();
  1627. #ifdef TOOLS_ENABLED
  1628. if (t->detect_normal && texture_hints[i] == ShaderLanguage::ShaderNode::Uniform::HINT_NORMAL) {
  1629. t->detect_normal(t->detect_normal_ud);
  1630. }
  1631. #endif
  1632. if (t->render_target)
  1633. t->render_target->used_in_frame = true;
  1634. glBindTexture(t->target, t->tex_id);
  1635. }
  1636. } else {
  1637. state.canvas_shader.set_custom_shader(0);
  1638. state.canvas_shader.bind();
  1639. }
  1640. state.canvas_shader.use_material((void *)material_ptr);
  1641. r_ris.shader_cache = shader_ptr;
  1642. r_ris.canvas_last_material = material;
  1643. r_ris.rebind_shader = false;
  1644. }
  1645. int blend_mode = r_ris.shader_cache ? r_ris.shader_cache->canvas_item.blend_mode : RasterizerStorageGLES2::Shader::CanvasItem::BLEND_MODE_MIX;
  1646. 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));
  1647. bool reclip = false;
  1648. if (r_ris.last_blend_mode != blend_mode) {
  1649. switch (blend_mode) {
  1650. case RasterizerStorageGLES2::Shader::CanvasItem::BLEND_MODE_MIX: {
  1651. glBlendEquation(GL_FUNC_ADD);
  1652. if (storage->frame.current_rt && storage->frame.current_rt->flags[RasterizerStorage::RENDER_TARGET_TRANSPARENT]) {
  1653. glBlendFuncSeparate(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA, GL_ONE, GL_ONE_MINUS_SRC_ALPHA);
  1654. } else {
  1655. glBlendFuncSeparate(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA, GL_ZERO, GL_ONE);
  1656. }
  1657. } break;
  1658. case RasterizerStorageGLES2::Shader::CanvasItem::BLEND_MODE_ADD: {
  1659. glBlendEquation(GL_FUNC_ADD);
  1660. if (storage->frame.current_rt && storage->frame.current_rt->flags[RasterizerStorage::RENDER_TARGET_TRANSPARENT]) {
  1661. glBlendFuncSeparate(GL_SRC_ALPHA, GL_ONE, GL_SRC_ALPHA, GL_ONE);
  1662. } else {
  1663. glBlendFuncSeparate(GL_SRC_ALPHA, GL_ONE, GL_ZERO, GL_ONE);
  1664. }
  1665. } break;
  1666. case RasterizerStorageGLES2::Shader::CanvasItem::BLEND_MODE_SUB: {
  1667. glBlendEquation(GL_FUNC_REVERSE_SUBTRACT);
  1668. if (storage->frame.current_rt && storage->frame.current_rt->flags[RasterizerStorage::RENDER_TARGET_TRANSPARENT]) {
  1669. glBlendFuncSeparate(GL_SRC_ALPHA, GL_ONE, GL_SRC_ALPHA, GL_ONE);
  1670. } else {
  1671. glBlendFuncSeparate(GL_SRC_ALPHA, GL_ONE, GL_ZERO, GL_ONE);
  1672. }
  1673. } break;
  1674. case RasterizerStorageGLES2::Shader::CanvasItem::BLEND_MODE_MUL: {
  1675. glBlendEquation(GL_FUNC_ADD);
  1676. if (storage->frame.current_rt && storage->frame.current_rt->flags[RasterizerStorage::RENDER_TARGET_TRANSPARENT]) {
  1677. glBlendFuncSeparate(GL_DST_COLOR, GL_ZERO, GL_DST_ALPHA, GL_ZERO);
  1678. } else {
  1679. glBlendFuncSeparate(GL_DST_COLOR, GL_ZERO, GL_ZERO, GL_ONE);
  1680. }
  1681. } break;
  1682. case RasterizerStorageGLES2::Shader::CanvasItem::BLEND_MODE_PMALPHA: {
  1683. glBlendEquation(GL_FUNC_ADD);
  1684. if (storage->frame.current_rt && storage->frame.current_rt->flags[RasterizerStorage::RENDER_TARGET_TRANSPARENT]) {
  1685. glBlendFuncSeparate(GL_ONE, GL_ONE_MINUS_SRC_ALPHA, GL_ONE, GL_ONE_MINUS_SRC_ALPHA);
  1686. } else {
  1687. glBlendFuncSeparate(GL_ONE, GL_ONE_MINUS_SRC_ALPHA, GL_ZERO, GL_ONE);
  1688. }
  1689. } break;
  1690. }
  1691. }
  1692. // using software transform?
  1693. // (i.e. don't send the transform matrix, send identity, and either use baked verts,
  1694. // or large fvf where the transform is done in the shader from transform stored in the fvf.)
  1695. if (!p_bij.is_single_item()) {
  1696. state.uniforms.modelview_matrix = Transform2D();
  1697. // final_modulate will be baked per item ref so the final_modulate can be an identity color
  1698. state.uniforms.final_modulate = Color(1, 1, 1, 1);
  1699. } else {
  1700. state.uniforms.modelview_matrix = ci->final_transform;
  1701. // could use the stored version of final_modulate in item ref? Test which is faster NYI
  1702. state.uniforms.final_modulate = unshaded ? ci->final_modulate : (ci->final_modulate * r_ris.item_group_modulate);
  1703. }
  1704. state.uniforms.extra_matrix = Transform2D();
  1705. _set_uniforms();
  1706. 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))
  1707. render_joined_item_commands(p_bij, NULL, reclip, material_ptr, false, r_ris);
  1708. r_ris.rebind_shader = true; // hacked in for now.
  1709. if ((blend_mode == RasterizerStorageGLES2::Shader::CanvasItem::BLEND_MODE_MIX || blend_mode == RasterizerStorageGLES2::Shader::CanvasItem::BLEND_MODE_PMALPHA) && r_ris.item_group_light && !unshaded) {
  1710. Light *light = r_ris.item_group_light;
  1711. bool light_used = false;
  1712. VS::CanvasLightMode mode = VS::CANVAS_LIGHT_MODE_ADD;
  1713. // we leave this set to 1, 1, 1, 1 if using software because the colors are baked into the vertices
  1714. if (p_bij.is_single_item()) {
  1715. state.uniforms.final_modulate = ci->final_modulate; // remove the canvas modulate
  1716. }
  1717. while (light) {
  1718. // use the bounding rect of the joined items, NOT only the bounding rect of the first item.
  1719. // note this is a cost of batching, the light culling will be less effective
  1720. // note that the r_ris.item_group_z will be out of date because we are using deferred rendering till canvas_render_items_end()
  1721. // so we have to test z against the stored value in the joined item
  1722. 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)) {
  1723. //intersects this light
  1724. if (!light_used || mode != light->mode) {
  1725. mode = light->mode;
  1726. switch (mode) {
  1727. case VS::CANVAS_LIGHT_MODE_ADD: {
  1728. glBlendEquation(GL_FUNC_ADD);
  1729. glBlendFunc(GL_SRC_ALPHA, GL_ONE);
  1730. } break;
  1731. case VS::CANVAS_LIGHT_MODE_SUB: {
  1732. glBlendEquation(GL_FUNC_REVERSE_SUBTRACT);
  1733. glBlendFunc(GL_SRC_ALPHA, GL_ONE);
  1734. } break;
  1735. case VS::CANVAS_LIGHT_MODE_MIX:
  1736. case VS::CANVAS_LIGHT_MODE_MASK: {
  1737. glBlendEquation(GL_FUNC_ADD);
  1738. glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
  1739. } break;
  1740. }
  1741. }
  1742. if (!light_used) {
  1743. state.canvas_shader.set_conditional(CanvasShaderGLES2::USE_LIGHTING, true);
  1744. light_used = true;
  1745. }
  1746. bool has_shadow = light->shadow_buffer.is_valid() && ci->light_mask & light->item_shadow_mask;
  1747. state.canvas_shader.set_conditional(CanvasShaderGLES2::USE_SHADOWS, has_shadow);
  1748. if (has_shadow) {
  1749. state.canvas_shader.set_conditional(CanvasShaderGLES2::SHADOW_USE_GRADIENT, light->shadow_gradient_length > 0);
  1750. state.canvas_shader.set_conditional(CanvasShaderGLES2::SHADOW_FILTER_NEAREST, light->shadow_filter == VS::CANVAS_LIGHT_FILTER_NONE);
  1751. state.canvas_shader.set_conditional(CanvasShaderGLES2::SHADOW_FILTER_PCF3, light->shadow_filter == VS::CANVAS_LIGHT_FILTER_PCF3);
  1752. state.canvas_shader.set_conditional(CanvasShaderGLES2::SHADOW_FILTER_PCF5, light->shadow_filter == VS::CANVAS_LIGHT_FILTER_PCF5);
  1753. state.canvas_shader.set_conditional(CanvasShaderGLES2::SHADOW_FILTER_PCF7, light->shadow_filter == VS::CANVAS_LIGHT_FILTER_PCF7);
  1754. state.canvas_shader.set_conditional(CanvasShaderGLES2::SHADOW_FILTER_PCF9, light->shadow_filter == VS::CANVAS_LIGHT_FILTER_PCF9);
  1755. state.canvas_shader.set_conditional(CanvasShaderGLES2::SHADOW_FILTER_PCF13, light->shadow_filter == VS::CANVAS_LIGHT_FILTER_PCF13);
  1756. }
  1757. state.canvas_shader.bind();
  1758. state.using_light = light;
  1759. state.using_shadow = has_shadow;
  1760. //always re-set uniforms, since light parameters changed
  1761. _set_uniforms();
  1762. state.canvas_shader.use_material((void *)material_ptr);
  1763. glActiveTexture(GL_TEXTURE0 + storage->config.max_texture_image_units - 6);
  1764. RasterizerStorageGLES2::Texture *t = storage->texture_owner.getornull(light->texture);
  1765. if (!t) {
  1766. glBindTexture(GL_TEXTURE_2D, storage->resources.white_tex);
  1767. } else {
  1768. t = t->get_ptr();
  1769. glBindTexture(t->target, t->tex_id);
  1770. }
  1771. glActiveTexture(GL_TEXTURE0);
  1772. // redraw using light.
  1773. // if there is no clip item, we can consider scissoring to the intersection area between the light and the item
  1774. // this can greatly reduce fill rate ..
  1775. // at the cost of glScissor commands, so is optional
  1776. if (!bdata.settings_scissor_lights || r_ris.current_clip) {
  1777. render_joined_item_commands(p_bij, NULL, reclip, material_ptr, true, r_ris);
  1778. } else {
  1779. bool scissor = _light_scissor_begin(p_bij.bounding_rect, light->xform_cache, light->rect_cache);
  1780. render_joined_item_commands(p_bij, NULL, reclip, material_ptr, true, r_ris);
  1781. if (scissor) {
  1782. glDisable(GL_SCISSOR_TEST);
  1783. }
  1784. }
  1785. state.using_light = NULL;
  1786. }
  1787. light = light->next_ptr;
  1788. }
  1789. if (light_used) {
  1790. state.canvas_shader.set_conditional(CanvasShaderGLES2::USE_LIGHTING, false);
  1791. state.canvas_shader.set_conditional(CanvasShaderGLES2::USE_SHADOWS, false);
  1792. state.canvas_shader.set_conditional(CanvasShaderGLES2::SHADOW_FILTER_NEAREST, false);
  1793. state.canvas_shader.set_conditional(CanvasShaderGLES2::SHADOW_FILTER_PCF3, false);
  1794. state.canvas_shader.set_conditional(CanvasShaderGLES2::SHADOW_FILTER_PCF5, false);
  1795. state.canvas_shader.set_conditional(CanvasShaderGLES2::SHADOW_FILTER_PCF7, false);
  1796. state.canvas_shader.set_conditional(CanvasShaderGLES2::SHADOW_FILTER_PCF9, false);
  1797. state.canvas_shader.set_conditional(CanvasShaderGLES2::SHADOW_FILTER_PCF13, false);
  1798. state.canvas_shader.bind();
  1799. r_ris.last_blend_mode = -1;
  1800. /*
  1801. //this is set again, so it should not be needed anyway?
  1802. state.canvas_item_modulate = unshaded ? ci->final_modulate : Color(
  1803. ci->final_modulate.r * p_modulate.r,
  1804. ci->final_modulate.g * p_modulate.g,
  1805. ci->final_modulate.b * p_modulate.b,
  1806. ci->final_modulate.a * p_modulate.a );
  1807. state.canvas_shader.set_uniform(CanvasShaderGLES2::MODELVIEW_MATRIX,state.final_transform);
  1808. state.canvas_shader.set_uniform(CanvasShaderGLES2::EXTRA_MATRIX,Transform2D());
  1809. state.canvas_shader.set_uniform(CanvasShaderGLES2::FINAL_MODULATE,state.canvas_item_modulate);
  1810. glBlendEquation(GL_FUNC_ADD);
  1811. if (storage->frame.current_rt->flags[RasterizerStorage::RENDER_TARGET_TRANSPARENT]) {
  1812. glBlendFuncSeparate(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA, GL_ONE, GL_ONE_MINUS_SRC_ALPHA);
  1813. } else {
  1814. glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
  1815. }
  1816. //@TODO RESET canvas_blend_mode
  1817. */
  1818. }
  1819. }
  1820. if (reclip) {
  1821. glEnable(GL_SCISSOR_TEST);
  1822. 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);
  1823. if (storage->frame.current_rt->flags[RasterizerStorage::RENDER_TARGET_VFLIP])
  1824. y = r_ris.current_clip->final_clip_rect.position.y;
  1825. 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);
  1826. }
  1827. }
  1828. void RasterizerCanvasGLES2::gl_enable_scissor(int p_x, int p_y, int p_width, int p_height) const {
  1829. glEnable(GL_SCISSOR_TEST);
  1830. glScissor(p_x, p_y, p_width, p_height);
  1831. }
  1832. void RasterizerCanvasGLES2::gl_disable_scissor() const {
  1833. glDisable(GL_SCISSOR_TEST);
  1834. }
  1835. void RasterizerCanvasGLES2::initialize() {
  1836. RasterizerGLES2::gl_check_errors();
  1837. RasterizerCanvasBaseGLES2::initialize();
  1838. batch_initialize();
  1839. // just reserve some space (may not be needed as we are orphaning, but hey ho)
  1840. glGenBuffers(1, &bdata.gl_vertex_buffer);
  1841. if (bdata.vertex_buffer_size_bytes) {
  1842. glBindBuffer(GL_ARRAY_BUFFER, bdata.gl_vertex_buffer);
  1843. glBufferData(GL_ARRAY_BUFFER, bdata.vertex_buffer_size_bytes, NULL, GL_DYNAMIC_DRAW);
  1844. glBindBuffer(GL_ARRAY_BUFFER, 0);
  1845. // pre fill index buffer, the indices never need to change so can be static
  1846. glGenBuffers(1, &bdata.gl_index_buffer);
  1847. glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, bdata.gl_index_buffer);
  1848. Vector<uint16_t> indices;
  1849. indices.resize(bdata.index_buffer_size_units);
  1850. for (unsigned int q = 0; q < bdata.max_quads; q++) {
  1851. int i_pos = q * 6; // 6 inds per quad
  1852. int q_pos = q * 4; // 4 verts per quad
  1853. indices.set(i_pos, q_pos);
  1854. indices.set(i_pos + 1, q_pos + 1);
  1855. indices.set(i_pos + 2, q_pos + 2);
  1856. indices.set(i_pos + 3, q_pos);
  1857. indices.set(i_pos + 4, q_pos + 2);
  1858. indices.set(i_pos + 5, q_pos + 3);
  1859. // we can only use 16 bit indices in GLES2!
  1860. #ifdef DEBUG_ENABLED
  1861. CRASH_COND((q_pos + 3) > 65535);
  1862. #endif
  1863. }
  1864. glBufferData(GL_ELEMENT_ARRAY_BUFFER, bdata.index_buffer_size_bytes, &indices[0], GL_STATIC_DRAW);
  1865. glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0);
  1866. } // only if there is a vertex buffer (batching is on)
  1867. RasterizerGLES2::gl_check_errors();
  1868. }
  1869. RasterizerCanvasGLES2::RasterizerCanvasGLES2() {
  1870. batch_constructor();
  1871. }