2 * Copyright © 2008, 2009 Intel Corporation
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:
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
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.
25 /** @file standalone.cpp
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.
33 #include "glsl_parser_extras.h"
34 #include "ir_optimization.h"
36 #include "loop_analysis.h"
37 #include "standalone_scaffolding.h"
38 #include "standalone.h"
39 #include "util/string_to_uint_map.h"
41 static const struct standalone_options
*options
;
44 initialize_context(struct gl_context
*ctx
, gl_api api
)
46 initialize_context_to_defaults(ctx
, api
);
48 /* The standalone compiler needs to claim support for almost
49 * everything in order to compile the built-in functions.
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;
71 switch (ctx
->Const
.GLSLVersion
) {
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;
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;
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 */
97 ctx
->Const
.MaxVarying
= ctx
->Const
.Program
[MESA_SHADER_VERTEX
].MaxOutputComponents
/ 4;
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;
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;
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 */
125 ctx
->Const
.MaxVarying
= ctx
->Const
.Program
[MESA_SHADER_VERTEX
].MaxOutputComponents
/ 4;
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;
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;
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 */
152 ctx
->Const
.MaxVarying
= ctx
->Const
.Program
[MESA_SHADER_VERTEX
].MaxOutputComponents
/ 4;
162 ctx
->Const
.MaxClipPlanes
= 8;
163 ctx
->Const
.MaxDrawBuffers
= 8;
164 ctx
->Const
.MinProgramTexelOffset
= -8;
165 ctx
->Const
.MaxProgramTexelOffset
= 7;
166 ctx
->Const
.MaxLights
= 8;
167 ctx
->Const
.MaxTextureCoordUnits
= 8;
168 ctx
->Const
.MaxTextureUnits
= 2;
170 ctx
->Const
.Program
[MESA_SHADER_VERTEX
].MaxAttribs
= 16;
171 ctx
->Const
.Program
[MESA_SHADER_VERTEX
].MaxTextureImageUnits
= 16;
172 ctx
->Const
.Program
[MESA_SHADER_VERTEX
].MaxUniformComponents
= 1024;
173 ctx
->Const
.Program
[MESA_SHADER_VERTEX
].MaxCombinedUniformComponents
= 1024;
174 ctx
->Const
.Program
[MESA_SHADER_VERTEX
].MaxInputComponents
= 0; /* not used */
175 ctx
->Const
.Program
[MESA_SHADER_VERTEX
].MaxOutputComponents
= 64;
177 ctx
->Const
.Program
[MESA_SHADER_GEOMETRY
].MaxTextureImageUnits
= 16;
178 ctx
->Const
.Program
[MESA_SHADER_GEOMETRY
].MaxUniformComponents
= 1024;
179 ctx
->Const
.Program
[MESA_SHADER_GEOMETRY
].MaxCombinedUniformComponents
= 1024;
180 ctx
->Const
.Program
[MESA_SHADER_GEOMETRY
].MaxInputComponents
=
181 ctx
->Const
.Program
[MESA_SHADER_VERTEX
].MaxOutputComponents
;
182 ctx
->Const
.Program
[MESA_SHADER_GEOMETRY
].MaxOutputComponents
= 128;
184 ctx
->Const
.Program
[MESA_SHADER_FRAGMENT
].MaxTextureImageUnits
= 16;
185 ctx
->Const
.Program
[MESA_SHADER_FRAGMENT
].MaxUniformComponents
= 1024;
186 ctx
->Const
.Program
[MESA_SHADER_FRAGMENT
].MaxCombinedUniformComponents
= 1024;
187 ctx
->Const
.Program
[MESA_SHADER_FRAGMENT
].MaxInputComponents
=
188 ctx
->Const
.Program
[MESA_SHADER_GEOMETRY
].MaxOutputComponents
;
189 ctx
->Const
.Program
[MESA_SHADER_FRAGMENT
].MaxOutputComponents
= 0; /* not used */
191 ctx
->Const
.MaxCombinedTextureImageUnits
=
192 ctx
->Const
.Program
[MESA_SHADER_VERTEX
].MaxTextureImageUnits
193 + ctx
->Const
.Program
[MESA_SHADER_GEOMETRY
].MaxTextureImageUnits
194 + ctx
->Const
.Program
[MESA_SHADER_FRAGMENT
].MaxTextureImageUnits
;
196 ctx
->Const
.MaxGeometryOutputVertices
= 256;
197 ctx
->Const
.MaxGeometryTotalOutputComponents
= 1024;
199 ctx
->Const
.MaxVarying
= 60 / 4;
202 ctx
->Const
.MaxClipPlanes
= 8;
203 ctx
->Const
.MaxCombinedTextureImageUnits
= 32;
204 ctx
->Const
.MaxDrawBuffers
= 4;
205 ctx
->Const
.MinProgramTexelOffset
= -8;
206 ctx
->Const
.MaxProgramTexelOffset
= 7;
207 ctx
->Const
.MaxLights
= 0;
208 ctx
->Const
.MaxTextureCoordUnits
= 0;
209 ctx
->Const
.MaxTextureUnits
= 0;
211 ctx
->Const
.Program
[MESA_SHADER_VERTEX
].MaxAttribs
= 16;
212 ctx
->Const
.Program
[MESA_SHADER_VERTEX
].MaxTextureImageUnits
= 16;
213 ctx
->Const
.Program
[MESA_SHADER_VERTEX
].MaxUniformComponents
= 1024;
214 ctx
->Const
.Program
[MESA_SHADER_VERTEX
].MaxCombinedUniformComponents
= 1024;
215 ctx
->Const
.Program
[MESA_SHADER_VERTEX
].MaxInputComponents
= 0; /* not used */
216 ctx
->Const
.Program
[MESA_SHADER_VERTEX
].MaxOutputComponents
= 16 * 4;
218 ctx
->Const
.Program
[MESA_SHADER_FRAGMENT
].MaxTextureImageUnits
= 16;
219 ctx
->Const
.Program
[MESA_SHADER_FRAGMENT
].MaxUniformComponents
= 224;
220 ctx
->Const
.Program
[MESA_SHADER_FRAGMENT
].MaxCombinedUniformComponents
= 224;
221 ctx
->Const
.Program
[MESA_SHADER_FRAGMENT
].MaxInputComponents
= 15 * 4;
222 ctx
->Const
.Program
[MESA_SHADER_FRAGMENT
].MaxOutputComponents
= 0; /* not used */
224 ctx
->Const
.MaxVarying
= ctx
->Const
.Program
[MESA_SHADER_FRAGMENT
].MaxInputComponents
/ 4;
228 ctx
->Const
.GenerateTemporaryNames
= true;
229 ctx
->Const
.MaxPatchVertices
= 32;
231 /* GL_ARB_explicit_uniform_location, GL_MAX_UNIFORM_LOCATIONS */
232 ctx
->Const
.MaxUserAssignableUniformLocations
=
233 4 * MESA_SHADER_STAGES
* MAX_UNIFORMS
;
235 ctx
->Driver
.NewShader
= _mesa_new_linked_shader
;
238 /* Returned string will have 'ctx' as its ralloc owner. */
240 load_text_file(void *ctx
, const char *file_name
)
244 size_t total_read
= 0;
245 FILE *fp
= fopen(file_name
, "rb");
251 fseek(fp
, 0L, SEEK_END
);
253 fseek(fp
, 0L, SEEK_SET
);
255 text
= (char *) ralloc_size(ctx
, size
+ 1);
258 size_t bytes
= fread(text
+ total_read
,
259 1, size
- total_read
, fp
);
260 if (bytes
< size
- total_read
) {
271 } while (total_read
< size
);
273 text
[total_read
] = '\0';
283 compile_shader(struct gl_context
*ctx
, struct gl_shader
*shader
)
285 struct _mesa_glsl_parse_state
*state
=
286 new(shader
) _mesa_glsl_parse_state(ctx
, shader
->Stage
, shader
);
288 _mesa_glsl_compile_shader(ctx
, shader
, options
->dump_ast
, options
->dump_hir
);
290 /* Print out the resulting IR */
291 if (!state
->error
&& options
->dump_lir
) {
292 _mesa_print_ir(stdout
, shader
->ir
, state
);
299 init_gl_program(struct gl_program
*prog
, GLenum target
)
301 mtx_init(&prog
->Mutex
, mtx_plain
);
304 prog
->Format
= GL_PROGRAM_FORMAT_ASCII_ARB
;
306 /* default mapping from samplers to texture units */
307 for (int i
= 0; i
< MAX_SAMPLERS
; i
++)
308 prog
->SamplerUnits
[i
] = i
;
311 extern "C" struct gl_shader_program
*
312 standalone_compile_shader(const struct standalone_options
*_options
,
313 unsigned num_files
, char* const* files
)
315 int status
= EXIT_SUCCESS
;
316 static struct gl_context local_ctx
;
317 struct gl_context
*ctx
= &local_ctx
;
318 bool glsl_es
= false;
322 switch (options
->glsl_version
) {
342 fprintf(stderr
, "Unrecognized GLSL version `%d'\n", options
->glsl_version
);
347 initialize_context(ctx
, API_OPENGLES2
);
349 initialize_context(ctx
, options
->glsl_version
> 130 ? API_OPENGL_CORE
: API_OPENGL_COMPAT
);
352 struct gl_shader_program
*whole_program
;
354 whole_program
= rzalloc (NULL
, struct gl_shader_program
);
355 assert(whole_program
!= NULL
);
356 whole_program
->InfoLog
= ralloc_strdup(whole_program
, "");
358 /* Created just to avoid segmentation faults */
359 whole_program
->AttributeBindings
= new string_to_uint_map
;
360 whole_program
->FragDataBindings
= new string_to_uint_map
;
361 whole_program
->FragDataIndexBindings
= new string_to_uint_map
;
363 for (unsigned i
= 0; i
< num_files
; i
++) {
364 whole_program
->Shaders
=
365 reralloc(whole_program
, whole_program
->Shaders
,
366 struct gl_shader
*, whole_program
->NumShaders
+ 1);
367 assert(whole_program
->Shaders
!= NULL
);
369 struct gl_shader
*shader
= rzalloc(whole_program
, gl_shader
);
371 whole_program
->Shaders
[whole_program
->NumShaders
] = shader
;
372 whole_program
->NumShaders
++;
374 const unsigned len
= strlen(files
[i
]);
378 const char *const ext
= & files
[i
][len
- 5];
379 /* TODO add support to read a .shader_test */
380 if (strncmp(".vert", ext
, 5) == 0 || strncmp(".glsl", ext
, 5) == 0)
381 shader
->Type
= GL_VERTEX_SHADER
;
382 else if (strncmp(".tesc", ext
, 5) == 0)
383 shader
->Type
= GL_TESS_CONTROL_SHADER
;
384 else if (strncmp(".tese", ext
, 5) == 0)
385 shader
->Type
= GL_TESS_EVALUATION_SHADER
;
386 else if (strncmp(".geom", ext
, 5) == 0)
387 shader
->Type
= GL_GEOMETRY_SHADER
;
388 else if (strncmp(".frag", ext
, 5) == 0)
389 shader
->Type
= GL_FRAGMENT_SHADER
;
390 else if (strncmp(".comp", ext
, 5) == 0)
391 shader
->Type
= GL_COMPUTE_SHADER
;
394 shader
->Stage
= _mesa_shader_enum_to_shader_stage(shader
->Type
);
396 shader
->Source
= load_text_file(whole_program
, files
[i
]);
397 if (shader
->Source
== NULL
) {
398 printf("File \"%s\" does not exist.\n", files
[i
]);
402 compile_shader(ctx
, shader
);
404 if (strlen(shader
->InfoLog
) > 0) {
405 if (!options
->just_log
)
406 printf("Info log for %s:\n", files
[i
]);
408 printf("%s", shader
->InfoLog
);
409 if (!options
->just_log
)
413 if (!shader
->CompileStatus
) {
414 status
= EXIT_FAILURE
;
419 if ((status
== EXIT_SUCCESS
) && options
->do_link
) {
420 _mesa_clear_shader_program_data(whole_program
);
422 link_shaders(ctx
, whole_program
);
423 status
= (whole_program
->LinkStatus
) ? EXIT_SUCCESS
: EXIT_FAILURE
;
425 if (strlen(whole_program
->InfoLog
) > 0) {
427 if (!options
->just_log
)
428 printf("Info log for linking:\n");
429 printf("%s", whole_program
->InfoLog
);
430 if (!options
->just_log
)
434 for (unsigned i
= 0; i
< MESA_SHADER_STAGES
; i
++) {
435 struct gl_linked_shader
*shader
= whole_program
->_LinkedShaders
[i
];
440 shader
->Program
= rzalloc(shader
, gl_program
);
441 init_gl_program(shader
->Program
, shader
->Stage
);
445 return whole_program
;
448 ralloc_free(whole_program
);
453 standalone_compiler_cleanup(struct gl_shader_program
*whole_program
)
455 for (unsigned i
= 0; i
< MESA_SHADER_STAGES
; i
++)
456 ralloc_free(whole_program
->_LinkedShaders
[i
]);
458 delete whole_program
->AttributeBindings
;
459 delete whole_program
->FragDataBindings
;
460 delete whole_program
->FragDataIndexBindings
;
462 ralloc_free(whole_program
);
463 _mesa_glsl_release_types();
464 _mesa_glsl_release_builtin_functions();