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 <inttypes.h> /* for PRIx64 macro */
24 #include <stdio.h>
25 #include <stdarg.h>
26 #include <string.h>
27 #include <assert.h>
28
29 #include "main/context.h"
30 #include "main/debug_output.h"
31 #include "main/formats.h"
32 #include "main/shaderobj.h"
33 #include "util/u_atomic.h" /* for p_atomic_cmpxchg */
34 #include "util/ralloc.h"
35 #include "util/disk_cache.h"
36 #include "util/mesa-blake3.h"
37 #include "ast.h"
38 #include "glsl_parser_extras.h"
39 #include "glsl_parser.h"
40 #include "glsl_to_nir.h"
41 #include "ir_optimization.h"
42 #include "builtin_functions.h"
43
44 /**
45 * Format a short human-readable description of the given GLSL version.
46 */
47 const char *
glsl_compute_version_string(void * mem_ctx,bool is_es,unsigned version)48 glsl_compute_version_string(void *mem_ctx, bool is_es, unsigned version)
49 {
50 return ralloc_asprintf(mem_ctx, "GLSL%s %d.%02d", is_es ? " ES" : "",
51 version / 100, version % 100);
52 }
53
54
55 static const unsigned known_desktop_glsl_versions[] =
56 { 110, 120, 130, 140, 150, 330, 400, 410, 420, 430, 440, 450, 460 };
57 static const unsigned known_desktop_gl_versions[] =
58 { 20, 21, 30, 31, 32, 33, 40, 41, 42, 43, 44, 45, 46 };
59
60
_mesa_glsl_parse_state(struct gl_context * _ctx,gl_shader_stage stage,void * mem_ctx)61 _mesa_glsl_parse_state::_mesa_glsl_parse_state(struct gl_context *_ctx,
62 gl_shader_stage stage,
63 void *mem_ctx)
64 : ctx(_ctx), exts(&_ctx->Extensions), consts(&_ctx->Const),
65 api(_ctx->API), cs_input_local_size_specified(false), cs_input_local_size(),
66 switch_state(), warnings_enabled(true)
67 {
68 assert(stage < MESA_SHADER_STAGES);
69 this->stage = stage;
70
71 this->scanner = NULL;
72 this->translation_unit.make_empty();
73 this->symbols = new(mem_ctx) glsl_symbol_table;
74
75 this->linalloc = linear_context(this);
76
77 this->info_log = ralloc_strdup(mem_ctx, "");
78 this->error = false;
79 this->loop_nesting_ast = NULL;
80
81 this->uses_builtin_functions = false;
82
83 /* Set default language version and extensions */
84 this->language_version = 110;
85 this->forced_language_version = ctx->Const.ForceGLSLVersion;
86 if (ctx->Const.GLSLZeroInit == 1) {
87 this->zero_init = (1u << ir_var_auto) | (1u << ir_var_temporary) | (1u << ir_var_shader_out);
88 } else if (ctx->Const.GLSLZeroInit == 2) {
89 this->zero_init = (1u << ir_var_auto) | (1u << ir_var_temporary) | (1u << ir_var_function_out);
90 } else {
91 this->zero_init = 0;
92 }
93 this->gl_version = 20;
94 this->compat_shader = true;
95 this->es_shader = false;
96 this->ARB_texture_rectangle_enable = true;
97
98 /* OpenGL ES 2.0 has different defaults from desktop GL. */
99 if (_mesa_is_gles2(ctx)) {
100 this->language_version = 100;
101 this->es_shader = true;
102 this->ARB_texture_rectangle_enable = false;
103 }
104
105 this->extensions = &ctx->Extensions;
106
107 this->Const.MaxLights = ctx->Const.MaxLights;
108 this->Const.MaxClipPlanes = ctx->Const.MaxClipPlanes;
109 this->Const.MaxTextureUnits = ctx->Const.MaxTextureUnits;
110 this->Const.MaxTextureCoords = ctx->Const.MaxTextureCoordUnits;
111 this->Const.MaxVertexAttribs = ctx->Const.Program[MESA_SHADER_VERTEX].MaxAttribs;
112 this->Const.MaxVertexUniformComponents = ctx->Const.Program[MESA_SHADER_VERTEX].MaxUniformComponents;
113 this->Const.MaxVertexTextureImageUnits = ctx->Const.Program[MESA_SHADER_VERTEX].MaxTextureImageUnits;
114 this->Const.MaxCombinedTextureImageUnits = ctx->Const.MaxCombinedTextureImageUnits;
115 this->Const.MaxTextureImageUnits = ctx->Const.Program[MESA_SHADER_FRAGMENT].MaxTextureImageUnits;
116 this->Const.MaxFragmentUniformComponents = ctx->Const.Program[MESA_SHADER_FRAGMENT].MaxUniformComponents;
117 this->Const.MinProgramTexelOffset = ctx->Const.MinProgramTexelOffset;
118 this->Const.MaxProgramTexelOffset = ctx->Const.MaxProgramTexelOffset;
119
120 this->Const.MaxDrawBuffers = ctx->Const.MaxDrawBuffers;
121
122 this->Const.MaxDualSourceDrawBuffers = ctx->Const.MaxDualSourceDrawBuffers;
123
124 /* 1.50 constants */
125 this->Const.MaxVertexOutputComponents = ctx->Const.Program[MESA_SHADER_VERTEX].MaxOutputComponents;
126 this->Const.MaxGeometryInputComponents = ctx->Const.Program[MESA_SHADER_GEOMETRY].MaxInputComponents;
127 this->Const.MaxGeometryOutputComponents = ctx->Const.Program[MESA_SHADER_GEOMETRY].MaxOutputComponents;
128 this->Const.MaxGeometryShaderInvocations = ctx->Const.MaxGeometryShaderInvocations;
129 this->Const.MaxFragmentInputComponents = ctx->Const.Program[MESA_SHADER_FRAGMENT].MaxInputComponents;
130 this->Const.MaxGeometryTextureImageUnits = ctx->Const.Program[MESA_SHADER_GEOMETRY].MaxTextureImageUnits;
131 this->Const.MaxGeometryOutputVertices = ctx->Const.MaxGeometryOutputVertices;
132 this->Const.MaxGeometryTotalOutputComponents = ctx->Const.MaxGeometryTotalOutputComponents;
133 this->Const.MaxGeometryUniformComponents = ctx->Const.Program[MESA_SHADER_GEOMETRY].MaxUniformComponents;
134
135 this->Const.MaxVertexAtomicCounters = ctx->Const.Program[MESA_SHADER_VERTEX].MaxAtomicCounters;
136 this->Const.MaxTessControlAtomicCounters = ctx->Const.Program[MESA_SHADER_TESS_CTRL].MaxAtomicCounters;
137 this->Const.MaxTessEvaluationAtomicCounters = ctx->Const.Program[MESA_SHADER_TESS_EVAL].MaxAtomicCounters;
138 this->Const.MaxGeometryAtomicCounters = ctx->Const.Program[MESA_SHADER_GEOMETRY].MaxAtomicCounters;
139 this->Const.MaxFragmentAtomicCounters = ctx->Const.Program[MESA_SHADER_FRAGMENT].MaxAtomicCounters;
140 this->Const.MaxComputeAtomicCounters = ctx->Const.Program[MESA_SHADER_COMPUTE].MaxAtomicCounters;
141 this->Const.MaxCombinedAtomicCounters = ctx->Const.MaxCombinedAtomicCounters;
142 this->Const.MaxAtomicBufferBindings = ctx->Const.MaxAtomicBufferBindings;
143 this->Const.MaxVertexAtomicCounterBuffers =
144 ctx->Const.Program[MESA_SHADER_VERTEX].MaxAtomicBuffers;
145 this->Const.MaxTessControlAtomicCounterBuffers =
146 ctx->Const.Program[MESA_SHADER_TESS_CTRL].MaxAtomicBuffers;
147 this->Const.MaxTessEvaluationAtomicCounterBuffers =
148 ctx->Const.Program[MESA_SHADER_TESS_EVAL].MaxAtomicBuffers;
149 this->Const.MaxGeometryAtomicCounterBuffers =
150 ctx->Const.Program[MESA_SHADER_GEOMETRY].MaxAtomicBuffers;
151 this->Const.MaxFragmentAtomicCounterBuffers =
152 ctx->Const.Program[MESA_SHADER_FRAGMENT].MaxAtomicBuffers;
153 this->Const.MaxComputeAtomicCounterBuffers =
154 ctx->Const.Program[MESA_SHADER_COMPUTE].MaxAtomicBuffers;
155 this->Const.MaxCombinedAtomicCounterBuffers =
156 ctx->Const.MaxCombinedAtomicBuffers;
157 this->Const.MaxAtomicCounterBufferSize =
158 ctx->Const.MaxAtomicBufferSize;
159
160 /* ARB_enhanced_layouts constants */
161 this->Const.MaxTransformFeedbackBuffers = ctx->Const.MaxTransformFeedbackBuffers;
162 this->Const.MaxTransformFeedbackInterleavedComponents = ctx->Const.MaxTransformFeedbackInterleavedComponents;
163
164 /* Compute shader constants */
165 for (unsigned i = 0; i < ARRAY_SIZE(this->Const.MaxComputeWorkGroupCount); i++)
166 this->Const.MaxComputeWorkGroupCount[i] = ctx->Const.MaxComputeWorkGroupCount[i];
167 for (unsigned i = 0; i < ARRAY_SIZE(this->Const.MaxComputeWorkGroupSize); i++)
168 this->Const.MaxComputeWorkGroupSize[i] = ctx->Const.MaxComputeWorkGroupSize[i];
169
170 this->Const.MaxComputeTextureImageUnits = ctx->Const.Program[MESA_SHADER_COMPUTE].MaxTextureImageUnits;
171 this->Const.MaxComputeUniformComponents = ctx->Const.Program[MESA_SHADER_COMPUTE].MaxUniformComponents;
172
173 this->Const.MaxImageUnits = ctx->Const.MaxImageUnits;
174 this->Const.MaxCombinedShaderOutputResources = ctx->Const.MaxCombinedShaderOutputResources;
175 this->Const.MaxImageSamples = ctx->Const.MaxImageSamples;
176 this->Const.MaxVertexImageUniforms = ctx->Const.Program[MESA_SHADER_VERTEX].MaxImageUniforms;
177 this->Const.MaxTessControlImageUniforms = ctx->Const.Program[MESA_SHADER_TESS_CTRL].MaxImageUniforms;
178 this->Const.MaxTessEvaluationImageUniforms = ctx->Const.Program[MESA_SHADER_TESS_EVAL].MaxImageUniforms;
179 this->Const.MaxGeometryImageUniforms = ctx->Const.Program[MESA_SHADER_GEOMETRY].MaxImageUniforms;
180 this->Const.MaxFragmentImageUniforms = ctx->Const.Program[MESA_SHADER_FRAGMENT].MaxImageUniforms;
181 this->Const.MaxComputeImageUniforms = ctx->Const.Program[MESA_SHADER_COMPUTE].MaxImageUniforms;
182 this->Const.MaxCombinedImageUniforms = ctx->Const.MaxCombinedImageUniforms;
183
184 /* ARB_viewport_array */
185 this->Const.MaxViewports = ctx->Const.MaxViewports;
186
187 /* tessellation shader constants */
188 this->Const.MaxPatchVertices = ctx->Const.MaxPatchVertices;
189 this->Const.MaxTessGenLevel = ctx->Const.MaxTessGenLevel;
190 this->Const.MaxTessControlInputComponents = ctx->Const.Program[MESA_SHADER_TESS_CTRL].MaxInputComponents;
191 this->Const.MaxTessControlOutputComponents = ctx->Const.Program[MESA_SHADER_TESS_CTRL].MaxOutputComponents;
192 this->Const.MaxTessControlTextureImageUnits = ctx->Const.Program[MESA_SHADER_TESS_CTRL].MaxTextureImageUnits;
193 this->Const.MaxTessEvaluationInputComponents = ctx->Const.Program[MESA_SHADER_TESS_EVAL].MaxInputComponents;
194 this->Const.MaxTessEvaluationOutputComponents = ctx->Const.Program[MESA_SHADER_TESS_EVAL].MaxOutputComponents;
195 this->Const.MaxTessEvaluationTextureImageUnits = ctx->Const.Program[MESA_SHADER_TESS_EVAL].MaxTextureImageUnits;
196 this->Const.MaxTessPatchComponents = ctx->Const.MaxTessPatchComponents;
197 this->Const.MaxTessControlTotalOutputComponents = ctx->Const.MaxTessControlTotalOutputComponents;
198 this->Const.MaxTessControlUniformComponents = ctx->Const.Program[MESA_SHADER_TESS_CTRL].MaxUniformComponents;
199 this->Const.MaxTessEvaluationUniformComponents = ctx->Const.Program[MESA_SHADER_TESS_EVAL].MaxUniformComponents;
200
201 /* GL 4.5 / OES_sample_variables */
202 this->Const.MaxSamples = ctx->Const.MaxSamples;
203
204 this->current_function = NULL;
205 this->toplevel_ir = NULL;
206 this->found_return = false;
207 this->found_begin_interlock = false;
208 this->found_end_interlock = false;
209 this->all_invariant = false;
210 this->user_structures = NULL;
211 this->num_user_structures = 0;
212 this->num_subroutines = 0;
213 this->subroutines = NULL;
214 this->num_subroutine_types = 0;
215 this->subroutine_types = NULL;
216
217 /* supported_versions should be large enough to support the known desktop
218 * GLSL versions plus 4 GLES versions (ES 1.00, ES 3.00, ES 3.10, ES 3.20)
219 */
220 STATIC_ASSERT((ARRAY_SIZE(known_desktop_glsl_versions) + 4) ==
221 ARRAY_SIZE(this->supported_versions));
222
223 /* Populate the list of supported GLSL versions */
224 /* FINISHME: Once the OpenGL 3.0 'forward compatible' context or
225 * the OpenGL 3.2 Core context is supported, this logic will need
226 * change. Older versions of GLSL are no longer supported
227 * outside the compatibility contexts of 3.x.
228 */
229 this->num_supported_versions = 0;
230 if (_mesa_is_desktop_gl(ctx)) {
231 for (unsigned i = 0; i < ARRAY_SIZE(known_desktop_glsl_versions); i++) {
232 if (known_desktop_glsl_versions[i] <= ctx->Const.GLSLVersion) {
233 this->supported_versions[this->num_supported_versions].ver
234 = known_desktop_glsl_versions[i];
235 this->supported_versions[this->num_supported_versions].gl_ver
236 = known_desktop_gl_versions[i];
237 this->supported_versions[this->num_supported_versions].es = false;
238 this->num_supported_versions++;
239 }
240 }
241 }
242 if (_mesa_is_gles2_compatible(ctx)) {
243 this->supported_versions[this->num_supported_versions].ver = 100;
244 this->supported_versions[this->num_supported_versions].gl_ver = 20;
245 this->supported_versions[this->num_supported_versions].es = true;
246 this->num_supported_versions++;
247 }
248 if (_mesa_is_gles3_compatible(ctx)) {
249 this->supported_versions[this->num_supported_versions].ver = 300;
250 this->supported_versions[this->num_supported_versions].gl_ver = 30;
251 this->supported_versions[this->num_supported_versions].es = true;
252 this->num_supported_versions++;
253 }
254 if (_mesa_is_gles31_compatible(ctx)) {
255 this->supported_versions[this->num_supported_versions].ver = 310;
256 this->supported_versions[this->num_supported_versions].gl_ver = 31;
257 this->supported_versions[this->num_supported_versions].es = true;
258 this->num_supported_versions++;
259 }
260 if (_mesa_is_gles32_compatible(ctx)) {
261 this->supported_versions[this->num_supported_versions].ver = 320;
262 this->supported_versions[this->num_supported_versions].gl_ver = 32;
263 this->supported_versions[this->num_supported_versions].es = true;
264 this->num_supported_versions++;
265 }
266
267 /* Create a string for use in error messages to tell the user which GLSL
268 * versions are supported.
269 */
270 char *supported = ralloc_strdup(this, "");
271 for (unsigned i = 0; i < this->num_supported_versions; i++) {
272 unsigned ver = this->supported_versions[i].ver;
273 const char *const prefix = (i == 0)
274 ? ""
275 : ((i == this->num_supported_versions - 1) ? ", and " : ", ");
276 const char *const suffix = (this->supported_versions[i].es) ? " ES" : "";
277
278 ralloc_asprintf_append(& supported, "%s%u.%02u%s",
279 prefix,
280 ver / 100, ver % 100,
281 suffix);
282 }
283
284 this->supported_version_string = supported;
285
286 if (ctx->Const.ForceGLSLExtensionsWarn)
287 _mesa_glsl_process_extension("all", NULL, "warn", NULL, this);
288
289 this->default_uniform_qualifier = new(this) ast_type_qualifier();
290 this->default_uniform_qualifier->flags.q.shared = 1;
291 this->default_uniform_qualifier->flags.q.column_major = 1;
292
293 this->default_shader_storage_qualifier = new(this) ast_type_qualifier();
294 this->default_shader_storage_qualifier->flags.q.shared = 1;
295 this->default_shader_storage_qualifier->flags.q.column_major = 1;
296
297 this->fs_uses_gl_fragcoord = false;
298 this->fs_redeclares_gl_fragcoord = false;
299 this->fs_origin_upper_left = false;
300 this->fs_pixel_center_integer = false;
301 this->fs_redeclares_gl_fragcoord_with_no_layout_qualifiers = false;
302
303 this->gs_input_prim_type_specified = false;
304 this->tcs_output_vertices_specified = false;
305 this->gs_input_size = 0;
306 this->in_qualifier = new(this) ast_type_qualifier();
307 this->out_qualifier = new(this) ast_type_qualifier();
308 this->fs_early_fragment_tests = false;
309 this->fs_inner_coverage = false;
310 this->fs_post_depth_coverage = false;
311 this->fs_pixel_interlock_ordered = false;
312 this->fs_pixel_interlock_unordered = false;
313 this->fs_sample_interlock_ordered = false;
314 this->fs_sample_interlock_unordered = false;
315 this->fs_blend_support = 0;
316 memset(this->atomic_counter_offsets, 0,
317 sizeof(this->atomic_counter_offsets));
318 this->allow_extension_directive_midshader =
319 ctx->Const.AllowGLSLExtensionDirectiveMidShader;
320 this->alias_shader_extension =
321 ctx->Const.AliasShaderExtension;
322 this->allow_vertex_texture_bias =
323 ctx->Const.AllowVertexTextureBias;
324 this->allow_glsl_120_subset_in_110 =
325 ctx->Const.AllowGLSL120SubsetIn110;
326 this->allow_builtin_variable_redeclaration =
327 ctx->Const.AllowGLSLBuiltinVariableRedeclaration;
328 this->ignore_write_to_readonly_var =
329 ctx->Const.GLSLIgnoreWriteToReadonlyVar;
330
331 this->cs_input_local_size_variable_specified = false;
332
333 /* ARB_bindless_texture */
334 this->bindless_sampler_specified = false;
335 this->bindless_image_specified = false;
336 this->bound_sampler_specified = false;
337 this->bound_image_specified = false;
338
339 this->language_version = this->forced_language_version ?
340 this->forced_language_version : this->language_version;
341 set_valid_gl_and_glsl_versions(NULL);
342 }
343
344 /**
345 * Determine whether the current GLSL version is sufficiently high to support
346 * a certain feature, and generate an error message if it isn't.
347 *
348 * \param required_glsl_version and \c required_glsl_es_version are
349 * interpreted as they are in _mesa_glsl_parse_state::is_version().
350 *
351 * \param locp is the parser location where the error should be reported.
352 *
353 * \param fmt (and additional arguments) constitute a printf-style error
354 * message to report if the version check fails. Information about the
355 * current and required GLSL versions will be appended. So, for example, if
356 * the GLSL version being compiled is 1.20, and check_version(130, 300, locp,
357 * "foo unsupported") is called, the error message will be "foo unsupported in
358 * GLSL 1.20 (GLSL 1.30 or GLSL 3.00 ES required)".
359 */
360 bool
check_version(unsigned required_glsl_version,unsigned required_glsl_es_version,YYLTYPE * locp,const char * fmt,...)361 _mesa_glsl_parse_state::check_version(unsigned required_glsl_version,
362 unsigned required_glsl_es_version,
363 YYLTYPE *locp, const char *fmt, ...)
364 {
365 if (this->is_version(required_glsl_version, required_glsl_es_version))
366 return true;
367
368 va_list args;
369 va_start(args, fmt);
370 char *problem = ralloc_vasprintf(this, fmt, args);
371 va_end(args);
372 const char *glsl_version_string
373 = glsl_compute_version_string(this, false, required_glsl_version);
374 const char *glsl_es_version_string
375 = glsl_compute_version_string(this, true, required_glsl_es_version);
376 const char *requirement_string = "";
377 if (required_glsl_version && required_glsl_es_version) {
378 requirement_string = ralloc_asprintf(this, " (%s or %s required)",
379 glsl_version_string,
380 glsl_es_version_string);
381 } else if (required_glsl_version) {
382 requirement_string = ralloc_asprintf(this, " (%s required)",
383 glsl_version_string);
384 } else if (required_glsl_es_version) {
385 requirement_string = ralloc_asprintf(this, " (%s required)",
386 glsl_es_version_string);
387 }
388 _mesa_glsl_error(locp, this, "%s in %s%s",
389 problem, this->get_version_string(),
390 requirement_string);
391
392 return false;
393 }
394
395 /**
396 * This makes sure any GLSL versions defined or overridden are valid. If not it
397 * sets a valid value.
398 */
399 void
set_valid_gl_and_glsl_versions(YYLTYPE * locp)400 _mesa_glsl_parse_state::set_valid_gl_and_glsl_versions(YYLTYPE *locp)
401 {
402 bool supported = false;
403 for (unsigned i = 0; i < this->num_supported_versions; i++) {
404 if (this->supported_versions[i].ver == this->language_version
405 && this->supported_versions[i].es == this->es_shader) {
406 this->gl_version = this->supported_versions[i].gl_ver;
407 supported = true;
408 break;
409 }
410 }
411
412 if (!supported) {
413 if (locp) {
414 _mesa_glsl_error(locp, this, "%s is not supported. "
415 "Supported versions are: %s",
416 this->get_version_string(),
417 this->supported_version_string);
418 }
419
420 /* On exit, the language_version must be set to a valid value.
421 * Later calls to _mesa_glsl_initialize_types will misbehave if
422 * the version is invalid.
423 */
424 switch (this->api) {
425 case API_OPENGL_COMPAT:
426 case API_OPENGL_CORE:
427 this->language_version = this->consts->GLSLVersion;
428 break;
429
430 case API_OPENGLES:
431 FALLTHROUGH;
432
433 case API_OPENGLES2:
434 this->language_version = 100;
435 break;
436 }
437 }
438 }
439
440 /**
441 * Process a GLSL #version directive.
442 *
443 * \param version is the integer that follows the #version token.
444 *
445 * \param ident is a string identifier that follows the integer, if any is
446 * present. Otherwise NULL.
447 */
448 void
process_version_directive(YYLTYPE * locp,int version,const char * ident)449 _mesa_glsl_parse_state::process_version_directive(YYLTYPE *locp, int version,
450 const char *ident)
451 {
452 bool es_token_present = false;
453 bool compat_token_present = false;
454 if (ident) {
455 if (strcmp(ident, "es") == 0) {
456 es_token_present = true;
457 } else if (version >= 150) {
458 if (strcmp(ident, "core") == 0) {
459 /* Accept the token. There's no need to record that this is
460 * a core profile shader since that's the only profile we support.
461 */
462 } else if (strcmp(ident, "compatibility") == 0) {
463 compat_token_present = true;
464
465 if (this->api != API_OPENGL_COMPAT &&
466 !this->consts->AllowGLSLCompatShaders) {
467 _mesa_glsl_error(locp, this,
468 "the compatibility profile is not supported");
469 }
470 } else {
471 _mesa_glsl_error(locp, this,
472 "\"%s\" is not a valid shading language profile; "
473 "if present, it must be \"core\"", ident);
474 }
475 } else {
476 _mesa_glsl_error(locp, this,
477 "illegal text following version number");
478 }
479 }
480
481 this->es_shader = es_token_present;
482 if (version == 100) {
483 if (es_token_present) {
484 _mesa_glsl_error(locp, this,
485 "GLSL 1.00 ES should be selected using "
486 "`#version 100'");
487 } else {
488 this->es_shader = true;
489 }
490 }
491
492 if (this->es_shader) {
493 this->ARB_texture_rectangle_enable = false;
494 }
495
496 if (this->forced_language_version)
497 this->language_version = this->forced_language_version;
498 else
499 this->language_version = version;
500
501 this->compat_shader = compat_token_present ||
502 this->consts->ForceCompatShaders ||
503 (this->api == API_OPENGL_COMPAT &&
504 this->language_version == 140) ||
505 (!this->es_shader && this->language_version < 140);
506
507 set_valid_gl_and_glsl_versions(locp);
508 }
509
510
511 /* This helper function will append the given message to the shader's
512 info log and report it via GL_ARB_debug_output. Per that extension,
513 'type' is one of the enum values classifying the message, and
514 'id' is the implementation-defined ID of the given message. */
515 static void
_mesa_glsl_msg(const YYLTYPE * locp,_mesa_glsl_parse_state * state,GLenum type,const char * fmt,va_list ap)516 _mesa_glsl_msg(const YYLTYPE *locp, _mesa_glsl_parse_state *state,
517 GLenum type, const char *fmt, va_list ap)
518 {
519 bool error = (type == MESA_DEBUG_TYPE_ERROR);
520 GLuint msg_id = 0;
521
522 assert(state->info_log != NULL);
523
524 /* Get the offset that the new message will be written to. */
525 int msg_offset = strlen(state->info_log);
526
527 if (locp->path) {
528 ralloc_asprintf_append(&state->info_log, "\"%s\"", locp->path);
529 } else {
530 ralloc_asprintf_append(&state->info_log, "%u", locp->source);
531 }
532 ralloc_asprintf_append(&state->info_log, ":%u(%u): %s: ",
533 locp->first_line, locp->first_column,
534 error ? "error" : "warning");
535
536 ralloc_vasprintf_append(&state->info_log, fmt, ap);
537
538 const char *const msg = &state->info_log[msg_offset];
539 struct gl_context *ctx = state->ctx;
540
541 /* Report the error via GL_ARB_debug_output. */
542 _mesa_shader_debug(ctx, type, &msg_id, msg);
543
544 ralloc_strcat(&state->info_log, "\n");
545 }
546
547 void
_mesa_glsl_error(YYLTYPE * locp,_mesa_glsl_parse_state * state,const char * fmt,...)548 _mesa_glsl_error(YYLTYPE *locp, _mesa_glsl_parse_state *state,
549 const char *fmt, ...)
550 {
551 va_list ap;
552
553 state->error = true;
554
555 va_start(ap, fmt);
556 _mesa_glsl_msg(locp, state, MESA_DEBUG_TYPE_ERROR, fmt, ap);
557 va_end(ap);
558 }
559
560
561 void
_mesa_glsl_warning(const YYLTYPE * locp,_mesa_glsl_parse_state * state,const char * fmt,...)562 _mesa_glsl_warning(const YYLTYPE *locp, _mesa_glsl_parse_state *state,
563 const char *fmt, ...)
564 {
565 if (state->warnings_enabled) {
566 va_list ap;
567
568 va_start(ap, fmt);
569 _mesa_glsl_msg(locp, state, MESA_DEBUG_TYPE_OTHER, fmt, ap);
570 va_end(ap);
571 }
572 }
573
574
575 /**
576 * Enum representing the possible behaviors that can be specified in
577 * an #extension directive.
578 */
579 enum ext_behavior {
580 extension_disable,
581 extension_enable,
582 extension_require,
583 extension_warn
584 };
585
586 /**
587 * Element type for _mesa_glsl_supported_extensions
588 */
589 struct _mesa_glsl_extension {
590 /**
591 * Name of the extension when referred to in a GLSL extension
592 * statement
593 */
594 const char *name;
595
596 /**
597 * Whether this extension is a part of AEP
598 */
599 bool aep;
600
601 /**
602 * Predicate that checks whether the relevant extension is available for
603 * this context.
604 */
605 bool (*available_pred)(const _mesa_glsl_parse_state *,
606 gl_api api, uint8_t version);
607
608 /**
609 * Flag in the _mesa_glsl_parse_state struct that should be set
610 * when this extension is enabled.
611 *
612 * See note in _mesa_glsl_extension::supported_flag about "pointer
613 * to member" types.
614 */
615 bool _mesa_glsl_parse_state::* enable_flag;
616
617 /**
618 * Flag in the _mesa_glsl_parse_state struct that should be set
619 * when the shader requests "warn" behavior for this extension.
620 *
621 * See note in _mesa_glsl_extension::supported_flag about "pointer
622 * to member" types.
623 */
624 bool _mesa_glsl_parse_state::* warn_flag;
625
626
627 bool compatible_with_state(const _mesa_glsl_parse_state *state,
628 gl_api api, uint8_t gl_version) const;
629 void set_flags(_mesa_glsl_parse_state *state, ext_behavior behavior) const;
630 };
631
632 /** Checks if the context supports a user-facing extension */
633 #define EXT(name_str, driver_cap, ...) \
634 static UNUSED bool \
635 has_##name_str(const _mesa_glsl_parse_state *state, gl_api api, uint8_t version) \
636 { \
637 return state->exts->driver_cap && (version >= \
638 _mesa_extension_table[MESA_EXTENSION_##name_str].version[api]); \
639 }
640 #include "main/extensions_table.h"
641 #undef EXT
642
643 static unsigned
mesa_stage_to_gl_stage_bit(unsigned stage)644 mesa_stage_to_gl_stage_bit(unsigned stage)
645 {
646 switch (stage) {
647 case MESA_SHADER_VERTEX:
648 return GL_VERTEX_SHADER_BIT;
649 case MESA_SHADER_TESS_CTRL:
650 return GL_TESS_CONTROL_SHADER_BIT;
651 case MESA_SHADER_TESS_EVAL:
652 return GL_TESS_EVALUATION_SHADER_BIT;
653 case MESA_SHADER_GEOMETRY:
654 return GL_GEOMETRY_SHADER_BIT;
655 case MESA_SHADER_FRAGMENT:
656 return GL_FRAGMENT_SHADER_BIT;
657 case MESA_SHADER_COMPUTE:
658 return GL_COMPUTE_SHADER_BIT;
659 default:
660 unreachable("glsl parser: invalid shader stage");
661 }
662 }
663
664 #define HAS_SUBGROUP_EXT(name, feature) \
665 static bool \
666 has_KHR_shader_subgroup_##name(const _mesa_glsl_parse_state *state, gl_api api, uint8_t version) \
667 { \
668 unsigned stage = mesa_stage_to_gl_stage_bit(state->stage); \
669 return state->exts->KHR_shader_subgroup && \
670 (version >= _mesa_extension_table[MESA_EXTENSION_KHR_shader_subgroup].version[api]) && \
671 (state->consts->ShaderSubgroupSupportedStages & stage) && \
672 (state->consts->ShaderSubgroupSupportedFeatures & GL_SUBGROUP_FEATURE_##feature##_BIT_KHR); \
673 }
674
HAS_SUBGROUP_EXT(basic,BASIC)675 HAS_SUBGROUP_EXT(basic, BASIC)
676 HAS_SUBGROUP_EXT(vote, VOTE)
677 HAS_SUBGROUP_EXT(arithmetic, ARITHMETIC)
678 HAS_SUBGROUP_EXT(ballot, BALLOT)
679 HAS_SUBGROUP_EXT(shuffle, SHUFFLE)
680 HAS_SUBGROUP_EXT(shuffle_relative, SHUFFLE_RELATIVE)
681 HAS_SUBGROUP_EXT(clustered, CLUSTERED)
682 HAS_SUBGROUP_EXT(quad_, QUAD)
683
684 static bool
685 has_KHR_shader_subgroup_quad(const _mesa_glsl_parse_state *state, gl_api api, uint8_t version)
686 {
687 return has_KHR_shader_subgroup_quad_(state, api, version) &&
688 ((state->stage == MESA_SHADER_FRAGMENT || state->stage == MESA_SHADER_COMPUTE) ||
689 state->consts->ShaderSubgroupQuadAllStages);
690 }
691
692 #define EXT(NAME) \
693 { "GL_" #NAME, false, has_##NAME, \
694 &_mesa_glsl_parse_state::NAME##_enable, \
695 &_mesa_glsl_parse_state::NAME##_warn }
696
697 #define EXT_AEP(NAME) \
698 { "GL_" #NAME, true, has_##NAME, \
699 &_mesa_glsl_parse_state::NAME##_enable, \
700 &_mesa_glsl_parse_state::NAME##_warn }
701
702 /**
703 * Table of extensions that can be enabled/disabled within a shader,
704 * and the conditions under which they are supported.
705 */
706 static const _mesa_glsl_extension _mesa_glsl_supported_extensions[] = {
707 /* ARB extensions go here, sorted alphabetically.
708 */
709 EXT(ARB_ES3_1_compatibility),
710 EXT(ARB_ES3_2_compatibility),
711 EXT(ARB_arrays_of_arrays),
712 EXT(ARB_bindless_texture),
713 EXT(ARB_compatibility),
714 EXT(ARB_compute_shader),
715 EXT(ARB_compute_variable_group_size),
716 EXT(ARB_conservative_depth),
717 EXT(ARB_cull_distance),
718 EXT(ARB_derivative_control),
719 EXT(ARB_draw_buffers),
720 EXT(ARB_draw_instanced),
721 EXT(ARB_enhanced_layouts),
722 EXT(ARB_explicit_attrib_location),
723 EXT(ARB_explicit_uniform_location),
724 EXT(ARB_fragment_coord_conventions),
725 EXT(ARB_fragment_layer_viewport),
726 EXT(ARB_fragment_shader_interlock),
727 EXT(ARB_gpu_shader5),
728 EXT(ARB_gpu_shader_fp64),
729 EXT(ARB_gpu_shader_int64),
730 EXT(ARB_post_depth_coverage),
731 EXT(ARB_sample_shading),
732 EXT(ARB_separate_shader_objects),
733 EXT(ARB_shader_atomic_counter_ops),
734 EXT(ARB_shader_atomic_counters),
735 EXT(ARB_shader_ballot),
736 EXT(ARB_shader_bit_encoding),
737 EXT(ARB_shader_clock),
738 EXT(ARB_shader_draw_parameters),
739 EXT(ARB_shader_group_vote),
740 EXT(ARB_shader_image_load_store),
741 EXT(ARB_shader_image_size),
742 EXT(ARB_shader_precision),
743 EXT(ARB_shader_stencil_export),
744 EXT(ARB_shader_storage_buffer_object),
745 EXT(ARB_shader_subroutine),
746 EXT(ARB_shader_texture_image_samples),
747 EXT(ARB_shader_texture_lod),
748 EXT(ARB_shader_viewport_layer_array),
749 EXT(ARB_shading_language_420pack),
750 EXT(ARB_shading_language_include),
751 EXT(ARB_shading_language_packing),
752 EXT(ARB_sparse_texture2),
753 EXT(ARB_sparse_texture_clamp),
754 EXT(ARB_tessellation_shader),
755 EXT(ARB_texture_cube_map_array),
756 EXT(ARB_texture_gather),
757 EXT(ARB_texture_multisample),
758 EXT(ARB_texture_query_levels),
759 EXT(ARB_texture_query_lod),
760 EXT(ARB_texture_rectangle),
761 EXT(ARB_uniform_buffer_object),
762 EXT(ARB_vertex_attrib_64bit),
763 EXT(ARB_viewport_array),
764
765 /* KHR extensions go here, sorted alphabetically.
766 */
767 EXT_AEP(KHR_blend_equation_advanced),
768 EXT(KHR_shader_subgroup_arithmetic),
769 EXT(KHR_shader_subgroup_ballot),
770 EXT(KHR_shader_subgroup_basic),
771 EXT(KHR_shader_subgroup_clustered),
772 EXT(KHR_shader_subgroup_quad),
773 EXT(KHR_shader_subgroup_shuffle),
774 EXT(KHR_shader_subgroup_shuffle_relative),
775 EXT(KHR_shader_subgroup_vote),
776
777 /* OES extensions go here, sorted alphabetically.
778 */
779 EXT(OES_EGL_image_external),
780 EXT(OES_EGL_image_external_essl3),
781 EXT(OES_geometry_point_size),
782 EXT(OES_geometry_shader),
783 EXT(OES_gpu_shader5),
784 EXT(OES_primitive_bounding_box),
785 EXT_AEP(OES_sample_variables),
786 EXT_AEP(OES_shader_image_atomic),
787 EXT(OES_shader_io_blocks),
788 EXT_AEP(OES_shader_multisample_interpolation),
789 EXT(OES_standard_derivatives),
790 EXT(OES_tessellation_point_size),
791 EXT(OES_tessellation_shader),
792 EXT(OES_texture_3D),
793 EXT(OES_texture_buffer),
794 EXT(OES_texture_cube_map_array),
795 EXT_AEP(OES_texture_storage_multisample_2d_array),
796 EXT(OES_viewport_array),
797
798 /* All other extensions go here, sorted alphabetically.
799 */
800 EXT(AMD_conservative_depth),
801 EXT(AMD_gpu_shader_half_float),
802 EXT(AMD_gpu_shader_int64),
803 EXT(AMD_shader_stencil_export),
804 EXT(AMD_shader_trinary_minmax),
805 EXT(AMD_texture_texture4),
806 EXT(AMD_vertex_shader_layer),
807 EXT(AMD_vertex_shader_viewport_index),
808 EXT(ANDROID_extension_pack_es31a),
809 EXT(ARM_shader_framebuffer_fetch_depth_stencil),
810 EXT(EXT_blend_func_extended),
811 EXT(EXT_demote_to_helper_invocation),
812 EXT(EXT_frag_depth),
813 EXT(EXT_draw_buffers),
814 EXT(EXT_draw_instanced),
815 EXT(EXT_clip_cull_distance),
816 EXT(EXT_geometry_point_size),
817 EXT_AEP(EXT_geometry_shader),
818 EXT(EXT_gpu_shader4),
819 EXT_AEP(EXT_gpu_shader5),
820 EXT_AEP(EXT_primitive_bounding_box),
821 EXT(EXT_separate_shader_objects),
822 EXT(EXT_shader_framebuffer_fetch),
823 EXT(EXT_shader_framebuffer_fetch_non_coherent),
824 EXT(EXT_shader_group_vote),
825 EXT(EXT_shader_image_load_formatted),
826 EXT(EXT_shader_image_load_store),
827 EXT(EXT_shader_implicit_conversions),
828 EXT(EXT_shader_integer_mix),
829 EXT_AEP(EXT_shader_io_blocks),
830 EXT(EXT_shader_samples_identical),
831 EXT(EXT_shadow_samplers),
832 EXT(EXT_tessellation_point_size),
833 EXT_AEP(EXT_tessellation_shader),
834 EXT(EXT_texture_array),
835 EXT_AEP(EXT_texture_buffer),
836 EXT_AEP(EXT_texture_cube_map_array),
837 EXT(EXT_texture_query_lod),
838 EXT(EXT_texture_shadow_lod),
839 EXT(INTEL_conservative_rasterization),
840 EXT(INTEL_shader_atomic_float_minmax),
841 EXT(INTEL_shader_integer_functions2),
842 EXT(MESA_shader_integer_functions),
843 EXT(NV_compute_shader_derivatives),
844 EXT(NV_fragment_shader_interlock),
845 EXT(NV_image_formats),
846 EXT(NV_shader_atomic_float),
847 EXT(NV_shader_atomic_int64),
848 EXT(NV_shader_noperspective_interpolation),
849 EXT(NV_viewport_array2),
850 EXT(OVR_multiview),
851 EXT(OVR_multiview2),
852 };
853
854 #undef EXT
855
856
857 /**
858 * Determine whether a given extension is compatible with the target,
859 * API, and extension information in the current parser state.
860 */
compatible_with_state(const _mesa_glsl_parse_state * state,gl_api api,uint8_t gl_version) const861 bool _mesa_glsl_extension::compatible_with_state(
862 const _mesa_glsl_parse_state *state, gl_api api, uint8_t gl_version) const
863 {
864 return this->available_pred(state, api, gl_version);
865 }
866
867 /**
868 * Set the appropriate flags in the parser state to establish the
869 * given behavior for this extension.
870 */
set_flags(_mesa_glsl_parse_state * state,ext_behavior behavior) const871 void _mesa_glsl_extension::set_flags(_mesa_glsl_parse_state *state,
872 ext_behavior behavior) const
873 {
874 /* Note: the ->* operator indexes into state by the
875 * offsets this->enable_flag and this->warn_flag. See
876 * _mesa_glsl_extension::supported_flag for more info.
877 */
878 state->*(this->enable_flag) = (behavior != extension_disable);
879 state->*(this->warn_flag) = (behavior == extension_warn);
880 }
881
882 /**
883 * Check alias_shader_extension for any aliased shader extensions
884 */
find_extension_alias(_mesa_glsl_parse_state * state,const char * name)885 static const char *find_extension_alias(_mesa_glsl_parse_state *state, const char *name)
886 {
887 char *exts, *field, *ext_alias = NULL;
888
889 /* Copy alias_shader_extension because strtok() is destructive. */
890 exts = strdup(state->alias_shader_extension);
891 if (exts) {
892 for (field = strtok(exts, ","); field != NULL; field = strtok(NULL, ",")) {
893 if(strncmp(name, field, strlen(name)) == 0) {
894 field = strstr(field, ":");
895 if(field) {
896 ext_alias = strdup(field + 1);
897 }
898 break;
899 }
900 }
901
902 free(exts);
903 }
904 return ext_alias;
905 }
906
907 /**
908 * Find an extension by name in _mesa_glsl_supported_extensions. If
909 * the name is not found, return NULL.
910 */
find_extension(_mesa_glsl_parse_state * state,const char * name)911 static const _mesa_glsl_extension *find_extension(_mesa_glsl_parse_state *state, const char *name)
912 {
913 const char *ext_alias = NULL;
914 if (state->alias_shader_extension) {
915 ext_alias = find_extension_alias(state, name);
916 name = ext_alias ? ext_alias : name;
917 }
918
919 for (unsigned i = 0; i < ARRAY_SIZE(_mesa_glsl_supported_extensions); ++i) {
920 if (strcmp(name, _mesa_glsl_supported_extensions[i].name) == 0) {
921 free((void *)ext_alias);
922 return &_mesa_glsl_supported_extensions[i];
923 }
924 }
925
926 free((void *)ext_alias);
927 return NULL;
928 }
929
930 bool
_mesa_glsl_process_extension(const char * name,YYLTYPE * name_locp,const char * behavior_string,YYLTYPE * behavior_locp,_mesa_glsl_parse_state * state)931 _mesa_glsl_process_extension(const char *name, YYLTYPE *name_locp,
932 const char *behavior_string, YYLTYPE *behavior_locp,
933 _mesa_glsl_parse_state *state)
934 {
935 uint8_t gl_version = state->exts->Version;
936 gl_api api = state->api;
937 ext_behavior behavior;
938 if (strcmp(behavior_string, "warn") == 0) {
939 behavior = extension_warn;
940 } else if (strcmp(behavior_string, "require") == 0) {
941 behavior = extension_require;
942 } else if (strcmp(behavior_string, "enable") == 0) {
943 behavior = extension_enable;
944 } else if (strcmp(behavior_string, "disable") == 0) {
945 behavior = extension_disable;
946 } else {
947 _mesa_glsl_error(behavior_locp, state,
948 "unknown extension behavior `%s'",
949 behavior_string);
950 return false;
951 }
952
953 /* If we're in a desktop context but with an ES shader, use an ES API enum
954 * to verify extension availability.
955 */
956 if (state->es_shader && api != API_OPENGLES2)
957 api = API_OPENGLES2;
958 /* Use the language-version derived GL version to extension checks, unless
959 * we're using meta, which sets the version to the max.
960 */
961 if (gl_version != 0xff)
962 gl_version = state->gl_version;
963
964 if (strcmp(name, "all") == 0) {
965 if ((behavior == extension_enable) || (behavior == extension_require)) {
966 _mesa_glsl_error(name_locp, state, "cannot %s all extensions",
967 (behavior == extension_enable)
968 ? "enable" : "require");
969 return false;
970 } else {
971 for (unsigned i = 0;
972 i < ARRAY_SIZE(_mesa_glsl_supported_extensions); ++i) {
973 const _mesa_glsl_extension *extension
974 = &_mesa_glsl_supported_extensions[i];
975 if (extension->compatible_with_state(state, api, gl_version)) {
976 _mesa_glsl_supported_extensions[i].set_flags(state, behavior);
977 }
978 }
979 }
980 } else {
981 const _mesa_glsl_extension *extension = find_extension(state, name);
982 if (extension &&
983 (extension->compatible_with_state(state, api, gl_version) ||
984 (state->consts->AllowGLSLCompatShaders &&
985 extension->compatible_with_state(state, API_OPENGL_COMPAT, gl_version)))) {
986 extension->set_flags(state, behavior);
987 if (extension->available_pred == has_ANDROID_extension_pack_es31a) {
988 for (unsigned i = 0;
989 i < ARRAY_SIZE(_mesa_glsl_supported_extensions); ++i) {
990 const _mesa_glsl_extension *extension =
991 &_mesa_glsl_supported_extensions[i];
992
993 if (!extension->aep)
994 continue;
995 /* AEP should not be enabled if all of the sub-extensions can't
996 * also be enabled. This is not the proper layer to do such
997 * error-checking though.
998 */
999 assert(extension->compatible_with_state(state, api, gl_version));
1000 extension->set_flags(state, behavior);
1001 }
1002 } else if (extension->available_pred == has_KHR_shader_subgroup_vote ||
1003 extension->available_pred == has_KHR_shader_subgroup_arithmetic ||
1004 extension->available_pred == has_KHR_shader_subgroup_ballot ||
1005 extension->available_pred == has_KHR_shader_subgroup_shuffle ||
1006 extension->available_pred == has_KHR_shader_subgroup_shuffle_relative ||
1007 extension->available_pred == has_KHR_shader_subgroup_clustered ||
1008 extension->available_pred == has_KHR_shader_subgroup_quad) {
1009 /* GLSL KHR_shader_subgroup spec says when any of above subgroup extension
1010 * is enabled, KHR_shader_subgroup_basic extension is also implicitly enabled.
1011 */
1012 for (unsigned i = 0; i < ARRAY_SIZE(_mesa_glsl_supported_extensions); ++i) {
1013 const _mesa_glsl_extension *extension = &_mesa_glsl_supported_extensions[i];
1014 if (extension->available_pred == has_KHR_shader_subgroup_basic) {
1015 assert(extension->compatible_with_state(state, api, gl_version));
1016 extension->set_flags(state, behavior);
1017 }
1018 }
1019 }
1020 } else {
1021 static const char fmt[] = "extension `%s' unsupported in %s shader";
1022
1023 if (behavior == extension_require) {
1024 _mesa_glsl_error(name_locp, state, fmt,
1025 name, _mesa_shader_stage_to_string(state->stage));
1026 return false;
1027 } else {
1028 _mesa_glsl_warning(name_locp, state, fmt,
1029 name, _mesa_shader_stage_to_string(state->stage));
1030 }
1031 }
1032 }
1033
1034 if (state->OVR_multiview2_enable)
1035 state->OVR_multiview_enable = true;
1036
1037 return true;
1038 }
1039
1040 /**
1041 * Recurses through <type> and <expr> if <expr> is an aggregate initializer
1042 * and sets <expr>'s <constructor_type> field to <type>. Gives later functions
1043 * (process_array_constructor, et al) sufficient information to do type
1044 * checking.
1045 *
1046 * Operates on assignments involving an aggregate initializer. E.g.,
1047 *
1048 * vec4 pos = {1.0, -1.0, 0.0, 1.0};
1049 *
1050 * or more ridiculously,
1051 *
1052 * struct S {
1053 * vec4 v[2];
1054 * };
1055 *
1056 * struct {
1057 * S a[2], b;
1058 * int c;
1059 * } aggregate = {
1060 * {
1061 * {
1062 * {
1063 * {1.0, 2.0, 3.0, 4.0}, // a[0].v[0]
1064 * {5.0, 6.0, 7.0, 8.0} // a[0].v[1]
1065 * } // a[0].v
1066 * }, // a[0]
1067 * {
1068 * {
1069 * {1.0, 2.0, 3.0, 4.0}, // a[1].v[0]
1070 * {5.0, 6.0, 7.0, 8.0} // a[1].v[1]
1071 * } // a[1].v
1072 * } // a[1]
1073 * }, // a
1074 * {
1075 * {
1076 * {1.0, 2.0, 3.0, 4.0}, // b.v[0]
1077 * {5.0, 6.0, 7.0, 8.0} // b.v[1]
1078 * } // b.v
1079 * }, // b
1080 * 4 // c
1081 * };
1082 *
1083 * This pass is necessary because the right-hand side of <type> e = { ... }
1084 * doesn't contain sufficient information to determine if the types match.
1085 */
1086 void
_mesa_ast_set_aggregate_type(const glsl_type * type,ast_expression * expr)1087 _mesa_ast_set_aggregate_type(const glsl_type *type,
1088 ast_expression *expr)
1089 {
1090 ast_aggregate_initializer *ai = (ast_aggregate_initializer *)expr;
1091 ai->constructor_type = type;
1092
1093 /* If the aggregate is an array, recursively set its elements' types. */
1094 if (glsl_type_is_array(type)) {
1095 /* Each array element has the type type->fields.array.
1096 *
1097 * E.g., if <type> if struct S[2] we want to set each element's type to
1098 * struct S.
1099 */
1100 for (exec_node *expr_node = ai->expressions.get_head_raw();
1101 !expr_node->is_tail_sentinel();
1102 expr_node = expr_node->next) {
1103 ast_expression *expr = exec_node_data(ast_expression, expr_node,
1104 link);
1105
1106 if (expr->oper == ast_aggregate)
1107 _mesa_ast_set_aggregate_type(type->fields.array, expr);
1108 }
1109
1110 /* If the aggregate is a struct, recursively set its fields' types. */
1111 } else if (glsl_type_is_struct(type)) {
1112 exec_node *expr_node = ai->expressions.get_head_raw();
1113
1114 /* Iterate through the struct's fields. */
1115 for (unsigned i = 0; !expr_node->is_tail_sentinel() && i < type->length;
1116 i++, expr_node = expr_node->next) {
1117 ast_expression *expr = exec_node_data(ast_expression, expr_node,
1118 link);
1119
1120 if (expr->oper == ast_aggregate) {
1121 _mesa_ast_set_aggregate_type(type->fields.structure[i].type, expr);
1122 }
1123 }
1124 /* If the aggregate is a matrix, set its columns' types. */
1125 } else if (glsl_type_is_matrix(type)) {
1126 for (exec_node *expr_node = ai->expressions.get_head_raw();
1127 !expr_node->is_tail_sentinel();
1128 expr_node = expr_node->next) {
1129 ast_expression *expr = exec_node_data(ast_expression, expr_node,
1130 link);
1131
1132 if (expr->oper == ast_aggregate)
1133 _mesa_ast_set_aggregate_type(glsl_get_column_type(type), expr);
1134 }
1135 }
1136 }
1137
1138 void
_mesa_ast_process_interface_block(YYLTYPE * locp,_mesa_glsl_parse_state * state,ast_interface_block * const block,const struct ast_type_qualifier & q)1139 _mesa_ast_process_interface_block(YYLTYPE *locp,
1140 _mesa_glsl_parse_state *state,
1141 ast_interface_block *const block,
1142 const struct ast_type_qualifier &q)
1143 {
1144 if (q.flags.q.buffer) {
1145 if (!state->has_shader_storage_buffer_objects()) {
1146 _mesa_glsl_error(locp, state,
1147 "#version 430 / GL_ARB_shader_storage_buffer_object "
1148 "required for defining shader storage blocks");
1149 } else if (state->ARB_shader_storage_buffer_object_warn) {
1150 _mesa_glsl_warning(locp, state,
1151 "#version 430 / GL_ARB_shader_storage_buffer_object "
1152 "required for defining shader storage blocks");
1153 }
1154 } else if (q.flags.q.uniform) {
1155 if (!state->has_uniform_buffer_objects()) {
1156 _mesa_glsl_error(locp, state,
1157 "#version 140 / GL_ARB_uniform_buffer_object "
1158 "required for defining uniform blocks");
1159 } else if (state->ARB_uniform_buffer_object_warn) {
1160 _mesa_glsl_warning(locp, state,
1161 "#version 140 / GL_ARB_uniform_buffer_object "
1162 "required for defining uniform blocks");
1163 }
1164 } else {
1165 if (!state->has_shader_io_blocks()) {
1166 if (state->es_shader) {
1167 _mesa_glsl_error(locp, state,
1168 "GL_OES_shader_io_blocks or #version 320 "
1169 "required for using interface blocks");
1170 } else {
1171 _mesa_glsl_error(locp, state,
1172 "#version 150 required for using "
1173 "interface blocks");
1174 }
1175 }
1176 }
1177
1178 /* From the GLSL 1.50.11 spec, section 4.3.7 ("Interface Blocks"):
1179 * "It is illegal to have an input block in a vertex shader
1180 * or an output block in a fragment shader"
1181 */
1182 if ((state->stage == MESA_SHADER_VERTEX) && q.flags.q.in) {
1183 _mesa_glsl_error(locp, state,
1184 "`in' interface block is not allowed for "
1185 "a vertex shader");
1186 } else if ((state->stage == MESA_SHADER_FRAGMENT) && q.flags.q.out) {
1187 _mesa_glsl_error(locp, state,
1188 "`out' interface block is not allowed for "
1189 "a fragment shader");
1190 }
1191
1192 /* Since block arrays require names, and both features are added in
1193 * the same language versions, we don't have to explicitly
1194 * version-check both things.
1195 */
1196 if (block->instance_name != NULL) {
1197 state->check_version(150, 300, locp, "interface blocks with "
1198 "an instance name are not allowed");
1199 }
1200
1201 ast_type_qualifier::bitset_t interface_type_mask;
1202 struct ast_type_qualifier temp_type_qualifier;
1203
1204 /* Get a bitmask containing only the in/out/uniform/buffer
1205 * flags, allowing us to ignore other irrelevant flags like
1206 * interpolation qualifiers.
1207 */
1208 temp_type_qualifier.flags.i = 0;
1209 temp_type_qualifier.flags.q.uniform = true;
1210 temp_type_qualifier.flags.q.in = true;
1211 temp_type_qualifier.flags.q.out = true;
1212 temp_type_qualifier.flags.q.buffer = true;
1213 temp_type_qualifier.flags.q.patch = true;
1214 interface_type_mask = temp_type_qualifier.flags.i;
1215
1216 /* Get the block's interface qualifier. The interface_qualifier
1217 * production rule guarantees that only one bit will be set (and
1218 * it will be in/out/uniform).
1219 */
1220 ast_type_qualifier::bitset_t block_interface_qualifier = q.flags.i;
1221
1222 block->default_layout.flags.i |= block_interface_qualifier;
1223
1224 if (state->stage == MESA_SHADER_GEOMETRY &&
1225 state->has_explicit_attrib_stream() &&
1226 block->default_layout.flags.q.out) {
1227 /* Assign global layout's stream value. */
1228 block->default_layout.flags.q.stream = 1;
1229 block->default_layout.flags.q.explicit_stream = 0;
1230 block->default_layout.stream = state->out_qualifier->stream;
1231 }
1232
1233 if (state->has_enhanced_layouts() && block->default_layout.flags.q.out &&
1234 state->exts->ARB_transform_feedback3) {
1235 /* Assign global layout's xfb_buffer value. */
1236 block->default_layout.flags.q.xfb_buffer = 1;
1237 block->default_layout.flags.q.explicit_xfb_buffer = 0;
1238 block->default_layout.xfb_buffer = state->out_qualifier->xfb_buffer;
1239 }
1240
1241 foreach_list_typed (ast_declarator_list, member, link, &block->declarations) {
1242 ast_type_qualifier& qualifier = member->type->qualifier;
1243 if ((qualifier.flags.i & interface_type_mask) == 0) {
1244 /* GLSLangSpec.1.50.11, 4.3.7 (Interface Blocks):
1245 * "If no optional qualifier is used in a member declaration, the
1246 * qualifier of the variable is just in, out, or uniform as declared
1247 * by interface-qualifier."
1248 */
1249 qualifier.flags.i |= block_interface_qualifier;
1250 } else if ((qualifier.flags.i & interface_type_mask) !=
1251 block_interface_qualifier) {
1252 /* GLSLangSpec.1.50.11, 4.3.7 (Interface Blocks):
1253 * "If optional qualifiers are used, they can include interpolation
1254 * and storage qualifiers and they must declare an input, output,
1255 * or uniform variable consistent with the interface qualifier of
1256 * the block."
1257 */
1258 _mesa_glsl_error(locp, state,
1259 "uniform/in/out qualifier on "
1260 "interface block member does not match "
1261 "the interface block");
1262 }
1263
1264 if (!(q.flags.q.in || q.flags.q.out) && qualifier.flags.q.invariant)
1265 _mesa_glsl_error(locp, state,
1266 "invariant qualifiers can be used only "
1267 "in interface block members for shader "
1268 "inputs or outputs");
1269 }
1270 }
1271
1272 static void
_mesa_ast_type_qualifier_print(const struct ast_type_qualifier * q)1273 _mesa_ast_type_qualifier_print(const struct ast_type_qualifier *q)
1274 {
1275 if (q->is_subroutine_decl())
1276 printf("subroutine ");
1277
1278 if (q->subroutine_list) {
1279 printf("subroutine (");
1280 q->subroutine_list->print();
1281 printf(")");
1282 }
1283
1284 if (q->flags.q.constant)
1285 printf("const ");
1286
1287 if (q->flags.q.invariant)
1288 printf("invariant ");
1289
1290 if (q->flags.q.attribute)
1291 printf("attribute ");
1292
1293 if (q->flags.q.varying)
1294 printf("varying ");
1295
1296 if (q->flags.q.in && q->flags.q.out)
1297 printf("inout ");
1298 else {
1299 if (q->flags.q.in)
1300 printf("in ");
1301
1302 if (q->flags.q.out)
1303 printf("out ");
1304 }
1305
1306 if (q->flags.q.centroid)
1307 printf("centroid ");
1308 if (q->flags.q.sample)
1309 printf("sample ");
1310 if (q->flags.q.patch)
1311 printf("patch ");
1312 if (q->flags.q.uniform)
1313 printf("uniform ");
1314 if (q->flags.q.buffer)
1315 printf("buffer ");
1316 if (q->flags.q.smooth)
1317 printf("smooth ");
1318 if (q->flags.q.flat)
1319 printf("flat ");
1320 if (q->flags.q.noperspective)
1321 printf("noperspective ");
1322 }
1323
1324
1325 void
print(void) const1326 ast_node::print(void) const
1327 {
1328 printf("unhandled node ");
1329 }
1330
1331
ast_node(void)1332 ast_node::ast_node(void)
1333 {
1334 this->location.path = NULL;
1335 this->location.source = 0;
1336 this->location.first_line = 0;
1337 this->location.first_column = 0;
1338 this->location.last_line = 0;
1339 this->location.last_column = 0;
1340 }
1341
1342
1343 static void
ast_opt_array_dimensions_print(const ast_array_specifier * array_specifier)1344 ast_opt_array_dimensions_print(const ast_array_specifier *array_specifier)
1345 {
1346 if (array_specifier)
1347 array_specifier->print();
1348 }
1349
1350
1351 void
print(void) const1352 ast_compound_statement::print(void) const
1353 {
1354 printf("{\n");
1355
1356 foreach_list_typed(ast_node, ast, link, &this->statements) {
1357 ast->print();
1358 }
1359
1360 printf("}\n");
1361 }
1362
1363
ast_compound_statement(int new_scope,ast_node * statements)1364 ast_compound_statement::ast_compound_statement(int new_scope,
1365 ast_node *statements)
1366 {
1367 this->new_scope = new_scope;
1368
1369 if (statements != NULL) {
1370 this->statements.push_degenerate_list_at_head(&statements->link);
1371 }
1372 }
1373
1374
1375 void
print(void) const1376 ast_expression::print(void) const
1377 {
1378 switch (oper) {
1379 case ast_assign:
1380 case ast_mul_assign:
1381 case ast_div_assign:
1382 case ast_mod_assign:
1383 case ast_add_assign:
1384 case ast_sub_assign:
1385 case ast_ls_assign:
1386 case ast_rs_assign:
1387 case ast_and_assign:
1388 case ast_xor_assign:
1389 case ast_or_assign:
1390 subexpressions[0]->print();
1391 printf("%s ", operator_string(oper));
1392 subexpressions[1]->print();
1393 break;
1394
1395 case ast_field_selection:
1396 subexpressions[0]->print();
1397 printf(". %s ", primary_expression.identifier);
1398 break;
1399
1400 case ast_plus:
1401 case ast_neg:
1402 case ast_bit_not:
1403 case ast_logic_not:
1404 case ast_pre_inc:
1405 case ast_pre_dec:
1406 printf("%s ", operator_string(oper));
1407 subexpressions[0]->print();
1408 break;
1409
1410 case ast_post_inc:
1411 case ast_post_dec:
1412 subexpressions[0]->print();
1413 printf("%s ", operator_string(oper));
1414 break;
1415
1416 case ast_conditional:
1417 subexpressions[0]->print();
1418 printf("? ");
1419 subexpressions[1]->print();
1420 printf(": ");
1421 subexpressions[2]->print();
1422 break;
1423
1424 case ast_array_index:
1425 subexpressions[0]->print();
1426 printf("[ ");
1427 subexpressions[1]->print();
1428 printf("] ");
1429 break;
1430
1431 case ast_function_call: {
1432 subexpressions[0]->print();
1433 printf("( ");
1434
1435 foreach_list_typed (ast_node, ast, link, &this->expressions) {
1436 if (&ast->link != this->expressions.get_head())
1437 printf(", ");
1438
1439 ast->print();
1440 }
1441
1442 printf(") ");
1443 break;
1444 }
1445
1446 case ast_identifier:
1447 printf("%s ", primary_expression.identifier);
1448 break;
1449
1450 case ast_int_constant:
1451 printf("%d ", primary_expression.int_constant);
1452 break;
1453
1454 case ast_uint_constant:
1455 printf("%u ", primary_expression.uint_constant);
1456 break;
1457
1458 case ast_float_constant:
1459 printf("%f ", primary_expression.float_constant);
1460 break;
1461
1462 case ast_double_constant:
1463 printf("%f ", primary_expression.double_constant);
1464 break;
1465
1466 case ast_int64_constant:
1467 printf("%" PRId64 " ", primary_expression.int64_constant);
1468 break;
1469
1470 case ast_uint64_constant:
1471 printf("%" PRIu64 " ", primary_expression.uint64_constant);
1472 break;
1473
1474 case ast_bool_constant:
1475 printf("%s ",
1476 primary_expression.bool_constant
1477 ? "true" : "false");
1478 break;
1479
1480 case ast_sequence: {
1481 printf("( ");
1482 foreach_list_typed (ast_node, ast, link, & this->expressions) {
1483 if (&ast->link != this->expressions.get_head())
1484 printf(", ");
1485
1486 ast->print();
1487 }
1488 printf(") ");
1489 break;
1490 }
1491
1492 case ast_aggregate: {
1493 printf("{ ");
1494 foreach_list_typed (ast_node, ast, link, & this->expressions) {
1495 if (&ast->link != this->expressions.get_head())
1496 printf(", ");
1497
1498 ast->print();
1499 }
1500 printf("} ");
1501 break;
1502 }
1503
1504 default:
1505 assert(0);
1506 break;
1507 }
1508 }
1509
ast_expression(int oper,ast_expression * ex0,ast_expression * ex1,ast_expression * ex2)1510 ast_expression::ast_expression(int oper,
1511 ast_expression *ex0,
1512 ast_expression *ex1,
1513 ast_expression *ex2) :
1514 primary_expression()
1515 {
1516 this->oper = ast_operators(oper);
1517 this->subexpressions[0] = ex0;
1518 this->subexpressions[1] = ex1;
1519 this->subexpressions[2] = ex2;
1520 this->non_lvalue_description = NULL;
1521 this->is_lhs = false;
1522 }
1523
1524
1525 void
print(void) const1526 ast_expression_statement::print(void) const
1527 {
1528 if (expression)
1529 expression->print();
1530
1531 printf("; ");
1532 }
1533
1534
ast_expression_statement(ast_expression * ex)1535 ast_expression_statement::ast_expression_statement(ast_expression *ex) :
1536 expression(ex)
1537 {
1538 /* empty */
1539 }
1540
1541
1542 void
print(void) const1543 ast_function::print(void) const
1544 {
1545 return_type->print();
1546 printf(" %s (", identifier);
1547
1548 foreach_list_typed(ast_node, ast, link, & this->parameters) {
1549 ast->print();
1550 }
1551
1552 printf(")");
1553 }
1554
1555
ast_function(void)1556 ast_function::ast_function(void)
1557 : return_type(NULL), identifier(NULL), is_definition(false),
1558 signature(NULL)
1559 {
1560 /* empty */
1561 }
1562
1563
1564 void
print(void) const1565 ast_fully_specified_type::print(void) const
1566 {
1567 _mesa_ast_type_qualifier_print(& qualifier);
1568 specifier->print();
1569 }
1570
1571
1572 void
print(void) const1573 ast_parameter_declarator::print(void) const
1574 {
1575 type->print();
1576 if (identifier)
1577 printf("%s ", identifier);
1578 ast_opt_array_dimensions_print(array_specifier);
1579 }
1580
1581
1582 void
print(void) const1583 ast_function_definition::print(void) const
1584 {
1585 prototype->print();
1586 body->print();
1587 }
1588
1589
1590 void
print(void) const1591 ast_declaration::print(void) const
1592 {
1593 printf("%s ", identifier);
1594 ast_opt_array_dimensions_print(array_specifier);
1595
1596 if (initializer) {
1597 printf("= ");
1598 initializer->print();
1599 }
1600 }
1601
1602
ast_declaration(const char * identifier,ast_array_specifier * array_specifier,ast_expression * initializer)1603 ast_declaration::ast_declaration(const char *identifier,
1604 ast_array_specifier *array_specifier,
1605 ast_expression *initializer)
1606 {
1607 this->identifier = identifier;
1608 this->array_specifier = array_specifier;
1609 this->initializer = initializer;
1610 }
1611
1612
1613 void
print(void) const1614 ast_declarator_list::print(void) const
1615 {
1616 assert(type || invariant);
1617
1618 if (type)
1619 type->print();
1620 else if (invariant)
1621 printf("invariant ");
1622 else
1623 printf("precise ");
1624
1625 foreach_list_typed (ast_node, ast, link, & this->declarations) {
1626 if (&ast->link != this->declarations.get_head())
1627 printf(", ");
1628
1629 ast->print();
1630 }
1631
1632 printf("; ");
1633 }
1634
1635
ast_declarator_list(ast_fully_specified_type * type)1636 ast_declarator_list::ast_declarator_list(ast_fully_specified_type *type)
1637 {
1638 this->type = type;
1639 this->invariant = false;
1640 this->precise = false;
1641 }
1642
1643 void
print(void) const1644 ast_jump_statement::print(void) const
1645 {
1646 switch (mode) {
1647 case ast_continue:
1648 printf("continue; ");
1649 break;
1650 case ast_break:
1651 printf("break; ");
1652 break;
1653 case ast_return:
1654 printf("return ");
1655 if (opt_return_value)
1656 opt_return_value->print();
1657
1658 printf("; ");
1659 break;
1660 case ast_discard:
1661 printf("discard; ");
1662 break;
1663 }
1664 }
1665
1666
ast_jump_statement(int mode,ast_expression * return_value)1667 ast_jump_statement::ast_jump_statement(int mode, ast_expression *return_value)
1668 : opt_return_value(NULL)
1669 {
1670 this->mode = ast_jump_modes(mode);
1671
1672 if (mode == ast_return)
1673 opt_return_value = return_value;
1674 }
1675
1676
1677 void
print(void) const1678 ast_demote_statement::print(void) const
1679 {
1680 printf("demote; ");
1681 }
1682
1683
1684 void
print(void) const1685 ast_selection_statement::print(void) const
1686 {
1687 printf("if ( ");
1688 condition->print();
1689 printf(") ");
1690
1691 then_statement->print();
1692
1693 if (else_statement) {
1694 printf("else ");
1695 else_statement->print();
1696 }
1697 }
1698
1699
ast_selection_statement(ast_expression * condition,ast_node * then_statement,ast_node * else_statement)1700 ast_selection_statement::ast_selection_statement(ast_expression *condition,
1701 ast_node *then_statement,
1702 ast_node *else_statement)
1703 {
1704 this->condition = condition;
1705 this->then_statement = then_statement;
1706 this->else_statement = else_statement;
1707 }
1708
1709
1710 void
print(void) const1711 ast_switch_statement::print(void) const
1712 {
1713 printf("switch ( ");
1714 test_expression->print();
1715 printf(") ");
1716
1717 body->print();
1718 }
1719
1720
ast_switch_statement(ast_expression * test_expression,ast_node * body)1721 ast_switch_statement::ast_switch_statement(ast_expression *test_expression,
1722 ast_node *body)
1723 {
1724 this->test_expression = test_expression;
1725 this->body = body;
1726 this->test_val = NULL;
1727 }
1728
1729
1730 void
print(void) const1731 ast_switch_body::print(void) const
1732 {
1733 printf("{\n");
1734 if (stmts != NULL) {
1735 stmts->print();
1736 }
1737 printf("}\n");
1738 }
1739
1740
ast_switch_body(ast_case_statement_list * stmts)1741 ast_switch_body::ast_switch_body(ast_case_statement_list *stmts)
1742 {
1743 this->stmts = stmts;
1744 }
1745
1746
print(void) const1747 void ast_case_label::print(void) const
1748 {
1749 if (test_value != NULL) {
1750 printf("case ");
1751 test_value->print();
1752 printf(": ");
1753 } else {
1754 printf("default: ");
1755 }
1756 }
1757
1758
ast_case_label(ast_expression * test_value)1759 ast_case_label::ast_case_label(ast_expression *test_value)
1760 {
1761 this->test_value = test_value;
1762 }
1763
1764
print(void) const1765 void ast_case_label_list::print(void) const
1766 {
1767 foreach_list_typed(ast_node, ast, link, & this->labels) {
1768 ast->print();
1769 }
1770 printf("\n");
1771 }
1772
1773
ast_case_label_list(void)1774 ast_case_label_list::ast_case_label_list(void)
1775 {
1776 }
1777
1778
print(void) const1779 void ast_case_statement::print(void) const
1780 {
1781 labels->print();
1782 foreach_list_typed(ast_node, ast, link, & this->stmts) {
1783 ast->print();
1784 printf("\n");
1785 }
1786 }
1787
1788
ast_case_statement(ast_case_label_list * labels)1789 ast_case_statement::ast_case_statement(ast_case_label_list *labels)
1790 {
1791 this->labels = labels;
1792 }
1793
1794
print(void) const1795 void ast_case_statement_list::print(void) const
1796 {
1797 foreach_list_typed(ast_node, ast, link, & this->cases) {
1798 ast->print();
1799 }
1800 }
1801
1802
ast_case_statement_list(void)1803 ast_case_statement_list::ast_case_statement_list(void)
1804 {
1805 }
1806
1807
1808 void
print(void) const1809 ast_iteration_statement::print(void) const
1810 {
1811 switch (mode) {
1812 case ast_for:
1813 printf("for( ");
1814 if (init_statement)
1815 init_statement->print();
1816 printf("; ");
1817
1818 if (condition)
1819 condition->print();
1820 printf("; ");
1821
1822 if (rest_expression)
1823 rest_expression->print();
1824 printf(") ");
1825
1826 body->print();
1827 break;
1828
1829 case ast_while:
1830 printf("while ( ");
1831 if (condition)
1832 condition->print();
1833 printf(") ");
1834 body->print();
1835 break;
1836
1837 case ast_do_while:
1838 printf("do ");
1839 body->print();
1840 printf("while ( ");
1841 if (condition)
1842 condition->print();
1843 printf("); ");
1844 break;
1845 }
1846 }
1847
1848
ast_iteration_statement(int mode,ast_node * init,ast_node * condition,ast_expression * rest_expression,ast_node * body)1849 ast_iteration_statement::ast_iteration_statement(int mode,
1850 ast_node *init,
1851 ast_node *condition,
1852 ast_expression *rest_expression,
1853 ast_node *body)
1854 {
1855 this->mode = ast_iteration_modes(mode);
1856 this->init_statement = init;
1857 this->condition = condition;
1858 this->rest_expression = rest_expression;
1859 this->body = body;
1860 }
1861
1862
1863 void
print(void) const1864 ast_struct_specifier::print(void) const
1865 {
1866 printf("struct %s { ", name);
1867 foreach_list_typed(ast_node, ast, link, &this->declarations) {
1868 ast->print();
1869 }
1870 printf("} ");
1871 }
1872
1873
ast_struct_specifier(const char * identifier,ast_declarator_list * declarator_list)1874 ast_struct_specifier::ast_struct_specifier(const char *identifier,
1875 ast_declarator_list *declarator_list)
1876 : name(identifier), layout(NULL), declarations(), is_declaration(true),
1877 type(NULL)
1878 {
1879 this->declarations.push_degenerate_list_at_head(&declarator_list->link);
1880 }
1881
print(void) const1882 void ast_subroutine_list::print(void) const
1883 {
1884 foreach_list_typed (ast_node, ast, link, & this->declarations) {
1885 if (&ast->link != this->declarations.get_head())
1886 printf(", ");
1887 ast->print();
1888 }
1889 }
1890
1891 static void
set_shader_inout_layout(struct gl_shader * shader,struct _mesa_glsl_parse_state * state)1892 set_shader_inout_layout(struct gl_shader *shader,
1893 struct _mesa_glsl_parse_state *state)
1894 {
1895 /* Should have been prevented by the parser. */
1896 if (shader->Stage != MESA_SHADER_GEOMETRY &&
1897 shader->Stage != MESA_SHADER_TESS_EVAL &&
1898 shader->Stage != MESA_SHADER_COMPUTE) {
1899 assert(!state->in_qualifier->flags.i);
1900 }
1901
1902 if (shader->Stage != MESA_SHADER_COMPUTE) {
1903 /* Should have been prevented by the parser. */
1904 assert(!state->cs_input_local_size_specified);
1905 assert(!state->cs_input_local_size_variable_specified);
1906 assert(state->cs_derivative_group == DERIVATIVE_GROUP_NONE);
1907 }
1908
1909 if (shader->Stage != MESA_SHADER_FRAGMENT) {
1910 /* Should have been prevented by the parser. */
1911 assert(!state->fs_uses_gl_fragcoord);
1912 assert(!state->fs_redeclares_gl_fragcoord);
1913 assert(!state->fs_pixel_center_integer);
1914 assert(!state->fs_origin_upper_left);
1915 assert(!state->fs_early_fragment_tests);
1916 assert(!state->fs_inner_coverage);
1917 assert(!state->fs_post_depth_coverage);
1918 assert(!state->fs_pixel_interlock_ordered);
1919 assert(!state->fs_pixel_interlock_unordered);
1920 assert(!state->fs_sample_interlock_ordered);
1921 assert(!state->fs_sample_interlock_unordered);
1922 }
1923
1924 for (unsigned i = 0; i < MAX_FEEDBACK_BUFFERS; i++) {
1925 if (state->out_qualifier->out_xfb_stride[i]) {
1926 unsigned xfb_stride;
1927 if (state->out_qualifier->out_xfb_stride[i]->
1928 process_qualifier_constant(state, "xfb_stride", &xfb_stride,
1929 true)) {
1930 shader->TransformFeedbackBufferStride[i] = xfb_stride;
1931 }
1932 }
1933 }
1934
1935 switch (shader->Stage) {
1936 case MESA_SHADER_TESS_CTRL:
1937 shader->info.TessCtrl.VerticesOut = 0;
1938 if (state->tcs_output_vertices_specified) {
1939 unsigned vertices;
1940 if (state->out_qualifier->vertices->
1941 process_qualifier_constant(state, "vertices", &vertices,
1942 false)) {
1943
1944 YYLTYPE loc = state->out_qualifier->vertices->get_location();
1945 if (vertices > state->Const.MaxPatchVertices) {
1946 _mesa_glsl_error(&loc, state, "vertices (%d) exceeds "
1947 "GL_MAX_PATCH_VERTICES", vertices);
1948 }
1949 shader->info.TessCtrl.VerticesOut = vertices;
1950 }
1951 }
1952 break;
1953 case MESA_SHADER_TESS_EVAL:
1954 shader->info.TessEval._PrimitiveMode = TESS_PRIMITIVE_UNSPECIFIED;
1955 if (state->in_qualifier->flags.q.prim_type) {
1956 switch (state->in_qualifier->prim_type) {
1957 case GL_TRIANGLES:
1958 shader->info.TessEval._PrimitiveMode = TESS_PRIMITIVE_TRIANGLES;
1959 break;
1960 case GL_QUADS:
1961 shader->info.TessEval._PrimitiveMode = TESS_PRIMITIVE_QUADS;
1962 break;
1963 case GL_ISOLINES:
1964 shader->info.TessEval._PrimitiveMode = TESS_PRIMITIVE_ISOLINES;
1965 break;
1966 }
1967 }
1968
1969 shader->info.TessEval.Spacing = TESS_SPACING_UNSPECIFIED;
1970 if (state->in_qualifier->flags.q.vertex_spacing)
1971 shader->info.TessEval.Spacing = state->in_qualifier->vertex_spacing;
1972
1973 shader->info.TessEval.VertexOrder = 0;
1974 if (state->in_qualifier->flags.q.ordering)
1975 shader->info.TessEval.VertexOrder = state->in_qualifier->ordering;
1976
1977 shader->info.TessEval.PointMode = -1;
1978 if (state->in_qualifier->flags.q.point_mode)
1979 shader->info.TessEval.PointMode = state->in_qualifier->point_mode;
1980 break;
1981 case MESA_SHADER_GEOMETRY:
1982 shader->info.Geom.VerticesOut = -1;
1983 if (state->out_qualifier->flags.q.max_vertices) {
1984 unsigned qual_max_vertices;
1985 if (state->out_qualifier->max_vertices->
1986 process_qualifier_constant(state, "max_vertices",
1987 &qual_max_vertices, true)) {
1988
1989 if (qual_max_vertices > state->Const.MaxGeometryOutputVertices) {
1990 YYLTYPE loc = state->out_qualifier->max_vertices->get_location();
1991 _mesa_glsl_error(&loc, state,
1992 "maximum output vertices (%d) exceeds "
1993 "GL_MAX_GEOMETRY_OUTPUT_VERTICES",
1994 qual_max_vertices);
1995 }
1996 shader->info.Geom.VerticesOut = qual_max_vertices;
1997 }
1998 }
1999
2000 if (state->gs_input_prim_type_specified) {
2001 shader->info.Geom.InputType =
2002 gl_to_mesa_prim(state->in_qualifier->prim_type);
2003 } else {
2004 shader->info.Geom.InputType = MESA_PRIM_UNKNOWN;
2005 }
2006
2007 if (state->out_qualifier->flags.q.prim_type) {
2008 shader->info.Geom.OutputType =
2009 gl_to_mesa_prim(state->out_qualifier->prim_type);
2010 } else {
2011 shader->info.Geom.OutputType = MESA_PRIM_UNKNOWN;
2012 }
2013
2014 shader->info.Geom.Invocations = 0;
2015 if (state->in_qualifier->flags.q.invocations) {
2016 unsigned invocations;
2017 if (state->in_qualifier->invocations->
2018 process_qualifier_constant(state, "invocations",
2019 &invocations, false)) {
2020
2021 YYLTYPE loc = state->in_qualifier->invocations->get_location();
2022 if (invocations > state->Const.MaxGeometryShaderInvocations) {
2023 _mesa_glsl_error(&loc, state,
2024 "invocations (%d) exceeds "
2025 "GL_MAX_GEOMETRY_SHADER_INVOCATIONS",
2026 invocations);
2027 }
2028 shader->info.Geom.Invocations = invocations;
2029 }
2030 }
2031 break;
2032
2033 case MESA_SHADER_COMPUTE:
2034 if (state->cs_input_local_size_specified) {
2035 for (int i = 0; i < 3; i++)
2036 shader->info.Comp.LocalSize[i] = state->cs_input_local_size[i];
2037 } else {
2038 for (int i = 0; i < 3; i++)
2039 shader->info.Comp.LocalSize[i] = 0;
2040 }
2041
2042 shader->info.Comp.LocalSizeVariable =
2043 state->cs_input_local_size_variable_specified;
2044
2045 shader->info.Comp.DerivativeGroup = state->cs_derivative_group;
2046
2047 if (state->NV_compute_shader_derivatives_enable) {
2048 /* We allow multiple cs_input_layout nodes, but do not store them in
2049 * a convenient place, so for now live with an empty location error.
2050 */
2051 YYLTYPE loc = {0};
2052 if (shader->info.Comp.DerivativeGroup == DERIVATIVE_GROUP_QUADS) {
2053 if (shader->info.Comp.LocalSize[0] % 2 != 0) {
2054 _mesa_glsl_error(&loc, state, "derivative_group_quadsNV must be used with a "
2055 "local group size whose first dimension "
2056 "is a multiple of 2\n");
2057 }
2058 if (shader->info.Comp.LocalSize[1] % 2 != 0) {
2059 _mesa_glsl_error(&loc, state, "derivative_group_quadsNV must be used with a "
2060 "local group size whose second dimension "
2061 "is a multiple of 2\n");
2062 }
2063 } else if (shader->info.Comp.DerivativeGroup == DERIVATIVE_GROUP_LINEAR) {
2064 if ((shader->info.Comp.LocalSize[0] *
2065 shader->info.Comp.LocalSize[1] *
2066 shader->info.Comp.LocalSize[2]) % 4 != 0) {
2067 _mesa_glsl_error(&loc, state, "derivative_group_linearNV must be used with a "
2068 "local group size whose total number of invocations "
2069 "is a multiple of 4\n");
2070 }
2071 }
2072 }
2073
2074 break;
2075
2076 case MESA_SHADER_FRAGMENT:
2077 shader->redeclares_gl_fragcoord = state->fs_redeclares_gl_fragcoord;
2078 shader->uses_gl_fragcoord = state->fs_uses_gl_fragcoord;
2079 shader->pixel_center_integer = state->fs_pixel_center_integer;
2080 shader->origin_upper_left = state->fs_origin_upper_left;
2081 shader->ARB_fragment_coord_conventions_enable =
2082 state->ARB_fragment_coord_conventions_enable;
2083 shader->EarlyFragmentTests = state->fs_early_fragment_tests;
2084 shader->InnerCoverage = state->fs_inner_coverage;
2085 shader->PostDepthCoverage = state->fs_post_depth_coverage;
2086 shader->PixelInterlockOrdered = state->fs_pixel_interlock_ordered;
2087 shader->PixelInterlockUnordered = state->fs_pixel_interlock_unordered;
2088 shader->SampleInterlockOrdered = state->fs_sample_interlock_ordered;
2089 shader->SampleInterlockUnordered = state->fs_sample_interlock_unordered;
2090 shader->BlendSupport = state->fs_blend_support;
2091 break;
2092
2093 default:
2094 /* Nothing to do. */
2095 break;
2096 }
2097
2098 shader->bindless_sampler = state->bindless_sampler_specified;
2099 shader->bindless_image = state->bindless_image_specified;
2100 shader->bound_sampler = state->bound_sampler_specified;
2101 shader->bound_image = state->bound_image_specified;
2102 shader->redeclares_gl_layer = state->redeclares_gl_layer;
2103 shader->layer_viewport_relative = state->layer_viewport_relative;
2104 }
2105
2106 extern "C" {
2107
2108 static void
assign_subroutine_indexes(struct _mesa_glsl_parse_state * state)2109 assign_subroutine_indexes(struct _mesa_glsl_parse_state *state)
2110 {
2111 int j, k;
2112 int index = 0;
2113
2114 for (j = 0; j < state->num_subroutines; j++) {
2115 while (state->subroutines[j]->subroutine_index == -1) {
2116 for (k = 0; k < state->num_subroutines; k++) {
2117 if (state->subroutines[k]->subroutine_index == index)
2118 break;
2119 else if (k == state->num_subroutines - 1) {
2120 state->subroutines[j]->subroutine_index = index;
2121 }
2122 }
2123 index++;
2124 }
2125 }
2126 }
2127
2128 static void
add_builtin_defines(struct _mesa_glsl_parse_state * state,void (* add_builtin_define)(struct glcpp_parser *,const char *,int),struct glcpp_parser * data,unsigned version,bool es)2129 add_builtin_defines(struct _mesa_glsl_parse_state *state,
2130 void (*add_builtin_define)(struct glcpp_parser *, const char *, int),
2131 struct glcpp_parser *data,
2132 unsigned version,
2133 bool es)
2134 {
2135 unsigned gl_version = state->exts->Version;
2136 gl_api api = state->api;
2137
2138 if (gl_version != 0xff) {
2139 unsigned i;
2140 for (i = 0; i < state->num_supported_versions; i++) {
2141 if (state->supported_versions[i].ver == version &&
2142 state->supported_versions[i].es == es) {
2143 gl_version = state->supported_versions[i].gl_ver;
2144 break;
2145 }
2146 }
2147
2148 if (i == state->num_supported_versions)
2149 return;
2150 }
2151
2152 if (es)
2153 api = API_OPENGLES2;
2154
2155 for (unsigned i = 0;
2156 i < ARRAY_SIZE(_mesa_glsl_supported_extensions); ++i) {
2157 const _mesa_glsl_extension *extension
2158 = &_mesa_glsl_supported_extensions[i];
2159 if (extension->compatible_with_state(state, api, gl_version)) {
2160 add_builtin_define(data, extension->name, 1);
2161 }
2162 }
2163 }
2164
2165 /* Implements parsing checks that we can't do during parsing */
2166 static void
do_late_parsing_checks(struct _mesa_glsl_parse_state * state)2167 do_late_parsing_checks(struct _mesa_glsl_parse_state *state)
2168 {
2169 if (state->stage == MESA_SHADER_COMPUTE && !state->has_compute_shader()) {
2170 YYLTYPE loc;
2171 memset(&loc, 0, sizeof(loc));
2172 _mesa_glsl_error(&loc, state, "Compute shaders require "
2173 "GLSL 4.30 or GLSL ES 3.10");
2174 }
2175 }
2176
2177 static void
opt_shader(const struct gl_constants * consts,const struct gl_extensions * exts,struct gl_shader * shader)2178 opt_shader(const struct gl_constants *consts,
2179 const struct gl_extensions *exts,
2180 struct gl_shader *shader)
2181 {
2182 assert(shader->CompileStatus != COMPILE_FAILURE &&
2183 !shader->ir->is_empty());
2184
2185 const struct gl_shader_compiler_options *options =
2186 &consts->ShaderCompilerOptions[shader->Stage];
2187
2188 /* Do some optimization at compile time to reduce shader IR size
2189 * and reduce later work if the same shader is linked multiple times.
2190 *
2191 * Run it just once, since NIR will do the real optimization.
2192 */
2193 do_common_optimization(shader->ir, false, options, consts->NativeIntegers);
2194
2195 validate_ir_tree(shader->ir);
2196
2197 enum ir_variable_mode other;
2198 switch (shader->Stage) {
2199 case MESA_SHADER_VERTEX:
2200 other = ir_var_shader_in;
2201 break;
2202 case MESA_SHADER_FRAGMENT:
2203 other = ir_var_shader_out;
2204 break;
2205 default:
2206 /* Something invalid to ensure optimize_dead_builtin_uniforms
2207 * doesn't remove anything other than uniforms or constants.
2208 */
2209 other = ir_var_mode_count;
2210 break;
2211 }
2212
2213 optimize_dead_builtin_variables(shader->ir, other);
2214
2215 lower_vector_derefs(shader);
2216
2217 lower_packing_builtins(shader->ir, exts->ARB_shading_language_packing,
2218 exts->ARB_gpu_shader5,
2219 consts->GLSLHasHalfFloatPacking);
2220 do_mat_op_to_vec(shader->ir);
2221
2222 lower_instructions(shader->ir, consts->ForceGLSLAbsSqrt,
2223 exts->ARB_gpu_shader5);
2224
2225 do_vec_index_to_cond_assign(shader->ir);
2226
2227 validate_ir_tree(shader->ir);
2228
2229 /* Retain any live IR, but trash the rest. */
2230 reparent_ir(shader->ir, shader->ir);
2231 }
2232
2233 static bool
can_skip_compile(struct gl_context * ctx,struct gl_shader * shader,const char * source,const blake3_hash source_blake3,bool force_recompile,bool source_has_shader_include)2234 can_skip_compile(struct gl_context *ctx, struct gl_shader *shader,
2235 const char *source, const blake3_hash source_blake3,
2236 bool force_recompile, bool source_has_shader_include)
2237 {
2238 if (!force_recompile) {
2239 if (ctx->Cache) {
2240 char buf[41];
2241 disk_cache_compute_key(ctx->Cache, source, strlen(source),
2242 shader->disk_cache_sha1);
2243 if (disk_cache_has_key(ctx->Cache, shader->disk_cache_sha1)) {
2244 /* We've seen this shader before and know it compiles */
2245 if (ctx->_Shader->Flags & GLSL_CACHE_INFO) {
2246 _mesa_sha1_format(buf, shader->disk_cache_sha1);
2247 fprintf(stderr, "deferring compile of shader: %s\n", buf);
2248 }
2249 shader->CompileStatus = COMPILE_SKIPPED;
2250
2251 free((void *)shader->FallbackSource);
2252
2253 /* Copy pre-processed shader include to fallback source otherwise
2254 * we have no guarantee the shader include source tree has not
2255 * changed.
2256 */
2257 if (source_has_shader_include) {
2258 shader->FallbackSource = strdup(source);
2259 memcpy(shader->fallback_source_blake3, source_blake3,
2260 BLAKE3_OUT_LEN);
2261 } else {
2262 shader->FallbackSource = NULL;
2263 }
2264 memcpy(shader->compiled_source_blake3, source_blake3,
2265 BLAKE3_OUT_LEN);
2266 return true;
2267 }
2268 }
2269 } else {
2270 /* We should only ever end up here if a re-compile has been forced by a
2271 * shader cache miss. In which case we can skip the compile if its
2272 * already been done by a previous fallback or the initial compile call.
2273 */
2274 if (shader->CompileStatus == COMPILE_SUCCESS)
2275 return true;
2276 }
2277
2278 return false;
2279 }
2280
2281 static void
log_compile_skip(struct gl_context * ctx,struct gl_shader * shader)2282 log_compile_skip(struct gl_context *ctx, struct gl_shader *shader)
2283 {
2284 if (ctx->_Shader->Flags & GLSL_DUMP) {
2285 _mesa_log("No GLSL IR for shader %d (shader may be from cache)\n",
2286 shader->Name);
2287 }
2288 }
2289
2290 void
_mesa_glsl_compile_shader(struct gl_context * ctx,struct gl_shader * shader,FILE * dump_ir_file,bool dump_ast,bool dump_hir,bool force_recompile)2291 _mesa_glsl_compile_shader(struct gl_context *ctx, struct gl_shader *shader,
2292 FILE *dump_ir_file, bool dump_ast, bool dump_hir,
2293 bool force_recompile)
2294 {
2295 const char *source;
2296 const uint8_t *source_blake3;
2297
2298 if (force_recompile && shader->FallbackSource) {
2299 source = shader->FallbackSource;
2300 source_blake3 = shader->fallback_source_blake3;
2301 } else {
2302 source = shader->Source;
2303 source_blake3 = shader->source_blake3;
2304 }
2305
2306 /* Note this will be true for shaders the have #include inside comments
2307 * however that should be rare enough not to worry about.
2308 */
2309 bool source_has_shader_include =
2310 strstr(source, "#include") == NULL ? false : true;
2311
2312 /* If there was no shader include we can check the shader cache and skip
2313 * compilation before we run the preprocessor. We never skip compiling
2314 * shaders that use ARB_shading_language_include because we would need to
2315 * keep duplicate copies of the shader include source tree and paths.
2316 */
2317 if (!source_has_shader_include &&
2318 can_skip_compile(ctx, shader, source, source_blake3, force_recompile,
2319 false)) {
2320 log_compile_skip(ctx, shader);
2321 return;
2322 }
2323
2324 struct _mesa_glsl_parse_state *state =
2325 new(shader) _mesa_glsl_parse_state(ctx, shader->Stage, shader);
2326
2327 if (ctx->Const.GenerateTemporaryNames)
2328 (void) p_atomic_cmpxchg(&ir_variable::temporaries_allocate_names,
2329 false, true);
2330
2331 if (!source_has_shader_include || !force_recompile) {
2332 state->error = glcpp_preprocess(state, &source, &state->info_log,
2333 add_builtin_defines, state, ctx);
2334 }
2335
2336 /* Now that we have run the preprocessor we can check the shader cache and
2337 * skip compilation if possible for those shaders that contained a shader
2338 * include.
2339 */
2340 if (source_has_shader_include &&
2341 can_skip_compile(ctx, shader, source, source_blake3, force_recompile,
2342 true)) {
2343 log_compile_skip(ctx, shader);
2344 return;
2345 }
2346
2347 if (!state->error) {
2348 _mesa_glsl_lexer_ctor(state, source);
2349 _mesa_glsl_parse(state);
2350 _mesa_glsl_lexer_dtor(state);
2351 do_late_parsing_checks(state);
2352 }
2353
2354 if (dump_ast) {
2355 foreach_list_typed(ast_node, ast, link, &state->translation_unit) {
2356 ast->print();
2357 }
2358 printf("\n\n");
2359 }
2360
2361 ralloc_free(shader->ir);
2362 ralloc_free(shader->nir);
2363 shader->nir = NULL;
2364 shader->ir = new(shader) exec_list;
2365 if (!state->error && !state->translation_unit.is_empty())
2366 _mesa_ast_to_hir(shader->ir, state);
2367
2368 if (!state->error) {
2369 validate_ir_tree(shader->ir);
2370
2371 /* Print out the unoptimized IR. */
2372 if (dump_hir) {
2373 _mesa_print_ir(stdout, shader->ir, state);
2374 }
2375 }
2376
2377 if (shader->InfoLog)
2378 ralloc_free(shader->InfoLog);
2379
2380 if (!state->error)
2381 set_shader_inout_layout(shader, state);
2382
2383 shader->CompileStatus = state->error ? COMPILE_FAILURE : COMPILE_SUCCESS;
2384 shader->InfoLog = state->info_log;
2385 shader->Version = state->language_version;
2386 shader->IsES = state->es_shader;
2387 shader->has_implicit_conversions = state->has_implicit_conversions();
2388 shader->has_implicit_int_to_uint_conversion =
2389 state->has_implicit_int_to_uint_conversion();
2390 shader->KHR_shader_subgroup_basic_enable = state->KHR_shader_subgroup_basic_enable;
2391
2392 struct gl_shader_compiler_options *options =
2393 &ctx->Const.ShaderCompilerOptions[shader->Stage];
2394
2395 if (!state->error && !shader->ir->is_empty()) {
2396 if (state->es_shader &&
2397 (options->LowerPrecisionFloat16 || options->LowerPrecisionInt16))
2398 lower_precision(options, shader->ir);
2399 lower_builtins(shader->ir);
2400 assign_subroutine_indexes(state);
2401 lower_subroutine(shader->ir, state);
2402 opt_shader(&ctx->Const, &ctx->Extensions, shader);
2403 }
2404
2405 if (!force_recompile) {
2406 free((void *)shader->FallbackSource);
2407
2408 /* Copy pre-processed shader include to fallback source otherwise we
2409 * have no guarantee the shader include source tree has not changed.
2410 */
2411 if (source_has_shader_include) {
2412 shader->FallbackSource = strdup(source);
2413 memcpy(shader->fallback_source_blake3, source_blake3, BLAKE3_OUT_LEN);
2414 } else {
2415 shader->FallbackSource = NULL;
2416 }
2417 }
2418
2419 delete state->symbols;
2420 ralloc_free(state);
2421
2422 if (ctx->_Shader && ctx->_Shader->Flags & GLSL_DUMP) {
2423 if (shader->CompileStatus) {
2424 assert(shader->ir);
2425 _mesa_log("GLSL IR for shader %d:\n", shader->Name);
2426 _mesa_print_ir(mesa_log_get_file(), shader->ir, NULL);
2427 _mesa_log("\n\n");
2428 } else {
2429 _mesa_log("GLSL shader %d failed to compile.\n", shader->Name);
2430 }
2431 if (shader->InfoLog && shader->InfoLog[0] != 0) {
2432 _mesa_log("GLSL shader %d info log:\n", shader->Name);
2433 _mesa_log("%s\n", shader->InfoLog);
2434 }
2435 }
2436
2437 if (dump_ir_file) {
2438 if (shader->CompileStatus) {
2439 assert(shader->ir);
2440 _mesa_print_ir(dump_ir_file, shader->ir, NULL);
2441 }
2442 }
2443
2444 if (shader->CompileStatus == COMPILE_SUCCESS) {
2445 memcpy(shader->compiled_source_blake3, source_blake3, BLAKE3_OUT_LEN);
2446
2447 shader->nir = glsl_to_nir(shader, options->NirOptions, source_blake3);
2448 }
2449
2450 if (ctx->Cache && shader->CompileStatus == COMPILE_SUCCESS) {
2451 char sha1_buf[41];
2452 disk_cache_put_key(ctx->Cache, shader->disk_cache_sha1);
2453 if (ctx->_Shader->Flags & GLSL_CACHE_INFO) {
2454 _mesa_sha1_format(sha1_buf, shader->disk_cache_sha1);
2455 fprintf(stderr, "marking shader: %s\n", sha1_buf);
2456 }
2457 }
2458 }
2459
2460 } /* extern "C" */
2461 /**
2462 * Do the set of common optimizations passes
2463 *
2464 * \param ir List of instructions to be optimized
2465 * \param linked Is the shader linked? This enables
2466 * optimizations passes that remove code at
2467 * global scope and could cause linking to
2468 * fail.
2469 * \param uniform_locations_assigned Have locations already been assigned for
2470 * uniforms? This prevents the declarations
2471 * of unused uniforms from being removed.
2472 * The setting of this flag only matters if
2473 * \c linked is \c true.
2474 * \param options The driver's preferred shader options.
2475 * \param native_integers Selects optimizations that depend on the
2476 * implementations supporting integers
2477 * natively (as opposed to supporting
2478 * integers in floating point registers).
2479 */
2480 bool
do_common_optimization(exec_list * ir,bool linked,const struct gl_shader_compiler_options * options,bool native_integers)2481 do_common_optimization(exec_list *ir, bool linked,
2482 const struct gl_shader_compiler_options *options,
2483 bool native_integers)
2484 {
2485 const bool debug = false;
2486 bool progress = false;
2487
2488 #define OPT(PASS, ...) do { \
2489 if (debug) { \
2490 fprintf(stderr, "START GLSL optimization %s\n", #PASS); \
2491 const bool opt_progress = PASS(__VA_ARGS__); \
2492 progress = opt_progress || progress; \
2493 if (opt_progress) \
2494 _mesa_print_ir(stderr, ir, NULL); \
2495 fprintf(stderr, "GLSL optimization %s: %s progress\n", \
2496 #PASS, opt_progress ? "made" : "no"); \
2497 } else { \
2498 progress = PASS(__VA_ARGS__) || progress; \
2499 } \
2500 } while (false)
2501
2502 OPT(propagate_invariance, ir);
2503 OPT(do_if_simplification, ir);
2504 OPT(opt_flatten_nested_if_blocks, ir);
2505
2506 if (options->OptimizeForAOS && !linked)
2507 OPT(opt_flip_matrices, ir);
2508
2509 OPT(do_dead_code_unlinked, ir);
2510 OPT(do_tree_grafting, ir);
2511 OPT(do_minmax_prune, ir);
2512 OPT(do_rebalance_tree, ir);
2513 OPT(do_algebraic, ir, native_integers, options);
2514 OPT(do_lower_jumps, ir, true, options->EmitNoCont);
2515
2516 /* If an optimization pass fails to preserve the invariant flag, calling
2517 * the pass only once earlier may result in incorrect code generation. Always call
2518 * propagate_invariance() last to avoid this possibility.
2519 */
2520 OPT(propagate_invariance, ir);
2521
2522 #undef OPT
2523
2524 return progress;
2525 }
2526