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( GLAD_GLX )
81 // Check that wx is really using GLX
82 if( !wxGLCanvas::GetGLXVersion() )
83 return 0;
84
85 if( Display* dpy = wxGetX11Display() )
86 {
87 if( !gladLoaderLoadGLX( dpy, DefaultScreen( dpy ) ) )
88 return 0;
89
90 XID drawable = aCanvas->GetXWindow();
91
92 if( glXSwapIntervalEXT && glXQueryDrawable && drawable )
93 {
94 if( aVal == -1 && !GLAD_GLX_EXT_swap_control_tear )
95 aVal = 1; // Late swaps not available
96
97 unsigned clampedInterval;
98 glXSwapIntervalEXT( dpy, drawable, aVal );
99 glXQueryDrawable( dpy, drawable, GLX_SWAP_INTERVAL_EXT, &clampedInterval );
100
101 if( aVal == -1 )
102 {
103 unsigned lateSwapsEnabled = 0;
104 glXQueryDrawable( dpy, drawable, GLX_LATE_SWAPS_TEAR_EXT, &lateSwapsEnabled );
105
106 if( lateSwapsEnabled )
107 clampedInterval = -1;
108 }
109
110 return clampedInterval;
111 }
112
113 if( glXSwapIntervalMESA && glXGetSwapIntervalMESA )
114 {
115 if( aVal == -1 )
116 aVal = 1;
117
118 if( !glXSwapIntervalMESA( aVal ) )
119 return aVal;
120 }
121
122 if( glXSwapIntervalSGI )
123 {
124 if( aVal == -1 )
125 aVal = 1;
126
127 if( !glXSwapIntervalSGI( aVal ) )
128 return aVal;
129 }
130 }
131
132#elif defined( GLAD_WGL ) && defined( GLAD_GL )
133
134 if( !gladLoaderLoadWGL( aCanvas->GetHDC() ) )
135 return 0;
136
137 if( !gladLoaderLoadGL() )
138 return 0;
139
140 const GLubyte* vendor = glGetString( GL_VENDOR );
141 const GLubyte* version = glGetString( GL_VERSION );
142
143 if( wglSwapIntervalEXT )
144 {
145 wxString vendorStr, versionStr;
146
147 if( vendor )
148 vendorStr = wxString( reinterpret_cast<const char*>( vendor ) );
149
150 if( version )
151 versionStr = wxString( reinterpret_cast<const char*>( version ) );
152
153 if( aVal == -1 && ( !wxGLCanvas::IsExtensionSupported( "WGL_EXT_swap_control_tear" ) ) )
154 aVal = 1;
155
156 // Trying to enable adaptive swapping on AMD drivers from 2017 or older leads to crash
157 if( aVal == -1 && vendorStr == wxS( "ATI Technologies Inc." ) )
158 {
159 wxArrayString parts = wxSplit( versionStr.AfterLast( ' ' ), '.', 0 );
160
161 if( parts.size() == 4 )
162 {
163 long majorVer = 0;
164
165 if( parts[0].ToLong( &majorVer ) )
166 {
167 if( majorVer <= 22 )
168 aVal = 1;
169 }
170 }
171 }
172
173 HDC hdc = wglGetCurrentDC();
174 HGLRC hglrc = wglGetCurrentContext();
175
176 if( hdc && hglrc )
177 {
178 int currentInterval = wglGetSwapIntervalEXT();
179
180 if( currentInterval != aVal )
181 {
182 wglSwapIntervalEXT( aVal );
183 currentInterval = wglGetSwapIntervalEXT();
184 }
185
186 return currentInterval;
187 }
188 }
189
190#endif
191 return 0;
192}
193#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