From 73793ab89cb5805509e0eef3136058cc9181677c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bart=C5=82omiej=20T=2E=20Listwon?= Date: Wed, 3 Jan 2024 12:03:56 +0100 Subject: [PATCH] Prevent shuffling custom shader functions (shader cache requires determinism) --- drivers/gles2/shader_compiler_gles2.cpp | 13 +++++++------ drivers/gles3/shader_compiler_gles3.cpp | 13 +++++++------ servers/visual/shader_language.cpp | 2 +- servers/visual/shader_language.h | 2 +- 4 files changed, 16 insertions(+), 14 deletions(-) diff --git a/drivers/gles2/shader_compiler_gles2.cpp b/drivers/gles2/shader_compiler_gles2.cpp index a56fe87b768..9948a35765b 100644 --- a/drivers/gles2/shader_compiler_gles2.cpp +++ b/drivers/gles2/shader_compiler_gles2.cpp @@ -229,17 +229,18 @@ void ShaderCompilerGLES2::_dump_function_deps(const SL::ShaderNode *p_node, cons ERR_FAIL_COND(fidx == -1); - for (Set::Element *E = p_node->functions[fidx].uses_function.front(); E; E = E->next()) { - if (r_added.has(E->get())) { + for (int ufidx = 0; ufidx < p_node->functions[fidx].uses_function.size(); ufidx++) { + StringName function_name = p_node->functions[fidx].uses_function[ufidx]; + if (r_added.has(function_name)) { continue; } - _dump_function_deps(p_node, E->get(), p_func_code, r_to_add, r_added); + _dump_function_deps(p_node, function_name, p_func_code, r_to_add, r_added); SL::FunctionNode *fnode = nullptr; for (int i = 0; i < p_node->functions.size(); i++) { - if (p_node->functions[i].name == E->get()) { + if (p_node->functions[i].name == function_name) { fnode = p_node->functions[i].function; break; } @@ -272,9 +273,9 @@ void ShaderCompilerGLES2::_dump_function_deps(const SL::ShaderNode *p_node, cons header += ")\n"; r_to_add += header.as_string(); - r_to_add += p_func_code[E->get()]; + r_to_add += p_func_code[function_name]; - r_added.insert(E->get()); + r_added.insert(function_name); } } diff --git a/drivers/gles3/shader_compiler_gles3.cpp b/drivers/gles3/shader_compiler_gles3.cpp index 443da3c62d3..746c2e91402 100644 --- a/drivers/gles3/shader_compiler_gles3.cpp +++ b/drivers/gles3/shader_compiler_gles3.cpp @@ -354,17 +354,18 @@ void ShaderCompilerGLES3::_dump_function_deps(const SL::ShaderNode *p_node, cons ERR_FAIL_COND(fidx == -1); - for (Set::Element *E = p_node->functions[fidx].uses_function.front(); E; E = E->next()) { - if (added.has(E->get())) { + for (int ufidx = 0; ufidx < p_node->functions[fidx].uses_function.size(); ufidx++) { + StringName function_name = p_node->functions[fidx].uses_function[ufidx]; + if (added.has(function_name)) { continue; //was added already } - _dump_function_deps(p_node, E->get(), p_func_code, r_to_add, added); + _dump_function_deps(p_node, function_name, p_func_code, r_to_add, added); SL::FunctionNode *fnode = nullptr; for (int i = 0; i < p_node->functions.size(); i++) { - if (p_node->functions[i].name == E->get()) { + if (p_node->functions[i].name == function_name) { fnode = p_node->functions[i].function; break; } @@ -396,9 +397,9 @@ void ShaderCompilerGLES3::_dump_function_deps(const SL::ShaderNode *p_node, cons header += ")\n"; r_to_add += header; - r_to_add += p_func_code[E->get()]; + r_to_add += p_func_code[function_name]; - added.insert(E->get()); + added.insert(function_name); } } diff --git a/servers/visual/shader_language.cpp b/servers/visual/shader_language.cpp index df03fdd94d0..cb7c11475d2 100644 --- a/servers/visual/shader_language.cpp +++ b/servers/visual/shader_language.cpp @@ -3405,7 +3405,7 @@ ShaderLanguage::Node *ShaderLanguage::_parse_expression(BlockNode *p_block, cons //add to current function as dependency for (int j = 0; j < shader->functions.size(); j++) { if (shader->functions[j].name == current_function) { - shader->functions.write[j].uses_function.insert(name); + shader->functions.write[j].uses_function.push_back(name); break; } } diff --git a/servers/visual/shader_language.h b/servers/visual/shader_language.h index 24b9a2f8aea..6472db6f41f 100644 --- a/servers/visual/shader_language.h +++ b/servers/visual/shader_language.h @@ -602,7 +602,7 @@ public: struct Function { StringName name; FunctionNode *function; - Set uses_function; + Vector uses_function; bool callable; };