conversion-explicit: use a different value for normalized +/- min
[piglit.git] / tests / spec / gl-2.0 / vertexattribpointer.c
blobed5dbea76e9d284d27552e9b8d93e2b9ed72651b
1 /*
2 * Copyright 2014 VMware, Inc.
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
21 * DEALINGS IN THE SOFTWARE.
24 /**
25 * Test glVertexAttribPointer with all combinations of types, sizes and
26 * normalized/unnormalized.
29 #include "piglit-util-gl.h"
31 PIGLIT_GL_TEST_CONFIG_BEGIN
32 config.supports_gl_compat_version = 20;
33 config.window_visual = PIGLIT_GL_VISUAL_RGBA | PIGLIT_GL_VISUAL_DOUBLE;
34 config.khr_no_error_support = PIGLIT_NO_ERRORS;
35 PIGLIT_GL_TEST_CONFIG_END
38 static const char *vertShaderText =
39 "uniform vec4 expected; \n"
40 "uniform float tolerance; \n"
41 "attribute vec4 attr; \n"
42 "varying vec4 color; \n"
43 " \n"
44 "void main() \n"
45 "{ \n"
46 " gl_Position = gl_Vertex; \n"
47 " vec4 diff = abs(expected - attr); \n"
48 " if (any(greaterThan(diff, vec4(tolerance)))) \n"
49 " color = vec4(1, 0, 0, 0); // bad! \n"
50 " else \n"
51 " color = vec4(0, 1, 0, 0); // good! \n"
52 "} \n";
54 static const char *fragShaderText =
55 "varying vec4 color;\n"
56 "void main()\n"
57 "{ \n"
58 " gl_FragColor = color; \n"
59 "} \n";
62 static const GLfloat float4_data[] = { -0.5, 0.0, 0.75, 1.0 };
63 static const GLdouble double4_data[] = { -0.5, 0.0, 0.75, 1.0 };
64 static const GLubyte ubyte4_data[] = { 100, 0, 200, 255 };
65 static const GLbyte byte4_data[] = { 50, 0, -25, -50 };
66 static const GLushort ushort4_data[] = { 16000, 0, 32000, 65535 };
67 static const GLshort short4_data[] = { 2000, 0, -4000, -8000 };
68 static const GLuint uint4_data[] = { 10000000, 0, 20000000, 80000000 };
69 static const GLint int4_data[] = { 10000000, 0, -20000000, -40000000 };
71 static GLuint Prog;
72 static GLint ToleranceUniform, ExpectedUniform, AttrAttrib;
76 * Test glVertexAttribArray(type, size, normalized)
78 static bool
79 test_array(GLenum type, GLuint size, GLboolean normalized)
81 static const GLfloat verts[4][2] = {
82 { -1.0, -1.0 },
83 { 1.0, -1.0 },
84 { 1.0, 1.0 },
85 { -1.0, 1.0 }
87 static const GLfloat green[4] = { 0.0, 1.0, 0.0, 0.0 };
88 GLubyte attr_buffer[4 * 4 * sizeof(double)];
89 float maxVal;
90 int typeSize;
91 const void *data;
92 float expected[4];
93 int i, p;
94 float tolerance;
96 switch (type) {
97 case GL_BYTE:
98 maxVal = 127.0;
99 typeSize = sizeof(GLbyte);
100 data = byte4_data;
101 for (i = 0; i < 4; i++)
102 expected[i] = (float) byte4_data[i];
103 break;
104 case GL_UNSIGNED_BYTE:
105 maxVal = 255.0;
106 typeSize = sizeof(GLubyte);
107 data = ubyte4_data;
108 for (i = 0; i < 4; i++)
109 expected[i] = (float) ubyte4_data[i];
110 break;
111 case GL_SHORT:
112 maxVal = 32767.0;
113 typeSize = sizeof(GLshort);
114 data = short4_data;
115 for (i = 0; i < 4; i++)
116 expected[i] = (float) short4_data[i];
117 break;
118 case GL_UNSIGNED_SHORT:
119 maxVal = 65535.0;
120 typeSize = sizeof(GLushort);
121 data = ushort4_data;
122 for (i = 0; i < 4; i++)
123 expected[i] = (float) ushort4_data[i];
124 break;
125 case GL_INT:
126 maxVal = (float) 0x7fffffff;
127 typeSize = sizeof(GLint);
128 data = int4_data;
129 for (i = 0; i < 4; i++)
130 expected[i] = (float) int4_data[i];
131 break;
132 case GL_UNSIGNED_INT:
133 maxVal = (float) 0xffffffff;
134 typeSize = sizeof(GLuint);
135 data = uint4_data;
136 for (i = 0; i < 4; i++)
137 expected[i] = (float) uint4_data[i];
138 break;
139 case GL_FLOAT:
140 maxVal = 1.0;
141 typeSize = sizeof(GLfloat);
142 data = float4_data;
143 for (i = 0; i < 4; i++)
144 expected[i] = (float) float4_data[i];
145 break;
146 case GL_DOUBLE:
147 maxVal = 1.0;
148 typeSize = sizeof(GLdouble);
149 data = double4_data;
150 for (i = 0; i < 4; i++)
151 expected[i] = (float) double4_data[i];
152 break;
153 default:
154 assert(0);
155 maxVal = 1.0;
156 typeSize = sizeof(GLfloat);
157 data = NULL;
160 if (normalized) {
161 for (i = 0; i < 4; i++) {
162 expected[i] /= maxVal;
166 /* set unused components to defaults */
167 switch (size) {
168 case 1:
169 expected[1] = 0.0;
170 case 2:
171 expected[2] = 0.0;
172 case 3:
173 expected[3] = 1.0;
176 /* Setup the attribute buffer by making four copies of the
177 * test's array data (for the four vertices).
180 int i, sz = typeSize * size;
181 for (i = 0; i < 4; i++) {
182 memcpy(attr_buffer + i * sz, data, sz);
186 glVertexAttribPointer(0, 2, GL_FLOAT, GL_FALSE, 0, verts);
187 glEnableVertexAttribArray(0);
188 glVertexAttribPointer(AttrAttrib, size, type,
189 normalized, 0, attr_buffer);
190 glEnableVertexAttribArray(AttrAttrib);
192 glViewport(0, 0, piglit_width, piglit_height);
193 glClearColor(1,0,0,0);
194 glClear(GL_COLOR_BUFFER_BIT);
196 if (type == GL_FLOAT ||
197 type == GL_DOUBLE ||
198 type == GL_INT ||
199 type == GL_UNSIGNED_INT)
200 tolerance = 1.0 / 0xffffff; /* 1 / (2^24-1) */
201 else
202 tolerance = 1.0 / maxVal;
204 glUniform1f(ToleranceUniform, tolerance);
205 glUniform4fv(ExpectedUniform, 1, expected);
207 glDrawArrays(GL_TRIANGLE_FAN, 0, 4);
209 glDisableVertexAttribArray(0);
210 glDisableVertexAttribArray(AttrAttrib);
212 p = piglit_probe_pixel_rgba(piglit_width / 2, piglit_height / 2,
213 green);
214 if (!p) {
215 printf("Test %s[%d] %s failed\n",
216 piglit_get_gl_enum_name(type),
217 size,
218 (normalized ? "Normalized" : "Unnormalized"));
219 fflush(stdout);
222 piglit_present_results();
224 return p;
228 enum piglit_result
229 piglit_display(void)
231 static const GLenum types[] = {
232 GL_BYTE,
233 GL_UNSIGNED_BYTE,
234 GL_SHORT,
235 GL_UNSIGNED_SHORT,
236 GL_INT,
237 GL_UNSIGNED_INT,
238 GL_FLOAT,
239 GL_DOUBLE
241 bool pass = true;
242 int t, size, normalized;
244 for (t = 0; t < ARRAY_SIZE(types); t++) {
245 for (size = 1; size <= 4; size++) {
246 for (normalized = 0; normalized < 2; normalized++) {
247 pass = test_array(types[t], size, normalized)
248 && pass;
253 return pass ? PIGLIT_PASS : PIGLIT_FAIL;
257 void
258 piglit_init(int argc, char **argv)
260 piglit_require_gl_version(20);
262 Prog = piglit_build_simple_program(vertShaderText, fragShaderText);
263 if (!Prog) {
264 printf("Failed to compile/link program\n");
265 piglit_report_result(PIGLIT_FAIL);
268 glUseProgram(Prog);
270 ExpectedUniform = glGetUniformLocation(Prog, "expected");
271 ToleranceUniform = glGetUniformLocation(Prog, "tolerance");
272 AttrAttrib = glGetAttribLocation(Prog, "attr");