run.c 35.4 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
#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 {
60
    TYPE_NONE,
61 62
    TYPE_CORE,
    TYPE_COMPAT,
Rob Clark's avatar
Rob Clark committed
63
    TYPE_ES,
64 65 66 67 68 69 70 71 72 73
    TYPE_VP,
    TYPE_FP,
};

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

74 75 76 77 78 79
struct binding_list {
    char *name;
    GLint index;
    struct binding_list *prev;
};

80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108
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;
}

109 110
#define SKIP_SPACES(str) while (*(str) == ' ') str++

111 112
static struct shader *
get_shaders(const struct context_info *core, const struct context_info *compat,
Rob Clark's avatar
Rob Clark committed
113
            const struct context_info *es,
114 115
            const char *text, size_t text_size,
            enum shader_type *type, unsigned *num_shaders,
116
            bool *use_separate_shader_objects,
117
            const char *shader_name, struct binding_list *binding)
118 119
{
    static const char *req = "[require]";
120
    static const char *gl_req = "\nGL >= ";
121
    static const char *glsl_req = "\nGLSL >= ";
Rob Clark's avatar
Rob Clark committed
122
    static const char *glsl_es_req = "\nGLSL ES >= ";
123 124
    static const char *fp_req = "\nGL_ARB_fragment_program";
    static const char *vp_req = "\nGL_ARB_vertex_program";
125
    static const char *sso_req = "\nSSO ENABLED";
126
    static const char *binding_req = "\nBindAttribLoc";
127 128 129
    static const char *gs = "geometry shader]\n";
    static const char *fs = "fragment ";
    static const char *vs = "vertex ";
130 131
    static const char *tcs = "tessellation control shader]\n";
    static const char *tes = "tessellation evaluation shader]\n";
132
    static const char *cs = "compute shader]\n";
133 134 135 136 137
    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;

138 139
    *use_separate_shader_objects = false;

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

143 144 145 146 147
    /* 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 */
    }

148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168
    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;
        }
Rob Clark's avatar
Rob Clark committed
169 170 171 172 173 174 175 176 177 178 179 180
    } else if (memcmp(text, glsl_es_req, strlen(glsl_es_req)) == 0) {
        text += strlen(glsl_es_req);
        long major = strtol(text, (char **)&text, 10);
        long minor = strtol(text + 1, (char **)&text, 10);
        long version = major * 100 + minor;

        if (unlikely(version > es->max_glsl_version)) {
            fprintf(stderr, "SKIP: %s requires GLSL ES %ld\n",
                    shader_name, version);
            return NULL;
        }
        *type = TYPE_ES;
181 182 183 184 185 186 187 188 189 190 191 192
    } 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;
193

194 195 196 197 198
    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);
199

200 201 202 203 204 205
        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;
        }
206 207 208
        if (memcmp(extension_text, sso_req, strlen(sso_req)) == 0) {
            *use_separate_shader_objects = true;
        }
209 210
    }

211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 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 261 262 263 264 265 266
    /* process binding */
    struct binding_list *binding_prev = binding = NULL;
    const char *pre_binding_text = text;

    /* binding = NULL if there's no binding required */
    binding = NULL;
    while ((pre_binding_text = memmem(pre_binding_text, end_text - pre_binding_text,
                                      binding_req, strlen(binding_req))) != NULL) {
        pre_binding_text += strlen(binding_req);

        const char *newline = memchr(pre_binding_text, '\n', end_text - pre_binding_text);

        SKIP_SPACES(pre_binding_text);

        char *endword = memchr(pre_binding_text, ' ', newline - pre_binding_text);

        /* if there's no more space in the same line */
        if (!endword) {
            fprintf(stderr, "SKIP: can't find attr index for this binding\n");
            continue;
        }

        char *binding_name = calloc(1, endword - pre_binding_text + 1);

        strncpy(binding_name, pre_binding_text, endword - pre_binding_text);

        pre_binding_text = endword;

        SKIP_SPACES(pre_binding_text);
        if (*pre_binding_text == '\n') {
            fprintf(stderr, "SKIP: can't find attr variable name for this binding\n");
            continue;
        }

        endword = memchr(pre_binding_text, ' ', newline - pre_binding_text);

        if (!endword)
            endword = (char *)newline;

        char *index_string = calloc(1, endword - pre_binding_text + 1);
        strncpy(index_string, pre_binding_text, endword - pre_binding_text);

        struct binding_list *binding_new = malloc(sizeof(struct binding_list));

        binding_new->index = strtol(index_string, NULL, 10);
        binding_new->name = binding_name;
        binding_new->prev = binding_prev;
        binding = binding_prev = binding_new;

        free(index_string);

        fprintf(stdout,
                "LOG: glBindAttribLocation(prog, %d, \"%s\") will be executed before linking\n",
                binding_new->index, binding_new->name);
    }

