2 * GL_EXT_pixel_buffer_object test
8 #define GL_GLEXT_PROTOTYPES
16 #include "../util/readtex.c" /* a hack, I know */
18 #define IMAGE_FILE "../images/girl.rgb"
20 static int ImgWidth
, ImgHeight
;
21 static GLenum ImgFormat
;
22 static GLubyte
*Image
= NULL
;
24 static int APosX
, APosY
; /* simple drawpixels */
25 static int BPosX
, BPosY
; /* read/draw pixels */
26 static int CPosX
, CPosY
; /* copypixels */
28 static GLboolean DrawFront
= GL_FALSE
;
29 static GLboolean ScaleAndBias
= GL_FALSE
;
30 static GLboolean Benchmark
= GL_FALSE
;
32 static GLuint DrawPBO
, TempPBO
;
35 static GLenum ReadFormat
= GL_RGBA
;
36 static GLenum ReadType
= GL_UNSIGNED_BYTE
;
44 APosX
= 5; APosY
= 20;
45 BPosX
= APosX
+ ImgWidth
+ 5; BPosY
= 20;
46 CPosX
= BPosX
+ ImgWidth
+ 5; CPosY
= 20;
51 PrintString(const char *s
)
54 glutBitmapCharacter(GLUT_BITMAP_8_BY_13
, (int) *s
);
61 SetupPixelTransfer(GLboolean 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);
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);
85 glClearColor(.3, .3, .3, 1);
86 glClear( GL_COLOR_BUFFER_BIT
);
88 /** Unbind UNPACK pixel buffer before calling glBitmap */
89 glBindBufferARB(GL_PIXEL_UNPACK_BUFFER_EXT
, 0);
91 glRasterPos2i(5, ImgHeight
+25);
92 PrintString("f = toggle front/back s = toggle scale/bias b = benchmark");
94 glRasterPos2i(5, ImgHeight
+40);
95 PrintString("GL_EXT_pixel_buffer_object test");
97 /* draw original image */
98 glRasterPos2i(APosX
, 5);
99 PrintString("Original");
100 glRasterPos2i(APosX
, APosY
);
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);
106 glBindBufferARB(GL_PIXEL_UNPACK_BUFFER_EXT
, 0);
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
);
117 GLint startTime
= glutGet(GLUT_ELAPSED_TIME
);
118 GLdouble seconds
, pixelsPerSecond
;
119 printf("Benchmarking...\n");
121 glReadPixels(APosX
, APosY
, ImgWidth
, ImgHeight
,
122 ReadFormat
, ReadType
, 0);
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
;
133 glReadPixels(APosX
, APosY
, ImgWidth
, ImgHeight
,
134 ReadFormat
, ReadType
, 0);
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);
142 glBindBufferARB(GL_PIXEL_UNPACK_BUFFER_EXT
, 0);
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
);
160 Reshape( int width
, int height
)
162 glViewport( 0, 0, width
, height
);
163 glMatrixMode( GL_PROJECTION
);
165 glOrtho( 0.0, width
, 0.0, height
, -1.0, 1.0 );
166 glMatrixMode( GL_MODELVIEW
);
172 Key( unsigned char key
, int x
, int y
)
181 ScaleAndBias
= !ScaleAndBias
;
184 DrawFront
= !DrawFront
;
186 glDrawBuffer(GL_FRONT
);
187 glReadBuffer(GL_FRONT
);
190 glDrawBuffer(GL_BACK
);
191 glReadBuffer(GL_BACK
);
193 printf("glDrawBuffer(%s)\n", DrawFront
? "GL_FRONT" : "GL_BACK");
204 Init( GLboolean ciMode
)
206 printf("GL_VERSION = %s\n", (char *) glGetString(GL_VERSION
));
207 printf("GL_RENDERER = %s\n", (char *) glGetString(GL_RENDERER
));
209 if (!glutExtensionSupported("GL_EXT_pixel_buffer_object")) {
210 printf("Sorry, this demo requires GL_EXT_pixel_buffer_object\n");
214 Image
= LoadRGBImage( IMAGE_FILE
, &ImgWidth
, &ImgHeight
, &ImgFormat
);
216 printf("Couldn't read %s\n", IMAGE_FILE
);
221 /* Convert RGB image to grayscale */
222 GLubyte
*indexImage
= (GLubyte
*) malloc( ImgWidth
* ImgHeight
);
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;
230 ImgFormat
= GL_COLOR_INDEX
;
232 for (i
=0;i
<255;i
++) {
234 glutSetColor(i
, g
, g
, g
);
238 printf("Loaded %d by %d image\n", ImgWidth
, ImgHeight
);
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
);
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
);
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
);
263 main( int argc
, char *argv
[] )
265 GLboolean ciMode
= GL_FALSE
;
266 if (argc
> 1 && strcmp(argv
[1], "-ci")==0) {
269 glutInit( &argc
, argv
);
270 glutInitWindowPosition( 0, 0 );
271 glutInitWindowSize( 750, 250 );
273 glutInitDisplayMode( GLUT_INDEX
| GLUT_DOUBLE
);
275 glutInitDisplayMode( GLUT_RGB
| GLUT_DOUBLE
);
276 glutCreateWindow(argv
[0]);
278 glutReshapeFunc( Reshape
);
279 glutKeyboardFunc( Key
);
280 glutDisplayFunc( Display
);