aboutsummaryrefslogtreecommitdiffstats
path: root/src/classes/com/sun/opengl/impl/windows/wgl/WindowsWGLDrawable.java
blob: 3b69ad3a2978a0e4241a1180a24bbf7a4efa980e (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
/*
 * Copyright (c) 2003 Sun Microsystems, Inc. All Rights Reserved.
 * 
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are
 * met:
 * 
 * - Redistribution of source code must retain the above copyright
 *   notice, this list of conditions and the following disclaimer.
 * 
 * - Redistribution in binary form must reproduce the above copyright
 *   notice, this list of conditions and the following disclaimer in the
 *   documentation and/or other materials provided with the distribution.
 * 
 * Neither the name of Sun Microsystems, Inc. or the names of
 * contributors may be used to endorse or promote products derived from
 * this software without specific prior written permission.
 * 
 * This software is provided "AS IS," without a warranty of any kind. ALL
 * EXPRESS OR IMPLIED CONDITIONS, REPRESENTATIONS AND WARRANTIES,
 * INCLUDING ANY IMPLIED WARRANTY OF MERCHANTABILITY, FITNESS FOR A
 * PARTICULAR PURPOSE OR NON-INFRINGEMENT, ARE HEREBY EXCLUDED. SUN
 * MICROSYSTEMS, INC. ("SUN") AND ITS LICENSORS SHALL NOT BE LIABLE FOR
 * ANY DAMAGES SUFFERED BY LICENSEE AS A RESULT OF USING, MODIFYING OR
 * DISTRIBUTING THIS SOFTWARE OR ITS DERIVATIVES. IN NO EVENT WILL SUN OR
 * ITS LICENSORS BE LIABLE FOR ANY LOST REVENUE, PROFIT OR DATA, OR FOR
 * DIRECT, INDIRECT, SPECIAL, CONSEQUENTIAL, INCIDENTAL OR PUNITIVE
 * DAMAGES, HOWEVER CAUSED AND REGARDLESS OF THE THEORY OF LIABILITY,
 * ARISING OUT OF THE USE OF OR INABILITY TO USE THIS SOFTWARE, EVEN IF
 * SUN HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGES.
 * 
 * You acknowledge that this software is not designed or intended for use
 * in the design, construction, operation or maintenance of any nuclear
 * facility.
 * 
 * Sun gratefully acknowledges that this software was originally authored
 * and developed by Kenneth Bradley Russell and Christopher John Kline.
 */

package com.sun.opengl.impl.windows.wgl;

import javax.media.opengl.*;
import com.sun.opengl.impl.*;

public abstract class WindowsWGLDrawable extends GLDrawableImpl {
  protected static final boolean DEBUG = Debug.debug("WindowsWGLDrawable");

  protected GLCapabilitiesChooser chooser;
  protected boolean pixelFormatChosen;

  // Workaround for problems on Intel 82855 cards
  private int  setPixelFormatFailCount;
  private static final int MAX_SET_PIXEL_FORMAT_FAIL_COUNT = 5;

  protected static final int MAX_PFORMATS = 256;
  protected static final int MAX_ATTRIBS  = 256;

  public WindowsWGLDrawable(GLDrawableFactory factory, NativeWindow comp, boolean realized,
                            GLCapabilities capabilities,
                            GLCapabilitiesChooser chooser) {
    super(factory, comp, realized);
    setChosenGLCapabilities(capabilities);
    this.chooser = chooser;
  }

  public int lockSurface() throws GLException {
    int ret = super.lockSurface();
    if(NativeWindow.LOCK_SURFACE_NOT_READY == ret) {
      if (DEBUG) {
          System.err.println("WindowsWGLDrawable.lockSurface: surface not ready");
      }
      return ret;
    }
    if (!pixelFormatChosen) {
      try {
        choosePixelFormat(true);
        setPixelFormatFailCount = 0;
      } catch (RuntimeException e) {
        if (DEBUG) {
          System.err.println("WindowsWGLDrawable.lockSurface: squelching exception");
          e.printStackTrace();
        }
        // Workaround for problems seen on Intel 82855 cards in particular
        // Make it look like the lockSurface() call didn't succeed
        unlockSurface();
        if (e instanceof GLException) {
          if (++setPixelFormatFailCount == MAX_SET_PIXEL_FORMAT_FAIL_COUNT) {
            setPixelFormatFailCount = 0;
            throw e;
          }
          return NativeWindow.LOCK_SURFACE_NOT_READY;
        } else {
          // Probably a user error in the GLCapabilitiesChooser or similar.
          // Don't propagate non-GLExceptions out because calling code
          // expects to catch only that exception type
          throw new GLException(e);
        }
      }
    }
    return ret;
  }


  protected void choosePixelFormat(boolean onscreen) {
    PIXELFORMATDESCRIPTOR pfd = null;
    int pixelFormat = 0;
    GLCapabilities chosenCaps = null;
    GLCapabilities capabilities = getCapabilities();
    long hdc = getNativeWindow().getSurfaceHandle();
    if (onscreen) {
      if ((pixelFormat = WGL.GetPixelFormat(hdc)) != 0) {
        // The Java2D/OpenGL pipeline probably already set a pixel
        // format for this canvas.
        if (DEBUG) {
          System.err.println("NOTE: pixel format already chosen (by Java2D/OpenGL pipeline?) for window: " + 
                             WGL.GetPixelFormat(hdc));
        }
        pfd = newPixelFormatDescriptor();
        if (WGL.DescribePixelFormat(hdc, pixelFormat, pfd.size(), pfd) == 0) {
          // FIXME: should this just be a warning? Not really critical...
          throw new GLException("Unable to describe pixel format " + pixelFormat +
                                " of window set by Java2D/OpenGL pipeline");
        }
        setChosenGLCapabilities(pfd2GLCapabilities(pfd));
        pixelFormatChosen = true;
        return;
      }

      GLCapabilities[] availableCaps = null;
      int numFormats = 0;
      pfd = newPixelFormatDescriptor();
      // Produce a recommended pixel format selection for the GLCapabilitiesChooser.
      // Use wglChoosePixelFormatARB if user requested multisampling and if we have it available
      WindowsWGLDrawable dummyDrawable = null;
      GLContextImpl     dummyContext  = null;
      WGLExt            dummyWGLExt   = null;
      if (capabilities.getSampleBuffers()) {
        dummyDrawable = new WindowsDummyWGLDrawable(getFactory());
        dummyContext  = (GLContextImpl) dummyDrawable.createContext(null);
        if (dummyContext != null) {
          dummyContext.makeCurrent();
          dummyWGLExt = (WGLExt) dummyContext.getPlatformGLExtensions();
        }
      }      
      int recommendedPixelFormat = -1;
      boolean haveWGLChoosePixelFormatARB = false;
      boolean haveWGLARBMultisample = false;
      boolean gotAvailableCaps = false;
      if (dummyWGLExt != null) {
        try {
          haveWGLChoosePixelFormatARB = dummyWGLExt.isExtensionAvailable("WGL_ARB_pixel_format");
          if (haveWGLChoosePixelFormatARB) {
            haveWGLARBMultisample = dummyWGLExt.isExtensionAvailable("WGL_ARB_multisample");

            int[]   iattributes = new int  [2 * MAX_ATTRIBS];
            int[]   iresults    = new int  [2 * MAX_ATTRIBS];
            float[] fattributes = new float[1];

            if (glCapabilities2iattributes(capabilities,
                                           iattributes,
                                           dummyWGLExt,
                                           false,
                                           null)) {
              int[] pformats = new int[MAX_PFORMATS];
              int[] numFormatsTmp = new int[1];
              if (dummyWGLExt.wglChoosePixelFormatARB(hdc,
                                                      iattributes, 0,
                                                      fattributes, 0,
                                                      MAX_PFORMATS,
                                                      pformats, 0,
                                                      numFormatsTmp, 0)) {
                numFormats = numFormatsTmp[0];
                if (numFormats > 0) {
                  // Remove one-basing of pixel format (added on later)
                  recommendedPixelFormat = pformats[0] - 1;
                  if (DEBUG) {
                    System.err.println(getThreadName() + ": Used wglChoosePixelFormatARB to recommend pixel format " + recommendedPixelFormat);
                  }
                }
              } else {
                if (DEBUG) {
                  System.err.println(getThreadName() + ": wglChoosePixelFormatARB failed: " + WGL.GetLastError() );
                  Thread.dumpStack();
                }
              }
              if (DEBUG) {
                if (recommendedPixelFormat < 0) {
                  System.err.print(getThreadName() + ": wglChoosePixelFormatARB didn't recommend a pixel format");
                  if (capabilities.getSampleBuffers()) {
                    System.err.print(" for multisampled GLCapabilities");
                  }
                  System.err.println();
                }
              }

              // Produce a list of GLCapabilities to give to the
              // GLCapabilitiesChooser.
              // Use wglGetPixelFormatAttribivARB instead of
              // DescribePixelFormat to get higher-precision information
              // about the pixel format (should make the GLCapabilities
              // more precise as well...i.e., remove the
              // "HardwareAccelerated" bit, which is basically
              // meaningless, and put in whether it can render to a
              // window, to a pbuffer, or to a pixmap)
              int niattribs = 0;
              iattributes[0] = WGLExt.WGL_NUMBER_PIXEL_FORMATS;
              if (dummyWGLExt.wglGetPixelFormatAttribivARB(hdc, 0, 0, 1, iattributes, 0, iresults, 0)) {
                numFormats = iresults[0];

                if (DEBUG) {
                  System.err.println("wglGetPixelFormatAttribivARB reported WGL_NUMBER_PIXEL_FORMATS = " + numFormats);
                }

                // Should we be filtering out the pixel formats which aren't
                // applicable, as we are doing here?
                // We don't have enough information in the GLCapabilities to
                // represent those that aren't...
                iattributes[niattribs++] = WGLExt.WGL_DRAW_TO_WINDOW;
                iattributes[niattribs++] = WGLExt.WGL_ACCELERATION;
                iattributes[niattribs++] = WGLExt.WGL_SUPPORT_OPENGL;
                iattributes[niattribs++] = WGLExt.WGL_DEPTH_BITS;
                iattributes[niattribs++] = WGLExt.WGL_STENCIL_BITS;
                iattributes[niattribs++] = WGLExt.WGL_DOUBLE_BUFFER;
                iattributes[niattribs++] = WGLExt.WGL_STEREO;
                iattributes[niattribs++] = WGLExt.WGL_PIXEL_TYPE;
                iattributes[niattribs++] = WGLExt.WGL_RED_BITS;
                iattributes[niattribs++] = WGLExt.WGL_GREEN_BITS;
                iattributes[niattribs++] = WGLExt.WGL_BLUE_BITS;
                iattributes[niattribs++] = WGLExt.WGL_ALPHA_BITS;
                iattributes[niattribs++] = WGLExt.WGL_ACCUM_RED_BITS;
                iattributes[niattribs++] = WGLExt.WGL_ACCUM_GREEN_BITS;
                iattributes[niattribs++] = WGLExt.WGL_ACCUM_BLUE_BITS;
                iattributes[niattribs++] = WGLExt.WGL_ACCUM_ALPHA_BITS;
                if (haveWGLARBMultisample) {
                  iattributes[niattribs++] = WGLExt.WGL_SAMPLE_BUFFERS;
                  iattributes[niattribs++] = WGLExt.WGL_SAMPLES;
                }

                availableCaps = new GLCapabilities[numFormats];
                for (int i = 0; i < numFormats; i++) {
                  if (!dummyWGLExt.wglGetPixelFormatAttribivARB(hdc, i+1, 0, niattribs, iattributes, 0, iresults, 0)) {
                    throw new GLException("Error getting pixel format attributes for pixel format " + (i + 1) + " of device context");
                  }
                  availableCaps[i] = iattributes2GLCapabilities(iattributes, niattribs, iresults, true);
                }
                gotAvailableCaps = true;
              } else {
                long lastErr = WGL.GetLastError();
                // Intel Extreme graphics fails with a zero error code
                if (lastErr != 0) {
                  throw new GLException("Unable to enumerate pixel formats of window using wglGetPixelFormatAttribivARB: error code " + WGL.GetLastError());
                }
              }
            }
          }
        } finally {
          dummyContext.release();
          dummyContext.destroy();
          dummyDrawable.destroy();
        }
      }

      // Fallback path for older cards, in particular Intel Extreme motherboard graphics
      if (!gotAvailableCaps) {
        if (DEBUG) {
          if (!capabilities.getSampleBuffers()) {
            System.err.println(getThreadName() + ": Using ChoosePixelFormat because multisampling not requested");
          } else {
            System.err.println(getThreadName() + ": Using ChoosePixelFormat because no wglChoosePixelFormatARB");
          }
        }
        pfd = glCapabilities2PFD(capabilities, onscreen);
        // Remove one-basing of pixel format (added on later)
        recommendedPixelFormat = WGL.ChoosePixelFormat(hdc, pfd) - 1;
        if (DEBUG) {
          System.err.println(getThreadName() + ": Recommended pixel format = " + recommendedPixelFormat);
        }

        numFormats = WGL.DescribePixelFormat(hdc, 1, 0, null);
        if (numFormats == 0) {
          throw new GLException("Unable to enumerate pixel formats of window " +
                                toHexString(hdc) + " for GLCapabilitiesChooser");
        }
        availableCaps = new GLCapabilities[numFormats];
        for (int i = 0; i < numFormats; i++) {
          if (WGL.DescribePixelFormat(hdc, 1 + i, pfd.size(), pfd) == 0) {
            throw new GLException("Error describing pixel format " + (1 + i) + " of device context");
          }
          availableCaps[i] = pfd2GLCapabilities(pfd);
        }
      }

      // NOTE: officially, should make a copy of all of these
      // GLCapabilities to avoid mutation by the end user during the
      // chooseCapabilities call, but for the time being, assume they
      // won't be changed

      // Supply information to chooser
      pixelFormat = chooser.chooseCapabilities(capabilities, availableCaps, recommendedPixelFormat);
      if ((pixelFormat < 0) || (pixelFormat >= numFormats)) {
        throw new GLException("Invalid result " + pixelFormat +
                              " from GLCapabilitiesChooser (should be between 0 and " +
                              (numFormats - 1) + ")");
      }
      if (DEBUG) {
        System.err.println(getThreadName() + ": Chosen pixel format (" + pixelFormat + "):");
        System.err.println(availableCaps[pixelFormat]);
      }
      chosenCaps = availableCaps[pixelFormat];
      pixelFormat += 1; // one-base the index
      if (WGL.DescribePixelFormat(hdc, pixelFormat, pfd.size(), pfd) == 0) {
        throw new GLException("Error re-describing the chosen pixel format: " + WGL.GetLastError());
      }
    } else {
      // For now, use ChoosePixelFormat for offscreen surfaces until
      // we figure out how to properly choose an offscreen-
      // compatible pixel format
      pfd = glCapabilities2PFD(capabilities, onscreen);
      pixelFormat = WGL.ChoosePixelFormat(hdc, pfd);
    }
    if (!WGL.SetPixelFormat(hdc, pixelFormat, pfd)) {
      long lastError = WGL.GetLastError();
      if (DEBUG) {
        System.err.println(getThreadName() + ": SetPixelFormat failed: current context = " + WGL.wglGetCurrentContext() +
                           ", current DC = " + WGL.wglGetCurrentDC());
        System.err.println(getThreadName() + ": GetPixelFormat(hdc " + toHexString(hdc) + ") returns " + WGL.GetPixelFormat(hdc));
      }
      throw new GLException("Unable to set pixel format " + pixelFormat + " for device context " + toHexString(hdc) + ": error code " + lastError);
    }
    // Reuse the previously-constructed GLCapabilities because it
    // turns out that using DescribePixelFormat on some pixel formats
    // (which, for example, support full-scene antialiasing) for some
    // reason return that they are not OpenGL-capable
    if (chosenCaps != null) {
      setChosenGLCapabilities(chosenCaps);
    } else {
      setChosenGLCapabilities(pfd2GLCapabilities(pfd));
    }
    pixelFormatChosen = true;
  }

  protected static PIXELFORMATDESCRIPTOR glCapabilities2PFD(GLCapabilities caps, boolean onscreen) {
    int colorDepth = (caps.getRedBits() +
                      caps.getGreenBits() +
                      caps.getBlueBits());
    if (colorDepth < 15) {
      throw new GLException("Bit depths < 15 (i.e., non-true-color) not supported");
    }
    PIXELFORMATDESCRIPTOR pfd = newPixelFormatDescriptor();
    int pfdFlags = (WGL.PFD_SUPPORT_OPENGL |
                    WGL.PFD_GENERIC_ACCELERATED);
    if (caps.getDoubleBuffered()) {
      pfdFlags |= WGL.PFD_DOUBLEBUFFER;
    }
    if (onscreen) {
      pfdFlags |= WGL.PFD_DRAW_TO_WINDOW;
    } else {
      pfdFlags |= WGL.PFD_DRAW_TO_BITMAP;
    }
    if (caps.getStereo()) {
      pfdFlags |= WGL.PFD_STEREO;
    }
    pfd.dwFlags(pfdFlags);
    pfd.iPixelType((byte) WGL.PFD_TYPE_RGBA);
    pfd.cColorBits((byte) colorDepth);
    pfd.cRedBits  ((byte) caps.getRedBits());
    pfd.cGreenBits((byte) caps.getGreenBits());
    pfd.cBlueBits ((byte) caps.getBlueBits());
    pfd.cAlphaBits((byte) caps.getAlphaBits());
    int accumDepth = (caps.getAccumRedBits() +
                      caps.getAccumGreenBits() +
                      caps.getAccumBlueBits());
    pfd.cAccumBits     ((byte) accumDepth);
    pfd.cAccumRedBits  ((byte) caps.getAccumRedBits());
    pfd.cAccumGreenBits((byte) caps.getAccumGreenBits());
    pfd.cAccumBlueBits ((byte) caps.getAccumBlueBits());
    pfd.cAccumAlphaBits((byte) caps.getAccumAlphaBits());
    pfd.cDepthBits((byte) caps.getDepthBits());
    pfd.cStencilBits((byte) caps.getStencilBits());
    pfd.iLayerType((byte) WGL.PFD_MAIN_PLANE);
    return pfd;
  }

  protected static PIXELFORMATDESCRIPTOR newPixelFormatDescriptor() {
    PIXELFORMATDESCRIPTOR pfd = PIXELFORMATDESCRIPTOR.create();
    pfd.nSize((short) pfd.size());
    pfd.nVersion((short) 1);
    return pfd;
  }

  protected static GLCapabilities pfd2GLCapabilities(PIXELFORMATDESCRIPTOR pfd) {
    if ((pfd.dwFlags() & WGL.PFD_SUPPORT_OPENGL) == 0) {
      return null;
    }
    GLCapabilities res = new GLCapabilities();
    res.setRedBits       (pfd.cRedBits());
    res.setGreenBits     (pfd.cGreenBits());
    res.setBlueBits      (pfd.cBlueBits());
    res.setAlphaBits     (pfd.cAlphaBits());
    res.setAccumRedBits  (pfd.cAccumRedBits());
    res.setAccumGreenBits(pfd.cAccumGreenBits());
    res.setAccumBlueBits (pfd.cAccumBlueBits());
    res.setAccumAlphaBits(pfd.cAccumAlphaBits());
    res.setDepthBits     (pfd.cDepthBits());
    res.setStencilBits   (pfd.cStencilBits());
    res.setDoubleBuffered((pfd.dwFlags() & WGL.PFD_DOUBLEBUFFER) != 0);
    res.setStereo        ((pfd.dwFlags() & WGL.PFD_STEREO) != 0);
    res.setHardwareAccelerated(((pfd.dwFlags() & WGL.PFD_GENERIC_FORMAT) == 0) ||
			       ((pfd.dwFlags() & WGL.PFD_GENERIC_ACCELERATED) != 0));
    return res;
  }

  protected static boolean glCapabilities2iattributes(GLCapabilities capabilities,
                                                      int[] iattributes,
                                                      WGLExt wglExt,
                                                      boolean pbuffer,
                                                      int[] floatMode) throws GLException {
    if (!wglExt.isExtensionAvailable("WGL_ARB_pixel_format")) {
      return false;
    }

    int niattribs = 0;

    iattributes[niattribs++] = WGLExt.WGL_SUPPORT_OPENGL;
    iattributes[niattribs++] = GL.GL_TRUE;
    if (pbuffer) {
      iattributes[niattribs++] = WGLExt.WGL_DRAW_TO_PBUFFER;
      iattributes[niattribs++] = GL.GL_TRUE;
    } else {
      iattributes[niattribs++] = WGLExt.WGL_DRAW_TO_WINDOW;
      iattributes[niattribs++] = GL.GL_TRUE;
    }

    iattributes[niattribs++] = WGLExt.WGL_DOUBLE_BUFFER;
    if (capabilities.getDoubleBuffered()) {
      iattributes[niattribs++] = GL.GL_TRUE;
    } else {
      iattributes[niattribs++] = GL.GL_FALSE;
    }

    iattributes[niattribs++] = WGLExt.WGL_STEREO;
    if (capabilities.getStereo()) {
      iattributes[niattribs++] = GL.GL_TRUE;
    } else {
      iattributes[niattribs++] = GL.GL_FALSE;
    }
    
    iattributes[niattribs++] = WGLExt.WGL_DEPTH_BITS;
    iattributes[niattribs++] = capabilities.getDepthBits();
    iattributes[niattribs++] = WGLExt.WGL_RED_BITS;
    iattributes[niattribs++] = capabilities.getRedBits();
    iattributes[niattribs++] = WGLExt.WGL_GREEN_BITS;
    iattributes[niattribs++] = capabilities.getGreenBits();
    iattributes[niattribs++] = WGLExt.WGL_BLUE_BITS;
    iattributes[niattribs++] = capabilities.getBlueBits();
    iattributes[niattribs++] = WGLExt.WGL_ALPHA_BITS;
    iattributes[niattribs++] = capabilities.getAlphaBits();
    iattributes[niattribs++] = WGLExt.WGL_STENCIL_BITS;
    iattributes[niattribs++] = capabilities.getStencilBits();
    if (capabilities.getAccumRedBits()   > 0 ||
        capabilities.getAccumGreenBits() > 0 ||
        capabilities.getAccumBlueBits()  > 0 ||
        capabilities.getAccumAlphaBits() > 0) {
      iattributes[niattribs++] = WGLExt.WGL_ACCUM_BITS;
      iattributes[niattribs++] = (capabilities.getAccumRedBits() +
                                  capabilities.getAccumGreenBits() +
                                  capabilities.getAccumBlueBits() +
                                  capabilities.getAccumAlphaBits());
      iattributes[niattribs++] = WGLExt.WGL_ACCUM_RED_BITS;
      iattributes[niattribs++] = capabilities.getAccumRedBits();
      iattributes[niattribs++] = WGLExt.WGL_ACCUM_GREEN_BITS;
      iattributes[niattribs++] = capabilities.getAccumGreenBits();
      iattributes[niattribs++] = WGLExt.WGL_ACCUM_BLUE_BITS;
      iattributes[niattribs++] = capabilities.getAccumBlueBits();
      iattributes[niattribs++] = WGLExt.WGL_ACCUM_ALPHA_BITS;
      iattributes[niattribs++] = capabilities.getAccumAlphaBits();
    }

    if (wglExt.isExtensionAvailable("WGL_ARB_multisample")) {
      if (capabilities.getSampleBuffers()) {
        iattributes[niattribs++] = WGLExt.WGL_SAMPLE_BUFFERS;
        iattributes[niattribs++] = GL.GL_TRUE;
        iattributes[niattribs++] = WGLExt.WGL_SAMPLES;
        iattributes[niattribs++] = capabilities.getNumSamples();
      }
    }

    boolean rtt      = capabilities.getPbufferRenderToTexture();
    boolean rect     = capabilities.getPbufferRenderToTextureRectangle();
    boolean useFloat = capabilities.getPbufferFloatingPointBuffers();
    boolean ati      = false;
    if (pbuffer) {
      // Check some invariants and set up some state
      if (rect && !rtt) {
        throw new GLException("Render-to-texture-rectangle requires render-to-texture to be specified");
      }

      if (rect) {
        if (!wglExt.isExtensionAvailable("GL_NV_texture_rectangle")) {
          throw new GLException("Render-to-texture-rectangle requires GL_NV_texture_rectangle extension");
        }
      }

      if (useFloat) {
        if (!wglExt.isExtensionAvailable("WGL_ATI_pixel_format_float") &&
            !wglExt.isExtensionAvailable("WGL_NV_float_buffer")) {
          throw new GLException("Floating-point pbuffers not supported by this hardware");
        }

        // Prefer NVidia extension over ATI
        if (wglExt.isExtensionAvailable("WGL_NV_float_buffer")) {
          ati = false;
          floatMode[0] = GLPbuffer.NV_FLOAT;
        } else {
          ati = true;
          floatMode[0] = GLPbuffer.ATI_FLOAT;
        }
        if (DEBUG) {
          System.err.println("Using " + (ati ? "ATI" : "NVidia") + " floating-point extension");
        }
      }

      // See whether we need to change the pixel type to support ATI's
      // floating-point pbuffers
      if (useFloat && ati) {
        if (rtt) {
          throw new GLException("Render-to-floating-point-texture not supported on ATI hardware");
        } else {
          iattributes[niattribs++] = WGLExt.WGL_PIXEL_TYPE;
          iattributes[niattribs++] = WGLExt.WGL_TYPE_RGBA_FLOAT;
        }
      } else {
        if (!rtt) {
          // Currently we don't support non-truecolor visuals in the
          // GLCapabilities, so we don't offer the option of making
          // color-index pbuffers.
          iattributes[niattribs++] = WGLExt.WGL_PIXEL_TYPE;
          iattributes[niattribs++] = WGLExt.WGL_TYPE_RGBA;
        }
      }

      if (useFloat && !ati) {
        iattributes[niattribs++] = WGLExt.WGL_FLOAT_COMPONENTS_NV;
        iattributes[niattribs++] = GL.GL_TRUE;
      }

      if (rtt) {
        if (useFloat) {
          assert(!ati);
          if (!rect) {
            throw new GLException("Render-to-floating-point-texture only supported on NVidia hardware with render-to-texture-rectangle");
          }
          iattributes[niattribs++] = WGLExt.WGL_BIND_TO_TEXTURE_RECTANGLE_FLOAT_RGB_NV;
          iattributes[niattribs++] = GL.GL_TRUE;
        } else {
          iattributes[niattribs++] = rect ? WGLExt.WGL_BIND_TO_TEXTURE_RECTANGLE_RGB_NV : WGLExt.WGL_BIND_TO_TEXTURE_RGB;
          iattributes[niattribs++] = GL.GL_TRUE;
        }
      }
    } else {
      iattributes[niattribs++] = WGLExt.WGL_PIXEL_TYPE;
      iattributes[niattribs++] = WGLExt.WGL_TYPE_RGBA;
    }

    return true;
  }

  protected static GLCapabilities iattributes2GLCapabilities(int[] iattribs,
                                                             int niattribs,
                                                             int[] iresults,
                                                             boolean requireRenderToWindow) {
    GLCapabilities res = new GLCapabilities();
    for (int i = 0; i < niattribs; i++) {
      int attr = iattribs[i];
      switch (attr) {
        case WGLExt.WGL_DRAW_TO_WINDOW:
          if (requireRenderToWindow && iresults[i] != GL.GL_TRUE)
            return null;
          break;

        case WGLExt.WGL_DRAW_TO_PBUFFER:
          break;

        case WGLExt.WGL_ACCELERATION:
          res.setHardwareAccelerated(iresults[i] == WGLExt.WGL_FULL_ACCELERATION);
          break;

        case WGLExt.WGL_SUPPORT_OPENGL:
          if (iresults[i] != GL.GL_TRUE)
            return null;
          break;

        case WGLExt.WGL_DEPTH_BITS:
          res.setDepthBits(iresults[i]);
          break;

        case WGLExt.WGL_STENCIL_BITS:
          res.setStencilBits(iresults[i]);
          break;

        case WGLExt.WGL_DOUBLE_BUFFER:
          res.setDoubleBuffered(iresults[i] == GL.GL_TRUE);
          break;

        case WGLExt.WGL_STEREO:
          res.setStereo(iresults[i] == GL.GL_TRUE);
          break;

        case WGLExt.WGL_PIXEL_TYPE:
          // Fail softly with unknown results here
          if (iresults[i] == WGLExt.WGL_TYPE_RGBA||
              iresults[i] == WGLExt.WGL_TYPE_RGBA_FLOAT) {
            res.setPbufferFloatingPointBuffers(true);
          }
          break;

        case WGLExt.WGL_FLOAT_COMPONENTS_NV:
          if (iresults[i] != 0) {
            res.setPbufferFloatingPointBuffers(true);
          }
          break;

        case WGLExt.WGL_RED_BITS:
          res.setRedBits(iresults[i]);
          break;
          
        case WGLExt.WGL_GREEN_BITS:
          res.setGreenBits(iresults[i]);
          break;

        case WGLExt.WGL_BLUE_BITS:
          res.setBlueBits(iresults[i]);
          break;

        case WGLExt.WGL_ALPHA_BITS:
          res.setAlphaBits(iresults[i]);
          break;

        case WGLExt.WGL_ACCUM_RED_BITS:
          res.setAccumRedBits(iresults[i]);
          break;

        case WGLExt.WGL_ACCUM_GREEN_BITS:
          res.setAccumGreenBits(iresults[i]);
          break;

        case WGLExt.WGL_ACCUM_BLUE_BITS:
          res.setAccumBlueBits(iresults[i]);
          break;

        case WGLExt.WGL_ACCUM_ALPHA_BITS:
          res.setAccumAlphaBits(iresults[i]);
          break;

        case WGLExt.WGL_SAMPLE_BUFFERS:
          res.setSampleBuffers(iresults[i] != 0);
          break;

        case WGLExt.WGL_SAMPLES:
          res.setNumSamples(iresults[i]);
          break;

        default:
          throw new GLException("Unknown pixel format attribute " + iattribs[i]);
      }
    }
    return res;
  }

  protected static String getThreadName() {
    return Thread.currentThread().getName();
  }
}