@@ -51,11 +51,15 @@ struct LVGLWidget<BaseWidget>::PrivateData {
5151 double mouseWheelDelta = 0.0 ;
5252 SmallStackRingBuffer keyBuffer;
5353
54- #if defined(DGL_CAIRO)
54+ #if defined(DGL_CAIRO)
5555 cairo_surface_t * surface = nullptr ;
56- #elif defined(DGL_OPENGL)
56+ #elif defined(DGL_OPENGL)
5757 GLuint textureId = 0 ;
58+ #ifdef DGL_USE_OPENGL3
59+ struct { GLuint prog, pos, tex; } gl3 = {};
5860 #endif
61+ #endif
62+
5963 Size<uint> textureSize;
6064 uint8_t * textureData = nullptr ;
6165
@@ -129,24 +133,108 @@ struct LVGLWidget<BaseWidget>::PrivateData {
129133 lv_indev_set_group (indev, group);
130134 }
131135
132- #ifdef DGL_OPENGL
136+ #ifdef DGL_USE_OPENGL3
137+ int status;
138+
139+ const GLuint fragment = glCreateShader (GL_FRAGMENT_SHADER );
140+ DISTRHO_SAFE_ASSERT_RETURN (fragment != 0 , gl3fail ());
141+
142+ const GLuint vertex = glCreateShader (GL_VERTEX_SHADER );
143+ DISTRHO_SAFE_ASSERT_RETURN (vertex != 0 , gl3fail ());
144+
145+ const GLuint program = glCreateProgram ();
146+ DISTRHO_SAFE_ASSERT_RETURN (program != 0 , gl3fail ());
147+
148+ #if defined(DGL_USE_GLES2)
149+ #define DGL_SHADER_HEADER " #version 100\n "
150+ #elif defined(DGL_USE_GLES3)
151+ #define DGL_SHADER_HEADER " #version 300 es\n "
152+ #else
153+ #define DGL_SHADER_HEADER " #version 150 core\n "
154+ #endif
155+
156+ {
157+ static constexpr const char * const src = DGL_SHADER_HEADER
158+ " precision mediump float;"
159+ " uniform vec4 color;"
160+ " uniform sampler2D stex;"
161+ #ifdef DGL_USE_GLES3
162+ " in vec2 vtex;"
163+ " out vec4 FragColor;"
164+ " void main() { FragColor = texture2D(stex, vtex); }" ;
165+ #else
166+ " varying vec2 vtex;"
167+ " void main() { gl_FragColor = texture2D(stex, vtex); }" ;
168+ #endif
169+
170+ glShaderSource (fragment, 1 , &src, nullptr );
171+ glCompileShader (fragment);
172+
173+ glGetShaderiv (fragment, GL_COMPILE_STATUS , &status);
174+ DISTRHO_SAFE_ASSERT_RETURN (status != 0 , gl3fail (fragment));
175+ }
176+
177+ {
178+ static constexpr const char * const src = DGL_SHADER_HEADER
179+ #ifdef DGL_USE_GLES3
180+ " in vec4 pos;"
181+ " in vec2 tex;"
182+ " out vec2 vtex;"
183+ #else
184+ " attribute vec4 pos;"
185+ " attribute vec2 tex;"
186+ " varying vec2 vtex;"
187+ #endif
188+ " void main() { gl_Position = pos; vtex = tex; }" ;
189+
190+ glShaderSource (vertex, 1 , &src, nullptr );
191+ glCompileShader (vertex);
192+
193+ glGetShaderiv (vertex, GL_COMPILE_STATUS , &status);
194+ DISTRHO_SAFE_ASSERT_RETURN (status != 0 , gl3fail (vertex));
195+ }
196+
197+ glAttachShader (program, fragment);
198+ glAttachShader (program, vertex);
199+ glLinkProgram (program);
200+
201+ glGetProgramiv (program, GL_LINK_STATUS , &status);
202+ DISTRHO_SAFE_ASSERT_RETURN (status != 0 , gl3fail ());
203+
204+ gl3.prog = program;
205+ gl3.pos = glGetAttribLocation (program, " pos" );
206+ gl3.tex = glGetAttribLocation (program, " tex" );
207+ #endif
208+
209+ #ifdef DGL_OPENGL
133210 glGenTextures (1 , &textureId);
134211 DISTRHO_SAFE_ASSERT_RETURN (textureId != 0 ,);
135212
213+ #ifndef DGL_USE_OPENGL3
136214 glEnable (GL_TEXTURE_2D );
215+ #endif
137216 glBindTexture (GL_TEXTURE_2D , textureId);
138217
218+ #if LV_COLOR_DEPTH == 8 && defined(DGL_USE_OPENGL3) && !defined(DGL_USE_GLES2)
219+ glTexParameteri (GL_TEXTURE_2D , GL_TEXTURE_SWIZZLE_G , GL_RED );
220+ glTexParameteri (GL_TEXTURE_2D , GL_TEXTURE_SWIZZLE_B , GL_RED );
221+ #endif
222+
139223 glTexParameteri (GL_TEXTURE_2D , GL_TEXTURE_MIN_FILTER , GL_LINEAR );
140224 glTexParameteri (GL_TEXTURE_2D , GL_TEXTURE_MAG_FILTER , GL_LINEAR );
141225 glTexParameteri (GL_TEXTURE_2D , GL_TEXTURE_WRAP_S , GL_CLAMP_TO_EDGE );
142226 glTexParameteri (GL_TEXTURE_2D , GL_TEXTURE_WRAP_T , GL_CLAMP_TO_EDGE );
143227
228+ #ifndef DGL_USE_GLES
144229 static constexpr const float transparent[] = { 0 .f , 0 .f , 0 .f , 0 .f };
145230 glTexParameterfv (GL_TEXTURE_2D , GL_TEXTURE_BORDER_COLOR , transparent);
231+ #endif
146232
147233 glBindTexture (GL_TEXTURE_2D , 0 );
234+ #ifndef DGL_USE_OPENGL3
148235 glDisable (GL_TEXTURE_2D );
149236 #endif
237+ #endif
150238
151239 lv_display_set_driver_data (display, this );
152240 lv_display_set_flush_cb (display, flush_cb);
@@ -229,6 +317,24 @@ struct LVGLWidget<BaseWidget>::PrivateData {
229317
230318 // ----------------------------------------------------------------------------------------------------------------
231319
320+ #ifdef DGL_USE_OPENGL3
321+ void gl3fail (const GLuint shaderErr = 0 )
322+ {
323+ if (shaderErr != 0 )
324+ {
325+ GLint len = 0 ;
326+ glGetShaderiv (shaderErr, GL_INFO_LOG_LENGTH , &len);
327+
328+ std::vector<GLchar> errorLog (len);
329+ glGetShaderInfoLog (shaderErr, len, &len, errorLog.data ());
330+
331+ d_stderr2 (" OpenGL3 shader compilation error: %s" , errorLog.data ());
332+ }
333+ }
334+ #endif
335+
336+ // ----------------------------------------------------------------------------------------------------------------
337+
232338 static void msleep (const uint32_t millis) noexcept
233339 {
234340 return DISTRHO_NAMESPACE::d_msleep (millis);
@@ -346,28 +452,7 @@ void LVGLWidget<BaseWidget>::onDisplay()
346452 const int32_t height = fullheight;
347453 #endif
348454
349- #if 0
350- // TODO see what is really needed here..
351- glColor4f(1.f, 1.f, 1.f, 1.f);
352- // glClearColor();
353- glBegin(GL_QUADS);
354- {
355- glTexCoord2f(0.f, 0.f);
356- glVertex2d(0, 0);
357-
358- glTexCoord2f(1.f, 0.f);
359- glVertex2d(width, 0);
360-
361- glTexCoord2f(1.f, 1.f);
362- glVertex2d(width, height);
363-
364- glTexCoord2f(0.f, 1.f);
365- glVertex2d(0, height);
366- }
367- glEnd();
368- #endif
369-
370- #if defined(DGL_CAIRO)
455+ #if defined(DGL_CAIRO)
371456 if (lvglData->surface != nullptr )
372457 {
373458 cairo_t * const handle = static_cast <const CairoGraphicsContext&>(BaseWidget::getGraphicsContext ()).handle ;
@@ -376,27 +461,38 @@ void LVGLWidget<BaseWidget>::onDisplay()
376461 }
377462
378463 lv_area_set (&lvglData->updatedArea , 0 , 0 , 0 , 0 );
379- #elif defined(DGL_OPENGL)
464+ #elif defined(DGL_OPENGL)
465+ #ifdef DGL_USE_OPENGL3
466+ if (lvglData->gl3 .prog == 0 )
467+ return ;
468+ glUseProgram (lvglData->gl3 .prog );
469+ glActiveTexture (GL_TEXTURE0 );
470+ #else
380471 glEnable (GL_TEXTURE_2D );
472+ #endif
381473 glBindTexture (GL_TEXTURE_2D , lvglData->textureId );
382474
383475 if (lvglData->updatedArea .x1 != lvglData->updatedArea .x2 || lvglData->updatedArea .y1 != lvglData->updatedArea .y2 )
384476 {
385- #if LV_COLOR_DEPTH == 32
477+ #if LV_COLOR_DEPTH == 32
386478 static constexpr const GLenum format = GL_BGRA ;
387479 static constexpr const GLenum ftype = GL_UNSIGNED_BYTE ;
388- #elif LV_COLOR_DEPTH == 24
480+ #elif LV_COLOR_DEPTH == 24
389481 static constexpr const GLenum format = GL_BGR ;
390482 static constexpr const GLenum ftype = GL_UNSIGNED_BYTE ;
391- #elif LV_COLOR_DEPTH == 16
483+ #elif LV_COLOR_DEPTH == 16
392484 static constexpr const GLenum format = GL_RGB ;
393485 static constexpr const GLenum ftype = GL_UNSIGNED_SHORT_5_6_5 ;
394- #elif LV_COLOR_DEPTH == 8
486+ #elif LV_COLOR_DEPTH == 8
487+ #if defined(DGL_USE_OPENGL3) && !defined(DGL_USE_GLES2)
488+ static constexpr const GLenum format = GL_RED ;
489+ #else
395490 static constexpr const GLenum format = GL_LUMINANCE ;
491+ #endif
396492 static constexpr const GLenum ftype = GL_UNSIGNED_BYTE ;
397- #else
493+ #else
398494 #error Unsupported color format
399- #endif
495+ #endif
400496
401497 glPixelStorei (GL_UNPACK_ALIGNMENT , 1 );
402498 glPixelStorei (GL_UNPACK_ROW_LENGTH , width);
@@ -432,6 +528,21 @@ void LVGLWidget<BaseWidget>::onDisplay()
432528 lv_area_set (&lvglData->updatedArea , 0 , 0 , 0 , 0 );
433529 }
434530
531+ #ifdef DGL_USE_OPENGL3
532+ const GLfloat vertices[] = { -1 .f , 1 .f , -1 .f , -1 .f , 1 .f , -1 .f , 1 .f , 1 .f };
533+ glVertexAttribPointer (lvglData->gl3 .pos , 2 , GL_FLOAT , GL_FALSE , 0 , vertices);
534+ glEnableVertexAttribArray (lvglData->gl3 .pos );
535+
536+ const GLfloat vtex[] = { 0 .f , 0 .f , 0 .f , 1 .f , 1 .f , 1 .f , 1 .f , 0 .f };
537+ glVertexAttribPointer (lvglData->gl3 .tex , 2 , GL_FLOAT , GL_FALSE , 0 , vtex);
538+ glEnableVertexAttribArray (lvglData->gl3 .tex );
539+
540+ const GLubyte order[] = { 0 , 1 , 2 , 0 , 2 , 3 };
541+ glDrawElements (GL_TRIANGLES , ARRAY_SIZE (order), GL_UNSIGNED_BYTE , order);
542+
543+ glDisableVertexAttribArray (lvglData->gl3 .tex );
544+ glDisableVertexAttribArray (lvglData->gl3 .pos );
545+ #else
435546 glBegin (GL_QUADS );
436547 {
437548 glTexCoord2f (0 .f , 0 .f );
@@ -447,10 +558,13 @@ void LVGLWidget<BaseWidget>::onDisplay()
447558 glVertex2d (0 , fullheight);
448559 }
449560 glEnd ();
561+ #endif
450562
451563 glBindTexture (GL_TEXTURE_2D , 0 );
564+ #ifndef DGL_USE_OPENGL3
452565 glDisable (GL_TEXTURE_2D );
453566 #endif
567+ #endif
454568}
455569
456570template <class BaseWidget >
0 commit comments