Skip to content

Commit 6f4b094

Browse files
committed
LVGL: Use latest DPF GL3 context details, restore program in ImGui
Signed-off-by: falkTX <falktx@falktx.com>
1 parent 0af6a56 commit 6f4b094

2 files changed

Lines changed: 42 additions & 134 deletions

File tree

generic/LVGL.cpp

Lines changed: 27 additions & 133 deletions
Original file line numberDiff line numberDiff line change
@@ -51,14 +51,11 @@ 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, obuf, vbuf, pos, tex; } gl3 = {};
6058
#endif
61-
#endif
6259

6360
Size<uint> textureSize;
6461
uint8_t* textureData = nullptr;
@@ -133,88 +130,6 @@ struct LVGLWidget<BaseWidget>::PrivateData {
133130
lv_indev_set_group(indev, group);
134131
}
135132

136-
#ifdef DGL_USE_OPENGL3
137-
int status;
138-
GLuint obuffer, vbuffer;
139-
140-
glGenBuffers(1, &obuffer);
141-
DISTRHO_SAFE_ASSERT_RETURN(obuffer != 0, gl3fail());
142-
143-
glGenBuffers(1, &vbuffer);
144-
DISTRHO_SAFE_ASSERT_RETURN(vbuffer != 0, gl3fail());
145-
146-
const GLuint fragment = glCreateShader(GL_FRAGMENT_SHADER);
147-
DISTRHO_SAFE_ASSERT_RETURN(fragment != 0, gl3fail());
148-
149-
const GLuint vertex = glCreateShader(GL_VERTEX_SHADER);
150-
DISTRHO_SAFE_ASSERT_RETURN(vertex != 0, gl3fail());
151-
152-
const GLuint program = glCreateProgram();
153-
DISTRHO_SAFE_ASSERT_RETURN(program != 0, gl3fail());
154-
155-
#if defined(DGL_USE_GLES2)
156-
#define DGL_SHADER_HEADER "#version 100\n"
157-
#elif defined(DGL_USE_GLES3)
158-
#define DGL_SHADER_HEADER "#version 300 es\n"
159-
#else
160-
#define DGL_SHADER_HEADER "#version 150 core\n"
161-
#endif
162-
163-
{
164-
static constexpr const char* const src = DGL_SHADER_HEADER
165-
"precision mediump float;"
166-
"uniform vec4 color;"
167-
"uniform sampler2D stex;"
168-
#ifdef DGL_USE_GLES3
169-
"in vec2 vtex;"
170-
"out vec4 FragColor;"
171-
"void main() { FragColor = texture(stex, vtex); }";
172-
#else
173-
"varying vec2 vtex;"
174-
"void main() { gl_FragColor = texture2D(stex, vtex); }";
175-
#endif
176-
177-
glShaderSource(fragment, 1, &src, nullptr);
178-
glCompileShader(fragment);
179-
180-
glGetShaderiv(fragment, GL_COMPILE_STATUS, &status);
181-
DISTRHO_SAFE_ASSERT_RETURN(status != 0, gl3fail(fragment));
182-
}
183-
184-
{
185-
static constexpr const char* const src = DGL_SHADER_HEADER
186-
#ifdef DGL_USE_GLES3
187-
"in vec4 pos;"
188-
"in vec2 tex;"
189-
"out vec2 vtex;"
190-
#else
191-
"attribute vec4 pos;"
192-
"attribute vec2 tex;"
193-
"varying vec2 vtex;"
194-
#endif
195-
"void main() { gl_Position = pos; vtex = tex; }";
196-
197-
glShaderSource(vertex, 1, &src, nullptr);
198-
glCompileShader(vertex);
199-
200-
glGetShaderiv(vertex, GL_COMPILE_STATUS, &status);
201-
DISTRHO_SAFE_ASSERT_RETURN(status != 0, gl3fail(vertex));
202-
}
203-
204-
glAttachShader(program, fragment);
205-
glAttachShader(program, vertex);
206-
glLinkProgram(program);
207-
208-
glGetProgramiv(program, GL_LINK_STATUS, &status);
209-
DISTRHO_SAFE_ASSERT_RETURN(status != 0, gl3fail());
210-
211-
gl3.prog = program;
212-
gl3.obuf = obuffer;
213-
gl3.vbuf = vbuffer;
214-
gl3.pos = glGetAttribLocation(program, "pos");
215-
gl3.tex = glGetAttribLocation(program, "tex");
216-
#endif
217-
218133
#ifdef DGL_OPENGL
219134
glGenTextures(1, &textureId);
220135
DISTRHO_SAFE_ASSERT_RETURN(textureId != 0,);
@@ -326,24 +241,6 @@ struct LVGLWidget<BaseWidget>::PrivateData {
326241

327242
// ----------------------------------------------------------------------------------------------------------------
328243

329-
#ifdef DGL_USE_OPENGL3
330-
void gl3fail(const GLuint shaderErr = 0)
331-
{
332-
if (shaderErr != 0)
333-
{
334-
GLint len = 0;
335-
glGetShaderiv(shaderErr, GL_INFO_LOG_LENGTH, &len);
336-
337-
std::vector<GLchar> errorLog(len);
338-
glGetShaderInfoLog(shaderErr, len, &len, errorLog.data());
339-
340-
d_stderr2("OpenGL3 shader compilation error: %s", errorLog.data());
341-
}
342-
}
343-
#endif
344-
345-
// ----------------------------------------------------------------------------------------------------------------
346-
347244
static void msleep(const uint32_t millis) noexcept
348245
{
349246
return DISTRHO_NAMESPACE::d_msleep(millis);
@@ -476,24 +373,26 @@ void LVGLWidget<BaseWidget>::onDisplay()
476373
const int32_t height = fullheight;
477374
#endif
478375

479-
#if defined(DGL_CAIRO)
376+
#if defined(DGL_CAIRO)
480377
if (lvglData->surface != nullptr)
481378
{
482379
cairo_t* const handle = static_cast<const CairoGraphicsContext&>(BaseWidget::getGraphicsContext()).handle;
483380
cairo_set_source_surface(handle, lvglData->surface, 0, 0);
484381
cairo_paint(handle);
485382
}
486-
487383
lv_area_set(&lvglData->updatedArea, 0, 0, 0, 0);
488-
#elif defined(DGL_OPENGL)
489-
#ifdef DGL_USE_OPENGL3
490-
if (lvglData->gl3.prog == 0)
384+
#elif defined(DGL_USE_OPENGL3)
385+
const OpenGL3GraphicsContext& gl3context
386+
= static_cast<const OpenGL3GraphicsContext&>(BaseWidget::getGraphicsContext());
387+
if (gl3context.program == 0)
491388
return;
492-
glUseProgram(lvglData->gl3.prog);
493389
glActiveTexture(GL_TEXTURE0);
494-
#else
390+
glUniform1i(gl3context.usingTexture, 1);
391+
#elif defined(DGL_OPENGL)
495392
glEnable(GL_TEXTURE_2D);
496393
#endif
394+
395+
#ifdef DGL_OPENGL
497396
glBindTexture(GL_TEXTURE_2D, lvglData->textureId);
498397

499398
if (lvglData->updatedArea.x1 != lvglData->updatedArea.x2 || lvglData->updatedArea.y1 != lvglData->updatedArea.y2)
@@ -567,37 +466,37 @@ void LVGLWidget<BaseWidget>::onDisplay()
567466

568467
lv_area_set(&lvglData->updatedArea, 0, 0, 0, 0);
569468
}
469+
#endif
570470

571-
#ifdef DGL_USE_OPENGL3
572-
const Window& window = BaseWidget::getWindow();
573-
const int32_t winwidth = static_cast<int32_t>(window.getWidth());
574-
const int32_t winheight = static_cast<int32_t>(window.getHeight());
575-
576-
GLfloat clipw = (static_cast<double>(fullwidth) / winwidth) * 2;
577-
GLfloat cliph = (static_cast<double>(fullheight) / winheight) * 2;
471+
#if defined(DGL_USE_OPENGL3)
472+
GLfloat clipw = (static_cast<double>(fullwidth) / gl3context.width) * 2;
473+
GLfloat cliph = (static_cast<double>(fullheight) / gl3context.height) * 2;
578474

579475
const GLfloat vertices[] = {
580476
-1.f, 1.f, -1.f, 1.f - cliph, -1.f + clipw, 1.f - cliph, -1.f + clipw, 1.f,
581477
0.f, 0.f, 0.f, 1.f, 1.f, 1.f, 1.f, 0.f
582478
};
583-
glBindBuffer(GL_ARRAY_BUFFER, lvglData->gl3.vbuf);
479+
glBindBuffer(GL_ARRAY_BUFFER, gl3context.buffers[0]);
584480
glBufferData(GL_ARRAY_BUFFER, sizeof(vertices), vertices, GL_STREAM_DRAW);
585-
glEnableVertexAttribArray(lvglData->gl3.pos);
586-
glEnableVertexAttribArray(lvglData->gl3.tex);
587-
glVertexAttribPointer(lvglData->gl3.pos, 2, GL_FLOAT, GL_FALSE, 0, nullptr);
588-
glVertexAttribPointer(lvglData->gl3.tex, 2, GL_FLOAT, GL_FALSE, 0, reinterpret_cast<void*>(sizeof(GLfloat) * 8));
481+
glEnableVertexAttribArray(gl3context.bounds);
482+
glEnableVertexAttribArray(gl3context.textureMap);
483+
glVertexAttribPointer(gl3context.bounds, 2, GL_FLOAT, GL_FALSE, 0, nullptr);
484+
glVertexAttribPointer(gl3context.textureMap, 2, GL_FLOAT, GL_FALSE, 0, reinterpret_cast<void*>(sizeof(GLfloat) * 8));
589485

590486
static constexpr const GLubyte order[] = { 0, 1, 2, 0, 2, 3 };
591-
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, lvglData->gl3.obuf);
487+
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, gl3context.buffers[1]);
592488
glBufferData(GL_ELEMENT_ARRAY_BUFFER, sizeof(order), order, GL_STATIC_DRAW);
593489

