Commits

Ivan Vučica committed 119143a

Finally got the texture grabbing using glXBindTexImageEXT to work. Done on NVIDIA 9400M using proprietary driver 304.64 on Ubuntu 12.04.

  • Participants
  • Parent commits d4fd996

Comments (0)

Files changed (6)

File FPManagedWindow.m

 #import "FPWindowManager.h"
 #import "GLConfiguration.h"
 
-
+#if SWITCH_CONTEXTS
 #define SWITCH_TO_STAGE_CONTEXT \
 	[mainDisplay makeCurrentContext:[_manager glxContext] \
                                onWindow:[_manager stage] \
 	[mainDisplay makeCurrentContext:[_manager glxContext] \
                                onWindow:_window \
                             contextName:[NSString stringWithFormat:@"Window %p", _window]];
+#endif
 
 #define PRINT_CURRENT_CONTEXT \
 	NSLog(@"=====>>> Current context: %@", [mainDisplay glxContextName]);
 	}
 
 	PRINT_CURRENT_CONTEXT;
-#if !SWITCH_CONTEXTS
+#if !SWITCH_CONTEXTS && defined(SWITCH_TO_STAGE_CONTEXT)
 	SWITCH_TO_STAGE_CONTEXT;
 #endif
 
 	SWITCH_TO_STAGE_CONTEXT;
 #endif
 	glDrawBuffer (GL_BACK);
+	glReadBuffer (GL_BACK);
 
-	glEnable(_textureTarget);
+	//glEnable(_textureTarget);
 	//glColor3f((rand() % 100) / 100., (rand() % 100) / 100., (rand() % 100) / 100.);
 	//glColor4f(1,1,1,1);
 	glColor4f(1,1,0.5+sin(_timeAlive*2)/2,1);
-	NSLog(@"%g %g", _timeAlive, 0.5+sin(_timeAlive/5)/2);
-	glBindTexture(_textureTarget, _texture);
-
+	//glBindTexture(_textureTarget, _texture);
+	
+	//[self _setupTestTexture];
 	PRINT_CURRENT_CONTEXT;
-#if 0	
+#if 1
 	glMatrixMode(GL_MODELVIEW);
 	glPushMatrix ();
 		glTranslatef (_x, _y, _z);
 		/*
 		extent_clip (0, 0, mirror->w, mirror->h);
 		*/
-#if 0
+#if 1
 		glBegin(GL_QUADS);
 		/*
 		for (list = mirror->draw_zone; list; list = g_slist_next (list))
 			quad_draw (list->data);
 		*/
 
-		#define INVERTED_Y 1
+		#define INVERTED_Y 0
 		#if INVERTED_Y
 		#define _Y(y) 1-y
 		#else
 		glTexCoord2f(0,_Y(1)); glVertex3f(0,_height,0);
 		glTexCoord2f(1,_Y(1)); glVertex3f(_width, _height, 0);
 		glTexCoord2f(1,_Y(0)); glVertex3f(_width, 0, 0);
-		
 		#undef _Y
 		
 		glEnd();
 		glVertex2f(_width,0);
 		glEnd();
 		glColor4f(1,1,1,1);
-		glEnable(_textureTarget);
+		//glEnable(_textureTarget);
 		glLineWidth(1);
 NSLog(@"___________ WINDOW %d x %d", _width, _height);
 
 	SWITCH_TO_STAGE_CONTEXT;
 #endif
 }
