From f8bbbec0ae4d01de6a46cfbad672979d1cb4d1cf Mon Sep 17 00:00:00 2001 From: rogerman Date: Tue, 19 Sep 2017 17:47:48 -0700 Subject: [PATCH] OpenGL Renderer: Fix possible crash that may occur if the 3D framebuffer is used before the 3D renderer has a chance to render at least once. (Partially addresses #59.) --- desmume/src/OGLRender.cpp | 16 ++++++++++------ desmume/src/OGLRender_3_2.cpp | 9 +++++---- 2 files changed, 15 insertions(+), 10 deletions(-) diff --git a/desmume/src/OGLRender.cpp b/desmume/src/OGLRender.cpp index e2a2a2d15..7bec38a94 100755 --- a/desmume/src/OGLRender.cpp +++ b/desmume/src/OGLRender.cpp @@ -4439,10 +4439,17 @@ Render3DError OpenGLRenderer_1_2::SetFramebufferSize(size_t w, size_t h) return OGLERROR_BEGINGL_FAILED; } - if (this->_mappedFramebuffer != NULL) + const size_t newFramebufferColorSizeBytes = w * h * sizeof(FragmentColor); + + if (this->isPBOSupported) { - glUnmapBufferARB(GL_PIXEL_PACK_BUFFER_ARB); - this->_mappedFramebuffer = NULL; + if (this->_mappedFramebuffer != NULL) + { + glUnmapBufferARB(GL_PIXEL_PACK_BUFFER_ARB); + } + + glBufferDataARB(GL_PIXEL_PACK_BUFFER_ARB, newFramebufferColorSizeBytes, NULL, GL_STREAM_READ_ARB); + this->_mappedFramebuffer = (FragmentColor *__restrict)glMapBufferARB(GL_PIXEL_PACK_BUFFER_ARB, GL_READ_ONLY_ARB); } if (this->isShaderSupported && this->isFBOSupported && this->isVBOSupported) @@ -4495,15 +4502,12 @@ Render3DError OpenGLRenderer_1_2::SetFramebufferSize(size_t w, size_t h) glRenderbufferStorageMultisampleEXT(GL_RENDERBUFFER_EXT, sampleSize, GL_DEPTH24_STENCIL8_EXT, w, h); } - const size_t newFramebufferColorSizeBytes = w * h * sizeof(FragmentColor); - this->_framebufferWidth = w; this->_framebufferHeight = h; this->_framebufferColorSizeBytes = newFramebufferColorSizeBytes; if (this->isPBOSupported) { - glBufferDataARB(GL_PIXEL_PACK_BUFFER_ARB, newFramebufferColorSizeBytes, NULL, GL_STREAM_READ_ARB); this->_framebufferColor = NULL; } else diff --git a/desmume/src/OGLRender_3_2.cpp b/desmume/src/OGLRender_3_2.cpp index 9885d0553..c950e13ec 100644 --- a/desmume/src/OGLRender_3_2.cpp +++ b/desmume/src/OGLRender_3_2.cpp @@ -1973,12 +1973,16 @@ Render3DError OpenGLRenderer_3_2::SetFramebufferSize(size_t w, size_t h) return OGLERROR_BEGINGL_FAILED; } + const size_t newFramebufferColorSizeBytes = w * h * sizeof(FragmentColor); + if (this->_mappedFramebuffer != NULL) { glUnmapBuffer(GL_PIXEL_PACK_BUFFER); - this->_mappedFramebuffer = NULL; } + glBufferData(GL_PIXEL_PACK_BUFFER, newFramebufferColorSizeBytes, NULL, GL_STREAM_READ); + this->_mappedFramebuffer = (FragmentColor *__restrict)glMapBuffer(GL_PIXEL_PACK_BUFFER, GL_READ_ONLY); + glActiveTexture(GL_TEXTURE0 + OGLTextureUnitID_FinalColor); glBindTexture(GL_TEXTURE_2D, OGLRef.texFinalColorID); glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, w, h, 0, GL_RGBA, GL_UNSIGNED_INT_8_8_8_8_REV, NULL); @@ -2030,9 +2034,6 @@ Render3DError OpenGLRenderer_3_2::SetFramebufferSize(size_t w, size_t h) glBindTexture(GL_TEXTURE_2D, OGLRef.texGDepthStencilAlphaID); glTexImage2D(GL_TEXTURE_2D, 0, GL_DEPTH24_STENCIL8, w, h, 0, GL_DEPTH_STENCIL, GL_UNSIGNED_INT_24_8, NULL); - const size_t newFramebufferColorSizeBytes = w * h * sizeof(FragmentColor); - glBufferData(GL_PIXEL_PACK_BUFFER, newFramebufferColorSizeBytes, NULL, GL_STREAM_READ); - this->_framebufferWidth = w; this->_framebufferHeight = h; this->_framebufferColorSizeBytes = newFramebufferColorSizeBytes;