Merge pull request #99704 from Chaosus/shader_pp_fix_orphan_strings

Fix orphan strings in shader preprocessor
This commit is contained in:
Rémi Verschelde 2024-12-02 17:20:39 +01:00
commit e60b18493d
No known key found for this signature in database
GPG Key ID: C3336907360768E1
4 changed files with 25 additions and 27 deletions

View File

@ -39,7 +39,7 @@
#define HAS_WARNING(flag) (warning_flags & flag)
int ShaderLanguage::instance_counter = 0;
SafeNumeric<int> ShaderLanguage::instance_counter;
String ShaderLanguage::get_operator_text(Operator p_op) {
static const char *op_names[OP_MAX] = { "==",
@ -11597,7 +11597,7 @@ ShaderLanguage::ShaderLanguage() {
nodes = nullptr;
completion_class = TAG_GLOBAL;
if (instance_counter == 0) {
if (instance_counter.get() == 0) {
int idx = 0;
while (builtin_func_defs[idx].name) {
if (builtin_func_defs[idx].tag == SubClassTag::TAG_GLOBAL) {
@ -11606,7 +11606,7 @@ ShaderLanguage::ShaderLanguage() {
idx++;
}
}
instance_counter++;
instance_counter.increment();
#ifdef DEBUG_ENABLED
warnings_check_map.insert(ShaderWarning::UNUSED_CONSTANT, &used_constants);
@ -11621,8 +11621,8 @@ ShaderLanguage::ShaderLanguage() {
ShaderLanguage::~ShaderLanguage() {
clear();
instance_counter--;
if (instance_counter == 0) {
instance_counter.decrement();
if (instance_counter.get() == 0) {
global_func_set.clear();
}
}

View File

@ -36,6 +36,7 @@
#include "core/string/ustring.h"
#include "core/templates/list.h"
#include "core/templates/rb_map.h"
#include "core/templates/safe_refcount.h"
#include "core/typedefs.h"
#include "core/variant/variant.h"
#include "scene/resources/shader_include.h"
@ -833,7 +834,7 @@ public:
static bool is_control_flow_keyword(String p_keyword);
static void get_builtin_funcs(List<String> *r_keywords);
static int instance_counter;
static SafeNumeric<int> instance_counter;
struct BuiltInInfo {
DataType type = TYPE_VOID;

View File

@ -1236,6 +1236,13 @@ ShaderPreprocessor::Define *ShaderPreprocessor::create_define(const String &p_bo
return define;
}
void ShaderPreprocessor::insert_builtin_define(String p_name, String p_value, State &p_state) {
Define *define = memnew(Define);
define->is_builtin = true;
define->body = p_value;
p_state.defines[p_name] = define;
}
void ShaderPreprocessor::clear_state() {
if (state != nullptr) {
for (const RBMap<String, Define *>::Element *E = state->defines.front(); E; E = E->next()) {
@ -1332,30 +1339,19 @@ Error ShaderPreprocessor::preprocess(const String &p_code, const String &p_filen
// Built-in defines.
{
static HashMap<StringName, String> defines;
const String rendering_method = OS::get_singleton()->get_current_rendering_method();
if (defines.is_empty()) {
const String rendering_method = OS::get_singleton()->get_current_rendering_method();
if (rendering_method == "forward_plus") {
defines["CURRENT_RENDERER"] = _MKSTR(2);
} else if (rendering_method == "mobile") {
defines["CURRENT_RENDERER"] = _MKSTR(1);
} else { // gl_compatibility
defines["CURRENT_RENDERER"] = _MKSTR(0);
}
defines["RENDERER_COMPATIBILITY"] = _MKSTR(0);
defines["RENDERER_MOBILE"] = _MKSTR(1);
defines["RENDERER_FORWARD_PLUS"] = _MKSTR(2);
if (rendering_method == "forward_plus") {
insert_builtin_define("CURRENT_RENDERER", _MKSTR(2), pp_state);
} else if (rendering_method == "mobile") {
insert_builtin_define("CURRENT_RENDERER", _MKSTR(1), pp_state);
} else { // gl_compatibility
insert_builtin_define("CURRENT_RENDERER", _MKSTR(0), pp_state);
}
for (const KeyValue<StringName, String> &E : defines) {
Define *define = memnew(Define);
define->is_builtin = true;
define->body = E.value;
pp_state.defines[E.key] = define;
}
insert_builtin_define("RENDERER_COMPATIBILITY", _MKSTR(0), pp_state);
insert_builtin_define("RENDERER_MOBILE", _MKSTR(1), pp_state);
insert_builtin_define("RENDERER_FORWARD_PLUS", _MKSTR(2), pp_state);
}
Error err = preprocess(&pp_state, p_code, r_result);

View File

@ -215,6 +215,7 @@ private:
void set_error(const String &p_error, int p_line);
static Define *create_define(const String &p_body);
void insert_builtin_define(String p_name, String p_value, State &p_state);
void clear_state();