2017-03-05 15:47:28 +01:00
/*************************************************************************/
/* rasterizer_canvas_gles3.cpp */
/*************************************************************************/
/* This file is part of: */
/* GODOT ENGINE */
2017-08-27 14:16:55 +02:00
/* https://godotengine.org */
2017-03-05 15:47:28 +01:00
/*************************************************************************/
2021-01-01 20:13:46 +01:00
/* Copyright (c) 2007-2021 Juan Linietsky, Ariel Manzur. */
/* Copyright (c) 2014-2021 Godot Engine contributors (cf. AUTHORS.md). */
2017-03-05 15:47:28 +01:00
/* */
/* 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-01-05 00:50:27 +01:00
2016-10-03 21:33:42 +02:00
# include "rasterizer_canvas_gles3.h"
2020-11-22 12:24:29 +01:00
# include "drivers/gles_common/rasterizer_asserts.h"
2017-06-27 03:58:03 +02:00
# include "servers/visual/visual_server_raster.h"
2018-09-11 18:13:45 +02:00
2020-10-14 09:32:05 +02: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-01-07 09:33:11 +01:00
2020-10-14 09:32:05 +02:00
void RasterizerCanvasGLES3 : : canvas_end ( ) {
batch_canvas_end ( ) ;
RasterizerCanvasBaseGLES3 : : canvas_end ( ) ;
2016-10-03 21:33:42 +02:00
}
2020-10-14 09:32:05 +02:00
void RasterizerCanvasGLES3 : : canvas_begin ( ) {
batch_canvas_begin ( ) ;
RasterizerCanvasBaseGLES3 : : canvas_begin ( ) ;
2016-10-03 21:33:42 +02:00
}
2020-10-14 09:32:05 +02:00
void RasterizerCanvasGLES3 : : canvas_render_items_begin ( const Color & p_modulate , Light * p_light , const Transform2D & p_base_transform ) {
batch_canvas_render_items_begin ( p_modulate , p_light , p_base_transform ) ;
2016-10-03 21:33:42 +02:00
}
2020-10-14 09:32:05 +02:00
void RasterizerCanvasGLES3 : : canvas_render_items_end ( ) {
batch_canvas_render_items_end ( ) ;
2016-10-03 21:33:42 +02:00
}
2020-10-14 09:32:05 +02:00
void RasterizerCanvasGLES3 : : canvas_render_items ( Item * p_item_list , int p_z , const Color & p_modulate , Light * p_light , const Transform2D & p_base_transform ) {
batch_canvas_render_items ( p_item_list , p_z , p_modulate , p_light , p_base_transform ) ;
2016-10-03 21:33:42 +02:00
}
2020-10-14 09:32:05 +02:00
void RasterizerCanvasGLES3 : : gl_checkerror ( ) {
GLenum e = glGetError ( ) ;
CRASH_COND ( e ! = GL_NO_ERROR ) ;
2016-10-03 21:33:42 +02:00
}
2020-10-14 09:32:05 +02:00
void RasterizerCanvasGLES3 : : gl_enable_scissor ( int p_x , int p_y , int p_width , int p_height ) const {
glEnable ( GL_SCISSOR_TEST ) ;
glScissor ( p_x , p_y , p_width , p_height ) ;
2016-10-03 21:33:42 +02:00
}
2020-10-14 09:32:05 +02:00
void RasterizerCanvasGLES3 : : gl_disable_scissor ( ) const {
glDisable ( GL_SCISSOR_TEST ) ;
2016-10-03 21:33:42 +02:00
}
2020-10-14 09:32:05 +02:00
// Legacy non-batched implementation for regression testing.
// Should be removed after testing phase to avoid duplicate codepaths.
void RasterizerCanvasGLES3 : : _legacy_canvas_render_item ( Item * p_ci , RenderItemState & r_ris ) {
storage - > info . render . _2d_item_count + + ;
2016-10-03 21:33:42 +02:00
2020-10-14 09:32:05 +02:00
if ( r_ris . prev_distance_field ! = p_ci - > distance_field ) {
2016-10-03 21:33:42 +02:00
2020-10-14 09:32:05 +02:00
state . canvas_shader . set_conditional ( CanvasShaderGLES3 : : USE_DISTANCE_FIELD , p_ci - > distance_field ) ;
r_ris . prev_distance_field = p_ci - > distance_field ;
r_ris . rebind_shader = true ;
}
2016-10-03 21:33:42 +02:00
2020-10-14 09:32:05 +02:00
if ( r_ris . current_clip ! = p_ci - > final_clip_owner ) {
2016-10-03 21:33:42 +02:00
2020-10-14 09:32:05 +02:00
r_ris . current_clip = p_ci - > final_clip_owner ;
2016-10-03 21:33:42 +02:00
2020-10-14 09:32:05 +02:00
//setup clip
if ( r_ris . current_clip ) {
2016-10-03 21:33:42 +02:00
2020-10-14 09:32:05 +02:00
glEnable ( GL_SCISSOR_TEST ) ;
int y = storage - > frame . current_rt - > height - ( r_ris . current_clip - > final_clip_rect . position . y + r_ris . current_clip - > final_clip_rect . size . y ) ;
if ( storage - > frame . current_rt - > flags [ RasterizerStorage : : RENDER_TARGET_VFLIP ] )
y = r_ris . current_clip - > final_clip_rect . position . y ;
2019-03-03 17:43:54 +01:00
2020-10-14 09:32:05 +02:00
glScissor ( r_ris . current_clip - > final_clip_rect . position . x , y , r_ris . current_clip - > final_clip_rect . size . x , r_ris . current_clip - > final_clip_rect . size . y ) ;
2016-10-03 21:33:42 +02:00
2020-10-14 09:32:05 +02:00
} else {
2017-06-18 04:26:49 +02:00
2020-10-14 09:32:05 +02:00
glDisable ( GL_SCISSOR_TEST ) ;
2017-06-18 04:26:49 +02:00
}
2016-10-03 21:33:42 +02:00
}
2020-10-14 09:32:05 +02:00
if ( p_ci - > copy_back_buffer ) {
2017-06-18 04:26:49 +02:00
2020-10-14 09:32:05 +02:00
if ( p_ci - > copy_back_buffer - > full ) {
2017-06-18 04:26:49 +02:00
2020-10-14 09:32:05 +02:00
_copy_texscreen ( Rect2 ( ) ) ;
2017-06-18 04:26:49 +02:00
} else {
2020-10-14 09:32:05 +02:00
_copy_texscreen ( p_ci - > copy_back_buffer - > rect ) ;
2017-06-18 04:26:49 +02:00
}
}
2020-10-14 09:32:05 +02:00
RasterizerStorageGLES3 : : Skeleton * skeleton = NULL ;
2016-10-03 21:33:42 +02:00
2020-10-14 09:32:05 +02: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 ) {
skeleton = NULL ;
} else {
state . skeleton_transform = r_ris . item_group_base_transform * skeleton - > base_transform_2d ;
state . skeleton_transform_inverse = state . skeleton_transform . affine_inverse ( ) ;
}
}
2016-10-03 21:33:42 +02:00
2020-10-14 09:32:05 +02:00
bool use_skeleton = skeleton ! = NULL ;
if ( r_ris . prev_use_skeleton ! = use_skeleton ) {
r_ris . rebind_shader = true ;
state . canvas_shader . set_conditional ( CanvasShaderGLES3 : : USE_SKELETON , use_skeleton ) ;
r_ris . prev_use_skeleton = use_skeleton ;
}
2016-10-03 21:33:42 +02:00
2020-10-14 09:32:05 +02:00
if ( skeleton ) {
glActiveTexture ( GL_TEXTURE0 + storage - > config . max_texture_image_units - 4 ) ;
glBindTexture ( GL_TEXTURE_2D , skeleton - > texture ) ;
state . using_skeleton = true ;
} else {
state . using_skeleton = false ;
}
2016-10-03 21:33:42 +02:00
}
2020-10-14 09:32:05 +02:00
//begin rect
Item * material_owner = p_ci - > material_owner ? p_ci - > material_owner : p_ci ;
2016-10-03 21:33:42 +02:00
2020-10-14 09:32:05 +02:00
RID material = material_owner - > material ;
2016-10-03 21:33:42 +02:00
2020-10-14 09:32:05 +02:00
if ( material ! = r_ris . canvas_last_material | | r_ris . rebind_shader ) {
2016-10-03 21:33:42 +02:00
2020-10-14 09:32:05 +02:00
RasterizerStorageGLES3 : : Material * material_ptr = storage - > material_owner . getornull ( material ) ;
RasterizerStorageGLES3 : : Shader * shader_ptr = NULL ;
2016-10-03 21:33:42 +02:00
2020-10-14 09:32:05 +02:00
if ( material_ptr ) {
2016-10-03 21:33:42 +02:00
2020-10-14 09:32:05 +02:00
shader_ptr = material_ptr - > shader ;
2016-10-03 21:33:42 +02:00
2020-10-14 09:32:05 +02:00
if ( shader_ptr & & shader_ptr - > mode ! = VS : : SHADER_CANVAS_ITEM ) {
shader_ptr = NULL ; //do not use non canvasitem shader
}
}
2016-10-03 21:33:42 +02:00
2020-10-14 09:32:05 +02:00
if ( shader_ptr ) {
2018-05-04 14:32:40 +02:00
2020-10-14 09:32:05 +02:00
if ( shader_ptr - > canvas_item . uses_screen_texture & & ! state . canvas_texscreen_used ) {
//copy if not copied before
_copy_texscreen ( Rect2 ( ) ) ;
2016-10-03 21:33:42 +02:00
2020-10-14 09:32:05 +02:00
// blend mode will have been enabled so make sure we disable it again later on
r_ris . last_blend_mode = r_ris . last_blend_mode ! = RasterizerStorageGLES3 : : Shader : : CanvasItem : : BLEND_MODE_DISABLED ? r_ris . last_blend_mode : - 1 ;
}
2016-10-03 21:33:42 +02:00
2020-10-14 09:32:05 +02:00
if ( shader_ptr ! = r_ris . shader_cache | | r_ris . rebind_shader ) {
2016-10-03 21:33:42 +02:00
2020-10-14 09:32:05 +02:00
if ( shader_ptr - > canvas_item . uses_time ) {
VisualServerRaster : : redraw_request ( ) ;
}
2018-05-04 14:32:40 +02:00
2020-10-14 09:32:05 +02:00
state . canvas_shader . set_custom_shader ( shader_ptr - > custom_code_id ) ;
state . canvas_shader . bind ( ) ;
}
2018-05-03 22:29:15 +02:00
2020-10-14 09:32:05 +02:00
if ( material_ptr - > ubo_id ) {
glBindBufferBase ( GL_UNIFORM_BUFFER , 2 , material_ptr - > ubo_id ) ;
}
2018-05-03 22:29:15 +02:00
2020-10-14 09:32:05 +02:00
int tc = material_ptr - > textures . size ( ) ;
RID * textures = material_ptr - > textures . ptrw ( ) ;
ShaderLanguage : : ShaderNode : : Uniform : : Hint * texture_hints = shader_ptr - > texture_hints . ptrw ( ) ;
2018-05-03 22:29:15 +02:00
2020-10-14 09:32:05 +02:00
for ( int i = 0 ; i < tc ; i + + ) {
2018-05-03 22:29:15 +02:00
2020-10-14 09:32:05 +02:00
glActiveTexture ( GL_TEXTURE2 + i ) ;
2018-05-04 14:32:40 +02:00
2020-10-14 09:32:05 +02:00
RasterizerStorageGLES3 : : Texture * t = storage - > texture_owner . getornull ( textures [ i ] ) ;
if ( ! t ) {
2016-10-03 21:33:42 +02:00
2020-10-14 09:32:05 +02:00
switch ( texture_hints [ i ] ) {
case ShaderLanguage : : ShaderNode : : Uniform : : HINT_BLACK_ALBEDO :
case ShaderLanguage : : ShaderNode : : Uniform : : HINT_BLACK : {
glBindTexture ( GL_TEXTURE_2D , storage - > resources . black_tex ) ;
} break ;
case ShaderLanguage : : ShaderNode : : Uniform : : HINT_ANISO : {
glBindTexture ( GL_TEXTURE_2D , storage - > resources . aniso_tex ) ;
} break ;
case ShaderLanguage : : ShaderNode : : Uniform : : HINT_NORMAL : {
glBindTexture ( GL_TEXTURE_2D , storage - > resources . normal_tex ) ;
} break ;
default : {
glBindTexture ( GL_TEXTURE_2D , storage - > resources . white_tex ) ;
} break ;
}
2016-10-03 21:33:42 +02:00
2020-10-14 09:32:05 +02:00
//check hints
2017-06-12 23:56:16 +02:00
2020-10-14 09:32:05 +02:00
continue ;
}
2018-05-03 22:29:15 +02:00
2020-10-14 09:32:05 +02:00
if ( t - > redraw_if_visible ) { //check before proxy, because this is usually used with proxies
VisualServerRaster : : redraw_request ( ) ;
}
2016-10-03 21:33:42 +02:00
2020-10-14 09:32:05 +02:00
t = t - > get_ptr ( ) ;
2017-07-01 02:30:17 +02:00
2020-10-14 09:32:05 +02:00
if ( storage - > config . srgb_decode_supported & & t - > using_srgb ) {
//no srgb in 2D
glTexParameteri ( t - > target , _TEXTURE_SRGB_DECODE_EXT , _SKIP_DECODE_EXT ) ;
t - > using_srgb = false ;
}
2017-07-01 02:30:17 +02:00
2020-10-14 09:32:05 +02:00
glBindTexture ( t - > target , t - > tex_id ) ;
}
2017-07-01 02:30:17 +02:00
2020-10-14 09:32:05 +02:00
} else {
state . canvas_shader . set_custom_shader ( 0 ) ;
state . canvas_shader . bind ( ) ;
}
2017-07-01 02:30:17 +02:00
2020-10-14 09:32:05 +02:00
r_ris . shader_cache = shader_ptr ;
2017-07-01 02:30:17 +02:00
2020-10-14 09:32:05 +02:00
r_ris . canvas_last_material = material ;
r_ris . rebind_shader = false ;
2017-07-01 02:30:17 +02:00
}
2020-10-14 09:32:05 +02:00
int blend_mode = r_ris . shader_cache ? r_ris . shader_cache - > canvas_item . blend_mode : RasterizerStorageGLES3 : : Shader : : CanvasItem : : BLEND_MODE_MIX ;
if ( blend_mode = = RasterizerStorageGLES3 : : Shader : : CanvasItem : : BLEND_MODE_DISABLED & & ( ! storage - > frame . current_rt | | ! storage - > frame . current_rt - > flags [ RasterizerStorage : : RENDER_TARGET_TRANSPARENT ] ) ) {
blend_mode = RasterizerStorageGLES3 : : Shader : : CanvasItem : : BLEND_MODE_MIX ;
2017-07-01 02:30:17 +02:00
}
2020-10-14 09:32:05 +02:00
bool unshaded = r_ris . shader_cache & & ( r_ris . shader_cache - > canvas_item . light_mode = = RasterizerStorageGLES3 : : Shader : : CanvasItem : : LIGHT_MODE_UNSHADED | | ( blend_mode ! = RasterizerStorageGLES3 : : Shader : : CanvasItem : : BLEND_MODE_MIX & & blend_mode ! = RasterizerStorageGLES3 : : Shader : : CanvasItem : : BLEND_MODE_PMALPHA ) ) ;
bool reclip = false ;
if ( r_ris . last_blend_mode ! = blend_mode ) {
if ( r_ris . last_blend_mode = = RasterizerStorageGLES3 : : Shader : : CanvasItem : : BLEND_MODE_DISABLED ) {
// re-enable it
glEnable ( GL_BLEND ) ;
} else if ( blend_mode = = RasterizerStorageGLES3 : : Shader : : CanvasItem : : BLEND_MODE_DISABLED ) {
// disable it
glDisable ( GL_BLEND ) ;
}
2017-07-01 02:30:17 +02:00
2020-10-14 09:32:05 +02:00
switch ( blend_mode ) {
2017-07-01 02:30:17 +02:00
2020-10-14 09:32:05 +02:00
case RasterizerStorageGLES3 : : Shader : : CanvasItem : : BLEND_MODE_DISABLED : {
2017-07-01 02:30:17 +02:00
2020-10-14 09:32:05 +02:00
// nothing to do here
2019-11-24 11:00:02 +01:00
2020-10-14 09:32:05 +02:00
} break ;
case RasterizerStorageGLES3 : : Shader : : CanvasItem : : BLEND_MODE_MIX : {
2019-11-24 11:00:02 +01:00
2020-10-14 09:32:05 +02:00
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 ) ;
}
2019-11-24 11:00:02 +01:00
2020-10-14 09:32:05 +02:00
} break ;
case RasterizerStorageGLES3 : : Shader : : CanvasItem : : BLEND_MODE_ADD : {
2019-11-24 11:00:02 +01:00
2020-10-14 09:32:05 +02:00
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 ) ;
}
2019-11-24 11:00:02 +01:00
2020-10-14 09:32:05 +02:00
} break ;
case RasterizerStorageGLES3 : : Shader : : CanvasItem : : BLEND_MODE_SUB : {
2019-11-24 11:00:02 +01:00
2020-10-14 09:32:05 +02:00
glBlendEquation ( GL_FUNC_REVERSE_SUBTRACT ) ;
if ( storage - > frame . current_rt & & storage - > frame . current_rt - > flags [ RasterizerStorage : : RENDER_TARGET_TRANSPARENT ] ) {
glBlendFuncSeparate ( GL_SRC_ALPHA , GL_ONE , GL_SRC_ALPHA , GL_ONE ) ;
} else {
glBlendFuncSeparate ( GL_SRC_ALPHA , GL_ONE , GL_ZERO , GL_ONE ) ;
}
} break ;
case RasterizerStorageGLES3 : : Shader : : CanvasItem : : BLEND_MODE_MUL : {
glBlendEquation ( GL_FUNC_ADD ) ;
if ( storage - > frame . current_rt & & storage - > frame . current_rt - > flags [ RasterizerStorage : : RENDER_TARGET_TRANSPARENT ] ) {
glBlendFuncSeparate ( GL_DST_COLOR , GL_ZERO , GL_DST_ALPHA , GL_ZERO ) ;
} else {
glBlendFuncSeparate ( GL_DST_COLOR , GL_ZERO , GL_ZERO , GL_ONE ) ;
}
2019-11-24 11:00:02 +01:00
2020-10-14 09:32:05 +02:00
} break ;
case RasterizerStorageGLES3 : : Shader : : CanvasItem : : BLEND_MODE_PMALPHA : {
glBlendEquation ( GL_FUNC_ADD ) ;
if ( storage - > frame . current_rt & & storage - > frame . current_rt - > flags [ RasterizerStorage : : RENDER_TARGET_TRANSPARENT ] ) {
glBlendFuncSeparate ( GL_ONE , GL_ONE_MINUS_SRC_ALPHA , GL_ONE , GL_ONE_MINUS_SRC_ALPHA ) ;
} else {
glBlendFuncSeparate ( GL_ONE , GL_ONE_MINUS_SRC_ALPHA , GL_ZERO , GL_ONE ) ;
}
2019-11-24 11:00:02 +01:00
2020-10-14 09:32:05 +02:00
} break ;
}
2019-11-24 11:00:02 +01:00
2020-10-14 09:32:05 +02:00
r_ris . last_blend_mode = blend_mode ;
2019-11-24 11:00:02 +01:00
}
2020-10-14 09:32:05 +02:00
state . canvas_item_modulate = unshaded ? p_ci - > final_modulate : Color ( p_ci - > final_modulate . r * r_ris . item_group_modulate . r , p_ci - > final_modulate . g * r_ris . item_group_modulate . g , p_ci - > final_modulate . b * r_ris . item_group_modulate . b , p_ci - > final_modulate . a * r_ris . item_group_modulate . a ) ;
2016-10-03 21:33:42 +02:00
2020-10-14 09:32:05 +02:00
state . final_transform = p_ci - > final_transform ;
state . extra_matrix = Transform2D ( ) ;
2016-10-03 21:33:42 +02:00
2020-10-14 09:32:05 +02:00
if ( state . using_skeleton ) {
state . canvas_shader . set_uniform ( CanvasShaderGLES3 : : SKELETON_TRANSFORM , state . skeleton_transform ) ;
state . canvas_shader . set_uniform ( CanvasShaderGLES3 : : SKELETON_TRANSFORM_INVERSE , state . skeleton_transform_inverse ) ;
2016-10-03 21:33:42 +02:00
}
2020-10-14 09:32:05 +02:00
state . canvas_shader . set_uniform ( CanvasShaderGLES3 : : FINAL_MODULATE , state . canvas_item_modulate ) ;
state . canvas_shader . set_uniform ( CanvasShaderGLES3 : : MODELVIEW_MATRIX , state . final_transform ) ;
state . canvas_shader . set_uniform ( CanvasShaderGLES3 : : EXTRA_MATRIX , state . extra_matrix ) ;
if ( storage - > frame . current_rt ) {
state . canvas_shader . set_uniform ( CanvasShaderGLES3 : : SCREEN_PIXEL_SIZE , Vector2 ( 1.0 / storage - > frame . current_rt - > width , 1.0 / storage - > frame . current_rt - > height ) ) ;
} else {
state . canvas_shader . set_uniform ( CanvasShaderGLES3 : : SCREEN_PIXEL_SIZE , Vector2 ( 1.0 , 1.0 ) ) ;
2016-10-03 21:33:42 +02:00
}
2020-10-14 09:32:05 +02:00
if ( unshaded | | ( state . canvas_item_modulate . a > 0.001 & & ( ! r_ris . shader_cache | | r_ris . shader_cache - > canvas_item . light_mode ! = RasterizerStorageGLES3 : : Shader : : CanvasItem : : LIGHT_MODE_LIGHT_ONLY ) & & ! p_ci - > light_masked ) )
_legacy_canvas_item_render_commands ( p_ci , r_ris . current_clip , reclip , nullptr ) ;
2016-10-03 21:33:42 +02:00
2020-10-14 09:32:05 +02:00
if ( ( blend_mode = = RasterizerStorageGLES3 : : Shader : : CanvasItem : : BLEND_MODE_MIX | | blend_mode = = RasterizerStorageGLES3 : : Shader : : CanvasItem : : BLEND_MODE_PMALPHA ) & & r_ris . item_group_light & & ! unshaded ) {
GLES3 fix normal map flipping with nvidia workaround
When not using TEXTURE_RECT path, flips have to sent via another method to the shader, to ensure that normal maps are correctly adjusted for direction. This PR adds an extra vertex attribute, LIGHT_ANGLE.
For nvidia workarounds, where the shader still has access to the final transform and extra matrix, the LIGHT_ANGLE can be 0 (no adjustment), 180 degrees for a horizontal flip, and negative indicates a vertical flip.
For batching path, the LIGHT_ANGLE can be used to directly specify the light angle for normal mapping, even when the final transform and extra matrix have been baked into vertex positions, so the same shader can be used for both.
2020-08-02 08:49:03 +02:00
2020-10-14 09:32:05 +02:00
Light * light = r_ris . item_group_light ;
bool light_used = false ;
VS : : CanvasLightMode mode = VS : : CANVAS_LIGHT_MODE_ADD ;
state . canvas_item_modulate = p_ci - > final_modulate ; // remove the canvas modulate
2016-10-03 21:33:42 +02:00
2020-10-14 09:32:05 +02:00
while ( light ) {
2016-10-03 21:33:42 +02:00
2020-10-14 09:32:05 +02:00
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 ) ) {
2016-10-03 21:33:42 +02:00
2020-10-14 09:32:05 +02:00
//intersects this light
2016-10-03 21:33:42 +02:00
2020-10-14 09:32:05 +02:00
if ( ! light_used | | mode ! = light - > mode ) {
2016-10-03 21:33:42 +02:00
2020-10-14 09:32:05 +02:00
mode = light - > mode ;
2016-10-03 21:33:42 +02:00
2020-10-14 09:32:05 +02:00
switch ( mode ) {
GLES3 fix normal map flipping with nvidia workaround
When not using TEXTURE_RECT path, flips have to sent via another method to the shader, to ensure that normal maps are correctly adjusted for direction. This PR adds an extra vertex attribute, LIGHT_ANGLE.
For nvidia workarounds, where the shader still has access to the final transform and extra matrix, the LIGHT_ANGLE can be 0 (no adjustment), 180 degrees for a horizontal flip, and negative indicates a vertical flip.
For batching path, the LIGHT_ANGLE can be used to directly specify the light angle for normal mapping, even when the final transform and extra matrix have been baked into vertex positions, so the same shader can be used for both.
2020-08-02 08:49:03 +02:00
2020-10-14 09:32:05 +02:00
case VS : : CANVAS_LIGHT_MODE_ADD : {
glBlendEquation ( GL_FUNC_ADD ) ;
glBlendFunc ( GL_SRC_ALPHA , GL_ONE ) ;
GLES3 fix normal map flipping with nvidia workaround
When not using TEXTURE_RECT path, flips have to sent via another method to the shader, to ensure that normal maps are correctly adjusted for direction. This PR adds an extra vertex attribute, LIGHT_ANGLE.
For nvidia workarounds, where the shader still has access to the final transform and extra matrix, the LIGHT_ANGLE can be 0 (no adjustment), 180 degrees for a horizontal flip, and negative indicates a vertical flip.
For batching path, the LIGHT_ANGLE can be used to directly specify the light angle for normal mapping, even when the final transform and extra matrix have been baked into vertex positions, so the same shader can be used for both.
2020-08-02 08:49:03 +02:00
2020-10-14 09:32:05 +02: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 ) ;
2020-10-12 11:06:30 +02:00
2020-10-14 09:32:05 +02:00
} break ;
}
}
2016-10-03 21:33:42 +02:00
2020-10-14 09:32:05 +02:00
if ( ! light_used ) {
2016-10-03 21:33:42 +02:00
2020-10-14 09:32:05 +02:00
state . canvas_shader . set_conditional ( CanvasShaderGLES3 : : USE_LIGHTING , true ) ;
light_used = true ;
}
2018-02-21 13:38:21 +01:00
2020-10-14 09:32:05 +02:00
bool has_shadow = light - > shadow_buffer . is_valid ( ) & & p_ci - > light_mask & light - > item_shadow_mask ;
state . canvas_shader . set_conditional ( CanvasShaderGLES3 : : USE_SHADOWS , has_shadow ) ;
if ( has_shadow ) {
state . canvas_shader . set_conditional ( CanvasShaderGLES3 : : SHADOW_USE_GRADIENT , light - > shadow_gradient_length > 0 ) ;
state . canvas_shader . set_conditional ( CanvasShaderGLES3 : : SHADOW_FILTER_NEAREST , light - > shadow_filter = = VS : : CANVAS_LIGHT_FILTER_NONE ) ;
state . canvas_shader . set_conditional ( CanvasShaderGLES3 : : SHADOW_FILTER_PCF3 , light - > shadow_filter = = VS : : CANVAS_LIGHT_FILTER_PCF3 ) ;
state . canvas_shader . set_conditional ( CanvasShaderGLES3 : : SHADOW_FILTER_PCF5 , light - > shadow_filter = = VS : : CANVAS_LIGHT_FILTER_PCF5 ) ;
state . canvas_shader . set_conditional ( CanvasShaderGLES3 : : SHADOW_FILTER_PCF7 , light - > shadow_filter = = VS : : CANVAS_LIGHT_FILTER_PCF7 ) ;
state . canvas_shader . set_conditional ( CanvasShaderGLES3 : : SHADOW_FILTER_PCF9 , light - > shadow_filter = = VS : : CANVAS_LIGHT_FILTER_PCF9 ) ;
state . canvas_shader . set_conditional ( CanvasShaderGLES3 : : SHADOW_FILTER_PCF13 , light - > shadow_filter = = VS : : CANVAS_LIGHT_FILTER_PCF13 ) ;
}
2020-05-05 17:25:45 +02:00
2020-10-14 09:32:05 +02:00
bool light_rebind = state . canvas_shader . bind ( ) ;
2020-05-05 17:25:45 +02:00
2020-10-14 09:32:05 +02:00
if ( light_rebind ) {
state . canvas_shader . set_uniform ( CanvasShaderGLES3 : : FINAL_MODULATE , state . canvas_item_modulate ) ;
state . canvas_shader . set_uniform ( CanvasShaderGLES3 : : MODELVIEW_MATRIX , state . final_transform ) ;
state . canvas_shader . set_uniform ( CanvasShaderGLES3 : : EXTRA_MATRIX , Transform2D ( ) ) ;
if ( storage - > frame . current_rt ) {
state . canvas_shader . set_uniform ( CanvasShaderGLES3 : : SCREEN_PIXEL_SIZE , Vector2 ( 1.0 / storage - > frame . current_rt - > width , 1.0 / storage - > frame . current_rt - > height ) ) ;
} else {
state . canvas_shader . set_uniform ( CanvasShaderGLES3 : : SCREEN_PIXEL_SIZE , Vector2 ( 1.0 , 1.0 ) ) ;
}
if ( state . using_skeleton ) {
state . canvas_shader . set_uniform ( CanvasShaderGLES3 : : SKELETON_TRANSFORM , state . skeleton_transform ) ;
state . canvas_shader . set_uniform ( CanvasShaderGLES3 : : SKELETON_TRANSFORM_INVERSE , state . skeleton_transform_inverse ) ;
}
}
GLES3 fix normal map flipping with nvidia workaround
When not using TEXTURE_RECT path, flips have to sent via another method to the shader, to ensure that normal maps are correctly adjusted for direction. This PR adds an extra vertex attribute, LIGHT_ANGLE.
For nvidia workarounds, where the shader still has access to the final transform and extra matrix, the LIGHT_ANGLE can be 0 (no adjustment), 180 degrees for a horizontal flip, and negative indicates a vertical flip.
For batching path, the LIGHT_ANGLE can be used to directly specify the light angle for normal mapping, even when the final transform and extra matrix have been baked into vertex positions, so the same shader can be used for both.
2020-08-02 08:49:03 +02:00
2020-10-14 09:32:05 +02:00
glBindBufferBase ( GL_UNIFORM_BUFFER , 1 , static_cast < LightInternal * > ( light - > light_internal . get_data ( ) ) - > ubo ) ;
GLES3 fix normal map flipping with nvidia workaround
When not using TEXTURE_RECT path, flips have to sent via another method to the shader, to ensure that normal maps are correctly adjusted for direction. This PR adds an extra vertex attribute, LIGHT_ANGLE.
For nvidia workarounds, where the shader still has access to the final transform and extra matrix, the LIGHT_ANGLE can be 0 (no adjustment), 180 degrees for a horizontal flip, and negative indicates a vertical flip.
For batching path, the LIGHT_ANGLE can be used to directly specify the light angle for normal mapping, even when the final transform and extra matrix have been baked into vertex positions, so the same shader can be used for both.
2020-08-02 08:49:03 +02:00
2020-10-14 09:32:05 +02:00
if ( has_shadow ) {
GLES3 fix normal map flipping with nvidia workaround
When not using TEXTURE_RECT path, flips have to sent via another method to the shader, to ensure that normal maps are correctly adjusted for direction. This PR adds an extra vertex attribute, LIGHT_ANGLE.
For nvidia workarounds, where the shader still has access to the final transform and extra matrix, the LIGHT_ANGLE can be 0 (no adjustment), 180 degrees for a horizontal flip, and negative indicates a vertical flip.
For batching path, the LIGHT_ANGLE can be used to directly specify the light angle for normal mapping, even when the final transform and extra matrix have been baked into vertex positions, so the same shader can be used for both.
2020-08-02 08:49:03 +02:00
2020-10-14 09:32:05 +02:00
RasterizerStorageGLES3 : : CanvasLightShadow * cls = storage - > canvas_light_shadow_owner . get ( light - > shadow_buffer ) ;
glActiveTexture ( GL_TEXTURE0 + storage - > config . max_texture_image_units - 2 ) ;
glBindTexture ( GL_TEXTURE_2D , cls - > distance ) ;
2020-05-05 17:25:45 +02:00
2020-10-14 09:32:05 +02:00
/*canvas_shader.set_uniform(CanvasShaderGLES3::SHADOW_MATRIX,light->shadow_matrix_cache);
canvas_shader . set_uniform ( CanvasShaderGLES3 : : SHADOW_ESM_MULTIPLIER , light - > shadow_esm_mult ) ;
canvas_shader . set_uniform ( CanvasShaderGLES3 : : LIGHT_SHADOW_COLOR , light - > shadow_color ) ; */
}
2020-05-05 17:25:45 +02:00
2020-10-14 09:32:05 +02:00
glActiveTexture ( GL_TEXTURE0 + storage - > config . max_texture_image_units - 1 ) ;
RasterizerStorageGLES3 : : Texture * t = storage - > texture_owner . getornull ( light - > texture ) ;
if ( ! t ) {
glBindTexture ( GL_TEXTURE_2D , storage - > resources . white_tex ) ;
} else {
t = t - > get_ptr ( ) ;
2020-05-05 17:25:45 +02:00
2020-10-14 09:32:05 +02:00
glBindTexture ( t - > target , t - > tex_id ) ;
}
2020-05-05 17:25:45 +02:00
2020-10-14 09:32:05 +02:00
glActiveTexture ( GL_TEXTURE0 ) ;
_legacy_canvas_item_render_commands ( p_ci , r_ris . current_clip , reclip , nullptr ) ; //redraw using light
}
2020-05-05 17:25:45 +02:00
2020-10-14 09:32:05 +02:00
light = light - > next_ptr ;
2020-05-05 17:25:45 +02:00
}
2020-10-14 09:32:05 +02:00
if ( light_used ) {
2020-05-05 17:25:45 +02:00
2020-10-14 09:32:05 +02:00
state . canvas_shader . set_conditional ( CanvasShaderGLES3 : : USE_LIGHTING , false ) ;
state . canvas_shader . set_conditional ( CanvasShaderGLES3 : : USE_SHADOWS , false ) ;
state . canvas_shader . set_conditional ( CanvasShaderGLES3 : : SHADOW_FILTER_NEAREST , false ) ;
state . canvas_shader . set_conditional ( CanvasShaderGLES3 : : SHADOW_FILTER_PCF3 , false ) ;
state . canvas_shader . set_conditional ( CanvasShaderGLES3 : : SHADOW_FILTER_PCF5 , false ) ;
state . canvas_shader . set_conditional ( CanvasShaderGLES3 : : SHADOW_FILTER_PCF7 , false ) ;
state . canvas_shader . set_conditional ( CanvasShaderGLES3 : : SHADOW_FILTER_PCF9 , false ) ;
state . canvas_shader . set_conditional ( CanvasShaderGLES3 : : SHADOW_FILTER_PCF13 , false ) ;
GLES3 fix normal map flipping with nvidia workaround
When not using TEXTURE_RECT path, flips have to sent via another method to the shader, to ensure that normal maps are correctly adjusted for direction. This PR adds an extra vertex attribute, LIGHT_ANGLE.
For nvidia workarounds, where the shader still has access to the final transform and extra matrix, the LIGHT_ANGLE can be 0 (no adjustment), 180 degrees for a horizontal flip, and negative indicates a vertical flip.
For batching path, the LIGHT_ANGLE can be used to directly specify the light angle for normal mapping, even when the final transform and extra matrix have been baked into vertex positions, so the same shader can be used for both.
2020-08-02 08:49:03 +02:00
2020-10-14 09:32:05 +02:00
state . canvas_shader . bind ( ) ;
2020-05-05 17:25:45 +02:00
2020-10-14 09:32:05 +02:00
r_ris . last_blend_mode = - 1 ;
2020-05-05 17:25:45 +02:00
2020-10-14 09:32:05 +02: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 ) ;
GLES3 fix normal map flipping with nvidia workaround
When not using TEXTURE_RECT path, flips have to sent via another method to the shader, to ensure that normal maps are correctly adjusted for direction. This PR adds an extra vertex attribute, LIGHT_ANGLE.
For nvidia workarounds, where the shader still has access to the final transform and extra matrix, the LIGHT_ANGLE can be 0 (no adjustment), 180 degrees for a horizontal flip, and negative indicates a vertical flip.
For batching path, the LIGHT_ANGLE can be used to directly specify the light angle for normal mapping, even when the final transform and extra matrix have been baked into vertex positions, so the same shader can be used for both.
2020-08-02 08:49:03 +02:00
2020-10-14 09:32:05 +02:00
state . canvas_shader . set_uniform ( CanvasShaderGLES3 : : MODELVIEW_MATRIX , state . final_transform ) ;
state . canvas_shader . set_uniform ( CanvasShaderGLES3 : : EXTRA_MATRIX , Transform2D ( ) ) ;
state . canvas_shader . set_uniform ( CanvasShaderGLES3 : : FINAL_MODULATE , state . canvas_item_modulate ) ;
GLES3 fix normal map flipping with nvidia workaround
When not using TEXTURE_RECT path, flips have to sent via another method to the shader, to ensure that normal maps are correctly adjusted for direction. This PR adds an extra vertex attribute, LIGHT_ANGLE.
For nvidia workarounds, where the shader still has access to the final transform and extra matrix, the LIGHT_ANGLE can be 0 (no adjustment), 180 degrees for a horizontal flip, and negative indicates a vertical flip.
For batching path, the LIGHT_ANGLE can be used to directly specify the light angle for normal mapping, even when the final transform and extra matrix have been baked into vertex positions, so the same shader can be used for both.
2020-08-02 08:49:03 +02:00
2020-10-14 09:32:05 +02:00
glBlendEquation ( GL_FUNC_ADD ) ;
GLES3 fix normal map flipping with nvidia workaround
When not using TEXTURE_RECT path, flips have to sent via another method to the shader, to ensure that normal maps are correctly adjusted for direction. This PR adds an extra vertex attribute, LIGHT_ANGLE.
For nvidia workarounds, where the shader still has access to the final transform and extra matrix, the LIGHT_ANGLE can be 0 (no adjustment), 180 degrees for a horizontal flip, and negative indicates a vertical flip.
For batching path, the LIGHT_ANGLE can be used to directly specify the light angle for normal mapping, even when the final transform and extra matrix have been baked into vertex positions, so the same shader can be used for both.
2020-08-02 08:49:03 +02:00
2020-10-14 09:32:05 +02: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 ) ;
}
GLES3 fix normal map flipping with nvidia workaround
When not using TEXTURE_RECT path, flips have to sent via another method to the shader, to ensure that normal maps are correctly adjusted for direction. This PR adds an extra vertex attribute, LIGHT_ANGLE.
For nvidia workarounds, where the shader still has access to the final transform and extra matrix, the LIGHT_ANGLE can be 0 (no adjustment), 180 degrees for a horizontal flip, and negative indicates a vertical flip.
For batching path, the LIGHT_ANGLE can be used to directly specify the light angle for normal mapping, even when the final transform and extra matrix have been baked into vertex positions, so the same shader can be used for both.
2020-08-02 08:49:03 +02:00
2020-10-14 09:32:05 +02:00
//@TODO RESET canvas_blend_mode
*/
GLES3 fix normal map flipping with nvidia workaround
When not using TEXTURE_RECT path, flips have to sent via another method to the shader, to ensure that normal maps are correctly adjusted for direction. This PR adds an extra vertex attribute, LIGHT_ANGLE.
For nvidia workarounds, where the shader still has access to the final transform and extra matrix, the LIGHT_ANGLE can be 0 (no adjustment), 180 degrees for a horizontal flip, and negative indicates a vertical flip.
For batching path, the LIGHT_ANGLE can be used to directly specify the light angle for normal mapping, even when the final transform and extra matrix have been baked into vertex positions, so the same shader can be used for both.
2020-08-02 08:49:03 +02:00
}
2020-10-14 09:32:05 +02:00
}
2020-05-05 17:25:45 +02:00
2020-10-14 09:32:05 +02:00
if ( reclip ) {
2020-05-05 17:25:45 +02:00
2020-10-14 09:32:05 +02:00
glEnable ( GL_SCISSOR_TEST ) ;
int y = storage - > frame . current_rt - > height - ( r_ris . current_clip - > final_clip_rect . position . y + r_ris . current_clip - > final_clip_rect . size . y ) ;
if ( storage - > frame . current_rt - > flags [ RasterizerStorage : : RENDER_TARGET_VFLIP ] )
y = r_ris . current_clip - > final_clip_rect . position . y ;
glScissor ( r_ris . current_clip - > final_clip_rect . position . x , y , r_ris . current_clip - > final_clip_rect . size . width , r_ris . current_clip - > final_clip_rect . size . height ) ;
}
}
GLES3 fix normal map flipping with nvidia workaround
When not using TEXTURE_RECT path, flips have to sent via another method to the shader, to ensure that normal maps are correctly adjusted for direction. This PR adds an extra vertex attribute, LIGHT_ANGLE.
For nvidia workarounds, where the shader still has access to the final transform and extra matrix, the LIGHT_ANGLE can be 0 (no adjustment), 180 degrees for a horizontal flip, and negative indicates a vertical flip.
For batching path, the LIGHT_ANGLE can be used to directly specify the light angle for normal mapping, even when the final transform and extra matrix have been baked into vertex positions, so the same shader can be used for both.
2020-08-02 08:49:03 +02:00
2020-10-14 09:32:05 +02:00
void RasterizerCanvasGLES3 : : render_batches ( Item : : Command * const * p_commands , Item * p_current_clip , bool & r_reclip , RasterizerStorageGLES3 : : Material * p_material ) {
// bdata.reset_flush();
// return;
2020-05-05 17:25:45 +02:00
2020-10-14 09:32:05 +02:00
int num_batches = bdata . batches . size ( ) ;
2020-05-05 17:25:45 +02:00
2020-10-14 09:32:05 +02:00
for ( int batch_num = 0 ; batch_num < num_batches ; batch_num + + ) {
const Batch & batch = bdata . batches [ batch_num ] ;
2020-05-05 17:25:45 +02:00
2020-10-14 09:32:05 +02:00
switch ( batch . type ) {
case RasterizerStorageCommon : : BT_RECT : {
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_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 ;
default : {
int end_command = batch . first_command + batch . num_commands ;
2016-10-03 21:33:42 +02:00
2020-10-14 09:32:05 +02:00
for ( int i = batch . first_command ; i < end_command ; i + + ) {
2016-10-03 21:33:42 +02:00
2020-10-14 09:32:05 +02:00
Item : : Command * c = p_commands [ i ] ;
2016-10-03 21:33:42 +02:00
2020-10-14 09:32:05 +02:00
switch ( c - > type ) {
2016-10-03 21:33:42 +02:00
2020-10-14 09:32:05 +02:00
case Item : : Command : : TYPE_LINE : {
2016-10-03 21:33:42 +02:00
2020-10-14 09:32:05 +02:00
Item : : CommandLine * line = static_cast < Item : : CommandLine * > ( c ) ;
_set_texture_rect_mode ( false ) ;
2016-10-03 21:33:42 +02:00
2020-10-14 09:32:05 +02:00
_bind_canvas_texture ( RID ( ) , RID ( ) ) ;
2016-10-03 21:33:42 +02:00
2020-10-14 09:32:05 +02:00
glVertexAttrib4f ( VS : : ARRAY_COLOR , line - > color . r , line - > color . g , line - > color . b , line - > color . a ) ;
2016-10-03 21:33:42 +02:00
2020-10-14 09:32:05 +02:00
if ( line - > width < = 1 ) {
Vector2 verts [ 2 ] = {
Vector2 ( line - > from . x , line - > from . y ) ,
Vector2 ( line - > to . x , line - > to . y )
} ;
2016-10-03 21:33:42 +02:00
2016-11-23 00:51:56 +01:00
# ifdef GLES_OVER_GL
2020-10-14 09:32:05 +02:00
if ( line - > antialiased )
glEnable ( GL_LINE_SMOOTH ) ;
2016-10-03 21:33:42 +02:00
# endif
2020-10-14 09:32:05 +02:00
//glLineWidth(line->width);
_draw_gui_primitive ( 2 , verts , NULL , NULL ) ;
2016-10-03 21:33:42 +02:00
2016-11-23 00:51:56 +01:00
# ifdef GLES_OVER_GL
2020-10-14 09:32:05 +02:00
if ( line - > antialiased )
glDisable ( GL_LINE_SMOOTH ) ;
2017-07-01 02:30:17 +02:00
# endif
2020-10-14 09:32:05 +02:00
} else {
//thicker line
2017-07-01 02:30:17 +02:00
2020-10-14 09:32:05 +02:00
Vector2 t = ( line - > from - line - > to ) . normalized ( ) . tangent ( ) * line - > width * 0.5 ;
2017-07-01 02:30:17 +02:00
2020-10-14 09:32:05 +02:00
Vector2 verts [ 4 ] = {
line - > from - t ,
line - > from + t ,
line - > to + t ,
line - > to - t ,
} ;
2017-07-01 02:30:17 +02:00
2020-10-14 09:32:05 +02:00
//glLineWidth(line->width);
_draw_gui_primitive ( 4 , verts , NULL , NULL ) ;
2017-07-01 02:30:17 +02:00
# ifdef GLES_OVER_GL
2020-10-14 09:32:05 +02:00
if ( line - > antialiased ) {
glEnable ( GL_LINE_SMOOTH ) ;
for ( int j = 0 ; j < 4 ; j + + ) {
Vector2 vertsl [ 2 ] = {
verts [ j ] ,
verts [ ( j + 1 ) % 4 ] ,
} ;
_draw_gui_primitive ( 2 , vertsl , NULL , NULL ) ;
}
glDisable ( GL_LINE_SMOOTH ) ;
}
2017-07-01 02:30:17 +02:00
# endif
2020-10-14 09:32:05 +02:00
}
} break ;
case Item : : Command : : TYPE_POLYLINE : {
2017-07-01 02:30:17 +02:00
2020-10-14 09:32:05 +02:00
Item : : CommandPolyLine * pline = static_cast < Item : : CommandPolyLine * > ( c ) ;
_set_texture_rect_mode ( false ) ;
2017-07-01 02:30:17 +02:00
2020-10-14 09:32:05 +02:00
_bind_canvas_texture ( RID ( ) , RID ( ) ) ;
2017-07-01 02:30:17 +02:00
2020-10-14 09:32:05 +02:00
if ( pline - > triangles . size ( ) ) {
2017-07-01 02:30:17 +02:00
2020-11-22 12:24:29 +01:00
# ifdef RASTERIZER_EXTRA_CHECKS
if ( pline - > triangle_colors . ptr ( ) & & ( pline - > triangle_colors . size ( ) ! = 1 ) ) {
RAST_DEV_DEBUG_ASSERT ( pline - > triangle_colors . size ( ) = = pline - > triangles . size ( ) ) ;
}
# endif
2020-10-14 09:32:05 +02:00
_draw_generic ( GL_TRIANGLE_STRIP , pline - > triangles . size ( ) , pline - > triangles . ptr ( ) , NULL , pline - > triangle_colors . ptr ( ) , pline - > triangle_colors . size ( ) = = 1 ) ;
2017-07-01 02:30:17 +02:00
# ifdef GLES_OVER_GL
2020-10-14 09:32:05 +02:00
glEnable ( GL_LINE_SMOOTH ) ;
if ( pline - > multiline ) {
//needs to be different
} else {
_draw_generic ( GL_LINE_LOOP , pline - > lines . size ( ) , pline - > lines . ptr ( ) , NULL , pline - > line_colors . ptr ( ) , pline - > line_colors . size ( ) = = 1 ) ;
}
glDisable ( GL_LINE_SMOOTH ) ;
2016-10-03 21:33:42 +02:00
# endif
2020-10-14 09:32:05 +02:00
} else {
2017-07-01 02:30:17 +02:00
# ifdef GLES_OVER_GL
2020-10-14 09:32:05 +02:00
if ( pline - > antialiased )
glEnable ( GL_LINE_SMOOTH ) ;
2017-07-01 02:30:17 +02:00
# endif
2017-12-09 18:11:26 +01:00
2020-10-14 09:32:05 +02: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 ;
2017-12-09 18:11:26 +01:00
2020-10-14 09:32:05 +02:00
while ( todo ) {
int to_draw = MIN ( max_per_call , todo ) ;
_draw_generic ( GL_LINES , to_draw * 2 , & pline - > lines . ptr ( ) [ offset ] , NULL , pline - > line_colors . size ( ) = = 1 ? pline - > line_colors . ptr ( ) : & pline - > line_colors . ptr ( ) [ offset ] , pline - > line_colors . size ( ) = = 1 ) ;
todo - = to_draw ;
offset + = to_draw * 2 ;
}
2017-12-09 18:11:26 +01:00
2020-10-14 09:32:05 +02:00
} else {
2017-12-09 18:11:26 +01:00
2020-10-14 09:32:05 +02:00
_draw_generic ( GL_LINE_STRIP , pline - > lines . size ( ) , pline - > lines . ptr ( ) , NULL , pline - > line_colors . ptr ( ) , pline - > line_colors . size ( ) = = 1 ) ;
}
2017-07-01 02:30:17 +02:00
# ifdef GLES_OVER_GL
2020-10-14 09:32:05 +02:00
if ( pline - > antialiased )
glDisable ( GL_LINE_SMOOTH ) ;
2017-07-01 02:30:17 +02:00
# endif
2020-10-14 09:32:05 +02:00
}
2016-10-03 21:33:42 +02:00
2020-10-14 09:32:05 +02:00
} break ;
case Item : : Command : : TYPE_RECT : {
2016-10-03 21:33:42 +02:00
2020-10-14 09:32:05 +02:00
Item : : CommandRect * rect = static_cast < Item : : CommandRect * > ( c ) ;
2016-10-03 21:33:42 +02:00
2020-10-14 09:32:05 +02:00
//set color
glVertexAttrib4f ( VS : : ARRAY_COLOR , rect - > modulate . r , rect - > modulate . g , rect - > modulate . b , rect - > modulate . a ) ;
2016-10-03 21:33:42 +02:00
2020-10-14 09:32:05 +02:00
RasterizerStorageGLES3 : : Texture * texture = _bind_canvas_texture ( rect - > texture , rect - > normal_map ) ;
2016-10-03 21:33:42 +02:00
2020-10-14 09:32:05 +02:00
if ( use_nvidia_rect_workaround ) {
render_rect_nvidia_workaround ( rect , texture ) ;
} else {
2016-10-03 21:33:42 +02:00
2020-10-14 09:32:05 +02:00
_set_texture_rect_mode ( true ) ;
2016-10-03 21:33:42 +02:00
2020-10-14 09:32:05 +02:00
if ( texture ) {
2016-10-03 21:33:42 +02:00
2020-10-14 09:32:05 +02:00
bool untile = false ;
2016-10-03 21:33:42 +02:00
2020-10-14 09:32:05 +02:00
if ( rect - > flags & CANVAS_RECT_TILE & & ! ( texture - > flags & VS : : TEXTURE_FLAG_REPEAT ) ) {
glTexParameterf ( GL_TEXTURE_2D , GL_TEXTURE_WRAP_S , GL_REPEAT ) ;
glTexParameterf ( GL_TEXTURE_2D , GL_TEXTURE_WRAP_T , GL_REPEAT ) ;
untile = true ;
}
2017-09-02 18:58:04 +02:00
2020-10-14 09:32:05 +02:00
Size2 texpixel_size ( 1.0 / texture - > width , 1.0 / texture - > height ) ;
Rect2 src_rect = ( rect - > flags & CANVAS_RECT_REGION ) ? Rect2 ( rect - > source . position * texpixel_size , rect - > source . size * texpixel_size ) : Rect2 ( 0 , 0 , 1 , 1 ) ;
Rect2 dst_rect = Rect2 ( rect - > rect . position , rect - > rect . size ) ;
2016-10-03 21:33:42 +02:00
2020-10-14 09:32:05 +02: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 ;
}
2016-10-03 21:33:42 +02:00
2020-10-14 09:32:05 +02:00
if ( rect - > flags & CANVAS_RECT_FLIP_H ) {
src_rect . size . x * = - 1 ;
}
2016-10-03 21:33:42 +02:00
2020-10-14 09:32:05 +02:00
if ( rect - > flags & CANVAS_RECT_FLIP_V ) {
src_rect . size . y * = - 1 ;
}
2016-10-03 21:33:42 +02:00
2020-10-14 09:32:05 +02:00
if ( rect - > flags & CANVAS_RECT_TRANSPOSE ) {
dst_rect . size . x * = - 1 ; // Encoding in the dst_rect.z uniform
}
2017-06-19 03:55:02 +02:00
2020-10-14 09:32:05 +02:00
state . canvas_shader . set_uniform ( CanvasShaderGLES3 : : COLOR_TEXPIXEL_SIZE , texpixel_size ) ;
2016-10-03 21:33:42 +02:00
2020-10-14 09:32:05 +02:00
state . canvas_shader . set_uniform ( CanvasShaderGLES3 : : DST_RECT , Color ( dst_rect . position . x , dst_rect . position . y , dst_rect . size . x , dst_rect . size . y ) ) ;
state . canvas_shader . set_uniform ( CanvasShaderGLES3 : : SRC_RECT , Color ( src_rect . position . x , src_rect . position . y , src_rect . size . x , src_rect . size . y ) ) ;
state . canvas_shader . set_uniform ( CanvasShaderGLES3 : : CLIP_RECT_UV , rect - > flags & CANVAS_RECT_CLIP_UV ) ;
2016-10-03 21:33:42 +02:00
2020-10-14 09:32:05 +02:00
glDrawArrays ( GL_TRIANGLE_FAN , 0 , 4 ) ;
storage - > info . render . _2d_draw_call_count + + ;
2016-10-03 21:33:42 +02:00
2020-10-14 09:32:05 +02: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-09-02 18:58:04 +02:00
2020-10-14 09:32:05 +02:00
} else {
Rect2 dst_rect = Rect2 ( rect - > rect . position , rect - > rect . size ) ;
2016-10-03 21:33:42 +02:00
2020-10-14 09:32:05 +02: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 ;
}
2016-10-03 21:33:42 +02:00
2020-10-14 09:32:05 +02:00
state . canvas_shader . set_uniform ( CanvasShaderGLES3 : : DST_RECT , Color ( dst_rect . position . x , dst_rect . position . y , dst_rect . size . x , dst_rect . size . y ) ) ;
state . canvas_shader . set_uniform ( CanvasShaderGLES3 : : SRC_RECT , Color ( 0 , 0 , 1 , 1 ) ) ;
state . canvas_shader . set_uniform ( CanvasShaderGLES3 : : CLIP_RECT_UV , false ) ;
glDrawArrays ( GL_TRIANGLE_FAN , 0 , 4 ) ;
storage - > info . render . _2d_draw_call_count + + ;
}
} // if not use nvidia workaround
} break ;
case Item : : Command : : TYPE_NINEPATCH : {
2016-10-03 21:33:42 +02:00
2020-10-14 09:32:05 +02:00
Item : : CommandNinePatch * np = static_cast < Item : : CommandNinePatch * > ( c ) ;
2016-10-03 21:33:42 +02:00
2020-10-14 09:32:05 +02:00
_set_texture_rect_mode ( true , true ) ;
2016-10-03 21:33:42 +02:00
2020-10-14 09:32:05 +02:00
glVertexAttrib4f ( VS : : ARRAY_COLOR , np - > color . r , np - > color . g , np - > color . b , np - > color . a ) ;
2016-10-03 21:33:42 +02:00
2020-10-14 09:32:05 +02:00
RasterizerStorageGLES3 : : Texture * texture = _bind_canvas_texture ( np - > texture , np - > normal_map ) ;
2016-10-03 21:33:42 +02:00
2020-10-14 09:32:05 +02:00
Size2 texpixel_size ;
2016-10-03 21:33:42 +02:00
2020-10-14 09:32:05 +02:00
if ( ! texture ) {
2016-10-03 21:33:42 +02:00
2020-10-14 09:32:05 +02:00
texpixel_size = Size2 ( 1 , 1 ) ;
2016-10-03 21:33:42 +02:00
2020-10-14 09:32:05 +02:00
state . canvas_shader . set_uniform ( CanvasShaderGLES3 : : SRC_RECT , Color ( 0 , 0 , 1 , 1 ) ) ;
2016-10-03 21:33:42 +02:00
2020-10-14 09:32:05 +02:00
} else {
2016-10-03 21:33:42 +02:00
2020-10-14 09:32:05 +02:00
if ( np - > source ! = Rect2 ( ) ) {
texpixel_size = Size2 ( 1.0 / np - > source . size . width , 1.0 / np - > source . size . height ) ;
state . canvas_shader . set_uniform ( CanvasShaderGLES3 : : SRC_RECT , Color ( np - > source . position . x / texture - > width , np - > source . position . y / texture - > height , np - > source . size . x / texture - > width , np - > source . size . y / texture - > height ) ) ;
} else {
texpixel_size = Size2 ( 1.0 / texture - > width , 1.0 / texture - > height ) ;
state . canvas_shader . set_uniform ( CanvasShaderGLES3 : : SRC_RECT , Color ( 0 , 0 , 1 , 1 ) ) ;
}
}
2016-10-03 21:33:42 +02:00
2020-10-14 09:32:05 +02:00
state . canvas_shader . set_uniform ( CanvasShaderGLES3 : : COLOR_TEXPIXEL_SIZE , texpixel_size ) ;
state . canvas_shader . set_uniform ( CanvasShaderGLES3 : : CLIP_RECT_UV , false ) ;
state . canvas_shader . set_uniform ( CanvasShaderGLES3 : : NP_REPEAT_H , int ( np - > axis_x ) ) ;
state . canvas_shader . set_uniform ( CanvasShaderGLES3 : : NP_REPEAT_V , int ( np - > axis_y ) ) ;
state . canvas_shader . set_uniform ( CanvasShaderGLES3 : : NP_DRAW_CENTER , np - > draw_center ) ;
state . canvas_shader . set_uniform ( CanvasShaderGLES3 : : NP_MARGINS , Color ( np - > margin [ MARGIN_LEFT ] , np - > margin [ MARGIN_TOP ] , np - > margin [ MARGIN_RIGHT ] , np - > margin [ MARGIN_BOTTOM ] ) ) ;
state . canvas_shader . set_uniform ( CanvasShaderGLES3 : : DST_RECT , Color ( np - > rect . position . x , np - > rect . position . y , np - > rect . size . x , np - > rect . size . y ) ) ;
2016-10-03 21:33:42 +02:00
2020-10-14 09:32:05 +02:00
glDrawArrays ( GL_TRIANGLE_FAN , 0 , 4 ) ;
2016-10-03 21:33:42 +02:00
2020-10-14 09:32:05 +02:00
storage - > info . render . _2d_draw_call_count + + ;
} break ;
case Item : : Command : : TYPE_PRIMITIVE : {
2016-10-03 21:33:42 +02:00
2020-10-14 09:32:05 +02:00
Item : : CommandPrimitive * primitive = static_cast < Item : : CommandPrimitive * > ( c ) ;
_set_texture_rect_mode ( false ) ;
2016-10-03 21:33:42 +02:00
2020-10-14 09:32:05 +02:00
ERR_CONTINUE ( primitive - > points . size ( ) < 1 ) ;
2016-10-03 21:33:42 +02:00
2020-10-14 09:32:05 +02:00
RasterizerStorageGLES3 : : Texture * texture = _bind_canvas_texture ( primitive - > texture , primitive - > normal_map ) ;
2016-10-03 21:33:42 +02:00
2020-10-14 09:32:05 +02:00
if ( texture ) {
Size2 texpixel_size ( 1.0 / texture - > width , 1.0 / texture - > height ) ;
state . canvas_shader . set_uniform ( CanvasShaderGLES3 : : COLOR_TEXPIXEL_SIZE , texpixel_size ) ;
}
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-10-14 09:32:05 +02:00
if ( primitive - > colors . size ( ) = = 1 & & primitive - > points . size ( ) > 1 ) {
2016-10-03 21:33:42 +02:00
2020-10-14 09:32:05 +02:00
Color col = primitive - > colors [ 0 ] ;
glVertexAttrib4f ( VS : : ARRAY_COLOR , col . r , col . g , col . b , col . a ) ;
2020-11-22 12:24:29 +01:00
colors = nullptr ;
2016-10-03 21:33:42 +02:00
2020-10-14 09:32:05 +02: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
2016-10-03 21:33:42 +02:00
2020-11-22 12:24:29 +01:00
_draw_gui_primitive ( primitive - > points . size ( ) , primitive - > points . ptr ( ) , colors , primitive - > uvs . ptr ( ) ) ;
2016-10-03 21:33:42 +02:00
2020-10-14 09:32:05 +02:00
} break ;
case Item : : Command : : TYPE_POLYGON : {
2016-10-03 21:33:42 +02:00
2020-10-14 09:32:05 +02:00
Item : : CommandPolygon * polygon = static_cast < Item : : CommandPolygon * > ( c ) ;
_set_texture_rect_mode ( false ) ;
2016-10-10 23:31:01 +02:00
2020-10-14 09:32:05 +02:00
RasterizerStorageGLES3 : : Texture * texture = _bind_canvas_texture ( polygon - > texture , polygon - > normal_map ) ;
2016-10-10 23:31:01 +02:00
2020-10-14 09:32:05 +02:00
if ( texture ) {
Size2 texpixel_size ( 1.0 / texture - > width , 1.0 / texture - > height ) ;
state . canvas_shader . set_uniform ( CanvasShaderGLES3 : : COLOR_TEXPIXEL_SIZE , texpixel_size ) ;
}
2018-05-03 22:29:15 +02:00
2020-10-14 09:32:05 +02: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 - > bones . ptr ( ) , polygon - > weights . ptr ( ) ) ;
2017-08-19 18:14:04 +02:00
# ifdef GLES_OVER_GL
2020-10-14 09:32:05 +02:00
if ( polygon - > antialiased ) {
glEnable ( GL_LINE_SMOOTH ) ;
if ( polygon - > antialiasing_use_indices ) {
_draw_generic_indices ( GL_LINE_STRIP , polygon - > indices . ptr ( ) , polygon - > count , polygon - > points . size ( ) , polygon - > points . ptr ( ) , polygon - > uvs . ptr ( ) , polygon - > colors . ptr ( ) , polygon - > colors . size ( ) = = 1 ) ;
} else {
_draw_generic ( GL_LINE_LOOP , polygon - > points . size ( ) , polygon - > points . ptr ( ) , polygon - > uvs . ptr ( ) , polygon - > colors . ptr ( ) , polygon - > colors . size ( ) = = 1 ) ;
}
glDisable ( GL_LINE_SMOOTH ) ;
}
2017-08-19 18:14:04 +02:00
# endif
2016-10-03 21:33:42 +02:00
2020-10-14 09:32:05 +02:00
} break ;
case Item : : Command : : TYPE_MESH : {
2018-08-29 21:48:32 +02:00
2020-10-14 09:32:05 +02:00
Item : : CommandMesh * mesh = static_cast < Item : : CommandMesh * > ( c ) ;
_set_texture_rect_mode ( false ) ;
2018-12-28 00:39:00 +01:00
2020-10-14 09:32:05 +02:00
RasterizerStorageGLES3 : : Texture * texture = _bind_canvas_texture ( mesh - > texture , mesh - > normal_map ) ;
2018-08-29 21:48:32 +02:00
2020-10-14 09:32:05 +02:00
if ( texture ) {
Size2 texpixel_size ( 1.0 / texture - > width , 1.0 / texture - > height ) ;
state . canvas_shader . set_uniform ( CanvasShaderGLES3 : : COLOR_TEXPIXEL_SIZE , texpixel_size ) ;
}
2018-08-29 21:48:32 +02:00
2020-10-14 09:32:05 +02:00
state . canvas_shader . set_uniform ( CanvasShaderGLES3 : : MODELVIEW_MATRIX , state . final_transform * mesh - > transform ) ;
2018-08-29 21:48:32 +02:00
2020-10-14 09:32:05 +02:00
RasterizerStorageGLES3 : : Mesh * mesh_data = storage - > mesh_owner . getornull ( mesh - > mesh ) ;
if ( mesh_data ) {
2018-08-29 21:48:32 +02:00
2020-10-14 09:32:05 +02:00
for ( int j = 0 ; j < mesh_data - > surfaces . size ( ) ; j + + ) {
RasterizerStorageGLES3 : : Surface * s = mesh_data - > surfaces [ j ] ;
// materials are ignored in 2D meshes, could be added but many things (ie, lighting mode, reading from screen, etc) would break as they are not meant be set up at this point of drawing
glBindVertexArray ( s - > array_id ) ;
2018-08-29 21:48:32 +02:00
2020-10-14 09:32:05 +02:00
glVertexAttrib4f ( VS : : ARRAY_COLOR , mesh - > modulate . r , mesh - > modulate . g , mesh - > modulate . b , mesh - > modulate . a ) ;
2018-08-29 21:48:32 +02:00
2020-10-14 09:32:05 +02:00
if ( s - > index_array_len ) {
glDrawElements ( gl_primitive [ s - > primitive ] , s - > index_array_len , ( s - > array_len > = ( 1 < < 16 ) ) ? GL_UNSIGNED_INT : GL_UNSIGNED_SHORT , 0 ) ;
} else {
glDrawArrays ( gl_primitive [ s - > primitive ] , 0 , s - > array_len ) ;
}
storage - > info . render . _2d_draw_call_count + + ;
2018-08-29 21:48:32 +02:00
2020-10-14 09:32:05 +02:00
glBindVertexArray ( 0 ) ;
}
}
state . canvas_shader . set_uniform ( CanvasShaderGLES3 : : MODELVIEW_MATRIX , state . final_transform ) ;
2018-08-29 21:48:32 +02:00
} break ;
2020-10-14 09:32:05 +02:00
case Item : : Command : : TYPE_MULTIMESH : {
Item : : CommandMultiMesh * mmesh = static_cast < Item : : CommandMultiMesh * > ( c ) ;
RasterizerStorageGLES3 : : MultiMesh * multi_mesh = storage - > multimesh_owner . getornull ( mmesh - > multimesh ) ;
if ( ! multi_mesh )
break ;
RasterizerStorageGLES3 : : Mesh * mesh_data = storage - > mesh_owner . getornull ( multi_mesh - > mesh ) ;
if ( ! mesh_data )
break ;
RasterizerStorageGLES3 : : Texture * texture = _bind_canvas_texture ( mmesh - > texture , mmesh - > normal_map ) ;
state . canvas_shader . set_conditional ( CanvasShaderGLES3 : : USE_INSTANCE_CUSTOM , multi_mesh - > custom_data_format ! = VS : : MULTIMESH_CUSTOM_DATA_NONE ) ;
state . canvas_shader . set_conditional ( CanvasShaderGLES3 : : USE_INSTANCING , true ) ;
//reset shader and force rebind
state . using_texture_rect = true ;
_set_texture_rect_mode ( false ) ;
if ( texture ) {
Size2 texpixel_size ( 1.0 / texture - > width , 1.0 / texture - > height ) ;
state . canvas_shader . set_uniform ( CanvasShaderGLES3 : : COLOR_TEXPIXEL_SIZE , texpixel_size ) ;
}
int amount = MIN ( multi_mesh - > size , multi_mesh - > visible_instances ) ;
if ( amount = = - 1 ) {
amount = multi_mesh - > size ;
}
for ( int j = 0 ; j < mesh_data - > surfaces . size ( ) ; j + + ) {
RasterizerStorageGLES3 : : Surface * s = mesh_data - > surfaces [ j ] ;
// materials are ignored in 2D meshes, could be added but many things (ie, lighting mode, reading from screen, etc) would break as they are not meant be set up at this point of drawing
glBindVertexArray ( s - > instancing_array_id ) ;
glBindBuffer ( GL_ARRAY_BUFFER , multi_mesh - > buffer ) ; //modify the buffer
int stride = ( multi_mesh - > xform_floats + multi_mesh - > color_floats + multi_mesh - > custom_data_floats ) * 4 ;
glEnableVertexAttribArray ( 8 ) ;
glVertexAttribPointer ( 8 , 4 , GL_FLOAT , GL_FALSE , stride , CAST_INT_TO_UCHAR_PTR ( 0 ) ) ;
glVertexAttribDivisor ( 8 , 1 ) ;
glEnableVertexAttribArray ( 9 ) ;
glVertexAttribPointer ( 9 , 4 , GL_FLOAT , GL_FALSE , stride , CAST_INT_TO_UCHAR_PTR ( 4 * 4 ) ) ;
glVertexAttribDivisor ( 9 , 1 ) ;
int color_ofs ;
if ( multi_mesh - > transform_format = = VS : : MULTIMESH_TRANSFORM_3D ) {
glEnableVertexAttribArray ( 10 ) ;
glVertexAttribPointer ( 10 , 4 , GL_FLOAT , GL_FALSE , stride , CAST_INT_TO_UCHAR_PTR ( 8 * 4 ) ) ;
glVertexAttribDivisor ( 10 , 1 ) ;
color_ofs = 12 * 4 ;
} else {
glDisableVertexAttribArray ( 10 ) ;
glVertexAttrib4f ( 10 , 0 , 0 , 1 , 0 ) ;
color_ofs = 8 * 4 ;
}
int custom_data_ofs = color_ofs ;
switch ( multi_mesh - > color_format ) {
case VS : : MULTIMESH_COLOR_MAX :
case VS : : MULTIMESH_COLOR_NONE : {
glDisableVertexAttribArray ( 11 ) ;
glVertexAttrib4f ( 11 , 1 , 1 , 1 , 1 ) ;
} break ;
case VS : : MULTIMESH_COLOR_8BIT : {
glEnableVertexAttribArray ( 11 ) ;
glVertexAttribPointer ( 11 , 4 , GL_UNSIGNED_BYTE , GL_TRUE , stride , CAST_INT_TO_UCHAR_PTR ( color_ofs ) ) ;
glVertexAttribDivisor ( 11 , 1 ) ;
custom_data_ofs + = 4 ;
} break ;
case VS : : MULTIMESH_COLOR_FLOAT : {
glEnableVertexAttribArray ( 11 ) ;
glVertexAttribPointer ( 11 , 4 , GL_FLOAT , GL_FALSE , stride , CAST_INT_TO_UCHAR_PTR ( color_ofs ) ) ;
glVertexAttribDivisor ( 11 , 1 ) ;
custom_data_ofs + = 4 * 4 ;
} break ;
}
switch ( multi_mesh - > custom_data_format ) {
case VS : : MULTIMESH_CUSTOM_DATA_MAX :
case VS : : MULTIMESH_CUSTOM_DATA_NONE : {
glDisableVertexAttribArray ( 12 ) ;
glVertexAttrib4f ( 12 , 1 , 1 , 1 , 1 ) ;
} break ;
case VS : : MULTIMESH_CUSTOM_DATA_8BIT : {
glEnableVertexAttribArray ( 12 ) ;
glVertexAttribPointer ( 12 , 4 , GL_UNSIGNED_BYTE , GL_TRUE , stride , CAST_INT_TO_UCHAR_PTR ( custom_data_ofs ) ) ;
glVertexAttribDivisor ( 12 , 1 ) ;
} break ;
case VS : : MULTIMESH_CUSTOM_DATA_FLOAT : {
glEnableVertexAttribArray ( 12 ) ;
glVertexAttribPointer ( 12 , 4 , GL_FLOAT , GL_FALSE , stride , CAST_INT_TO_UCHAR_PTR ( custom_data_ofs ) ) ;
glVertexAttribDivisor ( 12 , 1 ) ;
} break ;
}
if ( s - > index_array_len ) {
glDrawElementsInstanced ( gl_primitive [ s - > primitive ] , s - > index_array_len , ( s - > array_len > = ( 1 < < 16 ) ) ? GL_UNSIGNED_INT : GL_UNSIGNED_SHORT , 0 , amount ) ;
} else {
glDrawArraysInstanced ( gl_primitive [ s - > primitive ] , 0 , s - > array_len , amount ) ;
}
storage - > info . render . _2d_draw_call_count + + ;
glBindVertexArray ( 0 ) ;
}
state . canvas_shader . set_conditional ( CanvasShaderGLES3 : : USE_INSTANCE_CUSTOM , false ) ;
state . canvas_shader . set_conditional ( CanvasShaderGLES3 : : USE_INSTANCING , false ) ;
state . using_texture_rect = true ;
_set_texture_rect_mode ( false ) ;
2018-08-29 21:48:32 +02:00
} break ;
2020-10-14 09:32:05 +02:00
case Item : : Command : : TYPE_PARTICLES : {
Item : : CommandParticles * particles_cmd = static_cast < Item : : CommandParticles * > ( c ) ;
RasterizerStorageGLES3 : : Particles * particles = storage - > particles_owner . getornull ( particles_cmd - > particles ) ;
if ( ! particles )
break ;
if ( particles - > inactive & & ! particles - > emitting )
break ;
glVertexAttrib4f ( VS : : ARRAY_COLOR , 1 , 1 , 1 , 1 ) ; //not used, so keep white
VisualServerRaster : : redraw_request ( ) ;
storage - > particles_request_process ( particles_cmd - > particles ) ;
//enable instancing
state . canvas_shader . set_conditional ( CanvasShaderGLES3 : : USE_INSTANCE_CUSTOM , true ) ;
state . canvas_shader . set_conditional ( CanvasShaderGLES3 : : USE_PARTICLES , true ) ;
state . canvas_shader . set_conditional ( CanvasShaderGLES3 : : USE_INSTANCING , true ) ;
//reset shader and force rebind
state . using_texture_rect = true ;
_set_texture_rect_mode ( false ) ;
RasterizerStorageGLES3 : : Texture * texture = _bind_canvas_texture ( particles_cmd - > texture , particles_cmd - > normal_map ) ;
if ( texture ) {
Size2 texpixel_size ( 1.0 / texture - > width , 1.0 / texture - > height ) ;
state . canvas_shader . set_uniform ( CanvasShaderGLES3 : : COLOR_TEXPIXEL_SIZE , texpixel_size ) ;
} else {
state . canvas_shader . set_uniform ( CanvasShaderGLES3 : : COLOR_TEXPIXEL_SIZE , Vector2 ( 1.0 , 1.0 ) ) ;
}
if ( ! particles - > use_local_coords ) {
Transform2D inv_xf ;
inv_xf . set_axis ( 0 , Vector2 ( particles - > emission_transform . basis . get_axis ( 0 ) . x , particles - > emission_transform . basis . get_axis ( 0 ) . y ) ) ;
inv_xf . set_axis ( 1 , Vector2 ( particles - > emission_transform . basis . get_axis ( 1 ) . x , particles - > emission_transform . basis . get_axis ( 1 ) . y ) ) ;
inv_xf . set_origin ( Vector2 ( particles - > emission_transform . get_origin ( ) . x , particles - > emission_transform . get_origin ( ) . y ) ) ;
inv_xf . affine_invert ( ) ;
state . canvas_shader . set_uniform ( CanvasShaderGLES3 : : MODELVIEW_MATRIX , state . final_transform * inv_xf ) ;
}
glBindVertexArray ( data . particle_quad_array ) ; //use particle quad array
glBindBuffer ( GL_ARRAY_BUFFER , particles - > particle_buffers [ 0 ] ) ; //bind particle buffer
int stride = sizeof ( float ) * 4 * 6 ;
int amount = particles - > amount ;
if ( particles - > draw_order ! = VS : : PARTICLES_DRAW_ORDER_LIFETIME ) {
glEnableVertexAttribArray ( 8 ) ; //xform x
glVertexAttribPointer ( 8 , 4 , GL_FLOAT , GL_FALSE , stride , CAST_INT_TO_UCHAR_PTR ( sizeof ( float ) * 4 * 3 ) ) ;
glVertexAttribDivisor ( 8 , 1 ) ;
glEnableVertexAttribArray ( 9 ) ; //xform y
glVertexAttribPointer ( 9 , 4 , GL_FLOAT , GL_FALSE , stride , CAST_INT_TO_UCHAR_PTR ( sizeof ( float ) * 4 * 4 ) ) ;
glVertexAttribDivisor ( 9 , 1 ) ;
glEnableVertexAttribArray ( 10 ) ; //xform z
glVertexAttribPointer ( 10 , 4 , GL_FLOAT , GL_FALSE , stride , CAST_INT_TO_UCHAR_PTR ( sizeof ( float ) * 4 * 5 ) ) ;
glVertexAttribDivisor ( 10 , 1 ) ;
glEnableVertexAttribArray ( 11 ) ; //color
glVertexAttribPointer ( 11 , 4 , GL_FLOAT , GL_FALSE , stride , NULL ) ;
glVertexAttribDivisor ( 11 , 1 ) ;
glEnableVertexAttribArray ( 12 ) ; //custom
glVertexAttribPointer ( 12 , 4 , GL_FLOAT , GL_FALSE , stride , CAST_INT_TO_UCHAR_PTR ( sizeof ( float ) * 4 * 2 ) ) ;
glVertexAttribDivisor ( 12 , 1 ) ;
glDrawArraysInstanced ( GL_TRIANGLE_FAN , 0 , 4 , amount ) ;
storage - > info . render . _2d_draw_call_count + + ;
} else {
//split
int split = int ( Math : : ceil ( particles - > phase * particles - > amount ) ) ;
if ( amount - split > 0 ) {
glEnableVertexAttribArray ( 8 ) ; //xform x
glVertexAttribPointer ( 8 , 4 , GL_FLOAT , GL_FALSE , stride , CAST_INT_TO_UCHAR_PTR ( stride * split + sizeof ( float ) * 4 * 3 ) ) ;
glVertexAttribDivisor ( 8 , 1 ) ;
glEnableVertexAttribArray ( 9 ) ; //xform y
glVertexAttribPointer ( 9 , 4 , GL_FLOAT , GL_FALSE , stride , CAST_INT_TO_UCHAR_PTR ( stride * split + sizeof ( float ) * 4 * 4 ) ) ;
glVertexAttribDivisor ( 9 , 1 ) ;
glEnableVertexAttribArray ( 10 ) ; //xform z
glVertexAttribPointer ( 10 , 4 , GL_FLOAT , GL_FALSE , stride , CAST_INT_TO_UCHAR_PTR ( stride * split + sizeof ( float ) * 4 * 5 ) ) ;
glVertexAttribDivisor ( 10 , 1 ) ;
glEnableVertexAttribArray ( 11 ) ; //color
glVertexAttribPointer ( 11 , 4 , GL_FLOAT , GL_FALSE , stride , CAST_INT_TO_UCHAR_PTR ( stride * split + 0 ) ) ;
glVertexAttribDivisor ( 11 , 1 ) ;
glEnableVertexAttribArray ( 12 ) ; //custom
glVertexAttribPointer ( 12 , 4 , GL_FLOAT , GL_FALSE , stride , CAST_INT_TO_UCHAR_PTR ( stride * split + sizeof ( float ) * 4 * 2 ) ) ;
glVertexAttribDivisor ( 12 , 1 ) ;
glDrawArraysInstanced ( GL_TRIANGLE_FAN , 0 , 4 , amount - split ) ;
storage - > info . render . _2d_draw_call_count + + ;
}
if ( split > 0 ) {
glEnableVertexAttribArray ( 8 ) ; //xform x
glVertexAttribPointer ( 8 , 4 , GL_FLOAT , GL_FALSE , stride , CAST_INT_TO_UCHAR_PTR ( sizeof ( float ) * 4 * 3 ) ) ;
glVertexAttribDivisor ( 8 , 1 ) ;
glEnableVertexAttribArray ( 9 ) ; //xform y
glVertexAttribPointer ( 9 , 4 , GL_FLOAT , GL_FALSE , stride , CAST_INT_TO_UCHAR_PTR ( sizeof ( float ) * 4 * 4 ) ) ;
glVertexAttribDivisor ( 9 , 1 ) ;
glEnableVertexAttribArray ( 10 ) ; //xform z
glVertexAttribPointer ( 10 , 4 , GL_FLOAT , GL_FALSE , stride , CAST_INT_TO_UCHAR_PTR ( sizeof ( float ) * 4 * 5 ) ) ;
glVertexAttribDivisor ( 10 , 1 ) ;
glEnableVertexAttribArray ( 11 ) ; //color
glVertexAttribPointer ( 11 , 4 , GL_FLOAT , GL_FALSE , stride , NULL ) ;
glVertexAttribDivisor ( 11 , 1 ) ;
glEnableVertexAttribArray ( 12 ) ; //custom
glVertexAttribPointer ( 12 , 4 , GL_FLOAT , GL_FALSE , stride , CAST_INT_TO_UCHAR_PTR ( sizeof ( float ) * 4 * 2 ) ) ;
glVertexAttribDivisor ( 12 , 1 ) ;
glDrawArraysInstanced ( GL_TRIANGLE_FAN , 0 , 4 , split ) ;
storage - > info . render . _2d_draw_call_count + + ;
}
}
glBindVertexArray ( 0 ) ;
state . canvas_shader . set_conditional ( CanvasShaderGLES3 : : USE_INSTANCE_CUSTOM , false ) ;
state . canvas_shader . set_conditional ( CanvasShaderGLES3 : : USE_PARTICLES , false ) ;
state . canvas_shader . set_conditional ( CanvasShaderGLES3 : : USE_INSTANCING , false ) ;
state . using_texture_rect = true ;
_set_texture_rect_mode ( false ) ;
2018-08-29 21:48:32 +02:00
} break ;
2020-10-14 09:32:05 +02:00
case Item : : Command : : TYPE_CIRCLE : {
2018-08-29 21:48:32 +02:00
2020-10-14 09:32:05 +02:00
_set_texture_rect_mode ( false ) ;
2018-08-29 21:48:32 +02:00
2020-10-14 09:32:05 +02:00
Item : : CommandCircle * circle = static_cast < Item : : CommandCircle * > ( c ) ;
static const int numpoints = 32 ;
Vector2 points [ numpoints + 1 ] ;
points [ numpoints ] = circle - > pos ;
int indices [ numpoints * 3 ] ;
2018-08-29 21:48:32 +02:00
2020-10-14 09:32:05 +02:00
for ( int j = 0 ; j < numpoints ; j + + ) {
2018-08-29 21:48:32 +02:00
2020-10-14 09:32:05 +02:00
points [ j ] = circle - > pos + Vector2 ( Math : : sin ( j * Math_PI * 2.0 / numpoints ) , Math : : cos ( j * Math_PI * 2.0 / numpoints ) ) * circle - > radius ;
indices [ j * 3 + 0 ] = j ;
indices [ j * 3 + 1 ] = ( j + 1 ) % numpoints ;
indices [ j * 3 + 2 ] = numpoints ;
}
2017-06-21 21:25:45 +02:00
2020-10-14 09:32:05 +02:00
_bind_canvas_texture ( RID ( ) , RID ( ) ) ;
_draw_polygon ( indices , numpoints * 3 , numpoints + 1 , points , NULL , & circle - > color , true , NULL , NULL ) ;
2017-06-21 21:25:45 +02:00
2020-10-14 09:32:05 +02:00
//_draw_polygon(numpoints*3,indices,points,NULL,&circle->color,RID(),true);
//canvas_draw_circle(circle->indices.size(),circle->indices.ptr(),circle->points.ptr(),circle->uvs.ptr(),circle->colors.ptr(),circle->texture,circle->colors.size()==1);
} break ;
case Item : : Command : : TYPE_TRANSFORM : {
2017-06-21 21:25:45 +02:00
2020-10-14 09:32:05 +02:00
Item : : CommandTransform * transform = static_cast < Item : : CommandTransform * > ( c ) ;
state . extra_matrix = transform - > xform ;
state . canvas_shader . set_uniform ( CanvasShaderGLES3 : : EXTRA_MATRIX , state . extra_matrix ) ;
2017-06-21 21:25:45 +02:00
2020-10-14 09:32:05 +02:00
} break ;
case Item : : Command : : TYPE_CLIP_IGNORE : {
2017-06-21 21:25:45 +02:00
2020-10-14 09:32:05 +02:00
Item : : CommandClipIgnore * ci = static_cast < Item : : CommandClipIgnore * > ( c ) ;
if ( p_current_clip ) {
2017-06-21 21:25:45 +02:00
2020-10-14 09:32:05 +02:00
if ( ci - > ignore ! = r_reclip ) {
if ( ci - > ignore ) {
2017-06-21 21:25:45 +02:00
2020-10-14 09:32:05 +02:00
glDisable ( GL_SCISSOR_TEST ) ;
r_reclip = true ;
} else {
2017-06-21 21:25:45 +02:00
2020-10-14 09:32:05 +02:00
glEnable ( GL_SCISSOR_TEST ) ;
//glScissor(viewport.x+current_clip->final_clip_rect.pos.x,viewport.y+ (viewport.height-(current_clip->final_clip_rect.pos.y+current_clip->final_clip_rect.size.height)),
//current_clip->final_clip_rect.size.width,current_clip->final_clip_rect.size.height);
int y = storage - > frame . current_rt - > height - ( p_current_clip - > final_clip_rect . position . y + p_current_clip - > final_clip_rect . size . y ) ;
if ( storage - > frame . current_rt - > flags [ RasterizerStorage : : RENDER_TARGET_VFLIP ] )
y = p_current_clip - > final_clip_rect . position . y ;
2017-06-21 21:25:45 +02:00
2020-10-14 09:32:05 +02:00
glScissor ( p_current_clip - > final_clip_rect . position . x , y , p_current_clip - > final_clip_rect . size . x , p_current_clip - > final_clip_rect . size . y ) ;
2017-06-21 21:25:45 +02:00
2020-10-14 09:32:05 +02:00
r_reclip = false ;
}
}
}
2017-06-21 21:25:45 +02:00
2020-10-14 09:32:05 +02:00
} break ;
2017-06-21 21:25:45 +02:00
2020-10-14 09:32:05 +02:00
default : {
// FIXME: Proper error handling if relevant
//print_line("other");
} break ;
2017-06-21 21:25:45 +02:00
}
}
2020-10-14 09:32:05 +02:00
} // default
break ;
}
}
}
2017-06-21 21:25:45 +02:00
2020-10-14 09:32:05 +02:00
void RasterizerCanvasGLES3 : : render_joined_item ( const BItemJoined & p_bij , RenderItemState & r_ris ) {
storage - > info . render . _2d_item_count + + ;
2016-10-03 21:33:42 +02:00
2020-10-14 09:32:05 +02:00
# ifdef DEBUG_ENABLED
if ( bdata . diagnose_frame ) {
bdata . frame_string + = " \t joined_item " + itos ( p_bij . num_item_refs ) + " refs \n " ;
if ( p_bij . z_index ! = 0 ) {
bdata . frame_string + = " \t \t (z " + itos ( p_bij . z_index ) + " ) \n " ;
}
}
# endif
2016-10-03 21:33:42 +02:00
2020-10-14 09:32:05 +02:00
// all the joined items will share the same state with the first item
Item * p_ci = bdata . item_refs [ p_bij . first_item_ref ] . item ;
2016-10-03 21:33:42 +02:00
2020-10-14 09:32:05 +02:00
if ( r_ris . prev_distance_field ! = p_ci - > distance_field ) {
2017-06-12 23:56:16 +02:00
2020-10-14 09:32:05 +02:00
state . canvas_shader . set_conditional ( CanvasShaderGLES3 : : USE_DISTANCE_FIELD , p_ci - > distance_field ) ;
r_ris . prev_distance_field = p_ci - > distance_field ;
r_ris . rebind_shader = true ;
}
2017-06-12 23:56:16 +02:00
2020-10-14 09:32:05 +02:00
if ( r_ris . current_clip ! = p_ci - > final_clip_owner ) {
2016-10-03 21:33:42 +02:00
2020-10-14 09:32:05 +02:00
r_ris . current_clip = p_ci - > final_clip_owner ;
2016-10-03 21:33:42 +02:00
2020-10-14 09:32:05 +02:00
//setup clip
if ( r_ris . current_clip ) {
2016-10-03 21:33:42 +02:00
2020-10-14 09:32:05 +02:00
glEnable ( GL_SCISSOR_TEST ) ;
int y = storage - > frame . current_rt - > height - ( r_ris . current_clip - > final_clip_rect . position . y + r_ris . current_clip - > final_clip_rect . size . y ) ;
if ( storage - > frame . current_rt - > flags [ RasterizerStorage : : RENDER_TARGET_VFLIP ] )
y = r_ris . current_clip - > final_clip_rect . position . y ;
2016-10-03 21:33:42 +02:00
2020-10-14 09:32:05 +02:00
glScissor ( r_ris . current_clip - > final_clip_rect . position . x , y , r_ris . current_clip - > final_clip_rect . size . x , r_ris . current_clip - > final_clip_rect . size . y ) ;
2016-10-03 21:33:42 +02:00
2020-10-14 09:32:05 +02:00
} else {
2016-10-03 21:33:42 +02:00
2020-10-14 09:32:05 +02:00
glDisable ( GL_SCISSOR_TEST ) ;
}
}
2016-10-03 21:33:42 +02:00
2020-10-14 09:32:05 +02:00
if ( p_ci - > copy_back_buffer ) {
2016-10-03 21:33:42 +02:00
2020-10-14 09:32:05 +02:00
if ( p_ci - > copy_back_buffer - > full ) {
2016-10-03 21:33:42 +02:00
2020-10-14 09:32:05 +02:00
_copy_texscreen ( Rect2 ( ) ) ;
} else {
_copy_texscreen ( p_ci - > copy_back_buffer - > rect ) ;
2016-10-03 21:33:42 +02:00
}
}
2020-10-14 09:32:05 +02:00
if ( ! bdata . settings_use_batching | | ! bdata . settings_use_software_skinning ) {
2017-06-27 03:58:03 +02:00
2020-10-14 09:32:05 +02:00
RasterizerStorageGLES3 : : Skeleton * skeleton = NULL ;
2019-01-15 19:13:58 +01:00
2020-10-14 09:32:05 +02: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 ) {
skeleton = NULL ;
} else {
state . skeleton_transform = r_ris . item_group_base_transform * skeleton - > base_transform_2d ;
state . skeleton_transform_inverse = state . skeleton_transform . affine_inverse ( ) ;
}
}
2017-08-07 23:06:57 +02:00
2020-10-14 09:32:05 +02:00
bool use_skeleton = skeleton ! = NULL ;
if ( r_ris . prev_use_skeleton ! = use_skeleton ) {
r_ris . rebind_shader = true ;
state . canvas_shader . set_conditional ( CanvasShaderGLES3 : : USE_SKELETON , use_skeleton ) ;
r_ris . prev_use_skeleton = use_skeleton ;
}
2017-06-27 03:58:03 +02:00
2020-10-14 09:32:05 +02:00
if ( skeleton ) {
glActiveTexture ( GL_TEXTURE0 + storage - > config . max_texture_image_units - 4 ) ;
glBindTexture ( GL_TEXTURE_2D , skeleton - > texture ) ;
state . using_skeleton = true ;
} else {
state . using_skeleton = false ;
}
2017-06-27 03:58:03 +02:00
2020-10-14 09:32:05 +02:00
} // if not using batching
2017-06-27 03:58:03 +02:00
2020-10-14 09:32:05 +02:00
//begin rect
Item * material_owner = p_ci - > material_owner ? p_ci - > material_owner : p_ci ;
2017-06-27 03:58:03 +02:00
2020-10-14 09:32:05 +02:00
RID material = material_owner - > material ;
2017-06-27 03:58:03 +02:00
2020-10-14 09:32:05 +02:00
if ( material ! = r_ris . canvas_last_material | | r_ris . rebind_shader ) {
2017-06-27 03:58:03 +02:00
2020-10-14 09:32:05 +02:00
RasterizerStorageGLES3 : : Material * material_ptr = storage - > material_owner . getornull ( material ) ;
RasterizerStorageGLES3 : : Shader * shader_ptr = NULL ;
2017-06-27 03:58:03 +02:00
2020-10-14 09:32:05 +02:00
if ( material_ptr ) {
2017-06-27 03:58:03 +02:00
2020-10-14 09:32:05 +02:00
shader_ptr = material_ptr - > shader ;
2017-06-27 03:58:03 +02:00
2020-10-14 09:32:05 +02:00
if ( shader_ptr & & shader_ptr - > mode ! = VS : : SHADER_CANVAS_ITEM ) {
shader_ptr = NULL ; //do not use non canvasitem shader
}
}
2017-06-27 03:58:03 +02:00
2020-10-14 09:32:05 +02:00
if ( shader_ptr ) {
2017-06-27 03:58:03 +02:00
2020-10-14 09:32:05 +02:00
if ( shader_ptr - > canvas_item . uses_screen_texture & & ! state . canvas_texscreen_used ) {
//copy if not copied before
_copy_texscreen ( Rect2 ( ) ) ;
2017-06-27 03:58:03 +02:00
2020-10-14 09:32:05 +02:00
// blend mode will have been enabled so make sure we disable it again later on
r_ris . last_blend_mode = r_ris . last_blend_mode ! = RasterizerStorageGLES3 : : Shader : : CanvasItem : : BLEND_MODE_DISABLED ? r_ris . last_blend_mode : - 1 ;
}
2017-06-27 03:58:03 +02:00
2020-10-14 09:32:05 +02:00
if ( shader_ptr ! = r_ris . shader_cache | | r_ris . rebind_shader ) {
2017-06-27 03:58:03 +02:00
2020-10-14 09:32:05 +02:00
if ( shader_ptr - > canvas_item . uses_time ) {
VisualServerRaster : : redraw_request ( ) ;
}
2017-06-27 03:58:03 +02:00
2020-10-14 09:32:05 +02:00
state . canvas_shader . set_custom_shader ( shader_ptr - > custom_code_id ) ;
state . canvas_shader . bind ( ) ;
}
2017-06-27 03:58:03 +02:00
2020-10-14 09:32:05 +02:00
if ( material_ptr - > ubo_id ) {
glBindBufferBase ( GL_UNIFORM_BUFFER , 2 , material_ptr - > ubo_id ) ;
}
2017-08-07 23:06:57 +02:00
2020-10-14 09:32:05 +02:00
int tc = material_ptr - > textures . size ( ) ;
RID * textures = material_ptr - > textures . ptrw ( ) ;
ShaderLanguage : : ShaderNode : : Uniform : : Hint * texture_hints = shader_ptr - > texture_hints . ptrw ( ) ;
2017-06-27 03:58:03 +02:00
2020-10-14 09:32:05 +02:00
for ( int i = 0 ; i < tc ; i + + ) {
2017-08-07 23:06:57 +02:00
2020-10-14 09:32:05 +02:00
glActiveTexture ( GL_TEXTURE2 + i ) ;
2017-06-27 03:58:03 +02:00
2020-10-14 09:32:05 +02:00
RasterizerStorageGLES3 : : Texture * t = storage - > texture_owner . getornull ( textures [ i ] ) ;
if ( ! t ) {
2016-10-03 21:33:42 +02:00
2020-10-14 09:32:05 +02:00
switch ( texture_hints [ i ] ) {
case ShaderLanguage : : ShaderNode : : Uniform : : HINT_BLACK_ALBEDO :
case ShaderLanguage : : ShaderNode : : Uniform : : HINT_BLACK : {
glBindTexture ( GL_TEXTURE_2D , storage - > resources . black_tex ) ;
} break ;
case ShaderLanguage : : ShaderNode : : Uniform : : HINT_ANISO : {
glBindTexture ( GL_TEXTURE_2D , storage - > resources . aniso_tex ) ;
} break ;
case ShaderLanguage : : ShaderNode : : Uniform : : HINT_NORMAL : {
glBindTexture ( GL_TEXTURE_2D , storage - > resources . normal_tex ) ;
} break ;
default : {
glBindTexture ( GL_TEXTURE_2D , storage - > resources . white_tex ) ;
} break ;
}
2016-10-03 21:33:42 +02:00
2020-10-14 09:32:05 +02:00
//check hints
2016-10-03 21:33:42 +02:00
2020-10-14 09:32:05 +02:00
continue ;
}
2016-10-03 21:33:42 +02:00
2020-10-14 09:32:05 +02:00
if ( t - > redraw_if_visible ) { //check before proxy, because this is usually used with proxies
VisualServerRaster : : redraw_request ( ) ;
}
2016-10-03 21:33:42 +02:00
2020-10-14 09:32:05 +02:00
t = t - > get_ptr ( ) ;
2016-10-10 23:31:01 +02:00
2020-10-14 09:32:05 +02:00
if ( storage - > config . srgb_decode_supported & & t - > using_srgb ) {
//no srgb in 2D
glTexParameteri ( t - > target , _TEXTURE_SRGB_DECODE_EXT , _SKIP_DECODE_EXT ) ;
t - > using_srgb = false ;
}
2016-10-03 21:33:42 +02:00
2020-10-14 09:32:05 +02:00
glBindTexture ( t - > target , t - > tex_id ) ;
}
2016-10-03 21:33:42 +02:00
2020-10-14 09:32:05 +02:00
} else {
state . canvas_shader . set_custom_shader ( 0 ) ;
state . canvas_shader . bind ( ) ;
}
2016-10-03 21:33:42 +02:00
2020-10-14 09:32:05 +02:00
r_ris . shader_cache = shader_ptr ;
2016-10-03 21:33:42 +02:00
2020-10-14 09:32:05 +02:00
r_ris . canvas_last_material = material ;
r_ris . rebind_shader = false ;
}
2016-10-03 21:33:42 +02:00
2020-10-14 09:32:05 +02:00
int blend_mode = r_ris . shader_cache ? r_ris . shader_cache - > canvas_item . blend_mode : RasterizerStorageGLES3 : : Shader : : CanvasItem : : BLEND_MODE_MIX ;
if ( blend_mode = = RasterizerStorageGLES3 : : Shader : : CanvasItem : : BLEND_MODE_DISABLED & & ( ! storage - > frame . current_rt | | ! storage - > frame . current_rt - > flags [ RasterizerStorage : : RENDER_TARGET_TRANSPARENT ] ) ) {
blend_mode = RasterizerStorageGLES3 : : Shader : : CanvasItem : : BLEND_MODE_MIX ;
}
bool unshaded = r_ris . shader_cache & & ( r_ris . shader_cache - > canvas_item . light_mode = = RasterizerStorageGLES3 : : Shader : : CanvasItem : : LIGHT_MODE_UNSHADED | | ( blend_mode ! = RasterizerStorageGLES3 : : Shader : : CanvasItem : : BLEND_MODE_MIX & & blend_mode ! = RasterizerStorageGLES3 : : Shader : : CanvasItem : : BLEND_MODE_PMALPHA ) ) ;
bool reclip = false ;
if ( r_ris . last_blend_mode ! = blend_mode ) {
if ( r_ris . last_blend_mode = = RasterizerStorageGLES3 : : Shader : : CanvasItem : : BLEND_MODE_DISABLED ) {
// re-enable it
glEnable ( GL_BLEND ) ;
} else if ( blend_mode = = RasterizerStorageGLES3 : : Shader : : CanvasItem : : BLEND_MODE_DISABLED ) {
// disable it
glDisable ( GL_BLEND ) ;
2016-10-03 21:33:42 +02:00
}
2020-10-14 09:32:05 +02:00
switch ( blend_mode ) {
2016-10-03 21:33:42 +02:00
2020-10-14 09:32:05 +02:00
case RasterizerStorageGLES3 : : Shader : : CanvasItem : : BLEND_MODE_DISABLED : {
2016-10-03 21:33:42 +02:00
2020-10-14 09:32:05 +02:00
// nothing to do here
2016-10-03 21:33:42 +02:00
2020-10-14 09:32:05 +02:00
} break ;
case RasterizerStorageGLES3 : : Shader : : CanvasItem : : BLEND_MODE_MIX : {
2018-06-16 22:55:21 +02:00
2020-10-14 09:32:05 +02:00
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 ) ;
}
2016-10-03 21:33:42 +02:00
2020-10-14 09:32:05 +02:00
} break ;
case RasterizerStorageGLES3 : : Shader : : CanvasItem : : BLEND_MODE_ADD : {
2016-10-03 21:33:42 +02:00
2020-10-14 09:32:05 +02:00
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 ) ;
}
2016-10-03 21:33:42 +02:00
2020-10-14 09:32:05 +02:00
} break ;
case RasterizerStorageGLES3 : : Shader : : CanvasItem : : BLEND_MODE_SUB : {
2016-10-03 21:33:42 +02:00
2020-10-14 09:32:05 +02:00
glBlendEquation ( GL_FUNC_REVERSE_SUBTRACT ) ;
if ( storage - > frame . current_rt & & storage - > frame . current_rt - > flags [ RasterizerStorage : : RENDER_TARGET_TRANSPARENT ] ) {
glBlendFuncSeparate ( GL_SRC_ALPHA , GL_ONE , GL_SRC_ALPHA , GL_ONE ) ;
} else {
glBlendFuncSeparate ( GL_SRC_ALPHA , GL_ONE , GL_ZERO , GL_ONE ) ;
}
} break ;
case RasterizerStorageGLES3 : : Shader : : CanvasItem : : BLEND_MODE_MUL : {
glBlendEquation ( GL_FUNC_ADD ) ;
if ( storage - > frame . current_rt & & storage - > frame . current_rt - > flags [ RasterizerStorage : : RENDER_TARGET_TRANSPARENT ] ) {
glBlendFuncSeparate ( GL_DST_COLOR , GL_ZERO , GL_DST_ALPHA , GL_ZERO ) ;
} else {
glBlendFuncSeparate ( GL_DST_COLOR , GL_ZERO , GL_ZERO , GL_ONE ) ;
}
2016-10-03 21:33:42 +02:00
2020-10-14 09:32:05 +02:00
} break ;
case RasterizerStorageGLES3 : : Shader : : CanvasItem : : BLEND_MODE_PMALPHA : {
glBlendEquation ( GL_FUNC_ADD ) ;
if ( storage - > frame . current_rt & & storage - > frame . current_rt - > flags [ RasterizerStorage : : RENDER_TARGET_TRANSPARENT ] ) {
glBlendFuncSeparate ( GL_ONE , GL_ONE_MINUS_SRC_ALPHA , GL_ONE , GL_ONE_MINUS_SRC_ALPHA ) ;
} else {
glBlendFuncSeparate ( GL_ONE , GL_ONE_MINUS_SRC_ALPHA , GL_ZERO , GL_ONE ) ;
}
} break ;
2016-10-03 21:33:42 +02:00
}
2020-10-14 09:32:05 +02:00
r_ris . last_blend_mode = blend_mode ;
}
2018-05-03 22:29:15 +02:00
2020-10-14 09:32:05 +02:00
//state.canvas_item_modulate = unshaded ? p_ci->final_modulate : Color(p_ci->final_modulate.r * r_ris.item_group_modulate.r, p_ci->final_modulate.g * r_ris.item_group_modulate.g, p_ci->final_modulate.b * r_ris.item_group_modulate.b, p_ci->final_modulate.a * r_ris.item_group_modulate.a);
2018-05-03 22:29:15 +02:00
2020-10-14 09:32:05 +02:00
// state.final_transform = p_ci->final_transform;
// state.extra_matrix = Transform2D();
2018-05-03 22:29:15 +02: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.)
2020-10-14 09:32:05 +02:00
if ( ! p_bij . use_hardware_transform ( ) ) {
state . final_transform = Transform2D ( ) ;
// final_modulate will be baked per item ref so the final_modulate can be an identity color
state . canvas_item_modulate = Color ( 1 , 1 , 1 , 1 ) ;
} else {
state . final_transform = p_ci - > final_transform ;
// could use the stored version of final_modulate in item ref? Test which is faster NYI
state . canvas_item_modulate = unshaded ? p_ci - > final_modulate : ( p_ci - > final_modulate * r_ris . item_group_modulate ) ;
}
state . extra_matrix = Transform2D ( ) ;
if ( state . using_skeleton ) {
state . canvas_shader . set_uniform ( CanvasShaderGLES3 : : SKELETON_TRANSFORM , state . skeleton_transform ) ;
state . canvas_shader . set_uniform ( CanvasShaderGLES3 : : SKELETON_TRANSFORM_INVERSE , state . skeleton_transform_inverse ) ;
}
state . canvas_shader . set_uniform ( CanvasShaderGLES3 : : FINAL_MODULATE , state . canvas_item_modulate ) ;
state . canvas_shader . set_uniform ( CanvasShaderGLES3 : : MODELVIEW_MATRIX , state . final_transform ) ;
state . canvas_shader . set_uniform ( CanvasShaderGLES3 : : EXTRA_MATRIX , state . extra_matrix ) ;
if ( storage - > frame . current_rt ) {
state . canvas_shader . set_uniform ( CanvasShaderGLES3 : : SCREEN_PIXEL_SIZE , Vector2 ( 1.0 / storage - > frame . current_rt - > width , 1.0 / storage - > frame . current_rt - > height ) ) ;
} else {
state . canvas_shader . set_uniform ( CanvasShaderGLES3 : : SCREEN_PIXEL_SIZE , Vector2 ( 1.0 , 1.0 ) ) ;
}
if ( unshaded | | ( state . canvas_item_modulate . a > 0.001 & & ( ! r_ris . shader_cache | | r_ris . shader_cache - > canvas_item . light_mode ! = RasterizerStorageGLES3 : : Shader : : CanvasItem : : LIGHT_MODE_LIGHT_ONLY ) & & ! p_ci - > light_masked ) ) {
RasterizerStorageGLES3 : : Material * material_ptr = nullptr ;
render_joined_item_commands ( p_bij , NULL , reclip , material_ptr , false ) ;
}
2018-05-03 22:29:15 +02:00
2020-10-14 09:32:05 +02:00
if ( ( blend_mode = = RasterizerStorageGLES3 : : Shader : : CanvasItem : : BLEND_MODE_MIX | | blend_mode = = RasterizerStorageGLES3 : : Shader : : CanvasItem : : BLEND_MODE_PMALPHA ) & & r_ris . item_group_light & & ! unshaded ) {
2016-10-03 21:33:42 +02:00
2020-10-14 09:32:05 +02:00
Light * light = r_ris . item_group_light ;
bool light_used = false ;
VS : : CanvasLightMode mode = VS : : CANVAS_LIGHT_MODE_ADD ;
state . canvas_item_modulate = p_ci - > final_modulate ; // remove the canvas modulate
2016-10-03 21:33:42 +02:00
2020-10-14 09:32:05 +02:00
while ( light ) {
2016-10-10 23:31:01 +02:00
2020-10-14 09:32:05 +02:00
// 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
2016-10-10 23:31:01 +02:00
2020-10-14 09:32:05 +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 ( p_ci - > light_mask & light - > item_mask & & p_bij . z_index > = light - > z_min & & p_bij . z_index < = light - > z_max & & p_bij . bounding_rect . intersects_transformed ( light - > xform_cache , light - > rect_cache ) ) {
2016-10-10 23:31:01 +02:00
2020-10-14 09:32:05 +02:00
//intersects this light
2016-10-10 23:31:01 +02:00
2020-10-14 09:32:05 +02:00
if ( ! light_used | | mode ! = light - > mode ) {
2016-10-03 21:33:42 +02:00
2020-10-14 09:32:05 +02:00
mode = light - > mode ;
2016-10-10 23:31:01 +02:00
2020-10-14 09:32:05 +02:00
switch ( mode ) {
2018-05-07 13:24:29 +02:00
2020-10-14 09:32:05 +02:00
case VS : : CANVAS_LIGHT_MODE_ADD : {
glBlendEquation ( GL_FUNC_ADD ) ;
glBlendFunc ( GL_SRC_ALPHA , GL_ONE ) ;
2017-06-27 03:58:03 +02:00
2020-10-14 09:32:05 +02: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 ) ;
2017-12-26 19:13:00 +01:00
2020-10-14 09:32:05 +02:00
} break ;
2017-12-26 19:13:00 +01:00
}
}
2016-10-10 23:31:01 +02:00
2020-10-14 09:32:05 +02:00
if ( ! light_used ) {
2016-10-10 23:31:01 +02:00
2020-10-14 09:32:05 +02:00
state . canvas_shader . set_conditional ( CanvasShaderGLES3 : : USE_LIGHTING , true ) ;
light_used = true ;
}
2016-10-30 01:48:09 +02:00
2020-10-14 09:32:05 +02:00
bool has_shadow = light - > shadow_buffer . is_valid ( ) & & p_ci - > light_mask & light - > item_shadow_mask ;
state . canvas_shader . set_conditional ( CanvasShaderGLES3 : : USE_SHADOWS , has_shadow ) ;
if ( has_shadow ) {
state . canvas_shader . set_conditional ( CanvasShaderGLES3 : : SHADOW_USE_GRADIENT , light - > shadow_gradient_length > 0 ) ;
state . canvas_shader . set_conditional ( CanvasShaderGLES3 : : SHADOW_FILTER_NEAREST , light - > shadow_filter = = VS : : CANVAS_LIGHT_FILTER_NONE ) ;
state . canvas_shader . set_conditional ( CanvasShaderGLES3 : : SHADOW_FILTER_PCF3 , light - > shadow_filter = = VS : : CANVAS_LIGHT_FILTER_PCF3 ) ;
state . canvas_shader . set_conditional ( CanvasShaderGLES3 : : SHADOW_FILTER_PCF5 , light - > shadow_filter = = VS : : CANVAS_LIGHT_FILTER_PCF5 ) ;
state . canvas_shader . set_conditional ( CanvasShaderGLES3 : : SHADOW_FILTER_PCF7 , light - > shadow_filter = = VS : : CANVAS_LIGHT_FILTER_PCF7 ) ;
state . canvas_shader . set_conditional ( CanvasShaderGLES3 : : SHADOW_FILTER_PCF9 , light - > shadow_filter = = VS : : CANVAS_LIGHT_FILTER_PCF9 ) ;
state . canvas_shader . set_conditional ( CanvasShaderGLES3 : : SHADOW_FILTER_PCF13 , light - > shadow_filter = = VS : : CANVAS_LIGHT_FILTER_PCF13 ) ;
}
2016-10-30 01:48:09 +02:00
2020-10-14 09:32:05 +02:00
bool light_rebind = state . canvas_shader . bind ( ) ;
2016-10-10 23:31:01 +02:00
2020-10-14 09:32:05 +02:00
if ( light_rebind ) {
state . canvas_shader . set_uniform ( CanvasShaderGLES3 : : FINAL_MODULATE , state . canvas_item_modulate ) ;
state . canvas_shader . set_uniform ( CanvasShaderGLES3 : : MODELVIEW_MATRIX , state . final_transform ) ;
state . canvas_shader . set_uniform ( CanvasShaderGLES3 : : EXTRA_MATRIX , Transform2D ( ) ) ;
if ( storage - > frame . current_rt ) {
state . canvas_shader . set_uniform ( CanvasShaderGLES3 : : SCREEN_PIXEL_SIZE , Vector2 ( 1.0 / storage - > frame . current_rt - > width , 1.0 / storage - > frame . current_rt - > height ) ) ;
} else {
state . canvas_shader . set_uniform ( CanvasShaderGLES3 : : SCREEN_PIXEL_SIZE , Vector2 ( 1.0 , 1.0 ) ) ;
2018-07-16 16:43:26 +02:00
}
2020-10-14 09:32:05 +02:00
if ( state . using_skeleton ) {
state . canvas_shader . set_uniform ( CanvasShaderGLES3 : : SKELETON_TRANSFORM , state . skeleton_transform ) ;
state . canvas_shader . set_uniform ( CanvasShaderGLES3 : : SKELETON_TRANSFORM_INVERSE , state . skeleton_transform_inverse ) ;
2016-10-30 01:48:09 +02:00
}
2016-10-10 23:31:01 +02:00
}
2020-10-14 09:32:05 +02:00
glBindBufferBase ( GL_UNIFORM_BUFFER , 1 , static_cast < LightInternal * > ( light - > light_internal . get_data ( ) ) - > ubo ) ;
2018-05-07 13:24:29 +02:00
2020-10-14 09:32:05 +02:00
if ( has_shadow ) {
2018-05-07 13:24:29 +02:00
2020-10-14 09:32:05 +02:00
RasterizerStorageGLES3 : : CanvasLightShadow * cls = storage - > canvas_light_shadow_owner . get ( light - > shadow_buffer ) ;
glActiveTexture ( GL_TEXTURE0 + storage - > config . max_texture_image_units - 2 ) ;
glBindTexture ( GL_TEXTURE_2D , cls - > distance ) ;
2016-10-03 21:33:42 +02:00
2020-10-14 09:32:05 +02:00
/*canvas_shader.set_uniform(CanvasShaderGLES3::SHADOW_MATRIX,light->shadow_matrix_cache);
canvas_shader . set_uniform ( CanvasShaderGLES3 : : SHADOW_ESM_MULTIPLIER , light - > shadow_esm_mult ) ;
canvas_shader . set_uniform ( CanvasShaderGLES3 : : LIGHT_SHADOW_COLOR , light - > shadow_color ) ; */
}
2016-10-03 21:33:42 +02:00
2020-10-14 09:32:05 +02:00
glActiveTexture ( GL_TEXTURE0 + storage - > config . max_texture_image_units - 1 ) ;
RasterizerStorageGLES3 : : Texture * t = storage - > texture_owner . getornull ( light - > texture ) ;
if ( ! t ) {
glBindTexture ( GL_TEXTURE_2D , storage - > resources . white_tex ) ;
} else {
t = t - > get_ptr ( ) ;
2016-10-03 21:33:42 +02:00
2020-10-14 09:32:05 +02:00
glBindTexture ( t - > target , t - > tex_id ) ;
}
2016-10-03 21:33:42 +02:00
2020-10-14 09:32:05 +02:00
glActiveTexture ( GL_TEXTURE0 ) ;
2018-01-11 23:38:03 +01:00
2020-10-14 09:32:05 +02: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 ) {
render_joined_item_commands ( p_bij , NULL , reclip , nullptr , true ) ;
} else {
bool scissor = _light_scissor_begin ( p_bij . bounding_rect , light - > xform_cache , light - > rect_cache ) ;
render_joined_item_commands ( p_bij , NULL , reclip , nullptr , true ) ;
if ( scissor ) {
glDisable ( GL_SCISSOR_TEST ) ;
2018-01-11 23:38:03 +01:00
}
2020-10-14 09:32:05 +02:00
}
2016-10-03 21:33:42 +02:00
}
2020-10-14 09:32:05 +02:00
light = light - > next_ptr ;
2017-06-27 03:58:03 +02:00
}
2016-10-03 21:33:42 +02:00
2020-10-14 09:32:05 +02:00
if ( light_used ) {
2016-10-03 21:33:42 +02:00
2020-10-14 09:32:05 +02:00
state . canvas_shader . set_conditional ( CanvasShaderGLES3 : : USE_LIGHTING , false ) ;
state . canvas_shader . set_conditional ( CanvasShaderGLES3 : : USE_SHADOWS , false ) ;
state . canvas_shader . set_conditional ( CanvasShaderGLES3 : : SHADOW_FILTER_NEAREST , false ) ;
state . canvas_shader . set_conditional ( CanvasShaderGLES3 : : SHADOW_FILTER_PCF3 , false ) ;
state . canvas_shader . set_conditional ( CanvasShaderGLES3 : : SHADOW_FILTER_PCF5 , false ) ;
state . canvas_shader . set_conditional ( CanvasShaderGLES3 : : SHADOW_FILTER_PCF7 , false ) ;
state . canvas_shader . set_conditional ( CanvasShaderGLES3 : : SHADOW_FILTER_PCF9 , false ) ;
state . canvas_shader . set_conditional ( CanvasShaderGLES3 : : SHADOW_FILTER_PCF13 , false ) ;
2016-10-03 21:33:42 +02:00
2020-10-14 09:32:05 +02:00
state . canvas_shader . bind ( ) ;
2016-10-03 21:33:42 +02:00
2020-10-14 09:32:05 +02:00
r_ris . last_blend_mode = - 1 ;
2016-10-03 21:33:42 +02:00
2020-10-14 09:32:05 +02: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 ) ;
2016-10-03 21:33:42 +02:00
2020-10-14 09:32:05 +02:00
state . canvas_shader . set_uniform ( CanvasShaderGLES3 : : MODELVIEW_MATRIX , state . final_transform ) ;
state . canvas_shader . set_uniform ( CanvasShaderGLES3 : : EXTRA_MATRIX , Transform2D ( ) ) ;
state . canvas_shader . set_uniform ( CanvasShaderGLES3 : : FINAL_MODULATE , state . canvas_item_modulate ) ;
2016-10-03 21:33:42 +02:00
2020-10-14 09:32:05 +02:00
glBlendEquation ( GL_FUNC_ADD ) ;
2016-10-03 21:33:42 +02:00
2020-10-14 09:32:05 +02: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 ) ;
}
2016-10-03 21:33:42 +02:00
2020-10-14 09:32:05 +02:00
//@TODO RESET canvas_blend_mode
*/
}
}
2016-10-03 21:33:42 +02:00
2020-10-14 09:32:05 +02:00
if ( reclip ) {
2016-10-03 21:33:42 +02:00
2020-10-14 09:32:05 +02:00
glEnable ( GL_SCISSOR_TEST ) ;
int y = storage - > frame . current_rt - > height - ( r_ris . current_clip - > final_clip_rect . position . y + r_ris . current_clip - > final_clip_rect . size . y ) ;
if ( storage - > frame . current_rt - > flags [ RasterizerStorage : : RENDER_TARGET_VFLIP ] )
y = r_ris . current_clip - > final_clip_rect . position . y ;
glScissor ( r_ris . current_clip - > final_clip_rect . position . x , y , r_ris . current_clip - > final_clip_rect . size . width , r_ris . current_clip - > final_clip_rect . size . height ) ;
}
}
2016-10-03 21:33:42 +02:00
2020-10-14 09:32:05 +02: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 RasterizerCanvasGLES3 : : try_join_item ( Item * p_ci , RenderItemState & r_ris , bool & r_batch_break ) {
// if we set max join items to zero we can effectively prevent any joining, so
// none of the other logic needs to run. Good for testing regression bugs, and
// could conceivably be faster in some games.
if ( ! bdata . settings_max_join_item_commands ) {
return false ;
}
2016-10-03 21:33:42 +02:00
2020-10-14 09:32:05 +02:00
// if there are any state changes we change join to false
// we also set r_batch_break to true if we don't want this item joined to the next
// (e.g. an item that must not be joined at all)
r_batch_break = false ;
bool join = true ;
2016-10-03 21:33:42 +02:00
2020-10-14 09:32:05 +02:00
// light_masked may possibly need state checking here. Check for regressions!
2016-10-03 21:33:42 +02:00
2020-10-14 09:32:05 +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;
// }
2016-10-03 21:33:42 +02:00
2020-10-14 09:32:05 +02:00
if ( r_ris . current_clip ! = p_ci - > final_clip_owner ) {
r_ris . current_clip = p_ci - > final_clip_owner ;
join = false ;
}
2016-10-03 21:33:42 +02:00
2020-10-14 09:32:05 +02:00
// TODO: copy back buffer
2016-10-03 21:33:42 +02:00
2020-10-14 09:32:05 +02:00
if ( p_ci - > copy_back_buffer ) {
join = false ;
}
2016-10-03 21:33:42 +02:00
2020-10-14 09:32:05 +02:00
RasterizerStorageGLES3 : : Skeleton * skeleton = NULL ;
2016-10-03 21:33:42 +02:00
2020-10-14 09:32:05 +02: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 ) {
skeleton = NULL ;
}
}
2016-10-03 21:33:42 +02:00
2020-10-14 09:32:05 +02:00
bool skeleton_prevent_join = false ;
2016-10-03 21:33:42 +02:00
2020-10-14 09:32:05 +02:00
bool use_skeleton = skeleton ! = NULL ;
if ( r_ris . prev_use_skeleton ! = use_skeleton ) {
2016-10-03 21:33:42 +02:00
2020-10-14 09:32:05 +02:00
if ( ! bdata . settings_use_software_skinning )
r_ris . rebind_shader = true ;
2016-10-03 21:33:42 +02:00
2020-10-14 09:32:05 +02:00
r_ris . prev_use_skeleton = use_skeleton ;
// join = false;
skeleton_prevent_join = true ;
}
2016-10-03 21:33:42 +02:00
2020-10-14 09:32:05 +02:00
if ( skeleton ) {
// join = false;
skeleton_prevent_join = true ;
state . using_skeleton = true ;
} else {
state . using_skeleton = false ;
}
2016-10-03 21:33:42 +02:00
2020-10-14 09:32:05 +02:00
if ( skeleton_prevent_join ) {
if ( ! bdata . settings_use_software_skinning )
join = false ;
}
}
2016-10-03 21:33:42 +02:00
2020-10-14 09:32:05 +02:00
Item * material_owner = p_ci - > material_owner ? p_ci - > material_owner : p_ci ;
2016-10-03 21:33:42 +02:00
2020-10-14 09:32:05 +02:00
RID material = material_owner - > material ;
RasterizerStorageGLES3 : : Material * material_ptr = storage - > material_owner . getornull ( material ) ;
2016-10-10 23:31:01 +02:00
2020-10-14 09:32:05 +02:00
if ( material ! = r_ris . canvas_last_material | | r_ris . rebind_shader ) {
2016-10-03 21:33:42 +02:00
2020-10-14 09:32:05 +02:00
join = false ;
RasterizerStorageGLES3 : : Shader * shader_ptr = NULL ;
2016-10-03 21:33:42 +02:00
2020-10-14 09:32:05 +02:00
if ( material_ptr ) {
shader_ptr = material_ptr - > shader ;
2016-10-03 21:33:42 +02:00
2020-10-14 09:32:05 +02:00
if ( shader_ptr & & shader_ptr - > mode ! = VS : : SHADER_CANVAS_ITEM ) {
shader_ptr = NULL ; // not a canvas item shader, don't use.
}
}
2016-10-03 21:33:42 +02:00
2020-10-14 09:32:05 +02:00
if ( shader_ptr ) {
if ( shader_ptr - > canvas_item . uses_screen_texture ) {
if ( ! state . canvas_texscreen_used ) {
join = false ;
2016-10-03 21:33:42 +02:00
}
}
}
2020-10-14 09:32:05 +02:00
r_ris . shader_cache = shader_ptr ;
2016-10-03 21:33:42 +02:00
2020-10-14 09:32:05 +02:00
r_ris . canvas_last_material = material ;
2016-10-03 21:33:42 +02:00
2020-10-14 09:32:05 +02:00
r_ris . rebind_shader = false ;
2016-10-03 21:33:42 +02:00
}
2020-10-14 09:32:05 +02:00
int blend_mode = r_ris . shader_cache ? r_ris . shader_cache - > canvas_item . blend_mode : RasterizerStorageGLES3 : : Shader : : CanvasItem : : BLEND_MODE_MIX ;
bool unshaded = r_ris . shader_cache & & ( r_ris . shader_cache - > canvas_item . light_mode = = RasterizerStorageGLES3 : : Shader : : CanvasItem : : LIGHT_MODE_UNSHADED | | ( blend_mode ! = RasterizerStorageGLES3 : : Shader : : CanvasItem : : BLEND_MODE_MIX & & blend_mode ! = RasterizerStorageGLES3 : : Shader : : CanvasItem : : BLEND_MODE_PMALPHA ) ) ;
bool reclip = false ;
// we are precalculating the final_modulate ahead of time because we need this for baking of final modulate into vertex colors
// (only in software transform mode)
// This maybe inefficient storing it...
r_ris . final_modulate = unshaded ? p_ci - > final_modulate : ( p_ci - > final_modulate * r_ris . item_group_modulate ) ;
if ( r_ris . last_blend_mode ! = blend_mode ) {
join = false ;
r_ris . last_blend_mode = blend_mode ;
2016-10-03 21:33:42 +02:00
}
2020-10-14 09:32:05 +02:00
// does the shader contain BUILTINs which should break the batching?
bdata . joined_item_batch_flags = 0 ;
if ( r_ris . shader_cache ) {
2016-10-03 21:33:42 +02:00
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-10-14 09:32:05 +02:00
if ( and_flags ) {
2016-10-03 21:33:42 +02:00
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
2016-10-03 21:33:42 +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-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
}
2016-10-03 21:33:42 +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 ;
}
} // if not prevent item joining
2016-10-03 21:33:42 +02:00
}
}
2019-02-12 18:33:57 +01:00
2020-10-14 09:32:05 +02:00
if ( ( blend_mode = = RasterizerStorageGLES3 : : Shader : : CanvasItem : : BLEND_MODE_MIX | | blend_mode = = RasterizerStorageGLES3 : : Shader : : CanvasItem : : BLEND_MODE_PMALPHA ) & & r_ris . item_group_light & & ! unshaded ) {
2016-10-03 21:33:42 +02:00
2020-10-14 09:32:05 +02:00
// 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
2016-10-03 21:33:42 +02:00
2020-10-14 09:32:05 +02:00
bool light_allow_join = true ;
2016-10-03 21:33:42 +02:00
2020-10-14 09:32:05 +02:00
// 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...
2016-10-03 21:33:42 +02:00
2020-10-14 09:32:05 +02:00
// first calculate the light bitfield
uint64_t light_bitfield = 0 ;
uint64_t shadow_bitfield = 0 ;
Light * light = r_ris . item_group_light ;
2016-10-03 21:33:42 +02:00
2020-10-14 09:32:05 +02:00
int light_count = - 1 ;
while ( light ) {
light_count + + ;
uint64_t light_bit = 1ULL < < light_count ;
2016-10-03 21:33:42 +02:00
2020-10-14 09:32:05 +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 ) {
2016-10-03 21:33:42 +02:00
2020-10-14 09:32:05 +02:00
// 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)) {
2016-10-03 21:33:42 +02:00
2020-10-14 09:32:05 +02:00
light_bitfield | = light_bit ;
2016-10-03 21:33:42 +02:00
2020-10-14 09:32:05 +02:00
bool has_shadow = light - > shadow_buffer . is_valid ( ) & & p_ci - > light_mask & light - > item_shadow_mask ;
2016-10-03 21:33:42 +02:00
2020-10-14 09:32:05 +02:00
if ( has_shadow ) {
shadow_bitfield | = light_bit ;
}
}
2016-10-03 21:33:42 +02:00
2020-10-14 09:32:05 +02:00
light = light - > next_ptr ;
}
2016-10-03 21:33:42 +02:00
2020-10-14 09:32:05 +02:00
// 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 ;
2016-10-03 21:33:42 +02:00
2020-10-14 09:32:05 +02:00
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 ;
}
}
2016-10-03 21:33:42 +02:00
2020-10-14 09:32:05 +02:00
# ifdef DEBUG_ENABLED
if ( light_allow_join ) {
bdata . stats_light_items_joined + + ;
}
# endif
2016-10-03 21:33:42 +02:00
2020-10-14 09:32:05 +02:00
} // if below max join items
else {
// just don't allow joining if above overlap check max items
light_allow_join = false ;
}
}
2016-10-03 21:33:42 +02:00
2020-10-14 09:32:05 +02:00
} // if not batch broken already (no point in doing expensive overlap tests if not needed)
} // if bitfields don't match
} // if do light joining
2016-10-03 21:33:42 +02:00
2020-10-14 09:32:05 +02:00
if ( ! light_allow_join ) {
// can't join
join = false ;
// we also dont want to allow joining this item with the next item, because the next item could have no lights!
r_batch_break = true ;
}
2016-10-03 21:33:42 +02:00
2020-10-14 09:32:05 +02:00
} else {
// 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 ;
}
}
2016-10-03 21:33:42 +02:00
2020-10-14 09:32:05 +02:00
if ( reclip ) {
join = false ;
}
2016-10-03 21:33:42 +02:00
2020-10-14 09:32:05 +02:00
// non rects will break the batching anyway, we don't want to record item changes, detect this
if ( ! r_batch_break & & _detect_item_batch_break ( r_ris , p_ci , r_batch_break ) ) {
join = false ;
r_batch_break = true ;
}
2016-10-03 21:33:42 +02:00
2020-10-14 09:32:05 +02:00
return join ;
}
2016-10-03 21:33:42 +02:00
2020-10-14 09:32:05 +02:00
void RasterizerCanvasGLES3 : : canvas_render_items_implementation ( Item * p_item_list , int p_z , const Color & p_modulate , Light * p_light , const Transform2D & p_base_transform ) {
2019-12-10 23:05:15 +01:00
2020-10-14 09:32:05 +02:00
// parameters are easier to pass around in a structure
RenderItemState ris ;
ris . item_group_z = p_z ;
ris . item_group_modulate = p_modulate ;
ris . item_group_light = p_light ;
ris . item_group_base_transform = p_base_transform ;
ris . prev_distance_field = false ;
2019-12-10 23:05:15 +01:00
2020-10-14 09:32:05 +02:00
glBindBuffer ( GL_UNIFORM_BUFFER , state . canvas_item_ubo ) ;
2020-10-25 16:38:54 +01:00
glBufferData ( GL_UNIFORM_BUFFER , sizeof ( CanvasItemUBO ) , & state . canvas_item_ubo_data , _buffer_upload_usage_flag ) ;
2020-10-14 09:32:05 +02:00
glBindBuffer ( GL_UNIFORM_BUFFER , 0 ) ;
2019-12-10 23:05:15 +01:00
2020-10-14 09:32:05 +02:00
state . current_tex = RID ( ) ;
state . current_tex_ptr = NULL ;
state . current_normal = RID ( ) ;
glActiveTexture ( GL_TEXTURE0 ) ;
glBindTexture ( GL_TEXTURE_2D , storage - > resources . white_tex ) ;
2016-10-03 21:33:42 +02:00
2020-10-14 09:32:05 +02:00
// state.canvas_shader.set_conditional(CanvasShaderGLES3::USE_SKELETON, false);
// state.current_tex = RID();
// state.current_tex_ptr = NULL;
// state.current_normal = RID();
// state.canvas_texscreen_used = false;
// glActiveTexture(GL_TEXTURE0);
// glBindTexture(GL_TEXTURE_2D, storage->resources.white_tex);
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 ) {
2016-10-03 21:33:42 +02:00
2020-10-14 09:32:05 +02:00
Item * ci = p_item_list ;
_legacy_canvas_render_item ( ci , ris ) ;
p_item_list = p_item_list - > next ;
}
}
2016-10-03 21:33:42 +02:00
2020-10-14 09:32:05 +02:00
if ( ris . current_clip ) {
glDisable ( GL_SCISSOR_TEST ) ;
}
2016-10-03 21:33:42 +02:00
2020-10-14 09:32:05 +02:00
state . canvas_shader . set_conditional ( CanvasShaderGLES3 : : USE_SKELETON , false ) ;
}
2016-10-03 21:33:42 +02:00
2020-10-14 09:32:05 +02:00
void RasterizerCanvasGLES3 : : _batch_upload_buffers ( ) {
2018-08-24 09:35:07 +02:00
2020-10-14 09:32:05 +02:00
// noop?
if ( ! bdata . vertices . size ( ) )
return ;
2016-10-03 21:33:42 +02:00
2020-10-14 09:32:05 +02:00
glBindBuffer ( GL_ARRAY_BUFFER , bdata . gl_vertex_buffer ) ;
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 ) {
glBufferData ( GL_ARRAY_BUFFER , 0 , 0 , buffer_usage_flag ) ; // GL_DYNAMIC_DRAW);
}
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 ;
2016-10-03 21:33:42 +02:00
}
2020-10-14 09:32:05 +02:00
// might not be necessary
glBindBuffer ( GL_ARRAY_BUFFER , 0 ) ;
2016-10-03 21:33:42 +02:00
}
2020-10-14 09:32:05 +02:00
void RasterizerCanvasGLES3 : : _batch_render_lines ( const Batch & p_batch , RasterizerStorageGLES3 : : Material * p_material , bool p_anti_alias ) {
2016-10-03 21:33:42 +02:00
2020-10-14 09:32:05 +02:00
_set_texture_rect_mode ( false ) ;
2016-10-03 21:33:42 +02:00
2020-10-14 09:32:05 +02:00
_bind_canvas_texture ( RID ( ) , RID ( ) ) ;
2017-06-27 03:58:03 +02:00
2020-10-14 09:32:05 +02:00
glBindVertexArray ( batch_gl_data . batch_vertex_array [ 0 ] ) ;
2016-10-03 21:33:42 +02:00
2020-10-14 09:32:05 +02:00
glDisableVertexAttribArray ( VS : : ARRAY_COLOR ) ;
glVertexAttrib4fv ( VS : : ARRAY_COLOR , ( float * ) & p_batch . color ) ;
2016-10-03 21:33:42 +02:00
2020-10-14 09:32:05 +02:00
int64_t offset = p_batch . first_vert ; // 6 inds per quad at 2 bytes each
2016-10-03 21:33:42 +02:00
2020-10-14 09:32:05 +02:00
int num_elements = p_batch . num_commands * 2 ;
2016-10-03 21:33:42 +02:00
2020-10-14 09:32:05 +02:00
# ifdef GLES_OVER_GL
if ( p_anti_alias )
glEnable ( GL_LINE_SMOOTH ) ;
# endif
2016-10-03 21:33:42 +02:00
2020-10-14 09:32:05 +02:00
glDrawArrays ( GL_LINES , offset , num_elements ) ;
2016-10-03 21:33:42 +02:00
2020-10-14 09:32:05 +02:00
storage - > info . render . _2d_draw_call_count + + ;
2016-10-03 21:33:42 +02:00
2020-10-14 09:32:05 +02:00
glBindVertexArray ( 0 ) ;
2016-10-03 21:33:42 +02:00
2020-10-14 09:32:05 +02:00
# ifdef GLES_OVER_GL
if ( p_anti_alias )
glDisable ( GL_LINE_SMOOTH ) ;
# endif
2016-10-03 21:33:42 +02:00
}
2020-11-12 20:16:33 +01:00
void RasterizerCanvasGLES3 : : _batch_render_prepare ( ) {
//const bool &colored_verts = bdata.use_colored_vertices;
const bool & use_light_angles = bdata . use_light_angles ;
const bool & use_modulate = bdata . use_modulate ;
const bool & use_large_verts = bdata . use_large_verts ;
2018-09-30 22:51:50 +02:00
2020-11-12 20:16:33 +01:00
_set_texture_rect_mode ( false , false , use_light_angles , use_modulate , use_large_verts ) ;
2020-10-14 09:32:05 +02:00
2020-11-12 20:16:33 +01:00
// state.canvas_shader.set_uniform(CanvasShaderGLES3::CLIP_RECT_UV, p_rect->flags & CANVAS_RECT_CLIP_UV);
state . canvas_shader . set_uniform ( CanvasShaderGLES3 : : CLIP_RECT_UV , false ) ;
2018-09-30 22:51:50 +02:00
2020-11-12 20:16:33 +01:00
switch ( bdata . fvf ) {
case RasterizerStorageCommon : : FVF_UNBATCHED : // should not happen
return ;
break ;
case RasterizerStorageCommon : : FVF_REGULAR : // no change
glBindVertexArray ( batch_gl_data . batch_vertex_array [ 0 ] ) ;
break ;
case RasterizerStorageCommon : : FVF_COLOR :
glBindVertexArray ( batch_gl_data . batch_vertex_array [ 1 ] ) ;
break ;
case RasterizerStorageCommon : : FVF_LIGHT_ANGLE :
glBindVertexArray ( batch_gl_data . batch_vertex_array [ 2 ] ) ;
break ;
case RasterizerStorageCommon : : FVF_MODULATED :
glBindVertexArray ( batch_gl_data . batch_vertex_array [ 3 ] ) ;
break ;
case RasterizerStorageCommon : : FVF_LARGE :
glBindVertexArray ( batch_gl_data . batch_vertex_array [ 4 ] ) ;
break ;
}
2018-09-30 22:51:50 +02:00
}
2020-11-12 20:16:33 +01:00
void RasterizerCanvasGLES3 : : _batch_render_generic ( const Batch & p_batch , RasterizerStorageGLES3 : : Material * p_material ) {
2020-10-14 09:32:05 +02:00
ERR_FAIL_COND ( p_batch . num_commands < = 0 ) ;
2017-08-07 23:06:57 +02:00
2020-10-14 09:32:05 +02:00
const bool & use_light_angles = bdata . use_light_angles ;
const bool & use_modulate = bdata . use_modulate ;
const bool & use_large_verts = bdata . use_large_verts ;
2020-11-12 20:16:33 +01:00
const bool & colored_verts = bdata . use_colored_vertices | use_light_angles | use_modulate | use_large_verts ;
2017-08-07 23:06:57 +02:00
2020-10-14 09:32:05 +02:00
_set_texture_rect_mode ( false , false , use_light_angles , use_modulate , use_large_verts ) ;
2017-08-07 23:06:57 +02:00
2020-10-14 09:32:05 +02:00
// state.canvas_shader.set_uniform(CanvasShaderGLES3::CLIP_RECT_UV, p_rect->flags & CANVAS_RECT_CLIP_UV);
state . canvas_shader . set_uniform ( CanvasShaderGLES3 : : CLIP_RECT_UV , false ) ;
2017-08-07 23:06:57 +02:00
2020-10-14 09:32:05 +02:00
switch ( bdata . fvf ) {
case RasterizerStorageCommon : : FVF_UNBATCHED : // should not happen
return ;
break ;
case RasterizerStorageCommon : : FVF_REGULAR : // no change
glBindVertexArray ( batch_gl_data . batch_vertex_array [ 0 ] ) ;
break ;
case RasterizerStorageCommon : : FVF_COLOR :
glBindVertexArray ( batch_gl_data . batch_vertex_array [ 1 ] ) ;
break ;
case RasterizerStorageCommon : : FVF_LIGHT_ANGLE :
glBindVertexArray ( batch_gl_data . batch_vertex_array [ 2 ] ) ;
break ;
case RasterizerStorageCommon : : FVF_MODULATED :
glBindVertexArray ( batch_gl_data . batch_vertex_array [ 3 ] ) ;
break ;
case RasterizerStorageCommon : : FVF_LARGE :
glBindVertexArray ( batch_gl_data . batch_vertex_array [ 4 ] ) ;
break ;
2017-08-07 23:06:57 +02:00
}
2020-10-14 09:32:05 +02:00
// batch tex
const BatchTex & tex = bdata . batch_textures [ p_batch . batch_texture_id ] ;
2017-08-07 23:06:57 +02:00
2020-10-14 09:32:05 +02:00
_bind_canvas_texture ( tex . RID_texture , tex . RID_normal ) ;
2017-08-07 23:06:57 +02:00
2020-10-14 09:32:05 +02:00
if ( ! colored_verts ) {
// may not need this disable
glDisableVertexAttribArray ( VS : : ARRAY_COLOR ) ;
glVertexAttrib4fv ( VS : : ARRAY_COLOR , p_batch . color . get_data ( ) ) ;
}
2017-08-07 23:06:57 +02:00
2020-10-14 09:32:05 +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 ;
switch ( tex . tile_mode ) {
case BatchTex : : TILE_NORMAL : {
// if the texture is imported as tiled, no need to set GL state, as it will already be bound with repeat
if ( ! tex_is_already_tiled ) {
glTexParameterf ( GL_TEXTURE_2D , GL_TEXTURE_WRAP_S , GL_REPEAT ) ;
glTexParameterf ( GL_TEXTURE_2D , GL_TEXTURE_WRAP_T , GL_REPEAT ) ;
}
} break ;
default : {
} break ;
2017-08-07 23:06:57 +02:00
}
2020-10-14 09:32:05 +02: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 ( CanvasShaderGLES3 : : 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 ; // 6 inds per quad at 2 bytes each
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 ;
2017-08-07 23:06:57 +02:00
2020-11-12 20:16:33 +01:00
int num_elements = p_batch . num_commands ;
glDrawArrays ( GL_TRIANGLES , offset , num_elements ) ;
} break ;
}
2017-08-07 23:06:57 +02:00
2020-10-14 09:32:05 +02:00
storage - > info . render . _2d_draw_call_count + + ;
2017-08-07 23:06:57 +02:00
2020-10-14 09:32:05 +02:00
glBindVertexArray ( 0 ) ;
2017-08-07 23:06:57 +02:00
2020-10-14 09:32:05 +02:00
// gl_checkerror();
2017-08-07 23:06:57 +02:00
2020-10-14 09:32:05 +02:00
switch ( tex . tile_mode ) {
case BatchTex : : TILE_NORMAL : {
// if the texture is imported as tiled, no need to revert GL state
if ( ! tex_is_already_tiled ) {
glTexParameterf ( GL_TEXTURE_2D , GL_TEXTURE_WRAP_S , GL_CLAMP_TO_EDGE ) ;
glTexParameterf ( GL_TEXTURE_2D , GL_TEXTURE_WRAP_T , GL_CLAMP_TO_EDGE ) ;
}
} break ;
default : {
} break ;
2017-08-07 23:06:57 +02:00
}
2020-10-14 09:32:05 +02:00
/*
// may not be necessary .. state change optimization still TODO
glBindBuffer ( GL_ARRAY_BUFFER , 0 ) ;
glBindBuffer ( GL_ELEMENT_ARRAY_BUFFER , 0 ) ;
*/
2017-08-07 23:06:57 +02:00
}
2016-10-03 21:33:42 +02:00
void RasterizerCanvasGLES3 : : initialize ( ) {
2020-10-14 09:32:05 +02:00
gl_checkerror ( ) ;
RasterizerCanvasBaseGLES3 : : initialize ( ) ;
2016-10-03 21:33:42 +02:00
2020-10-14 09:32:05 +02:00
batch_initialize ( ) ;
2017-06-21 21:25:45 +02:00
2020-10-14 09:32:05 +02:00
// just reserve some space (may not be needed as we are orphaning, but hey ho)
glGenBuffers ( 1 , & bdata . gl_vertex_buffer ) ;
2016-11-23 00:51:56 +01:00
2020-10-14 09:32:05 +02:00
if ( bdata . vertex_buffer_size_bytes ) {
glBindBuffer ( GL_ARRAY_BUFFER , bdata . gl_vertex_buffer ) ;
glBufferData ( GL_ARRAY_BUFFER , bdata . vertex_buffer_size_bytes , NULL , GL_DYNAMIC_DRAW ) ;
2017-03-05 16:44:50 +01:00
glBindBuffer ( GL_ARRAY_BUFFER , 0 ) ;
2016-11-23 00:51:56 +01:00
2020-10-14 09:32:05 +02: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 ) ;
2016-11-23 00:51:56 +01:00
2020-10-14 09:32:05 +02:00
Vector < uint16_t > indices ;
indices . resize ( bdata . index_buffer_size_units ) ;
2016-11-23 00:51:56 +01:00
2020-10-14 09:32:05 +02:00
for ( unsigned int q = 0 ; q < bdata . max_quads ; q + + ) {
int i_pos = q * 6 ; // 6 inds per quad
int q_pos = q * 4 ; // 4 verts per quad
indices . set ( i_pos , q_pos ) ;
indices . set ( i_pos + 1 , q_pos + 1 ) ;
indices . set ( i_pos + 2 , q_pos + 2 ) ;
indices . set ( i_pos + 3 , q_pos ) ;
indices . set ( i_pos + 4 , q_pos + 2 ) ;
indices . set ( i_pos + 5 , q_pos + 3 ) ;
GLES3 fix normal map flipping with nvidia workaround
When not using TEXTURE_RECT path, flips have to sent via another method to the shader, to ensure that normal maps are correctly adjusted for direction. This PR adds an extra vertex attribute, LIGHT_ANGLE.
For nvidia workarounds, where the shader still has access to the final transform and extra matrix, the LIGHT_ANGLE can be 0 (no adjustment), 180 degrees for a horizontal flip, and negative indicates a vertical flip.
For batching path, the LIGHT_ANGLE can be used to directly specify the light angle for normal mapping, even when the final transform and extra matrix have been baked into vertex positions, so the same shader can be used for both.
2020-08-02 08:49:03 +02:00
2020-10-14 09:32:05 +02:00
// we can only use 16 bit indices in GLES2!
# ifdef DEBUG_ENABLED
CRASH_COND ( ( q_pos + 3 ) > 65535 ) ;
# endif
2016-11-23 00:51:56 +01:00
}
2017-06-12 23:56:16 +02:00
2020-10-14 09:32:05 +02:00
glBufferData ( GL_ELEMENT_ARRAY_BUFFER , bdata . index_buffer_size_bytes , & indices [ 0 ] , GL_STATIC_DRAW ) ;
2017-06-12 23:56:16 +02:00
glBindBuffer ( GL_ELEMENT_ARRAY_BUFFER , 0 ) ;
2019-11-10 19:51:56 +01:00
2020-10-14 09:32:05 +02:00
} // only if there is a vertex buffer (batching is on)
// vertex array objects
for ( int vao = 0 ; vao < 5 ; vao + + ) {
int sizeof_vert ;
switch ( vao ) {
case 0 :
sizeof_vert = sizeof ( BatchVertex ) ;
break ;
case 1 :
sizeof_vert = sizeof ( BatchVertexColored ) ;
break ;
case 2 :
sizeof_vert = sizeof ( BatchVertexLightAngled ) ;
break ;
case 3 :
sizeof_vert = sizeof ( BatchVertexModulated ) ;
break ;
case 4 :
sizeof_vert = sizeof ( BatchVertexLarge ) ;
break ;
}
2017-12-19 18:18:07 +01:00
2020-10-14 09:32:05 +02:00
glGenVertexArrays ( 1 , & batch_gl_data . batch_vertex_array [ vao ] ) ;
glBindVertexArray ( batch_gl_data . batch_vertex_array [ vao ] ) ;
glBindBuffer ( GL_ARRAY_BUFFER , bdata . gl_vertex_buffer ) ;
glBindBuffer ( GL_ELEMENT_ARRAY_BUFFER , bdata . gl_index_buffer ) ;
2016-10-03 21:33:42 +02:00
2020-10-14 09:32:05 +02:00
uint64_t pointer = 0 ;
glEnableVertexAttribArray ( VS : : ARRAY_VERTEX ) ;
glVertexAttribPointer ( VS : : ARRAY_VERTEX , 2 , GL_FLOAT , GL_FALSE , sizeof_vert , ( const void * ) pointer ) ;
2016-10-03 21:33:42 +02:00
2020-10-14 09:32:05 +02:00
// always send UVs, even within a texture specified because a shader can still use UVs
glEnableVertexAttribArray ( VS : : ARRAY_TEX_UV ) ;
glVertexAttribPointer ( VS : : ARRAY_TEX_UV , 2 , GL_FLOAT , GL_FALSE , sizeof_vert , CAST_INT_TO_UCHAR_PTR ( pointer + ( 2 * 4 ) ) ) ;
// optional attributes
bool a_color = false ;
bool a_light_angle = false ;
bool a_modulate = false ;
bool a_large = false ;
switch ( vao ) {
case 0 :
break ;
case 1 : {
a_color = true ;
} break ;
case 2 : {
a_color = true ;
a_light_angle = true ;
} break ;
case 3 : {
a_color = true ;
a_light_angle = true ;
a_modulate = true ;
} break ;
case 4 : {
a_color = true ;
a_light_angle = true ;
a_modulate = true ;
a_large = true ;
} break ;
}
2017-06-12 23:56:16 +02:00
2020-10-14 09:32:05 +02:00
if ( a_color ) {
glEnableVertexAttribArray ( VS : : ARRAY_COLOR ) ;
glVertexAttribPointer ( VS : : ARRAY_COLOR , 4 , GL_FLOAT , GL_FALSE , sizeof_vert , CAST_INT_TO_UCHAR_PTR ( pointer + ( 4 * 4 ) ) ) ;
}
if ( a_light_angle ) {
glEnableVertexAttribArray ( VS : : ARRAY_TANGENT ) ;
glVertexAttribPointer ( VS : : ARRAY_TANGENT , 1 , GL_FLOAT , GL_FALSE , sizeof_vert , CAST_INT_TO_UCHAR_PTR ( pointer + ( 8 * 4 ) ) ) ;
}
if ( a_modulate ) {
glEnableVertexAttribArray ( VS : : ARRAY_TEX_UV2 ) ;
glVertexAttribPointer ( VS : : ARRAY_TEX_UV2 , 4 , GL_FLOAT , GL_FALSE , sizeof_vert , CAST_INT_TO_UCHAR_PTR ( pointer + ( 9 * 4 ) ) ) ;
}
if ( a_large ) {
glEnableVertexAttribArray ( VS : : ARRAY_BONES ) ;
glVertexAttribPointer ( VS : : ARRAY_BONES , 2 , GL_FLOAT , GL_FALSE , sizeof_vert , CAST_INT_TO_UCHAR_PTR ( pointer + ( 13 * 4 ) ) ) ;
glEnableVertexAttribArray ( VS : : ARRAY_WEIGHTS ) ;
glVertexAttribPointer ( VS : : ARRAY_WEIGHTS , 4 , GL_FLOAT , GL_FALSE , sizeof_vert , CAST_INT_TO_UCHAR_PTR ( pointer + ( 15 * 4 ) ) ) ;
}
2017-06-21 21:25:45 +02:00
2020-10-14 09:32:05 +02:00
glBindVertexArray ( 0 ) ;
} // for vao
gl_checkerror ( ) ;
2016-10-03 21:33:42 +02:00
}
2017-03-05 16:44:50 +01:00
RasterizerCanvasGLES3 : : RasterizerCanvasGLES3 ( ) {
2020-05-05 17:25:45 +02:00
2020-10-14 09:32:05 +02:00
batch_constructor ( ) ;
2016-10-03 21:33:42 +02:00
}