aboutsummaryrefslogtreecommitdiff
path: root/tests/spec/arb_texture_compression/internal-format-query.c
blob: 0eb77ffb70eaa7b38ea22e5ffdcfb9a5e2ba87a3 (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
/*
 * 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
 * Verify behavior of TEXTURE_INTERNAL_FORMAT for generic compression formats
 *
 * Typically the \c GL_TEXTURE_INTERNAL_FORMAT query returns the internal
 * format specified by the application at \c glTexImage2D time.  This behavior
 * is modified for the generic compressed texture internal formats.
 * Specifically, the issues section of the GL_ARB_texture_compression spec
 * says:
 *
 *    "(10) Should functionality be provided to allow applications to save
 *    compressed images to disk and reuse them in subsequent runs without
 *    programming to specific formats?  If so, how?
 *
 *      RESOLVED:  Yes.  This can be done without knowledge of specific
 *      compression formats in the following manner:
 *
 *        * Call TexImage with an uncompressed image and a generic compressed
 *          internal format.  The texture image will be compressed by the GL, if
 *          possible.
 *
 *        * Call GetTexLevelParameteriv with a <value> of TEXTURE_COMPRESSED_ARB
 *          to determine if the GL was able to store the image in compressed
 *          form.
 *
 *        * Call GetTexLevelParameteriv with a <value> of
 *          TEXTURE_INTERNAL_FORMAT to determine the specific compressed image
 *          format in which the image is stored.
 *
 *        ..."
 *
 * The body of the spec (section 3.8.1, Texture Image Specification) also say:
 *
 *     "Generic compressed internal formats are never used directly as the
 *     internal formats of texture images.  If <internalformat> is one of the
 *     six generic compressed internal formats, its value is replaced by the
 *     symbolic constant for a specific compressed internal format of the GL's
 *     choosing with the same base internal format.  If no specific compressed
 *     format is available, <internalformat> is instead replaced by the
 *     corresponding base internal format.  If <internalformat> is given as or
 *     mapped to a specific compressed internal format, but the GL can not
 *     support images compressed in the chosen internal format for any reason
 *     (e.g., the compression format might not support 3D textures or borders),
 *     <internalformat> is replaced by the corresponding base internal format
 *     and the texture image will not be compressed by the GL."
 */
#include <stdarg.h>
#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 test_vector {
	const char *generic_compressed_format_string;
	GLenum generic_compressed_format;
	GLenum base_format;
};

/**
 * Generic texture formats in OpenGL 1.3 and GL_ARB_texture_compression.
 */
static const struct test_vector arb_texture_compression_formats[] = {
        { ENUM_AND_STRING(GL_COMPRESSED_ALPHA), GL_ALPHA },
        { ENUM_AND_STRING(GL_COMPRESSED_LUMINANCE), GL_LUMINANCE },
        { ENUM_AND_STRING(GL_COMPRESSED_LUMINANCE_ALPHA), GL_LUMINANCE_ALPHA },
        { ENUM_AND_STRING(GL_COMPRESSED_INTENSITY), GL_INTENSITY },
        { ENUM_AND_STRING(GL_COMPRESSED_RGB), GL_RGB },
        { ENUM_AND_STRING(GL_COMPRESSED_RGBA), GL_RGBA },
};

/**
 * Generic texture formats in OpenGL 3.0 and GL_ARB_texture_rg.
 */
static const struct test_vector arb_texture_rg_formats[] = {
        { ENUM_AND_STRING(GL_COMPRESSED_RED), GL_RED },
        { ENUM_AND_STRING(GL_COMPRESSED_RG), GL_RG },
};

/**
 * Generic texture formats in OpenGL 2.1 and GL_EXT_texture_sRGB.
 */
static const struct test_vector ext_texture_srgb_formats[] = {
	{ ENUM_AND_STRING(GL_COMPRESSED_SRGB_EXT), GL_RGB },
	{ ENUM_AND_STRING(GL_COMPRESSED_SRGB_ALPHA_EXT), GL_RGBA },
	{ ENUM_AND_STRING(GL_COMPRESSED_SLUMINANCE_EXT), GL_LUMINANCE },
	{ ENUM_AND_STRING(GL_COMPRESSED_SLUMINANCE_ALPHA_EXT), GL_LUMINANCE_ALPHA },
};

static GLubyte dummy_data[16 * 16 * 4];

