nvfx: expose GLSL
[mesa/mesa-lb.git] / progs / xdemos / glthreads.c
blobea5474870bdb20ae8aaf0ab04820c611e3f91b9e
1 /*
2 * Copyright (C) 2000 Brian Paul All Rights Reserved.
3 *
4 * Permission is hereby granted, free of charge, to any person obtaining a
5 * copy of this software and associated documentation files (the "Software"),
6 * to deal in the Software without restriction, including without limitation
7 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8 * and/or sell copies of the Software, and to permit persons to whom the
9 * Software is furnished to do so, subject to the following conditions:
11 * The above copyright notice and this permission notice shall be included
12 * in all copies or substantial portions of the Software.
14 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
15 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
17 * BRIAN PAUL BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
18 * AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
19 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
24 * This program tests GLX thread safety.
25 * Command line options:
26 * -p Open a display connection for each thread
27 * -l Enable application-side locking
28 * -n <num threads> Number of threads to create (default is 2)
29 * -display <display name> Specify X display (default is $DISPLAY)
30 * -t Use texture mapping
32 * Brian Paul 20 July 2000
37 * Notes:
38 * - Each thread gets its own GLX context.
40 * - The GLX contexts share texture objects.
42 * - When 't' is pressed to update the texture image, the window/thread which
43 * has input focus is signalled to change the texture. The other threads
44 * should see the updated texture the next time they call glBindTexture.
48 #if defined(PTHREADS) /* defined by Mesa on Linux and other platforms */
50 #include <assert.h>
51 #include <GL/gl.h>
52 #include <GL/glx.h>
53 #include <math.h>
54 #include <stdio.h>
55 #include <stdlib.h>
56 #include <string.h>
57 #include <unistd.h>
58 #include <pthread.h>
62 * Each window/thread/context:
64 struct winthread {
65 Display *Dpy;
66 int Index;
67 pthread_t Thread;
68 Window Win;
69 GLXContext Context;
70 float Angle;
71 int WinWidth, WinHeight;
72 GLboolean NewSize;
73 GLboolean Initialized;
74 GLboolean MakeNewTexture;
78 #define MAX_WINTHREADS 100
79 static struct winthread WinThreads[MAX_WINTHREADS];
80 static int NumWinThreads = 0;
81 static volatile GLboolean ExitFlag = GL_FALSE;
83 static GLboolean MultiDisplays = 0;
84 static GLboolean Locking = 0;
85 static GLboolean Texture = GL_FALSE;
86 static GLuint TexObj = 12;
87 static GLboolean Animate = GL_TRUE;
89 static pthread_mutex_t Mutex;
90 static pthread_cond_t CondVar;
91 static pthread_mutex_t CondMutex;
94 static void
95 Error(const char *msg)
97 fprintf(stderr, "Error: %s\n", msg);
98 exit(1);
102 static void
103 signal_redraw(void)
105 pthread_mutex_lock(&CondMutex);
106 pthread_cond_broadcast(&CondVar);
107 pthread_mutex_unlock(&CondMutex);
111 static void
112 MakeNewTexture(struct winthread *wt)
114 #define TEX_SIZE 128
115 static float step = 0.0;
116 GLfloat image[TEX_SIZE][TEX_SIZE][4];
117 GLint width;
118 int i, j;
120 for (j = 0; j < TEX_SIZE; j++) {
121 for (i = 0; i < TEX_SIZE; i++) {
122 float dt = 5.0 * (j - 0.5 * TEX_SIZE) / TEX_SIZE;
123 float ds = 5.0 * (i - 0.5 * TEX_SIZE) / TEX_SIZE;
124 float r = dt * dt + ds * ds + step;
125 image[j][i][0] =
126 image[j][i][1] =
127 image[j][i][2] = 0.75 + 0.25 * cos(r);
128 image[j][i][3] = 1.0;
132 step += 0.5;
134 glBindTexture(GL_TEXTURE_2D, TexObj);
136 glGetTexLevelParameteriv(GL_TEXTURE_2D, 0, GL_TEXTURE_WIDTH, &width);
137 if (width) {
138 assert(width == TEX_SIZE);
139 /* sub-tex replace */
140 glTexSubImage2D(GL_TEXTURE_2D, 0, 0, 0, TEX_SIZE, TEX_SIZE,
141 GL_RGBA, GL_FLOAT, image);
143 else {
144 /* create new */
145 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
146 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
148 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, TEX_SIZE, TEX_SIZE, 0,
149 GL_RGBA, GL_FLOAT, image);
155 /* draw a colored cube */
156 static void
157 draw_object(void)
159 glPushMatrix();
160 glScalef(0.75, 0.75, 0.75);
162 glColor3f(1, 0, 0);
164 if (Texture) {
165 glBindTexture(GL_TEXTURE_2D, TexObj);
166 glEnable(GL_TEXTURE_2D);
168 else {
169 glDisable(GL_TEXTURE_2D);
172 glBegin(GL_QUADS);
174 /* -X */
175 glColor3f(0, 1, 1);
176 glTexCoord2f(0, 0); glVertex3f(-1, -1, -1);
177 glTexCoord2f(1, 0); glVertex3f(-1, 1, -1);
178 glTexCoord2f(1, 1); glVertex3f(-1, 1, 1);
179 glTexCoord2f(0, 1); glVertex3f(-1, -1, 1);
181 /* +X */
182 glColor3f(1, 0, 0);
183 glTexCoord2f(0, 0); glVertex3f(1, -1, -1);
184 glTexCoord2f(1, 0); glVertex3f(1, 1, -1);
185 glTexCoord2f(1, 1); glVertex3f(1, 1, 1);
186 glTexCoord2f(0, 1); glVertex3f(1, -1, 1);
188 /* -Y */
189 glColor3f(1, 0, 1);
190 glTexCoord2f(0, 0); glVertex3f(-1, -1, -1);
191 glTexCoord2f(1, 0); glVertex3f( 1, -1, -1);
192 glTexCoord2f(1, 1); glVertex3f( 1, -1, 1);
193 glTexCoord2f(0, 1); glVertex3f(-1, -1, 1);
195 /* +Y */
196 glColor3f(0, 1, 0);
197 glTexCoord2f(0, 0); glVertex3f(-1, 1, -1);
198 glTexCoord2f(1, 0); glVertex3f( 1, 1, -1);
199 glTexCoord2f(1, 1); glVertex3f( 1, 1, 1);
200 glTexCoord2f(0, 1); glVertex3f(-1, 1, 1);
202 /* -Z */
203 glColor3f(1, 1, 0);
204 glTexCoord2f(0, 0); glVertex3f(-1, -1, -1);
205 glTexCoord2f(1, 0); glVertex3f( 1, -1, -1);
206 glTexCoord2f(1, 1); glVertex3f( 1, 1, -1);
207 glTexCoord2f(0, 1); glVertex3f(-1, 1, -1);
209 /* +Y */
210 glColor3f(0, 0, 1);
211 glTexCoord2f(0, 0); glVertex3f(-1, -1, 1);
212 glTexCoord2f(1, 0); glVertex3f( 1, -1, 1);
213 glTexCoord2f(1, 1); glVertex3f( 1, 1, 1);
214 glTexCoord2f(0, 1); glVertex3f(-1, 1, 1);
216 glEnd();
218 glPopMatrix();
222 /* signal resize of given window */
223 static void
224 resize(struct winthread *wt, int w, int h)
226 wt->NewSize = GL_TRUE;
227 wt->WinWidth = w;
228 wt->WinHeight = h;
229 if (!Animate)
230 signal_redraw();
235 * We have an instance of this for each thread.
237 static void
238 draw_loop(struct winthread *wt)
240 while (!ExitFlag) {
242 if (Locking)
243 pthread_mutex_lock(&Mutex);
245 glXMakeCurrent(wt->Dpy, wt->Win, wt->Context);
246 if (!wt->Initialized) {
247 printf("glthreads: %d: GL_RENDERER = %s\n", wt->Index,
248 (char *) glGetString(GL_RENDERER));
249 if (Texture /*&& wt->Index == 0*/) {
250 MakeNewTexture(wt);
252 wt->Initialized = GL_TRUE;
255 if (Locking)
256 pthread_mutex_unlock(&Mutex);
258 glEnable(GL_DEPTH_TEST);
260 if (wt->NewSize) {
261 GLfloat w = (float) wt->WinWidth / (float) wt->WinHeight;
262 glViewport(0, 0, wt->WinWidth, wt->WinHeight);
263 glMatrixMode(GL_PROJECTION);
264 glLoadIdentity();
265 glFrustum(-w, w, -1.0, 1.0, 1.5, 10);
266 glMatrixMode(GL_MODELVIEW);
267 glLoadIdentity();
268 glTranslatef(0, 0, -2.5);
269 wt->NewSize = GL_FALSE;
272 if (wt->MakeNewTexture) {
273 MakeNewTexture(wt);
274 wt->MakeNewTexture = GL_FALSE;
277 glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
279 glPushMatrix();
280 glRotatef(wt->Angle, 0, 1, 0);
281 glRotatef(wt->Angle, 1, 0, 0);
282 glScalef(0.7, 0.7, 0.7);
283 draw_object();
284 glPopMatrix();
286 if (Locking)
287 pthread_mutex_lock(&Mutex);
289 glXSwapBuffers(wt->Dpy, wt->Win);
291 if (Locking)
292 pthread_mutex_unlock(&Mutex);
294 if (Animate) {
295 usleep(5000);
297 else {
298 /* wait for signal to draw */
299 pthread_mutex_lock(&CondMutex);
300 pthread_cond_wait(&CondVar, &CondMutex);
301 pthread_mutex_unlock(&CondMutex);
303 wt->Angle += 1.0;
308 static void
309 keypress(XEvent *event, struct winthread *wt)
311 char buf[100];
312 KeySym keySym;
313 XComposeStatus stat;
315 XLookupString(&event->xkey, buf, sizeof(buf), &keySym, &stat);
317 switch (keySym) {
318 case XK_Escape:
319 /* tell all threads to exit */
320 if (!Animate) {
321 signal_redraw();
323 ExitFlag = GL_TRUE;
324 /*printf("exit draw_loop %d\n", wt->Index);*/
325 return;
326 case XK_t:
327 case XK_T:
328 if (Texture) {
329 wt->MakeNewTexture = GL_TRUE;
330 if (!Animate)
331 signal_redraw();
333 break;
334 case XK_a:
335 case XK_A:
336 Animate = !Animate;
337 if (Animate) /* yes, prev Animate state! */
338 signal_redraw();
339 break;
340 case XK_s:
341 case XK_S:
342 if (!Animate)
343 signal_redraw();
344 break;
345 default:
346 ; /* nop */
352 * The main process thread runs this loop.
353 * Single display connection for all threads.
355 static void
356 event_loop(Display *dpy)
358 XEvent event;
359 int i;
361 assert(!MultiDisplays);
363 while (!ExitFlag) {
365 if (Locking) {
366 while (1) {
367 int k;
368 pthread_mutex_lock(&Mutex);
369 k = XPending(dpy);
370 if (k) {
371 XNextEvent(dpy, &event);
372 pthread_mutex_unlock(&Mutex);
373 break;
375 pthread_mutex_unlock(&Mutex);
376 usleep(5000);
379 else {
380 XNextEvent(dpy, &event);
383 switch (event.type) {
384 case ConfigureNotify:
385 /* Find winthread for this event's window */
386 for (i = 0; i < NumWinThreads; i++) {
387 struct winthread *wt = &WinThreads[i];
388 if (event.xconfigure.window == wt->Win) {
389 resize(wt, event.xconfigure.width,
390 event.xconfigure.height);
391 break;
394 break;
395 case KeyPress:
396 for (i = 0; i < NumWinThreads; i++) {
397 struct winthread *wt = &WinThreads[i];
398 if (event.xkey.window == wt->Win) {
399 keypress(&event, wt);
400 break;
403 break;
404 default:
405 /*no-op*/ ;
412 * Separate display connection for each thread.
414 static void
415 event_loop_multi(void)
417 XEvent event;
418 int w = 0;
420 assert(MultiDisplays);
422 while (!ExitFlag) {
423 struct winthread *wt = &WinThreads[w];
424 if (XPending(wt->Dpy)) {
425 XNextEvent(wt->Dpy, &event);
426 switch (event.type) {
427 case ConfigureNotify:
428 resize(wt, event.xconfigure.width, event.xconfigure.height);
429 break;
430 case KeyPress:
431 keypress(&event, wt);
432 break;
433 default:
434 ; /* nop */
437 w = (w + 1) % NumWinThreads;
438 usleep(5000);
445 * we'll call this once for each thread, before the threads are created.
447 static void
448 create_window(struct winthread *wt, GLXContext shareCtx)
450 Window win;
451 GLXContext ctx;
452 int attrib[] = { GLX_RGBA,
453 GLX_RED_SIZE, 1,
454 GLX_GREEN_SIZE, 1,
455 GLX_BLUE_SIZE, 1,
456 GLX_DEPTH_SIZE, 1,
457 GLX_DOUBLEBUFFER,
458 None };
459 int scrnum;
460 XSetWindowAttributes attr;
461 unsigned long mask;
462 Window root;
463 XVisualInfo *visinfo;
464 int width = 160, height = 160;
465 int xpos = (wt->Index % 8) * (width + 10);
466 int ypos = (wt->Index / 8) * (width + 20);
468 scrnum = DefaultScreen(wt->Dpy);
469 root = RootWindow(wt->Dpy, scrnum);
471 visinfo = glXChooseVisual(wt->Dpy, scrnum, attrib);
472 if (!visinfo) {
473 Error("Unable to find RGB, Z, double-buffered visual");
476 /* window attributes */
477 attr.background_pixel = 0;
478 attr.border_pixel = 0;
479 attr.colormap = XCreateColormap(wt->Dpy, root, visinfo->visual, AllocNone);
480 attr.event_mask = StructureNotifyMask | ExposureMask | KeyPressMask;
481 mask = CWBackPixel | CWBorderPixel | CWColormap | CWEventMask;
483 win = XCreateWindow(wt->Dpy, root, xpos, ypos, width, height,
484 0, visinfo->depth, InputOutput,
485 visinfo->visual, mask, &attr);
486 if (!win) {
487 Error("Couldn't create window");
491 XSizeHints sizehints;
492 sizehints.x = xpos;
493 sizehints.y = ypos;
494 sizehints.width = width;
495 sizehints.height = height;
496 sizehints.flags = USSize | USPosition;
497 XSetNormalHints(wt->Dpy, win, &sizehints);
498 XSetStandardProperties(wt->Dpy, win, "glthreads", "glthreads",
499 None, (char **)NULL, 0, &sizehints);
503 ctx = glXCreateContext(wt->Dpy, visinfo, shareCtx, True);
504 if (!ctx) {
505 Error("Couldn't create GLX context");
508 XMapWindow(wt->Dpy, win);
509 XSync(wt->Dpy, 0);
511 /* save the info for this window/context */
512 wt->Win = win;
513 wt->Context = ctx;
514 wt->Angle = 0.0;
515 wt->WinWidth = width;
516 wt->WinHeight = height;
517 wt->NewSize = GL_TRUE;
522 * Called by pthread_create()
524 static void *
525 thread_function(void *p)
527 struct winthread *wt = (struct winthread *) p;
528 draw_loop(wt);
529 return NULL;
534 * called before exit to wait for all threads to finish
536 static void
537 clean_up(void)
539 int i;
541 /* wait for threads to finish */
542 for (i = 0; i < NumWinThreads; i++) {
543 pthread_join(WinThreads[i].Thread, NULL);
546 for (i = 0; i < NumWinThreads; i++) {
547 glXDestroyContext(WinThreads[i].Dpy, WinThreads[i].Context);
548 XDestroyWindow(WinThreads[i].Dpy, WinThreads[i].Win);
553 static void
554 usage(void)
556 printf("glthreads: test of GL thread safety (any key = exit)\n");
557 printf("Usage:\n");
558 printf(" glthreads [options]\n");
559 printf("Options:\n");
560 printf(" -display DISPLAYNAME Specify display string\n");
561 printf(" -n NUMTHREADS Number of threads to create\n");
562 printf(" -p Use a separate display connection for each thread\n");
563 printf(" -l Use application-side locking\n");
564 printf(" -t Enable texturing\n");
565 printf("Keyboard:\n");
566 printf(" Esc Exit\n");
567 printf(" t Change texture image (requires -t option)\n");
568 printf(" a Toggle animation\n");
569 printf(" s Step rotation (when not animating)\n");
574 main(int argc, char *argv[])
576 char *displayName = NULL;
577 int numThreads = 2;
578 Display *dpy = NULL;
579 int i;
580 Status threadStat;
582 if (argc == 1) {
583 usage();
585 else {
586 int i;
587 for (i = 1; i < argc; i++) {
588 if (strcmp(argv[i], "-display") == 0 && i + 1 < argc) {
589 displayName = argv[i + 1];
590 i++;
592 else if (strcmp(argv[i], "-p") == 0) {
593 MultiDisplays = 1;
595 else if (strcmp(argv[i], "-l") == 0) {
596 Locking = 1;
598 else if (strcmp(argv[i], "-t") == 0) {
599 Texture = 1;
601 else if (strcmp(argv[i], "-n") == 0 && i + 1 < argc) {
602 numThreads = atoi(argv[i + 1]);
603 if (numThreads < 1)
604 numThreads = 1;
605 else if (numThreads > MAX_WINTHREADS)
606 numThreads = MAX_WINTHREADS;
607 i++;
609 else {
610 usage();
611 exit(1);
616 if (Locking)
617 printf("glthreads: Using explicit locks around Xlib calls.\n");
618 else
619 printf("glthreads: No explict locking.\n");
621 if (MultiDisplays)
622 printf("glthreads: Per-thread display connections.\n");
623 else
624 printf("glthreads: Single display connection.\n");
627 * VERY IMPORTANT: call XInitThreads() before any other Xlib functions.
629 if (!MultiDisplays) {
630 if (!Locking) {
631 threadStat = XInitThreads();
632 if (threadStat) {
633 printf("XInitThreads() returned %d (success)\n", (int) threadStat);
635 else {
636 printf("XInitThreads() returned 0 (failure- this program may fail)\n");
640 dpy = XOpenDisplay(displayName);
641 if (!dpy) {
642 fprintf(stderr, "Unable to open display %s\n", XDisplayName(displayName));
643 return -1;
647 pthread_mutex_init(&Mutex, NULL);
648 pthread_mutex_init(&CondMutex, NULL);
649 pthread_cond_init(&CondVar, NULL);
651 printf("glthreads: creating windows\n");
653 NumWinThreads = numThreads;
655 /* Create the GLX windows and contexts */
656 for (i = 0; i < numThreads; i++) {
657 GLXContext share;
659 if (MultiDisplays) {
660 WinThreads[i].Dpy = XOpenDisplay(displayName);
661 assert(WinThreads[i].Dpy);
663 else {
664 WinThreads[i].Dpy = dpy;
666 WinThreads[i].Index = i;
667 WinThreads[i].Initialized = GL_FALSE;
669 share = (Texture && i > 0) ? WinThreads[0].Context : 0;
671 create_window(&WinThreads[i], share);
674 printf("glthreads: creating threads\n");
676 /* Create the threads */
677 for (i = 0; i < numThreads; i++) {
678 pthread_create(&WinThreads[i].Thread, NULL, thread_function,
679 (void*) &WinThreads[i]);
680 printf("glthreads: Created thread %p\n", (void *) WinThreads[i].Thread);
683 if (MultiDisplays)
684 event_loop_multi();
685 else
686 event_loop(dpy);
688 clean_up();
690 if (MultiDisplays) {
691 for (i = 0; i < numThreads; i++) {
692 XCloseDisplay(WinThreads[i].Dpy);
695 else {
696 XCloseDisplay(dpy);
699 return 0;
703 #else /* PTHREADS */
706 #include <stdio.h>
709 main(int argc, char *argv[])
711 printf("Sorry, this program wasn't compiled with PTHREADS defined.\n");
712 return 0;
716 #endif /* PTHREADS */