Skip to content

Commit c2c0060

Browse files
committed
Initial support for OpenGL ES 3.0 (GLES3)
1 parent a4eefdf commit c2c0060

9 files changed

Lines changed: 109 additions & 9 deletions

File tree

src/app/GUI/Settings/pluginssettingswidget.cpp

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,10 @@ PluginsSettingsWidget::PluginsSettingsWidget(QWidget *parent)
4141
, mShaderPath(nullptr)
4242
, mShaderTree(nullptr)
4343
{
44+
#ifdef USE_GLES
45+
const auto label = new QLabel(tr("Shaders not supported when using OpenGL ES 3.0."), this);
46+
addWidget(label);
47+
#else
4448
mShadersList = EffectsLoader::sInstance->getLoadedShaderEffects();
4549

4650
mShadersDisabled = AppSupport::getSettings("settings",
@@ -93,10 +97,12 @@ PluginsSettingsWidget::PluginsSettingsWidget(QWidget *parent)
9397
.arg(tr("Any changes in this section require a restart of Friction"),
9498
tr("Also note that shader effects are still considered experimental")));
9599
addWidget(infoLabel);
100+
#endif
96101
}
97102

98103
void PluginsSettingsWidget::applySettings()
99104
{
105+
#ifndef USE_GLES
100106
AppSupport::setSettings("settings",
101107
"CustomShaderPath",
102108
mShaderPath->text());
@@ -109,21 +115,27 @@ void PluginsSettingsWidget::applySettings()
109115
AppSupport::setSettings("settings",
110116
"DisabledShaders",
111117
disabledShaders);
118+
#endif
112119
}
113120

114121
void PluginsSettingsWidget::updateSettings(bool restore)
115122
{
123+
#ifndef USE_GLES
116124
mShaderPath->setText(AppSupport::getAppShaderEffectsPath(restore));
117125
mShadersDisabled = AppSupport::getSettings("settings",
118126
"DisabledShaders").toStringList();
119127
if (restore) {
120128
mShadersDisabled.clear();
121129
populateShaderTree();
122130
}
131+
#else
132+
Q_UNUSED(restore)
133+
#endif
123134
}
124135

125136
void PluginsSettingsWidget::populateShaderTree()
126137
{
138+
#ifndef USE_GLES
127139
mShaderTree->setSortingEnabled(false);
128140
mShaderTree->clear();
129141
for (auto &shader: mShadersList) {
@@ -138,4 +150,5 @@ void PluginsSettingsWidget::populateShaderTree()
138150
}
139151
mShaderTree->setSortingEnabled(true);
140152
mShaderTree->sortByColumn(0, Qt::AscendingOrder);
153+
#endif
141154
}

src/app/main.cpp

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,10 +51,23 @@
5151
void setDefaultFormat()
5252
{
5353
QApplication::setAttribute(Qt::AA_ShareOpenGLContexts);
54+
55+
#ifdef USE_GLES
56+
QApplication::setAttribute(Qt::AA_UseOpenGLES);
57+
#else
5458
QApplication::setAttribute(Qt::AA_UseDesktopOpenGL);
59+
#endif
60+
5561
QSurfaceFormat format;
62+
63+
#ifdef USE_GLES
64+
format.setVersion(3, 0);
65+
format.setProfile(QSurfaceFormat::NoProfile);
66+
#else
5667
format.setVersion(3, 3);
5768
format.setProfile(QSurfaceFormat::CoreProfile);
69+
#endif
70+
5871
format.setDepthBufferSize(24);
5972
format.setStencilBufferSize(8);
6073
format.setSamples(0);
@@ -316,6 +329,7 @@ int main(int argc, char *argv[])
316329
}
317330

318331
// init shaders
332+
#ifndef USE_GLES
319333
try {
320334
effectsLoader.iniShaderEffects();
321335
} catch(const std::exception& e) {
@@ -329,6 +343,7 @@ int main(int argc, char *argv[])
329343
}
330344
document.actionFinished();
331345
});
346+
#endif
332347

