summaryrefslogtreecommitdiff
path: root/tests/general/texunits.c
blob: 1a5f67eef7cd8b596150e405cf5afc2eb657b57e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
/*
 * Copyright (c) VMware, Inc.
 *
 * Permission is hereby granted, free of charge, to any person obtaining a
 * copy of this software and associated documentation files (the "Software"),
 * to deal in the Software without restriction, including without limitation
 * on the rights to use, copy, modify, merge, publish, distribute, sub
 * license, and/or sell copies of the Software, and to permit persons to whom
 * the Software is furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice (including the next
 * paragraph) shall be included in all copies or substantial portions of the
 * Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT.  IN NO EVENT SHALL
 * THE AUTHORS AND/OR THEIR SUPPLIERS BE LIABLE FOR ANY CLAIM,
 * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
 * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
 * USE OR OTHER DEALINGS IN THE SOFTWARE.
 */


/**
 * @file
 * Test texture unit state with respect to the different number of
 * texture coord units, image units, combined units, etc.
 */

#include "piglit-util.h"


#define MAX( A, B )   ( (A)>(B) ? (A) : (B) )


static int Width = 128, Height = 128;
static int Automatic = 0;


/** random number for checking state */
static GLfloat Random[128][4];

static GLint MaxTextureCoordUnits;
static GLint MaxTextureVertexUnits;
static GLint MaxTextureImageUnits;
static GLint MaxTextureCombinedUnits;


static void
generate_random_numbers(void)
{
   int i, j;
   for (i = 0; i < 128; i++) {
      for (j = 0; j < 4; j++) {
         /* values in [0, 1] */
         Random[i][j] = (rand() % 1000) * .001;
      }
   }
}


static GLboolean
equal4v(const GLfloat v1[4], const GLfloat v2[4])
{
   return (v1[0] == v2[0] &&
           v1[1] == v2[1] &&
           v1[2] == v2[2] &&
           v1[3] == v2[3]);
}


static GLboolean
equal16v(const GLfloat v1[16], const GLfloat v2[16])
{
   int i;
   for (i = 0; i < 16; i++) {
      if (v1[i] != v2[i])
         return GL_FALSE;
   }
   return GL_TRUE;
}


static void
report4v(const GLfloat exp[4], const GLfloat act[4])
{
   printf("Expected (%g, %g, %g, %g) but found (%g, %g, %g, %g)\n",
          exp[0], exp[1], exp[2], exp[3],
          act[0], act[1], act[2], act[3]);
}


static void
clear_errors()
{
   while (glGetError())
      ;
}


static GLboolean
test_rasterpos(void)
{
   GLenum err;
   int i;

   clear_errors();

   /* set current texcoords */
   for (i = 0; i < MaxTextureCoordUnits; i++) {
      glMultiTexCoord4fv(GL_TEXTURE0 + i, Random[i]);
   }

   /* query current texcoords */
   for (i = 0; i < MaxTextureCoordUnits; i++) {
      GLfloat v[4];
      glActiveTexture(GL_TEXTURE0 + i);
      glGetFloatv(GL_CURRENT_TEXTURE_COORDS, v);
      if (!equal4v(Random[i], v)) {
         printf("Get GL_CURRENT_TEXTURE_COORDS, unit %d failed\n", i);
         report4v(Random[i], v);
         return GL_FALSE;
      }
   }

   /* set raster pos to update raster tex coords */
   glRasterPos2i(0, 0);

   for (i = 0; i < MaxTextureCoordUnits; i++) {
      GLfloat v[4];
      glActiveTexture(GL_TEXTURE0 + i);
      glGetFloatv(GL_CURRENT_RASTER_TEXTURE_COORDS, v);
      if (!equal4v(Random[i], v)) {
         printf("Get GL_CURRENT_RASTER_TEXTURE_COORDS, unit %d failed\n", i);
         report4v(Random[i], v);
         return GL_FALSE;
      }
   }

   /* there should be no errors at this point */
   err = glGetError();
   if (err != GL_NO_ERROR) {
      printf("Unexpected GL error in %s(): 0x%x\n", __FUNCTION__, err);
   }

   /* this should generate an error */
   {
      GLfloat v[4];
      glActiveTexture(GL_TEXTURE0 + MaxTextureCoordUnits);
      if (MaxTextureCoordUnits == MaxTextureCombinedUnits) {
         /* INVALID_ENUM is expected */
         err = glGetError();
         if (err != GL_INVALID_ENUM) {
            printf("GL failed to raise GL_INVALID_ENUM setting texture unit\n");
            return GL_FALSE;
         }
      }
      else {
         /* INVALID_OPERATION is expected */
         glGetFloatv(GL_CURRENT_RASTER_TEXTURE_COORDS, v);
         if (glGetError() != GL_INVALID_OPERATION) {
            printf("GL failed to raise GL_INVALID_OPERATION quering invalid raster tex coords\n");
            return GL_FALSE;
         }
      }
   }

   return GL_TRUE;
}


