From 62e7fcf5802df2f3e16685cb578bbc4e81394a0c Mon Sep 17 00:00:00 2001 From: Sergey Lipskiy Date: Tue, 27 Dec 2016 23:26:36 +0700 Subject: [PATCH] Add cached GL functions. --- projects/msvc12/GLideN64.vcxproj | 2 + projects/msvc12/GLideN64.vcxproj.filters | 6 ++ .../OpenGLContext/opengl_CachedFunctions.cpp | 71 +++++++++++++++++++ .../OpenGLContext/opengl_CachedFunctions.h | 60 ++++++++++++++++ .../OpenGLContext/opengl_ContextImpl.cpp | 5 +- .../OpenGLContext/opengl_Parameters.cpp | 16 +++++ ...pengl_TextureManipulationObjectFactory.cpp | 49 ++++++++----- .../opengl_TextureManipulationObjectFactory.h | 4 +- src/Graphics/Parameters.h | 15 ++++ 9 files changed, 210 insertions(+), 18 deletions(-) create mode 100644 src/Graphics/OpenGLContext/opengl_CachedFunctions.cpp create mode 100644 src/Graphics/OpenGLContext/opengl_CachedFunctions.h diff --git a/projects/msvc12/GLideN64.vcxproj b/projects/msvc12/GLideN64.vcxproj index 0b0f8522..76433c1b 100644 --- a/projects/msvc12/GLideN64.vcxproj +++ b/projects/msvc12/GLideN64.vcxproj @@ -303,6 +303,7 @@ + @@ -431,6 +432,7 @@ + diff --git a/projects/msvc12/GLideN64.vcxproj.filters b/projects/msvc12/GLideN64.vcxproj.filters index dffe538a..c5ab96ec 100644 --- a/projects/msvc12/GLideN64.vcxproj.filters +++ b/projects/msvc12/GLideN64.vcxproj.filters @@ -296,6 +296,9 @@ Source Files\Graphics\OpenGL + + Source Files\Graphics\OpenGL + @@ -535,5 +538,8 @@ Header Files\Graphics\OpenGL + + Header Files\Graphics\OpenGL + \ No newline at end of file diff --git a/src/Graphics/OpenGLContext/opengl_CachedFunctions.cpp b/src/Graphics/OpenGLContext/opengl_CachedFunctions.cpp new file mode 100644 index 00000000..d23784ba --- /dev/null +++ b/src/Graphics/OpenGLContext/opengl_CachedFunctions.cpp @@ -0,0 +1,71 @@ +#include "GLFunctions.h" +#include "opengl_CachedFunctions.h" + +using namespace opengl; + +/*---------------CachedEnable-------------*/ + +CachedEnable::CachedEnable(graphics::Parameter _parameter) +: m_parameter(_parameter) +, m_enabled(false) +{ +} + +CachedEnable::CachedEnable() +: m_parameter(0U) +, m_enabled(false) +{ +} + +void CachedEnable::enable(bool _enable) +{ + // TODO make cacheable + if (_enable) { + glEnable(GLenum(m_parameter)); + } else { + glDisable(GLenum(m_parameter)); + } +} + +/*---------------CachedBindTexture-------------*/ + +CachedBindTexture::CachedBindTexture() +: m_name(0U) { +} + +void CachedBindTexture::bind(graphics::Parameter _target, graphics::ObjectName _name) +{ + m_name = _name; + // TODO make cacheable + glBindTexture(GLenum(_target), GLuint(_name)); +} + + +/*---------------CachedFunctions-------------*/ + +CachedFunctions::CachedFunctions() +{ +} + + +CachedFunctions::~CachedFunctions() +{ +} + +CachedEnable * CachedFunctions::getCachedEnable(graphics::Parameter _parameter) +{ + const u32 key(_parameter); + auto it = m_enables.find(key); + if (it == m_enables.end()) { + auto res = m_enables.emplace(key, _parameter); + if (res.second) + return &(res.first->second); + return nullptr; + } + return &(it->second); +} + +CachedBindTexture * CachedFunctions::getCachedBindTexture() +{ + return &m_bindTexture; +} diff --git a/src/Graphics/OpenGLContext/opengl_CachedFunctions.h b/src/Graphics/OpenGLContext/opengl_CachedFunctions.h new file mode 100644 index 00000000..49226a3b --- /dev/null +++ b/src/Graphics/OpenGLContext/opengl_CachedFunctions.h @@ -0,0 +1,60 @@ +#pragma once +#include +#include +#include + +namespace opengl { + + class CachedEnable { + public: + CachedEnable(); + CachedEnable(graphics::Parameter _parameter); + void enable(bool _enable); + + private: + const graphics::Parameter m_parameter; + bool m_enabled; + }; + + + template + class CachedBind { + public: + CachedBind(Bind * _bind) : m_bind(_bind), m_name(0U) {} + + void bind(graphics::Parameter _target, graphics::ObjectName _name) { + // TODO make cacheble + m_bind(GLenum(_target), GLuint(_name)); + } + + private: + graphics::ObjectName m_name; + Bind * m_bind; + }; + + class CachedBindTexture { + public: + CachedBindTexture(); + void bind(graphics::Parameter _target, graphics::ObjectName _name); + + private: + graphics::ObjectName m_name; + }; + + + class CachedFunctions + { + public: + CachedFunctions(); + ~CachedFunctions(); + CachedEnable * getCachedEnable(graphics::Parameter _parameter); + CachedBindTexture * getCachedBindTexture(); + + private: + typedef std::unordered_map EnableParameters; + + EnableParameters m_enables; + 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 848cbf21..a5b3d27f 100644 --- a/src/Graphics/OpenGLContext/opengl_ContextImpl.cpp +++ b/src/Graphics/OpenGLContext/opengl_ContextImpl.cpp @@ -2,6 +2,7 @@ #include #include "opengl_ContextImpl.h" #include "opengl_GLVersion.h" +#include "opengl_CachedFunctions.h" using namespace opengl; @@ -28,7 +29,9 @@ void ContextImpl::init() glGetIntegerv(GL_MINOR_VERSION, &version.minorVersion); LOG(LOG_VERBOSE, "OpenGL minor version: %d\n", version.minorVersion); - TextureManipulationObjectFactory textureObjectsFactory(version); + CachedFunctions cachedFunctions; + + TextureManipulationObjectFactory textureObjectsFactory(version, cachedFunctions); m_init2DTexture.reset(textureObjectsFactory.getInit2DTexture()); } diff --git a/src/Graphics/OpenGLContext/opengl_Parameters.cpp b/src/Graphics/OpenGLContext/opengl_Parameters.cpp index c988988f..6e5d89b0 100644 --- a/src/Graphics/OpenGLContext/opengl_Parameters.cpp +++ b/src/Graphics/OpenGLContext/opengl_Parameters.cpp @@ -24,4 +24,20 @@ namespace graphics { Parameter FLOAT(GL_FLOAT); } + namespace target { + Parameter TEXTURE_2D(GL_TEXTURE_2D); + Parameter TEXTURE_2D_MULTISAMPLE(GL_TEXTURE_2D_MULTISAMPLE); + } + + namespace enable { + Parameter BLEND(GL_BLEND); + Parameter CULL_FACE(GL_CULL_FACE); + Parameter DEPTH_TEST(GL_DEPTH_TEST); + Parameter DEPTH_CLAMP(GL_DEPTH_CLAMP); + Parameter CLIP_DISTANCE0(GL_CLIP_DISTANCE0); + Parameter DITHER(GL_DITHER); + Parameter POLYGON_OFFSET_FILL(GL_POLYGON_OFFSET_FILL); + Parameter SCISSOR_TEST(GL_SCISSOR_TEST); + } + } diff --git a/src/Graphics/OpenGLContext/opengl_TextureManipulationObjectFactory.cpp b/src/Graphics/OpenGLContext/opengl_TextureManipulationObjectFactory.cpp index f21ca9fd..0efbc95f 100644 --- a/src/Graphics/OpenGLContext/opengl_TextureManipulationObjectFactory.cpp +++ b/src/Graphics/OpenGLContext/opengl_TextureManipulationObjectFactory.cpp @@ -1,35 +1,34 @@ +#include #include "opengl_GLVersion.h" +#include "opengl_CachedFunctions.h" #include "opengl_TextureManipulationObjectFactory.h" namespace opengl { - TextureManipulationObjectFactory::TextureManipulationObjectFactory(const GLVersion & _version) - : m_version(_version) - { - } - - TextureManipulationObjectFactory::~TextureManipulationObjectFactory() - { - } - /*---------------Init2DTexture-------------*/ class Init2DTexImage : public Init2DTexture { 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 { if (_msaaLevel == 0) { - glBindTexture(GL_TEXTURE_2D, GLuint(_name)); + //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); } else { - glBindTexture(GL_TEXTURE_2D_MULTISAMPLE, GLuint(_name)); + //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); } - } + + private: + CachedBindTexture* m_bind; }; class Init2DTexStorage : public Init2DTexture { @@ -39,22 +38,27 @@ namespace opengl { return false; } + 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) { - glBindTexture(GL_TEXTURE_2D, GLuint(_name)); + 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); } else { - glBindTexture(GL_TEXTURE_2D_MULTISAMPLE, GLuint(_name)); + m_bind->bind(graphics::target::TEXTURE_2D_MULTISAMPLE, _name); glTexStorage2DMultisample(GL_TEXTURE_2D_MULTISAMPLE, _msaaLevel, GLenum(_internalFormat), _width, _height, false); } } + + private: + CachedBindTexture* m_bind; }; class Init2DTextureStorage : public Init2DTexture { @@ -79,15 +83,28 @@ namespace opengl { }; + /*---------------TextureManipulationObjectFactory-------------*/ + + TextureManipulationObjectFactory::TextureManipulationObjectFactory(const GLVersion & _version, + CachedFunctions & _cachedFunctions) + : m_version(_version) + , m_cachedFunctions(_cachedFunctions) + { + } + + TextureManipulationObjectFactory::~TextureManipulationObjectFactory() + { + } + Init2DTexture * TextureManipulationObjectFactory::getInit2DTexture() const { if (Init2DTextureStorage::Check(m_version)) return new Init2DTextureStorage; if (Init2DTexStorage::Check(m_version)) - return new Init2DTexStorage; + return new Init2DTexStorage(m_cachedFunctions.getCachedBindTexture()); - return new Init2DTexImage; + 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 cda4988b..93d4e89a 100644 --- a/src/Graphics/OpenGLContext/opengl_TextureManipulationObjectFactory.h +++ b/src/Graphics/OpenGLContext/opengl_TextureManipulationObjectFactory.h @@ -5,6 +5,7 @@ namespace opengl { struct GLVersion; + class CachedFunctions; class Init2DTexture { public: @@ -18,13 +19,14 @@ namespace opengl { class TextureManipulationObjectFactory { public: - TextureManipulationObjectFactory(const GLVersion & _version); + TextureManipulationObjectFactory(const GLVersion & _version, CachedFunctions & _cachedFunctions); ~TextureManipulationObjectFactory(); Init2DTexture * getInit2DTexture() const; private: const GLVersion & m_version; + CachedFunctions & m_cachedFunctions; }; } \ No newline at end of file diff --git a/src/Graphics/Parameters.h b/src/Graphics/Parameters.h index 405ba35a..d958e724 100644 --- a/src/Graphics/Parameters.h +++ b/src/Graphics/Parameters.h @@ -24,4 +24,19 @@ namespace graphics { extern Parameter FLOAT; } + namespace target { + extern Parameter TEXTURE_2D; + extern Parameter TEXTURE_2D_MULTISAMPLE; + } + + namespace enable { + extern Parameter BLEND; + extern Parameter CULL_FACE; + extern Parameter DEPTH_TEST; + extern Parameter DEPTH_CLAMP; + extern Parameter CLIP_DISTANCE0; + extern Parameter DITHER; + extern Parameter POLYGON_OFFSET_FILL; + extern Parameter SCISSOR_TEST; + } }