594490
glDrawElements(GL_TRIANGLES, ARRAY_SIZE(order), GL_UNSIGNED_BYTE, nullptr);
595491

596492
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0);
597-
glDisableVertexAttribArray(lvglData->gl3.tex);
598-
glDisableVertexAttribArray(lvglData->gl3.pos);
493+
glDisableVertexAttribArray(gl3context.textureMap);
494+
glDisableVertexAttribArray(gl3context.bounds);
599495
glBindBuffer(GL_ARRAY_BUFFER, 0);
600-
#else
496+
glUniform1i(gl3context.usingTexture, 0);
497+
498+
glBindTexture(GL_TEXTURE_2D, 0);
499+
#elif defined(DGL_OPENGL)
601500
glBegin(GL_QUADS);
602501
{
603502
glTexCoord2f(0.f, 0.f);
@@ -613,15 +512,10 @@ void LVGLWidget<BaseWidget>::onDisplay()
613512
glVertex2d(0, fullheight);
614513
}
615514
glEnd();
616-
#endif
617515

618516
glBindTexture(GL_TEXTURE_2D, 0);
619-
#ifdef DGL_USE_OPENGL3
620-
glUseProgram(0);
621-
#else
622517
glDisable(GL_TEXTURE_2D);
623518
#endif
624-
#endif
625519
}
626520

