1 /*
2  * Copyright 2016 Intel Corporation
3  *
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:
10  *
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.
14  *
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.
22  */
23 
24 /**
25  * \file
26  * \brief Test texturing from an ASTC miptree of a real image.
27  *
28  * The files under compressed/SLICED3D/{hdr, ldrl, ldrs} contain full miptrees,
29  * in the GL_*_ASTC_* formats, of a sliced 3D texture of waffles and fruit [1].
30  * The base level size was shrunken to 160x106 pixels and used to create a sliced
31  * 3D texture with depth=8.
32  * The files under the decompressed/SLICED3D/{hdr, ldrl, ldrs} directory contain
33  * the same miptree in GL_RGBA format. Each miplevel was obtained by decompressing
34  * the corresponding ASTC texture with astcenc [2].
35  *
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.
39  *
40  * [1] The reference image is located at:
41  *     http://people.freedesktop.org/~chadversary/permalink/2012-07-09/1574cff2-d091-4421-a3cf-b56c7943d060.jpg.
42  * [2] astcenc is the reference ASTC compression tool, available at:
43  *     http://malideveloper.arm.com/develop-for-mali/tools/software-tools/astc-evaluation-codec/.
44  */
45 
46 #include "piglit-util-gl.h"
47 #include "piglit_ktx.h"
48 
49 #define NUM_LEVELS 3
50 #define LEVEL0_WIDTH 160
51 #define LEVEL0_HEIGHT 106
52 #define LEVEL0_DEPTH 8
53 
54 #define NUM_VERTICES 4
55 
56 static GLuint prog;
57 
58 static struct piglit_gl_test_config *piglit_config;
59 
60 enum test_type
61 {
62 	TEST_TYPE_HDR,
63 	TEST_TYPE_LDR,
64 	TEST_TYPE_SRGB,
65 	TEST_TYPE_SRGB_FP,
66 };
67 
68 enum piglit_result
69 test_miptrees(void* input_type);
70 
71 static enum test_type ldr_test  = TEST_TYPE_LDR;
72 static enum test_type hdr_test  = TEST_TYPE_HDR;
73 static enum test_type srgb_test = TEST_TYPE_SRGB;
74 static enum test_type srgb_fp_test = TEST_TYPE_SRGB_FP;
75 static const struct piglit_subtest subtests[] = {
76 	{
77 		"LDR Profile",
78 		"ldr",
79 		test_miptrees,
80 		&ldr_test,
81 	},
82 	{
83 		"HDR Profile",
84 		"hdr",
85 		test_miptrees,
86 		&hdr_test,
87 	},
88 	{
89 		"sRGB decode",
90 		"srgb",
91 		test_miptrees,
92 		&srgb_test,
93 	},
94 	{
95 		"sRGB decode full precision",
96 		"srgb-fp",
97 		test_miptrees,
98 		&srgb_fp_test,
99 	},
100 	{NULL},
101 };
102 
103 PIGLIT_GL_TEST_CONFIG_BEGIN
104 
105 	piglit_config = &config;
106 	config.supports_gl_compat_version = 11;
107 	config.supports_gl_es_version = 30;
108 
109 	config.window_width = 2 * LEVEL0_WIDTH;
110 	config.window_height = LEVEL0_HEIGHT + (LEVEL0_HEIGHT >> 1);
111 	config.window_visual = PIGLIT_GL_VISUAL_RGB | PIGLIT_GL_VISUAL_DOUBLE;
112 
113 	config.subtests = subtests;
114 
115 PIGLIT_GL_TEST_CONFIG_END
116 
117 /**
118  * The \a filename is relative to the current test's source directory.
119  *
120  * A new texture is created and returned in \a tex_name.
121  */
122 static void
load_texture(const char * dir1,const char * dir2,const char * block_dim_str,GLuint * tex_name)123 load_texture(const char *dir1, const char *dir2,
124 	     const char *block_dim_str, GLuint *tex_name)
125 {
126 	struct piglit_ktx *ktx;
127 	const struct piglit_ktx_info *info;
128 	char filepath[4096];
129 	char cur_file[20];
130 	bool ok = true;
131 
132 	/* Generate filename for compressed texture */
133 	snprintf(cur_file, sizeof(cur_file), "waffles-%s.ktx",
134 		 block_dim_str);
135 
136 	piglit_join_paths(filepath, sizeof(filepath), 7,
137 	                  piglit_source_dir(),
138 	                  "tests",
139 	                  "spec",
140 	                  "khr_texture_compression_astc",
141 	                  dir1,
142 	                  dir2,
143 	                  cur_file);
144 
145 	ktx = piglit_ktx_read_file(filepath);
146 	if (ktx == NULL)
147 		piglit_report_result(PIGLIT_FAIL);
148 
149 	info = piglit_ktx_get_info(ktx);
150 	assert(info->num_miplevels == NUM_LEVELS);
151 	assert(info->target == GL_TEXTURE_3D);
152 	assert(info->pixel_width == LEVEL0_WIDTH);
153 	assert(info->pixel_height == LEVEL0_HEIGHT);
154 	assert(info->pixel_depth == LEVEL0_DEPTH);
155 
156 	*tex_name = 0;
157 	ok = piglit_ktx_load_texture(ktx, tex_name, NULL);
158 	if (!ok)
159 		piglit_report_result(PIGLIT_FAIL);
160 
161 	piglit_ktx_destroy(ktx);
162 }
163 
164 /** Compares the compressed texture against the decompressed texture */
draw_compare_levels(bool check_error,GLint level_pixel_size_loc,GLint pixel_offset_loc,GLint slice_loc,GLint depth_loc,GLint slice,GLuint compressed_tex,GLuint decompressed_tex)165 bool draw_compare_levels(bool check_error,
166 			 GLint level_pixel_size_loc, GLint pixel_offset_loc,
167 			 GLint slice_loc, GLint depth_loc, GLint slice,
168 			 GLuint compressed_tex, GLuint decompressed_tex)
169 {
170 	/* Fully-saturated magenta */
171 	static const float error_color[4] = {1.0, 0.0, 1.0, 1.0};
172 
173 	unsigned y = 0;
174 	unsigned x = 0;
175 	bool result = true;
176 	int level = 0;
177 
178 	for (; level < NUM_LEVELS; ++level) {
179 		int w = LEVEL0_WIDTH >> level;
180 		int h = LEVEL0_HEIGHT >> level;
181 		int d = LEVEL0_DEPTH >> level;
182 		bool pass = true;
183 		glUniform2f(level_pixel_size_loc, (float) w, (float) h);
184 		glUniform1f(slice_loc, slice);
185 		glUniform1f(depth_loc, d);
186 
187 		/* Draw miplevel of compressed texture. */
188 		glBindTexture(GL_TEXTURE_3D, compressed_tex);
189 		glUniform2f(pixel_offset_loc, x, y);
190 		glDrawArrays(GL_TRIANGLE_FAN, 0, NUM_VERTICES);
191 
192 		/* Check the textures (or error-colors) for equivalence. */
193 		if (check_error) {
194 			pass = piglit_probe_rect_rgba(x, y, w, h,
195 						      error_color);
196 		} else {
197 			/* Draw miplevel of decompressed texture. */
198 			glBindTexture(GL_TEXTURE_3D, decompressed_tex);
199 			glUniform2f(pixel_offset_loc, LEVEL0_WIDTH + x, y);
200 			glDrawArrays(GL_TRIANGLE_FAN, 0, NUM_VERTICES);
201 
202 			pass = piglit_probe_rects_equal(x, y,
203 						LEVEL0_WIDTH + x, y,
204 						w, h, GL_RGBA);
205 		}
206 		if (!pass) {
207 			piglit_loge("Slice: %d, Miplevel: %d",
208 				    slice, level);
209 			result = false;
210 		}
211 
212 		/* Update the next miplevel arrangement */
213 		if (level == 1)
214 			x += w;
215 		else
216 			y += h;
217 	}
218 
219 	piglit_present_results();
220 	return result;
221 }
222 
223 enum piglit_result
test_miptrees(void * input_type)224 test_miptrees(void* input_type)
225 {
226 	GLint slice_loc, depth_loc, pixel_offset_loc, level_pixel_size_loc;
227 	const enum test_type subtest = *(enum test_type*) input_type;
228 	const bool is_srgb_test = subtest == TEST_TYPE_SRGB;
229 	const bool is_hdr_test  = subtest == TEST_TYPE_HDR;
230 
231 	static const char * tests[4] = {"hdr", "ldrl", "ldrs", "ldrs"};
232 	static const char * block_dim_str[14] = {
233 		"4x4",
234 		"5x4",
235 		"5x5",
236 		"6x5",
237 		"6x6",
238 		"8x5",
239 		"8x6",
240 		"8x8",
241 		"10x5",
242 		"10x6",
243 		"10x8",
244 		"10x10",
245 		"12x10",
246 		"12x12"
247 	};
248 
249 	piglit_require_extension("GL_KHR_texture_compression_astc_ldr");
250 
251 	pixel_offset_loc = glGetUniformLocation(prog, "pixel_offset");
252 	level_pixel_size_loc = glGetUniformLocation(prog, "level_pixel_size");
253 	slice_loc = glGetUniformLocation(prog, "slice");
254 	depth_loc = glGetUniformLocation(prog, "depth");
255 
256 	/*  Check for error color if an LDR-only sys reading an HDR
257 	 *  texture. No need to draw a reference mipmap in this case.
258 	 */
259 	const bool has_hdr = piglit_is_extension_supported(
260 				"GL_KHR_texture_compression_astc_hdr");
261 	const bool check_error = is_hdr_test && !has_hdr;
262 	int slice, block_dims = 0;
263 
264 	if (!has_hdr)
265 		piglit_require_extension(
266 			"GL_KHR_texture_compression_astc_sliced_3d");
267 
268 	if (is_srgb_test)
269 		/* Loosen up the tolerence for sRGB tests. This will allow testing
270 		 * sRGB formats which have known precision issues in void extent
271 		 * blocks. See khronos bug#11294 for details.
272 		 */
273 		piglit_set_tolerance_for_bits(7, 7, 7, 7);
274 	else
275 		piglit_set_tolerance_for_bits(8, 8, 8, 8);
276 
277 	for ( ; block_dims < ARRAY_SIZE(block_dim_str); block_dims++) {
278 		/* Texture objects. */
279 		GLuint tex_compressed = 0;
280 		GLuint tex_decompressed = 0;
281 
282 		/* Load texture for current submode and block size */
283 		load_texture("compressed/SLICED3D", tests[subtest],
284 				block_dim_str[block_dims],
285 				&tex_compressed);
286 		if (!check_error) {
287 			load_texture("decompressed/SLICED3D",
288 				     tests[subtest],
289 				     block_dim_str[block_dims],
290 				     &tex_decompressed);
291 		}
292 		for (slice = 0 ; slice < LEVEL0_DEPTH; slice++) {
293 			/* Draw and compare each level of the two textures */
294 			glClear(GL_COLOR_BUFFER_BIT);
295 			if (!draw_compare_levels(check_error,
296 						 level_pixel_size_loc,
297 						 pixel_offset_loc,
298 						 slice_loc, depth_loc,
299 						 slice,
300 						 tex_compressed,
301 						 tex_decompressed)) {
302 				piglit_loge("Mode: %s Block: %s.",
303 					    tests[subtest],
304 					    block_dim_str[block_dims]);
305 				return PIGLIT_FAIL;
306 			}
307 		}
308 		/* Delete bound textures */
309 		glDeleteTextures(1, &tex_compressed);
310 		if (!check_error)
311 			glDeleteTextures(1, &tex_decompressed);
312 
313 	}
314 	return PIGLIT_PASS;
315 }
316 
317 void
piglit_init(int argc,char ** argv)318 piglit_init(int argc, char **argv)
319 {
320 	static const char vs_source[] =
321 		"#version 300 es\n"
322 		"\n"
323 		"uniform vec2 window_pixel_size;\n"
324 		"uniform vec2 level_pixel_size;\n"
325 		"uniform vec2 pixel_offset;\n"
326 		"\n"
327 		"// vertex is some corner of the unit square [0,1]^2 \n"
328 		"in vec2 vertex;\n"
329 		"out vec2 tex_coord;\n"
330 		"\n"
331 		"void main()\n"
332 		"{\n"
333 		"    vec2 pos = vertex;\n"
334 		"    pos *= level_pixel_size;\n"
335 		"    pos += pixel_offset;\n"
336 		"    pos /= 0.5 * window_pixel_size;\n"
337 		"    pos -= vec2(1, 1);\n"
338 		"    gl_Position = vec4(pos.xy, 0.0, 1.0);\n"
339 		"\n"
340 		"    tex_coord = vertex;\n"
341 		"}\n";
342 
343 	static const char fs_source[] =
344 		"#version 300 es\n"
345 		"precision highp float;\n"
346 		"\n"
347 		"uniform highp sampler3D tex;\n"
348 		"uniform float slice;\n"
349 		"uniform float depth;\n"
350 		"in vec2 tex_coord;\n"
351 		"out vec4 fragment_color;\n"
352 		"\n"
353 		"void main()\n"
354 		"{\n"
355 		"    vec4 t = texture(tex, vec3(tex_coord.x, tex_coord.y,\n"
356 		"                     slice / (depth - 1.0)));\n"
357 		"    fragment_color = vec4(t.rgb, 1.0);\n"
358 		"}\n";
359 
360 	/* Vertices to draw a square triangle strip. */
361 	static const GLfloat vertices[2 * NUM_VERTICES] = {
362 		0, 0,
363 		1, 0,
364 		1, 1,
365 		0, 1,
366 	};
367 
368 	GLint vertex_loc;
369 	GLuint vertex_buf;
370 	GLuint vao;
371 
372 	if (!piglit_is_gles())
373 		piglit_require_extension("GL_ARB_ES3_compatibility");
374 
375 	glClearColor(0.9098, 0.8314, 0.7843, 1.0);
376 	glViewport(0, 0, piglit_width, piglit_height);
377 
378 	glGenBuffers(1, &vertex_buf);
379 	glBindBuffer(GL_ARRAY_BUFFER, vertex_buf);
380 
381 	glGenVertexArrays(1, &vao);
382 	glBindVertexArray(vao);
383 
384 	prog = piglit_build_simple_program(vs_source, fs_source);
385 	glReleaseShaderCompiler();
386 	glUseProgram(prog);
387 
388 	vertex_loc = glGetAttribLocation(prog, "vertex");
389 	glEnableVertexAttribArray(vertex_loc);
390 	glVertexAttribPointer(vertex_loc, 2, GL_FLOAT, GL_FALSE, 0, NULL);
391 	glBufferData(GL_ARRAY_BUFFER, sizeof(vertices), vertices,
392 	             GL_STATIC_DRAW);
393 
394 	glUniform1i(glGetUniformLocation(prog, "tex"), 0);
395 	glUniform2f(glGetUniformLocation(prog, "window_pixel_size"),
396 	            piglit_width, piglit_height);
397 }
398 
399 enum piglit_result
piglit_display(void)400 piglit_display(void)
401 {
402 	return piglit_run_selected_subtests(piglit_config->subtests,
403 				      piglit_config->selected_subtests,
404 				      piglit_config->num_selected_subtests,
405 				      PIGLIT_SKIP);
406 }
407