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

Fix orphan strings in shader preprocessor #99704

Merged
merged 1 commit into from
Dec 2, 2024
Merged
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
10 changes: 5 additions & 5 deletions servers/rendering/shader_language.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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] = { "==",
Expand Down Expand Up @@ -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) {
Expand All @@ -11606,7 +11606,7 @@ ShaderLanguage::ShaderLanguage() {
idx++;
}
}
instance_counter++;
instance_counter.increment();

#ifdef DEBUG_ENABLED
warnings_check_map.insert(ShaderWarning::UNUSED_CONSTANT, &used_constants);
Expand All @@ -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();
}
}
3 changes: 2 additions & 1 deletion servers/rendering/shader_language.h
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -833,7 +834,7 @@ class ShaderLanguage {
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;
Expand Down
40 changes: 18 additions & 22 deletions servers/rendering/shader_preprocessor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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()) {
Expand Down Expand Up @@ -1332,30 +1339,19 @@ Error ShaderPreprocessor::preprocess(const String &p_code, const String &p_filen

// Built-in defines.
{
static HashMap<StringName, String> defines;

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);
const String rendering_method = OS::get_singleton()->get_current_rendering_method();

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);
Expand Down
1 change: 1 addition & 0 deletions servers/rendering/shader_preprocessor.h
Original file line number Diff line number Diff line change
Expand Up @@ -215,6 +215,7 @@ class ShaderPreprocessor {
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();

Expand Down