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
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.
26 * \brief Test texturing from an ASTC miptree of a real image.
28 * This is an adaptation of the khr_texture_compression_astc-miptree test.
30 * This test draws miplevels of the compressed textures in a 2D array
31 * according to the MIPLAYOUT_BELOW organization scheme. Each miplevel of
32 * both images are compared for equality after each level is drawn.
35 #include "piglit-util-gl.h"
36 #include "piglit_ktx.h"
39 #define level0_width 160
40 #define level0_height 106
42 #define num_vertices 4
46 static struct piglit_gl_test_config
*piglit_config
;
49 test_miptrees(void* odd
);
51 PIGLIT_GL_TEST_CONFIG_BEGIN
53 piglit_config
= &config
;
54 config
.supports_gl_compat_version
= 11;
55 config
.supports_gl_es_version
= 31;
57 config
.window_width
= 2 * level0_width
;
58 config
.window_height
= level0_height
+ (level0_height
>> 1);
59 config
.window_visual
= PIGLIT_GL_VISUAL_RGB
| PIGLIT_GL_VISUAL_DOUBLE
;
60 static bool is_odd
[2] = {true, false};
62 config
.subtests
= (struct piglit_subtest
[]) {
78 PIGLIT_GL_TEST_CONFIG_END
81 * The \a filename is relative to the current test's source directory.
83 * A new texture is created and returned in \a tex_name.
86 load_texture(const char *dir1
, const char *dir2
,
87 const char *filename
, GLuint
*tex_name
)
89 struct piglit_ktx
*ktx
;
90 const struct piglit_ktx_info
*info
;
94 piglit_join_paths(filepath
, sizeof(filepath
), 7,
98 "khr_texture_compression_astc",
103 ktx
= piglit_ktx_read_file(filepath
);
105 piglit_report_result(PIGLIT_FAIL
);
108 info
= piglit_ktx_get_info(ktx
);
109 assert(info
->num_miplevels
== num_levels
);
110 assert((info
->target
== GL_TEXTURE_2D_ARRAY
) || (info
->target
== GL_TEXTURE_2D
));
111 assert(info
->pixel_width
== level0_width
);
112 assert(info
->pixel_height
== level0_height
);
115 ok
= piglit_ktx_load_texture(ktx
, tex_name
, NULL
);
117 piglit_report_result(PIGLIT_FAIL
);
119 piglit_ktx_destroy(ktx
);
122 /** Compares the compresseed texture against the decompressed texture */
123 bool draw_compare_levels(GLint index
,
124 GLint level_pixel_size_loc
, GLint pixel_offset_loc
,
125 GLuint compressed_tex
)
132 glBindTexture(GL_TEXTURE_2D_ARRAY
, compressed_tex
);
134 for (; level
< num_levels
; ++level
) {
135 int w
= level0_width
>> level
;
136 int h
= level0_height
>> level
;
137 glUniform2f(level_pixel_size_loc
, (float) w
, (float) h
);
139 /* Draw miplevel of compressed texture 1. */
140 glUniform2f(pixel_offset_loc
, x
, y
);
141 glUniform1i(index
, 0);
142 glDrawArrays(GL_TRIANGLE_FAN
, 0, num_vertices
);
144 /* Draw miplevel of compressed texture 2. */
145 glUniform2f(pixel_offset_loc
, level0_width
+ x
, y
);
146 glUniform1i(index
, 1);
147 glDrawArrays(GL_TRIANGLE_FAN
, 0, num_vertices
);
150 pass
= piglit_probe_rects_equal(x
, y
,
155 piglit_loge("Miplevel %d", level
);
158 /* Update the next miplevel arrangement */
165 piglit_present_results();
170 test_miptrees(void* odd
)
173 int block_dims
= *(bool*)odd
;
175 /* Texture objects. */
176 GLuint compressed_tex
;
178 static const char * tests
[3] = {"hdr", "ldrs", "ldrl"};
179 static const char * block_dim_str
[2] = {"12x12", "5x5"};
182 GLint pixel_offset_loc
= glGetUniformLocation(prog
, "pixel_offset");
183 GLint level_pixel_size_loc
= glGetUniformLocation(prog
,
185 GLint index
= glGetUniformLocation(prog
, "index");
187 /* Generate filename for compressed texture */
189 snprintf(cur_file
, sizeof(cur_file
), "array/waffles-%s.ktx",
190 block_dim_str
[block_dims
]);
192 /* Test each submode */
193 for (; subtest
< ARRAY_SIZE(tests
); ++subtest
) {
195 /* Load texture for current submode and block size */
196 load_texture("compressed/2D", tests
[subtest
], cur_file
,
199 /* Draw and compare each level of the two textures */
200 glClear(GL_COLOR_BUFFER_BIT
);
201 if (!draw_compare_levels(index
, level_pixel_size_loc
,
202 pixel_offset_loc
, compressed_tex
)) {
203 piglit_loge("Mode %s Block %s.", tests
[subtest
],
204 block_dim_str
[block_dims
]);
212 piglit_init(int argc
, char **argv
)
214 static const char vs_source
[] =
217 "uniform vec2 window_pixel_size;\n"
218 "uniform vec2 level_pixel_size;\n"
219 "uniform vec2 pixel_offset;\n"
221 "// vertex is some corner of the unit square [0,1]^2 \n"
223 "out vec2 tex_coord;\n"
227 " vec2 pos = vertex;\n"
228 " pos *= level_pixel_size;\n"
229 " pos += pixel_offset;\n"
230 " pos /= 0.5 * window_pixel_size;\n"
231 " pos -= vec2(1, 1);\n"
232 " gl_Position = vec4(pos.xy, 0.0, 1.0);\n"
234 " tex_coord = vertex;\n"
237 static const char fs_source
[] =
239 "precision highp float;\n"
241 "uniform highp sampler2DArray tex;\n"
242 "uniform int index;\n"
243 "in vec2 tex_coord;\n"
244 "out vec4 fragment_color;\n"
248 " vec4 t = texture(tex, vec3(tex_coord.x, tex_coord.y, index));\n"
249 " fragment_color = vec4(t.rgb, 1.0);\n"
252 /* Vertices to draw a square triangle strip. */
253 static const GLfloat vertices
[2 * num_vertices
] = {
264 piglit_require_extension("GL_KHR_texture_compression_astc_ldr");
266 if (!piglit_is_gles())
267 piglit_require_extension("GL_ARB_ES3_compatibility");
269 glClearColor(0.9098, 0.8314, 0.7843, 1.0);
270 glViewport(0, 0, piglit_width
, piglit_height
);
272 glGenBuffers(1, &vertex_buf
);
273 glBindBuffer(GL_ARRAY_BUFFER
, vertex_buf
);
275 glGenVertexArrays(1, &vao
);
276 glBindVertexArray(vao
);
278 prog
= piglit_build_simple_program(vs_source
, fs_source
);
279 glReleaseShaderCompiler();
282 vertex_loc
= glGetAttribLocation(prog
, "vertex");
283 glEnableVertexAttribArray(vertex_loc
);
284 glVertexAttribPointer(vertex_loc
, 2, GL_FLOAT
, GL_FALSE
, 0, NULL
);
285 glBufferData(GL_ARRAY_BUFFER
, sizeof(vertices
), vertices
,
288 glUniform1i(glGetUniformLocation(prog
, "tex"), 0);
289 glUniform2f(glGetUniformLocation(prog
, "window_pixel_size"),
290 piglit_width
, piglit_height
);
296 return piglit_run_selected_subtests(piglit_config
->subtests
,
297 piglit_config
->selected_subtests
,
298 piglit_config
->num_selected_subtests
,