From dd16a020453dbbb7a08a17fe83d01072093afccc Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Du=C5=A1an=20Poizl?= Date: Tue, 20 Aug 2024 17:53:30 +0200 Subject: [PATCH] Preparation for OpenGL ES --- CMakeLists.txt | 18 ++++++++++++------ imagescrollareagl.cpp | 43 ++++++++++++++++++++++++++++++------------- imagescrollareagl.h | 5 +++-- main.cpp | 10 +++++++++- shaders/debayer.frag | 2 -- shaders/debayer.vert | 2 -- shaders/image.frag | 8 +++----- shaders/image.vert | 2 -- shaders/thumb.frag | 4 +--- shaders/thumb.vert | 8 +++----- 10 files changed, 61 insertions(+), 41 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index eed48fd..cc59037 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -53,12 +53,6 @@ set(TENMON_SRC stretchtoolbar.cpp stretchtoolbar.h ) -option(COLOR_MANAGMENT "Enable sRGB framebuffer support for gamma correct images and color profiles support" ON) - -if(COLOR_MANAGMENT) - add_compile_definitions("COLOR_MANAGMENT") -endif(COLOR_MANAGMENT) - qt_add_resources(TENMON_SRC resources/resources.qrc) qt_add_resources(TENMON_SRC shaders/shaders.qrc) qt_add_resources(TENMON_SRC scripts/scripts.qrc) @@ -81,6 +75,18 @@ qt_add_executable(tenmon WIN32 MACOSX_BUNDLE ${tenmon_ICON} ${TENMON_SRC}) find_path(FITS_INCLUDE fitsio2.h PATH_SUFFIXES cfitsio REQUIRED) target_include_directories(tenmon PRIVATE ${FITS_INCLUDE} ${CMAKE_BINARY_DIR} ${libXISF_SOURCE_DIR}) +option(COLOR_MANAGMENT "Enable sRGB framebuffer support for gamma correct images and color profiles support" ON) +if(COLOR_MANAGMENT) + target_compile_definitions(tenmon PRIVATE "COLOR_MANAGMENT") +endif(COLOR_MANAGMENT) + +# not ready yet +#option(USE_OPENGLES ON) +if(USE_OPENGLES) + target_compile_definitions(tenmon PRIVATE "NOU_GLES_CONTEXT") +endif(USE_OPENGLES) + + if(UNIX AND NOT APPLE) target_include_directories(tenmon PRIVATE ${GIO_INCLUDE_DIRS}) endif() diff --git a/imagescrollareagl.cpp b/imagescrollareagl.cpp index 9c5de06..c97b0bc 100644 --- a/imagescrollareagl.cpp +++ b/imagescrollareagl.cpp @@ -203,7 +203,7 @@ void ImageWidget::allocateThumbnails(const QStringList &paths) m_thumbnailTexture->destroy(); m_thumbnailTexture->create(); - m_thumbnailTexture->setFormat(QOpenGLTexture::RGB16_UNorm); + m_thumbnailTexture->setFormat(QOpenGLTexture::RGBA16_UNorm); m_thumbnailTexture->setSize(THUMB_SIZE, THUMB_SIZE); m_thumbnailTexture->setLayers(std::min((int)paths.size(), m_maxArrayLayers)); m_thumbnailTexture->allocateStorage(); @@ -354,7 +354,7 @@ void ImageWidget::paintGL() QMatrix4x4 mvp; mvp.ortho(rect()); m_thumbnailProgram->setUniformValue("mvp", mvp); - if(f3)f3->glDrawArraysInstanced(GL_TRIANGLE_STRIP, 0, 4, m_thumbnailCount); + fx->glDrawArraysInstanced(GL_TRIANGLE_STRIP, 0, 4, m_thumbnailCount); QPainter painter(this); const int w = width()/THUMB_SIZE_BORDER; @@ -433,12 +433,29 @@ void ImageWidget::resizeGL(int w, int h) void ImageWidget::initializeGL() { f = context()->functions(); + fx = context()->extraFunctions(); f->glClearColor(0.5f, 0.5f, 0.5f, 1.0f); - f3 = QOpenGLVersionFunctionsFactory::get(context()); - if(f3 == nullptr) + if(fx == nullptr) QMessageBox::critical(this, tr("OpenGL error"), tr("Could not initialize OpenGL 3.3 context. Ensure that proper GPU driver is installed.")); + auto loadShader = [](const QString &file) + { + QFile fr(file); + fr.open(QIODevice::ReadOnly); + QByteArray src; +#ifdef NOU_GLES_CONTEXT + src = "#version 300 es\n" + "precision highp float;\n" + "precision highp sampler2DArray;\n" + "#line 1\n"; +#else + src = "#version 330\n#line 1\n"; +#endif + src.append(fr.readAll()); + return src; + }; + m_vao = std::unique_ptr(new QOpenGLVertexArrayObject); m_vaoThumb = std::unique_ptr(new QOpenGLVertexArrayObject); m_vao->create(); @@ -477,8 +494,8 @@ void ImageWidget::initializeGL() // f->glVertexAttribPointer(0, 2, GL_FLOAT, false, sizeof(float)*4, 0); m_program = std::unique_ptr(new QOpenGLShaderProgram); - m_program->addShaderFromSourceFile(QOpenGLShader::Vertex, ":/image.vert"); - m_program->addShaderFromSourceFile(QOpenGLShader::Fragment, ":/image.frag"); + m_program->addShaderFromSourceCode(QOpenGLShader::Vertex, loadShader(":/image.vert")); + m_program->addShaderFromSourceCode(QOpenGLShader::Fragment, loadShader(":/image.frag")); if(!m_program->link()) { @@ -494,8 +511,8 @@ void ImageWidget::initializeGL() m_program->setUniformValue("scale", 1.0f, 0.0f); m_debayerProgram = std::unique_ptr(new QOpenGLShaderProgram); - m_debayerProgram->addShaderFromSourceFile(QOpenGLShader::Vertex, ":/debayer.vert"); - m_debayerProgram->addShaderFromSourceFile(QOpenGLShader::Fragment, ":/debayer.frag"); + m_debayerProgram->addShaderFromSourceCode(QOpenGLShader::Vertex, loadShader(":/debayer.vert")); + m_debayerProgram->addShaderFromSourceCode(QOpenGLShader::Fragment, loadShader(":/debayer.frag")); m_debayerProgram->bind(); m_debayerProgram->enableAttributeArray("qt_Vertex"); @@ -511,8 +528,8 @@ void ImageWidget::initializeGL() m_vaoThumb->bind(); m_thumbnailProgram = std::unique_ptr(new QOpenGLShaderProgram); - m_thumbnailProgram->addShaderFromSourceFile(QOpenGLShader::Vertex, ":/thumb.vert"); - m_thumbnailProgram->addShaderFromSourceFile(QOpenGLShader::Fragment, ":/thumb.frag"); + m_thumbnailProgram->addShaderFromSourceCode(QOpenGLShader::Vertex, loadShader(":/thumb.vert")); + m_thumbnailProgram->addShaderFromSourceCode(QOpenGLShader::Fragment, loadShader(":/thumb.frag")); m_thumbnailProgram->bind(); m_thumbnailProgram->enableAttributeArray("qt_Vertex"); @@ -532,8 +549,8 @@ void ImageWidget::initializeGL() m_bufferSizes->allocate(12); m_thumbnailProgram->enableAttributeArray("imageSize_num"); - f3->glVertexAttribIPointer(m_thumbnailProgram->attributeLocation("imageSize_num"), 3, GL_INT, 0, nullptr); - f3->glVertexAttribDivisor(m_thumbnailProgram->attributeLocation("imageSize_num"), 1); + fx->glVertexAttribIPointer(m_thumbnailProgram->attributeLocation("imageSize_num"), 3, GL_INT, 0, nullptr); + fx->glVertexAttribDivisor(m_thumbnailProgram->attributeLocation("imageSize_num"), 1); m_image = std::unique_ptr(new QOpenGLTexture(QOpenGLTexture::Target2D)); m_image->setFormat(QOpenGLTexture::RGB8U); @@ -543,7 +560,7 @@ void ImageWidget::initializeGL() m_image->setMagnificationFilter(QOpenGLTexture::Linear); m_thumbnailTexture = std::unique_ptr(new QOpenGLTexture(QOpenGLTexture::Target2DArray)); - m_thumbnailTexture->setFormat(QOpenGLTexture::RGB16_UNorm); + m_thumbnailTexture->setFormat(QOpenGLTexture::RGBA16_UNorm); m_thumbnailTexture->setSize(THUMB_SIZE, THUMB_SIZE); m_thumbnailTexture->setLayers(1); m_thumbnailTexture->allocateStorage(); diff --git a/imagescrollareagl.h b/imagescrollareagl.h index eec06f0..7b90e5e 100644 --- a/imagescrollareagl.h +++ b/imagescrollareagl.h @@ -4,7 +4,6 @@ #include #include #include -#include #include #include #include @@ -15,6 +14,8 @@ #include "imageringlist.h" #include "database.h" #include "stretchtoolbar.h" +#include +#include struct ImageThumb { @@ -29,7 +30,7 @@ class ImageWidget : public QOpenGLWidget { Q_OBJECT QOpenGLFunctions *f = nullptr; - QOpenGLFunctions_3_3_Core *f3 = nullptr; + QOpenGLExtraFunctions *fx = nullptr; QTimer *m_updateTimer = nullptr; std::unique_ptr m_program; std::unique_ptr m_thumbnailProgram; diff --git a/main.cpp b/main.cpp index 36fdc0e..c3ecc85 100644 --- a/main.cpp +++ b/main.cpp @@ -3,7 +3,6 @@ #include #include #include -#include "libxisf.h" int main(int argc, char *argv[]) { @@ -12,10 +11,19 @@ int main(int argc, char *argv[]) #endif QSurfaceFormat format; +#ifdef NOU_GLES_CONTEXT + format.setMajorVersion(3); + format.setMinorVersion(0); + format.setRenderableType(QSurfaceFormat::OpenGLES); + printf("OpenGL ES\n"); + qDebug() << "Requesting OpenGL ES"; +#else format.setMajorVersion(3); format.setMinorVersion(3); //format.setOption(QSurfaceFormat::DebugContext); format.setProfile(QSurfaceFormat::OpenGLContextProfile::CoreProfile); + printf("OpenGL\n"); +#endif QSurfaceFormat::setDefaultFormat(format); QApplication a(argc, argv); diff --git a/shaders/debayer.frag b/shaders/debayer.frag index 8830357..1dd4605 100644 --- a/shaders/debayer.frag +++ b/shaders/debayer.frag @@ -1,5 +1,3 @@ -#version 330 - uniform sampler2D qt_Texture0; uniform ivec2 firstRed; in vec2 qt_TexCoord0; diff --git a/shaders/debayer.vert b/shaders/debayer.vert index bc4ea25..2dcffd3 100644 --- a/shaders/debayer.vert +++ b/shaders/debayer.vert @@ -1,5 +1,3 @@ -#version 330 - uniform sampler2D qt_Texture0; in vec2 qt_Vertex; in vec2 qt_MultiTexCoord0; diff --git a/shaders/image.frag b/shaders/image.frag index c8788fd..f643f61 100644 --- a/shaders/image.frag +++ b/shaders/image.frag @@ -1,5 +1,3 @@ -#version 330 - uniform sampler2D qt_Texture0; uniform vec3 mtf_param[3]; uniform vec2 unit_scale; @@ -22,7 +20,7 @@ vec4 MTF(vec4 x, vec4 low, vec4 mid, vec4 high) { x = (x - low) / (high - low); x = clamp(x, vec4(0.0), vec4(1.0)); - return ((mid - 1) * x) / ((2 * mid - 1) * x - mid); + return ((mid - 1.0) * x) / ((2.0 * mid - 1.0) * x - mid); } vec3 falsecolor(float color) @@ -59,7 +57,7 @@ vec4 cubic(float v) vec4 textureBicubic(sampler2D sampler, vec2 texCoords) { - vec2 texSize = textureSize(sampler, 0); + vec2 texSize = vec2(textureSize(sampler, 0)); vec2 invTexSize = 1.0 / texSize; texCoords = texCoords * texSize - 0.5; @@ -133,7 +131,7 @@ void main(void) switch(filtering) { case 0://nearest - color = texelFetch(qt_Texture0, ivec2(qt_TexCoord0 * textureSize(qt_Texture0, 0)), 0); + color = texelFetch(qt_Texture0, ivec2(qt_TexCoord0 * vec2(textureSize(qt_Texture0, 0))), 0); break; default: case 1://bilinear diff --git a/shaders/image.vert b/shaders/image.vert index 9ffd760..02e525a 100644 --- a/shaders/image.vert +++ b/shaders/image.vert @@ -1,5 +1,3 @@ -#version 330 - uniform sampler2D qt_Texture0; in vec2 qt_Vertex; in vec2 qt_MultiTexCoord0; diff --git a/shaders/thumb.frag b/shaders/thumb.frag index 8264942..481a239 100644 --- a/shaders/thumb.frag +++ b/shaders/thumb.frag @@ -1,5 +1,3 @@ -#version 330 - uniform sampler2DArray qt_Texture0; uniform vec3 mtf_param[3]; uniform bool invert; @@ -10,7 +8,7 @@ vec4 MTF(vec4 x, vec4 low, vec4 mid, vec4 high) { x = (x - low) / (high - low); x = clamp(x, vec4(0.0), vec4(1.0)); - return ((mid - 1) * x) / ((2 * mid - 1) * x - mid); + return ((mid - 1.0) * x) / ((2.0 * mid - 1.0) * x - mid); } void main(void) diff --git a/shaders/thumb.vert b/shaders/thumb.vert index 9afdce2..5785606 100644 --- a/shaders/thumb.vert +++ b/shaders/thumb.vert @@ -1,5 +1,3 @@ -#version 330 - in vec2 qt_Vertex; in vec2 qt_MultiTexCoord0; in ivec3 imageSize_num; @@ -13,9 +11,9 @@ void main(void) { vec2 pos = qt_Vertex * 0.5; pos.y *= -1.0; - pos = pos * imageSize_num.xy + thumb_size.x; + pos = pos * vec2(imageSize_num.xy) + float(thumb_size.x); ivec2 off = ivec2(imageSize_num.z % viewport_row.z, imageSize_num.z / viewport_row.z) * thumb_size.yz; - gl_Position = mvp * vec4(pos - offset + off, 0.0, 1.0); - qt_TexCoord0 = vec3(qt_MultiTexCoord0, imageSize_num.z + 0.1); + gl_Position = mvp * vec4(pos - offset + vec2(off), 0.0, 1.0); + qt_TexCoord0 = vec3(qt_MultiTexCoord0, float(imageSize_num.z) + 0.1); }