267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301
    /* 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;
302 303 304 305 306 307 308 309
        } 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;
310 311 312 313
        } else if (memcmp(text, cs, strlen(cs)) == 0) {
            text += strlen(cs);
            shader[i].type = GL_COMPUTE_SHADER;
            shader[i].text = text;
314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343
        } 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;
344
    printf("%s - %s\n", *shader_name, message);
345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365
}

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;
        }

366
        if (unlikely(shader_test_size <= shader_test_length)) {
367
            shader_test_size *= 2;
368
            shader_test = realloc(shader_test, shader_test_size * sizeof(struct shader_test));
369 370 371 372 373 374 375 376 377 378
        }
        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;
}

379 380 381 382 383
const char **current_shader_names;
int max_threads;

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

384 385
#pragma GCC diagnostic push
#pragma GCC diagnostic ignored "-Wunused-result"
386 387 388
static void
abort_handler(int signo)
{
389 390 391 392 393 394 395 396
    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");
            }
397
        }
398 399
    } else {
       sigputs("\n => CRASHED <= during final teardown.\n");
400 401 402 403
    }
    sigputs("\n");
    _exit(-1);
}
404
#pragma GCC diagnostic pop
405

406 407 408 409 410
struct platform {
    const char *name;
    const char *pci_id;
};

Jason Ekstrand's avatar
Jason Ekstrand committed
411
const struct platform platforms[] = {
412 413 414 415 416 417 418 419
    "i965", "0x2A02",
    "g4x",  "0x2A42",
    "ilk",  "0x0042",
    "snb",  "0x0126",
    "ivb",  "0x016a",
    "hsw",  "0x0D2E",
    "byt",  "0x0F33",
    "bdw",  "0x162E",
Matt Turner's avatar
Matt Turner committed
420
    "skl",  "0x191D",
421
    "icl",  "0x8A50",
422 423
};

424 425 426
void print_usage(const char *prog_name)
{
    fprintf(stderr,
427
            "Usage: %s [-d <device>] [-j <max_threads>] [-o <driver>] [-p <pci"
428
            " id or platform name> [-b] <directories and *.shader_test files>\n",
429 430 431
            prog_name);
}

Rob Clark's avatar
Rob Clark committed
432 433 434 435 436 437 438 439 440 441 442 443 444
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);
    }
}

445 446 447
static int
get_glsl_version(void)
{
Rob Clark's avatar
Rob Clark committed
448
    const char *es_prefix = "OpenGL ES GLSL ES ";
449 450 451
    const char *ver = glGetString(GL_SHADING_LANGUAGE_VERSION);
    unsigned major = 0, minor = 0;

Rob Clark's avatar
Rob Clark committed
452 453 454
    if (strstr(ver, es_prefix) == ver)
        ver += strlen(es_prefix);

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

459 460 461 462 463 464 465 466 467 468 469 470 471 472 473
static EGLContext
create_context(EGLDisplay egl_dpy, EGLConfig cfg, enum shader_type type)
{
    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
    };
    static const EGLint es_attribs[] = {
        EGL_CONTEXT_CLIENT_VERSION, 2,
        EGL_NONE
    };
    switch (type) {
474
    case TYPE_CORE: {
475
        eglBindAPI(EGL_OPENGL_API);
476 477 478 479 480 481 482 483 484 485 486 487 488 489
        EGLContext core_ctx =
                eglCreateContext(egl_dpy, cfg, EGL_NO_CONTEXT, attribs);

        if (core_ctx == EGL_NO_CONTEXT) {
            static const EGLint attribs_31[] = {
                EGL_CONTEXT_MAJOR_VERSION_KHR, 3,
                EGL_CONTEXT_MINOR_VERSION_KHR, 1,
                EGL_NONE
            };
            core_ctx = eglCreateContext(egl_dpy, cfg, EGL_NO_CONTEXT, attribs_31);
        }

        return core_ctx;
    }
490 491 492
    case TYPE_COMPAT:
        eglBindAPI(EGL_OPENGL_API);
        return eglCreateContext(egl_dpy, cfg, EGL_NO_CONTEXT, &attribs[6]);
Rob Clark's avatar
Rob Clark committed
493 494 495
    case TYPE_ES:
        eglBindAPI(EGL_OPENGL_ES_API);
        return eglCreateContext(egl_dpy, cfg, EGL_NO_CONTEXT, es_attribs);
496 497 498 499 500
    default:
        return NULL;
    }
}

501 502 503
int
main(int argc, char **argv)
{
504 505
    char device_path[64];
    int device_id = 0;
506
    int opt;
507
    bool generate_prog_bin = 0;
508

509 510
    max_threads = omp_get_max_threads();

511
    while ((opt = getopt(argc, argv, "d:j:o:p:b")) != -1) {
512
        switch(opt) {
513 514 515 516 517 518 519 520 521 522
        case 'd': {
            char *endptr;

            device_id = strtol(optarg, &endptr, 10);
            if (endptr == optarg) {
                fprintf(stderr, "Invalid device ID.\n");
                return -1;
            }
            break;
        }
523 524 525 526
        case 'o':
            printf("### Overriding driver for %s ###\n", optarg);
            setenv("MESA_LOADER_DRIVER_OVERRIDE", optarg, 1);
            break;
527 528 529
        case 'p': {
            const struct platform *platform = NULL;
            for (unsigned i = 0; i < ARRAY_SIZE(platforms); i++) {
530
                if (strcasecmp(optarg, platforms[i].name) == 0) {
531 532 533
                    platform = platforms + i;
                    break;
                }
534 535
            }

536 537 538 539 540
            if (platform) {
                printf("### Compiling for %s(PCI_ID=%s) ###\n", platform->name,
                       platform->pci_id);
                setenv("INTEL_DEVID_OVERRIDE", platform->pci_id, 1);
                break;
541 542
            }

543 544 545 546 547 548 549 550 551 552 553 554 555 556
            /* Also allow a numeric PCI ID */
            if (strtol(optarg, NULL, 0) > 0) {
                setenv("INTEL_DEVID_OVERRIDE", optarg, 1);
                printf("### Compiling for PCI_ID=%s ###\n", optarg);
                break;
            }

            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");
            fprintf(stderr, "Or\nPCI-ID of other supported platform.\n");
            return -1;
