diff options
Diffstat (limited to 'drape/framebuffer.cpp')
-rw-r--r-- | drape/framebuffer.cpp | 105 |
1 files changed, 105 insertions, 0 deletions
diff --git a/drape/framebuffer.cpp b/drape/framebuffer.cpp new file mode 100644 index 0000000000..73d79173f5 --- /dev/null +++ b/drape/framebuffer.cpp @@ -0,0 +1,105 @@ +#include "drape/framebuffer.hpp" + +#include "drape/glfunctions.hpp" +#include "drape/oglcontext.hpp" + +#include "base/assert.hpp" +#include "base/logging.hpp" +#include "base/string_utils.hpp" + +namespace dp +{ +Framebuffer::~Framebuffer() { Destroy(); } +void Framebuffer::Destroy() +{ + if (m_colorTextureId != 0) + { + GLFunctions::glDeleteTexture(m_colorTextureId); + m_colorTextureId = 0; + } + + if (m_depthTextureId != 0) + { + GLFunctions::glDeleteTexture(m_depthTextureId); + m_depthTextureId = 0; + } + + if (m_framebufferId != 0) + { + GLFunctions::glDeleteFramebuffer(&m_framebufferId); + m_framebufferId = 0; + } +} + +void Framebuffer::SetDefaultContext(dp::OGLContext * context) { m_defaultContext = context; } +void Framebuffer::SetSize(uint32_t width, uint32_t height) +{ + ASSERT(m_defaultContext, ()); + + if (!m_isSupported) + return; + + if (m_width == width && m_height == height) + return; + + m_height = height; + m_width = width; + + Destroy(); + + m_colorTextureId = GLFunctions::glGenTexture(); + GLFunctions::glBindTexture(m_colorTextureId); + GLFunctions::glTexImage2D(m_width, m_height, gl_const::GLRGBA, gl_const::GLUnsignedByteType, + nullptr); + GLFunctions::glTexParameter(gl_const::GLMagFilter, gl_const::GLLinear); + GLFunctions::glTexParameter(gl_const::GLMinFilter, gl_const::GLLinear); + GLFunctions::glTexParameter(gl_const::GLWrapT, gl_const::GLClampToEdge); + GLFunctions::glTexParameter(gl_const::GLWrapS, gl_const::GLClampToEdge); + + m_depthTextureId = GLFunctions::glGenTexture(); + GLFunctions::glBindTexture(m_depthTextureId); + GLFunctions::glTexImage2D(m_width, m_height, gl_const::GLDepthComponent, + gl_const::GLUnsignedIntType, nullptr); + if (GLFunctions::CurrentApiVersion == dp::ApiVersion::OpenGLES3) + { + GLFunctions::glTexParameter(gl_const::GLMagFilter, gl_const::GLNearest); + GLFunctions::glTexParameter(gl_const::GLMinFilter, gl_const::GLNearest); + GLFunctions::glTexParameter(gl_const::GLWrapT, gl_const::GLClampToEdge); + GLFunctions::glTexParameter(gl_const::GLWrapS, gl_const::GLClampToEdge); + } + + GLFunctions::glBindTexture(0); + + GLFunctions::glGenFramebuffer(&m_framebufferId); + GLFunctions::glBindFramebuffer(m_framebufferId); + + GLFunctions::glFramebufferTexture2D(gl_const::GLColorAttachment, m_colorTextureId); + GLFunctions::glFramebufferTexture2D(gl_const::GLDepthAttachment, m_depthTextureId); + GLFunctions::glFramebufferTexture2D(gl_const::GLStencilAttachment, 0); + + uint32_t const status = GLFunctions::glCheckFramebufferStatus(); + if (status != gl_const::GLFramebufferComplete) + { + m_isSupported = false; + Destroy(); + LOG(LWARNING, ("Framebuffer is unsupported. Framebuffer status =", status)); + } + + m_defaultContext->setDefaultFramebuffer(); +} + +void Framebuffer::Enable() +{ + ASSERT(m_isSupported, ()); + GLFunctions::glBindFramebuffer(m_framebufferId); +} + +void Framebuffer::Disable() +{ + ASSERT(m_defaultContext, ()); + ASSERT(m_isSupported, ()); + m_defaultContext->setDefaultFramebuffer(); +} + +uint32_t Framebuffer::GetTextureId() const { return m_colorTextureId; } +} // namespace dp |