Skip to content

Commit 52c773f

Browse files
DreamNik10110111
andcommitted
Add support for high graphics mode on GLES3
Also fix OpenGL-related issues on Android. Co-authored-by: Ruslan Kabatsayev <b7.10110111@gmail.com>
1 parent 1ef626a commit 52c773f

12 files changed

Lines changed: 125 additions & 105 deletions

plugins/Scenery3d/src/GLFuncs.hpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,13 +22,13 @@
2222
#define GLFUNCS_HPP
2323

2424
#include <QOpenGLContext>
25+
#include <StelOpenGL.hpp>
2526

2627
#if !QT_CONFIG(opengles2)
27-
#include <QOpenGLFunctions_1_0>
2828
//! Defines some OpenGL functions not resolved through StelOpenGL (which only contains base OpenGL ES2 functions)
2929
//! Using the QOpenGLFunctions_*_* directly would solve this better, but it conflicts with the
3030
//! current StelOpenGL header dramatically.
31-
class GLExtFuncs : public QOpenGLFunctions_1_0
31+
class GLExtFuncs : public StelOpenGL::BaseFunctions
3232
{
3333
public:
3434
//! Since 3.2

src/StelMainView.cpp

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -885,13 +885,20 @@ void StelMainView::init()
885885
glInfo.supportsLuminanceTextures = format.profile() == QSurfaceFormat::CompatibilityProfile ||
886886
format.majorVersion() < 3;
887887
glInfo.isGLES = format.renderableType()==QSurfaceFormat::OpenGLES;
888+
glInfo.majorVersion = format.majorVersion();
888889
qInfo().nospace() << "Luminance textures are " << (glInfo.supportsLuminanceTextures ? "" : "not ") << "supported";
889890
glInfo.isCoreProfile = format.profile() == QSurfaceFormat::CoreProfile;
890891
#if defined Q_OS_HAIKU || defined Q_OS_SOLARIS
891892
// Haiku OS/Solaris hasn't hardware acceleration and we shouldn't use High Graphics Mode here
892893
glInfo.isHighGraphicsMode = false;
893894
#else
894-
glInfo.isHighGraphicsMode = !qApp->property("onetime_force_low_graphics").toBool() && !!StelOpenGL::highGraphicsFunctions();
895+
// GLES will always provide high graphics functions due to our choice of QOpenGLExtraFunctions,
896+
// so we also need to check that we have at least GLES3 to enable high graphics mode.
897+
// For desktop OpenGL our target version is also at least 3, so it's compatible with this check.
898+
// And we do need to check that high-graphics functions are available, since GL3.0 is not sufficient.
899+
glInfo.isHighGraphicsMode = glInfo.majorVersion >= 3 && !!StelOpenGL::highGraphicsFunctions();
900+
if (qApp->property("onetime_force_low_graphics").toBool())
901+
glInfo.isHighGraphicsMode = false;
895902
#endif
896903
qInfo() << "Running in" << (glInfo.isHighGraphicsMode ? "High" : "Low") << "Graphics Mode";
897904

src/StelMainView.hpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,7 @@ class StelMainView : public QGraphicsView
8585
bool isCoreProfile = false;
8686
bool isHighGraphicsMode = false;
8787
bool isGLES = false;
88+
GLint majorVersion = 0;
8889
};
8990

9091
StelMainView(QSettings* settings);

src/core/Dithering.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,8 @@ QString makeDitheringShader()
6161
return 1+R"(
6262
#line 1 101
6363
uniform mediump vec3 rgbMaxValue;
64-
uniform sampler2D ditherPattern;
64+
uniform mediump sampler2D ditherPattern;
65+
6566
mediump vec3 dither(mediump vec3 c)
6667
{
6768
c = clamp(c, 0., 1.);

src/core/StelApp.cpp

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -931,6 +931,7 @@ void main()
931931
postProcessorUniformLocations.ditherPattern = postProcessorProgram->uniformLocation("ditherPattern");
932932
postProcessorProgram->release();
933933

934+
#if !QT_CONFIG(opengles2)
934935
if(StelMainView::getInstance().getGLInformation().isHighGraphicsMode)
935936
{
936937
postProcessorProgramMS.reset(new QOpenGLShaderProgram);
@@ -965,11 +966,11 @@ void main()
965966
postProcessorUniformLocationsMS.numMultiSamples = postProcessorProgramMS->uniformLocation("numMultiSamples");
966967
postProcessorProgramMS->release();
967968
}
969+
#endif
968970
}
969971

