34 HGLRC GHOST_ContextWGL::s_sharedHGLRC =
NULL;
35 int GHOST_ContextWGL::s_sharedCount = 0;
40 return strstr((
const char *)glGetString(GL_VENDOR),
"Intel") !=
NULL;
47 int contextProfileMask,
48 int contextMajorVersion,
49 int contextMinorVersion,
51 int contextResetNotificationStrategy)
55 m_contextProfileMask(contextProfileMask),
56 m_contextMajorVersion(contextMajorVersion),
57 m_contextMinorVersion(contextMinorVersion),
58 m_contextFlags(contextFlags),
59 m_alphaBackground(alphaBackground),
60 m_contextResetNotificationStrategy(contextResetNotificationStrategy),
65 m_dummyRenderer(
NULL),
69 assert(m_hDC !=
NULL);
74 if (m_hGLRC !=
NULL) {
75 if (m_hGLRC == ::wglGetCurrentContext())
76 WIN32_CHK(::wglMakeCurrent(
NULL,
NULL));
78 if (m_hGLRC != s_sharedHGLRC || s_sharedCount == 1) {
79 assert(s_sharedCount > 0);
83 if (s_sharedCount == 0)
86 WIN32_CHK(::wglDeleteContext(m_hGLRC));
91 if (m_dummyRenderer) {
92 free((
void *)m_dummyRenderer);
93 free((
void *)m_dummyVendor);
94 free((
void *)m_dummyVersion);
106 if (WGLEW_EXT_swap_control)
114 if (WGLEW_EXT_swap_control) {
115 intervalOut = ::wglGetSwapIntervalEXT();
125 if (WIN32_CHK(::wglMakeCurrent(m_hDC, m_hGLRC))) {
135 if (WIN32_CHK(::wglMakeCurrent(
NULL,
NULL))) {
154 if (!(pfd.dwFlags & PFD_SUPPORT_OPENGL) || !(pfd.dwFlags & PFD_DRAW_TO_WINDOW) ||
155 !(pfd.dwFlags & PFD_DOUBLEBUFFER) ||
156 !(pfd.iPixelType == PFD_TYPE_RGBA) ||
157 (pfd.cColorBits > 32) ||
158 (pfd.dwFlags & PFD_GENERIC_FORMAT))
165 weight += pfd.cColorBits - 8;
167 if (preferredPFD.cAlphaBits > 0 && pfd.cAlphaBits > 0)
169 #ifdef WIN32_COMPOSITING
170 if ((preferredPFD.dwFlags & PFD_SUPPORT_COMPOSITION) && (pfd.dwFlags & PFD_SUPPORT_COMPOSITION))
183 int iPixelFormat = 0;
186 int iStereoPixelFormat = 0;
187 int stereoWeight = 0;
190 int iLastResortPixelFormat = ::ChoosePixelFormat(hDC, &preferredPFD);
192 WIN32_CHK(iLastResortPixelFormat != 0);
194 int lastPFD = ::DescribePixelFormat(hDC, 1,
sizeof(PIXELFORMATDESCRIPTOR),
NULL);
196 WIN32_CHK(lastPFD != 0);
198 for (
int i = 1; i <= lastPFD; i++) {
199 PIXELFORMATDESCRIPTOR pfd;
200 int check = ::DescribePixelFormat(hDC, i,
sizeof(PIXELFORMATDESCRIPTOR), &pfd);
202 WIN32_CHK(check == lastPFD);
211 if (
w > stereoWeight && (preferredPFD.dwFlags & pfd.dwFlags & PFD_STEREO)) {
213 iStereoPixelFormat = i;
218 if (iStereoPixelFormat != 0)
219 iPixelFormat = iStereoPixelFormat;
221 if (iPixelFormat == 0) {
222 fprintf(stderr,
"Warning! Using result of ChoosePixelFormat.\n");
223 iPixelFormat = iLastResortPixelFormat;
238 SetLastError(NO_ERROR);
240 DWORD dwExStyle = GetWindowLong(hWnd, GWL_EXSTYLE);
241 WIN32_CHK(GetLastError() == NO_ERROR);
243 WCHAR lpClassName[100] =
L"";
244 count = GetClassNameW(hWnd, lpClassName,
sizeof(lpClassName));
245 WIN32_CHK(
count != 0);
247 WCHAR lpWindowName[100] =
L"";
248 count = GetWindowTextW(hWnd, lpWindowName,
sizeof(lpWindowName));
249 WIN32_CHK(
count != 0);
251 DWORD dwStyle = GetWindowLong(hWnd, GWL_STYLE);
252 WIN32_CHK(GetLastError() == NO_ERROR);
255 GetWindowRect(hWnd, &rect);
256 WIN32_CHK(GetLastError() == NO_ERROR);
258 HWND hWndParent = (HWND)GetWindowLongPtr(hWnd, GWLP_HWNDPARENT);
259 WIN32_CHK(GetLastError() == NO_ERROR);
261 HMENU hMenu = GetMenu(hWnd);
262 WIN32_CHK(GetLastError() == NO_ERROR);
264 HINSTANCE hInstance = (HINSTANCE)GetWindowLongPtr(hWnd, GWLP_HINSTANCE);
265 WIN32_CHK(GetLastError() == NO_ERROR);
267 HWND hwndCloned = CreateWindowExW(dwExStyle,
273 rect.right - rect.left,
274 rect.bottom - rect.top,
280 WIN32_CHK(hwndCloned !=
NULL);
285 void GHOST_ContextWGL::initContextWGLEW(PIXELFORMATDESCRIPTOR &preferredPFD)
287 HWND dummyHWND =
NULL;
290 HGLRC dummyHGLRC =
NULL;
297 SetLastError(NO_ERROR);
299 prevHDC = ::wglGetCurrentDC();
300 WIN32_CHK(GetLastError() == NO_ERROR);
302 prevHGLRC = ::wglGetCurrentContext();
303 WIN32_CHK(GetLastError() == NO_ERROR);
307 if (iPixelFormat == 0)
310 PIXELFORMATDESCRIPTOR chosenPFD;
312 ::DescribePixelFormat(m_hDC, iPixelFormat,
sizeof(PIXELFORMATDESCRIPTOR), &chosenPFD)))
318 if (dummyHWND ==
NULL)
321 dummyHDC = GetDC(dummyHWND);
324 if (!WIN32_CHK(dummyHDC !=
NULL))
327 if (!WIN32_CHK(::SetPixelFormat(dummyHDC, iPixelFormat, &chosenPFD)))
330 dummyHGLRC = ::wglCreateContext(dummyHDC);
332 if (!WIN32_CHK(dummyHGLRC !=
NULL))
335 if (!WIN32_CHK(::wglMakeCurrent(dummyHDC, dummyHGLRC)))
338 if (
GLEW_CHK(glewInit()) != GLEW_OK)
339 fprintf(stderr,
"Warning! Dummy GLEW/WGLEW failed to initialize properly.\n");
344 free((
void *)m_dummyRenderer);
345 free((
void *)m_dummyVendor);
346 free((
void *)m_dummyVersion);
348 m_dummyRenderer = _strdup(
reinterpret_cast<const char *
>(glGetString(GL_RENDERER)));
349 m_dummyVendor = _strdup(
reinterpret_cast<const char *
>(glGetString(GL_VENDOR)));
350 m_dummyVersion = _strdup(
reinterpret_cast<const char *
>(glGetString(GL_VERSION)));
354 WIN32_CHK(::wglMakeCurrent(prevHDC, prevHGLRC));
356 if (dummyHGLRC !=
NULL)
357 WIN32_CHK(::wglDeleteContext(dummyHGLRC));
359 if (dummyHWND !=
NULL) {
360 if (dummyHDC !=
NULL)
361 WIN32_CHK(::ReleaseDC(dummyHWND, dummyHDC));
363 WIN32_CHK(::DestroyWindow(dummyHWND));
367 static void makeAttribList(std::vector<int> &out,
bool stereoVisual,
bool needAlpha)
372 out.push_back(WGL_SUPPORT_OPENGL_ARB);
373 out.push_back(GL_TRUE);
375 out.push_back(WGL_DRAW_TO_WINDOW_ARB);
376 out.push_back(GL_TRUE);
378 out.push_back(WGL_DOUBLE_BUFFER_ARB);
379 out.push_back(GL_TRUE);
381 out.push_back(WGL_ACCELERATION_ARB);
382 out.push_back(WGL_FULL_ACCELERATION_ARB);
385 out.push_back(WGL_STEREO_ARB);
386 out.push_back(GL_TRUE);
389 out.push_back(WGL_PIXEL_TYPE_ARB);
390 out.push_back(WGL_TYPE_RGBA_ARB);
392 out.push_back(WGL_COLOR_BITS_ARB);
396 out.push_back(WGL_ALPHA_BITS_ARB);
403 int GHOST_ContextWGL::_choose_pixel_format_arb_1(
bool stereoVisual,
bool needAlpha)
405 std::vector<int> iAttributes;
407 #define _MAX_PIXEL_FORMATS 32
409 int iPixelFormat = 0;
415 WIN32_CHK(wglChoosePixelFormatARB(
418 if (nNumFormats > 0) {
419 iPixelFormat = iPixelFormats[0];
421 #ifdef WIN32_COMPOSITING
424 PIXELFORMATDESCRIPTOR pfd;
427 for (i = 0; i < nNumFormats; i++) {
428 if (DescribePixelFormat(m_hDC, iPixelFormats[i],
sizeof(PIXELFORMATDESCRIPTOR), &pfd)) {
429 if (pfd.dwFlags & PFD_SUPPORT_COMPOSITION) {
430 iPixelFormat = iPixelFormats[i];
435 if (i == nNumFormats) {
436 fprintf(stderr,
"Warning! Unable to find a pixel format with compositing capability.\n");
443 if (iPixelFormat != 0) {
445 int alphaBits, iQuery = WGL_ALPHA_BITS_ARB;
446 wglGetPixelFormatAttribivARB(m_hDC, iPixelFormat, 0, 1, &iQuery, &alphaBits);
447 if (alphaBits == 0) {
448 fprintf(stderr,
"Warning! Unable to find a frame buffer with alpha channel.\n");
455 int GHOST_ContextWGL::choose_pixel_format_arb(
bool stereoVisual,
bool needAlpha)
459 iPixelFormat = _choose_pixel_format_arb_1(stereoVisual, needAlpha);
461 if (iPixelFormat == 0 && stereoVisual) {
462 fprintf(stderr,
"Warning! Unable to find a stereo pixel format.\n");
464 iPixelFormat = _choose_pixel_format_arb_1(
false, needAlpha);
472 int GHOST_ContextWGL::choose_pixel_format(
bool stereoVisual,
bool needAlpha)
474 PIXELFORMATDESCRIPTOR preferredPFD = {
475 sizeof(PIXELFORMATDESCRIPTOR),
478 PFD_SUPPORT_OPENGL | PFD_DRAW_TO_WINDOW |
480 (stereoVisual ? PFD_STEREO : 0) |
482 #ifdef WIN32_COMPOSITING
484 PFD_SUPPORT_COMPOSITION :
488 (BYTE)(needAlpha ? 32 : 24),
495 (BYTE)(needAlpha ? 8 : 0),
512 initContextWGLEW(preferredPFD);
514 int iPixelFormat = 0;
516 if (WGLEW_ARB_pixel_format)
517 iPixelFormat = choose_pixel_format_arb(stereoVisual, needAlpha);
519 if (iPixelFormat == 0)
528 fprintf(stderr,
"%s: %s\n", name,
context);
530 if (dummy && strcmp(dummy,
context) != 0)
531 fprintf(stderr,
"Warning! Dummy %s: %s\n", name, dummy);
537 SetLastError(NO_ERROR);
539 HGLRC prevHGLRC = ::wglGetCurrentContext();
540 WIN32_CHK(GetLastError() == NO_ERROR);
542 HDC prevHDC = ::wglGetCurrentDC();
543 WIN32_CHK(GetLastError() == NO_ERROR);
545 if (!WGLEW_ARB_create_context || ::GetPixelFormat(m_hDC) == 0) {
546 const bool needAlpha = m_alphaBackground;
550 PIXELFORMATDESCRIPTOR chosenPFD;
554 if (iPixelFormat == 0) {
558 lastPFD = ::DescribePixelFormat(
559 m_hDC, iPixelFormat,
sizeof(PIXELFORMATDESCRIPTOR), &chosenPFD);
561 if (!WIN32_CHK(lastPFD != 0)) {
565 if (needAlpha && chosenPFD.cAlphaBits == 0)
566 fprintf(stderr,
"Warning! Unable to find a pixel format with an alpha channel.\n");
568 if (!WIN32_CHK(::SetPixelFormat(m_hDC, iPixelFormat, &chosenPFD))) {
573 if (WGLEW_ARB_create_context) {
574 int profileBitCore = m_contextProfileMask & WGL_CONTEXT_CORE_PROFILE_BIT_ARB;
575 int profileBitCompat = m_contextProfileMask & WGL_CONTEXT_COMPATIBILITY_PROFILE_BIT_ARB;
578 int profileBitES = m_contextProfileMask & WGL_CONTEXT_ES_PROFILE_BIT_EXT;
581 if (!WGLEW_ARB_create_context_profile && profileBitCore)
582 fprintf(stderr,
"Warning! OpenGL core profile not available.\n");
584 if (!WGLEW_ARB_create_context_profile && profileBitCompat)
585 fprintf(stderr,
"Warning! OpenGL compatibility profile not available.\n");
588 if (!WGLEW_EXT_create_context_es_profile && profileBitES && m_contextMajorVersion == 1)
589 fprintf(stderr,
"Warning! OpenGL ES profile not available.\n");
591 if (!WGLEW_EXT_create_context_es2_profile && profileBitES && m_contextMajorVersion == 2)
592 fprintf(stderr,
"Warning! OpenGL ES2 profile not available.\n");
597 if (WGLEW_ARB_create_context_profile && profileBitCore)
598 profileMask |= profileBitCore;
600 if (WGLEW_ARB_create_context_profile && profileBitCompat)
601 profileMask |= profileBitCompat;
604 if (WGLEW_EXT_create_context_es_profile && profileBitES)
605 profileMask |= profileBitES;
608 if (profileMask != m_contextProfileMask)
609 fprintf(stderr,
"Warning! Ignoring untested OpenGL context profile mask bits.");
611 std::vector<int> iAttributes;
614 iAttributes.push_back(WGL_CONTEXT_PROFILE_MASK_ARB);
615 iAttributes.push_back(profileMask);
618 if (m_contextMajorVersion != 0) {
619 iAttributes.push_back(WGL_CONTEXT_MAJOR_VERSION_ARB);
620 iAttributes.push_back(m_contextMajorVersion);
623 if (m_contextMinorVersion != 0) {
624 iAttributes.push_back(WGL_CONTEXT_MINOR_VERSION_ARB);
625 iAttributes.push_back(m_contextMinorVersion);
628 if (m_contextFlags != 0) {
629 iAttributes.push_back(WGL_CONTEXT_FLAGS_ARB);
630 iAttributes.push_back(m_contextFlags);
633 if (m_contextResetNotificationStrategy != 0) {
634 if (WGLEW_ARB_create_context_robustness) {
635 iAttributes.push_back(WGL_CONTEXT_RESET_NOTIFICATION_STRATEGY_ARB);
636 iAttributes.push_back(m_contextResetNotificationStrategy);
639 fprintf(stderr,
"Warning! Cannot set the reset notification strategy.");
643 iAttributes.push_back(0);
645 m_hGLRC = ::wglCreateContextAttribsARB(m_hDC,
NULL, &(iAttributes[0]));
651 const bool silent = m_contextMajorVersion > 3;
652 if (!WIN32_CHK_SILENT(m_hGLRC !=
NULL, silent)) {
659 if (s_sharedHGLRC ==
NULL) {
660 s_sharedHGLRC = m_hGLRC;
662 else if (!WIN32_CHK(::wglShareLists(s_sharedHGLRC, m_hGLRC))) {
666 if (!WIN32_CHK(::wglMakeCurrent(m_hDC, m_hGLRC))) {
682 ::SwapBuffers(m_hDC);
686 const char *vendor =
reinterpret_cast<const char *
>(glGetString(GL_VENDOR));
687 const char *renderer =
reinterpret_cast<const char *
>(glGetString(GL_RENDERER));
688 const char *version =
reinterpret_cast<const char *
>(glGetString(GL_VERSION));
694 fprintf(stderr,
"Context Version: %d.%d\n", m_contextMajorVersion, m_contextMinorVersion);
700 ::wglMakeCurrent(prevHDC, prevHGLRC);
void BLI_kdtree_nd_() free(KDTree *tree)
static void makeAttribList(std::vector< int > &out, bool stereoVisual, bool needAlpha)
static int choose_pixel_format_legacy(HDC hDC, PIXELFORMATDESCRIPTOR &preferredPFD)
#define _MAX_PIXEL_FORMATS
static int weight_pixel_format(PIXELFORMATDESCRIPTOR &pfd, PIXELFORMATDESCRIPTOR &preferredPFD)
static void reportContextString(const char *name, const char *dummy, const char *context)
static bool is_crappy_intel_card()
static HWND clone_window(HWND hWnd, LPVOID lpParam)
SIMD_FORCE_INLINE const btScalar & w() const
Return the w value.
GHOST_ContextWGL(bool stereoVisual, bool alphaBackground, HWND hWnd, HDC hDC, int contextProfileMask, int contextMajorVersion, int contextMinorVersion, int contextFlags, int contextResetNotificationStrategy)
GHOST_TSuccess activateDrawingContext()
GHOST_TSuccess getSwapInterval(int &intervalOut)
GHOST_TSuccess swapBuffers()
GHOST_TSuccess releaseNativeHandles()
GHOST_TSuccess initializeDrawingContext()
GHOST_TSuccess releaseDrawingContext()
GHOST_TSuccess setSwapInterval(int interval)
static void initClearGL()
static void error(const char *str)
struct SELECTID_Context context