rasterizer_canvas_gles2.cpp 40 KB

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