2 * Copyright (c) 2010 VMware, Inc.
4 * Permission is hereby , 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 DEALINGS
24 * Test GL_EXT_texture_array and GL_MESA_texture_array.
25 * Note that the Mesa extension works with fixed-function fragment
26 * processing whereas the EXT version only works with shaders.
32 #include "piglit-util-gl.h"
34 PIGLIT_GL_TEST_CONFIG_BEGIN
36 config
.supports_gl_compat_version
= 10;
38 config
.window_width
= 700;
39 config
.window_height
= 400;
40 config
.window_visual
= PIGLIT_GL_VISUAL_DOUBLE
| PIGLIT_GL_VISUAL_RGB
;
41 config
.khr_no_error_support
= PIGLIT_NO_ERRORS
;
43 PIGLIT_GL_TEST_CONFIG_END
45 int height
= 100, ybase
= 0;
47 static const char *prog
= "array-texture";
49 static GLboolean have_MESA_texture_array
;
51 static GLuint array_tex_1d
;
52 static GLuint array_tex_2d
;
55 * We'll set each texture slice to a different solid color.
56 * XXX a better test would vary the color within each slice too.
60 static GLfloat colors
[NUM_COLORS
][3] = {
71 static const char *frag_shader_2d_array_text
=
72 "#extension GL_EXT_texture_array : enable \n"
73 "uniform sampler2DArray tex; \n"
76 " gl_FragColor = texture2DArray(tex, gl_TexCoord[0].xyz); \n"
79 static GLuint frag_shader_2d_array
;
80 static GLuint program_2d_array
;
83 static const char *frag_shader_1d_array_text
=
84 "#extension GL_EXT_texture_array : enable \n"
85 "uniform sampler1DArray tex; \n"
88 " gl_FragColor = texture1DArray(tex, gl_TexCoord[0].xy); \n"
91 static GLuint frag_shader_1d_array
;
92 static GLuint program_1d_array
;
100 GLenum err
= glGetError();
102 printf("%s: GL error 0x%x at line %d\n", prog
, err
, line
);
108 make_2d_array_texture(void)
110 GLfloat img
[NUM_COLORS
][64][32][4];
114 for (i
= 0; i
< NUM_COLORS
; i
++) {
115 for (j
= 0; j
< 64; j
++) {
116 for (k
= 0; k
< 32; k
++) {
117 img
[i
][j
][k
][0] = colors
[i
][0];
118 img
[i
][j
][k
][1] = colors
[i
][1];
119 img
[i
][j
][k
][2] = colors
[i
][2];
120 img
[i
][j
][k
][3] = 1.0;
125 glGenTextures(1, &tex
);
127 glBindTexture(GL_TEXTURE_2D_ARRAY_EXT
, tex
);
128 check_error(__LINE__
);
130 glTexImage3D(GL_TEXTURE_2D_ARRAY_EXT
, 0, GL_RGBA
,
131 32, 64, NUM_COLORS
, /* w, h, d */
133 GL_RGBA
, GL_FLOAT
, img
);
135 glTexParameteri(GL_TEXTURE_2D_ARRAY_EXT
, GL_TEXTURE_MIN_FILTER
, GL_NEAREST
);
136 glTexParameteri(GL_TEXTURE_2D_ARRAY_EXT
, GL_TEXTURE_MAG_FILTER
, GL_NEAREST
);
137 check_error(__LINE__
);
144 make_1d_array_texture(void)
146 GLfloat img
[NUM_COLORS
][16][4];
150 for (i
= 0; i
< NUM_COLORS
; i
++) {
151 for (j
= 0; j
< 16; j
++) {
152 img
[i
][j
][0] = colors
[i
][0];
153 img
[i
][j
][1] = colors
[i
][1];
154 img
[i
][j
][2] = colors
[i
][2];
159 glGenTextures(1, &tex
);
161 glBindTexture(GL_TEXTURE_1D_ARRAY_EXT
, tex
);
162 check_error(__LINE__
);
164 glTexImage2D(GL_TEXTURE_1D_ARRAY_EXT
, 0, GL_RGBA
,
165 16, NUM_COLORS
, /* w, depth */
167 GL_RGBA
, GL_FLOAT
, img
);
169 glTexParameteri(GL_TEXTURE_1D_ARRAY_EXT
, GL_TEXTURE_MIN_FILTER
, GL_NEAREST
);
170 glTexParameteri(GL_TEXTURE_1D_ARRAY_EXT
, GL_TEXTURE_MAG_FILTER
, GL_NEAREST
);
171 check_error(__LINE__
);
178 test_2d_array_texture(GLuint tex
)
180 GLboolean pass
, ret
= GL_TRUE
;
182 float width
= piglit_width
/ NUM_COLORS
;
185 glBindTexture(GL_TEXTURE_2D_ARRAY_EXT
, tex
);
186 /* render each image in the array, check its color */
187 for (i
= 0; i
< NUM_COLORS
; i
++) {
188 GLfloat r
= (GLfloat
) i
;
191 glTexCoord3f(0, 0, r
); glVertex2f(x
, ybase
);
192 glTexCoord3f(1, 0, r
); glVertex2f(x
+ width
, ybase
);
193 glTexCoord3f(1, 1, r
); glVertex2f(x
+ width
, ybase
+ height
);
194 glTexCoord3f(0, 1, r
); glVertex2f(x
, ybase
+ height
);
197 pass
= piglit_probe_pixel_rgb(x
+ (width
/ 2), ybase
+ (height
/ 2),
202 printf("%s: failed for 2D image/slice %d\n", prog
, i
);
208 glBindTexture(GL_TEXTURE_2D_ARRAY_EXT
, 0);
215 test_1d_array_texture(GLuint tex
)
217 GLboolean pass
, ret
= GL_TRUE
;
219 float width
= piglit_width
/ NUM_COLORS
;
222 glBindTexture(GL_TEXTURE_1D_ARRAY_EXT
, tex
);
224 /* render each image in the array, check its color */
225 for (i
= 0; i
< NUM_COLORS
; i
++) {
226 GLfloat r
= (GLfloat
) i
;
229 glTexCoord2f(0, r
); glVertex2f(x
, ybase
);
230 glTexCoord2f(1, r
); glVertex2f(x
+ width
, ybase
);
231 glTexCoord2f(1, r
); glVertex2f(x
+ width
, ybase
+ height
);
232 glTexCoord2f(0, r
); glVertex2f(x
, ybase
+ height
);
237 pass
= piglit_probe_pixel_rgb(x
+ (width
/ 2), ybase
+ (height
/ 2),
243 printf("%s: failed for 1D image/slice %d\n", prog
, i
);
249 glBindTexture(GL_TEXTURE_1D_ARRAY_EXT
, 0);
258 GLboolean pass
= GL_TRUE
;
261 if (!frag_shader_2d_array
) {
262 printf("%s: failed to compile 2D fragment shader.\n", prog
);
266 if (!program_2d_array
) {
267 printf("%s: failed to link 2D shader program.\n", prog
);
271 if (!frag_shader_1d_array
) {
272 printf("%s: failed to compile 1D fragment shader.\n", prog
);
276 if (!program_1d_array
) {
277 printf("%s: failed to link 1D shader program.\n", prog
);
282 piglit_ortho_projection(piglit_width
, piglit_height
, GL_FALSE
);
286 * Test 2d array texture with fragment shader
289 glClear(GL_COLOR_BUFFER_BIT
);
290 glUseProgram(program_2d_array
);
291 loc
= glGetUniformLocation(program_2d_array
, "tex");
292 glUniform1i(loc
, 0); /* texture unit p */
293 pass
&= test_2d_array_texture(array_tex_2d
);
299 * Test 2d array texture with fixed function
301 if (have_MESA_texture_array
) {
302 glEnable(GL_TEXTURE_2D_ARRAY_EXT
);
303 check_error(__LINE__
);
304 pass
&= test_2d_array_texture(array_tex_2d
);
305 glDisable(GL_TEXTURE_2D_ARRAY_EXT
);
306 check_error(__LINE__
);
311 * Test 1d array texture with fragment shader
314 glUseProgram(program_1d_array
);
315 loc
= glGetUniformLocation(program_1d_array
, "tex");
316 glUniform1i(loc
, 0); /* texture unit p */
317 pass
&= test_1d_array_texture(array_tex_1d
);
323 * Test 1d array texture with fixed function
325 if (have_MESA_texture_array
) {
326 glEnable(GL_TEXTURE_1D_ARRAY_EXT
);
327 check_error(__LINE__
);
328 pass
&= test_1d_array_texture(array_tex_1d
);
329 glDisable(GL_TEXTURE_1D_ARRAY_EXT
);
330 check_error(__LINE__
);
333 piglit_present_results();
334 return pass
? PIGLIT_PASS
: PIGLIT_FAIL
;
339 piglit_init(int argc
, char **argv
)
341 piglit_require_extension("GL_EXT_texture_array");
343 have_MESA_texture_array
= piglit_is_extension_supported("GL_MESA_texture_array");
345 /* Make shader programs */
346 frag_shader_2d_array
=
347 piglit_compile_shader_text(GL_FRAGMENT_SHADER
,
348 frag_shader_2d_array_text
);
349 check_error(__LINE__
);
351 program_2d_array
= piglit_link_simple_program(0, frag_shader_2d_array
);
352 check_error(__LINE__
);
354 frag_shader_1d_array
=
355 piglit_compile_shader_text(GL_FRAGMENT_SHADER
,
356 frag_shader_1d_array_text
);
357 check_error(__LINE__
);
359 program_1d_array
= piglit_link_simple_program(0, frag_shader_1d_array
);
360 check_error(__LINE__
);
362 /* make array textures */
363 array_tex_2d
= make_2d_array_texture();
364 array_tex_1d
= make_1d_array_texture();