ARB prog parser: fix parameter binding type
[mesa.git] / progs / xdemos / sharedtex_mt.c
1 /* $Id: sharedtex.c,v 1.2 2002/01/16 14:32:46 joukj Exp $ */
2
3 /*
4 * Test sharing of display lists and texture objects between GLX contests.
5 * Brian Paul
6 * Summer 2000
7 *
8 *
9 * Copyright (C) 2000 Brian Paul All Rights Reserved.
10 *
11 * Permission is hereby granted, free of charge, to any person obtaining a
12 * copy of this software and associated documentation files (the "Software"),
13 * to deal in the Software without restriction, including without limitation
14 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
15 * and/or sell copies of the Software, and to permit persons to whom the
16 * Software is furnished to do so, subject to the following conditions:
17 *
18 * The above copyright notice and this permission notice shall be included
19 * in all copies or substantial portions of the Software.
20 *
21 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
22 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
23 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
24 * BRIAN PAUL BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
25 * AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
26 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
27 *
28 *
29 * Modified 2009 for multithreading by Thomas Hellstrom.
30 */
31
32
33 #include <GL/gl.h>
34 #include <GL/glx.h>
35 #include <stdio.h>
36 #include <stdlib.h>
37 #include <unistd.h>
38 #include <string.h>
39 #include <pthread.h>
40 #include <X11/X.h>
41
42 struct thread_init_arg {
43 int id;
44 };
45
46 struct window {
47 pthread_mutex_t drawMutex;
48 char DisplayName[1000];
49 Display *Dpy;
50 Window Win;
51 GLXContext Context;
52 float Angle;
53 int Id;
54 XVisualInfo *visInfo;
55 };
56
57
58 #define MAX_WINDOWS 20
59 static struct window Windows[MAX_WINDOWS];
60 static int NumWindows = 0;
61 static int terminate = 0;
62 static GLXContext gCtx;
63 static Display *gDpy;
64 static GLuint Textures[3];
65
66
67
68 static void
69 Error(const char *display, const char *msg)
70 {
71 fprintf(stderr, "Error on display %s - %s\n", display, msg);
72 exit(1);
73 }
74
75
76 static int
77 initMainthread(Display *dpy, const char *displayName)
78 {
79 int scrnum;
80 XVisualInfo *visinfo;
81 int attrib[] = { GLX_RGBA,
82 GLX_RED_SIZE, 1,
83 GLX_GREEN_SIZE, 1,
84 GLX_BLUE_SIZE, 1,
85 GLX_DOUBLEBUFFER,
86 GLX_DEPTH_SIZE, 1,
87 None };
88
89 scrnum = DefaultScreen(dpy);
90 visinfo = glXChooseVisual(dpy, scrnum, attrib);
91 if (!visinfo) {
92 Error(displayName, "Unable to find RGB, double-buffered visual");
93 return -1;
94 }
95 gCtx = glXCreateContext(dpy, visinfo, NULL, True);
96 if (!gCtx) {
97 Error(displayName, "Couldn't create GLX context");
98 return -1;
99 }
100 return 0;
101 }
102
103 static struct window *
104 AddWindow(Display *dpy, const char *displayName, int xpos, int ypos,
105 GLXContext sCtx)
106 {
107 Window win;
108 GLXContext ctx;
109 int attrib[] = { GLX_RGBA,
110 GLX_RED_SIZE, 1,
111 GLX_GREEN_SIZE, 1,
112 GLX_BLUE_SIZE, 1,
113 GLX_DOUBLEBUFFER,
114 GLX_DEPTH_SIZE, 1,
115 None };
116 int scrnum;
117 XSetWindowAttributes attr;
118 unsigned long mask;
119 Window root;
120 XVisualInfo *visinfo;
121 int width = 300, height = 300;
122
123 if (NumWindows >= MAX_WINDOWS)
124 return NULL;
125
126 scrnum = DefaultScreen(dpy);
127 root = RootWindow(dpy, scrnum);
128
129 visinfo = glXChooseVisual(dpy, scrnum, attrib);
130 if (!visinfo) {
131 Error(displayName, "Unable to find RGB, double-buffered visual");
132 return NULL;
133 }
134
135 /* window attributes */
136 attr.background_pixel = 0;
137 attr.border_pixel = 0;
138 attr.colormap = XCreateColormap(dpy, root, visinfo->visual, AllocNone);
139 attr.event_mask = StructureNotifyMask | ExposureMask | KeyPressMask;
140 mask = CWBorderPixel | CWColormap | CWEventMask;
141
142 win = XCreateWindow(dpy, root, xpos, ypos, width, height,
143 0, visinfo->depth, InputOutput,
144 visinfo->visual, mask, &attr);
145 if (!win) {
146 Error(displayName, "Couldn't create window");
147 return NULL;
148 }
149
150 {
151 XSizeHints sizehints;
152 sizehints.x = xpos;
153 sizehints.y = ypos;
154 sizehints.width = width;
155 sizehints.height = height;
156 sizehints.flags = USSize | USPosition;
157 XSetNormalHints(dpy, win, &sizehints);
158 XSetStandardProperties(dpy, win, displayName, displayName,
159 None, (char **)NULL, 0, &sizehints);
160 }
161
162
163 ctx = glXCreateContext(dpy, visinfo,
164 sCtx ? sCtx : NULL, True);
165
166 if (!ctx) {
167 Error(displayName, "Couldn't create GLX context");
168 return NULL;
169 }
170
171 XMapWindow(dpy, win);
172
173 /* save the info for this window */
174 {
175 static int id = 0;
176 struct window *h = &Windows[NumWindows];
177 strcpy(h->DisplayName, displayName);
178 h->Dpy = dpy;
179 h->Win = win;
180 h->Context = ctx;
181 h->Angle = 0.0;
182 h->Id = id++;
183 h->visInfo = visinfo;
184 pthread_mutex_init(&h->drawMutex, NULL);
185 NumWindows++;
186 return &Windows[NumWindows-1];
187 }
188 }
189
190
191 static void
192 InitGLstuff(void)
193
194 {
195 glGenTextures(3, Textures);
196
197 /* setup first texture object */
198 {
199 GLubyte image[16][16][4];
200 GLint i, j;
201 glBindTexture(GL_TEXTURE_2D, Textures[0]);
202
203 /* red/white checkerboard */
204 for (i = 0; i < 16; i++) {
205 for (j = 0; j < 16; j++) {
206 if ((i ^ j) & 1) {
207 image[i][j][0] = 255;
208 image[i][j][1] = 255;
209 image[i][j][2] = 255;
210 image[i][j][3] = 255;
211 }
212 else {
213 image[i][j][0] = 255;
214 image[i][j][1] = 0;
215 image[i][j][2] = 0;
216 image[i][j][3] = 255;
217 }
218 }
219 }
220
221 glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
222 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, 16, 16, 0, GL_RGBA,
223 GL_UNSIGNED_BYTE, image);
224 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
225 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
226 }
227
228 /* setup second texture object */
229 {
230 GLubyte image[8][8][3];
231 GLint i, j;
232 glBindTexture(GL_TEXTURE_2D, Textures[1]);
233
234 /* green/yellow checkerboard */
235 for (i = 0; i < 8; i++) {
236 for (j = 0; j < 8; j++) {
237 if ((i ^ j) & 1) {
238 image[i][j][0] = 0;
239 image[i][j][1] = 255;
240 image[i][j][2] = 0;
241 }
242 else {
243 image[i][j][0] = 255;
244 image[i][j][1] = 255;
245 image[i][j][2] = 0;
246 }
247 }
248 }
249
250 glPixelStorei(GL_UNPACK_ALIGNMENT, 2);
251 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, 8, 8, 0, GL_RGB,
252 GL_UNSIGNED_BYTE, image);
253 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
254 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
255 }
256
257 /* setup second texture object */
258 {
259 GLubyte image[4][4][3];
260 GLint i, j;
261 glBindTexture(GL_TEXTURE_2D, Textures[2]);
262
263 /* blue/gray checkerboard */
264 for (i = 0; i < 4; i++) {
265 for (j = 0; j < 4; j++) {
266 if ((i ^ j) & 1) {
267 image[i][j][0] = 0;
268 image[i][j][1] = 0;
269 image[i][j][2] = 255;
270 }
271 else {
272 image[i][j][0] = 200;
273 image[i][j][1] = 200;
274 image[i][j][2] = 200;
275 }
276 }
277 }
278
279 glPixelStorei(GL_UNPACK_ALIGNMENT, 2);
280 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, 4, 4, 0, GL_RGB,
281 GL_UNSIGNED_BYTE, image);
282 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
283 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
284 }
285
286 /* Now make the cube object display list */
287
288 printf("GL_RENDERER: %s\n", (char *) glGetString(GL_RENDERER));
289 printf("GL_VERSION: %s\n", (char *) glGetString(GL_VERSION));
290 printf("GL_VENDOR: %s\n", (char *) glGetString(GL_VENDOR));
291 }
292
293 static void
294 Redraw(struct window *h)
295 {
296 pthread_mutex_lock(&h->drawMutex);
297 if (!glXMakeCurrent(h->Dpy, h->Win, h->Context)) {
298 Error(h->DisplayName, "glXMakeCurrent failed in Redraw");
299 pthread_mutex_unlock(&h->drawMutex);
300 return;
301 }
302
303 h->Angle += 1.0;
304
305 glShadeModel(GL_FLAT);
306 glClearColor(0.25, 0.25, 0.25, 1.0);
307 glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
308
309 glEnable(GL_TEXTURE_2D);
310 glEnable(GL_DEPTH_TEST);
311 glTexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE);
312
313 glColor3f(1, 1, 1);
314
315 glPushMatrix();
316 if (h->Id == 0)
317 glRotatef(h->Angle, 0, 1, -1);
318 else if (h->Id == 1)
319 glRotatef(-(h->Angle), 0, 1, -1);
320 else if (h->Id == 2)
321 glRotatef(h->Angle, 0, 1, 1);
322 else if (h->Id == 3)
323 glRotatef(-(h->Angle), 0, 1, 1);
324 glBindTexture(GL_TEXTURE_2D, Textures[0]);
325 glBegin(GL_POLYGON);
326 glTexCoord2f(0, 0); glVertex3f(-1, -1, -1);
327 glTexCoord2f(1, 0); glVertex3f(-1, 1, -1);
328 glTexCoord2f(1, 1); glVertex3f(-1, 1, 1);
329 glTexCoord2f(0, 1); glVertex3f(-1, -1, 1);
330 glEnd();
331 glBegin(GL_POLYGON);
332 glTexCoord2f(0, 0); glVertex3f(1, -1, -1);
333 glTexCoord2f(1, 0); glVertex3f(1, 1, -1);
334 glTexCoord2f(1, 1); glVertex3f(1, 1, 1);
335 glTexCoord2f(0, 1); glVertex3f(1, -1, 1);
336 glEnd();
337
338 glBindTexture(GL_TEXTURE_2D, Textures[1]);
339 glBegin(GL_POLYGON);
340 glTexCoord2f(0, 0); glVertex3f(-1, -1, -1);
341 glTexCoord2f(1, 0); glVertex3f( 1, -1, -1);
342 glTexCoord2f(1, 1); glVertex3f( 1, -1, 1);
343 glTexCoord2f(0, 1); glVertex3f(-1, -1, 1);
344 glEnd();
345 glBegin(GL_POLYGON);
346 glTexCoord2f(0, 0); glVertex3f(-1, 1, -1);
347 glTexCoord2f(1, 0); glVertex3f( 1, 1, -1);
348 glTexCoord2f(1, 1); glVertex3f( 1, 1, 1);
349 glTexCoord2f(0, 1); glVertex3f(-1, 1, 1);
350 glEnd();
351
352 glBindTexture(GL_TEXTURE_2D, Textures[2]);
353 glBegin(GL_POLYGON);
354 glTexCoord2f(0, 0); glVertex3f(-1, -1, -1);
355 glTexCoord2f(1, 0); glVertex3f( 1, -1, -1);
356 glTexCoord2f(1, 1); glVertex3f( 1, 1, -1);
357 glTexCoord2f(0, 1); glVertex3f(-1, 1, -1);
358 glEnd();
359 glBegin(GL_POLYGON);
360 glTexCoord2f(0, 0); glVertex3f(-1, -1, 1);
361 glTexCoord2f(1, 0); glVertex3f( 1, -1, 1);
362 glTexCoord2f(1, 1); glVertex3f( 1, 1, 1);
363 glTexCoord2f(0, 1); glVertex3f(-1, 1, 1);
364 glEnd();
365
366 glPopMatrix();
367
368 glXSwapBuffers(h->Dpy, h->Win);
369
370 if (!glXMakeCurrent(h->Dpy, None, NULL)) {
371 Error(h->DisplayName, "glXMakeCurrent failed in Redraw");
372 }
373 pthread_mutex_unlock(&h->drawMutex);
374 }
375
376 static void *threadRunner (void *arg)
377 {
378 struct thread_init_arg *tia = (struct thread_init_arg *) arg;
379 struct window *win;
380
381 win = &Windows[tia->id];
382
383 while(!terminate) {
384 usleep(1000);
385 Redraw(win);
386 }
387
388 return NULL;
389 }
390
391 static void
392 Resize(struct window *h, unsigned int width, unsigned int height)
393 {
394 pthread_mutex_lock(&h->drawMutex);
395
396 if (!glXMakeCurrent(h->Dpy, h->Win, h->Context)) {
397 Error(h->DisplayName, "glXMakeCurrent failed in Resize()");
398 pthread_mutex_unlock(&h->drawMutex);
399 return;
400 }
401
402 glViewport(0, 0, width, height);
403 glMatrixMode(GL_PROJECTION);
404 glLoadIdentity();
405 glFrustum(-1, 1, -1, 1, 2, 10);
406 glMatrixMode(GL_MODELVIEW);
407 glLoadIdentity();
408 glTranslatef(0, 0, -4.5);
409 if (!glXMakeCurrent(h->Dpy, None, NULL)) {
410 Error(h->DisplayName, "glXMakeCurrent failed in Resize()");
411 }
412 pthread_mutex_unlock(&h->drawMutex);
413 }
414
415
416 static void
417 EventLoop(void)
418 {
419 while (1) {
420 int i;
421 XEvent event;
422 XNextEvent(gDpy, &event);
423 for (i = 0; i < NumWindows; i++) {
424 struct window *h = &Windows[i];
425 if (event.xany.window == h->Win) {
426 switch (event.type) {
427 case Expose:
428 Redraw(h);
429 break;
430 case ConfigureNotify:
431 Resize(h, event.xconfigure.width, event.xconfigure.height);
432 break;
433 case KeyPress:
434 terminate = 1;
435 return;
436 default:
437 /*no-op*/ ;
438 }
439 }
440 }
441 }
442 }
443
444 int
445 main(int argc, char *argv[])
446 {
447 const char *dpyName = XDisplayName(NULL);
448 pthread_t t0, t1, t2, t3;
449 struct thread_init_arg tia0, tia1, tia2, tia3;
450 struct window *h0;
451
452 XInitThreads();
453
454 gDpy = XOpenDisplay(dpyName);
455 if (!gDpy) {
456 Error(dpyName, "Unable to open display");
457 return -1;
458 }
459
460 if (initMainthread(gDpy, dpyName))
461 return -1;
462
463 /* four windows and contexts sharing display lists and texture objects */
464 h0 = AddWindow(gDpy, dpyName, 10, 10, gCtx);
465 (void) AddWindow(gDpy, dpyName, 330, 10, gCtx);
466 (void) AddWindow(gDpy, dpyName, 10, 350, gCtx);
467 (void) AddWindow(gDpy, dpyName, 330, 350, gCtx);
468
469 if (!glXMakeCurrent(gDpy, h0->Win, gCtx)) {
470 Error(dpyName, "glXMakeCurrent failed for init thread.");
471 return -1;
472 }
473
474 InitGLstuff();
475
476 tia0.id = 0;
477 pthread_create(&t0, NULL, threadRunner, &tia0);
478 tia1.id = 1;
479 pthread_create(&t1, NULL, threadRunner, &tia1);
480 tia2.id = 2;
481 pthread_create(&t2, NULL, threadRunner, &tia2);
482 tia3.id = 3;
483 pthread_create(&t3, NULL, threadRunner, &tia3);
484 EventLoop();
485 return 0;
486 }