[Feat]: (MG_Test/Program): add test to check optfine shader behavior

This commit is contained in:
2025-11-12 15:56:40 +08:00
parent 8b8fa41ef2
commit bfeb827eb7
3 changed files with 120 additions and 16 deletions
+101 -1
View File
@@ -835,4 +835,104 @@ TEST_F(ProgramTest, MinecraftTexColor1_21_6) {
spvcSession.Compile(&result);
printf("%s\n\n", result);
}
}
}
const char* optifine_vs1 = R"(#version 460 core
in vec3 Position;
in vec2 UV0;
uniform mat4 ModelViewMat;
uniform mat4 ProjMat;
out vec2 texCoord0;
void main() {
gl_Position = ProjMat * ModelViewMat * vec4(Position, 1.0);
texCoord0 = UV0;
}
)";
const char* optifine_fs1 = R"(#version 460 core
uniform sampler2D Sampler0;
uniform vec4 ColorModulator;
in vec2 texCoord0;
out vec4 fragColor;
void main() {
vec4 color = texture(Sampler0, texCoord0);
if (color.a == 0.0) {
discard;
}
fragColor = color * ColorModulator;
})";
TEST_F(ProgramTest, CompileAndLinkOptfineSample1) {
char infoLog[1024] = "";
GLuint fs = CreateShader(GL_FRAGMENT_SHADER);
ShaderSource(fs, 1, &optifine_fs1, NULL);
CompileShader(fs);
GLint fsStatus = GL_FALSE;
GetShaderiv(fs, GL_COMPILE_STATUS, &fsStatus);
GetShaderInfoLog(fs, 1024, nullptr, infoLog);
ASSERT_EQ(fsStatus, GL_TRUE) << infoLog;
GLuint vs = CreateShader(GL_VERTEX_SHADER);
ShaderSource(vs, 1, &optifine_vs1, NULL);
CompileShader(vs);
GLint vsStatus = GL_FALSE;
GetShaderiv(vs, GL_COMPILE_STATUS, &vsStatus);
GetShaderInfoLog(vs, 1024, nullptr, infoLog);
ASSERT_EQ(vsStatus, GL_TRUE) << infoLog;
GLuint program = CreateProgram();
AttachShader(program, fs);
AttachShader(program, vs);
BindAttribLocation(program, 0, "Position");
BindAttribLocation(program, 2, "UV0");
BindAttribLocation(program, 1, "Color");
LinkProgram(program);
GLint linkStatus = GL_FALSE;
GetProgramiv(program, GL_LINK_STATUS, &linkStatus);
ASSERT_EQ(linkStatus, GL_TRUE);
printf("Program linked.\n");
UseProgram(program);
GLint posLoc = GetAttribLocation(program, "Position");
ASSERT_EQ(posLoc, 0);
GLint uv0Loc = GetAttribLocation(program, "UV0");
ASSERT_EQ(uv0Loc, 2);
auto programObject = MG_State::pGLContext->GetCurrentProgram();
auto& spirvs = programObject->GetGeneratedSpirv();
char* found_correct_uv0 = nullptr;
const char* needle = "layout(location = 2) in vec2 UV0;";
for (auto spirv: spirvs) {
MG_Util::ShaderTranspiler::SpvcSession spvcSession(spirv);
spvc_compiler_options options;
spvcSession.CreateOptions(&options);
spvc_compiler_options_set_uint(options, SPVC_COMPILER_OPTION_GLSL_VERSION, 460);
spvc_compiler_options_set_bool(options, SPVC_COMPILER_OPTION_GLSL_ES, SPVC_FALSE);
// spvc_compiler_options_set_bool(options, SPVC_COMPILER_OPTION_GLSL_VULKAN_SEMANTICS, SPVC_FALSE);
spvcSession.SetOptions(options);
const char* result = nullptr;
spvcSession.Compile(&result);
printf("%s\n\n", result);
const char* ret = strstr(result, needle);
if (ret)
found_correct_uv0 = (char*)ret;
}
ASSERT_TRUE(found_correct_uv0 != nullptr) << "Not found correct attribute in generated shader.\n(Searching for \"" << needle << "\")";
}
+15 -15
View File
@@ -342,41 +342,37 @@ TEST_F(ProgramUtilTest, DecompProgram) {
const char* blit_vs = R"(#version 460 core
in vec3 Position;
in vec2 UV;
in vec4 Color;
in vec2 UV0;
uniform mat4 ModelViewMat;
uniform mat4 ProjMat;
out vec2 texCoord;
out vec4 vertexColor;
out vec2 texCoord0;
void main() {
gl_Position = ProjMat * ModelViewMat * vec4(Position, 1.0);
texCoord = UV;
vertexColor = Color;
texCoord0 = UV0;
}
)";
const char* blit_fs = R"(#version 460 core
uniform sampler2D DiffuseSampler;
uniform sampler2D Sampler0;
uniform vec4 ColorModulator;
in vec2 texCoord;
in vec4 vertexColor;
in vec2 texCoord0;
out vec4 fragColor;
void main() {
vec4 color = texture(DiffuseSampler, texCoord) * vertexColor;
// blit final output of compositor into displayed back buffer
vec4 color = texture(Sampler0, texCoord0);
if (color.a == 0.0) {
discard;
}
fragColor = color * ColorModulator;
}
)";
})";
TEST_F(ProgramUtilTest, CompileAndLinkBlitProgram) {
using namespace MG_Util::ShaderTranspiler;
@@ -396,7 +392,7 @@ TEST_F(ProgramUtilTest, CompileAndLinkBlitProgram) {
UnorderedMap<String, Uint> attribLocations;
attribLocations["Position"] = 0;
attribLocations["UV"] = 2;
attribLocations["UV0"] = 2;
ProgramAttrib programAttrib{// .shaderTypes = { GL_VERTEX_SHADER, GL_FRAGMENT_SHADER },
.shaders = {vs_res.value(), fs_res.value()},
@@ -416,9 +412,13 @@ TEST_F(ProgramUtilTest, CompileAndLinkBlitProgram) {
auto it = attribLocations.find(in.name);
if (it != attribLocations.end()) {
ASSERT_EQ(it->second, in.layoutLocation());
std::cout << in.name << ": location = " << it->second << "\n";
attribLocations.erase(it);
}
}
ASSERT_TRUE(attribLocations.empty()) << "Not all vertex input location mapped!";
ProgramBinaryAttrib binaryAttrib{
.shaderTypes = {GL_VERTEX_SHADER, GL_FRAGMENT_SHADER},
.program = *program,
@@ -174,6 +174,10 @@ namespace MobileGL {
return std::unexpected(r);
}
for (auto [name, loc]: attrib.explicitAttribLocations) {
MGLOG_D("%s: got explicitly set - layout(location = %d) %s;", __func__, loc, name.c_str());
}
// UniquePtr<glslang::TIoMapResolver> resolver;
UniquePtr<TMglGlslIoResolver> resolver;
for (unsigned stage = 0; stage < EShLangCount; stage++) {