Merge pull request #94324 from SomeRanDev/shader_hint_enum

GDShader: Add `hint_enum` for `uniform int`
This commit is contained in:
Rémi Verschelde 2024-08-20 10:01:57 +02:00
commit 7999b4bb8a
No known key found for this signature in database
GPG Key ID: C3336907360768E1
5 changed files with 157 additions and 8 deletions

View File

@ -15,6 +15,9 @@
<member name="default_value_enabled" type="bool" setter="set_default_value_enabled" getter="is_default_value_enabled" default="false"> <member name="default_value_enabled" type="bool" setter="set_default_value_enabled" getter="is_default_value_enabled" default="false">
If [code]true[/code], the node will have a custom default value. If [code]true[/code], the node will have a custom default value.
</member> </member>
<member name="enum_names" type="PackedStringArray" setter="set_enum_names" getter="get_enum_names" default="PackedStringArray()">
The names used for the enum select in the editor. [member hint] must be [constant HINT_ENUM] for this to take effect.
</member>
<member name="hint" type="int" setter="set_hint" getter="get_hint" enum="VisualShaderNodeIntParameter.Hint" default="0"> <member name="hint" type="int" setter="set_hint" getter="get_hint" enum="VisualShaderNodeIntParameter.Hint" default="0">
Range hint of this node. Use it to customize valid parameter range. Range hint of this node. Use it to customize valid parameter range.
</member> </member>
@ -38,7 +41,10 @@
<constant name="HINT_RANGE_STEP" value="2" enum="Hint"> <constant name="HINT_RANGE_STEP" value="2" enum="Hint">
The parameter's value must be within the specified range, with the given [member step] between values. The parameter's value must be within the specified range, with the given [member step] between values.
</constant> </constant>
<constant name="HINT_MAX" value="3" enum="Hint"> <constant name="HINT_ENUM" value="3" enum="Hint">
The parameter uses an enum to associate preset values to names in the editor.
</constant>
<constant name="HINT_MAX" value="4" enum="Hint">
Represents the size of the [enum Hint] enum. Represents the size of the [enum Hint] enum.
</constant> </constant>
</constants> </constants>

View File

