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
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.
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"
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"
51 " color = vec4(0, 1, 0, 0); // good! \n"
54 static const char *fragShaderText
=
55 "varying vec4 color;\n"
58 " gl_FragColor = color; \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 };
72 static GLint ToleranceUniform
, ExpectedUniform
, AttrAttrib
;
76 * Test glVertexAttribArray(type, size, normalized)
79 test_array(GLenum type
, GLuint size
, GLboolean normalized
)
81 static const GLfloat verts
[4][2] = {
87 static const GLfloat green
[4] = { 0.0, 1.0, 0.0, 0.0 };
88 GLubyte attr_buffer
[4 * 4 * sizeof(double)];
99 typeSize
= sizeof(GLbyte
);
101 for (i
= 0; i
< 4; i
++)
102 expected
[i
] = (float) byte4_data
[i
];
104 case GL_UNSIGNED_BYTE
:
106 typeSize
= sizeof(GLubyte
);
108 for (i
= 0; i
< 4; i
++)
109 expected
[i
] = (float) ubyte4_data
[i
];
113 typeSize
= sizeof(GLshort
);
115 for (i
= 0; i
< 4; i
++)
116 expected
[i
] = (float) short4_data
[i
];
118 case GL_UNSIGNED_SHORT
:
120 typeSize
= sizeof(GLushort
);
122 for (i
= 0; i
< 4; i
++)
123 expected
[i
] = (float) ushort4_data
[i
];
126 maxVal
= (float) 0x7fffffff;
127 typeSize
= sizeof(GLint
);
129 for (i
= 0; i
< 4; i
++)
130 expected
[i
] = (float) int4_data
[i
];
132 case GL_UNSIGNED_INT
:
133 maxVal
= (float) 0xffffffff;
134 typeSize
= sizeof(GLuint
);
136 for (i
= 0; i
< 4; i
++)
137 expected
[i
] = (float) uint4_data
[i
];
141 typeSize
= sizeof(GLfloat
);
143 for (i
= 0; i
< 4; i
++)
144 expected
[i
] = (float) float4_data
[i
];
148 typeSize
= sizeof(GLdouble
);
150 for (i
= 0; i
< 4; i
++)
151 expected
[i
] = (float) double4_data
[i
];
156 typeSize
= sizeof(GLfloat
);
161 for (i
= 0; i
< 4; i
++) {
162 expected
[i
] /= maxVal
;
166 /* set unused components to defaults */
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
||
199 type
== GL_UNSIGNED_INT
)
200 tolerance
= 1.0 / 0xffffff; /* 1 / (2^24-1) */
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,
215 printf("Test %s[%d] %s failed\n",
216 piglit_get_gl_enum_name(type
),
218 (normalized
? "Normalized" : "Unnormalized"));
222 piglit_present_results();
231 static const GLenum types
[] = {
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
)
253 return pass
? PIGLIT_PASS
: PIGLIT_FAIL
;
258 piglit_init(int argc
, char **argv
)
260 piglit_require_gl_version(20);
262 Prog
= piglit_build_simple_program(vertShaderText
, fragShaderText
);
264 printf("Failed to compile/link program\n");
265 piglit_report_result(PIGLIT_FAIL
);
270 ExpectedUniform
= glGetUniformLocation(Prog
, "expected");
271 ToleranceUniform
= glGetUniformLocation(Prog
, "tolerance");
272 AttrAttrib
= glGetAttribLocation(Prog
, "attr");