333348
// disabled for now
334349
//effectsLoader.iniCustomBoxes();

src/cmake/friction-common.cmake

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ option(MAC_DEPLOY "Mac Deploy" OFF)
3030
option(WIN_DEPLOY "Windows Deploy" OFF)
3131
option(BUILD_SKIA "Build skia" ON)
3232
option(SKIA_STATIC "Static skia" OFF)
33+
option(USE_GLES "Use OpenGL ES 3.0 (experimental)" OFF)
3334

3435
set(SKIA_LIB_PATH "/mnt/skia" CACHE STRING "Path to prebuilt skia library")
3536

@@ -42,6 +43,9 @@ endif()
4243
if(${MAC_DEPLOY})
4344
add_definitions(-DMAC_DEPLOY)
4445
endif()
46+
if(USE_GLES)
47+
add_compile_definitions(USE_GLES)
48+
endif()
4549

4650
if(NOT APPLE)
4751
if(UNIX AND NOT CMAKE_CXX_COMPILER_ID STREQUAL "Clang")

src/core/Private/Tasks/offscreenqgl33c.h

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -46,13 +46,24 @@ class CORE_EXPORT OffscreenQGL33c : public QGL33 {
4646
}
4747

4848
void makeCurrent() {
49-
if(!mContext->makeCurrent(mOffscreenSurface))
49+
if (!mContext->makeCurrent(mOffscreenSurface)) {
50+
#ifdef USE_GLES
51+
PrettyRuntimeThrow("Making OpenGL ES context current failed.\n"
52+
"Make sure your GPU drivers support OpenGL ES 3.0.");
53+
#else
5054
PrettyRuntimeThrow("Making OpenGL context current failed.\n"
5155
"Make sure your GPU drivers support OpenGL 3.3 core.");
52-
if(!mInitialized) {
53-
if(!initializeOpenGLFunctions())
56+
#endif
57+
}
58+
if (!mInitialized) {
59+
#ifdef USE_GLES
60+
initializeOpenGLFunctions();
61+
#else
62+
if (!initializeOpenGLFunctions()) {
5463
PrettyRuntimeThrow("Initializing OpenGL 3.3 core functions failed.\n"
5564
"Make sure your GPU drivers support OpenGL 3.3 core.");
65+
}
66+
#endif
5667
mInitialized = true;
5768
}
5869
}

src/core/etexture.cpp

Lines changed: 23 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -59,10 +59,31 @@ SkBitmap eTexture::bitmapSnapshot(QGL33 * const gl) const {
5959
SkBitmap bitmap;
6060
const auto info = SkiaHelpers::getPremulRGBAInfo(fWidth, fHeight);
6161
bitmap.allocPixels(info);
62+
63+
#ifdef USE_GLES
64+
GLint currentTexture = 0;
65+
gl->glGetIntegerv(GL_TEXTURE_BINDING_2D, &currentTexture);
66+
67+
GLint oldFbo = 0;
68+
gl->glGetIntegerv(GL_FRAMEBUFFER_BINDING, &oldFbo);
69+
70+
GLuint tempFbo = 0;
71+
gl->glGenFramebuffers(1, &tempFbo);
72+
gl->glBindFramebuffer(GL_FRAMEBUFFER, tempFbo);
73+
74+
gl->glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0,
75+
GL_TEXTURE_2D, currentTexture, 0);
76+
77+
gl->glReadPixels(0, 0, fWidth, fHeight, GL_RGBA,
78+
GL_UNSIGNED_BYTE, bitmap.getPixels());
79+
80+
gl->glBindFramebuffer(GL_FRAMEBUFFER, oldFbo);
81+
gl->glDeleteFramebuffers(1, &tempFbo);
82+
#else
6283
gl->glGetTexImage(GL_TEXTURE_2D, 0, GL_RGBA,
6384
GL_UNSIGNED_BYTE, bitmap.getPixels());
64-
// glReadPixels(0, 0, fWidth, fHeight,
65-
// GL_RGBA, GL_UNSIGNED_BYTE, btmp.getPixels());
85+
#endif
86+
6687
return bitmap;
6788
}
6889

