upleb.uk

Public git repos — served from a NIP-34 GRASP relay at git.upleb.uk

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