Welcome to mirror list, hosted at ThFree Co, Russian Federation.

GHOST_ContextWGL.cpp « intern « ghost « intern - git.blender.org/blender.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: d3c190a13b13638b004bd27d3b6ca7d7fc86fa30 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
/* SPDX-License-Identifier: GPL-2.0-or-later
 * Copyright 2013 Blender Foundation. All rights reserved. */

/** \file
 * \ingroup GHOST
 *
 * Definition of GHOST_ContextWGL class.
 */

#include "GHOST_ContextWGL.h"

#include <tchar.h>

#include <cassert>
#include <cstdio>
#include <vector>

HGLRC GHOST_ContextWGL::s_sharedHGLRC = NULL;
int GHOST_ContextWGL::s_sharedCount = 0;

/* Some third-generation Intel video-cards are constantly bring problems */
static bool is_crappy_intel_card()
{
  return strstr((const char *)glGetString(GL_VENDOR), "Intel") != NULL;
}

GHOST_ContextWGL::GHOST_ContextWGL(bool stereoVisual,
                                   bool alphaBackground,
                                   HWND hWnd,
                                   HDC hDC,
                                   int contextProfileMask,
                                   int contextMajorVersion,
                                   int contextMinorVersion,
                                   int contextFlags,
                                   int contextResetNotificationStrategy)
    : GHOST_Context(stereoVisual),
      m_hWnd(hWnd),
      m_hDC(hDC),
      m_contextProfileMask(contextProfileMask),
      m_contextMajorVersion(contextMajorVersion),
      m_contextMinorVersion(contextMinorVersion),
      m_contextFlags(contextFlags),
      m_alphaBackground(alphaBackground),
      m_contextResetNotificationStrategy(contextResetNotificationStrategy),
      m_hGLRC(NULL)
#ifndef NDEBUG
      ,
      m_dummyVendor(NULL),
      m_dummyRenderer(NULL),
      m_dummyVersion(NULL)
#endif
{
  assert(m_hDC != NULL);
}

GHOST_ContextWGL::~GHOST_ContextWGL()
{
  if (m_hGLRC != NULL) {
    if (m_hGLRC == ::wglGetCurrentContext())
      WIN32_CHK(::wglMakeCurrent(NULL, NULL));

    if (m_hGLRC != s_sharedHGLRC || s_sharedCount == 1) {
      assert(s_sharedCount > 0);

      s_sharedCount--;

      if (s_sharedCount == 0)
        s_sharedHGLRC = NULL;

      WIN32_CHK(::wglDeleteContext(m_hGLRC));
    }
  }

#ifndef NDEBUG
  if (m_dummyRenderer) {
    free((void *)m_dummyRenderer);
    free((void *)m_dummyVendor);
    free((void *)m_dummyVersion);
  }
#endif
}

GHOST_TSuccess GHOST_ContextWGL::swapBuffers()
{
  return WIN32_CHK(::SwapBuffers(m_hDC)) ? GHOST_kSuccess : GHOST_kFailure;
}

GHOST_TSuccess GHOST_ContextWGL::setSwapInterval(int interval)
{
  if (epoxy_has_wgl_extension(m_hDC, "WGL_EXT_swap_control"))
    return WIN32_CHK(::wglSwapIntervalEXT(interval)) == TRUE ? GHOST_kSuccess : GHOST_kFailure;
  else
    return GHOST_kFailure;
}

GHOST_TSuccess GHOST_ContextWGL::getSwapInterval(int &intervalOut)
{
  if (epoxy_has_wgl_extension(m_hDC, "WGL_EXT_swap_control")) {
    intervalOut = ::wglGetSwapIntervalEXT();
    return GHOST_kSuccess;
  }
  else {
    return GHOST_kFailure;
  }
}

GHOST_TSuccess GHOST_ContextWGL::activateDrawingContext()
{
  if (WIN32_CHK(::wglMakeCurrent(m_hDC, m_hGLRC))) {
    return GHOST_kSuccess;
  }
  else {
    return GHOST_kFailure;
  }
}

