From bfeb827eb7f7d3ee47acfd7db9d244c795aa3eeb Mon Sep 17 00:00:00 2001 From: Swung0x48 Date: Wed, 12 Nov 2025 15:56:40 +0800 Subject: [PATCH] [Feat]: (MG_Test/Program): add test to check optfine shader behavior --- MobileGL/MG_Test/Program/ProgramTest.cpp | 102 +++++++++++++++++- MobileGL/MG_Test/Program/ProgramUtilTest.cpp | 30 +++--- .../ShaderTranspiler/ShaderCompiler.cpp | 4 + 3 files changed, 120 insertions(+), 16 deletions(-) diff --git a/MobileGL/MG_Test/Program/ProgramTest.cpp b/MobileGL/MG_Test/Program/ProgramTest.cpp index 1bb16ade..4fb40c35 100644 --- a/MobileGL/MG_Test/Program/ProgramTest.cpp +++ b/MobileGL/MG_Test/Program/ProgramTest.cpp @@ -835,4 +835,104 @@ TEST_F(ProgramTest, MinecraftTexColor1_21_6) { spvcSession.Compile(&result); printf("%s\n\n", result); } -} \ No newline at end of file +} + +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 << "\")"; +} + diff --git a/MobileGL/MG_Test/Program/ProgramUtilTest.cpp b/MobileGL/MG_Test/Program/ProgramUtilTest.cpp index 413e9c3c..dfc33bff 100644 --- a/MobileGL/MG_Test/Program/ProgramUtilTest.cpp +++ b/MobileGL/MG_Test/Program/ProgramUtilTest.cpp @@ -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 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, diff --git a/MobileGL/MG_Util/ShaderTranspiler/ShaderCompiler.cpp b/MobileGL/MG_Util/ShaderTranspiler/ShaderCompiler.cpp index ecfcf200..a8e5e722 100644 --- a/MobileGL/MG_Util/ShaderTranspiler/ShaderCompiler.cpp +++ b/MobileGL/MG_Util/ShaderTranspiler/ShaderCompiler.cpp @@ -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 resolver; UniquePtr resolver; for (unsigned stage = 0; stage < EShLangCount; stage++) {