627521
template <class BaseWidget>

opengl/DearImGui.cpp

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
/*
22
* Dear ImGui for DPF
33
* Copyright (C) 2021 Jean Pierre Cimalando <jp-dev@inbox.ru>
4-
* Copyright (C) 2021-2024 Filipe Coelho <falktx@falktx.com>
4+
* Copyright (C) 2021-2025 Filipe Coelho <falktx@falktx.com>
55
*
66
* Permission to use, copy, modify, and/or distribute this software for any purpose with
77
* or without fee is hereby granted, provided that the above copyright notice and this
@@ -30,6 +30,10 @@
3030
# define IMGUI_IMPL_OPENGL_LOADER_CUSTOM
3131
#endif
3232

33+
#ifdef DGL_USE_OPENGL3
34+
# include "OpenGL.hpp"
35+
#endif
36+
3337
#ifndef IMGUI_SKIP_IMPLEMENTATION
3438
# define IMGUI_DPF_BACKEND
3539
# include "DearImGui/imgui.cpp"
@@ -214,6 +218,12 @@ void ImGuiWidget<BaseWidget>::idleCallback()
214218
template <class BaseWidget>
215219
void ImGuiWidget<BaseWidget>::onDisplay()
216220
{
221+
#ifdef DGL_USE_OPENGL3
222+
const OpenGL3GraphicsContext& gl3context
223+
= static_cast<const OpenGL3GraphicsContext&>(BaseWidget::getGraphicsContext());
224+
glUseProgram(0);
225+
#endif
226+
217227
ImGui::SetCurrentContext(imData->context);
218228

219229
ImGuiIO& io(ImGui::GetIO());
@@ -247,6 +257,10 @@ void ImGuiWidget<BaseWidget>::onDisplay()
247257
ImGui_ImplOpenGL2_RenderDrawData(data);
248258
#endif
249259
}
260+
261+
#ifdef DGL_USE_OPENGL3
262+
glUseProgram(gl3context.program);
263+
#endif
250264
}
251265

252266
template <class BaseWidget>

0 commit comments

Comments
 (0)