rasterizer_canvas_gles2.cpp 40 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156
  1. /*************************************************************************/
  2. /* rasterizer_canvas_gles2.cpp */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2018 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2018 Godot Engine contributors (cf. AUTHORS.md) */
  10. /* */
  11. /* Permission is hereby granted, free of charge, to any person obtaining */
  12. /* a copy of this software and associated documentation files (the */
  13. /* "Software"), to deal in the Software without restriction, including */
  14. /* without limitation the rights to use, copy, modify, merge, publish, */
  15. /* distribute, sublicense, and/or sell copies of the Software, and to */
  16. /* permit persons to whom the Software is furnished to do so, subject to */
  17. /* the following conditions: */
  18. /* */
  19. /* The above copyright notice and this permission notice shall be */
  20. /* included in all copies or substantial portions of the Software. */
  21. /* */
  22. /* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
  23. /* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
  24. /* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.*/
  25. /* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
  26. /* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
  27. /* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
  28. /* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
  29. /*************************************************************************/
  30. #include "rasterizer_canvas_gles2.h"
  31. #include "os/os.h"
  32. #include "project_settings.h"
  33. #include "rasterizer_scene_gles2.h"
  34. #include "servers/visual/visual_server_raster.h"
  35. #ifndef GLES_OVER_GL
  36. #define glClearDepth glClearDepthf
  37. #endif
  38. RID RasterizerCanvasGLES2::light_internal_create() {
  39. return RID();
  40. }
  41. void RasterizerCanvasGLES2::light_internal_update(RID p_rid, Light *p_light) {
  42. }
  43. void RasterizerCanvasGLES2::light_internal_free(RID p_rid) {
  44. }
  45. void RasterizerCanvasGLES2::_set_uniforms() {
  46. state.canvas_shader.set_uniform(CanvasShaderGLES2::PROJECTION_MATRIX, state.uniforms.projection_matrix);
  47. state.canvas_shader.set_uniform(CanvasShaderGLES2::MODELVIEW_MATRIX, state.uniforms.modelview_matrix);
  48. state.canvas_shader.set_uniform(CanvasShaderGLES2::EXTRA_MATRIX, state.uniforms.extra_matrix);
  49. state.canvas_shader.set_uniform(CanvasShaderGLES2::FINAL_MODULATE, state.uniforms.final_modulate);
  50. }
  51. void RasterizerCanvasGLES2::canvas_begin() {
  52. if (storage->frame.clear_request) {
  53. glClearColor(storage->frame.clear_request_color.r,
  54. storage->frame.clear_request_color.g,
  55. storage->frame.clear_request_color.b,
  56. storage->frame.clear_request_color.a);
  57. glClear(GL_COLOR_BUFFER_BIT);
  58. storage->frame.clear_request = false;
  59. }
  60. if (storage->frame.current_rt) {
  61. glBindFramebuffer(GL_FRAMEBUFFER, storage->frame.current_rt->fbo);
  62. glColorMask(1, 1, 1, 1);
  63. }
  64. reset_canvas();
  65. glActiveTexture(GL_TEXTURE0);
  66. glBindTexture(GL_TEXTURE_2D, storage->resources.white_tex);
  67. glVertexAttrib4f(VS::ARRAY_COLOR, 1, 1, 1, 1);
  68. glDisableVertexAttribArray(VS::ARRAY_COLOR);
  69. // set up default uniforms
  70. Transform canvas_transform;
  71. if (storage->frame.current_rt) {
  72. float csy = 1.0;
  73. if (storage->frame.current_rt && storage->frame.current_rt->flags[RasterizerStorage::RENDER_TARGET_VFLIP]) {
  74. csy = -1.0;
  75. }
  76. canvas_transform.translate(-(storage->frame.current_rt->width / 2.0f), -(storage->frame.current_rt->height / 2.0f), 0.0f);
  77. canvas_transform.scale(Vector3(2.0f / storage->frame.current_rt->width, csy * -2.0f / storage->frame.current_rt->height, 1.0f));
  78. } else {
  79. Vector2 ssize = OS::get_singleton()->get_window_size();
  80. canvas_transform.translate(-(ssize.width / 2.0f), -(ssize.height / 2.0f), 0.0f);
  81. canvas_transform.scale(Vector3(2.0f / ssize.width, -2.0f / ssize.height, 1.0f));
  82. }
  83. state.uniforms.projection_matrix = canvas_transform;
  84. state.uniforms.final_modulate = Color(1, 1, 1, 1);
  85. state.uniforms.modelview_matrix = Transform2D();
  86. state.uniforms.extra_matrix = Transform2D();
  87. _set_uniforms();
  88. _bind_quad_buffer();
  89. }
  90. void RasterizerCanvasGLES2::canvas_end() {
  91. glBindBuffer(GL_ARRAY_BUFFER, 0);
  92. for (int i = 0; i < VS::ARRAY_MAX; i++) {
  93. glDisableVertexAttribArray(i);
  94. }
  95. state.using_texture_rect = false;
  96. state.using_ninepatch = false;
  97. }
  98. RasterizerStorageGLES2::Texture *RasterizerCanvasGLES2::_bind_canvas_texture(const RID &p_texture, const RID &p_normal_map) {
  99. RasterizerStorageGLES2::Texture *tex_return = NULL;
  100. if (p_texture.is_valid()) {
  101. RasterizerStorageGLES2::Texture *texture = storage->texture_owner.getornull(p_texture);
  102. if (!texture) {
  103. state.current_tex = RID();
  104. state.current_tex_ptr = NULL;
  105. glActiveTexture(GL_TEXTURE0);
  106. glBindTexture(GL_TEXTURE_2D, storage->resources.white_tex);
  107. } else {
  108. texture = texture->get_ptr();
  109. if (texture->render_target) {
  110. texture->render_target->used_in_frame = true;
  111. }
  112. glActiveTexture(GL_TEXTURE0);
  113. glBindTexture(GL_TEXTURE_2D, texture->tex_id);
  114. state.current_tex = p_texture;
  115. state.current_tex_ptr = texture;
  116. tex_return = texture;
  117. }
  118. } else {
  119. state.current_tex = RID();
  120. state.current_tex_ptr = NULL;
  121. glActiveTexture(GL_TEXTURE0);
  122. glBindTexture(GL_TEXTURE_2D, storage->resources.white_tex);
  123. }
  124. return tex_return;
  125. }
  126. void RasterizerCanvasGLES2::_set_texture_rect_mode(bool p_enable, bool p_ninepatch) {
  127. }
  128. void RasterizerCanvasGLES2::_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) {
  129. glBindBuffer(GL_ARRAY_BUFFER, data.polygon_buffer);
  130. uint32_t buffer_ofs = 0;
  131. glBufferSubData(GL_ARRAY_BUFFER, 0, sizeof(Vector2) * p_vertex_count, p_vertices);
  132. glEnableVertexAttribArray(VS::ARRAY_VERTEX);
  133. glVertexAttribPointer(VS::ARRAY_VERTEX, 2, GL_FLOAT, GL_FALSE, sizeof(Vector2), NULL);
  134. buffer_ofs += sizeof(Vector2) * p_vertex_count;
  135. if (p_singlecolor) {
  136. glDisableVertexAttribArray(VS::ARRAY_COLOR);
  137. Color m = *p_colors;
  138. glVertexAttrib4f(VS::ARRAY_COLOR, m.r, m.g, m.b, m.a);
  139. } else if (!p_colors) {
  140. glDisableVertexAttribArray(VS::ARRAY_COLOR);
  141. glVertexAttrib4f(VS::ARRAY_COLOR, 1, 1, 1, 1);
  142. } else {
  143. glBufferSubData(GL_ARRAY_BUFFER, buffer_ofs, sizeof(Color) * p_vertex_count, p_colors);
  144. glEnableVertexAttribArray(VS::ARRAY_COLOR);
  145. glVertexAttribPointer(VS::ARRAY_COLOR, 4, GL_FLOAT, GL_FALSE, sizeof(Color), ((uint8_t *)0) + buffer_ofs);
  146. buffer_ofs += sizeof(Color) * p_vertex_count;
  147. }
  148. if (p_uvs) {
  149. glBufferSubData(GL_ARRAY_BUFFER, buffer_ofs, sizeof(Vector2) * p_vertex_count, p_uvs);
  150. glEnableVertexAttribArray(VS::ARRAY_TEX_UV);
  151. glVertexAttribPointer(VS::ARRAY_TEX_UV, 2, GL_FLOAT, GL_FALSE, sizeof(Vector2), ((uint8_t *)0) + buffer_ofs);
  152. buffer_ofs += sizeof(Vector2) * p_vertex_count;
  153. } else {
  154. glDisableVertexAttribArray(VS::ARRAY_TEX_UV);
  155. }
  156. glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, data.polygon_index_buffer);
  157. glBufferSubData(GL_ELEMENT_ARRAY_BUFFER, 0, sizeof(int) * p_index_count, p_indices);
  158. glDrawElements(GL_TRIANGLES, p_index_count, GL_UNSIGNED_INT, 0);
  159. glBindBuffer(GL_ARRAY_BUFFER, 0);
  160. glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0);
  161. }
  162. void RasterizerCanvasGLES2::_draw_generic(GLuint p_primitive, int p_vertex_count, const Vector2 *p_vertices, const Vector2 *p_uvs, const Color *p_colors, bool p_singlecolor) {
  163. glBindBuffer(GL_ARRAY_BUFFER, data.polygon_buffer);
  164. uint32_t buffer_ofs = 0;
  165. glBufferSubData(GL_ARRAY_BUFFER, 0, sizeof(Vector2) * p_vertex_count, p_vertices);
  166. glEnableVertexAttribArray(VS::ARRAY_VERTEX);
  167. glVertexAttribPointer(VS::ARRAY_VERTEX, 2, GL_FLOAT, GL_FALSE, sizeof(Vector2), (uint8_t *)0);
  168. buffer_ofs += sizeof(Vector2) * p_vertex_count;
  169. if (p_singlecolor) {
  170. glDisableVertexAttribArray(VS::ARRAY_COLOR);
  171. Color m = *p_colors;
  172. glVertexAttrib4f(VS::ARRAY_COLOR, m.r, m.g, m.b, m.a);
  173. } else if (!p_colors) {
  174. glDisableVertexAttribArray(VS::ARRAY_COLOR);
  175. glVertexAttrib4f(VS::ARRAY_COLOR, 1, 1, 1, 1);
  176. } else {
  177. glBufferSubData(GL_ARRAY_BUFFER, buffer_ofs, sizeof(Color) * p_vertex_count, p_colors);
  178. glEnableVertexAttribArray(VS::ARRAY_COLOR);
  179. glVertexAttribPointer(VS::ARRAY_COLOR, 4, GL_FLOAT, GL_FALSE, sizeof(Color), ((uint8_t *)0) + buffer_ofs);
  180. buffer_ofs += sizeof(Color) * p_vertex_count;
  181. }
  182. if (p_uvs) {
  183. glBufferSubData(GL_ARRAY_BUFFER, buffer_ofs, sizeof(Vector2) * p_vertex_count, p_uvs);
  184. glEnableVertexAttribArray(VS::ARRAY_TEX_UV);
  185. glVertexAttribPointer(VS::ARRAY_TEX_UV, 2, GL_FLOAT, GL_FALSE, sizeof(Vector2), ((uint8_t *)0) + buffer_ofs);
  186. } else {
  187. glDisableVertexAttribArray(VS::ARRAY_TEX_UV);
  188. }
  189. glDrawArrays(p_primitive, 0, p_vertex_count);
  190. glBindBuffer(GL_ARRAY_BUFFER, 0);
  191. }
  192. void RasterizerCanvasGLES2::_draw_gui_primitive(int p_points, const Vector2 *p_vertices, const Color *p_colors, const Vector2 *p_uvs) {
  193. static const GLenum prim[5] = { GL_POINTS, GL_POINTS, GL_LINES, GL_TRIANGLES, GL_TRIANGLE_FAN };
  194. int color_offset = 0;
  195. int uv_offset = 0;
  196. int stride = 2;
  197. if (p_colors) {
  198. color_offset = stride;
  199. stride += 4;
  200. }
  201. if (p_uvs) {
  202. uv_offset = stride;
  203. stride += 2;
  204. }
  205. float buffer_data[(2 + 2 + 4) * 4];
  206. for (int i = 0; i < p_points; i++) {
  207. buffer_data[stride * i + 0] = p_vertices[i].x;
  208. buffer_data[stride * i + 1] = p_vertices[i].y;
  209. }
  210. if (p_colors) {
  211. for (int i = 0; i < p_points; i++) {
  212. buffer_data[stride * i + color_offset + 0] = p_colors[i].r;
  213. buffer_data[stride * i + color_offset + 1] = p_colors[i].g;
  214. buffer_data[stride * i + color_offset + 2] = p_colors[i].b;
  215. buffer_data[stride * i + color_offset + 3] = p_colors[i].a;
  216. }
  217. }
  218. if (p_uvs) {
  219. for (int i = 0; i < p_points; i++) {
  220. buffer_data[stride * i + uv_offset + 0] = p_uvs[i].x;
  221. buffer_data[stride * i + uv_offset + 1] = p_uvs[i].y;
  222. }
  223. }
  224. glBindBuffer(GL_ARRAY_BUFFER, data.polygon_buffer);
  225. glBufferSubData(GL_ARRAY_BUFFER, 0, p_points * stride * 4 * sizeof(float), buffer_data);
  226. glVertexAttribPointer(VS::ARRAY_VERTEX, 2, GL_FLOAT, GL_FALSE, stride * sizeof(float), NULL);
  227. if (p_colors) {
  228. glVertexAttribPointer(VS::ARRAY_COLOR, 4, GL_FLOAT, GL_FALSE, stride * sizeof(float), (uint8_t *)0 + color_offset * sizeof(float));
  229. glEnableVertexAttribArray(VS::ARRAY_COLOR);
  230. }
  231. if (p_uvs) {
  232. glVertexAttribPointer(VS::ARRAY_TEX_UV, 2, GL_FLOAT, GL_FALSE, stride * sizeof(float), (uint8_t *)0 + uv_offset * sizeof(float));
  233. glEnableVertexAttribArray(VS::ARRAY_TEX_UV);
  234. }
  235. glDrawArrays(prim[p_points], 0, p_points);
  236. glBindBuffer(GL_ARRAY_BUFFER, 0);
  237. }
  238. void RasterizerCanvasGLES2::_canvas_item_render_commands(Item *p_item, Item *current_clip, bool &reclip) {
  239. int command_count = p_item->commands.size();
  240. Item::Command **commands = p_item->commands.ptrw();
  241. for (int i = 0; i < command_count; i++) {
  242. Item::Command *command = commands[i];
  243. switch (command->type) {
  244. case Item::Command::TYPE_LINE: {
  245. Item::CommandLine *line = static_cast<Item::CommandLine *>(command);
  246. state.canvas_shader.set_conditional(CanvasShaderGLES2::USE_TEXTURE_RECT, false);
  247. state.canvas_shader.set_conditional(CanvasShaderGLES2::USE_UV_ATTRIBUTE, false);
  248. state.canvas_shader.bind();
  249. _set_uniforms();
  250. _bind_canvas_texture(RID(), RID());
  251. glDisableVertexAttribArray(VS::ARRAY_COLOR);
  252. glVertexAttrib4fv(VS::ARRAY_COLOR, line->color.components);
  253. state.canvas_shader.set_uniform(CanvasShaderGLES2::MODELVIEW_MATRIX, state.uniforms.modelview_matrix);
  254. if (line->width <= 1) {
  255. Vector2 verts[2] = {
  256. Vector2(line->from.x, line->from.y),
  257. Vector2(line->to.x, line->to.y)
  258. };
  259. _draw_gui_primitive(2, verts, NULL, NULL);
  260. } else {
  261. Vector2 t = (line->from - line->to).normalized().tangent() * line->width * 0.5;
  262. Vector2 verts[4] = {
  263. line->from - t,
  264. line->from + t,
  265. line->to + t,
  266. line->to - t
  267. };
  268. _draw_gui_primitive(4, verts, NULL, NULL);
  269. }
  270. } break;
  271. case Item::Command::TYPE_RECT: {
  272. Item::CommandRect *r = static_cast<Item::CommandRect *>(command);
  273. glDisableVertexAttribArray(VS::ARRAY_COLOR);
  274. glVertexAttrib4fv(VS::ARRAY_COLOR, r->modulate.components);
  275. _bind_quad_buffer();
  276. state.canvas_shader.set_conditional(CanvasShaderGLES2::USE_TEXTURE_RECT, true);
  277. state.canvas_shader.set_conditional(CanvasShaderGLES2::USE_UV_ATTRIBUTE, false);
  278. if (state.canvas_shader.bind())
  279. _set_uniforms();
  280. RasterizerStorageGLES2::Texture *tex = _bind_canvas_texture(r->texture, r->normal_map);
  281. if (!tex) {
  282. Rect2 dst_rect = Rect2(r->rect.position, r->rect.size);
  283. if (dst_rect.size.width < 0) {
  284. dst_rect.position.x += dst_rect.size.width;
  285. dst_rect.size.width *= -1;
  286. }
  287. if (dst_rect.size.height < 0) {
  288. dst_rect.position.y += dst_rect.size.height;
  289. dst_rect.size.height *= -1;
  290. }
  291. state.canvas_shader.set_uniform(CanvasShaderGLES2::DST_RECT, Color(dst_rect.position.x, dst_rect.position.y, dst_rect.size.x, dst_rect.size.y));
  292. state.canvas_shader.set_uniform(CanvasShaderGLES2::SRC_RECT, Color(0, 0, 1, 1));
  293. glDrawArrays(GL_TRIANGLE_FAN, 0, 4);
  294. } else {
  295. bool untile = false;
  296. if (r->flags & CANVAS_RECT_TILE && !(tex->flags & VS::TEXTURE_FLAG_REPEAT)) {
  297. glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
  298. glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
  299. untile = true;
  300. }
  301. Size2 texpixel_size(1.0 / tex->width, 1.0 / tex->height);
  302. Rect2 src_rect = (r->flags & CANVAS_RECT_REGION) ? Rect2(r->source.position * texpixel_size, r->source.size * texpixel_size) : Rect2(0, 0, 1, 1);
  303. Rect2 dst_rect = Rect2(r->rect.position, r->rect.size);
  304. state.canvas_shader.set_uniform(CanvasShaderGLES2::COLOR_TEXPIXEL_SIZE, texpixel_size);
  305. if (dst_rect.size.width < 0) {
  306. dst_rect.position.x += dst_rect.size.width;
  307. dst_rect.size.width *= -1;
  308. }
  309. if (dst_rect.size.height < 0) {
  310. dst_rect.position.y += dst_rect.size.height;
  311. dst_rect.size.height *= -1;
  312. }
  313. if (r->flags & CANVAS_RECT_FLIP_H) {
  314. src_rect.size.x *= -1;
  315. }
  316. if (r->flags & CANVAS_RECT_FLIP_V) {
  317. src_rect.size.y *= -1;
  318. }
  319. if (r->flags & CANVAS_RECT_TRANSPOSE) {
  320. dst_rect.size.x *= -1; // Encoding in the dst_rect.z uniform
  321. }
  322. state.canvas_shader.set_uniform(CanvasShaderGLES2::COLOR_TEXPIXEL_SIZE, texpixel_size);
  323. state.canvas_shader.set_uniform(CanvasShaderGLES2::DST_RECT, Color(dst_rect.position.x, dst_rect.position.y, dst_rect.size.x, dst_rect.size.y));
  324. state.canvas_shader.set_uniform(CanvasShaderGLES2::SRC_RECT, Color(src_rect.position.x, src_rect.position.y, src_rect.size.x, src_rect.size.y));
  325. glDrawArrays(GL_TRIANGLE_FAN, 0, 4);
  326. if (untile) {
  327. glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
  328. glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
  329. }
  330. }
  331. glBindBuffer(GL_ARRAY_BUFFER, 0);
  332. glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0);
  333. } break;
  334. case Item::Command::TYPE_NINEPATCH: {
  335. Item::CommandNinePatch *np = static_cast<Item::CommandNinePatch *>(command);
  336. state.canvas_shader.set_conditional(CanvasShaderGLES2::USE_TEXTURE_RECT, false);
  337. state.canvas_shader.set_conditional(CanvasShaderGLES2::USE_UV_ATTRIBUTE, true);
  338. if (state.canvas_shader.bind())
  339. _set_uniforms();
  340. glDisableVertexAttribArray(VS::ARRAY_COLOR);
  341. glVertexAttrib4fv(VS::ARRAY_COLOR, np->color.components);
  342. RasterizerStorageGLES2::Texture *tex = _bind_canvas_texture(np->texture, np->normal_map);
  343. if (!tex) {
  344. print_line("TODO: ninepatch without texture");
  345. continue;
  346. }
  347. Size2 texpixel_size(1.0 / tex->width, 1.0 / tex->height);
  348. // state.canvas_shader.set_uniform(CanvasShaderGLES2::MODELVIEW_MATRIX, state.uniforms.modelview_matrix);
  349. state.canvas_shader.set_uniform(CanvasShaderGLES2::COLOR_TEXPIXEL_SIZE, texpixel_size);
  350. // prepare vertex buffer
  351. float buffer[16 * 2 + 16 * 2];
  352. {
  353. // first row
  354. buffer[(0 * 4 * 4) + 0] = np->rect.position.x;
  355. buffer[(0 * 4 * 4) + 1] = np->rect.position.y;
  356. buffer[(0 * 4 * 4) + 2] = np->source.position.x * texpixel_size.x;
  357. buffer[(0 * 4 * 4) + 3] = np->source.position.y * texpixel_size.y;
  358. buffer[(0 * 4 * 4) + 4] = np->rect.position.x + np->margin[MARGIN_LEFT];
  359. buffer[(0 * 4 * 4) + 5] = np->rect.position.y;
  360. buffer[(0 * 4 * 4) + 6] = (np->source.position.x + np->margin[MARGIN_LEFT]) * texpixel_size.x;
  361. buffer[(0 * 4 * 4) + 7] = np->source.position.y * texpixel_size.y;
  362. buffer[(0 * 4 * 4) + 8] = np->rect.position.x + np->rect.size.x - np->margin[MARGIN_RIGHT];
  363. buffer[(0 * 4 * 4) + 9] = np->rect.position.y;
  364. buffer[(0 * 4 * 4) + 10] = (np->source.position.x + np->source.size.x - np->margin[MARGIN_RIGHT]) * texpixel_size.x;
  365. buffer[(0 * 4 * 4) + 11] = np->source.position.y * texpixel_size.y;
  366. buffer[(0 * 4 * 4) + 12] = np->rect.position.x + np->rect.size.x;
  367. buffer[(0 * 4 * 4) + 13] = np->rect.position.y;
  368. buffer[(0 * 4 * 4) + 14] = (np->source.position.x + np->source.size.x) * texpixel_size.x;
  369. buffer[(0 * 4 * 4) + 15] = np->source.position.y * texpixel_size.y;
  370. // second row
  371. buffer[(1 * 4 * 4) + 0] = np->rect.position.x;
  372. buffer[(1 * 4 * 4) + 1] = np->rect.position.y + np->margin[MARGIN_TOP];
  373. buffer[(1 * 4 * 4) + 2] = np->source.position.x * texpixel_size.x;
  374. buffer[(1 * 4 * 4) + 3] = (np->source.position.y + np->margin[MARGIN_TOP]) * texpixel_size.y;
  375. buffer[(1 * 4 * 4) + 4] = np->rect.position.x + np->margin[MARGIN_LEFT];
  376. buffer[(1 * 4 * 4) + 5] = np->rect.position.y + np->margin[MARGIN_TOP];
  377. buffer[(1 * 4 * 4) + 6] = (np->source.position.x + np->margin[MARGIN_LEFT]) * texpixel_size.x;
  378. buffer[(1 * 4 * 4) + 7] = (np->source.position.y + np->margin[MARGIN_TOP]) * texpixel_size.y;
  379. buffer[(1 * 4 * 4) + 8] = np->rect.position.x + np->rect.size.x - np->margin[MARGIN_RIGHT];
  380. buffer[(1 * 4 * 4) + 9] = np->rect.position.y + np->margin[MARGIN_TOP];
  381. buffer[(1 * 4 * 4) + 10] = (np->source.position.x + np->source.size.x - np->margin[MARGIN_RIGHT]) * texpixel_size.x;
  382. buffer[(1 * 4 * 4) + 11] = (np->source.position.y + np->margin[MARGIN_TOP]) * texpixel_size.y;
  383. buffer[(1 * 4 * 4) + 12] = np->rect.position.x + np->rect.size.x;
  384. buffer[(1 * 4 * 4) + 13] = np->rect.position.y + np->margin[MARGIN_TOP];
  385. buffer[(1 * 4 * 4) + 14] = (np->source.position.x + np->source.size.x) * texpixel_size.x;
  386. buffer[(1 * 4 * 4) + 15] = (np->source.position.y + np->margin[MARGIN_TOP]) * texpixel_size.y;
  387. // thrid row
  388. buffer[(2 * 4 * 4) + 0] = np->rect.position.x;
  389. buffer[(2 * 4 * 4) + 1] = np->rect.position.y + np->rect.size.y - np->margin[MARGIN_BOTTOM];
  390. buffer[(2 * 4 * 4) + 2] = np->source.position.x * texpixel_size.x;
  391. buffer[(2 * 4 * 4) + 3] = (np->source.position.y + np->source.size.y - np->margin[MARGIN_BOTTOM]) * texpixel_size.y;
  392. buffer[(2 * 4 * 4) + 4] = np->rect.position.x + np->margin[MARGIN_LEFT];
  393. buffer[(2 * 4 * 4) + 5] = np->rect.position.y + np->rect.size.y - np->margin[MARGIN_BOTTOM];
  394. buffer[(2 * 4 * 4) + 6] = (np->source.position.x + np->margin[MARGIN_LEFT]) * texpixel_size.x;
  395. buffer[(2 * 4 * 4) + 7] = (np->source.position.y + np->source.size.y - np->margin[MARGIN_BOTTOM]) * texpixel_size.y;
  396. buffer[(2 * 4 * 4) + 8] = np->rect.position.x + np->rect.size.x - np->margin[MARGIN_RIGHT];
  397. buffer[(2 * 4 * 4) + 9] = np->rect.position.y + np->rect.size.y - np->margin[MARGIN_BOTTOM];
  398. buffer[(2 * 4 * 4) + 10] = (np->source.position.x + np->source.size.x - np->margin[MARGIN_RIGHT]) * texpixel_size.x;
  399. buffer[(2 * 4 * 4) + 11] = (np->source.position.y + np->source.size.y - np->margin[MARGIN_BOTTOM]) * texpixel_size.y;
  400. buffer[(2 * 4 * 4) + 12] = np->rect.position.x + np->rect.size.x;
  401. buffer[(2 * 4 * 4) + 13] = np->rect.position.y + np->rect.size.y - np->margin[MARGIN_BOTTOM];
  402. buffer[(2 * 4 * 4) + 14] = (np->source.position.x + np->source.size.x) * texpixel_size.x;
  403. buffer[(2 * 4 * 4) + 15] = (np->source.position.y + np->source.size.y - np->margin[MARGIN_BOTTOM]) * texpixel_size.y;
  404. // fourth row
  405. buffer[(3 * 4 * 4) + 0] = np->rect.position.x;
  406. buffer[(3 * 4 * 4) + 1] = np->rect.position.y + np->rect.size.y;
  407. buffer[(3 * 4 * 4) + 2] = np->source.position.x * texpixel_size.x;
  408. buffer[(3 * 4 * 4) + 3] = (np->source.position.y + np->source.size.y) * texpixel_size.y;
  409. buffer[(3 * 4 * 4) + 4] = np->rect.position.x + np->margin[MARGIN_LEFT];
  410. buffer[(3 * 4 * 4) + 5] = np->rect.position.y + np->rect.size.y;
  411. buffer[(3 * 4 * 4) + 6] = (np->source.position.x + np->margin[MARGIN_LEFT]) * texpixel_size.x;
  412. buffer[(3 * 4 * 4) + 7] = (np->source.position.y + np->source.size.y) * texpixel_size.y;
  413. buffer[(3 * 4 * 4) + 8] = np->rect.position.x + np->rect.size.x - np->margin[MARGIN_RIGHT];
  414. buffer[(3 * 4 * 4) + 9] = np->rect.position.y + np->rect.size.y;
  415. buffer[(3 * 4 * 4) + 10] = (np->source.position.x + np->source.size.x - np->margin[MARGIN_RIGHT]) * texpixel_size.x;
  416. buffer[(3 * 4 * 4) + 11] = (np->source.position.y + np->source.size.y) * texpixel_size.y;
  417. buffer[(3 * 4 * 4) + 12] = np->rect.position.x + np->rect.size.x;
  418. buffer[(3 * 4 * 4) + 13] = np->rect.position.y + np->rect.size.y;
  419. buffer[(3 * 4 * 4) + 14] = (np->source.position.x + np->source.size.x) * texpixel_size.x;
  420. buffer[(3 * 4 * 4) + 15] = (np->source.position.y + np->source.size.y) * texpixel_size.y;
  421. // print_line(String::num((np->source.position.y + np->source.size.y) * texpixel_size.y));
  422. }
  423. glBindBuffer(GL_ARRAY_BUFFER, data.ninepatch_vertices);
  424. glBufferSubData(GL_ARRAY_BUFFER, 0, sizeof(float) * (16 + 16) * 2, buffer);
  425. glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, data.ninepatch_elements);
  426. glEnableVertexAttribArray(VS::ARRAY_VERTEX);
  427. glEnableVertexAttribArray(VS::ARRAY_TEX_UV);
  428. glVertexAttribPointer(VS::ARRAY_VERTEX, 2, GL_FLOAT, GL_FALSE, 4 * sizeof(float), NULL);
  429. glVertexAttribPointer(VS::ARRAY_TEX_UV, 2, GL_FLOAT, GL_FALSE, 4 * sizeof(float), (uint8_t *)0 + (sizeof(float) * 2));
  430. glDrawElements(GL_TRIANGLES, 18 * 3 - (np->draw_center ? 0 : 6), GL_UNSIGNED_BYTE, NULL);
  431. glBindBuffer(GL_ARRAY_BUFFER, 0);
  432. glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0);
  433. } break;
  434. case Item::Command::TYPE_CIRCLE: {
  435. Item::CommandCircle *circle = static_cast<Item::CommandCircle *>(command);
  436. state.canvas_shader.set_conditional(CanvasShaderGLES2::USE_TEXTURE_RECT, false);
  437. state.canvas_shader.set_conditional(CanvasShaderGLES2::USE_UV_ATTRIBUTE, false);
  438. if (state.canvas_shader.bind())
  439. _set_uniforms();
  440. static const int num_points = 32;
  441. Vector2 points[num_points + 1];
  442. points[num_points] = circle->pos;
  443. int indices[num_points * 3];
  444. for (int i = 0; i < num_points; i++) {
  445. points[i] = circle->pos + Vector2(Math::sin(i * Math_PI * 2.0 / num_points), Math::cos(i * Math_PI * 2.0 / num_points)) * circle->radius;
  446. indices[i * 3 + 0] = i;
  447. indices[i * 3 + 1] = (i + 1) % num_points;
  448. indices[i * 3 + 2] = num_points;
  449. }
  450. _bind_canvas_texture(RID(), RID());
  451. _draw_polygon(indices, num_points * 3, num_points + 1, points, NULL, &circle->color, true);
  452. } break;
  453. case Item::Command::TYPE_POLYGON: {
  454. Item::CommandPolygon *polygon = static_cast<Item::CommandPolygon *>(command);
  455. state.canvas_shader.set_conditional(CanvasShaderGLES2::USE_TEXTURE_RECT, false);
  456. state.canvas_shader.set_conditional(CanvasShaderGLES2::USE_UV_ATTRIBUTE, true);
  457. if (state.canvas_shader.bind())
  458. _set_uniforms();
  459. RasterizerStorageGLES2::Texture *texture = _bind_canvas_texture(polygon->texture, polygon->normal_map);
  460. if (texture) {
  461. Size2 texpixel_size(1.0 / texture->width, 1.0 / texture->height);
  462. state.canvas_shader.set_uniform(CanvasShaderGLES2::COLOR_TEXPIXEL_SIZE, texpixel_size);
  463. }
  464. _draw_polygon(polygon->indices.ptr(), polygon->count, polygon->points.size(), polygon->points.ptr(), polygon->uvs.ptr(), polygon->colors.ptr(), polygon->colors.size() == 1);
  465. } break;
  466. case Item::Command::TYPE_POLYLINE: {
  467. Item::CommandPolyLine *pline = static_cast<Item::CommandPolyLine *>(command);
  468. state.canvas_shader.set_conditional(CanvasShaderGLES2::USE_TEXTURE_RECT, false);
  469. state.canvas_shader.set_conditional(CanvasShaderGLES2::USE_UV_ATTRIBUTE, false);
  470. if (state.canvas_shader.bind())
  471. _set_uniforms();
  472. if (pline->triangles.size()) {
  473. _draw_generic(GL_TRIANGLE_STRIP, pline->triangles.size(), pline->triangles.ptr(), NULL, pline->triangle_colors.ptr(), pline->triangle_colors.size() == 1);
  474. } else {
  475. if (pline->multiline) {
  476. int todo = pline->lines.size() / 2;
  477. int max_per_call = data.polygon_buffer_size / (sizeof(real_t) * 4);
  478. int offset = 0;
  479. while (todo) {
  480. int to_draw = MIN(max_per_call, todo);
  481. _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);
  482. todo -= to_draw;
  483. offset += to_draw * 2;
  484. }
  485. } else {
  486. _draw_generic(GL_LINES, pline->lines.size(), pline->lines.ptr(), NULL, pline->line_colors.ptr(), pline->line_colors.size() == 1);
  487. }
  488. }
  489. } break;
  490. case Item::Command::TYPE_PRIMITIVE: {
  491. Item::CommandPrimitive *primitive = static_cast<Item::CommandPrimitive *>(command);
  492. state.canvas_shader.set_conditional(CanvasShaderGLES2::USE_TEXTURE_RECT, false);
  493. state.canvas_shader.set_conditional(CanvasShaderGLES2::USE_UV_ATTRIBUTE, true);
  494. if (state.canvas_shader.bind())
  495. _set_uniforms();
  496. ERR_CONTINUE(primitive->points.size() < 1);
  497. RasterizerStorageGLES2::Texture *texture = _bind_canvas_texture(primitive->texture, primitive->normal_map);
  498. if (texture) {
  499. Size2 texpixel_size(1.0 / texture->width, 1.0 / texture->height);
  500. state.canvas_shader.set_uniform(CanvasShaderGLES2::COLOR_TEXPIXEL_SIZE, texpixel_size);
  501. }
  502. if (primitive->colors.size() == 1 && primitive->points.size() > 1) {
  503. Color c = primitive->colors[0];
  504. glVertexAttrib4f(VS::ARRAY_COLOR, c.r, c.g, c.b, c.a);
  505. } else if (primitive->colors.empty()) {
  506. glVertexAttrib4f(VS::ARRAY_COLOR, 1, 1, 1, 1);
  507. }
  508. _draw_gui_primitive(primitive->points.size(), primitive->points.ptr(), primitive->colors.ptr(), primitive->uvs.ptr());
  509. } break;
  510. case Item::Command::TYPE_TRANSFORM: {
  511. Item::CommandTransform *transform = static_cast<Item::CommandTransform *>(command);
  512. state.uniforms.extra_matrix = transform->xform;
  513. state.canvas_shader.set_uniform(CanvasShaderGLES2::EXTRA_MATRIX, state.uniforms.extra_matrix);
  514. } break;
  515. case Item::Command::TYPE_PARTICLES: {
  516. } break;
  517. case Item::Command::TYPE_CLIP_IGNORE: {
  518. Item::CommandClipIgnore *ci = static_cast<Item::CommandClipIgnore *>(command);
  519. if (current_clip) {
  520. if (ci->ignore != reclip) {
  521. if (ci->ignore) {
  522. glDisable(GL_SCISSOR_TEST);
  523. reclip = true;
  524. } else {
  525. glEnable(GL_SCISSOR_TEST);
  526. int x = current_clip->final_clip_rect.position.x;
  527. int y = storage->frame.current_rt->height - (current_clip->final_clip_rect.position.y + current_clip->final_clip_rect.size.y);
  528. int w = current_clip->final_clip_rect.size.x;
  529. int h = current_clip->final_clip_rect.size.y;
  530. if (storage->frame.current_rt->flags[RasterizerStorage::RENDER_TARGET_VFLIP])
  531. y = current_clip->final_clip_rect.position.y;
  532. glScissor(x, y, w, h);
  533. reclip = false;
  534. }
  535. }
  536. }
  537. } break;
  538. default: {
  539. print_line("other");
  540. } break;
  541. }
  542. }
  543. }
  544. void RasterizerCanvasGLES2::_copy_texscreen(const Rect2 &p_rect) {
  545. // This isn't really working yet, so disabling for now.
  546. /*
  547. glDisable(GL_BLEND);
  548. state.canvas_texscreen_used = true;
  549. Vector2 wh(storage->frame.current_rt->width, storage->frame.current_rt->height);
  550. Color copy_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);
  551. if (p_rect != Rect2()) {
  552. // only use section
  553. storage->shaders.copy.set_conditional(CopyShaderGLES2::USE_COPY_SECTION, true);
  554. }
  555. storage->shaders.copy.bind();
  556. storage->shaders.copy.set_uniform(CopyShaderGLES2::COPY_SECTION, copy_section);
  557. _bind_quad_buffer();
  558. glBindFramebuffer(GL_FRAMEBUFFER, storage->frame.current_rt->copy_screen_effect.fbo);
  559. glActiveTexture(GL_TEXTURE0);
  560. glBindTexture(GL_TEXTURE_2D, storage->frame.current_rt->color);
  561. glDrawArrays(GL_TRIANGLE_FAN, 0, 4);
  562. glDisableVertexAttribArray(VS::ARRAY_VERTEX);
  563. glBindBuffer(GL_ARRAY_BUFFER, 0);
  564. glBindFramebuffer(GL_FRAMEBUFFER, storage->frame.current_rt->fbo);
  565. state.canvas_shader.bind();
  566. _bind_canvas_texture(state.current_tex, state.current_normal);
  567. glEnable(GL_BLEND);
  568. */
  569. }
  570. void RasterizerCanvasGLES2::canvas_render_items(Item *p_item_list, int p_z, const Color &p_modulate, Light *p_light, const Transform2D &p_base_transform) {
  571. Item *current_clip = NULL;
  572. RasterizerStorageGLES2::Shader *shader_cache = NULL;
  573. bool rebind_shader = true;
  574. Size2 rt_size = Size2(storage->frame.current_rt->width, storage->frame.current_rt->height);
  575. state.current_tex = RID();
  576. state.current_tex_ptr = NULL;
  577. state.current_normal = RID();
  578. glActiveTexture(GL_TEXTURE0);
  579. glBindTexture(GL_TEXTURE_2D, storage->resources.white_tex);
  580. int last_blend_mode = -1;
  581. RID canvas_last_material = RID();
  582. while (p_item_list) {
  583. Item *ci = p_item_list;
  584. if (current_clip != ci->final_clip_owner) {
  585. current_clip = ci->final_clip_owner;
  586. if (current_clip) {
  587. glEnable(GL_SCISSOR_TEST);
  588. int y = storage->frame.current_rt->height - (current_clip->final_clip_rect.position.y + current_clip->final_clip_rect.size.y);
  589. if (storage->frame.current_rt->flags[RasterizerStorage::RENDER_TARGET_VFLIP])
  590. y = current_clip->final_clip_rect.position.y;
  591. glScissor(current_clip->final_clip_rect.position.x, y, current_clip->final_clip_rect.size.width, current_clip->final_clip_rect.size.height);
  592. } else {
  593. glDisable(GL_SCISSOR_TEST);
  594. }
  595. }
  596. // TODO: copy back buffer
  597. if (ci->copy_back_buffer) {
  598. if (ci->copy_back_buffer->full) {
  599. _copy_texscreen(Rect2());
  600. } else {
  601. _copy_texscreen(ci->copy_back_buffer->rect);
  602. }
  603. }
  604. Item *material_owner = ci->material_owner ? ci->material_owner : ci;
  605. RID material = material_owner->material;
  606. if (material != canvas_last_material || rebind_shader) {
  607. RasterizerStorageGLES2::Material *material_ptr = storage->material_owner.getornull(material);
  608. RasterizerStorageGLES2::Shader *shader_ptr = NULL;
  609. if (material_ptr) {
  610. shader_ptr = material_ptr->shader;
  611. if (shader_ptr && shader_ptr->mode != VS::SHADER_CANVAS_ITEM) {
  612. shader_ptr = NULL; // not a canvas item shader, don't use.
  613. }
  614. }
  615. if (shader_ptr) {
  616. if (shader_ptr->canvas_item.uses_screen_texture) {
  617. _copy_texscreen(Rect2());
  618. }
  619. if (shader_ptr != shader_cache) {
  620. if (shader_ptr->canvas_item.uses_time) {
  621. VisualServerRaster::redraw_request();
  622. }
  623. state.canvas_shader.set_custom_shader(shader_ptr->custom_code_id);
  624. state.canvas_shader.bind();
  625. }
  626. int tc = material_ptr->textures.size();
  627. RID *textures = material_ptr->textures.ptrw();
  628. ShaderLanguage::ShaderNode::Uniform::Hint *texture_hints = shader_ptr->texture_hints.ptrw();
  629. for (int i = 0; i < tc; i++) {
  630. glActiveTexture(GL_TEXTURE2 + i);
  631. RasterizerStorageGLES2::Texture *t = storage->texture_owner.getornull(textures[i]);
  632. if (!t) {
  633. switch (texture_hints[i]) {
  634. case ShaderLanguage::ShaderNode::Uniform::HINT_BLACK_ALBEDO:
  635. case ShaderLanguage::ShaderNode::Uniform::HINT_BLACK: {
  636. glBindTexture(GL_TEXTURE_2D, storage->resources.black_tex);
  637. } break;
  638. case ShaderLanguage::ShaderNode::Uniform::HINT_ANISO: {
  639. glBindTexture(GL_TEXTURE_2D, storage->resources.aniso_tex);
  640. } break;
  641. case ShaderLanguage::ShaderNode::Uniform::HINT_NORMAL: {
  642. glBindTexture(GL_TEXTURE_2D, storage->resources.normal_tex);
  643. } break;
  644. default: {
  645. glBindTexture(GL_TEXTURE_2D, storage->resources.white_tex);
  646. } break;
  647. }
  648. continue;
  649. }
  650. t = t->get_ptr();
  651. glBindTexture(t->target, t->tex_id);
  652. }
  653. } else {
  654. state.canvas_shader.set_custom_shader(0);
  655. state.canvas_shader.bind();
  656. }
  657. shader_cache = shader_ptr;
  658. canvas_last_material = material;
  659. rebind_shader = false;
  660. }
  661. int blend_mode = shader_cache ? shader_cache->canvas_item.blend_mode : RasterizerStorageGLES2::Shader::CanvasItem::BLEND_MODE_MIX;
  662. bool unshaded = true || (shader_cache && blend_mode != RasterizerStorageGLES2::Shader::CanvasItem::BLEND_MODE_MIX);
  663. bool reclip = false;
  664. if (last_blend_mode != blend_mode) {
  665. switch (blend_mode) {
  666. case RasterizerStorageGLES2::Shader::CanvasItem::BLEND_MODE_MIX: {
  667. glBlendEquation(GL_FUNC_ADD);
  668. if (storage->frame.current_rt && storage->frame.current_rt->flags[RasterizerStorage::RENDER_TARGET_TRANSPARENT]) {
  669. glBlendFuncSeparate(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA, GL_ONE, GL_ONE_MINUS_SRC_ALPHA);
  670. } else {
  671. glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
  672. }
  673. } break;
  674. case RasterizerStorageGLES2::Shader::CanvasItem::BLEND_MODE_ADD: {
  675. glBlendEquation(GL_FUNC_ADD);
  676. glBlendFunc(GL_SRC_ALPHA, GL_ONE);
  677. } break;
  678. case RasterizerStorageGLES2::Shader::CanvasItem::BLEND_MODE_SUB: {
  679. glBlendEquation(GL_FUNC_REVERSE_SUBTRACT);
  680. glBlendFunc(GL_SRC_ALPHA, GL_ONE);
  681. } break;
  682. case RasterizerStorageGLES2::Shader::CanvasItem::BLEND_MODE_MUL: {
  683. glBlendEquation(GL_FUNC_ADD);
  684. glBlendFunc(GL_DST_COLOR, GL_ZERO);
  685. } break;
  686. case RasterizerStorageGLES2::Shader::CanvasItem::BLEND_MODE_PMALPHA: {
  687. glBlendEquation(GL_FUNC_ADD);
  688. glBlendFunc(GL_ONE, GL_ONE_MINUS_SRC_ALPHA);
  689. } break;
  690. }
  691. }
  692. state.uniforms.final_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);
  693. state.uniforms.modelview_matrix = ci->final_transform;
  694. state.uniforms.extra_matrix = Transform2D();
  695. _set_uniforms();
  696. _canvas_item_render_commands(p_item_list, NULL, reclip);
  697. rebind_shader = true; // hacked in for now.
  698. if (reclip) {
  699. glEnable(GL_SCISSOR_TEST);
  700. int y = storage->frame.current_rt->height - (current_clip->final_clip_rect.position.y + current_clip->final_clip_rect.size.y);
  701. if (storage->frame.current_rt->flags[RasterizerStorage::RENDER_TARGET_VFLIP])
  702. y = current_clip->final_clip_rect.position.y;
  703. glScissor(current_clip->final_clip_rect.position.x, y, current_clip->final_clip_rect.size.width, current_clip->final_clip_rect.size.height);
  704. }
  705. p_item_list = p_item_list->next;
  706. }
  707. if (current_clip) {
  708. glDisable(GL_SCISSOR_TEST);
  709. }
  710. }
  711. void RasterizerCanvasGLES2::canvas_debug_viewport_shadows(Light *p_lights_with_shadow) {
  712. }
  713. void RasterizerCanvasGLES2::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) {
  714. }
  715. void RasterizerCanvasGLES2::reset_canvas() {
  716. glDisable(GL_CULL_FACE);
  717. glDisable(GL_DEPTH_TEST);
  718. glDisable(GL_SCISSOR_TEST);
  719. glDisable(GL_DITHER);
  720. glEnable(GL_BLEND);
  721. if (storage->frame.current_rt && storage->frame.current_rt->flags[RasterizerStorage::RENDER_TARGET_TRANSPARENT]) {
  722. glBlendFuncSeparate(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA, GL_ONE, GL_ONE_MINUS_SRC_ALPHA);
  723. } else {
  724. glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
  725. }
  726. // bind the back buffer to a texture so shaders can use it.
  727. // It should probably use texture unit -3 (as GLES3 does as well) but currently that's buggy.
  728. // keeping this for now as there's nothing else that uses texture unit 2
  729. // TODO ^
  730. if (storage->frame.current_rt) {
  731. glActiveTexture(GL_TEXTURE0 + 2);
  732. glBindTexture(GL_TEXTURE_2D, storage->frame.current_rt->copy_screen_effect.color);
  733. }
  734. glBindBuffer(GL_ARRAY_BUFFER, 0);
  735. glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0);
  736. }
  737. void RasterizerCanvasGLES2::_bind_quad_buffer() {
  738. glBindBuffer(GL_ARRAY_BUFFER, data.canvas_quad_vertices);
  739. glEnableVertexAttribArray(VS::ARRAY_VERTEX);
  740. glVertexAttribPointer(VS::ARRAY_VERTEX, 2, GL_FLOAT, GL_FALSE, 0, NULL);
  741. }
  742. void RasterizerCanvasGLES2::draw_generic_textured_rect(const Rect2 &p_rect, const Rect2 &p_src) {
  743. state.canvas_shader.set_uniform(CanvasShaderGLES2::DST_RECT, Color(p_rect.position.x, p_rect.position.y, p_rect.size.x, p_rect.size.y));
  744. state.canvas_shader.set_uniform(CanvasShaderGLES2::SRC_RECT, Color(p_src.position.x, p_src.position.y, p_src.size.x, p_src.size.y));
  745. glDrawArrays(GL_TRIANGLE_FAN, 0, 4);
  746. }
  747. void RasterizerCanvasGLES2::draw_window_margins(int *black_margin, RID *black_image) {
  748. }
  749. void RasterizerCanvasGLES2::initialize() {
  750. // quad buffer
  751. {
  752. glGenBuffers(1, &data.canvas_quad_vertices);
  753. glBindBuffer(GL_ARRAY_BUFFER, data.canvas_quad_vertices);
  754. const float qv[8] = {
  755. 0, 0,
  756. 0, 1,
  757. 1, 1,
  758. 1, 0
  759. };
  760. glBufferData(GL_ARRAY_BUFFER, sizeof(float) * 8, qv, GL_STATIC_DRAW);
  761. glBindBuffer(GL_ARRAY_BUFFER, 0);
  762. }
  763. // polygon buffer
  764. {
  765. uint32_t poly_size = GLOBAL_DEF("rendering/limits/buffers/canvas_polygon_buffer_size_kb", 128);
  766. poly_size *= 1024;
  767. poly_size = MAX(poly_size, (2 + 2 + 4) * 4 * sizeof(float));
  768. glGenBuffers(1, &data.polygon_buffer);
  769. glBindBuffer(GL_ARRAY_BUFFER, data.polygon_buffer);
  770. glBufferData(GL_ARRAY_BUFFER, poly_size, NULL, GL_DYNAMIC_DRAW);
  771. data.polygon_buffer_size = poly_size;
  772. glBindBuffer(GL_ARRAY_BUFFER, 0);
  773. uint32_t index_size = GLOBAL_DEF("rendering/limits/buffers/canvas_polygon_index_size_kb", 128);
  774. index_size *= 1024; // kb
  775. glGenBuffers(1, &data.polygon_index_buffer);
  776. glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, data.polygon_index_buffer);
  777. glBufferData(GL_ELEMENT_ARRAY_BUFFER, index_size, NULL, GL_DYNAMIC_DRAW);
  778. glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0);
  779. }
  780. // ninepatch buffers
  781. {
  782. // array buffer
  783. glGenBuffers(1, &data.ninepatch_vertices);
  784. glBindBuffer(GL_ARRAY_BUFFER, data.ninepatch_vertices);
  785. glBufferData(GL_ARRAY_BUFFER, sizeof(float) * (16 + 16) * 2, NULL, GL_DYNAMIC_DRAW);
  786. glBindBuffer(GL_ARRAY_BUFFER, 0);
  787. // element buffer
  788. glGenBuffers(1, &data.ninepatch_elements);
  789. glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, data.ninepatch_elements);
  790. #define _EIDX(y, x) (y * 4 + x)
  791. uint8_t elems[3 * 2 * 9] = {
  792. // first row
  793. _EIDX(0, 0), _EIDX(0, 1), _EIDX(1, 1),
  794. _EIDX(1, 1), _EIDX(1, 0), _EIDX(0, 0),
  795. _EIDX(0, 1), _EIDX(0, 2), _EIDX(1, 2),
  796. _EIDX(1, 2), _EIDX(1, 1), _EIDX(0, 1),
  797. _EIDX(0, 2), _EIDX(0, 3), _EIDX(1, 3),
  798. _EIDX(1, 3), _EIDX(1, 2), _EIDX(0, 2),
  799. // second row
  800. _EIDX(1, 0), _EIDX(1, 1), _EIDX(2, 1),
  801. _EIDX(2, 1), _EIDX(2, 0), _EIDX(1, 0),
  802. // the center one would be here, but we'll put it at the end
  803. // so it's easier to disable the center and be able to use
  804. // one draw call for both
  805. _EIDX(1, 2), _EIDX(1, 3), _EIDX(2, 3),
  806. _EIDX(2, 3), _EIDX(2, 2), _EIDX(1, 2),
  807. // third row
  808. _EIDX(2, 0), _EIDX(2, 1), _EIDX(3, 1),
  809. _EIDX(3, 1), _EIDX(3, 0), _EIDX(2, 0),
  810. _EIDX(2, 1), _EIDX(2, 2), _EIDX(3, 2),
  811. _EIDX(3, 2), _EIDX(3, 1), _EIDX(2, 1),
  812. _EIDX(2, 2), _EIDX(2, 3), _EIDX(3, 3),
  813. _EIDX(3, 3), _EIDX(3, 2), _EIDX(2, 2),
  814. // center field
  815. _EIDX(1, 1), _EIDX(1, 2), _EIDX(2, 2),
  816. _EIDX(2, 2), _EIDX(2, 1), _EIDX(1, 1)
  817. };
  818. ;
  819. #undef _EIDX
  820. glBufferData(GL_ELEMENT_ARRAY_BUFFER, sizeof(elems), elems, GL_STATIC_DRAW);
  821. glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0);
  822. }
  823. state.canvas_shader.init();
  824. state.canvas_shader.set_conditional(CanvasShaderGLES2::USE_TEXTURE_RECT, true);
  825. state.canvas_shader.bind();
  826. }
  827. void RasterizerCanvasGLES2::finalize() {
  828. }
  829. RasterizerCanvasGLES2::RasterizerCanvasGLES2() {
  830. }