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