2023-01-10 15:26:54 +01:00
/**************************************************************************/
/* rasterizer_canvas_gles2.cpp */
/**************************************************************************/
/* This file is part of: */
/* GODOT ENGINE */
/* https://godotengine.org */
/**************************************************************************/
/* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */
/* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */
/* */
/* 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. */
/**************************************************************************/
2018-09-11 18:13:45 +02:00
2017-12-04 13:41:34 +01:00
# include "rasterizer_canvas_gles2.h"
2018-09-11 18:13:45 +02:00
# include "core/os/os.h"
# include "core/project_settings.h"
2021-06-21 11:11:33 +02:00
# include "drivers/gles2/rasterizer_gles2.h"
2020-11-22 12:24:29 +01:00
# include "drivers/gles_common/rasterizer_asserts.h"
2017-12-04 13:41:34 +01:00
# include "rasterizer_scene_gles2.h"
# include "servers/visual/visual_server_raster.h"
2018-09-11 18:13:45 +02:00
2020-03-27 10:19:37 +01:00
static const GLenum gl_primitive [ ] = {
GL_POINTS ,
GL_LINES ,
GL_LINE_STRIP ,
GL_LINE_LOOP ,
GL_TRIANGLES ,
GL_TRIANGLE_STRIP ,
GL_TRIANGLE_FAN
} ;
2017-12-04 13:41:34 +01:00
2020-10-14 09:32:05 +02:00
void RasterizerCanvasGLES2 : : _batch_upload_buffers ( ) {
// noop?
2021-05-05 12:44:11 +02:00
if ( ! bdata . vertices . size ( ) ) {
2020-10-14 09:32:05 +02:00
return ;
2021-05-05 12:44:11 +02:00
}
2020-04-20 20:32:10 +02:00
2020-10-14 09:32:05 +02:00
glBindBuffer ( GL_ARRAY_BUFFER , bdata . gl_vertex_buffer ) ;
2020-03-27 10:19:37 +01:00
2020-10-25 16:38:54 +01:00
// usage flag is a project setting
GLenum buffer_usage_flag = GL_DYNAMIC_DRAW ;
if ( bdata . buffer_mode_batch_upload_flag_stream ) {
buffer_usage_flag = GL_STREAM_DRAW ;
}
2020-10-14 09:32:05 +02:00
// orphan the old (for now)
2020-10-25 16:38:54 +01:00
if ( bdata . buffer_mode_batch_upload_send_null ) {
2021-05-04 16:00:45 +02:00
glBufferData ( GL_ARRAY_BUFFER , 0 , nullptr , buffer_usage_flag ) ; // GL_DYNAMIC_DRAW);
2020-10-25 16:38:54 +01:00
}
2017-12-04 13:41:34 +01:00
2020-10-14 09:32:05 +02:00
switch ( bdata . fvf ) {
case RasterizerStorageCommon : : FVF_UNBATCHED : // should not happen
break ;
case RasterizerStorageCommon : : FVF_REGULAR : // no change
2020-10-25 16:38:54 +01:00
glBufferData ( GL_ARRAY_BUFFER , sizeof ( BatchVertex ) * bdata . vertices . size ( ) , bdata . vertices . get_data ( ) , buffer_usage_flag ) ;
2020-10-14 09:32:05 +02:00
break ;
case RasterizerStorageCommon : : FVF_COLOR :
2020-10-25 16:38:54 +01:00
glBufferData ( GL_ARRAY_BUFFER , sizeof ( BatchVertexColored ) * bdata . unit_vertices . size ( ) , bdata . unit_vertices . get_unit ( 0 ) , buffer_usage_flag ) ;
2020-10-14 09:32:05 +02:00
break ;
case RasterizerStorageCommon : : FVF_LIGHT_ANGLE :
2020-10-25 16:38:54 +01:00
glBufferData ( GL_ARRAY_BUFFER , sizeof ( BatchVertexLightAngled ) * bdata . unit_vertices . size ( ) , bdata . unit_vertices . get_unit ( 0 ) , buffer_usage_flag ) ;
2020-10-14 09:32:05 +02:00
break ;
case RasterizerStorageCommon : : FVF_MODULATED :
2020-10-25 16:38:54 +01:00
glBufferData ( GL_ARRAY_BUFFER , sizeof ( BatchVertexModulated ) * bdata . unit_vertices . size ( ) , bdata . unit_vertices . get_unit ( 0 ) , buffer_usage_flag ) ;
2020-10-14 09:32:05 +02:00
break ;
case RasterizerStorageCommon : : FVF_LARGE :
2020-10-25 16:38:54 +01:00
glBufferData ( GL_ARRAY_BUFFER , sizeof ( BatchVertexLarge ) * bdata . unit_vertices . size ( ) , bdata . unit_vertices . get_unit ( 0 ) , buffer_usage_flag ) ;
2020-10-14 09:32:05 +02:00
break ;
2020-03-27 10:19:37 +01:00
}
2017-12-04 13:41:34 +01:00
2020-10-14 09:32:05 +02:00
// might not be necessary
glBindBuffer ( GL_ARRAY_BUFFER , 0 ) ;
2020-03-27 10:19:37 +01:00
}
2017-12-04 13:41:34 +01:00
2020-10-14 09:32:05 +02:00
void RasterizerCanvasGLES2 : : _batch_render_lines ( const Batch & p_batch , RasterizerStorageGLES2 : : Material * p_material , bool p_anti_alias ) {
_set_texture_rect_mode ( false ) ;
2018-02-24 14:48:22 +01:00
2020-10-14 09:32:05 +02:00
if ( state . canvas_shader . bind ( ) ) {
_set_uniforms ( ) ;
state . canvas_shader . use_material ( ( void * ) p_material ) ;
2019-01-14 16:03:33 +01:00
}
2017-12-04 13:41:34 +01:00
2020-10-14 09:32:05 +02:00
_bind_canvas_texture ( RID ( ) , RID ( ) ) ;
2018-02-24 14:48:22 +01:00
2020-10-14 09:32:05 +02:00
glDisableVertexAttribArray ( VS : : ARRAY_COLOR ) ;
glVertexAttrib4fv ( VS : : ARRAY_COLOR , ( float * ) & p_batch . color ) ;
2017-12-04 13:41:34 +01:00
2020-10-14 09:32:05 +02:00
# ifdef GLES_OVER_GL
2021-05-05 12:44:11 +02:00
if ( p_anti_alias ) {
2020-10-14 09:32:05 +02:00
glEnable ( GL_LINE_SMOOTH ) ;
2021-05-05 12:44:11 +02:00
}
2020-10-14 09:32:05 +02:00
# endif
2017-12-04 13:41:34 +01:00
2020-10-14 09:32:05 +02:00
int sizeof_vert = sizeof ( BatchVertex ) ;
2017-12-04 13:41:34 +01:00
2020-10-14 09:32:05 +02:00
// bind the index and vertex buffer
glBindBuffer ( GL_ARRAY_BUFFER , bdata . gl_vertex_buffer ) ;
glBindBuffer ( GL_ELEMENT_ARRAY_BUFFER , bdata . gl_index_buffer ) ;
2017-12-04 13:41:34 +01:00
2020-10-14 09:32:05 +02:00
uint64_t pointer = 0 ;
glVertexAttribPointer ( VS : : ARRAY_VERTEX , 2 , GL_FLOAT , GL_FALSE , sizeof_vert , ( const void * ) pointer ) ;
2017-12-04 13:41:34 +01:00
2020-10-14 09:32:05 +02:00
glDisableVertexAttribArray ( VS : : ARRAY_TEX_UV ) ;
2017-12-04 13:41:34 +01:00
2020-10-14 09:32:05 +02:00
int64_t offset = p_batch . first_vert ; // 6 inds per quad at 2 bytes each
2017-12-04 13:41:34 +01:00
2020-10-14 09:32:05 +02:00
int num_elements = p_batch . num_commands * 2 ;
glDrawArrays ( GL_LINES , offset , num_elements ) ;
2017-12-04 13:41:34 +01:00
2020-10-14 09:32:05 +02:00
storage - > info . render . _2d_draw_call_count + + ;
2017-12-04 13:41:34 +01:00
2020-10-14 09:32:05 +02:00
// may not be necessary .. state change optimization still TODO
2020-03-27 10:19:37 +01:00
glBindBuffer ( GL_ARRAY_BUFFER , 0 ) ;
2020-10-14 09:32:05 +02:00
glBindBuffer ( GL_ELEMENT_ARRAY_BUFFER , 0 ) ;
2017-12-04 13:41:34 +01:00
2020-10-14 09:32:05 +02:00
# ifdef GLES_OVER_GL
2021-05-05 12:44:11 +02:00
if ( p_anti_alias ) {
2020-10-14 09:32:05 +02:00
glDisable ( GL_LINE_SMOOTH ) ;
2021-05-05 12:44:11 +02:00
}
2020-03-27 10:19:37 +01:00
# endif
2017-12-04 13:41:34 +01:00
}
2020-11-12 20:16:33 +01:00
void RasterizerCanvasGLES2 : : _batch_render_generic ( const Batch & p_batch , RasterizerStorageGLES2 : : Material * p_material ) {
2020-03-27 10:19:37 +01:00
ERR_FAIL_COND ( p_batch . num_commands < = 0 ) ;
2017-12-04 13:41:34 +01:00
2020-08-15 15:31:16 +02:00
const bool & use_light_angles = bdata . use_light_angles ;
2020-10-14 09:32:05 +02:00
const bool & use_modulate = bdata . use_modulate ;
const bool & use_large_verts = bdata . use_large_verts ;
2020-11-12 20:16:33 +01:00
const bool & colored_verts = bdata . use_colored_vertices | use_light_angles | use_modulate | use_large_verts ;
2020-08-15 15:31:16 +02:00
2020-03-27 10:19:37 +01:00
int sizeof_vert ;
2020-10-14 09:32:05 +02:00
switch ( bdata . fvf ) {
default :
sizeof_vert = 0 ; // prevent compiler warning - this should never happen
break ;
2020-11-12 20:16:33 +01:00
case RasterizerStorageCommon : : FVF_UNBATCHED : {
sizeof_vert = 0 ; // prevent compiler warning - this should never happen
2020-10-14 09:32:05 +02:00
return ;
2020-11-12 20:16:33 +01:00
} break ;
2020-10-14 09:32:05 +02:00
case RasterizerStorageCommon : : FVF_REGULAR : // no change
2020-08-15 15:31:16 +02:00
sizeof_vert = sizeof ( BatchVertex ) ;
2020-10-14 09:32:05 +02:00
break ;
case RasterizerStorageCommon : : FVF_COLOR :
2020-08-15 15:31:16 +02:00
sizeof_vert = sizeof ( BatchVertexColored ) ;
2020-10-14 09:32:05 +02:00
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 ;
2018-08-20 02:10:14 +02:00
}
2017-12-04 13:41:34 +01:00
2020-07-15 10:28:16 +02:00
// make sure to set all conditionals BEFORE binding the shader
2020-10-14 09:32:05 +02:00
_set_texture_rect_mode ( false , use_light_angles , use_modulate , use_large_verts ) ;
2020-11-12 20:16:33 +01:00
// batch tex
const BatchTex & tex = bdata . batch_textures [ p_batch . batch_texture_id ] ;
2020-10-14 09:32:05 +02:00
//VSG::rasterizer->gl_check_for_error();
2017-12-04 13:41:34 +01:00
2020-07-15 10:28:16 +02:00
// 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 ) {
state . canvas_shader . set_conditional ( CanvasShaderGLES2 : : USE_FORCE_REPEAT , true ) ;
}
2020-03-27 10:19:37 +01:00
if ( state . canvas_shader . bind ( ) ) {
_set_uniforms ( ) ;
state . canvas_shader . use_material ( ( void * ) p_material ) ;
2018-08-20 02:10:14 +02:00
}
2017-12-04 13:41:34 +01:00
2020-03-27 10:19:37 +01:00
_bind_canvas_texture ( tex . RID_texture , tex . RID_normal ) ;
2017-12-04 13:41:34 +01:00
2020-03-27 10:19:37 +01:00
// 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 ) ;
2017-12-04 13:41:34 +01:00
2020-03-27 10:19:37 +01:00
// always send UVs, even within a texture specified because a shader can still use UVs
glEnableVertexAttribArray ( VS : : ARRAY_TEX_UV ) ;
2020-10-14 09:32:05 +02:00
glVertexAttribPointer ( VS : : ARRAY_TEX_UV , 2 , GL_FLOAT , GL_FALSE , sizeof_vert , CAST_INT_TO_UCHAR_PTR ( pointer + ( 2 * 4 ) ) ) ;
2017-12-04 13:41:34 +01:00
2020-03-27 10:19:37 +01:00
// color
if ( ! colored_verts ) {
glDisableVertexAttribArray ( VS : : ARRAY_COLOR ) ;
glVertexAttrib4fv ( VS : : ARRAY_COLOR , p_batch . color . get_data ( ) ) ;
} else {
2018-08-20 02:10:14 +02:00
glEnableVertexAttribArray ( VS : : ARRAY_COLOR ) ;
2020-10-14 09:32:05 +02:00
glVertexAttribPointer ( VS : : ARRAY_COLOR , 4 , GL_FLOAT , GL_FALSE , sizeof_vert , CAST_INT_TO_UCHAR_PTR ( pointer + ( 4 * 4 ) ) ) ;
2018-08-20 02:10:14 +02:00
}
2017-12-04 13:41:34 +01:00
2020-08-15 15:31:16 +02:00
if ( use_light_angles ) {
glEnableVertexAttribArray ( VS : : ARRAY_TANGENT ) ;
2020-10-14 09:32:05 +02:00
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 ) ) ) ;
2020-08-15 15:31:16 +02:00
}
2020-06-23 09:34:17 +02:00
// 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 ;
2020-07-15 10:28:16 +02:00
if ( tex . tile_mode = = 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 ) ;
}
2020-03-27 10:19:37 +01:00
}
// 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 ( CanvasShaderGLES2 : : COLOR_TEXPIXEL_SIZE , tps ) ;
2020-11-12 20:16:33 +01:00
switch ( p_batch . type ) {
default : {
// prevent compiler warning
} break ;
case RasterizerStorageCommon : : BT_RECT : {
int64_t offset = p_batch . first_vert * 3 ;
2020-03-27 10:19:37 +01:00
2020-11-12 20:16:33 +01:00
int num_elements = p_batch . num_commands * 6 ;
glDrawElements ( GL_TRIANGLES , num_elements , GL_UNSIGNED_SHORT , ( void * ) offset ) ;
} break ;
case RasterizerStorageCommon : : BT_POLY : {
int64_t offset = p_batch . first_vert ;
int num_elements = p_batch . num_commands ;
glDrawArrays ( GL_TRIANGLES , offset , num_elements ) ;
} break ;
}
2020-03-27 10:19:37 +01:00
2020-04-16 12:46:52 +02:00
storage - > info . render . _2d_draw_call_count + + ;
2020-03-27 10:19:37 +01:00
switch ( tex . tile_mode ) {
case BatchTex : : TILE_FORCE_REPEAT : {
state . canvas_shader . set_conditional ( CanvasShaderGLES2 : : USE_FORCE_REPEAT , false ) ;
} break ;
case BatchTex : : TILE_NORMAL : {
2020-06-23 09:34:17 +02:00
// 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 ) ;
}
2020-03-27 10:19:37 +01:00
} break ;
default : {
} break ;
2018-08-20 02:10:14 +02:00
}
2017-12-04 13:41:34 +01:00
2020-08-15 15:31:16 +02:00
// could these have ifs?
2020-03-27 10:19:37 +01:00
glDisableVertexAttribArray ( VS : : ARRAY_TEX_UV ) ;
glDisableVertexAttribArray ( VS : : ARRAY_COLOR ) ;
2020-08-15 15:31:16 +02:00
glDisableVertexAttribArray ( VS : : ARRAY_TANGENT ) ;
2020-10-14 09:32:05 +02:00
glDisableVertexAttribArray ( VS : : ARRAY_TEX_UV2 ) ;
glDisableVertexAttribArray ( VS : : ARRAY_BONES ) ;
glDisableVertexAttribArray ( VS : : ARRAY_WEIGHTS ) ;
2017-12-04 13:41:34 +01:00
2020-03-27 10:19:37 +01:00
// may not be necessary .. state change optimization still TODO
2018-08-20 02:10:14 +02:00
glBindBuffer ( GL_ARRAY_BUFFER , 0 ) ;
2020-03-27 10:19:37 +01:00
glBindBuffer ( GL_ELEMENT_ARRAY_BUFFER , 0 ) ;
2018-08-20 02:10:14 +02:00
}
2017-12-04 13:41:34 +01:00
2021-04-12 17:55:59 +02:00
void RasterizerCanvasGLES2 : : render_batches ( Item * p_current_clip , bool & r_reclip , RasterizerStorageGLES2 : : Material * p_material ) {
2020-03-27 10:19:37 +01:00
int num_batches = bdata . batches . size ( ) ;
2017-12-04 13:41:34 +01:00
2020-03-27 10:19:37 +01:00
for ( int batch_num = 0 ; batch_num < num_batches ; batch_num + + ) {
const Batch & batch = bdata . batches [ batch_num ] ;
2017-12-04 13:41:34 +01:00
2020-03-27 10:19:37 +01:00
switch ( batch . type ) {
2020-10-14 09:32:05 +02:00
case RasterizerStorageCommon : : BT_RECT : {
2020-11-12 20:16:33 +01:00
_batch_render_generic ( batch , p_material ) ;
2020-03-27 10:19:37 +01:00
} break ;
2020-10-14 09:32:05 +02:00
case RasterizerStorageCommon : : BT_POLY : {
2020-11-12 20:16:33 +01:00
_batch_render_generic ( batch , p_material ) ;
2020-10-14 09:32:05 +02:00
} 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 ;
2020-03-27 10:19:37 +01:00
default : {
int end_command = batch . first_command + batch . num_commands ;
2017-12-04 13:41:34 +01:00
2021-10-04 15:13:43 +02:00
DEV_ASSERT ( batch . item ) ;
2021-04-12 17:55:59 +02:00
RasterizerCanvas : : Item : : Command * const * commands = batch . item - > commands . ptr ( ) ;
2020-03-27 10:19:37 +01:00
for ( int i = batch . first_command ; i < end_command ; i + + ) {
2021-04-12 17:55:59 +02:00
Item : : Command * command = commands [ i ] ;
2017-12-04 13:41:34 +01:00
2020-03-27 10:19:37 +01:00
switch ( command - > type ) {
case Item : : Command : : TYPE_LINE : {
Item : : CommandLine * line = static_cast < Item : : CommandLine * > ( command ) ;
2017-12-04 13:41:34 +01:00
2020-08-15 15:31:16 +02:00
_set_texture_rect_mode ( false ) ;
2020-10-14 09:32:05 +02:00
2020-03-27 10:19:37 +01:00
if ( state . canvas_shader . bind ( ) ) {
_set_uniforms ( ) ;
state . canvas_shader . use_material ( ( void * ) p_material ) ;
}
_bind_canvas_texture ( RID ( ) , RID ( ) ) ;
2018-08-20 02:10:14 +02:00
2020-03-27 10:19:37 +01:00
glDisableVertexAttribArray ( VS : : ARRAY_COLOR ) ;
glVertexAttrib4fv ( VS : : ARRAY_COLOR , line - > color . components ) ;
2017-12-04 13:41:34 +01:00
2020-03-27 10:19:37 +01:00
state . canvas_shader . set_uniform ( CanvasShaderGLES2 : : MODELVIEW_MATRIX , state . uniforms . modelview_matrix ) ;
2017-12-04 13:41:34 +01:00
2020-03-27 10:19:37 +01:00
if ( line - > width < = 1 ) {
Vector2 verts [ 2 ] = {
Vector2 ( line - > from . x , line - > from . y ) ,
Vector2 ( line - > to . x , line - > to . y )
} ;
2017-12-04 13:41:34 +01:00
2019-07-12 16:48:02 +02:00
# ifdef GLES_OVER_GL
2021-05-05 12:44:11 +02:00
if ( line - > antialiased ) {
2020-03-27 10:19:37 +01:00
glEnable ( GL_LINE_SMOOTH ) ;
2021-05-05 12:44:11 +02:00
}
2019-07-12 16:48:02 +02:00
# endif
2021-05-04 16:00:45 +02:00
_draw_gui_primitive ( 2 , verts , nullptr , nullptr ) ;
2019-07-12 16:48:02 +02:00
# ifdef GLES_OVER_GL
2021-05-05 12:44:11 +02:00
if ( line - > antialiased ) {
2020-03-27 10:19:37 +01:00
glDisable ( GL_LINE_SMOOTH ) ;
2021-05-05 12:44:11 +02:00
}
2019-07-12 16:48:02 +02:00
# endif
2020-03-27 10:19:37 +01:00
} else {
Vector2 t = ( line - > from - line - > to ) . normalized ( ) . tangent ( ) * line - > width * 0.5 ;
2018-07-31 23:52:55 +02:00
2020-03-27 10:19:37 +01:00
Vector2 verts [ 4 ] = {
line - > from - t ,
line - > from + t ,
line - > to + t ,
line - > to - t
} ;
2018-07-31 23:52:55 +02:00
2021-05-04 16:00:45 +02:00
_draw_gui_primitive ( 4 , verts , nullptr , nullptr ) ;
2019-07-12 16:48:02 +02:00
# ifdef GLES_OVER_GL
2020-03-27 10:19:37 +01:00
if ( line - > antialiased ) {
glEnable ( GL_LINE_SMOOTH ) ;
for ( int j = 0 ; j < 4 ; j + + ) {
Vector2 vertsl [ 2 ] = {
verts [ j ] ,
verts [ ( j + 1 ) % 4 ] ,
} ;
2021-05-04 16:00:45 +02:00
_draw_gui_primitive ( 2 , vertsl , nullptr , nullptr ) ;
2020-03-27 10:19:37 +01:00
}
glDisable ( GL_LINE_SMOOTH ) ;
}
2019-07-12 16:48:02 +02:00
# endif
2020-03-27 10:19:37 +01:00
}
} break ;
2017-12-04 13:41:34 +01:00
2020-03-27 10:19:37 +01:00
case Item : : Command : : TYPE_RECT : {
Item : : CommandRect * r = static_cast < Item : : CommandRect * > ( command ) ;
2017-12-04 13:41:34 +01:00
2020-03-27 10:19:37 +01:00
glDisableVertexAttribArray ( VS : : ARRAY_COLOR ) ;
glVertexAttrib4fv ( VS : : ARRAY_COLOR , r - > modulate . components ) ;
2017-12-04 13:41:34 +01:00
2020-03-27 10:19:37 +01:00
bool can_tile = true ;
if ( r - > texture . is_valid ( ) & & r - > flags & CANVAS_RECT_TILE & & ! storage - > config . support_npot_repeat_mipmap ) {
// workaround for when setting tiling does not work due to hardware limitation
2019-02-25 02:34:12 +01:00
2020-03-27 10:19:37 +01:00
RasterizerStorageGLES2 : : Texture * texture = storage - > texture_owner . getornull ( r - > texture ) ;
2019-02-25 02:34:12 +01:00
2020-03-27 10:19:37 +01:00
if ( texture ) {
texture = texture - > get_ptr ( ) ;
2019-02-25 02:34:12 +01:00
2020-03-27 10:19:37 +01:00
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 ) {
state . canvas_shader . set_conditional ( CanvasShaderGLES2 : : USE_FORCE_REPEAT , true ) ;
can_tile = false ;
}
}
}
2019-02-25 02:34:12 +01:00
2020-03-27 10:19:37 +01:00
// On some widespread Nvidia cards, the normal draw method can produce some
// flickering in draw_rect and especially TileMap rendering (tiles randomly flicker).
// See GH-9913.
// To work it around, we use a simpler draw method which does not flicker, but gives
// a non negligible performance hit, so it's opt-in (GH-24466).
if ( use_nvidia_rect_workaround ) {
2020-08-15 15:31:16 +02:00
// are we using normal maps, if so we want to use light angle
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 ;
}
_set_texture_rect_mode ( false , send_light_angles ) ;
2020-03-27 10:19:37 +01:00
if ( state . canvas_shader . bind ( ) ) {
_set_uniforms ( ) ;
state . canvas_shader . use_material ( ( void * ) p_material ) ;
}
2018-12-18 21:40:28 +01:00
2020-03-27 10:19:37 +01:00
Vector2 points [ 4 ] = {
r - > rect . position ,
r - > rect . position + Vector2 ( r - > rect . size . x , 0.0 ) ,
r - > rect . position + r - > rect . size ,
r - > rect . position + Vector2 ( 0.0 , r - > rect . size . y ) ,
} ;
2018-12-18 21:40:28 +01:00
2020-03-27 10:19:37 +01:00
if ( r - > rect . size . x < 0 ) {
SWAP ( points [ 0 ] , points [ 1 ] ) ;
SWAP ( points [ 2 ] , points [ 3 ] ) ;
}
if ( r - > rect . size . y < 0 ) {
SWAP ( points [ 0 ] , points [ 3 ] ) ;
SWAP ( points [ 1 ] , points [ 2 ] ) ;
}
2018-12-18 21:40:28 +01:00
2020-03-27 10:19:37 +01:00
RasterizerStorageGLES2 : : Texture * texture = _bind_canvas_texture ( r - > texture , r - > normal_map ) ;
2018-12-18 21:40:28 +01:00
2020-03-27 10:19:37 +01:00
if ( texture ) {
Size2 texpixel_size ( 1.0 / texture - > width , 1.0 / texture - > height ) ;
2018-08-20 02:10:14 +02:00
2020-03-27 10:19:37 +01:00
Rect2 src_rect = ( r - > flags & CANVAS_RECT_REGION ) ? Rect2 ( r - > source . position * texpixel_size , r - > source . size * texpixel_size ) : Rect2 ( 0 , 0 , 1 , 1 ) ;
2018-12-18 21:40:28 +01:00
2020-03-27 10:19:37 +01:00
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 ) ,
} ;
2020-08-15 15:31:16 +02:00
// for encoding in light angle
bool flip_h = false ;
bool flip_v = false ;
2020-03-27 10:19:37 +01:00
if ( r - > flags & CANVAS_RECT_TRANSPOSE ) {
SWAP ( uvs [ 1 ] , uvs [ 3 ] ) ;
}
2017-12-04 13:41:34 +01:00
2020-03-27 10:19:37 +01:00
if ( r - > flags & CANVAS_RECT_FLIP_H ) {
SWAP ( uvs [ 0 ] , uvs [ 1 ] ) ;
SWAP ( uvs [ 2 ] , uvs [ 3 ] ) ;
2020-08-15 15:31:16 +02:00
flip_h = true ;
flip_v = ! flip_v ;
2020-03-27 10:19:37 +01:00
}
if ( r - > flags & CANVAS_RECT_FLIP_V ) {
SWAP ( uvs [ 0 ] , uvs [ 3 ] ) ;
SWAP ( uvs [ 1 ] , uvs [ 2 ] ) ;
2020-08-15 15:31:16 +02:00
flip_v = ! flip_v ;
2020-03-27 10:19:37 +01:00
}
2017-12-04 13:41:34 +01:00
2020-03-27 10:19:37 +01:00
state . canvas_shader . set_uniform ( CanvasShaderGLES2 : : COLOR_TEXPIXEL_SIZE , texpixel_size ) ;
2017-12-04 13:41:34 +01:00
2020-03-27 10:19:37 +01:00
bool untile = false ;
2017-12-04 13:41:34 +01:00
2020-03-27 10:19:37 +01:00
if ( can_tile & & r - > 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 ;
}
2017-12-04 13:41:34 +01:00
2020-08-15 15:31:16 +02:00
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 ;
2021-05-05 12:44:11 +02:00
if ( flip_h ) {
2020-08-15 15:31:16 +02:00
angle = Math_PI ;
2021-05-05 12:44:11 +02:00
}
2020-08-15 15:31:16 +02:00
// add 1 (to take care of zero floating point error with sign)
angle + = 1.0f ;
// flip if necessary
2021-05-05 12:44:11 +02:00
if ( flip_v ) {
2020-08-15 15:31:16 +02:00
angle * = - 1.0f ;
2021-05-05 12:44:11 +02:00
}
2020-08-15 15:31:16 +02:00
// 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 } ;
2021-05-04 16:00:45 +02:00
_draw_gui_primitive ( 4 , points , nullptr , uvs , light_angles ) ;
2020-08-15 15:31:16 +02:00
} else {
2021-05-04 16:00:45 +02:00
_draw_gui_primitive ( 4 , points , nullptr , uvs ) ;
2020-08-15 15:31:16 +02:00
}
2017-12-04 13:41:34 +01:00
2020-03-27 10:19:37 +01:00
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 {
static const Vector2 uvs [ 4 ] = {
Vector2 ( 0.0 , 0.0 ) ,
Vector2 ( 0.0 , 1.0 ) ,
Vector2 ( 1.0 , 1.0 ) ,
Vector2 ( 1.0 , 0.0 ) ,
} ;
state . canvas_shader . set_uniform ( CanvasShaderGLES2 : : COLOR_TEXPIXEL_SIZE , Vector2 ( ) ) ;
2021-05-04 16:00:45 +02:00
_draw_gui_primitive ( 4 , points , nullptr , uvs ) ;
2020-03-27 10:19:37 +01:00
}
2017-12-04 13:41:34 +01:00
2020-03-27 10:19:37 +01:00
} else {
// This branch is better for performance, but can produce flicker on Nvidia, see above comment.
_bind_quad_buffer ( ) ;
2017-12-04 13:41:34 +01:00
2020-08-15 15:31:16 +02:00
_set_texture_rect_mode ( true ) ;
2017-12-04 13:41:34 +01:00
2020-03-27 10:19:37 +01:00
if ( state . canvas_shader . bind ( ) ) {
_set_uniforms ( ) ;
state . canvas_shader . use_material ( ( void * ) p_material ) ;
}
2017-12-04 13:41:34 +01:00
2020-03-27 10:19:37 +01:00
RasterizerStorageGLES2 : : Texture * tex = _bind_canvas_texture ( r - > texture , r - > normal_map ) ;
2017-12-04 13:41:34 +01:00
2020-03-27 10:19:37 +01:00
if ( ! tex ) {
Rect2 dst_rect = Rect2 ( r - > rect . position , r - > rect . size ) ;
2018-08-20 02:10:14 +02:00
2020-03-27 10:19:37 +01:00
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 ;
}
2018-08-20 02:10:14 +02:00
2020-03-27 10:19:37 +01:00
state . canvas_shader . set_uniform ( CanvasShaderGLES2 : : DST_RECT , Color ( dst_rect . position . x , dst_rect . position . y , dst_rect . size . x , dst_rect . size . y ) ) ;
state . canvas_shader . set_uniform ( CanvasShaderGLES2 : : SRC_RECT , Color ( 0 , 0 , 1 , 1 ) ) ;
2017-12-04 13:41:34 +01:00
2020-03-27 10:19:37 +01:00
glDrawArrays ( GL_TRIANGLE_FAN , 0 , 4 ) ;
2020-05-07 11:16:07 +02:00
storage - > info . render . _2d_draw_call_count + + ;
2020-03-27 10:19:37 +01:00
} else {
bool untile = false ;
2017-12-04 13:41:34 +01:00
2020-03-27 10:19:37 +01:00
if ( can_tile & & r - > flags & CANVAS_RECT_TILE & & ! ( tex - > 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 ;
}
2017-12-04 13:41:34 +01:00
2020-03-27 10:19:37 +01:00
Size2 texpixel_size ( 1.0 / tex - > width , 1.0 / tex - > height ) ;
Rect2 src_rect = ( r - > flags & CANVAS_RECT_REGION ) ? Rect2 ( r - > source . position * texpixel_size , r - > source . size * texpixel_size ) : Rect2 ( 0 , 0 , 1 , 1 ) ;
2018-07-31 23:52:55 +02:00
2020-03-27 10:19:37 +01:00
Rect2 dst_rect = Rect2 ( r - > rect . position , r - > rect . size ) ;
2019-02-25 02:34:12 +01:00
2020-03-27 10:19:37 +01:00
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 ;
}
2019-02-25 02:34:12 +01:00
2020-03-27 10:19:37 +01:00
if ( r - > flags & CANVAS_RECT_FLIP_H ) {
src_rect . size . x * = - 1 ;
}
2017-12-04 13:41:34 +01:00
2020-03-27 10:19:37 +01:00
if ( r - > flags & CANVAS_RECT_FLIP_V ) {
src_rect . size . y * = - 1 ;
}
2017-12-04 13:41:34 +01:00
2020-03-27 10:19:37 +01:00
if ( r - > flags & CANVAS_RECT_TRANSPOSE ) {
dst_rect . size . x * = - 1 ; // Encoding in the dst_rect.z uniform
}
2017-12-04 13:41:34 +01:00
2020-03-27 10:19:37 +01:00
state . canvas_shader . set_uniform ( CanvasShaderGLES2 : : COLOR_TEXPIXEL_SIZE , texpixel_size ) ;
2018-08-20 02:10:14 +02:00
2020-03-27 10:19:37 +01:00
state . canvas_shader . set_uniform ( CanvasShaderGLES2 : : DST_RECT , Color ( dst_rect . position . x , dst_rect . position . y , dst_rect . size . x , dst_rect . size . y ) ) ;
state . canvas_shader . set_uniform ( CanvasShaderGLES2 : : SRC_RECT , Color ( src_rect . position . x , src_rect . position . y , src_rect . size . x , src_rect . size . y ) ) ;
2018-08-20 02:10:14 +02:00
2020-03-27 10:19:37 +01:00
glDrawArrays ( GL_TRIANGLE_FAN , 0 , 4 ) ;
2020-05-07 11:16:07 +02:00
storage - > info . render . _2d_draw_call_count + + ;
2017-12-04 13:41:34 +01:00
2020-03-27 10:19:37 +01:00
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 ) ;
}
}
2017-12-04 13:41:34 +01:00
2020-03-27 10:19:37 +01:00
glBindBuffer ( GL_ARRAY_BUFFER , 0 ) ;
glBindBuffer ( GL_ELEMENT_ARRAY_BUFFER , 0 ) ;
}
2017-12-04 13:41:34 +01:00
2020-03-27 10:19:37 +01:00
state . canvas_shader . set_conditional ( CanvasShaderGLES2 : : USE_FORCE_REPEAT , false ) ;
2017-12-04 13:41:34 +01:00
2020-03-27 10:19:37 +01:00
} break ;
2018-02-24 14:48:22 +01:00
2020-03-27 10:19:37 +01:00
case Item : : Command : : TYPE_NINEPATCH : {
Item : : CommandNinePatch * np = static_cast < Item : : CommandNinePatch * > ( command ) ;
2019-09-16 23:49:38 +02:00
2020-08-15 15:31:16 +02:00
_set_texture_rect_mode ( false ) ;
2020-03-27 10:19:37 +01:00
if ( state . canvas_shader . bind ( ) ) {
_set_uniforms ( ) ;
state . canvas_shader . use_material ( ( void * ) p_material ) ;
}
2019-09-16 23:49:38 +02:00
2020-03-27 10:19:37 +01:00
glDisableVertexAttribArray ( VS : : ARRAY_COLOR ) ;
glVertexAttrib4fv ( VS : : ARRAY_COLOR , np - > color . components ) ;
2017-12-04 13:41:34 +01:00
2020-03-27 10:19:37 +01:00
RasterizerStorageGLES2 : : Texture * tex = _bind_canvas_texture ( np - > texture , np - > normal_map ) ;
2018-02-24 14:48:22 +01:00
2020-03-27 10:19:37 +01:00
if ( ! tex ) {
// FIXME: Handle textureless ninepatch gracefully
WARN_PRINT ( " NinePatch without texture not supported yet in GLES2 backend, skipping. " ) ;
continue ;
}
if ( tex - > width = = 0 | | tex - > height = = 0 ) {
WARN_PRINT ( " Cannot set empty texture to NinePatch. " ) ;
continue ;
}
2017-12-04 13:41:34 +01:00
2020-03-27 10:19:37 +01:00
Size2 texpixel_size ( 1.0 / tex - > width , 1.0 / tex - > height ) ;
2017-12-04 13:41:34 +01:00
2020-03-27 10:19:37 +01:00
// state.canvas_shader.set_uniform(CanvasShaderGLES2::MODELVIEW_MATRIX, state.uniforms.modelview_matrix);
state . canvas_shader . set_uniform ( CanvasShaderGLES2 : : COLOR_TEXPIXEL_SIZE , texpixel_size ) ;
2017-12-04 13:41:34 +01:00
2020-03-27 10:19:37 +01:00
Rect2 source = np - > source ;
if ( source . size . x = = 0 & & source . size . y = = 0 ) {
source . size . x = tex - > width ;
source . size . y = tex - > height ;
}
2017-12-04 13:41:34 +01:00
2020-03-27 10:19:37 +01:00
float screen_scale = 1.0 ;
2017-12-04 13:41:34 +01:00
2020-10-14 09:32:05 +02:00
if ( ( bdata . settings_ninepatch_mode = = 1 ) & & ( source . size . x ! = 0 ) & & ( source . size . y ! = 0 ) ) {
2020-03-27 10:19:37 +01:00
screen_scale = MIN ( np - > rect . size . x / source . size . x , np - > rect . size . y / source . size . y ) ;
screen_scale = MIN ( 1.0 , screen_scale ) ;
}
2017-12-04 13:41:34 +01:00
2020-03-27 10:19:37 +01:00
// prepare vertex buffer
2017-12-04 13:41:34 +01:00
2020-03-27 10:19:37 +01:00
// this buffer contains [ POS POS UV UV ] *
2017-12-04 13:41:34 +01:00
2020-03-27 10:19:37 +01:00
float buffer [ 16 * 2 + 16 * 2 ] ;
2017-12-04 13:41:34 +01:00
2020-03-27 10:19:37 +01:00
{
// first row
2017-12-04 13:41:34 +01:00
2020-03-27 10:19:37 +01:00
buffer [ ( 0 * 4 * 4 ) + 0 ] = np - > rect . position . x ;
buffer [ ( 0 * 4 * 4 ) + 1 ] = np - > rect . position . y ;
2017-12-04 13:41:34 +01:00
2020-03-27 10:19:37 +01:00
buffer [ ( 0 * 4 * 4 ) + 2 ] = source . position . x * texpixel_size . x ;
buffer [ ( 0 * 4 * 4 ) + 3 ] = source . position . y * texpixel_size . y ;
2017-12-04 13:41:34 +01:00
2020-03-27 10:19:37 +01:00
buffer [ ( 0 * 4 * 4 ) + 4 ] = np - > rect . position . x + np - > margin [ MARGIN_LEFT ] * screen_scale ;
buffer [ ( 0 * 4 * 4 ) + 5 ] = np - > rect . position . y ;
2017-12-04 13:41:34 +01:00
2020-03-27 10:19:37 +01:00
buffer [ ( 0 * 4 * 4 ) + 6 ] = ( source . position . x + np - > margin [ MARGIN_LEFT ] ) * texpixel_size . x ;
buffer [ ( 0 * 4 * 4 ) + 7 ] = source . position . y * texpixel_size . y ;
2017-12-04 13:41:34 +01:00
2020-03-27 10:19:37 +01:00
buffer [ ( 0 * 4 * 4 ) + 8 ] = np - > rect . position . x + np - > rect . size . x - np - > margin [ MARGIN_RIGHT ] * screen_scale ;
buffer [ ( 0 * 4 * 4 ) + 9 ] = np - > rect . position . y ;
2017-12-04 13:41:34 +01:00
2020-03-27 10:19:37 +01:00
buffer [ ( 0 * 4 * 4 ) + 10 ] = ( source . position . x + source . size . x - np - > margin [ MARGIN_RIGHT ] ) * texpixel_size . x ;
buffer [ ( 0 * 4 * 4 ) + 11 ] = source . position . y * texpixel_size . y ;
2017-12-04 13:41:34 +01:00
2020-03-27 10:19:37 +01:00
buffer [ ( 0 * 4 * 4 ) + 12 ] = np - > rect . position . x + np - > rect . size . x ;
buffer [ ( 0 * 4 * 4 ) + 13 ] = np - > rect . position . y ;
2017-12-04 13:41:34 +01:00
2020-03-27 10:19:37 +01:00
buffer [ ( 0 * 4 * 4 ) + 14 ] = ( source . position . x + source . size . x ) * texpixel_size . x ;
buffer [ ( 0 * 4 * 4 ) + 15 ] = source . position . y * texpixel_size . y ;
2017-12-04 13:41:34 +01:00
2020-03-27 10:19:37 +01:00
// second row
2017-12-04 13:41:34 +01:00
2020-03-27 10:19:37 +01:00
buffer [ ( 1 * 4 * 4 ) + 0 ] = np - > rect . position . x ;
buffer [ ( 1 * 4 * 4 ) + 1 ] = np - > rect . position . y + np - > margin [ MARGIN_TOP ] * screen_scale ;
2017-12-04 13:41:34 +01:00
2020-03-27 10:19:37 +01:00
buffer [ ( 1 * 4 * 4 ) + 2 ] = source . position . x * texpixel_size . x ;
buffer [ ( 1 * 4 * 4 ) + 3 ] = ( source . position . y + np - > margin [ MARGIN_TOP ] ) * texpixel_size . y ;
2017-12-04 13:41:34 +01:00
2020-03-27 10:19:37 +01:00
buffer [ ( 1 * 4 * 4 ) + 4 ] = np - > rect . position . x + np - > margin [ MARGIN_LEFT ] * screen_scale ;
buffer [ ( 1 * 4 * 4 ) + 5 ] = np - > rect . position . y + np - > margin [ MARGIN_TOP ] * screen_scale ;
2017-12-04 13:41:34 +01:00
2020-03-27 10:19:37 +01:00
buffer [ ( 1 * 4 * 4 ) + 6 ] = ( source . position . x + np - > margin [ MARGIN_LEFT ] ) * texpixel_size . x ;
buffer [ ( 1 * 4 * 4 ) + 7 ] = ( source . position . y + np - > margin [ MARGIN_TOP ] ) * texpixel_size . y ;
2017-12-04 13:41:34 +01:00
2020-03-27 10:19:37 +01:00
buffer [ ( 1 * 4 * 4 ) + 8 ] = np - > rect . position . x + np - > rect . size . x - np - > margin [ MARGIN_RIGHT ] * screen_scale ;
buffer [ ( 1 * 4 * 4 ) + 9 ] = np - > rect . position . y + np - > margin [ MARGIN_TOP ] * screen_scale ;
2017-12-04 13:41:34 +01:00
2020-03-27 10:19:37 +01:00
buffer [ ( 1 * 4 * 4 ) + 10 ] = ( source . position . x + source . size . x - np - > margin [ MARGIN_RIGHT ] ) * texpixel_size . x ;
buffer [ ( 1 * 4 * 4 ) + 11 ] = ( source . position . y + np - > margin [ MARGIN_TOP ] ) * texpixel_size . y ;
2017-12-04 13:41:34 +01:00
2020-03-27 10:19:37 +01:00
buffer [ ( 1 * 4 * 4 ) + 12 ] = np - > rect . position . x + np - > rect . size . x ;
buffer [ ( 1 * 4 * 4 ) + 13 ] = np - > rect . position . y + np - > margin [ MARGIN_TOP ] * screen_scale ;
2017-12-04 13:41:34 +01:00
2020-03-27 10:19:37 +01:00
buffer [ ( 1 * 4 * 4 ) + 14 ] = ( source . position . x + source . size . x ) * texpixel_size . x ;
buffer [ ( 1 * 4 * 4 ) + 15 ] = ( source . position . y + np - > margin [ MARGIN_TOP ] ) * texpixel_size . y ;
2017-12-04 13:41:34 +01:00
2020-03-27 10:19:37 +01:00
// third row
2017-12-04 13:41:34 +01:00
2020-03-27 10:19:37 +01:00
buffer [ ( 2 * 4 * 4 ) + 0 ] = np - > rect . position . x ;
buffer [ ( 2 * 4 * 4 ) + 1 ] = np - > rect . position . y + np - > rect . size . y - np - > margin [ MARGIN_BOTTOM ] * screen_scale ;
2017-12-04 13:41:34 +01:00
2020-03-27 10:19:37 +01:00
buffer [ ( 2 * 4 * 4 ) + 2 ] = source . position . x * texpixel_size . x ;
buffer [ ( 2 * 4 * 4 ) + 3 ] = ( source . position . y + source . size . y - np - > margin [ MARGIN_BOTTOM ] ) * texpixel_size . y ;
2017-12-04 13:41:34 +01:00
2020-03-27 10:19:37 +01:00
buffer [ ( 2 * 4 * 4 ) + 4 ] = np - > rect . position . x + np - > margin [ MARGIN_LEFT ] * screen_scale ;
buffer [ ( 2 * 4 * 4 ) + 5 ] = np - > rect . position . y + np - > rect . size . y - np - > margin [ MARGIN_BOTTOM ] * screen_scale ;
2017-12-04 13:41:34 +01:00
2020-03-27 10:19:37 +01:00
buffer [ ( 2 * 4 * 4 ) + 6 ] = ( source . position . x + np - > margin [ MARGIN_LEFT ] ) * texpixel_size . x ;
buffer [ ( 2 * 4 * 4 ) + 7 ] = ( source . position . y + source . size . y - np - > margin [ MARGIN_BOTTOM ] ) * texpixel_size . y ;
2017-12-04 13:41:34 +01:00
2020-03-27 10:19:37 +01:00
buffer [ ( 2 * 4 * 4 ) + 8 ] = np - > rect . position . x + np - > rect . size . x - np - > margin [ MARGIN_RIGHT ] * screen_scale ;
buffer [ ( 2 * 4 * 4 ) + 9 ] = np - > rect . position . y + np - > rect . size . y - np - > margin [ MARGIN_BOTTOM ] * screen_scale ;
2017-12-04 13:41:34 +01:00
2020-03-27 10:19:37 +01:00
buffer [ ( 2 * 4 * 4 ) + 10 ] = ( source . position . x + source . size . x - np - > margin [ MARGIN_RIGHT ] ) * texpixel_size . x ;
buffer [ ( 2 * 4 * 4 ) + 11 ] = ( source . position . y + source . size . y - np - > margin [ MARGIN_BOTTOM ] ) * texpixel_size . y ;
2017-12-04 13:41:34 +01:00
2020-03-27 10:19:37 +01:00
buffer [ ( 2 * 4 * 4 ) + 12 ] = np - > rect . position . x + np - > rect . size . x ;
buffer [ ( 2 * 4 * 4 ) + 13 ] = np - > rect . position . y + np - > rect . size . y - np - > margin [ MARGIN_BOTTOM ] * screen_scale ;
2017-12-04 13:41:34 +01:00
2020-03-27 10:19:37 +01:00
buffer [ ( 2 * 4 * 4 ) + 14 ] = ( source . position . x + source . size . x ) * texpixel_size . x ;
buffer [ ( 2 * 4 * 4 ) + 15 ] = ( source . position . y + source . size . y - np - > margin [ MARGIN_BOTTOM ] ) * texpixel_size . y ;
2017-12-04 13:41:34 +01:00
2020-03-27 10:19:37 +01:00
// fourth row
2017-12-04 13:41:34 +01:00
2020-03-27 10:19:37 +01:00
buffer [ ( 3 * 4 * 4 ) + 0 ] = np - > rect . position . x ;
buffer [ ( 3 * 4 * 4 ) + 1 ] = np - > rect . position . y + np - > rect . size . y ;
2017-12-04 13:41:34 +01:00
2020-03-27 10:19:37 +01:00
buffer [ ( 3 * 4 * 4 ) + 2 ] = source . position . x * texpixel_size . x ;
buffer [ ( 3 * 4 * 4 ) + 3 ] = ( source . position . y + source . size . y ) * texpixel_size . y ;
2017-12-04 13:41:34 +01:00
2020-03-27 10:19:37 +01:00
buffer [ ( 3 * 4 * 4 ) + 4 ] = np - > rect . position . x + np - > margin [ MARGIN_LEFT ] * screen_scale ;
buffer [ ( 3 * 4 * 4 ) + 5 ] = np - > rect . position . y + np - > rect . size . y ;
2017-12-04 13:41:34 +01:00
2020-03-27 10:19:37 +01:00
buffer [ ( 3 * 4 * 4 ) + 6 ] = ( source . position . x + np - > margin [ MARGIN_LEFT ] ) * texpixel_size . x ;
buffer [ ( 3 * 4 * 4 ) + 7 ] = ( source . position . y + source . size . y ) * texpixel_size . y ;
2017-12-04 13:41:34 +01:00
2020-03-27 10:19:37 +01:00
buffer [ ( 3 * 4 * 4 ) + 8 ] = np - > rect . position . x + np - > rect . size . x - np - > margin [ MARGIN_RIGHT ] * screen_scale ;
buffer [ ( 3 * 4 * 4 ) + 9 ] = np - > rect . position . y + np - > rect . size . y ;
2017-12-04 13:41:34 +01:00
2020-03-27 10:19:37 +01:00
buffer [ ( 3 * 4 * 4 ) + 10 ] = ( source . position . x + source . size . x - np - > margin [ MARGIN_RIGHT ] ) * texpixel_size . x ;
buffer [ ( 3 * 4 * 4 ) + 11 ] = ( source . position . y + source . size . y ) * texpixel_size . y ;
2017-12-04 13:41:34 +01:00
2020-03-27 10:19:37 +01:00
buffer [ ( 3 * 4 * 4 ) + 12 ] = np - > rect . position . x + np - > rect . size . x ;
buffer [ ( 3 * 4 * 4 ) + 13 ] = np - > rect . position . y + np - > rect . size . y ;
2017-12-04 13:41:34 +01:00
2020-03-27 10:19:37 +01:00
buffer [ ( 3 * 4 * 4 ) + 14 ] = ( source . position . x + source . size . x ) * texpixel_size . x ;
buffer [ ( 3 * 4 * 4 ) + 15 ] = ( source . position . y + source . size . y ) * texpixel_size . y ;
}
2017-12-04 13:41:34 +01:00
2020-03-27 10:19:37 +01:00
glBindBuffer ( GL_ARRAY_BUFFER , data . ninepatch_vertices ) ;
2020-10-25 16:38:54 +01:00
glBufferData ( GL_ARRAY_BUFFER , sizeof ( float ) * ( 16 + 16 ) * 2 , buffer , _buffer_upload_usage_flag ) ;
2017-12-04 13:41:34 +01:00
2020-03-27 10:19:37 +01:00
glBindBuffer ( GL_ELEMENT_ARRAY_BUFFER , data . ninepatch_elements ) ;
2017-12-04 13:41:34 +01:00
2020-03-27 10:19:37 +01:00
glEnableVertexAttribArray ( VS : : ARRAY_VERTEX ) ;
glEnableVertexAttribArray ( VS : : ARRAY_TEX_UV ) ;
2017-12-04 13:41:34 +01:00
2021-05-04 16:00:45 +02:00
glVertexAttribPointer ( VS : : ARRAY_VERTEX , 2 , GL_FLOAT , GL_FALSE , 4 * sizeof ( float ) , nullptr ) ;
2020-03-27 10:19:37 +01:00
glVertexAttribPointer ( VS : : ARRAY_TEX_UV , 2 , GL_FLOAT , GL_FALSE , 4 * sizeof ( float ) , CAST_INT_TO_UCHAR_PTR ( ( sizeof ( float ) * 2 ) ) ) ;
2017-12-04 13:41:34 +01:00
2021-05-04 16:00:45 +02:00
glDrawElements ( GL_TRIANGLES , 18 * 3 - ( np - > draw_center ? 0 : 6 ) , GL_UNSIGNED_BYTE , nullptr ) ;
2017-12-04 13:41:34 +01:00
2020-03-27 10:19:37 +01:00
glBindBuffer ( GL_ARRAY_BUFFER , 0 ) ;
glBindBuffer ( GL_ELEMENT_ARRAY_BUFFER , 0 ) ;
2020-04-16 12:46:52 +02:00
storage - > info . render . _2d_draw_call_count + + ;
2017-12-04 13:41:34 +01:00
2020-03-27 10:19:37 +01:00
} break ;
2017-12-04 13:41:34 +01:00
2020-03-27 10:19:37 +01:00
case Item : : Command : : TYPE_CIRCLE : {
Item : : CommandCircle * circle = static_cast < Item : : CommandCircle * > ( command ) ;
2017-12-04 13:41:34 +01:00
2020-08-15 15:31:16 +02:00
_set_texture_rect_mode ( false ) ;
2017-12-04 13:41:34 +01:00
2020-03-27 10:19:37 +01:00
if ( state . canvas_shader . bind ( ) ) {
_set_uniforms ( ) ;
state . canvas_shader . use_material ( ( void * ) p_material ) ;
}
2017-12-04 13:41:34 +01:00
2020-03-27 10:19:37 +01:00
static const int num_points = 32 ;
2017-12-04 13:41:34 +01:00
2020-03-27 10:19:37 +01:00
Vector2 points [ num_points + 1 ] ;
points [ num_points ] = circle - > pos ;
2017-12-04 13:41:34 +01:00
2020-03-27 10:19:37 +01:00
int indices [ num_points * 3 ] ;
2017-12-04 13:41:34 +01:00
2020-03-27 10:19:37 +01:00
for ( int j = 0 ; j < num_points ; j + + ) {
points [ j ] = circle - > pos + Vector2 ( Math : : sin ( j * Math_PI * 2.0 / num_points ) , Math : : cos ( j * Math_PI * 2.0 / num_points ) ) * circle - > radius ;
indices [ j * 3 + 0 ] = j ;
indices [ j * 3 + 1 ] = ( j + 1 ) % num_points ;
indices [ j * 3 + 2 ] = num_points ;
}
2018-12-28 00:39:00 +01:00
2020-03-27 10:19:37 +01:00
_bind_canvas_texture ( RID ( ) , RID ( ) ) ;
2018-12-28 00:39:00 +01:00
2021-05-04 16:00:45 +02:00
_draw_polygon ( indices , num_points * 3 , num_points + 1 , points , nullptr , & circle - > color , true ) ;
2020-03-27 10:19:37 +01:00
} break ;
2018-12-28 00:39:00 +01:00
2020-03-27 10:19:37 +01:00
case Item : : Command : : TYPE_POLYGON : {
Item : : CommandPolygon * polygon = static_cast < Item : : CommandPolygon * > ( command ) ;
2018-12-28 00:39:00 +01:00
2020-08-15 15:31:16 +02:00
_set_texture_rect_mode ( false ) ;
2018-12-28 00:39:00 +01:00
2020-03-27 10:19:37 +01:00
if ( state . canvas_shader . bind ( ) ) {
_set_uniforms ( ) ;
state . canvas_shader . use_material ( ( void * ) p_material ) ;
}
2018-12-28 00:39:00 +01:00
2020-03-27 10:19:37 +01:00
RasterizerStorageGLES2 : : Texture * texture = _bind_canvas_texture ( polygon - > texture , polygon - > normal_map ) ;
2018-12-28 00:39:00 +01:00
2020-03-27 10:19:37 +01:00
if ( texture ) {
Size2 texpixel_size ( 1.0 / texture - > width , 1.0 / texture - > height ) ;
state . canvas_shader . set_uniform ( CanvasShaderGLES2 : : COLOR_TEXPIXEL_SIZE , texpixel_size ) ;
}
2018-12-28 00:39:00 +01:00
2020-03-27 10:19:37 +01:00
_draw_polygon ( polygon - > indices . ptr ( ) , polygon - > count , polygon - > points . size ( ) , polygon - > points . ptr ( ) , polygon - > uvs . ptr ( ) , polygon - > colors . ptr ( ) , polygon - > colors . size ( ) = = 1 , polygon - > weights . ptr ( ) , polygon - > bones . 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 ) ;
2018-12-28 00:39:00 +01:00
}
2020-03-27 10:19:37 +01:00
glDisable ( GL_LINE_SMOOTH ) ;
2018-12-28 00:39:00 +01:00
}
2020-03-27 10:19:37 +01:00
# endif
} break ;
case Item : : Command : : TYPE_MESH : {
Item : : CommandMesh * mesh = static_cast < Item : : CommandMesh * > ( command ) ;
2020-10-14 09:32:05 +02:00
2020-08-15 15:31:16 +02:00
_set_texture_rect_mode ( false ) ;
2018-12-28 00:39:00 +01:00
2020-03-27 10:19:37 +01:00
if ( state . canvas_shader . bind ( ) ) {
_set_uniforms ( ) ;
state . canvas_shader . use_material ( ( void * ) p_material ) ;
}
2018-12-28 00:39:00 +01:00
2020-03-27 10:19:37 +01:00
RasterizerStorageGLES2 : : Texture * texture = _bind_canvas_texture ( mesh - > texture , mesh - > normal_map ) ;
2018-12-28 00:39:00 +01:00
2020-03-27 10:19:37 +01:00
if ( texture ) {
Size2 texpixel_size ( 1.0 / texture - > width , 1.0 / texture - > height ) ;
state . canvas_shader . set_uniform ( CanvasShaderGLES2 : : COLOR_TEXPIXEL_SIZE , texpixel_size ) ;
}
2018-12-28 00:39:00 +01:00
2020-03-27 10:19:37 +01:00
RasterizerStorageGLES2 : : Mesh * mesh_data = storage - > mesh_owner . getornull ( mesh - > mesh ) ;
if ( mesh_data ) {
for ( int j = 0 ; j < mesh_data - > surfaces . size ( ) ; j + + ) {
RasterizerStorageGLES2 : : 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
2018-12-28 00:39:00 +01:00
2020-03-27 10:19:37 +01:00
glBindBuffer ( GL_ARRAY_BUFFER , s - > vertex_id ) ;
2018-12-28 00:39:00 +01:00
2020-03-27 10:19:37 +01:00
if ( s - > index_array_len > 0 ) {
glBindBuffer ( GL_ELEMENT_ARRAY_BUFFER , s - > index_id ) ;
}
2018-12-28 00:39:00 +01:00
2020-03-27 10:19:37 +01:00
for ( int k = 0 ; k < VS : : ARRAY_MAX - 1 ; k + + ) {
if ( s - > attribs [ k ] . enabled ) {
glEnableVertexAttribArray ( k ) ;
glVertexAttribPointer ( s - > attribs [ k ] . index , s - > attribs [ k ] . size , s - > attribs [ k ] . type , s - > attribs [ k ] . normalized , s - > attribs [ k ] . stride , CAST_INT_TO_UCHAR_PTR ( s - > attribs [ k ] . offset ) ) ;
} else {
glDisableVertexAttribArray ( k ) ;
switch ( k ) {
case VS : : ARRAY_NORMAL : {
glVertexAttrib4f ( VS : : ARRAY_NORMAL , 0.0 , 0.0 , 1 , 1 ) ;
} break ;
case VS : : ARRAY_COLOR : {
glVertexAttrib4f ( VS : : ARRAY_COLOR , 1 , 1 , 1 , 1 ) ;
} break ;
default : {
}
}
}
}
2017-12-04 13:41:34 +01:00
2020-03-27 10:19:37 +01:00
if ( s - > index_array_len > 0 ) {
2021-05-04 16:00:45 +02:00
glDrawElements ( gl_primitive [ s - > primitive ] , s - > index_array_len , ( s - > array_len > = ( 1 < < 16 ) ) ? GL_UNSIGNED_INT : GL_UNSIGNED_SHORT , nullptr ) ;
2020-03-27 10:19:37 +01:00
} else {
glDrawArrays ( gl_primitive [ s - > primitive ] , 0 , s - > array_len ) ;
}
}
2018-12-28 00:39:00 +01:00
2020-03-27 10:19:37 +01:00
for ( int j = 1 ; j < VS : : ARRAY_MAX - 1 ; j + + ) {
glDisableVertexAttribArray ( j ) ;
}
}
2018-12-28 00:39:00 +01:00
2020-04-16 12:46:52 +02:00
storage - > info . render . _2d_draw_call_count + + ;
2020-03-27 10:19:37 +01:00
} break ;
case Item : : Command : : TYPE_MULTIMESH : {
Item : : CommandMultiMesh * mmesh = static_cast < Item : : CommandMultiMesh * > ( command ) ;
2018-12-28 00:39:00 +01:00
2020-03-27 10:19:37 +01:00
RasterizerStorageGLES2 : : MultiMesh * multi_mesh = storage - > multimesh_owner . getornull ( mmesh - > multimesh ) ;
2018-12-28 00:39:00 +01:00
2021-05-05 12:44:11 +02:00
if ( ! multi_mesh ) {
2020-03-27 10:19:37 +01:00
break ;
2021-05-05 12:44:11 +02:00
}
2020-03-27 10:19:37 +01:00
RasterizerStorageGLES2 : : Mesh * mesh_data = storage - > mesh_owner . getornull ( multi_mesh - > mesh ) ;
2018-12-28 00:39:00 +01:00
2021-05-05 12:44:11 +02:00
if ( ! mesh_data ) {
2020-03-27 10:19:37 +01:00
break ;
2021-05-05 12:44:11 +02:00
}
2018-12-28 00:39:00 +01:00
2022-09-15 14:20:12 +02:00
int amount = MIN ( multi_mesh - > size , multi_mesh - > visible_instances ) ;
if ( amount = = - 1 ) {
amount = multi_mesh - > size ;
}
if ( ! amount ) {
break ;
}
2020-03-27 10:19:37 +01:00
state . canvas_shader . set_conditional ( CanvasShaderGLES2 : : USE_INSTANCE_CUSTOM , multi_mesh - > custom_data_format ! = VS : : MULTIMESH_CUSTOM_DATA_NONE ) ;
state . canvas_shader . set_conditional ( CanvasShaderGLES2 : : USE_INSTANCING , true ) ;
2020-08-15 15:31:16 +02:00
_set_texture_rect_mode ( false ) ;
2018-12-28 00:39:00 +01:00
2020-03-27 10:19:37 +01:00
if ( state . canvas_shader . bind ( ) ) {
_set_uniforms ( ) ;
state . canvas_shader . use_material ( ( void * ) p_material ) ;
}
2018-12-28 00:39:00 +01:00
2020-03-27 10:19:37 +01:00
RasterizerStorageGLES2 : : Texture * texture = _bind_canvas_texture ( mmesh - > texture , mmesh - > normal_map ) ;
2018-12-28 00:39:00 +01:00
2020-03-27 10:19:37 +01:00
if ( texture ) {
Size2 texpixel_size ( 1.0 / texture - > width , 1.0 / texture - > height ) ;
state . canvas_shader . set_uniform ( CanvasShaderGLES2 : : COLOR_TEXPIXEL_SIZE , texpixel_size ) ;
}
2018-12-28 00:39:00 +01:00
2020-03-27 10:19:37 +01:00
//reset shader and force rebind
2018-12-28 00:39:00 +01:00
2020-03-27 10:19:37 +01:00
int stride = multi_mesh - > color_floats + multi_mesh - > custom_data_floats + multi_mesh - > xform_floats ;
2018-12-28 00:39:00 +01:00
2020-03-27 10:19:37 +01:00
int color_ofs = multi_mesh - > xform_floats ;
int custom_data_ofs = color_ofs + multi_mesh - > color_floats ;
2018-12-28 00:39:00 +01:00
2020-03-27 10:19:37 +01:00
// drawing
2018-12-28 00:39:00 +01:00
2020-03-27 10:19:37 +01:00
const float * base_buffer = multi_mesh - > data . ptr ( ) ;
2018-12-28 00:39:00 +01:00
2020-03-27 10:19:37 +01:00
for ( int j = 0 ; j < mesh_data - > surfaces . size ( ) ; j + + ) {
RasterizerStorageGLES2 : : 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
2018-12-28 00:39:00 +01:00
2020-03-27 10:19:37 +01:00
//bind buffers for mesh surface
glBindBuffer ( GL_ARRAY_BUFFER , s - > vertex_id ) ;
2018-12-28 00:39:00 +01:00
2020-03-27 10:19:37 +01:00
if ( s - > index_array_len > 0 ) {
glBindBuffer ( GL_ELEMENT_ARRAY_BUFFER , s - > index_id ) ;
}
2018-07-23 09:07:32 +02:00
2020-03-27 10:19:37 +01:00
for ( int k = 0 ; k < VS : : ARRAY_MAX - 1 ; k + + ) {
if ( s - > attribs [ k ] . enabled ) {
glEnableVertexAttribArray ( k ) ;
glVertexAttribPointer ( s - > attribs [ k ] . index , s - > attribs [ k ] . size , s - > attribs [ k ] . type , s - > attribs [ k ] . normalized , s - > attribs [ k ] . stride , CAST_INT_TO_UCHAR_PTR ( s - > attribs [ k ] . offset ) ) ;
} else {
glDisableVertexAttribArray ( k ) ;
switch ( k ) {
case VS : : ARRAY_NORMAL : {
glVertexAttrib4f ( VS : : ARRAY_NORMAL , 0.0 , 0.0 , 1 , 1 ) ;
} break ;
case VS : : ARRAY_COLOR : {
glVertexAttrib4f ( VS : : ARRAY_COLOR , 1 , 1 , 1 , 1 ) ;
} break ;
default : {
}
}
}
}
2018-07-31 23:52:55 +02:00
2020-03-27 10:19:37 +01:00
for ( int k = 0 ; k < amount ; k + + ) {
const float * buffer = base_buffer + k * stride ;
{
glVertexAttrib4fv ( INSTANCE_ATTRIB_BASE + 0 , & buffer [ 0 ] ) ;
glVertexAttrib4fv ( INSTANCE_ATTRIB_BASE + 1 , & buffer [ 4 ] ) ;
if ( multi_mesh - > transform_format = = VS : : MULTIMESH_TRANSFORM_3D ) {
glVertexAttrib4fv ( INSTANCE_ATTRIB_BASE + 2 , & buffer [ 8 ] ) ;
} else {
glVertexAttrib4f ( INSTANCE_ATTRIB_BASE + 2 , 0.0 , 0.0 , 1.0 , 0.0 ) ;
}
}
2018-07-31 23:52:55 +02:00
2020-03-27 10:19:37 +01:00
if ( multi_mesh - > color_floats ) {
if ( multi_mesh - > color_format = = VS : : MULTIMESH_COLOR_8BIT ) {
uint8_t * color_data = ( uint8_t * ) ( buffer + color_ofs ) ;
glVertexAttrib4f ( INSTANCE_ATTRIB_BASE + 3 , color_data [ 0 ] / 255.0 , color_data [ 1 ] / 255.0 , color_data [ 2 ] / 255.0 , color_data [ 3 ] / 255.0 ) ;
} else {
glVertexAttrib4fv ( INSTANCE_ATTRIB_BASE + 3 , buffer + color_ofs ) ;
}
} else {
glVertexAttrib4f ( INSTANCE_ATTRIB_BASE + 3 , 1.0 , 1.0 , 1.0 , 1.0 ) ;
}
if ( multi_mesh - > custom_data_floats ) {
if ( multi_mesh - > custom_data_format = = VS : : MULTIMESH_CUSTOM_DATA_8BIT ) {
uint8_t * custom_data = ( uint8_t * ) ( buffer + custom_data_ofs ) ;
glVertexAttrib4f ( INSTANCE_ATTRIB_BASE + 4 , custom_data [ 0 ] / 255.0 , custom_data [ 1 ] / 255.0 , custom_data [ 2 ] / 255.0 , custom_data [ 3 ] / 255.0 ) ;
} else {
glVertexAttrib4fv ( INSTANCE_ATTRIB_BASE + 4 , buffer + custom_data_ofs ) ;
}
}
if ( s - > index_array_len > 0 ) {
2021-05-04 16:00:45 +02:00
glDrawElements ( gl_primitive [ s - > primitive ] , s - > index_array_len , ( s - > array_len > = ( 1 < < 16 ) ) ? GL_UNSIGNED_INT : GL_UNSIGNED_SHORT , nullptr ) ;
2020-03-27 10:19:37 +01:00
} else {
glDrawArrays ( gl_primitive [ s - > primitive ] , 0 , s - > array_len ) ;
}
}
}
2018-07-31 23:52:55 +02:00
2020-10-14 09:32:05 +02:00
// 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 ) ;
2020-03-27 10:19:37 +01:00
state . canvas_shader . set_conditional ( CanvasShaderGLES2 : : USE_INSTANCING , false ) ;
2020-04-16 12:46:52 +02:00
storage - > info . render . _2d_draw_call_count + + ;
2020-03-27 10:19:37 +01:00
} break ;
case Item : : Command : : TYPE_POLYLINE : {
Item : : CommandPolyLine * pline = static_cast < Item : : CommandPolyLine * > ( command ) ;
2020-10-14 09:32:05 +02:00
_set_texture_rect_mode ( false ) ;
2020-03-27 10:19:37 +01:00
if ( state . canvas_shader . bind ( ) ) {
_set_uniforms ( ) ;
state . canvas_shader . use_material ( ( void * ) p_material ) ;
}
_bind_canvas_texture ( RID ( ) , RID ( ) ) ;
if ( pline - > triangles . size ( ) ) {
2021-05-04 16:00:45 +02:00
_draw_generic ( GL_TRIANGLE_STRIP , pline - > triangles . size ( ) , pline - > triangles . ptr ( ) , nullptr , pline - > triangle_colors . ptr ( ) , pline - > triangle_colors . size ( ) = = 1 ) ;
2019-07-12 16:48:02 +02:00
# ifdef GLES_OVER_GL
2020-03-27 10:19:37 +01:00
glEnable ( GL_LINE_SMOOTH ) ;
if ( pline - > multiline ) {
//needs to be different
} else {
2021-05-04 16:00:45 +02:00
_draw_generic ( GL_LINE_LOOP , pline - > lines . size ( ) , pline - > lines . ptr ( ) , nullptr , pline - > line_colors . ptr ( ) , pline - > line_colors . size ( ) = = 1 ) ;
2020-03-27 10:19:37 +01:00
}
glDisable ( GL_LINE_SMOOTH ) ;
2019-07-12 16:48:02 +02:00
# endif
2020-03-27 10:19:37 +01:00
} else {
2019-07-12 16:48:02 +02:00
# ifdef GLES_OVER_GL
2021-05-05 12:44:11 +02:00
if ( pline - > antialiased ) {
2020-03-27 10:19:37 +01:00
glEnable ( GL_LINE_SMOOTH ) ;
2021-05-05 12:44:11 +02:00
}
2019-07-12 16:48:02 +02:00
# endif
2020-03-27 10:19:37 +01:00
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 ) ;
2021-05-04 16:00:45 +02:00
_draw_generic ( GL_LINES , to_draw * 2 , & pline - > lines . ptr ( ) [ offset ] , nullptr , pline - > line_colors . size ( ) = = 1 ? pline - > line_colors . ptr ( ) : & pline - > line_colors . ptr ( ) [ offset ] , pline - > line_colors . size ( ) = = 1 ) ;
2020-03-27 10:19:37 +01:00
todo - = to_draw ;
offset + = to_draw * 2 ;
}
} else {
2021-05-04 16:00:45 +02:00
_draw_generic ( GL_LINE_STRIP , pline - > lines . size ( ) , pline - > lines . ptr ( ) , nullptr , pline - > line_colors . ptr ( ) , pline - > line_colors . size ( ) = = 1 ) ;
2020-03-27 10:19:37 +01:00
}
2019-07-12 16:48:02 +02:00
# ifdef GLES_OVER_GL
2021-05-05 12:44:11 +02:00
if ( pline - > antialiased ) {
2020-03-27 10:19:37 +01:00
glDisable ( GL_LINE_SMOOTH ) ;
2021-05-05 12:44:11 +02:00
}
2019-07-12 16:48:02 +02:00
# endif
2020-03-27 10:19:37 +01:00
}
} break ;
2017-12-04 13:41:34 +01:00
2020-03-27 10:19:37 +01:00
case Item : : Command : : TYPE_PRIMITIVE : {
Item : : CommandPrimitive * primitive = static_cast < Item : : CommandPrimitive * > ( command ) ;
2020-08-15 15:31:16 +02:00
_set_texture_rect_mode ( false ) ;
2017-12-04 13:41:34 +01:00
2020-03-27 10:19:37 +01:00
if ( state . canvas_shader . bind ( ) ) {
_set_uniforms ( ) ;
state . canvas_shader . use_material ( ( void * ) p_material ) ;
}
2017-12-04 13:41:34 +01:00
2020-03-27 10:19:37 +01:00
ERR_CONTINUE ( primitive - > points . size ( ) < 1 ) ;
2017-12-04 13:41:34 +01:00
2020-03-27 10:19:37 +01:00
RasterizerStorageGLES2 : : Texture * texture = _bind_canvas_texture ( primitive - > texture , primitive - > normal_map ) ;
2017-12-04 13:41:34 +01:00
2020-03-27 10:19:37 +01:00
if ( texture ) {
Size2 texpixel_size ( 1.0 / texture - > width , 1.0 / texture - > height ) ;
state . canvas_shader . set_uniform ( CanvasShaderGLES2 : : COLOR_TEXPIXEL_SIZE , texpixel_size ) ;
}
2017-12-04 13:41:34 +01:00
2020-11-22 12:24:29 +01:00
// we need a temporary because this must be nulled out
// if only a single color specified
const Color * colors = primitive - > colors . ptr ( ) ;
2020-03-27 10:19:37 +01:00
if ( primitive - > colors . size ( ) = = 1 & & primitive - > points . size ( ) > 1 ) {
Color c = primitive - > colors [ 0 ] ;
glVertexAttrib4f ( VS : : ARRAY_COLOR , c . r , c . g , c . b , c . a ) ;
2020-11-22 12:24:29 +01:00
colors = nullptr ;
2020-03-27 10:19:37 +01:00
} else if ( primitive - > colors . empty ( ) ) {
glVertexAttrib4f ( VS : : ARRAY_COLOR , 1 , 1 , 1 , 1 ) ;
}
2020-11-22 12:24:29 +01:00
# ifdef RASTERIZER_EXTRA_CHECKS
else {
RAST_DEV_DEBUG_ASSERT ( primitive - > colors . size ( ) = = primitive - > points . size ( ) ) ;
}
if ( primitive - > uvs . ptr ( ) ) {
RAST_DEV_DEBUG_ASSERT ( primitive - > uvs . size ( ) = = primitive - > points . size ( ) ) ;
}
# endif
2017-12-04 13:41:34 +01:00
2020-11-22 12:24:29 +01:00
_draw_gui_primitive ( primitive - > points . size ( ) , primitive - > points . ptr ( ) , colors , primitive - > uvs . ptr ( ) ) ;
2020-03-27 10:19:37 +01:00
} break ;
2017-12-04 13:41:34 +01:00
2020-03-27 10:19:37 +01:00
case Item : : Command : : TYPE_TRANSFORM : {
Item : : CommandTransform * transform = static_cast < Item : : CommandTransform * > ( command ) ;
state . uniforms . extra_matrix = transform - > xform ;
state . canvas_shader . set_uniform ( CanvasShaderGLES2 : : EXTRA_MATRIX , state . uniforms . extra_matrix ) ;
} break ;
2017-12-04 13:41:34 +01:00
2020-03-27 10:19:37 +01:00
case Item : : Command : : TYPE_PARTICLES : {
} break ;
case Item : : Command : : TYPE_CLIP_IGNORE : {
Item : : CommandClipIgnore * ci = static_cast < Item : : CommandClipIgnore * > ( command ) ;
if ( p_current_clip ) {
if ( ci - > ignore ! = r_reclip ) {
if ( ci - > ignore ) {
glDisable ( GL_SCISSOR_TEST ) ;
r_reclip = true ;
} else {
glEnable ( GL_SCISSOR_TEST ) ;
2017-12-04 13:41:34 +01:00
2020-03-27 10:19:37 +01:00
int x = p_current_clip - > final_clip_rect . position . x ;
int y = storage - > frame . current_rt - > height - ( p_current_clip - > final_clip_rect . position . y + p_current_clip - > final_clip_rect . size . y ) ;
int w = p_current_clip - > final_clip_rect . size . x ;
int h = p_current_clip - > final_clip_rect . size . y ;
2017-12-04 13:41:34 +01:00
2021-05-05 12:44:11 +02:00
if ( storage - > frame . current_rt - > flags [ RasterizerStorage : : RENDER_TARGET_VFLIP ] ) {
2020-03-27 10:19:37 +01:00
y = p_current_clip - > final_clip_rect . position . y ;
2021-05-05 12:44:11 +02:00
}
2017-12-04 13:41:34 +01:00
2020-03-27 10:19:37 +01:00
glScissor ( x , y , w , h ) ;
2017-12-04 13:41:34 +01:00
2020-03-27 10:19:37 +01:00
r_reclip = false ;
}
}
}
2018-06-16 22:55:21 +02:00
2020-03-27 10:19:37 +01:00
} break ;
2017-12-04 13:41:34 +01:00
2020-03-27 10:19:37 +01:00
default : {
// FIXME: Proper error handling if relevant
//print_line("other");
} break ;
2017-12-04 13:41:34 +01:00
}
}
2020-03-27 10:19:37 +01:00
} // default
break ;
2017-12-04 13:41:34 +01:00
}
}
}
GLES2 2D batching - item reordering, light joining and light modulate fix
Although 2D draws in painters order with strict ordering, in certain circumstances items can be reordered to increase batching / decrease state changes, without affecting the end result. This can be determined by an overlap test.
In situation with item:
A-B-A
providing the third item does not overlap the second, they can be reordered:
A-A-B
Items already contain an AABB which can be used for this overlap test.
1)
To utilise this, I have implemented item reordering (only for single rects for now), with the lookahead adjustable in project settings. This can increase performance in situations where items may not be grouped in the scene tree by texture. It can also be switched off (by setting lookahead to 0).
2)
This same trick can be used to help join items that are lit. Lit items previously would prevent joining completely, thus missing out on performance gains other than multi-command items such as tilemaps.
In this PR, lights are assigned as bits in a bitfield (up to 64, the optimization is disabled above this), and on each try_item (for joining), the bitfield for lights and shadows is constructed and compared with the previous items. If these match the 2 items can potentially be joined. However, this can only be done without changing the rendered result if an overlap test is successful.
This overlap test can be adjusted to join items up to a specific number of item references, selectable in project settings, or turned off.
3)
The legacy uniform single rect drawing routine seems to have been identified as the source of flicker, particularly on nvidia. However, it can also be up to 2x as fast. Because of the speed the batching contains a fallback where it can use the legacy single rect method, but I have now added a project setting to make this switchable. In most cases with batching it should not be necessary (as single rects are drawn less frequently) and thus the flickering can be totally avoided.
4)
This PR also fixes a color modulate bug when drawing light passes, in certain situations (particularly custom _draw routines with multiple rects).
5)
This PR also fixes #38291, a bug in the legacy renderer where light passes could draw rects in wrong position.
2020-04-29 09:24:43 +02:00
void RasterizerCanvasGLES2 : : canvas_end ( ) {
2020-10-14 09:32:05 +02:00
batch_canvas_end ( ) ;
GLES2 2D batching - item reordering, light joining and light modulate fix
Although 2D draws in painters order with strict ordering, in certain circumstances items can be reordered to increase batching / decrease state changes, without affecting the end result. This can be determined by an overlap test.
In situation with item:
A-B-A
providing the third item does not overlap the second, they can be reordered:
A-A-B
Items already contain an AABB which can be used for this overlap test.
1)
To utilise this, I have implemented item reordering (only for single rects for now), with the lookahead adjustable in project settings. This can increase performance in situations where items may not be grouped in the scene tree by texture. It can also be switched off (by setting lookahead to 0).
2)
This same trick can be used to help join items that are lit. Lit items previously would prevent joining completely, thus missing out on performance gains other than multi-command items such as tilemaps.
In this PR, lights are assigned as bits in a bitfield (up to 64, the optimization is disabled above this), and on each try_item (for joining), the bitfield for lights and shadows is constructed and compared with the previous items. If these match the 2 items can potentially be joined. However, this can only be done without changing the rendered result if an overlap test is successful.
This overlap test can be adjusted to join items up to a specific number of item references, selectable in project settings, or turned off.
3)
The legacy uniform single rect drawing routine seems to have been identified as the source of flicker, particularly on nvidia. However, it can also be up to 2x as fast. Because of the speed the batching contains a fallback where it can use the legacy single rect method, but I have now added a project setting to make this switchable. In most cases with batching it should not be necessary (as single rects are drawn less frequently) and thus the flickering can be totally avoided.
4)
This PR also fixes a color modulate bug when drawing light passes, in certain situations (particularly custom _draw routines with multiple rects).
5)
This PR also fixes #38291, a bug in the legacy renderer where light passes could draw rects in wrong position.
2020-04-29 09:24:43 +02:00
RasterizerCanvasBaseGLES2 : : canvas_end ( ) ;
}
2020-04-17 09:44:12 +02:00
void RasterizerCanvasGLES2 : : canvas_begin ( ) {
2020-10-14 09:32:05 +02:00
batch_canvas_begin ( ) ;
2020-04-17 09:44:12 +02:00
RasterizerCanvasBaseGLES2 : : canvas_begin ( ) ;
}
2020-04-12 14:52:25 +02:00
void RasterizerCanvasGLES2 : : canvas_render_items_begin ( const Color & p_modulate , Light * p_light , const Transform2D & p_base_transform ) {
2020-10-14 09:32:05 +02:00
batch_canvas_render_items_begin ( p_modulate , p_light , p_base_transform ) ;
2020-04-12 14:52:25 +02:00
}
void RasterizerCanvasGLES2 : : canvas_render_items_end ( ) {
2020-10-14 09:32:05 +02:00
batch_canvas_render_items_end ( ) ;
2020-04-12 14:52:25 +02:00
}
void RasterizerCanvasGLES2 : : canvas_render_items ( Item * p_item_list , int p_z , const Color & p_modulate , Light * p_light , const Transform2D & p_base_transform ) {
2020-10-14 09:32:05 +02:00
batch_canvas_render_items ( p_item_list , p_z , p_modulate , p_light , p_base_transform ) ;
2020-03-27 10:19:37 +01:00
}
2017-12-04 13:41:34 +01:00
2020-03-27 10:19:37 +01:00
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 ) {
// 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 ;
2017-12-04 13:41:34 +01:00
2018-12-28 00:39:00 +01:00
state . canvas_shader . set_conditional ( CanvasShaderGLES2 : : USE_SKELETON , false ) ;
2017-12-04 13:41:34 +01:00
state . current_tex = RID ( ) ;
2021-05-04 16:00:45 +02:00
state . current_tex_ptr = nullptr ;
2017-12-04 13:41:34 +01:00
state . current_normal = RID ( ) ;
2019-01-24 21:00:33 +01:00
state . canvas_texscreen_used = false ;
2017-12-04 13:41:34 +01:00
2023-08-09 15:05:25 +02:00
WRAPPED_GL_ACTIVE_TEXTURE ( GL_TEXTURE0 ) ;
2017-12-04 13:41:34 +01:00
glBindTexture ( GL_TEXTURE_2D , storage - > resources . white_tex ) ;
2020-03-27 10:19:37 +01:00
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 ) {
Item * ci = p_item_list ;
2020-10-14 09:32:05 +02:00
_legacy_canvas_render_item ( ci , ris ) ;
2020-03-27 10:19:37 +01:00
p_item_list = p_item_list - > next ;
}
}
2017-12-04 13:41:34 +01:00
2020-03-27 10:19:37 +01:00
if ( ris . current_clip ) {
glDisable ( GL_SCISSOR_TEST ) ;
}
2017-12-04 13:41:34 +01:00
2020-03-27 10:19:37 +01:00
state . canvas_shader . set_conditional ( CanvasShaderGLES2 : : USE_SKELETON , false ) ;
}
2017-12-04 13:41:34 +01:00
2020-03-27 10:19:37 +01:00
// 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 RasterizerCanvasGLES2 : : 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 ;
}
2018-08-20 02:10:14 +02:00
2020-03-27 10:19:37 +01:00
// if there are any state changes we change join to false
2020-05-11 21:19:34 +02:00
// 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)
2020-03-27 10:19:37 +01:00
r_batch_break = false ;
bool join = true ;
2017-12-04 13:41:34 +01:00
2021-02-18 10:57:02 +01:00
// light_masked objects we just don't currently support for joining
// (this could possibly be improved at a later date)
if ( p_ci - > light_masked ) {
join = false ;
r_batch_break = true ;
}
2020-03-27 10:19:37 +01:00
2020-04-06 13:49:47 +02:00
// 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;
// }
2020-03-27 10:19:37 +01:00
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 ;
}
2021-05-04 16:00:45 +02:00
RasterizerStorageGLES2 : : Skeleton * skeleton = nullptr ;
2020-03-27 10:19:37 +01:00
{
//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 ) {
2021-05-04 16:00:45 +02:00
skeleton = nullptr ;
2017-12-04 13:41:34 +01:00
}
}
2020-10-14 09:32:05 +02:00
bool skeleton_prevent_join = false ;
2021-05-04 16:00:45 +02:00
bool use_skeleton = skeleton ! = nullptr ;
2020-03-27 10:19:37 +01:00
if ( r_ris . prev_use_skeleton ! = use_skeleton ) {
2021-05-05 12:44:11 +02:00
if ( ! bdata . settings_use_software_skinning ) {
2020-10-14 09:32:05 +02:00
r_ris . rebind_shader = true ;
2021-05-05 12:44:11 +02:00
}
2020-10-14 09:32:05 +02:00
2020-03-27 10:19:37 +01:00
r_ris . prev_use_skeleton = use_skeleton ;
2020-10-14 09:32:05 +02:00
// join = false;
skeleton_prevent_join = true ;
2020-03-27 10:19:37 +01:00
}
2017-12-04 13:41:34 +01:00
2020-03-27 10:19:37 +01:00
if ( skeleton ) {
2020-10-14 09:32:05 +02:00
// join = false;
skeleton_prevent_join = true ;
2020-03-27 10:19:37 +01:00
state . using_skeleton = true ;
} else {
state . using_skeleton = false ;
2017-12-04 13:41:34 +01:00
}
2020-10-14 09:32:05 +02:00
if ( skeleton_prevent_join ) {
2021-05-05 12:44:11 +02:00
if ( ! bdata . settings_use_software_skinning ) {
2020-10-14 09:32:05 +02:00
join = false ;
2021-05-05 12:44:11 +02:00
}
2020-10-14 09:32:05 +02:00
}
2020-03-27 10:19:37 +01:00
}
2017-12-04 13:41:34 +01:00
2020-03-27 10:19:37 +01:00
Item * material_owner = p_ci - > material_owner ? p_ci - > material_owner : p_ci ;
2018-12-28 00:39:00 +01:00
2020-03-27 10:19:37 +01:00
RID material = material_owner - > material ;
RasterizerStorageGLES2 : : Material * material_ptr = storage - > material_owner . getornull ( material ) ;
if ( material ! = r_ris . canvas_last_material | | r_ris . rebind_shader ) {
join = false ;
2021-05-04 16:00:45 +02:00
RasterizerStorageGLES2 : : Shader * shader_ptr = nullptr ;
2020-03-27 10:19:37 +01:00
if ( material_ptr ) {
shader_ptr = material_ptr - > shader ;
2021-04-18 09:31:59 +02:00
// special case, if the user has made an error in the shader code
if ( shader_ptr & & ! shader_ptr - > valid ) {
join = false ;
r_batch_break = true ;
}
2020-03-27 10:19:37 +01:00
if ( shader_ptr & & shader_ptr - > mode ! = VS : : SHADER_CANVAS_ITEM ) {
2021-05-04 16:00:45 +02:00
shader_ptr = nullptr ; // not a canvas item shader, don't use.
2018-12-28 00:39:00 +01:00
}
2020-03-27 10:19:37 +01:00
}
2018-12-28 00:39:00 +01:00
2020-03-27 10:19:37 +01:00
if ( shader_ptr ) {
if ( shader_ptr - > canvas_item . uses_screen_texture ) {
if ( ! state . canvas_texscreen_used ) {
join = false ;
}
2018-12-28 00:39:00 +01:00
}
}
2020-03-27 10:19:37 +01:00
r_ris . shader_cache = shader_ptr ;
2018-08-20 02:10:14 +02:00
2020-03-27 10:19:37 +01:00
r_ris . canvas_last_material = material ;
2017-12-04 13:41:34 +01:00
2020-03-27 10:19:37 +01:00
r_ris . rebind_shader = false ;
}
2017-12-04 13:41:34 +01:00
2020-03-27 10:19:37 +01:00
int blend_mode = r_ris . shader_cache ? r_ris . shader_cache - > canvas_item . blend_mode : RasterizerStorageGLES2 : : Shader : : CanvasItem : : BLEND_MODE_MIX ;
bool unshaded = r_ris . shader_cache & & ( r_ris . shader_cache - > canvas_item . light_mode = = RasterizerStorageGLES2 : : Shader : : CanvasItem : : LIGHT_MODE_UNSHADED | | ( blend_mode ! = RasterizerStorageGLES2 : : Shader : : CanvasItem : : BLEND_MODE_MIX & & blend_mode ! = RasterizerStorageGLES2 : : Shader : : CanvasItem : : BLEND_MODE_PMALPHA ) ) ;
bool reclip = false ;
2017-12-04 13:41:34 +01:00
2020-04-06 13:49:47 +02:00
// 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 ) ;
2020-03-27 10:19:37 +01:00
if ( r_ris . last_blend_mode ! = blend_mode ) {
join = false ;
r_ris . last_blend_mode = blend_mode ;
}
2017-12-04 13:41:34 +01:00
2020-05-11 21:19:34 +02:00
// does the shader contain BUILTINs which should break the batching?
bdata . joined_item_batch_flags = 0 ;
2020-05-23 12:35:33 +02:00
if ( r_ris . shader_cache ) {
2020-11-19 16:09:33 +01:00
unsigned int and_flags = r_ris . shader_cache - > canvas_item . batch_flags & ( RasterizerStorageCommon : : PREVENT_COLOR_BAKING | RasterizerStorageCommon : : PREVENT_VERTEX_BAKING | RasterizerStorageCommon : : PREVENT_ITEM_JOINING ) ;
2020-05-11 21:19:34 +02:00
if ( and_flags ) {
2020-11-19 16:09:33 +01:00
// special case for preventing item joining altogether
if ( and_flags & RasterizerStorageCommon : : PREVENT_ITEM_JOINING ) {
join = false ;
//r_batch_break = true; // don't think we need a batch break
2020-10-14 09:32:05 +02:00
2020-11-19 16:09:33 +01:00
// 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 ;
} else {
bool use_larger_fvfs = true ;
2020-05-11 21:19:34 +02:00
2020-10-14 09:32:05 +02:00
if ( and_flags = = RasterizerStorageCommon : : PREVENT_COLOR_BAKING ) {
2020-11-19 16:09:33 +01:00
// 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 ;
}
2020-10-14 09:32:05 +02:00
}
2020-11-19 16:09:33 +01:00
// 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 ;
}
2020-10-14 09:32:05 +02:00
2020-11-19 16:09:33 +01:00
/*
2020-10-14 09:32:05 +02:00
if ( and_flags = = RasterizerStorageCommon : : PREVENT_COLOR_BAKING ) {
2020-05-11 21:19:34 +02:00
// 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 ;
}
2020-10-14 09:32:05 +02:00
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 ;
}
2020-05-11 21:19:34 +02:00
}
if ( break_batching ) {
join = false ;
r_batch_break = true ;
// 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 ;
}
2020-10-14 09:32:05 +02:00
*/
2020-11-19 16:09:33 +01:00
} // if not prevent item joining
2020-05-11 21:19:34 +02:00
}
}
2020-03-27 10:19:37 +01:00
if ( ( blend_mode = = RasterizerStorageGLES2 : : Shader : : CanvasItem : : BLEND_MODE_MIX | | blend_mode = = RasterizerStorageGLES2 : : 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
2020-04-12 14:52:25 +02:00
GLES2 2D batching - item reordering, light joining and light modulate fix
Although 2D draws in painters order with strict ordering, in certain circumstances items can be reordered to increase batching / decrease state changes, without affecting the end result. This can be determined by an overlap test.
In situation with item:
A-B-A
providing the third item does not overlap the second, they can be reordered:
A-A-B
Items already contain an AABB which can be used for this overlap test.
1)
To utilise this, I have implemented item reordering (only for single rects for now), with the lookahead adjustable in project settings. This can increase performance in situations where items may not be grouped in the scene tree by texture. It can also be switched off (by setting lookahead to 0).
2)
This same trick can be used to help join items that are lit. Lit items previously would prevent joining completely, thus missing out on performance gains other than multi-command items such as tilemaps.
In this PR, lights are assigned as bits in a bitfield (up to 64, the optimization is disabled above this), and on each try_item (for joining), the bitfield for lights and shadows is constructed and compared with the previous items. If these match the 2 items can potentially be joined. However, this can only be done without changing the rendered result if an overlap test is successful.
This overlap test can be adjusted to join items up to a specific number of item references, selectable in project settings, or turned off.
3)
The legacy uniform single rect drawing routine seems to have been identified as the source of flicker, particularly on nvidia. However, it can also be up to 2x as fast. Because of the speed the batching contains a fallback where it can use the legacy single rect method, but I have now added a project setting to make this switchable. In most cases with batching it should not be necessary (as single rects are drawn less frequently) and thus the flickering can be totally avoided.
4)
This PR also fixes a color modulate bug when drawing light passes, in certain situations (particularly custom _draw routines with multiple rects).
5)
This PR also fixes #38291, a bug in the legacy renderer where light passes could draw rects in wrong position.
2020-04-29 09:24:43 +02:00
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 + + ;
2020-05-18 15:21:26 +02:00
uint64_t light_bit = 1ULL < < light_count ;
GLES2 2D batching - item reordering, light joining and light modulate fix
Although 2D draws in painters order with strict ordering, in certain circumstances items can be reordered to increase batching / decrease state changes, without affecting the end result. This can be determined by an overlap test.
In situation with item:
A-B-A
providing the third item does not overlap the second, they can be reordered:
A-A-B
Items already contain an AABB which can be used for this overlap test.
1)
To utilise this, I have implemented item reordering (only for single rects for now), with the lookahead adjustable in project settings. This can increase performance in situations where items may not be grouped in the scene tree by texture. It can also be switched off (by setting lookahead to 0).
2)
This same trick can be used to help join items that are lit. Lit items previously would prevent joining completely, thus missing out on performance gains other than multi-command items such as tilemaps.
In this PR, lights are assigned as bits in a bitfield (up to 64, the optimization is disabled above this), and on each try_item (for joining), the bitfield for lights and shadows is constructed and compared with the previous items. If these match the 2 items can potentially be joined. However, this can only be done without changing the rendered result if an overlap test is successful.
This overlap test can be adjusted to join items up to a specific number of item references, selectable in project settings, or turned off.
3)
The legacy uniform single rect drawing routine seems to have been identified as the source of flicker, particularly on nvidia. However, it can also be up to 2x as fast. Because of the speed the batching contains a fallback where it can use the legacy single rect method, but I have now added a project setting to make this switchable. In most cases with batching it should not be necessary (as single rects are drawn less frequently) and thus the flickering can be totally avoided.
4)
This PR also fixes a color modulate bug when drawing light passes, in certain situations (particularly custom _draw routines with multiple rects).
5)
This PR also fixes #38291, a bug in the legacy renderer where light passes could draw rects in wrong position.
2020-04-29 09:24:43 +02:00
// 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 ;
2020-10-14 09:32:05 +02:00
// 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 ;
GLES2 2D batching - item reordering, light joining and light modulate fix
Although 2D draws in painters order with strict ordering, in certain circumstances items can be reordered to increase batching / decrease state changes, without affecting the end result. This can be determined by an overlap test.
In situation with item:
A-B-A
providing the third item does not overlap the second, they can be reordered:
A-A-B
Items already contain an AABB which can be used for this overlap test.
1)
To utilise this, I have implemented item reordering (only for single rects for now), with the lookahead adjustable in project settings. This can increase performance in situations where items may not be grouped in the scene tree by texture. It can also be switched off (by setting lookahead to 0).
2)
This same trick can be used to help join items that are lit. Lit items previously would prevent joining completely, thus missing out on performance gains other than multi-command items such as tilemaps.
In this PR, lights are assigned as bits in a bitfield (up to 64, the optimization is disabled above this), and on each try_item (for joining), the bitfield for lights and shadows is constructed and compared with the previous items. If these match the 2 items can potentially be joined. However, this can only be done without changing the rendered result if an overlap test is successful.
This overlap test can be adjusted to join items up to a specific number of item references, selectable in project settings, or turned off.
3)
The legacy uniform single rect drawing routine seems to have been identified as the source of flicker, particularly on nvidia. However, it can also be up to 2x as fast. Because of the speed the batching contains a fallback where it can use the legacy single rect method, but I have now added a project setting to make this switchable. In most cases with batching it should not be necessary (as single rects are drawn less frequently) and thus the flickering can be totally avoided.
4)
This PR also fixes a color modulate bug when drawing light passes, in certain situations (particularly custom _draw routines with multiple rects).
5)
This PR also fixes #38291, a bug in the legacy renderer where light passes could draw rects in wrong position.
2020-04-29 09:24:43 +02:00
}
} else {
2020-06-14 09:16:38 +02:00
// 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 ;
}
2020-03-27 10:19:37 +01:00
}
2019-01-24 21:00:33 +01:00
2020-03-27 10:19:37 +01:00
if ( reclip ) {
join = false ;
}
2019-01-24 21:00:33 +01:00
2020-03-27 10:19:37 +01:00
// non rects will break the batching anyway, we don't want to record item changes, detect this
2020-10-14 09:32:05 +02:00
if ( ! r_batch_break & & _detect_item_batch_break ( r_ris , p_ci , r_batch_break ) ) {
2020-03-27 10:19:37 +01:00
join = false ;
2020-10-14 09:32:05 +02:00
2020-03-27 10:19:37 +01:00
r_batch_break = true ;
}
2017-12-04 13:41:34 +01:00
2020-03-27 10:19:37 +01:00
return join ;
}
2017-12-04 13:41:34 +01:00
2020-03-27 10:19:37 +01:00
// Legacy non-batched implementation for regression testing.
// Should be removed after testing phase to avoid duplicate codepaths.
2020-10-14 09:32:05 +02:00
void RasterizerCanvasGLES2 : : _legacy_canvas_render_item ( Item * p_ci , RenderItemState & r_ris ) {
2020-04-16 12:46:52 +02:00
storage - > info . render . _2d_item_count + + ;
2020-03-27 10:19:37 +01:00
if ( r_ris . current_clip ! = p_ci - > final_clip_owner ) {
r_ris . current_clip = p_ci - > final_clip_owner ;
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 ) ;
2021-05-05 12:44:11 +02:00
if ( storage - > frame . current_rt - > flags [ RasterizerStorage : : RENDER_TARGET_VFLIP ] ) {
2020-03-27 10:19:37 +01:00
y = r_ris . current_clip - > final_clip_rect . position . y ;
2021-05-05 12:44:11 +02:00
}
2020-03-27 10:19:37 +01:00
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 ) ;
} else {
glDisable ( GL_SCISSOR_TEST ) ;
}
}
// TODO: copy back buffer
2018-02-24 14:48:22 +01:00
2020-03-27 10:19:37 +01:00
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 ) ;
}
}
2021-05-04 16:00:45 +02:00
RasterizerStorageGLES2 : : Skeleton * skeleton = nullptr ;
2020-03-27 10:19:37 +01:00
{
//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 ) {
2021-05-04 16:00:45 +02:00
skeleton = nullptr ;
2017-12-04 13:41:34 +01:00
} else {
2020-03-27 10:19:37 +01:00
state . skeleton_transform = r_ris . item_group_base_transform * skeleton - > base_transform_2d ;
state . skeleton_transform_inverse = state . skeleton_transform . affine_inverse ( ) ;
state . skeleton_texture_size = Vector2 ( skeleton - > size * 2 , 0 ) ;
2017-12-04 13:41:34 +01:00
}
2020-03-27 10:19:37 +01:00
}
2021-05-04 16:00:45 +02:00
bool use_skeleton = skeleton ! = nullptr ;
2020-03-27 10:19:37 +01:00
if ( r_ris . prev_use_skeleton ! = use_skeleton ) {
r_ris . rebind_shader = true ;
state . canvas_shader . set_conditional ( CanvasShaderGLES2 : : USE_SKELETON , use_skeleton ) ;
r_ris . prev_use_skeleton = use_skeleton ;
}
if ( skeleton ) {
2023-08-09 15:05:25 +02:00
WRAPPED_GL_ACTIVE_TEXTURE ( GL_TEXTURE0 + storage - > config . max_texture_image_units - 3 ) ;
2020-03-27 10:19:37 +01:00
glBindTexture ( GL_TEXTURE_2D , skeleton - > tex_id ) ;
state . using_skeleton = true ;
} else {
state . using_skeleton = false ;
}
}
2017-12-04 13:41:34 +01:00
2020-03-27 10:19:37 +01:00
Item * material_owner = p_ci - > material_owner ? p_ci - > material_owner : p_ci ;
2017-12-04 13:41:34 +01:00
2020-03-27 10:19:37 +01:00
RID material = material_owner - > material ;
RasterizerStorageGLES2 : : Material * material_ptr = storage - > material_owner . getornull ( material ) ;
2017-12-04 13:41:34 +01:00
2020-03-27 10:19:37 +01:00
if ( material ! = r_ris . canvas_last_material | | r_ris . rebind_shader ) {
2021-05-04 16:00:45 +02:00
RasterizerStorageGLES2 : : Shader * shader_ptr = nullptr ;
2020-03-27 10:19:37 +01:00
if ( material_ptr ) {
shader_ptr = material_ptr - > shader ;
if ( shader_ptr & & shader_ptr - > mode ! = VS : : SHADER_CANVAS_ITEM ) {
2021-05-04 16:00:45 +02:00
shader_ptr = nullptr ; // not a canvas item shader, don't use.
2020-03-27 10:19:37 +01:00
}
2017-12-04 13:41:34 +01:00
}
2020-03-27 10:19:37 +01:00
if ( shader_ptr ) {
if ( shader_ptr - > canvas_item . uses_screen_texture ) {
if ( ! state . canvas_texscreen_used ) {
//copy if not copied before
_copy_texscreen ( Rect2 ( ) ) ;
2017-12-04 13:41:34 +01:00
2020-03-27 10:19:37 +01:00
// blend mode will have been enabled so make sure we disable it again later on
//last_blend_mode = last_blend_mode != RasterizerStorageGLES2::Shader::CanvasItem::BLEND_MODE_DISABLED ? last_blend_mode : -1;
}
2017-12-04 13:41:34 +01:00
2020-03-27 10:19:37 +01:00
if ( storage - > frame . current_rt - > copy_screen_effect . color ) {
2023-08-09 15:05:25 +02:00
WRAPPED_GL_ACTIVE_TEXTURE ( GL_TEXTURE0 + storage - > config . max_texture_image_units - 4 ) ;
2020-03-27 10:19:37 +01:00
glBindTexture ( GL_TEXTURE_2D , storage - > frame . current_rt - > copy_screen_effect . color ) ;
}
}
2017-12-04 13:41:34 +01:00
2020-03-27 10:19:37 +01:00
if ( shader_ptr ! = r_ris . shader_cache ) {
if ( shader_ptr - > canvas_item . uses_time ) {
2021-10-05 20:05:57 +02:00
VisualServerRaster : : redraw_request ( false ) ;
2020-03-27 10:19:37 +01:00
}
2017-12-04 13:41:34 +01:00
2020-03-27 10:19:37 +01:00
state . canvas_shader . set_custom_shader ( shader_ptr - > custom_code_id ) ;
state . canvas_shader . bind ( ) ;
}
2017-12-04 13:41:34 +01:00
2020-03-27 10:19:37 +01:00
int tc = material_ptr - > textures . size ( ) ;
Pair < StringName , RID > * textures = material_ptr - > textures . ptrw ( ) ;
2017-12-04 13:41:34 +01:00
2020-03-27 10:19:37 +01:00
ShaderLanguage : : ShaderNode : : Uniform : : Hint * texture_hints = shader_ptr - > texture_hints . ptrw ( ) ;
for ( int i = 0 ; i < tc ; i + + ) {
2023-08-09 15:05:25 +02:00
WRAPPED_GL_ACTIVE_TEXTURE ( GL_TEXTURE0 + i ) ;
2020-03-27 10:19:37 +01:00
RasterizerStorageGLES2 : : Texture * t = storage - > texture_owner . getornull ( textures [ i ] . second ) ;
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 ;
2021-08-14 19:14:59 +02:00
case ShaderLanguage : : ShaderNode : : Uniform : : HINT_TRANSPARENT : {
glBindTexture ( GL_TEXTURE_2D , storage - > resources . transparent_tex ) ;
} break ;
2020-03-27 10:19:37 +01:00
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 ;
2019-01-05 16:56:54 +01:00
}
2020-03-27 10:19:37 +01:00
continue ;
}
if ( t - > redraw_if_visible ) {
2022-06-12 07:44:59 +02:00
VisualServerRaster : : redraw_request ( false ) ;
2020-03-27 10:19:37 +01:00
}
t = t - > get_ptr ( ) ;
# ifdef TOOLS_ENABLED
if ( t - > detect_normal & & texture_hints [ i ] = = ShaderLanguage : : ShaderNode : : Uniform : : HINT_NORMAL ) {
t - > detect_normal ( t - > detect_normal_ud ) ;
}
# endif
2021-05-05 12:44:11 +02:00
if ( t - > render_target ) {
2020-03-27 10:19:37 +01:00
t - > render_target - > used_in_frame = true ;
2021-05-05 12:44:11 +02:00
}
2020-03-27 10:19:37 +01:00
glBindTexture ( t - > target , t - > tex_id ) ;
2017-12-04 13:41:34 +01:00
}
2020-03-27 10:19:37 +01:00
} else {
state . canvas_shader . set_custom_shader ( 0 ) ;
state . canvas_shader . bind ( ) ;
2017-12-04 13:41:34 +01:00
}
2020-03-27 10:19:37 +01:00
state . canvas_shader . use_material ( ( void * ) material_ptr ) ;
2017-12-04 13:41:34 +01:00
2020-03-27 10:19:37 +01:00
r_ris . shader_cache = shader_ptr ;
2017-12-04 13:41:34 +01:00
2020-03-27 10:19:37 +01:00
r_ris . canvas_last_material = material ;
2017-12-04 13:41:34 +01:00
2020-03-27 10:19:37 +01:00
r_ris . rebind_shader = false ;
}
int blend_mode = r_ris . shader_cache ? r_ris . shader_cache - > canvas_item . blend_mode : RasterizerStorageGLES2 : : Shader : : CanvasItem : : BLEND_MODE_MIX ;
bool unshaded = r_ris . shader_cache & & ( r_ris . shader_cache - > canvas_item . light_mode = = RasterizerStorageGLES2 : : Shader : : CanvasItem : : LIGHT_MODE_UNSHADED | | ( blend_mode ! = RasterizerStorageGLES2 : : Shader : : CanvasItem : : BLEND_MODE_MIX & & blend_mode ! = RasterizerStorageGLES2 : : Shader : : CanvasItem : : BLEND_MODE_PMALPHA ) ) ;
bool reclip = false ;
if ( r_ris . last_blend_mode ! = blend_mode ) {
switch ( blend_mode ) {
case RasterizerStorageGLES2 : : 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 RasterizerStorageGLES2 : : 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 ) ;
}
2017-12-04 13:41:34 +01:00
2020-03-27 10:19:37 +01:00
} break ;
case RasterizerStorageGLES2 : : 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 RasterizerStorageGLES2 : : 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 RasterizerStorageGLES2 : : 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 ;
}
}
2018-07-31 23:52:55 +02:00
2020-03-27 10:19:37 +01:00
state . uniforms . final_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 ) ;
2017-12-04 13:41:34 +01:00
2020-03-27 10:19:37 +01:00
state . uniforms . modelview_matrix = p_ci - > final_transform ;
state . uniforms . extra_matrix = Transform2D ( ) ;
2018-12-20 14:09:07 +01:00
2020-03-27 10:19:37 +01:00
_set_uniforms ( ) ;
2018-12-20 14:09:07 +01:00
2021-05-05 12:44:11 +02:00
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 ) ) {
2021-05-04 16:00:45 +02:00
_legacy_canvas_item_render_commands ( p_ci , nullptr , reclip , material_ptr ) ;
2021-05-05 12:44:11 +02:00
}
2018-12-20 14:09:07 +01:00
2020-03-27 10:19:37 +01:00
r_ris . rebind_shader = true ; // hacked in for now.
2018-12-20 14:09:07 +01:00
2020-03-27 10:19:37 +01:00
if ( ( blend_mode = = RasterizerStorageGLES2 : : Shader : : CanvasItem : : BLEND_MODE_MIX | | blend_mode = = RasterizerStorageGLES2 : : 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 . uniforms . final_modulate = p_ci - > final_modulate ; // remove the canvas modulate
2018-12-20 14:09:07 +01:00
2020-03-27 10:19:37 +01:00
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
2018-12-20 14:09:07 +01:00
2020-03-27 10:19:37 +01:00
if ( ! light_used | | mode ! = light - > mode ) {
mode = light - > mode ;
2018-12-20 14:09:07 +01:00
2020-03-27 10:19:37 +01:00
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 ) ;
2018-12-20 14:09:07 +01:00
2020-03-27 10:19:37 +01:00
} break ;
2018-12-20 14:09:07 +01:00
}
2020-03-27 10:19:37 +01:00
}
2018-12-20 14:09:07 +01:00
2020-03-27 10:19:37 +01:00
if ( ! light_used ) {
state . canvas_shader . set_conditional ( CanvasShaderGLES2 : : USE_LIGHTING , true ) ;
light_used = true ;
}
2018-12-20 14:09:07 +01:00
2020-03-27 10:19:37 +01:00
bool has_shadow = light - > shadow_buffer . is_valid ( ) & & p_ci - > light_mask & light - > item_shadow_mask ;
state . canvas_shader . set_conditional ( CanvasShaderGLES2 : : USE_SHADOWS , has_shadow ) ;
if ( has_shadow ) {
state . canvas_shader . set_conditional ( CanvasShaderGLES2 : : SHADOW_USE_GRADIENT , light - > shadow_gradient_length > 0 ) ;
state . canvas_shader . set_conditional ( CanvasShaderGLES2 : : SHADOW_FILTER_NEAREST , light - > shadow_filter = = VS : : CANVAS_LIGHT_FILTER_NONE ) ;
state . canvas_shader . set_conditional ( CanvasShaderGLES2 : : SHADOW_FILTER_PCF3 , light - > shadow_filter = = VS : : CANVAS_LIGHT_FILTER_PCF3 ) ;
state . canvas_shader . set_conditional ( CanvasShaderGLES2 : : SHADOW_FILTER_PCF5 , light - > shadow_filter = = VS : : CANVAS_LIGHT_FILTER_PCF5 ) ;
state . canvas_shader . set_conditional ( CanvasShaderGLES2 : : SHADOW_FILTER_PCF7 , light - > shadow_filter = = VS : : CANVAS_LIGHT_FILTER_PCF7 ) ;
state . canvas_shader . set_conditional ( CanvasShaderGLES2 : : SHADOW_FILTER_PCF9 , light - > shadow_filter = = VS : : CANVAS_LIGHT_FILTER_PCF9 ) ;
state . canvas_shader . set_conditional ( CanvasShaderGLES2 : : SHADOW_FILTER_PCF13 , light - > shadow_filter = = VS : : CANVAS_LIGHT_FILTER_PCF13 ) ;
}
2018-12-20 14:09:07 +01:00
2020-03-27 10:19:37 +01:00
state . canvas_shader . bind ( ) ;
state . using_light = light ;
state . using_shadow = has_shadow ;
//always re-set uniforms, since light parameters changed
_set_uniforms ( ) ;
state . canvas_shader . use_material ( ( void * ) material_ptr ) ;
2018-12-20 14:09:07 +01:00
2023-08-09 15:05:25 +02:00
WRAPPED_GL_ACTIVE_TEXTURE ( GL_TEXTURE0 + storage - > config . max_texture_image_units - 6 ) ;
2020-03-27 10:19:37 +01:00
RasterizerStorageGLES2 : : Texture * t = storage - > texture_owner . getornull ( light - > texture ) ;
if ( ! t ) {
glBindTexture ( GL_TEXTURE_2D , storage - > resources . white_tex ) ;
} else {
t = t - > get_ptr ( ) ;
2019-01-14 16:03:33 +01:00
2020-03-27 10:19:37 +01:00
glBindTexture ( t - > target , t - > tex_id ) ;
2018-12-20 14:09:07 +01:00
}
2023-08-09 15:05:25 +02:00
WRAPPED_GL_ACTIVE_TEXTURE ( GL_TEXTURE0 ) ;
_legacy_canvas_item_render_commands ( p_ci , nullptr , reclip , material_ptr ) ; //redraw using lights
2021-05-04 16:00:45 +02:00
state . using_light = nullptr ;
2018-12-20 14:09:07 +01:00
}
2020-03-27 10:19:37 +01:00
light = light - > next_ptr ;
}
2018-12-20 14:09:07 +01:00
2020-03-27 10:19:37 +01:00
if ( light_used ) {
state . canvas_shader . set_conditional ( CanvasShaderGLES2 : : USE_LIGHTING , false ) ;
state . canvas_shader . set_conditional ( CanvasShaderGLES2 : : USE_SHADOWS , false ) ;
state . canvas_shader . set_conditional ( CanvasShaderGLES2 : : SHADOW_FILTER_NEAREST , false ) ;
state . canvas_shader . set_conditional ( CanvasShaderGLES2 : : SHADOW_FILTER_PCF3 , false ) ;
state . canvas_shader . set_conditional ( CanvasShaderGLES2 : : SHADOW_FILTER_PCF5 , false ) ;
state . canvas_shader . set_conditional ( CanvasShaderGLES2 : : SHADOW_FILTER_PCF7 , false ) ;
state . canvas_shader . set_conditional ( CanvasShaderGLES2 : : SHADOW_FILTER_PCF9 , false ) ;
state . canvas_shader . set_conditional ( CanvasShaderGLES2 : : SHADOW_FILTER_PCF13 , false ) ;
2018-12-20 14:09:07 +01:00
2020-03-27 10:19:37 +01:00
state . canvas_shader . bind ( ) ;
2018-12-20 14:09:07 +01:00
2020-03-27 10:19:37 +01:00
r_ris . last_blend_mode = - 1 ;
2018-12-20 14:09:07 +01:00
2020-03-27 10:19:37 +01:00
/*
//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 ) ;
2018-12-20 14:09:07 +01:00
2020-10-14 09:32:05 +02:00
2020-03-27 10:19:37 +01:00
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 ) ;
2018-12-20 14:09:07 +01:00
2020-03-27 10:19:37 +01:00
glBlendEquation ( GL_FUNC_ADD ) ;
2018-12-20 14:09:07 +01:00
2020-03-27 10:19:37 +01:00
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 ) ;
2018-12-20 14:09:07 +01:00
}
2020-03-27 10:19:37 +01:00
//@TODO RESET canvas_blend_mode
*/
2018-12-20 14:09:07 +01:00
}
2020-03-27 10:19:37 +01:00
}
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 ) ;
2021-05-05 12:44:11 +02:00
if ( storage - > frame . current_rt - > flags [ RasterizerStorage : : RENDER_TARGET_VFLIP ] ) {
2020-03-27 10:19:37 +01:00
y = r_ris . current_clip - > final_clip_rect . position . y ;
2021-05-05 12:44:11 +02:00
}
2020-03-27 10:19:37 +01:00
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 RasterizerCanvasGLES2 : : render_joined_item ( const BItemJoined & p_bij , RenderItemState & r_ris ) {
2020-04-16 12:46:52 +02:00
storage - > info . render . _2d_item_count + + ;
2021-03-11 11:10:43 +01:00
# if defined(TOOLS_ENABLED) && defined(DEBUG_ENABLED)
2020-04-17 09:44:12 +02:00
if ( bdata . diagnose_frame ) {
2021-03-11 11:10:43 +01:00
bdata . frame_string + = _diagnose_make_item_joined_string ( p_bij ) ;
2020-04-17 09:44:12 +02:00
}
2020-04-23 12:50:49 +02:00
# endif
2020-04-17 09:44:12 +02:00
2020-03-27 10:19:37 +01:00
// all the joined items will share the same state with the first item
Item * ci = bdata . item_refs [ p_bij . first_item_ref ] . item ;
if ( r_ris . current_clip ! = ci - > final_clip_owner ) {
r_ris . current_clip = ci - > final_clip_owner ;
if ( r_ris . current_clip ) {
2017-12-04 13:41:34 +01:00
glEnable ( GL_SCISSOR_TEST ) ;
2020-03-27 10:19:37 +01:00
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 ) ;
2021-05-05 12:44:11 +02:00
if ( storage - > frame . current_rt - > flags [ RasterizerStorage : : RENDER_TARGET_VFLIP ] ) {
2020-03-27 10:19:37 +01:00
y = r_ris . current_clip - > final_clip_rect . position . y ;
2021-05-05 12:44:11 +02:00
}
2020-03-27 10:19:37 +01:00
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 ) ;
} else {
glDisable ( GL_SCISSOR_TEST ) ;
2017-12-04 13:41:34 +01:00
}
}
2020-03-27 10:19:37 +01:00
// TODO: copy back buffer
if ( ci - > copy_back_buffer ) {
if ( ci - > copy_back_buffer - > full ) {
_copy_texscreen ( Rect2 ( ) ) ;
} else {
_copy_texscreen ( ci - > copy_back_buffer - > rect ) ;
}
2017-12-04 13:41:34 +01:00
}
2018-12-28 00:39:00 +01:00
2020-10-14 09:32:05 +02:00
if ( ! bdata . settings_use_batching | | ! bdata . settings_use_software_skinning ) {
2021-05-04 16:00:45 +02:00
RasterizerStorageGLES2 : : Skeleton * skeleton = nullptr ;
2017-12-04 13:41:34 +01:00
2020-03-27 10:19:37 +01:00
//skeleton handling
if ( ci - > skeleton . is_valid ( ) & & storage - > skeleton_owner . owns ( ci - > skeleton ) ) {
skeleton = storage - > skeleton_owner . get ( ci - > skeleton ) ;
if ( ! skeleton - > use_2d ) {
2021-05-04 16:00:45 +02:00
skeleton = nullptr ;
2020-03-27 10:19:37 +01:00
} else {
state . skeleton_transform = r_ris . item_group_base_transform * skeleton - > base_transform_2d ;
state . skeleton_transform_inverse = state . skeleton_transform . affine_inverse ( ) ;
state . skeleton_texture_size = Vector2 ( skeleton - > size * 2 , 0 ) ;
}
}
2021-05-04 16:00:45 +02:00
bool use_skeleton = skeleton ! = nullptr ;
2020-03-27 10:19:37 +01:00
if ( r_ris . prev_use_skeleton ! = use_skeleton ) {
r_ris . rebind_shader = true ;
state . canvas_shader . set_conditional ( CanvasShaderGLES2 : : USE_SKELETON , use_skeleton ) ;
r_ris . prev_use_skeleton = use_skeleton ;
}
2017-12-04 13:41:34 +01:00
2020-03-27 10:19:37 +01:00
if ( skeleton ) {
2023-08-09 15:05:25 +02:00
WRAPPED_GL_ACTIVE_TEXTURE ( GL_TEXTURE0 + storage - > config . max_texture_image_units - 3 ) ;
2020-03-27 10:19:37 +01:00
glBindTexture ( GL_TEXTURE_2D , skeleton - > tex_id ) ;
state . using_skeleton = true ;
} else {
state . using_skeleton = false ;
}
2020-10-14 09:32:05 +02:00
} // if not using batching
2017-12-04 13:41:34 +01:00
2020-03-27 10:19:37 +01:00
Item * material_owner = ci - > material_owner ? ci - > material_owner : ci ;
2018-12-20 14:09:07 +01:00
2020-03-27 10:19:37 +01:00
RID material = material_owner - > material ;
RasterizerStorageGLES2 : : Material * material_ptr = storage - > material_owner . getornull ( material ) ;
2018-12-20 14:09:07 +01:00
2020-03-27 10:19:37 +01:00
if ( material ! = r_ris . canvas_last_material | | r_ris . rebind_shader ) {
2021-05-04 16:00:45 +02:00
RasterizerStorageGLES2 : : Shader * shader_ptr = nullptr ;
2018-12-20 14:09:07 +01:00
2020-03-27 10:19:37 +01:00
if ( material_ptr ) {
shader_ptr = material_ptr - > shader ;
2018-12-20 14:09:07 +01:00
2020-03-27 10:19:37 +01:00
if ( shader_ptr & & shader_ptr - > mode ! = VS : : SHADER_CANVAS_ITEM ) {
2021-05-04 16:00:45 +02:00
shader_ptr = nullptr ; // not a canvas item shader, don't use.
2020-03-27 10:19:37 +01:00
}
}
2018-12-20 14:09:07 +01:00
2020-03-27 10:19:37 +01:00
if ( shader_ptr ) {
if ( shader_ptr - > canvas_item . uses_screen_texture ) {
if ( ! state . canvas_texscreen_used ) {
//copy if not copied before
_copy_texscreen ( Rect2 ( ) ) ;
2018-12-20 14:09:07 +01:00
2020-03-27 10:19:37 +01:00
// blend mode will have been enabled so make sure we disable it again later on
//last_blend_mode = last_blend_mode != RasterizerStorageGLES2::Shader::CanvasItem::BLEND_MODE_DISABLED ? last_blend_mode : -1;
}
2018-12-20 14:09:07 +01:00
2020-03-27 10:19:37 +01:00
if ( storage - > frame . current_rt - > copy_screen_effect . color ) {
2023-08-09 15:05:25 +02:00
WRAPPED_GL_ACTIVE_TEXTURE ( GL_TEXTURE0 + storage - > config . max_texture_image_units - 4 ) ;
2020-03-27 10:19:37 +01:00
glBindTexture ( GL_TEXTURE_2D , storage - > frame . current_rt - > copy_screen_effect . color ) ;
}
}
2018-12-20 14:09:07 +01:00
2020-03-27 10:19:37 +01:00
if ( shader_ptr ! = r_ris . shader_cache ) {
if ( shader_ptr - > canvas_item . uses_time ) {
2021-10-05 20:05:57 +02:00
VisualServerRaster : : redraw_request ( false ) ;
2020-03-27 10:19:37 +01:00
}
2018-12-20 14:09:07 +01:00
2020-03-27 10:19:37 +01:00
state . canvas_shader . set_custom_shader ( shader_ptr - > custom_code_id ) ;
state . canvas_shader . bind ( ) ;
}
2018-12-20 14:09:07 +01:00
2020-03-27 10:19:37 +01:00
int tc = material_ptr - > textures . size ( ) ;
Pair < StringName , RID > * textures = material_ptr - > textures . ptrw ( ) ;
2018-12-20 14:09:07 +01:00
2020-03-27 10:19:37 +01:00
ShaderLanguage : : ShaderNode : : Uniform : : Hint * texture_hints = shader_ptr - > texture_hints . ptrw ( ) ;
2018-12-20 14:09:07 +01:00
2020-03-27 10:19:37 +01:00
for ( int i = 0 ; i < tc ; i + + ) {
2023-08-09 15:05:25 +02:00
WRAPPED_GL_ACTIVE_TEXTURE ( GL_TEXTURE0 + i ) ;
2018-12-20 14:09:07 +01:00
2020-03-27 10:19:37 +01:00
RasterizerStorageGLES2 : : Texture * t = storage - > texture_owner . getornull ( textures [ i ] . second ) ;
2018-12-20 14:09:07 +01:00
2020-03-27 10:19:37 +01:00
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 ;
2021-08-14 19:14:59 +02:00
case ShaderLanguage : : ShaderNode : : Uniform : : HINT_TRANSPARENT : {
glBindTexture ( GL_TEXTURE_2D , storage - > resources . transparent_tex ) ;
} break ;
2020-03-27 10:19:37 +01:00
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 ;
}
2018-12-20 14:09:07 +01:00
2020-03-27 10:19:37 +01:00
continue ;
}
2018-12-20 14:09:07 +01:00
2020-03-27 10:19:37 +01:00
if ( t - > redraw_if_visible ) {
2022-06-12 07:44:59 +02:00
VisualServerRaster : : redraw_request ( false ) ;
2020-03-27 10:19:37 +01:00
}
2018-12-20 14:09:07 +01:00
2020-03-27 10:19:37 +01:00
t = t - > get_ptr ( ) ;
2018-12-20 14:09:07 +01:00
2020-03-27 10:19:37 +01:00
# ifdef TOOLS_ENABLED
if ( t - > detect_normal & & texture_hints [ i ] = = ShaderLanguage : : ShaderNode : : Uniform : : HINT_NORMAL ) {
t - > detect_normal ( t - > detect_normal_ud ) ;
}
# endif
2021-05-05 12:44:11 +02:00
if ( t - > render_target ) {
2020-03-27 10:19:37 +01:00
t - > render_target - > used_in_frame = true ;
2021-05-05 12:44:11 +02:00
}
2019-12-10 23:05:15 +01:00
2020-03-27 10:19:37 +01:00
glBindTexture ( t - > target , t - > tex_id ) ;
2019-12-10 23:05:15 +01:00
}
2020-03-27 10:19:37 +01:00
} else {
state . canvas_shader . set_custom_shader ( 0 ) ;
state . canvas_shader . bind ( ) ;
}
state . canvas_shader . use_material ( ( void * ) material_ptr ) ;
2019-12-10 23:05:15 +01:00
2020-03-27 10:19:37 +01:00
r_ris . shader_cache = shader_ptr ;
2018-12-20 14:09:07 +01:00
2020-03-27 10:19:37 +01:00
r_ris . canvas_last_material = material ;
r_ris . rebind_shader = false ;
}
2018-12-20 14:09:07 +01:00
2020-03-27 10:19:37 +01:00
int blend_mode = r_ris . shader_cache ? r_ris . shader_cache - > canvas_item . blend_mode : RasterizerStorageGLES2 : : Shader : : CanvasItem : : BLEND_MODE_MIX ;
bool unshaded = r_ris . shader_cache & & ( r_ris . shader_cache - > canvas_item . light_mode = = RasterizerStorageGLES2 : : Shader : : CanvasItem : : LIGHT_MODE_UNSHADED | | ( blend_mode ! = RasterizerStorageGLES2 : : Shader : : CanvasItem : : BLEND_MODE_MIX & & blend_mode ! = RasterizerStorageGLES2 : : Shader : : CanvasItem : : BLEND_MODE_PMALPHA ) ) ;
bool reclip = false ;
2018-12-20 14:09:07 +01:00
2020-03-27 10:19:37 +01:00
if ( r_ris . last_blend_mode ! = blend_mode ) {
switch ( blend_mode ) {
case RasterizerStorageGLES2 : : 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 ) ;
2018-12-20 14:09:07 +01:00
}
2020-03-27 10:19:37 +01:00
} break ;
case RasterizerStorageGLES2 : : 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 RasterizerStorageGLES2 : : 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 RasterizerStorageGLES2 : : 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 RasterizerStorageGLES2 : : 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 ;
2018-12-20 14:09:07 +01:00
}
}
2020-10-24 18:36:07 +02:00
// using software transform?
// (i.e. don't send the transform matrix, send identity, and either use baked verts,
// or large fvf where the transform is done in the shader from transform stored in the fvf.)
2021-03-17 16:09:50 +01:00
if ( ! p_bij . is_single_item ( ) ) {
2020-03-27 10:19:37 +01:00
state . uniforms . modelview_matrix = Transform2D ( ) ;
GLES2 2D batching - item reordering, light joining and light modulate fix
Although 2D draws in painters order with strict ordering, in certain circumstances items can be reordered to increase batching / decrease state changes, without affecting the end result. This can be determined by an overlap test.
In situation with item:
A-B-A
providing the third item does not overlap the second, they can be reordered:
A-A-B
Items already contain an AABB which can be used for this overlap test.
1)
To utilise this, I have implemented item reordering (only for single rects for now), with the lookahead adjustable in project settings. This can increase performance in situations where items may not be grouped in the scene tree by texture. It can also be switched off (by setting lookahead to 0).
2)
This same trick can be used to help join items that are lit. Lit items previously would prevent joining completely, thus missing out on performance gains other than multi-command items such as tilemaps.
In this PR, lights are assigned as bits in a bitfield (up to 64, the optimization is disabled above this), and on each try_item (for joining), the bitfield for lights and shadows is constructed and compared with the previous items. If these match the 2 items can potentially be joined. However, this can only be done without changing the rendered result if an overlap test is successful.
This overlap test can be adjusted to join items up to a specific number of item references, selectable in project settings, or turned off.
3)
The legacy uniform single rect drawing routine seems to have been identified as the source of flicker, particularly on nvidia. However, it can also be up to 2x as fast. Because of the speed the batching contains a fallback where it can use the legacy single rect method, but I have now added a project setting to make this switchable. In most cases with batching it should not be necessary (as single rects are drawn less frequently) and thus the flickering can be totally avoided.
4)
This PR also fixes a color modulate bug when drawing light passes, in certain situations (particularly custom _draw routines with multiple rects).
5)
This PR also fixes #38291, a bug in the legacy renderer where light passes could draw rects in wrong position.
2020-04-29 09:24:43 +02:00
// final_modulate will be baked per item ref so the final_modulate can be an identity color
2020-04-06 13:49:47 +02:00
state . uniforms . final_modulate = Color ( 1 , 1 , 1 , 1 ) ;
} else {
2020-03-27 10:19:37 +01:00
state . uniforms . modelview_matrix = ci - > final_transform ;
2020-04-06 13:49:47 +02:00
// could use the stored version of final_modulate in item ref? Test which is faster NYI
state . uniforms . final_modulate = unshaded ? ci - > final_modulate : ( ci - > final_modulate * r_ris . item_group_modulate ) ;
}
2020-03-27 10:19:37 +01:00
state . uniforms . extra_matrix = Transform2D ( ) ;
2017-12-04 13:41:34 +01:00
2020-03-27 10:19:37 +01:00
_set_uniforms ( ) ;
2017-12-04 13:41:34 +01:00
2021-05-05 12:44:11 +02:00
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 ) & & ! ci - > light_masked ) ) {
2021-05-04 16:00:45 +02:00
render_joined_item_commands ( p_bij , nullptr , reclip , material_ptr , false , r_ris ) ;
2021-05-05 12:44:11 +02:00
}
2017-12-04 13:41:34 +01:00
2020-03-27 10:19:37 +01:00
r_ris . rebind_shader = true ; // hacked in for now.
2017-12-04 13:41:34 +01:00
2020-03-27 10:19:37 +01:00
if ( ( blend_mode = = RasterizerStorageGLES2 : : Shader : : CanvasItem : : BLEND_MODE_MIX | | blend_mode = = RasterizerStorageGLES2 : : 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 ;
GLES2 2D batching - item reordering, light joining and light modulate fix
Although 2D draws in painters order with strict ordering, in certain circumstances items can be reordered to increase batching / decrease state changes, without affecting the end result. This can be determined by an overlap test.
In situation with item:
A-B-A
providing the third item does not overlap the second, they can be reordered:
A-A-B
Items already contain an AABB which can be used for this overlap test.
1)
To utilise this, I have implemented item reordering (only for single rects for now), with the lookahead adjustable in project settings. This can increase performance in situations where items may not be grouped in the scene tree by texture. It can also be switched off (by setting lookahead to 0).
2)
This same trick can be used to help join items that are lit. Lit items previously would prevent joining completely, thus missing out on performance gains other than multi-command items such as tilemaps.
In this PR, lights are assigned as bits in a bitfield (up to 64, the optimization is disabled above this), and on each try_item (for joining), the bitfield for lights and shadows is constructed and compared with the previous items. If these match the 2 items can potentially be joined. However, this can only be done without changing the rendered result if an overlap test is successful.
This overlap test can be adjusted to join items up to a specific number of item references, selectable in project settings, or turned off.
3)
The legacy uniform single rect drawing routine seems to have been identified as the source of flicker, particularly on nvidia. However, it can also be up to 2x as fast. Because of the speed the batching contains a fallback where it can use the legacy single rect method, but I have now added a project setting to make this switchable. In most cases with batching it should not be necessary (as single rects are drawn less frequently) and thus the flickering can be totally avoided.
4)
This PR also fixes a color modulate bug when drawing light passes, in certain situations (particularly custom _draw routines with multiple rects).
5)
This PR also fixes #38291, a bug in the legacy renderer where light passes could draw rects in wrong position.
2020-04-29 09:24:43 +02:00
// we leave this set to 1, 1, 1, 1 if using software because the colors are baked into the vertices
2021-03-17 16:09:50 +01:00
if ( p_bij . is_single_item ( ) ) {
GLES2 2D batching - item reordering, light joining and light modulate fix
Although 2D draws in painters order with strict ordering, in certain circumstances items can be reordered to increase batching / decrease state changes, without affecting the end result. This can be determined by an overlap test.
In situation with item:
A-B-A
providing the third item does not overlap the second, they can be reordered:
A-A-B
Items already contain an AABB which can be used for this overlap test.
1)
To utilise this, I have implemented item reordering (only for single rects for now), with the lookahead adjustable in project settings. This can increase performance in situations where items may not be grouped in the scene tree by texture. It can also be switched off (by setting lookahead to 0).
2)
This same trick can be used to help join items that are lit. Lit items previously would prevent joining completely, thus missing out on performance gains other than multi-command items such as tilemaps.
In this PR, lights are assigned as bits in a bitfield (up to 64, the optimization is disabled above this), and on each try_item (for joining), the bitfield for lights and shadows is constructed and compared with the previous items. If these match the 2 items can potentially be joined. However, this can only be done without changing the rendered result if an overlap test is successful.
This overlap test can be adjusted to join items up to a specific number of item references, selectable in project settings, or turned off.
3)
The legacy uniform single rect drawing routine seems to have been identified as the source of flicker, particularly on nvidia. However, it can also be up to 2x as fast. Because of the speed the batching contains a fallback where it can use the legacy single rect method, but I have now added a project setting to make this switchable. In most cases with batching it should not be necessary (as single rects are drawn less frequently) and thus the flickering can be totally avoided.
4)
This PR also fixes a color modulate bug when drawing light passes, in certain situations (particularly custom _draw routines with multiple rects).
5)
This PR also fixes #38291, a bug in the legacy renderer where light passes could draw rects in wrong position.
2020-04-29 09:24:43 +02:00
state . uniforms . final_modulate = ci - > final_modulate ; // remove the canvas modulate
}
2018-07-31 23:52:55 +02:00
2020-03-27 10:19:37 +01:00
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
2020-04-23 12:50:49 +02:00
// 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 ( 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 ) ) {
2020-03-27 10:19:37 +01:00
//intersects this light
2018-09-30 22:51:50 +02:00
2020-03-27 10:19:37 +01:00
if ( ! light_used | | mode ! = light - > mode ) {
mode = light - > mode ;
2018-09-30 22:51:50 +02:00
2020-03-27 10:19:37 +01:00
switch ( mode ) {
case VS : : CANVAS_LIGHT_MODE_ADD : {
glBlendEquation ( GL_FUNC_ADD ) ;
glBlendFunc ( GL_SRC_ALPHA , GL_ONE ) ;
2018-09-30 22:51:50 +02:00
2020-03-27 10:19:37 +01:00
} 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 ) ;
2018-09-30 22:51:50 +02:00
2020-03-27 10:19:37 +01:00
} break ;
}
}
2019-01-14 23:00:36 +01:00
2020-03-27 10:19:37 +01:00
if ( ! light_used ) {
state . canvas_shader . set_conditional ( CanvasShaderGLES2 : : USE_LIGHTING , true ) ;
light_used = true ;
}
2019-01-14 23:00:36 +01:00
2020-03-27 10:19:37 +01:00
bool has_shadow = light - > shadow_buffer . is_valid ( ) & & ci - > light_mask & light - > item_shadow_mask ;
state . canvas_shader . set_conditional ( CanvasShaderGLES2 : : USE_SHADOWS , has_shadow ) ;
if ( has_shadow ) {
state . canvas_shader . set_conditional ( CanvasShaderGLES2 : : SHADOW_USE_GRADIENT , light - > shadow_gradient_length > 0 ) ;
state . canvas_shader . set_conditional ( CanvasShaderGLES2 : : SHADOW_FILTER_NEAREST , light - > shadow_filter = = VS : : CANVAS_LIGHT_FILTER_NONE ) ;
state . canvas_shader . set_conditional ( CanvasShaderGLES2 : : SHADOW_FILTER_PCF3 , light - > shadow_filter = = VS : : CANVAS_LIGHT_FILTER_PCF3 ) ;
state . canvas_shader . set_conditional ( CanvasShaderGLES2 : : SHADOW_FILTER_PCF5 , light - > shadow_filter = = VS : : CANVAS_LIGHT_FILTER_PCF5 ) ;
state . canvas_shader . set_conditional ( CanvasShaderGLES2 : : SHADOW_FILTER_PCF7 , light - > shadow_filter = = VS : : CANVAS_LIGHT_FILTER_PCF7 ) ;
state . canvas_shader . set_conditional ( CanvasShaderGLES2 : : SHADOW_FILTER_PCF9 , light - > shadow_filter = = VS : : CANVAS_LIGHT_FILTER_PCF9 ) ;
state . canvas_shader . set_conditional ( CanvasShaderGLES2 : : SHADOW_FILTER_PCF13 , light - > shadow_filter = = VS : : CANVAS_LIGHT_FILTER_PCF13 ) ;
}
2019-01-14 23:00:36 +01:00
2020-03-27 10:19:37 +01:00
state . canvas_shader . bind ( ) ;
state . using_light = light ;
state . using_shadow = has_shadow ;
2019-01-14 23:00:36 +01:00
2020-03-27 10:19:37 +01:00
//always re-set uniforms, since light parameters changed
_set_uniforms ( ) ;
state . canvas_shader . use_material ( ( void * ) material_ptr ) ;
2019-01-14 23:00:36 +01:00
2023-08-09 15:05:25 +02:00
WRAPPED_GL_ACTIVE_TEXTURE ( GL_TEXTURE0 + storage - > config . max_texture_image_units - 6 ) ;
2020-03-27 10:19:37 +01:00
RasterizerStorageGLES2 : : Texture * t = storage - > texture_owner . getornull ( light - > texture ) ;
if ( ! t ) {
glBindTexture ( GL_TEXTURE_2D , storage - > resources . white_tex ) ;
} else {
t = t - > get_ptr ( ) ;
2019-01-14 23:00:36 +01:00
2020-03-27 10:19:37 +01:00
glBindTexture ( t - > target , t - > tex_id ) ;
}
2019-01-14 23:00:36 +01:00
2023-08-09 15:05:25 +02:00
WRAPPED_GL_ACTIVE_TEXTURE ( GL_TEXTURE0 ) ;
2019-01-14 23:00:36 +01:00
2020-03-27 10:19:37 +01:00
// 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 ) {
2021-05-04 16:00:45 +02:00
render_joined_item_commands ( p_bij , nullptr , reclip , material_ptr , true , r_ris ) ;
2020-03-27 10:19:37 +01:00
} else {
bool scissor = _light_scissor_begin ( p_bij . bounding_rect , light - > xform_cache , light - > rect_cache ) ;
2021-05-04 16:00:45 +02:00
render_joined_item_commands ( p_bij , nullptr , reclip , material_ptr , true , r_ris ) ;
2020-03-27 10:19:37 +01:00
if ( scissor ) {
glDisable ( GL_SCISSOR_TEST ) ;
}
}
2019-01-14 23:00:36 +01:00
2021-05-04 16:00:45 +02:00
state . using_light = nullptr ;
2020-03-27 10:19:37 +01:00
}
light = light - > next_ptr ;
}
if ( light_used ) {
state . canvas_shader . set_conditional ( CanvasShaderGLES2 : : USE_LIGHTING , false ) ;
state . canvas_shader . set_conditional ( CanvasShaderGLES2 : : USE_SHADOWS , false ) ;
state . canvas_shader . set_conditional ( CanvasShaderGLES2 : : SHADOW_FILTER_NEAREST , false ) ;
state . canvas_shader . set_conditional ( CanvasShaderGLES2 : : SHADOW_FILTER_PCF3 , false ) ;
state . canvas_shader . set_conditional ( CanvasShaderGLES2 : : SHADOW_FILTER_PCF5 , false ) ;
state . canvas_shader . set_conditional ( CanvasShaderGLES2 : : SHADOW_FILTER_PCF7 , false ) ;
state . canvas_shader . set_conditional ( CanvasShaderGLES2 : : SHADOW_FILTER_PCF9 , false ) ;
state . canvas_shader . set_conditional ( CanvasShaderGLES2 : : SHADOW_FILTER_PCF13 , false ) ;
2019-01-14 23:00:36 +01:00
2020-03-27 10:19:37 +01:00
state . canvas_shader . bind ( ) ;
2019-01-14 23:00:36 +01:00
2020-03-27 10:19:37 +01:00
r_ris . last_blend_mode = - 1 ;
2019-01-14 23:00:36 +01:00
2020-03-27 10:19:37 +01:00
/*
//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 ) ;
2019-01-14 23:00:36 +01:00
2020-10-14 09:32:05 +02:00
2020-03-27 10:19:37 +01:00
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 ) ;
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
*/
}
2019-01-14 23:00:36 +01:00
}
2019-02-12 13:54:53 +01:00
2020-03-27 10:19:37 +01:00
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 ) ;
2021-05-05 12:44:11 +02:00
if ( storage - > frame . current_rt - > flags [ RasterizerStorage : : RENDER_TARGET_VFLIP ] ) {
2020-03-27 10:19:37 +01:00
y = r_ris . current_clip - > final_clip_rect . position . y ;
2021-05-05 12:44:11 +02:00
}
2020-03-27 10:19:37 +01:00
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 ) ;
}
2017-12-04 13:41:34 +01:00
}
2020-10-14 09:32:05 +02:00
void RasterizerCanvasGLES2 : : gl_enable_scissor ( int p_x , int p_y , int p_width , int p_height ) const {
2020-03-27 10:19:37 +01:00
glEnable ( GL_SCISSOR_TEST ) ;
2020-10-14 09:32:05 +02:00
glScissor ( p_x , p_y , p_width , p_height ) ;
2020-03-27 10:19:37 +01:00
}
2017-12-04 13:41:34 +01:00
2020-10-14 09:32:05 +02:00
void RasterizerCanvasGLES2 : : gl_disable_scissor ( ) const {
glDisable ( GL_SCISSOR_TEST ) ;
2020-03-27 10:19:37 +01:00
}
2017-12-04 13:41:34 +01:00
2020-03-27 10:19:37 +01:00
void RasterizerCanvasGLES2 : : initialize ( ) {
2021-06-21 11:11:33 +02:00
RasterizerGLES2 : : gl_check_errors ( ) ;
2020-03-27 10:19:37 +01:00
RasterizerCanvasBaseGLES2 : : initialize ( ) ;
2017-12-04 13:41:34 +01:00
2020-10-14 09:32:05 +02:00
batch_initialize ( ) ;
2018-08-20 02:10:14 +02:00
2020-03-27 10:19:37 +01:00
// just reserve some space (may not be needed as we are orphaning, but hey ho)
glGenBuffers ( 1 , & bdata . gl_vertex_buffer ) ;
2018-09-30 22:51:50 +02:00
2020-03-27 10:19:37 +01:00
if ( bdata . vertex_buffer_size_bytes ) {
glBindBuffer ( GL_ARRAY_BUFFER , bdata . gl_vertex_buffer ) ;
2021-05-04 16:00:45 +02:00
glBufferData ( GL_ARRAY_BUFFER , bdata . vertex_buffer_size_bytes , nullptr , GL_DYNAMIC_DRAW ) ;
2020-03-27 10:19:37 +01:00
glBindBuffer ( GL_ARRAY_BUFFER , 0 ) ;
2018-10-02 15:53:24 +02:00
2020-03-27 10:19:37 +01:00
// 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 ) ;
Vector < uint16_t > indices ;
indices . resize ( bdata . index_buffer_size_units ) ;
2020-10-14 09:32:05 +02:00
for ( unsigned int q = 0 ; q < bdata . max_quads ; q + + ) {
2020-03-27 10:19:37 +01:00
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 ) ;
// we can only use 16 bit indices in GLES2!
# ifdef DEBUG_ENABLED
CRASH_COND ( ( q_pos + 3 ) > 65535 ) ;
# endif
}
2019-01-14 16:03:33 +01:00
2020-03-27 10:19:37 +01:00
glBufferData ( GL_ELEMENT_ARRAY_BUFFER , bdata . index_buffer_size_bytes , & indices [ 0 ] , GL_STATIC_DRAW ) ;
glBindBuffer ( GL_ELEMENT_ARRAY_BUFFER , 0 ) ;
2017-12-04 13:41:34 +01:00
2020-03-27 10:19:37 +01:00
} // only if there is a vertex buffer (batching is on)
2021-06-21 11:11:33 +02:00
RasterizerGLES2 : : gl_check_errors ( ) ;
2017-12-04 13:41:34 +01:00
}
RasterizerCanvasGLES2 : : RasterizerCanvasGLES2 ( ) {
2020-10-14 09:32:05 +02:00
batch_constructor ( ) ;
2017-12-04 13:41:34 +01:00
}