2022-04-05 20:49:15 +02:00
|
|
|
#include "Framebuffer.hpp"
|
|
|
|
#include "OpenGL.hpp"
|
|
|
|
|
|
|
|
bool CFramebuffer::alloc(int w, int h) {
|
2022-07-12 23:11:34 +02:00
|
|
|
bool firstAlloc = false;
|
|
|
|
|
|
|
|
if (m_iFb == (uint32_t)-1) {
|
2022-04-05 20:49:15 +02:00
|
|
|
firstAlloc = true;
|
|
|
|
glGenFramebuffers(1, &m_iFb);
|
|
|
|
}
|
|
|
|
|
2022-07-12 23:11:34 +02:00
|
|
|
if (m_cTex.m_iTexID == 0) {
|
2022-04-05 20:49:15 +02:00
|
|
|
firstAlloc = true;
|
|
|
|
glGenTextures(1, &m_cTex.m_iTexID);
|
|
|
|
glBindTexture(GL_TEXTURE_2D, m_cTex.m_iTexID);
|
|
|
|
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
|
|
|
|
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
|
|
|
|
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
|
|
|
|
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
|
|
|
|
}
|
|
|
|
|
2022-07-12 23:11:34 +02:00
|
|
|
if (firstAlloc || m_Size != Vector2D(w, h)) {
|
2022-04-05 20:49:15 +02:00
|
|
|
glBindTexture(GL_TEXTURE_2D, m_cTex.m_iTexID);
|
|
|
|
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, w, h, 0, GL_RGBA, GL_UNSIGNED_BYTE, 0);
|
|
|
|
|
|
|
|
glBindFramebuffer(GL_FRAMEBUFFER, m_iFb);
|
|
|
|
glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, m_cTex.m_iTexID, 0);
|
|
|
|
|
2022-04-11 16:39:48 +02:00
|
|
|
|
2022-04-13 17:34:13 +02:00
|
|
|
// TODO: Allow this with gles2
|
|
|
|
#ifndef GLES2
|
2022-04-11 16:39:48 +02:00
|
|
|
if (m_pStencilTex) {
|
|
|
|
glBindTexture(GL_TEXTURE_2D, m_pStencilTex->m_iTexID);
|
2022-04-13 17:51:18 +02:00
|
|
|
glTexImage2D(GL_TEXTURE_2D, 0, GL_DEPTH24_STENCIL8, w, h, 0, GL_DEPTH_STENCIL, GL_UNSIGNED_INT_24_8, 0);
|
2022-04-11 16:39:48 +02:00
|
|
|
|
|
|
|
glBindFramebuffer(GL_FRAMEBUFFER, m_iFb);
|
|
|
|
|
|
|
|
glFramebufferTexture2D(GL_FRAMEBUFFER, GL_STENCIL_ATTACHMENT, GL_TEXTURE_2D, m_pStencilTex->m_iTexID, 0);
|
|
|
|
}
|
2022-04-13 17:34:13 +02:00
|
|
|
#endif
|
2022-04-11 16:39:48 +02:00
|
|
|
|
2022-04-05 20:49:15 +02:00
|
|
|
auto status = glCheckFramebufferStatus(GL_FRAMEBUFFER);
|
|
|
|
RASSERT((status == GL_FRAMEBUFFER_COMPLETE), "Framebuffer incomplete, couldn't create! (FB status: %i)", status);
|
|
|
|
|
|
|
|
Debug::log(LOG, "Framebuffer created, status %i", status);
|
|
|
|
}
|
|
|
|
|
|
|
|
glBindTexture(GL_TEXTURE_2D, 0);
|
|
|
|
glBindFramebuffer(GL_FRAMEBUFFER, g_pHyprOpenGL->m_iCurrentOutputFb);
|
|
|
|
|
|
|
|
m_Size = Vector2D(w, h);
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
void CFramebuffer::bind() {
|
2022-04-13 17:34:13 +02:00
|
|
|
#ifndef GLES2
|
2022-04-05 20:49:15 +02:00
|
|
|
glBindFramebuffer(GL_DRAW_FRAMEBUFFER, m_iFb);
|
2022-04-13 17:34:13 +02:00
|
|
|
#else
|
|
|
|
glBindFramebuffer(GL_FRAMEBUFFER, m_iFb);
|
|
|
|
#endif
|
2022-05-18 20:33:54 +02:00
|
|
|
glViewport(0, 0, g_pHyprOpenGL->m_RenderData.pMonitor->vecPixelSize.x, g_pHyprOpenGL->m_RenderData.pMonitor->vecPixelSize.y);
|
2022-04-05 20:49:15 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void CFramebuffer::release() {
|
|
|
|
if (m_iFb != (uint32_t)-1 && m_iFb) {
|
|
|
|
glDeleteFramebuffers(1, &m_iFb);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (m_cTex.m_iTexID) {
|
|
|
|
glDeleteTextures(1, &m_cTex.m_iTexID);
|
|
|
|
}
|
|
|
|
|
|
|
|
m_cTex.m_iTexID = 0;
|
|
|
|
m_iFb = -1;
|
|
|
|
}
|
2022-07-12 23:11:34 +02:00
|
|
|
|
|
|
|
CFramebuffer::~CFramebuffer() {
|
|
|
|
release();
|
|
|
|
}
|