Commit | Line | Data |
---|---|---|
b311480e | 1 | // Created on: 2011-09-20 |
2 | // Created by: Sergey ZERCHANINOV | |
973c2be1 | 3 | // Copyright (c) 2011-2014 OPEN CASCADE SAS |
b311480e | 4 | // |
973c2be1 | 5 | // This file is part of Open CASCADE Technology software library. |
b311480e | 6 | // |
d5f74e42 | 7 | // This library is free software; you can redistribute it and/or modify it under |
8 | // the terms of the GNU Lesser General Public License version 2.1 as published | |
973c2be1 | 9 | // by the Free Software Foundation, with special exception defined in the file |
10 | // OCCT_LGPL_EXCEPTION.txt. Consult the file LICENSE_LGPL_21.txt included in OCCT | |
11 | // distribution for complete text of the license and disclaimer of any warranty. | |
b311480e | 12 | // |
973c2be1 | 13 | // Alternatively, this file may be used under the terms of Open CASCADE |
14 | // commercial license or contractual agreement. | |
b311480e | 15 | |
b5ac8292 | 16 | #include <OpenGl_GlCore12.hxx> |
2166f0fa | 17 | |
2166f0fa | 18 | #include <OpenGl_Context.hxx> |
25b97fac | 19 | #include <OpenGl_GraphicDriver.hxx> |
c827ea3a | 20 | #include <OpenGl_Window.hxx> |
2166f0fa SK |
21 | |
22 | #include <Aspect_GraphicDeviceDefinitionError.hxx> | |
825aa485 | 23 | #include <Graphic3d_TransformUtils.hxx> |
2166f0fa | 24 | #include <TCollection_AsciiString.hxx> |
b5ac8292 | 25 | #include <TCollection_ExtendedString.hxx> |
c04c30b3 | 26 | #include <Graphic3d_GraphicDriver.hxx> |
2166f0fa | 27 | |
25e59720 | 28 | IMPLEMENT_STANDARD_RTTIEXT(OpenGl_Window,Standard_Transient) |
92efcf78 | 29 | |
1ce0716b | 30 | #if defined(HAVE_EGL) |
25b97fac | 31 | #include <EGL/egl.h> |
32 | #endif | |
33 | ||
2166f0fa | 34 | |
4fe56619 | 35 | #if !defined(__APPLE__) || defined(MACOSX_USE_GLX) |
36 | ||
2166f0fa SK |
37 | namespace |
38 | { | |
2166f0fa | 39 | |
1ce0716b | 40 | #if defined(HAVE_EGL) |
25b97fac | 41 | // |
42 | #elif defined(_WIN32) | |
58655684 | 43 | |
abe46077 | 44 | // WGL_ARB_pixel_format |
45 | #ifndef WGL_NUMBER_PIXEL_FORMATS_ARB | |
46 | #define WGL_NUMBER_PIXEL_FORMATS_ARB 0x2000 | |
47 | #define WGL_DRAW_TO_WINDOW_ARB 0x2001 | |
48 | #define WGL_DRAW_TO_BITMAP_ARB 0x2002 | |
49 | #define WGL_ACCELERATION_ARB 0x2003 | |
50 | #define WGL_NEED_PALETTE_ARB 0x2004 | |
51 | #define WGL_NEED_SYSTEM_PALETTE_ARB 0x2005 | |
52 | #define WGL_SWAP_LAYER_BUFFERS_ARB 0x2006 | |
53 | #define WGL_SWAP_METHOD_ARB 0x2007 | |
54 | #define WGL_NUMBER_OVERLAYS_ARB 0x2008 | |
55 | #define WGL_NUMBER_UNDERLAYS_ARB 0x2009 | |
56 | #define WGL_TRANSPARENT_ARB 0x200A | |
57 | #define WGL_TRANSPARENT_RED_VALUE_ARB 0x2037 | |
58 | #define WGL_TRANSPARENT_GREEN_VALUE_ARB 0x2038 | |
59 | #define WGL_TRANSPARENT_BLUE_VALUE_ARB 0x2039 | |
60 | #define WGL_TRANSPARENT_ALPHA_VALUE_ARB 0x203A | |
61 | #define WGL_TRANSPARENT_INDEX_VALUE_ARB 0x203B | |
62 | #define WGL_SHARE_DEPTH_ARB 0x200C | |
63 | #define WGL_SHARE_STENCIL_ARB 0x200D | |
64 | #define WGL_SHARE_ACCUM_ARB 0x200E | |
65 | #define WGL_SUPPORT_GDI_ARB 0x200F | |
66 | #define WGL_SUPPORT_OPENGL_ARB 0x2010 | |
67 | #define WGL_DOUBLE_BUFFER_ARB 0x2011 | |
68 | #define WGL_STEREO_ARB 0x2012 | |
69 | #define WGL_PIXEL_TYPE_ARB 0x2013 | |
70 | #define WGL_COLOR_BITS_ARB 0x2014 | |
71 | #define WGL_RED_BITS_ARB 0x2015 | |
72 | #define WGL_RED_SHIFT_ARB 0x2016 | |
73 | #define WGL_GREEN_BITS_ARB 0x2017 | |
74 | #define WGL_GREEN_SHIFT_ARB 0x2018 | |
75 | #define WGL_BLUE_BITS_ARB 0x2019 | |
76 | #define WGL_BLUE_SHIFT_ARB 0x201A | |
77 | #define WGL_ALPHA_BITS_ARB 0x201B | |
78 | #define WGL_ALPHA_SHIFT_ARB 0x201C | |
79 | #define WGL_ACCUM_BITS_ARB 0x201D | |
80 | #define WGL_ACCUM_RED_BITS_ARB 0x201E | |
81 | #define WGL_ACCUM_GREEN_BITS_ARB 0x201F | |
82 | #define WGL_ACCUM_BLUE_BITS_ARB 0x2020 | |
83 | #define WGL_ACCUM_ALPHA_BITS_ARB 0x2021 | |
84 | #define WGL_DEPTH_BITS_ARB 0x2022 | |
85 | #define WGL_STENCIL_BITS_ARB 0x2023 | |
86 | #define WGL_AUX_BUFFERS_ARB 0x2024 | |
87 | ||
88 | #define WGL_NO_ACCELERATION_ARB 0x2025 | |
89 | #define WGL_GENERIC_ACCELERATION_ARB 0x2026 | |
90 | #define WGL_FULL_ACCELERATION_ARB 0x2027 | |
91 | ||
92 | #define WGL_SWAP_EXCHANGE_ARB 0x2028 | |
93 | #define WGL_SWAP_COPY_ARB 0x2029 | |
94 | #define WGL_SWAP_UNDEFINED_ARB 0x202A | |
95 | ||
96 | #define WGL_TYPE_RGBA_ARB 0x202B | |
97 | #define WGL_TYPE_COLORINDEX_ARB 0x202C | |
98 | #endif // WGL_NUMBER_PIXEL_FORMATS_ARB | |
99 | ||
58655684 | 100 | // WGL_ARB_create_context_profile |
101 | #ifndef WGL_CONTEXT_MAJOR_VERSION_ARB | |
102 | #define WGL_CONTEXT_MAJOR_VERSION_ARB 0x2091 | |
103 | #define WGL_CONTEXT_MINOR_VERSION_ARB 0x2092 | |
104 | #define WGL_CONTEXT_LAYER_PLANE_ARB 0x2093 | |
105 | #define WGL_CONTEXT_FLAGS_ARB 0x2094 | |
106 | #define WGL_CONTEXT_PROFILE_MASK_ARB 0x9126 | |
107 | ||
108 | // WGL_CONTEXT_FLAGS bits | |
109 | #define WGL_CONTEXT_DEBUG_BIT_ARB 0x0001 | |
110 | #define WGL_CONTEXT_FORWARD_COMPATIBLE_BIT_ARB 0x0002 | |
111 | ||
112 | // WGL_CONTEXT_PROFILE_MASK_ARB bits | |
113 | #define WGL_CONTEXT_CORE_PROFILE_BIT_ARB 0x00000001 | |
114 | #define WGL_CONTEXT_COMPATIBILITY_PROFILE_BIT_ARB 0x00000002 | |
115 | #endif // WGL_CONTEXT_MAJOR_VERSION_ARB | |
116 | ||
117 | static LRESULT CALLBACK wndProcDummy (HWND theWin, UINT theMsg, WPARAM theParamW, LPARAM theParamL) | |
118 | { | |
119 | return DefWindowProcW (theWin, theMsg, theParamW, theParamL); | |
120 | } | |
2166f0fa | 121 | #else |
b6bf4ec1 | 122 | |
123 | // GLX_ARB_create_context | |
124 | #ifndef GLX_CONTEXT_MAJOR_VERSION_ARB | |
125 | #define GLX_CONTEXT_DEBUG_BIT_ARB 0x00000001 | |
126 | #define GLX_CONTEXT_FORWARD_COMPATIBLE_BIT_ARB 0x00000002 | |
127 | #define GLX_CONTEXT_MAJOR_VERSION_ARB 0x2091 | |
128 | #define GLX_CONTEXT_MINOR_VERSION_ARB 0x2092 | |
129 | #define GLX_CONTEXT_FLAGS_ARB 0x2094 | |
130 | ||
131 | // GLX_ARB_create_context_profile | |
132 | #define GLX_CONTEXT_CORE_PROFILE_BIT_ARB 0x00000001 | |
133 | #define GLX_CONTEXT_COMPATIBILITY_PROFILE_BIT_ARB 0x00000002 | |
134 | #define GLX_CONTEXT_PROFILE_MASK_ARB 0x9126 | |
135 | #endif | |
136 | ||
137 | //! Dummy XError handler which just skips errors | |
138 | static int xErrorDummyHandler (Display* /*theDisplay*/, | |
139 | XErrorEvent* /*theErrorEvent*/) | |
140 | { | |
141 | return 0; | |
142 | } | |
143 | ||
144 | //! Auxiliary method to format list. | |
145 | static void addMsgToList (TCollection_ExtendedString& theList, | |
146 | const TCollection_ExtendedString& theMsg) | |
2166f0fa | 147 | { |
b6bf4ec1 | 148 | if (!theList.IsEmpty()) |
149 | { | |
150 | theList += ", "; | |
151 | } | |
152 | theList += theMsg; | |
2166f0fa SK |
153 | } |
154 | #endif | |
155 | ||
5640d653 | 156 | } |
2166f0fa SK |
157 | |
158 | // ======================================================================= | |
159 | // function : OpenGl_Window | |
160 | // purpose : | |
161 | // ======================================================================= | |
25b97fac | 162 | OpenGl_Window::OpenGl_Window (const Handle(OpenGl_GraphicDriver)& theDriver, |
c357e426 | 163 | const Handle(Aspect_Window)& thePlatformWindow, |
5e27df78 | 164 | Aspect_RenderingContext theGContext, |
58655684 | 165 | const Handle(OpenGl_Caps)& theCaps, |
5e27df78 | 166 | const Handle(OpenGl_Context)& theShareCtx) |
73192b37 | 167 | : myGlContext (new OpenGl_Context (theCaps)), |
2166f0fa | 168 | myOwnGContext (theGContext == 0), |
c357e426 | 169 | myPlatformWindow (thePlatformWindow), |
f978241f | 170 | mySwapInterval (theCaps->swapInterval) |
2166f0fa | 171 | { |
c357e426 | 172 | myPlatformWindow->Size (myWidth, myHeight); |
2166f0fa | 173 | |
4e1523ef | 174 | Standard_Boolean isCoreProfile = Standard_False; |
175 | ||
1ce0716b | 176 | #if defined(HAVE_EGL) |
25b97fac | 177 | EGLDisplay anEglDisplay = (EGLDisplay )theDriver->getRawGlDisplay(); |
178 | EGLContext anEglContext = (EGLContext )theDriver->getRawGlContext(); | |
179 | EGLConfig anEglConfig = (EGLConfig )theDriver->getRawGlConfig(); | |
180 | if (anEglDisplay == EGL_NO_DISPLAY | |
181 | || anEglContext == EGL_NO_CONTEXT | |
182 | || anEglConfig == NULL) | |
183 | { | |
9775fa61 | 184 | throw Aspect_GraphicDeviceDefinitionError("OpenGl_Window, EGL does not provide compatible configurations!"); |
25b97fac | 185 | return; |
186 | } | |
187 | ||
188 | EGLSurface anEglSurf = EGL_NO_SURFACE; | |
189 | if (theGContext == (EGLContext )EGL_NO_CONTEXT) | |
190 | { | |
191 | // create new surface | |
c357e426 | 192 | anEglSurf = eglCreateWindowSurface (anEglDisplay, |
193 | anEglConfig, | |
194 | (EGLNativeWindowType )myPlatformWindow->NativeHandle(), | |
195 | NULL); | |
25b97fac | 196 | if (anEglSurf == EGL_NO_SURFACE) |
197 | { | |
9775fa61 | 198 | throw Aspect_GraphicDeviceDefinitionError("OpenGl_Window, EGL is unable to create surface for window!"); |
25b97fac | 199 | return; |
200 | } | |
201 | } | |
202 | else if (theGContext != anEglContext) | |
203 | { | |
9775fa61 | 204 | throw Aspect_GraphicDeviceDefinitionError("OpenGl_Window, EGL is used in unsupported combination!"); |
25b97fac | 205 | return; |
206 | } | |
207 | else | |
208 | { | |
209 | anEglSurf = eglGetCurrentSurface(EGL_DRAW); | |
210 | if (anEglSurf == EGL_NO_SURFACE) | |
211 | { | |
9775fa61 | 212 | throw Aspect_GraphicDeviceDefinitionError("OpenGl_Window, EGL is unable to retrieve current surface!"); |
25b97fac | 213 | return; |
214 | } | |
215 | } | |
216 | ||
4e1523ef | 217 | myGlContext->Init ((Aspect_Drawable )anEglSurf, (Aspect_Display )anEglDisplay, (Aspect_RenderingContext )anEglContext, isCoreProfile); |
25b97fac | 218 | #elif defined(_WIN32) |
219 | (void )theDriver; | |
c357e426 | 220 | HWND aWindow = (HWND )myPlatformWindow->NativeHandle(); |
5e27df78 | 221 | HDC aWindowDC = GetDC (aWindow); |
222 | HGLRC aGContext = (HGLRC )theGContext; | |
223 | ||
3d8969b1 | 224 | PIXELFORMATDESCRIPTOR aPixelFrmt; |
abe46077 | 225 | memset (&aPixelFrmt, 0, sizeof(aPixelFrmt)); |
226 | aPixelFrmt.nSize = sizeof(PIXELFORMATDESCRIPTOR); | |
227 | aPixelFrmt.nVersion = 1; | |
228 | aPixelFrmt.dwFlags = PFD_DRAW_TO_WINDOW | PFD_SUPPORT_OPENGL | PFD_DOUBLEBUFFER; | |
229 | aPixelFrmt.iPixelType = PFD_TYPE_RGBA; | |
230 | aPixelFrmt.cColorBits = 24; | |
231 | aPixelFrmt.cDepthBits = 24; | |
232 | aPixelFrmt.cStencilBits = 8; | |
233 | aPixelFrmt.iLayerType = PFD_MAIN_PLANE; | |
234 | if (theCaps->contextStereo) | |
235 | { | |
236 | aPixelFrmt.dwFlags |= PFD_STEREO; | |
237 | } | |
238 | ||
239 | int aPixelFrmtId = ChoosePixelFormat (aWindowDC, &aPixelFrmt); | |
b5ac8292 | 240 | |
241 | // in case of failure try without stereo if any | |
4e1523ef | 242 | const Standard_Boolean hasStereo = aPixelFrmtId != 0 && theCaps->contextStereo; |
b5ac8292 | 243 | if (aPixelFrmtId == 0 && theCaps->contextStereo) |
244 | { | |
245 | TCollection_ExtendedString aMsg ("OpenGl_Window::CreateWindow: " | |
246 | "ChoosePixelFormat is unable to find stereo supported pixel format. " | |
247 | "Choosing similar non stereo format."); | |
3b523c4c | 248 | myGlContext->PushMessage (GL_DEBUG_SOURCE_APPLICATION, |
249 | GL_DEBUG_TYPE_OTHER, | |
250 | 0, GL_DEBUG_SEVERITY_HIGH, aMsg); | |
b5ac8292 | 251 | |
252 | aPixelFrmt.dwFlags &= ~PFD_STEREO; | |
253 | aPixelFrmtId = ChoosePixelFormat (aWindowDC, &aPixelFrmt); | |
254 | } | |
255 | ||
3d8969b1 | 256 | if (aPixelFrmtId == 0) |
5e27df78 | 257 | { |
258 | ReleaseDC (aWindow, aWindowDC); | |
259 | ||
260 | TCollection_AsciiString aMsg ("OpenGl_Window::CreateWindow: ChoosePixelFormat failed. Error code: "); | |
261 | aMsg += (int )GetLastError(); | |
9775fa61 | 262 | throw Aspect_GraphicDeviceDefinitionError(aMsg.ToCString()); |
5e27df78 | 263 | return; |
264 | } | |
265 | ||
abe46077 | 266 | DescribePixelFormat (aWindowDC, aPixelFrmtId, sizeof(aPixelFrmt), &aPixelFrmt); |
5e27df78 | 267 | |
58655684 | 268 | HGLRC aSlaveCtx = !theShareCtx.IsNull() ? (HGLRC )theShareCtx->myGContext : NULL; |
5e27df78 | 269 | if (aGContext == NULL) |
270 | { | |
58655684 | 271 | // create temporary context to retrieve advanced context creation procedures |
272 | HMODULE aModule = GetModuleHandleW(NULL); | |
273 | WNDCLASSW aClass; memset (&aClass, 0, sizeof(aClass)); | |
274 | aClass.style = CS_HREDRAW | CS_VREDRAW | CS_OWNDC; | |
275 | aClass.lpfnWndProc = wndProcDummy; | |
276 | aClass.hInstance = aModule; | |
277 | aClass.lpszClassName = L"OpenGl_WindowTmp"; | |
278 | HWND aWinTmp = NULL; | |
279 | HDC aDevCtxTmp = NULL; | |
280 | HGLRC aRendCtxTmp = NULL; | |
4e1523ef | 281 | if ((!theCaps->contextDebug && !theCaps->contextNoAccel && theCaps->contextCompatible) |
58655684 | 282 | || RegisterClassW (&aClass) == 0) |
283 | { | |
284 | aClass.lpszClassName = NULL; | |
285 | } | |
286 | if (aClass.lpszClassName != NULL) | |
287 | { | |
229c0b6a | 288 | DWORD anExStyle = WS_EX_TOOLWINDOW | WS_EX_WINDOWEDGE; |
289 | #if (_WIN32_WINNT >= 0x0500) | |
290 | anExStyle |= WS_EX_NOACTIVATE; | |
291 | #endif | |
292 | aWinTmp = CreateWindowExW(anExStyle, | |
58655684 | 293 | aClass.lpszClassName, L"OpenGl_WindowTmp", |
294 | WS_POPUP | WS_CLIPSIBLINGS | WS_CLIPCHILDREN | WS_DISABLED, | |
295 | 2, 2, 4, 4, | |
296 | NULL, NULL, aModule, NULL); | |
297 | } | |
298 | if (aWinTmp != NULL) | |
299 | { | |
300 | aDevCtxTmp = GetDC (aWinTmp); | |
301 | SetPixelFormat (aDevCtxTmp, aPixelFrmtId, &aPixelFrmt); | |
302 | aRendCtxTmp = wglCreateContext (aDevCtxTmp); | |
303 | } | |
abe46077 | 304 | |
305 | typedef BOOL (WINAPI *wglChoosePixelFormatARB_t)(HDC theDevCtx, | |
306 | const int* theIntAttribs, | |
307 | const float* theFloatAttribs, | |
308 | unsigned int theMaxFormats, | |
309 | int* theFormatsOut, | |
310 | unsigned int* theNumFormatsOut); | |
311 | typedef HGLRC (WINAPI *wglCreateContextAttribsARB_t)(HDC theDevCtx, | |
312 | HGLRC theShareContext, | |
313 | const int* theAttribs); | |
314 | wglChoosePixelFormatARB_t aChoosePixProc = NULL; | |
315 | wglCreateContextAttribsARB_t aCreateCtxProc = NULL; | |
58655684 | 316 | if (aRendCtxTmp != NULL) |
317 | { | |
318 | wglMakeCurrent (aDevCtxTmp, aRendCtxTmp); | |
319 | ||
320 | typedef const char* (WINAPI *wglGetExtensionsStringARB_t)(HDC theDeviceContext); | |
abe46077 | 321 | wglGetExtensionsStringARB_t aGetExtensions = (wglGetExtensionsStringARB_t )wglGetProcAddress ("wglGetExtensionsStringARB"); |
322 | const char* aWglExts = (aGetExtensions != NULL) ? aGetExtensions (wglGetCurrentDC()) : NULL; | |
323 | if (OpenGl_Context::CheckExtension (aWglExts, "WGL_ARB_pixel_format")) | |
324 | { | |
325 | aChoosePixProc = (wglChoosePixelFormatARB_t )wglGetProcAddress ("wglChoosePixelFormatARB"); | |
326 | } | |
327 | if (OpenGl_Context::CheckExtension (aWglExts, "WGL_ARB_create_context_profile")) | |
328 | { | |
329 | aCreateCtxProc = (wglCreateContextAttribsARB_t )wglGetProcAddress ("wglCreateContextAttribsARB"); | |
330 | } | |
331 | } | |
332 | ||
333 | // choose extended pixel format | |
334 | if (aChoosePixProc != NULL) | |
335 | { | |
336 | const int aPixAttribs[] = | |
337 | { | |
338 | WGL_DRAW_TO_WINDOW_ARB, GL_TRUE, | |
339 | WGL_SUPPORT_OPENGL_ARB, GL_TRUE, | |
340 | WGL_DOUBLE_BUFFER_ARB, GL_TRUE, | |
4e1523ef | 341 | WGL_STEREO_ARB, hasStereo ? GL_TRUE : GL_FALSE, |
abe46077 | 342 | WGL_PIXEL_TYPE_ARB, WGL_TYPE_RGBA_ARB, |
343 | //WGL_SAMPLE_BUFFERS_ARB, 1, | |
344 | //WGL_SAMPLES_ARB, 8, | |
345 | WGL_COLOR_BITS_ARB, 24, | |
346 | WGL_DEPTH_BITS_ARB, 24, | |
347 | WGL_STENCIL_BITS_ARB, 8, | |
348 | WGL_ACCELERATION_ARB, theCaps->contextNoAccel ? WGL_NO_ACCELERATION_ARB : WGL_FULL_ACCELERATION_ARB, | |
349 | 0, 0, | |
350 | }; | |
351 | unsigned int aFrmtsNb = 0; | |
352 | aChoosePixProc (aWindowDC, aPixAttribs, NULL, 1, &aPixelFrmtId, &aFrmtsNb); | |
353 | } | |
354 | ||
355 | // setup pixel format - may be set only once per window | |
356 | if (!SetPixelFormat (aWindowDC, aPixelFrmtId, &aPixelFrmt)) | |
357 | { | |
358 | ReleaseDC (aWindow, aWindowDC); | |
359 | ||
360 | TCollection_AsciiString aMsg("OpenGl_Window::CreateWindow: SetPixelFormat failed. Error code: "); | |
361 | aMsg += (int )GetLastError(); | |
9775fa61 | 362 | throw Aspect_GraphicDeviceDefinitionError(aMsg.ToCString()); |
abe46077 | 363 | return; |
364 | } | |
365 | ||
366 | // create GL context with extra options | |
367 | if (aCreateCtxProc != NULL) | |
368 | { | |
4e1523ef | 369 | if (!theCaps->contextCompatible) |
abe46077 | 370 | { |
4e1523ef | 371 | int aCoreCtxAttribs[] = |
372 | { | |
373 | WGL_CONTEXT_MAJOR_VERSION_ARB, 3, | |
374 | WGL_CONTEXT_MINOR_VERSION_ARB, 2, | |
375 | WGL_CONTEXT_PROFILE_MASK_ARB, WGL_CONTEXT_CORE_PROFILE_BIT_ARB, | |
376 | WGL_CONTEXT_FLAGS_ARB, theCaps->contextDebug ? WGL_CONTEXT_DEBUG_BIT_ARB : 0, | |
377 | 0, 0 | |
378 | }; | |
379 | ||
380 | // Try to create the core profile of highest OpenGL version supported by OCCT | |
381 | // (this will be done automatically by some drivers when requesting 3.2, | |
382 | // but some will not (e.g. AMD Catalyst) since WGL_ARB_create_context_profile specification allows both implementations). | |
383 | for (int aLowVer4 = 5; aLowVer4 >= 0 && aGContext == NULL; --aLowVer4) | |
384 | { | |
385 | aCoreCtxAttribs[1] = 4; | |
386 | aCoreCtxAttribs[3] = aLowVer4; | |
387 | aGContext = aCreateCtxProc (aWindowDC, aSlaveCtx, aCoreCtxAttribs); | |
388 | } | |
389 | for (int aLowVer3 = 3; aLowVer3 >= 2 && aGContext == NULL; --aLowVer3) | |
390 | { | |
391 | aCoreCtxAttribs[1] = 3; | |
392 | aCoreCtxAttribs[3] = aLowVer3; | |
393 | aGContext = aCreateCtxProc (aWindowDC, aSlaveCtx, aCoreCtxAttribs); | |
394 | } | |
395 | isCoreProfile = aGContext != NULL; | |
396 | } | |
397 | ||
398 | if (aGContext == NULL) | |
399 | { | |
400 | int aCtxAttribs[] = | |
401 | { | |
402 | // Beware! NVIDIA drivers reject context creation when WGL_CONTEXT_PROFILE_MASK_ARB are specified | |
403 | // but not WGL_CONTEXT_MAJOR_VERSION_ARB/WGL_CONTEXT_MINOR_VERSION_ARB. | |
404 | //WGL_CONTEXT_PROFILE_MASK_ARB, WGL_CONTEXT_COMPATIBILITY_PROFILE_BIT_ARB, | |
405 | WGL_CONTEXT_FLAGS_ARB, theCaps->contextDebug ? WGL_CONTEXT_DEBUG_BIT_ARB : 0, | |
406 | 0, 0 | |
407 | }; | |
408 | isCoreProfile = Standard_False; | |
409 | aGContext = aCreateCtxProc (aWindowDC, aSlaveCtx, aCtxAttribs); | |
410 | ||
411 | if (aGContext != NULL | |
412 | && !theCaps->contextCompatible) | |
413 | { | |
414 | TCollection_ExtendedString aMsg("OpenGl_Window::CreateWindow: core profile creation failed."); | |
3b523c4c | 415 | myGlContext->PushMessage (GL_DEBUG_SOURCE_APPLICATION, GL_DEBUG_TYPE_PORTABILITY, 0, GL_DEBUG_SEVERITY_LOW, aMsg); |
4e1523ef | 416 | } |
417 | } | |
abe46077 | 418 | |
abe46077 | 419 | if (aGContext != NULL) |
58655684 | 420 | { |
abe46077 | 421 | aSlaveCtx = NULL; |
58655684 | 422 | } |
423 | } | |
424 | ||
425 | if (aRendCtxTmp != NULL) | |
426 | { | |
427 | wglDeleteContext (aRendCtxTmp); | |
428 | } | |
429 | if (aDevCtxTmp != NULL) | |
430 | { | |
431 | ReleaseDC (aWinTmp, aDevCtxTmp); | |
432 | } | |
433 | if (aWinTmp != NULL) | |
434 | { | |
435 | DestroyWindow (aWinTmp); | |
436 | } | |
437 | if (aClass.lpszClassName != NULL) | |
438 | { | |
439 | UnregisterClassW (aClass.lpszClassName, aModule); | |
440 | } | |
441 | ||
442 | if (aGContext == NULL) | |
443 | { | |
abe46077 | 444 | // create context using obsolete functionality |
58655684 | 445 | aGContext = wglCreateContext (aWindowDC); |
446 | } | |
5e27df78 | 447 | if (aGContext == NULL) |
448 | { | |
449 | ReleaseDC (aWindow, aWindowDC); | |
450 | ||
451 | TCollection_AsciiString aMsg ("OpenGl_Window::CreateWindow: wglCreateContext failed. Error code: "); | |
452 | aMsg += (int )GetLastError(); | |
9775fa61 | 453 | throw Aspect_GraphicDeviceDefinitionError(aMsg.ToCString()); |
5e27df78 | 454 | return; |
455 | } | |
456 | } | |
457 | ||
458 | // all GL context within one OpenGl_GraphicDriver should be shared! | |
58655684 | 459 | if (aSlaveCtx != NULL && wglShareLists (aSlaveCtx, aGContext) != TRUE) |
5e27df78 | 460 | { |
461 | TCollection_AsciiString aMsg ("OpenGl_Window::CreateWindow: wglShareLists failed. Error code: "); | |
462 | aMsg += (int )GetLastError(); | |
9775fa61 | 463 | throw Aspect_GraphicDeviceDefinitionError(aMsg.ToCString()); |
5e27df78 | 464 | return; |
465 | } | |
466 | ||
4e1523ef | 467 | myGlContext->Init ((Aspect_Handle )aWindow, (Aspect_Handle )aWindowDC, (Aspect_RenderingContext )aGContext, isCoreProfile); |
5e27df78 | 468 | #else |
b6bf4ec1 | 469 | Window aWindow = (Window )myPlatformWindow->NativeHandle(); |
25b97fac | 470 | Display* aDisp = theDriver->GetDisplayConnection()->GetDisplay(); |
5e27df78 | 471 | GLXContext aGContext = (GLXContext )theGContext; |
b6bf4ec1 | 472 | GLXContext aSlaveCtx = !theShareCtx.IsNull() ? (GLXContext )theShareCtx->myGContext : NULL; |
473 | ||
474 | XWindowAttributes aWinAttribs; | |
475 | XGetWindowAttributes (aDisp, aWindow, &aWinAttribs); | |
476 | XVisualInfo aVisInfo; | |
477 | aVisInfo.visualid = aWinAttribs.visual->visualid; | |
478 | aVisInfo.screen = DefaultScreen (aDisp); | |
479 | int aNbItems; | |
480 | XVisualInfo* aVis = XGetVisualInfo (aDisp, VisualIDMask | VisualScreenMask, &aVisInfo, &aNbItems); | |
481 | int isGl = 0; | |
482 | if (aVis == NULL) | |
2166f0fa | 483 | { |
9775fa61 | 484 | throw Aspect_GraphicDeviceDefinitionError("OpenGl_Window::CreateWindow: XGetVisualInfo is unable to choose needed configuration in existing OpenGL context. "); |
b6bf4ec1 | 485 | return; |
2166f0fa | 486 | } |
b6bf4ec1 | 487 | else if (glXGetConfig (aDisp, aVis, GLX_USE_GL, &isGl) != 0 || !isGl) |
2166f0fa | 488 | { |
9775fa61 | 489 | throw Aspect_GraphicDeviceDefinitionError("OpenGl_Window::CreateWindow: window Visual does not support GL rendering!"); |
b6bf4ec1 | 490 | return; |
491 | } | |
3d8969b1 | 492 | |
b6bf4ec1 | 493 | // create new context |
494 | GLXFBConfig anFBConfig = myPlatformWindow->NativeFBConfig(); | |
495 | const char* aGlxExts = glXQueryExtensionsString (aDisp, aVisInfo.screen); | |
496 | if (myOwnGContext | |
497 | && anFBConfig != NULL | |
498 | && OpenGl_Context::CheckExtension (aGlxExts, "GLX_ARB_create_context_profile")) | |
499 | { | |
500 | // Replace default XError handler to ignore errors. | |
501 | // Warning - this is global for all threads! | |
502 | typedef int (*xerrorhandler_t)(Display* , XErrorEvent* ); | |
503 | xerrorhandler_t anOldHandler = XSetErrorHandler(xErrorDummyHandler); | |
504 | ||
505 | typedef GLXContext (*glXCreateContextAttribsARB_t)(Display* dpy, GLXFBConfig config, | |
506 | GLXContext share_context, Bool direct, | |
507 | const int* attrib_list); | |
508 | glXCreateContextAttribsARB_t aCreateCtxProc = (glXCreateContextAttribsARB_t )glXGetProcAddress((const GLubyte* )"glXCreateContextAttribsARB"); | |
509 | if (!theCaps->contextCompatible) | |
95db72f1 | 510 | { |
b6bf4ec1 | 511 | int aCoreCtxAttribs[] = |
95db72f1 | 512 | { |
b6bf4ec1 | 513 | GLX_CONTEXT_MAJOR_VERSION_ARB, 3, |
514 | GLX_CONTEXT_MINOR_VERSION_ARB, 2, | |
515 | GLX_CONTEXT_PROFILE_MASK_ARB, GLX_CONTEXT_CORE_PROFILE_BIT_ARB, | |
516 | GLX_CONTEXT_FLAGS_ARB, theCaps->contextDebug ? GLX_CONTEXT_DEBUG_BIT_ARB : 0, | |
517 | 0, 0 | |
518 | }; | |
519 | ||
520 | // try to create the core profile of highest OpenGL version supported by OCCT | |
521 | for (int aLowVer4 = 5; aLowVer4 >= 0 && aGContext == NULL; --aLowVer4) | |
2166f0fa | 522 | { |
b6bf4ec1 | 523 | aCoreCtxAttribs[1] = 4; |
524 | aCoreCtxAttribs[3] = aLowVer4; | |
525 | aGContext = aCreateCtxProc (aDisp, anFBConfig, aSlaveCtx, True, aCoreCtxAttribs); | |
2166f0fa | 526 | } |
b6bf4ec1 | 527 | for (int aLowVer3 = 3; aLowVer3 >= 2 && aGContext == NULL; --aLowVer3) |
95db72f1 | 528 | { |
b6bf4ec1 | 529 | aCoreCtxAttribs[1] = 3; |
530 | aCoreCtxAttribs[3] = aLowVer3; | |
531 | aGContext = aCreateCtxProc (aDisp, anFBConfig, aSlaveCtx, True, aCoreCtxAttribs); | |
95db72f1 | 532 | } |
b6bf4ec1 | 533 | isCoreProfile = aGContext != NULL; |
2166f0fa | 534 | } |
2166f0fa | 535 | |
b6bf4ec1 | 536 | if (aGContext == NULL) |
2166f0fa | 537 | { |
b6bf4ec1 | 538 | int aCtxAttribs[] = |
b5ac8292 | 539 | { |
b6bf4ec1 | 540 | GLX_CONTEXT_FLAGS_ARB, theCaps->contextDebug ? GLX_CONTEXT_DEBUG_BIT_ARB : 0, |
541 | 0, 0 | |
542 | }; | |
543 | isCoreProfile = Standard_False; | |
544 | aGContext = aCreateCtxProc (aDisp, anFBConfig, aSlaveCtx, True, aCtxAttribs); | |
b5ac8292 | 545 | |
b6bf4ec1 | 546 | if (aGContext != NULL |
547 | && !theCaps->contextCompatible) | |
95db72f1 | 548 | { |
b6bf4ec1 | 549 | TCollection_ExtendedString aMsg("OpenGl_Window::CreateWindow: core profile creation failed."); |
3b523c4c | 550 | myGlContext->PushMessage (GL_DEBUG_SOURCE_APPLICATION, GL_DEBUG_TYPE_PORTABILITY, 0, GL_DEBUG_SEVERITY_LOW, aMsg); |
95db72f1 | 551 | } |
2166f0fa | 552 | } |
b6bf4ec1 | 553 | XSetErrorHandler(anOldHandler); |
554 | } | |
2166f0fa | 555 | |
b6bf4ec1 | 556 | if (myOwnGContext |
557 | && aGContext == NULL) | |
558 | { | |
559 | aGContext = glXCreateContext (aDisp, aVis, aSlaveCtx, GL_TRUE); | |
560 | if (aGContext == NULL) | |
2166f0fa | 561 | { |
9775fa61 | 562 | throw Aspect_GraphicDeviceDefinitionError("OpenGl_Window::CreateWindow: glXCreateContext failed."); |
2166f0fa SK |
563 | return; |
564 | } | |
b6bf4ec1 | 565 | } |
2166f0fa | 566 | |
b6bf4ec1 | 567 | // check Visual for OpenGl context's parameters compatibility |
568 | TCollection_ExtendedString aList; | |
569 | int isDoubleBuffer = 0, isRGBA = 0, isStereo = 0; | |
570 | int aDepthSize = 0, aStencilSize = 0; | |
571 | glXGetConfig (aDisp, aVis, GLX_RGBA, &isRGBA); | |
572 | glXGetConfig (aDisp, aVis, GLX_DOUBLEBUFFER, &isDoubleBuffer); | |
573 | glXGetConfig (aDisp, aVis, GLX_STEREO, &isStereo); | |
574 | glXGetConfig (aDisp, aVis, GLX_DEPTH_SIZE, &aDepthSize); | |
575 | glXGetConfig (aDisp, aVis, GLX_STENCIL_SIZE, &aStencilSize); | |
576 | if (aDepthSize < 1) addMsgToList (aList, "no depth buffer"); | |
577 | if (aStencilSize < 1) addMsgToList (aList, "no stencil buffer"); | |
578 | if (isRGBA == 0) addMsgToList (aList, "no RGBA color buffer"); | |
579 | if (isDoubleBuffer == 0) addMsgToList (aList, "no Double Buffer"); | |
580 | if (theCaps->contextStereo && isStereo == 0) | |
581 | { | |
582 | addMsgToList (aList, "no Quad Buffer"); | |
583 | } | |
584 | else if (!theCaps->contextStereo && isStereo == 1) | |
585 | { | |
586 | addMsgToList (aList, "extra Quad Buffer"); | |
587 | } | |
588 | if (!aList.IsEmpty()) | |
589 | { | |
590 | TCollection_ExtendedString aMsg = TCollection_ExtendedString ("OpenGl_Window::CreateWindow: window Visual is incomplete: ") + aList; | |
3b523c4c | 591 | myGlContext->PushMessage (GL_DEBUG_SOURCE_APPLICATION, |
592 | GL_DEBUG_TYPE_OTHER, | |
593 | 0, GL_DEBUG_SEVERITY_MEDIUM, aMsg); | |
2166f0fa SK |
594 | } |
595 | ||
4e1523ef | 596 | myGlContext->Init ((Aspect_Drawable )aWindow, (Aspect_Display )aDisp, (Aspect_RenderingContext )aGContext, isCoreProfile); |
2166f0fa | 597 | #endif |
5e27df78 | 598 | myGlContext->Share (theShareCtx); |
f978241f | 599 | myGlContext->SetSwapInterval (mySwapInterval); |
2166f0fa | 600 | Init(); |
2166f0fa SK |
601 | } |
602 | ||
603 | // ======================================================================= | |
604 | // function : ~OpenGl_Window | |
605 | // purpose : | |
606 | // ======================================================================= | |
607 | OpenGl_Window::~OpenGl_Window() | |
608 | { | |
25b97fac | 609 | if (!myOwnGContext |
610 | || myGlContext.IsNull()) | |
fd4a6963 | 611 | { |
612 | myGlContext.Nullify(); | |
613 | return; | |
614 | } | |
615 | ||
616 | // release "GL" context if it is owned by window | |
544da5a4 | 617 | // Mesa implementation can fail to destroy GL context if it set for current thread. |
618 | // It should be safer to unset thread GL context before its destruction. | |
1ce0716b | 619 | #if defined(HAVE_EGL) |
25b97fac | 620 | if ((EGLSurface )myGlContext->myWindow != EGL_NO_SURFACE) |
621 | { | |
622 | eglDestroySurface ((EGLDisplay )myGlContext->myDisplay, | |
623 | (EGLSurface )myGlContext->myWindow); | |
624 | } | |
625 | #elif defined(_WIN32) | |
fd4a6963 | 626 | HWND aWindow = (HWND )myGlContext->myWindow; |
627 | HDC aWindowDC = (HDC )myGlContext->myWindowDC; | |
628 | HGLRC aWindowGContext = (HGLRC )myGlContext->myGContext; | |
544da5a4 | 629 | HGLRC aThreadGContext = wglGetCurrentContext(); |
5e27df78 | 630 | myGlContext.Nullify(); |
2166f0fa | 631 | |
544da5a4 | 632 | if (aThreadGContext != NULL) |
2166f0fa | 633 | { |
544da5a4 | 634 | if (aThreadGContext == aWindowGContext) |
2166f0fa | 635 | { |
fd4a6963 | 636 | wglMakeCurrent (NULL, NULL); |
2166f0fa | 637 | } |
fd4a6963 | 638 | |
639 | wglDeleteContext (aWindowGContext); | |
2166f0fa | 640 | } |
fd4a6963 | 641 | ReleaseDC (aWindow, aWindowDC); |
5e27df78 | 642 | #else |
544da5a4 | 643 | Display* aDisplay = (Display* )myGlContext->myDisplay; |
644 | GLXContext aWindowGContext = (GLXContext )myGlContext->myGContext; | |
645 | GLXContext aThreadGContext = glXGetCurrentContext(); | |
5e27df78 | 646 | myGlContext.Nullify(); |
647 | ||
fd4a6963 | 648 | if (aDisplay != NULL) |
2166f0fa | 649 | { |
544da5a4 | 650 | if (aThreadGContext == aWindowGContext) |
651 | { | |
652 | glXMakeCurrent (aDisplay, None, NULL); | |
653 | } | |
654 | ||
5e27df78 | 655 | // FSXXX sync necessary if non-direct rendering |
656 | glXWaitGL(); | |
544da5a4 | 657 | glXDestroyContext (aDisplay, aWindowGContext); |
2166f0fa SK |
658 | } |
659 | #endif | |
660 | } | |
661 | ||
4fe56619 | 662 | #endif // !__APPLE__ |
663 | ||
2166f0fa SK |
664 | // ======================================================================= |
665 | // function : Activate | |
666 | // purpose : | |
667 | // ======================================================================= | |
668 | Standard_Boolean OpenGl_Window::Activate() | |
669 | { | |
86fa64d9 | 670 | return myGlContext->MakeCurrent(); |
2166f0fa SK |
671 | } |
672 | ||
4fe56619 | 673 | #if !defined(__APPLE__) || defined(MACOSX_USE_GLX) |
674 | ||
2166f0fa SK |
675 | // ======================================================================= |
676 | // function : Resize | |
677 | // purpose : call_subr_resize | |
678 | // ======================================================================= | |
c357e426 | 679 | void OpenGl_Window::Resize() |
2166f0fa | 680 | { |
1ce0716b | 681 | #if !defined(_WIN32) && !defined(HAVE_EGL) |
73192b37 | 682 | Display* aDisp = (Display* )myGlContext->myDisplay; |
2166f0fa SK |
683 | if (aDisp == NULL) |
684 | return; | |
73192b37 | 685 | #endif |
2166f0fa | 686 | |
c357e426 | 687 | Standard_Integer aWidth = 0; |
688 | Standard_Integer aHeight = 0; | |
689 | myPlatformWindow->Size (aWidth, aHeight); | |
690 | ||
2166f0fa | 691 | // If the size is not changed - do nothing |
c357e426 | 692 | if ((myWidth == aWidth) && (myHeight == aHeight)) |
2166f0fa SK |
693 | return; |
694 | ||
c357e426 | 695 | myWidth = aWidth; |
696 | myHeight = aHeight; | |
2166f0fa | 697 | |
1ce0716b | 698 | #if !defined(_WIN32) && !defined(HAVE_EGL) |
5e27df78 | 699 | XResizeWindow (aDisp, myGlContext->myWindow, (unsigned int )myWidth, (unsigned int )myHeight); |
2166f0fa SK |
700 | XSync (aDisp, False); |
701 | #endif | |
702 | ||
703 | Init(); | |
704 | } | |
705 | ||
2166f0fa SK |
706 | // ======================================================================= |
707 | // function : Init | |
708 | // purpose : | |
709 | // ======================================================================= | |
710 | void OpenGl_Window::Init() | |
711 | { | |
712 | if (!Activate()) | |
713 | return; | |
714 | ||
1ce0716b | 715 | #if defined(HAVE_EGL) |
e3573bb9 | 716 | eglQuerySurface ((EGLDisplay )myGlContext->myDisplay, (EGLSurface )myGlContext->myWindow, EGL_WIDTH, &myWidth); |
717 | eglQuerySurface ((EGLDisplay )myGlContext->myDisplay, (EGLSurface )myGlContext->myWindow, EGL_HEIGHT, &myHeight); | |
25b97fac | 718 | #elif defined(_WIN32) |
62e1beed | 719 | // |
2166f0fa SK |
720 | #else |
721 | Window aRootWin; | |
722 | int aDummy; | |
723 | unsigned int aDummyU; | |
724 | unsigned int aNewWidth = 0; | |
725 | unsigned int aNewHeight = 0; | |
73192b37 | 726 | Display* aDisp = (Display* )myGlContext->myDisplay; |
5e27df78 | 727 | XGetGeometry (aDisp, myGlContext->myWindow, &aRootWin, &aDummy, &aDummy, &aNewWidth, &aNewHeight, &aDummyU, &aDummyU); |
2166f0fa SK |
728 | myWidth = aNewWidth; |
729 | myHeight = aNewHeight; | |
730 | #endif | |
731 | ||
5bd9ed93 | 732 | glDisable (GL_DITHER); |
ca3c13d1 | 733 | glDisable (GL_SCISSOR_TEST); |
3bffef55 | 734 | const Standard_Integer aViewport[4] = { 0, 0, myWidth, myHeight }; |
735 | myGlContext->ResizeViewport (aViewport); | |
ca3c13d1 | 736 | #if !defined(GL_ES_VERSION_2_0) |
3bffef55 | 737 | myGlContext->SetDrawBuffer (GL_BACK); |
4e1523ef | 738 | if (myGlContext->core11 != NULL) |
739 | { | |
740 | glMatrixMode (GL_MODELVIEW); | |
741 | } | |
ca3c13d1 | 742 | #endif |
2166f0fa SK |
743 | } |
744 | ||
2166f0fa | 745 | // ======================================================================= |
c357e426 | 746 | // function : SetSwapInterval |
2166f0fa SK |
747 | // purpose : |
748 | // ======================================================================= | |
c357e426 | 749 | void OpenGl_Window::SetSwapInterval() |
2166f0fa | 750 | { |
c357e426 | 751 | if (mySwapInterval != myGlContext->caps->swapInterval) |
b5ac8292 | 752 | { |
c357e426 | 753 | mySwapInterval = myGlContext->caps->swapInterval; |
754 | myGlContext->SetSwapInterval (mySwapInterval); | |
b5ac8292 | 755 | } |
2166f0fa | 756 | } |
c357e426 | 757 | |
758 | #endif // !__APPLE__ |