rasterizer_canvas_gles2.cpp 89 KB

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