xref: /aosp_15_r20/external/mesa3d/src/gallium/drivers/radeonsi/glsl_tests/amdgcn_glslc.c (revision 6104692788411f58d303aa86923a9ff6ecaded22)
1 /*
2  * Copyright © 2014 Intel Corporation
3  * Copyright © 2016 Advanced Micro Devices, Inc.
4  *
5  * SPDX-License-Identifier: MIT
6  */
7 
8 /**
9  * This program reads and compiles multiple GLSL shaders from one source file.
10  * Each shader must begin with the #shader directive. Syntax:
11  *     #shader [vs|tcs|tes|gs|ps|cs] [name]
12  *
13  * The shader name is printed, followed by the stderr output from
14  * glCreateShaderProgramv. (radeonsi prints the shader disassembly there)
15  *
16  * The optional parameter -mcpu=[processor] forces radeonsi to compile for
17  * the specified GPU processor. (e.g. tahiti, bonaire, tonga)
18  *
19  * The program doesn't check if the underlying driver is really radeonsi.
20  * OpenGL 4.3 Core profile is required.
21  */
22 
23 /* for asprintf() */
24 #define _GNU_SOURCE
25 
26 #include <stdio.h>
27 #include <fcntl.h>
28 #include <string.h>
29 #include <stdlib.h>
30 
31 #include <epoxy/gl.h>
32 #include <epoxy/egl.h>
33 #include <gbm.h>
34 
35 #define unlikely(x) __builtin_expect(!!(x), 0)
36 #define ARRAY_SIZE(x) (sizeof(x) / sizeof((x)[0]))
37 
38 static int fd;
39 static EGLDisplay egl_dpy;
40 static EGLContext ctx;
41 
42 static void
create_gl_core_context()43 create_gl_core_context()
44 {
45     const char *client_extensions = eglQueryString(EGL_NO_DISPLAY,
46                                                    EGL_EXTENSIONS);
47     if (!client_extensions) {
48         fprintf(stderr, "ERROR: Missing EGL_EXT_client_extensions\n");
49         exit(1);
50     }
51 
52     if (!strstr(client_extensions, "EGL_MESA_platform_gbm")) {
53         fprintf(stderr, "ERROR: Missing EGL_MESA_platform_gbm\n");
54         exit(1);
55     }
56 
57     fd = open("/dev/dri/renderD128", O_RDWR);
58     if (unlikely(fd < 0)) {
59         fprintf(stderr, "ERROR: Couldn't open /dev/dri/renderD128\n");
60         exit(1);
61     }
62 
63     struct gbm_device *gbm = gbm_create_device(fd);
64     if (unlikely(gbm == NULL)) {
65         fprintf(stderr, "ERROR: Couldn't create gbm device\n");
66         exit(1);
67     }
68 
69     egl_dpy = eglGetPlatformDisplayEXT(EGL_PLATFORM_GBM_MESA,
70                                        gbm, NULL);
71     if (unlikely(egl_dpy == EGL_NO_DISPLAY)) {
72         fprintf(stderr, "ERROR: eglGetDisplay() failed\n");
73         exit(1);
74     }
75 
76     if (unlikely(!eglInitialize(egl_dpy, NULL, NULL))) {
77         fprintf(stderr, "ERROR: eglInitialize() failed\n");
78         exit(1);
79     }
80 
81     static const char *egl_extension[] = {
82             "EGL_KHR_create_context",
83             "EGL_KHR_surfaceless_context"
84     };
85     const char *extension_string = eglQueryString(egl_dpy, EGL_EXTENSIONS);
86     for (int i = 0; i < ARRAY_SIZE(egl_extension); i++) {
87         if (strstr(extension_string, egl_extension[i]) == NULL) {
88             fprintf(stderr, "ERROR: Missing %s\n", egl_extension[i]);
89             exit(1);
90         }
91     }
92 
93     static const EGLint config_attribs[] = {
94         EGL_RENDERABLE_TYPE, EGL_OPENGL_BIT,
95         EGL_NONE
96     };
97     EGLConfig cfg;
98     EGLint count;
99 
100     if (!eglChooseConfig(egl_dpy, config_attribs, &cfg, 1, &count) ||
101         count == 0) {
102         fprintf(stderr, "ERROR: eglChooseConfig() failed\n");
103         exit(1);
104     }
105     eglBindAPI(EGL_OPENGL_API);
106 
107     static const EGLint attribs[] = {
108         EGL_CONTEXT_OPENGL_PROFILE_MASK_KHR,
109         EGL_CONTEXT_OPENGL_CORE_PROFILE_BIT_KHR,
110         EGL_CONTEXT_MAJOR_VERSION_KHR, 4,
111         EGL_CONTEXT_MINOR_VERSION_KHR, 3,
112         EGL_NONE
113     };
114     ctx = eglCreateContext(egl_dpy, cfg, EGL_NO_CONTEXT, attribs);
115     if (ctx == EGL_NO_CONTEXT) {
116         fprintf(stderr, "eglCreateContext(GL 3.2) failed.\n");
117         exit(1);
118     }
119 
120     if (!eglMakeCurrent(egl_dpy, EGL_NO_SURFACE, EGL_NO_SURFACE, ctx)) {
121         fprintf(stderr, "eglMakeCurrent failed.\n");
122         exit(1);
123     }
124 }
125 
126 static char *
read_file(const char * filename)127 read_file(const char *filename)
128 {
129     FILE *f = fopen(filename, "r");
130     if (!f) {
131         fprintf(stderr, "Can't open file: %s\n", filename);
132         exit(1);
133     }
134 
135     fseek(f, 0, SEEK_END);
136     int filesize = ftell(f);
137     fseek(f, 0, SEEK_SET);
138 
139     char *input = (char*)malloc(filesize + 1);
140     if (!input) {
141         fprintf(stderr, "malloc failed\n");
142         exit(1);
143     }
144 
145     if (fread(input, filesize, 1, f) != 1) {
146         fprintf(stderr, "fread failed\n");
147         exit(1);
148     }
149     fclose(f);
150 
151     input[filesize] = 0;
152     return input;
153 }
154 
addenv(const char * name,const char * value)155 static void addenv(const char *name, const char *value)
156 {
157     const char *orig = getenv(name);
158     if (orig) {
159         char *newval;
160         (void)!asprintf(&newval, "%s,%s", orig, value);
161         setenv(name, newval, 1);
162         free(newval);
163     } else {
164         setenv(name, value, 1);
165     }
166 }
167 
168 int
main(int argc,char ** argv)169 main(int argc, char **argv)
170 {
171     const char *filename = NULL;
172 
173     for (int i = 1; i < argc; i++) {
174         if (strstr(argv[i], "-mcpu=") == argv[i]) {
175             setenv("AMD_FORCE_FAMILY", argv[i] + 6, 1);
176         } else if (filename == NULL) {
177             filename = argv[i];
178         } else {
179             if (strcmp(argv[i], "--help") != 0 && strcmp(argv[i], "-h") != 0)
180                 fprintf(stderr, "Unknown option: %s\n\n", argv[i]);
181 
182             fprintf(stderr, "Usage: amdgcn_glslc -mcpu=[chip name] [glsl filename]\n");
183             return 1;
184         }
185     }
186 
187     if (filename == NULL) {
188         fprintf(stderr, "No filename specified.\n");
189         return 1;
190     }
191 
192     addenv("R600_DEBUG", "precompile,vs,tcs,tes,gs,ps,cs,noir,notgsi");
193 
194     create_gl_core_context();
195 
196     /* Read the source. */
197     char *input = read_file(filename);
198 
199     /* Comment out lines beginning with ; (FileCheck prefix). */
200     if (input[0] == ';')
201         memcpy(input, "//", 2);
202 
203     char *s = input;
204     while (s = strstr(s, "\n;"))
205         memcpy(s + 1, "//", 2);
206 
207     s = input;
208     while (s && (s = strstr(s, "#shader "))) {
209         char type_str[16], name[128];
210         GLenum type;
211 
212         /* If #shader is not at the beginning of the line. */
213         if (s != input && s[-1] != '\n' && s[-1] != 0) {
214             s = strstr(s, "\n");
215             continue;
216         }
217 
218         /* Parse the #shader directive. */
219         if (sscanf(s + 8, "%s %s", type_str, name) != 2) {
220             fprintf(stderr, "Cannot parse #shader directive.\n");
221             continue;
222         }
223 
224         if (!strcmp(type_str, "vs"))
225             type = GL_VERTEX_SHADER;
226         else if (!strcmp(type_str, "tcs"))
227             type = GL_TESS_CONTROL_SHADER;
228         else if (!strcmp(type_str, "tes"))
229             type = GL_TESS_EVALUATION_SHADER;
230         else if (!strcmp(type_str, "gs"))
231             type = GL_GEOMETRY_SHADER;
232         else if (!strcmp(type_str, "fs"))
233             type = GL_FRAGMENT_SHADER;
234         else if (!strcmp(type_str, "cs"))
235             type = GL_COMPUTE_SHADER;
236 
237         /* Go the next line. */
238         s = strstr(s, "\n");
239         if (!s)
240             break;
241         s++;
242 
243         const char *source = s;
244 
245         /* Cut the shader source at the end. */
246         s = strstr(s, "#shader");
247         if (s && s[-1] == '\n')
248             s[-1] = 0;
249 
250         /* Compile the shader. */
251         printf("@%s:\n", name);
252 
253         /* Redirect stderr to stdout for the compiler. */
254         FILE *stderr_original = stderr;
255         stderr = stdout;
256         GLuint prog = glCreateShaderProgramv(type, 1, &source);
257         stderr = stderr_original;
258 
259         GLint linked;
260         glGetProgramiv(prog, GL_LINK_STATUS, &linked);
261 
262         if (!linked) {
263             char log[4096];
264             GLsizei length;
265 
266             glGetProgramInfoLog(prog, sizeof(log), &length, log);
267             fprintf(stderr, "ERROR: Compile failure:\n\n%s\n%s\n",
268                     source, log);
269             return 1;
270         }
271 
272         glDeleteProgram(prog);
273     }
274     return 0;
275 }
276