970972
void StelApp::highGraphicsModeDraw()
971973
{
972-
#if !QT_CONFIG(opengles2)
973974
const auto targetFBO = currentFbo;
974975
StelProjector::StelProjectorParams params = core->getCurrentStelProjectorParams();
975976
const auto w = params.viewportXywh[2] * params.devicePixelsPerPixel;
@@ -982,11 +983,17 @@ void StelApp::highGraphicsModeDraw()
982983
qInfo() << "OpenGL viewport size:" << viewport[2] << "x" << viewport[3];
983984

984985
qInfo().nospace() << "Creating scene FBO with size " << w << "x" << h;
986+
987+
#if QT_CONFIG(opengles2)
988+
const auto internalFormat = GL_RGBA16F;
989+
#else
985990
const auto internalFormat = GL_RGBA16;
991+
#endif
986992
QOpenGLFramebufferObjectFormat format;
987993
format.setAttachment(QOpenGLFramebufferObject::CombinedDepthStencil);
988994
format.setInternalTextureFormat(internalFormat);
989995
sceneFBO.reset(new QOpenGLFramebufferObject(w, h, format));
996+
#if !QT_CONFIG(opengles2)
990997
GLint maxSamples = 1;
991998
GL(gl->glGetIntegerv(GL_MAX_SAMPLES, &maxSamples));
992999
const auto samples = confSettings->value("video/multisampling", 0).toInt();
@@ -1025,6 +1032,7 @@ void StelApp::highGraphicsModeDraw()
10251032
<< status;
10261033
}
10271034
}
1035+
#endif
10281036
}
10291037

10301038
if(sceneMultisampledFBO)
@@ -1090,7 +1098,6 @@ void StelApp::highGraphicsModeDraw()
10901098
GL(gl->glDrawArrays(GL_TRIANGLE_STRIP, 0, 4));
10911099
GL(gl->glDisable(GL_BLEND));
10921100
postProcessorVAO->release();
1093-
#endif
10941101
}
10951102

10961103
//! Main drawing function called at each frame

src/core/StelOpenGL.cpp

Lines changed: 54 additions & 67 deletions
Original file line numberDiff line numberDiff line change
@@ -67,80 +67,67 @@ QByteArray StelOpenGL::globalShaderPrefix(const ShaderType type)
6767
{
6868
const auto& glInfo = StelMainView::getInstance().getGLInformation();
6969

70-
if(glInfo.isHighGraphicsMode)
70+
QByteArray prefix;
71+
72+
if (glInfo.isHighGraphicsMode)
7173
{
72-
if(type == VERTEX_SHADER)
73-
{
74-
static const QByteArray prefix = 1+R"(
75-
#version 330
76-
#define ATTRIBUTE in
77-
#define VARYING out
78-
#define texture2D(a,b) texture(a,b)
79-
#define texture2D_3(a,b,c) texture(a,b,c)
80-
#define texture2DProj(a,b) textureProj(a,b)
81-
#define texture2DProj_3(a,b,c) textureProj(a,b,c)
82-
#define shadow2D_x(a,b) texture(a,b)
83-
#define shadow2DProj_x(a,b) textureProj(a,b)
84-
#define textureCube(a,b) texture(a,b)
85-
#define textureCube_3(a,b,c) texture(a,b,c)
86-
#line 1
87-
)";
88-
return prefix;
89-
}
90-
else // FRAGMENT_SHADER
91-
{
92-
static const QByteArray prefix = 1+R"(
93-
#version 330
94-
#define VARYING in
95-
out vec4 FRAG_COLOR;
96-
#define texture2D(a,b) texture(a,b)
97-
#define texture2D_3(a,b,c) texture(a,b,c)
98-
#define texture2DProj(a,b) textureProj(a,b)
99-
#define texture2DProj_3(a,b,c) textureProj(a,b,c)
100-
#define shadow2D_x(a,b) texture(a,b)
101-
#define shadow2DProj_x(a,b) textureProj(a,b)
102-
#define textureCube(a,b) texture(a,b)
103-
#define textureCube_3(a,b,c) texture(a,b,c)
104-
#define textureGrad_SUPPORTED
105-
#line 1
106-
)";
107-
return prefix;
108-
}
109-
}
74+
if (glInfo.isGLES)
75+
prefix += "#version 300 es\n";
76+
else
77+
prefix += "#version 330\n";
11078

