add pbobench: a benchmark for pbo functions
[piglit.git] / tests / texturing / compressedteximage.c
blob45ae695b25cac9c58088d59bc89351013dcf21cc
1 /*
2 * Copyright © 2011 Intel Corporation
4 * Permission is hereby granted, free of charge, to any person obtaining a
5 * copy of this software and associated documentation files (the "Software"),
6 * to deal in the Software without restriction, including without limitation
7 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8 * and/or sell copies of the Software, and to permit persons to whom the
9 * Software is furnished to do so, subject to the following conditions:
11 * The above copyright notice and this permission notice (including the next
12 * paragraph) shall be included in all copies or substantial portions of the
13 * Software.
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
18 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
20 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
21 * IN THE SOFTWARE.
24 /** @file compressedteximage.c
26 * Tests that fetching and uploading compressed texture data works
27 * correctly.
29 * The other compressed texture tests are about decoding of data that
30 * was uploaded from uncompressed, while this tries a round-trip after
31 * the initial upload, testing glGetCompressedTexImage() and
32 * glCompressedTexImage2D().
35 #include "piglit-util-gl.h"
37 #define SIZE 256
39 PIGLIT_GL_TEST_CONFIG_BEGIN
41 config.supports_gl_compat_version = 10;
43 config.window_width = (SIZE*2)+60;
44 config.window_height = SIZE+20;
45 config.window_visual = PIGLIT_GL_VISUAL_RGB | PIGLIT_GL_VISUAL_DOUBLE;
46 config.khr_no_error_support = PIGLIT_NO_ERRORS;
48 PIGLIT_GL_TEST_CONFIG_END
50 struct format {
51 GLenum token;
52 const char **extension;
55 static struct format *format;
57 const char *FXT1[] = {
58 "GL_3DFX_texture_compression_FXT1",
59 NULL
62 const char *S3TC[] = {
63 "GL_EXT_texture_compression_s3tc",
64 NULL
67 const char *S3TC_srgb[] = {
68 "GL_EXT_texture_compression_s3tc",
69 "GL_EXT_texture_sRGB",
70 NULL
73 const char *RGTC[] = {
74 "GL_ARB_texture_compression_rgtc",
75 NULL
78 const char *RGTC_signed[] = {
79 "GL_ARB_texture_compression_rgtc",
80 "GL_EXT_texture_snorm",
81 NULL
84 const char *BPTC[] = {
85 "GL_ARB_texture_compression_bptc",
86 NULL
89 static struct format formats[] = {
90 { GL_COMPRESSED_RGB_FXT1_3DFX, FXT1 },
91 { GL_COMPRESSED_RGBA_FXT1_3DFX, FXT1 },
93 { GL_COMPRESSED_RGB_S3TC_DXT1_EXT, S3TC },
94 { GL_COMPRESSED_RGBA_S3TC_DXT1_EXT, S3TC },
95 { GL_COMPRESSED_RGBA_S3TC_DXT3_EXT, S3TC },
96 { GL_COMPRESSED_RGBA_S3TC_DXT5_EXT, S3TC },
98 { GL_COMPRESSED_SRGB_S3TC_DXT1_EXT, S3TC_srgb },
99 { GL_COMPRESSED_SRGB_ALPHA_S3TC_DXT1_EXT, S3TC_srgb },
100 { GL_COMPRESSED_SRGB_ALPHA_S3TC_DXT3_EXT, S3TC_srgb },
101 { GL_COMPRESSED_SRGB_ALPHA_S3TC_DXT5_EXT, S3TC_srgb },
103 { GL_COMPRESSED_RGBA_BPTC_UNORM, BPTC },
104 { GL_COMPRESSED_SRGB_ALPHA_BPTC_UNORM, BPTC },
105 { GL_COMPRESSED_RGB_BPTC_UNSIGNED_FLOAT, BPTC },
106 { GL_COMPRESSED_RGB_BPTC_SIGNED_FLOAT, BPTC },
108 { GL_COMPRESSED_RED_RGTC1_EXT, RGTC },
109 { GL_COMPRESSED_SIGNED_RED_RGTC1_EXT, RGTC_signed },
110 { GL_COMPRESSED_RED_GREEN_RGTC2_EXT, RGTC },
111 { GL_COMPRESSED_SIGNED_RED_GREEN_RGTC2_EXT, RGTC_signed },
114 static void
115 display_mipmaps(int x, int y)
117 int i;
119 glEnable(GL_TEXTURE_2D);
121 /* Disply all the mipmap levels */
122 for (i = SIZE; i > 0; i /= 2) {
123 piglit_draw_rect_tex(x, y, i, i,
124 0, 0, 1, 1);
126 x += i + 5;
130 static GLboolean
131 check_resulting_mipmaps(int x, int y)
133 GLboolean pass = GL_TRUE;
134 int size;
135 float red[4] = {1.0, 0.0, 0.0, 1.0};
136 float green[4] = {0.0, 1.0, 0.0, 1.0};
137 float blue[4] = {0.0, 0.0, 1.0, 1.0};
138 float white[4] = {1.0, 1.0, 1.0, 1.0};
140 /* for r, rg textures, overwrite what the expected colors are. */
141 if (format->token == GL_COMPRESSED_RED_RGTC1_EXT ||
142 format->token == GL_COMPRESSED_SIGNED_RED_RGTC1_EXT) {
143 green[1] = 0;
144 blue[2] = 0;
145 white[1] = 0;
146 white[2] = 0;
148 if (format->token == GL_COMPRESSED_RED_GREEN_RGTC2_EXT ||
149 format->token == GL_COMPRESSED_SIGNED_RED_GREEN_RGTC2_EXT) {
150 blue[2] = 0;
151 white[2] = 0;
154 for (size = SIZE; size > 0; size /= 2) {
155 if (size == 4)
156 pass = pass && piglit_probe_pixel_rgb(x + 2, y + 2,
157 red);
158 else if (size == 2)
159 pass = pass && piglit_probe_pixel_rgb(x + 1, y + 1,
160 green);
161 else if (size == 1)
162 pass = pass && piglit_probe_pixel_rgb(x, y,
163 blue);
164 else {
165 pass = pass && piglit_probe_pixel_rgb(x + size / 4,
166 y + size / 4,
167 red);
168 pass = pass && piglit_probe_pixel_rgb(x + size * 3 / 4,
169 y + size / 4,
170 green);
171 pass = pass && piglit_probe_pixel_rgb(x + size / 4,
172 y + size * 3 / 4,
173 blue);
174 pass = pass && piglit_probe_pixel_rgb(x + size * 3 / 4,
175 y + size * 3 / 4,
176 white);
178 x += size + 5;
181 return pass;
184 enum piglit_result
185 piglit_display(void)
187 GLuint tex, tex_src;
188 bool pass;
189 int level;
190 unsigned bw, bh, bs;
192 piglit_get_compressed_block_size(format->token, &bw, &bh, &bs);
193 glClearColor(0.5, 0.5, 0.5, 0.5);
194 glClear(GL_COLOR_BUFFER_BIT);
196 tex_src = piglit_rgbw_texture(format->token, SIZE, SIZE,
197 GL_TRUE, GL_FALSE,
198 GL_UNSIGNED_NORMALIZED);
199 glGenTextures(1, &tex);
201 for (level = 0; (SIZE >> level) > 0; level++) {
202 int w, h;
203 int expected_size, size;
204 void *compressed;
206 w = SIZE >> level;
207 h = SIZE >> level;
208 expected_size = piglit_compressed_image_size(format->token, w, h);
210 glBindTexture(GL_TEXTURE_2D, tex_src);
211 glGetTexLevelParameteriv(GL_TEXTURE_2D, level,
212 GL_TEXTURE_COMPRESSED_IMAGE_SIZE,
213 &size);
215 if (size != expected_size) {
216 fprintf(stderr, "Format %s level %d (%dx%d) size %d "
217 "doesn't match expected size %d\n",
218 piglit_get_gl_enum_name(format->token),
219 level, w, h, size, expected_size);
220 piglit_report_result(PIGLIT_FAIL);
223 compressed = malloc(size);
225 glGetCompressedTexImage(GL_TEXTURE_2D, level, compressed);
227 glBindTexture(GL_TEXTURE_2D, tex);
228 glCompressedTexImage2D(GL_TEXTURE_2D, level, format->token,
229 w, h, 0, size, compressed);
230 if (!piglit_check_gl_error(GL_NO_ERROR))
231 piglit_report_result(PIGLIT_FAIL);
233 free(compressed);
236 glDeleteTextures(1, &tex_src);
237 glBindTexture(GL_TEXTURE_2D, tex);
239 display_mipmaps(10, 10);
240 pass = check_resulting_mipmaps(10, 10);
242 piglit_present_results();
244 return pass ? PIGLIT_PASS : PIGLIT_FAIL;
247 static void
248 usage(int argc, char **argv)
250 int i;
252 fprintf(stderr, "Usage: %s <format>\n", argv[0]);
253 fprintf(stderr, "format is one of:\n");
254 for (i = 0; i < ARRAY_SIZE(formats); i++) {
255 fprintf(stderr, " %s\n",
256 piglit_get_gl_enum_name(formats[i].token));
258 exit(1);
261 void
262 piglit_init(int argc, char **argv)
264 int i;
267 piglit_ortho_projection(piglit_width, piglit_height, GL_FALSE);
269 if (argc != 2)
270 usage(argc, argv);
272 format = NULL;
274 const GLenum arg = piglit_get_gl_enum_from_name(argv[1]);
275 for (i = 0; i < ARRAY_SIZE(formats); i++) {
276 if (formats[i].token == arg) {
277 format = &formats[i];
278 break;
282 if (!format)
283 usage(argc, argv);
285 for (i = 0; format->extension[i]; i++)
286 piglit_require_extension(format->extension[i]);
288 if (format->token == GL_COMPRESSED_RGB_BPTC_SIGNED_FLOAT)
289 piglit_set_tolerance_for_bits(7, 7, 7, 7);