Skip to content

Commit 58694cd

Browse files
committed
More GL3/GLES compat fixes
Signed-off-by: falkTX <falktx@falktx.com>
1 parent d4b8048 commit 58694cd

File tree

5 files changed

+82
-35
lines changed

5 files changed

+82
-35
lines changed

Makefile.base.mk

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,8 @@
2525
# USE_NANOVG_FBO=false
2626
# USE_NANOVG_FREETYPE=false
2727
# USE_FILE_BROWSER=true
28+
# USE_GLES2=false
29+
# USE_GLES3=false
2830
# USE_WEB_VIEW=false
2931

3032
# STATIC_BUILD=true
@@ -465,7 +467,7 @@ endif
465467
else ifeq ($(WASM),true)
466468

467469
# wasm builds cannot work using regular desktop OpenGL
468-
ifeq (,$(USE_GLES2)$(USE_GLES3))
470+
ifeq (,$(findstring true,$(USE_GLES2)$(USE_GLES3)))
469471
USE_GLES2 = true
470472
endif
471473

@@ -538,14 +540,14 @@ else ifeq ($(MACOS),true)
538540
OPENGL_FLAGS = -DGL_SILENCE_DEPRECATION=1 -Wno-deprecated-declarations
539541
OPENGL_LIBS = -framework OpenGL
540542
else ifeq ($(WASM),true)
541-
ifeq ($(USE_GLES2),true)
542-
OPENGL_LIBS = -sMIN_WEBGL_VERSION=2 -sMAX_WEBGL_VERSION=2
543+
OPENGL_FLAGS =
544+
ifeq ($(USE_GLES3),true)
545+
OPENGL_LIBS = -sMIN_WEBGL_VERSION=3 -sMAX_WEBGL_VERSION=3
543546
else
544-
ifneq ($(USE_GLES3),true)
545-
OPENGL_LIBS = -sLEGACY_GL_EMULATION -sGL_UNSAFE_OPTS=0
546-
endif
547+
OPENGL_LIBS = -sMIN_WEBGL_VERSION=2 -sMAX_WEBGL_VERSION=2
547548
endif
548549
else ifeq ($(WINDOWS),true)
550+
OPENGL_FLAGS =
549551
OPENGL_LIBS = -lopengl32
550552
else
551553
OPENGL_FLAGS = $(shell $(PKG_CONFIG) --cflags gl x11)
@@ -575,7 +577,7 @@ DGL_FLAGS += -DHAVE_VULKAN
575577
VULKAN_FLAGS = $(shell $(PKG_CONFIG) --cflags vulkan)
576578
VULKAN_LIBS = $(shell $(PKG_CONFIG) --libs vulkan)
577579

578-
ifneq ($(WINDOWS),true)
580+
ifneq ($(HAIKU_OR_MACOS_OR_WASM_OR_WINDOWS),true)
579581
VULKAN_LIBS += -ldl
580582
endif
581583

dgl/OpenGL-include.hpp

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,12 @@
5353

5454
#ifdef DISTRHO_OS_MAC
5555
# ifdef DGL_USE_OPENGL3
56+
# ifdef DGL_USE_GLES
57+
# warning GLES with macOS is not supported
58+
# undef DGL_USE_GLES
59+
# undef DGL_USE_GLES2
60+
# undef DGL_USE_GLES3
61+
# endif
5662
# include <OpenGL/gl3.h>
5763
# include <OpenGL/gl3ext.h>
5864
# else
@@ -63,8 +69,16 @@
6369
# define GL_GLEXT_PROTOTYPES
6470
# endif
6571
# ifndef __GLEW_H__
66-
# include <GL/gl.h>
67-
# include <GL/glext.h>
72+
# if defined(DGL_USE_GLES2)
73+
# include <GLES2/gl2.h>
74+
# include <GLES2/gl2ext.h>
75+
# elif defined(DGL_USE_GLES3)
76+
# include <GLES3/gl3.h>
77+
# include <GLES3/gl3ext.h>
78+
# else
79+
# include <GL/gl.h>
80+
# include <GL/glext.h>
81+
# endif
6882
# endif
6983
#endif
7084

