Path: blob/21.2-virgl/src/gallium/drivers/radeonsi/glsl_tests/amdgcn_glslc.c
4574 views
/*1* Copyright © 2014 Intel Corporation2* Copyright © 2016 Advanced Micro Devices, Inc.3* All Rights Reserved.4*5* Permission is hereby granted, free of charge, to any person obtaining a6* copy of this software and associated documentation files (the "Software"),7* to deal in the Software without restriction, including without limitation8* the rights to use, copy, modify, merge, publish, distribute, sublicense,9* and/or sell copies of the Software, and to permit persons to whom the10* Software is furnished to do so, subject to the following conditions:11*12* The above copyright notice and this permission notice (including the next13* paragraph) shall be included in all copies or substantial portions of the14* Software.15*16* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR17* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,18* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL19* THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER20* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING21* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER22* DEALINGS IN THE SOFTWARE.23*/2425/**26* This program reads and compiles multiple GLSL shaders from one source file.27* Each shader must begin with the #shader directive. Syntax:28* #shader [vs|tcs|tes|gs|ps|cs] [name]29*30* The shader name is printed, followed by the stderr output from31* glCreateShaderProgramv. (radeonsi prints the shader disassembly there)32*33* The optional parameter -mcpu=[processor] forces radeonsi to compile for34* the specified GPU processor. (e.g. tahiti, bonaire, tonga)35*36* The program doesn't check if the underlying driver is really radeonsi.37* OpenGL 4.3 Core profile is required.38*/3940/* for asprintf() */41#define _GNU_SOURCE4243#include <stdio.h>44#include <fcntl.h>45#include <string.h>46#include <stdlib.h>4748#include <epoxy/gl.h>49#include <epoxy/egl.h>50#include <gbm.h>5152#define unlikely(x) __builtin_expect(!!(x), 0)53#define ARRAY_SIZE(x) (sizeof(x) / sizeof((x)[0]))5455static int fd;56static EGLDisplay egl_dpy;57static EGLContext ctx;5859static void60create_gl_core_context()61{62const char *client_extensions = eglQueryString(EGL_NO_DISPLAY,63EGL_EXTENSIONS);64if (!client_extensions) {65fprintf(stderr, "ERROR: Missing EGL_EXT_client_extensions\n");66exit(1);67}6869if (!strstr(client_extensions, "EGL_MESA_platform_gbm")) {70fprintf(stderr, "ERROR: Missing EGL_MESA_platform_gbm\n");71exit(1);72}7374fd = open("/dev/dri/renderD128", O_RDWR);75if (unlikely(fd < 0)) {76fprintf(stderr, "ERROR: Couldn't open /dev/dri/renderD128\n");77exit(1);78}7980struct gbm_device *gbm = gbm_create_device(fd);81if (unlikely(gbm == NULL)) {82fprintf(stderr, "ERROR: Couldn't create gbm device\n");83exit(1);84}8586egl_dpy = eglGetPlatformDisplayEXT(EGL_PLATFORM_GBM_MESA,87gbm, NULL);88if (unlikely(egl_dpy == EGL_NO_DISPLAY)) {89fprintf(stderr, "ERROR: eglGetDisplay() failed\n");90exit(1);91}9293if (unlikely(!eglInitialize(egl_dpy, NULL, NULL))) {94fprintf(stderr, "ERROR: eglInitialize() failed\n");95exit(1);96}9798static const char *egl_extension[] = {99"EGL_KHR_create_context",100"EGL_KHR_surfaceless_context"101};102const char *extension_string = eglQueryString(egl_dpy, EGL_EXTENSIONS);103for (int i = 0; i < ARRAY_SIZE(egl_extension); i++) {104if (strstr(extension_string, egl_extension[i]) == NULL) {105fprintf(stderr, "ERROR: Missing %s\n", egl_extension[i]);106exit(1);107}108}109110static const EGLint config_attribs[] = {111EGL_RENDERABLE_TYPE, EGL_OPENGL_BIT,112EGL_NONE113};114EGLConfig cfg;115EGLint count;116117if (!eglChooseConfig(egl_dpy, config_attribs, &cfg, 1, &count) ||118count == 0) {119fprintf(stderr, "ERROR: eglChooseConfig() failed\n");120exit(1);121}122eglBindAPI(EGL_OPENGL_API);123124static const EGLint attribs[] = {125EGL_CONTEXT_OPENGL_PROFILE_MASK_KHR,126EGL_CONTEXT_OPENGL_CORE_PROFILE_BIT_KHR,127EGL_CONTEXT_MAJOR_VERSION_KHR, 4,128EGL_CONTEXT_MINOR_VERSION_KHR, 3,129EGL_NONE130};131ctx = eglCreateContext(egl_dpy, cfg, EGL_NO_CONTEXT, attribs);132if (ctx == EGL_NO_CONTEXT) {133fprintf(stderr, "eglCreateContext(GL 3.2) failed.\n");134exit(1);135}136137if (!eglMakeCurrent(egl_dpy, EGL_NO_SURFACE, EGL_NO_SURFACE, ctx)) {138fprintf(stderr, "eglMakeCurrent failed.\n");139exit(1);140}141}142143static char *144read_file(const char *filename)145{146FILE *f = fopen(filename, "r");147if (!f) {148fprintf(stderr, "Can't open file: %s\n", filename);149exit(1);150}151152fseek(f, 0, SEEK_END);153int filesize = ftell(f);154fseek(f, 0, SEEK_SET);155156char *input = (char*)malloc(filesize + 1);157if (!input) {158fprintf(stderr, "malloc failed\n");159exit(1);160}161162if (fread(input, filesize, 1, f) != 1) {163fprintf(stderr, "fread failed\n");164exit(1);165}166fclose(f);167168input[filesize] = 0;169return input;170}171172static void addenv(const char *name, const char *value)173{174const char *orig = getenv(name);175if (orig) {176char *newval;177(void)!asprintf(&newval, "%s,%s", orig, value);178setenv(name, newval, 1);179free(newval);180} else {181setenv(name, value, 1);182}183}184185int186main(int argc, char **argv)187{188const char *filename = NULL;189190for (int i = 1; i < argc; i++) {191if (strstr(argv[i], "-mcpu=") == argv[i]) {192setenv("SI_FORCE_FAMILY", argv[i] + 6, 1);193} else if (filename == NULL) {194filename = argv[i];195} else {196if (strcmp(argv[i], "--help") != 0 && strcmp(argv[i], "-h") != 0)197fprintf(stderr, "Unknown option: %s\n\n", argv[i]);198199fprintf(stderr, "Usage: amdgcn_glslc -mcpu=[chip name] [glsl filename]\n");200return 1;201}202}203204if (filename == NULL) {205fprintf(stderr, "No filename specified.\n");206return 1;207}208209addenv("R600_DEBUG", "precompile,vs,tcs,tes,gs,ps,cs,noir,notgsi");210211create_gl_core_context();212213/* Read the source. */214char *input = read_file(filename);215216/* Comment out lines beginning with ; (FileCheck prefix). */217if (input[0] == ';')218memcpy(input, "//", 2);219220char *s = input;221while (s = strstr(s, "\n;"))222memcpy(s + 1, "//", 2);223224s = input;225while (s && (s = strstr(s, "#shader "))) {226char type_str[16], name[128];227GLenum type;228229/* If #shader is not at the beginning of the line. */230if (s != input && s[-1] != '\n' && s[-1] != 0) {231s = strstr(s, "\n");232continue;233}234235/* Parse the #shader directive. */236if (sscanf(s + 8, "%s %s", type_str, name) != 2) {237fprintf(stderr, "Cannot parse #shader directive.\n");238continue;239}240241if (!strcmp(type_str, "vs"))242type = GL_VERTEX_SHADER;243else if (!strcmp(type_str, "tcs"))244type = GL_TESS_CONTROL_SHADER;245else if (!strcmp(type_str, "tes"))246type = GL_TESS_EVALUATION_SHADER;247else if (!strcmp(type_str, "gs"))248type = GL_GEOMETRY_SHADER;249else if (!strcmp(type_str, "fs"))250type = GL_FRAGMENT_SHADER;251else if (!strcmp(type_str, "cs"))252type = GL_COMPUTE_SHADER;253254/* Go the next line. */255s = strstr(s, "\n");256if (!s)257break;258s++;259260const char *source = s;261262/* Cut the shader source at the end. */263s = strstr(s, "#shader");264if (s && s[-1] == '\n')265s[-1] = 0;266267/* Compile the shader. */268printf("@%s:\n", name);269270/* Redirect stderr to stdout for the compiler. */271FILE *stderr_original = stderr;272stderr = stdout;273GLuint prog = glCreateShaderProgramv(type, 1, &source);274stderr = stderr_original;275276GLint linked;277glGetProgramiv(prog, GL_LINK_STATUS, &linked);278279if (!linked) {280char log[4096];281GLsizei length;282283glGetProgramInfoLog(prog, sizeof(log), &length, log);284fprintf(stderr, "ERROR: Compile failure:\n\n%s\n%s\n",285source, log);286return 1;287}288289glDeleteProgram(prog);290}291return 0;292}293294295