run.c 27.2 KB
Newer Older
1 2 3
/* vim: set expandtab tabstop=4 softtabstop=4 shiftwidth=4: */
/*
 * Copyright © 2014 Intel Corporation
4
 * Copyright © 2015 Advanced Micro Devices, Inc.
5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25
 *
 * Permission is hereby granted, free of charge, to any person obtaining a
 * copy of this software and associated documentation files (the "Software"),
 * to deal in the Software without restriction, including without limitation
 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
 * and/or sell copies of the Software, and to permit persons to whom the
 * Software is furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice (including the next
 * paragraph) shall be included in all copies or substantial portions of the
 * Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
 * DEALINGS IN THE SOFTWARE.
 */

Rob Clark's avatar
Rob Clark committed
26 27 28 29
/* for memmem(). The man page doesn't say __USE_GNU... */
/* also for asprintf() */
#define _GNU_SOURCE

30 31 32 33
#include <time.h>
#include <stdio.h>
#include <fcntl.h>
#include <assert.h>
34
#include <signal.h>
35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71
#include <string.h>
#include <stdlib.h>
#include <unistd.h>
#include <stdbool.h>
#include <sys/mman.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <ftw.h>

#include <epoxy/gl.h>
#include <epoxy/egl.h>
#include <gbm.h>
#include <omp.h>

#define unlikely(x) __builtin_expect(!!(x), 0)

#define ARRAY_SIZE(x) (sizeof(x) / sizeof((x)[0]))

struct context_info {
    char *extension_string;
    int extension_string_len;
    int max_glsl_version;
};

enum shader_type {
    TYPE_CORE,
    TYPE_COMPAT,
    TYPE_VP,
    TYPE_FP,
};

struct shader {
    const char *text;
    int length;
    int type;
};

72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100
static bool
extension_in_string(const char *haystack, const char *needle)
{
    const unsigned needle_len = strlen(needle);

    if (needle_len == 0)
        return false;

    while (true) {
        const char *const s = strstr(haystack, needle);

        if (s == NULL)
            return false;

        if (s[needle_len] == ' ' || s[needle_len] == '\0')
            return true;

        /* strstr found an extension whose name begins with
         * needle, but whose name is not equal to needle.
         * Restart the search at s + needle_len so that we
         * don't just find the same extension again and go
         * into an infinite loop.
         */
        haystack = s + needle_len;
    }

    return false;
}

