Browse Source

opengl3: bind all buffers so it works in more contexts, like wasm

Signed-off-by: falkTX <falktx@falktx.com>
pull/506/head
falkTX 2 months ago
parent
commit
d5fac415e4
Signed by: falkTX <falktx@falktx.com> GPG Key ID: CDBAA37ABC74FBA0
4 changed files with 87 additions and 35 deletions
  1. +1
    -1
      cmake/DPF-plugin.cmake
  2. +85
    -32
      dgl/src/OpenGL3.cpp
  3. +1
    -1
      dgl/src/WindowPrivateData.hpp
  4. +0
    -1
      examples/Parameters/CMakeLists.txt

+ 1
- 1
cmake/DPF-plugin.cmake View File

@@ -1167,7 +1167,7 @@ function(dpf__add_dgl_gles3 SHARED_RESOURCES USE_FILE_BROWSER USE_WEB_VIEW)


dpf__add_dgl_system_libs() dpf__add_dgl_system_libs()
target_link_libraries(dgl-gles3 PRIVATE dgl-system-libs) target_link_libraries(dgl-gles3 PRIVATE dgl-system-libs)
target_link_options(dgl-gles2
target_link_options(dgl-gles3
INTERFACE INTERFACE
$<$<BOOL:${EMSCRIPTEN}>:-sMIN_WEBGL_VERSION=3> $<$<BOOL:${EMSCRIPTEN}>:-sMIN_WEBGL_VERSION=3>
$<$<BOOL:${EMSCRIPTEN}>:-sMAX_WEBGL_VERSION=3> $<$<BOOL:${EMSCRIPTEN}>:-sMAX_WEBGL_VERSION=3>


+ 85
- 32
dgl/src/OpenGL3.cpp View File

@@ -89,7 +89,7 @@ DGL_EXT(PFNGLVERTEXATTRIBPOINTERPROC, glVertexAttribPointer)


struct OpenGL3GraphicsContext : GraphicsContext struct OpenGL3GraphicsContext : GraphicsContext
{ {
mutable int prog, color, pos, tex, texok;
mutable int prog, obuf, vbuf, color, pos, tex, texok;
mutable uint w, h; mutable uint w, h;
}; };


@@ -135,13 +135,20 @@ void Line<T>::draw(const GraphicsContext& context, const T width)
glLineWidth(static_cast<GLfloat>(width)); glLineWidth(static_cast<GLfloat>(width));


const GLfloat vertices[] = { x1, y1, x2, y2, }; const GLfloat vertices[] = { x1, y1, x2, y2, };
glVertexAttribPointer(gl3context.pos, 2, GL_FLOAT, GL_FALSE, 0, vertices);
glBindBuffer(GL_ARRAY_BUFFER, gl3context.vbuf);
glBufferData(GL_ARRAY_BUFFER, sizeof(vertices), vertices, GL_STREAM_DRAW);
glEnableVertexAttribArray(gl3context.pos); glEnableVertexAttribArray(gl3context.pos);
glVertexAttribPointer(gl3context.pos, 2, GL_FLOAT, GL_FALSE, 0, nullptr);


const GLubyte order[] = { 0, 1 }; const GLubyte order[] = { 0, 1 };
glDrawElements(GL_LINES, ARRAY_SIZE(order), GL_UNSIGNED_BYTE, order);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, gl3context.obuf);
glBufferData(GL_ELEMENT_ARRAY_BUFFER, sizeof(order), order, GL_STATIC_DRAW);


glDrawElements(GL_LINES, ARRAY_SIZE(order), GL_UNSIGNED_BYTE, nullptr);

glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0);
glDisableVertexAttribArray(gl3context.pos); glDisableVertexAttribArray(gl3context.pos);
glBindBuffer(GL_ARRAY_BUFFER, 0);
} }


#ifdef DGL_ALLOW_DEPRECATED_METHODS #ifdef DGL_ALLOW_DEPRECATED_METHODS
@@ -196,8 +203,12 @@ static void drawCircle(const GraphicsContext& context,
x = cos * x - sin * y; x = cos * x - sin * y;
y = sin * t + cos * y; y = sin * t + cos * y;
} }
glVertexAttribPointer(gl3context.pos, 2, GL_FLOAT, GL_FALSE, 0, vertices);

