1 /*
2  * Copyright (c) 2015 VMware, Inc.
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  * on the rights to use, copy, modify, merge, publish, distribute, sub
8  * license, and/or sell copies of the Software, and to permit persons to whom
9  * the 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,
16  * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
17  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
18  * NON-INFRINGEMENT.  IN NO EVENT SHALL VMWARE AND/OR THEIR SUPPLIERS
19  * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
20  * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
21  * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22  * SOFTWARE.
23  */
24 
25 /**
26  * Test glTexImage2D and glGetTexImage with a variety of combinations of
27  * internal formats, and user-specified formats/types.
28  *
29  * Brian Paul
30  * 31 August 2015
31  */
32 
33 #include "piglit-util-gl.h"
34 
35 PIGLIT_GL_TEST_CONFIG_BEGIN
36 	config.supports_gl_compat_version = 10;
37 	config.window_visual = PIGLIT_GL_VISUAL_RGBA;
38 	config.khr_no_error_support = PIGLIT_NO_ERRORS;
39 PIGLIT_GL_TEST_CONFIG_END
40 
41 
42 static bool
test_format(GLenum intFormat,GLenum format,GLenum type)43 test_format(GLenum intFormat, GLenum format, GLenum type)
44 {
45 	const int width = 8, height = 8;
46 	GLuint tex;
47 	GLubyte *image, *getimage;
48 	bool pass = true;
49 	int i, bytes;
50 
51 	switch (intFormat) {
52 	case GL_ALPHA8I_EXT:
53 	case GL_ALPHA8UI_EXT:
54 		bytes = 1;
55 		break;
56 	case GL_ALPHA16I_EXT:
57 	case GL_ALPHA16UI_EXT:
58 		bytes = 2;
59 		break;
60 	case GL_ALPHA32I_EXT:
61 	case GL_ALPHA32UI_EXT:
62 		bytes = 4;
63 		break;
64 	case GL_RGB8I:
65 	case GL_RGB8UI:
66 		bytes = 3;
67 		break;
68 	case GL_RGBA8I:
69 	case GL_RGBA8UI:
70 		bytes = 4;
71 		break;
72 	case GL_RGB16I:
73 	case GL_RGB16UI:
74 		bytes = 6;
75 		break;
76 	case GL_RGBA16I:
77 	case GL_RGBA16UI:
78 		bytes = 8;
79 		break;
80 	case GL_RGB32I:
81 	case GL_RGB32UI:
82 		bytes = 12;
83 		break;
84 	case GL_RGBA32I:
85 	case GL_RGBA32UI:
86 		bytes = 16;
87 		break;
88 	default:
89 		assert(!"Unexpected format");
90 		bytes = 0;
91 	}
92 
93 	image = malloc(width * height * bytes);
94 	for (i = 0; i < width * height * bytes; i++) {
95 		image[i] = i & 255;
96 	}
97 
98 	glGenTextures(1, &tex);
99 	glBindTexture(GL_TEXTURE_2D, tex);
100 	glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
101 	glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
102 
103 	glTexImage2D(GL_TEXTURE_2D, 0, intFormat, width, height, 0,
104 		     format, type, image);
105 	if (!piglit_check_gl_error(GL_NO_ERROR)) {
106 		pass = false;
107 	}
108 
109 	getimage = malloc(width * height * bytes);
110 
111 	glGetTexImage(GL_TEXTURE_2D, 0, format, type, getimage);
112 	if (!piglit_check_gl_error(GL_NO_ERROR)) {
113 		pass = false;
114 	}
115 
116 	if (memcmp(image, getimage, width * height * bytes) != 0) {
117 		pass = false;
118 	}
119 
120 	if (!pass) {
121 		printf("Fail for intFormat=%s, format=%s, type=%s\n",
122 		       piglit_get_gl_enum_name(intFormat),
123 		       piglit_get_gl_enum_name(format),
124 		       piglit_get_gl_enum_name(type));
125 	}
126 
127 	glDeleteTextures(1, &tex);
128 
129 	return pass;
130 }
131 
132 
133 enum piglit_result
piglit_display(void)134 piglit_display(void)
135 {
136 	/* nothing */
137 	return PIGLIT_SKIP;
138 }
139 
140 
141 void
piglit_init(int argc,char ** argv)142 piglit_init(int argc, char **argv)
143 {
144 	/* These format combinations should all work.  But this list is
145 	 * not exhaustive.
146 	 */
147 	static const GLenum formats[][3] = {
148 		/* 8-bit/channel */
149 		{ GL_ALPHA8UI_EXT, GL_ALPHA_INTEGER, GL_UNSIGNED_BYTE },
150 		{ GL_ALPHA8I_EXT, GL_ALPHA_INTEGER, GL_BYTE },
151 		{ GL_RGB8UI, GL_RGB_INTEGER, GL_UNSIGNED_BYTE },
152 		{ GL_RGB8I, GL_RGB_INTEGER, GL_BYTE },
153 		{ GL_RGBA8UI, GL_RGBA_INTEGER, GL_UNSIGNED_BYTE },
154 		{ GL_RGBA8I, GL_RGBA_INTEGER, GL_BYTE },
155 		{ GL_RGBA8UI, GL_RGBA_INTEGER, GL_UNSIGNED_INT_8_8_8_8 },
156 		{ GL_RGBA8UI, GL_RGBA_INTEGER, GL_UNSIGNED_INT_8_8_8_8_REV },
157 		/* 16-bit */
158 		{ GL_ALPHA16UI_EXT, GL_ALPHA_INTEGER, GL_UNSIGNED_SHORT },
159 		{ GL_ALPHA16I_EXT, GL_ALPHA_INTEGER, GL_SHORT },
160 		{ GL_RGB16UI, GL_RGB_INTEGER, GL_UNSIGNED_SHORT },
161 		{ GL_RGB16I, GL_RGB_INTEGER, GL_SHORT },
162 		{ GL_RGBA16UI, GL_RGBA_INTEGER, GL_UNSIGNED_SHORT },
163 		{ GL_RGBA16I, GL_RGBA_INTEGER, GL_SHORT },
164 		/* 32-bit */
165 		{ GL_ALPHA32UI_EXT, GL_ALPHA_INTEGER, GL_UNSIGNED_INT },
166 		{ GL_ALPHA32I_EXT, GL_ALPHA_INTEGER, GL_INT },
167 		{ GL_RGB32UI, GL_RGB_INTEGER, GL_UNSIGNED_INT },
168 		{ GL_RGB32I, GL_RGB_INTEGER, GL_INT },
169 		{ GL_RGBA32UI, GL_RGBA_INTEGER, GL_UNSIGNED_INT },
170 		{ GL_RGBA32I, GL_RGBA_INTEGER, GL_INT }
171 	};
172 	int i;
173 	bool pass = true;
174 
175 	piglit_require_extension("GL_EXT_texture_integer");
176 
177 	glPixelStorei(GL_PACK_ALIGNMENT, 1);
178 	glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
179 
180 	for (i = 0; i < ARRAY_SIZE(formats); i++) {
181 		pass = test_format(formats[i][0], formats[i][1], formats[i][2])
182 			&& pass;
183 	}
184 
185 	piglit_report_result(pass ? PIGLIT_PASS : PIGLIT_FAIL);
186 }
187