From 4c2ffa7af37a96aa551c282cc02879c907ae14cd Mon Sep 17 00:00:00 2001 From: Yuri Roubinsky Date: Fri, 6 Aug 2021 15:33:15 +0300 Subject: [PATCH] Fix test_shader_lang (for 3.x) --- main/tests/test_shader_lang.cpp | 140 ++++++++++++++++++++++++-------- 1 file changed, 107 insertions(+), 33 deletions(-) diff --git a/main/tests/test_shader_lang.cpp b/main/tests/test_shader_lang.cpp index a8b2e262bb2..adc1c510ba7 100644 --- a/main/tests/test_shader_lang.cpp +++ b/main/tests/test_shader_lang.cpp @@ -38,6 +38,7 @@ #include "scene/gui/control.h" #include "scene/gui/text_edit.h" #include "servers/visual/shader_language.h" +#include "servers/visual/shader_types.h" typedef ShaderLanguage SL; @@ -310,49 +311,122 @@ MainLoop *test() { if (cmdlargs.empty()) { //try editor! - print_line("usage: godot -test shader_lang "); + print_line("usage: godot -test shaderlang "); return nullptr; } - String test = cmdlargs.back()->get(); - - FileAccess *fa = FileAccess::open(test, FileAccess::READ); - - if (!fa) { - ERR_FAIL_V(nullptr); - } - - String code; - - while (true) { - CharType c = fa->get_8(); - if (fa->eof_reached()) { - break; - } - code += c; - } + List code_list; + List> dt_list; + List> rm_list; + List> types_list; + int test_count = 0; SL sl; - print_line("tokens:\n\n" + sl.token_debug(code)); - Map dt; - dt["fragment"].built_ins["ALBEDO"] = SL::TYPE_VEC3; - dt["fragment"].can_discard = true; + if (cmdlargs.empty() || cmdlargs.back()->get() == "shaderlang") { + { + String code; + code += "shader_type canvas_item;\n"; + code += "render_mode test_rm;\n"; + code += "\n"; + code += "void fragment() {\n"; + code += "\tCOLOR = vec4(1.0);\n"; + code += "\tdiscard;\n"; + code += "}\n"; + code_list.push_back(code); - Vector rm; - rm.push_back("popo"); - Set types; - types.insert("spatial"); + Vector rm; + rm.push_back("test_rm"); + rm_list.push_back(rm); - Error err = sl.compile(code, dt, rm, types); + Map dt; + dt["fragment"].built_ins["COLOR"] = SL::TYPE_VEC4; + dt["fragment"].can_discard = true; + dt_list.push_back(dt); - if (err) { - print_line("Error at line: " + rtos(sl.get_error_line()) + ": " + sl.get_error_text()); - return nullptr; + Set types; + types.insert("canvas_item"); + types_list.push_back(types); + + test_count++; + } + +#ifndef _3D_DISABLED + { + String code; + code += "shader_type spatial;\n"; + code += "render_mode test_rm;\n"; + code += "\n"; + code += "void fragment() {\n"; + code += "\tALBEDO = vec3(1.0);\n"; + code += "\tdiscard;\n"; + code += "}\n"; + code_list.push_back(code); + + Vector rm; + rm.push_back("test_rm"); + rm_list.push_back(rm); + + Map dt; + dt["fragment"].built_ins["ALBEDO"] = SL::TYPE_VEC3; + dt["fragment"].can_discard = true; + dt_list.push_back(dt); + + Set types; + types.insert("spatial"); + types_list.push_back(types); + + test_count++; + } +#endif } else { - String code2; - recreate_code(&code2, sl.get_shader()); - print_line("code:\n\n" + code2); + FileAccess *fa = FileAccess::open(cmdlargs.back()->get(), FileAccess::READ); + String code; + if (!fa) { + ERR_FAIL_V(nullptr); + } + while (true) { + CharType c = fa->get_8(); + if (fa->eof_reached()) { + break; + } + code += c; + } + code_list.push_back(code); + String type = sl.get_shader_type(code); + + if (type == "canvas_item") { + dt_list.push_back(ShaderTypes::get_singleton()->get_functions(VisualServer::ShaderMode::SHADER_CANVAS_ITEM)); + rm_list.push_back(ShaderTypes::get_singleton()->get_modes(VisualServer::ShaderMode::SHADER_CANVAS_ITEM)); + } else if (type == "spatial") { + dt_list.push_back(ShaderTypes::get_singleton()->get_functions(VisualServer::ShaderMode::SHADER_SPATIAL)); + rm_list.push_back(ShaderTypes::get_singleton()->get_modes(VisualServer::ShaderMode::SHADER_SPATIAL)); + } else if (type == "particles") { + dt_list.push_back(ShaderTypes::get_singleton()->get_functions(VisualServer::ShaderMode::SHADER_PARTICLES)); + rm_list.push_back(ShaderTypes::get_singleton()->get_modes(VisualServer::ShaderMode::SHADER_PARTICLES)); + } + types_list.push_back(ShaderTypes::get_singleton()->get_types()); + + test_count++; + } + + for (int i = 0; i < test_count; i++) { + String code = code_list[i]; + Map dt = dt_list[i]; + Vector rm = rm_list[i]; + Set types = types_list[i]; + + print_line("tokens:\n\n" + sl.token_debug(code)); + Error err = sl.compile(code, dt, rm, types); + + if (err) { + print_line("Error at line: " + rtos(sl.get_error_line()) + ": " + sl.get_error_text()); + return nullptr; + } else { + String code2; + recreate_code(&code2, sl.get_shader()); + print_line("code:\n\n" + code2); + } } return nullptr;