diff --git a/MG/MG_GL/State/Buffer/BufferState.cpp b/MG/MG_GL/State/Buffer/BufferState.cpp index b13a1988..b85a7ad5 100644 --- a/MG/MG_GL/State/Buffer/BufferState.cpp +++ b/MG/MG_GL/State/Buffer/BufferState.cpp @@ -219,7 +219,8 @@ bool BufferState::ValidateGeneratedName(GLuint buffer) { void BufferState::Delete(GLuint buffer) { buffers_.erase(buffer); if (ValidateGeneratedName(buffer)) - freeId_.emplace_back(buffer); + // TODO: Prevent the object that uses a freeId from conflicting with legacy object in the backend. + //freeId_.emplace_back(buffer); for (auto& [target, id] : currentBindings_) { if (id == buffer) id = 0; } diff --git a/MG/MG_GL/State/Framebuffer/FramebufferState.cpp b/MG/MG_GL/State/Framebuffer/FramebufferState.cpp index 479813b8..14a3280c 100644 --- a/MG/MG_GL/State/Framebuffer/FramebufferState.cpp +++ b/MG/MG_GL/State/Framebuffer/FramebufferState.cpp @@ -32,7 +32,8 @@ GLenum FramebufferState::Delete(GLuint framebuffer) { if (framebuffer == 0) return GL_INVALID_VALUE; if (framebuffers_.erase(framebuffer)) { - freeIds_.insert(framebuffer); + // TODO: Prevent the object that uses a freeId from conflicting with legacy object in the backend. + //freeIds_.insert(framebuffer); for (auto& [target, id] : currentBindings_) { if (id == framebuffer) id = 0; } diff --git a/MG/MG_GL/State/Program/ProgramState.cpp b/MG/MG_GL/State/Program/ProgramState.cpp index 9a01da02..fed6fb58 100644 --- a/MG/MG_GL/State/Program/ProgramState.cpp +++ b/MG/MG_GL/State/Program/ProgramState.cpp @@ -48,7 +48,7 @@ GLenum ProgramState::DeleteProgram(GLuint program) { } programs_.erase(program); - freeProgramIds_.insert(program); + //freeProgramIds_.insert(program); if (currentProgram_ == program) currentProgram_ = 0; MG_Util::Debug::LogD("MG_State: Program: DeleteProgram success, id=%u", program); @@ -261,6 +261,7 @@ GLenum ProgramState::SetUniform(GLuint program, GLint location, GLsizei count, c } UniformValue& uniform = prog.uniformValues[uniformName]; + uniform.setData(type, count, value); MG_Util::Debug::LogD("MG_State: Program: SetUniform success: %s set", uniformName.c_str()); return GL_NO_ERROR; @@ -303,7 +304,7 @@ GLenum ProgramState::SetUniformMatrix(GLuint program, GLint location, GLsizei co } UniformValue& uniform = prog.uniformValues[uniformName]; - uniform.setData(matrixType, count * MG_Util::Program::GetMatrixElementCount(matrixType), value); + uniform.setData(matrixType, count, value); MG_Util::Debug::LogD("MG_State: Program: SetUniformMatrix success: %s set", uniformName.c_str()); return GL_NO_ERROR; } @@ -334,15 +335,15 @@ void ProgramState::UpdateUniform##suffix##Vector1(GLint location, GLsizei count, } \ void ProgramState::UpdateUniform##suffix##Vector2(GLint location, GLsizei count, const type* value) { \ MG_Util::Debug::LogD("MG_State: Program: UpdateUniform" #suffix "Vector2 called, location=%d, count=%d", location, count); \ - SetUniform(currentProgram_, location, count * 2, value, vecType); \ + SetUniform(currentProgram_, location, count, value, vecType); \ } \ void ProgramState::UpdateUniform##suffix##Vector3(GLint location, GLsizei count, const type* value) { \ MG_Util::Debug::LogD("MG_State: Program: UpdateUniform" #suffix "Vector3 called, location=%d, count=%d", location, count); \ - SetUniform(currentProgram_, location, count * 3, value, vecType); \ + SetUniform(currentProgram_, location, count, value, vecType); \ } \ void ProgramState::UpdateUniform##suffix##Vector4(GLint location, GLsizei count, const type* value) { \ MG_Util::Debug::LogD("MG_State: Program: UpdateUniform" #suffix "Vector4 called, location=%d, count=%d", location, count); \ - SetUniform(currentProgram_, location, count * 4, value, vecType); \ + SetUniform(currentProgram_, location, count, value, vecType); \ } IMPLEMENT_UNIFORM_FUNCTIONS(GLfloat, Float, GL_FLOAT_VEC4) @@ -462,16 +463,62 @@ void ProgramState::CleanupShaders_() { // UniformValue template -void UniformValue::setData(GLenum uniformType, GLsizei numElements, const T* values) { - MG_Util::Debug::LogD("MG_State: Program: UniformValue::setData called, type=0x%X, count=%d", uniformType, numElements); +void UniformValue::setData(GLenum uniformType, GLsizei count_, const T* values) { + MG_Util::Debug::LogD("MG_State: Program: UniformValue::setData called, type=0x%X, count=%d", uniformType, count_); //type = uniformType; - count = numElements; + count = count_; floatData.clear(); intData.clear(); uintData.clear(); boolData.clear(); + switch (type) { + case GL_FLOAT: numElements = count_ * 1; break; + case GL_FLOAT_VEC2: numElements = count_ * 2; break; + case GL_FLOAT_VEC3: numElements = count_ * 3; break; + case GL_FLOAT_VEC4: numElements = count_ * 4; break; + case GL_INT: numElements = count_ * 1; break; + case GL_INT_VEC2: numElements = count_ * 2; break; + case GL_INT_VEC3: numElements = count_ * 3; break; + case GL_INT_VEC4: numElements = count_ * 4; break; + case GL_UNSIGNED_INT: numElements = count_ * 1; break; + case GL_UNSIGNED_INT_VEC2: numElements = count_ * 2; break; + case GL_UNSIGNED_INT_VEC3: numElements = count_ * 3; break; + case GL_UNSIGNED_INT_VEC4: numElements = count_ * 4; break; + case GL_BOOL: numElements = count_ * 1; break; + case GL_BOOL_VEC2: numElements = count_ * 2; break; + case GL_BOOL_VEC3: numElements = count_ * 3; break; + case GL_BOOL_VEC4: numElements = count_ * 4; break; + case GL_FLOAT_MAT2: numElements = count_ * 4; break; // 2x2 + case GL_FLOAT_MAT3: numElements = count_ * 9; break; // 3x3 + case GL_FLOAT_MAT4: numElements = count_ * 16; break; // 4x4 + case GL_FLOAT_MAT2x3: numElements = count_ * 6; break; // 2x3 + case GL_FLOAT_MAT2x4: numElements = count_ * 8; break; // 2x4 + case GL_FLOAT_MAT3x2: numElements = count_ * 6; break; // 3x2 + case GL_FLOAT_MAT3x4: numElements = count_ * 12; break; // 3x4 + case GL_FLOAT_MAT4x2: numElements = count_ * 8; break; // 4x2 + case GL_FLOAT_MAT4x3: numElements = count_ * 12; break; // 4x3 + case GL_SAMPLER_1D: + case GL_SAMPLER_2D: + case GL_SAMPLER_3D: + case GL_SAMPLER_CUBE: + case GL_SAMPLER_1D_SHADOW: + case GL_SAMPLER_2D_SHADOW: + case GL_SAMPLER_CUBE_SHADOW: + case GL_SAMPLER_1D_ARRAY: + case GL_SAMPLER_2D_ARRAY: + case GL_SAMPLER_1D_ARRAY_SHADOW: + case GL_SAMPLER_2D_ARRAY_SHADOW: + case GL_SAMPLER_BUFFER: + case GL_SAMPLER_2D_MULTISAMPLE: + case GL_SAMPLER_2D_MULTISAMPLE_ARRAY: numElements = count_ * 1; break; + default: + MG_Util::Debug::LogW("MG_State: Program: UniformValue::setData warning: unsupported type 0x%X for numElements", uniformType); + numElements = 1; + return; + } + for (GLsizei i = 0; i < numElements; ++i) { switch (uniformType) { case GL_FLOAT: diff --git a/MG/MG_GL/State/Program/ProgramState.h b/MG/MG_GL/State/Program/ProgramState.h index bbcfe372..9d97802d 100644 --- a/MG/MG_GL/State/Program/ProgramState.h +++ b/MG/MG_GL/State/Program/ProgramState.h @@ -27,16 +27,18 @@ struct UniformValue { std::vector uintData; std::vector boolData; GLsizei count; + GLsizei numElements; UniformValue() : type(0), count(0) {} template - void setData(GLenum uniformType, GLsizei numElements, const T* values); + void setData(GLenum uniformType, GLsizei count_, const T* values); }; struct ProgramObject { std::vector attachedShaders; UncertainBool linked; + bool dirty; GLint linkStatus; std::string infoLog; std::unordered_map attribBindings; diff --git a/MG/MG_GL/State/Program/ShaderObject.cpp b/MG/MG_GL/State/Program/ShaderObject.cpp index 0928a551..a8031905 100644 --- a/MG/MG_GL/State/Program/ShaderObject.cpp +++ b/MG/MG_GL/State/Program/ShaderObject.cpp @@ -60,7 +60,8 @@ GLenum ProgramState::DeleteShader(GLuint shader) { MG_Util::Debug::LogD("MG_State: Program: DeleteShader info: shader %u marked for deferred deletion (in use)", shader); } else { shaders_.erase(shader); - freeShaderIds_.insert(shader); + // TODO: Prevent the object that uses a freeId from conflicting with legacy object in the backend. + //freeShaderIds_.insert(shader); MG_Util::Debug::LogD("MG_State: Program: DeleteShader success: shader %u deleted immediately", shader); } return GL_NO_ERROR; diff --git a/MG/MG_GL/State/Texture/TextureState.cpp b/MG/MG_GL/State/Texture/TextureState.cpp index 78f552f3..6280e1c7 100644 --- a/MG/MG_GL/State/Texture/TextureState.cpp +++ b/MG/MG_GL/State/Texture/TextureState.cpp @@ -610,7 +610,7 @@ GLenum TextureState::Delete(GLuint texture) { if (it != this->textures.end()) { InvalidateTextureInAllUnits_(texture); this->textures.erase(it); - freeID_.emplace_back(texture); + //freeID_.emplace_back(texture); MG_Util::Debug::LogD("MG_State: Texture: Delete succeeded for texture=%u", texture); } else { MG_Util::Debug::LogW("MG_State: Texture: Delete texture %u not found", texture); @@ -632,7 +632,8 @@ GLenum TextureState::DeleteN(GLsizei n, const GLuint* textures) { if (it != this->textures.end()) { InvalidateTextureInAllUnits_(id); this->textures.erase(it); - freeID_.emplace_back(id); + // TODO: Prevent the object that uses a freeId from conflicting with legacy object in the backend. + //freeID_.emplace_back(id); MG_Util::Debug::LogD("MG_State: Texture: DeleteN deleted texture=%u", id); } else { MG_Util::Debug::LogW("MG_State: Texture: DeleteN texture %u not found", id); diff --git a/MG/MG_UTIL/Program/DebugTool.cpp b/MG/MG_UTIL/Program/DebugTool.cpp index 608b9495..e02e123c 100644 --- a/MG/MG_UTIL/Program/DebugTool.cpp +++ b/MG/MG_UTIL/Program/DebugTool.cpp @@ -54,7 +54,7 @@ namespace MG_Util::Program { std::ostringstream ss; ss << "["; - const size_t elemCount = std::min(value.count, 16); // 限制最大显示元素 + const size_t elemCount = std::min(value.count, 16); switch(value.type) { case GL_FLOAT: diff --git a/MG/MG_UTIL/Program/GLSLTool.cpp b/MG/MG_UTIL/Program/GLSLTool.cpp index ad6983b8..47190d86 100644 --- a/MG/MG_UTIL/Program/GLSLTool.cpp +++ b/MG/MG_UTIL/Program/GLSLTool.cpp @@ -395,8 +395,6 @@ namespace MG_Util::Program { prog.uniformLocations[name] = final_location; UniformValue uniformValue; uniformValue.type = gl_type; - uniformValue.count = spvc_type_get_array_dimension(type, 0) > 0 ? - spvc_type_get_array_dimension(type, 0) : 1; prog.uniformValues[name] = uniformValue; } }