dgl/OpenGL.hpp

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -31,16 +31,18 @@ ImageFormat asDISTRHOImageFormat(const GLenum format)
3131
{
3232
switch (format)
3333
{
34-
#if defined(DGL_USE_OPENGL3)
34+
#if defined(DGL_USE_OPENGL3) && !defined(DGL_USE_GLES2)
3535
case GL_RED:
3636
#else
3737
case GL_LUMINANCE:
3838
#endif
3939
return kImageFormatGrayscale;
40+
#ifndef DGL_USE_GLES
4041
case GL_BGR:
4142
return kImageFormatBGR;
4243
case GL_BGRA:
4344
return kImageFormatBGRA;
45+
#endif
4446
case GL_RGB:
4547
return kImageFormatRGB;
4648
case GL_RGBA:
@@ -58,22 +60,30 @@ GLenum asOpenGLImageFormat(const ImageFormat format)
5860
case kImageFormatNull:
5961
break;
6062
case kImageFormatGrayscale:
61-
#if defined(DGL_USE_OPENGL3)
63+
#if defined(DGL_USE_OPENGL3) && !defined(DGL_USE_GLES2)
6264
return GL_RED;
6365
#else
6466
return GL_LUMINANCE;
6567
#endif
6668
case kImageFormatBGR:
69+
#ifndef DGL_USE_GLES
6770
return GL_BGR;
71+
#else
72+
return 0;
73+
#endif
6874
case kImageFormatBGRA:
75+
#ifndef DGL_USE_GLES
6976
return GL_BGRA;
77+
#else
78+
return 0;
79+
#endif
7080
case kImageFormatRGB:
7181
return GL_RGB;
7282
case kImageFormatRGBA:
7383
return GL_RGBA;
7484
}
7585

76-
return 0x0;
86+
return 0;
7787
}
7888

7989
// --------------------------------------------------------------------------------------------------------------------

dgl/src/OpenGL3.cpp

Lines changed: 27 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -406,41 +406,44 @@ static void setupOpenGLImage(const OpenGLImage& image, GLuint textureId)
406406
DISTRHO_SAFE_ASSERT_RETURN(image.isValid(),);
407407

408408
const ImageFormat imageFormat = image.getFormat();
409-
GLint intformat = GL_RGBA;
409+
GLint intformat;
410410

411-
#ifdef DGL_USE_GLES2
412-
// GLESv2 does not support BGR
411+
#ifdef DGL_USE_GLES
412+
// GLES does not support BGR
413413
DISTRHO_SAFE_ASSERT_RETURN(imageFormat != kImageFormatBGR && imageFormat != kImageFormatBGRA,);
414414
#endif
415415

416+
glBindTexture(GL_TEXTURE_2D, textureId);
417+
416418
switch (imageFormat)
417419
{
418420
case kImageFormatBGR:
419421
case kImageFormatRGB:
420422
intformat = GL_RGB;
421423
break;
422424
case kImageFormatGrayscale:
423-
#if defined(DGL_USE_GLES)
424-
intformat = GL_R8;
425-
#elif defined(DGL_USE_OPENGL3)
426-
intformat = GL_RED;
427-
#else
425+
#ifdef DGL_USE_GLES2
428426
intformat = GL_LUMINANCE;
427+
#else
428+
intformat = GL_RED;
429+
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_SWIZZLE_G, GL_RED);
430+
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_SWIZZLE_B, GL_RED);
429431
#endif
430432
break;
431433
default:
434+
intformat = GL_RGBA;
432435
break;
433436
}
434437

435-
glBindTexture(GL_TEXTURE_2D, textureId);
436-
437438
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
438439
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
439440
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_BORDER);
440441
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_BORDER);
441442

