ext_gpu_shader4: add compiler tests for everything
[piglit.git] / tests / spec / khr_texture_compression_astc / khr_compressed_astc-miptree-array.c
blob0cc0ec8192cadf4ac1b17cc3130428ddceefe67e
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 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"
38 #define num_levels 8
39 #define level0_width 160
40 #define level0_height 106
42 #define num_vertices 4
44 static GLuint prog;
46 static struct piglit_gl_test_config *piglit_config;
48 enum piglit_result
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[]) {
64 "5x5 Block Dim",
65 "odd",
66 test_miptrees,
67 &is_odd[0]
70 "12x12 Block Dim",
71 "even",
72 test_miptrees,
73 &is_odd[1]
75 {NULL},
78 PIGLIT_GL_TEST_CONFIG_END
80 /**
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.
85 static void
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;
91 char filepath[4096];
92 bool ok = true;
94 piglit_join_paths(filepath, sizeof(filepath), 7,
95 piglit_source_dir(),
96 "tests",
97 "spec",
98 "khr_texture_compression_astc",
99 dir1,
100 dir2,
101 filename);
103 ktx = piglit_ktx_read_file(filepath);
104 if (ktx == NULL)
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);
114 *tex_name = 0;
115 ok = piglit_ktx_load_texture(ktx, tex_name, NULL);
116 if (!ok)
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)
127 unsigned y = 0;
128 unsigned x = 0;
129 bool pass = true;
130 int level = 0;
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);
149 if (pass) {
150 pass = piglit_probe_rects_equal(x, y,
151 level0_width + x, y,
152 w, h, GL_RGBA);
154 if (!pass)
155 piglit_loge("Miplevel %d", level);
158 /* Update the next miplevel arrangement */
159 if (level == 1)
160 x += w;
161 else
162 y += h;
165 piglit_present_results();
166 return pass;
169 enum piglit_result
170 test_miptrees(void* odd)
172 int subtest = 0;
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,
184 "level_pixel_size");
185 GLint index = glGetUniformLocation(prog, "index");
187 /* Generate filename for compressed texture */
188 char cur_file[50];
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,
197 &compressed_tex);
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]);
205 return PIGLIT_FAIL;
208 return PIGLIT_PASS;
211 void
212 piglit_init(int argc, char **argv)
214 static const char vs_source[] =
215 "#version 300 es\n"
216 "\n"
217 "uniform vec2 window_pixel_size;\n"
218 "uniform vec2 level_pixel_size;\n"
219 "uniform vec2 pixel_offset;\n"
220 "\n"
221 "// vertex is some corner of the unit square [0,1]^2 \n"
222 "in vec2 vertex;\n"
223 "out vec2 tex_coord;\n"
224 "\n"
225 "void main()\n"
226 "{\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"
233 "\n"
234 " tex_coord = vertex;\n"
235 "}\n";
237 static const char fs_source[] =
238 "#version 300 es\n"
239 "precision highp float;\n"
240 "\n"
241 "uniform highp sampler2DArray tex;\n"
242 "uniform int index;\n"
243 "in vec2 tex_coord;\n"
244 "out vec4 fragment_color;\n"
245 "\n"
246 "void main()\n"
247 "{\n"
248 " vec4 t = texture(tex, vec3(tex_coord.x, tex_coord.y, index));\n"
249 " fragment_color = vec4(t.rgb, 1.0);\n"
250 "}\n";
252 /* Vertices to draw a square triangle strip. */
253 static const GLfloat vertices[2 * num_vertices] = {
254 0, 0,
255 1, 0,
256 1, 1,
257 0, 1,
260 GLint vertex_loc;
261 GLuint vertex_buf;
262 GLuint vao;
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();
280 glUseProgram(prog);
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,
286 GL_STATIC_DRAW);
288 glUniform1i(glGetUniformLocation(prog, "tex"), 0);
289 glUniform2f(glGetUniformLocation(prog, "window_pixel_size"),
290 piglit_width, piglit_height);
293 enum piglit_result
294 piglit_display(void)
296 return piglit_run_selected_subtests(piglit_config->subtests,
297 piglit_config->selected_subtests,
298 piglit_config->num_selected_subtests,
299 PIGLIT_SKIP);