557
        }
558 559
        case 'j':
            max_threads = atoi(optarg);
560
            break;
561 562 563
        case 'b':
            generate_prog_bin = 1;
            break;
564 565 566
        default:
            fprintf(stderr, "Unknown option: %x\n", opt);
            print_usage(argv[0]);
567 568 569 570
            return -1;
        }
    }

571 572 573
    if (unlikely(optind >= argc)) {
        fprintf(stderr, "No directories specified\n");
        print_usage(argv[0]);
574 575 576 577
        return -1;
    }

    setenv("allow_glsl_extension_directive_midshader", "true", 1);
578
    setenv("allow_glsl_builtin_variable_redeclaration", "true", 1);
579
    setenv("shader_precompile", "true", 1);
580
    setenv("MESA_GLSL_CACHE_DISABLE", "true", 1);
581
    setenv("GALLIUM_THREAD", "0", 1);
Rob Clark's avatar
Rob Clark committed
582
    addenv("ST_DEBUG", "precompile");
583
    addenv("R600_DEBUG", "precompile");
Rob Clark's avatar
Rob Clark committed
584
    addenv("FD_MESA_DEBUG", "shaderdb");
585
    addenv("V3D_DEBUG", "precompile");
586

587 588 589 590 591 592 593
    const char *client_extensions = eglQueryString(EGL_NO_DISPLAY,
                                                   EGL_EXTENSIONS);
    if (!client_extensions) {
        fprintf(stderr, "ERROR: Missing EGL_EXT_client_extensions\n");
        return -1;
    }

