glsl-1.30: add more loop unroll tests
[piglit.git] / tests / glx / glx-shader-sharing.c
blobfa304fd2eb50414eeed953bd7f8a9b628a02bf90
1 /*
2 * Copyright (c) 2010 VMware, Inc.
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 /**
25 * @file glx-shader-sharing.c
26 * @author Brian Paul
28 * Create two GLX contexts with shared shaders. Destroy first context,
29 * draw with second context.
32 #include "piglit-util-gl.h"
33 #include "piglit-glx-util.h"
35 int piglit_width = 50, piglit_height = 50;
37 static const char *TestName = "glx-shader-sharing";
39 static Display *dpy;
40 static Window win;
41 static XVisualInfo *visinfo;
44 static const char *vert_shader_text =
45 "void main() \n"
46 "{ \n"
47 " gl_Position = ftransform(); \n"
48 " gl_FrontColor = gl_Color; \n"
49 "} \n";
51 static const char *frag_shader_text =
52 "void main() \n"
53 "{ \n"
54 " gl_FragColor = vec4(1.0) - gl_Color; \n"
55 "} \n";
58 static GLuint vert_shader, frag_shader;
60 static GLuint program;
63 static void
64 check_error(int line)
66 GLenum e = glGetError();
67 if (e)
68 printf("GL Error 0x%x at line %d\n", e, line);
72 enum piglit_result
73 draw(Display *dpy)
75 const GLfloat red[3] = {1.0F, 0.0F, 0.0F};
76 const GLfloat green[3] = {0.0F, 1.0F, 0.0F};
77 GLXContext ctx1 = piglit_get_glx_context(dpy, visinfo);
78 GLXContext ctx2 = piglit_get_glx_context_share(dpy, visinfo, ctx1);
79 int ok;
81 if (!ctx1 || !ctx2) {
82 fprintf(stderr, "%s: create contexts failed\n", TestName);
83 piglit_report_result(PIGLIT_FAIL);
87 * Bind first context, make some shaders, draw something.
89 glXMakeCurrent(dpy, win, ctx1);
91 piglit_dispatch_default_init(PIGLIT_DISPATCH_GL);
93 if (piglit_get_gl_version() < 20) {
94 printf("%s: Requires OpenGL 2.0\n", TestName);
95 return PIGLIT_SKIP;
98 glClearColor(1.0, 0.0, 0.0, 1.0);
99 glClear(GL_COLOR_BUFFER_BIT);
101 vert_shader = piglit_compile_shader_text(GL_VERTEX_SHADER, vert_shader_text);
102 frag_shader = piglit_compile_shader_text(GL_FRAGMENT_SHADER, frag_shader_text);
103 program = piglit_link_simple_program(vert_shader, frag_shader);
104 check_error(__LINE__);
105 if (!program) {
106 piglit_report_result(PIGLIT_FAIL);
109 glUseProgram(program);
110 check_error(__LINE__);
112 piglit_ortho_projection(piglit_width, piglit_height, GL_FALSE);
113 glClearColor(0.1, 0.1, 0.1, 0.0);
114 glClear(GL_COLOR_BUFFER_BIT);
116 glColor3f(0, 1, 1);
117 piglit_draw_rect(10, 10, piglit_width - 20, piglit_height - 20);
118 check_error(__LINE__);
120 ok = piglit_probe_pixel_rgb(piglit_width / 2, piglit_height / 2, red);
122 glXSwapBuffers(dpy, win);
124 if (!ok) {
125 printf("%s: drawing with context 1 failed\n", TestName);
126 return PIGLIT_FAIL;
130 * Destroy first context
132 glXDestroyContext(dpy, ctx1);
136 * Draw something with second context (and shaders above)
138 glXMakeCurrent(dpy, win, ctx2);
140 /*program = piglit_link_simple_program(vert_shader, frag_shader);*/
141 check_error(__LINE__);
142 assert(program);
144 glUseProgram(program);
145 check_error(__LINE__);
147 piglit_ortho_projection(piglit_width, piglit_height, GL_FALSE);
148 glClearColor(0.2, 0.2, 0.2, 0.0);
149 glClear(GL_COLOR_BUFFER_BIT);
151 glColor3f(1, 0, 1);
152 piglit_draw_rect(10, 10, piglit_width - 20, piglit_height - 20);
153 check_error(__LINE__);
155 ok = piglit_probe_pixel_rgb(piglit_width / 2, piglit_height / 2, green);
157 glXSwapBuffers(dpy, win);
159 if (!ok) {
160 printf("%s: drawing with context 2 failed\n", TestName);
161 return PIGLIT_FAIL;
164 glXDestroyContext(dpy, ctx2);
167 return PIGLIT_PASS;
172 main(int argc, char **argv)
174 int i;
176 for(i = 1; i < argc; ++i) {
177 if (strcmp(argv[i], "-auto") == 0)
178 piglit_automatic = 1;
179 else
180 fprintf(stderr, "%s bad option: %s\n", TestName, argv[i]);
183 dpy = XOpenDisplay(NULL);
184 if (dpy == NULL) {
185 fprintf(stderr, "%s: open display failed\n", TestName);
186 piglit_report_result(PIGLIT_FAIL);
189 visinfo = piglit_get_glx_visual(dpy);
190 win = piglit_get_glx_window(dpy, visinfo);
192 piglit_glx_event_loop(dpy, draw);
194 return 0;