1 /* 2 * Copyright (c) 2003 Sun Microsystems, Inc. All Rights Reserved. 3 * 4 * Redistribution and use in source and binary forms, with or without 5 * modification, are permitted provided that the following conditions are 6 * met: 7 * 8 * - Redistribution of source code must retain the above copyright 9 * notice, this list of conditions and the following disclaimer. 10 * 11 * - Redistribution in binary form must reproduce the above copyright 12 * notice, this list of conditions and the following disclaimer in the 13 * documentation and/or other materials provided with the distribution. 14 * 15 * Neither the name of Sun Microsystems, Inc. or the names of 16 * contributors may be used to endorse or promote products derived from 17 * this software without specific prior written permission. 18 * 19 * This software is provided "AS IS," without a warranty of any kind. ALL 20 * EXPRESS OR IMPLIED CONDITIONS, REPRESENTATIONS AND WARRANTIES, 21 * INCLUDING ANY IMPLIED WARRANTY OF MERCHANTABILITY, FITNESS FOR A 22 * PARTICULAR PURPOSE OR NON-INFRINGEMENT, ARE HEREBY EXCLUDED. SUN 23 * MICROSYSTEMS, INC. ("SUN") AND ITS LICENSORS SHALL NOT BE LIABLE FOR 24 * ANY DAMAGES SUFFERED BY LICENSEE AS A RESULT OF USING, MODIFYING OR 25 * DISTRIBUTING THIS SOFTWARE OR ITS DERIVATIVES. IN NO EVENT WILL SUN OR 26 * ITS LICENSORS BE LIABLE FOR ANY LOST REVENUE, PROFIT OR DATA, OR FOR 27 * DIRECT, INDIRECT, SPECIAL, CONSEQUENTIAL, INCIDENTAL OR PUNITIVE 28 * DAMAGES, HOWEVER CAUSED AND REGARDLESS OF THE THEORY OF LIABILITY, 29 * ARISING OUT OF THE USE OF OR INABILITY TO USE THIS SOFTWARE, EVEN IF 30 * SUN HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGES. 31 * 32 * You acknowledge that this software is not designed or intended for use 33 * in the design, construction, operation or maintenance of any nuclear 34 * facility. 35 * 36 * Sun gratefully acknowledges that this software was originally authored 37 * and developed by Kenneth Bradley Russell and Christopher John Kline. 38 */ 39 40 package com.sun.opengl.impl.windows; 41 42 import javax.media.opengl.*; 43 import com.sun.opengl.impl.*; 44 45 public class WindowsPbufferGLDrawable extends WindowsGLDrawable { 46 private int initWidth; 47 private int initHeight; 48 49 private WGLExt cachedWGLExt; // cached WGLExt instance from parent GLCanvas, 50 // needed to destroy pbuffer 51 private long buffer; // pbuffer handle 52 private int width; 53 private int height; 54 55 private int floatMode; 56 WindowsPbufferGLDrawable(GLCapabilities capabilities, int initialWidth, int initialHeight, WindowsGLDrawable dummyDrawable, WGLExt wglExt)57 public WindowsPbufferGLDrawable(GLCapabilities capabilities, 58 int initialWidth, 59 int initialHeight, 60 WindowsGLDrawable dummyDrawable, 61 WGLExt wglExt) { 62 super(capabilities, null); 63 this.initWidth = initialWidth; 64 this.initHeight = initialHeight; 65 if (initWidth <= 0 || initHeight <= 0) { 66 throw new GLException("Initial width and height of pbuffer must be positive (were (" + 67 initWidth + ", " + initHeight + "))"); 68 } 69 70 if (DEBUG) { 71 System.out.println("Pbuffer caps on init: " + capabilities + 72 (capabilities.getPbufferRenderToTexture() ? " [rtt]" : "") + 73 (capabilities.getPbufferRenderToTextureRectangle() ? " [rect]" : "") + 74 (capabilities.getPbufferFloatingPointBuffers() ? " [float]" : "")); 75 } 76 77 createPbuffer(dummyDrawable.getHDC(), wglExt); 78 } 79 createContext(GLContext shareWith)80 public GLContext createContext(GLContext shareWith) { 81 return new WindowsPbufferGLContext(this, shareWith); 82 } 83 destroy()84 public void destroy() { 85 if (hdc != 0) { 86 // Must release DC and pbuffer 87 // NOTE that since the context is not current, glGetError() can 88 // not be called here, so we skip the use of any composable 89 // pipelines (see WindowsOnscreenGLContext.makeCurrentImpl) 90 WGLExt wglExt = cachedWGLExt; 91 if (wglExt.wglReleasePbufferDCARB(buffer, hdc) == 0) { 92 throw new GLException("Error releasing pbuffer device context: error code " + WGL.GetLastError()); 93 } 94 hdc = 0; 95 if (!wglExt.wglDestroyPbufferARB(buffer)) { 96 throw new GLException("Error destroying pbuffer: error code " + WGL.GetLastError()); 97 } 98 buffer = 0; 99 setChosenGLCapabilities(null); 100 } 101 } 102 setSize(int width, int height)103 public void setSize(int width, int height) { 104 // FIXME 105 throw new GLException("Not yet implemented"); 106 } 107 getWidth()108 public int getWidth() { 109 return width; 110 } 111 getHeight()112 public int getHeight() { 113 return height; 114 } 115 getCapabilities()116 public GLCapabilities getCapabilities() { 117 return capabilities; 118 } 119 getPbuffer()120 public long getPbuffer() { 121 return buffer; 122 } 123 getFloatingPointMode()124 public int getFloatingPointMode() { 125 return floatMode; 126 } 127 swapBuffers()128 public void swapBuffers() throws GLException { 129 // FIXME: this doesn't make sense any more because we don't have 130 // access to our OpenGL context here 131 /* 132 // FIXME: do we need to do anything if the pbuffer is double-buffered? 133 // For now, just grab the pixels for the render-to-texture support. 134 if (rtt && !hasRTT) { 135 if (DEBUG) { 136 System.err.println("Copying pbuffer data to GL_TEXTURE_2D state"); 137 } 138 139 GL gl = getGL(); 140 gl.glCopyTexSubImage2D(textureTarget, 0, 0, 0, 0, 0, width, height); 141 } 142 */ 143 } 144 createPbuffer(long parentHdc, WGLExt wglExt)145 private void createPbuffer(long parentHdc, WGLExt wglExt) { 146 int[] iattributes = new int [2*MAX_ATTRIBS]; 147 float[] fattributes = new float[1]; 148 int[] floatModeTmp = new int[1]; 149 int niattribs = 0; 150 151 if (DEBUG) { 152 System.out.println("Pbuffer parentHdc = " + toHexString(parentHdc)); 153 System.out.println("Pbuffer caps: " + capabilities + 154 (capabilities.getPbufferRenderToTexture() ? " [rtt]" : "") + 155 (capabilities.getPbufferRenderToTextureRectangle() ? " [rect]" : "") + 156 (capabilities.getPbufferFloatingPointBuffers() ? " [float]" : "")); 157 } 158 159 if (!glCapabilities2iattributes(capabilities, 160 iattributes, 161 wglExt, 162 true, 163 floatModeTmp)) { 164 throw new GLException("Pbuffer-related extensions not supported"); 165 } 166 167 floatMode = floatModeTmp[0]; 168 boolean rtt = capabilities.getPbufferRenderToTexture(); 169 boolean rect = capabilities.getPbufferRenderToTextureRectangle(); 170 boolean useFloat = capabilities.getPbufferFloatingPointBuffers(); 171 boolean ati = false; 172 173 if (useFloat) { 174 ati = (floatMode == GLPbuffer.ATI_FLOAT); 175 } 176 177 int[] pformats = new int[MAX_PFORMATS]; 178 int nformats; 179 int[] nformatsTmp = new int[1]; 180 if (!wglExt.wglChoosePixelFormatARB(parentHdc, 181 iattributes, 0, 182 fattributes, 0, 183 MAX_PFORMATS, 184 pformats, 0, 185 nformatsTmp, 0)) { 186 throw new GLException("pbuffer creation error: wglChoosePixelFormatARB() failed"); 187 } 188 nformats = nformatsTmp[0]; 189 if (nformats <= 0) { 190 throw new GLException("pbuffer creation error: Couldn't find a suitable pixel format"); 191 } 192 193 boolean haveMultisample = wglExt.isExtensionAvailable("WGL_ARB_multisample"); 194 195 if (DEBUG) { 196 System.err.println("" + nformats + " suitable pixel formats found"); 197 // query pixel format 198 iattributes[0] = WGLExt.WGL_RED_BITS_ARB; 199 iattributes[1] = WGLExt.WGL_GREEN_BITS_ARB; 200 iattributes[2] = WGLExt.WGL_BLUE_BITS_ARB; 201 iattributes[3] = WGLExt.WGL_ALPHA_BITS_ARB; 202 iattributes[4] = WGLExt.WGL_DEPTH_BITS_ARB; 203 iattributes[5] = (useFloat ? (ati ? WGLExt.WGL_PIXEL_TYPE_ARB : WGLExt.WGL_FLOAT_COMPONENTS_NV) : WGLExt.WGL_RED_BITS_ARB); 204 iattributes[6] = (haveMultisample ? WGLExt.WGL_SAMPLE_BUFFERS_ARB : WGLExt.WGL_RED_BITS_ARB); 205 iattributes[7] = (haveMultisample ? WGLExt.WGL_SAMPLES_ARB : WGLExt.WGL_RED_BITS_ARB); 206 iattributes[8] = WGLExt.WGL_DRAW_TO_PBUFFER_ARB; 207 int[] ivalues = new int[9]; 208 for (int i = 0; i < nformats; i++) { 209 if (!wglExt.wglGetPixelFormatAttribivARB(parentHdc, pformats[i], 0, 9, iattributes, 0, ivalues, 0)) { 210 throw new GLException("Error while querying pixel format " + pformats[i] + 211 "'s (index " + i + "'s) capabilities for debugging"); 212 } 213 System.err.print("pixel format " + pformats[i] + " (index " + i + "): "); 214 System.err.print( "r: " + ivalues[0]); 215 System.err.print(" g: " + ivalues[1]); 216 System.err.print(" b: " + ivalues[2]); 217 System.err.print(" a: " + ivalues[3]); 218 System.err.print(" depth: " + ivalues[4]); 219 if (haveMultisample) { 220 System.err.print(" multisample: " + ivalues[6]); 221 } 222 System.err.print(" samples: " + ivalues[7]); 223 if (useFloat) { 224 if (ati) { 225 if (ivalues[5] == WGLExt.WGL_TYPE_RGBA_FLOAT_ATI) { 226 System.err.print(" [ati float]"); 227 } else if (ivalues[5] != WGLExt.WGL_TYPE_RGBA_ARB) { 228 System.err.print(" [unknown pixel type " + ivalues[5] + "]"); 229 } 230 } else { 231 if (ivalues[5] != 0) { 232 System.err.print(" [float]"); 233 } 234 } 235 } 236 237 if (ivalues[8] != 0) { 238 System.err.print(" [pbuffer]"); 239 } 240 System.err.println(); 241 } 242 } 243 244 long tmpBuffer = 0; 245 int whichFormat = -1; 246 // Loop is a workaround for bugs in NVidia's recent drivers 247 for (whichFormat = 0; whichFormat < nformats; whichFormat++) { 248 int format = pformats[whichFormat]; 249 250 // Create the p-buffer. 251 niattribs = 0; 252 253 if (rtt) { 254 iattributes[niattribs++] = WGLExt.WGL_TEXTURE_FORMAT_ARB; 255 if (useFloat) { 256 iattributes[niattribs++] = WGLExt.WGL_TEXTURE_FLOAT_RGB_NV; 257 } else { 258 iattributes[niattribs++] = WGLExt.WGL_TEXTURE_RGBA_ARB; 259 } 260 261 iattributes[niattribs++] = WGLExt.WGL_TEXTURE_TARGET_ARB; 262 iattributes[niattribs++] = rect ? WGLExt.WGL_TEXTURE_RECTANGLE_NV : WGLExt.WGL_TEXTURE_2D_ARB; 263 264 iattributes[niattribs++] = WGLExt.WGL_MIPMAP_TEXTURE_ARB; 265 iattributes[niattribs++] = GL.GL_FALSE; 266 267 iattributes[niattribs++] = WGLExt.WGL_PBUFFER_LARGEST_ARB; 268 iattributes[niattribs++] = GL.GL_FALSE; 269 } 270 271 iattributes[niattribs++] = 0; 272 273 tmpBuffer = wglExt.wglCreatePbufferARB(parentHdc, format, initWidth, initHeight, iattributes, 0); 274 if (tmpBuffer != 0) { 275 // Done 276 break; 277 } 278 } 279 280 if (tmpBuffer == 0) { 281 throw new GLException("pbuffer creation error: wglCreatePbufferARB() failed: tried " + nformats + 282 " pixel formats, last error was: " + wglGetLastError()); 283 } 284 285 // Get the device context. 286 long tmpHdc = wglExt.wglGetPbufferDCARB(tmpBuffer); 287 if (tmpHdc == 0) { 288 throw new GLException("pbuffer creation error: wglGetPbufferDCARB() failed"); 289 } 290 291 // Set up instance variables 292 buffer = tmpBuffer; 293 hdc = tmpHdc; 294 cachedWGLExt = wglExt; 295 296 // Re-query chosen pixel format 297 { 298 niattribs = 0; 299 iattributes[niattribs++] = WGLExt.WGL_ACCELERATION_ARB; 300 iattributes[niattribs++] = WGLExt.WGL_RED_BITS_ARB; 301 iattributes[niattribs++] = WGLExt.WGL_GREEN_BITS_ARB; 302 iattributes[niattribs++] = WGLExt.WGL_BLUE_BITS_ARB; 303 iattributes[niattribs++] = WGLExt.WGL_ALPHA_BITS_ARB; 304 iattributes[niattribs++] = WGLExt.WGL_DEPTH_BITS_ARB; 305 iattributes[niattribs++] = WGLExt.WGL_STENCIL_BITS_ARB; 306 iattributes[niattribs++] = WGLExt.WGL_DOUBLE_BUFFER_ARB; 307 iattributes[niattribs++] = WGLExt.WGL_STEREO_ARB; 308 iattributes[niattribs++] = WGLExt.WGL_ACCUM_RED_BITS_ARB; 309 iattributes[niattribs++] = WGLExt.WGL_ACCUM_GREEN_BITS_ARB; 310 iattributes[niattribs++] = WGLExt.WGL_ACCUM_BLUE_BITS_ARB; 311 iattributes[niattribs++] = WGLExt.WGL_ACCUM_ALPHA_BITS_ARB; 312 iattributes[niattribs++] = (useFloat ? (ati ? WGLExt.WGL_PIXEL_TYPE_ARB : WGLExt.WGL_FLOAT_COMPONENTS_NV) : WGLExt.WGL_RED_BITS_ARB); 313 iattributes[niattribs++] = (haveMultisample ? WGLExt.WGL_SAMPLE_BUFFERS_ARB : WGLExt.WGL_RED_BITS_ARB); 314 iattributes[niattribs++] = (haveMultisample ? WGLExt.WGL_SAMPLES_ARB : WGLExt.WGL_RED_BITS_ARB); 315 iattributes[niattribs++] = WGLExt.WGL_DRAW_TO_PBUFFER_ARB; 316 int[] ivalues = new int[niattribs]; 317 // FIXME: usually prefer to throw exceptions, but failure here is not critical 318 if (wglExt.wglGetPixelFormatAttribivARB(parentHdc, pformats[whichFormat], 0, niattribs, iattributes, 0, ivalues, 0)) { 319 setChosenGLCapabilities(iattributes2GLCapabilities(iattributes, niattribs, ivalues, false)); 320 } 321 } 322 323 // Determine the actual width and height we were able to create. 324 int[] tmp = new int[1]; 325 wglExt.wglQueryPbufferARB( buffer, WGLExt.WGL_PBUFFER_WIDTH_ARB, tmp, 0 ); 326 width = tmp[0]; 327 wglExt.wglQueryPbufferARB( buffer, WGLExt.WGL_PBUFFER_HEIGHT_ARB, tmp, 0 ); 328 height = tmp[0]; 329 330 if (DEBUG) { 331 System.err.println("Created pbuffer " + width + " x " + height); 332 } 333 } 334 wglGetLastError()335 private static String wglGetLastError() { 336 return WindowsGLDrawableFactory.wglGetLastError(); 337 } 338 } 339