From 5e67d79ae7394e55367d8bdc47715f47f85c4eb8 Mon Sep 17 00:00:00 2001 From: Sergey Lipskiy Date: Sat, 14 Jan 2017 17:08:02 +0700 Subject: [PATCH] Implement Context::blitFramebuffers --- src/Graphics/Context.cpp | 9 ++++ src/Graphics/Context.h | 18 ++++++++ src/Graphics/ContextImpl.h | 1 + ...opengl_BufferManipulationObjectFactory.cpp | 46 +++++++++++++++++++ .../opengl_BufferManipulationObjectFactory.h | 19 ++++++-- .../OpenGLContext/opengl_ContextImpl.cpp | 12 +++++ .../OpenGLContext/opengl_ContextImpl.h | 9 ++++ src/GraphicsDrawer.cpp | 24 +++++++++- src/GraphicsDrawer.h | 10 ++++ src/OpenGL.h | 2 +- src/PostProcessor.cpp | 3 +- 11 files changed, 144 insertions(+), 9 deletions(-) diff --git a/src/Graphics/Context.cpp b/src/Graphics/Context.cpp index 6c689c49..2103cb6b 100644 --- a/src/Graphics/Context.cpp +++ b/src/Graphics/Context.cpp @@ -98,6 +98,8 @@ void Context::setTextureParameters(const TexParameters & _parameters) m_impl->setTextureParameters(_parameters); } +/*---------------Framebuffer-------------*/ + ObjectHandle Context::createFramebuffer() { return m_impl->createFramebuffer(); @@ -123,11 +125,18 @@ void Context::addFrameBufferRenderTarget(const FrameBufferRenderTarget & _params m_impl->addFrameBufferRenderTarget(_params); } +bool Context::blitFramebuffers(const BlitFramebuffersParams & _params) +{ + return m_impl->blitFramebuffers(_params); +} + PixelWriteBuffer * Context::createPixelWriteBuffer(size_t _sizeInBytes) { return m_impl->createPixelWriteBuffer(_sizeInBytes); } +/*---------------Shaders-------------*/ + CombinerProgram * Context::createCombinerProgram(Combiner & _color, Combiner & _alpha, const CombinerKey & _key) { return m_impl->createCombinerProgram(_color, _alpha, _key); diff --git a/src/Graphics/Context.h b/src/Graphics/Context.h index cd7ab7aa..bf4fcc77 100644 --- a/src/Graphics/Context.h +++ b/src/Graphics/Context.h @@ -131,6 +131,24 @@ namespace graphics { void addFrameBufferRenderTarget(const FrameBufferRenderTarget & _params); + struct BlitFramebuffersParams + { + ObjectHandle readBuffer; + ObjectHandle drawBuffer; + s32 srcX0; + s32 srcY0; + s32 srcX1; + s32 srcY1; + s32 dstX0; + s32 dstY0; + s32 dstX1; + s32 dstY1; + Parameter mask; + Parameter filter; + }; + + bool blitFramebuffers(const BlitFramebuffersParams & _params); + PixelWriteBuffer * createPixelWriteBuffer(size_t _sizeInBytes); /*---------------Shaders-------------*/ diff --git a/src/Graphics/ContextImpl.h b/src/Graphics/ContextImpl.h index 07c1333d..66fae902 100644 --- a/src/Graphics/ContextImpl.h +++ b/src/Graphics/ContextImpl.h @@ -32,6 +32,7 @@ namespace graphics { virtual void addFrameBufferRenderTarget(const Context::FrameBufferRenderTarget & _params) = 0; virtual ObjectHandle createRenderbuffer() = 0; virtual void initRenderbuffer(const Context::InitRenderbufferParams & _params) = 0; + virtual bool blitFramebuffers(const Context::BlitFramebuffersParams & _params) = 0; virtual PixelWriteBuffer * createPixelWriteBuffer(size_t _sizeInBytes) = 0; virtual CombinerProgram * createCombinerProgram(Combiner & _color, Combiner & _alpha, const CombinerKey & _key) = 0; virtual bool saveShadersStorage(const Combiners & _combiners) = 0; diff --git a/src/Graphics/OpenGLContext/opengl_BufferManipulationObjectFactory.cpp b/src/Graphics/OpenGLContext/opengl_BufferManipulationObjectFactory.cpp index 164b1d0d..459ad4a7 100644 --- a/src/Graphics/OpenGLContext/opengl_BufferManipulationObjectFactory.cpp +++ b/src/Graphics/OpenGLContext/opengl_BufferManipulationObjectFactory.cpp @@ -1,6 +1,7 @@ #include #include "opengl_GLInfo.h" #include "opengl_CachedFunctions.h" +#include "opengl_Utils.h" #include "opengl_BufferManipulationObjectFactory.h" //#define ENABLE_GL_4_5 @@ -221,6 +222,43 @@ private: CachedBindBuffer * m_bind; }; +/*---------------BlitFramebuffers-------------*/ + +class BlitFramebuffersImpl : public BlitFramebuffers +{ +public: + static bool Check(const GLInfo & _glinfo) { + return !_glinfo.isGLES2; + } + + BlitFramebuffersImpl(CachedBindFramebuffer * _bind) + : m_bind(_bind) {} + + bool blitFramebuffers(const graphics::Context::BlitFramebuffersParams & _params) override + { + m_bind->bind(graphics::bufferTarget::READ_FRAMEBUFFER, _params.readBuffer); + m_bind->bind(graphics::bufferTarget::DRAW_FRAMEBUFFER, _params.drawBuffer); + glBlitFramebuffer( + _params.srcX0, _params.srcY0, _params.srcX1, _params.srcY1, + _params.dstX0, _params.dstY0, _params.dstX1, _params.dstY1, + GLbitfield(_params.mask), GLenum(_params.filter) + ); + return !Utils::isGLError(); + } + +private: + CachedBindFramebuffer * m_bind; +}; + +class DummyBlitFramebuffers: public BlitFramebuffers +{ +public: + bool blitFramebuffers(const graphics::Context::BlitFramebuffersParams & _params) override + { + return false; + } +}; + /*---------------BufferManipulationObjectFactory-------------*/ BufferManipulationObjectFactory::BufferManipulationObjectFactory(const GLInfo & _info, @@ -261,6 +299,14 @@ AddFramebufferRenderTarget * BufferManipulationObjectFactory::getAddFramebufferR return new AddFramebufferTexture2D(m_cachedFunctions.geCachedBindFramebuffer()); } +BlitFramebuffers * BufferManipulationObjectFactory::getBlitFramebuffers() const +{ + if (BlitFramebuffersImpl::Check(m_glInfo)) + return new BlitFramebuffersImpl(m_cachedFunctions.geCachedBindFramebuffer()); + + return new DummyBlitFramebuffers; +} + CreatePixelWriteBuffer * BufferManipulationObjectFactory::createPixelWriteBuffer() const { if (m_glInfo.isGLES2) diff --git a/src/Graphics/OpenGLContext/opengl_BufferManipulationObjectFactory.h b/src/Graphics/OpenGLContext/opengl_BufferManipulationObjectFactory.h index 32d2c621..26e2b104 100644 --- a/src/Graphics/OpenGLContext/opengl_BufferManipulationObjectFactory.h +++ b/src/Graphics/OpenGLContext/opengl_BufferManipulationObjectFactory.h @@ -13,38 +13,45 @@ namespace opengl { class CreateFramebufferObject { public: - virtual ~CreateFramebufferObject() {}; + virtual ~CreateFramebufferObject() {} virtual graphics::ObjectHandle createFramebuffer() = 0; }; class CreateRenderbuffer { public: - virtual ~CreateRenderbuffer() {}; + virtual ~CreateRenderbuffer() {} virtual graphics::ObjectHandle createRenderbuffer() = 0; }; class InitRenderbuffer { public: - virtual ~InitRenderbuffer() {}; + virtual ~InitRenderbuffer() {} virtual void initRenderbuffer(const graphics::Context::InitRenderbufferParams & _params) = 0; }; class AddFramebufferRenderTarget { public: - virtual ~AddFramebufferRenderTarget() {}; + virtual ~AddFramebufferRenderTarget() {} virtual void addFrameBufferRenderTarget(const graphics::Context::FrameBufferRenderTarget & _params) = 0; }; class CreatePixelWriteBuffer { public: - virtual ~CreatePixelWriteBuffer() {}; + virtual ~CreatePixelWriteBuffer() {} virtual graphics::PixelWriteBuffer * createPixelWriteBuffer(size_t _sizeInBytes) = 0; }; + class BlitFramebuffers + { + public: + virtual ~BlitFramebuffers() {} + virtual bool blitFramebuffers(const graphics::Context::BlitFramebuffersParams & _params) = 0; + }; + class BufferManipulationObjectFactory { public: @@ -61,6 +68,8 @@ namespace opengl { CreatePixelWriteBuffer * createPixelWriteBuffer() const; + BlitFramebuffers * getBlitFramebuffers() const; + private: const GLInfo & m_glInfo; CachedFunctions & m_cachedFunctions; diff --git a/src/Graphics/OpenGLContext/opengl_ContextImpl.cpp b/src/Graphics/OpenGLContext/opengl_ContextImpl.cpp index f7285979..e8683396 100644 --- a/src/Graphics/OpenGLContext/opengl_ContextImpl.cpp +++ b/src/Graphics/OpenGLContext/opengl_ContextImpl.cpp @@ -42,6 +42,7 @@ void ContextImpl::init() m_initRenderbuffer.reset(bufferObjectFactory.getInitRenderbuffer()); m_addFramebufferRenderTarget.reset(bufferObjectFactory.getAddFramebufferRenderTarget()); m_createPixelWriteBuffer.reset(bufferObjectFactory.createPixelWriteBuffer()); + m_blitFramebuffers.reset(bufferObjectFactory.getBlitFramebuffers()); } { @@ -136,6 +137,8 @@ void ContextImpl::clearDepthBuffer() enableScissor->enable(true); } +/*---------------Texture-------------*/ + graphics::ObjectHandle ContextImpl::createTexture(graphics::Parameter _target) { return m_createTexture->createTexture(_target); @@ -163,6 +166,8 @@ void ContextImpl::setTextureParameters(const graphics::Context::TexParameters & m_set2DTextureParameters->setTextureParameters(_parameters); } +/*---------------Framebuffer-------------*/ + graphics::ObjectHandle ContextImpl::createFramebuffer() { return m_createFramebuffer->createFramebuffer(); @@ -190,11 +195,18 @@ void ContextImpl::addFrameBufferRenderTarget(const graphics::Context::FrameBuffe m_addFramebufferRenderTarget->addFrameBufferRenderTarget(_params); } +bool ContextImpl::blitFramebuffers(const graphics::Context::BlitFramebuffersParams & _params) +{ + return m_blitFramebuffers->blitFramebuffers(_params); +} + graphics::PixelWriteBuffer * ContextImpl::createPixelWriteBuffer(size_t _sizeInBytes) { return m_createPixelWriteBuffer->createPixelWriteBuffer(_sizeInBytes); } +/*---------------Shaders-------------*/ + graphics::CombinerProgram * ContextImpl::createCombinerProgram(Combiner & _color, Combiner & _alpha, const CombinerKey & _key) { return m_combinerProgramBuilder->buildCombinerProgram(_color, _alpha, _key); diff --git a/src/Graphics/OpenGLContext/opengl_ContextImpl.h b/src/Graphics/OpenGLContext/opengl_ContextImpl.h index 90101036..ec0d6927 100644 --- a/src/Graphics/OpenGLContext/opengl_ContextImpl.h +++ b/src/Graphics/OpenGLContext/opengl_ContextImpl.h @@ -44,6 +44,8 @@ namespace opengl { void clearDepthBuffer() override; + /*---------------Texture-------------*/ + graphics::ObjectHandle createTexture(graphics::Parameter _target) override; void deleteTexture(graphics::ObjectHandle _name) override; @@ -54,6 +56,8 @@ namespace opengl { void setTextureParameters(const graphics::Context::TexParameters & _parameters) override; + /*---------------Framebuffer-------------*/ + graphics::ObjectHandle createFramebuffer() override; void deleteFramebuffer(graphics::ObjectHandle _name) override; @@ -64,8 +68,12 @@ namespace opengl { void addFrameBufferRenderTarget(const graphics::Context::FrameBufferRenderTarget & _params) override; + bool blitFramebuffers(const graphics::Context::BlitFramebuffersParams & _params) override; + graphics::PixelWriteBuffer * createPixelWriteBuffer(size_t _sizeInBytes) override; + /*---------------Shaders-------------*/ + graphics::CombinerProgram * createCombinerProgram(Combiner & _color, Combiner & _alpha, const CombinerKey & _key) override; bool saveShadersStorage(const graphics::Combiners & _combiners) override; @@ -106,6 +114,7 @@ namespace opengl { std::unique_ptr m_initRenderbuffer; std::unique_ptr m_addFramebufferRenderTarget; std::unique_ptr m_createPixelWriteBuffer; + std::unique_ptr m_blitFramebuffers; std::unique_ptr m_graphicsDrawer; std::unique_ptr m_textDrawer; diff --git a/src/GraphicsDrawer.cpp b/src/GraphicsDrawer.cpp index 2ee39ff5..9533fb7f 100644 --- a/src/GraphicsDrawer.cpp +++ b/src/GraphicsDrawer.cpp @@ -1471,4 +1471,26 @@ void GraphicsDrawer::copyTexturedRect(const CopyRectParams & _params) gSP.changed |= CHANGED_GEOMETRYMODE | CHANGED_VIEWPORT; gDP.changed |= CHANGED_RENDERMODE | CHANGED_TILE | CHANGED_COMBINE | CHANGED_SCISSOR; -} \ No newline at end of file +} + +void GraphicsDrawer::blitOrCopyTexturedRect(const BlitOrCopyRectParams & _params) +{ + Context::BlitFramebuffersParams blitParams; + blitParams.readBuffer = _params.readBuffer; + blitParams.drawBuffer = _params.drawBuffer; + blitParams.srcX0 = _params.srcX0; + blitParams.srcX1 = _params.srcX1; + blitParams.dstX0 = _params.dstX0; + blitParams.dstX1 = _params.dstX1; + blitParams.srcY0 = _params.srcY0; + blitParams.srcY1 = _params.srcY1; + blitParams.dstY0 = _params.dstY0; + blitParams.dstY1 = _params.dstY1; + blitParams.mask = _params.mask; + blitParams.filter = _params.filter; + + if (gfxContext.blitFramebuffers(blitParams)) + return; + + copyTexturedRect(_params); +} diff --git a/src/GraphicsDrawer.h b/src/GraphicsDrawer.h index 7cac4520..8dc1cfa3 100644 --- a/src/GraphicsDrawer.h +++ b/src/GraphicsDrawer.h @@ -3,6 +3,7 @@ #include #include #include "gSP.h" +#include "Graphics/ObjectHandle.h" #include "Graphics/Parameter.h" namespace graphics { @@ -99,6 +100,15 @@ public: void copyTexturedRect(const CopyRectParams & _params); + struct BlitOrCopyRectParams : public CopyRectParams + { + graphics::ObjectHandle readBuffer; + graphics::ObjectHandle drawBuffer; + graphics::Parameter mask; + }; + + void blitOrCopyTexturedRect(const BlitOrCopyRectParams & _params); + void drawText(const char *_pText, float x, float y); void drawOSD(); diff --git a/src/OpenGL.h b/src/OpenGL.h index a56a41da..1f2095dc 100644 --- a/src/OpenGL.h +++ b/src/OpenGL.h @@ -58,7 +58,7 @@ typedef char GLchar; #include "common/GLFunctions.h" #define GL_IMAGE_TEXTURES_SUPPORT #define GL_MULTISAMPLING_SUPPORT -#define NO_BLIT_BUFFER_COPY +//#define NO_BLIT_BUFFER_COPY #endif // OS_MAC_OS_X #endif // GLES2 diff --git a/src/PostProcessor.cpp b/src/PostProcessor.cpp index b7090fe9..e1fab888 100644 --- a/src/PostProcessor.cpp +++ b/src/PostProcessor.cpp @@ -10,8 +10,7 @@ #include #include -#define NEW_POST_PROCESSOR - +//#define NEW_POST_PROCESSOR #if defined(GLES3_1) #define SHADER_VERSION "#version 310 es \n"