KiCad PCB EDA Suite
Loading...
Searching...
No Matches
gl_utils.h
Go to the documentation of this file.
1/*
2 * This program source code file is part of KiCad, a free EDA CAD application.
3 *
4 * Copyright The KiCad Developers, see AUTHORS.txt for contributors
5 *
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU General Public License
8 * as published by the Free Software Foundation; either version 2
9 * of the License, or (at your option) any later version.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, you may find one here:
18 * http://www.gnu.org/licenses/old-licenses/gpl-3.0.html
19 * or you may search the http://www.gnu.org website for the version 3 license,
20 * or you may write to the Free Software Foundation, Inc.,
21 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
22 */
23
24#ifndef GL_UTILS_H
25#define GL_UTILS_H
26
27#include <gal/opengl/kiglew.h> // Must be included first
28#include <wx/glcanvas.h>
29#include <wx/utils.h>
30
31#ifdef _WIN32
32 #ifdef __MINGW32__
33 #pragma GCC push_options
34 #pragma GCC optimize( "O0" )
35 #else
36 #pragma optimize( "", off )
37 #endif
38#endif
39
41{
42public:
49 static int SetSwapInterval( int aVal )
50 {
51#if defined( __linux__ ) && !defined( KICAD_USE_EGL )
52
53 if( Display* dpy = glXGetCurrentDisplay() )
54 {
55 GLXDrawable drawable = glXGetCurrentDrawable();
56
57 std::string exts( glXQueryExtensionsString( dpy, DefaultScreen( dpy ) ) );
58
59 if( glXSwapIntervalEXT && glXQueryDrawable && drawable
60 && exts.find( "GLX_EXT_swap_control" ) != std::string::npos )
61 {
62 if( aVal == -1 )
63 {
64 if( exts.find( "GLX_EXT_swap_control_tear" ) == std::string::npos )
65 {
66 aVal = 1;
67 }
68 else
69 {
70 // Even though the extensions might be available,
71 // we need to be sure that late/adaptive swaps are
72 // enabled on the drawable.
73
74 unsigned lateSwapsEnabled = 0;
75 glXQueryDrawable( dpy, drawable, GLX_LATE_SWAPS_TEAR_EXT,
76 &lateSwapsEnabled );
77
78 if( !lateSwapsEnabled )
79 {
80 aVal = 0;
81 }
82 }
83 }
84
85 unsigned clampedInterval;
86 glXSwapIntervalEXT( dpy, drawable, aVal );
87 glXQueryDrawable( dpy, drawable, GLX_SWAP_INTERVAL_EXT, &clampedInterval );
88
89 return clampedInterval;
90 }
91
92 if( glXSwapIntervalMESA && glXGetSwapIntervalMESA
93 && exts.find( "GLX_MESA_swap_control" ) != std::string::npos )
94 {
95 if( aVal == -1 )
96 aVal = 1;
97
98 if( !glXSwapIntervalMESA( aVal ) )
99 return aVal;
100 }
101
102 if( glXSwapIntervalSGI && exts.find( "GLX_SGI_swap_control" ) != std::string::npos )
103 {
104 if( aVal == -1 )
105 aVal = 1;
106
107 if( !glXSwapIntervalSGI( aVal ) )
108 return aVal;
109 }
110 }
111
112#elif defined( _WIN32 )
113
114 const GLubyte* vendor = glGetString( GL_VENDOR );
115 const GLubyte* version = glGetString( GL_VERSION );
116
117 if( wglSwapIntervalEXT && wxGLCanvas::IsExtensionSupported( "WGL_EXT_swap_control" ) )
118 {
119 wxString vendorStr = vendor;
120 wxString versionStr = version;
121
122 if( aVal == -1 && ( !wxGLCanvas::IsExtensionSupported( "WGL_EXT_swap_control_tear" ) ) )
123 aVal = 1;
124
125 // Trying to enable adaptive swapping on AMD drivers from 2017 or older leads to crash
126 if( aVal == -1 && vendorStr == wxS( "ATI Technologies Inc." ) )
127 {
128 wxArrayString parts = wxSplit( versionStr.AfterLast( ' ' ), '.', 0 );
129
130 if( parts.size() == 4 )
131 {
132 long majorVer = 0;
133
134 if( parts[0].ToLong( &majorVer ) )
135 {
136 if( majorVer <= 22 )
137 aVal = 1;
138 }
139 }
140 }
141
142 HDC hdc = wglGetCurrentDC();
143 HGLRC hglrc = wglGetCurrentContext();
144
145 if( hdc && hglrc )
146 {
147 int currentInterval = wglGetSwapIntervalEXT();
148
149 if( currentInterval != aVal )
150 {
151 wglSwapIntervalEXT( aVal );
152 currentInterval = wglGetSwapIntervalEXT();
153 }
154
155 return currentInterval;
156 }
157 }
158
159#endif
160 return 0;
161 }
162};
163
164#ifdef _WIN32
165 #ifdef __MINGW32__
166 #pragma GCC pop_options
167 #else
168 #pragma optimize( "", on )
169 #endif
170#endif
171
172#endif /* GL_CONTEXT_MANAGER_H */
static int SetSwapInterval(int aVal)
Attempt to set the OpenGL swap interval.
Definition: gl_utils.h:49