glsl: split out libstandalone
[mesa.git] / src / compiler / glsl / standalone.cpp
1 /*
2 * Copyright © 2008, 2009 Intel Corporation
3 *
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:
10 *
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.
14 *
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
21 * DEALINGS IN THE SOFTWARE.
22 */
23 #include <getopt.h>
24
25 /** @file standalone.cpp
26 *
27 * Standalone compiler helper lib. Used by standalone glsl_compiler and
28 * also available to drivers to implement their own standalone compiler
29 * with driver backend.
30 */
31
32 #include "ast.h"
33 #include "glsl_parser_extras.h"
34 #include "ir_optimization.h"
35 #include "program.h"
36 #include "program/hash_table.h"
37 #include "loop_analysis.h"
38 #include "standalone_scaffolding.h"
39 #include "standalone.h"
40
41 static const struct standalone_options *options;
42
43 static void
44 initialize_context(struct gl_context *ctx, gl_api api)
45 {
46 initialize_context_to_defaults(ctx, api);
47
48 /* The standalone compiler needs to claim support for almost
49 * everything in order to compile the built-in functions.
50 */
51 ctx->Const.GLSLVersion = options->glsl_version;
52 ctx->Extensions.ARB_ES3_compatibility = true;
53 ctx->Const.MaxComputeWorkGroupCount[0] = 65535;
54 ctx->Const.MaxComputeWorkGroupCount[1] = 65535;
55 ctx->Const.MaxComputeWorkGroupCount[2] = 65535;
56 ctx->Const.MaxComputeWorkGroupSize[0] = 1024;
57 ctx->Const.MaxComputeWorkGroupSize[1] = 1024;
58 ctx->Const.MaxComputeWorkGroupSize[2] = 64;
59 ctx->Const.MaxComputeWorkGroupInvocations = 1024;
60 ctx->Const.MaxComputeSharedMemorySize = 32768;
61 ctx->Const.Program[MESA_SHADER_COMPUTE].MaxTextureImageUnits = 16;
62 ctx->Const.Program[MESA_SHADER_COMPUTE].MaxUniformComponents = 1024;
63 ctx->Const.Program[MESA_SHADER_COMPUTE].MaxCombinedUniformComponents = 1024;
64 ctx->Const.Program[MESA_SHADER_COMPUTE].MaxInputComponents = 0; /* not used */
65 ctx->Const.Program[MESA_SHADER_COMPUTE].MaxOutputComponents = 0; /* not used */
66 ctx->Const.Program[MESA_SHADER_COMPUTE].MaxAtomicBuffers = 8;
67 ctx->Const.Program[MESA_SHADER_COMPUTE].MaxAtomicCounters = 8;
68 ctx->Const.Program[MESA_SHADER_COMPUTE].MaxImageUniforms = 8;
69 ctx->Const.Program[MESA_SHADER_COMPUTE].MaxUniformBlocks = 12;
70
71 switch (ctx->Const.GLSLVersion) {
72 case 100:
73 ctx->Const.MaxClipPlanes = 0;
74 ctx->Const.MaxCombinedTextureImageUnits = 8;
75 ctx->Const.MaxDrawBuffers = 2;
76 ctx->Const.MinProgramTexelOffset = 0;
77 ctx->Const.MaxProgramTexelOffset = 0;
78 ctx->Const.MaxLights = 0;
79 ctx->Const.MaxTextureCoordUnits = 0;
80 ctx->Const.MaxTextureUnits = 8;
81
82 ctx->Const.Program[MESA_SHADER_VERTEX].MaxAttribs = 8;
83 ctx->Const.Program[MESA_SHADER_VERTEX].MaxTextureImageUnits = 0;
84 ctx->Const.Program[MESA_SHADER_VERTEX].MaxUniformComponents = 128 * 4;
85 ctx->Const.Program[MESA_SHADER_VERTEX].MaxCombinedUniformComponents = 128 * 4;
86 ctx->Const.Program[MESA_SHADER_VERTEX].MaxInputComponents = 0; /* not used */
87 ctx->Const.Program[MESA_SHADER_VERTEX].MaxOutputComponents = 32;
88
89 ctx->Const.Program[MESA_SHADER_FRAGMENT].MaxTextureImageUnits =
90 ctx->Const.MaxCombinedTextureImageUnits;
91 ctx->Const.Program[MESA_SHADER_FRAGMENT].MaxUniformComponents = 16 * 4;
92 ctx->Const.Program[MESA_SHADER_FRAGMENT].MaxCombinedUniformComponents = 16 * 4;
93 ctx->Const.Program[MESA_SHADER_FRAGMENT].MaxInputComponents =
94 ctx->Const.Program[MESA_SHADER_VERTEX].MaxOutputComponents;
95 ctx->Const.Program[MESA_SHADER_FRAGMENT].MaxOutputComponents = 0; /* not used */
96
97 ctx->Const.MaxVarying = ctx->Const.Program[MESA_SHADER_VERTEX].MaxOutputComponents / 4;
98 break;
99 case 110:
100 case 120:
101 ctx->Const.MaxClipPlanes = 6;
102 ctx->Const.MaxCombinedTextureImageUnits = 2;
103 ctx->Const.MaxDrawBuffers = 1;
104 ctx->Const.MinProgramTexelOffset = 0;
105 ctx->Const.MaxProgramTexelOffset = 0;
106 ctx->Const.MaxLights = 8;
107 ctx->Const.MaxTextureCoordUnits = 2;
108 ctx->Const.MaxTextureUnits = 2;
109
110 ctx->Const.Program[MESA_SHADER_VERTEX].MaxAttribs = 16;
111 ctx->Const.Program[MESA_SHADER_VERTEX].MaxTextureImageUnits = 0;
112 ctx->Const.Program[MESA_SHADER_VERTEX].MaxUniformComponents = 512;
113 ctx->Const.Program[MESA_SHADER_VERTEX].MaxCombinedUniformComponents = 512;
114 ctx->Const.Program[MESA_SHADER_VERTEX].MaxInputComponents = 0; /* not used */
115 ctx->Const.Program[MESA_SHADER_VERTEX].MaxOutputComponents = 32;
116
117 ctx->Const.Program[MESA_SHADER_FRAGMENT].MaxTextureImageUnits =
118 ctx->Const.MaxCombinedTextureImageUnits;
119 ctx->Const.Program[MESA_SHADER_FRAGMENT].MaxUniformComponents = 64;
120 ctx->Const.Program[MESA_SHADER_FRAGMENT].MaxCombinedUniformComponents = 64;
121 ctx->Const.Program[MESA_SHADER_FRAGMENT].MaxInputComponents =
122 ctx->Const.Program[MESA_SHADER_VERTEX].MaxOutputComponents;
123 ctx->Const.Program[MESA_SHADER_FRAGMENT].MaxOutputComponents = 0; /* not used */
124
125 ctx->Const.MaxVarying = ctx->Const.Program[MESA_SHADER_VERTEX].MaxOutputComponents / 4;
126 break;
127 case 130:
128 case 140:
129 ctx->Const.MaxClipPlanes = 8;
130 ctx->Const.MaxCombinedTextureImageUnits = 16;
131 ctx->Const.MaxDrawBuffers = 8;
132 ctx->Const.MinProgramTexelOffset = -8;
133 ctx->Const.MaxProgramTexelOffset = 7;
134 ctx->Const.MaxLights = 8;
135 ctx->Const.MaxTextureCoordUnits = 8;
136 ctx->Const.MaxTextureUnits = 2;
137
138 ctx->Const.Program[MESA_SHADER_VERTEX].MaxAttribs = 16;
139 ctx->Const.Program[MESA_SHADER_VERTEX].MaxTextureImageUnits = 16;
140 ctx->Const.Program[MESA_SHADER_VERTEX].MaxUniformComponents = 1024;
141 ctx->Const.Program[MESA_SHADER_VERTEX].MaxCombinedUniformComponents = 1024;
142 ctx->Const.Program[MESA_SHADER_VERTEX].MaxInputComponents = 0; /* not used */
143 ctx->Const.Program[MESA_SHADER_VERTEX].MaxOutputComponents = 64;
144
145 ctx->Const.Program[MESA_SHADER_FRAGMENT].MaxTextureImageUnits = 16;
146 ctx->Const.Program[MESA_SHADER_FRAGMENT].MaxUniformComponents = 1024;
147 ctx->Const.Program[MESA_SHADER_FRAGMENT].MaxCombinedUniformComponents = 1024;
148 ctx->Const.Program[MESA_SHADER_FRAGMENT].MaxInputComponents =
149 ctx->Const.Program[MESA_SHADER_VERTEX].MaxOutputComponents;
150 ctx->Const.Program[MESA_SHADER_FRAGMENT].MaxOutputComponents = 0; /* not used */
151
152 ctx->Const.MaxVarying = ctx->Const.Program[MESA_SHADER_VERTEX].MaxOutputComponents / 4;
153 break;
154 case 150:
155 case 330:
156 ctx->Const.MaxClipPlanes = 8;
157 ctx->Const.MaxDrawBuffers = 8;
158 ctx->Const.MinProgramTexelOffset = -8;
159 ctx->Const.MaxProgramTexelOffset = 7;
160 ctx->Const.MaxLights = 8;
161 ctx->Const.MaxTextureCoordUnits = 8;
162 ctx->Const.MaxTextureUnits = 2;
163
164 ctx->Const.Program[MESA_SHADER_VERTEX].MaxAttribs = 16;
165 ctx->Const.Program[MESA_SHADER_VERTEX].MaxTextureImageUnits = 16;
166 ctx->Const.Program[MESA_SHADER_VERTEX].MaxUniformComponents = 1024;
167 ctx->Const.Program[MESA_SHADER_VERTEX].MaxCombinedUniformComponents = 1024;
168 ctx->Const.Program[MESA_SHADER_VERTEX].MaxInputComponents = 0; /* not used */
169 ctx->Const.Program[MESA_SHADER_VERTEX].MaxOutputComponents = 64;
170
171 ctx->Const.Program[MESA_SHADER_GEOMETRY].MaxTextureImageUnits = 16;
172 ctx->Const.Program[MESA_SHADER_GEOMETRY].MaxUniformComponents = 1024;
173 ctx->Const.Program[MESA_SHADER_GEOMETRY].MaxCombinedUniformComponents = 1024;
174 ctx->Const.Program[MESA_SHADER_GEOMETRY].MaxInputComponents =
175 ctx->Const.Program[MESA_SHADER_VERTEX].MaxOutputComponents;
176 ctx->Const.Program[MESA_SHADER_GEOMETRY].MaxOutputComponents = 128;
177
178 ctx->Const.Program[MESA_SHADER_FRAGMENT].MaxTextureImageUnits = 16;
179 ctx->Const.Program[MESA_SHADER_FRAGMENT].MaxUniformComponents = 1024;
180 ctx->Const.Program[MESA_SHADER_FRAGMENT].MaxCombinedUniformComponents = 1024;
181 ctx->Const.Program[MESA_SHADER_FRAGMENT].MaxInputComponents =
182 ctx->Const.Program[MESA_SHADER_GEOMETRY].MaxOutputComponents;
183 ctx->Const.Program[MESA_SHADER_FRAGMENT].MaxOutputComponents = 0; /* not used */
184
185 ctx->Const.MaxCombinedTextureImageUnits =
186 ctx->Const.Program[MESA_SHADER_VERTEX].MaxTextureImageUnits
187 + ctx->Const.Program[MESA_SHADER_GEOMETRY].MaxTextureImageUnits
188 + ctx->Const.Program[MESA_SHADER_FRAGMENT].MaxTextureImageUnits;
189
190 ctx->Const.MaxGeometryOutputVertices = 256;
191 ctx->Const.MaxGeometryTotalOutputComponents = 1024;
192
193 ctx->Const.MaxVarying = 60 / 4;
194 break;
195 case 300:
196 ctx->Const.MaxClipPlanes = 8;
197 ctx->Const.MaxCombinedTextureImageUnits = 32;
198 ctx->Const.MaxDrawBuffers = 4;
199 ctx->Const.MinProgramTexelOffset = -8;
200 ctx->Const.MaxProgramTexelOffset = 7;
201 ctx->Const.MaxLights = 0;
202 ctx->Const.MaxTextureCoordUnits = 0;
203 ctx->Const.MaxTextureUnits = 0;
204
205 ctx->Const.Program[MESA_SHADER_VERTEX].MaxAttribs = 16;
206 ctx->Const.Program[MESA_SHADER_VERTEX].MaxTextureImageUnits = 16;
207 ctx->Const.Program[MESA_SHADER_VERTEX].MaxUniformComponents = 1024;
208 ctx->Const.Program[MESA_SHADER_VERTEX].MaxCombinedUniformComponents = 1024;
209 ctx->Const.Program[MESA_SHADER_VERTEX].MaxInputComponents = 0; /* not used */
210 ctx->Const.Program[MESA_SHADER_VERTEX].MaxOutputComponents = 16 * 4;
211
212 ctx->Const.Program[MESA_SHADER_FRAGMENT].MaxTextureImageUnits = 16;
213 ctx->Const.Program[MESA_SHADER_FRAGMENT].MaxUniformComponents = 224;
214 ctx->Const.Program[MESA_SHADER_FRAGMENT].MaxCombinedUniformComponents = 224;
215 ctx->Const.Program[MESA_SHADER_FRAGMENT].MaxInputComponents = 15 * 4;
216 ctx->Const.Program[MESA_SHADER_FRAGMENT].MaxOutputComponents = 0; /* not used */
217
218 ctx->Const.MaxVarying = ctx->Const.Program[MESA_SHADER_FRAGMENT].MaxInputComponents / 4;
219 break;
220 }
221
222 ctx->Const.GenerateTemporaryNames = true;
223 ctx->Const.MaxPatchVertices = 32;
224
225 ctx->Driver.NewShader = _mesa_new_shader;
226 }
227
228 /* Returned string will have 'ctx' as its ralloc owner. */
229 static char *
230 load_text_file(void *ctx, const char *file_name)
231 {
232 char *text = NULL;
233 size_t size;
234 size_t total_read = 0;
235 FILE *fp = fopen(file_name, "rb");
236
237 if (!fp) {
238 return NULL;
239 }
240
241 fseek(fp, 0L, SEEK_END);
242 size = ftell(fp);
243 fseek(fp, 0L, SEEK_SET);
244
245 text = (char *) ralloc_size(ctx, size + 1);
246 if (text != NULL) {
247 do {
248 size_t bytes = fread(text + total_read,
249 1, size - total_read, fp);
250 if (bytes < size - total_read) {
251 free(text);
252 text = NULL;
253 goto error;
254 }
255
256 if (bytes == 0) {
257 break;
258 }
259
260 total_read += bytes;
261 } while (total_read < size);
262
263 text[total_read] = '\0';
264 error:;
265 }
266
267 fclose(fp);
268
269 return text;
270 }
271
272 void
273 compile_shader(struct gl_context *ctx, struct gl_shader *shader)
274 {
275 struct _mesa_glsl_parse_state *state =
276 new(shader) _mesa_glsl_parse_state(ctx, shader->Stage, shader);
277
278 _mesa_glsl_compile_shader(ctx, shader, options->dump_ast, options->dump_hir);
279
280 /* Print out the resulting IR */
281 if (!state->error && options->dump_lir) {
282 _mesa_print_ir(stdout, shader->ir, state);
283 }
284
285 return;
286 }
287
288 void
289 init_gl_program(struct gl_program *prog, GLenum target)
290 {
291 mtx_init(&prog->Mutex, mtx_plain);
292
293 prog->RefCount = 1;
294 prog->Format = GL_PROGRAM_FORMAT_ASCII_ARB;
295
296 /* default mapping from samplers to texture units */
297 for (int i = 0; i < MAX_SAMPLERS; i++)
298 prog->SamplerUnits[i] = i;
299 }
300
301 extern "C" struct gl_shader_program *
302 standalone_compile_shader(const struct standalone_options *_options,
303 unsigned num_files, char* const* files)
304 {
305 int status = EXIT_SUCCESS;
306 static struct gl_context local_ctx;
307 struct gl_context *ctx = &local_ctx;
308 bool glsl_es = false;
309
310 options = _options;
311
312 switch (options->glsl_version) {
313 case 100:
314 case 300:
315 glsl_es = true;
316 break;
317 case 110:
318 case 120:
319 case 130:
320 case 140:
321 case 150:
322 case 330:
323 glsl_es = false;
324 break;
325 default:
326 fprintf(stderr, "Unrecognized GLSL version `%d'\n", options->glsl_version);
327 return NULL;
328 }
329
330 initialize_context(ctx, (glsl_es) ? API_OPENGLES2 : API_OPENGL_COMPAT);
331
332 struct gl_shader_program *whole_program;
333
334 whole_program = rzalloc (NULL, struct gl_shader_program);
335 assert(whole_program != NULL);
336 whole_program->InfoLog = ralloc_strdup(whole_program, "");
337
338 /* Created just to avoid segmentation faults */
339 whole_program->AttributeBindings = new string_to_uint_map;
340 whole_program->FragDataBindings = new string_to_uint_map;
341 whole_program->FragDataIndexBindings = new string_to_uint_map;
342
343 for (unsigned i = 0; i < num_files; i++) {
344 whole_program->Shaders =
345 reralloc(whole_program, whole_program->Shaders,
346 struct gl_shader *, whole_program->NumShaders + 1);
347 assert(whole_program->Shaders != NULL);
348
349 struct gl_shader *shader = rzalloc(whole_program, gl_shader);
350
351 whole_program->Shaders[whole_program->NumShaders] = shader;
352 whole_program->NumShaders++;
353
354 const unsigned len = strlen(files[i]);
355 if (len < 6)
356 goto fail;
357
358 const char *const ext = & files[i][len - 5];
359 /* TODO add support to read a .shader_test */
360 if (strncmp(".vert", ext, 5) == 0 || strncmp(".glsl", ext, 5) == 0)
361 shader->Type = GL_VERTEX_SHADER;
362 else if (strncmp(".tesc", ext, 5) == 0)
363 shader->Type = GL_TESS_CONTROL_SHADER;
364 else if (strncmp(".tese", ext, 5) == 0)
365 shader->Type = GL_TESS_EVALUATION_SHADER;
366 else if (strncmp(".geom", ext, 5) == 0)
367 shader->Type = GL_GEOMETRY_SHADER;
368 else if (strncmp(".frag", ext, 5) == 0)
369 shader->Type = GL_FRAGMENT_SHADER;
370 else if (strncmp(".comp", ext, 5) == 0)
371 shader->Type = GL_COMPUTE_SHADER;
372 else
373 goto fail;
374 shader->Stage = _mesa_shader_enum_to_shader_stage(shader->Type);
375
376 shader->Source = load_text_file(whole_program, files[i]);
377 if (shader->Source == NULL) {
378 printf("File \"%s\" does not exist.\n", files[i]);
379 exit(EXIT_FAILURE);
380 }
381
382 compile_shader(ctx, shader);
383
384 if (strlen(shader->InfoLog) > 0)
385 printf("Info log for %s:\n%s\n", files[i], shader->InfoLog);
386
387 if (!shader->CompileStatus) {
388 status = EXIT_FAILURE;
389 break;
390 }
391 }
392
393 if ((status == EXIT_SUCCESS) && options->do_link) {
394 _mesa_clear_shader_program_data(whole_program);
395
396 link_shaders(ctx, whole_program);
397 status = (whole_program->LinkStatus) ? EXIT_SUCCESS : EXIT_FAILURE;
398
399 if (strlen(whole_program->InfoLog) > 0)
400 printf("Info log for linking:\n%s\n", whole_program->InfoLog);
401
402 for (unsigned i = 0; i < MESA_SHADER_STAGES; i++) {
403 struct gl_shader *shader = whole_program->_LinkedShaders[i];
404
405 if (!shader)
406 continue;
407
408 shader->Program = rzalloc(shader, gl_program);
409 init_gl_program(shader->Program, shader->Type);
410 }
411 }
412
413 return whole_program;
414
415 fail:
416 ralloc_free(whole_program);
417 return NULL;
418 }
419
420 extern "C" void
421 standalone_compiler_cleanup(struct gl_shader_program *whole_program)
422 {
423 for (unsigned i = 0; i < MESA_SHADER_STAGES; i++)
424 ralloc_free(whole_program->_LinkedShaders[i]);
425
426 delete whole_program->AttributeBindings;
427 delete whole_program->FragDataBindings;
428 delete whole_program->FragDataIndexBindings;
429
430 ralloc_free(whole_program);
431 _mesa_glsl_release_types();
432 _mesa_glsl_release_builtin_functions();
433 }