Merge pull request #51316 from Chaosus/fix_shader_test

This commit is contained in:
Rémi Verschelde 2022-02-22 13:53:53 +01:00 committed by GitHub
commit 24713a27ae
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 107 additions and 33 deletions

View File

@ -38,6 +38,7 @@
#include "scene/gui/control.h" #include "scene/gui/control.h"
#include "scene/gui/text_edit.h" #include "scene/gui/text_edit.h"
#include "servers/visual/shader_language.h" #include "servers/visual/shader_language.h"
#include "servers/visual/shader_types.h"
typedef ShaderLanguage SL; typedef ShaderLanguage SL;
@ -310,49 +311,122 @@ MainLoop *test() {
if (cmdlargs.empty()) { if (cmdlargs.empty()) {
//try editor! //try editor!
print_line("usage: godot -test shader_lang <shader>"); print_line("usage: godot -test shaderlang <shader>");
return nullptr; return nullptr;
} }
String test = cmdlargs.back()->get(); List<String> code_list;
List<Map<StringName, SL::FunctionInfo>> dt_list;
FileAccess *fa = FileAccess::open(test, FileAccess::READ); List<Vector<StringName>> rm_list;
List<Set<String>> types_list;
if (!fa) { int test_count = 0;
ERR_FAIL_V(nullptr);
}
String code;
while (true) {
CharType c = fa->get_8();
if (fa->eof_reached()) {
break;
}
code += c;
}
SL sl; SL sl;
print_line("tokens:\n\n" + sl.token_debug(code));
Map<StringName, SL::FunctionInfo> dt; if (cmdlargs.empty() || cmdlargs.back()->get() == "shaderlang") {
dt["fragment"].built_ins["ALBEDO"] = SL::TYPE_VEC3; {
dt["fragment"].can_discard = true; 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<StringName> rm; Vector<StringName> rm;
rm.push_back("popo"); rm.push_back("test_rm");
Set<String> types; rm_list.push_back(rm);
types.insert("spatial");
Error err = sl.compile(code, dt, rm, types); Map<StringName, SL::FunctionInfo> dt;
dt["fragment"].built_ins["COLOR"] = SL::TYPE_VEC4;
dt["fragment"].can_discard = true;
dt_list.push_back(dt);
if (err) { Set<String> types;
print_line("Error at line: " + rtos(sl.get_error_line()) + ": " + sl.get_error_text()); types.insert("canvas_item");
return nullptr; 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<StringName> rm;
rm.push_back("test_rm");
rm_list.push_back(rm);
Map<StringName, SL::FunctionInfo> dt;
dt["fragment"].built_ins["ALBEDO"] = SL::TYPE_VEC3;
dt["fragment"].can_discard = true;
dt_list.push_back(dt);
Set<String> types;
types.insert("spatial");
types_list.push_back(types);
test_count++;
}
#endif
} else { } else {
String code2; FileAccess *fa = FileAccess::open(cmdlargs.back()->get(), FileAccess::READ);
recreate_code(&code2, sl.get_shader()); String code;
print_line("code:\n\n" + code2); 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<StringName, SL::FunctionInfo> dt = dt_list[i];
Vector<StringName> rm = rm_list[i];
Set<String> 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; return nullptr;