cmake: move defaults into the per-platform section
[piglit.git] / tests / general / attribs-half-float.c
blobdaec5468fa2a0b16f44e8e1cf9595908987cf9fc
1 /*
2 * Copyright © 2011 Marek Olšák <maraeo@gmail.com>
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 * Test for immediate-mode style commands like glNormal, glColor, etc.
26 * with vertex arrays, immediate mode and display lists for GL_NV_half_float
27 * extension.
29 * glVertex and the commands taking a pointer (e.g. glColor*v) are not
30 * tested here.
33 #include "piglit-util-gl.h"
35 PIGLIT_GL_TEST_CONFIG_BEGIN
37 config.supports_gl_compat_version = 10;
39 config.window_width = 512;
40 config.window_height = 512;
41 config.window_visual = PIGLIT_GL_VISUAL_DOUBLE | PIGLIT_GL_VISUAL_RGBA;
42 config.khr_no_error_support = PIGLIT_NO_ERRORS;
44 PIGLIT_GL_TEST_CONFIG_END
46 #define ftoh(f) piglit_half_from_float(f)
48 static GLboolean snorm_equation_23;
50 enum {
52 RG,
53 RGB,
54 RGBA
57 enum {
58 FLOAT_TYPE,
59 INT_TYPE,
60 UINT_TYPE
63 enum {
64 VERTEX_ARRAYS,
65 IMMEDIATE_MODE,
66 DISPLAY_LIST,
67 NUM_MODES
70 static const char *mode_to_str[NUM_MODES] = {
71 "vertex arrays",
72 "immediate mode",
73 "display list"
76 static void draw_quad(unsigned mode, float *v,
77 void (*attrib)(float x, float y, float z, float w))
79 static const float verts[] = {
80 0, 0,
81 0, 10,
82 10, 10,
83 10, 0
85 int i;
87 switch (mode) {
88 case VERTEX_ARRAYS:
89 attrib(v[0], v[1], v[2], v[3]);
90 glEnableClientState(GL_VERTEX_ARRAY);
91 glVertexPointer(2, GL_FLOAT, 0, verts);
92 glDrawArrays(GL_QUADS, 0, 4);
93 glDisableClientState(GL_VERTEX_ARRAY);
94 break;
95 case IMMEDIATE_MODE:
96 glBegin(GL_QUADS);
97 for (i = 0; i < 4; i++) {
98 attrib(v[0], v[1], v[2], v[3]);
99 glVertex2fv(&verts[i*2]);
101 glEnd();
102 break;
103 case DISPLAY_LIST:
104 glNewList(1, GL_COMPILE);
105 glBegin(GL_QUADS);
106 for (i = 0; i < 4; i++) {
107 attrib(v[0], v[1], v[2], v[3]);
108 glVertex2fv(&verts[i*2]);
110 glEnd();
111 glEndList();
112 glCallList(1);
113 break;
114 default:
115 assert(0);
119 static GLboolean test(int x, int y, const char *shaderfunc,
120 unsigned mask, unsigned type, unsigned mode,
121 void (*attrib)(float x, float y, float z, float w),
122 const char *info)
124 static const char *templ = {
125 "%s \n"
126 "#extension GL_ARB_explicit_attrib_location : require \n"
127 "layout(location = 1) in %s attr; \n"
128 "void main() { \n"
129 " gl_Position = gl_ModelViewProjectionMatrix * gl_Vertex; \n"
130 " gl_FrontColor = (%s) * vec4(1.0, 1.0, 1.0, 0.5); \n"
131 "} \n"
133 GLuint prog, vs;
134 GLboolean pass = GL_TRUE;
135 char vstext[1024];
136 int i;
138 float color[3][4] = {
139 {0.2, 0.4, 0.6, 0.8},
140 {0, 1, 0, 1},
141 {0.5, 0.3, 0.9, 0.2}
144 sprintf(vstext, templ,
145 type != FLOAT_TYPE ? "#version 130" : "",
146 type == INT_TYPE ? "ivec4" : type == UINT_TYPE ? "uvec4" : "vec4",
147 shaderfunc);
149 /* Create the shader. */
150 vs = piglit_compile_shader_text(GL_VERTEX_SHADER, vstext);
151 if (!vs)
152 piglit_report_result(PIGLIT_FAIL);
153 prog = piglit_link_simple_program(vs, 0);
154 if (!prog)
155 piglit_report_result(PIGLIT_FAIL);
157 /* Render. */
158 glUseProgram(prog);
159 glLoadIdentity();
160 glTranslatef(x, y, 0);
162 draw_quad(mode, color[0], attrib);
163 glDrawArrays(GL_QUADS, 0, 4);
165 glTranslatef(10, 0, 0);
166 draw_quad(mode, color[1], attrib);
168 glTranslatef(10, 0, 0);
169 draw_quad(mode, color[2], attrib);
171 switch (mask) {
172 case R:
173 for (i = 0; i < 3; i++)
174 color[i][1] = 0;
175 /* fall through */
176 case RG:
177 for (i = 0; i < 3; i++)
178 color[i][2] = 0;
179 /* fall through */
180 case RGB:
181 for (i = 0; i < 3; i++)
182 color[i][3] = 1;
183 /* fall through */
184 case RGBA:
185 break;
186 default:
187 assert(0);
190 if (strstr(info, "GL_INT_2_10_10_10_REV-norm")) {
191 if (snorm_equation_23) {
192 for (i = 0; i < 3; i++) {
193 if (color[i][3] < 1)
194 color[i][3] = 0;
197 else {
198 for (i = 0; i < 3; i++) {
199 if (color[i][3] < 0.333)
200 color[i][3] = 0;
201 else if (color[i][3] < 1)
202 color[i][3] = 0.333;
205 } else if (strstr(info, "GL_INT_2_10_10_10_REV")) {
206 for (i = 0; i < 3; i++) {
207 if (color[i][3] < 1)
208 color[i][3] = 0;
210 } else if (strstr(info, "GL_UNSIGNED_INT_2_10_10_10_REV")) {
211 for (i = 0; i < 3; i++) {
212 if (color[i][3] < 0.333)
213 color[i][3] = 0;
214 else if (color[i][3] < 0.666)
215 color[i][3] = 0.333;
216 else if (color[i][3] < 1)
217 color[i][3] = 0.666;
221 /* We scale alpha in the shader to ensure it's not > 1 when it should be 1. */
222 for (i = 0; i < 3; i++)
223 color[i][3] *= 0.5;
225 /* Probe pixels. */
226 pass = piglit_probe_pixel_rgba(x+5, y+5, color[0]) && pass;
227 pass = piglit_probe_pixel_rgba(x+15, y+5, color[1]) && pass;
228 pass = piglit_probe_pixel_rgba(x+25, y+5, color[2]) && pass;
229 return pass;
232 typedef GLboolean (*test_func)(int x, int y, unsigned mode);
234 #define DEFINE_TEST(func, funcsuffix, params, shaderfunc, mask, type, info) \
235 static void invoke_##func##funcsuffix(float x, float y, float z, float w) \
237 func params; \
240 static GLboolean test_##func##funcsuffix(int x, int y, unsigned mode) \
242 printf("Testing %s, %s\n", #func "(" info ")", mode_to_str[mode]); \
243 return test(x, y, shaderfunc, mask, type, mode, invoke_##func##funcsuffix, info); \
246 DEFINE_TEST(glNormal3hNV,, (ftoh(x),ftoh(y),ftoh(z)), "vec4(gl_Normal, 1.0)", RGB, FLOAT_TYPE, "")
247 DEFINE_TEST(glColor3hNV,, (ftoh(x), ftoh(y), ftoh(z)), "gl_Color", RGB, FLOAT_TYPE, "")
248 DEFINE_TEST(glColor4hNV,, (ftoh(x), ftoh(y), ftoh(z), ftoh(w)), "gl_Color", RGBA, FLOAT_TYPE, "")
249 DEFINE_TEST(glTexCoord1hNV,, (ftoh(x)), "gl_MultiTexCoord0", R, FLOAT_TYPE, "")
250 DEFINE_TEST(glTexCoord2hNV,, (ftoh(x),ftoh(y)), "gl_MultiTexCoord0", RG, FLOAT_TYPE, "")
251 DEFINE_TEST(glTexCoord3hNV,, (ftoh(x),ftoh(y),ftoh(z)), "gl_MultiTexCoord0", RGB, FLOAT_TYPE, "")
252 DEFINE_TEST(glTexCoord4hNV,, (ftoh(x),ftoh(y),ftoh(z),ftoh(w)), "gl_MultiTexCoord0", RGBA, FLOAT_TYPE, "")
253 DEFINE_TEST(glMultiTexCoord1hNV,, (GL_TEXTURE1, ftoh(x)), "gl_MultiTexCoord1", R, FLOAT_TYPE, "")
254 DEFINE_TEST(glMultiTexCoord2hNV,, (GL_TEXTURE1, ftoh(x),ftoh(y)), "gl_MultiTexCoord1", RG, FLOAT_TYPE, "")
255 DEFINE_TEST(glMultiTexCoord3hNV,, (GL_TEXTURE1, ftoh(x),ftoh(y),ftoh(z)), "gl_MultiTexCoord1", RGB, FLOAT_TYPE, "")
256 DEFINE_TEST(glMultiTexCoord4hNV,, (GL_TEXTURE1, ftoh(x),ftoh(y),ftoh(z),ftoh(w)), "gl_MultiTexCoord1", RGBA, FLOAT_TYPE, "")
257 DEFINE_TEST(glSecondaryColor3hNV,, (ftoh(x),ftoh(y),ftoh(z)), "gl_SecondaryColor", RGB, FLOAT_TYPE, "")
259 static test_func tests[] = {
260 test_glNormal3hNV,
261 test_glColor3hNV,
262 test_glColor4hNV,
263 test_glTexCoord1hNV,
264 test_glTexCoord2hNV,
265 test_glTexCoord3hNV,
266 test_glTexCoord4hNV,
267 test_glMultiTexCoord1hNV,
268 test_glMultiTexCoord2hNV,
269 test_glMultiTexCoord3hNV,
270 test_glMultiTexCoord4hNV,
271 test_glSecondaryColor3hNV,
274 enum piglit_result piglit_display(void)
276 GLboolean pass = GL_TRUE;
277 int i, mode;
278 int x = 0, y = 0;
280 glClear(GL_COLOR_BUFFER_BIT);
282 for (mode = 0; mode < NUM_MODES; mode++) {
283 printf("\n");
284 for (i = 0; i < ARRAY_SIZE(tests); i++) {
285 pass = tests[i](x, y, mode) && pass;
286 x += 40;
287 if (x+40 > piglit_width) {
288 x = 0;
289 y += 20;
294 if (!piglit_check_gl_error(GL_NO_ERROR))
295 piglit_report_result(PIGLIT_FAIL);
296 piglit_present_results();
298 return pass ? PIGLIT_PASS : PIGLIT_FAIL;
301 void piglit_init(int argc, char **argv)
303 piglit_require_gl_version(20);
304 piglit_require_extension("GL_ARB_explicit_attrib_location");
305 piglit_require_extension("GL_NV_half_float");
306 piglit_ortho_projection(piglit_width, piglit_height, GL_FALSE);
308 snorm_equation_23 = piglit_get_gl_version() >= 42;
310 glClearColor(0.2, 0.2, 0.2, 1.0);