aboutsummaryrefslogtreecommitdiff
path: root/tests/spec/gl-3.0/required-sized-texture-formats.c
blob: e0f64c4722eb7cf3797dedc1c44dc7273e32e5b6 (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
/* 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.
 */

#include "piglit-util-gl-common.h"
#include "sized-internalformats.h"

/**
 * @file required-sized-formats.c
 *
 * Tests that the required sized internal formats for GL 3.0 are
 * exposed.  See page 180 of the GL 3.0 pdf (20080923), "Required
 * Texture Formats".  Notably:
 *
 *     "In addition, implementations are required to support the
 *      following sized internal formats. Requesting one of these
 *      internal formats for any texture type will allocate exactly
 *      the internal component sizes and types shown for that format
 *      in tables 3.16- 3.17:"
 */

PIGLIT_GL_TEST_MAIN(
    32 /*window_width*/,
    32 /*window_height*/,
    PIGLIT_GL_VISUAL_DOUBLE | PIGLIT_GL_VISUAL_RGB | PIGLIT_GL_VISUAL_ALPHA)

GLenum type_queries[CHANNELS] = {
	GL_TEXTURE_RED_TYPE,
	GL_TEXTURE_GREEN_TYPE,
	GL_TEXTURE_BLUE_TYPE,
	GL_TEXTURE_ALPHA_TYPE,
	GL_TEXTURE_LUMINANCE_TYPE,
	GL_TEXTURE_INTENSITY_TYPE,
	GL_TEXTURE_DEPTH_TYPE,
	GL_NONE,
};

GLenum size_queries[CHANNELS] = {
	GL_TEXTURE_RED_SIZE,
	GL_TEXTURE_GREEN_SIZE,
	GL_TEXTURE_BLUE_SIZE,
	GL_TEXTURE_ALPHA_SIZE,
	GL_TEXTURE_LUMINANCE_SIZE,
	GL_TEXTURE_INTENSITY_SIZE,
	GL_TEXTURE_DEPTH_SIZE,
	GL_TEXTURE_STENCIL_SIZE,
};

enum piglit_result
piglit_display(void)
{
	/* UNREACHED */
	return PIGLIT_FAIL;
}

void
piglit_init(int argc, char **argv)
{
	bool pass = true;
	GLuint tex;
	int i, c;

	piglit_require_gl_version(30);

	glGenTextures(1, &tex);
	glBindTexture(GL_TEXTURE_2D, tex);

	for (i = 0; required_formats[i].token != GL_NONE; i++) {
		GLint sizes[CHANNELS];
		GLint types[CHANNELS];
		bool format_pass = true;
		GLenum format, type;
		const struct sized_internalformat *f;

		/* FINISHME: Add support for future GL versions. */
		if (required_formats[i].version != 30)
			continue;

		f = get_sized_internalformat(required_formats[i].token);

		if (f->token == GL_DEPTH24_STENCIL8 ||
		    f->token == GL_DEPTH32F_STENCIL8) {
			format = GL_DEPTH_STENCIL;
			type = GL_UNSIGNED_INT_24_8;
		} else if (get_channel_size(f, D)) {
			format = GL_DEPTH_COMPONENT;
			type = GL_FLOAT;
		} else {
			format = GL_RGBA;
			type = GL_FLOAT;

			/* Have to specify integer data for integer textures. */
			for (c = R; c <= I; c++) {
				if (get_channel_type(f, c) == GL_UNSIGNED_INT ||
				    get_channel_type(f, c) == GL_INT) {
					format = GL_RGBA_INTEGER;
					type = GL_UNSIGNED_INT;
					break;
				}
			}
		}

		glTexImage2D(GL_TEXTURE_2D, 0, f->token,
			     1, 1, 0,
			     format, type, NULL);

		if (glGetError() != 0) {
			printf("Unexpected error creating %s texture\n",
			       f->name);
			pass = false;
			continue;
		}

		for (c = 0; c < CHANNELS; c++) {
			glGetTexLevelParameteriv(GL_TEXTURE_2D, 0,
						 size_queries[c], &sizes[c]);
			if (c != S) {
				glGetTexLevelParameteriv(GL_TEXTURE_2D, 0,
							 type_queries[c],
							 &types[c]);
			} else {
				/* For stencil, there's no query for
				 * the type, so our table above
				 * records a type/size of unorm 8, and
				 * we'll just set the query result
				 * here to unorm so that we only look
				 * at the size.
				 */
				if (sizes[c] != 0)
					types[c] = GL_UNSIGNED_NORMALIZED;
				else
					types[c] = GL_NONE;
			}

			/* We use ~0 as the signal for the compressed
			 * texture formats.  While the colors being
			 * interpolated across the 4x4 blocks have 8
			 * bits in them, the spec suggests reporting
			 * some approximate value less than that.
			 * From page 319 of the GL 3.0 spec:
			 *
			 *     "Queries of value of TEXTURE RED SIZE,
			 *      TEXTURE GREEN SIZE, [...] return the
			 *      actual resolutions of the stored image
			 *      array components, not the resolutions
			 *      specified when the image array was
			 *      defined. For texture images with a
			 *      compressed internal format, the
			 *      resolutions returned specify the
			 *      component resolution of an
			 *      uncompressed internal format that
			 *      produces an image of roughly the same
			 *      quality as the compressed image in
			 *      question. Since the quality of the
			 *      implementation’s compression algorithm
			 *      is likely data-dependent, the returned
			 *      component sizes should be treated only
			 *      as rough approximations.
			 */
			if (f->bits[c] == SCMP ||
			    f->bits[c] == UCMP) {
				if (sizes[c] <= 0 || sizes[c] > 8)
					format_pass = false;
			} else {
				if (sizes[c] != get_channel_size(f, c)) {
					format_pass = false;
				}
			}

			if (types[c] != get_channel_type(f, c))
				format_pass = false;
		}

		if (!format_pass) {
			printf("format %s:\n",
			       f->name);

			printf("  expected: ");
			for (c = 0; c < CHANNELS; c++) {
				print_bits(get_channel_size(f, c),
					   get_channel_type(f, c));
				printf(" ");
			}
			printf("\n");

			printf("  observed: ");
			for (c = 0; c < CHANNELS; c++) {
				print_bits(sizes[c], types[c]);
				printf(" ");
			}
			printf("\n");

			pass = false;
		}
	}

	glDeleteTextures(1, &tex);

	piglit_report_result(pass ? PIGLIT_PASS : PIGLIT_FAIL);
}