101 102 103 104
static struct shader *
get_shaders(const struct context_info *core, const struct context_info *compat,
            const char *text, size_t text_size,
            enum shader_type *type, unsigned *num_shaders,
105
            bool *use_separate_shader_objects,
106 107 108
            const char *shader_name)
{
    static const char *req = "[require]";
109
    static const char *gl_req = "\nGL >= ";
110 111 112
    static const char *glsl_req = "\nGLSL >= ";
    static const char *fp_req = "\nGL_ARB_fragment_program";
    static const char *vp_req = "\nGL_ARB_vertex_program";
113
    static const char *sso_req = "\nSSO ENABLED";
114 115 116
    static const char *gs = "geometry shader]\n";
    static const char *fs = "fragment ";
    static const char *vs = "vertex ";
117 118
    static const char *tcs = "tessellation control shader]\n";
    static const char *tes = "tessellation evaluation shader]\n";
119
    static const char *cs = "compute shader]\n";
120 121 122 123 124
    static const char *shder = "shader]\n";
    static const char *program = "program]\n";
    static const char *test = "test]\n";
    const char *end_text = text + text_size;

125 126
    *use_separate_shader_objects = false;

127 128 129
    /* Find the [require] block and parse it first. */
    text = memmem(text, end_text - text, req, strlen(req)) + strlen(req);

130 131 132 133 134
    /* Skip the GL >= x.y line if present. */
    if (memcmp(text, gl_req, strlen(gl_req)) == 0) {
        text += strlen(gl_req) + 3; /* for x.y */
    }

135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178
    if (memcmp(text, glsl_req, strlen(glsl_req)) == 0) {
        text += strlen(glsl_req);
        long major = strtol(text, (char **)&text, 10);
        long minor = strtol(text + 1, (char **)&text, 10);
        long version = major * 100 + minor;

        if (version <= 130) {
            if (unlikely(version > compat->max_glsl_version)) {
                fprintf(stderr, "SKIP: %s requires GLSL %ld\n",
                        shader_name, version);
                return NULL;
            }
            *type = TYPE_COMPAT;
        } else {
            if (unlikely(version > core->max_glsl_version)) {
                fprintf(stderr, "SKIP: %s requires GLSL %ld\n",
                        shader_name, version);
                return NULL;
            }
            *type = TYPE_CORE;
        }
    } else if (memcmp(text, fp_req, strlen(fp_req)) == 0) {
        *type = TYPE_FP;
    } else if (memcmp(text, vp_req, strlen(vp_req)) == 0) {
        *type = TYPE_VP;
    } else {
        fprintf(stderr, "ERROR: Unexpected token in %s\n", shader_name);
        return NULL;
    }

    const struct context_info *info = *type == TYPE_CORE ? core : compat;

    const char *extension_text = text;
    while ((extension_text = memmem(extension_text, end_text - extension_text,
                                    "\nGL_", strlen("\nGL_"))) != NULL) {
        extension_text += 1;
        const char *newline = memchr(extension_text, '\n',
                                     end_text - extension_text);
        if (memmem(info->extension_string, info->extension_string_len,
                   extension_text, newline - extension_text) == NULL) {
            fprintf(stderr, "SKIP: %s requires unavailable extension %.*s\n",
                    shader_name, (int)(newline - extension_text), extension_text);
            return NULL;
        }
179 180 181
        if (memcmp(extension_text, sso_req, strlen(sso_req)) == 0) {
            *use_separate_shader_objects = true;
        }
182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218
    }

    /* Find the shaders. */
    unsigned shader_size = 3;
    struct shader *shader = malloc(shader_size * sizeof(struct shader));
    unsigned i = 0;
    while ((text = memmem(text, end_text - text, "\n[", strlen("\n["))) != NULL) {
        const char *save_text = text;
        text += strlen("\n[");

        if (shader_size == i)
            shader = realloc(shader, ++shader_size * sizeof(struct shader));

        if (memcmp(text, fs, strlen(fs)) == 0) {
            text += strlen(fs);
            if (memcmp(text, shder, strlen(shder)) == 0) {
                shader[i].type = GL_FRAGMENT_SHADER;
                text += strlen(shder);
            } else if (memcmp(text, program, strlen(program)) == 0) {
                shader[i].type = GL_FRAGMENT_PROGRAM_ARB;
                text += strlen(program);
            }
            shader[i].text = text;
        } else if (memcmp(text, vs, strlen(vs)) == 0) {
            text += strlen(vs);
            if (memcmp(text, shder, strlen(shder)) == 0) {
                shader[i].type = GL_VERTEX_SHADER;
                text += strlen(shder);
            } else if (memcmp(text, program, strlen(program)) == 0) {
                shader[i].type = GL_VERTEX_PROGRAM_ARB;
                text += strlen(program);
            }
            shader[i].text = text;
        } else if (memcmp(text, gs, strlen(gs)) == 0) {
            text += strlen(gs);
            shader[i].type = GL_GEOMETRY_SHADER;
            shader[i].text = text;
219 220 221 222 223 224 225 226
        } else if (memcmp(text, tcs, strlen(tcs)) == 0) {
            text += strlen(tcs);
            shader[i].type = GL_TESS_CONTROL_SHADER;
            shader[i].text = text;
        } else if (memcmp(text, tes, strlen(tes)) == 0) {
            text += strlen(tes);
            shader[i].type = GL_TESS_EVALUATION_SHADER;
            shader[i].text = text;
227 228 229 230
        } else if (memcmp(text, cs, strlen(cs)) == 0) {
            text += strlen(cs);
            shader[i].type = GL_COMPUTE_SHADER;
            shader[i].text = text;
231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260
        } else if (memcmp(text, test, strlen(test)) == 0) {
            shader[i - 1].length = save_text + 1 - shader[i - 1].text;
            goto out;
        } else {
            fprintf(stderr, "ERROR: Unexpected token in %s\n", shader_name);
            free(shader);
            return NULL;
        }

        if (i != 0)
            shader[i - 1].length = save_text + 1 - shader[i - 1].text;
        i++;
    }

    shader[i - 1].length = end_text - shader[i - 1].text;

 out:
    *num_shaders = i;
    return shader;
}

