ext_gpu_shader4: add compiler tests for everything
[piglit.git] / tests / spec / arb_clear_texture / common.c
blob3503fb071464fcf06ce09e33c8ff3ded2a93403a
1 /*
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
13 * Software.
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.
24 #include "common.h"
26 #define TEX_WIDTH 64
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,
51 static GLuint
52 create_texture(GLenum internalFormat,
53 GLenum format,
54 GLenum type,
55 GLsizei texelSize)
57 GLubyte *data, *p;
58 GLuint tex;
59 int i;
61 data = malloc(TEX_WIDTH * TEX_HEIGHT * texelSize);
62 p = data;
64 /* Fill the data with increasing bytes */
65 for (i = 0; i < TEX_WIDTH * TEX_HEIGHT * texelSize; i++)
66 *(p++) = i;
68 glGenTextures(1, &tex);
69 glBindTexture(GL_TEXTURE_2D, tex);
70 glTexImage2D(GL_TEXTURE_2D,
71 0, /* level */
72 internalFormat,
73 TEX_WIDTH, TEX_HEIGHT,
74 0, /* border */
75 format, type,
76 data);
78 free(data);
80 return tex;
83 static void
84 clear_texture(GLuint tex, GLenum format, GLenum type, GLsizei texelSize)
86 /* Clear one region using a NULL (all zeroes) value */
87 glClearTexSubImage(tex,
88 0, /* level */
89 ZERO_CLEAR_X,
90 ZERO_CLEAR_Y,
91 0, /* z */
92 ZERO_CLEAR_WIDTH,
93 ZERO_CLEAR_HEIGHT,
94 1, /* depth */
95 format,
96 type,
97 NULL /* value */);
99 /* Clear another region to a known value */
100 glClearTexSubImage(tex,
101 0, /* level */
102 VALUE_CLEAR_X,
103 VALUE_CLEAR_Y,
104 0, /* z */
105 VALUE_CLEAR_WIDTH,
106 VALUE_CLEAR_HEIGHT,
107 1, /* depth */
108 format,
109 type,
110 clearValue);
113 static bool
114 is_value_clear(const GLubyte *texel, GLsizei texelSize)
116 int i;
118 for (i = 0; i < texelSize; i++)
119 if (texel[i] != clearValue[i])
120 return false;
122 return true;
125 static bool
126 is_zero_clear(const GLubyte *texel, GLsizei texelSize)
128 int i;
130 for (i = 0; i < texelSize; i++)
131 if (texel[i] != 0)
132 return false;
134 return true;
137 static bool
138 check_texels(GLenum format, GLenum type, GLsizei texelSize)
140 GLubyte *data, *p;
141 bool success = true;
142 int x, y, b;
144 data = malloc(TEX_WIDTH * TEX_HEIGHT * texelSize);
146 glPixelStorei(GL_PACK_ALIGNMENT, 1);
148 glGetTexImage(GL_TEXTURE_2D,
149 0, /* level */
150 format, type,
151 data);
153 p = data;
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))
162 success = false;
163 } else if (x >= ZERO_CLEAR_X &&
164 x < ZERO_CLEAR_X + ZERO_CLEAR_WIDTH &&
165 y >= ZERO_CLEAR_Y &&
166 y < ZERO_CLEAR_Y + ZERO_CLEAR_HEIGHT) {
167 if (!is_zero_clear(p, texelSize))
168 success = false;
169 } else {
170 for (b = 0; b < texelSize; b++)
171 if (p[b] != ((p + b - data) & 0xff))
172 success = false;
175 p += texelSize;
179 free(data);
181 return success;
184 bool
185 test_format(GLenum internalFormat,
186 GLenum format,
187 GLenum type,
188 GLsizei texelSize)
190 GLuint tex;
191 bool pass;
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))
205 return false;
207 clear_texture(tex, format, type, texelSize);
209 if (!piglit_check_gl_error(GL_NO_ERROR))
210 return false;
212 glBindTexture(GL_TEXTURE_2D, tex);
214 pass = check_texels(format, type, texelSize);
216 glBindTexture(GL_TEXTURE_2D, 0);
218 glDeleteTextures(1, &tex);
220 return pass;
223 bool
224 test_formats(const struct format *formats,
225 int n_formats)
227 bool overallResult = true;
228 bool pass;
229 int i;
231 for (i = 0; i < n_formats; i++) {
232 const struct format *format = formats + i;
234 pass = test_format(format->internalFormat,
235 format->format,
236 format->type,
237 format->texelSize);
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;
251 bool
252 test_invalid_format(GLenum internalFormat,
253 GLenum texImageFormat,
254 GLenum texImageType,
255 GLenum clearValueFormat,
256 GLenum clearValueType)
258 static const GLubyte dummy_data[sizeof (float) * 4];
259 bool pass = true;
260 GLuint tex;
262 glGenTextures(1, &tex);
263 glBindTexture(GL_TEXTURE_2D, tex);
264 glTexImage2D(GL_TEXTURE_2D,
265 0, /* level */
266 internalFormat,
267 1, 1, /* width/height */
268 0, /* border */
269 texImageFormat,
270 texImageType,
271 dummy_data);
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);
282 return pass;