Backends: OpenGL3: Destroy shader objects right away (#4244)
In OpenGL, after a shader program has been linked, shader objects don't need to be kept neither attached to the program nor in existence
diff --git a/backends/imgui_impl_opengl3.cpp b/backends/imgui_impl_opengl3.cpp
index 712de9b..2048603 100644
--- a/backends/imgui_impl_opengl3.cpp
+++ b/backends/imgui_impl_opengl3.cpp
@@ -7,13 +7,14 @@
// [X] Renderer: User texture binding. Use 'GLuint' OpenGL texture identifier as void*/ImTextureID. Read the FAQ about ImTextureID!
// [x] Renderer: Desktop GL only: Support for large meshes (64k+ vertices) with 16-bit indices.
-// You can use unmodified imgui_impl_* files in your project. See examples/ folder for examples of using this.
+// You can use unmodified imgui_impl_* files in your project. See examples/ folder for examples of using this.
// Prefer including the entire imgui/ repository into your project (either as a copy or as a submodule), and only build the backends you need.
// If you are new to Dear ImGui, read documentation from the docs/ folder + read the top of imgui.cpp.
// Read online: https://github.com/ocornut/imgui/tree/master/docs
// CHANGELOG
// (minor and older changes stripped away, please see git history for details)
+// 2021-06-21: OpenGL: Destroy individual vertex/fragment shader objects right after they are linked into the main shader.
// 2021-05-24: OpenGL: Access GL_CLIP_ORIGIN when "GL_ARB_clip_control" extension is detected, inside of just OpenGL 4.5 version.
// 2021-05-19: OpenGL: Replaced direct access to ImDrawCmd::TextureId with a call to ImDrawCmd::GetTexID(). (will become a requirement)
// 2021-04-06: OpenGL: Don't try to read GL_CLIP_ORIGIN unless we're OpenGL 4.5 or greater.
@@ -155,7 +156,7 @@
static GLuint g_GlVersion = 0; // Extracted at runtime using GL_MAJOR_VERSION, GL_MINOR_VERSION queries (e.g. 320 for GL 3.2)
static char g_GlslVersionString[32] = ""; // Specified by user or detected based on compile time GL settings.
static GLuint g_FontTexture = 0;
-static GLuint g_ShaderHandle = 0, g_VertHandle = 0, g_FragHandle = 0;
+static GLuint g_ShaderHandle = 0;
static GLint g_AttribLocationTex = 0, g_AttribLocationProjMtx = 0; // Uniforms location
static GLuint g_AttribLocationVtxPos = 0, g_AttribLocationVtxUV = 0, g_AttribLocationVtxColor = 0; // Vertex attributes location
static unsigned int g_VboHandle = 0, g_ElementsHandle = 0;
@@ -693,23 +694,29 @@
// Create shaders
const GLchar* vertex_shader_with_version[2] = { g_GlslVersionString, vertex_shader };
- g_VertHandle = glCreateShader(GL_VERTEX_SHADER);
- glShaderSource(g_VertHandle, 2, vertex_shader_with_version, NULL);
- glCompileShader(g_VertHandle);
- CheckShader(g_VertHandle, "vertex shader");
+ GLuint vert_handle = glCreateShader(GL_VERTEX_SHADER);
+ glShaderSource(vert_handle, 2, vertex_shader_with_version, NULL);
+ glCompileShader(vert_handle);
+ CheckShader(vert_handle, "vertex shader");
const GLchar* fragment_shader_with_version[2] = { g_GlslVersionString, fragment_shader };
- g_FragHandle = glCreateShader(GL_FRAGMENT_SHADER);
- glShaderSource(g_FragHandle, 2, fragment_shader_with_version, NULL);
- glCompileShader(g_FragHandle);
- CheckShader(g_FragHandle, "fragment shader");
+ GLuint frag_handle = glCreateShader(GL_FRAGMENT_SHADER);
+ glShaderSource(frag_handle, 2, fragment_shader_with_version, NULL);
+ glCompileShader(frag_handle);
+ CheckShader(frag_handle, "fragment shader");
+ // Link
g_ShaderHandle = glCreateProgram();
- glAttachShader(g_ShaderHandle, g_VertHandle);
- glAttachShader(g_ShaderHandle, g_FragHandle);
+ glAttachShader(g_ShaderHandle, vert_handle);
+ glAttachShader(g_ShaderHandle, frag_handle);
glLinkProgram(g_ShaderHandle);
CheckProgram(g_ShaderHandle, "shader program");
+ glDetachShader(g_ShaderHandle, vert_handle);
+ glDetachShader(g_ShaderHandle, frag_handle);
+ glDeleteShader(vert_handle);
+ glDeleteShader(frag_handle);
+
g_AttribLocationTex = glGetUniformLocation(g_ShaderHandle, "Texture");
g_AttribLocationProjMtx = glGetUniformLocation(g_ShaderHandle, "ProjMtx");
g_AttribLocationVtxPos = (GLuint)glGetAttribLocation(g_ShaderHandle, "Position");
@@ -736,10 +743,6 @@
{
if (g_VboHandle) { glDeleteBuffers(1, &g_VboHandle); g_VboHandle = 0; }
if (g_ElementsHandle) { glDeleteBuffers(1, &g_ElementsHandle); g_ElementsHandle = 0; }
- if (g_ShaderHandle && g_VertHandle) { glDetachShader(g_ShaderHandle, g_VertHandle); }
- if (g_ShaderHandle && g_FragHandle) { glDetachShader(g_ShaderHandle, g_FragHandle); }
- if (g_VertHandle) { glDeleteShader(g_VertHandle); g_VertHandle = 0; }
- if (g_FragHandle) { glDeleteShader(g_FragHandle); g_FragHandle = 0; }
if (g_ShaderHandle) { glDeleteProgram(g_ShaderHandle); g_ShaderHandle = 0; }
ImGui_ImplOpenGL3_DestroyFontsTexture();
diff --git a/docs/CHANGELOG.txt b/docs/CHANGELOG.txt
index 6fb4f09..c4ba478 100644
--- a/docs/CHANGELOG.txt
+++ b/docs/CHANGELOG.txt
@@ -54,6 +54,7 @@
- Backends: DX12: Fix texture casting crash on 32-bit systems (introduced on 2021/05/19 and v1.83) + added comments
about building on 32-bit systems. (#4225) [@kingofthebongo2008]
- Backends: OpenGL3: Handle GL_CLIP_ORIGIN on <4.5 contexts if "GL_ARB_clip_control" extension is detected. (#4170, #3998)
+- Backends: OpenGL3: Destroy vertex/fragment shader objects right after they are linked into main shader. (#4244) [@Crowbarous]
- Examples: Updated all .vcxproj to VS2015 (toolset v140) to facilitate usage with vcpkg.
- Examples: SDL2: Accomodate for vcpkg install having headers in SDL2/SDL.h vs SDL.h.