2
0

rasterizer_canvas_gles3.cpp 84 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257
  1. /*************************************************************************/
  2. /* rasterizer_canvas_gles3.cpp */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2020 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2020 Godot Engine contributors (cf. AUTHORS.md). */
  10. /* */
  11. /* Permission is hereby granted, free of charge, to any person obtaining */
  12. /* a copy of this software and associated documentation files (the */
  13. /* "Software"), to deal in the Software without restriction, including */
  14. /* without limitation the rights to use, copy, modify, merge, publish, */
  15. /* distribute, sublicense, and/or sell copies of the Software, and to */
  16. /* permit persons to whom the Software is furnished to do so, subject to */
  17. /* the following conditions: */
  18. /* */
  19. /* The above copyright notice and this permission notice shall be */
  20. /* included in all copies or substantial portions of the Software. */
  21. /* */
  22. /* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
  23. /* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
  24. /* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.*/
  25. /* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
  26. /* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
  27. /* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
  28. /* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
  29. /*************************************************************************/
  30. #include "rasterizer_canvas_gles3.h"
  31. #include "core/os/os.h"
  32. #include "core/project_settings.h"
  33. #include "rasterizer_scene_gles3.h"
  34. #include "servers/visual/visual_server_raster.h"
  35. #ifndef GLES_OVER_GL
  36. #define glClearDepth glClearDepthf
  37. #endif
  38. static _FORCE_INLINE_ void store_transform2d(const Transform2D &p_mtx, float *p_array) {
  39. p_array[0] = p_mtx.elements[0][0];
  40. p_array[1] = p_mtx.elements[0][1];
  41. p_array[2] = 0;
  42. p_array[3] = 0;
  43. p_array[4] = p_mtx.elements[1][0];
  44. p_array[5] = p_mtx.elements[1][1];
  45. p_array[6] = 0;
  46. p_array[7] = 0;
  47. p_array[8] = 0;
  48. p_array[9] = 0;
  49. p_array[10] = 1;
  50. p_array[11] = 0;
  51. p_array[12] = p_mtx.elements[2][0];
  52. p_array[13] = p_mtx.elements[2][1];
  53. p_array[14] = 0;
  54. p_array[15] = 1;
  55. }
  56. static _FORCE_INLINE_ void store_transform(const Transform &p_mtx, float *p_array) {
  57. p_array[0] = p_mtx.basis.elements[0][0];
  58. p_array[1] = p_mtx.basis.elements[1][0];
  59. p_array[2] = p_mtx.basis.elements[2][0];
  60. p_array[3] = 0;
  61. p_array[4] = p_mtx.basis.elements[0][1];
  62. p_array[5] = p_mtx.basis.elements[1][1];
  63. p_array[6] = p_mtx.basis.elements[2][1];
  64. p_array[7] = 0;
  65. p_array[8] = p_mtx.basis.elements[0][2];
  66. p_array[9] = p_mtx.basis.elements[1][2];
  67. p_array[10] = p_mtx.basis.elements[2][2];
  68. p_array[11] = 0;
  69. p_array[12] = p_mtx.origin.x;
  70. p_array[13] = p_mtx.origin.y;
  71. p_array[14] = p_mtx.origin.z;
  72. p_array[15] = 1;
  73. }
  74. static _FORCE_INLINE_ void store_camera(const CameraMatrix &p_mtx, float *p_array) {
  75. for (int i = 0; i < 4; i++) {
  76. for (int j = 0; j < 4; j++) {
  77. p_array[i * 4 + j] = p_mtx.matrix[i][j];
  78. }
  79. }
  80. }
  81. RID RasterizerCanvasGLES3::light_internal_create() {
  82. LightInternal *li = memnew(LightInternal);
  83. glGenBuffers(1, &li->ubo);
  84. glBindBuffer(GL_UNIFORM_BUFFER, li->ubo);
  85. glBufferData(GL_UNIFORM_BUFFER, sizeof(LightInternal::UBOData), &state.canvas_item_ubo_data, GL_DYNAMIC_DRAW);
  86. glBindBuffer(GL_UNIFORM_BUFFER, 0);
  87. return light_internal_owner.make_rid(li);
  88. }
  89. void RasterizerCanvasGLES3::light_internal_update(RID p_rid, Light *p_light) {
  90. LightInternal *li = light_internal_owner.getornull(p_rid);
  91. ERR_FAIL_COND(!li);
  92. store_transform2d(p_light->light_shader_xform, li->ubo_data.light_matrix);
  93. store_transform2d(p_light->xform_cache.affine_inverse(), li->ubo_data.local_matrix);
  94. store_camera(p_light->shadow_matrix_cache, li->ubo_data.shadow_matrix);
  95. for (int i = 0; i < 4; i++) {
  96. li->ubo_data.color[i] = p_light->color[i] * p_light->energy;
  97. li->ubo_data.shadow_color[i] = p_light->shadow_color[i];
  98. }
  99. li->ubo_data.light_pos[0] = p_light->light_shader_pos.x;
  100. li->ubo_data.light_pos[1] = p_light->light_shader_pos.y;
  101. li->ubo_data.shadowpixel_size = (1.0 / p_light->shadow_buffer_size) * (1.0 + p_light->shadow_smooth);
  102. li->ubo_data.light_outside_alpha = p_light->mode == VS::CANVAS_LIGHT_MODE_MASK ? 1.0 : 0.0;
  103. li->ubo_data.light_height = p_light->height;
  104. if (p_light->radius_cache == 0)
  105. li->ubo_data.shadow_gradient = 0;
  106. else
  107. li->ubo_data.shadow_gradient = p_light->shadow_gradient_length / (p_light->radius_cache * 1.1);
  108. li->ubo_data.shadow_distance_mult = (p_light->radius_cache * 1.1);
  109. glBindBuffer(GL_UNIFORM_BUFFER, li->ubo);
  110. glBufferData(GL_UNIFORM_BUFFER, sizeof(LightInternal::UBOData), &li->ubo_data, GL_DYNAMIC_DRAW);
  111. glBindBuffer(GL_UNIFORM_BUFFER, 0);
  112. }
  113. void RasterizerCanvasGLES3::light_internal_free(RID p_rid) {
  114. LightInternal *li = light_internal_owner.getornull(p_rid);
  115. ERR_FAIL_COND(!li);
  116. glDeleteBuffers(1, &li->ubo);
  117. light_internal_owner.free(p_rid);
  118. memdelete(li);
  119. }
  120. void RasterizerCanvasGLES3::canvas_begin() {
  121. if (storage->frame.current_rt && storage->frame.clear_request) {
  122. // a clear request may be pending, so do it
  123. bool transparent = storage->frame.current_rt->flags[RasterizerStorage::RENDER_TARGET_TRANSPARENT];
  124. glClearColor(storage->frame.clear_request_color.r,
  125. storage->frame.clear_request_color.g,
  126. storage->frame.clear_request_color.b,
  127. transparent ? storage->frame.clear_request_color.a : 1.0);
  128. glClear(GL_COLOR_BUFFER_BIT);
  129. storage->frame.clear_request = false;
  130. glColorMask(1, 1, 1, transparent ? 1 : 0);
  131. }
  132. reset_canvas();
  133. state.canvas_shader.set_conditional(CanvasShaderGLES3::USE_TEXTURE_RECT, true);
  134. state.canvas_shader.set_conditional(CanvasShaderGLES3::USE_LIGHTING, false);
  135. state.canvas_shader.set_conditional(CanvasShaderGLES3::USE_SHADOWS, false);
  136. state.canvas_shader.set_conditional(CanvasShaderGLES3::SHADOW_FILTER_NEAREST, false);
  137. state.canvas_shader.set_conditional(CanvasShaderGLES3::SHADOW_FILTER_PCF3, false);
  138. state.canvas_shader.set_conditional(CanvasShaderGLES3::SHADOW_FILTER_PCF5, false);
  139. state.canvas_shader.set_conditional(CanvasShaderGLES3::SHADOW_FILTER_PCF7, false);
  140. state.canvas_shader.set_conditional(CanvasShaderGLES3::SHADOW_FILTER_PCF9, false);
  141. state.canvas_shader.set_conditional(CanvasShaderGLES3::SHADOW_FILTER_PCF13, false);
  142. state.canvas_shader.set_conditional(CanvasShaderGLES3::USE_DISTANCE_FIELD, false);
  143. state.canvas_shader.set_conditional(CanvasShaderGLES3::USE_NINEPATCH, false);
  144. state.canvas_shader.set_conditional(CanvasShaderGLES3::USE_SKELETON, false);
  145. state.canvas_shader.set_custom_shader(0);
  146. state.canvas_shader.bind();
  147. state.canvas_shader.set_uniform(CanvasShaderGLES3::FINAL_MODULATE, Color(1, 1, 1, 1));
  148. state.canvas_shader.set_uniform(CanvasShaderGLES3::MODELVIEW_MATRIX, Transform2D());
  149. state.canvas_shader.set_uniform(CanvasShaderGLES3::EXTRA_MATRIX, Transform2D());
  150. if (storage->frame.current_rt) {
  151. state.canvas_shader.set_uniform(CanvasShaderGLES3::SCREEN_PIXEL_SIZE, Vector2(1.0 / storage->frame.current_rt->width, 1.0 / storage->frame.current_rt->height));
  152. } else {
  153. state.canvas_shader.set_uniform(CanvasShaderGLES3::SCREEN_PIXEL_SIZE, Vector2(1.0, 1.0));
  154. }
  155. //state.canvas_shader.set_uniform(CanvasShaderGLES3::PROJECTION_MATRIX,state.vp);
  156. //state.canvas_shader.set_uniform(CanvasShaderGLES3::MODELVIEW_MATRIX,Transform());
  157. //state.canvas_shader.set_uniform(CanvasShaderGLES3::EXTRA_MATRIX,Transform());
  158. glBindBufferBase(GL_UNIFORM_BUFFER, 0, state.canvas_item_ubo);
  159. glBindVertexArray(data.canvas_quad_array);
  160. state.using_texture_rect = true;
  161. state.using_ninepatch = false;
  162. state.using_skeleton = false;
  163. }
  164. void RasterizerCanvasGLES3::canvas_end() {
  165. glBindVertexArray(0);
  166. glBindBufferBase(GL_UNIFORM_BUFFER, 0, 0);
  167. glColorMask(1, 1, 1, 1);
  168. glVertexAttrib4f(VS::ARRAY_COLOR, 1, 1, 1, 1);
  169. state.using_texture_rect = false;
  170. state.using_ninepatch = false;
  171. }
  172. RasterizerStorageGLES3::Texture *RasterizerCanvasGLES3::_bind_canvas_texture(const RID &p_texture, const RID &p_normal_map, bool p_force) {
  173. RasterizerStorageGLES3::Texture *tex_return = NULL;
  174. if (p_texture == state.current_tex && !p_force) {
  175. tex_return = state.current_tex_ptr;
  176. } else if (p_texture.is_valid()) {
  177. RasterizerStorageGLES3::Texture *texture = storage->texture_owner.getornull(p_texture);
  178. if (!texture) {
  179. state.current_tex = RID();
  180. state.current_tex_ptr = NULL;
  181. glActiveTexture(GL_TEXTURE0);
  182. glBindTexture(GL_TEXTURE_2D, storage->resources.white_tex);
  183. } else {
  184. if (texture->redraw_if_visible) { //check before proxy, because this is usually used with proxies
  185. VisualServerRaster::redraw_request();
  186. }
  187. texture = texture->get_ptr();
  188. if (texture->render_target)
  189. texture->render_target->used_in_frame = true;
  190. glActiveTexture(GL_TEXTURE0);
  191. glBindTexture(GL_TEXTURE_2D, texture->tex_id);
  192. state.current_tex = p_texture;
  193. state.current_tex_ptr = texture;
  194. tex_return = texture;
  195. }
  196. } else {
  197. glActiveTexture(GL_TEXTURE0);
  198. glBindTexture(GL_TEXTURE_2D, storage->resources.white_tex);
  199. state.current_tex = RID();
  200. state.current_tex_ptr = NULL;
  201. }
  202. if (p_normal_map == state.current_normal && !p_force) {
  203. //do none
  204. state.canvas_shader.set_uniform(CanvasShaderGLES3::USE_DEFAULT_NORMAL, state.current_normal.is_valid());
  205. } else if (p_normal_map.is_valid()) {
  206. RasterizerStorageGLES3::Texture *normal_map = storage->texture_owner.getornull(p_normal_map);
  207. if (!normal_map) {
  208. state.current_normal = RID();
  209. glActiveTexture(GL_TEXTURE1);
  210. glBindTexture(GL_TEXTURE_2D, storage->resources.normal_tex);
  211. state.canvas_shader.set_uniform(CanvasShaderGLES3::USE_DEFAULT_NORMAL, false);
  212. } else {
  213. if (normal_map->redraw_if_visible) { //check before proxy, because this is usually used with proxies
  214. VisualServerRaster::redraw_request();
  215. }
  216. normal_map = normal_map->get_ptr();
  217. glActiveTexture(GL_TEXTURE1);
  218. glBindTexture(GL_TEXTURE_2D, normal_map->tex_id);
  219. state.current_normal = p_normal_map;
  220. state.canvas_shader.set_uniform(CanvasShaderGLES3::USE_DEFAULT_NORMAL, true);
  221. }
  222. } else {
  223. state.current_normal = RID();
  224. glActiveTexture(GL_TEXTURE1);
  225. glBindTexture(GL_TEXTURE_2D, storage->resources.normal_tex);
  226. state.canvas_shader.set_uniform(CanvasShaderGLES3::USE_DEFAULT_NORMAL, false);
  227. }
  228. return tex_return;
  229. }
  230. void RasterizerCanvasGLES3::_set_texture_rect_mode(bool p_enable, bool p_ninepatch) {
  231. if (state.using_texture_rect == p_enable && state.using_ninepatch == p_ninepatch)
  232. return;
  233. if (p_enable) {
  234. glBindVertexArray(data.canvas_quad_array);
  235. } else {
  236. glBindVertexArray(0);
  237. glBindBuffer(GL_ARRAY_BUFFER, 0);
  238. glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0);
  239. }
  240. state.canvas_shader.set_conditional(CanvasShaderGLES3::USE_NINEPATCH, p_ninepatch && p_enable);
  241. state.canvas_shader.set_conditional(CanvasShaderGLES3::USE_TEXTURE_RECT, p_enable);
  242. state.canvas_shader.bind();
  243. state.canvas_shader.set_uniform(CanvasShaderGLES3::FINAL_MODULATE, state.canvas_item_modulate);
  244. state.canvas_shader.set_uniform(CanvasShaderGLES3::MODELVIEW_MATRIX, state.final_transform);
  245. state.canvas_shader.set_uniform(CanvasShaderGLES3::EXTRA_MATRIX, state.extra_matrix);
  246. if (state.using_skeleton) {
  247. state.canvas_shader.set_uniform(CanvasShaderGLES3::SKELETON_TRANSFORM, state.skeleton_transform);
  248. state.canvas_shader.set_uniform(CanvasShaderGLES3::SKELETON_TRANSFORM_INVERSE, state.skeleton_transform_inverse);
  249. }
  250. if (storage->frame.current_rt) {
  251. state.canvas_shader.set_uniform(CanvasShaderGLES3::SCREEN_PIXEL_SIZE, Vector2(1.0 / storage->frame.current_rt->width, 1.0 / storage->frame.current_rt->height));
  252. } else {
  253. state.canvas_shader.set_uniform(CanvasShaderGLES3::SCREEN_PIXEL_SIZE, Vector2(1.0, 1.0));
  254. }
  255. state.using_texture_rect = p_enable;
  256. state.using_ninepatch = p_ninepatch;
  257. }
  258. void RasterizerCanvasGLES3::_draw_polygon(const int *p_indices, int p_index_count, int p_vertex_count, const Vector2 *p_vertices, const Vector2 *p_uvs, const Color *p_colors, bool p_singlecolor, const int *p_bones, const float *p_weights) {
  259. glBindVertexArray(data.polygon_buffer_pointer_array);
  260. glBindBuffer(GL_ARRAY_BUFFER, data.polygon_buffer);
  261. #ifndef GLES_OVER_GL
  262. // Orphan the buffer to avoid CPU/GPU sync points caused by glBufferSubData
  263. glBufferData(GL_ARRAY_BUFFER, data.polygon_buffer_size, NULL, GL_DYNAMIC_DRAW);
  264. #endif
  265. uint32_t buffer_ofs = 0;
  266. //vertex
  267. glBufferSubData(GL_ARRAY_BUFFER, buffer_ofs, sizeof(Vector2) * p_vertex_count, p_vertices);
  268. glEnableVertexAttribArray(VS::ARRAY_VERTEX);
  269. glVertexAttribPointer(VS::ARRAY_VERTEX, 2, GL_FLOAT, false, sizeof(Vector2), CAST_INT_TO_UCHAR_PTR(buffer_ofs));
  270. buffer_ofs += sizeof(Vector2) * p_vertex_count;
  271. //color
  272. #ifdef DEBUG_ENABLED
  273. ERR_FAIL_COND(buffer_ofs > data.polygon_buffer_size);
  274. #endif
  275. if (p_singlecolor) {
  276. glDisableVertexAttribArray(VS::ARRAY_COLOR);
  277. Color m = *p_colors;
  278. glVertexAttrib4f(VS::ARRAY_COLOR, m.r, m.g, m.b, m.a);
  279. } else if (!p_colors) {
  280. glDisableVertexAttribArray(VS::ARRAY_COLOR);
  281. glVertexAttrib4f(VS::ARRAY_COLOR, 1, 1, 1, 1);
  282. } else {
  283. glBufferSubData(GL_ARRAY_BUFFER, buffer_ofs, sizeof(Color) * p_vertex_count, p_colors);
  284. glEnableVertexAttribArray(VS::ARRAY_COLOR);
  285. glVertexAttribPointer(VS::ARRAY_COLOR, 4, GL_FLOAT, false, sizeof(Color), CAST_INT_TO_UCHAR_PTR(buffer_ofs));
  286. buffer_ofs += sizeof(Color) * p_vertex_count;
  287. }
  288. #ifdef DEBUG_ENABLED
  289. ERR_FAIL_COND(buffer_ofs > data.polygon_buffer_size);
  290. #endif
  291. if (p_uvs) {
  292. glBufferSubData(GL_ARRAY_BUFFER, buffer_ofs, sizeof(Vector2) * p_vertex_count, p_uvs);
  293. glEnableVertexAttribArray(VS::ARRAY_TEX_UV);
  294. glVertexAttribPointer(VS::ARRAY_TEX_UV, 2, GL_FLOAT, false, sizeof(Vector2), CAST_INT_TO_UCHAR_PTR(buffer_ofs));
  295. buffer_ofs += sizeof(Vector2) * p_vertex_count;
  296. } else {
  297. glDisableVertexAttribArray(VS::ARRAY_TEX_UV);
  298. }
  299. #ifdef DEBUG_ENABLED
  300. ERR_FAIL_COND(buffer_ofs > data.polygon_buffer_size);
  301. #endif
  302. if (p_bones && p_weights) {
  303. glBufferSubData(GL_ARRAY_BUFFER, buffer_ofs, sizeof(int) * 4 * p_vertex_count, p_bones);
  304. glEnableVertexAttribArray(VS::ARRAY_BONES);
  305. //glVertexAttribPointer(VS::ARRAY_BONES, 4, GL_UNSIGNED_INT, false, sizeof(int) * 4, ((uint8_t *)0) + buffer_ofs);
  306. glVertexAttribIPointer(VS::ARRAY_BONES, 4, GL_UNSIGNED_INT, sizeof(int) * 4, CAST_INT_TO_UCHAR_PTR(buffer_ofs));
  307. buffer_ofs += sizeof(int) * 4 * p_vertex_count;
  308. glBufferSubData(GL_ARRAY_BUFFER, buffer_ofs, sizeof(float) * 4 * p_vertex_count, p_weights);
  309. glEnableVertexAttribArray(VS::ARRAY_WEIGHTS);
  310. glVertexAttribPointer(VS::ARRAY_WEIGHTS, 4, GL_FLOAT, false, sizeof(float) * 4, CAST_INT_TO_UCHAR_PTR(buffer_ofs));
  311. buffer_ofs += sizeof(float) * 4 * p_vertex_count;
  312. } else if (state.using_skeleton) {
  313. glVertexAttribI4ui(VS::ARRAY_BONES, 0, 0, 0, 0);
  314. glVertexAttrib4f(VS::ARRAY_WEIGHTS, 0, 0, 0, 0);
  315. }
  316. #ifdef DEBUG_ENABLED
  317. ERR_FAIL_COND(buffer_ofs > data.polygon_buffer_size);
  318. #endif
  319. //bind the indices buffer.
  320. glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, data.polygon_index_buffer);
  321. #ifndef GLES_OVER_GL
  322. // Orphan the buffer to avoid CPU/GPU sync points caused by glBufferSubData
  323. glBufferData(GL_ELEMENT_ARRAY_BUFFER, data.polygon_index_buffer_size, NULL, GL_DYNAMIC_DRAW);
  324. #endif
  325. glBufferSubData(GL_ELEMENT_ARRAY_BUFFER, 0, sizeof(int) * p_index_count, p_indices);
  326. //draw the triangles.
  327. glDrawElements(GL_TRIANGLES, p_index_count, GL_UNSIGNED_INT, 0);
  328. storage->frame.canvas_draw_commands++;
  329. if (p_bones && p_weights) {
  330. //not used so often, so disable when used
  331. glDisableVertexAttribArray(VS::ARRAY_BONES);
  332. glDisableVertexAttribArray(VS::ARRAY_WEIGHTS);
  333. }
  334. glBindVertexArray(0);
  335. glBindBuffer(GL_ARRAY_BUFFER, 0);
  336. }
  337. void RasterizerCanvasGLES3::_draw_generic(GLuint p_primitive, int p_vertex_count, const Vector2 *p_vertices, const Vector2 *p_uvs, const Color *p_colors, bool p_singlecolor) {
  338. glBindVertexArray(data.polygon_buffer_pointer_array);
  339. glBindBuffer(GL_ARRAY_BUFFER, data.polygon_buffer);
  340. #ifndef GLES_OVER_GL
  341. // Orphan the buffer to avoid CPU/GPU sync points caused by glBufferSubData
  342. glBufferData(GL_ARRAY_BUFFER, data.polygon_buffer_size, NULL, GL_DYNAMIC_DRAW);
  343. #endif
  344. uint32_t buffer_ofs = 0;
  345. //vertex
  346. glBufferSubData(GL_ARRAY_BUFFER, buffer_ofs, sizeof(Vector2) * p_vertex_count, p_vertices);
  347. glEnableVertexAttribArray(VS::ARRAY_VERTEX);
  348. glVertexAttribPointer(VS::ARRAY_VERTEX, 2, GL_FLOAT, false, sizeof(Vector2), CAST_INT_TO_UCHAR_PTR(buffer_ofs));
  349. buffer_ofs += sizeof(Vector2) * p_vertex_count;
  350. //color
  351. if (p_singlecolor) {
  352. glDisableVertexAttribArray(VS::ARRAY_COLOR);
  353. Color m = *p_colors;
  354. glVertexAttrib4f(VS::ARRAY_COLOR, m.r, m.g, m.b, m.a);
  355. } else if (!p_colors) {
  356. glDisableVertexAttribArray(VS::ARRAY_COLOR);
  357. glVertexAttrib4f(VS::ARRAY_COLOR, 1, 1, 1, 1);
  358. } else {
  359. glBufferSubData(GL_ARRAY_BUFFER, buffer_ofs, sizeof(Color) * p_vertex_count, p_colors);
  360. glEnableVertexAttribArray(VS::ARRAY_COLOR);
  361. glVertexAttribPointer(VS::ARRAY_COLOR, 4, GL_FLOAT, false, sizeof(Color), CAST_INT_TO_UCHAR_PTR(buffer_ofs));
  362. buffer_ofs += sizeof(Color) * p_vertex_count;
  363. }
  364. if (p_uvs) {
  365. glBufferSubData(GL_ARRAY_BUFFER, buffer_ofs, sizeof(Vector2) * p_vertex_count, p_uvs);
  366. glEnableVertexAttribArray(VS::ARRAY_TEX_UV);
  367. glVertexAttribPointer(VS::ARRAY_TEX_UV, 2, GL_FLOAT, false, sizeof(Vector2), CAST_INT_TO_UCHAR_PTR(buffer_ofs));
  368. buffer_ofs += sizeof(Vector2) * p_vertex_count;
  369. } else {
  370. glDisableVertexAttribArray(VS::ARRAY_TEX_UV);
  371. }
  372. glDrawArrays(p_primitive, 0, p_vertex_count);
  373. storage->frame.canvas_draw_commands++;
  374. glBindVertexArray(0);
  375. glBindBuffer(GL_ARRAY_BUFFER, 0);
  376. }
  377. void RasterizerCanvasGLES3::_draw_generic_indices(GLuint p_primitive, const int *p_indices, int p_index_count, int p_vertex_count, const Vector2 *p_vertices, const Vector2 *p_uvs, const Color *p_colors, bool p_singlecolor) {
  378. glBindVertexArray(data.polygon_buffer_pointer_array);
  379. glBindBuffer(GL_ARRAY_BUFFER, data.polygon_buffer);
  380. #ifndef GLES_OVER_GL
  381. // Orphan the buffer to avoid CPU/GPU sync points caused by glBufferSubData
  382. glBufferData(GL_ARRAY_BUFFER, data.polygon_buffer_size, NULL, GL_DYNAMIC_DRAW);
  383. #endif
  384. uint32_t buffer_ofs = 0;
  385. //vertex
  386. glBufferSubData(GL_ARRAY_BUFFER, buffer_ofs, sizeof(Vector2) * p_vertex_count, p_vertices);
  387. glEnableVertexAttribArray(VS::ARRAY_VERTEX);
  388. glVertexAttribPointer(VS::ARRAY_VERTEX, 2, GL_FLOAT, false, sizeof(Vector2), CAST_INT_TO_UCHAR_PTR(buffer_ofs));
  389. buffer_ofs += sizeof(Vector2) * p_vertex_count;
  390. //color
  391. #ifdef DEBUG_ENABLED
  392. ERR_FAIL_COND(buffer_ofs > data.polygon_buffer_size);
  393. #endif
  394. if (p_singlecolor) {
  395. glDisableVertexAttribArray(VS::ARRAY_COLOR);
  396. Color m = *p_colors;
  397. glVertexAttrib4f(VS::ARRAY_COLOR, m.r, m.g, m.b, m.a);
  398. } else if (!p_colors) {
  399. glDisableVertexAttribArray(VS::ARRAY_COLOR);
  400. glVertexAttrib4f(VS::ARRAY_COLOR, 1, 1, 1, 1);
  401. } else {
  402. glBufferSubData(GL_ARRAY_BUFFER, buffer_ofs, sizeof(Color) * p_vertex_count, p_colors);
  403. glEnableVertexAttribArray(VS::ARRAY_COLOR);
  404. glVertexAttribPointer(VS::ARRAY_COLOR, 4, GL_FLOAT, false, sizeof(Color), CAST_INT_TO_UCHAR_PTR(buffer_ofs));
  405. buffer_ofs += sizeof(Color) * p_vertex_count;
  406. }
  407. #ifdef DEBUG_ENABLED
  408. ERR_FAIL_COND(buffer_ofs > data.polygon_buffer_size);
  409. #endif
  410. if (p_uvs) {
  411. glBufferSubData(GL_ARRAY_BUFFER, buffer_ofs, sizeof(Vector2) * p_vertex_count, p_uvs);
  412. glEnableVertexAttribArray(VS::ARRAY_TEX_UV);
  413. glVertexAttribPointer(VS::ARRAY_TEX_UV, 2, GL_FLOAT, false, sizeof(Vector2), CAST_INT_TO_UCHAR_PTR(buffer_ofs));
  414. buffer_ofs += sizeof(Vector2) * p_vertex_count;
  415. } else {
  416. glDisableVertexAttribArray(VS::ARRAY_TEX_UV);
  417. }
  418. #ifdef DEBUG_ENABLED
  419. ERR_FAIL_COND(buffer_ofs > data.polygon_buffer_size);
  420. #endif
  421. //bind the indices buffer.
  422. glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, data.polygon_index_buffer);
  423. #ifndef GLES_OVER_GL
  424. // Orphan the buffer to avoid CPU/GPU sync points caused by glBufferSubData
  425. glBufferData(GL_ELEMENT_ARRAY_BUFFER, data.polygon_index_buffer_size, NULL, GL_DYNAMIC_DRAW);
  426. #endif
  427. glBufferSubData(GL_ELEMENT_ARRAY_BUFFER, 0, sizeof(int) * p_index_count, p_indices);
  428. //draw the triangles.
  429. glDrawElements(p_primitive, p_index_count, GL_UNSIGNED_INT, 0);
  430. storage->frame.canvas_draw_commands++;
  431. glBindVertexArray(0);
  432. glBindBuffer(GL_ARRAY_BUFFER, 0);
  433. }
  434. void RasterizerCanvasGLES3::_draw_gui_primitive(int p_points, const Vector2 *p_vertices, const Color *p_colors, const Vector2 *p_uvs) {
  435. static const GLenum prim[5] = { GL_POINTS, GL_POINTS, GL_LINES, GL_TRIANGLES, GL_TRIANGLE_FAN };
  436. //#define GLES_USE_PRIMITIVE_BUFFER
  437. int version = 0;
  438. int color_ofs = 0;
  439. int uv_ofs = 0;
  440. int stride = 2;
  441. if (p_colors) { //color
  442. version |= 1;
  443. color_ofs = stride;
  444. stride += 4;
  445. }
  446. if (p_uvs) { //uv
  447. version |= 2;
  448. uv_ofs = stride;
  449. stride += 2;
  450. }
  451. float b[(2 + 2 + 4) * 4];
  452. for (int i = 0; i < p_points; i++) {
  453. b[stride * i + 0] = p_vertices[i].x;
  454. b[stride * i + 1] = p_vertices[i].y;
  455. }
  456. if (p_colors) {
  457. for (int i = 0; i < p_points; i++) {
  458. b[stride * i + color_ofs + 0] = p_colors[i].r;
  459. b[stride * i + color_ofs + 1] = p_colors[i].g;
  460. b[stride * i + color_ofs + 2] = p_colors[i].b;
  461. b[stride * i + color_ofs + 3] = p_colors[i].a;
  462. }
  463. }
  464. if (p_uvs) {
  465. for (int i = 0; i < p_points; i++) {
  466. b[stride * i + uv_ofs + 0] = p_uvs[i].x;
  467. b[stride * i + uv_ofs + 1] = p_uvs[i].y;
  468. }
  469. }
  470. glBindBuffer(GL_ARRAY_BUFFER, data.polygon_buffer);
  471. #ifndef GLES_OVER_GL
  472. // Orphan the buffer to avoid CPU/GPU sync points caused by glBufferSubData
  473. glBufferData(GL_ARRAY_BUFFER, data.polygon_buffer_size, NULL, GL_DYNAMIC_DRAW);
  474. #endif
  475. //TODO the below call may need to be replaced with: glBufferSubData(GL_ARRAY_BUFFER, 0, p_points * stride * 4 * sizeof(float), &b[0]);
  476. glBufferSubData(GL_ARRAY_BUFFER, 0, p_points * stride * 4, &b[0]);
  477. glBindVertexArray(data.polygon_buffer_quad_arrays[version]);
  478. glDrawArrays(prim[p_points], 0, p_points);
  479. glBindVertexArray(0);
  480. glBindBuffer(GL_ARRAY_BUFFER, 0);
  481. storage->frame.canvas_draw_commands++;
  482. }
  483. static const GLenum gl_primitive[] = {
  484. GL_POINTS,
  485. GL_LINES,
  486. GL_LINE_STRIP,
  487. GL_LINE_LOOP,
  488. GL_TRIANGLES,
  489. GL_TRIANGLE_STRIP,
  490. GL_TRIANGLE_FAN
  491. };
  492. void RasterizerCanvasGLES3::_canvas_item_render_commands(Item *p_item, Item *current_clip, bool &reclip) {
  493. int cc = p_item->commands.size();
  494. Item::Command **commands = p_item->commands.ptrw();
  495. for (int i = 0; i < cc; i++) {
  496. Item::Command *c = commands[i];
  497. switch (c->type) {
  498. case Item::Command::TYPE_LINE: {
  499. Item::CommandLine *line = static_cast<Item::CommandLine *>(c);
  500. _set_texture_rect_mode(false);
  501. _bind_canvas_texture(RID(), RID());
  502. glVertexAttrib4f(VS::ARRAY_COLOR, line->color.r, line->color.g, line->color.b, line->color.a);
  503. if (line->width <= 1) {
  504. Vector2 verts[2] = {
  505. Vector2(line->from.x, line->from.y),
  506. Vector2(line->to.x, line->to.y)
  507. };
  508. #ifdef GLES_OVER_GL
  509. if (line->antialiased)
  510. glEnable(GL_LINE_SMOOTH);
  511. #endif
  512. //glLineWidth(line->width);
  513. _draw_gui_primitive(2, verts, NULL, NULL);
  514. #ifdef GLES_OVER_GL
  515. if (line->antialiased)
  516. glDisable(GL_LINE_SMOOTH);
  517. #endif
  518. } else {
  519. //thicker line
  520. Vector2 t = (line->from - line->to).normalized().tangent() * line->width * 0.5;
  521. Vector2 verts[4] = {
  522. line->from - t,
  523. line->from + t,
  524. line->to + t,
  525. line->to - t,
  526. };
  527. //glLineWidth(line->width);
  528. _draw_gui_primitive(4, verts, NULL, NULL);
  529. #ifdef GLES_OVER_GL
  530. if (line->antialiased) {
  531. glEnable(GL_LINE_SMOOTH);
  532. for (int j = 0; j < 4; j++) {
  533. Vector2 vertsl[2] = {
  534. verts[j],
  535. verts[(j + 1) % 4],
  536. };
  537. _draw_gui_primitive(2, vertsl, NULL, NULL);
  538. }
  539. glDisable(GL_LINE_SMOOTH);
  540. }
  541. #endif
  542. }
  543. } break;
  544. case Item::Command::TYPE_POLYLINE: {
  545. Item::CommandPolyLine *pline = static_cast<Item::CommandPolyLine *>(c);
  546. _set_texture_rect_mode(false);
  547. _bind_canvas_texture(RID(), RID());
  548. if (pline->triangles.size()) {
  549. _draw_generic(GL_TRIANGLE_STRIP, pline->triangles.size(), pline->triangles.ptr(), NULL, pline->triangle_colors.ptr(), pline->triangle_colors.size() == 1);
  550. #ifdef GLES_OVER_GL
  551. glEnable(GL_LINE_SMOOTH);
  552. if (pline->multiline) {
  553. //needs to be different
  554. } else {
  555. _draw_generic(GL_LINE_LOOP, pline->lines.size(), pline->lines.ptr(), NULL, pline->line_colors.ptr(), pline->line_colors.size() == 1);
  556. }
  557. glDisable(GL_LINE_SMOOTH);
  558. #endif
  559. } else {
  560. #ifdef GLES_OVER_GL
  561. if (pline->antialiased)
  562. glEnable(GL_LINE_SMOOTH);
  563. #endif
  564. if (pline->multiline) {
  565. int todo = pline->lines.size() / 2;
  566. int max_per_call = data.polygon_buffer_size / (sizeof(real_t) * 4);
  567. int offset = 0;
  568. while (todo) {
  569. int to_draw = MIN(max_per_call, todo);
  570. _draw_generic(GL_LINES, to_draw * 2, &pline->lines.ptr()[offset], NULL, pline->line_colors.size() == 1 ? pline->line_colors.ptr() : &pline->line_colors.ptr()[offset], pline->line_colors.size() == 1);
  571. todo -= to_draw;
  572. offset += to_draw * 2;
  573. }
  574. } else {
  575. _draw_generic(GL_LINE_STRIP, pline->lines.size(), pline->lines.ptr(), NULL, pline->line_colors.ptr(), pline->line_colors.size() == 1);
  576. }
  577. #ifdef GLES_OVER_GL
  578. if (pline->antialiased)
  579. glDisable(GL_LINE_SMOOTH);
  580. #endif
  581. }
  582. } break;
  583. case Item::Command::TYPE_RECT: {
  584. Item::CommandRect *rect = static_cast<Item::CommandRect *>(c);
  585. _set_texture_rect_mode(true);
  586. //set color
  587. glVertexAttrib4f(VS::ARRAY_COLOR, rect->modulate.r, rect->modulate.g, rect->modulate.b, rect->modulate.a);
  588. RasterizerStorageGLES3::Texture *texture = _bind_canvas_texture(rect->texture, rect->normal_map);
  589. if (texture) {
  590. bool untile = false;
  591. if (rect->flags & CANVAS_RECT_TILE && !(texture->flags & VS::TEXTURE_FLAG_REPEAT)) {
  592. glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
  593. glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
  594. untile = true;
  595. }
  596. Size2 texpixel_size(1.0 / texture->width, 1.0 / texture->height);
  597. Rect2 src_rect = (rect->flags & CANVAS_RECT_REGION) ? Rect2(rect->source.position * texpixel_size, rect->source.size * texpixel_size) : Rect2(0, 0, 1, 1);
  598. Rect2 dst_rect = Rect2(rect->rect.position, rect->rect.size);
  599. if (dst_rect.size.width < 0) {
  600. dst_rect.position.x += dst_rect.size.width;
  601. dst_rect.size.width *= -1;
  602. }
  603. if (dst_rect.size.height < 0) {
  604. dst_rect.position.y += dst_rect.size.height;
  605. dst_rect.size.height *= -1;
  606. }
  607. if (rect->flags & CANVAS_RECT_FLIP_H) {
  608. src_rect.size.x *= -1;
  609. }
  610. if (rect->flags & CANVAS_RECT_FLIP_V) {
  611. src_rect.size.y *= -1;
  612. }
  613. if (rect->flags & CANVAS_RECT_TRANSPOSE) {
  614. dst_rect.size.x *= -1; // Encoding in the dst_rect.z uniform
  615. }
  616. state.canvas_shader.set_uniform(CanvasShaderGLES3::COLOR_TEXPIXEL_SIZE, texpixel_size);
  617. state.canvas_shader.set_uniform(CanvasShaderGLES3::DST_RECT, Color(dst_rect.position.x, dst_rect.position.y, dst_rect.size.x, dst_rect.size.y));
  618. state.canvas_shader.set_uniform(CanvasShaderGLES3::SRC_RECT, Color(src_rect.position.x, src_rect.position.y, src_rect.size.x, src_rect.size.y));
  619. state.canvas_shader.set_uniform(CanvasShaderGLES3::CLIP_RECT_UV, rect->flags & CANVAS_RECT_CLIP_UV);
  620. glDrawArrays(GL_TRIANGLE_FAN, 0, 4);
  621. if (untile) {
  622. glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
  623. glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
  624. }
  625. } else {
  626. Rect2 dst_rect = Rect2(rect->rect.position, rect->rect.size);
  627. if (dst_rect.size.width < 0) {
  628. dst_rect.position.x += dst_rect.size.width;
  629. dst_rect.size.width *= -1;
  630. }
  631. if (dst_rect.size.height < 0) {
  632. dst_rect.position.y += dst_rect.size.height;
  633. dst_rect.size.height *= -1;
  634. }
  635. state.canvas_shader.set_uniform(CanvasShaderGLES3::DST_RECT, Color(dst_rect.position.x, dst_rect.position.y, dst_rect.size.x, dst_rect.size.y));
  636. state.canvas_shader.set_uniform(CanvasShaderGLES3::SRC_RECT, Color(0, 0, 1, 1));
  637. state.canvas_shader.set_uniform(CanvasShaderGLES3::CLIP_RECT_UV, false);
  638. glDrawArrays(GL_TRIANGLE_FAN, 0, 4);
  639. }
  640. storage->frame.canvas_draw_commands++;
  641. } break;
  642. case Item::Command::TYPE_NINEPATCH: {
  643. Item::CommandNinePatch *np = static_cast<Item::CommandNinePatch *>(c);
  644. _set_texture_rect_mode(true, true);
  645. glVertexAttrib4f(VS::ARRAY_COLOR, np->color.r, np->color.g, np->color.b, np->color.a);
  646. RasterizerStorageGLES3::Texture *texture = _bind_canvas_texture(np->texture, np->normal_map);
  647. Size2 texpixel_size;
  648. if (!texture) {
  649. texpixel_size = Size2(1, 1);
  650. state.canvas_shader.set_uniform(CanvasShaderGLES3::SRC_RECT, Color(0, 0, 1, 1));
  651. } else {
  652. if (np->source != Rect2()) {
  653. texpixel_size = Size2(1.0 / np->source.size.width, 1.0 / np->source.size.height);
  654. state.canvas_shader.set_uniform(CanvasShaderGLES3::SRC_RECT, Color(np->source.position.x / texture->width, np->source.position.y / texture->height, np->source.size.x / texture->width, np->source.size.y / texture->height));
  655. } else {
  656. texpixel_size = Size2(1.0 / texture->width, 1.0 / texture->height);
  657. state.canvas_shader.set_uniform(CanvasShaderGLES3::SRC_RECT, Color(0, 0, 1, 1));
  658. }
  659. }
  660. state.canvas_shader.set_uniform(CanvasShaderGLES3::COLOR_TEXPIXEL_SIZE, texpixel_size);
  661. state.canvas_shader.set_uniform(CanvasShaderGLES3::CLIP_RECT_UV, false);
  662. state.canvas_shader.set_uniform(CanvasShaderGLES3::NP_REPEAT_H, int(np->axis_x));
  663. state.canvas_shader.set_uniform(CanvasShaderGLES3::NP_REPEAT_V, int(np->axis_y));
  664. state.canvas_shader.set_uniform(CanvasShaderGLES3::NP_DRAW_CENTER, np->draw_center);
  665. state.canvas_shader.set_uniform(CanvasShaderGLES3::NP_MARGINS, Color(np->margin[MARGIN_LEFT], np->margin[MARGIN_TOP], np->margin[MARGIN_RIGHT], np->margin[MARGIN_BOTTOM]));
  666. state.canvas_shader.set_uniform(CanvasShaderGLES3::DST_RECT, Color(np->rect.position.x, np->rect.position.y, np->rect.size.x, np->rect.size.y));
  667. glDrawArrays(GL_TRIANGLE_FAN, 0, 4);
  668. storage->frame.canvas_draw_commands++;
  669. } break;
  670. case Item::Command::TYPE_PRIMITIVE: {
  671. Item::CommandPrimitive *primitive = static_cast<Item::CommandPrimitive *>(c);
  672. _set_texture_rect_mode(false);
  673. ERR_CONTINUE(primitive->points.size() < 1);
  674. RasterizerStorageGLES3::Texture *texture = _bind_canvas_texture(primitive->texture, primitive->normal_map);
  675. if (texture) {
  676. Size2 texpixel_size(1.0 / texture->width, 1.0 / texture->height);
  677. state.canvas_shader.set_uniform(CanvasShaderGLES3::COLOR_TEXPIXEL_SIZE, texpixel_size);
  678. }
  679. if (primitive->colors.size() == 1 && primitive->points.size() > 1) {
  680. Color col = primitive->colors[0];
  681. glVertexAttrib4f(VS::ARRAY_COLOR, col.r, col.g, col.b, col.a);
  682. } else if (primitive->colors.empty()) {
  683. glVertexAttrib4f(VS::ARRAY_COLOR, 1, 1, 1, 1);
  684. }
  685. _draw_gui_primitive(primitive->points.size(), primitive->points.ptr(), primitive->colors.ptr(), primitive->uvs.ptr());
  686. } break;
  687. case Item::Command::TYPE_POLYGON: {
  688. Item::CommandPolygon *polygon = static_cast<Item::CommandPolygon *>(c);
  689. _set_texture_rect_mode(false);
  690. RasterizerStorageGLES3::Texture *texture = _bind_canvas_texture(polygon->texture, polygon->normal_map);
  691. if (texture) {
  692. Size2 texpixel_size(1.0 / texture->width, 1.0 / texture->height);
  693. state.canvas_shader.set_uniform(CanvasShaderGLES3::COLOR_TEXPIXEL_SIZE, texpixel_size);
  694. }
  695. _draw_polygon(polygon->indices.ptr(), polygon->count, polygon->points.size(), polygon->points.ptr(), polygon->uvs.ptr(), polygon->colors.ptr(), polygon->colors.size() == 1, polygon->bones.ptr(), polygon->weights.ptr());
  696. #ifdef GLES_OVER_GL
  697. if (polygon->antialiased) {
  698. glEnable(GL_LINE_SMOOTH);
  699. _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);
  700. glDisable(GL_LINE_SMOOTH);
  701. }
  702. #endif
  703. } break;
  704. case Item::Command::TYPE_MESH: {
  705. Item::CommandMesh *mesh = static_cast<Item::CommandMesh *>(c);
  706. _set_texture_rect_mode(false);
  707. RasterizerStorageGLES3::Texture *texture = _bind_canvas_texture(mesh->texture, mesh->normal_map);
  708. if (texture) {
  709. Size2 texpixel_size(1.0 / texture->width, 1.0 / texture->height);
  710. state.canvas_shader.set_uniform(CanvasShaderGLES3::COLOR_TEXPIXEL_SIZE, texpixel_size);
  711. }
  712. state.canvas_shader.set_uniform(CanvasShaderGLES3::MODELVIEW_MATRIX, state.final_transform * mesh->transform);
  713. RasterizerStorageGLES3::Mesh *mesh_data = storage->mesh_owner.getornull(mesh->mesh);
  714. if (mesh_data) {
  715. for (int j = 0; j < mesh_data->surfaces.size(); j++) {
  716. RasterizerStorageGLES3::Surface *s = mesh_data->surfaces[j];
  717. // 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
  718. glBindVertexArray(s->array_id);
  719. glVertexAttrib4f(VS::ARRAY_COLOR, mesh->modulate.r, mesh->modulate.g, mesh->modulate.b, mesh->modulate.a);
  720. if (s->index_array_len) {
  721. glDrawElements(gl_primitive[s->primitive], s->index_array_len, (s->array_len >= (1 << 16)) ? GL_UNSIGNED_INT : GL_UNSIGNED_SHORT, 0);
  722. } else {
  723. glDrawArrays(gl_primitive[s->primitive], 0, s->array_len);
  724. }
  725. glBindVertexArray(0);
  726. }
  727. }
  728. state.canvas_shader.set_uniform(CanvasShaderGLES3::MODELVIEW_MATRIX, state.final_transform);
  729. } break;
  730. case Item::Command::TYPE_MULTIMESH: {
  731. Item::CommandMultiMesh *mmesh = static_cast<Item::CommandMultiMesh *>(c);
  732. RasterizerStorageGLES3::MultiMesh *multi_mesh = storage->multimesh_owner.getornull(mmesh->multimesh);
  733. if (!multi_mesh)
  734. break;
  735. RasterizerStorageGLES3::Mesh *mesh_data = storage->mesh_owner.getornull(multi_mesh->mesh);
  736. if (!mesh_data)
  737. break;
  738. RasterizerStorageGLES3::Texture *texture = _bind_canvas_texture(mmesh->texture, mmesh->normal_map);
  739. state.canvas_shader.set_conditional(CanvasShaderGLES3::USE_INSTANCE_CUSTOM, multi_mesh->custom_data_format != VS::MULTIMESH_CUSTOM_DATA_NONE);
  740. state.canvas_shader.set_conditional(CanvasShaderGLES3::USE_INSTANCING, true);
  741. //reset shader and force rebind
  742. state.using_texture_rect = true;
  743. _set_texture_rect_mode(false);
  744. if (texture) {
  745. Size2 texpixel_size(1.0 / texture->width, 1.0 / texture->height);
  746. state.canvas_shader.set_uniform(CanvasShaderGLES3::COLOR_TEXPIXEL_SIZE, texpixel_size);
  747. }
  748. int amount = MIN(multi_mesh->size, multi_mesh->visible_instances);
  749. if (amount == -1) {
  750. amount = multi_mesh->size;
  751. }
  752. for (int j = 0; j < mesh_data->surfaces.size(); j++) {
  753. RasterizerStorageGLES3::Surface *s = mesh_data->surfaces[j];
  754. // 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
  755. glBindVertexArray(s->instancing_array_id);
  756. glBindBuffer(GL_ARRAY_BUFFER, multi_mesh->buffer); //modify the buffer
  757. int stride = (multi_mesh->xform_floats + multi_mesh->color_floats + multi_mesh->custom_data_floats) * 4;
  758. glEnableVertexAttribArray(8);
  759. glVertexAttribPointer(8, 4, GL_FLOAT, GL_FALSE, stride, CAST_INT_TO_UCHAR_PTR(0));
  760. glVertexAttribDivisor(8, 1);
  761. glEnableVertexAttribArray(9);
  762. glVertexAttribPointer(9, 4, GL_FLOAT, GL_FALSE, stride, CAST_INT_TO_UCHAR_PTR(4 * 4));
  763. glVertexAttribDivisor(9, 1);
  764. int color_ofs;
  765. if (multi_mesh->transform_format == VS::MULTIMESH_TRANSFORM_3D) {
  766. glEnableVertexAttribArray(10);
  767. glVertexAttribPointer(10, 4, GL_FLOAT, GL_FALSE, stride, CAST_INT_TO_UCHAR_PTR(8 * 4));
  768. glVertexAttribDivisor(10, 1);
  769. color_ofs = 12 * 4;
  770. } else {
  771. glDisableVertexAttribArray(10);
  772. glVertexAttrib4f(10, 0, 0, 1, 0);
  773. color_ofs = 8 * 4;
  774. }
  775. int custom_data_ofs = color_ofs;
  776. switch (multi_mesh->color_format) {
  777. case VS::MULTIMESH_COLOR_NONE: {
  778. glDisableVertexAttribArray(11);
  779. glVertexAttrib4f(11, 1, 1, 1, 1);
  780. } break;
  781. case VS::MULTIMESH_COLOR_8BIT: {
  782. glEnableVertexAttribArray(11);
  783. glVertexAttribPointer(11, 4, GL_UNSIGNED_BYTE, GL_TRUE, stride, CAST_INT_TO_UCHAR_PTR(color_ofs));
  784. glVertexAttribDivisor(11, 1);
  785. custom_data_ofs += 4;
  786. } break;
  787. case VS::MULTIMESH_COLOR_FLOAT: {
  788. glEnableVertexAttribArray(11);
  789. glVertexAttribPointer(11, 4, GL_FLOAT, GL_FALSE, stride, CAST_INT_TO_UCHAR_PTR(color_ofs));
  790. glVertexAttribDivisor(11, 1);
  791. custom_data_ofs += 4 * 4;
  792. } break;
  793. }
  794. switch (multi_mesh->custom_data_format) {
  795. case VS::MULTIMESH_CUSTOM_DATA_NONE: {
  796. glDisableVertexAttribArray(12);
  797. glVertexAttrib4f(12, 1, 1, 1, 1);
  798. } break;
  799. case VS::MULTIMESH_CUSTOM_DATA_8BIT: {
  800. glEnableVertexAttribArray(12);
  801. glVertexAttribPointer(12, 4, GL_UNSIGNED_BYTE, GL_TRUE, stride, CAST_INT_TO_UCHAR_PTR(custom_data_ofs));
  802. glVertexAttribDivisor(12, 1);
  803. } break;
  804. case VS::MULTIMESH_CUSTOM_DATA_FLOAT: {
  805. glEnableVertexAttribArray(12);
  806. glVertexAttribPointer(12, 4, GL_FLOAT, GL_FALSE, stride, CAST_INT_TO_UCHAR_PTR(custom_data_ofs));
  807. glVertexAttribDivisor(12, 1);
  808. } break;
  809. }
  810. if (s->index_array_len) {
  811. glDrawElementsInstanced(gl_primitive[s->primitive], s->index_array_len, (s->array_len >= (1 << 16)) ? GL_UNSIGNED_INT : GL_UNSIGNED_SHORT, 0, amount);
  812. } else {
  813. glDrawArraysInstanced(gl_primitive[s->primitive], 0, s->array_len, amount);
  814. }
  815. glBindVertexArray(0);
  816. }
  817. state.canvas_shader.set_conditional(CanvasShaderGLES3::USE_INSTANCE_CUSTOM, false);
  818. state.canvas_shader.set_conditional(CanvasShaderGLES3::USE_INSTANCING, false);
  819. state.using_texture_rect = true;
  820. _set_texture_rect_mode(false);
  821. } break;
  822. case Item::Command::TYPE_PARTICLES: {
  823. Item::CommandParticles *particles_cmd = static_cast<Item::CommandParticles *>(c);
  824. RasterizerStorageGLES3::Particles *particles = storage->particles_owner.getornull(particles_cmd->particles);
  825. if (!particles)
  826. break;
  827. if (particles->inactive && !particles->emitting)
  828. break;
  829. glVertexAttrib4f(VS::ARRAY_COLOR, 1, 1, 1, 1); //not used, so keep white
  830. VisualServerRaster::redraw_request();
  831. storage->particles_request_process(particles_cmd->particles);
  832. //enable instancing
  833. state.canvas_shader.set_conditional(CanvasShaderGLES3::USE_INSTANCE_CUSTOM, true);
  834. state.canvas_shader.set_conditional(CanvasShaderGLES3::USE_PARTICLES, true);
  835. state.canvas_shader.set_conditional(CanvasShaderGLES3::USE_INSTANCING, true);
  836. //reset shader and force rebind
  837. state.using_texture_rect = true;
  838. _set_texture_rect_mode(false);
  839. RasterizerStorageGLES3::Texture *texture = _bind_canvas_texture(particles_cmd->texture, particles_cmd->normal_map);
  840. if (texture) {
  841. Size2 texpixel_size(1.0 / texture->width, 1.0 / texture->height);
  842. state.canvas_shader.set_uniform(CanvasShaderGLES3::COLOR_TEXPIXEL_SIZE, texpixel_size);
  843. } else {
  844. state.canvas_shader.set_uniform(CanvasShaderGLES3::COLOR_TEXPIXEL_SIZE, Vector2(1.0, 1.0));
  845. }
  846. if (!particles->use_local_coords) {
  847. Transform2D inv_xf;
  848. inv_xf.set_axis(0, Vector2(particles->emission_transform.basis.get_axis(0).x, particles->emission_transform.basis.get_axis(0).y));
  849. inv_xf.set_axis(1, Vector2(particles->emission_transform.basis.get_axis(1).x, particles->emission_transform.basis.get_axis(1).y));
  850. inv_xf.set_origin(Vector2(particles->emission_transform.get_origin().x, particles->emission_transform.get_origin().y));
  851. inv_xf.affine_invert();
  852. state.canvas_shader.set_uniform(CanvasShaderGLES3::MODELVIEW_MATRIX, state.final_transform * inv_xf);
  853. }
  854. glBindVertexArray(data.particle_quad_array); //use particle quad array
  855. glBindBuffer(GL_ARRAY_BUFFER, particles->particle_buffers[0]); //bind particle buffer
  856. int stride = sizeof(float) * 4 * 6;
  857. int amount = particles->amount;
  858. if (particles->draw_order != VS::PARTICLES_DRAW_ORDER_LIFETIME) {
  859. glEnableVertexAttribArray(8); //xform x
  860. glVertexAttribPointer(8, 4, GL_FLOAT, GL_FALSE, stride, CAST_INT_TO_UCHAR_PTR(sizeof(float) * 4 * 3));
  861. glVertexAttribDivisor(8, 1);
  862. glEnableVertexAttribArray(9); //xform y
  863. glVertexAttribPointer(9, 4, GL_FLOAT, GL_FALSE, stride, CAST_INT_TO_UCHAR_PTR(sizeof(float) * 4 * 4));
  864. glVertexAttribDivisor(9, 1);
  865. glEnableVertexAttribArray(10); //xform z
  866. glVertexAttribPointer(10, 4, GL_FLOAT, GL_FALSE, stride, CAST_INT_TO_UCHAR_PTR(sizeof(float) * 4 * 5));
  867. glVertexAttribDivisor(10, 1);
  868. glEnableVertexAttribArray(11); //color
  869. glVertexAttribPointer(11, 4, GL_FLOAT, GL_FALSE, stride, NULL);
  870. glVertexAttribDivisor(11, 1);
  871. glEnableVertexAttribArray(12); //custom
  872. glVertexAttribPointer(12, 4, GL_FLOAT, GL_FALSE, stride, CAST_INT_TO_UCHAR_PTR(sizeof(float) * 4 * 2));
  873. glVertexAttribDivisor(12, 1);
  874. glDrawArraysInstanced(GL_TRIANGLE_FAN, 0, 4, amount);
  875. } else {
  876. //split
  877. int split = int(Math::ceil(particles->phase * particles->amount));
  878. if (amount - split > 0) {
  879. glEnableVertexAttribArray(8); //xform x
  880. glVertexAttribPointer(8, 4, GL_FLOAT, GL_FALSE, stride, CAST_INT_TO_UCHAR_PTR(stride * split + sizeof(float) * 4 * 3));
  881. glVertexAttribDivisor(8, 1);
  882. glEnableVertexAttribArray(9); //xform y
  883. glVertexAttribPointer(9, 4, GL_FLOAT, GL_FALSE, stride, CAST_INT_TO_UCHAR_PTR(stride * split + sizeof(float) * 4 * 4));
  884. glVertexAttribDivisor(9, 1);
  885. glEnableVertexAttribArray(10); //xform z
  886. glVertexAttribPointer(10, 4, GL_FLOAT, GL_FALSE, stride, CAST_INT_TO_UCHAR_PTR(stride * split + sizeof(float) * 4 * 5));
  887. glVertexAttribDivisor(10, 1);
  888. glEnableVertexAttribArray(11); //color
  889. glVertexAttribPointer(11, 4, GL_FLOAT, GL_FALSE, stride, CAST_INT_TO_UCHAR_PTR(stride * split + 0));
  890. glVertexAttribDivisor(11, 1);
  891. glEnableVertexAttribArray(12); //custom
  892. glVertexAttribPointer(12, 4, GL_FLOAT, GL_FALSE, stride, CAST_INT_TO_UCHAR_PTR(stride * split + sizeof(float) * 4 * 2));
  893. glVertexAttribDivisor(12, 1);
  894. glDrawArraysInstanced(GL_TRIANGLE_FAN, 0, 4, amount - split);
  895. }
  896. if (split > 0) {
  897. glEnableVertexAttribArray(8); //xform x
  898. glVertexAttribPointer(8, 4, GL_FLOAT, GL_FALSE, stride, CAST_INT_TO_UCHAR_PTR(sizeof(float) * 4 * 3));
  899. glVertexAttribDivisor(8, 1);
  900. glEnableVertexAttribArray(9); //xform y
  901. glVertexAttribPointer(9, 4, GL_FLOAT, GL_FALSE, stride, CAST_INT_TO_UCHAR_PTR(sizeof(float) * 4 * 4));
  902. glVertexAttribDivisor(9, 1);
  903. glEnableVertexAttribArray(10); //xform z
  904. glVertexAttribPointer(10, 4, GL_FLOAT, GL_FALSE, stride, CAST_INT_TO_UCHAR_PTR(sizeof(float) * 4 * 5));
  905. glVertexAttribDivisor(10, 1);
  906. glEnableVertexAttribArray(11); //color
  907. glVertexAttribPointer(11, 4, GL_FLOAT, GL_FALSE, stride, NULL);
  908. glVertexAttribDivisor(11, 1);
  909. glEnableVertexAttribArray(12); //custom
  910. glVertexAttribPointer(12, 4, GL_FLOAT, GL_FALSE, stride, CAST_INT_TO_UCHAR_PTR(sizeof(float) * 4 * 2));
  911. glVertexAttribDivisor(12, 1);
  912. glDrawArraysInstanced(GL_TRIANGLE_FAN, 0, 4, split);
  913. }
  914. }
  915. glBindVertexArray(0);
  916. state.canvas_shader.set_conditional(CanvasShaderGLES3::USE_INSTANCE_CUSTOM, false);
  917. state.canvas_shader.set_conditional(CanvasShaderGLES3::USE_PARTICLES, false);
  918. state.canvas_shader.set_conditional(CanvasShaderGLES3::USE_INSTANCING, false);
  919. state.using_texture_rect = true;
  920. _set_texture_rect_mode(false);
  921. } break;
  922. case Item::Command::TYPE_CIRCLE: {
  923. _set_texture_rect_mode(false);
  924. Item::CommandCircle *circle = static_cast<Item::CommandCircle *>(c);
  925. static const int numpoints = 32;
  926. Vector2 points[numpoints + 1];
  927. points[numpoints] = circle->pos;
  928. int indices[numpoints * 3];
  929. for (int j = 0; j < numpoints; j++) {
  930. points[j] = circle->pos + Vector2(Math::sin(j * Math_PI * 2.0 / numpoints), Math::cos(j * Math_PI * 2.0 / numpoints)) * circle->radius;
  931. indices[j * 3 + 0] = j;
  932. indices[j * 3 + 1] = (j + 1) % numpoints;
  933. indices[j * 3 + 2] = numpoints;
  934. }
  935. _bind_canvas_texture(RID(), RID());
  936. _draw_polygon(indices, numpoints * 3, numpoints + 1, points, NULL, &circle->color, true, NULL, NULL);
  937. //_draw_polygon(numpoints*3,indices,points,NULL,&circle->color,RID(),true);
  938. //canvas_draw_circle(circle->indices.size(),circle->indices.ptr(),circle->points.ptr(),circle->uvs.ptr(),circle->colors.ptr(),circle->texture,circle->colors.size()==1);
  939. } break;
  940. case Item::Command::TYPE_TRANSFORM: {
  941. Item::CommandTransform *transform = static_cast<Item::CommandTransform *>(c);
  942. state.extra_matrix = transform->xform;
  943. state.canvas_shader.set_uniform(CanvasShaderGLES3::EXTRA_MATRIX, state.extra_matrix);
  944. } break;
  945. case Item::Command::TYPE_CLIP_IGNORE: {
  946. Item::CommandClipIgnore *ci = static_cast<Item::CommandClipIgnore *>(c);
  947. if (current_clip) {
  948. if (ci->ignore != reclip) {
  949. if (ci->ignore) {
  950. glDisable(GL_SCISSOR_TEST);
  951. reclip = true;
  952. } else {
  953. glEnable(GL_SCISSOR_TEST);
  954. //glScissor(viewport.x+current_clip->final_clip_rect.pos.x,viewport.y+ (viewport.height-(current_clip->final_clip_rect.pos.y+current_clip->final_clip_rect.size.height)),
  955. //current_clip->final_clip_rect.size.width,current_clip->final_clip_rect.size.height);
  956. int y = storage->frame.current_rt->height - (current_clip->final_clip_rect.position.y + current_clip->final_clip_rect.size.y);
  957. if (storage->frame.current_rt->flags[RasterizerStorage::RENDER_TARGET_VFLIP])
  958. y = current_clip->final_clip_rect.position.y;
  959. glScissor(current_clip->final_clip_rect.position.x, y, current_clip->final_clip_rect.size.x, current_clip->final_clip_rect.size.y);
  960. reclip = false;
  961. }
  962. }
  963. }
  964. } break;
  965. }
  966. }
  967. }
  968. void RasterizerCanvasGLES3::_copy_texscreen(const Rect2 &p_rect) {
  969. ERR_FAIL_COND_MSG(storage->frame.current_rt->effects.mip_maps[0].sizes.size() == 0, "Can't use screen texture copying in a render target configured without copy buffers.");
  970. glDisable(GL_BLEND);
  971. state.canvas_texscreen_used = true;
  972. //blur diffuse into effect mipmaps using separatable convolution
  973. //storage->shaders.copy.set_conditional(CopyShaderGLES3::GAUSSIAN_HORIZONTAL,true);
  974. Vector2 wh(storage->frame.current_rt->width, storage->frame.current_rt->height);
  975. Color blur_section(p_rect.position.x / wh.x, p_rect.position.y / wh.y, p_rect.size.x / wh.x, p_rect.size.y / wh.y);
  976. if (p_rect != Rect2()) {
  977. scene_render->state.effect_blur_shader.set_conditional(EffectBlurShaderGLES3::USE_BLUR_SECTION, true);
  978. storage->shaders.copy.set_conditional(CopyShaderGLES3::USE_COPY_SECTION, true);
  979. }
  980. glBindFramebuffer(GL_FRAMEBUFFER, storage->frame.current_rt->effects.mip_maps[0].sizes[0].fbo);
  981. glActiveTexture(GL_TEXTURE0);
  982. glBindTexture(GL_TEXTURE_2D, storage->frame.current_rt->color);
  983. storage->shaders.copy.bind();
  984. storage->shaders.copy.set_uniform(CopyShaderGLES3::COPY_SECTION, blur_section);
  985. scene_render->_copy_screen();
  986. for (int i = 0; i < storage->frame.current_rt->effects.mip_maps[1].sizes.size(); i++) {
  987. int vp_w = storage->frame.current_rt->effects.mip_maps[1].sizes[i].width;
  988. int vp_h = storage->frame.current_rt->effects.mip_maps[1].sizes[i].height;
  989. glViewport(0, 0, vp_w, vp_h);
  990. //horizontal pass
  991. scene_render->state.effect_blur_shader.set_conditional(EffectBlurShaderGLES3::GAUSSIAN_HORIZONTAL, true);
  992. scene_render->state.effect_blur_shader.bind();
  993. scene_render->state.effect_blur_shader.set_uniform(EffectBlurShaderGLES3::PIXEL_SIZE, Vector2(1.0 / vp_w, 1.0 / vp_h));
  994. scene_render->state.effect_blur_shader.set_uniform(EffectBlurShaderGLES3::LOD, float(i));
  995. scene_render->state.effect_blur_shader.set_uniform(EffectBlurShaderGLES3::BLUR_SECTION, blur_section);
  996. glActiveTexture(GL_TEXTURE0);
  997. glBindTexture(GL_TEXTURE_2D, storage->frame.current_rt->effects.mip_maps[0].color); //previous level, since mipmaps[0] starts one level bigger
  998. glBindFramebuffer(GL_FRAMEBUFFER, storage->frame.current_rt->effects.mip_maps[1].sizes[i].fbo);
  999. scene_render->_copy_screen();
  1000. scene_render->state.effect_blur_shader.set_conditional(EffectBlurShaderGLES3::GAUSSIAN_HORIZONTAL, false);
  1001. //vertical pass
  1002. scene_render->state.effect_blur_shader.set_conditional(EffectBlurShaderGLES3::GAUSSIAN_VERTICAL, true);
  1003. scene_render->state.effect_blur_shader.bind();
  1004. scene_render->state.effect_blur_shader.set_uniform(EffectBlurShaderGLES3::PIXEL_SIZE, Vector2(1.0 / vp_w, 1.0 / vp_h));
  1005. scene_render->state.effect_blur_shader.set_uniform(EffectBlurShaderGLES3::LOD, float(i));
  1006. scene_render->state.effect_blur_shader.set_uniform(EffectBlurShaderGLES3::BLUR_SECTION, blur_section);
  1007. glActiveTexture(GL_TEXTURE0);
  1008. glBindTexture(GL_TEXTURE_2D, storage->frame.current_rt->effects.mip_maps[1].color);
  1009. glBindFramebuffer(GL_FRAMEBUFFER, storage->frame.current_rt->effects.mip_maps[0].sizes[i + 1].fbo); //next level, since mipmaps[0] starts one level bigger
  1010. scene_render->_copy_screen();
  1011. scene_render->state.effect_blur_shader.set_conditional(EffectBlurShaderGLES3::GAUSSIAN_VERTICAL, false);
  1012. }
  1013. scene_render->state.effect_blur_shader.set_conditional(EffectBlurShaderGLES3::USE_BLUR_SECTION, false);
  1014. storage->shaders.copy.set_conditional(CopyShaderGLES3::USE_COPY_SECTION, false);
  1015. glBindFramebuffer(GL_FRAMEBUFFER, storage->frame.current_rt->fbo); //back to front
  1016. glViewport(0, 0, storage->frame.current_rt->width, storage->frame.current_rt->height);
  1017. // back to canvas, force rebind
  1018. state.using_texture_rect = true;
  1019. _set_texture_rect_mode(false);
  1020. _bind_canvas_texture(state.current_tex, state.current_normal, true);
  1021. glEnable(GL_BLEND);
  1022. }
  1023. void RasterizerCanvasGLES3::canvas_render_items(Item *p_item_list, int p_z, const Color &p_modulate, Light *p_light, const Transform2D &p_transform) {
  1024. Item *current_clip = NULL;
  1025. RasterizerStorageGLES3::Shader *shader_cache = NULL;
  1026. bool rebind_shader = true;
  1027. glBindBuffer(GL_UNIFORM_BUFFER, state.canvas_item_ubo);
  1028. glBufferData(GL_UNIFORM_BUFFER, sizeof(CanvasItemUBO), &state.canvas_item_ubo_data, GL_DYNAMIC_DRAW);
  1029. glBindBuffer(GL_UNIFORM_BUFFER, 0);
  1030. state.current_tex = RID();
  1031. state.current_tex_ptr = NULL;
  1032. state.current_normal = RID();
  1033. glActiveTexture(GL_TEXTURE0);
  1034. glBindTexture(GL_TEXTURE_2D, storage->resources.white_tex);
  1035. int last_blend_mode = -1;
  1036. RID canvas_last_material;
  1037. bool prev_distance_field = false;
  1038. bool prev_use_skeleton = false;
  1039. while (p_item_list) {
  1040. Item *ci = p_item_list;
  1041. if (prev_distance_field != ci->distance_field) {
  1042. state.canvas_shader.set_conditional(CanvasShaderGLES3::USE_DISTANCE_FIELD, ci->distance_field);
  1043. prev_distance_field = ci->distance_field;
  1044. rebind_shader = true;
  1045. }
  1046. if (current_clip != ci->final_clip_owner) {
  1047. current_clip = ci->final_clip_owner;
  1048. //setup clip
  1049. if (current_clip) {
  1050. glEnable(GL_SCISSOR_TEST);
  1051. int y = storage->frame.current_rt->height - (current_clip->final_clip_rect.position.y + current_clip->final_clip_rect.size.y);
  1052. if (storage->frame.current_rt->flags[RasterizerStorage::RENDER_TARGET_VFLIP])
  1053. y = current_clip->final_clip_rect.position.y;
  1054. glScissor(current_clip->final_clip_rect.position.x, y, current_clip->final_clip_rect.size.x, current_clip->final_clip_rect.size.y);
  1055. } else {
  1056. glDisable(GL_SCISSOR_TEST);
  1057. }
  1058. }
  1059. if (ci->copy_back_buffer) {
  1060. if (ci->copy_back_buffer->full) {
  1061. _copy_texscreen(Rect2());
  1062. } else {
  1063. _copy_texscreen(ci->copy_back_buffer->rect);
  1064. }
  1065. }
  1066. RasterizerStorageGLES3::Skeleton *skeleton = NULL;
  1067. {
  1068. //skeleton handling
  1069. if (ci->skeleton.is_valid() && storage->skeleton_owner.owns(ci->skeleton)) {
  1070. skeleton = storage->skeleton_owner.get(ci->skeleton);
  1071. if (!skeleton->use_2d) {
  1072. skeleton = NULL;
  1073. } else {
  1074. state.skeleton_transform = p_transform * skeleton->base_transform_2d;
  1075. state.skeleton_transform_inverse = state.skeleton_transform.affine_inverse();
  1076. }
  1077. }
  1078. bool use_skeleton = skeleton != NULL;
  1079. if (prev_use_skeleton != use_skeleton) {
  1080. rebind_shader = true;
  1081. state.canvas_shader.set_conditional(CanvasShaderGLES3::USE_SKELETON, use_skeleton);
  1082. prev_use_skeleton = use_skeleton;
  1083. }
  1084. if (skeleton) {
  1085. glActiveTexture(GL_TEXTURE0 + storage->config.max_texture_image_units - 4);
  1086. glBindTexture(GL_TEXTURE_2D, skeleton->texture);
  1087. state.using_skeleton = true;
  1088. } else {
  1089. state.using_skeleton = false;
  1090. }
  1091. }
  1092. //begin rect
  1093. Item *material_owner = ci->material_owner ? ci->material_owner : ci;
  1094. RID material = material_owner->material;
  1095. if (material != canvas_last_material || rebind_shader) {
  1096. RasterizerStorageGLES3::Material *material_ptr = storage->material_owner.getornull(material);
  1097. RasterizerStorageGLES3::Shader *shader_ptr = NULL;
  1098. if (material_ptr) {
  1099. shader_ptr = material_ptr->shader;
  1100. if (shader_ptr && shader_ptr->mode != VS::SHADER_CANVAS_ITEM) {
  1101. shader_ptr = NULL; //do not use non canvasitem shader
  1102. }
  1103. }
  1104. if (shader_ptr) {
  1105. if (shader_ptr->canvas_item.uses_screen_texture && !state.canvas_texscreen_used) {
  1106. //copy if not copied before
  1107. _copy_texscreen(Rect2());
  1108. // blend mode will have been enabled so make sure we disable it again later on
  1109. last_blend_mode = last_blend_mode != RasterizerStorageGLES3::Shader::CanvasItem::BLEND_MODE_DISABLED ? last_blend_mode : -1;
  1110. }
  1111. if (shader_ptr != shader_cache || rebind_shader) {
  1112. if (shader_ptr->canvas_item.uses_time) {
  1113. VisualServerRaster::redraw_request();
  1114. }
  1115. state.canvas_shader.set_custom_shader(shader_ptr->custom_code_id);
  1116. state.canvas_shader.bind();
  1117. }
  1118. if (material_ptr->ubo_id) {
  1119. glBindBufferBase(GL_UNIFORM_BUFFER, 2, material_ptr->ubo_id);
  1120. }
  1121. int tc = material_ptr->textures.size();
  1122. RID *textures = material_ptr->textures.ptrw();
  1123. ShaderLanguage::ShaderNode::Uniform::Hint *texture_hints = shader_ptr->texture_hints.ptrw();
  1124. for (int i = 0; i < tc; i++) {
  1125. glActiveTexture(GL_TEXTURE2 + i);
  1126. RasterizerStorageGLES3::Texture *t = storage->texture_owner.getornull(textures[i]);
  1127. if (!t) {
  1128. switch (texture_hints[i]) {
  1129. case ShaderLanguage::ShaderNode::Uniform::HINT_BLACK_ALBEDO:
  1130. case ShaderLanguage::ShaderNode::Uniform::HINT_BLACK: {
  1131. glBindTexture(GL_TEXTURE_2D, storage->resources.black_tex);
  1132. } break;
  1133. case ShaderLanguage::ShaderNode::Uniform::HINT_ANISO: {
  1134. glBindTexture(GL_TEXTURE_2D, storage->resources.aniso_tex);
  1135. } break;
  1136. case ShaderLanguage::ShaderNode::Uniform::HINT_NORMAL: {
  1137. glBindTexture(GL_TEXTURE_2D, storage->resources.normal_tex);
  1138. } break;
  1139. default: {
  1140. glBindTexture(GL_TEXTURE_2D, storage->resources.white_tex);
  1141. } break;
  1142. }
  1143. //check hints
  1144. continue;
  1145. }
  1146. if (t->redraw_if_visible) { //check before proxy, because this is usually used with proxies
  1147. VisualServerRaster::redraw_request();
  1148. }
  1149. t = t->get_ptr();
  1150. if (storage->config.srgb_decode_supported && t->using_srgb) {
  1151. //no srgb in 2D
  1152. glTexParameteri(t->target, _TEXTURE_SRGB_DECODE_EXT, _SKIP_DECODE_EXT);
  1153. t->using_srgb = false;
  1154. }
  1155. glBindTexture(t->target, t->tex_id);
  1156. }
  1157. } else {
  1158. state.canvas_shader.set_custom_shader(0);
  1159. state.canvas_shader.bind();
  1160. }
  1161. shader_cache = shader_ptr;
  1162. canvas_last_material = material;
  1163. rebind_shader = false;
  1164. }
  1165. int blend_mode = shader_cache ? shader_cache->canvas_item.blend_mode : RasterizerStorageGLES3::Shader::CanvasItem::BLEND_MODE_MIX;
  1166. if (blend_mode == RasterizerStorageGLES3::Shader::CanvasItem::BLEND_MODE_DISABLED && (!storage->frame.current_rt || !storage->frame.current_rt->flags[RasterizerStorage::RENDER_TARGET_TRANSPARENT])) {
  1167. blend_mode = RasterizerStorageGLES3::Shader::CanvasItem::BLEND_MODE_MIX;
  1168. }
  1169. bool unshaded = shader_cache && (shader_cache->canvas_item.light_mode == RasterizerStorageGLES3::Shader::CanvasItem::LIGHT_MODE_UNSHADED || (blend_mode != RasterizerStorageGLES3::Shader::CanvasItem::BLEND_MODE_MIX && blend_mode != RasterizerStorageGLES3::Shader::CanvasItem::BLEND_MODE_PMALPHA));
  1170. bool reclip = false;
  1171. if (last_blend_mode != blend_mode) {
  1172. if (last_blend_mode == RasterizerStorageGLES3::Shader::CanvasItem::BLEND_MODE_DISABLED) {
  1173. // re-enable it
  1174. glEnable(GL_BLEND);
  1175. } else if (blend_mode == RasterizerStorageGLES3::Shader::CanvasItem::BLEND_MODE_DISABLED) {
  1176. // disable it
  1177. glDisable(GL_BLEND);
  1178. }
  1179. switch (blend_mode) {
  1180. case RasterizerStorageGLES3::Shader::CanvasItem::BLEND_MODE_DISABLED: {
  1181. // nothing to do here
  1182. } break;
  1183. case RasterizerStorageGLES3::Shader::CanvasItem::BLEND_MODE_MIX: {
  1184. glBlendEquation(GL_FUNC_ADD);
  1185. if (storage->frame.current_rt && storage->frame.current_rt->flags[RasterizerStorage::RENDER_TARGET_TRANSPARENT]) {
  1186. glBlendFuncSeparate(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA, GL_ONE, GL_ONE_MINUS_SRC_ALPHA);
  1187. } else {
  1188. glBlendFuncSeparate(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA, GL_ZERO, GL_ONE);
  1189. }
  1190. } break;
  1191. case RasterizerStorageGLES3::Shader::CanvasItem::BLEND_MODE_ADD: {
  1192. glBlendEquation(GL_FUNC_ADD);
  1193. if (storage->frame.current_rt && storage->frame.current_rt->flags[RasterizerStorage::RENDER_TARGET_TRANSPARENT]) {
  1194. glBlendFuncSeparate(GL_SRC_ALPHA, GL_ONE, GL_SRC_ALPHA, GL_ONE);
  1195. } else {
  1196. glBlendFuncSeparate(GL_SRC_ALPHA, GL_ONE, GL_ZERO, GL_ONE);
  1197. }
  1198. } break;
  1199. case RasterizerStorageGLES3::Shader::CanvasItem::BLEND_MODE_SUB: {
  1200. glBlendEquation(GL_FUNC_REVERSE_SUBTRACT);
  1201. if (storage->frame.current_rt && storage->frame.current_rt->flags[RasterizerStorage::RENDER_TARGET_TRANSPARENT]) {
  1202. glBlendFuncSeparate(GL_SRC_ALPHA, GL_ONE, GL_SRC_ALPHA, GL_ONE);
  1203. } else {
  1204. glBlendFuncSeparate(GL_SRC_ALPHA, GL_ONE, GL_ZERO, GL_ONE);
  1205. }
  1206. } break;
  1207. case RasterizerStorageGLES3::Shader::CanvasItem::BLEND_MODE_MUL: {
  1208. glBlendEquation(GL_FUNC_ADD);
  1209. if (storage->frame.current_rt && storage->frame.current_rt->flags[RasterizerStorage::RENDER_TARGET_TRANSPARENT]) {
  1210. glBlendFuncSeparate(GL_DST_COLOR, GL_ZERO, GL_DST_ALPHA, GL_ZERO);
  1211. } else {
  1212. glBlendFuncSeparate(GL_DST_COLOR, GL_ZERO, GL_ZERO, GL_ONE);
  1213. }
  1214. } break;
  1215. case RasterizerStorageGLES3::Shader::CanvasItem::BLEND_MODE_PMALPHA: {
  1216. glBlendEquation(GL_FUNC_ADD);
  1217. if (storage->frame.current_rt && storage->frame.current_rt->flags[RasterizerStorage::RENDER_TARGET_TRANSPARENT]) {
  1218. glBlendFuncSeparate(GL_ONE, GL_ONE_MINUS_SRC_ALPHA, GL_ONE, GL_ONE_MINUS_SRC_ALPHA);
  1219. } else {
  1220. glBlendFuncSeparate(GL_ONE, GL_ONE_MINUS_SRC_ALPHA, GL_ZERO, GL_ONE);
  1221. }
  1222. } break;
  1223. }
  1224. last_blend_mode = blend_mode;
  1225. }
  1226. state.canvas_item_modulate = unshaded ? ci->final_modulate : Color(ci->final_modulate.r * p_modulate.r, ci->final_modulate.g * p_modulate.g, ci->final_modulate.b * p_modulate.b, ci->final_modulate.a * p_modulate.a);
  1227. state.final_transform = ci->final_transform;
  1228. state.extra_matrix = Transform2D();
  1229. if (state.using_skeleton) {
  1230. state.canvas_shader.set_uniform(CanvasShaderGLES3::SKELETON_TRANSFORM, state.skeleton_transform);
  1231. state.canvas_shader.set_uniform(CanvasShaderGLES3::SKELETON_TRANSFORM_INVERSE, state.skeleton_transform_inverse);
  1232. }
  1233. state.canvas_shader.set_uniform(CanvasShaderGLES3::FINAL_MODULATE, state.canvas_item_modulate);
  1234. state.canvas_shader.set_uniform(CanvasShaderGLES3::MODELVIEW_MATRIX, state.final_transform);
  1235. state.canvas_shader.set_uniform(CanvasShaderGLES3::EXTRA_MATRIX, state.extra_matrix);
  1236. if (storage->frame.current_rt) {
  1237. state.canvas_shader.set_uniform(CanvasShaderGLES3::SCREEN_PIXEL_SIZE, Vector2(1.0 / storage->frame.current_rt->width, 1.0 / storage->frame.current_rt->height));
  1238. } else {
  1239. state.canvas_shader.set_uniform(CanvasShaderGLES3::SCREEN_PIXEL_SIZE, Vector2(1.0, 1.0));
  1240. }
  1241. if (unshaded || (state.canvas_item_modulate.a > 0.001 && (!shader_cache || shader_cache->canvas_item.light_mode != RasterizerStorageGLES3::Shader::CanvasItem::LIGHT_MODE_LIGHT_ONLY) && !ci->light_masked))
  1242. _canvas_item_render_commands(ci, current_clip, reclip);
  1243. if ((blend_mode == RasterizerStorageGLES3::Shader::CanvasItem::BLEND_MODE_MIX || blend_mode == RasterizerStorageGLES3::Shader::CanvasItem::BLEND_MODE_PMALPHA) && p_light && !unshaded) {
  1244. Light *light = p_light;
  1245. bool light_used = false;
  1246. VS::CanvasLightMode mode = VS::CANVAS_LIGHT_MODE_ADD;
  1247. state.canvas_item_modulate = ci->final_modulate; // remove the canvas modulate
  1248. while (light) {
  1249. if (ci->light_mask & light->item_mask && p_z >= light->z_min && p_z <= light->z_max && ci->global_rect_cache.intersects_transformed(light->xform_cache, light->rect_cache)) {
  1250. //intersects this light
  1251. if (!light_used || mode != light->mode) {
  1252. mode = light->mode;
  1253. switch (mode) {
  1254. case VS::CANVAS_LIGHT_MODE_ADD: {
  1255. glBlendEquation(GL_FUNC_ADD);
  1256. glBlendFunc(GL_SRC_ALPHA, GL_ONE);
  1257. } break;
  1258. case VS::CANVAS_LIGHT_MODE_SUB: {
  1259. glBlendEquation(GL_FUNC_REVERSE_SUBTRACT);
  1260. glBlendFunc(GL_SRC_ALPHA, GL_ONE);
  1261. } break;
  1262. case VS::CANVAS_LIGHT_MODE_MIX:
  1263. case VS::CANVAS_LIGHT_MODE_MASK: {
  1264. glBlendEquation(GL_FUNC_ADD);
  1265. glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
  1266. } break;
  1267. }
  1268. }
  1269. if (!light_used) {
  1270. state.canvas_shader.set_conditional(CanvasShaderGLES3::USE_LIGHTING, true);
  1271. light_used = true;
  1272. }
  1273. bool has_shadow = light->shadow_buffer.is_valid() && ci->light_mask & light->item_shadow_mask;
  1274. state.canvas_shader.set_conditional(CanvasShaderGLES3::USE_SHADOWS, has_shadow);
  1275. if (has_shadow) {
  1276. state.canvas_shader.set_conditional(CanvasShaderGLES3::SHADOW_USE_GRADIENT, light->shadow_gradient_length > 0);
  1277. state.canvas_shader.set_conditional(CanvasShaderGLES3::SHADOW_FILTER_NEAREST, light->shadow_filter == VS::CANVAS_LIGHT_FILTER_NONE);
  1278. state.canvas_shader.set_conditional(CanvasShaderGLES3::SHADOW_FILTER_PCF3, light->shadow_filter == VS::CANVAS_LIGHT_FILTER_PCF3);
  1279. state.canvas_shader.set_conditional(CanvasShaderGLES3::SHADOW_FILTER_PCF5, light->shadow_filter == VS::CANVAS_LIGHT_FILTER_PCF5);
  1280. state.canvas_shader.set_conditional(CanvasShaderGLES3::SHADOW_FILTER_PCF7, light->shadow_filter == VS::CANVAS_LIGHT_FILTER_PCF7);
  1281. state.canvas_shader.set_conditional(CanvasShaderGLES3::SHADOW_FILTER_PCF9, light->shadow_filter == VS::CANVAS_LIGHT_FILTER_PCF9);
  1282. state.canvas_shader.set_conditional(CanvasShaderGLES3::SHADOW_FILTER_PCF13, light->shadow_filter == VS::CANVAS_LIGHT_FILTER_PCF13);
  1283. }
  1284. bool light_rebind = state.canvas_shader.bind();
  1285. if (light_rebind) {
  1286. state.canvas_shader.set_uniform(CanvasShaderGLES3::FINAL_MODULATE, state.canvas_item_modulate);
  1287. state.canvas_shader.set_uniform(CanvasShaderGLES3::MODELVIEW_MATRIX, state.final_transform);
  1288. state.canvas_shader.set_uniform(CanvasShaderGLES3::EXTRA_MATRIX, Transform2D());
  1289. if (storage->frame.current_rt) {
  1290. state.canvas_shader.set_uniform(CanvasShaderGLES3::SCREEN_PIXEL_SIZE, Vector2(1.0 / storage->frame.current_rt->width, 1.0 / storage->frame.current_rt->height));
  1291. } else {
  1292. state.canvas_shader.set_uniform(CanvasShaderGLES3::SCREEN_PIXEL_SIZE, Vector2(1.0, 1.0));
  1293. }
  1294. if (state.using_skeleton) {
  1295. state.canvas_shader.set_uniform(CanvasShaderGLES3::SKELETON_TRANSFORM, state.skeleton_transform);
  1296. state.canvas_shader.set_uniform(CanvasShaderGLES3::SKELETON_TRANSFORM_INVERSE, state.skeleton_transform_inverse);
  1297. }
  1298. }
  1299. glBindBufferBase(GL_UNIFORM_BUFFER, 1, static_cast<LightInternal *>(light->light_internal.get_data())->ubo);
  1300. if (has_shadow) {
  1301. RasterizerStorageGLES3::CanvasLightShadow *cls = storage->canvas_light_shadow_owner.get(light->shadow_buffer);
  1302. glActiveTexture(GL_TEXTURE0 + storage->config.max_texture_image_units - 2);
  1303. glBindTexture(GL_TEXTURE_2D, cls->distance);
  1304. /*canvas_shader.set_uniform(CanvasShaderGLES3::SHADOW_MATRIX,light->shadow_matrix_cache);
  1305. canvas_shader.set_uniform(CanvasShaderGLES3::SHADOW_ESM_MULTIPLIER,light->shadow_esm_mult);
  1306. canvas_shader.set_uniform(CanvasShaderGLES3::LIGHT_SHADOW_COLOR,light->shadow_color);*/
  1307. }
  1308. glActiveTexture(GL_TEXTURE0 + storage->config.max_texture_image_units - 1);
  1309. RasterizerStorageGLES3::Texture *t = storage->texture_owner.getornull(light->texture);
  1310. if (!t) {
  1311. glBindTexture(GL_TEXTURE_2D, storage->resources.white_tex);
  1312. } else {
  1313. t = t->get_ptr();
  1314. glBindTexture(t->target, t->tex_id);
  1315. }
  1316. glActiveTexture(GL_TEXTURE0);
  1317. _canvas_item_render_commands(ci, current_clip, reclip); //redraw using light
  1318. }
  1319. light = light->next_ptr;
  1320. }
  1321. if (light_used) {
  1322. state.canvas_shader.set_conditional(CanvasShaderGLES3::USE_LIGHTING, false);
  1323. state.canvas_shader.set_conditional(CanvasShaderGLES3::USE_SHADOWS, false);
  1324. state.canvas_shader.set_conditional(CanvasShaderGLES3::SHADOW_FILTER_NEAREST, false);
  1325. state.canvas_shader.set_conditional(CanvasShaderGLES3::SHADOW_FILTER_PCF3, false);
  1326. state.canvas_shader.set_conditional(CanvasShaderGLES3::SHADOW_FILTER_PCF5, false);
  1327. state.canvas_shader.set_conditional(CanvasShaderGLES3::SHADOW_FILTER_PCF7, false);
  1328. state.canvas_shader.set_conditional(CanvasShaderGLES3::SHADOW_FILTER_PCF9, false);
  1329. state.canvas_shader.set_conditional(CanvasShaderGLES3::SHADOW_FILTER_PCF13, false);
  1330. state.canvas_shader.bind();
  1331. last_blend_mode = -1;
  1332. /*
  1333. //this is set again, so it should not be needed anyway?
  1334. state.canvas_item_modulate = unshaded ? ci->final_modulate : Color(
  1335. ci->final_modulate.r * p_modulate.r,
  1336. ci->final_modulate.g * p_modulate.g,
  1337. ci->final_modulate.b * p_modulate.b,
  1338. ci->final_modulate.a * p_modulate.a );
  1339. state.canvas_shader.set_uniform(CanvasShaderGLES3::MODELVIEW_MATRIX,state.final_transform);
  1340. state.canvas_shader.set_uniform(CanvasShaderGLES3::EXTRA_MATRIX,Transform2D());
  1341. state.canvas_shader.set_uniform(CanvasShaderGLES3::FINAL_MODULATE,state.canvas_item_modulate);
  1342. glBlendEquation(GL_FUNC_ADD);
  1343. if (storage->frame.current_rt->flags[RasterizerStorage::RENDER_TARGET_TRANSPARENT]) {
  1344. glBlendFuncSeparate(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA, GL_ONE, GL_ONE_MINUS_SRC_ALPHA);
  1345. } else {
  1346. glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
  1347. }
  1348. //@TODO RESET canvas_blend_mode
  1349. */
  1350. }
  1351. }
  1352. if (reclip) {
  1353. glEnable(GL_SCISSOR_TEST);
  1354. int y = storage->frame.current_rt->height - (current_clip->final_clip_rect.position.y + current_clip->final_clip_rect.size.y);
  1355. if (storage->frame.current_rt->flags[RasterizerStorage::RENDER_TARGET_VFLIP])
  1356. y = current_clip->final_clip_rect.position.y;
  1357. glScissor(current_clip->final_clip_rect.position.x, y, current_clip->final_clip_rect.size.width, current_clip->final_clip_rect.size.height);
  1358. }
  1359. p_item_list = p_item_list->next;
  1360. }
  1361. if (current_clip) {
  1362. glDisable(GL_SCISSOR_TEST);
  1363. }
  1364. //disable states that may have been used
  1365. state.canvas_shader.set_conditional(CanvasShaderGLES3::USE_DISTANCE_FIELD, false);
  1366. state.canvas_shader.set_conditional(CanvasShaderGLES3::USE_SKELETON, false);
  1367. state.canvas_shader.set_conditional(CanvasShaderGLES3::USE_INSTANCE_CUSTOM, false);
  1368. state.canvas_shader.set_conditional(CanvasShaderGLES3::USE_PARTICLES, false);
  1369. state.canvas_shader.set_conditional(CanvasShaderGLES3::USE_INSTANCING, false);
  1370. state.canvas_shader.set_conditional(CanvasShaderGLES3::USE_LIGHTING, false);
  1371. state.canvas_shader.set_conditional(CanvasShaderGLES3::USE_SHADOWS, false);
  1372. }
  1373. void RasterizerCanvasGLES3::canvas_debug_viewport_shadows(Light *p_lights_with_shadow) {
  1374. Light *light = p_lights_with_shadow;
  1375. canvas_begin(); //reset
  1376. glVertexAttrib4f(VS::ARRAY_COLOR, 1, 1, 1, 1);
  1377. int h = 10;
  1378. int w = storage->frame.current_rt->width;
  1379. int ofs = h;
  1380. glDisable(GL_BLEND);
  1381. while (light) {
  1382. if (light->shadow_buffer.is_valid()) {
  1383. RasterizerStorageGLES3::CanvasLightShadow *sb = storage->canvas_light_shadow_owner.get(light->shadow_buffer);
  1384. if (sb) {
  1385. glBindTexture(GL_TEXTURE_2D, sb->distance);
  1386. draw_generic_textured_rect(Rect2(h, ofs, w - h * 2, h), Rect2(0, 0, 1, 1));
  1387. ofs += h * 2;
  1388. }
  1389. }
  1390. light = light->shadows_next_ptr;
  1391. }
  1392. canvas_end();
  1393. }
  1394. void RasterizerCanvasGLES3::canvas_light_shadow_buffer_update(RID p_buffer, const Transform2D &p_light_xform, int p_light_mask, float p_near, float p_far, LightOccluderInstance *p_occluders, CameraMatrix *p_xform_cache) {
  1395. RasterizerStorageGLES3::CanvasLightShadow *cls = storage->canvas_light_shadow_owner.get(p_buffer);
  1396. ERR_FAIL_COND(!cls);
  1397. glDisable(GL_BLEND);
  1398. glDisable(GL_SCISSOR_TEST);
  1399. glDisable(GL_DITHER);
  1400. glDisable(GL_CULL_FACE);
  1401. glDepthFunc(GL_LEQUAL);
  1402. glEnable(GL_DEPTH_TEST);
  1403. glDepthMask(true);
  1404. glBindFramebuffer(GL_FRAMEBUFFER, cls->fbo);
  1405. state.canvas_shadow_shader.bind();
  1406. glViewport(0, 0, cls->size, cls->height);
  1407. glClearDepth(1.0f);
  1408. glClearColor(1, 1, 1, 1);
  1409. glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
  1410. VS::CanvasOccluderPolygonCullMode cull = VS::CANVAS_OCCLUDER_POLYGON_CULL_DISABLED;
  1411. for (int i = 0; i < 4; i++) {
  1412. //make sure it remains orthogonal, makes easy to read angle later
  1413. Transform light;
  1414. light.origin[0] = p_light_xform[2][0];
  1415. light.origin[1] = p_light_xform[2][1];
  1416. light.basis[0][0] = p_light_xform[0][0];
  1417. light.basis[0][1] = p_light_xform[1][0];
  1418. light.basis[1][0] = p_light_xform[0][1];
  1419. light.basis[1][1] = p_light_xform[1][1];
  1420. //light.basis.scale(Vector3(to_light.elements[0].length(),to_light.elements[1].length(),1));
  1421. //p_near=1;
  1422. CameraMatrix projection;
  1423. {
  1424. real_t fov = 90;
  1425. real_t nearp = p_near;
  1426. real_t farp = p_far;
  1427. real_t aspect = 1.0;
  1428. real_t ymax = nearp * Math::tan(Math::deg2rad(fov * 0.5));
  1429. real_t ymin = -ymax;
  1430. real_t xmin = ymin * aspect;
  1431. real_t xmax = ymax * aspect;
  1432. projection.set_frustum(xmin, xmax, ymin, ymax, nearp, farp);
  1433. }
  1434. Vector3 cam_target = Basis(Vector3(0, 0, Math_PI * 2 * (i / 4.0))).xform(Vector3(0, 1, 0));
  1435. projection = projection * CameraMatrix(Transform().looking_at(cam_target, Vector3(0, 0, -1)).affine_inverse());
  1436. state.canvas_shadow_shader.set_uniform(CanvasShadowShaderGLES3::PROJECTION_MATRIX, projection);
  1437. state.canvas_shadow_shader.set_uniform(CanvasShadowShaderGLES3::LIGHT_MATRIX, light);
  1438. state.canvas_shadow_shader.set_uniform(CanvasShadowShaderGLES3::DISTANCE_NORM, 1.0 / p_far);
  1439. if (i == 0)
  1440. *p_xform_cache = projection;
  1441. glViewport(0, (cls->height / 4) * i, cls->size, cls->height / 4);
  1442. LightOccluderInstance *instance = p_occluders;
  1443. while (instance) {
  1444. RasterizerStorageGLES3::CanvasOccluder *cc = storage->canvas_occluder_owner.get(instance->polygon_buffer);
  1445. if (!cc || cc->len == 0 || !(p_light_mask & instance->light_mask)) {
  1446. instance = instance->next;
  1447. continue;
  1448. }
  1449. state.canvas_shadow_shader.set_uniform(CanvasShadowShaderGLES3::WORLD_MATRIX, instance->xform_cache);
  1450. VS::CanvasOccluderPolygonCullMode transformed_cull_cache = instance->cull_cache;
  1451. if (transformed_cull_cache != VS::CANVAS_OCCLUDER_POLYGON_CULL_DISABLED &&
  1452. (p_light_xform.basis_determinant() * instance->xform_cache.basis_determinant()) < 0) {
  1453. transformed_cull_cache =
  1454. transformed_cull_cache == VS::CANVAS_OCCLUDER_POLYGON_CULL_CLOCKWISE ?
  1455. VS::CANVAS_OCCLUDER_POLYGON_CULL_COUNTER_CLOCKWISE :
  1456. VS::CANVAS_OCCLUDER_POLYGON_CULL_CLOCKWISE;
  1457. }
  1458. if (cull != transformed_cull_cache) {
  1459. cull = transformed_cull_cache;
  1460. switch (cull) {
  1461. case VS::CANVAS_OCCLUDER_POLYGON_CULL_DISABLED: {
  1462. glDisable(GL_CULL_FACE);
  1463. } break;
  1464. case VS::CANVAS_OCCLUDER_POLYGON_CULL_CLOCKWISE: {
  1465. glEnable(GL_CULL_FACE);
  1466. glCullFace(GL_FRONT);
  1467. } break;
  1468. case VS::CANVAS_OCCLUDER_POLYGON_CULL_COUNTER_CLOCKWISE: {
  1469. glEnable(GL_CULL_FACE);
  1470. glCullFace(GL_BACK);
  1471. } break;
  1472. }
  1473. }
  1474. glBindVertexArray(cc->array_id);
  1475. glDrawElements(GL_TRIANGLES, cc->len * 3, GL_UNSIGNED_SHORT, 0);
  1476. instance = instance->next;
  1477. }
  1478. }
  1479. glBindVertexArray(0);
  1480. }
  1481. void RasterizerCanvasGLES3::reset_canvas() {
  1482. if (storage->frame.current_rt) {
  1483. glBindFramebuffer(GL_FRAMEBUFFER, storage->frame.current_rt->fbo);
  1484. glColorMask(1, 1, 1, 1); //don't touch alpha
  1485. }
  1486. glBindVertexArray(0);
  1487. glDisable(GL_CULL_FACE);
  1488. glDisable(GL_DEPTH_TEST);
  1489. glDisable(GL_SCISSOR_TEST);
  1490. glDisable(GL_DITHER);
  1491. glEnable(GL_BLEND);
  1492. glBlendEquation(GL_FUNC_ADD);
  1493. if (storage->frame.current_rt && storage->frame.current_rt->flags[RasterizerStorage::RENDER_TARGET_TRANSPARENT]) {
  1494. glBlendFuncSeparate(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA, GL_ONE, GL_ONE_MINUS_SRC_ALPHA);
  1495. } else {
  1496. glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
  1497. }
  1498. //glPolygonMode(GL_FRONT_AND_BACK,GL_FILL);
  1499. //glLineWidth(1.0);
  1500. glBindBuffer(GL_ARRAY_BUFFER, 0);
  1501. glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0);
  1502. //use for reading from screen
  1503. if (storage->frame.current_rt && !storage->frame.current_rt->flags[RasterizerStorage::RENDER_TARGET_NO_SAMPLING]) {
  1504. glActiveTexture(GL_TEXTURE0 + storage->config.max_texture_image_units - 3);
  1505. glBindTexture(GL_TEXTURE_2D, storage->frame.current_rt->effects.mip_maps[0].color);
  1506. }
  1507. glActiveTexture(GL_TEXTURE0);
  1508. glBindTexture(GL_TEXTURE_2D, storage->resources.white_tex);
  1509. glVertexAttrib4f(VS::ARRAY_COLOR, 1, 1, 1, 1);
  1510. Transform canvas_transform;
  1511. if (storage->frame.current_rt) {
  1512. float csy = 1.0;
  1513. if (storage->frame.current_rt && storage->frame.current_rt->flags[RasterizerStorage::RENDER_TARGET_VFLIP]) {
  1514. csy = -1.0;
  1515. }
  1516. canvas_transform.translate(-(storage->frame.current_rt->width / 2.0f), -(storage->frame.current_rt->height / 2.0f), 0.0f);
  1517. canvas_transform.scale(Vector3(2.0f / storage->frame.current_rt->width, csy * -2.0f / storage->frame.current_rt->height, 1.0f));
  1518. } else {
  1519. Vector2 ssize = OS::get_singleton()->get_window_size();
  1520. canvas_transform.translate(-(ssize.width / 2.0f), -(ssize.height / 2.0f), 0.0f);
  1521. canvas_transform.scale(Vector3(2.0f / ssize.width, -2.0f / ssize.height, 1.0f));
  1522. }
  1523. state.vp = canvas_transform;
  1524. store_transform(canvas_transform, state.canvas_item_ubo_data.projection_matrix);
  1525. state.canvas_item_ubo_data.time = storage->frame.time[0];
  1526. glBindBuffer(GL_UNIFORM_BUFFER, state.canvas_item_ubo);
  1527. glBufferData(GL_UNIFORM_BUFFER, sizeof(CanvasItemUBO), &state.canvas_item_ubo_data, GL_DYNAMIC_DRAW);
  1528. glBindBuffer(GL_UNIFORM_BUFFER, 0);
  1529. state.canvas_texscreen_used = false;
  1530. }
  1531. void RasterizerCanvasGLES3::draw_generic_textured_rect(const Rect2 &p_rect, const Rect2 &p_src) {
  1532. state.canvas_shader.set_uniform(CanvasShaderGLES3::DST_RECT, Color(p_rect.position.x, p_rect.position.y, p_rect.size.x, p_rect.size.y));
  1533. state.canvas_shader.set_uniform(CanvasShaderGLES3::SRC_RECT, Color(p_src.position.x, p_src.position.y, p_src.size.x, p_src.size.y));
  1534. state.canvas_shader.set_uniform(CanvasShaderGLES3::CLIP_RECT_UV, false);
  1535. glDrawArrays(GL_TRIANGLE_FAN, 0, 4);
  1536. }
  1537. void RasterizerCanvasGLES3::draw_lens_distortion_rect(const Rect2 &p_rect, float p_k1, float p_k2, const Vector2 &p_eye_center, float p_oversample) {
  1538. Vector2 half_size;
  1539. if (storage->frame.current_rt) {
  1540. half_size = Vector2(storage->frame.current_rt->width, storage->frame.current_rt->height);
  1541. } else {
  1542. half_size = OS::get_singleton()->get_window_size();
  1543. }
  1544. half_size *= 0.5;
  1545. Vector2 offset((p_rect.position.x - half_size.x) / half_size.x, (p_rect.position.y - half_size.y) / half_size.y);
  1546. Vector2 scale(p_rect.size.x / half_size.x, p_rect.size.y / half_size.y);
  1547. float aspect_ratio = p_rect.size.x / p_rect.size.y;
  1548. // setup our lens shader
  1549. state.lens_shader.bind();
  1550. state.lens_shader.set_uniform(LensDistortedShaderGLES3::OFFSET, offset);
  1551. state.lens_shader.set_uniform(LensDistortedShaderGLES3::SCALE, scale);
  1552. state.lens_shader.set_uniform(LensDistortedShaderGLES3::K1, p_k1);
  1553. state.lens_shader.set_uniform(LensDistortedShaderGLES3::K2, p_k2);
  1554. state.lens_shader.set_uniform(LensDistortedShaderGLES3::EYE_CENTER, p_eye_center);
  1555. state.lens_shader.set_uniform(LensDistortedShaderGLES3::UPSCALE, p_oversample);
  1556. state.lens_shader.set_uniform(LensDistortedShaderGLES3::ASPECT_RATIO, aspect_ratio);
  1557. glBindBufferBase(GL_UNIFORM_BUFFER, 0, state.canvas_item_ubo);
  1558. glBindVertexArray(data.canvas_quad_array);
  1559. // and draw
  1560. glDrawArrays(GL_TRIANGLE_FAN, 0, 4);
  1561. glBindVertexArray(0);
  1562. glBindBufferBase(GL_UNIFORM_BUFFER, 0, 0);
  1563. }
  1564. void RasterizerCanvasGLES3::draw_window_margins(int *black_margin, RID *black_image) {
  1565. Vector2 window_size = OS::get_singleton()->get_window_size();
  1566. int window_h = window_size.height;
  1567. int window_w = window_size.width;
  1568. glBindFramebuffer(GL_FRAMEBUFFER, RasterizerStorageGLES3::system_fbo);
  1569. glViewport(0, 0, window_size.width, window_size.height);
  1570. canvas_begin();
  1571. if (black_image[MARGIN_LEFT].is_valid()) {
  1572. _bind_canvas_texture(black_image[MARGIN_LEFT], RID());
  1573. Size2 sz(storage->texture_get_width(black_image[MARGIN_LEFT]), storage->texture_get_height(black_image[MARGIN_LEFT]));
  1574. draw_generic_textured_rect(Rect2(0, 0, black_margin[MARGIN_LEFT], window_h), Rect2(0, 0, sz.x, sz.y));
  1575. } else if (black_margin[MARGIN_LEFT]) {
  1576. glActiveTexture(GL_TEXTURE0);
  1577. glBindTexture(GL_TEXTURE_2D, storage->resources.black_tex);
  1578. draw_generic_textured_rect(Rect2(0, 0, black_margin[MARGIN_LEFT], window_h), Rect2(0, 0, 1, 1));
  1579. }
  1580. if (black_image[MARGIN_RIGHT].is_valid()) {
  1581. _bind_canvas_texture(black_image[MARGIN_RIGHT], RID());
  1582. Size2 sz(storage->texture_get_width(black_image[MARGIN_RIGHT]), storage->texture_get_height(black_image[MARGIN_RIGHT]));
  1583. draw_generic_textured_rect(Rect2(window_w - black_margin[MARGIN_RIGHT], 0, black_margin[MARGIN_RIGHT], window_h), Rect2(0, 0, sz.x, sz.y));
  1584. } else if (black_margin[MARGIN_RIGHT]) {
  1585. glActiveTexture(GL_TEXTURE0);
  1586. glBindTexture(GL_TEXTURE_2D, storage->resources.black_tex);
  1587. draw_generic_textured_rect(Rect2(window_w - black_margin[MARGIN_RIGHT], 0, black_margin[MARGIN_RIGHT], window_h), Rect2(0, 0, 1, 1));
  1588. }
  1589. if (black_image[MARGIN_TOP].is_valid()) {
  1590. _bind_canvas_texture(black_image[MARGIN_TOP], RID());
  1591. Size2 sz(storage->texture_get_width(black_image[MARGIN_TOP]), storage->texture_get_height(black_image[MARGIN_TOP]));
  1592. draw_generic_textured_rect(Rect2(0, 0, window_w, black_margin[MARGIN_TOP]), Rect2(0, 0, sz.x, sz.y));
  1593. } else if (black_margin[MARGIN_TOP]) {
  1594. glActiveTexture(GL_TEXTURE0);
  1595. glBindTexture(GL_TEXTURE_2D, storage->resources.black_tex);
  1596. draw_generic_textured_rect(Rect2(0, 0, window_w, black_margin[MARGIN_TOP]), Rect2(0, 0, 1, 1));
  1597. }
  1598. if (black_image[MARGIN_BOTTOM].is_valid()) {
  1599. _bind_canvas_texture(black_image[MARGIN_BOTTOM], RID());
  1600. Size2 sz(storage->texture_get_width(black_image[MARGIN_BOTTOM]), storage->texture_get_height(black_image[MARGIN_BOTTOM]));
  1601. draw_generic_textured_rect(Rect2(0, window_h - black_margin[MARGIN_BOTTOM], window_w, black_margin[MARGIN_BOTTOM]), Rect2(0, 0, sz.x, sz.y));
  1602. } else if (black_margin[MARGIN_BOTTOM]) {
  1603. glActiveTexture(GL_TEXTURE0);
  1604. glBindTexture(GL_TEXTURE_2D, storage->resources.black_tex);
  1605. draw_generic_textured_rect(Rect2(0, window_h - black_margin[MARGIN_BOTTOM], window_w, black_margin[MARGIN_BOTTOM]), Rect2(0, 0, 1, 1));
  1606. }
  1607. }
  1608. void RasterizerCanvasGLES3::initialize() {
  1609. {
  1610. //quad buffers
  1611. glGenBuffers(1, &data.canvas_quad_vertices);
  1612. glBindBuffer(GL_ARRAY_BUFFER, data.canvas_quad_vertices);
  1613. {
  1614. const float qv[8] = {
  1615. 0, 0,
  1616. 0, 1,
  1617. 1, 1,
  1618. 1, 0
  1619. };
  1620. glBufferData(GL_ARRAY_BUFFER, sizeof(float) * 8, qv, GL_STATIC_DRAW);
  1621. }
  1622. glBindBuffer(GL_ARRAY_BUFFER, 0); //unbind
  1623. glGenVertexArrays(1, &data.canvas_quad_array);
  1624. glBindVertexArray(data.canvas_quad_array);
  1625. glBindBuffer(GL_ARRAY_BUFFER, data.canvas_quad_vertices);
  1626. glVertexAttribPointer(0, 2, GL_FLOAT, GL_FALSE, sizeof(float) * 2, 0);
  1627. glEnableVertexAttribArray(0);
  1628. glBindVertexArray(0);
  1629. glBindBuffer(GL_ARRAY_BUFFER, 0); //unbind
  1630. }
  1631. {
  1632. //particle quad buffers
  1633. glGenBuffers(1, &data.particle_quad_vertices);
  1634. glBindBuffer(GL_ARRAY_BUFFER, data.particle_quad_vertices);
  1635. {
  1636. //quad of size 1, with pivot on the center for particles, then regular UVS. Color is general plus fetched from particle
  1637. const float qv[16] = {
  1638. -0.5, -0.5,
  1639. 0.0, 0.0,
  1640. -0.5, 0.5,
  1641. 0.0, 1.0,
  1642. 0.5, 0.5,
  1643. 1.0, 1.0,
  1644. 0.5, -0.5,
  1645. 1.0, 0.0
  1646. };
  1647. glBufferData(GL_ARRAY_BUFFER, sizeof(float) * 16, qv, GL_STATIC_DRAW);
  1648. }
  1649. glBindBuffer(GL_ARRAY_BUFFER, 0); //unbind
  1650. glGenVertexArrays(1, &data.particle_quad_array);
  1651. glBindVertexArray(data.particle_quad_array);
  1652. glBindBuffer(GL_ARRAY_BUFFER, data.particle_quad_vertices);
  1653. glEnableVertexAttribArray(VS::ARRAY_VERTEX);
  1654. glVertexAttribPointer(VS::ARRAY_VERTEX, 2, GL_FLOAT, GL_FALSE, sizeof(float) * 4, 0);
  1655. glEnableVertexAttribArray(VS::ARRAY_TEX_UV);
  1656. glVertexAttribPointer(VS::ARRAY_TEX_UV, 2, GL_FLOAT, GL_FALSE, sizeof(float) * 4, CAST_INT_TO_UCHAR_PTR(8));
  1657. glBindVertexArray(0);
  1658. glBindBuffer(GL_ARRAY_BUFFER, 0); //unbind
  1659. }
  1660. {
  1661. uint32_t poly_size = GLOBAL_DEF_RST("rendering/limits/buffers/canvas_polygon_buffer_size_kb", 128);
  1662. ProjectSettings::get_singleton()->set_custom_property_info("rendering/limits/buffers/canvas_polygon_buffer_size_kb", PropertyInfo(Variant::INT, "rendering/limits/buffers/canvas_polygon_buffer_size_kb", PROPERTY_HINT_RANGE, "0,256,1,or_greater"));
  1663. poly_size *= 1024; //kb
  1664. poly_size = MAX(poly_size, (2 + 2 + 4) * 4 * sizeof(float));
  1665. glGenBuffers(1, &data.polygon_buffer);
  1666. glBindBuffer(GL_ARRAY_BUFFER, data.polygon_buffer);
  1667. glBufferData(GL_ARRAY_BUFFER, poly_size, NULL, GL_DYNAMIC_DRAW); //allocate max size
  1668. glBindBuffer(GL_ARRAY_BUFFER, 0);
  1669. data.polygon_buffer_size = poly_size;
  1670. //quad arrays
  1671. for (int i = 0; i < 4; i++) {
  1672. glGenVertexArrays(1, &data.polygon_buffer_quad_arrays[i]);
  1673. glBindVertexArray(data.polygon_buffer_quad_arrays[i]);
  1674. glBindBuffer(GL_ARRAY_BUFFER, data.polygon_buffer);
  1675. int uv_ofs = 0;
  1676. int color_ofs = 0;
  1677. int stride = 2 * 4;
  1678. if (i & 1) { //color
  1679. color_ofs = stride;
  1680. stride += 4 * 4;
  1681. }
  1682. if (i & 2) { //uv
  1683. uv_ofs = stride;
  1684. stride += 2 * 4;
  1685. }
  1686. glEnableVertexAttribArray(VS::ARRAY_VERTEX);
  1687. glVertexAttribPointer(VS::ARRAY_VERTEX, 2, GL_FLOAT, GL_FALSE, stride, NULL);
  1688. if (i & 1) {
  1689. glEnableVertexAttribArray(VS::ARRAY_COLOR);
  1690. glVertexAttribPointer(VS::ARRAY_COLOR, 4, GL_FLOAT, GL_FALSE, stride, CAST_INT_TO_UCHAR_PTR(color_ofs));
  1691. }
  1692. if (i & 2) {
  1693. glEnableVertexAttribArray(VS::ARRAY_TEX_UV);
  1694. glVertexAttribPointer(VS::ARRAY_TEX_UV, 2, GL_FLOAT, GL_FALSE, stride, CAST_INT_TO_UCHAR_PTR(uv_ofs));
  1695. }
  1696. glBindVertexArray(0);
  1697. }
  1698. glGenVertexArrays(1, &data.polygon_buffer_pointer_array);
  1699. uint32_t index_size = GLOBAL_DEF_RST("rendering/limits/buffers/canvas_polygon_index_buffer_size_kb", 128);
  1700. ProjectSettings::get_singleton()->set_custom_property_info("rendering/limits/buffers/canvas_polygon_index_buffer_size_kb", PropertyInfo(Variant::INT, "rendering/limits/buffers/canvas_polygon_index_buffer_size_kb", PROPERTY_HINT_RANGE, "0,256,1,or_greater"));
  1701. index_size *= 1024; //kb
  1702. glGenBuffers(1, &data.polygon_index_buffer);
  1703. glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, data.polygon_index_buffer);
  1704. glBufferData(GL_ELEMENT_ARRAY_BUFFER, index_size, NULL, GL_DYNAMIC_DRAW); //allocate max size
  1705. glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0);
  1706. data.polygon_index_buffer_size = index_size;
  1707. }
  1708. store_transform(Transform(), state.canvas_item_ubo_data.projection_matrix);
  1709. glGenBuffers(1, &state.canvas_item_ubo);
  1710. glBindBuffer(GL_UNIFORM_BUFFER, state.canvas_item_ubo);
  1711. glBufferData(GL_UNIFORM_BUFFER, sizeof(CanvasItemUBO), &state.canvas_item_ubo_data, GL_DYNAMIC_DRAW);
  1712. glBindBuffer(GL_UNIFORM_BUFFER, 0);
  1713. state.canvas_shader.init();
  1714. state.canvas_shader.set_base_material_tex_index(2);
  1715. state.canvas_shadow_shader.init();
  1716. state.lens_shader.init();
  1717. state.canvas_shader.set_conditional(CanvasShaderGLES3::USE_RGBA_SHADOWS, storage->config.use_rgba_2d_shadows);
  1718. state.canvas_shadow_shader.set_conditional(CanvasShadowShaderGLES3::USE_RGBA_SHADOWS, storage->config.use_rgba_2d_shadows);
  1719. state.canvas_shader.set_conditional(CanvasShaderGLES3::USE_PIXEL_SNAP, GLOBAL_DEF("rendering/quality/2d/use_pixel_snap", false));
  1720. }
  1721. void RasterizerCanvasGLES3::finalize() {
  1722. glDeleteBuffers(1, &data.canvas_quad_vertices);
  1723. glDeleteVertexArrays(1, &data.canvas_quad_array);
  1724. glDeleteBuffers(1, &data.canvas_quad_vertices);
  1725. glDeleteVertexArrays(1, &data.canvas_quad_array);
  1726. glDeleteVertexArrays(1, &data.polygon_buffer_pointer_array);
  1727. }
  1728. RasterizerCanvasGLES3::RasterizerCanvasGLES3() {
  1729. }