From e021076b4c75b221fd496906aeaea3d2d9cf07d5 Mon Sep 17 00:00:00 2001 From: Chaosus Date: Sat, 26 Oct 2024 12:31:01 +0300 Subject: [PATCH] Add renderer state defines to shader preprocessor --- servers/rendering/shader_preprocessor.cpp | 34 +++++++++++++++++++++++ servers/rendering/shader_preprocessor.h | 1 + 2 files changed, 35 insertions(+) diff --git a/servers/rendering/shader_preprocessor.cpp b/servers/rendering/shader_preprocessor.cpp index 0e41a178b56..88ea74cdfcd 100644 --- a/servers/rendering/shader_preprocessor.cpp +++ b/servers/rendering/shader_preprocessor.cpp @@ -816,6 +816,11 @@ void ShaderPreprocessor::process_undef(Tokenizer *p_tokenizer) { } if (state->defines.has(label)) { + if (state->defines[label]->is_builtin) { + set_error(vformat(RTR("Cannot use '%s' on built-in define."), "undef"), line); + return; + } + memdelete(state->defines[label]); state->defines.erase(label); } @@ -1324,6 +1329,35 @@ Error ShaderPreprocessor::preprocess(const String &p_code, const String &p_filen pp_state.current_filename = p_filename; pp_state.save_regions = r_regions != nullptr; } + + // Built-in defines. + { + static HashMap 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); + } + + for (const KeyValue &E : defines) { + Define *define = memnew(Define); + define->is_builtin = true; + define->body = E.value; + pp_state.defines[E.key] = define; + } + } + Error err = preprocess(&pp_state, p_code, r_result); if (err != OK) { if (r_error_text) { diff --git a/servers/rendering/shader_preprocessor.h b/servers/rendering/shader_preprocessor.h index 0a90aec9587..b3d9594bcbb 100644 --- a/servers/rendering/shader_preprocessor.h +++ b/servers/rendering/shader_preprocessor.h @@ -132,6 +132,7 @@ private: struct Define { Vector arguments; String body; + bool is_builtin = false; }; struct Branch {