GHOST_TSuccess GHOST_ContextWGL::releaseDrawingContext()
{
  if (WIN32_CHK(::wglMakeCurrent(NULL, NULL))) {
    return GHOST_kSuccess;
  }
  else {
    return GHOST_kFailure;
  }
}

/* Ron Fosner's code for weighting pixel formats and forcing software.
 * See http://www.opengl.org/resources/faq/technical/weight.cpp
 */
static int weight_pixel_format(PIXELFORMATDESCRIPTOR &pfd, PIXELFORMATDESCRIPTOR &preferredPFD)
{
  int weight = 0;

  /* assume desktop color depth is 32 bits per pixel */

  /* cull unusable pixel formats */
  /* if no formats can be found, can we determine why it was rejected? */
  if (!(pfd.dwFlags & PFD_SUPPORT_OPENGL) || !(pfd.dwFlags & PFD_DRAW_TO_WINDOW) ||
      !(pfd.dwFlags & PFD_DOUBLEBUFFER) || /* Blender _needs_ this. */
      !(pfd.iPixelType == PFD_TYPE_RGBA) ||
      (pfd.cColorBits > 32) ||            /* 64 bit formats disable AERO. */
      (pfd.dwFlags & PFD_GENERIC_FORMAT)) /* No software renderers. */
  {
    return 0;
  }

  weight = 1; /* it's usable */

  weight += pfd.cColorBits - 8;

  if (preferredPFD.cAlphaBits > 0 && pfd.cAlphaBits > 0)
    weight++;
#ifdef WIN32_COMPOSITING
  if ((preferredPFD.dwFlags & PFD_SUPPORT_COMPOSITION) && (pfd.dwFlags & PFD_SUPPORT_COMPOSITION))
    weight++;
#endif

  return weight;
}

/*
 * A modification of Ron Fosner's replacement for ChoosePixelFormat
 * returns 0 on error, else returns the pixel format number to be used
 */
static int choose_pixel_format_legacy(HDC hDC, PIXELFORMATDESCRIPTOR &preferredPFD)
{
  int iPixelFormat = 0;
  int weight = 0;

  int iStereoPixelFormat = 0;
  int stereoWeight = 0;

  /* choose a pixel format using the useless Windows function in case we come up empty handed */
  int iLastResortPixelFormat = ::ChoosePixelFormat(hDC, &preferredPFD);

  WIN32_CHK(iLastResortPixelFormat != 0);

  int lastPFD = ::DescribePixelFormat(hDC, 1, sizeof(PIXELFORMATDESCRIPTOR), NULL);

  WIN32_CHK(lastPFD != 0);

  for (int i = 1; i <= lastPFD; i++) {
    PIXELFORMATDESCRIPTOR pfd;
    int check = ::DescribePixelFormat(hDC, i, sizeof(PIXELFORMATDESCRIPTOR), &pfd);

    WIN32_CHK(check == lastPFD);

    int w = weight_pixel_format(pfd, preferredPFD);

    if (w > weight) {
      weight = w;
      iPixelFormat = i;
    }

    if (w > stereoWeight && (preferredPFD.dwFlags & pfd.dwFlags & PFD_STEREO)) {
      stereoWeight = w;
      iStereoPixelFormat = i;
    }
  }

  /* choose any available stereo format over a non-stereo format */
  if (iStereoPixelFormat != 0)
    iPixelFormat = iStereoPixelFormat;

  if (iPixelFormat == 0) {
    fprintf(stderr, "Warning! Using result of ChoosePixelFormat.\n");
    iPixelFormat = iLastResortPixelFormat;
  }

  return iPixelFormat;
}

/**
 * Clone a window for the purpose of creating a temporary context to initialize WGL extensions.
 * There is no generic way to clone the lpParam parameter,
 * so the caller is responsible for cloning it themselves.
 */
