cl: Don't use device_infos if num_device_infos == 0
[piglit.git] / tests / shaders / fp-set-02.c
blobc34f3c049e8ff86dfc9ccce6da1897a7b580561f
1 /*
2 * Copyright © 2009 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
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 fp-set-02.c
26 * Validate all of the set-on instructions in GL_NV_fragment_program_option
28 * Each set-on instruction is validated by comparing the value 0.5 with all
29 * combinations 0.0, 0.5, and 1.0 on the four channels. Reference squares are
30 * even rows, and testing squares are on odd rows.
32 * \author Ian Romanick <ian.d.romanick@intel.com>
35 #include "piglit-util-gl.h"
37 typedef int (cmp_func)(float a, float b);
39 static int eq_func(float a, float b);
40 static int fl_func(float a, float b);
41 static int ge_func(float a, float b);
42 static int gt_func(float a, float b);
43 static int le_func(float a, float b);
44 static int lt_func(float a, float b);
45 static int ne_func(float a, float b);
46 static int tr_func(float a, float b);
48 struct {
49 char *opcode;
50 cmp_func *func;
51 } tests[] = {
52 { "STR", tr_func },
53 { "SFL", fl_func },
54 { "SEQ", eq_func },
55 { "SNE", ne_func },
56 { "SGE", ge_func },
57 { "SLT", lt_func },
58 { "SGT", gt_func },
59 { "SLE", le_func },
62 /* One column for each possible combination of set-on results
64 #define TEST_COLS (3 * 3 * 3 * 3)
66 /* One for each set-on opcode and its reference square.
68 #define TEST_ROWS (ARRAY_SIZE(tests) * 2)
70 #define BOX_SIZE 8
72 PIGLIT_GL_TEST_CONFIG_BEGIN
74 config.supports_gl_compat_version = 10;
76 config.window_width = (((BOX_SIZE+1)*TEST_COLS)+1);
77 config.window_height = (((BOX_SIZE+1)*TEST_ROWS)+1);
78 config.window_visual = PIGLIT_GL_VISUAL_RGB | PIGLIT_GL_VISUAL_DOUBLE;
80 PIGLIT_GL_TEST_CONFIG_END
82 /**
83 * Source for the fragment program to render the reference box.
85 static const char reference_shader_source[] =
86 "!!ARBfp1.0\n"
87 "MOV result.color, program.env[0];\n"
88 "END"
91 static const char shader_template[] =
92 "!!ARBfp1.0\n"
93 "OPTION NV_fragment_program;\n"
94 "%s result.color, program.env[1], fragment.color;\n"
95 "END"
99 /**
100 * \name Handles to fragment programs.
102 /*@{*/
103 static GLint reference_prog;
104 static GLint progs[ARRAY_SIZE(tests)];
105 /*@}*/
108 eq_func(float a, float b)
110 return a == b;
114 fl_func(float a, float b)
116 return 0;
120 ge_func(float a, float b)
122 return a >= b;
126 gt_func(float a, float b)
128 return a > b;
132 le_func(float a, float b)
134 return a <= b;
139 lt_func(float a, float b)
141 return a < b;
145 ne_func(float a, float b)
147 return a != b;
151 tr_func(float a, float b)
153 return 1;
157 enum piglit_result
158 piglit_display(void)
160 const GLfloat comparator[4] = { 0.5, 0.5, 0.5, 0.5 };
161 static const float values[3] = { 0.0, 0.5, 1.0 };
162 unsigned i;
163 unsigned j;
164 enum piglit_result result = PIGLIT_PASS;
165 GLfloat color[4];
166 GLfloat ref[4];
168 glClear(GL_COLOR_BUFFER_BIT);
169 glEnable(GL_FRAGMENT_PROGRAM_ARB);
171 glProgramEnvParameter4fvARB(GL_FRAGMENT_PROGRAM_ARB, 1, comparator);
173 for (i = 0; i < (2 * ARRAY_SIZE(progs)); i++) {
174 const int y = (i * (BOX_SIZE + 1)) + 1;
175 const unsigned idx = i >> 1;
177 if ((i & 1) == 0) {
178 glBindProgramARB(GL_FRAGMENT_PROGRAM_ARB,
179 reference_prog);
180 } else {
181 glBindProgramARB(GL_FRAGMENT_PROGRAM_ARB,
182 progs[idx]);
185 for (j = 0; j < (3 * 3 * 3 * 3); j++) {
186 const int x = (j * (BOX_SIZE + 1)) + 1;
188 /* Determine the color of the reference square.
189 * This depends on both the set-on function and
190 * the mask.
192 ref[0] = values[j % 3];
193 ref[1] = values[(j / 3) % 3];
194 ref[2] = values[(j / 9) % 3];
195 ref[3] = values[(j / 27) % 3];
197 color[0] = tests[idx].func(comparator[0], ref[0])
198 ? 1.0 : 0.0;
199 color[1] = tests[idx].func(comparator[1], ref[1])
200 ? 1.0 : 0.0;
201 color[2] = tests[idx].func(comparator[2], ref[2])
202 ? 1.0 : 0.0;
203 color[3] = tests[idx].func(comparator[3], ref[3])
204 ? 1.0 : 0.0;
206 glProgramEnvParameter4fvARB(GL_FRAGMENT_PROGRAM_ARB,
207 0, color);
208 glColor4fv(ref);
210 piglit_draw_rect(x, y, BOX_SIZE, BOX_SIZE);
211 if ((i & 1) == 0)
212 continue;
214 if (!piglit_probe_pixel_rgb(x + (BOX_SIZE / 2),
215 y + (BOX_SIZE / 2),
216 color)) {
217 if (!piglit_automatic)
218 printf("%s failed on ref = "
219 "{ %.01f %.01f %.01f %.01f }\n",
220 tests[idx].opcode,
221 ref[0], ref[1], ref[2], ref[2]);
223 result = PIGLIT_FAIL;
229 piglit_present_results();
230 return result;
234 void
235 piglit_init(int argc, char **argv)
237 unsigned i;
239 (void) argc;
240 (void) argv;
242 piglit_require_fragment_program();
243 piglit_require_extension("GL_NV_fragment_program_option");
244 piglit_ortho_projection(piglit_width, piglit_height, GL_FALSE);
246 reference_prog = piglit_compile_program(GL_FRAGMENT_PROGRAM_ARB,
247 reference_shader_source);
249 for (i = 0; i < ARRAY_SIZE(tests); i++) {
250 char shader_source[512];
252 sprintf(shader_source, shader_template, tests[i].opcode);
253 progs[i] = piglit_compile_program(GL_FRAGMENT_PROGRAM_ARB,
254 shader_source);
258 glClearColor(0.5, 0.5, 0.5, 1.0);