Skip to content

Commit

Permalink
Forbid using derivative functions outside the fragment/light in shader
Browse files Browse the repository at this point in the history
  • Loading branch information
Chaosus committed Jun 18, 2024
1 parent e2fc6d3 commit 33c2a76
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 1 deletion.
48 changes: 47 additions & 1 deletion servers/rendering/shader_language.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3085,6 +3085,19 @@ const ShaderLanguage::BuiltinFuncConstArgs ShaderLanguage::builtin_func_const_ar
{ nullptr, 0, 0, 0 }
};

const ShaderLanguage::BuiltinFuncFragLight ShaderLanguage::builtin_func_fragment_light_only[] = {
{ "dFdx" },
{ "dFdxCoarse" },
{ "dFdxFine" },
{ "dFdy" },
{ "dFdyCoarse" },
{ "dFdyFine" },
{ "fwidth" },
{ "fwidthCoarse" },
{ "fwidthFine" },
{ nullptr }
};

bool ShaderLanguage::is_const_suffix_lut_initialized = false;

bool ShaderLanguage::_validate_function_call(BlockNode *p_block, const FunctionInfo &p_function_info, OperatorNode *p_func, DataType *r_ret_type, StringName *r_ret_type_str, bool *r_is_custom_function) {
Expand Down Expand Up @@ -3137,6 +3150,7 @@ bool ShaderLanguage::_validate_function_call(BlockNode *p_block, const FunctionI
if (argcount <= 4) {
// test builtins
int idx = 0;
bool is_non_frag_light = current_function != "fragment" && current_function != "light";

while (builtin_func_defs[idx].name) {
if (completion_class != builtin_func_defs[idx].tag) {
Expand Down Expand Up @@ -3178,6 +3192,16 @@ bool ShaderLanguage::_validate_function_call(BlockNode *p_block, const FunctionI
}

if (!fail) {
if (is_non_frag_light) {
int idx2 = 0;
while (builtin_func_fragment_light_only[idx2].name) {
if (name == builtin_func_fragment_light_only[arg_idx].name) {

Check failure on line 3198 in servers/rendering/shader_language.cpp

View workflow job for this annotation

GitHub Actions / 🐧 Linux / Editor w/ Mono (target=editor)

'arg_idx' was not declared in this scope

Check failure on line 3198 in servers/rendering/shader_language.cpp

View workflow job for this annotation

GitHub Actions / 🐧 Linux / Editor with doubles and GCC sanitizers (target=editor, tests=yes, dev_build=yes, scu_build=yes, precision=double, use_asan=yes, use_ubsan=yes, linker=gold)

'arg_idx' was not declared in this scope

Check failure on line 3198 in servers/rendering/shader_language.cpp

View workflow job for this annotation

GitHub Actions / 🐧 Linux / Editor with clang sanitizers (target=editor, tests=yes, dev_build=yes, use_asan=yes, use_ubsan=yes, use_llvm=yes, linker=lld)

use of undeclared identifier 'arg_idx'; did you mean 'char_idx'?

Check failure on line 3198 in servers/rendering/shader_language.cpp

View workflow job for this annotation

GitHub Actions / 🐧 Linux / Editor with ThreadSanitizer (target=editor, tests=yes, dev_build=yes, use_tsan=yes, use_llvm=yes, linker=lld)

use of undeclared identifier 'arg_idx'; did you mean 'char_idx'?

Check failure on line 3198 in servers/rendering/shader_language.cpp

View workflow job for this annotation

GitHub Actions / 🐧 Linux / Template w/ Mono (target=template_release)

'arg_idx' was not declared in this scope

Check failure on line 3198 in servers/rendering/shader_language.cpp

View workflow job for this annotation

GitHub Actions / 🏁 Windows / Editor (target=editor, tests=yes)

'arg_idx': undeclared identifier

Check failure on line 3198 in servers/rendering/shader_language.cpp

View workflow job for this annotation

GitHub Actions / 🐧 Linux / Minimal template (target=template_release, everything disabled)

'arg_idx' was not declared in this scope

Check failure on line 3198 in servers/rendering/shader_language.cpp

View workflow job for this annotation

GitHub Actions / 🏁 Windows / Template (target=template_release)

'arg_idx': undeclared identifier
_set_error(vformat(RTR("'%s' cannot be used outside of the '%s' or '%s' processor functions."), String(name), "fragment", "light"));
return false;
}
idx2++;
}
}
{
int constarg_idx = 0;
while (builtin_func_const_args[constarg_idx].name) {
Expand Down Expand Up @@ -10324,11 +10348,33 @@ Error ShaderLanguage::complete(const String &p_code, const ShaderCompileInfo &p_
}
}

bool is_non_frag_light = current_function != "fragment" && current_function != "light";

while (builtin_func_defs[idx].name) {
if (low_end && builtin_func_defs[idx].high_end) {
idx++;
continue;
}

if (is_non_frag_light) {
bool found = false;
{
int idx2 = 0;
while (builtin_func_fragment_light_only[idx2].name) {
if (String(builtin_func_fragment_light_only[idx2].name) == builtin_func_defs[idx].name) {
found = true;
break;
}
idx2++;
}
}

if (found) {
idx++;
continue;
}
}

matches.insert(String(builtin_func_defs[idx].name), ScriptLanguage::CODE_COMPLETION_KIND_FUNCTION);
idx++;
}
Expand Down Expand Up @@ -10498,7 +10544,7 @@ Error ShaderLanguage::complete(const String &p_code, const ShaderCompileInfo &p_
int idx2 = 0;
HashSet<int> out_args;
while (builtin_func_out_args[idx2].name != nullptr) {
if (builtin_func_out_args[idx2].name == builtin_func_defs[idx].name) {
if (String(builtin_func_out_args[idx2].name) == builtin_func_defs[idx].name) {
for (int i = 0; i < BuiltinFuncOutArgs::MAX_ARGS; i++) {
int arg = builtin_func_out_args[idx2].arguments[i];
if (arg == -1) {
Expand Down
5 changes: 5 additions & 0 deletions servers/rendering/shader_language.h
Original file line number Diff line number Diff line change
Expand Up @@ -1059,6 +1059,10 @@ class ShaderLanguage {
int max;
};

struct BuiltinFuncFragLight {
const char *name;
};

CompletionType completion_type;
ShaderNode::Uniform::Hint current_uniform_hint = ShaderNode::Uniform::HINT_NONE;
TextureFilter current_uniform_filter = FILTER_DEFAULT;
Expand All @@ -1083,6 +1087,7 @@ class ShaderLanguage {
static const BuiltinFuncDef builtin_func_defs[];
static const BuiltinFuncOutArgs builtin_func_out_args[];
static const BuiltinFuncConstArgs builtin_func_const_args[];
static const BuiltinFuncFragLight builtin_func_fragment_light_only[];

static bool is_const_suffix_lut_initialized;

Expand Down

0 comments on commit 33c2a76

Please sign in to comment.