1// dear imgui: Renderer Backend for modern OpenGL with shaders / programmatic pipeline
2// - Desktop GL: 2.x 3.x 4.x
3// - Embedded GL: ES 2.0 (WebGL 1.0), ES 3.0 (WebGL 2.0)
4// This needs to be used along with a Platform Backend (e.g. GLFW, SDL, Win32, custom..)
5
6// Implemented features:
7// [X] Renderer: User texture binding. Use 'GLuint' OpenGL texture as texture identifier. Read the FAQ about ImTextureID/ImTextureRef!
8// [x] Renderer: Large meshes support (64k+ vertices) even with 16-bit indices (ImGuiBackendFlags_RendererHasVtxOffset) [Desktop OpenGL only!]
9// [X] Renderer: Texture updates support for dynamic font atlas (ImGuiBackendFlags_RendererHasTextures).
10// [X] Renderer: Multi-viewport support (multiple windows). Enable with 'io.ConfigFlags |= ImGuiConfigFlags_ViewportsEnable'.
11
12// About WebGL/ES:
13// - You need to '#define IMGUI_IMPL_OPENGL_ES2' or '#define IMGUI_IMPL_OPENGL_ES3' to use WebGL or OpenGL ES.
14// - This is done automatically on iOS, Android and Emscripten targets.
15// - For other targets, the define needs to be visible from the imgui_impl_opengl3.cpp compilation unit. If unsure, define globally or in imconfig.h.
16
17// You can use unmodified imgui_impl_* files in your project. See examples/ folder for examples of using this.
18// Prefer including the entire imgui/ repository into your project (either as a copy or as a submodule), and only build the backends you need.
19// Learn about Dear ImGui:
20// - FAQ https://dearimgui.com/faq
21// - Getting Started https://dearimgui.com/getting-started
22// - Documentation https://dearimgui.com/docs (same as your local docs/ folder).
23// - Introduction, links and more at the top of imgui.cpp
24
25// CHANGELOG
26// (minor and older changes stripped away, please see git history for details)
27// 2025-XX-XX: Platform: Added support for multiple windows via the ImGuiPlatformIO interface.
28// 2025-07-22: OpenGL: Add and call embedded loader shutdown during ImGui_ImplOpenGL3_Shutdown() to facilitate multiple init/shutdown cycles in same process. (#8792)
29// 2025-07-15: OpenGL: Set GL_UNPACK_ALIGNMENT to 1 before updating textures (#8802) + restore non-WebGL/ES update path that doesn't require a CPU-side copy.
30// 2025-06-11: OpenGL: Added support for ImGuiBackendFlags_RendererHasTextures, for dynamic font atlas. Removed ImGui_ImplOpenGL3_CreateFontsTexture() and ImGui_ImplOpenGL3_DestroyFontsTexture().
31// 2025-06-04: OpenGL: Made GLES 3.20 contexts not access GL_CONTEXT_PROFILE_MASK nor GL_PRIMITIVE_RESTART. (#8664)
32// 2025-02-18: OpenGL: Lazily reinitialize embedded GL loader for when calling backend from e.g. other DLL boundaries. (#8406)
33// 2024-10-07: OpenGL: Changed default texture sampler to Clamp instead of Repeat/Wrap.
34// 2024-06-28: OpenGL: ImGui_ImplOpenGL3_NewFrame() recreates font texture if it has been destroyed by ImGui_ImplOpenGL3_DestroyFontsTexture(). (#7748)
35// 2024-05-07: OpenGL: Update loader for Linux to support EGL/GLVND. (#7562)
36// 2024-04-16: OpenGL: Detect ES3 contexts on desktop based on version string, to e.g. avoid calling glPolygonMode() on them. (#7447)
37// 2024-01-09: OpenGL: Update GL3W based imgui_impl_opengl3_loader.h to load "libGL.so" and variants, fixing regression on distros missing a symlink.
38// 2023-11-08: OpenGL: Update GL3W based imgui_impl_opengl3_loader.h to load "libGL.so" instead of "libGL.so.1", accommodating for NetBSD systems having only "libGL.so.3" available. (#6983)
39// 2023-10-05: OpenGL: Rename symbols in our internal loader so that LTO compilation with another copy of gl3w is possible. (#6875, #6668, #4445)
40// 2023-06-20: OpenGL: Fixed erroneous use glGetIntegerv(GL_CONTEXT_PROFILE_MASK) on contexts lower than 3.2. (#6539, #6333)
41// 2023-05-09: OpenGL: Support for glBindSampler() backup/restore on ES3. (#6375)
42// 2023-04-18: OpenGL: Restore front and back polygon mode separately when supported by context. (#6333)
43// 2023-03-23: OpenGL: Properly restoring "no shader program bound" if it was the case prior to running the rendering function. (#6267, #6220, #6224)
44// 2023-03-15: OpenGL: Fixed GL loader crash when GL_VERSION returns NULL. (#6154, #4445, #3530)
45// 2023-03-06: OpenGL: Fixed restoration of a potentially deleted OpenGL program, by calling glIsProgram(). (#6220, #6224)
46// 2022-11-09: OpenGL: Reverted use of glBufferSubData(), too many corruptions issues + old issues seemingly can't be reproed with Intel drivers nowadays (revert 2021-12-15 and 2022-05-23 changes).
47// 2022-10-11: Using 'nullptr' instead of 'NULL' as per our switch to C++11.
48// 2022-09-27: OpenGL: Added ability to '#define IMGUI_IMPL_OPENGL_DEBUG'.
49// 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).
50// 2022-05-13: OpenGL: Fixed state corruption on OpenGL ES 2.0 due to not preserving GL_ELEMENT_ARRAY_BUFFER_BINDING and vertex attribute states.
51// 2021-12-15: OpenGL: Using buffer orphaning + glBufferSubData(), seems to fix leaks with multi-viewports with some Intel HD drivers.
52// 2021-08-23: OpenGL: Fixed ES 3.0 shader ("#version 300 es") use normal precision floats to avoid wobbly rendering at HD resolutions.
53// 2021-08-19: OpenGL: Embed and use our own minimal GL loader (imgui_impl_opengl3_loader.h), removing requirement and support for third-party loader.
54// 2021-06-29: Reorganized backend to pull data from a single structure to facilitate usage with multiple-contexts (all g_XXXX access changed to bd->XXXX).
55// 2021-06-25: OpenGL: Use OES_vertex_array extension on Emscripten + backup/restore current state.
56// 2021-06-21: OpenGL: Destroy individual vertex/fragment shader objects right after they are linked into the main shader.
57// 2021-05-24: OpenGL: Access GL_CLIP_ORIGIN when "GL_ARB_clip_control" extension is detected, inside of just OpenGL 4.5 version.
58// 2021-05-19: OpenGL: Replaced direct access to ImDrawCmd::TextureId with a call to ImDrawCmd::GetTexID(). (will become a requirement)
59// 2021-04-06: OpenGL: Don't try to read GL_CLIP_ORIGIN unless we're OpenGL 4.5 or greater.
60// 2021-02-18: OpenGL: Change blending equation to preserve alpha in output buffer.
61// 2021-01-03: OpenGL: Backup, setup and restore GL_STENCIL_TEST state.
62// 2020-10-23: OpenGL: Backup, setup and restore GL_PRIMITIVE_RESTART state.
63// 2020-10-15: OpenGL: Use glGetString(GL_VERSION) instead of glGetIntegerv(GL_MAJOR_VERSION, ...) when the later returns zero (e.g. Desktop GL 2.x)
64// 2020-09-17: OpenGL: Fix to avoid compiling/calling glBindSampler() on ES or pre-3.3 context which have the defines set by a loader.
65// 2020-07-10: OpenGL: Added support for glad2 OpenGL loader.
66// 2020-05-08: OpenGL: Made default GLSL version 150 (instead of 130) on OSX.
67// 2020-04-21: OpenGL: Fixed handling of glClipControl(GL_UPPER_LEFT) by inverting projection matrix.
68// 2020-04-12: OpenGL: Fixed context version check mistakenly testing for 4.0+ instead of 3.2+ to enable ImGuiBackendFlags_RendererHasVtxOffset.
69// 2020-03-24: OpenGL: Added support for glbinding 2.x OpenGL loader.
70// 2020-01-07: OpenGL: Added support for glbinding 3.x OpenGL loader.
71// 2019-10-25: OpenGL: Using a combination of GL define and runtime GL version to decide whether to use glDrawElementsBaseVertex(). Fix building with pre-3.2 GL loaders.
72// 2019-09-22: OpenGL: Detect default GL loader using __has_include compiler facility.
73// 2019-09-16: OpenGL: Tweak initialization code to allow application calling ImGui_ImplOpenGL3_CreateFontsTexture() before the first NewFrame() call.
74// 2019-05-29: OpenGL: Desktop GL only: Added support for large mesh (64K+ vertices), enable ImGuiBackendFlags_RendererHasVtxOffset flag.
75// 2019-04-30: OpenGL: Added support for special ImDrawCallback_ResetRenderState callback to reset render state.
76// 2019-03-29: OpenGL: Not calling glBindBuffer more than necessary in the render loop.
77// 2019-03-15: OpenGL: Added a GL call + comments in ImGui_ImplOpenGL3_Init() to detect uninitialized GL function loaders early.
78// 2019-03-03: OpenGL: Fix support for ES 2.0 (WebGL 1.0).
79// 2019-02-20: OpenGL: Fix for OSX not supporting OpenGL 4.5, we don't try to read GL_CLIP_ORIGIN even if defined by the headers/loader.
80// 2019-02-11: OpenGL: Projecting clipping rectangles correctly using draw_data->FramebufferScale to allow multi-viewports for retina display.
81// 2019-02-01: OpenGL: Using GLSL 410 shaders for any version over 410 (e.g. 430, 450).
82// 2018-11-30: Misc: Setting up io.BackendRendererName so it can be displayed in the About Window.
83// 2018-11-13: OpenGL: Support for GL 4.5's glClipControl(GL_UPPER_LEFT) / GL_CLIP_ORIGIN.
84// 2018-08-29: OpenGL: Added support for more OpenGL loaders: glew and glad, with comments indicative that any loader can be used.
85// 2018-08-09: OpenGL: Default to OpenGL ES 3 on iOS and Android. GLSL version default to "#version 300 ES".
86// 2018-07-30: OpenGL: Support for GLSL 300 ES and 410 core. Fixes for Emscripten compilation.
87// 2018-07-10: OpenGL: Support for more GLSL versions (based on the GLSL version string). Added error output when shaders fail to compile/link.
88// 2018-06-08: Misc: Extracted imgui_impl_opengl3.cpp/.h away from the old combined GLFW/SDL+OpenGL3 examples.
89// 2018-06-08: OpenGL: Use draw_data->DisplayPos and draw_data->DisplaySize to setup projection matrix and clipping rectangle.
90// 2018-05-25: OpenGL: Removed unnecessary backup/restore of GL_ELEMENT_ARRAY_BUFFER_BINDING since this is part of the VAO state.
91// 2018-05-14: OpenGL: Making the call to glBindSampler() optional so 3.2 context won't fail if the function is a nullptr pointer.
92// 2018-03-06: OpenGL: Added const char* glsl_version parameter to ImGui_ImplOpenGL3_Init() so user can override the GLSL version e.g. "#version 150".
93// 2018-02-23: OpenGL: Create the VAO in the render function so the setup can more easily be used with multiple shared GL context.
94// 2018-02-16: Misc: Obsoleted the io.RenderDrawListsFn callback and exposed ImGui_ImplSdlGL3_RenderDrawData() in the .h file so you can call it yourself.
95// 2018-01-07: OpenGL: Changed GLSL shader version from 330 to 150.
96// 2017-09-01: OpenGL: Save and restore current bound sampler. Save and restore current polygon mode.
97// 2017-05-01: OpenGL: Fixed save and restore of current blend func state.
98// 2017-05-01: OpenGL: Fixed save and restore of current GL_ACTIVE_TEXTURE.
99// 2016-09-05: OpenGL: Fixed save and restore of current scissor rectangle.
100// 2016-07-29: OpenGL: Explicitly setting GL_UNPACK_ROW_LENGTH to reduce issues because SDL changes it. (#752)
101
102//----------------------------------------
103// OpenGL GLSL GLSL
104// version version string
105//----------------------------------------
106// 2.0 110 "#version 110"
107// 2.1 120 "#version 120"
108// 3.0 130 "#version 130"
109// 3.1 140 "#version 140"
110// 3.2 150 "#version 150"
111// 3.3 330 "#version 330 core"
112// 4.0 400 "#version 400 core"
113// 4.1 410 "#version 410 core"
114// 4.2 420 "#version 410 core"
115// 4.3 430 "#version 430 core"
116// ES 2.0 100 "#version 100" = WebGL 1.0
117// ES 3.0 300 "#version 300 es" = WebGL 2.0
118//----------------------------------------
119
120#if defined(_MSC_VER) && !defined(_CRT_SECURE_NO_WARNINGS)
121#define _CRT_SECURE_NO_WARNINGS
122#endif
123
124#include "imgui.h"
125#ifndef IMGUI_DISABLE
126#include "imgui_impl_opengl3.h"
127#include <stdio.h>
128#include <stdint.h> // intptr_t
129#if defined(__APPLE__)
130#include <TargetConditionals.h>
131#endif
132
133// Clang/GCC warnings with -Weverything
134#if defined(__clang__)
135#pragma clang diagnostic push
136#pragma clang diagnostic ignored "-Wunknown-warning-option" // warning: ignore unknown flags
137#pragma clang diagnostic ignored "-Wold-style-cast" // warning: use of old-style cast
138#pragma clang diagnostic ignored "-Wsign-conversion" // warning: implicit conversion changes signedness
139#pragma clang diagnostic ignored "-Wunused-macros" // warning: macro is not used
140#pragma clang diagnostic ignored "-Wnonportable-system-include-path"
141#pragma clang diagnostic ignored "-Wcast-function-type" // warning: cast between incompatible function types (for loader)
142#endif
143#if defined(__GNUC__)
144#pragma GCC diagnostic push
145#pragma GCC diagnostic ignored "-Wpragmas" // warning: unknown option after '#pragma GCC diagnostic' kind
146#pragma GCC diagnostic ignored "-Wunknown-warning-option" // warning: unknown warning group 'xxx'
147#pragma GCC diagnostic ignored "-Wcast-function-type" // warning: cast between incompatible function types (for loader)
148#pragma GCC diagnostic ignored "-Wstrict-overflow" // warning: assuming signed overflow does not occur when simplifying division / ..when changing X +- C1 cmp C2 to X cmp C2 -+ C1
149#endif
150
151// GL includes
152#if defined(IMGUI_IMPL_OPENGL_ES2)
153#if (defined(__APPLE__) && (TARGET_OS_IOS || TARGET_OS_TV))
154#include <OpenGLES/ES2/gl.h> // Use GL ES 2
155#else
156#include <GLES2/gl2.h> // Use GL ES 2
157#endif
158#if defined(__EMSCRIPTEN__)
159#ifndef GL_GLEXT_PROTOTYPES
160#define GL_GLEXT_PROTOTYPES
161#endif
162#include <GLES2/gl2ext.h>
163#endif
164#elif defined(IMGUI_IMPL_OPENGL_ES3)
165#if (defined(__APPLE__) && (TARGET_OS_IOS || TARGET_OS_TV))
166#include <OpenGLES/ES3/gl.h> // Use GL ES 3
167#else
168#include <GLES3/gl3.h> // Use GL ES 3
169#endif
170#elif !defined(IMGUI_IMPL_OPENGL_LOADER_CUSTOM)
171// Modern desktop OpenGL doesn't have a standard portable header file to load OpenGL function pointers.
172// Helper libraries are often used for this purpose! Here we are using our own minimal custom loader based on gl3w.
173// In the rest of your app/engine, you can use another loader of your choice (gl3w, glew, glad, glbinding, glext, glLoadGen, etc.).
174// If you happen to be developing a new feature for this backend (imgui_impl_opengl3.cpp):
175// - You may need to regenerate imgui_impl_opengl3_loader.h to add new symbols. See https://github.com/dearimgui/gl3w_stripped
176// Typically you would run: python3 ./gl3w_gen.py --output ../imgui/backends/imgui_impl_opengl3_loader.h --ref ../imgui/backends/imgui_impl_opengl3.cpp ./extra_symbols.txt
177// - You can temporarily use an unstripped version. See https://github.com/dearimgui/gl3w_stripped/releases
178// Changes to this backend using new APIs should be accompanied by a regenerated stripped loader version.
179#define IMGL3W_IMPL
180#define IMGUI_IMPL_OPENGL_LOADER_IMGL3W
181#include "imgui_impl_opengl3_loader.h"
182#endif
183
184// Vertex arrays are not supported on ES2/WebGL1 unless Emscripten which uses an extension
185#ifndef IMGUI_IMPL_OPENGL_ES2
186#define IMGUI_IMPL_OPENGL_USE_VERTEX_ARRAY
187#elif defined(__EMSCRIPTEN__)
188#define IMGUI_IMPL_OPENGL_USE_VERTEX_ARRAY
189#define glBindVertexArray glBindVertexArrayOES
190#define glGenVertexArrays glGenVertexArraysOES
191#define glDeleteVertexArrays glDeleteVertexArraysOES
192#define GL_VERTEX_ARRAY_BINDING GL_VERTEX_ARRAY_BINDING_OES
193#endif
194
195// Desktop GL 2.0+ has extension and glPolygonMode() which GL ES and WebGL don't have..
196// A desktop ES context can technically compile fine with our loader, so we also perform a runtime checks
197#if !defined(IMGUI_IMPL_OPENGL_ES2) && !defined(IMGUI_IMPL_OPENGL_ES3)
198#define IMGUI_IMPL_OPENGL_HAS_EXTENSIONS // has glGetIntegerv(GL_NUM_EXTENSIONS)
199#define IMGUI_IMPL_OPENGL_MAY_HAVE_POLYGON_MODE // may have glPolygonMode()
200#endif
201
202// Desktop GL 2.1+ and GL ES 3.0+ have glBindBuffer() with GL_PIXEL_UNPACK_BUFFER target.
203#if !defined(IMGUI_IMPL_OPENGL_ES2)
204#define IMGUI_IMPL_OPENGL_MAY_HAVE_BIND_BUFFER_PIXEL_UNPACK
205#endif
206
207// Desktop GL 3.1+ has GL_PRIMITIVE_RESTART state
208#if !defined(IMGUI_IMPL_OPENGL_ES2) && !defined(IMGUI_IMPL_OPENGL_ES3) && defined(GL_VERSION_3_1)
209#define IMGUI_IMPL_OPENGL_MAY_HAVE_PRIMITIVE_RESTART
210#endif
211
212// Desktop GL 3.2+ has glDrawElementsBaseVertex() which GL ES and WebGL don't have.
213#if !defined(IMGUI_IMPL_OPENGL_ES2) && !defined(IMGUI_IMPL_OPENGL_ES3) && defined(GL_VERSION_3_2)
214#define IMGUI_IMPL_OPENGL_MAY_HAVE_VTX_OFFSET
215#endif
216
217// Desktop GL 3.3+ and GL ES 3.0+ have glBindSampler()
218#if !defined(IMGUI_IMPL_OPENGL_ES2) && (defined(IMGUI_IMPL_OPENGL_ES3) || defined(GL_VERSION_3_3))
219#define IMGUI_IMPL_OPENGL_MAY_HAVE_BIND_SAMPLER
220#endif
221
222// [Debugging]
223//#define IMGUI_IMPL_OPENGL_DEBUG
224#ifdef IMGUI_IMPL_OPENGL_DEBUG
225#include <stdio.h>
226#define GL_CALL(_CALL) do { _CALL; GLenum gl_err = glGetError(); if (gl_err != 0) fprintf(stderr, "GL error 0x%x returned from '%s'.\n", gl_err, #_CALL); } while (0) // Call with error check
227#else
228#define GL_CALL(_CALL) _CALL // Call without error check
229#endif
230
231// OpenGL Data
232struct ImGui_ImplOpenGL3_Data
233{
234 GLuint GlVersion; // Extracted at runtime using GL_MAJOR_VERSION, GL_MINOR_VERSION queries (e.g. 320 for GL 3.2)
235 char GlslVersionString[32]; // Specified by user or detected based on compile time GL settings.
236 bool GlProfileIsES2;
237 bool GlProfileIsES3;
238 bool GlProfileIsCompat;
239 GLint GlProfileMask;
240 GLint MaxTextureSize;
241 GLuint ShaderHandle;
242 GLint AttribLocationTex; // Uniforms location
243 GLint AttribLocationProjMtx;
244 GLuint AttribLocationVtxPos; // Vertex attributes location
245 GLuint AttribLocationVtxUV;
246 GLuint AttribLocationVtxColor;
247 unsigned int VboHandle, ElementsHandle;
248 GLsizeiptr VertexBufferSize;
249 GLsizeiptr IndexBufferSize;
250 bool HasPolygonMode;
251 bool HasClipOrigin;
252 bool UseBufferSubData;
253 ImVector<char> TempBuffer;
254
255 ImGui_ImplOpenGL3_Data() { memset(s: (void*)this, c: 0, n: sizeof(*this)); }
256};
257
258// Backend data stored in io.BackendRendererUserData to allow support for multiple Dear ImGui contexts
259// It is STRONGLY preferred that you use docking branch with multi-viewports (== single Dear ImGui context + multiple windows) instead of multiple Dear ImGui contexts.
260static ImGui_ImplOpenGL3_Data* ImGui_ImplOpenGL3_GetBackendData()
261{
262 return ImGui::GetCurrentContext() ? (ImGui_ImplOpenGL3_Data*)ImGui::GetIO().BackendRendererUserData : nullptr;
263}
264
265// Forward Declarations
266static void ImGui_ImplOpenGL3_InitMultiViewportSupport();
267static void ImGui_ImplOpenGL3_ShutdownMultiViewportSupport();
268
269// OpenGL vertex attribute state (for ES 1.0 and ES 2.0 only)
270#ifndef IMGUI_IMPL_OPENGL_USE_VERTEX_ARRAY
271struct ImGui_ImplOpenGL3_VtxAttribState
272{
273 GLint Enabled, Size, Type, Normalized, Stride;
274 GLvoid* Ptr;
275
276 void GetState(GLint index)
277 {
278 glGetVertexAttribiv(index, GL_VERTEX_ATTRIB_ARRAY_ENABLED, &Enabled);
279 glGetVertexAttribiv(index, GL_VERTEX_ATTRIB_ARRAY_SIZE, &Size);
280 glGetVertexAttribiv(index, GL_VERTEX_ATTRIB_ARRAY_TYPE, &Type);
281 glGetVertexAttribiv(index, GL_VERTEX_ATTRIB_ARRAY_NORMALIZED, &Normalized);
282 glGetVertexAttribiv(index, GL_VERTEX_ATTRIB_ARRAY_STRIDE, &Stride);
283 glGetVertexAttribPointerv(index, GL_VERTEX_ATTRIB_ARRAY_POINTER, &Ptr);
284 }
285 void SetState(GLint index)
286 {
287 glVertexAttribPointer(index, Size, Type, (GLboolean)Normalized, Stride, Ptr);
288 if (Enabled) glEnableVertexAttribArray(index); else glDisableVertexAttribArray(index);
289 }
290};
291#endif
292
293// Not static to allow third-party code to use that if they want to (but undocumented)
294bool ImGui_ImplOpenGL3_InitLoader();
295bool ImGui_ImplOpenGL3_InitLoader()
296{
297 // Initialize our loader
298#ifdef IMGUI_IMPL_OPENGL_LOADER_IMGL3W
299 if (glGetIntegerv == nullptr && imgl3wInit() != 0)
300 {
301 fprintf(stderr, format: "Failed to initialize OpenGL loader!\n");
302 return false;
303 }
304#endif
305 return true;
306}
307
308// Functions
309bool ImGui_ImplOpenGL3_Init(const char* glsl_version)
310{
311 ImGuiIO& io = ImGui::GetIO();
312 IMGUI_CHECKVERSION();
313 IM_ASSERT(io.BackendRendererUserData == nullptr && "Already initialized a renderer backend!");
314
315 // Initialize loader
316 if (!ImGui_ImplOpenGL3_InitLoader())
317 return false;
318
319 // Setup backend capabilities flags
320 ImGui_ImplOpenGL3_Data* bd = IM_NEW(ImGui_ImplOpenGL3_Data)();
321 io.BackendRendererUserData = (void*)bd;
322 io.BackendRendererName = "imgui_impl_opengl3";
323
324 // Query for GL version (e.g. 320 for GL 3.2)
325 const char* gl_version_str = (const char*)glGetString(GL_VERSION);
326#if defined(IMGUI_IMPL_OPENGL_ES2)
327 // GLES 2
328 bd->GlVersion = 200;
329 bd->GlProfileIsES2 = true;
330 IM_UNUSED(gl_version_str);
331#else
332 // Desktop or GLES 3
333 GLint major = 0;
334 GLint minor = 0;
335 glGetIntegerv(GL_MAJOR_VERSION, &major);
336 glGetIntegerv(GL_MINOR_VERSION, &minor);
337 if (major == 0 && minor == 0)
338 sscanf(s: gl_version_str, format: "%d.%d", &major, &minor); // Query GL_VERSION in desktop GL 2.x, the string will start with "<major>.<minor>"
339 bd->GlVersion = (GLuint)(major * 100 + minor * 10);
340 glGetIntegerv(GL_MAX_TEXTURE_SIZE, &bd->MaxTextureSize);
341
342#if defined(IMGUI_IMPL_OPENGL_ES3)
343 bd->GlProfileIsES3 = true;
344#else
345 if (strncmp(s1: gl_version_str, s2: "OpenGL ES 3", n: 11) == 0)
346 bd->GlProfileIsES3 = true;
347#endif
348
349#if defined(GL_CONTEXT_PROFILE_MASK)
350 if (!bd->GlProfileIsES3 && bd->GlVersion >= 320)
351 glGetIntegerv(GL_CONTEXT_PROFILE_MASK, &bd->GlProfileMask);
352 bd->GlProfileIsCompat = (bd->GlProfileMask & GL_CONTEXT_COMPATIBILITY_PROFILE_BIT) != 0;
353#endif
354
355 bd->UseBufferSubData = false;
356 /*
357 // Query vendor to enable glBufferSubData kludge
358#ifdef _WIN32
359 if (const char* vendor = (const char*)glGetString(GL_VENDOR))
360 if (strncmp(vendor, "Intel", 5) == 0)
361 bd->UseBufferSubData = true;
362#endif
363 */
364#endif
365
366#ifdef IMGUI_IMPL_OPENGL_DEBUG
367 printf("GlVersion = %d, \"%s\"\nGlProfileIsCompat = %d\nGlProfileMask = 0x%X\nGlProfileIsES2/IsEs3 = %d/%d\nGL_VENDOR = '%s'\nGL_RENDERER = '%s'\n", bd->GlVersion, gl_version_str, bd->GlProfileIsCompat, bd->GlProfileMask, bd->GlProfileIsES2, bd->GlProfileIsES3, (const char*)glGetString(GL_VENDOR), (const char*)glGetString(GL_RENDERER)); // [DEBUG]
368#endif
369
370#ifdef IMGUI_IMPL_OPENGL_MAY_HAVE_VTX_OFFSET
371 if (bd->GlVersion >= 320)
372 io.BackendFlags |= ImGuiBackendFlags_RendererHasVtxOffset; // We can honor the ImDrawCmd::VtxOffset field, allowing for large meshes.
373#endif
374 io.BackendFlags |= ImGuiBackendFlags_RendererHasTextures; // We can honor ImGuiPlatformIO::Textures[] requests during render.
375 io.BackendFlags |= ImGuiBackendFlags_RendererHasViewports; // We can create multi-viewports on the Renderer side (optional)
376
377 ImGuiPlatformIO& platform_io = ImGui::GetPlatformIO();
378 platform_io.Renderer_TextureMaxWidth = platform_io.Renderer_TextureMaxHeight = (int)bd->MaxTextureSize;
379
380 // Store GLSL version string so we can refer to it later in case we recreate shaders.
381 // Note: GLSL version is NOT the same as GL version. Leave this to nullptr if unsure.
382 if (glsl_version == nullptr)
383 {
384#if defined(IMGUI_IMPL_OPENGL_ES2)
385 glsl_version = "#version 100";
386#elif defined(IMGUI_IMPL_OPENGL_ES3)
387 glsl_version = "#version 300 es";
388#elif defined(__APPLE__)
389 glsl_version = "#version 150";
390#else
391 glsl_version = "#version 130";
392#endif
393 }
394 IM_ASSERT((int)strlen(glsl_version) + 2 < IM_ARRAYSIZE(bd->GlslVersionString));
395 strcpy(dest: bd->GlslVersionString, src: glsl_version);
396 strcat(dest: bd->GlslVersionString, src: "\n");
397
398 // Make an arbitrary GL call (we don't actually need the result)
399 // IF YOU GET A CRASH HERE: it probably means the OpenGL function loader didn't do its job. Let us know!
400 GLint current_texture;
401 glGetIntegerv(GL_TEXTURE_BINDING_2D, &current_texture);
402
403 // Detect extensions we support
404#ifdef IMGUI_IMPL_OPENGL_MAY_HAVE_POLYGON_MODE
405 bd->HasPolygonMode = (!bd->GlProfileIsES2 && !bd->GlProfileIsES3);
406#endif
407 bd->HasClipOrigin = (bd->GlVersion >= 450);
408#ifdef IMGUI_IMPL_OPENGL_HAS_EXTENSIONS
409 GLint num_extensions = 0;
410 glGetIntegerv(GL_NUM_EXTENSIONS, &num_extensions);
411 for (GLint i = 0; i < num_extensions; i++)
412 {
413 const char* extension = (const char*)glGetStringi(GL_EXTENSIONS, i);
414 if (extension != nullptr && strcmp(s1: extension, s2: "GL_ARB_clip_control") == 0)
415 bd->HasClipOrigin = true;
416 }
417#endif
418
419 ImGui_ImplOpenGL3_InitMultiViewportSupport();
420
421 return true;
422}
423
424void ImGui_ImplOpenGL3_Shutdown()
425{
426 ImGui_ImplOpenGL3_Data* bd = ImGui_ImplOpenGL3_GetBackendData();
427 IM_ASSERT(bd != nullptr && "No renderer backend to shutdown, or already shutdown?");
428 ImGuiIO& io = ImGui::GetIO();
429
430 ImGui_ImplOpenGL3_ShutdownMultiViewportSupport();
431 ImGui_ImplOpenGL3_DestroyDeviceObjects();
432 io.BackendRendererName = nullptr;
433 io.BackendRendererUserData = nullptr;
434 io.BackendFlags &= ~(ImGuiBackendFlags_RendererHasVtxOffset | ImGuiBackendFlags_RendererHasTextures | ImGuiBackendFlags_RendererHasViewports);
435 IM_DELETE(p: bd);
436
437#ifdef IMGUI_IMPL_OPENGL_LOADER_IMGL3W
438 imgl3wShutdown();
439#endif
440}
441
442void ImGui_ImplOpenGL3_NewFrame()
443{
444 ImGui_ImplOpenGL3_Data* bd = ImGui_ImplOpenGL3_GetBackendData();
445 IM_ASSERT(bd != nullptr && "Context or backend not initialized! Did you call ImGui_ImplOpenGL3_Init()?");
446
447 ImGui_ImplOpenGL3_InitLoader(); // Lazily init loader if not already done for e.g. DLL boundaries.
448
449 if (!bd->ShaderHandle)
450 if (!ImGui_ImplOpenGL3_CreateDeviceObjects())
451 IM_ASSERT(0 && "ImGui_ImplOpenGL3_CreateDeviceObjects() failed!");
452}
453
454static void ImGui_ImplOpenGL3_SetupRenderState(ImDrawData* draw_data, int fb_width, int fb_height, GLuint vertex_array_object)
455{
456 ImGui_ImplOpenGL3_Data* bd = ImGui_ImplOpenGL3_GetBackendData();
457
458 // Setup render state: alpha-blending enabled, no face culling, no depth testing, scissor enabled, polygon fill
459 glEnable(GL_BLEND);
460 glBlendEquation(GL_FUNC_ADD);
461 glBlendFuncSeparate(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA, GL_ONE, GL_ONE_MINUS_SRC_ALPHA);
462 glDisable(GL_CULL_FACE);
463 glDisable(GL_DEPTH_TEST);
464 glDisable(GL_STENCIL_TEST);
465 glEnable(GL_SCISSOR_TEST);
466#ifdef IMGUI_IMPL_OPENGL_MAY_HAVE_PRIMITIVE_RESTART
467 if (!bd->GlProfileIsES3 && bd->GlVersion >= 310)
468 glDisable(GL_PRIMITIVE_RESTART);
469#endif
470#ifdef IMGUI_IMPL_OPENGL_MAY_HAVE_POLYGON_MODE
471 if (bd->HasPolygonMode)
472 glPolygonMode(GL_FRONT_AND_BACK, GL_FILL);
473#endif
474
475 // Support for GL 4.5 rarely used glClipControl(GL_UPPER_LEFT)
476#if defined(GL_CLIP_ORIGIN)
477 bool clip_origin_lower_left = true;
478 if (bd->HasClipOrigin)
479 {
480 GLenum current_clip_origin = 0; glGetIntegerv(GL_CLIP_ORIGIN, (GLint*)&current_clip_origin);
481 if (current_clip_origin == GL_UPPER_LEFT)
482 clip_origin_lower_left = false;
483 }
484#endif
485
486 // Setup viewport, orthographic projection matrix
487 // Our visible imgui space lies from draw_data->DisplayPos (top left) to draw_data->DisplayPos+data_data->DisplaySize (bottom right). DisplayPos is (0,0) for single viewport apps.
488 GL_CALL(glViewport(0, 0, (GLsizei)fb_width, (GLsizei)fb_height));
489 float L = draw_data->DisplayPos.x;
490 float R = draw_data->DisplayPos.x + draw_data->DisplaySize.x;
491 float T = draw_data->DisplayPos.y;
492 float B = draw_data->DisplayPos.y + draw_data->DisplaySize.y;
493#if defined(GL_CLIP_ORIGIN)
494 if (!clip_origin_lower_left) { float tmp = T; T = B; B = tmp; } // Swap top and bottom if origin is upper left
495#endif
496 const float ortho_projection[4][4] =
497 {
498 { 2.0f/(R-L), 0.0f, 0.0f, 0.0f },
499 { 0.0f, 2.0f/(T-B), 0.0f, 0.0f },
500 { 0.0f, 0.0f, -1.0f, 0.0f },
501 { (R+L)/(L-R), (T+B)/(B-T), 0.0f, 1.0f },
502 };
503 glUseProgram(bd->ShaderHandle);
504 glUniform1i(bd->AttribLocationTex, 0);
505 glUniformMatrix4fv(bd->AttribLocationProjMtx, 1, GL_FALSE, &ortho_projection[0][0]);
506
507#ifdef IMGUI_IMPL_OPENGL_MAY_HAVE_BIND_SAMPLER
508 if (bd->GlVersion >= 330 || bd->GlProfileIsES3)
509 glBindSampler(0, 0); // We use combined texture/sampler state. Applications using GL 3.3 and GL ES 3.0 may set that otherwise.
510#endif
511
512 (void)vertex_array_object;
513#ifdef IMGUI_IMPL_OPENGL_USE_VERTEX_ARRAY
514 glBindVertexArray(vertex_array_object);
515#endif
516
517 // Bind vertex/index buffers and setup attributes for ImDrawVert
518 GL_CALL(glBindBuffer(GL_ARRAY_BUFFER, bd->VboHandle));
519 GL_CALL(glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, bd->ElementsHandle));
520 GL_CALL(glEnableVertexAttribArray(bd->AttribLocationVtxPos));
521 GL_CALL(glEnableVertexAttribArray(bd->AttribLocationVtxUV));
522 GL_CALL(glEnableVertexAttribArray(bd->AttribLocationVtxColor));
523 GL_CALL(glVertexAttribPointer(bd->AttribLocationVtxPos, 2, GL_FLOAT, GL_FALSE, sizeof(ImDrawVert), (GLvoid*)offsetof(ImDrawVert, pos)));
524 GL_CALL(glVertexAttribPointer(bd->AttribLocationVtxUV, 2, GL_FLOAT, GL_FALSE, sizeof(ImDrawVert), (GLvoid*)offsetof(ImDrawVert, uv)));
525 GL_CALL(glVertexAttribPointer(bd->AttribLocationVtxColor, 4, GL_UNSIGNED_BYTE, GL_TRUE, sizeof(ImDrawVert), (GLvoid*)offsetof(ImDrawVert, col)));
526}
527
528// OpenGL3 Render function.
529// Note that this implementation is little overcomplicated because we are saving/setting up/restoring every OpenGL state explicitly.
530// This is in order to be able to run within an OpenGL engine that doesn't do so.
531void ImGui_ImplOpenGL3_RenderDrawData(ImDrawData* draw_data)
532{
533 // Avoid rendering when minimized, scale coordinates for retina displays (screen coordinates != framebuffer coordinates)
534 int fb_width = (int)(draw_data->DisplaySize.x * draw_data->FramebufferScale.x);
535 int fb_height = (int)(draw_data->DisplaySize.y * draw_data->FramebufferScale.y);
536 if (fb_width <= 0 || fb_height <= 0)
537 return;
538
539 ImGui_ImplOpenGL3_InitLoader(); // Lazily init loader if not already done for e.g. DLL boundaries.
540
541 ImGui_ImplOpenGL3_Data* bd = ImGui_ImplOpenGL3_GetBackendData();
542
543 // Catch up with texture updates. Most of the times, the list will have 1 element with an OK status, aka nothing to do.
544 // (This almost always points to ImGui::GetPlatformIO().Textures[] but is part of ImDrawData to allow overriding or disabling texture updates).
545 if (draw_data->Textures != nullptr)
546 for (ImTextureData* tex : *draw_data->Textures)
547 if (tex->Status != ImTextureStatus_OK)
548 ImGui_ImplOpenGL3_UpdateTexture(tex);
549
550 // Backup GL state
551 GLenum last_active_texture; glGetIntegerv(GL_ACTIVE_TEXTURE, (GLint*)&last_active_texture);
552 glActiveTexture(GL_TEXTURE0);
553 GLuint last_program; glGetIntegerv(GL_CURRENT_PROGRAM, (GLint*)&last_program);
554 GLuint last_texture; glGetIntegerv(GL_TEXTURE_BINDING_2D, (GLint*)&last_texture);
555#ifdef IMGUI_IMPL_OPENGL_MAY_HAVE_BIND_SAMPLER
556 GLuint last_sampler; if (bd->GlVersion >= 330 || bd->GlProfileIsES3) { glGetIntegerv(GL_SAMPLER_BINDING, (GLint*)&last_sampler); } else { last_sampler = 0; }
557#endif
558 GLuint last_array_buffer; glGetIntegerv(GL_ARRAY_BUFFER_BINDING, (GLint*)&last_array_buffer);
559#ifndef IMGUI_IMPL_OPENGL_USE_VERTEX_ARRAY
560 // This is part of VAO on OpenGL 3.0+ and OpenGL ES 3.0+.
561 GLint last_element_array_buffer; glGetIntegerv(GL_ELEMENT_ARRAY_BUFFER_BINDING, &last_element_array_buffer);
562 ImGui_ImplOpenGL3_VtxAttribState last_vtx_attrib_state_pos; last_vtx_attrib_state_pos.GetState(bd->AttribLocationVtxPos);
563 ImGui_ImplOpenGL3_VtxAttribState last_vtx_attrib_state_uv; last_vtx_attrib_state_uv.GetState(bd->AttribLocationVtxUV);
564 ImGui_ImplOpenGL3_VtxAttribState last_vtx_attrib_state_color; last_vtx_attrib_state_color.GetState(bd->AttribLocationVtxColor);
565#endif
566#ifdef IMGUI_IMPL_OPENGL_USE_VERTEX_ARRAY
567 GLuint last_vertex_array_object; glGetIntegerv(GL_VERTEX_ARRAY_BINDING, (GLint*)&last_vertex_array_object);
568#endif
569#ifdef IMGUI_IMPL_OPENGL_MAY_HAVE_POLYGON_MODE
570 GLint last_polygon_mode[2]; if (bd->HasPolygonMode) { glGetIntegerv(GL_POLYGON_MODE, last_polygon_mode); }
571#endif
572 GLint last_viewport[4]; glGetIntegerv(GL_VIEWPORT, last_viewport);
573 GLint last_scissor_box[4]; glGetIntegerv(GL_SCISSOR_BOX, last_scissor_box);
574 GLenum last_blend_src_rgb; glGetIntegerv(GL_BLEND_SRC_RGB, (GLint*)&last_blend_src_rgb);
575 GLenum last_blend_dst_rgb; glGetIntegerv(GL_BLEND_DST_RGB, (GLint*)&last_blend_dst_rgb);
576 GLenum last_blend_src_alpha; glGetIntegerv(GL_BLEND_SRC_ALPHA, (GLint*)&last_blend_src_alpha);
577 GLenum last_blend_dst_alpha; glGetIntegerv(GL_BLEND_DST_ALPHA, (GLint*)&last_blend_dst_alpha);
578 GLenum last_blend_equation_rgb; glGetIntegerv(GL_BLEND_EQUATION_RGB, (GLint*)&last_blend_equation_rgb);
579 GLenum last_blend_equation_alpha; glGetIntegerv(GL_BLEND_EQUATION_ALPHA, (GLint*)&last_blend_equation_alpha);
580 GLboolean last_enable_blend = glIsEnabled(GL_BLEND);
581 GLboolean last_enable_cull_face = glIsEnabled(GL_CULL_FACE);
582 GLboolean last_enable_depth_test = glIsEnabled(GL_DEPTH_TEST);
583 GLboolean last_enable_stencil_test = glIsEnabled(GL_STENCIL_TEST);
584 GLboolean last_enable_scissor_test = glIsEnabled(GL_SCISSOR_TEST);
585#ifdef IMGUI_IMPL_OPENGL_MAY_HAVE_PRIMITIVE_RESTART
586 GLboolean last_enable_primitive_restart = (!bd->GlProfileIsES3 && bd->GlVersion >= 310) ? glIsEnabled(GL_PRIMITIVE_RESTART) : GL_FALSE;
587#endif
588
589 // Setup desired GL state
590 // Recreate the VAO every time (this is to easily allow multiple GL contexts to be rendered to. VAO are not shared among GL contexts)
591 // The renderer would actually work without any VAO bound, but then our VertexAttrib calls would overwrite the default one currently bound.
592 GLuint vertex_array_object = 0;
593#ifdef IMGUI_IMPL_OPENGL_USE_VERTEX_ARRAY
594 GL_CALL(glGenVertexArrays(1, &vertex_array_object));
595#endif
596 ImGui_ImplOpenGL3_SetupRenderState(draw_data, fb_width, fb_height, vertex_array_object);
597
598 // Will project scissor/clipping rectangles into framebuffer space
599 ImVec2 clip_off = draw_data->DisplayPos; // (0,0) unless using multi-viewports
600 ImVec2 clip_scale = draw_data->FramebufferScale; // (1,1) unless using retina display which are often (2,2)
601
602 // Render command lists
603 for (const ImDrawList* draw_list : draw_data->CmdLists)
604 {
605 // Upload vertex/index buffers
606 // - OpenGL drivers are in a very sorry state nowadays....
607 // During 2021 we attempted to switch from glBufferData() to orphaning+glBufferSubData() following reports
608 // of leaks on Intel GPU when using multi-viewports on Windows.
609 // - After this we kept hearing of various display corruptions issues. We started disabling on non-Intel GPU, but issues still got reported on Intel.
610 // - We are now back to using exclusively glBufferData(). So bd->UseBufferSubData IS ALWAYS FALSE in this code.
611 // We are keeping the old code path for a while in case people finding new issues may want to test the bd->UseBufferSubData path.
612 // - See https://github.com/ocornut/imgui/issues/4468 and please report any corruption issues.
613 const GLsizeiptr vtx_buffer_size = (GLsizeiptr)draw_list->VtxBuffer.Size * (int)sizeof(ImDrawVert);
614 const GLsizeiptr idx_buffer_size = (GLsizeiptr)draw_list->IdxBuffer.Size * (int)sizeof(ImDrawIdx);
615 if (bd->UseBufferSubData)
616 {
617 if (bd->VertexBufferSize < vtx_buffer_size)
618 {
619 bd->VertexBufferSize = vtx_buffer_size;
620 GL_CALL(glBufferData(GL_ARRAY_BUFFER, bd->VertexBufferSize, nullptr, GL_STREAM_DRAW));
621 }
622 if (bd->IndexBufferSize < idx_buffer_size)
623 {
624 bd->IndexBufferSize = idx_buffer_size;
625 GL_CALL(glBufferData(GL_ELEMENT_ARRAY_BUFFER, bd->IndexBufferSize, nullptr, GL_STREAM_DRAW));
626 }
627 GL_CALL(glBufferSubData(GL_ARRAY_BUFFER, 0, vtx_buffer_size, (const GLvoid*)draw_list->VtxBuffer.Data));
628 GL_CALL(glBufferSubData(GL_ELEMENT_ARRAY_BUFFER, 0, idx_buffer_size, (const GLvoid*)draw_list->IdxBuffer.Data));
629 }
630 else
631 {
632 GL_CALL(glBufferData(GL_ARRAY_BUFFER, vtx_buffer_size, (const GLvoid*)draw_list->VtxBuffer.Data, GL_STREAM_DRAW));
633 GL_CALL(glBufferData(GL_ELEMENT_ARRAY_BUFFER, idx_buffer_size, (const GLvoid*)draw_list->IdxBuffer.Data, GL_STREAM_DRAW));
634 }
635
636 for (int cmd_i = 0; cmd_i < draw_list->CmdBuffer.Size; cmd_i++)
637 {
638 const ImDrawCmd* pcmd = &draw_list->CmdBuffer[cmd_i];
639 if (pcmd->UserCallback != nullptr)
640 {
641 // User callback, registered via ImDrawList::AddCallback()
642 // (ImDrawCallback_ResetRenderState is a special callback value used by the user to request the renderer to reset render state.)
643 if (pcmd->UserCallback == ImDrawCallback_ResetRenderState)
644 ImGui_ImplOpenGL3_SetupRenderState(draw_data, fb_width, fb_height, vertex_array_object);
645 else
646 pcmd->UserCallback(draw_list, pcmd);
647 }
648 else
649 {
650 // Project scissor/clipping rectangles into framebuffer space
651 ImVec2 clip_min((pcmd->ClipRect.x - clip_off.x) * clip_scale.x, (pcmd->ClipRect.y - clip_off.y) * clip_scale.y);
652 ImVec2 clip_max((pcmd->ClipRect.z - clip_off.x) * clip_scale.x, (pcmd->ClipRect.w - clip_off.y) * clip_scale.y);
653 if (clip_max.x <= clip_min.x || clip_max.y <= clip_min.y)
654 continue;
655
656 // Apply scissor/clipping rectangle (Y is inverted in OpenGL)
657 GL_CALL(glScissor((int)clip_min.x, (int)((float)fb_height - clip_max.y), (int)(clip_max.x - clip_min.x), (int)(clip_max.y - clip_min.y)));
658
659 // Bind texture, Draw
660 GL_CALL(glBindTexture(GL_TEXTURE_2D, (GLuint)(intptr_t)pcmd->GetTexID()));
661#ifdef IMGUI_IMPL_OPENGL_MAY_HAVE_VTX_OFFSET
662 if (bd->GlVersion >= 320)
663 GL_CALL(glDrawElementsBaseVertex(GL_TRIANGLES, (GLsizei)pcmd->ElemCount, sizeof(ImDrawIdx) == 2 ? GL_UNSIGNED_SHORT : GL_UNSIGNED_INT, (void*)(intptr_t)(pcmd->IdxOffset * sizeof(ImDrawIdx)), (GLint)pcmd->VtxOffset));
664 else
665#endif
666 GL_CALL(glDrawElements(GL_TRIANGLES, (GLsizei)pcmd->ElemCount, sizeof(ImDrawIdx) == 2 ? GL_UNSIGNED_SHORT : GL_UNSIGNED_INT, (void*)(intptr_t)(pcmd->IdxOffset * sizeof(ImDrawIdx))));
667 }
668 }
669 }
670
671 // Destroy the temporary VAO
672#ifdef IMGUI_IMPL_OPENGL_USE_VERTEX_ARRAY
673 GL_CALL(glDeleteVertexArrays(1, &vertex_array_object));
674#endif
675
676 // Restore modified GL state
677 // This "glIsProgram()" check is required because if the program is "pending deletion" at the time of binding backup, it will have been deleted by now and will cause an OpenGL error. See #6220.
678 if (last_program == 0 || glIsProgram(last_program)) glUseProgram(last_program);
679 glBindTexture(GL_TEXTURE_2D, last_texture);
680#ifdef IMGUI_IMPL_OPENGL_MAY_HAVE_BIND_SAMPLER
681 if (bd->GlVersion >= 330 || bd->GlProfileIsES3)
682 glBindSampler(0, last_sampler);
683#endif
684 glActiveTexture(last_active_texture);
685#ifdef IMGUI_IMPL_OPENGL_USE_VERTEX_ARRAY
686 glBindVertexArray(last_vertex_array_object);
687#endif
688 glBindBuffer(GL_ARRAY_BUFFER, last_array_buffer);
689#ifndef IMGUI_IMPL_OPENGL_USE_VERTEX_ARRAY
690 glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, last_element_array_buffer);
691 last_vtx_attrib_state_pos.SetState(bd->AttribLocationVtxPos);
692 last_vtx_attrib_state_uv.SetState(bd->AttribLocationVtxUV);
693 last_vtx_attrib_state_color.SetState(bd->AttribLocationVtxColor);
694#endif
695 glBlendEquationSeparate(last_blend_equation_rgb, last_blend_equation_alpha);
696 glBlendFuncSeparate(last_blend_src_rgb, last_blend_dst_rgb, last_blend_src_alpha, last_blend_dst_alpha);
697 if (last_enable_blend) glEnable(GL_BLEND); else glDisable(GL_BLEND);
698 if (last_enable_cull_face) glEnable(GL_CULL_FACE); else glDisable(GL_CULL_FACE);
699 if (last_enable_depth_test) glEnable(GL_DEPTH_TEST); else glDisable(GL_DEPTH_TEST);
700 if (last_enable_stencil_test) glEnable(GL_STENCIL_TEST); else glDisable(GL_STENCIL_TEST);
701 if (last_enable_scissor_test) glEnable(GL_SCISSOR_TEST); else glDisable(GL_SCISSOR_TEST);
702#ifdef IMGUI_IMPL_OPENGL_MAY_HAVE_PRIMITIVE_RESTART
703 if (!bd->GlProfileIsES3 && bd->GlVersion >= 310) { if (last_enable_primitive_restart) glEnable(GL_PRIMITIVE_RESTART); else glDisable(GL_PRIMITIVE_RESTART); }
704#endif
705
706#ifdef IMGUI_IMPL_OPENGL_MAY_HAVE_POLYGON_MODE
707 // Desktop OpenGL 3.0 and OpenGL 3.1 had separate polygon draw modes for front-facing and back-facing faces of polygons
708 if (bd->HasPolygonMode) { if (bd->GlVersion <= 310 || bd->GlProfileIsCompat) { glPolygonMode(GL_FRONT, (GLenum)last_polygon_mode[0]); glPolygonMode(GL_BACK, (GLenum)last_polygon_mode[1]); } else { glPolygonMode(GL_FRONT_AND_BACK, (GLenum)last_polygon_mode[0]); } }
709#endif // IMGUI_IMPL_OPENGL_MAY_HAVE_POLYGON_MODE
710
711 glViewport(last_viewport[0], last_viewport[1], (GLsizei)last_viewport[2], (GLsizei)last_viewport[3]);
712 glScissor(last_scissor_box[0], last_scissor_box[1], (GLsizei)last_scissor_box[2], (GLsizei)last_scissor_box[3]);
713 (void)bd; // Not all compilation paths use this
714}
715
716static void ImGui_ImplOpenGL3_DestroyTexture(ImTextureData* tex)
717{
718 GLuint gl_tex_id = (GLuint)(intptr_t)tex->TexID;
719 glDeleteTextures(1, &gl_tex_id);
720
721 // Clear identifiers and mark as destroyed (in order to allow e.g. calling InvalidateDeviceObjects while running)
722 tex->SetTexID(ImTextureID_Invalid);
723 tex->SetStatus(ImTextureStatus_Destroyed);
724}
725
726void ImGui_ImplOpenGL3_UpdateTexture(ImTextureData* tex)
727{
728 // FIXME: Consider backing up and restoring
729 if (tex->Status == ImTextureStatus_WantCreate || tex->Status == ImTextureStatus_WantUpdates)
730 {
731#ifdef GL_UNPACK_ROW_LENGTH // Not on WebGL/ES
732 GL_CALL(glPixelStorei(GL_UNPACK_ROW_LENGTH, 0));
733#endif
734#ifdef GL_UNPACK_ALIGNMENT
735 GL_CALL(glPixelStorei(GL_UNPACK_ALIGNMENT, 1));
736#endif
737 }
738
739 if (tex->Status == ImTextureStatus_WantCreate)
740 {
741 // Create and upload new texture to graphics system
742 //IMGUI_DEBUG_LOG("UpdateTexture #%03d: WantCreate %dx%d\n", tex->UniqueID, tex->Width, tex->Height);
743 IM_ASSERT(tex->TexID == 0 && tex->BackendUserData == nullptr);
744 IM_ASSERT(tex->Format == ImTextureFormat_RGBA32);
745 const void* pixels = tex->GetPixels();
746 GLuint gl_texture_id = 0;
747
748 // Upload texture to graphics system
749 // (Bilinear sampling is required by default. Set 'io.Fonts->Flags |= ImFontAtlasFlags_NoBakedLines' or 'style.AntiAliasedLinesUseTex = false' to allow point/nearest sampling)
750 GLint last_texture;
751 GL_CALL(glGetIntegerv(GL_TEXTURE_BINDING_2D, &last_texture));
752 GL_CALL(glGenTextures(1, &gl_texture_id));
753 GL_CALL(glBindTexture(GL_TEXTURE_2D, gl_texture_id));
754 GL_CALL(glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR));
755 GL_CALL(glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR));
756 GL_CALL(glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE));
757 GL_CALL(glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE));
758 GL_CALL(glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, tex->Width, tex->Height, 0, GL_RGBA, GL_UNSIGNED_BYTE, pixels));
759
760 // Store identifiers
761 tex->SetTexID((ImTextureID)(intptr_t)gl_texture_id);
762 tex->SetStatus(ImTextureStatus_OK);
763
764 // Restore state
765 GL_CALL(glBindTexture(GL_TEXTURE_2D, last_texture));
766 }
767 else if (tex->Status == ImTextureStatus_WantUpdates)
768 {
769 // Update selected blocks. We only ever write to textures regions which have never been used before!
770 // This backend choose to use tex->Updates[] but you can use tex->UpdateRect to upload a single region.
771 GLint last_texture;
772 GL_CALL(glGetIntegerv(GL_TEXTURE_BINDING_2D, &last_texture));
773
774 GLuint gl_tex_id = (GLuint)(intptr_t)tex->TexID;
775 GL_CALL(glBindTexture(GL_TEXTURE_2D, gl_tex_id));
776#if GL_UNPACK_ROW_LENGTH // Not on WebGL/ES
777 GL_CALL(glPixelStorei(GL_UNPACK_ROW_LENGTH, tex->Width));
778 for (ImTextureRect& r : tex->Updates)
779 GL_CALL(glTexSubImage2D(GL_TEXTURE_2D, 0, r.x, r.y, r.w, r.h, GL_RGBA, GL_UNSIGNED_BYTE, tex->GetPixelsAt(r.x, r.y)));
780 GL_CALL(glPixelStorei(GL_UNPACK_ROW_LENGTH, 0));
781#else
782 // GL ES doesn't have GL_UNPACK_ROW_LENGTH, so we need to (A) copy to a contiguous buffer or (B) upload line by line.
783 ImGui_ImplOpenGL3_Data* bd = ImGui_ImplOpenGL3_GetBackendData();
784 for (ImTextureRect& r : tex->Updates)
785 {
786 const int src_pitch = r.w * tex->BytesPerPixel;
787 bd->TempBuffer.resize(r.h * src_pitch);
788 char* out_p = bd->TempBuffer.Data;
789 for (int y = 0; y < r.h; y++, out_p += src_pitch)
790 memcpy(out_p, tex->GetPixelsAt(r.x, r.y + y), src_pitch);
791 IM_ASSERT(out_p == bd->TempBuffer.end());
792 GL_CALL(glTexSubImage2D(GL_TEXTURE_2D, 0, r.x, r.y, r.w, r.h, GL_RGBA, GL_UNSIGNED_BYTE, bd->TempBuffer.Data));
793 }
794#endif
795 tex->SetStatus(ImTextureStatus_OK);
796 GL_CALL(glBindTexture(GL_TEXTURE_2D, last_texture)); // Restore state
797 }
798 else if (tex->Status == ImTextureStatus_WantDestroy && tex->UnusedFrames > 0)
799 ImGui_ImplOpenGL3_DestroyTexture(tex);
800}
801
802// If you get an error please report on github. You may try different GL context version or GLSL version. See GL<>GLSL version table at the top of this file.
803static bool CheckShader(GLuint handle, const char* desc)
804{
805 ImGui_ImplOpenGL3_Data* bd = ImGui_ImplOpenGL3_GetBackendData();
806 GLint status = 0, log_length = 0;
807 glGetShaderiv(handle, GL_COMPILE_STATUS, &status);
808 glGetShaderiv(handle, GL_INFO_LOG_LENGTH, &log_length);
809 if ((GLboolean)status == GL_FALSE)
810 fprintf(stderr, format: "ERROR: ImGui_ImplOpenGL3_CreateDeviceObjects: failed to compile %s! With GLSL: %s\n", desc, bd->GlslVersionString);
811 if (log_length > 1)
812 {
813 ImVector<char> buf;
814 buf.resize(new_size: (int)(log_length + 1));
815 glGetShaderInfoLog(handle, log_length, nullptr, (GLchar*)buf.begin());
816 fprintf(stderr, format: "%s\n", buf.begin());
817 }
818 return (GLboolean)status == GL_TRUE;
819}
820
821// If you get an error please report on GitHub. You may try different GL context version or GLSL version.
822static bool CheckProgram(GLuint handle, const char* desc)
823{
824 ImGui_ImplOpenGL3_Data* bd = ImGui_ImplOpenGL3_GetBackendData();
825 GLint status = 0, log_length = 0;
826 glGetProgramiv(handle, GL_LINK_STATUS, &status);
827 glGetProgramiv(handle, GL_INFO_LOG_LENGTH, &log_length);
828 if ((GLboolean)status == GL_FALSE)
829 fprintf(stderr, format: "ERROR: ImGui_ImplOpenGL3_CreateDeviceObjects: failed to link %s! With GLSL %s\n", desc, bd->GlslVersionString);
830 if (log_length > 1)
831 {
832 ImVector<char> buf;
833 buf.resize(new_size: (int)(log_length + 1));
834 glGetProgramInfoLog(handle, log_length, nullptr, (GLchar*)buf.begin());
835 fprintf(stderr, format: "%s\n", buf.begin());
836 }
837 return (GLboolean)status == GL_TRUE;
838}
839
840bool ImGui_ImplOpenGL3_CreateDeviceObjects()
841{
842 ImGui_ImplOpenGL3_Data* bd = ImGui_ImplOpenGL3_GetBackendData();
843
844 // Backup GL state
845 GLint last_texture, last_array_buffer;
846 glGetIntegerv(GL_TEXTURE_BINDING_2D, &last_texture);
847 glGetIntegerv(GL_ARRAY_BUFFER_BINDING, &last_array_buffer);
848#ifdef IMGUI_IMPL_OPENGL_MAY_HAVE_BIND_BUFFER_PIXEL_UNPACK
849 GLint last_pixel_unpack_buffer = 0;
850 if (bd->GlVersion >= 210) { glGetIntegerv(GL_PIXEL_UNPACK_BUFFER_BINDING, &last_pixel_unpack_buffer); glBindBuffer(GL_PIXEL_UNPACK_BUFFER, 0); }
851#endif
852#ifdef IMGUI_IMPL_OPENGL_USE_VERTEX_ARRAY
853 GLint last_vertex_array;
854 glGetIntegerv(GL_VERTEX_ARRAY_BINDING, &last_vertex_array);
855#endif
856
857 // Parse GLSL version string
858 int glsl_version = 130;
859 sscanf(s: bd->GlslVersionString, format: "#version %d", &glsl_version);
860
861 const GLchar* vertex_shader_glsl_120 =
862 "uniform mat4 ProjMtx;\n"
863 "attribute vec2 Position;\n"
864 "attribute vec2 UV;\n"
865 "attribute vec4 Color;\n"
866 "varying vec2 Frag_UV;\n"
867 "varying vec4 Frag_Color;\n"
868 "void main()\n"
869 "{\n"
870 " Frag_UV = UV;\n"
871 " Frag_Color = Color;\n"
872 " gl_Position = ProjMtx * vec4(Position.xy,0,1);\n"
873 "}\n";
874
875 const GLchar* vertex_shader_glsl_130 =
876 "uniform mat4 ProjMtx;\n"
877 "in vec2 Position;\n"
878 "in vec2 UV;\n"
879 "in vec4 Color;\n"
880 "out vec2 Frag_UV;\n"
881 "out vec4 Frag_Color;\n"
882 "void main()\n"
883 "{\n"
884 " Frag_UV = UV;\n"
885 " Frag_Color = Color;\n"
886 " gl_Position = ProjMtx * vec4(Position.xy,0,1);\n"
887 "}\n";
888
889 const GLchar* vertex_shader_glsl_300_es =
890 "precision highp float;\n"
891 "layout (location = 0) in vec2 Position;\n"
892 "layout (location = 1) in vec2 UV;\n"
893 "layout (location = 2) in vec4 Color;\n"
894 "uniform mat4 ProjMtx;\n"
895 "out vec2 Frag_UV;\n"
896 "out vec4 Frag_Color;\n"
897 "void main()\n"
898 "{\n"
899 " Frag_UV = UV;\n"
900 " Frag_Color = Color;\n"
901 " gl_Position = ProjMtx * vec4(Position.xy,0,1);\n"
902 "}\n";
903
904 const GLchar* vertex_shader_glsl_410_core =
905 "layout (location = 0) in vec2 Position;\n"
906 "layout (location = 1) in vec2 UV;\n"
907 "layout (location = 2) in vec4 Color;\n"
908 "uniform mat4 ProjMtx;\n"
909 "out vec2 Frag_UV;\n"
910 "out vec4 Frag_Color;\n"
911 "void main()\n"
912 "{\n"
913 " Frag_UV = UV;\n"
914 " Frag_Color = Color;\n"
915 " gl_Position = ProjMtx * vec4(Position.xy,0,1);\n"
916 "}\n";
917
918 const GLchar* fragment_shader_glsl_120 =
919 "#ifdef GL_ES\n"
920 " precision mediump float;\n"
921 "#endif\n"
922 "uniform sampler2D Texture;\n"
923 "varying vec2 Frag_UV;\n"
924 "varying vec4 Frag_Color;\n"
925 "void main()\n"
926 "{\n"
927 " gl_FragColor = Frag_Color * texture2D(Texture, Frag_UV.st);\n"
928 "}\n";
929
930 const GLchar* fragment_shader_glsl_130 =
931 "uniform sampler2D Texture;\n"
932 "in vec2 Frag_UV;\n"
933 "in vec4 Frag_Color;\n"
934 "out vec4 Out_Color;\n"
935 "void main()\n"
936 "{\n"
937 " Out_Color = Frag_Color * texture(Texture, Frag_UV.st);\n"
938 "}\n";
939
940 const GLchar* fragment_shader_glsl_300_es =
941 "precision mediump float;\n"
942 "uniform sampler2D Texture;\n"
943 "in vec2 Frag_UV;\n"
944 "in vec4 Frag_Color;\n"
945 "layout (location = 0) out vec4 Out_Color;\n"
946 "void main()\n"
947 "{\n"
948 " Out_Color = Frag_Color * texture(Texture, Frag_UV.st);\n"
949 "}\n";
950
951 const GLchar* fragment_shader_glsl_410_core =
952 "in vec2 Frag_UV;\n"
953 "in vec4 Frag_Color;\n"
954 "uniform sampler2D Texture;\n"
955 "layout (location = 0) out vec4 Out_Color;\n"
956 "void main()\n"
957 "{\n"
958 " Out_Color = Frag_Color * texture(Texture, Frag_UV.st);\n"
959 "}\n";
960
961 // Select shaders matching our GLSL versions
962 const GLchar* vertex_shader = nullptr;
963 const GLchar* fragment_shader = nullptr;
964 if (glsl_version < 130)
965 {
966 vertex_shader = vertex_shader_glsl_120;
967 fragment_shader = fragment_shader_glsl_120;
968 }
969 else if (glsl_version >= 410)
970 {
971 vertex_shader = vertex_shader_glsl_410_core;
972 fragment_shader = fragment_shader_glsl_410_core;
973 }
974 else if (glsl_version == 300)
975 {
976 vertex_shader = vertex_shader_glsl_300_es;
977 fragment_shader = fragment_shader_glsl_300_es;
978 }
979 else
980 {
981 vertex_shader = vertex_shader_glsl_130;
982 fragment_shader = fragment_shader_glsl_130;
983 }
984
985 // Create shaders
986 const GLchar* vertex_shader_with_version[2] = { bd->GlslVersionString, vertex_shader };
987 GLuint vert_handle;
988 GL_CALL(vert_handle = glCreateShader(GL_VERTEX_SHADER));
989 glShaderSource(vert_handle, 2, vertex_shader_with_version, nullptr);
990 glCompileShader(vert_handle);
991 if (!CheckShader(handle: vert_handle, desc: "vertex shader"))
992 return false;
993
994 const GLchar* fragment_shader_with_version[2] = { bd->GlslVersionString, fragment_shader };
995 GLuint frag_handle;
996 GL_CALL(frag_handle = glCreateShader(GL_FRAGMENT_SHADER));
997 glShaderSource(frag_handle, 2, fragment_shader_with_version, nullptr);
998 glCompileShader(frag_handle);
999 if (!CheckShader(handle: frag_handle, desc: "fragment shader"))
1000 return false;
1001
1002 // Link
1003 bd->ShaderHandle = glCreateProgram();
1004 glAttachShader(bd->ShaderHandle, vert_handle);
1005 glAttachShader(bd->ShaderHandle, frag_handle);
1006 glLinkProgram(bd->ShaderHandle);
1007 if (!CheckProgram(handle: bd->ShaderHandle, desc: "shader program"))
1008 return false;
1009
1010 glDetachShader(bd->ShaderHandle, vert_handle);
1011 glDetachShader(bd->ShaderHandle, frag_handle);
1012 glDeleteShader(vert_handle);
1013 glDeleteShader(frag_handle);
1014
1015 bd->AttribLocationTex = glGetUniformLocation(bd->ShaderHandle, "Texture");
1016 bd->AttribLocationProjMtx = glGetUniformLocation(bd->ShaderHandle, "ProjMtx");
1017 bd->AttribLocationVtxPos = (GLuint)glGetAttribLocation(bd->ShaderHandle, "Position");
1018 bd->AttribLocationVtxUV = (GLuint)glGetAttribLocation(bd->ShaderHandle, "UV");
1019 bd->AttribLocationVtxColor = (GLuint)glGetAttribLocation(bd->ShaderHandle, "Color");
1020
1021 // Create buffers
1022 glGenBuffers(1, &bd->VboHandle);
1023 glGenBuffers(1, &bd->ElementsHandle);
1024
1025 // Restore modified GL state
1026 glBindTexture(GL_TEXTURE_2D, last_texture);
1027 glBindBuffer(GL_ARRAY_BUFFER, last_array_buffer);
1028#ifdef IMGUI_IMPL_OPENGL_MAY_HAVE_BIND_BUFFER_PIXEL_UNPACK
1029 if (bd->GlVersion >= 210) { glBindBuffer(GL_PIXEL_UNPACK_BUFFER, last_pixel_unpack_buffer); }
1030#endif
1031#ifdef IMGUI_IMPL_OPENGL_USE_VERTEX_ARRAY
1032 glBindVertexArray(last_vertex_array);
1033#endif
1034
1035 return true;
1036}
1037
1038void ImGui_ImplOpenGL3_DestroyDeviceObjects()
1039{
1040 ImGui_ImplOpenGL3_Data* bd = ImGui_ImplOpenGL3_GetBackendData();
1041 if (bd->VboHandle) { glDeleteBuffers(1, &bd->VboHandle); bd->VboHandle = 0; }
1042 if (bd->ElementsHandle) { glDeleteBuffers(1, &bd->ElementsHandle); bd->ElementsHandle = 0; }
1043 if (bd->ShaderHandle) { glDeleteProgram(bd->ShaderHandle); bd->ShaderHandle = 0; }
1044
1045 // Destroy all textures
1046 for (ImTextureData* tex : ImGui::GetPlatformIO().Textures)
1047 if (tex->RefCount == 1)
1048 ImGui_ImplOpenGL3_DestroyTexture(tex);
1049}
1050
1051//--------------------------------------------------------------------------------------------------------
1052// MULTI-VIEWPORT / PLATFORM INTERFACE SUPPORT
1053// This is an _advanced_ and _optional_ feature, allowing the backend to create and handle multiple viewports simultaneously.
1054// If you are new to dear imgui or creating a new binding for dear imgui, it is recommended that you completely ignore this section first..
1055//--------------------------------------------------------------------------------------------------------
1056
1057static void ImGui_ImplOpenGL3_RenderWindow(ImGuiViewport* viewport, void*)
1058{
1059 if (!(viewport->Flags & ImGuiViewportFlags_NoRendererClear))
1060 {
1061 ImVec4 clear_color = ImVec4(0.0f, 0.0f, 0.0f, 1.0f);
1062 glClearColor(clear_color.x, clear_color.y, clear_color.z, clear_color.w);
1063 glClear(GL_COLOR_BUFFER_BIT);
1064 }
1065 ImGui_ImplOpenGL3_RenderDrawData(draw_data: viewport->DrawData);
1066}
1067
1068static void ImGui_ImplOpenGL3_InitMultiViewportSupport()
1069{
1070 ImGuiPlatformIO& platform_io = ImGui::GetPlatformIO();
1071 platform_io.Renderer_RenderWindow = ImGui_ImplOpenGL3_RenderWindow;
1072}
1073
1074static void ImGui_ImplOpenGL3_ShutdownMultiViewportSupport()
1075{
1076 ImGui::DestroyPlatformWindows();
1077}
1078
1079//-----------------------------------------------------------------------------
1080
1081#if defined(__GNUC__)
1082#pragma GCC diagnostic pop
1083#endif
1084#if defined(__clang__)
1085#pragma clang diagnostic pop
1086#endif
1087
1088#endif // #ifndef IMGUI_DISABLE
1089

source code of imgui/backends/imgui_impl_opengl3.cpp