2022-04-05 20:49:15 +02:00
|
|
|
#include "Framebuffer.hpp"
|
|
|
|
#include "OpenGL.hpp"
|
|
|
|
|
2024-06-08 10:07:59 +02:00
|
|
|
CFramebuffer::CFramebuffer() {
|
2024-11-01 16:52:03 +01:00
|
|
|
;
|
2024-06-08 10:07:59 +02:00
|
|
|
}
|
|
|
|
|
2023-10-30 16:56:02 +01:00
|
|
|
bool CFramebuffer::alloc(int w, int h, uint32_t drmFormat) {
|
2022-07-12 23:11:34 +02:00
|
|
|
bool firstAlloc = false;
|
2023-09-06 12:51:36 +02:00
|
|
|
RASSERT((w > 1 && h > 1), "cannot alloc a FB with negative / zero size! (attempted {}x{})", w, h);
|
2022-09-25 20:07:48 +02:00
|
|
|
|
2024-06-08 10:07:59 +02:00
|
|
|
uint32_t glFormat = FormatUtils::drmFormatToGL(drmFormat);
|
|
|
|
uint32_t glType = FormatUtils::glFormatToType(glFormat);
|
2023-10-30 16:56:02 +01:00
|
|
|
|
2024-11-01 16:52:03 +01:00
|
|
|
if (!m_cTex)
|
|
|
|
m_cTex = makeShared<CTexture>();
|
|
|
|
|
2024-08-08 21:01:50 +02:00
|
|
|
if (!m_iFbAllocated) {
|
2022-04-05 20:49:15 +02:00
|
|
|
firstAlloc = true;
|
|
|
|
glGenFramebuffers(1, &m_iFb);
|
2024-08-08 21:01:50 +02:00
|
|
|
m_iFbAllocated = true;
|
2022-04-05 20:49:15 +02:00
|
|
|
}
|
|
|
|
|
2024-06-08 10:07:59 +02:00
|
|
|
if (m_cTex->m_iTexID == 0) {
|
2022-04-05 20:49:15 +02:00
|
|
|
firstAlloc = true;
|
2024-06-08 10:07:59 +02:00
|
|
|
m_cTex->allocate();
|
|
|
|
glBindTexture(GL_TEXTURE_2D, m_cTex->m_iTexID);
|
2022-04-05 20:49:15 +02:00
|
|
|
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);
|
|
|
|
}
|
|
|
|
|
2023-07-29 13:27:40 +02:00
|
|
|
if (firstAlloc || m_vSize != Vector2D(w, h)) {
|
2024-06-08 10:07:59 +02:00
|
|
|
glBindTexture(GL_TEXTURE_2D, m_cTex->m_iTexID);
|
2023-10-30 16:56:02 +01:00
|
|
|
glTexImage2D(GL_TEXTURE_2D, 0, glFormat, w, h, 0, GL_RGBA, glType, 0);
|
2022-04-05 20:49:15 +02:00
|
|
|
|
|
|
|
glBindFramebuffer(GL_FRAMEBUFFER, m_iFb);
|
2024-06-08 10:07:59 +02:00
|
|
|
glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, m_cTex->m_iTexID, 0);
|
2022-04-05 20:49:15 +02:00
|
|
|
|
2022-12-16 18:17:31 +01: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-12-16 18:17:31 +01:00
|
|
|
#endif
|
2022-04-11 16:39:48 +02:00
|
|
|
|
2022-04-05 20:49:15 +02:00
|
|
|
auto status = glCheckFramebufferStatus(GL_FRAMEBUFFER);
|
2024-04-03 15:23:19 +02:00
|
|
|
RASSERT((status == GL_FRAMEBUFFER_COMPLETE), "Framebuffer incomplete, couldn't create! (FB status: {}, GL Error: 0x{:x})", status, (int)glGetError());
|
2022-04-05 20:49:15 +02:00
|
|
|
|
2023-09-06 12:51:36 +02:00
|
|
|
Debug::log(LOG, "Framebuffer created, status {}", status);
|
2022-04-05 20:49:15 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
glBindTexture(GL_TEXTURE_2D, 0);
|
2024-11-01 16:52:03 +01:00
|
|
|
if (g_pHyprOpenGL)
|
|
|
|
glBindFramebuffer(GL_FRAMEBUFFER, g_pHyprOpenGL->m_iCurrentOutputFb);
|
2022-04-05 20:49:15 +02:00
|
|
|
|
2023-07-29 13:27:40 +02:00
|
|
|
m_vSize = Vector2D(w, h);
|
2022-04-05 20:49:15 +02:00
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2023-11-24 11:54:21 +01:00
|
|
|
void CFramebuffer::addStencil() {
|
2023-12-05 16:41:17 +01:00
|
|
|
// TODO: Allow this with gles2
|
|
|
|
#ifndef GLES2
|
2023-11-24 11:54:21 +01:00
|
|
|
glBindTexture(GL_TEXTURE_2D, m_pStencilTex->m_iTexID);
|
|
|
|
glTexImage2D(GL_TEXTURE_2D, 0, GL_DEPTH24_STENCIL8, m_vSize.x, m_vSize.y, 0, GL_DEPTH_STENCIL, GL_UNSIGNED_INT_24_8, 0);
|
|
|
|
|
|
|
|
glBindFramebuffer(GL_FRAMEBUFFER, m_iFb);
|
|
|
|
|
|
|
|
glFramebufferTexture2D(GL_FRAMEBUFFER, GL_STENCIL_ATTACHMENT, GL_TEXTURE_2D, m_pStencilTex->m_iTexID, 0);
|
|
|
|
|
|
|
|
auto status = glCheckFramebufferStatus(GL_FRAMEBUFFER);
|
|
|
|
RASSERT((status == GL_FRAMEBUFFER_COMPLETE), "Failed adding a stencil to fbo!", status);
|
|
|
|
|
|
|
|
glBindTexture(GL_TEXTURE_2D, 0);
|
|
|
|
glBindFramebuffer(GL_FRAMEBUFFER, g_pHyprOpenGL->m_iCurrentOutputFb);
|
2023-12-05 16:41:17 +01:00
|
|
|
#endif
|
2023-11-24 11:54:21 +01:00
|
|
|
}
|
|
|
|
|
2022-04-05 20:49:15 +02:00
|
|
|
void CFramebuffer::bind() {
|
2022-12-16 18:17:31 +01:00
|
|
|
#ifndef GLES2
|
2022-04-05 20:49:15 +02:00
|
|
|
glBindFramebuffer(GL_DRAW_FRAMEBUFFER, m_iFb);
|
2022-12-16 18:17:31 +01:00
|
|
|
#else
|
2022-04-13 17:34:13 +02:00
|
|
|
glBindFramebuffer(GL_FRAMEBUFFER, m_iFb);
|
2022-12-16 18:17:31 +01:00
|
|
|
#endif
|
2024-11-01 16:52:03 +01:00
|
|
|
if (g_pHyprOpenGL)
|
|
|
|
glViewport(0, 0, g_pHyprOpenGL->m_RenderData.pMonitor->vecPixelSize.x, g_pHyprOpenGL->m_RenderData.pMonitor->vecPixelSize.y);
|
|
|
|
else
|
|
|
|
glViewport(0, 0, m_vSize.x, m_vSize.y);
|
2022-04-05 20:49:15 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void CFramebuffer::release() {
|
2024-08-08 21:01:50 +02:00
|
|
|
if (m_iFbAllocated)
|
2022-04-05 20:49:15 +02:00
|
|
|
glDeleteFramebuffers(1, &m_iFb);
|
|
|
|
|
2024-11-01 16:52:03 +01:00
|
|
|
m_cTex.reset();
|
2024-08-08 21:01:50 +02:00
|
|
|
m_iFbAllocated = false;
|
|
|
|
m_vSize = Vector2D();
|
2022-04-05 20:49:15 +02:00
|
|
|
}
|
2022-07-12 23:11:34 +02:00
|
|
|
|
|
|
|
CFramebuffer::~CFramebuffer() {
|
|
|
|
release();
|
2022-09-25 20:07:48 +02:00
|
|
|
}
|
2022-11-06 18:52:09 +01:00
|
|
|
|
|
|
|
bool CFramebuffer::isAllocated() {
|
2024-08-08 21:01:50 +02:00
|
|
|
return m_iFbAllocated;
|
2022-11-06 18:52:09 +01:00
|
|
|
}
|