glBindBuffer(GL_ARRAY_BUFFER, gl3context.vbuf);
glBufferData(GL_ARRAY_BUFFER, sizeof(GLfloat) * (numSegments + 1), vertices, GL_STREAM_DRAW);
glEnableVertexAttribArray(gl3context.pos); glEnableVertexAttribArray(gl3context.pos);
glVertexAttribPointer(gl3context.pos, 2, GL_FLOAT, GL_FALSE, 0, nullptr);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, gl3context.obuf);


if (outline) if (outline)
{ {
@@ -208,7 +219,9 @@ static void drawCircle(const GraphicsContext& context,
order[i * 2 + 1] = i + 1; order[i * 2 + 1] = i + 1;
} }
order[numSegments * 2 - 1] = 0; order[numSegments * 2 - 1] = 0;
glDrawElements(GL_LINES, numSegments * 2, GL_UNSIGNED_SHORT, order);

glBufferData(GL_ELEMENT_ARRAY_BUFFER, sizeof(GLushort) * numSegments * 2, order, GL_DYNAMIC_DRAW);
glDrawElements(GL_LINES, numSegments * 2, GL_UNSIGNED_SHORT, nullptr);
} }
else else
{ {
@@ -225,10 +238,13 @@ static void drawCircle(const GraphicsContext& context,
} }
order[numSegments * 3 - 2] = 0; order[numSegments * 3 - 2] = 0;


glDrawElements(GL_TRIANGLES, numSegments * 3, GL_UNSIGNED_SHORT, order);
glBufferData(GL_ELEMENT_ARRAY_BUFFER, sizeof(GLushort) * numSegments * 3, order, GL_DYNAMIC_DRAW);
glDrawElements(GL_TRIANGLES, numSegments * 3, GL_UNSIGNED_SHORT, nullptr);
} }


glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0);
glDisableVertexAttribArray(gl3context.pos); glDisableVertexAttribArray(gl3context.pos);
glBindBuffer(GL_ARRAY_BUFFER, 0);
} }


template<typename T> template<typename T>
@@ -292,13 +308,19 @@ static void drawTriangle(const GraphicsContext& context,
const GLfloat y3 = (static_cast<double>(pos3.getY()) / gl3context.h) * -2 + 1; const GLfloat y3 = (static_cast<double>(pos3.getY()) / gl3context.h) * -2 + 1;


const GLfloat vertices[] = { x1, y1, x2, y2, x3, y3 }; const GLfloat vertices[] = { x1, y1, x2, y2, x3, y3 };
glVertexAttribPointer(gl3context.pos, 2, GL_FLOAT, GL_FALSE, 0, vertices);

glBindBuffer(GL_ARRAY_BUFFER, gl3context.vbuf);
glBufferData(GL_ARRAY_BUFFER, sizeof(vertices), vertices, GL_STREAM_DRAW);
glEnableVertexAttribArray(gl3context.pos); glEnableVertexAttribArray(gl3context.pos);
glVertexAttribPointer(gl3context.pos, 2, GL_FLOAT, GL_FALSE, 0, nullptr);


if (outline) if (outline)
{ {
const GLubyte order[] = { 0, 1, 1, 2, 2, 0 };
glDrawElements(GL_LINES, ARRAY_SIZE(order), GL_UNSIGNED_BYTE, order);
static constexpr const GLubyte order[] = { 0, 1, 1, 2, 2, 0 };
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, gl3context.obuf);
glBufferData(GL_ELEMENT_ARRAY_BUFFER, sizeof(order), order, GL_STATIC_DRAW);
glDrawElements(GL_LINES, ARRAY_SIZE(order), GL_UNSIGNED_BYTE, nullptr);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0);
} }
else else
{ {
@@ -306,6 +328,7 @@ static void drawTriangle(const GraphicsContext& context,
} }


glDisableVertexAttribArray(gl3context.pos); glDisableVertexAttribArray(gl3context.pos);
glBindBuffer(GL_ARRAY_BUFFER, 0);
} }


