summaryrefslogtreecommitdiff
path: root/piglit_ktx.c
blob: 517e1eaa8a735685aa587e9aa85d2d64729d5368 (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
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
/*
 * Copyright 2012 Intel Corporation
 *
 * 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.
 */

#include <assert.h>
#include <stdarg.h>
#include <stdlib.h>
#include <string.h>

#include "piglit_ktx.h"

/* FIXME: Remove #defines when piglit-dispatch gains support for GLES. */
#define GL_TEXTURE_1D				0x0DE0
#define GL_TEXTURE_1D_ARRAY			0x8C18
#define GL_TEXTURE_2D				0x0DE1
#define GL_TEXTURE_2D_ARRAY			0x8C1A
#define GL_TEXTURE_3D				0x806F
#define GL_TEXTURE_CUBE_MAP			0x8513
#define GL_TEXTURE_CUBE_MAP_ARRAY		0x9009

#define GL_TEXTURE_BINDING_1D			0x8068
#define GL_TEXTURE_BINDING_1D_ARRAY		0x8C1C
#define GL_TEXTURE_BINDING_2D			0x8069
#define GL_TEXTURE_BINDING_2D_ARRAY		0x8C1D
#define GL_TEXTURE_BINDING_3D			0x806A
#define GL_TEXTURE_BINDING_CUBE_MAP		0x8514
#define GL_TEXTURE_BINDING_CUBE_MAP_ARRAY	0x900A

static const int piglit_ktx_header_length = 64;
static const char piglit_ktx_magic_number[12] =
	{ 0xab, 'K', 'T', 'X', ' ', '1', '1', 0xbb, '\r', '\n', 0x1a, '\n' };

static void
minify(uint32_t *n)
{
	assert(*n != 0);

	if (*n > 1)
		*n >>= 1;
}

/**
 * \brief KTX data.
 */
struct piglit_ktx {
	struct piglit_ktx_info info;

	/** \brief The raw KTX data. */
	void *data;

	/**
	 * \brief Array of images.
	 *
	 * Array length is piglit_ktx_info::num_images.
	 */
	struct piglit_ktx_image *images;
};

static void
piglit_ktx_error(const char *format, ...)
{
	va_list va;
	va_start(va, format);

	printf("error: piglit_ktx: ");
	vprintf(format, va);
	printf("\n");
	fflush(stdout);

	va_end(va);
}

void
piglit_ktx_destroy(struct piglit_ktx *self)
{
	if (self == NULL)
		return;

	if (self->images != NULL)
		free(self->images);

	if (self->data)
		free(self->data);

	free(self);
}

/**
 * \brief Calculate and set self->info->target.
 */
static bool
piglit_ktx_calc_target(struct piglit_ktx *self)
{
	struct piglit_ktx_info *info = &self->info;

	if (info->pixel_width == 0) {
		goto bad_target;
	} else if (info->pixel_height == 0) {
		if (info->pixel_depth != 0)
			goto bad_target;
		if (info->num_faces != 1)
			goto bad_target;

		if (info->array_length == 0) {
			info->target = GL_TEXTURE_1D;
		} else {
			info->target = GL_TEXTURE_1D_ARRAY;
		}
	} else if (info->pixel_depth == 0) {
		if (info->array_length == 0) {
			if (info->num_faces == 1)
				info->target = GL_TEXTURE_2D;
			else if (info->num_faces == 6)
				info->target = GL_TEXTURE_CUBE_MAP;
			else
				goto bad_target;
		} else {
			if (info->num_faces == 1)
				info->target = GL_TEXTURE_2D_ARRAY;
			else if (info->num_faces == 6)
				info->target = GL_TEXTURE_CUBE_MAP_ARRAY;
			else
				goto bad_target;
		}
	} else {
		if (info->array_length != 0)
			goto bad_target;
		if (info->num_faces != 0)
			goto bad_target;

		info->target = GL_TEXTURE_3D;
	}

	return true;

bad_target:
	piglit_ktx_error("%s", "invalid texture target: pixel_size, "
			 "array_size, and num_faces are incompatible");
	return false;
}

static bool
piglit_ktx_parse_header(struct piglit_ktx *self)
{
	struct piglit_ktx_info *info = &self->info;
	bool ok = true;

	/*
	 * Used to iterate through KTX header with a step of 32 bits. The KTX
	 * spec declares all header fields as u32 values.
	 */
	const uint32_t *u32 = self->data;

	if (info->size < piglit_ktx_header_length) {
		piglit_ktx_error("data size must be at least length of KTX "
				 "header, %d bytes", piglit_ktx_header_length);
		return false;
	}

	if (memcmp(u32, piglit_ktx_magic_number,
		   sizeof(piglit_ktx_magic_number)) != 0) {
		piglit_ktx_error("%s", "KTX header does not begin with KTX "
				 "magic number");
		return false;
	}

	switch (u32[3]) {
	case 0x04030201:
		/* Little endian is supported. */
		break;
	case 0x01020304:
		piglit_ktx_error("%s", "KTX header declares big endian data, "
				 "but Piglit supports only little endian");
		return false;
	default:
		piglit_ktx_error("KTX header has bad value (0x%x) for "
				 "endianness flag", u32[3]);
		return false;
	}

	info->gl_type = u32[4];
	info->gl_type_size = u32[5];
	info->gl_format = u32[6];
	info->gl_internal_format = u32[7];
	info->gl_base_internal_format = u32[8];
	info->pixel_width = u32[9];
	info->pixel_height = u32[10];
	info->pixel_depth = u32[11];
	info->array_length = u32[12];
	info->num_faces = u32[13];
	info->num_miplevels = u32[14];

	if (info->num_miplevels == 0) {
		piglit_ktx_error("%s", "KTX header requests automatic "
		                 "mipmap generation, which Piglit does not "
		                 "support");
		return false;
	}

	if (u32[15] != 0) {
		piglit_ktx_error("%s", "KTX header declares presence of "
				 "arbitrary key/value data, which Piglit "
				 "does not support");
		return false;
	}

	ok = piglit_ktx_calc_target(self);
	if (!ok)
		return false;

	if (info->target == GL_TEXTURE_CUBE_MAP)
		info->num_images = 6 * info->num_miplevels;
	else
		info->num_images = info->num_miplevels;

	return true;
}

static void
piglit_ktx_calc_base_image_size(struct piglit_ktx *self,
				uint32_t *width,
				uint32_t *height,
				uint32_t *depth)
{
	struct piglit_ktx_info *info = &self->info;

	switch (info->target) {
		case GL_TEXTURE_1D:
			*width = info->pixel_width;
			*height = 0;
			*depth = 0;
			break;
		case GL_TEXTURE_1D_ARRAY:
			*width = info->pixel_width;
			*height = info->array_length;
			*depth = 0;
			break;
		case GL_TEXTURE_2D:
			*width = info->pixel_width;
			*height = info->pixel_height;
			*depth = 0;
			break;
		case GL_TEXTURE_2D_ARRAY:
			*width = info->pixel_width;
			*height = info->pixel_height;
			*depth = info->array_length;
			break;
		case GL_TEXTURE_CUBE_MAP:
			*width = info->pixel_width;
			*height = info->pixel_height;
			*depth = 0;
			break;
		case GL_TEXTURE_CUBE_MAP_ARRAY:
			*width = info->pixel_width;
			*height = info->pixel_height;
			*depth = 6 * info->array_length;
			break;
		case GL_TEXTURE_3D:
			*width = info->pixel_width;
			*height = info->pixel_height;
			*depth = info->pixel_depth;
			break;
		default:
			assert(0);
			break;
	}
}

static bool
piglit_ktx_parse_images(struct piglit_ktx *self)
{
	struct piglit_ktx_info *info = &self->info;

	/* Current image being parsed. */
	struct piglit_ktx_image *image;

	/*
	 * Current byte being parsed. Used to traverse data with a step of
	 * either 8 or 32 bits.
	 */
	union piglit_ktx_position {
		uint8_t *u8;
		uint32_t *u32;
	} p;

	/* Size of image, as passed to glTexImage(). */
	uint32_t pixel_width;
	uint32_t pixel_height;
	uint32_t pixel_depth;

	/* Loop counters */
	int miplevel;
	int face;

	piglit_ktx_calc_base_image_size(self,
					&pixel_width,
					&pixel_height,
					&pixel_depth);

	self->images = calloc(info->num_images, sizeof(*self->images));

	/* Skip header. */
	p.u8 = self->data;
	p.u8 += piglit_ktx_header_length;

	/* Begin parsing first image. */
	image = &self->images[0];

#define CUR_SIZE (p.u8 - (uint8_t *) self->data)

	for (miplevel = 0; miplevel < info->num_miplevels; ++miplevel) {
		uint32_t image_size;

		if (info->size < CUR_SIZE + 1) {
			/*
			 * Reading the image size below would access
			 * out-of-bounds memory.
			 */
			piglit_ktx_error("size of data stream must be at "
					 "least %u", CUR_SIZE + 1);
			return false;
		}

		image_size = *p.u32;
		++p.u32;

		for (face = 0; face < 6; ++face) {
			assert(image - self->images < info->num_images);

			image->data = p.u8;
			image->size = image_size;
			image->miplevel = miplevel;
			image->face = face;
			image->pixel_width = pixel_width;
			image->pixel_height = pixel_height;
			image->pixel_depth = pixel_depth;

			p.u8 += image_size;
			++image;

			/* Padding */
			while (CUR_SIZE % 4 != 0)
				++p.u8;

			if (info->target != GL_TEXTURE_CUBE_MAP)
				break;
		}

		switch (info->target) {
			case GL_TEXTURE_3D:
				minify(&pixel_width);
				minify(&pixel_height);
				minify(&pixel_depth);
				break;
			case GL_TEXTURE_2D:
			case GL_TEXTURE_2D_ARRAY:
			case GL_TEXTURE_CUBE_MAP:
			case GL_TEXTURE_CUBE_MAP_ARRAY:
				minify(&pixel_width);
				minify(&pixel_height);
				break;
			case GL_TEXTURE_1D:
			case GL_TEXTURE_1D_ARRAY:
				minify(&pixel_width);
				break;
			default:
				assert(0);
				break;
		}
	}

	if (info->size < CUR_SIZE) {
		/*
		 * The last image's data lies, at least partially, in
		 * out-of-bounds memory.
		 */
		piglit_ktx_error("size of data stream must be at least %zd",
				 CUR_SIZE);
		return false;
	}


	/*
	 * Up until now, info->size was an upper bound on the data size. Now
	 * the actual data size is known.
	 */
	info->size = CUR_SIZE;

	return true;

#undef CUR_SIZE
}

static bool
piglit_ktx_parse_data(struct piglit_ktx *self)
{
	bool ok = true;

	ok = ok && piglit_ktx_parse_header(self);
	ok = ok && piglit_ktx_parse_images(self);

	return ok;
}

struct piglit_ktx*
piglit_ktx_read_file(const char *filename)
{
	struct piglit_ktx *self;

	FILE *file = NULL;
	size_t size_read = 0;

	bool ok = true;
	int error = 0;

	self = calloc(1, sizeof(*self));
	if (self == NULL)
		goto out_of_memory;

	file = fopen(filename, "rb");
	if (file == NULL)
		goto bad_open;

	error = fseek(file, 0, SEEK_END);
	if (error)
		goto bad_read;
	self->info.size = ftell(file);
	error = fseek(file, 0, SEEK_SET);
	if (error)
		goto bad_read;

	self->data = malloc(self->info.size);
	if (self->data == NULL)
		goto out_of_memory;

	/*
	 * On Unix, one should mmap() here rather than copy. But this code
	 * needs to support Windows too.
	 */
	size_read = fread(self->data, 1, self->info.size, file);
	if (size_read < self->info.size)
		goto bad_read;

	ok = piglit_ktx_parse_data(self);
	goto end;

out_of_memory:
	ok = false;
	piglit_ktx_error("%s", "out of memory");
	goto end;

bad_open:
	ok = false;
	piglit_ktx_error("failed to open file: %s", filename);
	goto end;

bad_read:
	ok = false;
	piglit_ktx_error("errors in reading file: %s", filename);
	goto end;

end:
	if (file != NULL)
		fclose(file);

	if (!ok) {
		piglit_ktx_destroy(self);
		self = NULL;
	}

	return self;
}

struct piglit_ktx*
ktx_file_read_bytes(const void *bytes , size_t size)
{
	struct piglit_ktx *self;
	bool ok = true;

	self = calloc(1, sizeof(*self));
	if (self == NULL) {
		piglit_ktx_error("%s", "out of memory");
		return NULL;
	}

	self->info.size = size;
	memcpy(self->data, bytes, size);

	ok = piglit_ktx_parse_data(self);
	if (!ok) {
		piglit_ktx_destroy(self);
		return NULL;
	}

	return self;
}

bool
piglit_ktx_write_file(struct piglit_ktx *self, const char *filename)
{
	FILE *file = NULL;
	size_t size_written = 0;
	bool ok = true;

	file = fopen(filename, "wb");
	if (file == NULL)
		goto bad_open;

	size_written = fwrite(self->data, self->info.size, 1, file);
	if (size_written < self->info.size)
		goto bad_write;

	goto end;

bad_open:
	ok = false;
	piglit_ktx_error("failed to open file: %s", filename);
	goto end;

bad_write:
	ok = false;
	piglit_ktx_error("errors in writing file: %s", filename);
	goto end;

end:
	if (file != NULL)
		fclose(file);

	return ok;
}

bool
piglit_ktx_write_bytes(struct piglit_ktx *self, void *bytes)
{
	memcpy(bytes, self->data, self->info.size);
	return true;
}

const struct piglit_ktx_image*
piglit_ktx_get_image(struct piglit_ktx *self,
		     int miplevel,
		     int cube_face)
{
	const struct piglit_ktx_info *info = &self->info;

	if (miplevel < 0 || miplevel >= info->num_miplevels) {
		piglit_ktx_error("bad miplevel %d", miplevel);
		return NULL;
	}

	if (cube_face < 0 || cube_face >= 6) {
		piglit_ktx_error("bad cube_face %d", cube_face);
		return NULL;
	}

	if (cube_face != 0 && info->target != GL_TEXTURE_CUBE_MAP) {
		piglit_ktx_error("cube face %d was requested. cube face may "
				 "be requested only for non-array cubemaps",
				 cube_face);
		return NULL;
	}

	if (info->target == GL_TEXTURE_CUBE_MAP)
		return &self->images[6 * miplevel + cube_face];
	else
		return &self->images[miplevel];
}

static bool
piglit_ktx_load_cubeface(struct piglit_ktx *self,
                         int image,
                         GLenum *gl_error)
{
	const struct piglit_ktx_info *info = &self->info;
	const struct piglit_ktx_image *img = &self->images[image];

	GLenum face = GL_TEXTURE_CUBE_MAP_POSITIVE_X + (image % 6);
	int level = image / 6;

	if (info->gl_type == 0)
		glCompressedTexImage2D(face,
				       level,
				       info->gl_internal_format,
				       img->pixel_width,
				       img->pixel_height,
				       0 /*border*/,
				       img->size,
				       img->data);
	else
		glTexImage2D(face,
			     level,
			     info->gl_internal_format,
			     img->pixel_width,
			     img->pixel_height,
			     0 /*border*/,
			     info->gl_format,
			     info->gl_type,
			     img->data);

	*gl_error = glGetError();
	return *gl_error == 0;
}

static bool
piglit_ktx_load_noncubeface(struct piglit_ktx *self,
                            int image,
                            GLenum *gl_error)
{
	const struct piglit_ktx_info *info = &self->info;
	const struct piglit_ktx_image *img = &self->images[image];
	int level = image;

	switch (info->target) {
	case GL_TEXTURE_1D:
		if (info->gl_type == 0)
			glCompressedTexImage1D(info->target,
					       level,
					       info->gl_internal_format,
					       img->pixel_width,
					       0 /*border*/,
					       img->size,
					       img->data);
		else
			glTexImage1D(info->target,
				     level,
				     info->gl_internal_format,
				     img->pixel_width,
				     0 /*border*/,
				     info->gl_format,
				     info->gl_type,
				     img->data);
		break;
	case GL_TEXTURE_1D_ARRAY:
	case GL_TEXTURE_2D:
	case GL_TEXTURE_CUBE_MAP:
		if (info->gl_type == 0)
			glCompressedTexImage2D(info->target,
					       level,
					       info->gl_internal_format,
					       img->pixel_width,
					       img->pixel_height,
					       0 /*border*/,
					       img->size,
					       img->data);
		else
			glTexImage2D(info->target,
				     level,
				     info->gl_internal_format,
				     img->pixel_width,
				     img->pixel_height,
				     0 /*border*/,
				     info->gl_format,
				     info->gl_type,
				     img->data);
		break;
	case GL_TEXTURE_2D_ARRAY:
	case GL_TEXTURE_3D:
	case GL_TEXTURE_CUBE_MAP_ARRAY:
		if (info->gl_type == 0)
			glCompressedTexImage3D(info->target,
					       level,
					       info->gl_internal_format,
					       img->pixel_width,
					       img->pixel_height,
					       img->pixel_depth,
					       0 /*border*/,
					       img->size,
					       img->data);
		else
			glTexImage3D(info->target,
				     level,
				     info->gl_internal_format,
				     img->pixel_width,
				     img->pixel_height,
				     img->pixel_depth,
				     0 /*border*/,
				     info->gl_format,
				     info->gl_type,
				     img->data);
		break;
	default:
		*gl_error = 0;
		piglit_ktx_error("bad texture target 0x%x",
		                 info->target);
		return false;
	}

	*gl_error = glGetError();
	return *gl_error == 0;

unsupported_on_gles:
	*gl_error = 0;
	piglit_ktx_error("%s", "GLES supports only GL_TEXTURE_2D and "
			 "GL_TEXTURE_CUBE_MAP");
	return false;
}

static bool
piglit_ktx_load_image(struct piglit_ktx *self,
                      int image,
                      GLenum *gl_error)
{
	if (self->info.target == GL_TEXTURE_CUBE_MAP)
		return piglit_ktx_load_cubeface(self, image, gl_error);
	else
		return piglit_ktx_load_noncubeface(self, image, gl_error);
}

static GLuint
target_to_texture_binding(GLuint target)
{
	switch (target) {
		case GL_TEXTURE_1D:
			return GL_TEXTURE_BINDING_1D;
		case GL_TEXTURE_1D_ARRAY:
			return GL_TEXTURE_BINDING_1D_ARRAY;
		case GL_TEXTURE_2D:
			return GL_TEXTURE_BINDING_2D;
		case GL_TEXTURE_2D_ARRAY:
			return GL_TEXTURE_BINDING_2D_ARRAY;
		case GL_TEXTURE_CUBE_MAP:
			return GL_TEXTURE_BINDING_2D;
		case GL_TEXTURE_CUBE_MAP_ARRAY:
			return GL_TEXTURE_BINDING_CUBE_MAP_ARRAY;
			break;
		case GL_TEXTURE_3D:
			return GL_TEXTURE_BINDING_3D;
		default:
			assert(0);
			break;
	}

	return 0;
}

bool
piglit_ktx_load_texture(struct piglit_ktx *self,
			GLuint *tex_name,
			GLenum *gl_error)
{
	const struct piglit_ktx_info *info = &self->info;

	/*
	 * Local store for glGetError(). A local store is needed because
	 * gl_error may be null.
	 */
	GLenum my_gl_error = GL_NO_ERROR;

	/*
	 * The texture object bound to the texture target before this function call.
	 * Before returning, we rebind the texture.
	 */
	GLint old_bound_tex;

	/*
	 * The GL_UNPACK_ALIGNMENT before this function call.
	 * For KTX, the alignment must be set to 4. Before returning, we restore the unpack alignment
	 *
	 */
	GLint old_unpack_alignment;

	bool made_texture = false;

	bool ok = true;
	int i;

	assert(tex_name != NULL);

	glGetIntegerv(target_to_texture_binding(info->target),
	              &old_bound_tex);
	glGetIntegerv(GL_UNPACK_ALIGNMENT, &old_unpack_alignment);

	/* Reset GL error state. */
	while (glGetError())
		;

	if (*tex_name == 0) {
		glGenTextures(1, tex_name);
		made_texture = true;

		my_gl_error = glGetError();
		if (my_gl_error)
			goto fail;
	}

	glBindTexture(info->target, *tex_name);
	my_gl_error = glGetError();
	if (my_gl_error)
		goto fail;

	for (i = 0; i < info->num_images; ++i) {
		ok = piglit_ktx_load_image(self, i, &my_gl_error);
		if (!ok)
			goto fail;
	}

	goto cleanup;

fail:
	ok = false;

	if (gl_error != NULL)
		*gl_error = my_gl_error;

	if (made_texture && *tex_name != 0) {
		glDeleteTextures(1, tex_name);
		*tex_name = 0;
	}

cleanup:
	/* Reset GL error state. */
	while (glGetError())
		;;

	glBindTexture(info->target, old_bound_tex);
	glPixelStorei(GL_UNPACK_ALIGNMENT, old_unpack_alignment);
	return ok;
}

const struct piglit_ktx_info*
piglit_ktx_get_info(struct piglit_ktx *self)
{
	return &self->info;
}