Skip to content

Commit

Permalink
background: add blurring
Browse files Browse the repository at this point in the history
  • Loading branch information
vaxerski committed Feb 21, 2024
1 parent 2836f02 commit 1b57d52
Show file tree
Hide file tree
Showing 8 changed files with 645 additions and 13 deletions.
14 changes: 14 additions & 0 deletions src/config/ConfigManager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,13 @@ void CConfigManager::init() {
m_config.addSpecialConfigValue("background", "monitor", Hyprlang::STRING{""});
m_config.addSpecialConfigValue("background", "path", Hyprlang::STRING{""});
m_config.addSpecialConfigValue("background", "color", Hyprlang::INT{0xFF111111});
m_config.addSpecialConfigValue("background", "blur_size", Hyprlang::INT{8});
m_config.addSpecialConfigValue("background", "blur_passes", Hyprlang::INT{0});
m_config.addSpecialConfigValue("background", "noise", Hyprlang::FLOAT{0.0117});
m_config.addSpecialConfigValue("background", "contrast", Hyprlang::FLOAT{0.8917});
m_config.addSpecialConfigValue("background", "brightness", Hyprlang::FLOAT{0.8172});
m_config.addSpecialConfigValue("background", "vibrancy", Hyprlang::FLOAT{0.1686});
m_config.addSpecialConfigValue("background", "vibrancy_darkness", Hyprlang::FLOAT{0.05});

m_config.addSpecialCategory("input-field", Hyprlang::SSpecialCategoryOptions{.key = nullptr, .anonymousKeyBased = true});
m_config.addSpecialConfigValue("input-field", "monitor", Hyprlang::STRING{""});
Expand Down Expand Up @@ -83,6 +90,13 @@ std::vector<CConfigManager::SWidgetConfig> CConfigManager::getWidgetConfigs() {
{
{"path", m_config.getSpecialConfigValue("background", "path", k.c_str())},
{"color", m_config.getSpecialConfigValue("background", "color", k.c_str())},
{"blur_size", m_config.getSpecialConfigValue("background", "blur_size", k.c_str())},
{"blur_passes", m_config.getSpecialConfigValue("background", "blur_passes", k.c_str())},
{"noise", m_config.getSpecialConfigValue("background", "noise", k.c_str())},
{"contrast", m_config.getSpecialConfigValue("background", "contrast", k.c_str())},
{"vibrancy", m_config.getSpecialConfigValue("background", "vibrancy", k.c_str())},
{"brightness", m_config.getSpecialConfigValue("background", "brightness", k.c_str())},
{"vibrancy_darkness", m_config.getSpecialConfigValue("background", "vibrancy_darkness", k.c_str())},
}
});
// clang-format on
Expand Down
112 changes: 112 additions & 0 deletions src/renderer/Framebuffer.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,112 @@
#include "Framebuffer.hpp"
#include "../helpers/Log.hpp"
#include <libdrm/drm_fourcc.h>

static uint32_t drmFormatToGL(uint32_t drm) {
switch (drm) {
case DRM_FORMAT_XRGB8888:
case DRM_FORMAT_XBGR8888: return GL_RGBA; // doesn't matter, opengl is gucci in this case.
case DRM_FORMAT_XRGB2101010:
case DRM_FORMAT_XBGR2101010: return GL_RGB10_A2;
default: return GL_RGBA;
}
return GL_RGBA;
}

static uint32_t glFormatToType(uint32_t gl) {
return gl != GL_RGBA ? GL_UNSIGNED_INT_2_10_10_10_REV : GL_UNSIGNED_BYTE;
}

bool CFramebuffer::alloc(int w, int h, uint32_t drmFormat) {
bool firstAlloc = false;

uint32_t glFormat = drmFormatToGL(drmFormat);
uint32_t glType = glFormatToType(glFormat);

if (m_iFb == (uint32_t)-1) {
firstAlloc = true;
glGenFramebuffers(1, &m_iFb);
}

if (m_cTex.m_iTexID == 0) {
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);
}

if (firstAlloc || m_vSize != Vector2D(w, h)) {
glBindTexture(GL_TEXTURE_2D, m_cTex.m_iTexID);
glTexImage2D(GL_TEXTURE_2D, 0, glFormat, w, h, 0, GL_RGBA, glType, 0);

glBindFramebuffer(GL_FRAMEBUFFER, m_iFb);
glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, m_cTex.m_iTexID, 0);

if (m_pStencilTex) {
glBindTexture(GL_TEXTURE_2D, m_pStencilTex->m_iTexID);
glTexImage2D(GL_TEXTURE_2D, 0, GL_DEPTH24_STENCIL8, w, h, 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);
if (status != GL_FRAMEBUFFER_COMPLETE) {
Debug::log(ERR, "Framebuffer incomplete, couldn't create! (FB status: {})", status);
abort();
}

Debug::log(LOG, "Framebuffer created, status {}", status);
}

glBindTexture(GL_TEXTURE_2D, 0);
glBindFramebuffer(GL_FRAMEBUFFER, 0);

m_vSize = Vector2D(w, h);

return true;
}

void CFramebuffer::addStencil() {
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, 0);
}

void CFramebuffer::bind() const {
glBindFramebuffer(GL_DRAW_FRAMEBUFFER, m_iFb);
glViewport(0, 0, m_vSize.x, m_vSize.y);
}

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;
m_vSize = Vector2D();
}

CFramebuffer::~CFramebuffer() {
release();
}

bool CFramebuffer::isAllocated() {
return m_iFb != (GLuint)-1;
}
24 changes: 24 additions & 0 deletions src/renderer/Framebuffer.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
#pragma once

#include "../helpers/Vector2D.hpp"
#include <GLES3/gl32.h>
#include "Texture.hpp"

class CFramebuffer {
public:
~CFramebuffer();

bool alloc(int w, int h, uint32_t format = GL_RGBA);
void addStencil();
void bind() const;
void release();
void reset();
bool isAllocated();

Vector2D m_vSize;

CTexture m_cTex;
GLuint m_iFb = -1;

CTexture* m_pStencilTex = nullptr;
};
Loading

0 comments on commit 1b57d52

Please sign in to comment.