Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added render_mode world_vertex_coords in canvas #24002

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions drivers/gles2/rasterizer_canvas_gles2.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,8 @@ void RasterizerCanvasGLES2::_set_uniforms() {

state.canvas_shader.set_uniform(CanvasShaderGLES2::PROJECTION_MATRIX, state.uniforms.projection_matrix);
state.canvas_shader.set_uniform(CanvasShaderGLES2::MODELVIEW_MATRIX, state.uniforms.modelview_matrix);
state.canvas_shader.set_uniform(CanvasShaderGLES2::WORLD_MATRIX, state.uniforms.world_matrix);
state.canvas_shader.set_uniform(CanvasShaderGLES2::INV_WORLD_MATRIX, state.uniforms.inv_world_matrix);
state.canvas_shader.set_uniform(CanvasShaderGLES2::EXTRA_MATRIX, state.uniforms.extra_matrix);

state.canvas_shader.set_uniform(CanvasShaderGLES2::FINAL_MODULATE, state.uniforms.final_modulate);
Expand Down Expand Up @@ -178,6 +180,8 @@ void RasterizerCanvasGLES2::canvas_begin() {
state.uniforms.final_modulate = Color(1, 1, 1, 1);

state.uniforms.modelview_matrix = Transform2D();
state.uniforms.world_matrix = Transform2D();
state.uniforms.inv_world_matrix = Transform2D();
state.uniforms.extra_matrix = Transform2D();

_set_uniforms();
Expand Down Expand Up @@ -1579,6 +1583,8 @@ void RasterizerCanvasGLES2::canvas_render_items(Item *p_item_list, int p_z, cons
state.uniforms.final_modulate = unshaded ? ci->final_modulate : Color(ci->final_modulate.r * p_modulate.r, ci->final_modulate.g * p_modulate.g, ci->final_modulate.b * p_modulate.b, ci->final_modulate.a * p_modulate.a);

state.uniforms.modelview_matrix = ci->final_transform;
state.uniforms.world_matrix = p_base_transform.affine_inverse() * ci->final_transform;
state.uniforms.inv_world_matrix = state.uniforms.world_matrix.affine_inverse();
state.uniforms.extra_matrix = Transform2D();

_set_uniforms();
Expand Down
2 changes: 2 additions & 0 deletions drivers/gles2/rasterizer_canvas_gles2.h
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,8 @@ class RasterizerCanvasGLES2 : public RasterizerCanvas {
Transform projection_matrix;

Transform2D modelview_matrix;
Transform2D world_matrix;
Transform2D inv_world_matrix;
Transform2D extra_matrix;

Color final_modulate;
Expand Down
3 changes: 3 additions & 0 deletions drivers/gles2/shader_compiler_gles2.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -884,6 +884,8 @@ ShaderCompilerGLES2::ShaderCompilerGLES2() {
actions[VS::SHADER_CANVAS_ITEM].renames["POINT_SIZE"] = "gl_PointSize";

actions[VS::SHADER_CANVAS_ITEM].renames["WORLD_MATRIX"] = "modelview_matrix";
actions[VS::SHADER_CANVAS_ITEM].renames["GLOBAL_MATRIX"] = "world_matrix";
actions[VS::SHADER_CANVAS_ITEM].renames["INV_GLOBAL_MATRIX"] = "inv_world_matrix";
actions[VS::SHADER_CANVAS_ITEM].renames["PROJECTION_MATRIX"] = "projection_matrix";
actions[VS::SHADER_CANVAS_ITEM].renames["EXTRA_MATRIX"] = "extra_matrix";
actions[VS::SHADER_CANVAS_ITEM].renames["TIME"] = "time";
Expand Down Expand Up @@ -918,6 +920,7 @@ ShaderCompilerGLES2::ShaderCompilerGLES2() {
actions[VS::SHADER_CANVAS_ITEM].usage_defines["NORMALMAP"] = "#define NORMALMAP_USED\n";
actions[VS::SHADER_CANVAS_ITEM].usage_defines["LIGHT"] = "#define USE_LIGHT_SHADER_CODE\n";
actions[VS::SHADER_CANVAS_ITEM].render_mode_defines["skip_vertex_transform"] = "#define SKIP_TRANSFORM_USED\n";
actions[VS::SHADER_CANVAS_ITEM].render_mode_defines["world_vertex_coords"] = "#define VERTEX_WORLD_COORDS_USED\n";

/** SPATIAL SHADER **/

Expand Down
19 changes: 17 additions & 2 deletions drivers/gles2/shaders/canvas.glsl
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@ uniform highp mat4 projection_matrix;
#include "stdlib.glsl"

uniform highp mat4 modelview_matrix;
uniform highp mat4 world_matrix;
uniform highp mat4 inv_world_matrix;
uniform highp mat4 extra_matrix;
attribute highp vec2 vertex; // attrib:0
attribute vec4 color_attrib; // attrib:3
Expand Down Expand Up @@ -149,6 +151,12 @@ void main() {
uv = uv_attrib;
#endif

#if !defined(SKIP_TRANSFORM_USED) && defined(VERTEX_WORLD_COORDS_USED)
outvec = world_matrix * extra_matrix_instance * outvec;
#endif

#define extra_matrix extra_matrix_instance

{
vec2 src_vtx = outvec.xy;
/* clang-format off */
Expand All @@ -158,11 +166,18 @@ VERTEX_SHADER_CODE
/* clang-format on */
}

#if !defined(SKIP_TRANSFORM_USED)
outvec = extra_matrix_instance * outvec;
#if !defined(SKIP_TRANSFORM_USED) && defined(VERTEX_WORLD_COORDS_USED)
outvec = inv_world_matrix * outvec;
outvec = modelview_matrix * outvec;
#endif

#if !defined(SKIP_TRANSFORM_USED) && !defined(VERTEX_WORLD_COORDS_USED)
outvec = extra_matrix * outvec;
outvec = modelview_matrix * outvec;
#endif

#undef extra_matrix

color_interp = color;

#ifdef USE_PIXEL_SNAP
Expand Down
12 changes: 12 additions & 0 deletions drivers/gles3/rasterizer_canvas_gles3.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -176,6 +176,8 @@ void RasterizerCanvasGLES3::canvas_begin() {
state.canvas_shader.bind();
state.canvas_shader.set_uniform(CanvasShaderGLES3::FINAL_MODULATE, Color(1, 1, 1, 1));
state.canvas_shader.set_uniform(CanvasShaderGLES3::MODELVIEW_MATRIX, Transform2D());
state.canvas_shader.set_uniform(CanvasShaderGLES3::WORLD_MATRIX, Transform2D());
state.canvas_shader.set_uniform(CanvasShaderGLES3::INV_WORLD_MATRIX, Transform2D());
state.canvas_shader.set_uniform(CanvasShaderGLES3::EXTRA_MATRIX, Transform2D());
if (storage->frame.current_rt) {
state.canvas_shader.set_uniform(CanvasShaderGLES3::SCREEN_PIXEL_SIZE, Vector2(1.0 / storage->frame.current_rt->width, 1.0 / storage->frame.current_rt->height));
Expand Down Expand Up @@ -305,6 +307,8 @@ void RasterizerCanvasGLES3::_set_texture_rect_mode(bool p_enable, bool p_ninepat
state.canvas_shader.bind();
state.canvas_shader.set_uniform(CanvasShaderGLES3::FINAL_MODULATE, state.canvas_item_modulate);
state.canvas_shader.set_uniform(CanvasShaderGLES3::MODELVIEW_MATRIX, state.final_transform);
state.canvas_shader.set_uniform(CanvasShaderGLES3::WORLD_MATRIX, state.world_transform);
state.canvas_shader.set_uniform(CanvasShaderGLES3::INV_WORLD_MATRIX, state.inv_world_transform);
state.canvas_shader.set_uniform(CanvasShaderGLES3::EXTRA_MATRIX, state.extra_matrix);
if (state.using_skeleton) {
state.canvas_shader.set_uniform(CanvasShaderGLES3::SKELETON_TRANSFORM, state.skeleton_transform);
Expand Down Expand Up @@ -1008,6 +1012,8 @@ void RasterizerCanvasGLES3::_canvas_item_render_commands(Item *p_item, Item *cur
inv_xf.affine_invert();

state.canvas_shader.set_uniform(CanvasShaderGLES3::MODELVIEW_MATRIX, state.final_transform * inv_xf);
state.canvas_shader.set_uniform(CanvasShaderGLES3::WORLD_MATRIX, state.world_transform * inv_xf);
state.canvas_shader.set_uniform(CanvasShaderGLES3::INV_WORLD_MATRIX, state.inv_world_transform * inv_xf);
}

glBindVertexArray(data.particle_quad_array); //use particle quad array
Expand Down Expand Up @@ -1509,10 +1515,14 @@ void RasterizerCanvasGLES3::canvas_render_items(Item *p_item_list, int p_z, cons
state.canvas_item_modulate = unshaded ? ci->final_modulate : Color(ci->final_modulate.r * p_modulate.r, ci->final_modulate.g * p_modulate.g, ci->final_modulate.b * p_modulate.b, ci->final_modulate.a * p_modulate.a);

state.final_transform = ci->final_transform;
state.world_transform = p_transform.affine_inverse() * ci->final_transform;
state.inv_world_transform = state.world_transform.affine_inverse();
state.extra_matrix = Transform2D();

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::WORLD_MATRIX, state.world_transform);
state.canvas_shader.set_uniform(CanvasShaderGLES3::INV_WORLD_MATRIX, state.inv_world_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));
Expand Down Expand Up @@ -1584,6 +1594,8 @@ void RasterizerCanvasGLES3::canvas_render_items(Item *p_item_list, int p_z, cons

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::WORLD_MATRIX, state.world_transform);
state.canvas_shader.set_uniform(CanvasShaderGLES3::INV_WORLD_MATRIX, state.inv_world_transform);
state.canvas_shader.set_uniform(CanvasShaderGLES3::EXTRA_MATRIX, Transform2D());
}

Expand Down
2 changes: 2 additions & 0 deletions drivers/gles3/rasterizer_canvas_gles3.h
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,8 @@ class RasterizerCanvasGLES3 : public RasterizerCanvas {
Color canvas_item_modulate;
Transform2D extra_matrix;
Transform2D final_transform;
Transform2D world_transform;
Transform2D inv_world_transform;
bool using_skeleton;
Transform2D skeleton_transform;
Transform2D skeleton_transform_inverse;
Expand Down
3 changes: 3 additions & 0 deletions drivers/gles3/shader_compiler_gles3.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -885,6 +885,8 @@ ShaderCompilerGLES3::ShaderCompilerGLES3() {
actions[VS::SHADER_CANVAS_ITEM].renames["POINT_SIZE"] = "gl_PointSize";

actions[VS::SHADER_CANVAS_ITEM].renames["WORLD_MATRIX"] = "modelview_matrix";
actions[VS::SHADER_CANVAS_ITEM].renames["GLOBAL_MATRIX"] = "world_matrix";
actions[VS::SHADER_CANVAS_ITEM].renames["INV_GLOBAL_MATRIX"] = "inv_world_matrix";
actions[VS::SHADER_CANVAS_ITEM].renames["PROJECTION_MATRIX"] = "projection_matrix";
actions[VS::SHADER_CANVAS_ITEM].renames["EXTRA_MATRIX"] = "extra_matrix";
actions[VS::SHADER_CANVAS_ITEM].renames["TIME"] = "time";
Expand Down Expand Up @@ -919,6 +921,7 @@ ShaderCompilerGLES3::ShaderCompilerGLES3() {
actions[VS::SHADER_CANVAS_ITEM].usage_defines["NORMALMAP"] = "#define NORMALMAP_USED\n";
actions[VS::SHADER_CANVAS_ITEM].usage_defines["LIGHT"] = "#define USE_LIGHT_SHADER_CODE\n";
actions[VS::SHADER_CANVAS_ITEM].render_mode_defines["skip_vertex_transform"] = "#define SKIP_TRANSFORM_USED\n";
actions[VS::SHADER_CANVAS_ITEM].render_mode_defines["world_vertex_coords"] = "#define VERTEX_WORLD_COORDS_USED\n";

/** SPATIAL SHADER **/

Expand Down
13 changes: 12 additions & 1 deletion drivers/gles3/shaders/canvas.glsl
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,8 @@ layout(std140) uniform CanvasItemData { //ubo:0

uniform highp mat4 modelview_matrix;
uniform highp mat4 extra_matrix;
uniform highp mat4 world_matrix;
uniform highp mat4 inv_world_matrix;

out highp vec2 uv_interp;
out mediump vec4 color_interp;
Expand Down Expand Up @@ -148,6 +150,10 @@ void main() {
outvec.xy /= color_texpixel_size;
#endif

#if !defined(SKIP_TRANSFORM_USED) && defined(VERTEX_WORLD_COORDS_USED)
outvec = world_matrix * extra_matrix_instance * outvec;
#endif

#define extra_matrix extra_matrix_instance

//for compatibility with the fragment shader we need to use uv here
Expand All @@ -167,7 +173,12 @@ VERTEX_SHADER_CODE
pixel_size_interp = abs(dst_rect.zw) * vertex;
#endif

#if !defined(SKIP_TRANSFORM_USED)
#if !defined(SKIP_TRANSFORM_USED) && defined(VERTEX_WORLD_COORDS_USED)
outvec = inv_world_matrix * outvec;
outvec = modelview_matrix * outvec;
#endif

#if !defined(SKIP_TRANSFORM_USED) && !defined(VERTEX_WORLD_COORDS_USED)
outvec = extra_matrix * outvec;
outvec = modelview_matrix * outvec;
#endif
Expand Down
3 changes: 3 additions & 0 deletions servers/visual/shader_types.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -202,6 +202,8 @@ ShaderTypes::ShaderTypes() {
shader_modes[VS::SHADER_CANVAS_ITEM].functions["vertex"].built_ins["POINT_SIZE"] = ShaderLanguage::TYPE_FLOAT;

shader_modes[VS::SHADER_CANVAS_ITEM].functions["vertex"].built_ins["WORLD_MATRIX"] = constt(ShaderLanguage::TYPE_MAT4);
shader_modes[VS::SHADER_CANVAS_ITEM].functions["vertex"].built_ins["GLOBAL_MATRIX"] = constt(ShaderLanguage::TYPE_MAT4);
shader_modes[VS::SHADER_CANVAS_ITEM].functions["vertex"].built_ins["INV_GLOBAL_MATRIX"] = constt(ShaderLanguage::TYPE_MAT4);
shader_modes[VS::SHADER_CANVAS_ITEM].functions["vertex"].built_ins["PROJECTION_MATRIX"] = constt(ShaderLanguage::TYPE_MAT4);
shader_modes[VS::SHADER_CANVAS_ITEM].functions["vertex"].built_ins["EXTRA_MATRIX"] = constt(ShaderLanguage::TYPE_MAT4);
shader_modes[VS::SHADER_CANVAS_ITEM].functions["vertex"].built_ins["TIME"] = constt(ShaderLanguage::TYPE_FLOAT);
Expand Down Expand Up @@ -245,6 +247,7 @@ ShaderTypes::ShaderTypes() {
shader_modes[VS::SHADER_CANVAS_ITEM].functions["light"].can_discard = true;

shader_modes[VS::SHADER_CANVAS_ITEM].modes.push_back("skip_vertex_transform");
shader_modes[VS::SHADER_CANVAS_ITEM].modes.push_back("world_vertex_coords");

shader_modes[VS::SHADER_CANVAS_ITEM].modes.push_back("blend_mix");
shader_modes[VS::SHADER_CANVAS_ITEM].modes.push_back("blend_add");
Expand Down