glsl: test loop unroll with uint overflow
[piglit.git] / tests / util / piglit-framework-gl.c
blobdd0ab4b0196f45ac80619287fe61e5f9363ef36e
1 /*
2 * Copyright © 2009 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 <assert.h>
25 #include <ctype.h>
26 #include <string.h>
27 #include <stdio.h>
28 #include <stdlib.h>
29 #include <math.h>
31 #include "piglit-util-gl.h"
32 #include "piglit-framework-gl/piglit_gl_framework.h"
34 struct piglit_gl_framework *gl_fw;
36 const char *piglit_binary_name;
37 bool piglit_dump_png = false;
38 bool piglit_use_fbo = false;
39 bool piglit_khr_no_error = false;
40 int piglit_automatic = 0;
41 unsigned piglit_winsys_fbo = 0;
43 int piglit_width;
44 int piglit_height;
46 static void
47 process_args(int *argc, char *argv[], unsigned *force_samples,
48 struct piglit_gl_test_config *config);
50 bool
51 piglit_gl_test_config_override_size(struct piglit_gl_test_config *config)
53 const char *default_size;
54 unsigned int width;
55 unsigned int height;
57 default_size = getenv("PIGLIT_DEFAULT_SIZE");
58 if (!default_size)
59 return false;
61 if (sscanf(default_size, "%ux%u", &width, &height) != 2)
62 return false;
64 if (width == 0 || height == 0)
65 return false;
67 config->window_width = width;
68 config->window_height = height;
69 return true;
72 void
73 piglit_gl_test_config_init(struct piglit_gl_test_config *config)
75 memset(config, 0, sizeof(*config));
77 if (!piglit_gl_test_config_override_size(config)) {
78 /* Default window size. Note: Win8's min window width */
79 /* seems to be 160 pixels. When the window size is */
80 /* unexpectedly resized, tests are marked as "WARN". */
81 /* Let's use a larger default to avoid that. */
82 config->window_width = 160;
83 config->window_height = 160;
86 config->khr_no_error_support = PIGLIT_UNKNOWN_ERROR_STATUS;
89 static void
90 delete_arg(char *argv[], int *argc, int arg)
92 int i;
94 for (i = arg + 1; i < *argc; i++) {
95 argv[i - 1] = argv[i];
97 (*argc)--;
101 * Recognized arguments are removed from @a argv. The updated array
102 * length is returned in @a argc.
104 static void
105 process_args(int *argc, char *argv[], unsigned *force_samples,
106 struct piglit_gl_test_config *config)
108 int j;
110 piglit_binary_name = argv[0];
112 piglit_parse_subtest_args(argc, argv, config->subtests,
113 &config->selected_subtests,
114 &config->num_selected_subtests);
116 for (j = 1; j < *argc; j++) {
117 if (!strcmp(argv[j], "-auto")) {
118 piglit_automatic = 1;
119 delete_arg(argv, argc, j--);
120 } else if (!strcmp(argv[j], "-fbo")) {
121 piglit_use_fbo = true;
122 delete_arg(argv, argc, j--);
123 } else if (!strcmp(argv[j], "-png")) {
124 piglit_dump_png = true;
125 delete_arg(argv, argc, j--);
126 } else if (!strcmp(argv[j], "-rlimit")) {
127 char *ptr;
128 unsigned long lim;
129 int i;
131 j++;
132 if (j >= *argc) {
133 fprintf(stderr,
134 "-rlimit requires an argument\n");
135 piglit_report_result(PIGLIT_FAIL);
138 lim = strtoul(argv[j], &ptr, 0);
139 if (ptr == argv[j]) {
140 fprintf(stderr,
141 "-rlimit requires an argument\n");
142 piglit_report_result(PIGLIT_FAIL);
145 piglit_set_rlimit(lim);
147 /* Remove 2 arguments (hence the 'i - 2') from the
148 * command line.
150 for (i = j + 1; i < *argc; i++) {
151 argv[i - 2] = argv[i];
153 *argc -= 2;
154 j -= 2;
155 } else if (!strncmp(argv[j], "-samples=", 9)) {
156 *force_samples = atoi(argv[j]+9);
157 delete_arg(argv, argc, j--);
158 } else if (!strcmp(argv[j], "-khr_no_error")) {
159 piglit_khr_no_error = true;
160 delete_arg(argv, argc, j--);
161 if (config->khr_no_error_support ==
162 PIGLIT_UNKNOWN_ERROR_STATUS) {
163 fprintf(stderr,
164 "khr_no_error_support unknown "
165 "skipping test!\n");
166 piglit_report_result(PIGLIT_SKIP);
167 } else if (config->khr_no_error_support ==
168 PIGLIT_HAS_ERRORS) {
169 piglit_report_result(PIGLIT_SKIP);
170 } else {
171 assert(config->khr_no_error_support ==
172 PIGLIT_NO_ERRORS);
174 } else if (!strcmp(argv[j], "-compat")) {
175 if (config->supports_gl_es_version) {
176 fprintf(stderr,
177 "-compat isn't allowed with ES tests!\n");
178 piglit_report_result(PIGLIT_FAIL);
180 config->supports_gl_compat_version = 10;
181 config->supports_gl_core_version = 0;
182 puts("The compatibility profile forced.");
183 delete_arg(argv, argc, j--);
188 void
189 piglit_gl_process_args(int *argc, char *argv[],
190 struct piglit_gl_test_config *config)
192 unsigned force_samples = 0;
194 process_args(argc, argv, &force_samples, config);
196 if (force_samples > 1)
197 config->window_samples = force_samples;
201 void
202 piglit_gl_test_run(int argc, char *argv[],
203 const struct piglit_gl_test_config *config)
205 piglit_width = config->window_width;
206 piglit_height = config->window_height;
208 gl_fw = piglit_gl_framework_factory(config);
209 if (gl_fw == NULL) {
210 printf("piglit: error: failed to create "
211 "piglit_gl_framework\n");
212 piglit_report_result(PIGLIT_FAIL);
215 gl_fw->run_test(&gl_fw, argc, argv);
216 assert(false);
219 void
220 piglit_post_redisplay(void)
222 if (gl_fw->post_redisplay)
223 gl_fw->post_redisplay(gl_fw);
226 void
227 piglit_set_keyboard_func(void (*func)(unsigned char key, int x, int y))
229 if (gl_fw->set_keyboard_func)
230 gl_fw->set_keyboard_func(gl_fw, func);
233 void
234 piglit_swap_buffers(void)
236 if (gl_fw->swap_buffers)
237 gl_fw->swap_buffers(gl_fw);
240 void
241 piglit_present_results(void)
243 if (piglit_dump_png) {
244 static char *fileprefix = NULL;
245 static int frame = 0;
246 char *filename;
247 GLenum base_format = GL_RGBA;
248 GLubyte *image;
249 if (fileprefix == NULL) {
250 int i;
251 fileprefix = strdup(piglit_binary_name);
252 fileprefix = basename(fileprefix);
253 /* Strip potentially bad characters */
254 for (i = 0; fileprefix[i]; i++) {
255 if (!isalnum(fileprefix[i]) && fileprefix[i] != '-')
256 fileprefix[i] = '_';
259 image = malloc(4 * piglit_width * piglit_height);
260 glReadPixels(0, 0, piglit_width, piglit_height,
261 base_format, GL_UNSIGNED_BYTE, image);
262 assert(glGetError() == GL_NO_ERROR);
264 (void)!asprintf(&filename, "%s%03d.png", fileprefix, frame++);
266 printf("Writing %s...\n", filename);
267 piglit_write_png(filename, base_format, piglit_width,
268 piglit_height, image, true);
269 free(filename);
270 free(image);
273 if (!piglit_automatic)
274 piglit_swap_buffers();
277 void
278 piglit_set_reshape_func(void (*func)(int w, int h))
280 if (!gl_fw->set_reshape_func)
281 gl_fw->set_reshape_func(gl_fw, func);
284 enum piglit_result
285 piglit_create_dma_buf(unsigned w, unsigned h, unsigned fourcc,
286 const void *src_data, struct piglit_dma_buf **buf)
288 if (!gl_fw->create_dma_buf)
289 return PIGLIT_SKIP;
291 return gl_fw->create_dma_buf(w, h, fourcc, src_data, buf);
294 void
295 piglit_destroy_dma_buf(struct piglit_dma_buf *buf)
297 if (gl_fw->destroy_dma_buf)
298 gl_fw->destroy_dma_buf(buf);
301 size_t
302 piglit_get_selected_tests(const char ***selected_subtests)
304 *selected_subtests = gl_fw->test_config->selected_subtests;
305 return gl_fw->test_config->num_selected_subtests;