2 /* This is a good test for glXSwapBuffers on non-current windows,
3 * and the glXCopyContext function. Fixed several Mesa/DRI bugs with
4 * this program on 15 June 2002.
6 * Joe's comments follow:
8 * I have tried some different approaches for being able to
9 * draw to multiple windows using one context, or a copied
10 * context. Mesa/indirect rendering works to use one context
11 * for multiple windows, but crashes with glXCopyContext.
12 * DRI is badly broken, at least for ATI.
14 * I also noticed that glXMakeCurrent allows a window and context
15 * from different visuals to be attached (haven't tested recently).
17 * Joe Krahn <jkrahn@nc.rr.com>
32 #define DEGTOR (M_PI/180.0)
34 static int AttributeList
[] = { GLX_RGBA
, GLX_DOUBLEBUFFER
, None
};
36 int main(int argc
, char **argv
)
40 XSetWindowAttributes swa
;
42 GLXContext ctx1
, ctx2
;
47 fprintf(stderr
, "This program tests GLX context switching.\n");
48 fprintf(stderr
, "Usage: jkrahntest <n>\n");
49 fprintf(stderr
, "Where n is:\n");
50 fprintf(stderr
, "\t1) Use two contexts and swap only when the context is current (typical case).\n");
51 fprintf(stderr
, "\t2) Use two contexts and swap at the same time.\n");
52 fprintf(stderr
, "\t\t Used to crash Mesa & nVidia, and DRI artifacts. Seems OK now.\n");
53 fprintf(stderr
, "\t3) Use one context, but only swap when a context is current.\n");
54 fprintf(stderr
, "\t\t Serious artifacts for DRI at least with ATI.\n");
55 fprintf(stderr
, "\t4) Use one context, swap both windows at the same time, so the left\n");
56 fprintf(stderr
, "\t\t window has no context at swap time. Severe artifacts for DRI.\n");
57 fprintf(stderr
, "\t5) Use two contexts, copying one to the other when switching windows.\n");
58 fprintf(stderr
, "\t\t DRI gives an error, indirect rendering crashes server.\n");
64 /* get a connection */
65 dpy
= XOpenDisplay(NULL
);
67 /* Get an appropriate visual */
68 vi
= glXChooseVisual(dpy
, DefaultScreen(dpy
), AttributeList
);
70 fprintf(stderr
, "No matching visuals found.\n");
74 /* Create two GLX contexts, with list sharing */
75 ctx1
= glXCreateContext(dpy
, vi
, 0, True
);
76 ctx2
= glXCreateContext(dpy
, vi
, ctx1
, True
);
78 /* create a colormap */
79 swa
.colormap
= XCreateColormap(dpy
, RootWindow(dpy
, vi
->screen
),
80 vi
->visual
, AllocNone
);
83 /* Create two windows */
84 win1
= XCreateWindow(dpy
, RootWindow(dpy
, vi
->screen
),
86 0, vi
->depth
, InputOutput
, vi
->visual
,
87 CWBorderPixel
| CWColormap
, &swa
);
88 XStoreName(dpy
, win1
, "Test [L]");
89 XMapWindow(dpy
, win1
);
90 XMoveWindow(dpy
, win1
, 10, 10); /* Initial requested x,y may not be honored */
93 static const char *name
= "window";
96 sizehints
.width
= 200;
97 sizehints
.height
= 200;
98 sizehints
.flags
= USSize
| USPosition
;
99 XSetNormalHints(dpy
, win1
, &sizehints
);
100 XSetStandardProperties(dpy
, win1
, name
, name
,
101 None
, (char **)NULL
, 0, &sizehints
);
105 win2
= XCreateWindow(dpy
, RootWindow(dpy
, vi
->screen
),
107 0, vi
->depth
, InputOutput
, vi
->visual
,
108 CWBorderPixel
| CWColormap
, &swa
);
109 XStoreName(dpy
, win1
, "Test [R]");
110 XMapWindow(dpy
, win2
);
111 XMoveWindow(dpy
, win2
, 260, 10);
113 XSizeHints sizehints
;
114 static const char *name
= "window";
117 sizehints
.width
= 200;
118 sizehints
.height
= 200;
119 sizehints
.flags
= USSize
| USPosition
;
120 XSetNormalHints(dpy
, win2
, &sizehints
);
121 XSetStandardProperties(dpy
, win2
, name
, name
,
122 None
, (char **)NULL
, 0, &sizehints
);
126 /* Now draw some spinning things */
127 for (angle
= 0; angle
< 360*4; angle
+= 10.0) {
128 /* Connect the context to window 1 */
129 glXMakeCurrent(dpy
, win1
, ctx1
);
131 /* Clear and draw in window 1 */
132 glDrawBuffer(GL_BACK
);
133 glClearColor(1, 1, 0, 1);
134 glClear(GL_COLOR_BUFFER_BIT
| GL_DEPTH_BUFFER_BIT
);
136 glBegin(GL_TRIANGLES
);
138 glVertex2f(cos(angle
* DEGTOR
), sin(angle
* DEGTOR
));
139 glVertex2f(cos((angle
+ 20.0) * DEGTOR
),
140 sin((angle
+ 20.0) * DEGTOR
));
144 if (test
== 1 || test
== 3 || test
== 5)
145 glXSwapBuffers(dpy
, win1
);
148 glXCopyContext(dpy
, ctx1
, ctx2
, GL_ALL_ATTRIB_BITS
);
149 /* Connect the context to window 2 */
150 if (test
== 3 || test
== 4) {
151 glXMakeCurrent(dpy
, win2
, ctx1
);
153 glXMakeCurrent(dpy
, win2
, ctx2
);
156 /* Clear and draw in window 2 */
157 glDrawBuffer(GL_BACK
);
158 glClearColor(0, 0, 1, 1);
159 glClear(GL_COLOR_BUFFER_BIT
);
161 glBegin(GL_TRIANGLES
);
163 glVertex2f(cos(angle
* DEGTOR
), sin(angle
* DEGTOR
));
164 glVertex2f(cos((angle
+ 20.0) * DEGTOR
),
165 sin((angle
+ 20.0) * DEGTOR
));
170 if (test
== 2 || test
== 4)
171 glXSwapBuffers(dpy
, win1
);
172 glXSwapBuffers(dpy
, win2
);