summaryrefslogtreecommitdiff
path: root/tests/texturing/texsubimage.c
blob: 5d38736da778b4e58b9c3d58522b180e19ccbdd9 (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
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
/*
 * Copyright © 2011 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
 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
 * 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 NONINFRINGEMENT.  IN NO EVENT SHALL
 * THE AUTHORS OR COPYRIGHT HOLDERS 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.
 *
 */


/**
 * This should expose any errors in texel addressing within a texture image
 * when calling glTexSubImage1D/2D/3D().
 *
 * Brian Paul
 * October 2011
 */


#include "piglit-util-gl.h"
#include "../fbo/fbo-formats.h"

#define STR(x) #x
#define STRINGIFY(x) STR(x)

PIGLIT_GL_TEST_CONFIG_BEGIN

	config.supports_gl_compat_version = 10;

	config.window_visual = PIGLIT_GL_VISUAL_RGBA | PIGLIT_GL_VISUAL_DOUBLE;

	config.window_width = 512;
	config.window_height = 512;

PIGLIT_GL_TEST_CONFIG_END

/**
 * This is a subset of the formats in fbo-formats.h
 * We don't test non-color, float, or int/uint textures at this time.
 */
static const struct test_desc texsubimage_test_sets[] = {
	{
		core,
		ARRAY_SIZE(core),
		"Core formats",
		GL_UNSIGNED_NORMALIZED,
	},
	{
		tdfx_texture_compression_fxt1,
		ARRAY_SIZE(tdfx_texture_compression_fxt1),
		"GL_3DFX_texture_compression_FXT1",
		GL_UNSIGNED_NORMALIZED,
		{"GL_ARB_texture_compression",
		 "GL_3DFX_texture_compression_FXT1"},
	},
	{
		ext_texture_compression_s3tc,
		ARRAY_SIZE(ext_texture_compression_s3tc),
		"GL_EXT_texture_compression_s3tc",
		GL_UNSIGNED_NORMALIZED,
		{"GL_ARB_texture_compression",
		 "GL_EXT_texture_compression_s3tc"},
	},
	{
		ext_texture_compression_rgtc,
		ARRAY_SIZE(ext_texture_compression_rgtc),
		"GL_EXT_texture_compression_rgtc",
		GL_UNSIGNED_NORMALIZED,
		{"GL_EXT_texture_compression_rgtc"}
	},
	{
		ext_texture_compression_latc,
		ARRAY_SIZE(ext_texture_compression_latc),
		"GL_EXT_texture_compression_latc",
		GL_UNSIGNED_NORMALIZED,
		{"GL_EXT_texture_compression_latc"}
	}
};

/* Default texture size. Other values might be used if the texture has
 * less dimensions or other restrictions */
#define DEFAULT_TEX_WIDTH 128
#define DEFAULT_TEX_HEIGHT 64
#define DEFAULT_TEX_DEPTH 8

/* List of texture targets to test, terminated by GL_NONE */
static const GLenum *test_targets;

/* If set to GL_TRUE then the texture sub image upload will be read
 * from a PBO */
static GLboolean use_pbo = GL_FALSE;

static const char fragment_1d_array[] =
	"#extension GL_EXT_texture_array : require\n"
	"uniform sampler1DArray tex;\n"
	"const float TEX_HEIGHT = " STRINGIFY(DEFAULT_TEX_HEIGHT) ".0;\n"
	"void\n"
	"main()\n"
	"{\n"
	"        float layer = gl_TexCoord[0].t * TEX_HEIGHT - 0.5;\n"
	"        gl_FragColor = texture1DArray(tex, vec2(gl_TexCoord[0].s,\n"
	"                                                layer));\n"
	"}\n";

static const char fragment_2d_array[] =
	"#extension GL_EXT_texture_array : require\n"
	"uniform sampler2DArray tex;\n"
	"const float TEX_DEPTH = " STRINGIFY(DEFAULT_TEX_DEPTH) ".0;\n"
	"void\n"
	"main()\n"
	"{\n"
	"        float layer = gl_TexCoord[0].p * TEX_DEPTH - 0.5;\n"
	"        gl_FragColor = texture2DArray(tex, vec3(gl_TexCoord[0].st,\n"
	"                                                layer));\n"
	"}\n";

static const char vertex_cube_map_array[] =
	"const float N_SIDES = 6.0;\n"
	"const float TEX_DEPTH = " STRINGIFY(DEFAULT_TEX_DEPTH) ".0 *\n"
	"                        N_SIDES;\n"
	"void\n"
	"main()\n"
	"{\n"
	"        vec2 face_coord;\n"
	"        vec3 res;\n"
	"        float slice = gl_MultiTexCoord0.p * TEX_DEPTH - 0.5;\n"
	"        float layer = floor(slice / N_SIDES);\n"
	"        int face = int(floor(mod(slice, N_SIDES)));\n"
	"\n"
	"        face_coord = gl_MultiTexCoord0.st * 2.0 - 1.0;\n"
	"        if (face == 0)\n"
	"                res = vec3(1.0, -face_coord.ts);\n"
	"        else if (face == 1)\n"
	"                res = vec3(-1.0, face_coord.ts * vec2(-1.0, 1.0));\n"
	"        else if (face == 2)\n"
	"                res = vec3(face_coord.s, 1.0, face_coord.t);\n"
	"        else if (face == 3)\n"
	"                res = vec3(face_coord.s, -1.0, -face_coord.t);\n"
	"        else if (face == 4)\n"
	"                res = vec3(face_coord.st * vec2(1.0, -1.0), 1.0);\n"
	"        else\n"
	"                res = vec3(-face_coord.st, -1.0);\n"
	"        gl_TexCoord[0] = vec4(res, layer);\n"
	"        gl_Position = ftransform();\n"
	"}\n";

static const char fragment_cube_map_array[] =
	"#extension GL_ARB_texture_cube_map_array : require\n"
	"uniform samplerCubeArray tex;\n"
	"void\n"
	"main()\n"
	"{\n"
	"        gl_FragColor = texture(tex, gl_TexCoord[0]);\n"
	"}\n";

/**
 * XXX add this to piglit-util if useful elsewhere.
 */
static GLvoid
piglit_draw_rect_tex3d(float x, float y, float w, float h,
		       float tx, float ty, float tw, float th,
		       float tz0, float tz1)
{
	float verts[4][4];
	float tex[4][3];

	verts[0][0] = x;
	verts[0][1] = y;
	verts[0][2] = 0.0;
	verts[0][3] = 1.0;
	tex[0][0] = tx;
	tex[0][1] = ty;
	tex[0][2] = tz0;
	verts[1][0] = x + w;
	verts[1][1] = y;
	verts[1][2] = 0.0;
	verts[1][3] = 1.0;
	tex[1][0] = tx + tw;
	tex[1][1] = ty;
	tex[1][2] = tz1;
	verts[2][0] = x + w;
	verts[2][1] = y + h;
	verts[2][2] = 0.0;
	verts[2][3] = 1.0;
	tex[2][0] = tx + tw;
	tex[2][1] = ty + th;
	tex[2][2] = tz1;
	verts[3][0] = x;
	verts[3][1] = y + h;
	verts[3][2] = 0.0;
	verts[3][3] = 1.0;
	tex[3][0] = tx;
	tex[3][1] = ty + th;
	tex[3][2] = tz0;

	glVertexPointer(4, GL_FLOAT, 0, verts);
	glTexCoordPointer(3, GL_FLOAT, 0, tex);
	glEnableClientState(GL_VERTEX_ARRAY);
	glEnableClientState(GL_TEXTURE_COORD_ARRAY);

	glDrawArrays(GL_QUADS, 0, 4);

	glDisableClientState(GL_VERTEX_ARRAY);
	glDisableClientState(GL_TEXTURE_COORD_ARRAY);
}

static GLboolean
equal_images(GLenum target,
	     const GLubyte *original_ref,
	     const GLubyte *updated_ref,
	     const GLubyte *testImg,
	     GLuint w, GLuint h, GLuint d,
	     GLuint tx, GLuint ty, GLuint tz,
	     GLuint tw, GLuint th, GLuint td)
{
        GLboolean res = GL_TRUE;
	const GLubyte *ref;
	GLuint z, y, x;

	switch (target) {
	case GL_TEXTURE_1D:
		ty = 0;
		th = 1;
		/* flow through */
	case GL_TEXTURE_2D:
	case GL_TEXTURE_1D_ARRAY:
		tz = 0;
		td = 1;
		break;
	}

	for (z = 0; z < d; z++) {
		for (y = 0; y < h; y++) {
			for (x = 0; x < w; x++) {
				if (x >= tx && x < tx + tw &&
				    y >= ty && y < ty + th &&
				    z >= tz && z < tz + td)
					ref = updated_ref;
				else
					ref = original_ref;

				if (memcmp(ref, testImg, 4)) {
                                        printf("%u:%u:%u:%u %u:%u:%u:%u %d %d %d\n",
                                               ref[0], ref[1], ref[2], ref[3],
                                               testImg[0], testImg[1], testImg[2], testImg[3],
                                               x, y, z);
                                        res = GL_FALSE;
                                }

				testImg += 4;
				original_ref += 4;
				updated_ref += 4;
			}
		}
	}

	return res;
}

/**
 * Get block size for compressed format.
 * \return GL_TRUE if format is compressed, GL_FALSE otherwise
 * XXX this could be a piglit util function if useful elsewhere.
 */
static GLboolean
get_format_block_size(GLenum format, GLuint *bw, GLuint *bh)
{
	switch (format) {
	case GL_COMPRESSED_RGB_FXT1_3DFX:
	case GL_COMPRESSED_RGBA_FXT1_3DFX:
		*bw = 8;
		*bh = 4;
		return GL_TRUE;
	case GL_COMPRESSED_RGB_S3TC_DXT1_EXT:
	case GL_COMPRESSED_RGBA_S3TC_DXT1_EXT:
	case GL_COMPRESSED_RGBA_S3TC_DXT3_EXT:
	case GL_COMPRESSED_RGBA_S3TC_DXT5_EXT:
	case GL_COMPRESSED_SRGB_S3TC_DXT1_EXT:
	case GL_COMPRESSED_SRGB_ALPHA_S3TC_DXT1_EXT:
	case GL_COMPRESSED_SRGB_ALPHA_S3TC_DXT3_EXT:
	case GL_COMPRESSED_SRGB_ALPHA_S3TC_DXT5_EXT:
		*bw = 4;
		*bh = 4;
		return GL_TRUE;
	case GL_COMPRESSED_RED:
	case GL_COMPRESSED_RED_RGTC1_EXT:
	case GL_COMPRESSED_RG:
	case GL_COMPRESSED_RED_GREEN_RGTC2_EXT:
		*bw = 4;
		*bh = 4;
		return GL_TRUE;
	case GL_COMPRESSED_LUMINANCE_LATC1_EXT:
	case GL_COMPRESSED_LUMINANCE_ALPHA_LATC2_EXT:
		*bw = 4;
		*bh = 4;
		return GL_TRUE;
	default:
		*bw = *bh = 1;
		return GL_FALSE;
	}
}

/**
 * Draw each image of the texture to the framebuffer and then save the
 * entire thing to a buffer with glReadPixels().
 */
static void
draw_and_read_texture(GLuint w, GLuint h, GLuint d, GLubyte *ref)
{
	int i;

	for (i = 0; i < d; i++) {
		float tz = (i + 0.5f) / d;
		piglit_draw_rect_tex3d(i / 8 * w, i % 8 * h, /* x/y */
				       w, h,
				       0.0, 0.0, /* tx/ty */
				       1.0, 1.0, /* tw/th */
				       tz, tz /* tz0/tz1 */);
	}

	for (i = 0; i < d; i += 8) {
		glReadPixels(i / 8 * w, i % 8 * h,
			     w, h * MIN2(8, d - i),
			     GL_RGBA, GL_UNSIGNED_BYTE,
			     ref);
		ref += 8 * w * h * 4;
	}
}

static GLuint
create_texture(GLenum target,
	       GLenum intFormat,
	       GLsizei w, GLsizei h, GLsizei d,
	       GLenum srcFormat,
	       const GLubyte *img)
{
	GLuint tex;

	glPixelStorei(GL_UNPACK_ROW_LENGTH, w);
	glPixelStorei(GL_UNPACK_IMAGE_HEIGHT, h);

	glGenTextures(1, &tex);
	glBindTexture(target, tex);
	glTexParameteri(target, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
	glTexParameteri(target, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
	glPixelStorei(GL_UNPACK_SKIP_PIXELS, 0);
	glPixelStorei(GL_UNPACK_SKIP_ROWS, 0);
	glPixelStorei(GL_UNPACK_SKIP_IMAGES, 0);
	if (d > 1) {
		glTexImage3D(target, 0, intFormat, w, h, d, 0,
			     srcFormat, GL_UNSIGNED_BYTE, img);
	}
	else if (h > 1) {
		glTexImage2D(target, 0, intFormat, w, h, 0,
			     srcFormat, GL_UNSIGNED_BYTE, img);
	}
	else if (w > 1) {
		glTexImage1D(target, 0, intFormat, w, 0,
			     srcFormat, GL_UNSIGNED_BYTE, img);
	} else {
		assert(!"Unknown texture dimensions");
	}

	return tex;
}

/**
 * Create two textures with different reference values. Draw both of
 * the textures to the framebuffer and save the reference images with
 * glReadPixels.
 *
 * Loop:
 *  - Create another texture with the same initial values as the first
 *    texture
 *  - replace a random sub-region of the texture image with values from
 *    the 2nd texture
 *  - draw the texture to the framebuffer and read back with glReadPixels
 *  - compare reference images to test image choosing either the first
 *    or second reference image for each pixel depending on whether it
 *    is within the updated region
 * \param target  GL_TEXTURE_1D/2D/3D
 * \param intFormat  the internal texture format
 */
static GLboolean
test_format(GLenum target, GLenum intFormat)
{
	const GLenum srcFormat = GL_RGBA;
	GLuint w = DEFAULT_TEX_WIDTH;
	GLuint h = DEFAULT_TEX_HEIGHT;
	GLuint d = DEFAULT_TEX_DEPTH;
	GLuint tex, i, j, k, n, t;
	GLubyte *original_img, *original_ref;
	GLubyte *updated_img, *updated_ref;
	GLubyte *testImg;
	GLboolean pass = GL_TRUE;
	GLuint bw, bh, wMask, hMask, dMask;
	GLuint pbo = 0;
	get_format_block_size(intFormat, &bw, &bh);
	wMask = ~(bw-1);
	hMask = ~(bh-1);
	dMask = ~0;

	if (target == GL_TEXTURE_CUBE_MAP_ARRAY_ARB) {
		w = h;
		d *= 6;
	} else if (target != GL_TEXTURE_3D && target != GL_TEXTURE_2D_ARRAY) {
		d = 1;
	}

	if (target == GL_TEXTURE_1D)
		h = 1;

	original_img = (GLubyte *) malloc(w * h * d * 4);
	original_ref = (GLubyte *) malloc(w * h * d * 4);
	updated_img = (GLubyte *) malloc(w * h * d * 4);
	updated_ref = (GLubyte *) malloc(w * h * d * 4);
	testImg = (GLubyte *) malloc(w * h * d * 4);

	/* fill source tex images */
	n = 0;
	for (i = 0; i < d; i++) {
		for (j = 0; j < h; j++) {
			for (k = 0; k < w; k++) {
				original_img[n + 0] = j * 4;
				original_img[n + 1] = k * 2;
				original_img[n + 2] = i * 128 / d;
				original_img[n + 3] = 255;

				/* Swizzle the components in the
				 * updated image
				 */
				updated_img[n + 0] = original_img[n + 1];
				updated_img[n + 1] = original_img[n + 2];
				updated_img[n + 2] = original_img[n + 0];
				updated_img[n + 3] = original_img[n + 3];

				n += 4;
			}
		}
	}

	if (use_pbo) {
		glGenBuffers(1, &pbo);
		glBindBuffer(GL_PIXEL_UNPACK_BUFFER, pbo);
		glBufferData(GL_PIXEL_UNPACK_BUFFER,
			     w * h * d * 4,
			     updated_img,
			     GL_STATIC_DRAW);
		glBindBuffer(GL_PIXEL_UNPACK_BUFFER, 0);
	}

	glTexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE);


	/* draw original reference image */
	tex = create_texture(target, intFormat, w, h, d,
			     srcFormat, original_img);
	glClear(GL_COLOR_BUFFER_BIT);
	draw_and_read_texture(w, h, d, original_ref);
	glDeleteTextures(1, &tex);

	/* draw updated reference image */
	tex = create_texture(target, intFormat, w, h, d,
			     srcFormat, updated_img);
	glClear(GL_COLOR_BUFFER_BIT);
	draw_and_read_texture(w, h, d, updated_ref);
	glDeleteTextures(1, &tex);

	for (t = 0; t < 10; t++) {
		/* Choose random region of texture to update.
		 * Use sizes and positions that are multiples of
		 * the compressed block size.
		 */
		GLint tw = (rand() % w) & wMask;
		GLint th = (rand() % h) & hMask;
		GLint td = (rand() % d) & dMask;
		GLint tx = (rand() % (w - tw)) & wMask;
		GLint ty = (rand() % (h - th)) & hMask;
		GLint tz = (rand() % (d - td)) & dMask;

		/* Recreate the original texture */
		tex = create_texture(target, intFormat, w, h, d,
				     srcFormat, original_img);

		assert(tx + tw <= w);
		assert(ty + th <= h);
		assert(tz + td <= d);

		if (use_pbo)
			glBindBuffer(GL_PIXEL_UNPACK_BUFFER, pbo);

		/* replace texture region with data from updated image */
		glPixelStorei(GL_UNPACK_SKIP_PIXELS, tx);
		glPixelStorei(GL_UNPACK_SKIP_ROWS, ty);
		glPixelStorei(GL_UNPACK_SKIP_IMAGES, tz);
		if (d > 1) {
			glTexSubImage3D(target, 0, tx, ty, tz, tw, th, td,
					srcFormat, GL_UNSIGNED_BYTE,
					use_pbo ? NULL : updated_img);
		} else if (h > 1) {
			glTexSubImage2D(target, 0, tx, ty, tw, th,
					srcFormat, GL_UNSIGNED_BYTE,
					use_pbo ? NULL : updated_img);
		} else if (w > 1) {
			glTexSubImage1D(target, 0, tx, tw,
					srcFormat, GL_UNSIGNED_BYTE,
					use_pbo ? NULL : updated_img);
		} else {
			assert(!"Unknown image dimensions");
		}

		if (use_pbo)
			glBindBuffer(GL_PIXEL_UNPACK_BUFFER, 0);

		/* draw test image */
		glClear(GL_COLOR_BUFFER_BIT);
		draw_and_read_texture(w, h, d, testImg);

		glDeleteTextures(1, &tex);

		piglit_present_results();

		if (!equal_images(target,
				  original_ref, updated_ref, testImg,
				  w, h, d,
				  tx, ty, tz, tw, th, td)) {
			printf("texsubimage failed\n");
			printf("  target: %s\n", piglit_get_gl_enum_name(target));
			printf("  internal format: %s\n", piglit_get_gl_enum_name(intFormat));
			printf("  region: %d, %d  %d x %d\n", tx, ty, tw, th);
			pass = GL_FALSE;
			break;
		}
	}

	free(original_img);
	free(original_ref);
	free(updated_img);
	free(updated_ref);
	free(testImg);
	if (use_pbo)
		glDeleteBuffers(1, &pbo);

	return pass;
}


/**
 * Test all formats in texsubimage_test_sets[] for the given
 * texture target.
 */
static GLboolean
test_formats(GLenum target)
{
	GLboolean pass = GL_TRUE;
	GLuint program = 0;
	int i, j;

	switch (target) {
	case GL_TEXTURE_1D_ARRAY:
		program = piglit_build_simple_program(NULL, fragment_1d_array);
		break;
	case GL_TEXTURE_2D_ARRAY:
		program = piglit_build_simple_program(NULL, fragment_2d_array);
		break;
	case GL_TEXTURE_CUBE_MAP_ARRAY:
		program = piglit_build_simple_program(vertex_cube_map_array,
						      fragment_cube_map_array);
		break;
	default:
		glEnable(target);
		break;
	}

	if (program != 0) {
		GLuint tex_location;

		glUseProgram(program);
		tex_location = glGetUniformLocation(program, "tex");
		glUniform1i(tex_location, 0);
	}

	/* loop over the format groups */
	for (i = 0; i < ARRAY_SIZE(texsubimage_test_sets); i++) {
		const struct test_desc *set = &texsubimage_test_sets[i];
		GLboolean skip = GL_FALSE;

		/* only test compressed formats with 2D textures */
		if (i > 0 && target != GL_TEXTURE_2D)
			continue;

		/* skip formats for unsupported extensions */
		for (j = 0; j < ARRAY_SIZE(set->ext); j++) {
			if (set->ext[j] &&
			    !piglit_is_extension_supported(set->ext[j])) {
				/* req'd extension not supported */
				skip = GL_TRUE;
				break;
			}
		}
		if (skip)
			continue;

		/* loop over formats in the set */
		for (j = 0; j < set->num_formats; j++) {
			if (!test_format(target,
					 set->format[j].internalformat)) {
				pass = GL_FALSE;
			}
		}
	}

	if (program == 0) {
		glDisable(target);
	} else {
		glUseProgram(0);
		glDeleteProgram(program);
	}

	return pass;
}


enum piglit_result
piglit_display(void)
{
	GLboolean pass = GL_TRUE;
	int i;

	/* Loop over 1/2/3D texture targets */
	for (i = 0; test_targets[i] != GL_NONE; i++) {
		pass = test_formats(test_targets[i]) && pass;
	}

	return pass ? PIGLIT_PASS : PIGLIT_FAIL;
}


void
piglit_init(int argc, char **argv)
{
	static const GLenum core_targets[] = {
		GL_TEXTURE_1D,
		GL_TEXTURE_2D,
		GL_TEXTURE_3D,
		GL_NONE
	};
	static const GLenum array_targets[] = {
		GL_TEXTURE_1D_ARRAY_EXT,
		GL_TEXTURE_2D_ARRAY_EXT,
		GL_NONE
	};
	static const GLenum cube_map_array_targets[] = {
		GL_TEXTURE_CUBE_MAP_ARRAY_ARB,
		GL_NONE
	};
	int remaining_argc = 1;
	int i;

	test_targets = core_targets;

	for (i = 1; i < argc; i++) {
		if (!strcmp(argv[i], "array")) {
			piglit_require_extension("GL_EXT_texture_array");
			piglit_require_GLSL();
			test_targets = array_targets;
		} else if (!strcmp(argv[i], "cube_map_array")) {
			piglit_require_extension
				("GL_ARB_texture_cube_map_array");
			piglit_require_GLSL();
			test_targets = cube_map_array_targets;
		} else if (!strcmp(argv[i], "pbo")) {
			piglit_require_extension("GL_ARB_pixel_buffer_object");
			use_pbo = GL_TRUE;
		} else {
			argv[remaining_argc++] = argv[i];
		}
	}

	fbo_formats_init(remaining_argc, argv, 0);
	(void) fbo_formats_display;

	piglit_ortho_projection(piglit_width, piglit_height, GL_FALSE);
}