static GLboolean
test_texture_matrix(void)
{
   GLenum err;
   int i;

   clear_errors();

   /* set tex matrices */
   for (i = 0; i < MaxTextureCoordUnits; i++) {
      glActiveTexture(GL_TEXTURE0 + i);
      glMatrixMode(GL_TEXTURE);
      glLoadMatrixf((GLfloat *) Random + (i * 4) % 124);
   }

   /* query matrices */
   for (i = 0; i < MaxTextureCoordUnits; i++) {
      GLfloat m[16];
      glActiveTexture(GL_TEXTURE0 + i);
      glGetFloatv(GL_TEXTURE_MATRIX, m);
      if (!equal16v((GLfloat *) Random + (i * 4) % 124, m)) {
         printf("Get texture matrix unit %d failed\n", i);
         return GL_FALSE;
      }
   }

   /* there should be no errors at this point */
   err = glGetError();
   if (err != GL_NO_ERROR) {
      printf("Unexpected GL error in %s(): 0x%x\n", __FUNCTION__, err);
   }

   /* this should generate an error */
   {
      GLfloat m[16];
      glActiveTexture(GL_TEXTURE0 + MaxTextureCoordUnits);
      if (MaxTextureCoordUnits == MaxTextureCombinedUnits) {
         /* INVALID_ENUM is expected */
         err = glGetError();
         if (err != GL_INVALID_ENUM) {
            printf("GL failed to raise GL_INVALID_ENUM setting texture unit\n");
            return GL_FALSE;
         }
      }
      else {
         /* INVALID_OPERATION is expected */
         glGetFloatv(GL_TEXTURE_MATRIX, m);
         if (glGetError() != GL_INVALID_OPERATION) {
            printf("GL failed to raise GL_INVALID_OPERATION querying invalid texture matrix\n");
            return GL_FALSE;
         }
      }
   }

   return GL_TRUE;
}


static GLboolean
test_texture_params(void)
{
   GLuint tex[100];
   GLenum err;
   int i;
   int maxUnit;

   clear_errors();

   glGenTextures(MaxTextureCombinedUnits, tex);

   /* set per-unit state */
   for (i = 0; i < MaxTextureCombinedUnits; i++) {
      glActiveTexture(GL_TEXTURE0 + i);
      glBindTexture(GL_TEXTURE_2D, tex[i]);
      glTexParameterfv(GL_TEXTURE_2D, GL_TEXTURE_BORDER_COLOR, Random[i]);
   }

   /* check per-unit state */
   for (i = 0; i < MaxTextureCombinedUnits; i++) {
      GLfloat v[4];
      glActiveTexture(GL_TEXTURE0 + i);
      glBindTexture(GL_TEXTURE_2D, tex[i]);
      /* any per-unit state will do: */
      glGetTexParameterfv(GL_TEXTURE_2D, GL_TEXTURE_BORDER_COLOR, v);
      if (!equal4v(v, Random[i])) {
         printf("Setting per-unit param state failed for unit %d\n", i);
         report4v(Random[i], v);
         return GL_FALSE;
      }
   }

   /* there should be no errors at this point */
   err = glGetError();
   if (err != GL_NO_ERROR) {
      printf("Unexpected GL error in %s(): 0x%x\n", __FUNCTION__, err);
   }

   maxUnit = MAX(MaxTextureCombinedUnits, MaxTextureCoordUnits);

   /* this should generate an error */
   glActiveTexture(GL_TEXTURE0 + maxUnit);
   /* INVALID_ENUM is expected */
   err = glGetError();
   if (err != GL_INVALID_ENUM) {
      printf("GL failed to raise GL_INVALID_ENUM setting texture unit\n");
      return GL_FALSE;
   }

   return GL_TRUE;
}