template<typename T> template<typename T>
@@ -363,21 +386,29 @@ static void drawRectangle(const GraphicsContext& context, const Rectangle<T>& re
const GLfloat h = (static_cast<double>(rect.getHeight()) / gl3context.h) * -2; const GLfloat h = (static_cast<double>(rect.getHeight()) / gl3context.h) * -2;


const GLfloat vertices[] = { x, y, x, y + h, x + w, y + h, x + w, y }; const GLfloat vertices[] = { x, y, x, y + h, x + w, y + h, x + w, y };
glVertexAttribPointer(gl3context.pos, 2, GL_FLOAT, GL_FALSE, 0, vertices);

glBindBuffer(GL_ARRAY_BUFFER, gl3context.vbuf);
glBufferData(GL_ARRAY_BUFFER, sizeof(vertices), vertices, GL_STREAM_DRAW);
glEnableVertexAttribArray(gl3context.pos); glEnableVertexAttribArray(gl3context.pos);
glVertexAttribPointer(gl3context.pos, 2, GL_FLOAT, GL_FALSE, 0, nullptr);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, gl3context.obuf);


if (outline) if (outline)
{ {
const GLubyte order[] = { 0, 1, 1, 2, 2, 3, 3, 0 };
glDrawElements(GL_LINES, ARRAY_SIZE(order), GL_UNSIGNED_BYTE, order);
static constexpr const GLubyte order[] = { 0, 1, 1, 2, 2, 3, 3, 0 };
glBufferData(GL_ELEMENT_ARRAY_BUFFER, sizeof(order), order, GL_STATIC_DRAW);
glDrawElements(GL_LINES, ARRAY_SIZE(order), GL_UNSIGNED_BYTE, nullptr);
} }
else else
{ {
const GLubyte order[] = { 0, 1, 2, 0, 2, 3 };
glDrawElements(GL_TRIANGLES, ARRAY_SIZE(order), GL_UNSIGNED_BYTE, order);
static constexpr const GLubyte order[] = { 0, 1, 2, 0, 2, 3 };
glBufferData(GL_ELEMENT_ARRAY_BUFFER, sizeof(order), order, GL_STATIC_DRAW);
glDrawElements(GL_TRIANGLES, ARRAY_SIZE(order), GL_UNSIGNED_BYTE, nullptr);
} }


glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0);
glDisableVertexAttribArray(gl3context.pos); glDisableVertexAttribArray(gl3context.pos);
glBindBuffer(GL_ARRAY_BUFFER, 0);
} }


template<typename T> template<typename T>
@@ -495,9 +526,6 @@ void OpenGLImage::drawAt(const GraphicsContext& context, const Point<int>& pos)
setupCalled = true; setupCalled = true;
} }


const GLfloat color[4] = { 1.0f, 1.0f, 1.0f, 1.0f };
glUniform4fv(gl3context.color, 1, color);

const GLfloat x = (static_cast<double>(pos.getX()) / gl3context.w) * 2 - 1; const GLfloat x = (static_cast<double>(pos.getX()) / gl3context.w) * 2 - 1;
const GLfloat y = (static_cast<double>(pos.getY()) / gl3context.h) * -2 + 1; const GLfloat y = (static_cast<double>(pos.getY()) / gl3context.h) * -2 + 1;
const GLfloat w = (static_cast<double>(getWidth()) / gl3context.w) * 2; const GLfloat w = (static_cast<double>(getWidth()) / gl3context.w) * 2;
@@ -507,20 +535,28 @@ void OpenGLImage::drawAt(const GraphicsContext& context, const Point<int>& pos)
glBindTexture(GL_TEXTURE_2D, textureId); glBindTexture(GL_TEXTURE_2D, textureId);
glUniform1i(gl3context.texok, 1); glUniform1i(gl3context.texok, 1);


const GLfloat vertices[] = { x, y, x, y + h, x + w, y + h, x + w, y };
glVertexAttribPointer(gl3context.pos, 2, GL_FLOAT, GL_FALSE, 0, vertices);
const GLfloat vertices[] = {
x, y, x, y + h, x + w, y + h, x + w, y,
0.f, 0.f, 0.f, 1.f, 1.f, 1.f, 1.f, 0.f,
};
glBindBuffer(GL_ARRAY_BUFFER, gl3context.vbuf);
glBufferData(GL_ARRAY_BUFFER, sizeof(vertices), vertices, GL_STREAM_DRAW);
glEnableVertexAttribArray(gl3context.pos); glEnableVertexAttribArray(gl3context.pos);

