Fix test_shader_lang (for 3.x)

This commit is contained in:
Yuri Roubinsky 2021-08-06 15:33:15 +03:00
parent 1bbf46bfba
commit 4c2ffa7af3

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,20 +311,80 @@ 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;
List<Vector<StringName>> rm_list;
List<Set<String>> types_list;
int test_count = 0;
FileAccess *fa = FileAccess::open(test, FileAccess::READ); SL sl;
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<StringName> rm;
rm.push_back("test_rm");
rm_list.push_back(rm);
Map<StringName, SL::FunctionInfo> dt;
dt["fragment"].built_ins["COLOR"] = SL::TYPE_VEC4;
dt["fragment"].can_discard = true;
dt_list.push_back(dt);
Set<String> 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<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 {
FileAccess *fa = FileAccess::open(cmdlargs.back()->get(), FileAccess::READ);
String code;
if (!fa) { if (!fa) {
ERR_FAIL_V(nullptr); ERR_FAIL_V(nullptr);
} }
String code;
while (true) { while (true) {
CharType c = fa->get_8(); CharType c = fa->get_8();
if (fa->eof_reached()) { if (fa->eof_reached()) {
@ -331,19 +392,31 @@ MainLoop *test() {
} }
code += c; 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];
SL sl;
print_line("tokens:\n\n" + sl.token_debug(code)); print_line("tokens:\n\n" + sl.token_debug(code));
Map<StringName, SL::FunctionInfo> dt;
dt["fragment"].built_ins["ALBEDO"] = SL::TYPE_VEC3;
dt["fragment"].can_discard = true;
Vector<StringName> rm;
rm.push_back("popo");
Set<String> types;
types.insert("spatial");
Error err = sl.compile(code, dt, rm, types); Error err = sl.compile(code, dt, rm, types);
if (err) { if (err) {
@ -354,6 +427,7 @@ MainLoop *test() {
recreate_code(&code2, sl.get_shader()); recreate_code(&code2, sl.get_shader());
print_line("code:\n\n" + code2); print_line("code:\n\n" + code2);
} }
}
return nullptr; return nullptr;
} }