Skip to content

Commit c3ec8ee

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
1 parent 7bf07d2 commit c3ec8ee

File tree

3 files changed

+38
-10
lines changed

3 files changed

+38
-10
lines changed

backends/imgui_impl_opengl3.cpp

+29-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,12 @@ 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+
if (const char* vendor = (const char*)glGetString(GL_VENDOR))
269+
if (strncmp(vendor, "Intel", 5) == 0)
270+
bd->UseBufferSubData = true;
271+
//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]
264272
#else
265273
bd->GlVersion = 200; // GLES 2
266274
#endif
@@ -474,20 +482,31 @@ void ImGui_ImplOpenGL3_RenderDrawData(ImDrawData* draw_data)
474482
const ImDrawList* cmd_list = draw_data->CmdLists[n];
475483

476484
// 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)
485+
// - 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)
486+
// - On NVIDIA drivers we got reports that using orphaning + glBufferSubData() led to glitches when using multi-viewports.
487+
// - OpenGL drivers are in a very sorry state in 2022, for now we are switching code path based on vendors.
488+
const GLsizeiptr vtx_buffer_size = (GLsizeiptr)cmd_list->VtxBuffer.Size * (int)sizeof(ImDrawVert);
489+
const GLsizeiptr idx_buffer_size = (GLsizeiptr)cmd_list->IdxBuffer.Size * (int)sizeof(ImDrawIdx);
490+
if (bd->UseBufferSubData)
480491
{
481-
bd->VertexBufferSize = vtx_buffer_size;
482-
glBufferData(GL_ARRAY_BUFFER, bd->VertexBufferSize, NULL, GL_STREAM_DRAW);
492+
if (bd->VertexBufferSize < vtx_buffer_size)
493+
{
494+
bd->VertexBufferSize = vtx_buffer_size;
495+
glBufferData(GL_ARRAY_BUFFER, bd->VertexBufferSize, NULL, GL_STREAM_DRAW);
496+
}
497+
if (bd->IndexBufferSize < idx_buffer_size)
498+
{
499+
bd->IndexBufferSize = idx_buffer_size;
500+
glBufferData(GL_ELEMENT_ARRAY_BUFFER, bd->IndexBufferSize, NULL, GL_STREAM_DRAW);
501+
}
502+
glBufferSubData(GL_ARRAY_BUFFER, 0, vtx_buffer_size, (const GLvoid*)cmd_list->VtxBuffer.Data);
503+
glBufferSubData(GL_ELEMENT_ARRAY_BUFFER, 0, idx_buffer_size, (const GLvoid*)cmd_list->IdxBuffer.Data);
483504
}
484-
if (bd->IndexBufferSize < idx_buffer_size)
505+
else
485506
{
486-
bd->IndexBufferSize = idx_buffer_size;
487-
glBufferData(GL_ELEMENT_ARRAY_BUFFER, bd->IndexBufferSize, NULL, GL_STREAM_DRAW);
507+
glBufferData(GL_ARRAY_BUFFER, vtx_buffer_size, (const GLvoid*)cmd_list->VtxBuffer.Data, GL_STREAM_DRAW);
508+
glBufferData(GL_ELEMENT_ARRAY_BUFFER, idx_buffer_size, (const GLvoid*)cmd_list->IdxBuffer.Data, GL_STREAM_DRAW);
488509
}
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);
491510

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

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 Intel GPU
125+
based on querying glGetString(GL_VENDOR). Essentially we got report of accumulating leaks on Intel with
126+
multi-viewports when using simple glBufferData() without orphaning, and report of corruptions on other GPUs
127+
with multi-viewports when using orphaning and glBufferSubData(), so currently switching technique based on
128+
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)