2 * Copyright (c) 2017 Timothy Arceri
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
27 * Tests setting shader source on an already compiled shader. If we don't
28 * compile the new source we need to make sure the old source being used if
29 * Mesa's on-disk shader cache is forced to fallback and recompile the shader.
34 #include "piglit-util-gl.h"
36 PIGLIT_GL_TEST_CONFIG_BEGIN
38 config
.supports_gl_compat_version
= 10;
40 config
.window_visual
= PIGLIT_GL_VISUAL_RGB
;
42 PIGLIT_GL_TEST_CONFIG_END
44 static const char vs_one
[] =
45 "varying vec4 color;\n"
47 " gl_Position = gl_ModelViewProjectionMatrix * gl_Vertex;\n"
48 " color = vec4(0.0, 0.4, 0.0, 1.0);\n"
51 static const char vs_two
[] =
52 "varying vec4 color;\n"
54 " gl_Position = gl_ModelViewProjectionMatrix * gl_Vertex;\n"
55 " color = vec4(0.4, 0.4, 0.0, 1.0);\n"
58 static const char fs_one
[] =
59 "varying vec4 color;\n"
61 " gl_FragColor = color;\n"
64 static const char fs_two
[] =
65 "varying vec4 color;\n"
67 " gl_FragColor = color + vec4(0.4, 0.0, 0.4, 0.0);\n"
70 static const GLfloat expect_one_one
[3] = { 0.0, 0.4, 0.0 };
71 static const GLfloat expect_one_two
[3] = { 0.4, 0.4, 0.4 };
72 static const GLfloat expect_two_one
[3] = { 0.4, 0.4, 0.0 };
73 static const GLfloat expect_two_two
[3] = { 0.8, 0.4, 0.4 };
77 static GLuint program
;
80 static void compile_shader(GLuint shader
)
84 glCompileShaderARB(shader
);
86 glGetObjectParameterivARB(shader
, GL_OBJECT_COMPILE_STATUS_ARB
, &status
);
90 glGetInfoLogARB(shader
, 1000, &len
, log
);
91 fprintf(stderr
, "Error: problem compiling shader: %s\n", log
);
92 piglit_report_result(PIGLIT_FAIL
);
96 static void link_and_use_program()
100 glLinkProgramARB(program
);
101 glGetObjectParameterivARB(program
, GL_OBJECT_LINK_STATUS_ARB
, &status
);
105 glGetInfoLogARB(program
, 1000, &len
, log
);
106 fprintf(stderr
, "Error: problem linking program: %s\n", log
);
107 piglit_report_result(PIGLIT_FAIL
);
110 glUseProgramObjectARB(program
);
113 static void compile_shaders()
119 static void setup_shaders(const char * vstext
, const char * fstext
)
121 glShaderSourceARB(vs
, 1, (const GLchar
**)&vstext
, NULL
);
122 glShaderSourceARB(fs
, 1, (const GLchar
**)&fstext
, NULL
);
128 enum piglit_result result
= PIGLIT_PASS
;
130 piglit_ortho_projection(piglit_width
, piglit_height
, GL_FALSE
);
132 glClear(GL_COLOR_BUFFER_BIT
);
134 setup_shaders(vs_one
, fs_one
);
136 link_and_use_program();
137 piglit_draw_rect(0, 0, piglit_width
/ 2, piglit_height
/ 2);
138 if (!piglit_probe_pixel_rgb(piglit_width
/ 4, piglit_height
/ 4, expect_one_one
))
139 result
= PIGLIT_FAIL
;
141 setup_shaders(vs_two
, fs_two
);
143 link_and_use_program();
144 piglit_draw_rect(piglit_width
/ 2, 0, piglit_width
/ 2, piglit_height
/ 2);
145 if (!piglit_probe_pixel_rgb(3 * piglit_width
/ 4, piglit_height
/ 4, expect_two_two
))
146 result
= PIGLIT_FAIL
;
148 /* We have now seen all the shaders so Mesa will skip compiling them
149 * in future. If we link with a combination it hasn't seen before it
150 * will be forced to fallback and compile them, which is what will
151 * happen in the following two tests.
154 setup_shaders(vs_two
, fs_one
);
156 setup_shaders(vs_one
, fs_two
);
157 link_and_use_program();
158 piglit_draw_rect(0, piglit_height
/ 2, piglit_width
/ 2, piglit_height
/ 2);
159 if (!piglit_probe_pixel_rgb(piglit_width
/ 4, 3 * piglit_height
/ 4, expect_two_one
))
160 result
= PIGLIT_FAIL
;
163 setup_shaders(vs_two
, fs_two
);
164 setup_shaders(vs_two
, fs_one
);
165 link_and_use_program();
166 piglit_draw_rect(piglit_width
/ 2, piglit_height
/ 2, piglit_width
/ 2, piglit_height
/ 2);
167 if (!piglit_probe_pixel_rgb(3 * piglit_width
/ 4, 3 * piglit_height
/ 4, expect_one_two
))
168 result
= PIGLIT_FAIL
;
174 piglit_init(int argc
, char **argv
)
176 piglit_require_vertex_shader();
177 piglit_require_fragment_shader();
179 vs
= glCreateShaderObjectARB(GL_VERTEX_SHADER_ARB
);
180 fs
= glCreateShaderObjectARB(GL_FRAGMENT_SHADER_ARB
);
181 program
= glCreateProgramObjectARB();
182 glAttachObjectARB(program
, vs
);
183 glAttachObjectARB(program
, fs
);