[Piglit] [PATCH 2/7] arb_texture_cube_map_array: basic cubemap test

Brian Paul brianp at vmware.com
Thu Sep 6 07:47:18 PDT 2012


On 09/05/2012 09:34 PM, Dave Airlie wrote:
> This creates a cube map array of 2 cubes, and tests the upload via
> glTexImage3D and rendering of each layer happens correctly.
>
> note it needs to redefine the common cubemap stuff as they are in the
> wrong order for cubemap arrays.
>
> Signed-off-by: Dave Airlie<airlied at redhat.com>
> ---
>   tests/spec/CMakeLists.txt                          |    1 +
>   .../arb_texture_cube_map_array/CMakeLists.gl.txt   |   12 +
>   .../spec/arb_texture_cube_map_array/CMakeLists.txt |    1 +
>   tests/spec/arb_texture_cube_map_array/cubemap.c    |  406 ++++++++++++++++++++
>   4 files changed, 420 insertions(+), 0 deletions(-)
>   create mode 100644 tests/spec/arb_texture_cube_map_array/CMakeLists.gl.txt
>   create mode 100644 tests/spec/arb_texture_cube_map_array/CMakeLists.txt
>   create mode 100644 tests/spec/arb_texture_cube_map_array/cubemap.c
>
> diff --git a/tests/spec/CMakeLists.txt b/tests/spec/CMakeLists.txt
> index cd374d1..d4eea6f 100644
> --- a/tests/spec/CMakeLists.txt
> +++ b/tests/spec/CMakeLists.txt
> @@ -18,6 +18,7 @@ add_subdirectory (arb_uniform_buffer_object)
>   add_subdirectory (ati_draw_buffers)
>   add_subdirectory (arb_texture_buffer_object)
>   add_subdirectory (arb_texture_compression)
> +add_subdirectory (arb_texture_cube_map_array)
>   add_subdirectory (arb_texture_float)
>   add_subdirectory (arb_texture_storage)
>   add_subdirectory (arb_timer_query)
> diff --git a/tests/spec/arb_texture_cube_map_array/CMakeLists.gl.txt b/tests/spec/arb_texture_cube_map_array/CMakeLists.gl.txt
> new file mode 100644
> index 0000000..c8f90f8
> --- /dev/null
> +++ b/tests/spec/arb_texture_cube_map_array/CMakeLists.gl.txt
> @@ -0,0 +1,12 @@
> +include_directories(
> +	${GLEXT_INCLUDE_DIR}
> +	${OPENGL_INCLUDE_PATH}
> +)
> +
> +link_libraries (
> +	piglitutil_${piglit_target_api}
> +	${OPENGL_gl_LIBRARY}
> +	${OPENGL_glu_LIBRARY}
> +)
> +
> +piglit_add_executable (arb_texture_cube_map_array-cubemap cubemap.c)
> diff --git a/tests/spec/arb_texture_cube_map_array/CMakeLists.txt b/tests/spec/arb_texture_cube_map_array/CMakeLists.txt
> new file mode 100644
> index 0000000..144a306
> --- /dev/null
> +++ b/tests/spec/arb_texture_cube_map_array/CMakeLists.txt
> @@ -0,0 +1 @@
> +piglit_include_target_api()
> diff --git a/tests/spec/arb_texture_cube_map_array/cubemap.c b/tests/spec/arb_texture_cube_map_array/cubemap.c
> new file mode 100644
> index 0000000..f18740c
> --- /dev/null
> +++ b/tests/spec/arb_texture_cube_map_array/cubemap.c
> @@ -0,0 +1,406 @@
> +/*
> + * Copyright © 2012 Red Hat
> + *
> + * Permission is hereby granted, free of charge, to any person obtaining a
> + * copy of this software and associated documentation files (the "Software"),
> + * to deal in the Software without restriction, including without limitation
> + * the rights to use, copy, modify, merge, publish, distribute, sublicense,
> + * and/or sell copies of the Software, and to permit persons to whom the
> + * Software is furnished to do so, subject to the following conditions:
> + *
> + * The above copyright notice and this permission notice (including the next
> + * paragraph) shall be included in all copies or substantial portions of the
> + * Software.
> + *
> + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
> + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
> + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
> + * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
> + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
> + * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
> + * IN THE SOFTWARE.
> + *
> + * Authors:
> + *    Dave Airlie

It would be good to have a description of what this test does here.


> + *
> + */
> +
> +#include "piglit-util-gl-common.h"
> +
> +#define PAD		5
> +
> +#define NUM_LAYERS 2
> +
> +PIGLIT_GL_TEST_MAIN(
> +    (64 * 6 + PAD * 9) * 2 /*window_width*/,
> +    400*NUM_LAYERS /*window_height*/,
> +    GLUT_DOUBLE | GLUT_RGB)
> +
> +int max_size;
> +
> +static const char *prog = "arb_texture_cube_map_array-cubemap";
> +
> +static GLfloat colors[][3] = {

Minor nit: I think this and subsequent array declarations can be 
'static const'.


> +	{1.0, 1.0, 1.0},
> +	{1.0, 1.0, 0.0},
> +	{1.0, 0.0, 0.0},
> +	{1.0, 0.0, 1.0},
> +	{0.0, 0.0, 1.0},
> +	{0.0, 1.0, 1.0},
> +	{0.0, 1.0, 0.0},
> +};
> +
> +/* These texture coordinates should have 1 or -1 in the major axis selecting
> + * the face, and a nearly-1-or-negative-1 value in the other two coordinates
> + * which will be used to produce the s,t values used to sample that face's
> + * image.
> + */
> +GLfloat a_cube_face_texcoords[6][4][4] = {
> +	{ /* GL_TEXTURE_CUBE_MAP_POSITIVE_X */
> +		{1.0,  0.99,  0.99, 0.0},
> +		{1.0,  0.99, -0.99, 0.0},
> +		{1.0, -0.99, -0.99, 0.0},
> +		{1.0, -0.99,  0.99, 0.0},
> +	},
> +	{ /* GL_TEXTURE_CUBE_MAP_NEGATIVE_X */
> +		{-1.0,  0.99, -0.99, 0.0},
> +		{-1.0,  0.99,  0.99, 0.0},
> +		{-1.0, -0.99,  0.99, 0.0},
> +		{-1.0, -0.99, -0.99, 0.0},
> +	},
> +	{ /* GL_TEXTURE_CUBE_MAP_POSITIVE_Y */
> +		{-0.99, 1.0, -0.99, 0.0},
> +		{ 0.99, 1.0, -0.99, 0.0},
> +		{ 0.99, 1.0,  0.99, 0.0},
> +		{-0.99, 1.0,  0.99, 0.0},
> +	},
> +	{ /* GL_TEXTURE_CUBE_MAP_NEGATIVE_Y */
> +		{-0.99, -1.0,  0.99, 0.0},
> +		{-0.99, -1.0, -0.99, 0.0},
> +		{ 0.99, -1.0, -0.99, 0.0},
> +		{ 0.99, -1.0,  0.99, 0.0},
> +	},
> +	{ /* GL_TEXTURE_CUBE_MAP_POSITIVE_Z */
> +		{-0.99,  0.99, 1.0, 0.0},
> +		{-0.99, -0.99, 1.0, 0.0},
> +		{ 0.99, -0.99, 1.0, 0.0},
> +		{ 0.99,  0.99, 1.0, 0.0},
> +	},
> +
> +

extra blank lines

> +	{ /* GL_TEXTURE_CUBE_MAP_NEGATIVE_Z */
> +		{ 0.99,  0.99, -1.0, 0.0},
> +		{-0.99,  0.99, -1.0, 0.0},
> +		{-0.99, -0.99, -1.0, 0.0},
> +		{ 0.99, -0.99, -1.0, 0.0},
> +	},
> +};

