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 (C) 2020-2022 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* renderer = glGetString( GL_RENDERER );
116 const GLubyte* version = glGetString( GL_VERSION );
117
118 if( wglSwapIntervalEXT && wxGLCanvas::IsExtensionSupported( "WGL_EXT_swap_control" ) )
119 {
120 wxString vendorStr = vendor;
121 wxString versionStr = version;
122
123 if( aVal == -1 && ( !wxGLCanvas::IsExtensionSupported( "WGL_EXT_swap_control_tear" ) ) )
124 aVal = 1;
125
126 // Trying to enable adaptive swapping on AMD drivers from 2017 or older leads to crash
127 if( aVal == -1 && vendorStr == wxS( "ATI Technologies Inc." ) )
128 {
129 wxArrayString parts = wxSplit( versionStr.AfterLast( ' ' ), '.', 0 );
130
131 if( parts.size() == 4 )
132 {
133 long majorVer = 0;
134
135 if( parts[0].ToLong( &majorVer ) )
136 {
137 if( majorVer <= 22 )
138 aVal = 1;
139 }
140 }
141 }
142
143 HDC hdc = wglGetCurrentDC();
144 HGLRC hglrc = wglGetCurrentContext();
145
146 if( hdc && hglrc )
147 {
148 int currentInterval = wglGetSwapIntervalEXT();
149
150 if( currentInterval != aVal )
151 {
152 wglSwapIntervalEXT( aVal );
153 currentInterval = wglGetSwapIntervalEXT();
154 }
155
156 return currentInterval;
157 }
158 }
159
160#endif
161 return 0;
162 }
163};
164
165#ifdef _WIN32
166 #ifdef __MINGW32__
167 #pragma GCC pop_options
168 #else
169 #pragma optimize( "", on )
170 #endif
171#endif
172
173#endif /* GL_CONTEXT_MANAGER_H */
static int SetSwapInterval(int aVal)
Attempts to set the OpenGL swap interval.
Definition: gl_utils.h:49