Skip to content

Commit ca222d3

Browse files
committed
Backends: OpenGL: Partially revert 1.86 change of using glBufferSubData(): now only done on Intel GPUs. (#4468, #3381, #2981, #4825, #4832, #5127)
Essentially reverts 389982e for non-Intel GPUs + update imgui_impl_opengl3_loader.h Amended once (force-pushed).
1 parent 7bf07d2 commit ca222d3

File tree

3 files changed

+40
-10
lines changed

3 files changed

+40
-10
lines changed

backends/imgui_impl_opengl3.cpp

+31-10
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414

1515
// CHANGELOG
1616
// (minor and older changes stripped away, please see git history for details)
17+
// 2022-05-23: OpenGL: Reworking 2021-12-15 "Using buffer orphaning" so it only happens on Intel GPU, seems to cause problems otherwise. (#4468, #4825, #4832, #5127).
1718
// 2022-05-13: OpenGL: Fix state corruption on OpenGL ES 2.0 due to not preserving GL_ELEMENT_ARRAY_BUFFER_BINDING and vertex attribute states.
1819
// 2021-12-15: OpenGL: Using buffer orphaning + glBufferSubData(), seems to fix leaks with multi-viewports with some Intel HD drivers.
1920
// 2021-08-23: OpenGL: Fixed ES 3.0 shader ("#version 300 es") use normal precision floats to avoid wobbly rendering at HD resolutions.
@@ -193,6 +194,7 @@ struct ImGui_ImplOpenGL3_Data
193194
GLsizeiptr VertexBufferSize;
194195
GLsizeiptr IndexBufferSize;
195196
bool HasClipOrigin;
197+
bool UseBufferSubData;
196198

197199
ImGui_ImplOpenGL3_Data() { memset((void*)this, 0, sizeof(*this)); }
198200
};
@@ -261,6 +263,14 @@ bool ImGui_ImplOpenGL3_Init(const char* glsl_version)
261263
sscanf(gl_version, "%d.%d", &major, &minor);
262264
}
263265
bd->GlVersion = (GLuint)(major * 100 + minor * 10);
266+
267+
// Query vendor to enable glBufferSubData kludge
268+
#ifdef _WIN32
269+
if (const char* vendor = (const char*)glGetString(GL_VENDOR))
270+
if (strncmp(vendor, "Intel", 5) == 0)
271+
bd->UseBufferSubData = true;
272+
#endif
273+
//printf("GL_MAJOR_VERSION = %d\nGL_MINOR_VERSION = %d\nGL_VENDOR = '%s'\nGL_RENDERER = '%s'\n", major, minor, (const char*)glGetString(GL_VENDOR), (const char*)glGetString(GL_RENDERER)); // [DEBUG]
264274
#else
265275
bd->GlVersion = 200; // GLES 2
266276
#endif
@@ -474,20 +484,31 @@ void ImGui_ImplOpenGL3_RenderDrawData(ImDrawData* draw_data)
474484
const ImDrawList* cmd_list = draw_data->CmdLists[n];
475485