I wouldn't be surprised if these coords already exist in another 
cubemap-related piglit test.  If so, maybe they can be shared.


> +
> +const char *a_cube_face_names[6] = {
> +	"POSITIVE_X",
> +	"NEGATIVE_X",
> +	"POSITIVE_Y",
> +	"NEGATIVE_Y",
> +	"POSITIVE_Z",
> +	"NEGATIVE_Z",
> +};
> +
> +const GLenum a_cube_face_targets[6] = {
> +	GL_TEXTURE_CUBE_MAP_POSITIVE_X,
> +	GL_TEXTURE_CUBE_MAP_NEGATIVE_X,
> +	GL_TEXTURE_CUBE_MAP_POSITIVE_Y,
> +	GL_TEXTURE_CUBE_MAP_NEGATIVE_Y,
> +	GL_TEXTURE_CUBE_MAP_POSITIVE_Z,
> +	GL_TEXTURE_CUBE_MAP_NEGATIVE_Z,
> +};
> +
> +static void
> +check_error(int line)
> +{
> +   GLenum err = glGetError();
> +   if (err) {
> +      printf("%s: GL error 0x%x at line %d\n", prog, err, line);
> +   }
> +}

Use calls to piglit_check_gl_error(GL_NO_ERROR) instead?


> +
> +static const char *frag_shader =
> + "#extension GL_ARB_texture_cube_map_array : enable\n"
> + "uniform samplerCubeArray tex; \n"
> + "void main()\n"
> + "{\n"
> + " gl_FragColor = texture(tex, gl_TexCoord[0]);\n"
> +  "}\n";
> +
> +static GLuint frag_shader_cube_array;
> +static GLuint program_cube_array;
> +
> +#if defined(_MSC_VER)
> +/**
> + * Find the first bit set in i and return the index set of that bit.
> + */
> +static int
> +ffs(int i)
> +{
> +	int bit;
> +
> +	if (i == 0) {
> +		return 0;
> +	}
> +
> +	for (bit = 1; !(i&  1); bit++) {
> +		i = i>>  1;
> +	}
> +
> +	return bit;
> +}
> +#endif
> +
> +static void
> +set_image(int level, int size, int *color)
> +{
> +	GLfloat *color1;
> +	GLfloat *color2;
> +	GLfloat *tex;
> +	int x, y;
> +	int face;
> +	//	int color = 0;
> +	int face_size;
> +	face_size = size * size * 3;
> +	tex = malloc(NUM_LAYERS * 6 * face_size * sizeof(GLfloat));
> +
> +	for (face = 0; face<  NUM_LAYERS * 6; face++) {
> +		if (face % 6 == 0)
> +			*color = face / 6;
> +		color1 = colors[*color];
> +		color2 = colors[(*color + 1) % ARRAY_SIZE(colors)];
> +		/* Set the texture for this face to one corner being color2 and the
> +		 * rest color1.  If the texture is 1x1, then it's all color1.
> +		 */
> +		for (y = 0; y<  size; y++) {
> +			for (x = 0; x<  size; x++) {
> +				GLfloat *chosen_color;
> +
> +				if (y>= (size / 2) || x>= (size / 2))
> +					chosen_color = color1;
> +				else
> +					chosen_color = color2;
> +
> +				tex[(face * face_size) + (y * size + x) * 3 + 0] = chosen_color[0];
> +				tex[(face * face_size) + (y * size + x) * 3 + 1] = chosen_color[1];
> +				tex[(face * face_size) + (y * size + x) * 3 + 2] = chosen_color[2];
> +			}
> +		}
> +		*color = (*color + 1) % ARRAY_SIZE(colors);
> +	}
> +	glTexImage3D(GL_TEXTURE_CUBE_MAP_ARRAY, level, GL_RGB, size, size, 6 * NUM_LAYERS, 0, GL_RGB, GL_FLOAT, tex);
> +
> +	free(tex);
> +}
> +
> +/**
> + * Tests that the mipmap drawn at (x,y)-(x+size,y+size) has the majority color,
> + * with color+1 in bottom left.
> + */
> +static GLboolean

