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