rasterizer_canvas_gles3.cpp 62 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625
  1. /*************************************************************************/
  2. /* rasterizer_canvas_gles3.cpp */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2022 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2022 Godot Engine contributors (cf. AUTHORS.md). */
  10. /* */
  11. /* Permission is hereby granted, free of charge, to any person obtaining */
  12. /* a copy of this software and associated documentation files (the */
  13. /* "Software"), to deal in the Software without restriction, including */
  14. /* without limitation the rights to use, copy, modify, merge, publish, */
  15. /* distribute, sublicense, and/or sell copies of the Software, and to */
  16. /* permit persons to whom the Software is furnished to do so, subject to */
  17. /* the following conditions: */
  18. /* */
  19. /* The above copyright notice and this permission notice shall be */
  20. /* included in all copies or substantial portions of the Software. */
  21. /* */
  22. /* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
  23. /* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
  24. /* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.*/
  25. /* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
  26. /* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
  27. /* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
  28. /* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
  29. /*************************************************************************/
  30. #include "rasterizer_canvas_gles3.h"
  31. #ifdef GLES3_ENABLED
  32. #include "core/os/os.h"
  33. #include "rasterizer_scene_gles3.h"
  34. #include "core/config/project_settings.h"
  35. #include "servers/rendering/rendering_server_default.h"
  36. #include "storage/config.h"
  37. #include "storage/material_storage.h"
  38. #include "storage/mesh_storage.h"
  39. #include "storage/texture_storage.h"
  40. #ifndef GLES_OVER_GL
  41. #define glClearDepth glClearDepthf
  42. #endif
  43. //static const GLenum gl_primitive[] = {
  44. // GL_POINTS,
  45. // GL_LINES,
  46. // GL_LINE_STRIP,
  47. // GL_LINE_LOOP,
  48. // GL_TRIANGLES,
  49. // GL_TRIANGLE_STRIP,
  50. // GL_TRIANGLE_FAN
  51. //};
  52. void RasterizerCanvasGLES3::_update_transform_2d_to_mat4(const Transform2D &p_transform, float *p_mat4) {
  53. p_mat4[0] = p_transform.columns[0][0];
  54. p_mat4[1] = p_transform.columns[0][1];
  55. p_mat4[2] = 0;
  56. p_mat4[3] = 0;
  57. p_mat4[4] = p_transform.columns[1][0];
  58. p_mat4[5] = p_transform.columns[1][1];
  59. p_mat4[6] = 0;
  60. p_mat4[7] = 0;
  61. p_mat4[8] = 0;
  62. p_mat4[9] = 0;
  63. p_mat4[10] = 1;
  64. p_mat4[11] = 0;
  65. p_mat4[12] = p_transform.columns[2][0];
  66. p_mat4[13] = p_transform.columns[2][1];
  67. p_mat4[14] = 0;
  68. p_mat4[15] = 1;
  69. }
  70. void RasterizerCanvasGLES3::_update_transform_2d_to_mat2x4(const Transform2D &p_transform, float *p_mat2x4) {
  71. p_mat2x4[0] = p_transform.columns[0][0];
  72. p_mat2x4[1] = p_transform.columns[1][0];
  73. p_mat2x4[2] = 0;
  74. p_mat2x4[3] = p_transform.columns[2][0];
  75. p_mat2x4[4] = p_transform.columns[0][1];
  76. p_mat2x4[5] = p_transform.columns[1][1];
  77. p_mat2x4[6] = 0;
  78. p_mat2x4[7] = p_transform.columns[2][1];
  79. }
  80. void RasterizerCanvasGLES3::_update_transform_2d_to_mat2x3(const Transform2D &p_transform, float *p_mat2x3) {
  81. p_mat2x3[0] = p_transform.columns[0][0];
  82. p_mat2x3[1] = p_transform.columns[0][1];
  83. p_mat2x3[2] = p_transform.columns[1][0];
  84. p_mat2x3[3] = p_transform.columns[1][1];
  85. p_mat2x3[4] = p_transform.columns[2][0];
  86. p_mat2x3[5] = p_transform.columns[2][1];
  87. }
  88. void RasterizerCanvasGLES3::_update_transform_to_mat4(const Transform3D &p_transform, float *p_mat4) {
  89. p_mat4[0] = p_transform.basis.rows[0][0];
  90. p_mat4[1] = p_transform.basis.rows[1][0];
  91. p_mat4[2] = p_transform.basis.rows[2][0];
  92. p_mat4[3] = 0;
  93. p_mat4[4] = p_transform.basis.rows[0][1];
  94. p_mat4[5] = p_transform.basis.rows[1][1];
  95. p_mat4[6] = p_transform.basis.rows[2][1];
  96. p_mat4[7] = 0;
  97. p_mat4[8] = p_transform.basis.rows[0][2];
  98. p_mat4[9] = p_transform.basis.rows[1][2];
  99. p_mat4[10] = p_transform.basis.rows[2][2];
  100. p_mat4[11] = 0;
  101. p_mat4[12] = p_transform.origin.x;
  102. p_mat4[13] = p_transform.origin.y;
  103. p_mat4[14] = p_transform.origin.z;
  104. p_mat4[15] = 1;
  105. }
  106. void RasterizerCanvasGLES3::canvas_render_items(RID p_to_render_target, Item *p_item_list, const Color &p_modulate, Light *p_light_list, Light *p_directional_list, const Transform2D &p_canvas_transform, RS::CanvasItemTextureFilter p_default_filter, RS::CanvasItemTextureRepeat p_default_repeat, bool p_snap_2d_vertices_to_pixel, bool &r_sdf_used) {
  107. GLES3::TextureStorage *texture_storage = GLES3::TextureStorage::get_singleton();
  108. GLES3::MaterialStorage *material_storage = GLES3::MaterialStorage::get_singleton();
  109. Transform2D canvas_transform_inverse = p_canvas_transform.affine_inverse();
  110. // Clear out any state that may have been left from the 3D pass.
  111. reset_canvas();
  112. // TODO: Setup Directional Lights
  113. // TODO: Setup lights
  114. {
  115. //update canvas state uniform buffer
  116. StateBuffer state_buffer;
  117. Size2i ssize = texture_storage->render_target_get_size(p_to_render_target);
  118. Transform3D screen_transform;
  119. screen_transform.translate(-(ssize.width / 2.0f), -(ssize.height / 2.0f), 0.0f);
  120. screen_transform.scale(Vector3(2.0f / ssize.width, 2.0f / ssize.height, 1.0f));
  121. _update_transform_to_mat4(screen_transform, state_buffer.screen_transform);
  122. _update_transform_2d_to_mat4(p_canvas_transform, state_buffer.canvas_transform);
  123. Transform2D normal_transform = p_canvas_transform;
  124. normal_transform.columns[0].normalize();
  125. normal_transform.columns[1].normalize();
  126. normal_transform.columns[2] = Vector2();
  127. _update_transform_2d_to_mat4(normal_transform, state_buffer.canvas_normal_transform);
  128. state_buffer.canvas_modulate[0] = p_modulate.r;
  129. state_buffer.canvas_modulate[1] = p_modulate.g;
  130. state_buffer.canvas_modulate[2] = p_modulate.b;
  131. state_buffer.canvas_modulate[3] = p_modulate.a;
  132. Size2 render_target_size = texture_storage->render_target_get_size(p_to_render_target);
  133. state_buffer.screen_pixel_size[0] = 1.0 / render_target_size.x;
  134. state_buffer.screen_pixel_size[1] = 1.0 / render_target_size.y;
  135. // TODO: temporary, this should be set at the top of this function
  136. glViewport(0, 0, render_target_size.x, render_target_size.y);
  137. state_buffer.time = state.time;
  138. state_buffer.use_pixel_snap = p_snap_2d_vertices_to_pixel;
  139. state_buffer.directional_light_count = 0; //directional_light_count;
  140. Vector2 canvas_scale = p_canvas_transform.get_scale();
  141. state_buffer.sdf_to_screen[0] = render_target_size.width / canvas_scale.x;
  142. state_buffer.sdf_to_screen[1] = render_target_size.height / canvas_scale.y;
  143. state_buffer.screen_to_sdf[0] = 1.0 / state_buffer.sdf_to_screen[0];
  144. state_buffer.screen_to_sdf[1] = 1.0 / state_buffer.sdf_to_screen[1];
  145. Rect2 sdf_rect = texture_storage->render_target_get_sdf_rect(p_to_render_target);
  146. Rect2 sdf_tex_rect(sdf_rect.position / canvas_scale, sdf_rect.size / canvas_scale);
  147. state_buffer.sdf_to_tex[0] = 1.0 / sdf_tex_rect.size.width;
  148. state_buffer.sdf_to_tex[1] = 1.0 / sdf_tex_rect.size.height;
  149. state_buffer.sdf_to_tex[2] = -sdf_tex_rect.position.x / sdf_tex_rect.size.width;
  150. state_buffer.sdf_to_tex[3] = -sdf_tex_rect.position.y / sdf_tex_rect.size.height;
  151. state_buffer.tex_to_sdf = 1.0 / ((canvas_scale.x + canvas_scale.y) * 0.5);
  152. glBindBufferBase(GL_UNIFORM_BUFFER, BASE_UNIFORM_LOCATION, state.canvas_state_buffer);
  153. glBufferData(GL_UNIFORM_BUFFER, sizeof(StateBuffer), &state_buffer, GL_STREAM_DRAW);
  154. GLuint global_buffer = material_storage->global_variables_get_uniform_buffer();
  155. glBindBufferBase(GL_UNIFORM_BUFFER, GLOBAL_UNIFORM_LOCATION, global_buffer);
  156. glBindBuffer(GL_UNIFORM_BUFFER, 0);
  157. }
  158. {
  159. state.default_filter = p_default_filter;
  160. state.default_repeat = p_default_repeat;
  161. }
  162. r_sdf_used = false;
  163. int item_count = 0;
  164. bool backbuffer_cleared = false;
  165. bool time_used = false;
  166. bool material_screen_texture_found = false;
  167. Rect2 back_buffer_rect;
  168. bool backbuffer_copy = false;
  169. Item *ci = p_item_list;
  170. Item *canvas_group_owner = nullptr;
  171. while (ci) {
  172. if (ci->copy_back_buffer && canvas_group_owner == nullptr) {
  173. backbuffer_copy = true;
  174. if (ci->copy_back_buffer->full) {
  175. back_buffer_rect = Rect2();
  176. } else {
  177. back_buffer_rect = ci->copy_back_buffer->rect;
  178. }
  179. }
  180. // Check material for something that may change flow of rendering, but do not bind for now.
  181. RID material = ci->material_owner == nullptr ? ci->material : ci->material_owner->material;
  182. if (material.is_valid()) {
  183. GLES3::CanvasMaterialData *md = static_cast<GLES3::CanvasMaterialData *>(material_storage->material_get_data(material, RS::SHADER_CANVAS_ITEM));
  184. if (md && md->shader_data->valid) {
  185. if (md->shader_data->uses_screen_texture && canvas_group_owner == nullptr) {
  186. if (!material_screen_texture_found) {
  187. backbuffer_copy = true;
  188. back_buffer_rect = Rect2();
  189. }
  190. }
  191. if (md->shader_data->uses_sdf) {
  192. r_sdf_used = true;
  193. }
  194. if (md->shader_data->uses_time) {
  195. time_used = true;
  196. }
  197. }
  198. }
  199. if (ci->canvas_group_owner != nullptr) {
  200. if (canvas_group_owner == nullptr) {
  201. // Canvas group begins here, render until before this item
  202. _render_items(p_to_render_target, item_count, canvas_transform_inverse, p_light_list);
  203. item_count = 0;
  204. Rect2i group_rect = ci->canvas_group_owner->global_rect_cache;
  205. if (ci->canvas_group_owner->canvas_group->mode == RS::CANVAS_GROUP_MODE_OPAQUE) {
  206. texture_storage->render_target_copy_to_back_buffer(p_to_render_target, group_rect, false);
  207. } else if (!backbuffer_cleared) {
  208. texture_storage->render_target_clear_back_buffer(p_to_render_target, Rect2i(), Color(0, 0, 0, 0));
  209. backbuffer_cleared = true;
  210. }
  211. backbuffer_copy = false;
  212. canvas_group_owner = ci->canvas_group_owner; //continue until owner found
  213. }
  214. ci->canvas_group_owner = nullptr; //must be cleared
  215. }
  216. if (!backbuffer_cleared && canvas_group_owner == nullptr && ci->canvas_group != nullptr && !backbuffer_copy) {
  217. texture_storage->render_target_clear_back_buffer(p_to_render_target, Rect2i(), Color(0, 0, 0, 0));
  218. backbuffer_cleared = true;
  219. }
  220. if (ci == canvas_group_owner) {
  221. _render_items(p_to_render_target, item_count, canvas_transform_inverse, p_light_list, true);
  222. item_count = 0;
  223. if (ci->canvas_group->blur_mipmaps) {
  224. texture_storage->render_target_gen_back_buffer_mipmaps(p_to_render_target, ci->global_rect_cache);
  225. }
  226. canvas_group_owner = nullptr;
  227. }
  228. if (backbuffer_copy) {
  229. //render anything pending, including clearing if no items
  230. _render_items(p_to_render_target, item_count, canvas_transform_inverse, p_light_list);
  231. item_count = 0;
  232. texture_storage->render_target_copy_to_back_buffer(p_to_render_target, back_buffer_rect, true);
  233. backbuffer_copy = false;
  234. material_screen_texture_found = true; //after a backbuffer copy, screen texture makes no further copies
  235. }
  236. // just add all items for now
  237. items[item_count++] = ci;
  238. if (!ci->next || item_count == MAX_RENDER_ITEMS - 1) {
  239. _render_items(p_to_render_target, item_count, canvas_transform_inverse, p_light_list);
  240. //then reset
  241. item_count = 0;
  242. }
  243. ci = ci->next;
  244. }
  245. if (time_used) {
  246. RenderingServerDefault::redraw_request();
  247. }
  248. // Clear out state used in 2D pass
  249. reset_canvas();
  250. }
  251. void RasterizerCanvasGLES3::_render_items(RID p_to_render_target, int p_item_count, const Transform2D &p_canvas_transform_inverse, Light *p_lights, bool p_to_backbuffer) {
  252. GLES3::TextureStorage *texture_storage = GLES3::TextureStorage::get_singleton();
  253. GLES3::MaterialStorage *material_storage = GLES3::MaterialStorage::get_singleton();
  254. Item *current_clip = nullptr;
  255. Transform2D canvas_transform_inverse = p_canvas_transform_inverse;
  256. canvas_begin(p_to_render_target, p_to_backbuffer);
  257. RID prev_material;
  258. uint32_t index = 0;
  259. GLES3::CanvasShaderData::BlendMode last_blend_mode = GLES3::CanvasShaderData::BLEND_MODE_MIX;
  260. GLES3::CanvasShaderData *shader_data_cache = nullptr;
  261. state.current_tex = texture_storage->texture_gl_get_default(GLES3::DEFAULT_GL_TEXTURE_WHITE);
  262. state.current_tex_ptr = nullptr;
  263. state.current_normal = RID();
  264. state.current_specular = RID();
  265. state.canvas_texscreen_used = false;
  266. state.current_shader_version = state.canvas_shader_default_version;
  267. for (int i = 0; i < p_item_count; i++) {
  268. Item *ci = items[i];
  269. if (current_clip != ci->final_clip_owner) {
  270. _render_batch(index);
  271. current_clip = ci->final_clip_owner;
  272. //setup clip
  273. if (current_clip) {
  274. glEnable(GL_SCISSOR_TEST);
  275. glScissor(current_clip->final_clip_rect.position.x, current_clip->final_clip_rect.position.y, current_clip->final_clip_rect.size.x, current_clip->final_clip_rect.size.y);
  276. } else {
  277. glDisable(GL_SCISSOR_TEST);
  278. }
  279. }
  280. RID material = ci->material_owner == nullptr ? ci->material : ci->material_owner->material;
  281. if (material.is_null() && ci->canvas_group != nullptr) {
  282. material = default_canvas_group_material;
  283. }
  284. if (material != prev_material) {
  285. _render_batch(index);
  286. GLES3::CanvasMaterialData *material_data = nullptr;
  287. if (material.is_valid()) {
  288. material_data = static_cast<GLES3::CanvasMaterialData *>(material_storage->material_get_data(material, RS::SHADER_CANVAS_ITEM));
  289. }
  290. if (material_data) {
  291. if (material_data->shader_data->version.is_valid() && material_data->shader_data->valid) {
  292. // Bind uniform buffer and textures
  293. material_data->bind_uniforms();
  294. state.current_shader_version = material_data->shader_data->version;
  295. shader_data_cache = material_data->shader_data;
  296. } else {
  297. state.current_shader_version = state.canvas_shader_default_version;
  298. shader_data_cache = nullptr;
  299. }
  300. } else {
  301. state.current_shader_version = state.canvas_shader_default_version;
  302. shader_data_cache = nullptr;
  303. }
  304. prev_material = material;
  305. }
  306. GLES3::CanvasShaderData::BlendMode blend_mode = shader_data_cache ? shader_data_cache->blend_mode : GLES3::CanvasShaderData::BLEND_MODE_MIX;
  307. if (last_blend_mode != blend_mode) {
  308. if (last_blend_mode == GLES3::CanvasShaderData::BLEND_MODE_DISABLED) {
  309. // re-enable it
  310. glEnable(GL_BLEND);
  311. } else if (blend_mode == GLES3::CanvasShaderData::BLEND_MODE_DISABLED) {
  312. // disable it
  313. glDisable(GL_BLEND);
  314. }
  315. switch (blend_mode) {
  316. case GLES3::CanvasShaderData::BLEND_MODE_DISABLED: {
  317. // Nothing to do here.
  318. } break;
  319. case GLES3::CanvasShaderData::BLEND_MODE_MIX: {
  320. glBlendEquation(GL_FUNC_ADD);
  321. if (state.transparent_render_target) {
  322. glBlendFuncSeparate(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA, GL_ONE, GL_ONE_MINUS_SRC_ALPHA);
  323. } else {
  324. glBlendFuncSeparate(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA, GL_ZERO, GL_ONE);
  325. }
  326. } break;
  327. case GLES3::CanvasShaderData::BLEND_MODE_ADD: {
  328. glBlendEquation(GL_FUNC_ADD);
  329. if (state.transparent_render_target) {
  330. glBlendFuncSeparate(GL_SRC_ALPHA, GL_ONE, GL_SRC_ALPHA, GL_ONE);
  331. } else {
  332. glBlendFuncSeparate(GL_SRC_ALPHA, GL_ONE, GL_ZERO, GL_ONE);
  333. }
  334. } break;
  335. case GLES3::CanvasShaderData::BLEND_MODE_SUB: {
  336. glBlendEquation(GL_FUNC_REVERSE_SUBTRACT);
  337. if (state.transparent_render_target) {
  338. glBlendFuncSeparate(GL_SRC_ALPHA, GL_ONE, GL_SRC_ALPHA, GL_ONE);
  339. } else {
  340. glBlendFuncSeparate(GL_SRC_ALPHA, GL_ONE, GL_ZERO, GL_ONE);
  341. }
  342. } break;
  343. case GLES3::CanvasShaderData::BLEND_MODE_MUL: {
  344. glBlendEquation(GL_FUNC_ADD);
  345. if (state.transparent_render_target) {
  346. glBlendFuncSeparate(GL_DST_COLOR, GL_ZERO, GL_DST_ALPHA, GL_ZERO);
  347. } else {
  348. glBlendFuncSeparate(GL_DST_COLOR, GL_ZERO, GL_ZERO, GL_ONE);
  349. }
  350. } break;
  351. case GLES3::CanvasShaderData::BLEND_MODE_PMALPHA: {
  352. glBlendEquation(GL_FUNC_ADD);
  353. if (state.transparent_render_target) {
  354. glBlendFuncSeparate(GL_ONE, GL_ONE_MINUS_SRC_ALPHA, GL_ONE, GL_ONE_MINUS_SRC_ALPHA);
  355. } else {
  356. glBlendFuncSeparate(GL_ONE, GL_ONE_MINUS_SRC_ALPHA, GL_ZERO, GL_ONE);
  357. }
  358. } break;
  359. }
  360. last_blend_mode = blend_mode;
  361. }
  362. _render_item(p_to_render_target, ci, canvas_transform_inverse, current_clip, p_lights, index);
  363. }
  364. // Render last command
  365. _render_batch(index);
  366. }
  367. void RasterizerCanvasGLES3::_render_item(RID p_render_target, const Item *p_item, const Transform2D &p_canvas_transform_inverse, Item *&current_clip, Light *p_lights, uint32_t &r_index) {
  368. // Used by Polygon and Mesh.
  369. static const GLenum prim[5] = { GL_POINTS, GL_LINES, GL_LINE_STRIP, GL_TRIANGLES, GL_TRIANGLE_STRIP };
  370. RS::CanvasItemTextureFilter current_filter = state.default_filter;
  371. RS::CanvasItemTextureRepeat current_repeat = state.default_repeat;
  372. if (p_item->texture_filter != RS::CANVAS_ITEM_TEXTURE_FILTER_DEFAULT) {
  373. current_filter = p_item->texture_filter;
  374. }
  375. if (p_item->texture_repeat != RS::CANVAS_ITEM_TEXTURE_REPEAT_DEFAULT) {
  376. current_repeat = p_item->texture_repeat;
  377. }
  378. Transform2D base_transform = p_canvas_transform_inverse * p_item->final_transform;
  379. Transform2D draw_transform; // Used by transform command
  380. Color base_color = p_item->final_modulate;
  381. uint32_t base_flags = 0;
  382. bool reclip = false;
  383. bool skipping = false;
  384. const Item::Command *c = p_item->commands;
  385. while (c) {
  386. if (skipping && c->type != Item::Command::TYPE_ANIMATION_SLICE) {
  387. c = c->next;
  388. continue;
  389. }
  390. if (c->type != Item::Command::TYPE_MESH) {
  391. // For Meshes, this gets updated below.
  392. _update_transform_2d_to_mat2x3(base_transform * draw_transform, state.instance_data_array[r_index].world);
  393. }
  394. for (int i = 0; i < 4; i++) {
  395. state.instance_data_array[r_index].modulation[i] = 0.0;
  396. state.instance_data_array[r_index].ninepatch_margins[i] = 0.0;
  397. state.instance_data_array[r_index].src_rect[i] = 0.0;
  398. state.instance_data_array[r_index].dst_rect[i] = 0.0;
  399. state.instance_data_array[r_index].lights[i] = uint32_t(0);
  400. }
  401. state.instance_data_array[r_index].color_texture_pixel_size[0] = 0.0;
  402. state.instance_data_array[r_index].color_texture_pixel_size[1] = 0.0;
  403. state.instance_data_array[r_index].pad[0] = 0.0;
  404. state.instance_data_array[r_index].pad[1] = 0.0;
  405. state.instance_data_array[r_index].flags = base_flags | (state.instance_data_array[r_index == 0 ? 0 : r_index - 1].flags & (FLAGS_DEFAULT_NORMAL_MAP_USED | FLAGS_DEFAULT_SPECULAR_MAP_USED)); //reset on each command for sanity, keep canvastexture binding config
  406. switch (c->type) {
  407. case Item::Command::TYPE_RECT: {
  408. const Item::CommandRect *rect = static_cast<const Item::CommandRect *>(c);
  409. if (rect->flags & CANVAS_RECT_TILE) {
  410. current_repeat = RenderingServer::CanvasItemTextureRepeat::CANVAS_ITEM_TEXTURE_REPEAT_ENABLED;
  411. }
  412. if (rect->texture != state.current_tex || state.current_primitive_points != 0 || state.current_command != Item::Command::TYPE_RECT) {
  413. _render_batch(r_index);
  414. state.current_primitive_points = 0;
  415. state.current_command = Item::Command::TYPE_RECT;
  416. }
  417. _bind_canvas_texture(rect->texture, current_filter, current_repeat, r_index);
  418. GLES3::MaterialStorage::get_singleton()->shaders.canvas_shader.version_bind_shader(state.current_shader_version, CanvasShaderGLES3::MODE_QUAD);
  419. Rect2 src_rect;
  420. Rect2 dst_rect;
  421. if (rect->texture != RID()) {
  422. src_rect = (rect->flags & CANVAS_RECT_REGION) ? Rect2(rect->source.position * state.current_pixel_size, rect->source.size * state.current_pixel_size) : Rect2(0, 0, 1, 1);
  423. dst_rect = Rect2(rect->rect.position, rect->rect.size);
  424. if (dst_rect.size.width < 0) {
  425. dst_rect.position.x += dst_rect.size.width;
  426. dst_rect.size.width *= -1;
  427. }
  428. if (dst_rect.size.height < 0) {
  429. dst_rect.position.y += dst_rect.size.height;
  430. dst_rect.size.height *= -1;
  431. }
  432. if (rect->flags & CANVAS_RECT_FLIP_H) {
  433. src_rect.size.x *= -1;
  434. }
  435. if (rect->flags & CANVAS_RECT_FLIP_V) {
  436. src_rect.size.y *= -1;
  437. }
  438. if (rect->flags & CANVAS_RECT_TRANSPOSE) {
  439. dst_rect.size.x *= -1; // Encoding in the dst_rect.z uniform
  440. }
  441. if (rect->flags & CANVAS_RECT_CLIP_UV) {
  442. state.instance_data_array[r_index].flags |= FLAGS_CLIP_RECT_UV;
  443. }
  444. } else {
  445. dst_rect = Rect2(rect->rect.position, rect->rect.size);
  446. if (dst_rect.size.width < 0) {
  447. dst_rect.position.x += dst_rect.size.width;
  448. dst_rect.size.width *= -1;
  449. }
  450. if (dst_rect.size.height < 0) {
  451. dst_rect.position.y += dst_rect.size.height;
  452. dst_rect.size.height *= -1;
  453. }
  454. src_rect = Rect2(0, 0, 1, 1);
  455. }
  456. if (rect->flags & CANVAS_RECT_MSDF) {
  457. state.instance_data_array[r_index].flags |= FLAGS_USE_MSDF;
  458. state.instance_data_array[r_index].msdf[0] = rect->px_range; // Pixel range.
  459. state.instance_data_array[r_index].msdf[1] = rect->outline; // Outline size.
  460. state.instance_data_array[r_index].msdf[2] = 0.f; // Reserved.
  461. state.instance_data_array[r_index].msdf[3] = 0.f; // Reserved.
  462. }
  463. state.instance_data_array[r_index].modulation[0] = rect->modulate.r * base_color.r;
  464. state.instance_data_array[r_index].modulation[1] = rect->modulate.g * base_color.g;
  465. state.instance_data_array[r_index].modulation[2] = rect->modulate.b * base_color.b;
  466. state.instance_data_array[r_index].modulation[3] = rect->modulate.a * base_color.a;
  467. state.instance_data_array[r_index].src_rect[0] = src_rect.position.x;
  468. state.instance_data_array[r_index].src_rect[1] = src_rect.position.y;
  469. state.instance_data_array[r_index].src_rect[2] = src_rect.size.width;
  470. state.instance_data_array[r_index].src_rect[3] = src_rect.size.height;
  471. state.instance_data_array[r_index].dst_rect[0] = dst_rect.position.x;
  472. state.instance_data_array[r_index].dst_rect[1] = dst_rect.position.y;
  473. state.instance_data_array[r_index].dst_rect[2] = dst_rect.size.width;
  474. state.instance_data_array[r_index].dst_rect[3] = dst_rect.size.height;
  475. r_index++;
  476. if (r_index >= state.max_instances_per_batch - 1) {
  477. _render_batch(r_index);
  478. }
  479. } break;
  480. case Item::Command::TYPE_NINEPATCH: {
  481. const Item::CommandNinePatch *np = static_cast<const Item::CommandNinePatch *>(c);
  482. if (np->texture != state.current_tex || state.current_primitive_points != 0 || state.current_command != Item::Command::TYPE_NINEPATCH) {
  483. _render_batch(r_index);
  484. state.current_primitive_points = 0;
  485. state.current_command = Item::Command::TYPE_NINEPATCH;
  486. }
  487. //bind textures
  488. _bind_canvas_texture(np->texture, current_filter, current_repeat, r_index);
  489. GLES3::MaterialStorage::get_singleton()->shaders.canvas_shader.version_bind_shader(state.current_shader_version, CanvasShaderGLES3::MODE_NINEPATCH);
  490. Rect2 src_rect;
  491. Rect2 dst_rect(np->rect.position.x, np->rect.position.y, np->rect.size.x, np->rect.size.y);
  492. if (np->texture == RID()) {
  493. state.current_pixel_size = Size2(1, 1);
  494. src_rect = Rect2(0, 0, 1, 1);
  495. } else {
  496. if (np->source != Rect2()) {
  497. src_rect = Rect2(np->source.position.x * state.current_pixel_size.width, np->source.position.y * state.current_pixel_size.height, np->source.size.x * state.current_pixel_size.width, np->source.size.y * state.current_pixel_size.height);
  498. state.instance_data_array[r_index].color_texture_pixel_size[0] = 1.0 / np->source.size.width;
  499. state.instance_data_array[r_index].color_texture_pixel_size[1] = 1.0 / np->source.size.height;
  500. } else {
  501. src_rect = Rect2(0, 0, 1, 1);
  502. }
  503. }
  504. state.instance_data_array[r_index].modulation[0] = np->color.r * base_color.r;
  505. state.instance_data_array[r_index].modulation[1] = np->color.g * base_color.g;
  506. state.instance_data_array[r_index].modulation[2] = np->color.b * base_color.b;
  507. state.instance_data_array[r_index].modulation[3] = np->color.a * base_color.a;
  508. state.instance_data_array[r_index].src_rect[0] = src_rect.position.x;
  509. state.instance_data_array[r_index].src_rect[1] = src_rect.position.y;
  510. state.instance_data_array[r_index].src_rect[2] = src_rect.size.width;
  511. state.instance_data_array[r_index].src_rect[3] = src_rect.size.height;
  512. state.instance_data_array[r_index].dst_rect[0] = dst_rect.position.x;
  513. state.instance_data_array[r_index].dst_rect[1] = dst_rect.position.y;
  514. state.instance_data_array[r_index].dst_rect[2] = dst_rect.size.width;
  515. state.instance_data_array[r_index].dst_rect[3] = dst_rect.size.height;
  516. state.instance_data_array[r_index].flags |= int(np->axis_x) << FLAGS_NINEPATCH_H_MODE_SHIFT;
  517. state.instance_data_array[r_index].flags |= int(np->axis_y) << FLAGS_NINEPATCH_V_MODE_SHIFT;
  518. if (np->draw_center) {
  519. state.instance_data_array[r_index].flags |= FLAGS_NINEPACH_DRAW_CENTER;
  520. }
  521. state.instance_data_array[r_index].ninepatch_margins[0] = np->margin[SIDE_LEFT];
  522. state.instance_data_array[r_index].ninepatch_margins[1] = np->margin[SIDE_TOP];
  523. state.instance_data_array[r_index].ninepatch_margins[2] = np->margin[SIDE_RIGHT];
  524. state.instance_data_array[r_index].ninepatch_margins[3] = np->margin[SIDE_BOTTOM];
  525. r_index++;
  526. if (r_index >= state.max_instances_per_batch - 1) {
  527. _render_batch(r_index);
  528. }
  529. // Restore if overridden.
  530. state.instance_data_array[r_index].color_texture_pixel_size[0] = state.current_pixel_size.x;
  531. state.instance_data_array[r_index].color_texture_pixel_size[1] = state.current_pixel_size.y;
  532. } break;
  533. case Item::Command::TYPE_POLYGON: {
  534. const Item::CommandPolygon *polygon = static_cast<const Item::CommandPolygon *>(c);
  535. PolygonBuffers *pb = polygon_buffers.polygons.getptr(polygon->polygon.polygon_id);
  536. ERR_CONTINUE(!pb);
  537. if (polygon->texture != state.current_tex || state.current_primitive_points != 0 || state.current_command != Item::Command::TYPE_POLYGON) {
  538. _render_batch(r_index);
  539. state.current_primitive_points = 0;
  540. state.current_command = Item::Command::TYPE_POLYGON;
  541. }
  542. _bind_canvas_texture(polygon->texture, current_filter, current_repeat, r_index);
  543. GLES3::MaterialStorage::get_singleton()->shaders.canvas_shader.version_bind_shader(state.current_shader_version, CanvasShaderGLES3::MODE_ATTRIBUTES);
  544. state.current_primitive = polygon->primitive;
  545. state.instance_data_array[r_index].modulation[0] = base_color.r;
  546. state.instance_data_array[r_index].modulation[1] = base_color.g;
  547. state.instance_data_array[r_index].modulation[2] = base_color.b;
  548. state.instance_data_array[r_index].modulation[3] = base_color.a;
  549. for (int j = 0; j < 4; j++) {
  550. state.instance_data_array[r_index].src_rect[j] = 0;
  551. state.instance_data_array[r_index].dst_rect[j] = 0;
  552. state.instance_data_array[r_index].ninepatch_margins[j] = 0;
  553. }
  554. _bind_instance_data_buffer(1);
  555. glBindVertexArray(pb->vertex_array);
  556. if (pb->color_disabled) {
  557. glVertexAttrib4f(RS::ARRAY_COLOR, pb->color.r, pb->color.g, pb->color.b, pb->color.a);
  558. }
  559. if (pb->index_buffer != 0) {
  560. glDrawElements(prim[polygon->primitive], pb->count, GL_UNSIGNED_INT, nullptr);
  561. } else {
  562. glDrawArrays(prim[polygon->primitive], 0, pb->count);
  563. }
  564. glBindVertexArray(0);
  565. state.fences[state.current_buffer] = glFenceSync(GL_SYNC_GPU_COMMANDS_COMPLETE, 0);
  566. state.current_buffer = (state.current_buffer + 1) % state.canvas_instance_data_buffers.size();
  567. if (pb->color_disabled) {
  568. // Reset so this doesn't pollute other draw calls.
  569. glVertexAttrib4f(RS::ARRAY_COLOR, 1.0, 1.0, 1.0, 1.0);
  570. }
  571. } break;
  572. case Item::Command::TYPE_PRIMITIVE: {
  573. const Item::CommandPrimitive *primitive = static_cast<const Item::CommandPrimitive *>(c);
  574. if (state.current_primitive_points != primitive->point_count || state.current_command != Item::Command::TYPE_PRIMITIVE) {
  575. _render_batch(r_index);
  576. state.current_primitive_points = primitive->point_count;
  577. state.current_command = Item::Command::TYPE_PRIMITIVE;
  578. }
  579. _bind_canvas_texture(RID(), current_filter, current_repeat, r_index);
  580. GLES3::MaterialStorage::get_singleton()->shaders.canvas_shader.version_bind_shader(state.current_shader_version, CanvasShaderGLES3::MODE_PRIMITIVE);
  581. for (uint32_t j = 0; j < MIN(3u, primitive->point_count); j++) {
  582. state.instance_data_array[r_index].points[j * 2 + 0] = primitive->points[j].x;
  583. state.instance_data_array[r_index].points[j * 2 + 1] = primitive->points[j].y;
  584. state.instance_data_array[r_index].uvs[j * 2 + 0] = primitive->uvs[j].x;
  585. state.instance_data_array[r_index].uvs[j * 2 + 1] = primitive->uvs[j].y;
  586. Color col = primitive->colors[j] * base_color;
  587. state.instance_data_array[r_index].colors[j * 2 + 0] = (uint32_t(Math::make_half_float(col.g)) << 16) | Math::make_half_float(col.r);
  588. state.instance_data_array[r_index].colors[j * 2 + 1] = (uint32_t(Math::make_half_float(col.a)) << 16) | Math::make_half_float(col.b);
  589. }
  590. r_index++;
  591. if (primitive->point_count == 4) {
  592. // Reset base data
  593. _update_transform_2d_to_mat2x3(base_transform * draw_transform, state.instance_data_array[r_index].world);
  594. state.instance_data_array[r_index].color_texture_pixel_size[0] = 0.0;
  595. state.instance_data_array[r_index].color_texture_pixel_size[1] = 0.0;
  596. state.instance_data_array[r_index].flags = base_flags | (state.instance_data_array[r_index == 0 ? 0 : r_index - 1].flags & (FLAGS_DEFAULT_NORMAL_MAP_USED | FLAGS_DEFAULT_SPECULAR_MAP_USED)); //reset on each command for sanity, keep canvastexture binding config
  597. for (uint32_t j = 0; j < 3; j++) {
  598. //second half of triangle
  599. state.instance_data_array[r_index].points[j * 2 + 0] = primitive->points[j + 1].x;
  600. state.instance_data_array[r_index].points[j * 2 + 1] = primitive->points[j + 1].y;
  601. state.instance_data_array[r_index].uvs[j * 2 + 0] = primitive->uvs[j + 1].x;
  602. state.instance_data_array[r_index].uvs[j * 2 + 1] = primitive->uvs[j + 1].y;
  603. Color col = primitive->colors[j + 1] * base_color;
  604. state.instance_data_array[r_index].colors[j * 2 + 0] = (uint32_t(Math::make_half_float(col.g)) << 16) | Math::make_half_float(col.r);
  605. state.instance_data_array[r_index].colors[j * 2 + 1] = (uint32_t(Math::make_half_float(col.a)) << 16) | Math::make_half_float(col.b);
  606. }
  607. r_index++;
  608. }
  609. if (r_index >= state.max_instances_per_batch - 1) {
  610. _render_batch(r_index);
  611. }
  612. } break;
  613. case Item::Command::TYPE_MESH:
  614. case Item::Command::TYPE_MULTIMESH:
  615. case Item::Command::TYPE_PARTICLES: {
  616. GLES3::MeshStorage *mesh_storage = GLES3::MeshStorage::get_singleton();
  617. RID mesh;
  618. RID mesh_instance;
  619. RID texture;
  620. Color modulate(1, 1, 1, 1);
  621. uint32_t instance_count = 1;
  622. GLuint multimesh_buffer = 0;
  623. uint32_t multimesh_stride = 0;
  624. uint32_t multimesh_color_offset = 0;
  625. bool multimesh_uses_color = false;
  626. bool multimesh_uses_custom_data = false;
  627. if (c->type == Item::Command::TYPE_MESH) {
  628. const Item::CommandMesh *m = static_cast<const Item::CommandMesh *>(c);
  629. mesh = m->mesh;
  630. mesh_instance = m->mesh_instance;
  631. texture = m->texture;
  632. modulate = m->modulate;
  633. _update_transform_2d_to_mat2x3(base_transform * draw_transform * m->transform, state.instance_data_array[r_index].world);
  634. } else if (c->type == Item::Command::TYPE_MULTIMESH) {
  635. const Item::CommandMultiMesh *mm = static_cast<const Item::CommandMultiMesh *>(c);
  636. RID multimesh = mm->multimesh;
  637. mesh = mesh_storage->multimesh_get_mesh(multimesh);
  638. texture = mm->texture;
  639. if (mesh_storage->multimesh_get_transform_format(multimesh) != RS::MULTIMESH_TRANSFORM_2D) {
  640. break;
  641. }
  642. instance_count = mesh_storage->multimesh_get_instances_to_draw(multimesh);
  643. if (instance_count == 0) {
  644. break;
  645. }
  646. multimesh_buffer = mesh_storage->multimesh_get_gl_buffer(multimesh);
  647. multimesh_stride = mesh_storage->multimesh_get_stride(multimesh);
  648. multimesh_color_offset = mesh_storage->multimesh_get_color_offset(multimesh);
  649. multimesh_uses_color = mesh_storage->multimesh_uses_colors(multimesh);
  650. multimesh_uses_custom_data = mesh_storage->multimesh_uses_custom_data(multimesh);
  651. }
  652. // TODO: implement particles here
  653. if (mesh.is_null()) {
  654. break;
  655. }
  656. if (texture != state.current_tex || state.current_primitive_points != 0 || state.current_command != Item::Command::TYPE_PRIMITIVE) {
  657. _render_batch(r_index);
  658. state.current_primitive_points = 0;
  659. state.current_command = c->type;
  660. }
  661. _bind_canvas_texture(texture, current_filter, current_repeat, r_index);
  662. if (instance_count == 1) {
  663. GLES3::MaterialStorage::get_singleton()->shaders.canvas_shader.version_bind_shader(state.current_shader_version, CanvasShaderGLES3::MODE_ATTRIBUTES);
  664. } else if (instance_count > 1) {
  665. GLES3::MaterialStorage::get_singleton()->shaders.canvas_shader.version_bind_shader(state.current_shader_version, CanvasShaderGLES3::MODE_INSTANCED);
  666. } else {
  667. ERR_PRINT("Must have at least one mesh instance to draw mesh");
  668. }
  669. uint32_t surf_count = mesh_storage->mesh_get_surface_count(mesh);
  670. state.instance_data_array[r_index].modulation[0] = base_color.r * modulate.r;
  671. state.instance_data_array[r_index].modulation[1] = base_color.g * modulate.g;
  672. state.instance_data_array[r_index].modulation[2] = base_color.b * modulate.b;
  673. state.instance_data_array[r_index].modulation[3] = base_color.a * modulate.a;
  674. for (int j = 0; j < 4; j++) {
  675. state.instance_data_array[r_index].src_rect[j] = 0;
  676. state.instance_data_array[r_index].dst_rect[j] = 0;
  677. state.instance_data_array[r_index].ninepatch_margins[j] = 0;
  678. }
  679. _bind_instance_data_buffer(1);
  680. for (uint32_t j = 0; j < surf_count; j++) {
  681. void *surface = mesh_storage->mesh_get_surface(mesh, j);
  682. RS::PrimitiveType primitive = mesh_storage->mesh_surface_get_primitive(surface);
  683. ERR_CONTINUE(primitive < 0 || primitive >= RS::PRIMITIVE_MAX);
  684. GLuint vertex_array_gl = 0;
  685. GLuint index_array_gl = 0;
  686. uint32_t input_mask = 0; // 2D meshes always use the same vertex format
  687. if (mesh_instance.is_valid()) {
  688. mesh_storage->mesh_instance_surface_get_vertex_arrays_and_format(mesh_instance, j, input_mask, vertex_array_gl);
  689. } else {
  690. mesh_storage->mesh_surface_get_vertex_arrays_and_format(surface, input_mask, vertex_array_gl);
  691. }
  692. index_array_gl = mesh_storage->mesh_surface_get_index_buffer(surface, 0);
  693. bool use_index_buffer = false;
  694. glBindVertexArray(vertex_array_gl);
  695. if (index_array_gl != 0) {
  696. glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, index_array_gl);
  697. use_index_buffer = true;
  698. }
  699. if (instance_count > 1) {
  700. // Bind instance buffers.
  701. glBindBuffer(GL_ARRAY_BUFFER, multimesh_buffer);
  702. glEnableVertexAttribArray(1);
  703. glVertexAttribPointer(1, 4, GL_FLOAT, GL_FALSE, multimesh_stride * sizeof(float), CAST_INT_TO_UCHAR_PTR(0));
  704. glVertexAttribDivisor(1, 1);
  705. glEnableVertexAttribArray(2);
  706. glVertexAttribPointer(2, 4, GL_FLOAT, GL_FALSE, multimesh_stride * sizeof(float), CAST_INT_TO_UCHAR_PTR(4 * 4));
  707. glVertexAttribDivisor(2, 1);
  708. if (multimesh_uses_color || multimesh_uses_custom_data) {
  709. glEnableVertexAttribArray(5);
  710. glVertexAttribIPointer(5, 4, GL_UNSIGNED_INT, multimesh_stride * sizeof(float), CAST_INT_TO_UCHAR_PTR(multimesh_color_offset * sizeof(float)));
  711. glVertexAttribDivisor(5, 1);
  712. }
  713. }
  714. GLenum primitive_gl = prim[int(primitive)];
  715. if (instance_count == 1) {
  716. if (use_index_buffer) {
  717. glDrawElements(primitive_gl, mesh_storage->mesh_surface_get_vertices_drawn_count(surface), mesh_storage->mesh_surface_get_index_type(surface), 0);
  718. } else {
  719. glDrawArrays(primitive_gl, 0, mesh_storage->mesh_surface_get_vertices_drawn_count(surface));
  720. }
  721. } else if (instance_count > 1) {
  722. if (use_index_buffer) {
  723. glDrawElementsInstanced(primitive_gl, mesh_storage->mesh_surface_get_vertices_drawn_count(surface), mesh_storage->mesh_surface_get_index_type(surface), 0, instance_count);
  724. } else {
  725. glDrawArraysInstanced(primitive_gl, 0, mesh_storage->mesh_surface_get_vertices_drawn_count(surface), instance_count);
  726. }
  727. }
  728. state.fences[state.current_buffer] = glFenceSync(GL_SYNC_GPU_COMMANDS_COMPLETE, 0);
  729. state.current_buffer = (state.current_buffer + 1) % state.canvas_instance_data_buffers.size();
  730. glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0);
  731. if (instance_count > 1) {
  732. glDisableVertexAttribArray(5);
  733. glDisableVertexAttribArray(6);
  734. glDisableVertexAttribArray(7);
  735. glDisableVertexAttribArray(8);
  736. }
  737. }
  738. } break;
  739. case Item::Command::TYPE_TRANSFORM: {
  740. const Item::CommandTransform *transform = static_cast<const Item::CommandTransform *>(c);
  741. draw_transform = transform->xform;
  742. } break;
  743. case Item::Command::TYPE_CLIP_IGNORE: {
  744. const Item::CommandClipIgnore *ci = static_cast<const Item::CommandClipIgnore *>(c);
  745. if (current_clip) {
  746. if (ci->ignore != reclip) {
  747. if (ci->ignore) {
  748. glDisable(GL_SCISSOR_TEST);
  749. reclip = true;
  750. } else {
  751. // Scissor area is already set
  752. glEnable(GL_SCISSOR_TEST);
  753. reclip = false;
  754. }
  755. }
  756. }
  757. } break;
  758. case Item::Command::TYPE_ANIMATION_SLICE: {
  759. /*
  760. const Item::CommandAnimationSlice *as = static_cast<const Item::CommandAnimationSlice *>(c);
  761. double current_time = RendererCompositorRD::singleton->get_total_time();
  762. double local_time = Math::fposmod(current_time - as->offset, as->animation_length);
  763. skipping = !(local_time >= as->slice_begin && local_time < as->slice_end);
  764. RenderingServerDefault::redraw_request(); // animation visible means redraw request
  765. */
  766. } break;
  767. }
  768. c = c->next;
  769. }
  770. if (current_clip && reclip) {
  771. //will make it re-enable clipping if needed afterwards
  772. current_clip = nullptr;
  773. }
  774. }
  775. void RasterizerCanvasGLES3::_render_batch(uint32_t &r_index) {
  776. if (r_index > 0) {
  777. _bind_instance_data_buffer(r_index);
  778. glBindVertexArray(data.canvas_quad_array);
  779. if (state.current_primitive_points == 0) {
  780. glDrawArraysInstanced(GL_TRIANGLE_FAN, 0, 4, r_index);
  781. } else {
  782. static const GLenum prim[5] = { GL_POINTS, GL_POINTS, GL_LINES, GL_TRIANGLES, GL_TRIANGLES };
  783. glDrawArraysInstanced(prim[state.current_primitive_points], 0, state.current_primitive_points, r_index);
  784. }
  785. glBindBuffer(GL_UNIFORM_BUFFER, 0);
  786. state.fences[state.current_buffer] = glFenceSync(GL_SYNC_GPU_COMMANDS_COMPLETE, 0);
  787. state.current_buffer = (state.current_buffer + 1) % state.canvas_instance_data_buffers.size();
  788. //copy the new data into the base of the batch
  789. for (int i = 0; i < 4; i++) {
  790. state.instance_data_array[0].modulation[i] = state.instance_data_array[r_index].modulation[i];
  791. state.instance_data_array[0].ninepatch_margins[i] = state.instance_data_array[r_index].ninepatch_margins[i];
  792. state.instance_data_array[0].src_rect[i] = state.instance_data_array[r_index].src_rect[i];
  793. state.instance_data_array[0].dst_rect[i] = state.instance_data_array[r_index].dst_rect[i];
  794. state.instance_data_array[0].lights[i] = state.instance_data_array[r_index].lights[i];
  795. }
  796. state.instance_data_array[0].flags = state.instance_data_array[r_index].flags;
  797. state.instance_data_array[0].color_texture_pixel_size[0] = state.instance_data_array[r_index].color_texture_pixel_size[0];
  798. state.instance_data_array[0].color_texture_pixel_size[1] = state.instance_data_array[r_index].color_texture_pixel_size[1];
  799. state.instance_data_array[0].pad[0] = state.instance_data_array[r_index].pad[0];
  800. state.instance_data_array[0].pad[1] = state.instance_data_array[r_index].pad[1];
  801. for (int i = 0; i < 6; i++) {
  802. state.instance_data_array[0].world[i] = state.instance_data_array[r_index].world[i];
  803. }
  804. r_index = 0;
  805. }
  806. }
  807. void RasterizerCanvasGLES3::_bind_instance_data_buffer(uint32_t p_max_index) {
  808. if (p_max_index == 0) {
  809. return;
  810. }
  811. // If the previous operation is not done yet, allocate a new buffer
  812. if (state.fences[state.current_buffer] != GLsync()) {
  813. GLint syncStatus;
  814. glGetSynciv(state.fences[state.current_buffer], GL_SYNC_STATUS, sizeof(GLint), nullptr, &syncStatus);
  815. if (syncStatus == GL_UNSIGNALED) {
  816. _allocate_instance_data_buffer();
  817. } else {
  818. glDeleteSync(state.fences[state.current_buffer]);
  819. }
  820. }
  821. glBindBufferBase(GL_UNIFORM_BUFFER, INSTANCE_UNIFORM_LOCATION, state.canvas_instance_data_buffers[state.current_buffer]);
  822. #ifdef JAVASCRIPT_ENABLED
  823. //WebGL 2.0 does not support mapping buffers, so use slow glBufferData instead
  824. glBufferData(GL_UNIFORM_BUFFER, sizeof(InstanceData) * p_max_index, state.instance_data_array, GL_DYNAMIC_DRAW);
  825. #else
  826. void *ubo = glMapBufferRange(GL_UNIFORM_BUFFER, 0, sizeof(InstanceData) * p_max_index, GL_MAP_WRITE_BIT | GL_MAP_UNSYNCHRONIZED_BIT);
  827. memcpy(ubo, state.instance_data_array, sizeof(InstanceData) * p_max_index);
  828. glUnmapBuffer(GL_UNIFORM_BUFFER);
  829. #endif
  830. }
  831. RID RasterizerCanvasGLES3::light_create() {
  832. return RID();
  833. }
  834. void RasterizerCanvasGLES3::light_set_texture(RID p_rid, RID p_texture) {
  835. }
  836. void RasterizerCanvasGLES3::light_set_use_shadow(RID p_rid, bool p_enable) {
  837. }
  838. void RasterizerCanvasGLES3::light_update_shadow(RID p_rid, int p_shadow_index, const Transform2D &p_light_xform, int p_light_mask, float p_near, float p_far, LightOccluderInstance *p_occluders) {
  839. }
  840. void RasterizerCanvasGLES3::light_update_directional_shadow(RID p_rid, int p_shadow_index, const Transform2D &p_light_xform, int p_light_mask, float p_cull_distance, const Rect2 &p_clip_rect, LightOccluderInstance *p_occluders) {
  841. }
  842. void RasterizerCanvasGLES3::render_sdf(RID p_render_target, LightOccluderInstance *p_occluders) {
  843. }
  844. RID RasterizerCanvasGLES3::occluder_polygon_create() {
  845. return RID();
  846. }
  847. void RasterizerCanvasGLES3::occluder_polygon_set_shape(RID p_occluder, const Vector<Vector2> &p_points, bool p_closed) {
  848. }
  849. void RasterizerCanvasGLES3::occluder_polygon_set_cull_mode(RID p_occluder, RS::CanvasOccluderPolygonCullMode p_mode) {
  850. }
  851. void RasterizerCanvasGLES3::set_shadow_texture_size(int p_size) {
  852. }
  853. bool RasterizerCanvasGLES3::free(RID p_rid) {
  854. return true;
  855. }
  856. void RasterizerCanvasGLES3::update() {
  857. }
  858. void RasterizerCanvasGLES3::canvas_begin(RID p_to_render_target, bool p_to_backbuffer) {
  859. GLES3::TextureStorage *texture_storage = GLES3::TextureStorage::get_singleton();
  860. GLES3::Config *config = GLES3::Config::get_singleton();
  861. GLES3::RenderTarget *render_target = texture_storage->get_render_target(p_to_render_target);
  862. if (p_to_backbuffer) {
  863. glBindFramebuffer(GL_FRAMEBUFFER, render_target->backbuffer_fbo);
  864. glActiveTexture(GL_TEXTURE0 + config->max_texture_image_units - 4);
  865. GLES3::Texture *tex = texture_storage->get_texture(texture_storage->texture_gl_get_default(GLES3::DEFAULT_GL_TEXTURE_WHITE));
  866. glBindTexture(GL_TEXTURE_2D, tex->tex_id);
  867. } else {
  868. glBindFramebuffer(GL_FRAMEBUFFER, render_target->fbo);
  869. glActiveTexture(GL_TEXTURE0 + config->max_texture_image_units - 4);
  870. glBindTexture(GL_TEXTURE_2D, render_target->backbuffer);
  871. }
  872. if (render_target->is_transparent) {
  873. state.transparent_render_target = true;
  874. glBlendFuncSeparate(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA, GL_ONE, GL_ONE_MINUS_SRC_ALPHA);
  875. } else {
  876. state.transparent_render_target = false;
  877. glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
  878. }
  879. if (render_target && render_target->clear_requested) {
  880. const Color &col = render_target->clear_color;
  881. glClearColor(col.r, col.g, col.b, col.a);
  882. glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT | GL_STENCIL_BUFFER_BIT);
  883. render_target->clear_requested = false;
  884. }
  885. glActiveTexture(GL_TEXTURE0);
  886. GLES3::Texture *tex = texture_storage->get_texture(texture_storage->texture_gl_get_default(GLES3::DEFAULT_GL_TEXTURE_WHITE));
  887. glBindTexture(GL_TEXTURE_2D, tex->tex_id);
  888. }
  889. void RasterizerCanvasGLES3::_bind_canvas_texture(RID p_texture, RS::CanvasItemTextureFilter p_base_filter, RS::CanvasItemTextureRepeat p_base_repeat, uint32_t &r_index) {
  890. GLES3::TextureStorage *texture_storage = GLES3::TextureStorage::get_singleton();
  891. GLES3::Config *config = GLES3::Config::get_singleton();
  892. if (p_texture == RID()) {
  893. p_texture = texture_storage->texture_gl_get_default(GLES3::DEFAULT_GL_TEXTURE_WHITE);
  894. }
  895. if (state.current_tex == p_texture) {
  896. return; //nothing to do, its the same
  897. }
  898. state.current_tex = p_texture;
  899. GLES3::CanvasTexture *ct = nullptr;
  900. GLES3::Texture *t = texture_storage->get_texture(p_texture);
  901. if (t) {
  902. //regular texture
  903. if (!t->canvas_texture) {
  904. t->canvas_texture = memnew(GLES3::CanvasTexture);
  905. t->canvas_texture->diffuse = p_texture;
  906. }
  907. ct = t->canvas_texture;
  908. } else {
  909. ct = texture_storage->get_canvas_texture(p_texture);
  910. }
  911. if (!ct) {
  912. // Invalid Texture RID.
  913. _bind_canvas_texture(default_canvas_texture, p_base_filter, p_base_repeat, r_index);
  914. return;
  915. }
  916. RS::CanvasItemTextureFilter filter = ct->texture_filter != RS::CANVAS_ITEM_TEXTURE_FILTER_DEFAULT ? ct->texture_filter : p_base_filter;
  917. ERR_FAIL_COND(filter == RS::CANVAS_ITEM_TEXTURE_FILTER_DEFAULT);
  918. RS::CanvasItemTextureRepeat repeat = ct->texture_repeat != RS::CANVAS_ITEM_TEXTURE_REPEAT_DEFAULT ? ct->texture_repeat : p_base_repeat;
  919. ERR_FAIL_COND(repeat == RS::CANVAS_ITEM_TEXTURE_REPEAT_DEFAULT);
  920. GLES3::Texture *texture = texture_storage->get_texture(ct->diffuse);
  921. if (!texture) {
  922. state.current_tex = texture_storage->texture_gl_get_default(GLES3::DEFAULT_GL_TEXTURE_WHITE);
  923. GLES3::Texture *tex = texture_storage->get_texture(state.current_tex);
  924. state.current_tex_ptr = tex;
  925. ct->size_cache = Size2i(tex->width, tex->height);
  926. glActiveTexture(GL_TEXTURE0);
  927. glBindTexture(GL_TEXTURE_2D, tex->tex_id);
  928. } else {
  929. glActiveTexture(GL_TEXTURE0);
  930. glBindTexture(GL_TEXTURE_2D, texture->tex_id);
  931. state.current_tex = p_texture;
  932. state.current_tex_ptr = texture;
  933. ct->size_cache = Size2i(texture->width, texture->height);
  934. texture->gl_set_filter(filter);
  935. texture->gl_set_repeat(repeat);
  936. }
  937. GLES3::Texture *normal_map = texture_storage->get_texture(ct->normal_map);
  938. if (!normal_map) {
  939. state.current_normal = RID();
  940. ct->use_normal_cache = false;
  941. glActiveTexture(GL_TEXTURE0 + GLES3::Config::get_singleton()->max_texture_image_units - 6);
  942. GLES3::Texture *tex = texture_storage->get_texture(texture_storage->texture_gl_get_default(GLES3::DEFAULT_GL_TEXTURE_NORMAL));
  943. glBindTexture(GL_TEXTURE_2D, tex->tex_id);
  944. } else {
  945. glActiveTexture(GL_TEXTURE0 + config->max_texture_image_units - 6);
  946. glBindTexture(GL_TEXTURE_2D, normal_map->tex_id);
  947. state.current_normal = ct->normal_map;
  948. ct->use_normal_cache = true;
  949. texture->gl_set_filter(filter);
  950. texture->gl_set_repeat(repeat);
  951. }
  952. GLES3::Texture *specular_map = texture_storage->get_texture(ct->specular);
  953. if (!specular_map) {
  954. state.current_specular = RID();
  955. ct->use_specular_cache = false;
  956. glActiveTexture(GL_TEXTURE0 + config->max_texture_image_units - 7);
  957. GLES3::Texture *tex = texture_storage->get_texture(texture_storage->texture_gl_get_default(GLES3::DEFAULT_GL_TEXTURE_WHITE));
  958. glBindTexture(GL_TEXTURE_2D, tex->tex_id);
  959. } else {
  960. glActiveTexture(GL_TEXTURE0 + config->max_texture_image_units - 7);
  961. glBindTexture(GL_TEXTURE_2D, specular_map->tex_id);
  962. state.current_specular = ct->specular;
  963. ct->use_specular_cache = true;
  964. texture->gl_set_filter(filter);
  965. texture->gl_set_repeat(repeat);
  966. }
  967. if (ct->use_specular_cache) {
  968. state.instance_data_array[r_index].flags |= FLAGS_DEFAULT_SPECULAR_MAP_USED;
  969. } else {
  970. state.instance_data_array[r_index].flags &= ~FLAGS_DEFAULT_SPECULAR_MAP_USED;
  971. }
  972. if (ct->use_normal_cache) {
  973. state.instance_data_array[r_index].flags |= FLAGS_DEFAULT_NORMAL_MAP_USED;
  974. } else {
  975. state.instance_data_array[r_index].flags &= ~FLAGS_DEFAULT_NORMAL_MAP_USED;
  976. }
  977. state.instance_data_array[r_index].specular_shininess = uint32_t(CLAMP(ct->specular_color.a * 255.0, 0, 255)) << 24;
  978. state.instance_data_array[r_index].specular_shininess |= uint32_t(CLAMP(ct->specular_color.b * 255.0, 0, 255)) << 16;
  979. state.instance_data_array[r_index].specular_shininess |= uint32_t(CLAMP(ct->specular_color.g * 255.0, 0, 255)) << 8;
  980. state.instance_data_array[r_index].specular_shininess |= uint32_t(CLAMP(ct->specular_color.r * 255.0, 0, 255));
  981. state.current_pixel_size.x = 1.0 / float(ct->size_cache.x);
  982. state.current_pixel_size.y = 1.0 / float(ct->size_cache.y);
  983. state.instance_data_array[r_index].color_texture_pixel_size[0] = state.current_pixel_size.x;
  984. state.instance_data_array[r_index].color_texture_pixel_size[1] = state.current_pixel_size.y;
  985. }
  986. void RasterizerCanvasGLES3::reset_canvas() {
  987. glDisable(GL_CULL_FACE);
  988. glDisable(GL_DEPTH_TEST);
  989. glDisable(GL_SCISSOR_TEST);
  990. glEnable(GL_BLEND);
  991. glBlendFuncSeparate(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA, GL_ZERO, GL_ONE);
  992. glBindBuffer(GL_ARRAY_BUFFER, 0);
  993. glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0);
  994. }
  995. void RasterizerCanvasGLES3::canvas_debug_viewport_shadows(Light *p_lights_with_shadow) {
  996. }
  997. void RasterizerCanvasGLES3::canvas_light_shadow_buffer_update(RID p_buffer, const Transform2D &p_light_xform, int p_light_mask, float p_near, float p_far, LightOccluderInstance *p_occluders, Projection *p_xform_cache) {
  998. }
  999. void RasterizerCanvasGLES3::draw_lens_distortion_rect(const Rect2 &p_rect, float p_k1, float p_k2, const Vector2 &p_eye_center, float p_oversample) {
  1000. }
  1001. RendererCanvasRender::PolygonID RasterizerCanvasGLES3::request_polygon(const Vector<int> &p_indices, const Vector<Point2> &p_points, const Vector<Color> &p_colors, const Vector<Point2> &p_uvs, const Vector<int> &p_bones, const Vector<float> &p_weights) {
  1002. // We interleave the vertex data into one big VBO to improve cache coherence
  1003. uint32_t vertex_count = p_points.size();
  1004. uint32_t stride = 2;
  1005. if ((uint32_t)p_colors.size() == vertex_count) {
  1006. stride += 4;
  1007. }
  1008. if ((uint32_t)p_uvs.size() == vertex_count) {
  1009. stride += 2;
  1010. }
  1011. if ((uint32_t)p_bones.size() == vertex_count * 4 && (uint32_t)p_weights.size() == vertex_count * 4) {
  1012. stride += 4;
  1013. }
  1014. PolygonBuffers pb;
  1015. glGenBuffers(1, &pb.vertex_buffer);
  1016. glGenVertexArrays(1, &pb.vertex_array);
  1017. glBindVertexArray(pb.vertex_array);
  1018. pb.count = vertex_count;
  1019. pb.index_buffer = 0;
  1020. uint32_t buffer_size = stride * p_points.size();
  1021. Vector<uint8_t> polygon_buffer;
  1022. polygon_buffer.resize(buffer_size * sizeof(float));
  1023. {
  1024. glBindBuffer(GL_ARRAY_BUFFER, pb.vertex_buffer);
  1025. glBufferData(GL_ARRAY_BUFFER, stride * vertex_count * sizeof(float), nullptr, GL_STATIC_DRAW); // TODO may not be necessary
  1026. uint8_t *r = polygon_buffer.ptrw();
  1027. float *fptr = reinterpret_cast<float *>(r);
  1028. uint32_t *uptr = (uint32_t *)r;
  1029. uint32_t base_offset = 0;
  1030. {
  1031. // Always uses vertex positions
  1032. glEnableVertexAttribArray(RS::ARRAY_VERTEX);
  1033. glVertexAttribPointer(RS::ARRAY_VERTEX, 2, GL_FLOAT, GL_FALSE, stride * sizeof(float), nullptr);
  1034. const Vector2 *points_ptr = p_points.ptr();
  1035. for (uint32_t i = 0; i < vertex_count; i++) {
  1036. fptr[base_offset + i * stride + 0] = points_ptr[i].x;
  1037. fptr[base_offset + i * stride + 1] = points_ptr[i].y;
  1038. }
  1039. base_offset += 2;
  1040. }
  1041. // Next add colors
  1042. if ((uint32_t)p_colors.size() == vertex_count) {
  1043. glEnableVertexAttribArray(RS::ARRAY_COLOR);
  1044. glVertexAttribPointer(RS::ARRAY_COLOR, 4, GL_FLOAT, GL_FALSE, stride * sizeof(float), CAST_INT_TO_UCHAR_PTR(base_offset * sizeof(float)));
  1045. const Color *color_ptr = p_colors.ptr();
  1046. for (uint32_t i = 0; i < vertex_count; i++) {
  1047. fptr[base_offset + i * stride + 0] = color_ptr[i].r;
  1048. fptr[base_offset + i * stride + 1] = color_ptr[i].g;
  1049. fptr[base_offset + i * stride + 2] = color_ptr[i].b;
  1050. fptr[base_offset + i * stride + 3] = color_ptr[i].a;
  1051. }
  1052. base_offset += 4;
  1053. } else {
  1054. glDisableVertexAttribArray(RS::ARRAY_COLOR);
  1055. pb.color_disabled = true;
  1056. pb.color = p_colors.size() == 1 ? p_colors[0] : Color(1.0, 1.0, 1.0, 1.0);
  1057. }
  1058. if ((uint32_t)p_uvs.size() == vertex_count) {
  1059. glEnableVertexAttribArray(RS::ARRAY_TEX_UV);
  1060. glVertexAttribPointer(RS::ARRAY_TEX_UV, 2, GL_FLOAT, GL_FALSE, stride * sizeof(float), CAST_INT_TO_UCHAR_PTR(base_offset * sizeof(float)));
  1061. const Vector2 *uv_ptr = p_uvs.ptr();
  1062. for (uint32_t i = 0; i < vertex_count; i++) {
  1063. fptr[base_offset + i * stride + 0] = uv_ptr[i].x;
  1064. fptr[base_offset + i * stride + 1] = uv_ptr[i].y;
  1065. }
  1066. base_offset += 2;
  1067. } else {
  1068. glDisableVertexAttribArray(RS::ARRAY_TEX_UV);
  1069. }
  1070. if ((uint32_t)p_indices.size() == vertex_count * 4 && (uint32_t)p_weights.size() == vertex_count * 4) {
  1071. glEnableVertexAttribArray(RS::ARRAY_BONES);
  1072. glVertexAttribPointer(RS::ARRAY_BONES, 4, GL_UNSIGNED_INT, GL_FALSE, stride * sizeof(float), CAST_INT_TO_UCHAR_PTR(base_offset * sizeof(float)));
  1073. const int *bone_ptr = p_bones.ptr();
  1074. for (uint32_t i = 0; i < vertex_count; i++) {
  1075. uint16_t *bone16w = (uint16_t *)&uptr[base_offset + i * stride];
  1076. bone16w[0] = bone_ptr[i * 4 + 0];
  1077. bone16w[1] = bone_ptr[i * 4 + 1];
  1078. bone16w[2] = bone_ptr[i * 4 + 2];
  1079. bone16w[3] = bone_ptr[i * 4 + 3];
  1080. }
  1081. base_offset += 2;
  1082. } else {
  1083. glDisableVertexAttribArray(RS::ARRAY_BONES);
  1084. }
  1085. if ((uint32_t)p_weights.size() == vertex_count * 4) {
  1086. glEnableVertexAttribArray(RS::ARRAY_WEIGHTS);
  1087. glVertexAttribPointer(RS::ARRAY_WEIGHTS, 4, GL_FLOAT, GL_FALSE, stride * sizeof(float), CAST_INT_TO_UCHAR_PTR(base_offset * sizeof(float)));
  1088. const float *weight_ptr = p_weights.ptr();
  1089. for (uint32_t i = 0; i < vertex_count; i++) {
  1090. uint16_t *weight16w = (uint16_t *)&uptr[base_offset + i * stride];
  1091. weight16w[0] = CLAMP(weight_ptr[i * 4 + 0] * 65535, 0, 65535);
  1092. weight16w[1] = CLAMP(weight_ptr[i * 4 + 1] * 65535, 0, 65535);
  1093. weight16w[2] = CLAMP(weight_ptr[i * 4 + 2] * 65535, 0, 65535);
  1094. weight16w[3] = CLAMP(weight_ptr[i * 4 + 3] * 65535, 0, 65535);
  1095. }
  1096. base_offset += 2;
  1097. } else {
  1098. glDisableVertexAttribArray(RS::ARRAY_WEIGHTS);
  1099. }
  1100. ERR_FAIL_COND_V(base_offset != stride, 0);
  1101. glBufferData(GL_ARRAY_BUFFER, vertex_count * stride * sizeof(float), polygon_buffer.ptr(), GL_STATIC_DRAW);
  1102. }
  1103. if (p_indices.size()) {
  1104. //create indices, as indices were requested
  1105. Vector<uint8_t> index_buffer;
  1106. index_buffer.resize(p_indices.size() * sizeof(int32_t));
  1107. {
  1108. uint8_t *w = index_buffer.ptrw();
  1109. memcpy(w, p_indices.ptr(), sizeof(int32_t) * p_indices.size());
  1110. }
  1111. glGenBuffers(1, &pb.index_buffer);
  1112. glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, pb.index_buffer);
  1113. glBufferData(GL_ELEMENT_ARRAY_BUFFER, p_indices.size() * 4, nullptr, GL_STATIC_DRAW); // TODO may not be necessary
  1114. glBufferData(GL_ELEMENT_ARRAY_BUFFER, p_indices.size() * 4, index_buffer.ptr(), GL_STATIC_DRAW);
  1115. pb.count = p_indices.size();
  1116. }
  1117. glBindVertexArray(0);
  1118. glBindBuffer(GL_ARRAY_BUFFER, 0);
  1119. glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0);
  1120. PolygonID id = polygon_buffers.last_id++;
  1121. polygon_buffers.polygons[id] = pb;
  1122. return id;
  1123. }
  1124. void RasterizerCanvasGLES3::free_polygon(PolygonID p_polygon) {
  1125. PolygonBuffers *pb_ptr = polygon_buffers.polygons.getptr(p_polygon);
  1126. ERR_FAIL_COND(!pb_ptr);
  1127. PolygonBuffers &pb = *pb_ptr;
  1128. if (pb.index_buffer != 0) {
  1129. glDeleteBuffers(1, &pb.index_buffer);
  1130. }
  1131. glDeleteVertexArrays(1, &pb.vertex_array);
  1132. glDeleteBuffers(1, &pb.vertex_buffer);
  1133. polygon_buffers.polygons.erase(p_polygon);
  1134. }
  1135. // Creates a new uniform buffer and uses it right away
  1136. // This expands the instance buffer continually
  1137. // In theory allocations can reach as high as number_of_draw_calls * 3 frames
  1138. // because OpenGL can start rendering subsequent frames before finishing the current one
  1139. void RasterizerCanvasGLES3::_allocate_instance_data_buffer() {
  1140. GLuint new_buffer;
  1141. glGenBuffers(1, &new_buffer);
  1142. glBindBuffer(GL_UNIFORM_BUFFER, new_buffer);
  1143. glBufferData(GL_UNIFORM_BUFFER, sizeof(InstanceData) * state.max_instances_per_batch, nullptr, GL_DYNAMIC_DRAW);
  1144. state.current_buffer = (state.current_buffer + 1);
  1145. state.canvas_instance_data_buffers.insert(state.current_buffer, new_buffer);
  1146. state.fences.insert(state.current_buffer, GLsync());
  1147. state.current_buffer = state.current_buffer % state.canvas_instance_data_buffers.size();
  1148. glBindBuffer(GL_UNIFORM_BUFFER, 0);
  1149. }
  1150. void RasterizerCanvasGLES3::set_time(double p_time) {
  1151. state.time = p_time;
  1152. }
  1153. RasterizerCanvasGLES3 *RasterizerCanvasGLES3::singleton = nullptr;
  1154. RasterizerCanvasGLES3 *RasterizerCanvasGLES3::get_singleton() {
  1155. return singleton;
  1156. }
  1157. RasterizerCanvasGLES3::RasterizerCanvasGLES3() {
  1158. singleton = this;
  1159. GLES3::MaterialStorage *material_storage = GLES3::MaterialStorage::get_singleton();
  1160. GLES3::Config *config = GLES3::Config::get_singleton();
  1161. // quad buffer
  1162. {
  1163. glGenBuffers(1, &data.canvas_quad_vertices);
  1164. glBindBuffer(GL_ARRAY_BUFFER, data.canvas_quad_vertices);
  1165. const float qv[8] = {
  1166. 0, 0,
  1167. 0, 1,
  1168. 1, 1,
  1169. 1, 0
  1170. };
  1171. glBufferData(GL_ARRAY_BUFFER, sizeof(float) * 8, qv, GL_STATIC_DRAW);
  1172. glBindBuffer(GL_ARRAY_BUFFER, 0);
  1173. glGenVertexArrays(1, &data.canvas_quad_array);
  1174. glBindVertexArray(data.canvas_quad_array);
  1175. glBindBuffer(GL_ARRAY_BUFFER, data.canvas_quad_vertices);
  1176. glVertexAttribPointer(0, 2, GL_FLOAT, GL_FALSE, sizeof(float) * 2, nullptr);
  1177. glEnableVertexAttribArray(0);
  1178. glBindVertexArray(0);
  1179. glBindBuffer(GL_ARRAY_BUFFER, 0); //unbind
  1180. }
  1181. {
  1182. //particle quad buffers
  1183. glGenBuffers(1, &data.particle_quad_vertices);
  1184. glBindBuffer(GL_ARRAY_BUFFER, data.particle_quad_vertices);
  1185. {
  1186. //quad of size 1, with pivot on the center for particles, then regular UVS. Color is general plus fetched from particle
  1187. const float qv[16] = {
  1188. -0.5, -0.5,
  1189. 0.0, 0.0,
  1190. -0.5, 0.5,
  1191. 0.0, 1.0,
  1192. 0.5, 0.5,
  1193. 1.0, 1.0,
  1194. 0.5, -0.5,
  1195. 1.0, 0.0
  1196. };
  1197. glBufferData(GL_ARRAY_BUFFER, sizeof(float) * 16, qv, GL_STATIC_DRAW);
  1198. }
  1199. glBindBuffer(GL_ARRAY_BUFFER, 0); //unbind
  1200. glGenVertexArrays(1, &data.particle_quad_array);
  1201. glBindVertexArray(data.particle_quad_array);
  1202. glBindBuffer(GL_ARRAY_BUFFER, data.particle_quad_vertices);
  1203. glEnableVertexAttribArray(RS::ARRAY_VERTEX);
  1204. glVertexAttribPointer(RS::ARRAY_VERTEX, 2, GL_FLOAT, GL_FALSE, sizeof(float) * 4, nullptr);
  1205. glEnableVertexAttribArray(RS::ARRAY_TEX_UV);
  1206. glVertexAttribPointer(RS::ARRAY_TEX_UV, 2, GL_FLOAT, GL_FALSE, sizeof(float) * 4, CAST_INT_TO_UCHAR_PTR(8));
  1207. glBindVertexArray(0);
  1208. glBindBuffer(GL_ARRAY_BUFFER, 0); //unbind
  1209. }
  1210. // ninepatch buffers
  1211. {
  1212. // array buffer
  1213. glGenBuffers(1, &data.ninepatch_vertices);
  1214. glBindBuffer(GL_ARRAY_BUFFER, data.ninepatch_vertices);
  1215. glBufferData(GL_ARRAY_BUFFER, sizeof(float) * (16 + 16) * 2, nullptr, GL_DYNAMIC_DRAW);
  1216. glBindBuffer(GL_ARRAY_BUFFER, 0);
  1217. // element buffer
  1218. glGenBuffers(1, &data.ninepatch_elements);
  1219. glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, data.ninepatch_elements);
  1220. #define _EIDX(y, x) (y * 4 + x)
  1221. uint8_t elems[3 * 2 * 9] = {
  1222. // first row
  1223. _EIDX(0, 0), _EIDX(0, 1), _EIDX(1, 1),
  1224. _EIDX(1, 1), _EIDX(1, 0), _EIDX(0, 0),
  1225. _EIDX(0, 1), _EIDX(0, 2), _EIDX(1, 2),
  1226. _EIDX(1, 2), _EIDX(1, 1), _EIDX(0, 1),
  1227. _EIDX(0, 2), _EIDX(0, 3), _EIDX(1, 3),
  1228. _EIDX(1, 3), _EIDX(1, 2), _EIDX(0, 2),
  1229. // second row
  1230. _EIDX(1, 0), _EIDX(1, 1), _EIDX(2, 1),
  1231. _EIDX(2, 1), _EIDX(2, 0), _EIDX(1, 0),
  1232. // the center one would be here, but we'll put it at the end
  1233. // so it's easier to disable the center and be able to use
  1234. // one draw call for both
  1235. _EIDX(1, 2), _EIDX(1, 3), _EIDX(2, 3),
  1236. _EIDX(2, 3), _EIDX(2, 2), _EIDX(1, 2),
  1237. // third row
  1238. _EIDX(2, 0), _EIDX(2, 1), _EIDX(3, 1),
  1239. _EIDX(3, 1), _EIDX(3, 0), _EIDX(2, 0),
  1240. _EIDX(2, 1), _EIDX(2, 2), _EIDX(3, 2),
  1241. _EIDX(3, 2), _EIDX(3, 1), _EIDX(2, 1),
  1242. _EIDX(2, 2), _EIDX(2, 3), _EIDX(3, 3),
  1243. _EIDX(3, 3), _EIDX(3, 2), _EIDX(2, 2),
  1244. // center field
  1245. _EIDX(1, 1), _EIDX(1, 2), _EIDX(2, 2),
  1246. _EIDX(2, 2), _EIDX(2, 1), _EIDX(1, 1)
  1247. };
  1248. #undef _EIDX
  1249. glBufferData(GL_ELEMENT_ARRAY_BUFFER, sizeof(elems), elems, GL_STATIC_DRAW);
  1250. glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0);
  1251. }
  1252. int uniform_max_size = config->max_uniform_buffer_size;
  1253. if (uniform_max_size < 65536) {
  1254. state.max_lights_per_render = 64;
  1255. state.max_instances_per_batch = 128;
  1256. } else {
  1257. state.max_lights_per_render = 256;
  1258. state.max_instances_per_batch = 512;
  1259. }
  1260. // Reserve 64 Uniform Buffers for instance data
  1261. state.canvas_instance_data_buffers.resize(64);
  1262. state.fences.resize(64);
  1263. glGenBuffers(64, state.canvas_instance_data_buffers.ptr());
  1264. for (int i = 0; i < 64; i++) {
  1265. state.fences[i] = GLsync();
  1266. glBindBuffer(GL_UNIFORM_BUFFER, state.canvas_instance_data_buffers[i]);
  1267. glBufferData(GL_UNIFORM_BUFFER, sizeof(InstanceData) * state.max_instances_per_batch, nullptr, GL_DYNAMIC_DRAW);
  1268. }
  1269. glBindBuffer(GL_UNIFORM_BUFFER, 0);
  1270. state.instance_data_array = memnew_arr(InstanceData, state.max_instances_per_batch);
  1271. glGenBuffers(1, &state.canvas_state_buffer);
  1272. glBindBuffer(GL_UNIFORM_BUFFER, state.canvas_state_buffer);
  1273. glBufferData(GL_UNIFORM_BUFFER, sizeof(StateBuffer), nullptr, GL_STREAM_DRAW);
  1274. glBindBuffer(GL_UNIFORM_BUFFER, 0);
  1275. String global_defines;
  1276. global_defines += "#define MAX_GLOBAL_VARIABLES 256\n"; // TODO: this is arbitrary for now
  1277. global_defines += "#define MAX_LIGHTS " + itos(state.max_instances_per_batch) + "\n";
  1278. global_defines += "#define MAX_DRAW_DATA_INSTANCES " + itos(state.max_instances_per_batch) + "\n";
  1279. GLES3::MaterialStorage::get_singleton()->shaders.canvas_shader.initialize(global_defines);
  1280. state.canvas_shader_default_version = GLES3::MaterialStorage::get_singleton()->shaders.canvas_shader.version_create();
  1281. GLES3::MaterialStorage::get_singleton()->shaders.canvas_shader.version_bind_shader(state.canvas_shader_default_version, CanvasShaderGLES3::MODE_QUAD);
  1282. {
  1283. default_canvas_group_shader = material_storage->shader_allocate();
  1284. material_storage->shader_initialize(default_canvas_group_shader);
  1285. material_storage->shader_set_code(default_canvas_group_shader, R"(
  1286. // Default CanvasGroup shader.
  1287. shader_type canvas_item;
  1288. void fragment() {
  1289. vec4 c = textureLod(SCREEN_TEXTURE, SCREEN_UV, 0.0);
  1290. if (c.a > 0.0001) {
  1291. c.rgb /= c.a;
  1292. }
  1293. COLOR *= c;
  1294. }
  1295. )");
  1296. default_canvas_group_material = material_storage->material_allocate();
  1297. material_storage->material_initialize(default_canvas_group_material);
  1298. material_storage->material_set_shader(default_canvas_group_material, default_canvas_group_shader);
  1299. }
  1300. state.current_shader_version = state.canvas_shader_default_version;
  1301. state.time = 0.0;
  1302. }
  1303. RasterizerCanvasGLES3::~RasterizerCanvasGLES3() {
  1304. GLES3::MaterialStorage *material_storage = GLES3::MaterialStorage::get_singleton();
  1305. GLES3::MaterialStorage::get_singleton()->shaders.canvas_shader.version_free(state.canvas_shader_default_version);
  1306. material_storage->material_free(default_canvas_group_material);
  1307. material_storage->shader_free(default_canvas_group_shader);
  1308. singleton = nullptr;
  1309. glDeleteBuffers(1, &data.canvas_quad_vertices);
  1310. glDeleteVertexArrays(1, &data.canvas_quad_array);
  1311. glDeleteBuffers(1, &data.canvas_quad_vertices);
  1312. glDeleteVertexArrays(1, &data.canvas_quad_array);
  1313. }
  1314. #endif // GLES3_ENABLED