[Feat] (MG_Impl/Program): use helper functions to simplify the code

This commit is contained in:
2025-08-11 15:40:17 +08:00
parent 2c40177f06
commit 4ec815a383
+66 -54
View File
@@ -4,6 +4,58 @@
namespace MobileGL {
namespace MG_Impl::GLImpl {
static bool CheckShaderNameValidity(Uint shader) {
if (!MG_State::pGLContext->ValidateShaderName(shader)) {
MG_State::pGLContext->RecordError(
ErrorCode::InvalidValue,
MakeShared<GenericErrorInfo>("MG_Impl/GLImpl", __func__,
"`shader` is not a value generated by OpenGL."));
return false;
}
return true;
}
static SharedPtr<MG_State::GLState::ShaderObject> TryToGetShaderObject(Uint shader) {
if (!CheckShaderNameValidity(shader))
return nullptr;
auto shaderObject = MG_State::pGLContext->GetShaderObject(shader);
if (!shaderObject) {
MG_State::pGLContext->RecordError(
ErrorCode::InvalidOperation,
MakeShared<GenericErrorInfo>("MG_Impl/GLImpl", __func__,
"`shader` is not a shader object."));
return nullptr;
}
return shaderObject;
}
static bool CheckProgramNameValidity(GLuint program) {
if (!MG_State::pGLContext->ValidateProgramName(program)) {
MG_State::pGLContext->RecordError(
ErrorCode::InvalidValue,
MakeShared<GenericErrorInfo>("MG_Impl/GLImpl", __func__,
"`program` is not a value generated by OpenGL"));
return false;
}
return true;
}
static SharedPtr<MG_State::GLState::ProgramObject> TryToGetProgramObject(GLuint program) {
if (!CheckProgramNameValidity(program))
return nullptr;
auto programObject = MG_State::pGLContext->GetProgramObject(program);
if (!programObject) {
MG_State::pGLContext->RecordError(
ErrorCode::InvalidOperation,
MakeShared<GenericErrorInfo>("MG_Impl/GLImpl", __func__,
"`program` is not a program object."));
return nullptr;
}
return programObject;
}
void AttachShader_State(GLuint program, GLuint shader) {
THROW_UNIMPL_EXCEPTION;
}
@@ -13,22 +65,9 @@ namespace MobileGL {
}
void CompileShader_State(GLuint shader) {
if (!MG_State::pGLContext->ValidateShaderName(shader)) {
MG_State::pGLContext->RecordError(
ErrorCode::InvalidValue,
MakeShared<GenericErrorInfo>("MG_Impl/GLImpl", __func__,
"`shader` is not a value generated by OpenGL."));
auto shaderObject = TryToGetShaderObject(shader);
if (!shaderObject)
return;
}
auto shaderObject = MG_State::pGLContext->GetShaderObject(shader);
if (!shaderObject) {
MG_State::pGLContext->RecordError(
ErrorCode::InvalidOperation,
MakeShared<GenericErrorInfo>("MG_Impl/GLImpl", __func__,
"`shader` is not a shader object."));
return;
}
shaderObject->Compile();
}
@@ -49,54 +88,24 @@ namespace MobileGL {
}
void DeleteProgram_State(GLuint program) {
if (!MG_State::pGLContext->ValidateProgramName(program)) {
MG_State::pGLContext->RecordError(
ErrorCode::InvalidValue,
MakeShared<GenericErrorInfo>("MG_Impl/GLImpl", __func__,
"`program` is not a value generated by OpenGL"));
if (!CheckProgramNameValidity(program))
return;
}
MG_State::pGLContext->MarkProgramForDeletion(program);
}
void DeleteShader_State(GLuint shader) {
if (!MG_State::pGLContext->ValidateShaderName(shader)) {
MG_State::pGLContext->RecordError(
ErrorCode::InvalidValue,
MakeShared<GenericErrorInfo>("MG_Impl/GLImpl", __func__,
"`shader` is not a value generated by OpenGL"));
if (!CheckProgramNameValidity(shader))
return;
}
MG_State::pGLContext->MarkShaderForDeletion(shader);
}
void DetachShader_State(GLuint program, GLuint shader) {
if (!MG_State::pGLContext->ValidateProgramName(program) ||
!MG_State::pGLContext->ValidateShaderName(shader)) {
MG_State::pGLContext->RecordError(
ErrorCode::InvalidValue,
MakeShared<GenericErrorInfo>("MG_Impl/GLImpl", __func__,
"Either `program` or `shader` is a value that was not generated by OpenGL"));
auto shaderObject = TryToGetShaderObject(shader);
if (!shaderObject)
return;
}
auto programObject = MG_State::pGLContext->GetProgramObject(program);
if (!programObject) {
MG_State::pGLContext->RecordError(
ErrorCode::InvalidOperation,
MakeShared<GenericErrorInfo>("MG_Impl/GLImpl", __func__,
"`program` is not a program object."));
auto programObject = TryToGetProgramObject(program);
if (!programObject)
return;
}
auto shaderObject = MG_State::pGLContext->GetShaderObject(shader);
if (!shaderObject) {
MG_State::pGLContext->RecordError(
ErrorCode::InvalidOperation,
MakeShared<GenericErrorInfo>("MG_Impl/GLImpl", __func__,
"`shader` is not a shader object."));
return;
}
auto count = programObject->DetachShader(shaderObject);
if (count <= 0) {
@@ -163,7 +172,7 @@ namespace MobileGL {
* A program object marked for deletion with glDeleteProgram but still in use as part of current
* rendering state is still considered a program object and glIsProgram will return GL_TRUE.
*/
return MG_State::pGLContext->ValidateProgramName(program);
return CheckProgramNameValidity(program);
}
GLboolean IsShader_State(GLuint shader) {
@@ -171,11 +180,14 @@ namespace MobileGL {
* A shader object marked for deletion with glDeleteShader but still attached to a program object is still
* considered a shader object and glIsShader will return GL_TRUE.
*/
return MG_State::pGLContext->ValidateShaderName(shader);
return CheckShaderNameValidity(shader);
}
void LinkProgram_State(GLuint program) {
THROW_UNIMPL_EXCEPTION;
auto programObject = TryToGetProgramObject(program);
if (!programObject)
return;
programObject->Link();
}
void ShaderSource_State(GLuint shader, GLsizei count, const GLchar* const* string, const GLint* length) {