diff options
author | Kenneth Russel <[email protected]> | 2004-04-29 21:50:38 +0000 |
---|---|---|
committer | Kenneth Russel <[email protected]> | 2004-04-29 21:50:38 +0000 |
commit | be5d9ba85bad4cf845778d1bdcc4361d864a4604 (patch) | |
tree | bc0d0083211eff4c12a745eb48ec7efc8d31020f | |
parent | 595577662881914de98204d0ef43e329e365d021 (diff) |
Fixed Issue 79: PBuffers on macintosh fail to glCopyTexSubImage2D
git-svn-id: file:///usr/local/projects/SUN/JOGL/git-svn/svn-server-sync/jogl/trunk@128 232f8b59-042b-4e1e-8c03-345bb8c30851
-rw-r--r-- | make/cgl-macosx.cfg | 6 | ||||
-rw-r--r-- | make/stub_includes/macosx/window-system.c | 6 | ||||
-rw-r--r-- | src/native/jogl/MacOSXWindowSystemInterface.m | 107 | ||||
-rw-r--r-- | src/net/java/games/jogl/impl/macosx/MacOSXOnscreenGLContext.java | 1 | ||||
-rw-r--r-- | src/net/java/games/jogl/impl/macosx/MacOSXPbufferGLContext.java | 117 |
5 files changed, 98 insertions, 139 deletions
diff --git a/make/cgl-macosx.cfg b/make/cgl-macosx.cfg index a933b36d9..b3eab2184 100644 --- a/make/cgl-macosx.cfg +++ b/make/cgl-macosx.cfg @@ -35,10 +35,10 @@ CustomCCode extern void updateContext(void* nsContext, void* nsView); CustomCCode extern void* updateContextRegister(void* nsContext, void* nsView); CustomCCode extern void updateContextUnregister(void* nsContext, void* nsView, void* updater); -CustomCCode extern void* createPBuffer(void* nsContext, int width, int height); +CustomCCode extern void* createPBuffer(int renderTarget, int width, int height); CustomCCode extern Bool destroyPBuffer(void* nsContext, void* pBuffer); -CustomCCode extern int bindPBuffer(void* nsContext, void* pBuffer); -CustomCCode extern void unbindPBuffer(void* nsContext, void* pBuffer, int pBufferTextureName); +CustomCCode extern void setContextPBuffer(void* nsContext, void* pBuffer); +CustomCCode extern void setContextTextureImageToPBuffer(void* nsContext, void* pBuffer, int colorBuffer); CustomCCode extern void* getProcAddress(const char *procName); diff --git a/make/stub_includes/macosx/window-system.c b/make/stub_includes/macosx/window-system.c index 2908b07be..4b3f95649 100644 --- a/make/stub_includes/macosx/window-system.c +++ b/make/stub_includes/macosx/window-system.c @@ -25,9 +25,9 @@ void updateContext(void* nsContext, void* nsView); void* updateContextRegister(void* nsContext, void* nsView); void updateContextUnregister(void* nsContext, void* nsView, void* updater); -void* createPBuffer(void* nsContext, int width, int height); +void* createPBuffer(int renderTarget, int width, int height); Bool destroyPBuffer(void* nsContext, void* pBuffer); -int bindPBuffer(void* nsContext, void* pBuffer); -void unbindPBuffer(void* nsContext, void* pBuffer, int pBufferTextureName); +void setContextPBuffer(void* nsContext, void* pBuffer); +void setContextTextureImageToPBuffer(void* nsContext, void* pBuffer, int colorBuffer); void* getProcAddress(const char *procName); diff --git a/src/native/jogl/MacOSXWindowSystemInterface.m b/src/native/jogl/MacOSXWindowSystemInterface.m index 7d2f317c9..0f7c5aa7e 100644 --- a/src/native/jogl/MacOSXWindowSystemInterface.m +++ b/src/native/jogl/MacOSXWindowSystemInterface.m @@ -155,54 +155,18 @@ void updateContextUnregister(void* context, void* view, void* updater) [contextUpdater release]; } -#ifndef USE_GL_TEXTURE_RECTANGLE_EXT -static int getNextPowerOf2(int number) +void* createPBuffer(int renderTarget, int width, int height) { - if (((number-1) & number) == 0) - { - //ex: 8 -> 0b1000; 8-1=7 -> 0b0111; 0b1000&0b0111 == 0 - return number; - } - int power = 0; - while (number > 0) - { - number = number>>1; - power++; - } - return (1<<power); -} -#endif + // fprintf(stderr, "createPBuffer renderTarget=%d width=%d height=%d\n", renderTarget, width, height); -void* createPBuffer(void* context, int width, int height) -{ -///fprintf(stderr, "createPBuffer context=%p width=%d height=%d\n", context, width, height); -#ifdef AVAILABLE_MAC_OS_X_VERSION_10_3_AND_LATER) - NSOpenGLContext *nsContext = (NSOpenGLContext*)context; - -#ifdef USE_GL_TEXTURE_RECTANGLE_EXT - unsigned long taget = GL_TEXTURE_RECTANGLE_EXT; -#else - unsigned long taget = GL_TEXTURE_2D; // texture size must be a multiple of power of 2 - - width = getNextPowerOf2(width); - height = getNextPowerOf2(height); -#endif - - NSOpenGLPixelBuffer* pBuffer = [[NSOpenGLPixelBuffer alloc] initWithTextureTarget:taget textureInternalFormat:GL_RGBA textureMaxMipMapLevel:0 pixelsWide:width pixelsHigh:height]; - - [nsContext setPixelBuffer:pBuffer cubeMapFace:0 mipMapLevel:0 currentVirtualScreen:0]; - [nsContext update]; - - return pBuffer; -#else - return NULL; -#endif + NSOpenGLPixelBuffer* pBuffer = [[NSOpenGLPixelBuffer alloc] initWithTextureTarget:renderTarget textureInternalFormat:GL_RGBA textureMaxMipMapLevel:0 pixelsWide:width pixelsHigh:height]; + + return pBuffer; } Bool destroyPBuffer(void* context, void* buffer) { //fprintf(stderr, "destroyPBuffer context=%p, buffer=%p\n", context, buffer); -#ifdef AVAILABLE_MAC_OS_X_VERSION_10_3_AND_LATER) NSOpenGLContext *nsContext = (NSOpenGLContext*)context; NSOpenGLPixelBuffer *pBuffer = (NSOpenGLPixelBuffer*)buffer; @@ -213,61 +177,20 @@ Bool destroyPBuffer(void* context, void* buffer) [pBuffer release]; return true; -#else - return false; -#endif } -int bindPBuffer(void* context, void* buffer) -{ -//fprintf(stderr, "bindPBuffer context=%p, buffer=%p\n", context, buffer); -#ifdef AVAILABLE_MAC_OS_X_VERSION_10_3_AND_LATER) - NSOpenGLContext *nsContext = (NSOpenGLContext*)context; - NSOpenGLPixelBuffer *pBuffer = (NSOpenGLPixelBuffer*)buffer; - - glPushMatrix(); - glPushAttrib(GL_CURRENT_BIT|GL_ENABLE_BIT|GL_TEXTURE_BIT|GL_TRANSFORM_BIT); - - GLuint pBufferTextureName; - glGenTextures(1, &pBufferTextureName); - - glBindTexture([pBuffer textureTarget], pBufferTextureName); - - glTexParameteri([pBuffer textureTarget], GL_TEXTURE_MIN_FILTER, GL_NEAREST); - glTexParameteri([pBuffer textureTarget], GL_TEXTURE_MAG_FILTER, GL_NEAREST); - - [nsContext setTextureImageToPixelBuffer:pBuffer colorBuffer:GL_FRONT_LEFT]; - - glTexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE); - glEnable([pBuffer textureTarget]); - -#ifdef USE_GL_TEXTURE_RECTANGLE_EXT - //GLint matrixMode; - //glGetIntegerv(GL_MATRIX_MODE, &matrixMode); - glMatrixMode(GL_TEXTURE); - glLoadIdentity(); - glScalef([pBuffer pixelsWide], [pBuffer pixelsHigh], 1.0f); // GL_TEXTURE_RECTANGLE_EXT wants texture coordinates in texture image space (not 0 to 1) - //glMatrixMode(matrixMode); -#endif - - return pBufferTextureName; -#else - return 0; -#endif +void setContextPBuffer(void* context, void* buffer) { + NSOpenGLContext *nsContext = (NSOpenGLContext*)context; + NSOpenGLPixelBuffer *pBuffer = (NSOpenGLPixelBuffer*)buffer; + + [nsContext setPixelBuffer: pBuffer cubeMapFace: 0 mipMapLevel: 0 currentVirtualScreen: [nsContext currentVirtualScreen]]; } -void unbindPBuffer(void* context, void* buffer, int texture) -{ -//fprintf(stderr, "unbindPBuffer context=%p, buffer=%p\n", context, buffer); -#ifdef AVAILABLE_MAC_OS_X_VERSION_10_3_AND_LATER) - NSOpenGLPixelBuffer *pBuffer = (NSOpenGLPixelBuffer*)buffer; - GLuint pBufferTextureName = (GLuint)texture; - - glDeleteTextures(1, &pBufferTextureName); - - glPopAttrib(); - glPopMatrix(); -#endif +void setContextTextureImageToPBuffer(void* context, void* buffer, int colorBuffer) { + NSOpenGLContext *nsContext = (NSOpenGLContext*)context; + NSOpenGLPixelBuffer *pBuffer = (NSOpenGLPixelBuffer*)buffer; + + [nsContext setTextureImageToPixelBuffer: pBuffer colorBuffer: (unsigned long) colorBuffer]; } #include <mach-o/dyld.h> diff --git a/src/net/java/games/jogl/impl/macosx/MacOSXOnscreenGLContext.java b/src/net/java/games/jogl/impl/macosx/MacOSXOnscreenGLContext.java index cb75fe166..e957092ee 100644 --- a/src/net/java/games/jogl/impl/macosx/MacOSXOnscreenGLContext.java +++ b/src/net/java/games/jogl/impl/macosx/MacOSXOnscreenGLContext.java @@ -107,6 +107,7 @@ public class MacOSXOnscreenGLContext extends MacOSXGLContext { public synchronized GLContext createPbufferContext(GLCapabilities capabilities, int initialWidth, int initialHeight) { MacOSXPbufferGLContext ctx = new MacOSXPbufferGLContext(capabilities, initialWidth, initialHeight); + GLContextShareSet.registerSharing(this, ctx); pbuffersToInstantiate.add(ctx); return ctx; } diff --git a/src/net/java/games/jogl/impl/macosx/MacOSXPbufferGLContext.java b/src/net/java/games/jogl/impl/macosx/MacOSXPbufferGLContext.java index 1edd88238..6ac854090 100644 --- a/src/net/java/games/jogl/impl/macosx/MacOSXPbufferGLContext.java +++ b/src/net/java/games/jogl/impl/macosx/MacOSXPbufferGLContext.java @@ -7,21 +7,18 @@ public class MacOSXPbufferGLContext extends MacOSXGLContext { private static final boolean DEBUG = false; - // see MacOSXWindowSystemInterface.m createPBuffer - private static final boolean USE_GL_TEXTURE_RECTANGLE_EXT = true; - protected int initWidth; protected int initHeight; private long pBuffer; - private int pBufferTextureName; protected int width; protected int height; - // FIXME: kept around because we create the OpenGL context lazily to - // better integrate with the MacOSXGLContext framework - private long nsContextOfParent; + // State for render-to-texture and render-to-texture-rectangle support + private boolean created; + private int textureTarget; // e.g. GL_TEXTURE_2D, GL_TEXTURE_RECTANGLE_NV + private int texture; // actual texture object public MacOSXPbufferGLContext(GLCapabilities capabilities, int initialWidth, int initialHeight) { super(null, capabilities, null, null); @@ -40,11 +37,15 @@ public class MacOSXPbufferGLContext extends MacOSXGLContext { } public void bindPbufferToTexture() { - pBufferTextureName = CGL.bindPBuffer(nsContextOfParent, pBuffer); + GL gl = getGL(); + gl.glBindTexture(textureTarget, texture); + // FIXME: not clear whether this is really necessary, but since + // the API docs seem to imply it is and since it doesn't seem to + // impact performance, leaving it in + CGL.setContextTextureImageToPBuffer(nsContext, pBuffer, GL.GL_FRONT); } public void releasePbufferFromTexture() { - CGL.unbindPBuffer(nsContextOfParent, pBuffer, pBufferTextureName); } public void createPbuffer(long parentView, long parentContext) { @@ -53,38 +54,68 @@ public class MacOSXPbufferGLContext extends MacOSXGLContext { // context is current because otherwise we don't have the cgl // extensions available to us resetGLFunctionAvailability(); + + int renderTarget; + if (capabilities.getOffscreenRenderToTextureRectangle()) { + width = initWidth; + height = initHeight; + renderTarget = GL.GL_TEXTURE_RECTANGLE_EXT; + } else { + width = getNextPowerOf2(initWidth); + height = getNextPowerOf2(initHeight); + renderTarget = GL.GL_TEXTURE_2D; + } - this.pBuffer = CGL.createPBuffer(nsContext, initWidth, initHeight); + this.pBuffer = CGL.createPBuffer(renderTarget, width, height); if (this.pBuffer == 0) { throw new GLException("pbuffer creation error: CGL.createPBuffer() failed"); } - nsContextOfParent = parentContext; - - if (USE_GL_TEXTURE_RECTANGLE_EXT) - { - // GL_TEXTURE_RECTANGLE_EXT - width = initWidth; - height = initHeight; - } - else - { - // GL_TEXTURE_2D - width = getNextPowerOf2(initWidth); - height = getNextPowerOf2(initHeight); - } - if (DEBUG) { System.err.println("Created pbuffer " + width + " x " + height); } } + protected synchronized boolean makeCurrent(Runnable initAction) throws GLException { + created = false; + + if (pBuffer == 0) { + // pbuffer not instantiated yet + return false; + } + + boolean res = super.makeCurrent(initAction); + if (created) { + // Initialize render-to-texture support if requested + boolean rect = capabilities.getOffscreenRenderToTextureRectangle(); + GL gl = getGL(); + if (rect) { + if (!gl.isExtensionAvailable("GL_EXT_texture_rectangle")) { + System.err.println("MacOSXPbufferGLContext: WARNING: GL_EXT_texture_rectangle extension not " + + "supported; skipping requested render_to_texture_rectangle support for pbuffer"); + rect = false; + } + } + textureTarget = (rect ? GL.GL_TEXTURE_RECTANGLE_EXT : GL.GL_TEXTURE_2D); + int[] tmp = new int[1]; + gl.glGenTextures(1, tmp); + texture = tmp[0]; + gl.glBindTexture(textureTarget, texture); + gl.glTexParameteri(textureTarget, GL.GL_TEXTURE_MIN_FILTER, GL.GL_NEAREST); + gl.glTexParameteri(textureTarget, GL.GL_TEXTURE_MAG_FILTER, GL.GL_NEAREST); + gl.glTexParameteri(textureTarget, GL.GL_TEXTURE_WRAP_S, GL.GL_CLAMP_TO_EDGE); + gl.glTexParameteri(textureTarget, GL.GL_TEXTURE_WRAP_T, GL.GL_CLAMP_TO_EDGE); + gl.glCopyTexImage2D(textureTarget, 0, GL.GL_RGB, 0, 0, width, height, 0); + } + return res; + } + public void destroyPBuffer() { if (this.pBuffer != 0) { - CGL.destroyPBuffer(nsContext, pBuffer); + CGL.destroyPBuffer(nsContext, pBuffer); } - this.pBuffer = 0; - + this.pBuffer = 0; + if (DEBUG) { System.err.println("Destroyed pbuffer " + width + " x " + height); } @@ -99,24 +130,28 @@ public class MacOSXPbufferGLContext extends MacOSXGLContext { // resizing of the pbuffer anyway. return false; } - + public void swapBuffers() throws GLException { // FIXME: do we need to do anything if the pbuffer is double-buffered? } - int getNextPowerOf2(int number) - { - if (((number-1) & number) == 0) - { - //ex: 8 -> 0b1000; 8-1=7 -> 0b0111; 0b1000&0b0111 == 0 - return number; - } - int power = 0; - while (number > 0) - { + private int getNextPowerOf2(int number) { + if (((number-1) & number) == 0) { + //ex: 8 -> 0b1000; 8-1=7 -> 0b0111; 0b1000&0b0111 == 0 + return number; + } + int power = 0; + while (number > 0) { number = number>>1; power++; - } - return (1<<power); + } + return (1<<power); + } + + protected void create() { + super.create(); + created = true; + // Must now associate the pbuffer with our newly-created context + CGL.setContextPBuffer(nsContext, pBuffer); } } |