fix the spelling in whole piglit
[piglit.git] / tests / general / texunits.c
blobc6fbcb1cdb2398d2e38f643b048098936365574a
1 /*
2 * Copyright (c) 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 * on the rights to use, copy, modify, merge, publish, distribute, sub
8 * license, and/or sell copies of the Software, and to permit persons to whom
9 * the 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 NON-INFRINGEMENT. IN NO EVENT SHALL
18 * THE AUTHORS AND/OR THEIR SUPPLIERS BE LIABLE FOR ANY CLAIM,
19 * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
20 * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
21 * USE OR OTHER DEALINGS IN THE SOFTWARE.
25 /**
26 * @file
27 * Test texture unit state with respect to the different number of
28 * texture coord units, image units, combined units, etc.
31 #include "piglit-util-gl.h"
33 PIGLIT_GL_TEST_CONFIG_BEGIN
35 config.supports_gl_compat_version = 10;
37 config.window_visual = PIGLIT_GL_VISUAL_RGB | PIGLIT_GL_VISUAL_DOUBLE;
39 config.khr_no_error_support = PIGLIT_NO_ERRORS;
41 PIGLIT_GL_TEST_CONFIG_END
43 #define MAX_UNITS 256
45 /** random number for checking state */
46 static GLfloat Random[MAX_UNITS][4];
48 static GLint MaxTextureCoordUnits;
49 static GLint MaxTextureVertexUnits;
50 static GLint MaxTextureImageUnits;
51 static GLint MaxTextureCombinedUnits;
54 static void
55 generate_random_numbers(void)
57 int i, j;
58 for (i = 0; i < ARRAY_SIZE(Random); i++) {
59 for (j = 0; j < 4; j++) {
60 /* values in [0, 1] */
61 Random[i][j] = (rand() % 1000) * .001;
67 static GLboolean
68 equal4v(const GLfloat v1[4], const GLfloat v2[4])
70 return (v1[0] == v2[0] &&
71 v1[1] == v2[1] &&
72 v1[2] == v2[2] &&
73 v1[3] == v2[3]);
77 static GLboolean
78 equal16v(const GLfloat v1[16], const GLfloat v2[16])
80 int i;
81 for (i = 0; i < 16; i++) {
82 if (v1[i] != v2[i])
83 return GL_FALSE;
85 return GL_TRUE;
89 static void
90 report4v(const GLfloat exp[4], const GLfloat act[4])
92 printf("Expected (%g, %g, %g, %g) but found (%g, %g, %g, %g)\n",
93 exp[0], exp[1], exp[2], exp[3],
94 act[0], act[1], act[2], act[3]);
98 static void
99 clear_errors()
101 while (glGetError())
106 static GLboolean
107 test_rasterpos(void)
109 int i;
111 clear_errors();
113 /* set current texcoords */
114 for (i = 0; i < MaxTextureCoordUnits; i++) {
115 glMultiTexCoord4fv(GL_TEXTURE0 + i, Random[i]);
118 /* query current texcoords */
119 for (i = 0; i < MaxTextureCoordUnits; i++) {
120 GLfloat v[4];
121 glActiveTexture(GL_TEXTURE0 + i);
122 glGetFloatv(GL_CURRENT_TEXTURE_COORDS, v);
123 if (!equal4v(Random[i], v)) {
124 printf("Get GL_CURRENT_TEXTURE_COORDS, unit %d failed\n", i);
125 report4v(Random[i], v);
126 return GL_FALSE;
130 /* set raster pos to update raster tex coords */
131 glRasterPos2i(0, 0);
133 for (i = 0; i < MaxTextureCoordUnits; i++) {
134 GLfloat v[4];
135 glActiveTexture(GL_TEXTURE0 + i);
136 glGetFloatv(GL_CURRENT_RASTER_TEXTURE_COORDS, v);
137 if (!equal4v(Random[i], v)) {
138 printf("Get GL_CURRENT_RASTER_TEXTURE_COORDS, unit %d failed\n", i);
139 report4v(Random[i], v);
140 return GL_FALSE;
144 /* there should be no errors at this point */
145 if (!piglit_check_gl_error(GL_NO_ERROR)) {
146 return GL_FALSE;
149 if (!piglit_khr_no_error) {
150 /* this should generate an error */
151 GLfloat v[4];
152 glActiveTexture(GL_TEXTURE0 + MaxTextureCoordUnits);
153 if (MaxTextureCoordUnits == MaxTextureCombinedUnits) {
154 /* INVALID_ENUM is expected */
155 if (!piglit_check_gl_error(GL_INVALID_ENUM)) {
156 return GL_FALSE;
159 else {
160 /* INVALID_OPERATION is expected */
161 glGetFloatv(GL_CURRENT_RASTER_TEXTURE_COORDS, v);
162 if (!piglit_check_gl_error(GL_INVALID_OPERATION)) {
163 return GL_FALSE;
168 return GL_TRUE;
172 static GLboolean
173 test_texture_matrix(void)
175 int i;
177 clear_errors();
179 /* set tex matrices */
180 for (i = 0; i < MaxTextureCoordUnits; i++) {
181 glActiveTexture(GL_TEXTURE0 + i);
182 glMatrixMode(GL_TEXTURE);
183 glLoadMatrixf((GLfloat *) Random + (i * 4) % 124);
186 /* query matrices */
187 for (i = 0; i < MaxTextureCoordUnits; i++) {
188 GLfloat m[16];
189 glActiveTexture(GL_TEXTURE0 + i);
190 glGetFloatv(GL_TEXTURE_MATRIX, m);
191 if (!equal16v((GLfloat *) Random + (i * 4) % 124, m)) {
192 printf("Get texture matrix unit %d failed\n", i);
193 return GL_FALSE;
197 /* there should be no errors at this point */
198 if (!piglit_check_gl_error(GL_NO_ERROR)) {
199 return GL_FALSE;
202 if (!piglit_khr_no_error) {
203 /* this should generate an error */
204 GLfloat m[16];
205 glActiveTexture(GL_TEXTURE0 + MaxTextureCoordUnits);
206 if (MaxTextureCoordUnits == MaxTextureCombinedUnits) {
207 /* INVALID_ENUM is expected */
208 if (!piglit_check_gl_error(GL_INVALID_ENUM)) {
209 return GL_FALSE;
212 else {
213 /* INVALID_OPERATION is expected */
214 glGetFloatv(GL_TEXTURE_MATRIX, m);
215 if (!piglit_check_gl_error(GL_INVALID_OPERATION)) {
216 return GL_FALSE;
221 return GL_TRUE;
225 static GLboolean
226 test_texture_params(void)
228 GLuint tex[MAX_UNITS];
229 int i;
230 int maxUnit;
232 clear_errors();
234 glGenTextures(MaxTextureCombinedUnits, tex);
236 /* set per-unit state */
237 for (i = 0; i < MaxTextureCombinedUnits; i++) {
238 glActiveTexture(GL_TEXTURE0 + i);
239 glBindTexture(GL_TEXTURE_2D, tex[i]);
240 glTexParameterfv(GL_TEXTURE_2D, GL_TEXTURE_BORDER_COLOR, Random[i]);
243 /* check per-unit state */
244 for (i = 0; i < MaxTextureCombinedUnits; i++) {
245 GLfloat v[4];
246 glActiveTexture(GL_TEXTURE0 + i);
247 glBindTexture(GL_TEXTURE_2D, tex[i]);
248 /* any per-unit state will do: */
249 glGetTexParameterfv(GL_TEXTURE_2D, GL_TEXTURE_BORDER_COLOR, v);
250 if (!equal4v(v, Random[i])) {
251 printf("Setting per-unit param state failed for unit %d\n", i);
252 report4v(Random[i], v);
253 return GL_FALSE;
257 /* there should be no errors at this point */
258 if (!piglit_check_gl_error(GL_NO_ERROR)) {
259 return GL_FALSE;
262 if (!piglit_khr_no_error) {
263 maxUnit = MAX2(MaxTextureCombinedUnits, MaxTextureCoordUnits);
265 /* this should generate an error */
266 glActiveTexture(GL_TEXTURE0 + maxUnit);
267 /* INVALID_ENUM is expected */
268 if (!piglit_check_gl_error(GL_INVALID_ENUM)) {
269 return GL_FALSE;
273 return GL_TRUE;
277 static GLboolean
278 test_texture_env(void)
280 /* Texture Environment state is fixed-function; not used by shaders */
281 int i;
283 clear_errors();
285 /* set per-unit state */
286 for (i = 0; i < MaxTextureCoordUnits; i++) {
287 glActiveTexture(GL_TEXTURE0 + i);
288 glTexEnvfv(GL_TEXTURE_ENV, GL_TEXTURE_ENV_COLOR, Random[i]);
289 if (!piglit_check_gl_error(GL_NO_ERROR)) {
290 return GL_FALSE;
294 /* check per-unit state */
295 for (i = 0; i < MaxTextureCoordUnits; i++) {
296 GLfloat v[4];
297 glActiveTexture(GL_TEXTURE0 + i);
298 glGetTexEnvfv(GL_TEXTURE_ENV, GL_TEXTURE_ENV_COLOR, v);
299 if (!equal4v(v, Random[i])) {
300 printf("Setting per-unit env state failed for unit %d\n", i);
301 report4v(Random[i], v);
302 return GL_FALSE;
306 /* there should be no errors at this point */
307 if (!piglit_check_gl_error(GL_NO_ERROR)) {
308 return GL_FALSE;
311 return GL_TRUE;
315 static void
316 report_info(void)
318 printf("GL_RENDERER = %s\n", (char *) glGetString(GL_RENDERER));
319 printf("GL_MAX_TEXTURE_COORDS = %d\n", MaxTextureCoordUnits);
320 printf("GL_MAX_TEXTURE_IMAGE_UNITS = %d\n", MaxTextureImageUnits);
321 printf("GL_MAX_VERTEX_TEXTURE_IMAGE_UNITS = %d\n", MaxTextureVertexUnits);
322 printf("GL_MAX_COMBINED_TEXTURE_IMAGE_UNITS = %d\n", MaxTextureCombinedUnits);
326 enum piglit_result
327 piglit_display(void)
329 GLboolean pass = GL_TRUE;
331 pass = test_rasterpos() && pass;
332 pass = test_texture_matrix() && pass;
333 pass = test_texture_params() && pass;
334 pass = test_texture_env() && pass;
336 return pass ? PIGLIT_PASS : PIGLIT_FAIL;
340 static void
341 init(void)
343 if (piglit_is_extension_supported("GL_ARB_vertex_shader")) {
344 glGetIntegerv(GL_MAX_TEXTURE_COORDS, &MaxTextureCoordUnits);
345 glGetIntegerv(GL_MAX_TEXTURE_IMAGE_UNITS, &MaxTextureImageUnits);
346 glGetIntegerv(GL_MAX_VERTEX_TEXTURE_IMAGE_UNITS, &MaxTextureVertexUnits);
347 glGetIntegerv(GL_MAX_COMBINED_TEXTURE_IMAGE_UNITS, &MaxTextureCombinedUnits);
349 else if (piglit_is_extension_supported("GL_ARB_fragment_shader") ||
350 piglit_is_extension_supported("GL_ARB_fragment_program")) {
351 glGetIntegerv(GL_MAX_TEXTURE_COORDS, &MaxTextureCoordUnits);
352 glGetIntegerv(GL_MAX_TEXTURE_IMAGE_UNITS, &MaxTextureImageUnits);
353 MaxTextureVertexUnits = 0;
354 MaxTextureCombinedUnits = MaxTextureImageUnits;
356 else {
357 glGetIntegerv(GL_MAX_TEXTURE_UNITS, &MaxTextureCoordUnits);
358 MaxTextureImageUnits =
359 MaxTextureCombinedUnits = MaxTextureCoordUnits;
360 MaxTextureVertexUnits = 0;
363 report_info();
365 if (MaxTextureCombinedUnits > MAX_UNITS) {
366 /* Need to increase the MAX_UNITS limit */
367 piglit_report_result(PIGLIT_WARN);
370 generate_random_numbers();
372 glMatrixMode(GL_PROJECTION);
373 glLoadIdentity();
374 glOrtho(0.0, 1.0, 0.0, 1.0, -1.0, 1.0);
375 glMatrixMode(GL_MODELVIEW);
376 glLoadIdentity();
380 void
381 piglit_init(int argc, char *argv[])
383 piglit_require_gl_version(13);
385 init();