From 3404f4e86b8230adba9b402f74beba55a18fe8bb Mon Sep 17 00:00:00 2001 From: Roberto Fernandez Bautista Date: Fri, 30 Oct 2020 19:35:24 +0000 Subject: [PATCH] Check GLEW functions exist before calling them Fixes https://gitlab.com/kicad/code/kicad/-/issues/6235 --- common/gal/opengl/cached_container_gpu.cpp | 7 +++++- common/gal/opengl/cached_container_ram.cpp | 4 +++- common/gal/opengl/gpu_manager.cpp | 7 ++++-- common/gal/opengl/opengl_compositor.cpp | 7 ++++-- common/gal/opengl/shader.cpp | 25 ++++++++++++---------- 5 files changed, 33 insertions(+), 17 deletions(-) diff --git a/common/gal/opengl/cached_container_gpu.cpp b/common/gal/opengl/cached_container_gpu.cpp index bca5e078ac..b1da9e3b20 100644 --- a/common/gal/opengl/cached_container_gpu.cpp +++ b/common/gal/opengl/cached_container_gpu.cpp @@ -65,7 +65,8 @@ CACHED_CONTAINER_GPU::~CACHED_CONTAINER_GPU() if( m_isMapped ) Unmap(); - glDeleteBuffers( 1, &m_glBufferHandle ); + if( glDeleteBuffers ) + glDeleteBuffers( 1, &m_glBufferHandle ); } @@ -73,6 +74,10 @@ void CACHED_CONTAINER_GPU::Map() { wxCHECK( !IsMapped(), /*void*/ ); + // OpenGL version might suddenly stop being available in Windows when an RDP session is started + if( !glBindBuffer ) + throw std::runtime_error( "OpenGL no longer available!" ); + glBindBuffer( GL_ARRAY_BUFFER, m_glBufferHandle ); m_vertices = static_cast( glMapBuffer( GL_ARRAY_BUFFER, GL_READ_WRITE ) ); diff --git a/common/gal/opengl/cached_container_ram.cpp b/common/gal/opengl/cached_container_ram.cpp index a306373dfa..7d77d79f80 100644 --- a/common/gal/opengl/cached_container_ram.cpp +++ b/common/gal/opengl/cached_container_ram.cpp @@ -51,7 +51,9 @@ CACHED_CONTAINER_RAM::CACHED_CONTAINER_RAM( unsigned int aSize ) : CACHED_CONTAINER_RAM::~CACHED_CONTAINER_RAM() { - glDeleteBuffers( 1, &m_verticesBuffer ); + if( glDeleteBuffers ) + glDeleteBuffers( 1, &m_verticesBuffer ); + free( m_vertices ); } diff --git a/common/gal/opengl/gpu_manager.cpp b/common/gal/opengl/gpu_manager.cpp index 93ff22f7e4..48bd0f1fea 100644 --- a/common/gal/opengl/gpu_manager.cpp +++ b/common/gal/opengl/gpu_manager.cpp @@ -85,8 +85,11 @@ GPU_CACHED_MANAGER::~GPU_CACHED_MANAGER() { if( m_buffersInitialized ) { - glBindBuffer( GL_ARRAY_BUFFER, 0 ); - glDeleteBuffers( 1, &m_indicesBuffer ); + if( glBindBuffer ) + glBindBuffer( GL_ARRAY_BUFFER, 0 ); + + if( glDeleteBuffers ) + glDeleteBuffers( 1, &m_indicesBuffer ); } } diff --git a/common/gal/opengl/opengl_compositor.cpp b/common/gal/opengl/opengl_compositor.cpp index 6fae6014f9..2133cd9477 100644 --- a/common/gal/opengl/opengl_compositor.cpp +++ b/common/gal/opengl/opengl_compositor.cpp @@ -399,8 +399,11 @@ void OPENGL_COMPOSITOR::clean() m_buffers.clear(); - glDeleteFramebuffersEXT( 1, &m_mainFbo ); - glDeleteRenderbuffersEXT( 1, &m_depthBuffer ); + if( glDeleteFramebuffersEXT ) + glDeleteFramebuffersEXT( 1, &m_mainFbo ); + + if( glDeleteRenderbuffersEXT ) + glDeleteRenderbuffersEXT( 1, &m_depthBuffer ); m_initialized = false; } diff --git a/common/gal/opengl/shader.cpp b/common/gal/opengl/shader.cpp index d78379c36b..f61ede2cd0 100644 --- a/common/gal/opengl/shader.cpp +++ b/common/gal/opengl/shader.cpp @@ -59,20 +59,23 @@ SHADER::~SHADER() if( isProgramCreated ) { - // Delete the shaders and the program - for( std::deque::iterator it = shaderNumbers.begin(); - it != shaderNumbers.end(); ++it ) + if( glIsShader ) { - GLuint shader = *it; - - if( glIsShader( shader ) ) + // Delete the shaders and the program + for( std::deque::iterator it = shaderNumbers.begin(); it != shaderNumbers.end(); + ++it ) { - glDetachShader( programNumber, shader ); - glDeleteShader( shader ); - } - } + GLuint shader = *it; - glDeleteProgram( programNumber ); + if( glIsShader( shader ) ) + { + glDetachShader( programNumber, shader ); + glDeleteShader( shader ); + } + } + + glDeleteProgram( programNumber ); + } } }