2 * Copyright © 2017 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
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 unsuccessful-relink.c
27 * Render using a program with a uniform. Modify the uniform and then
28 * do a relink that will fail. This shouldn’t affect the original
29 * program and it should render with the new uniform value.
31 * GLSL 4.6 spec section 7.3:
33 * “If a program object that is active for any shader stage is
34 * re-linked unsuccessfully, the link status will be set to FALSE,
35 * but any existing executables and associated state will remain part
36 * of the current rendering state until a subsequent call to
37 * UseProgram, UseProgramStages, or BindProgramPipeline removes them
41 #include "piglit-util-gl.h"
43 PIGLIT_GL_TEST_CONFIG_BEGIN
45 config
.supports_gl_compat_version
= 20;
46 config
.window_visual
= PIGLIT_GL_VISUAL_RGB
| PIGLIT_GL_VISUAL_DOUBLE
;
48 PIGLIT_GL_TEST_CONFIG_END
50 static const char vs_source
[] =
51 "attribute vec4 piglit_vertex;\n"
54 " gl_Position = piglit_vertex;\n"
57 static const char fs_source
[] =
58 "uniform vec4 color;\n"
61 " gl_FragColor = color;\n"
64 static const float green
[] = { 0.0f
, 1.0f
, 0.0f
, 1.0f
};
65 static const float purple
[] = { 0.5f
, 0.0f
, 0.5f
, 1.0f
};
68 try_render(GLuint vertex_attrib
,
71 struct vertex
{ float x
, y
; };
72 static const struct vertex verts
[] = {
73 { -1, -1 }, { 1, -1 },
79 /* This isn’t using piglit_draw_rect because that tries to
80 * call glGetAttribLocation which won’t work on the unlinked
84 glGenBuffers(1, &buf
);
85 glBindBuffer(GL_ARRAY_BUFFER
, buf
);
86 glBufferData(GL_ARRAY_BUFFER
, sizeof verts
, verts
, GL_STATIC_DRAW
);
87 glVertexAttribPointer(vertex_attrib
, 2, GL_FLOAT
,
88 GL_FALSE
, sizeof verts
[0],
89 (void *) offsetof(struct vertex
, x
));
90 glEnableVertexAttribArray(vertex_attrib
);
91 glDrawArrays(GL_TRIANGLE_STRIP
, 0, 4);
92 glBindBuffer(GL_ARRAY_BUFFER
, 0);
93 glDeleteBuffers(1, &buf
);
95 if (!piglit_check_gl_error(GL_NO_ERROR
)) {
96 fprintf(stderr
, "error while drawing\n");
97 piglit_report_result(PIGLIT_FAIL
);
100 pass
= piglit_probe_rect_rgb(0, 0,
101 piglit_width
, piglit_height
,
104 fprintf(stderr
, "render failed\n");
110 unsuccessful_link(GLuint prog
)
116 /* Add the fs shader again. This should cause a link error
117 * because there would be two main functions in the fragment
119 for (i
= 0; i
< 2; i
++) {
120 shader
= piglit_compile_shader_text(GL_FRAGMENT_SHADER
,
122 glAttachShader(prog
, shader
);
127 glGetProgramiv(prog
, GL_LINK_STATUS
, &status
);
130 fprintf(stderr
, "Broken shader unexpectedly linked\n");
131 piglit_report_result(PIGLIT_FAIL
);
142 void *dummy_data
[2048];
145 glViewport(0, 0, piglit_width
, piglit_height
);
146 glClear(GL_COLOR_BUFFER_BIT
);
148 prog
= piglit_build_simple_program(vs_source
, fs_source
);
150 color_uniform
= glGetUniformLocation(prog
, "color");
152 if (color_uniform
== -1) {
153 fprintf(stderr
, "color uniform missing\n");
154 piglit_report_result(PIGLIT_FAIL
);
157 vertex_attrib
= glGetAttribLocation(prog
, "piglit_vertex");
159 if (vertex_attrib
== -1) {
160 fprintf(stderr
, "piglit_vertex attrib missing\n");
161 piglit_report_result(PIGLIT_FAIL
);
166 glUniform4fv(color_uniform
, 1, purple
);
168 if (!try_render(vertex_attrib
, purple
))
171 glUniform4fv(color_uniform
, 1, green
);
173 unsuccessful_link(prog
);
175 /* This test currently causes a use-after-free error in Mesa
176 * which causes sporadic failures. In order to increase the
177 * chances of making the test fail, we will do lots of little
178 * redundant allocations in the hope of overwriting the data
179 * previously allocated and freed by Mesa.
181 for (i
= 0; i
< ARRAY_SIZE(dummy_data
); i
++) {
182 dummy_data
[i
] = malloc(64);
183 memset(dummy_data
[i
], 0, 64);
185 for (i
= 0; i
< ARRAY_SIZE(dummy_data
); i
++)
188 if (!try_render(vertex_attrib
, green
))
191 piglit_present_results();
193 glDeleteProgram(prog
);
195 return pass
? PIGLIT_PASS
: PIGLIT_FAIL
;
199 piglit_init(int argc
, char **argv
)