enum piglit_result
piglit_display(void)
{
	return PIGLIT_FAIL;
}

GLenum *
add_formats(GLenum *formats, GLint *total, unsigned add, ...)
{
	va_list ap;
	unsigned i;

	formats = realloc(formats, sizeof(GLenum) * (*total + add));

	va_start(ap, add);

	for (i = 0; i < add; i++) {
		formats[*total] = va_arg(ap, GLenum);
		(*total)++;
	}

	va_end(ap);
	return formats;
}

static bool
try_formats(const struct test_vector *t, unsigned num_tests,
	    const GLenum *compressed_formats, GLint num_compressed_formats)
{
	bool pass = true;
	unsigned i;

	for (i = 0; i < num_tests; i++) {
		GLuint tex;
		GLint is_compressed;
		GLenum format;

		if (!piglit_automatic) {
			printf("Trying %s/0x%04x (base format = 0x%04x)...\n",
			       t[i].generic_compressed_format_string,
			       t[i].generic_compressed_format,
			       t[i].base_format);
		}

		glGenTextures(1, &tex);
		glBindTexture(GL_TEXTURE_2D, tex);
		glTexImage2D(GL_TEXTURE_2D, 0,
			     t[i].generic_compressed_format,
			     16, 16, 0,
			     /* Remember that GL_INTESITY is not a valid format
			      * for image data.
			      */
			     (t[i].base_format == GL_INTENSITY)
			     ? GL_RGBA : t[i].base_format,
			     GL_UNSIGNED_BYTE,
			     dummy_data);

		glGetTexLevelParameteriv(GL_TEXTURE_2D,
					 0,
					 GL_TEXTURE_COMPRESSED,
					 &is_compressed);

		glGetTexLevelParameteriv(GL_TEXTURE_2D,
					 0,
					 GL_TEXTURE_INTERNAL_FORMAT,
					 (GLint *) &format);

		if (!piglit_automatic) {
			printf("  is %scompressed, internal format = 0x%04x\n",
			       is_compressed ? "" : "not ",
			       format);
		}

		if (is_compressed) {
			unsigned j;

			for (j = 0; j < num_compressed_formats; j++) {
				if (format == compressed_formats[j])
					break;
			}

			if (format == t[i].generic_compressed_format) {
				fprintf(stderr,
					"%s did compress, but it got the "
					"generic\n"
					"format as the specific internal "
					"format.\n",
					t[i].generic_compressed_format_string);
				pass = false;
			} else if (format <= 4 || format == t[i].base_format) {
				fprintf(stderr,
					"%s did compress, but it got an "
					"internal\n"
					"format 0x%04x that is "
					"non-compressed\n",
					t[i].generic_compressed_format_string,
					format);
				pass = false;
			} else if (j == num_compressed_formats) {
				fprintf(stderr,
					"%s did compress, but it got an "
					"internal\n"
					"format of 0x%04x when "
					"one of the supported compressed "
					"formats was expected.\n"
					"This may just mean the test does not "
					"know about the compessed format that\n"
					"was selected by the driver.\n",
					t[i].generic_compressed_format_string,
					format);
			}
		} else if (format != t[i].base_format) {
			if (format == t[i].generic_compressed_format) {
				fprintf(stderr,
					"%s did not compress, but it got the "
					"generic\n"
					"format as the specific internal "
					"format.\n",
					t[i].generic_compressed_format_string);
			} else {
				fprintf(stderr,
					"%s did not compress, but it got an "
					"internal\n"
					"format of 0x%04x when "
					"0x%04x was expected.\n",
					t[i].generic_compressed_format_string,
					format,
					t[i].base_format);
			}

			pass = false;
		}

		glBindTexture(GL_TEXTURE_2D, 0);
		glDeleteTextures(1, &tex);

		if (!piglit_automatic) {
			printf("\n");
		}
	}

	return pass;
}

