imgui_impl_opengl3.cpp 53 KB

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