ext_gpu_shader4: add compiler tests for everything
[piglit.git] / tests / spec / khr_texture_compression_astc / khr_compressed_astc-miptree.c
blob2fdfb28caa33cb0b7887fb9c25677b4aff6246f1
1 /*
2 * Copyright 2015 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
21 * DEALINGS IN THE SOFTWARE.
24 /**
25 * \file
26 * \brief Test texturing from an ASTC miptree of a real image.
28 * This test is an adaptation of the oes_compressed_etc1_rgb8_textures test.
30 * The files under compressed/2D contain full miptrees, in the
31 * GL_*_ASTC_* formats, of a 2D texture of waffles and fruit [1]. The base
32 * level size was shrunken to 160x106 pixels. The files under the decompressed/2D
33 * directory contain the same miptree in GL_RGBA format. Each miplevel was
34 * obtained by decompressing the corresponding ASTC texture with astcenc [2].
36 * This test draws miplevels of the compressed textures in a space-efficient
37 * manner It does the same when drawing the decompressed texture on the right.
38 * Each miplevel of both images are compared for equality after being drawn.
40 * [1] The reference image is located at http://people.freedesktop.org/~chadversary/permalink/2012-07-09/1574cff2-d091-4421-a3cf-b56c7943d060.jpg.
41 * [2] astcenc is the reference ASTC compression tool, available at http://malideveloper.arm.com/develop-for-mali/tools/software-tools/astc-evaluation-codec/.
44 #include "piglit-util-gl.h"
45 #include "piglit_ktx.h"
47 #define NUM_LEVELS 8
48 #define LEVEL0_WIDTH 160
49 #define LEVEL0_HEIGHT 106
51 #define NUM_VERTICES 4
53 static GLuint prog;
55 static struct piglit_gl_test_config *piglit_config;
57 enum test_type
59 TEST_TYPE_HDR,
60 TEST_TYPE_LDR,
61 TEST_TYPE_SRGB,
62 TEST_TYPE_SRGB_FP,
63 TEST_TYPE_SRGB_SD,
66 enum piglit_result
67 test_miptrees(void* input_type);
69 static enum test_type ldr_test = TEST_TYPE_LDR;
70 static enum test_type hdr_test = TEST_TYPE_HDR;
71 static enum test_type srgb_test = TEST_TYPE_SRGB;
72 static enum test_type srgb_fp_test = TEST_TYPE_SRGB_FP;
73 static enum test_type srgb_skip_test = TEST_TYPE_SRGB_SD;
74 static const struct piglit_subtest subtests[] = {
76 "LDR Profile",
77 "ldr",
78 test_miptrees,
79 &ldr_test,
82 "HDR Profile",
83 "hdr",
84 test_miptrees,
85 &hdr_test,
88 "sRGB decode",
89 "srgb",
90 test_miptrees,
91 &srgb_test,
94 "sRGB decode full precision",
95 "srgb-fp",
96 test_miptrees,
97 &srgb_fp_test,
100 "sRGB skip decode",
101 "srgb-sd",
102 test_miptrees,
103 &srgb_skip_test,
105 {NULL},
108 PIGLIT_GL_TEST_CONFIG_BEGIN
110 piglit_config = &config;
111 config.supports_gl_compat_version = 11;
112 config.supports_gl_es_version = 20;
114 config.window_width = 2 * LEVEL0_WIDTH;
115 config.window_height = LEVEL0_HEIGHT + (LEVEL0_HEIGHT >> 1);
116 config.window_visual = PIGLIT_GL_VISUAL_RGB | PIGLIT_GL_VISUAL_DOUBLE;
118 config.subtests = subtests;
120 PIGLIT_GL_TEST_CONFIG_END
124 * The \a filename is relative to the current test's source directory.
126 * A new texture is created and returned in \a tex_name.
128 static void
129 load_texture(const char *dir1, const char *dir2,
130 const char *block_dim_str, GLuint *tex_name)
132 struct piglit_ktx *ktx;
133 const struct piglit_ktx_info *info;
134 char filepath[4096];
135 char cur_file[20];
136 bool ok = true;
138 /* Generate filename for compressed texture */
139 snprintf(cur_file, sizeof(cur_file), "waffles-%s.ktx",
140 block_dim_str);
142 piglit_join_paths(filepath, sizeof(filepath), 7,
143 piglit_source_dir(),
144 "tests",
145 "spec",
146 "khr_texture_compression_astc",
147 dir1,
148 dir2,
149 cur_file);
151 ktx = piglit_ktx_read_file(filepath);
152 if (ktx == NULL)
153 piglit_report_result(PIGLIT_FAIL);
155 info = piglit_ktx_get_info(ktx);
156 assert(info->num_miplevels == NUM_LEVELS);
157 assert(info->target == GL_TEXTURE_2D);
158 assert(info->pixel_width == LEVEL0_WIDTH);
159 assert(info->pixel_height == LEVEL0_HEIGHT);
161 *tex_name = 0;
162 ok = piglit_ktx_load_texture(ktx, tex_name, NULL);
163 if (!ok)
164 piglit_report_result(PIGLIT_FAIL);
166 piglit_ktx_destroy(ktx);
169 /** Compares the compressed texture against the decompressed texture */
170 bool draw_compare_levels(bool check_error, bool srgb_skip_decode,
171 GLint level_pixel_size_loc, GLint pixel_offset_loc,
172 GLuint compressed_tex, GLuint decompressed_tex)
174 /* Fully-saturated magenta */
175 static const float error_color[4] = {1.0, 0.0, 1.0, 1.0};
177 unsigned y = 0;
178 unsigned x = 0;
179 bool pass = true;
180 int level = 0;
182 for (; level < NUM_LEVELS; ++level) {
183 int w = LEVEL0_WIDTH >> level;
184 int h = LEVEL0_HEIGHT >> level;
185 glUniform2f(level_pixel_size_loc, (float) w, (float) h);
188 /* Draw miplevel of compressed texture. */
189 glBindTexture(GL_TEXTURE_2D, compressed_tex);
190 if (srgb_skip_decode)
191 glTexParameteri(GL_TEXTURE_2D,
192 GL_TEXTURE_SRGB_DECODE_EXT,
193 GL_SKIP_DECODE_EXT);
194 glUniform2f(pixel_offset_loc, x, y);
195 glDrawArrays(GL_TRIANGLE_FAN, 0, NUM_VERTICES);
197 /* Draw miplevel of decompressed texture. */
198 if (!check_error) {
199 glBindTexture(GL_TEXTURE_2D, decompressed_tex);
200 if (srgb_skip_decode)
201 glTexParameteri(GL_TEXTURE_2D,
202 GL_TEXTURE_SRGB_DECODE_EXT,
203 GL_SKIP_DECODE_EXT);
204 glUniform2f(pixel_offset_loc, LEVEL0_WIDTH + x, y);
205 glDrawArrays(GL_TRIANGLE_FAN, 0, NUM_VERTICES);
208 /* Check the textures (or error-colors) for equivalence. */
209 if (pass) {
210 if (check_error) {
211 pass = piglit_probe_rect_rgba(x, y, w, h,
212 error_color);
213 } else {
214 pass = piglit_probe_rects_equal(x, y,
215 LEVEL0_WIDTH + x, y,
216 w, h, GL_RGBA);
219 if (!pass)
220 piglit_loge("Miplevel %d", level);
223 /* Update the next miplevel arrangement */
224 if (level == 1)
225 x += w;
226 else
227 y += h;
230 /* Delete bound textures */
231 glDeleteTextures(1, &compressed_tex);
232 glDeleteTextures(1, &decompressed_tex);
234 piglit_present_results();
235 return pass;
238 enum piglit_result
239 test_miptrees(void* input_type)
241 const enum test_type subtest = *(enum test_type*) input_type;
242 const bool is_srgb_test = subtest == TEST_TYPE_SRGB;
243 const bool is_srgb_skip_decode_test = subtest == TEST_TYPE_SRGB_SD;
244 const bool is_hdr_test = subtest == TEST_TYPE_HDR;
246 static const char * tests[5] = {"hdr", "ldrl", "ldrs", "ldrs", "ldrs"};
247 static const char * block_dim_str[14] = {
248 "4x4",
249 "5x4",
250 "5x5",
251 "6x5",
252 "6x6",
253 "8x5",
254 "8x6",
255 "8x8",
256 "10x5",
257 "10x6",
258 "10x8",
259 "10x10",
260 "12x10",
261 "12x12"
264 GLint pixel_offset_loc = glGetUniformLocation(prog, "pixel_offset");
265 GLint level_pixel_size_loc = glGetUniformLocation(prog,
266 "level_pixel_size");
268 if (is_srgb_skip_decode_test)
269 piglit_require_extension("GL_EXT_texture_sRGB_decode");
271 /* Check for error color if an LDR-only sys reading an HDR
272 * texture. No need to draw a reference mipmap in this case.
274 const bool has_hdr = piglit_is_extension_supported(
275 "GL_KHR_texture_compression_astc_hdr");
276 const bool check_error = is_hdr_test && !has_hdr;
277 int block_dims;
279 if (is_srgb_test)
280 /* Loosen up the tolerence for sRGB tests. This will allow testing
281 * sRGB formats which have known precision issues in void extent
282 * blocks. See khronos bug#11294 for details.
284 piglit_set_tolerance_for_bits(7, 7, 7, 7);
285 else
286 piglit_set_tolerance_for_bits(8, 8, 8, 8);
288 for (block_dims = 0; block_dims < ARRAY_SIZE(block_dim_str); ++block_dims) {
290 /* Texture objects. */
291 GLuint tex_compressed = 0;
292 GLuint tex_decompressed = 0;
294 /* Load texture for current submode and block size */
295 load_texture("compressed/2D", tests[subtest],
296 block_dim_str[block_dims],
297 &tex_compressed);
298 if (!check_error) {
299 load_texture("decompressed/2D", tests[subtest],
300 block_dim_str[block_dims],
301 &tex_decompressed);
304 /* Draw and compare each level of the two textures */
305 glClear(GL_COLOR_BUFFER_BIT);
306 if (!draw_compare_levels(check_error, is_srgb_skip_decode_test,
307 level_pixel_size_loc,
308 pixel_offset_loc,
309 tex_compressed,
310 tex_decompressed)) {
311 piglit_loge("Mode %s Block %s.",
312 tests[subtest],
313 block_dim_str[block_dims]);
314 return PIGLIT_FAIL;
317 return PIGLIT_PASS;
320 void
321 piglit_init(int argc, char **argv)
323 static const char vs_source[] =
324 "#version 100\n"
325 "\n"
326 "uniform vec2 window_pixel_size;\n"
327 "uniform vec2 level_pixel_size;\n"
328 "uniform vec2 pixel_offset;\n"
329 "\n"
330 "// vertex is some corner of the unit square [0,1]^2 \n"
331 "attribute vec2 vertex;\n"
332 "varying vec2 tex_coord;\n"
333 "\n"
334 "void main()\n"
335 "{\n"
336 " vec2 pos = vertex;\n"
337 " pos *= level_pixel_size;\n"
338 " pos += pixel_offset;\n"
339 " pos /= 0.5 * window_pixel_size;\n"
340 " pos -= vec2(1, 1);\n"
341 " gl_Position = vec4(pos.xy, 0.0, 1.0);\n"
342 "\n"
343 " tex_coord = vertex;\n"
344 "}\n";
346 static const char fs_source[] =
347 "#version 100\n"
348 "precision highp float;\n"
349 "\n"
350 "uniform sampler2D tex;\n"
351 "varying vec2 tex_coord;\n"
352 "\n"
353 "void main()\n"
354 "{\n"
355 " vec4 t = texture2D(tex, tex_coord);\n"
356 " gl_FragColor = vec4(t.rgb, 1.0);\n"
357 "}\n";
359 /* Vertices to draw a square triangle strip. */
360 static const GLfloat vertices[2 * NUM_VERTICES] = {
361 0, 0,
362 1, 0,
363 1, 1,
364 0, 1,
367 GLint vertex_loc;
368 GLuint vertex_buf;
369 GLuint vao;
371 piglit_require_extension("GL_KHR_texture_compression_astc_ldr");
373 glClearColor(0.9098, 0.8314, 0.7843, 1.0);
374 glViewport(0, 0, piglit_width, piglit_height);
376 glGenBuffers(1, &vertex_buf);
377 glBindBuffer(GL_ARRAY_BUFFER, vertex_buf);
379 glGenVertexArrays(1, &vao);
380 glBindVertexArray(vao);
382 prog = piglit_build_simple_program(vs_source, fs_source);
383 glReleaseShaderCompiler();
384 glUseProgram(prog);
386 vertex_loc = glGetAttribLocation(prog, "vertex");
387 glEnableVertexAttribArray(vertex_loc);
388 glVertexAttribPointer(vertex_loc, 2, GL_FLOAT, GL_FALSE, 0, NULL);
389 glBufferData(GL_ARRAY_BUFFER, sizeof(vertices), vertices,
390 GL_STATIC_DRAW);
392 glUniform1i(glGetUniformLocation(prog, "tex"), 0);
393 glUniform2f(glGetUniformLocation(prog, "window_pixel_size"),
394 piglit_width, piglit_height);
397 enum piglit_result
398 piglit_display(void)
400 return piglit_run_selected_subtests(piglit_config->subtests,
401 piglit_config->selected_subtests,
402 piglit_config->num_selected_subtests,
403 PIGLIT_SKIP);