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
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
25 * @file glx-shader-sharing.c
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";
41 static XVisualInfo
*visinfo
;
44 static const char *vert_shader_text
=
47 " gl_Position = ftransform(); \n"
48 " gl_FrontColor = gl_Color; \n"
51 static const char *frag_shader_text
=
54 " gl_FragColor = vec4(1.0) - gl_Color; \n"
58 static GLuint vert_shader
, frag_shader
;
60 static GLuint program
;
66 GLenum e
= glGetError();
68 printf("GL Error 0x%x at line %d\n", e
, line
);
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
);
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
);
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__
);
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
);
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
);
125 printf("%s: drawing with context 1 failed\n", TestName
);
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__
);
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
);
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
);
160 printf("%s: drawing with context 2 failed\n", TestName
);
164 glXDestroyContext(dpy
, ctx2
);
172 main(int argc
, char **argv
)
176 for(i
= 1; i
< argc
; ++i
) {
177 if (strcmp(argv
[i
], "-auto") == 0)
178 piglit_automatic
= 1;
180 fprintf(stderr
, "%s bad option: %s\n", TestName
, argv
[i
]);
183 dpy
= XOpenDisplay(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
);