594
    if (!extension_in_string(client_extensions, "EGL_MESA_platform_gbm")) {
595 596 597 598
        fprintf(stderr, "ERROR: Missing EGL_MESA_platform_gbm\n");
        return -1;
    }

599 600
    int ret = 0;

601 602 603 604
    snprintf(device_path, sizeof(device_path),
             "/dev/dri/renderD%d", device_id + 128);

    int fd = open(device_path, O_RDWR);
605
    if (unlikely(fd < 0)) {
606
        fprintf(stderr, "ERROR: Couldn't open %s\n", device_path);
607 608 609 610 611 612 613 614 615 616
        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;
    }

617 618
    EGLDisplay egl_dpy = eglGetPlatformDisplayEXT(EGL_PLATFORM_GBM_MESA,
                                                  gbm, NULL);
619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634
    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"
    };
635
    const char *egl_extension_string = eglQueryString(egl_dpy, EGL_EXTENSIONS);
636
    for (int i = 0; i < ARRAY_SIZE(egl_extension); i++) {
637
        if (!extension_in_string(egl_extension_string, egl_extension[i])) {
638
            fprintf(stderr, "ERROR: Missing %s\n", egl_extension[i]);
639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657
            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;
    }

Rob Clark's avatar
Rob Clark committed
658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674
    static struct context_info core = { 0 }, compat = { 0 }, es = { 0 };

    EGLContext es_ctx = create_context(egl_dpy, cfg, TYPE_ES);
    if (es_ctx != EGL_NO_CONTEXT &&
        eglMakeCurrent(egl_dpy, EGL_NO_SURFACE, EGL_NO_SURFACE, es_ctx)) {

        es.extension_string = (char *)glGetString(GL_EXTENSIONS);
        es.extension_string_len = strlen(es.extension_string);

        es.max_glsl_version = get_glsl_version();

        if (!extension_in_string(es.extension_string, "GL_KHR_debug")) {
            fprintf(stderr, "ERROR: Missing GL_KHR_debug\n");
            ret = -1;
            goto egl_terminate;
        }
    }
675

676 677
    EGLContext core_ctx = create_context(egl_dpy, cfg, TYPE_CORE);

678 679 680
    if (core_ctx != EGL_NO_CONTEXT &&
        eglMakeCurrent(egl_dpy, EGL_NO_SURFACE, EGL_NO_SURFACE, core_ctx)) {
        int num_extensions;
681 682
        char *gl_extension_string;

683 684 685 686
        glGetIntegerv(GL_NUM_EXTENSIONS, &num_extensions);

        size_t extension_string_size = num_extensions * 26;
        core.extension_string = malloc(extension_string_size);
687
        gl_extension_string = core.extension_string;
688 689 690 691 692 693 694
        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);

695
            if (unlikely(gl_extension_string + len + 1 >= end_extension_string)) {
696
                extension_string_size *= 2;
697
                size_t extension_string_offset = gl_extension_string -
698
                                                 core.extension_string;
699 700
                core.extension_string = realloc(core.extension_string,
                                                extension_string_size);
701
                gl_extension_string = core.extension_string +
702
                                   extension_string_offset;
703 704 705 706
                end_extension_string = core.extension_string +
                                       extension_string_size;
            }

707 708 709
            memcpy(gl_extension_string, ext, len);
            gl_extension_string[len] = ' ';
            gl_extension_string += len + 1;
710
        }