const GLfloat vtex[] = { 0.f, 0.f, 0.f, 1.f, 1.f, 1.f, 1.f, 0.f };
glVertexAttribPointer(gl3context.tex, 2, GL_FLOAT, GL_FALSE, 0, vtex);
glEnableVertexAttribArray(gl3context.tex); glEnableVertexAttribArray(gl3context.tex);
glVertexAttribPointer(gl3context.pos, 2, GL_FLOAT, GL_FALSE, 0, nullptr);
glVertexAttribPointer(gl3context.tex, 2, GL_FLOAT, GL_FALSE, 0, reinterpret_cast<void*>(sizeof(GLfloat) * 8));


const GLubyte order[] = { 0, 1, 2, 0, 2, 3 };
glDrawElements(GL_TRIANGLES, ARRAY_SIZE(order), GL_UNSIGNED_BYTE, order);
static constexpr const GLubyte order[] = { 0, 1, 2, 0, 2, 3 };
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, gl3context.obuf);
glBufferData(GL_ELEMENT_ARRAY_BUFFER, sizeof(order), order, GL_STATIC_DRAW);


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

glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0);
glDisableVertexAttribArray(gl3context.tex); glDisableVertexAttribArray(gl3context.tex);
glDisableVertexAttribArray(gl3context.pos); glDisableVertexAttribArray(gl3context.pos);
glUniform1i(gl3context.texok, 0); glUniform1i(gl3context.texok, 0);
glBindBuffer(GL_ARRAY_BUFFER, 0);
glBindTexture(GL_TEXTURE_2D, 0); glBindTexture(GL_TEXTURE_2D, 0);
} }


@@ -687,20 +723,28 @@ void ImageBaseKnob<OpenGLImage>::onDisplay()
h = (static_cast<double>(getHeight()) / gl3context.h) * -2; h = (static_cast<double>(getHeight()) / gl3context.h) * -2;
} }


const GLfloat vertices[] = { x, y, x, y + h, x + w, y + h, x + w, y };
glVertexAttribPointer(gl3context.pos, 2, GL_FLOAT, GL_FALSE, 0, vertices);
const GLfloat vertices[] = {
x, y, x, y + h, x + w, y + h, x + w, y,
0.f, 0.f, 0.f, 1.f, 1.f, 1.f, 1.f, 0.f,
};
glBindBuffer(GL_ARRAY_BUFFER, gl3context.vbuf);
glBufferData(GL_ARRAY_BUFFER, sizeof(vertices), vertices, GL_STREAM_DRAW);
glEnableVertexAttribArray(gl3context.pos); glEnableVertexAttribArray(gl3context.pos);

const GLfloat vtex[] = { 0.f, 0.f, 0.f, 1.f, 1.f, 1.f, 1.f, 0.f };
glVertexAttribPointer(gl3context.tex, 2, GL_FLOAT, GL_FALSE, 0, vtex);
glEnableVertexAttribArray(gl3context.tex); glEnableVertexAttribArray(gl3context.tex);
glVertexAttribPointer(gl3context.pos, 2, GL_FLOAT, GL_FALSE, 0, nullptr);
glVertexAttribPointer(gl3context.tex, 2, GL_FLOAT, GL_FALSE, 0, reinterpret_cast<void*>(sizeof(GLfloat) * 8));

static constexpr const GLubyte order[] = { 0, 1, 2, 0, 2, 3 };
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, gl3context.obuf);
glBufferData(GL_ELEMENT_ARRAY_BUFFER, sizeof(order), order, GL_STATIC_DRAW);


const GLubyte order[] = { 0, 1, 2, 0, 2, 3 };
glDrawElements(GL_TRIANGLES, ARRAY_SIZE(order), GL_UNSIGNED_BYTE, order);
glDrawElements(GL_TRIANGLES, ARRAY_SIZE(order), GL_UNSIGNED_BYTE, nullptr);


glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0);
glDisableVertexAttribArray(gl3context.tex); glDisableVertexAttribArray(gl3context.tex);
glDisableVertexAttribArray(gl3context.pos); glDisableVertexAttribArray(gl3context.pos);
glUniform1i(gl3context.texok, 0); glUniform1i(gl3context.texok, 0);
glBindBuffer(GL_ARRAY_BUFFER, 0);
glBindTexture(GL_TEXTURE_2D, 0); glBindTexture(GL_TEXTURE_2D, 0);
} }


@@ -787,6 +831,13 @@ DGL_EXT(PFNGLVERTEXATTRIBPOINTERPROC, glVertexAttribPointer)
if (gl3context.prog == 0) if (gl3context.prog == 0)
{ {
int status; int status;
GLuint obuffer, vbuffer;

glGenBuffers(1, &obuffer);
DISTRHO_SAFE_ASSERT_RETURN(obuffer != 0, contextCreationFail(gl3context));

glGenBuffers(1, &vbuffer);
DISTRHO_SAFE_ASSERT_RETURN(vbuffer != 0, contextCreationFail(gl3context));


const GLuint fragment = glCreateShader(GL_FRAGMENT_SHADER); const GLuint fragment = glCreateShader(GL_FRAGMENT_SHADER);
DISTRHO_SAFE_ASSERT_RETURN(fragment != 0, contextCreationFail(gl3context)); DISTRHO_SAFE_ASSERT_RETURN(fragment != 0, contextCreationFail(gl3context));
@@ -814,7 +865,7 @@ DGL_EXT(PFNGLVERTEXATTRIBPOINTERPROC, glVertexAttribPointer)
#ifdef DGL_USE_GLES3 #ifdef DGL_USE_GLES3
"in vec2 vtex;" "in vec2 vtex;"
"out vec4 FragColor;" "out vec4 FragColor;"
"void main() { FragColor = texok ? texture2D(stex, vtex) : color; }";
"void main() { FragColor = texok ? texture(stex, vtex) : color; }";
#else #else
"varying vec2 vtex;" "varying vec2 vtex;"
"void main() { gl_FragColor = texok ? texture2D(stex, vtex) : color; }"; "void main() { gl_FragColor = texok ? texture2D(stex, vtex) : color; }";
@@ -855,6 +906,8 @@ DGL_EXT(PFNGLVERTEXATTRIBPOINTERPROC, glVertexAttribPointer)
DISTRHO_SAFE_ASSERT_RETURN(status != 0, contextCreationFail(gl3context)); DISTRHO_SAFE_ASSERT_RETURN(status != 0, contextCreationFail(gl3context));


gl3context.prog = program; gl3context.prog = program;
gl3context.obuf = obuffer;
gl3context.vbuf = vbuffer;
gl3context.color = glGetUniformLocation(program, "color"); gl3context.color = glGetUniformLocation(program, "color");
gl3context.texok = glGetUniformLocation(program, "texok"); gl3context.texok = glGetUniformLocation(program, "texok");
gl3context.pos = glGetAttribLocation(program, "pos"); gl3context.pos = glGetAttribLocation(program, "pos");


+ 1
- 1
dgl/src/WindowPrivateData.hpp View File

@@ -45,7 +45,7 @@ struct Window::PrivateData : IdleCallback {
PuglView* view; PuglView* view;


/** Reserved space for graphics context. */ /** Reserved space for graphics context. */
mutable uint8_t graphicsContext[sizeof(int) * 7];
mutable uint8_t graphicsContext[sizeof(int) * 9];


/** The top-level widgets associated with this Window. */ /** The top-level widgets associated with this Window. */
std::list<TopLevelWidget*> topLevelWidgets; std::list<TopLevelWidget*> topLevelWidgets;


+ 0
- 1
examples/Parameters/CMakeLists.txt View File

@@ -3,7 +3,6 @@


dpf_add_plugin(d_parameters dpf_add_plugin(d_parameters
TARGETS jack ladspa dssi lv2 vst2 vst3 clap TARGETS jack ladspa dssi lv2 vst2 vst3 clap
UI_TYPE gles2
FILES_DSP FILES_DSP
ExamplePluginParameters.cpp ExamplePluginParameters.cpp
FILES_UI FILES_UI


Loading…
Cancel
Save