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 diff --git a/MobileGL/MG_Impl/GLImpl/Program/GL_Program.cpp b/MobileGL/MG_Impl/GLImpl/Program/GL_Program.cpp index a8ba513c..5e389b15 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) { @@ -143,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, @@ -195,7 +237,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) { @@ -242,7 +291,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 @@ -334,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) { @@ -371,6 +451,7 @@ namespace MobileGL { ErrorCode::InvalidValue, MakeShared("MG_Impl/GLImpl", __func__, "`count` is less than 0.")); + return; } auto shaderObject = TryToGetShaderObject(shader); @@ -385,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; @@ -397,48 +478,173 @@ namespace MobileGL { MG_State::pGLContext->UseProgram(program); } + 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)); + } + + 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) { - THROW_UNIMPL_EXCEPTION; + 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) { - 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_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 f4534ed4..9278ba28 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), }; @@ -52,6 +52,7 @@ namespace MobileGL { // PostLink(); DoReflection(); + GenerateBinary(); } void ProgramObject::MarkAsDeleted() { @@ -80,21 +81,45 @@ 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) 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++) { auto& inVar = m_program->getPipeInput(i); + 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) { + 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)]); + std::swap(m_attribTypes[location], m_attribTypes[std::distance(m_attribs.begin(), it)]); + } } + // UBO int uboCount = m_program->getNumUniformBlocks(); for (int i = 0; i < uboCount; i++) { auto& ubo = m_program->getUniformBlock(i); @@ -102,6 +127,72 @@ 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 srcResult = ShaderCompiler::DecompileShader(session); + assert(srcResult); + auto src = srcResult.value(); + + 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) { + 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) { + if (m_uniformLocations.find(name) != m_uniformLocations.end()) + m_uniformSizesInBytes[m_uniformLocations[name]] = size; + } + // assert(m_uniformOffsets.size() == GetUniformCount()); + // assert(m_uniformSizesInBytes.size() == GetUniformCount()); + } + + void ProgramObject::WaitUntilGenerationCompleted() { + } + + 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..dec3b366 100644 --- a/MobileGL/MG_State/GLState/ProgramState/ProgramObject.h +++ b/MobileGL/MG_State/GLState/ProgramState/ProgramObject.h @@ -14,21 +14,48 @@ 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; } 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); + 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]; + } + void* MapUBO() { + return m_uboScratch.data(); + } Bool GetDeleteStatus() const { return m_deleteStatus; } Bool GetLinkStatus() const { return m_linkStatus; } @@ -37,9 +64,11 @@ 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 WaitUntilGenerationCompleted(); // void PreLink(); // void PostLink(); @@ -48,15 +77,28 @@ namespace MobileGL { SharedPtr m_program; + Vector> m_generatedSpirv; + + // Attributes + UnorderedMap m_explicitAttribLocations; + Vector m_attribs; + Vector m_attribTypes; + // 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; + 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; @@ -66,7 +108,7 @@ namespace MobileGL { String m_infoLog; Bool m_deleteStatus = false; - Bool m_linkStatus = true; + Bool m_linkStatus = false; Bool m_validateStatus = true; }; } // namespace GLState 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) { diff --git a/MobileGL/MG_Test/Program/ProgramTest.cpp b/MobileGL/MG_Test/Program/ProgramTest.cpp index 6e0cd42a..1a3d6b87 100644 --- a/MobileGL/MG_Test/Program/ProgramTest.cpp +++ b/MobileGL/MG_Test/Program/ProgramTest.cpp @@ -23,7 +23,13 @@ TEST_F(ProgramTest, Sanity) { const char* vsSrc = R"(#version 460 -in vec4 Position; +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; @@ -36,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 / InSize; + oneTexel = (1.0 * (fIn1 * fIn2 * fIn3 * fIn4 * fIn5 * fIn6)) / InSize; texCoord = Position.xy / OutSize; })"; @@ -58,6 +64,7 @@ uniform vec3 Offset; uniform vec3 ColorScale; layout(location = 6) uniform float Saturation; uniform int AQuickFoxJumpsOverALazyDog; +uniform int intVal; out vec4 fragColor; @@ -78,7 +85,7 @@ void main() { vec3 Chroma = OutColor - Luma; OutColor = (Chroma * Saturation) + Luma; - fragColor = vec4(OutColor, 1.0); + fragColor = vec4(OutColor, float(intVal)); })"; @@ -95,32 +102,125 @@ 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(); 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"); - 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); + + 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); +} + +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 } 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; };