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,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;
} }