Merge pull request #94812 from clayjohn/SHADER-normal-roughness

Emit `normal_roughness` compatibility code in custom functions
This commit is contained in:
Rémi Verschelde 2024-07-28 17:46:45 +02:00
commit 8e47aafa08
No known key found for this signature in database
GPG Key ID: C3336907360768E1
3 changed files with 52 additions and 43 deletions

View File

@ -1286,6 +1286,13 @@ String ShaderCompiler::_dump_node_code(const SL::Node *p_node, int p_level, Gene
break;
}
if (function->arguments[j].tex_argument_check) {
if (function->arguments[j].tex_hint == ShaderLanguage::ShaderNode::Uniform::HINT_SCREEN_TEXTURE) {
is_screen_texture = true;
} else if (function->arguments[j].tex_hint == ShaderLanguage::ShaderNode::Uniform::HINT_DEPTH_TEXTURE) {
is_depth_texture = true;
} else if (function->arguments[j].tex_hint == ShaderLanguage::ShaderNode::Uniform::HINT_NORMAL_ROUGHNESS_TEXTURE) {
is_normal_roughness_texture = true;
}
sampler_name = _get_sampler_name(function->arguments[j].tex_argument_filter, function->arguments[j].tex_argument_repeat);
found = true;
break;

View File

@ -4761,7 +4761,7 @@ bool ShaderLanguage::_validate_assign(Node *p_node, const FunctionInfo &p_functi
return false;
}
bool ShaderLanguage::_propagate_function_call_sampler_uniform_settings(const StringName &p_name, int p_argument, TextureFilter p_filter, TextureRepeat p_repeat) {
bool ShaderLanguage::_propagate_function_call_sampler_uniform_settings(const StringName &p_name, int p_argument, TextureFilter p_filter, TextureRepeat p_repeat, ShaderNode::Uniform::Hint p_hint) {
for (int i = 0; i < shader->vfunctions.size(); i++) {
if (shader->vfunctions[i].name == p_name) {
ERR_FAIL_INDEX_V(p_argument, shader->vfunctions[i].function->arguments.size(), false);
@ -4770,20 +4770,21 @@ bool ShaderLanguage::_propagate_function_call_sampler_uniform_settings(const Str
_set_error(vformat(RTR("Sampler argument %d of function '%s' called more than once using both built-ins and uniform textures, this is not supported (use either one or the other)."), p_argument, String(p_name)));
return false;
} else if (arg->tex_argument_check) {
//was checked, verify that filter and repeat are the same
if (arg->tex_argument_filter == p_filter && arg->tex_argument_repeat == p_repeat) {
// Was checked, verify that filter, repeat, and hint are the same.
if (arg->tex_argument_filter == p_filter && arg->tex_argument_repeat == p_repeat && arg->tex_hint == p_hint) {
return true;
} else {
_set_error(vformat(RTR("Sampler argument %d of function '%s' called more than once using textures that differ in either filter or repeat setting."), p_argument, String(p_name)));
_set_error(vformat(RTR("Sampler argument %d of function '%s' called more than once using textures that differ in either filter, repeat, or texture hint setting."), p_argument, String(p_name)));
return false;
}
} else {
arg->tex_argument_check = true;
arg->tex_argument_filter = p_filter;
arg->tex_argument_repeat = p_repeat;
arg->tex_hint = p_hint;
for (KeyValue<StringName, HashSet<int>> &E : arg->tex_argument_connect) {
for (const int &F : E.value) {
if (!_propagate_function_call_sampler_uniform_settings(E.key, F, p_filter, p_repeat)) {
if (!_propagate_function_call_sampler_uniform_settings(E.key, F, p_filter, p_repeat, p_hint)) {
return false;
}
}
@ -5583,7 +5584,7 @@ ShaderLanguage::Node *ShaderLanguage::_parse_expression(BlockNode *p_block, cons
}
//propagate
if (!_propagate_function_call_sampler_uniform_settings(name, i, u->filter, u->repeat)) {
if (!_propagate_function_call_sampler_uniform_settings(name, i, u->filter, u->repeat, u->hint)) {
return nullptr;
}
} else if (p_function_info.built_ins.has(varname)) {

View File

@ -578,42 +578,6 @@ public:
Node(NODE_TYPE_STRUCT) {}
};
struct FunctionNode : public Node {
struct Argument {
ArgumentQualifier qualifier;
StringName name;
DataType type;
StringName struct_name;
DataPrecision precision;
//for passing textures as arguments
bool tex_argument_check;
TextureFilter tex_argument_filter;
TextureRepeat tex_argument_repeat;
bool tex_builtin_check;
StringName tex_builtin;
bool is_const;
int array_size;
HashMap<StringName, HashSet<int>> tex_argument_connect;
};
StringName name;
DataType return_type = TYPE_VOID;
StringName return_struct_name;
DataPrecision return_precision = PRECISION_DEFAULT;
int return_array_size = 0;
Vector<Argument> arguments;
BlockNode *body = nullptr;
bool can_discard = false;
virtual DataType get_datatype() const override { return return_type; }
virtual String get_datatype_name() const override { return String(return_struct_name); }
virtual int get_array_size() const override { return return_array_size; }
FunctionNode() :
Node(NODE_TYPE_FUNCTION) {}
};
struct ShaderNode : public Node {
struct Constant {
StringName name;
@ -722,6 +686,43 @@ public:
Node(NODE_TYPE_SHADER) {}
};
struct FunctionNode : public Node {
struct Argument {
ArgumentQualifier qualifier;
StringName name;
DataType type;
StringName struct_name;
DataPrecision precision;
//for passing textures as arguments
bool tex_argument_check;
TextureFilter tex_argument_filter;
TextureRepeat tex_argument_repeat;
bool tex_builtin_check;
StringName tex_builtin;
ShaderNode::Uniform::Hint tex_hint;
bool is_const;
int array_size;
HashMap<StringName, HashSet<int>> tex_argument_connect;
};
StringName name;
DataType return_type = TYPE_VOID;
StringName return_struct_name;
DataPrecision return_precision = PRECISION_DEFAULT;
int return_array_size = 0;
Vector<Argument> arguments;
BlockNode *body = nullptr;
bool can_discard = false;
virtual DataType get_datatype() const override { return return_type; }
virtual String get_datatype_name() const override { return String(return_struct_name); }
virtual int get_array_size() const override { return return_array_size; }
FunctionNode() :
Node(NODE_TYPE_FUNCTION) {}
};
struct UniformOrderComparator {
_FORCE_INLINE_ bool operator()(const Pair<StringName, int> &A, const Pair<StringName, int> &B) const {
return A.second < B.second;
@ -1122,7 +1123,7 @@ private:
bool _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 = nullptr);
bool _parse_function_arguments(BlockNode *p_block, const FunctionInfo &p_function_info, OperatorNode *p_func, int *r_complete_arg = nullptr);
bool _propagate_function_call_sampler_uniform_settings(const StringName &p_name, int p_argument, TextureFilter p_filter, TextureRepeat p_repeat);
bool _propagate_function_call_sampler_uniform_settings(const StringName &p_name, int p_argument, TextureFilter p_filter, TextureRepeat p_repeat, ShaderNode::Uniform::Hint p_hint);
bool _propagate_function_call_sampler_builtin_reference(const StringName &p_name, int p_argument, const StringName &p_builtin);
bool _validate_varying_assign(ShaderNode::Varying &p_varying, String *r_message);
bool _check_node_constness(const Node *p_node) const;