1 /* $Id: jkrahntest.c,v 1.1 2002/06/16 03:57:48 brianp Exp $ */
3 /* This is a good test for glXSwapBuffers on non-current windows,
4 * and the glXCopyContext function. Fixed several Mesa/DRI bugs with
5 * this program on 15 June 2002.
7 * Joe's comments follow:
9 * I have tried some different approaches for being able to
10 * draw to multiple windows using one context, or a copied
11 * context. Mesa/indirect rendering works to use one context
12 * for multiple windows, but crashes with glXCopyContext.
13 * DRI is badly broken, at least for ATI.
15 * I also noticed that glXMakeCurrent allows a window and context
16 * from different visuals to be attached (haven't tested recently).
18 * Joe Krahn <jkrahn@nc.rr.com>
29 #define DEGTOR (M_PI/180.0)
31 static int AttributeList
[] = { GLX_RGBA
, GLX_DOUBLEBUFFER
, None
};
33 int main(int argc
, char **argv
)
37 XSetWindowAttributes swa
;
39 GLXContext ctx1
, ctx2
;
44 fprintf(stderr
, "This program tests GLX context switching.\n");
45 fprintf(stderr
, "Usage: cxbug <n>\n");
46 fprintf(stderr
, "Where n is:\n");
47 fprintf(stderr
, "\t1) Use two contexts and swap only when the context is current (typical case).\n");
48 fprintf(stderr
, "\t2) Use two contexts and swap at the same time.\n");
49 fprintf(stderr
, "\t\t Used to crash Mesa & nVidia, and DRI artifacts. Seems OK now.\n");
50 fprintf(stderr
, "\t3) Use one context, but only swap when a context is current.\n");
51 fprintf(stderr
, "\t\t Serious artifacts for DRI at least with ATI.\n");
52 fprintf(stderr
, "\t4) Use one context, swap both windows at the same time, so the left\n");
53 fprintf(stderr
, "\t\t window has no context at swap time. Severe artifacts for DRI.\n");
54 fprintf(stderr
, "\t5) Use two contexts, copying one to the other when switching windows.\n");
55 fprintf(stderr
, "\t\t DRI gives an error, indirect rendering crashes server.\n");
61 /* get a connection */
62 dpy
= XOpenDisplay(NULL
);
64 /* Get an appropriate visual */
65 vi
= glXChooseVisual(dpy
, DefaultScreen(dpy
), AttributeList
);
67 fprintf(stderr
, "No matching visuals found.\n");
71 /* Create two GLX contexts, with list sharing */
72 ctx1
= glXCreateContext(dpy
, vi
, 0, True
);
73 ctx2
= glXCreateContext(dpy
, vi
, ctx1
, True
);
75 /* create a colormap */
76 swa
.colormap
= XCreateColormap(dpy
, RootWindow(dpy
, vi
->screen
),
77 vi
->visual
, AllocNone
);
80 /* Create two windows */
81 win1
= XCreateWindow(dpy
, RootWindow(dpy
, vi
->screen
),
83 0, vi
->depth
, InputOutput
, vi
->visual
,
84 CWBorderPixel
| CWColormap
, &swa
);
85 XStoreName(dpy
, win1
, "Test [L]");
86 XMapWindow(dpy
, win1
);
87 XMoveWindow(dpy
, win1
, 10, 10); /* Initial requested x,y may not be honored */
90 static const char *name
= "window";
93 sizehints
.width
= 200;
94 sizehints
.height
= 200;
95 sizehints
.flags
= USSize
| USPosition
;
96 XSetNormalHints(dpy
, win1
, &sizehints
);
97 XSetStandardProperties(dpy
, win1
, name
, name
,
98 None
, (char **)NULL
, 0, &sizehints
);
102 win2
= XCreateWindow(dpy
, RootWindow(dpy
, vi
->screen
),
104 0, vi
->depth
, InputOutput
, vi
->visual
,
105 CWBorderPixel
| CWColormap
, &swa
);
106 XStoreName(dpy
, win1
, "Test [R]");
107 XMapWindow(dpy
, win2
);
108 XMoveWindow(dpy
, win2
, 260, 10);
110 XSizeHints sizehints
;
111 static const char *name
= "window";
114 sizehints
.width
= 200;
115 sizehints
.height
= 200;
116 sizehints
.flags
= USSize
| USPosition
;
117 XSetNormalHints(dpy
, win2
, &sizehints
);
118 XSetStandardProperties(dpy
, win2
, name
, name
,
119 None
, (char **)NULL
, 0, &sizehints
);
123 /* Now draw some spinning things */
124 for (angle
= 0; angle
< 360*4; angle
+= 10.0) {
125 /* Connect the context to window 1 */
126 glXMakeCurrent(dpy
, win1
, ctx1
);
128 /* Clear and draw in window 1 */
129 glDrawBuffer(GL_BACK
);
130 glClearColor(1, 1, 0, 1);
131 glClear(GL_COLOR_BUFFER_BIT
| GL_DEPTH_BUFFER_BIT
);
133 glBegin(GL_TRIANGLES
);
135 glVertex2f(cos(angle
* DEGTOR
), sin(angle
* DEGTOR
));
136 glVertex2f(cos((angle
+ 20.0) * DEGTOR
),
137 sin((angle
+ 20.0) * DEGTOR
));
141 if (test
== 1 || test
== 3 || test
== 5)
142 glXSwapBuffers(dpy
, win1
);
145 glXCopyContext(dpy
, ctx1
, ctx2
, GL_ALL_ATTRIB_BITS
);
146 /* Connect the context to window 2 */
147 if (test
== 3 || test
== 4) {
148 glXMakeCurrent(dpy
, win2
, ctx1
);
150 glXMakeCurrent(dpy
, win2
, ctx2
);
153 /* Clear and draw in window 2 */
154 glDrawBuffer(GL_BACK
);
155 glClearColor(0, 0, 1, 1);
156 glClear(GL_COLOR_BUFFER_BIT
);
158 glBegin(GL_TRIANGLES
);
160 glVertex2f(cos(angle
* DEGTOR
), sin(angle
* DEGTOR
));
161 glVertex2f(cos((angle
+ 20.0) * DEGTOR
),
162 sin((angle
+ 20.0) * DEGTOR
));
167 if (test
== 2 || test
== 4)
168 glXSwapBuffers(dpy
, win1
);
169 glXSwapBuffers(dpy
, win2
);