static void
callback(GLenum source, GLenum type, GLuint id, GLenum severity, GLsizei length,
         const GLchar *message, const void *userParam)
{
    assert(source == GL_DEBUG_SOURCE_SHADER_COMPILER);
    assert(type == GL_DEBUG_TYPE_OTHER);
    assert(severity == GL_DEBUG_SEVERITY_NOTIFICATION);

    const char *const *shader_name = userParam;
261
    printf("%s - %s\n", *shader_name, message);
262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282
}

static unsigned shader_test_size = 1 << 15; /* next-pow-2(num shaders in db) */
static unsigned shader_test_length;
static struct shader_test {
    char *filename;
    off_t filesize;
} *shader_test;

static int
gather_shader_test(const char *fpath, const struct stat *sb, int typeflag)
{
    static const char *ext = ".shader_test";

    if (strlen(fpath) >= strlen(ext) &&
        memcmp(fpath + strlen(fpath) - strlen(ext), ext, strlen(ext)) == 0) {
        if (unlikely(!S_ISREG(sb->st_mode))) {
            fprintf(stderr, "ERROR: %s is not a regular file\n", fpath);
            return -1;
        }

283
        if (unlikely(shader_test_size <= shader_test_length)) {
284
            shader_test_size *= 2;
Matt Turner's avatar
Matt Turner committed
285
            shader_test = realloc(shader_test, shader_test_size * sizeof(struct shader_test));
286 287 288 289 290 291 292 293 294 295
        }
        shader_test[shader_test_length].filename = malloc(strlen(fpath) + 1);
        memcpy(shader_test[shader_test_length].filename, fpath, strlen(fpath) + 1);
        shader_test[shader_test_length].filesize = sb->st_size;
        shader_test_length++;
    }

    return 0;
}

296 297 298 299 300
const char **current_shader_names;
int max_threads;

#define sigputs(str) write(STDERR_FILENO, str, strlen(str))

301 302
#pragma GCC diagnostic push
#pragma GCC diagnostic ignored "-Wunused-result"
303 304 305
static void
abort_handler(int signo)
{
306 307 308 309 310 311 312 313
    if (current_shader_names) {
        sigputs("\n => CRASHED <= while processing these shaders:\n\n");
        for (int i = 0; i < max_threads; i++) {
            if (current_shader_names[i]) {
                sigputs("    ");
                sigputs(current_shader_names[i]);
                sigputs("\n");
            }
314
        }
315 316
    } else {
       sigputs("\n => CRASHED <= during final teardown.\n");
317 318 319 320
    }
    sigputs("\n");
    _exit(-1);
}
321
#pragma GCC diagnostic pop
322

323 324 325 326 327
struct platform {
    const char *name;
    const char *pci_id;
};

Jason Ekstrand's avatar
Jason Ekstrand committed
328
const struct platform platforms[] = {
329 330 331 332 333 334 335 336
    "i965", "0x2A02",
    "g4x",  "0x2A42",
    "ilk",  "0x0042",
    "snb",  "0x0126",
    "ivb",  "0x016a",
    "hsw",  "0x0D2E",
    "byt",  "0x0F33",
    "bdw",  "0x162E",
Matt Turner's avatar
Matt Turner committed
337
    "skl",  "0x191D",
338 339
};

340 341 342
void print_usage(const char *prog_name)
{
    fprintf(stderr,
343
            "Usage: %s [-d <device>] [-j <max_threads>] [-o <driver>] [-p <platform>] <directories and *.shader_test files>\n",
344 345 346
            prog_name);
}

Rob Clark's avatar
Rob Clark committed
347 348 349 350 351 352 353 354 355 356 357 358 359
static void addenv(const char *name, const char *value)
{
    const char *orig = getenv(name);
    if (orig) {
        char *newval;
        asprintf(&newval, "%s,%s", orig, value);
        setenv(name, newval, 1);
        free(newval);
    } else {
        setenv(name, value, 1);
    }
}

360 361 362 363 364 365 366 367 368 369
static int
get_glsl_version(void)
{
    const char *ver = glGetString(GL_SHADING_LANGUAGE_VERSION);
    unsigned major = 0, minor = 0;

    sscanf(ver, "%u.%u", &major, &minor);
    return major * 100 + minor;
}

