From 4316d4e13d0263a3746333ec9fa1de0325fcca23 Mon Sep 17 00:00:00 2001 From: BZLZHH Date: Sat, 17 May 2025 01:25:13 +0800 Subject: [PATCH] [Feat] (ShittilyDrawing): Implement DrawBuffer. Ensure read fbo is bound. --- .../Implementations/GL/Drawing/GL_Drawing.cpp | 47 ++++++++++++++++++- .../Implementations/GL/Drawing/GL_Drawing.h | 1 + .../GLFuncsDefinitions/GLFuncsDefinitions.cpp | 2 +- 3 files changed, 47 insertions(+), 3 deletions(-) diff --git a/MG/MG_GL/Implementations/GL/Drawing/GL_Drawing.cpp b/MG/MG_GL/Implementations/GL/Drawing/GL_Drawing.cpp index 5d1c145e..c31809c9 100644 --- a/MG/MG_GL/Implementations/GL/Drawing/GL_Drawing.cpp +++ b/MG/MG_GL/Implementations/GL/Drawing/GL_Drawing.cpp @@ -23,7 +23,7 @@ namespace MG_GL::GL { if (outFormat) *outFormat = format; break; - + case GL_DEPTH_COMPONENT24: if (outInternalFormat) *outInternalFormat = internalFormat; @@ -646,7 +646,7 @@ namespace MG_GL::GL { } static GLuint lastBoundProgram = 0; - static GLuint lastBoundFBO[2] = {0}; + static GLuint lastBoundFBO[2] = {0}; // 0: DRAW FBO, 1: READ FBO static std::array lastBoundTextures; void RealizeFBOState(GLenum fbtype) { @@ -1026,6 +1026,7 @@ namespace MG_GL::GL { // Framebuffer RealizeFBOState(GL_DRAW_FRAMEBUFFER); + RealizeFBOState(GL_READ_FRAMEBUFFER); } void DrawArraysSHITTILY(GLenum mode, GLint first, GLsizei count) { @@ -1109,6 +1110,7 @@ namespace MG_GL::GL { SyncAllTexturesToGLES(textureState); RealizeFBOState(GL_DRAW_FRAMEBUFFER); + RealizeFBOState(GL_READ_FRAMEBUFFER); static GLfloat lastClearColor[4] = {-1.0f, -1.0f, -1.0f, -1.0f}; if (memcmp(lastClearColor, commonState->clearColor, sizeof(lastClearColor)) != 0) { @@ -1227,4 +1229,45 @@ namespace MG_GL::GL { } } } + + void DrawBuffer(GLenum buf) { + SyncAllToGLES(); + RealizeFBOState(GL_READ_FRAMEBUFFER); + RealizeFBOState(GL_READ_FRAMEBUFFER); + + GLint currentFBO; + CallAndCheck(::GLES::glGetIntegerv(GL_FRAMEBUFFER_BINDING, ¤tFBO);) + + if (currentFBO == 0) { + GLenum buffers[1] = {GL_NONE}; + switch (buf) { + case GL_FRONT: + case GL_BACK: + case GL_NONE: + buffers[0] = buf; + CallAndCheck(::GLES::glDrawBuffers(1, buffers);) + break; + default: + break; + } + } else { + GLint maxAttachments; + CallAndCheck(::GLES::glGetIntegerv(GL_MAX_COLOR_ATTACHMENTS, &maxAttachments);) + + if (buf == GL_NONE) { + auto *buffers = (GLenum *)alloca(maxAttachments * sizeof(GLenum)); + for (int i = 0; i < maxAttachments; i++) { + buffers[i] = GL_NONE; + } + CallAndCheck(::GLES::glDrawBuffers(maxAttachments, buffers);) + } else if (buf >= GL_COLOR_ATTACHMENT0 && + buf < GL_COLOR_ATTACHMENT0 + maxAttachments) { + auto *buffers = (GLenum *)alloca(maxAttachments * sizeof(GLenum)); + for (int i = 0; i < maxAttachments; i++) { + buffers[i] = (i == (buf - GL_COLOR_ATTACHMENT0)) ? buf : GL_NONE; + } + CallAndCheck(::GLES::glDrawBuffers(maxAttachments, buffers);) + } + } + } } \ No newline at end of file diff --git a/MG/MG_GL/Implementations/GL/Drawing/GL_Drawing.h b/MG/MG_GL/Implementations/GL/Drawing/GL_Drawing.h index ac6f5528..1b1694cf 100644 --- a/MG/MG_GL/Implementations/GL/Drawing/GL_Drawing.h +++ b/MG/MG_GL/Implementations/GL/Drawing/GL_Drawing.h @@ -11,6 +11,7 @@ namespace MG_GL::GL { void DrawElementsBaseVertex(GLenum mode, GLsizei count, GLenum type, const GLvoid *indices, GLint basevertex); void MultiDrawElements(GLenum mode, const GLsizei *count, GLenum type, const GLvoid *const *indices, GLsizei drawcount); void MultiDrawElementsBaseVertex(GLenum mode, const GLsizei *count, GLenum type, const GLvoid *const *indices, GLsizei drawcount, const GLint *basevertex); + void DrawBuffer(GLenum buf); } #endif //MOBILEGL_GL_DRAWING_H diff --git a/MG/MG_GL/Implementations/GL/GLFuncsDefinitions/GLFuncsDefinitions.cpp b/MG/MG_GL/Implementations/GL/GLFuncsDefinitions/GLFuncsDefinitions.cpp index f4a08b16..dd8cb2df 100644 --- a/MG/MG_GL/Implementations/GL/GLFuncsDefinitions/GLFuncsDefinitions.cpp +++ b/MG/MG_GL/Implementations/GL/GLFuncsDefinitions/GLFuncsDefinitions.cpp @@ -403,7 +403,7 @@ DECLARE_GL_FUNCTION_STUB_HEAD(void, EdgeFlag, GLboolean flag ) DECLARE_GL_FUNCTI DECLARE_GL_FUNCTION_STUB_HEAD(void, EdgeFlagv, const GLboolean *flag ) DECLARE_GL_FUNCTION_STUB_END_NO_RETURN(void, EdgeFlagv,flag) DECLARE_GL_FUNCTION_STUB_HEAD(void, ClipPlane, GLenum plane, const GLdouble *equation ) DECLARE_GL_FUNCTION_STUB_END_NO_RETURN(void, ClipPlane,plane,equation) DECLARE_GL_FUNCTION_STUB_HEAD(void, GetClipPlane, GLenum plane, GLdouble *equation ) DECLARE_GL_FUNCTION_STUB_END_NO_RETURN(void, GetClipPlane,plane,equation) -DECLARE_GL_FUNCTION_STUB_HEAD(void, DrawBuffer, GLenum mode ) DECLARE_GL_FUNCTION_STUB_END_NO_RETURN(void, DrawBuffer,mode) +DECLARE_GL_FUNCTION_HEAD(void, DrawBuffer, GLenum mode ) DECLARE_GL_FUNCTION_END_NO_RETURN(void, DrawBuffer,mode) DECLARE_GL_FUNCTION_STUB_HEAD(void, EnableClientState, GLenum cap ) DECLARE_GL_FUNCTION_STUB_END_NO_RETURN(void, EnableClientState,cap) DECLARE_GL_FUNCTION_STUB_HEAD(void, DisableClientState, GLenum cap ) DECLARE_GL_FUNCTION_STUB_END_NO_RETURN(void, DisableClientState,cap) DECLARE_GL_FUNCTION_STUB_HEAD(void, GetDoublev, GLenum pname, GLdouble *params ) DECLARE_GL_FUNCTION_STUB_END_NO_RETURN(void, GetDoublev,pname,params)