2022-04-05 20:49:15 +02:00
|
|
|
#include "Framebuffer.hpp"
|
|
|
|
|
#include "OpenGL.hpp"
|
2025-06-22 12:40:45 +03:00
|
|
|
#include "../config/ConfigValue.hpp"
|
2022-04-05 20:49:15 +02:00
|
|
|
|
2024-06-08 10:07:59 +02:00
|
|
|
CFramebuffer::CFramebuffer() {
|
2024-11-01 15:52:03 +00:00
|
|
|
;
|
2024-06-08 10:07:59 +02:00
|
|
|
}
|
|
|
|
|
|
2023-10-30 15:56:02 +00:00
|
|
|
bool CFramebuffer::alloc(int w, int h, uint32_t drmFormat) {
|
2022-07-12 23:11:34 +02:00
|
|
|
bool firstAlloc = false;
|
2025-04-13 22:40:15 +02:00
|
|
|
RASSERT((w > 0 && h > 0), "cannot alloc a FB with negative / zero size! (attempted {}x{})", w, h);
|
2022-09-25 20:07:48 +02:00
|
|
|
|
2025-06-22 12:40:45 +03:00
|
|
|
static auto PFP16 = CConfigValue<Hyprlang::INT>("render:send_content_type");
|
|
|
|
|
|
|
|
|
|
uint32_t glFormat = NFormatUtils::drmFormatToGL(drmFormat);
|
|
|
|
|
uint32_t glType = NFormatUtils::glFormatToType(glFormat);
|
2023-10-30 15:56:02 +00:00
|
|
|
|
2025-05-09 22:16:21 +01:00
|
|
|
if (drmFormat != m_drmFormat || m_size != Vector2D{w, h})
|
|
|
|
|
release();
|
|
|
|
|
|
|
|
|
|
m_drmFormat = drmFormat;
|
|
|
|
|
|
2025-05-05 23:44:49 +02:00
|
|
|
if (!m_tex) {
|
|
|
|
|
m_tex = makeShared<CTexture>();
|
|
|
|
|
m_tex->allocate();
|
renderer: reduce a lot of glcalls and cache various states (#10757)
* opengl: cache viewport state
according to nvidia docs calling glViewPort unnecessarily on the same
already set viewport is wasteful and can cause state changes when not
needed. cache it in a struct and only call it when the viewport is
actually changing.
* opengl: cache glenable/gldisable state
avoid making multiple glenable/gldisable calls on already set caps, can
cause state changes and incur driver overhead.
* opengl: cache glscissor box
only call glscissor if the box actually has changed, try to avoid state
changes.
* opengl: cache gluniform calls
cache the gluniform calls, the uniform values are cached in driver per
program only the drawcalls setting the uniform yet again with the same
value on same location is causing more overhead then caching it ourself
and just no oping on it if no changes.
* shader: rewrite handling of uniforms and state
this is way faster as we don't need to mess with maps (hashing, etc) and instead can just use an array
* opengl: stuff and 300 shaders
* opengl: typo
* opengl: get the uniform locations properly
now that the legacy shaders are gone get the uniformlocations for
SKIP_CM etc, so they can be properly set and used depending on if
cm_enabled is set to false or true, before it was falling back to a
legacy shader that didnt even have those uniforms.
* opengl: check epsilon on float and remove extra glcall
seems an extra unset glcall was added, remove it. and check the float
epsilon on the glfloat.
* opengl: remove instanced shader draw
remove the instanced boolean from the vertex shader, might be neglible
differences, needs more benchmark/work to see if its even worth it.
* texture: cache texture paramaters
parameters where occasionally set twice or more on same texture, short
version wrap it and cache it. and move gpu churn to cpu churn.
add a bind/unbind to texture aswell.
* texture: use fast std::array caching
cache the texparameter values in fast array lookups
and incase we dont want it cached, apply it anyways.
* shader: fix typo and hdr typo
actually use Matrix4x2fv in the 4x2fv cache function, and send the
proper float array for hdr.
* texture: make caching not linear lookup
make caching of texture params not linear.
* minor style changes
* opengl: revert drawarrays
revert the mostly code style reduce loc change of drawarrays, and focus
on the caching. its a if else case going wrong here breaking
blur/contrast amongst others drawing.
---------
Co-authored-by: Vaxry <vaxry@vaxry.net>
2025-06-25 12:42:32 +02:00
|
|
|
m_tex->bind();
|
|
|
|
|
m_tex->setTexParameter(GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
|
|
|
|
|
m_tex->setTexParameter(GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
|
|
|
|
|
m_tex->setTexParameter(GL_TEXTURE_MAG_FILTER, GL_LINEAR);
|
|
|
|
|
m_tex->setTexParameter(GL_TEXTURE_MIN_FILTER, GL_LINEAR);
|
2025-02-08 01:46:26 +01:00
|
|
|
firstAlloc = true;
|
|
|
|
|
}
|
|
|
|
|
|
2025-05-05 23:44:49 +02:00
|
|
|
if (!m_fbAllocated) {
|
|
|
|
|
glGenFramebuffers(1, &m_fb);
|
|
|
|
|
m_fbAllocated = true;
|
|
|
|
|
firstAlloc = true;
|
2022-04-05 20:49:15 +02:00
|
|
|
}
|
|
|
|
|
|
2025-05-05 23:44:49 +02:00
|
|
|
if (firstAlloc || m_size != Vector2D(w, h)) {
|
renderer: reduce a lot of glcalls and cache various states (#10757)
* opengl: cache viewport state
according to nvidia docs calling glViewPort unnecessarily on the same
already set viewport is wasteful and can cause state changes when not
needed. cache it in a struct and only call it when the viewport is
actually changing.
* opengl: cache glenable/gldisable state
avoid making multiple glenable/gldisable calls on already set caps, can
cause state changes and incur driver overhead.
* opengl: cache glscissor box
only call glscissor if the box actually has changed, try to avoid state
changes.
* opengl: cache gluniform calls
cache the gluniform calls, the uniform values are cached in driver per
program only the drawcalls setting the uniform yet again with the same
value on same location is causing more overhead then caching it ourself
and just no oping on it if no changes.
* shader: rewrite handling of uniforms and state
this is way faster as we don't need to mess with maps (hashing, etc) and instead can just use an array
* opengl: stuff and 300 shaders
* opengl: typo
* opengl: get the uniform locations properly
now that the legacy shaders are gone get the uniformlocations for
SKIP_CM etc, so they can be properly set and used depending on if
cm_enabled is set to false or true, before it was falling back to a
legacy shader that didnt even have those uniforms.
* opengl: check epsilon on float and remove extra glcall
seems an extra unset glcall was added, remove it. and check the float
epsilon on the glfloat.
* opengl: remove instanced shader draw
remove the instanced boolean from the vertex shader, might be neglible
differences, needs more benchmark/work to see if its even worth it.
* texture: cache texture paramaters
parameters where occasionally set twice or more on same texture, short
version wrap it and cache it. and move gpu churn to cpu churn.
add a bind/unbind to texture aswell.
* texture: use fast std::array caching
cache the texparameter values in fast array lookups
and incase we dont want it cached, apply it anyways.
* shader: fix typo and hdr typo
actually use Matrix4x2fv in the 4x2fv cache function, and send the
proper float array for hdr.
* texture: make caching not linear lookup
make caching of texture params not linear.
* minor style changes
* opengl: revert drawarrays
revert the mostly code style reduce loc change of drawarrays, and focus
on the caching. its a if else case going wrong here breaking
blur/contrast amongst others drawing.
---------
Co-authored-by: Vaxry <vaxry@vaxry.net>
2025-06-25 12:42:32 +02:00
|
|
|
m_tex->bind();
|
2025-06-22 12:40:45 +03:00
|
|
|
glTexImage2D(GL_TEXTURE_2D, 0, *PFP16 ? GL_RGBA16F : glFormat, w, h, 0, GL_RGBA, glType, nullptr);
|
2025-05-05 23:44:49 +02:00
|
|
|
glBindFramebuffer(GL_FRAMEBUFFER, m_fb);
|
|
|
|
|
glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, m_tex->m_texID, 0);
|
2022-04-05 20:49:15 +02:00
|
|
|
|
2025-05-05 23:44:49 +02:00
|
|
|
if (m_stencilTex) {
|
renderer: reduce a lot of glcalls and cache various states (#10757)
* opengl: cache viewport state
according to nvidia docs calling glViewPort unnecessarily on the same
already set viewport is wasteful and can cause state changes when not
needed. cache it in a struct and only call it when the viewport is
actually changing.
* opengl: cache glenable/gldisable state
avoid making multiple glenable/gldisable calls on already set caps, can
cause state changes and incur driver overhead.
* opengl: cache glscissor box
only call glscissor if the box actually has changed, try to avoid state
changes.
* opengl: cache gluniform calls
cache the gluniform calls, the uniform values are cached in driver per
program only the drawcalls setting the uniform yet again with the same
value on same location is causing more overhead then caching it ourself
and just no oping on it if no changes.
* shader: rewrite handling of uniforms and state
this is way faster as we don't need to mess with maps (hashing, etc) and instead can just use an array
* opengl: stuff and 300 shaders
* opengl: typo
* opengl: get the uniform locations properly
now that the legacy shaders are gone get the uniformlocations for
SKIP_CM etc, so they can be properly set and used depending on if
cm_enabled is set to false or true, before it was falling back to a
legacy shader that didnt even have those uniforms.
* opengl: check epsilon on float and remove extra glcall
seems an extra unset glcall was added, remove it. and check the float
epsilon on the glfloat.
* opengl: remove instanced shader draw
remove the instanced boolean from the vertex shader, might be neglible
differences, needs more benchmark/work to see if its even worth it.
* texture: cache texture paramaters
parameters where occasionally set twice or more on same texture, short
version wrap it and cache it. and move gpu churn to cpu churn.
add a bind/unbind to texture aswell.
* texture: use fast std::array caching
cache the texparameter values in fast array lookups
and incase we dont want it cached, apply it anyways.
* shader: fix typo and hdr typo
actually use Matrix4x2fv in the 4x2fv cache function, and send the
proper float array for hdr.
* texture: make caching not linear lookup
make caching of texture params not linear.
* minor style changes
* opengl: revert drawarrays
revert the mostly code style reduce loc change of drawarrays, and focus
on the caching. its a if else case going wrong here breaking
blur/contrast amongst others drawing.
---------
Co-authored-by: Vaxry <vaxry@vaxry.net>
2025-06-25 12:42:32 +02:00
|
|
|
m_stencilTex->bind();
|
2024-12-07 18:51:18 +01:00
|
|
|
glTexImage2D(GL_TEXTURE_2D, 0, GL_DEPTH24_STENCIL8, w, h, 0, GL_DEPTH_STENCIL, GL_UNSIGNED_INT_24_8, nullptr);
|
2025-05-05 23:44:49 +02:00
|
|
|
glFramebufferTexture2D(GL_FRAMEBUFFER, GL_STENCIL_ATTACHMENT, GL_TEXTURE_2D, m_stencilTex->m_texID, 0);
|
2022-04-11 16:39:48 +02:00
|
|
|
}
|
|
|
|
|
|
2022-04-05 20:49:15 +02:00
|
|
|
auto status = glCheckFramebufferStatus(GL_FRAMEBUFFER);
|
2025-08-14 19:44:56 +05:00
|
|
|
RASSERT((status == GL_FRAMEBUFFER_COMPLETE), "Framebuffer incomplete, couldn't create! (FB status: {}, GL Error: 0x{:x})", status, sc<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);
|
2025-02-08 01:46:26 +01:00
|
|
|
glBindFramebuffer(GL_FRAMEBUFFER, 0);
|
2022-04-05 20:49:15 +02:00
|
|
|
|
2025-05-05 23:44:49 +02:00
|
|
|
m_size = Vector2D(w, h);
|
2022-04-05 20:49:15 +02:00
|
|
|
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
2024-11-02 15:26:25 +00:00
|
|
|
void CFramebuffer::addStencil(SP<CTexture> tex) {
|
2025-05-05 23:44:49 +02:00
|
|
|
m_stencilTex = tex;
|
renderer: reduce a lot of glcalls and cache various states (#10757)
* opengl: cache viewport state
according to nvidia docs calling glViewPort unnecessarily on the same
already set viewport is wasteful and can cause state changes when not
needed. cache it in a struct and only call it when the viewport is
actually changing.
* opengl: cache glenable/gldisable state
avoid making multiple glenable/gldisable calls on already set caps, can
cause state changes and incur driver overhead.
* opengl: cache glscissor box
only call glscissor if the box actually has changed, try to avoid state
changes.
* opengl: cache gluniform calls
cache the gluniform calls, the uniform values are cached in driver per
program only the drawcalls setting the uniform yet again with the same
value on same location is causing more overhead then caching it ourself
and just no oping on it if no changes.
* shader: rewrite handling of uniforms and state
this is way faster as we don't need to mess with maps (hashing, etc) and instead can just use an array
* opengl: stuff and 300 shaders
* opengl: typo
* opengl: get the uniform locations properly
now that the legacy shaders are gone get the uniformlocations for
SKIP_CM etc, so they can be properly set and used depending on if
cm_enabled is set to false or true, before it was falling back to a
legacy shader that didnt even have those uniforms.
* opengl: check epsilon on float and remove extra glcall
seems an extra unset glcall was added, remove it. and check the float
epsilon on the glfloat.
* opengl: remove instanced shader draw
remove the instanced boolean from the vertex shader, might be neglible
differences, needs more benchmark/work to see if its even worth it.
* texture: cache texture paramaters
parameters where occasionally set twice or more on same texture, short
version wrap it and cache it. and move gpu churn to cpu churn.
add a bind/unbind to texture aswell.
* texture: use fast std::array caching
cache the texparameter values in fast array lookups
and incase we dont want it cached, apply it anyways.
* shader: fix typo and hdr typo
actually use Matrix4x2fv in the 4x2fv cache function, and send the
proper float array for hdr.
* texture: make caching not linear lookup
make caching of texture params not linear.
* minor style changes
* opengl: revert drawarrays
revert the mostly code style reduce loc change of drawarrays, and focus
on the caching. its a if else case going wrong here breaking
blur/contrast amongst others drawing.
---------
Co-authored-by: Vaxry <vaxry@vaxry.net>
2025-06-25 12:42:32 +02:00
|
|
|
m_stencilTex->bind();
|
2025-05-05 23:44:49 +02:00
|
|
|
glTexImage2D(GL_TEXTURE_2D, 0, GL_DEPTH24_STENCIL8, m_size.x, m_size.y, 0, GL_DEPTH_STENCIL, GL_UNSIGNED_INT_24_8, nullptr);
|
2023-11-24 10:54:21 +00:00
|
|
|
|
2025-05-05 23:44:49 +02:00
|
|
|
glBindFramebuffer(GL_FRAMEBUFFER, m_fb);
|
2023-11-24 10:54:21 +00:00
|
|
|
|
2025-05-05 23:44:49 +02:00
|
|
|
glFramebufferTexture2D(GL_FRAMEBUFFER, GL_STENCIL_ATTACHMENT, GL_TEXTURE_2D, m_stencilTex->m_texID, 0);
|
2023-11-24 10:54:21 +00:00
|
|
|
|
|
|
|
|
auto status = glCheckFramebufferStatus(GL_FRAMEBUFFER);
|
|
|
|
|
RASSERT((status == GL_FRAMEBUFFER_COMPLETE), "Failed adding a stencil to fbo!", status);
|
|
|
|
|
|
renderer: reduce a lot of glcalls and cache various states (#10757)
* opengl: cache viewport state
according to nvidia docs calling glViewPort unnecessarily on the same
already set viewport is wasteful and can cause state changes when not
needed. cache it in a struct and only call it when the viewport is
actually changing.
* opengl: cache glenable/gldisable state
avoid making multiple glenable/gldisable calls on already set caps, can
cause state changes and incur driver overhead.
* opengl: cache glscissor box
only call glscissor if the box actually has changed, try to avoid state
changes.
* opengl: cache gluniform calls
cache the gluniform calls, the uniform values are cached in driver per
program only the drawcalls setting the uniform yet again with the same
value on same location is causing more overhead then caching it ourself
and just no oping on it if no changes.
* shader: rewrite handling of uniforms and state
this is way faster as we don't need to mess with maps (hashing, etc) and instead can just use an array
* opengl: stuff and 300 shaders
* opengl: typo
* opengl: get the uniform locations properly
now that the legacy shaders are gone get the uniformlocations for
SKIP_CM etc, so they can be properly set and used depending on if
cm_enabled is set to false or true, before it was falling back to a
legacy shader that didnt even have those uniforms.
* opengl: check epsilon on float and remove extra glcall
seems an extra unset glcall was added, remove it. and check the float
epsilon on the glfloat.
* opengl: remove instanced shader draw
remove the instanced boolean from the vertex shader, might be neglible
differences, needs more benchmark/work to see if its even worth it.
* texture: cache texture paramaters
parameters where occasionally set twice or more on same texture, short
version wrap it and cache it. and move gpu churn to cpu churn.
add a bind/unbind to texture aswell.
* texture: use fast std::array caching
cache the texparameter values in fast array lookups
and incase we dont want it cached, apply it anyways.
* shader: fix typo and hdr typo
actually use Matrix4x2fv in the 4x2fv cache function, and send the
proper float array for hdr.
* texture: make caching not linear lookup
make caching of texture params not linear.
* minor style changes
* opengl: revert drawarrays
revert the mostly code style reduce loc change of drawarrays, and focus
on the caching. its a if else case going wrong here breaking
blur/contrast amongst others drawing.
---------
Co-authored-by: Vaxry <vaxry@vaxry.net>
2025-06-25 12:42:32 +02:00
|
|
|
m_stencilTex->unbind();
|
2025-02-08 01:46:26 +01:00
|
|
|
glBindFramebuffer(GL_FRAMEBUFFER, 0);
|
2023-11-24 10:54:21 +00:00
|
|
|
}
|
|
|
|
|
|
2022-04-05 20:49:15 +02:00
|
|
|
void CFramebuffer::bind() {
|
2025-05-05 23:44:49 +02:00
|
|
|
glBindFramebuffer(GL_DRAW_FRAMEBUFFER, m_fb);
|
2025-02-08 01:46:26 +01:00
|
|
|
|
2024-11-01 15:52:03 +00:00
|
|
|
if (g_pHyprOpenGL)
|
renderer: reduce a lot of glcalls and cache various states (#10757)
* opengl: cache viewport state
according to nvidia docs calling glViewPort unnecessarily on the same
already set viewport is wasteful and can cause state changes when not
needed. cache it in a struct and only call it when the viewport is
actually changing.
* opengl: cache glenable/gldisable state
avoid making multiple glenable/gldisable calls on already set caps, can
cause state changes and incur driver overhead.
* opengl: cache glscissor box
only call glscissor if the box actually has changed, try to avoid state
changes.
* opengl: cache gluniform calls
cache the gluniform calls, the uniform values are cached in driver per
program only the drawcalls setting the uniform yet again with the same
value on same location is causing more overhead then caching it ourself
and just no oping on it if no changes.
* shader: rewrite handling of uniforms and state
this is way faster as we don't need to mess with maps (hashing, etc) and instead can just use an array
* opengl: stuff and 300 shaders
* opengl: typo
* opengl: get the uniform locations properly
now that the legacy shaders are gone get the uniformlocations for
SKIP_CM etc, so they can be properly set and used depending on if
cm_enabled is set to false or true, before it was falling back to a
legacy shader that didnt even have those uniforms.
* opengl: check epsilon on float and remove extra glcall
seems an extra unset glcall was added, remove it. and check the float
epsilon on the glfloat.
* opengl: remove instanced shader draw
remove the instanced boolean from the vertex shader, might be neglible
differences, needs more benchmark/work to see if its even worth it.
* texture: cache texture paramaters
parameters where occasionally set twice or more on same texture, short
version wrap it and cache it. and move gpu churn to cpu churn.
add a bind/unbind to texture aswell.
* texture: use fast std::array caching
cache the texparameter values in fast array lookups
and incase we dont want it cached, apply it anyways.
* shader: fix typo and hdr typo
actually use Matrix4x2fv in the 4x2fv cache function, and send the
proper float array for hdr.
* texture: make caching not linear lookup
make caching of texture params not linear.
* minor style changes
* opengl: revert drawarrays
revert the mostly code style reduce loc change of drawarrays, and focus
on the caching. its a if else case going wrong here breaking
blur/contrast amongst others drawing.
---------
Co-authored-by: Vaxry <vaxry@vaxry.net>
2025-06-25 12:42:32 +02:00
|
|
|
g_pHyprOpenGL->setViewport(0, 0, g_pHyprOpenGL->m_renderData.pMonitor->m_pixelSize.x, g_pHyprOpenGL->m_renderData.pMonitor->m_pixelSize.y);
|
2024-11-01 15:52:03 +00:00
|
|
|
else
|
2025-05-05 23:44:49 +02:00
|
|
|
glViewport(0, 0, m_size.x, m_size.y);
|
2022-04-05 20:49:15 +02:00
|
|
|
}
|
|
|
|
|
|
2025-02-08 01:46:26 +01:00
|
|
|
void CFramebuffer::unbind() {
|
|
|
|
|
glBindFramebuffer(GL_DRAW_FRAMEBUFFER, 0);
|
|
|
|
|
}
|
2024-11-03 15:16:08 +00:00
|
|
|
|
2025-02-08 01:46:26 +01:00
|
|
|
void CFramebuffer::release() {
|
2025-05-05 23:44:49 +02:00
|
|
|
if (m_fbAllocated) {
|
|
|
|
|
glBindFramebuffer(GL_FRAMEBUFFER, m_fb);
|
2025-02-08 01:46:26 +01:00
|
|
|
glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, 0, 0);
|
|
|
|
|
glBindFramebuffer(GL_FRAMEBUFFER, 0);
|
2024-11-03 15:16:08 +00:00
|
|
|
|
2025-05-05 23:44:49 +02:00
|
|
|
glDeleteFramebuffers(1, &m_fb);
|
|
|
|
|
m_fbAllocated = false;
|
|
|
|
|
m_fb = 0;
|
2025-02-08 01:46:26 +01:00
|
|
|
}
|
|
|
|
|
|
2025-05-05 23:44:49 +02:00
|
|
|
if (m_tex)
|
|
|
|
|
m_tex.reset();
|
2022-04-05 20:49:15 +02:00
|
|
|
|
2025-05-05 23:44:49 +02:00
|
|
|
m_size = 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 17:52:09 +00:00
|
|
|
|
|
|
|
|
bool CFramebuffer::isAllocated() {
|
2025-05-05 23:44:49 +02:00
|
|
|
return m_fbAllocated && m_tex;
|
2024-11-02 15:26:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
SP<CTexture> CFramebuffer::getTexture() {
|
2025-05-05 23:44:49 +02:00
|
|
|
return m_tex;
|
2024-11-02 15:26:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
GLuint CFramebuffer::getFBID() {
|
2025-05-05 23:44:49 +02:00
|
|
|
return m_fbAllocated ? m_fb : 0;
|
2024-11-02 15:26:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
SP<CTexture> CFramebuffer::getStencilTex() {
|
2025-05-05 23:44:49 +02:00
|
|
|
return m_stencilTex;
|
2024-11-02 15:26:25 +00:00
|
|
|
}
|