static HWND clone_window(HWND hWnd, LPVOID lpParam)
{
  int count;

  SetLastError(NO_ERROR);

  DWORD dwExStyle = GetWindowLong(hWnd, GWL_EXSTYLE);
  WIN32_CHK(GetLastError() == NO_ERROR);

  WCHAR lpClassName[100] = L"";
  count = GetClassNameW(hWnd, lpClassName, sizeof(lpClassName));
  WIN32_CHK(count != 0);

  WCHAR lpWindowName[100] = L"";
  count = GetWindowTextW(hWnd, lpWindowName, sizeof(lpWindowName));
  WIN32_CHK(count != 0);

  DWORD dwStyle = GetWindowLong(hWnd, GWL_STYLE);
  WIN32_CHK(GetLastError() == NO_ERROR);

  RECT rect;
  GetWindowRect(hWnd, &rect);
  WIN32_CHK(GetLastError() == NO_ERROR);

  HWND hWndParent = (HWND)GetWindowLongPtr(hWnd, GWLP_HWNDPARENT);
  WIN32_CHK(GetLastError() == NO_ERROR);

  HMENU hMenu = GetMenu(hWnd);
  WIN32_CHK(GetLastError() == NO_ERROR);

  HINSTANCE hInstance = (HINSTANCE)GetWindowLongPtr(hWnd, GWLP_HINSTANCE);
  WIN32_CHK(GetLastError() == NO_ERROR);

  HWND hwndCloned = CreateWindowExW(dwExStyle,
                                    lpClassName,
                                    lpWindowName,
                                    dwStyle,
                                    rect.left,
                                    rect.top,
                                    rect.right - rect.left,
                                    rect.bottom - rect.top,
                                    hWndParent,
                                    hMenu,
                                    hInstance,
                                    lpParam);

  WIN32_CHK(hwndCloned != NULL);

  return hwndCloned;
}

static void makeAttribList(std::vector<int> &out, bool stereoVisual, bool needAlpha)
{
  out.clear();
  out.reserve(30);

  out.push_back(WGL_SUPPORT_OPENGL_ARB);
  out.push_back(GL_TRUE);

  out.push_back(WGL_DRAW_TO_WINDOW_ARB);
  out.push_back(GL_TRUE);

  out.push_back(WGL_DOUBLE_BUFFER_ARB);
  out.push_back(GL_TRUE);

  out.push_back(WGL_ACCELERATION_ARB);
  out.push_back(WGL_FULL_ACCELERATION_ARB);

  if (stereoVisual) {
    out.push_back(WGL_STEREO_ARB);
    out.push_back(GL_TRUE);
  }

  out.push_back(WGL_PIXEL_TYPE_ARB);
  out.push_back(WGL_TYPE_RGBA_ARB);

  out.push_back(WGL_COLOR_BITS_ARB);
  out.push_back(24);

  if (needAlpha) {
    out.push_back(WGL_ALPHA_BITS_ARB);
    out.push_back(8);
  }

  out.push_back(0);
}

/* Temporary context used to create the actual context. We need ARB pixel format
 * and context extensions, which are only available within a context. */
struct DummyContextWGL {
  HWND dummyHWND = NULL;

  HDC dummyHDC = NULL;
  HGLRC dummyHGLRC = NULL;

  HDC prevHDC = NULL;
  HGLRC prevHGLRC = NULL;

  int dummyPixelFormat = 0;

  PIXELFORMATDESCRIPTOR preferredPFD;

  bool has_WGL_ARB_pixel_format = false;
  bool has_WGL_ARB_create_context = false;
  bool has_WGL_ARB_create_context_profile = false;
  bool has_WGL_ARB_create_context_robustness = false;