476486
// Upload vertex/index buffers
477-
GLsizeiptr vtx_buffer_size = (GLsizeiptr)cmd_list->VtxBuffer.Size * (int)sizeof(ImDrawVert);
478-
GLsizeiptr idx_buffer_size = (GLsizeiptr)cmd_list->IdxBuffer.Size * (int)sizeof(ImDrawIdx);
479-
if (bd->VertexBufferSize < vtx_buffer_size)
487+
// - On Intel windows drivers we got reports that regular glBufferData() led to accumulating leaks when using multi-viewports, so we started using orphaning + glBufferSubData(). (See https://github.com/ocornut/imgui/issues/4468)
488+
// - On NVIDIA drivers we got reports that using orphaning + glBufferSubData() led to glitches when using multi-viewports.
489+
// - OpenGL drivers are in a very sorry state in 2022, for now we are switching code path based on vendors.
490+
const GLsizeiptr vtx_buffer_size = (GLsizeiptr)cmd_list->VtxBuffer.Size * (int)sizeof(ImDrawVert);
491+
const GLsizeiptr idx_buffer_size = (GLsizeiptr)cmd_list->IdxBuffer.Size * (int)sizeof(ImDrawIdx);
492+
if (bd->UseBufferSubData)
480493
{
481-
bd->VertexBufferSize = vtx_buffer_size;
482-
glBufferData(GL_ARRAY_BUFFER, bd->VertexBufferSize, NULL, GL_STREAM_DRAW);
494+
if (bd->VertexBufferSize < vtx_buffer_size)
495+
{
496+
bd->VertexBufferSize = vtx_buffer_size;
497+
glBufferData(GL_ARRAY_BUFFER, bd->VertexBufferSize, NULL, GL_STREAM_DRAW);
498+
}
499+
if (bd->IndexBufferSize < idx_buffer_size)
500+
{
501+
bd->IndexBufferSize = idx_buffer_size;
502+
glBufferData(GL_ELEMENT_ARRAY_BUFFER, bd->IndexBufferSize, NULL, GL_STREAM_DRAW);
503+
}
504+
glBufferSubData(GL_ARRAY_BUFFER, 0, vtx_buffer_size, (const GLvoid*)cmd_list->VtxBuffer.Data);
505+
glBufferSubData(GL_ELEMENT_ARRAY_BUFFER, 0, idx_buffer_size, (const GLvoid*)cmd_list->IdxBuffer.Data);
483506
}
484-
if (bd->IndexBufferSize < idx_buffer_size)
507+
else
485508
{
486-
bd->IndexBufferSize = idx_buffer_size;
487-
glBufferData(GL_ELEMENT_ARRAY_BUFFER, bd->IndexBufferSize, NULL, GL_STREAM_DRAW);
509+
glBufferData(GL_ARRAY_BUFFER, vtx_buffer_size, (const GLvoid*)cmd_list->VtxBuffer.Data, GL_STREAM_DRAW);
510+
glBufferData(GL_ELEMENT_ARRAY_BUFFER, idx_buffer_size, (const GLvoid*)cmd_list->IdxBuffer.Data, GL_STREAM_DRAW);
488511
}
489-
glBufferSubData(GL_ARRAY_BUFFER, 0, vtx_buffer_size, (const GLvoid*)cmd_list->VtxBuffer.Data);
490-
glBufferSubData(GL_ELEMENT_ARRAY_BUFFER, 0, idx_buffer_size, (const GLvoid*)cmd_list->IdxBuffer.Data);
491512

492513
for (int cmd_i = 0; cmd_i < cmd_list->CmdBuffer.Size; cmd_i++)
493514
{

backends/imgui_impl_opengl3_loader.h

+2
Original file line numberDiff line numberDiff line change
@@ -164,6 +164,8 @@ typedef khronos_uint8_t GLubyte;
164164
#define GL_FLOAT 0x1406
165165
#define GL_RGBA 0x1908
166166
#define GL_FILL 0x1B02
167+
#define GL_VENDOR 0x1F00
168+
#define GL_RENDERER 0x1F01
167169
#define GL_VERSION 0x1F02
168170
#define GL_EXTENSIONS 0x1F03
169171
#define GL_LINEAR 0x2601

docs/CHANGELOG.txt

+7
Original file line numberDiff line numberDiff line change
@@ -121,6 +121,13 @@ Other Changes:
121121
- Backends: OSX: Monitor NSKeyUp events to catch missing keyUp for key when user press Cmd + key (#5128) [@thedmd]
122122
- Backends: OSX, Metal: Store backend data in a per-context struct, allowing to use these backends with
123123
multiple contexts. (#5203, #5221, #4141) [@noisewuwei]
124+
- Backends: OpenGL3: Partially revert 1.86 change of using glBufferSubData(): now only done on Windows and
125+
Intel GPU, based on querying glGetString(GL_VENDOR). Essentially we got report of accumulating leaks on Intel
126+
with multi-viewports when using simple glBufferData() without orphaning, and report of corruptions on other
127+
GPUs with multi-viewports when using orphaning and glBufferSubData(), so currently switching technique based
128+
on GPU vendor, which unfortunately reinforce the cargo-cult nature of dealing with OpenGL drivers.
129+
Navigating the space of mysterious OpenGL drivers is particularly difficult as they are known to rely on
130+
application specific whitelisting. (#4468, #3381, #2981, #4825, #4832, #5127).
124131
- Backends: OpenGL3: Fix state corruption on OpenGL ES 2.0 due to not preserving GL_ELEMENT_ARRAY_BUFFER_BINDING
125132
and vertex attribute states. [@rokups]
126133
- Examples: Emscripten+WebGPU: Fix building for latest WebGPU specs. (#3632)

0 commit comments

Comments
 (0)