443+
#ifndef DGL_USE_GLES
442444
static const float trans[] = { 0.0f, 0.0f, 0.0f, 0.0f };
443445
glTexParameterfv(GL_TEXTURE_2D, GL_TEXTURE_BORDER_COLOR, trans);
446+
#endif
444447

445448
glPixelStorei(GL_PACK_ALIGNMENT, 1);
446449
glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
@@ -572,8 +575,10 @@ void ImageBaseKnob<OpenGLImage>::onDisplay()
572575
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_BORDER);
573576
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_BORDER);
574577

578+
#ifndef DGL_USE_GLES
575579
static const float trans[] = { 0.0f, 0.0f, 0.0f, 0.0f };
576580
glTexParameterfv(GL_TEXTURE_2D, GL_TEXTURE_BORDER_COLOR, trans);
581+
#endif
577582

578583
glPixelStorei(GL_PACK_ALIGNMENT, 1);
579584
glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
@@ -740,8 +745,14 @@ DGL_EXT(PFNGLVERTEXATTRIBPOINTERPROC, glVertexAttribPointer)
740745
"uniform vec4 color;"
741746
"uniform sampler2D stex;"
742747
"uniform bool texok;"
748+
#ifdef DGL_USE_GLES3
749+
"in vec2 vtex;"
750+
"out vec4 FragColor;"
751+
"void main() { FragColor = texok ? texture2D(stex, vtex) : color; }";
752+
#else
743753
"varying vec2 vtex;"
744754
"void main() { gl_FragColor = texok ? texture2D(stex, vtex) : color; }";
755+
#endif
745756

746757
glShaderSource(fragment, 1, &src, nullptr);
747758
glCompileShader(fragment);
@@ -752,9 +763,15 @@ DGL_EXT(PFNGLVERTEXATTRIBPOINTERPROC, glVertexAttribPointer)
752763

