Convert libGL and DRI drivers to require libdrm.
[mesa-demos.git] / src / tests / pbo.c
bloba74e8e148b3a2dcfcb395ae16f27627f38d7c7ff
1 /*
2 * GL_EXT_pixel_buffer_object test
3 *
4 * Brian Paul
5 * 11 March 2004
6 */
8 #define GL_GLEXT_PROTOTYPES
10 #include <assert.h>
11 #include <stdio.h>
12 #include <stdlib.h>
13 #include <math.h>
14 #include <GL/glut.h>
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;
41 static void
42 Reset( void )
44 APosX = 5; APosY = 20;
45 BPosX = APosX + ImgWidth + 5; BPosY = 20;
46 CPosX = BPosX + ImgWidth + 5; CPosY = 20;
50 static void
51 PrintString(const char *s)
53 while (*s) {
54 glutBitmapCharacter(GLUT_BITMAP_8_BY_13, (int) *s);
55 s++;
60 static void
61 SetupPixelTransfer(GLboolean invert)
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);
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);
82 static void
83 Display( void )
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);
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);
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);
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;
132 else {
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);
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);
152 if (!DrawFront)
153 glutSwapBuffers();
154 else
155 glFinish();
159 static void
160 Reshape( int width, int height )
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();
171 static void
172 Key( unsigned char key, int x, int y )
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);
189 else {
190 glDrawBuffer(GL_BACK);
191 glReadBuffer(GL_BACK);
193 printf("glDrawBuffer(%s)\n", DrawFront ? "GL_FRONT" : "GL_BACK");
194 break;
195 case 27:
196 exit(0);
197 break;
199 glutPostRedisplay();
203 static void
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");
211 exit(0);
214 Image = LoadRGBImage( IMAGE_FILE, &ImgWidth, &ImgHeight, &ImgFormat );
215 if (!Image) {
216 printf("Couldn't read %s\n", IMAGE_FILE);
217 exit(0);
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;
228 free(Image);
229 Image = indexImage;
230 ImgFormat = GL_COLOR_INDEX;
232 for (i=0;i<255;i++) {
233 float g = i / 255.0;
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);
245 Reset();
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) {
267 ciMode = GL_TRUE;
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;