|
| 1 | +/* |
| 2 | + * Stellarium |
| 3 | + * Copyright (C) 2023 Ruslan Kabatsayev |
| 4 | + * |
| 5 | + * This program is free software; you can redistribute it and/or |
| 6 | + * modify it under the terms of the GNU General Public License |
| 7 | + * as published by the Free Software Foundation; either version 2 |
| 8 | + * of the License, or (at your option) any later version. |
| 9 | + * |
| 10 | + * This program is distributed in the hope that it will be useful, |
| 11 | + * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 12 | + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 13 | + * GNU General Public License for more details. |
| 14 | + * |
| 15 | + * You should have received a copy of the GNU General Public License |
| 16 | + * along with this program; if not, write to the Free Software |
| 17 | + * Foundation, Inc., 51 Franklin Street, Suite 500, Boston, MA 02110-1335, USA. |
| 18 | + */ |
| 19 | + |
| 20 | +#include "TextureAverageComputer.hpp" |
| 21 | + |
| 22 | +#if !QT_CONFIG(opengles2) // This class uses glGetTexImage(), which is not supported in GLES2 |
| 23 | + |
| 24 | +#include <QOpenGLFunctions_3_3_Core> |
| 25 | + |
| 26 | +namespace |
| 27 | +{ |
| 28 | + |
| 29 | +int roundDownToClosestPowerOfTwo(const int x) |
| 30 | +{ |
| 31 | + if(x==0) return 1; |
| 32 | + int shift=0; |
| 33 | + for(auto v=x;v;v>>=1) |
| 34 | + ++shift; |
| 35 | + return 1<<(shift-1); |
| 36 | +} |
| 37 | + |
| 38 | +} |
| 39 | + |
| 40 | +Vec4f TextureAverageComputer::getTextureAverageSimple(const GLuint texture, const int width, const int height) |
| 41 | +{ |
| 42 | + // Get average value of the pixels as the value of the deepest mipmap level |
| 43 | + gl.glActiveTexture(GL_TEXTURE0); |
| 44 | + gl.glBindTexture(GL_TEXTURE_2D, texture); |
| 45 | + gl.glGenerateMipmap(GL_TEXTURE_2D); |
| 46 | + |
| 47 | + using namespace std; |
| 48 | + // Formula from the glspec, "Mipmapping" subsection in section 3.8.11 Texture Minification |
| 49 | + const auto totalMipmapLevels = 1+floor(log2(max(width,height))); |
| 50 | + const auto deepestLevel=totalMipmapLevels-1; |
| 51 | + |
| 52 | +#ifndef NDEBUG |
| 53 | + // Sanity check |
| 54 | + int deepestMipmapLevelWidth=-1, deepestMipmapLevelHeight=-1; |
| 55 | + gl.glGetTexLevelParameteriv(GL_TEXTURE_2D, deepestLevel, GL_TEXTURE_WIDTH, &deepestMipmapLevelWidth); |
| 56 | + gl.glGetTexLevelParameteriv(GL_TEXTURE_2D, deepestLevel, GL_TEXTURE_HEIGHT, &deepestMipmapLevelHeight); |
| 57 | + assert(deepestMipmapLevelWidth==1); |
| 58 | + assert(deepestMipmapLevelHeight==1); |
| 59 | +#endif |
| 60 | + |
| 61 | + Vec4f pixel; |
| 62 | + gl.glGetTexImage(GL_TEXTURE_2D, deepestLevel, GL_RGBA, GL_FLOAT, &pixel[0]); |
| 63 | + return pixel; |
| 64 | +} |
| 65 | + |
| 66 | +// Clobbers: |
| 67 | +// GL_VERTEX_ARRAY_BINDING, GL_ACTIVE_TEXTURE, GL_TEXTURE_BINDING_2D, GL_CURRENT_PROGRAM, |
| 68 | +// input texture's minification filter |
| 69 | +Vec4f TextureAverageComputer::getTextureAverageWithWorkaround(const GLuint texture) |
| 70 | +{ |
| 71 | + // Play it safe: we don't want to make the GPU struggle with very large textures |
| 72 | + // if we happen to make them ~4 times larger. Instead round the dimensions down. |
| 73 | + const auto potWidth = roundDownToClosestPowerOfTwo(npotWidth); |
| 74 | + const auto potHeight = roundDownToClosestPowerOfTwo(npotHeight); |
| 75 | + |
| 76 | + gl.glActiveTexture(GL_TEXTURE0); |
| 77 | + gl.glBindTexture(GL_TEXTURE_2D, texture); |
| 78 | + gl.glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR); |
| 79 | + |
| 80 | + blitTexProgram->bind(); |
| 81 | + |
| 82 | + GLint oldViewport[4]; |
| 83 | + gl.glGetIntegerv(GL_VIEWPORT, oldViewport); |
| 84 | + GLint oldFBO=-1; |
| 85 | + gl.glGetIntegerv(GL_DRAW_FRAMEBUFFER_BINDING, &oldFBO); |
| 86 | + |
| 87 | + gl.glBindFramebuffer(GL_FRAMEBUFFER, potFBO); |
| 88 | + gl.glViewport(0,0,potWidth,potHeight); |
| 89 | + |
| 90 | + gl.glBindVertexArray(vao); |
| 91 | + gl.glDrawArrays(GL_TRIANGLE_STRIP, 0, 4); |
| 92 | + gl.glBindVertexArray(0); |
| 93 | + |
| 94 | + gl.glViewport(oldViewport[0], oldViewport[1], oldViewport[2], oldViewport[3]); |
| 95 | + gl.glBindFramebuffer(GL_FRAMEBUFFER, oldFBO); |
| 96 | + |
| 97 | + blitTexProgram->release(); |
| 98 | + |
| 99 | + return getTextureAverageSimple(potTex, potWidth, potHeight); |
| 100 | +} |
| 101 | + |
| 102 | +Vec4f TextureAverageComputer::getTextureAverage(const GLuint texture) |
| 103 | +{ |
| 104 | + if(workaroundNeeded) |
| 105 | + return getTextureAverageWithWorkaround(texture); |
| 106 | + return getTextureAverageSimple(texture, npotWidth, npotHeight); |
| 107 | +} |
| 108 | + |
| 109 | +void TextureAverageComputer::init() |
| 110 | +{ |
| 111 | + GLuint texture = -1; |
| 112 | + gl.glGenTextures(1, &texture); |
| 113 | + assert(texture>0); |
| 114 | + gl.glActiveTexture(GL_TEXTURE0); |
| 115 | + gl.glBindTexture(GL_TEXTURE_2D, texture); |
| 116 | + |
| 117 | + std::vector<Vec4f> data; |
| 118 | + for(int n=0; n<10; ++n) |
| 119 | + data.emplace_back(1,1,1,1); |
| 120 | + for(int n=0; n<10; ++n) |
| 121 | + data.emplace_back(1,1,1,0); |
| 122 | + for(int n=0; n<10; ++n) |
| 123 | + data.emplace_back(1,1,0,0); |
| 124 | + for(int n=0; n<10; ++n) |
| 125 | + data.emplace_back(1,0,0,0); |
| 126 | + |
| 127 | + constexpr int width = 63; |
| 128 | + for(int n=data.size(); n<width; ++n) |
| 129 | + data.emplace_back(0,0,0,0); |
| 130 | + |
| 131 | + gl.glTexImage2D(GL_TEXTURE_2D,0,GL_RGBA8,data.size(),1,0,GL_RGBA,GL_FLOAT,&data[0][0]); |
| 132 | + const auto mipmapAverage = getTextureAverageSimple(texture, width, 1); |
| 133 | + |
| 134 | + const auto sum = std::accumulate(data.begin(), data.end(), Vec4f(0,0,0,0)); |
| 135 | + const auto trueAverage = sum / float(data.size()); |
| 136 | + qDebug().nospace() << "Test texture true average: " |
| 137 | + << trueAverage[0] << ", " |
| 138 | + << trueAverage[1] << ", " |
| 139 | + << trueAverage[2] << ", " |
| 140 | + << trueAverage[3]; |
| 141 | + qDebug().nospace() << "Test texture mipmap average: " |
| 142 | + << mipmapAverage[0] << ", " |
| 143 | + << mipmapAverage[1] << ", " |
| 144 | + << mipmapAverage[2] << ", " |
| 145 | + << mipmapAverage[3]; |
| 146 | + |
| 147 | + const auto diff = mipmapAverage - trueAverage; |
| 148 | + using std::abs; |
| 149 | + const auto maxDiff = std::max({abs(diff[0]),abs(diff[1]),abs(diff[2]),abs(diff[3])}); |
| 150 | + workaroundNeeded = maxDiff >= 2./255.; |
| 151 | + |
| 152 | + if(workaroundNeeded) |
| 153 | + { |
| 154 | + qDebug() << "Mipmap average is unusable, will resize textures to " |
| 155 | + "power-of-two size when average value is required."; |
| 156 | + } |
| 157 | + else |
| 158 | + { |
| 159 | + qDebug() << "Mipmap average works correctly"; |
| 160 | + } |
| 161 | + |
| 162 | + gl.glBindTexture(GL_TEXTURE_2D, 0); |
| 163 | + gl.glDeleteTextures(1, &texture); |
| 164 | + |
| 165 | + inited = true; |
| 166 | +} |
| 167 | + |
| 168 | +// Clobbers: GL_TEXTURE_BINDING_2D, GL_VERTEX_ARRAY_BINDING, GL_ARRAY_BUFFER_BINDING |
| 169 | +TextureAverageComputer::TextureAverageComputer(QOpenGLFunctions_3_3_Core& gl, const int texWidth, const int texHeight, const GLenum internalFormat) |
| 170 | + : gl(gl) |
| 171 | + , npotWidth(texWidth) |
| 172 | + , npotHeight(texHeight) |
| 173 | +{ |
| 174 | + if(!inited) init(); |
| 175 | + if(!workaroundNeeded) return; |
| 176 | + |
| 177 | + GLint oldFBO=-1; |
| 178 | + gl.glGetIntegerv(GL_DRAW_FRAMEBUFFER_BINDING, &oldFBO); |
| 179 | + |
| 180 | + gl.glGenFramebuffers(1, &potFBO); |
| 181 | + gl.glGenTextures(1, &potTex); |
| 182 | + gl.glBindTexture(GL_TEXTURE_2D, potTex); |
| 183 | + const auto potWidth = roundDownToClosestPowerOfTwo(npotWidth); |
| 184 | + const auto potHeight = roundDownToClosestPowerOfTwo(npotHeight); |
| 185 | + gl.glTexImage2D(GL_TEXTURE_2D,0,internalFormat,potWidth,potHeight,0,GL_RGBA,GL_UNSIGNED_BYTE,nullptr); |
| 186 | + gl.glBindTexture(GL_TEXTURE_2D,0); |
| 187 | + gl.glBindFramebuffer(GL_DRAW_FRAMEBUFFER,potFBO); |
| 188 | + gl.glFramebufferTexture2D(GL_DRAW_FRAMEBUFFER,GL_COLOR_ATTACHMENT0,GL_TEXTURE_2D,potTex,0); |
| 189 | + [[maybe_unused]] const auto status=gl.glCheckFramebufferStatus(GL_DRAW_FRAMEBUFFER); |
| 190 | + assert(status==GL_FRAMEBUFFER_COMPLETE); |
| 191 | + gl.glBindFramebuffer(GL_DRAW_FRAMEBUFFER,0); |
| 192 | + |
| 193 | + gl.glGenVertexArrays(1, &vao); |
| 194 | + gl.glBindVertexArray(vao); |
| 195 | + gl.glGenBuffers(1, &vbo); |
| 196 | + gl.glBindBuffer(GL_ARRAY_BUFFER, vbo); |
| 197 | + const GLfloat vertices[]= |
| 198 | + { |
| 199 | + -1, -1, |
| 200 | + 1, -1, |
| 201 | + -1, 1, |
| 202 | + 1, 1, |
| 203 | + }; |
| 204 | + gl.glBufferData(GL_ARRAY_BUFFER, sizeof vertices, vertices, GL_STATIC_DRAW); |
| 205 | + constexpr GLuint attribIndex=0; |
| 206 | + constexpr int coordsPerVertex=2; |
| 207 | + gl.glVertexAttribPointer(attribIndex, coordsPerVertex, GL_FLOAT, false, 0, 0); |
| 208 | + gl.glEnableVertexAttribArray(attribIndex); |
| 209 | + gl.glBindVertexArray(0); |
| 210 | + |
| 211 | + blitTexProgram.reset(new QOpenGLShaderProgram); |
| 212 | + blitTexProgram->addShaderFromSourceCode(QOpenGLShader::Vertex, 1+R"( |
| 213 | +#version 330 |
| 214 | +layout(location=0) in vec4 vertex; |
| 215 | +out vec2 texcoord; |
| 216 | +void main() |
| 217 | +{ |
| 218 | + gl_Position = vertex; |
| 219 | + texcoord = vertex.st*0.5+vec2(0.5); |
| 220 | +} |
| 221 | +)"); |
| 222 | + blitTexProgram->addShaderFromSourceCode(QOpenGLShader::Fragment, 1+R"( |
| 223 | +#version 330 |
| 224 | +in vec2 texcoord; |
| 225 | +out vec4 color; |
| 226 | +uniform sampler2D tex; |
| 227 | +void main() |
| 228 | +{ |
| 229 | + color = texture(tex, texcoord); |
| 230 | +} |
| 231 | +)"); |
| 232 | + blitTexProgram->link(); |
| 233 | + blitTexProgram->bind(); |
| 234 | + blitTexProgram->setUniformValue("tex", 0); |
| 235 | + blitTexProgram->release(); |
| 236 | + |
| 237 | + gl.glBindFramebuffer(GL_DRAW_FRAMEBUFFER, oldFBO); |
| 238 | +} |
| 239 | + |
| 240 | +TextureAverageComputer::~TextureAverageComputer() |
| 241 | +{ |
| 242 | + gl.glDeleteTextures(1, &potTex); |
| 243 | + gl.glDeleteFramebuffers(1, &potFBO); |
| 244 | + gl.glDeleteVertexArrays(1, &vao); |
| 245 | + gl.glDeleteBuffers(1, &vbo); |
| 246 | +} |
| 247 | + |
| 248 | +#endif |
0 commit comments