rasterizer_canvas_gles3.cpp 78 KB

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