diff --git a/src/Graphics/Context.cpp b/src/Graphics/Context.cpp index 02de7e29..ba3aa023 100644 --- a/src/Graphics/Context.cpp +++ b/src/Graphics/Context.cpp @@ -33,11 +33,9 @@ void Context::deleteTexture(ObjectName _name) return m_impl->deleteTexture(_name); } -void Context::init2DTexture(ObjectName _name, u32 _msaaLevel, u32 _width, u32 _height, u32 _mipMapLevel, - Parameter _format, Parameter _internalFormat, Parameter _dataType, const void * _data) +void Context::init2DTexture(const InitTextureParams & _params) { - return m_impl->init2DTexture(_name, _msaaLevel, _width, _height, - _mipMapLevel, _format, _internalFormat, _dataType, _data); + return m_impl->init2DTexture(_params); } bool Context::isMultisamplingSupported() const diff --git a/src/Graphics/Context.h b/src/Graphics/Context.h index 7fa56f10..e4e323cc 100644 --- a/src/Graphics/Context.h +++ b/src/Graphics/Context.h @@ -24,8 +24,19 @@ namespace graphics { void deleteTexture(ObjectName _name); - void init2DTexture(ObjectName _name, u32 _msaaLevel, u32 _width, u32 _height, u32 _mipMapLevel, - Parameter _format, Parameter _internalFormat, Parameter _dataType, const void * _data); + struct InitTextureParams { + ObjectName name; + u32 msaaLevel = 0; + u32 width = 0; + u32 height = 0; + u32 mipMapLevel = 0; + Parameter format; + Parameter internalFormat; + Parameter dataType; + const void * data; + }; + + void init2DTexture(const InitTextureParams & _params); bool isMultisamplingSupported() const; diff --git a/src/Graphics/ContextImpl.h b/src/Graphics/ContextImpl.h index 99525c15..4b627b57 100644 --- a/src/Graphics/ContextImpl.h +++ b/src/Graphics/ContextImpl.h @@ -2,6 +2,8 @@ #include "ObjectName.h" #include "Parameter.h" +#include "Context.h" + namespace graphics { class ContextImpl @@ -12,8 +14,7 @@ namespace graphics { virtual void destroy() = 0; virtual ObjectName createTexture() = 0; virtual void deleteTexture(ObjectName _name) = 0; - virtual void init2DTexture(ObjectName _name, u32 _msaaLevel, u32 _width, u32 _height, u32 _mipMapLevel, - Parameter _format, Parameter _internalFormat, Parameter _dataType, const void * _data) = 0; + virtual void init2DTexture(const Context::InitTextureParams & _params) = 0; }; } diff --git a/src/Graphics/ObjectName.h b/src/Graphics/ObjectName.h index 4b77e1c3..d79ac398 100644 --- a/src/Graphics/ObjectName.h +++ b/src/Graphics/ObjectName.h @@ -6,6 +6,7 @@ namespace graphics { class ObjectName { public: + ObjectName() : m_name(0) {} explicit ObjectName(u32 _name) : m_name(_name) {} explicit operator u32() const { return m_name; } diff --git a/src/Graphics/OpenGLContext/opengl_CachedFunctions.cpp b/src/Graphics/OpenGLContext/opengl_CachedFunctions.cpp index d23784ba..aa8d8024 100644 --- a/src/Graphics/OpenGLContext/opengl_CachedFunctions.cpp +++ b/src/Graphics/OpenGLContext/opengl_CachedFunctions.cpp @@ -17,6 +17,11 @@ CachedEnable::CachedEnable() { } +void CachedEnable::reset() +{ + m_enabled = false; +} + void CachedEnable::enable(bool _enable) { // TODO make cacheable @@ -33,6 +38,11 @@ CachedBindTexture::CachedBindTexture() : m_name(0U) { } +void CachedBindTexture::reset() +{ + m_name = graphics::ObjectName(0U); +} + void CachedBindTexture::bind(graphics::Parameter _target, graphics::ObjectName _name) { m_name = _name; @@ -52,6 +62,14 @@ CachedFunctions::~CachedFunctions() { } +void CachedFunctions::reset() { + for (auto it : m_enables) + it.second.reset(); + + m_bindTexture.reset(); +} + + CachedEnable * CachedFunctions::getCachedEnable(graphics::Parameter _parameter) { const u32 key(_parameter); diff --git a/src/Graphics/OpenGLContext/opengl_CachedFunctions.h b/src/Graphics/OpenGLContext/opengl_CachedFunctions.h index 49226a3b..34f935de 100644 --- a/src/Graphics/OpenGLContext/opengl_CachedFunctions.h +++ b/src/Graphics/OpenGLContext/opengl_CachedFunctions.h @@ -9,6 +9,9 @@ namespace opengl { public: CachedEnable(); CachedEnable(graphics::Parameter _parameter); + + void reset(); + void enable(bool _enable); private: @@ -35,6 +38,9 @@ namespace opengl { class CachedBindTexture { public: CachedBindTexture(); + + void reset(); + void bind(graphics::Parameter _target, graphics::ObjectName _name); private: @@ -47,9 +53,14 @@ namespace opengl { public: CachedFunctions(); ~CachedFunctions(); + + void reset(); + CachedEnable * getCachedEnable(graphics::Parameter _parameter); + CachedBindTexture * getCachedBindTexture(); + private: typedef std::unordered_map EnableParameters; @@ -57,4 +68,4 @@ namespace opengl { CachedBindTexture m_bindTexture; }; -} \ No newline at end of file +} diff --git a/src/Graphics/OpenGLContext/opengl_ContextImpl.cpp b/src/Graphics/OpenGLContext/opengl_ContextImpl.cpp index a5b3d27f..eb48b222 100644 --- a/src/Graphics/OpenGLContext/opengl_ContextImpl.cpp +++ b/src/Graphics/OpenGLContext/opengl_ContextImpl.cpp @@ -1,8 +1,6 @@ #include #include #include "opengl_ContextImpl.h" -#include "opengl_GLVersion.h" -#include "opengl_CachedFunctions.h" using namespace opengl; @@ -21,17 +19,13 @@ void ContextImpl::init() { initGLFunctions(); - GLVersion version; - glGetIntegerv(GL_MAJOR_VERSION, &version.majorVersion); - LOG(LOG_VERBOSE, "OpenGL major version: %d\n", version.majorVersion); - assert(version.majorVersion >= 3 && "Plugin requires GL version 3 or higher."); - GLint minorVersion = 0; - glGetIntegerv(GL_MINOR_VERSION, &version.minorVersion); - LOG(LOG_VERBOSE, "OpenGL minor version: %d\n", version.minorVersion); + glGetIntegerv(GL_MAJOR_VERSION, &m_version.majorVersion); + LOG(LOG_VERBOSE, "OpenGL major version: %d\n", m_version.majorVersion); + assert(m_version.majorVersion >= 3 && "Plugin requires GL version 3 or higher."); + glGetIntegerv(GL_MINOR_VERSION, &m_version.minorVersion); + LOG(LOG_VERBOSE, "OpenGL minor version: %d\n", m_version.minorVersion); - CachedFunctions cachedFunctions; - - TextureManipulationObjectFactory textureObjectsFactory(version, cachedFunctions); + TextureManipulationObjectFactory textureObjectsFactory(m_version, m_cachedFunctions); m_init2DTexture.reset(textureObjectsFactory.getInit2DTexture()); } @@ -54,11 +48,8 @@ void ContextImpl::deleteTexture(graphics::ObjectName _name) glDeleteTextures(1, &glName); } -void ContextImpl::init2DTexture(graphics::ObjectName _name, u32 _msaaLevel, u32 _width, u32 _height, u32 _mipMapLevel, - graphics::Parameter _format, graphics::Parameter _internalFormat, graphics::Parameter _dataType, - const void * _data) +void ContextImpl::init2DTexture(const graphics::Context::InitTextureParams & _params) { assert(m_init2DTexture); - m_init2DTexture->init2DTexture(_name, _msaaLevel, _width, _height, - _mipMapLevel, _format, _internalFormat, _dataType, _data); + m_init2DTexture->init2DTexture(_params); } diff --git a/src/Graphics/OpenGLContext/opengl_ContextImpl.h b/src/Graphics/OpenGLContext/opengl_ContextImpl.h index 2f6e30d6..afba7c55 100644 --- a/src/Graphics/OpenGLContext/opengl_ContextImpl.h +++ b/src/Graphics/OpenGLContext/opengl_ContextImpl.h @@ -2,6 +2,8 @@ #include #include #include "opengl_TextureManipulationObjectFactory.h" +#include "opengl_GLVersion.h" +#include "opengl_CachedFunctions.h" namespace opengl { @@ -19,12 +21,12 @@ namespace opengl { void deleteTexture(graphics::ObjectName _name) override; - void init2DTexture(graphics::ObjectName _name, u32 _msaaLevel, u32 _width, u32 _height, u32 _mipMapLevel, - graphics::Parameter _format, graphics::Parameter _internalFormat, graphics::Parameter _dataType, - const void * _data) override; + void init2DTexture(const graphics::Context::InitTextureParams & _params) override; private: std::unique_ptr m_init2DTexture; + GLVersion m_version; + CachedFunctions m_cachedFunctions; }; } diff --git a/src/Graphics/OpenGLContext/opengl_TextureManipulationObjectFactory.cpp b/src/Graphics/OpenGLContext/opengl_TextureManipulationObjectFactory.cpp index 0efbc95f..0d4be742 100644 --- a/src/Graphics/OpenGLContext/opengl_TextureManipulationObjectFactory.cpp +++ b/src/Graphics/OpenGLContext/opengl_TextureManipulationObjectFactory.cpp @@ -11,19 +11,30 @@ namespace opengl { public: Init2DTexImage(CachedBindTexture* _bind) : m_bind(_bind) {} - void init2DTexture(graphics::ObjectName _name, u32 _msaaLevel, - u32 _width, u32 _height, u32 _mipMapLevel, - graphics::Parameter _format, graphics::Parameter _internalFormat, - graphics::Parameter _dataType, const void * _data) override { + void init2DTexture(const graphics::Context::InitTextureParams & _params) override + { - if (_msaaLevel == 0) { + if (_params.msaaLevel == 0) { //glBindTexture(GL_TEXTURE_2D, GLuint(_name)); - m_bind->bind(graphics::target::TEXTURE_2D, _name); - glTexImage2D(GL_TEXTURE_2D, _mipMapLevel, GLuint(_internalFormat), _width, _height, 0, GLenum(_format), GLenum(_dataType), _data); + m_bind->bind(graphics::target::TEXTURE_2D, _params.name); + glTexImage2D(GL_TEXTURE_2D, + _params.mipMapLevel, + GLuint(_params.internalFormat), + _params.width, + _params.height, + 0, + GLenum(_params.format), + GLenum(_params.dataType), + _params.data); } else { //glBindTexture(GL_TEXTURE_2D_MULTISAMPLE, GLuint(_name)); - m_bind->bind(graphics::target::TEXTURE_2D_MULTISAMPLE, _name); - glTexImage2DMultisample(GL_TEXTURE_2D_MULTISAMPLE, _msaaLevel, GLenum(_internalFormat), _width, _height, false); + m_bind->bind(graphics::target::TEXTURE_2D_MULTISAMPLE, _params.name); + glTexImage2DMultisample(GL_TEXTURE_2D_MULTISAMPLE, + _params.msaaLevel, + GLenum(_params.internalFormat), + _params.width, + _params.height, + false); } } @@ -40,19 +51,33 @@ namespace opengl { Init2DTexStorage(CachedBindTexture* _bind) : m_bind(_bind) {} - void init2DTexture(graphics::ObjectName _name, u32 _msaaLevel, - u32 _width, u32 _height, u32 _mipMapLevel, - graphics::Parameter _format, graphics::Parameter _internalFormat, - graphics::Parameter _dataType, const void * _data) override { - - if (_msaaLevel == 0) { - m_bind->bind(graphics::target::TEXTURE_2D, _name); - glTexStorage2D(GL_TEXTURE_2D, _mipMapLevel, GLenum(_internalFormat), _width, _height); - if (_data != nullptr) - glTexSubImage2D(GL_TEXTURE_2D, _mipMapLevel, 0, 0, _width, _height, GLuint(_format), GLenum(_dataType), _data); + void init2DTexture(const graphics::Context::InitTextureParams & _params) override + { + if (_params.msaaLevel == 0) { + m_bind->bind(graphics::target::TEXTURE_2D, _params.name); + glTexStorage2D(GL_TEXTURE_2D, + _params.mipMapLevel, + GLenum(_params.internalFormat), + _params.width, + _params.height); + if (_params.data != nullptr) + glTexSubImage2D(GL_TEXTURE_2D, + _params.mipMapLevel, + 0, 0, + _params.width, + _params.height, + GLuint(_params.format), + GLenum(_params.dataType), + _params.data); } else { - m_bind->bind(graphics::target::TEXTURE_2D_MULTISAMPLE, _name); - glTexStorage2DMultisample(GL_TEXTURE_2D_MULTISAMPLE, _msaaLevel, GLenum(_internalFormat), _width, _height, false); + m_bind->bind(graphics::target::TEXTURE_2D_MULTISAMPLE, _params.name); + glTexStorage2DMultisample( + GL_TEXTURE_2D_MULTISAMPLE, + _params.msaaLevel, + GLenum(_params.internalFormat), + _params.width, + _params.height, + false); } } @@ -67,17 +92,31 @@ namespace opengl { // return (_version.majorVersion > 4) || (_version.majorVersion == 4 && _version.minorVersion >= 5); return false; } - void init2DTexture(graphics::ObjectName _name, u32 _msaaLevel, - u32 _width, u32 _height, u32 _mipMapLevel, - graphics::Parameter _format, graphics::Parameter _internalFormat, - graphics::Parameter _dataType, const void * _data) override { + void init2DTexture(const graphics::Context::InitTextureParams & _params) override + { - if (_msaaLevel == 0) { - glTextureStorage2D(GLuint(_name), _mipMapLevel, GLenum(_internalFormat), _width, _height); - if (_data != nullptr) - glTextureSubImage2D(GLuint(_name), _mipMapLevel, 0, 0, _width, _height, GLuint(_format), GLenum(_dataType), _data); + if (_params.msaaLevel == 0) { + glTextureStorage2D(GLuint(_params.name), + _params.mipMapLevel, + GLenum(_params.internalFormat), + _params.width, + _params.height); + if (_params.data != nullptr) + glTextureSubImage2D(GLuint(_params.name), + _params.mipMapLevel, + 0, 0, + _params.width, + _params.height, + GLuint(_params.format), + GLenum(_params.dataType), + _params.data); } else { - glTexStorage2DMultisample(GLuint(_name), _msaaLevel, GLenum(_internalFormat), _width, _height, false); + glTexStorage2DMultisample(GLuint(_params.name), + _params.msaaLevel, + GLenum(_params.internalFormat), + _params.width, + _params.height, + false); } } }; @@ -107,4 +146,4 @@ namespace opengl { return new Init2DTexImage(m_cachedFunctions.getCachedBindTexture()); } -} \ No newline at end of file +} diff --git a/src/Graphics/OpenGLContext/opengl_TextureManipulationObjectFactory.h b/src/Graphics/OpenGLContext/opengl_TextureManipulationObjectFactory.h index 93d4e89a..e4cceb58 100644 --- a/src/Graphics/OpenGLContext/opengl_TextureManipulationObjectFactory.h +++ b/src/Graphics/OpenGLContext/opengl_TextureManipulationObjectFactory.h @@ -1,6 +1,7 @@ #pragma once #include #include +#include namespace opengl { @@ -10,10 +11,7 @@ namespace opengl { class Init2DTexture { public: virtual ~Init2DTexture() {}; - virtual void init2DTexture(graphics::ObjectName _name, u32 _msaaLevel, - u32 _width, u32 _height, u32 _mipMapLevel, - graphics::Parameter _format, graphics::Parameter _internalFormat, - graphics::Parameter _dataType, const void * _data) = 0; + virtual void init2DTexture(const graphics::Context::InitTextureParams & _params) = 0; }; class TextureManipulationObjectFactory @@ -29,4 +27,4 @@ namespace opengl { CachedFunctions & m_cachedFunctions; }; -} \ No newline at end of file +} diff --git a/src/Graphics/Parameter.h b/src/Graphics/Parameter.h index eb4cfca0..fa4e4e86 100644 --- a/src/Graphics/Parameter.h +++ b/src/Graphics/Parameter.h @@ -6,6 +6,7 @@ namespace graphics { class Parameter { public: + Parameter() : m_parameter(0U) {} Parameter(u32 _parameter) : m_parameter(_parameter) {} explicit operator u32() const { return m_parameter; } explicit operator s32() const { return static_cast(m_parameter); } diff --git a/src/Textures.cpp b/src/Textures.cpp index f40b5e19..e7bba342 100644 --- a/src/Textures.cpp +++ b/src/Textures.cpp @@ -480,8 +480,17 @@ void TextureCache::init() glBindTexture(GL_TEXTURE_2D, m_pDummy->glName); glTexImage2D( GL_TEXTURE_2D, 0, GL_RGBA, 2, 2, 0, GL_RGBA, GL_UNSIGNED_BYTE, dummyTexture ); #else - gfxContext.init2DTexture(graphics::ObjectName(m_pDummy->glName), 0, m_pDummy->realWidth, m_pDummy->realHeight, 0, - graphics::color::RGBA, graphics::internalcolor::RGBA, graphics::type::UNSIGNED_BYTE, dummyTexture); + graphics::Context::InitTextureParams params; + params.name = graphics::ObjectName(m_pDummy->glName); + params.mipMapLevel = 0; + params.msaaLevel = 0; + params.width = m_pDummy->realWidth; + params.height = m_pDummy->realHeight; + params.format = graphics::color::RGBA; + params.internalFormat = graphics::internalcolor::RGBA; + params.dataType = graphics::type::UNSIGNED_BYTE; + params.data = dummyTexture; + gfxContext.init2DTexture(params); #endif m_cachedBytes = m_pDummy->textureBytes; @@ -519,9 +528,16 @@ void TextureCache::init() m_pMSDummy = addFrameBufferTexture(); // we don't want to remove dummy texture _initDummyTexture(m_pMSDummy); - gfxContext.init2DTexture(graphics::ObjectName(m_pMSDummy->glName), config.video.multisampling, - m_pMSDummy->realWidth, m_pMSDummy->realHeight, 0, - graphics::color::RGBA, graphics::internalcolor::RGBA, graphics::type::UNSIGNED_BYTE, nullptr); + graphics::Context::InitTextureParams params; + params.name = graphics::ObjectName(m_pMSDummy->glName); + params.mipMapLevel = 0; + params.msaaLevel = config.video.multisampling; + params.width = m_pMSDummy->realWidth; + params.height = m_pMSDummy->realHeight; + params.format = graphics::color::RGBA; + params.internalFormat = graphics::internalcolor::RGBA; + params.dataType = graphics::type::UNSIGNED_BYTE; + gfxContext.init2DTexture(params); activateMSDummy(0); activateMSDummy(1);