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, see <https://www.gnu.org/licenses/>.
18 */
19
20#include <wx/platform.h>
21#include <wx/version.h>
22
23#if ( defined( __unix__ ) and not defined( __APPLE__ ) )
24 #if wxHAS_EGL or wxHAS_GLX
25 #if wxHAS_EGL
26 #include <glad/egl.h>
27 #endif
28 #if wxHAS_GLX
29 #include <glad/glx.h>
30 #endif
31 #else
32 #if wxUSE_GLCANVAS_EGL
33 #include <glad/egl.h>
34 #else
35 #include <glad/glx.h>
36 #endif
37 #endif
38#elif defined( _WIN32 )
39 #include <glad/wgl.h>
40#endif
41
42#include <kicad_gl/gl_utils.h>
43#include <kicad_gl/kiglad.h>
44
45#include <wx/glcanvas.h>
46#include <wx/utils.h>
47#include <string>
48
49
50wxString GL_UTILS::DetectGLBackend( wxGLCanvas* aCanvas )
51{
52 wxString backend;
53
54#ifdef __WXGTK__
55 #if wxCHECK_VERSION( 3, 3, 2 )
56 int eglMajor = 0, eglMinor = 0;
57
58 if( aCanvas->GetEGLVersion( &eglMajor, &eglMinor ) )
59 backend = wxString::Format( "EGL %d.%d", eglMajor, eglMinor );
60 else if( int glxVersion = aCanvas->GetGLXVersion() )
61 backend = wxString::Format( "GLX %d.%d", glxVersion / 10, glxVersion % 10 );
62
63 #else // !wxCHECK_VERSION( 3, 3, 2 )
64 #if wxUSE_GLCANVAS_EGL
65 backend = "EGL";
66 #else
67 if( int glxVersion = aCanvas->GetGLXVersion() )
68 backend = wxString::Format( "GLX %d.%d", glxVersion / 10, glxVersion % 10 );
69 #endif
70
71 #endif // !wxCHECK_VERSION( 3, 3, 2 )
72#endif // __WXGTK__
73
74 return backend;
75}
76
77#if !wxCHECK_VERSION( 3, 3, 3 )
78int GL_UTILS::SetSwapInterval( wxGLCanvas* aCanvas, int aVal )
79{
80#if defined( __WXGTK__ ) && wxCHECK_VERSION( 3, 3, 2 )
81 // On Wayland the canvas uses EGL, where wxGLCanvasEGL::SwapBuffers() forces the swap interval
82 // back to 0 on the first buffer swap so eglSwapBuffers() cannot block. A raw eglSwapInterval()
83 // poke here would be reverted before the first frame, so we go through wx, which stashes the
84 // request and applies it in place of its own reset.
85 //
86 // wx defaults to EGL on X11 too, but the reset exists there because eglSwapBuffers() blocks for
87 // up to a second on an occluded window with a non-zero interval. Only Wayland gates drawing on a
88 // frame callback and so is safe to leave throttled by vsync, so restrict this to Wayland and let
89 // X11 fall through to the self-throttling return 0.
90 if( wxGetEnv( wxT( "WAYLAND_DISPLAY" ), nullptr ) && aCanvas->GetEGLVersion( nullptr, nullptr ) )
91 {
92 switch( aCanvas->SetSwapInterval( aVal ) )
93 {
94 case wxGLCanvas::SwapInterval::Set:
95 // A negative request enables adaptive vsync, which effectively runs at interval 1.
96 return aVal < 0 ? 1 : aVal;
97
98 case wxGLCanvas::SwapInterval::NonAdaptive:
99 return 1;
100
101 case wxGLCanvas::SwapInterval::NotSet:
102 return 0;
103 }
104
105 return 0;
106 }
107#endif
108
109#if defined( GLAD_GLX )
110 // Check that wx is really using GLX
111 if( !wxGLCanvas::GetGLXVersion() )
112 return 0;
113
114 if( Display* dpy = wxGetX11Display() )
115 {
116 if( !gladLoaderLoadGLX( dpy, DefaultScreen( dpy ) ) )
117 return 0;
118
119 XID drawable = aCanvas->GetXWindow();
120
121 if( glXSwapIntervalEXT && glXQueryDrawable && drawable )
122 {
123 if( aVal == -1 && !GLAD_GLX_EXT_swap_control_tear )
124 aVal = 1; // Late swaps not available
125
126 unsigned clampedInterval;
127 glXSwapIntervalEXT( dpy, drawable, aVal );
128 glXQueryDrawable( dpy, drawable, GLX_SWAP_INTERVAL_EXT, &clampedInterval );
129
130 if( aVal == -1 )
131 {
132 unsigned lateSwapsEnabled = 0;
133 glXQueryDrawable( dpy, drawable, GLX_LATE_SWAPS_TEAR_EXT, &lateSwapsEnabled );
134
135 if( lateSwapsEnabled )
136 clampedInterval = -1;
137 }
138
139 return clampedInterval;
140 }
141
142 if( glXSwapIntervalMESA && glXGetSwapIntervalMESA )
143 {
144 if( aVal == -1 )
145 aVal = 1;
146
147 if( !glXSwapIntervalMESA( aVal ) )
148 return aVal;
149 }
150
151 if( glXSwapIntervalSGI )
152 {
153 if( aVal == -1 )
154 aVal = 1;
155
156 if( !glXSwapIntervalSGI( aVal ) )
157 return aVal;
158 }
159 }
160
161#elif defined( GLAD_WGL ) && defined( GLAD_GL )
162
163 if( !gladLoaderLoadWGL( aCanvas->GetHDC() ) )
164 return 0;
165
166 if( !gladLoaderLoadGL() )
167 return 0;
168
169 const GLubyte* vendor = glGetString( GL_VENDOR );
170 const GLubyte* version = glGetString( GL_VERSION );
171
172 if( wglSwapIntervalEXT )
173 {
174 wxString vendorStr, versionStr;
175
176 if( vendor )
177 vendorStr = wxString( reinterpret_cast<const char*>( vendor ) );
178
179 if( version )
180 versionStr = wxString( reinterpret_cast<const char*>( version ) );
181
182 if( aVal == -1 && ( !wxGLCanvas::IsExtensionSupported( "WGL_EXT_swap_control_tear" ) ) )
183 aVal = 1;
184
185 // Trying to enable adaptive swapping on AMD drivers from 2017 or older leads to crash
186 if( aVal == -1 && vendorStr == wxS( "ATI Technologies Inc." ) )
187 {
188 wxArrayString parts = wxSplit( versionStr.AfterLast( ' ' ), '.', 0 );
189
190 if( parts.size() == 4 )
191 {
192 long majorVer = 0;
193
194 if( parts[0].ToLong( &majorVer ) )
195 {
196 if( majorVer <= 22 )
197 aVal = 1;
198 }
199 }
200 }
201
202 HDC hdc = wglGetCurrentDC();
203 HGLRC hglrc = wglGetCurrentContext();
204
205 if( hdc && hglrc )
206 {
207 int currentInterval = wglGetSwapIntervalEXT();
208
209 if( currentInterval != aVal )
210 {
211 wglSwapIntervalEXT( aVal );
212 currentInterval = wglGetSwapIntervalEXT();
213 }
214
215 return currentInterval;
216 }
217 }
218
219#endif
220 return 0;
221}
222#endif /* !wxCHECK_VERSION( 3, 3, 3 ) */
static int SetSwapInterval(wxGLCanvas *aCanvas, int aVal)
Attempt to set the OpenGL swap interval.
Definition gl_utils.cpp:78
static wxString DetectGLBackend(wxGLCanvas *aCanvas)
Definition gl_utils.cpp:50