1 /*
2  * Copyright 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  * 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:
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, 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
21  * IN THE SOFTWARE.
22  */
23 
24 /**
25  * This test exercises some subtle format issues for GL_ARB_copy_image.
26  * If a driver supports texture formats which only vary by swizzling (ex: RGBA vs.
27  * BGRA) we may wind up using different hardware texture formats depending
28  * on the user-specified format and type arguments to glTexImage.
29  * When we try to copy between such textures, the copy-sub-image code
30  * must be able to handle the swizzling.
31  *
32  * Brian Paul
33  * 31 August 2015
34  */
35 
36 
37 #include "piglit-util-gl.h"
38 
39 PIGLIT_GL_TEST_CONFIG_BEGIN
40 	config.supports_gl_compat_version = 13;
41 	config.window_visual = PIGLIT_GL_VISUAL_RGB | PIGLIT_GL_VISUAL_DOUBLE;
42 	config.khr_no_error_support = PIGLIT_NO_ERRORS;
43 PIGLIT_GL_TEST_CONFIG_END
44 
45 
46 static bool
test_combination(GLenum intFormat,GLenum srcFormat,GLenum srcType,GLenum dstFormat,GLenum dstType)47 test_combination(GLenum intFormat,
48 		 GLenum srcFormat, GLenum srcType,
49 		 GLenum dstFormat, GLenum dstType)
50 {
51 	const int width = 16, height = 16;
52 	int i;
53 	GLuint textures[2];
54 	GLubyte *image, *getimage;
55 	bool pass = true;
56 	int comps;
57 
58 	switch (srcFormat) {
59 	case GL_RGB:
60 	case GL_BGR:
61 		comps = 3;
62 		break;
63 	case GL_RGBA:
64 	case GL_BGRA:
65 	case GL_RGBA_INTEGER:
66 	case GL_BGRA_INTEGER:
67 		comps = 4;
68 		break;
69 	default:
70 		assert(!"Unexpected format");
71 		comps = 4;
72 	}
73 
74 	getimage = malloc(width * height * comps);
75 
76 	image = malloc(width * height * comps);
77 	if (comps == 4) {
78 		for (i = 0; i < width * height; i++) {
79 			image[i * 4 + 0] = 0xff;
80 			image[i * 4 + 1] = 0x80;
81 			image[i * 4 + 2] = 0x40;
82 			image[i * 4 + 3] = 0x20;
83 		}
84 	}
85 	else {
86 		for (i = 0; i < width * height; i++) {
87 			image[i * 3 + 0] = 0xff;
88 			image[i * 3 + 1] = 0x80;
89 			image[i * 3 + 2] = 0x40;
90 		}
91 	}
92 
93 	glGenTextures(2, textures);
94 
95 	/* setup tex0 */
96 	glBindTexture(GL_TEXTURE_2D, textures[0]);
97 	glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
98 	glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
99 	glTexImage2D(GL_TEXTURE_2D, 0, intFormat, width, height, 0,
100 		     srcFormat, srcType, image);
101 
102 	/* setup tex1 */
103 	glBindTexture(GL_TEXTURE_2D, textures[1]);
104 	glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
105 	glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
106 	glTexImage2D(GL_TEXTURE_2D, 0, intFormat, width, height, 0,
107 		     dstFormat, dstType, NULL);
108 
109 	if (!piglit_check_gl_error(GL_NO_ERROR)) {
110 		/* should be no errors */
111 		pass = false;
112 	}
113 
114 	/* Copy from tex0 to tex1 */
115 	glCopyImageSubData(textures[0], GL_TEXTURE_2D, 0, /* src image */
116 			   0, 0, 0,  /* src offset */
117 			   textures[1], GL_TEXTURE_2D, 0, /* dst image */
118 			   0, 0, 0,  /* dst offset */
119 			   width, height, 1);  /* src size */
120 
121 	/* Readback tex1 */
122 	glGetTexImage(GL_TEXTURE_2D, 0, srcFormat, srcType, getimage);
123 
124 	if (memcmp(image, getimage, width * height * comps) != 0) {
125 		printf("Failure:\n");
126 		printf("  internal tex format=%s\n",
127 		       piglit_get_gl_enum_name(intFormat));
128 		printf("  src tex format=%s type=%s\n",
129 		       piglit_get_gl_enum_name(srcFormat),
130 		       piglit_get_gl_enum_name(srcType));
131 		printf("  dst tex format=%s type=%s\n",
132 		       piglit_get_gl_enum_name(dstFormat),
133 		       piglit_get_gl_enum_name(dstType));
134 		printf("Expected %u %u %u %u\n",
135 		       image[0], image[1], image[2], image[3]);
136 		printf("Found %u %u %u %u\n",
137 		       getimage[0], getimage[1], getimage[2], getimage[3]);
138 		pass = false;
139 	}
140 
141 	glDeleteTextures(2, textures);
142 
143 	free(image);
144 	free(getimage);
145 
146 	return pass;
147 }
148 
149 
150 enum piglit_result
piglit_display(void)151 piglit_display(void)
152 {
153 	/* nothing */
154 	return PIGLIT_SKIP;
155 }
156 
157 
158 void
piglit_init(int argc,char ** argv)159 piglit_init(int argc, char **argv)
160 {
161 	static const GLenum formats[] = {
162 		GL_RGBA,
163 		GL_BGRA
164 	};
165 	static const GLenum rgbFormats[] = {
166 		GL_RGB,
167 		GL_BGR
168 	};
169 	static const GLenum intFormats[] = {
170 		GL_RGBA_INTEGER,
171 		GL_BGRA_INTEGER,
172 	};
173 	static const GLenum types[] = {
174 		GL_UNSIGNED_BYTE,
175 		GL_UNSIGNED_INT_8_8_8_8,
176 		GL_UNSIGNED_INT_8_8_8_8_REV,
177 	};
178 	int sf, st, df, dt;
179 	bool pass = true;
180 
181 	piglit_require_extension("GL_ARB_copy_image");
182 
183 	/* Test all RGBA format/type combinations for the src/dst textures */
184 	for (sf = 0; sf < ARRAY_SIZE(formats); sf++) {
185 		for (df = 0; df < ARRAY_SIZE(formats); df++) {
186 			for (st = 0; st < ARRAY_SIZE(types); st++) {
187 				for (dt = 0; dt < ARRAY_SIZE(types); dt++) {
188 					if (!test_combination(GL_RGBA,
189 							      formats[sf],
190 							      types[st],
191 							      formats[df],
192 							      types[dt])) {
193 						pass = false;
194 					}
195 				}
196 			}
197 		}
198 	}
199 
200 	/* RGB formats */
201 	for (sf = 0; sf < ARRAY_SIZE(rgbFormats); sf++) {
202 		for (df = 0; df < ARRAY_SIZE(rgbFormats); df++) {
203 			if (!test_combination(GL_RGBA,
204 					      rgbFormats[sf],
205 					      GL_UNSIGNED_BYTE,
206 					      rgbFormats[df],
207 					      GL_UNSIGNED_BYTE)) {
208 				pass = false;
209 			}
210 		}
211 	}
212 
213 	/* integer formats */
214         if (piglit_is_extension_supported("GL_EXT_texture_integer")) {
215 		for (sf = 0; sf < ARRAY_SIZE(intFormats); sf++) {
216 			for (df = 0; df < ARRAY_SIZE(intFormats); df++) {
217 				for (st = 0; st < ARRAY_SIZE(types); st++) {
218 					for (dt = 0; dt < ARRAY_SIZE(types); dt++) {
219 						if (!test_combination(GL_RGBA8UI,
220 								      intFormats[sf],
221 								      types[st],
222 								      intFormats[df],
223 								      types[dt])) {
224 							pass = false;
225 						}
226 					}
227 				}
228 			}
229 		}
230 	}
231 
232 	piglit_report_result(pass ? PIGLIT_PASS : PIGLIT_FAIL);
233 }
234