fbo-mrt-alphatest: Actually require MRTs to be available.
[piglit.git] / tests / spec / ext_texture_integer / getteximage-clamping.c
blob3df29974d18a42ab85ae26ad8b4f52ef868d5b16
1 /*
2 * Copyright © 2011 Intel Corporation
3 * Copyright (c) 2010 VMware, Inc.
4 * Copyright (c) 2011 Dave Airlie
6 * Permission is hereby granted, free of charge, to any person obtaining a
7 * copy of this software and associated documentation files (the "Software"),
8 * to deal in the Software without restriction, including without limitation
9 * on the rights to use, copy, modify, merge, publish, distribute, sub
10 * license, and/or sell copies of the Software, and to permit persons to whom
11 * the Software is furnished to do so, subject to the following conditions:
13 * The above copyright notice and this permission notice (including the next
14 * paragraph) shall be included in all copies or substantial portions of the
15 * Software.
17 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
18 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
19 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
20 * NON-INFRINGEMENT. IN NO EVENT SHALL VMWARE AND/OR THEIR SUPPLIERS
21 * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
22 * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
23 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
24 * SOFTWARE.
27 /**
28 * @file getteximage-clamping.c
30 * From the GL_EXT_texture_integer spec:
33 * "(modify the section labeled "Final Conversion", p. 222)
35 * For a floating-point RGBA color, each component is first
36 * clamped to [0, 1]. Then the appropriate conversion formula
37 * from table 4.7 is applied to the component. For an integer
38 * RGBA color, each component is clamped to the representable
39 * range of <type>."
41 * This test checks the conversion and clamping by making a texture of
42 * every sized internal format it can, and reads it to every
43 * format/type combo it can.
47 #include "piglit-util-gl.h"
49 PIGLIT_GL_TEST_CONFIG_BEGIN
51 config.supports_gl_compat_version = 10;
53 config.window_visual = PIGLIT_GL_VISUAL_RGBA | PIGLIT_GL_VISUAL_DOUBLE;
54 config.khr_no_error_support = PIGLIT_NO_ERRORS;
56 PIGLIT_GL_TEST_CONFIG_END
58 struct format_info {
59 const char *name;
60 GLenum internal_format, base_format;
61 int size;
62 bool sign;
65 /* FINISHME: I'd like this set of texture formats to be sanitized a
66 * bit, and it should probably be shared with the other testcases in
67 * this directory.
69 static const struct format_info formats[] = {
70 { "GL_RGBA8I", GL_RGBA8I, GL_RGBA_INTEGER, 8, true },
71 { "GL_RGBA8UI", GL_RGBA8UI , GL_RGBA_INTEGER, 8, false },
72 { "GL_RGBA16I", GL_RGBA16I, GL_RGBA_INTEGER, 16, true },
73 { "GL_RGBA16UI", GL_RGBA16UI, GL_RGBA_INTEGER, 16, false },
74 { "GL_RGBA32I", GL_RGBA32I, GL_RGBA_INTEGER, 32, true },
75 { "GL_RGBA32UI", GL_RGBA32UI, GL_RGBA_INTEGER, 32, false },
77 { "GL_RGBA8I (bgra)", GL_RGBA8I, GL_BGRA_INTEGER, 8, true },
78 { "GL_RGBA8UI (bgra)", GL_RGBA8UI , GL_BGRA_INTEGER, 8, false },
79 { "GL_RGBA16I (bgra)", GL_RGBA16I, GL_BGRA_INTEGER, 16, true },
80 { "GL_RGBA16UI (bgra)", GL_RGBA16UI, GL_BGRA_INTEGER, 16, false },
81 { "GL_RGBA32I (bgra)", GL_RGBA32I, GL_BGRA_INTEGER, 32, true },
82 { "GL_RGBA32UI (bgra)", GL_RGBA32UI, GL_BGRA_INTEGER, 32, false },
84 { "GL_RGB8I", GL_RGB8I, GL_RGB_INTEGER, 8, true },
85 { "GL_RGB8UI", GL_RGB8UI , GL_RGB_INTEGER, 8, false },
86 { "GL_RGB16I", GL_RGB16I, GL_RGB_INTEGER, 16, true },
87 { "GL_RGB16UI", GL_RGB16UI, GL_RGB_INTEGER, 16, false },
88 { "GL_RGB32I", GL_RGB32I, GL_RGB_INTEGER, 32, true },
89 { "GL_RGB32UI", GL_RGB32UI, GL_RGB_INTEGER, 32, false },
91 { "GL_ALPHA8I_EXT", GL_ALPHA8I_EXT, GL_ALPHA_INTEGER_EXT, 8, true },
92 { "GL_ALPHA8UI_EXT", GL_ALPHA8UI_EXT , GL_ALPHA_INTEGER_EXT, 8, false },
93 { "GL_ALPHA16I_EXT", GL_ALPHA16I_EXT, GL_ALPHA_INTEGER_EXT, 16, true },
94 { "GL_ALPHA16UI_EXT", GL_ALPHA16UI_EXT, GL_ALPHA_INTEGER_EXT, 16, false },
95 { "GL_ALPHA32I_EXT", GL_ALPHA32I_EXT, GL_ALPHA_INTEGER_EXT, 32, true },
96 { "GL_ALPHA32UI_EXT", GL_ALPHA32UI_EXT, GL_ALPHA_INTEGER_EXT, 32, false },
98 { "GL_LUMINANCE8I_EXT", GL_LUMINANCE8I_EXT, GL_LUMINANCE_INTEGER_EXT, 8, true },
99 { "GL_LUMINANCE8UI_EXT", GL_LUMINANCE8UI_EXT , GL_LUMINANCE_INTEGER_EXT, 8, false },
100 { "GL_LUMINANCE16I_EXT", GL_LUMINANCE16I_EXT, GL_LUMINANCE_INTEGER_EXT, 16, true },
101 { "GL_LUMINANCE16UI_EXT", GL_LUMINANCE16UI_EXT, GL_LUMINANCE_INTEGER_EXT, 16, false },
102 { "GL_LUMINANCE32I_EXT", GL_LUMINANCE32I_EXT, GL_LUMINANCE_INTEGER_EXT, 32, true },
103 { "GL_LUMINANCE32UI_EXT", GL_LUMINANCE32UI_EXT, GL_LUMINANCE_INTEGER_EXT, 32, false },
105 { "GL_LUMINANCE_ALPHA8I_EXT", GL_LUMINANCE_ALPHA8I_EXT, GL_LUMINANCE_ALPHA_INTEGER_EXT, 8, true },
106 { "GL_LUMINANCE_ALPHA8UI_EXT", GL_LUMINANCE_ALPHA8UI_EXT , GL_LUMINANCE_ALPHA_INTEGER_EXT, 8, false },
107 { "GL_LUMINANCE_ALPHA16I_EXT", GL_LUMINANCE_ALPHA16I_EXT, GL_LUMINANCE_ALPHA_INTEGER_EXT, 16, true },
108 { "GL_LUMINANCE_ALPHA16UI_EXT", GL_LUMINANCE_ALPHA16UI_EXT, GL_LUMINANCE_ALPHA_INTEGER_EXT, 16, false },
109 { "GL_LUMINANCE_ALPHA32I_EXT", GL_LUMINANCE_ALPHA32I_EXT, GL_LUMINANCE_ALPHA_INTEGER_EXT, 32, true },
110 { "GL_LUMINANCE_ALPHA32UI_EXT", GL_LUMINANCE_ALPHA32UI_EXT, GL_LUMINANCE_ALPHA_INTEGER_EXT, 32, false },
112 { "GL_INTENSITY8I_EXT", GL_INTENSITY8I_EXT, GL_RED_INTEGER_EXT, 8, true },
113 { "GL_INTENSITY8UI_EXT", GL_INTENSITY8UI_EXT , GL_RED_INTEGER_EXT, 8, false },
114 { "GL_INTENSITY16I_EXT", GL_INTENSITY16I_EXT, GL_RED_INTEGER_EXT, 16, true },
115 { "GL_INTENSITY16UI_EXT", GL_INTENSITY16UI_EXT, GL_RED_INTEGER_EXT, 16, false },
116 { "GL_INTENSITY32I_EXT", GL_INTENSITY32I_EXT, GL_RED_INTEGER_EXT, 32, true },
117 { "GL_INTENSITY32UI_EXT", GL_INTENSITY32UI_EXT, GL_RED_INTEGER_EXT, 32, false },
119 { "GL_RG8I", GL_RG8I, GL_RG_INTEGER, 8, true },
120 { "GL_RG8UI", GL_RG8UI , GL_RG_INTEGER, 8, false },
121 { "GL_RG16I", GL_RG16I, GL_RG_INTEGER, 16, true },
122 { "GL_RG16UI", GL_RG16UI, GL_RG_INTEGER, 16, false },
123 { "GL_RG32I", GL_RG32I, GL_RG_INTEGER, 32, true },
124 { "GL_RG32UI", GL_RG32UI, GL_RG_INTEGER, 32, false },
125 { "GL_R8I", GL_R8I, GL_RED_INTEGER, 8, true },
126 { "GL_R8UI", GL_R8UI , GL_RED_INTEGER, 8, false },
127 { "GL_R16I", GL_R16I, GL_RED_INTEGER, 16, true },
128 { "GL_R16UI", GL_R16UI, GL_RED_INTEGER, 16, false },
129 { "GL_R32I", GL_R32I, GL_RED_INTEGER, 32, true },
130 { "GL_R32UI", GL_R32UI, GL_RED_INTEGER, 32, false },
132 { "GL_RGB10_A2UI", GL_RGB10_A2UI, GL_RGBA_INTEGER, 10, false },
133 { "GL_RGB10_A2UI (bgra)", GL_RGB10_A2UI, GL_BGRA_INTEGER, 10, false },
134 { "GL_RGB10_A2UI (rev)", GL_RGB10_A2UI, GL_RGBA_INTEGER, 10, true },
135 { "GL_RGB10_A2UI (rev bgra)", GL_RGB10_A2UI, GL_BGRA_INTEGER, 10, true },
138 struct read_format_info {
139 GLenum format, type;
140 int size;
141 bool sign;
144 /* Integer formats from table 3.5 and 3.6 of the GL 3.0 specification */
145 static const struct read_format_info read_formats[] = {
146 { GL_RGBA_INTEGER, GL_UNSIGNED_INT, 32, false },
147 { GL_RGBA_INTEGER, GL_INT, 32, true },
148 { GL_RGBA_INTEGER, GL_UNSIGNED_SHORT, 16, false },
149 { GL_RGBA_INTEGER, GL_SHORT, 16, true },
150 { GL_RGBA_INTEGER, GL_UNSIGNED_BYTE, 8, false },
151 { GL_RGBA_INTEGER, GL_BYTE, 8, true },
153 { GL_RED_INTEGER, GL_UNSIGNED_INT, 32, false },
154 { GL_RED_INTEGER, GL_INT, 32, true },
155 { GL_RED_INTEGER, GL_UNSIGNED_SHORT, 16, false },
156 { GL_RED_INTEGER, GL_SHORT, 16, true },
157 { GL_RED_INTEGER, GL_UNSIGNED_BYTE, 8, false },
158 { GL_RED_INTEGER, GL_BYTE, 8, true },
160 { GL_GREEN_INTEGER, GL_UNSIGNED_INT, 32, false },
161 { GL_GREEN_INTEGER, GL_INT, 32, true },
162 { GL_GREEN_INTEGER, GL_UNSIGNED_SHORT, 16, false },
163 { GL_GREEN_INTEGER, GL_SHORT, 16, true },
164 { GL_GREEN_INTEGER, GL_UNSIGNED_BYTE, 8, false },
165 { GL_GREEN_INTEGER, GL_BYTE, 8, true },
167 { GL_BLUE_INTEGER, GL_UNSIGNED_INT, 32, false },
168 { GL_BLUE_INTEGER, GL_INT, 32, true },
169 { GL_BLUE_INTEGER, GL_UNSIGNED_SHORT, 16, false },
170 { GL_BLUE_INTEGER, GL_SHORT, 16, true },
171 { GL_BLUE_INTEGER, GL_UNSIGNED_BYTE, 8, false },
172 { GL_BLUE_INTEGER, GL_BYTE, 8, true },
174 { GL_ALPHA_INTEGER, GL_UNSIGNED_INT, 32, false },
175 { GL_ALPHA_INTEGER, GL_INT, 32, true },
176 { GL_ALPHA_INTEGER, GL_UNSIGNED_SHORT, 16, false },
177 { GL_ALPHA_INTEGER, GL_SHORT, 16, true },
178 { GL_ALPHA_INTEGER, GL_UNSIGNED_BYTE, 8, false },
179 { GL_ALPHA_INTEGER, GL_BYTE, 8, true },
181 { GL_RG_INTEGER, GL_UNSIGNED_INT, 32, false },
182 { GL_RG_INTEGER, GL_INT, 32, true },
183 { GL_RG_INTEGER, GL_UNSIGNED_SHORT, 16, false },
184 { GL_RG_INTEGER, GL_SHORT, 16, true },
185 { GL_RG_INTEGER, GL_UNSIGNED_BYTE, 8, false },
186 { GL_RG_INTEGER, GL_BYTE, 8, true },
188 { GL_RGB_INTEGER, GL_UNSIGNED_INT, 32, false },
189 { GL_RGB_INTEGER, GL_INT, 32, true },
190 { GL_RGB_INTEGER, GL_UNSIGNED_SHORT, 16, false },
191 { GL_RGB_INTEGER, GL_SHORT, 16, true },
192 { GL_RGB_INTEGER, GL_UNSIGNED_BYTE, 8, false },
193 { GL_RGB_INTEGER, GL_BYTE, 8, true },
195 /* RGBA was put at the top so that the more obvious failures come first. */
197 { GL_BGR_INTEGER, GL_UNSIGNED_INT, 32, false },
198 { GL_BGR_INTEGER, GL_INT, 32, true },
199 { GL_BGR_INTEGER, GL_UNSIGNED_SHORT, 16, false },
200 { GL_BGR_INTEGER, GL_SHORT, 16, true },
201 { GL_BGR_INTEGER, GL_UNSIGNED_BYTE, 8, false },
202 { GL_BGR_INTEGER, GL_BYTE, 8, true },
204 { GL_BGRA_INTEGER, GL_UNSIGNED_INT, 32, false },
205 { GL_BGRA_INTEGER, GL_INT, 32, true },
206 { GL_BGRA_INTEGER, GL_UNSIGNED_SHORT, 16, false },
207 { GL_BGRA_INTEGER, GL_SHORT, 16, true },
208 { GL_BGRA_INTEGER, GL_UNSIGNED_BYTE, 8, false },
209 { GL_BGRA_INTEGER, GL_BYTE, 8, true },
211 /* FINISHME: Add more RGB10_A2UI. Note the other packed formats
212 * besides 10/10/10/2 included in the spec!
214 { GL_RGBA_INTEGER, GL_UNSIGNED_INT_10_10_10_2, 32, false },
215 { GL_RGBA_INTEGER, GL_UNSIGNED_INT_2_10_10_10_REV, 32, false },
218 static bool test_rg = false;
219 static bool test_rgb10_a2ui = false;
221 static void
222 usage(void)
224 fprintf(stderr, "usage:\n");
225 fprintf(stderr,
226 "ext_texture_integer-teximage-clamping "
227 "[GL_ARB_texture_rg | GL_ARB_texture_rgb10_a2ui]\n");
228 exit(1);
231 static const uint32_t texels_u[][4] = {
232 { 0xfffffff0, 0x00000000, 0x00000000, 0x00000000 },
233 { 0x00000000, 0xfffffff0, 0x00000000, 0x00000000 },
234 { 0x00000000, 0x00000000, 0xfffffff0, 0x00000000 },
235 { 0x00000000, 0x00000000, 0x00000000, 0xfffffff0 },
236 { 0xffffffff, 0xffffffff, 0xffffffff, 0xffffffff },
239 static const uint32_t texels_s[][4] = {
240 { 0x7fffffff, 0x80000000, 0x00000000, 0x00000000 },
241 { 0x00000000, 0x7fffffff, 0x80000000, 0x00000000 },
242 { 0x00000000, 0x00000000, 0x7fffffff, 0x80000000 },
243 { 0x80000000, 0x00000000, 0x00000000, 0x7fffffff },
244 { 0xffffffff, 0xffffffff, 0xffffffff, 0xffffffff },
247 static void
248 print_packed(const struct read_format_info *read_info, char *read,
249 int pixel, int chans)
251 int i;
253 char *read_pixel = read + pixel * chans * read_info->size / 8;
255 switch (read_info->type) {
256 case GL_UNSIGNED_INT:
257 case GL_INT:
258 for (i = 0; i < chans; i++)
259 fprintf(stderr, " 0x%08x", ((uint32_t *)read_pixel)[i]);
260 for (; i < 4; i++)
261 fprintf(stderr, " ");
262 break;
264 case GL_UNSIGNED_SHORT:
265 case GL_SHORT:
266 for (i = 0; i < chans; i++)
267 fprintf(stderr, " 0x%04x", ((uint16_t *)read)[i]);
268 for (; i < 4; i++)
269 fprintf(stderr, " ");
270 break;
272 case GL_UNSIGNED_BYTE:
273 case GL_BYTE:
274 for (i = 0; i < chans; i++)
275 fprintf(stderr, " 0x%02x", ((uint8_t *)read)[i]);
276 for (; i < 4; i++)
277 fprintf(stderr, " ");
278 break;
280 default:
281 /* FINISHME: GL_texture_rgb10_a2ui */
282 abort();
286 static void
287 report_fail(const struct format_info *tex_info,
288 const char *format_name,
289 const struct read_format_info *read_info,
290 uint32_t texels[][4],
291 void *read, void *expected,
292 int pixels, int chans)
294 int i;
296 fprintf(stderr, "Failure reading from %s to %s/%s\n",
297 format_name,
298 piglit_get_gl_enum_name(read_info->format),
299 piglit_get_gl_enum_name(read_info->type));
301 /* 10/channel + 3 spaces. */
302 fprintf(stderr, " %43s", "expected RGBA in texels");
303 if (read_info->size == 32)
304 fprintf(stderr, " %43s", "expected packed");
305 else if (read_info->size == 16)
306 fprintf(stderr, " %31s", "expected packed");
307 else if (read_info->size == 8)
308 fprintf(stderr, " %19s", "expected packed");
309 else
310 abort(); /* FINISHME: GL_ARB_texture_rgb10_a2ui */
311 fprintf(stderr, " read values");
312 fprintf(stderr, "\n");
313 for (i = 0; i < pixels; i++) {
314 fprintf(stderr, " 0x%08x 0x%08x 0x%08x 0x%08x",
315 texels[i][0],
316 texels[i][1],
317 texels[i][2],
318 texels[i][3]);
320 fprintf(stderr, " ");
321 print_packed(read_info, expected, i, chans);
322 fprintf(stderr, " ");
323 print_packed(read_info, read, i, chans);
325 fprintf(stderr, "\n");
329 static void
330 pack(const struct read_format_info *read_info, void *packed,
331 int pixel, int chan,
332 int values_per_pixel, uint32_t value)
334 int32_t ivalue = value;
335 void *pack_dst = ((char *)packed) + ((pixel * values_per_pixel +
336 chan) * read_info->size / 8);
338 switch (read_info->type) {
339 case GL_UNSIGNED_INT:
340 case GL_INT:
341 *(uint32_t *)pack_dst = value;
342 break;
344 case GL_UNSIGNED_SHORT:
345 if (value > 0xffff)
346 value = 0xffff;
347 *(uint16_t *)pack_dst = value;
348 break;
350 case GL_SHORT:
351 if (ivalue > 32767)
352 ivalue = 32767;
353 else if (ivalue < -32768)
354 ivalue = -32768;
355 *(int16_t *)pack_dst = ivalue;
356 break;
358 case GL_UNSIGNED_BYTE:
359 if (value > 0xff)
360 value = 0xff;
361 *(uint8_t *)pack_dst = value;
362 break;
364 case GL_BYTE:
365 if (ivalue > 127)
366 ivalue = 127;
367 else if (ivalue < -128)
368 ivalue = -128;
369 *(int8_t *)pack_dst = ivalue;
370 break;
372 default:
373 /* FINISHME: GL_texture_rgb10_a2ui */
374 abort();
378 static enum piglit_result
379 read_format(const struct format_info *tex_info,
380 const struct read_format_info *read_info,
381 uint32_t texels[][4], int num_texels)
383 size_t texels_size = num_texels * 4 * sizeof(uint32_t);
384 char *expected;
385 char *read;
386 int i;
387 int chans = 0;
388 enum piglit_result result;
390 const char *suffix;
391 char *name;
392 if (tex_info->internal_format == GL_RGB10_A2UI) {
393 if (tex_info->base_format == GL_BGRA_INTEGER) {
394 if (tex_info->sign)
395 suffix = " (rev bgra)";
396 else
397 suffix = " (bgra)";
398 } else {
399 if (tex_info->sign)
400 suffix = " (rev)";
401 else
402 suffix = "";
404 } else if (tex_info->base_format == GL_BGRA_INTEGER)
405 suffix = " (bgra)";
406 else
407 suffix = "";
409 if (!test_rg && (read_info->format == GL_RED_INTEGER ||
410 read_info->format == GL_RG_INTEGER)) {
411 return PIGLIT_SKIP;
414 if (!test_rgb10_a2ui) {
415 /* Packed integer pixel formats were introduced with
416 * GL_texture_rgb10_a2ui.
418 switch (read_info->type) {
419 case GL_INT:
420 case GL_UNSIGNED_INT:
421 case GL_SHORT:
422 case GL_UNSIGNED_SHORT:
423 case GL_BYTE:
424 case GL_UNSIGNED_BYTE:
425 break;
426 default:
427 return PIGLIT_SKIP;
431 /* FINISHME: Again, not really sure how to handle sign conversion. */
432 if (tex_info->sign != read_info->sign)
433 return PIGLIT_SKIP;
435 asprintf(&name, "%s%s:\n", piglit_get_gl_enum_name(tex_info->internal_format), suffix);
437 printf("Reading from %s to %s/%s\n", name,
438 piglit_get_gl_enum_name(read_info->format),
439 piglit_get_gl_enum_name(read_info->type));
441 expected = (char *)malloc(texels_size);
442 read = (char *)malloc(texels_size);
444 memset(expected, 0xd0, texels_size);
445 memset(read, 0xd0, texels_size);
447 glGetTexImage(GL_TEXTURE_2D, 0,
448 read_info->format, read_info->type, read);
450 switch (read_info->format) {
451 case GL_RGBA_INTEGER:
452 chans = 4;
453 for (i = 0; i < num_texels; i++) {
454 pack(read_info, expected, i, 0, chans, texels[i][0]);
455 pack(read_info, expected, i, 1, chans, texels[i][1]);
456 pack(read_info, expected, i, 2, chans, texels[i][2]);
457 pack(read_info, expected, i, 3, chans, texels[i][3]);
459 break;
461 case GL_BGRA_INTEGER:
462 chans = 4;
463 for (i = 0; i < num_texels; i++) {
464 pack(read_info, expected, i, 0, chans, texels[i][2]);
465 pack(read_info, expected, i, 1, chans, texels[i][1]);
466 pack(read_info, expected, i, 2, chans, texels[i][0]);
467 pack(read_info, expected, i, 3, chans, texels[i][3]);
469 break;
471 case GL_RGB_INTEGER:
472 chans = 3;
473 for (i = 0; i < num_texels; i++) {
474 pack(read_info, expected, i, 0, chans, texels[i][0]);
475 pack(read_info, expected, i, 1, chans, texels[i][1]);
476 pack(read_info, expected, i, 2, chans, texels[i][2]);
478 break;
480 case GL_BGR_INTEGER:
481 chans = 3;
482 for (i = 0; i < num_texels; i++) {
483 pack(read_info, expected, i, 0, chans, texels[i][2]);
484 pack(read_info, expected, i, 1, chans, texels[i][1]);
485 pack(read_info, expected, i, 2, chans, texels[i][0]);
487 break;
489 case GL_RED_INTEGER:
490 chans = 1;
491 for (i = 0; i < num_texels; i++) {
492 pack(read_info, expected, i, 0, chans, texels[i][0]);
494 break;
496 case GL_GREEN_INTEGER:
497 chans = 1;
498 for (i = 0; i < num_texels; i++) {
499 pack(read_info, expected, i, 0, chans, texels[i][1]);
501 break;
503 case GL_BLUE_INTEGER:
504 chans = 1;
505 for (i = 0; i < num_texels; i++) {
506 pack(read_info, expected, i, 0, chans, texels[i][2]);
508 break;
510 case GL_ALPHA_INTEGER:
511 chans = 1;
512 for (i = 0; i < num_texels; i++) {
513 pack(read_info, expected, i, 0, chans, texels[i][3]);
515 break;
517 case GL_RG_INTEGER:
518 chans = 2;
519 for (i = 0; i < num_texels; i++) {
520 pack(read_info, expected, i, 0, chans, texels[i][0]);
521 pack(read_info, expected, i, 1, chans, texels[i][1]);
523 break;
525 default:
526 assert(0);
527 return PIGLIT_SKIP;
530 if (memcmp(expected, read, num_texels * chans * read_info->size / 8)) {
531 report_fail(tex_info, name, read_info, texels, read, expected,
532 num_texels, chans);
533 result = PIGLIT_FAIL;
534 } else {
535 result = PIGLIT_PASS;
538 free(read);
539 free(expected);
540 free(name);
542 return result;
545 static enum piglit_result
546 test_format(const struct format_info *info)
548 int num_texels = ARRAY_SIZE(texels_s);
549 uint32_t texels[ARRAY_SIZE(texels_s)][4];
550 GLenum type;
551 int lbits, abits, ibits, rbits, gbits, bbits;
552 int i, readf;
553 enum piglit_result result = PIGLIT_SKIP;
554 const char *name = piglit_get_gl_enum_name(info->internal_format);
556 if (!test_rg && ((info->base_format == GL_RED_INTEGER &&
557 !strstr(name, "GL_INTENSITY")) ||
558 info->base_format == GL_RG_INTEGER)) {
559 return PIGLIT_SKIP;
562 if (!test_rgb10_a2ui && info->internal_format == GL_RGB10_A2UI)
563 return PIGLIT_SKIP;
565 /* FINISHME: We only test conversion from large signed to
566 * small signed or large unsigned to small unsigned. The
567 * rules just say that when reading pixels, the value is
568 * clamped to the representable range, not how/when sign
569 * extension occurs, and whether the clamping applies before
570 * or after
572 if (info->size != 32)
573 return PIGLIT_SKIP;
575 if (info->sign) {
576 memcpy(texels, texels_s, sizeof(texels_s));
577 type = GL_INT;
578 } else {
579 memcpy(texels, texels_u, sizeof(texels_u));
580 type = GL_UNSIGNED_INT;
582 glTexImage2D(GL_TEXTURE_2D, 0, info->internal_format, num_texels, 1, 0,
583 GL_RGBA_INTEGER_EXT, type, texels);
585 glGetTexLevelParameteriv(GL_TEXTURE_2D, 0,
586 GL_TEXTURE_LUMINANCE_SIZE, &lbits);
587 glGetTexLevelParameteriv(GL_TEXTURE_2D, 0,
588 GL_TEXTURE_ALPHA_SIZE, &abits);
589 glGetTexLevelParameteriv(GL_TEXTURE_2D, 0,
590 GL_TEXTURE_INTENSITY_SIZE, &ibits);
591 glGetTexLevelParameteriv(GL_TEXTURE_2D, 0,
592 GL_TEXTURE_RED_SIZE, &rbits);
593 glGetTexLevelParameteriv(GL_TEXTURE_2D, 0,
594 GL_TEXTURE_GREEN_SIZE, &gbits);
595 glGetTexLevelParameteriv(GL_TEXTURE_2D, 0,
596 GL_TEXTURE_BLUE_SIZE, &bbits);
598 /* Compute the RGBA channels that should be read from the
599 * texture given the input RGBA texels we gave. See Table 6.1
600 * ("Texture, table, and filter return values") of the GL 3.0
601 * specification. Note that input R is always mapped to L or
602 * I, and comes back out in R (except for ALPHA).
604 if (ibits || lbits) {
605 for (i = 0; i < num_texels; i++) {
606 texels[i][1] = 0;
607 texels[i][2] = 0;
609 } else {
610 if (!rbits) {
611 for (i = 0; i < num_texels; i++)
612 texels[i][0] = 0;
614 if (!gbits) {
615 for (i = 0; i < num_texels; i++)
616 texels[i][1] = 0;
618 if (!bbits) {
619 for (i = 0; i < num_texels; i++)
620 texels[i][2] = 0;
623 /* Everybody's consistent on A bits in table 6.1. */
624 if (!abits) {
625 for (i = 0; i < num_texels; i++)
626 texels[i][3] = 1;
629 for (readf = 0; readf < ARRAY_SIZE(read_formats); readf++) {
630 piglit_merge_result(&result, read_format(info,
631 &read_formats[readf],
632 texels, num_texels));
634 if (result == PIGLIT_FAIL)
635 return result;
638 return result;
642 enum piglit_result
643 piglit_display(void)
645 /* unreached */
646 return PIGLIT_FAIL;
650 void
651 piglit_init(int argc, char **argv)
653 GLuint tex;
654 int i, texf;
655 enum piglit_result result = PIGLIT_SKIP;
657 /* Note: We test later extensions by testing them against all
658 * combinations of (storage format, read format) including the
659 * formats from previous extensions.
661 for (i = 1; i < argc; i++) {
662 if (strcmp(argv[i], "GL_ARB_texture_rg") == 0) {
663 piglit_require_extension(argv[i]);
664 test_rg = true;
665 } else if (strcmp(argv[i], "GL_ARB_texture_rgb10_a2ui") == 0) {
666 piglit_require_extension(argv[i]);
667 test_rg = true;
668 test_rgb10_a2ui = true;
669 } else {
670 usage();
671 exit(1);
675 piglit_require_extension("GL_EXT_texture_integer");
677 glGenTextures(1, &tex);
678 glBindTexture(GL_TEXTURE_2D, tex);
679 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
680 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
682 for (texf = 0; texf < ARRAY_SIZE(formats); texf++)
683 piglit_merge_result(&result, test_format(&formats[texf]));
685 piglit_report_result(result);