Skip to content

Commit e60b184

Browse files
committedDec 2, 2024
Merge pull request #99704 from Chaosus/shader_pp_fix_orphan_strings
Fix orphan strings in shader preprocessor
2 parents 0c4ad02 + 5a48e52 commit e60b184

File tree

4 files changed

+26
-28
lines changed

4 files changed

+26
-28
lines changed
 

‎servers/rendering/shader_language.cpp

+5-5
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@
3939

4040
#define HAS_WARNING(flag) (warning_flags & flag)
4141

42-
int ShaderLanguage::instance_counter = 0;
42+
SafeNumeric<int> ShaderLanguage::instance_counter;
4343

4444
String ShaderLanguage::get_operator_text(Operator p_op) {
4545
static const char *op_names[OP_MAX] = { "==",
@@ -11597,7 +11597,7 @@ ShaderLanguage::ShaderLanguage() {
1159711597
nodes = nullptr;
1159811598
completion_class = TAG_GLOBAL;
1159911599

11600-
if (instance_counter == 0) {
11600+
if (instance_counter.get() == 0) {
1160111601
int idx = 0;
1160211602
while (builtin_func_defs[idx].name) {
1160311603
if (builtin_func_defs[idx].tag == SubClassTag::TAG_GLOBAL) {
@@ -11606,7 +11606,7 @@ ShaderLanguage::ShaderLanguage() {
1160611606
idx++;
1160711607
}
1160811608
}
11609-
instance_counter++;
11609+
instance_counter.increment();
1161011610

1161111611
#ifdef DEBUG_ENABLED
1161211612
warnings_check_map.insert(ShaderWarning::UNUSED_CONSTANT, &used_constants);
@@ -11621,8 +11621,8 @@ ShaderLanguage::ShaderLanguage() {
1162111621

1162211622
ShaderLanguage::~ShaderLanguage() {
1162311623
clear();
11624-
instance_counter--;
11625-
if (instance_counter == 0) {
11624+
instance_counter.decrement();
11625+
if (instance_counter.get() == 0) {
1162611626
global_func_set.clear();
1162711627
}
1162811628
}

‎servers/rendering/shader_language.h

+2-1
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@
3636
#include "core/string/ustring.h"
3737
#include "core/templates/list.h"
3838
#include "core/templates/rb_map.h"
39+
#include "core/templates/safe_refcount.h"
3940
#include "core/typedefs.h"
4041
#include "core/variant/variant.h"
4142
#include "scene/resources/shader_include.h"
@@ -833,7 +834,7 @@ class ShaderLanguage {
833834
static bool is_control_flow_keyword(String p_keyword);
834835
static void get_builtin_funcs(List<String> *r_keywords);
835836

836-
static int instance_counter;
837+
static SafeNumeric<int> instance_counter;
837838

838839
struct BuiltInInfo {
839840
DataType type = TYPE_VOID;

‎servers/rendering/shader_preprocessor.cpp

+18-22
Original file line numberDiff line numberDiff line change
@@ -1236,6 +1236,13 @@ ShaderPreprocessor::Define *ShaderPreprocessor::create_define(const String &p_bo
12361236
return define;
12371237
}
12381238

1239+
void ShaderPreprocessor::insert_builtin_define(String p_name, String p_value, State &p_state) {
1240+
Define *define = memnew(Define);
1241+
define->is_builtin = true;
1242+
define->body = p_value;
1243+
p_state.defines[p_name] = define;
1244+
}
1245+
12391246
void ShaderPreprocessor::clear_state() {
12401247
if (state != nullptr) {
12411248
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
13321339

13331340
// Built-in defines.
13341341
{
1335-
static HashMap<StringName, String> defines;
1336-
1337-
if (defines.is_empty()) {
1338-
const String rendering_method = OS::get_singleton()->get_current_rendering_method();
1339-
1340-
if (rendering_method == "forward_plus") {
1341-
defines["CURRENT_RENDERER"] = _MKSTR(2);
1342-
} else if (rendering_method == "mobile") {
1343-
defines["CURRENT_RENDERER"] = _MKSTR(1);
1344-
} else { // gl_compatibility
1345-
defines["CURRENT_RENDERER"] = _MKSTR(0);
1346-
}
1347-
1348-
defines["RENDERER_COMPATIBILITY"] = _MKSTR(0);
1349-
defines["RENDERER_MOBILE"] = _MKSTR(1);
1350-
defines["RENDERER_FORWARD_PLUS"] = _MKSTR(2);
1342+
const String rendering_method = OS::get_singleton()->get_current_rendering_method();
1343+
1344+
if (rendering_method == "forward_plus") {
1345+
insert_builtin_define("CURRENT_RENDERER", _MKSTR(2), pp_state);
1346+
} else if (rendering_method == "mobile") {
1347+
insert_builtin_define("CURRENT_RENDERER", _MKSTR(1), pp_state);
1348+
} else { // gl_compatibility
1349+
insert_builtin_define("CURRENT_RENDERER", _MKSTR(0), pp_state);
13511350
}
13521351

1353-
for (const KeyValue<StringName, String> &E : defines) {
1354-
Define *define = memnew(Define);
1355-
define->is_builtin = true;
1356-
define->body = E.value;
1357-
pp_state.defines[E.key] = define;
1358-
}
1352+
insert_builtin_define("RENDERER_COMPATIBILITY", _MKSTR(0), pp_state);
1353+
insert_builtin_define("RENDERER_MOBILE", _MKSTR(1), pp_state);
1354+
insert_builtin_define("RENDERER_FORWARD_PLUS", _MKSTR(2), pp_state);
13591355
}
13601356

13611357
Error err = preprocess(&pp_state, p_code, r_result);

‎servers/rendering/shader_preprocessor.h

+1
Original file line numberDiff line numberDiff line change
@@ -215,6 +215,7 @@ class ShaderPreprocessor {
215215
void set_error(const String &p_error, int p_line);
216216

217217
static Define *create_define(const String &p_body);
218+
void insert_builtin_define(String p_name, String p_value, State &p_state);
218219

219220
void clear_state();
220221

0 commit comments

Comments
 (0)
Please sign in to comment.