  DummyContextWGL(HDC hDC, HWND hWnd, bool stereoVisual, bool needAlpha)
  {
    preferredPFD = {
        sizeof(PIXELFORMATDESCRIPTOR), /* size */
        1,                             /* version */
        (DWORD)(PFD_SUPPORT_OPENGL | PFD_DRAW_TO_WINDOW |
                PFD_DOUBLEBUFFER |                /* support double-buffering */
                (stereoVisual ? PFD_STEREO : 0) | /* support stereo */
                (
#ifdef WIN32_COMPOSITING
                    /* Support composition for transparent background. */
                    needAlpha ? PFD_SUPPORT_COMPOSITION :
#endif
                                0)),
        PFD_TYPE_RGBA,               /* color type */
        (BYTE)(needAlpha ? 32 : 24), /* preferred color depth */
        0,
        0,
        0,
        0,
        0,
        0,                         /* color bits (ignored) */
        (BYTE)(needAlpha ? 8 : 0), /* alpha buffer */
        0,                         /* alpha shift (ignored) */
        0,                         /* no accumulation buffer */
        0,
        0,
        0,
        0,              /* accum bits (ignored) */
        0,              /* depth buffer */
        0,              /* stencil buffer */
        0,              /* no auxiliary buffers */
        PFD_MAIN_PLANE, /* main layer */
        0,              /* reserved */
        0,
        0,
        0 /* layer, visible, and damage masks (ignored) */
    };

    SetLastError(NO_ERROR);

    prevHDC = ::wglGetCurrentDC();
    WIN32_CHK(GetLastError() == NO_ERROR);

    prevHGLRC = ::wglGetCurrentContext();
    WIN32_CHK(GetLastError() == NO_ERROR);

    dummyPixelFormat = choose_pixel_format_legacy(hDC, preferredPFD);

    if (dummyPixelFormat == 0)
      return;

    PIXELFORMATDESCRIPTOR chosenPFD;
    if (!WIN32_CHK(::DescribePixelFormat(
            hDC, dummyPixelFormat, sizeof(PIXELFORMATDESCRIPTOR), &chosenPFD)))
      return;

    if (hWnd) {
      dummyHWND = clone_window(hWnd, NULL);

      if (dummyHWND == NULL)
        return;

      dummyHDC = GetDC(dummyHWND);
    }

    if (!WIN32_CHK(dummyHDC != NULL))
      return;

    if (!WIN32_CHK(::SetPixelFormat(dummyHDC, dummyPixelFormat, &chosenPFD)))
      return;

    dummyHGLRC = ::wglCreateContext(dummyHDC);

    if (!WIN32_CHK(dummyHGLRC != NULL))
      return;

    if (!WIN32_CHK(::wglMakeCurrent(dummyHDC, dummyHGLRC)))
      return;

    has_WGL_ARB_pixel_format = epoxy_has_wgl_extension(hDC, "WGL_ARB_pixel_format");
    has_WGL_ARB_create_context = epoxy_has_wgl_extension(hDC, "WGL_ARB_create_context");
    has_WGL_ARB_create_context_profile = epoxy_has_wgl_extension(hDC,
                                                                 "WGL_ARB_create_context_profile");
    has_WGL_ARB_create_context_robustness = epoxy_has_wgl_extension(
        hDC, "WGL_ARB_create_context_robustness");
  }

  ~DummyContextWGL()
  {
    WIN32_CHK(::wglMakeCurrent(prevHDC, prevHGLRC));

    if (dummyHGLRC != NULL)
      WIN32_CHK(::wglDeleteContext(dummyHGLRC));

    if (dummyHWND != NULL) {
      if (dummyHDC != NULL)
        WIN32_CHK(::ReleaseDC(dummyHWND, dummyHDC));

      WIN32_CHK(::DestroyWindow(dummyHWND));
    }
  }
};

int GHOST_ContextWGL::_choose_pixel_format_arb_1(bool stereoVisual, bool needAlpha)
{
  std::vector<int> iAttributes;

#define _MAX_PIXEL_FORMATS 32

  int iPixelFormat = 0;
  int iPixelFormats[_MAX_PIXEL_FORMATS];

  makeAttribList(iAttributes, stereoVisual, needAlpha);

  UINT nNumFormats;
  WIN32_CHK(wglChoosePixelFormatARB(
      m_hDC, &(iAttributes[0]), NULL, _MAX_PIXEL_FORMATS, iPixelFormats, &nNumFormats));

  if (nNumFormats > 0) {
    iPixelFormat = iPixelFormats[0];

#ifdef WIN32_COMPOSITING
    if (needAlpha) {
      // scan through all pixel format to make sure one supports compositing
      PIXELFORMATDESCRIPTOR pfd;
      int i;

      for (i = 0; i < nNumFormats; i++) {
        if (DescribePixelFormat(m_hDC, iPixelFormats[i], sizeof(PIXELFORMATDESCRIPTOR), &pfd)) {
          if (pfd.dwFlags & PFD_SUPPORT_COMPOSITION) {
            iPixelFormat = iPixelFormats[i];
            break;
          }
        }
      }
      if (i == nNumFormats) {
        fprintf(stderr, "Warning! Unable to find a pixel format with compositing capability.\n");
      }
    }
#endif
  }

  // check pixel format
  if (iPixelFormat != 0) {
    if (needAlpha) {
      int alphaBits, iQuery = WGL_ALPHA_BITS_ARB;
      wglGetPixelFormatAttribivARB(m_hDC, iPixelFormat, 0, 1, &iQuery, &alphaBits);
      if (alphaBits == 0) {
        fprintf(stderr, "Warning! Unable to find a frame buffer with alpha channel.\n");
      }
    }
  }
  return iPixelFormat;
}