111-
if(type == VERTEX_SHADER)
79+
prefix += "#define texture2D(a,b) texture(a,b)\n"
80+
"#define texture2D_3(a,b,c) texture(a,b,c)\n"
81+
"#define texture2DProj(a,b) textureProj(a,b)\n"
82+
"#define texture2DProj_3(a,b,c) textureProj(a,b,c)\n"
83+
"#define shadow2D_x(a,b) texture(a,b)\n"
84+
"#define shadow2DProj_x(a,b) textureProj(a,b)\n"
85+
"#define textureCube(a,b) texture(a,b)\n"
86+
"#define textureCube_3(a,b,c) texture(a,b,c)\n";
87+
}
88+
else
11289
{
113-
static const QByteArray prefix = 1+R"(
114-
#define ATTRIBUTE attribute
115-
#define VARYING varying
116-
#define texture2D_3(a,b,c) texture2D(a,b,c)
117-
#define texture2DProj_3(a,b,c) texture2DProj(a,b,c)
118-
#define shadow2D_x(a,b) shadow2D(a,b).x
119-
#define shadow2DProj_x(a,b) shadow2DProj(a,b).x
120-
#define textureCube_3(a,b,c) textureCube(a,b,c)
121-
#line 1
122-
)";
123-
return prefix;
90+
prefix += "#define texture2D_3(a,b,c) texture2D(a,b,c)\n"
91+
"#define texture2DProj_3(a,b,c) texture2DProj(a,b,c)\n"
92+
"#define shadow2D_x(a,b) shadow2D(a,b).x\n"
93+
"#define shadow2DProj_x(a,b) shadow2DProj(a,b).x\n"
94+
"#define textureCube_3(a,b,c) textureCube(a,b,c)\n";
12495
}
125-
else // FRAGMENT_SHADER
96+
97+
if (glInfo.isGLES)
98+
prefix += "precision mediump float;\n";
99+
100+
switch (type)
126101
{
127-
static const QByteArray prefix = 1+R"(
128-
#define VARYING varying
129-
#define FRAG_COLOR gl_FragColor
130-
#define texture2D_3(a,b,c) texture2D(a,b,c)
131-
#define texture2DProj_3(a,b,c) texture2DProj(a,b,c)
132-
#define shadow2D_x(a,b) shadow2D(a,b).x
133-
#define shadow2DProj_x(a,b) shadow2DProj(a,b).x
134-
#define textureCube_3(a,b,c) textureCube(a,b,c)
135-
#line 1
136-
)";
137-
if(glInfo.isGLES)
138-
return "precision mediump float;\n" + prefix;
139-
return prefix;
102+
case VERTEX_SHADER:
103+
if (glInfo.isHighGraphicsMode)
104+
prefix += "#define ATTRIBUTE in\n"
105+
"#define VARYING out\n";
106+
else
107+
prefix += "#define ATTRIBUTE attribute\n"
108+
"#define VARYING varying\n";
109+
break;
110+
111+
case FRAGMENT_SHADER:
112+
if (glInfo.isHighGraphicsMode)
113+
prefix += "#define VARYING in\n"
114+
"#define textureGrad_SUPPORTED\n"
115+
"out highp vec4 FRAG_COLOR;\n";
116+
else
117+
prefix += "#define VARYING varying\n"
118+
"#define FRAG_COLOR gl_FragColor\n";
119+
break;
120+
121+
case GEOMETRY_SHADER: break;
140122
}
123+
124+
prefix += "#line 1\n";
125+
126+
return prefix;
141127
}
142128

