From 652d7e6699d53cbee00af3d41c8e09ed0c84e3d5 Mon Sep 17 00:00:00 2001 From: ts-korhonen Date: Sun, 6 Mar 2022 01:06:47 +0200 Subject: [PATCH] qt: gl-core; Use 4.1 core macOS & get glsl version from context MacOS will use OpenGL 2.1 unless requested 4.1 core. Shader language version is now set to what is reported by the driver. --- src/qt/qt_opengloptions.cpp | 23 +++++++++-------------- src/qt/qt_opengloptions.hpp | 5 +++-- src/qt/qt_opengloptionsdialog.cpp | 5 +++-- src/qt/qt_opengloptionsdialog.hpp | 6 +++++- src/qt/qt_openglrenderer.cpp | 26 ++++++++++++++++++++++---- src/qt/qt_openglrenderer.hpp | 2 ++ 6 files changed, 44 insertions(+), 23 deletions(-) diff --git a/src/qt/qt_opengloptions.cpp b/src/qt/qt_opengloptions.cpp index 513f05f02..0d5b9fc77 100644 --- a/src/qt/qt_opengloptions.cpp +++ b/src/qt/qt_opengloptions.cpp @@ -45,8 +45,9 @@ void main() {\n\ color = texture(texsampler, tex);\n\ }\n"; -OpenGLOptions::OpenGLOptions(QObject *parent, bool loadConfig) +OpenGLOptions::OpenGLOptions(QObject *parent, bool loadConfig, const QString &glslVersion) : QObject(parent) + , m_glslVersion(glslVersion) { if (!loadConfig) return; @@ -147,7 +148,7 @@ OpenGLOptions::addShader(const QString &path) auto match = version.match(shader_text); - QString version_line("#version 130"); + QString version_line(m_glslVersion); if (match.hasMatch()) { /* Extract existing version and remove it. */ @@ -155,10 +156,6 @@ OpenGLOptions::addShader(const QString &path) shader_text.remove(version); } - if (QOpenGLContext::currentContext() && QOpenGLContext::currentContext()->isOpenGLES()) { - /* Force #version 300 es (the default of #version 100 es is too old and too limited) */ - version_line = "#version 300 es"; - } auto shader = new QOpenGLShaderProgram(this); auto throw_shader_error = [path, shader](const QString &what) { @@ -169,10 +166,12 @@ OpenGLOptions::addShader(const QString &path) .toStdString()); }; - if (!shader->addShaderFromSourceCode(QOpenGLShader::Vertex, version_line % "\n#extension GL_ARB_shading_language_420pack : enable\n" % "\n#define VERTEX\n" % shader_text)) + static const char *extension = "\n#extension GL_ARB_shading_language_420pack : enable\n"; + + if (!shader->addShaderFromSourceCode(QOpenGLShader::Vertex, version_line % extension % "\n#define VERTEX\n#line 1\n" % shader_text)) throw_shader_error(tr("Error compiling vertex shader in file \"%1\"")); - if (!shader->addShaderFromSourceCode(QOpenGLShader::Fragment, version_line % "\n#extension GL_ARB_shading_language_420pack : enable\n" % "\n#define FRAGMENT\n" % shader_text)) + if (!shader->addShaderFromSourceCode(QOpenGLShader::Fragment, version_line % extension % "\n#define FRAGMENT\n#line 1\n" % shader_text)) throw_shader_error(tr("Error compiling fragment shader in file \"%1\"")); if (!shader->link()) @@ -184,13 +183,9 @@ OpenGLOptions::addShader(const QString &path) void OpenGLOptions::addDefaultShader() { - QString version = QOpenGLContext::currentContext() && QOpenGLContext::currentContext()->isOpenGLES() - ? "#version 300 es\n" - : "#version 130\n"; - auto shader = new QOpenGLShaderProgram(this); - shader->addShaderFromSourceCode(QOpenGLShader::Vertex, version % vertex_shader); - shader->addShaderFromSourceCode(QOpenGLShader::Fragment, version % fragment_shader); + shader->addShaderFromSourceCode(QOpenGLShader::Vertex, m_glslVersion % "\n" % vertex_shader); + shader->addShaderFromSourceCode(QOpenGLShader::Fragment, m_glslVersion % "\n" % fragment_shader); shader->link(); m_shaders << OpenGLShaderPass(shader, QString()); } diff --git a/src/qt/qt_opengloptions.hpp b/src/qt/qt_opengloptions.hpp index 45a11377a..b88cf4b07 100644 --- a/src/qt/qt_opengloptions.hpp +++ b/src/qt/qt_opengloptions.hpp @@ -72,14 +72,14 @@ public: enum FilterType { Nearest, Linear }; - OpenGLOptions(QObject *parent = nullptr, bool loadConfig = false); + OpenGLOptions(QObject *parent, bool loadConfig, const QString &glslVersion); RenderBehaviorType renderBehavior() const { return m_renderBehavior; } int framerate() const { return m_framerate; } bool vSync() const { return m_vsync; } FilterType filter() const; - const QList &shaders() const { return m_shaders; }; + const QList &shaders() const { return m_shaders; } void setRenderBehavior(RenderBehaviorType value); void setFrameRate(int value); @@ -95,6 +95,7 @@ private: bool m_vsync = false; FilterType m_filter = Nearest; QList m_shaders; + QString m_glslVersion; }; #endif diff --git a/src/qt/qt_opengloptionsdialog.cpp b/src/qt/qt_opengloptionsdialog.cpp index d996fbd3c..c87989161 100644 --- a/src/qt/qt_opengloptionsdialog.cpp +++ b/src/qt/qt_opengloptionsdialog.cpp @@ -24,9 +24,10 @@ #include "qt_util.hpp" #include "ui_qt_opengloptionsdialog.h" -OpenGLOptionsDialog::OpenGLOptionsDialog(QWidget *parent, const OpenGLOptions &options) +OpenGLOptionsDialog::OpenGLOptionsDialog(QWidget *parent, const OpenGLOptions &options, std::function optionsFactory) : QDialog(parent) , ui(new Ui::OpenGLOptionsDialog) + , createOptions(optionsFactory) { ui->setupUi(this); @@ -54,7 +55,7 @@ OpenGLOptionsDialog::~OpenGLOptionsDialog() void OpenGLOptionsDialog::accept() { - auto options = new OpenGLOptions(); + auto options = createOptions(); options->setRenderBehavior( ui->syncWithVideo->isChecked() diff --git a/src/qt/qt_opengloptionsdialog.hpp b/src/qt/qt_opengloptionsdialog.hpp index 833c58471..6b1c673bd 100644 --- a/src/qt/qt_opengloptionsdialog.hpp +++ b/src/qt/qt_opengloptionsdialog.hpp @@ -19,6 +19,8 @@ #include +#include + #include "qt_opengloptions.hpp" namespace Ui { @@ -29,7 +31,7 @@ class OpenGLOptionsDialog : public QDialog { Q_OBJECT public: - explicit OpenGLOptionsDialog(QWidget *parent, const OpenGLOptions &options); + explicit OpenGLOptionsDialog(QWidget *parent, const OpenGLOptions &options, std::function optionsFactory); ~OpenGLOptionsDialog(); signals: @@ -41,6 +43,8 @@ public slots: private: Ui::OpenGLOptionsDialog *ui; + std::function createOptions; + private slots: void on_addShader_clicked(); }; diff --git a/src/qt/qt_openglrenderer.cpp b/src/qt/qt_openglrenderer.cpp index 2557b3ba1..d4724fdde 100644 --- a/src/qt/qt_openglrenderer.cpp +++ b/src/qt/qt_openglrenderer.cpp @@ -42,9 +42,12 @@ OpenGLRenderer::OpenGLRenderer(QWidget *parent) QSurfaceFormat format; +#ifdef Q_OS_MACOS + format.setVersion(4, 1); +#else + format.setVersion(3, 2); +#endif format.setProfile(QSurfaceFormat::OpenGLContextProfile::CoreProfile); - format.setMajorVersion(3); - format.setMinorVersion(2); if (QOpenGLContext::openGLModuleType() == QOpenGLContext::LibGLES) format.setRenderableType(QSurfaceFormat::OpenGLES); @@ -114,8 +117,23 @@ OpenGLRenderer::initialize() if (!context->makeCurrent(this)) throw opengl_init_error(tr("Couldn't switch to OpenGL context.")); + auto version = context->format().version(); + + if (version.first < 3) + throw opengl_init_error(tr("OpenGL version 3.0 or greater is required. Current version is %1.%2").arg(version.first).arg(version.second)); + initializeOpenGLFunctions(); + /* Prepare the shader version string */ + glslVersion = reinterpret_cast(glGetString(GL_SHADING_LANGUAGE_VERSION)); + glslVersion.truncate(4); + glslVersion.remove('.'); + glslVersion.prepend("#version "); + if (QOpenGLContext::openGLModuleType() == QOpenGLContext::LibGLES) + glslVersion.append(" es"); + else if (context->format().profile() == QSurfaceFormat::CoreProfile) + glslVersion.append(" core"); + initializeExtensions(); initializeBuffers(); @@ -147,7 +165,7 @@ OpenGLRenderer::initialize() glTexImage2D(GL_TEXTURE_2D, 0, QOpenGLTexture::RGBA8_UNorm, INIT_WIDTH, INIT_HEIGHT, 0, QOpenGLTexture::BGRA, QOpenGLTexture::UInt32_RGBA8_Rev, NULL); - options = new OpenGLOptions(this, true); + options = new OpenGLOptions(this, true, glslVersion); applyOptions(); @@ -211,7 +229,7 @@ OpenGLRenderer::finalize() QDialog * OpenGLRenderer::getOptions(QWidget *parent) { - auto dialog = new OpenGLOptionsDialog(parent, *options); + auto dialog = new OpenGLOptionsDialog(parent, *options, [this]() { return new OpenGLOptions(this, false, glslVersion); }); connect(dialog, &OpenGLOptionsDialog::optionsChanged, this, &OpenGLRenderer::updateOptions); diff --git a/src/qt/qt_openglrenderer.hpp b/src/qt/qt_openglrenderer.hpp index db154d950..a83eac5dc 100644 --- a/src/qt/qt_openglrenderer.hpp +++ b/src/qt/qt_openglrenderer.hpp @@ -77,6 +77,8 @@ private: OpenGLOptions *options; QTimer *renderTimer; + QString glslVersion; + bool isInitialized = false; bool isFinalized = false;