2 * Copyright (c) 2014 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
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
27 #define TEX_HEIGHT 256
29 #define ZERO_CLEAR_X 10
30 #define ZERO_CLEAR_Y 15
31 #define ZERO_CLEAR_WIDTH 8
32 #define ZERO_CLEAR_HEIGHT 12
34 #define VALUE_CLEAR_X 30
35 #define VALUE_CLEAR_Y 50
36 #define VALUE_CLEAR_WIDTH 9
37 #define VALUE_CLEAR_HEIGHT 13
39 /* Arbitrary values that is big enough for four doubles */
40 static const GLubyte clearValue
[] = {
41 0x1f, 0x1e, 0x1d, 0x1c,
42 0x1b, 0x1a, 0x19, 0x18,
43 0x17, 0x16, 0x15, 0x14,
44 0x13, 0x12, 0x11, 0x10,
45 0x0f, 0x0e, 0x0d, 0x0c,
46 0x0b, 0x0a, 0x09, 0x08,
47 0x07, 0x06, 0x05, 0x04,
48 0x03, 0x02, 0x01, 0x00,
52 create_texture(GLenum internalFormat
,
61 data
= malloc(TEX_WIDTH
* TEX_HEIGHT
* texelSize
);
64 /* Fill the data with increasing bytes */
65 for (i
= 0; i
< TEX_WIDTH
* TEX_HEIGHT
* texelSize
; i
++)
68 glGenTextures(1, &tex
);
69 glBindTexture(GL_TEXTURE_2D
, tex
);
70 glTexImage2D(GL_TEXTURE_2D
,
73 TEX_WIDTH
, TEX_HEIGHT
,
84 clear_texture(GLuint tex
, GLenum format
, GLenum type
, GLsizei texelSize
)
86 /* Clear one region using a NULL (all zeroes) value */
87 glClearTexSubImage(tex
,
99 /* Clear another region to a known value */
100 glClearTexSubImage(tex
,
114 is_value_clear(const GLubyte
*texel
, GLsizei texelSize
)
118 for (i
= 0; i
< texelSize
; i
++)
119 if (texel
[i
] != clearValue
[i
])
126 is_zero_clear(const GLubyte
*texel
, GLsizei texelSize
)
130 for (i
= 0; i
< texelSize
; i
++)
138 check_texels(GLenum format
, GLenum type
, GLsizei texelSize
)
144 data
= malloc(TEX_WIDTH
* TEX_HEIGHT
* texelSize
);
146 glPixelStorei(GL_PACK_ALIGNMENT
, 1);
148 glGetTexImage(GL_TEXTURE_2D
,
155 for (y
= 0; y
< TEX_HEIGHT
; y
++) {
156 for (x
= 0; x
< TEX_WIDTH
; x
++) {
157 if (x
>= VALUE_CLEAR_X
&&
158 x
< VALUE_CLEAR_X
+ VALUE_CLEAR_WIDTH
&&
159 y
>= VALUE_CLEAR_Y
&&
160 y
< VALUE_CLEAR_Y
+ VALUE_CLEAR_HEIGHT
) {
161 if (!is_value_clear(p
, texelSize
))
163 } else if (x
>= ZERO_CLEAR_X
&&
164 x
< ZERO_CLEAR_X
+ ZERO_CLEAR_WIDTH
&&
166 y
< ZERO_CLEAR_Y
+ ZERO_CLEAR_HEIGHT
) {
167 if (!is_zero_clear(p
, texelSize
))
170 for (b
= 0; b
< texelSize
; b
++)
171 if (p
[b
] != ((p
+ b
- data
) & 0xff))
185 test_format(GLenum internalFormat
,
193 /* glClearTexture is either in the GL_ARB_clear_texture
194 * extension or in core in GL 4.4
196 if (piglit_get_gl_version() < 44 &&
197 !piglit_is_extension_supported("GL_ARB_clear_texture")) {
198 printf("OpenGL 4.4 or GL_ARB_clear_texture is required.\n");
199 piglit_report_result(PIGLIT_SKIP
);
202 tex
= create_texture(internalFormat
, format
, type
, texelSize
);
204 if (!piglit_check_gl_error(GL_NO_ERROR
))
207 clear_texture(tex
, format
, type
, texelSize
);
209 if (!piglit_check_gl_error(GL_NO_ERROR
))
212 glBindTexture(GL_TEXTURE_2D
, tex
);
214 pass
= check_texels(format
, type
, texelSize
);
216 glBindTexture(GL_TEXTURE_2D
, 0);
218 glDeleteTextures(1, &tex
);
224 test_formats(const struct format
*formats
,
227 bool overallResult
= true;
231 for (i
= 0; i
< n_formats
; i
++) {
232 const struct format
*format
= formats
+ i
;
234 pass
= test_format(format
->internalFormat
,
239 printf("internalFormat = %s, format = %s, type = %s : %s\n",
240 piglit_get_gl_enum_name(format
->internalFormat
),
241 piglit_get_gl_enum_name(format
->format
),
242 piglit_get_gl_enum_name(format
->type
),
243 pass
? "pass" : "fail");
245 overallResult
&= pass
;
248 return overallResult
;
252 test_invalid_format(GLenum internalFormat
,
253 GLenum texImageFormat
,
255 GLenum clearValueFormat
,
256 GLenum clearValueType
)
258 static const GLubyte dummy_data
[sizeof (float) * 4];
262 glGenTextures(1, &tex
);
263 glBindTexture(GL_TEXTURE_2D
, tex
);
264 glTexImage2D(GL_TEXTURE_2D
,
267 1, 1, /* width/height */
273 pass
&= piglit_check_gl_error(GL_NO_ERROR
);
275 glClearTexImage(tex
, 0, clearValueFormat
, clearValueType
, dummy_data
);
277 pass
&= piglit_check_gl_error(GL_INVALID_OPERATION
);
279 glBindTexture(GL_TEXTURE_2D
, 0);
280 glDeleteTextures(1, &tex
);