From d6620959b3a2f6626a60efee2620194c65ed36ba Mon Sep 17 00:00:00 2001 From: Swung0x48 Date: Sat, 16 Aug 2025 01:10:04 +0800 Subject: [PATCH 01/12] [Feat] (MG_State/Program): BindAttribLocation WIP --- .../MG_Impl/GLImpl/Program/GL_Program.cpp | 22 +++++++++++- .../GLState/ProgramState/ProgramObject.cpp | 36 +++++++++++++++++++ .../GLState/ProgramState/ProgramObject.h | 10 ++++++ MobileGL/MG_Test/Program/ProgramTest.cpp | 3 +- 4 files changed, 69 insertions(+), 2 deletions(-) diff --git a/MobileGL/MG_Impl/GLImpl/Program/GL_Program.cpp b/MobileGL/MG_Impl/GLImpl/Program/GL_Program.cpp index a8ba513c..1ec29ee4 100644 --- a/MobileGL/MG_Impl/GLImpl/Program/GL_Program.cpp +++ b/MobileGL/MG_Impl/GLImpl/Program/GL_Program.cpp @@ -85,7 +85,27 @@ namespace MobileGL { } void BindAttribLocation_State(GLuint program, GLuint index, const GLchar* name) { - THROW_UNIMPL_EXCEPTION; + if (index >= MG_State::GLState::VertexArrayObject::MAX_VERTEX_ATTRIBS) { + MG_State::pGLContext->RecordError( + ErrorCode::InvalidValue, + MakeShared("MG_Impl/GLImpl", __func__, + "`index` is greater than or equal to `GL_MAX_VERTEX_ATTRIBS`.")); + return; + } + + if (strncmp(name, "gl_", 3) == 0) { + MG_State::pGLContext->RecordError( + ErrorCode::InvalidOperation, + MakeShared("MG_Impl/GLImpl", __func__, + "`name` starts with the reserved prefix `gl_`.")); + return; + } + + auto programObject = TryToGetProgramObject(program); + if (!programObject) + return; + + programObject->SetExplicitAttribLocation(index, name); } void CompileShader_State(GLuint shader) { diff --git a/MobileGL/MG_State/GLState/ProgramState/ProgramObject.cpp b/MobileGL/MG_State/GLState/ProgramState/ProgramObject.cpp index f4534ed4..f38d2885 100644 --- a/MobileGL/MG_State/GLState/ProgramState/ProgramObject.cpp +++ b/MobileGL/MG_State/GLState/ProgramState/ProgramObject.cpp @@ -89,12 +89,44 @@ namespace MobileGL { m_uniformTypes[location] = uniform.glDefineType; } + // attributes (pipe in) int inCount = m_program->getNumPipeInputs(); + m_attribs.resize(inCount); + + // Parse explicit location in shader for (int i = 0; i < inCount; i++) { auto& inVar = m_program->getPipeInput(i); + auto location = inVar.layoutLocation(); m_attribInNameMaxLength = std::max(m_attribInNameMaxLength, (Int)inVar.name.length()); + // TODO: how to parse location specified in shader? + } + // Place explicitly set locations + for (int i = 0; i < inCount; i++) { + auto& inVar = m_program->getPipeInput(i); + + auto it = m_explicitAttribLocations.find(inVar.name); + if (it != m_explicitAttribLocations.end()) { + m_attribs[it->second] = inVar.name; + } + } + // Place yet placed attributes + int nextAvailLoc = 0; + for (int i = 0; i < inCount; i++) { + auto& inVar = m_program->getPipeInput(i); + auto it = m_explicitAttribLocations.find(inVar.name); + if (it != m_explicitAttribLocations.end()) { + continue; + } + while (nextAvailLoc < inCount && !m_attribs[nextAvailLoc].empty()) + ++nextAvailLoc; + + assert(nextAvailLoc < inCount); + + m_attribs[nextAvailLoc] = inVar.name; } + + // UBO int uboCount = m_program->getNumUniformBlocks(); for (int i = 0; i < uboCount; i++) { auto& ubo = m_program->getUniformBlock(i); @@ -102,6 +134,10 @@ namespace MobileGL { } } + void ProgramObject::SetExplicitAttribLocation(Uint index, const char *name) { + m_explicitAttribLocations[name] = index; + } + // void ProgramObject::PreLink() { // m_uniforms.clear(); // m_uniformOffsets.clear(); diff --git a/MobileGL/MG_State/GLState/ProgramState/ProgramObject.h b/MobileGL/MG_State/GLState/ProgramState/ProgramObject.h index 76a65543..9947c633 100644 --- a/MobileGL/MG_State/GLState/ProgramState/ProgramObject.h +++ b/MobileGL/MG_State/GLState/ProgramState/ProgramObject.h @@ -14,6 +14,9 @@ namespace MobileGL { SizeT DetachShader(SharedPtr shader); void Link(); void MarkAsDeleted(); + + void SetExplicitAttribLocation(Uint index, const char* name); + Vector>& GetAttachedShaders(); const String& GetInfoLog() const { return m_infoLog; } Int GetUniformMaxLength() const { return m_uniformNameMaxLength; } @@ -48,11 +51,18 @@ namespace MobileGL { SharedPtr m_program; + // Attributes + UnorderedMap m_explicitAttribLocations; + Vector m_attribs; + // Uniforms // MG_Util::ShaderTranspiler::SpvcMetadata m_metadata; UnorderedMap m_uniformLocations; + // Ordered by location, + // aka. m_uniformNames[loc] == "name at location `loc`" Vector m_uniformNames; + // ditto. Vector m_uniformTypes; // Need to be reflected after linking of SPIR-V binary diff --git a/MobileGL/MG_Test/Program/ProgramTest.cpp b/MobileGL/MG_Test/Program/ProgramTest.cpp index 6e0cd42a..95118920 100644 --- a/MobileGL/MG_Test/Program/ProgramTest.cpp +++ b/MobileGL/MG_Test/Program/ProgramTest.cpp @@ -24,6 +24,7 @@ TEST_F(ProgramTest, Sanity) { const char* vsSrc = R"(#version 460 in vec4 Position; +in float fIn; layout(location = 0) uniform mat4 ProjMat; uniform vec2 InSize; @@ -36,7 +37,7 @@ void main(){ vec4 outPos = ProjMat * vec4(Position.xy, 0.0, 1.0); gl_Position = vec4(outPos.xy, 0.2, 1.0); - oneTexel = 1.0 / InSize; + oneTexel = (1.0 * fIn) / InSize; texCoord = Position.xy / OutSize; })"; From f328f71f626162f7d22eb08d67291dcec136bd7f Mon Sep 17 00:00:00 2001 From: Swung0x48 Date: Sat, 16 Aug 2025 22:51:21 +0800 Subject: [PATCH 02/12] [Feat] (MG_State/Program): BindAttribLocation --- .../GLState/ProgramState/ProgramObject.cpp | 33 +++++-------------- 1 file changed, 8 insertions(+), 25 deletions(-) diff --git a/MobileGL/MG_State/GLState/ProgramState/ProgramObject.cpp b/MobileGL/MG_State/GLState/ProgramState/ProgramObject.cpp index f38d2885..fb1315aa 100644 --- a/MobileGL/MG_State/GLState/ProgramState/ProgramObject.cpp +++ b/MobileGL/MG_State/GLState/ProgramState/ProgramObject.cpp @@ -93,38 +93,21 @@ namespace MobileGL { int inCount = m_program->getNumPipeInputs(); m_attribs.resize(inCount); - // Parse explicit location in shader + // Get locations parsed in program for (int i = 0; i < inCount; i++) { auto& inVar = m_program->getPipeInput(i); auto location = inVar.layoutLocation(); m_attribInNameMaxLength = std::max(m_attribInNameMaxLength, (Int)inVar.name.length()); - // TODO: how to parse location specified in shader? + m_attribs[location] = inVar.name; } - // Place explicitly set locations - for (int i = 0; i < inCount; i++) { - auto& inVar = m_program->getPipeInput(i); - - auto it = m_explicitAttribLocations.find(inVar.name); - if (it != m_explicitAttribLocations.end()) { - m_attribs[it->second] = inVar.name; + // Implement glBindAttribLocation semantics + for (auto& [name, location]: m_explicitAttribLocations) { + assert(location < m_attribs.size()); + if (m_attribs[location] != name) { + auto it = std::find(m_attribs.begin(), m_attribs.end(), name); + std::swap(m_attribs[location], m_attribs[std::distance(m_attribs.begin(), it)]); } } - // Place yet placed attributes - int nextAvailLoc = 0; - for (int i = 0; i < inCount; i++) { - auto& inVar = m_program->getPipeInput(i); - auto it = m_explicitAttribLocations.find(inVar.name); - if (it != m_explicitAttribLocations.end()) { - continue; - } - while (nextAvailLoc < inCount && !m_attribs[nextAvailLoc].empty()) - ++nextAvailLoc; - - assert(nextAvailLoc < inCount); - - m_attribs[nextAvailLoc] = inVar.name; - } - // UBO int uboCount = m_program->getNumUniformBlocks(); From 6416788856731ee5947aba4e247fb2545ecfe705 Mon Sep 17 00:00:00 2001 From: Swung0x48 Date: Sat, 16 Aug 2025 22:54:31 +0800 Subject: [PATCH 03/12] [Fix] (MG_State/Program): skip attrib location binding when it is not active at the first place --- MobileGL/MG_State/GLState/ProgramState/ProgramObject.cpp | 2 ++ 1 file changed, 2 insertions(+) diff --git a/MobileGL/MG_State/GLState/ProgramState/ProgramObject.cpp b/MobileGL/MG_State/GLState/ProgramState/ProgramObject.cpp index fb1315aa..669717a2 100644 --- a/MobileGL/MG_State/GLState/ProgramState/ProgramObject.cpp +++ b/MobileGL/MG_State/GLState/ProgramState/ProgramObject.cpp @@ -105,6 +105,8 @@ namespace MobileGL { assert(location < m_attribs.size()); if (m_attribs[location] != name) { auto it = std::find(m_attribs.begin(), m_attribs.end(), name); + if (it == m_attribs.end()) + continue; std::swap(m_attribs[location], m_attribs[std::distance(m_attribs.begin(), it)]); } } From a072c2fc7069bb8b8ed02a81025ee04d1f4181f6 Mon Sep 17 00:00:00 2001 From: Swung0x48 Date: Sat, 16 Aug 2025 23:10:12 +0800 Subject: [PATCH 04/12] [Feat] (MG_State/Program): GetAttribLocation --- MobileGL/MG_Impl/GLImpl/Program/GL_Program.cpp | 9 ++++++++- MobileGL/MG_State/GLState/ProgramState/ProgramObject.h | 7 ++++++- 2 files changed, 14 insertions(+), 2 deletions(-) diff --git a/MobileGL/MG_Impl/GLImpl/Program/GL_Program.cpp b/MobileGL/MG_Impl/GLImpl/Program/GL_Program.cpp index 1ec29ee4..14242442 100644 --- a/MobileGL/MG_Impl/GLImpl/Program/GL_Program.cpp +++ b/MobileGL/MG_Impl/GLImpl/Program/GL_Program.cpp @@ -215,7 +215,14 @@ namespace MobileGL { } GLint GetAttribLocation_State(GLuint program, const GLchar* name) { - THROW_UNIMPL_EXCEPTION; + auto programObject = TryToGetProgramObject(program); + if (!programObject) + return -1; + if (strncmp(name, "gl_", 3) == 0) + return -1; + if (!programObject->GetLinkStatus()) + return -1; + return programObject->GetAttributeLocation(name); } void GetProgramiv_State(GLuint program, GLenum pname, GLint* params) { diff --git a/MobileGL/MG_State/GLState/ProgramState/ProgramObject.h b/MobileGL/MG_State/GLState/ProgramState/ProgramObject.h index 9947c633..31a1d920 100644 --- a/MobileGL/MG_State/GLState/ProgramState/ProgramObject.h +++ b/MobileGL/MG_State/GLState/ProgramState/ProgramObject.h @@ -33,6 +33,11 @@ namespace MobileGL { return m_uniformNames[index]; } + Int GetAttributeLocation(const String& name) { + const auto it = std::find(m_attribs.begin(), m_attribs.end(), name); + return (it == m_attribs.end()) ? -1 : std::distance(m_attribs.begin(), it); + } + Bool GetDeleteStatus() const { return m_deleteStatus; } Bool GetLinkStatus() const { return m_linkStatus; } Bool GetValidateStatus() const { return m_validateStatus; } @@ -76,7 +81,7 @@ namespace MobileGL { String m_infoLog; Bool m_deleteStatus = false; - Bool m_linkStatus = true; + Bool m_linkStatus = false; Bool m_validateStatus = true; }; } // namespace GLState From dc1a344cf56c7ff99c2a9ae615e10e12a25ae442 Mon Sep 17 00:00:00 2001 From: Swung0x48 Date: Sat, 16 Aug 2025 23:10:52 +0800 Subject: [PATCH 05/12] [Feat] (MG_Test/Program): Test for GetAttribLocation/BindAttribLocation --- MobileGL/MG_Test/Program/ProgramTest.cpp | 20 +++++++++++++++++--- 1 file changed, 17 insertions(+), 3 deletions(-) diff --git a/MobileGL/MG_Test/Program/ProgramTest.cpp b/MobileGL/MG_Test/Program/ProgramTest.cpp index 95118920..0449d560 100644 --- a/MobileGL/MG_Test/Program/ProgramTest.cpp +++ b/MobileGL/MG_Test/Program/ProgramTest.cpp @@ -23,8 +23,13 @@ TEST_F(ProgramTest, Sanity) { const char* vsSrc = R"(#version 460 -in vec4 Position; -in float fIn; +layout (location = 0) in vec4 Position; +in float fIn4; +in float fIn2; +in float fIn5; +in float fIn6; +in float fIn1; +in float fIn3; layout(location = 0) uniform mat4 ProjMat; uniform vec2 InSize; @@ -37,7 +42,7 @@ void main(){ vec4 outPos = ProjMat * vec4(Position.xy, 0.0, 1.0); gl_Position = vec4(outPos.xy, 0.2, 1.0); - oneTexel = (1.0 * fIn) / InSize; + oneTexel = (1.0 * (fIn1 * fIn2 * fIn3 * fIn4 * fIn5 * fIn6)) / InSize; texCoord = Position.xy / OutSize; })"; @@ -111,6 +116,10 @@ TEST_F(ProgramTest, CompileAndLink) { GLuint program = CreateProgram(); AttachShader(program, vs); AttachShader(program, fs); + + BindAttribLocation(program, 1, "fIn1"); + BindAttribLocation(program, 3, "fIn3"); + BindAttribLocation(program, 5, "fIn5"); printf("Linking program...\n"); LinkProgram(program); printf("Program linked.\n"); @@ -124,4 +133,9 @@ TEST_F(ProgramTest, CompileAndLink) { GLint uniformNameMaxLength = 0; GetProgramiv(program, GL_ACTIVE_UNIFORM_MAX_LENGTH, &uniformNameMaxLength); EXPECT_EQ(uniformNameMaxLength, 12); + + EXPECT_EQ(GetAttribLocation(program, "Position"), 0); + EXPECT_EQ(GetAttribLocation(program, "fIn1"), 1); + EXPECT_EQ(GetAttribLocation(program, "fIn3"), 3); + EXPECT_EQ(GetAttribLocation(program, "fIn5"), 5); } From f6a0042356580b63df6cedfdf6dd66ae0f561672 Mon Sep 17 00:00:00 2001 From: Swung0x48 Date: Sat, 16 Aug 2025 23:30:26 +0800 Subject: [PATCH 06/12] [Chore] (workflow): don't debug output by default --- .github/workflows/test.yml | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/.github/workflows/test.yml b/.github/workflows/test.yml index 308be194..db49cd8f 100644 --- a/.github/workflows/test.yml +++ b/.github/workflows/test.yml @@ -48,4 +48,9 @@ jobs: - name: Test working-directory: ${{env.TEST_ROOT}}/build-test - run: ctest -V \ No newline at end of file + run: | + if [ "${{ secrets.ACTIONS_STEP_DEBUG }}" == "true" ]; then + ctest -V + else + ctest + fi From 2036caa4a00159e4d465c3505b95099a7142b3c4 Mon Sep 17 00:00:00 2001 From: Swung0x48 Date: Mon, 18 Aug 2025 09:50:30 +0800 Subject: [PATCH 07/12] [Chore] (MG_State/Program): remove shaderTypes from ProgramAttrib --- .../MG_State/GLState/ProgramState/ProgramObject.cpp | 2 +- MobileGL/MG_Test/Program/ProgramUtilTest.cpp | 10 +++++----- MobileGL/MG_Util/ShaderTranspiler/Types.h | 2 +- 3 files changed, 7 insertions(+), 7 deletions(-) diff --git a/MobileGL/MG_State/GLState/ProgramState/ProgramObject.cpp b/MobileGL/MG_State/GLState/ProgramState/ProgramObject.cpp index 669717a2..3e40609f 100644 --- a/MobileGL/MG_State/GLState/ProgramState/ProgramObject.cpp +++ b/MobileGL/MG_State/GLState/ProgramState/ProgramObject.cpp @@ -36,7 +36,7 @@ namespace MobileGL { } MG_Util::ShaderTranspiler::ProgramAttrib attrib{ - .shaderTypes = Move(shaderTypes), + // .shaderTypes = Move(shaderTypes), .shaders = Move(shaders), }; diff --git a/MobileGL/MG_Test/Program/ProgramUtilTest.cpp b/MobileGL/MG_Test/Program/ProgramUtilTest.cpp index da2d5506..6de004e3 100644 --- a/MobileGL/MG_Test/Program/ProgramUtilTest.cpp +++ b/MobileGL/MG_Test/Program/ProgramUtilTest.cpp @@ -133,7 +133,7 @@ TEST_F(ProgramUtilTest, CompileFragmentShaderWithDiscard) { } ProgramAttrib programAttrib { - .shaderTypes = { GL_FRAGMENT_SHADER }, + // .shaderTypes = { GL_FRAGMENT_SHADER }, .shaders = { res.value() } }; @@ -159,7 +159,7 @@ TEST_F(ProgramUtilTest, CompileFragmentShaderWithDiscard) { } for (SizeT i = 0; i < spirvs.size(); ++i) { - std::cout << "Decompiling " << MG_Util::ConvertGLEnumToString(programAttrib.shaderTypes[i]) << std::endl; + std::cout << "Decompiling " << MG_Util::ConvertGLEnumToString(binaryAttrib.shaderTypes[i]) << std::endl; auto src = ShaderCompiler::DecompileShader(sessions[i]); if (!src) { ASSERT_NE(src.error().errc, 0); @@ -245,7 +245,7 @@ TEST_F(ProgramUtilTest, CompileAndLinkProgram) { } ProgramAttrib programAttrib { - .shaderTypes = { GL_VERTEX_SHADER, GL_FRAGMENT_SHADER }, + // .shaderTypes = { GL_VERTEX_SHADER, GL_FRAGMENT_SHADER }, .shaders = { vs_res.value(), fs_res.value() } }; @@ -279,7 +279,7 @@ TEST_F(ProgramUtilTest, DecompProgram) { } ProgramAttrib programAttrib { - .shaderTypes = { GL_VERTEX_SHADER, GL_FRAGMENT_SHADER }, + // .shaderTypes = { GL_VERTEX_SHADER, GL_FRAGMENT_SHADER }, .shaders = { vs_res.value(), fs_res.value() } }; @@ -303,7 +303,7 @@ TEST_F(ProgramUtilTest, DecompProgram) { } for (SizeT i = 0; i < spirvs.size(); ++i) { - std::cout << "Decompiling " << MG_Util::ConvertGLEnumToString(programAttrib.shaderTypes[i]) << std::endl; + std::cout << "Decompiling " << MG_Util::ConvertGLEnumToString(binaryAttrib.shaderTypes[i]) << std::endl; auto src = ShaderCompiler::DecompileShader(sessions[i]); if (!src) { ASSERT_NE(src.error().errc, 0); diff --git a/MobileGL/MG_Util/ShaderTranspiler/Types.h b/MobileGL/MG_Util/ShaderTranspiler/Types.h index c4c90ef1..922fe59e 100644 --- a/MobileGL/MG_Util/ShaderTranspiler/Types.h +++ b/MobileGL/MG_Util/ShaderTranspiler/Types.h @@ -20,7 +20,7 @@ namespace MobileGL { }; struct ProgramAttrib { - Vector shaderTypes; + // Vector shaderTypes; Vector> shaders; }; From cb59bc043166d2dd4ba9c4b234662c65c5e1ad61 Mon Sep 17 00:00:00 2001 From: Swung0x48 Date: Mon, 18 Aug 2025 11:07:33 +0800 Subject: [PATCH 08/12] [Feat] (MG_State/Program): generate program binary (SPIR-V), reflect generated ubo size/offsets --- .../MG_Impl/GLImpl/Program/GL_Program.cpp | 2 +- .../GLState/ProgramState/ProgramObject.cpp | 52 +++++++++++++++++++ .../GLState/ProgramState/ProgramObject.h | 5 +- 3 files changed, 57 insertions(+), 2 deletions(-) diff --git a/MobileGL/MG_Impl/GLImpl/Program/GL_Program.cpp b/MobileGL/MG_Impl/GLImpl/Program/GL_Program.cpp index 14242442..35089fc5 100644 --- a/MobileGL/MG_Impl/GLImpl/Program/GL_Program.cpp +++ b/MobileGL/MG_Impl/GLImpl/Program/GL_Program.cpp @@ -269,7 +269,7 @@ namespace MobileGL { *params = programObject->GetActiveUniformBlocksCount(); break; case GL_ACTIVE_UNIFORM_BLOCK_MAX_NAME_LENGTH: // ditto. - *params = programObject->GetActiveUniformBlocksMaxLength(); + *params = programObject->GetActiveUniformBlocksMaxNameLength(); break; case GL_COMPUTE_WORK_GROUP_SIZE: // GL >= 4.3 diff --git a/MobileGL/MG_State/GLState/ProgramState/ProgramObject.cpp b/MobileGL/MG_State/GLState/ProgramState/ProgramObject.cpp index 3e40609f..27f3ea96 100644 --- a/MobileGL/MG_State/GLState/ProgramState/ProgramObject.cpp +++ b/MobileGL/MG_State/GLState/ProgramState/ProgramObject.cpp @@ -52,6 +52,7 @@ namespace MobileGL { // PostLink(); DoReflection(); + GenerateBinary(); } void ProgramObject::MarkAsDeleted() { @@ -119,6 +120,57 @@ namespace MobileGL { } } + void ProgramObject::GenerateBinary() { + /* As we passed first stage compilation/linking, + * we'll assume all the operations here should + * pass. We may be able to employ some optimizations + * here without the burden of error reporting. + */ + using namespace MG_Util::ShaderTranspiler; + Vector> shaders(m_shaders.size()); + Vector shaderTypes(m_shaders.size()); + for (SizeT i = 0; i < m_shaders.size(); i++) { + auto shaderType = ConvertGLShaderTypeByMGLShaderStage(m_shaders[i]->GetShaderStage()); + shaderTypes[i] = shaderType; + ShaderAttrib attrib { + .shaderType = shaderType, + .sourceStr = m_shaders[i]->GetShaderSource(), + .flags = 0 + }; + auto res = ShaderCompiler::CompileShader(attrib); + assert(res); + shaders[i] = res.value(); + } + + ProgramAttrib attrib { + .shaders = Move(shaders), + }; + auto programResult = ShaderCompiler::LinkProgram(attrib); + assert(programResult); + auto program = programResult.value(); + + ProgramBinaryAttrib binaryAttrib { + .shaderTypes = shaderTypes, + .program = *program, + }; + auto binaryResult = ShaderCompiler::GetSpirvBinaryFromProgram(binaryAttrib); + assert(binaryResult); + m_generatedSpirv = Move(binaryResult.value()); + SpvcSession session(m_generatedSpirv[0]); + auto& meta = session.GetMetadata(); + auto size = meta.uboSize; + m_uboScratch.resize(size); + m_uniformOffsets.resize(meta.plainUniformOffsetsInUBO.size()); + for (const auto& [name, offset] : meta.plainUniformOffsetsInUBO) { + m_uniformOffsets[m_uniformLocations[name]] = offset; + } + + auto srcResult = ShaderCompiler::DecompileShader(session); + assert(srcResult); + auto src = srcResult.value(); + printf("decompiled src: \n%s", src.c_str()); + } + void ProgramObject::SetExplicitAttribLocation(Uint index, const char *name) { m_explicitAttribLocations[name] = index; } diff --git a/MobileGL/MG_State/GLState/ProgramState/ProgramObject.h b/MobileGL/MG_State/GLState/ProgramState/ProgramObject.h index 31a1d920..932a229e 100644 --- a/MobileGL/MG_State/GLState/ProgramState/ProgramObject.h +++ b/MobileGL/MG_State/GLState/ProgramState/ProgramObject.h @@ -45,9 +45,10 @@ namespace MobileGL { Int GetActiveAttributesCount() const { return m_program->getNumPipeInputs(); } Int GetActiveUniformBlocksCount() const { return m_program->getNumUniformBlocks(); } Int GetActiveAttributesMaxLength() const { return m_attribInNameMaxLength; } - Int GetActiveUniformBlocksMaxLength() const { return m_uniformBlockNameMaxLength; } + Int GetActiveUniformBlocksMaxNameLength() const { return m_uniformBlockNameMaxLength; } private: void DoReflection(); + void GenerateBinary(); // void PreLink(); // void PostLink(); @@ -56,6 +57,8 @@ namespace MobileGL { SharedPtr m_program; + Vector> m_generatedSpirv; + // Attributes UnorderedMap m_explicitAttribLocations; Vector m_attribs; From 2617903e71aa01e59ad05106f52a74558e957f37 Mon Sep 17 00:00:00 2001 From: Swung0x48 Date: Mon, 18 Aug 2025 14:30:17 +0800 Subject: [PATCH 09/12] [Feat] (MG_State/Program): GetActiveAttrib --- .../MG_Impl/GLImpl/Program/GL_Program.cpp | 24 ++++++++++++++++++- .../GLState/ProgramState/ProgramObject.cpp | 6 +++++ .../GLState/ProgramState/ProgramObject.h | 8 +++++++ 3 files changed, 37 insertions(+), 1 deletion(-) diff --git a/MobileGL/MG_Impl/GLImpl/Program/GL_Program.cpp b/MobileGL/MG_Impl/GLImpl/Program/GL_Program.cpp index 35089fc5..3e1f3fe8 100644 --- a/MobileGL/MG_Impl/GLImpl/Program/GL_Program.cpp +++ b/MobileGL/MG_Impl/GLImpl/Program/GL_Program.cpp @@ -163,7 +163,29 @@ namespace MobileGL { void GetActiveAttrib_State(GLuint program, GLuint index, GLsizei bufSize, GLsizei* length, GLint* size, GLenum* type, GLchar* name) { - THROW_UNIMPL_EXCEPTION; + if (bufSize < 0) { + MG_State::pGLContext->RecordError( + ErrorCode::InvalidValue, + MakeShared("MG_Impl/GLImpl", __func__, + "`bufSize` is less than 0.")); + return; + } + auto programObject = TryToGetProgramObject(program); + if (!programObject) + return; + auto attribCount = programObject->GetActiveAttributesCount(); + if (index >= attribCount) { + MG_State::pGLContext->RecordError( + ErrorCode::InvalidValue, + MakeShared("MG_Impl/GLImpl", __func__, + "`index` is greater than or equal to the number of active attribute variables in `program`.")); + return; + } + if (type != nullptr) + *type = programObject->GetAttribType(index); + if (bufSize == 0) return; + auto& attribName = programObject->GetAttribName(index); + CopyStr(bufSize, length, name, attribName.c_str(), attribName.length()); } void GetActiveUniform_State(GLuint program, GLuint index, GLsizei bufSize, GLsizei* length, GLint* size, diff --git a/MobileGL/MG_State/GLState/ProgramState/ProgramObject.cpp b/MobileGL/MG_State/GLState/ProgramState/ProgramObject.cpp index 27f3ea96..7bbd4c94 100644 --- a/MobileGL/MG_State/GLState/ProgramState/ProgramObject.cpp +++ b/MobileGL/MG_State/GLState/ProgramState/ProgramObject.cpp @@ -93,6 +93,7 @@ namespace MobileGL { // attributes (pipe in) int inCount = m_program->getNumPipeInputs(); m_attribs.resize(inCount); + m_attribTypes.resize(inCount); // Get locations parsed in program for (int i = 0; i < inCount; i++) { @@ -100,6 +101,7 @@ namespace MobileGL { auto location = inVar.layoutLocation(); m_attribInNameMaxLength = std::max(m_attribInNameMaxLength, (Int)inVar.name.length()); m_attribs[location] = inVar.name; + m_attribTypes[location] = inVar.glDefineType; } // Implement glBindAttribLocation semantics for (auto& [name, location]: m_explicitAttribLocations) { @@ -109,6 +111,7 @@ namespace MobileGL { if (it == m_attribs.end()) continue; std::swap(m_attribs[location], m_attribs[std::distance(m_attribs.begin(), it)]); + std::swap(m_attribTypes[location], m_attribTypes[std::distance(m_attribs.begin(), it)]); } } @@ -171,6 +174,9 @@ namespace MobileGL { printf("decompiled src: \n%s", src.c_str()); } + void ProgramObject::WaitUntilGenerationCompleted() { + } + void ProgramObject::SetExplicitAttribLocation(Uint index, const char *name) { m_explicitAttribLocations[name] = index; } diff --git a/MobileGL/MG_State/GLState/ProgramState/ProgramObject.h b/MobileGL/MG_State/GLState/ProgramState/ProgramObject.h index 932a229e..5357d166 100644 --- a/MobileGL/MG_State/GLState/ProgramState/ProgramObject.h +++ b/MobileGL/MG_State/GLState/ProgramState/ProgramObject.h @@ -37,6 +37,12 @@ namespace MobileGL { const auto it = std::find(m_attribs.begin(), m_attribs.end(), name); return (it == m_attribs.end()) ? -1 : std::distance(m_attribs.begin(), it); } + GLenum GetAttribType(Uint index) const { + return m_attribTypes[index]; + } + const String& GetAttribName(Uint index) const { + return m_attribs[index]; + } Bool GetDeleteStatus() const { return m_deleteStatus; } Bool GetLinkStatus() const { return m_linkStatus; } @@ -49,6 +55,7 @@ namespace MobileGL { private: void DoReflection(); void GenerateBinary(); + void WaitUntilGenerationCompleted(); // void PreLink(); // void PostLink(); @@ -62,6 +69,7 @@ namespace MobileGL { // Attributes UnorderedMap m_explicitAttribLocations; Vector m_attribs; + Vector m_attribTypes; // Uniforms // MG_Util::ShaderTranspiler::SpvcMetadata m_metadata; From 476de2b8f784c26f2dd09b2239f9f4bd79e23391 Mon Sep 17 00:00:00 2001 From: Swung0x48 Date: Thu, 21 Aug 2025 22:32:17 +0800 Subject: [PATCH 10/12] [Feat] (MG_State/Program): Get current program --- .../MG_Impl/GLImpl/Program/GL_Program.cpp | 36 +++++++++++++++++-- MobileGL/MG_State/GLState/Core.cpp | 4 +++ MobileGL/MG_State/GLState/Core.h | 2 +- .../GLState/ProgramState/ProgramObject.cpp | 20 ++++++++--- .../GLState/ProgramState/ProgramObject.h | 18 +++++++++- .../GLState/ProgramState/ProgramState.h | 4 +++ 6 files changed, 75 insertions(+), 9 deletions(-) diff --git a/MobileGL/MG_Impl/GLImpl/Program/GL_Program.cpp b/MobileGL/MG_Impl/GLImpl/Program/GL_Program.cpp index 3e1f3fe8..b33c49eb 100644 --- a/MobileGL/MG_Impl/GLImpl/Program/GL_Program.cpp +++ b/MobileGL/MG_Impl/GLImpl/Program/GL_Program.cpp @@ -383,12 +383,43 @@ namespace MobileGL { return programObject->GetUniformLocation(name); } + void GetUniform_State(GLuint program, GLint location, void* params) { + auto programObject = TryToGetProgramObject(program); + if (!programObject) + return; + + if (!programObject->GetLinkStatus()) { + MG_State::pGLContext->RecordError( + ErrorCode::InvalidOperation, + MakeShared("MG_Impl/GLImpl", __func__, + "`program` has not been successfully linked.")); + return; + } + + if (location >= programObject->GetUniformCount() || programObject->GetUniformName(location).empty()) { + MG_State::pGLContext->RecordError( + ErrorCode::InvalidOperation, + MakeShared("MG_Impl/GLImpl", __func__, + "`location` does not correspond to a valid uniform variable location for the specified program object.")); + return; + } + auto isOpaque = programObject->IsUniformOpaqueAtLocation(location); + if (!isOpaque) { + // TODO: probably handle int/float differences + auto offset = programObject->GetUniformOffset(location); + auto size = programObject->GetUniformSizesInBytes(location); + char* pUBO = (char*)programObject->MapUBO(); + memcpy(params, pUBO + offset, size); + } + // TODO: handle 1i variant as texture unit + } + void GetUniformfv_State(GLuint program, GLint location, GLfloat* params) { - THROW_UNIMPL_EXCEPTION; + GetUniform_State(program, location, params); } void GetUniformiv_State(GLuint program, GLint location, GLint* params) { - THROW_UNIMPL_EXCEPTION; + GetUniform_State(program, location, params); } GLboolean IsProgram_State(GLuint program) { @@ -447,6 +478,7 @@ namespace MobileGL { } void Uniform1fv_State(GLint location, GLsizei count, const GLfloat* value) { + THROW_UNIMPL_EXCEPTION; } diff --git a/MobileGL/MG_State/GLState/Core.cpp b/MobileGL/MG_State/GLState/Core.cpp index 57f35fad..a00fe537 100644 --- a/MobileGL/MG_State/GLState/Core.cpp +++ b/MobileGL/MG_State/GLState/Core.cpp @@ -191,6 +191,10 @@ namespace MobileGL { void GLContext::UseProgram(Uint program) { return m_programState.UseProgram(program); } + + SharedPtr GLContext::GetCurrentProgram() { + return m_programState.GetCurrentProgram(); + } } // namespace GLState GLState::GLContext* pGLContext; diff --git a/MobileGL/MG_State/GLState/Core.h b/MobileGL/MG_State/GLState/Core.h index 3efc751b..25ccea97 100644 --- a/MobileGL/MG_State/GLState/Core.h +++ b/MobileGL/MG_State/GLState/Core.h @@ -64,7 +64,7 @@ namespace MobileGL { SharedPtr GetProgramObject(Uint index); SharedPtr GetShaderObject(Uint index); void UseProgram(Uint program); - + SharedPtr GetCurrentProgram(); private: // Error ErrorState m_errorState; diff --git a/MobileGL/MG_State/GLState/ProgramState/ProgramObject.cpp b/MobileGL/MG_State/GLState/ProgramState/ProgramObject.cpp index 7bbd4c94..d1da8fef 100644 --- a/MobileGL/MG_State/GLState/ProgramState/ProgramObject.cpp +++ b/MobileGL/MG_State/GLState/ProgramState/ProgramObject.cpp @@ -81,13 +81,17 @@ namespace MobileGL { m_uniformNames.resize(m_maxUniformLocation + 1); m_uniformTypes.resize(m_maxUniformLocation + 1); + m_uniformIsOpaqueType.resize(m_maxUniformLocation + 1); m_uniformOffsets.resize(m_maxUniformLocation + 1); + m_uniformArraySizes.resize(m_maxUniformLocation + 1); for (int i = 0; i < uniformCount; i++) { auto& uniform = m_program->getUniform(i); auto location = uniform.layoutLocation(); m_uniformNames[location] = uniform.name; m_uniformTypes[location] = uniform.glDefineType; + m_uniformIsOpaqueType[location] = uniform.getType()->isOpaque(); + m_uniformArraySizes[location] = uniform.size; } // attributes (pipe in) @@ -159,7 +163,12 @@ namespace MobileGL { auto binaryResult = ShaderCompiler::GetSpirvBinaryFromProgram(binaryAttrib); assert(binaryResult); m_generatedSpirv = Move(binaryResult.value()); + SpvcSession session(m_generatedSpirv[0]); + auto srcResult = ShaderCompiler::DecompileShader(session); + assert(srcResult); + auto src = srcResult.value(); + auto& meta = session.GetMetadata(); auto size = meta.uboSize; m_uboScratch.resize(size); @@ -167,11 +176,12 @@ namespace MobileGL { for (const auto& [name, offset] : meta.plainUniformOffsetsInUBO) { m_uniformOffsets[m_uniformLocations[name]] = offset; } - - auto srcResult = ShaderCompiler::DecompileShader(session); - assert(srcResult); - auto src = srcResult.value(); - printf("decompiled src: \n%s", src.c_str()); + m_uniformSizesInBytes.resize(meta.plainUniformMemberSizesInBytes.size()); + for (const auto& [name, size] : meta.plainUniformMemberSizesInBytes) { + m_uniformSizesInBytes[m_uniformLocations[name]] = size; + } + assert(m_uniformOffsets.size() == GetUniformCount()); + assert(m_uniformSizesInBytes.size() == GetUniformCount()); } void ProgramObject::WaitUntilGenerationCompleted() { diff --git a/MobileGL/MG_State/GLState/ProgramState/ProgramObject.h b/MobileGL/MG_State/GLState/ProgramState/ProgramObject.h index 5357d166..dec3b366 100644 --- a/MobileGL/MG_State/GLState/ProgramState/ProgramObject.h +++ b/MobileGL/MG_State/GLState/ProgramState/ProgramObject.h @@ -23,15 +23,25 @@ namespace MobileGL { Uint GetUniformCount() { return m_uniformNames.size(); } Int GetUniformLocation(const String& name) { const auto it = m_uniformLocations.find(name); - return (it == m_uniformLocations.end()) ? -1 : it->second; + return (it == m_uniformLocations.end()) ? -1 : (Int)it->second; } GLenum GetUniformType(Uint index) const { return m_uniformTypes[index]; } + Bool IsUniformOpaqueAtLocation(Uint location) const { + return m_uniformIsOpaqueType[location]; + } + const String& GetUniformName(Uint index) const { return m_uniformNames[index]; } + Uint GetUniformOffset(Uint location) const { + return m_uniformOffsets[location]; + } + Uint GetUniformSizesInBytes(Uint location) const { + return m_uniformSizesInBytes[location]; + } Int GetAttributeLocation(const String& name) { const auto it = std::find(m_attribs.begin(), m_attribs.end(), name); @@ -43,6 +53,9 @@ namespace MobileGL { const String& GetAttribName(Uint index) const { return m_attribs[index]; } + void* MapUBO() { + return m_uboScratch.data(); + } Bool GetDeleteStatus() const { return m_deleteStatus; } Bool GetLinkStatus() const { return m_linkStatus; } @@ -80,9 +93,12 @@ namespace MobileGL { Vector m_uniformNames; // ditto. Vector m_uniformTypes; + Vector m_uniformIsOpaqueType; + Vector m_uniformArraySizes; // Need to be reflected after linking of SPIR-V binary Vector m_uniformOffsets; + Vector m_uniformSizesInBytes; Vector m_uboScratch; Uint m_maxUniformLocation = 0; diff --git a/MobileGL/MG_State/GLState/ProgramState/ProgramState.h b/MobileGL/MG_State/GLState/ProgramState/ProgramState.h index ff981cb1..068792f3 100644 --- a/MobileGL/MG_State/GLState/ProgramState/ProgramState.h +++ b/MobileGL/MG_State/GLState/ProgramState/ProgramState.h @@ -21,6 +21,10 @@ namespace MobileGL { SharedPtr GetShaderObject(Uint shader); void MarkShaderObjectForDeletion(Uint shader); Bool ValidateShaderObject(Uint shader) const; + + SharedPtr GetCurrentProgram() const { + return m_currentProgram; + } private: template static Bool CheckIndexAvail(const SizeT idx, const Vector& vec) { From bc46cb9c5f92d6dd3c28ac407a9f75acca4d17a4 Mon Sep 17 00:00:00 2001 From: Swung0x48 Date: Fri, 22 Aug 2025 14:58:24 +0800 Subject: [PATCH 11/12] [Feat] (MG_State/Program): glUniform*v --- .../MG_Impl/GLImpl/Program/GL_Program.cpp | 55 +++++++++++++++---- .../GLState/ProgramState/ProgramObject.cpp | 10 ++-- MobileGL/MG_Test/Program/ProgramTest.cpp | 50 +++++++++++++---- 3 files changed, 91 insertions(+), 24 deletions(-) diff --git a/MobileGL/MG_Impl/GLImpl/Program/GL_Program.cpp b/MobileGL/MG_Impl/GLImpl/Program/GL_Program.cpp index b33c49eb..9708004f 100644 --- a/MobileGL/MG_Impl/GLImpl/Program/GL_Program.cpp +++ b/MobileGL/MG_Impl/GLImpl/Program/GL_Program.cpp @@ -451,6 +451,7 @@ namespace MobileGL { ErrorCode::InvalidValue, MakeShared("MG_Impl/GLImpl", __func__, "`count` is less than 0.")); + return; } auto shaderObject = TryToGetShaderObject(shader); @@ -465,7 +466,7 @@ namespace MobileGL { shaderObject->SetShaderSource(Move(src)); } - void UseProgram_State(GLuint program) { + void UseProgram_State(GLint program) { if (program == 0) { MG_State::pGLContext->UseProgram(0); return; @@ -477,37 +478,71 @@ namespace MobileGL { MG_State::pGLContext->UseProgram(program); } - void Uniform1fv_State(GLint location, GLsizei count, const GLfloat* value) { + template + void Uniform_State(MG_State::GLState::ProgramObject& programObject, GLuint location, T* value) { + auto size = programObject.GetUniformSizesInBytes(location); + auto offset = programObject.GetUniformOffset(location); + assert(size >= VecCount * sizeof(T)); + memcpy((char*)programObject.MapUBO() + offset, value, VecCount * sizeof(T)); + } - THROW_UNIMPL_EXCEPTION; + template + void Uniformv_State(GLint location, GLsizei count, T* value) { + if (location == -1) + return; + + auto programObject = MG_State::pGLContext->GetCurrentProgram(); + if (programObject == nullptr) { + MG_State::pGLContext->RecordError( + ErrorCode::InvalidOperation, + MakeShared("MG_Impl/GLImpl", __func__, + "There is no current program object.")); + return; + } + + if (location >= programObject->GetUniformCount() || location < -1) { + MG_State::pGLContext->RecordError( + ErrorCode::InvalidOperation, + MakeShared("MG_Impl/GLImpl", __func__, + "`location` is an invalid uniform location for the current program object and `location` is not equal to -1.")); + return; + } + + for (GLint offset = 0; offset < count; offset++) { + Uniform_State(*programObject, location + offset, value + offset * VecCount); + } + } + + void Uniform1fv_State(GLint location, GLsizei count, const GLfloat* value) { + Uniformv_State<1>(location, count, value); } void Uniform2fv_State(GLint location, GLsizei count, const GLfloat* value) { - THROW_UNIMPL_EXCEPTION; + Uniformv_State<2>(location, count, value); } void Uniform3fv_State(GLint location, GLsizei count, const GLfloat* value) { - THROW_UNIMPL_EXCEPTION; + Uniformv_State<3>(location, count, value); } void Uniform4fv_State(GLint location, GLsizei count, const GLfloat* value) { - THROW_UNIMPL_EXCEPTION; + Uniformv_State<4>(location, count, value); } void Uniform1iv_State(GLint location, GLsizei count, const GLint* value) { - THROW_UNIMPL_EXCEPTION; + Uniformv_State<1>(location, count, value); } void Uniform2iv_State(GLint location, GLsizei count, const GLint* value) { - THROW_UNIMPL_EXCEPTION; + Uniformv_State<2>(location, count, value); } void Uniform3iv_State(GLint location, GLsizei count, const GLint* value) { - THROW_UNIMPL_EXCEPTION; + Uniformv_State<3>(location, count, value); } void Uniform4iv_State(GLint location, GLsizei count, const GLint* value) { - THROW_UNIMPL_EXCEPTION; + Uniformv_State<4>(location, count, value); } void UniformMatrix2fv_State(GLint location, GLsizei count, GLboolean transpose, const GLfloat* value) { diff --git a/MobileGL/MG_State/GLState/ProgramState/ProgramObject.cpp b/MobileGL/MG_State/GLState/ProgramState/ProgramObject.cpp index d1da8fef..9278ba28 100644 --- a/MobileGL/MG_State/GLState/ProgramState/ProgramObject.cpp +++ b/MobileGL/MG_State/GLState/ProgramState/ProgramObject.cpp @@ -174,14 +174,16 @@ namespace MobileGL { m_uboScratch.resize(size); m_uniformOffsets.resize(meta.plainUniformOffsetsInUBO.size()); for (const auto& [name, offset] : meta.plainUniformOffsetsInUBO) { - m_uniformOffsets[m_uniformLocations[name]] = offset; + if (m_uniformLocations.find(name) != m_uniformLocations.end()) + m_uniformOffsets[m_uniformLocations[name]] = offset; } m_uniformSizesInBytes.resize(meta.plainUniformMemberSizesInBytes.size()); for (const auto& [name, size] : meta.plainUniformMemberSizesInBytes) { - m_uniformSizesInBytes[m_uniformLocations[name]] = size; + if (m_uniformLocations.find(name) != m_uniformLocations.end()) + m_uniformSizesInBytes[m_uniformLocations[name]] = size; } - assert(m_uniformOffsets.size() == GetUniformCount()); - assert(m_uniformSizesInBytes.size() == GetUniformCount()); + // assert(m_uniformOffsets.size() == GetUniformCount()); + // assert(m_uniformSizesInBytes.size() == GetUniformCount()); } void ProgramObject::WaitUntilGenerationCompleted() { diff --git a/MobileGL/MG_Test/Program/ProgramTest.cpp b/MobileGL/MG_Test/Program/ProgramTest.cpp index 0449d560..52120260 100644 --- a/MobileGL/MG_Test/Program/ProgramTest.cpp +++ b/MobileGL/MG_Test/Program/ProgramTest.cpp @@ -64,6 +64,7 @@ uniform vec3 Offset; uniform vec3 ColorScale; layout(location = 6) uniform float Saturation; uniform int AQuickFoxJumpsOverALazyDog; +uniform int intVal; out vec4 fragColor; @@ -84,7 +85,7 @@ void main() { vec3 Chroma = OutColor - Luma; OutColor = (Chroma * Saturation) + Luma; - fragColor = vec4(OutColor, 1.0); + fragColor = vec4(OutColor, float(intVal)); })"; @@ -101,16 +102,26 @@ TEST_F(ProgramTest, CompileFragment) { } TEST_F(ProgramTest, CompileAndLink) { + char infoLog[1024] = ""; + GLuint vs = CreateShader(GL_VERTEX_SHADER); ShaderSource(vs, 1, &vsSrc, NULL); printf("Compiling vertex shader: %s\n", vsSrc); CompileShader(vs); + GLint vsStatus = GL_FALSE; + GetShaderiv(vs, GL_COMPILE_STATUS, &vsStatus); + GetShaderInfoLog(vs, 1024, nullptr, infoLog); + ASSERT_EQ(vsStatus, GL_TRUE) << infoLog; printf("Compiled vertex shader.\n"); GLuint fs = CreateShader(GL_FRAGMENT_SHADER); ShaderSource(fs, 1, &fsSrc, NULL); printf("Compiling fragment shader: %s\n", fsSrc); CompileShader(fs); + GLint fsStatus = GL_FALSE; + GetShaderiv(fs, GL_COMPILE_STATUS, &fsStatus); + GetShaderInfoLog(fs, 1024, nullptr, infoLog); + ASSERT_EQ(fsStatus, GL_TRUE) << infoLog; printf("Compiled fragment shader.\n"); GLuint program = CreateProgram(); @@ -124,18 +135,37 @@ TEST_F(ProgramTest, CompileAndLink) { LinkProgram(program); printf("Program linked.\n"); - EXPECT_EQ(GetUniformLocation(program, "ProjMat"), 0); - EXPECT_EQ(GetUniformLocation(program, "Gray"), 1); - EXPECT_EQ(GetUniformLocation(program, "Saturation"), 6); + ASSERT_EQ(GetUniformLocation(program, "ProjMat"), 0); + ASSERT_EQ(GetUniformLocation(program, "Gray"), 1); + ASSERT_EQ(GetUniformLocation(program, "Saturation"), 6); GLint uniformCount = 0; GetProgramiv(program, GL_ACTIVE_UNIFORMS, &uniformCount); - EXPECT_EQ(uniformCount, 11); + ASSERT_EQ(uniformCount, 12); GLint uniformNameMaxLength = 0; GetProgramiv(program, GL_ACTIVE_UNIFORM_MAX_LENGTH, &uniformNameMaxLength); - EXPECT_EQ(uniformNameMaxLength, 12); + ASSERT_EQ(uniformNameMaxLength, 12); - EXPECT_EQ(GetAttribLocation(program, "Position"), 0); - EXPECT_EQ(GetAttribLocation(program, "fIn1"), 1); - EXPECT_EQ(GetAttribLocation(program, "fIn3"), 3); - EXPECT_EQ(GetAttribLocation(program, "fIn5"), 5); + ASSERT_EQ(GetAttribLocation(program, "Position"), 0); + ASSERT_EQ(GetAttribLocation(program, "fIn1"), 1); + ASSERT_EQ(GetAttribLocation(program, "fIn3"), 3); + ASSERT_EQ(GetAttribLocation(program, "fIn5"), 5); + + UseProgram(program); + + auto locRed = GetUniformLocation(program, "RedMatrix"); + Uniform3f(locRed, 1.0, 3.0, 5.0); + float redVal[3]; + GetUniformfv(program, locRed, redVal); + ASSERT_EQ(redVal[0], 1.0); + ASSERT_EQ(redVal[1], 3.0); + ASSERT_EQ(redVal[2], 5.0); + + auto locAbc = GetUniformLocation(program, "AQuickFoxJumpsOverALazyDog"); + ASSERT_EQ(locAbc, -1); + + auto locInt = GetUniformLocation(program, "intVal"); + Uniform1i(locInt, 114514); + int intVal; + GetUniformiv(program, locInt, &intVal); + EXPECT_EQ(intVal, 114514); } From 2be194c147e733e3e4407cc473efd1706cdb4ae0 Mon Sep 17 00:00:00 2001 From: Swung0x48 Date: Fri, 22 Aug 2025 15:47:14 +0800 Subject: [PATCH 12/12] [Feat] (MG_State/Program): glUniformMatrix* without transpose --- .../MG_Impl/GLImpl/Program/GL_Program.cpp | 96 ++++++++++++++++++- MobileGL/MG_Test/Program/ProgramTest.cpp | 55 +++++++++++ 2 files changed, 148 insertions(+), 3 deletions(-) diff --git a/MobileGL/MG_Impl/GLImpl/Program/GL_Program.cpp b/MobileGL/MG_Impl/GLImpl/Program/GL_Program.cpp index 9708004f..5e389b15 100644 --- a/MobileGL/MG_Impl/GLImpl/Program/GL_Program.cpp +++ b/MobileGL/MG_Impl/GLImpl/Program/GL_Program.cpp @@ -546,15 +546,105 @@ namespace MobileGL { } void UniformMatrix2fv_State(GLint location, GLsizei count, GLboolean transpose, const GLfloat* value) { - THROW_UNIMPL_EXCEPTION; + // For 2x2 matrices, we have 4 elements per matrix + // If transpose is GL_TRUE, we need to transpose the matrix data + if (location == -1) + return; + + auto programObject = MG_State::pGLContext->GetCurrentProgram(); + if (programObject == nullptr) { + MG_State::pGLContext->RecordError( + ErrorCode::InvalidOperation, + MakeShared("MG_Impl/GLImpl", __func__, + "There is no current program object.")); + return; + } + + if (location >= programObject->GetUniformCount() || location < -1) { + MG_State::pGLContext->RecordError( + ErrorCode::InvalidOperation, + MakeShared("MG_Impl/GLImpl", __func__, + "`location` is an invalid uniform location for the current program object and `location` is not equal to -1.")); + return; + } + + // For matrix uniforms, we handle each matrix individually + for (GLint i = 0; i < count; i++) { + // Note: In this implementation, we're not actually transposing the matrix data + // as we're directly copying to UBO. The transpose parameter is typically used + // in OpenGL to indicate whether the matrix should be transposed before being + // loaded into the uniform variable. In our case, we assume the shader compiler + // has handled the appropriate matrix layout. + Uniform_State<4>(*programObject, location + i, value + i * 4); + } } void UniformMatrix3fv_State(GLint location, GLsizei count, GLboolean transpose, const GLfloat* value) { - THROW_UNIMPL_EXCEPTION; + // For 3x3 matrices, we have 9 elements per matrix + // If transpose is GL_TRUE, we need to transpose the matrix data + if (location == -1) + return; + + auto programObject = MG_State::pGLContext->GetCurrentProgram(); + if (programObject == nullptr) { + MG_State::pGLContext->RecordError( + ErrorCode::InvalidOperation, + MakeShared("MG_Impl/GLImpl", __func__, + "There is no current program object.")); + return; + } + + if (location >= programObject->GetUniformCount() || location < -1) { + MG_State::pGLContext->RecordError( + ErrorCode::InvalidOperation, + MakeShared("MG_Impl/GLImpl", __func__, + "`location` is an invalid uniform location for the current program object and `location` is not equal to -1.")); + return; + } + + // For matrix uniforms, we handle each matrix individually + for (GLint i = 0; i < count; i++) { + // Note: In this implementation, we're not actually transposing the matrix data + // as we're directly copying to UBO. The transpose parameter is typically used + // in OpenGL to indicate whether the matrix should be transposed before being + // loaded into the uniform variable. In our case, we assume the shader compiler + // has handled the appropriate matrix layout. + Uniform_State<9>(*programObject, location + i, value + i * 9); + } } void UniformMatrix4fv_State(GLint location, GLsizei count, GLboolean transpose, const GLfloat* value) { - THROW_UNIMPL_EXCEPTION; + // For 4x4 matrices, we have 16 elements per matrix + // If transpose is GL_TRUE, we need to transpose the matrix data + if (location == -1) + return; + + auto programObject = MG_State::pGLContext->GetCurrentProgram(); + if (programObject == nullptr) { + MG_State::pGLContext->RecordError( + ErrorCode::InvalidOperation, + MakeShared("MG_Impl/GLImpl", __func__, + "There is no current program object.")); + return; + } + + if (location >= programObject->GetUniformCount() || location < -1) { + MG_State::pGLContext->RecordError( + ErrorCode::InvalidOperation, + MakeShared("MG_Impl/GLImpl", __func__, + "`location` is an invalid uniform location for the current program object and `location` is not equal to -1.")); + return; + } + + // For matrix uniforms, we handle each matrix individually + for (GLint i = 0; i < count; i++) { + // Note: In this implementation, we're not actually transposing the matrix data + // as we're directly copying to UBO. The transpose parameter is typically used + // in OpenGL to indicate whether the matrix should be transposed before being + // loaded into the uniform variable. In our case, we assume the shader compiler + // has handled the appropriate matrix layout. + Uniform_State<16>(*programObject, location + i, value + i * 16); + } } void ValidateProgram_State(GLuint program) { diff --git a/MobileGL/MG_Test/Program/ProgramTest.cpp b/MobileGL/MG_Test/Program/ProgramTest.cpp index 52120260..1a3d6b87 100644 --- a/MobileGL/MG_Test/Program/ProgramTest.cpp +++ b/MobileGL/MG_Test/Program/ProgramTest.cpp @@ -169,3 +169,58 @@ TEST_F(ProgramTest, CompileAndLink) { GetUniformiv(program, locInt, &intVal); EXPECT_EQ(intVal, 114514); } + +TEST_F(ProgramTest, UniformMatrixFunctions) { + char infoLog[1024] = ""; + + GLuint vs = CreateShader(GL_VERTEX_SHADER); + ShaderSource(vs, 1, &vsSrc, NULL); + printf("Compiling vertex shader: %s\n", vsSrc); + CompileShader(vs); + GLint vsStatus = GL_FALSE; + GetShaderiv(vs, GL_COMPILE_STATUS, &vsStatus); + GetShaderInfoLog(vs, 1024, nullptr, infoLog); + ASSERT_EQ(vsStatus, GL_TRUE) << infoLog; + printf("Compiled vertex shader.\n"); + + GLuint fs = CreateShader(GL_FRAGMENT_SHADER); + ShaderSource(fs, 1, &fsSrc, NULL); + printf("Compiling fragment shader: %s\n", fsSrc); + CompileShader(fs); + GLint fsStatus = GL_FALSE; + GetShaderiv(fs, GL_COMPILE_STATUS, &fsStatus); + GetShaderInfoLog(fs, 1024, nullptr, infoLog); + ASSERT_EQ(fsStatus, GL_TRUE) << infoLog; + printf("Compiled fragment shader.\n"); + + GLuint program = CreateProgram(); + AttachShader(program, vs); + AttachShader(program, fs); + + BindAttribLocation(program, 1, "fIn1"); + BindAttribLocation(program, 3, "fIn3"); + BindAttribLocation(program, 5, "fIn5"); + printf("Linking program...\n"); + LinkProgram(program); + printf("Program linked.\n"); + + UseProgram(program); + + // Test UniformMatrix2fv + auto locProjMat = GetUniformLocation(program, "ProjMat"); + ASSERT_NE(locProjMat, -1); + + // 4x4 matrix (16 elements) + GLfloat matrix4x4[16] = { + 1.0f, 0.0f, 0.0f, 0.0f, + 0.0f, 1.0f, 0.0f, 0.0f, + 0.0f, 0.0f, 1.0f, 0.0f, + 0.0f, 0.0f, 0.0f, 1.0f + }; + + // Test UniformMatrix4fv with count = 1 + UniformMatrix4fv(locProjMat, 1, GL_FALSE, matrix4x4); + + // Test with multiple matrices (count > 1) + // For this test, we would need uniforms that are arrays of matrices +}