143-
QOpenGLFunctions_3_3_Core* StelOpenGL::highGraphicsFunctions()
129+
130+
auto StelOpenGL::highGraphicsFunctions() -> HighGraphicsFunctions*
144131
{
145132
#if !QT_CONFIG(opengles2)
146133
# if QT_VERSION >= QT_VERSION_CHECK(6,0,0)
@@ -149,6 +136,6 @@ QOpenGLFunctions_3_3_Core* StelOpenGL::highGraphicsFunctions()
149136
return QOpenGLContext::currentContext()->versionFunctions<QOpenGLFunctions_3_3_Core>();
150137
# endif
151138
#else
152-
return nullptr;
139+
return QOpenGLContext::currentContext()->extraFunctions();
153140
#endif
154141
}

src/core/StelOpenGL.hpp

Lines changed: 21 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -35,11 +35,29 @@
3535
# define GL(line) line
3636
#endif
3737

38-
class QOpenGLFunctions_3_3_Core;
38+
#if defined(Q_OS_ANDROID)
39+
# include <QOpenGLExtraFunctions>
40+
# include <GLES3/gl32.h>
41+
# define APIENTRY
42+
#elif QT_CONFIG(opengles2)
43+
# include <QOpenGLExtraFunctions>
44+
#else
45+
# include <QOpenGLFunctions_1_0>
46+
# include <QOpenGLFunctions_3_3_Core>
47+
#endif
3948

4049
//! Namespace containing some OpenGL helpers
4150
namespace StelOpenGL
4251
{
52+
53+
#if defined(Q_OS_ANDROID) || QT_CONFIG(opengles2)
54+
using BaseFunctions = QOpenGLExtraFunctions;
55+
using HighGraphicsFunctions = QOpenGLExtraFunctions;
56+
#else
57+
using BaseFunctions = QOpenGLFunctions_1_0;
58+
using HighGraphicsFunctions = QOpenGLFunctions_3_3_Core;
59+
#endif
60+
4361
//! The main context as created by the StelMainView (only used for debugging)
4462
extern QOpenGLContext* mainContext;
4563

@@ -54,12 +72,13 @@ namespace StelOpenGL
5472
{
5573
VERTEX_SHADER,
5674
FRAGMENT_SHADER,
75+
GEOMETRY_SHADER,
5776
};
5877
//! Returns a prefix containing \#version directive and a few defines for
5978
// the GLSL version supported in current GL context.
6079
QByteArray globalShaderPrefix(ShaderType);
6180
//! Returns high-graphics OpenGL functions, for use in non-ANGLE/CompatProfile/etc. modes.
62-
QOpenGLFunctions_3_3_Core* highGraphicsFunctions();
81+
HighGraphicsFunctions* highGraphicsFunctions();
6382
}
6483

6584
// This is still needed for the ARM platform (armhf)

src/core/StelPainter.cpp

Lines changed: 15 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -2376,14 +2376,13 @@ void main()
23762376
texturesColorShaderVars.texture = texturesColorShaderProgram->uniformLocation("tex");
23772377
texturesColorShaderVars.saturation = texturesColorShaderProgram->uniformLocation("saturation");
23782378