static GLboolean
test_texture_env(void)
{
   /* Texture Environment state is fixed-function; not used by shaders */
   GLenum err;
   int i;

   clear_errors();

   /* set per-unit state */
   for (i = 0; i < MaxTextureImageUnits; i++) {
      glActiveTexture(GL_TEXTURE0 + i);
      glTexEnvfv(GL_TEXTURE_ENV, GL_TEXTURE_ENV_COLOR, Random[i]);
   }

   /* check per-unit state */
   for (i = 0; i < MaxTextureImageUnits; i++) {
      GLfloat v[4];
      glActiveTexture(GL_TEXTURE0 + i);
      glGetTexEnvfv(GL_TEXTURE_ENV, GL_TEXTURE_ENV_COLOR, v);
      if (!equal4v(v, Random[i])) {
         printf("Setting per-unit env state failed for unit %d\n", i);
         report4v(Random[i], v);
         return GL_FALSE;
      }
   }

   /* there should be no errors at this point */
   err = glGetError();
   if (err != GL_NO_ERROR) {
      printf("Unexpected GL error in %s(): 0x%x\n", __FUNCTION__, err);
   }

   /* this should generate an error */
   {
      glActiveTexture(GL_TEXTURE0 + MaxTextureImageUnits);
      if (MaxTextureImageUnits == MaxTextureCombinedUnits) {
         /* INVALID_ENUM is expected */
         err = glGetError();
         if (err != GL_INVALID_ENUM) {
            printf("GL failed to raise GL_INVALID_ENUM setting texture unit\n");
            return GL_FALSE;
         }
      }
      else {
         glTexEnvfv(GL_TEXTURE_ENV, GL_TEXTURE_ENV_COLOR, Random[0]);
         /* INVALID_OPERATION is expected */
         err = glGetError();
         if (err != GL_INVALID_OPERATION) {
            printf("GL failed to raise GL_INVALID_OPERATION setting texture env\n");
            return GL_FALSE;
         }
      }
   }

   return GL_TRUE;
}


static void
report_info(void)
{
   printf("GL_RENDERER = %s\n", (char *) glGetString(GL_RENDERER));
   printf("GL_MAX_TEXTURE_COORDS = %d\n", MaxTextureCoordUnits);
   printf("GL_MAX_TEXTURE_IMAGE_UNITS = %d\n", MaxTextureImageUnits);
   printf("GL_MAX_VERTEX_TEXTURE_IMAGE_UNITS = %d\n", MaxTextureVertexUnits);
   printf("GL_MAX_COMBINED_TEXTURE_IMAGE_UNITS = %d\n", MaxTextureCombinedUnits);
}


static void
redisplay(void)
{
   if (Automatic) {
      GLboolean pass = GL_TRUE;

      pass = test_rasterpos() && pass;
      pass = test_texture_matrix() && pass;
      pass = test_texture_params() && pass;
      pass = test_texture_env() && pass;

      if (pass) {
         piglit_report_result(PIGLIT_SUCCESS);
      }
      else {
         report_info();
         piglit_report_result(PIGLIT_FAILURE);
      }
      exit(1);
   }
}


static void
reshape(int width, int height)
{
   Width = width;
   Height = height;
   glViewport(0, 0, width, height);
   glMatrixMode(GL_PROJECTION);
   glLoadIdentity();
   glOrtho(0.0, 1.0, 0.0, 1.0, -1.0, 1.0);
   glMatrixMode(GL_MODELVIEW);
   glLoadIdentity();
}


static void
key(unsigned char key, int x, int y)
{
   if (key == 27) {
      exit(0);
   }
   glutPostRedisplay();
}


static void
init(void)
{
   if (glutExtensionSupported("GL_ARB_fragment_program")) {
      glGetIntegerv(GL_MAX_TEXTURE_COORDS, &MaxTextureCoordUnits);
      glGetIntegerv(GL_MAX_TEXTURE_IMAGE_UNITS, &MaxTextureImageUnits);
      glGetIntegerv(GL_MAX_VERTEX_TEXTURE_IMAGE_UNITS, &MaxTextureVertexUnits);
      glGetIntegerv(GL_MAX_COMBINED_TEXTURE_IMAGE_UNITS, &MaxTextureCombinedUnits);
   }
   else {
      glGetIntegerv(GL_MAX_TEXTURE_UNITS, &MaxTextureCoordUnits);
      MaxTextureImageUnits =
      MaxTextureVertexUnits =
      MaxTextureCombinedUnits = MaxTextureCoordUnits;
   }

   if (0)
      report_info();

   generate_random_numbers();

   reshape(Width, Height);
}


int
main(int argc, char *argv[])
{
   glutInit(&argc, argv);
   if (argc == 2 && !strcmp(argv[1], "-auto"))
      Automatic = 1;

   glutInitWindowPosition(0, 0);
   glutInitWindowSize(Width, Height);
   glutInitDisplayMode(GLUT_RGB | GLUT_DOUBLE);
   glutCreateWindow(argv[0]);

   glewInit();

   if (!GLEW_VERSION_1_3) {
	   printf("Requires OpenGL 1.3\n");
	   piglit_report_result(PIGLIT_SKIP);
   }

   glutReshapeFunc(reshape);
   glutDisplayFunc(redisplay);
   if (!Automatic) {
      glutKeyboardFunc(key);
   }
   init();
   glutMainLoop();
   return 0;
}