summaryrefslogtreecommitdiff
path: root/tests/spec/arb_texture_compression/invalid-formats.c
blob: f4b147fd45330bd4e05d5ef954db0aefd4b25884 (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
/*
 * Copyright © 2011 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.
 */

/**
 * \file internal-format-query.c
 * Check that only general-purpose fmts are listed by COMPRESSED_TEXTURE_FORMATS
 *
 * From page 117 (page 129 of the PDF) of the OpenGL 1.3 spec says:
 *
 *     "The set of specific compressed internal formats supported by the
 *     renderer can be obtained by querying the value of COMPRESSED TEXTURE
 *     FORMATS. The only values returned by this query are those corresponding
 *     to formats suitable for general-purpose usage. The renderer will not
 *     enumerate formats with restrictions that need to be specifically
 *     understood prior to use."
 *
 * All texture compression extensions have taken this to mean only linear RGB
 * and linear RGBA formats should be exposed.
 */
#include "piglit-util-gl-common.h"

PIGLIT_GL_TEST_CONFIG_BEGIN

	config.window_width = 10;
	config.window_height = 10;
	config.window_visual = PIGLIT_GL_VISUAL_RGB;

PIGLIT_GL_TEST_CONFIG_END

#define ENUM_AND_STRING(e) \
	# e, e

struct format_tuple {
	const char *name;
	GLenum format;
};

/**
 * Set of formats for a particular extension
 *
 * Each array of "good" formats and "bad" formats is terminated by a sentinel
 * with \c format set to zero.
 *
 * \note
 * The arrays are sized for the largest set from any extension.  They may need
 * to be expanded in the future.
 */
struct format_list {
	/**
	 * Formats that are part of the extension and should be exposed.
	 */
	struct format_tuple good[11];

	/**
	 * Formats that are part of the extension but should not be exposed.
	 */
	struct format_tuple bad[5];
};

/**
 * Formats belonging to GL_ARB_texture_comrpession_bptc
 */
static const struct format_list bptc_formats = {
	{
		{ ENUM_AND_STRING(GL_COMPRESSED_RGBA_BPTC_UNORM_ARB) },
		{ ENUM_AND_STRING(GL_COMPRESSED_SRGB_ALPHA_BPTC_UNORM_ARB) },
		{ ENUM_AND_STRING(GL_COMPRESSED_RGB_BPTC_SIGNED_FLOAT_ARB) },
		{ ENUM_AND_STRING(GL_COMPRESSED_RGB_BPTC_UNSIGNED_FLOAT_ARB) },
		{ NULL, 0 },
	},
	{
		{ NULL, 0 },
	}
};

/**
 * Formats belonging to GL_ARB_texture_comrpession_rgtc
 */
static const struct format_list rgtc_formats = {
	{
		{ NULL, 0 },
	},
	{
		{ ENUM_AND_STRING(GL_COMPRESSED_RED_RGTC1) },
		{ ENUM_AND_STRING(GL_COMPRESSED_SIGNED_RED_RGTC1) },
		{ ENUM_AND_STRING(GL_COMPRESSED_RG_RGTC2) },
		{ ENUM_AND_STRING(GL_COMPRESSED_SIGNED_RG_RGTC2) },
		{ NULL, 0 },
	}
};

/**
 * Formats belonging to GL_3DFX_texture_comrpession_FXT1
 */
static const struct format_list fxt1_formats = {
	{
		{ ENUM_AND_STRING(GL_COMPRESSED_RGB_FXT1_3DFX) },
		{ ENUM_AND_STRING(GL_COMPRESSED_RGBA_FXT1_3DFX) },
		{ NULL, 0 },
	},
	{
		{ NULL, 0 },
	}
};

/**
 * Formats belonging to GL_ATI_texture_comrpession_3dc
 */
static const struct format_list ati_3dc_formats = {
	{
		{ NULL, 0 },
	},
	{
		{ "GL_COMPRESSED_LUMINANCE_ALPHA_3DC_ATI", 0x8837 },
		{ NULL, 0 },
	}
};

/**
 * Formats belonging to GL_EXT_texture_comrpession_latc
 */
static const struct format_list latc_formats = {
	{
		{ NULL, 0 },
	},
	{
		{ ENUM_AND_STRING(GL_COMPRESSED_LUMINANCE_LATC1_EXT) },
		{ ENUM_AND_STRING(GL_COMPRESSED_SIGNED_LUMINANCE_LATC1_EXT) },
		{ ENUM_AND_STRING(GL_COMPRESSED_LUMINANCE_ALPHA_LATC2_EXT) },
		{ ENUM_AND_STRING(GL_COMPRESSED_SIGNED_LUMINANCE_ALPHA_LATC2_EXT) },
		{ NULL, 0 },
	}
};

/**
 * Formats belonging to GL_EXT_texture_comrpession_s3tc
 */
static const struct format_list s3tc_formats = {
	{
		{ ENUM_AND_STRING(GL_COMPRESSED_RGB_S3TC_DXT1_EXT) },
		{ ENUM_AND_STRING(GL_COMPRESSED_RGBA_S3TC_DXT3_EXT) },
		{ ENUM_AND_STRING(GL_COMPRESSED_RGBA_S3TC_DXT5_EXT) },
		{ NULL, 0 },
	},
	{
		{ ENUM_AND_STRING(GL_COMPRESSED_RGBA_S3TC_DXT1_EXT) },
		{ NULL, 0 },
	}
};

/**
 * Formats belonging to GL_EXT_texture_sRGB
 *
 * These should only be exported if GL_EXT_texture_compression_s3tc is also
 * supported.
 */
static const struct format_list srgb_formats = {
	{
		{ NULL, 0 },
	},
	{
		{ ENUM_AND_STRING(GL_COMPRESSED_SRGB_S3TC_DXT1_EXT) },
		{ ENUM_AND_STRING(GL_COMPRESSED_SRGB_ALPHA_S3TC_DXT1_EXT) },
		{ ENUM_AND_STRING(GL_COMPRESSED_SRGB_ALPHA_S3TC_DXT3_EXT) },
		{ ENUM_AND_STRING(GL_COMPRESSED_SRGB_ALPHA_S3TC_DXT5_EXT) },
		{ NULL, 0 },
	}
};

/**
 * Formats belonging to GL_OES_comrpessed_paletted_texture
 */
static const struct format_list paletted_formats = {
	{
		{ ENUM_AND_STRING(GL_PALETTE4_RGB8_OES) },
		{ ENUM_AND_STRING(GL_PALETTE4_RGBA8_OES) },
		{ ENUM_AND_STRING(GL_PALETTE4_R5_G6_B5_OES) },
		{ ENUM_AND_STRING(GL_PALETTE4_RGBA4_OES) },
		{ ENUM_AND_STRING(GL_PALETTE4_RGB5_A1_OES) },
		{ ENUM_AND_STRING(GL_PALETTE8_RGB8_OES) },
		{ ENUM_AND_STRING(GL_PALETTE8_RGBA8_OES) },
		{ ENUM_AND_STRING(GL_PALETTE8_R5_G6_B5_OES) },
		{ ENUM_AND_STRING(GL_PALETTE8_RGBA4_OES) },
		{ ENUM_AND_STRING(GL_PALETTE8_RGB5_A1_OES) },
		{ NULL, 0 },
	},
	{
		{ NULL, 0 },
	}
};

/**
 * List of all known compression methods to test
 *
 * The dummy first element is because this list is used by \c main to replace
 * the \c argv vector when no command line parameters are supplied.
 */
const char *all_formats[] = {
	"argv[0]",
	"bptc",
	"s3tc",
	"fxt1",
	"latc",
	"3dc",
	"rgtc",
	"srgb",
	"paletted"
};

enum piglit_result
piglit_display(void)
{
	return PIGLIT_FAIL;
}

static bool
reject_bad(const struct format_tuple *bad, GLenum *compressed_formats,
	   GLint num_compressed_formats, bool check_errors)
{
	bool pass = true;
	unsigned i;
	unsigned j;

	for (i = 0; bad[i].format != 0; i++) {
		for (j = 0; j < num_compressed_formats; j++) {
			if (compressed_formats[j] == 0)
				continue;

			if (compressed_formats[j] == bad[i].format)
				break;
		}

		if (j != num_compressed_formats) {
			if (check_errors) {
				fprintf(stderr,
					"%s should not be available.\n",
					bad[i].name);
				pass = false;
			}

			/* Replace formats that have been processed with
			 * zero.  This allows detection of values that don't
			 * belong to any compression extension.
			 */
			compressed_formats[j] = 0;
		}
	}

	return pass;
}

static bool
try_formats(const struct format_list *t, GLenum *compressed_formats,
	    GLint num_compressed_formats, bool check_errors, bool supported)
{
	bool pass = true;

	if (!supported) {
		pass = reject_bad(t->good, compressed_formats,
				  num_compressed_formats, check_errors)
			&& pass;
	} else {
		unsigned i;
		unsigned j;

		for (i = 0; t->good[i].format != 0; i++) {
			for (j = 0; j < num_compressed_formats; j++) {
				if (compressed_formats[j] == 0)
					continue;

				if (compressed_formats[j] == t->good[i].format)
					break;
			}

			if (j == num_compressed_formats) {
				if (check_errors) {
					fprintf(stderr,
						"%s should be available.\n",
						t->good[i].name);
					pass = false;
				}
			} else {
				/* Replace formats that have been processed
				 * with zero.  This allows detection of values
				 * that don't belong to any compression
				 * extension.
				 */
				compressed_formats[j] = 0;
			}
		}
	}

	pass = reject_bad(t->bad, compressed_formats, num_compressed_formats,
			  check_errors)
		&& pass;

	return pass;
}

void
piglit_init(int argc, char **argv)
{
	GLint num_compressed_formats;
	GLenum *compressed_formats = NULL;
	unsigned i;
	bool pass = true;
	bool log_header = true;
	bool do_all = false;
	bool check_errors = true;

	piglit_require_extension("GL_ARB_texture_compression");

	glGetIntegerv(GL_NUM_COMPRESSED_TEXTURE_FORMATS,
		      &num_compressed_formats);
	if (num_compressed_formats == 0) {
		printf("No compressed formats supported.\n");
	} else {
		compressed_formats = calloc(num_compressed_formats,
					    sizeof(GLenum));
		glGetIntegerv(GL_COMPRESSED_TEXTURE_FORMATS,
			      (GLint *) compressed_formats);

		printf("Driver reported the following compressed formats:\n");
		for (i = 0; i < num_compressed_formats; i++) {
			printf("    0x%04x\n", compressed_formats[i]);
		}
		printf("\n");
		fflush(stdout);
	}

	/* First scan the list of formats looking for zeros.  We use that as
	 * magic flag later in the test.
	 */
	for (i = 0; i < num_compressed_formats; i++) {
		if (compressed_formats[i] == 0) {
			fprintf(stderr,
				"Invalid value 0x0000 in format list.\n");
			piglit_report_result(PIGLIT_FAIL);
		}
	}

	/* The "unknown" flag instructs the test to just check for values that
	 * don't belong to any compression extension supported by this
	 * implementation.
	 */
	if (argc > 1 && strcmp("unknown", argv[1]) == 0) {
		check_errors = false;
		argv++;
		argc--;
	}

	if (argc == 1) {
		argv = (char **) all_formats;  /* cast away const */
		argc = ARRAY_SIZE(all_formats);
		do_all = true;
	}

	for (i = 1; i < argc; i++) {
		if (strcmp(argv[i], "bptc") == 0) {
			pass = try_formats(&bptc_formats,
					   compressed_formats,
					   num_compressed_formats,
					   check_errors,
					   piglit_is_extension_supported("GL_ARB_texture_compression_bptc"))
				&& pass;
		} else if (strcmp(argv[i], "s3tc") == 0) {
			pass = try_formats(&s3tc_formats,
					   compressed_formats,
					   num_compressed_formats,
					   check_errors,
					   piglit_is_extension_supported("GL_EXT_texture_compression_s3tc"))
				&& pass;
		} else if (strcmp(argv[i], "fxt1") == 0) {
			pass = try_formats(&fxt1_formats,
					   compressed_formats,
					   num_compressed_formats,
					   check_errors,
					   piglit_is_extension_supported("GL_3DFX_texture_compression_FXT1"))
				&& pass;
		} else if (strcmp(argv[i], "latc") == 0) {
			pass = try_formats(&latc_formats,
					   compressed_formats,
					   num_compressed_formats,
					   check_errors,
					   piglit_is_extension_supported("GL_EXT_texture_compression_latc"))
				&& pass;
		} else if (strcmp(argv[i], "3dc") == 0) {
			pass = try_formats(&ati_3dc_formats,
					   compressed_formats,
					   num_compressed_formats,
					   check_errors,
					   piglit_is_extension_supported("GL_ATI_texture_compression_3dc"))
				&& pass;
		} else if (strcmp(argv[i], "rgtc") == 0) {
			pass = try_formats(&rgtc_formats,
					   compressed_formats,
					   num_compressed_formats,
					   check_errors,
					   (piglit_is_extension_supported("GL_ARB_texture_compression_rgtc")
					    || piglit_is_extension_supported("GL_EXT_texture_compression_rgtc")))
				&& pass;
		} else if (strcmp(argv[i], "srgb") == 0) {
			pass = try_formats(&srgb_formats,
					   compressed_formats,
					   num_compressed_formats,
					   check_errors,
					   (piglit_is_extension_supported("GL_EXT_texture_sRGB")
					    && piglit_is_extension_supported("GL_EXT_texture_compression_s3tc")))
				&& pass;
		} else if (strcmp(argv[i], "paletted") == 0) {
			pass = try_formats(&paletted_formats,
					   compressed_formats,
					   num_compressed_formats,
					   check_errors,
					   piglit_is_extension_supported("GL_OES_compressed_paletted_texture"))
				&& pass;
		} else {
			fprintf(stderr,
				"Unrecognized selection `%s'\n", argv[i]);
			piglit_report_result(PIGLIT_FAIL);
		}
	}

	/* After all of the known formats have been processed, the entire
	 * format array should be zeroed out.  Any non-zero values are either
	 * errors or formats from unknown extensions... meaning that the test
	 * may need to be updated.
	 */
	if (do_all) {
		for (i = 0; i < num_compressed_formats; i++) {
			if (compressed_formats[i] != 0) {
				if (log_header) {
					fprintf(stderr,
						"Unrecognized compressed "
						"texture formats:\n");
					log_header = false;
				}

				fprintf(stderr, "    0x%04x\n",
					compressed_formats[i]);
				pass = false;
			}
		}
	}

	piglit_report_result(pass ? PIGLIT_PASS : PIGLIT_FAIL);
}