diff --git a/doc/classes/ProjectSettings.xml b/doc/classes/ProjectSettings.xml
index c6553b4d75d..c19d6d1a99d 100644
--- a/doc/classes/ProjectSettings.xml
+++ b/doc/classes/ProjectSettings.xml
@@ -1062,6 +1062,10 @@
Shaders have a time variable that constantly increases. At some point, it needs to be rolled back to zero to avoid precision errors on shader animations. This setting specifies when (in seconds).
+
+ Choose between default mode where corner scalings are preserved matching the artwork, and scaling mode.
+ Not available in GLES3 when [member rendering/batching/options/use_batching] is off.
+
Some NVIDIA GPU drivers have a bug which produces flickering issues for the [code]draw_rect[/code] method, especially as used in [TileMap]. Refer to [url=https://github.com/godotengine/godot/issues/9913]GitHub issue 9913[/url] for details.
If [code]true[/code], this option enables a "safe" code path for such NVIDIA GPUs at the cost of performance. This option affects GLES2 and GLES3 rendering, but only on desktop platforms.
@@ -1070,6 +1074,10 @@
If [code]true[/code], forces snapping of polygons to pixels in 2D rendering. May help in some pixel art styles.
Consider using the project setting [member rendering/batching/precision/uv_contract] to prevent artifacts.
+
+ If [code]true[/code], performs 2d skinning on the CPU rather than the GPU. This provides greater compatibility with a wide range of hardware, and also may be faster in some circumstances.
+ Currently only available when [member rendering/batching/options/use_batching] is active.
+
If [code]true[/code], allocates the main framebuffer with high dynamic range. High dynamic range allows the use of [Color] values greater than 1.
[b]Note:[/b] Only available on the GLES3 backend.
diff --git a/drivers/SCsub b/drivers/SCsub
index 6cd3150741a..77531978125 100644
--- a/drivers/SCsub
+++ b/drivers/SCsub
@@ -26,6 +26,7 @@ SConscript("winmidi/SCsub")
if env["platform"] != "server":
SConscript("gles3/SCsub")
SConscript("gles2/SCsub")
+ SConscript("gles_common/SCsub")
SConscript("gl_context/SCsub")
else:
SConscript("dummy/SCsub")
diff --git a/drivers/gles2/rasterizer_canvas_base_gles2.cpp b/drivers/gles2/rasterizer_canvas_base_gles2.cpp
index dc3ddef886f..ab78ec3b855 100644
--- a/drivers/gles2/rasterizer_canvas_base_gles2.cpp
+++ b/drivers/gles2/rasterizer_canvas_base_gles2.cpp
@@ -56,7 +56,12 @@ void RasterizerCanvasBaseGLES2::canvas_begin() {
// always start with light_angle unset
state.using_light_angle = false;
- state.canvas_shader.set_conditional(CanvasShaderGLES2::USE_LIGHT_ANGLE, false);
+ state.using_large_vertex = false;
+ state.using_modulate = false;
+
+ state.canvas_shader.set_conditional(CanvasShaderGLES2::USE_ATTRIB_LIGHT_ANGLE, false);
+ state.canvas_shader.set_conditional(CanvasShaderGLES2::USE_ATTRIB_MODULATE, false);
+ state.canvas_shader.set_conditional(CanvasShaderGLES2::USE_ATTRIB_LARGE_VERTEX, false);
state.canvas_shader.bind();
int viewport_x, viewport_y, viewport_width, viewport_height;
@@ -160,13 +165,23 @@ void RasterizerCanvasBaseGLES2::draw_generic_textured_rect(const Rect2 &p_rect,
glDrawArrays(GL_TRIANGLE_FAN, 0, 4);
}
-void RasterizerCanvasBaseGLES2::_set_texture_rect_mode(bool p_texture_rect, bool p_light_angle) {
+void RasterizerCanvasBaseGLES2::_set_texture_rect_mode(bool p_texture_rect, bool p_light_angle, bool p_modulate, bool p_large_vertex) {
// always set this directly (this could be state checked)
state.canvas_shader.set_conditional(CanvasShaderGLES2::USE_TEXTURE_RECT, p_texture_rect);
if (state.using_light_angle != p_light_angle) {
state.using_light_angle = p_light_angle;
- state.canvas_shader.set_conditional(CanvasShaderGLES2::USE_LIGHT_ANGLE, p_light_angle);
+ state.canvas_shader.set_conditional(CanvasShaderGLES2::USE_ATTRIB_LIGHT_ANGLE, p_light_angle);
+ }
+
+ if (state.using_modulate != p_modulate) {
+ state.using_modulate = p_modulate;
+ state.canvas_shader.set_conditional(CanvasShaderGLES2::USE_ATTRIB_MODULATE, p_modulate);
+ }
+
+ if (state.using_large_vertex != p_large_vertex) {
+ state.using_large_vertex = p_large_vertex;
+ state.canvas_shader.set_conditional(CanvasShaderGLES2::USE_ATTRIB_LARGE_VERTEX, p_large_vertex);
}
}
@@ -431,8 +446,21 @@ void RasterizerCanvasBaseGLES2::_draw_polygon(const int *p_indices, int p_index_
glBindBuffer(GL_ARRAY_BUFFER, data.polygon_buffer);
+ // precalculate the buffer size ahead of time instead of making multiple glBufferSubData calls
+ uint32_t total_buffer_size = sizeof(Vector2) * p_vertex_count;
+ if (!p_singlecolor && p_colors) {
+ total_buffer_size += sizeof(Color) * p_vertex_count;
+ }
+ if (p_uvs) {
+ total_buffer_size += sizeof(Vector2) * p_vertex_count;
+ }
+ if (p_weights && p_bones) {
+ total_buffer_size += sizeof(float) * 4 * p_vertex_count; // weights
+ total_buffer_size += sizeof(int) * 4 * p_vertex_count; // bones
+ }
+
uint32_t buffer_ofs = 0;
- storage->buffer_orphan_and_upload(data.polygon_buffer_size, 0, sizeof(Vector2) * p_vertex_count, p_vertices);
+ storage->buffer_orphan_and_upload(data.polygon_buffer_size, 0, total_buffer_size, p_vertices);
glEnableVertexAttribArray(VS::ARRAY_VERTEX);
glVertexAttribPointer(VS::ARRAY_VERTEX, 2, GL_FLOAT, GL_FALSE, sizeof(Vector2), NULL);
@@ -446,14 +474,12 @@ void RasterizerCanvasBaseGLES2::_draw_polygon(const int *p_indices, int p_index_
glDisableVertexAttribArray(VS::ARRAY_COLOR);
glVertexAttrib4f(VS::ARRAY_COLOR, 1, 1, 1, 1);
} else {
- glBufferSubData(GL_ARRAY_BUFFER, buffer_ofs, sizeof(Color) * p_vertex_count, p_colors);
glEnableVertexAttribArray(VS::ARRAY_COLOR);
glVertexAttribPointer(VS::ARRAY_COLOR, 4, GL_FLOAT, GL_FALSE, sizeof(Color), CAST_INT_TO_UCHAR_PTR(buffer_ofs));
buffer_ofs += sizeof(Color) * p_vertex_count;
}
if (p_uvs) {
- glBufferSubData(GL_ARRAY_BUFFER, buffer_ofs, sizeof(Vector2) * p_vertex_count, p_uvs);
glEnableVertexAttribArray(VS::ARRAY_TEX_UV);
glVertexAttribPointer(VS::ARRAY_TEX_UV, 2, GL_FLOAT, GL_FALSE, sizeof(Vector2), CAST_INT_TO_UCHAR_PTR(buffer_ofs));
buffer_ofs += sizeof(Vector2) * p_vertex_count;
@@ -462,12 +488,10 @@ void RasterizerCanvasBaseGLES2::_draw_polygon(const int *p_indices, int p_index_
}
if (p_weights && p_bones) {
- glBufferSubData(GL_ARRAY_BUFFER, buffer_ofs, sizeof(float) * 4 * p_vertex_count, p_weights);
glEnableVertexAttribArray(VS::ARRAY_WEIGHTS);
glVertexAttribPointer(VS::ARRAY_WEIGHTS, 4, GL_FLOAT, GL_FALSE, sizeof(float) * 4, CAST_INT_TO_UCHAR_PTR(buffer_ofs));
buffer_ofs += sizeof(float) * 4 * p_vertex_count;
- glBufferSubData(GL_ARRAY_BUFFER, buffer_ofs, sizeof(int) * 4 * p_vertex_count, p_bones);
glEnableVertexAttribArray(VS::ARRAY_BONES);
glVertexAttribPointer(VS::ARRAY_BONES, 4, GL_UNSIGNED_INT, GL_FALSE, sizeof(int) * 4, CAST_INT_TO_UCHAR_PTR(buffer_ofs));
buffer_ofs += sizeof(int) * 4 * p_vertex_count;
@@ -501,8 +525,17 @@ void RasterizerCanvasBaseGLES2::_draw_generic(GLuint p_primitive, int p_vertex_c
glBindBuffer(GL_ARRAY_BUFFER, data.polygon_buffer);
+ // precalculate the buffer size ahead of time instead of making multiple glBufferSubData calls
+ uint32_t total_buffer_size = sizeof(Vector2) * p_vertex_count;
+ if (!p_singlecolor && p_colors) {
+ total_buffer_size += sizeof(Color) * p_vertex_count;
+ }
+ if (p_uvs) {
+ total_buffer_size += sizeof(Vector2) * p_vertex_count;
+ }
+
uint32_t buffer_ofs = 0;
- storage->buffer_orphan_and_upload(data.polygon_buffer_size, 0, sizeof(Vector2) * p_vertex_count, p_vertices);
+ storage->buffer_orphan_and_upload(data.polygon_buffer_size, 0, total_buffer_size, p_vertices);
glEnableVertexAttribArray(VS::ARRAY_VERTEX);
glVertexAttribPointer(VS::ARRAY_VERTEX, 2, GL_FLOAT, GL_FALSE, sizeof(Vector2), NULL);
@@ -516,14 +549,12 @@ void RasterizerCanvasBaseGLES2::_draw_generic(GLuint p_primitive, int p_vertex_c
glDisableVertexAttribArray(VS::ARRAY_COLOR);
glVertexAttrib4f(VS::ARRAY_COLOR, 1, 1, 1, 1);
} else {
- glBufferSubData(GL_ARRAY_BUFFER, buffer_ofs, sizeof(Color) * p_vertex_count, p_colors);
glEnableVertexAttribArray(VS::ARRAY_COLOR);
glVertexAttribPointer(VS::ARRAY_COLOR, 4, GL_FLOAT, GL_FALSE, sizeof(Color), CAST_INT_TO_UCHAR_PTR(buffer_ofs));
buffer_ofs += sizeof(Color) * p_vertex_count;
}
if (p_uvs) {
- glBufferSubData(GL_ARRAY_BUFFER, buffer_ofs, sizeof(Vector2) * p_vertex_count, p_uvs);
glEnableVertexAttribArray(VS::ARRAY_TEX_UV);
glVertexAttribPointer(VS::ARRAY_TEX_UV, 2, GL_FLOAT, GL_FALSE, sizeof(Vector2), CAST_INT_TO_UCHAR_PTR(buffer_ofs));
} else {
@@ -1024,10 +1055,4 @@ void RasterizerCanvasBaseGLES2::finalize() {
}
RasterizerCanvasBaseGLES2::RasterizerCanvasBaseGLES2() {
-#ifdef GLES_OVER_GL
- use_nvidia_rect_workaround = GLOBAL_GET("rendering/quality/2d/use_nvidia_rect_flicker_workaround");
-#else
- // Not needed (a priori) on GLES devices
- use_nvidia_rect_workaround = false;
-#endif
}
diff --git a/drivers/gles2/rasterizer_canvas_base_gles2.h b/drivers/gles2/rasterizer_canvas_base_gles2.h
index abe0dbc65ce..f500db55ed7 100644
--- a/drivers/gles2/rasterizer_canvas_base_gles2.h
+++ b/drivers/gles2/rasterizer_canvas_base_gles2.h
@@ -31,13 +31,14 @@
#ifndef RASTERIZERCANVASBASEGLES2_H
#define RASTERIZERCANVASBASEGLES2_H
-#include "rasterizer_array_gles2.h"
+#include "drivers/gles_common/rasterizer_array.h"
#include "rasterizer_storage_gles2.h"
#include "servers/visual/rasterizer.h"
#include "shaders/canvas.glsl.gen.h"
#include "shaders/lens_distorted.glsl.gen.h"
+#include "drivers/gles_common/rasterizer_storage_common.h"
#include "shaders/canvas_shadow.glsl.gen.h"
class RasterizerCanvasBaseGLES2 : public RasterizerCanvas {
@@ -77,7 +78,11 @@ public:
LensDistortedShaderGLES2 lens_shader;
bool using_texture_rect;
+
bool using_light_angle;
+ bool using_modulate;
+ bool using_large_vertex;
+
bool using_ninepatch;
bool using_skeleton;
@@ -102,8 +107,6 @@ public:
RasterizerStorageGLES2 *storage;
- bool use_nvidia_rect_workaround;
-
void _set_uniforms();
virtual RID light_internal_create();
@@ -131,7 +134,7 @@ public:
virtual void canvas_debug_viewport_shadows(Light *p_lights_with_shadow);
RasterizerStorageGLES2::Texture *_bind_canvas_texture(const RID &p_texture, const RID &p_normal_map);
- void _set_texture_rect_mode(bool p_texture_rect, bool p_light_angle = false);
+ void _set_texture_rect_mode(bool p_texture_rect, bool p_light_angle = false, bool p_modulate = false, bool p_large_vertex = false);
void initialize();
void finalize();
diff --git a/drivers/gles2/rasterizer_canvas_gles2.cpp b/drivers/gles2/rasterizer_canvas_gles2.cpp
index 3c97a9ff8c4..798cbe6cb13 100644
--- a/drivers/gles2/rasterizer_canvas_gles2.cpp
+++ b/drivers/gles2/rasterizer_canvas_gles2.cpp
@@ -45,163 +45,6 @@ static const GLenum gl_primitive[] = {
GL_TRIANGLE_FAN
};
-RasterizerCanvasGLES2::BatchData::BatchData() {
- reset_flush();
- gl_vertex_buffer = 0;
- gl_index_buffer = 0;
- max_quads = 0;
- vertex_buffer_size_units = 0;
- vertex_buffer_size_bytes = 0;
- index_buffer_size_units = 0;
- index_buffer_size_bytes = 0;
- use_colored_vertices = false;
- use_light_angles = false;
- settings_use_batching = false;
- settings_max_join_item_commands = 0;
- settings_colored_vertex_format_threshold = 0.0f;
- settings_batch_buffer_num_verts = 0;
- scissor_threshold_area = 0.0f;
- joined_item_batch_flags = 0;
- diagnose_frame = false;
- next_diagnose_tick = 10000;
- diagnose_frame_number = 9999999999; // some high number
- join_across_z_indices = true;
- settings_item_reordering_lookahead = 0;
-
- settings_use_batching_original_choice = false;
- settings_flash_batching = false;
- settings_diagnose_frame = false;
- settings_scissor_lights = false;
- settings_scissor_threshold = -1.0f;
- settings_use_single_rect_fallback = false;
- settings_light_max_join_items = 16;
-
- settings_uv_contract = false;
- settings_uv_contract_amount = 0.0f;
-
- stats_items_sorted = 0;
- stats_light_items_joined = 0;
-}
-
-void RasterizerCanvasGLES2::RenderItemState::reset() {
- current_clip = nullptr;
- shader_cache = nullptr;
- rebind_shader = true;
- prev_use_skeleton = false;
- last_blend_mode = -1;
- canvas_last_material = RID();
- item_group_z = 0;
- item_group_light = nullptr;
- final_modulate = Color(-1.0, -1.0, -1.0, -1.0); // just something unlikely
-
- joined_item = nullptr;
-}
-
-// just translate the color into something easily readable and not too verbose
-String RasterizerCanvasGLES2::BatchColor::to_string() const {
- String sz = "{";
- const float *data = get_data();
- for (int c = 0; c < 4; c++) {
- float f = data[c];
- int val = ((f * 255.0f) + 0.5f);
- sz += String(Variant(val)) + " ";
- }
- sz += "}";
- return sz;
-}
-
-RasterizerStorageGLES2::Texture *RasterizerCanvasGLES2::_get_canvas_texture(const RID &p_texture) const {
- if (p_texture.is_valid()) {
-
- RasterizerStorageGLES2::Texture *texture = storage->texture_owner.getornull(p_texture);
-
- if (texture) {
- return texture->get_ptr();
- }
- }
-
- return 0;
-}
-
-int RasterizerCanvasGLES2::_batch_find_or_create_tex(const RID &p_texture, const RID &p_normal, bool p_tile, int p_previous_match) {
-
- // optimization .. in 99% cases the last matched value will be the same, so no need to traverse the list
- if (p_previous_match > 0) // if it is zero, it will get hit first in the linear search anyway
- {
- const BatchTex &batch_texture = bdata.batch_textures[p_previous_match];
-
- // note for future reference, if RID implementation changes, this could become more expensive
- if ((batch_texture.RID_texture == p_texture) && (batch_texture.RID_normal == p_normal)) {
- // tiling mode must also match
- bool tiles = batch_texture.tile_mode != BatchTex::TILE_OFF;
-
- if (tiles == p_tile)
- // match!
- return p_previous_match;
- }
- }
-
- // not the previous match .. we will do a linear search ... slower, but should happen
- // not very often except with non-batchable runs, which are going to be slow anyway
- // n.b. could possibly be replaced later by a fast hash table
- for (int n = 0; n < bdata.batch_textures.size(); n++) {
- const BatchTex &batch_texture = bdata.batch_textures[n];
- if ((batch_texture.RID_texture == p_texture) && (batch_texture.RID_normal == p_normal)) {
-
- // tiling mode must also match
- bool tiles = batch_texture.tile_mode != BatchTex::TILE_OFF;
-
- if (tiles == p_tile)
- // match!
- return n;
- }
- }
-
- // pushing back from local variable .. not ideal but has to use a Vector because non pod
- // due to RIDs
- BatchTex new_batch_tex;
- new_batch_tex.RID_texture = p_texture;
- new_batch_tex.RID_normal = p_normal;
-
- // get the texture
- RasterizerStorageGLES2::Texture *texture = _get_canvas_texture(p_texture);
-
- if (texture) {
- new_batch_tex.tex_pixel_size.x = 1.0 / texture->width;
- new_batch_tex.tex_pixel_size.y = 1.0 / texture->height;
- new_batch_tex.flags = texture->flags;
- } else {
- // maybe doesn't need doing...
- new_batch_tex.tex_pixel_size.x = 1.0;
- new_batch_tex.tex_pixel_size.y = 1.0;
- new_batch_tex.flags = 0;
- }
-
- if (p_tile) {
- if (texture) {
- // default
- new_batch_tex.tile_mode = BatchTex::TILE_NORMAL;
-
- // no hardware support for non power of 2 tiling
- if (!storage->config.support_npot_repeat_mipmap) {
- if (next_power_of_2(texture->alloc_width) != (unsigned int)texture->alloc_width && next_power_of_2(texture->alloc_height) != (unsigned int)texture->alloc_height) {
- new_batch_tex.tile_mode = BatchTex::TILE_FORCE_REPEAT;
- }
- }
- } else {
- // this should not happen?
- new_batch_tex.tile_mode = BatchTex::TILE_OFF;
- }
- } else {
- new_batch_tex.tile_mode = BatchTex::TILE_OFF;
- }
-
- // push back
- bdata.batch_textures.push_back(new_batch_tex);
-
- return bdata.batch_textures.size() - 1;
-}
-
void RasterizerCanvasGLES2::_batch_upload_buffers() {
// noop?
@@ -211,138 +54,123 @@ void RasterizerCanvasGLES2::_batch_upload_buffers() {
glBindBuffer(GL_ARRAY_BUFFER, bdata.gl_vertex_buffer);
// orphan the old (for now)
- glBufferData(GL_ARRAY_BUFFER, 0, 0, GL_DYNAMIC_DRAW);
+ //glBufferData(GL_ARRAY_BUFFER, 0, 0, GL_DYNAMIC_DRAW);
- if (!bdata.use_light_angles) {
- if (!bdata.use_colored_vertices) {
+ switch (bdata.fvf) {
+ case RasterizerStorageCommon::FVF_UNBATCHED: // should not happen
+ break;
+ case RasterizerStorageCommon::FVF_REGULAR: // no change
glBufferData(GL_ARRAY_BUFFER, sizeof(BatchVertex) * bdata.vertices.size(), bdata.vertices.get_data(), GL_DYNAMIC_DRAW);
- } else {
+ break;
+ case RasterizerStorageCommon::FVF_COLOR:
glBufferData(GL_ARRAY_BUFFER, sizeof(BatchVertexColored) * bdata.unit_vertices.size(), bdata.unit_vertices.get_unit(0), GL_DYNAMIC_DRAW);
- }
- } else {
- glBufferData(GL_ARRAY_BUFFER, sizeof(BatchVertexLightAngled) * bdata.unit_vertices.size(), bdata.unit_vertices.get_unit(0), GL_DYNAMIC_DRAW);
+ break;
+ case RasterizerStorageCommon::FVF_LIGHT_ANGLE:
+ glBufferData(GL_ARRAY_BUFFER, sizeof(BatchVertexLightAngled) * bdata.unit_vertices.size(), bdata.unit_vertices.get_unit(0), GL_DYNAMIC_DRAW);
+ break;
+ case RasterizerStorageCommon::FVF_MODULATED:
+ glBufferData(GL_ARRAY_BUFFER, sizeof(BatchVertexModulated) * bdata.unit_vertices.size(), bdata.unit_vertices.get_unit(0), GL_DYNAMIC_DRAW);
+ break;
+ case RasterizerStorageCommon::FVF_LARGE:
+ glBufferData(GL_ARRAY_BUFFER, sizeof(BatchVertexLarge) * bdata.unit_vertices.size(), bdata.unit_vertices.get_unit(0), GL_DYNAMIC_DRAW);
+ break;
}
// might not be necessary
glBindBuffer(GL_ARRAY_BUFFER, 0);
}
-RasterizerCanvasGLES2::Batch *RasterizerCanvasGLES2::_batch_request_new(bool p_blank) {
- Batch *batch = bdata.batches.request();
- if (!batch) {
- // grow the batches
- bdata.batches.grow();
+void RasterizerCanvasGLES2::_batch_render_lines(const Batch &p_batch, RasterizerStorageGLES2::Material *p_material, bool p_anti_alias) {
- // and the temporary batches (used for color verts)
- bdata.batches_temp.reset();
- bdata.batches_temp.grow();
+ _set_texture_rect_mode(false);
- // this should always succeed after growing
- batch = bdata.batches.request();
-#ifdef DEBUG_ENABLED
- CRASH_COND(!batch);
-#endif
+ if (state.canvas_shader.bind()) {
+ _set_uniforms();
+ state.canvas_shader.use_material((void *)p_material);
}
- if (p_blank)
- memset(batch, 0, sizeof(Batch));
+ _bind_canvas_texture(RID(), RID());
- return batch;
+ glDisableVertexAttribArray(VS::ARRAY_COLOR);
+ glVertexAttrib4fv(VS::ARRAY_COLOR, (float *)&p_batch.color);
+
+#ifdef GLES_OVER_GL
+ if (p_anti_alias)
+ glEnable(GL_LINE_SMOOTH);
+#endif
+
+ int sizeof_vert = sizeof(BatchVertex);
+
+ // bind the index and vertex buffer
+ glBindBuffer(GL_ARRAY_BUFFER, bdata.gl_vertex_buffer);
+ glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, bdata.gl_index_buffer);
+
+ uint64_t pointer = 0;
+ glVertexAttribPointer(VS::ARRAY_VERTEX, 2, GL_FLOAT, GL_FALSE, sizeof_vert, (const void *)pointer);
+
+ glDisableVertexAttribArray(VS::ARRAY_TEX_UV);
+
+ int64_t offset = p_batch.first_vert; // 6 inds per quad at 2 bytes each
+
+ int num_elements = p_batch.num_commands * 2;
+ glDrawArrays(GL_LINES, offset, num_elements);
+
+ storage->info.render._2d_draw_call_count++;
+
+ // may not be necessary .. state change optimization still TODO
+ glBindBuffer(GL_ARRAY_BUFFER, 0);
+ glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0);
+
+#ifdef GLES_OVER_GL
+ if (p_anti_alias)
+ glDisable(GL_LINE_SMOOTH);
+#endif
}
-// This function may be called MULTIPLE TIMES for each item, so needs to record how far it has got
-bool RasterizerCanvasGLES2::prefill_joined_item(FillState &r_fill_state, int &r_command_start, Item *p_item, Item *p_current_clip, bool &r_reclip, RasterizerStorageGLES2::Material *p_material) {
- // we will prefill batches and vertices ready for sending in one go to the vertex buffer
- int command_count = p_item->commands.size();
- Item::Command *const *commands = p_item->commands.ptr();
+void RasterizerCanvasGLES2::_batch_render_polys(const Batch &p_batch, RasterizerStorageGLES2::Material *p_material) {
- // checking the color for not being white makes it 92/90 times faster in the case where it is white
- bool multiply_final_modulate = false;
- if (!r_fill_state.use_hardware_transform && (r_fill_state.final_modulate != Color(1, 1, 1, 1))) {
- multiply_final_modulate = true;
+ _set_texture_rect_mode(false);
+
+ if (state.canvas_shader.bind()) {
+ _set_uniforms();
+ state.canvas_shader.use_material((void *)p_material);
}
- // start batch is a dummy batch (tex id -1) .. could be made more efficient
- if (!r_fill_state.curr_batch) {
- r_fill_state.curr_batch = _batch_request_new();
- r_fill_state.curr_batch->type = Batch::BT_DEFAULT;
- r_fill_state.curr_batch->first_command = r_command_start;
- // should tex_id be set to -1? check this
- }
+ // batch tex
+ const BatchTex &tex = bdata.batch_textures[p_batch.batch_texture_id];
+ _bind_canvas_texture(tex.RID_texture, tex.RID_normal);
- // we need to return which command we got up to, so
- // store this outside the loop
- int command_num;
+ // state.canvas_shader.set_uniform(CanvasShaderGLES2::MODELVIEW_MATRIX, Transform());
- // do as many commands as possible until the vertex buffer will be full up
- for (command_num = r_command_start; command_num < command_count; command_num++) {
+ int sizeof_vert = sizeof(BatchVertexColored);
- Item::Command *command = commands[command_num];
+ // bind the index and vertex buffer
+ glBindBuffer(GL_ARRAY_BUFFER, bdata.gl_vertex_buffer);
+ glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, bdata.gl_index_buffer);
- switch (command->type) {
+ uint64_t pointer = 0;
+ glVertexAttribPointer(VS::ARRAY_VERTEX, 2, GL_FLOAT, GL_FALSE, sizeof_vert, (const void *)pointer);
- default: {
- _prefill_default_batch(r_fill_state, command_num, *p_item);
- } break;
- case Item::Command::TYPE_TRANSFORM: {
- // if the extra matrix has been sent already,
- // break this extra matrix software path (as we don't want to unset it on the GPU etc)
- if (r_fill_state.extra_matrix_sent) {
- _prefill_default_batch(r_fill_state, command_num, *p_item);
- } else {
- // Extra matrix fast path.
- // Instead of sending the command immediately, we store the modified transform (in combined)
- // for software transform, and only flush this transform command if we NEED to (i.e. we want to
- // render some default commands)
- Item::CommandTransform *transform = static_cast(command);
- const Transform2D &extra_matrix = transform->xform;
+ glEnableVertexAttribArray(VS::ARRAY_TEX_UV);
+ glVertexAttribPointer(VS::ARRAY_TEX_UV, 2, GL_FLOAT, GL_FALSE, sizeof_vert, CAST_INT_TO_UCHAR_PTR(pointer + (2 * 4)));
- if (r_fill_state.use_hardware_transform) {
- // if we are using hardware transform mode, we have already sent the final transform,
- // so we only want to software transform the extra matrix
- r_fill_state.transform_combined = extra_matrix;
- } else {
- r_fill_state.transform_combined = p_item->final_transform * extra_matrix;
- }
- // after a transform command, always use some form of software transform (either the combined final + extra, or just the extra)
- // until we flush this dirty extra matrix because we need to render default commands.
- r_fill_state.transform_mode = _find_transform_mode(r_fill_state.transform_combined);
+ glEnableVertexAttribArray(VS::ARRAY_COLOR);
+ glVertexAttribPointer(VS::ARRAY_COLOR, 4, GL_FLOAT, GL_FALSE, sizeof_vert, CAST_INT_TO_UCHAR_PTR(pointer + (4 * 4)));
- // make a note of which command the dirty extra matrix is store in, so we can send it later
- // if necessary
- r_fill_state.transform_extra_command_number_p1 = command_num + 1; // plus 1 so we can test against zero
- }
- } break;
- case Item::Command::TYPE_RECT: {
+ int64_t offset = p_batch.first_vert; // 6 inds per quad at 2 bytes each
- Item::CommandRect *rect = static_cast(command);
+ int num_elements = p_batch.num_commands;
+ glDrawArrays(GL_TRIANGLES, offset, num_elements);
- // unoptimized - could this be done once per batch / batch texture?
- bool send_light_angles = rect->normal_map != RID();
+ storage->info.render._2d_draw_call_count++;
- bool buffer_full = false;
+ // could these have ifs?
+ glDisableVertexAttribArray(VS::ARRAY_TEX_UV);
+ glDisableVertexAttribArray(VS::ARRAY_COLOR);
- // the template params must be explicit for compilation,
- // this forces building the multiple versions of the function.
- if (send_light_angles) {
- buffer_full = prefill_rect(rect, r_fill_state, r_command_start, command_num, command_count, commands, p_item, multiply_final_modulate);
- } else {
- buffer_full = prefill_rect(rect, r_fill_state, r_command_start, command_num, command_count, commands, p_item, multiply_final_modulate);
- }
-
- if (buffer_full)
- return true;
-
- } break;
- }
- }
-
- // VERY IMPORTANT to return where we got to, because this func may be called multiple
- // times per item.
- // Don't miss out on this step by calling return earlier in the function without setting r_command_start.
- r_command_start = command_num;
-
- return false;
+ // may not be necessary .. state change optimization still TODO
+ glBindBuffer(GL_ARRAY_BUFFER, 0);
+ glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0);
}
void RasterizerCanvasGLES2::_batch_render_rects(const Batch &p_batch, RasterizerStorageGLES2::Material *p_material) {
@@ -351,23 +179,43 @@ void RasterizerCanvasGLES2::_batch_render_rects(const Batch &p_batch, Rasterizer
const bool &colored_verts = bdata.use_colored_vertices;
const bool &use_light_angles = bdata.use_light_angles;
+ const bool &use_modulate = bdata.use_modulate;
+ const bool &use_large_verts = bdata.use_large_verts;
int sizeof_vert;
- if (!use_light_angles) {
- if (!colored_verts) {
+
+ switch (bdata.fvf) {
+ default:
+ sizeof_vert = 0; // prevent compiler warning - this should never happen
+ break;
+ case RasterizerStorageCommon::FVF_UNBATCHED: // should not happen
+ return;
+ break;
+ case RasterizerStorageCommon::FVF_REGULAR: // no change
sizeof_vert = sizeof(BatchVertex);
- } else {
+ break;
+ case RasterizerStorageCommon::FVF_COLOR:
sizeof_vert = sizeof(BatchVertexColored);
- }
- } else {
- sizeof_vert = sizeof(BatchVertexLightAngled);
+ break;
+ case RasterizerStorageCommon::FVF_LIGHT_ANGLE:
+ sizeof_vert = sizeof(BatchVertexLightAngled);
+ break;
+ case RasterizerStorageCommon::FVF_MODULATED:
+ sizeof_vert = sizeof(BatchVertexModulated);
+ break;
+ case RasterizerStorageCommon::FVF_LARGE:
+ sizeof_vert = sizeof(BatchVertexLarge);
+ break;
}
// batch tex
const BatchTex &tex = bdata.batch_textures[p_batch.batch_texture_id];
// make sure to set all conditionals BEFORE binding the shader
- _set_texture_rect_mode(false, use_light_angles);
+ //state.canvas_shader.set_conditional(CanvasShaderGLES2::USE_TEXTURE_RECT, false);
+ _set_texture_rect_mode(false, use_light_angles, use_modulate, use_large_verts);
+
+ //VSG::rasterizer->gl_check_for_error();
// force repeat is set if non power of 2 texture, and repeat is needed if hardware doesn't support npot
if (tex.tile_mode == BatchTex::TILE_FORCE_REPEAT) {
@@ -379,6 +227,9 @@ void RasterizerCanvasGLES2::_batch_render_rects(const Batch &p_batch, Rasterizer
state.canvas_shader.use_material((void *)p_material);
}
+ // batch tex
+ //const BatchTex &tex = bdata.batch_textures[p_batch.batch_texture_id];
+
_bind_canvas_texture(tex.RID_texture, tex.RID_normal);
// bind the index and vertex buffer
@@ -389,21 +240,33 @@ void RasterizerCanvasGLES2::_batch_render_rects(const Batch &p_batch, Rasterizer
glVertexAttribPointer(VS::ARRAY_VERTEX, 2, GL_FLOAT, GL_FALSE, sizeof_vert, (const void *)pointer);
// always send UVs, even within a texture specified because a shader can still use UVs
- glVertexAttribPointer(VS::ARRAY_TEX_UV, 2, GL_FLOAT, GL_FALSE, sizeof_vert, CAST_INT_TO_UCHAR_PTR(pointer + (2 * 4)));
glEnableVertexAttribArray(VS::ARRAY_TEX_UV);
+ glVertexAttribPointer(VS::ARRAY_TEX_UV, 2, GL_FLOAT, GL_FALSE, sizeof_vert, CAST_INT_TO_UCHAR_PTR(pointer + (2 * 4)));
// color
if (!colored_verts) {
glDisableVertexAttribArray(VS::ARRAY_COLOR);
glVertexAttrib4fv(VS::ARRAY_COLOR, p_batch.color.get_data());
} else {
- glVertexAttribPointer(VS::ARRAY_COLOR, 4, GL_FLOAT, GL_FALSE, sizeof_vert, CAST_INT_TO_UCHAR_PTR(pointer + (4 * 4)));
glEnableVertexAttribArray(VS::ARRAY_COLOR);
+ glVertexAttribPointer(VS::ARRAY_COLOR, 4, GL_FLOAT, GL_FALSE, sizeof_vert, CAST_INT_TO_UCHAR_PTR(pointer + (4 * 4)));
}
if (use_light_angles) {
- glVertexAttribPointer(VS::ARRAY_TANGENT, 1, GL_FLOAT, GL_FALSE, sizeof_vert, CAST_INT_TO_UCHAR_PTR(pointer + (8 * 4)));
glEnableVertexAttribArray(VS::ARRAY_TANGENT);
+ glVertexAttribPointer(VS::ARRAY_TANGENT, 1, GL_FLOAT, GL_FALSE, sizeof_vert, CAST_INT_TO_UCHAR_PTR(pointer + (8 * 4)));
+ }
+
+ if (use_modulate) {
+ glEnableVertexAttribArray(VS::ARRAY_TEX_UV2);
+ glVertexAttribPointer(VS::ARRAY_TEX_UV2, 4, GL_FLOAT, GL_FALSE, sizeof_vert, CAST_INT_TO_UCHAR_PTR(pointer + (9 * 4)));
+ }
+
+ if (use_large_verts) {
+ glEnableVertexAttribArray(VS::ARRAY_BONES);
+ glVertexAttribPointer(VS::ARRAY_BONES, 2, GL_FLOAT, GL_FALSE, sizeof_vert, CAST_INT_TO_UCHAR_PTR(pointer + (13 * 4)));
+ glEnableVertexAttribArray(VS::ARRAY_WEIGHTS);
+ glVertexAttribPointer(VS::ARRAY_WEIGHTS, 4, GL_FLOAT, GL_FALSE, sizeof_vert, CAST_INT_TO_UCHAR_PTR(pointer + (15 * 4)));
}
// We only want to set the GL wrapping mode if the texture is not already tiled (i.e. set in Import).
@@ -426,7 +289,7 @@ void RasterizerCanvasGLES2::_batch_render_rects(const Batch &p_batch, Rasterizer
tex.tex_pixel_size.to(tps);
state.canvas_shader.set_uniform(CanvasShaderGLES2::COLOR_TEXPIXEL_SIZE, tps);
- int64_t offset = p_batch.first_quad * 6 * 2; // 6 inds per quad at 2 bytes each
+ int64_t offset = p_batch.first_vert * 3;
int num_elements = p_batch.num_commands * 6;
glDrawElements(GL_TRIANGLES, num_elements, GL_UNSIGNED_SHORT, (void *)offset);
@@ -452,121 +315,15 @@ void RasterizerCanvasGLES2::_batch_render_rects(const Batch &p_batch, Rasterizer
glDisableVertexAttribArray(VS::ARRAY_TEX_UV);
glDisableVertexAttribArray(VS::ARRAY_COLOR);
glDisableVertexAttribArray(VS::ARRAY_TANGENT);
+ glDisableVertexAttribArray(VS::ARRAY_TEX_UV2);
+ glDisableVertexAttribArray(VS::ARRAY_BONES);
+ glDisableVertexAttribArray(VS::ARRAY_WEIGHTS);
// may not be necessary .. state change optimization still TODO
glBindBuffer(GL_ARRAY_BUFFER, 0);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0);
}
-#ifdef DEBUG_ENABLED
-String RasterizerCanvasGLES2::get_command_type_string(const Item::Command &p_command) const {
- String sz = "";
-
- switch (p_command.type) {
- default:
- break;
- case Item::Command::TYPE_LINE: {
- sz = "l";
- } break;
- case Item::Command::TYPE_POLYLINE: {
- sz = "PL";
- } break;
- case Item::Command::TYPE_RECT: {
- sz = "r";
- } break;
- case Item::Command::TYPE_NINEPATCH: {
- sz = "n";
- } break;
- case Item::Command::TYPE_PRIMITIVE: {
- sz = "PR";
- } break;
- case Item::Command::TYPE_POLYGON: {
- sz = "p";
- } break;
- case Item::Command::TYPE_MESH: {
- sz = "m";
- } break;
- case Item::Command::TYPE_MULTIMESH: {
- sz = "MM";
- } break;
- case Item::Command::TYPE_PARTICLES: {
- sz = "PA";
- } break;
- case Item::Command::TYPE_CIRCLE: {
- sz = "c";
- } break;
- case Item::Command::TYPE_TRANSFORM: {
- sz = "t";
-
- // add a bit more info in debug build
- const Item::CommandTransform *transform = static_cast(&p_command);
- const Transform2D &mat = transform->xform;
-
- sz += " ";
- sz += String(Variant(mat.elements[2]));
- sz += " ";
- } break;
- case Item::Command::TYPE_CLIP_IGNORE: {
- sz = "CI";
- } break;
- } // switch
-
- return sz;
-}
-
-void RasterizerCanvasGLES2::diagnose_batches(Item::Command *const *p_commands) {
- int num_batches = bdata.batches.size();
-
- BatchColor curr_color;
- curr_color.set(Color(-1, -1, -1, -1));
- bool first_color_change = true;
-
- for (int batch_num = 0; batch_num < num_batches; batch_num++) {
- const Batch &batch = bdata.batches[batch_num];
- bdata.frame_string += "\t\t\tbatch ";
-
- switch (batch.type) {
- case Batch::BT_RECT: {
- bdata.frame_string += "R ";
- bdata.frame_string += itos(batch.first_command) + "-";
- bdata.frame_string += itos(batch.num_commands);
-
- int tex_id = (int)bdata.batch_textures[batch.batch_texture_id].RID_texture.get_id();
- bdata.frame_string += " [" + itos(batch.batch_texture_id) + " - " + itos(tex_id) + "]";
-
- bdata.frame_string += " " + batch.color.to_string();
-
- if (batch.num_commands > 1) {
- bdata.frame_string += " MULTI";
- }
- if (curr_color != batch.color) {
- curr_color = batch.color;
- if (!first_color_change) {
- bdata.frame_string += " color";
- } else {
- first_color_change = false;
- }
- }
- bdata.frame_string += "\n";
- } break;
- default: {
- bdata.frame_string += "D ";
- bdata.frame_string += itos(batch.first_command) + "-";
- bdata.frame_string += itos(batch.num_commands) + " ";
-
- int num_show = MIN(batch.num_commands, 16);
- for (int n = 0; n < num_show; n++) {
- const Item::Command &comm = *p_commands[batch.first_command + n];
- bdata.frame_string += get_command_type_string(comm) + " ";
- }
-
- bdata.frame_string += "\n";
- } break;
- }
- }
-}
-#endif
-
void RasterizerCanvasGLES2::render_batches(Item::Command *const *p_commands, Item *p_current_clip, bool &r_reclip, RasterizerStorageGLES2::Material *p_material) {
int num_batches = bdata.batches.size();
@@ -575,9 +332,18 @@ void RasterizerCanvasGLES2::render_batches(Item::Command *const *p_commands, Ite
const Batch &batch = bdata.batches[batch_num];
switch (batch.type) {
- case Batch::BT_RECT: {
+ case RasterizerStorageCommon::BT_RECT: {
_batch_render_rects(batch, p_material);
} break;
+ case RasterizerStorageCommon::BT_POLY: {
+ _batch_render_polys(batch, p_material);
+ } break;
+ case RasterizerStorageCommon::BT_LINE: {
+ _batch_render_lines(batch, p_material, false);
+ } break;
+ case RasterizerStorageCommon::BT_LINE_AA: {
+ _batch_render_lines(batch, p_material, true);
+ } break;
default: {
int end_command = batch.first_command + batch.num_commands;
@@ -592,6 +358,7 @@ void RasterizerCanvasGLES2::render_batches(Item::Command *const *p_commands, Ite
Item::CommandLine *line = static_cast(command);
_set_texture_rect_mode(false);
+
if (state.canvas_shader.bind()) {
_set_uniforms();
state.canvas_shader.use_material((void *)p_material);
@@ -924,8 +691,7 @@ void RasterizerCanvasGLES2::render_batches(Item::Command *const *p_commands, Ite
float screen_scale = 1.0;
- if (source.size.x != 0 && source.size.y != 0) {
-
+ if ((bdata.settings_ninepatch_mode == 1) && (source.size.x != 0) && (source.size.y != 0)) {
screen_scale = MIN(np->rect.size.x / source.size.x, np->rect.size.y / source.size.y);
screen_scale = MIN(1.0, screen_scale);
}
@@ -1126,6 +892,7 @@ void RasterizerCanvasGLES2::render_batches(Item::Command *const *p_commands, Ite
case Item::Command::TYPE_MESH: {
Item::CommandMesh *mesh = static_cast(command);
+
_set_texture_rect_mode(false);
if (state.canvas_shader.bind()) {
@@ -1306,7 +1073,10 @@ void RasterizerCanvasGLES2::render_batches(Item::Command *const *p_commands, Ite
}
}
- _set_texture_rect_mode(false);
+ // LIGHT ANGLE PR replaced USE_INSTANCE_CUSTOM line with below .. think it was a typo,
+ // but just in case, made this note.
+ //_set_texture_rect_mode(false);
+ state.canvas_shader.set_conditional(CanvasShaderGLES2::USE_INSTANCE_CUSTOM, false);
state.canvas_shader.set_conditional(CanvasShaderGLES2::USE_INSTANCING, false);
storage->info.render._2d_draw_call_count++;
@@ -1314,7 +1084,7 @@ void RasterizerCanvasGLES2::render_batches(Item::Command *const *p_commands, Ite
case Item::Command::TYPE_POLYLINE: {
Item::CommandPolyLine *pline = static_cast(command);
- state.canvas_shader.set_conditional(CanvasShaderGLES2::USE_TEXTURE_RECT, false);
+ _set_texture_rect_mode(false);
if (state.canvas_shader.bind()) {
_set_uniforms();
@@ -1441,527 +1211,28 @@ void RasterizerCanvasGLES2::render_batches(Item::Command *const *p_commands, Ite
break;
}
}
-
- // zero all the batch data ready for a new run
- bdata.reset_flush();
-}
-
-void RasterizerCanvasGLES2::render_joined_item_commands(const BItemJoined &p_bij, Item *p_current_clip, bool &r_reclip, RasterizerStorageGLES2::Material *p_material, bool p_lit) {
-
- Item *item = 0;
- Item *first_item = bdata.item_refs[p_bij.first_item_ref].item;
-
- FillState fill_state;
- fill_state.reset();
- fill_state.use_hardware_transform = p_bij.use_hardware_transform();
- fill_state.extra_matrix_sent = false;
-
- // in the special case of custom shaders that read from VERTEX (i.e. vertex position)
- // we want to disable software transform of extra matrix
- if (bdata.joined_item_batch_flags & RasterizerStorageGLES2::Shader::CanvasItem::PREVENT_VERTEX_BAKING) {
- fill_state.extra_matrix_sent = true;
- }
-
- for (unsigned int i = 0; i < p_bij.num_item_refs; i++) {
- const BItemRef &ref = bdata.item_refs[p_bij.first_item_ref + i];
- item = ref.item;
-
- if (!p_lit) {
- // if not lit we use the complex calculated final modulate
- fill_state.final_modulate = ref.final_modulate;
- } else {
- // if lit we ignore canvas modulate and just use the item modulate
- fill_state.final_modulate = item->final_modulate;
- }
-
- int command_count = item->commands.size();
- int command_start = 0;
-
- // ONCE OFF fill state setup, that will be retained over multiple calls to
- // prefill_joined_item()
- fill_state.transform_combined = item->final_transform;
-
- // decide the initial transform mode, and make a backup
- // in orig_transform_mode in case we need to switch back
- if (!fill_state.use_hardware_transform) {
- fill_state.transform_mode = _find_transform_mode(fill_state.transform_combined);
- } else {
- fill_state.transform_mode = TM_NONE;
- }
- fill_state.orig_transform_mode = fill_state.transform_mode;
-
- // keep track of when we added an extra matrix
- // so we can defer sending until we see a default command
- fill_state.transform_extra_command_number_p1 = 0;
-
- while (command_start < command_count) {
- // fill as many batches as possible (until all done, or the vertex buffer is full)
- bool bFull = prefill_joined_item(fill_state, command_start, item, p_current_clip, r_reclip, p_material);
-
- if (bFull) {
- // always pass first item (commands for default are always first item)
- flush_render_batches(first_item, p_current_clip, r_reclip, p_material);
- fill_state.reset();
- }
- }
- }
-
- // flush if any left
- flush_render_batches(first_item, p_current_clip, r_reclip, p_material);
-}
-
-void RasterizerCanvasGLES2::flush_render_batches(Item *p_first_item, Item *p_current_clip, bool &r_reclip, RasterizerStorageGLES2::Material *p_material) {
-
- // some heuristic to decide whether to use colored verts.
- // feel free to tweak this.
- // this could use hysteresis, to prevent jumping between methods
- // .. however probably not necessary
- bdata.use_colored_vertices = false;
-
- if (bdata.use_light_angles) {
- _translate_batches_to_larger_FVF();
- } else {
- // only check whether to convert if there are quads (prevent divide by zero)
- // and we haven't decided to prevent color baking (due to e.g. MODULATE
- // being used in a shader)
- if (bdata.total_quads && !(bdata.joined_item_batch_flags & RasterizerStorageGLES2::Shader::CanvasItem::PREVENT_COLOR_BAKING)) {
- // minus 1 to prevent single primitives (ratio 1.0) always being converted to colored..
- // in that case it is slightly cheaper to just have the color as part of the batch
- float ratio = (float)(bdata.total_color_changes - 1) / (float)bdata.total_quads;
-
- // use bigger than or equal so that 0.0 threshold can force always using colored verts
- if (ratio >= bdata.settings_colored_vertex_format_threshold) {
- bdata.use_colored_vertices = true;
-
- // small perf cost versus going straight to colored verts (maybe around 10%)
- // however more straightforward
- _translate_batches_to_larger_FVF();
- //_batch_translate_to_colored();
- }
- }
- } // if not using light angles
-
- // send buffers to opengl
- _batch_upload_buffers();
-
- Item::Command *const *commands = p_first_item->commands.ptr();
-
-#ifdef DEBUG_ENABLED
- if (bdata.diagnose_frame) {
- diagnose_batches(commands);
- }
-#endif
-
- render_batches(commands, p_current_clip, r_reclip, p_material);
-}
-
-void RasterizerCanvasGLES2::_canvas_item_render_commands(Item *p_item, Item *p_current_clip, bool &r_reclip, RasterizerStorageGLES2::Material *p_material) {
-
- int command_count = p_item->commands.size();
-
- Item::Command *const *commands = p_item->commands.ptr();
-
- // legacy .. just create one massive batch and render everything as before
- bdata.batches.reset();
- Batch *batch = _batch_request_new();
- batch->type = Batch::BT_DEFAULT;
- batch->num_commands = command_count;
-
- render_batches(commands, p_current_clip, r_reclip, p_material);
-}
-
-void RasterizerCanvasGLES2::record_items(Item *p_item_list, int p_z) {
- while (p_item_list) {
- BSortItem *s = bdata.sort_items.request_with_grow();
-
- s->item = p_item_list;
- s->z_index = p_z;
-
- p_item_list = p_item_list->next;
- }
-}
-
-void RasterizerCanvasGLES2::sort_items() {
- // turned off?
- if (!bdata.settings_item_reordering_lookahead) {
- return;
- }
-
- for (int s = 0; s < bdata.sort_items.size() - 2; s++) {
- if (sort_items_from(s)) {
-#ifdef DEBUG_ENABLED
- bdata.stats_items_sorted++;
-#endif
- }
- }
-}
-
-bool RasterizerCanvasGLES2::sort_items_from(int p_start) {
-#ifdef DEBUG_ENABLED
- ERR_FAIL_COND_V((p_start + 1) >= bdata.sort_items.size(), false)
-#endif
-
- const BSortItem &start = bdata.sort_items[p_start];
- int start_z = start.z_index;
-
- // check start is the right type for sorting
- if (start.item->commands.size() != 1) {
- return false;
- }
- const Item::Command &command_start = *start.item->commands[0];
- if (command_start.type != Item::Command::TYPE_RECT) {
- return false;
- }
-
- BSortItem &second = bdata.sort_items[p_start + 1];
- if (second.z_index != start_z) {
- // no sorting across z indices (for now)
- return false;
- }
-
- // if the neighbours are already a good match
- if (_sort_items_match(start, second)) // order is crucial, start first
- {
- return false;
- }
-
- // local cached aabb
- Rect2 second_AABB = second.item->global_rect_cache;
-
- // if the start and 2nd items overlap, can do no more
- if (start.item->global_rect_cache.intersects(second_AABB)) {
- return false;
- }
-
- // which neighbour to test
- int test_last = 2 + bdata.settings_item_reordering_lookahead;
- for (int test = 2; test < test_last; test++) {
- int test_sort_item_id = p_start + test;
-
- // if we've got to the end of the list, can't sort any more, give up
- if (test_sort_item_id >= bdata.sort_items.size()) {
- return false;
- }
-
- BSortItem *test_sort_item = &bdata.sort_items[test_sort_item_id];
-
- // across z indices?
- if (test_sort_item->z_index != start_z) {
- return false;
- }
-
- Item *test_item = test_sort_item->item;
-
- // if the test item overlaps the second item, we can't swap, AT ALL
- // because swapping an item OVER this one would cause artefacts
- if (second_AABB.intersects(test_item->global_rect_cache)) {
- return false;
- }
-
- // do they match?
- if (!_sort_items_match(start, *test_sort_item)) // order is crucial, start first
- {
- continue;
- }
-
- // we can only swap if there are no AABB overlaps with sandwiched neighbours
- bool ok = true;
-
- // start from 2, no need to check 1 as the second has already been checked against this item
- // in the intersection test above
- for (int sn = 2; sn < test; sn++) {
- BSortItem *sandwich_neighbour = &bdata.sort_items[p_start + sn];
- if (test_item->global_rect_cache.intersects(sandwich_neighbour->item->global_rect_cache)) {
- ok = false;
- break;
- }
- }
- if (!ok) {
- continue;
- }
-
- // it is ok to exchange them!
- BSortItem temp;
- temp.assign(second);
- second.assign(*test_sort_item);
- test_sort_item->assign(temp);
-
- return true;
- } // for test
-
- return false;
-}
-
-void RasterizerCanvasGLES2::join_sorted_items() {
- sort_items();
-
- int z = VS::CANVAS_ITEM_Z_MIN;
- _render_item_state.item_group_z = z;
-
- for (int s = 0; s < bdata.sort_items.size(); s++) {
- const BSortItem &si = bdata.sort_items[s];
- Item *ci = si.item;
-
- // change z?
- if (si.z_index != z) {
- z = si.z_index;
-
- // may not be required
- _render_item_state.item_group_z = z;
-
- // if z ranged lights are present, sometimes we have to disable joining over z_indices.
- // we do this here.
- // Note this restriction may be able to be relaxed with light bitfields, investigate!
- if (!bdata.join_across_z_indices) {
- _render_item_state.join_batch_break = true;
- }
- }
-
- bool join;
-
- if (_render_item_state.join_batch_break) {
- // always start a new batch for this item
- join = false;
-
- // could be another batch break (i.e. prevent NEXT item from joining this)
- // so we still need to run try_join_item
- // even though we know join is false.
- // also we need to run try_join_item for every item because it keeps the state up to date,
- // if we didn't run it the state would be out of date.
- try_join_item(ci, _render_item_state, _render_item_state.join_batch_break);
- } else {
- join = try_join_item(ci, _render_item_state, _render_item_state.join_batch_break);
- }
-
- // assume the first item will always return no join
- if (!join) {
- _render_item_state.joined_item = bdata.items_joined.request_with_grow();
- _render_item_state.joined_item->first_item_ref = bdata.item_refs.size();
- _render_item_state.joined_item->num_item_refs = 1;
- _render_item_state.joined_item->bounding_rect = ci->global_rect_cache;
- _render_item_state.joined_item->z_index = z;
- _render_item_state.joined_item->flags = bdata.joined_item_batch_flags;
-
- // add the reference
- BItemRef *r = bdata.item_refs.request_with_grow();
- r->item = ci;
- // we are storing final_modulate in advance per item reference
- // for baking into vertex colors.
- // this may not be ideal... as we are increasing the size of item reference,
- // but it is stupidly complex to calculate later, which would probably be slower.
- r->final_modulate = _render_item_state.final_modulate;
- } else {
- CRASH_COND(_render_item_state.joined_item == 0);
- _render_item_state.joined_item->num_item_refs += 1;
- _render_item_state.joined_item->bounding_rect = _render_item_state.joined_item->bounding_rect.merge(ci->global_rect_cache);
-
- BItemRef *r = bdata.item_refs.request_with_grow();
- r->item = ci;
- r->final_modulate = _render_item_state.final_modulate;
- }
-
- } // for s through sort items
-}
-
-void RasterizerCanvasGLES2::join_items(Item *p_item_list, int p_z) {
-
- _render_item_state.item_group_z = p_z;
-
- // join is whether to join to the previous batch.
- // batch_break is whether to PREVENT the next batch from joining with us
- // batch_break must be preserved over z_indices,
- // so is stored in _render_item_state.join_batch_break
-
- // if z ranged lights are present, sometimes we have to disable joining over z_indices.
- // we do this here
- if (!bdata.join_across_z_indices) {
- _render_item_state.join_batch_break = true;
- }
-
- while (p_item_list) {
-
- Item *ci = p_item_list;
-
- bool join;
-
- if (_render_item_state.join_batch_break) {
- // always start a new batch for this item
- join = false;
-
- // could be another batch break (i.e. prevent NEXT item from joining this)
- // so we still need to run try_join_item
- // even though we know join is false.
- // also we need to run try_join_item for every item because it keeps the state up to date,
- // if we didn't run it the state would be out of date.
- try_join_item(ci, _render_item_state, _render_item_state.join_batch_break);
- } else {
- join = try_join_item(ci, _render_item_state, _render_item_state.join_batch_break);
- }
-
- // assume the first item will always return no join
- if (!join) {
- _render_item_state.joined_item = bdata.items_joined.request_with_grow();
- _render_item_state.joined_item->first_item_ref = bdata.item_refs.size();
- _render_item_state.joined_item->num_item_refs = 1;
- _render_item_state.joined_item->bounding_rect = ci->global_rect_cache;
- _render_item_state.joined_item->z_index = p_z;
-
- // add the reference
- BItemRef *r = bdata.item_refs.request_with_grow();
- r->item = ci;
- // we are storing final_modulate in advance per item reference
- // for baking into vertex colors.
- // this may not be ideal... as we are increasing the size of item reference,
- // but it is stupidly complex to calculate later, which would probably be slower.
- r->final_modulate = _render_item_state.final_modulate;
- } else {
- CRASH_COND(_render_item_state.joined_item == 0);
- _render_item_state.joined_item->num_item_refs += 1;
- _render_item_state.joined_item->bounding_rect = _render_item_state.joined_item->bounding_rect.merge(ci->global_rect_cache);
-
- BItemRef *r = bdata.item_refs.request_with_grow();
- r->item = ci;
- r->final_modulate = _render_item_state.final_modulate;
- }
-
- p_item_list = p_item_list->next;
- }
}
void RasterizerCanvasGLES2::canvas_end() {
-#ifdef DEBUG_ENABLED
- if (bdata.diagnose_frame) {
- bdata.frame_string += "canvas_end\n";
- if (bdata.stats_items_sorted) {
- bdata.frame_string += "\titems reordered: " + itos(bdata.stats_items_sorted) + "\n";
- }
- if (bdata.stats_light_items_joined) {
- bdata.frame_string += "\tlight items joined: " + itos(bdata.stats_light_items_joined) + "\n";
- }
-
- print_line(bdata.frame_string);
- }
-#endif
-
+ batch_canvas_end();
RasterizerCanvasBaseGLES2::canvas_end();
}
void RasterizerCanvasGLES2::canvas_begin() {
- // diagnose_frame?
- bdata.frame_string = ""; // just in case, always set this as we don't want a string leak in release...
-#ifdef DEBUG_ENABLED
- if (bdata.settings_diagnose_frame) {
- bdata.diagnose_frame = false;
-
- uint32_t tick = OS::get_singleton()->get_ticks_msec();
- uint64_t frame = Engine::get_singleton()->get_frames_drawn();
-
- if (tick >= bdata.next_diagnose_tick) {
- bdata.next_diagnose_tick = tick + 10000;
-
- // the plus one is prevent starting diagnosis half way through frame
- bdata.diagnose_frame_number = frame + 1;
- }
-
- if (frame == bdata.diagnose_frame_number) {
- bdata.diagnose_frame = true;
- bdata.reset_stats();
- }
-
- if (bdata.diagnose_frame) {
- bdata.frame_string = "canvas_begin FRAME " + itos(frame) + "\n";
- }
- }
-#endif
-
+ batch_canvas_begin();
RasterizerCanvasBaseGLES2::canvas_begin();
}
void RasterizerCanvasGLES2::canvas_render_items_begin(const Color &p_modulate, Light *p_light, const Transform2D &p_base_transform) {
- // if we are debugging, flash each frame between batching renderer and old version to compare for regressions
- if (bdata.settings_flash_batching) {
- if ((Engine::get_singleton()->get_frames_drawn() % 2) == 0)
- bdata.settings_use_batching = true;
- else
- bdata.settings_use_batching = false;
- }
-
- if (!bdata.settings_use_batching) {
- return;
- }
-
- // this only needs to be done when screen size changes, but this should be
- // infrequent enough
- _calculate_scissor_threshold_area();
-
- // set up render item state for all the z_indexes (this is common to all z_indexes)
- _render_item_state.reset();
- _render_item_state.item_group_modulate = p_modulate;
- _render_item_state.item_group_light = p_light;
- _render_item_state.item_group_base_transform = p_base_transform;
- _render_item_state.light_region.reset();
-
- // batch break must be preserved over the different z indices,
- // to prevent joining to an item on a previous index if not allowed
- _render_item_state.join_batch_break = false;
-
- // whether to join across z indices depends on whether there are z ranged lights.
- // joined z_index items can be wrongly classified with z ranged lights.
- bdata.join_across_z_indices = true;
-
- int light_count = 0;
- while (p_light) {
- light_count++;
-
- if ((p_light->z_min != VS::CANVAS_ITEM_Z_MIN) || (p_light->z_max != VS::CANVAS_ITEM_Z_MAX)) {
- // prevent joining across z indices. This would have caused visual regressions
- bdata.join_across_z_indices = false;
- }
-
- p_light = p_light->next_ptr;
- }
-
- // can't use the light region bitfield if there are too many lights
- // hopefully most games won't blow this limit..
- // if they do they will work but it won't batch join items just in case
- if (light_count > 64) {
- _render_item_state.light_region.too_many_lights = true;
- }
+ batch_canvas_render_items_begin(p_modulate, p_light, p_base_transform);
}
void RasterizerCanvasGLES2::canvas_render_items_end() {
- if (!bdata.settings_use_batching) {
- return;
- }
-
- join_sorted_items();
-
-#ifdef DEBUG_ENABLED
- if (bdata.diagnose_frame) {
- bdata.frame_string += "items\n";
- }
-#endif
-
- // batching render is deferred until after going through all the z_indices, joining all the items
- canvas_render_items_implementation(0, 0, _render_item_state.item_group_modulate,
- _render_item_state.item_group_light,
- _render_item_state.item_group_base_transform);
-
- bdata.items_joined.reset();
- bdata.item_refs.reset();
- bdata.sort_items.reset();
+ batch_canvas_render_items_end();
}
void RasterizerCanvasGLES2::canvas_render_items(Item *p_item_list, int p_z, const Color &p_modulate, Light *p_light, const Transform2D &p_base_transform) {
- // stage 1 : join similar items, so that their state changes are not repeated,
- // and commands from joined items can be batched together
- if (bdata.settings_use_batching) {
- record_items(p_item_list, p_z);
- return;
- }
-
- // only legacy renders at this stage, batched renderer doesn't render until canvas_render_items_end()
- canvas_render_items_implementation(p_item_list, p_z, p_modulate, p_light, p_base_transform);
+ batch_canvas_render_items(p_item_list, p_z, p_modulate, p_light, p_base_transform);
}
void RasterizerCanvasGLES2::canvas_render_items_implementation(Item *p_item_list, int p_z, const Color &p_modulate, Light *p_light, const Transform2D &p_base_transform) {
@@ -1991,7 +1262,7 @@ void RasterizerCanvasGLES2::canvas_render_items_implementation(Item *p_item_list
while (p_item_list) {
Item *ci = p_item_list;
- _canvas_render_item(ci, ris);
+ _legacy_canvas_render_item(ci, ris);
p_item_list = p_item_list->next;
}
}
@@ -2053,19 +1324,31 @@ bool RasterizerCanvasGLES2::try_join_item(Item *p_ci, RenderItemState &r_ris, bo
}
}
+ bool skeleton_prevent_join = false;
+
bool use_skeleton = skeleton != NULL;
if (r_ris.prev_use_skeleton != use_skeleton) {
- r_ris.rebind_shader = true;
+
+ if (!bdata.settings_use_software_skinning)
+ r_ris.rebind_shader = true;
+
r_ris.prev_use_skeleton = use_skeleton;
- join = false;
+ // join = false;
+ skeleton_prevent_join = true;
}
if (skeleton) {
- join = false;
+ // join = false;
+ skeleton_prevent_join = true;
state.using_skeleton = true;
} else {
state.using_skeleton = false;
}
+
+ if (skeleton_prevent_join) {
+ if (!bdata.settings_use_software_skinning)
+ join = false;
+ }
}
Item *material_owner = p_ci->material_owner ? p_ci->material_owner : p_ci;
@@ -2119,17 +1402,48 @@ bool RasterizerCanvasGLES2::try_join_item(Item *p_ci, RenderItemState &r_ris, bo
bdata.joined_item_batch_flags = 0;
if (r_ris.shader_cache) {
- unsigned int and_flags = r_ris.shader_cache->canvas_item.batch_flags & (RasterizerStorageGLES2::Shader::CanvasItem::PREVENT_COLOR_BAKING | RasterizerStorageGLES2::Shader::CanvasItem::PREVENT_VERTEX_BAKING);
+ unsigned int and_flags = r_ris.shader_cache->canvas_item.batch_flags & (RasterizerStorageCommon::PREVENT_COLOR_BAKING | RasterizerStorageCommon::PREVENT_VERTEX_BAKING);
if (and_flags) {
- bool break_batching = true;
+ bool use_larger_fvfs = true;
- if (and_flags == RasterizerStorageGLES2::Shader::CanvasItem::PREVENT_COLOR_BAKING) {
+ if (and_flags == RasterizerStorageCommon::PREVENT_COLOR_BAKING) {
+ // in some circumstances, if the modulate is identity, we still allow baking because reading modulate / color
+ // will still be okay to do in the shader with no ill effects
+ if (r_ris.final_modulate == Color(1, 1, 1, 1)) {
+ use_larger_fvfs = false;
+ }
+ }
+
+ // new .. always use large FVF
+ if (use_larger_fvfs) {
+ if (and_flags == RasterizerStorageCommon::PREVENT_COLOR_BAKING) {
+ bdata.joined_item_batch_flags |= RasterizerStorageCommon::USE_MODULATE_FVF;
+ } else {
+ // we need to save on the joined item that it should use large fvf.
+ // This info will then be used in filling and rendering
+ bdata.joined_item_batch_flags |= RasterizerStorageCommon::USE_LARGE_FVF;
+ }
+
+ bdata.joined_item_batch_flags |= r_ris.shader_cache->canvas_item.batch_flags;
+ }
+
+ /*
+ if (and_flags == RasterizerStorageCommon::PREVENT_COLOR_BAKING) {
// in some circumstances, if the modulate is identity, we still allow baking because reading modulate / color
// will still be okay to do in the shader with no ill effects
if (r_ris.final_modulate == Color(1, 1, 1, 1)) {
break_batching = false;
}
+ else
+ {
+ // new .. large FVF
+ break_batching = false;
+
+ // we need to save on the joined item that it should use large fvf.
+ // This info will then be used in filling and rendering
+ bdata.joined_item_batch_flags |= RasterizerStorageCommon::USE_LARGE_FVF;
+ }
}
if (break_batching) {
@@ -2139,6 +1453,7 @@ bool RasterizerCanvasGLES2::try_join_item(Item *p_ci, RenderItemState &r_ris, bo
// save the flags so that they don't need to be recalculated in the 2nd pass
bdata.joined_item_batch_flags |= r_ris.shader_cache->canvas_item.batch_flags;
}
+ */
}
}
@@ -2228,6 +1543,8 @@ bool RasterizerCanvasGLES2::try_join_item(Item *p_ci, RenderItemState &r_ris, bo
if (!light_allow_join) {
// can't join
join = false;
+ // we also dont want to allow joining this item with the next item, because the next item could have no lights!
+ r_batch_break = true;
}
} else {
@@ -2247,69 +1564,18 @@ bool RasterizerCanvasGLES2::try_join_item(Item *p_ci, RenderItemState &r_ris, bo
}
// non rects will break the batching anyway, we don't want to record item changes, detect this
- if (!r_batch_break && _detect_batch_break(p_ci)) {
+ if (!r_batch_break && _detect_item_batch_break(r_ris, p_ci, r_batch_break)) {
join = false;
+
r_batch_break = true;
}
return join;
}
-bool RasterizerCanvasGLES2::_detect_batch_break(Item *p_ci) {
- int command_count = p_ci->commands.size();
-
- // Any item that contains commands that are default
- // (i.e. not handled by software transform and the batching renderer) should not be joined.
-
- // In order to work this out, it does a lookahead through the commands,
- // which could potentially be very expensive. As such it makes sense to put a limit on this
- // to some small number, which will catch nearly all cases which need joining,
- // but not be overly expensive in the case of items with large numbers of commands.
-
- // It is hard to know what this number should be, empirically,
- // and this has not been fully investigated. It works to join single sprite items when set to 1 or above.
- // Note that there is a cost to increasing this because it has to look in advance through
- // the commands.
- // On the other hand joining items where possible will usually be better up to a certain
- // number where the cost of software transform is higher than separate drawcalls with hardware
- // transform.
-
- // if there are more than this number of commands in the item, we
- // don't allow joining (separate state changes, and hardware transform)
- // This is set to quite a conservative (low) number until investigated properly.
- // const int MAX_JOIN_ITEM_COMMANDS = 16;
-
- if (command_count > bdata.settings_max_join_item_commands) {
- return true;
- } else {
- Item::Command *const *commands = p_ci->commands.ptr();
-
- // do as many commands as possible until the vertex buffer will be full up
- for (int command_num = 0; command_num < command_count; command_num++) {
-
- Item::Command *command = commands[command_num];
- CRASH_COND(!command);
-
- switch (command->type) {
-
- default: {
- return true;
- } break;
- case Item::Command::TYPE_RECT:
- case Item::Command::TYPE_TRANSFORM: {
- } break;
- } // switch
-
- } // for through commands
-
- } // else
-
- return false;
-}
-
// Legacy non-batched implementation for regression testing.
// Should be removed after testing phase to avoid duplicate codepaths.
-void RasterizerCanvasGLES2::_canvas_render_item(Item *p_ci, RenderItemState &r_ris) {
+void RasterizerCanvasGLES2::_legacy_canvas_render_item(Item *p_ci, RenderItemState &r_ris) {
storage->info.render._2d_item_count++;
if (r_ris.current_clip != p_ci->final_clip_owner) {
@@ -2536,7 +1802,7 @@ void RasterizerCanvasGLES2::_canvas_render_item(Item *p_ci, RenderItemState &r_r
_set_uniforms();
if (unshaded || (state.uniforms.final_modulate.a > 0.001 && (!r_ris.shader_cache || r_ris.shader_cache->canvas_item.light_mode != RasterizerStorageGLES2::Shader::CanvasItem::LIGHT_MODE_LIGHT_ONLY) && !p_ci->light_masked))
- _canvas_item_render_commands(p_ci, NULL, reclip, material_ptr);
+ _legacy_canvas_item_render_commands(p_ci, NULL, reclip, material_ptr);
r_ris.rebind_shader = true; // hacked in for now.
@@ -2615,7 +1881,7 @@ void RasterizerCanvasGLES2::_canvas_render_item(Item *p_ci, RenderItemState &r_r
}
glActiveTexture(GL_TEXTURE0);
- _canvas_item_render_commands(p_ci, NULL, reclip, material_ptr); //redraw using light
+ _legacy_canvas_item_render_commands(p_ci, NULL, reclip, material_ptr); //redraw using light
state.using_light = NULL;
}
@@ -2646,6 +1912,7 @@ void RasterizerCanvasGLES2::_canvas_render_item(Item *p_ci, RenderItemState &r_r
ci->final_modulate.b * p_modulate.b,
ci->final_modulate.a * p_modulate.a );
+
state.canvas_shader.set_uniform(CanvasShaderGLES2::MODELVIEW_MATRIX,state.final_transform);
state.canvas_shader.set_uniform(CanvasShaderGLES2::EXTRA_MATRIX,Transform2D());
state.canvas_shader.set_uniform(CanvasShaderGLES2::FINAL_MODULATE,state.canvas_item_modulate);
@@ -2717,9 +1984,9 @@ void RasterizerCanvasGLES2::render_joined_item(const BItemJoined &p_bij, RenderI
}
}
- RasterizerStorageGLES2::Skeleton *skeleton = NULL;
+ if (!bdata.settings_use_batching || !bdata.settings_use_software_skinning) {
+ RasterizerStorageGLES2::Skeleton *skeleton = NULL;
- {
//skeleton handling
if (ci->skeleton.is_valid() && storage->skeleton_owner.owns(ci->skeleton)) {
skeleton = storage->skeleton_owner.get(ci->skeleton);
@@ -2746,7 +2013,8 @@ void RasterizerCanvasGLES2::render_joined_item(const BItemJoined &p_bij, RenderI
} else {
state.using_skeleton = false;
}
- }
+
+ } // if not using batching
Item *material_owner = ci->material_owner ? ci->material_owner : ci;
@@ -3055,6 +2323,7 @@ void RasterizerCanvasGLES2::render_joined_item(const BItemJoined &p_bij, RenderI
ci->final_modulate.b * p_modulate.b,
ci->final_modulate.a * p_modulate.a );
+
state.canvas_shader.set_uniform(CanvasShaderGLES2::MODELVIEW_MATRIX,state.final_transform);
state.canvas_shader.set_uniform(CanvasShaderGLES2::EXTRA_MATRIX,Transform2D());
state.canvas_shader.set_uniform(CanvasShaderGLES2::FINAL_MODULATE,state.canvas_item_modulate);
@@ -3081,246 +2350,19 @@ void RasterizerCanvasGLES2::render_joined_item(const BItemJoined &p_bij, RenderI
}
}
-bool RasterizerCanvasGLES2::_light_find_intersection(const Rect2 &p_item_rect, const Transform2D &p_light_xform, const Rect2 &p_light_rect, Rect2 &r_cliprect) const {
- // transform light to world space (note this is done in the earlier intersection test, so could
- // be made more efficient)
- Vector2 pts[4] = {
- p_light_xform.xform(p_light_rect.position),
- p_light_xform.xform(Vector2(p_light_rect.position.x + p_light_rect.size.x, p_light_rect.position.y)),
- p_light_xform.xform(Vector2(p_light_rect.position.x, p_light_rect.position.y + p_light_rect.size.y)),
- p_light_xform.xform(Vector2(p_light_rect.position.x + p_light_rect.size.x, p_light_rect.position.y + p_light_rect.size.y)),
- };
-
- // calculate the light bound rect in world space
- Rect2 lrect(pts[0].x, pts[0].y, 0, 0);
- for (int n = 1; n < 4; n++) {
- lrect.expand_to(pts[n]);
- }
-
- // intersection between the 2 rects
- // they should probably always intersect, because of earlier check, but just in case...
- if (!p_item_rect.intersects(lrect))
- return false;
-
- // note this does almost the same as Rect2.clip but slightly more efficient for our use case
- r_cliprect.position.x = MAX(p_item_rect.position.x, lrect.position.x);
- r_cliprect.position.y = MAX(p_item_rect.position.y, lrect.position.y);
-
- Point2 item_rect_end = p_item_rect.position + p_item_rect.size;
- Point2 lrect_end = lrect.position + lrect.size;
-
- r_cliprect.size.x = MIN(item_rect_end.x, lrect_end.x) - r_cliprect.position.x;
- r_cliprect.size.y = MIN(item_rect_end.y, lrect_end.y) - r_cliprect.position.y;
-
- return true;
-}
-
-bool RasterizerCanvasGLES2::_light_scissor_begin(const Rect2 &p_item_rect, const Transform2D &p_light_xform, const Rect2 &p_light_rect) const {
-
- float area_item = p_item_rect.size.x * p_item_rect.size.y; // double check these are always positive
-
- // quick reject .. the area of pixels saved can never be more than the area of the item
- if (area_item < bdata.scissor_threshold_area) {
- return false;
- }
-
- Rect2 cliprect;
- if (!_light_find_intersection(p_item_rect, p_light_xform, p_light_rect, cliprect)) {
- // should not really occur .. but just in case
- cliprect = Rect2(0, 0, 0, 0);
- } else {
- // some conditions not to scissor
- // determine the area (fill rate) that will be saved
- float area_cliprect = cliprect.size.x * cliprect.size.y;
- float area_saved = area_item - area_cliprect;
-
- // if area saved is too small, don't scissor
- if (area_saved < bdata.scissor_threshold_area) {
- return false;
- }
- }
-
+void RasterizerCanvasGLES2::gl_enable_scissor(int p_x, int p_y, int p_width, int p_height) const {
glEnable(GL_SCISSOR_TEST);
- int y = storage->frame.current_rt->height - (cliprect.position.y + cliprect.size.y);
- if (storage->frame.current_rt->flags[RasterizerStorage::RENDER_TARGET_VFLIP])
- y = cliprect.position.y;
- glScissor(cliprect.position.x, y, cliprect.size.width, cliprect.size.height);
-
- return true;
+ glScissor(p_x, p_y, p_width, p_height);
}
-void RasterizerCanvasGLES2::_calculate_scissor_threshold_area() {
- if (!bdata.settings_scissor_lights) {
- return;
- }
-
- // scissor area threshold is 0.0 to 1.0 in the settings for ease of use.
- // we need to translate to an absolute area to determine quickly whether
- // to scissor.
- if (bdata.settings_scissor_threshold < 0.0001f) {
- bdata.scissor_threshold_area = -1.0f; // will always pass
- } else {
- // in pixels
- int w = storage->frame.current_rt->width;
- int h = storage->frame.current_rt->height;
-
- int screen_area = w * h;
-
- bdata.scissor_threshold_area = bdata.settings_scissor_threshold * screen_area;
- }
+void RasterizerCanvasGLES2::gl_disable_scissor() const {
+ glDisable(GL_SCISSOR_TEST);
}
void RasterizerCanvasGLES2::initialize() {
RasterizerCanvasBaseGLES2::initialize();
- bdata.settings_use_batching = GLOBAL_GET("rendering/batching/options/use_batching");
- bdata.settings_max_join_item_commands = GLOBAL_GET("rendering/batching/parameters/max_join_item_commands");
- bdata.settings_colored_vertex_format_threshold = GLOBAL_GET("rendering/batching/parameters/colored_vertex_format_threshold");
- bdata.settings_item_reordering_lookahead = GLOBAL_GET("rendering/batching/parameters/item_reordering_lookahead");
- bdata.settings_light_max_join_items = GLOBAL_GET("rendering/batching/lights/max_join_items");
- bdata.settings_use_single_rect_fallback = GLOBAL_GET("rendering/batching/options/single_rect_fallback");
-
- // alternatively only enable uv contract if pixel snap in use,
- // but with this enable bool, it should not be necessary
- bdata.settings_uv_contract = GLOBAL_GET("rendering/batching/precision/uv_contract");
- bdata.settings_uv_contract_amount = (float)GLOBAL_GET("rendering/batching/precision/uv_contract_amount") / 1000000.0f;
-
- // we can use the threshold to determine whether to turn scissoring off or on
- bdata.settings_scissor_threshold = GLOBAL_GET("rendering/batching/lights/scissor_area_threshold");
- if (bdata.settings_scissor_threshold > 0.999f) {
- bdata.settings_scissor_lights = false;
- } else {
- bdata.settings_scissor_lights = true;
-
- // apply power of 4 relationship for the area, as most of the important changes
- // will be happening at low values of scissor threshold
- bdata.settings_scissor_threshold *= bdata.settings_scissor_threshold;
- bdata.settings_scissor_threshold *= bdata.settings_scissor_threshold;
- }
-
- // The sweet spot on my desktop for cache is actually smaller than the max, and this
- // is the default. This saves memory too so we will use it for now, needs testing to see whether this varies according
- // to device / platform.
- bdata.settings_batch_buffer_num_verts = GLOBAL_GET("rendering/batching/parameters/batch_buffer_size");
-
- // override the use_batching setting in the editor
- // (note that if the editor can't start, you can't change the use_batching project setting!)
- if (Engine::get_singleton()->is_editor_hint()) {
- bool use_in_editor = GLOBAL_GET("rendering/batching/options/use_batching_in_editor");
- bdata.settings_use_batching = use_in_editor;
-
- // fix some settings in the editor, as the performance not worth the risk
- bdata.settings_use_single_rect_fallback = false;
- }
-
- // if we are using batching, we will purposefully disable the nvidia workaround.
- // This is because the only reason to use the single rect fallback is the approx 2x speed
- // of the uniform drawing technique. If we used nvidia workaround, speed would be
- // approx equal to the batcher drawing technique (indexed primitive + VB).
- if (bdata.settings_use_batching) {
- use_nvidia_rect_workaround = false;
- }
-
- // For debugging, if flash is set in project settings, it will flash on alternate frames
- // between the non-batched renderer and the batched renderer,
- // in order to find regressions.
- // This should not be used except during development.
- // make a note of the original choice in case we are flashing on and off the batching
- bdata.settings_use_batching_original_choice = bdata.settings_use_batching;
- bdata.settings_flash_batching = GLOBAL_GET("rendering/batching/debug/flash_batching");
- if (!bdata.settings_use_batching) {
- // no flash when batching turned off
- bdata.settings_flash_batching = false;
- }
-
- // frame diagnosis. print out the batches every nth frame
- bdata.settings_diagnose_frame = false;
- if (!Engine::get_singleton()->is_editor_hint() && bdata.settings_use_batching) {
- bdata.settings_diagnose_frame = GLOBAL_GET("rendering/batching/debug/diagnose_frame");
- }
-
- // the maximum num quads in a batch is limited by GLES2. We can have only 16 bit indices,
- // which means we can address a vertex buffer of max size 65535. 4 vertices are needed per quad.
-
- // Note this determines the memory use by the vertex buffer vector. max quads (65536/4)-1
- // but can be reduced to save memory if really required (will result in more batches though)
- const int max_possible_quads = (65536 / 4) - 1;
- const int min_possible_quads = 8; // some reasonable small value
-
- // value from project settings
- int max_quads = bdata.settings_batch_buffer_num_verts / 4;
-
- // sanity checks
- max_quads = CLAMP(max_quads, min_possible_quads, max_possible_quads);
- bdata.settings_max_join_item_commands = CLAMP(bdata.settings_max_join_item_commands, 0, 65535);
- bdata.settings_colored_vertex_format_threshold = CLAMP(bdata.settings_colored_vertex_format_threshold, 0.0f, 1.0f);
- bdata.settings_scissor_threshold = CLAMP(bdata.settings_scissor_threshold, 0.0f, 1.0f);
- bdata.settings_light_max_join_items = CLAMP(bdata.settings_light_max_join_items, 0, 65535);
- bdata.settings_item_reordering_lookahead = CLAMP(bdata.settings_item_reordering_lookahead, 0, 65535);
-
- // for debug purposes, output a string with the batching options
- String batching_options_string = "OpenGL ES 2.0 Batching: ";
- if (bdata.settings_use_batching) {
- batching_options_string += "ON";
-
- if (OS::get_singleton()->is_stdout_verbose()) {
- batching_options_string += "\n\tOPTIONS\n";
- batching_options_string += "\tmax_join_item_commands " + itos(bdata.settings_max_join_item_commands) + "\n";
- batching_options_string += "\tcolored_vertex_format_threshold " + String(Variant(bdata.settings_colored_vertex_format_threshold)) + "\n";
- batching_options_string += "\tbatch_buffer_size " + itos(bdata.settings_batch_buffer_num_verts) + "\n";
- batching_options_string += "\tlight_scissor_area_threshold " + String(Variant(bdata.settings_scissor_threshold)) + "\n";
-
- batching_options_string += "\titem_reordering_lookahead " + itos(bdata.settings_item_reordering_lookahead) + "\n";
- batching_options_string += "\tlight_max_join_items " + itos(bdata.settings_light_max_join_items) + "\n";
- batching_options_string += "\tsingle_rect_fallback " + String(Variant(bdata.settings_use_single_rect_fallback)) + "\n";
-
- batching_options_string += "\tdebug_flash " + String(Variant(bdata.settings_flash_batching)) + "\n";
- batching_options_string += "\tdiagnose_frame " + String(Variant(bdata.settings_diagnose_frame));
- }
-
- print_line(batching_options_string);
- }
-
- // special case, for colored vertex format threshold.
- // as the comparison is >=, we want to be able to totally turn on or off
- // conversion to colored vertex format at the extremes, so we will force
- // 1.0 to be just above 1.0
- if (bdata.settings_colored_vertex_format_threshold > 0.995f) {
- bdata.settings_colored_vertex_format_threshold = 1.01f;
- }
-
- // save memory when batching off
- if (!bdata.settings_use_batching) {
- max_quads = 0;
- }
-
- uint32_t sizeof_batch_vert = sizeof(BatchVertex);
-
- bdata.max_quads = max_quads;
-
- // 4 verts per quad
- bdata.vertex_buffer_size_units = max_quads * 4;
-
- // the index buffer can be longer than 65535, only the indices need to be within this range
- bdata.index_buffer_size_units = max_quads * 6;
-
- // this comes out at approx 64K for non-colored vertex buffer, and 128K for colored vertex buffer
- bdata.vertex_buffer_size_bytes = bdata.vertex_buffer_size_units * sizeof_batch_vert;
- bdata.index_buffer_size_bytes = bdata.index_buffer_size_units * 2; // 16 bit inds
-
- // create equal number of normal and (max) unit sized verts (as the normal may need to be translated to a larger FVF)
- bdata.vertices.create(bdata.vertex_buffer_size_units); // 512k
- bdata.unit_vertices.create(bdata.vertices.max_size(), sizeof(BatchVertexLightAngled));
-
- // extra data per vert needed for larger FVFs
- bdata.light_angles.create(bdata.vertices.max_size());
-
- // num batches will be auto increased dynamically if required
- bdata.batches.create(1024);
- bdata.batches_temp.create(bdata.batches.max_size());
-
- // batch textures can also be increased dynamically
- bdata.batch_textures.create(32);
+ batch_initialize();
// just reserve some space (may not be needed as we are orphaning, but hey ho)
glGenBuffers(1, &bdata.gl_vertex_buffer);
@@ -3337,7 +2379,7 @@ void RasterizerCanvasGLES2::initialize() {
Vector indices;
indices.resize(bdata.index_buffer_size_units);
- for (int q = 0; q < max_quads; q++) {
+ for (unsigned int q = 0; q < bdata.max_quads; q++) {
int i_pos = q * 6; // 6 inds per quad
int q_pos = q * 4; // 4 verts per quad
indices.set(i_pos, q_pos);
@@ -3361,5 +2403,5 @@ void RasterizerCanvasGLES2::initialize() {
RasterizerCanvasGLES2::RasterizerCanvasGLES2() {
- bdata.settings_use_batching = false;
+ batch_constructor();
}
diff --git a/drivers/gles2/rasterizer_canvas_gles2.h b/drivers/gles2/rasterizer_canvas_gles2.h
index 8fd2a887462..b5518d336fc 100644
--- a/drivers/gles2/rasterizer_canvas_gles2.h
+++ b/drivers/gles2/rasterizer_canvas_gles2.h
@@ -31,297 +31,14 @@
#ifndef RASTERIZERCANVASGLES2_H
#define RASTERIZERCANVASGLES2_H
+#include "drivers/gles_common/rasterizer_canvas_batcher.h"
#include "rasterizer_canvas_base_gles2.h"
class RasterizerSceneGLES2;
-class RasterizerCanvasGLES2 : public RasterizerCanvasBaseGLES2 {
+class RasterizerCanvasGLES2 : public RasterizerCanvasBaseGLES2, public RasterizerCanvasBatcher {
- // used to determine whether we use hardware transform (none)
- // software transform all verts, or software transform just a translate
- // (no rotate or scale)
- enum TransformMode {
- TM_NONE,
- TM_ALL,
- TM_TRANSLATE,
- };
-
- // pod versions of vector and color and RID, need to be 32 bit for vertex format
- struct BatchVector2 {
- float x, y;
- void set(const Vector2 &p_o) {
- x = p_o.x;
- y = p_o.y;
- }
- void to(Vector2 &r_o) const {
- r_o.x = x;
- r_o.y = y;
- }
- };
-
- struct BatchColor {
- float r, g, b, a;
- void set(const Color &p_c) {
- r = p_c.r;
- g = p_c.g;
- b = p_c.b;
- a = p_c.a;
- }
- bool operator==(const BatchColor &p_c) const {
- return (r == p_c.r) && (g == p_c.g) && (b == p_c.b) && (a == p_c.a);
- }
- bool operator!=(const BatchColor &p_c) const { return (*this == p_c) == false; }
- bool equals(const Color &p_c) const {
- return (r == p_c.r) && (g == p_c.g) && (b == p_c.b) && (a == p_c.a);
- }
- const float *get_data() const { return &r; }
- String to_string() const;
- };
-
- struct BatchVertex {
- // must be 32 bit pod
- BatchVector2 pos;
- BatchVector2 uv;
- };
-
- struct BatchVertexColored : public BatchVertex {
- // must be 32 bit pod
- BatchColor col;
- };
-
- struct BatchVertexLightAngled : public BatchVertexColored {
- // must be pod
- float light_angle;
- };
-
- struct Batch {
- enum CommandType : uint32_t {
- BT_DEFAULT,
- BT_RECT,
- };
-
- CommandType type;
- uint32_t first_command; // also item reference number
- uint32_t num_commands;
- uint32_t first_quad;
- uint32_t batch_texture_id;
- BatchColor color;
- };
-
- struct BatchTex {
- enum TileMode : uint32_t {
- TILE_OFF,
- TILE_NORMAL,
- TILE_FORCE_REPEAT,
- };
- RID RID_texture;
- RID RID_normal;
- TileMode tile_mode;
- BatchVector2 tex_pixel_size;
- uint32_t flags;
- };
-
- // items in a list to be sorted prior to joining
- struct BSortItem {
- // have a function to keep as pod, rather than operator
- void assign(const BSortItem &o) {
- item = o.item;
- z_index = o.z_index;
- }
- Item *item;
- int z_index;
- };
-
- // batch item may represent 1 or more items
- struct BItemJoined {
- uint32_t first_item_ref;
- uint32_t num_item_refs;
-
- Rect2 bounding_rect;
-
- // note the z_index may only be correct for the first of the joined item references
- // this has implications for light culling with z ranged lights.
- int16_t z_index;
-
- // these are defined in RasterizerStorageGLES2::Shader::CanvasItem::BatchFlags
- uint16_t flags;
-
- // we are always splitting items with lots of commands,
- // and items with unhandled primitives (default)
- bool use_hardware_transform() const { return num_item_refs == 1; }
- };
-
- struct BItemRef {
- Item *item;
- Color final_modulate;
- };
-
- struct BLightRegion {
- void reset() {
- light_bitfield = 0;
- shadow_bitfield = 0;
- too_many_lights = false;
- }
- uint64_t light_bitfield;
- uint64_t shadow_bitfield;
- bool too_many_lights; // we can only do light region optimization if there are 64 or less lights
- };
-
- struct BatchData {
- BatchData();
- void reset_flush() {
- batches.reset();
- batch_textures.reset();
-
- vertices.reset();
- light_angles.reset();
-
- total_quads = 0;
- total_color_changes = 0;
- use_light_angles = false;
- }
-
- GLuint gl_vertex_buffer;
- GLuint gl_index_buffer;
-
- uint32_t max_quads;
- uint32_t vertex_buffer_size_units;
- uint32_t vertex_buffer_size_bytes;
- uint32_t index_buffer_size_units;
- uint32_t index_buffer_size_bytes;
-
- // small vertex FVF type - pos and UV.
- // This will always be written to initially, but can be translated
- // to larger FVFs if necessary.
- RasterizerArrayGLES2 vertices;
-
- // extra data which can be stored during prefilling, for later translation to larger FVFs
- RasterizerArrayGLES2 light_angles;
-
- // instead of having a different buffer for each vertex FVF type
- // we have a special array big enough for the biggest FVF
- // which can have a changeable unit size, and reuse it.
- RasterizerUnitArrayGLES2 unit_vertices;
-
- RasterizerArrayGLES2 batches;
- RasterizerArrayGLES2 batches_temp; // used for translating to colored vertex batches
- RasterizerArray_non_pod_GLES2 batch_textures; // the only reason this is non-POD is because of RIDs
-
- // flexible vertex format.
- // all verts have pos and UV.
- // some have color, some light angles etc.
- bool use_colored_vertices;
- bool use_light_angles;
-
- RasterizerArrayGLES2 items_joined;
- RasterizerArrayGLES2 item_refs;
-
- // items are sorted prior to joining
- RasterizerArrayGLES2 sort_items;
-
- // counts
- int total_quads;
-
- // we keep a record of how many color changes caused new batches
- // if the colors are causing an excessive number of batches, we switch
- // to alternate batching method and add color to the vertex format.
- int total_color_changes;
-
- // if the shader is using MODULATE, we prevent baking color so the final_modulate can
- // be read in the shader.
- // if the shader is reading VERTEX, we prevent baking vertex positions with extra matrices etc
- // to prevent the read position being incorrect.
- // These flags are defined in RasterizerStorageGLES2::Shader::CanvasItem::BatchFlags
- uint32_t joined_item_batch_flags;
-
- // measured in pixels, recalculated each frame
- float scissor_threshold_area;
-
- // diagnose this frame, every nTh frame when settings_diagnose_frame is on
- bool diagnose_frame;
- String frame_string;
- uint32_t next_diagnose_tick;
- uint64_t diagnose_frame_number;
-
- // whether to join items across z_indices - this can interfere with z ranged lights,
- // so has to be disabled in some circumstances
- bool join_across_z_indices;
-
- // global settings
- bool settings_use_batching; // the current use_batching (affected by flash)
- bool settings_use_batching_original_choice; // the choice entered in project settings
- bool settings_flash_batching; // for regression testing, flash between non-batched and batched renderer
- bool settings_diagnose_frame; // print out batches to help optimize / regression test
- int settings_max_join_item_commands;
- float settings_colored_vertex_format_threshold;
- int settings_batch_buffer_num_verts;
- bool settings_scissor_lights;
- float settings_scissor_threshold; // 0.0 to 1.0
- int settings_item_reordering_lookahead;
- bool settings_use_single_rect_fallback;
- int settings_light_max_join_items;
-
- // uv contraction
- bool settings_uv_contract;
- float settings_uv_contract_amount;
-
- // only done on diagnose frame
- void reset_stats() {
- stats_items_sorted = 0;
- stats_light_items_joined = 0;
- }
-
- // frame stats (just for monitoring and debugging)
- int stats_items_sorted;
- int stats_light_items_joined;
- } bdata;
-
- struct RenderItemState {
- RenderItemState() { reset(); }
- void reset();
- Item *current_clip;
- RasterizerStorageGLES2::Shader *shader_cache;
- bool rebind_shader;
- bool prev_use_skeleton;
- int last_blend_mode;
- RID canvas_last_material;
- Color final_modulate;
-
- // used for joining items only
- BItemJoined *joined_item;
- bool join_batch_break;
- BLightRegion light_region;
-
- // 'item group' is data over a single call to canvas_render_items
- int item_group_z;
- Color item_group_modulate;
- Light *item_group_light;
- Transform2D item_group_base_transform;
- } _render_item_state;
-
- struct FillState {
- void reset() {
- // don't reset members that need to be preserved after flushing
- // half way through a list of commands
- curr_batch = 0;
- batch_tex_id = -1;
- texpixel_size = Vector2(1, 1);
- contract_uvs = false;
- }
- Batch *curr_batch;
- int batch_tex_id;
- bool use_hardware_transform;
- bool contract_uvs;
- Vector2 texpixel_size;
- Color final_modulate;
- TransformMode transform_mode;
- TransformMode orig_transform_mode;
-
- // support for extra matrices
- bool extra_matrix_sent; // whether sent on this item (in which case software transform can't be used untl end of item)
- int transform_extra_command_number_p1; // plus one to allow fast checking against zero
- Transform2D transform_combined; // final * extra
- };
+ friend class RasterizerCanvasBatcher;
public:
virtual void canvas_render_items_begin(const Color &p_modulate, Light *p_light, const Transform2D &p_base_transform);
@@ -332,598 +49,27 @@ public:
private:
// legacy codepath .. to remove after testing
- void _canvas_render_item(Item *p_ci, RenderItemState &r_ris);
- void _canvas_item_render_commands(Item *p_item, Item *p_current_clip, bool &r_reclip, RasterizerStorageGLES2::Material *p_material);
+ void _legacy_canvas_render_item(Item *p_ci, RenderItemState &r_ris);
// high level batch funcs
void canvas_render_items_implementation(Item *p_item_list, int p_z, const Color &p_modulate, Light *p_light, const Transform2D &p_base_transform);
void render_joined_item(const BItemJoined &p_bij, RenderItemState &r_ris);
- void record_items(Item *p_item_list, int p_z);
- void join_items(Item *p_item_list, int p_z);
- void join_sorted_items();
bool try_join_item(Item *p_ci, RenderItemState &r_ris, bool &r_batch_break);
- void render_joined_item_commands(const BItemJoined &p_bij, Item *p_current_clip, bool &r_reclip, RasterizerStorageGLES2::Material *p_material, bool p_lit);
void render_batches(Item::Command *const *p_commands, Item *p_current_clip, bool &r_reclip, RasterizerStorageGLES2::Material *p_material);
- bool prefill_joined_item(FillState &r_fill_state, int &r_command_start, Item *p_item, Item *p_current_clip, bool &r_reclip, RasterizerStorageGLES2::Material *p_material);
-
- void flush_render_batches(Item *p_first_item, Item *p_current_clip, bool &r_reclip, RasterizerStorageGLES2::Material *p_material);
-
// low level batch funcs
- int _batch_find_or_create_tex(const RID &p_texture, const RID &p_normal, bool p_tile, int p_previous_match);
- RasterizerStorageGLES2::Texture *_get_canvas_texture(const RID &p_texture) const;
void _batch_upload_buffers();
void _batch_render_rects(const Batch &p_batch, RasterizerStorageGLES2::Material *p_material);
- BatchVertex *_batch_vertex_request_new() { return bdata.vertices.request(); }
- Batch *_batch_request_new(bool p_blank = true);
+ void _batch_render_polys(const Batch &p_batch, RasterizerStorageGLES2::Material *p_material);
+ void _batch_render_lines(const Batch &p_batch, RasterizerStorageGLES2::Material *p_material, bool p_anti_alias);
- bool _detect_batch_break(Item *p_ci);
- void _software_transform_vertex(BatchVector2 &r_v, const Transform2D &p_tr) const;
- void _software_transform_vertex(Vector2 &r_v, const Transform2D &p_tr) const;
- TransformMode _find_transform_mode(const Transform2D &p_tr) const;
- void _prefill_default_batch(FillState &r_fill_state, int p_command_num, const Item &p_item);
-
- // sorting
- void sort_items();
- bool sort_items_from(int p_start);
- bool _sort_items_match(const BSortItem &p_a, const BSortItem &p_b) const;
-
- // light scissoring
- bool _light_find_intersection(const Rect2 &p_item_rect, const Transform2D &p_light_xform, const Rect2 &p_light_rect, Rect2 &r_cliprect) const;
- bool _light_scissor_begin(const Rect2 &p_item_rect, const Transform2D &p_light_xform, const Rect2 &p_light_rect) const;
- void _calculate_scissor_threshold_area();
-
- // no need to compile these in in release, they are unneeded outside the editor and only add to executable size
-#ifdef DEBUG_ENABLED
- void diagnose_batches(Item::Command *const *p_commands);
- String get_command_type_string(const Item::Command &p_command) const;
-#endif
+ // funcs used from rasterizer_canvas_batcher template
+ void gl_enable_scissor(int p_x, int p_y, int p_width, int p_height) const;
+ void gl_disable_scissor() const;
public:
void initialize();
RasterizerCanvasGLES2();
-
-private:
- template
- bool prefill_rect(Item::CommandRect *rect, FillState &r_fill_state, int &r_command_start, int command_num, int command_count, Item::Command *const *commands, Item *p_item, bool multiply_final_modulate);
-
- template
- void _translate_batches_to_larger_FVF();
};
-//////////////////////////////////////////////////////////////
-
-// Default batches will not occur in software transform only items
-// EXCEPT IN THE CASE OF SINGLE RECTS (and this may well not occur, check the logic in prefill_join_item TYPE_RECT)
-// but can occur where transform commands have been sent during hardware batch
-inline void RasterizerCanvasGLES2::_prefill_default_batch(FillState &r_fill_state, int p_command_num, const Item &p_item) {
- if (r_fill_state.curr_batch->type == Batch::BT_DEFAULT) {
- // don't need to flush an extra transform command?
- if (!r_fill_state.transform_extra_command_number_p1) {
- // another default command, just add to the existing batch
- r_fill_state.curr_batch->num_commands++;
- } else {
-#if defined(TOOLS_ENABLED) && defined(DEBUG_ENABLED)
- if (r_fill_state.transform_extra_command_number_p1 != p_command_num) {
- WARN_PRINT_ONCE("_prefill_default_batch : transform_extra_command_number_p1 != p_command_num");
- }
-#endif
- // if the first member of the batch is a transform we have to be careful
- if (!r_fill_state.curr_batch->num_commands) {
- // there can be leading useless extra transforms (sometimes happens with debug collision polys)
- // we need to rejig the first_command for the first useful transform
- r_fill_state.curr_batch->first_command += r_fill_state.transform_extra_command_number_p1 - 1;
- }
-
- // we do have a pending extra transform command to flush
- // either the extra transform is in the prior command, or not, in which case we need 2 batches
- r_fill_state.curr_batch->num_commands += 2;
-
- r_fill_state.transform_extra_command_number_p1 = 0; // mark as sent
- r_fill_state.extra_matrix_sent = true;
-
- // the original mode should always be hardware transform ..
- // test this assumption
- //CRASH_COND(r_fill_state.orig_transform_mode != TM_NONE);
- r_fill_state.transform_mode = r_fill_state.orig_transform_mode;
-
- // do we need to restore anything else?
- }
- } else {
- // end of previous different type batch, so start new default batch
-
- // first consider whether there is a dirty extra matrix to send
- if (r_fill_state.transform_extra_command_number_p1) {
- // get which command the extra is in, and blank all the records as it no longer is stored CPU side
- int extra_command = r_fill_state.transform_extra_command_number_p1 - 1; // plus 1 based
- r_fill_state.transform_extra_command_number_p1 = 0;
- r_fill_state.extra_matrix_sent = true;
-
- // send the extra to the GPU in a batch
- r_fill_state.curr_batch = _batch_request_new();
- r_fill_state.curr_batch->type = Batch::BT_DEFAULT;
- r_fill_state.curr_batch->first_command = extra_command;
- r_fill_state.curr_batch->num_commands = 1;
-
- // revert to the original transform mode
- // e.g. go back to NONE if we were in hardware transform mode
- r_fill_state.transform_mode = r_fill_state.orig_transform_mode;
-
- // reset the original transform if we are going back to software mode,
- // because the extra is now done on the GPU...
- // (any subsequent extras are sent directly to the GPU, no deferring)
- if (r_fill_state.orig_transform_mode != TM_NONE) {
- r_fill_state.transform_combined = p_item.final_transform;
- }
-
- // can possibly combine batch with the next one in some cases
- // this is more efficient than having an extra batch especially for the extra
- if ((extra_command + 1) == p_command_num) {
- r_fill_state.curr_batch->num_commands = 2;
- return;
- }
- }
-
- // start default batch
- r_fill_state.curr_batch = _batch_request_new();
- r_fill_state.curr_batch->type = Batch::BT_DEFAULT;
- r_fill_state.curr_batch->first_command = p_command_num;
- r_fill_state.curr_batch->num_commands = 1;
- }
-}
-
-inline void RasterizerCanvasGLES2::_software_transform_vertex(BatchVector2 &r_v, const Transform2D &p_tr) const {
- Vector2 vc(r_v.x, r_v.y);
- vc = p_tr.xform(vc);
- r_v.set(vc);
-}
-
-inline void RasterizerCanvasGLES2::_software_transform_vertex(Vector2 &r_v, const Transform2D &p_tr) const {
- r_v = p_tr.xform(r_v);
-}
-
-inline RasterizerCanvasGLES2::TransformMode RasterizerCanvasGLES2::_find_transform_mode(const Transform2D &p_tr) const {
- // decided whether to do translate only for software transform
- if ((p_tr.elements[0].x == 1.0) &&
- (p_tr.elements[0].y == 0.0) &&
- (p_tr.elements[1].x == 0.0) &&
- (p_tr.elements[1].y == 1.0)) {
- return TM_TRANSLATE;
- }
-
- return TM_ALL;
-}
-
-inline bool RasterizerCanvasGLES2::_sort_items_match(const BSortItem &p_a, const BSortItem &p_b) const {
- const Item *a = p_a.item;
- const Item *b = p_b.item;
-
- if (b->commands.size() != 1)
- return false;
-
- // tested outside function
- // if (a->commands.size() != 1)
- // return false;
-
- const Item::Command &cb = *b->commands[0];
- if (cb.type != Item::Command::TYPE_RECT)
- return false;
-
- const Item::Command &ca = *a->commands[0];
- // tested outside function
- // if (ca.type != Item::Command::TYPE_RECT)
- // return false;
-
- const Item::CommandRect *rect_a = static_cast(&ca);
- const Item::CommandRect *rect_b = static_cast(&cb);
-
- if (rect_a->texture != rect_b->texture)
- return false;
-
- return true;
-}
-
-//////////////////////////////////////////////////////////////
-// TEMPLATE FUNCS
-
-// Translation always involved adding color to the FVF, which enables
-// joining of batches that have different colors.
-// There is a trade off. Non colored verts are smaller so work faster, but
-// there comes a point where it is better to just use colored verts to avoid lots of
-// batches.
-// In addition this can optionally add light angles to the FVF, necessary for normal mapping.
-template
-void RasterizerCanvasGLES2::_translate_batches_to_larger_FVF() {
-
- // zeros the size and sets up how big each unit is
- bdata.unit_vertices.prepare(sizeof(BATCH_VERTEX_TYPE));
- bdata.batches_temp.reset();
-
- // As the vertices_colored and batches_temp are 'mirrors' of the non-colored version,
- // the sizes should be equal, and allocations should never fail. Hence the use of debug
- // asserts to check program flow, these should not occur at runtime unless the allocation
- // code has been altered.
-#if defined(TOOLS_ENABLED) && defined(DEBUG_ENABLED)
- CRASH_COND(bdata.unit_vertices.max_size() != bdata.vertices.max_size());
- CRASH_COND(bdata.batches_temp.max_size() != bdata.batches.max_size());
-#endif
-
- Color curr_col(-1.0, -1.0, -1.0, -1.0);
-
- Batch *dest_batch = 0;
-
- const float *source_light_angles = &bdata.light_angles[0];
-
- // translate the batches into vertex colored batches
- for (int n = 0; n < bdata.batches.size(); n++) {
- const Batch &source_batch = bdata.batches[n];
-
- // does source batch use light angles?
- const BatchTex &btex = bdata.batch_textures[source_batch.batch_texture_id];
- bool source_batch_uses_light_angles = btex.RID_normal != RID();
-
- bool needs_new_batch = true;
-
- if (dest_batch) {
- if (dest_batch->type == source_batch.type) {
- if (source_batch.type == Batch::BT_RECT) {
- if (dest_batch->batch_texture_id == source_batch.batch_texture_id) {
- // add to previous batch
- dest_batch->num_commands += source_batch.num_commands;
- needs_new_batch = false;
-
- // create the colored verts (only if not default)
- int first_vert = source_batch.first_quad * 4;
- int end_vert = 4 * (source_batch.first_quad + source_batch.num_commands);
-
- for (int v = first_vert; v < end_vert; v++) {
- const BatchVertex &bv = bdata.vertices[v];
- BATCH_VERTEX_TYPE *cv = (BatchVertexLightAngled *)bdata.unit_vertices.request();
-#if defined(TOOLS_ENABLED) && defined(DEBUG_ENABLED)
- CRASH_COND(!cv);
-#endif
- cv->pos = bv.pos;
- cv->uv = bv.uv;
- cv->col = source_batch.color;
-
- if (INCLUDE_LIGHT_ANGLES) {
- // this is required to allow compilation with non light angle vertex.
- // it should be compiled out.
- BatchVertexLightAngled *lv = (BatchVertexLightAngled *)cv;
- if (source_batch_uses_light_angles)
- lv->light_angle = *source_light_angles++;
- else
- lv->light_angle = 0.0f; // dummy, unused in vertex shader (could possibly be left uninitialized, but probably bad idea)
- }
- }
- } // textures match
- } else {
- // default
- // we can still join, but only under special circumstances
- // does this ever happen? not sure at this stage, but left for future expansion
- uint32_t source_last_command = source_batch.first_command + source_batch.num_commands;
- if (source_last_command == dest_batch->first_command) {
- dest_batch->num_commands += source_batch.num_commands;
- needs_new_batch = false;
- } // if the commands line up exactly
- }
- } // if both batches are the same type
-
- } // if dest batch is valid
-
- if (needs_new_batch) {
- dest_batch = bdata.batches_temp.request();
-#if defined(TOOLS_ENABLED) && defined(DEBUG_ENABLED)
- CRASH_COND(!dest_batch);
-#endif
-
- *dest_batch = source_batch;
-
- // create the colored verts (only if not default)
- if (source_batch.type != Batch::BT_DEFAULT) {
- int first_vert = source_batch.first_quad * 4;
- int end_vert = 4 * (source_batch.first_quad + source_batch.num_commands);
-
- for (int v = first_vert; v < end_vert; v++) {
- const BatchVertex &bv = bdata.vertices[v];
- BATCH_VERTEX_TYPE *cv = (BatchVertexLightAngled *)bdata.unit_vertices.request();
-#if defined(TOOLS_ENABLED) && defined(DEBUG_ENABLED)
- CRASH_COND(!cv);
-#endif
- cv->pos = bv.pos;
- cv->uv = bv.uv;
- cv->col = source_batch.color;
-
- if (INCLUDE_LIGHT_ANGLES) {
- // this is required to allow compilation with non light angle vertex.
- // it should be compiled out.
- BatchVertexLightAngled *lv = (BatchVertexLightAngled *)cv;
- if (source_batch_uses_light_angles)
- lv->light_angle = *source_light_angles++;
- else
- lv->light_angle = 0.0f; // dummy, unused in vertex shader (could possibly be left uninitialized, but probably bad idea)
- } // if using light angles
- }
- }
- }
- }
-
- // copy the temporary batches to the master batch list (this could be avoided but it makes the code cleaner)
- bdata.batches.copy_from(bdata.batches_temp);
-}
-
-// return true if buffer full up, else return false
-template
-bool RasterizerCanvasGLES2::prefill_rect(Item::CommandRect *rect, FillState &r_fill_state, int &r_command_start, int command_num, int command_count, Item::Command *const *commands, Item *p_item, bool multiply_final_modulate) {
- bool change_batch = false;
-
- // conditions for creating a new batch
- if (r_fill_state.curr_batch->type != Batch::BT_RECT) {
- change_batch = true;
-
- // check for special case if there is only a single or small number of rects,
- // in which case we will use the legacy default rect renderer
- // because it is faster for single rects
-
- // we only want to do this if not a joined item with more than 1 item,
- // because joined items with more than 1, the command * will be incorrect
- // NOTE - this is assuming that use_hardware_transform means that it is a non-joined item!!
- // If that assumption is incorrect this will go horribly wrong.
- if (bdata.settings_use_single_rect_fallback && r_fill_state.use_hardware_transform) {
- bool is_single_rect = false;
- int command_num_next = command_num + 1;
- if (command_num_next < command_count) {
- Item::Command *command_next = commands[command_num_next];
- if ((command_next->type != Item::Command::TYPE_RECT) && (command_next->type != Item::Command::TYPE_TRANSFORM)) {
- is_single_rect = true;
- }
- } else {
- is_single_rect = true;
- }
- // if it is a rect on its own, do exactly the same as the default routine
- if (is_single_rect) {
- _prefill_default_batch(r_fill_state, command_num, *p_item);
- return false;
- }
- } // if use hardware transform
- }
-
- Color col = rect->modulate;
- if (multiply_final_modulate) {
- col *= r_fill_state.final_modulate;
- }
-
- // instead of doing all the texture preparation for EVERY rect,
- // we build a list of texture combinations and do this once off.
- // This means we have a potentially rather slow step to identify which texture combo
- // using the RIDs.
- int old_batch_tex_id = r_fill_state.batch_tex_id;
- r_fill_state.batch_tex_id = _batch_find_or_create_tex(rect->texture, rect->normal_map, rect->flags & CANVAS_RECT_TILE, old_batch_tex_id);
-
- //r_fill_state.use_light_angles = send_light_angles;
- if (SEND_LIGHT_ANGLES)
- bdata.use_light_angles = true;
-
- // try to create vertices BEFORE creating a batch,
- // because if the vertex buffer is full, we need to finish this
- // function, draw what we have so far, and then start a new set of batches
-
- // request FOUR vertices at a time, this is more efficient
- BatchVertex *bvs = bdata.vertices.request(4);
- if (!bvs) {
- // run out of space in the vertex buffer .. finish this function and draw what we have so far
- // return where we got to
- r_command_start = command_num;
- return true;
- }
-
- // conditions for creating a new batch
- if (old_batch_tex_id != r_fill_state.batch_tex_id) {
- change_batch = true;
- }
-
- // we need to treat color change separately because we need to count these
- // to decide whether to switch on the fly to colored vertices.
- if (!r_fill_state.curr_batch->color.equals(col)) {
- change_batch = true;
- bdata.total_color_changes++;
- }
-
- if (change_batch) {
- // put the tex pixel size in a local (less verbose and can be a register)
- const BatchTex &batchtex = bdata.batch_textures[r_fill_state.batch_tex_id];
- batchtex.tex_pixel_size.to(r_fill_state.texpixel_size);
-
- if (bdata.settings_uv_contract) {
- r_fill_state.contract_uvs = (batchtex.flags & VS::TEXTURE_FLAG_FILTER) == 0;
- }
-
- // need to preserve texpixel_size between items
- r_fill_state.texpixel_size = r_fill_state.texpixel_size;
-
- // open new batch (this should never fail, it dynamically grows)
- r_fill_state.curr_batch = _batch_request_new(false);
-
- r_fill_state.curr_batch->type = Batch::BT_RECT;
- r_fill_state.curr_batch->color.set(col);
- r_fill_state.curr_batch->batch_texture_id = r_fill_state.batch_tex_id;
- r_fill_state.curr_batch->first_command = command_num;
- r_fill_state.curr_batch->num_commands = 1;
- r_fill_state.curr_batch->first_quad = bdata.total_quads;
- } else {
- // we could alternatively do the count when closing a batch .. perhaps more efficient
- r_fill_state.curr_batch->num_commands++;
- }
-
- // fill the quad geometry
- Vector2 mins = rect->rect.position;
-
- if (r_fill_state.transform_mode == TM_TRANSLATE) {
- _software_transform_vertex(mins, r_fill_state.transform_combined);
- }
-
- Vector2 maxs = mins + rect->rect.size;
-
- // just aliases
- BatchVertex *bA = &bvs[0];
- BatchVertex *bB = &bvs[1];
- BatchVertex *bC = &bvs[2];
- BatchVertex *bD = &bvs[3];
-
- bA->pos.x = mins.x;
- bA->pos.y = mins.y;
-
- bB->pos.x = maxs.x;
- bB->pos.y = mins.y;
-
- bC->pos.x = maxs.x;
- bC->pos.y = maxs.y;
-
- bD->pos.x = mins.x;
- bD->pos.y = maxs.y;
-
- // possibility of applying flips here for normal mapping .. but they don't seem to be used
- if (rect->rect.size.x < 0) {
- SWAP(bA->pos, bB->pos);
- SWAP(bC->pos, bD->pos);
- }
- if (rect->rect.size.y < 0) {
- SWAP(bA->pos, bD->pos);
- SWAP(bB->pos, bC->pos);
- }
-
- if (r_fill_state.transform_mode == TM_ALL) {
- _software_transform_vertex(bA->pos, r_fill_state.transform_combined);
- _software_transform_vertex(bB->pos, r_fill_state.transform_combined);
- _software_transform_vertex(bC->pos, r_fill_state.transform_combined);
- _software_transform_vertex(bD->pos, r_fill_state.transform_combined);
- }
-
- // uvs
- Vector2 src_min;
- Vector2 src_max;
- if (rect->flags & CANVAS_RECT_REGION) {
- src_min = rect->source.position;
- src_max = src_min + rect->source.size;
-
- src_min *= r_fill_state.texpixel_size;
- src_max *= r_fill_state.texpixel_size;
-
- const float uv_epsilon = bdata.settings_uv_contract_amount;
-
- // nudge offset for the maximum to prevent precision error on GPU reading into line outside the source rect
- // this is very difficult to get right.
- if (r_fill_state.contract_uvs) {
- src_min.x += uv_epsilon;
- src_min.y += uv_epsilon;
- src_max.x -= uv_epsilon;
- src_max.y -= uv_epsilon;
- }
- } else {
- src_min = Vector2(0, 0);
- src_max = Vector2(1, 1);
- }
-
- // 10% faster calculating the max first
- Vector2 uvs[4] = {
- src_min,
- Vector2(src_max.x, src_min.y),
- src_max,
- Vector2(src_min.x, src_max.y),
- };
-
- // for encoding in light angle
- // flips should be optimized out when not being used for light angle.
- bool flip_h = false;
- bool flip_v = false;
-
- if (rect->flags & CANVAS_RECT_TRANSPOSE) {
- SWAP(uvs[1], uvs[3]);
- }
-
- if (rect->flags & CANVAS_RECT_FLIP_H) {
- SWAP(uvs[0], uvs[1]);
- SWAP(uvs[2], uvs[3]);
- flip_h = !flip_h;
- flip_v = !flip_v;
- }
- if (rect->flags & CANVAS_RECT_FLIP_V) {
- SWAP(uvs[0], uvs[3]);
- SWAP(uvs[1], uvs[2]);
- flip_v = !flip_v;
- }
-
- bA->uv.set(uvs[0]);
- bB->uv.set(uvs[1]);
- bC->uv.set(uvs[2]);
- bD->uv.set(uvs[3]);
-
- if (SEND_LIGHT_ANGLES) {
- // we can either keep the light angles in sync with the verts when writing,
- // or sync them up during translation. We are syncing in translation.
- // N.B. There may be batches that don't require light_angles between batches that do.
- float *angles = bdata.light_angles.request(4);
-#if defined(TOOLS_ENABLED) && defined(DEBUG_ENABLED)
- CRASH_COND(angles == nullptr);
-#endif
-
- float angle = 0.0f;
- const float TWO_PI = Math_PI * 2;
-
- if (r_fill_state.transform_mode != TM_NONE) {
-
- const Transform2D &tr = r_fill_state.transform_combined;
-
- // apply to an x axis
- // the x axis and y axis can be taken directly from the transform (no need to xform identity vectors)
- Vector2 x_axis(tr.elements[0][0], tr.elements[1][0]);
-
- // have to do a y axis to check for scaling flips
- // this is hassle and extra slowness. We could only allow flips via the flags.
- Vector2 y_axis(tr.elements[0][1], tr.elements[1][1]);
-
- // has the x / y axis flipped due to scaling?
- float cross = x_axis.cross(y_axis);
- if (cross < 0.0f) {
- flip_v = !flip_v;
- }
-
- // passing an angle is smaller than a vector, it can be reconstructed in the shader
- angle = x_axis.angle();
-
- // we don't want negative angles, as negative is used to encode flips.
- // This moves range from -PI to PI to 0 to TWO_PI
- if (angle < 0.0f)
- angle += TWO_PI;
-
- } // if transform needed
-
- // if horizontal flip, angle is shifted by 180 degrees
- if (flip_h) {
- angle += Math_PI;
-
- // mod to get back to 0 to TWO_PI range
- angle = fmodf(angle, TWO_PI);
- }
-
- // add 1 (to take care of zero floating point error with sign)
- angle += 1.0f;
-
- // flip if necessary to indicate a vertical flip in the shader
- if (flip_v)
- angle *= -1.0f;
-
- // light angle must be sent for each vert, instead as a single uniform in the uniform draw method
- // this has the benefit of enabling batching with light angles.
- for (int n = 0; n < 4; n++) {
- angles[n] = angle;
- }
- }
-
- // increment quad count
- bdata.total_quads++;
-
- return false;
-}
-
#endif // RASTERIZERCANVASGLES2_H
diff --git a/drivers/gles2/rasterizer_gles2.cpp b/drivers/gles2/rasterizer_gles2.cpp
index ec49f460c4f..1c5ce101762 100644
--- a/drivers/gles2/rasterizer_gles2.cpp
+++ b/drivers/gles2/rasterizer_gles2.cpp
@@ -483,6 +483,42 @@ void RasterizerGLES2::make_current() {
void RasterizerGLES2::register_config() {
}
+// returns NULL if no error, or an error string
+const char *RasterizerGLES2::gl_check_for_error(bool p_print_error) {
+ GLenum err = glGetError();
+
+ const char *err_string = nullptr;
+
+ switch (err) {
+ default: {
+ // not recognised
+ } break;
+ case GL_NO_ERROR: {
+ } break;
+ case GL_INVALID_ENUM: {
+ err_string = "GL_INVALID_ENUM";
+ } break;
+ case GL_INVALID_VALUE: {
+ err_string = "GL_INVALID_VALUE";
+ } break;
+ case GL_INVALID_OPERATION: {
+ err_string = "GL_INVALID_OPERATION";
+ } break;
+ case GL_INVALID_FRAMEBUFFER_OPERATION: {
+ err_string = "GL_INVALID_FRAMEBUFFER_OPERATION";
+ } break;
+ case GL_OUT_OF_MEMORY: {
+ err_string = "GL_OUT_OF_MEMORY";
+ } break;
+ }
+
+ if (p_print_error && err_string) {
+ print_line(err_string);
+ }
+
+ return err_string;
+}
+
RasterizerGLES2::RasterizerGLES2() {
storage = memnew(RasterizerStorageGLES2);
diff --git a/drivers/gles2/rasterizer_gles2.h b/drivers/gles2/rasterizer_gles2.h
index 80bca08162a..9e6aaaf1640 100644
--- a/drivers/gles2/rasterizer_gles2.h
+++ b/drivers/gles2/rasterizer_gles2.h
@@ -71,6 +71,8 @@ public:
virtual bool is_low_end() const { return true; }
+ virtual const char *gl_check_for_error(bool p_print_error = true);
+
RasterizerGLES2();
~RasterizerGLES2();
};
diff --git a/drivers/gles2/rasterizer_storage_gles2.cpp b/drivers/gles2/rasterizer_storage_gles2.cpp
index c211f6377b0..80b5b5a1c31 100644
--- a/drivers/gles2/rasterizer_storage_gles2.cpp
+++ b/drivers/gles2/rasterizer_storage_gles2.cpp
@@ -1452,6 +1452,7 @@ void RasterizerStorageGLES2::_update_shader(Shader *p_shader) const {
shaders.actions_canvas.usage_flag_pointers["TIME"] = &p_shader->canvas_item.uses_time;
shaders.actions_canvas.usage_flag_pointers["MODULATE"] = &p_shader->canvas_item.uses_modulate;
shaders.actions_canvas.usage_flag_pointers["COLOR"] = &p_shader->canvas_item.uses_color;
+
shaders.actions_canvas.usage_flag_pointers["VERTEX"] = &p_shader->canvas_item.uses_vertex;
actions = &shaders.actions_canvas;
@@ -1552,10 +1553,10 @@ void RasterizerStorageGLES2::_update_shader(Shader *p_shader) const {
// some logic for batching
if (p_shader->mode == VS::SHADER_CANVAS_ITEM) {
if (p_shader->canvas_item.uses_modulate | p_shader->canvas_item.uses_color) {
- p_shader->canvas_item.batch_flags |= Shader::CanvasItem::PREVENT_COLOR_BAKING;
+ p_shader->canvas_item.batch_flags |= RasterizerStorageCommon::PREVENT_COLOR_BAKING;
}
if (p_shader->canvas_item.uses_vertex) {
- p_shader->canvas_item.batch_flags |= Shader::CanvasItem::PREVENT_VERTEX_BAKING;
+ p_shader->canvas_item.batch_flags |= RasterizerStorageCommon::PREVENT_VERTEX_BAKING;
}
}
@@ -3787,14 +3788,17 @@ void RasterizerStorageGLES2::_update_skeleton_transform_buffer(const PoolVector<
glBindBuffer(GL_ARRAY_BUFFER, resources.skeleton_transform_buffer);
+ uint32_t buffer_size = p_size * sizeof(float);
+
if (p_size > resources.skeleton_transform_buffer_size) {
// new requested buffer is bigger, so resizing the GPU buffer
resources.skeleton_transform_buffer_size = p_size;
- glBufferData(GL_ARRAY_BUFFER, p_size * sizeof(float), p_data.read().ptr(), GL_DYNAMIC_DRAW);
+ glBufferData(GL_ARRAY_BUFFER, buffer_size, p_data.read().ptr(), GL_DYNAMIC_DRAW);
} else {
- glBufferSubData(GL_ARRAY_BUFFER, 0, p_size * sizeof(float), p_data.read().ptr());
+ // this may not be best, it could be better to use glBufferData in both cases.
+ buffer_orphan_and_upload(resources.skeleton_transform_buffer_size, 0, buffer_size, p_data.read().ptr());
}
glBindBuffer(GL_ARRAY_BUFFER, 0);
diff --git a/drivers/gles2/rasterizer_storage_gles2.h b/drivers/gles2/rasterizer_storage_gles2.h
index c814cd38675..cdb698de8b0 100644
--- a/drivers/gles2/rasterizer_storage_gles2.h
+++ b/drivers/gles2/rasterizer_storage_gles2.h
@@ -40,11 +40,6 @@
#include "shaders/copy.glsl.gen.h"
#include "shaders/cubemap_filter.glsl.gen.h"
-/*
-#include "shaders/blend_shape.glsl.gen.h"
-#include "shaders/canvas.glsl.gen.h"
-#include "shaders/particles.glsl.gen.h"
-*/
class RasterizerCanvasGLES2;
class RasterizerSceneGLES2;
@@ -450,10 +445,7 @@ public:
// these flags are specifically for batching
// some of the logic is thus in rasterizer_storage.cpp
// we could alternatively set bitflags for each 'uses' and test on the fly
- enum BatchFlags {
- PREVENT_COLOR_BAKING = 1 << 0,
- PREVENT_VERTEX_BAKING = 1 << 1,
- };
+ // defined in RasterizerStorageCommon::BatchFlags
unsigned int batch_flags;
bool uses_screen_texture;
diff --git a/drivers/gles2/shaders/canvas.glsl b/drivers/gles2/shaders/canvas.glsl
index 38b9998d63d..5613ae1e01e 100644
--- a/drivers/gles2/shaders/canvas.glsl
+++ b/drivers/gles2/shaders/canvas.glsl
@@ -19,7 +19,7 @@ uniform highp mat4 modelview_matrix;
uniform highp mat4 extra_matrix;
attribute highp vec2 vertex; // attrib:0
-#ifdef USE_LIGHT_ANGLE
+#ifdef USE_ATTRIB_LIGHT_ANGLE
// shared with tangent, not used in canvas shader
attribute highp float light_angle; // attrib:2
#endif
@@ -27,6 +27,16 @@ attribute highp float light_angle; // attrib:2
attribute vec4 color_attrib; // attrib:3
attribute vec2 uv_attrib; // attrib:4
+#ifdef USE_ATTRIB_MODULATE
+attribute highp vec4 modulate_attrib; // attrib:5
+#endif
+
+#ifdef USE_ATTRIB_LARGE_VERTEX
+// shared with skeleton attributes, not used in batched shader
+attribute highp vec2 translate_attrib; // attrib:6
+attribute highp vec4 basis_attrib; // attrib:7
+#endif
+
#ifdef USE_SKELETON
attribute highp vec4 bone_indices; // attrib:6
attribute highp vec4 bone_weights; // attrib:7
@@ -54,6 +64,12 @@ uniform highp mat4 skeleton_transform_inverse;
varying vec2 uv_interp;
varying vec4 color_interp;
+
+#ifdef USE_ATTRIB_MODULATE
+// modulate doesn't need interpolating but we need to send it to the fragment shader
+varying vec4 modulate_interp;
+#endif
+
#ifdef MODULATE_USED
uniform vec4 final_modulate;
#endif
@@ -171,6 +187,24 @@ VERTEX_SHADER_CODE
gl_PointSize = point_size;
+#ifdef USE_ATTRIB_MODULATE
+ // modulate doesn't need interpolating but we need to send it to the fragment shader
+ modulate_interp = modulate_attrib;
+#endif
+
+#ifdef USE_ATTRIB_LARGE_VERTEX
+ // transform is in attributes
+ vec2 temp;
+
+ temp = outvec.xy;
+ temp.x = (outvec.x * basis_attrib.x) + (outvec.y * basis_attrib.z);
+ temp.y = (outvec.x * basis_attrib.y) + (outvec.y * basis_attrib.w);
+
+ temp += translate_attrib;
+ outvec.xy = temp;
+
+#endif
+
#if !defined(SKIP_TRANSFORM_USED)
outvec = extra_matrix_instance * outvec;
outvec = modelview_matrix * outvec;
@@ -225,7 +259,7 @@ VERTEX_SHADER_CODE
pos = outvec.xy;
#endif
-#ifdef USE_LIGHT_ANGLE
+#ifdef USE_ATTRIB_LIGHT_ANGLE
// we add a fixed offset because we are using the sign later,
// and don't want floating point error around 0.0
float la = abs(light_angle) - 1.0;
@@ -303,6 +337,10 @@ uniform mediump sampler2D normal_texture; // texunit:-2
varying mediump vec2 uv_interp;
varying mediump vec4 color_interp;
+#ifdef USE_ATTRIB_MODULATE
+varying mediump vec4 modulate_interp;
+#endif
+
uniform highp float time;
uniform vec4 final_modulate;
@@ -442,6 +480,11 @@ FRAGMENT_SHADER_CODE
color *= final_modulate;
#endif
+#ifdef USE_ATTRIB_MODULATE
+ // todo .. this won't be used at the same time as MODULATE_USED
+ color *= modulate_interp;
+#endif
+
#ifdef USE_LIGHTING
vec2 light_vec = transformed_light_uv;
diff --git a/drivers/gles3/rasterizer_canvas_base_gles3.cpp b/drivers/gles3/rasterizer_canvas_base_gles3.cpp
new file mode 100644
index 00000000000..eebe9e75405
--- /dev/null
+++ b/drivers/gles3/rasterizer_canvas_base_gles3.cpp
@@ -0,0 +1,1321 @@
+/*************************************************************************/
+/* rasterizer_canvas_base_gles3.cpp */
+/*************************************************************************/
+/* This file is part of: */
+/* GODOT ENGINE */
+/* https://godotengine.org */
+/*************************************************************************/
+/* Copyright (c) 2007-2020 Juan Linietsky, Ariel Manzur. */
+/* Copyright (c) 2014-2020 Godot Engine contributors (cf. AUTHORS.md). */
+/* */
+/* Permission is hereby granted, free of charge, to any person obtaining */
+/* a copy of this software and associated documentation files (the */
+/* "Software"), to deal in the Software without restriction, including */
+/* without limitation the rights to use, copy, modify, merge, publish, */
+/* distribute, sublicense, and/or sell copies of the Software, and to */
+/* permit persons to whom the Software is furnished to do so, subject to */
+/* the following conditions: */
+/* */
+/* The above copyright notice and this permission notice shall be */
+/* included in all copies or substantial portions of the Software. */
+/* */
+/* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
+/* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
+/* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.*/
+/* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
+/* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
+/* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
+/* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
+/*************************************************************************/
+
+#include "rasterizer_canvas_base_gles3.h"
+
+#include "core/os/os.h"
+#include "core/project_settings.h"
+#include "rasterizer_scene_gles3.h"
+#include "servers/visual/visual_server_raster.h"
+
+#ifndef GLES_OVER_GL
+#define glClearDepth glClearDepthf
+#endif
+
+static _FORCE_INLINE_ void store_transform2d(const Transform2D &p_mtx, float *p_array) {
+
+ p_array[0] = p_mtx.elements[0][0];
+ p_array[1] = p_mtx.elements[0][1];
+ p_array[2] = 0;
+ p_array[3] = 0;
+ p_array[4] = p_mtx.elements[1][0];
+ p_array[5] = p_mtx.elements[1][1];
+ p_array[6] = 0;
+ p_array[7] = 0;
+ p_array[8] = 0;
+ p_array[9] = 0;
+ p_array[10] = 1;
+ p_array[11] = 0;
+ p_array[12] = p_mtx.elements[2][0];
+ p_array[13] = p_mtx.elements[2][1];
+ p_array[14] = 0;
+ p_array[15] = 1;
+}
+
+static _FORCE_INLINE_ void store_transform(const Transform &p_mtx, float *p_array) {
+ p_array[0] = p_mtx.basis.elements[0][0];
+ p_array[1] = p_mtx.basis.elements[1][0];
+ p_array[2] = p_mtx.basis.elements[2][0];
+ p_array[3] = 0;
+ p_array[4] = p_mtx.basis.elements[0][1];
+ p_array[5] = p_mtx.basis.elements[1][1];
+ p_array[6] = p_mtx.basis.elements[2][1];
+ p_array[7] = 0;
+ p_array[8] = p_mtx.basis.elements[0][2];
+ p_array[9] = p_mtx.basis.elements[1][2];
+ p_array[10] = p_mtx.basis.elements[2][2];
+ p_array[11] = 0;
+ p_array[12] = p_mtx.origin.x;
+ p_array[13] = p_mtx.origin.y;
+ p_array[14] = p_mtx.origin.z;
+ p_array[15] = 1;
+}
+
+static _FORCE_INLINE_ void store_camera(const CameraMatrix &p_mtx, float *p_array) {
+
+ for (int i = 0; i < 4; i++) {
+ for (int j = 0; j < 4; j++) {
+
+ p_array[i * 4 + j] = p_mtx.matrix[i][j];
+ }
+ }
+}
+
+RID RasterizerCanvasBaseGLES3::light_internal_create() {
+
+ LightInternal *li = memnew(LightInternal);
+
+ glGenBuffers(1, &li->ubo);
+ glBindBuffer(GL_UNIFORM_BUFFER, li->ubo);
+ glBufferData(GL_UNIFORM_BUFFER, sizeof(LightInternal::UBOData), &state.canvas_item_ubo_data, GL_DYNAMIC_DRAW);
+ glBindBuffer(GL_UNIFORM_BUFFER, 0);
+
+ return light_internal_owner.make_rid(li);
+}
+
+void RasterizerCanvasBaseGLES3::light_internal_update(RID p_rid, Light *p_light) {
+
+ LightInternal *li = light_internal_owner.getornull(p_rid);
+ ERR_FAIL_COND(!li);
+
+ store_transform2d(p_light->light_shader_xform, li->ubo_data.light_matrix);
+ store_transform2d(p_light->xform_cache.affine_inverse(), li->ubo_data.local_matrix);
+ store_camera(p_light->shadow_matrix_cache, li->ubo_data.shadow_matrix);
+
+ for (int i = 0; i < 4; i++) {
+
+ li->ubo_data.color[i] = p_light->color[i] * p_light->energy;
+ li->ubo_data.shadow_color[i] = p_light->shadow_color[i];
+ }
+
+ li->ubo_data.light_pos[0] = p_light->light_shader_pos.x;
+ li->ubo_data.light_pos[1] = p_light->light_shader_pos.y;
+ li->ubo_data.shadowpixel_size = (1.0 / p_light->shadow_buffer_size) * (1.0 + p_light->shadow_smooth);
+ li->ubo_data.light_outside_alpha = p_light->mode == VS::CANVAS_LIGHT_MODE_MASK ? 1.0 : 0.0;
+ li->ubo_data.light_height = p_light->height;
+ if (p_light->radius_cache == 0)
+ li->ubo_data.shadow_gradient = 0;
+ else
+ li->ubo_data.shadow_gradient = p_light->shadow_gradient_length / (p_light->radius_cache * 1.1);
+
+ li->ubo_data.shadow_distance_mult = (p_light->radius_cache * 1.1);
+
+ glBindBuffer(GL_UNIFORM_BUFFER, li->ubo);
+ glBufferData(GL_UNIFORM_BUFFER, sizeof(LightInternal::UBOData), &li->ubo_data, GL_DYNAMIC_DRAW);
+ glBindBuffer(GL_UNIFORM_BUFFER, 0);
+}
+
+void RasterizerCanvasBaseGLES3::light_internal_free(RID p_rid) {
+
+ LightInternal *li = light_internal_owner.getornull(p_rid);
+ ERR_FAIL_COND(!li);
+
+ glDeleteBuffers(1, &li->ubo);
+ light_internal_owner.free(p_rid);
+ memdelete(li);
+}
+
+void RasterizerCanvasBaseGLES3::canvas_begin() {
+
+ if (storage->frame.current_rt && storage->frame.clear_request) {
+ // a clear request may be pending, so do it
+ bool transparent = storage->frame.current_rt->flags[RasterizerStorage::RENDER_TARGET_TRANSPARENT];
+
+ glClearColor(storage->frame.clear_request_color.r,
+ storage->frame.clear_request_color.g,
+ storage->frame.clear_request_color.b,
+ transparent ? storage->frame.clear_request_color.a : 1.0);
+ glClear(GL_COLOR_BUFFER_BIT);
+ storage->frame.clear_request = false;
+ glColorMask(1, 1, 1, transparent ? 1 : 0);
+ }
+
+ reset_canvas();
+
+ state.canvas_shader.set_conditional(CanvasShaderGLES3::USE_TEXTURE_RECT, true);
+ state.canvas_shader.set_conditional(CanvasShaderGLES3::USE_LIGHTING, false);
+ state.canvas_shader.set_conditional(CanvasShaderGLES3::USE_SHADOWS, false);
+ state.canvas_shader.set_conditional(CanvasShaderGLES3::SHADOW_FILTER_NEAREST, false);
+ state.canvas_shader.set_conditional(CanvasShaderGLES3::SHADOW_FILTER_PCF3, false);
+ state.canvas_shader.set_conditional(CanvasShaderGLES3::SHADOW_FILTER_PCF5, false);
+ state.canvas_shader.set_conditional(CanvasShaderGLES3::SHADOW_FILTER_PCF7, false);
+ state.canvas_shader.set_conditional(CanvasShaderGLES3::SHADOW_FILTER_PCF9, false);
+ state.canvas_shader.set_conditional(CanvasShaderGLES3::SHADOW_FILTER_PCF13, false);
+ state.canvas_shader.set_conditional(CanvasShaderGLES3::USE_DISTANCE_FIELD, false);
+ state.canvas_shader.set_conditional(CanvasShaderGLES3::USE_NINEPATCH, false);
+
+ state.canvas_shader.set_conditional(CanvasShaderGLES3::USE_ATTRIB_LIGHT_ANGLE, false);
+ state.canvas_shader.set_conditional(CanvasShaderGLES3::USE_ATTRIB_MODULATE, false);
+ state.canvas_shader.set_conditional(CanvasShaderGLES3::USE_ATTRIB_LARGE_VERTEX, false);
+
+ state.canvas_shader.set_conditional(CanvasShaderGLES3::USE_SKELETON, false);
+
+ state.canvas_shader.set_custom_shader(0);
+ state.canvas_shader.bind();
+ state.canvas_shader.set_uniform(CanvasShaderGLES3::FINAL_MODULATE, Color(1, 1, 1, 1));
+ state.canvas_shader.set_uniform(CanvasShaderGLES3::MODELVIEW_MATRIX, Transform2D());
+ state.canvas_shader.set_uniform(CanvasShaderGLES3::EXTRA_MATRIX, Transform2D());
+ if (storage->frame.current_rt) {
+ state.canvas_shader.set_uniform(CanvasShaderGLES3::SCREEN_PIXEL_SIZE, Vector2(1.0 / storage->frame.current_rt->width, 1.0 / storage->frame.current_rt->height));
+ } else {
+ state.canvas_shader.set_uniform(CanvasShaderGLES3::SCREEN_PIXEL_SIZE, Vector2(1.0, 1.0));
+ }
+
+ //state.canvas_shader.set_uniform(CanvasShaderGLES3::PROJECTION_MATRIX,state.vp);
+ //state.canvas_shader.set_uniform(CanvasShaderGLES3::MODELVIEW_MATRIX,Transform());
+ //state.canvas_shader.set_uniform(CanvasShaderGLES3::EXTRA_MATRIX,Transform());
+
+ glBindBufferBase(GL_UNIFORM_BUFFER, 0, state.canvas_item_ubo);
+ glBindVertexArray(data.canvas_quad_array);
+ state.using_texture_rect = true;
+ state.using_ninepatch = false;
+
+ state.using_light_angle = false;
+ state.using_modulate = false;
+ state.using_large_vertex = false;
+
+ state.using_skeleton = false;
+}
+
+void RasterizerCanvasBaseGLES3::canvas_end() {
+
+ glBindVertexArray(0);
+ glBindBufferBase(GL_UNIFORM_BUFFER, 0, 0);
+ glColorMask(1, 1, 1, 1);
+
+ glVertexAttrib4f(VS::ARRAY_COLOR, 1, 1, 1, 1);
+
+ state.using_texture_rect = false;
+ state.using_ninepatch = false;
+ state.using_light_angle = false;
+}
+
+RasterizerStorageGLES3::Texture *RasterizerCanvasBaseGLES3::_bind_canvas_texture(const RID &p_texture, const RID &p_normal_map, bool p_force) {
+
+ RasterizerStorageGLES3::Texture *tex_return = NULL;
+
+ if (p_texture == state.current_tex && !p_force) {
+ tex_return = state.current_tex_ptr;
+ } else if (p_texture.is_valid()) {
+
+ RasterizerStorageGLES3::Texture *texture = storage->texture_owner.getornull(p_texture);
+
+ if (!texture) {
+ state.current_tex = RID();
+ state.current_tex_ptr = NULL;
+ glActiveTexture(GL_TEXTURE0);
+ glBindTexture(GL_TEXTURE_2D, storage->resources.white_tex);
+
+ } else {
+
+ if (texture->redraw_if_visible) { //check before proxy, because this is usually used with proxies
+ VisualServerRaster::redraw_request();
+ }
+
+ texture = texture->get_ptr();
+
+ if (texture->render_target)
+ texture->render_target->used_in_frame = true;
+
+ glActiveTexture(GL_TEXTURE0);
+ glBindTexture(GL_TEXTURE_2D, texture->tex_id);
+ state.current_tex = p_texture;
+ state.current_tex_ptr = texture;
+
+ tex_return = texture;
+ }
+
+ } else {
+
+ glActiveTexture(GL_TEXTURE0);
+ glBindTexture(GL_TEXTURE_2D, storage->resources.white_tex);
+ state.current_tex = RID();
+ state.current_tex_ptr = NULL;
+ }
+
+ if (p_normal_map == state.current_normal && !p_force) {
+ //do none
+ state.canvas_shader.set_uniform(CanvasShaderGLES3::USE_DEFAULT_NORMAL, state.current_normal.is_valid());
+
+ } else if (p_normal_map.is_valid()) {
+
+ RasterizerStorageGLES3::Texture *normal_map = storage->texture_owner.getornull(p_normal_map);
+
+ if (!normal_map) {
+ state.current_normal = RID();
+ glActiveTexture(GL_TEXTURE1);
+ glBindTexture(GL_TEXTURE_2D, storage->resources.normal_tex);
+ state.canvas_shader.set_uniform(CanvasShaderGLES3::USE_DEFAULT_NORMAL, false);
+
+ } else {
+
+ if (normal_map->redraw_if_visible) { //check before proxy, because this is usually used with proxies
+ VisualServerRaster::redraw_request();
+ }
+
+ normal_map = normal_map->get_ptr();
+
+ glActiveTexture(GL_TEXTURE1);
+ glBindTexture(GL_TEXTURE_2D, normal_map->tex_id);
+ state.current_normal = p_normal_map;
+ state.canvas_shader.set_uniform(CanvasShaderGLES3::USE_DEFAULT_NORMAL, true);
+ }
+
+ } else {
+
+ state.current_normal = RID();
+ glActiveTexture(GL_TEXTURE1);
+ glBindTexture(GL_TEXTURE_2D, storage->resources.normal_tex);
+ state.canvas_shader.set_uniform(CanvasShaderGLES3::USE_DEFAULT_NORMAL, false);
+ }
+
+ return tex_return;
+}
+
+void RasterizerCanvasBaseGLES3::_set_texture_rect_mode(bool p_enable, bool p_ninepatch, bool p_light_angle, bool p_modulate, bool p_large_vertex) {
+
+ // this state check could be done individually
+ if (state.using_texture_rect == p_enable && state.using_ninepatch == p_ninepatch && state.using_light_angle == p_light_angle && state.using_modulate == p_modulate && state.using_large_vertex == p_large_vertex)
+ return;
+
+ if (p_enable) {
+ glBindVertexArray(data.canvas_quad_array);
+
+ } else {
+ glBindVertexArray(0);
+ glBindBuffer(GL_ARRAY_BUFFER, 0);
+ glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0);
+ }
+
+ state.canvas_shader.set_conditional(CanvasShaderGLES3::USE_NINEPATCH, p_ninepatch && p_enable);
+ state.canvas_shader.set_conditional(CanvasShaderGLES3::USE_TEXTURE_RECT, p_enable);
+
+ state.canvas_shader.set_conditional(CanvasShaderGLES3::USE_ATTRIB_LIGHT_ANGLE, p_light_angle);
+ state.canvas_shader.set_conditional(CanvasShaderGLES3::USE_ATTRIB_MODULATE, p_modulate);
+ state.canvas_shader.set_conditional(CanvasShaderGLES3::USE_ATTRIB_LARGE_VERTEX, p_large_vertex);
+
+ state.canvas_shader.bind();
+ state.canvas_shader.set_uniform(CanvasShaderGLES3::FINAL_MODULATE, state.canvas_item_modulate);
+ state.canvas_shader.set_uniform(CanvasShaderGLES3::MODELVIEW_MATRIX, state.final_transform);
+ state.canvas_shader.set_uniform(CanvasShaderGLES3::EXTRA_MATRIX, state.extra_matrix);
+ if (state.using_skeleton) {
+ state.canvas_shader.set_uniform(CanvasShaderGLES3::SKELETON_TRANSFORM, state.skeleton_transform);
+ state.canvas_shader.set_uniform(CanvasShaderGLES3::SKELETON_TRANSFORM_INVERSE, state.skeleton_transform_inverse);
+ }
+ if (storage->frame.current_rt) {
+ state.canvas_shader.set_uniform(CanvasShaderGLES3::SCREEN_PIXEL_SIZE, Vector2(1.0 / storage->frame.current_rt->width, 1.0 / storage->frame.current_rt->height));
+ } else {
+ state.canvas_shader.set_uniform(CanvasShaderGLES3::SCREEN_PIXEL_SIZE, Vector2(1.0, 1.0));
+ }
+
+ state.using_texture_rect = p_enable;
+ state.using_ninepatch = p_ninepatch;
+
+ state.using_light_angle = p_light_angle;
+ state.using_modulate = p_modulate;
+ state.using_large_vertex = p_large_vertex;
+}
+
+void RasterizerCanvasBaseGLES3::_draw_polygon(const int *p_indices, int p_index_count, int p_vertex_count, const Vector2 *p_vertices, const Vector2 *p_uvs, const Color *p_colors, bool p_singlecolor, const int *p_bones, const float *p_weights) {
+
+ glBindVertexArray(data.polygon_buffer_pointer_array);
+ glBindBuffer(GL_ARRAY_BUFFER, data.polygon_buffer);
+
+ // precalculate the buffer size ahead of time instead of making multiple glBufferSubData calls
+ uint32_t total_buffer_size = sizeof(Vector2) * p_vertex_count;
+ if (!p_singlecolor && p_colors) {
+ total_buffer_size += sizeof(Color) * p_vertex_count;
+ }
+ if (p_uvs) {
+ total_buffer_size += sizeof(Vector2) * p_vertex_count;
+ }
+ if (p_weights && p_bones) {
+ total_buffer_size += sizeof(int) * 4 * p_vertex_count; // bones
+ total_buffer_size += sizeof(float) * 4 * p_vertex_count; // weights
+ }
+
+ uint32_t buffer_ofs = 0;
+ storage->buffer_orphan_and_upload(data.polygon_buffer_size, buffer_ofs, total_buffer_size, p_vertices);
+
+ glEnableVertexAttribArray(VS::ARRAY_VERTEX);
+ glVertexAttribPointer(VS::ARRAY_VERTEX, 2, GL_FLOAT, false, sizeof(Vector2), CAST_INT_TO_UCHAR_PTR(buffer_ofs));
+ buffer_ofs += sizeof(Vector2) * p_vertex_count;
+ //color
+#ifdef DEBUG_ENABLED
+ ERR_FAIL_COND(buffer_ofs > data.polygon_buffer_size);
+#endif
+
+ if (p_singlecolor) {
+ glDisableVertexAttribArray(VS::ARRAY_COLOR);
+ Color m = *p_colors;
+ glVertexAttrib4f(VS::ARRAY_COLOR, m.r, m.g, m.b, m.a);
+ } else if (!p_colors) {
+ glDisableVertexAttribArray(VS::ARRAY_COLOR);
+ glVertexAttrib4f(VS::ARRAY_COLOR, 1, 1, 1, 1);
+ } else {
+
+ glEnableVertexAttribArray(VS::ARRAY_COLOR);
+ glVertexAttribPointer(VS::ARRAY_COLOR, 4, GL_FLOAT, false, sizeof(Color), CAST_INT_TO_UCHAR_PTR(buffer_ofs));
+ buffer_ofs += sizeof(Color) * p_vertex_count;
+ }
+
+#ifdef DEBUG_ENABLED
+ ERR_FAIL_COND(buffer_ofs > data.polygon_buffer_size);
+#endif
+
+ if (p_uvs) {
+
+ glEnableVertexAttribArray(VS::ARRAY_TEX_UV);
+ glVertexAttribPointer(VS::ARRAY_TEX_UV, 2, GL_FLOAT, false, sizeof(Vector2), CAST_INT_TO_UCHAR_PTR(buffer_ofs));
+ buffer_ofs += sizeof(Vector2) * p_vertex_count;
+
+ } else {
+ glDisableVertexAttribArray(VS::ARRAY_TEX_UV);
+ }
+
+#ifdef DEBUG_ENABLED
+ ERR_FAIL_COND(buffer_ofs > data.polygon_buffer_size);
+#endif
+
+ if (p_bones && p_weights) {
+
+ glEnableVertexAttribArray(VS::ARRAY_BONES);
+ //glVertexAttribPointer(VS::ARRAY_BONES, 4, GL_UNSIGNED_INT, false, sizeof(int) * 4, ((uint8_t *)0) + buffer_ofs);
+ glVertexAttribIPointer(VS::ARRAY_BONES, 4, GL_UNSIGNED_INT, sizeof(int) * 4, CAST_INT_TO_UCHAR_PTR(buffer_ofs));
+ buffer_ofs += sizeof(int) * 4 * p_vertex_count;
+
+ glEnableVertexAttribArray(VS::ARRAY_WEIGHTS);
+ glVertexAttribPointer(VS::ARRAY_WEIGHTS, 4, GL_FLOAT, false, sizeof(float) * 4, CAST_INT_TO_UCHAR_PTR(buffer_ofs));
+ buffer_ofs += sizeof(float) * 4 * p_vertex_count;
+
+ } else if (state.using_skeleton) {
+ glVertexAttribI4ui(VS::ARRAY_BONES, 0, 0, 0, 0);
+ glVertexAttrib4f(VS::ARRAY_WEIGHTS, 0, 0, 0, 0);
+ }
+
+#ifdef DEBUG_ENABLED
+ ERR_FAIL_COND(buffer_ofs > data.polygon_buffer_size);
+#endif
+
+ //bind the indices buffer.
+ glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, data.polygon_index_buffer);
+ storage->buffer_orphan_and_upload(data.polygon_index_buffer_size, 0, sizeof(int) * p_index_count, p_indices, GL_ELEMENT_ARRAY_BUFFER);
+
+ //draw the triangles.
+ glDrawElements(GL_TRIANGLES, p_index_count, GL_UNSIGNED_INT, 0);
+
+ storage->info.render._2d_draw_call_count++;
+
+ if (p_bones && p_weights) {
+ //not used so often, so disable when used
+ glDisableVertexAttribArray(VS::ARRAY_BONES);
+ glDisableVertexAttribArray(VS::ARRAY_WEIGHTS);
+ }
+
+ glBindVertexArray(0);
+ glBindBuffer(GL_ARRAY_BUFFER, 0);
+}
+
+void RasterizerCanvasBaseGLES3::_draw_generic(GLuint p_primitive, int p_vertex_count, const Vector2 *p_vertices, const Vector2 *p_uvs, const Color *p_colors, bool p_singlecolor) {
+
+ glBindVertexArray(data.polygon_buffer_pointer_array);
+ glBindBuffer(GL_ARRAY_BUFFER, data.polygon_buffer);
+
+ // precalculate the buffer size ahead of time instead of making multiple glBufferSubData calls
+ uint32_t total_buffer_size = sizeof(Vector2) * p_vertex_count;
+ if (!p_singlecolor && p_colors) {
+ total_buffer_size += sizeof(Color) * p_vertex_count;
+ }
+ if (p_uvs) {
+ total_buffer_size += sizeof(Vector2) * p_vertex_count;
+ }
+
+ //vertex
+ uint32_t buffer_ofs = 0;
+ storage->buffer_orphan_and_upload(data.polygon_buffer_size, buffer_ofs, total_buffer_size, p_vertices);
+
+ glEnableVertexAttribArray(VS::ARRAY_VERTEX);
+ glVertexAttribPointer(VS::ARRAY_VERTEX, 2, GL_FLOAT, false, sizeof(Vector2), CAST_INT_TO_UCHAR_PTR(buffer_ofs));
+ buffer_ofs += sizeof(Vector2) * p_vertex_count;
+
+ //color
+ if (p_singlecolor) {
+ glDisableVertexAttribArray(VS::ARRAY_COLOR);
+ Color m = *p_colors;
+ glVertexAttrib4f(VS::ARRAY_COLOR, m.r, m.g, m.b, m.a);
+ } else if (!p_colors) {
+ glDisableVertexAttribArray(VS::ARRAY_COLOR);
+ glVertexAttrib4f(VS::ARRAY_COLOR, 1, 1, 1, 1);
+ } else {
+
+ glEnableVertexAttribArray(VS::ARRAY_COLOR);
+ glVertexAttribPointer(VS::ARRAY_COLOR, 4, GL_FLOAT, false, sizeof(Color), CAST_INT_TO_UCHAR_PTR(buffer_ofs));
+ buffer_ofs += sizeof(Color) * p_vertex_count;
+ }
+
+ if (p_uvs) {
+
+ glEnableVertexAttribArray(VS::ARRAY_TEX_UV);
+ glVertexAttribPointer(VS::ARRAY_TEX_UV, 2, GL_FLOAT, false, sizeof(Vector2), CAST_INT_TO_UCHAR_PTR(buffer_ofs));
+ buffer_ofs += sizeof(Vector2) * p_vertex_count;
+
+ } else {
+ glDisableVertexAttribArray(VS::ARRAY_TEX_UV);
+ }
+
+ glDrawArrays(p_primitive, 0, p_vertex_count);
+
+ storage->info.render._2d_draw_call_count++;
+
+ glBindVertexArray(0);
+ glBindBuffer(GL_ARRAY_BUFFER, 0);
+}
+
+void RasterizerCanvasBaseGLES3::_draw_generic_indices(GLuint p_primitive, const int *p_indices, int p_index_count, int p_vertex_count, const Vector2 *p_vertices, const Vector2 *p_uvs, const Color *p_colors, bool p_singlecolor) {
+
+ glBindVertexArray(data.polygon_buffer_pointer_array);
+ glBindBuffer(GL_ARRAY_BUFFER, data.polygon_buffer);
+
+ //vertex
+ uint32_t buffer_ofs = 0;
+ storage->buffer_orphan_and_upload(data.polygon_buffer_size, buffer_ofs, sizeof(Vector2) * p_vertex_count, p_vertices);
+
+ glEnableVertexAttribArray(VS::ARRAY_VERTEX);
+ glVertexAttribPointer(VS::ARRAY_VERTEX, 2, GL_FLOAT, false, sizeof(Vector2), CAST_INT_TO_UCHAR_PTR(buffer_ofs));
+ buffer_ofs += sizeof(Vector2) * p_vertex_count;
+ //color
+#ifdef DEBUG_ENABLED
+ ERR_FAIL_COND(buffer_ofs > data.polygon_buffer_size);
+#endif
+
+ if (p_singlecolor) {
+ glDisableVertexAttribArray(VS::ARRAY_COLOR);
+ Color m = *p_colors;
+ glVertexAttrib4f(VS::ARRAY_COLOR, m.r, m.g, m.b, m.a);
+ } else if (!p_colors) {
+ glDisableVertexAttribArray(VS::ARRAY_COLOR);
+ glVertexAttrib4f(VS::ARRAY_COLOR, 1, 1, 1, 1);
+ } else {
+
+ glBufferSubData(GL_ARRAY_BUFFER, buffer_ofs, sizeof(Color) * p_vertex_count, p_colors);
+ glEnableVertexAttribArray(VS::ARRAY_COLOR);
+ glVertexAttribPointer(VS::ARRAY_COLOR, 4, GL_FLOAT, false, sizeof(Color), CAST_INT_TO_UCHAR_PTR(buffer_ofs));
+ buffer_ofs += sizeof(Color) * p_vertex_count;
+ }
+
+#ifdef DEBUG_ENABLED
+ ERR_FAIL_COND(buffer_ofs > data.polygon_buffer_size);
+#endif
+
+ if (p_uvs) {
+
+ glBufferSubData(GL_ARRAY_BUFFER, buffer_ofs, sizeof(Vector2) * p_vertex_count, p_uvs);
+ glEnableVertexAttribArray(VS::ARRAY_TEX_UV);
+ glVertexAttribPointer(VS::ARRAY_TEX_UV, 2, GL_FLOAT, false, sizeof(Vector2), CAST_INT_TO_UCHAR_PTR(buffer_ofs));
+ buffer_ofs += sizeof(Vector2) * p_vertex_count;
+
+ } else {
+ glDisableVertexAttribArray(VS::ARRAY_TEX_UV);
+ }
+
+#ifdef DEBUG_ENABLED
+ ERR_FAIL_COND(buffer_ofs > data.polygon_buffer_size);
+#endif
+
+ //bind the indices buffer.
+ glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, data.polygon_index_buffer);
+ storage->buffer_orphan_and_upload(data.polygon_index_buffer_size, 0, sizeof(int) * p_index_count, p_indices, GL_ELEMENT_ARRAY_BUFFER);
+
+ //draw the triangles.
+ glDrawElements(p_primitive, p_index_count, GL_UNSIGNED_INT, 0);
+
+ storage->info.render._2d_draw_call_count++;
+
+ glBindVertexArray(0);
+ glBindBuffer(GL_ARRAY_BUFFER, 0);
+}
+
+void RasterizerCanvasBaseGLES3::_draw_gui_primitive(int p_points, const Vector2 *p_vertices, const Color *p_colors, const Vector2 *p_uvs, const float *p_light_angles) {
+
+ static const GLenum prim[5] = { GL_POINTS, GL_POINTS, GL_LINES, GL_TRIANGLES, GL_TRIANGLE_FAN };
+
+ //#define GLES_USE_PRIMITIVE_BUFFER
+
+ int version = 0;
+ int color_ofs = 0;
+ int uv_ofs = 0;
+ int light_angle_ofs = 0;
+ int stride = 2;
+
+ if (p_colors) { //color
+ version |= 1;
+ color_ofs = stride;
+ stride += 4;
+ }
+
+ if (p_uvs) { //uv
+ version |= 2;
+ uv_ofs = stride;
+ stride += 2;
+ }
+
+ if (p_light_angles) { //light_angles
+ version |= 4;
+ light_angle_ofs = stride;
+ stride += 1;
+ }
+
+ float b[(2 + 2 + 4 + 1) * 4];
+
+ for (int i = 0; i < p_points; i++) {
+ b[stride * i + 0] = p_vertices[i].x;
+ b[stride * i + 1] = p_vertices[i].y;
+ }
+
+ if (p_colors) {
+
+ for (int i = 0; i < p_points; i++) {
+ b[stride * i + color_ofs + 0] = p_colors[i].r;
+ b[stride * i + color_ofs + 1] = p_colors[i].g;
+ b[stride * i + color_ofs + 2] = p_colors[i].b;
+ b[stride * i + color_ofs + 3] = p_colors[i].a;
+ }
+ }
+
+ if (p_uvs) {
+
+ for (int i = 0; i < p_points; i++) {
+ b[stride * i + uv_ofs + 0] = p_uvs[i].x;
+ b[stride * i + uv_ofs + 1] = p_uvs[i].y;
+ }
+ }
+
+ if (p_light_angles) {
+
+ for (int i = 0; i < p_points; i++) {
+ b[stride * i + light_angle_ofs] = p_light_angles[i];
+ }
+ }
+
+ glBindBuffer(GL_ARRAY_BUFFER, data.polygon_buffer);
+ //TODO the below call may need to be replaced with: p_points * stride * 4 * sizeof(float), &b[0]);
+ storage->buffer_orphan_and_upload(data.polygon_buffer_size, 0, p_points * stride * 4, &b[0]);
+
+ glBindVertexArray(data.polygon_buffer_quad_arrays[version]);
+ glDrawArrays(prim[p_points], 0, p_points);
+ glBindVertexArray(0);
+ glBindBuffer(GL_ARRAY_BUFFER, 0);
+
+ storage->info.render._2d_draw_call_count++;
+}
+
+void RasterizerCanvasBaseGLES3::render_rect_nvidia_workaround(const Item::CommandRect *p_rect, const RasterizerStorageGLES3::Texture *p_texture) {
+
+ if (p_texture) {
+
+ bool send_light_angles = false;
+
+ // only need to use light angles when normal mapping
+ // otherwise we can use the default shader
+ if (state.current_normal != RID()) {
+ send_light_angles = true;
+ }
+
+ // we don't want to use texture rect, and we want to send light angles if we are using normal mapping
+ _set_texture_rect_mode(false, false, send_light_angles);
+
+ bool untile = false;
+
+ if (p_rect->flags & CANVAS_RECT_TILE && !(p_texture->flags & VS::TEXTURE_FLAG_REPEAT)) {
+ glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
+ glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
+ untile = true;
+ }
+
+ Size2 texpixel_size(1.0 / p_texture->width, 1.0 / p_texture->height);
+
+ state.canvas_shader.set_uniform(CanvasShaderGLES3::CLIP_RECT_UV, p_rect->flags & CANVAS_RECT_CLIP_UV);
+
+ Vector2 points[4] = {
+ p_rect->rect.position,
+ p_rect->rect.position + Vector2(p_rect->rect.size.x, 0.0),
+ p_rect->rect.position + p_rect->rect.size,
+ p_rect->rect.position + Vector2(0.0, p_rect->rect.size.y),
+ };
+
+ if (p_rect->rect.size.x < 0) {
+ SWAP(points[0], points[1]);
+ SWAP(points[2], points[3]);
+ }
+ if (p_rect->rect.size.y < 0) {
+ SWAP(points[0], points[3]);
+ SWAP(points[1], points[2]);
+ }
+ Rect2 src_rect = (p_rect->flags & CANVAS_RECT_REGION) ? Rect2(p_rect->source.position * texpixel_size, p_rect->source.size * texpixel_size) : Rect2(0, 0, 1, 1);
+
+ Vector2 uvs[4] = {
+ src_rect.position,
+ src_rect.position + Vector2(src_rect.size.x, 0.0),
+ src_rect.position + src_rect.size,
+ src_rect.position + Vector2(0.0, src_rect.size.y),
+ };
+
+ // for encoding in light angle
+ bool flip_h = false;
+ bool flip_v = false;
+
+ if (p_rect->flags & CANVAS_RECT_TRANSPOSE) {
+ SWAP(uvs[1], uvs[3]);
+ }
+
+ if (p_rect->flags & CANVAS_RECT_FLIP_H) {
+ SWAP(uvs[0], uvs[1]);
+ SWAP(uvs[2], uvs[3]);
+ flip_h = true;
+ flip_v = !flip_v;
+ }
+ if (p_rect->flags & CANVAS_RECT_FLIP_V) {
+ SWAP(uvs[0], uvs[3]);
+ SWAP(uvs[1], uvs[2]);
+ flip_v = !flip_v;
+ }
+
+ if (send_light_angles) {
+ // for single rects, there is no need to fully utilize the light angle,
+ // we only need it to encode flips (horz and vert). But the shader can be reused with
+ // batching in which case the angle encodes the transform as well as
+ // the flips.
+ // Note transpose is NYI. I don't think it worked either with the non-nvidia method.
+
+ // if horizontal flip, angle is 180
+ float angle = 0.0f;
+ if (flip_h)
+ angle = Math_PI;
+
+ // add 1 (to take care of zero floating point error with sign)
+ angle += 1.0f;
+
+ // flip if necessary
+ if (flip_v)
+ angle *= -1.0f;
+
+ // light angle must be sent for each vert, instead as a single uniform in the uniform draw method
+ // this has the benefit of enabling batching with light angles.
+ float light_angles[4] = { angle, angle, angle, angle };
+
+ _draw_gui_primitive(4, points, NULL, uvs, light_angles);
+ } else {
+ _draw_gui_primitive(4, points, NULL, uvs);
+ }
+
+ if (untile) {
+ glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
+ glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
+ }
+
+ } else {
+ _set_texture_rect_mode(false);
+
+ state.canvas_shader.set_uniform(CanvasShaderGLES3::CLIP_RECT_UV, false);
+
+ Vector2 points[4] = {
+ p_rect->rect.position,
+ p_rect->rect.position + Vector2(p_rect->rect.size.x, 0.0),
+ p_rect->rect.position + p_rect->rect.size,
+ p_rect->rect.position + Vector2(0.0, p_rect->rect.size.y),
+ };
+
+ _draw_gui_primitive(4, points, NULL, nullptr);
+ }
+}
+
+void RasterizerCanvasBaseGLES3::_copy_texscreen(const Rect2 &p_rect) {
+
+ ERR_FAIL_COND_MSG(storage->frame.current_rt->effects.mip_maps[0].sizes.size() == 0, "Can't use screen texture copying in a render target configured without copy buffers.");
+
+ glDisable(GL_BLEND);
+
+ state.canvas_texscreen_used = true;
+ //blur diffuse into effect mipmaps using separatable convolution
+ //storage->shaders.copy.set_conditional(CopyShaderGLES3::GAUSSIAN_HORIZONTAL,true);
+
+ Vector2 wh(storage->frame.current_rt->width, storage->frame.current_rt->height);
+
+ Color blur_section(p_rect.position.x / wh.x, p_rect.position.y / wh.y, p_rect.size.x / wh.x, p_rect.size.y / wh.y);
+
+ if (p_rect != Rect2()) {
+
+ scene_render->state.effect_blur_shader.set_conditional(EffectBlurShaderGLES3::USE_BLUR_SECTION, true);
+ storage->shaders.copy.set_conditional(CopyShaderGLES3::USE_COPY_SECTION, true);
+ }
+
+ glBindFramebuffer(GL_FRAMEBUFFER, storage->frame.current_rt->effects.mip_maps[0].sizes[0].fbo);
+ glActiveTexture(GL_TEXTURE0);
+ glBindTexture(GL_TEXTURE_2D, storage->frame.current_rt->color);
+
+ storage->shaders.copy.bind();
+ storage->shaders.copy.set_uniform(CopyShaderGLES3::COPY_SECTION, blur_section);
+
+ scene_render->_copy_screen();
+
+ for (int i = 0; i < storage->frame.current_rt->effects.mip_maps[1].sizes.size(); i++) {
+
+ int vp_w = storage->frame.current_rt->effects.mip_maps[1].sizes[i].width;
+ int vp_h = storage->frame.current_rt->effects.mip_maps[1].sizes[i].height;
+ glViewport(0, 0, vp_w, vp_h);
+ //horizontal pass
+ scene_render->state.effect_blur_shader.set_conditional(EffectBlurShaderGLES3::GAUSSIAN_HORIZONTAL, true);
+ scene_render->state.effect_blur_shader.bind();
+ scene_render->state.effect_blur_shader.set_uniform(EffectBlurShaderGLES3::PIXEL_SIZE, Vector2(1.0 / vp_w, 1.0 / vp_h));
+ scene_render->state.effect_blur_shader.set_uniform(EffectBlurShaderGLES3::LOD, float(i));
+ scene_render->state.effect_blur_shader.set_uniform(EffectBlurShaderGLES3::BLUR_SECTION, blur_section);
+ glActiveTexture(GL_TEXTURE0);
+ glBindTexture(GL_TEXTURE_2D, storage->frame.current_rt->effects.mip_maps[0].color); //previous level, since mipmaps[0] starts one level bigger
+ glBindFramebuffer(GL_FRAMEBUFFER, storage->frame.current_rt->effects.mip_maps[1].sizes[i].fbo);
+
+ scene_render->_copy_screen();
+
+ scene_render->state.effect_blur_shader.set_conditional(EffectBlurShaderGLES3::GAUSSIAN_HORIZONTAL, false);
+
+ //vertical pass
+ scene_render->state.effect_blur_shader.set_conditional(EffectBlurShaderGLES3::GAUSSIAN_VERTICAL, true);
+ scene_render->state.effect_blur_shader.bind();
+ scene_render->state.effect_blur_shader.set_uniform(EffectBlurShaderGLES3::PIXEL_SIZE, Vector2(1.0 / vp_w, 1.0 / vp_h));
+ scene_render->state.effect_blur_shader.set_uniform(EffectBlurShaderGLES3::LOD, float(i));
+ scene_render->state.effect_blur_shader.set_uniform(EffectBlurShaderGLES3::BLUR_SECTION, blur_section);
+ glActiveTexture(GL_TEXTURE0);
+ glBindTexture(GL_TEXTURE_2D, storage->frame.current_rt->effects.mip_maps[1].color);
+ glBindFramebuffer(GL_FRAMEBUFFER, storage->frame.current_rt->effects.mip_maps[0].sizes[i + 1].fbo); //next level, since mipmaps[0] starts one level bigger
+
+ scene_render->_copy_screen();
+
+ scene_render->state.effect_blur_shader.set_conditional(EffectBlurShaderGLES3::GAUSSIAN_VERTICAL, false);
+ }
+
+ scene_render->state.effect_blur_shader.set_conditional(EffectBlurShaderGLES3::USE_BLUR_SECTION, false);
+ storage->shaders.copy.set_conditional(CopyShaderGLES3::USE_COPY_SECTION, false);
+
+ glBindFramebuffer(GL_FRAMEBUFFER, storage->frame.current_rt->fbo); //back to front
+ glViewport(0, 0, storage->frame.current_rt->width, storage->frame.current_rt->height);
+
+ // back to canvas, force rebind
+ state.using_texture_rect = true;
+ _set_texture_rect_mode(false);
+
+ _bind_canvas_texture(state.current_tex, state.current_normal, true);
+
+ glEnable(GL_BLEND);
+}
+
+void RasterizerCanvasBaseGLES3::canvas_debug_viewport_shadows(Light *p_lights_with_shadow) {
+
+ Light *light = p_lights_with_shadow;
+
+ canvas_begin(); //reset
+ glVertexAttrib4f(VS::ARRAY_COLOR, 1, 1, 1, 1);
+ int h = 10;
+ int w = storage->frame.current_rt->width;
+ int ofs = h;
+ glDisable(GL_BLEND);
+
+ while (light) {
+ if (light->shadow_buffer.is_valid()) {
+
+ RasterizerStorageGLES3::CanvasLightShadow *sb = storage->canvas_light_shadow_owner.get(light->shadow_buffer);
+ if (sb) {
+ glBindTexture(GL_TEXTURE_2D, sb->distance);
+ draw_generic_textured_rect(Rect2(h, ofs, w - h * 2, h), Rect2(0, 0, 1, 1));
+ ofs += h * 2;
+ }
+ }
+
+ light = light->shadows_next_ptr;
+ }
+
+ canvas_end();
+}
+
+void RasterizerCanvasBaseGLES3::canvas_light_shadow_buffer_update(RID p_buffer, const Transform2D &p_light_xform, int p_light_mask, float p_near, float p_far, LightOccluderInstance *p_occluders, CameraMatrix *p_xform_cache) {
+
+ RasterizerStorageGLES3::CanvasLightShadow *cls = storage->canvas_light_shadow_owner.get(p_buffer);
+ ERR_FAIL_COND(!cls);
+
+ glDisable(GL_BLEND);
+ glDisable(GL_SCISSOR_TEST);
+ glDisable(GL_DITHER);
+ glDisable(GL_CULL_FACE);
+ glDepthFunc(GL_LEQUAL);
+ glEnable(GL_DEPTH_TEST);
+ glDepthMask(true);
+
+ glBindFramebuffer(GL_FRAMEBUFFER, cls->fbo);
+
+ state.canvas_shadow_shader.bind();
+
+ glViewport(0, 0, cls->size, cls->height);
+ glClearDepth(1.0f);
+ glClearColor(1, 1, 1, 1);
+ glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
+
+ VS::CanvasOccluderPolygonCullMode cull = VS::CANVAS_OCCLUDER_POLYGON_CULL_DISABLED;
+
+ for (int i = 0; i < 4; i++) {
+
+ //make sure it remains orthogonal, makes easy to read angle later
+
+ Transform light;
+ light.origin[0] = p_light_xform[2][0];
+ light.origin[1] = p_light_xform[2][1];
+ light.basis[0][0] = p_light_xform[0][0];
+ light.basis[0][1] = p_light_xform[1][0];
+ light.basis[1][0] = p_light_xform[0][1];
+ light.basis[1][1] = p_light_xform[1][1];
+
+ //light.basis.scale(Vector3(to_light.elements[0].length(),to_light.elements[1].length(),1));
+
+ //p_near=1;
+ CameraMatrix projection;
+ {
+ real_t fov = 90;
+ real_t nearp = p_near;
+ real_t farp = p_far;
+ real_t aspect = 1.0;
+
+ real_t ymax = nearp * Math::tan(Math::deg2rad(fov * 0.5));
+ real_t ymin = -ymax;
+ real_t xmin = ymin * aspect;
+ real_t xmax = ymax * aspect;
+
+ projection.set_frustum(xmin, xmax, ymin, ymax, nearp, farp);
+ }
+
+ Vector3 cam_target = Basis(Vector3(0, 0, Math_PI * 2 * (i / 4.0))).xform(Vector3(0, 1, 0));
+ projection = projection * CameraMatrix(Transform().looking_at(cam_target, Vector3(0, 0, -1)).affine_inverse());
+
+ state.canvas_shadow_shader.set_uniform(CanvasShadowShaderGLES3::PROJECTION_MATRIX, projection);
+ state.canvas_shadow_shader.set_uniform(CanvasShadowShaderGLES3::LIGHT_MATRIX, light);
+ state.canvas_shadow_shader.set_uniform(CanvasShadowShaderGLES3::DISTANCE_NORM, 1.0 / p_far);
+
+ if (i == 0)
+ *p_xform_cache = projection;
+
+ glViewport(0, (cls->height / 4) * i, cls->size, cls->height / 4);
+
+ LightOccluderInstance *instance = p_occluders;
+
+ while (instance) {
+
+ RasterizerStorageGLES3::CanvasOccluder *cc = storage->canvas_occluder_owner.getornull(instance->polygon_buffer);
+ if (!cc || cc->len == 0 || !(p_light_mask & instance->light_mask)) {
+
+ instance = instance->next;
+ continue;
+ }
+
+ state.canvas_shadow_shader.set_uniform(CanvasShadowShaderGLES3::WORLD_MATRIX, instance->xform_cache);
+
+ VS::CanvasOccluderPolygonCullMode transformed_cull_cache = instance->cull_cache;
+
+ if (transformed_cull_cache != VS::CANVAS_OCCLUDER_POLYGON_CULL_DISABLED &&
+ (p_light_xform.basis_determinant() * instance->xform_cache.basis_determinant()) < 0) {
+ transformed_cull_cache =
+ transformed_cull_cache == VS::CANVAS_OCCLUDER_POLYGON_CULL_CLOCKWISE ?
+ VS::CANVAS_OCCLUDER_POLYGON_CULL_COUNTER_CLOCKWISE :
+ VS::CANVAS_OCCLUDER_POLYGON_CULL_CLOCKWISE;
+ }
+
+ if (cull != transformed_cull_cache) {
+
+ cull = transformed_cull_cache;
+ switch (cull) {
+ case VS::CANVAS_OCCLUDER_POLYGON_CULL_DISABLED: {
+
+ glDisable(GL_CULL_FACE);
+
+ } break;
+ case VS::CANVAS_OCCLUDER_POLYGON_CULL_CLOCKWISE: {
+
+ glEnable(GL_CULL_FACE);
+ glCullFace(GL_FRONT);
+ } break;
+ case VS::CANVAS_OCCLUDER_POLYGON_CULL_COUNTER_CLOCKWISE: {
+
+ glEnable(GL_CULL_FACE);
+ glCullFace(GL_BACK);
+
+ } break;
+ }
+ }
+
+ glBindVertexArray(cc->array_id);
+ glDrawElements(GL_TRIANGLES, cc->len * 3, GL_UNSIGNED_SHORT, 0);
+
+ instance = instance->next;
+ }
+ }
+
+ glBindVertexArray(0);
+}
+void RasterizerCanvasBaseGLES3::reset_canvas() {
+
+ if (storage->frame.current_rt) {
+ glBindFramebuffer(GL_FRAMEBUFFER, storage->frame.current_rt->fbo);
+ glColorMask(1, 1, 1, 1); //don't touch alpha
+ }
+
+ glBindVertexArray(0);
+ glDisable(GL_CULL_FACE);
+ glDisable(GL_DEPTH_TEST);
+ glDisable(GL_SCISSOR_TEST);
+ glDisable(GL_DITHER);
+ glEnable(GL_BLEND);
+ glBlendEquation(GL_FUNC_ADD);
+ if (storage->frame.current_rt && storage->frame.current_rt->flags[RasterizerStorage::RENDER_TARGET_TRANSPARENT]) {
+ glBlendFuncSeparate(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA, GL_ONE, GL_ONE_MINUS_SRC_ALPHA);
+ } else {
+ glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
+ }
+ //glPolygonMode(GL_FRONT_AND_BACK,GL_FILL);
+ //glLineWidth(1.0);
+ glBindBuffer(GL_ARRAY_BUFFER, 0);
+ glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0);
+
+ //use for reading from screen
+ if (storage->frame.current_rt && !storage->frame.current_rt->flags[RasterizerStorage::RENDER_TARGET_NO_SAMPLING]) {
+ glActiveTexture(GL_TEXTURE0 + storage->config.max_texture_image_units - 3);
+ glBindTexture(GL_TEXTURE_2D, storage->frame.current_rt->effects.mip_maps[0].color);
+ }
+
+ glActiveTexture(GL_TEXTURE0);
+ glBindTexture(GL_TEXTURE_2D, storage->resources.white_tex);
+
+ glVertexAttrib4f(VS::ARRAY_COLOR, 1, 1, 1, 1);
+
+ Transform canvas_transform;
+
+ if (storage->frame.current_rt) {
+
+ float csy = 1.0;
+ if (storage->frame.current_rt && storage->frame.current_rt->flags[RasterizerStorage::RENDER_TARGET_VFLIP]) {
+ csy = -1.0;
+ }
+ canvas_transform.translate(-(storage->frame.current_rt->width / 2.0f), -(storage->frame.current_rt->height / 2.0f), 0.0f);
+ canvas_transform.scale(Vector3(2.0f / storage->frame.current_rt->width, csy * -2.0f / storage->frame.current_rt->height, 1.0f));
+ } else {
+ Vector2 ssize = OS::get_singleton()->get_window_size();
+ canvas_transform.translate(-(ssize.width / 2.0f), -(ssize.height / 2.0f), 0.0f);
+ canvas_transform.scale(Vector3(2.0f / ssize.width, -2.0f / ssize.height, 1.0f));
+ }
+
+ state.vp = canvas_transform;
+
+ store_transform(canvas_transform, state.canvas_item_ubo_data.projection_matrix);
+ state.canvas_item_ubo_data.time = storage->frame.time[0];
+
+ glBindBuffer(GL_UNIFORM_BUFFER, state.canvas_item_ubo);
+ glBufferData(GL_UNIFORM_BUFFER, sizeof(CanvasItemUBO), &state.canvas_item_ubo_data, GL_DYNAMIC_DRAW);
+ glBindBuffer(GL_UNIFORM_BUFFER, 0);
+
+ state.canvas_texscreen_used = false;
+}
+
+void RasterizerCanvasBaseGLES3::draw_generic_textured_rect(const Rect2 &p_rect, const Rect2 &p_src) {
+
+ state.canvas_shader.set_uniform(CanvasShaderGLES3::DST_RECT, Color(p_rect.position.x, p_rect.position.y, p_rect.size.x, p_rect.size.y));
+ state.canvas_shader.set_uniform(CanvasShaderGLES3::SRC_RECT, Color(p_src.position.x, p_src.position.y, p_src.size.x, p_src.size.y));
+ state.canvas_shader.set_uniform(CanvasShaderGLES3::CLIP_RECT_UV, false);
+
+ glDrawArrays(GL_TRIANGLE_FAN, 0, 4);
+}
+
+void RasterizerCanvasBaseGLES3::draw_lens_distortion_rect(const Rect2 &p_rect, float p_k1, float p_k2, const Vector2 &p_eye_center, float p_oversample) {
+ Vector2 half_size;
+ if (storage->frame.current_rt) {
+ half_size = Vector2(storage->frame.current_rt->width, storage->frame.current_rt->height);
+ } else {
+ half_size = OS::get_singleton()->get_window_size();
+ }
+ half_size *= 0.5;
+ Vector2 offset((p_rect.position.x - half_size.x) / half_size.x, (p_rect.position.y - half_size.y) / half_size.y);
+ Vector2 scale(p_rect.size.x / half_size.x, p_rect.size.y / half_size.y);
+
+ float aspect_ratio = p_rect.size.x / p_rect.size.y;
+
+ // setup our lens shader
+ state.lens_shader.bind();
+ state.lens_shader.set_uniform(LensDistortedShaderGLES3::OFFSET, offset);
+ state.lens_shader.set_uniform(LensDistortedShaderGLES3::SCALE, scale);
+ state.lens_shader.set_uniform(LensDistortedShaderGLES3::K1, p_k1);
+ state.lens_shader.set_uniform(LensDistortedShaderGLES3::K2, p_k2);
+ state.lens_shader.set_uniform(LensDistortedShaderGLES3::EYE_CENTER, p_eye_center);
+ state.lens_shader.set_uniform(LensDistortedShaderGLES3::UPSCALE, p_oversample);
+ state.lens_shader.set_uniform(LensDistortedShaderGLES3::ASPECT_RATIO, aspect_ratio);
+
+ glBindBufferBase(GL_UNIFORM_BUFFER, 0, state.canvas_item_ubo);
+ glBindVertexArray(data.canvas_quad_array);
+
+ // and draw
+ glDrawArrays(GL_TRIANGLE_FAN, 0, 4);
+
+ glBindVertexArray(0);
+ glBindBufferBase(GL_UNIFORM_BUFFER, 0, 0);
+}
+
+void RasterizerCanvasBaseGLES3::draw_window_margins(int *black_margin, RID *black_image) {
+
+ Vector2 window_size = OS::get_singleton()->get_window_size();
+ int window_h = window_size.height;
+ int window_w = window_size.width;
+
+ glBindFramebuffer(GL_FRAMEBUFFER, RasterizerStorageGLES3::system_fbo);
+ glViewport(0, 0, window_size.width, window_size.height);
+ canvas_begin();
+
+ if (black_image[MARGIN_LEFT].is_valid()) {
+ _bind_canvas_texture(black_image[MARGIN_LEFT], RID(), true);
+ Size2 sz(storage->texture_get_width(black_image[MARGIN_LEFT]), storage->texture_get_height(black_image[MARGIN_LEFT]));
+
+ draw_generic_textured_rect(Rect2(0, 0, black_margin[MARGIN_LEFT], window_h),
+ Rect2(0, 0, (float)black_margin[MARGIN_LEFT] / sz.x, (float)(window_h) / sz.y));
+ } else if (black_margin[MARGIN_LEFT]) {
+ glActiveTexture(GL_TEXTURE0);
+ glBindTexture(GL_TEXTURE_2D, storage->resources.black_tex);
+
+ draw_generic_textured_rect(Rect2(0, 0, black_margin[MARGIN_LEFT], window_h), Rect2(0, 0, 1, 1));
+ }
+
+ if (black_image[MARGIN_RIGHT].is_valid()) {
+ _bind_canvas_texture(black_image[MARGIN_RIGHT], RID(), true);
+ Size2 sz(storage->texture_get_width(black_image[MARGIN_RIGHT]), storage->texture_get_height(black_image[MARGIN_RIGHT]));
+ draw_generic_textured_rect(Rect2(window_w - black_margin[MARGIN_RIGHT], 0, black_margin[MARGIN_RIGHT], window_h),
+ Rect2(0, 0, (float)black_margin[MARGIN_RIGHT] / sz.x, (float)window_h / sz.y));
+ } else if (black_margin[MARGIN_RIGHT]) {
+ glActiveTexture(GL_TEXTURE0);
+ glBindTexture(GL_TEXTURE_2D, storage->resources.black_tex);
+
+ draw_generic_textured_rect(Rect2(window_w - black_margin[MARGIN_RIGHT], 0, black_margin[MARGIN_RIGHT], window_h), Rect2(0, 0, 1, 1));
+ }
+
+ if (black_image[MARGIN_TOP].is_valid()) {
+ _bind_canvas_texture(black_image[MARGIN_TOP], RID(), true);
+
+ Size2 sz(storage->texture_get_width(black_image[MARGIN_TOP]), storage->texture_get_height(black_image[MARGIN_TOP]));
+ draw_generic_textured_rect(Rect2(0, 0, window_w, black_margin[MARGIN_TOP]),
+ Rect2(0, 0, (float)window_w / sz.x, (float)black_margin[MARGIN_TOP] / sz.y));
+
+ } else if (black_margin[MARGIN_TOP]) {
+ glActiveTexture(GL_TEXTURE0);
+ glBindTexture(GL_TEXTURE_2D, storage->resources.black_tex);
+
+ draw_generic_textured_rect(Rect2(0, 0, window_w, black_margin[MARGIN_TOP]), Rect2(0, 0, 1, 1));
+ }
+
+ if (black_image[MARGIN_BOTTOM].is_valid()) {
+
+ _bind_canvas_texture(black_image[MARGIN_BOTTOM], RID(), true);
+
+ Size2 sz(storage->texture_get_width(black_image[MARGIN_BOTTOM]), storage->texture_get_height(black_image[MARGIN_BOTTOM]));
+ draw_generic_textured_rect(Rect2(0, window_h - black_margin[MARGIN_BOTTOM], window_w, black_margin[MARGIN_BOTTOM]),
+ Rect2(0, 0, (float)window_w / sz.x, (float)black_margin[MARGIN_BOTTOM] / sz.y));
+
+ } else if (black_margin[MARGIN_BOTTOM]) {
+
+ glActiveTexture(GL_TEXTURE0);
+ glBindTexture(GL_TEXTURE_2D, storage->resources.black_tex);
+
+ draw_generic_textured_rect(Rect2(0, window_h - black_margin[MARGIN_BOTTOM], window_w, black_margin[MARGIN_BOTTOM]), Rect2(0, 0, 1, 1));
+ }
+}
+
+void RasterizerCanvasBaseGLES3::initialize() {
+
+ {
+ //quad buffers
+
+ glGenBuffers(1, &data.canvas_quad_vertices);
+ glBindBuffer(GL_ARRAY_BUFFER, data.canvas_quad_vertices);
+ {
+ const float qv[8] = {
+ 0, 0,
+ 0, 1,
+ 1, 1,
+ 1, 0
+ };
+
+ glBufferData(GL_ARRAY_BUFFER, sizeof(float) * 8, qv, GL_STATIC_DRAW);
+ }
+
+ glBindBuffer(GL_ARRAY_BUFFER, 0); //unbind
+
+ glGenVertexArrays(1, &data.canvas_quad_array);
+ glBindVertexArray(data.canvas_quad_array);
+ glBindBuffer(GL_ARRAY_BUFFER, data.canvas_quad_vertices);
+ glVertexAttribPointer(0, 2, GL_FLOAT, GL_FALSE, sizeof(float) * 2, 0);
+ glEnableVertexAttribArray(0);
+ glBindVertexArray(0);
+ glBindBuffer(GL_ARRAY_BUFFER, 0); //unbind
+ }
+ {
+ //particle quad buffers
+
+ glGenBuffers(1, &data.particle_quad_vertices);
+ glBindBuffer(GL_ARRAY_BUFFER, data.particle_quad_vertices);
+ {
+ //quad of size 1, with pivot on the center for particles, then regular UVS. Color is general plus fetched from particle
+ const float qv[16] = {
+ -0.5, -0.5,
+ 0.0, 0.0,
+ -0.5, 0.5,
+ 0.0, 1.0,
+ 0.5, 0.5,
+ 1.0, 1.0,
+ 0.5, -0.5,
+ 1.0, 0.0
+ };
+
+ glBufferData(GL_ARRAY_BUFFER, sizeof(float) * 16, qv, GL_STATIC_DRAW);
+ }
+
+ glBindBuffer(GL_ARRAY_BUFFER, 0); //unbind
+
+ glGenVertexArrays(1, &data.particle_quad_array);
+ glBindVertexArray(data.particle_quad_array);
+ glBindBuffer(GL_ARRAY_BUFFER, data.particle_quad_vertices);
+ glEnableVertexAttribArray(VS::ARRAY_VERTEX);
+ glVertexAttribPointer(VS::ARRAY_VERTEX, 2, GL_FLOAT, GL_FALSE, sizeof(float) * 4, 0);
+ glEnableVertexAttribArray(VS::ARRAY_TEX_UV);
+ glVertexAttribPointer(VS::ARRAY_TEX_UV, 2, GL_FLOAT, GL_FALSE, sizeof(float) * 4, CAST_INT_TO_UCHAR_PTR(8));
+ glBindVertexArray(0);
+ glBindBuffer(GL_ARRAY_BUFFER, 0); //unbind
+ }
+ {
+
+ uint32_t poly_size = GLOBAL_DEF_RST("rendering/limits/buffers/canvas_polygon_buffer_size_kb", 128);
+ ProjectSettings::get_singleton()->set_custom_property_info("rendering/limits/buffers/canvas_polygon_buffer_size_kb", PropertyInfo(Variant::INT, "rendering/limits/buffers/canvas_polygon_buffer_size_kb", PROPERTY_HINT_RANGE, "0,256,1,or_greater"));
+ poly_size *= 1024; //kb
+ poly_size = MAX(poly_size, (2 + 2 + 4 + 1) * 4 * sizeof(float));
+ glGenBuffers(1, &data.polygon_buffer);
+ glBindBuffer(GL_ARRAY_BUFFER, data.polygon_buffer);
+ glBufferData(GL_ARRAY_BUFFER, poly_size, NULL, GL_DYNAMIC_DRAW); //allocate max size
+ glBindBuffer(GL_ARRAY_BUFFER, 0);
+ data.polygon_buffer_size = poly_size;
+
+ //quad arrays
+ for (int i = 0; i < Data::NUM_QUAD_ARRAY_VARIATIONS; i++) {
+ glGenVertexArrays(1, &data.polygon_buffer_quad_arrays[i]);
+ glBindVertexArray(data.polygon_buffer_quad_arrays[i]);
+ glBindBuffer(GL_ARRAY_BUFFER, data.polygon_buffer);
+
+ int uv_ofs = 0;
+ int color_ofs = 0;
+ int light_angle_ofs = 0;
+ int stride = 2 * 4;
+
+ if (i & 1) { //color
+ color_ofs = stride;
+ stride += 4 * 4;
+ }
+
+ if (i & 2) { //uv
+ uv_ofs = stride;
+ stride += 2 * 4;
+ }
+
+ if (i & 4) { //light_angle
+ light_angle_ofs = stride;
+ stride += 1 * 4;
+ }
+
+ glEnableVertexAttribArray(VS::ARRAY_VERTEX);
+ glVertexAttribPointer(VS::ARRAY_VERTEX, 2, GL_FLOAT, GL_FALSE, stride, NULL);
+
+ if (i & 1) {
+ glEnableVertexAttribArray(VS::ARRAY_COLOR);
+ glVertexAttribPointer(VS::ARRAY_COLOR, 4, GL_FLOAT, GL_FALSE, stride, CAST_INT_TO_UCHAR_PTR(color_ofs));
+ }
+
+ if (i & 2) {
+ glEnableVertexAttribArray(VS::ARRAY_TEX_UV);
+ glVertexAttribPointer(VS::ARRAY_TEX_UV, 2, GL_FLOAT, GL_FALSE, stride, CAST_INT_TO_UCHAR_PTR(uv_ofs));
+ }
+
+ if (i & 4) {
+ // reusing tangent for light_angle
+ glEnableVertexAttribArray(VS::ARRAY_TANGENT);
+ glVertexAttribPointer(VS::ARRAY_TANGENT, 1, GL_FLOAT, GL_FALSE, stride, CAST_INT_TO_UCHAR_PTR(light_angle_ofs));
+ }
+
+ glBindVertexArray(0);
+ }
+
+ glGenVertexArrays(1, &data.polygon_buffer_pointer_array);
+
+ uint32_t index_size = GLOBAL_DEF_RST("rendering/limits/buffers/canvas_polygon_index_buffer_size_kb", 128);
+ ProjectSettings::get_singleton()->set_custom_property_info("rendering/limits/buffers/canvas_polygon_index_buffer_size_kb", PropertyInfo(Variant::INT, "rendering/limits/buffers/canvas_polygon_index_buffer_size_kb", PROPERTY_HINT_RANGE, "0,256,1,or_greater"));
+ index_size *= 1024; //kb
+ glGenBuffers(1, &data.polygon_index_buffer);
+ glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, data.polygon_index_buffer);
+ glBufferData(GL_ELEMENT_ARRAY_BUFFER, index_size, NULL, GL_DYNAMIC_DRAW); //allocate max size
+ glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0);
+
+ data.polygon_index_buffer_size = index_size;
+ }
+
+ store_transform(Transform(), state.canvas_item_ubo_data.projection_matrix);
+
+ glGenBuffers(1, &state.canvas_item_ubo);
+ glBindBuffer(GL_UNIFORM_BUFFER, state.canvas_item_ubo);
+ glBufferData(GL_UNIFORM_BUFFER, sizeof(CanvasItemUBO), &state.canvas_item_ubo_data, GL_DYNAMIC_DRAW);
+ glBindBuffer(GL_UNIFORM_BUFFER, 0);
+
+ state.canvas_shader.init();
+ state.canvas_shader.set_base_material_tex_index(2);
+ state.canvas_shadow_shader.init();
+ state.lens_shader.init();
+
+ state.canvas_shader.set_conditional(CanvasShaderGLES3::USE_RGBA_SHADOWS, storage->config.use_rgba_2d_shadows);
+ state.canvas_shadow_shader.set_conditional(CanvasShadowShaderGLES3::USE_RGBA_SHADOWS, storage->config.use_rgba_2d_shadows);
+
+ state.canvas_shader.set_conditional(CanvasShaderGLES3::USE_PIXEL_SNAP, GLOBAL_DEF("rendering/quality/2d/use_pixel_snap", false));
+}
+
+void RasterizerCanvasBaseGLES3::finalize() {
+
+ glDeleteBuffers(1, &data.canvas_quad_vertices);
+ glDeleteVertexArrays(1, &data.canvas_quad_array);
+
+ glDeleteBuffers(1, &data.canvas_quad_vertices);
+ glDeleteVertexArrays(1, &data.canvas_quad_array);
+
+ glDeleteVertexArrays(1, &data.polygon_buffer_pointer_array);
+}
+
+RasterizerCanvasBaseGLES3::RasterizerCanvasBaseGLES3() {
+}
diff --git a/drivers/gles3/rasterizer_canvas_base_gles3.h b/drivers/gles3/rasterizer_canvas_base_gles3.h
new file mode 100644
index 00000000000..687b7cae54c
--- /dev/null
+++ b/drivers/gles3/rasterizer_canvas_base_gles3.h
@@ -0,0 +1,163 @@
+/*************************************************************************/
+/* rasterizer_canvas_base_gles3.h */
+/*************************************************************************/
+/* This file is part of: */
+/* GODOT ENGINE */
+/* https://godotengine.org */
+/*************************************************************************/
+/* Copyright (c) 2007-2020 Juan Linietsky, Ariel Manzur. */
+/* Copyright (c) 2014-2020 Godot Engine contributors (cf. AUTHORS.md). */
+/* */
+/* Permission is hereby granted, free of charge, to any person obtaining */
+/* a copy of this software and associated documentation files (the */
+/* "Software"), to deal in the Software without restriction, including */
+/* without limitation the rights to use, copy, modify, merge, publish, */
+/* distribute, sublicense, and/or sell copies of the Software, and to */
+/* permit persons to whom the Software is furnished to do so, subject to */
+/* the following conditions: */
+/* */
+/* The above copyright notice and this permission notice shall be */
+/* included in all copies or substantial portions of the Software. */
+/* */
+/* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
+/* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
+/* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.*/
+/* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
+/* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
+/* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
+/* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
+/*************************************************************************/
+
+#ifndef RASTERIZERCANVASBASEGLES3_H
+#define RASTERIZERCANVASBASEGLES3_H
+
+#include "rasterizer_storage_gles3.h"
+#include "servers/visual/rasterizer.h"
+
+#include "shaders/canvas_shadow.glsl.gen.h"
+#include "shaders/lens_distorted.glsl.gen.h"
+
+class RasterizerSceneGLES3;
+
+class RasterizerCanvasBaseGLES3 : public RasterizerCanvas {
+public:
+ struct CanvasItemUBO {
+
+ float projection_matrix[16];
+ float time;
+ uint8_t padding[12];
+ };
+
+ RasterizerSceneGLES3 *scene_render;
+
+ struct Data {
+
+ enum { NUM_QUAD_ARRAY_VARIATIONS = 8 };
+
+ GLuint canvas_quad_vertices;
+ GLuint canvas_quad_array;
+
+ GLuint polygon_buffer;
+ GLuint polygon_buffer_quad_arrays[NUM_QUAD_ARRAY_VARIATIONS];
+ GLuint polygon_buffer_pointer_array;
+ GLuint polygon_index_buffer;
+
+ GLuint particle_quad_vertices;
+ GLuint particle_quad_array;
+
+ uint32_t polygon_buffer_size;
+ uint32_t polygon_index_buffer_size;
+
+ } data;
+
+ struct State {
+ CanvasItemUBO canvas_item_ubo_data;
+ GLuint canvas_item_ubo;
+ bool canvas_texscreen_used;
+ CanvasShaderGLES3 canvas_shader;
+ CanvasShadowShaderGLES3 canvas_shadow_shader;
+ LensDistortedShaderGLES3 lens_shader;
+
+ bool using_texture_rect;
+ bool using_ninepatch;
+
+ bool using_light_angle;
+ bool using_modulate;
+ bool using_large_vertex;
+
+ RID current_tex;
+ RID current_normal;
+ RasterizerStorageGLES3::Texture *current_tex_ptr;
+
+ Transform vp;
+
+ Color canvas_item_modulate;
+ Transform2D extra_matrix;
+ Transform2D final_transform;
+ bool using_skeleton;
+ Transform2D skeleton_transform;
+ Transform2D skeleton_transform_inverse;
+
+ } state;
+
+ RasterizerStorageGLES3 *storage;
+
+ struct LightInternal : public RID_Data {
+
+ struct UBOData {
+
+ float light_matrix[16];
+ float local_matrix[16];
+ float shadow_matrix[16];
+ float color[4];
+ float shadow_color[4];
+ float light_pos[2];
+ float shadowpixel_size;
+ float shadow_gradient;
+ float light_height;
+ float light_outside_alpha;
+ float shadow_distance_mult;
+ uint8_t padding[4];
+ } ubo_data;
+
+ GLuint ubo;
+ };
+
+ RID_Owner light_internal_owner;
+
+ virtual RID light_internal_create();
+ virtual void light_internal_update(RID p_rid, Light *p_light);
+ virtual void light_internal_free(RID p_rid);
+
+ virtual void canvas_begin();
+ virtual void canvas_end();
+
+ void _set_texture_rect_mode(bool p_enable, bool p_ninepatch = false, bool p_light_angle = false, bool p_modulate = false, bool p_large_vertex = false);
+ RasterizerStorageGLES3::Texture *_bind_canvas_texture(const RID &p_texture, const RID &p_normal_map, bool p_force = false);
+
+ void _draw_gui_primitive(int p_points, const Vector2 *p_vertices, const Color *p_colors, const Vector2 *p_uvs, const float *p_light_angles = nullptr);
+ void _draw_polygon(const int *p_indices, int p_index_count, int p_vertex_count, const Vector2 *p_vertices, const Vector2 *p_uvs, const Color *p_colors, bool p_singlecolor, const int *p_bones, const float *p_weights);
+ void _draw_generic(GLuint p_primitive, int p_vertex_count, const Vector2 *p_vertices, const Vector2 *p_uvs, const Color *p_colors, bool p_singlecolor);
+ void _draw_generic_indices(GLuint p_primitive, const int *p_indices, int p_index_count, int p_vertex_count, const Vector2 *p_vertices, const Vector2 *p_uvs, const Color *p_colors, bool p_singlecolor);
+
+ void _copy_texscreen(const Rect2 &p_rect);
+
+ virtual void canvas_debug_viewport_shadows(Light *p_lights_with_shadow);
+
+ virtual void canvas_light_shadow_buffer_update(RID p_buffer, const Transform2D &p_light_xform, int p_light_mask, float p_near, float p_far, LightOccluderInstance *p_occluders, CameraMatrix *p_xform_cache);
+
+ virtual void reset_canvas();
+
+ void draw_generic_textured_rect(const Rect2 &p_rect, const Rect2 &p_src);
+ void draw_lens_distortion_rect(const Rect2 &p_rect, float p_k1, float p_k2, const Vector2 &p_eye_center, float p_oversample);
+ void render_rect_nvidia_workaround(const Item::CommandRect *p_rect, const RasterizerStorageGLES3::Texture *p_texture);
+
+ void initialize();
+ void finalize();
+
+ virtual void draw_window_margins(int *black_margin, RID *black_image);
+
+ RasterizerCanvasBaseGLES3();
+};
+
+#endif // RASTERIZERCANVASBASEGLES3_H
diff --git a/drivers/gles3/rasterizer_canvas_gles3.cpp b/drivers/gles3/rasterizer_canvas_gles3.cpp
index f8c404ede85..b8aa24136ed 100644
--- a/drivers/gles3/rasterizer_canvas_gles3.cpp
+++ b/drivers/gles3/rasterizer_canvas_gles3.cpp
@@ -29,579 +29,8 @@
/*************************************************************************/
#include "rasterizer_canvas_gles3.h"
-
-#include "core/os/os.h"
-#include "core/project_settings.h"
-#include "rasterizer_scene_gles3.h"
#include "servers/visual/visual_server_raster.h"
-#ifndef GLES_OVER_GL
-#define glClearDepth glClearDepthf
-#endif
-
-static _FORCE_INLINE_ void store_transform2d(const Transform2D &p_mtx, float *p_array) {
-
- p_array[0] = p_mtx.elements[0][0];
- p_array[1] = p_mtx.elements[0][1];
- p_array[2] = 0;
- p_array[3] = 0;
- p_array[4] = p_mtx.elements[1][0];
- p_array[5] = p_mtx.elements[1][1];
- p_array[6] = 0;
- p_array[7] = 0;
- p_array[8] = 0;
- p_array[9] = 0;
- p_array[10] = 1;
- p_array[11] = 0;
- p_array[12] = p_mtx.elements[2][0];
- p_array[13] = p_mtx.elements[2][1];
- p_array[14] = 0;
- p_array[15] = 1;
-}
-
-static _FORCE_INLINE_ void store_transform(const Transform &p_mtx, float *p_array) {
- p_array[0] = p_mtx.basis.elements[0][0];
- p_array[1] = p_mtx.basis.elements[1][0];
- p_array[2] = p_mtx.basis.elements[2][0];
- p_array[3] = 0;
- p_array[4] = p_mtx.basis.elements[0][1];
- p_array[5] = p_mtx.basis.elements[1][1];
- p_array[6] = p_mtx.basis.elements[2][1];
- p_array[7] = 0;
- p_array[8] = p_mtx.basis.elements[0][2];
- p_array[9] = p_mtx.basis.elements[1][2];
- p_array[10] = p_mtx.basis.elements[2][2];
- p_array[11] = 0;
- p_array[12] = p_mtx.origin.x;
- p_array[13] = p_mtx.origin.y;
- p_array[14] = p_mtx.origin.z;
- p_array[15] = 1;
-}
-
-static _FORCE_INLINE_ void store_camera(const CameraMatrix &p_mtx, float *p_array) {
-
- for (int i = 0; i < 4; i++) {
- for (int j = 0; j < 4; j++) {
-
- p_array[i * 4 + j] = p_mtx.matrix[i][j];
- }
- }
-}
-
-RID RasterizerCanvasGLES3::light_internal_create() {
-
- LightInternal *li = memnew(LightInternal);
-
- glGenBuffers(1, &li->ubo);
- glBindBuffer(GL_UNIFORM_BUFFER, li->ubo);
- glBufferData(GL_UNIFORM_BUFFER, sizeof(LightInternal::UBOData), &state.canvas_item_ubo_data, GL_DYNAMIC_DRAW);
- glBindBuffer(GL_UNIFORM_BUFFER, 0);
-
- return light_internal_owner.make_rid(li);
-}
-
-void RasterizerCanvasGLES3::light_internal_update(RID p_rid, Light *p_light) {
-
- LightInternal *li = light_internal_owner.getornull(p_rid);
- ERR_FAIL_COND(!li);
-
- store_transform2d(p_light->light_shader_xform, li->ubo_data.light_matrix);
- store_transform2d(p_light->xform_cache.affine_inverse(), li->ubo_data.local_matrix);
- store_camera(p_light->shadow_matrix_cache, li->ubo_data.shadow_matrix);
-
- for (int i = 0; i < 4; i++) {
-
- li->ubo_data.color[i] = p_light->color[i] * p_light->energy;
- li->ubo_data.shadow_color[i] = p_light->shadow_color[i];
- }
-
- li->ubo_data.light_pos[0] = p_light->light_shader_pos.x;
- li->ubo_data.light_pos[1] = p_light->light_shader_pos.y;
- li->ubo_data.shadowpixel_size = (1.0 / p_light->shadow_buffer_size) * (1.0 + p_light->shadow_smooth);
- li->ubo_data.light_outside_alpha = p_light->mode == VS::CANVAS_LIGHT_MODE_MASK ? 1.0 : 0.0;
- li->ubo_data.light_height = p_light->height;
- if (p_light->radius_cache == 0)
- li->ubo_data.shadow_gradient = 0;
- else
- li->ubo_data.shadow_gradient = p_light->shadow_gradient_length / (p_light->radius_cache * 1.1);
-
- li->ubo_data.shadow_distance_mult = (p_light->radius_cache * 1.1);
-
- glBindBuffer(GL_UNIFORM_BUFFER, li->ubo);
- glBufferData(GL_UNIFORM_BUFFER, sizeof(LightInternal::UBOData), &li->ubo_data, GL_DYNAMIC_DRAW);
- glBindBuffer(GL_UNIFORM_BUFFER, 0);
-}
-
-void RasterizerCanvasGLES3::light_internal_free(RID p_rid) {
-
- LightInternal *li = light_internal_owner.getornull(p_rid);
- ERR_FAIL_COND(!li);
-
- glDeleteBuffers(1, &li->ubo);
- light_internal_owner.free(p_rid);
- memdelete(li);
-}
-
-void RasterizerCanvasGLES3::canvas_begin() {
-
- if (storage->frame.current_rt && storage->frame.clear_request) {
- // a clear request may be pending, so do it
- bool transparent = storage->frame.current_rt->flags[RasterizerStorage::RENDER_TARGET_TRANSPARENT];
-
- glClearColor(storage->frame.clear_request_color.r,
- storage->frame.clear_request_color.g,
- storage->frame.clear_request_color.b,
- transparent ? storage->frame.clear_request_color.a : 1.0);
- glClear(GL_COLOR_BUFFER_BIT);
- storage->frame.clear_request = false;
- glColorMask(1, 1, 1, transparent ? 1 : 0);
- }
-
- reset_canvas();
-
- state.canvas_shader.set_conditional(CanvasShaderGLES3::USE_TEXTURE_RECT, true);
- state.canvas_shader.set_conditional(CanvasShaderGLES3::USE_LIGHTING, false);
- state.canvas_shader.set_conditional(CanvasShaderGLES3::USE_SHADOWS, false);
- state.canvas_shader.set_conditional(CanvasShaderGLES3::SHADOW_FILTER_NEAREST, false);
- state.canvas_shader.set_conditional(CanvasShaderGLES3::SHADOW_FILTER_PCF3, false);
- state.canvas_shader.set_conditional(CanvasShaderGLES3::SHADOW_FILTER_PCF5, false);
- state.canvas_shader.set_conditional(CanvasShaderGLES3::SHADOW_FILTER_PCF7, false);
- state.canvas_shader.set_conditional(CanvasShaderGLES3::SHADOW_FILTER_PCF9, false);
- state.canvas_shader.set_conditional(CanvasShaderGLES3::SHADOW_FILTER_PCF13, false);
- state.canvas_shader.set_conditional(CanvasShaderGLES3::USE_DISTANCE_FIELD, false);
- state.canvas_shader.set_conditional(CanvasShaderGLES3::USE_NINEPATCH, false);
- state.canvas_shader.set_conditional(CanvasShaderGLES3::USE_LIGHT_ANGLE, false);
- state.canvas_shader.set_conditional(CanvasShaderGLES3::USE_SKELETON, false);
-
- state.canvas_shader.set_custom_shader(0);
- state.canvas_shader.bind();
- state.canvas_shader.set_uniform(CanvasShaderGLES3::FINAL_MODULATE, Color(1, 1, 1, 1));
- state.canvas_shader.set_uniform(CanvasShaderGLES3::MODELVIEW_MATRIX, Transform2D());
- state.canvas_shader.set_uniform(CanvasShaderGLES3::EXTRA_MATRIX, Transform2D());
- if (storage->frame.current_rt) {
- state.canvas_shader.set_uniform(CanvasShaderGLES3::SCREEN_PIXEL_SIZE, Vector2(1.0 / storage->frame.current_rt->width, 1.0 / storage->frame.current_rt->height));
- } else {
- state.canvas_shader.set_uniform(CanvasShaderGLES3::SCREEN_PIXEL_SIZE, Vector2(1.0, 1.0));
- }
-
- //state.canvas_shader.set_uniform(CanvasShaderGLES3::PROJECTION_MATRIX,state.vp);
- //state.canvas_shader.set_uniform(CanvasShaderGLES3::MODELVIEW_MATRIX,Transform());
- //state.canvas_shader.set_uniform(CanvasShaderGLES3::EXTRA_MATRIX,Transform());
-
- glBindBufferBase(GL_UNIFORM_BUFFER, 0, state.canvas_item_ubo);
- glBindVertexArray(data.canvas_quad_array);
- state.using_texture_rect = true;
- state.using_ninepatch = false;
- state.using_light_angle = false;
- state.using_skeleton = false;
-}
-
-void RasterizerCanvasGLES3::canvas_end() {
-
- glBindVertexArray(0);
- glBindBufferBase(GL_UNIFORM_BUFFER, 0, 0);
- glColorMask(1, 1, 1, 1);
-
- glVertexAttrib4f(VS::ARRAY_COLOR, 1, 1, 1, 1);
-
- state.using_texture_rect = false;
- state.using_ninepatch = false;
- state.using_light_angle = false;
-}
-
-RasterizerStorageGLES3::Texture *RasterizerCanvasGLES3::_bind_canvas_texture(const RID &p_texture, const RID &p_normal_map, bool p_force) {
-
- RasterizerStorageGLES3::Texture *tex_return = NULL;
-
- if (p_texture == state.current_tex && !p_force) {
- tex_return = state.current_tex_ptr;
- } else if (p_texture.is_valid()) {
-
- RasterizerStorageGLES3::Texture *texture = storage->texture_owner.getornull(p_texture);
-
- if (!texture) {
- state.current_tex = RID();
- state.current_tex_ptr = NULL;
- glActiveTexture(GL_TEXTURE0);
- glBindTexture(GL_TEXTURE_2D, storage->resources.white_tex);
-
- } else {
-
- if (texture->redraw_if_visible) { //check before proxy, because this is usually used with proxies
- VisualServerRaster::redraw_request();
- }
-
- texture = texture->get_ptr();
-
- if (texture->render_target)
- texture->render_target->used_in_frame = true;
-
- glActiveTexture(GL_TEXTURE0);
- glBindTexture(GL_TEXTURE_2D, texture->tex_id);
- state.current_tex = p_texture;
- state.current_tex_ptr = texture;
-
- tex_return = texture;
- }
-
- } else {
-
- glActiveTexture(GL_TEXTURE0);
- glBindTexture(GL_TEXTURE_2D, storage->resources.white_tex);
- state.current_tex = RID();
- state.current_tex_ptr = NULL;
- }
-
- if (p_normal_map == state.current_normal && !p_force) {
- //do none
- state.canvas_shader.set_uniform(CanvasShaderGLES3::USE_DEFAULT_NORMAL, state.current_normal.is_valid());
-
- } else if (p_normal_map.is_valid()) {
-
- RasterizerStorageGLES3::Texture *normal_map = storage->texture_owner.getornull(p_normal_map);
-
- if (!normal_map) {
- state.current_normal = RID();
- glActiveTexture(GL_TEXTURE1);
- glBindTexture(GL_TEXTURE_2D, storage->resources.normal_tex);
- state.canvas_shader.set_uniform(CanvasShaderGLES3::USE_DEFAULT_NORMAL, false);
-
- } else {
-
- if (normal_map->redraw_if_visible) { //check before proxy, because this is usually used with proxies
- VisualServerRaster::redraw_request();
- }
-
- normal_map = normal_map->get_ptr();
-
- glActiveTexture(GL_TEXTURE1);
- glBindTexture(GL_TEXTURE_2D, normal_map->tex_id);
- state.current_normal = p_normal_map;
- state.canvas_shader.set_uniform(CanvasShaderGLES3::USE_DEFAULT_NORMAL, true);
- }
-
- } else {
-
- state.current_normal = RID();
- glActiveTexture(GL_TEXTURE1);
- glBindTexture(GL_TEXTURE_2D, storage->resources.normal_tex);
- state.canvas_shader.set_uniform(CanvasShaderGLES3::USE_DEFAULT_NORMAL, false);
- }
-
- return tex_return;
-}
-
-void RasterizerCanvasGLES3::_set_texture_rect_mode(bool p_enable, bool p_ninepatch, bool p_light_angle) {
-
- if (state.using_texture_rect == p_enable && state.using_ninepatch == p_ninepatch && state.using_light_angle == p_light_angle)
- return;
-
- if (p_enable) {
- glBindVertexArray(data.canvas_quad_array);
-
- } else {
- glBindVertexArray(0);
- glBindBuffer(GL_ARRAY_BUFFER, 0);
- glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0);
- }
-
- state.canvas_shader.set_conditional(CanvasShaderGLES3::USE_NINEPATCH, p_ninepatch && p_enable);
- state.canvas_shader.set_conditional(CanvasShaderGLES3::USE_TEXTURE_RECT, p_enable);
- state.canvas_shader.set_conditional(CanvasShaderGLES3::USE_LIGHT_ANGLE, p_light_angle);
- state.canvas_shader.bind();
- state.canvas_shader.set_uniform(CanvasShaderGLES3::FINAL_MODULATE, state.canvas_item_modulate);
- state.canvas_shader.set_uniform(CanvasShaderGLES3::MODELVIEW_MATRIX, state.final_transform);
- state.canvas_shader.set_uniform(CanvasShaderGLES3::EXTRA_MATRIX, state.extra_matrix);
- if (state.using_skeleton) {
- state.canvas_shader.set_uniform(CanvasShaderGLES3::SKELETON_TRANSFORM, state.skeleton_transform);
- state.canvas_shader.set_uniform(CanvasShaderGLES3::SKELETON_TRANSFORM_INVERSE, state.skeleton_transform_inverse);
- }
- if (storage->frame.current_rt) {
- state.canvas_shader.set_uniform(CanvasShaderGLES3::SCREEN_PIXEL_SIZE, Vector2(1.0 / storage->frame.current_rt->width, 1.0 / storage->frame.current_rt->height));
- } else {
- state.canvas_shader.set_uniform(CanvasShaderGLES3::SCREEN_PIXEL_SIZE, Vector2(1.0, 1.0));
- }
- state.using_texture_rect = p_enable;
- state.using_ninepatch = p_ninepatch;
- state.using_light_angle = p_light_angle;
-}
-
-void RasterizerCanvasGLES3::_draw_polygon(const int *p_indices, int p_index_count, int p_vertex_count, const Vector2 *p_vertices, const Vector2 *p_uvs, const Color *p_colors, bool p_singlecolor, const int *p_bones, const float *p_weights) {
-
- glBindVertexArray(data.polygon_buffer_pointer_array);
- glBindBuffer(GL_ARRAY_BUFFER, data.polygon_buffer);
-
- uint32_t buffer_ofs = 0;
- storage->buffer_orphan_and_upload(data.polygon_buffer_size, buffer_ofs, sizeof(Vector2) * p_vertex_count, p_vertices);
-
- glEnableVertexAttribArray(VS::ARRAY_VERTEX);
- glVertexAttribPointer(VS::ARRAY_VERTEX, 2, GL_FLOAT, false, sizeof(Vector2), CAST_INT_TO_UCHAR_PTR(buffer_ofs));
- buffer_ofs += sizeof(Vector2) * p_vertex_count;
- //color
-#ifdef DEBUG_ENABLED
- ERR_FAIL_COND(buffer_ofs > data.polygon_buffer_size);
-#endif
-
- if (p_singlecolor) {
- glDisableVertexAttribArray(VS::ARRAY_COLOR);
- Color m = *p_colors;
- glVertexAttrib4f(VS::ARRAY_COLOR, m.r, m.g, m.b, m.a);
- } else if (!p_colors) {
- glDisableVertexAttribArray(VS::ARRAY_COLOR);
- glVertexAttrib4f(VS::ARRAY_COLOR, 1, 1, 1, 1);
- } else {
-
- glBufferSubData(GL_ARRAY_BUFFER, buffer_ofs, sizeof(Color) * p_vertex_count, p_colors);
- glEnableVertexAttribArray(VS::ARRAY_COLOR);
- glVertexAttribPointer(VS::ARRAY_COLOR, 4, GL_FLOAT, false, sizeof(Color), CAST_INT_TO_UCHAR_PTR(buffer_ofs));
- buffer_ofs += sizeof(Color) * p_vertex_count;
- }
-
-#ifdef DEBUG_ENABLED
- ERR_FAIL_COND(buffer_ofs > data.polygon_buffer_size);
-#endif
-
- if (p_uvs) {
-
- glBufferSubData(GL_ARRAY_BUFFER, buffer_ofs, sizeof(Vector2) * p_vertex_count, p_uvs);
- glEnableVertexAttribArray(VS::ARRAY_TEX_UV);
- glVertexAttribPointer(VS::ARRAY_TEX_UV, 2, GL_FLOAT, false, sizeof(Vector2), CAST_INT_TO_UCHAR_PTR(buffer_ofs));
- buffer_ofs += sizeof(Vector2) * p_vertex_count;
-
- } else {
- glDisableVertexAttribArray(VS::ARRAY_TEX_UV);
- }
-
-#ifdef DEBUG_ENABLED
- ERR_FAIL_COND(buffer_ofs > data.polygon_buffer_size);
-#endif
-
- if (p_bones && p_weights) {
-
- glBufferSubData(GL_ARRAY_BUFFER, buffer_ofs, sizeof(int) * 4 * p_vertex_count, p_bones);
- glEnableVertexAttribArray(VS::ARRAY_BONES);
- //glVertexAttribPointer(VS::ARRAY_BONES, 4, GL_UNSIGNED_INT, false, sizeof(int) * 4, ((uint8_t *)0) + buffer_ofs);
- glVertexAttribIPointer(VS::ARRAY_BONES, 4, GL_UNSIGNED_INT, sizeof(int) * 4, CAST_INT_TO_UCHAR_PTR(buffer_ofs));
- buffer_ofs += sizeof(int) * 4 * p_vertex_count;
-
- glBufferSubData(GL_ARRAY_BUFFER, buffer_ofs, sizeof(float) * 4 * p_vertex_count, p_weights);
- glEnableVertexAttribArray(VS::ARRAY_WEIGHTS);
- glVertexAttribPointer(VS::ARRAY_WEIGHTS, 4, GL_FLOAT, false, sizeof(float) * 4, CAST_INT_TO_UCHAR_PTR(buffer_ofs));
- buffer_ofs += sizeof(float) * 4 * p_vertex_count;
-
- } else if (state.using_skeleton) {
- glVertexAttribI4ui(VS::ARRAY_BONES, 0, 0, 0, 0);
- glVertexAttrib4f(VS::ARRAY_WEIGHTS, 0, 0, 0, 0);
- }
-
-#ifdef DEBUG_ENABLED
- ERR_FAIL_COND(buffer_ofs > data.polygon_buffer_size);
-#endif
-
- //bind the indices buffer.
- glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, data.polygon_index_buffer);
- storage->buffer_orphan_and_upload(data.polygon_index_buffer_size, 0, sizeof(int) * p_index_count, p_indices, GL_ELEMENT_ARRAY_BUFFER);
-
- //draw the triangles.
- glDrawElements(GL_TRIANGLES, p_index_count, GL_UNSIGNED_INT, 0);
-
- storage->info.render._2d_draw_call_count++;
-
- if (p_bones && p_weights) {
- //not used so often, so disable when used
- glDisableVertexAttribArray(VS::ARRAY_BONES);
- glDisableVertexAttribArray(VS::ARRAY_WEIGHTS);
- }
-
- glBindVertexArray(0);
- glBindBuffer(GL_ARRAY_BUFFER, 0);
-}
-
-void RasterizerCanvasGLES3::_draw_generic(GLuint p_primitive, int p_vertex_count, const Vector2 *p_vertices, const Vector2 *p_uvs, const Color *p_colors, bool p_singlecolor) {
-
- glBindVertexArray(data.polygon_buffer_pointer_array);
- glBindBuffer(GL_ARRAY_BUFFER, data.polygon_buffer);
-
- uint32_t buffer_ofs = 0;
- storage->buffer_orphan_and_upload(data.polygon_buffer_size, buffer_ofs, sizeof(Vector2) * p_vertex_count, p_vertices);
-
- glEnableVertexAttribArray(VS::ARRAY_VERTEX);
- glVertexAttribPointer(VS::ARRAY_VERTEX, 2, GL_FLOAT, false, sizeof(Vector2), CAST_INT_TO_UCHAR_PTR(buffer_ofs));
- buffer_ofs += sizeof(Vector2) * p_vertex_count;
- //color
-
- if (p_singlecolor) {
- glDisableVertexAttribArray(VS::ARRAY_COLOR);
- Color m = *p_colors;
- glVertexAttrib4f(VS::ARRAY_COLOR, m.r, m.g, m.b, m.a);
- } else if (!p_colors) {
- glDisableVertexAttribArray(VS::ARRAY_COLOR);
- glVertexAttrib4f(VS::ARRAY_COLOR, 1, 1, 1, 1);
- } else {
-
- glBufferSubData(GL_ARRAY_BUFFER, buffer_ofs, sizeof(Color) * p_vertex_count, p_colors);
- glEnableVertexAttribArray(VS::ARRAY_COLOR);
- glVertexAttribPointer(VS::ARRAY_COLOR, 4, GL_FLOAT, false, sizeof(Color), CAST_INT_TO_UCHAR_PTR(buffer_ofs));
- buffer_ofs += sizeof(Color) * p_vertex_count;
- }
-
- if (p_uvs) {
-
- glBufferSubData(GL_ARRAY_BUFFER, buffer_ofs, sizeof(Vector2) * p_vertex_count, p_uvs);
- glEnableVertexAttribArray(VS::ARRAY_TEX_UV);
- glVertexAttribPointer(VS::ARRAY_TEX_UV, 2, GL_FLOAT, false, sizeof(Vector2), CAST_INT_TO_UCHAR_PTR(buffer_ofs));
- buffer_ofs += sizeof(Vector2) * p_vertex_count;
-
- } else {
- glDisableVertexAttribArray(VS::ARRAY_TEX_UV);
- }
-
- glDrawArrays(p_primitive, 0, p_vertex_count);
-
- storage->info.render._2d_draw_call_count++;
-
- glBindVertexArray(0);
- glBindBuffer(GL_ARRAY_BUFFER, 0);
-}
-
-void RasterizerCanvasGLES3::_draw_generic_indices(GLuint p_primitive, const int *p_indices, int p_index_count, int p_vertex_count, const Vector2 *p_vertices, const Vector2 *p_uvs, const Color *p_colors, bool p_singlecolor) {
-
- glBindVertexArray(data.polygon_buffer_pointer_array);
- glBindBuffer(GL_ARRAY_BUFFER, data.polygon_buffer);
-
- uint32_t buffer_ofs = 0;
- storage->buffer_orphan_and_upload(data.polygon_buffer_size, buffer_ofs, sizeof(Vector2) * p_vertex_count, p_vertices);
-
- glEnableVertexAttribArray(VS::ARRAY_VERTEX);
- glVertexAttribPointer(VS::ARRAY_VERTEX, 2, GL_FLOAT, false, sizeof(Vector2), CAST_INT_TO_UCHAR_PTR(buffer_ofs));
- buffer_ofs += sizeof(Vector2) * p_vertex_count;
- //color
-#ifdef DEBUG_ENABLED
- ERR_FAIL_COND(buffer_ofs > data.polygon_buffer_size);
-#endif
-
- if (p_singlecolor) {
- glDisableVertexAttribArray(VS::ARRAY_COLOR);
- Color m = *p_colors;
- glVertexAttrib4f(VS::ARRAY_COLOR, m.r, m.g, m.b, m.a);
- } else if (!p_colors) {
- glDisableVertexAttribArray(VS::ARRAY_COLOR);
- glVertexAttrib4f(VS::ARRAY_COLOR, 1, 1, 1, 1);
- } else {
-
- glBufferSubData(GL_ARRAY_BUFFER, buffer_ofs, sizeof(Color) * p_vertex_count, p_colors);
- glEnableVertexAttribArray(VS::ARRAY_COLOR);
- glVertexAttribPointer(VS::ARRAY_COLOR, 4, GL_FLOAT, false, sizeof(Color), CAST_INT_TO_UCHAR_PTR(buffer_ofs));
- buffer_ofs += sizeof(Color) * p_vertex_count;
- }
-
-#ifdef DEBUG_ENABLED
- ERR_FAIL_COND(buffer_ofs > data.polygon_buffer_size);
-#endif
-
- if (p_uvs) {
-
- glBufferSubData(GL_ARRAY_BUFFER, buffer_ofs, sizeof(Vector2) * p_vertex_count, p_uvs);
- glEnableVertexAttribArray(VS::ARRAY_TEX_UV);
- glVertexAttribPointer(VS::ARRAY_TEX_UV, 2, GL_FLOAT, false, sizeof(Vector2), CAST_INT_TO_UCHAR_PTR(buffer_ofs));
- buffer_ofs += sizeof(Vector2) * p_vertex_count;
-
- } else {
- glDisableVertexAttribArray(VS::ARRAY_TEX_UV);
- }
-
-#ifdef DEBUG_ENABLED
- ERR_FAIL_COND(buffer_ofs > data.polygon_buffer_size);
-#endif
-
- //bind the indices buffer.
- glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, data.polygon_index_buffer);
-
- storage->buffer_orphan_and_upload(data.polygon_index_buffer_size, 0, sizeof(int) * p_index_count, p_indices, GL_ELEMENT_ARRAY_BUFFER);
-
- //draw the triangles.
- glDrawElements(p_primitive, p_index_count, GL_UNSIGNED_INT, 0);
-
- storage->info.render._2d_draw_call_count++;
-
- glBindVertexArray(0);
- glBindBuffer(GL_ARRAY_BUFFER, 0);
-}
-
-void RasterizerCanvasGLES3::_draw_gui_primitive(int p_points, const Vector2 *p_vertices, const Color *p_colors, const Vector2 *p_uvs, const float *p_light_angles) {
-
- static const GLenum prim[5] = { GL_POINTS, GL_POINTS, GL_LINES, GL_TRIANGLES, GL_TRIANGLE_FAN };
-
- //#define GLES_USE_PRIMITIVE_BUFFER
-
- int version = 0;
- int color_ofs = 0;
- int uv_ofs = 0;
- int light_angle_ofs = 0;
- int stride = 2;
-
- if (p_colors) { //color
- version |= 1;
- color_ofs = stride;
- stride += 4;
- }
-
- if (p_uvs) { //uv
- version |= 2;
- uv_ofs = stride;
- stride += 2;
- }
-
- if (p_light_angles) { //light_angles
- version |= 4;
- light_angle_ofs = stride;
- stride += 1;
- }
-
- float b[(2 + 2 + 4 + 1) * 4];
-
- for (int i = 0; i < p_points; i++) {
- b[stride * i + 0] = p_vertices[i].x;
- b[stride * i + 1] = p_vertices[i].y;
- }
-
- if (p_colors) {
-
- for (int i = 0; i < p_points; i++) {
- b[stride * i + color_ofs + 0] = p_colors[i].r;
- b[stride * i + color_ofs + 1] = p_colors[i].g;
- b[stride * i + color_ofs + 2] = p_colors[i].b;
- b[stride * i + color_ofs + 3] = p_colors[i].a;
- }
- }
-
- if (p_uvs) {
-
- for (int i = 0; i < p_points; i++) {
- b[stride * i + uv_ofs + 0] = p_uvs[i].x;
- b[stride * i + uv_ofs + 1] = p_uvs[i].y;
- }
- }
-
- if (p_light_angles) {
-
- for (int i = 0; i < p_points; i++) {
- b[stride * i + light_angle_ofs] = p_light_angles[i];
- }
- }
-
- glBindBuffer(GL_ARRAY_BUFFER, data.polygon_buffer);
- //TODO the below call may need to be replaced with: (p_points * stride * 4 * sizeof(float), &b[0]);
- storage->buffer_orphan_and_upload(data.polygon_buffer_size, 0, p_points * stride * 4, &b[0]);
-
- glBindVertexArray(data.polygon_buffer_quad_arrays[version]);
- glDrawArrays(prim[p_points], 0, p_points);
- glBindVertexArray(0);
- glBindBuffer(GL_ARRAY_BUFFER, 0);
-
- storage->info.render._2d_draw_call_count++;
-}
-
static const GLenum gl_primitive[] = {
GL_POINTS,
GL_LINES,
@@ -612,849 +41,1885 @@ static const GLenum gl_primitive[] = {
GL_TRIANGLE_FAN
};
-void RasterizerCanvasGLES3::render_rect_nvidia_workaround(const Item::CommandRect *p_rect, const RasterizerStorageGLES3::Texture *p_texture) {
+void RasterizerCanvasGLES3::canvas_end() {
+ batch_canvas_end();
+ RasterizerCanvasBaseGLES3::canvas_end();
+}
- if (p_texture) {
+void RasterizerCanvasGLES3::canvas_begin() {
+ batch_canvas_begin();
+ RasterizerCanvasBaseGLES3::canvas_begin();
+}
- bool send_light_angles = false;
+void RasterizerCanvasGLES3::canvas_render_items_begin(const Color &p_modulate, Light *p_light, const Transform2D &p_base_transform) {
+ batch_canvas_render_items_begin(p_modulate, p_light, p_base_transform);
+}
- // only need to use light angles when normal mapping
- // otherwise we can use the default shader
- if (state.current_normal != RID()) {
- send_light_angles = true;
- }
+void RasterizerCanvasGLES3::canvas_render_items_end() {
+ batch_canvas_render_items_end();
+}
- // we don't want to use texture rect, and we want to send light angles if we are using normal mapping
- _set_texture_rect_mode(false, false, send_light_angles);
+void RasterizerCanvasGLES3::canvas_render_items(Item *p_item_list, int p_z, const Color &p_modulate, Light *p_light, const Transform2D &p_base_transform) {
+ batch_canvas_render_items(p_item_list, p_z, p_modulate, p_light, p_base_transform);
+}
- bool untile = false;
+void RasterizerCanvasGLES3::gl_checkerror() {
+ GLenum e = glGetError();
+ CRASH_COND(e != GL_NO_ERROR);
+}
- if (p_rect->flags & CANVAS_RECT_TILE && !(p_texture->flags & VS::TEXTURE_FLAG_REPEAT)) {
- glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
- glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
- untile = true;
- }
+void RasterizerCanvasGLES3::gl_enable_scissor(int p_x, int p_y, int p_width, int p_height) const {
+ glEnable(GL_SCISSOR_TEST);
+ glScissor(p_x, p_y, p_width, p_height);
+}
- Size2 texpixel_size(1.0 / p_texture->width, 1.0 / p_texture->height);
+void RasterizerCanvasGLES3::gl_disable_scissor() const {
+ glDisable(GL_SCISSOR_TEST);
+}
- state.canvas_shader.set_uniform(CanvasShaderGLES3::CLIP_RECT_UV, p_rect->flags & CANVAS_RECT_CLIP_UV);
+// Legacy non-batched implementation for regression testing.
+// Should be removed after testing phase to avoid duplicate codepaths.
+void RasterizerCanvasGLES3::_legacy_canvas_render_item(Item *p_ci, RenderItemState &r_ris) {
+ storage->info.render._2d_item_count++;
- Vector2 points[4] = {
- p_rect->rect.position,
- p_rect->rect.position + Vector2(p_rect->rect.size.x, 0.0),
- p_rect->rect.position + p_rect->rect.size,
- p_rect->rect.position + Vector2(0.0, p_rect->rect.size.y),
- };
+ if (r_ris.prev_distance_field != p_ci->distance_field) {
- if (p_rect->rect.size.x < 0) {
- SWAP(points[0], points[1]);
- SWAP(points[2], points[3]);
- }
- if (p_rect->rect.size.y < 0) {
- SWAP(points[0], points[3]);
- SWAP(points[1], points[2]);
- }
- Rect2 src_rect = (p_rect->flags & CANVAS_RECT_REGION) ? Rect2(p_rect->source.position * texpixel_size, p_rect->source.size * texpixel_size) : Rect2(0, 0, 1, 1);
+ state.canvas_shader.set_conditional(CanvasShaderGLES3::USE_DISTANCE_FIELD, p_ci->distance_field);
+ r_ris.prev_distance_field = p_ci->distance_field;
+ r_ris.rebind_shader = true;
+ }
- Vector2 uvs[4] = {
- src_rect.position,
- src_rect.position + Vector2(src_rect.size.x, 0.0),
- src_rect.position + src_rect.size,
- src_rect.position + Vector2(0.0, src_rect.size.y),
- };
+ if (r_ris.current_clip != p_ci->final_clip_owner) {
- // for encoding in light angle
- bool flip_h = false;
- bool flip_v = false;
+ r_ris.current_clip = p_ci->final_clip_owner;
- if (p_rect->flags & CANVAS_RECT_TRANSPOSE) {
- SWAP(uvs[1], uvs[3]);
- }
+ //setup clip
+ if (r_ris.current_clip) {
- if (p_rect->flags & CANVAS_RECT_FLIP_H) {
- SWAP(uvs[0], uvs[1]);
- SWAP(uvs[2], uvs[3]);
- flip_h = true;
- flip_v = !flip_v;
- }
- if (p_rect->flags & CANVAS_RECT_FLIP_V) {
- SWAP(uvs[0], uvs[3]);
- SWAP(uvs[1], uvs[2]);
- flip_v = !flip_v;
- }
+ glEnable(GL_SCISSOR_TEST);
+ int y = storage->frame.current_rt->height - (r_ris.current_clip->final_clip_rect.position.y + r_ris.current_clip->final_clip_rect.size.y);
+ if (storage->frame.current_rt->flags[RasterizerStorage::RENDER_TARGET_VFLIP])
+ y = r_ris.current_clip->final_clip_rect.position.y;
- if (send_light_angles) {
- // for single rects, there is no need to fully utilize the light angle,
- // we only need it to encode flips (horz and vert). But the shader can be reused with
- // batching in which case the angle encodes the transform as well as
- // the flips.
- // Note transpose is NYI. I don't think it worked either with the non-nvidia method.
+ glScissor(r_ris.current_clip->final_clip_rect.position.x, y, r_ris.current_clip->final_clip_rect.size.x, r_ris.current_clip->final_clip_rect.size.y);
- // if horizontal flip, angle is 180
- float angle = 0.0f;
- if (flip_h)
- angle = Math_PI;
-
- // add 1 (to take care of zero floating point error with sign)
- angle += 1.0f;
-
- // flip if necessary
- if (flip_v)
- angle *= -1.0f;
-
- // light angle must be sent for each vert, instead as a single uniform in the uniform draw method
- // this has the benefit of enabling batching with light angles.
- float light_angles[4] = { angle, angle, angle, angle };
-
- _draw_gui_primitive(4, points, NULL, uvs, light_angles);
} else {
- _draw_gui_primitive(4, points, NULL, uvs);
+
+ glDisable(GL_SCISSOR_TEST);
+ }
+ }
+
+ if (p_ci->copy_back_buffer) {
+
+ if (p_ci->copy_back_buffer->full) {
+
+ _copy_texscreen(Rect2());
+ } else {
+ _copy_texscreen(p_ci->copy_back_buffer->rect);
+ }
+ }
+
+ RasterizerStorageGLES3::Skeleton *skeleton = NULL;
+
+ {
+ //skeleton handling
+ if (p_ci->skeleton.is_valid() && storage->skeleton_owner.owns(p_ci->skeleton)) {
+ skeleton = storage->skeleton_owner.get(p_ci->skeleton);
+ if (!skeleton->use_2d) {
+ skeleton = NULL;
+ } else {
+ state.skeleton_transform = r_ris.item_group_base_transform * skeleton->base_transform_2d;
+ state.skeleton_transform_inverse = state.skeleton_transform.affine_inverse();
+ }
}
- if (untile) {
- glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
- glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
+ bool use_skeleton = skeleton != NULL;
+ if (r_ris.prev_use_skeleton != use_skeleton) {
+ r_ris.rebind_shader = true;
+ state.canvas_shader.set_conditional(CanvasShaderGLES3::USE_SKELETON, use_skeleton);
+ r_ris.prev_use_skeleton = use_skeleton;
+ }
+
+ if (skeleton) {
+ glActiveTexture(GL_TEXTURE0 + storage->config.max_texture_image_units - 4);
+ glBindTexture(GL_TEXTURE_2D, skeleton->texture);
+ state.using_skeleton = true;
+ } else {
+ state.using_skeleton = false;
+ }
+ }
+
+ //begin rect
+ Item *material_owner = p_ci->material_owner ? p_ci->material_owner : p_ci;
+
+ RID material = material_owner->material;
+
+ if (material != r_ris.canvas_last_material || r_ris.rebind_shader) {
+
+ RasterizerStorageGLES3::Material *material_ptr = storage->material_owner.getornull(material);
+ RasterizerStorageGLES3::Shader *shader_ptr = NULL;
+
+ if (material_ptr) {
+
+ shader_ptr = material_ptr->shader;
+
+ if (shader_ptr && shader_ptr->mode != VS::SHADER_CANVAS_ITEM) {
+ shader_ptr = NULL; //do not use non canvasitem shader
+ }
+ }
+
+ if (shader_ptr) {
+
+ if (shader_ptr->canvas_item.uses_screen_texture && !state.canvas_texscreen_used) {
+ //copy if not copied before
+ _copy_texscreen(Rect2());
+
+ // blend mode will have been enabled so make sure we disable it again later on
+ r_ris.last_blend_mode = r_ris.last_blend_mode != RasterizerStorageGLES3::Shader::CanvasItem::BLEND_MODE_DISABLED ? r_ris.last_blend_mode : -1;
+ }
+
+ if (shader_ptr != r_ris.shader_cache || r_ris.rebind_shader) {
+
+ if (shader_ptr->canvas_item.uses_time) {
+ VisualServerRaster::redraw_request();
+ }
+
+ state.canvas_shader.set_custom_shader(shader_ptr->custom_code_id);
+ state.canvas_shader.bind();
+ }
+
+ if (material_ptr->ubo_id) {
+ glBindBufferBase(GL_UNIFORM_BUFFER, 2, material_ptr->ubo_id);
+ }
+
+ int tc = material_ptr->textures.size();
+ RID *textures = material_ptr->textures.ptrw();
+ ShaderLanguage::ShaderNode::Uniform::Hint *texture_hints = shader_ptr->texture_hints.ptrw();
+
+ for (int i = 0; i < tc; i++) {
+
+ glActiveTexture(GL_TEXTURE2 + i);
+
+ RasterizerStorageGLES3::Texture *t = storage->texture_owner.getornull(textures[i]);
+ if (!t) {
+
+ switch (texture_hints[i]) {
+ case ShaderLanguage::ShaderNode::Uniform::HINT_BLACK_ALBEDO:
+ case ShaderLanguage::ShaderNode::Uniform::HINT_BLACK: {
+ glBindTexture(GL_TEXTURE_2D, storage->resources.black_tex);
+ } break;
+ case ShaderLanguage::ShaderNode::Uniform::HINT_ANISO: {
+ glBindTexture(GL_TEXTURE_2D, storage->resources.aniso_tex);
+ } break;
+ case ShaderLanguage::ShaderNode::Uniform::HINT_NORMAL: {
+ glBindTexture(GL_TEXTURE_2D, storage->resources.normal_tex);
+ } break;
+ default: {
+ glBindTexture(GL_TEXTURE_2D, storage->resources.white_tex);
+ } break;
+ }
+
+ //check hints
+
+ continue;
+ }
+
+ if (t->redraw_if_visible) { //check before proxy, because this is usually used with proxies
+ VisualServerRaster::redraw_request();
+ }
+
+ t = t->get_ptr();
+
+ if (storage->config.srgb_decode_supported && t->using_srgb) {
+ //no srgb in 2D
+ glTexParameteri(t->target, _TEXTURE_SRGB_DECODE_EXT, _SKIP_DECODE_EXT);
+ t->using_srgb = false;
+ }
+
+ glBindTexture(t->target, t->tex_id);
+ }
+
+ } else {
+ state.canvas_shader.set_custom_shader(0);
+ state.canvas_shader.bind();
+ }
+
+ r_ris.shader_cache = shader_ptr;
+
+ r_ris.canvas_last_material = material;
+ r_ris.rebind_shader = false;
+ }
+
+ int blend_mode = r_ris.shader_cache ? r_ris.shader_cache->canvas_item.blend_mode : RasterizerStorageGLES3::Shader::CanvasItem::BLEND_MODE_MIX;
+ if (blend_mode == RasterizerStorageGLES3::Shader::CanvasItem::BLEND_MODE_DISABLED && (!storage->frame.current_rt || !storage->frame.current_rt->flags[RasterizerStorage::RENDER_TARGET_TRANSPARENT])) {
+ blend_mode = RasterizerStorageGLES3::Shader::CanvasItem::BLEND_MODE_MIX;
+ }
+ bool unshaded = r_ris.shader_cache && (r_ris.shader_cache->canvas_item.light_mode == RasterizerStorageGLES3::Shader::CanvasItem::LIGHT_MODE_UNSHADED || (blend_mode != RasterizerStorageGLES3::Shader::CanvasItem::BLEND_MODE_MIX && blend_mode != RasterizerStorageGLES3::Shader::CanvasItem::BLEND_MODE_PMALPHA));
+ bool reclip = false;
+
+ if (r_ris.last_blend_mode != blend_mode) {
+ if (r_ris.last_blend_mode == RasterizerStorageGLES3::Shader::CanvasItem::BLEND_MODE_DISABLED) {
+ // re-enable it
+ glEnable(GL_BLEND);
+ } else if (blend_mode == RasterizerStorageGLES3::Shader::CanvasItem::BLEND_MODE_DISABLED) {
+ // disable it
+ glDisable(GL_BLEND);
+ }
+
+ switch (blend_mode) {
+
+ case RasterizerStorageGLES3::Shader::CanvasItem::BLEND_MODE_DISABLED: {
+
+ // nothing to do here
+
+ } break;
+ case RasterizerStorageGLES3::Shader::CanvasItem::BLEND_MODE_MIX: {
+
+ glBlendEquation(GL_FUNC_ADD);
+ if (storage->frame.current_rt && storage->frame.current_rt->flags[RasterizerStorage::RENDER_TARGET_TRANSPARENT]) {
+ glBlendFuncSeparate(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA, GL_ONE, GL_ONE_MINUS_SRC_ALPHA);
+ } else {
+ glBlendFuncSeparate(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA, GL_ZERO, GL_ONE);
+ }
+
+ } break;
+ case RasterizerStorageGLES3::Shader::CanvasItem::BLEND_MODE_ADD: {
+
+ glBlendEquation(GL_FUNC_ADD);
+ if (storage->frame.current_rt && storage->frame.current_rt->flags[RasterizerStorage::RENDER_TARGET_TRANSPARENT]) {
+ glBlendFuncSeparate(GL_SRC_ALPHA, GL_ONE, GL_SRC_ALPHA, GL_ONE);
+ } else {
+ glBlendFuncSeparate(GL_SRC_ALPHA, GL_ONE, GL_ZERO, GL_ONE);
+ }
+
+ } break;
+ case RasterizerStorageGLES3::Shader::CanvasItem::BLEND_MODE_SUB: {
+
+ glBlendEquation(GL_FUNC_REVERSE_SUBTRACT);
+ if (storage->frame.current_rt && storage->frame.current_rt->flags[RasterizerStorage::RENDER_TARGET_TRANSPARENT]) {
+ glBlendFuncSeparate(GL_SRC_ALPHA, GL_ONE, GL_SRC_ALPHA, GL_ONE);
+ } else {
+ glBlendFuncSeparate(GL_SRC_ALPHA, GL_ONE, GL_ZERO, GL_ONE);
+ }
+ } break;
+ case RasterizerStorageGLES3::Shader::CanvasItem::BLEND_MODE_MUL: {
+ glBlendEquation(GL_FUNC_ADD);
+ if (storage->frame.current_rt && storage->frame.current_rt->flags[RasterizerStorage::RENDER_TARGET_TRANSPARENT]) {
+ glBlendFuncSeparate(GL_DST_COLOR, GL_ZERO, GL_DST_ALPHA, GL_ZERO);
+ } else {
+ glBlendFuncSeparate(GL_DST_COLOR, GL_ZERO, GL_ZERO, GL_ONE);
+ }
+
+ } break;
+ case RasterizerStorageGLES3::Shader::CanvasItem::BLEND_MODE_PMALPHA: {
+ glBlendEquation(GL_FUNC_ADD);
+ if (storage->frame.current_rt && storage->frame.current_rt->flags[RasterizerStorage::RENDER_TARGET_TRANSPARENT]) {
+ glBlendFuncSeparate(GL_ONE, GL_ONE_MINUS_SRC_ALPHA, GL_ONE, GL_ONE_MINUS_SRC_ALPHA);
+ } else {
+ glBlendFuncSeparate(GL_ONE, GL_ONE_MINUS_SRC_ALPHA, GL_ZERO, GL_ONE);
+ }
+
+ } break;
+ }
+
+ r_ris.last_blend_mode = blend_mode;
+ }
+
+ state.canvas_item_modulate = unshaded ? p_ci->final_modulate : Color(p_ci->final_modulate.r * r_ris.item_group_modulate.r, p_ci->final_modulate.g * r_ris.item_group_modulate.g, p_ci->final_modulate.b * r_ris.item_group_modulate.b, p_ci->final_modulate.a * r_ris.item_group_modulate.a);
+
+ state.final_transform = p_ci->final_transform;
+ state.extra_matrix = Transform2D();
+
+ if (state.using_skeleton) {
+ state.canvas_shader.set_uniform(CanvasShaderGLES3::SKELETON_TRANSFORM, state.skeleton_transform);
+ state.canvas_shader.set_uniform(CanvasShaderGLES3::SKELETON_TRANSFORM_INVERSE, state.skeleton_transform_inverse);
+ }
+
+ state.canvas_shader.set_uniform(CanvasShaderGLES3::FINAL_MODULATE, state.canvas_item_modulate);
+ state.canvas_shader.set_uniform(CanvasShaderGLES3::MODELVIEW_MATRIX, state.final_transform);
+ state.canvas_shader.set_uniform(CanvasShaderGLES3::EXTRA_MATRIX, state.extra_matrix);
+ if (storage->frame.current_rt) {
+ state.canvas_shader.set_uniform(CanvasShaderGLES3::SCREEN_PIXEL_SIZE, Vector2(1.0 / storage->frame.current_rt->width, 1.0 / storage->frame.current_rt->height));
+ } else {
+ state.canvas_shader.set_uniform(CanvasShaderGLES3::SCREEN_PIXEL_SIZE, Vector2(1.0, 1.0));
+ }
+ if (unshaded || (state.canvas_item_modulate.a > 0.001 && (!r_ris.shader_cache || r_ris.shader_cache->canvas_item.light_mode != RasterizerStorageGLES3::Shader::CanvasItem::LIGHT_MODE_LIGHT_ONLY) && !p_ci->light_masked))
+ _legacy_canvas_item_render_commands(p_ci, r_ris.current_clip, reclip, nullptr);
+
+ if ((blend_mode == RasterizerStorageGLES3::Shader::CanvasItem::BLEND_MODE_MIX || blend_mode == RasterizerStorageGLES3::Shader::CanvasItem::BLEND_MODE_PMALPHA) && r_ris.item_group_light && !unshaded) {
+
+ Light *light = r_ris.item_group_light;
+ bool light_used = false;
+ VS::CanvasLightMode mode = VS::CANVAS_LIGHT_MODE_ADD;
+ state.canvas_item_modulate = p_ci->final_modulate; // remove the canvas modulate
+
+ while (light) {
+
+ if (p_ci->light_mask & light->item_mask && r_ris.item_group_z >= light->z_min && r_ris.item_group_z <= light->z_max && p_ci->global_rect_cache.intersects_transformed(light->xform_cache, light->rect_cache)) {
+
+ //intersects this light
+
+ if (!light_used || mode != light->mode) {
+
+ mode = light->mode;
+
+ switch (mode) {
+
+ case VS::CANVAS_LIGHT_MODE_ADD: {
+ glBlendEquation(GL_FUNC_ADD);
+ glBlendFunc(GL_SRC_ALPHA, GL_ONE);
+
+ } break;
+ case VS::CANVAS_LIGHT_MODE_SUB: {
+ glBlendEquation(GL_FUNC_REVERSE_SUBTRACT);
+ glBlendFunc(GL_SRC_ALPHA, GL_ONE);
+ } break;
+ case VS::CANVAS_LIGHT_MODE_MIX:
+ case VS::CANVAS_LIGHT_MODE_MASK: {
+ glBlendEquation(GL_FUNC_ADD);
+ glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
+
+ } break;
+ }
+ }
+
+ if (!light_used) {
+
+ state.canvas_shader.set_conditional(CanvasShaderGLES3::USE_LIGHTING, true);
+ light_used = true;
+ }
+
+ bool has_shadow = light->shadow_buffer.is_valid() && p_ci->light_mask & light->item_shadow_mask;
+
+ state.canvas_shader.set_conditional(CanvasShaderGLES3::USE_SHADOWS, has_shadow);
+ if (has_shadow) {
+ state.canvas_shader.set_conditional(CanvasShaderGLES3::SHADOW_USE_GRADIENT, light->shadow_gradient_length > 0);
+ state.canvas_shader.set_conditional(CanvasShaderGLES3::SHADOW_FILTER_NEAREST, light->shadow_filter == VS::CANVAS_LIGHT_FILTER_NONE);
+ state.canvas_shader.set_conditional(CanvasShaderGLES3::SHADOW_FILTER_PCF3, light->shadow_filter == VS::CANVAS_LIGHT_FILTER_PCF3);
+ state.canvas_shader.set_conditional(CanvasShaderGLES3::SHADOW_FILTER_PCF5, light->shadow_filter == VS::CANVAS_LIGHT_FILTER_PCF5);
+ state.canvas_shader.set_conditional(CanvasShaderGLES3::SHADOW_FILTER_PCF7, light->shadow_filter == VS::CANVAS_LIGHT_FILTER_PCF7);
+ state.canvas_shader.set_conditional(CanvasShaderGLES3::SHADOW_FILTER_PCF9, light->shadow_filter == VS::CANVAS_LIGHT_FILTER_PCF9);
+ state.canvas_shader.set_conditional(CanvasShaderGLES3::SHADOW_FILTER_PCF13, light->shadow_filter == VS::CANVAS_LIGHT_FILTER_PCF13);
+ }
+
+ bool light_rebind = state.canvas_shader.bind();
+
+ if (light_rebind) {
+ state.canvas_shader.set_uniform(CanvasShaderGLES3::FINAL_MODULATE, state.canvas_item_modulate);
+ state.canvas_shader.set_uniform(CanvasShaderGLES3::MODELVIEW_MATRIX, state.final_transform);
+ state.canvas_shader.set_uniform(CanvasShaderGLES3::EXTRA_MATRIX, Transform2D());
+ if (storage->frame.current_rt) {
+ state.canvas_shader.set_uniform(CanvasShaderGLES3::SCREEN_PIXEL_SIZE, Vector2(1.0 / storage->frame.current_rt->width, 1.0 / storage->frame.current_rt->height));
+ } else {
+ state.canvas_shader.set_uniform(CanvasShaderGLES3::SCREEN_PIXEL_SIZE, Vector2(1.0, 1.0));
+ }
+ if (state.using_skeleton) {
+ state.canvas_shader.set_uniform(CanvasShaderGLES3::SKELETON_TRANSFORM, state.skeleton_transform);
+ state.canvas_shader.set_uniform(CanvasShaderGLES3::SKELETON_TRANSFORM_INVERSE, state.skeleton_transform_inverse);
+ }
+ }
+
+ glBindBufferBase(GL_UNIFORM_BUFFER, 1, static_cast(light->light_internal.get_data())->ubo);
+
+ if (has_shadow) {
+
+ RasterizerStorageGLES3::CanvasLightShadow *cls = storage->canvas_light_shadow_owner.get(light->shadow_buffer);
+ glActiveTexture(GL_TEXTURE0 + storage->config.max_texture_image_units - 2);
+ glBindTexture(GL_TEXTURE_2D, cls->distance);
+
+ /*canvas_shader.set_uniform(CanvasShaderGLES3::SHADOW_MATRIX,light->shadow_matrix_cache);
+ canvas_shader.set_uniform(CanvasShaderGLES3::SHADOW_ESM_MULTIPLIER,light->shadow_esm_mult);
+ canvas_shader.set_uniform(CanvasShaderGLES3::LIGHT_SHADOW_COLOR,light->shadow_color);*/
+ }
+
+ glActiveTexture(GL_TEXTURE0 + storage->config.max_texture_image_units - 1);
+ RasterizerStorageGLES3::Texture *t = storage->texture_owner.getornull(light->texture);
+ if (!t) {
+ glBindTexture(GL_TEXTURE_2D, storage->resources.white_tex);
+ } else {
+ t = t->get_ptr();
+
+ glBindTexture(t->target, t->tex_id);
+ }
+
+ glActiveTexture(GL_TEXTURE0);
+ _legacy_canvas_item_render_commands(p_ci, r_ris.current_clip, reclip, nullptr); //redraw using light
+ }
+
+ light = light->next_ptr;
+ }
+
+ if (light_used) {
+
+ state.canvas_shader.set_conditional(CanvasShaderGLES3::USE_LIGHTING, false);
+ state.canvas_shader.set_conditional(CanvasShaderGLES3::USE_SHADOWS, false);
+ state.canvas_shader.set_conditional(CanvasShaderGLES3::SHADOW_FILTER_NEAREST, false);
+ state.canvas_shader.set_conditional(CanvasShaderGLES3::SHADOW_FILTER_PCF3, false);
+ state.canvas_shader.set_conditional(CanvasShaderGLES3::SHADOW_FILTER_PCF5, false);
+ state.canvas_shader.set_conditional(CanvasShaderGLES3::SHADOW_FILTER_PCF7, false);
+ state.canvas_shader.set_conditional(CanvasShaderGLES3::SHADOW_FILTER_PCF9, false);
+ state.canvas_shader.set_conditional(CanvasShaderGLES3::SHADOW_FILTER_PCF13, false);
+
+ state.canvas_shader.bind();
+
+ r_ris.last_blend_mode = -1;
+
+ /*
+ //this is set again, so it should not be needed anyway?
+ state.canvas_item_modulate = unshaded ? ci->final_modulate : Color(
+ ci->final_modulate.r * p_modulate.r,
+ ci->final_modulate.g * p_modulate.g,
+ ci->final_modulate.b * p_modulate.b,
+ ci->final_modulate.a * p_modulate.a );
+
+
+ state.canvas_shader.set_uniform(CanvasShaderGLES3::MODELVIEW_MATRIX,state.final_transform);
+ state.canvas_shader.set_uniform(CanvasShaderGLES3::EXTRA_MATRIX,Transform2D());
+ state.canvas_shader.set_uniform(CanvasShaderGLES3::FINAL_MODULATE,state.canvas_item_modulate);
+
+ glBlendEquation(GL_FUNC_ADD);
+
+ if (storage->frame.current_rt->flags[RasterizerStorage::RENDER_TARGET_TRANSPARENT]) {
+ glBlendFuncSeparate(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA, GL_ONE, GL_ONE_MINUS_SRC_ALPHA);
+ } else {
+ glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
+ }
+
+ //@TODO RESET canvas_blend_mode
+ */
+ }
+ }
+
+ if (reclip) {
+
+ glEnable(GL_SCISSOR_TEST);
+ int y = storage->frame.current_rt->height - (r_ris.current_clip->final_clip_rect.position.y + r_ris.current_clip->final_clip_rect.size.y);
+ if (storage->frame.current_rt->flags[RasterizerStorage::RENDER_TARGET_VFLIP])
+ y = r_ris.current_clip->final_clip_rect.position.y;
+ glScissor(r_ris.current_clip->final_clip_rect.position.x, y, r_ris.current_clip->final_clip_rect.size.width, r_ris.current_clip->final_clip_rect.size.height);
+ }
+}
+
+void RasterizerCanvasGLES3::render_batches(Item::Command *const *p_commands, Item *p_current_clip, bool &r_reclip, RasterizerStorageGLES3::Material *p_material) {
+ // bdata.reset_flush();
+ // return;
+
+ int num_batches = bdata.batches.size();
+
+ for (int batch_num = 0; batch_num < num_batches; batch_num++) {
+ const Batch &batch = bdata.batches[batch_num];
+
+ switch (batch.type) {
+ case RasterizerStorageCommon::BT_RECT: {
+ _batch_render_rects(batch, p_material);
+ } break;
+ case RasterizerStorageCommon::BT_POLY: {
+ _batch_render_polys(batch, p_material);
+ } break;
+ case RasterizerStorageCommon::BT_LINE: {
+ _batch_render_lines(batch, p_material, false);
+ } break;
+ case RasterizerStorageCommon::BT_LINE_AA: {
+ _batch_render_lines(batch, p_material, true);
+ } break;
+ default: {
+ int end_command = batch.first_command + batch.num_commands;
+
+ for (int i = batch.first_command; i < end_command; i++) {
+
+ Item::Command *c = p_commands[i];
+
+ switch (c->type) {
+
+ case Item::Command::TYPE_LINE: {
+
+ Item::CommandLine *line = static_cast(c);
+ _set_texture_rect_mode(false);
+
+ _bind_canvas_texture(RID(), RID());
+
+ glVertexAttrib4f(VS::ARRAY_COLOR, line->color.r, line->color.g, line->color.b, line->color.a);
+
+ if (line->width <= 1) {
+ Vector2 verts[2] = {
+ Vector2(line->from.x, line->from.y),
+ Vector2(line->to.x, line->to.y)
+ };
+
+#ifdef GLES_OVER_GL
+ if (line->antialiased)
+ glEnable(GL_LINE_SMOOTH);
+#endif
+ //glLineWidth(line->width);
+ _draw_gui_primitive(2, verts, NULL, NULL);
+
+#ifdef GLES_OVER_GL
+ if (line->antialiased)
+ glDisable(GL_LINE_SMOOTH);
+#endif
+ } else {
+ //thicker line
+
+ Vector2 t = (line->from - line->to).normalized().tangent() * line->width * 0.5;
+
+ Vector2 verts[4] = {
+ line->from - t,
+ line->from + t,
+ line->to + t,
+ line->to - t,
+ };
+
+ //glLineWidth(line->width);
+ _draw_gui_primitive(4, verts, NULL, NULL);
+#ifdef GLES_OVER_GL
+ if (line->antialiased) {
+ glEnable(GL_LINE_SMOOTH);
+ for (int j = 0; j < 4; j++) {
+ Vector2 vertsl[2] = {
+ verts[j],
+ verts[(j + 1) % 4],
+ };
+ _draw_gui_primitive(2, vertsl, NULL, NULL);
+ }
+ glDisable(GL_LINE_SMOOTH);
+ }
+#endif
+ }
+ } break;
+ case Item::Command::TYPE_POLYLINE: {
+
+ Item::CommandPolyLine *pline = static_cast(c);
+ _set_texture_rect_mode(false);
+
+ _bind_canvas_texture(RID(), RID());
+
+ if (pline->triangles.size()) {
+
+ _draw_generic(GL_TRIANGLE_STRIP, pline->triangles.size(), pline->triangles.ptr(), NULL, pline->triangle_colors.ptr(), pline->triangle_colors.size() == 1);
+#ifdef GLES_OVER_GL
+ glEnable(GL_LINE_SMOOTH);
+ if (pline->multiline) {
+ //needs to be different
+ } else {
+ _draw_generic(GL_LINE_LOOP, pline->lines.size(), pline->lines.ptr(), NULL, pline->line_colors.ptr(), pline->line_colors.size() == 1);
+ }
+ glDisable(GL_LINE_SMOOTH);
+#endif
+ } else {
+
+#ifdef GLES_OVER_GL
+ if (pline->antialiased)
+ glEnable(GL_LINE_SMOOTH);
+#endif
+
+ if (pline->multiline) {
+ int todo = pline->lines.size() / 2;
+ int max_per_call = data.polygon_buffer_size / (sizeof(real_t) * 4);
+ int offset = 0;
+
+ while (todo) {
+ int to_draw = MIN(max_per_call, todo);
+ _draw_generic(GL_LINES, to_draw * 2, &pline->lines.ptr()[offset], NULL, pline->line_colors.size() == 1 ? pline->line_colors.ptr() : &pline->line_colors.ptr()[offset], pline->line_colors.size() == 1);
+ todo -= to_draw;
+ offset += to_draw * 2;
+ }
+
+ } else {
+
+ _draw_generic(GL_LINE_STRIP, pline->lines.size(), pline->lines.ptr(), NULL, pline->line_colors.ptr(), pline->line_colors.size() == 1);
+ }
+
+#ifdef GLES_OVER_GL
+ if (pline->antialiased)
+ glDisable(GL_LINE_SMOOTH);
+#endif
+ }
+
+ } break;
+ case Item::Command::TYPE_RECT: {
+
+ Item::CommandRect *rect = static_cast(c);
+
+ //set color
+ glVertexAttrib4f(VS::ARRAY_COLOR, rect->modulate.r, rect->modulate.g, rect->modulate.b, rect->modulate.a);
+
+ RasterizerStorageGLES3::Texture *texture = _bind_canvas_texture(rect->texture, rect->normal_map);
+
+ if (use_nvidia_rect_workaround) {
+ render_rect_nvidia_workaround(rect, texture);
+ } else {
+
+ _set_texture_rect_mode(true);
+
+ if (texture) {
+
+ bool untile = false;
+
+ if (rect->flags & CANVAS_RECT_TILE && !(texture->flags & VS::TEXTURE_FLAG_REPEAT)) {
+ glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
+ glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
+ untile = true;
+ }
+
+ Size2 texpixel_size(1.0 / texture->width, 1.0 / texture->height);
+ Rect2 src_rect = (rect->flags & CANVAS_RECT_REGION) ? Rect2(rect->source.position * texpixel_size, rect->source.size * texpixel_size) : Rect2(0, 0, 1, 1);
+ Rect2 dst_rect = Rect2(rect->rect.position, rect->rect.size);
+
+ if (dst_rect.size.width < 0) {
+ dst_rect.position.x += dst_rect.size.width;
+ dst_rect.size.width *= -1;
+ }
+ if (dst_rect.size.height < 0) {
+ dst_rect.position.y += dst_rect.size.height;
+ dst_rect.size.height *= -1;
+ }
+
+ if (rect->flags & CANVAS_RECT_FLIP_H) {
+ src_rect.size.x *= -1;
+ }
+
+ if (rect->flags & CANVAS_RECT_FLIP_V) {
+ src_rect.size.y *= -1;
+ }
+
+ if (rect->flags & CANVAS_RECT_TRANSPOSE) {
+ dst_rect.size.x *= -1; // Encoding in the dst_rect.z uniform
+ }
+
+ state.canvas_shader.set_uniform(CanvasShaderGLES3::COLOR_TEXPIXEL_SIZE, texpixel_size);
+
+ state.canvas_shader.set_uniform(CanvasShaderGLES3::DST_RECT, Color(dst_rect.position.x, dst_rect.position.y, dst_rect.size.x, dst_rect.size.y));
+ state.canvas_shader.set_uniform(CanvasShaderGLES3::SRC_RECT, Color(src_rect.position.x, src_rect.position.y, src_rect.size.x, src_rect.size.y));
+ state.canvas_shader.set_uniform(CanvasShaderGLES3::CLIP_RECT_UV, rect->flags & CANVAS_RECT_CLIP_UV);
+
+ glDrawArrays(GL_TRIANGLE_FAN, 0, 4);
+ storage->info.render._2d_draw_call_count++;
+
+ if (untile) {
+ glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
+ glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
+ }
+
+ } else {
+ Rect2 dst_rect = Rect2(rect->rect.position, rect->rect.size);
+
+ if (dst_rect.size.width < 0) {
+ dst_rect.position.x += dst_rect.size.width;
+ dst_rect.size.width *= -1;
+ }
+ if (dst_rect.size.height < 0) {
+ dst_rect.position.y += dst_rect.size.height;
+ dst_rect.size.height *= -1;
+ }
+
+ state.canvas_shader.set_uniform(CanvasShaderGLES3::DST_RECT, Color(dst_rect.position.x, dst_rect.position.y, dst_rect.size.x, dst_rect.size.y));
+ state.canvas_shader.set_uniform(CanvasShaderGLES3::SRC_RECT, Color(0, 0, 1, 1));
+ state.canvas_shader.set_uniform(CanvasShaderGLES3::CLIP_RECT_UV, false);
+ glDrawArrays(GL_TRIANGLE_FAN, 0, 4);
+ storage->info.render._2d_draw_call_count++;
+ }
+ } // if not use nvidia workaround
+ } break;
+ case Item::Command::TYPE_NINEPATCH: {
+
+ Item::CommandNinePatch *np = static_cast(c);
+
+ _set_texture_rect_mode(true, true);
+
+ glVertexAttrib4f(VS::ARRAY_COLOR, np->color.r, np->color.g, np->color.b, np->color.a);
+
+ RasterizerStorageGLES3::Texture *texture = _bind_canvas_texture(np->texture, np->normal_map);
+
+ Size2 texpixel_size;
+
+ if (!texture) {
+
+ texpixel_size = Size2(1, 1);
+
+ state.canvas_shader.set_uniform(CanvasShaderGLES3::SRC_RECT, Color(0, 0, 1, 1));
+
+ } else {
+
+ if (np->source != Rect2()) {
+ texpixel_size = Size2(1.0 / np->source.size.width, 1.0 / np->source.size.height);
+ state.canvas_shader.set_uniform(CanvasShaderGLES3::SRC_RECT, Color(np->source.position.x / texture->width, np->source.position.y / texture->height, np->source.size.x / texture->width, np->source.size.y / texture->height));
+ } else {
+ texpixel_size = Size2(1.0 / texture->width, 1.0 / texture->height);
+ state.canvas_shader.set_uniform(CanvasShaderGLES3::SRC_RECT, Color(0, 0, 1, 1));
+ }
+ }
+
+ state.canvas_shader.set_uniform(CanvasShaderGLES3::COLOR_TEXPIXEL_SIZE, texpixel_size);
+ state.canvas_shader.set_uniform(CanvasShaderGLES3::CLIP_RECT_UV, false);
+ state.canvas_shader.set_uniform(CanvasShaderGLES3::NP_REPEAT_H, int(np->axis_x));
+ state.canvas_shader.set_uniform(CanvasShaderGLES3::NP_REPEAT_V, int(np->axis_y));
+ state.canvas_shader.set_uniform(CanvasShaderGLES3::NP_DRAW_CENTER, np->draw_center);
+ state.canvas_shader.set_uniform(CanvasShaderGLES3::NP_MARGINS, Color(np->margin[MARGIN_LEFT], np->margin[MARGIN_TOP], np->margin[MARGIN_RIGHT], np->margin[MARGIN_BOTTOM]));
+ state.canvas_shader.set_uniform(CanvasShaderGLES3::DST_RECT, Color(np->rect.position.x, np->rect.position.y, np->rect.size.x, np->rect.size.y));
+
+ glDrawArrays(GL_TRIANGLE_FAN, 0, 4);
+
+ storage->info.render._2d_draw_call_count++;
+ } break;
+ case Item::Command::TYPE_PRIMITIVE: {
+
+ Item::CommandPrimitive *primitive = static_cast(c);
+ _set_texture_rect_mode(false);
+
+ ERR_CONTINUE(primitive->points.size() < 1);
+
+ RasterizerStorageGLES3::Texture *texture = _bind_canvas_texture(primitive->texture, primitive->normal_map);
+
+ if (texture) {
+ Size2 texpixel_size(1.0 / texture->width, 1.0 / texture->height);
+ state.canvas_shader.set_uniform(CanvasShaderGLES3::COLOR_TEXPIXEL_SIZE, texpixel_size);
+ }
+ if (primitive->colors.size() == 1 && primitive->points.size() > 1) {
+
+ Color col = primitive->colors[0];
+ glVertexAttrib4f(VS::ARRAY_COLOR, col.r, col.g, col.b, col.a);
+
+ } else if (primitive->colors.empty()) {
+ glVertexAttrib4f(VS::ARRAY_COLOR, 1, 1, 1, 1);
+ }
+
+ _draw_gui_primitive(primitive->points.size(), primitive->points.ptr(), primitive->colors.ptr(), primitive->uvs.ptr());
+
+ } break;
+ case Item::Command::TYPE_POLYGON: {
+
+ Item::CommandPolygon *polygon = static_cast(c);
+ _set_texture_rect_mode(false);
+
+ RasterizerStorageGLES3::Texture *texture = _bind_canvas_texture(polygon->texture, polygon->normal_map);
+
+ if (texture) {
+ Size2 texpixel_size(1.0 / texture->width, 1.0 / texture->height);
+ state.canvas_shader.set_uniform(CanvasShaderGLES3::COLOR_TEXPIXEL_SIZE, texpixel_size);
+ }
+
+ _draw_polygon(polygon->indices.ptr(), polygon->count, polygon->points.size(), polygon->points.ptr(), polygon->uvs.ptr(), polygon->colors.ptr(), polygon->colors.size() == 1, polygon->bones.ptr(), polygon->weights.ptr());
+#ifdef GLES_OVER_GL
+ if (polygon->antialiased) {
+ glEnable(GL_LINE_SMOOTH);
+ if (polygon->antialiasing_use_indices) {
+ _draw_generic_indices(GL_LINE_STRIP, polygon->indices.ptr(), polygon->count, polygon->points.size(), polygon->points.ptr(), polygon->uvs.ptr(), polygon->colors.ptr(), polygon->colors.size() == 1);
+ } else {
+ _draw_generic(GL_LINE_LOOP, polygon->points.size(), polygon->points.ptr(), polygon->uvs.ptr(), polygon->colors.ptr(), polygon->colors.size() == 1);
+ }
+ glDisable(GL_LINE_SMOOTH);
+ }
+#endif
+
+ } break;
+ case Item::Command::TYPE_MESH: {
+
+ Item::CommandMesh *mesh = static_cast(c);
+ _set_texture_rect_mode(false);
+
+ RasterizerStorageGLES3::Texture *texture = _bind_canvas_texture(mesh->texture, mesh->normal_map);
+
+ if (texture) {
+ Size2 texpixel_size(1.0 / texture->width, 1.0 / texture->height);
+ state.canvas_shader.set_uniform(CanvasShaderGLES3::COLOR_TEXPIXEL_SIZE, texpixel_size);
+ }
+
+ state.canvas_shader.set_uniform(CanvasShaderGLES3::MODELVIEW_MATRIX, state.final_transform * mesh->transform);
+
+ RasterizerStorageGLES3::Mesh *mesh_data = storage->mesh_owner.getornull(mesh->mesh);
+ if (mesh_data) {
+
+ for (int j = 0; j < mesh_data->surfaces.size(); j++) {
+ RasterizerStorageGLES3::Surface *s = mesh_data->surfaces[j];
+ // materials are ignored in 2D meshes, could be added but many things (ie, lighting mode, reading from screen, etc) would break as they are not meant be set up at this point of drawing
+ glBindVertexArray(s->array_id);
+
+ glVertexAttrib4f(VS::ARRAY_COLOR, mesh->modulate.r, mesh->modulate.g, mesh->modulate.b, mesh->modulate.a);
+
+ if (s->index_array_len) {
+ glDrawElements(gl_primitive[s->primitive], s->index_array_len, (s->array_len >= (1 << 16)) ? GL_UNSIGNED_INT : GL_UNSIGNED_SHORT, 0);
+ } else {
+ glDrawArrays(gl_primitive[s->primitive], 0, s->array_len);
+ }
+ storage->info.render._2d_draw_call_count++;
+
+ glBindVertexArray(0);
+ }
+ }
+ state.canvas_shader.set_uniform(CanvasShaderGLES3::MODELVIEW_MATRIX, state.final_transform);
+
+ } break;
+ case Item::Command::TYPE_MULTIMESH: {
+
+ Item::CommandMultiMesh *mmesh = static_cast(c);
+
+ RasterizerStorageGLES3::MultiMesh *multi_mesh = storage->multimesh_owner.getornull(mmesh->multimesh);
+
+ if (!multi_mesh)
+ break;
+
+ RasterizerStorageGLES3::Mesh *mesh_data = storage->mesh_owner.getornull(multi_mesh->mesh);
+
+ if (!mesh_data)
+ break;
+
+ RasterizerStorageGLES3::Texture *texture = _bind_canvas_texture(mmesh->texture, mmesh->normal_map);
+
+ state.canvas_shader.set_conditional(CanvasShaderGLES3::USE_INSTANCE_CUSTOM, multi_mesh->custom_data_format != VS::MULTIMESH_CUSTOM_DATA_NONE);
+ state.canvas_shader.set_conditional(CanvasShaderGLES3::USE_INSTANCING, true);
+ //reset shader and force rebind
+ state.using_texture_rect = true;
+ _set_texture_rect_mode(false);
+
+ if (texture) {
+ Size2 texpixel_size(1.0 / texture->width, 1.0 / texture->height);
+ state.canvas_shader.set_uniform(CanvasShaderGLES3::COLOR_TEXPIXEL_SIZE, texpixel_size);
+ }
+
+ int amount = MIN(multi_mesh->size, multi_mesh->visible_instances);
+
+ if (amount == -1) {
+ amount = multi_mesh->size;
+ }
+
+ for (int j = 0; j < mesh_data->surfaces.size(); j++) {
+ RasterizerStorageGLES3::Surface *s = mesh_data->surfaces[j];
+ // materials are ignored in 2D meshes, could be added but many things (ie, lighting mode, reading from screen, etc) would break as they are not meant be set up at this point of drawing
+ glBindVertexArray(s->instancing_array_id);
+
+ glBindBuffer(GL_ARRAY_BUFFER, multi_mesh->buffer); //modify the buffer
+
+ int stride = (multi_mesh->xform_floats + multi_mesh->color_floats + multi_mesh->custom_data_floats) * 4;
+ glEnableVertexAttribArray(8);
+ glVertexAttribPointer(8, 4, GL_FLOAT, GL_FALSE, stride, CAST_INT_TO_UCHAR_PTR(0));
+ glVertexAttribDivisor(8, 1);
+ glEnableVertexAttribArray(9);
+ glVertexAttribPointer(9, 4, GL_FLOAT, GL_FALSE, stride, CAST_INT_TO_UCHAR_PTR(4 * 4));
+ glVertexAttribDivisor(9, 1);
+
+ int color_ofs;
+
+ if (multi_mesh->transform_format == VS::MULTIMESH_TRANSFORM_3D) {
+ glEnableVertexAttribArray(10);
+ glVertexAttribPointer(10, 4, GL_FLOAT, GL_FALSE, stride, CAST_INT_TO_UCHAR_PTR(8 * 4));
+ glVertexAttribDivisor(10, 1);
+ color_ofs = 12 * 4;
+ } else {
+ glDisableVertexAttribArray(10);
+ glVertexAttrib4f(10, 0, 0, 1, 0);
+ color_ofs = 8 * 4;
+ }
+
+ int custom_data_ofs = color_ofs;
+
+ switch (multi_mesh->color_format) {
+
+ case VS::MULTIMESH_COLOR_MAX:
+ case VS::MULTIMESH_COLOR_NONE: {
+ glDisableVertexAttribArray(11);
+ glVertexAttrib4f(11, 1, 1, 1, 1);
+ } break;
+ case VS::MULTIMESH_COLOR_8BIT: {
+ glEnableVertexAttribArray(11);
+ glVertexAttribPointer(11, 4, GL_UNSIGNED_BYTE, GL_TRUE, stride, CAST_INT_TO_UCHAR_PTR(color_ofs));
+ glVertexAttribDivisor(11, 1);
+ custom_data_ofs += 4;
+
+ } break;
+ case VS::MULTIMESH_COLOR_FLOAT: {
+ glEnableVertexAttribArray(11);
+ glVertexAttribPointer(11, 4, GL_FLOAT, GL_FALSE, stride, CAST_INT_TO_UCHAR_PTR(color_ofs));
+ glVertexAttribDivisor(11, 1);
+ custom_data_ofs += 4 * 4;
+ } break;
+ }
+
+ switch (multi_mesh->custom_data_format) {
+
+ case VS::MULTIMESH_CUSTOM_DATA_MAX:
+ case VS::MULTIMESH_CUSTOM_DATA_NONE: {
+ glDisableVertexAttribArray(12);
+ glVertexAttrib4f(12, 1, 1, 1, 1);
+ } break;
+ case VS::MULTIMESH_CUSTOM_DATA_8BIT: {
+ glEnableVertexAttribArray(12);
+ glVertexAttribPointer(12, 4, GL_UNSIGNED_BYTE, GL_TRUE, stride, CAST_INT_TO_UCHAR_PTR(custom_data_ofs));
+ glVertexAttribDivisor(12, 1);
+
+ } break;
+ case VS::MULTIMESH_CUSTOM_DATA_FLOAT: {
+ glEnableVertexAttribArray(12);
+ glVertexAttribPointer(12, 4, GL_FLOAT, GL_FALSE, stride, CAST_INT_TO_UCHAR_PTR(custom_data_ofs));
+ glVertexAttribDivisor(12, 1);
+ } break;
+ }
+
+ if (s->index_array_len) {
+ glDrawElementsInstanced(gl_primitive[s->primitive], s->index_array_len, (s->array_len >= (1 << 16)) ? GL_UNSIGNED_INT : GL_UNSIGNED_SHORT, 0, amount);
+ } else {
+ glDrawArraysInstanced(gl_primitive[s->primitive], 0, s->array_len, amount);
+ }
+ storage->info.render._2d_draw_call_count++;
+
+ glBindVertexArray(0);
+ }
+
+ state.canvas_shader.set_conditional(CanvasShaderGLES3::USE_INSTANCE_CUSTOM, false);
+ state.canvas_shader.set_conditional(CanvasShaderGLES3::USE_INSTANCING, false);
+ state.using_texture_rect = true;
+ _set_texture_rect_mode(false);
+
+ } break;
+ case Item::Command::TYPE_PARTICLES: {
+
+ Item::CommandParticles *particles_cmd = static_cast(c);
+
+ RasterizerStorageGLES3::Particles *particles = storage->particles_owner.getornull(particles_cmd->particles);
+ if (!particles)
+ break;
+
+ if (particles->inactive && !particles->emitting)
+ break;
+
+ glVertexAttrib4f(VS::ARRAY_COLOR, 1, 1, 1, 1); //not used, so keep white
+
+ VisualServerRaster::redraw_request();
+
+ storage->particles_request_process(particles_cmd->particles);
+ //enable instancing
+
+ state.canvas_shader.set_conditional(CanvasShaderGLES3::USE_INSTANCE_CUSTOM, true);
+ state.canvas_shader.set_conditional(CanvasShaderGLES3::USE_PARTICLES, true);
+ state.canvas_shader.set_conditional(CanvasShaderGLES3::USE_INSTANCING, true);
+ //reset shader and force rebind
+ state.using_texture_rect = true;
+ _set_texture_rect_mode(false);
+
+ RasterizerStorageGLES3::Texture *texture = _bind_canvas_texture(particles_cmd->texture, particles_cmd->normal_map);
+
+ if (texture) {
+ Size2 texpixel_size(1.0 / texture->width, 1.0 / texture->height);
+ state.canvas_shader.set_uniform(CanvasShaderGLES3::COLOR_TEXPIXEL_SIZE, texpixel_size);
+ } else {
+ state.canvas_shader.set_uniform(CanvasShaderGLES3::COLOR_TEXPIXEL_SIZE, Vector2(1.0, 1.0));
+ }
+
+ if (!particles->use_local_coords) {
+
+ Transform2D inv_xf;
+ inv_xf.set_axis(0, Vector2(particles->emission_transform.basis.get_axis(0).x, particles->emission_transform.basis.get_axis(0).y));
+ inv_xf.set_axis(1, Vector2(particles->emission_transform.basis.get_axis(1).x, particles->emission_transform.basis.get_axis(1).y));
+ inv_xf.set_origin(Vector2(particles->emission_transform.get_origin().x, particles->emission_transform.get_origin().y));
+ inv_xf.affine_invert();
+
+ state.canvas_shader.set_uniform(CanvasShaderGLES3::MODELVIEW_MATRIX, state.final_transform * inv_xf);
+ }
+
+ glBindVertexArray(data.particle_quad_array); //use particle quad array
+ glBindBuffer(GL_ARRAY_BUFFER, particles->particle_buffers[0]); //bind particle buffer
+
+ int stride = sizeof(float) * 4 * 6;
+
+ int amount = particles->amount;
+
+ if (particles->draw_order != VS::PARTICLES_DRAW_ORDER_LIFETIME) {
+
+ glEnableVertexAttribArray(8); //xform x
+ glVertexAttribPointer(8, 4, GL_FLOAT, GL_FALSE, stride, CAST_INT_TO_UCHAR_PTR(sizeof(float) * 4 * 3));
+ glVertexAttribDivisor(8, 1);
+ glEnableVertexAttribArray(9); //xform y
+ glVertexAttribPointer(9, 4, GL_FLOAT, GL_FALSE, stride, CAST_INT_TO_UCHAR_PTR(sizeof(float) * 4 * 4));
+ glVertexAttribDivisor(9, 1);
+ glEnableVertexAttribArray(10); //xform z
+ glVertexAttribPointer(10, 4, GL_FLOAT, GL_FALSE, stride, CAST_INT_TO_UCHAR_PTR(sizeof(float) * 4 * 5));
+ glVertexAttribDivisor(10, 1);
+ glEnableVertexAttribArray(11); //color
+ glVertexAttribPointer(11, 4, GL_FLOAT, GL_FALSE, stride, NULL);
+ glVertexAttribDivisor(11, 1);
+ glEnableVertexAttribArray(12); //custom
+ glVertexAttribPointer(12, 4, GL_FLOAT, GL_FALSE, stride, CAST_INT_TO_UCHAR_PTR(sizeof(float) * 4 * 2));
+ glVertexAttribDivisor(12, 1);
+
+ glDrawArraysInstanced(GL_TRIANGLE_FAN, 0, 4, amount);
+ storage->info.render._2d_draw_call_count++;
+ } else {
+ //split
+ int split = int(Math::ceil(particles->phase * particles->amount));
+
+ if (amount - split > 0) {
+ glEnableVertexAttribArray(8); //xform x
+ glVertexAttribPointer(8, 4, GL_FLOAT, GL_FALSE, stride, CAST_INT_TO_UCHAR_PTR(stride * split + sizeof(float) * 4 * 3));
+ glVertexAttribDivisor(8, 1);
+ glEnableVertexAttribArray(9); //xform y
+ glVertexAttribPointer(9, 4, GL_FLOAT, GL_FALSE, stride, CAST_INT_TO_UCHAR_PTR(stride * split + sizeof(float) * 4 * 4));
+ glVertexAttribDivisor(9, 1);
+ glEnableVertexAttribArray(10); //xform z
+ glVertexAttribPointer(10, 4, GL_FLOAT, GL_FALSE, stride, CAST_INT_TO_UCHAR_PTR(stride * split + sizeof(float) * 4 * 5));
+ glVertexAttribDivisor(10, 1);
+ glEnableVertexAttribArray(11); //color
+ glVertexAttribPointer(11, 4, GL_FLOAT, GL_FALSE, stride, CAST_INT_TO_UCHAR_PTR(stride * split + 0));
+ glVertexAttribDivisor(11, 1);
+ glEnableVertexAttribArray(12); //custom
+ glVertexAttribPointer(12, 4, GL_FLOAT, GL_FALSE, stride, CAST_INT_TO_UCHAR_PTR(stride * split + sizeof(float) * 4 * 2));
+ glVertexAttribDivisor(12, 1);
+
+ glDrawArraysInstanced(GL_TRIANGLE_FAN, 0, 4, amount - split);
+ storage->info.render._2d_draw_call_count++;
+ }
+
+ if (split > 0) {
+ glEnableVertexAttribArray(8); //xform x
+ glVertexAttribPointer(8, 4, GL_FLOAT, GL_FALSE, stride, CAST_INT_TO_UCHAR_PTR(sizeof(float) * 4 * 3));
+ glVertexAttribDivisor(8, 1);
+ glEnableVertexAttribArray(9); //xform y
+ glVertexAttribPointer(9, 4, GL_FLOAT, GL_FALSE, stride, CAST_INT_TO_UCHAR_PTR(sizeof(float) * 4 * 4));
+ glVertexAttribDivisor(9, 1);
+ glEnableVertexAttribArray(10); //xform z
+ glVertexAttribPointer(10, 4, GL_FLOAT, GL_FALSE, stride, CAST_INT_TO_UCHAR_PTR(sizeof(float) * 4 * 5));
+ glVertexAttribDivisor(10, 1);
+ glEnableVertexAttribArray(11); //color
+ glVertexAttribPointer(11, 4, GL_FLOAT, GL_FALSE, stride, NULL);
+ glVertexAttribDivisor(11, 1);
+ glEnableVertexAttribArray(12); //custom
+ glVertexAttribPointer(12, 4, GL_FLOAT, GL_FALSE, stride, CAST_INT_TO_UCHAR_PTR(sizeof(float) * 4 * 2));
+ glVertexAttribDivisor(12, 1);
+
+ glDrawArraysInstanced(GL_TRIANGLE_FAN, 0, 4, split);
+ storage->info.render._2d_draw_call_count++;
+ }
+ }
+
+ glBindVertexArray(0);
+
+ state.canvas_shader.set_conditional(CanvasShaderGLES3::USE_INSTANCE_CUSTOM, false);
+ state.canvas_shader.set_conditional(CanvasShaderGLES3::USE_PARTICLES, false);
+ state.canvas_shader.set_conditional(CanvasShaderGLES3::USE_INSTANCING, false);
+ state.using_texture_rect = true;
+ _set_texture_rect_mode(false);
+
+ } break;
+ case Item::Command::TYPE_CIRCLE: {
+
+ _set_texture_rect_mode(false);
+
+ Item::CommandCircle *circle = static_cast(c);
+ static const int numpoints = 32;
+ Vector2 points[numpoints + 1];
+ points[numpoints] = circle->pos;
+ int indices[numpoints * 3];
+
+ for (int j = 0; j < numpoints; j++) {
+
+ points[j] = circle->pos + Vector2(Math::sin(j * Math_PI * 2.0 / numpoints), Math::cos(j * Math_PI * 2.0 / numpoints)) * circle->radius;
+ indices[j * 3 + 0] = j;
+ indices[j * 3 + 1] = (j + 1) % numpoints;
+ indices[j * 3 + 2] = numpoints;
+ }
+
+ _bind_canvas_texture(RID(), RID());
+ _draw_polygon(indices, numpoints * 3, numpoints + 1, points, NULL, &circle->color, true, NULL, NULL);
+
+ //_draw_polygon(numpoints*3,indices,points,NULL,&circle->color,RID(),true);
+ //canvas_draw_circle(circle->indices.size(),circle->indices.ptr(),circle->points.ptr(),circle->uvs.ptr(),circle->colors.ptr(),circle->texture,circle->colors.size()==1);
+ } break;
+ case Item::Command::TYPE_TRANSFORM: {
+
+ Item::CommandTransform *transform = static_cast(c);
+ state.extra_matrix = transform->xform;
+ state.canvas_shader.set_uniform(CanvasShaderGLES3::EXTRA_MATRIX, state.extra_matrix);
+
+ } break;
+ case Item::Command::TYPE_CLIP_IGNORE: {
+
+ Item::CommandClipIgnore *ci = static_cast(c);
+ if (p_current_clip) {
+
+ if (ci->ignore != r_reclip) {
+ if (ci->ignore) {
+
+ glDisable(GL_SCISSOR_TEST);
+ r_reclip = true;
+ } else {
+
+ glEnable(GL_SCISSOR_TEST);
+ //glScissor(viewport.x+current_clip->final_clip_rect.pos.x,viewport.y+ (viewport.height-(current_clip->final_clip_rect.pos.y+current_clip->final_clip_rect.size.height)),
+ //current_clip->final_clip_rect.size.width,current_clip->final_clip_rect.size.height);
+ int y = storage->frame.current_rt->height - (p_current_clip->final_clip_rect.position.y + p_current_clip->final_clip_rect.size.y);
+ if (storage->frame.current_rt->flags[RasterizerStorage::RENDER_TARGET_VFLIP])
+ y = p_current_clip->final_clip_rect.position.y;
+
+ glScissor(p_current_clip->final_clip_rect.position.x, y, p_current_clip->final_clip_rect.size.x, p_current_clip->final_clip_rect.size.y);
+
+ r_reclip = false;
+ }
+ }
+ }
+
+ } break;
+
+ default: {
+ // FIXME: Proper error handling if relevant
+ //print_line("other");
+ } break;
+ }
+ }
+
+ } // default
+ break;
+ }
+ }
+}
+
+void RasterizerCanvasGLES3::render_joined_item(const BItemJoined &p_bij, RenderItemState &r_ris) {
+ storage->info.render._2d_item_count++;
+
+#ifdef DEBUG_ENABLED
+ if (bdata.diagnose_frame) {
+ bdata.frame_string += "\tjoined_item " + itos(p_bij.num_item_refs) + " refs\n";
+ if (p_bij.z_index != 0) {
+ bdata.frame_string += "\t\t(z " + itos(p_bij.z_index) + ")\n";
+ }
+ }
+#endif
+
+ // this must be reset for each joined item,
+ // it only exists to prevent capturing the screen more than once per item
+ state.canvas_texscreen_used = false;
+
+ // all the joined items will share the same state with the first item
+ Item *p_ci = bdata.item_refs[p_bij.first_item_ref].item;
+
+ if (r_ris.prev_distance_field != p_ci->distance_field) {
+
+ state.canvas_shader.set_conditional(CanvasShaderGLES3::USE_DISTANCE_FIELD, p_ci->distance_field);
+ r_ris.prev_distance_field = p_ci->distance_field;
+ r_ris.rebind_shader = true;
+ }
+
+ if (r_ris.current_clip != p_ci->final_clip_owner) {
+
+ r_ris.current_clip = p_ci->final_clip_owner;
+
+ //setup clip
+ if (r_ris.current_clip) {
+
+ glEnable(GL_SCISSOR_TEST);
+ int y = storage->frame.current_rt->height - (r_ris.current_clip->final_clip_rect.position.y + r_ris.current_clip->final_clip_rect.size.y);
+ if (storage->frame.current_rt->flags[RasterizerStorage::RENDER_TARGET_VFLIP])
+ y = r_ris.current_clip->final_clip_rect.position.y;
+
+ glScissor(r_ris.current_clip->final_clip_rect.position.x, y, r_ris.current_clip->final_clip_rect.size.x, r_ris.current_clip->final_clip_rect.size.y);
+
+ } else {
+
+ glDisable(GL_SCISSOR_TEST);
+ }
+ }
+
+ if (p_ci->copy_back_buffer) {
+
+ if (p_ci->copy_back_buffer->full) {
+
+ _copy_texscreen(Rect2());
+ } else {
+ _copy_texscreen(p_ci->copy_back_buffer->rect);
+ }
+ }
+
+ if (!bdata.settings_use_batching || !bdata.settings_use_software_skinning) {
+
+ RasterizerStorageGLES3::Skeleton *skeleton = NULL;
+
+ //skeleton handling
+ if (p_ci->skeleton.is_valid() && storage->skeleton_owner.owns(p_ci->skeleton)) {
+ skeleton = storage->skeleton_owner.get(p_ci->skeleton);
+ if (!skeleton->use_2d) {
+ skeleton = NULL;
+ } else {
+ state.skeleton_transform = r_ris.item_group_base_transform * skeleton->base_transform_2d;
+ state.skeleton_transform_inverse = state.skeleton_transform.affine_inverse();
+ }
+ }
+
+ bool use_skeleton = skeleton != NULL;
+ if (r_ris.prev_use_skeleton != use_skeleton) {
+ r_ris.rebind_shader = true;
+ state.canvas_shader.set_conditional(CanvasShaderGLES3::USE_SKELETON, use_skeleton);
+ r_ris.prev_use_skeleton = use_skeleton;
+ }
+
+ if (skeleton) {
+ glActiveTexture(GL_TEXTURE0 + storage->config.max_texture_image_units - 4);
+ glBindTexture(GL_TEXTURE_2D, skeleton->texture);
+ state.using_skeleton = true;
+ } else {
+ state.using_skeleton = false;
+ }
+
+ } // if not using batching
+
+ //begin rect
+ Item *material_owner = p_ci->material_owner ? p_ci->material_owner : p_ci;
+
+ RID material = material_owner->material;
+
+ if (material != r_ris.canvas_last_material || r_ris.rebind_shader) {
+
+ RasterizerStorageGLES3::Material *material_ptr = storage->material_owner.getornull(material);
+ RasterizerStorageGLES3::Shader *shader_ptr = NULL;
+
+ if (material_ptr) {
+
+ shader_ptr = material_ptr->shader;
+
+ if (shader_ptr && shader_ptr->mode != VS::SHADER_CANVAS_ITEM) {
+ shader_ptr = NULL; //do not use non canvasitem shader
+ }
+ }
+
+ if (shader_ptr) {
+
+ if (shader_ptr->canvas_item.uses_screen_texture && !state.canvas_texscreen_used) {
+ //copy if not copied before
+ _copy_texscreen(Rect2());
+
+ // blend mode will have been enabled so make sure we disable it again later on
+ r_ris.last_blend_mode = r_ris.last_blend_mode != RasterizerStorageGLES3::Shader::CanvasItem::BLEND_MODE_DISABLED ? r_ris.last_blend_mode : -1;
+ }
+
+ if (shader_ptr != r_ris.shader_cache || r_ris.rebind_shader) {
+
+ if (shader_ptr->canvas_item.uses_time) {
+ VisualServerRaster::redraw_request();
+ }
+
+ state.canvas_shader.set_custom_shader(shader_ptr->custom_code_id);
+ state.canvas_shader.bind();
+ }
+
+ if (material_ptr->ubo_id) {
+ glBindBufferBase(GL_UNIFORM_BUFFER, 2, material_ptr->ubo_id);
+ }
+
+ int tc = material_ptr->textures.size();
+ RID *textures = material_ptr->textures.ptrw();
+ ShaderLanguage::ShaderNode::Uniform::Hint *texture_hints = shader_ptr->texture_hints.ptrw();
+
+ for (int i = 0; i < tc; i++) {
+
+ glActiveTexture(GL_TEXTURE2 + i);
+
+ RasterizerStorageGLES3::Texture *t = storage->texture_owner.getornull(textures[i]);
+ if (!t) {
+
+ switch (texture_hints[i]) {
+ case ShaderLanguage::ShaderNode::Uniform::HINT_BLACK_ALBEDO:
+ case ShaderLanguage::ShaderNode::Uniform::HINT_BLACK: {
+ glBindTexture(GL_TEXTURE_2D, storage->resources.black_tex);
+ } break;
+ case ShaderLanguage::ShaderNode::Uniform::HINT_ANISO: {
+ glBindTexture(GL_TEXTURE_2D, storage->resources.aniso_tex);
+ } break;
+ case ShaderLanguage::ShaderNode::Uniform::HINT_NORMAL: {
+ glBindTexture(GL_TEXTURE_2D, storage->resources.normal_tex);
+ } break;
+ default: {
+ glBindTexture(GL_TEXTURE_2D, storage->resources.white_tex);
+ } break;
+ }
+
+ //check hints
+
+ continue;
+ }
+
+ if (t->redraw_if_visible) { //check before proxy, because this is usually used with proxies
+ VisualServerRaster::redraw_request();
+ }
+
+ t = t->get_ptr();
+
+ if (storage->config.srgb_decode_supported && t->using_srgb) {
+ //no srgb in 2D
+ glTexParameteri(t->target, _TEXTURE_SRGB_DECODE_EXT, _SKIP_DECODE_EXT);
+ t->using_srgb = false;
+ }
+
+ glBindTexture(t->target, t->tex_id);
+ }
+
+ } else {
+ state.canvas_shader.set_custom_shader(0);
+ state.canvas_shader.bind();
+ }
+
+ r_ris.shader_cache = shader_ptr;
+
+ r_ris.canvas_last_material = material;
+ r_ris.rebind_shader = false;
+ }
+
+ int blend_mode = r_ris.shader_cache ? r_ris.shader_cache->canvas_item.blend_mode : RasterizerStorageGLES3::Shader::CanvasItem::BLEND_MODE_MIX;
+ if (blend_mode == RasterizerStorageGLES3::Shader::CanvasItem::BLEND_MODE_DISABLED && (!storage->frame.current_rt || !storage->frame.current_rt->flags[RasterizerStorage::RENDER_TARGET_TRANSPARENT])) {
+ blend_mode = RasterizerStorageGLES3::Shader::CanvasItem::BLEND_MODE_MIX;
+ }
+ bool unshaded = r_ris.shader_cache && (r_ris.shader_cache->canvas_item.light_mode == RasterizerStorageGLES3::Shader::CanvasItem::LIGHT_MODE_UNSHADED || (blend_mode != RasterizerStorageGLES3::Shader::CanvasItem::BLEND_MODE_MIX && blend_mode != RasterizerStorageGLES3::Shader::CanvasItem::BLEND_MODE_PMALPHA));
+ bool reclip = false;
+
+ if (r_ris.last_blend_mode != blend_mode) {
+ if (r_ris.last_blend_mode == RasterizerStorageGLES3::Shader::CanvasItem::BLEND_MODE_DISABLED) {
+ // re-enable it
+ glEnable(GL_BLEND);
+ } else if (blend_mode == RasterizerStorageGLES3::Shader::CanvasItem::BLEND_MODE_DISABLED) {
+ // disable it
+ glDisable(GL_BLEND);
+ }
+
+ switch (blend_mode) {
+
+ case RasterizerStorageGLES3::Shader::CanvasItem::BLEND_MODE_DISABLED: {
+
+ // nothing to do here
+
+ } break;
+ case RasterizerStorageGLES3::Shader::CanvasItem::BLEND_MODE_MIX: {
+
+ glBlendEquation(GL_FUNC_ADD);
+ if (storage->frame.current_rt && storage->frame.current_rt->flags[RasterizerStorage::RENDER_TARGET_TRANSPARENT]) {
+ glBlendFuncSeparate(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA, GL_ONE, GL_ONE_MINUS_SRC_ALPHA);
+ } else {
+ glBlendFuncSeparate(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA, GL_ZERO, GL_ONE);
+ }
+
+ } break;
+ case RasterizerStorageGLES3::Shader::CanvasItem::BLEND_MODE_ADD: {
+
+ glBlendEquation(GL_FUNC_ADD);
+ if (storage->frame.current_rt && storage->frame.current_rt->flags[RasterizerStorage::RENDER_TARGET_TRANSPARENT]) {
+ glBlendFuncSeparate(GL_SRC_ALPHA, GL_ONE, GL_SRC_ALPHA, GL_ONE);
+ } else {
+ glBlendFuncSeparate(GL_SRC_ALPHA, GL_ONE, GL_ZERO, GL_ONE);
+ }
+
+ } break;
+ case RasterizerStorageGLES3::Shader::CanvasItem::BLEND_MODE_SUB: {
+
+ glBlendEquation(GL_FUNC_REVERSE_SUBTRACT);
+ if (storage->frame.current_rt && storage->frame.current_rt->flags[RasterizerStorage::RENDER_TARGET_TRANSPARENT]) {
+ glBlendFuncSeparate(GL_SRC_ALPHA, GL_ONE, GL_SRC_ALPHA, GL_ONE);
+ } else {
+ glBlendFuncSeparate(GL_SRC_ALPHA, GL_ONE, GL_ZERO, GL_ONE);
+ }
+ } break;
+ case RasterizerStorageGLES3::Shader::CanvasItem::BLEND_MODE_MUL: {
+ glBlendEquation(GL_FUNC_ADD);
+ if (storage->frame.current_rt && storage->frame.current_rt->flags[RasterizerStorage::RENDER_TARGET_TRANSPARENT]) {
+ glBlendFuncSeparate(GL_DST_COLOR, GL_ZERO, GL_DST_ALPHA, GL_ZERO);
+ } else {
+ glBlendFuncSeparate(GL_DST_COLOR, GL_ZERO, GL_ZERO, GL_ONE);
+ }
+
+ } break;
+ case RasterizerStorageGLES3::Shader::CanvasItem::BLEND_MODE_PMALPHA: {
+ glBlendEquation(GL_FUNC_ADD);
+ if (storage->frame.current_rt && storage->frame.current_rt->flags[RasterizerStorage::RENDER_TARGET_TRANSPARENT]) {
+ glBlendFuncSeparate(GL_ONE, GL_ONE_MINUS_SRC_ALPHA, GL_ONE, GL_ONE_MINUS_SRC_ALPHA);
+ } else {
+ glBlendFuncSeparate(GL_ONE, GL_ONE_MINUS_SRC_ALPHA, GL_ZERO, GL_ONE);
+ }
+
+ } break;
+ }
+
+ r_ris.last_blend_mode = blend_mode;
+ }
+
+ //state.canvas_item_modulate = unshaded ? p_ci->final_modulate : Color(p_ci->final_modulate.r * r_ris.item_group_modulate.r, p_ci->final_modulate.g * r_ris.item_group_modulate.g, p_ci->final_modulate.b * r_ris.item_group_modulate.b, p_ci->final_modulate.a * r_ris.item_group_modulate.a);
+
+ // state.final_transform = p_ci->final_transform;
+ // state.extra_matrix = Transform2D();
+
+ // using software transform
+ if (!p_bij.use_hardware_transform()) {
+ state.final_transform = Transform2D();
+ // final_modulate will be baked per item ref so the final_modulate can be an identity color
+ state.canvas_item_modulate = Color(1, 1, 1, 1);
+ } else {
+ state.final_transform = p_ci->final_transform;
+ // could use the stored version of final_modulate in item ref? Test which is faster NYI
+ state.canvas_item_modulate = unshaded ? p_ci->final_modulate : (p_ci->final_modulate * r_ris.item_group_modulate);
+ }
+ state.extra_matrix = Transform2D();
+
+ if (state.using_skeleton) {
+ state.canvas_shader.set_uniform(CanvasShaderGLES3::SKELETON_TRANSFORM, state.skeleton_transform);
+ state.canvas_shader.set_uniform(CanvasShaderGLES3::SKELETON_TRANSFORM_INVERSE, state.skeleton_transform_inverse);
+ }
+
+ state.canvas_shader.set_uniform(CanvasShaderGLES3::FINAL_MODULATE, state.canvas_item_modulate);
+ state.canvas_shader.set_uniform(CanvasShaderGLES3::MODELVIEW_MATRIX, state.final_transform);
+ state.canvas_shader.set_uniform(CanvasShaderGLES3::EXTRA_MATRIX, state.extra_matrix);
+ if (storage->frame.current_rt) {
+ state.canvas_shader.set_uniform(CanvasShaderGLES3::SCREEN_PIXEL_SIZE, Vector2(1.0 / storage->frame.current_rt->width, 1.0 / storage->frame.current_rt->height));
+ } else {
+ state.canvas_shader.set_uniform(CanvasShaderGLES3::SCREEN_PIXEL_SIZE, Vector2(1.0, 1.0));
+ }
+ if (unshaded || (state.canvas_item_modulate.a > 0.001 && (!r_ris.shader_cache || r_ris.shader_cache->canvas_item.light_mode != RasterizerStorageGLES3::Shader::CanvasItem::LIGHT_MODE_LIGHT_ONLY) && !p_ci->light_masked)) {
+ RasterizerStorageGLES3::Material *material_ptr = nullptr;
+ render_joined_item_commands(p_bij, NULL, reclip, material_ptr, false);
+ }
+
+ if ((blend_mode == RasterizerStorageGLES3::Shader::CanvasItem::BLEND_MODE_MIX || blend_mode == RasterizerStorageGLES3::Shader::CanvasItem::BLEND_MODE_PMALPHA) && r_ris.item_group_light && !unshaded) {
+
+ Light *light = r_ris.item_group_light;
+ bool light_used = false;
+ VS::CanvasLightMode mode = VS::CANVAS_LIGHT_MODE_ADD;
+ state.canvas_item_modulate = p_ci->final_modulate; // remove the canvas modulate
+
+ while (light) {
+
+ // use the bounding rect of the joined items, NOT only the bounding rect of the first item.
+ // note this is a cost of batching, the light culling will be less effective
+
+ // note that the r_ris.item_group_z will be out of date because we are using deferred rendering till canvas_render_items_end()
+ // so we have to test z against the stored value in the joined item
+ if (p_ci->light_mask & light->item_mask && p_bij.z_index >= light->z_min && p_bij.z_index <= light->z_max && p_bij.bounding_rect.intersects_transformed(light->xform_cache, light->rect_cache)) {
+
+ //intersects this light
+
+ if (!light_used || mode != light->mode) {
+
+ mode = light->mode;
+
+ switch (mode) {
+
+ case VS::CANVAS_LIGHT_MODE_ADD: {
+ glBlendEquation(GL_FUNC_ADD);
+ glBlendFunc(GL_SRC_ALPHA, GL_ONE);
+
+ } break;
+ case VS::CANVAS_LIGHT_MODE_SUB: {
+ glBlendEquation(GL_FUNC_REVERSE_SUBTRACT);
+ glBlendFunc(GL_SRC_ALPHA, GL_ONE);
+ } break;
+ case VS::CANVAS_LIGHT_MODE_MIX:
+ case VS::CANVAS_LIGHT_MODE_MASK: {
+ glBlendEquation(GL_FUNC_ADD);
+ glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
+
+ } break;
+ }
+ }
+
+ if (!light_used) {
+
+ state.canvas_shader.set_conditional(CanvasShaderGLES3::USE_LIGHTING, true);
+ light_used = true;
+ }
+
+ bool has_shadow = light->shadow_buffer.is_valid() && p_ci->light_mask & light->item_shadow_mask;
+
+ state.canvas_shader.set_conditional(CanvasShaderGLES3::USE_SHADOWS, has_shadow);
+ if (has_shadow) {
+ state.canvas_shader.set_conditional(CanvasShaderGLES3::SHADOW_USE_GRADIENT, light->shadow_gradient_length > 0);
+ state.canvas_shader.set_conditional(CanvasShaderGLES3::SHADOW_FILTER_NEAREST, light->shadow_filter == VS::CANVAS_LIGHT_FILTER_NONE);
+ state.canvas_shader.set_conditional(CanvasShaderGLES3::SHADOW_FILTER_PCF3, light->shadow_filter == VS::CANVAS_LIGHT_FILTER_PCF3);
+ state.canvas_shader.set_conditional(CanvasShaderGLES3::SHADOW_FILTER_PCF5, light->shadow_filter == VS::CANVAS_LIGHT_FILTER_PCF5);
+ state.canvas_shader.set_conditional(CanvasShaderGLES3::SHADOW_FILTER_PCF7, light->shadow_filter == VS::CANVAS_LIGHT_FILTER_PCF7);
+ state.canvas_shader.set_conditional(CanvasShaderGLES3::SHADOW_FILTER_PCF9, light->shadow_filter == VS::CANVAS_LIGHT_FILTER_PCF9);
+ state.canvas_shader.set_conditional(CanvasShaderGLES3::SHADOW_FILTER_PCF13, light->shadow_filter == VS::CANVAS_LIGHT_FILTER_PCF13);
+ }
+
+ bool light_rebind = state.canvas_shader.bind();
+
+ if (light_rebind) {
+ state.canvas_shader.set_uniform(CanvasShaderGLES3::FINAL_MODULATE, state.canvas_item_modulate);
+ state.canvas_shader.set_uniform(CanvasShaderGLES3::MODELVIEW_MATRIX, state.final_transform);
+ state.canvas_shader.set_uniform(CanvasShaderGLES3::EXTRA_MATRIX, Transform2D());
+ if (storage->frame.current_rt) {
+ state.canvas_shader.set_uniform(CanvasShaderGLES3::SCREEN_PIXEL_SIZE, Vector2(1.0 / storage->frame.current_rt->width, 1.0 / storage->frame.current_rt->height));
+ } else {
+ state.canvas_shader.set_uniform(CanvasShaderGLES3::SCREEN_PIXEL_SIZE, Vector2(1.0, 1.0));
+ }
+ if (state.using_skeleton) {
+ state.canvas_shader.set_uniform(CanvasShaderGLES3::SKELETON_TRANSFORM, state.skeleton_transform);
+ state.canvas_shader.set_uniform(CanvasShaderGLES3::SKELETON_TRANSFORM_INVERSE, state.skeleton_transform_inverse);
+ }
+ }
+
+ glBindBufferBase(GL_UNIFORM_BUFFER, 1, static_cast(light->light_internal.get_data())->ubo);
+
+ if (has_shadow) {
+
+ RasterizerStorageGLES3::CanvasLightShadow *cls = storage->canvas_light_shadow_owner.get(light->shadow_buffer);
+ glActiveTexture(GL_TEXTURE0 + storage->config.max_texture_image_units - 2);
+ glBindTexture(GL_TEXTURE_2D, cls->distance);
+
+ /*canvas_shader.set_uniform(CanvasShaderGLES3::SHADOW_MATRIX,light->shadow_matrix_cache);
+ canvas_shader.set_uniform(CanvasShaderGLES3::SHADOW_ESM_MULTIPLIER,light->shadow_esm_mult);
+ canvas_shader.set_uniform(CanvasShaderGLES3::LIGHT_SHADOW_COLOR,light->shadow_color);*/
+ }
+
+ glActiveTexture(GL_TEXTURE0 + storage->config.max_texture_image_units - 1);
+ RasterizerStorageGLES3::Texture *t = storage->texture_owner.getornull(light->texture);
+ if (!t) {
+ glBindTexture(GL_TEXTURE_2D, storage->resources.white_tex);
+ } else {
+ t = t->get_ptr();
+
+ glBindTexture(t->target, t->tex_id);
+ }
+
+ glActiveTexture(GL_TEXTURE0);
+
+ // redraw using light.
+ // if there is no clip item, we can consider scissoring to the intersection area between the light and the item
+ // this can greatly reduce fill rate ..
+ // at the cost of glScissor commands, so is optional
+ if (!bdata.settings_scissor_lights || r_ris.current_clip) {
+ render_joined_item_commands(p_bij, NULL, reclip, nullptr, true);
+ } else {
+ bool scissor = _light_scissor_begin(p_bij.bounding_rect, light->xform_cache, light->rect_cache);
+ render_joined_item_commands(p_bij, NULL, reclip, nullptr, true);
+ if (scissor) {
+ glDisable(GL_SCISSOR_TEST);
+ }
+ }
+ }
+
+ light = light->next_ptr;
+ }
+
+ if (light_used) {
+
+ state.canvas_shader.set_conditional(CanvasShaderGLES3::USE_LIGHTING, false);
+ state.canvas_shader.set_conditional(CanvasShaderGLES3::USE_SHADOWS, false);
+ state.canvas_shader.set_conditional(CanvasShaderGLES3::SHADOW_FILTER_NEAREST, false);
+ state.canvas_shader.set_conditional(CanvasShaderGLES3::SHADOW_FILTER_PCF3, false);
+ state.canvas_shader.set_conditional(CanvasShaderGLES3::SHADOW_FILTER_PCF5, false);
+ state.canvas_shader.set_conditional(CanvasShaderGLES3::SHADOW_FILTER_PCF7, false);
+ state.canvas_shader.set_conditional(CanvasShaderGLES3::SHADOW_FILTER_PCF9, false);
+ state.canvas_shader.set_conditional(CanvasShaderGLES3::SHADOW_FILTER_PCF13, false);
+
+ state.canvas_shader.bind();
+
+ r_ris.last_blend_mode = -1;
+
+ /*
+ //this is set again, so it should not be needed anyway?
+ state.canvas_item_modulate = unshaded ? ci->final_modulate : Color(
+ ci->final_modulate.r * p_modulate.r,
+ ci->final_modulate.g * p_modulate.g,
+ ci->final_modulate.b * p_modulate.b,
+ ci->final_modulate.a * p_modulate.a );
+
+
+ state.canvas_shader.set_uniform(CanvasShaderGLES3::MODELVIEW_MATRIX,state.final_transform);
+ state.canvas_shader.set_uniform(CanvasShaderGLES3::EXTRA_MATRIX,Transform2D());
+ state.canvas_shader.set_uniform(CanvasShaderGLES3::FINAL_MODULATE,state.canvas_item_modulate);
+
+ glBlendEquation(GL_FUNC_ADD);
+
+ if (storage->frame.current_rt->flags[RasterizerStorage::RENDER_TARGET_TRANSPARENT]) {
+ glBlendFuncSeparate(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA, GL_ONE, GL_ONE_MINUS_SRC_ALPHA);
+ } else {
+ glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
+ }
+
+ //@TODO RESET canvas_blend_mode
+ */
+ }
+ }
+
+ if (reclip) {
+
+ glEnable(GL_SCISSOR_TEST);
+ int y = storage->frame.current_rt->height - (r_ris.current_clip->final_clip_rect.position.y + r_ris.current_clip->final_clip_rect.size.y);
+ if (storage->frame.current_rt->flags[RasterizerStorage::RENDER_TARGET_VFLIP])
+ y = r_ris.current_clip->final_clip_rect.position.y;
+ glScissor(r_ris.current_clip->final_clip_rect.position.x, y, r_ris.current_clip->final_clip_rect.size.width, r_ris.current_clip->final_clip_rect.size.height);
+ }
+}
+
+// This function is a dry run of the state changes when drawing the item.
+// It should duplicate the logic in _canvas_render_item,
+// to decide whether items are similar enough to join
+// i.e. no state differences between the 2 items.
+bool RasterizerCanvasGLES3::try_join_item(Item *p_ci, RenderItemState &r_ris, bool &r_batch_break) {
+ // if we set max join items to zero we can effectively prevent any joining, so
+ // none of the other logic needs to run. Good for testing regression bugs, and
+ // could conceivably be faster in some games.
+ if (!bdata.settings_max_join_item_commands) {
+ return false;
+ }
+
+ // if there are any state changes we change join to false
+ // we also set r_batch_break to true if we don't want this item joined to the next
+ // (e.g. an item that must not be joined at all)
+ r_batch_break = false;
+ bool join = true;
+
+ // light_masked may possibly need state checking here. Check for regressions!
+
+ // we will now allow joining even if final modulate is different
+ // we will instead bake the final modulate into the vertex colors
+ // if (p_ci->final_modulate != r_ris.final_modulate) {
+ // join = false;
+ // r_ris.final_modulate = p_ci->final_modulate;
+ // }
+
+ if (r_ris.current_clip != p_ci->final_clip_owner) {
+ r_ris.current_clip = p_ci->final_clip_owner;
+ join = false;
+ }
+
+ // TODO: copy back buffer
+
+ if (p_ci->copy_back_buffer) {
+ join = false;
+ }
+
+ RasterizerStorageGLES3::Skeleton *skeleton = NULL;
+
+ {
+ //skeleton handling
+ if (p_ci->skeleton.is_valid() && storage->skeleton_owner.owns(p_ci->skeleton)) {
+ skeleton = storage->skeleton_owner.get(p_ci->skeleton);
+ if (!skeleton->use_2d) {
+ skeleton = NULL;
+ }
+ }
+
+ bool skeleton_prevent_join = false;
+
+ bool use_skeleton = skeleton != NULL;
+ if (r_ris.prev_use_skeleton != use_skeleton) {
+
+ if (!bdata.settings_use_software_skinning)
+ r_ris.rebind_shader = true;
+
+ r_ris.prev_use_skeleton = use_skeleton;
+ // join = false;
+ skeleton_prevent_join = true;
+ }
+
+ if (skeleton) {
+ // join = false;
+ skeleton_prevent_join = true;
+ state.using_skeleton = true;
+ } else {
+ state.using_skeleton = false;
+ }
+
+ if (skeleton_prevent_join) {
+ if (!bdata.settings_use_software_skinning)
+ join = false;
+ }
+ }
+
+ Item *material_owner = p_ci->material_owner ? p_ci->material_owner : p_ci;
+
+ RID material = material_owner->material;
+ RasterizerStorageGLES3::Material *material_ptr = storage->material_owner.getornull(material);
+
+ if (material != r_ris.canvas_last_material || r_ris.rebind_shader) {
+
+ join = false;
+ RasterizerStorageGLES3::Shader *shader_ptr = NULL;
+
+ if (material_ptr) {
+ shader_ptr = material_ptr->shader;
+
+ if (shader_ptr && shader_ptr->mode != VS::SHADER_CANVAS_ITEM) {
+ shader_ptr = NULL; // not a canvas item shader, don't use.
+ }
+ }
+
+ if (shader_ptr) {
+ if (shader_ptr->canvas_item.uses_screen_texture) {
+ if (!state.canvas_texscreen_used) {
+ join = false;
+ }
+ }
+ }
+
+ r_ris.shader_cache = shader_ptr;
+
+ r_ris.canvas_last_material = material;
+
+ r_ris.rebind_shader = false;
+ }
+
+ int blend_mode = r_ris.shader_cache ? r_ris.shader_cache->canvas_item.blend_mode : RasterizerStorageGLES3::Shader::CanvasItem::BLEND_MODE_MIX;
+ bool unshaded = r_ris.shader_cache && (r_ris.shader_cache->canvas_item.light_mode == RasterizerStorageGLES3::Shader::CanvasItem::LIGHT_MODE_UNSHADED || (blend_mode != RasterizerStorageGLES3::Shader::CanvasItem::BLEND_MODE_MIX && blend_mode != RasterizerStorageGLES3::Shader::CanvasItem::BLEND_MODE_PMALPHA));
+ bool reclip = false;
+
+ // we are precalculating the final_modulate ahead of time because we need this for baking of final modulate into vertex colors
+ // (only in software transform mode)
+ // This maybe inefficient storing it...
+ r_ris.final_modulate = unshaded ? p_ci->final_modulate : (p_ci->final_modulate * r_ris.item_group_modulate);
+
+ if (r_ris.last_blend_mode != blend_mode) {
+ join = false;
+ r_ris.last_blend_mode = blend_mode;
+ }
+
+ // does the shader contain BUILTINs which should break the batching?
+ bdata.joined_item_batch_flags = 0;
+ if (r_ris.shader_cache) {
+
+ unsigned int and_flags = r_ris.shader_cache->canvas_item.batch_flags & (RasterizerStorageCommon::PREVENT_COLOR_BAKING | RasterizerStorageCommon::PREVENT_VERTEX_BAKING);
+ if (and_flags) {
+
+ bool use_larger_fvfs = true;
+
+ if (and_flags == RasterizerStorageCommon::PREVENT_COLOR_BAKING) {
+ // in some circumstances, if the modulate is identity, we still allow baking because reading modulate / color
+ // will still be okay to do in the shader with no ill effects
+ if (r_ris.final_modulate == Color(1, 1, 1, 1)) {
+ use_larger_fvfs = false;
+ }
+ }
+
+ // new .. always use large FVF
+ if (use_larger_fvfs) {
+ if (and_flags == RasterizerStorageCommon::PREVENT_COLOR_BAKING) {
+ bdata.joined_item_batch_flags |= RasterizerStorageCommon::USE_MODULATE_FVF;
+ } else {
+ // we need to save on the joined item that it should use large fvf.
+ // This info will then be used in filling and rendering
+ bdata.joined_item_batch_flags |= RasterizerStorageCommon::USE_LARGE_FVF;
+ }
+
+ bdata.joined_item_batch_flags |= r_ris.shader_cache->canvas_item.batch_flags;
+ }
+ }
+ }
+
+ if ((blend_mode == RasterizerStorageGLES3::Shader::CanvasItem::BLEND_MODE_MIX || blend_mode == RasterizerStorageGLES3::Shader::CanvasItem::BLEND_MODE_PMALPHA) && r_ris.item_group_light && !unshaded) {
+
+ // we cannot join lit items easily.
+ // it is possible, but not if they overlap, because
+ // a + light_blend + b + light_blend IS NOT THE SAME AS
+ // a + b + light_blend
+
+ bool light_allow_join = true;
+
+ // this is a quick getout if we have turned off light joining
+ if ((bdata.settings_light_max_join_items == 0) || r_ris.light_region.too_many_lights) {
+ light_allow_join = false;
+ } else {
+ // do light joining...
+
+ // first calculate the light bitfield
+ uint64_t light_bitfield = 0;
+ uint64_t shadow_bitfield = 0;
+ Light *light = r_ris.item_group_light;
+
+ int light_count = -1;
+ while (light) {
+ light_count++;
+ uint64_t light_bit = 1ULL << light_count;
+
+ // note that as a cost of batching, the light culling will be less effective
+ if (p_ci->light_mask & light->item_mask && r_ris.item_group_z >= light->z_min && r_ris.item_group_z <= light->z_max) {
+
+ // Note that with the above test, it is possible to also include a bound check.
+ // Tests so far have indicated better performance without it, but there may be reason to change this at a later stage,
+ // so I leave the line here for reference:
+ // && p_ci->global_rect_cache.intersects_transformed(light->xform_cache, light->rect_cache)) {
+
+ light_bitfield |= light_bit;
+
+ bool has_shadow = light->shadow_buffer.is_valid() && p_ci->light_mask & light->item_shadow_mask;
+
+ if (has_shadow) {
+ shadow_bitfield |= light_bit;
+ }
+ }
+
+ light = light->next_ptr;
+ }
+
+ // now compare to previous
+ if ((r_ris.light_region.light_bitfield != light_bitfield) || (r_ris.light_region.shadow_bitfield != shadow_bitfield)) {
+ light_allow_join = false;
+
+ r_ris.light_region.light_bitfield = light_bitfield;
+ r_ris.light_region.shadow_bitfield = shadow_bitfield;
+ } else {
+ // only do these checks if necessary
+ if (join && (!r_batch_break)) {
+
+ // we still can't join, even if the lights are exactly the same, if there is overlap between the previous and this item
+ if (r_ris.joined_item && light_bitfield) {
+ if ((int)r_ris.joined_item->num_item_refs <= bdata.settings_light_max_join_items) {
+ for (uint32_t r = 0; r < r_ris.joined_item->num_item_refs; r++) {
+ Item *pRefItem = bdata.item_refs[r_ris.joined_item->first_item_ref + r].item;
+ if (p_ci->global_rect_cache.intersects(pRefItem->global_rect_cache)) {
+ light_allow_join = false;
+ break;
+ }
+ }
+
+#ifdef DEBUG_ENABLED
+ if (light_allow_join) {
+ bdata.stats_light_items_joined++;
+ }
+#endif
+
+ } // if below max join items
+ else {
+ // just don't allow joining if above overlap check max items
+ light_allow_join = false;
+ }
+ }
+
+ } // if not batch broken already (no point in doing expensive overlap tests if not needed)
+ } // if bitfields don't match
+ } // if do light joining
+
+ if (!light_allow_join) {
+ // can't join
+ join = false;
+ // we also dont want to allow joining this item with the next item, because the next item could have no lights!
+ r_batch_break = true;
}
} else {
- _set_texture_rect_mode(false);
-
- state.canvas_shader.set_uniform(CanvasShaderGLES3::CLIP_RECT_UV, false);
-
- Vector2 points[4] = {
- p_rect->rect.position,
- p_rect->rect.position + Vector2(p_rect->rect.size.x, 0.0),
- p_rect->rect.position + p_rect->rect.size,
- p_rect->rect.position + Vector2(0.0, p_rect->rect.size.y),
- };
-
- _draw_gui_primitive(4, points, NULL, nullptr);
- }
-}
-
-void RasterizerCanvasGLES3::_canvas_item_render_commands(Item *p_item, Item *current_clip, bool &reclip) {
-
- int cc = p_item->commands.size();
- Item::Command **commands = p_item->commands.ptrw();
-
- for (int i = 0; i < cc; i++) {
-
- Item::Command *c = commands[i];
-
- switch (c->type) {
- case Item::Command::TYPE_LINE: {
-
- Item::CommandLine *line = static_cast(c);
- _set_texture_rect_mode(false);
-
- _bind_canvas_texture(RID(), RID());
-
- glVertexAttrib4f(VS::ARRAY_COLOR, line->color.r, line->color.g, line->color.b, line->color.a);
-
- if (line->width <= 1) {
- Vector2 verts[2] = {
- Vector2(line->from.x, line->from.y),
- Vector2(line->to.x, line->to.y)
- };
-
-#ifdef GLES_OVER_GL
- if (line->antialiased)
- glEnable(GL_LINE_SMOOTH);
-#endif
- //glLineWidth(line->width);
- _draw_gui_primitive(2, verts, NULL, NULL);
-
-#ifdef GLES_OVER_GL
- if (line->antialiased)
- glDisable(GL_LINE_SMOOTH);
-#endif
- } else {
- //thicker line
-
- Vector2 t = (line->from - line->to).normalized().tangent() * line->width * 0.5;
-
- Vector2 verts[4] = {
- line->from - t,
- line->from + t,
- line->to + t,
- line->to - t,
- };
-
- //glLineWidth(line->width);
- _draw_gui_primitive(4, verts, NULL, NULL);
-#ifdef GLES_OVER_GL
- if (line->antialiased) {
- glEnable(GL_LINE_SMOOTH);
- for (int j = 0; j < 4; j++) {
- Vector2 vertsl[2] = {
- verts[j],
- verts[(j + 1) % 4],
- };
- _draw_gui_primitive(2, vertsl, NULL, NULL);
- }
- glDisable(GL_LINE_SMOOTH);
- }
-#endif
- }
- } break;
- case Item::Command::TYPE_POLYLINE: {
-
- Item::CommandPolyLine *pline = static_cast(c);
- _set_texture_rect_mode(false);
-
- _bind_canvas_texture(RID(), RID());
-
- if (pline->triangles.size()) {
-
- _draw_generic(GL_TRIANGLE_STRIP, pline->triangles.size(), pline->triangles.ptr(), NULL, pline->triangle_colors.ptr(), pline->triangle_colors.size() == 1);
-#ifdef GLES_OVER_GL
- glEnable(GL_LINE_SMOOTH);
- if (pline->multiline) {
- //needs to be different
- } else {
- _draw_generic(GL_LINE_LOOP, pline->lines.size(), pline->lines.ptr(), NULL, pline->line_colors.ptr(), pline->line_colors.size() == 1);
- }
- glDisable(GL_LINE_SMOOTH);
-#endif
- } else {
-
-#ifdef GLES_OVER_GL
- if (pline->antialiased)
- glEnable(GL_LINE_SMOOTH);
-#endif
-
- if (pline->multiline) {
- int todo = pline->lines.size() / 2;
- int max_per_call = data.polygon_buffer_size / (sizeof(real_t) * 4);
- int offset = 0;
-
- while (todo) {
- int to_draw = MIN(max_per_call, todo);
- _draw_generic(GL_LINES, to_draw * 2, &pline->lines.ptr()[offset], NULL, pline->line_colors.size() == 1 ? pline->line_colors.ptr() : &pline->line_colors.ptr()[offset], pline->line_colors.size() == 1);
- todo -= to_draw;
- offset += to_draw * 2;
- }
-
- } else {
-
- _draw_generic(GL_LINE_STRIP, pline->lines.size(), pline->lines.ptr(), NULL, pline->line_colors.ptr(), pline->line_colors.size() == 1);
- }
-
-#ifdef GLES_OVER_GL
- if (pline->antialiased)
- glDisable(GL_LINE_SMOOTH);
-#endif
- }
-
- } break;
- case Item::Command::TYPE_RECT: {
-
- Item::CommandRect *rect = static_cast(c);
-
- //set color
- glVertexAttrib4f(VS::ARRAY_COLOR, rect->modulate.r, rect->modulate.g, rect->modulate.b, rect->modulate.a);
-
- RasterizerStorageGLES3::Texture *texture = _bind_canvas_texture(rect->texture, rect->normal_map);
-
- if (use_nvidia_rect_workaround) {
- render_rect_nvidia_workaround(rect, texture);
- } else {
-
- _set_texture_rect_mode(true);
-
- if (texture) {
-
- bool untile = false;
-
- if (rect->flags & CANVAS_RECT_TILE && !(texture->flags & VS::TEXTURE_FLAG_REPEAT)) {
- glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
- glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
- untile = true;
- }
-
- Size2 texpixel_size(1.0 / texture->width, 1.0 / texture->height);
- Rect2 src_rect = (rect->flags & CANVAS_RECT_REGION) ? Rect2(rect->source.position * texpixel_size, rect->source.size * texpixel_size) : Rect2(0, 0, 1, 1);
- Rect2 dst_rect = Rect2(rect->rect.position, rect->rect.size);
-
- if (dst_rect.size.width < 0) {
- dst_rect.position.x += dst_rect.size.width;
- dst_rect.size.width *= -1;
- }
- if (dst_rect.size.height < 0) {
- dst_rect.position.y += dst_rect.size.height;
- dst_rect.size.height *= -1;
- }
-
- if (rect->flags & CANVAS_RECT_FLIP_H) {
- src_rect.size.x *= -1;
- }
-
- if (rect->flags & CANVAS_RECT_FLIP_V) {
- src_rect.size.y *= -1;
- }
-
- if (rect->flags & CANVAS_RECT_TRANSPOSE) {
- dst_rect.size.x *= -1; // Encoding in the dst_rect.z uniform
- }
-
- state.canvas_shader.set_uniform(CanvasShaderGLES3::COLOR_TEXPIXEL_SIZE, texpixel_size);
-
- state.canvas_shader.set_uniform(CanvasShaderGLES3::DST_RECT, Color(dst_rect.position.x, dst_rect.position.y, dst_rect.size.x, dst_rect.size.y));
- state.canvas_shader.set_uniform(CanvasShaderGLES3::SRC_RECT, Color(src_rect.position.x, src_rect.position.y, src_rect.size.x, src_rect.size.y));
- state.canvas_shader.set_uniform(CanvasShaderGLES3::CLIP_RECT_UV, rect->flags & CANVAS_RECT_CLIP_UV);
-
- glDrawArrays(GL_TRIANGLE_FAN, 0, 4);
- storage->info.render._2d_draw_call_count++;
-
- if (untile) {
- glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
- glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
- }
-
- } else {
- Rect2 dst_rect = Rect2(rect->rect.position, rect->rect.size);
-
- if (dst_rect.size.width < 0) {
- dst_rect.position.x += dst_rect.size.width;
- dst_rect.size.width *= -1;
- }
- if (dst_rect.size.height < 0) {
- dst_rect.position.y += dst_rect.size.height;
- dst_rect.size.height *= -1;
- }
-
- state.canvas_shader.set_uniform(CanvasShaderGLES3::DST_RECT, Color(dst_rect.position.x, dst_rect.position.y, dst_rect.size.x, dst_rect.size.y));
- state.canvas_shader.set_uniform(CanvasShaderGLES3::SRC_RECT, Color(0, 0, 1, 1));
- state.canvas_shader.set_uniform(CanvasShaderGLES3::CLIP_RECT_UV, false);
- glDrawArrays(GL_TRIANGLE_FAN, 0, 4);
- storage->info.render._2d_draw_call_count++;
- }
- } // if not use nvidia workaround
- } break;
- case Item::Command::TYPE_NINEPATCH: {
-
- Item::CommandNinePatch *np = static_cast(c);
-
- _set_texture_rect_mode(true, true);
-
- glVertexAttrib4f(VS::ARRAY_COLOR, np->color.r, np->color.g, np->color.b, np->color.a);
-
- RasterizerStorageGLES3::Texture *texture = _bind_canvas_texture(np->texture, np->normal_map);
-
- Size2 texpixel_size;
-
- if (!texture) {
-
- texpixel_size = Size2(1, 1);
-
- state.canvas_shader.set_uniform(CanvasShaderGLES3::SRC_RECT, Color(0, 0, 1, 1));
-
- } else {
-
- if (np->source != Rect2()) {
- texpixel_size = Size2(1.0 / np->source.size.width, 1.0 / np->source.size.height);
- state.canvas_shader.set_uniform(CanvasShaderGLES3::SRC_RECT, Color(np->source.position.x / texture->width, np->source.position.y / texture->height, np->source.size.x / texture->width, np->source.size.y / texture->height));
- } else {
- texpixel_size = Size2(1.0 / texture->width, 1.0 / texture->height);
- state.canvas_shader.set_uniform(CanvasShaderGLES3::SRC_RECT, Color(0, 0, 1, 1));
- }
- }
-
- state.canvas_shader.set_uniform(CanvasShaderGLES3::COLOR_TEXPIXEL_SIZE, texpixel_size);
- state.canvas_shader.set_uniform(CanvasShaderGLES3::CLIP_RECT_UV, false);
- state.canvas_shader.set_uniform(CanvasShaderGLES3::NP_REPEAT_H, int(np->axis_x));
- state.canvas_shader.set_uniform(CanvasShaderGLES3::NP_REPEAT_V, int(np->axis_y));
- state.canvas_shader.set_uniform(CanvasShaderGLES3::NP_DRAW_CENTER, np->draw_center);
- state.canvas_shader.set_uniform(CanvasShaderGLES3::NP_MARGINS, Color(np->margin[MARGIN_LEFT], np->margin[MARGIN_TOP], np->margin[MARGIN_RIGHT], np->margin[MARGIN_BOTTOM]));
- state.canvas_shader.set_uniform(CanvasShaderGLES3::DST_RECT, Color(np->rect.position.x, np->rect.position.y, np->rect.size.x, np->rect.size.y));
-
- glDrawArrays(GL_TRIANGLE_FAN, 0, 4);
-
- storage->info.render._2d_draw_call_count++;
- } break;
- case Item::Command::TYPE_PRIMITIVE: {
-
- Item::CommandPrimitive *primitive = static_cast(c);
- _set_texture_rect_mode(false);
-
- ERR_CONTINUE(primitive->points.size() < 1);
-
- RasterizerStorageGLES3::Texture *texture = _bind_canvas_texture(primitive->texture, primitive->normal_map);
-
- if (texture) {
- Size2 texpixel_size(1.0 / texture->width, 1.0 / texture->height);
- state.canvas_shader.set_uniform(CanvasShaderGLES3::COLOR_TEXPIXEL_SIZE, texpixel_size);
- }
- if (primitive->colors.size() == 1 && primitive->points.size() > 1) {
-
- Color col = primitive->colors[0];
- glVertexAttrib4f(VS::ARRAY_COLOR, col.r, col.g, col.b, col.a);
-
- } else if (primitive->colors.empty()) {
- glVertexAttrib4f(VS::ARRAY_COLOR, 1, 1, 1, 1);
- }
-
- _draw_gui_primitive(primitive->points.size(), primitive->points.ptr(), primitive->colors.ptr(), primitive->uvs.ptr());
-
- } break;
- case Item::Command::TYPE_POLYGON: {
-
- Item::CommandPolygon *polygon = static_cast(c);
- _set_texture_rect_mode(false);
-
- RasterizerStorageGLES3::Texture *texture = _bind_canvas_texture(polygon->texture, polygon->normal_map);
-
- if (texture) {
- Size2 texpixel_size(1.0 / texture->width, 1.0 / texture->height);
- state.canvas_shader.set_uniform(CanvasShaderGLES3::COLOR_TEXPIXEL_SIZE, texpixel_size);
- }
-
- _draw_polygon(polygon->indices.ptr(), polygon->count, polygon->points.size(), polygon->points.ptr(), polygon->uvs.ptr(), polygon->colors.ptr(), polygon->colors.size() == 1, polygon->bones.ptr(), polygon->weights.ptr());
-#ifdef GLES_OVER_GL
- if (polygon->antialiased) {
- glEnable(GL_LINE_SMOOTH);
- if (polygon->antialiasing_use_indices) {
- _draw_generic_indices(GL_LINE_STRIP, polygon->indices.ptr(), polygon->count, polygon->points.size(), polygon->points.ptr(), polygon->uvs.ptr(), polygon->colors.ptr(), polygon->colors.size() == 1);
- } else {
- _draw_generic(GL_LINE_LOOP, polygon->points.size(), polygon->points.ptr(), polygon->uvs.ptr(), polygon->colors.ptr(), polygon->colors.size() == 1);
- }
- glDisable(GL_LINE_SMOOTH);
- }
-#endif
-
- } break;
- case Item::Command::TYPE_MESH: {
-
- Item::CommandMesh *mesh = static_cast(c);
- _set_texture_rect_mode(false);
-
- RasterizerStorageGLES3::Texture *texture = _bind_canvas_texture(mesh->texture, mesh->normal_map);
-
- if (texture) {
- Size2 texpixel_size(1.0 / texture->width, 1.0 / texture->height);
- state.canvas_shader.set_uniform(CanvasShaderGLES3::COLOR_TEXPIXEL_SIZE, texpixel_size);
- }
-
- state.canvas_shader.set_uniform(CanvasShaderGLES3::MODELVIEW_MATRIX, state.final_transform * mesh->transform);
-
- RasterizerStorageGLES3::Mesh *mesh_data = storage->mesh_owner.getornull(mesh->mesh);
- if (mesh_data) {
-
- for (int j = 0; j < mesh_data->surfaces.size(); j++) {
- RasterizerStorageGLES3::Surface *s = mesh_data->surfaces[j];
- // materials are ignored in 2D meshes, could be added but many things (ie, lighting mode, reading from screen, etc) would break as they are not meant be set up at this point of drawing
- glBindVertexArray(s->array_id);
-
- glVertexAttrib4f(VS::ARRAY_COLOR, mesh->modulate.r, mesh->modulate.g, mesh->modulate.b, mesh->modulate.a);
-
- if (s->index_array_len) {
- glDrawElements(gl_primitive[s->primitive], s->index_array_len, (s->array_len >= (1 << 16)) ? GL_UNSIGNED_INT : GL_UNSIGNED_SHORT, 0);
- } else {
- glDrawArrays(gl_primitive[s->primitive], 0, s->array_len);
- }
- storage->info.render._2d_draw_call_count++;
-
- glBindVertexArray(0);
- }
- }
- state.canvas_shader.set_uniform(CanvasShaderGLES3::MODELVIEW_MATRIX, state.final_transform);
-
- } break;
- case Item::Command::TYPE_MULTIMESH: {
-
- Item::CommandMultiMesh *mmesh = static_cast(c);
-
- RasterizerStorageGLES3::MultiMesh *multi_mesh = storage->multimesh_owner.getornull(mmesh->multimesh);
-
- if (!multi_mesh)
- break;
-
- RasterizerStorageGLES3::Mesh *mesh_data = storage->mesh_owner.getornull(multi_mesh->mesh);
-
- if (!mesh_data)
- break;
-
- RasterizerStorageGLES3::Texture *texture = _bind_canvas_texture(mmesh->texture, mmesh->normal_map);
-
- state.canvas_shader.set_conditional(CanvasShaderGLES3::USE_INSTANCE_CUSTOM, multi_mesh->custom_data_format != VS::MULTIMESH_CUSTOM_DATA_NONE);
- state.canvas_shader.set_conditional(CanvasShaderGLES3::USE_INSTANCING, true);
- //reset shader and force rebind
- state.using_texture_rect = true;
- _set_texture_rect_mode(false);
-
- if (texture) {
- Size2 texpixel_size(1.0 / texture->width, 1.0 / texture->height);
- state.canvas_shader.set_uniform(CanvasShaderGLES3::COLOR_TEXPIXEL_SIZE, texpixel_size);
- }
-
- int amount = MIN(multi_mesh->size, multi_mesh->visible_instances);
-
- if (amount == -1) {
- amount = multi_mesh->size;
- }
-
- for (int j = 0; j < mesh_data->surfaces.size(); j++) {
- RasterizerStorageGLES3::Surface *s = mesh_data->surfaces[j];
- // materials are ignored in 2D meshes, could be added but many things (ie, lighting mode, reading from screen, etc) would break as they are not meant be set up at this point of drawing
- glBindVertexArray(s->instancing_array_id);
-
- glBindBuffer(GL_ARRAY_BUFFER, multi_mesh->buffer); //modify the buffer
-
- int stride = (multi_mesh->xform_floats + multi_mesh->color_floats + multi_mesh->custom_data_floats) * 4;
- glEnableVertexAttribArray(8);
- glVertexAttribPointer(8, 4, GL_FLOAT, GL_FALSE, stride, CAST_INT_TO_UCHAR_PTR(0));
- glVertexAttribDivisor(8, 1);
- glEnableVertexAttribArray(9);
- glVertexAttribPointer(9, 4, GL_FLOAT, GL_FALSE, stride, CAST_INT_TO_UCHAR_PTR(4 * 4));
- glVertexAttribDivisor(9, 1);
-
- int color_ofs;
-
- if (multi_mesh->transform_format == VS::MULTIMESH_TRANSFORM_3D) {
- glEnableVertexAttribArray(10);
- glVertexAttribPointer(10, 4, GL_FLOAT, GL_FALSE, stride, CAST_INT_TO_UCHAR_PTR(8 * 4));
- glVertexAttribDivisor(10, 1);
- color_ofs = 12 * 4;
- } else {
- glDisableVertexAttribArray(10);
- glVertexAttrib4f(10, 0, 0, 1, 0);
- color_ofs = 8 * 4;
- }
-
- int custom_data_ofs = color_ofs;
-
- switch (multi_mesh->color_format) {
-
- case VS::MULTIMESH_COLOR_MAX:
- case VS::MULTIMESH_COLOR_NONE: {
- glDisableVertexAttribArray(11);
- glVertexAttrib4f(11, 1, 1, 1, 1);
- } break;
- case VS::MULTIMESH_COLOR_8BIT: {
- glEnableVertexAttribArray(11);
- glVertexAttribPointer(11, 4, GL_UNSIGNED_BYTE, GL_TRUE, stride, CAST_INT_TO_UCHAR_PTR(color_ofs));
- glVertexAttribDivisor(11, 1);
- custom_data_ofs += 4;
-
- } break;
- case VS::MULTIMESH_COLOR_FLOAT: {
- glEnableVertexAttribArray(11);
- glVertexAttribPointer(11, 4, GL_FLOAT, GL_FALSE, stride, CAST_INT_TO_UCHAR_PTR(color_ofs));
- glVertexAttribDivisor(11, 1);
- custom_data_ofs += 4 * 4;
- } break;
- }
-
- switch (multi_mesh->custom_data_format) {
-
- case VS::MULTIMESH_CUSTOM_DATA_MAX:
- case VS::MULTIMESH_CUSTOM_DATA_NONE: {
- glDisableVertexAttribArray(12);
- glVertexAttrib4f(12, 1, 1, 1, 1);
- } break;
- case VS::MULTIMESH_CUSTOM_DATA_8BIT: {
- glEnableVertexAttribArray(12);
- glVertexAttribPointer(12, 4, GL_UNSIGNED_BYTE, GL_TRUE, stride, CAST_INT_TO_UCHAR_PTR(custom_data_ofs));
- glVertexAttribDivisor(12, 1);
-
- } break;
- case VS::MULTIMESH_CUSTOM_DATA_FLOAT: {
- glEnableVertexAttribArray(12);
- glVertexAttribPointer(12, 4, GL_FLOAT, GL_FALSE, stride, CAST_INT_TO_UCHAR_PTR(custom_data_ofs));
- glVertexAttribDivisor(12, 1);
- } break;
- }
-
- if (s->index_array_len) {
- glDrawElementsInstanced(gl_primitive[s->primitive], s->index_array_len, (s->array_len >= (1 << 16)) ? GL_UNSIGNED_INT : GL_UNSIGNED_SHORT, 0, amount);
- } else {
- glDrawArraysInstanced(gl_primitive[s->primitive], 0, s->array_len, amount);
- }
- storage->info.render._2d_draw_call_count++;
-
- glBindVertexArray(0);
- }
-
- state.canvas_shader.set_conditional(CanvasShaderGLES3::USE_INSTANCE_CUSTOM, false);
- state.canvas_shader.set_conditional(CanvasShaderGLES3::USE_INSTANCING, false);
- state.using_texture_rect = true;
- _set_texture_rect_mode(false);
-
- } break;
- case Item::Command::TYPE_PARTICLES: {
-
- Item::CommandParticles *particles_cmd = static_cast(c);
-
- RasterizerStorageGLES3::Particles *particles = storage->particles_owner.getornull(particles_cmd->particles);
- if (!particles)
- break;
-
- if (particles->inactive && !particles->emitting)
- break;
-
- glVertexAttrib4f(VS::ARRAY_COLOR, 1, 1, 1, 1); //not used, so keep white
-
- VisualServerRaster::redraw_request();
-
- storage->particles_request_process(particles_cmd->particles);
- //enable instancing
-
- state.canvas_shader.set_conditional(CanvasShaderGLES3::USE_INSTANCE_CUSTOM, true);
- state.canvas_shader.set_conditional(CanvasShaderGLES3::USE_PARTICLES, true);
- state.canvas_shader.set_conditional(CanvasShaderGLES3::USE_INSTANCING, true);
- //reset shader and force rebind
- state.using_texture_rect = true;
- _set_texture_rect_mode(false);
-
- RasterizerStorageGLES3::Texture *texture = _bind_canvas_texture(particles_cmd->texture, particles_cmd->normal_map);
-
- if (texture) {
- Size2 texpixel_size(1.0 / texture->width, 1.0 / texture->height);
- state.canvas_shader.set_uniform(CanvasShaderGLES3::COLOR_TEXPIXEL_SIZE, texpixel_size);
- } else {
- state.canvas_shader.set_uniform(CanvasShaderGLES3::COLOR_TEXPIXEL_SIZE, Vector2(1.0, 1.0));
- }
-
- if (!particles->use_local_coords) {
-
- Transform2D inv_xf;
- inv_xf.set_axis(0, Vector2(particles->emission_transform.basis.get_axis(0).x, particles->emission_transform.basis.get_axis(0).y));
- inv_xf.set_axis(1, Vector2(particles->emission_transform.basis.get_axis(1).x, particles->emission_transform.basis.get_axis(1).y));
- inv_xf.set_origin(Vector2(particles->emission_transform.get_origin().x, particles->emission_transform.get_origin().y));
- inv_xf.affine_invert();
-
- state.canvas_shader.set_uniform(CanvasShaderGLES3::MODELVIEW_MATRIX, state.final_transform * inv_xf);
- }
-
- glBindVertexArray(data.particle_quad_array); //use particle quad array
- glBindBuffer(GL_ARRAY_BUFFER, particles->particle_buffers[0]); //bind particle buffer
-
- int stride = sizeof(float) * 4 * 6;
-
- int amount = particles->amount;
-
- if (particles->draw_order != VS::PARTICLES_DRAW_ORDER_LIFETIME) {
-
- glEnableVertexAttribArray(8); //xform x
- glVertexAttribPointer(8, 4, GL_FLOAT, GL_FALSE, stride, CAST_INT_TO_UCHAR_PTR(sizeof(float) * 4 * 3));
- glVertexAttribDivisor(8, 1);
- glEnableVertexAttribArray(9); //xform y
- glVertexAttribPointer(9, 4, GL_FLOAT, GL_FALSE, stride, CAST_INT_TO_UCHAR_PTR(sizeof(float) * 4 * 4));
- glVertexAttribDivisor(9, 1);
- glEnableVertexAttribArray(10); //xform z
- glVertexAttribPointer(10, 4, GL_FLOAT, GL_FALSE, stride, CAST_INT_TO_UCHAR_PTR(sizeof(float) * 4 * 5));
- glVertexAttribDivisor(10, 1);
- glEnableVertexAttribArray(11); //color
- glVertexAttribPointer(11, 4, GL_FLOAT, GL_FALSE, stride, NULL);
- glVertexAttribDivisor(11, 1);
- glEnableVertexAttribArray(12); //custom
- glVertexAttribPointer(12, 4, GL_FLOAT, GL_FALSE, stride, CAST_INT_TO_UCHAR_PTR(sizeof(float) * 4 * 2));
- glVertexAttribDivisor(12, 1);
-
- glDrawArraysInstanced(GL_TRIANGLE_FAN, 0, 4, amount);
- storage->info.render._2d_draw_call_count++;
- } else {
- //split
- int split = int(Math::ceil(particles->phase * particles->amount));
-
- if (amount - split > 0) {
- glEnableVertexAttribArray(8); //xform x
- glVertexAttribPointer(8, 4, GL_FLOAT, GL_FALSE, stride, CAST_INT_TO_UCHAR_PTR(stride * split + sizeof(float) * 4 * 3));
- glVertexAttribDivisor(8, 1);
- glEnableVertexAttribArray(9); //xform y
- glVertexAttribPointer(9, 4, GL_FLOAT, GL_FALSE, stride, CAST_INT_TO_UCHAR_PTR(stride * split + sizeof(float) * 4 * 4));
- glVertexAttribDivisor(9, 1);
- glEnableVertexAttribArray(10); //xform z
- glVertexAttribPointer(10, 4, GL_FLOAT, GL_FALSE, stride, CAST_INT_TO_UCHAR_PTR(stride * split + sizeof(float) * 4 * 5));
- glVertexAttribDivisor(10, 1);
- glEnableVertexAttribArray(11); //color
- glVertexAttribPointer(11, 4, GL_FLOAT, GL_FALSE, stride, CAST_INT_TO_UCHAR_PTR(stride * split + 0));
- glVertexAttribDivisor(11, 1);
- glEnableVertexAttribArray(12); //custom
- glVertexAttribPointer(12, 4, GL_FLOAT, GL_FALSE, stride, CAST_INT_TO_UCHAR_PTR(stride * split + sizeof(float) * 4 * 2));
- glVertexAttribDivisor(12, 1);
-
- glDrawArraysInstanced(GL_TRIANGLE_FAN, 0, 4, amount - split);
- storage->info.render._2d_draw_call_count++;
- }
-
- if (split > 0) {
- glEnableVertexAttribArray(8); //xform x
- glVertexAttribPointer(8, 4, GL_FLOAT, GL_FALSE, stride, CAST_INT_TO_UCHAR_PTR(sizeof(float) * 4 * 3));
- glVertexAttribDivisor(8, 1);
- glEnableVertexAttribArray(9); //xform y
- glVertexAttribPointer(9, 4, GL_FLOAT, GL_FALSE, stride, CAST_INT_TO_UCHAR_PTR(sizeof(float) * 4 * 4));
- glVertexAttribDivisor(9, 1);
- glEnableVertexAttribArray(10); //xform z
- glVertexAttribPointer(10, 4, GL_FLOAT, GL_FALSE, stride, CAST_INT_TO_UCHAR_PTR(sizeof(float) * 4 * 5));
- glVertexAttribDivisor(10, 1);
- glEnableVertexAttribArray(11); //color
- glVertexAttribPointer(11, 4, GL_FLOAT, GL_FALSE, stride, NULL);
- glVertexAttribDivisor(11, 1);
- glEnableVertexAttribArray(12); //custom
- glVertexAttribPointer(12, 4, GL_FLOAT, GL_FALSE, stride, CAST_INT_TO_UCHAR_PTR(sizeof(float) * 4 * 2));
- glVertexAttribDivisor(12, 1);
-
- glDrawArraysInstanced(GL_TRIANGLE_FAN, 0, 4, split);
- storage->info.render._2d_draw_call_count++;
- }
- }
-
- glBindVertexArray(0);
-
- state.canvas_shader.set_conditional(CanvasShaderGLES3::USE_INSTANCE_CUSTOM, false);
- state.canvas_shader.set_conditional(CanvasShaderGLES3::USE_PARTICLES, false);
- state.canvas_shader.set_conditional(CanvasShaderGLES3::USE_INSTANCING, false);
- state.using_texture_rect = true;
- _set_texture_rect_mode(false);
-
- } break;
- case Item::Command::TYPE_CIRCLE: {
-
- _set_texture_rect_mode(false);
-
- Item::CommandCircle *circle = static_cast(c);
- static const int numpoints = 32;
- Vector2 points[numpoints + 1];
- points[numpoints] = circle->pos;
- int indices[numpoints * 3];
-
- for (int j = 0; j < numpoints; j++) {
-
- points[j] = circle->pos + Vector2(Math::sin(j * Math_PI * 2.0 / numpoints), Math::cos(j * Math_PI * 2.0 / numpoints)) * circle->radius;
- indices[j * 3 + 0] = j;
- indices[j * 3 + 1] = (j + 1) % numpoints;
- indices[j * 3 + 2] = numpoints;
- }
-
- _bind_canvas_texture(RID(), RID());
- _draw_polygon(indices, numpoints * 3, numpoints + 1, points, NULL, &circle->color, true, NULL, NULL);
-
- //_draw_polygon(numpoints*3,indices,points,NULL,&circle->color,RID(),true);
- //canvas_draw_circle(circle->indices.size(),circle->indices.ptr(),circle->points.ptr(),circle->uvs.ptr(),circle->colors.ptr(),circle->texture,circle->colors.size()==1);
- } break;
- case Item::Command::TYPE_TRANSFORM: {
-
- Item::CommandTransform *transform = static_cast(c);
- state.extra_matrix = transform->xform;
- state.canvas_shader.set_uniform(CanvasShaderGLES3::EXTRA_MATRIX, state.extra_matrix);
-
- } break;
- case Item::Command::TYPE_CLIP_IGNORE: {
-
- Item::CommandClipIgnore *ci = static_cast(c);
- if (current_clip) {
-
- if (ci->ignore != reclip) {
- if (ci->ignore) {
-
- glDisable(GL_SCISSOR_TEST);
- reclip = true;
- } else {
-
- glEnable(GL_SCISSOR_TEST);
- //glScissor(viewport.x+current_clip->final_clip_rect.pos.x,viewport.y+ (viewport.height-(current_clip->final_clip_rect.pos.y+current_clip->final_clip_rect.size.height)),
- //current_clip->final_clip_rect.size.width,current_clip->final_clip_rect.size.height);
- int y = storage->frame.current_rt->height - (current_clip->final_clip_rect.position.y + current_clip->final_clip_rect.size.y);
- if (storage->frame.current_rt->flags[RasterizerStorage::RENDER_TARGET_VFLIP])
- y = current_clip->final_clip_rect.position.y;
-
- glScissor(current_clip->final_clip_rect.position.x, y, current_clip->final_clip_rect.size.x, current_clip->final_clip_rect.size.y);
-
- reclip = false;
- }
- }
- }
-
- } break;
+ // if the last item had lights, we should not join it to this one (which has no lights)
+ if (r_ris.light_region.light_bitfield || r_ris.light_region.shadow_bitfield) {
+ join = false;
+
+ // setting these to zero ensures that any following item with lights will, by definition,
+ // be affected by a different set of lights, and thus prevent a join
+ r_ris.light_region.light_bitfield = 0;
+ r_ris.light_region.shadow_bitfield = 0;
}
}
-}
-void RasterizerCanvasGLES3::_copy_texscreen(const Rect2 &p_rect) {
-
- ERR_FAIL_COND_MSG(storage->frame.current_rt->effects.mip_maps[0].sizes.size() == 0, "Can't use screen texture copying in a render target configured without copy buffers.");
-
- glDisable(GL_BLEND);
-
- state.canvas_texscreen_used = true;
- //blur diffuse into effect mipmaps using separatable convolution
- //storage->shaders.copy.set_conditional(CopyShaderGLES3::GAUSSIAN_HORIZONTAL,true);
-
- Vector2 wh(storage->frame.current_rt->width, storage->frame.current_rt->height);
-
- Color blur_section(p_rect.position.x / wh.x, p_rect.position.y / wh.y, p_rect.size.x / wh.x, p_rect.size.y / wh.y);
-
- if (p_rect != Rect2()) {
-
- scene_render->state.effect_blur_shader.set_conditional(EffectBlurShaderGLES3::USE_BLUR_SECTION, true);
- storage->shaders.copy.set_conditional(CopyShaderGLES3::USE_COPY_SECTION, true);
+ if (reclip) {
+ join = false;
}
- glBindFramebuffer(GL_FRAMEBUFFER, storage->frame.current_rt->effects.mip_maps[0].sizes[0].fbo);
- glActiveTexture(GL_TEXTURE0);
- glBindTexture(GL_TEXTURE_2D, storage->frame.current_rt->color);
-
- storage->shaders.copy.bind();
- storage->shaders.copy.set_uniform(CopyShaderGLES3::COPY_SECTION, blur_section);
-
- scene_render->_copy_screen();
-
- for (int i = 0; i < storage->frame.current_rt->effects.mip_maps[1].sizes.size(); i++) {
-
- int vp_w = storage->frame.current_rt->effects.mip_maps[1].sizes[i].width;
- int vp_h = storage->frame.current_rt->effects.mip_maps[1].sizes[i].height;
- glViewport(0, 0, vp_w, vp_h);
- //horizontal pass
- scene_render->state.effect_blur_shader.set_conditional(EffectBlurShaderGLES3::GAUSSIAN_HORIZONTAL, true);
- scene_render->state.effect_blur_shader.bind();
- scene_render->state.effect_blur_shader.set_uniform(EffectBlurShaderGLES3::PIXEL_SIZE, Vector2(1.0 / vp_w, 1.0 / vp_h));
- scene_render->state.effect_blur_shader.set_uniform(EffectBlurShaderGLES3::LOD, float(i));
- scene_render->state.effect_blur_shader.set_uniform(EffectBlurShaderGLES3::BLUR_SECTION, blur_section);
- glActiveTexture(GL_TEXTURE0);
- glBindTexture(GL_TEXTURE_2D, storage->frame.current_rt->effects.mip_maps[0].color); //previous level, since mipmaps[0] starts one level bigger
- glBindFramebuffer(GL_FRAMEBUFFER, storage->frame.current_rt->effects.mip_maps[1].sizes[i].fbo);
-
- scene_render->_copy_screen();
-
- scene_render->state.effect_blur_shader.set_conditional(EffectBlurShaderGLES3::GAUSSIAN_HORIZONTAL, false);
-
- //vertical pass
- scene_render->state.effect_blur_shader.set_conditional(EffectBlurShaderGLES3::GAUSSIAN_VERTICAL, true);
- scene_render->state.effect_blur_shader.bind();
- scene_render->state.effect_blur_shader.set_uniform(EffectBlurShaderGLES3::PIXEL_SIZE, Vector2(1.0 / vp_w, 1.0 / vp_h));
- scene_render->state.effect_blur_shader.set_uniform(EffectBlurShaderGLES3::LOD, float(i));
- scene_render->state.effect_blur_shader.set_uniform(EffectBlurShaderGLES3::BLUR_SECTION, blur_section);
- glActiveTexture(GL_TEXTURE0);
- glBindTexture(GL_TEXTURE_2D, storage->frame.current_rt->effects.mip_maps[1].color);
- glBindFramebuffer(GL_FRAMEBUFFER, storage->frame.current_rt->effects.mip_maps[0].sizes[i + 1].fbo); //next level, since mipmaps[0] starts one level bigger
-
- scene_render->_copy_screen();
-
- scene_render->state.effect_blur_shader.set_conditional(EffectBlurShaderGLES3::GAUSSIAN_VERTICAL, false);
+ // non rects will break the batching anyway, we don't want to record item changes, detect this
+ if (!r_batch_break && _detect_item_batch_break(r_ris, p_ci, r_batch_break)) {
+ join = false;
+ r_batch_break = true;
}
- scene_render->state.effect_blur_shader.set_conditional(EffectBlurShaderGLES3::USE_BLUR_SECTION, false);
- storage->shaders.copy.set_conditional(CopyShaderGLES3::USE_COPY_SECTION, false);
-
- glBindFramebuffer(GL_FRAMEBUFFER, storage->frame.current_rt->fbo); //back to front
- glViewport(0, 0, storage->frame.current_rt->width, storage->frame.current_rt->height);
-
- // back to canvas, force rebind
- state.using_texture_rect = true;
- _set_texture_rect_mode(false);
-
- _bind_canvas_texture(state.current_tex, state.current_normal, true);
-
- glEnable(GL_BLEND);
+ return join;
}
-void RasterizerCanvasGLES3::canvas_render_items(Item *p_item_list, int p_z, const Color &p_modulate, Light *p_light, const Transform2D &p_transform) {
+void RasterizerCanvasGLES3::canvas_render_items_implementation(Item *p_item_list, int p_z, const Color &p_modulate, Light *p_light, const Transform2D &p_base_transform) {
- Item *current_clip = NULL;
- RasterizerStorageGLES3::Shader *shader_cache = NULL;
-
- bool rebind_shader = true;
+ // parameters are easier to pass around in a structure
+ RenderItemState ris;
+ ris.item_group_z = p_z;
+ ris.item_group_modulate = p_modulate;
+ ris.item_group_light = p_light;
+ ris.item_group_base_transform = p_base_transform;
+ ris.prev_distance_field = false;
glBindBuffer(GL_UNIFORM_BUFFER, state.canvas_item_ubo);
glBufferData(GL_UNIFORM_BUFFER, sizeof(CanvasItemUBO), &state.canvas_item_ubo_data, GL_DYNAMIC_DRAW);
@@ -1466,939 +1931,369 @@ void RasterizerCanvasGLES3::canvas_render_items(Item *p_item_list, int p_z, cons
glActiveTexture(GL_TEXTURE0);
glBindTexture(GL_TEXTURE_2D, storage->resources.white_tex);
- int last_blend_mode = -1;
+ // state.canvas_shader.set_conditional(CanvasShaderGLES3::USE_SKELETON, false);
+ // state.current_tex = RID();
+ // state.current_tex_ptr = NULL;
+ // state.current_normal = RID();
+ // state.canvas_texscreen_used = false;
+ // glActiveTexture(GL_TEXTURE0);
+ // glBindTexture(GL_TEXTURE_2D, storage->resources.white_tex);
- RID canvas_last_material;
-
- bool prev_distance_field = false;
- bool prev_use_skeleton = false;
-
- while (p_item_list) {
-
- Item *ci = p_item_list;
- storage->info.render._2d_item_count++;
-
- if (prev_distance_field != ci->distance_field) {
-
- state.canvas_shader.set_conditional(CanvasShaderGLES3::USE_DISTANCE_FIELD, ci->distance_field);
- prev_distance_field = ci->distance_field;
- rebind_shader = true;
+ if (bdata.settings_use_batching) {
+ for (int j = 0; j < bdata.items_joined.size(); j++) {
+ render_joined_item(bdata.items_joined[j], ris);
}
+ } else {
+ while (p_item_list) {
- if (current_clip != ci->final_clip_owner) {
-
- current_clip = ci->final_clip_owner;
-
- //setup clip
- if (current_clip) {
-
- glEnable(GL_SCISSOR_TEST);
- int y = storage->frame.current_rt->height - (current_clip->final_clip_rect.position.y + current_clip->final_clip_rect.size.y);
- if (storage->frame.current_rt->flags[RasterizerStorage::RENDER_TARGET_VFLIP])
- y = current_clip->final_clip_rect.position.y;
-
- glScissor(current_clip->final_clip_rect.position.x, y, current_clip->final_clip_rect.size.x, current_clip->final_clip_rect.size.y);
-
- } else {
-
- glDisable(GL_SCISSOR_TEST);
- }
+ Item *ci = p_item_list;
+ _legacy_canvas_render_item(ci, ris);
+ p_item_list = p_item_list->next;
}
-
- if (ci->copy_back_buffer) {
-
- if (ci->copy_back_buffer->full) {
-
- _copy_texscreen(Rect2());
- } else {
- _copy_texscreen(ci->copy_back_buffer->rect);
- }
- }
-
- RasterizerStorageGLES3::Skeleton *skeleton = NULL;
-
- {
- //skeleton handling
- if (ci->skeleton.is_valid() && storage->skeleton_owner.owns(ci->skeleton)) {
- skeleton = storage->skeleton_owner.get(ci->skeleton);
- if (!skeleton->use_2d) {
- skeleton = NULL;
- } else {
- state.skeleton_transform = p_transform * skeleton->base_transform_2d;
- state.skeleton_transform_inverse = state.skeleton_transform.affine_inverse();
- }
- }
-
- bool use_skeleton = skeleton != NULL;
- if (prev_use_skeleton != use_skeleton) {
- rebind_shader = true;
- state.canvas_shader.set_conditional(CanvasShaderGLES3::USE_SKELETON, use_skeleton);
- prev_use_skeleton = use_skeleton;
- }
-
- if (skeleton) {
- glActiveTexture(GL_TEXTURE0 + storage->config.max_texture_image_units - 4);
- glBindTexture(GL_TEXTURE_2D, skeleton->texture);
- state.using_skeleton = true;
- } else {
- state.using_skeleton = false;
- }
- }
-
- //begin rect
- Item *material_owner = ci->material_owner ? ci->material_owner : ci;
-
- RID material = material_owner->material;
-
- if (material != canvas_last_material || rebind_shader) {
-
- RasterizerStorageGLES3::Material *material_ptr = storage->material_owner.getornull(material);
- RasterizerStorageGLES3::Shader *shader_ptr = NULL;
-
- if (material_ptr) {
-
- shader_ptr = material_ptr->shader;
-
- if (shader_ptr && shader_ptr->mode != VS::SHADER_CANVAS_ITEM) {
- shader_ptr = NULL; //do not use non canvasitem shader
- }
- }
-
- if (shader_ptr) {
-
- if (shader_ptr->canvas_item.uses_screen_texture && !state.canvas_texscreen_used) {
- //copy if not copied before
- _copy_texscreen(Rect2());
-
- // blend mode will have been enabled so make sure we disable it again later on
- last_blend_mode = last_blend_mode != RasterizerStorageGLES3::Shader::CanvasItem::BLEND_MODE_DISABLED ? last_blend_mode : -1;
- }
-
- if (shader_ptr != shader_cache || rebind_shader) {
-
- if (shader_ptr->canvas_item.uses_time) {
- VisualServerRaster::redraw_request();
- }
-
- state.canvas_shader.set_custom_shader(shader_ptr->custom_code_id);
- state.canvas_shader.bind();
- }
-
- if (material_ptr->ubo_id) {
- glBindBufferBase(GL_UNIFORM_BUFFER, 2, material_ptr->ubo_id);
- }
-
- int tc = material_ptr->textures.size();
- RID *textures = material_ptr->textures.ptrw();
- ShaderLanguage::ShaderNode::Uniform::Hint *texture_hints = shader_ptr->texture_hints.ptrw();
-
- for (int i = 0; i < tc; i++) {
-
- glActiveTexture(GL_TEXTURE2 + i);
-
- RasterizerStorageGLES3::Texture *t = storage->texture_owner.getornull(textures[i]);
- if (!t) {
-
- switch (texture_hints[i]) {
- case ShaderLanguage::ShaderNode::Uniform::HINT_BLACK_ALBEDO:
- case ShaderLanguage::ShaderNode::Uniform::HINT_BLACK: {
- glBindTexture(GL_TEXTURE_2D, storage->resources.black_tex);
- } break;
- case ShaderLanguage::ShaderNode::Uniform::HINT_ANISO: {
- glBindTexture(GL_TEXTURE_2D, storage->resources.aniso_tex);
- } break;
- case ShaderLanguage::ShaderNode::Uniform::HINT_NORMAL: {
- glBindTexture(GL_TEXTURE_2D, storage->resources.normal_tex);
- } break;
- default: {
- glBindTexture(GL_TEXTURE_2D, storage->resources.white_tex);
- } break;
- }
-
- //check hints
-
- continue;
- }
-
- if (t->redraw_if_visible) { //check before proxy, because this is usually used with proxies
- VisualServerRaster::redraw_request();
- }
-
- t = t->get_ptr();
-
- if (storage->config.srgb_decode_supported && t->using_srgb) {
- //no srgb in 2D
- glTexParameteri(t->target, _TEXTURE_SRGB_DECODE_EXT, _SKIP_DECODE_EXT);
- t->using_srgb = false;
- }
-
- glBindTexture(t->target, t->tex_id);
- }
-
- } else {
- state.canvas_shader.set_custom_shader(0);
- state.canvas_shader.bind();
- }
-
- shader_cache = shader_ptr;
-
- canvas_last_material = material;
- rebind_shader = false;
- }
-
- int blend_mode = shader_cache ? shader_cache->canvas_item.blend_mode : RasterizerStorageGLES3::Shader::CanvasItem::BLEND_MODE_MIX;
- if (blend_mode == RasterizerStorageGLES3::Shader::CanvasItem::BLEND_MODE_DISABLED && (!storage->frame.current_rt || !storage->frame.current_rt->flags[RasterizerStorage::RENDER_TARGET_TRANSPARENT])) {
- blend_mode = RasterizerStorageGLES3::Shader::CanvasItem::BLEND_MODE_MIX;
- }
- bool unshaded = shader_cache && (shader_cache->canvas_item.light_mode == RasterizerStorageGLES3::Shader::CanvasItem::LIGHT_MODE_UNSHADED || (blend_mode != RasterizerStorageGLES3::Shader::CanvasItem::BLEND_MODE_MIX && blend_mode != RasterizerStorageGLES3::Shader::CanvasItem::BLEND_MODE_PMALPHA));
- bool reclip = false;
-
- if (last_blend_mode != blend_mode) {
- if (last_blend_mode == RasterizerStorageGLES3::Shader::CanvasItem::BLEND_MODE_DISABLED) {
- // re-enable it
- glEnable(GL_BLEND);
- } else if (blend_mode == RasterizerStorageGLES3::Shader::CanvasItem::BLEND_MODE_DISABLED) {
- // disable it
- glDisable(GL_BLEND);
- }
-
- switch (blend_mode) {
-
- case RasterizerStorageGLES3::Shader::CanvasItem::BLEND_MODE_DISABLED: {
-
- // nothing to do here
-
- } break;
- case RasterizerStorageGLES3::Shader::CanvasItem::BLEND_MODE_MIX: {
-
- glBlendEquation(GL_FUNC_ADD);
- if (storage->frame.current_rt && storage->frame.current_rt->flags[RasterizerStorage::RENDER_TARGET_TRANSPARENT]) {
- glBlendFuncSeparate(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA, GL_ONE, GL_ONE_MINUS_SRC_ALPHA);
- } else {
- glBlendFuncSeparate(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA, GL_ZERO, GL_ONE);
- }
-
- } break;
- case RasterizerStorageGLES3::Shader::CanvasItem::BLEND_MODE_ADD: {
-
- glBlendEquation(GL_FUNC_ADD);
- if (storage->frame.current_rt && storage->frame.current_rt->flags[RasterizerStorage::RENDER_TARGET_TRANSPARENT]) {
- glBlendFuncSeparate(GL_SRC_ALPHA, GL_ONE, GL_SRC_ALPHA, GL_ONE);
- } else {
- glBlendFuncSeparate(GL_SRC_ALPHA, GL_ONE, GL_ZERO, GL_ONE);
- }
-
- } break;
- case RasterizerStorageGLES3::Shader::CanvasItem::BLEND_MODE_SUB: {
-
- glBlendEquation(GL_FUNC_REVERSE_SUBTRACT);
- if (storage->frame.current_rt && storage->frame.current_rt->flags[RasterizerStorage::RENDER_TARGET_TRANSPARENT]) {
- glBlendFuncSeparate(GL_SRC_ALPHA, GL_ONE, GL_SRC_ALPHA, GL_ONE);
- } else {
- glBlendFuncSeparate(GL_SRC_ALPHA, GL_ONE, GL_ZERO, GL_ONE);
- }
- } break;
- case RasterizerStorageGLES3::Shader::CanvasItem::BLEND_MODE_MUL: {
- glBlendEquation(GL_FUNC_ADD);
- if (storage->frame.current_rt && storage->frame.current_rt->flags[RasterizerStorage::RENDER_TARGET_TRANSPARENT]) {
- glBlendFuncSeparate(GL_DST_COLOR, GL_ZERO, GL_DST_ALPHA, GL_ZERO);
- } else {
- glBlendFuncSeparate(GL_DST_COLOR, GL_ZERO, GL_ZERO, GL_ONE);
- }
-
- } break;
- case RasterizerStorageGLES3::Shader::CanvasItem::BLEND_MODE_PMALPHA: {
- glBlendEquation(GL_FUNC_ADD);
- if (storage->frame.current_rt && storage->frame.current_rt->flags[RasterizerStorage::RENDER_TARGET_TRANSPARENT]) {
- glBlendFuncSeparate(GL_ONE, GL_ONE_MINUS_SRC_ALPHA, GL_ONE, GL_ONE_MINUS_SRC_ALPHA);
- } else {
- glBlendFuncSeparate(GL_ONE, GL_ONE_MINUS_SRC_ALPHA, GL_ZERO, GL_ONE);
- }
-
- } break;
- }
-
- last_blend_mode = blend_mode;
- }
-
- state.canvas_item_modulate = unshaded ? ci->final_modulate : Color(ci->final_modulate.r * p_modulate.r, ci->final_modulate.g * p_modulate.g, ci->final_modulate.b * p_modulate.b, ci->final_modulate.a * p_modulate.a);
-
- state.final_transform = ci->final_transform;
- state.extra_matrix = Transform2D();
-
- if (state.using_skeleton) {
- state.canvas_shader.set_uniform(CanvasShaderGLES3::SKELETON_TRANSFORM, state.skeleton_transform);
- state.canvas_shader.set_uniform(CanvasShaderGLES3::SKELETON_TRANSFORM_INVERSE, state.skeleton_transform_inverse);
- }
-
- state.canvas_shader.set_uniform(CanvasShaderGLES3::FINAL_MODULATE, state.canvas_item_modulate);
- state.canvas_shader.set_uniform(CanvasShaderGLES3::MODELVIEW_MATRIX, state.final_transform);
- state.canvas_shader.set_uniform(CanvasShaderGLES3::EXTRA_MATRIX, state.extra_matrix);
- if (storage->frame.current_rt) {
- state.canvas_shader.set_uniform(CanvasShaderGLES3::SCREEN_PIXEL_SIZE, Vector2(1.0 / storage->frame.current_rt->width, 1.0 / storage->frame.current_rt->height));
- } else {
- state.canvas_shader.set_uniform(CanvasShaderGLES3::SCREEN_PIXEL_SIZE, Vector2(1.0, 1.0));
- }
- if (unshaded || (state.canvas_item_modulate.a > 0.001 && (!shader_cache || shader_cache->canvas_item.light_mode != RasterizerStorageGLES3::Shader::CanvasItem::LIGHT_MODE_LIGHT_ONLY) && !ci->light_masked))
- _canvas_item_render_commands(ci, current_clip, reclip);
-
- if ((blend_mode == RasterizerStorageGLES3::Shader::CanvasItem::BLEND_MODE_MIX || blend_mode == RasterizerStorageGLES3::Shader::CanvasItem::BLEND_MODE_PMALPHA) && p_light && !unshaded) {
-
- Light *light = p_light;
- bool light_used = false;
- VS::CanvasLightMode mode = VS::CANVAS_LIGHT_MODE_ADD;
- state.canvas_item_modulate = ci->final_modulate; // remove the canvas modulate
-
- while (light) {
-
- if (ci->light_mask & light->item_mask && p_z >= light->z_min && p_z <= light->z_max && ci->global_rect_cache.intersects_transformed(light->xform_cache, light->rect_cache)) {
-
- //intersects this light
-
- if (!light_used || mode != light->mode) {
-
- mode = light->mode;
-
- switch (mode) {
-
- case VS::CANVAS_LIGHT_MODE_ADD: {
- glBlendEquation(GL_FUNC_ADD);
- glBlendFunc(GL_SRC_ALPHA, GL_ONE);
-
- } break;
- case VS::CANVAS_LIGHT_MODE_SUB: {
- glBlendEquation(GL_FUNC_REVERSE_SUBTRACT);
- glBlendFunc(GL_SRC_ALPHA, GL_ONE);
- } break;
- case VS::CANVAS_LIGHT_MODE_MIX:
- case VS::CANVAS_LIGHT_MODE_MASK: {
- glBlendEquation(GL_FUNC_ADD);
- glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
-
- } break;
- }
- }
-
- if (!light_used) {
-
- state.canvas_shader.set_conditional(CanvasShaderGLES3::USE_LIGHTING, true);
- light_used = true;
- }
-
- bool has_shadow = light->shadow_buffer.is_valid() && ci->light_mask & light->item_shadow_mask;
-
- state.canvas_shader.set_conditional(CanvasShaderGLES3::USE_SHADOWS, has_shadow);
- if (has_shadow) {
- state.canvas_shader.set_conditional(CanvasShaderGLES3::SHADOW_USE_GRADIENT, light->shadow_gradient_length > 0);
- state.canvas_shader.set_conditional(CanvasShaderGLES3::SHADOW_FILTER_NEAREST, light->shadow_filter == VS::CANVAS_LIGHT_FILTER_NONE);
- state.canvas_shader.set_conditional(CanvasShaderGLES3::SHADOW_FILTER_PCF3, light->shadow_filter == VS::CANVAS_LIGHT_FILTER_PCF3);
- state.canvas_shader.set_conditional(CanvasShaderGLES3::SHADOW_FILTER_PCF5, light->shadow_filter == VS::CANVAS_LIGHT_FILTER_PCF5);
- state.canvas_shader.set_conditional(CanvasShaderGLES3::SHADOW_FILTER_PCF7, light->shadow_filter == VS::CANVAS_LIGHT_FILTER_PCF7);
- state.canvas_shader.set_conditional(CanvasShaderGLES3::SHADOW_FILTER_PCF9, light->shadow_filter == VS::CANVAS_LIGHT_FILTER_PCF9);
- state.canvas_shader.set_conditional(CanvasShaderGLES3::SHADOW_FILTER_PCF13, light->shadow_filter == VS::CANVAS_LIGHT_FILTER_PCF13);
- }
-
- bool light_rebind = state.canvas_shader.bind();
-
- if (light_rebind) {
- state.canvas_shader.set_uniform(CanvasShaderGLES3::FINAL_MODULATE, state.canvas_item_modulate);
- state.canvas_shader.set_uniform(CanvasShaderGLES3::MODELVIEW_MATRIX, state.final_transform);
- state.canvas_shader.set_uniform(CanvasShaderGLES3::EXTRA_MATRIX, Transform2D());
- if (storage->frame.current_rt) {
- state.canvas_shader.set_uniform(CanvasShaderGLES3::SCREEN_PIXEL_SIZE, Vector2(1.0 / storage->frame.current_rt->width, 1.0 / storage->frame.current_rt->height));
- } else {
- state.canvas_shader.set_uniform(CanvasShaderGLES3::SCREEN_PIXEL_SIZE, Vector2(1.0, 1.0));
- }
- if (state.using_skeleton) {
- state.canvas_shader.set_uniform(CanvasShaderGLES3::SKELETON_TRANSFORM, state.skeleton_transform);
- state.canvas_shader.set_uniform(CanvasShaderGLES3::SKELETON_TRANSFORM_INVERSE, state.skeleton_transform_inverse);
- }
- }
-
- glBindBufferBase(GL_UNIFORM_BUFFER, 1, static_cast(light->light_internal.get_data())->ubo);
-
- if (has_shadow) {
-
- RasterizerStorageGLES3::CanvasLightShadow *cls = storage->canvas_light_shadow_owner.get(light->shadow_buffer);
- glActiveTexture(GL_TEXTURE0 + storage->config.max_texture_image_units - 2);
- glBindTexture(GL_TEXTURE_2D, cls->distance);
-
- /*canvas_shader.set_uniform(CanvasShaderGLES3::SHADOW_MATRIX,light->shadow_matrix_cache);
- canvas_shader.set_uniform(CanvasShaderGLES3::SHADOW_ESM_MULTIPLIER,light->shadow_esm_mult);
- canvas_shader.set_uniform(CanvasShaderGLES3::LIGHT_SHADOW_COLOR,light->shadow_color);*/
- }
-
- glActiveTexture(GL_TEXTURE0 + storage->config.max_texture_image_units - 1);
- RasterizerStorageGLES3::Texture *t = storage->texture_owner.getornull(light->texture);
- if (!t) {
- glBindTexture(GL_TEXTURE_2D, storage->resources.white_tex);
- } else {
- t = t->get_ptr();
-
- glBindTexture(t->target, t->tex_id);
- }
-
- glActiveTexture(GL_TEXTURE0);
- _canvas_item_render_commands(ci, current_clip, reclip); //redraw using light
- }
-
- light = light->next_ptr;
- }
-
- if (light_used) {
-
- state.canvas_shader.set_conditional(CanvasShaderGLES3::USE_LIGHTING, false);
- state.canvas_shader.set_conditional(CanvasShaderGLES3::USE_SHADOWS, false);
- state.canvas_shader.set_conditional(CanvasShaderGLES3::SHADOW_FILTER_NEAREST, false);
- state.canvas_shader.set_conditional(CanvasShaderGLES3::SHADOW_FILTER_PCF3, false);
- state.canvas_shader.set_conditional(CanvasShaderGLES3::SHADOW_FILTER_PCF5, false);
- state.canvas_shader.set_conditional(CanvasShaderGLES3::SHADOW_FILTER_PCF7, false);
- state.canvas_shader.set_conditional(CanvasShaderGLES3::SHADOW_FILTER_PCF9, false);
- state.canvas_shader.set_conditional(CanvasShaderGLES3::SHADOW_FILTER_PCF13, false);
-
- state.canvas_shader.bind();
-
- last_blend_mode = -1;
-
- /*
- //this is set again, so it should not be needed anyway?
- state.canvas_item_modulate = unshaded ? ci->final_modulate : Color(
- ci->final_modulate.r * p_modulate.r,
- ci->final_modulate.g * p_modulate.g,
- ci->final_modulate.b * p_modulate.b,
- ci->final_modulate.a * p_modulate.a );
-
-
- state.canvas_shader.set_uniform(CanvasShaderGLES3::MODELVIEW_MATRIX,state.final_transform);
- state.canvas_shader.set_uniform(CanvasShaderGLES3::EXTRA_MATRIX,Transform2D());
- state.canvas_shader.set_uniform(CanvasShaderGLES3::FINAL_MODULATE,state.canvas_item_modulate);
-
- glBlendEquation(GL_FUNC_ADD);
-
- if (storage->frame.current_rt->flags[RasterizerStorage::RENDER_TARGET_TRANSPARENT]) {
- glBlendFuncSeparate(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA, GL_ONE, GL_ONE_MINUS_SRC_ALPHA);
- } else {
- glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
- }
-
- //@TODO RESET canvas_blend_mode
- */
- }
- }
-
- if (reclip) {
-
- glEnable(GL_SCISSOR_TEST);
- int y = storage->frame.current_rt->height - (current_clip->final_clip_rect.position.y + current_clip->final_clip_rect.size.y);
- if (storage->frame.current_rt->flags[RasterizerStorage::RENDER_TARGET_VFLIP])
- y = current_clip->final_clip_rect.position.y;
- glScissor(current_clip->final_clip_rect.position.x, y, current_clip->final_clip_rect.size.width, current_clip->final_clip_rect.size.height);
- }
-
- p_item_list = p_item_list->next;
}
- if (current_clip) {
+ if (ris.current_clip) {
glDisable(GL_SCISSOR_TEST);
}
- //disable states that may have been used
- state.canvas_shader.set_conditional(CanvasShaderGLES3::USE_DISTANCE_FIELD, false);
+
state.canvas_shader.set_conditional(CanvasShaderGLES3::USE_SKELETON, false);
- state.canvas_shader.set_conditional(CanvasShaderGLES3::USE_INSTANCE_CUSTOM, false);
- state.canvas_shader.set_conditional(CanvasShaderGLES3::USE_PARTICLES, false);
- state.canvas_shader.set_conditional(CanvasShaderGLES3::USE_INSTANCING, false);
- state.canvas_shader.set_conditional(CanvasShaderGLES3::USE_LIGHTING, false);
- state.canvas_shader.set_conditional(CanvasShaderGLES3::USE_SHADOWS, false);
}
-void RasterizerCanvasGLES3::canvas_debug_viewport_shadows(Light *p_lights_with_shadow) {
+void RasterizerCanvasGLES3::_batch_upload_buffers() {
- Light *light = p_lights_with_shadow;
+ // noop?
+ if (!bdata.vertices.size())
+ return;
- canvas_begin(); //reset
- glVertexAttrib4f(VS::ARRAY_COLOR, 1, 1, 1, 1);
- int h = 10;
- int w = storage->frame.current_rt->width;
- int ofs = h;
- glDisable(GL_BLEND);
+ glBindBuffer(GL_ARRAY_BUFFER, bdata.gl_vertex_buffer);
- while (light) {
- if (light->shadow_buffer.is_valid()) {
+ // orphan the old (for now)
+ //glBufferData(GL_ARRAY_BUFFER, 0, 0, GL_DYNAMIC_DRAW);
- RasterizerStorageGLES3::CanvasLightShadow *sb = storage->canvas_light_shadow_owner.get(light->shadow_buffer);
- if (sb) {
- glBindTexture(GL_TEXTURE_2D, sb->distance);
- draw_generic_textured_rect(Rect2(h, ofs, w - h * 2, h), Rect2(0, 0, 1, 1));
- ofs += h * 2;
- }
- }
-
- light = light->shadows_next_ptr;
+ switch (bdata.fvf) {
+ case RasterizerStorageCommon::FVF_UNBATCHED: // should not happen
+ break;
+ case RasterizerStorageCommon::FVF_REGULAR: // no change
+ glBufferData(GL_ARRAY_BUFFER, sizeof(BatchVertex) * bdata.vertices.size(), bdata.vertices.get_data(), GL_DYNAMIC_DRAW);
+ break;
+ case RasterizerStorageCommon::FVF_COLOR:
+ glBufferData(GL_ARRAY_BUFFER, sizeof(BatchVertexColored) * bdata.unit_vertices.size(), bdata.unit_vertices.get_unit(0), GL_DYNAMIC_DRAW);
+ break;
+ case RasterizerStorageCommon::FVF_LIGHT_ANGLE:
+ glBufferData(GL_ARRAY_BUFFER, sizeof(BatchVertexLightAngled) * bdata.unit_vertices.size(), bdata.unit_vertices.get_unit(0), GL_DYNAMIC_DRAW);
+ break;
+ case RasterizerStorageCommon::FVF_MODULATED:
+ glBufferData(GL_ARRAY_BUFFER, sizeof(BatchVertexModulated) * bdata.unit_vertices.size(), bdata.unit_vertices.get_unit(0), GL_DYNAMIC_DRAW);
+ break;
+ case RasterizerStorageCommon::FVF_LARGE:
+ glBufferData(GL_ARRAY_BUFFER, sizeof(BatchVertexLarge) * bdata.unit_vertices.size(), bdata.unit_vertices.get_unit(0), GL_DYNAMIC_DRAW);
+ break;
}
- canvas_end();
-}
-
-void RasterizerCanvasGLES3::canvas_light_shadow_buffer_update(RID p_buffer, const Transform2D &p_light_xform, int p_light_mask, float p_near, float p_far, LightOccluderInstance *p_occluders, CameraMatrix *p_xform_cache) {
-
- RasterizerStorageGLES3::CanvasLightShadow *cls = storage->canvas_light_shadow_owner.get(p_buffer);
- ERR_FAIL_COND(!cls);
-
- glDisable(GL_BLEND);
- glDisable(GL_SCISSOR_TEST);
- glDisable(GL_DITHER);
- glDisable(GL_CULL_FACE);
- glDepthFunc(GL_LEQUAL);
- glEnable(GL_DEPTH_TEST);
- glDepthMask(true);
-
- glBindFramebuffer(GL_FRAMEBUFFER, cls->fbo);
-
- state.canvas_shadow_shader.bind();
-
- glViewport(0, 0, cls->size, cls->height);
- glClearDepth(1.0f);
- glClearColor(1, 1, 1, 1);
- glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
-
- VS::CanvasOccluderPolygonCullMode cull = VS::CANVAS_OCCLUDER_POLYGON_CULL_DISABLED;
-
- for (int i = 0; i < 4; i++) {
-
- //make sure it remains orthogonal, makes easy to read angle later
-
- Transform light;
- light.origin[0] = p_light_xform[2][0];
- light.origin[1] = p_light_xform[2][1];
- light.basis[0][0] = p_light_xform[0][0];
- light.basis[0][1] = p_light_xform[1][0];
- light.basis[1][0] = p_light_xform[0][1];
- light.basis[1][1] = p_light_xform[1][1];
-
- //light.basis.scale(Vector3(to_light.elements[0].length(),to_light.elements[1].length(),1));
-
- //p_near=1;
- CameraMatrix projection;
- {
- real_t fov = 90;
- real_t nearp = p_near;
- real_t farp = p_far;
- real_t aspect = 1.0;
-
- real_t ymax = nearp * Math::tan(Math::deg2rad(fov * 0.5));
- real_t ymin = -ymax;
- real_t xmin = ymin * aspect;
- real_t xmax = ymax * aspect;
-
- projection.set_frustum(xmin, xmax, ymin, ymax, nearp, farp);
- }
-
- Vector3 cam_target = Basis(Vector3(0, 0, Math_PI * 2 * (i / 4.0))).xform(Vector3(0, 1, 0));
- projection = projection * CameraMatrix(Transform().looking_at(cam_target, Vector3(0, 0, -1)).affine_inverse());
-
- state.canvas_shadow_shader.set_uniform(CanvasShadowShaderGLES3::PROJECTION_MATRIX, projection);
- state.canvas_shadow_shader.set_uniform(CanvasShadowShaderGLES3::LIGHT_MATRIX, light);
- state.canvas_shadow_shader.set_uniform(CanvasShadowShaderGLES3::DISTANCE_NORM, 1.0 / p_far);
-
- if (i == 0)
- *p_xform_cache = projection;
-
- glViewport(0, (cls->height / 4) * i, cls->size, cls->height / 4);
-
- LightOccluderInstance *instance = p_occluders;
-
- while (instance) {
-
- RasterizerStorageGLES3::CanvasOccluder *cc = storage->canvas_occluder_owner.getornull(instance->polygon_buffer);
- if (!cc || cc->len == 0 || !(p_light_mask & instance->light_mask)) {
-
- instance = instance->next;
- continue;
- }
-
- state.canvas_shadow_shader.set_uniform(CanvasShadowShaderGLES3::WORLD_MATRIX, instance->xform_cache);
-
- VS::CanvasOccluderPolygonCullMode transformed_cull_cache = instance->cull_cache;
-
- if (transformed_cull_cache != VS::CANVAS_OCCLUDER_POLYGON_CULL_DISABLED &&
- (p_light_xform.basis_determinant() * instance->xform_cache.basis_determinant()) < 0) {
- transformed_cull_cache =
- transformed_cull_cache == VS::CANVAS_OCCLUDER_POLYGON_CULL_CLOCKWISE ?
- VS::CANVAS_OCCLUDER_POLYGON_CULL_COUNTER_CLOCKWISE :
- VS::CANVAS_OCCLUDER_POLYGON_CULL_CLOCKWISE;
- }
-
- if (cull != transformed_cull_cache) {
-
- cull = transformed_cull_cache;
- switch (cull) {
- case VS::CANVAS_OCCLUDER_POLYGON_CULL_DISABLED: {
-
- glDisable(GL_CULL_FACE);
-
- } break;
- case VS::CANVAS_OCCLUDER_POLYGON_CULL_CLOCKWISE: {
-
- glEnable(GL_CULL_FACE);
- glCullFace(GL_FRONT);
- } break;
- case VS::CANVAS_OCCLUDER_POLYGON_CULL_COUNTER_CLOCKWISE: {
-
- glEnable(GL_CULL_FACE);
- glCullFace(GL_BACK);
-
- } break;
- }
- }
-
- glBindVertexArray(cc->array_id);
- glDrawElements(GL_TRIANGLES, cc->len * 3, GL_UNSIGNED_SHORT, 0);
-
- instance = instance->next;
- }
- }
-
- glBindVertexArray(0);
-}
-void RasterizerCanvasGLES3::reset_canvas() {
-
- if (storage->frame.current_rt) {
- glBindFramebuffer(GL_FRAMEBUFFER, storage->frame.current_rt->fbo);
- glColorMask(1, 1, 1, 1); //don't touch alpha
- }
-
- glBindVertexArray(0);
- glDisable(GL_CULL_FACE);
- glDisable(GL_DEPTH_TEST);
- glDisable(GL_SCISSOR_TEST);
- glDisable(GL_DITHER);
- glEnable(GL_BLEND);
- glBlendEquation(GL_FUNC_ADD);
- if (storage->frame.current_rt && storage->frame.current_rt->flags[RasterizerStorage::RENDER_TARGET_TRANSPARENT]) {
- glBlendFuncSeparate(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA, GL_ONE, GL_ONE_MINUS_SRC_ALPHA);
- } else {
- glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
- }
- //glPolygonMode(GL_FRONT_AND_BACK,GL_FILL);
- //glLineWidth(1.0);
+ // might not be necessary
glBindBuffer(GL_ARRAY_BUFFER, 0);
- glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0);
-
- //use for reading from screen
- if (storage->frame.current_rt && !storage->frame.current_rt->flags[RasterizerStorage::RENDER_TARGET_NO_SAMPLING]) {
- glActiveTexture(GL_TEXTURE0 + storage->config.max_texture_image_units - 3);
- glBindTexture(GL_TEXTURE_2D, storage->frame.current_rt->effects.mip_maps[0].color);
- }
-
- glActiveTexture(GL_TEXTURE0);
- glBindTexture(GL_TEXTURE_2D, storage->resources.white_tex);
-
- glVertexAttrib4f(VS::ARRAY_COLOR, 1, 1, 1, 1);
-
- Transform canvas_transform;
-
- if (storage->frame.current_rt) {
-
- float csy = 1.0;
- if (storage->frame.current_rt && storage->frame.current_rt->flags[RasterizerStorage::RENDER_TARGET_VFLIP]) {
- csy = -1.0;
- }
- canvas_transform.translate(-(storage->frame.current_rt->width / 2.0f), -(storage->frame.current_rt->height / 2.0f), 0.0f);
- canvas_transform.scale(Vector3(2.0f / storage->frame.current_rt->width, csy * -2.0f / storage->frame.current_rt->height, 1.0f));
- } else {
- Vector2 ssize = OS::get_singleton()->get_window_size();
- canvas_transform.translate(-(ssize.width / 2.0f), -(ssize.height / 2.0f), 0.0f);
- canvas_transform.scale(Vector3(2.0f / ssize.width, -2.0f / ssize.height, 1.0f));
- }
-
- state.vp = canvas_transform;
-
- store_transform(canvas_transform, state.canvas_item_ubo_data.projection_matrix);
- state.canvas_item_ubo_data.time = storage->frame.time[0];
-
- glBindBuffer(GL_UNIFORM_BUFFER, state.canvas_item_ubo);
- glBufferData(GL_UNIFORM_BUFFER, sizeof(CanvasItemUBO), &state.canvas_item_ubo_data, GL_DYNAMIC_DRAW);
- glBindBuffer(GL_UNIFORM_BUFFER, 0);
-
- state.canvas_texscreen_used = false;
}
-void RasterizerCanvasGLES3::draw_generic_textured_rect(const Rect2 &p_rect, const Rect2 &p_src) {
+void RasterizerCanvasGLES3::_batch_render_lines(const Batch &p_batch, RasterizerStorageGLES3::Material *p_material, bool p_anti_alias) {
- state.canvas_shader.set_uniform(CanvasShaderGLES3::DST_RECT, Color(p_rect.position.x, p_rect.position.y, p_rect.size.x, p_rect.size.y));
- state.canvas_shader.set_uniform(CanvasShaderGLES3::SRC_RECT, Color(p_src.position.x, p_src.position.y, p_src.size.x, p_src.size.y));
+ _set_texture_rect_mode(false);
+
+ _bind_canvas_texture(RID(), RID());
+
+ glBindVertexArray(batch_gl_data.batch_vertex_array[0]);
+
+ glDisableVertexAttribArray(VS::ARRAY_COLOR);
+ glVertexAttrib4fv(VS::ARRAY_COLOR, (float *)&p_batch.color);
+
+ int64_t offset = p_batch.first_vert; // 6 inds per quad at 2 bytes each
+
+ int num_elements = p_batch.num_commands * 2;
+
+#ifdef GLES_OVER_GL
+ if (p_anti_alias)
+ glEnable(GL_LINE_SMOOTH);
+#endif
+
+ glDrawArrays(GL_LINES, offset, num_elements);
+
+ storage->info.render._2d_draw_call_count++;
+
+ glBindVertexArray(0);
+
+#ifdef GLES_OVER_GL
+ if (p_anti_alias)
+ glDisable(GL_LINE_SMOOTH);
+#endif
+}
+
+void RasterizerCanvasGLES3::_batch_render_polys(const Batch &p_batch, RasterizerStorageGLES3::Material *p_material) {
+ ERR_FAIL_COND(p_batch.num_commands <= 0);
+
+ _set_texture_rect_mode(false);
state.canvas_shader.set_uniform(CanvasShaderGLES3::CLIP_RECT_UV, false);
- glDrawArrays(GL_TRIANGLE_FAN, 0, 4);
-}
+ glBindVertexArray(batch_gl_data.batch_vertex_array[1]);
-void RasterizerCanvasGLES3::draw_lens_distortion_rect(const Rect2 &p_rect, float p_k1, float p_k2, const Vector2 &p_eye_center, float p_oversample) {
- Vector2 half_size;
- if (storage->frame.current_rt) {
- half_size = Vector2(storage->frame.current_rt->width, storage->frame.current_rt->height);
- } else {
- half_size = OS::get_singleton()->get_window_size();
- }
- half_size *= 0.5;
- Vector2 offset((p_rect.position.x - half_size.x) / half_size.x, (p_rect.position.y - half_size.y) / half_size.y);
- Vector2 scale(p_rect.size.x / half_size.x, p_rect.size.y / half_size.y);
+ // batch tex
+ const BatchTex &tex = bdata.batch_textures[p_batch.batch_texture_id];
- float aspect_ratio = p_rect.size.x / p_rect.size.y;
+ _bind_canvas_texture(tex.RID_texture, tex.RID_normal);
- // setup our lens shader
- state.lens_shader.bind();
- state.lens_shader.set_uniform(LensDistortedShaderGLES3::OFFSET, offset);
- state.lens_shader.set_uniform(LensDistortedShaderGLES3::SCALE, scale);
- state.lens_shader.set_uniform(LensDistortedShaderGLES3::K1, p_k1);
- state.lens_shader.set_uniform(LensDistortedShaderGLES3::K2, p_k2);
- state.lens_shader.set_uniform(LensDistortedShaderGLES3::EYE_CENTER, p_eye_center);
- state.lens_shader.set_uniform(LensDistortedShaderGLES3::UPSCALE, p_oversample);
- state.lens_shader.set_uniform(LensDistortedShaderGLES3::ASPECT_RATIO, aspect_ratio);
+ // may not need this disable
+ // glDisableVertexAttribArray(VS::ARRAY_COLOR);
+ // glVertexAttrib4fv(VS::ARRAY_COLOR, p_batch.color.get_data());
- glBindBufferBase(GL_UNIFORM_BUFFER, 0, state.canvas_item_ubo);
- glBindVertexArray(data.canvas_quad_array);
+ // we need to convert explicitly from pod Vec2 to Vector2 ...
+ // could use a cast but this might be unsafe in future
+ Vector2 tps;
+ tex.tex_pixel_size.to(tps);
+ state.canvas_shader.set_uniform(CanvasShaderGLES3::COLOR_TEXPIXEL_SIZE, tps);
- // and draw
- glDrawArrays(GL_TRIANGLE_FAN, 0, 4);
+ int64_t offset = p_batch.first_vert;
+
+ int num_elements = p_batch.num_commands;
+ glDrawArrays(GL_TRIANGLES, offset, num_elements);
+
+ storage->info.render._2d_draw_call_count++;
glBindVertexArray(0);
- glBindBufferBase(GL_UNIFORM_BUFFER, 0, 0);
}
-void RasterizerCanvasGLES3::draw_window_margins(int *black_margin, RID *black_image) {
+void RasterizerCanvasGLES3::_batch_render_rects(const Batch &p_batch, RasterizerStorageGLES3::Material *p_material) {
+ ERR_FAIL_COND(p_batch.num_commands <= 0);
- Vector2 window_size = OS::get_singleton()->get_window_size();
- int window_h = window_size.height;
- int window_w = window_size.width;
+ const bool &colored_verts = bdata.use_colored_vertices;
+ const bool &use_light_angles = bdata.use_light_angles;
+ const bool &use_modulate = bdata.use_modulate;
+ const bool &use_large_verts = bdata.use_large_verts;
- glBindFramebuffer(GL_FRAMEBUFFER, RasterizerStorageGLES3::system_fbo);
- glViewport(0, 0, window_size.width, window_size.height);
- canvas_begin();
+ _set_texture_rect_mode(false, false, use_light_angles, use_modulate, use_large_verts);
- if (black_image[MARGIN_LEFT].is_valid()) {
- _bind_canvas_texture(black_image[MARGIN_LEFT], RID(), true);
- Size2 sz(storage->texture_get_width(black_image[MARGIN_LEFT]), storage->texture_get_height(black_image[MARGIN_LEFT]));
+ // state.canvas_shader.set_uniform(CanvasShaderGLES3::CLIP_RECT_UV, p_rect->flags & CANVAS_RECT_CLIP_UV);
+ state.canvas_shader.set_uniform(CanvasShaderGLES3::CLIP_RECT_UV, false);
- draw_generic_textured_rect(Rect2(0, 0, black_margin[MARGIN_LEFT], window_h),
- Rect2(0, 0, (float)black_margin[MARGIN_LEFT] / sz.x, (float)(window_h) / sz.y));
- } else if (black_margin[MARGIN_LEFT]) {
- glActiveTexture(GL_TEXTURE0);
- glBindTexture(GL_TEXTURE_2D, storage->resources.black_tex);
-
- draw_generic_textured_rect(Rect2(0, 0, black_margin[MARGIN_LEFT], window_h), Rect2(0, 0, 1, 1));
+ switch (bdata.fvf) {
+ case RasterizerStorageCommon::FVF_UNBATCHED: // should not happen
+ return;
+ break;
+ case RasterizerStorageCommon::FVF_REGULAR: // no change
+ glBindVertexArray(batch_gl_data.batch_vertex_array[0]);
+ break;
+ case RasterizerStorageCommon::FVF_COLOR:
+ glBindVertexArray(batch_gl_data.batch_vertex_array[1]);
+ break;
+ case RasterizerStorageCommon::FVF_LIGHT_ANGLE:
+ glBindVertexArray(batch_gl_data.batch_vertex_array[2]);
+ break;
+ case RasterizerStorageCommon::FVF_MODULATED:
+ glBindVertexArray(batch_gl_data.batch_vertex_array[3]);
+ break;
+ case RasterizerStorageCommon::FVF_LARGE:
+ glBindVertexArray(batch_gl_data.batch_vertex_array[4]);
+ break;
}
- if (black_image[MARGIN_RIGHT].is_valid()) {
- _bind_canvas_texture(black_image[MARGIN_RIGHT], RID(), true);
- Size2 sz(storage->texture_get_width(black_image[MARGIN_RIGHT]), storage->texture_get_height(black_image[MARGIN_RIGHT]));
- draw_generic_textured_rect(Rect2(window_w - black_margin[MARGIN_RIGHT], 0, black_margin[MARGIN_RIGHT], window_h),
- Rect2(0, 0, (float)black_margin[MARGIN_RIGHT] / sz.x, (float)window_h / sz.y));
- } else if (black_margin[MARGIN_RIGHT]) {
- glActiveTexture(GL_TEXTURE0);
- glBindTexture(GL_TEXTURE_2D, storage->resources.black_tex);
+ // if (state.canvas_shader.bind()) {
+ // _set_uniforms();
+ // state.canvas_shader.use_material((void *)p_material);
+ // }
- draw_generic_textured_rect(Rect2(window_w - black_margin[MARGIN_RIGHT], 0, black_margin[MARGIN_RIGHT], window_h), Rect2(0, 0, 1, 1));
+ // batch tex
+ const BatchTex &tex = bdata.batch_textures[p_batch.batch_texture_id];
+
+ _bind_canvas_texture(tex.RID_texture, tex.RID_normal);
+
+ if (!colored_verts) {
+ // may not need this disable
+ glDisableVertexAttribArray(VS::ARRAY_COLOR);
+ glVertexAttrib4fv(VS::ARRAY_COLOR, p_batch.color.get_data());
}
- if (black_image[MARGIN_TOP].is_valid()) {
- _bind_canvas_texture(black_image[MARGIN_TOP], RID(), true);
+ // We only want to set the GL wrapping mode if the texture is not already tiled (i.e. set in Import).
+ // This is an optimization left over from the legacy renderer.
+ // If we DID set tiling in the API, and reverted to clamped, then the next draw using this texture
+ // may use clamped mode incorrectly.
+ bool tex_is_already_tiled = tex.flags & VS::TEXTURE_FLAG_REPEAT;
- Size2 sz(storage->texture_get_width(black_image[MARGIN_TOP]), storage->texture_get_height(black_image[MARGIN_TOP]));
- draw_generic_textured_rect(Rect2(0, 0, window_w, black_margin[MARGIN_TOP]),
- Rect2(0, 0, (float)window_w / sz.x, (float)black_margin[MARGIN_TOP] / sz.y));
-
- } else if (black_margin[MARGIN_TOP]) {
- glActiveTexture(GL_TEXTURE0);
- glBindTexture(GL_TEXTURE_2D, storage->resources.black_tex);
-
- draw_generic_textured_rect(Rect2(0, 0, window_w, black_margin[MARGIN_TOP]), Rect2(0, 0, 1, 1));
+ switch (tex.tile_mode) {
+ case BatchTex::TILE_NORMAL: {
+ // if the texture is imported as tiled, no need to set GL state, as it will already be bound with repeat
+ if (!tex_is_already_tiled) {
+ glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
+ glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
+ }
+ } break;
+ default: {
+ } break;
}
- if (black_image[MARGIN_BOTTOM].is_valid()) {
+ // we need to convert explicitly from pod Vec2 to Vector2 ...
+ // could use a cast but this might be unsafe in future
+ Vector2 tps;
+ tex.tex_pixel_size.to(tps);
+ state.canvas_shader.set_uniform(CanvasShaderGLES3::COLOR_TEXPIXEL_SIZE, tps);
- _bind_canvas_texture(black_image[MARGIN_BOTTOM], RID(), true);
+ int64_t offset = p_batch.first_vert * 3; // 6 inds per quad at 2 bytes each
- Size2 sz(storage->texture_get_width(black_image[MARGIN_BOTTOM]), storage->texture_get_height(black_image[MARGIN_BOTTOM]));
- draw_generic_textured_rect(Rect2(0, window_h - black_margin[MARGIN_BOTTOM], window_w, black_margin[MARGIN_BOTTOM]),
- Rect2(0, 0, (float)window_w / sz.x, (float)black_margin[MARGIN_BOTTOM] / sz.y));
+ int num_elements = p_batch.num_commands * 6;
+ glDrawElements(GL_TRIANGLES, num_elements, GL_UNSIGNED_SHORT, (void *)offset);
- } else if (black_margin[MARGIN_BOTTOM]) {
+ storage->info.render._2d_draw_call_count++;
- glActiveTexture(GL_TEXTURE0);
- glBindTexture(GL_TEXTURE_2D, storage->resources.black_tex);
+ glBindVertexArray(0);
- draw_generic_textured_rect(Rect2(0, window_h - black_margin[MARGIN_BOTTOM], window_w, black_margin[MARGIN_BOTTOM]), Rect2(0, 0, 1, 1));
+ // gl_checkerror();
+
+ switch (tex.tile_mode) {
+ case BatchTex::TILE_NORMAL: {
+ // if the texture is imported as tiled, no need to revert GL state
+ if (!tex_is_already_tiled) {
+ glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
+ glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
+ }
+ } break;
+ default: {
+ } break;
}
+
+ /*
+ // may not be necessary .. state change optimization still TODO
+ glBindBuffer(GL_ARRAY_BUFFER, 0);
+ glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0);
+ */
}
void RasterizerCanvasGLES3::initialize() {
+ gl_checkerror();
+ RasterizerCanvasBaseGLES3::initialize();
- {
- //quad buffers
+ batch_initialize();
- glGenBuffers(1, &data.canvas_quad_vertices);
- glBindBuffer(GL_ARRAY_BUFFER, data.canvas_quad_vertices);
- {
- const float qv[8] = {
- 0, 0,
- 0, 1,
- 1, 1,
- 1, 0
- };
+ // just reserve some space (may not be needed as we are orphaning, but hey ho)
+ glGenBuffers(1, &bdata.gl_vertex_buffer);
- glBufferData(GL_ARRAY_BUFFER, sizeof(float) * 8, qv, GL_STATIC_DRAW);
- }
-
- glBindBuffer(GL_ARRAY_BUFFER, 0); //unbind
-
- glGenVertexArrays(1, &data.canvas_quad_array);
- glBindVertexArray(data.canvas_quad_array);
- glBindBuffer(GL_ARRAY_BUFFER, data.canvas_quad_vertices);
- glVertexAttribPointer(0, 2, GL_FLOAT, GL_FALSE, sizeof(float) * 2, 0);
- glEnableVertexAttribArray(0);
- glBindVertexArray(0);
- glBindBuffer(GL_ARRAY_BUFFER, 0); //unbind
- }
- {
- //particle quad buffers
-
- glGenBuffers(1, &data.particle_quad_vertices);
- glBindBuffer(GL_ARRAY_BUFFER, data.particle_quad_vertices);
- {
- //quad of size 1, with pivot on the center for particles, then regular UVS. Color is general plus fetched from particle
- const float qv[16] = {
- -0.5, -0.5,
- 0.0, 0.0,
- -0.5, 0.5,
- 0.0, 1.0,
- 0.5, 0.5,
- 1.0, 1.0,
- 0.5, -0.5,
- 1.0, 0.0
- };
-
- glBufferData(GL_ARRAY_BUFFER, sizeof(float) * 16, qv, GL_STATIC_DRAW);
- }
-
- glBindBuffer(GL_ARRAY_BUFFER, 0); //unbind
-
- glGenVertexArrays(1, &data.particle_quad_array);
- glBindVertexArray(data.particle_quad_array);
- glBindBuffer(GL_ARRAY_BUFFER, data.particle_quad_vertices);
- glEnableVertexAttribArray(VS::ARRAY_VERTEX);
- glVertexAttribPointer(VS::ARRAY_VERTEX, 2, GL_FLOAT, GL_FALSE, sizeof(float) * 4, 0);
- glEnableVertexAttribArray(VS::ARRAY_TEX_UV);
- glVertexAttribPointer(VS::ARRAY_TEX_UV, 2, GL_FLOAT, GL_FALSE, sizeof(float) * 4, CAST_INT_TO_UCHAR_PTR(8));
- glBindVertexArray(0);
- glBindBuffer(GL_ARRAY_BUFFER, 0); //unbind
- }
- {
-
- uint32_t poly_size = GLOBAL_DEF_RST("rendering/limits/buffers/canvas_polygon_buffer_size_kb", 128);
- ProjectSettings::get_singleton()->set_custom_property_info("rendering/limits/buffers/canvas_polygon_buffer_size_kb", PropertyInfo(Variant::INT, "rendering/limits/buffers/canvas_polygon_buffer_size_kb", PROPERTY_HINT_RANGE, "0,256,1,or_greater"));
- poly_size *= 1024; //kb
- poly_size = MAX(poly_size, (2 + 2 + 4 + 1) * 4 * sizeof(float));
- glGenBuffers(1, &data.polygon_buffer);
- glBindBuffer(GL_ARRAY_BUFFER, data.polygon_buffer);
- glBufferData(GL_ARRAY_BUFFER, poly_size, NULL, GL_DYNAMIC_DRAW); //allocate max size
+ if (bdata.vertex_buffer_size_bytes) {
+ glBindBuffer(GL_ARRAY_BUFFER, bdata.gl_vertex_buffer);
+ glBufferData(GL_ARRAY_BUFFER, bdata.vertex_buffer_size_bytes, NULL, GL_DYNAMIC_DRAW);
glBindBuffer(GL_ARRAY_BUFFER, 0);
- data.polygon_buffer_size = poly_size;
- //quad arrays
- for (int i = 0; i < Data::NUM_QUAD_ARRAY_VARIATIONS; i++) {
- glGenVertexArrays(1, &data.polygon_buffer_quad_arrays[i]);
- glBindVertexArray(data.polygon_buffer_quad_arrays[i]);
- glBindBuffer(GL_ARRAY_BUFFER, data.polygon_buffer);
+ // pre fill index buffer, the indices never need to change so can be static
+ glGenBuffers(1, &bdata.gl_index_buffer);
+ glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, bdata.gl_index_buffer);
- int uv_ofs = 0;
- int color_ofs = 0;
- int light_angle_ofs = 0;
- int stride = 2 * 4;
+ Vector indices;
+ indices.resize(bdata.index_buffer_size_units);
- if (i & 1) { //color
- color_ofs = stride;
- stride += 4 * 4;
- }
+ for (unsigned int q = 0; q < bdata.max_quads; q++) {
+ int i_pos = q * 6; // 6 inds per quad
+ int q_pos = q * 4; // 4 verts per quad
+ indices.set(i_pos, q_pos);
+ indices.set(i_pos + 1, q_pos + 1);
+ indices.set(i_pos + 2, q_pos + 2);
+ indices.set(i_pos + 3, q_pos);
+ indices.set(i_pos + 4, q_pos + 2);
+ indices.set(i_pos + 5, q_pos + 3);
- if (i & 2) { //uv
- uv_ofs = stride;
- stride += 2 * 4;
- }
-
- if (i & 4) { //light_angle
- light_angle_ofs = stride;
- stride += 1 * 4;
- }
-
- glEnableVertexAttribArray(VS::ARRAY_VERTEX);
- glVertexAttribPointer(VS::ARRAY_VERTEX, 2, GL_FLOAT, GL_FALSE, stride, NULL);
-
- if (i & 1) {
- glEnableVertexAttribArray(VS::ARRAY_COLOR);
- glVertexAttribPointer(VS::ARRAY_COLOR, 4, GL_FLOAT, GL_FALSE, stride, CAST_INT_TO_UCHAR_PTR(color_ofs));
- }
-
- if (i & 2) {
- glEnableVertexAttribArray(VS::ARRAY_TEX_UV);
- glVertexAttribPointer(VS::ARRAY_TEX_UV, 2, GL_FLOAT, GL_FALSE, stride, CAST_INT_TO_UCHAR_PTR(uv_ofs));
- }
-
- if (i & 4) {
- // reusing tangent for light_angle
- glEnableVertexAttribArray(VS::ARRAY_TANGENT);
- glVertexAttribPointer(VS::ARRAY_TANGENT, 1, GL_FLOAT, GL_FALSE, stride, CAST_INT_TO_UCHAR_PTR(light_angle_ofs));
- }
-
- glBindVertexArray(0);
+ // we can only use 16 bit indices in GLES2!
+#ifdef DEBUG_ENABLED
+ CRASH_COND((q_pos + 3) > 65535);
+#endif
}
- glGenVertexArrays(1, &data.polygon_buffer_pointer_array);
-
- uint32_t index_size = GLOBAL_DEF_RST("rendering/limits/buffers/canvas_polygon_index_buffer_size_kb", 128);
- ProjectSettings::get_singleton()->set_custom_property_info("rendering/limits/buffers/canvas_polygon_index_buffer_size_kb", PropertyInfo(Variant::INT, "rendering/limits/buffers/canvas_polygon_index_buffer_size_kb", PROPERTY_HINT_RANGE, "0,256,1,or_greater"));
- index_size *= 1024; //kb
- glGenBuffers(1, &data.polygon_index_buffer);
- glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, data.polygon_index_buffer);
- glBufferData(GL_ELEMENT_ARRAY_BUFFER, index_size, NULL, GL_DYNAMIC_DRAW); //allocate max size
+ glBufferData(GL_ELEMENT_ARRAY_BUFFER, bdata.index_buffer_size_bytes, &indices[0], GL_STATIC_DRAW);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0);
- data.polygon_index_buffer_size = index_size;
- }
+ } // only if there is a vertex buffer (batching is on)
- store_transform(Transform(), state.canvas_item_ubo_data.projection_matrix);
+ // vertex array objects
+ for (int vao = 0; vao < 5; vao++) {
- glGenBuffers(1, &state.canvas_item_ubo);
- glBindBuffer(GL_UNIFORM_BUFFER, state.canvas_item_ubo);
- glBufferData(GL_UNIFORM_BUFFER, sizeof(CanvasItemUBO), &state.canvas_item_ubo_data, GL_DYNAMIC_DRAW);
- glBindBuffer(GL_UNIFORM_BUFFER, 0);
+ int sizeof_vert;
+ switch (vao) {
+ case 0:
+ sizeof_vert = sizeof(BatchVertex);
+ break;
+ case 1:
+ sizeof_vert = sizeof(BatchVertexColored);
+ break;
+ case 2:
+ sizeof_vert = sizeof(BatchVertexLightAngled);
+ break;
+ case 3:
+ sizeof_vert = sizeof(BatchVertexModulated);
+ break;
+ case 4:
+ sizeof_vert = sizeof(BatchVertexLarge);
+ break;
+ }
- state.canvas_shader.init();
- state.canvas_shader.set_base_material_tex_index(2);
- state.canvas_shadow_shader.init();
- state.lens_shader.init();
+ glGenVertexArrays(1, &batch_gl_data.batch_vertex_array[vao]);
+ glBindVertexArray(batch_gl_data.batch_vertex_array[vao]);
+ glBindBuffer(GL_ARRAY_BUFFER, bdata.gl_vertex_buffer);
+ glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, bdata.gl_index_buffer);
- state.canvas_shader.set_conditional(CanvasShaderGLES3::USE_RGBA_SHADOWS, storage->config.use_rgba_2d_shadows);
- state.canvas_shadow_shader.set_conditional(CanvasShadowShaderGLES3::USE_RGBA_SHADOWS, storage->config.use_rgba_2d_shadows);
+ uint64_t pointer = 0;
+ glEnableVertexAttribArray(VS::ARRAY_VERTEX);
+ glVertexAttribPointer(VS::ARRAY_VERTEX, 2, GL_FLOAT, GL_FALSE, sizeof_vert, (const void *)pointer);
- state.canvas_shader.set_conditional(CanvasShaderGLES3::USE_PIXEL_SNAP, GLOBAL_DEF("rendering/quality/2d/use_pixel_snap", false));
-}
+ // always send UVs, even within a texture specified because a shader can still use UVs
+ glEnableVertexAttribArray(VS::ARRAY_TEX_UV);
+ glVertexAttribPointer(VS::ARRAY_TEX_UV, 2, GL_FLOAT, GL_FALSE, sizeof_vert, CAST_INT_TO_UCHAR_PTR(pointer + (2 * 4)));
-void RasterizerCanvasGLES3::finalize() {
+ // optional attributes
+ bool a_color = false;
+ bool a_light_angle = false;
+ bool a_modulate = false;
+ bool a_large = false;
- glDeleteBuffers(1, &data.canvas_quad_vertices);
- glDeleteVertexArrays(1, &data.canvas_quad_array);
+ switch (vao) {
+ case 0:
+ break;
+ case 1: {
+ a_color = true;
+ } break;
+ case 2: {
+ a_color = true;
+ a_light_angle = true;
+ } break;
+ case 3: {
+ a_color = true;
+ a_light_angle = true;
+ a_modulate = true;
+ } break;
+ case 4: {
+ a_color = true;
+ a_light_angle = true;
+ a_modulate = true;
+ a_large = true;
+ } break;
+ }
- glDeleteBuffers(1, &data.canvas_quad_vertices);
- glDeleteVertexArrays(1, &data.canvas_quad_array);
+ if (a_color) {
+ glEnableVertexAttribArray(VS::ARRAY_COLOR);
+ glVertexAttribPointer(VS::ARRAY_COLOR, 4, GL_FLOAT, GL_FALSE, sizeof_vert, CAST_INT_TO_UCHAR_PTR(pointer + (4 * 4)));
+ }
+ if (a_light_angle) {
+ glEnableVertexAttribArray(VS::ARRAY_TANGENT);
+ glVertexAttribPointer(VS::ARRAY_TANGENT, 1, GL_FLOAT, GL_FALSE, sizeof_vert, CAST_INT_TO_UCHAR_PTR(pointer + (8 * 4)));
+ }
+ if (a_modulate) {
+ glEnableVertexAttribArray(VS::ARRAY_TEX_UV2);
+ glVertexAttribPointer(VS::ARRAY_TEX_UV2, 4, GL_FLOAT, GL_FALSE, sizeof_vert, CAST_INT_TO_UCHAR_PTR(pointer + (9 * 4)));
+ }
+ if (a_large) {
+ glEnableVertexAttribArray(VS::ARRAY_BONES);
+ glVertexAttribPointer(VS::ARRAY_BONES, 2, GL_FLOAT, GL_FALSE, sizeof_vert, CAST_INT_TO_UCHAR_PTR(pointer + (13 * 4)));
+ glEnableVertexAttribArray(VS::ARRAY_WEIGHTS);
+ glVertexAttribPointer(VS::ARRAY_WEIGHTS, 4, GL_FLOAT, GL_FALSE, sizeof_vert, CAST_INT_TO_UCHAR_PTR(pointer + (15 * 4)));
+ }
- glDeleteVertexArrays(1, &data.polygon_buffer_pointer_array);
+ glBindVertexArray(0);
+ } // for vao
+ gl_checkerror();
}
RasterizerCanvasGLES3::RasterizerCanvasGLES3() {
- // Not needed (a priori) on GLES devices
- use_nvidia_rect_workaround = false;
-#ifdef GLES_OVER_GL
- use_nvidia_rect_workaround = GLOBAL_GET("rendering/quality/2d/use_nvidia_rect_flicker_workaround");
-#endif
+ batch_constructor();
}
diff --git a/drivers/gles3/rasterizer_canvas_gles3.h b/drivers/gles3/rasterizer_canvas_gles3.h
index 5179e48332f..cec859fee7b 100644
--- a/drivers/gles3/rasterizer_canvas_gles3.h
+++ b/drivers/gles3/rasterizer_canvas_gles3.h
@@ -31,132 +31,49 @@
#ifndef RASTERIZERCANVASGLES3_H
#define RASTERIZERCANVASGLES3_H
-#include "rasterizer_storage_gles3.h"
-#include "servers/visual/rasterizer.h"
+#include "drivers/gles_common/rasterizer_canvas_batcher.h"
+#include "rasterizer_canvas_base_gles3.h"
-#include "shaders/canvas_shadow.glsl.gen.h"
-#include "shaders/lens_distorted.glsl.gen.h"
+class RasterizerCanvasGLES3 : public RasterizerCanvasBaseGLES3, public RasterizerCanvasBatcher {
+ friend class RasterizerCanvasBatcher;
-class RasterizerSceneGLES3;
+private:
+ struct BatchGLData {
+ // for batching
+ GLuint batch_vertex_array[5];
+ } batch_gl_data;
-class RasterizerCanvasGLES3 : public RasterizerCanvas {
public:
- struct CanvasItemUBO {
-
- float projection_matrix[16];
- float time;
- uint8_t padding[12];
- };
-
- RasterizerSceneGLES3 *scene_render;
-
- struct Data {
-
- enum { NUM_QUAD_ARRAY_VARIATIONS = 8 };
-
- GLuint canvas_quad_vertices;
- GLuint canvas_quad_array;
-
- GLuint polygon_buffer;
- GLuint polygon_buffer_quad_arrays[NUM_QUAD_ARRAY_VARIATIONS];
- GLuint polygon_buffer_pointer_array;
- GLuint polygon_index_buffer;
-
- GLuint particle_quad_vertices;
- GLuint particle_quad_array;
-
- uint32_t polygon_buffer_size;
- uint32_t polygon_index_buffer_size;
-
- } data;
-
- struct State {
- CanvasItemUBO canvas_item_ubo_data;
- GLuint canvas_item_ubo;
- bool canvas_texscreen_used;
- CanvasShaderGLES3 canvas_shader;
- CanvasShadowShaderGLES3 canvas_shadow_shader;
- LensDistortedShaderGLES3 lens_shader;
-
- bool using_texture_rect;
- bool using_ninepatch;
- bool using_light_angle;
-
- RID current_tex;
- RID current_normal;
- RasterizerStorageGLES3::Texture *current_tex_ptr;
-
- Transform vp;
-
- Color canvas_item_modulate;
- Transform2D extra_matrix;
- Transform2D final_transform;
- bool using_skeleton;
- Transform2D skeleton_transform;
- Transform2D skeleton_transform_inverse;
-
- } state;
-
- RasterizerStorageGLES3 *storage;
- bool use_nvidia_rect_workaround;
-
- struct LightInternal : public RID_Data {
-
- struct UBOData {
-
- float light_matrix[16];
- float local_matrix[16];
- float shadow_matrix[16];
- float color[4];
- float shadow_color[4];
- float light_pos[2];
- float shadowpixel_size;
- float shadow_gradient;
- float light_height;
- float light_outside_alpha;
- float shadow_distance_mult;
- uint8_t padding[4];
- } ubo_data;
-
- GLuint ubo;
- };
-
- RID_Owner light_internal_owner;
-
- virtual RID light_internal_create();
- virtual void light_internal_update(RID p_rid, Light *p_light);
- virtual void light_internal_free(RID p_rid);
-
+ virtual void canvas_render_items_begin(const Color &p_modulate, Light *p_light, const Transform2D &p_base_transform);
+ virtual void canvas_render_items_end();
+ virtual void canvas_render_items(Item *p_item_list, int p_z, const Color &p_modulate, Light *p_light, const Transform2D &p_base_transform);
virtual void canvas_begin();
virtual void canvas_end();
- _FORCE_INLINE_ void _set_texture_rect_mode(bool p_enable, bool p_ninepatch = false, bool p_light_angle = false);
- _FORCE_INLINE_ RasterizerStorageGLES3::Texture *_bind_canvas_texture(const RID &p_texture, const RID &p_normal_map, bool p_force = false);
+private:
+ // legacy codepath .. to remove after testing
+ void _legacy_canvas_render_item(Item *p_ci, RenderItemState &r_ris);
- _FORCE_INLINE_ void _draw_gui_primitive(int p_points, const Vector2 *p_vertices, const Color *p_colors, const Vector2 *p_uvs, const float *p_light_angles = nullptr);
- _FORCE_INLINE_ void _draw_polygon(const int *p_indices, int p_index_count, int p_vertex_count, const Vector2 *p_vertices, const Vector2 *p_uvs, const Color *p_colors, bool p_singlecolor, const int *p_bones, const float *p_weights);
- _FORCE_INLINE_ void _draw_generic(GLuint p_primitive, int p_vertex_count, const Vector2 *p_vertices, const Vector2 *p_uvs, const Color *p_colors, bool p_singlecolor);
- _FORCE_INLINE_ void _draw_generic_indices(GLuint p_primitive, const int *p_indices, int p_index_count, int p_vertex_count, const Vector2 *p_vertices, const Vector2 *p_uvs, const Color *p_colors, bool p_singlecolor);
+ // high level batch funcs
+ void canvas_render_items_implementation(Item *p_item_list, int p_z, const Color &p_modulate, Light *p_light, const Transform2D &p_base_transform);
+ void render_joined_item(const BItemJoined &p_bij, RenderItemState &r_ris);
+ bool try_join_item(Item *p_ci, RenderItemState &r_ris, bool &r_batch_break);
+ void render_batches(Item::Command *const *p_commands, Item *p_current_clip, bool &r_reclip, RasterizerStorageGLES3::Material *p_material);
- _FORCE_INLINE_ void _canvas_item_render_commands(Item *p_item, Item *current_clip, bool &reclip);
- _FORCE_INLINE_ void _copy_texscreen(const Rect2 &p_rect);
+ // low level batch funcs
+ void _batch_upload_buffers();
+ void _batch_render_rects(const Batch &p_batch, RasterizerStorageGLES3::Material *p_material);
+ void _batch_render_polys(const Batch &p_batch, RasterizerStorageGLES3::Material *p_material);
+ void _batch_render_lines(const Batch &p_batch, RasterizerStorageGLES3::Material *p_material, bool p_anti_alias);
- virtual void canvas_render_items(Item *p_item_list, int p_z, const Color &p_modulate, Light *p_light, const Transform2D &p_transform);
- virtual void canvas_debug_viewport_shadows(Light *p_lights_with_shadow);
+ // funcs used from rasterizer_canvas_batcher template
+ void gl_enable_scissor(int p_x, int p_y, int p_width, int p_height) const;
+ void gl_disable_scissor() const;
- virtual void canvas_light_shadow_buffer_update(RID p_buffer, const Transform2D &p_light_xform, int p_light_mask, float p_near, float p_far, LightOccluderInstance *p_occluders, CameraMatrix *p_xform_cache);
-
- virtual void reset_canvas();
-
- void draw_generic_textured_rect(const Rect2 &p_rect, const Rect2 &p_src);
- void draw_lens_distortion_rect(const Rect2 &p_rect, float p_k1, float p_k2, const Vector2 &p_eye_center, float p_oversample);
- void render_rect_nvidia_workaround(const Item::CommandRect *p_rect, const RasterizerStorageGLES3::Texture *p_texture);
+ void gl_checkerror();
+public:
void initialize();
- void finalize();
-
- virtual void draw_window_margins(int *black_margin, RID *black_image);
-
RasterizerCanvasGLES3();
};
diff --git a/drivers/gles3/rasterizer_gles3.h b/drivers/gles3/rasterizer_gles3.h
index b15e5af2947..0c443a40138 100644
--- a/drivers/gles3/rasterizer_gles3.h
+++ b/drivers/gles3/rasterizer_gles3.h
@@ -71,7 +71,7 @@ public:
virtual bool is_low_end() const { return false; }
- const char *gl_check_for_error(bool p_print_error = true);
+ virtual const char *gl_check_for_error(bool p_print_error = true);
RasterizerGLES3();
~RasterizerGLES3();
diff --git a/drivers/gles3/rasterizer_storage_gles3.cpp b/drivers/gles3/rasterizer_storage_gles3.cpp
index 296be971cc1..77021631c6e 100644
--- a/drivers/gles3/rasterizer_storage_gles3.cpp
+++ b/drivers/gles3/rasterizer_storage_gles3.cpp
@@ -2305,6 +2305,10 @@ void RasterizerStorageGLES3::_update_shader(Shader *p_shader) const {
p_shader->canvas_item.uses_screen_texture = false;
p_shader->canvas_item.uses_screen_uv = false;
p_shader->canvas_item.uses_time = false;
+ p_shader->canvas_item.uses_modulate = false;
+ p_shader->canvas_item.uses_color = false;
+ p_shader->canvas_item.uses_vertex = false;
+ p_shader->canvas_item.batch_flags = 0;
shaders.actions_canvas.render_mode_values["blend_add"] = Pair(&p_shader->canvas_item.blend_mode, Shader::CanvasItem::BLEND_MODE_ADD);
shaders.actions_canvas.render_mode_values["blend_mix"] = Pair(&p_shader->canvas_item.blend_mode, Shader::CanvasItem::BLEND_MODE_MIX);
@@ -2321,6 +2325,10 @@ void RasterizerStorageGLES3::_update_shader(Shader *p_shader) const {
shaders.actions_canvas.usage_flag_pointers["SCREEN_TEXTURE"] = &p_shader->canvas_item.uses_screen_texture;
shaders.actions_canvas.usage_flag_pointers["TIME"] = &p_shader->canvas_item.uses_time;
+ shaders.actions_canvas.usage_flag_pointers["MODULATE"] = &p_shader->canvas_item.uses_modulate;
+ shaders.actions_canvas.usage_flag_pointers["COLOR"] = &p_shader->canvas_item.uses_color;
+ shaders.actions_canvas.usage_flag_pointers["VERTEX"] = &p_shader->canvas_item.uses_vertex;
+
actions = &shaders.actions_canvas;
actions->uniforms = &p_shader->uniforms;
@@ -2417,6 +2425,16 @@ void RasterizerStorageGLES3::_update_shader(Shader *p_shader) const {
p_shader->uses_vertex_time = gen_code.uses_vertex_time;
p_shader->uses_fragment_time = gen_code.uses_fragment_time;
+ // some logic for batching
+ if (p_shader->mode == VS::SHADER_CANVAS_ITEM) {
+ if (p_shader->canvas_item.uses_modulate | p_shader->canvas_item.uses_color) {
+ p_shader->canvas_item.batch_flags |= RasterizerStorageCommon::PREVENT_COLOR_BAKING;
+ }
+ if (p_shader->canvas_item.uses_vertex) {
+ p_shader->canvas_item.batch_flags |= RasterizerStorageCommon::PREVENT_VERTEX_BAKING;
+ }
+ }
+
//all materials using this shader will have to be invalidated, unfortunately
for (SelfList *E = p_shader->materials.first(); E; E = E->next()) {
@@ -5067,7 +5085,8 @@ void RasterizerStorageGLES3::update_dirty_multimeshes() {
if (multimesh->size && multimesh->dirty_data) {
glBindBuffer(GL_ARRAY_BUFFER, multimesh->buffer);
- glBufferSubData(GL_ARRAY_BUFFER, 0, multimesh->data.size() * sizeof(float), multimesh->data.ptr());
+ uint32_t buffer_size = multimesh->data.size() * sizeof(float);
+ glBufferData(GL_ARRAY_BUFFER, buffer_size, multimesh->data.ptr(), GL_DYNAMIC_DRAW);
glBindBuffer(GL_ARRAY_BUFFER, 0);
}
@@ -8313,6 +8332,9 @@ void RasterizerStorageGLES3::initialize() {
#endif
+ // not yet detected on GLES3 (is this mandated?)
+ config.support_npot_repeat_mipmap = true;
+
config.pvrtc_supported = config.extensions.has("GL_IMG_texture_compression_pvrtc");
config.srgb_decode_supported = config.extensions.has("GL_EXT_texture_sRGB_decode");
diff --git a/drivers/gles3/rasterizer_storage_gles3.h b/drivers/gles3/rasterizer_storage_gles3.h
index 86c97d82126..98f0ab606e6 100644
--- a/drivers/gles3/rasterizer_storage_gles3.h
+++ b/drivers/gles3/rasterizer_storage_gles3.h
@@ -84,6 +84,7 @@ public:
bool srgb_decode_supported;
+ bool support_npot_repeat_mipmap;
bool texture_float_linear_supported;
bool framebuffer_float_supported;
bool framebuffer_half_float_supported;
@@ -455,9 +456,18 @@ public:
int light_mode;
+ // these flags are specifically for batching
+ // some of the logic is thus in rasterizer_storage.cpp
+ // we could alternatively set bitflags for each 'uses' and test on the fly
+ // defined in RasterizerStorageCommon::BatchFlags
+ unsigned int batch_flags;
+
bool uses_screen_texture;
bool uses_screen_uv;
bool uses_time;
+ bool uses_modulate;
+ bool uses_color;
+ bool uses_vertex;
} canvas_item;
diff --git a/drivers/gles3/shaders/canvas.glsl b/drivers/gles3/shaders/canvas.glsl
index 92f580a2b64..49919063f87 100644
--- a/drivers/gles3/shaders/canvas.glsl
+++ b/drivers/gles3/shaders/canvas.glsl
@@ -3,13 +3,23 @@
layout(location = 0) in highp vec2 vertex;
-#ifdef USE_LIGHT_ANGLE
+#ifdef USE_ATTRIB_LIGHT_ANGLE
layout(location = 2) in highp float light_angle;
#endif
/* clang-format on */
layout(location = 3) in vec4 color_attrib;
+#ifdef USE_ATTRIB_MODULATE
+layout(location = 5) in vec4 modulate_attrib; // attrib:5
+#endif
+
+#ifdef USE_ATTRIB_LARGE_VERTEX
+// shared with skeleton attributes, not used in batched shader
+layout(location = 6) in vec2 translate_attrib; // attrib:6
+layout(location = 7) in vec4 basis_attrib; // attrib:7
+#endif
+
#ifdef USE_SKELETON
layout(location = 6) in uvec4 bone_indices; // attrib:6
layout(location = 7) in vec4 bone_weights; // attrib:7
@@ -53,6 +63,12 @@ uniform highp mat4 extra_matrix;
out highp vec2 uv_interp;
out mediump vec4 color_interp;
+
+#ifdef USE_ATTRIB_MODULATE
+// modulate doesn't need interpolating but we need to send it to the fragment shader
+out mediump vec4 modulate_interp;
+#endif
+
#ifdef MODULATE_USED
uniform mediump vec4 final_modulate;
#endif
@@ -177,6 +193,23 @@ VERTEX_SHADER_CODE
pixel_size_interp = abs(dst_rect.zw) * vertex;
#endif
+#ifdef USE_ATTRIB_MODULATE
+ // modulate doesn't need interpolating but we need to send it to the fragment shader
+ modulate_interp = modulate_attrib;
+#endif
+
+#ifdef USE_ATTRIB_LARGE_VERTEX
+ // transform is in attributes
+ vec2 temp;
+
+ temp = outvec.xy;
+ temp.x = (outvec.x * basis_attrib.x) + (outvec.y * basis_attrib.z);
+ temp.y = (outvec.x * basis_attrib.y) + (outvec.y * basis_attrib.w);
+
+ temp += translate_attrib;
+ outvec.xy = temp;
+#endif
+
#if !defined(SKIP_TRANSFORM_USED)
outvec = extra_matrix * outvec;
outvec = modelview_matrix * outvec;
@@ -253,7 +286,7 @@ VERTEX_SHADER_CODE
pos = outvec.xy;
#endif
-#ifdef USE_LIGHT_ANGLE
+#ifdef USE_ATTRIB_LIGHT_ANGLE
// we add a fixed offset because we are using the sign later,
// and don't want floating point error around 0.0
float la = abs(light_angle) - 1.0;
@@ -294,6 +327,10 @@ uniform mediump sampler2D normal_texture; // texunit:1
in highp vec2 uv_interp;
in mediump vec4 color_interp;
+#ifdef USE_ATTRIB_MODULATE
+in mediump vec4 modulate_interp;
+#endif
+
#if defined(SCREEN_TEXTURE_USED)
uniform sampler2D screen_texture; // texunit:-3
@@ -410,16 +447,14 @@ uniform bool np_draw_center;
// left top right bottom in pixel coordinates
uniform vec4 np_margins;
-float map_ninepatch_axis(float pixel, float draw_size, float tex_pixel_size, float margin_begin, float margin_end, float s_ratio, int np_repeat, inout int draw_center) {
+float map_ninepatch_axis(float pixel, float draw_size, float tex_pixel_size, float margin_begin, float margin_end, int np_repeat, inout int draw_center) {
float tex_size = 1.0 / tex_pixel_size;
- float screen_margin_begin = margin_begin / s_ratio;
- float screen_margin_end = margin_end / s_ratio;
- if (pixel < screen_margin_begin) {
- return pixel * s_ratio * tex_pixel_size;
- } else if (pixel >= draw_size - screen_margin_end) {
- return (tex_size - (draw_size - pixel) * s_ratio) * tex_pixel_size;
+ if (pixel < margin_begin) {
+ return pixel * tex_pixel_size;
+ } else if (pixel >= draw_size - margin_end) {
+ return (tex_size - (draw_size - pixel)) * tex_pixel_size;
} else {
if (!np_draw_center) {
draw_center--;
@@ -428,21 +463,21 @@ float map_ninepatch_axis(float pixel, float draw_size, float tex_pixel_size, flo
// np_repeat is passed as uniform using NinePatchRect::AxisStretchMode enum.
if (np_repeat == 0) { // Stretch.
// Convert to ratio.
- float ratio = (pixel - screen_margin_begin) / (draw_size - screen_margin_begin - screen_margin_end);
+ float ratio = (pixel - margin_begin) / (draw_size - margin_begin - margin_end);
// Scale to source texture.
return (margin_begin + ratio * (tex_size - margin_begin - margin_end)) * tex_pixel_size;
} else if (np_repeat == 1) { // Tile.
// Convert to offset.
- float ofs = mod((pixel - screen_margin_begin), tex_size - margin_begin - margin_end);
+ float ofs = mod((pixel - margin_begin), tex_size - margin_begin - margin_end);
// Scale to source texture.
return (margin_begin + ofs) * tex_pixel_size;
} else if (np_repeat == 2) { // Tile Fit.
// Calculate scale.
- float src_area = draw_size - screen_margin_begin - screen_margin_end;
+ float src_area = draw_size - margin_begin - margin_end;
float dst_area = tex_size - margin_begin - margin_end;
float scale = max(1.0, floor(src_area / max(dst_area, 0.0000001) + 0.5));
// Convert to ratio.
- float ratio = (pixel - screen_margin_begin) / src_area;
+ float ratio = (pixel - margin_begin) / src_area;
ratio = mod(ratio * scale, 1.0);
// Scale to source texture.
return (margin_begin + ratio * dst_area) * tex_pixel_size;
@@ -467,11 +502,9 @@ void main() {
#ifdef USE_NINEPATCH
int draw_center = 2;
- float s_ratio = max((1.0 / color_texpixel_size.x) / abs(dst_rect.z), (1.0 / color_texpixel_size.y) / abs(dst_rect.w));
- s_ratio = max(1.0, s_ratio);
uv = vec2(
- map_ninepatch_axis(pixel_size_interp.x, abs(dst_rect.z), color_texpixel_size.x, np_margins.x, np_margins.z, s_ratio, np_repeat_h, draw_center),
- map_ninepatch_axis(pixel_size_interp.y, abs(dst_rect.w), color_texpixel_size.y, np_margins.y, np_margins.w, s_ratio, np_repeat_v, draw_center));
+ map_ninepatch_axis(pixel_size_interp.x, abs(dst_rect.z), color_texpixel_size.x, np_margins.x, np_margins.z, np_repeat_h, draw_center),
+ map_ninepatch_axis(pixel_size_interp.y, abs(dst_rect.w), color_texpixel_size.y, np_margins.y, np_margins.w, np_repeat_v, draw_center));
if (draw_center == 0) {
color.a = 0.0;
@@ -549,6 +582,11 @@ FRAGMENT_SHADER_CODE
color *= final_modulate;
#endif
+#ifdef USE_ATTRIB_MODULATE
+ // todo .. this won't be used at the same time as MODULATE_USED
+ color *= modulate_interp;
+#endif
+
#ifdef USE_LIGHTING
vec2 light_vec = transformed_light_uv;
diff --git a/drivers/gles_common/SCsub b/drivers/gles_common/SCsub
new file mode 100644
index 00000000000..91e1140b750
--- /dev/null
+++ b/drivers/gles_common/SCsub
@@ -0,0 +1,5 @@
+#!/usr/bin/env python
+
+Import("env")
+
+env.add_source_files(env.drivers_sources, "*.cpp")
diff --git a/drivers/gles_common/batch_diagnose.inc b/drivers/gles_common/batch_diagnose.inc
new file mode 100644
index 00000000000..46b0a72825d
--- /dev/null
+++ b/drivers/gles_common/batch_diagnose.inc
@@ -0,0 +1,148 @@
+String get_command_type_string(const RasterizerCanvas::Item::Command &p_command) const {
+ String sz = "";
+
+ switch (p_command.type) {
+ default:
+ break;
+ case RasterizerCanvas::Item::Command::TYPE_LINE: {
+ sz = "l";
+ } break;
+ case RasterizerCanvas::Item::Command::TYPE_POLYLINE: {
+ sz = "PL";
+ } break;
+ case RasterizerCanvas::Item::Command::TYPE_RECT: {
+ sz = "r";
+ } break;
+ case RasterizerCanvas::Item::Command::TYPE_NINEPATCH: {
+ sz = "n";
+ } break;
+ case RasterizerCanvas::Item::Command::TYPE_PRIMITIVE: {
+ sz = "PR";
+ } break;
+ case RasterizerCanvas::Item::Command::TYPE_POLYGON: {
+ sz = "p";
+ } break;
+ case RasterizerCanvas::Item::Command::TYPE_MESH: {
+ sz = "m";
+ } break;
+ case RasterizerCanvas::Item::Command::TYPE_MULTIMESH: {
+ sz = "MM";
+ } break;
+ case RasterizerCanvas::Item::Command::TYPE_PARTICLES: {
+ sz = "PA";
+ } break;
+ case RasterizerCanvas::Item::Command::TYPE_CIRCLE: {
+ sz = "c";
+ } break;
+ case RasterizerCanvas::Item::Command::TYPE_TRANSFORM: {
+ sz = "t";
+
+ // add a bit more info in debug build
+ const RasterizerCanvas::Item::CommandTransform *transform = static_cast(&p_command);
+ const Transform2D &mat = transform->xform;
+
+ sz += " ";
+ sz += String(Variant(mat.elements[2]));
+ sz += " ";
+ } break;
+ case RasterizerCanvas::Item::Command::TYPE_CLIP_IGNORE: {
+ sz = "CI";
+ } break;
+ } // switch
+
+ return sz;
+}
+
+void diagnose_batches(RasterizerCanvas::Item::Command *const *p_commands) {
+ int num_batches = bdata.batches.size();
+
+ BatchColor curr_color;
+ curr_color.set(Color(-1, -1, -1, -1));
+ bool first_color_change = true;
+
+ for (int batch_num = 0; batch_num < num_batches; batch_num++) {
+ const Batch &batch = bdata.batches[batch_num];
+ bdata.frame_string += "\t\t\tbatch ";
+
+ switch (batch.type) {
+
+ case RasterizerStorageCommon::BT_POLY: {
+ bdata.frame_string += "P ";
+ bdata.frame_string += itos(batch.first_command) + "-";
+ bdata.frame_string += itos(batch.num_commands);
+
+ bdata.frame_string += " " + batch.color.to_string();
+
+ if (batch.num_commands > 1) {
+ bdata.frame_string += " MULTI";
+ }
+ if (curr_color != batch.color) {
+ curr_color = batch.color;
+ if (!first_color_change) {
+ bdata.frame_string += " color";
+ } else {
+ first_color_change = false;
+ }
+ }
+ bdata.frame_string += "\n";
+ } break;
+ case RasterizerStorageCommon::BT_LINE:
+ case RasterizerStorageCommon::BT_LINE_AA: {
+ bdata.frame_string += "L ";
+ bdata.frame_string += itos(batch.first_command) + "-";
+ bdata.frame_string += itos(batch.num_commands);
+
+ bdata.frame_string += " " + batch.color.to_string();
+
+ if (batch.num_commands > 1) {
+ bdata.frame_string += " MULTI";
+ }
+ if (curr_color != batch.color) {
+ curr_color = batch.color;
+ if (!first_color_change) {
+ bdata.frame_string += " color";
+ } else {
+ first_color_change = false;
+ }
+ }
+ bdata.frame_string += "\n";
+ } break;
+ case RasterizerStorageCommon::BT_RECT: {
+ bdata.frame_string += "R ";
+ bdata.frame_string += itos(batch.first_command) + "-";
+ bdata.frame_string += itos(batch.num_commands);
+
+ int tex_id = (int)bdata.batch_textures[batch.batch_texture_id].RID_texture.get_id();
+ bdata.frame_string += " [" + itos(batch.batch_texture_id) + " - " + itos(tex_id) + "]";
+
+ bdata.frame_string += " " + batch.color.to_string();
+
+ if (batch.num_commands > 1) {
+ bdata.frame_string += " MULTI";
+ }
+ if (curr_color != batch.color) {
+ curr_color = batch.color;
+ if (!first_color_change) {
+ bdata.frame_string += " color";
+ } else {
+ first_color_change = false;
+ }
+ }
+ bdata.frame_string += "\n";
+ } break;
+ default: {
+ bdata.frame_string += "D ";
+ bdata.frame_string += itos(batch.first_command) + "-";
+ bdata.frame_string += itos(batch.num_commands) + " ";
+
+ int num_show = MIN(batch.num_commands, 16);
+ for (int n = 0; n < num_show; n++) {
+ const RasterizerCanvas::Item::Command &comm = *p_commands[batch.first_command + n];
+ bdata.frame_string += get_command_type_string(comm) + " ";
+ }
+
+ bdata.frame_string += "\n";
+ } break;
+ }
+ }
+}
diff --git a/drivers/gles2/rasterizer_array_gles2.h b/drivers/gles_common/rasterizer_array.h
similarity index 72%
rename from drivers/gles2/rasterizer_array_gles2.h
rename to drivers/gles_common/rasterizer_array.h
index 64588934d8c..13360ba23b6 100644
--- a/drivers/gles2/rasterizer_array_gles2.h
+++ b/drivers/gles_common/rasterizer_array.h
@@ -1,5 +1,5 @@
/*************************************************************************/
-/* rasterizer_array_gles2.h */
+/* rasterizer_array.h */
/*************************************************************************/
/* This file is part of: */
/* GODOT ENGINE */
@@ -30,36 +30,6 @@
#pragma once
-/*************************************************************************/
-/* rasterizer_array_gles2.h */
-/*************************************************************************/
-/* This file is part of: */
-/* GODOT ENGINE */
-/* https://godotengine.org */
-/*************************************************************************/
-/* Copyright (c) 2007-2020 Juan Linietsky, Ariel Manzur. */
-/* Copyright (c) 2014-2020 Godot Engine contributors (cf. AUTHORS.md). */
-/* */
-/* Permission is hereby granted, free of charge, to any person obtaining */
-/* a copy of this software and associated documentation files (the */
-/* "Software"), to deal in the Software without restriction, including */
-/* without limitation the rights to use, copy, modify, merge, publish, */
-/* distribute, sublicense, and/or sell copies of the Software, and to */
-/* permit persons to whom the Software is furnished to do so, subject to */
-/* the following conditions: */
-/* */
-/* The above copyright notice and this permission notice shall be */
-/* included in all copies or substantial portions of the Software. */
-/* */
-/* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
-/* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
-/* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.*/
-/* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
-/* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
-/* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
-/* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
-/*************************************************************************/
-
/**
* Fast single-threaded growable array for POD types.
* For use in render drivers, not for general use.
@@ -141,14 +111,14 @@ private:
};
template
-class RasterizerArrayGLES2 {
+class RasterizerArray {
public:
- RasterizerArrayGLES2() {
+ RasterizerArray() {
_list = 0;
_size = 0;
_max_size = 0;
}
- ~RasterizerArrayGLES2() { free(); }
+ ~RasterizerArray() { free(); }
T &operator[](unsigned int ui) { return _list[ui]; }
const T &operator[](unsigned int ui) const { return _list[ui]; }
@@ -208,7 +178,7 @@ public:
int max_size() const { return _max_size; }
const T *get_data() const { return _list; }
- bool copy_from(const RasterizerArrayGLES2 &o) {
+ bool copy_from(const RasterizerArray &o) {
// no resizing done here, it should be done manually
if (o.size() > _max_size)
return false;
@@ -247,9 +217,9 @@ private:
};
template
-class RasterizerArray_non_pod_GLES2 {
+class RasterizerArray_non_pod {
public:
- RasterizerArray_non_pod_GLES2() {
+ RasterizerArray_non_pod() {
_size = 0;
}
@@ -287,3 +257,72 @@ private:
Vector _list;
int _size;
};
+
+// very simple non-growable array, that keeps track of the size of a 'unit'
+// which can be cast to whatever vertex format FVF required, and is initially
+// created with enough memory to hold the biggest FVF.
+// This allows multiple FVFs to use the same array.
+class RasterizerUnitArray {
+public:
+ RasterizerUnitArray() {
+ _list = nullptr;
+ free();
+ }
+ ~RasterizerUnitArray() { free(); }
+
+ uint8_t *get_unit(unsigned int ui) { return &_list[ui * _unit_size_bytes]; }
+ const uint8_t *get_unit(unsigned int ui) const { return &_list[ui * _unit_size_bytes]; }
+
+ int size() const { return _size; }
+ int max_size() const { return _max_size; }
+
+ void free() {
+ if (_list) {
+ memdelete_arr(_list);
+ _list = 0;
+ }
+ _size = 0;
+ _max_size = 0;
+ _max_size_bytes = 0;
+ _unit_size_bytes = 0;
+ }
+
+ void create(int p_max_size_units, int p_max_unit_size_bytes) {
+ free();
+
+ _max_unit_size_bytes = p_max_unit_size_bytes;
+ _max_size = p_max_size_units;
+ _max_size_bytes = p_max_size_units * p_max_unit_size_bytes;
+
+ if (_max_size_bytes) {
+ _list = memnew_arr(uint8_t, _max_size_bytes);
+ }
+ }
+
+ void prepare(int p_unit_size_bytes) {
+ _unit_size_bytes = p_unit_size_bytes;
+ _size = 0;
+ }
+
+ // several items at a time
+ uint8_t *request(int p_num_items = 1) {
+ int old_size = _size;
+ _size += p_num_items;
+
+ if (_size <= _max_size) {
+ return get_unit(old_size);
+ }
+
+ // revert
+ _size = old_size;
+ return nullptr;
+ }
+
+private:
+ uint8_t *_list;
+ int _size; // in units
+ int _max_size; // in units
+ int _max_size_bytes;
+ int _unit_size_bytes;
+ int _max_unit_size_bytes;
+};
diff --git a/drivers/gles_common/rasterizer_canvas_batcher.h b/drivers/gles_common/rasterizer_canvas_batcher.h
new file mode 100644
index 00000000000..83b6a2d86db
--- /dev/null
+++ b/drivers/gles_common/rasterizer_canvas_batcher.h
@@ -0,0 +1,2926 @@
+/*************************************************************************/
+/* rasterizer_canvas_batcher.h */
+/*************************************************************************/
+/* This file is part of: */
+/* GODOT ENGINE */
+/* https://godotengine.org */
+/*************************************************************************/
+/* Copyright (c) 2007-2020 Juan Linietsky, Ariel Manzur. */
+/* Copyright (c) 2014-2020 Godot Engine contributors (cf. AUTHORS.md). */
+/* */
+/* Permission is hereby granted, free of charge, to any person obtaining */
+/* a copy of this software and associated documentation files (the */
+/* "Software"), to deal in the Software without restriction, including */
+/* without limitation the rights to use, copy, modify, merge, publish, */
+/* distribute, sublicense, and/or sell copies of the Software, and to */
+/* permit persons to whom the Software is furnished to do so, subject to */
+/* the following conditions: */
+/* */
+/* The above copyright notice and this permission notice shall be */
+/* included in all copies or substantial portions of the Software. */
+/* */
+/* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
+/* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
+/* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.*/
+/* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
+/* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
+/* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
+/* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
+/*************************************************************************/
+
+#pragma once
+
+#include "core/os/os.h"
+#include "core/project_settings.h"
+#include "rasterizer_array.h"
+#include "rasterizer_storage_common.h"
+#include "servers/visual/rasterizer.h"
+
+// We are using the curiously recurring template pattern
+// https://en.wikipedia.org/wiki/Curiously_recurring_template_pattern
+// For static polymorphism.
+
+// This makes it super easy to access
+// data / call funcs in the derived rasterizers from the base without writing and
+// maintaining a boatload of virtual functions.
+// In addition it assures that vtable will not be used and the function calls can be optimized,
+// because it gives compile time static polymorphism.
+
+// These macros makes it simpler and less verbose to define (and redefine) the inline functions
+// template preamble
+#define T_PREAMBLE template
+// class preamble
+#define C_PREAMBLE RasterizerCanvasBatcher
+// generic preamble
+#define PREAMBLE(RET_T) \
+ T_PREAMBLE \
+ RET_T C_PREAMBLE
+
+template
+class RasterizerCanvasBatcher {
+
+public:
+ // used to determine whether we use hardware transform (none)
+ // software transform all verts, or software transform just a translate
+ // (no rotate or scale)
+ enum TransformMode {
+ TM_NONE,
+ TM_ALL,
+ TM_TRANSLATE,
+ };
+
+ // pod versions of vector and color and RID, need to be 32 bit for vertex format
+ struct BatchVector2 {
+ float x, y;
+ void set(float xx, float yy) {
+ x = xx;
+ y = yy;
+ }
+ void set(const Vector2 &p_o) {
+ x = p_o.x;
+ y = p_o.y;
+ }
+ void to(Vector2 &r_o) const {
+ r_o.x = x;
+ r_o.y = y;
+ }
+ };
+
+ struct BatchColor {
+ float r, g, b, a;
+ void set_white() {
+ r = 1.0f;
+ g = 1.0f;
+ b = 1.0f;
+ a = 1.0f;
+ }
+ void set(const Color &p_c) {
+ r = p_c.r;
+ g = p_c.g;
+ b = p_c.b;
+ a = p_c.a;
+ }
+ bool operator==(const BatchColor &p_c) const {
+ return (r == p_c.r) && (g == p_c.g) && (b == p_c.b) && (a == p_c.a);
+ }
+ bool operator!=(const BatchColor &p_c) const { return (*this == p_c) == false; }
+ bool equals(const Color &p_c) const {
+ return (r == p_c.r) && (g == p_c.g) && (b == p_c.b) && (a == p_c.a);
+ }
+ const float *get_data() const { return &r; }
+ String to_string() const {
+ String sz = "{";
+ const float *data = get_data();
+ for (int c = 0; c < 4; c++) {
+ float f = data[c];
+ int val = ((f * 255.0f) + 0.5f);
+ sz += String(Variant(val)) + " ";
+ }
+ sz += "}";
+ return sz;
+ }
+ };
+
+ // simplest FVF - local or baked position
+ struct BatchVertex {
+ // must be 32 bit pod
+ BatchVector2 pos;
+ BatchVector2 uv;
+ };
+
+ // simple FVF but also incorporating baked color
+ struct BatchVertexColored : public BatchVertex {
+ // must be 32 bit pod
+ BatchColor col;
+ };
+
+ // if we are using normal mapping, we need light angles to be sent
+ struct BatchVertexLightAngled : public BatchVertexColored {
+ // must be pod
+ float light_angle;
+ };
+
+ // CUSTOM SHADER vertex formats. These are larger but will probably
+ // be needed with custom shaders in order to have the data accessible in the shader.
+
+ // if we are using COLOR in vertex shader but not position (VERTEX)
+ struct BatchVertexModulated : public BatchVertexLightAngled {
+ BatchColor modulate;
+ };
+
+ struct BatchTransform {
+ BatchVector2 translate;
+ BatchVector2 basis[2];
+ };
+
+ // last resort, specially for custom shader, we put everything possible into a huge FVF
+ // not very efficient, but better than no batching at all.
+ struct BatchVertexLarge : public BatchVertexModulated {
+ // must be pod
+ BatchTransform transform;
+ };
+
+ // Batch should be as small as possible, and ideally nicely aligned (is 32 bytes at the moment)
+ struct Batch {
+ RasterizerStorageCommon::BatchType type; // should be 16 bit
+ uint16_t batch_texture_id;
+
+ // also item reference number
+ uint32_t first_command;
+
+ // in the case of DEFAULT, this is num commands.
+ // with rects, is number of command and rects.
+ // with lines, is number of lines
+ uint32_t num_commands;
+
+ // first vertex of this batch in the vertex lists
+ uint32_t first_vert;
+
+ BatchColor color;
+ };
+
+ struct BatchTex {
+ enum TileMode : uint32_t {
+ TILE_OFF,
+ TILE_NORMAL,
+ TILE_FORCE_REPEAT,
+ };
+ RID RID_texture;
+ RID RID_normal;
+ TileMode tile_mode;
+ BatchVector2 tex_pixel_size;
+ uint32_t flags;
+ };
+
+ // items in a list to be sorted prior to joining
+ struct BSortItem {
+ // have a function to keep as pod, rather than operator
+ void assign(const BSortItem &o) {
+ item = o.item;
+ z_index = o.z_index;
+ }
+ RasterizerCanvas::Item *item;
+ int z_index;
+ };
+
+ // batch item may represent 1 or more items
+ struct BItemJoined {
+ uint32_t first_item_ref;
+ uint32_t num_item_refs;
+
+ Rect2 bounding_rect;
+
+ // note the z_index may only be correct for the first of the joined item references
+ // this has implications for light culling with z ranged lights.
+ int16_t z_index;
+
+ // these are defined in RasterizerStorageCommon::BatchFlags
+ uint16_t flags;
+
+ // we are always splitting items with lots of commands,
+ // and items with unhandled primitives (default)
+ bool use_hardware_transform() const { return num_item_refs == 1; }
+ };
+
+ struct BItemRef {
+ RasterizerCanvas::Item *item;
+ Color final_modulate;
+ };
+
+ struct BLightRegion {
+ void reset() {
+ light_bitfield = 0;
+ shadow_bitfield = 0;
+ too_many_lights = false;
+ }
+ uint64_t light_bitfield;
+ uint64_t shadow_bitfield;
+ bool too_many_lights; // we can only do light region optimization if there are 64 or less lights
+ };
+
+ struct BatchData {
+
+ BatchData() {
+ reset_flush();
+ reset_joined_item();
+
+ gl_vertex_buffer = 0;
+ gl_index_buffer = 0;
+ max_quads = 0;
+ vertex_buffer_size_units = 0;
+ vertex_buffer_size_bytes = 0;
+ index_buffer_size_units = 0;
+ index_buffer_size_bytes = 0;
+
+ use_colored_vertices = false;
+
+ settings_use_batching = false;
+ settings_max_join_item_commands = 0;
+ settings_colored_vertex_format_threshold = 0.0f;
+ settings_batch_buffer_num_verts = 0;
+ scissor_threshold_area = 0.0f;
+ joined_item_batch_flags = 0;
+ diagnose_frame = false;
+ next_diagnose_tick = 10000;
+ diagnose_frame_number = 9999999999; // some high number
+ join_across_z_indices = true;
+ settings_item_reordering_lookahead = 0;
+
+ settings_use_batching_original_choice = false;
+ settings_flash_batching = false;
+ settings_diagnose_frame = false;
+ settings_scissor_lights = false;
+ settings_scissor_threshold = -1.0f;
+ settings_use_single_rect_fallback = false;
+ settings_use_software_skinning = true;
+ settings_ninepatch_mode = 0; // default
+ settings_light_max_join_items = 16;
+
+ settings_uv_contract = false;
+ settings_uv_contract_amount = 0.0f;
+
+ stats_items_sorted = 0;
+ stats_light_items_joined = 0;
+ }
+
+ // called for each joined item
+ void reset_joined_item() {
+ // noop but left in as a stub
+ }
+
+ // called after each flush
+ void reset_flush() {
+ batches.reset();
+ batch_textures.reset();
+
+ vertices.reset();
+ light_angles.reset();
+ vertex_colors.reset();
+ vertex_modulates.reset();
+ vertex_transforms.reset();
+
+ total_quads = 0;
+ total_verts = 0;
+ total_color_changes = 0;
+
+ use_light_angles = false;
+ use_modulate = false;
+ use_large_verts = false;
+ fvf = RasterizerStorageCommon::FVF_REGULAR;
+ }
+
+ unsigned int gl_vertex_buffer;
+ unsigned int gl_index_buffer;
+
+ uint32_t max_quads;
+ uint32_t vertex_buffer_size_units;
+ uint32_t vertex_buffer_size_bytes;
+ uint32_t index_buffer_size_units;
+ uint32_t index_buffer_size_bytes;
+
+ // small vertex FVF type - pos and UV.
+ // This will always be written to initially, but can be translated
+ // to larger FVFs if necessary.
+ RasterizerArray vertices;
+
+ // extra data which can be stored during prefilling, for later translation to larger FVFs
+ RasterizerArray light_angles;
+ RasterizerArray vertex_colors; // these aren't usually used, but are for polys
+ RasterizerArray vertex_modulates;
+ RasterizerArray vertex_transforms;
+
+ // instead of having a different buffer for each vertex FVF type
+ // we have a special array big enough for the biggest FVF
+ // which can have a changeable unit size, and reuse it.
+ RasterizerUnitArray unit_vertices;
+
+ RasterizerArray batches;
+ RasterizerArray batches_temp; // used for translating to colored vertex batches
+ RasterizerArray_non_pod batch_textures; // the only reason this is non-POD is because of RIDs
+
+ // SHOULD THESE BE IN FILLSTATE?
+ // flexible vertex format.
+ // all verts have pos and UV.
+ // some have color, some light angles etc.
+ RasterizerStorageCommon::FVF fvf;
+ bool use_colored_vertices;
+ bool use_light_angles;
+ bool use_modulate;
+ bool use_large_verts;
+
+ // if the shader is using MODULATE, we prevent baking color so the final_modulate can
+ // be read in the shader.
+ // if the shader is reading VERTEX, we prevent baking vertex positions with extra matrices etc
+ // to prevent the read position being incorrect.
+ // These flags are defined in RasterizerStorageCommon::BatchFlags
+ uint32_t joined_item_batch_flags;
+
+ RasterizerArray items_joined;
+ RasterizerArray item_refs;
+
+ // items are sorted prior to joining
+ RasterizerArray sort_items;
+
+ // counts
+ int total_quads;
+ int total_verts;
+
+ // we keep a record of how many color changes caused new batches
+ // if the colors are causing an excessive number of batches, we switch
+ // to alternate batching method and add color to the vertex format.
+ int total_color_changes;
+
+ // measured in pixels, recalculated each frame
+ float scissor_threshold_area;
+
+ // diagnose this frame, every nTh frame when settings_diagnose_frame is on
+ bool diagnose_frame;
+ String frame_string;
+ uint32_t next_diagnose_tick;
+ uint64_t diagnose_frame_number;
+
+ // whether to join items across z_indices - this can interfere with z ranged lights,
+ // so has to be disabled in some circumstances
+ bool join_across_z_indices;
+
+ // global settings
+ bool settings_use_batching; // the current use_batching (affected by flash)
+ bool settings_use_batching_original_choice; // the choice entered in project settings
+ bool settings_flash_batching; // for regression testing, flash between non-batched and batched renderer
+ bool settings_diagnose_frame; // print out batches to help optimize / regression test
+ int settings_max_join_item_commands;
+ float settings_colored_vertex_format_threshold;
+ int settings_batch_buffer_num_verts;
+ bool settings_scissor_lights;
+ float settings_scissor_threshold; // 0.0 to 1.0
+ int settings_item_reordering_lookahead;
+ bool settings_use_single_rect_fallback;
+ bool settings_use_software_skinning;
+ int settings_light_max_join_items;
+ int settings_ninepatch_mode;
+
+ // uv contraction
+ bool settings_uv_contract;
+ float settings_uv_contract_amount;
+
+ // only done on diagnose frame
+ void reset_stats() {
+ stats_items_sorted = 0;
+ stats_light_items_joined = 0;
+ }
+
+ // frame stats (just for monitoring and debugging)
+ int stats_items_sorted;
+ int stats_light_items_joined;
+ } bdata;
+
+ struct FillState {
+ void reset_flush() {
+ // don't reset members that need to be preserved after flushing
+ // half way through a list of commands
+ curr_batch = 0;
+ batch_tex_id = -1;
+ texpixel_size = Vector2(1, 1);
+ contract_uvs = false;
+
+ sequence_batch_type_flags = 0;
+ }
+
+ void reset_joined_item(bool p_use_hardware_transform) {
+ reset_flush();
+ use_hardware_transform = p_use_hardware_transform;
+ extra_matrix_sent = false;
+ }
+
+ // for batching multiple types, we don't allow mixing RECTs / LINEs etc.
+ // using flags allows quicker rejection of sequences with different batch types
+ uint32_t sequence_batch_type_flags;
+
+ Batch *curr_batch;
+ int batch_tex_id;
+ bool use_hardware_transform;
+ bool contract_uvs;
+ Vector2 texpixel_size;
+ Color final_modulate;
+ TransformMode transform_mode;
+ TransformMode orig_transform_mode;
+
+ // support for extra matrices
+ bool extra_matrix_sent; // whether sent on this item (in which case sofware transform can't be used untl end of item)
+ int transform_extra_command_number_p1; // plus one to allow fast checking against zero
+ Transform2D transform_combined; // final * extra
+ };
+
+ // used during try_join
+ struct RenderItemState {
+ RenderItemState() { reset(); }
+ void reset() {
+ current_clip = nullptr;
+ shader_cache = nullptr;
+ rebind_shader = true;
+ prev_use_skeleton = false;
+ last_blend_mode = -1;
+ canvas_last_material = RID();
+ item_group_z = 0;
+ item_group_light = nullptr;
+ final_modulate = Color(-1.0, -1.0, -1.0, -1.0); // just something unlikely
+
+ joined_item_batch_type_flags_curr = 0;
+ joined_item_batch_type_flags_prev = 0;
+
+ joined_item = nullptr;
+ }
+
+ RasterizerCanvas::Item *current_clip;
+ typename T_STORAGE::Shader *shader_cache;
+ bool rebind_shader;
+ bool prev_use_skeleton;
+ bool prev_distance_field;
+ int last_blend_mode;
+ RID canvas_last_material;
+ Color final_modulate;
+
+ // used for joining items only
+ BItemJoined *joined_item;
+ bool join_batch_break;
+ BLightRegion light_region;
+
+ // we need some logic to prevent joining items that have vastly different batch types
+ // these are defined in RasterizerStorageCommon::BatchTypeFlags
+ uint32_t joined_item_batch_type_flags_curr;
+ uint32_t joined_item_batch_type_flags_prev;
+
+ // 'item group' is data over a single call to canvas_render_items
+ int item_group_z;
+ Color item_group_modulate;
+ RasterizerCanvas::Light *item_group_light;
+ Transform2D item_group_base_transform;
+ } _render_item_state;
+
+ bool use_nvidia_rect_workaround;
+
+ //////////////////////////////////////////////////////////////////////////////
+ // End of structs used by the batcher. Beginning of funcs.
+private:
+ // curiously recurring template pattern - allows access to functions in the DERIVED class
+ // this is kind of like using virtual functions but more efficient as they are resolved at compile time
+ T_STORAGE *get_storage() { return static_cast(this)->storage; }
+ const T_STORAGE *get_storage() const { return static_cast(this)->storage; }
+ T *get_this() { return static_cast(this); }
+ const T *get_this() const { return static_cast(this); }
+
+protected:
+ // main functions called from the rasterizer canvas
+ void batch_constructor();
+ void batch_initialize();
+
+ void batch_canvas_begin();
+ void batch_canvas_end();
+ void batch_canvas_render_items_begin(const Color &p_modulate, RasterizerCanvas::Light *p_light, const Transform2D &p_base_transform);
+ void batch_canvas_render_items_end();
+ void batch_canvas_render_items(RasterizerCanvas::Item *p_item_list, int p_z, const Color &p_modulate, RasterizerCanvas::Light *p_light, const Transform2D &p_base_transform);
+
+ // recording and sorting items from the initial pass
+ void record_items(RasterizerCanvas::Item *p_item_list, int p_z);
+ void join_sorted_items();
+ void sort_items();
+ bool _sort_items_match(const BSortItem &p_a, const BSortItem &p_b) const;
+ bool sort_items_from(int p_start);
+
+ // joining logic
+ bool _disallow_item_join_if_batch_types_too_different(RenderItemState &r_ris, uint32_t btf_allowed);
+ bool _detect_item_batch_break(RenderItemState &r_ris, RasterizerCanvas::Item *p_ci, bool &r_batch_break);
+
+ // drives the loop filling batches and flushing
+ void render_joined_item_commands(const BItemJoined &p_bij, RasterizerCanvas::Item *p_current_clip, bool &r_reclip, typename T_STORAGE::Material *p_material, bool p_lit);
+
+private:
+ // flush once full or end of joined item
+ void flush_render_batches(RasterizerCanvas::Item *p_first_item, RasterizerCanvas::Item *p_current_clip, bool &r_reclip, typename T_STORAGE::Material *p_material);
+
+ // a single joined item can contain multiple itemrefs, and thus create lots of batches
+ bool prefill_joined_item(FillState &r_fill_state, int &r_command_start, RasterizerCanvas::Item *p_item, RasterizerCanvas::Item *p_current_clip, bool &r_reclip, typename T_STORAGE::Material *p_material);
+
+ // prefilling different types of batch
+
+ // default batch is an 'unhandled' legacy type batch that will be drawn with the legacy path,
+ // all other batches are accelerated.
+ void _prefill_default_batch(FillState &r_fill_state, int p_command_num, const RasterizerCanvas::Item &p_item);
+
+ // accelerated batches
+ bool _prefill_line(RasterizerCanvas::Item::CommandLine *p_line, FillState &r_fill_state, int &r_command_start, int command_num, int command_count, RasterizerCanvas::Item *p_item, bool multiply_final_modulate);
+ template
+ bool _prefill_ninepatch(RasterizerCanvas::Item::CommandNinePatch *p_np, FillState &r_fill_state, int &r_command_start, int command_num, int command_count, RasterizerCanvas::Item *p_item, bool multiply_final_modulate);
+ template
+ bool _prefill_polygon(RasterizerCanvas::Item::CommandPolygon *p_poly, FillState &r_fill_state, int &r_command_start, int command_num, int command_count, RasterizerCanvas::Item *p_item, bool multiply_final_modulate);
+ template
+ bool _prefill_rect(RasterizerCanvas::Item::CommandRect *rect, FillState &r_fill_state, int &r_command_start, int command_num, int command_count, RasterizerCanvas::Item::Command *const *commands, RasterizerCanvas::Item *p_item, bool multiply_final_modulate);
+
+ // dealing with textures
+ int _batch_find_or_create_tex(const RID &p_texture, const RID &p_normal, bool p_tile, int p_previous_match);
+
+protected:
+ // legacy support for non batched mode
+ void _legacy_canvas_item_render_commands(RasterizerCanvas::Item *p_item, RasterizerCanvas::Item *p_current_clip, bool &r_reclip, typename T_STORAGE::Material *p_material);
+
+ // light scissoring
+ bool _light_scissor_begin(const Rect2 &p_item_rect, const Transform2D &p_light_xform, const Rect2 &p_light_rect) const;
+ bool _light_find_intersection(const Rect2 &p_item_rect, const Transform2D &p_light_xform, const Rect2 &p_light_rect, Rect2 &r_cliprect) const;
+ void _calculate_scissor_threshold_area();
+
+private:
+ // translating vertex formats prior to rendering
+ void _translate_batches_to_vertex_colored_FVF();
+ template
+ void _translate_batches_to_larger_FVF();
+
+protected:
+ // accessory funcs
+ void _software_transform_vertex(BatchVector2 &r_v, const Transform2D &p_tr) const;
+ void _software_transform_vertex(Vector2 &r_v, const Transform2D &p_tr) const;
+ TransformMode _find_transform_mode(const Transform2D &p_tr) const {
+ // decided whether to do translate only for software transform
+ if ((p_tr.elements[0].x == 1.0f) &&
+ (p_tr.elements[0].y == 0.0f) &&
+ (p_tr.elements[1].x == 0.0f) &&
+ (p_tr.elements[1].y == 1.0f)) {
+ return TM_TRANSLATE;
+ }
+
+ return TM_ALL;
+ }
+ bool _software_skin_poly(RasterizerCanvas::Item::CommandPolygon *p_poly, RasterizerCanvas::Item *p_item, BatchVertex *bvs, BatchColor *vertex_colors, const FillState &p_fill_state, const BatchColor *p_precalced_colors);
+ typename T_STORAGE::Texture *_get_canvas_texture(const RID &p_texture) const {
+ if (p_texture.is_valid()) {
+
+ typename T_STORAGE::Texture *texture = get_storage()->texture_owner.getornull(p_texture);
+
+ if (texture) {
+ return texture->get_ptr();
+ }
+ }
+
+ return 0;
+ }
+
+public:
+ Batch *_batch_request_new(bool p_blank = true) {
+ Batch *batch = bdata.batches.request();
+ if (!batch) {
+ // grow the batches
+ bdata.batches.grow();
+
+ // and the temporary batches (used for color verts)
+ bdata.batches_temp.reset();
+ bdata.batches_temp.grow();
+
+ // this should always succeed after growing
+ batch = bdata.batches.request();
+#if defined(TOOLS_ENABLED) && defined(DEBUG_ENABLED)
+ CRASH_COND(!batch);
+#endif
+ }
+
+ if (p_blank)
+ memset(batch, 0, sizeof(Batch));
+
+ return batch;
+ }
+
+ BatchVertex *_batch_vertex_request_new() {
+ return bdata.vertices.request();
+ }
+
+protected:
+ // no need to compile these in in release, they are unneeded outside the editor and only add to executable size
+#if defined(TOOLS_ENABLED) && defined(DEBUG_ENABLED)
+#include "batch_diagnose.inc"
+#endif
+};
+
+PREAMBLE(void)::batch_canvas_begin() {
+ // diagnose_frame?
+ bdata.frame_string = ""; // just in case, always set this as we don't want a string leak in release...
+#if defined(TOOLS_ENABLED) && defined(DEBUG_ENABLED)
+ if (bdata.settings_diagnose_frame) {
+ bdata.diagnose_frame = false;
+
+ uint32_t tick = OS::get_singleton()->get_ticks_msec();
+ uint64_t frame = Engine::get_singleton()->get_frames_drawn();
+
+ if (tick >= bdata.next_diagnose_tick) {
+ bdata.next_diagnose_tick = tick + 10000;
+
+ // the plus one is prevent starting diagnosis half way through frame
+ bdata.diagnose_frame_number = frame + 1;
+ }
+
+ if (frame == bdata.diagnose_frame_number) {
+ bdata.diagnose_frame = true;
+ bdata.reset_stats();
+ }
+
+ if (bdata.diagnose_frame) {
+ bdata.frame_string = "canvas_begin FRAME " + itos(frame) + "\n";
+ }
+ }
+#endif
+}
+
+PREAMBLE(void)::batch_canvas_end() {
+#if defined(TOOLS_ENABLED) && defined(DEBUG_ENABLED)
+ if (bdata.diagnose_frame) {
+ bdata.frame_string += "canvas_end\n";
+ if (bdata.stats_items_sorted) {
+ bdata.frame_string += "\titems reordered: " + itos(bdata.stats_items_sorted) + "\n";
+ }
+ if (bdata.stats_light_items_joined) {
+ bdata.frame_string += "\tlight items joined: " + itos(bdata.stats_light_items_joined) + "\n";
+ }
+
+ print_line(bdata.frame_string);
+ }
+#endif
+}
+
+PREAMBLE(void)::batch_canvas_render_items_begin(const Color &p_modulate, RasterizerCanvas::Light *p_light, const Transform2D &p_base_transform) {
+ // if we are debugging, flash each frame between batching renderer and old version to compare for regressions
+ if (bdata.settings_flash_batching) {
+ if ((Engine::get_singleton()->get_frames_drawn() % 2) == 0)
+ bdata.settings_use_batching = true;
+ else
+ bdata.settings_use_batching = false;
+ }
+
+ if (!bdata.settings_use_batching) {
+ return;
+ }
+
+ // this only needs to be done when screen size changes, but this should be
+ // infrequent enough
+ _calculate_scissor_threshold_area();
+
+ // set up render item state for all the z_indexes (this is common to all z_indexes)
+ _render_item_state.reset();
+ _render_item_state.item_group_modulate = p_modulate;
+ _render_item_state.item_group_light = p_light;
+ _render_item_state.item_group_base_transform = p_base_transform;
+ _render_item_state.light_region.reset();
+
+ // batch break must be preserved over the different z indices,
+ // to prevent joining to an item on a previous index if not allowed
+ _render_item_state.join_batch_break = false;
+
+ // whether to join across z indices depends on whether there are z ranged lights.
+ // joined z_index items can be wrongly classified with z ranged lights.
+ bdata.join_across_z_indices = true;
+
+ int light_count = 0;
+ while (p_light) {
+ light_count++;
+
+ if ((p_light->z_min != VS::CANVAS_ITEM_Z_MIN) || (p_light->z_max != VS::CANVAS_ITEM_Z_MAX)) {
+ // prevent joining across z indices. This would have caused visual regressions
+ bdata.join_across_z_indices = false;
+ }
+
+ p_light = p_light->next_ptr;
+ }
+
+ // can't use the light region bitfield if there are too many lights
+ // hopefully most games won't blow this limit..
+ // if they do they will work but it won't batch join items just in case
+ if (light_count > 64) {
+ _render_item_state.light_region.too_many_lights = true;
+ }
+}
+
+PREAMBLE(void)::batch_canvas_render_items_end() {
+ if (!bdata.settings_use_batching) {
+ return;
+ }
+
+ join_sorted_items();
+
+#if defined(TOOLS_ENABLED) && defined(DEBUG_ENABLED)
+ if (bdata.diagnose_frame) {
+ bdata.frame_string += "items\n";
+ }
+#endif
+
+ // batching render is deferred until after going through all the z_indices, joining all the items
+ get_this()->canvas_render_items_implementation(0, 0, _render_item_state.item_group_modulate,
+ _render_item_state.item_group_light,
+ _render_item_state.item_group_base_transform);
+
+ bdata.items_joined.reset();
+ bdata.item_refs.reset();
+ bdata.sort_items.reset();
+}
+
+PREAMBLE(void)::batch_canvas_render_items(RasterizerCanvas::Item *p_item_list, int p_z, const Color &p_modulate, RasterizerCanvas::Light *p_light, const Transform2D &p_base_transform) {
+ // stage 1 : join similar items, so that their state changes are not repeated,
+ // and commands from joined items can be batched together
+ if (bdata.settings_use_batching) {
+ record_items(p_item_list, p_z);
+ return;
+ }
+
+ // only legacy renders at this stage, batched renderer doesn't render until canvas_render_items_end()
+ get_this()->canvas_render_items_implementation(p_item_list, p_z, p_modulate, p_light, p_base_transform);
+}
+
+// Default batches will not occur in software transform only items
+// EXCEPT IN THE CASE OF SINGLE RECTS (and this may well not occur, check the logic in prefill_join_item TYPE_RECT)
+// but can occur where transform commands have been sent during hardware batch
+PREAMBLE(void)::_prefill_default_batch(FillState &r_fill_state, int p_command_num, const RasterizerCanvas::Item &p_item) {
+ if (r_fill_state.curr_batch->type == RasterizerStorageCommon::BT_DEFAULT) {
+ // don't need to flush an extra transform command?
+ if (!r_fill_state.transform_extra_command_number_p1) {
+ // another default command, just add to the existing batch
+ r_fill_state.curr_batch->num_commands++;
+ } else {
+#if defined(TOOLS_ENABLED) && defined(DEBUG_ENABLED)
+ if (r_fill_state.transform_extra_command_number_p1 != p_command_num) {
+ WARN_PRINT_ONCE("_prefill_default_batch : transform_extra_command_number_p1 != p_command_num");
+ }
+#endif
+ // if the first member of the batch is a transform we have to be careful
+ if (!r_fill_state.curr_batch->num_commands) {
+ // there can be leading useless extra transforms (sometimes happens with debug collision polys)
+ // we need to rejig the first_command for the first useful transform
+ r_fill_state.curr_batch->first_command += r_fill_state.transform_extra_command_number_p1 - 1;
+ }
+
+ // we do have a pending extra transform command to flush
+ // either the extra transform is in the prior command, or not, in which case we need 2 batches
+ r_fill_state.curr_batch->num_commands += 2;
+
+ r_fill_state.transform_extra_command_number_p1 = 0; // mark as sent
+ r_fill_state.extra_matrix_sent = true;
+
+ // the original mode should always be hardware transform ..
+ // test this assumption
+ //CRASH_COND(r_fill_state.orig_transform_mode != TM_NONE);
+ r_fill_state.transform_mode = r_fill_state.orig_transform_mode;
+
+ // do we need to restore anything else?
+ }
+ } else {
+ // end of previous different type batch, so start new default batch
+
+ // first consider whether there is a dirty extra matrix to send
+ if (r_fill_state.transform_extra_command_number_p1) {
+ // get which command the extra is in, and blank all the records as it no longer is stored CPU side
+ int extra_command = r_fill_state.transform_extra_command_number_p1 - 1; // plus 1 based
+ r_fill_state.transform_extra_command_number_p1 = 0;
+ r_fill_state.extra_matrix_sent = true;
+
+ // send the extra to the GPU in a batch
+ r_fill_state.curr_batch = _batch_request_new();
+ r_fill_state.curr_batch->type = RasterizerStorageCommon::BT_DEFAULT;
+ r_fill_state.curr_batch->first_command = extra_command;
+ r_fill_state.curr_batch->num_commands = 1;
+
+ // revert to the original transform mode
+ // e.g. go back to NONE if we were in hardware transform mode
+ r_fill_state.transform_mode = r_fill_state.orig_transform_mode;
+
+ // reset the original transform if we are going back to software mode,
+ // because the extra is now done on the GPU...
+ // (any subsequent extras are sent directly to the GPU, no deferring)
+ if (r_fill_state.orig_transform_mode != TM_NONE) {
+ r_fill_state.transform_combined = p_item.final_transform;
+ }
+
+ // can possibly combine batch with the next one in some cases
+ // this is more efficient than having an extra batch especially for the extra
+ if ((extra_command + 1) == p_command_num) {
+ r_fill_state.curr_batch->num_commands = 2;
+ return;
+ }
+ }
+
+ // start default batch
+ r_fill_state.curr_batch = _batch_request_new();
+ r_fill_state.curr_batch->type = RasterizerStorageCommon::BT_DEFAULT;
+ r_fill_state.curr_batch->first_command = p_command_num;
+ r_fill_state.curr_batch->num_commands = 1;
+ }
+}
+
+PREAMBLE(int)::_batch_find_or_create_tex(const RID &p_texture, const RID &p_normal, bool p_tile, int p_previous_match) {
+
+ // optimization .. in 99% cases the last matched value will be the same, so no need to traverse the list
+ if (p_previous_match > 0) // if it is zero, it will get hit first in the linear search anyway
+ {
+ const BatchTex &batch_texture = bdata.batch_textures[p_previous_match];
+
+ // note for future reference, if RID implementation changes, this could become more expensive
+ if ((batch_texture.RID_texture == p_texture) && (batch_texture.RID_normal == p_normal)) {
+ // tiling mode must also match
+ bool tiles = batch_texture.tile_mode != BatchTex::TILE_OFF;
+
+ if (tiles == p_tile)
+ // match!
+ return p_previous_match;
+ }
+ }
+
+ // not the previous match .. we will do a linear search ... slower, but should happen
+ // not very often except with non-batchable runs, which are going to be slow anyway
+ // n.b. could possibly be replaced later by a fast hash table
+ for (int n = 0; n < bdata.batch_textures.size(); n++) {
+ const BatchTex &batch_texture = bdata.batch_textures[n];
+ if ((batch_texture.RID_texture == p_texture) && (batch_texture.RID_normal == p_normal)) {
+
+ // tiling mode must also match
+ bool tiles = batch_texture.tile_mode != BatchTex::TILE_OFF;
+
+ if (tiles == p_tile)
+ // match!
+ return n;
+ }
+ }
+
+ // pushing back from local variable .. not ideal but has to use a Vector because non pod
+ // due to RIDs
+ BatchTex new_batch_tex;
+ new_batch_tex.RID_texture = p_texture;
+ new_batch_tex.RID_normal = p_normal;
+
+ // get the texture
+ typename T_STORAGE::Texture *texture = _get_canvas_texture(p_texture);
+
+ if (texture) {
+ new_batch_tex.tex_pixel_size.x = 1.0 / texture->width;
+ new_batch_tex.tex_pixel_size.y = 1.0 / texture->height;
+ new_batch_tex.flags = texture->flags;
+ } else {
+ // maybe doesn't need doing...
+ new_batch_tex.tex_pixel_size.x = 1.0;
+ new_batch_tex.tex_pixel_size.y = 1.0;
+ new_batch_tex.flags = 0;
+ }
+
+ if (p_tile) {
+ if (texture) {
+ // default
+ new_batch_tex.tile_mode = BatchTex::TILE_NORMAL;
+
+ // no hardware support for non power of 2 tiling
+ if (!get_storage()->config.support_npot_repeat_mipmap) {
+ if (next_power_of_2(texture->alloc_width) != (unsigned int)texture->alloc_width && next_power_of_2(texture->alloc_height) != (unsigned int)texture->alloc_height) {
+ new_batch_tex.tile_mode = BatchTex::TILE_FORCE_REPEAT;
+ }
+ }
+ } else {
+ // this should not happen?
+ new_batch_tex.tile_mode = BatchTex::TILE_OFF;
+ }
+ } else {
+ new_batch_tex.tile_mode = BatchTex::TILE_OFF;
+ }
+
+ // push back
+ bdata.batch_textures.push_back(new_batch_tex);
+
+ return bdata.batch_textures.size() - 1;
+}
+
+PREAMBLE(void)::batch_constructor() {
+ bdata.settings_use_batching = false;
+
+#ifdef GLES_OVER_GL
+ use_nvidia_rect_workaround = GLOBAL_GET("rendering/quality/2d/use_nvidia_rect_flicker_workaround");
+#else
+ // Not needed (a priori) on GLES devices
+ use_nvidia_rect_workaround = false;
+#endif
+}
+
+PREAMBLE(void)::batch_initialize() {
+ bdata.settings_use_batching = GLOBAL_GET("rendering/batching/options/use_batching");
+ bdata.settings_max_join_item_commands = GLOBAL_GET("rendering/batching/parameters/max_join_item_commands");
+ bdata.settings_colored_vertex_format_threshold = GLOBAL_GET("rendering/batching/parameters/colored_vertex_format_threshold");
+ bdata.settings_item_reordering_lookahead = GLOBAL_GET("rendering/batching/parameters/item_reordering_lookahead");
+ bdata.settings_light_max_join_items = GLOBAL_GET("rendering/batching/lights/max_join_items");
+ bdata.settings_use_single_rect_fallback = GLOBAL_GET("rendering/batching/options/single_rect_fallback");
+ bdata.settings_use_software_skinning = GLOBAL_GET("rendering/quality/2d/use_software_skinning");
+ bdata.settings_ninepatch_mode = GLOBAL_GET("rendering/quality/2d/ninepatch_mode");
+
+ // alternatively only enable uv contract if pixel snap in use,
+ // but with this enable bool, it should not be necessary
+ bdata.settings_uv_contract = GLOBAL_GET("rendering/batching/precision/uv_contract");
+ bdata.settings_uv_contract_amount = (float)GLOBAL_GET("rendering/batching/precision/uv_contract_amount") / 1000000.0f;
+
+ // we can use the threshold to determine whether to turn scissoring off or on
+ bdata.settings_scissor_threshold = GLOBAL_GET("rendering/batching/lights/scissor_area_threshold");
+ if (bdata.settings_scissor_threshold > 0.999f) {
+ bdata.settings_scissor_lights = false;
+ } else {
+ bdata.settings_scissor_lights = true;
+
+ // apply power of 4 relationship for the area, as most of the important changes
+ // will be happening at low values of scissor threshold
+ bdata.settings_scissor_threshold *= bdata.settings_scissor_threshold;
+ bdata.settings_scissor_threshold *= bdata.settings_scissor_threshold;
+ }
+
+ // The sweet spot on my desktop for cache is actually smaller than the max, and this
+ // is the default. This saves memory too so we will use it for now, needs testing to see whether this varies according
+ // to device / platform.
+ bdata.settings_batch_buffer_num_verts = GLOBAL_GET("rendering/batching/parameters/batch_buffer_size");
+
+ // override the use_batching setting in the editor
+ // (note that if the editor can't start, you can't change the use_batching project setting!)
+ if (Engine::get_singleton()->is_editor_hint()) {
+ bool use_in_editor = GLOBAL_GET("rendering/batching/options/use_batching_in_editor");
+ bdata.settings_use_batching = use_in_editor;
+
+ // fix some settings in the editor, as the performance not worth the risk
+ bdata.settings_use_single_rect_fallback = false;
+ }
+
+ // if we are using batching, we will purposefully disable the nvidia workaround.
+ // This is because the only reason to use the single rect fallback is the approx 2x speed
+ // of the uniform drawing technique. If we used nvidia workaround, speed would be
+ // approx equal to the batcher drawing technique (indexed primitive + VB).
+ if (bdata.settings_use_batching) {
+ use_nvidia_rect_workaround = false;
+ }
+
+ // For debugging, if flash is set in project settings, it will flash on alternate frames
+ // between the non-batched renderer and the batched renderer,
+ // in order to find regressions.
+ // This should not be used except during development.
+ // make a note of the original choice in case we are flashing on and off the batching
+ bdata.settings_use_batching_original_choice = bdata.settings_use_batching;
+ bdata.settings_flash_batching = GLOBAL_GET("rendering/batching/debug/flash_batching");
+ if (!bdata.settings_use_batching) {
+ // no flash when batching turned off
+ bdata.settings_flash_batching = false;
+ }
+
+ // frame diagnosis. print out the batches every nth frame
+ bdata.settings_diagnose_frame = false;
+ if (!Engine::get_singleton()->is_editor_hint() && bdata.settings_use_batching) {
+ // {
+ bdata.settings_diagnose_frame = GLOBAL_GET("rendering/batching/debug/diagnose_frame");
+ }
+
+ // the maximum num quads in a batch is limited by GLES2. We can have only 16 bit indices,
+ // which means we can address a vertex buffer of max size 65535. 4 vertices are needed per quad.
+
+ // Note this determines the memory use by the vertex buffer vector. max quads (65536/4)-1
+ // but can be reduced to save memory if really required (will result in more batches though)
+ const int max_possible_quads = (65536 / 4) - 1;
+ const int min_possible_quads = 8; // some reasonable small value
+
+ // value from project settings
+ int max_quads = bdata.settings_batch_buffer_num_verts / 4;
+
+ // sanity checks
+ max_quads = CLAMP(max_quads, min_possible_quads, max_possible_quads);
+ bdata.settings_max_join_item_commands = CLAMP(bdata.settings_max_join_item_commands, 0, 65535);
+ bdata.settings_colored_vertex_format_threshold = CLAMP(bdata.settings_colored_vertex_format_threshold, 0.0f, 1.0f);
+ bdata.settings_scissor_threshold = CLAMP(bdata.settings_scissor_threshold, 0.0f, 1.0f);
+ bdata.settings_light_max_join_items = CLAMP(bdata.settings_light_max_join_items, 0, 65535);
+ bdata.settings_item_reordering_lookahead = CLAMP(bdata.settings_item_reordering_lookahead, 0, 65535);
+
+ // for debug purposes, output a string with the batching options
+ String batching_options_string = "OpenGL ES Batching: ";
+ if (bdata.settings_use_batching) {
+ batching_options_string += "ON";
+
+ if (OS::get_singleton()->is_stdout_verbose()) {
+ batching_options_string += "\n\tOPTIONS\n";
+ batching_options_string += "\tmax_join_item_commands " + itos(bdata.settings_max_join_item_commands) + "\n";
+ batching_options_string += "\tcolored_vertex_format_threshold " + String(Variant(bdata.settings_colored_vertex_format_threshold)) + "\n";
+ batching_options_string += "\tbatch_buffer_size " + itos(bdata.settings_batch_buffer_num_verts) + "\n";
+ batching_options_string += "\tlight_scissor_area_threshold " + String(Variant(bdata.settings_scissor_threshold)) + "\n";
+
+ batching_options_string += "\titem_reordering_lookahead " + itos(bdata.settings_item_reordering_lookahead) + "\n";
+ batching_options_string += "\tlight_max_join_items " + itos(bdata.settings_light_max_join_items) + "\n";
+ batching_options_string += "\tsingle_rect_fallback " + String(Variant(bdata.settings_use_single_rect_fallback)) + "\n";
+
+ batching_options_string += "\tdebug_flash " + String(Variant(bdata.settings_flash_batching)) + "\n";
+ batching_options_string += "\tdiagnose_frame " + String(Variant(bdata.settings_diagnose_frame));
+ }
+
+ print_line(batching_options_string);
+ }
+
+ // special case, for colored vertex format threshold.
+ // as the comparison is >=, we want to be able to totally turn on or off
+ // conversion to colored vertex format at the extremes, so we will force
+ // 1.0 to be just above 1.0
+ if (bdata.settings_colored_vertex_format_threshold > 0.995f) {
+ bdata.settings_colored_vertex_format_threshold = 1.01f;
+ }
+
+ // save memory when batching off
+ if (!bdata.settings_use_batching) {
+ max_quads = 0;
+ }
+
+ uint32_t sizeof_batch_vert = sizeof(BatchVertex);
+
+ bdata.max_quads = max_quads;
+
+ // 4 verts per quad
+ bdata.vertex_buffer_size_units = max_quads * 4;
+
+ // the index buffer can be longer than 65535, only the indices need to be within this range
+ bdata.index_buffer_size_units = max_quads * 6;
+
+ const int max_verts = bdata.vertex_buffer_size_units;
+
+ // this comes out at approx 64K for non-colored vertex buffer, and 128K for colored vertex buffer
+ bdata.vertex_buffer_size_bytes = max_verts * sizeof_batch_vert;
+ bdata.index_buffer_size_bytes = bdata.index_buffer_size_units * 2; // 16 bit inds
+
+ // create equal number of normal and (max) unit sized verts (as the normal may need to be translated to a larger FVF)
+ bdata.vertices.create(max_verts); // 512k
+ bdata.unit_vertices.create(max_verts, sizeof(BatchVertexLarge));
+
+ // extra data per vert needed for larger FVFs
+ bdata.light_angles.create(max_verts);
+ bdata.vertex_colors.create(max_verts);
+ bdata.vertex_modulates.create(max_verts);
+ bdata.vertex_transforms.create(max_verts);
+
+ // num batches will be auto increased dynamically if required
+ bdata.batches.create(1024);
+ bdata.batches_temp.create(bdata.batches.max_size());
+
+ // batch textures can also be increased dynamically
+ bdata.batch_textures.create(32);
+}
+
+PREAMBLE(bool)::_light_scissor_begin(const Rect2 &p_item_rect, const Transform2D &p_light_xform, const Rect2 &p_light_rect) const {
+
+ float area_item = p_item_rect.size.x * p_item_rect.size.y; // double check these are always positive
+
+ // quick reject .. the area of pixels saved can never be more than the area of the item
+ if (area_item < bdata.scissor_threshold_area) {
+ return false;
+ }
+
+ Rect2 cliprect;
+ if (!_light_find_intersection(p_item_rect, p_light_xform, p_light_rect, cliprect)) {
+ // should not really occur .. but just in case
+ cliprect = Rect2(0, 0, 0, 0);
+ } else {
+ // some conditions not to scissor
+ // determine the area (fill rate) that will be saved
+ float area_cliprect = cliprect.size.x * cliprect.size.y;
+ float area_saved = area_item - area_cliprect;
+
+ // if area saved is too small, don't scissor
+ if (area_saved < bdata.scissor_threshold_area) {
+ return false;
+ }
+ }
+
+ int rh = get_storage()->frame.current_rt->height;
+
+ int y = rh - (cliprect.position.y + cliprect.size.y);
+ if (get_storage()->frame.current_rt->flags[RasterizerStorage::RENDER_TARGET_VFLIP])
+ y = cliprect.position.y;
+ get_this()->gl_enable_scissor(cliprect.position.x, y, cliprect.size.width, cliprect.size.height);
+
+ return true;
+}
+
+PREAMBLE(bool)::_light_find_intersection(const Rect2 &p_item_rect, const Transform2D &p_light_xform, const Rect2 &p_light_rect, Rect2 &r_cliprect) const {
+ // transform light to world space (note this is done in the earlier intersection test, so could
+ // be made more efficient)
+ Vector2 pts[4] = {
+ p_light_xform.xform(p_light_rect.position),
+ p_light_xform.xform(Vector2(p_light_rect.position.x + p_light_rect.size.x, p_light_rect.position.y)),
+ p_light_xform.xform(Vector2(p_light_rect.position.x, p_light_rect.position.y + p_light_rect.size.y)),
+ p_light_xform.xform(Vector2(p_light_rect.position.x + p_light_rect.size.x, p_light_rect.position.y + p_light_rect.size.y)),
+ };
+
+ // calculate the light bound rect in world space
+ Rect2 lrect(pts[0].x, pts[0].y, 0, 0);
+ for (int n = 1; n < 4; n++) {
+ lrect.expand_to(pts[n]);
+ }
+
+ // intersection between the 2 rects
+ // they should probably always intersect, because of earlier check, but just in case...
+ if (!p_item_rect.intersects(lrect))
+ return false;
+
+ // note this does almost the same as Rect2.clip but slightly more efficient for our use case
+ r_cliprect.position.x = MAX(p_item_rect.position.x, lrect.position.x);
+ r_cliprect.position.y = MAX(p_item_rect.position.y, lrect.position.y);
+
+ Point2 item_rect_end = p_item_rect.position + p_item_rect.size;
+ Point2 lrect_end = lrect.position + lrect.size;
+
+ r_cliprect.size.x = MIN(item_rect_end.x, lrect_end.x) - r_cliprect.position.x;
+ r_cliprect.size.y = MIN(item_rect_end.y, lrect_end.y) - r_cliprect.position.y;
+
+ return true;
+}
+
+PREAMBLE(void)::_calculate_scissor_threshold_area() {
+ if (!bdata.settings_scissor_lights) {
+ return;
+ }
+
+ // scissor area threshold is 0.0 to 1.0 in the settings for ease of use.
+ // we need to translate to an absolute area to determine quickly whether
+ // to scissor.
+ if (bdata.settings_scissor_threshold < 0.0001f) {
+ bdata.scissor_threshold_area = -1.0f; // will always pass
+ } else {
+ // in pixels
+ int w = get_storage()->frame.current_rt->width;
+ int h = get_storage()->frame.current_rt->height;
+
+ int screen_area = w * h;
+
+ bdata.scissor_threshold_area = bdata.settings_scissor_threshold * screen_area;
+ }
+}
+
+PREAMBLE(bool)::_prefill_line(RasterizerCanvas::Item::CommandLine *p_line, FillState &r_fill_state, int &r_command_start, int command_num, int command_count, RasterizerCanvas::Item *p_item, bool multiply_final_modulate) {
+ bool change_batch = false;
+
+ // we have separate batch types for non and anti aliased lines.
+ // You can't batch the different types together.
+ RasterizerStorageCommon::BatchType line_batch_type = RasterizerStorageCommon::BT_LINE;
+ uint32_t line_batch_flags = RasterizerStorageCommon::BTF_LINE;
+#ifdef GLES_OVER_GL
+ if (p_line->antialiased) {
+ line_batch_type = RasterizerStorageCommon::BT_LINE_AA;
+ line_batch_flags = RasterizerStorageCommon::BTF_LINE_AA;
+ }
+#endif
+
+ // conditions for creating a new batch
+ if (r_fill_state.curr_batch->type != line_batch_type) {
+ if (r_fill_state.sequence_batch_type_flags & (~line_batch_flags)) {
+ // don't allow joining to a different sequence type
+ r_command_start = command_num;
+ return true;
+ }
+ r_fill_state.sequence_batch_type_flags |= line_batch_flags;
+
+ change_batch = true;
+ }
+
+ // get the baked line color
+ Color col = p_line->color;
+
+ if (multiply_final_modulate)
+ col *= r_fill_state.final_modulate;
+
+ BatchColor bcol;
+ bcol.set(col);
+
+ // if the color has changed we need a new batch
+ // (only single color line batches supported so far)
+ if (r_fill_state.curr_batch->color != bcol)
+ change_batch = true;
+
+ // not sure if needed
+ r_fill_state.batch_tex_id = -1;
+
+ // try to create vertices BEFORE creating a batch,
+ // because if the vertex buffer is full, we need to finish this
+ // function, draw what we have so far, and then start a new set of batches
+
+ // request multiple vertices at a time, this is more efficient
+ BatchVertex *bvs = bdata.vertices.request(2);
+ if (!bvs) {
+ // run out of space in the vertex buffer .. finish this function and draw what we have so far
+ // return where we got to
+ r_command_start = command_num;
+ return true;
+ }
+
+ if (change_batch) {
+
+ // open new batch (this should never fail, it dynamically grows)
+ r_fill_state.curr_batch = _batch_request_new(false);
+
+ r_fill_state.curr_batch->type = line_batch_type;
+ r_fill_state.curr_batch->color = bcol;
+ r_fill_state.curr_batch->batch_texture_id = -1;
+ r_fill_state.curr_batch->first_command = command_num;
+ r_fill_state.curr_batch->num_commands = 1;
+ //r_fill_state.curr_batch->first_quad = bdata.total_quads;
+ r_fill_state.curr_batch->first_vert = bdata.total_verts;
+ } else {
+ // we could alternatively do the count when closing a batch .. perhaps more efficient
+ r_fill_state.curr_batch->num_commands++;
+ }
+
+ // fill the geometry
+ Vector2 from = p_line->from;
+ Vector2 to = p_line->to;
+
+ if (r_fill_state.transform_mode != TM_NONE) {
+ _software_transform_vertex(from, r_fill_state.transform_combined);
+ _software_transform_vertex(to, r_fill_state.transform_combined);
+ }
+
+ bvs[0].pos.set(from);
+ bvs[0].uv.set(0, 0); // may not be necessary
+ bvs[1].pos.set(to);
+ bvs[1].uv.set(0, 0);
+
+ bdata.total_verts += 2;
+
+ return false;
+}
+
+//unsigned int _ninepatch_apply_tiling_modes(RasterizerCanvas::Item::CommandNinePatch *p_np, Rect2 &r_source) {
+// unsigned int rect_flags = 0;
+
+// switch (p_np->axis_x) {
+// default:
+// break;
+// case VisualServer::NINE_PATCH_TILE: {
+// r_source.size.x = p_np->rect.size.x;
+// rect_flags = RasterizerCanvas::CANVAS_RECT_TILE;
+// } break;
+// case VisualServer::NINE_PATCH_TILE_FIT: {
+// // prevent divide by zero (may never happen)
+// if (r_source.size.x) {
+// int units = p_np->rect.size.x / r_source.size.x;
+// if (!units)
+// units++;
+// r_source.size.x = r_source.size.x * units;
+// rect_flags = RasterizerCanvas::CANVAS_RECT_TILE;
+// }
+// } break;
+// }
+
+// switch (p_np->axis_y) {
+// default:
+// break;
+// case VisualServer::NINE_PATCH_TILE: {
+// r_source.size.y = p_np->rect.size.y;
+// rect_flags = RasterizerCanvas::CANVAS_RECT_TILE;
+// } break;
+// case VisualServer::NINE_PATCH_TILE_FIT: {
+// // prevent divide by zero (may never happen)
+// if (r_source.size.y) {
+// int units = p_np->rect.size.y / r_source.size.y;
+// if (!units)
+// units++;
+// r_source.size.y = r_source.size.y * units;
+// rect_flags = RasterizerCanvas::CANVAS_RECT_TILE;
+// }
+// } break;
+// }
+
+// return rect_flags;
+//}
+
+T_PREAMBLE
+template
+bool C_PREAMBLE::_prefill_ninepatch(RasterizerCanvas::Item::CommandNinePatch *p_np, FillState &r_fill_state, int &r_command_start, int command_num, int command_count, RasterizerCanvas::Item *p_item, bool multiply_final_modulate) {
+ typename T_STORAGE::Texture *tex = get_storage()->texture_owner.getornull(p_np->texture);
+
+ if (!tex) {
+ // FIXME: Handle textureless ninepatch gracefully
+ WARN_PRINT("NinePatch without texture not supported yet in GLES2 backend, skipping.");
+ return false;
+ }
+ if (tex->width == 0 || tex->height == 0) {
+ WARN_PRINT("Cannot set empty texture to NinePatch.");
+ return false;
+ }
+
+ // first check there are enough verts for this to complete successfully
+ if (bdata.vertices.size() + (4 * 9) > bdata.vertices.max_size()) {
+ // return where we got to
+ r_command_start = command_num;
+ return true;
+ }
+
+ // create a temporary rect so we can reuse the rect routine
+ RasterizerCanvas::Item::CommandRect trect;
+
+ trect.texture = p_np->texture;
+ trect.normal_map = p_np->normal_map;
+ trect.modulate = p_np->color;
+ trect.flags = RasterizerCanvas::CANVAS_RECT_REGION;
+
+ //Size2 texpixel_size(1.0f / tex->width, 1.0f / tex->height);
+
+ Rect2 source = p_np->source;
+ if (source.size.x == 0 && source.size.y == 0) {
+ source.size.x = tex->width;
+ source.size.y = tex->height;
+ }
+
+ float screen_scale = 1.0f;
+
+ // optional crazy ninepatch scaling mode
+ if ((bdata.settings_ninepatch_mode == 1) && (source.size.x != 0) && (source.size.y != 0)) {
+ screen_scale = MIN(p_np->rect.size.x / source.size.x, p_np->rect.size.y / source.size.y);
+ screen_scale = MIN(1.0, screen_scale);
+ }
+
+ // deal with nine patch texture wrapping modes
+ // this is switched off because it may not be possible with batching
+ // trect.flags |= _ninepatch_apply_tiling_modes(p_np, source);
+
+ // translate to rects
+ Rect2 &rt = trect.rect;
+ Rect2 &src = trect.source;
+
+ float tex_margin_left = p_np->margin[MARGIN_LEFT];
+ float tex_margin_right = p_np->margin[MARGIN_RIGHT];
+ float tex_margin_top = p_np->margin[MARGIN_TOP];
+ float tex_margin_bottom = p_np->margin[MARGIN_BOTTOM];
+
+ float x[4];
+ x[0] = p_np->rect.position.x;
+ x[1] = x[0] + (p_np->margin[MARGIN_LEFT] * screen_scale);
+ x[3] = x[0] + (p_np->rect.size.x);
+ x[2] = x[3] - (p_np->margin[MARGIN_RIGHT] * screen_scale);
+
+ float y[4];
+ y[0] = p_np->rect.position.y;
+ y[1] = y[0] + (p_np->margin[MARGIN_TOP] * screen_scale);
+ y[3] = y[0] + (p_np->rect.size.y);
+ y[2] = y[3] - (p_np->margin[MARGIN_BOTTOM] * screen_scale);
+
+ float u[4];
+ u[0] = source.position.x;
+ u[1] = u[0] + tex_margin_left;
+ u[3] = u[0] + source.size.x;
+ u[2] = u[3] - tex_margin_right;
+
+ float v[4];
+ v[0] = source.position.y;
+ v[1] = v[0] + tex_margin_top;
+ v[3] = v[0] + source.size.y;
+ v[2] = v[3] - tex_margin_bottom;
+
+ // temporarily override to prevent single rect fallback
+ bool single_rect_fallback = bdata.settings_use_single_rect_fallback;
+ bdata.settings_use_single_rect_fallback = false;
+
+ // each line of the ninepatch
+ for (int line = 0; line < 3; line++) {
+ rt.position = Vector2(x[0], y[line]);
+ rt.size = Vector2(x[1] - x[0], y[line + 1] - y[line]);
+ src.position = Vector2(u[0], v[line]);
+ src.size = Vector2(u[1] - u[0], v[line + 1] - v[line]);
+ _prefill_rect(&trect, r_fill_state, r_command_start, command_num, command_count, nullptr, p_item, multiply_final_modulate);
+
+ if ((line == 1) && (!p_np->draw_center))
+ ;
+ else {
+ rt.position.x = x[1];
+ rt.size.x = x[2] - x[1];
+ src.position.x = u[1];
+ src.size.x = u[2] - u[1];
+ _prefill_rect(&trect, r_fill_state, r_command_start, command_num, command_count, nullptr, p_item, multiply_final_modulate);
+ }
+
+ rt.position.x = x[2];
+ rt.size.x = x[3] - x[2];
+ src.position.x = u[2];
+ src.size.x = u[3] - u[2];
+ _prefill_rect(&trect, r_fill_state, r_command_start, command_num, command_count, nullptr, p_item, multiply_final_modulate);
+ }
+
+ // restore single rect fallback
+ bdata.settings_use_single_rect_fallback = single_rect_fallback;
+ return false;
+}
+
+T_PREAMBLE
+template
+bool C_PREAMBLE::_prefill_polygon(RasterizerCanvas::Item::CommandPolygon *p_poly, FillState &r_fill_state, int &r_command_start, int command_num, int command_count, RasterizerCanvas::Item *p_item, bool multiply_final_modulate) {
+ bool change_batch = false;
+
+ // conditions for creating a new batch
+ if (r_fill_state.curr_batch->type != RasterizerStorageCommon::BT_POLY) {
+
+ // don't allow joining to a different sequence type
+ if (r_fill_state.sequence_batch_type_flags & (~RasterizerStorageCommon::BTF_POLY)) {
+ // don't allow joining to a different sequence type
+ r_command_start = command_num;
+ return true;
+ }
+ r_fill_state.sequence_batch_type_flags |= RasterizerStorageCommon::BTF_POLY;
+
+ change_batch = true;
+ }
+
+ int num_inds = p_poly->indices.size();
+
+ // nothing to draw?
+ if (!num_inds)
+ return false;
+
+ // we aren't using indices, so will transform verts more than once .. less efficient.
+ // could be done with a temporary vertex buffer
+ BatchVertex *bvs = bdata.vertices.request(num_inds);
+ if (!bvs) {
+ // run out of space in the vertex buffer .. finish this function and draw what we have so far
+ // return where we got to
+ r_command_start = command_num;
+ return true;
+ }
+
+ BatchColor *vertex_colors = bdata.vertex_colors.request(num_inds);
+#if defined(TOOLS_ENABLED) && defined(DEBUG_ENABLED)
+ CRASH_COND(!vertex_colors);
+#endif
+
+ // the modulate is always baked
+ Color modulate;
+ if (multiply_final_modulate)
+ modulate = r_fill_state.final_modulate;
+ else
+ modulate = Color(1, 1, 1, 1);
+
+ int old_batch_tex_id = r_fill_state.batch_tex_id;
+ r_fill_state.batch_tex_id = _batch_find_or_create_tex(p_poly->texture, p_poly->normal_map, false, old_batch_tex_id);
+
+ // conditions for creating a new batch
+ if (old_batch_tex_id != r_fill_state.batch_tex_id) {
+ change_batch = true;
+ }
+
+ // N.B. polygons don't have color thus don't need a batch change with color
+
+ if (change_batch) {
+ // put the tex pixel size in a local (less verbose and can be a register)
+ const BatchTex &batchtex = bdata.batch_textures[r_fill_state.batch_tex_id];
+ batchtex.tex_pixel_size.to(r_fill_state.texpixel_size);
+
+ if (bdata.settings_uv_contract) {
+ r_fill_state.contract_uvs = (batchtex.flags & VS::TEXTURE_FLAG_FILTER) == 0;
+ }
+
+ // open new batch (this should never fail, it dynamically grows)
+ r_fill_state.curr_batch = _batch_request_new(false);
+
+ r_fill_state.curr_batch->type = RasterizerStorageCommon::BT_POLY;
+
+ // modulate unused except for debugging?
+ r_fill_state.curr_batch->color.set(modulate);
+ r_fill_state.curr_batch->batch_texture_id = r_fill_state.batch_tex_id;
+ r_fill_state.curr_batch->first_command = command_num;
+ r_fill_state.curr_batch->num_commands = num_inds;
+ // r_fill_state.curr_batch->num_elements = num_inds;
+ r_fill_state.curr_batch->first_vert = bdata.total_verts;
+ } else {
+ // we could alternatively do the count when closing a batch .. perhaps more efficient
+ r_fill_state.curr_batch->num_commands += num_inds;
+ }
+
+ // PRECALCULATE THE COLORS (as there may be less colors than there are indices
+ // in either hardware or software paths)
+ BatchColor vcol;
+ int num_verts = p_poly->points.size();
+
+ // in special cases, only 1 color is specified by convention, so we want to preset this
+ // to use in all verts.
+ if (p_poly->colors.size())
+ vcol.set(p_poly->colors[0] * modulate);
+ else
+ // color is undefined, use modulate color straight
+ vcol.set(modulate);
+
+ BatchColor *precalced_colors = (BatchColor *)alloca(num_verts * sizeof(BatchColor));
+
+ // two stage, super efficient setup of precalculated colors
+ int num_colors_specified = p_poly->colors.size();
+
+ for (int n = 0; n < num_colors_specified; n++) {
+ vcol.set(p_poly->colors[n] * modulate);
+ precalced_colors[n] = vcol;
+ }
+ for (int n = num_colors_specified; n < num_verts; n++) {
+ precalced_colors[n] = vcol;
+ }
+
+ if (!_software_skin_poly(p_poly, p_item, bvs, vertex_colors, r_fill_state, precalced_colors)) {
+
+ for (int n = 0; n < num_inds; n++) {
+ int ind = p_poly->indices[n];
+
+#if defined(TOOLS_ENABLED) && defined(DEBUG_ENABLED)
+ CRASH_COND(ind >= p_poly->points.size());
+#endif
+
+ // this could be moved outside the loop
+ if (r_fill_state.transform_mode != TM_NONE) {
+ Vector2 pos = p_poly->points[ind];
+ _software_transform_vertex(pos, r_fill_state.transform_combined);
+ bvs[n].pos.set(pos.x, pos.y);
+ } else {
+ const Point2 &pos = p_poly->points[ind];
+ bvs[n].pos.set(pos.x, pos.y);
+ }
+
+ if (ind < p_poly->uvs.size()) {
+ const Point2 &uv = p_poly->uvs[ind];
+ bvs[n].uv.set(uv.x, uv.y);
+ }
+
+ vertex_colors[n] = precalced_colors[ind];
+ }
+ } // if not software skinning
+
+ // increment total vert count
+ bdata.total_verts += num_inds;
+
+ return false;
+}
+
+PREAMBLE(bool)::_software_skin_poly(RasterizerCanvas::Item::CommandPolygon *p_poly, RasterizerCanvas::Item *p_item, BatchVertex *bvs, BatchColor *vertex_colors, const FillState &p_fill_state, const BatchColor *p_precalced_colors) {
+
+ // alternatively could check get_this()->state.using_skeleton
+ if (p_item->skeleton == RID())
+ return false;
+
+ int num_inds = p_poly->indices.size();
+ int num_verts = p_poly->points.size();
+
+ RID skeleton = p_item->skeleton;
+ int bone_count = RasterizerStorage::base_singleton->skeleton_get_bone_count(skeleton);
+
+ // we want a temporary buffer of positions to transform
+ Vector2 *pTemps = (Vector2 *)alloca(num_verts * sizeof(Vector2));
+ memset((void *)pTemps, 0, num_verts * sizeof(Vector2));
+
+ // these are used in the shader but don't appear to be needed for software transform
+ // const Transform2D &skel_trans = get_this()->state.skeleton_transform;
+ // const Transform2D &skel_trans_inv = get_this()->state.skeleton_transform_inverse;
+
+ // get the bone transforms.
+ // this is not ideal because we don't know in advance which bones are needed
+ // for any particular poly, but depends how cheap the skeleton_bone_get_transform_2d call is
+ Transform2D *bone_transforms = (Transform2D *)alloca(bone_count * sizeof(Transform2D));
+ for (int b = 0; b < bone_count; b++) {
+ bone_transforms[b] = RasterizerStorage::base_singleton->skeleton_bone_get_transform_2d(skeleton, b);
+ }
+
+ if (num_verts && (p_poly->bones.size() == num_verts * 4) && (p_poly->weights.size() == p_poly->bones.size())) {
+
+ for (int n = 0; n < num_verts; n++) {
+ const Vector2 &src_pos = p_poly->points[n];
+ Vector2 &dst_pos = pTemps[n];
+
+ // there can be an offset on the polygon at rigging time, this has to be accounted for
+ // note it may be possible that this could be concatenated with the bone transforms to save extra transforms - not sure yet
+ Vector2 src_pos_back_transformed = p_item->xform.xform(src_pos);
+
+ float total_weight = 0.0f;
+
+ for (int k = 0; k < 4; k++) {
+ int bone_id = p_poly->bones[n * 4 + k];
+ float weight = p_poly->weights[n * 4 + k];
+ if (weight == 0.0f)
+ continue;
+
+ total_weight += weight;
+
+#if defined(TOOLS_ENABLED) && defined(DEBUG_ENABLED)
+ CRASH_COND(bone_id >= bone_count);
+#endif
+ const Transform2D &bone_tr = bone_transforms[bone_id];
+
+ Vector2 pos = bone_tr.xform(src_pos_back_transformed);
+
+ dst_pos += pos * weight;
+ }
+
+ // this is some unexplained weirdness with verts with no weights,
+ // but it seemed to work for the example project ... watch for regressions
+ if (total_weight < 0.01f)
+ dst_pos = src_pos;
+ else {
+ dst_pos /= total_weight;
+
+ // retransform back from the poly offset space
+ dst_pos = p_item->xform.xform_inv(dst_pos);
+ }
+ }
+
+ // software transform with combined matrix?
+ if (p_fill_state.transform_mode != TM_NONE) {
+ for (int n = 0; n < num_verts; n++) {
+ Vector2 &dst_pos = pTemps[n];
+ _software_transform_vertex(dst_pos, p_fill_state.transform_combined);
+ }
+ }
+
+ } // if bone format matches
+ else {
+ // not supported
+ }
+
+ // output to the batch verts
+ for (int n = 0; n < num_inds; n++) {
+ int ind = p_poly->indices[n];
+
+#if defined(TOOLS_ENABLED) && defined(DEBUG_ENABLED)
+ CRASH_COND(ind >= num_verts);
+#endif
+ const Point2 &pos = pTemps[ind];
+ bvs[n].pos.set(pos.x, pos.y);
+
+ if (ind < p_poly->uvs.size()) {
+ const Point2 &uv = p_poly->uvs[ind];
+ bvs[n].uv.set(uv.x, uv.y);
+ }
+
+ vertex_colors[n] = p_precalced_colors[ind];
+ }
+
+ return true;
+}
+
+T_PREAMBLE
+template
+bool C_PREAMBLE::_prefill_rect(RasterizerCanvas::Item::CommandRect *rect, FillState &r_fill_state, int &r_command_start, int command_num, int command_count, RasterizerCanvas::Item::Command *const *commands, RasterizerCanvas::Item *p_item, bool multiply_final_modulate) {
+ bool change_batch = false;
+
+ // conditions for creating a new batch
+ if (r_fill_state.curr_batch->type != RasterizerStorageCommon::BT_RECT) {
+
+ // don't allow joining to a different sequence type
+ if (r_fill_state.sequence_batch_type_flags & (~RasterizerStorageCommon::BTF_RECT)) {
+ // don't allow joining to a different sequence type
+ r_command_start = command_num;
+ return true;
+ }
+ r_fill_state.sequence_batch_type_flags |= RasterizerStorageCommon::BTF_RECT;
+
+ change_batch = true;
+
+ // check for special case if there is only a single or small number of rects,
+ // in which case we will use the legacy default rect renderer
+ // because it is faster for single rects
+
+ // we only want to do this if not a joined item with more than 1 item,
+ // because joined items with more than 1, the command * will be incorrect
+ // NOTE - this is assuming that use_hardware_transform means that it is a non-joined item!!
+ // If that assumption is incorrect this will go horribly wrong.
+ if (bdata.settings_use_single_rect_fallback && r_fill_state.use_hardware_transform) {
+ bool is_single_rect = false;
+ int command_num_next = command_num + 1;
+ if (command_num_next < command_count) {
+ RasterizerCanvas::Item::Command *command_next = commands[command_num_next];
+ if ((command_next->type != RasterizerCanvas::Item::Command::TYPE_RECT) && (command_next->type != RasterizerCanvas::Item::Command::TYPE_TRANSFORM)) {
+ is_single_rect = true;
+ }
+ } else {
+ is_single_rect = true;
+ }
+ // if it is a rect on its own, do exactly the same as the default routine
+ if (is_single_rect) {
+ _prefill_default_batch(r_fill_state, command_num, *p_item);
+ return false;
+ }
+ } // if use hardware transform
+ }
+
+ // try to create vertices BEFORE creating a batch,
+ // because if the vertex buffer is full, we need to finish this
+ // function, draw what we have so far, and then start a new set of batches
+
+ // request FOUR vertices at a time, this is more efficient
+ BatchVertex *bvs = bdata.vertices.request(4);
+ if (!bvs) {
+ // run out of space in the vertex buffer .. finish this function and draw what we have so far
+ // return where we got to
+ r_command_start = command_num;
+ return true;
+ }
+
+ // are we using large FVF?
+ const bool use_large_verts = bdata.use_large_verts;
+ const bool use_modulate = bdata.use_modulate;
+
+ Color col = rect->modulate;
+
+ if (!use_large_verts) {
+ if (multiply_final_modulate) {
+ col *= r_fill_state.final_modulate;
+ }
+ }
+
+ // instead of doing all the texture preparation for EVERY rect,
+ // we build a list of texture combinations and do this once off.
+ // This means we have a potentially rather slow step to identify which texture combo
+ // using the RIDs.
+ int old_batch_tex_id = r_fill_state.batch_tex_id;
+ r_fill_state.batch_tex_id = _batch_find_or_create_tex(rect->texture, rect->normal_map, rect->flags & RasterizerCanvas::CANVAS_RECT_TILE, old_batch_tex_id);
+
+ //r_fill_state.use_light_angles = send_light_angles;
+ if (SEND_LIGHT_ANGLES) {
+ bdata.use_light_angles = true;
+ }
+
+ // conditions for creating a new batch
+ if (old_batch_tex_id != r_fill_state.batch_tex_id) {
+ change_batch = true;
+ }
+
+ // we need to treat color change separately because we need to count these
+ // to decide whether to switch on the fly to colored vertices.
+ if (!r_fill_state.curr_batch->color.equals(col)) {
+ change_batch = true;
+ bdata.total_color_changes++;
+ }
+
+ if (change_batch) {
+ // put the tex pixel size in a local (less verbose and can be a register)
+ const BatchTex &batchtex = bdata.batch_textures[r_fill_state.batch_tex_id];
+ batchtex.tex_pixel_size.to(r_fill_state.texpixel_size);
+
+ if (bdata.settings_uv_contract) {
+ r_fill_state.contract_uvs = (batchtex.flags & VS::TEXTURE_FLAG_FILTER) == 0;
+ }
+
+ // need to preserve texpixel_size between items
+ //r_fill_state.texpixel_size = r_fill_state.texpixel_size;
+
+ // open new batch (this should never fail, it dynamically grows)
+ r_fill_state.curr_batch = _batch_request_new(false);
+
+ r_fill_state.curr_batch->type = RasterizerStorageCommon::BT_RECT;
+ r_fill_state.curr_batch->color.set(col);
+ r_fill_state.curr_batch->batch_texture_id = r_fill_state.batch_tex_id;
+ r_fill_state.curr_batch->first_command = command_num;
+ r_fill_state.curr_batch->num_commands = 1;
+ //r_fill_state.curr_batch->first_quad = bdata.total_quads;
+ r_fill_state.curr_batch->first_vert = bdata.total_verts;
+ } else {
+ // we could alternatively do the count when closing a batch .. perhaps more efficient
+ r_fill_state.curr_batch->num_commands++;
+ }
+
+ // fill the quad geometry
+ Vector2 mins = rect->rect.position;
+
+ if (r_fill_state.transform_mode == TM_TRANSLATE) {
+
+ if (!use_large_verts) {
+ _software_transform_vertex(mins, r_fill_state.transform_combined);
+ }
+ }
+
+ Vector2 maxs = mins + rect->rect.size;
+
+ // just aliases
+ BatchVertex *bA = &bvs[0];
+ BatchVertex *bB = &bvs[1];
+ BatchVertex *bC = &bvs[2];
+ BatchVertex *bD = &bvs[3];
+
+ bA->pos.x = mins.x;
+ bA->pos.y = mins.y;
+
+ bB->pos.x = maxs.x;
+ bB->pos.y = mins.y;
+
+ bC->pos.x = maxs.x;
+ bC->pos.y = maxs.y;
+
+ bD->pos.x = mins.x;
+ bD->pos.y = maxs.y;
+
+ // possibility of applying flips here for normal mapping .. but they don't seem to be used
+ if (rect->rect.size.x < 0) {
+ SWAP(bA->pos, bB->pos);
+ SWAP(bC->pos, bD->pos);
+ }
+ if (rect->rect.size.y < 0) {
+ SWAP(bA->pos, bD->pos);
+ SWAP(bB->pos, bC->pos);
+ }
+
+ if (r_fill_state.transform_mode == TM_ALL) {
+
+ if (!use_large_verts) {
+ _software_transform_vertex(bA->pos, r_fill_state.transform_combined);
+ _software_transform_vertex(bB->pos, r_fill_state.transform_combined);
+ _software_transform_vertex(bC->pos, r_fill_state.transform_combined);
+ _software_transform_vertex(bD->pos, r_fill_state.transform_combined);
+ }
+ }
+
+ // uvs
+ Vector2 src_min;
+ Vector2 src_max;
+ if (rect->flags & RasterizerCanvas::CANVAS_RECT_REGION) {
+ src_min = rect->source.position;
+ src_max = src_min + rect->source.size;
+
+ src_min *= r_fill_state.texpixel_size;
+ src_max *= r_fill_state.texpixel_size;
+
+ const float uv_epsilon = bdata.settings_uv_contract_amount;
+
+ // nudge offset for the maximum to prevent precision error on GPU reading into line outside the source rect
+ // this is very difficult to get right.
+ if (r_fill_state.contract_uvs) {
+ src_min.x += uv_epsilon;
+ src_min.y += uv_epsilon;
+ src_max.x -= uv_epsilon;
+ src_max.y -= uv_epsilon;
+ }
+ } else {
+ src_min = Vector2(0, 0);
+ src_max = Vector2(1, 1);
+ }
+
+ // 10% faster calculating the max first
+ Vector2 uvs[4] = {
+ src_min,
+ Vector2(src_max.x, src_min.y),
+ src_max,
+ Vector2(src_min.x, src_max.y),
+ };
+
+ // for encoding in light angle
+ // flips should be optimized out when not being used for light angle.
+ bool flip_h = false;
+ bool flip_v = false;
+
+ if (rect->flags & RasterizerCanvas::CANVAS_RECT_TRANSPOSE) {
+ SWAP(uvs[1], uvs[3]);
+ }
+
+ if (rect->flags & RasterizerCanvas::CANVAS_RECT_FLIP_H) {
+ SWAP(uvs[0], uvs[1]);
+ SWAP(uvs[2], uvs[3]);
+ flip_h = !flip_h;
+ flip_v = !flip_v;
+ }
+ if (rect->flags & RasterizerCanvas::CANVAS_RECT_FLIP_V) {
+ SWAP(uvs[0], uvs[3]);
+ SWAP(uvs[1], uvs[2]);
+ flip_v = !flip_v;
+ }
+
+ bA->uv.set(uvs[0]);
+ bB->uv.set(uvs[1]);
+ bC->uv.set(uvs[2]);
+ bD->uv.set(uvs[3]);
+
+ // modulate
+ if (use_modulate) {
+ // store the final modulate separately from the rect modulate
+ BatchColor *pBC = bdata.vertex_modulates.request(4);
+#if defined(TOOLS_ENABLED) && defined(DEBUG_ENABLED)
+ CRASH_COND(pBC == nullptr);
+#endif
+ pBC[0].set(r_fill_state.final_modulate);
+ pBC[1] = pBC[0];
+ pBC[2] = pBC[0];
+ pBC[3] = pBC[0];
+ }
+
+ if (use_large_verts) {
+ // store the transform separately
+ BatchTransform *pBT = bdata.vertex_transforms.request(4);
+#if defined(TOOLS_ENABLED) && defined(DEBUG_ENABLED)
+ CRASH_COND(pBT == nullptr);
+#endif
+
+ const Transform2D &tr = r_fill_state.transform_combined;
+
+ pBT[0].translate.set(tr.elements[2]);
+ // could do swizzling in shader?
+ pBT[0].basis[0].set(tr.elements[0][0], tr.elements[1][0]);
+ pBT[0].basis[1].set(tr.elements[0][1], tr.elements[1][1]);
+
+ pBT[1] = pBT[0];
+ pBT[2] = pBT[0];
+ pBT[3] = pBT[0];
+ }
+
+ if (SEND_LIGHT_ANGLES) {
+ // we can either keep the light angles in sync with the verts when writing,
+ // or sync them up during translation. We are syncing in translation.
+ // N.B. There may be batches that don't require light_angles between batches that do.
+ float *angles = bdata.light_angles.request(4);
+#if defined(TOOLS_ENABLED) && defined(DEBUG_ENABLED)
+ CRASH_COND(angles == nullptr);
+#endif
+
+ float angle = 0.0f;
+ const float TWO_PI = Math_PI * 2;
+
+ if (r_fill_state.transform_mode != TM_NONE) {
+
+ const Transform2D &tr = r_fill_state.transform_combined;
+
+ // apply to an x axis
+ // the x axis and y axis can be taken directly from the transform (no need to xform identity vectors)
+ Vector2 x_axis(tr.elements[0][0], tr.elements[1][0]);
+
+ // have to do a y axis to check for scaling flips
+ // this is hassle and extra slowness. We could only allow flips via the flags.
+ Vector2 y_axis(tr.elements[0][1], tr.elements[1][1]);
+
+ // has the x / y axis flipped due to scaling?
+ float cross = x_axis.cross(y_axis);
+ if (cross < 0.0f) {
+ flip_v = !flip_v;
+ }
+
+ // passing an angle is smaller than a vector, it can be reconstructed in the shader
+ angle = x_axis.angle();
+
+ // we don't want negative angles, as negative is used to encode flips.
+ // This moves range from -PI to PI to 0 to TWO_PI
+ if (angle < 0.0f)
+ angle += TWO_PI;
+
+ } // if transform needed
+
+ // if horizontal flip, angle is shifted by 180 degrees
+ if (flip_h) {
+ angle += Math_PI;
+
+ // mod to get back to 0 to TWO_PI range
+ angle = fmodf(angle, TWO_PI);
+ }
+
+ // add 1 (to take care of zero floating point error with sign)
+ angle += 1.0f;
+
+ // flip if necessary to indicate a vertical flip in the shader
+ if (flip_v)
+ angle *= -1.0f;
+
+ // light angle must be sent for each vert, instead as a single uniform in the uniform draw method
+ // this has the benefit of enabling batching with light angles.
+ for (int n = 0; n < 4; n++) {
+ angles[n] = angle;
+ }
+ }
+
+ // increment quad count
+ bdata.total_quads++;
+ bdata.total_verts += 4;
+
+ return false;
+}
+
+// This function may be called MULTIPLE TIMES for each item, so needs to record how far it has got
+PREAMBLE(bool)::prefill_joined_item(FillState &r_fill_state, int &r_command_start, RasterizerCanvas::Item *p_item, RasterizerCanvas::Item *p_current_clip, bool &r_reclip, typename T_STORAGE::Material *p_material) {
+ // we will prefill batches and vertices ready for sending in one go to the vertex buffer
+ int command_count = p_item->commands.size();
+ RasterizerCanvas::Item::Command *const *commands = p_item->commands.ptr();
+
+ // checking the color for not being white makes it 92/90 times faster in the case where it is white
+ bool multiply_final_modulate = false;
+ if (!r_fill_state.use_hardware_transform && (r_fill_state.final_modulate != Color(1, 1, 1, 1))) {
+ multiply_final_modulate = true;
+ }
+
+ // start batch is a dummy batch (tex id -1) .. could be made more efficient
+ if (!r_fill_state.curr_batch) {
+ // OLD METHOD, but left dangling zero length default batches
+ // r_fill_state.curr_batch = _batch_request_new();
+ // r_fill_state.curr_batch->type = RasterizerStorageCommon::BT_DEFAULT;
+ // r_fill_state.curr_batch->first_command = r_command_start;
+ // should tex_id be set to -1? check this
+
+ // allocate dummy batch on the stack, it should always get replaced
+ // note that the rest of the structure is uninitialized, this should not matter
+ // if the type is checked before anything else.
+ r_fill_state.curr_batch = (Batch *)alloca(sizeof(Batch));
+ r_fill_state.curr_batch->type = RasterizerStorageCommon::BT_DUMMY;
+
+ // this is assumed to be the case
+ //CRASH_COND (r_fill_state.transform_extra_command_number_p1);
+ }
+
+ // we need to return which command we got up to, so
+ // store this outside the loop
+ int command_num;
+
+ // do as many commands as possible until the vertex buffer will be full up
+ for (command_num = r_command_start; command_num < command_count; command_num++) {
+
+ RasterizerCanvas::Item::Command *command = commands[command_num];
+
+ switch (command->type) {
+
+ default: {
+ _prefill_default_batch(r_fill_state, command_num, *p_item);
+ } break;
+ case RasterizerCanvas::Item::Command::TYPE_TRANSFORM: {
+ // if the extra matrix has been sent already,
+ // break this extra matrix software path (as we don't want to unset it on the GPU etc)
+ if (r_fill_state.extra_matrix_sent) {
+ _prefill_default_batch(r_fill_state, command_num, *p_item);
+ } else {
+ // Extra matrix fast path.
+ // Instead of sending the command immediately, we store the modified transform (in combined)
+ // for software transform, and only flush this transform command if we NEED to (i.e. we want to
+ // render some default commands)
+ RasterizerCanvas::Item::CommandTransform *transform = static_cast(command);
+ const Transform2D &extra_matrix = transform->xform;
+
+ if (r_fill_state.use_hardware_transform) {
+ // if we are using hardware transform mode, we have already sent the final transform,
+ // so we only want to software transform the extra matrix
+ r_fill_state.transform_combined = extra_matrix;
+ } else {
+ r_fill_state.transform_combined = p_item->final_transform * extra_matrix;
+ }
+ // after a transform command, always use some form of software transform (either the combined final + extra, or just the extra)
+ // until we flush this dirty extra matrix because we need to render default commands.
+ r_fill_state.transform_mode = _find_transform_mode(r_fill_state.transform_combined);
+
+ // make a note of which command the dirty extra matrix is store in, so we can send it later
+ // if necessary
+ r_fill_state.transform_extra_command_number_p1 = command_num + 1; // plus 1 so we can test against zero
+ }
+ } break;
+ case RasterizerCanvas::Item::Command::TYPE_RECT: {
+
+ RasterizerCanvas::Item::CommandRect *rect = static_cast(command);
+
+ // unoptimized - could this be done once per batch / batch texture?
+ bool send_light_angles = rect->normal_map != RID();
+
+ bool buffer_full = false;
+
+ // the template params must be explicit for compilation,
+ // this forces building the multiple versions of the function.
+ if (send_light_angles) {
+ buffer_full = _prefill_rect(rect, r_fill_state, r_command_start, command_num, command_count, commands, p_item, multiply_final_modulate);
+ } else {
+ buffer_full = _prefill_rect(rect, r_fill_state, r_command_start, command_num, command_count, commands, p_item, multiply_final_modulate);
+ }
+
+ if (buffer_full)
+ return true;
+
+ } break;
+ case RasterizerCanvas::Item::Command::TYPE_NINEPATCH: {
+
+ RasterizerCanvas::Item::CommandNinePatch *np = static_cast(command);
+
+ if ((np->axis_x != VisualServer::NINE_PATCH_STRETCH) || (np->axis_y != VisualServer::NINE_PATCH_STRETCH)) {
+ // not accelerated
+ _prefill_default_batch(r_fill_state, command_num, *p_item);
+ continue;
+ }
+
+ // unoptimized - could this be done once per batch / batch texture?
+ bool send_light_angles = np->normal_map != RID();
+
+ bool buffer_full = false;
+
+ if (send_light_angles)
+ buffer_full = _prefill_ninepatch(np, r_fill_state, r_command_start, command_num, command_count, p_item, multiply_final_modulate);
+ else
+ buffer_full = _prefill_ninepatch(np, r_fill_state, r_command_start, command_num, command_count, p_item, multiply_final_modulate);
+
+ if (buffer_full)
+ return true;
+
+ } break;
+
+ case RasterizerCanvas::Item::Command::TYPE_LINE: {
+
+ RasterizerCanvas::Item::CommandLine *line = static_cast(command);
+
+ if (line->width <= 1) {
+ bool buffer_full = _prefill_line(line, r_fill_state, r_command_start, command_num, command_count, p_item, multiply_final_modulate);
+
+ if (buffer_full)
+ return true;
+ } else {
+ // not accelerated
+ _prefill_default_batch(r_fill_state, command_num, *p_item);
+ }
+ } break;
+
+ case RasterizerCanvas::Item::Command::TYPE_POLYGON: {
+
+ // not using software skinning?
+ if (!bdata.settings_use_software_skinning && get_this()->state.using_skeleton) {
+ // not accelerated
+ _prefill_default_batch(r_fill_state, command_num, *p_item);
+ } else {
+ RasterizerCanvas::Item::CommandPolygon *polygon = static_cast(command);
+
+ // unoptimized - could this be done once per batch / batch texture?
+ bool send_light_angles = polygon->normal_map != RID();
+
+ bool buffer_full = false;
+
+ if (send_light_angles) {
+ // NYI
+ _prefill_default_batch(r_fill_state, command_num, *p_item);
+ //buffer_full = prefill_polygon(polygon, r_fill_state, r_command_start, command_num, command_count, p_item, multiply_final_modulate);
+ } else
+ buffer_full = _prefill_polygon(polygon, r_fill_state, r_command_start, command_num, command_count, p_item, multiply_final_modulate);
+
+ if (buffer_full)
+ return true;
+ } // using software skinning path
+
+ } break;
+ }
+ }
+
+ // VERY IMPORTANT to return where we got to, because this func may be called multiple
+ // times per item.
+ // Don't miss out on this step by calling return earlier in the function without setting r_command_start.
+ r_command_start = command_num;
+
+ return false;
+}
+
+PREAMBLE(void)::flush_render_batches(RasterizerCanvas::Item *p_first_item, RasterizerCanvas::Item *p_current_clip, bool &r_reclip, typename T_STORAGE::Material *p_material) {
+
+ // some heuristic to decide whether to use colored verts.
+ // feel free to tweak this.
+ // this could use hysteresis, to prevent jumping between methods
+ // .. however probably not necessary
+ bdata.use_colored_vertices = false;
+
+ // switch from regular to colored?
+ if (bdata.fvf == RasterizerStorageCommon::FVF_REGULAR) {
+ // only check whether to convert if there are quads (prevent divide by zero)
+ // and we haven't decided to prevent color baking (due to e.g. MODULATE
+ // being used in a shader)
+ if (bdata.total_quads && !(bdata.joined_item_batch_flags & RasterizerStorageCommon::PREVENT_COLOR_BAKING)) {
+ // minus 1 to prevent single primitives (ratio 1.0) always being converted to colored..
+ // in that case it is slightly cheaper to just have the color as part of the batch
+ float ratio = (float)(bdata.total_color_changes - 1) / (float)bdata.total_quads;
+
+ // use bigger than or equal so that 0.0 threshold can force always using colored verts
+ if (ratio >= bdata.settings_colored_vertex_format_threshold) {
+ bdata.use_colored_vertices = true;
+ bdata.fvf = RasterizerStorageCommon::FVF_COLOR;
+ }
+ }
+
+ // if we used vertex colors
+ if (bdata.vertex_colors.size()) {
+ bdata.use_colored_vertices = true;
+ bdata.fvf = RasterizerStorageCommon::FVF_COLOR;
+ }
+
+ // needs light angles?
+ if (bdata.use_light_angles) {
+ bdata.fvf = RasterizerStorageCommon::FVF_LIGHT_ANGLE;
+ }
+ }
+
+ // translate if required to larger FVFs
+ switch (bdata.fvf) {
+ case RasterizerStorageCommon::FVF_UNBATCHED: // should not happen
+ break;
+ case RasterizerStorageCommon::FVF_REGULAR: // no change
+ break;
+ case RasterizerStorageCommon::FVF_COLOR: {
+ // special case, where vertex colors are used (polys)
+ if (!bdata.vertex_colors.size())
+ _translate_batches_to_larger_FVF();
+ else
+ // normal, reduce number of batches by baking batch colors
+ _translate_batches_to_vertex_colored_FVF();
+ } break;
+ case RasterizerStorageCommon::FVF_LIGHT_ANGLE:
+ _translate_batches_to_larger_FVF();
+ break;
+ case RasterizerStorageCommon::FVF_MODULATED:
+ _translate_batches_to_larger_FVF();
+ break;
+ case RasterizerStorageCommon::FVF_LARGE:
+ _translate_batches_to_larger_FVF();
+ break;
+ }
+
+ // send buffers to opengl
+ get_this()->_batch_upload_buffers();
+
+ RasterizerCanvas::Item::Command *const *commands = p_first_item->commands.ptr();
+
+#if defined(TOOLS_ENABLED) && defined(DEBUG_ENABLED)
+ if (bdata.diagnose_frame) {
+ diagnose_batches(commands);
+ }
+#endif
+
+ get_this()->render_batches(commands, p_current_clip, r_reclip, p_material);
+}
+
+PREAMBLE(void)::render_joined_item_commands(const BItemJoined &p_bij, RasterizerCanvas::Item *p_current_clip, bool &r_reclip, typename T_STORAGE::Material *p_material, bool p_lit) {
+
+ RasterizerCanvas::Item *item = 0;
+ RasterizerCanvas::Item *first_item = bdata.item_refs[p_bij.first_item_ref].item;
+
+ // fill_state and bdata have once off setup per joined item, and a smaller reset on flush
+ FillState fill_state;
+ fill_state.reset_joined_item(p_bij.use_hardware_transform());
+
+ bdata.reset_joined_item();
+
+ // should this joined item be using large FVF?
+ if (p_bij.flags & RasterizerStorageCommon::USE_MODULATE_FVF) {
+ bdata.use_modulate = true;
+ bdata.fvf = RasterizerStorageCommon::FVF_MODULATED;
+ }
+ if (p_bij.flags & RasterizerStorageCommon::USE_LARGE_FVF) {
+ bdata.use_modulate = true;
+ bdata.use_large_verts = true;
+ bdata.fvf = RasterizerStorageCommon::FVF_LARGE;
+ }
+
+ // in the special case of custom shaders that read from VERTEX (i.e. vertex position)
+ // we want to disable software transform of extra matrix
+ if (bdata.joined_item_batch_flags & RasterizerStorageCommon::PREVENT_VERTEX_BAKING) {
+ fill_state.extra_matrix_sent = true;
+ }
+
+ for (unsigned int i = 0; i < p_bij.num_item_refs; i++) {
+ const BItemRef &ref = bdata.item_refs[p_bij.first_item_ref + i];
+ item = ref.item;
+
+ if (!p_lit) {
+ // if not lit we use the complex calculated final modulate
+ fill_state.final_modulate = ref.final_modulate;
+ } else {
+ // if lit we ignore canvas modulate and just use the item modulate
+ fill_state.final_modulate = item->final_modulate;
+ }
+
+ int command_count = item->commands.size();
+ int command_start = 0;
+
+ // ONCE OFF fill state setup, that will be retained over multiple calls to
+ // prefill_joined_item()
+ fill_state.transform_combined = item->final_transform;
+
+ // decide the initial transform mode, and make a backup
+ // in orig_transform_mode in case we need to switch back
+ if (!fill_state.use_hardware_transform) {
+ fill_state.transform_mode = _find_transform_mode(fill_state.transform_combined);
+ } else {
+ fill_state.transform_mode = TM_NONE;
+ }
+ fill_state.orig_transform_mode = fill_state.transform_mode;
+
+ // keep track of when we added an extra matrix
+ // so we can defer sending until we see a default command
+ fill_state.transform_extra_command_number_p1 = 0;
+
+ while (command_start < command_count) {
+ // fill as many batches as possible (until all done, or the vertex buffer is full)
+ bool bFull = get_this()->prefill_joined_item(fill_state, command_start, item, p_current_clip, r_reclip, p_material);
+
+ if (bFull) {
+ // always pass first item (commands for default are always first item)
+ flush_render_batches(first_item, p_current_clip, r_reclip, p_material);
+
+ // zero all the batch data ready for a new run
+ bdata.reset_flush();
+
+ // don't zero all the fill state, some may need to be preserved
+ fill_state.reset_flush();
+ }
+ }
+ }
+
+ // flush if any left
+ flush_render_batches(first_item, p_current_clip, r_reclip, p_material);
+
+ // zero all the batch data ready for a new run
+ bdata.reset_flush();
+}
+
+PREAMBLE(void)::_legacy_canvas_item_render_commands(RasterizerCanvas::Item *p_item, RasterizerCanvas::Item *p_current_clip, bool &r_reclip, typename T_STORAGE::Material *p_material) {
+
+ int command_count = p_item->commands.size();
+
+ RasterizerCanvas::Item::Command *const *commands = p_item->commands.ptr();
+
+ // legacy .. just create one massive batch and render everything as before
+ bdata.batches.reset();
+ Batch *batch = _batch_request_new();
+ batch->type = RasterizerStorageCommon::BT_DEFAULT;
+ batch->num_commands = command_count;
+
+ get_this()->render_batches(commands, p_current_clip, r_reclip, p_material);
+ bdata.reset_flush();
+}
+
+PREAMBLE(void)::record_items(RasterizerCanvas::Item *p_item_list, int p_z) {
+ while (p_item_list) {
+ BSortItem *s = bdata.sort_items.request_with_grow();
+
+ s->item = p_item_list;
+ s->z_index = p_z;
+
+ p_item_list = p_item_list->next;
+ }
+}
+
+PREAMBLE(void)::join_sorted_items() {
+ sort_items();
+
+ int z = VS::CANVAS_ITEM_Z_MIN;
+ _render_item_state.item_group_z = z;
+
+ for (int s = 0; s < bdata.sort_items.size(); s++) {
+ const BSortItem &si = bdata.sort_items[s];
+ RasterizerCanvas::Item *ci = si.item;
+
+ // change z?
+ if (si.z_index != z) {
+ z = si.z_index;
+
+ // may not be required
+ _render_item_state.item_group_z = z;
+
+ // if z ranged lights are present, sometimes we have to disable joining over z_indices.
+ // we do this here.
+ // Note this restriction may be able to be relaxed with light bitfields, investigate!
+ if (!bdata.join_across_z_indices) {
+ _render_item_state.join_batch_break = true;
+ }
+ }
+
+ bool join;
+
+ if (_render_item_state.join_batch_break) {
+ // always start a new batch for this item
+ join = false;
+
+ // could be another batch break (i.e. prevent NEXT item from joining this)
+ // so we still need to run try_join_item
+ // even though we know join is false.
+ // also we need to run try_join_item for every item because it keeps the state up to date,
+ // if we didn't run it the state would be out of date.
+ get_this()->try_join_item(ci, _render_item_state, _render_item_state.join_batch_break);
+ } else {
+ join = get_this()->try_join_item(ci, _render_item_state, _render_item_state.join_batch_break);
+ }
+
+ // assume the first item will always return no join
+ if (!join) {
+ _render_item_state.joined_item = bdata.items_joined.request_with_grow();
+ _render_item_state.joined_item->first_item_ref = bdata.item_refs.size();
+ _render_item_state.joined_item->num_item_refs = 1;
+ _render_item_state.joined_item->bounding_rect = ci->global_rect_cache;
+ _render_item_state.joined_item->z_index = z;
+ _render_item_state.joined_item->flags = bdata.joined_item_batch_flags;
+
+ // we need some logic to prevent joining items that have vastly different batch types
+ _render_item_state.joined_item_batch_type_flags_prev = _render_item_state.joined_item_batch_type_flags_curr;
+
+ // add the reference
+ BItemRef *r = bdata.item_refs.request_with_grow();
+ r->item = ci;
+ // we are storing final_modulate in advance per item reference
+ // for baking into vertex colors.
+ // this may not be ideal... as we are increasing the size of item reference,
+ // but it is stupidly complex to calculate later, which would probably be slower.
+ r->final_modulate = _render_item_state.final_modulate;
+ } else {
+ CRASH_COND(_render_item_state.joined_item == 0);
+ _render_item_state.joined_item->num_item_refs += 1;
+ _render_item_state.joined_item->bounding_rect = _render_item_state.joined_item->bounding_rect.merge(ci->global_rect_cache);
+
+ BItemRef *r = bdata.item_refs.request_with_grow();
+ r->item = ci;
+ r->final_modulate = _render_item_state.final_modulate;
+ }
+
+ } // for s through sort items
+}
+
+PREAMBLE(void)::sort_items() {
+ // turned off?
+ if (!bdata.settings_item_reordering_lookahead) {
+ return;
+ }
+
+ for (int s = 0; s < bdata.sort_items.size() - 2; s++) {
+ if (sort_items_from(s)) {
+#if defined(TOOLS_ENABLED) && defined(DEBUG_ENABLED)
+ bdata.stats_items_sorted++;
+#endif
+ }
+ }
+}
+
+PREAMBLE(bool)::_sort_items_match(const BSortItem &p_a, const BSortItem &p_b) const {
+ const RasterizerCanvas::Item *a = p_a.item;
+ const RasterizerCanvas::Item *b = p_b.item;
+
+ if (b->commands.size() != 1)
+ return false;
+
+ // tested outside function
+ // if (a->commands.size() != 1)
+ // return false;
+
+ const RasterizerCanvas::Item::Command &cb = *b->commands[0];
+ if (cb.type != RasterizerCanvas::Item::Command::TYPE_RECT)
+ return false;
+
+ const RasterizerCanvas::Item::Command &ca = *a->commands[0];
+ // tested outside function
+ // if (ca.type != Item::Command::TYPE_RECT)
+ // return false;
+
+ const RasterizerCanvas::Item::CommandRect *rect_a = static_cast(&ca);
+ const RasterizerCanvas::Item::CommandRect *rect_b = static_cast(&cb);
+
+ if (rect_a->texture != rect_b->texture)
+ return false;
+
+ /* ALTERNATIVE APPROACH NOT LIMITED TO RECTS
+const RasterizerCanvas::Item::Command &ca = *a->commands[0];
+const RasterizerCanvas::Item::Command &cb = *b->commands[0];
+
+if (ca.type != cb.type)
+ return false;
+
+// do textures match?
+switch (ca.type)
+{
+default:
+ break;
+case RasterizerCanvas::Item::Command::TYPE_RECT:
+ {
+ const RasterizerCanvas::Item::CommandRect *comm_a = static_cast(&ca);
+ const RasterizerCanvas::Item::CommandRect *comm_b = static_cast(&cb);
+ if (comm_a->texture != comm_b->texture)
+ return false;
+ }
+ break;
+case RasterizerCanvas::Item::Command::TYPE_POLYGON:
+ {
+ const RasterizerCanvas::Item::CommandPolygon *comm_a = static_cast(&ca);
+ const RasterizerCanvas::Item::CommandPolygon *comm_b = static_cast(&cb);
+ if (comm_a->texture != comm_b->texture)
+ return false;
+ }
+ break;
+}
+*/
+
+ return true;
+}
+
+PREAMBLE(bool)::sort_items_from(int p_start) {
+#if defined(TOOLS_ENABLED) && defined(DEBUG_ENABLED)
+ ERR_FAIL_COND_V((p_start + 1) >= bdata.sort_items.size(), false)
+#endif
+
+ const BSortItem &start = bdata.sort_items[p_start];
+ int start_z = start.z_index;
+
+ // check start is the right type for sorting
+ if (start.item->commands.size() != 1) {
+ return false;
+ }
+ const RasterizerCanvas::Item::Command &command_start = *start.item->commands[0];
+ if (command_start.type != RasterizerCanvas::Item::Command::TYPE_RECT) {
+ return false;
+ }
+
+ BSortItem &second = bdata.sort_items[p_start + 1];
+ if (second.z_index != start_z) {
+ // no sorting across z indices (for now)
+ return false;
+ }
+
+ // if the neighbours are already a good match
+ if (_sort_items_match(start, second)) // order is crucial, start first
+ {
+ return false;
+ }
+
+ // local cached aabb
+ Rect2 second_AABB = second.item->global_rect_cache;
+
+ // if the start and 2nd items overlap, can do no more
+ if (start.item->global_rect_cache.intersects(second_AABB)) {
+ return false;
+ }
+
+ // which neighbour to test
+ int test_last = 2 + bdata.settings_item_reordering_lookahead;
+ for (int test = 2; test < test_last; test++) {
+ int test_sort_item_id = p_start + test;
+
+ // if we've got to the end of the list, can't sort any more, give up
+ if (test_sort_item_id >= bdata.sort_items.size()) {
+ return false;
+ }
+
+ BSortItem *test_sort_item = &bdata.sort_items[test_sort_item_id];
+
+ // across z indices?
+ if (test_sort_item->z_index != start_z) {
+ return false;
+ }
+
+ RasterizerCanvas::Item *test_item = test_sort_item->item;
+
+ // if the test item overlaps the second item, we can't swap, AT ALL
+ // because swapping an item OVER this one would cause artefacts
+ if (second_AABB.intersects(test_item->global_rect_cache)) {
+ return false;
+ }
+
+ // do they match?
+ if (!_sort_items_match(start, *test_sort_item)) // order is crucial, start first
+ {
+ continue;
+ }
+
+ // we can only swap if there are no AABB overlaps with sandwiched neighbours
+ bool ok = true;
+
+ // start from 2, no need to check 1 as the second has already been checked against this item
+ // in the intersection test above
+ for (int sn = 2; sn < test; sn++) {
+ BSortItem *sandwich_neighbour = &bdata.sort_items[p_start + sn];
+ if (test_item->global_rect_cache.intersects(sandwich_neighbour->item->global_rect_cache)) {
+ ok = false;
+ break;
+ }
+ }
+ if (!ok) {
+ continue;
+ }
+
+ // it is ok to exchange them!
+ BSortItem temp;
+ temp.assign(second);
+ second.assign(*test_sort_item);
+ test_sort_item->assign(temp);
+
+ return true;
+ } // for test
+
+ return false;
+}
+
+PREAMBLE(void)::_software_transform_vertex(BatchVector2 &r_v, const Transform2D &p_tr) const {
+ Vector2 vc(r_v.x, r_v.y);
+ vc = p_tr.xform(vc);
+ r_v.set(vc);
+}
+
+PREAMBLE(void)::_software_transform_vertex(Vector2 &r_v, const Transform2D &p_tr) const {
+ r_v = p_tr.xform(r_v);
+}
+
+PREAMBLE(void)::_translate_batches_to_vertex_colored_FVF() {
+ // zeros the size and sets up how big each unit is
+ bdata.unit_vertices.prepare(sizeof(BatchVertexColored));
+
+ const BatchColor *source_vertex_colors = &bdata.vertex_colors[0];
+ CRASH_COND(bdata.vertex_colors.size() != bdata.vertices.size());
+
+ int num_verts = bdata.vertices.size();
+
+ for (int n = 0; n < num_verts; n++) {
+ const BatchVertex &bv = bdata.vertices[n];
+
+ BatchVertexColored *cv = (BatchVertexColored *)bdata.unit_vertices.request();
+
+ cv->pos = bv.pos;
+ cv->uv = bv.uv;
+ cv->col = *source_vertex_colors++;
+ }
+}
+
+// Translation always involved adding color to the FVF, which enables
+// joining of batches that have different colors.
+// There is a trade off. Non colored verts are smaller so work faster, but
+// there comes a point where it is better to just use colored verts to avoid lots of
+// batches.
+// In addition this can optionally add light angles to the FVF, necessary for normal mapping.
+T_PREAMBLE
+template
+void C_PREAMBLE::_translate_batches_to_larger_FVF() {
+
+ // zeros the size and sets up how big each unit is
+ bdata.unit_vertices.prepare(sizeof(BATCH_VERTEX_TYPE));
+ bdata.batches_temp.reset();
+
+ // As the vertices_colored and batches_temp are 'mirrors' of the non-colored version,
+ // the sizes should be equal, and allocations should never fail. Hence the use of debug
+ // asserts to check program flow, these should not occur at runtime unless the allocation
+ // code has been altered.
+#if defined(TOOLS_ENABLED) && defined(DEBUG_ENABLED)
+ CRASH_COND(bdata.unit_vertices.max_size() != bdata.vertices.max_size());
+ CRASH_COND(bdata.batches_temp.max_size() != bdata.batches.max_size());
+#endif
+
+ Color curr_col(-1.0f, -1.0f, -1.0f, -1.0f);
+
+ Batch *dest_batch = nullptr;
+
+ const float *source_light_angles = &bdata.light_angles[0];
+ const BatchColor *source_vertex_modulates = &bdata.vertex_modulates[0];
+ const BatchTransform *source_vertex_transforms = &bdata.vertex_transforms[0];
+
+ // translate the batches into vertex colored batches
+ for (int n = 0; n < bdata.batches.size(); n++) {
+ const Batch &source_batch = bdata.batches[n];
+
+ // does source batch use light angles?
+ const BatchTex &btex = bdata.batch_textures[source_batch.batch_texture_id];
+ bool source_batch_uses_light_angles = btex.RID_normal != RID();
+
+ bool needs_new_batch = true;
+
+ if (dest_batch) {
+ if (dest_batch->type == source_batch.type) {
+ if (source_batch.type == RasterizerStorageCommon::BT_RECT) {
+ if (dest_batch->batch_texture_id == source_batch.batch_texture_id) {
+ // add to previous batch
+ dest_batch->num_commands += source_batch.num_commands;
+ needs_new_batch = false;
+
+ // create the colored verts (only if not default)
+ //int first_vert = source_batch.first_quad * 4;
+ //int end_vert = 4 * (source_batch.first_quad + source_batch.num_commands);
+ int first_vert = source_batch.first_vert;
+ int end_vert = first_vert + (4 * source_batch.num_commands);
+
+ for (int v = first_vert; v < end_vert; v++) {
+ const BatchVertex &bv = bdata.vertices[v];
+ BATCH_VERTEX_TYPE *cv = (BATCH_VERTEX_TYPE *)bdata.unit_vertices.request();
+#if defined(TOOLS_ENABLED) && defined(DEBUG_ENABLED)
+ CRASH_COND(!cv);
+#endif
+ cv->pos = bv.pos;
+ cv->uv = bv.uv;
+ cv->col = source_batch.color;
+
+ if (INCLUDE_LIGHT_ANGLES) {
+ // this is required to allow compilation with non light angle vertex.
+ // it should be compiled out.
+ BatchVertexLightAngled *lv = (BatchVertexLightAngled *)cv;
+ if (source_batch_uses_light_angles)
+ lv->light_angle = *source_light_angles++;
+ else
+ lv->light_angle = 0.0f; // dummy, unused in vertex shader (could possibly be left uninitialized, but probably bad idea)
+ } // if including light angles
+
+ if (INCLUDE_MODULATE) {
+ BatchVertexModulated *mv = (BatchVertexModulated *)cv;
+ mv->modulate = *source_vertex_modulates++;
+ } // including modulate
+
+ if (INCLUDE_LARGE) {
+ BatchVertexLarge *lv = (BatchVertexLarge *)cv;
+ lv->transform = *source_vertex_transforms++;
+ } // if including large
+ }
+ } // textures match
+ } else {
+ // default
+ // we can still join, but only under special circumstances
+ // does this ever happen? not sure at this stage, but left for future expansion
+ uint32_t source_last_command = source_batch.first_command + source_batch.num_commands;
+ if (source_last_command == dest_batch->first_command) {
+ dest_batch->num_commands += source_batch.num_commands;
+ needs_new_batch = false;
+ } // if the commands line up exactly
+ }
+ } // if both batches are the same type
+
+ } // if dest batch is valid
+
+ if (needs_new_batch) {
+ dest_batch = bdata.batches_temp.request();
+#if defined(TOOLS_ENABLED) && defined(DEBUG_ENABLED)
+ CRASH_COND(!dest_batch);
+#endif
+
+ *dest_batch = source_batch;
+
+ // create the colored verts (only if not default)
+ if (source_batch.type != RasterizerStorageCommon::BT_DEFAULT) {
+ // int first_vert = source_batch.first_quad * 4;
+ // int end_vert = 4 * (source_batch.first_quad + source_batch.num_commands);
+ int first_vert = source_batch.first_vert;
+ int end_vert = first_vert + (4 * source_batch.num_commands);
+
+ for (int v = first_vert; v < end_vert; v++) {
+ const BatchVertex &bv = bdata.vertices[v];
+ BATCH_VERTEX_TYPE *cv = (BATCH_VERTEX_TYPE *)bdata.unit_vertices.request();
+#if defined(TOOLS_ENABLED) && defined(DEBUG_ENABLED)
+ CRASH_COND(!cv);
+#endif
+ cv->pos = bv.pos;
+ cv->uv = bv.uv;
+ cv->col = source_batch.color;
+
+ if (INCLUDE_LIGHT_ANGLES) {
+ // this is required to allow compilation with non light angle vertex.
+ // it should be compiled out.
+ BatchVertexLightAngled *lv = (BatchVertexLightAngled *)cv;
+ if (source_batch_uses_light_angles)
+ lv->light_angle = *source_light_angles++;
+ else
+ lv->light_angle = 0.0f; // dummy, unused in vertex shader (could possibly be left uninitialized, but probably bad idea)
+ } // if using light angles
+
+ if (INCLUDE_MODULATE) {
+ BatchVertexModulated *mv = (BatchVertexModulated *)cv;
+ mv->modulate = *source_vertex_modulates++;
+ } // including modulate
+
+ if (INCLUDE_LARGE) {
+ BatchVertexLarge *lv = (BatchVertexLarge *)cv;
+ lv->transform = *source_vertex_transforms++;
+ } // if including large
+ }
+ }
+ }
+ }
+
+ // copy the temporary batches to the master batch list (this could be avoided but it makes the code cleaner)
+ bdata.batches.copy_from(bdata.batches_temp);
+}
+
+PREAMBLE(bool)::_disallow_item_join_if_batch_types_too_different(RenderItemState &r_ris, uint32_t btf_allowed) {
+ r_ris.joined_item_batch_type_flags_curr |= btf_allowed;
+
+ bool disallow = false;
+
+ if (r_ris.joined_item_batch_type_flags_prev & (~btf_allowed))
+ disallow = true;
+
+ return disallow;
+}
+
+PREAMBLE(bool)::_detect_item_batch_break(RenderItemState &r_ris, RasterizerCanvas::Item *p_ci, bool &r_batch_break) {
+ int command_count = p_ci->commands.size();
+
+ // Any item that contains commands that are default
+ // (i.e. not handled by software transform and the batching renderer) should not be joined.
+
+ // ALSO batched types that differ in what the vertex format is needed to be should not be
+ // joined.
+
+ // In order to work this out, it does a lookahead through the commands,
+ // which could potentially be very expensive. As such it makes sense to put a limit on this
+ // to some small number, which will catch nearly all cases which need joining,
+ // but not be overly expensive in the case of items with large numbers of commands.
+
+ // It is hard to know what this number should be, empirically,
+ // and this has not been fully investigated. It works to join single sprite items when set to 1 or above.
+ // Note that there is a cost to increasing this because it has to look in advance through
+ // the commands.
+ // On the other hand joining items where possible will usually be better up to a certain
+ // number where the cost of software transform is higher than separate drawcalls with hardware
+ // transform.
+
+ // if there are more than this number of commands in the item, we
+ // don't allow joining (separate state changes, and hardware transform)
+ // This is set to quite a conservative (low) number until investigated properly.
+ // const int MAX_JOIN_ITEM_COMMANDS = 16;
+
+ r_ris.joined_item_batch_type_flags_curr = 0;
+
+ if (command_count > bdata.settings_max_join_item_commands) {
+ return true;
+ } else {
+ RasterizerCanvas::Item::Command *const *commands = p_ci->commands.ptr();
+
+ // run through the commands looking for one that could prevent joining
+ for (int command_num = 0; command_num < command_count; command_num++) {
+
+ RasterizerCanvas::Item::Command *command = commands[command_num];
+ CRASH_COND(!command);
+
+ switch (command->type) {
+
+ default: {
+ //r_batch_break = true;
+ return true;
+ } break;
+ case RasterizerCanvas::Item::Command::TYPE_LINE: {
+ // special case, only batches certain lines
+ RasterizerCanvas::Item::CommandLine *line = static_cast(command);
+
+ if (line->width > 1) {
+ //r_batch_break = true;
+ return true;
+ }
+
+ if (_disallow_item_join_if_batch_types_too_different(r_ris, RasterizerStorageCommon::BTF_LINE | RasterizerStorageCommon::BTF_LINE_AA)) {
+ return true;
+ }
+ } break;
+ case RasterizerCanvas::Item::Command::TYPE_POLYGON: {
+ //return true;
+ // only allow polygons to join if they aren't skeleton
+ RasterizerCanvas::Item::CommandPolygon *poly = static_cast(command);
+
+ // return true;
+
+ // light angles not yet implemented, treat as default
+ if (poly->normal_map != RID())
+ return true;
+
+ // we could possibly join polygons that are software skinned? NYI
+
+ if (!get_this()->bdata.settings_use_software_skinning && poly->bones.size())
+ return true;
+
+ if (_disallow_item_join_if_batch_types_too_different(r_ris, RasterizerStorageCommon::BTF_POLY)) {
+ //r_batch_break = true;
+ return true;
+ }
+ } break;
+ case RasterizerCanvas::Item::Command::TYPE_RECT: {
+ if (_disallow_item_join_if_batch_types_too_different(r_ris, RasterizerStorageCommon::BTF_RECT))
+ return true;
+ } break;
+ case RasterizerCanvas::Item::Command::TYPE_NINEPATCH: {
+ // do not handle tiled ninepatches, these can't be batched and need to use legacy method
+ RasterizerCanvas::Item::CommandNinePatch *np = static_cast(command);
+ if ((np->axis_x != VisualServer::NINE_PATCH_STRETCH) || (np->axis_y != VisualServer::NINE_PATCH_STRETCH))
+ return true;
+
+ if (_disallow_item_join_if_batch_types_too_different(r_ris, RasterizerStorageCommon::BTF_RECT))
+ return true;
+ } break;
+ case RasterizerCanvas::Item::Command::TYPE_TRANSFORM: {
+ // compatible with all types
+ } break;
+ } // switch
+
+ } // for through commands
+
+ } // else
+
+ return false;
+}
+
+#undef PREAMBLE
+#undef T_PREAMBLE
+#undef C_PREAMBLE
diff --git a/drivers/gles_common/rasterizer_storage_common.h b/drivers/gles_common/rasterizer_storage_common.h
new file mode 100644
index 00000000000..e5750de9b9c
--- /dev/null
+++ b/drivers/gles_common/rasterizer_storage_common.h
@@ -0,0 +1,71 @@
+/*************************************************************************/
+/* rasterizer_storage_common.h */
+/*************************************************************************/
+/* This file is part of: */
+/* GODOT ENGINE */
+/* https://godotengine.org */
+/*************************************************************************/
+/* Copyright (c) 2007-2020 Juan Linietsky, Ariel Manzur. */
+/* Copyright (c) 2014-2020 Godot Engine contributors (cf. AUTHORS.md). */
+/* */
+/* Permission is hereby granted, free of charge, to any person obtaining */
+/* a copy of this software and associated documentation files (the */
+/* "Software"), to deal in the Software without restriction, including */
+/* without limitation the rights to use, copy, modify, merge, publish, */
+/* distribute, sublicense, and/or sell copies of the Software, and to */
+/* permit persons to whom the Software is furnished to do so, subject to */
+/* the following conditions: */
+/* */
+/* The above copyright notice and this permission notice shall be */
+/* included in all copies or substantial portions of the Software. */
+/* */
+/* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
+/* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
+/* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.*/
+/* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
+/* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
+/* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
+/* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
+/*************************************************************************/
+
+#pragma once
+
+class RasterizerStorageCommon {
+public:
+ enum FVF {
+ FVF_UNBATCHED,
+ FVF_REGULAR,
+ FVF_COLOR,
+ FVF_LIGHT_ANGLE,
+ FVF_MODULATED,
+ FVF_LARGE,
+ };
+
+ // these flags are specifically for batching
+ // some of the logic is thus in rasterizer_storage.cpp
+ // we could alternatively set bitflags for each 'uses' and test on the fly
+ enum BatchFlags {
+ PREVENT_COLOR_BAKING = 1 << 0,
+ PREVENT_VERTEX_BAKING = 1 << 1,
+
+ USE_MODULATE_FVF = 1 << 2,
+ USE_LARGE_FVF = 1 << 3,
+ };
+
+ enum BatchType : uint16_t {
+ BT_DEFAULT = 0,
+ BT_RECT = 1,
+ BT_LINE = 2,
+ BT_LINE_AA = 3,
+ BT_POLY = 4,
+ BT_DUMMY = 5, // dummy batch is just used to keep the batch creation loop simple
+ };
+
+ enum BatchTypeFlags {
+ BTF_DEFAULT = 1 << BT_DEFAULT,
+ BTF_RECT = 1 << BT_RECT,
+ BTF_LINE = 1 << BT_LINE,
+ BTF_LINE_AA = 1 << BT_LINE_AA,
+ BTF_POLY = 1 << BT_POLY,
+ };
+};
diff --git a/editor/plugins/canvas_item_editor_plugin.cpp b/editor/plugins/canvas_item_editor_plugin.cpp
index d9dd875bbd6..a7b05ce2c5e 100644
--- a/editor/plugins/canvas_item_editor_plugin.cpp
+++ b/editor/plugins/canvas_item_editor_plugin.cpp
@@ -53,6 +53,8 @@
#include "scene/main/viewport.h"
#include "scene/resources/packed_scene.h"
+#include
+
// Min and Max are power of two in order to play nicely with successive increment.
// That way, we can naturally reach a 100% zoom from boundaries.
#define MIN_ZOOM 1. / 128
diff --git a/platform/x11/detect.py b/platform/x11/detect.py
index 6f28f07084e..da542044cfb 100644
--- a/platform/x11/detect.py
+++ b/platform/x11/detect.py
@@ -116,6 +116,7 @@ def configure(env):
env.Prepend(CCFLAGS=["-g2"])
elif env["target"] == "debug":
+ env.Prepend(CCFLAGS=["-ggdb"])
env.Prepend(CCFLAGS=["-g3"])
env.Prepend(CPPDEFINES=["DEBUG_ENABLED"])
env.Append(LINKFLAGS=["-rdynamic"])
diff --git a/servers/visual/rasterizer.h b/servers/visual/rasterizer.h
index f1d1716fb1e..689a94acd3c 100644
--- a/servers/visual/rasterizer.h
+++ b/servers/visual/rasterizer.h
@@ -1180,6 +1180,8 @@ public:
virtual bool is_low_end() const = 0;
+ virtual const char *gl_check_for_error(bool p_print_error = true) = 0;
+
virtual ~Rasterizer() {}
};
diff --git a/servers/visual_server.cpp b/servers/visual_server.cpp
index 1293b176aca..bda411ea69c 100644
--- a/servers/visual_server.cpp
+++ b/servers/visual_server.cpp
@@ -2439,6 +2439,10 @@ VisualServer::VisualServer() {
GLOBAL_DEF(sz_balance_render_tree, 0.17f);
ProjectSettings::get_singleton()->set_custom_property_info(sz_balance_render_tree, PropertyInfo(Variant::REAL, sz_balance_render_tree, PROPERTY_HINT_RANGE, "0.0,1.0,0.01"));
+ GLOBAL_DEF("rendering/quality/2d/use_software_skinning", true);
+ GLOBAL_DEF("rendering/quality/2d/ninepatch_mode", 0);
+ ProjectSettings::get_singleton()->set_custom_property_info("rendering/quality/2d/ninepatch_mode", PropertyInfo(Variant::INT, "rendering/quality/2d/ninepatch_mode", PROPERTY_HINT_ENUM, "Default,Scaling"));
+
GLOBAL_DEF("rendering/batching/options/use_batching", true);
GLOBAL_DEF_RST("rendering/batching/options/use_batching_in_editor", true);
GLOBAL_DEF("rendering/batching/options/single_rect_fallback", false);