Skip to content

Commit 9dfb36c

Browse files
committed
Merge pull request #98549 from Chaosus/shader_renderer_states
Add renderer state defines to shader preprocessor
2 parents 6bbc1cb + e021076 commit 9dfb36c

File tree

2 files changed

+35
-0
lines changed

2 files changed

+35
-0
lines changed

servers/rendering/shader_preprocessor.cpp

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -816,6 +816,11 @@ void ShaderPreprocessor::process_undef(Tokenizer *p_tokenizer) {
816816
}
817817

818818
if (state->defines.has(label)) {
819+
if (state->defines[label]->is_builtin) {
820+
set_error(vformat(RTR("Cannot use '%s' on built-in define."), "undef"), line);
821+
return;
822+
}
823+
819824
memdelete(state->defines[label]);
820825
state->defines.erase(label);
821826
}
@@ -1324,6 +1329,35 @@ Error ShaderPreprocessor::preprocess(const String &p_code, const String &p_filen
13241329
pp_state.current_filename = p_filename;
13251330
pp_state.save_regions = r_regions != nullptr;
13261331
}
1332+
1333+
// Built-in defines.
1334+
{
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);
1351+
}
1352+
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+
}
1359+
}
1360+
13271361
Error err = preprocess(&pp_state, p_code, r_result);
13281362
if (err != OK) {
13291363
if (r_error_text) {

servers/rendering/shader_preprocessor.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -132,6 +132,7 @@ class ShaderPreprocessor {
132132
struct Define {
133133
Vector<String> arguments;
134134
String body;
135+
bool is_builtin = false;
135136
};
136137

137138
struct Branch {

0 commit comments

Comments
 (0)