2020-06-26 17:39:43 +00:00
|
|
|
/*
|
|
|
|
* This program source code file is part of KiCad, a free EDA CAD application.
|
|
|
|
*
|
2022-11-17 16:29:11 +00:00
|
|
|
* Copyright (C) 2020-2022 Kicad Developers, see AUTHORS.txt for contributors
|
2020-06-26 17:39:43 +00:00
|
|
|
*
|
|
|
|
* This program is free software; you can redistribute it and/or
|
|
|
|
* modify it under the terms of the GNU General Public License
|
|
|
|
* as published by the Free Software Foundation; either version 2
|
|
|
|
* of the License, or (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
* along with this program; if not, you may find one here:
|
|
|
|
* http://www.gnu.org/licenses/old-licenses/gpl-3.0.html
|
|
|
|
* or you may search the http://www.gnu.org website for the version 3 license,
|
|
|
|
* or you may write to the Free Software Foundation, Inc.,
|
|
|
|
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
|
|
|
|
*/
|
|
|
|
|
|
|
|
#ifndef GL_UTILS_H
|
|
|
|
#define GL_UTILS_H
|
|
|
|
|
2022-06-07 19:33:28 +00:00
|
|
|
#include <gal/opengl/kiglew.h> // Must be included first
|
2022-01-06 18:51:25 +00:00
|
|
|
#include <wx/glcanvas.h>
|
2021-11-05 16:05:15 +00:00
|
|
|
#include <wx/utils.h>
|
2020-06-26 17:39:43 +00:00
|
|
|
|
2022-12-02 21:06:27 +00:00
|
|
|
#ifdef _WIN32
|
|
|
|
#pragma optimize( "", off )
|
|
|
|
#endif
|
|
|
|
|
2020-06-26 17:39:43 +00:00
|
|
|
class GL_UTILS
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
/**
|
2020-12-19 16:00:52 +00:00
|
|
|
* Attempts to set the OpenGL swap interval.
|
|
|
|
*
|
|
|
|
* @param aVal if -1 = try to set adaptive swapping, 0 = sync off, 1 = sync with VSYNC rate.
|
2020-06-26 17:39:43 +00:00
|
|
|
* @return actual value set
|
|
|
|
*/
|
|
|
|
static int SetSwapInterval( int aVal )
|
|
|
|
{
|
2020-12-19 16:00:52 +00:00
|
|
|
#if defined( __linux__ ) && !defined( KICAD_USE_EGL )
|
2021-11-05 16:05:15 +00:00
|
|
|
|
2022-06-07 19:33:28 +00:00
|
|
|
if( Display* dpy = glXGetCurrentDisplay() )
|
2020-06-26 17:39:43 +00:00
|
|
|
{
|
2022-06-07 19:33:28 +00:00
|
|
|
GLXDrawable drawable = glXGetCurrentDrawable();
|
|
|
|
|
2022-01-09 01:11:13 +00:00
|
|
|
std::string exts( glXQueryExtensionsString( dpy, DefaultScreen( dpy ) ) );
|
|
|
|
|
2022-06-07 19:33:28 +00:00
|
|
|
if( glXSwapIntervalEXT && glXQueryDrawable && drawable
|
|
|
|
&& exts.find( "GLX_EXT_swap_control" ) != std::string::npos )
|
2021-08-13 21:11:41 +00:00
|
|
|
{
|
2022-11-17 16:32:34 +00:00
|
|
|
if( aVal == -1 )
|
2021-08-13 21:11:41 +00:00
|
|
|
{
|
2022-06-07 19:33:28 +00:00
|
|
|
if( exts.find( "GLX_EXT_swap_control_tear" ) == std::string::npos )
|
2021-08-13 21:11:41 +00:00
|
|
|
{
|
2022-11-17 16:32:34 +00:00
|
|
|
aVal = 1;
|
2021-08-13 21:11:41 +00:00
|
|
|
}
|
2022-06-07 19:33:28 +00:00
|
|
|
else
|
|
|
|
{
|
|
|
|
// Even though the extensions might be available,
|
|
|
|
// we need to be sure that late/adaptive swaps are
|
|
|
|
// enabled on the drawable.
|
|
|
|
|
|
|
|
unsigned lateSwapsEnabled = 0;
|
|
|
|
glXQueryDrawable( dpy, drawable, GLX_LATE_SWAPS_TEAR_EXT,
|
|
|
|
&lateSwapsEnabled );
|
|
|
|
|
|
|
|
if( !lateSwapsEnabled )
|
|
|
|
{
|
|
|
|
aVal = 0;
|
|
|
|
}
|
|
|
|
}
|
2021-08-13 21:11:41 +00:00
|
|
|
}
|
2020-06-26 17:39:43 +00:00
|
|
|
|
2022-06-07 19:33:28 +00:00
|
|
|
unsigned clampedInterval;
|
|
|
|
glXSwapIntervalEXT( dpy, drawable, aVal );
|
|
|
|
glXQueryDrawable( dpy, drawable, GLX_SWAP_INTERVAL_EXT, &clampedInterval );
|
2020-06-26 17:39:43 +00:00
|
|
|
|
2022-06-07 19:33:28 +00:00
|
|
|
return clampedInterval;
|
|
|
|
}
|
2020-06-26 17:39:43 +00:00
|
|
|
|
2022-06-07 19:33:28 +00:00
|
|
|
if( glXSwapIntervalMESA && glXGetSwapIntervalMESA
|
|
|
|
&& exts.find( "GLX_MESA_swap_control" ) != std::string::npos )
|
|
|
|
{
|
2022-11-17 16:32:34 +00:00
|
|
|
if( aVal == -1 )
|
|
|
|
aVal = 1;
|
2020-06-26 17:39:43 +00:00
|
|
|
|
2022-11-17 16:32:34 +00:00
|
|
|
if( !glXSwapIntervalMESA( aVal ) )
|
|
|
|
return aVal;
|
2022-06-07 19:33:28 +00:00
|
|
|
}
|
2020-06-26 17:39:43 +00:00
|
|
|
|
2022-11-17 16:32:34 +00:00
|
|
|
if( glXSwapIntervalSGI && exts.find( "GLX_SGI_swap_control" ) != std::string::npos )
|
2022-06-07 19:33:28 +00:00
|
|
|
{
|
2022-11-17 16:32:34 +00:00
|
|
|
if( aVal == -1 )
|
|
|
|
aVal = 1;
|
2020-06-26 17:39:43 +00:00
|
|
|
|
2022-11-17 16:32:34 +00:00
|
|
|
if( !glXSwapIntervalSGI( aVal ) )
|
|
|
|
return aVal;
|
2022-06-07 19:33:28 +00:00
|
|
|
}
|
2020-06-26 17:39:43 +00:00
|
|
|
}
|
|
|
|
|
2022-11-17 16:29:11 +00:00
|
|
|
#elif defined( _WIN32 )
|
|
|
|
|
2022-12-03 22:11:17 +00:00
|
|
|
const GLubyte* vendor = glGetString( GL_VENDOR );
|
2022-12-04 07:35:28 +00:00
|
|
|
//const GLubyte* renderer = glGetString( GL_RENDERER );
|
2022-12-03 22:11:17 +00:00
|
|
|
const GLubyte* version = glGetString( GL_VERSION );
|
|
|
|
|
2022-11-17 16:29:11 +00:00
|
|
|
if( wglSwapIntervalEXT && wxGLCanvas::IsExtensionSupported( "WGL_EXT_swap_control" ) )
|
|
|
|
{
|
2022-12-03 22:11:17 +00:00
|
|
|
wxString vendorStr = vendor;
|
|
|
|
wxString versionStr = version;
|
|
|
|
|
|
|
|
if( aVal == -1 && ( !wxGLCanvas::IsExtensionSupported( "WGL_EXT_swap_control_tear" ) ) )
|
2022-11-17 16:29:11 +00:00
|
|
|
aVal = 1;
|
|
|
|
|
2022-12-03 22:11:17 +00:00
|
|
|
// Trying to enable adaptive swapping on AMD drivers from 2017 or older leads to crash
|
|
|
|
if( aVal == -1 && vendorStr == wxS( "ATI Technologies Inc." ) )
|
|
|
|
{
|
2022-12-04 07:35:28 +00:00
|
|
|
wxArrayString parts = wxSplit( versionStr.AfterLast( ' ' ), '.', 0 );
|
2022-12-03 22:11:17 +00:00
|
|
|
|
|
|
|
if( parts.size() == 4 )
|
|
|
|
{
|
2022-12-04 07:35:28 +00:00
|
|
|
long majorVer = 0;
|
2022-12-03 22:11:17 +00:00
|
|
|
|
2022-12-04 07:35:28 +00:00
|
|
|
if( parts[0].ToLong( &majorVer ) )
|
2022-12-03 22:11:17 +00:00
|
|
|
{
|
|
|
|
if( majorVer <= 22 )
|
|
|
|
aVal = 1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2022-12-02 21:06:27 +00:00
|
|
|
|
|
|
|
HDC hdc = wglGetCurrentDC();
|
|
|
|
HGLRC hglrc = wglGetCurrentContext();
|
|
|
|
|
|
|
|
if( hdc && hglrc )
|
|
|
|
{
|
|
|
|
int currentInterval = wglGetSwapIntervalEXT();
|
2022-11-18 23:33:19 +00:00
|
|
|
|
2022-12-02 21:06:27 +00:00
|
|
|
if( currentInterval != aVal )
|
|
|
|
{
|
|
|
|
wglSwapIntervalEXT( aVal );
|
|
|
|
currentInterval = wglGetSwapIntervalEXT();
|
|
|
|
}
|
|
|
|
|
|
|
|
return currentInterval;
|
|
|
|
}
|
2022-11-17 16:29:11 +00:00
|
|
|
}
|
|
|
|
|
2020-12-19 16:00:52 +00:00
|
|
|
#endif
|
2022-11-17 16:29:11 +00:00
|
|
|
return 0;
|
2020-06-26 17:39:43 +00:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2022-12-02 21:06:27 +00:00
|
|
|
#ifdef _WIN32
|
|
|
|
#pragma optimize( "", on )
|
|
|
|
#endif
|
|
|
|
|
2020-06-26 17:39:43 +00:00
|
|
|
#endif /* GL_CONTEXT_MANAGER_H */
|