diff --git a/src/java/org/lwjgl/opengl/Pbuffer.java b/src/java/org/lwjgl/opengl/Pbuffer.java index d042d827..6a6aabdb 100644 --- a/src/java/org/lwjgl/opengl/Pbuffer.java +++ b/src/java/org/lwjgl/opengl/Pbuffer.java @@ -134,11 +134,6 @@ public final class Pbuffer { */ public static final int DEPTH_BUFFER = RenderTexture.WGL_DEPTH_COMPONENT_NV; - /** - * Current Pbuffer - */ - private static Pbuffer currentBuffer; - /** * Handle to the native GL rendering context */ @@ -154,18 +149,43 @@ public final class Pbuffer { */ private final int height; + /* + * The Display context that this buffer shares or null + */ + private final Object display_context; + static { Sys.initialize(); } /** - * Construct an instance of a Pbuffer. If this fails then an LWJGLException will be thrown. The buffer is single-buffered. + * Create an instance of a Pbuffer using the Display context. The buffer is double-buffered, like the Display. + *
+ * NOTE: The Pbuffer will use the same context as the Display and requires that the Display has been created. Therefore, + * no pixel format or render to texture parameters can be specified. All OpenGL state, + * including display lists, textures etc. is shared between the Pbuffer and the Display. If the Display is destroyed, + * the Pbuffer will not be usable, even if the Display is created again. + * + * This kind of Pbuffer is the fastest, because the context switch overhead is minimum. + * + * @param width Pbuffer width + * @param height Pbuffer height + */ + public static Pbuffer createPbufferUsingDisplayContext(int width, int height) throws LWJGLException { + if (!Display.isCreated()) + throw new IllegalStateException("The Display must be created before a shared Pbuffer can be created that use the Display context"); + int handle = createPbuffer(true, width, height, null, null); + return new Pbuffer(width, height, Display.getContext(), handle); + } + + /** + * Create an instance of a Pbuffer with a unique OpenGL context. The buffer is single-buffered. * * NOTE: The Pbuffer will have its own context that shares display lists and textures with the Display context (if it is created), * but it will have its own OpenGL state. Therefore, state changes to a pbuffer will not be seen in the window context and vice versa. * - * NOTE: Some OpenGL implementations requires the shared contexts to use the same pixel format. So if possible, use the same - * bpp, alpha, depth and stencil values used to create the main window. + * This kind of Pbuffer is primarily intended for non interactive use, since the makeCurrent context switch will be more expensive + * than a Pbuffer using the Display context. * * The renderTexture parameter defines the necessary state for enabling render-to-texture. When this parameter is null, * render-to-texture is not available. Before using render-to-texture, the Pbuffer capabilities must be queried to ensure that @@ -176,18 +196,27 @@ public final class Pbuffer { * @param pixel_format Minimum Pbuffer context properties * @param renderTexture */ - public Pbuffer(int width, int height, PixelFormat pixel_format, RenderTexture renderTexture) throws LWJGLException { + public static Pbuffer createPbufferUsingUniqueContext(int width, int height, PixelFormat pixel_format, RenderTexture renderTexture) throws LWJGLException { + int handle = createPbuffer(false, width, height, pixel_format, renderTexture); + return new Pbuffer(width, height, null, handle); + } + + private Pbuffer(int width, int height, Object display_context, int handle) { this.width = width; this.height = height; + this.display_context = display_context; + this.handle = handle; + } + private static int createPbuffer(boolean use_display_context, int width, int height, PixelFormat pixel_format, RenderTexture renderTexture) throws LWJGLException { GLContext.loadOpenGLLibrary(); try { if ( renderTexture == null ) - handle = nCreate(width, height, pixel_format, null, null); + return nCreate(use_display_context, width, height, pixel_format, null, null); else - handle = nCreate(width, height, pixel_format, - renderTexture.pixelFormatCaps, - renderTexture.pBufferAttribs); + return nCreate(use_display_context, width, height, pixel_format, + renderTexture.pixelFormatCaps, + renderTexture.pBufferAttribs); } catch (LWJGLException e) { GLContext.unloadOpenGLLibrary(); throw e; @@ -212,12 +241,14 @@ public final class Pbuffer { /** * Method to make the Pbuffer context current. All subsequent OpenGL calls will go to this buffer. - * @throws LWJGLException of the context could not be made current + * @throws LWJGLException if the context could not be made current */ public synchronized void makeCurrent() throws LWJGLException { - currentBuffer = this; + if (display_context != null && display_context != Display.getContext()) + throw new IllegalStateException("Cannot make a Pbuffer invalid after the Display has been destroyed"); nMakeCurrent(handle); - GLContext.useContext(this); + if (display_context == null) + GLContext.useContext(this); } /** @@ -235,7 +266,7 @@ public final class Pbuffer { /** * Native method to create a Pbuffer */ - private static native int nCreate(int width, int height, PixelFormat pixel_format, + private static native int nCreate(boolean shared, int width, int height, PixelFormat pixel_format, IntBuffer pixelFormatCaps, IntBuffer pBufferAttribs) throws LWJGLException; @@ -321,6 +352,4 @@ public final class Pbuffer { public int getWidth() { return width; } - - } diff --git a/src/java/org/lwjgl/test/opengl/PbufferTest.java b/src/java/org/lwjgl/test/opengl/PbufferTest.java index 613dc377..218cf351 100644 --- a/src/java/org/lwjgl/test/opengl/PbufferTest.java +++ b/src/java/org/lwjgl/test/opengl/PbufferTest.java @@ -96,7 +96,7 @@ public class PbufferTest { private void initialize() { try { //find displaymode - pbuffer = new Pbuffer(512, 512, new PixelFormat(), null); + pbuffer = Pbuffer.createPbufferUsingUniqueContext(512, 512, new PixelFormat(), null); mode = findDisplayMode(800, 600, 16); Display.setDisplayMode(mode); // start of in windowed mode @@ -176,7 +176,7 @@ public class PbufferTest { System.out.println("Buffer contents lost - will recreate the buffer"); pbuffer.destroy(); try { - pbuffer = new Pbuffer(512, 512, new PixelFormat(), null); + pbuffer = Pbuffer.createPbufferUsingUniqueContext(512, 512, new PixelFormat(), null); initPbuffer(); } catch (LWJGLException e) { e.printStackTrace(); diff --git a/src/native/common/org_lwjgl_opengl_Display.h b/src/native/common/org_lwjgl_opengl_Display.h index 25ccb536..c38e4256 100644 --- a/src/native/common/org_lwjgl_opengl_Display.h +++ b/src/native/common/org_lwjgl_opengl_Display.h @@ -8,15 +8,15 @@ extern "C" { #endif /* Inaccessible static: current_mode */ +/* Inaccessible static: initial_mode */ /* Inaccessible static: timeNow */ /* Inaccessible static: timeThen */ /* Inaccessible static: title */ /* Inaccessible static: fullscreen */ +/* Inaccessible static: vsync */ /* Inaccessible static: vbo_tracker */ /* Inaccessible static: context */ -/* Inaccessible static: createdMouse */ -/* Inaccessible static: createdKeyboard */ -/* Inaccessible static: createdController */ +/* Inaccessible static: timeLate */ /* * Class: org_lwjgl_opengl_Display * Method: nGetAvailableDisplayModes diff --git a/src/native/common/org_lwjgl_opengl_Pbuffer.h b/src/native/common/org_lwjgl_opengl_Pbuffer.h index 619b066b..0f71f6dd 100644 --- a/src/native/common/org_lwjgl_opengl_Pbuffer.h +++ b/src/native/common/org_lwjgl_opengl_Pbuffer.h @@ -41,7 +41,6 @@ extern "C" { #define org_lwjgl_opengl_Pbuffer_BACK_RIGHT_BUFFER 8326L #undef org_lwjgl_opengl_Pbuffer_DEPTH_BUFFER #define org_lwjgl_opengl_Pbuffer_DEPTH_BUFFER 8359L -/* Inaccessible static: currentBuffer */ /* * Class: org_lwjgl_opengl_Pbuffer * Method: nIsBufferLost @@ -69,10 +68,10 @@ JNIEXPORT jint JNICALL Java_org_lwjgl_opengl_Pbuffer_getPbufferCaps /* * Class: org_lwjgl_opengl_Pbuffer * Method: nCreate - * Signature: (IILorg/lwjgl/opengl/PixelFormat;Ljava/nio/IntBuffer;Ljava/nio/IntBuffer;)I + * Signature: (ZIILorg/lwjgl/opengl/PixelFormat;Ljava/nio/IntBuffer;Ljava/nio/IntBuffer;)I */ JNIEXPORT jint JNICALL Java_org_lwjgl_opengl_Pbuffer_nCreate - (JNIEnv *, jclass, jint, jint, jobject, jobject, jobject); + (JNIEnv *, jclass, jboolean, jint, jint, jobject, jobject, jobject); /* * Class: org_lwjgl_opengl_Pbuffer diff --git a/src/native/linux/Window.h b/src/native/linux/Window.h index 5dbf20bd..c6cd710f 100644 --- a/src/native/linux/Window.h +++ b/src/native/linux/Window.h @@ -111,6 +111,16 @@ */ extern Window getCurrentWindow(void); + /* + * get the current context + */ + extern GLXContext getCurrentGLXContext(void); + + /* + * get the current GLXFBConfig for the current context + */ + extern GLXFBConfig getCurrentGLXFBConfig(void); + /* * Return true if we are in fullscreen mode */ diff --git a/src/native/linux/org_lwjgl_input_Mouse.cpp b/src/native/linux/org_lwjgl_input_Mouse.cpp index bacd9c24..2fc927c5 100644 --- a/src/native/linux/org_lwjgl_input_Mouse.cpp +++ b/src/native/linux/org_lwjgl_input_Mouse.cpp @@ -139,7 +139,8 @@ static void grabPointer(void) { if (result == GrabSuccess) { pointer_grabbed = true; // make sure we have a centered window - XF86VidModeSetViewPort(getDisplay(), getCurrentScreen(), 0, 0); + if (isFullscreen()) + XF86VidModeSetViewPort(getDisplay(), getCurrentScreen(), 0, 0); XFlush(getDisplay()); } } diff --git a/src/native/linux/org_lwjgl_opengl_Display.cpp b/src/native/linux/org_lwjgl_opengl_Display.cpp index cdb6e6f7..d8473561 100644 --- a/src/native/linux/org_lwjgl_opengl_Display.cpp +++ b/src/native/linux/org_lwjgl_opengl_Display.cpp @@ -57,9 +57,9 @@ #define ERR_MSG_SIZE 1024 static GLXContext context = NULL; // OpenGL rendering context +static GLXFBConfig *configs = NULL; static GLXWindow glx_window; static XVisualInfo *vis_info = NULL; -static GLXFBConfig *configs = NULL; static Atom delete_atom; static Colormap cmap; @@ -85,6 +85,14 @@ static bool async_x_error; static char error_message[ERR_MSG_SIZE]; static Atom warp_atom; +GLXFBConfig getCurrentGLXFBConfig(void) { + return configs[0]; +} + +GLXContext getCurrentGLXContext(void) { + return context; +} + Atom getWarpAtom(void) { return warp_atom; } @@ -274,6 +282,8 @@ JNIEXPORT void JNICALL Java_org_lwjgl_opengl_Display_nSetTitle static void destroyWindow(void) { setRepeatMode(AutoRepeatModeDefault); + if (USEGLX13) + glXDestroyWindow(getDisplay(), glx_window); XDestroyWindow(getDisplay(), current_win); XFreeColormap(getDisplay(), cmap); } @@ -371,13 +381,13 @@ JNIEXPORT void JNICALL Java_org_lwjgl_opengl_Display_nMakeCurrent throwException(env, "Could not make display context current"); } -static void releaseContext(void) { +/*static void releaseContext(void) { if (USEGLX13) glXMakeContextCurrent(getDisplay(), None, None, NULL); else glXMakeCurrent(getDisplay(), None, NULL); } - +*/ int convertToBPE(int bpp) { int bpe; switch (bpp) { @@ -515,9 +525,8 @@ static void dumpVisualInfo(XVisualInfo *vis_info) { } static void destroyContext(void) { - releaseContext(); +// releaseContext(); if (USEGLX13) { - glXDestroyWindow(getDisplay(), glx_window); XFree(configs); configs = NULL; } @@ -528,10 +537,13 @@ static void destroyContext(void) { } static bool initWindowGLX13(JNIEnv *env, jobject pixel_format) { - configs = chooseVisualGLX13(env, pixel_format, true, GLX_WINDOW_BIT, true); + configs = chooseVisualGLX13(env, pixel_format, true, GLX_WINDOW_BIT | GLX_PBUFFER_BIT, true); if (configs == NULL) { - throwException(env, "Could not find a matching pixel format"); - return false; + configs = chooseVisualGLX13(env, pixel_format, true, GLX_WINDOW_BIT, true); + if (configs == NULL) { + throwException(env, "Could not find a matching pixel format"); + return false; + } } context = glXCreateNewContext(getDisplay(), configs[0], GLX_RGBA_TYPE, NULL, True); if (context == NULL) { @@ -556,6 +568,7 @@ static bool initWindowGLX13(JNIEnv *env, jobject pixel_format) { if (!checkXError(env)) { glXDestroyContext(getDisplay(), context); XFree(configs); + XFree(vis_info); return false; } return true; @@ -584,6 +597,7 @@ static bool initWindowGLX(JNIEnv *env, jobject pixel_format) { } if (!checkXError(env)) { glXDestroyContext(getDisplay(), context); + XFree(vis_info); return false; } return true; diff --git a/src/native/linux/org_lwjgl_opengl_Pbuffer.cpp b/src/native/linux/org_lwjgl_opengl_Pbuffer.cpp index a42a2dfa..e6aab2e3 100644 --- a/src/native/linux/org_lwjgl_opengl_Pbuffer.cpp +++ b/src/native/linux/org_lwjgl_opengl_Pbuffer.cpp @@ -48,6 +48,7 @@ typedef struct _PbufferInfo { GLXPbuffer buffer; GLXContext context; + bool use_display_context; } PbufferInfo; JNIEXPORT jboolean JNICALL Java_org_lwjgl_opengl_Pbuffer_nIsBufferLost @@ -60,7 +61,7 @@ JNIEXPORT jboolean JNICALL Java_org_lwjgl_opengl_Pbuffer_nIsBufferLost JNIEXPORT jint JNICALL Java_org_lwjgl_opengl_Pbuffer_getPbufferCaps (JNIEnv *env, jclass clazz) { - // Only support thw GLX 1.3 Pbuffers and ignore the GLX_SGIX_pbuffer extension + // Only support the GLX 1.3 Pbuffers and ignore the GLX_SGIX_pbuffer extension return extgl_Extensions.GLX13 ? org_lwjgl_opengl_Pbuffer_PBUFFER_SUPPORTED : 0; } @@ -68,13 +69,85 @@ static void destroyPbuffer(PbufferInfo *buffer_info) { GLXPbuffer buffer = buffer_info->buffer; GLXContext context = buffer_info->context; glXDestroyPbuffer(getDisplay(), buffer); - glXDestroyContext(getDisplay(), context); + if (!buffer_info->use_display_context) + glXDestroyContext(getDisplay(), context); free(buffer_info); decDisplay(); } -JNIEXPORT jint JNICALL Java_org_lwjgl_opengl_Pbuffer_nCreate(JNIEnv *env, jclass clazz, jint width, jint height, jobject pixel_format, - jobject pixelFormatCaps, jobject pBufferAttribs) +static bool checkPbufferCaps(JNIEnv *env, GLXFBConfig config, int width, int height) { + int max; + int result = glXGetFBConfigAttrib(getDisplay(), config, GLX_MAX_PBUFFER_WIDTH, &max); + if (result != Success) { + throwException(env, "Could not get GLX_MAX_PBUFFER_WIDTH from configuration"); + return false; + } + if (max < width) { + throwException(env, "Width too large"); + return false; + } + result = glXGetFBConfigAttrib(getDisplay(), config, GLX_MAX_PBUFFER_HEIGHT, &max); + if (result != Success) { + throwException(env, "Could not get GLX_MAX_PBUFFER_WIDTH from configuration"); + return false; + } + if (max < height) { + throwException(env, "Height too large"); + return false; + } + return true; +} + +static bool createPbufferUsingUniqueContext(JNIEnv *env, PbufferInfo *pbuffer_info, jobject pixel_format, int width, int height, const int *buffer_attribs) { + GLXFBConfig *configs = chooseVisualGLX13(env, pixel_format, false, GLX_PBUFFER_BIT, false); + if (configs == NULL) { + throwException(env, "No matching pixel format"); + return false; + } + if (!checkPbufferCaps(env, configs[0], width, height)) { + XFree(configs); + return false; + } + GLXContext context = glXCreateNewContext(getDisplay(), configs[0], GLX_RGBA_TYPE, getCurrentContext(), True); + if (context == NULL) { + XFree(configs); + throwException(env, "Could not create a GLX context"); + return false; + } + jboolean allow_software_acceleration = getBooleanProperty(env, "org.lwjgl.opengl.Window.allowSoftwareOpenGL"); + if (!allow_software_acceleration && glXIsDirect(getDisplay(), context) == False) { + glXDestroyContext(getDisplay(), context); + XFree(configs); + throwException(env, "Could not create a direct GLX context"); + return false; + } + GLXPbuffer buffer = glXCreatePbuffer(getDisplay(), configs[0], buffer_attribs); + XFree(configs); + pbuffer_info->context = context; + pbuffer_info->buffer = buffer; + return true; +} + +static bool createPbufferUsingDisplayContext(JNIEnv *env, PbufferInfo *buffer_info, int width, int height, const int *buffer_attribs) { + if (!checkPbufferCaps(env, getCurrentGLXFBConfig(), width, height)) { + return false; + } + int drawable_type; + if (glXGetFBConfigAttrib(getDisplay(), getCurrentGLXFBConfig(), GLX_DRAWABLE_TYPE, &drawable_type) != Success) { + throwException(env, "Could not get GLX_DRAWABLE_TYPE attribute from Display context"); + return false; + } + if (drawable_type & GLX_PBUFFER_BIT == 0) { + throwException(env, "Display context does not support Pbuffers"); + return false; + } + GLXPbuffer buffer = glXCreatePbuffer(getDisplay(), getCurrentGLXFBConfig(), buffer_attribs); + buffer_info->buffer = buffer; + buffer_info->context = getCurrentGLXContext(); + return true; +} + +JNIEXPORT jint JNICALL Java_org_lwjgl_opengl_Pbuffer_nCreate(JNIEnv *env, jclass clazz, jboolean use_display_context, jint width, jint height, jobject pixel_format, jobject pixelFormatCaps, jobject pBufferAttribs) { Display *disp = incDisplay(env); if (disp == NULL) { @@ -87,48 +160,22 @@ JNIEXPORT jint JNICALL Java_org_lwjgl_opengl_Pbuffer_nCreate(JNIEnv *env, jclass return -1; } - GLXFBConfig *configs = chooseVisualGLX13(env, pixel_format, false, GLX_PBUFFER_BIT, false); - if (configs == 0) { - XFree(configs); - throwException(env, "No matching pixel format"); - return -1; - } - int max; - glXGetFBConfigAttrib(disp, configs[0], GLX_MAX_PBUFFER_WIDTH, &max); - if (max < width) { - XFree(configs); - throwException(env, "Width too large"); - return -1; - } - glXGetFBConfigAttrib(disp, configs[0], GLX_MAX_PBUFFER_HEIGHT, &max); - if (max < height) { - XFree(configs); - throwException(env, "Height too large"); - return -1; - } - GLXContext context = glXCreateNewContext(disp, configs[0], GLX_RGBA_TYPE, getCurrentContext(), True); - if (context == NULL) { - XFree(configs); - throwException(env, "Could not create a GLX context"); - return -1; - } - jboolean allow_software_acceleration = getBooleanProperty(env, "org.lwjgl.opengl.Window.allowSoftwareOpenGL"); - if (!allow_software_acceleration && glXIsDirect(disp, context) == False) { - glXDestroyContext(disp, context); - XFree(configs); - throwException(env, "Could not create a direct GLX context"); - return -1; - } const int buffer_attribs[] = {GLX_PBUFFER_WIDTH, width, GLX_PBUFFER_HEIGHT, height, GLX_PRESERVED_CONTENTS, True, - GLX_LARGEST_PBUFFER, False}; + GLX_LARGEST_PBUFFER, False, + None, None}; - GLXPbuffer buffer = glXCreatePbuffer(disp, configs[0], buffer_attribs); - XFree(configs); PbufferInfo *buffer_info = (PbufferInfo *)malloc(sizeof(PbufferInfo)); - buffer_info->buffer = buffer; - buffer_info->context = context; + buffer_info->use_display_context = use_display_context; + bool result; + if (use_display_context) { + result = createPbufferUsingDisplayContext(env, buffer_info, width, height, buffer_attribs); + } else { + result = createPbufferUsingUniqueContext(env, buffer_info, pixel_format, width, height, buffer_attribs); + } + if (!result) + return -1; if (!checkXError(env)) { destroyPbuffer(buffer_info); return -1;