711 712
        gl_extension_string[-1] = '\0';
        core.extension_string_len = gl_extension_string - 1 -
713 714
                                    core.extension_string;

715
        core.max_glsl_version = get_glsl_version();
716

717
        if (!extension_in_string(core.extension_string, "GL_KHR_debug")) {
718 719 720 721 722 723
            fprintf(stderr, "ERROR: Missing GL_KHR_debug\n");
            ret = -1;
            goto egl_terminate;
        }
    }

724
    EGLContext compat_ctx = create_context(egl_dpy, cfg, TYPE_COMPAT);
725 726 727 728 729 730 731 732 733 734 735 736 737 738
    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);

739
        compat.max_glsl_version = get_glsl_version();
740

741
        if (!extension_in_string(compat.extension_string, "GL_KHR_debug")) {
742 743 744 745 746 747 748
            fprintf(stderr, "ERROR: Missing GL_KHR_debug\n");
            ret = -1;
            goto egl_terminate;
        }
    }

    shader_test = malloc(shader_test_size * sizeof(struct shader_test));
749
    for (int i = optind; i < argc; i++) {
750 751 752
        ftw(argv[i], gather_shader_test, 100);
    }

753 754 755 756 757 758 759
    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");

760
    #pragma omp parallel if(max_threads > 1 && shader_test_length > max_threads)
761 762 763 764 765 766 767
    {
        const char *current_shader_name;
        unsigned shaders_compiled = 0;
        unsigned ctx_switches = 0;
        struct timespec start, end;
        clock_gettime(CLOCK_THREAD_CPUTIME_ID, &start);

Rob Clark's avatar
Rob Clark committed
768 769 770 771 772 773 774 775 776 777 778 779 780 781
        EGLContext es_ctx = create_context(egl_dpy, cfg, TYPE_ES);
        if (es_ctx != EGL_NO_CONTEXT &&
            eglMakeCurrent(egl_dpy, EGL_NO_SURFACE, EGL_NO_SURFACE, es_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);
        }

782
        EGLContext core_ctx = create_context(egl_dpy, cfg, TYPE_CORE);
783 784 785 786 787 788 789 790 791 792 793 794 795
        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);
        }

796
        EGLContext compat_ctx = create_context(egl_dpy, cfg, TYPE_COMPAT);
797 798 799 800 801
        if (compat_ctx == EGL_NO_CONTEXT) {
            fprintf(stderr, "ERROR: eglCreateContext() failed\n");
            exit(-1);
        }

802
        enum shader_type current_type = TYPE_NONE;
803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819
        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;
820
            current_shader_names[omp_get_thread_num()] = current_shader_name;
821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841

            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;
842
            bool use_separate_shader_objects;
843
            struct binding_list *binding;
Rob Clark's avatar
Rob Clark committed
844
            struct shader *shader = get_shaders(&core, &compat, &es,
845 846
                                                text, shader_test[i].filesize,
                                                &type, &num_shaders,
847
                                                &use_separate_shader_objects,
848
                                                current_shader_name, binding);
849 850 851 852
            if (unlikely(shader == NULL)) {
                continue;
            }

853 854 855
            if (current_type != type) {
                EGLContext ctx;

856
                ctx_switches++;
857 858

                switch (type) {
Rob Clark's avatar
Rob Clark committed
859 860 861 862
                case TYPE_ES:
                    eglBindAPI(EGL_OPENGL_ES_API);
                    ctx = es_ctx;
                    break;
863 864 865 866 867 868 869 870 871 872
                case TYPE_CORE:
                    eglBindAPI(EGL_OPENGL_API);
                    ctx = core_ctx;
                    break;
                default:
                    eglBindAPI(EGL_OPENGL_API);
                    ctx = compat_ctx;
                    break;
                }

873
                if (!eglMakeCurrent(egl_dpy, EGL_NO_SURFACE, EGL_NO_SURFACE,
874
                                    ctx)) {
875 876 877 878
                    fprintf(stderr, "ERROR: eglMakeCurrent() failed\n");
                    continue;
                }
            }