370 371 372
int
main(int argc, char **argv)
{
373 374
    char device_path[64];
    int device_id = 0;
375 376
    int opt;

377 378
    max_threads = omp_get_max_threads();

379
    while ((opt = getopt(argc, argv, "d:j:o:p:")) != -1) {
380
        switch(opt) {
381 382 383 384 385 386 387 388 389 390
        case 'd': {
            char *endptr;

            device_id = strtol(optarg, &endptr, 10);
            if (endptr == optarg) {
                fprintf(stderr, "Invalid device ID.\n");
                return -1;
            }
            break;
        }
391 392 393 394
        case 'o':
            printf("### Overriding driver for %s ###\n", optarg);
            setenv("MESA_LOADER_DRIVER_OVERRIDE", optarg, 1);
            break;
395 396 397 398 399 400 401
        case 'p': {
            const struct platform *platform = NULL;
            for (unsigned i = 0; i < ARRAY_SIZE(platforms); i++) {
                if (strcmp(optarg, platforms[i].name) == 0) {
                    platform = platforms + i;
                    break;
                }
402 403
            }

404 405 406 407 408 409 410 411 412 413 414 415
            if (platform == NULL) {
                fprintf(stderr, "Invalid platform.\nValid platforms are:");
                for (unsigned i = 0; i < ARRAY_SIZE(platforms); i++)
                    fprintf(stderr, " %s", platforms[i].name);
                fprintf(stderr, "\n");
                return -1;
            }

            printf("### Compiling for %s ###\n", platform->name);
            setenv("INTEL_DEVID_OVERRIDE", platform->pci_id, 1);
            break;
        }
416 417
        case 'j':
            max_threads = atoi(optarg);
418
            break;
419 420 421
        default:
            fprintf(stderr, "Unknown option: %x\n", opt);
            print_usage(argv[0]);
422 423 424 425
            return -1;
        }
    }

426 427 428
    if (unlikely(optind >= argc)) {
        fprintf(stderr, "No directories specified\n");
        print_usage(argv[0]);
429 430 431 432 433
        return -1;
    }

    setenv("allow_glsl_extension_directive_midshader", "true", 1);
    setenv("shader_precompile", "true", 1);
434
    setenv("MESA_GLSL_CACHE_DISABLE", "true", 1);
435
    setenv("GALLIUM_THREAD", "0", 1);
Rob Clark's avatar
Rob Clark committed
436
    addenv("ST_DEBUG", "precompile");
437
    addenv("R600_DEBUG", "precompile");
Rob Clark's avatar
Rob Clark committed
438
    addenv("FD_MESA_DEBUG", "shaderdb");
439

440 441 442 443 444 445 446
    const char *client_extensions = eglQueryString(EGL_NO_DISPLAY,
                                                   EGL_EXTENSIONS);
    if (!client_extensions) {
        fprintf(stderr, "ERROR: Missing EGL_EXT_client_extensions\n");
        return -1;
    }

447
    if (!extension_in_string(client_extensions, "EGL_MESA_platform_gbm")) {
448 449 450 451
        fprintf(stderr, "ERROR: Missing EGL_MESA_platform_gbm\n");
        return -1;
    }

452 453
    int ret = 0;

454 455 456 457
    snprintf(device_path, sizeof(device_path),
             "/dev/dri/renderD%d", device_id + 128);

    int fd = open(device_path, O_RDWR);
458
    if (unlikely(fd < 0)) {
459
        fprintf(stderr, "ERROR: Couldn't open %s\n", device_path);
460 461 462 463 464 465 466 467 468 469
        return -1;
    }

    struct gbm_device *gbm = gbm_create_device(fd);
    if (unlikely(gbm == NULL)) {
        fprintf(stderr, "ERROR: Couldn't create gbm device\n");
        ret = -1;
        goto close_fd;
    }

470 471
    EGLDisplay egl_dpy = eglGetPlatformDisplayEXT(EGL_PLATFORM_GBM_MESA,
                                                  gbm, NULL);
472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487
    if (unlikely(egl_dpy == EGL_NO_DISPLAY)) {
        fprintf(stderr, "ERROR: eglGetDisplay() failed\n");
        ret = -1;
        goto destroy_gbm_device;
    }

    if (unlikely(!eglInitialize(egl_dpy, NULL, NULL))) {
        fprintf(stderr, "ERROR: eglInitialize() failed\n");
        ret = -1;
        goto destroy_gbm_device;
    }

    static const char *egl_extension[] = {
            "EGL_KHR_create_context",
            "EGL_KHR_surfaceless_context"
    };
488
    const char *egl_extension_string = eglQueryString(egl_dpy, EGL_EXTENSIONS);
489
    for (int i = 0; i < ARRAY_SIZE(egl_extension); i++) {
490
        if (!extension_in_string(egl_extension_string, egl_extension[i])) {
491
            fprintf(stderr, "ERROR: Missing %s\n", egl_extension[i]);
492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525
            ret = -1;
            goto egl_terminate;
        }
    }

    static const EGLint config_attribs[] = {
        EGL_RENDERABLE_TYPE, EGL_OPENGL_BIT,
        EGL_NONE
    };
    EGLConfig cfg;
    EGLint count;

    if (!eglChooseConfig(egl_dpy, config_attribs, &cfg, 1, &count) ||
        count == 0) {
        fprintf(stderr, "ERROR: eglChooseConfig() failed\n");
        ret = -1;
        goto egl_terminate;
    }
    eglBindAPI(EGL_OPENGL_API);

    static struct context_info core = { 0 }, compat = { 0 };

    static const EGLint attribs[] = {
        EGL_CONTEXT_OPENGL_PROFILE_MASK_KHR,
        EGL_CONTEXT_OPENGL_CORE_PROFILE_BIT_KHR,
        EGL_CONTEXT_MAJOR_VERSION_KHR, 3,
        EGL_CONTEXT_MINOR_VERSION_KHR, 2,
        EGL_NONE
    };
    EGLContext core_ctx = eglCreateContext(egl_dpy, cfg, EGL_NO_CONTEXT,
                                           attribs);
    if (core_ctx != EGL_NO_CONTEXT &&
        eglMakeCurrent(egl_dpy, EGL_NO_SURFACE, EGL_NO_SURFACE, core_ctx)) {
        int num_extensions;
526 527
        char *gl_extension_string;

528 529 530 531
        glGetIntegerv(GL_NUM_EXTENSIONS, &num_extensions);

        size_t extension_string_size = num_extensions * 26;
        core.extension_string = malloc(extension_string_size);
532
        gl_extension_string = core.extension_string;
533 534 535 536 537 538 539
        char *end_extension_string = core.extension_string +
                                     extension_string_size;

        for (int i = 0; i < num_extensions; i++) {
            const char *ext = glGetStringi(GL_EXTENSIONS, i);
            size_t len = strlen(ext);

540
            if (unlikely(gl_extension_string + len + 1 >= end_extension_string)) {
541
                extension_string_size *= 2;
542
                size_t extension_string_offset = gl_extension_string -
543
                                                 core.extension_string;
544 545
                core.extension_string = realloc(core.extension_string,
                                                extension_string_size);
546
                gl_extension_string = core.extension_string +
547
                                   extension_string_offset;
548 549 550 551
                end_extension_string = core.extension_string +
                                       extension_string_size;
            }

552 553 554
            memcpy(gl_extension_string, ext, len);
            gl_extension_string[len] = ' ';
            gl_extension_string += len + 1;
555
        }
556 557
        gl_extension_string[-1] = '\0';
        core.extension_string_len = gl_extension_string - 1 -
558 559
                                    core.extension_string;

560
        core.max_glsl_version = get_glsl_version();
561

562
        if (!extension_in_string(core.extension_string, "GL_KHR_debug")) {
563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584
            fprintf(stderr, "ERROR: Missing GL_KHR_debug\n");
            ret = -1;
            goto egl_terminate;
        }
    }

    EGLContext compat_ctx = eglCreateContext(egl_dpy, cfg, EGL_NO_CONTEXT,
                                             &attribs[6]);
    if (compat_ctx == EGL_NO_CONTEXT) {
        fprintf(stderr, "ERROR: eglCreateContext() failed\n");
        ret = -1;
        goto egl_terminate;
    }

    if (!eglMakeCurrent(egl_dpy, EGL_NO_SURFACE, EGL_NO_SURFACE, compat_ctx)) {
        fprintf(stderr, "ERROR: eglMakeCurrent() failed\n");
        ret = -1;
        goto egl_terminate;
    } else {
        compat.extension_string = (char *)glGetString(GL_EXTENSIONS);
        compat.extension_string_len = strlen(compat.extension_string);

585
        compat.max_glsl_version = get_glsl_version();
586

587
        if (!extension_in_string(compat.extension_string, "GL_KHR_debug")) {
588 589 590 591 592 593 594
            fprintf(stderr, "ERROR: Missing GL_KHR_debug\n");
            ret = -1;
            goto egl_terminate;
        }
    }

    shader_test = malloc(shader_test_size * sizeof(struct shader_test));
595
    for (int i = optind; i < argc; i++) {
596 597 598
        ftw(argv[i], gather_shader_test, 100);
    }

599 600 601 602 603 604 605
    current_shader_names = calloc(max_threads, sizeof(const char *));

    if (signal(SIGABRT, abort_handler) == SIG_ERR)
        fprintf(stderr, "WARNING: could not install SIGABRT handler.\n");
    if (signal(SIGSEGV, abort_handler) == SIG_ERR)
        fprintf(stderr, "WARNING: could not install SIGSEGV handler.\n");

606
    #pragma omp parallel if(max_threads > 1 && shader_test_length > max_threads)
607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655
    {
        const char *current_shader_name;
        unsigned shaders_compiled = 0;
        unsigned ctx_switches = 0;
        struct timespec start, end;
        clock_gettime(CLOCK_THREAD_CPUTIME_ID, &start);

        eglBindAPI(EGL_OPENGL_API);

        EGLContext core_ctx = eglCreateContext(egl_dpy, cfg, EGL_NO_CONTEXT,
                                               attribs);
        if (core_ctx != EGL_NO_CONTEXT &&
            eglMakeCurrent(egl_dpy, EGL_NO_SURFACE, EGL_NO_SURFACE, core_ctx)) {
            glEnable(GL_DEBUG_OUTPUT);
            glEnable(GL_DEBUG_OUTPUT_SYNCHRONOUS);
            glDebugMessageControl(GL_DONT_CARE, GL_DONT_CARE, GL_DONT_CARE,
                                  0, NULL, GL_FALSE);
            glDebugMessageControl(GL_DEBUG_SOURCE_SHADER_COMPILER,
                                  GL_DEBUG_TYPE_OTHER,
                                  GL_DEBUG_SEVERITY_NOTIFICATION, 0, NULL,
                                  GL_TRUE);
            glDebugMessageCallback(callback, &current_shader_name);
        }

        EGLContext compat_ctx = eglCreateContext(egl_dpy, cfg, EGL_NO_CONTEXT,
                                                 &attribs[6]);
        if (compat_ctx == EGL_NO_CONTEXT) {
            fprintf(stderr, "ERROR: eglCreateContext() failed\n");
            exit(-1);
        }

        bool ctx_is_core = false;
        if (!eglMakeCurrent(egl_dpy, EGL_NO_SURFACE, EGL_NO_SURFACE,
                            compat_ctx)) {
            fprintf(stderr, "ERROR: eglMakeCurrent() failed\n");
        }

        glEnable(GL_DEBUG_OUTPUT);
        glEnable(GL_DEBUG_OUTPUT_SYNCHRONOUS);
        glDebugMessageControl(GL_DONT_CARE, GL_DONT_CARE, GL_DONT_CARE,
                              0, NULL, GL_FALSE);
        glDebugMessageControl(GL_DEBUG_SOURCE_SHADER_COMPILER,
                              GL_DEBUG_TYPE_OTHER,
                              GL_DEBUG_SEVERITY_NOTIFICATION, 0, NULL, GL_TRUE);
        glDebugMessageCallback(callback, &current_shader_name);

        #pragma omp for schedule(dynamic)
        for (int i = 0; i < shader_test_length; i++) {
            current_shader_name = shader_test[i].filename;
656
            current_shader_names[omp_get_thread_num()] = current_shader_name;
657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677

            int fd = open(current_shader_name, O_RDONLY);
            if (unlikely(fd == -1)) {
                perror("open");
                continue;
            }

            char *text = mmap(NULL, shader_test[i].filesize, PROT_READ,
                              MAP_PRIVATE, fd, 0);
            if (unlikely(text == MAP_FAILED)) {
                perror("mmap");
                continue;
            }

            if (unlikely(close(fd) == -1)) {
                perror("close");
                continue;
            }

            enum shader_type type;
            unsigned num_shaders;
678
            bool use_separate_shader_objects;
679 680 681
            struct shader *shader = get_shaders(&core, &compat,
                                                text, shader_test[i].filesize,
                                                &type, &num_shaders,
682
                                                &use_separate_shader_objects,
683 684 685 686 687 688 689 690 691 692 693 694 695 696 697
                                                current_shader_name);
            if (unlikely(shader == NULL)) {
                continue;
            }

            if (ctx_is_core != (type == TYPE_CORE)) {
                ctx_switches++;
                if (!eglMakeCurrent(egl_dpy, EGL_NO_SURFACE, EGL_NO_SURFACE,
                                    type == TYPE_CORE ? core_ctx : compat_ctx)) {
                    fprintf(stderr, "ERROR: eglMakeCurrent() failed\n");
                    continue;
                }
            }
            ctx_is_core = type == TYPE_CORE;

698 699 700 701 702 703
            /* If there's only one GLSL shader, mark it separable so
             * inputs and outputs aren't eliminated.
             */
            if (num_shaders == 1 && type != TYPE_VP && type != TYPE_FP)
                use_separate_shader_objects = true;

704 705
            if (use_separate_shader_objects) {
                for (unsigned i = 0; i < num_shaders; i++) {
706
                    const char *const_text;
707 708 709 710
                    unsigned size = shader[i].length + 1;
                    /* Using alloca crashes in the GLSL compiler.  */
                    char *text = malloc(size);
                    memset(text, 0, size);
711 712 713 714 715 716

                    /* Make it zero-terminated. */
                    memcpy(text, shader[i].text, shader[i].length);
                    text[shader[i].length] = 0;

                    const_text = text;
717 718 719
                    GLuint prog = glCreateShaderProgramv(shader[i].type, 1,
                                                         &const_text);
                    glDeleteProgram(prog);
720
                    free(text);
721 722
                }
            } else if (type == TYPE_CORE || type == TYPE_COMPAT) {
723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749
                GLuint prog = glCreateProgram();

                for (unsigned i = 0; i < num_shaders; i++) {
                    GLuint s = glCreateShader(shader[i].type);
                    glShaderSource(s, 1, &shader[i].text, &shader[i].length);
                    glCompileShader(s);

                    GLint param;
                    glGetShaderiv(s, GL_COMPILE_STATUS, &param);
                    if (unlikely(!param)) {
                        GLchar log[4096];
                        GLsizei length;
                        glGetShaderInfoLog(s, 4096, &length, log);

                        fprintf(stderr, "ERROR: %s failed to compile:\n%s\n",
                                current_shader_name, log);
                    }
                    glAttachShader(prog, s);
                    glDeleteShader(s);
                }

                glLinkProgram(prog);
                glDeleteProgram(prog);
            } else {
                for (unsigned i = 0; i < num_shaders; i++) {
                    GLuint prog;
                    glGenProgramsARB(1, &prog);
750
                    glBindProgramARB(shader[i].type, prog);
751 752 753 754 755 756 757 758 759 760 761
                    glProgramStringARB(shader[i].type, GL_PROGRAM_FORMAT_ASCII_ARB,
                                       shader[i].length, shader[i].text);
                    glDeleteProgramsARB(1, &prog);
                    if (glGetError() == GL_INVALID_OPERATION) {
                        fprintf(stderr, "ERROR: %s failed to compile\n",
                                current_shader_name);
                    }
                }
            }
            shaders_compiled += num_shaders;

762 763
            current_shader_names[omp_get_thread_num()] = NULL;

764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784
            free(shader);
            free(shader_test[i].filename);

            if (unlikely(munmap(text, shader_test[i].filesize) == -1)) {
                perror("munmap");
                continue;
            }
        }

        eglDestroyContext(egl_dpy, compat_ctx);
        eglDestroyContext(egl_dpy, core_ctx);
        eglReleaseThread();

        clock_gettime(CLOCK_THREAD_CPUTIME_ID, &end);
        printf("Thread %d took %.2lf seconds and compiled %u shaders "
               "(not including SIMD16) with %u GL context switches\n",
               omp_get_thread_num(),
               (end.tv_sec - start.tv_sec) + 10e-9 * (end.tv_nsec - start.tv_nsec),
               shaders_compiled, ctx_switches);
    }

785
    free(current_shader_names);
786 787
    free(shader_test);
    free(core.extension_string);
788
    current_shader_names = NULL;
789 790 791 792 793 794 795 796 797 798

 egl_terminate:
    eglTerminate(egl_dpy);
 destroy_gbm_device:
    gbm_device_destroy(gbm);
 close_fd:
    close(fd);

    return ret;
}