KiCad PCB EDA Suite
Loading...
Searching...
No Matches
gl_utils.cpp
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#include <wx/platform.h>
25#include <wx/version.h>
26
27#if ( defined( __unix__ ) and not defined( __APPLE__ ) )
28 #if wxHAS_EGL or wxHAS_GLX
29 #if wxHAS_EGL
30 #include <glad/egl.h>
31 #endif
32 #if wxHAS_GLX
33 #include <glad/glx.h>
34 #endif
35 #else
36 #if wxUSE_GLCANVAS_EGL
37 #include <glad/egl.h>
38 #else
39 #include <glad/glx.h>
40 #endif
41 #endif
42#elif defined( _WIN32 )
43 #include <glad/wgl.h>
44#endif
45
46#include <kicad_gl/gl_utils.h>
47#include <kicad_gl/kiglad.h>
48
49#include <wx/glcanvas.h>
50#include <wx/utils.h>
51#include <string>
52
53
54wxString GL_UTILS::DetectGLBackend( wxGLCanvas* aCanvas )
55{
56 wxString backend;
57
58#ifdef __WXGTK__
59 #if wxCHECK_VERSION( 3, 3, 2 )
60 int eglMajor = 0, eglMinor = 0;
61
62 if( aCanvas->GetEGLVersion( &eglMajor, &eglMinor ) )
63 backend = wxString::Format( "EGL %d.%d", eglMajor, eglMinor );
64 else if( int glxVersion = aCanvas->GetGLXVersion() )
65 backend = wxString::Format( "GLX %d.%d", glxVersion / 10, glxVersion % 10 );
66
67 #else // !wxCHECK_VERSION( 3, 3, 2 )
68 #if wxUSE_GLCANVAS_EGL
69 backend = "EGL";
70 #else
71 if( int glxVersion = aCanvas->GetGLXVersion() )
72 backend = wxString::Format( "GLX %d.%d", glxVersion / 10, glxVersion % 10 );
73 #endif
74
75 #endif // !wxCHECK_VERSION( 3, 3, 2 )
76#endif // __WXGTK__
77
78 return backend;
79}
80
81#if !wxCHECK_VERSION( 3, 3, 3 )
82int GL_UTILS::SetSwapInterval( wxGLCanvas* aCanvas, int aVal )
83{
84#if defined( GLAD_GLX )
85 // Check that wx is really using GLX
86 if( !wxGLCanvas::GetGLXVersion() )
87 return 0;
88
89 if( Display* dpy = wxGetX11Display() )
90 {
91 if( !gladLoaderLoadGLX( dpy, DefaultScreen( dpy ) ) )
92 return 0;
93
94 XID drawable = aCanvas->GetXWindow();
95
96 if( glXSwapIntervalEXT && glXQueryDrawable && drawable )
97 {
98 if( aVal == -1 && !GLAD_GLX_EXT_swap_control_tear )
99 aVal = 1; // Late swaps not available
100
101 unsigned clampedInterval;
102 glXSwapIntervalEXT( dpy, drawable, aVal );
103 glXQueryDrawable( dpy, drawable, GLX_SWAP_INTERVAL_EXT, &clampedInterval );
104
105 if( aVal == -1 )
106 {
107 unsigned lateSwapsEnabled = 0;
108 glXQueryDrawable( dpy, drawable, GLX_LATE_SWAPS_TEAR_EXT, &lateSwapsEnabled );
109
110 if( lateSwapsEnabled )
111 clampedInterval = -1;
112 }
113
114 return clampedInterval;
115 }
116
117 if( glXSwapIntervalMESA && glXGetSwapIntervalMESA )
118 {
119 if( aVal == -1 )
120 aVal = 1;
121
122 if( !glXSwapIntervalMESA( aVal ) )
123 return aVal;
124 }
125
126 if( glXSwapIntervalSGI )
127 {
128 if( aVal == -1 )
129 aVal = 1;
130
131 if( !glXSwapIntervalSGI( aVal ) )
132 return aVal;
133 }
134 }
135
136#elif defined( GLAD_WGL ) && defined( GLAD_GL )
137
138 if( !gladLoaderLoadWGL( aCanvas->GetHDC() ) )
139 return 0;
140
141 if( !gladLoaderLoadGL() )
142 return 0;
143
144 const GLubyte* vendor = glGetString( GL_VENDOR );
145 const GLubyte* version = glGetString( GL_VERSION );
146
147 if( wglSwapIntervalEXT )
148 {
149 wxString vendorStr, versionStr;
150
151 if( vendor )
152 vendorStr = wxString( reinterpret_cast<const char*>( vendor ) );
153
154 if( version )
155 versionStr = wxString( reinterpret_cast<const char*>( version ) );
156
157 if( aVal == -1 && ( !wxGLCanvas::IsExtensionSupported( "WGL_EXT_swap_control_tear" ) ) )
158 aVal = 1;
159
160 // Trying to enable adaptive swapping on AMD drivers from 2017 or older leads to crash
161 if( aVal == -1 && vendorStr == wxS( "ATI Technologies Inc." ) )
162 {
163 wxArrayString parts = wxSplit( versionStr.AfterLast( ' ' ), '.', 0 );
164
165 if( parts.size() == 4 )
166 {
167 long majorVer = 0;
168
169 if( parts[0].ToLong( &majorVer ) )
170 {
171 if( majorVer <= 22 )
172 aVal = 1;
173 }
174 }
175 }
176
177 HDC hdc = wglGetCurrentDC();
178 HGLRC hglrc = wglGetCurrentContext();
179
180 if( hdc && hglrc )
181 {
182 int currentInterval = wglGetSwapIntervalEXT();
183
184 if( currentInterval != aVal )
185 {
186 wglSwapIntervalEXT( aVal );
187 currentInterval = wglGetSwapIntervalEXT();
188 }
189
190 return currentInterval;
191 }
192 }
193
194#endif
195 return 0;
196}
197#endif /* !wxCHECK_VERSION( 3, 3, 3 ) */
static int SetSwapInterval(wxGLCanvas *aCanvas, int aVal)
Attempt to set the OpenGL swap interval.
Definition gl_utils.cpp:82
static wxString DetectGLBackend(wxGLCanvas *aCanvas)
Definition gl_utils.cpp:54