From 94106858075e77e1b94de2bc6ceab1144146e8b1 Mon Sep 17 00:00:00 2001 From: Connor McLaughlin Date: Wed, 23 Oct 2019 00:35:08 +1000 Subject: [PATCH] Common: Optionally create framebuffer with GL textures --- src/common/gl_texture.cpp | 20 +++++++++++++++++++- src/common/gl_texture.h | 9 ++++++++- 2 files changed, 27 insertions(+), 2 deletions(-) diff --git a/src/common/gl_texture.cpp b/src/common/gl_texture.cpp index b08072015..efcd2aeb2 100644 --- a/src/common/gl_texture.cpp +++ b/src/common/gl_texture.cpp @@ -1,11 +1,12 @@ #include "gl_texture.h" +#include "YBaseLib/Assert.h" #include "YBaseLib/Log.h" Log_SetChannel(GL); namespace GL { Texture::Texture(u32 width, u32 height, GLenum format, GLenum type, const void* data /* = nullptr */, - bool linear_filter /* = false */) + bool linear_filter /* = false */, bool create_framebuffer /* = false */) : m_width(width), m_height(height) { glGenTextures(1, &m_id); @@ -14,10 +15,21 @@ Texture::Texture(u32 width, u32 height, GLenum format, GLenum type, const void* glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, linear_filter ? GL_LINEAR : GL_NEAREST); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, linear_filter ? GL_LINEAR : GL_NEAREST); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAX_LEVEL, 1); + + if (create_framebuffer) + { + glGenFramebuffers(1, &m_fbo_id); + glBindFramebuffer(GL_DRAW_FRAMEBUFFER, m_fbo_id); + glFramebufferTexture2D(GL_DRAW_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, m_id, 0); + Assert(glCheckFramebufferStatus(GL_DRAW_FRAMEBUFFER) == GL_FRAMEBUFFER_COMPLETE); + } } Texture::~Texture() { + if (m_fbo_id != 0) + glDeleteFramebuffers(1, &m_fbo_id); + glDeleteTextures(1, &m_id); } @@ -26,6 +38,12 @@ void Texture::Bind() glBindTexture(GL_TEXTURE_2D, m_id); } +void Texture::BindFramebuffer(GLenum target /*= GL_DRAW_FRAMEBUFFER*/) +{ + DebugAssert(m_fbo_id != 0); + glBindFramebuffer(target, m_fbo_id); +} + void Texture::Unbind() { glBindTexture(GL_TEXTURE_2D, 0); diff --git a/src/common/gl_texture.h b/src/common/gl_texture.h index 4e9bc34b3..638f33079 100644 --- a/src/common/gl_texture.h +++ b/src/common/gl_texture.h @@ -6,20 +6,27 @@ namespace GL { class Texture { public: - Texture(u32 width, u32 height, GLenum format, GLenum type, const void* data = nullptr, bool linear_filter = false); + Texture(u32 width, u32 height, GLenum format, GLenum type, const void* data = nullptr, bool linear_filter = false, + bool create_framebuffer = false); ~Texture(); GLuint GetGLId() const { return m_id; } u32 GetWidth() const { return m_width; } u32 GetHeight() const { return m_height; } + GLuint GetGLFramebufferID() const { return m_fbo_id; } + void Bind(); + void BindFramebuffer(GLenum target = GL_DRAW_FRAMEBUFFER); + static void Unbind(); private: GLuint m_id; u32 m_width; u32 m_height; + + GLuint m_fbo_id = 0; }; } // namespace GL \ No newline at end of file