cl: Don't use device_infos if num_device_infos == 0
[piglit.git] / tests / shaders / glsl-cache-fallback-shader-source.c
blobb367c5a64df98455d7189b5c3883aadd41a9dd4b
1 /*
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
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.
25 /**
26 * \file
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.
32 #include <stdio.h>
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"
46 "void main() {\n"
47 " gl_Position = gl_ModelViewProjectionMatrix * gl_Vertex;\n"
48 " color = vec4(0.0, 0.4, 0.0, 1.0);\n"
49 "}\n";
51 static const char vs_two[] =
52 "varying vec4 color;\n"
53 "void main() {\n"
54 " gl_Position = gl_ModelViewProjectionMatrix * gl_Vertex;\n"
55 " color = vec4(0.4, 0.4, 0.0, 1.0);\n"
56 "}\n";
58 static const char fs_one[] =
59 "varying vec4 color;\n"
60 "void main() {\n"
61 " gl_FragColor = color;\n"
62 "}\n";
64 static const char fs_two[] =
65 "varying vec4 color;\n"
66 "void main() {\n"
67 " gl_FragColor = color + vec4(0.4, 0.0, 0.4, 0.0);\n"
68 "}\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 };
75 static GLuint vs;
76 static GLuint fs;
77 static GLuint program;
80 static void compile_shader(GLuint shader)
82 GLint status;
84 glCompileShaderARB(shader);
86 glGetObjectParameterivARB(shader, GL_OBJECT_COMPILE_STATUS_ARB, &status);
87 if (!status) {
88 GLchar log[1000];
89 GLsizei len;
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()
98 GLint status;
100 glLinkProgramARB(program);
101 glGetObjectParameterivARB(program, GL_OBJECT_LINK_STATUS_ARB, &status);
102 if (!status) {
103 GLchar log[1000];
104 GLsizei len;
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()
115 compile_shader(vs);
116 compile_shader(fs);
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);
125 enum piglit_result
126 piglit_display(void)
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);
135 compile_shaders();
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);
142 compile_shaders();
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);
155 compile_shaders();
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;
162 compile_shaders();
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;
170 return result;
173 void
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);