void
piglit_init(int argc, char **argv)
{
	GLint num_compressed_formats;
	GLenum *compressed_formats = NULL;
	unsigned i;
	bool pass = 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 if (num_compressed_formats < 0) {
		fprintf(stderr,
			"Invalid number of compressed formats (%d) reported\n",
			num_compressed_formats);
		piglit_report_result(PIGLIT_FAIL);
	} 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);
	}

	/* There are some specific formats that are valid for certain generic
	 * formats that are not returned by the GL_COMRPESSED_TEXTURE_FORMATS
	 * query.  That query only returns formats that have no restrictions or
	 * caveats for RGB or RGBA base formats.  We have to add these formats
	 * to the list of possible formats by hand.
	 */
	if (piglit_is_extension_supported("GL_EXT_texture_compression_latc")) {
		compressed_formats =
			add_formats(compressed_formats,
				    &num_compressed_formats,
				    4,
				    GL_COMPRESSED_LUMINANCE_LATC1_EXT,
				    GL_COMPRESSED_SIGNED_LUMINANCE_LATC1_EXT,
				    GL_COMPRESSED_LUMINANCE_ALPHA_LATC2_EXT,
				    GL_COMPRESSED_SIGNED_LUMINANCE_ALPHA_LATC2_EXT);
	}

	if (piglit_is_extension_supported("GL_ATI_texture_compression_3dc")) {
		compressed_formats =
			add_formats(compressed_formats,
				    &num_compressed_formats,
				    1,
				    0x8837);
	}

	pass = try_formats(arb_texture_compression_formats,
			   ARRAY_SIZE(arb_texture_compression_formats),
			   compressed_formats,
			   num_compressed_formats);

	/* Remove the various luminance and luminance-alpha formats from the
	 * list since they cannot be used for the later tests.
	 */
	if (piglit_is_extension_supported("GL_ATI_texture_compression_3dc")) {
		num_compressed_formats--;
	}

	if (piglit_is_extension_supported("GL_EXT_texture_compression_latc")) {
		num_compressed_formats -= 4;
	}

	/* Add the RGTC formats, then check them.
	 */
	if (piglit_is_extension_supported("GL_ARB_texture_rg")) {
		if (piglit_is_extension_supported("GL_ARB_texture_compression_rgtc")
		    || piglit_is_extension_supported("GL_EXT_texture_compression_rgtc")) {
			compressed_formats =
				add_formats(compressed_formats,
					    &num_compressed_formats,
					    4,
					    GL_COMPRESSED_RED_RGTC1,
					    GL_COMPRESSED_SIGNED_RED_RGTC1,
					    GL_COMPRESSED_RG_RGTC2,
					    GL_COMPRESSED_SIGNED_RG_RGTC2);
		}

		pass = try_formats(arb_texture_rg_formats,
				   ARRAY_SIZE(arb_texture_rg_formats),
				   compressed_formats,
				   num_compressed_formats)
			&& pass;

		/* Remove the RGTC formats from the list since they cannot be
		 * used for the later tests.
		 */
		if (piglit_is_extension_supported("GL_ARB_texture_compression_rgtc")
		    || piglit_is_extension_supported("GL_EXT_texture_compression_rgtc")) {
			num_compressed_formats -= 4;
		}
	}


	/* Add the sRGB formats, then check them.
	 */
	if (piglit_is_extension_supported("GL_EXT_texture_sRGB")) {
		compressed_formats =
			add_formats(compressed_formats,
				    &num_compressed_formats,
				    4,
				    GL_COMPRESSED_SRGB,
				    GL_COMPRESSED_SRGB_ALPHA,
				    GL_COMPRESSED_SLUMINANCE,
				    GL_COMPRESSED_SLUMINANCE_ALPHA);

		if (piglit_is_extension_supported("GL_EXT_texture_compression_s3tc")) {
			compressed_formats =
				add_formats(compressed_formats,
					    &num_compressed_formats,
					    4,
					    GL_COMPRESSED_SRGB_S3TC_DXT1_EXT,
					    GL_COMPRESSED_SRGB_ALPHA_S3TC_DXT1_EXT,
					    GL_COMPRESSED_SRGB_ALPHA_S3TC_DXT3_EXT,
					    GL_COMPRESSED_SRGB_ALPHA_S3TC_DXT5_EXT);
		}

		pass = try_formats(ext_texture_srgb_formats,
				   ARRAY_SIZE(ext_texture_srgb_formats),
				   compressed_formats,
				   num_compressed_formats)
			&& pass;

		/* Remove the sRGB formats from the list since they cannot be
		 * used for the later tests.
		 */
		num_compressed_formats -= 4;
		if (piglit_is_extension_supported("GL_EXT_texture_compression_s3tc")) {
			num_compressed_formats -= 4;
		}
	}

	piglit_report_result(pass ? PIGLIT_PASS : PIGLIT_FAIL);
}