Brian Paul | d373819 | 2002-06-16 03:57:48 +0000 | [diff] [blame^] | 1 | /* $Id: jkrahntest.c,v 1.1 2002/06/16 03:57:48 brianp Exp $ */ |
| 2 | |
| 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. |
| 6 | * |
| 7 | * Joe's comments follow: |
| 8 | * |
| 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. |
| 14 | * |
| 15 | * I also noticed that glXMakeCurrent allows a window and context |
| 16 | * from different visuals to be attached (haven't tested recently). |
| 17 | * |
| 18 | * Joe Krahn <jkrahn@nc.rr.com> |
| 19 | */ |
| 20 | |
| 21 | #include <GL/glx.h> |
| 22 | #include <GL/gl.h> |
| 23 | #include <string.h> |
| 24 | #include <stdio.h> |
| 25 | #include <stdlib.h> |
| 26 | #include <unistd.h> |
| 27 | #include <math.h> |
| 28 | #define M_PI 3.14159 |
| 29 | #define DEGTOR (M_PI/180.0) |
| 30 | |
| 31 | static int AttributeList[] = { GLX_RGBA, GLX_DOUBLEBUFFER, None }; |
| 32 | |
| 33 | int main(int argc, char **argv) |
| 34 | { |
| 35 | Window win1, win2; |
| 36 | XVisualInfo *vi; |
| 37 | XSetWindowAttributes swa; |
| 38 | Display *dpy; |
| 39 | GLXContext ctx1, ctx2; |
| 40 | float angle; |
| 41 | int test; |
| 42 | |
| 43 | if (argc < 2) { |
| 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"); |
| 56 | |
| 57 | exit(1); |
| 58 | } |
| 59 | test = atoi(argv[1]); |
| 60 | |
| 61 | /* get a connection */ |
| 62 | dpy = XOpenDisplay(NULL); |
| 63 | |
| 64 | /* Get an appropriate visual */ |
| 65 | vi = glXChooseVisual(dpy, DefaultScreen(dpy), AttributeList); |
| 66 | if (vi == 0) { |
| 67 | fprintf(stderr, "No matching visuals found.\n"); |
| 68 | exit(-1); |
| 69 | } |
| 70 | |
| 71 | /* Create two GLX contexts, with list sharing */ |
| 72 | ctx1 = glXCreateContext(dpy, vi, 0, True); |
| 73 | ctx2 = glXCreateContext(dpy, vi, ctx1, True); |
| 74 | |
| 75 | /* create a colormap */ |
| 76 | swa.colormap = XCreateColormap(dpy, RootWindow(dpy, vi->screen), |
| 77 | vi->visual, AllocNone); |
| 78 | swa.border_pixel = 0; |
| 79 | |
| 80 | /* Create two windows */ |
| 81 | win1 = XCreateWindow(dpy, RootWindow(dpy, vi->screen), |
| 82 | 10, 10, 200, 200, |
| 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 */ |
| 88 | { |
| 89 | XSizeHints sizehints; |
| 90 | static const char *name = "window"; |
| 91 | sizehints.x = 10; |
| 92 | sizehints.y = 10; |
| 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); |
| 99 | } |
| 100 | |
| 101 | |
| 102 | win2 = XCreateWindow(dpy, RootWindow(dpy, vi->screen), |
| 103 | 250, 10, 200, 200, |
| 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); |
| 109 | { |
| 110 | XSizeHints sizehints; |
| 111 | static const char *name = "window"; |
| 112 | sizehints.x = 10; |
| 113 | sizehints.y = 10; |
| 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); |
| 120 | } |
| 121 | |
| 122 | |
| 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); |
| 127 | |
| 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); |
| 132 | glColor3f(1, 0, 0); |
| 133 | glBegin(GL_TRIANGLES); |
| 134 | glVertex2f(0, 0); |
| 135 | glVertex2f(cos(angle * DEGTOR), sin(angle * DEGTOR)); |
| 136 | glVertex2f(cos((angle + 20.0) * DEGTOR), |
| 137 | sin((angle + 20.0) * DEGTOR)); |
| 138 | glEnd(); |
| 139 | glFlush(); |
| 140 | |
| 141 | if (test == 1 || test == 3 || test == 5) |
| 142 | glXSwapBuffers(dpy, win1); |
| 143 | |
| 144 | if (test == 5) |
| 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); |
| 149 | } else { |
| 150 | glXMakeCurrent(dpy, win2, ctx2); |
| 151 | } |
| 152 | |
| 153 | /* Clear and draw in window 2 */ |
| 154 | glDrawBuffer(GL_BACK); |
| 155 | glClearColor(0, 0, 1, 1); |
| 156 | glClear(GL_COLOR_BUFFER_BIT); |
| 157 | glColor3f(1, 1, 0); |
| 158 | glBegin(GL_TRIANGLES); |
| 159 | glVertex2f(0, 0); |
| 160 | glVertex2f(cos(angle * DEGTOR), sin(angle * DEGTOR)); |
| 161 | glVertex2f(cos((angle + 20.0) * DEGTOR), |
| 162 | sin((angle + 20.0) * DEGTOR)); |
| 163 | glEnd(); |
| 164 | glFlush(); |
| 165 | |
| 166 | /* Swap buffers */ |
| 167 | if (test == 2 || test == 4) |
| 168 | glXSwapBuffers(dpy, win1); |
| 169 | glXSwapBuffers(dpy, win2); |
| 170 | |
| 171 | /* wait a while */ |
| 172 | glXWaitX(); |
| 173 | usleep(20000); |
| 174 | } |
| 175 | |
| 176 | return 0; |
| 177 | } |