File indexing completed on 2024-11-10 04:56:54

0001 /*
0002     KWin - the KDE window manager
0003     This file is part of the KDE project.
0004 
0005     SPDX-FileCopyrightText: 2023 Xaver Hugl <xaver.hugl@gmail.com>
0006 
0007     SPDX-License-Identifier: GPL-2.0-or-later
0008 */
0009 #include "gllut.h"
0010 
0011 #include <vector>
0012 
0013 namespace KWin
0014 {
0015 
0016 GlLookUpTable::GlLookUpTable(GLuint handle, size_t size)
0017     : m_handle(handle)
0018     , m_size(size)
0019 {
0020 }
0021 
0022 GlLookUpTable::~GlLookUpTable()
0023 {
0024     glDeleteTextures(1, &m_handle);
0025 }
0026 
0027 GLuint GlLookUpTable::handle() const
0028 {
0029     return m_handle;
0030 }
0031 
0032 size_t GlLookUpTable::size() const
0033 {
0034     return m_size;
0035 }
0036 
0037 void GlLookUpTable::bind()
0038 {
0039     glBindTexture(GL_TEXTURE_2D, m_handle);
0040 }
0041 
0042 std::unique_ptr<GlLookUpTable> GlLookUpTable::create(const std::function<QVector3D(size_t value)> &func, size_t size)
0043 {
0044     GLuint handle = 0;
0045     glGenTextures(1, &handle);
0046     if (!handle) {
0047         return nullptr;
0048     }
0049     // this uses 2D textures because OpenGL ES doesn't support 1D textures
0050     glBindTexture(GL_TEXTURE_2D, handle);
0051     glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAX_LEVEL, 0);
0052     glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_LOD, 0);
0053     glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAX_LOD, 0);
0054     glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
0055     glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
0056     glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
0057     glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
0058     std::vector<float> data;
0059     data.reserve(4 * size);
0060     for (size_t i = 0; i < size; i++) {
0061         const auto color = func(i);
0062         data.push_back(color.x());
0063         data.push_back(color.y());
0064         data.push_back(color.z());
0065         data.push_back(1);
0066     }
0067     glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA16F, size, 1, 0, GL_RGBA, GL_FLOAT, data.data());
0068     glBindTexture(GL_TEXTURE_2D, 0);
0069     return std::make_unique<GlLookUpTable>(handle, size);
0070 }
0071 
0072 }