From 0429ec8053550c8f7a1994532777d5646a48dadb Mon Sep 17 00:00:00 2001 From: Xaver Hugl Date: Thu, 12 Oct 2023 15:23:07 +0200 Subject: [PATCH] libkwineffects: add a 3d lookup table class --- src/CMakeLists.txt | 2 + src/libkwineffects/gllut3D.cpp | 88 ++++++++++++++++++++++++++++++++++ src/libkwineffects/gllut3D.h | 44 +++++++++++++++++ 3 files changed, 134 insertions(+) create mode 100644 src/libkwineffects/gllut3D.cpp create mode 100644 src/libkwineffects/gllut3D.h diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt index 8df1e0a5d7..84f61445de 100644 --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -110,6 +110,7 @@ target_sources(kwin PRIVATE libkwineffects/effecttogglablestate.cpp libkwineffects/glframebuffer.cpp libkwineffects/gllut.cpp + libkwineffects/gllut3D.cpp libkwineffects/glplatform.cpp libkwineffects/glshader.cpp libkwineffects/glshadermanager.cpp @@ -432,6 +433,7 @@ install(FILES libkwineffects/colorspace.h libkwineffects/glframebuffer.h libkwineffects/gllut.h + libkwineffects/gllut3D.h libkwineffects/glplatform.h libkwineffects/glshader.h libkwineffects/glshadermanager.h diff --git a/src/libkwineffects/gllut3D.cpp b/src/libkwineffects/gllut3D.cpp new file mode 100644 index 0000000000..828152f1e0 --- /dev/null +++ b/src/libkwineffects/gllut3D.cpp @@ -0,0 +1,88 @@ +/* + KWin - the KDE window manager + This file is part of the KDE project. + + SPDX-FileCopyrightText: 2023 Xaver Hugl + + SPDX-License-Identifier: GPL-2.0-or-later +*/ +#include "gllut3D.h" + +#include + +namespace KWin +{ + +GlLookUpTable3D::GlLookUpTable3D(GLuint handle, size_t xSize, size_t ySize, size_t zSize) + : m_handle(handle) + , m_xSize(xSize) + , m_ySize(ySize) + , m_zSize(zSize) +{ +} + +GlLookUpTable3D::~GlLookUpTable3D() +{ + glDeleteTextures(1, &m_handle); +} + +GLuint GlLookUpTable3D::handle() const +{ + return m_handle; +} + +size_t GlLookUpTable3D::xSize() const +{ + return m_xSize; +} + +size_t GlLookUpTable3D::ySize() const +{ + return m_ySize; +} + +size_t GlLookUpTable3D::zSize() const +{ + return m_zSize; +} + +void GlLookUpTable3D::bind() +{ + glBindTexture(GL_TEXTURE_3D, m_handle); +} + +std::unique_ptr GlLookUpTable3D::create(const std::function &mapping, size_t xSize, size_t ySize, size_t zSize) +{ + GLuint handle = 0; + glGenTextures(1, &handle); + if (!handle) { + return nullptr; + } + glBindTexture(GL_TEXTURE_3D, handle); + glTexParameteri(GL_TEXTURE_3D, GL_TEXTURE_MAX_LEVEL, 0); + glTexParameteri(GL_TEXTURE_3D, GL_TEXTURE_MIN_LOD, 0); + glTexParameteri(GL_TEXTURE_3D, GL_TEXTURE_MAX_LOD, 0); + glTexParameteri(GL_TEXTURE_3D, GL_TEXTURE_MIN_FILTER, GL_LINEAR); + glTexParameteri(GL_TEXTURE_3D, GL_TEXTURE_MAG_FILTER, GL_LINEAR); + glTexParameteri(GL_TEXTURE_3D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE); + glTexParameteri(GL_TEXTURE_3D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE); + glTexParameteri(GL_TEXTURE_3D, GL_TEXTURE_WRAP_R, GL_CLAMP_TO_EDGE); + QVector data; + data.reserve(4 * xSize * ySize * zSize); + for (size_t z = 0; z < zSize; z++) { + for (size_t y = 0; y < ySize; y++) { + for (size_t x = 0; x < xSize; x++) { + const auto color = mapping(x, y, z); + data.push_back(color.x()); + data.push_back(color.y()); + data.push_back(color.z()); + data.push_back(1); + } + } + } + glTexImage3D(GL_TEXTURE_3D, 0, GL_RGBA16F, xSize, ySize, zSize, 0, GL_RGBA, GL_FLOAT, data.data()); + glBindTexture(GL_TEXTURE_3D, 0); + return std::make_unique(handle, xSize, ySize, zSize); +} + +} diff --git a/src/libkwineffects/gllut3D.h b/src/libkwineffects/gllut3D.h new file mode 100644 index 0000000000..569c1040c9 --- /dev/null +++ b/src/libkwineffects/gllut3D.h @@ -0,0 +1,44 @@ +/* + KWin - the KDE window manager + This file is part of the KDE project. + + SPDX-FileCopyrightText: 2023 Xaver Hugl + + SPDX-License-Identifier: GPL-2.0-or-later +*/ +#pragma once + +#include "kwin_export.h" + +#include +#include +#include +#include +#include + +namespace KWin +{ + +class KWIN_EXPORT GlLookUpTable3D +{ +public: + explicit GlLookUpTable3D(GLuint handle, size_t xSize, size_t ySize, size_t zSize); + ~GlLookUpTable3D(); + + GLuint handle() const; + size_t xSize() const; + size_t ySize() const; + size_t zSize() const; + + void bind(); + + static std::unique_ptr create(const std::function &mapping, size_t xSize, size_t ySize, size_t zSize); + +private: + const GLuint m_handle; + const size_t m_xSize; + const size_t m_ySize; + const size_t m_zSize; +}; + +}