int GHOST_ContextWGL::choose_pixel_format_arb(bool stereoVisual, bool needAlpha)
{
  int iPixelFormat;

  iPixelFormat = _choose_pixel_format_arb_1(stereoVisual, needAlpha);

  if (iPixelFormat == 0 && stereoVisual) {
    fprintf(stderr, "Warning! Unable to find a stereo pixel format.\n");

    iPixelFormat = _choose_pixel_format_arb_1(false, needAlpha);

    m_stereoVisual = false;  // set context property to actual value
  }

  return iPixelFormat;
}

#ifndef NDEBUG
static void reportContextString(const char *name, const char *dummy, const char *context)
{
  fprintf(stderr, "%s: %s\n", name, context);

  if (dummy && strcmp(dummy, context) != 0)
    fprintf(stderr, "Warning! Dummy %s: %s\n", name, dummy);
}
#endif

GHOST_TSuccess GHOST_ContextWGL::initializeDrawingContext()
{
  SetLastError(NO_ERROR);

  HGLRC prevHGLRC = ::wglGetCurrentContext();
  WIN32_CHK(GetLastError() == NO_ERROR);

  HDC prevHDC = ::wglGetCurrentDC();
  WIN32_CHK(GetLastError() == NO_ERROR);

  {
    const bool needAlpha = m_alphaBackground;
    DummyContextWGL dummy(m_hDC, m_hWnd, m_stereoVisual, needAlpha);

    if (!dummy.has_WGL_ARB_create_context || ::GetPixelFormat(m_hDC) == 0) {
      int iPixelFormat = 0;

      if (dummy.has_WGL_ARB_pixel_format)
        iPixelFormat = choose_pixel_format_arb(m_stereoVisual, needAlpha);

      if (iPixelFormat == 0)
        iPixelFormat = choose_pixel_format_legacy(m_hDC, dummy.preferredPFD);

      if (iPixelFormat == 0) {
        goto error;
      }

      PIXELFORMATDESCRIPTOR chosenPFD;
      int lastPFD = ::DescribePixelFormat(
          m_hDC, iPixelFormat, sizeof(PIXELFORMATDESCRIPTOR), &chosenPFD);

      if (!WIN32_CHK(lastPFD != 0)) {
        goto error;
      }

      if (needAlpha && chosenPFD.cAlphaBits == 0)
        fprintf(stderr, "Warning! Unable to find a pixel format with an alpha channel.\n");

      if (!WIN32_CHK(::SetPixelFormat(m_hDC, iPixelFormat, &chosenPFD))) {
        goto error;
      }
    }

    if (dummy.has_WGL_ARB_create_context) {
      int profileBitCore = m_contextProfileMask & WGL_CONTEXT_CORE_PROFILE_BIT_ARB;
      int profileBitCompat = m_contextProfileMask & WGL_CONTEXT_COMPATIBILITY_PROFILE_BIT_ARB;

      if (!dummy.has_WGL_ARB_create_context_profile && profileBitCore)
        fprintf(stderr, "Warning! OpenGL core profile not available.\n");

      if (!dummy.has_WGL_ARB_create_context_profile && profileBitCompat)
        fprintf(stderr, "Warning! OpenGL compatibility profile not available.\n");

      int profileMask = 0;

      if (dummy.has_WGL_ARB_create_context_profile && profileBitCore)
        profileMask |= profileBitCore;

      if (dummy.has_WGL_ARB_create_context_profile && profileBitCompat)
        profileMask |= profileBitCompat;

      if (profileMask != m_contextProfileMask)
        fprintf(stderr, "Warning! Ignoring untested OpenGL context profile mask bits.");

      std::vector<int> iAttributes;

      if (profileMask) {
        iAttributes.push_back(WGL_CONTEXT_PROFILE_MASK_ARB);
        iAttributes.push_back(profileMask);
      }

      if (m_contextMajorVersion != 0) {
        iAttributes.push_back(WGL_CONTEXT_MAJOR_VERSION_ARB);
        iAttributes.push_back(m_contextMajorVersion);
      }

      if (m_contextMinorVersion != 0) {
        iAttributes.push_back(WGL_CONTEXT_MINOR_VERSION_ARB);
        iAttributes.push_back(m_contextMinorVersion);
      }

      if (m_contextFlags != 0) {
        iAttributes.push_back(WGL_CONTEXT_FLAGS_ARB);
        iAttributes.push_back(m_contextFlags);
      }

      if (m_contextResetNotificationStrategy != 0) {
        if (dummy.has_WGL_ARB_create_context_robustness) {
          iAttributes.push_back(WGL_CONTEXT_RESET_NOTIFICATION_STRATEGY_ARB);
          iAttributes.push_back(m_contextResetNotificationStrategy);
        }
        else {
          fprintf(stderr, "Warning! Cannot set the reset notification strategy.");
        }
      }

      iAttributes.push_back(0);

      m_hGLRC = ::wglCreateContextAttribsARB(m_hDC, NULL, &(iAttributes[0]));
    }
  }

  /* Silence warnings interpreted as errors by users when trying to get
   * a context with version higher than 3.3 Core. */
  {
    const bool silent = m_contextMajorVersion > 3;
    if (!WIN32_CHK_SILENT(m_hGLRC != NULL, silent)) {
      goto error;
    }
  }

  s_sharedCount++;

  if (s_sharedHGLRC == NULL) {
    s_sharedHGLRC = m_hGLRC;
  }
  else if (!WIN32_CHK(::wglShareLists(s_sharedHGLRC, m_hGLRC))) {
    goto error;
  }

  if (!WIN32_CHK(::wglMakeCurrent(m_hDC, m_hGLRC))) {
    goto error;
  }

  if (is_crappy_intel_card()) {
    /* Some Intel cards with context 4.1 or 4.2
     * don't have the point sprite enabled by default.
     *
     * However GL_POINT_SPRITE was removed in 3.2 and is now permanently ON.
     * Then use brute force. */
    glEnable(GL_POINT_SPRITE);
  }

  initClearGL();
  ::SwapBuffers(m_hDC);

#ifndef NDEBUG
  {
    const char *vendor = reinterpret_cast<const char *>(glGetString(GL_VENDOR));
    const char *renderer = reinterpret_cast<const char *>(glGetString(GL_RENDERER));
    const char *version = reinterpret_cast<const char *>(glGetString(GL_VERSION));

    reportContextString("Vendor", m_dummyVendor, vendor);
    reportContextString("Renderer", m_dummyRenderer, renderer);
    reportContextString("Version", m_dummyVersion, version);

    fprintf(stderr, "Context Version: %d.%d\n", m_contextMajorVersion, m_contextMinorVersion);
  }
#endif

  return GHOST_kSuccess;
error:
  ::wglMakeCurrent(prevHDC, prevHGLRC);
  return GHOST_kFailure;
}

GHOST_TSuccess GHOST_ContextWGL::releaseNativeHandles()
{
  GHOST_TSuccess success = m_hGLRC != s_sharedHGLRC || s_sharedCount == 1 ? GHOST_kSuccess :
                                                                            GHOST_kFailure;

  m_hWnd = NULL;
  m_hDC = NULL;

  return success;
}