2379-
if(StelMainView::getInstance().getGLInformation().isCoreProfile)
2379+
const auto& glInfo = StelMainView::getInstance().getGLInformation();
2380+
if(glInfo.isCoreProfile && glInfo.isHighGraphicsMode && !glInfo.isGLES)
23802381
{
23812382
// In Core profile wide lines (width>1px) are not supported, so this shader is used to render them with triangles.
23822383

23832384
// Common template for the geometry shader
2384-
const QByteArray geomSrc = 1+R"(
2385-
#version 330
2386-
2385+
const QByteArray geomSrc = StelOpenGL::globalShaderPrefix(StelOpenGL::GEOMETRY_SHADER) + R"(
23872386
layout(lines) in;
23882387
layout(triangle_strip, max_vertices=4) out;
23892388
uniform vec2 viewportSize;
@@ -2436,9 +2435,8 @@ void main()
24362435

24372436
// First a basic shader using constant line color
24382437
QOpenGLShader wideLineVertShader(QOpenGLShader::Vertex);
2439-
wideLineVertShader.compileSourceCode(1+R"(
2440-
#version 330
2441-
in vec3 vertex;
2438+
wideLineVertShader.compileSourceCode(StelOpenGL::globalShaderPrefix(StelOpenGL::VERTEX_SHADER) + R"(
2439+
ATTRIBUTE vec3 vertex;
24422440
uniform mat4 projectionMatrix;
24432441
void main()
24442442
{
@@ -2454,13 +2452,11 @@ void main()
24542452
qWarning().noquote() << "StelPainter: Warnings while compiling wide line geometry shader: " << wideLineGeomShader.log();
24552453

24562454
QOpenGLShader wideLineFragShader(QOpenGLShader::Fragment);
2457-
wideLineFragShader.compileSourceCode(1+R"(
2458-
#version 330
2455+
wideLineFragShader.compileSourceCode(StelOpenGL::globalShaderPrefix(StelOpenGL::FRAGMENT_SHADER) + R"(
24592456
uniform vec4 color;
2460-
out vec4 outputColor;
24612457
void main()
24622458
{
2463-
outputColor = color;
2459+
FRAG_COLOR = color;
24642460
}
24652461
)");
24662462
if (!wideLineFragShader.log().isEmpty())
@@ -2478,16 +2474,15 @@ void main()
24782474

24792475
// Now a version of the shader that supports color interpolated along the line
24802476
QOpenGLShader colorfulWideLineVertShader(QOpenGLShader::Vertex);
2481-
colorfulWideLineVertShader.compileSourceCode(1+R"(
2482-
#version 330
2483-
in vec3 vertex;
2484-
in vec4 color;
2485-
out vec4 varyingColor;
2477+
colorfulWideLineVertShader.compileSourceCode(StelOpenGL::globalShaderPrefix(StelOpenGL::VERTEX_SHADER) + R"(
2478+
ATTRIBUTE vec3 vertex;
2479+
ATTRIBUTE vec4 color;
2480+
VARYING vec4 varyingColor;
24862481
uniform mat4 projectionMatrix;
24872482
void main()
24882483
{
24892484
gl_Position = projectionMatrix*vec4(vertex, 1.);
2490-
varyingColor = color;
2485+
varyingColor = color;
24912486
}
24922487
)");
24932488
if (!colorfulWideLineVertShader.log().isEmpty())
@@ -2499,13 +2494,11 @@ void main()
24992494
qWarning().noquote() << "StelPainter: Warnings while compiling colorful wide line geometry shader: " << colorfulWideLineGeomShader.log();
25002495

25012496
QOpenGLShader colorfulWideLineFragShader(QOpenGLShader::Fragment);
2502-
colorfulWideLineFragShader.compileSourceCode(1+R"(
2503-
#version 330
2504-
in vec4 geomColor;
2505-
out vec4 outputColor;
2497+
colorfulWideLineFragShader.compileSourceCode(StelOpenGL::globalShaderPrefix(StelOpenGL::FRAGMENT_SHADER) + R"(
2498+
VARYING vec4 geomColor;
25062499
void main()
25072500
{
2508-
outputColor = geomColor;
2501+
FRAG_COLOR = geomColor;
25092502
}
25102503
)");
25112504
if (!colorfulWideLineFragShader.log().isEmpty())

0 commit comments

Comments
 (0)