From 0e7483bdd4024902a1c7842d1d56229c30e59d17 Mon Sep 17 00:00:00 2001 From: BZLZHH Date: Sun, 4 May 2025 02:09:25 +0800 Subject: [PATCH] [Feat] (ProgramState): Implement more Global-GL-State functions. - These functions were implemented: - glBlendFunc - glBlendFuncSeparate - glClear - glClearColor - glClearDepth - glColorMask - glDepthFunc - glDepthMask - glDisable - glEnable - glViewport --- MG/Constants.h | 22 ++++ .../Implementations/GL/Common/GL_Common.cpp | 95 ++++++++++++++-- .../Implementations/GL/Common/GL_Common.h | 10 +- .../GLFuncsDefinitions/GLFuncsDefinitions.cpp | 16 +-- MG/MG_GL/State/Common/CommonState.cpp | 101 ++++++++++++++++++ MG/MG_GL/State/Common/CommonState.h | 40 +++++++ MG/MG_GL/State/Core/GLState.cpp | 48 ++++++++- MG/MG_GL/State/Core/GLState.h | 11 ++ 8 files changed, 326 insertions(+), 17 deletions(-) diff --git a/MG/Constants.h b/MG/Constants.h index 49ec46e2..0e921e8f 100644 --- a/MG/Constants.h +++ b/MG/Constants.h @@ -23,6 +23,28 @@ namespace MG_Constants { inline const int LOG_LEVEL_FATAL = 0x0014; } + namespace Blend { + static const std::unordered_set VALID_FACTORS = { + GL_ZERO, GL_ONE, GL_SRC_COLOR, GL_ONE_MINUS_SRC_COLOR, + GL_DST_COLOR, GL_ONE_MINUS_DST_COLOR, GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA, + GL_DST_ALPHA, GL_ONE_MINUS_DST_ALPHA, GL_CONSTANT_COLOR, GL_ONE_MINUS_CONSTANT_COLOR, + GL_CONSTANT_ALPHA, GL_ONE_MINUS_CONSTANT_ALPHA, GL_SRC_ALPHA_SATURATE + }; // OpenGL 3 + } + + namespace Depth { + static const std::unordered_set VALID_FUNCS = { + GL_NEVER, GL_LESS, GL_EQUAL, GL_LEQUAL, GL_GREATER, GL_NOTEQUAL, GL_GEQUAL, GL_ALWAYS + }; // OpenGL 3 + } + + namespace CommonState { + static const std::unordered_set VALID_CAPS = { + GL_BLEND, GL_DEPTH_TEST, GL_STENCIL_TEST, GL_SCISSOR_TEST, GL_CULL_FACE, + GL_POLYGON_OFFSET_FILL, GL_SAMPLE_ALPHA_TO_COVERAGE, GL_SAMPLE_COVERAGE + }; // OpenGL 3 + } + namespace Framebuffer { static const std::unordered_set VALID_ATTACHMENTS = { GL_COLOR_ATTACHMENT0, GL_DEPTH_ATTACHMENT, diff --git a/MG/MG_GL/Implementations/GL/Common/GL_Common.cpp b/MG/MG_GL/Implementations/GL/Common/GL_Common.cpp index 24a98644..b2389138 100644 --- a/MG/MG_GL/Implementations/GL/Common/GL_Common.cpp +++ b/MG/MG_GL/Implementations/GL/Common/GL_Common.cpp @@ -5,16 +5,99 @@ #include "GL_Common.h" namespace MG_GL::GL { - void ClearDepth(GLdouble depth) { - + void Enable(GLenum cap) { + MG_Util::Debug::LogD("glEnable, cap: %s", MG_Util::Debug::GLEnumToString(cap)); + GLenum result = MG_State::glEnable(cap); + if (result == GL_NO_ERROR) return; + MG_State::SetError(result); + MG_Util::Debug::LogE("Error enabling capability: %s", MG_Util::Debug::GLEnumToString(result)); } - - void ClearColor(GLfloat red, GLfloat green, GLfloat blue, GLfloat alpha) { - + + void Disable(GLenum cap) { + MG_Util::Debug::LogD("glDisable, cap: %s", MG_Util::Debug::GLEnumToString(cap)); + GLenum result = MG_State::glDisable(cap); + if (result == GL_NO_ERROR) return; + MG_State::SetError(result); + MG_Util::Debug::LogE("Error disabling capability: %s", MG_Util::Debug::GLEnumToString(result)); + } + + void BlendFunc(GLenum sfactor, GLenum dfactor) { + MG_Util::Debug::LogD("glBlendFunc, sfactor: %s, dfactor: %s", + MG_Util::Debug::GLEnumToString(sfactor), + MG_Util::Debug::GLEnumToString(dfactor)); + GLenum result = MG_State::glBlendFunc(sfactor, dfactor); + if (result == GL_NO_ERROR) return; + MG_State::SetError(result); + MG_Util::Debug::LogE("Error setting blend func: %s", MG_Util::Debug::GLEnumToString(result)); + } + + void BlendFuncSeparate(GLenum srcRGB, GLenum dstRGB, GLenum srcAlpha, GLenum dstAlpha) { + MG_Util::Debug::LogD("glBlendFuncSeparate, srcRGB: %s, dstRGB: %s, srcAlpha: %s, dstAlpha: %s", + MG_Util::Debug::GLEnumToString(srcRGB), + MG_Util::Debug::GLEnumToString(dstRGB), + MG_Util::Debug::GLEnumToString(srcAlpha), + MG_Util::Debug::GLEnumToString(dstAlpha)); + GLenum result = MG_State::glBlendFuncSeparate(srcRGB, dstRGB, srcAlpha, dstAlpha); + if (result == GL_NO_ERROR) return; + MG_State::SetError(result); + MG_Util::Debug::LogE("Error setting separate blend func: %s", MG_Util::Debug::GLEnumToString(result)); } void Clear(GLbitfield mask) { - + MG_Util::Debug::LogD("glClear, mask: 0x%X", mask); + GLenum result = MG_State::glClear(mask); + if (result == GL_NO_ERROR) return; + MG_State::SetError(result); + MG_Util::Debug::LogE("Error clearing buffers: %s", MG_Util::Debug::GLEnumToString(result)); + } + + void ClearColor(GLfloat red, GLfloat green, GLfloat blue, GLfloat alpha) { + MG_Util::Debug::LogD("glClearColor, rgba: [%.2f, %.2f, %.2f, %.2f]", red, green, blue, alpha); + GLenum result = MG_State::glClearColor(red, green, blue, alpha); + if (result == GL_NO_ERROR) return; + MG_State::SetError(result); + MG_Util::Debug::LogE("Error setting clear color: %s", MG_Util::Debug::GLEnumToString(result)); + } + + void ClearDepth(GLdouble depth) { + MG_Util::Debug::LogD("glClearDepth, depth: %.3f", depth); + GLenum result = MG_State::glClearDepth(depth); + if (result == GL_NO_ERROR) return; + MG_State::SetError(result); + MG_Util::Debug::LogE("Error setting clear depth: %s", MG_Util::Debug::GLEnumToString(result)); + } + + void ColorMask(GLboolean red, GLboolean green, GLboolean blue, GLboolean alpha) { + MG_Util::Debug::LogD("glColorMask, rgba: [%d, %d, %d, %d]", red, green, blue, alpha); + GLenum result = MG_State::glColorMask(red, green, blue, alpha); + if (result == GL_NO_ERROR) return; + MG_State::SetError(result); + MG_Util::Debug::LogE("Error setting color mask: %s", MG_Util::Debug::GLEnumToString(result)); + } + + void DepthFunc(GLenum func) { + MG_Util::Debug::LogD("glDepthFunc, func: %s", MG_Util::Debug::GLEnumToString(func)); + GLenum result = MG_State::glDepthFunc(func); + if (result == GL_NO_ERROR) return; + MG_State::SetError(result); + MG_Util::Debug::LogE("Error setting depth func: %s", MG_Util::Debug::GLEnumToString(result)); + } + + void DepthMask(GLboolean flag) { + MG_Util::Debug::LogD("glDepthMask, flag: %d", flag); + GLenum result = MG_State::glDepthMask(flag); + if (result == GL_NO_ERROR) return; + MG_State::SetError(result); + MG_Util::Debug::LogE("Error setting depth mask: %s", MG_Util::Debug::GLEnumToString(result)); + } + + void Viewport(GLint x, GLint y, GLsizei width, GLsizei height) { + MG_Util::Debug::LogD("glViewport, x: %d, y: %d, width: %d, height: %d", + x, y, width, height); + GLenum result = MG_State::glViewport(x, y, width, height); + if (result == GL_NO_ERROR) return; + MG_State::SetError(result); + MG_Util::Debug::LogE("Error setting viewport: %s", MG_Util::Debug::GLEnumToString(result)); } void PixelStorei(GLenum pname, GLint param) { diff --git a/MG/MG_GL/Implementations/GL/Common/GL_Common.h b/MG/MG_GL/Implementations/GL/Common/GL_Common.h index ed5f58a7..bb0a4e91 100644 --- a/MG/MG_GL/Implementations/GL/Common/GL_Common.h +++ b/MG/MG_GL/Implementations/GL/Common/GL_Common.h @@ -8,10 +8,18 @@ #include "../../../../Includes.h" namespace MG_GL::GL { - void ClearDepth(::GLdouble depth); + void ClearDepth(GLdouble depth); void ClearColor(GLfloat red, GLfloat green, GLfloat blue, GLfloat alpha); void Clear(GLbitfield mask); void PixelStorei(GLenum pname, GLint param); + void Enable(GLenum cap); + void Disable(GLenum cap); + void BlendFunc(GLenum sfactor, GLenum dfactor); + void BlendFuncSeparate(GLenum srcRGB, GLenum dstRGB, GLenum srcAlpha, GLenum dstAlpha); + void ColorMask(GLboolean red, GLboolean green, GLboolean blue, GLboolean alpha); + void DepthFunc(GLenum func); + void DepthMask(GLboolean flag); + void Viewport(GLint x, GLint y, GLsizei width, GLsizei height); } #endif //MOBILEGL_GL_COMMON_H diff --git a/MG/MG_GL/Implementations/GL/GLFuncsDefinitions/GLFuncsDefinitions.cpp b/MG/MG_GL/Implementations/GL/GLFuncsDefinitions/GLFuncsDefinitions.cpp index 1bd623b4..df0e9d10 100644 --- a/MG/MG_GL/Implementations/GL/GLFuncsDefinitions/GLFuncsDefinitions.cpp +++ b/MG/MG_GL/Implementations/GL/GLFuncsDefinitions/GLFuncsDefinitions.cpp @@ -43,8 +43,8 @@ DECLARE_GL_FUNCTION_HEAD(void, BindTexture, GLenum target, GLuint texture) DECLA DECLARE_GL_FUNCTION_STUB_HEAD(void, BlendColor, GLfloat red, GLfloat green, GLfloat blue, GLfloat alpha) DECLARE_GL_FUNCTION_STUB_END_NO_RETURN(void, BlendColor, red,green,blue,alpha) DECLARE_GL_FUNCTION_STUB_HEAD(void, BlendEquation, GLenum mode) DECLARE_GL_FUNCTION_STUB_END_NO_RETURN(void, BlendEquation, mode) DECLARE_GL_FUNCTION_STUB_HEAD(void, BlendEquationSeparate, GLenum modeRGB, GLenum modeAlpha) DECLARE_GL_FUNCTION_STUB_END_NO_RETURN(void, BlendEquationSeparate, modeRGB,modeAlpha) -DECLARE_GL_FUNCTION_STUB_HEAD(void, BlendFunc, GLenum sfactor, GLenum dfactor) DECLARE_GL_FUNCTION_STUB_END_NO_RETURN(void, BlendFunc, sfactor,dfactor) -DECLARE_GL_FUNCTION_STUB_HEAD(void, BlendFuncSeparate, GLenum sfactorRGB, GLenum dfactorRGB, GLenum sfactorAlpha, GLenum dfactorAlpha) DECLARE_GL_FUNCTION_STUB_END_NO_RETURN(void, BlendFuncSeparate, sfactorRGB,dfactorRGB,sfactorAlpha,dfactorAlpha) +DECLARE_GL_FUNCTION_HEAD(void, BlendFunc, GLenum sfactor, GLenum dfactor) DECLARE_GL_FUNCTION_END_NO_RETURN(void, BlendFunc, sfactor,dfactor) +DECLARE_GL_FUNCTION_HEAD(void, BlendFuncSeparate, GLenum sfactorRGB, GLenum dfactorRGB, GLenum sfactorAlpha, GLenum dfactorAlpha) DECLARE_GL_FUNCTION_END_NO_RETURN(void, BlendFuncSeparate, sfactorRGB,dfactorRGB,sfactorAlpha,dfactorAlpha) DECLARE_GL_FUNCTION_HEAD(void, BufferData, GLenum target, GLsizeiptr size, const void *data, GLenum usage) DECLARE_GL_FUNCTION_END_NO_RETURN(void, BufferData, target,size,data,usage) DECLARE_GL_FUNCTION_STUB_HEAD(void, BufferSubData, GLenum target, GLintptr offset, GLsizeiptr size, const void *data) DECLARE_GL_FUNCTION_STUB_END_NO_RETURN(void, BufferSubData, target,offset,size,data) DECLARE_GL_FUNCTION_HEAD(GLenum, CheckFramebufferStatus, GLenum target) DECLARE_GL_FUNCTION_END(GLenum, CheckFramebufferStatus, target) @@ -52,7 +52,7 @@ DECLARE_GL_FUNCTION_HEAD(void, Clear, GLbitfield mask) DECLARE_GL_FUNCTION_STUB_ DECLARE_GL_FUNCTION_HEAD(void, ClearColor, GLfloat red, GLfloat green, GLfloat blue, GLfloat alpha) DECLARE_GL_FUNCTION_STUB_END_NO_RETURN(void, ClearColor, red,green,blue,alpha) DECLARE_GL_FUNCTION_STUB_HEAD(void, ClearDepthf, GLfloat d) DECLARE_GL_FUNCTION_STUB_END_NO_RETURN(void, ClearDepthf, d) DECLARE_GL_FUNCTION_STUB_HEAD(void, ClearStencil, GLint s) DECLARE_GL_FUNCTION_STUB_END_NO_RETURN(void, ClearStencil, s) -DECLARE_GL_FUNCTION_STUB_HEAD(void, ColorMask, GLboolean red, GLboolean green, GLboolean blue, GLboolean alpha) DECLARE_GL_FUNCTION_STUB_END_NO_RETURN(void, ColorMask, red,green,blue,alpha) +DECLARE_GL_FUNCTION_HEAD(void, ColorMask, GLboolean red, GLboolean green, GLboolean blue, GLboolean alpha) DECLARE_GL_FUNCTION_END_NO_RETURN(void, ColorMask, red,green,blue,alpha) DECLARE_GL_FUNCTION_HEAD(void, CompileShader, GLuint shader) DECLARE_GL_FUNCTION_END_NO_RETURN(void, CompileShader, shader) DECLARE_GL_FUNCTION_STUB_HEAD(void, CompressedTexImage2D, GLenum target, GLint level, GLenum internalformat, GLsizei width, GLsizei height, GLint border, GLsizei imageSize, const void *data) DECLARE_GL_FUNCTION_STUB_END_NO_RETURN(void, CompressedTexImage2D, target,level,internalformat,width,height,border,imageSize,data) DECLARE_GL_FUNCTION_STUB_HEAD(void, CompressedTexSubImage2D, GLenum target, GLint level, GLint xoffset, GLint yoffset, GLsizei width, GLsizei height, GLenum format, GLsizei imageSize, const void *data) DECLARE_GL_FUNCTION_STUB_END_NO_RETURN(void, CompressedTexSubImage2D, target,level,xoffset,yoffset,width,height,format,imageSize,data) @@ -67,15 +67,15 @@ DECLARE_GL_FUNCTION_HEAD(void, DeleteProgram, GLuint program) DECLARE_GL_FUNCTIO DECLARE_GL_FUNCTION_STUB_HEAD(void, DeleteRenderbuffers, GLsizei n, const GLuint *renderbuffers) DECLARE_GL_FUNCTION_STUB_END_NO_RETURN(void, DeleteRenderbuffers, n,renderbuffers) DECLARE_GL_FUNCTION_HEAD(void, DeleteShader, GLuint shader) DECLARE_GL_FUNCTION_END_NO_RETURN(void, DeleteShader, shader) DECLARE_GL_FUNCTION_HEAD(void, DeleteTextures, GLsizei n, const GLuint *textures) DECLARE_GL_FUNCTION_END_NO_RETURN(void, DeleteTextures, n,textures) -DECLARE_GL_FUNCTION_STUB_HEAD(void, DepthFunc, GLenum func) DECLARE_GL_FUNCTION_STUB_END_NO_RETURN(void, DepthFunc, func) -DECLARE_GL_FUNCTION_STUB_HEAD(void, DepthMask, GLboolean flag) DECLARE_GL_FUNCTION_STUB_END_NO_RETURN(void, DepthMask, flag) +DECLARE_GL_FUNCTION_HEAD(void, DepthFunc, GLenum func) DECLARE_GL_FUNCTION_END_NO_RETURN(void, DepthFunc, func) +DECLARE_GL_FUNCTION_HEAD(void, DepthMask, GLboolean flag) DECLARE_GL_FUNCTION_END_NO_RETURN(void, DepthMask, flag) DECLARE_GL_FUNCTION_STUB_HEAD(void, DepthRangef, GLfloat n, GLfloat f) DECLARE_GL_FUNCTION_STUB_END_NO_RETURN(void, DepthRangef, n,f) DECLARE_GL_FUNCTION_STUB_HEAD(void, DetachShader, GLuint program, GLuint shader) DECLARE_GL_FUNCTION_STUB_END_NO_RETURN(void, DetachShader, program,shader) -DECLARE_GL_FUNCTION_STUB_HEAD(void, Disable, GLenum cap) DECLARE_GL_FUNCTION_STUB_END_NO_RETURN(void, Disable, cap) +DECLARE_GL_FUNCTION_HEAD(void, Disable, GLenum cap) DECLARE_GL_FUNCTION_END_NO_RETURN(void, Disable, cap) DECLARE_GL_FUNCTION_STUB_HEAD(void, DisableVertexAttribArray, GLuint index) DECLARE_GL_FUNCTION_STUB_END_NO_RETURN(void, DisableVertexAttribArray, index) DECLARE_GL_FUNCTION_HEAD(void, DrawArrays, GLenum mode, GLint first, GLsizei count) DECLARE_GL_FUNCTION_END_NO_RETURN(void, DrawArrays, mode,first,count) DECLARE_GL_FUNCTION_HEAD(void, DrawElements, GLenum mode, GLsizei count, GLenum type, const void *indices) DECLARE_GL_FUNCTION_END_NO_RETURN(void, DrawElements, mode,count,type,indices) -DECLARE_GL_FUNCTION_STUB_HEAD(void, Enable, GLenum cap) DECLARE_GL_FUNCTION_STUB_END_NO_RETURN(void, Enable, cap) +DECLARE_GL_FUNCTION_HEAD(void, Enable, GLenum cap) DECLARE_GL_FUNCTION_END_NO_RETURN(void, Enable, cap) DECLARE_GL_FUNCTION_HEAD(void, EnableVertexAttribArray, GLuint index) DECLARE_GL_FUNCTION_END_NO_RETURN(void, EnableVertexAttribArray, index) DECLARE_GL_FUNCTION_STUB_HEAD(void, Finish) DECLARE_GL_FUNCTION_STUB_END_NO_RETURN(void, Finish) DECLARE_GL_FUNCTION_STUB_HEAD(void, Flush) DECLARE_GL_FUNCTION_STUB_END_NO_RETURN(void, Flush) @@ -173,7 +173,7 @@ DECLARE_GL_FUNCTION_STUB_HEAD(void, VertexAttrib3fv, GLuint index, const GLfloat DECLARE_GL_FUNCTION_STUB_HEAD(void, VertexAttrib4f, GLuint index, GLfloat x, GLfloat y, GLfloat z, GLfloat w) DECLARE_GL_FUNCTION_STUB_END_NO_RETURN(void, VertexAttrib4f, index,x,y,z,w) DECLARE_GL_FUNCTION_STUB_HEAD(void, VertexAttrib4fv, GLuint index, const GLfloat *v) DECLARE_GL_FUNCTION_STUB_END_NO_RETURN(void, VertexAttrib4fv, index,v) DECLARE_GL_FUNCTION_HEAD(void, VertexAttribPointer, GLuint index, GLint size, GLenum type, GLboolean normalized, GLsizei stride, const void *pointer) DECLARE_GL_FUNCTION_END_NO_RETURN(void, VertexAttribPointer, index,size,type,normalized,stride,pointer) -DECLARE_GL_FUNCTION_STUB_HEAD(void, Viewport, GLint x, GLint y, GLsizei width, GLsizei height) DECLARE_GL_FUNCTION_STUB_END_NO_RETURN(void, Viewport, x,y,width,height) +DECLARE_GL_FUNCTION_HEAD(void, Viewport, GLint x, GLint y, GLsizei width, GLsizei height) DECLARE_GL_FUNCTION_END_NO_RETURN(void, Viewport, x,y,width,height) DECLARE_GL_FUNCTION_STUB_HEAD(void, ReadBuffer, GLenum src) DECLARE_GL_FUNCTION_STUB_END_NO_RETURN(void, ReadBuffer, src) DECLARE_GL_FUNCTION_STUB_HEAD(void, DrawRangeElements, GLenum mode, GLuint start, GLuint end, GLsizei count, GLenum type, const void *indices) DECLARE_GL_FUNCTION_STUB_END_NO_RETURN(void, DrawRangeElements, mode,start,end,count,type,indices) DECLARE_GL_FUNCTION_STUB_HEAD(void, TexImage3D, GLenum target, GLint level, GLint internalformat, GLsizei width, GLsizei height, GLsizei depth, GLint border, GLenum format, GLenum type, const void *pixels) DECLARE_GL_FUNCTION_STUB_END_NO_RETURN(void, TexImage3D, target,level,internalformat,width,height,depth,border,format,type,pixels) diff --git a/MG/MG_GL/State/Common/CommonState.cpp b/MG/MG_GL/State/Common/CommonState.cpp index 50552b03..ec437424 100644 --- a/MG/MG_GL/State/Common/CommonState.cpp +++ b/MG/MG_GL/State/Common/CommonState.cpp @@ -4,6 +4,11 @@ #include "CommonState.h" +CommonState::CommonState() { + pixelStoreParams[GL_PACK_ALIGNMENT] = 4; + pixelStoreParams[GL_UNPACK_ALIGNMENT] = 4; +} + GLenum CommonState::SetPixelStoreInt(GLenum pname, GLint param) { if (MG_Constants::PixelStore::VALID_PARAM_NAMES.find(pname) == MG_Constants::PixelStore::VALID_PARAM_NAMES.end()) { return GL_INVALID_ENUM; @@ -54,3 +59,99 @@ GLint CommonState::QueryPixelStoreInt(GLenum pname) { auto defaultIt = MG_Constants::PixelStore::DEFAULT_VALUES_MAP.find(pname); return (defaultIt != MG_Constants::PixelStore::DEFAULT_VALUES_MAP.end()) ? defaultIt->second : 0; } + +GLenum CommonState::Enable(GLenum cap) { + if (MG_Constants::CommonState::VALID_CAPS.find(cap) == MG_Constants::CommonState::VALID_CAPS.end()) { + return GL_INVALID_ENUM; + } + capabilities[cap] = true; + return GL_NO_ERROR; +} + +GLenum CommonState::Disable(GLenum cap) { + if (MG_Constants::CommonState::VALID_CAPS.find(cap) == MG_Constants::CommonState::VALID_CAPS.end()) { + return GL_INVALID_ENUM; + } + capabilities[cap] = false; + return GL_NO_ERROR; +} + +GLenum CommonState::BlendFunc(GLenum sfactor, GLenum dfactor) { + if (MG_Constants::Blend::VALID_FACTORS.find(sfactor) == MG_Constants::Blend::VALID_FACTORS.end() || + MG_Constants::Blend::VALID_FACTORS.find(dfactor) == MG_Constants::Blend::VALID_FACTORS.end()) { + return GL_INVALID_ENUM; + } + + blendSrcRGB = blendSrcAlpha = sfactor; + blendDstRGB = blendDstAlpha = dfactor; + return GL_NO_ERROR; +} + +GLenum CommonState::BlendFuncSeparate(GLenum srcRGB, GLenum dstRGB, + GLenum srcAlpha, GLenum dstAlpha) { + if (MG_Constants::Blend::VALID_FACTORS.find(srcRGB) == MG_Constants::Blend::VALID_FACTORS.end() || + MG_Constants::Blend::VALID_FACTORS.find(dstRGB) == MG_Constants::Blend::VALID_FACTORS.end() || + MG_Constants::Blend::VALID_FACTORS.find(srcAlpha) == MG_Constants::Blend::VALID_FACTORS.end() || + MG_Constants::Blend::VALID_FACTORS.find(dstAlpha) == MG_Constants::Blend::VALID_FACTORS.end()) { + return GL_INVALID_ENUM; + } + blendSrcRGB = srcRGB; + blendDstRGB = dstRGB; + blendSrcAlpha = srcAlpha; + blendDstAlpha = dstAlpha; + return GL_NO_ERROR; +} + +GLenum CommonState::Clear(GLbitfield mask) { + const GLbitfield validBits = GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT | GL_STENCIL_BUFFER_BIT; + if ((mask & ~validBits) != 0) { + return GL_INVALID_VALUE; + } + return GL_NO_ERROR; +} + +GLenum CommonState::ClearColor(GLfloat red, GLfloat green, GLfloat blue, GLfloat alpha) { + clearColor[0] = red; + clearColor[1] = green; + clearColor[2] = blue; + clearColor[3] = alpha; + return GL_NO_ERROR; +} + +GLenum CommonState::ClearDepth(GLfloat depth) { + clearDepth = depth; + return GL_NO_ERROR; +} + +GLenum CommonState::ColorMask(GLboolean red, GLboolean green, GLboolean blue, GLboolean alpha) { + colorMask[0] = red; + colorMask[1] = green; + colorMask[2] = blue; + colorMask[3] = alpha; + return GL_NO_ERROR; +} + +GLenum CommonState::DepthFunc(GLenum func) { + if (MG_Constants::Depth::VALID_FUNCS.find(func) == MG_Constants::Depth::VALID_FUNCS.end()) { + return GL_INVALID_ENUM; + } + depthFunc = func; + return GL_NO_ERROR; +} + +GLenum CommonState::DepthMask(GLboolean flag) { + depthMask = flag; + return GL_NO_ERROR; +} + +GLenum CommonState::Viewport(GLint x, GLint y, GLsizei width, GLsizei height) { + if (width < 0 || height < 0) { + return GL_INVALID_VALUE; + } + + viewport[0] = x; + viewport[1] = y; + viewport[2] = width; + viewport[3] = height; + return GL_NO_ERROR; +} \ No newline at end of file diff --git a/MG/MG_GL/State/Common/CommonState.h b/MG/MG_GL/State/Common/CommonState.h index ab4b25fe..df77e207 100644 --- a/MG/MG_GL/State/Common/CommonState.h +++ b/MG/MG_GL/State/Common/CommonState.h @@ -10,11 +10,51 @@ class CommonState { private: + // Pixel storage parameters std::unordered_map pixelStoreParams; + + // Blend state + GLenum blendSrcRGB = GL_ONE; + GLenum blendDstRGB = GL_ZERO; + GLenum blendSrcAlpha = GL_ONE; + GLenum blendDstAlpha = GL_ZERO; + + // Clear state + GLfloat clearColor[4] = {0.0f, 0.0f, 0.0f, 0.0f}; + GLfloat clearDepth = 1.0f; + + // Color mask + GLboolean colorMask[4] = {GL_TRUE, GL_TRUE, GL_TRUE, GL_TRUE}; + + // Depth state + GLenum depthFunc = GL_LESS; + GLboolean depthMask = GL_TRUE; + + // Viewport + GLint viewport[4] = {0, 0, 0, 0}; + + // Capability enables + std::unordered_map capabilities; public: + CommonState(); + // Return: the validity of the operation, according to OpenGL 3 standard GLenum SetPixelStoreInt(GLenum pname, GLint param); + + GLenum Enable(GLenum cap); + GLenum Disable(GLenum cap); + GLenum BlendFunc(GLenum sfactor, GLenum dfactor); + GLenum BlendFuncSeparate(GLenum srcRGB, GLenum dstRGB, + GLenum srcAlpha, GLenum dstAlpha); + GLenum Clear(GLbitfield mask); + GLenum ClearColor(GLfloat red, GLfloat green, GLfloat blue, GLfloat alpha); + GLenum ClearDepth(GLfloat depth); + GLenum ColorMask(GLboolean red, GLboolean green, + GLboolean blue, GLboolean alpha); + GLenum DepthFunc(GLenum func); + GLenum DepthMask(GLboolean flag); + GLenum Viewport(GLint x, GLint y, GLsizei width, GLsizei height); GLint QueryPixelStoreInt(GLenum pname); }; diff --git a/MG/MG_GL/State/Core/GLState.cpp b/MG/MG_GL/State/Core/GLState.cpp index 862f039e..f7f5f290 100644 --- a/MG/MG_GL/State/Core/GLState.cpp +++ b/MG/MG_GL/State/Core/GLState.cpp @@ -99,14 +99,58 @@ namespace MG_State { } // Common - GLenum MG_State::SetPixelStoreInt(GLenum pname, GLint param) { + GLenum SetPixelStoreInt(GLenum pname, GLint param) { return MG_State_T::commonState->SetPixelStoreInt(pname, param); } - GLint MG_State::QueryPixelStoreInt(GLenum pname) { + GLint QueryPixelStoreInt(GLenum pname) { return MG_State_T::commonState->QueryPixelStoreInt(pname); } + GLenum glEnable(GLenum cap) { + return MG_State_T::commonState->Enable(cap); + } + + GLenum glDisable(GLenum cap) { + return MG_State_T::commonState->Disable(cap); + } + + GLenum glBlendFunc(GLenum sfactor, GLenum dfactor) { + return MG_State_T::commonState->BlendFunc(sfactor, dfactor); + } + + GLenum glBlendFuncSeparate(GLenum srcRGB, GLenum dstRGB, GLenum srcAlpha, GLenum dstAlpha) { + return MG_State_T::commonState->BlendFuncSeparate(srcRGB, dstRGB, srcAlpha, dstAlpha); + } + + GLenum glClear(GLbitfield mask) { + return MG_State_T::commonState->Clear(mask); + } + + GLenum glClearColor(GLfloat red, GLfloat green, GLfloat blue, GLfloat alpha) { + return MG_State_T::commonState->ClearColor(red, green, blue, alpha); + } + + GLenum glClearDepth(GLdouble depth) { + return MG_State_T::commonState->ClearDepth(static_cast(depth)); + } + + GLenum glColorMask(GLboolean red, GLboolean green, GLboolean blue, GLboolean alpha) { + return MG_State_T::commonState->ColorMask(red, green, blue, alpha); + } + + GLenum glDepthFunc(GLenum func) { + return MG_State_T::commonState->DepthFunc(func); + } + + GLenum glDepthMask(GLboolean flag) { + return MG_State_T::commonState->DepthMask(flag); + } + + GLenum glViewport(GLint x, GLint y, GLsizei width, GLsizei height) { + return MG_State_T::commonState->Viewport(x, y, width, height); + } + // Buffer GLenum AcquireBufferMemory(GLenum target, GLenum access, void** mappedPtr) { return MG_State_T::bufferState->AcquireBufferMemory(target, access, mappedPtr); diff --git a/MG/MG_GL/State/Core/GLState.h b/MG/MG_GL/State/Core/GLState.h index 2691f275..e86b040b 100644 --- a/MG/MG_GL/State/Core/GLState.h +++ b/MG/MG_GL/State/Core/GLState.h @@ -43,6 +43,17 @@ namespace MG_State { // Common GLenum SetPixelStoreInt(GLenum pname, GLint param); GLint QueryPixelStoreInt(GLenum pname); + GLenum glEnable(GLenum cap); + GLenum glDisable(GLenum cap); + GLenum glBlendFunc(GLenum sfactor, GLenum dfactor); + GLenum glBlendFuncSeparate(GLenum srcRGB, GLenum dstRGB, GLenum srcAlpha, GLenum dstAlpha); + GLenum glClear(GLbitfield mask); + GLenum glClearColor(GLfloat red, GLfloat green, GLfloat blue, GLfloat alpha); + GLenum glClearDepth(GLdouble depth); + GLenum glColorMask(GLboolean red, GLboolean green, GLboolean blue, GLboolean alpha); + GLenum glDepthFunc(GLenum func); + GLenum glDepthMask(GLboolean flag); + GLenum glViewport(GLint x, GLint y, GLsizei width, GLsizei height); // Buffer GLenum AcquireBufferMemory(GLenum target, GLenum access, void** mappedPtr);