perf/pixel-rate: new pixel throughput microbenchmark
[piglit.git] / tests / spec / oes_texture_compression_astc / oes_compressed_astc-miptree-3d.c
blob8b3750918b8cd7146b8ce1fb9f940790aaa300c9
1 /*
2 * Copyright 2016 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 * The files under compressed/3D/{hdr, ldrl, ldrs} contain full miptrees, in
29 * the GL_*_ASTC_* formats, of a 3D texture of waffles and fruit [1]. The base
30 * level size was shrunken to 160x106 pixels and used to create a 3D texture
31 * with depth=8. The files under the decompressed/3D/{hdr, ldrl, ldrs} directory
32 * contain the same miptree in GL_RGBA format. Each miplevel was obtained by
33 * decompressing the corresponding ASTC texture with astcenc [2].
35 * This test draws miplevels of the compressed textures in a space-efficient
36 * manner. It does the same when drawing the decompressed texture on the right.
37 * Each miplevel of both images are compared for equality after being drawn.
39 * [1] The reference image is located at:
40 * 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:
42 * http://malideveloper.arm.com/develop-for-mali/tools/software-tools/astc-evaluation-codec/.
45 #include "piglit-util-gl.h"
46 #include "piglit_ktx.h"
48 #define NUM_LEVELS 3
49 #define LEVEL0_WIDTH 160
50 #define LEVEL0_HEIGHT 106
51 #define LEVEL0_DEPTH 8
53 #define NUM_VERTICES 4
55 static GLuint prog;
57 static struct piglit_gl_test_config *piglit_config;
59 enum test_type
61 TEST_TYPE_HDR,
62 TEST_TYPE_LDR,
63 TEST_TYPE_SRGB,
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 const struct piglit_subtest subtests[] = {
74 "LDR Profile",
75 "ldr",
76 test_miptrees,
77 &ldr_test,
80 "HDR Profile",
81 "hdr",
82 test_miptrees,
83 &hdr_test,
86 "sRGB decode",
87 "srgb",
88 test_miptrees,
89 &srgb_test,
91 {NULL},
94 PIGLIT_GL_TEST_CONFIG_BEGIN
96 piglit_config = &config;
97 config.supports_gl_compat_version = 11;
98 config.supports_gl_es_version = 30;
100 config.window_width = 2 * LEVEL0_WIDTH;
101 config.window_height = LEVEL0_HEIGHT + (LEVEL0_HEIGHT >> 1);
102 config.window_visual = PIGLIT_GL_VISUAL_RGB | PIGLIT_GL_VISUAL_DOUBLE;
104 config.subtests = subtests;
106 PIGLIT_GL_TEST_CONFIG_END
109 * The \a filename is relative to the current test's source directory.
111 * A new texture is created and returned in \a tex_name.
113 static void
114 load_texture(const char *dir1, const char *dir2,
115 const char *block_dim_str, GLuint *tex_name)
117 struct piglit_ktx *ktx;
118 const struct piglit_ktx_info *info;
119 char filepath[4096];
120 char cur_file[20];
121 bool ok = true;
123 /* Generate filename for compressed texture */
124 snprintf(cur_file, sizeof(cur_file), "waffles-%s.ktx",
125 block_dim_str);
127 piglit_join_paths(filepath, sizeof(filepath), 7,
128 piglit_source_dir(),
129 "tests",
130 "spec",
131 "oes_texture_compression_astc",
132 dir1,
133 dir2,
134 cur_file);
136 ktx = piglit_ktx_read_file(filepath);
137 if (ktx == NULL)
138 piglit_report_result(PIGLIT_FAIL);
140 info = piglit_ktx_get_info(ktx);
141 assert(info->num_miplevels == NUM_LEVELS);
142 assert(info->target == GL_TEXTURE_3D);
143 assert(info->pixel_width == LEVEL0_WIDTH);
144 assert(info->pixel_height == LEVEL0_HEIGHT);
145 assert(info->pixel_depth == LEVEL0_DEPTH);
147 *tex_name = 0;
148 ok = piglit_ktx_load_texture(ktx, tex_name, NULL);
149 if (!ok)
150 piglit_report_result(PIGLIT_FAIL);
152 piglit_ktx_destroy(ktx);
155 /** Compares the compressed texture against the decompressed texture */
156 bool draw_compare_levels(bool check_error,
157 GLint level_pixel_size_loc, GLint pixel_offset_loc,
158 GLint slice_loc, GLint depth_loc, GLint slice,
159 GLuint compressed_tex, GLuint decompressed_tex)
161 /* Fully-saturated magenta */
162 static const float error_color[4] = {1.0, 0.0, 1.0, 1.0};
164 unsigned y = 0;
165 unsigned x = 0;
166 bool result = true;
167 int level = 0;
169 for (; level < NUM_LEVELS; ++level) {
170 int w = LEVEL0_WIDTH >> level;
171 int h = LEVEL0_HEIGHT >> level;
172 int d = LEVEL0_DEPTH >> level;
173 bool pass = true;
174 glUniform2f(level_pixel_size_loc, (float) w, (float) h);
175 glUniform1f(slice_loc, slice);
176 glUniform1f(depth_loc, d);
178 /* Draw miplevel of compressed texture. */
179 glBindTexture(GL_TEXTURE_3D, compressed_tex);
180 glUniform2f(pixel_offset_loc, x, y);
181 glDrawArrays(GL_TRIANGLE_FAN, 0, NUM_VERTICES);
183 /* Check the textures (or error-colors) for equivalence. */
184 if (check_error) {
185 pass = piglit_probe_rect_rgba(x, y, w, h,
186 error_color);
187 } else {
188 /* Draw miplevel of decompressed texture. */
189 glBindTexture(GL_TEXTURE_3D, decompressed_tex);
190 glUniform2f(pixel_offset_loc, LEVEL0_WIDTH + x, y);
191 glDrawArrays(GL_TRIANGLE_FAN, 0, NUM_VERTICES);
193 pass = piglit_probe_rects_equal(x, y,
194 LEVEL0_WIDTH + x, y,
195 w, h, GL_RGBA);
198 if (!pass) {
199 piglit_loge("Slice: %d, Miplevel: %d",
200 slice, level);
201 result = false;
204 /* Update the next miplevel arrangement */
205 if (level == 1)
206 x += w;
207 else
208 y += h;
211 piglit_present_results();
212 return result;
215 enum piglit_result
216 test_miptrees(void* input_type)
218 GLint slice_loc, depth_loc, pixel_offset_loc, level_pixel_size_loc;
219 const enum test_type subtest = *(enum test_type*) input_type;
220 const bool is_srgb_test = subtest == TEST_TYPE_SRGB;
221 const bool is_hdr_test = subtest == TEST_TYPE_HDR;
223 static const char * tests[3] = {"hdr", "ldrl", "ldrs"};
224 static const char * block_dim_str[] = {
225 "3x3x3",
226 "4x3x3",
227 "4x4x3",
228 "4x4x4",
229 "5x4x4",
230 "5x5x4",
231 "5x5x5",
232 "6x5x5",
233 "6x6x5",
234 "6x6x6"
237 pixel_offset_loc = glGetUniformLocation(prog, "pixel_offset");
238 level_pixel_size_loc = glGetUniformLocation(prog, "level_pixel_size");
239 slice_loc = glGetUniformLocation(prog, "slice");
240 depth_loc = glGetUniformLocation(prog, "depth");
242 /* Check for error color if an LDR-only sys reading an HDR
243 * texture. No need to draw a reference mipmap in this case.
245 const bool has_hdr = piglit_is_extension_supported(
246 "GL_KHR_texture_compression_astc_hdr");
247 const bool check_error = is_hdr_test && !has_hdr;
248 int block_dims = 0, slice;;
250 if (is_srgb_test)
251 /* Loosen up the tolerance for sRGB tests. This will allow testing
252 * sRGB formats which have known precision issues in void extent
253 * blocks. See khronos bug#11294 for details.
255 piglit_set_tolerance_for_bits(7, 7, 7, 7);
256 else
257 piglit_set_tolerance_for_bits(8, 8, 8, 8);
259 for ( ; block_dims < ARRAY_SIZE(block_dim_str); ++block_dims) {
260 /* Texture objects. */
261 GLuint tex_compressed = 0;
262 GLuint tex_decompressed = 0;
264 /* Load texture for current submode and block size */
265 load_texture("compressed/3D", tests[subtest],
266 block_dim_str[block_dims],
267 &tex_compressed);
268 if (!check_error) {
269 load_texture("decompressed/3D", tests[subtest],
270 block_dim_str[block_dims],
271 &tex_decompressed);
274 for (slice = 0; slice < LEVEL0_DEPTH; slice++) {
275 /* Draw and compare each level of the two textures */
276 glClear(GL_COLOR_BUFFER_BIT);
277 if (!draw_compare_levels(check_error,
278 level_pixel_size_loc,
279 pixel_offset_loc,
280 slice_loc, depth_loc,
281 slice,
282 tex_compressed,
283 tex_decompressed)) {
284 piglit_loge("Mode: %s Block: %s.",
285 tests[subtest],
286 block_dim_str[block_dims]);
287 return PIGLIT_FAIL;
290 /* Delete bound textures */
291 glDeleteTextures(1, &tex_compressed);
292 if (!check_error)
293 glDeleteTextures(1, &tex_decompressed);
295 return PIGLIT_PASS;
298 void
299 piglit_init(int argc, char **argv)
301 static const char vs_source[] =
302 "#version 300 es\n"
303 "\n"
304 "uniform vec2 window_pixel_size;\n"
305 "uniform vec2 level_pixel_size;\n"
306 "uniform vec2 pixel_offset;\n"
307 "\n"
308 "// vertex is some corner of the unit square [0,1]^2 \n"
309 "in vec2 vertex;\n"
310 "out vec2 tex_coord;\n"
311 "\n"
312 "void main()\n"
313 "{\n"
314 " vec2 pos = vertex;\n"
315 " pos *= level_pixel_size;\n"
316 " pos += pixel_offset;\n"
317 " pos /= 0.5 * window_pixel_size;\n"
318 " pos -= vec2(1, 1);\n"
319 " gl_Position = vec4(pos.xy, 0.0, 1.0);\n"
320 "\n"
321 " tex_coord = vertex;\n"
322 "}\n";
324 static const char fs_source[] =
325 "#version 300 es\n"
326 "precision highp float;\n"
327 "\n"
328 "uniform highp sampler3D tex;\n"
329 "uniform float slice;\n"
330 "uniform float depth;\n"
331 "in vec2 tex_coord;\n"
332 "out vec4 fragment_color;\n"
333 "\n"
334 "void main()\n"
335 "{\n"
336 " vec4 t = texture(tex, vec3(tex_coord.x, tex_coord.y,\n"
337 " slice / (depth - 1.0)));\n"
338 " fragment_color = vec4(t.rgb, 1.0);\n"
339 "}\n";
341 /* Vertices to draw a square triangle strip. */
342 static const GLfloat vertices[2 * NUM_VERTICES] = {
343 0, 0,
344 1, 0,
345 1, 1,
346 0, 1,
349 GLint vertex_loc;
350 GLuint vertex_buf;
351 GLuint vao;
353 piglit_require_extension("GL_OES_texture_compression_astc");
355 if (!piglit_is_gles())
356 piglit_require_extension("GL_ARB_ES3_compatibility");
358 glClearColor(0.9098, 0.8314, 0.7843, 1.0);
359 glViewport(0, 0, piglit_width, piglit_height);
361 glGenBuffers(1, &vertex_buf);
362 glBindBuffer(GL_ARRAY_BUFFER, vertex_buf);
364 glGenVertexArrays(1, &vao);
365 glBindVertexArray(vao);
367 prog = piglit_build_simple_program(vs_source, fs_source);
368 glReleaseShaderCompiler();
369 glUseProgram(prog);
371 vertex_loc = glGetAttribLocation(prog, "vertex");
372 glEnableVertexAttribArray(vertex_loc);
373 glVertexAttribPointer(vertex_loc, 2, GL_FLOAT, GL_FALSE, 0, NULL);
374 glBufferData(GL_ARRAY_BUFFER, sizeof(vertices), vertices,
375 GL_STATIC_DRAW);
377 glUniform1i(glGetUniformLocation(prog, "tex"), 0);
378 glUniform2f(glGetUniformLocation(prog, "window_pixel_size"),
379 piglit_width, piglit_height);
382 enum piglit_result
383 piglit_display(void)
385 return piglit_run_selected_subtests(piglit_config->subtests,
386 piglit_config->selected_subtests,
387 piglit_config->num_selected_subtests,
388 PIGLIT_SKIP);