Merge branch 'master' into r500test
[mesa.git] / progs / tests / sharedtex.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
30 #include <GL/gl.h>
31 #include <GL/glx.h>
32 #include <stdio.h>
33 #include <stdlib.h>
34 #include <unistd.h>
35 #include <string.h>
36
37
38 struct window {
39 char DisplayName[1000];
40 Display *Dpy;
41 Window Win;
42 GLXContext Context;
43 float Angle;
44 int Id;
45 };
46
47
48 #define MAX_WINDOWS 20
49 static struct window Windows[MAX_WINDOWS];
50 static int NumWindows = 0;
51
52
53 static GLuint Textures[3];
54 static GLuint CubeList;
55
56
57
58 static void
59 Error(const char *display, const char *msg)
60 {
61 fprintf(stderr, "Error on display %s - %s\n", display, msg);
62 exit(1);
63 }
64
65
66 static struct window *
67 AddWindow(const char *displayName, int xpos, int ypos,
68 const struct window *shareWindow)
69 {
70 Display *dpy;
71 Window win;
72 GLXContext ctx;
73 int attrib[] = { GLX_RGBA,
74 GLX_RED_SIZE, 1,
75 GLX_GREEN_SIZE, 1,
76 GLX_BLUE_SIZE, 1,
77 GLX_DOUBLEBUFFER,
78 GLX_DEPTH_SIZE, 1,
79 None };
80 int scrnum;
81 XSetWindowAttributes attr;
82 unsigned long mask;
83 Window root;
84 XVisualInfo *visinfo;
85 int width = 300, height = 300;
86
87 if (NumWindows >= MAX_WINDOWS)
88 return NULL;
89
90 dpy = XOpenDisplay(displayName);
91 if (!dpy) {
92 Error(displayName, "Unable to open display");
93 return NULL;
94 }
95
96 scrnum = DefaultScreen(dpy);
97 root = RootWindow(dpy, scrnum);
98
99 visinfo = glXChooseVisual(dpy, scrnum, attrib);
100 if (!visinfo) {
101 Error(displayName, "Unable to find RGB, double-buffered visual");
102 return NULL;
103 }
104
105 /* window attributes */
106 attr.background_pixel = 0;
107 attr.border_pixel = 0;
108 attr.colormap = XCreateColormap(dpy, root, visinfo->visual, AllocNone);
109 attr.event_mask = StructureNotifyMask | ExposureMask | KeyPressMask;
110 mask = CWBackPixel | CWBorderPixel | CWColormap | CWEventMask;
111
112 win = XCreateWindow(dpy, root, xpos, ypos, width, height,
113 0, visinfo->depth, InputOutput,
114 visinfo->visual, mask, &attr);
115 if (!win) {
116 Error(displayName, "Couldn't create window");
117 return NULL;
118 }
119
120 {
121 XSizeHints sizehints;
122 sizehints.x = xpos;
123 sizehints.y = ypos;
124 sizehints.width = width;
125 sizehints.height = height;
126 sizehints.flags = USSize | USPosition;
127 XSetNormalHints(dpy, win, &sizehints);
128 XSetStandardProperties(dpy, win, displayName, displayName,
129 None, (char **)NULL, 0, &sizehints);
130 }
131
132
133 ctx = glXCreateContext(dpy, visinfo,
134 shareWindow ? shareWindow->Context : NULL,
135 True);
136 if (!ctx) {
137 Error(displayName, "Couldn't create GLX context");
138 return NULL;
139 }
140
141 XMapWindow(dpy, win);
142
143 if (!glXMakeCurrent(dpy, win, ctx)) {
144 Error(displayName, "glXMakeCurrent failed");
145 printf("glXMakeCurrent failed in Redraw()\n");
146 return NULL;
147 }
148
149 /* save the info for this window */
150 {
151 static int id = 0;
152 struct window *h = &Windows[NumWindows];
153 strcpy(h->DisplayName, displayName);
154 h->Dpy = dpy;
155 h->Win = win;
156 h->Context = ctx;
157 h->Angle = 0.0;
158 h->Id = id++;
159 NumWindows++;
160 return &Windows[NumWindows-1];
161 }
162
163 }
164
165
166 static void
167 InitGLstuff(struct window *h)
168 {
169 if (!glXMakeCurrent(h->Dpy, h->Win, h->Context)) {
170 Error(h->DisplayName, "glXMakeCurrent failed in InitGLstuff");
171 return;
172 }
173
174 glGenTextures(3, Textures);
175
176 /* setup first texture object */
177 {
178 GLubyte image[16][16][4];
179 GLint i, j;
180 glBindTexture(GL_TEXTURE_2D, Textures[0]);
181
182 /* red/white checkerboard */
183 for (i = 0; i < 16; i++) {
184 for (j = 0; j < 16; j++) {
185 if ((i ^ j) & 1) {
186 image[i][j][0] = 255;
187 image[i][j][1] = 255;
188 image[i][j][2] = 255;
189 image[i][j][3] = 255;
190 }
191 else {
192 image[i][j][0] = 255;
193 image[i][j][1] = 0;
194 image[i][j][2] = 0;
195 image[i][j][3] = 255;
196 }
197 }
198 }
199
200 glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
201 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, 16, 16, 0, GL_RGBA,
202 GL_UNSIGNED_BYTE, image);
203 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
204 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
205 }
206
207 /* setup second texture object */
208 {
209 GLubyte image[8][8][3];
210 GLint i, j;
211 glBindTexture(GL_TEXTURE_2D, Textures[1]);
212
213 /* green/yellow checkerboard */
214 for (i = 0; i < 8; i++) {
215 for (j = 0; j < 8; j++) {
216 if ((i ^ j) & 1) {
217 image[i][j][0] = 0;
218 image[i][j][1] = 255;
219 image[i][j][2] = 0;
220 }
221 else {
222 image[i][j][0] = 255;
223 image[i][j][1] = 255;
224 image[i][j][2] = 0;
225 }
226 }
227 }
228
229 glPixelStorei(GL_UNPACK_ALIGNMENT, 2);
230 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, 8, 8, 0, GL_RGB,
231 GL_UNSIGNED_BYTE, image);
232 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
233 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
234 }
235
236 /* setup second texture object */
237 {
238 GLubyte image[4][4][3];
239 GLint i, j;
240 glBindTexture(GL_TEXTURE_2D, Textures[2]);
241
242 /* blue/gray checkerboard */
243 for (i = 0; i < 4; i++) {
244 for (j = 0; j < 4; j++) {
245 if ((i ^ j) & 1) {
246 image[i][j][0] = 0;
247 image[i][j][1] = 0;
248 image[i][j][2] = 255;
249 }
250 else {
251 image[i][j][0] = 200;
252 image[i][j][1] = 200;
253 image[i][j][2] = 200;
254 }
255 }
256 }
257
258 glPixelStorei(GL_UNPACK_ALIGNMENT, 2);
259 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, 4, 4, 0, GL_RGB,
260 GL_UNSIGNED_BYTE, image);
261 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
262 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
263 }
264
265 /* Now make the cube object display list */
266 CubeList = glGenLists(1);
267 glNewList(CubeList, GL_COMPILE);
268 {
269 glBindTexture(GL_TEXTURE_2D, Textures[0]);
270 glBegin(GL_POLYGON);
271 glTexCoord2f(0, 0); glVertex3f(-1, -1, -1);
272 glTexCoord2f(1, 0); glVertex3f(-1, 1, -1);
273 glTexCoord2f(1, 1); glVertex3f(-1, 1, 1);
274 glTexCoord2f(0, 1); glVertex3f(-1, -1, 1);
275 glEnd();
276 glBegin(GL_POLYGON);
277 glTexCoord2f(0, 0); glVertex3f(1, -1, -1);
278 glTexCoord2f(1, 0); glVertex3f(1, 1, -1);
279 glTexCoord2f(1, 1); glVertex3f(1, 1, 1);
280 glTexCoord2f(0, 1); glVertex3f(1, -1, 1);
281 glEnd();
282
283 glBindTexture(GL_TEXTURE_2D, Textures[1]);
284 glBegin(GL_POLYGON);
285 glTexCoord2f(0, 0); glVertex3f(-1, -1, -1);
286 glTexCoord2f(1, 0); glVertex3f( 1, -1, -1);
287 glTexCoord2f(1, 1); glVertex3f( 1, -1, 1);
288 glTexCoord2f(0, 1); glVertex3f(-1, -1, 1);
289 glEnd();
290 glBegin(GL_POLYGON);
291 glTexCoord2f(0, 0); glVertex3f(-1, 1, -1);
292 glTexCoord2f(1, 0); glVertex3f( 1, 1, -1);
293 glTexCoord2f(1, 1); glVertex3f( 1, 1, 1);
294 glTexCoord2f(0, 1); glVertex3f(-1, 1, 1);
295 glEnd();
296
297 glBindTexture(GL_TEXTURE_2D, Textures[2]);
298 glBegin(GL_POLYGON);
299 glTexCoord2f(0, 0); glVertex3f(-1, -1, -1);
300 glTexCoord2f(1, 0); glVertex3f( 1, -1, -1);
301 glTexCoord2f(1, 1); glVertex3f( 1, 1, -1);
302 glTexCoord2f(0, 1); glVertex3f(-1, 1, -1);
303 glEnd();
304 glBegin(GL_POLYGON);
305 glTexCoord2f(0, 0); glVertex3f(-1, -1, 1);
306 glTexCoord2f(1, 0); glVertex3f( 1, -1, 1);
307 glTexCoord2f(1, 1); glVertex3f( 1, 1, 1);
308 glTexCoord2f(0, 1); glVertex3f(-1, 1, 1);
309 glEnd();
310 }
311 glEndList();
312
313 printf("GL_RENDERER: %s\n", (char *) glGetString(GL_RENDERER));
314 printf("GL_VERSION: %s\n", (char *) glGetString(GL_VERSION));
315 printf("GL_VENDOR: %s\n", (char *) glGetString(GL_VENDOR));
316 }
317
318
319
320 static void
321 Redraw(struct window *h)
322 {
323 if (!glXMakeCurrent(h->Dpy, h->Win, h->Context)) {
324 Error(h->DisplayName, "glXMakeCurrent failed");
325 printf("glXMakeCurrent failed in Redraw()\n");
326 return;
327 }
328
329 h->Angle += 1.0;
330
331 glShadeModel(GL_FLAT);
332 glClearColor(0.25, 0.25, 0.25, 1.0);
333 glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
334
335 glEnable(GL_TEXTURE_2D);
336 glEnable(GL_DEPTH_TEST);
337 glTexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE);
338
339 glColor3f(1, 1, 1);
340
341 glPushMatrix();
342 if (h->Id == 0)
343 glRotatef(h->Angle, 0, 1, -1);
344 else if (h->Id == 1)
345 glRotatef(-(h->Angle), 0, 1, -1);
346 else if (h->Id == 2)
347 glRotatef(h->Angle, 0, 1, 1);
348 else if (h->Id == 3)
349 glRotatef(-(h->Angle), 0, 1, 1);
350 glCallList(CubeList);
351 glPopMatrix();
352
353 glXSwapBuffers(h->Dpy, h->Win);
354 }
355
356
357
358 static void
359 Resize(const struct window *h, unsigned int width, unsigned int height)
360 {
361 if (!glXMakeCurrent(h->Dpy, h->Win, h->Context)) {
362 Error(h->DisplayName, "glXMakeCurrent failed in Resize()");
363 return;
364 }
365 glViewport(0, 0, width, height);
366 glMatrixMode(GL_PROJECTION);
367 glLoadIdentity();
368 glFrustum(-1, 1, -1, 1, 2, 10);
369 glMatrixMode(GL_MODELVIEW);
370 glLoadIdentity();
371 glTranslatef(0, 0, -4.5);
372 }
373
374
375
376 static void
377 EventLoop(void)
378 {
379 while (1) {
380 int i;
381 for (i = 0; i < NumWindows; i++) {
382 struct window *h = &Windows[i];
383 while (XPending(h->Dpy) > 0) {
384 XEvent event;
385 XNextEvent(h->Dpy, &event);
386 if (event.xany.window == h->Win) {
387 switch (event.type) {
388 case Expose:
389 Redraw(h);
390 break;
391 case ConfigureNotify:
392 Resize(h, event.xconfigure.width, event.xconfigure.height);
393 break;
394 case KeyPress:
395 return;
396 default:
397 /*no-op*/ ;
398 }
399 }
400 else {
401 printf("window mismatch\n");
402 }
403 }
404 Redraw(h);
405 }
406 usleep(1);
407 }
408 }
409
410
411 #if 0
412 static void
413 PrintInfo(const struct window *h)
414 {
415 printf("Name: %s\n", h->DisplayName);
416 printf(" Display: %p\n", (void *) h->Dpy);
417 printf(" Window: 0x%x\n", (int) h->Win);
418 printf(" Context: 0x%x\n", (int) h->Context);
419 }
420 #endif
421
422
423 int
424 main(int argc, char *argv[])
425 {
426 const char *dpyName = XDisplayName(NULL);
427
428 struct window *h0, *h1, *h2, *h3;
429
430 /* four windows and contexts sharing display lists and texture objects */
431 h0 = AddWindow(dpyName, 10, 10, NULL);
432 h1 = AddWindow(dpyName, 330, 10, h0);
433 h2 = AddWindow(dpyName, 10, 350, h0);
434 h3 = AddWindow(dpyName, 330, 350, h0);
435
436 InitGLstuff(h0);
437
438 EventLoop();
439 return 0;
440 }