a42f3d23df0e01940c56a4f986aa8cdcab82325f
[mesa.git] / src / glsl / glsl_parser_extras.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 <stdio.h>
24 #include <stdarg.h>
25 #include <string.h>
26 #include <assert.h>
27
28 extern "C" {
29 #include "main/core.h" /* for struct gl_context */
30 #include "main/context.h"
31 #include "main/shaderobj.h"
32 }
33
34 #include "ralloc.h"
35 #include "ast.h"
36 #include "glsl_parser_extras.h"
37 #include "glsl_parser.h"
38 #include "ir_optimization.h"
39 #include "loop_analysis.h"
40
41 /**
42 * Format a short human-readable description of the given GLSL version.
43 */
44 const char *
45 glsl_compute_version_string(void *mem_ctx, bool is_es, unsigned version)
46 {
47 return ralloc_asprintf(mem_ctx, "GLSL%s %d.%02d", is_es ? " ES" : "",
48 version / 100, version % 100);
49 }
50
51
52 static unsigned known_desktop_glsl_versions[] =
53 { 110, 120, 130, 140, 150, 330, 400, 410, 420, 430, 440 };
54
55
56 _mesa_glsl_parse_state::_mesa_glsl_parse_state(struct gl_context *_ctx,
57 gl_shader_stage stage,
58 void *mem_ctx)
59 : ctx(_ctx), cs_input_local_size_specified(false), cs_input_local_size(),
60 switch_state()
61 {
62 assert(stage < MESA_SHADER_STAGES);
63 this->stage = stage;
64
65 this->scanner = NULL;
66 this->translation_unit.make_empty();
67 this->symbols = new(mem_ctx) glsl_symbol_table;
68
69 this->info_log = ralloc_strdup(mem_ctx, "");
70 this->error = false;
71 this->loop_nesting_ast = NULL;
72
73 this->struct_specifier_depth = 0;
74
75 this->uses_builtin_functions = false;
76
77 /* Set default language version and extensions */
78 this->language_version = ctx->Const.ForceGLSLVersion ?
79 ctx->Const.ForceGLSLVersion : 110;
80 this->es_shader = false;
81 this->ARB_texture_rectangle_enable = true;
82
83 /* OpenGL ES 2.0 has different defaults from desktop GL. */
84 if (ctx->API == API_OPENGLES2) {
85 this->language_version = 100;
86 this->es_shader = true;
87 this->ARB_texture_rectangle_enable = false;
88 }
89
90 this->extensions = &ctx->Extensions;
91
92 this->Const.MaxLights = ctx->Const.MaxLights;
93 this->Const.MaxClipPlanes = ctx->Const.MaxClipPlanes;
94 this->Const.MaxTextureUnits = ctx->Const.MaxTextureUnits;
95 this->Const.MaxTextureCoords = ctx->Const.MaxTextureCoordUnits;
96 this->Const.MaxVertexAttribs = ctx->Const.Program[MESA_SHADER_VERTEX].MaxAttribs;
97 this->Const.MaxVertexUniformComponents = ctx->Const.Program[MESA_SHADER_VERTEX].MaxUniformComponents;
98 this->Const.MaxVertexTextureImageUnits = ctx->Const.Program[MESA_SHADER_VERTEX].MaxTextureImageUnits;
99 this->Const.MaxCombinedTextureImageUnits = ctx->Const.MaxCombinedTextureImageUnits;
100 this->Const.MaxTextureImageUnits = ctx->Const.Program[MESA_SHADER_FRAGMENT].MaxTextureImageUnits;
101 this->Const.MaxFragmentUniformComponents = ctx->Const.Program[MESA_SHADER_FRAGMENT].MaxUniformComponents;
102 this->Const.MinProgramTexelOffset = ctx->Const.MinProgramTexelOffset;
103 this->Const.MaxProgramTexelOffset = ctx->Const.MaxProgramTexelOffset;
104
105 this->Const.MaxDrawBuffers = ctx->Const.MaxDrawBuffers;
106
107 /* 1.50 constants */
108 this->Const.MaxVertexOutputComponents = ctx->Const.Program[MESA_SHADER_VERTEX].MaxOutputComponents;
109 this->Const.MaxGeometryInputComponents = ctx->Const.Program[MESA_SHADER_GEOMETRY].MaxInputComponents;
110 this->Const.MaxGeometryOutputComponents = ctx->Const.Program[MESA_SHADER_GEOMETRY].MaxOutputComponents;
111 this->Const.MaxFragmentInputComponents = ctx->Const.Program[MESA_SHADER_FRAGMENT].MaxInputComponents;
112 this->Const.MaxGeometryTextureImageUnits = ctx->Const.Program[MESA_SHADER_GEOMETRY].MaxTextureImageUnits;
113 this->Const.MaxGeometryOutputVertices = ctx->Const.MaxGeometryOutputVertices;
114 this->Const.MaxGeometryTotalOutputComponents = ctx->Const.MaxGeometryTotalOutputComponents;
115 this->Const.MaxGeometryUniformComponents = ctx->Const.Program[MESA_SHADER_GEOMETRY].MaxUniformComponents;
116
117 this->Const.MaxVertexAtomicCounters = ctx->Const.Program[MESA_SHADER_VERTEX].MaxAtomicCounters;
118 this->Const.MaxGeometryAtomicCounters = ctx->Const.Program[MESA_SHADER_GEOMETRY].MaxAtomicCounters;
119 this->Const.MaxFragmentAtomicCounters = ctx->Const.Program[MESA_SHADER_FRAGMENT].MaxAtomicCounters;
120 this->Const.MaxCombinedAtomicCounters = ctx->Const.MaxCombinedAtomicCounters;
121 this->Const.MaxAtomicBufferBindings = ctx->Const.MaxAtomicBufferBindings;
122
123 /* Compute shader constants */
124 for (unsigned i = 0; i < Elements(this->Const.MaxComputeWorkGroupCount); i++)
125 this->Const.MaxComputeWorkGroupCount[i] = ctx->Const.MaxComputeWorkGroupCount[i];
126 for (unsigned i = 0; i < Elements(this->Const.MaxComputeWorkGroupSize); i++)
127 this->Const.MaxComputeWorkGroupSize[i] = ctx->Const.MaxComputeWorkGroupSize[i];
128
129 this->Const.MaxImageUnits = ctx->Const.MaxImageUnits;
130 this->Const.MaxCombinedImageUnitsAndFragmentOutputs = ctx->Const.MaxCombinedImageUnitsAndFragmentOutputs;
131 this->Const.MaxImageSamples = ctx->Const.MaxImageSamples;
132 this->Const.MaxVertexImageUniforms = ctx->Const.Program[MESA_SHADER_VERTEX].MaxImageUniforms;
133 this->Const.MaxGeometryImageUniforms = ctx->Const.Program[MESA_SHADER_GEOMETRY].MaxImageUniforms;
134 this->Const.MaxFragmentImageUniforms = ctx->Const.Program[MESA_SHADER_FRAGMENT].MaxImageUniforms;
135 this->Const.MaxCombinedImageUniforms = ctx->Const.MaxCombinedImageUniforms;
136
137 this->current_function = NULL;
138 this->toplevel_ir = NULL;
139 this->found_return = false;
140 this->all_invariant = false;
141 this->user_structures = NULL;
142 this->num_user_structures = 0;
143
144 /* Populate the list of supported GLSL versions */
145 /* FINISHME: Once the OpenGL 3.0 'forward compatible' context or
146 * the OpenGL 3.2 Core context is supported, this logic will need
147 * change. Older versions of GLSL are no longer supported
148 * outside the compatibility contexts of 3.x.
149 */
150 this->num_supported_versions = 0;
151 if (_mesa_is_desktop_gl(ctx)) {
152 for (unsigned i = 0; i < ARRAY_SIZE(known_desktop_glsl_versions); i++) {
153 if (known_desktop_glsl_versions[i] <= ctx->Const.GLSLVersion) {
154 this->supported_versions[this->num_supported_versions].ver
155 = known_desktop_glsl_versions[i];
156 this->supported_versions[this->num_supported_versions].es = false;
157 this->num_supported_versions++;
158 }
159 }
160 }
161 if (ctx->API == API_OPENGLES2 || ctx->Extensions.ARB_ES2_compatibility) {
162 this->supported_versions[this->num_supported_versions].ver = 100;
163 this->supported_versions[this->num_supported_versions].es = true;
164 this->num_supported_versions++;
165 }
166 if (_mesa_is_gles3(ctx) || ctx->Extensions.ARB_ES3_compatibility) {
167 this->supported_versions[this->num_supported_versions].ver = 300;
168 this->supported_versions[this->num_supported_versions].es = true;
169 this->num_supported_versions++;
170 }
171 assert(this->num_supported_versions
172 <= ARRAY_SIZE(this->supported_versions));
173
174 /* Create a string for use in error messages to tell the user which GLSL
175 * versions are supported.
176 */
177 char *supported = ralloc_strdup(this, "");
178 for (unsigned i = 0; i < this->num_supported_versions; i++) {
179 unsigned ver = this->supported_versions[i].ver;
180 const char *const prefix = (i == 0)
181 ? ""
182 : ((i == this->num_supported_versions - 1) ? ", and " : ", ");
183 const char *const suffix = (this->supported_versions[i].es) ? " ES" : "";
184
185 ralloc_asprintf_append(& supported, "%s%u.%02u%s",
186 prefix,
187 ver / 100, ver % 100,
188 suffix);
189 }
190
191 this->supported_version_string = supported;
192
193 if (ctx->Const.ForceGLSLExtensionsWarn)
194 _mesa_glsl_process_extension("all", NULL, "warn", NULL, this);
195
196 this->default_uniform_qualifier = new(this) ast_type_qualifier();
197 this->default_uniform_qualifier->flags.q.shared = 1;
198 this->default_uniform_qualifier->flags.q.column_major = 1;
199
200 this->gs_input_prim_type_specified = false;
201 this->gs_input_size = 0;
202 this->in_qualifier = new(this) ast_type_qualifier();
203 this->out_qualifier = new(this) ast_type_qualifier();
204 this->early_fragment_tests = false;
205 memset(this->atomic_counter_offsets, 0,
206 sizeof(this->atomic_counter_offsets));
207 }
208
209 /**
210 * Determine whether the current GLSL version is sufficiently high to support
211 * a certain feature, and generate an error message if it isn't.
212 *
213 * \param required_glsl_version and \c required_glsl_es_version are
214 * interpreted as they are in _mesa_glsl_parse_state::is_version().
215 *
216 * \param locp is the parser location where the error should be reported.
217 *
218 * \param fmt (and additional arguments) constitute a printf-style error
219 * message to report if the version check fails. Information about the
220 * current and required GLSL versions will be appended. So, for example, if
221 * the GLSL version being compiled is 1.20, and check_version(130, 300, locp,
222 * "foo unsupported") is called, the error message will be "foo unsupported in
223 * GLSL 1.20 (GLSL 1.30 or GLSL 3.00 ES required)".
224 */
225 bool
226 _mesa_glsl_parse_state::check_version(unsigned required_glsl_version,
227 unsigned required_glsl_es_version,
228 YYLTYPE *locp, const char *fmt, ...)
229 {
230 if (this->is_version(required_glsl_version, required_glsl_es_version))
231 return true;
232
233 va_list args;
234 va_start(args, fmt);
235 char *problem = ralloc_vasprintf(this, fmt, args);
236 va_end(args);
237 const char *glsl_version_string
238 = glsl_compute_version_string(this, false, required_glsl_version);
239 const char *glsl_es_version_string
240 = glsl_compute_version_string(this, true, required_glsl_es_version);
241 const char *requirement_string = "";
242 if (required_glsl_version && required_glsl_es_version) {
243 requirement_string = ralloc_asprintf(this, " (%s or %s required)",
244 glsl_version_string,
245 glsl_es_version_string);
246 } else if (required_glsl_version) {
247 requirement_string = ralloc_asprintf(this, " (%s required)",
248 glsl_version_string);
249 } else if (required_glsl_es_version) {
250 requirement_string = ralloc_asprintf(this, " (%s required)",
251 glsl_es_version_string);
252 }
253 _mesa_glsl_error(locp, this, "%s in %s%s",
254 problem, this->get_version_string(),
255 requirement_string);
256
257 return false;
258 }
259
260 /**
261 * Process a GLSL #version directive.
262 *
263 * \param version is the integer that follows the #version token.
264 *
265 * \param ident is a string identifier that follows the integer, if any is
266 * present. Otherwise NULL.
267 */
268 void
269 _mesa_glsl_parse_state::process_version_directive(YYLTYPE *locp, int version,
270 const char *ident)
271 {
272 bool es_token_present = false;
273 if (ident) {
274 if (strcmp(ident, "es") == 0) {
275 es_token_present = true;
276 } else if (version >= 150) {
277 if (strcmp(ident, "core") == 0) {
278 /* Accept the token. There's no need to record that this is
279 * a core profile shader since that's the only profile we support.
280 */
281 } else if (strcmp(ident, "compatibility") == 0) {
282 _mesa_glsl_error(locp, this,
283 "the compatibility profile is not supported");
284 } else {
285 _mesa_glsl_error(locp, this,
286 "\"%s\" is not a valid shading language profile; "
287 "if present, it must be \"core\"", ident);
288 }
289 } else {
290 _mesa_glsl_error(locp, this,
291 "illegal text following version number");
292 }
293 }
294
295 this->es_shader = es_token_present;
296 if (version == 100) {
297 if (es_token_present) {
298 _mesa_glsl_error(locp, this,
299 "GLSL 1.00 ES should be selected using "
300 "`#version 100'");
301 } else {
302 this->es_shader = true;
303 }
304 }
305
306 if (this->es_shader) {
307 this->ARB_texture_rectangle_enable = false;
308 }
309
310 this->language_version = version;
311
312 bool supported = false;
313 for (unsigned i = 0; i < this->num_supported_versions; i++) {
314 if (this->supported_versions[i].ver == (unsigned) version
315 && this->supported_versions[i].es == this->es_shader) {
316 supported = true;
317 break;
318 }
319 }
320
321 if (!supported) {
322 _mesa_glsl_error(locp, this, "%s is not supported. "
323 "Supported versions are: %s",
324 this->get_version_string(),
325 this->supported_version_string);
326
327 /* On exit, the language_version must be set to a valid value.
328 * Later calls to _mesa_glsl_initialize_types will misbehave if
329 * the version is invalid.
330 */
331 switch (this->ctx->API) {
332 case API_OPENGL_COMPAT:
333 case API_OPENGL_CORE:
334 this->language_version = this->ctx->Const.GLSLVersion;
335 break;
336
337 case API_OPENGLES:
338 assert(!"Should not get here.");
339 /* FALLTHROUGH */
340
341 case API_OPENGLES2:
342 this->language_version = 100;
343 break;
344 }
345 }
346 }
347
348
349 /**
350 * Translate a gl_shader_stage to a short shader stage name for debug
351 * printouts and error messages.
352 */
353 const char *
354 _mesa_shader_stage_to_string(unsigned stage)
355 {
356 switch (stage) {
357 case MESA_SHADER_VERTEX: return "vertex";
358 case MESA_SHADER_FRAGMENT: return "fragment";
359 case MESA_SHADER_GEOMETRY: return "geometry";
360 }
361
362 assert(!"Should not get here.");
363 return "unknown";
364 }
365
366 /* This helper function will append the given message to the shader's
367 info log and report it via GL_ARB_debug_output. Per that extension,
368 'type' is one of the enum values classifying the message, and
369 'id' is the implementation-defined ID of the given message. */
370 static void
371 _mesa_glsl_msg(const YYLTYPE *locp, _mesa_glsl_parse_state *state,
372 GLenum type, const char *fmt, va_list ap)
373 {
374 bool error = (type == MESA_DEBUG_TYPE_ERROR);
375 GLuint msg_id = 0;
376
377 assert(state->info_log != NULL);
378
379 /* Get the offset that the new message will be written to. */
380 int msg_offset = strlen(state->info_log);
381
382 ralloc_asprintf_append(&state->info_log, "%u:%u(%u): %s: ",
383 locp->source,
384 locp->first_line,
385 locp->first_column,
386 error ? "error" : "warning");
387 ralloc_vasprintf_append(&state->info_log, fmt, ap);
388
389 const char *const msg = &state->info_log[msg_offset];
390 struct gl_context *ctx = state->ctx;
391
392 /* Report the error via GL_ARB_debug_output. */
393 _mesa_shader_debug(ctx, type, &msg_id, msg, strlen(msg));
394
395 ralloc_strcat(&state->info_log, "\n");
396 }
397
398 void
399 _mesa_glsl_error(YYLTYPE *locp, _mesa_glsl_parse_state *state,
400 const char *fmt, ...)
401 {
402 va_list ap;
403
404 state->error = true;
405
406 va_start(ap, fmt);
407 _mesa_glsl_msg(locp, state, MESA_DEBUG_TYPE_ERROR, fmt, ap);
408 va_end(ap);
409 }
410
411
412 void
413 _mesa_glsl_warning(const YYLTYPE *locp, _mesa_glsl_parse_state *state,
414 const char *fmt, ...)
415 {
416 va_list ap;
417
418 va_start(ap, fmt);
419 _mesa_glsl_msg(locp, state, MESA_DEBUG_TYPE_OTHER, fmt, ap);
420 va_end(ap);
421 }
422
423
424 /**
425 * Enum representing the possible behaviors that can be specified in
426 * an #extension directive.
427 */
428 enum ext_behavior {
429 extension_disable,
430 extension_enable,
431 extension_require,
432 extension_warn
433 };
434
435 /**
436 * Element type for _mesa_glsl_supported_extensions
437 */
438 struct _mesa_glsl_extension {
439 /**
440 * Name of the extension when referred to in a GLSL extension
441 * statement
442 */
443 const char *name;
444
445 /** True if this extension is available to desktop GL shaders */
446 bool avail_in_GL;
447
448 /** True if this extension is available to GLES shaders */
449 bool avail_in_ES;
450
451 /**
452 * Flag in the gl_extensions struct indicating whether this
453 * extension is supported by the driver, or
454 * &gl_extensions::dummy_true if supported by all drivers.
455 *
456 * Note: the type (GLboolean gl_extensions::*) is a "pointer to
457 * member" type, the type-safe alternative to the "offsetof" macro.
458 * In a nutshell:
459 *
460 * - foo bar::* p declares p to be an "offset" to a field of type
461 * foo that exists within struct bar
462 * - &bar::baz computes the "offset" of field baz within struct bar
463 * - x.*p accesses the field of x that exists at "offset" p
464 * - x->*p is equivalent to (*x).*p
465 */
466 const GLboolean gl_extensions::* supported_flag;
467
468 /**
469 * Flag in the _mesa_glsl_parse_state struct that should be set
470 * when this extension is enabled.
471 *
472 * See note in _mesa_glsl_extension::supported_flag about "pointer
473 * to member" types.
474 */
475 bool _mesa_glsl_parse_state::* enable_flag;
476
477 /**
478 * Flag in the _mesa_glsl_parse_state struct that should be set
479 * when the shader requests "warn" behavior for this extension.
480 *
481 * See note in _mesa_glsl_extension::supported_flag about "pointer
482 * to member" types.
483 */
484 bool _mesa_glsl_parse_state::* warn_flag;
485
486
487 bool compatible_with_state(const _mesa_glsl_parse_state *state) const;
488 void set_flags(_mesa_glsl_parse_state *state, ext_behavior behavior) const;
489 };
490
491 #define EXT(NAME, GL, ES, SUPPORTED_FLAG) \
492 { "GL_" #NAME, GL, ES, &gl_extensions::SUPPORTED_FLAG, \
493 &_mesa_glsl_parse_state::NAME##_enable, \
494 &_mesa_glsl_parse_state::NAME##_warn }
495
496 /**
497 * Table of extensions that can be enabled/disabled within a shader,
498 * and the conditions under which they are supported.
499 */
500 static const _mesa_glsl_extension _mesa_glsl_supported_extensions[] = {
501 /* API availability */
502 /* name GL ES supported flag */
503 EXT(ARB_arrays_of_arrays, true, false, ARB_arrays_of_arrays),
504 EXT(ARB_conservative_depth, true, false, ARB_conservative_depth),
505 EXT(ARB_draw_buffers, true, false, dummy_true),
506 EXT(ARB_draw_instanced, true, false, ARB_draw_instanced),
507 EXT(ARB_explicit_attrib_location, true, false, ARB_explicit_attrib_location),
508 EXT(ARB_fragment_coord_conventions, true, false, ARB_fragment_coord_conventions),
509 EXT(ARB_texture_rectangle, true, false, dummy_true),
510 EXT(EXT_texture_array, true, false, EXT_texture_array),
511 EXT(ARB_separate_shader_objects, true, false, ARB_separate_shader_objects),
512 EXT(ARB_shader_texture_lod, true, false, ARB_shader_texture_lod),
513 EXT(ARB_shader_stencil_export, true, false, ARB_shader_stencil_export),
514 EXT(AMD_conservative_depth, true, false, ARB_conservative_depth),
515 EXT(AMD_shader_stencil_export, true, false, ARB_shader_stencil_export),
516 EXT(OES_texture_3D, false, true, EXT_texture3D),
517 EXT(OES_EGL_image_external, false, true, OES_EGL_image_external),
518 EXT(ARB_shader_bit_encoding, true, false, ARB_shader_bit_encoding),
519 EXT(ARB_uniform_buffer_object, true, false, ARB_uniform_buffer_object),
520 EXT(OES_standard_derivatives, false, true, OES_standard_derivatives),
521 EXT(ARB_texture_cube_map_array, true, false, ARB_texture_cube_map_array),
522 EXT(ARB_shading_language_packing, true, false, ARB_shading_language_packing),
523 EXT(ARB_shading_language_420pack, true, false, ARB_shading_language_420pack),
524 EXT(ARB_texture_multisample, true, false, ARB_texture_multisample),
525 EXT(ARB_texture_query_levels, true, false, ARB_texture_query_levels),
526 EXT(ARB_texture_query_lod, true, false, ARB_texture_query_lod),
527 EXT(ARB_gpu_shader5, true, false, ARB_gpu_shader5),
528 EXT(AMD_vertex_shader_layer, true, false, AMD_vertex_shader_layer),
529 EXT(EXT_shader_integer_mix, true, true, EXT_shader_integer_mix),
530 EXT(ARB_texture_gather, true, false, ARB_texture_gather),
531 EXT(ARB_shader_atomic_counters, true, false, ARB_shader_atomic_counters),
532 EXT(ARB_sample_shading, true, false, ARB_sample_shading),
533 EXT(AMD_shader_trinary_minmax, true, false, dummy_true),
534 EXT(ARB_viewport_array, true, false, ARB_viewport_array),
535 EXT(ARB_compute_shader, true, false, ARB_compute_shader),
536 EXT(ARB_shader_image_load_store, true, false, ARB_shader_image_load_store),
537 };
538
539 #undef EXT
540
541
542 /**
543 * Determine whether a given extension is compatible with the target,
544 * API, and extension information in the current parser state.
545 */
546 bool _mesa_glsl_extension::compatible_with_state(const _mesa_glsl_parse_state *
547 state) const
548 {
549 /* Check that this extension matches whether we are compiling
550 * for desktop GL or GLES.
551 */
552 if (state->es_shader) {
553 if (!this->avail_in_ES) return false;
554 } else {
555 if (!this->avail_in_GL) return false;
556 }
557
558 /* Check that this extension is supported by the OpenGL
559 * implementation.
560 *
561 * Note: the ->* operator indexes into state->extensions by the
562 * offset this->supported_flag. See
563 * _mesa_glsl_extension::supported_flag for more info.
564 */
565 return state->extensions->*(this->supported_flag);
566 }
567
568 /**
569 * Set the appropriate flags in the parser state to establish the
570 * given behavior for this extension.
571 */
572 void _mesa_glsl_extension::set_flags(_mesa_glsl_parse_state *state,
573 ext_behavior behavior) const
574 {
575 /* Note: the ->* operator indexes into state by the
576 * offsets this->enable_flag and this->warn_flag. See
577 * _mesa_glsl_extension::supported_flag for more info.
578 */
579 state->*(this->enable_flag) = (behavior != extension_disable);
580 state->*(this->warn_flag) = (behavior == extension_warn);
581 }
582
583 /**
584 * Find an extension by name in _mesa_glsl_supported_extensions. If
585 * the name is not found, return NULL.
586 */
587 static const _mesa_glsl_extension *find_extension(const char *name)
588 {
589 for (unsigned i = 0; i < Elements(_mesa_glsl_supported_extensions); ++i) {
590 if (strcmp(name, _mesa_glsl_supported_extensions[i].name) == 0) {
591 return &_mesa_glsl_supported_extensions[i];
592 }
593 }
594 return NULL;
595 }
596
597
598 bool
599 _mesa_glsl_process_extension(const char *name, YYLTYPE *name_locp,
600 const char *behavior_string, YYLTYPE *behavior_locp,
601 _mesa_glsl_parse_state *state)
602 {
603 ext_behavior behavior;
604 if (strcmp(behavior_string, "warn") == 0) {
605 behavior = extension_warn;
606 } else if (strcmp(behavior_string, "require") == 0) {
607 behavior = extension_require;
608 } else if (strcmp(behavior_string, "enable") == 0) {
609 behavior = extension_enable;
610 } else if (strcmp(behavior_string, "disable") == 0) {
611 behavior = extension_disable;
612 } else {
613 _mesa_glsl_error(behavior_locp, state,
614 "unknown extension behavior `%s'",
615 behavior_string);
616 return false;
617 }
618
619 if (strcmp(name, "all") == 0) {
620 if ((behavior == extension_enable) || (behavior == extension_require)) {
621 _mesa_glsl_error(name_locp, state, "cannot %s all extensions",
622 (behavior == extension_enable)
623 ? "enable" : "require");
624 return false;
625 } else {
626 for (unsigned i = 0;
627 i < Elements(_mesa_glsl_supported_extensions); ++i) {
628 const _mesa_glsl_extension *extension
629 = &_mesa_glsl_supported_extensions[i];
630 if (extension->compatible_with_state(state)) {
631 _mesa_glsl_supported_extensions[i].set_flags(state, behavior);
632 }
633 }
634 }
635 } else {
636 const _mesa_glsl_extension *extension = find_extension(name);
637 if (extension && extension->compatible_with_state(state)) {
638 extension->set_flags(state, behavior);
639 } else {
640 static const char *const fmt = "extension `%s' unsupported in %s shader";
641
642 if (behavior == extension_require) {
643 _mesa_glsl_error(name_locp, state, fmt,
644 name, _mesa_shader_stage_to_string(state->stage));
645 return false;
646 } else {
647 _mesa_glsl_warning(name_locp, state, fmt,
648 name, _mesa_shader_stage_to_string(state->stage));
649 }
650 }
651 }
652
653 return true;
654 }
655
656
657 /**
658 * Recurses through <type> and <expr> if <expr> is an aggregate initializer
659 * and sets <expr>'s <constructor_type> field to <type>. Gives later functions
660 * (process_array_constructor, et al) sufficient information to do type
661 * checking.
662 *
663 * Operates on assignments involving an aggregate initializer. E.g.,
664 *
665 * vec4 pos = {1.0, -1.0, 0.0, 1.0};
666 *
667 * or more ridiculously,
668 *
669 * struct S {
670 * vec4 v[2];
671 * };
672 *
673 * struct {
674 * S a[2], b;
675 * int c;
676 * } aggregate = {
677 * {
678 * {
679 * {
680 * {1.0, 2.0, 3.0, 4.0}, // a[0].v[0]
681 * {5.0, 6.0, 7.0, 8.0} // a[0].v[1]
682 * } // a[0].v
683 * }, // a[0]
684 * {
685 * {
686 * {1.0, 2.0, 3.0, 4.0}, // a[1].v[0]
687 * {5.0, 6.0, 7.0, 8.0} // a[1].v[1]
688 * } // a[1].v
689 * } // a[1]
690 * }, // a
691 * {
692 * {
693 * {1.0, 2.0, 3.0, 4.0}, // b.v[0]
694 * {5.0, 6.0, 7.0, 8.0} // b.v[1]
695 * } // b.v
696 * }, // b
697 * 4 // c
698 * };
699 *
700 * This pass is necessary because the right-hand side of <type> e = { ... }
701 * doesn't contain sufficient information to determine if the types match.
702 */
703 void
704 _mesa_ast_set_aggregate_type(const glsl_type *type,
705 ast_expression *expr)
706 {
707 ast_aggregate_initializer *ai = (ast_aggregate_initializer *)expr;
708 ai->constructor_type = type;
709
710 /* If the aggregate is an array, recursively set its elements' types. */
711 if (type->is_array()) {
712 /* Each array element has the type type->element_type().
713 *
714 * E.g., if <type> if struct S[2] we want to set each element's type to
715 * struct S.
716 */
717 for (exec_node *expr_node = ai->expressions.head;
718 !expr_node->is_tail_sentinel();
719 expr_node = expr_node->next) {
720 ast_expression *expr = exec_node_data(ast_expression, expr_node,
721 link);
722
723 if (expr->oper == ast_aggregate)
724 _mesa_ast_set_aggregate_type(type->element_type(), expr);
725 }
726
727 /* If the aggregate is a struct, recursively set its fields' types. */
728 } else if (type->is_record()) {
729 exec_node *expr_node = ai->expressions.head;
730
731 /* Iterate through the struct's fields. */
732 for (unsigned i = 0; !expr_node->is_tail_sentinel() && i < type->length;
733 i++, expr_node = expr_node->next) {
734 ast_expression *expr = exec_node_data(ast_expression, expr_node,
735 link);
736
737 if (expr->oper == ast_aggregate) {
738 _mesa_ast_set_aggregate_type(type->fields.structure[i].type, expr);
739 }
740 }
741 /* If the aggregate is a matrix, set its columns' types. */
742 } else if (type->is_matrix()) {
743 for (exec_node *expr_node = ai->expressions.head;
744 !expr_node->is_tail_sentinel();
745 expr_node = expr_node->next) {
746 ast_expression *expr = exec_node_data(ast_expression, expr_node,
747 link);
748
749 if (expr->oper == ast_aggregate)
750 _mesa_ast_set_aggregate_type(type->column_type(), expr);
751 }
752 }
753 }
754
755
756 void
757 _mesa_ast_type_qualifier_print(const struct ast_type_qualifier *q)
758 {
759 if (q->flags.q.constant)
760 printf("const ");
761
762 if (q->flags.q.invariant)
763 printf("invariant ");
764
765 if (q->flags.q.attribute)
766 printf("attribute ");
767
768 if (q->flags.q.varying)
769 printf("varying ");
770
771 if (q->flags.q.in && q->flags.q.out)
772 printf("inout ");
773 else {
774 if (q->flags.q.in)
775 printf("in ");
776
777 if (q->flags.q.out)
778 printf("out ");
779 }
780
781 if (q->flags.q.centroid)
782 printf("centroid ");
783 if (q->flags.q.sample)
784 printf("sample ");
785 if (q->flags.q.uniform)
786 printf("uniform ");
787 if (q->flags.q.smooth)
788 printf("smooth ");
789 if (q->flags.q.flat)
790 printf("flat ");
791 if (q->flags.q.noperspective)
792 printf("noperspective ");
793 }
794
795
796 void
797 ast_node::print(void) const
798 {
799 printf("unhandled node ");
800 }
801
802
803 ast_node::ast_node(void)
804 {
805 this->location.source = 0;
806 this->location.first_line = 0;
807 this->location.first_column = 0;
808 this->location.last_line = 0;
809 this->location.last_column = 0;
810 }
811
812
813 static void
814 ast_opt_array_dimensions_print(const ast_array_specifier *array_specifier)
815 {
816 if (array_specifier)
817 array_specifier->print();
818 }
819
820
821 void
822 ast_compound_statement::print(void) const
823 {
824 printf("{\n");
825
826 foreach_list_const(n, &this->statements) {
827 ast_node *ast = exec_node_data(ast_node, n, link);
828 ast->print();
829 }
830
831 printf("}\n");
832 }
833
834
835 ast_compound_statement::ast_compound_statement(int new_scope,
836 ast_node *statements)
837 {
838 this->new_scope = new_scope;
839
840 if (statements != NULL) {
841 this->statements.push_degenerate_list_at_head(&statements->link);
842 }
843 }
844
845
846 void
847 ast_expression::print(void) const
848 {
849 switch (oper) {
850 case ast_assign:
851 case ast_mul_assign:
852 case ast_div_assign:
853 case ast_mod_assign:
854 case ast_add_assign:
855 case ast_sub_assign:
856 case ast_ls_assign:
857 case ast_rs_assign:
858 case ast_and_assign:
859 case ast_xor_assign:
860 case ast_or_assign:
861 subexpressions[0]->print();
862 printf("%s ", operator_string(oper));
863 subexpressions[1]->print();
864 break;
865
866 case ast_field_selection:
867 subexpressions[0]->print();
868 printf(". %s ", primary_expression.identifier);
869 break;
870
871 case ast_plus:
872 case ast_neg:
873 case ast_bit_not:
874 case ast_logic_not:
875 case ast_pre_inc:
876 case ast_pre_dec:
877 printf("%s ", operator_string(oper));
878 subexpressions[0]->print();
879 break;
880
881 case ast_post_inc:
882 case ast_post_dec:
883 subexpressions[0]->print();
884 printf("%s ", operator_string(oper));
885 break;
886
887 case ast_conditional:
888 subexpressions[0]->print();
889 printf("? ");
890 subexpressions[1]->print();
891 printf(": ");
892 subexpressions[2]->print();
893 break;
894
895 case ast_array_index:
896 subexpressions[0]->print();
897 printf("[ ");
898 subexpressions[1]->print();
899 printf("] ");
900 break;
901
902 case ast_function_call: {
903 subexpressions[0]->print();
904 printf("( ");
905
906 foreach_list_const (n, &this->expressions) {
907 if (n != this->expressions.get_head())
908 printf(", ");
909
910 ast_node *ast = exec_node_data(ast_node, n, link);
911 ast->print();
912 }
913
914 printf(") ");
915 break;
916 }
917
918 case ast_identifier:
919 printf("%s ", primary_expression.identifier);
920 break;
921
922 case ast_int_constant:
923 printf("%d ", primary_expression.int_constant);
924 break;
925
926 case ast_uint_constant:
927 printf("%u ", primary_expression.uint_constant);
928 break;
929
930 case ast_float_constant:
931 printf("%f ", primary_expression.float_constant);
932 break;
933
934 case ast_bool_constant:
935 printf("%s ",
936 primary_expression.bool_constant
937 ? "true" : "false");
938 break;
939
940 case ast_sequence: {
941 printf("( ");
942 foreach_list_const(n, & this->expressions) {
943 if (n != this->expressions.get_head())
944 printf(", ");
945
946 ast_node *ast = exec_node_data(ast_node, n, link);
947 ast->print();
948 }
949 printf(") ");
950 break;
951 }
952
953 case ast_aggregate: {
954 printf("{ ");
955 foreach_list_const(n, & this->expressions) {
956 if (n != this->expressions.get_head())
957 printf(", ");
958
959 ast_node *ast = exec_node_data(ast_node, n, link);
960 ast->print();
961 }
962 printf("} ");
963 break;
964 }
965
966 default:
967 assert(0);
968 break;
969 }
970 }
971
972 ast_expression::ast_expression(int oper,
973 ast_expression *ex0,
974 ast_expression *ex1,
975 ast_expression *ex2) :
976 primary_expression()
977 {
978 this->oper = ast_operators(oper);
979 this->subexpressions[0] = ex0;
980 this->subexpressions[1] = ex1;
981 this->subexpressions[2] = ex2;
982 this->non_lvalue_description = NULL;
983 }
984
985
986 void
987 ast_expression_statement::print(void) const
988 {
989 if (expression)
990 expression->print();
991
992 printf("; ");
993 }
994
995
996 ast_expression_statement::ast_expression_statement(ast_expression *ex) :
997 expression(ex)
998 {
999 /* empty */
1000 }
1001
1002
1003 void
1004 ast_function::print(void) const
1005 {
1006 return_type->print();
1007 printf(" %s (", identifier);
1008
1009 foreach_list_const(n, & this->parameters) {
1010 ast_node *ast = exec_node_data(ast_node, n, link);
1011 ast->print();
1012 }
1013
1014 printf(")");
1015 }
1016
1017
1018 ast_function::ast_function(void)
1019 : return_type(NULL), identifier(NULL), is_definition(false),
1020 signature(NULL)
1021 {
1022 /* empty */
1023 }
1024
1025
1026 void
1027 ast_fully_specified_type::print(void) const
1028 {
1029 _mesa_ast_type_qualifier_print(& qualifier);
1030 specifier->print();
1031 }
1032
1033
1034 void
1035 ast_parameter_declarator::print(void) const
1036 {
1037 type->print();
1038 if (identifier)
1039 printf("%s ", identifier);
1040 ast_opt_array_dimensions_print(array_specifier);
1041 }
1042
1043
1044 void
1045 ast_function_definition::print(void) const
1046 {
1047 prototype->print();
1048 body->print();
1049 }
1050
1051
1052 void
1053 ast_declaration::print(void) const
1054 {
1055 printf("%s ", identifier);
1056 ast_opt_array_dimensions_print(array_specifier);
1057
1058 if (initializer) {
1059 printf("= ");
1060 initializer->print();
1061 }
1062 }
1063
1064
1065 ast_declaration::ast_declaration(const char *identifier,
1066 ast_array_specifier *array_specifier,
1067 ast_expression *initializer)
1068 {
1069 this->identifier = identifier;
1070 this->array_specifier = array_specifier;
1071 this->initializer = initializer;
1072 }
1073
1074
1075 void
1076 ast_declarator_list::print(void) const
1077 {
1078 assert(type || invariant);
1079
1080 if (type)
1081 type->print();
1082 else
1083 printf("invariant ");
1084
1085 foreach_list_const (ptr, & this->declarations) {
1086 if (ptr != this->declarations.get_head())
1087 printf(", ");
1088
1089 ast_node *ast = exec_node_data(ast_node, ptr, link);
1090 ast->print();
1091 }
1092
1093 printf("; ");
1094 }
1095
1096
1097 ast_declarator_list::ast_declarator_list(ast_fully_specified_type *type)
1098 {
1099 this->type = type;
1100 this->invariant = false;
1101 }
1102
1103 void
1104 ast_jump_statement::print(void) const
1105 {
1106 switch (mode) {
1107 case ast_continue:
1108 printf("continue; ");
1109 break;
1110 case ast_break:
1111 printf("break; ");
1112 break;
1113 case ast_return:
1114 printf("return ");
1115 if (opt_return_value)
1116 opt_return_value->print();
1117
1118 printf("; ");
1119 break;
1120 case ast_discard:
1121 printf("discard; ");
1122 break;
1123 }
1124 }
1125
1126
1127 ast_jump_statement::ast_jump_statement(int mode, ast_expression *return_value)
1128 : opt_return_value(NULL)
1129 {
1130 this->mode = ast_jump_modes(mode);
1131
1132 if (mode == ast_return)
1133 opt_return_value = return_value;
1134 }
1135
1136
1137 void
1138 ast_selection_statement::print(void) const
1139 {
1140 printf("if ( ");
1141 condition->print();
1142 printf(") ");
1143
1144 then_statement->print();
1145
1146 if (else_statement) {
1147 printf("else ");
1148 else_statement->print();
1149 }
1150
1151 }
1152
1153
1154 ast_selection_statement::ast_selection_statement(ast_expression *condition,
1155 ast_node *then_statement,
1156 ast_node *else_statement)
1157 {
1158 this->condition = condition;
1159 this->then_statement = then_statement;
1160 this->else_statement = else_statement;
1161 }
1162
1163
1164 void
1165 ast_switch_statement::print(void) const
1166 {
1167 printf("switch ( ");
1168 test_expression->print();
1169 printf(") ");
1170
1171 body->print();
1172 }
1173
1174
1175 ast_switch_statement::ast_switch_statement(ast_expression *test_expression,
1176 ast_node *body)
1177 {
1178 this->test_expression = test_expression;
1179 this->body = body;
1180 }
1181
1182
1183 void
1184 ast_switch_body::print(void) const
1185 {
1186 printf("{\n");
1187 if (stmts != NULL) {
1188 stmts->print();
1189 }
1190 printf("}\n");
1191 }
1192
1193
1194 ast_switch_body::ast_switch_body(ast_case_statement_list *stmts)
1195 {
1196 this->stmts = stmts;
1197 }
1198
1199
1200 void ast_case_label::print(void) const
1201 {
1202 if (test_value != NULL) {
1203 printf("case ");
1204 test_value->print();
1205 printf(": ");
1206 } else {
1207 printf("default: ");
1208 }
1209 }
1210
1211
1212 ast_case_label::ast_case_label(ast_expression *test_value)
1213 {
1214 this->test_value = test_value;
1215 }
1216
1217
1218 void ast_case_label_list::print(void) const
1219 {
1220 foreach_list_const(n, & this->labels) {
1221 ast_node *ast = exec_node_data(ast_node, n, link);
1222 ast->print();
1223 }
1224 printf("\n");
1225 }
1226
1227
1228 ast_case_label_list::ast_case_label_list(void)
1229 {
1230 }
1231
1232
1233 void ast_case_statement::print(void) const
1234 {
1235 labels->print();
1236 foreach_list_const(n, & this->stmts) {
1237 ast_node *ast = exec_node_data(ast_node, n, link);
1238 ast->print();
1239 printf("\n");
1240 }
1241 }
1242
1243
1244 ast_case_statement::ast_case_statement(ast_case_label_list *labels)
1245 {
1246 this->labels = labels;
1247 }
1248
1249
1250 void ast_case_statement_list::print(void) const
1251 {
1252 foreach_list_const(n, & this->cases) {
1253 ast_node *ast = exec_node_data(ast_node, n, link);
1254 ast->print();
1255 }
1256 }
1257
1258
1259 ast_case_statement_list::ast_case_statement_list(void)
1260 {
1261 }
1262
1263
1264 void
1265 ast_iteration_statement::print(void) const
1266 {
1267 switch (mode) {
1268 case ast_for:
1269 printf("for( ");
1270 if (init_statement)
1271 init_statement->print();
1272 printf("; ");
1273
1274 if (condition)
1275 condition->print();
1276 printf("; ");
1277
1278 if (rest_expression)
1279 rest_expression->print();
1280 printf(") ");
1281
1282 body->print();
1283 break;
1284
1285 case ast_while:
1286 printf("while ( ");
1287 if (condition)
1288 condition->print();
1289 printf(") ");
1290 body->print();
1291 break;
1292
1293 case ast_do_while:
1294 printf("do ");
1295 body->print();
1296 printf("while ( ");
1297 if (condition)
1298 condition->print();
1299 printf("); ");
1300 break;
1301 }
1302 }
1303
1304
1305 ast_iteration_statement::ast_iteration_statement(int mode,
1306 ast_node *init,
1307 ast_node *condition,
1308 ast_expression *rest_expression,
1309 ast_node *body)
1310 {
1311 this->mode = ast_iteration_modes(mode);
1312 this->init_statement = init;
1313 this->condition = condition;
1314 this->rest_expression = rest_expression;
1315 this->body = body;
1316 }
1317
1318
1319 void
1320 ast_struct_specifier::print(void) const
1321 {
1322 printf("struct %s { ", name);
1323 foreach_list_const(n, &this->declarations) {
1324 ast_node *ast = exec_node_data(ast_node, n, link);
1325 ast->print();
1326 }
1327 printf("} ");
1328 }
1329
1330
1331 ast_struct_specifier::ast_struct_specifier(const char *identifier,
1332 ast_declarator_list *declarator_list)
1333 {
1334 if (identifier == NULL) {
1335 static unsigned anon_count = 1;
1336 identifier = ralloc_asprintf(this, "#anon_struct_%04x", anon_count);
1337 anon_count++;
1338 }
1339 name = identifier;
1340 this->declarations.push_degenerate_list_at_head(&declarator_list->link);
1341 is_declaration = true;
1342 }
1343
1344 static void
1345 set_shader_inout_layout(struct gl_shader *shader,
1346 struct _mesa_glsl_parse_state *state)
1347 {
1348 if (shader->Stage != MESA_SHADER_GEOMETRY) {
1349 /* Should have been prevented by the parser. */
1350 assert(!state->in_qualifier->flags.i);
1351 assert(!state->out_qualifier->flags.i);
1352 }
1353
1354 if (shader->Stage != MESA_SHADER_COMPUTE) {
1355 /* Should have been prevented by the parser. */
1356 assert(!state->cs_input_local_size_specified);
1357 }
1358
1359 switch (shader->Stage) {
1360 case MESA_SHADER_GEOMETRY:
1361 shader->Geom.VerticesOut = 0;
1362 if (state->out_qualifier->flags.q.max_vertices)
1363 shader->Geom.VerticesOut = state->out_qualifier->max_vertices;
1364
1365 if (state->gs_input_prim_type_specified) {
1366 shader->Geom.InputType = state->in_qualifier->prim_type;
1367 } else {
1368 shader->Geom.InputType = PRIM_UNKNOWN;
1369 }
1370
1371 if (state->out_qualifier->flags.q.prim_type) {
1372 shader->Geom.OutputType = state->out_qualifier->prim_type;
1373 } else {
1374 shader->Geom.OutputType = PRIM_UNKNOWN;
1375 }
1376
1377 shader->Geom.Invocations = 0;
1378 if (state->in_qualifier->flags.q.invocations)
1379 shader->Geom.Invocations = state->in_qualifier->invocations;
1380 break;
1381
1382 case MESA_SHADER_COMPUTE:
1383 if (state->cs_input_local_size_specified) {
1384 for (int i = 0; i < 3; i++)
1385 shader->Comp.LocalSize[i] = state->cs_input_local_size[i];
1386 } else {
1387 for (int i = 0; i < 3; i++)
1388 shader->Comp.LocalSize[i] = 0;
1389 }
1390 break;
1391
1392 default:
1393 /* Nothing to do. */
1394 break;
1395 }
1396 }
1397
1398 extern "C" {
1399
1400 void
1401 _mesa_glsl_compile_shader(struct gl_context *ctx, struct gl_shader *shader,
1402 bool dump_ast, bool dump_hir)
1403 {
1404 struct _mesa_glsl_parse_state *state =
1405 new(shader) _mesa_glsl_parse_state(ctx, shader->Stage, shader);
1406 const char *source = shader->Source;
1407
1408 state->error = glcpp_preprocess(state, &source, &state->info_log,
1409 &ctx->Extensions, ctx);
1410
1411 if (!state->error) {
1412 _mesa_glsl_lexer_ctor(state, source);
1413 _mesa_glsl_parse(state);
1414 _mesa_glsl_lexer_dtor(state);
1415 }
1416
1417 if (dump_ast) {
1418 foreach_list_const(n, &state->translation_unit) {
1419 ast_node *ast = exec_node_data(ast_node, n, link);
1420 ast->print();
1421 }
1422 printf("\n\n");
1423 }
1424
1425 ralloc_free(shader->ir);
1426 shader->ir = new(shader) exec_list;
1427 if (!state->error && !state->translation_unit.is_empty())
1428 _mesa_ast_to_hir(shader->ir, state);
1429
1430 if (!state->error) {
1431 validate_ir_tree(shader->ir);
1432
1433 /* Print out the unoptimized IR. */
1434 if (dump_hir) {
1435 _mesa_print_ir(stdout, shader->ir, state);
1436 }
1437 }
1438
1439
1440 if (!state->error && !shader->ir->is_empty()) {
1441 struct gl_shader_compiler_options *options =
1442 &ctx->ShaderCompilerOptions[shader->Stage];
1443
1444 /* Do some optimization at compile time to reduce shader IR size
1445 * and reduce later work if the same shader is linked multiple times
1446 */
1447 while (do_common_optimization(shader->ir, false, false, 32, options,
1448 ctx->Const.NativeIntegers))
1449 ;
1450
1451 validate_ir_tree(shader->ir);
1452 }
1453
1454 if (shader->InfoLog)
1455 ralloc_free(shader->InfoLog);
1456
1457 shader->symbols = state->symbols;
1458 shader->CompileStatus = !state->error;
1459 shader->InfoLog = state->info_log;
1460 shader->Version = state->language_version;
1461 shader->IsES = state->es_shader;
1462 shader->uses_builtin_functions = state->uses_builtin_functions;
1463
1464 if (!state->error)
1465 set_shader_inout_layout(shader, state);
1466
1467 /* Retain any live IR, but trash the rest. */
1468 reparent_ir(shader->ir, shader->ir);
1469
1470 ralloc_free(state);
1471 }
1472
1473 } /* extern "C" */
1474 /**
1475 * Do the set of common optimizations passes
1476 *
1477 * \param ir List of instructions to be optimized
1478 * \param linked Is the shader linked? This enables
1479 * optimizations passes that remove code at
1480 * global scope and could cause linking to
1481 * fail.
1482 * \param uniform_locations_assigned Have locations already been assigned for
1483 * uniforms? This prevents the declarations
1484 * of unused uniforms from being removed.
1485 * The setting of this flag only matters if
1486 * \c linked is \c true.
1487 * \param max_unroll_iterations Maximum number of loop iterations to be
1488 * unrolled. Setting to 0 disables loop
1489 * unrolling.
1490 * \param options The driver's preferred shader options.
1491 */
1492 bool
1493 do_common_optimization(exec_list *ir, bool linked,
1494 bool uniform_locations_assigned,
1495 unsigned max_unroll_iterations,
1496 const struct gl_shader_compiler_options *options,
1497 bool native_integers)
1498 {
1499 GLboolean progress = GL_FALSE;
1500
1501 progress = lower_instructions(ir, SUB_TO_ADD_NEG) || progress;
1502
1503 if (linked) {
1504 progress = do_function_inlining(ir) || progress;
1505 progress = do_dead_functions(ir) || progress;
1506 progress = do_structure_splitting(ir) || progress;
1507 }
1508 progress = do_if_simplification(ir) || progress;
1509 progress = opt_flatten_nested_if_blocks(ir) || progress;
1510 progress = do_copy_propagation(ir) || progress;
1511 progress = do_copy_propagation_elements(ir) || progress;
1512
1513 if (options->OptimizeForAOS && !linked)
1514 progress = opt_flip_matrices(ir) || progress;
1515
1516 if (linked && options->OptimizeForAOS) {
1517 progress = do_vectorize(ir) || progress;
1518 }
1519
1520 if (linked)
1521 progress = do_dead_code(ir, uniform_locations_assigned) || progress;
1522 else
1523 progress = do_dead_code_unlinked(ir) || progress;
1524 progress = do_dead_code_local(ir) || progress;
1525 progress = do_tree_grafting(ir) || progress;
1526 progress = do_constant_propagation(ir) || progress;
1527 if (linked)
1528 progress = do_constant_variable(ir) || progress;
1529 else
1530 progress = do_constant_variable_unlinked(ir) || progress;
1531 progress = do_constant_folding(ir) || progress;
1532 progress = do_cse(ir) || progress;
1533 progress = do_algebraic(ir, native_integers) || progress;
1534 progress = do_lower_jumps(ir) || progress;
1535 progress = do_vec_index_to_swizzle(ir) || progress;
1536 progress = lower_vector_insert(ir, false) || progress;
1537 progress = do_swizzle_swizzle(ir) || progress;
1538 progress = do_noop_swizzle(ir) || progress;
1539
1540 progress = optimize_split_arrays(ir, linked) || progress;
1541 progress = optimize_redundant_jumps(ir) || progress;
1542
1543 loop_state *ls = analyze_loop_variables(ir);
1544 if (ls->loop_found) {
1545 progress = set_loop_controls(ir, ls) || progress;
1546 progress = unroll_loops(ir, ls, max_unroll_iterations) || progress;
1547 }
1548 delete ls;
1549
1550 return progress;
1551 }
1552
1553 extern "C" {
1554
1555 /**
1556 * To be called at GL teardown time, this frees compiler datastructures.
1557 *
1558 * After calling this, any previously compiled shaders and shader
1559 * programs would be invalid. So this should happen at approximately
1560 * program exit.
1561 */
1562 void
1563 _mesa_destroy_shader_compiler(void)
1564 {
1565 _mesa_destroy_shader_compiler_caches();
1566
1567 _mesa_glsl_release_types();
1568 }
1569
1570 /**
1571 * Releases compiler caches to trade off performance for memory.
1572 *
1573 * Intended to be used with glReleaseShaderCompiler().
1574 */
1575 void
1576 _mesa_destroy_shader_compiler_caches(void)
1577 {
1578 _mesa_glsl_release_builtin_functions();
1579 }
1580
1581 }