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