imgui_impl_opengl3.cpp 54 KB

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