Brian Paul | 746e59f | 2004-03-13 18:31:14 +0000 | [diff] [blame] | 1 | /* |
| 2 | * GL_EXT_pixel_buffer_object test |
| 3 | * |
| 4 | * Brian Paul |
| 5 | * 11 March 2004 |
| 6 | */ |
| 7 | |
| 8 | #define GL_GLEXT_PROTOTYPES |
| 9 | |
| 10 | #include <assert.h> |
| 11 | #include <stdio.h> |
| 12 | #include <stdlib.h> |
| 13 | #include <math.h> |
| 14 | #include <GL/glut.h> |
| 15 | |
Brian Paul | 746e59f | 2004-03-13 18:31:14 +0000 | [diff] [blame] | 16 | #include "../util/readtex.c" /* a hack, I know */ |
| 17 | |
| 18 | #define IMAGE_FILE "../images/girl.rgb" |
| 19 | |
| 20 | static int ImgWidth, ImgHeight; |
| 21 | static GLenum ImgFormat; |
| 22 | static GLubyte *Image = NULL; |
| 23 | |
| 24 | static int APosX, APosY; /* simple drawpixels */ |
| 25 | static int BPosX, BPosY; /* read/draw pixels */ |
| 26 | static int CPosX, CPosY; /* copypixels */ |
| 27 | |
| 28 | static GLboolean DrawFront = GL_FALSE; |
| 29 | static GLboolean ScaleAndBias = GL_FALSE; |
| 30 | static GLboolean Benchmark = GL_FALSE; |
| 31 | |
| 32 | static GLuint DrawPBO, TempPBO; |
| 33 | |
| 34 | |
| 35 | static GLenum ReadFormat = GL_RGBA; |
| 36 | static GLenum ReadType = GL_UNSIGNED_BYTE; |
| 37 | |
| 38 | |
| 39 | |
| 40 | |
| 41 | static void |
| 42 | Reset( void ) |
| 43 | { |
| 44 | APosX = 5; APosY = 20; |
| 45 | BPosX = APosX + ImgWidth + 5; BPosY = 20; |
| 46 | CPosX = BPosX + ImgWidth + 5; CPosY = 20; |
| 47 | } |
| 48 | |
| 49 | |
| 50 | static void |
| 51 | PrintString(const char *s) |
| 52 | { |
| 53 | while (*s) { |
| 54 | glutBitmapCharacter(GLUT_BITMAP_8_BY_13, (int) *s); |
| 55 | s++; |
| 56 | } |
| 57 | } |
| 58 | |
| 59 | |
| 60 | static void |
| 61 | SetupPixelTransfer(GLboolean invert) |
| 62 | { |
| 63 | if (invert) { |
| 64 | glPixelTransferf(GL_RED_SCALE, -1.0); |
| 65 | glPixelTransferf(GL_RED_BIAS, 1.0); |
| 66 | glPixelTransferf(GL_GREEN_SCALE, -1.0); |
| 67 | glPixelTransferf(GL_GREEN_BIAS, 1.0); |
| 68 | glPixelTransferf(GL_BLUE_SCALE, -1.0); |
| 69 | glPixelTransferf(GL_BLUE_BIAS, 1.0); |
| 70 | } |
| 71 | else { |
| 72 | glPixelTransferf(GL_RED_SCALE, 1.0); |
| 73 | glPixelTransferf(GL_RED_BIAS, 0.0); |
| 74 | glPixelTransferf(GL_GREEN_SCALE, 1.0); |
| 75 | glPixelTransferf(GL_GREEN_BIAS, 0.0); |
| 76 | glPixelTransferf(GL_BLUE_SCALE, 1.0); |
| 77 | glPixelTransferf(GL_BLUE_BIAS, 0.0); |
| 78 | } |
| 79 | } |
| 80 | |
| 81 | |
| 82 | static void |
| 83 | Display( void ) |
| 84 | { |
| 85 | glClearColor(.3, .3, .3, 1); |
| 86 | glClear( GL_COLOR_BUFFER_BIT ); |
| 87 | |
| 88 | /** Unbind UNPACK pixel buffer before calling glBitmap */ |
| 89 | glBindBufferARB(GL_PIXEL_UNPACK_BUFFER_EXT, 0); |
| 90 | |
| 91 | glRasterPos2i(5, ImgHeight+25); |
| 92 | PrintString("f = toggle front/back s = toggle scale/bias b = benchmark"); |
| 93 | |
| 94 | glRasterPos2i(5, ImgHeight+40); |
Brian Paul | 1fd4a55 | 2004-09-23 17:32:12 +0000 | [diff] [blame] | 95 | PrintString("GL_EXT_pixel_buffer_object test"); |
Brian Paul | 746e59f | 2004-03-13 18:31:14 +0000 | [diff] [blame] | 96 | |
| 97 | /* draw original image */ |
| 98 | glRasterPos2i(APosX, 5); |
| 99 | PrintString("Original"); |
| 100 | glRasterPos2i(APosX, APosY); |
| 101 | glEnable(GL_DITHER); |
| 102 | SetupPixelTransfer(GL_FALSE); |
| 103 | /*** Draw from the DrawPBO */ |
| 104 | glBindBufferARB(GL_PIXEL_UNPACK_BUFFER_EXT, DrawPBO); |
| 105 | glDrawPixels(ImgWidth, ImgHeight, ImgFormat, GL_UNSIGNED_BYTE, 0); |
Brian Paul | c6136ea | 2004-10-31 15:27:12 +0000 | [diff] [blame] | 106 | glBindBufferARB(GL_PIXEL_UNPACK_BUFFER_EXT, 0); |
Brian Paul | 746e59f | 2004-03-13 18:31:14 +0000 | [diff] [blame] | 107 | |
| 108 | /* do readpixels, drawpixels */ |
| 109 | glRasterPos2i(BPosX, 5); |
| 110 | PrintString("Read/DrawPixels"); |
| 111 | SetupPixelTransfer(ScaleAndBias); |
| 112 | /*** read into the Temp PBO */ |
| 113 | glBindBufferARB(GL_PIXEL_PACK_BUFFER_EXT, TempPBO); |
| 114 | if (Benchmark) { |
| 115 | GLint reads = 0; |
| 116 | GLint endTime; |
| 117 | GLint startTime = glutGet(GLUT_ELAPSED_TIME); |
| 118 | GLdouble seconds, pixelsPerSecond; |
| 119 | printf("Benchmarking...\n"); |
| 120 | do { |
| 121 | glReadPixels(APosX, APosY, ImgWidth, ImgHeight, |
| 122 | ReadFormat, ReadType, 0); |
| 123 | reads++; |
| 124 | endTime = glutGet(GLUT_ELAPSED_TIME); |
| 125 | } while (endTime - startTime < 4000); /* 4 seconds */ |
| 126 | seconds = (double) (endTime - startTime) / 1000.0; |
| 127 | pixelsPerSecond = reads * ImgWidth * ImgHeight / seconds; |
| 128 | printf("Result: %d reads in %f seconds = %f pixels/sec\n", |
| 129 | reads, seconds, pixelsPerSecond); |
| 130 | Benchmark = GL_FALSE; |
| 131 | } |
| 132 | else { |
| 133 | glReadPixels(APosX, APosY, ImgWidth, ImgHeight, |
| 134 | ReadFormat, ReadType, 0); |
| 135 | } |
| 136 | glRasterPos2i(BPosX, BPosY); |
| 137 | glDisable(GL_DITHER); |
| 138 | SetupPixelTransfer(GL_FALSE); |
| 139 | /*** draw from the Temp PBO */ |
| 140 | glBindBufferARB(GL_PIXEL_UNPACK_BUFFER_EXT, TempPBO); |
| 141 | glDrawPixels(ImgWidth, ImgHeight, ReadFormat, ReadType, 0); |
Brian Paul | c6136ea | 2004-10-31 15:27:12 +0000 | [diff] [blame] | 142 | glBindBufferARB(GL_PIXEL_UNPACK_BUFFER_EXT, 0); |
Brian Paul | 746e59f | 2004-03-13 18:31:14 +0000 | [diff] [blame] | 143 | |
| 144 | /* do copypixels */ |
| 145 | glRasterPos2i(CPosX, 5); |
| 146 | PrintString("CopyPixels"); |
| 147 | glRasterPos2i(CPosX, CPosY); |
| 148 | glDisable(GL_DITHER); |
| 149 | SetupPixelTransfer(ScaleAndBias); |
| 150 | glCopyPixels(APosX, APosY, ImgWidth, ImgHeight, GL_COLOR); |
| 151 | |
| 152 | if (!DrawFront) |
| 153 | glutSwapBuffers(); |
| 154 | else |
| 155 | glFinish(); |
| 156 | } |
| 157 | |
| 158 | |
| 159 | static void |
| 160 | Reshape( int width, int height ) |
| 161 | { |
| 162 | glViewport( 0, 0, width, height ); |
| 163 | glMatrixMode( GL_PROJECTION ); |
| 164 | glLoadIdentity(); |
| 165 | glOrtho( 0.0, width, 0.0, height, -1.0, 1.0 ); |
| 166 | glMatrixMode( GL_MODELVIEW ); |
| 167 | glLoadIdentity(); |
| 168 | } |
| 169 | |
| 170 | |
| 171 | static void |
| 172 | Key( unsigned char key, int x, int y ) |
| 173 | { |
| 174 | (void) x; |
| 175 | (void) y; |
| 176 | switch (key) { |
| 177 | case 'b': |
| 178 | Benchmark = GL_TRUE; |
| 179 | break; |
| 180 | case 's': |
| 181 | ScaleAndBias = !ScaleAndBias; |
| 182 | break; |
| 183 | case 'f': |
| 184 | DrawFront = !DrawFront; |
| 185 | if (DrawFront) { |
| 186 | glDrawBuffer(GL_FRONT); |
| 187 | glReadBuffer(GL_FRONT); |
| 188 | } |
| 189 | else { |
| 190 | glDrawBuffer(GL_BACK); |
| 191 | glReadBuffer(GL_BACK); |
| 192 | } |
| 193 | printf("glDrawBuffer(%s)\n", DrawFront ? "GL_FRONT" : "GL_BACK"); |
| 194 | break; |
| 195 | case 27: |
| 196 | exit(0); |
| 197 | break; |
| 198 | } |
| 199 | glutPostRedisplay(); |
| 200 | } |
| 201 | |
| 202 | |
| 203 | static void |
| 204 | Init( GLboolean ciMode ) |
| 205 | { |
| 206 | printf("GL_VERSION = %s\n", (char *) glGetString(GL_VERSION)); |
| 207 | printf("GL_RENDERER = %s\n", (char *) glGetString(GL_RENDERER)); |
| 208 | |
| 209 | if (!glutExtensionSupported("GL_EXT_pixel_buffer_object")) { |
| 210 | printf("Sorry, this demo requires GL_EXT_pixel_buffer_object\n"); |
| 211 | exit(0); |
| 212 | } |
| 213 | |
| 214 | Image = LoadRGBImage( IMAGE_FILE, &ImgWidth, &ImgHeight, &ImgFormat ); |
| 215 | if (!Image) { |
| 216 | printf("Couldn't read %s\n", IMAGE_FILE); |
| 217 | exit(0); |
| 218 | } |
| 219 | |
| 220 | if (ciMode) { |
| 221 | /* Convert RGB image to grayscale */ |
| 222 | GLubyte *indexImage = (GLubyte *) malloc( ImgWidth * ImgHeight ); |
| 223 | GLint i; |
| 224 | for (i=0; i<ImgWidth*ImgHeight; i++) { |
| 225 | int gray = Image[i*3] + Image[i*3+1] + Image[i*3+2]; |
| 226 | indexImage[i] = gray / 3; |
| 227 | } |
| 228 | free(Image); |
| 229 | Image = indexImage; |
| 230 | ImgFormat = GL_COLOR_INDEX; |
| 231 | |
| 232 | for (i=0;i<255;i++) { |
| 233 | float g = i / 255.0; |
| 234 | glutSetColor(i, g, g, g); |
| 235 | } |
| 236 | } |
| 237 | |
| 238 | printf("Loaded %d by %d image\n", ImgWidth, ImgHeight ); |
| 239 | |
| 240 | glPixelStorei(GL_UNPACK_ALIGNMENT, 1); |
| 241 | glPixelStorei(GL_UNPACK_ROW_LENGTH, ImgWidth); |
| 242 | glPixelStorei(GL_PACK_ALIGNMENT, 1); |
| 243 | glPixelStorei(GL_PACK_ROW_LENGTH, ImgWidth); |
| 244 | |
| 245 | Reset(); |
| 246 | |
| 247 | /* put image into DrawPBO */ |
| 248 | glGenBuffersARB(1, &DrawPBO); |
| 249 | glBindBufferARB(GL_PIXEL_UNPACK_BUFFER_EXT, DrawPBO); |
| 250 | glBufferDataARB(GL_PIXEL_UNPACK_BUFFER_EXT, |
| 251 | ImgWidth * ImgHeight * 4, Image, GL_STATIC_DRAW); |
| 252 | |
| 253 | /* Setup TempPBO - used for glReadPixels & glDrawPixels */ |
| 254 | glGenBuffersARB(1, &TempPBO); |
| 255 | glBindBufferARB(GL_PIXEL_PACK_BUFFER_EXT, TempPBO); |
| 256 | glBufferDataARB(GL_PIXEL_PACK_BUFFER_EXT, |
| 257 | ImgWidth * ImgHeight * 4, NULL, GL_DYNAMIC_COPY); |
| 258 | |
| 259 | } |
| 260 | |
| 261 | |
| 262 | int |
| 263 | main( int argc, char *argv[] ) |
| 264 | { |
| 265 | GLboolean ciMode = GL_FALSE; |
| 266 | if (argc > 1 && strcmp(argv[1], "-ci")==0) { |
| 267 | ciMode = GL_TRUE; |
| 268 | } |
| 269 | glutInit( &argc, argv ); |
| 270 | glutInitWindowPosition( 0, 0 ); |
| 271 | glutInitWindowSize( 750, 250 ); |
| 272 | if (ciMode) |
| 273 | glutInitDisplayMode( GLUT_INDEX | GLUT_DOUBLE ); |
| 274 | else |
| 275 | glutInitDisplayMode( GLUT_RGB | GLUT_DOUBLE ); |
| 276 | glutCreateWindow(argv[0]); |
| 277 | Init(ciMode); |
| 278 | glutReshapeFunc( Reshape ); |
| 279 | glutKeyboardFunc( Key ); |
| 280 | glutDisplayFunc( Display ); |
| 281 | glutMainLoop(); |
| 282 | return 0; |
| 283 | } |