From bb83c4adec61d51d72c429c0f4172dc4c6479343 Mon Sep 17 00:00:00 2001 From: Giwayume Date: Wed, 27 Dec 2023 15:58:50 -0500 Subject: [PATCH] Add custom shader attributes to Canvas Item Shaders --- drivers/gles3/rasterizer_canvas_gles3.cpp | 26 ++++++---- drivers/gles3/rasterizer_canvas_gles3.h | 1 + drivers/gles3/shaders/canvas.glsl | 49 ++++++++++++++----- drivers/gles3/storage/material_storage.cpp | 13 +++++ drivers/gles3/storage/material_storage.h | 4 ++ .../plugins/visual_shader_editor_plugin.cpp | 2 + scene/resources/visual_shader.cpp | 2 + .../renderer_rd/renderer_canvas_render_rd.cpp | 5 ++ .../rendering/renderer_rd/shaders/canvas.glsl | 27 +++++++++- servers/rendering/shader_types.cpp | 2 + 10 files changed, 106 insertions(+), 25 deletions(-) diff --git a/drivers/gles3/rasterizer_canvas_gles3.cpp b/drivers/gles3/rasterizer_canvas_gles3.cpp index 5d6cef6e056..8bcec8dd2f5 100644 --- a/drivers/gles3/rasterizer_canvas_gles3.cpp +++ b/drivers/gles3/rasterizer_canvas_gles3.cpp @@ -627,6 +627,11 @@ void RasterizerCanvasGLES3::_render_items(RID p_to_render_target, int p_item_cou state.canvas_instance_batches[state.current_batch_index].material = material; state.canvas_instance_batches[state.current_batch_index].material_data = material_data; + if (shader_data_cache) { + state.canvas_instance_batches[state.current_batch_index].vertex_input_mask = shader_data_cache->vertex_input_mask; + } else { + state.canvas_instance_batches[state.current_batch_index].vertex_input_mask = RS::ARRAY_FORMAT_VERTEX | RS::ARRAY_COLOR | RS::ARRAY_TEX_UV; + } } GLES3::CanvasShaderData::BlendMode blend_mode = shader_data_cache ? shader_data_cache->blend_mode : GLES3::CanvasShaderData::BLEND_MODE_MIX; @@ -1412,11 +1417,12 @@ void RasterizerCanvasGLES3::_render_batch(Light *p_lights, uint32_t p_index, Ren GLuint vertex_array_gl = 0; GLuint index_array_gl = 0; - uint64_t input_mask = RS::ARRAY_FORMAT_VERTEX | RS::ARRAY_FORMAT_COLOR | RS::ARRAY_FORMAT_TEX_UV; // 2D meshes always use the same vertex format. + uint64_t vertex_input_mask = state.canvas_instance_batches[p_index].vertex_input_mask; + if (mesh_instance.is_valid()) { - mesh_storage->mesh_instance_surface_get_vertex_arrays_and_format(mesh_instance, j, input_mask, vertex_array_gl); + mesh_storage->mesh_instance_surface_get_vertex_arrays_and_format(mesh_instance, j, vertex_input_mask, vertex_array_gl); } else { - mesh_storage->mesh_surface_get_vertex_arrays_and_format(surface, input_mask, vertex_array_gl); + mesh_storage->mesh_surface_get_vertex_arrays_and_format(surface, vertex_input_mask, vertex_array_gl); } index_array_gl = mesh_storage->mesh_surface_get_index_buffer(surface, 0); @@ -1472,9 +1478,9 @@ void RasterizerCanvasGLES3::_render_batch(Light *p_lights, uint32_t p_index, Ren glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0); if (use_instancing) { glDisableVertexAttribArray(5); - glDisableVertexAttribArray(6); - glDisableVertexAttribArray(7); glDisableVertexAttribArray(8); + glDisableVertexAttribArray(9); + glDisableVertexAttribArray(10); } if (r_render_info) { // Meshes, Particles, and MultiMesh are always just one object with one draw call. @@ -1539,15 +1545,15 @@ void RasterizerCanvasGLES3::_new_batch(bool &r_batch_broken) { } void RasterizerCanvasGLES3::_enable_attributes(uint32_t p_start, bool p_primitive, uint32_t p_rate) { - uint32_t split = p_primitive ? 11 : 12; - for (uint32_t i = 6; i < split; i++) { + uint32_t split = p_primitive ? 13 : 14; + for (uint32_t i = 8; i < split; i++) { glEnableVertexAttribArray(i); - glVertexAttribPointer(i, 4, GL_FLOAT, GL_FALSE, sizeof(InstanceData), CAST_INT_TO_UCHAR_PTR(p_start + (i - 6) * 4 * sizeof(float))); + glVertexAttribPointer(i, 4, GL_FLOAT, GL_FALSE, sizeof(InstanceData), CAST_INT_TO_UCHAR_PTR(p_start + (i - 8) * 4 * sizeof(float))); glVertexAttribDivisor(i, p_rate); } - for (uint32_t i = split; i <= 13; i++) { + for (uint32_t i = split; i <= 15; i++) { glEnableVertexAttribArray(i); - glVertexAttribIPointer(i, 4, GL_UNSIGNED_INT, sizeof(InstanceData), CAST_INT_TO_UCHAR_PTR(p_start + (i - 6) * 4 * sizeof(float))); + glVertexAttribIPointer(i, 4, GL_UNSIGNED_INT, sizeof(InstanceData), CAST_INT_TO_UCHAR_PTR(p_start + (i - 8) * 4 * sizeof(float))); glVertexAttribDivisor(i, p_rate); } } diff --git a/drivers/gles3/rasterizer_canvas_gles3.h b/drivers/gles3/rasterizer_canvas_gles3.h index 552b2afd6bd..2b70df32383 100644 --- a/drivers/gles3/rasterizer_canvas_gles3.h +++ b/drivers/gles3/rasterizer_canvas_gles3.h @@ -269,6 +269,7 @@ public: RID material; GLES3::CanvasMaterialData *material_data = nullptr; CanvasShaderGLES3::ShaderVariant shader_variant = CanvasShaderGLES3::MODE_QUAD; + uint64_t vertex_input_mask; const Item::Command *command = nullptr; Item::Command::Type command_type = Item::Command::TYPE_ANIMATION_SLICE; // Can default to any type that doesn't form a batch. diff --git a/drivers/gles3/shaders/canvas.glsl b/drivers/gles3/shaders/canvas.glsl index ce8fe25625b..5cc81b8cec5 100644 --- a/drivers/gles3/shaders/canvas.glsl +++ b/drivers/gles3/shaders/canvas.glsl @@ -32,18 +32,26 @@ layout(location = 5) in highp uvec4 instance_color_custom_data; // Color packed #include "stdlib_inc.glsl" -layout(location = 6) in highp vec4 attrib_A; -layout(location = 7) in highp vec4 attrib_B; -layout(location = 8) in highp vec4 attrib_C; -layout(location = 9) in highp vec4 attrib_D; -layout(location = 10) in highp vec4 attrib_E; -#ifdef USE_PRIMITIVE -layout(location = 11) in highp uvec4 attrib_F; -#else -layout(location = 11) in highp vec4 attrib_F; +#if defined(CUSTOM0_USED) +layout(location = 6) in highp vec4 custom0_attrib; #endif -layout(location = 12) in highp uvec4 attrib_G; -layout(location = 13) in highp uvec4 attrib_H; + +#if defined(CUSTOM1_USED) +layout(location = 7) in highp vec4 custom1_attrib; +#endif + +layout(location = 8) in highp vec4 attrib_A; +layout(location = 9) in highp vec4 attrib_B; +layout(location = 10) in highp vec4 attrib_C; +layout(location = 11) in highp vec4 attrib_D; +layout(location = 12) in highp vec4 attrib_E; +#ifdef USE_PRIMITIVE +layout(location = 13) in highp uvec4 attrib_F; +#else +layout(location = 13) in highp vec4 attrib_F; +#endif +layout(location = 14) in highp uvec4 attrib_G; +layout(location = 15) in highp uvec4 attrib_H; #define read_draw_data_world_x attrib_A.xy #define read_draw_data_world_y attrib_A.zw @@ -137,6 +145,13 @@ void main() { vec4 instance_custom = vec4(0.0); +#if defined(CUSTOM0_USED) + vec4 custom0 = vec4(0.0); +#endif +#if defined(CUSTOM1_USED) + vec4 custom1 = vec4(0.0); +#endif + #ifdef USE_PRIMITIVE vec2 vertex; vec2 uv; @@ -169,9 +184,9 @@ void main() { if (bool(read_draw_data_flags & FLAGS_INSTANCING_HAS_CUSTOM_DATA)) { instance_custom = vec4(unpackHalf2x16(instance_color_custom_data.z), unpackHalf2x16(instance_color_custom_data.w)); } -#endif +#endif // !USE_INSTANCING -#else +#else // !USE_ATTRIBUTES vec2 vertex_base_arr[6] = vec2[](vec2(0.0, 0.0), vec2(0.0, 1.0), vec2(1.0, 1.0), vec2(1.0, 0.0), vec2(0.0, 0.0), vec2(1.0, 1.0)); vec2 vertex_base = vertex_base_arr[gl_VertexID % 6]; @@ -179,6 +194,14 @@ void main() { vec4 color = read_draw_data_modulation; vec2 vertex = read_draw_data_dst_rect.xy + abs(read_draw_data_dst_rect.zw) * mix(vertex_base, vec2(1.0, 1.0) - vertex_base, lessThan(read_draw_data_src_rect.zw, vec2(0.0, 0.0))); +#endif // USE_ATTRIBUTES + +#if defined(CUSTOM0_USED) + custom0 = custom0_attrib; +#endif + +#if defined(CUSTOM1_USED) + custom1 = custom1_attrib; #endif mat4 model_matrix = mat4(vec4(read_draw_data_world_x, 0.0, 0.0), vec4(read_draw_data_world_y, 0.0, 0.0), vec4(0.0, 0.0, 1.0, 0.0), vec4(read_draw_data_world_ofs, 0.0, 1.0)); diff --git a/drivers/gles3/storage/material_storage.cpp b/drivers/gles3/storage/material_storage.cpp index 13ab05c0a08..04d1460c32f 100644 --- a/drivers/gles3/storage/material_storage.cpp +++ b/drivers/gles3/storage/material_storage.cpp @@ -1160,6 +1160,9 @@ MaterialStorage::MaterialStorage() { actions.renames["INSTANCE_ID"] = "gl_InstanceID"; actions.renames["VERTEX_ID"] = "gl_VertexID"; + actions.renames["CUSTOM0"] = "custom0"; + actions.renames["CUSTOM1"] = "custom1"; + actions.renames["LIGHT_POSITION"] = "light_position"; actions.renames["LIGHT_DIRECTION"] = "light_direction"; actions.renames["LIGHT_IS_DIRECTIONAL"] = "is_directional"; @@ -1180,6 +1183,8 @@ MaterialStorage::MaterialStorage() { actions.usage_defines["NORMAL_MAP"] = "#define NORMAL_MAP_USED\n"; actions.usage_defines["LIGHT"] = "#define LIGHT_SHADER_CODE_USED\n"; actions.usage_defines["SPECULAR_SHININESS"] = "#define SPECULAR_SHININESS_USED\n"; + actions.usage_defines["CUSTOM0"] = "#define CUSTOM0_USED\n"; + actions.usage_defines["CUSTOM1"] = "#define CUSTOM1_USED\n"; actions.render_mode_defines["skip_vertex_transform"] = "#define SKIP_TRANSFORM_USED\n"; actions.render_mode_defines["unshaded"] = "#define MODE_UNSHADED\n"; @@ -2541,6 +2546,8 @@ void CanvasShaderData::set_code(const String &p_code) { uses_screen_texture_mipmaps = false; uses_sdf = false; uses_time = false; + uses_custom0 = false; + uses_custom1 = false; if (code.is_empty()) { return; // Just invalid, but no error. @@ -2565,6 +2572,8 @@ void CanvasShaderData::set_code(const String &p_code) { actions.usage_flag_pointers["texture_sdf"] = &uses_sdf; actions.usage_flag_pointers["TIME"] = &uses_time; + actions.usage_flag_pointers["CUSTOM0"] = &uses_custom0; + actions.usage_flag_pointers["CUSTOM1"] = &uses_custom1; actions.uniforms = &uniforms; Error err = MaterialStorage::get_singleton()->shaders.compiler_canvas.compile(RS::SHADER_CANVAS_ITEM, code, &actions, path, gen_code); @@ -2601,6 +2610,10 @@ void CanvasShaderData::set_code(const String &p_code) { MaterialStorage::get_singleton()->shaders.canvas_shader.version_set_code(version, gen_code.code, gen_code.uniforms, gen_code.stage_globals[ShaderCompiler::STAGE_VERTEX], gen_code.stage_globals[ShaderCompiler::STAGE_FRAGMENT], gen_code.defines, texture_uniform_data); ERR_FAIL_COND(!MaterialStorage::get_singleton()->shaders.canvas_shader.version_is_valid(version)); + vertex_input_mask = RS::ARRAY_FORMAT_VERTEX | RS::ARRAY_COLOR | RS::ARRAY_TEX_UV; + vertex_input_mask |= uses_custom0 << RS::ARRAY_CUSTOM0; + vertex_input_mask |= uses_custom1 << RS::ARRAY_CUSTOM1; + ubo_size = gen_code.uniform_total_size; ubo_offsets = gen_code.uniform_offsets; texture_uniforms = gen_code.texture_uniforms; diff --git a/drivers/gles3/storage/material_storage.h b/drivers/gles3/storage/material_storage.h index 3c5080c6e1b..59f5682362d 100644 --- a/drivers/gles3/storage/material_storage.h +++ b/drivers/gles3/storage/material_storage.h @@ -168,6 +168,10 @@ struct CanvasShaderData : public ShaderData { bool uses_screen_texture_mipmaps; bool uses_sdf; bool uses_time; + bool uses_custom0; + bool uses_custom1; + + uint64_t vertex_input_mask; virtual void set_code(const String &p_Code); virtual bool is_animated() const; diff --git a/editor/plugins/visual_shader_editor_plugin.cpp b/editor/plugins/visual_shader_editor_plugin.cpp index 5c22e454ab4..653982ca08a 100644 --- a/editor/plugins/visual_shader_editor_plugin.cpp +++ b/editor/plugins/visual_shader_editor_plugin.cpp @@ -5786,6 +5786,8 @@ VisualShaderEditor::VisualShaderEditor() { add_options.push_back(AddOption("AtLightPass", "Input/Vertex", "VisualShaderNodeInput", vformat(input_param_for_vertex_and_fragment_shader_modes, "at_light_pass", "AT_LIGHT_PASS"), { "at_light_pass" }, VisualShaderNode::PORT_TYPE_BOOLEAN, TYPE_FLAGS_VERTEX, Shader::MODE_CANVAS_ITEM)); add_options.push_back(AddOption("CanvasMatrix", "Input/Vertex", "VisualShaderNodeInput", vformat(input_param_for_vertex_shader_mode, "canvas_matrix", "CANVAS_MATRIX"), { "canvas_matrix" }, VisualShaderNode::PORT_TYPE_TRANSFORM, TYPE_FLAGS_VERTEX, Shader::MODE_CANVAS_ITEM)); + add_options.push_back(AddOption("Custom0", "Input/Vertex", "VisualShaderNodeInput", vformat(input_param_for_vertex_shader_mode, "custom0", "CUSTOM0"), { "custom0" }, VisualShaderNode::PORT_TYPE_VECTOR_4D, TYPE_FLAGS_VERTEX, Shader::MODE_CANVAS_ITEM)); + add_options.push_back(AddOption("Custom1", "Input/Vertex", "VisualShaderNodeInput", vformat(input_param_for_vertex_shader_mode, "custom1", "CUSTOM1"), { "custom1" }, VisualShaderNode::PORT_TYPE_VECTOR_4D, TYPE_FLAGS_VERTEX, Shader::MODE_CANVAS_ITEM)); add_options.push_back(AddOption("InstanceCustom", "Input/Vertex", "VisualShaderNodeInput", vformat(input_param_for_vertex_shader_mode, "instance_custom", "INSTANCE_CUSTOM"), { "instance_custom" }, VisualShaderNode::PORT_TYPE_VECTOR_4D, TYPE_FLAGS_VERTEX, Shader::MODE_CANVAS_ITEM)); add_options.push_back(AddOption("InstanceId", "Input/Vertex", "VisualShaderNodeInput", vformat(input_param_for_vertex_shader_mode, "instance_id", "INSTANCE_ID"), { "instance_id" }, VisualShaderNode::PORT_TYPE_SCALAR_INT, TYPE_FLAGS_VERTEX, Shader::MODE_CANVAS_ITEM)); add_options.push_back(AddOption("ModelMatrix", "Input/Vertex", "VisualShaderNodeInput", vformat(input_param_for_vertex_shader_mode, "model_matrix", "MODEL_MATRIX"), { "model_matrix" }, VisualShaderNode::PORT_TYPE_TRANSFORM, TYPE_FLAGS_VERTEX, Shader::MODE_CANVAS_ITEM)); diff --git a/scene/resources/visual_shader.cpp b/scene/resources/visual_shader.cpp index 9fb6b364a7b..118b24d803d 100644 --- a/scene/resources/visual_shader.cpp +++ b/scene/resources/visual_shader.cpp @@ -2952,6 +2952,8 @@ const VisualShaderNodeInput::Port VisualShaderNodeInput::ports[] = { { Shader::MODE_CANVAS_ITEM, VisualShader::TYPE_VERTEX, VisualShaderNode::PORT_TYPE_TRANSFORM, "screen_matrix", "SCREEN_MATRIX" }, { Shader::MODE_CANVAS_ITEM, VisualShader::TYPE_VERTEX, VisualShaderNode::PORT_TYPE_SCALAR, "time", "TIME" }, { Shader::MODE_CANVAS_ITEM, VisualShader::TYPE_VERTEX, VisualShaderNode::PORT_TYPE_BOOLEAN, "at_light_pass", "AT_LIGHT_PASS" }, + { Shader::MODE_CANVAS_ITEM, VisualShader::TYPE_VERTEX, VisualShaderNode::PORT_TYPE_VECTOR_4D, "custom0", "CUSTOM0" }, + { Shader::MODE_CANVAS_ITEM, VisualShader::TYPE_VERTEX, VisualShaderNode::PORT_TYPE_VECTOR_4D, "custom1", "CUSTOM1" }, { Shader::MODE_CANVAS_ITEM, VisualShader::TYPE_VERTEX, VisualShaderNode::PORT_TYPE_VECTOR_4D, "instance_custom", "INSTANCE_CUSTOM" }, { Shader::MODE_CANVAS_ITEM, VisualShader::TYPE_VERTEX, VisualShaderNode::PORT_TYPE_SCALAR_INT, "instance_id", "INSTANCE_ID" }, { Shader::MODE_CANVAS_ITEM, VisualShader::TYPE_VERTEX, VisualShaderNode::PORT_TYPE_SCALAR_INT, "vertex_id", "VERTEX_ID" }, diff --git a/servers/rendering/renderer_rd/renderer_canvas_render_rd.cpp b/servers/rendering/renderer_rd/renderer_canvas_render_rd.cpp index 657628111ae..d57ba1ccce9 100644 --- a/servers/rendering/renderer_rd/renderer_canvas_render_rd.cpp +++ b/servers/rendering/renderer_rd/renderer_canvas_render_rd.cpp @@ -2562,6 +2562,9 @@ RendererCanvasRenderRD::RendererCanvasRenderRD() { actions.renames["INSTANCE_ID"] = "gl_InstanceIndex"; actions.renames["VERTEX_ID"] = "gl_VertexIndex"; + actions.renames["CUSTOM0"] = "custom0"; + actions.renames["CUSTOM1"] = "custom1"; + actions.renames["LIGHT_POSITION"] = "light_position"; actions.renames["LIGHT_DIRECTION"] = "light_direction"; actions.renames["LIGHT_IS_DIRECTIONAL"] = "is_directional"; @@ -2583,6 +2586,8 @@ RendererCanvasRenderRD::RendererCanvasRenderRD() { actions.usage_defines["LIGHT"] = "#define LIGHT_SHADER_CODE_USED\n"; actions.usage_defines["SPECULAR_SHININESS"] = "#define SPECULAR_SHININESS_USED\n"; actions.usage_defines["POINT_SIZE"] = "#define USE_POINT_SIZE\n"; + actions.usage_defines["CUSTOM0"] = "#define CUSTOM0_USED\n"; + actions.usage_defines["CUSTOM1"] = "#define CUSTOM1_USED\n"; actions.render_mode_defines["skip_vertex_transform"] = "#define SKIP_TRANSFORM_USED\n"; actions.render_mode_defines["unshaded"] = "#define MODE_UNSHADED\n"; diff --git a/servers/rendering/renderer_rd/shaders/canvas.glsl b/servers/rendering/renderer_rd/shaders/canvas.glsl index 7a13ac72079..ee9a67f5950 100644 --- a/servers/rendering/renderer_rd/shaders/canvas.glsl +++ b/servers/rendering/renderer_rd/shaders/canvas.glsl @@ -9,6 +9,14 @@ layout(location = 0) in vec2 vertex_attrib; layout(location = 3) in vec4 color_attrib; layout(location = 4) in vec2 uv_attrib; +#if defined(CUSTOM0_USED) +layout(location = 6) in vec4 custom0_attrib; +#endif + +#if defined(CUSTOM1_USED) +layout(location = 7) in vec4 custom1_attrib; +#endif + layout(location = 10) in uvec4 bone_attrib; layout(location = 11) in vec4 weight_attrib; @@ -44,6 +52,13 @@ vec3 srgb_to_linear(vec3 color) { void main() { vec4 instance_custom = vec4(0.0); +#if defined(CUSTOM0_USED) + vec4 custom0 = vec4(0.0); +#endif +#if defined(CUSTOM1_USED) + vec4 custom1 = vec4(0.0); +#endif + #ifdef USE_PRIMITIVE //weird bug, @@ -78,9 +93,17 @@ void main() { color *= draw_data.modulation; vec2 uv = uv_attrib; +#if defined(CUSTOM0_USED) + custom0 = custom0_attrib; +#endif + +#if defined(CUSTOM1_USED) + custom1 = custom1_attrib; +#endif + uvec4 bones = bone_attrib; vec4 bone_weights = weight_attrib; -#else +#else // !USE_ATTRIBUTES vec2 vertex_base_arr[4] = vec2[](vec2(0.0, 0.0), vec2(0.0, 1.0), vec2(1.0, 1.0), vec2(1.0, 0.0)); vec2 vertex_base = vertex_base_arr[gl_VertexIndex]; @@ -90,7 +113,7 @@ void main() { vec2 vertex = draw_data.dst_rect.xy + abs(draw_data.dst_rect.zw) * mix(vertex_base, vec2(1.0, 1.0) - vertex_base, lessThan(draw_data.src_rect.zw, vec2(0.0, 0.0))); uvec4 bones = uvec4(0, 0, 0, 0); -#endif +#endif // USE_ATTRIBUTES mat4 model_matrix = mat4(vec4(draw_data.world_x, 0.0, 0.0), vec4(draw_data.world_y, 0.0, 0.0), vec4(0.0, 0.0, 1.0, 0.0), vec4(draw_data.world_ofs, 0.0, 1.0)); diff --git a/servers/rendering/shader_types.cpp b/servers/rendering/shader_types.cpp index 3e19e8f01a6..1272c59a2df 100644 --- a/servers/rendering/shader_types.cpp +++ b/servers/rendering/shader_types.cpp @@ -250,6 +250,8 @@ ShaderTypes::ShaderTypes() { shader_modes[RS::SHADER_CANVAS_ITEM].functions["vertex"].built_ins["VERTEX_ID"] = constt(ShaderLanguage::TYPE_INT); shader_modes[RS::SHADER_CANVAS_ITEM].functions["vertex"].built_ins["AT_LIGHT_PASS"] = constt(ShaderLanguage::TYPE_BOOL); shader_modes[RS::SHADER_CANVAS_ITEM].functions["vertex"].built_ins["TEXTURE_PIXEL_SIZE"] = constt(ShaderLanguage::TYPE_VEC2); + shader_modes[RS::SHADER_CANVAS_ITEM].functions["vertex"].built_ins["CUSTOM0"] = constt(ShaderLanguage::TYPE_VEC4); + shader_modes[RS::SHADER_CANVAS_ITEM].functions["vertex"].built_ins["CUSTOM1"] = constt(ShaderLanguage::TYPE_VEC4); shader_modes[RS::SHADER_CANVAS_ITEM].functions["vertex"].can_discard = false; shader_modes[RS::SHADER_CANVAS_ITEM].functions["vertex"].main_function = true;