blob: 294f9c306071dea6b74138d38bdfda394539d6ca [file] [log] [blame]
Keith Whitwell48e6fff2006-11-01 14:26:10 +00001/*
Brian55d4f322007-10-24 13:55:22 -06002 * GL_ARB_pixel_buffer_object test
Keith Whitwell48e6fff2006-11-01 14:26:10 +00003 *
4 * Command line options:
Brian55d4f322007-10-24 13:55:22 -06005 * -w WIDTH -h HEIGHT sets window size
Keith Whitwell48e6fff2006-11-01 14:26:10 +00006 *
Keith Whitwell48e6fff2006-11-01 14:26:10 +00007 */
8
Keith Whitwell48e6fff2006-11-01 14:26:10 +00009#include <math.h>
10#include <stdio.h>
11#include <stdlib.h>
12#include <string.h>
José Fonseca2e61d132009-01-24 16:39:49 +000013#include <GL/glew.h>
Keith Whitwell48e6fff2006-11-01 14:26:10 +000014#include <GL/glut.h>
15
16#include "readtex.h"
17
18
19#define ANIMATE 10
20#define PBO 11
21#define QUIT 100
22
Brian55d4f322007-10-24 13:55:22 -060023static GLuint DrawPBO;
24
Keith Whitwell48e6fff2006-11-01 14:26:10 +000025static GLboolean Animate = GL_TRUE;
26static GLboolean use_pbo = 1;
27static GLboolean whole_rect = 1;
28
29static GLfloat Drift = 0.0;
30static GLfloat drift_increment = 1/255.0;
31static GLfloat Xrot = 20.0, Yrot = 30.0;
32
33static GLuint Width = 1024;
34static GLuint Height = 512;
35
36
37static void Idle( void )
38{
39 if (Animate) {
40
41 Drift += drift_increment;
42 if (Drift >= 1.0)
43 Drift = 0.0;
44
45 glutPostRedisplay();
46 }
47}
48
Brian55d4f322007-10-24 13:55:22 -060049/*static int max( int a, int b ) { return a > b ? a : b; }*/
José Fonseca7ef8e4e2009-02-12 13:54:20 +000050
51#ifndef min
Keith Whitwell48e6fff2006-11-01 14:26:10 +000052static int min( int a, int b ) { return a < b ? a : b; }
José Fonseca7ef8e4e2009-02-12 13:54:20 +000053#endif
Keith Whitwell48e6fff2006-11-01 14:26:10 +000054
55static void DrawObject()
56{
57 GLint size = Width * Height * 4;
58
59 if (use_pbo) {
60 /* XXX: This is extremely important - semantically makes the buffer
61 * contents undefined, but in practice means that the driver can
62 * release the old copy of the texture and allocate a new one
63 * without waiting for outstanding rendering to complete.
64 */
Brian55d4f322007-10-24 13:55:22 -060065 glBindBufferARB(GL_PIXEL_UNPACK_BUFFER_EXT, DrawPBO);
Keith Whitwell48e6fff2006-11-01 14:26:10 +000066 glBufferDataARB(GL_PIXEL_UNPACK_BUFFER_EXT, size, NULL, GL_STREAM_DRAW_ARB);
67
68 {
69 char *image = glMapBufferARB(GL_PIXEL_UNPACK_BUFFER_EXT, GL_WRITE_ONLY_ARB);
70
71 printf("char %d\n", (unsigned char)(Drift * 255));
72
Brian55d4f322007-10-24 13:55:22 -060073 memset(image, (unsigned char)(Drift * 255), size);
Keith Whitwell48e6fff2006-11-01 14:26:10 +000074
75 glUnmapBufferARB(GL_PIXEL_UNPACK_BUFFER_EXT);
76 }
77
78
79 /* BGRA is required for most hardware paths:
80 */
81 glTexImage2D(GL_TEXTURE_RECTANGLE_ARB, 0, GL_RGBA, Width, Height, 0,
82 GL_BGRA, GL_UNSIGNED_BYTE, NULL);
83 }
84 else {
85 static char *image = NULL;
86
87 if (image == NULL)
88 image = malloc(size);
89
Brian55d4f322007-10-24 13:55:22 -060090 glBindBufferARB(GL_PIXEL_UNPACK_BUFFER_EXT, 0);
91
92 memset(image, (unsigned char)(Drift * 255), size);
Keith Whitwell48e6fff2006-11-01 14:26:10 +000093
94 /* BGRA should be the fast path for regular uploads as well.
95 */
96 glTexImage2D(GL_TEXTURE_RECTANGLE_ARB, 0, GL_RGBA, Width, Height, 0,
97 GL_BGRA, GL_UNSIGNED_BYTE, image);
98 }
99
100 {
101 int x,y,w,h;
102
103 if (whole_rect) {
104 x = y = 0;
105 w = Width;
106 h = Height;
107 }
108 else {
109 x = y = 0;
110 w = min(10, Width);
111 h = min(10, Height);
112 }
113
114 glBegin(GL_QUADS);
115
116 glTexCoord2f( x, y);
117 glVertex2f( x, y );
118
119 glTexCoord2f( x, y + h);
120 glVertex2f( x, y + h);
121
122 glTexCoord2f( x + w + .5, y + h);
123 glVertex2f( x + w, y + h );
124
125 glTexCoord2f( x + w, y + .5);
126 glVertex2f( x + w, y );
127
128 glEnd();
129 }
130}
131
132
133
134static void Display( void )
135{
136 static GLint T0 = 0;
137 static GLint Frames = 0;
138 GLint t;
139
140 glClear( GL_COLOR_BUFFER_BIT );
141
142 glPushMatrix();
143 DrawObject();
144 glPopMatrix();
145
146 glutSwapBuffers();
147
148 Frames++;
149
150 t = glutGet(GLUT_ELAPSED_TIME);
151 if (t - T0 >= 1000) {
152 GLfloat seconds = (t - T0) / 1000.0;
153
154 GLfloat fps = Frames / seconds;
155 printf("%d frames in %6.3f seconds = %6.3f FPS\n", Frames, seconds, fps);
156
157 drift_increment = 2.2 * seconds / Frames;
158 T0 = t;
159 Frames = 0;
160 }
161}
162
163
164static void Reshape( int width, int height )
165{
166 glViewport( 0, 0, width, height );
167 glMatrixMode( GL_PROJECTION );
168 glLoadIdentity();
169/* glFrustum( -1.0, 1.0, -1.0, 1.0, 10.0, 100.0 ); */
170 gluOrtho2D( 0, width, height, 0 );
171 glMatrixMode( GL_MODELVIEW );
172 glLoadIdentity();
173 glTranslatef(0.375, 0.375, 0);
174}
175
176
177static void ModeMenu(int entry)
178{
179 if (entry==ANIMATE) {
180 Animate = !Animate;
181 }
182 else if (entry==PBO) {
183 use_pbo = !use_pbo;
184 }
185 else if (entry==QUIT) {
186 exit(0);
187 }
188
189 glutPostRedisplay();
190}
191
192
193static void Key( unsigned char key, int x, int y )
194{
195 (void) x;
196 (void) y;
197 switch (key) {
198 case 27:
199 exit(0);
200 break;
201 }
202 glutPostRedisplay();
203}
204
205
206static void SpecialKey( int key, int x, int y )
207{
208 float step = 3.0;
209 (void) x;
210 (void) y;
211
212 switch (key) {
213 case GLUT_KEY_UP:
214 Xrot += step;
215 break;
216 case GLUT_KEY_DOWN:
217 Xrot -= step;
218 break;
219 case GLUT_KEY_LEFT:
220 Yrot += step;
221 break;
222 case GLUT_KEY_RIGHT:
223 Yrot -= step;
224 break;
225 }
226 glutPostRedisplay();
227}
228
229
230static void Init( int argc, char *argv[] )
231{
232 const char *exten = (const char *) glGetString(GL_EXTENSIONS);
Brian55d4f322007-10-24 13:55:22 -0600233 GLuint texObj;
Keith Whitwell48e6fff2006-11-01 14:26:10 +0000234 GLint size;
235
236
Brian55d4f322007-10-24 13:55:22 -0600237 if (!strstr(exten, "GL_ARB_pixel_buffer_object")) {
238 printf("Sorry, GL_ARB_pixel_buffer_object not supported by this renderer.\n");
Keith Whitwell48e6fff2006-11-01 14:26:10 +0000239 exit(1);
240 }
241
242 glGetIntegerv(GL_MAX_TEXTURE_SIZE, &size);
243 printf("%d x %d max texture size\n", size, size);
244
245 glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
246
247 /* allocate two texture objects */
248 glGenTextures(1, &texObj);
249
250 /* setup the texture objects */
251 glActiveTextureARB(GL_TEXTURE0_ARB);
252 glBindTexture(GL_TEXTURE_RECTANGLE_ARB, texObj);
253
254 glTexParameteri(GL_TEXTURE_RECTANGLE_ARB, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
255 glTexParameteri(GL_TEXTURE_RECTANGLE_ARB, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
256
257 glGenBuffersARB(1, &DrawPBO);
258
259 glBindBufferARB(GL_PIXEL_UNPACK_BUFFER_EXT, DrawPBO);
260 glBufferDataARB(GL_PIXEL_UNPACK_BUFFER_EXT,
261 Width * Height * 4, NULL, GL_STREAM_DRAW);
262
263 glTexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE);
264
265 glEnable(GL_TEXTURE_RECTANGLE_ARB);
266
267 glShadeModel(GL_SMOOTH);
268 glClearColor(0.3, 0.3, 0.4, 1.0);
269
270 if (argc > 1 && strcmp(argv[1], "-info")==0) {
271 printf("GL_RENDERER = %s\n", (char *) glGetString(GL_RENDERER));
272 printf("GL_VERSION = %s\n", (char *) glGetString(GL_VERSION));
273 printf("GL_VENDOR = %s\n", (char *) glGetString(GL_VENDOR));
274 printf("GL_EXTENSIONS = %s\n", (char *) glGetString(GL_EXTENSIONS));
275 }
276}
277
278
279int main( int argc, char *argv[] )
280{
281 GLint i;
282
283 glutInit( &argc, argv );
284
285 for (i = 1; i < argc; i++) {
286 if (strcmp(argv[i], "-w") == 0) {
287 Width = atoi(argv[i+1]);
288 if (Width <= 0) {
289 printf("Error, bad width\n");
290 exit(1);
291 }
292 i++;
293 }
294 else if (strcmp(argv[i], "-h") == 0) {
295 Height = atoi(argv[i+1]);
296 if (Height <= 0) {
297 printf("Error, bad height\n");
298 exit(1);
299 }
300 i++;
301 }
302 }
303
304 glutInitWindowSize( Width, Height );
305 glutInitWindowPosition( 0, 0 );
306 glutInitDisplayMode( GLUT_RGB | GLUT_DOUBLE );
307 glutCreateWindow(argv[0] );
José Fonseca2e61d132009-01-24 16:39:49 +0000308 glewInit();
Keith Whitwell48e6fff2006-11-01 14:26:10 +0000309
310 Init( argc, argv );
311
312 glutReshapeFunc( Reshape );
313 glutKeyboardFunc( Key );
314 glutSpecialFunc( SpecialKey );
315 glutDisplayFunc( Display );
316 glutIdleFunc( Idle );
317
318 glutCreateMenu(ModeMenu);
319 glutAddMenuEntry("Toggle Animation", ANIMATE);
320 glutAddMenuEntry("Toggle PBO", PBO);
321 glutAddMenuEntry("Quit", QUIT);
322 glutAttachMenu(GLUT_RIGHT_BUTTON);
323
324 glutMainLoop();
325 return 0;
326}