From 663f696d8f5b59216fbdf0aff4beee5142a160b8 Mon Sep 17 00:00:00 2001 From: Sergey Lipskiy Date: Sun, 22 Jan 2017 18:10:52 +0700 Subject: [PATCH] Remove direct calls of glBindTexture and GL_UNPACK_ALIGNMENT related functions. --- src/Graphics/Context.cpp | 15 ++++++++++ src/Graphics/Context.h | 12 ++++++++ src/Graphics/ContextImpl.h | 3 ++ .../OpenGLContext/opengl_CachedFunctions.cpp | 13 +++++++++ .../OpenGLContext/opengl_CachedFunctions.h | 9 ++++++ .../OpenGLContext/opengl_ContextImpl.cpp | 17 +++++++++++ .../OpenGLContext/opengl_ContextImpl.h | 6 ++++ src/Textures.cpp | 29 ++++++++++--------- src/Textures.h | 2 +- 9 files changed, 92 insertions(+), 14 deletions(-) diff --git a/src/Graphics/Context.cpp b/src/Graphics/Context.cpp index 1019eb5d..e67ed966 100644 --- a/src/Graphics/Context.cpp +++ b/src/Graphics/Context.cpp @@ -110,6 +110,21 @@ void Context::setTextureParameters(const TexParameters & _parameters) m_impl->setTextureParameters(_parameters); } +void Context::bindTexture(const BindTextureParameters & _params) +{ + m_impl->bindTexture(_params); +} + +void Context::setTextureUnpackAlignment(s32 _param) +{ + m_impl->setTextureUnpackAlignment(_param); +} + +s32 Context::getTextureUnpackAlignment() const +{ + return m_impl->getTextureUnpackAlignment(); +} + void Context::bindImageTexture(const BindImageTextureParameters & _params) { m_impl->bindImageTexture(_params); diff --git a/src/Graphics/Context.h b/src/Graphics/Context.h index 296824f0..0f720b65 100644 --- a/src/Graphics/Context.h +++ b/src/Graphics/Context.h @@ -110,6 +110,18 @@ namespace graphics { void setTextureParameters(const TexParameters & _parameters); + struct BindTextureParameters { + ObjectHandle texture; + Parameter textureUnitIndex; + Parameter target; + }; + + void bindTexture(const BindTextureParameters & _params); + + void setTextureUnpackAlignment(s32 _param); + + s32 getTextureUnpackAlignment() const; + struct BindImageTextureParameters { Parameter imageUnit; ObjectHandle texture; diff --git a/src/Graphics/ContextImpl.h b/src/Graphics/ContextImpl.h index 00911f23..20d6d594 100644 --- a/src/Graphics/ContextImpl.h +++ b/src/Graphics/ContextImpl.h @@ -28,6 +28,9 @@ namespace graphics { virtual void init2DTexture(const Context::InitTextureParams & _params) = 0; virtual void update2DTexture(const Context::UpdateTextureDataParams & _params) = 0; virtual void setTextureParameters(const Context::TexParameters & _parameters) = 0; + virtual void bindTexture(const Context::BindTextureParameters & _params) = 0; + virtual void setTextureUnpackAlignment(s32 _param) = 0; + virtual s32 getTextureUnpackAlignment() const = 0; virtual void bindImageTexture(const Context::BindImageTextureParameters & _params) = 0; virtual FramebufferTextureFormats * getFramebufferTextureFormats() = 0; virtual ObjectHandle createFramebuffer() = 0; diff --git a/src/Graphics/OpenGLContext/opengl_CachedFunctions.cpp b/src/Graphics/OpenGLContext/opengl_CachedFunctions.cpp index ce686b1c..c979cb2b 100644 --- a/src/Graphics/OpenGLContext/opengl_CachedFunctions.cpp +++ b/src/Graphics/OpenGLContext/opengl_CachedFunctions.cpp @@ -133,6 +133,14 @@ void CachedUseProgram::useProgram(graphics::ObjectHandle _program) glUseProgram(GLuint(_program)); } +/*---------------CachedTextureUnpackAlignment-------------*/ + +void CachedTextureUnpackAlignment::setTextureUnpackAlignment(s32 _param) +{ + if (update(_param)) + glPixelStorei(GL_UNPACK_ALIGNMENT, _param); +} + /*---------------CachedFunctions-------------*/ CachedFunctions::CachedFunctions(const GLInfo & _glinfo) @@ -258,3 +266,8 @@ CachedUseProgram * CachedFunctions::getCachedUseProgram() { return &m_useProgram; } + +CachedTextureUnpackAlignment * CachedFunctions::getCachedTextureUnpackAlignment() +{ + return &m_unpackAlignment; +} diff --git a/src/Graphics/OpenGLContext/opengl_CachedFunctions.h b/src/Graphics/OpenGLContext/opengl_CachedFunctions.h index 6364e146..1e7addfb 100644 --- a/src/Graphics/OpenGLContext/opengl_CachedFunctions.h +++ b/src/Graphics/OpenGLContext/opengl_CachedFunctions.h @@ -201,6 +201,12 @@ namespace opengl { void useProgram(graphics::ObjectHandle _program); }; + class CachedTextureUnpackAlignment : public Cached1 + { + public: + void setTextureUnpackAlignment(s32 _param); + }; + /*---------------CachedFunctions-------------*/ class CachedFunctions @@ -243,6 +249,8 @@ namespace opengl { CachedUseProgram * getCachedUseProgram(); + CachedTextureUnpackAlignment * getCachedTextureUnpackAlignment(); + private: typedef std::unordered_map EnableParameters; @@ -262,6 +270,7 @@ namespace opengl { CachedClearColor m_clearColor; CachedVertexAttribArray m_attribArray; CachedUseProgram m_useProgram; + CachedTextureUnpackAlignment m_unpackAlignment; }; } diff --git a/src/Graphics/OpenGLContext/opengl_ContextImpl.cpp b/src/Graphics/OpenGLContext/opengl_ContextImpl.cpp index 9a391b43..3a796619 100644 --- a/src/Graphics/OpenGLContext/opengl_ContextImpl.cpp +++ b/src/Graphics/OpenGLContext/opengl_ContextImpl.cpp @@ -178,6 +178,23 @@ void ContextImpl::setTextureParameters(const graphics::Context::TexParameters & m_set2DTextureParameters->setTextureParameters(_parameters); } +void ContextImpl::bindTexture(const graphics::Context::BindTextureParameters & _params) { + m_cachedFunctions->getCachedActiveTexture()->setActiveTexture(_params.textureUnitIndex); + m_cachedFunctions->getCachedBindTexture()->bind(_params.target, _params.texture); +} + +void ContextImpl::setTextureUnpackAlignment(s32 _param) +{ + m_cachedFunctions->getCachedTextureUnpackAlignment()->setTextureUnpackAlignment(_param); +} + +s32 ContextImpl::getTextureUnpackAlignment() const +{ + GLint unpackAlignment; + glGetIntegerv(GL_UNPACK_ALIGNMENT, &unpackAlignment); + return unpackAlignment; +} + void ContextImpl::bindImageTexture(const graphics::Context::BindImageTextureParameters & _params) { if (glBindImageTexture != nullptr) diff --git a/src/Graphics/OpenGLContext/opengl_ContextImpl.h b/src/Graphics/OpenGLContext/opengl_ContextImpl.h index 24ca881c..354ea5f4 100644 --- a/src/Graphics/OpenGLContext/opengl_ContextImpl.h +++ b/src/Graphics/OpenGLContext/opengl_ContextImpl.h @@ -59,6 +59,12 @@ namespace opengl { void setTextureParameters(const graphics::Context::TexParameters & _parameters) override; + void bindTexture(const graphics::Context::BindTextureParameters & _params) override; + + void setTextureUnpackAlignment(s32 _param) override; + + s32 getTextureUnpackAlignment() const override; + void bindImageTexture(const graphics::Context::BindImageTextureParameters & _params) override; /*---------------Framebuffer-------------*/ diff --git a/src/Textures.cpp b/src/Textures.cpp index 4a35d6ef..c48d426f 100644 --- a/src/Textures.cpp +++ b/src/Textures.cpp @@ -562,7 +562,7 @@ void TextureCache::_checkCacheSize() CachedTexture * TextureCache::_addTexture(u32 _crc32) { if (m_curUnpackAlignment == 0) - glGetIntegerv(GL_UNPACK_ALIGNMENT, &m_curUnpackAlignment); + m_curUnpackAlignment = gfxContext.getTextureUnpackAlignment(); _checkCacheSize(); m_textures.emplace_front(gfxContext.createTexture(target::TEXTURE_2D)); Textures::iterator new_iter = m_textures.begin(); @@ -833,9 +833,9 @@ void TextureCache::_loadBackground(CachedTexture *pTexture) glInternalFormat, (uint64)pTexture->crc, &ghqTexInfo) != 0 && ghqTexInfo.data != nullptr) { if (ghqTexInfo.width % 2 != 0 && - ghqTexInfo.format != GL_RGBA && - m_curUnpackAlignment > 1) - glPixelStorei(GL_UNPACK_ALIGNMENT, 2); + ghqTexInfo.format != GL_RGBA && + m_curUnpackAlignment > 1) + gfxContext.setTextureUnpackAlignment(2); Context::InitTextureParams params; params.handle = pTexture->name; params.mipMapLevel = 0; @@ -867,7 +867,7 @@ void TextureCache::_loadBackground(CachedTexture *pTexture) gfxContext.init2DTexture(params); } if (m_curUnpackAlignment > 1) - glPixelStorei(GL_UNPACK_ALIGNMENT, m_curUnpackAlignment); + gfxContext.setTextureUnpackAlignment(m_curUnpackAlignment); free(pSwapped); free(pDest); } @@ -1111,7 +1111,7 @@ void TextureCache::_load(u32 _tile, CachedTexture *_pTexture) pDest = (u32*)malloc(_pTexture->textureBytes); assert(pDest != nullptr); - GLint mipLevel = 0; + s32 mipLevel = 0; _pTexture->max_level = 0; if (config.generalEmulation.enableLOD != 0 && gSP.texture.level > 1) @@ -1168,9 +1168,9 @@ void TextureCache::_load(u32 _tile, CachedTexture *_pTexture) } if (!bLoaded) { if (tmptex.realWidth % 2 != 0 && - glInternalFormat != GL_RGBA && - m_curUnpackAlignment > 1) - glPixelStorei(GL_UNPACK_ALIGNMENT, 2); + glInternalFormat != GL_RGBA && + m_curUnpackAlignment > 1) + gfxContext.setTextureUnpackAlignment(2); Context::InitTextureParams params; params.handle = _pTexture->name; params.mipMapLevel = mipLevel; @@ -1208,7 +1208,7 @@ void TextureCache::_load(u32 _tile, CachedTexture *_pTexture) _pTexture->textureBytes += (tmptex.realWidth * tmptex.realHeight) << sizeShift; } if (m_curUnpackAlignment > 1) - glPixelStorei(GL_UNPACK_ALIGNMENT, m_curUnpackAlignment); + gfxContext.setTextureUnpackAlignment(m_curUnpackAlignment); free(pDest); } @@ -1263,7 +1263,7 @@ void TextureCache::activateTexture(u32 _t, CachedTexture *_pTexture) const bool bUseBilinear = (gDP.otherMode.textureFilter | (gSP.objRendermode&G_OBJRM_BILERP)) != 0; const bool bUseLOD = currentCombiner()->usesLOD(); - const GLint texLevel = bUseLOD ? _pTexture->max_level : 0; + const s32 texLevel = bUseLOD ? _pTexture->max_level : 0; params.maxMipmapLevel = Parameter(texLevel); if (config.texture.bilinearMode == BILINEAR_STANDARD) { @@ -1377,10 +1377,13 @@ void TextureCache::_updateBackground() m_misses++; - glActiveTexture( GL_TEXTURE0 ); CachedTexture * pCurrent = _addTexture(crc); - glBindTexture( GL_TEXTURE_2D, GLuint(pCurrent->name) ); + Context::BindTextureParameters bindParams; + bindParams.target = target::TEXTURE_2D; + bindParams.texture = pCurrent->name; + bindParams.textureUnitIndex = textureIndices::Tex[0]; + gfxContext.bindTexture(bindParams); pCurrent->address = gSP.bgImage.address; diff --git a/src/Textures.h b/src/Textures.h index 5068bae1..dfc072db 100644 --- a/src/Textures.h +++ b/src/Textures.h @@ -93,7 +93,7 @@ private: u32 m_hits, m_misses; u32 m_maxBytes; u32 m_cachedBytes; - GLint m_curUnpackAlignment; + s32 m_curUnpackAlignment; bool m_toggleDumpTex; };