1/**************************************************************************/
2/* rasterizer_canvas_gles3.cpp */
3/**************************************************************************/
4/* This file is part of: */
5/* GODOT ENGINE */
6/* https://godotengine.org */
7/**************************************************************************/
8/* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */
9/* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */
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
31#include "rasterizer_canvas_gles3.h"
32
33#ifdef GLES3_ENABLED
34
35#include "core/os/os.h"
36#include "rasterizer_scene_gles3.h"
37
38#include "core/config/project_settings.h"
39#include "core/math/geometry_2d.h"
40#include "servers/rendering/rendering_server_default.h"
41#include "storage/config.h"
42#include "storage/material_storage.h"
43#include "storage/mesh_storage.h"
44#include "storage/particles_storage.h"
45#include "storage/texture_storage.h"
46
47void RasterizerCanvasGLES3::_update_transform_2d_to_mat4(const Transform2D &p_transform, float *p_mat4) {
48 p_mat4[0] = p_transform.columns[0][0];
49 p_mat4[1] = p_transform.columns[0][1];
50 p_mat4[2] = 0;
51 p_mat4[3] = 0;
52 p_mat4[4] = p_transform.columns[1][0];
53 p_mat4[5] = p_transform.columns[1][1];
54 p_mat4[6] = 0;
55 p_mat4[7] = 0;
56 p_mat4[8] = 0;
57 p_mat4[9] = 0;
58 p_mat4[10] = 1;
59 p_mat4[11] = 0;
60 p_mat4[12] = p_transform.columns[2][0];
61 p_mat4[13] = p_transform.columns[2][1];
62 p_mat4[14] = 0;
63 p_mat4[15] = 1;
64}
65
66void RasterizerCanvasGLES3::_update_transform_2d_to_mat2x4(const Transform2D &p_transform, float *p_mat2x4) {
67 p_mat2x4[0] = p_transform.columns[0][0];
68 p_mat2x4[1] = p_transform.columns[1][0];
69 p_mat2x4[2] = 0;
70 p_mat2x4[3] = p_transform.columns[2][0];
71
72 p_mat2x4[4] = p_transform.columns[0][1];
73 p_mat2x4[5] = p_transform.columns[1][1];
74 p_mat2x4[6] = 0;
75 p_mat2x4[7] = p_transform.columns[2][1];
76}
77
78void RasterizerCanvasGLES3::_update_transform_2d_to_mat2x3(const Transform2D &p_transform, float *p_mat2x3) {
79 p_mat2x3[0] = p_transform.columns[0][0];
80 p_mat2x3[1] = p_transform.columns[0][1];
81 p_mat2x3[2] = p_transform.columns[1][0];
82 p_mat2x3[3] = p_transform.columns[1][1];
83 p_mat2x3[4] = p_transform.columns[2][0];
84 p_mat2x3[5] = p_transform.columns[2][1];
85}
86
87void RasterizerCanvasGLES3::_update_transform_to_mat4(const Transform3D &p_transform, float *p_mat4) {
88 p_mat4[0] = p_transform.basis.rows[0][0];
89 p_mat4[1] = p_transform.basis.rows[1][0];
90 p_mat4[2] = p_transform.basis.rows[2][0];
91 p_mat4[3] = 0;
92 p_mat4[4] = p_transform.basis.rows[0][1];
93 p_mat4[5] = p_transform.basis.rows[1][1];
94 p_mat4[6] = p_transform.basis.rows[2][1];
95 p_mat4[7] = 0;
96 p_mat4[8] = p_transform.basis.rows[0][2];
97 p_mat4[9] = p_transform.basis.rows[1][2];
98 p_mat4[10] = p_transform.basis.rows[2][2];
99 p_mat4[11] = 0;
100 p_mat4[12] = p_transform.origin.x;
101 p_mat4[13] = p_transform.origin.y;
102 p_mat4[14] = p_transform.origin.z;
103 p_mat4[15] = 1;
104}
105
106void RasterizerCanvasGLES3::canvas_render_items(RID p_to_render_target, Item *p_item_list, const Color &p_modulate, Light *p_light_list, Light *p_directional_light_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 GLES3::MeshStorage *mesh_storage = GLES3::MeshStorage::get_singleton();
110
111 Transform2D canvas_transform_inverse = p_canvas_transform.affine_inverse();
112
113 // Clear out any state that may have been left from the 3D pass.
114 reset_canvas();
115
116 if (state.canvas_instance_data_buffers[state.current_data_buffer_index].fence != GLsync()) {
117 GLint syncStatus;
118 glGetSynciv(state.canvas_instance_data_buffers[state.current_data_buffer_index].fence, GL_SYNC_STATUS, 1, nullptr, &syncStatus);
119 if (syncStatus == GL_UNSIGNALED) {
120 // If older than 2 frames, wait for sync OpenGL can have up to 3 frames in flight, any more and we need to sync anyway.
121 if (state.canvas_instance_data_buffers[state.current_data_buffer_index].last_frame_used < RSG::rasterizer->get_frame_number() - 2) {
122#ifndef WEB_ENABLED
123 // On web, we do nothing as the glSubBufferData will force a sync anyway and WebGL does not like waiting.
124 glClientWaitSync(state.canvas_instance_data_buffers[state.current_data_buffer_index].fence, 0, 100000000); // wait for up to 100ms
125#endif
126 state.canvas_instance_data_buffers[state.current_data_buffer_index].last_frame_used = RSG::rasterizer->get_frame_number();
127 glDeleteSync(state.canvas_instance_data_buffers[state.current_data_buffer_index].fence);
128 state.canvas_instance_data_buffers[state.current_data_buffer_index].fence = GLsync();
129 } else {
130 // Used in last frame or frame before that. OpenGL can get up to two frames behind, so these buffers may still be in use
131 // Allocate a new buffer and use that.
132 _allocate_instance_data_buffer();
133 }
134 } else {
135 // Already finished all rendering commands, we can use it.
136 state.canvas_instance_data_buffers[state.current_data_buffer_index].last_frame_used = RSG::rasterizer->get_frame_number();
137 glDeleteSync(state.canvas_instance_data_buffers[state.current_data_buffer_index].fence);
138 state.canvas_instance_data_buffers[state.current_data_buffer_index].fence = GLsync();
139 }
140 }
141
142 //setup directional lights if exist
143
144 uint32_t light_count = 0;
145 uint32_t directional_light_count = 0;
146 {
147 Light *l = p_directional_light_list;
148 uint32_t index = 0;
149
150 while (l) {
151 if (index == data.max_lights_per_render) {
152 l->render_index_cache = -1;
153 l = l->next_ptr;
154 continue;
155 }
156
157 CanvasLight *clight = canvas_light_owner.get_or_null(l->light_internal);
158 if (!clight) { //unused or invalid texture
159 l->render_index_cache = -1;
160 l = l->next_ptr;
161 ERR_CONTINUE(!clight);
162 }
163
164 Vector2 canvas_light_dir = l->xform_cache.columns[1].normalized();
165
166 state.light_uniforms[index].position[0] = -canvas_light_dir.x;
167 state.light_uniforms[index].position[1] = -canvas_light_dir.y;
168
169 _update_transform_2d_to_mat2x4(clight->shadow.directional_xform, state.light_uniforms[index].shadow_matrix);
170
171 state.light_uniforms[index].height = l->height; //0..1 here
172
173 for (int i = 0; i < 4; i++) {
174 state.light_uniforms[index].shadow_color[i] = uint8_t(CLAMP(int32_t(l->shadow_color[i] * 255.0), 0, 255));
175 state.light_uniforms[index].color[i] = l->color[i];
176 }
177
178 state.light_uniforms[index].color[3] *= l->energy; //use alpha for energy, so base color can go separate
179
180 if (state.shadow_fb != 0) {
181 state.light_uniforms[index].shadow_pixel_size = (1.0 / state.shadow_texture_size) * (1.0 + l->shadow_smooth);
182 state.light_uniforms[index].shadow_z_far_inv = 1.0 / clight->shadow.z_far;
183 state.light_uniforms[index].shadow_y_ofs = clight->shadow.y_offset;
184 } else {
185 state.light_uniforms[index].shadow_pixel_size = 1.0;
186 state.light_uniforms[index].shadow_z_far_inv = 1.0;
187 state.light_uniforms[index].shadow_y_ofs = 0;
188 }
189
190 state.light_uniforms[index].flags = l->blend_mode << LIGHT_FLAGS_BLEND_SHIFT;
191 state.light_uniforms[index].flags |= l->shadow_filter << LIGHT_FLAGS_FILTER_SHIFT;
192
193 if (clight->shadow.enabled) {
194 state.light_uniforms[index].flags |= LIGHT_FLAGS_HAS_SHADOW;
195 }
196
197 l->render_index_cache = index;
198
199 index++;
200 l = l->next_ptr;
201 }
202
203 light_count = index;
204 directional_light_count = light_count;
205 state.using_directional_lights = directional_light_count > 0;
206 }
207
208 //setup lights if exist
209
210 {
211 Light *l = p_light_list;
212 uint32_t index = light_count;
213
214 while (l) {
215 if (index == data.max_lights_per_render) {
216 l->render_index_cache = -1;
217 l = l->next_ptr;
218 continue;
219 }
220
221 CanvasLight *clight = canvas_light_owner.get_or_null(l->light_internal);
222 if (!clight) { //unused or invalid texture
223 l->render_index_cache = -1;
224 l = l->next_ptr;
225 ERR_CONTINUE(!clight);
226 }
227
228 Vector2 canvas_light_pos = p_canvas_transform.xform(l->xform.get_origin()); //convert light position to canvas coordinates, as all computation is done in canvas coords to avoid precision loss
229 state.light_uniforms[index].position[0] = canvas_light_pos.x;
230 state.light_uniforms[index].position[1] = canvas_light_pos.y;
231
232 _update_transform_2d_to_mat2x4(l->light_shader_xform.affine_inverse(), state.light_uniforms[index].matrix);
233 _update_transform_2d_to_mat2x4(l->xform_cache.affine_inverse(), state.light_uniforms[index].shadow_matrix);
234
235 state.light_uniforms[index].height = l->height * (p_canvas_transform.columns[0].length() + p_canvas_transform.columns[1].length()) * 0.5; //approximate height conversion to the canvas size, since all calculations are done in canvas coords to avoid precision loss
236 for (int i = 0; i < 4; i++) {
237 state.light_uniforms[index].shadow_color[i] = uint8_t(CLAMP(int32_t(l->shadow_color[i] * 255.0), 0, 255));
238 state.light_uniforms[index].color[i] = l->color[i];
239 }
240
241 state.light_uniforms[index].color[3] *= l->energy; //use alpha for energy, so base color can go separate
242
243 if (state.shadow_fb != 0) {
244 state.light_uniforms[index].shadow_pixel_size = (1.0 / state.shadow_texture_size) * (1.0 + l->shadow_smooth);
245 state.light_uniforms[index].shadow_z_far_inv = 1.0 / clight->shadow.z_far;
246 state.light_uniforms[index].shadow_y_ofs = clight->shadow.y_offset;
247 } else {
248 state.light_uniforms[index].shadow_pixel_size = 1.0;
249 state.light_uniforms[index].shadow_z_far_inv = 1.0;
250 state.light_uniforms[index].shadow_y_ofs = 0;
251 }
252
253 state.light_uniforms[index].flags = l->blend_mode << LIGHT_FLAGS_BLEND_SHIFT;
254 state.light_uniforms[index].flags |= l->shadow_filter << LIGHT_FLAGS_FILTER_SHIFT;
255
256 if (clight->shadow.enabled) {
257 state.light_uniforms[index].flags |= LIGHT_FLAGS_HAS_SHADOW;
258 }
259
260 if (clight->texture.is_valid()) {
261 Rect2 atlas_rect = GLES3::TextureStorage::get_singleton()->texture_atlas_get_texture_rect(clight->texture);
262 state.light_uniforms[index].atlas_rect[0] = atlas_rect.position.x;
263 state.light_uniforms[index].atlas_rect[1] = atlas_rect.position.y;
264 state.light_uniforms[index].atlas_rect[2] = atlas_rect.size.width;
265 state.light_uniforms[index].atlas_rect[3] = atlas_rect.size.height;
266
267 } else {
268 state.light_uniforms[index].atlas_rect[0] = 0;
269 state.light_uniforms[index].atlas_rect[1] = 0;
270 state.light_uniforms[index].atlas_rect[2] = 0;
271 state.light_uniforms[index].atlas_rect[3] = 0;
272 }
273
274 l->render_index_cache = index;
275
276 index++;
277 l = l->next_ptr;
278 }
279
280 light_count = index;
281 }
282
283 if (light_count > 0) {
284 glBindBufferBase(GL_UNIFORM_BUFFER, LIGHT_UNIFORM_LOCATION, state.canvas_instance_data_buffers[state.current_data_buffer_index].light_ubo);
285
286#ifdef WEB_ENABLED
287 glBufferSubData(GL_UNIFORM_BUFFER, 0, sizeof(LightUniform) * light_count, state.light_uniforms);
288#else
289 // On Desktop and mobile we map the memory without synchronizing for maximum speed.
290 void *ubo = glMapBufferRange(GL_UNIFORM_BUFFER, 0, sizeof(LightUniform) * light_count, GL_MAP_WRITE_BIT | GL_MAP_UNSYNCHRONIZED_BIT);
291 memcpy(ubo, state.light_uniforms, sizeof(LightUniform) * light_count);
292 glUnmapBuffer(GL_UNIFORM_BUFFER);
293#endif
294
295 GLuint texture_atlas = texture_storage->texture_atlas_get_texture();
296 if (texture_atlas == 0) {
297 GLES3::Texture *tex = texture_storage->get_texture(texture_storage->texture_gl_get_default(GLES3::DEFAULT_GL_TEXTURE_WHITE));
298 texture_atlas = tex->tex_id;
299 }
300 glActiveTexture(GL_TEXTURE0 + GLES3::Config::get_singleton()->max_texture_image_units - 2);
301 glBindTexture(GL_TEXTURE_2D, texture_atlas);
302 GLuint shadow_tex = state.shadow_texture;
303 if (shadow_tex == 0) {
304 GLES3::Texture *tex = texture_storage->get_texture(texture_storage->texture_gl_get_default(GLES3::DEFAULT_GL_TEXTURE_WHITE));
305 shadow_tex = tex->tex_id;
306 }
307 glActiveTexture(GL_TEXTURE0 + GLES3::Config::get_singleton()->max_texture_image_units - 3);
308 glBindTexture(GL_TEXTURE_2D, shadow_tex);
309 }
310
311 {
312 //update canvas state uniform buffer
313 StateBuffer state_buffer;
314
315 Size2i ssize = texture_storage->render_target_get_size(p_to_render_target);
316
317 // If we've overridden the render target's color texture, then we need
318 // to invert the Y axis, so 2D texture appear right side up.
319 // We're probably rendering directly to an XR device.
320 float y_scale = texture_storage->render_target_get_override_color(p_to_render_target).is_valid() ? -2.0f : 2.0f;
321
322 Transform3D screen_transform;
323 screen_transform.translate_local(-(ssize.width / 2.0f), -(ssize.height / 2.0f), 0.0f);
324 screen_transform.scale(Vector3(2.0f / ssize.width, y_scale / ssize.height, 1.0f));
325 _update_transform_to_mat4(screen_transform, state_buffer.screen_transform);
326 _update_transform_2d_to_mat4(p_canvas_transform, state_buffer.canvas_transform);
327
328 Transform2D normal_transform = p_canvas_transform;
329 normal_transform.columns[0].normalize();
330 normal_transform.columns[1].normalize();
331 normal_transform.columns[2] = Vector2();
332 _update_transform_2d_to_mat4(normal_transform, state_buffer.canvas_normal_transform);
333
334 state_buffer.canvas_modulate[0] = p_modulate.r;
335 state_buffer.canvas_modulate[1] = p_modulate.g;
336 state_buffer.canvas_modulate[2] = p_modulate.b;
337 state_buffer.canvas_modulate[3] = p_modulate.a;
338
339 Size2 render_target_size = texture_storage->render_target_get_size(p_to_render_target);
340 state_buffer.screen_pixel_size[0] = 1.0 / render_target_size.x;
341 state_buffer.screen_pixel_size[1] = 1.0 / render_target_size.y;
342
343 state_buffer.time = state.time;
344 state_buffer.use_pixel_snap = p_snap_2d_vertices_to_pixel;
345
346 state_buffer.directional_light_count = directional_light_count;
347
348 Vector2 canvas_scale = p_canvas_transform.get_scale();
349
350 state_buffer.sdf_to_screen[0] = render_target_size.width / canvas_scale.x;
351 state_buffer.sdf_to_screen[1] = render_target_size.height / canvas_scale.y;
352
353 state_buffer.screen_to_sdf[0] = 1.0 / state_buffer.sdf_to_screen[0];
354 state_buffer.screen_to_sdf[1] = 1.0 / state_buffer.sdf_to_screen[1];
355
356 Rect2 sdf_rect = texture_storage->render_target_get_sdf_rect(p_to_render_target);
357 Rect2 sdf_tex_rect(sdf_rect.position / canvas_scale, sdf_rect.size / canvas_scale);
358
359 state_buffer.sdf_to_tex[0] = 1.0 / sdf_tex_rect.size.width;
360 state_buffer.sdf_to_tex[1] = 1.0 / sdf_tex_rect.size.height;
361 state_buffer.sdf_to_tex[2] = -sdf_tex_rect.position.x / sdf_tex_rect.size.width;
362 state_buffer.sdf_to_tex[3] = -sdf_tex_rect.position.y / sdf_tex_rect.size.height;
363
364 state_buffer.tex_to_sdf = 1.0 / ((canvas_scale.x + canvas_scale.y) * 0.5);
365
366 glBindBufferBase(GL_UNIFORM_BUFFER, BASE_UNIFORM_LOCATION, state.canvas_instance_data_buffers[state.current_data_buffer_index].state_ubo);
367 glBufferData(GL_UNIFORM_BUFFER, sizeof(StateBuffer), &state_buffer, GL_STREAM_DRAW);
368
369 GLuint global_buffer = material_storage->global_shader_parameters_get_uniform_buffer();
370
371 glBindBufferBase(GL_UNIFORM_BUFFER, GLOBAL_UNIFORM_LOCATION, global_buffer);
372 glBindBuffer(GL_UNIFORM_BUFFER, 0);
373 }
374
375 glActiveTexture(GL_TEXTURE0 + GLES3::Config::get_singleton()->max_texture_image_units - 5);
376 glBindTexture(GL_TEXTURE_2D, texture_storage->render_target_get_sdf_texture(p_to_render_target));
377
378 {
379 state.default_filter = p_default_filter;
380 state.default_repeat = p_default_repeat;
381 }
382
383 Size2 render_target_size = texture_storage->render_target_get_size(p_to_render_target);
384 glViewport(0, 0, render_target_size.x, render_target_size.y);
385
386 r_sdf_used = false;
387 int item_count = 0;
388 bool backbuffer_cleared = false;
389 bool time_used = false;
390 bool material_screen_texture_cached = false;
391 bool material_screen_texture_mipmaps_cached = false;
392 Rect2 back_buffer_rect;
393 bool backbuffer_copy = false;
394 bool backbuffer_gen_mipmaps = false;
395 bool update_skeletons = false;
396
397 Item *ci = p_item_list;
398 Item *canvas_group_owner = nullptr;
399 bool skip_item = false;
400
401 state.last_item_index = 0;
402
403 while (ci) {
404 if (ci->copy_back_buffer && canvas_group_owner == nullptr) {
405 backbuffer_copy = true;
406
407 if (ci->copy_back_buffer->full) {
408 back_buffer_rect = Rect2();
409 } else {
410 back_buffer_rect = ci->copy_back_buffer->rect;
411 }
412 }
413
414 // Check material for something that may change flow of rendering, but do not bind for now.
415 RID material = ci->material_owner == nullptr ? ci->material : ci->material_owner->material;
416 if (material.is_valid()) {
417 GLES3::CanvasMaterialData *md = static_cast<GLES3::CanvasMaterialData *>(material_storage->material_get_data(material, RS::SHADER_CANVAS_ITEM));
418 if (md && md->shader_data->valid) {
419 if (md->shader_data->uses_screen_texture && canvas_group_owner == nullptr) {
420 if (!material_screen_texture_cached) {
421 backbuffer_copy = true;
422 back_buffer_rect = Rect2();
423 backbuffer_gen_mipmaps = md->shader_data->uses_screen_texture_mipmaps;
424 } else if (!material_screen_texture_mipmaps_cached) {
425 backbuffer_gen_mipmaps = md->shader_data->uses_screen_texture_mipmaps;
426 }
427 }
428
429 if (md->shader_data->uses_sdf) {
430 r_sdf_used = true;
431 }
432 if (md->shader_data->uses_time) {
433 time_used = true;
434 }
435 }
436 }
437
438 if (ci->skeleton.is_valid()) {
439 const Item::Command *c = ci->commands;
440
441 while (c) {
442 if (c->type == Item::Command::TYPE_MESH) {
443 const Item::CommandMesh *cm = static_cast<const Item::CommandMesh *>(c);
444 if (cm->mesh_instance.is_valid()) {
445 mesh_storage->mesh_instance_check_for_update(cm->mesh_instance);
446 mesh_storage->mesh_instance_set_canvas_item_transform(cm->mesh_instance, canvas_transform_inverse * ci->final_transform);
447 update_skeletons = true;
448 }
449 }
450 c = c->next;
451 }
452 }
453
454 if (ci->canvas_group_owner != nullptr) {
455 if (canvas_group_owner == nullptr) {
456 if (update_skeletons) {
457 mesh_storage->update_mesh_instances();
458 update_skeletons = false;
459 }
460 // Canvas group begins here, render until before this item
461 _render_items(p_to_render_target, item_count, canvas_transform_inverse, p_light_list, r_sdf_used);
462 item_count = 0;
463
464 if (ci->canvas_group_owner->canvas_group->mode != RS::CANVAS_GROUP_MODE_TRANSPARENT) {
465 Rect2i group_rect = ci->canvas_group_owner->global_rect_cache;
466 texture_storage->render_target_copy_to_back_buffer(p_to_render_target, group_rect, false);
467 if (ci->canvas_group_owner->canvas_group->mode == RS::CANVAS_GROUP_MODE_CLIP_AND_DRAW) {
468 ci->canvas_group_owner->use_canvas_group = false;
469 items[item_count++] = ci->canvas_group_owner;
470 }
471 } else if (!backbuffer_cleared) {
472 texture_storage->render_target_clear_back_buffer(p_to_render_target, Rect2i(), Color(0, 0, 0, 0));
473 backbuffer_cleared = true;
474 }
475
476 backbuffer_copy = false;
477 canvas_group_owner = ci->canvas_group_owner; //continue until owner found
478 }
479
480 ci->canvas_group_owner = nullptr; //must be cleared
481 }
482
483 if (canvas_group_owner == nullptr && ci->canvas_group != nullptr && ci->canvas_group->mode != RS::CANVAS_GROUP_MODE_CLIP_AND_DRAW) {
484 skip_item = true;
485 }
486
487 if (ci == canvas_group_owner) {
488 if (update_skeletons) {
489 mesh_storage->update_mesh_instances();
490 update_skeletons = false;
491 }
492 _render_items(p_to_render_target, item_count, canvas_transform_inverse, p_light_list, r_sdf_used, true);
493 item_count = 0;
494
495 if (ci->canvas_group->blur_mipmaps) {
496 texture_storage->render_target_gen_back_buffer_mipmaps(p_to_render_target, ci->global_rect_cache);
497 }
498
499 canvas_group_owner = nullptr;
500 // Backbuffer is dirty now and needs to be re-cleared if another CanvasGroup needs it.
501 backbuffer_cleared = false;
502
503 // Tell the renderer to paint this as a canvas group
504 ci->use_canvas_group = true;
505 } else {
506 ci->use_canvas_group = false;
507 }
508
509 if (backbuffer_copy) {
510 if (update_skeletons) {
511 mesh_storage->update_mesh_instances();
512 update_skeletons = false;
513 }
514 //render anything pending, including clearing if no items
515
516 _render_items(p_to_render_target, item_count, canvas_transform_inverse, p_light_list, r_sdf_used);
517 item_count = 0;
518
519 texture_storage->render_target_copy_to_back_buffer(p_to_render_target, back_buffer_rect, backbuffer_gen_mipmaps);
520
521 backbuffer_copy = false;
522 material_screen_texture_cached = true; // After a backbuffer copy, screen texture makes no further copies.
523 material_screen_texture_mipmaps_cached = backbuffer_gen_mipmaps;
524 backbuffer_gen_mipmaps = false;
525 }
526
527 if (backbuffer_gen_mipmaps) {
528 texture_storage->render_target_gen_back_buffer_mipmaps(p_to_render_target, back_buffer_rect);
529
530 backbuffer_gen_mipmaps = false;
531 material_screen_texture_mipmaps_cached = true;
532 }
533
534 // just add all items for now
535 if (skip_item) {
536 skip_item = false;
537 } else {
538 items[item_count++] = ci;
539 }
540
541 if (!ci->next || item_count == MAX_RENDER_ITEMS - 1) {
542 if (update_skeletons) {
543 mesh_storage->update_mesh_instances();
544 update_skeletons = false;
545 }
546 _render_items(p_to_render_target, item_count, canvas_transform_inverse, p_light_list, r_sdf_used, canvas_group_owner != nullptr);
547 //then reset
548 item_count = 0;
549 }
550
551 ci = ci->next;
552 }
553
554 if (time_used) {
555 RenderingServerDefault::redraw_request();
556 }
557
558 state.canvas_instance_data_buffers[state.current_data_buffer_index].fence = glFenceSync(GL_SYNC_GPU_COMMANDS_COMPLETE, 0);
559
560 // Clear out state used in 2D pass
561 reset_canvas();
562 state.current_data_buffer_index = (state.current_data_buffer_index + 1) % state.canvas_instance_data_buffers.size();
563 state.current_instance_buffer_index = 0;
564}
565
566void RasterizerCanvasGLES3::_render_items(RID p_to_render_target, int p_item_count, const Transform2D &p_canvas_transform_inverse, Light *p_lights, bool &r_sdf_used, bool p_to_backbuffer) {
567 GLES3::MaterialStorage *material_storage = GLES3::MaterialStorage::get_singleton();
568
569 canvas_begin(p_to_render_target, p_to_backbuffer);
570
571 if (p_item_count <= 0) {
572 // Nothing to draw, just call canvas_begin() to clear the render target and return.
573 return;
574 }
575
576 uint32_t index = 0;
577 Item *current_clip = nullptr;
578 GLES3::CanvasShaderData *shader_data_cache = nullptr;
579
580 // Record Batches.
581 // First item always forms its own batch.
582 bool batch_broken = false;
583 _new_batch(batch_broken);
584
585 // Override the start position and index as we want to start from where we finished off last time.
586 state.canvas_instance_batches[state.current_batch_index].start = state.last_item_index;
587 index = 0;
588
589 for (int i = 0; i < p_item_count; i++) {
590 Item *ci = items[i];
591
592 if (ci->final_clip_owner != state.canvas_instance_batches[state.current_batch_index].clip) {
593 _new_batch(batch_broken);
594 state.canvas_instance_batches[state.current_batch_index].clip = ci->final_clip_owner;
595 current_clip = ci->final_clip_owner;
596 }
597
598 RID material = ci->material_owner == nullptr ? ci->material : ci->material_owner->material;
599 if (ci->use_canvas_group) {
600 if (ci->canvas_group->mode == RS::CANVAS_GROUP_MODE_CLIP_AND_DRAW) {
601 material = default_clip_children_material;
602 } else {
603 if (material.is_null()) {
604 if (ci->canvas_group->mode == RS::CANVAS_GROUP_MODE_CLIP_ONLY) {
605 material = default_clip_children_material;
606 } else {
607 material = default_canvas_group_material;
608 }
609 }
610 }
611 }
612
613 if (material != state.canvas_instance_batches[state.current_batch_index].material) {
614 _new_batch(batch_broken);
615
616 GLES3::CanvasMaterialData *material_data = nullptr;
617 if (material.is_valid()) {
618 material_data = static_cast<GLES3::CanvasMaterialData *>(material_storage->material_get_data(material, RS::SHADER_CANVAS_ITEM));
619 }
620 shader_data_cache = nullptr;
621 if (material_data) {
622 if (material_data->shader_data->version.is_valid() && material_data->shader_data->valid) {
623 shader_data_cache = material_data->shader_data;
624 }
625 }
626
627 state.canvas_instance_batches[state.current_batch_index].material = material;
628 state.canvas_instance_batches[state.current_batch_index].material_data = material_data;
629 }
630
631 GLES3::CanvasShaderData::BlendMode blend_mode = shader_data_cache ? shader_data_cache->blend_mode : GLES3::CanvasShaderData::BLEND_MODE_MIX;
632
633 _record_item_commands(ci, p_to_render_target, p_canvas_transform_inverse, current_clip, blend_mode, p_lights, index, batch_broken, r_sdf_used);
634 }
635
636 if (index == 0) {
637 // Nothing to render, just return.
638 state.current_batch_index = 0;
639 state.canvas_instance_batches.clear();
640 return;
641 }
642
643 // Copy over all data needed for rendering.
644 glBindBuffer(GL_ARRAY_BUFFER, state.canvas_instance_data_buffers[state.current_data_buffer_index].instance_buffers[state.current_instance_buffer_index]);
645#ifdef WEB_ENABLED
646 glBufferSubData(GL_ARRAY_BUFFER, state.last_item_index * sizeof(InstanceData), sizeof(InstanceData) * index, state.instance_data_array);
647#else
648 // On Desktop and mobile we map the memory without synchronizing for maximum speed.
649 void *buffer = glMapBufferRange(GL_ARRAY_BUFFER, state.last_item_index * sizeof(InstanceData), index * sizeof(InstanceData), GL_MAP_WRITE_BIT | GL_MAP_UNSYNCHRONIZED_BIT);
650 memcpy(buffer, state.instance_data_array, index * sizeof(InstanceData));
651 glUnmapBuffer(GL_ARRAY_BUFFER);
652#endif
653
654 glDisable(GL_SCISSOR_TEST);
655 current_clip = nullptr;
656
657 GLES3::CanvasShaderData::BlendMode last_blend_mode = GLES3::CanvasShaderData::BLEND_MODE_MIX;
658 Color last_blend_color;
659
660 state.current_tex = RID();
661
662 for (uint32_t i = 0; i <= state.current_batch_index; i++) {
663 //setup clip
664 if (current_clip != state.canvas_instance_batches[i].clip) {
665 current_clip = state.canvas_instance_batches[i].clip;
666 if (current_clip) {
667 glEnable(GL_SCISSOR_TEST);
668 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);
669 } else {
670 glDisable(GL_SCISSOR_TEST);
671 }
672 }
673
674 GLES3::CanvasMaterialData *material_data = state.canvas_instance_batches[i].material_data;
675 CanvasShaderGLES3::ShaderVariant variant = state.canvas_instance_batches[i].shader_variant;
676 uint64_t specialization = 0;
677 specialization |= uint64_t(state.canvas_instance_batches[i].lights_disabled);
678 specialization |= uint64_t(!GLES3::Config::get_singleton()->float_texture_supported) << 1;
679 RID shader_version = data.canvas_shader_default_version;
680
681 if (material_data) {
682 if (material_data->shader_data->version.is_valid() && material_data->shader_data->valid) {
683 // Bind uniform buffer and textures
684 material_data->bind_uniforms();
685 shader_version = material_data->shader_data->version;
686 }
687 }
688
689 bool success = GLES3::MaterialStorage::get_singleton()->shaders.canvas_shader.version_bind_shader(shader_version, variant, specialization);
690 if (!success) {
691 continue;
692 }
693
694 GLES3::CanvasShaderData::BlendMode blend_mode = state.canvas_instance_batches[i].blend_mode;
695 Color blend_color = state.canvas_instance_batches[i].blend_color;
696
697 if (last_blend_mode != blend_mode || last_blend_color != blend_color) {
698 if (last_blend_mode == GLES3::CanvasShaderData::BLEND_MODE_DISABLED) {
699 // re-enable it
700 glEnable(GL_BLEND);
701 } else if (blend_mode == GLES3::CanvasShaderData::BLEND_MODE_DISABLED) {
702 // disable it
703 glDisable(GL_BLEND);
704 }
705
706 switch (blend_mode) {
707 case GLES3::CanvasShaderData::BLEND_MODE_DISABLED: {
708 // Nothing to do here.
709 } break;
710 case GLES3::CanvasShaderData::BLEND_MODE_LCD: {
711 glBlendEquation(GL_FUNC_ADD);
712 if (state.transparent_render_target) {
713 glBlendFuncSeparate(GL_CONSTANT_COLOR, GL_ONE_MINUS_SRC_COLOR, GL_ONE, GL_ONE_MINUS_SRC_ALPHA);
714 } else {
715 glBlendFuncSeparate(GL_CONSTANT_COLOR, GL_ONE_MINUS_SRC_COLOR, GL_ZERO, GL_ONE);
716 }
717 glBlendColor(blend_color.r, blend_color.g, blend_color.b, blend_color.a);
718
719 } break;
720 case GLES3::CanvasShaderData::BLEND_MODE_MIX: {
721 glBlendEquation(GL_FUNC_ADD);
722 if (state.transparent_render_target) {
723 glBlendFuncSeparate(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA, GL_ONE, GL_ONE_MINUS_SRC_ALPHA);
724 } else {
725 glBlendFuncSeparate(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA, GL_ZERO, GL_ONE);
726 }
727
728 } break;
729 case GLES3::CanvasShaderData::BLEND_MODE_ADD: {
730 glBlendEquation(GL_FUNC_ADD);
731 if (state.transparent_render_target) {
732 glBlendFuncSeparate(GL_SRC_ALPHA, GL_ONE, GL_SRC_ALPHA, GL_ONE);
733 } else {
734 glBlendFuncSeparate(GL_SRC_ALPHA, GL_ONE, GL_ZERO, GL_ONE);
735 }
736
737 } break;
738 case GLES3::CanvasShaderData::BLEND_MODE_SUB: {
739 glBlendEquation(GL_FUNC_REVERSE_SUBTRACT);
740 if (state.transparent_render_target) {
741 glBlendFuncSeparate(GL_SRC_ALPHA, GL_ONE, GL_SRC_ALPHA, GL_ONE);
742 } else {
743 glBlendFuncSeparate(GL_SRC_ALPHA, GL_ONE, GL_ZERO, GL_ONE);
744 }
745 } break;
746 case GLES3::CanvasShaderData::BLEND_MODE_MUL: {
747 glBlendEquation(GL_FUNC_ADD);
748 if (state.transparent_render_target) {
749 glBlendFuncSeparate(GL_DST_COLOR, GL_ZERO, GL_DST_ALPHA, GL_ZERO);
750 } else {
751 glBlendFuncSeparate(GL_DST_COLOR, GL_ZERO, GL_ZERO, GL_ONE);
752 }
753
754 } break;
755 case GLES3::CanvasShaderData::BLEND_MODE_PMALPHA: {
756 glBlendEquation(GL_FUNC_ADD);
757 if (state.transparent_render_target) {
758 glBlendFuncSeparate(GL_ONE, GL_ONE_MINUS_SRC_ALPHA, GL_ONE, GL_ONE_MINUS_SRC_ALPHA);
759 } else {
760 glBlendFuncSeparate(GL_ONE, GL_ONE_MINUS_SRC_ALPHA, GL_ZERO, GL_ONE);
761 }
762
763 } break;
764 }
765 last_blend_mode = blend_mode;
766 last_blend_color = blend_color;
767 }
768
769 _render_batch(p_lights, i);
770 }
771
772 state.current_batch_index = 0;
773 state.canvas_instance_batches.clear();
774 state.last_item_index += index;
775}
776
777void RasterizerCanvasGLES3::_record_item_commands(const Item *p_item, RID p_render_target, const Transform2D &p_canvas_transform_inverse, Item *&current_clip, GLES3::CanvasShaderData::BlendMode p_blend_mode, Light *p_lights, uint32_t &r_index, bool &r_batch_broken, bool &r_sdf_used) {
778 RenderingServer::CanvasItemTextureFilter texture_filter = p_item->texture_filter == RS::CANVAS_ITEM_TEXTURE_FILTER_DEFAULT ? state.default_filter : p_item->texture_filter;
779
780 if (texture_filter != state.canvas_instance_batches[state.current_batch_index].filter) {
781 _new_batch(r_batch_broken);
782
783 state.canvas_instance_batches[state.current_batch_index].filter = texture_filter;
784 }
785
786 RenderingServer::CanvasItemTextureRepeat texture_repeat = p_item->texture_repeat == RS::CANVAS_ITEM_TEXTURE_REPEAT_DEFAULT ? state.default_repeat : p_item->texture_repeat;
787
788 if (texture_repeat != state.canvas_instance_batches[state.current_batch_index].repeat) {
789 _new_batch(r_batch_broken);
790
791 state.canvas_instance_batches[state.current_batch_index].repeat = texture_repeat;
792 }
793
794 Transform2D base_transform = p_canvas_transform_inverse * p_item->final_transform;
795 Transform2D draw_transform; // Used by transform command
796
797 Color base_color = p_item->final_modulate;
798 uint32_t base_flags = 0;
799 Size2 texpixel_size;
800
801 bool reclip = false;
802
803 bool skipping = false;
804
805 // TODO: consider making lights a per-batch property and then baking light operations in the shader for better performance.
806 uint32_t lights[4] = { 0, 0, 0, 0 };
807
808 uint16_t light_count = 0;
809
810 {
811 Light *light = p_lights;
812
813 while (light) {
814 if (light->render_index_cache >= 0 && p_item->light_mask & light->item_mask && p_item->z_final >= light->z_min && p_item->z_final <= light->z_max && p_item->global_rect_cache.intersects_transformed(light->xform_cache, light->rect_cache)) {
815 uint32_t light_index = light->render_index_cache;
816 lights[light_count >> 2] |= light_index << ((light_count & 3) * 8);
817
818 light_count++;
819
820 if (light_count == data.max_lights_per_item - 1) {
821 break;
822 }
823 }
824 light = light->next_ptr;
825 }
826
827 base_flags |= light_count << FLAGS_LIGHT_COUNT_SHIFT;
828 }
829
830 bool lights_disabled = light_count == 0 && !state.using_directional_lights;
831
832 if (lights_disabled != state.canvas_instance_batches[state.current_batch_index].lights_disabled) {
833 _new_batch(r_batch_broken);
834 state.canvas_instance_batches[state.current_batch_index].lights_disabled = lights_disabled;
835 }
836
837 const Item::Command *c = p_item->commands;
838 while (c) {
839 if (skipping && c->type != Item::Command::TYPE_ANIMATION_SLICE) {
840 c = c->next;
841 continue;
842 }
843
844 if (c->type != Item::Command::TYPE_MESH) {
845 // For Meshes, this gets updated below.
846 _update_transform_2d_to_mat2x3(base_transform * draw_transform, state.instance_data_array[r_index].world);
847 }
848
849 // Zero out most fields.
850 for (int i = 0; i < 4; i++) {
851 state.instance_data_array[r_index].modulation[i] = 0.0;
852 state.instance_data_array[r_index].ninepatch_margins[i] = 0.0;
853 state.instance_data_array[r_index].src_rect[i] = 0.0;
854 state.instance_data_array[r_index].dst_rect[i] = 0.0;
855 state.instance_data_array[r_index].lights[i] = uint32_t(0);
856 }
857 state.instance_data_array[r_index].color_texture_pixel_size[0] = 0.0;
858 state.instance_data_array[r_index].color_texture_pixel_size[1] = 0.0;
859
860 state.instance_data_array[r_index].pad[0] = 0.0;
861 state.instance_data_array[r_index].pad[1] = 0.0;
862
863 state.instance_data_array[r_index].lights[0] = lights[0];
864 state.instance_data_array[r_index].lights[1] = lights[1];
865 state.instance_data_array[r_index].lights[2] = lights[2];
866 state.instance_data_array[r_index].lights[3] = lights[3];
867
868 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
869
870 Color blend_color = base_color;
871 GLES3::CanvasShaderData::BlendMode blend_mode = p_blend_mode;
872 if (c->type == Item::Command::TYPE_RECT) {
873 const Item::CommandRect *rect = static_cast<const Item::CommandRect *>(c);
874 if (rect->flags & CANVAS_RECT_LCD) {
875 blend_mode = GLES3::CanvasShaderData::BLEND_MODE_LCD;
876 blend_color = rect->modulate * base_color;
877 }
878 }
879
880 if (blend_mode != state.canvas_instance_batches[state.current_batch_index].blend_mode || blend_color != state.canvas_instance_batches[state.current_batch_index].blend_color) {
881 _new_batch(r_batch_broken);
882 state.canvas_instance_batches[state.current_batch_index].blend_mode = blend_mode;
883 state.canvas_instance_batches[state.current_batch_index].blend_color = blend_color;
884 }
885
886 switch (c->type) {
887 case Item::Command::TYPE_RECT: {
888 const Item::CommandRect *rect = static_cast<const Item::CommandRect *>(c);
889
890 if (rect->flags & CANVAS_RECT_TILE && state.canvas_instance_batches[state.current_batch_index].repeat != RenderingServer::CanvasItemTextureRepeat::CANVAS_ITEM_TEXTURE_REPEAT_ENABLED) {
891 _new_batch(r_batch_broken);
892 state.canvas_instance_batches[state.current_batch_index].repeat = RenderingServer::CanvasItemTextureRepeat::CANVAS_ITEM_TEXTURE_REPEAT_ENABLED;
893 }
894
895 if (rect->texture != state.canvas_instance_batches[state.current_batch_index].tex || state.canvas_instance_batches[state.current_batch_index].command_type != Item::Command::TYPE_RECT) {
896 _new_batch(r_batch_broken);
897 state.canvas_instance_batches[state.current_batch_index].tex = rect->texture;
898 state.canvas_instance_batches[state.current_batch_index].command_type = Item::Command::TYPE_RECT;
899 state.canvas_instance_batches[state.current_batch_index].command = c;
900 state.canvas_instance_batches[state.current_batch_index].shader_variant = CanvasShaderGLES3::MODE_QUAD;
901 }
902
903 _prepare_canvas_texture(rect->texture, state.canvas_instance_batches[state.current_batch_index].filter, state.canvas_instance_batches[state.current_batch_index].repeat, r_index, texpixel_size);
904
905 Rect2 src_rect;
906 Rect2 dst_rect;
907
908 if (rect->texture != RID()) {
909 src_rect = (rect->flags & CANVAS_RECT_REGION) ? Rect2(rect->source.position * texpixel_size, rect->source.size * texpixel_size) : Rect2(0, 0, 1, 1);
910 dst_rect = Rect2(rect->rect.position, rect->rect.size);
911
912 if (dst_rect.size.width < 0) {
913 dst_rect.position.x += dst_rect.size.width;
914 dst_rect.size.width *= -1;
915 }
916 if (dst_rect.size.height < 0) {
917 dst_rect.position.y += dst_rect.size.height;
918 dst_rect.size.height *= -1;
919 }
920
921 if (rect->flags & CANVAS_RECT_FLIP_H) {
922 src_rect.size.x *= -1;
923 state.instance_data_array[r_index].flags |= FLAGS_FLIP_H;
924 }
925
926 if (rect->flags & CANVAS_RECT_FLIP_V) {
927 src_rect.size.y *= -1;
928 state.instance_data_array[r_index].flags |= FLAGS_FLIP_V;
929 }
930
931 if (rect->flags & CANVAS_RECT_TRANSPOSE) {
932 state.instance_data_array[r_index].flags |= FLAGS_TRANSPOSE_RECT;
933 }
934
935 if (rect->flags & CANVAS_RECT_CLIP_UV) {
936 state.instance_data_array[r_index].flags |= FLAGS_CLIP_RECT_UV;
937 }
938
939 } else {
940 dst_rect = Rect2(rect->rect.position, rect->rect.size);
941
942 if (dst_rect.size.width < 0) {
943 dst_rect.position.x += dst_rect.size.width;
944 dst_rect.size.width *= -1;
945 }
946 if (dst_rect.size.height < 0) {
947 dst_rect.position.y += dst_rect.size.height;
948 dst_rect.size.height *= -1;
949 }
950
951 src_rect = Rect2(0, 0, 1, 1);
952 }
953
954 if (rect->flags & CANVAS_RECT_MSDF) {
955 state.instance_data_array[r_index].flags |= FLAGS_USE_MSDF;
956 state.instance_data_array[r_index].msdf[0] = rect->px_range; // Pixel range.
957 state.instance_data_array[r_index].msdf[1] = rect->outline; // Outline size.
958 state.instance_data_array[r_index].msdf[2] = 0.f; // Reserved.
959 state.instance_data_array[r_index].msdf[3] = 0.f; // Reserved.
960 } else if (rect->flags & CANVAS_RECT_LCD) {
961 state.instance_data_array[r_index].flags |= FLAGS_USE_LCD;
962 }
963
964 state.instance_data_array[r_index].modulation[0] = rect->modulate.r * base_color.r;
965 state.instance_data_array[r_index].modulation[1] = rect->modulate.g * base_color.g;
966 state.instance_data_array[r_index].modulation[2] = rect->modulate.b * base_color.b;
967 state.instance_data_array[r_index].modulation[3] = rect->modulate.a * base_color.a;
968
969 state.instance_data_array[r_index].src_rect[0] = src_rect.position.x;
970 state.instance_data_array[r_index].src_rect[1] = src_rect.position.y;
971 state.instance_data_array[r_index].src_rect[2] = src_rect.size.width;
972 state.instance_data_array[r_index].src_rect[3] = src_rect.size.height;
973
974 state.instance_data_array[r_index].dst_rect[0] = dst_rect.position.x;
975 state.instance_data_array[r_index].dst_rect[1] = dst_rect.position.y;
976 state.instance_data_array[r_index].dst_rect[2] = dst_rect.size.width;
977 state.instance_data_array[r_index].dst_rect[3] = dst_rect.size.height;
978
979 _add_to_batch(r_index, r_batch_broken);
980 } break;
981
982 case Item::Command::TYPE_NINEPATCH: {
983 const Item::CommandNinePatch *np = static_cast<const Item::CommandNinePatch *>(c);
984
985 if (np->texture != state.canvas_instance_batches[state.current_batch_index].tex || state.canvas_instance_batches[state.current_batch_index].command_type != Item::Command::TYPE_NINEPATCH) {
986 _new_batch(r_batch_broken);
987 state.canvas_instance_batches[state.current_batch_index].tex = np->texture;
988 state.canvas_instance_batches[state.current_batch_index].command_type = Item::Command::TYPE_NINEPATCH;
989 state.canvas_instance_batches[state.current_batch_index].command = c;
990 state.canvas_instance_batches[state.current_batch_index].shader_variant = CanvasShaderGLES3::MODE_NINEPATCH;
991 }
992
993 _prepare_canvas_texture(np->texture, state.canvas_instance_batches[state.current_batch_index].filter, state.canvas_instance_batches[state.current_batch_index].repeat, r_index, texpixel_size);
994
995 Rect2 src_rect;
996 Rect2 dst_rect(np->rect.position.x, np->rect.position.y, np->rect.size.x, np->rect.size.y);
997
998 if (np->texture == RID()) {
999 texpixel_size = Size2(1, 1);
1000 src_rect = Rect2(0, 0, 1, 1);
1001
1002 } else {
1003 if (np->source != Rect2()) {
1004 src_rect = Rect2(np->source.position.x * texpixel_size.width, np->source.position.y * texpixel_size.height, np->source.size.x * texpixel_size.width, np->source.size.y * texpixel_size.height);
1005 state.instance_data_array[r_index].color_texture_pixel_size[0] = 1.0 / np->source.size.width;
1006 state.instance_data_array[r_index].color_texture_pixel_size[1] = 1.0 / np->source.size.height;
1007
1008 } else {
1009 src_rect = Rect2(0, 0, 1, 1);
1010 }
1011 }
1012
1013 state.instance_data_array[r_index].modulation[0] = np->color.r * base_color.r;
1014 state.instance_data_array[r_index].modulation[1] = np->color.g * base_color.g;
1015 state.instance_data_array[r_index].modulation[2] = np->color.b * base_color.b;
1016 state.instance_data_array[r_index].modulation[3] = np->color.a * base_color.a;
1017
1018 state.instance_data_array[r_index].src_rect[0] = src_rect.position.x;
1019 state.instance_data_array[r_index].src_rect[1] = src_rect.position.y;
1020 state.instance_data_array[r_index].src_rect[2] = src_rect.size.width;
1021 state.instance_data_array[r_index].src_rect[3] = src_rect.size.height;
1022
1023 state.instance_data_array[r_index].dst_rect[0] = dst_rect.position.x;
1024 state.instance_data_array[r_index].dst_rect[1] = dst_rect.position.y;
1025 state.instance_data_array[r_index].dst_rect[2] = dst_rect.size.width;
1026 state.instance_data_array[r_index].dst_rect[3] = dst_rect.size.height;
1027
1028 state.instance_data_array[r_index].flags |= int(np->axis_x) << FLAGS_NINEPATCH_H_MODE_SHIFT;
1029 state.instance_data_array[r_index].flags |= int(np->axis_y) << FLAGS_NINEPATCH_V_MODE_SHIFT;
1030
1031 if (np->draw_center) {
1032 state.instance_data_array[r_index].flags |= FLAGS_NINEPACH_DRAW_CENTER;
1033 }
1034
1035 state.instance_data_array[r_index].ninepatch_margins[0] = np->margin[SIDE_LEFT];
1036 state.instance_data_array[r_index].ninepatch_margins[1] = np->margin[SIDE_TOP];
1037 state.instance_data_array[r_index].ninepatch_margins[2] = np->margin[SIDE_RIGHT];
1038 state.instance_data_array[r_index].ninepatch_margins[3] = np->margin[SIDE_BOTTOM];
1039
1040 _add_to_batch(r_index, r_batch_broken);
1041
1042 // Restore if overridden.
1043 state.instance_data_array[r_index].color_texture_pixel_size[0] = texpixel_size.x;
1044 state.instance_data_array[r_index].color_texture_pixel_size[1] = texpixel_size.y;
1045 } break;
1046
1047 case Item::Command::TYPE_POLYGON: {
1048 const Item::CommandPolygon *polygon = static_cast<const Item::CommandPolygon *>(c);
1049
1050 // Polygon's can't be batched, so always create a new batch
1051 _new_batch(r_batch_broken);
1052
1053 state.canvas_instance_batches[state.current_batch_index].tex = polygon->texture;
1054 state.canvas_instance_batches[state.current_batch_index].command_type = Item::Command::TYPE_POLYGON;
1055 state.canvas_instance_batches[state.current_batch_index].command = c;
1056 state.canvas_instance_batches[state.current_batch_index].shader_variant = CanvasShaderGLES3::MODE_ATTRIBUTES;
1057
1058 _prepare_canvas_texture(polygon->texture, state.canvas_instance_batches[state.current_batch_index].filter, state.canvas_instance_batches[state.current_batch_index].repeat, r_index, texpixel_size);
1059
1060 state.instance_data_array[r_index].modulation[0] = base_color.r;
1061 state.instance_data_array[r_index].modulation[1] = base_color.g;
1062 state.instance_data_array[r_index].modulation[2] = base_color.b;
1063 state.instance_data_array[r_index].modulation[3] = base_color.a;
1064
1065 for (int j = 0; j < 4; j++) {
1066 state.instance_data_array[r_index].src_rect[j] = 0;
1067 state.instance_data_array[r_index].dst_rect[j] = 0;
1068 state.instance_data_array[r_index].ninepatch_margins[j] = 0;
1069 }
1070
1071 _add_to_batch(r_index, r_batch_broken);
1072 } break;
1073
1074 case Item::Command::TYPE_PRIMITIVE: {
1075 const Item::CommandPrimitive *primitive = static_cast<const Item::CommandPrimitive *>(c);
1076
1077 if (primitive->point_count != state.canvas_instance_batches[state.current_batch_index].primitive_points || state.canvas_instance_batches[state.current_batch_index].command_type != Item::Command::TYPE_PRIMITIVE) {
1078 _new_batch(r_batch_broken);
1079 state.canvas_instance_batches[state.current_batch_index].tex = primitive->texture;
1080 state.canvas_instance_batches[state.current_batch_index].primitive_points = primitive->point_count;
1081 state.canvas_instance_batches[state.current_batch_index].command_type = Item::Command::TYPE_PRIMITIVE;
1082 state.canvas_instance_batches[state.current_batch_index].command = c;
1083 state.canvas_instance_batches[state.current_batch_index].shader_variant = CanvasShaderGLES3::MODE_PRIMITIVE;
1084 }
1085
1086 _prepare_canvas_texture(state.canvas_instance_batches[state.current_batch_index].tex, state.canvas_instance_batches[state.current_batch_index].filter, state.canvas_instance_batches[state.current_batch_index].repeat, r_index, texpixel_size);
1087
1088 for (uint32_t j = 0; j < MIN(3u, primitive->point_count); j++) {
1089 state.instance_data_array[r_index].points[j * 2 + 0] = primitive->points[j].x;
1090 state.instance_data_array[r_index].points[j * 2 + 1] = primitive->points[j].y;
1091 state.instance_data_array[r_index].uvs[j * 2 + 0] = primitive->uvs[j].x;
1092 state.instance_data_array[r_index].uvs[j * 2 + 1] = primitive->uvs[j].y;
1093 Color col = primitive->colors[j] * base_color;
1094 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);
1095 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);
1096 }
1097
1098 _add_to_batch(r_index, r_batch_broken);
1099
1100 if (primitive->point_count == 4) {
1101 // Reset base data.
1102 _update_transform_2d_to_mat2x3(base_transform * draw_transform, state.instance_data_array[r_index].world);
1103 _prepare_canvas_texture(state.canvas_instance_batches[state.current_batch_index].tex, state.canvas_instance_batches[state.current_batch_index].filter, state.canvas_instance_batches[state.current_batch_index].repeat, r_index, texpixel_size);
1104
1105 for (uint32_t j = 0; j < 3; j++) {
1106 int offset = j == 0 ? 0 : 1;
1107 // Second triangle in the quad. Uses vertices 0, 2, 3.
1108 state.instance_data_array[r_index].points[j * 2 + 0] = primitive->points[j + offset].x;
1109 state.instance_data_array[r_index].points[j * 2 + 1] = primitive->points[j + offset].y;
1110 state.instance_data_array[r_index].uvs[j * 2 + 0] = primitive->uvs[j + offset].x;
1111 state.instance_data_array[r_index].uvs[j * 2 + 1] = primitive->uvs[j + offset].y;
1112 Color col = primitive->colors[j + offset] * base_color;
1113 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);
1114 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);
1115 }
1116
1117 _add_to_batch(r_index, r_batch_broken);
1118 }
1119 } break;
1120
1121 case Item::Command::TYPE_MESH:
1122 case Item::Command::TYPE_MULTIMESH:
1123 case Item::Command::TYPE_PARTICLES: {
1124 // Mesh's can't be batched, so always create a new batch
1125 _new_batch(r_batch_broken);
1126
1127 Color modulate(1, 1, 1, 1);
1128 state.canvas_instance_batches[state.current_batch_index].shader_variant = CanvasShaderGLES3::MODE_ATTRIBUTES;
1129 if (c->type == Item::Command::TYPE_MESH) {
1130 const Item::CommandMesh *m = static_cast<const Item::CommandMesh *>(c);
1131 state.canvas_instance_batches[state.current_batch_index].tex = m->texture;
1132 _update_transform_2d_to_mat2x3(base_transform * draw_transform * m->transform, state.instance_data_array[r_index].world);
1133 modulate = m->modulate;
1134
1135 } else if (c->type == Item::Command::TYPE_MULTIMESH) {
1136 const Item::CommandMultiMesh *mm = static_cast<const Item::CommandMultiMesh *>(c);
1137 state.canvas_instance_batches[state.current_batch_index].tex = mm->texture;
1138 state.canvas_instance_batches[state.current_batch_index].shader_variant = CanvasShaderGLES3::MODE_INSTANCED;
1139
1140 if (GLES3::MeshStorage::get_singleton()->multimesh_uses_colors(mm->multimesh)) {
1141 state.instance_data_array[r_index].flags |= FLAGS_INSTANCING_HAS_COLORS;
1142 }
1143 if (GLES3::MeshStorage::get_singleton()->multimesh_uses_custom_data(mm->multimesh)) {
1144 state.instance_data_array[r_index].flags |= FLAGS_INSTANCING_HAS_CUSTOM_DATA;
1145 }
1146 } else if (c->type == Item::Command::TYPE_PARTICLES) {
1147 GLES3::ParticlesStorage *particles_storage = GLES3::ParticlesStorage::get_singleton();
1148 GLES3::TextureStorage *texture_storage = GLES3::TextureStorage::get_singleton();
1149
1150 const Item::CommandParticles *pt = static_cast<const Item::CommandParticles *>(c);
1151 RID particles = pt->particles;
1152 state.canvas_instance_batches[state.current_batch_index].tex = pt->texture;
1153 state.canvas_instance_batches[state.current_batch_index].shader_variant = CanvasShaderGLES3::MODE_INSTANCED;
1154 state.instance_data_array[r_index].flags |= FLAGS_INSTANCING_HAS_COLORS;
1155 state.instance_data_array[r_index].flags |= FLAGS_INSTANCING_HAS_CUSTOM_DATA;
1156
1157 if (particles_storage->particles_has_collision(particles) && texture_storage->render_target_is_sdf_enabled(p_render_target)) {
1158 // Pass collision information.
1159 Transform2D xform = p_item->final_transform;
1160
1161 GLuint sdf_texture = texture_storage->render_target_get_sdf_texture(p_render_target);
1162
1163 Rect2 to_screen;
1164 {
1165 Rect2 sdf_rect = texture_storage->render_target_get_sdf_rect(p_render_target);
1166
1167 to_screen.size = Vector2(1.0 / sdf_rect.size.width, 1.0 / sdf_rect.size.height);
1168 to_screen.position = -sdf_rect.position * to_screen.size;
1169 }
1170
1171 particles_storage->particles_set_canvas_sdf_collision(pt->particles, true, xform, to_screen, sdf_texture);
1172 } else {
1173 particles_storage->particles_set_canvas_sdf_collision(pt->particles, false, Transform2D(), Rect2(), 0);
1174 }
1175 r_sdf_used |= particles_storage->particles_has_collision(particles);
1176 }
1177
1178 state.canvas_instance_batches[state.current_batch_index].command = c;
1179 state.canvas_instance_batches[state.current_batch_index].command_type = c->type;
1180
1181 _prepare_canvas_texture(state.canvas_instance_batches[state.current_batch_index].tex, state.canvas_instance_batches[state.current_batch_index].filter, state.canvas_instance_batches[state.current_batch_index].repeat, r_index, texpixel_size);
1182
1183 state.instance_data_array[r_index].modulation[0] = base_color.r * modulate.r;
1184 state.instance_data_array[r_index].modulation[1] = base_color.g * modulate.g;
1185 state.instance_data_array[r_index].modulation[2] = base_color.b * modulate.b;
1186 state.instance_data_array[r_index].modulation[3] = base_color.a * modulate.a;
1187
1188 for (int j = 0; j < 4; j++) {
1189 state.instance_data_array[r_index].src_rect[j] = 0;
1190 state.instance_data_array[r_index].dst_rect[j] = 0;
1191 state.instance_data_array[r_index].ninepatch_margins[j] = 0;
1192 }
1193 _add_to_batch(r_index, r_batch_broken);
1194 } break;
1195
1196 case Item::Command::TYPE_TRANSFORM: {
1197 const Item::CommandTransform *transform = static_cast<const Item::CommandTransform *>(c);
1198 draw_transform = transform->xform;
1199 } break;
1200
1201 case Item::Command::TYPE_CLIP_IGNORE: {
1202 const Item::CommandClipIgnore *ci = static_cast<const Item::CommandClipIgnore *>(c);
1203 if (current_clip) {
1204 if (ci->ignore != reclip) {
1205 _new_batch(r_batch_broken);
1206 if (ci->ignore) {
1207 state.canvas_instance_batches[state.current_batch_index].clip = nullptr;
1208 reclip = true;
1209 } else {
1210 state.canvas_instance_batches[state.current_batch_index].clip = current_clip;
1211 reclip = false;
1212 }
1213 }
1214 }
1215 } break;
1216
1217 case Item::Command::TYPE_ANIMATION_SLICE: {
1218 const Item::CommandAnimationSlice *as = static_cast<const Item::CommandAnimationSlice *>(c);
1219 double current_time = RSG::rasterizer->get_total_time();
1220 double local_time = Math::fposmod(current_time - as->offset, as->animation_length);
1221 skipping = !(local_time >= as->slice_begin && local_time < as->slice_end);
1222
1223 RenderingServerDefault::redraw_request(); // animation visible means redraw request
1224 } break;
1225 }
1226
1227 c = c->next;
1228 r_batch_broken = false;
1229 }
1230
1231 if (current_clip && reclip) {
1232 //will make it re-enable clipping if needed afterwards
1233 current_clip = nullptr;
1234 }
1235}
1236
1237void RasterizerCanvasGLES3::_render_batch(Light *p_lights, uint32_t p_index) {
1238 ERR_FAIL_COND(!state.canvas_instance_batches[state.current_batch_index].command);
1239
1240 // Used by Polygon and Mesh.
1241 static const GLenum prim[5] = { GL_POINTS, GL_LINES, GL_LINE_STRIP, GL_TRIANGLES, GL_TRIANGLE_STRIP };
1242
1243 _bind_canvas_texture(state.canvas_instance_batches[p_index].tex, state.canvas_instance_batches[p_index].filter, state.canvas_instance_batches[p_index].repeat);
1244
1245 switch (state.canvas_instance_batches[p_index].command_type) {
1246 case Item::Command::TYPE_RECT:
1247 case Item::Command::TYPE_NINEPATCH: {
1248 glBindVertexArray(data.indexed_quad_array);
1249 glBindBuffer(GL_ARRAY_BUFFER, state.canvas_instance_data_buffers[state.current_data_buffer_index].instance_buffers[state.canvas_instance_batches[p_index].instance_buffer_index]);
1250 uint32_t range_start = state.canvas_instance_batches[p_index].start * sizeof(InstanceData);
1251 _enable_attributes(range_start, false);
1252
1253 glDrawElementsInstanced(GL_TRIANGLES, 6, GL_UNSIGNED_INT, 0, state.canvas_instance_batches[p_index].instance_count);
1254 glBindVertexArray(0);
1255
1256 } break;
1257
1258 case Item::Command::TYPE_POLYGON: {
1259 const Item::CommandPolygon *polygon = static_cast<const Item::CommandPolygon *>(state.canvas_instance_batches[p_index].command);
1260
1261 PolygonBuffers *pb = polygon_buffers.polygons.getptr(polygon->polygon.polygon_id);
1262 ERR_FAIL_NULL(pb);
1263
1264 glBindVertexArray(pb->vertex_array);
1265 glBindBuffer(GL_ARRAY_BUFFER, state.canvas_instance_data_buffers[state.current_data_buffer_index].instance_buffers[state.canvas_instance_batches[p_index].instance_buffer_index]);
1266
1267 uint32_t range_start = state.canvas_instance_batches[p_index].start * sizeof(InstanceData);
1268 _enable_attributes(range_start, false);
1269
1270 if (pb->color_disabled && pb->color != Color(1.0, 1.0, 1.0, 1.0)) {
1271 glVertexAttrib4f(RS::ARRAY_COLOR, pb->color.r, pb->color.g, pb->color.b, pb->color.a);
1272 }
1273
1274 if (pb->index_buffer != 0) {
1275 glDrawElementsInstanced(prim[polygon->primitive], pb->count, GL_UNSIGNED_INT, nullptr, 1);
1276 } else {
1277 glDrawArraysInstanced(prim[polygon->primitive], 0, pb->count, 1);
1278 }
1279 glBindVertexArray(0);
1280
1281 if (pb->color_disabled && pb->color != Color(1.0, 1.0, 1.0, 1.0)) {
1282 // Reset so this doesn't pollute other draw calls.
1283 glVertexAttrib4f(RS::ARRAY_COLOR, 1.0, 1.0, 1.0, 1.0);
1284 }
1285 } break;
1286
1287 case Item::Command::TYPE_PRIMITIVE: {
1288 glBindVertexArray(data.canvas_quad_array);
1289 glBindBuffer(GL_ARRAY_BUFFER, state.canvas_instance_data_buffers[state.current_data_buffer_index].instance_buffers[state.canvas_instance_batches[p_index].instance_buffer_index]);
1290 uint32_t range_start = state.canvas_instance_batches[p_index].start * sizeof(InstanceData);
1291 _enable_attributes(range_start, true);
1292
1293 const GLenum primitive[5] = { GL_POINTS, GL_POINTS, GL_LINES, GL_TRIANGLES, GL_TRIANGLES };
1294 int instance_count = state.canvas_instance_batches[p_index].instance_count;
1295 ERR_FAIL_COND(instance_count <= 0);
1296 if (instance_count >= 1) {
1297 glDrawArraysInstanced(primitive[state.canvas_instance_batches[p_index].primitive_points], 0, state.canvas_instance_batches[p_index].primitive_points, instance_count);
1298 }
1299
1300 } break;
1301
1302 case Item::Command::TYPE_MESH:
1303 case Item::Command::TYPE_MULTIMESH:
1304 case Item::Command::TYPE_PARTICLES: {
1305 GLES3::MeshStorage *mesh_storage = GLES3::MeshStorage::get_singleton();
1306 GLES3::ParticlesStorage *particles_storage = GLES3::ParticlesStorage::get_singleton();
1307 RID mesh;
1308 RID mesh_instance;
1309 uint32_t instance_count = 1;
1310 GLuint instance_buffer = 0;
1311 uint32_t instance_stride = 0;
1312 uint32_t instance_color_offset = 0;
1313 bool instance_uses_color = false;
1314 bool instance_uses_custom_data = false;
1315 bool use_instancing = false;
1316
1317 if (state.canvas_instance_batches[p_index].command_type == Item::Command::TYPE_MESH) {
1318 const Item::CommandMesh *m = static_cast<const Item::CommandMesh *>(state.canvas_instance_batches[p_index].command);
1319 mesh = m->mesh;
1320 mesh_instance = m->mesh_instance;
1321
1322 } else if (state.canvas_instance_batches[p_index].command_type == Item::Command::TYPE_MULTIMESH) {
1323 const Item::CommandMultiMesh *mm = static_cast<const Item::CommandMultiMesh *>(state.canvas_instance_batches[p_index].command);
1324 RID multimesh = mm->multimesh;
1325 mesh = mesh_storage->multimesh_get_mesh(multimesh);
1326
1327 if (mesh_storage->multimesh_get_transform_format(multimesh) != RS::MULTIMESH_TRANSFORM_2D) {
1328 break;
1329 }
1330
1331 instance_count = mesh_storage->multimesh_get_instances_to_draw(multimesh);
1332
1333 if (instance_count == 0) {
1334 break;
1335 }
1336
1337 instance_buffer = mesh_storage->multimesh_get_gl_buffer(multimesh);
1338 instance_stride = mesh_storage->multimesh_get_stride(multimesh);
1339 instance_color_offset = mesh_storage->multimesh_get_color_offset(multimesh);
1340 instance_uses_color = mesh_storage->multimesh_uses_colors(multimesh);
1341 instance_uses_custom_data = mesh_storage->multimesh_uses_custom_data(multimesh);
1342 use_instancing = true;
1343
1344 } else if (state.canvas_instance_batches[p_index].command_type == Item::Command::TYPE_PARTICLES) {
1345 const Item::CommandParticles *pt = static_cast<const Item::CommandParticles *>(state.canvas_instance_batches[p_index].command);
1346 RID particles = pt->particles;
1347 mesh = particles_storage->particles_get_draw_pass_mesh(particles, 0);
1348
1349 ERR_BREAK(particles_storage->particles_get_mode(particles) != RS::PARTICLES_MODE_2D);
1350 particles_storage->particles_request_process(particles);
1351
1352 if (particles_storage->particles_is_inactive(particles)) {
1353 break;
1354 }
1355
1356 RenderingServerDefault::redraw_request(); // Active particles means redraw request.
1357
1358 int dpc = particles_storage->particles_get_draw_passes(particles);
1359 if (dpc == 0) {
1360 break; // Nothing to draw.
1361 }
1362
1363 instance_count = particles_storage->particles_get_amount(particles);
1364 instance_buffer = particles_storage->particles_get_gl_buffer(particles);
1365 instance_stride = 12; // 8 bytes for instance transform and 4 bytes for packed color and custom.
1366 instance_color_offset = 8; // 8 bytes for instance transform.
1367 instance_uses_color = true;
1368 instance_uses_custom_data = true;
1369 use_instancing = true;
1370 }
1371
1372 ERR_FAIL_COND(mesh.is_null());
1373
1374 uint32_t surf_count = mesh_storage->mesh_get_surface_count(mesh);
1375
1376 for (uint32_t j = 0; j < surf_count; j++) {
1377 void *surface = mesh_storage->mesh_get_surface(mesh, j);
1378
1379 RS::PrimitiveType primitive = mesh_storage->mesh_surface_get_primitive(surface);
1380 ERR_CONTINUE(primitive < 0 || primitive >= RS::PRIMITIVE_MAX);
1381
1382 GLuint vertex_array_gl = 0;
1383 GLuint index_array_gl = 0;
1384
1385 uint32_t input_mask = 0; // 2D meshes always use the same vertex format
1386 if (mesh_instance.is_valid()) {
1387 mesh_storage->mesh_instance_surface_get_vertex_arrays_and_format(mesh_instance, j, input_mask, vertex_array_gl);
1388 } else {
1389 mesh_storage->mesh_surface_get_vertex_arrays_and_format(surface, input_mask, vertex_array_gl);
1390 }
1391
1392 index_array_gl = mesh_storage->mesh_surface_get_index_buffer(surface, 0);
1393 bool use_index_buffer = false;
1394 glBindVertexArray(vertex_array_gl);
1395 glBindBuffer(GL_ARRAY_BUFFER, state.canvas_instance_data_buffers[state.current_data_buffer_index].instance_buffers[state.canvas_instance_batches[p_index].instance_buffer_index]);
1396
1397 uint32_t range_start = state.canvas_instance_batches[p_index].start * sizeof(InstanceData);
1398 _enable_attributes(range_start, false, instance_count);
1399
1400 if (index_array_gl != 0) {
1401 glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, index_array_gl);
1402 use_index_buffer = true;
1403 }
1404
1405 if (use_instancing) {
1406 if (instance_buffer == 0) {
1407 break;
1408 }
1409 // Bind instance buffers.
1410 glBindBuffer(GL_ARRAY_BUFFER, instance_buffer);
1411 glEnableVertexAttribArray(1);
1412 glVertexAttribPointer(1, 4, GL_FLOAT, GL_FALSE, instance_stride * sizeof(float), CAST_INT_TO_UCHAR_PTR(0));
1413 glVertexAttribDivisor(1, 1);
1414 glEnableVertexAttribArray(2);
1415 glVertexAttribPointer(2, 4, GL_FLOAT, GL_FALSE, instance_stride * sizeof(float), CAST_INT_TO_UCHAR_PTR(4 * 4));
1416 glVertexAttribDivisor(2, 1);
1417
1418 if (instance_uses_color || instance_uses_custom_data) {
1419 glEnableVertexAttribArray(5);
1420 glVertexAttribIPointer(5, 4, GL_UNSIGNED_INT, instance_stride * sizeof(float), CAST_INT_TO_UCHAR_PTR(instance_color_offset * sizeof(float)));
1421 glVertexAttribDivisor(5, 1);
1422 }
1423 }
1424
1425 GLenum primitive_gl = prim[int(primitive)];
1426
1427 if (use_index_buffer) {
1428 glDrawElementsInstanced(primitive_gl, mesh_storage->mesh_surface_get_vertices_drawn_count(surface), mesh_storage->mesh_surface_get_index_type(surface), 0, instance_count);
1429 } else {
1430 glDrawArraysInstanced(primitive_gl, 0, mesh_storage->mesh_surface_get_vertices_drawn_count(surface), instance_count);
1431 }
1432 glBindBuffer(GL_ARRAY_BUFFER, 0);
1433 glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0);
1434 if (use_instancing) {
1435 glDisableVertexAttribArray(5);
1436 glDisableVertexAttribArray(6);
1437 glDisableVertexAttribArray(7);
1438 glDisableVertexAttribArray(8);
1439 }
1440 }
1441
1442 } break;
1443 case Item::Command::TYPE_TRANSFORM:
1444 case Item::Command::TYPE_CLIP_IGNORE:
1445 case Item::Command::TYPE_ANIMATION_SLICE: {
1446 // Can ignore these as they only impact batch creation.
1447 } break;
1448 }
1449}
1450
1451void RasterizerCanvasGLES3::_add_to_batch(uint32_t &r_index, bool &r_batch_broken) {
1452 state.canvas_instance_batches[state.current_batch_index].instance_count++;
1453 r_index++;
1454 if (r_index + state.last_item_index >= data.max_instances_per_buffer) {
1455 // Copy over all data needed for rendering right away
1456 // then go back to recording item commands.
1457 glBindBuffer(GL_ARRAY_BUFFER, state.canvas_instance_data_buffers[state.current_data_buffer_index].instance_buffers[state.current_instance_buffer_index]);
1458#ifdef WEB_ENABLED
1459 glBufferSubData(GL_ARRAY_BUFFER, state.last_item_index * sizeof(InstanceData), sizeof(InstanceData) * r_index, state.instance_data_array);
1460#else
1461 // On Desktop and mobile we map the memory without synchronizing for maximum speed.
1462 void *buffer = glMapBufferRange(GL_ARRAY_BUFFER, state.last_item_index * sizeof(InstanceData), r_index * sizeof(InstanceData), GL_MAP_WRITE_BIT | GL_MAP_UNSYNCHRONIZED_BIT);
1463 memcpy(buffer, state.instance_data_array, r_index * sizeof(InstanceData));
1464 glUnmapBuffer(GL_ARRAY_BUFFER);
1465#endif
1466 _allocate_instance_buffer();
1467 r_index = 0;
1468 state.last_item_index = 0;
1469 r_batch_broken = false; // Force a new batch to be created
1470 _new_batch(r_batch_broken);
1471 state.canvas_instance_batches[state.current_batch_index].start = 0;
1472 }
1473}
1474
1475void RasterizerCanvasGLES3::_new_batch(bool &r_batch_broken) {
1476 if (state.canvas_instance_batches.size() == 0) {
1477 state.canvas_instance_batches.push_back(Batch());
1478 return;
1479 }
1480
1481 if (r_batch_broken || state.canvas_instance_batches[state.current_batch_index].instance_count == 0) {
1482 return;
1483 }
1484
1485 r_batch_broken = true;
1486
1487 // Copy the properties of the current batch, we will manually update the things that changed.
1488 Batch new_batch = state.canvas_instance_batches[state.current_batch_index];
1489 new_batch.instance_count = 0;
1490 new_batch.start = state.canvas_instance_batches[state.current_batch_index].start + state.canvas_instance_batches[state.current_batch_index].instance_count;
1491 new_batch.instance_buffer_index = state.current_instance_buffer_index;
1492 state.current_batch_index++;
1493 state.canvas_instance_batches.push_back(new_batch);
1494}
1495
1496void RasterizerCanvasGLES3::_enable_attributes(uint32_t p_start, bool p_primitive, uint32_t p_rate) {
1497 uint32_t split = p_primitive ? 11 : 12;
1498 for (uint32_t i = 6; i < split; i++) {
1499 glEnableVertexAttribArray(i);
1500 glVertexAttribPointer(i, 4, GL_FLOAT, GL_FALSE, sizeof(InstanceData), CAST_INT_TO_UCHAR_PTR(p_start + (i - 6) * 4 * sizeof(float)));
1501 glVertexAttribDivisor(i, p_rate);
1502 }
1503 for (uint32_t i = split; i <= 13; i++) {
1504 glEnableVertexAttribArray(i);
1505 glVertexAttribIPointer(i, 4, GL_UNSIGNED_INT, sizeof(InstanceData), CAST_INT_TO_UCHAR_PTR(p_start + (i - 6) * 4 * sizeof(float)));
1506 glVertexAttribDivisor(i, p_rate);
1507 }
1508}
1509RID RasterizerCanvasGLES3::light_create() {
1510 CanvasLight canvas_light;
1511 return canvas_light_owner.make_rid(canvas_light);
1512}
1513
1514void RasterizerCanvasGLES3::light_set_texture(RID p_rid, RID p_texture) {
1515 GLES3::TextureStorage *texture_storage = GLES3::TextureStorage::get_singleton();
1516
1517 CanvasLight *cl = canvas_light_owner.get_or_null(p_rid);
1518 ERR_FAIL_NULL(cl);
1519 if (cl->texture == p_texture) {
1520 return;
1521 }
1522
1523 ERR_FAIL_COND(p_texture.is_valid() && !texture_storage->owns_texture(p_texture));
1524
1525 if (cl->texture.is_valid()) {
1526 texture_storage->texture_remove_from_texture_atlas(cl->texture);
1527 }
1528 cl->texture = p_texture;
1529
1530 if (cl->texture.is_valid()) {
1531 texture_storage->texture_add_to_texture_atlas(cl->texture);
1532 }
1533}
1534
1535void RasterizerCanvasGLES3::light_set_use_shadow(RID p_rid, bool p_enable) {
1536 CanvasLight *cl = canvas_light_owner.get_or_null(p_rid);
1537 ERR_FAIL_NULL(cl);
1538
1539 cl->shadow.enabled = p_enable;
1540}
1541
1542void 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) {
1543 GLES3::Config *config = GLES3::Config::get_singleton();
1544
1545 CanvasLight *cl = canvas_light_owner.get_or_null(p_rid);
1546 ERR_FAIL_COND(!cl->shadow.enabled);
1547
1548 _update_shadow_atlas();
1549
1550 cl->shadow.z_far = p_far;
1551 cl->shadow.y_offset = float(p_shadow_index * 2 + 1) / float(data.max_lights_per_render * 2);
1552
1553 glBindFramebuffer(GL_FRAMEBUFFER, state.shadow_fb);
1554 glViewport(0, p_shadow_index * 2, state.shadow_texture_size, 2);
1555
1556 glDepthMask(GL_TRUE);
1557 glEnable(GL_DEPTH_TEST);
1558 glDepthFunc(GL_LESS);
1559 glDisable(GL_BLEND);
1560
1561 glEnable(GL_SCISSOR_TEST);
1562 glScissor(0, p_shadow_index * 2, state.shadow_texture_size, 2);
1563 glClearColor(p_far, p_far, p_far, 1.0);
1564 glClearDepth(1.0);
1565 glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
1566
1567 glCullFace(GL_BACK);
1568 glDisable(GL_CULL_FACE);
1569 RS::CanvasOccluderPolygonCullMode cull_mode = RS::CANVAS_OCCLUDER_POLYGON_CULL_DISABLED;
1570
1571 CanvasOcclusionShaderGLES3::ShaderVariant variant = config->float_texture_supported ? CanvasOcclusionShaderGLES3::MODE_SHADOW : CanvasOcclusionShaderGLES3::MODE_SHADOW_RGBA;
1572 bool success = shadow_render.shader.version_bind_shader(shadow_render.shader_version, variant);
1573 if (!success) {
1574 return;
1575 }
1576
1577 for (int i = 0; i < 4; i++) {
1578 glViewport((state.shadow_texture_size / 4) * i, p_shadow_index * 2, (state.shadow_texture_size / 4), 2);
1579
1580 Projection projection;
1581 {
1582 real_t fov = 90;
1583 real_t nearp = p_near;
1584 real_t farp = p_far;
1585 real_t aspect = 1.0;
1586
1587 real_t ymax = nearp * Math::tan(Math::deg_to_rad(fov * 0.5));
1588 real_t ymin = -ymax;
1589 real_t xmin = ymin * aspect;
1590 real_t xmax = ymax * aspect;
1591
1592 projection.set_frustum(xmin, xmax, ymin, ymax, nearp, farp);
1593 }
1594
1595 Vector3 cam_target = Basis::from_euler(Vector3(0, 0, Math_TAU * ((i + 3) / 4.0))).xform(Vector3(0, 1, 0));
1596
1597 projection = projection * Projection(Transform3D().looking_at(cam_target, Vector3(0, 0, -1)).affine_inverse());
1598 shadow_render.shader.version_set_uniform(CanvasOcclusionShaderGLES3::PROJECTION, projection, shadow_render.shader_version, variant);
1599
1600 static const Vector2 directions[4] = { Vector2(1, 0), Vector2(0, 1), Vector2(-1, 0), Vector2(0, -1) };
1601 shadow_render.shader.version_set_uniform(CanvasOcclusionShaderGLES3::DIRECTION, directions[i].x, directions[i].y, shadow_render.shader_version, variant);
1602 shadow_render.shader.version_set_uniform(CanvasOcclusionShaderGLES3::Z_FAR, p_far, shadow_render.shader_version, variant);
1603
1604 LightOccluderInstance *instance = p_occluders;
1605
1606 while (instance) {
1607 OccluderPolygon *co = occluder_polygon_owner.get_or_null(instance->occluder);
1608
1609 if (!co || co->vertex_array == 0 || !(p_light_mask & instance->light_mask)) {
1610 instance = instance->next;
1611 continue;
1612 }
1613
1614 Transform2D modelview = p_light_xform * instance->xform_cache;
1615 shadow_render.shader.version_set_uniform(CanvasOcclusionShaderGLES3::MODELVIEW1, modelview.columns[0][0], modelview.columns[1][0], 0, modelview.columns[2][0], shadow_render.shader_version, variant);
1616 shadow_render.shader.version_set_uniform(CanvasOcclusionShaderGLES3::MODELVIEW2, modelview.columns[0][1], modelview.columns[1][1], 0, modelview.columns[2][1], shadow_render.shader_version, variant);
1617
1618 if (co->cull_mode != cull_mode) {
1619 if (co->cull_mode == RS::CANVAS_OCCLUDER_POLYGON_CULL_DISABLED) {
1620 glDisable(GL_CULL_FACE);
1621 } else {
1622 if (cull_mode == RS::CANVAS_OCCLUDER_POLYGON_CULL_DISABLED) {
1623 // Last time was disabled, so enable and set proper face.
1624 glEnable(GL_CULL_FACE);
1625 }
1626 glCullFace(co->cull_mode == RS::CANVAS_OCCLUDER_POLYGON_CULL_CLOCKWISE ? GL_FRONT : GL_BACK);
1627 }
1628 cull_mode = co->cull_mode;
1629 }
1630
1631 glBindVertexArray(co->vertex_array);
1632 glDrawElements(GL_TRIANGLES, 3 * co->line_point_count, GL_UNSIGNED_SHORT, 0);
1633
1634 instance = instance->next;
1635 }
1636 }
1637
1638 glBindVertexArray(0);
1639 glBindFramebuffer(GL_FRAMEBUFFER, 0);
1640 glDepthMask(GL_FALSE);
1641 glDisable(GL_DEPTH_TEST);
1642 glDisable(GL_SCISSOR_TEST);
1643}
1644
1645void 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) {
1646 GLES3::Config *config = GLES3::Config::get_singleton();
1647
1648 CanvasLight *cl = canvas_light_owner.get_or_null(p_rid);
1649 ERR_FAIL_COND(!cl->shadow.enabled);
1650
1651 _update_shadow_atlas();
1652
1653 Vector2 light_dir = p_light_xform.columns[1].normalized();
1654
1655 Vector2 center = p_clip_rect.get_center();
1656
1657 float to_edge_distance = ABS(light_dir.dot(p_clip_rect.get_support(light_dir)) - light_dir.dot(center));
1658
1659 Vector2 from_pos = center - light_dir * (to_edge_distance + p_cull_distance);
1660 float distance = to_edge_distance * 2.0 + p_cull_distance;
1661 float half_size = p_clip_rect.size.length() * 0.5; //shadow length, must keep this no matter the angle
1662
1663 cl->shadow.z_far = distance;
1664 cl->shadow.y_offset = float(p_shadow_index * 2 + 1) / float(data.max_lights_per_render * 2);
1665
1666 Transform2D to_light_xform;
1667
1668 to_light_xform[2] = from_pos;
1669 to_light_xform[1] = light_dir;
1670 to_light_xform[0] = -light_dir.orthogonal();
1671
1672 to_light_xform.invert();
1673
1674 glBindFramebuffer(GL_FRAMEBUFFER, state.shadow_fb);
1675 glViewport(0, p_shadow_index * 2, state.shadow_texture_size, 2);
1676
1677 glDepthMask(GL_TRUE);
1678 glEnable(GL_DEPTH_TEST);
1679 glDepthFunc(GL_LESS);
1680 glDisable(GL_BLEND);
1681
1682 glEnable(GL_SCISSOR_TEST);
1683 glScissor(0, p_shadow_index * 2, state.shadow_texture_size, 2);
1684 glClearColor(1.0, 1.0, 1.0, 1.0);
1685 glClearDepth(1.0);
1686 glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
1687
1688 glCullFace(GL_BACK);
1689 glDisable(GL_CULL_FACE);
1690 RS::CanvasOccluderPolygonCullMode cull_mode = RS::CANVAS_OCCLUDER_POLYGON_CULL_DISABLED;
1691
1692 CanvasOcclusionShaderGLES3::ShaderVariant variant = config->float_texture_supported ? CanvasOcclusionShaderGLES3::MODE_SHADOW : CanvasOcclusionShaderGLES3::MODE_SHADOW_RGBA;
1693 bool success = shadow_render.shader.version_bind_shader(shadow_render.shader_version, variant);
1694 if (!success) {
1695 return;
1696 }
1697
1698 Projection projection;
1699 projection.set_orthogonal(-half_size, half_size, -0.5, 0.5, 0.0, distance);
1700 projection = projection * Projection(Transform3D().looking_at(Vector3(0, 1, 0), Vector3(0, 0, -1)).affine_inverse());
1701
1702 shadow_render.shader.version_set_uniform(CanvasOcclusionShaderGLES3::PROJECTION, projection, shadow_render.shader_version, variant);
1703 shadow_render.shader.version_set_uniform(CanvasOcclusionShaderGLES3::DIRECTION, 0.0, 1.0, shadow_render.shader_version, variant);
1704 shadow_render.shader.version_set_uniform(CanvasOcclusionShaderGLES3::Z_FAR, distance, shadow_render.shader_version, variant);
1705
1706 LightOccluderInstance *instance = p_occluders;
1707
1708 while (instance) {
1709 OccluderPolygon *co = occluder_polygon_owner.get_or_null(instance->occluder);
1710
1711 if (!co || co->vertex_array == 0 || !(p_light_mask & instance->light_mask)) {
1712 instance = instance->next;
1713 continue;
1714 }
1715
1716 Transform2D modelview = to_light_xform * instance->xform_cache;
1717 shadow_render.shader.version_set_uniform(CanvasOcclusionShaderGLES3::MODELVIEW1, modelview.columns[0][0], modelview.columns[1][0], 0, modelview.columns[2][0], shadow_render.shader_version, variant);
1718 shadow_render.shader.version_set_uniform(CanvasOcclusionShaderGLES3::MODELVIEW2, modelview.columns[0][1], modelview.columns[1][1], 0, modelview.columns[2][1], shadow_render.shader_version, variant);
1719
1720 if (co->cull_mode != cull_mode) {
1721 if (co->cull_mode == RS::CANVAS_OCCLUDER_POLYGON_CULL_DISABLED) {
1722 glDisable(GL_CULL_FACE);
1723 } else {
1724 if (cull_mode == RS::CANVAS_OCCLUDER_POLYGON_CULL_DISABLED) {
1725 // Last time was disabled, so enable and set proper face.
1726 glEnable(GL_CULL_FACE);
1727 }
1728 glCullFace(co->cull_mode == RS::CANVAS_OCCLUDER_POLYGON_CULL_CLOCKWISE ? GL_FRONT : GL_BACK);
1729 }
1730 cull_mode = co->cull_mode;
1731 }
1732
1733 glBindVertexArray(co->vertex_array);
1734 glDrawElements(GL_TRIANGLES, 3 * co->line_point_count, GL_UNSIGNED_SHORT, 0);
1735
1736 instance = instance->next;
1737 }
1738
1739 Transform2D to_shadow;
1740 to_shadow.columns[0].x = 1.0 / -(half_size * 2.0);
1741 to_shadow.columns[2].x = 0.5;
1742
1743 cl->shadow.directional_xform = to_shadow * to_light_xform;
1744
1745 glBindVertexArray(0);
1746 glBindFramebuffer(GL_FRAMEBUFFER, 0);
1747 glDepthMask(GL_FALSE);
1748 glDisable(GL_DEPTH_TEST);
1749 glDisable(GL_SCISSOR_TEST);
1750 glDisable(GL_CULL_FACE);
1751}
1752
1753void RasterizerCanvasGLES3::_update_shadow_atlas() {
1754 GLES3::Config *config = GLES3::Config::get_singleton();
1755
1756 if (state.shadow_fb == 0) {
1757 glActiveTexture(GL_TEXTURE0);
1758
1759 glGenFramebuffers(1, &state.shadow_fb);
1760 glBindFramebuffer(GL_FRAMEBUFFER, state.shadow_fb);
1761
1762 glGenRenderbuffers(1, &state.shadow_depth_buffer);
1763 glBindRenderbuffer(GL_RENDERBUFFER, state.shadow_depth_buffer);
1764 glRenderbufferStorage(GL_RENDERBUFFER, GL_DEPTH_COMPONENT24, state.shadow_texture_size, data.max_lights_per_render * 2);
1765 glFramebufferRenderbuffer(GL_FRAMEBUFFER, GL_DEPTH_ATTACHMENT, GL_RENDERBUFFER, state.shadow_depth_buffer);
1766
1767 glGenTextures(1, &state.shadow_texture);
1768 glBindTexture(GL_TEXTURE_2D, state.shadow_texture);
1769 if (config->float_texture_supported) {
1770 glTexImage2D(GL_TEXTURE_2D, 0, GL_R32F, state.shadow_texture_size, data.max_lights_per_render * 2, 0, GL_RED, GL_FLOAT, nullptr);
1771 } else {
1772 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA8, state.shadow_texture_size, data.max_lights_per_render * 2, 0, GL_RGBA, GL_UNSIGNED_BYTE, nullptr);
1773 }
1774
1775 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
1776 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
1777 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
1778 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
1779 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_BASE_LEVEL, 0);
1780 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAX_LEVEL, 1);
1781 glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, state.shadow_texture, 0);
1782
1783 GLenum status = glCheckFramebufferStatus(GL_FRAMEBUFFER);
1784 if (status != GL_FRAMEBUFFER_COMPLETE) {
1785 glDeleteFramebuffers(1, &state.shadow_fb);
1786 glDeleteTextures(1, &state.shadow_texture);
1787 glDeleteRenderbuffers(1, &state.shadow_depth_buffer);
1788 state.shadow_fb = 0;
1789 state.shadow_texture = 0;
1790 state.shadow_depth_buffer = 0;
1791 WARN_PRINT("Could not create CanvasItem shadow atlas, status: " + GLES3::TextureStorage::get_singleton()->get_framebuffer_error(status));
1792 }
1793 GLES3::Utilities::get_singleton()->texture_allocated_data(state.shadow_texture, state.shadow_texture_size * data.max_lights_per_render * 2 * 4, "2D shadow atlas texture");
1794 glBindFramebuffer(GL_FRAMEBUFFER, GLES3::TextureStorage::system_fbo);
1795 }
1796}
1797
1798void RasterizerCanvasGLES3::render_sdf(RID p_render_target, LightOccluderInstance *p_occluders) {
1799 GLES3::TextureStorage *texture_storage = GLES3::TextureStorage::get_singleton();
1800
1801 GLuint fb = texture_storage->render_target_get_sdf_framebuffer(p_render_target);
1802 Rect2i rect = texture_storage->render_target_get_sdf_rect(p_render_target);
1803
1804 Transform2D to_sdf;
1805 to_sdf.columns[0] *= rect.size.width;
1806 to_sdf.columns[1] *= rect.size.height;
1807 to_sdf.columns[2] = rect.position;
1808
1809 Transform2D to_clip;
1810 to_clip.columns[0] *= 2.0;
1811 to_clip.columns[1] *= 2.0;
1812 to_clip.columns[2] = -Vector2(1.0, 1.0);
1813
1814 to_clip = to_clip * to_sdf.affine_inverse();
1815
1816 glBindFramebuffer(GL_FRAMEBUFFER, fb);
1817 glViewport(0, 0, rect.size.width, rect.size.height);
1818
1819 glDepthMask(GL_FALSE);
1820 glDisable(GL_DEPTH_TEST);
1821 glDisable(GL_BLEND);
1822 glDisable(GL_CULL_FACE);
1823 glDisable(GL_SCISSOR_TEST);
1824
1825 glClearColor(0.0, 0.0, 0.0, 0.0);
1826 glClear(GL_COLOR_BUFFER_BIT);
1827
1828 CanvasOcclusionShaderGLES3::ShaderVariant variant = CanvasOcclusionShaderGLES3::MODE_SDF;
1829 bool success = shadow_render.shader.version_bind_shader(shadow_render.shader_version, variant);
1830 if (!success) {
1831 return;
1832 }
1833
1834 shadow_render.shader.version_set_uniform(CanvasOcclusionShaderGLES3::PROJECTION, Projection(), shadow_render.shader_version, variant);
1835 shadow_render.shader.version_set_uniform(CanvasOcclusionShaderGLES3::DIRECTION, 0.0, 0.0, shadow_render.shader_version, variant);
1836 shadow_render.shader.version_set_uniform(CanvasOcclusionShaderGLES3::Z_FAR, 0.0, shadow_render.shader_version, variant);
1837
1838 LightOccluderInstance *instance = p_occluders;
1839
1840 while (instance) {
1841 OccluderPolygon *oc = occluder_polygon_owner.get_or_null(instance->occluder);
1842
1843 if (!oc || oc->sdf_vertex_array == 0) {
1844 instance = instance->next;
1845 continue;
1846 }
1847
1848 Transform2D modelview = to_clip * instance->xform_cache;
1849 shadow_render.shader.version_set_uniform(CanvasOcclusionShaderGLES3::MODELVIEW1, modelview.columns[0][0], modelview.columns[1][0], 0, modelview.columns[2][0], shadow_render.shader_version, variant);
1850 shadow_render.shader.version_set_uniform(CanvasOcclusionShaderGLES3::MODELVIEW2, modelview.columns[0][1], modelview.columns[1][1], 0, modelview.columns[2][1], shadow_render.shader_version, variant);
1851
1852 glBindVertexArray(oc->sdf_vertex_array);
1853 glDrawElements(oc->sdf_is_lines ? GL_LINES : GL_TRIANGLES, oc->sdf_index_count, GL_UNSIGNED_INT, 0);
1854
1855 instance = instance->next;
1856 }
1857
1858 texture_storage->render_target_sdf_process(p_render_target); //done rendering, process it
1859 glBindVertexArray(0);
1860 glBindFramebuffer(GL_FRAMEBUFFER, 0);
1861}
1862
1863RID RasterizerCanvasGLES3::occluder_polygon_create() {
1864 OccluderPolygon occluder;
1865
1866 return occluder_polygon_owner.make_rid(occluder);
1867}
1868
1869void RasterizerCanvasGLES3::occluder_polygon_set_shape(RID p_occluder, const Vector<Vector2> &p_points, bool p_closed) {
1870 OccluderPolygon *oc = occluder_polygon_owner.get_or_null(p_occluder);
1871 ERR_FAIL_NULL(oc);
1872
1873 Vector<Vector2> lines;
1874
1875 if (p_points.size()) {
1876 int lc = p_points.size() * 2;
1877
1878 lines.resize(lc - (p_closed ? 0 : 2));
1879 {
1880 Vector2 *w = lines.ptrw();
1881 const Vector2 *r = p_points.ptr();
1882
1883 int max = lc / 2;
1884 if (!p_closed) {
1885 max--;
1886 }
1887 for (int i = 0; i < max; i++) {
1888 Vector2 a = r[i];
1889 Vector2 b = r[(i + 1) % (lc / 2)];
1890 w[i * 2 + 0] = a;
1891 w[i * 2 + 1] = b;
1892 }
1893 }
1894 }
1895
1896 if (oc->line_point_count != lines.size() && oc->vertex_array != 0) {
1897 glDeleteVertexArrays(1, &oc->vertex_array);
1898 GLES3::Utilities::get_singleton()->buffer_free_data(oc->vertex_buffer);
1899 GLES3::Utilities::get_singleton()->buffer_free_data(oc->index_buffer);
1900
1901 oc->vertex_array = 0;
1902 oc->vertex_buffer = 0;
1903 oc->index_buffer = 0;
1904 }
1905
1906 if (lines.size()) {
1907 Vector<uint8_t> geometry;
1908 Vector<uint8_t> indices;
1909 int lc = lines.size();
1910
1911 geometry.resize(lc * 6 * sizeof(float));
1912 indices.resize(lc * 3 * sizeof(uint16_t));
1913
1914 {
1915 uint8_t *vw = geometry.ptrw();
1916 float *vwptr = reinterpret_cast<float *>(vw);
1917 uint8_t *iw = indices.ptrw();
1918 uint16_t *iwptr = (uint16_t *)iw;
1919
1920 const Vector2 *lr = lines.ptr();
1921
1922 const int POLY_HEIGHT = 16384;
1923
1924 for (int i = 0; i < lc / 2; i++) {
1925 vwptr[i * 12 + 0] = lr[i * 2 + 0].x;
1926 vwptr[i * 12 + 1] = lr[i * 2 + 0].y;
1927 vwptr[i * 12 + 2] = POLY_HEIGHT;
1928
1929 vwptr[i * 12 + 3] = lr[i * 2 + 1].x;
1930 vwptr[i * 12 + 4] = lr[i * 2 + 1].y;
1931 vwptr[i * 12 + 5] = POLY_HEIGHT;
1932
1933 vwptr[i * 12 + 6] = lr[i * 2 + 1].x;
1934 vwptr[i * 12 + 7] = lr[i * 2 + 1].y;
1935 vwptr[i * 12 + 8] = -POLY_HEIGHT;
1936
1937 vwptr[i * 12 + 9] = lr[i * 2 + 0].x;
1938 vwptr[i * 12 + 10] = lr[i * 2 + 0].y;
1939 vwptr[i * 12 + 11] = -POLY_HEIGHT;
1940
1941 iwptr[i * 6 + 0] = i * 4 + 0;
1942 iwptr[i * 6 + 1] = i * 4 + 1;
1943 iwptr[i * 6 + 2] = i * 4 + 2;
1944
1945 iwptr[i * 6 + 3] = i * 4 + 2;
1946 iwptr[i * 6 + 4] = i * 4 + 3;
1947 iwptr[i * 6 + 5] = i * 4 + 0;
1948 }
1949 }
1950
1951 if (oc->vertex_array == 0) {
1952 oc->line_point_count = lc;
1953 glGenVertexArrays(1, &oc->vertex_array);
1954 glBindVertexArray(oc->vertex_array);
1955 glGenBuffers(1, &oc->vertex_buffer);
1956 glBindBuffer(GL_ARRAY_BUFFER, oc->vertex_buffer);
1957
1958 GLES3::Utilities::get_singleton()->buffer_allocate_data(GL_ARRAY_BUFFER, oc->vertex_buffer, lc * 6 * sizeof(float), geometry.ptr(), GL_STATIC_DRAW, "Occluder polygon vertex buffer");
1959
1960 glEnableVertexAttribArray(RS::ARRAY_VERTEX);
1961 glVertexAttribPointer(RS::ARRAY_VERTEX, 3, GL_FLOAT, GL_FALSE, 3 * sizeof(float), nullptr);
1962
1963 glGenBuffers(1, &oc->index_buffer);
1964 glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, oc->index_buffer);
1965 GLES3::Utilities::get_singleton()->buffer_allocate_data(GL_ELEMENT_ARRAY_BUFFER, oc->index_buffer, 3 * lc * sizeof(uint16_t), indices.ptr(), GL_STATIC_DRAW, "Occluder polygon index buffer");
1966
1967 glBindVertexArray(0);
1968 } else {
1969 glBindBuffer(GL_ARRAY_BUFFER, oc->vertex_buffer);
1970 glBufferData(GL_ARRAY_BUFFER, lc * 6 * sizeof(float), geometry.ptr(), GL_STATIC_DRAW);
1971 glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, oc->index_buffer);
1972 glBufferData(GL_ELEMENT_ARRAY_BUFFER, 3 * lc * sizeof(uint16_t), indices.ptr(), GL_STATIC_DRAW);
1973 glBindBuffer(GL_ARRAY_BUFFER, 0);
1974 glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0);
1975 }
1976 }
1977
1978 // sdf
1979
1980 Vector<int> sdf_indices;
1981
1982 if (p_points.size()) {
1983 if (p_closed) {
1984 sdf_indices = Geometry2D::triangulate_polygon(p_points);
1985 oc->sdf_is_lines = false;
1986 } else {
1987 int max = p_points.size();
1988 sdf_indices.resize(max * 2);
1989
1990 int *iw = sdf_indices.ptrw();
1991 for (int i = 0; i < max; i++) {
1992 iw[i * 2 + 0] = i;
1993 iw[i * 2 + 1] = (i + 1) % max;
1994 }
1995 oc->sdf_is_lines = true;
1996 }
1997 }
1998
1999 if (oc->sdf_index_count != sdf_indices.size() && oc->sdf_point_count != p_points.size() && oc->sdf_vertex_array != 0) {
2000 glDeleteVertexArrays(1, &oc->sdf_vertex_array);
2001 GLES3::Utilities::get_singleton()->buffer_free_data(oc->sdf_vertex_buffer);
2002 GLES3::Utilities::get_singleton()->buffer_free_data(oc->sdf_index_buffer);
2003
2004 oc->sdf_vertex_array = 0;
2005 oc->sdf_vertex_buffer = 0;
2006 oc->sdf_index_buffer = 0;
2007
2008 oc->sdf_index_count = sdf_indices.size();
2009 oc->sdf_point_count = p_points.size();
2010 }
2011
2012 if (sdf_indices.size()) {
2013 if (oc->sdf_vertex_array == 0) {
2014 oc->sdf_index_count = sdf_indices.size();
2015 oc->sdf_point_count = p_points.size();
2016 glGenVertexArrays(1, &oc->sdf_vertex_array);
2017 glBindVertexArray(oc->sdf_vertex_array);
2018 glGenBuffers(1, &oc->sdf_vertex_buffer);
2019 glBindBuffer(GL_ARRAY_BUFFER, oc->sdf_vertex_buffer);
2020
2021 GLES3::Utilities::get_singleton()->buffer_allocate_data(GL_ARRAY_BUFFER, oc->sdf_vertex_buffer, oc->sdf_point_count * 2 * sizeof(float), p_points.to_byte_array().ptr(), GL_STATIC_DRAW, "Occluder polygon SDF vertex buffer");
2022
2023 glEnableVertexAttribArray(RS::ARRAY_VERTEX);
2024 glVertexAttribPointer(RS::ARRAY_VERTEX, 2, GL_FLOAT, GL_FALSE, 2 * sizeof(float), nullptr);
2025
2026 glGenBuffers(1, &oc->sdf_index_buffer);
2027 glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, oc->sdf_index_buffer);
2028 GLES3::Utilities::get_singleton()->buffer_allocate_data(GL_ELEMENT_ARRAY_BUFFER, oc->sdf_index_buffer, oc->sdf_index_count * sizeof(uint32_t), sdf_indices.to_byte_array().ptr(), GL_STATIC_DRAW, "Occluder polygon SDF index buffer");
2029
2030 glBindVertexArray(0);
2031 } else {
2032 glBindBuffer(GL_ARRAY_BUFFER, oc->sdf_vertex_buffer);
2033 glBufferData(GL_ARRAY_BUFFER, p_points.size() * 2 * sizeof(float), p_points.to_byte_array().ptr(), GL_STATIC_DRAW);
2034 glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, oc->sdf_index_buffer);
2035 glBufferData(GL_ELEMENT_ARRAY_BUFFER, sdf_indices.size() * sizeof(uint32_t), sdf_indices.to_byte_array().ptr(), GL_STATIC_DRAW);
2036 glBindBuffer(GL_ARRAY_BUFFER, 0);
2037 glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0);
2038 }
2039 }
2040}
2041
2042void RasterizerCanvasGLES3::occluder_polygon_set_cull_mode(RID p_occluder, RS::CanvasOccluderPolygonCullMode p_mode) {
2043 OccluderPolygon *oc = occluder_polygon_owner.get_or_null(p_occluder);
2044 ERR_FAIL_NULL(oc);
2045 oc->cull_mode = p_mode;
2046}
2047
2048void RasterizerCanvasGLES3::set_shadow_texture_size(int p_size) {
2049 GLES3::Config *config = GLES3::Config::get_singleton();
2050 p_size = nearest_power_of_2_templated(p_size);
2051
2052 if (p_size > config->max_texture_size) {
2053 p_size = config->max_texture_size;
2054 WARN_PRINT("Attempting to set CanvasItem shadow atlas size to " + itos(p_size) + " which is beyond limit of " + itos(config->max_texture_size) + "supported by hardware.");
2055 }
2056
2057 if (p_size == state.shadow_texture_size) {
2058 return;
2059 }
2060 state.shadow_texture_size = p_size;
2061
2062 if (state.shadow_fb != 0) {
2063 glDeleteFramebuffers(1, &state.shadow_fb);
2064 GLES3::Utilities::get_singleton()->texture_free_data(state.shadow_texture);
2065 glDeleteRenderbuffers(1, &state.shadow_depth_buffer);
2066 state.shadow_fb = 0;
2067 state.shadow_texture = 0;
2068 state.shadow_depth_buffer = 0;
2069 }
2070 _update_shadow_atlas();
2071}
2072
2073bool RasterizerCanvasGLES3::free(RID p_rid) {
2074 if (canvas_light_owner.owns(p_rid)) {
2075 CanvasLight *cl = canvas_light_owner.get_or_null(p_rid);
2076 ERR_FAIL_NULL_V(cl, false);
2077 canvas_light_owner.free(p_rid);
2078 } else if (occluder_polygon_owner.owns(p_rid)) {
2079 occluder_polygon_set_shape(p_rid, Vector<Vector2>(), false);
2080 occluder_polygon_owner.free(p_rid);
2081 } else {
2082 return false;
2083 }
2084
2085 return true;
2086}
2087
2088void RasterizerCanvasGLES3::update() {
2089}
2090
2091void RasterizerCanvasGLES3::canvas_begin(RID p_to_render_target, bool p_to_backbuffer) {
2092 GLES3::TextureStorage *texture_storage = GLES3::TextureStorage::get_singleton();
2093 GLES3::Config *config = GLES3::Config::get_singleton();
2094
2095 GLES3::RenderTarget *render_target = texture_storage->get_render_target(p_to_render_target);
2096
2097 if (p_to_backbuffer) {
2098 glBindFramebuffer(GL_FRAMEBUFFER, render_target->backbuffer_fbo);
2099 glActiveTexture(GL_TEXTURE0 + config->max_texture_image_units - 4);
2100 GLES3::Texture *tex = texture_storage->get_texture(texture_storage->texture_gl_get_default(GLES3::DEFAULT_GL_TEXTURE_WHITE));
2101 glBindTexture(GL_TEXTURE_2D, tex->tex_id);
2102 } else {
2103 glBindFramebuffer(GL_FRAMEBUFFER, render_target->fbo);
2104 glActiveTexture(GL_TEXTURE0 + config->max_texture_image_units - 4);
2105 glBindTexture(GL_TEXTURE_2D, render_target->backbuffer);
2106 }
2107
2108 if (render_target->is_transparent || p_to_backbuffer) {
2109 state.transparent_render_target = true;
2110 glBlendFuncSeparate(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA, GL_ONE, GL_ONE_MINUS_SRC_ALPHA);
2111 } else {
2112 state.transparent_render_target = false;
2113 glBlendFuncSeparate(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA, GL_ZERO, GL_ONE);
2114 }
2115
2116 if (render_target && render_target->clear_requested) {
2117 const Color &col = render_target->clear_color;
2118 glClearColor(col.r, col.g, col.b, render_target->is_transparent ? col.a : 1.0f);
2119
2120 glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT | GL_STENCIL_BUFFER_BIT);
2121 render_target->clear_requested = false;
2122 }
2123
2124 glActiveTexture(GL_TEXTURE0);
2125 GLES3::Texture *tex = texture_storage->get_texture(texture_storage->texture_gl_get_default(GLES3::DEFAULT_GL_TEXTURE_WHITE));
2126 glBindTexture(GL_TEXTURE_2D, tex->tex_id);
2127}
2128
2129void RasterizerCanvasGLES3::_bind_canvas_texture(RID p_texture, RS::CanvasItemTextureFilter p_base_filter, RS::CanvasItemTextureRepeat p_base_repeat) {
2130 GLES3::TextureStorage *texture_storage = GLES3::TextureStorage::get_singleton();
2131 GLES3::Config *config = GLES3::Config::get_singleton();
2132
2133 if (p_texture == RID()) {
2134 p_texture = default_canvas_texture;
2135 }
2136
2137 if (state.current_tex == p_texture && state.current_filter_mode == p_base_filter && state.current_repeat_mode == p_base_repeat) {
2138 return;
2139 }
2140
2141 state.current_tex = p_texture;
2142 state.current_filter_mode = p_base_filter;
2143 state.current_repeat_mode = p_base_repeat;
2144
2145 GLES3::CanvasTexture *ct = nullptr;
2146
2147 GLES3::Texture *t = texture_storage->get_texture(p_texture);
2148
2149 if (t) {
2150 ERR_FAIL_COND(!t->canvas_texture);
2151 ct = t->canvas_texture;
2152 if (t->render_target) {
2153 t->render_target->used_in_frame = true;
2154 }
2155 } else {
2156 ct = texture_storage->get_canvas_texture(p_texture);
2157 }
2158
2159 if (!ct) {
2160 // Invalid Texture RID.
2161 _bind_canvas_texture(default_canvas_texture, p_base_filter, p_base_repeat);
2162 return;
2163 }
2164
2165 RS::CanvasItemTextureFilter filter = ct->texture_filter != RS::CANVAS_ITEM_TEXTURE_FILTER_DEFAULT ? ct->texture_filter : p_base_filter;
2166 ERR_FAIL_COND(filter == RS::CANVAS_ITEM_TEXTURE_FILTER_DEFAULT);
2167
2168 RS::CanvasItemTextureRepeat repeat = ct->texture_repeat != RS::CANVAS_ITEM_TEXTURE_REPEAT_DEFAULT ? ct->texture_repeat : p_base_repeat;
2169 ERR_FAIL_COND(repeat == RS::CANVAS_ITEM_TEXTURE_REPEAT_DEFAULT);
2170
2171 GLES3::Texture *texture = texture_storage->get_texture(ct->diffuse);
2172
2173 if (!texture) {
2174 GLES3::Texture *tex = texture_storage->get_texture(texture_storage->texture_gl_get_default(GLES3::DEFAULT_GL_TEXTURE_WHITE));
2175 glActiveTexture(GL_TEXTURE0);
2176 glBindTexture(GL_TEXTURE_2D, tex->tex_id);
2177 } else {
2178 glActiveTexture(GL_TEXTURE0);
2179 glBindTexture(GL_TEXTURE_2D, texture->tex_id);
2180 texture->gl_set_filter(filter);
2181 texture->gl_set_repeat(repeat);
2182 if (texture->render_target) {
2183 texture->render_target->used_in_frame = true;
2184 }
2185 }
2186
2187 GLES3::Texture *normal_map = texture_storage->get_texture(ct->normal_map);
2188
2189 if (!normal_map) {
2190 glActiveTexture(GL_TEXTURE0 + config->max_texture_image_units - 6);
2191 GLES3::Texture *tex = texture_storage->get_texture(texture_storage->texture_gl_get_default(GLES3::DEFAULT_GL_TEXTURE_NORMAL));
2192 glBindTexture(GL_TEXTURE_2D, tex->tex_id);
2193 } else {
2194 glActiveTexture(GL_TEXTURE0 + config->max_texture_image_units - 6);
2195 glBindTexture(GL_TEXTURE_2D, normal_map->tex_id);
2196 normal_map->gl_set_filter(filter);
2197 normal_map->gl_set_repeat(repeat);
2198 if (normal_map->render_target) {
2199 normal_map->render_target->used_in_frame = true;
2200 }
2201 }
2202
2203 GLES3::Texture *specular_map = texture_storage->get_texture(ct->specular);
2204
2205 if (!specular_map) {
2206 glActiveTexture(GL_TEXTURE0 + config->max_texture_image_units - 7);
2207 GLES3::Texture *tex = texture_storage->get_texture(texture_storage->texture_gl_get_default(GLES3::DEFAULT_GL_TEXTURE_WHITE));
2208 glBindTexture(GL_TEXTURE_2D, tex->tex_id);
2209 } else {
2210 glActiveTexture(GL_TEXTURE0 + config->max_texture_image_units - 7);
2211 glBindTexture(GL_TEXTURE_2D, specular_map->tex_id);
2212 specular_map->gl_set_filter(filter);
2213 specular_map->gl_set_repeat(repeat);
2214 if (specular_map->render_target) {
2215 specular_map->render_target->used_in_frame = true;
2216 }
2217 }
2218}
2219
2220void RasterizerCanvasGLES3::_prepare_canvas_texture(RID p_texture, RS::CanvasItemTextureFilter p_base_filter, RS::CanvasItemTextureRepeat p_base_repeat, uint32_t &r_index, Size2 &r_texpixel_size) {
2221 GLES3::TextureStorage *texture_storage = GLES3::TextureStorage::get_singleton();
2222
2223 if (p_texture == RID()) {
2224 p_texture = default_canvas_texture;
2225 }
2226
2227 GLES3::CanvasTexture *ct = nullptr;
2228
2229 GLES3::Texture *t = texture_storage->get_texture(p_texture);
2230
2231 if (t) {
2232 //regular texture
2233 if (!t->canvas_texture) {
2234 t->canvas_texture = memnew(GLES3::CanvasTexture);
2235 t->canvas_texture->diffuse = p_texture;
2236 }
2237
2238 ct = t->canvas_texture;
2239 } else {
2240 ct = texture_storage->get_canvas_texture(p_texture);
2241 }
2242
2243 if (!ct) {
2244 // Invalid Texture RID.
2245 _prepare_canvas_texture(default_canvas_texture, p_base_filter, p_base_repeat, r_index, r_texpixel_size);
2246 return;
2247 }
2248
2249 GLES3::Texture *texture = texture_storage->get_texture(ct->diffuse);
2250 Size2i size_cache;
2251 if (!texture) {
2252 ct->diffuse = texture_storage->texture_gl_get_default(GLES3::DEFAULT_GL_TEXTURE_WHITE);
2253 GLES3::Texture *tex = texture_storage->get_texture(ct->diffuse);
2254 size_cache = Size2i(tex->width, tex->height);
2255 } else {
2256 size_cache = Size2i(texture->width, texture->height);
2257 }
2258
2259 GLES3::Texture *normal_map = texture_storage->get_texture(ct->normal_map);
2260
2261 if (ct->specular_color.a < 0.999) {
2262 state.instance_data_array[r_index].flags |= FLAGS_DEFAULT_SPECULAR_MAP_USED;
2263 } else {
2264 state.instance_data_array[r_index].flags &= ~FLAGS_DEFAULT_SPECULAR_MAP_USED;
2265 }
2266
2267 if (normal_map) {
2268 state.instance_data_array[r_index].flags |= FLAGS_DEFAULT_NORMAL_MAP_USED;
2269 } else {
2270 state.instance_data_array[r_index].flags &= ~FLAGS_DEFAULT_NORMAL_MAP_USED;
2271 }
2272
2273 state.instance_data_array[r_index].specular_shininess = uint32_t(CLAMP(ct->specular_color.a * 255.0, 0, 255)) << 24;
2274 state.instance_data_array[r_index].specular_shininess |= uint32_t(CLAMP(ct->specular_color.b * 255.0, 0, 255)) << 16;
2275 state.instance_data_array[r_index].specular_shininess |= uint32_t(CLAMP(ct->specular_color.g * 255.0, 0, 255)) << 8;
2276 state.instance_data_array[r_index].specular_shininess |= uint32_t(CLAMP(ct->specular_color.r * 255.0, 0, 255));
2277
2278 r_texpixel_size.x = 1.0 / float(size_cache.x);
2279 r_texpixel_size.y = 1.0 / float(size_cache.y);
2280
2281 state.instance_data_array[r_index].color_texture_pixel_size[0] = r_texpixel_size.x;
2282 state.instance_data_array[r_index].color_texture_pixel_size[1] = r_texpixel_size.y;
2283}
2284
2285void RasterizerCanvasGLES3::reset_canvas() {
2286 glDisable(GL_CULL_FACE);
2287 glDisable(GL_DEPTH_TEST);
2288 glDisable(GL_SCISSOR_TEST);
2289 glEnable(GL_BLEND);
2290 glBlendEquation(GL_FUNC_ADD);
2291 glBlendFuncSeparate(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA, GL_ZERO, GL_ONE);
2292
2293 glActiveTexture(GL_TEXTURE0 + GLES3::Config::get_singleton()->max_texture_image_units - 2);
2294 glBindTexture(GL_TEXTURE_2D, 0);
2295 glActiveTexture(GL_TEXTURE0 + GLES3::Config::get_singleton()->max_texture_image_units - 3);
2296 glBindTexture(GL_TEXTURE_2D, 0);
2297 glActiveTexture(GL_TEXTURE0);
2298
2299 glBindBuffer(GL_ARRAY_BUFFER, 0);
2300 glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0);
2301}
2302
2303void RasterizerCanvasGLES3::draw_lens_distortion_rect(const Rect2 &p_rect, float p_k1, float p_k2, const Vector2 &p_eye_center, float p_oversample) {
2304}
2305
2306RendererCanvasRender::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) {
2307 // We interleave the vertex data into one big VBO to improve cache coherence
2308 uint32_t vertex_count = p_points.size();
2309 uint32_t stride = 2;
2310 if ((uint32_t)p_colors.size() == vertex_count) {
2311 stride += 4;
2312 }
2313 if ((uint32_t)p_uvs.size() == vertex_count) {
2314 stride += 2;
2315 }
2316 if ((uint32_t)p_bones.size() == vertex_count * 4 && (uint32_t)p_weights.size() == vertex_count * 4) {
2317 stride += 4;
2318 }
2319
2320 PolygonBuffers pb;
2321 glGenBuffers(1, &pb.vertex_buffer);
2322 glGenVertexArrays(1, &pb.vertex_array);
2323 glBindVertexArray(pb.vertex_array);
2324 pb.count = vertex_count;
2325 pb.index_buffer = 0;
2326
2327 uint32_t buffer_size = stride * p_points.size();
2328
2329 Vector<uint8_t> polygon_buffer;
2330 polygon_buffer.resize(buffer_size * sizeof(float));
2331 {
2332 glBindBuffer(GL_ARRAY_BUFFER, pb.vertex_buffer);
2333 uint8_t *r = polygon_buffer.ptrw();
2334 float *fptr = reinterpret_cast<float *>(r);
2335 uint32_t *uptr = (uint32_t *)r;
2336 uint32_t base_offset = 0;
2337 {
2338 // Always uses vertex positions
2339 glEnableVertexAttribArray(RS::ARRAY_VERTEX);
2340 glVertexAttribPointer(RS::ARRAY_VERTEX, 2, GL_FLOAT, GL_FALSE, stride * sizeof(float), nullptr);
2341 const Vector2 *points_ptr = p_points.ptr();
2342
2343 for (uint32_t i = 0; i < vertex_count; i++) {
2344 fptr[base_offset + i * stride + 0] = points_ptr[i].x;
2345 fptr[base_offset + i * stride + 1] = points_ptr[i].y;
2346 }
2347
2348 base_offset += 2;
2349 }
2350
2351 // Next add colors
2352 if ((uint32_t)p_colors.size() == vertex_count) {
2353 glEnableVertexAttribArray(RS::ARRAY_COLOR);
2354 glVertexAttribPointer(RS::ARRAY_COLOR, 4, GL_FLOAT, GL_FALSE, stride * sizeof(float), CAST_INT_TO_UCHAR_PTR(base_offset * sizeof(float)));
2355
2356 const Color *color_ptr = p_colors.ptr();
2357
2358 for (uint32_t i = 0; i < vertex_count; i++) {
2359 fptr[base_offset + i * stride + 0] = color_ptr[i].r;
2360 fptr[base_offset + i * stride + 1] = color_ptr[i].g;
2361 fptr[base_offset + i * stride + 2] = color_ptr[i].b;
2362 fptr[base_offset + i * stride + 3] = color_ptr[i].a;
2363 }
2364 base_offset += 4;
2365 } else {
2366 glDisableVertexAttribArray(RS::ARRAY_COLOR);
2367 pb.color_disabled = true;
2368 pb.color = p_colors.size() == 1 ? p_colors[0] : Color(1.0, 1.0, 1.0, 1.0);
2369 }
2370
2371 if ((uint32_t)p_uvs.size() == vertex_count) {
2372 glEnableVertexAttribArray(RS::ARRAY_TEX_UV);
2373 glVertexAttribPointer(RS::ARRAY_TEX_UV, 2, GL_FLOAT, GL_FALSE, stride * sizeof(float), CAST_INT_TO_UCHAR_PTR(base_offset * sizeof(float)));
2374
2375 const Vector2 *uv_ptr = p_uvs.ptr();
2376
2377 for (uint32_t i = 0; i < vertex_count; i++) {
2378 fptr[base_offset + i * stride + 0] = uv_ptr[i].x;
2379 fptr[base_offset + i * stride + 1] = uv_ptr[i].y;
2380 }
2381
2382 base_offset += 2;
2383 } else {
2384 glDisableVertexAttribArray(RS::ARRAY_TEX_UV);
2385 }
2386
2387 if ((uint32_t)p_indices.size() == vertex_count * 4 && (uint32_t)p_weights.size() == vertex_count * 4) {
2388 glEnableVertexAttribArray(RS::ARRAY_BONES);
2389 glVertexAttribPointer(RS::ARRAY_BONES, 4, GL_UNSIGNED_INT, GL_FALSE, stride * sizeof(float), CAST_INT_TO_UCHAR_PTR(base_offset * sizeof(float)));
2390
2391 const int *bone_ptr = p_bones.ptr();
2392
2393 for (uint32_t i = 0; i < vertex_count; i++) {
2394 uint16_t *bone16w = (uint16_t *)&uptr[base_offset + i * stride];
2395
2396 bone16w[0] = bone_ptr[i * 4 + 0];
2397 bone16w[1] = bone_ptr[i * 4 + 1];
2398 bone16w[2] = bone_ptr[i * 4 + 2];
2399 bone16w[3] = bone_ptr[i * 4 + 3];
2400 }
2401
2402 base_offset += 2;
2403 } else {
2404 glDisableVertexAttribArray(RS::ARRAY_BONES);
2405 }
2406
2407 if ((uint32_t)p_weights.size() == vertex_count * 4) {
2408 glEnableVertexAttribArray(RS::ARRAY_WEIGHTS);
2409 glVertexAttribPointer(RS::ARRAY_WEIGHTS, 4, GL_FLOAT, GL_FALSE, stride * sizeof(float), CAST_INT_TO_UCHAR_PTR(base_offset * sizeof(float)));
2410
2411 const float *weight_ptr = p_weights.ptr();
2412
2413 for (uint32_t i = 0; i < vertex_count; i++) {
2414 uint16_t *weight16w = (uint16_t *)&uptr[base_offset + i * stride];
2415
2416 weight16w[0] = CLAMP(weight_ptr[i * 4 + 0] * 65535, 0, 65535);
2417 weight16w[1] = CLAMP(weight_ptr[i * 4 + 1] * 65535, 0, 65535);
2418 weight16w[2] = CLAMP(weight_ptr[i * 4 + 2] * 65535, 0, 65535);
2419 weight16w[3] = CLAMP(weight_ptr[i * 4 + 3] * 65535, 0, 65535);
2420 }
2421
2422 base_offset += 2;
2423 } else {
2424 glDisableVertexAttribArray(RS::ARRAY_WEIGHTS);
2425 }
2426
2427 ERR_FAIL_COND_V(base_offset != stride, 0);
2428 GLES3::Utilities::get_singleton()->buffer_allocate_data(GL_ARRAY_BUFFER, pb.vertex_buffer, vertex_count * stride * sizeof(float), polygon_buffer.ptr(), GL_STATIC_DRAW, "Polygon 2D vertex buffer");
2429 }
2430
2431 if (p_indices.size()) {
2432 //create indices, as indices were requested
2433 Vector<uint8_t> index_buffer;
2434 index_buffer.resize(p_indices.size() * sizeof(int32_t));
2435 {
2436 uint8_t *w = index_buffer.ptrw();
2437 memcpy(w, p_indices.ptr(), sizeof(int32_t) * p_indices.size());
2438 }
2439 glGenBuffers(1, &pb.index_buffer);
2440 glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, pb.index_buffer);
2441 GLES3::Utilities::get_singleton()->buffer_allocate_data(GL_ELEMENT_ARRAY_BUFFER, pb.index_buffer, p_indices.size() * 4, index_buffer.ptr(), GL_STATIC_DRAW, "Polygon 2D index buffer");
2442 pb.count = p_indices.size();
2443 }
2444
2445 glBindVertexArray(0);
2446 glBindBuffer(GL_ARRAY_BUFFER, 0);
2447 glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0);
2448
2449 PolygonID id = polygon_buffers.last_id++;
2450
2451 polygon_buffers.polygons[id] = pb;
2452
2453 return id;
2454}
2455
2456void RasterizerCanvasGLES3::free_polygon(PolygonID p_polygon) {
2457 PolygonBuffers *pb_ptr = polygon_buffers.polygons.getptr(p_polygon);
2458 ERR_FAIL_NULL(pb_ptr);
2459
2460 PolygonBuffers &pb = *pb_ptr;
2461
2462 if (pb.index_buffer != 0) {
2463 GLES3::Utilities::get_singleton()->buffer_free_data(pb.index_buffer);
2464 }
2465
2466 glDeleteVertexArrays(1, &pb.vertex_array);
2467 GLES3::Utilities::get_singleton()->buffer_free_data(pb.vertex_buffer);
2468
2469 polygon_buffers.polygons.erase(p_polygon);
2470}
2471
2472// Creates a new uniform buffer and uses it right away
2473// This expands the instance buffer continually
2474// In theory allocations can reach as high as number of windows * 3 frames
2475// because OpenGL can start rendering subsequent frames before finishing the current one
2476void RasterizerCanvasGLES3::_allocate_instance_data_buffer() {
2477 GLuint new_buffers[3];
2478 glGenBuffers(3, new_buffers);
2479 // Batch UBO.
2480 glBindBuffer(GL_ARRAY_BUFFER, new_buffers[0]);
2481 GLES3::Utilities::get_singleton()->buffer_allocate_data(GL_ARRAY_BUFFER, new_buffers[0], data.max_instance_buffer_size, nullptr, GL_STREAM_DRAW, "2D Batch UBO[" + itos(state.current_data_buffer_index) + "][0]");
2482 // Light uniform buffer.
2483 glBindBuffer(GL_UNIFORM_BUFFER, new_buffers[1]);
2484 GLES3::Utilities::get_singleton()->buffer_allocate_data(GL_UNIFORM_BUFFER, new_buffers[1], sizeof(LightUniform) * data.max_lights_per_render, nullptr, GL_STREAM_DRAW, "2D Lights UBO[" + itos(state.current_data_buffer_index) + "]");
2485 // State buffer.
2486 glBindBuffer(GL_UNIFORM_BUFFER, new_buffers[2]);
2487 GLES3::Utilities::get_singleton()->buffer_allocate_data(GL_UNIFORM_BUFFER, new_buffers[2], sizeof(StateBuffer), nullptr, GL_STREAM_DRAW, "2D State UBO[" + itos(state.current_data_buffer_index) + "]");
2488
2489 state.current_data_buffer_index = (state.current_data_buffer_index + 1);
2490 DataBuffer db;
2491 db.instance_buffers.push_back(new_buffers[0]);
2492 db.light_ubo = new_buffers[1];
2493 db.state_ubo = new_buffers[2];
2494 db.last_frame_used = RSG::rasterizer->get_frame_number();
2495 state.canvas_instance_data_buffers.insert(state.current_data_buffer_index, db);
2496 state.current_data_buffer_index = state.current_data_buffer_index % state.canvas_instance_data_buffers.size();
2497 glBindBuffer(GL_ARRAY_BUFFER, 0);
2498 glBindBuffer(GL_UNIFORM_BUFFER, 0);
2499}
2500void RasterizerCanvasGLES3::_allocate_instance_buffer() {
2501 state.current_instance_buffer_index++;
2502
2503 if (int(state.current_instance_buffer_index) < state.canvas_instance_data_buffers[state.current_data_buffer_index].instance_buffers.size()) {
2504 // We already allocated another buffer in a previous frame, so we can just use it.
2505 return;
2506 }
2507
2508 GLuint new_buffer;
2509 glGenBuffers(1, &new_buffer);
2510
2511 glBindBuffer(GL_ARRAY_BUFFER, new_buffer);
2512 GLES3::Utilities::get_singleton()->buffer_allocate_data(GL_ARRAY_BUFFER, new_buffer, data.max_instance_buffer_size, nullptr, GL_STREAM_DRAW, "Batch UBO[" + itos(state.current_data_buffer_index) + "][" + itos(state.canvas_instance_data_buffers[state.current_data_buffer_index].instance_buffers.size()) + "]");
2513
2514 state.canvas_instance_data_buffers[state.current_data_buffer_index].instance_buffers.push_back(new_buffer);
2515
2516 glBindBuffer(GL_ARRAY_BUFFER, 0);
2517}
2518
2519void RasterizerCanvasGLES3::set_time(double p_time) {
2520 state.time = p_time;
2521}
2522
2523RasterizerCanvasGLES3 *RasterizerCanvasGLES3::singleton = nullptr;
2524
2525RasterizerCanvasGLES3 *RasterizerCanvasGLES3::get_singleton() {
2526 return singleton;
2527}
2528
2529RasterizerCanvasGLES3::RasterizerCanvasGLES3() {
2530 singleton = this;
2531 GLES3::TextureStorage *texture_storage = GLES3::TextureStorage::get_singleton();
2532 GLES3::MaterialStorage *material_storage = GLES3::MaterialStorage::get_singleton();
2533 GLES3::Config *config = GLES3::Config::get_singleton();
2534
2535 glVertexAttrib4f(RS::ARRAY_COLOR, 1.0, 1.0, 1.0, 1.0);
2536
2537 polygon_buffers.last_id = 1;
2538 // quad buffer
2539 {
2540 glGenBuffers(1, &data.canvas_quad_vertices);
2541 glBindBuffer(GL_ARRAY_BUFFER, data.canvas_quad_vertices);
2542
2543 const float qv[8] = {
2544 0, 0,
2545 0, 1,
2546 1, 1,
2547 1, 0
2548 };
2549
2550 glBufferData(GL_ARRAY_BUFFER, sizeof(float) * 8, qv, GL_STATIC_DRAW);
2551
2552 glBindBuffer(GL_ARRAY_BUFFER, 0);
2553
2554 glGenVertexArrays(1, &data.canvas_quad_array);
2555 glBindVertexArray(data.canvas_quad_array);
2556 glBindBuffer(GL_ARRAY_BUFFER, data.canvas_quad_vertices);
2557 glVertexAttribPointer(0, 2, GL_FLOAT, GL_FALSE, sizeof(float) * 2, nullptr);
2558 glEnableVertexAttribArray(0);
2559 glBindVertexArray(0);
2560 glBindBuffer(GL_ARRAY_BUFFER, 0); //unbind
2561 }
2562
2563 {
2564 //particle quad buffers
2565
2566 glGenBuffers(1, &data.particle_quad_vertices);
2567 glBindBuffer(GL_ARRAY_BUFFER, data.particle_quad_vertices);
2568 {
2569 //quad of size 1, with pivot on the center for particles, then regular UVS. Color is general plus fetched from particle
2570 const float qv[16] = {
2571 -0.5, -0.5,
2572 0.0, 0.0,
2573 -0.5, 0.5,
2574 0.0, 1.0,
2575 0.5, 0.5,
2576 1.0, 1.0,
2577 0.5, -0.5,
2578 1.0, 0.0
2579 };
2580
2581 glBufferData(GL_ARRAY_BUFFER, sizeof(float) * 16, qv, GL_STATIC_DRAW);
2582 }
2583
2584 glBindBuffer(GL_ARRAY_BUFFER, 0); //unbind
2585
2586 glGenVertexArrays(1, &data.particle_quad_array);
2587 glBindVertexArray(data.particle_quad_array);
2588 glBindBuffer(GL_ARRAY_BUFFER, data.particle_quad_vertices);
2589 glEnableVertexAttribArray(RS::ARRAY_VERTEX);
2590 glVertexAttribPointer(RS::ARRAY_VERTEX, 2, GL_FLOAT, GL_FALSE, sizeof(float) * 4, nullptr);
2591 glEnableVertexAttribArray(RS::ARRAY_TEX_UV);
2592 glVertexAttribPointer(RS::ARRAY_TEX_UV, 2, GL_FLOAT, GL_FALSE, sizeof(float) * 4, CAST_INT_TO_UCHAR_PTR(8));
2593 glBindVertexArray(0);
2594 glBindBuffer(GL_ARRAY_BUFFER, 0); //unbind
2595 }
2596
2597 // ninepatch buffers
2598 {
2599 // array buffer
2600 glGenBuffers(1, &data.ninepatch_vertices);
2601 glBindBuffer(GL_ARRAY_BUFFER, data.ninepatch_vertices);
2602
2603 glBufferData(GL_ARRAY_BUFFER, sizeof(float) * (16 + 16) * 2, nullptr, GL_DYNAMIC_DRAW);
2604
2605 glBindBuffer(GL_ARRAY_BUFFER, 0);
2606
2607 // element buffer
2608 glGenBuffers(1, &data.ninepatch_elements);
2609 glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, data.ninepatch_elements);
2610
2611#define _EIDX(y, x) (y * 4 + x)
2612 uint8_t elems[3 * 2 * 9] = {
2613 // first row
2614
2615 _EIDX(0, 0), _EIDX(0, 1), _EIDX(1, 1),
2616 _EIDX(1, 1), _EIDX(1, 0), _EIDX(0, 0),
2617
2618 _EIDX(0, 1), _EIDX(0, 2), _EIDX(1, 2),
2619 _EIDX(1, 2), _EIDX(1, 1), _EIDX(0, 1),
2620
2621 _EIDX(0, 2), _EIDX(0, 3), _EIDX(1, 3),
2622 _EIDX(1, 3), _EIDX(1, 2), _EIDX(0, 2),
2623
2624 // second row
2625
2626 _EIDX(1, 0), _EIDX(1, 1), _EIDX(2, 1),
2627 _EIDX(2, 1), _EIDX(2, 0), _EIDX(1, 0),
2628
2629 // the center one would be here, but we'll put it at the end
2630 // so it's easier to disable the center and be able to use
2631 // one draw call for both
2632
2633 _EIDX(1, 2), _EIDX(1, 3), _EIDX(2, 3),
2634 _EIDX(2, 3), _EIDX(2, 2), _EIDX(1, 2),
2635
2636 // third row
2637
2638 _EIDX(2, 0), _EIDX(2, 1), _EIDX(3, 1),
2639 _EIDX(3, 1), _EIDX(3, 0), _EIDX(2, 0),
2640
2641 _EIDX(2, 1), _EIDX(2, 2), _EIDX(3, 2),
2642 _EIDX(3, 2), _EIDX(3, 1), _EIDX(2, 1),
2643
2644 _EIDX(2, 2), _EIDX(2, 3), _EIDX(3, 3),
2645 _EIDX(3, 3), _EIDX(3, 2), _EIDX(2, 2),
2646
2647 // center field
2648
2649 _EIDX(1, 1), _EIDX(1, 2), _EIDX(2, 2),
2650 _EIDX(2, 2), _EIDX(2, 1), _EIDX(1, 1)
2651 };
2652#undef _EIDX
2653
2654 glBufferData(GL_ELEMENT_ARRAY_BUFFER, sizeof(elems), elems, GL_STATIC_DRAW);
2655
2656 glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0);
2657 }
2658
2659 int uniform_max_size = config->max_uniform_buffer_size;
2660 if (uniform_max_size < 65536) {
2661 data.max_lights_per_render = 64;
2662 } else {
2663 data.max_lights_per_render = 256;
2664 }
2665
2666 // Reserve 3 Uniform Buffers for instance data Frame N, N+1 and N+2
2667 data.max_instances_per_buffer = uint32_t(GLOBAL_GET("rendering/gl_compatibility/item_buffer_size"));
2668 data.max_instance_buffer_size = data.max_instances_per_buffer * sizeof(InstanceData); // 16,384 instances * 128 bytes = 2,097,152 bytes = 2,048 kb
2669 state.canvas_instance_data_buffers.resize(3);
2670 state.canvas_instance_batches.reserve(200);
2671
2672 for (int i = 0; i < 3; i++) {
2673 GLuint new_buffers[3];
2674 glGenBuffers(3, new_buffers);
2675 // Batch UBO.
2676 glBindBuffer(GL_ARRAY_BUFFER, new_buffers[0]);
2677 GLES3::Utilities::get_singleton()->buffer_allocate_data(GL_ARRAY_BUFFER, new_buffers[0], data.max_instance_buffer_size, nullptr, GL_STREAM_DRAW, "Batch UBO[0][0]");
2678 // Light uniform buffer.
2679 glBindBuffer(GL_UNIFORM_BUFFER, new_buffers[1]);
2680 GLES3::Utilities::get_singleton()->buffer_allocate_data(GL_UNIFORM_BUFFER, new_buffers[1], sizeof(LightUniform) * data.max_lights_per_render, nullptr, GL_STREAM_DRAW, "2D lights UBO[0]");
2681 // State buffer.
2682 glBindBuffer(GL_UNIFORM_BUFFER, new_buffers[2]);
2683 GLES3::Utilities::get_singleton()->buffer_allocate_data(GL_UNIFORM_BUFFER, new_buffers[2], sizeof(StateBuffer), nullptr, GL_STREAM_DRAW, "2D state UBO[0]");
2684 DataBuffer db;
2685 db.instance_buffers.push_back(new_buffers[0]);
2686 db.light_ubo = new_buffers[1];
2687 db.state_ubo = new_buffers[2];
2688 db.last_frame_used = 0;
2689 db.fence = glFenceSync(GL_SYNC_GPU_COMMANDS_COMPLETE, 0);
2690 state.canvas_instance_data_buffers[i] = db;
2691 }
2692 glBindBuffer(GL_ARRAY_BUFFER, 0);
2693 glBindBuffer(GL_UNIFORM_BUFFER, 0);
2694
2695 state.instance_data_array = memnew_arr(InstanceData, data.max_instances_per_buffer);
2696 state.light_uniforms = memnew_arr(LightUniform, data.max_lights_per_render);
2697
2698 {
2699 const uint32_t indices[6] = { 0, 2, 1, 3, 2, 0 };
2700 glGenVertexArrays(1, &data.indexed_quad_array);
2701 glBindVertexArray(data.indexed_quad_array);
2702 glBindBuffer(GL_ARRAY_BUFFER, data.canvas_quad_vertices);
2703 glGenBuffers(1, &data.indexed_quad_buffer);
2704 glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, data.indexed_quad_buffer);
2705 glBufferData(GL_ELEMENT_ARRAY_BUFFER, sizeof(uint32_t) * 6, indices, GL_STATIC_DRAW);
2706 glBindVertexArray(0);
2707 }
2708
2709 String global_defines;
2710 global_defines += "#define MAX_GLOBAL_SHADER_UNIFORMS 256\n"; // TODO: this is arbitrary for now
2711 global_defines += "#define MAX_LIGHTS " + itos(data.max_lights_per_render) + "\n";
2712
2713 GLES3::MaterialStorage::get_singleton()->shaders.canvas_shader.initialize(global_defines, 1);
2714 data.canvas_shader_default_version = GLES3::MaterialStorage::get_singleton()->shaders.canvas_shader.version_create();
2715
2716 state.shadow_texture_size = GLOBAL_GET("rendering/2d/shadow_atlas/size");
2717 shadow_render.shader.initialize();
2718 shadow_render.shader_version = shadow_render.shader.version_create();
2719
2720 {
2721 default_canvas_group_shader = material_storage->shader_allocate();
2722 material_storage->shader_initialize(default_canvas_group_shader);
2723
2724 material_storage->shader_set_code(default_canvas_group_shader, R"(
2725// Default CanvasGroup shader.
2726
2727shader_type canvas_item;
2728render_mode unshaded;
2729
2730uniform sampler2D screen_texture : hint_screen_texture, repeat_disable, filter_nearest;
2731
2732void fragment() {
2733 vec4 c = textureLod(screen_texture, SCREEN_UV, 0.0);
2734
2735 if (c.a > 0.0001) {
2736 c.rgb /= c.a;
2737 }
2738
2739 COLOR *= c;
2740}
2741)");
2742 default_canvas_group_material = material_storage->material_allocate();
2743 material_storage->material_initialize(default_canvas_group_material);
2744
2745 material_storage->material_set_shader(default_canvas_group_material, default_canvas_group_shader);
2746 }
2747
2748 {
2749 default_clip_children_shader = material_storage->shader_allocate();
2750 material_storage->shader_initialize(default_clip_children_shader);
2751
2752 material_storage->shader_set_code(default_clip_children_shader, R"(
2753// Default clip children shader.
2754
2755shader_type canvas_item;
2756render_mode unshaded;
2757
2758uniform sampler2D screen_texture : hint_screen_texture, repeat_disable, filter_nearest;
2759
2760void fragment() {
2761 vec4 c = textureLod(screen_texture, SCREEN_UV, 0.0);
2762 COLOR.rgb = c.rgb;
2763}
2764)");
2765 default_clip_children_material = material_storage->material_allocate();
2766 material_storage->material_initialize(default_clip_children_material);
2767
2768 material_storage->material_set_shader(default_clip_children_material, default_clip_children_shader);
2769 }
2770
2771 default_canvas_texture = texture_storage->canvas_texture_allocate();
2772 texture_storage->canvas_texture_initialize(default_canvas_texture);
2773
2774 state.time = 0.0;
2775}
2776
2777RasterizerCanvasGLES3::~RasterizerCanvasGLES3() {
2778 singleton = nullptr;
2779
2780 GLES3::MaterialStorage *material_storage = GLES3::MaterialStorage::get_singleton();
2781 material_storage->shaders.canvas_shader.version_free(data.canvas_shader_default_version);
2782 shadow_render.shader.version_free(shadow_render.shader_version);
2783 material_storage->material_free(default_canvas_group_material);
2784 material_storage->shader_free(default_canvas_group_shader);
2785 material_storage->material_free(default_clip_children_material);
2786 material_storage->shader_free(default_clip_children_shader);
2787 singleton = nullptr;
2788
2789 glDeleteBuffers(1, &data.canvas_quad_vertices);
2790 glDeleteVertexArrays(1, &data.canvas_quad_array);
2791
2792 glDeleteBuffers(1, &data.canvas_quad_vertices);
2793 glDeleteVertexArrays(1, &data.canvas_quad_array);
2794
2795 GLES3::TextureStorage::get_singleton()->canvas_texture_free(default_canvas_texture);
2796 memdelete_arr(state.instance_data_array);
2797 memdelete_arr(state.light_uniforms);
2798
2799 if (state.shadow_fb != 0) {
2800 glDeleteFramebuffers(1, &state.shadow_fb);
2801 GLES3::Utilities::get_singleton()->texture_free_data(state.shadow_texture);
2802 glDeleteRenderbuffers(1, &state.shadow_depth_buffer);
2803 state.shadow_fb = 0;
2804 state.shadow_texture = 0;
2805 state.shadow_depth_buffer = 0;
2806 }
2807
2808 for (uint32_t i = 0; i < state.canvas_instance_data_buffers.size(); i++) {
2809 for (int j = 0; j < state.canvas_instance_data_buffers[i].instance_buffers.size(); j++) {
2810 if (state.canvas_instance_data_buffers[i].instance_buffers[j]) {
2811 GLES3::Utilities::get_singleton()->buffer_free_data(state.canvas_instance_data_buffers[i].instance_buffers[j]);
2812 }
2813 }
2814 if (state.canvas_instance_data_buffers[i].light_ubo) {
2815 GLES3::Utilities::get_singleton()->buffer_free_data(state.canvas_instance_data_buffers[i].light_ubo);
2816 }
2817 if (state.canvas_instance_data_buffers[i].state_ubo) {
2818 GLES3::Utilities::get_singleton()->buffer_free_data(state.canvas_instance_data_buffers[i].state_ubo);
2819 }
2820 }
2821}
2822
2823#endif // GLES3_ENABLED
2824