879
            current_type = type;
880

881 882 883 884 885 886
            /* 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;

887
            if (type == TYPE_CORE || type == TYPE_COMPAT || type == TYPE_ES) {
888
                GLuint prog = glCreateProgram();
889
                GLint param;
890

891 892 893
                if (use_separate_shader_objects)
                   glProgramParameteri(prog, GL_PROGRAM_SEPARABLE, GL_TRUE);

894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911
                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);

                    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);
                }

912 913 914 915 916 917 918 919 920
                /* takes care of pre-bindings */
                while (binding != NULL) {
                    struct binding_list *prev = binding->prev;
                    glBindAttribLocation(prog, binding->index, binding->name);
                    free(binding->name);
                    free(binding);
                    binding = prev;
                }

921
                glLinkProgram(prog);
922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964

                glGetProgramiv(prog, GL_LINK_STATUS, &param);
                if (unlikely(!param)) {
                    GLchar log[4096];
                    GLsizei length;
                    glGetProgramInfoLog(prog, sizeof(log), &length, log);

                    fprintf(stderr, "ERROR: failed to link progam:\n%s\n",
                           log);
                } else if (generate_prog_bin) {
                    /* generating shader program binary */
                    char *prog_buf;
                    GLenum format;
                    GLsizei length = 0;
                    FILE *fp;

                    glGetProgramiv(prog, GL_PROGRAM_BINARY_LENGTH, &length);

                    if (glGetError() != GL_NO_ERROR) {
                        fprintf(stderr,
                                "ERROR: failed to generate a program binary "
                                "(invalid program size).\n");
                        continue;
                    }

                    prog_buf = (char *)malloc(length);

                    if (!prog_buf) {
                        fprintf(stderr,
                                "ERROR: failed to generate a program binary "
                                "(malloc failed)\n");
                        continue;
                    }

                    glGetProgramBinary(prog, length, &length, &format, prog_buf);
                    if (glGetError() != GL_NO_ERROR) {
                        fprintf(stderr,
                                "ERROR: failed to generate a program binary "
                                "(GetProgramBinary failed)\n");
                        free(prog_buf);
                        continue;
                    }

965 966
                    char *out_filename = NULL;
                    asprintf(&out_filename, "%s.bin", current_shader_name);
967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982

                    fp = fopen(out_filename, "wb");
                    fprintf(stdout,
                            "\nBinary program has been successfully generated for %s.\n"
                            "\nWriting it to the file.....\n"
                            "===============================================\n"
                            "File Name : %s\nFormat : %d\nSize : %d Byte\n"
                            "===============================================\n\n",
                            current_shader_name, out_filename, format, length);

                    fwrite(prog_buf, sizeof(char), length, fp);
                    fclose(fp);
                    free(out_filename);
                    free(prog_buf);
                }

983 984 985 986 987
                glDeleteProgram(prog);
            } else {
                for (unsigned i = 0; i < num_shaders; i++) {
                    GLuint prog;
                    glGenProgramsARB(1, &prog);
988
                    glBindProgramARB(shader[i].type, prog);
989 990 991 992 993 994 995 996 997 998 999
                    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;

1000 1001
            current_shader_names[omp_get_thread_num()] = NULL;

1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012
            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);
Rob Clark's avatar
Rob Clark committed
1013
        eglDestroyContext(egl_dpy, es_ctx);
1014 1015 1016 1017 1018 1019 1020 1021 1022 1023
        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);
    }

1024
    free(current_shader_names);
1025 1026
    free(shader_test);
    free(core.extension_string);
1027
    current_shader_names = NULL;
1028 1029 1030 1031 1032 1033 1034 1035 1036 1037

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

    return ret;
}