src/core/glhelpers.cpp

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -154,8 +154,23 @@ void gIniProgram(QGL33 * const gl, GLuint& program,
154154
} catch(...) {
155155
RuntimeThrow("Could not load shader data from file.");
156156
}
157-
const char* const vShaderCode = vertexCode.c_str();
158-
const char* const fShaderCode = fragmentCode.c_str();
157+
158+
#ifdef USE_GLES
159+
auto patchShaderGLES = [](std::string& code) {
160+
size_t pos = code.find("#version");
161+
if (pos != std::string::npos) {
162+
size_t endLine = code.find("\n", pos);
163+
if (endLine != std::string::npos) {
164+
code.replace(pos, endLine - pos, "#version 300 es\nprecision highp float;");
165+
}
166+
}
167+
};
168+
patchShaderGLES(vertexCode);
169+
patchShaderGLES(fragmentCode);
170+
#endif
171+
172+
const char* vShaderCode = vertexCode.c_str();
173+
const char* fShaderCode = fragmentCode.c_str();
159174

160175
const GLuint vertexShader = gl->glCreateShader(GL_VERTEX_SHADER);
161176
gl->glShaderSource(vertexShader, 1, &vShaderCode, nullptr);

src/core/glhelpers.h

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,13 +26,19 @@
2626
#ifndef GLHELPERS_H
2727
#define GLHELPERS_H
2828

29-
#include <QOpenGLFunctions_3_3_Core>
3029
#include <QPoint>
3130
#include "skia/skiaincludes.h"
3231

3332
#include "exceptions.h"
3433

34+
#if USE_GLES
35+
#include <QOpenGLExtraFunctions>
36+
typedef QOpenGLExtraFunctions QGL33;
37+
#else
38+
#include <QOpenGLFunctions_3_3_Core>
3539
typedef QOpenGLFunctions_3_3_Core QGL33;
40+
#endif
41+
3642
#define BUFFER_OFFSET(i) ((void*)(i))
3743

3844
CORE_EXPORT

src/ui/widgets/glwidget.cpp

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,9 +38,13 @@ GLWidget::GLWidget(QWidget *parent) : QOpenGLWidget(parent)
3838

3939
void GLWidget::initializeGL()
4040
{
41+
#ifdef USE_GLES
42+
initializeOpenGLFunctions();
43+
#else
4144
if (!initializeOpenGLFunctions()) {
4245
RuntimeThrow(tr("Initializing OpenGL 3.3 failed."));
4346
}
47+
#endif
4448

4549
glClearColor(0, 0, 0, 1);
4650

src/ui/widgets/glwindow.cpp

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -84,9 +84,20 @@ void GLWindow::initializeGL() {
8484
RuntimeThrow("Application-wide shared OpenGL context not found");
8585
if(context()->shareContext() != globalCtx)
8686
context()->setShareContext(globalCtx);
87-
if(!initializeOpenGLFunctions())
87+
88+
#ifdef USE_GLES
89+
if (!context() || !context()->isValid()) {
90+
RuntimeThrow("Initializing OpenGL ES failed. Context is invalid. "
91+
"Make sure your GPU supports OpenGL ES 3.0.");
92+
}
93+
initializeOpenGLFunctions();
94+
#else
95+
if (!initializeOpenGLFunctions()) {
8896
RuntimeThrow("Initializing OpenGL 3.3 functions failed. "
8997
"Make sure your GPU supports OpenGL 3.3.");
98+
}
99+
#endif
100+
90101
initialize();
91102
} catch(const std::exception& e) {
92103
gPrintExceptionFatal(e);

0 commit comments

Comments
 (0)