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