arb_copy_image: test copying of different mipmap levels of a texture
[piglit.git] / tests / glx / glx-multi-context-ib-1.c
blob7adca9fbc196fe01dfef9dcb755492ecb793633b
1 /*
2 * Copyright © 2009-2011 Intel Corporation
4 * Permission is hereby granted, free of charge, to any person obtaining a
5 * copy of this software and associated documentation files (the "Software"),
6 * to deal in the Software without restriction, including without limitation
7 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8 * and/or sell copies of the Software, and to permit persons to whom the
9 * Software is furnished to do so, subject to the following conditions:
11 * The above copyright notice and this permission notice (including the next
12 * paragraph) shall be included in all copies or substantial portions of the
13 * Software.
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
18 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
20 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
21 * IN THE SOFTWARE.
24 /** @file glx-multi-context-ib.c
26 * Tests that multiple contexts drawing using index buffers work correctly.
28 * Catches a bug in the i965 driver in which index buffer state was
29 * not reemitted across batchbuffer boundaries, if the first draw
30 * after the batch didn't use the IB.
33 #include <unistd.h>
34 #include "piglit-util-gl.h"
35 #include "piglit-glx-util.h"
37 int piglit_width = 50, piglit_height = 50;
38 static Display *dpy;
39 static Window win;
40 static GLXContext ctx0, ctx1;
42 GLuint vb_c0, vb_c1, ib_c0, ib_c1;
43 static float green[] = {0, 1, 0, 0};
44 static float red[] = {1, 0, 0, 0};
46 /* c0 sets up an IB that will mess up c1's drawing, by only indexing
47 * outside of c0's VBO.
49 static void
50 context0_init()
52 float vb_data[] = {
53 0, 0,
54 0, 0,
55 0, 0,
56 0, 0,
57 -1, -1,
58 1, -1,
59 1, 1,
60 -1, 1,
62 uint32_t ib_data[] = {
63 4, 5, 6, 7,
66 glGenBuffersARB(1, &vb_c0);
67 glGenBuffersARB(1, &ib_c0);
68 glBindBufferARB(GL_ARRAY_BUFFER_ARB, vb_c0);
69 glBindBufferARB(GL_ELEMENT_ARRAY_BUFFER_ARB, ib_c0);
71 glBufferDataARB(GL_ARRAY_BUFFER_ARB, sizeof(vb_data), vb_data,
72 GL_STATIC_DRAW);
73 glBufferDataARB(GL_ELEMENT_ARRAY_BUFFER_ARB, sizeof(ib_data), ib_data,
74 GL_STATIC_DRAW);
77 static void
78 context1_init()
80 float vb_data[] = {
81 -1, -1,
82 1, -1,
83 1, 1,
84 -1, 1,
86 uint32_t ib_data[] = {
87 0, 1, 2, 3,
90 glGenBuffersARB(1, &vb_c1);
91 glGenBuffersARB(1, &ib_c1);
92 glBindBufferARB(GL_ARRAY_BUFFER_ARB, vb_c1);
93 glBindBufferARB(GL_ELEMENT_ARRAY_BUFFER_ARB, ib_c1);
95 glBufferDataARB(GL_ARRAY_BUFFER_ARB, sizeof(vb_data), vb_data,
96 GL_STATIC_DRAW);
97 glBufferDataARB(GL_ELEMENT_ARRAY_BUFFER_ARB, sizeof(ib_data), ib_data,
98 GL_STATIC_DRAW);
101 static void
102 context0_frame(void)
104 glXMakeCurrent(dpy, win, ctx0);
106 glColor4fv(red);
108 glBindBufferARB(GL_ARRAY_BUFFER_ARB, vb_c0);
109 glBindBufferARB(GL_ELEMENT_ARRAY_BUFFER_ARB, ib_c0);
110 glEnableClientState(GL_VERTEX_ARRAY);
111 glVertexPointer(2, GL_FLOAT, 0, (void *)0);
113 glDrawElements(GL_TRIANGLE_FAN, 4, GL_UNSIGNED_INT, (void *)0);
116 static void
117 context1_frame(int iter)
119 glXMakeCurrent(dpy, win, ctx1);
121 /* This is the drawing without an IB that triggered the driver
122 * not reemitting IB state in the next draw call.
124 * The other context just exists to ensure that the race for
125 * the IB getting smashed is lost, and is also the thing that
126 * produces the glFlush()-based batchbuffer emits we rely on.
128 glColor4fv(red);
129 glDrawArrays(GL_TRIANGLE_FAN, 0, 4);
131 /* Failing draw call. */
132 if (iter == 1)
133 glColor4fv(green);
134 else
135 glColor4fv(red);
137 glBindBufferARB(GL_ARRAY_BUFFER_ARB, vb_c0);
138 glBindBufferARB(GL_ELEMENT_ARRAY_BUFFER_ARB, ib_c0);
139 glEnableClientState(GL_VERTEX_ARRAY);
140 glVertexPointer(2, GL_FLOAT, 0, (void *)0);
142 glDrawElements(GL_TRIANGLE_FAN, 4, GL_UNSIGNED_INT, (void *)0);
145 enum piglit_result
146 draw(Display *dpy)
148 bool pass;
150 context0_frame();
151 context1_frame(0);
152 /* The issue was that on the second frame, failure occurred. */
153 context0_frame();
154 context1_frame(1);
156 pass = piglit_probe_rect_rgb(0, 0, piglit_width, piglit_height, green);
158 glXSwapBuffers(dpy, win);
160 glXMakeCurrent(dpy, None, None);
162 return pass ? PIGLIT_PASS : PIGLIT_FAIL;
166 main(int argc, char **argv)
168 XVisualInfo *visinfo;
169 int i;
171 for(i = 1; i < argc; ++i) {
172 if (!strcmp(argv[i], "-auto"))
173 piglit_automatic = 1;
174 else
175 fprintf(stderr, "Unknown option: %s\n", argv[i]);
178 dpy = XOpenDisplay(NULL);
179 if (dpy == NULL) {
180 fprintf(stderr, "couldn't open display\n");
181 piglit_report_result(PIGLIT_FAIL);
183 visinfo = piglit_get_glx_visual(dpy);
184 win = piglit_get_glx_window(dpy, visinfo);
186 ctx0 = piglit_get_glx_context(dpy, visinfo);
187 ctx1 = piglit_get_glx_context(dpy, visinfo);
189 glXMakeCurrent(dpy, win, ctx0);
190 piglit_dispatch_default_init(PIGLIT_DISPATCH_GL);
191 piglit_require_extension("GL_ARB_vertex_buffer_object");
192 context0_init();
193 glXMakeCurrent(dpy, win, ctx1);
194 context1_init();
196 piglit_glx_event_loop(dpy, draw);
198 XFree(visinfo);
199 glXDestroyContext(dpy, ctx0);
200 glXDestroyContext(dpy, ctx1);
202 return 0;