+
+
+-(BOOL)_setupTestTexture
+{
+	int textureData[] = {
+		255, 0, 0,     255, 255, 255,
+		255, 255, 255, 0, 0, 0
+	};
+
+	GLuint _width = 2, _height = 2;
+	GLuint _internalFormat = GL_RGBA;
+
+        GLuint errorID=glGetError();
+        if(errorID != GL_NO_ERROR)
+        {
+                printf("GL ERROR: %d\n", (errorID));
+                return NO;
+        }
+        glBindTexture(GL_TEXTURE_2D, _texture);
+        glTexImage2D(GL_TEXTURE_2D, 0, _internalFormat, (GLsizei)_width, (GLsizei)_height, 0, _internalFormat, GL_UNSIGNED_BYTE, textureData);
+        glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
+        glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
+
+	return YES;	
+}
+
+
+
 static int grabCount = 0;
 -(void)bindTexture
 {
 	{
 		case GLX_TEXTURE_2D_EXT:
 		_textureTarget = GL_TEXTURE_2D;
+		NSLog(@"Texture target 2D");
 		break;
 		case GLX_TEXTURE_RECTANGLE_EXT:
 		_textureTarget = GL_TEXTURE_RECTANGLE;
+		NSLog(@"Texture target rectangle");
 		break;
 		default:
 		NSLog(@"Unknown texture target %d", _textureTarget);
 		glEnable(_textureTarget);
 		glBindTexture(_textureTarget, _texture);
 NSLog(@"Bound texture, now binding drawable %d", _drawable);
-		glXBindTexImageEXT(display, _drawable, GLX_FRONT_EXT, NULL);
+		glXBindTexImageEXT(display, _drawable, GLX_FRONT_LEFT_EXT, NULL);
 NSLog(@"Bound drawable");
-		glDisable(_textureTarget);
+
+		// NOTE: use of non-power-of-two textures might require
+		// use of NEAREST filter.
+		glTexParameteri(_textureTarget, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
+		glTexParameteri(_textureTarget, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
+
+
+		//glDisable(_textureTarget);
 	}
 	else
 	{
 		glBindTexture(_textureTarget, _texture);
 
 		glReadBuffer (GL_FRONT);
-		//glDrawBuffer (GL_FRONT);
+		glDrawBuffer (GL_BACK);
 
 #if HAVE_GL_COPY_TEX_IMAGE_2D
 		// FIXME: we don't actually get any image for some reason
 
 		glDisable(_textureTarget);
 	}
+#if SWITCH_CONTEXTS
 SWITCH_TO_STAGE_CONTEXT;	
+#endif
 NSLog(@"Bound");
 //exit(0);
 }

File FPWindowManager.m

 	XFree(_glxFBConfigs);
 	[mainDisplay makeCurrentContext:NULL
 	                       onWindow:None
-	                    contextName:@"Stage"];
+	                    contextName:@"NULL"];
 	glXDestroyContext(display, _glxContext);
 	XDestroyWindow(display, _stage);
 	XCompositeReleaseOverlayWindow(display, _overlay);
 {
 	FPX11Display * mainDisplay = [FPX11Display mainDisplay];
 	Display * display = [mainDisplay display];
+#if SWITCH_CONTEXTS
 	[mainDisplay makeCurrentContext:_glxContext
 	                       onWindow:_stage
 	                    contextName:@"Stage"];
+#endif
 
 	XSync([[FPX11Display mainDisplay] display], False);
 	NSLog(@"=============== NEXT FRAME ===========");
 	NSLog(@"Starting with context %@", [mainDisplay glxContextName]);
+	glDrawBuffer(GL_BACK);
 	glViewport(0, 0, _width, _height);
 	glClearColor(0.2,0.2,0.4,1);
 	glClear(GL_COLOR_BUFFER_BIT);
 	glEnd();
 #endif	
 
+#if SWITCH_CONTEXTS
 	[mainDisplay makeCurrentContext:_glxContext
 	                       onWindow:_stage
 	                    contextName:@"Stage"];
+#endif
 	XSync([[FPX11Display mainDisplay] display], False);
 	glFlush();
 	
         glXWaitVideoSync( 2, ( sync + 1 ) % 2, &sync );
 #else
 	usleep(1000000/60 * 2); // 30fps
+	//usleep(1000000/60 * 8); // 7.5fps
 #endif
 	/* end vsync */
 }

File GLConfiguration.h

 /// FPManagedWindow
 #define SAFE_MODE 0
 #define USE_GLX1_3 1
-#define SWITCH_CONTEXTS 1
+#define SWITCH_CONTEXTS 0 // doesn't help glXBindTexImage, but helps with glReadPixels
 
 #if !SAFE_MODE
 	#define HAVE_GL_COPY_TEX_IMAGE_2D 0
 {
 	NSAutoreleasePool * pool = [NSAutoreleasePool new];
 
+	NSLog(@"FIREPLACE - Starting");
 	FPWindowManager * t = [FPWindowManager new];
 	if(!t)
 	{

File run-fireplace.sh

+#!/bin/bash
+
+# first start gldb-gui and configure it via options->target to use mode 'remote via tcp/ip' on port 9118
+#if /bin/true ; then
+if /bin/false ; then
+	export LD_PRELOAD=libbugle.so
+	export BUGLE_DEBUGGER=tcp
+	export BUGLE_DEBUGGER_PORT=9118 
+fi
+#/usr/GNUstep/System/Tools/openapp ./Fireplace.app
+./Fireplace.app/Fireplace
 	exit 1
 fi
 #LIBGL_ALWAYS_INDIRECT=1 ./run-xephyr /usr/GNUstep/System/Tools/openapp ./Fireplace.app
-./run-xephyr /usr/GNUstep/System/Tools/openapp ./Fireplace.app
+./run-xephyr ./run-fireplace.sh
+
 
 #./run-xephyr &
 #DISPLAY=:3 /usr/bin/gdb ./Fireplace.app/Fireplace