@ -5368,6 +5368,20 @@ String VisualShaderNodeIntParameter::generate_global(Shader::Mode p_mode, Visual
code += _get_qual_str() + "uniform int " + get_parameter_name() + " : hint_range(" + itos(hint_range_min) + ", " + itos(hint_range_max) + ")"; code += _get_qual_str() + "uniform int " + get_parameter_name() + " : hint_range(" + itos(hint_range_min) + ", " + itos(hint_range_max) + ")";
} else if (hint == HINT_RANGE_STEP) { } else if (hint == HINT_RANGE_STEP) {
code += _get_qual_str() + "uniform int " + get_parameter_name() + " : hint_range(" + itos(hint_range_min) + ", " + itos(hint_range_max) + ", " + itos(hint_range_step) + ")"; code += _get_qual_str() + "uniform int " + get_parameter_name() + " : hint_range(" + itos(hint_range_min) + ", " + itos(hint_range_max) + ", " + itos(hint_range_step) + ")";
} else if (hint == HINT_ENUM) {
code += _get_qual_str() + "uniform int " + get_parameter_name() + " : hint_enum(";
bool first = true;
for (const String &_name : hint_enum_names) {
if (first) {
first = false;
} else {
code += ", ";
}
code += "\"" + _name.c_escape() + "\"";
}
code += ")";
} else { } else {
code += _get_qual_str() + "uniform int " + get_parameter_name(); code += _get_qual_str() + "uniform int " + get_parameter_name();
} }
@ -5439,6 +5453,18 @@ int VisualShaderNodeIntParameter::get_step() const {
return hint_range_step; return hint_range_step;
} }
void VisualShaderNodeIntParameter::set_enum_names(const PackedStringArray &p_names) {
if (hint_enum_names == p_names) {
return;
}
hint_enum_names = p_names;
emit_changed();
}
PackedStringArray VisualShaderNodeIntParameter::get_enum_names() const {
return hint_enum_names;
}
void VisualShaderNodeIntParameter::set_default_value_enabled(bool p_default_value_enabled) { void VisualShaderNodeIntParameter::set_default_value_enabled(bool p_default_value_enabled) {
if (default_value_enabled == p_default_value_enabled) { if (default_value_enabled == p_default_value_enabled) {
return; return;
@ -5476,22 +5502,27 @@ void VisualShaderNodeIntParameter::_bind_methods() {
ClassDB::bind_method(D_METHOD("set_step", "value"), &VisualShaderNodeIntParameter::set_step); ClassDB::bind_method(D_METHOD("set_step", "value"), &VisualShaderNodeIntParameter::set_step);
ClassDB::bind_method(D_METHOD("get_step"), &VisualShaderNodeIntParameter::get_step); ClassDB::bind_method(D_METHOD("get_step"), &VisualShaderNodeIntParameter::get_step);
ClassDB::bind_method(D_METHOD("set_enum_names", "names"), &VisualShaderNodeIntParameter::set_enum_names);
ClassDB::bind_method(D_METHOD("get_enum_names"), &VisualShaderNodeIntParameter::get_enum_names);
ClassDB::bind_method(D_METHOD("set_default_value_enabled", "enabled"), &VisualShaderNodeIntParameter::set_default_value_enabled); ClassDB::bind_method(D_METHOD("set_default_value_enabled", "enabled"), &VisualShaderNodeIntParameter::set_default_value_enabled);
ClassDB::bind_method(D_METHOD("is_default_value_enabled"), &VisualShaderNodeIntParameter::is_default_value_enabled); ClassDB::bind_method(D_METHOD("is_default_value_enabled"), &VisualShaderNodeIntParameter::is_default_value_enabled);
ClassDB::bind_method(D_METHOD("set_default_value", "value"), &VisualShaderNodeIntParameter::set_default_value); ClassDB::bind_method(D_METHOD("set_default_value", "value"), &VisualShaderNodeIntParameter::set_default_value);
ClassDB::bind_method(D_METHOD("get_default_value"), &VisualShaderNodeIntParameter::get_default_value); ClassDB::bind_method(D_METHOD("get_default_value"), &VisualShaderNodeIntParameter::get_default_value);
ADD_PROPERTY(PropertyInfo(Variant::INT, "hint", PROPERTY_HINT_ENUM, "None,Range,Range + Step"), "set_hint", "get_hint"); ADD_PROPERTY(PropertyInfo(Variant::INT, "hint", PROPERTY_HINT_ENUM, "None,Range,Range + Step,Enum"), "set_hint", "get_hint");
ADD_PROPERTY(PropertyInfo(Variant::INT, "min"), "set_min", "get_min"); ADD_PROPERTY(PropertyInfo(Variant::INT, "min"), "set_min", "get_min");
ADD_PROPERTY(PropertyInfo(Variant::INT, "max"), "set_max", "get_max"); ADD_PROPERTY(PropertyInfo(Variant::INT, "max"), "set_max", "get_max");
ADD_PROPERTY(PropertyInfo(Variant::INT, "step"), "set_step", "get_step"); ADD_PROPERTY(PropertyInfo(Variant::INT, "step"), "set_step", "get_step");
ADD_PROPERTY(PropertyInfo(Variant::PACKED_STRING_ARRAY, "enum_names"), "set_enum_names", "get_enum_names");
ADD_PROPERTY(PropertyInfo(Variant::BOOL, "default_value_enabled"), "set_default_value_enabled", "is_default_value_enabled"); ADD_PROPERTY(PropertyInfo(Variant::BOOL, "default_value_enabled"), "set_default_value_enabled", "is_default_value_enabled");
ADD_PROPERTY(PropertyInfo(Variant::INT, "default_value"), "set_default_value", "get_default_value"); ADD_PROPERTY(PropertyInfo(Variant::INT, "default_value"), "set_default_value", "get_default_value");
BIND_ENUM_CONSTANT(HINT_NONE); BIND_ENUM_CONSTANT(HINT_NONE);
BIND_ENUM_CONSTANT(HINT_RANGE); BIND_ENUM_CONSTANT(HINT_RANGE);
BIND_ENUM_CONSTANT(HINT_RANGE_STEP); BIND_ENUM_CONSTANT(HINT_RANGE_STEP);
BIND_ENUM_CONSTANT(HINT_ENUM);
BIND_ENUM_CONSTANT(HINT_MAX); BIND_ENUM_CONSTANT(HINT_MAX);
} }
@ -5513,6 +5544,9 @@ Vector<StringName> VisualShaderNodeIntParameter::get_editable_properties() const
if (hint == HINT_RANGE_STEP) { if (hint == HINT_RANGE_STEP) {
props.push_back("step"); props.push_back("step");
} }
if (hint == HINT_ENUM) {
props.push_back("enum_names");
}
props.push_back("default_value_enabled"); props.push_back("default_value_enabled");
if (default_value_enabled) { if (default_value_enabled) {
props.push_back("default_value"); props.push_back("default_value");

View File

@ -2115,6 +2115,7 @@ public:
HINT_NONE, HINT_NONE,
HINT_RANGE, HINT_RANGE,
HINT_RANGE_STEP, HINT_RANGE_STEP,
HINT_ENUM,
HINT_MAX, HINT_MAX,
}; };
@ -2123,6 +2124,7 @@ private:
int hint_range_min = 0; int hint_range_min = 0;
int hint_range_max = 100; int hint_range_max = 100;
int hint_range_step = 1; int hint_range_step = 1;
PackedStringArray hint_enum_names;
bool default_value_enabled = false; bool default_value_enabled = false;
int default_value = 0; int default_value = 0;
@ -2158,6 +2160,9 @@ public:
void set_step(int p_value); void set_step(int p_value);
int get_step() const; int get_step() const;
void set_enum_names(const PackedStringArray &p_names);
PackedStringArray get_enum_names() const;
void set_default_value_enabled(bool p_enabled); void set_default_value_enabled(bool p_enabled);
bool is_default_value_enabled() const; bool is_default_value_enabled() const;

View File

@ -96,6 +96,7 @@ const char *ShaderLanguage::token_names[TK_MAX] = {
"FLOAT_CONSTANT", "FLOAT_CONSTANT",
"INT_CONSTANT", "INT_CONSTANT",
"UINT_CONSTANT", "UINT_CONSTANT",
"STRING_CONSTANT",
"TYPE_VOID", "TYPE_VOID",
"TYPE_BOOL", "TYPE_BOOL",
"TYPE_BVEC2", "TYPE_BVEC2",
@ -212,6 +213,7 @@ const char *ShaderLanguage::token_names[TK_MAX] = {
"HINT_ANISOTROPY_TEXTURE", "HINT_ANISOTROPY_TEXTURE",
"HINT_SOURCE_COLOR", "HINT_SOURCE_COLOR",
"HINT_RANGE", "HINT_RANGE",
"HINT_ENUM",
"HINT_INSTANCE_INDEX", "HINT_INSTANCE_INDEX",
"HINT_SCREEN_TEXTURE", "HINT_SCREEN_TEXTURE",
"HINT_NORMAL_ROUGHNESS_TEXTURE", "HINT_NORMAL_ROUGHNESS_TEXTURE",
@ -365,6 +367,7 @@ const ShaderLanguage::KeyWord ShaderLanguage::keyword_list[] = {
{ TK_HINT_SOURCE_COLOR, "source_color", CF_UNSPECIFIED, {}, {} }, { TK_HINT_SOURCE_COLOR, "source_color", CF_UNSPECIFIED, {}, {} },
{ TK_HINT_RANGE, "hint_range", CF_UNSPECIFIED, {}, {} }, { TK_HINT_RANGE, "hint_range", CF_UNSPECIFIED, {}, {} },
{ TK_HINT_ENUM, "hint_enum", CF_UNSPECIFIED, {}, {} },
{ TK_HINT_INSTANCE_INDEX, "instance_index", CF_UNSPECIFIED, {}, {} }, { TK_HINT_INSTANCE_INDEX, "instance_index", CF_UNSPECIFIED, {}, {} },
// sampler hints // sampler hints
@ -512,7 +515,54 @@ ShaderLanguage::Token ShaderLanguage::_get_token() {
return _make_token(TK_OP_NOT); return _make_token(TK_OP_NOT);
} break; } break;
//case '"' //string - no strings in shader case '"': {
String _content = "";
bool _previous_backslash = false;
while (true) {
bool _ended = false;
char32_t c = GETCHAR(0);
if (c == 0) {
return _make_token(TK_ERROR, "EOF reached before string termination.");
}
switch (c) {
case '"': {
if (_previous_backslash) {
_content += '"';
_previous_backslash = false;
} else {
_ended = true;
}
break;
}
case '\\': {
if (_previous_backslash) {
_content += '\\';
}
_previous_backslash = !_previous_backslash;
break;
}
case '\n': {
return _make_token(TK_ERROR, "Unexpected end of string.");
}
default: {
if (!_previous_backslash) {
_content += c;
} else {
return _make_token(TK_ERROR, "Only \\\" and \\\\ escape characters supported.");
}
break;
}
}
char_idx++;
if (_ended) {
break;
}
}
return _make_token(TK_STRING_CONSTANT, _content);
} break;
//case '\'' //string - no strings in shader //case '\'' //string - no strings in shader
case '{': case '{':
return _make_token(TK_CURLY_BRACKET_OPEN); return _make_token(TK_CURLY_BRACKET_OPEN);
@ -1127,6 +1177,9 @@ String ShaderLanguage::get_uniform_hint_name(ShaderNode::Uniform::Hint p_hint) {
case ShaderNode::Uniform::HINT_RANGE: { case ShaderNode::Uniform::HINT_RANGE: {
result = "hint_range"; result = "hint_range";
} break; } break;
case ShaderNode::Uniform::HINT_ENUM: {
result = "hint_enum";
} break;
case ShaderNode::Uniform::HINT_SOURCE_COLOR: { case ShaderNode::Uniform::HINT_SOURCE_COLOR: {
result = "source_color"; result = "source_color";
} break; } break;
@ -4146,6 +4199,11 @@ PropertyInfo ShaderLanguage::uniform_to_property_info(const ShaderNode::Uniform
if (p_uniform.array_size > 0) { if (p_uniform.array_size > 0) {
pi.type = Variant::PACKED_INT32_ARRAY; pi.type = Variant::PACKED_INT32_ARRAY;
// TODO: Handle range and encoding for for unsigned values. // TODO: Handle range and encoding for for unsigned values.
} else if (p_uniform.hint == ShaderLanguage::ShaderNode::Uniform::HINT_ENUM) {
pi.type = Variant::INT;
pi.hint = PROPERTY_HINT_ENUM;
String hint_string;
pi.hint_string = String(",").join(p_uniform.hint_enum_names);
} else { } else {
pi.type = Variant::INT; pi.type = Variant::INT;
pi.hint = PROPERTY_HINT_RANGE; pi.hint = PROPERTY_HINT_RANGE;
@ -8988,6 +9046,40 @@ Error ShaderLanguage::_parse_shader(const HashMap<StringName, FunctionInfo> &p_f
new_hint = ShaderNode::Uniform::HINT_RANGE; new_hint = ShaderNode::Uniform::HINT_RANGE;
} break; } break;
case TK_HINT_ENUM: {
if (type != TYPE_INT) {
_set_error(vformat(RTR("Enum hint is for '%s' only."), "int"));
return ERR_PARSE_ERROR;
}
tk = _get_token();
if (tk.type != TK_PARENTHESIS_OPEN) {
_set_expected_after_error("(", "hint_enum");
return ERR_PARSE_ERROR;
}
while (true) {
tk = _get_token();
if (tk.type != TK_STRING_CONSTANT) {
_set_error(RTR("Expected a string constant."));
return ERR_PARSE_ERROR;
}
uniform.hint_enum_names.push_back(tk.text);
tk = _get_token();
if (tk.type == TK_PARENTHESIS_CLOSE) {
break;
} else if (tk.type != TK_COMMA) {
_set_error(RTR("Expected ',' or ')' after string constant."));
return ERR_PARSE_ERROR;
}
}
new_hint = ShaderNode::Uniform::HINT_ENUM;
} break;
case TK_HINT_INSTANCE_INDEX: { case TK_HINT_INSTANCE_INDEX: {
if (custom_instance_index != -1) { if (custom_instance_index != -1) {
_set_error(vformat(RTR("Can only specify '%s' once."), "instance_index")); _set_error(vformat(RTR("Can only specify '%s' once."), "instance_index"));
@ -9082,7 +9174,9 @@ Error ShaderLanguage::_parse_shader(const HashMap<StringName, FunctionInfo> &p_f
default: default:
break; break;
} }
if (((new_filter != FILTER_DEFAULT || new_repeat != REPEAT_DEFAULT) || (new_hint != ShaderNode::Uniform::HINT_NONE && new_hint != ShaderNode::Uniform::HINT_SOURCE_COLOR && new_hint != ShaderNode::Uniform::HINT_RANGE)) && !is_sampler_type(type)) {
bool is_sampler_hint = new_hint != ShaderNode::Uniform::HINT_NONE && new_hint != ShaderNode::Uniform::HINT_SOURCE_COLOR && new_hint != ShaderNode::Uniform::HINT_RANGE && new_hint != ShaderNode::Uniform::HINT_ENUM;
if (((new_filter != FILTER_DEFAULT || new_repeat != REPEAT_DEFAULT) || is_sampler_hint) && !is_sampler_type(type)) {
_set_error(RTR("This hint is only for sampler types.")); _set_error(RTR("This hint is only for sampler types."));
return ERR_PARSE_ERROR; return ERR_PARSE_ERROR;
} }
@ -10785,15 +10879,21 @@ Error ShaderLanguage::complete(const String &p_code, const ShaderCompileInfo &p_
} }
} else if ((completion_base == DataType::TYPE_INT || completion_base == DataType::TYPE_FLOAT) && !completion_base_array) { } else if ((completion_base == DataType::TYPE_INT || completion_base == DataType::TYPE_FLOAT) && !completion_base_array) {
if (current_uniform_hint == ShaderNode::Uniform::HINT_NONE) { if (current_uniform_hint == ShaderNode::Uniform::HINT_NONE) {
ScriptLanguage::CodeCompletionOption option("hint_range", ScriptLanguage::CODE_COMPLETION_KIND_PLAIN_TEXT); Vector<String> options;
if (completion_base == DataType::TYPE_INT) { if (completion_base == DataType::TYPE_INT) {
option.insert_text = "hint_range(0, 100, 1)"; options.push_back("hint_range(0, 100, 1)");
options.push_back("hint_enum(\"Zero\", \"One\", \"Two\")");
} else { } else {
option.insert_text = "hint_range(0.0, 1.0, 0.1)"; options.push_back("hint_range(0.0, 1.0, 0.1)");
} }
r_options->push_back(option); for (const String &option_text : options) {
String hint_name = option_text.substr(0, option_text.find_char(char32_t('(')));
ScriptLanguage::CodeCompletionOption option(hint_name, ScriptLanguage::CODE_COMPLETION_KIND_PLAIN_TEXT);
option.insert_text = option_text;
r_options->push_back(option);
}
} }
} else if ((int(completion_base) > int(TYPE_MAT4) && int(completion_base) < int(TYPE_STRUCT))) { } else if ((int(completion_base) > int(TYPE_MAT4) && int(completion_base) < int(TYPE_STRUCT))) {
Vector<String> options; Vector<String> options;

View File

@ -59,6 +59,7 @@ public:
TK_FLOAT_CONSTANT, TK_FLOAT_CONSTANT,
TK_INT_CONSTANT, TK_INT_CONSTANT,
TK_UINT_CONSTANT, TK_UINT_CONSTANT,
TK_STRING_CONSTANT,
TK_TYPE_VOID, TK_TYPE_VOID,
TK_TYPE_BOOL, TK_TYPE_BOOL,
TK_TYPE_BVEC2, TK_TYPE_BVEC2,
@ -175,6 +176,7 @@ public:
TK_HINT_ANISOTROPY_TEXTURE, TK_HINT_ANISOTROPY_TEXTURE,
TK_HINT_SOURCE_COLOR, TK_HINT_SOURCE_COLOR,
TK_HINT_RANGE, TK_HINT_RANGE,
TK_HINT_ENUM,
TK_HINT_INSTANCE_INDEX, TK_HINT_INSTANCE_INDEX,
TK_HINT_SCREEN_TEXTURE, TK_HINT_SCREEN_TEXTURE,
TK_HINT_NORMAL_ROUGHNESS_TEXTURE, TK_HINT_NORMAL_ROUGHNESS_TEXTURE,
@ -623,6 +625,7 @@ public:
enum Hint { enum Hint {
HINT_NONE, HINT_NONE,
HINT_RANGE, HINT_RANGE,
HINT_ENUM,
HINT_SOURCE_COLOR, HINT_SOURCE_COLOR,
HINT_NORMAL, HINT_NORMAL,
HINT_ROUGHNESS_NORMAL, HINT_ROUGHNESS_NORMAL,
@ -661,6 +664,7 @@ public:
TextureFilter filter = FILTER_DEFAULT; TextureFilter filter = FILTER_DEFAULT;
TextureRepeat repeat = REPEAT_DEFAULT; TextureRepeat repeat = REPEAT_DEFAULT;
float hint_range[3]; float hint_range[3];
PackedStringArray hint_enum_names;
int instance_index = 0; int instance_index = 0;
String group; String group;
String subgroup; String subgroup;