We're moving toward 'bool' for stuff like this, but not a big deal.


> +test_results(int x, int y, int size, int level, int face, GLboolean mipmapped,
> +	     int color, int layer)
> +{
> +	GLfloat *color1 = colors[color];
> +	GLfloat *color2 = colors[(color + 1) % ARRAY_SIZE(colors)];
> +	GLboolean pass = GL_TRUE;
> +	int x1 = x + size / 4, x2 = x + size * 3 / 4;
> +	int y1 = y + size / 4, y2 = y + size * 3 / 4;
> +
> +	if (size == 1) {
> +		pass = pass&&  piglit_probe_pixel_rgb(x1, y1, color1);
> +	} else {
> +		pass = pass&&  piglit_probe_pixel_rgb(x1, y1, color2);
> +		pass = pass&&  piglit_probe_pixel_rgb(x2, y1, color1);
> +		pass = pass&&  piglit_probe_pixel_rgb(x2, y2, color1);
> +		pass = pass&&  piglit_probe_pixel_rgb(x1, y2, color1);
> +	}
> +
> +	if (!pass) {
> +		int base_size = size * (1<<  level);
> +		printf("Cube map failed at size %dx%d, level %d (%dx%d), face %s%s\n",
> +		       base_size, base_size, level, size, size,
> +		       a_cube_face_names[face],
> +		       mipmapped ? ", mipmapped" : "");
> +	}
> +
> +	return pass;
> +}
> +
> +static GLboolean
> +draw_at_size(int size, int x_offset, int y_offset,
> +	     GLboolean mipmapped)
> +{
> +	GLfloat row_y = PAD + y_offset;
> +	int dim, face;
> +	int color = 0, level = 0;
> +	GLuint texname;
> +	GLboolean pass = GL_TRUE;
> +	GLint loc;
> +	
> +	glUseProgram(program_cube_array);
> +	loc = glGetUniformLocation(program_cube_array, "tex");
> +	glUniform1i(loc, 0); /* texture unit p */
> +
> +	/* Create the texture. */
> +	glGenTextures(1,&texname);
> +	glBindTexture(GL_TEXTURE_CUBE_MAP_ARRAY_ARB, texname);
> +
> +	/* For each face drawing, we want to only see that face's contents at
> +	 * that mipmap level.
> +	 */
> +	if (mipmapped) {
> +		glTexParameteri(GL_TEXTURE_CUBE_MAP_ARRAY_ARB,
> +				GL_TEXTURE_MIN_FILTER,
> +				GL_NEAREST_MIPMAP_NEAREST);
> +	} else {
> +		glTexParameteri(GL_TEXTURE_CUBE_MAP_ARRAY_ARB,
> +				GL_TEXTURE_MIN_FILTER,
> +				GL_NEAREST);
> +	}
> +	glTexParameteri(GL_TEXTURE_CUBE_MAP_ARRAY_ARB,
> +			GL_TEXTURE_MAG_FILTER, GL_NEAREST);
> +	glTexParameteri(GL_TEXTURE_CUBE_MAP_ARRAY_ARB,
> +			GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
> +	glTexParameteri(GL_TEXTURE_CUBE_MAP_ARRAY_ARB,
> +			GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
> +
> +	/* Fill in faces on each level */
> +	for (dim = size; dim>  0; dim /= 2) {
> +	        set_image(level, dim,&color);
> +		if (!mipmapped)
> +			break;
> +
> +		level++;
> +	}
> +
> +	color = 0;
> +	level = 0;
> +	for (dim = size; dim>  0; dim /= 2) {
> +		GLfloat row_x = PAD + x_offset;
> +
> +		for (face = 0; face<  6 * NUM_LAYERS; face++) {
> +			GLint realface = face % 6;
> +			GLint layer = face / 6;
> +			GLfloat base_x = row_x + realface * (max_size + PAD);
> +			GLfloat base_y = row_y + (400 * layer);
> +
> +			if (realface == 0)
> +			    color = layer;
> +			glBegin(GL_QUADS);
> +
> +			a_cube_face_texcoords[realface][0][3] = layer;
> +			a_cube_face_texcoords[realface][1][3] = layer;
> +			a_cube_face_texcoords[realface][2][3] = layer;
> +			a_cube_face_texcoords[realface][3][3] = layer;
> +
> +			glTexCoord4fv(a_cube_face_texcoords[realface][0]);
> +			glVertex2f(base_x, base_y);
> +
> +			glTexCoord4fv(a_cube_face_texcoords[realface][1]);
> +			glVertex2f(base_x + dim, base_y);
> +
> +			glTexCoord4fv(a_cube_face_texcoords[realface][2]);
> +			glVertex2f(base_x + dim, base_y + dim);
> +
> +			glTexCoord4fv(a_cube_face_texcoords[realface][3]);
> +			glVertex2f(base_x, base_y + dim);
> +
> +			glEnd();
> +
> +			if (dim>  2) {
> +				pass = test_results(base_x, base_y,
> +						    dim, level, realface,
> +						    mipmapped,
> +						    color, layer)&&  pass;
> +			}
> +
> +			color = (color + 1) % ARRAY_SIZE(colors);
> +		}
> +
> +		if (!mipmapped)
> +			break;
> +
> +		row_y += dim + PAD;
> +		level++;
> +	}
> +
> +	glDeleteTextures(1,&texname);
> +
> +	glUseProgram(0);
> +
> +	return pass;
> +}
> +
> +
> +enum piglit_result
> +piglit_display(void)
> +{
> +	int dim;
> +	GLboolean pass = GL_TRUE;
> +	int i = 0, y_offset = 0;
> +	int row_dim = 0;
> +
> +	piglit_ortho_projection(piglit_width, piglit_height, GL_FALSE);
> +
> +	/* Clear background to gray */
> +	glClearColor(0.5, 0.5, 0.5, 1.0);
> +	glClear(GL_COLOR_BUFFER_BIT);
> +
> +	/* First, do each size from MAX_SIZExMAX_SIZE to 1x1 as a
> +	 * single texture level.
> +	 */
> +	y_offset = 0;
> +	for (dim = max_size; dim>  0; dim /= 2) {
> +		pass = draw_at_size(dim, 0, y_offset, GL_FALSE)&&  pass;
> +		y_offset += dim + PAD;
> +	}
> +
> +	/* Next, do each size with mipmaps from MAX_SIZExMAX_SIZE
> +	 * to 1x1.
> +	 */
> +	y_offset = 0;
> +	for (dim = max_size; dim>  0; dim /= 2) {
> +		int x_offset = (i % 2 == 1) ? 0 : piglit_width / 2;
> +
> +		row_dim = (row_dim<  dim) ? dim : row_dim;
> +
> +		pass&= draw_at_size(dim, x_offset, y_offset, GL_TRUE);
> +		if (i % 2 == 0) {
> +			y_offset += row_dim * 2 + (ffs(dim) + 3) * PAD;
> +			row_dim = 0;
> +		}
> +		i++;
> +	}
> +
> +	glutSwapBuffers();
> +
> +	return pass ? PIGLIT_PASS : PIGLIT_FAIL;
> +}
> +
> +void
> +piglit_init(int argc, char **argv)
> +{
> +	int i;
> +
> +	piglit_require_extension("GL_ARB_texture_cube_map_array");
> +
> +	max_size = 64;
> +
> +	for (i = 1; i<  argc; i++) {
> +		if (strcmp(argv[i], "npot") == 0) {
> +			piglit_require_extension("GL_ARB_texture_non_power_of_two");
> +			max_size = 50;
> +			break;
> +		}
> +	}
> +
> +	frag_shader_cube_array = piglit_compile_shader_text(GL_FRAGMENT_SHADER, frag_shader);
> +	check_error(__LINE__);
> +	program_cube_array = piglit_link_simple_program(0, frag_shader_cube_array);
> +	check_error(__LINE__);
> +}

Reviewed-by: Brian Paul <brianp at vmware.com>


More information about the Piglit mailing list