753764
{
754765
static constexpr const char* const src = DGL_SHADER_HEADER
766+
#ifdef DGL_USE_GLES3
767+
"in vec4 pos;"
768+
"in vec2 tex;"
769+
"out vec2 vtex;"
770+
#else
755771
"attribute vec4 pos;"
756772
"attribute vec2 tex;"
757773
"varying vec2 vtex;"
774+
#endif
758775
"void main() { gl_Position = pos; vtex = tex; }";
759776

760777
glShaderSource(vertex, 1, &src, nullptr);

dgl/src/nanovg/nanovg_gl.h

Lines changed: 17 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -173,7 +173,7 @@ struct GLNVGtexture {
173173
int width, height;
174174
int type;
175175
int flags;
176-
#if defined NANOVG_GLES2
176+
#if defined(NANOVG_GLES2) || defined(NANOVG_GLES3)
177177
unsigned char* data;
178178
#endif
179179
};
@@ -813,8 +813,8 @@ static int glnvg__renderCreateTexture(void* uptr, int type, int w, int h, int im
813813
switch (type)
814814
{
815815
case NVG_TEXTURE_BGR:
816-
#if NANOVG_GLES2
817-
// GLES2 cannot handle GL_BGR, do local conversion to GL_RGB
816+
#if defined(NANOVG_GLES2) || defined(NANOVG_GLES3)
817+
// GLES cannot handle GL_BGR, do local conversion to GL_RGB
818818
tex->data = (uint8_t*)malloc(sizeof(uint8_t) * 3 * w * h);
819819
for (int i = 0; i < w * h; ++i)
820820
{
@@ -829,8 +829,8 @@ static int glnvg__renderCreateTexture(void* uptr, int type, int w, int h, int im
829829
#endif
830830
break;
831831
case NVG_TEXTURE_BGRA:
832-
#if NANOVG_GLES2
833-
// GLES2 cannot handle GL_BGRA, do local conversion to GL_RGBA
832+
#if defined(NANOVG_GLES2) || defined(NANOVG_GLES3)
833+
// GLES cannot handle GL_BGRA, do local conversion to GL_RGBA
834834
tex->data = (uint8_t*)malloc(sizeof(uint8_t) * 4 * w * h);
835835
for (int i = 0; i < w * h; ++i)
836836
{
@@ -840,7 +840,7 @@ static int glnvg__renderCreateTexture(void* uptr, int type, int w, int h, int im
840840
tex->data[i*3+3] = data[i*3+0];
841841
}
842842
data = tex->data;
843-
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, w, h, 0, GL_BGRA, GL_UNSIGNED_BYTE, data);
843+
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, w, h, 0, GL_RGBA, GL_UNSIGNED_BYTE, data);
844844
#else
845845
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, w, h, 0, GL_BGRA, GL_UNSIGNED_BYTE, data);
846846
#endif
@@ -852,11 +852,11 @@ static int glnvg__renderCreateTexture(void* uptr, int type, int w, int h, int im
852852
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, w, h, 0, GL_RGBA, GL_UNSIGNED_BYTE, data);
853853
break;
854854
default:
855-
#if defined (NANOVG_GL2)
855+
#if defined(NANOVG_GL2) || defined(NANOVG_GLES2)
856856
glTexImage2D(GL_TEXTURE_2D, 0, GL_LUMINANCE, w, h, 0, GL_LUMINANCE, GL_UNSIGNED_BYTE, data);
857-
#elif defined(NANOVG_GLES2) || defined(NANOVG_GLES3)
858-
glTexImage2D(GL_TEXTURE_2D, 0, GL_R8, w, h, 0, GL_RED, GL_UNSIGNED_BYTE, data);
859857
#else
858+
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_SWIZZLE_G, GL_RED);
859+
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_SWIZZLE_B, GL_RED);
860860
glTexImage2D(GL_TEXTURE_2D, 0, GL_RED, w, h, 0, GL_RED, GL_UNSIGNED_BYTE, data);
861861
#endif
862862
break;
@@ -960,19 +960,23 @@ static int glnvg__renderUpdateTexture(void* uptr, int image, int x, int y, int w
960960
switch (tex->type)
961961
{
962962
case NVG_TEXTURE_BGR:
963+
#if !(defined(NANOVG_GLES2) || defined(NANOVG_GLES3))
963964
glTexSubImage2D(GL_TEXTURE_2D, 0, x,y, w,h, GL_BGR, GL_UNSIGNED_BYTE, data);
964965
break;
965-
case NVG_TEXTURE_BGRA:
966-
glTexSubImage2D(GL_TEXTURE_2D, 0, x,y, w,h, GL_BGRA, GL_UNSIGNED_BYTE, data);
967-
break;
966+
#endif
968967
case NVG_TEXTURE_RGB:
969968
glTexSubImage2D(GL_TEXTURE_2D, 0, x,y, w,h, GL_RGB, GL_UNSIGNED_BYTE, data);
970969
break;
970+
case NVG_TEXTURE_BGRA:
971+
#if !(defined(NANOVG_GLES2) || defined(NANOVG_GLES3))
972+
glTexSubImage2D(GL_TEXTURE_2D, 0, x,y, w,h, GL_BGRA, GL_UNSIGNED_BYTE, data);
973+
break;
974+
#endif
971975
case NVG_TEXTURE_RGBA:
972976
glTexSubImage2D(GL_TEXTURE_2D, 0, x,y, w,h, GL_RGBA, GL_UNSIGNED_BYTE, data);
973977
break;
974978
default:
975-
#if defined(NANOVG_GL2)
979+
#if defined(NANOVG_GL2) || defined(NANOVG_GLES2)
976980
glTexSubImage2D(GL_TEXTURE_2D, 0, x,y, w,h, GL_LUMINANCE, GL_UNSIGNED_BYTE, data);
977981
#else
978982
glTexSubImage2D(GL_TEXTURE_2D, 0, x,y, w,h, GL_RED, GL_UNSIGNED_BYTE, data);

0 commit comments

Comments
 (0)