diff options
author | Yuri Roubinsky <chaosus89@gmail.com> | 2021-08-08 08:04:20 +0300 |
---|---|---|
committer | Yuri Roubinsky <chaosus89@gmail.com> | 2021-12-08 11:34:40 +0300 |
commit | 76f6c0849c35ba1a3993b91892a977a5504e6f28 (patch) | |
tree | fae61763bf084ab0f5cf1d357d144cf8a3402907 | |
parent | ff118d7a88f56bce1f35a0c700521b0c63cec32c (diff) |
Make `compile` shader function to use struct instead long parameter list
-rw-r--r-- | drivers/gles3/shader_compiler_gles3.cpp | 8 | ||||
-rw-r--r-- | editor/plugins/shader_editor_plugin.cpp | 16 | ||||
-rw-r--r-- | editor/plugins/visual_shader_editor_plugin.cpp | 8 | ||||
-rw-r--r-- | servers/rendering/renderer_rd/shader_compiler_rd.cpp | 8 | ||||
-rw-r--r-- | servers/rendering/shader_language.cpp | 34 | ||||
-rw-r--r-- | servers/rendering/shader_language.h | 13 | ||||
-rw-r--r-- | tests/servers/test_shader_lang.cpp | 7 |
7 files changed, 69 insertions, 25 deletions
diff --git a/drivers/gles3/shader_compiler_gles3.cpp b/drivers/gles3/shader_compiler_gles3.cpp index 168080f2f1..c76d80b0a1 100644 --- a/drivers/gles3/shader_compiler_gles3.cpp +++ b/drivers/gles3/shader_compiler_gles3.cpp @@ -861,7 +861,13 @@ ShaderLanguage::DataType ShaderCompilerGLES3::_get_variable_type(const StringNam Error ShaderCompilerGLES3::compile(RS::ShaderMode p_mode, const String &p_code, IdentifierActions *p_actions, const String &p_path, GeneratedCode &r_gen_code) { ShaderLanguage::VaryingFunctionNames var_names; - Error err = parser.compile(p_code, ShaderTypes::get_singleton()->get_functions(p_mode), ShaderTypes::get_singleton()->get_modes(p_mode), var_names, ShaderTypes::get_singleton()->get_types(), _get_variable_type); + ShaderLanguage::ShaderCompileInfo info; + info.functions = ShaderTypes::get_singleton()->get_functions(p_mode); + info.render_modes = ShaderTypes::get_singleton()->get_modes(p_mode); + info.shader_types = ShaderTypes::get_singleton()->get_types(); + info.global_variable_type_func = _get_variable_type; + + Error err = parser.compile(p_code, info); // Error ShaderLanguage::compile(const String &p_code, const Map<StringName, FunctionInfo> &p_functions, const Vector<StringName> &p_render_modes, const Set<String> &p_shader_types, GlobalVariableGetTypeFunc p_global_variable_type_func) { if (err != OK) { diff --git a/editor/plugins/shader_editor_plugin.cpp b/editor/plugins/shader_editor_plugin.cpp index 5d14590797..fa23ccf200 100644 --- a/editor/plugins/shader_editor_plugin.cpp +++ b/editor/plugins/shader_editor_plugin.cpp @@ -204,7 +204,13 @@ void ShaderTextEditor::_code_complete_script(const String &p_code, List<ScriptCo ShaderLanguage sl; String calltip; - sl.complete(p_code, ShaderTypes::get_singleton()->get_functions(RenderingServer::ShaderMode(shader->get_mode())), ShaderTypes::get_singleton()->get_modes(RenderingServer::ShaderMode(shader->get_mode())), ShaderLanguage::VaryingFunctionNames(), ShaderTypes::get_singleton()->get_types(), _get_global_variable_type, r_options, calltip); + ShaderLanguage::ShaderCompileInfo info; + info.functions = ShaderTypes::get_singleton()->get_functions(RenderingServer::ShaderMode(shader->get_mode())); + info.render_modes = ShaderTypes::get_singleton()->get_modes(RenderingServer::ShaderMode(shader->get_mode())); + info.shader_types = ShaderTypes::get_singleton()->get_types(); + info.global_variable_type_func = _get_global_variable_type; + + sl.complete(p_code, info, r_options, calltip); get_text_editor()->set_code_hint(calltip); } @@ -216,12 +222,18 @@ void ShaderTextEditor::_validate_script() { //List<StringName> params; //shader->get_param_list(¶ms); + ShaderLanguage::ShaderCompileInfo info; + info.functions = ShaderTypes::get_singleton()->get_functions(RenderingServer::ShaderMode(shader->get_mode())); + info.render_modes = ShaderTypes::get_singleton()->get_modes(RenderingServer::ShaderMode(shader->get_mode())); + info.shader_types = ShaderTypes::get_singleton()->get_types(); + info.global_variable_type_func = _get_global_variable_type; + ShaderLanguage sl; sl.enable_warning_checking(saved_warnings_enabled); sl.set_warning_flags(saved_warning_flags); - Error err = sl.compile(code, ShaderTypes::get_singleton()->get_functions(RenderingServer::ShaderMode(shader->get_mode())), ShaderTypes::get_singleton()->get_modes(RenderingServer::ShaderMode(shader->get_mode())), ShaderLanguage::VaryingFunctionNames(), ShaderTypes::get_singleton()->get_types(), _get_global_variable_type); + Error err = sl.compile(code, info); if (err != OK) { String error_text = "error(" + itos(sl.get_error_line()) + "): " + sl.get_error_text(); diff --git a/editor/plugins/visual_shader_editor_plugin.cpp b/editor/plugins/visual_shader_editor_plugin.cpp index a71a8b33cb..ca84fa1cf0 100644 --- a/editor/plugins/visual_shader_editor_plugin.cpp +++ b/editor/plugins/visual_shader_editor_plugin.cpp @@ -3943,9 +3943,15 @@ void VisualShaderEditor::_update_preview() { preview_text->set_text(code); + ShaderLanguage::ShaderCompileInfo info; + info.functions = ShaderTypes::get_singleton()->get_functions(RenderingServer::ShaderMode(visual_shader->get_mode())); + info.render_modes = ShaderTypes::get_singleton()->get_modes(RenderingServer::ShaderMode(visual_shader->get_mode())); + info.shader_types = ShaderTypes::get_singleton()->get_types(); + info.global_variable_type_func = _get_global_variable_type; + ShaderLanguage sl; - Error err = sl.compile(code, ShaderTypes::get_singleton()->get_functions(RenderingServer::ShaderMode(visual_shader->get_mode())), ShaderTypes::get_singleton()->get_modes(RenderingServer::ShaderMode(visual_shader->get_mode())), ShaderLanguage::VaryingFunctionNames(), ShaderTypes::get_singleton()->get_types(), _get_global_variable_type); + Error err = sl.compile(code, info); for (int i = 0; i < preview_text->get_line_count(); i++) { preview_text->set_line_background_color(i, Color(0, 0, 0, 0)); diff --git a/servers/rendering/renderer_rd/shader_compiler_rd.cpp b/servers/rendering/renderer_rd/shader_compiler_rd.cpp index b02b3d2723..794c999d1d 100644 --- a/servers/rendering/renderer_rd/shader_compiler_rd.cpp +++ b/servers/rendering/renderer_rd/shader_compiler_rd.cpp @@ -1413,7 +1413,13 @@ ShaderLanguage::DataType ShaderCompilerRD::_get_variable_type(const StringName & } Error ShaderCompilerRD::compile(RS::ShaderMode p_mode, const String &p_code, IdentifierActions *p_actions, const String &p_path, GeneratedCode &r_gen_code) { - Error err = parser.compile(p_code, ShaderTypes::get_singleton()->get_functions(p_mode), ShaderTypes::get_singleton()->get_modes(p_mode), ShaderLanguage::VaryingFunctionNames(), ShaderTypes::get_singleton()->get_types(), _get_variable_type); + SL::ShaderCompileInfo info; + info.functions = ShaderTypes::get_singleton()->get_functions(p_mode); + info.render_modes = ShaderTypes::get_singleton()->get_modes(p_mode); + info.shader_types = ShaderTypes::get_singleton()->get_types(); + info.global_variable_type_func = _get_variable_type; + + Error err = parser.compile(p_code, info); if (err != OK) { Vector<String> shader = p_code.split("\n"); diff --git a/servers/rendering/shader_language.cpp b/servers/rendering/shader_language.cpp index f5e91d0423..6acadc6882 100644 --- a/servers/rendering/shader_language.cpp +++ b/servers/rendering/shader_language.cpp @@ -8993,17 +8993,17 @@ uint32_t ShaderLanguage::get_warning_flags() const { } #endif // DEBUG_ENABLED -Error ShaderLanguage::compile(const String &p_code, const Map<StringName, FunctionInfo> &p_functions, const Vector<StringName> &p_render_modes, const VaryingFunctionNames &p_varying_function_names, const Set<String> &p_shader_types, GlobalVariableGetTypeFunc p_global_variable_type_func) { +Error ShaderLanguage::compile(const String &p_code, const ShaderCompileInfo &p_info) { clear(); code = p_code; - global_var_get_type_func = p_global_variable_type_func; - varying_function_names = p_varying_function_names; + global_var_get_type_func = p_info.global_variable_type_func; + varying_function_names = p_info.varying_function_names; nodes = nullptr; shader = alloc_node<ShaderNode>(); - Error err = _parse_shader(p_functions, p_render_modes, p_shader_types); + Error err = _parse_shader(p_info.functions, p_info.render_modes, p_info.shader_types); #ifdef DEBUG_ENABLED if (check_warnings) { @@ -9017,17 +9017,17 @@ Error ShaderLanguage::compile(const String &p_code, const Map<StringName, Functi return OK; } -Error ShaderLanguage::complete(const String &p_code, const Map<StringName, FunctionInfo> &p_functions, const Vector<StringName> &p_render_modes, const VaryingFunctionNames &p_varying_function_names, const Set<String> &p_shader_types, GlobalVariableGetTypeFunc p_global_variable_type_func, List<ScriptCodeCompletionOption> *r_options, String &r_call_hint) { +Error ShaderLanguage::complete(const String &p_code, const ShaderCompileInfo &p_info, List<ScriptCodeCompletionOption> *r_options, String &r_call_hint) { clear(); code = p_code; - varying_function_names = p_varying_function_names; + varying_function_names = p_info.varying_function_names; nodes = nullptr; - global_var_get_type_func = p_global_variable_type_func; + global_var_get_type_func = p_info.global_variable_type_func; shader = alloc_node<ShaderNode>(); - _parse_shader(p_functions, p_render_modes, p_shader_types); + _parse_shader(p_info.functions, p_info.render_modes, p_info.shader_types); switch (completion_type) { case COMPLETION_NONE: { @@ -9035,8 +9035,8 @@ Error ShaderLanguage::complete(const String &p_code, const Map<StringName, Funct return OK; } break; case COMPLETION_RENDER_MODE: { - for (int i = 0; i < p_render_modes.size(); i++) { - ScriptCodeCompletionOption option(p_render_modes[i], ScriptCodeCompletionOption::KIND_ENUM); + for (int i = 0; i < p_info.render_modes.size(); i++) { + ScriptCodeCompletionOption option(p_info.render_modes[i], ScriptCodeCompletionOption::KIND_ENUM); r_options->push_back(option); } @@ -9054,7 +9054,7 @@ Error ShaderLanguage::complete(const String &p_code, const Map<StringName, Funct return OK; } break; case COMPLETION_MAIN_FUNCTION: { - for (const KeyValue<StringName, FunctionInfo> &E : p_functions) { + for (const KeyValue<StringName, FunctionInfo> &E : p_info.functions) { ScriptCodeCompletionOption option(E.key, ScriptCodeCompletionOption::KIND_FUNCTION); r_options->push_back(option); } @@ -9090,8 +9090,8 @@ Error ShaderLanguage::complete(const String &p_code, const Map<StringName, Funct } if (comp_ident) { - if (p_functions.has("global")) { - for (const KeyValue<StringName, BuiltInInfo> &E : p_functions["global"].built_ins) { + if (p_info.functions.has("global")) { + for (const KeyValue<StringName, BuiltInInfo> &E : p_info.functions["global"].built_ins) { ScriptCodeCompletionOption::Kind kind = ScriptCodeCompletionOption::KIND_MEMBER; if (E.value.constant) { kind = ScriptCodeCompletionOption::KIND_CONSTANT; @@ -9100,8 +9100,8 @@ Error ShaderLanguage::complete(const String &p_code, const Map<StringName, Funct } } - if (p_functions.has("constants")) { - for (const KeyValue<StringName, BuiltInInfo> &E : p_functions["constants"].built_ins) { + if (p_info.functions.has("constants")) { + for (const KeyValue<StringName, BuiltInInfo> &E : p_info.functions["constants"].built_ins) { ScriptCodeCompletionOption::Kind kind = ScriptCodeCompletionOption::KIND_MEMBER; if (E.value.constant) { kind = ScriptCodeCompletionOption::KIND_CONSTANT; @@ -9110,8 +9110,8 @@ Error ShaderLanguage::complete(const String &p_code, const Map<StringName, Funct } } - if (skip_function != StringName() && p_functions.has(skip_function)) { - for (const KeyValue<StringName, BuiltInInfo> &E : p_functions[skip_function].built_ins) { + if (skip_function != StringName() && p_info.functions.has(skip_function)) { + for (const KeyValue<StringName, BuiltInInfo> &E : p_info.functions[skip_function].built_ins) { ScriptCodeCompletionOption::Kind kind = ScriptCodeCompletionOption::KIND_MEMBER; if (E.value.constant) { kind = ScriptCodeCompletionOption::KIND_CONSTANT; diff --git a/servers/rendering/shader_language.h b/servers/rendering/shader_language.h index 3de89a89a5..ed218975cd 100644 --- a/servers/rendering/shader_language.h +++ b/servers/rendering/shader_language.h @@ -1024,8 +1024,17 @@ public: void clear(); static String get_shader_type(const String &p_code); - Error compile(const String &p_code, const Map<StringName, FunctionInfo> &p_functions, const Vector<StringName> &p_render_modes, const VaryingFunctionNames &p_varying_function_names, const Set<String> &p_shader_types, GlobalVariableGetTypeFunc p_global_variable_type_func); - Error complete(const String &p_code, const Map<StringName, FunctionInfo> &p_functions, const Vector<StringName> &p_render_modes, const VaryingFunctionNames &p_varying_function_names, const Set<String> &p_shader_types, GlobalVariableGetTypeFunc p_global_variable_type_func, List<ScriptCodeCompletionOption> *r_options, String &r_call_hint); + + struct ShaderCompileInfo { + Map<StringName, FunctionInfo> functions; + Vector<StringName> render_modes; + VaryingFunctionNames varying_function_names = VaryingFunctionNames(); + Set<String> shader_types; + GlobalVariableGetTypeFunc global_variable_type_func = nullptr; + }; + + Error compile(const String &p_code, const ShaderCompileInfo &p_info); + Error complete(const String &p_code, const ShaderCompileInfo &p_info, List<ScriptCodeCompletionOption> *r_options, String &r_call_hint); String get_error_text(); int get_error_line(); diff --git a/tests/servers/test_shader_lang.cpp b/tests/servers/test_shader_lang.cpp index f4a32c6723..1f8f395e64 100644 --- a/tests/servers/test_shader_lang.cpp +++ b/tests/servers/test_shader_lang.cpp @@ -346,7 +346,12 @@ MainLoop *test() { Set<String> types; types.insert("spatial"); - Error err = sl.compile(code, dt, rm, ShaderLanguage::VaryingFunctionNames(), types, nullptr); + ShaderLanguage::ShaderCompileInfo info; + info.functions = dt; + info.render_modes = rm; + info.shader_types = types; + + Error err = sl.compile(code, info); if (err) { print_line("Error at line: " + rtos(sl.get_error_line()) + ": " + sl.get_error_text()); |