Path: blob/21.2-virgl/src/compiler/glsl/glsl_parser_extras.cpp
4547 views
/*1* Copyright © 2008, 2009 Intel Corporation2*3* Permission is hereby granted, free of charge, to any person obtaining a4* copy of this software and associated documentation files (the "Software"),5* to deal in the Software without restriction, including without limitation6* the rights to use, copy, modify, merge, publish, distribute, sublicense,7* and/or sell copies of the Software, and to permit persons to whom the8* Software is furnished to do so, subject to the following conditions:9*10* The above copyright notice and this permission notice (including the next11* paragraph) shall be included in all copies or substantial portions of the12* Software.13*14* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR15* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,16* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL17* THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER18* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING19* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER20* DEALINGS IN THE SOFTWARE.21*/22#include <inttypes.h> /* for PRIx64 macro */23#include <stdio.h>24#include <stdarg.h>25#include <string.h>26#include <assert.h>2728#include "main/context.h"29#include "main/debug_output.h"30#include "main/formats.h"31#include "main/shaderobj.h"32#include "util/u_atomic.h" /* for p_atomic_cmpxchg */33#include "util/ralloc.h"34#include "util/disk_cache.h"35#include "util/mesa-sha1.h"36#include "ast.h"37#include "glsl_parser_extras.h"38#include "glsl_parser.h"39#include "ir_optimization.h"40#include "loop_analysis.h"41#include "builtin_functions.h"4243/**44* Format a short human-readable description of the given GLSL version.45*/46const char *47glsl_compute_version_string(void *mem_ctx, bool is_es, unsigned version)48{49return ralloc_asprintf(mem_ctx, "GLSL%s %d.%02d", is_es ? " ES" : "",50version / 100, version % 100);51}525354static const unsigned known_desktop_glsl_versions[] =55{ 110, 120, 130, 140, 150, 330, 400, 410, 420, 430, 440, 450, 460 };56static const unsigned known_desktop_gl_versions[] =57{ 20, 21, 30, 31, 32, 33, 40, 41, 42, 43, 44, 45, 46 };585960_mesa_glsl_parse_state::_mesa_glsl_parse_state(struct gl_context *_ctx,61gl_shader_stage stage,62void *mem_ctx)63: ctx(_ctx), cs_input_local_size_specified(false), cs_input_local_size(),64switch_state(), warnings_enabled(true)65{66assert(stage < MESA_SHADER_STAGES);67this->stage = stage;6869this->scanner = NULL;70this->translation_unit.make_empty();71this->symbols = new(mem_ctx) glsl_symbol_table;7273this->linalloc = linear_alloc_parent(this, 0);7475this->info_log = ralloc_strdup(mem_ctx, "");76this->error = false;77this->loop_nesting_ast = NULL;7879this->uses_builtin_functions = false;8081/* Set default language version and extensions */82this->language_version = 110;83this->forced_language_version = ctx->Const.ForceGLSLVersion;84if (ctx->Const.GLSLZeroInit == 1) {85this->zero_init = (1u << ir_var_auto) | (1u << ir_var_temporary) | (1u << ir_var_shader_out);86} else if (ctx->Const.GLSLZeroInit == 2) {87this->zero_init = (1u << ir_var_auto) | (1u << ir_var_temporary) | (1u << ir_var_function_out);88} else {89this->zero_init = 0;90}91this->gl_version = 20;92this->compat_shader = true;93this->es_shader = false;94this->ARB_texture_rectangle_enable = true;9596/* OpenGL ES 2.0 has different defaults from desktop GL. */97if (ctx->API == API_OPENGLES2) {98this->language_version = 100;99this->es_shader = true;100this->ARB_texture_rectangle_enable = false;101}102103this->extensions = &ctx->Extensions;104105this->Const.MaxLights = ctx->Const.MaxLights;106this->Const.MaxClipPlanes = ctx->Const.MaxClipPlanes;107this->Const.MaxTextureUnits = ctx->Const.MaxTextureUnits;108this->Const.MaxTextureCoords = ctx->Const.MaxTextureCoordUnits;109this->Const.MaxVertexAttribs = ctx->Const.Program[MESA_SHADER_VERTEX].MaxAttribs;110this->Const.MaxVertexUniformComponents = ctx->Const.Program[MESA_SHADER_VERTEX].MaxUniformComponents;111this->Const.MaxVertexTextureImageUnits = ctx->Const.Program[MESA_SHADER_VERTEX].MaxTextureImageUnits;112this->Const.MaxCombinedTextureImageUnits = ctx->Const.MaxCombinedTextureImageUnits;113this->Const.MaxTextureImageUnits = ctx->Const.Program[MESA_SHADER_FRAGMENT].MaxTextureImageUnits;114this->Const.MaxFragmentUniformComponents = ctx->Const.Program[MESA_SHADER_FRAGMENT].MaxUniformComponents;115this->Const.MinProgramTexelOffset = ctx->Const.MinProgramTexelOffset;116this->Const.MaxProgramTexelOffset = ctx->Const.MaxProgramTexelOffset;117118this->Const.MaxDrawBuffers = ctx->Const.MaxDrawBuffers;119120this->Const.MaxDualSourceDrawBuffers = ctx->Const.MaxDualSourceDrawBuffers;121122/* 1.50 constants */123this->Const.MaxVertexOutputComponents = ctx->Const.Program[MESA_SHADER_VERTEX].MaxOutputComponents;124this->Const.MaxGeometryInputComponents = ctx->Const.Program[MESA_SHADER_GEOMETRY].MaxInputComponents;125this->Const.MaxGeometryOutputComponents = ctx->Const.Program[MESA_SHADER_GEOMETRY].MaxOutputComponents;126this->Const.MaxGeometryShaderInvocations = ctx->Const.MaxGeometryShaderInvocations;127this->Const.MaxFragmentInputComponents = ctx->Const.Program[MESA_SHADER_FRAGMENT].MaxInputComponents;128this->Const.MaxGeometryTextureImageUnits = ctx->Const.Program[MESA_SHADER_GEOMETRY].MaxTextureImageUnits;129this->Const.MaxGeometryOutputVertices = ctx->Const.MaxGeometryOutputVertices;130this->Const.MaxGeometryTotalOutputComponents = ctx->Const.MaxGeometryTotalOutputComponents;131this->Const.MaxGeometryUniformComponents = ctx->Const.Program[MESA_SHADER_GEOMETRY].MaxUniformComponents;132133this->Const.MaxVertexAtomicCounters = ctx->Const.Program[MESA_SHADER_VERTEX].MaxAtomicCounters;134this->Const.MaxTessControlAtomicCounters = ctx->Const.Program[MESA_SHADER_TESS_CTRL].MaxAtomicCounters;135this->Const.MaxTessEvaluationAtomicCounters = ctx->Const.Program[MESA_SHADER_TESS_EVAL].MaxAtomicCounters;136this->Const.MaxGeometryAtomicCounters = ctx->Const.Program[MESA_SHADER_GEOMETRY].MaxAtomicCounters;137this->Const.MaxFragmentAtomicCounters = ctx->Const.Program[MESA_SHADER_FRAGMENT].MaxAtomicCounters;138this->Const.MaxComputeAtomicCounters = ctx->Const.Program[MESA_SHADER_COMPUTE].MaxAtomicCounters;139this->Const.MaxCombinedAtomicCounters = ctx->Const.MaxCombinedAtomicCounters;140this->Const.MaxAtomicBufferBindings = ctx->Const.MaxAtomicBufferBindings;141this->Const.MaxVertexAtomicCounterBuffers =142ctx->Const.Program[MESA_SHADER_VERTEX].MaxAtomicBuffers;143this->Const.MaxTessControlAtomicCounterBuffers =144ctx->Const.Program[MESA_SHADER_TESS_CTRL].MaxAtomicBuffers;145this->Const.MaxTessEvaluationAtomicCounterBuffers =146ctx->Const.Program[MESA_SHADER_TESS_EVAL].MaxAtomicBuffers;147this->Const.MaxGeometryAtomicCounterBuffers =148ctx->Const.Program[MESA_SHADER_GEOMETRY].MaxAtomicBuffers;149this->Const.MaxFragmentAtomicCounterBuffers =150ctx->Const.Program[MESA_SHADER_FRAGMENT].MaxAtomicBuffers;151this->Const.MaxComputeAtomicCounterBuffers =152ctx->Const.Program[MESA_SHADER_COMPUTE].MaxAtomicBuffers;153this->Const.MaxCombinedAtomicCounterBuffers =154ctx->Const.MaxCombinedAtomicBuffers;155this->Const.MaxAtomicCounterBufferSize =156ctx->Const.MaxAtomicBufferSize;157158/* ARB_enhanced_layouts constants */159this->Const.MaxTransformFeedbackBuffers = ctx->Const.MaxTransformFeedbackBuffers;160this->Const.MaxTransformFeedbackInterleavedComponents = ctx->Const.MaxTransformFeedbackInterleavedComponents;161162/* Compute shader constants */163for (unsigned i = 0; i < ARRAY_SIZE(this->Const.MaxComputeWorkGroupCount); i++)164this->Const.MaxComputeWorkGroupCount[i] = ctx->Const.MaxComputeWorkGroupCount[i];165for (unsigned i = 0; i < ARRAY_SIZE(this->Const.MaxComputeWorkGroupSize); i++)166this->Const.MaxComputeWorkGroupSize[i] = ctx->Const.MaxComputeWorkGroupSize[i];167168this->Const.MaxComputeTextureImageUnits = ctx->Const.Program[MESA_SHADER_COMPUTE].MaxTextureImageUnits;169this->Const.MaxComputeUniformComponents = ctx->Const.Program[MESA_SHADER_COMPUTE].MaxUniformComponents;170171this->Const.MaxImageUnits = ctx->Const.MaxImageUnits;172this->Const.MaxCombinedShaderOutputResources = ctx->Const.MaxCombinedShaderOutputResources;173this->Const.MaxImageSamples = ctx->Const.MaxImageSamples;174this->Const.MaxVertexImageUniforms = ctx->Const.Program[MESA_SHADER_VERTEX].MaxImageUniforms;175this->Const.MaxTessControlImageUniforms = ctx->Const.Program[MESA_SHADER_TESS_CTRL].MaxImageUniforms;176this->Const.MaxTessEvaluationImageUniforms = ctx->Const.Program[MESA_SHADER_TESS_EVAL].MaxImageUniforms;177this->Const.MaxGeometryImageUniforms = ctx->Const.Program[MESA_SHADER_GEOMETRY].MaxImageUniforms;178this->Const.MaxFragmentImageUniforms = ctx->Const.Program[MESA_SHADER_FRAGMENT].MaxImageUniforms;179this->Const.MaxComputeImageUniforms = ctx->Const.Program[MESA_SHADER_COMPUTE].MaxImageUniforms;180this->Const.MaxCombinedImageUniforms = ctx->Const.MaxCombinedImageUniforms;181182/* ARB_viewport_array */183this->Const.MaxViewports = ctx->Const.MaxViewports;184185/* tessellation shader constants */186this->Const.MaxPatchVertices = ctx->Const.MaxPatchVertices;187this->Const.MaxTessGenLevel = ctx->Const.MaxTessGenLevel;188this->Const.MaxTessControlInputComponents = ctx->Const.Program[MESA_SHADER_TESS_CTRL].MaxInputComponents;189this->Const.MaxTessControlOutputComponents = ctx->Const.Program[MESA_SHADER_TESS_CTRL].MaxOutputComponents;190this->Const.MaxTessControlTextureImageUnits = ctx->Const.Program[MESA_SHADER_TESS_CTRL].MaxTextureImageUnits;191this->Const.MaxTessEvaluationInputComponents = ctx->Const.Program[MESA_SHADER_TESS_EVAL].MaxInputComponents;192this->Const.MaxTessEvaluationOutputComponents = ctx->Const.Program[MESA_SHADER_TESS_EVAL].MaxOutputComponents;193this->Const.MaxTessEvaluationTextureImageUnits = ctx->Const.Program[MESA_SHADER_TESS_EVAL].MaxTextureImageUnits;194this->Const.MaxTessPatchComponents = ctx->Const.MaxTessPatchComponents;195this->Const.MaxTessControlTotalOutputComponents = ctx->Const.MaxTessControlTotalOutputComponents;196this->Const.MaxTessControlUniformComponents = ctx->Const.Program[MESA_SHADER_TESS_CTRL].MaxUniformComponents;197this->Const.MaxTessEvaluationUniformComponents = ctx->Const.Program[MESA_SHADER_TESS_EVAL].MaxUniformComponents;198199/* GL 4.5 / OES_sample_variables */200this->Const.MaxSamples = ctx->Const.MaxSamples;201202this->current_function = NULL;203this->toplevel_ir = NULL;204this->found_return = false;205this->found_begin_interlock = false;206this->found_end_interlock = false;207this->all_invariant = false;208this->user_structures = NULL;209this->num_user_structures = 0;210this->num_subroutines = 0;211this->subroutines = NULL;212this->num_subroutine_types = 0;213this->subroutine_types = NULL;214215/* supported_versions should be large enough to support the known desktop216* GLSL versions plus 4 GLES versions (ES 1.00, ES 3.00, ES 3.10, ES 3.20)217*/218STATIC_ASSERT((ARRAY_SIZE(known_desktop_glsl_versions) + 4) ==219ARRAY_SIZE(this->supported_versions));220221/* Populate the list of supported GLSL versions */222/* FINISHME: Once the OpenGL 3.0 'forward compatible' context or223* the OpenGL 3.2 Core context is supported, this logic will need224* change. Older versions of GLSL are no longer supported225* outside the compatibility contexts of 3.x.226*/227this->num_supported_versions = 0;228if (_mesa_is_desktop_gl(ctx)) {229for (unsigned i = 0; i < ARRAY_SIZE(known_desktop_glsl_versions); i++) {230if (known_desktop_glsl_versions[i] <= ctx->Const.GLSLVersion) {231this->supported_versions[this->num_supported_versions].ver232= known_desktop_glsl_versions[i];233this->supported_versions[this->num_supported_versions].gl_ver234= known_desktop_gl_versions[i];235this->supported_versions[this->num_supported_versions].es = false;236this->num_supported_versions++;237}238}239}240if (ctx->API == API_OPENGLES2 || ctx->Extensions.ARB_ES2_compatibility) {241this->supported_versions[this->num_supported_versions].ver = 100;242this->supported_versions[this->num_supported_versions].gl_ver = 20;243this->supported_versions[this->num_supported_versions].es = true;244this->num_supported_versions++;245}246if (_mesa_is_gles3(ctx) || ctx->Extensions.ARB_ES3_compatibility) {247this->supported_versions[this->num_supported_versions].ver = 300;248this->supported_versions[this->num_supported_versions].gl_ver = 30;249this->supported_versions[this->num_supported_versions].es = true;250this->num_supported_versions++;251}252if (_mesa_is_gles31(ctx) || ctx->Extensions.ARB_ES3_1_compatibility) {253this->supported_versions[this->num_supported_versions].ver = 310;254this->supported_versions[this->num_supported_versions].gl_ver = 31;255this->supported_versions[this->num_supported_versions].es = true;256this->num_supported_versions++;257}258if ((ctx->API == API_OPENGLES2 && ctx->Version >= 32) ||259ctx->Extensions.ARB_ES3_2_compatibility) {260this->supported_versions[this->num_supported_versions].ver = 320;261this->supported_versions[this->num_supported_versions].gl_ver = 32;262this->supported_versions[this->num_supported_versions].es = true;263this->num_supported_versions++;264}265266/* Create a string for use in error messages to tell the user which GLSL267* versions are supported.268*/269char *supported = ralloc_strdup(this, "");270for (unsigned i = 0; i < this->num_supported_versions; i++) {271unsigned ver = this->supported_versions[i].ver;272const char *const prefix = (i == 0)273? ""274: ((i == this->num_supported_versions - 1) ? ", and " : ", ");275const char *const suffix = (this->supported_versions[i].es) ? " ES" : "";276277ralloc_asprintf_append(& supported, "%s%u.%02u%s",278prefix,279ver / 100, ver % 100,280suffix);281}282283this->supported_version_string = supported;284285if (ctx->Const.ForceGLSLExtensionsWarn)286_mesa_glsl_process_extension("all", NULL, "warn", NULL, this);287288this->default_uniform_qualifier = new(this) ast_type_qualifier();289this->default_uniform_qualifier->flags.q.shared = 1;290this->default_uniform_qualifier->flags.q.column_major = 1;291292this->default_shader_storage_qualifier = new(this) ast_type_qualifier();293this->default_shader_storage_qualifier->flags.q.shared = 1;294this->default_shader_storage_qualifier->flags.q.column_major = 1;295296this->fs_uses_gl_fragcoord = false;297this->fs_redeclares_gl_fragcoord = false;298this->fs_origin_upper_left = false;299this->fs_pixel_center_integer = false;300this->fs_redeclares_gl_fragcoord_with_no_layout_qualifiers = false;301302this->gs_input_prim_type_specified = false;303this->tcs_output_vertices_specified = false;304this->gs_input_size = 0;305this->in_qualifier = new(this) ast_type_qualifier();306this->out_qualifier = new(this) ast_type_qualifier();307this->fs_early_fragment_tests = false;308this->fs_inner_coverage = false;309this->fs_post_depth_coverage = false;310this->fs_pixel_interlock_ordered = false;311this->fs_pixel_interlock_unordered = false;312this->fs_sample_interlock_ordered = false;313this->fs_sample_interlock_unordered = false;314this->fs_blend_support = 0;315memset(this->atomic_counter_offsets, 0,316sizeof(this->atomic_counter_offsets));317this->allow_extension_directive_midshader =318ctx->Const.AllowGLSLExtensionDirectiveMidShader;319this->allow_glsl_120_subset_in_110 =320ctx->Const.AllowGLSL120SubsetIn110;321this->allow_builtin_variable_redeclaration =322ctx->Const.AllowGLSLBuiltinVariableRedeclaration;323this->ignore_write_to_readonly_var =324ctx->Const.GLSLIgnoreWriteToReadonlyVar;325326this->cs_input_local_size_variable_specified = false;327328/* ARB_bindless_texture */329this->bindless_sampler_specified = false;330this->bindless_image_specified = false;331this->bound_sampler_specified = false;332this->bound_image_specified = false;333334this->language_version = this->forced_language_version ?335this->forced_language_version : this->language_version;336set_valid_gl_and_glsl_versions(NULL);337}338339/**340* Determine whether the current GLSL version is sufficiently high to support341* a certain feature, and generate an error message if it isn't.342*343* \param required_glsl_version and \c required_glsl_es_version are344* interpreted as they are in _mesa_glsl_parse_state::is_version().345*346* \param locp is the parser location where the error should be reported.347*348* \param fmt (and additional arguments) constitute a printf-style error349* message to report if the version check fails. Information about the350* current and required GLSL versions will be appended. So, for example, if351* the GLSL version being compiled is 1.20, and check_version(130, 300, locp,352* "foo unsupported") is called, the error message will be "foo unsupported in353* GLSL 1.20 (GLSL 1.30 or GLSL 3.00 ES required)".354*/355bool356_mesa_glsl_parse_state::check_version(unsigned required_glsl_version,357unsigned required_glsl_es_version,358YYLTYPE *locp, const char *fmt, ...)359{360if (this->is_version(required_glsl_version, required_glsl_es_version))361return true;362363va_list args;364va_start(args, fmt);365char *problem = ralloc_vasprintf(this, fmt, args);366va_end(args);367const char *glsl_version_string368= glsl_compute_version_string(this, false, required_glsl_version);369const char *glsl_es_version_string370= glsl_compute_version_string(this, true, required_glsl_es_version);371const char *requirement_string = "";372if (required_glsl_version && required_glsl_es_version) {373requirement_string = ralloc_asprintf(this, " (%s or %s required)",374glsl_version_string,375glsl_es_version_string);376} else if (required_glsl_version) {377requirement_string = ralloc_asprintf(this, " (%s required)",378glsl_version_string);379} else if (required_glsl_es_version) {380requirement_string = ralloc_asprintf(this, " (%s required)",381glsl_es_version_string);382}383_mesa_glsl_error(locp, this, "%s in %s%s",384problem, this->get_version_string(),385requirement_string);386387return false;388}389390/**391* This makes sure any GLSL versions defined or overridden are valid. If not it392* sets a valid value.393*/394void395_mesa_glsl_parse_state::set_valid_gl_and_glsl_versions(YYLTYPE *locp)396{397bool supported = false;398for (unsigned i = 0; i < this->num_supported_versions; i++) {399if (this->supported_versions[i].ver == this->language_version400&& this->supported_versions[i].es == this->es_shader) {401this->gl_version = this->supported_versions[i].gl_ver;402supported = true;403break;404}405}406407if (!supported) {408if (locp) {409_mesa_glsl_error(locp, this, "%s is not supported. "410"Supported versions are: %s",411this->get_version_string(),412this->supported_version_string);413}414415/* On exit, the language_version must be set to a valid value.416* Later calls to _mesa_glsl_initialize_types will misbehave if417* the version is invalid.418*/419switch (this->ctx->API) {420case API_OPENGL_COMPAT:421case API_OPENGL_CORE:422this->language_version = this->ctx->Const.GLSLVersion;423break;424425case API_OPENGLES:426FALLTHROUGH;427428case API_OPENGLES2:429this->language_version = 100;430break;431}432}433}434435/**436* Process a GLSL #version directive.437*438* \param version is the integer that follows the #version token.439*440* \param ident is a string identifier that follows the integer, if any is441* present. Otherwise NULL.442*/443void444_mesa_glsl_parse_state::process_version_directive(YYLTYPE *locp, int version,445const char *ident)446{447bool es_token_present = false;448bool compat_token_present = false;449if (ident) {450if (strcmp(ident, "es") == 0) {451es_token_present = true;452} else if (version >= 150) {453if (strcmp(ident, "core") == 0) {454/* Accept the token. There's no need to record that this is455* a core profile shader since that's the only profile we support.456*/457} else if (strcmp(ident, "compatibility") == 0) {458compat_token_present = true;459460if (this->ctx->API != API_OPENGL_COMPAT) {461_mesa_glsl_error(locp, this,462"the compatibility profile is not supported");463}464} else {465_mesa_glsl_error(locp, this,466"\"%s\" is not a valid shading language profile; "467"if present, it must be \"core\"", ident);468}469} else {470_mesa_glsl_error(locp, this,471"illegal text following version number");472}473}474475this->es_shader = es_token_present;476if (version == 100) {477if (es_token_present) {478_mesa_glsl_error(locp, this,479"GLSL 1.00 ES should be selected using "480"`#version 100'");481} else {482this->es_shader = true;483}484}485486if (this->es_shader) {487this->ARB_texture_rectangle_enable = false;488}489490if (this->forced_language_version)491this->language_version = this->forced_language_version;492else493this->language_version = version;494495this->compat_shader = compat_token_present ||496(this->ctx->API == API_OPENGL_COMPAT &&497this->language_version == 140) ||498(!this->es_shader && this->language_version < 140);499500set_valid_gl_and_glsl_versions(locp);501}502503504/* This helper function will append the given message to the shader's505info log and report it via GL_ARB_debug_output. Per that extension,506'type' is one of the enum values classifying the message, and507'id' is the implementation-defined ID of the given message. */508static void509_mesa_glsl_msg(const YYLTYPE *locp, _mesa_glsl_parse_state *state,510GLenum type, const char *fmt, va_list ap)511{512bool error = (type == MESA_DEBUG_TYPE_ERROR);513GLuint msg_id = 0;514515assert(state->info_log != NULL);516517/* Get the offset that the new message will be written to. */518int msg_offset = strlen(state->info_log);519520if (locp->path) {521ralloc_asprintf_append(&state->info_log, "\"%s\"", locp->path);522} else {523ralloc_asprintf_append(&state->info_log, "%u", locp->source);524}525ralloc_asprintf_append(&state->info_log, ":%u(%u): %s: ",526locp->first_line, locp->first_column,527error ? "error" : "warning");528529ralloc_vasprintf_append(&state->info_log, fmt, ap);530531const char *const msg = &state->info_log[msg_offset];532struct gl_context *ctx = state->ctx;533534/* Report the error via GL_ARB_debug_output. */535_mesa_shader_debug(ctx, type, &msg_id, msg);536537ralloc_strcat(&state->info_log, "\n");538}539540void541_mesa_glsl_error(YYLTYPE *locp, _mesa_glsl_parse_state *state,542const char *fmt, ...)543{544va_list ap;545546state->error = true;547548va_start(ap, fmt);549_mesa_glsl_msg(locp, state, MESA_DEBUG_TYPE_ERROR, fmt, ap);550va_end(ap);551}552553554void555_mesa_glsl_warning(const YYLTYPE *locp, _mesa_glsl_parse_state *state,556const char *fmt, ...)557{558if (state->warnings_enabled) {559va_list ap;560561va_start(ap, fmt);562_mesa_glsl_msg(locp, state, MESA_DEBUG_TYPE_OTHER, fmt, ap);563va_end(ap);564}565}566567568/**569* Enum representing the possible behaviors that can be specified in570* an #extension directive.571*/572enum ext_behavior {573extension_disable,574extension_enable,575extension_require,576extension_warn577};578579/**580* Element type for _mesa_glsl_supported_extensions581*/582struct _mesa_glsl_extension {583/**584* Name of the extension when referred to in a GLSL extension585* statement586*/587const char *name;588589/**590* Whether this extension is a part of AEP591*/592bool aep;593594/**595* Predicate that checks whether the relevant extension is available for596* this context.597*/598bool (*available_pred)(const struct gl_context *,599gl_api api, uint8_t version);600601/**602* Flag in the _mesa_glsl_parse_state struct that should be set603* when this extension is enabled.604*605* See note in _mesa_glsl_extension::supported_flag about "pointer606* to member" types.607*/608bool _mesa_glsl_parse_state::* enable_flag;609610/**611* Flag in the _mesa_glsl_parse_state struct that should be set612* when the shader requests "warn" behavior for this extension.613*614* See note in _mesa_glsl_extension::supported_flag about "pointer615* to member" types.616*/617bool _mesa_glsl_parse_state::* warn_flag;618619620bool compatible_with_state(const _mesa_glsl_parse_state *state,621gl_api api, uint8_t gl_version) const;622void set_flags(_mesa_glsl_parse_state *state, ext_behavior behavior) const;623};624625/** Checks if the context supports a user-facing extension */626#define EXT(name_str, driver_cap, ...) \627static UNUSED bool \628has_##name_str(const struct gl_context *ctx, gl_api api, uint8_t version) \629{ \630return ctx->Extensions.driver_cap && (version >= \631_mesa_extension_table[MESA_EXTENSION_##name_str].version[api]); \632}633#include "main/extensions_table.h"634#undef EXT635636#define EXT(NAME) \637{ "GL_" #NAME, false, has_##NAME, \638&_mesa_glsl_parse_state::NAME##_enable, \639&_mesa_glsl_parse_state::NAME##_warn }640641#define EXT_AEP(NAME) \642{ "GL_" #NAME, true, has_##NAME, \643&_mesa_glsl_parse_state::NAME##_enable, \644&_mesa_glsl_parse_state::NAME##_warn }645646/**647* Table of extensions that can be enabled/disabled within a shader,648* and the conditions under which they are supported.649*/650static const _mesa_glsl_extension _mesa_glsl_supported_extensions[] = {651/* ARB extensions go here, sorted alphabetically.652*/653EXT(ARB_ES3_1_compatibility),654EXT(ARB_ES3_2_compatibility),655EXT(ARB_arrays_of_arrays),656EXT(ARB_bindless_texture),657EXT(ARB_compatibility),658EXT(ARB_compute_shader),659EXT(ARB_compute_variable_group_size),660EXT(ARB_conservative_depth),661EXT(ARB_cull_distance),662EXT(ARB_derivative_control),663EXT(ARB_draw_buffers),664EXT(ARB_draw_instanced),665EXT(ARB_enhanced_layouts),666EXT(ARB_explicit_attrib_location),667EXT(ARB_explicit_uniform_location),668EXT(ARB_fragment_coord_conventions),669EXT(ARB_fragment_layer_viewport),670EXT(ARB_fragment_shader_interlock),671EXT(ARB_gpu_shader5),672EXT(ARB_gpu_shader_fp64),673EXT(ARB_gpu_shader_int64),674EXT(ARB_post_depth_coverage),675EXT(ARB_sample_shading),676EXT(ARB_separate_shader_objects),677EXT(ARB_shader_atomic_counter_ops),678EXT(ARB_shader_atomic_counters),679EXT(ARB_shader_ballot),680EXT(ARB_shader_bit_encoding),681EXT(ARB_shader_clock),682EXT(ARB_shader_draw_parameters),683EXT(ARB_shader_group_vote),684EXT(ARB_shader_image_load_store),685EXT(ARB_shader_image_size),686EXT(ARB_shader_precision),687EXT(ARB_shader_stencil_export),688EXT(ARB_shader_storage_buffer_object),689EXT(ARB_shader_subroutine),690EXT(ARB_shader_texture_image_samples),691EXT(ARB_shader_texture_lod),692EXT(ARB_shader_viewport_layer_array),693EXT(ARB_shading_language_420pack),694EXT(ARB_shading_language_include),695EXT(ARB_shading_language_packing),696EXT(ARB_tessellation_shader),697EXT(ARB_texture_cube_map_array),698EXT(ARB_texture_gather),699EXT(ARB_texture_multisample),700EXT(ARB_texture_query_levels),701EXT(ARB_texture_query_lod),702EXT(ARB_texture_rectangle),703EXT(ARB_uniform_buffer_object),704EXT(ARB_vertex_attrib_64bit),705EXT(ARB_viewport_array),706707/* KHR extensions go here, sorted alphabetically.708*/709EXT_AEP(KHR_blend_equation_advanced),710711/* OES extensions go here, sorted alphabetically.712*/713EXT(OES_EGL_image_external),714EXT(OES_EGL_image_external_essl3),715EXT(OES_geometry_point_size),716EXT(OES_geometry_shader),717EXT(OES_gpu_shader5),718EXT(OES_primitive_bounding_box),719EXT_AEP(OES_sample_variables),720EXT_AEP(OES_shader_image_atomic),721EXT(OES_shader_io_blocks),722EXT_AEP(OES_shader_multisample_interpolation),723EXT(OES_standard_derivatives),724EXT(OES_tessellation_point_size),725EXT(OES_tessellation_shader),726EXT(OES_texture_3D),727EXT(OES_texture_buffer),728EXT(OES_texture_cube_map_array),729EXT_AEP(OES_texture_storage_multisample_2d_array),730EXT(OES_viewport_array),731732/* All other extensions go here, sorted alphabetically.733*/734EXT(AMD_conservative_depth),735EXT(AMD_gpu_shader_int64),736EXT(AMD_shader_stencil_export),737EXT(AMD_shader_trinary_minmax),738EXT(AMD_texture_texture4),739EXT(AMD_vertex_shader_layer),740EXT(AMD_vertex_shader_viewport_index),741EXT(ANDROID_extension_pack_es31a),742EXT(EXT_blend_func_extended),743EXT(EXT_demote_to_helper_invocation),744EXT(EXT_frag_depth),745EXT(EXT_draw_buffers),746EXT(EXT_draw_instanced),747EXT(EXT_clip_cull_distance),748EXT(EXT_geometry_point_size),749EXT_AEP(EXT_geometry_shader),750EXT(EXT_gpu_shader4),751EXT_AEP(EXT_gpu_shader5),752EXT_AEP(EXT_primitive_bounding_box),753EXT(EXT_separate_shader_objects),754EXT(EXT_shader_framebuffer_fetch),755EXT(EXT_shader_framebuffer_fetch_non_coherent),756EXT(EXT_shader_group_vote),757EXT(EXT_shader_image_load_formatted),758EXT(EXT_shader_image_load_store),759EXT(EXT_shader_implicit_conversions),760EXT(EXT_shader_integer_mix),761EXT_AEP(EXT_shader_io_blocks),762EXT(EXT_shader_samples_identical),763EXT(EXT_tessellation_point_size),764EXT_AEP(EXT_tessellation_shader),765EXT(EXT_texture_array),766EXT_AEP(EXT_texture_buffer),767EXT_AEP(EXT_texture_cube_map_array),768EXT(EXT_texture_query_lod),769EXT(EXT_texture_shadow_lod),770EXT(INTEL_conservative_rasterization),771EXT(INTEL_shader_atomic_float_minmax),772EXT(INTEL_shader_integer_functions2),773EXT(MESA_shader_integer_functions),774EXT(NV_compute_shader_derivatives),775EXT(NV_fragment_shader_interlock),776EXT(NV_image_formats),777EXT(NV_shader_atomic_float),778EXT(NV_shader_atomic_int64),779EXT(NV_viewport_array2),780};781782#undef EXT783784785/**786* Determine whether a given extension is compatible with the target,787* API, and extension information in the current parser state.788*/789bool _mesa_glsl_extension::compatible_with_state(790const _mesa_glsl_parse_state *state, gl_api api, uint8_t gl_version) const791{792return this->available_pred(state->ctx, api, gl_version);793}794795/**796* Set the appropriate flags in the parser state to establish the797* given behavior for this extension.798*/799void _mesa_glsl_extension::set_flags(_mesa_glsl_parse_state *state,800ext_behavior behavior) const801{802/* Note: the ->* operator indexes into state by the803* offsets this->enable_flag and this->warn_flag. See804* _mesa_glsl_extension::supported_flag for more info.805*/806state->*(this->enable_flag) = (behavior != extension_disable);807state->*(this->warn_flag) = (behavior == extension_warn);808}809810/**811* Find an extension by name in _mesa_glsl_supported_extensions. If812* the name is not found, return NULL.813*/814static const _mesa_glsl_extension *find_extension(const char *name)815{816for (unsigned i = 0; i < ARRAY_SIZE(_mesa_glsl_supported_extensions); ++i) {817if (strcmp(name, _mesa_glsl_supported_extensions[i].name) == 0) {818return &_mesa_glsl_supported_extensions[i];819}820}821return NULL;822}823824bool825_mesa_glsl_process_extension(const char *name, YYLTYPE *name_locp,826const char *behavior_string, YYLTYPE *behavior_locp,827_mesa_glsl_parse_state *state)828{829uint8_t gl_version = state->ctx->Extensions.Version;830gl_api api = state->ctx->API;831ext_behavior behavior;832if (strcmp(behavior_string, "warn") == 0) {833behavior = extension_warn;834} else if (strcmp(behavior_string, "require") == 0) {835behavior = extension_require;836} else if (strcmp(behavior_string, "enable") == 0) {837behavior = extension_enable;838} else if (strcmp(behavior_string, "disable") == 0) {839behavior = extension_disable;840} else {841_mesa_glsl_error(behavior_locp, state,842"unknown extension behavior `%s'",843behavior_string);844return false;845}846847/* If we're in a desktop context but with an ES shader, use an ES API enum848* to verify extension availability.849*/850if (state->es_shader && api != API_OPENGLES2)851api = API_OPENGLES2;852/* Use the language-version derived GL version to extension checks, unless853* we're using meta, which sets the version to the max.854*/855if (gl_version != 0xff)856gl_version = state->gl_version;857858if (strcmp(name, "all") == 0) {859if ((behavior == extension_enable) || (behavior == extension_require)) {860_mesa_glsl_error(name_locp, state, "cannot %s all extensions",861(behavior == extension_enable)862? "enable" : "require");863return false;864} else {865for (unsigned i = 0;866i < ARRAY_SIZE(_mesa_glsl_supported_extensions); ++i) {867const _mesa_glsl_extension *extension868= &_mesa_glsl_supported_extensions[i];869if (extension->compatible_with_state(state, api, gl_version)) {870_mesa_glsl_supported_extensions[i].set_flags(state, behavior);871}872}873}874} else {875const _mesa_glsl_extension *extension = find_extension(name);876if (extension && extension->compatible_with_state(state, api, gl_version)) {877extension->set_flags(state, behavior);878if (extension->available_pred == has_ANDROID_extension_pack_es31a) {879for (unsigned i = 0;880i < ARRAY_SIZE(_mesa_glsl_supported_extensions); ++i) {881const _mesa_glsl_extension *extension =882&_mesa_glsl_supported_extensions[i];883884if (!extension->aep)885continue;886/* AEP should not be enabled if all of the sub-extensions can't887* also be enabled. This is not the proper layer to do such888* error-checking though.889*/890assert(extension->compatible_with_state(state, api, gl_version));891extension->set_flags(state, behavior);892}893}894} else {895static const char fmt[] = "extension `%s' unsupported in %s shader";896897if (behavior == extension_require) {898_mesa_glsl_error(name_locp, state, fmt,899name, _mesa_shader_stage_to_string(state->stage));900return false;901} else {902_mesa_glsl_warning(name_locp, state, fmt,903name, _mesa_shader_stage_to_string(state->stage));904}905}906}907908return true;909}910911912/**913* Recurses through <type> and <expr> if <expr> is an aggregate initializer914* and sets <expr>'s <constructor_type> field to <type>. Gives later functions915* (process_array_constructor, et al) sufficient information to do type916* checking.917*918* Operates on assignments involving an aggregate initializer. E.g.,919*920* vec4 pos = {1.0, -1.0, 0.0, 1.0};921*922* or more ridiculously,923*924* struct S {925* vec4 v[2];926* };927*928* struct {929* S a[2], b;930* int c;931* } aggregate = {932* {933* {934* {935* {1.0, 2.0, 3.0, 4.0}, // a[0].v[0]936* {5.0, 6.0, 7.0, 8.0} // a[0].v[1]937* } // a[0].v938* }, // a[0]939* {940* {941* {1.0, 2.0, 3.0, 4.0}, // a[1].v[0]942* {5.0, 6.0, 7.0, 8.0} // a[1].v[1]943* } // a[1].v944* } // a[1]945* }, // a946* {947* {948* {1.0, 2.0, 3.0, 4.0}, // b.v[0]949* {5.0, 6.0, 7.0, 8.0} // b.v[1]950* } // b.v951* }, // b952* 4 // c953* };954*955* This pass is necessary because the right-hand side of <type> e = { ... }956* doesn't contain sufficient information to determine if the types match.957*/958void959_mesa_ast_set_aggregate_type(const glsl_type *type,960ast_expression *expr)961{962ast_aggregate_initializer *ai = (ast_aggregate_initializer *)expr;963ai->constructor_type = type;964965/* If the aggregate is an array, recursively set its elements' types. */966if (type->is_array()) {967/* Each array element has the type type->fields.array.968*969* E.g., if <type> if struct S[2] we want to set each element's type to970* struct S.971*/972for (exec_node *expr_node = ai->expressions.get_head_raw();973!expr_node->is_tail_sentinel();974expr_node = expr_node->next) {975ast_expression *expr = exec_node_data(ast_expression, expr_node,976link);977978if (expr->oper == ast_aggregate)979_mesa_ast_set_aggregate_type(type->fields.array, expr);980}981982/* If the aggregate is a struct, recursively set its fields' types. */983} else if (type->is_struct()) {984exec_node *expr_node = ai->expressions.get_head_raw();985986/* Iterate through the struct's fields. */987for (unsigned i = 0; !expr_node->is_tail_sentinel() && i < type->length;988i++, expr_node = expr_node->next) {989ast_expression *expr = exec_node_data(ast_expression, expr_node,990link);991992if (expr->oper == ast_aggregate) {993_mesa_ast_set_aggregate_type(type->fields.structure[i].type, expr);994}995}996/* If the aggregate is a matrix, set its columns' types. */997} else if (type->is_matrix()) {998for (exec_node *expr_node = ai->expressions.get_head_raw();999!expr_node->is_tail_sentinel();1000expr_node = expr_node->next) {1001ast_expression *expr = exec_node_data(ast_expression, expr_node,1002link);10031004if (expr->oper == ast_aggregate)1005_mesa_ast_set_aggregate_type(type->column_type(), expr);1006}1007}1008}10091010void1011_mesa_ast_process_interface_block(YYLTYPE *locp,1012_mesa_glsl_parse_state *state,1013ast_interface_block *const block,1014const struct ast_type_qualifier &q)1015{1016if (q.flags.q.buffer) {1017if (!state->has_shader_storage_buffer_objects()) {1018_mesa_glsl_error(locp, state,1019"#version 430 / GL_ARB_shader_storage_buffer_object "1020"required for defining shader storage blocks");1021} else if (state->ARB_shader_storage_buffer_object_warn) {1022_mesa_glsl_warning(locp, state,1023"#version 430 / GL_ARB_shader_storage_buffer_object "1024"required for defining shader storage blocks");1025}1026} else if (q.flags.q.uniform) {1027if (!state->has_uniform_buffer_objects()) {1028_mesa_glsl_error(locp, state,1029"#version 140 / GL_ARB_uniform_buffer_object "1030"required for defining uniform blocks");1031} else if (state->ARB_uniform_buffer_object_warn) {1032_mesa_glsl_warning(locp, state,1033"#version 140 / GL_ARB_uniform_buffer_object "1034"required for defining uniform blocks");1035}1036} else {1037if (!state->has_shader_io_blocks()) {1038if (state->es_shader) {1039_mesa_glsl_error(locp, state,1040"GL_OES_shader_io_blocks or #version 320 "1041"required for using interface blocks");1042} else {1043_mesa_glsl_error(locp, state,1044"#version 150 required for using "1045"interface blocks");1046}1047}1048}10491050/* From the GLSL 1.50.11 spec, section 4.3.7 ("Interface Blocks"):1051* "It is illegal to have an input block in a vertex shader1052* or an output block in a fragment shader"1053*/1054if ((state->stage == MESA_SHADER_VERTEX) && q.flags.q.in) {1055_mesa_glsl_error(locp, state,1056"`in' interface block is not allowed for "1057"a vertex shader");1058} else if ((state->stage == MESA_SHADER_FRAGMENT) && q.flags.q.out) {1059_mesa_glsl_error(locp, state,1060"`out' interface block is not allowed for "1061"a fragment shader");1062}10631064/* Since block arrays require names, and both features are added in1065* the same language versions, we don't have to explicitly1066* version-check both things.1067*/1068if (block->instance_name != NULL) {1069state->check_version(150, 300, locp, "interface blocks with "1070"an instance name are not allowed");1071}10721073ast_type_qualifier::bitset_t interface_type_mask;1074struct ast_type_qualifier temp_type_qualifier;10751076/* Get a bitmask containing only the in/out/uniform/buffer1077* flags, allowing us to ignore other irrelevant flags like1078* interpolation qualifiers.1079*/1080temp_type_qualifier.flags.i = 0;1081temp_type_qualifier.flags.q.uniform = true;1082temp_type_qualifier.flags.q.in = true;1083temp_type_qualifier.flags.q.out = true;1084temp_type_qualifier.flags.q.buffer = true;1085temp_type_qualifier.flags.q.patch = true;1086interface_type_mask = temp_type_qualifier.flags.i;10871088/* Get the block's interface qualifier. The interface_qualifier1089* production rule guarantees that only one bit will be set (and1090* it will be in/out/uniform).1091*/1092ast_type_qualifier::bitset_t block_interface_qualifier = q.flags.i;10931094block->default_layout.flags.i |= block_interface_qualifier;10951096if (state->stage == MESA_SHADER_GEOMETRY &&1097state->has_explicit_attrib_stream() &&1098block->default_layout.flags.q.out) {1099/* Assign global layout's stream value. */1100block->default_layout.flags.q.stream = 1;1101block->default_layout.flags.q.explicit_stream = 0;1102block->default_layout.stream = state->out_qualifier->stream;1103}11041105if (state->has_enhanced_layouts() && block->default_layout.flags.q.out) {1106/* Assign global layout's xfb_buffer value. */1107block->default_layout.flags.q.xfb_buffer = 1;1108block->default_layout.flags.q.explicit_xfb_buffer = 0;1109block->default_layout.xfb_buffer = state->out_qualifier->xfb_buffer;1110}11111112foreach_list_typed (ast_declarator_list, member, link, &block->declarations) {1113ast_type_qualifier& qualifier = member->type->qualifier;1114if ((qualifier.flags.i & interface_type_mask) == 0) {1115/* GLSLangSpec.1.50.11, 4.3.7 (Interface Blocks):1116* "If no optional qualifier is used in a member declaration, the1117* qualifier of the variable is just in, out, or uniform as declared1118* by interface-qualifier."1119*/1120qualifier.flags.i |= block_interface_qualifier;1121} else if ((qualifier.flags.i & interface_type_mask) !=1122block_interface_qualifier) {1123/* GLSLangSpec.1.50.11, 4.3.7 (Interface Blocks):1124* "If optional qualifiers are used, they can include interpolation1125* and storage qualifiers and they must declare an input, output,1126* or uniform variable consistent with the interface qualifier of1127* the block."1128*/1129_mesa_glsl_error(locp, state,1130"uniform/in/out qualifier on "1131"interface block member does not match "1132"the interface block");1133}11341135if (!(q.flags.q.in || q.flags.q.out) && qualifier.flags.q.invariant)1136_mesa_glsl_error(locp, state,1137"invariant qualifiers can be used only "1138"in interface block members for shader "1139"inputs or outputs");1140}1141}11421143static void1144_mesa_ast_type_qualifier_print(const struct ast_type_qualifier *q)1145{1146if (q->is_subroutine_decl())1147printf("subroutine ");11481149if (q->subroutine_list) {1150printf("subroutine (");1151q->subroutine_list->print();1152printf(")");1153}11541155if (q->flags.q.constant)1156printf("const ");11571158if (q->flags.q.invariant)1159printf("invariant ");11601161if (q->flags.q.attribute)1162printf("attribute ");11631164if (q->flags.q.varying)1165printf("varying ");11661167if (q->flags.q.in && q->flags.q.out)1168printf("inout ");1169else {1170if (q->flags.q.in)1171printf("in ");11721173if (q->flags.q.out)1174printf("out ");1175}11761177if (q->flags.q.centroid)1178printf("centroid ");1179if (q->flags.q.sample)1180printf("sample ");1181if (q->flags.q.patch)1182printf("patch ");1183if (q->flags.q.uniform)1184printf("uniform ");1185if (q->flags.q.buffer)1186printf("buffer ");1187if (q->flags.q.smooth)1188printf("smooth ");1189if (q->flags.q.flat)1190printf("flat ");1191if (q->flags.q.noperspective)1192printf("noperspective ");1193}119411951196void1197ast_node::print(void) const1198{1199printf("unhandled node ");1200}120112021203ast_node::ast_node(void)1204{1205this->location.path = NULL;1206this->location.source = 0;1207this->location.first_line = 0;1208this->location.first_column = 0;1209this->location.last_line = 0;1210this->location.last_column = 0;1211}121212131214static void1215ast_opt_array_dimensions_print(const ast_array_specifier *array_specifier)1216{1217if (array_specifier)1218array_specifier->print();1219}122012211222void1223ast_compound_statement::print(void) const1224{1225printf("{\n");12261227foreach_list_typed(ast_node, ast, link, &this->statements) {1228ast->print();1229}12301231printf("}\n");1232}123312341235ast_compound_statement::ast_compound_statement(int new_scope,1236ast_node *statements)1237{1238this->new_scope = new_scope;12391240if (statements != NULL) {1241this->statements.push_degenerate_list_at_head(&statements->link);1242}1243}124412451246void1247ast_expression::print(void) const1248{1249switch (oper) {1250case ast_assign:1251case ast_mul_assign:1252case ast_div_assign:1253case ast_mod_assign:1254case ast_add_assign:1255case ast_sub_assign:1256case ast_ls_assign:1257case ast_rs_assign:1258case ast_and_assign:1259case ast_xor_assign:1260case ast_or_assign:1261subexpressions[0]->print();1262printf("%s ", operator_string(oper));1263subexpressions[1]->print();1264break;12651266case ast_field_selection:1267subexpressions[0]->print();1268printf(". %s ", primary_expression.identifier);1269break;12701271case ast_plus:1272case ast_neg:1273case ast_bit_not:1274case ast_logic_not:1275case ast_pre_inc:1276case ast_pre_dec:1277printf("%s ", operator_string(oper));1278subexpressions[0]->print();1279break;12801281case ast_post_inc:1282case ast_post_dec:1283subexpressions[0]->print();1284printf("%s ", operator_string(oper));1285break;12861287case ast_conditional:1288subexpressions[0]->print();1289printf("? ");1290subexpressions[1]->print();1291printf(": ");1292subexpressions[2]->print();1293break;12941295case ast_array_index:1296subexpressions[0]->print();1297printf("[ ");1298subexpressions[1]->print();1299printf("] ");1300break;13011302case ast_function_call: {1303subexpressions[0]->print();1304printf("( ");13051306foreach_list_typed (ast_node, ast, link, &this->expressions) {1307if (&ast->link != this->expressions.get_head())1308printf(", ");13091310ast->print();1311}13121313printf(") ");1314break;1315}13161317case ast_identifier:1318printf("%s ", primary_expression.identifier);1319break;13201321case ast_int_constant:1322printf("%d ", primary_expression.int_constant);1323break;13241325case ast_uint_constant:1326printf("%u ", primary_expression.uint_constant);1327break;13281329case ast_float_constant:1330printf("%f ", primary_expression.float_constant);1331break;13321333case ast_double_constant:1334printf("%f ", primary_expression.double_constant);1335break;13361337case ast_int64_constant:1338printf("%" PRId64 " ", primary_expression.int64_constant);1339break;13401341case ast_uint64_constant:1342printf("%" PRIu64 " ", primary_expression.uint64_constant);1343break;13441345case ast_bool_constant:1346printf("%s ",1347primary_expression.bool_constant1348? "true" : "false");1349break;13501351case ast_sequence: {1352printf("( ");1353foreach_list_typed (ast_node, ast, link, & this->expressions) {1354if (&ast->link != this->expressions.get_head())1355printf(", ");13561357ast->print();1358}1359printf(") ");1360break;1361}13621363case ast_aggregate: {1364printf("{ ");1365foreach_list_typed (ast_node, ast, link, & this->expressions) {1366if (&ast->link != this->expressions.get_head())1367printf(", ");13681369ast->print();1370}1371printf("} ");1372break;1373}13741375default:1376assert(0);1377break;1378}1379}13801381ast_expression::ast_expression(int oper,1382ast_expression *ex0,1383ast_expression *ex1,1384ast_expression *ex2) :1385primary_expression()1386{1387this->oper = ast_operators(oper);1388this->subexpressions[0] = ex0;1389this->subexpressions[1] = ex1;1390this->subexpressions[2] = ex2;1391this->non_lvalue_description = NULL;1392this->is_lhs = false;1393}139413951396void1397ast_expression_statement::print(void) const1398{1399if (expression)1400expression->print();14011402printf("; ");1403}140414051406ast_expression_statement::ast_expression_statement(ast_expression *ex) :1407expression(ex)1408{1409/* empty */1410}141114121413void1414ast_function::print(void) const1415{1416return_type->print();1417printf(" %s (", identifier);14181419foreach_list_typed(ast_node, ast, link, & this->parameters) {1420ast->print();1421}14221423printf(")");1424}142514261427ast_function::ast_function(void)1428: return_type(NULL), identifier(NULL), is_definition(false),1429signature(NULL)1430{1431/* empty */1432}143314341435void1436ast_fully_specified_type::print(void) const1437{1438_mesa_ast_type_qualifier_print(& qualifier);1439specifier->print();1440}144114421443void1444ast_parameter_declarator::print(void) const1445{1446type->print();1447if (identifier)1448printf("%s ", identifier);1449ast_opt_array_dimensions_print(array_specifier);1450}145114521453void1454ast_function_definition::print(void) const1455{1456prototype->print();1457body->print();1458}145914601461void1462ast_declaration::print(void) const1463{1464printf("%s ", identifier);1465ast_opt_array_dimensions_print(array_specifier);14661467if (initializer) {1468printf("= ");1469initializer->print();1470}1471}147214731474ast_declaration::ast_declaration(const char *identifier,1475ast_array_specifier *array_specifier,1476ast_expression *initializer)1477{1478this->identifier = identifier;1479this->array_specifier = array_specifier;1480this->initializer = initializer;1481}148214831484void1485ast_declarator_list::print(void) const1486{1487assert(type || invariant);14881489if (type)1490type->print();1491else if (invariant)1492printf("invariant ");1493else1494printf("precise ");14951496foreach_list_typed (ast_node, ast, link, & this->declarations) {1497if (&ast->link != this->declarations.get_head())1498printf(", ");14991500ast->print();1501}15021503printf("; ");1504}150515061507ast_declarator_list::ast_declarator_list(ast_fully_specified_type *type)1508{1509this->type = type;1510this->invariant = false;1511this->precise = false;1512}15131514void1515ast_jump_statement::print(void) const1516{1517switch (mode) {1518case ast_continue:1519printf("continue; ");1520break;1521case ast_break:1522printf("break; ");1523break;1524case ast_return:1525printf("return ");1526if (opt_return_value)1527opt_return_value->print();15281529printf("; ");1530break;1531case ast_discard:1532printf("discard; ");1533break;1534}1535}153615371538ast_jump_statement::ast_jump_statement(int mode, ast_expression *return_value)1539: opt_return_value(NULL)1540{1541this->mode = ast_jump_modes(mode);15421543if (mode == ast_return)1544opt_return_value = return_value;1545}154615471548void1549ast_demote_statement::print(void) const1550{1551printf("demote; ");1552}155315541555void1556ast_selection_statement::print(void) const1557{1558printf("if ( ");1559condition->print();1560printf(") ");15611562then_statement->print();15631564if (else_statement) {1565printf("else ");1566else_statement->print();1567}1568}156915701571ast_selection_statement::ast_selection_statement(ast_expression *condition,1572ast_node *then_statement,1573ast_node *else_statement)1574{1575this->condition = condition;1576this->then_statement = then_statement;1577this->else_statement = else_statement;1578}157915801581void1582ast_switch_statement::print(void) const1583{1584printf("switch ( ");1585test_expression->print();1586printf(") ");15871588body->print();1589}159015911592ast_switch_statement::ast_switch_statement(ast_expression *test_expression,1593ast_node *body)1594{1595this->test_expression = test_expression;1596this->body = body;1597this->test_val = NULL;1598}159916001601void1602ast_switch_body::print(void) const1603{1604printf("{\n");1605if (stmts != NULL) {1606stmts->print();1607}1608printf("}\n");1609}161016111612ast_switch_body::ast_switch_body(ast_case_statement_list *stmts)1613{1614this->stmts = stmts;1615}161616171618void ast_case_label::print(void) const1619{1620if (test_value != NULL) {1621printf("case ");1622test_value->print();1623printf(": ");1624} else {1625printf("default: ");1626}1627}162816291630ast_case_label::ast_case_label(ast_expression *test_value)1631{1632this->test_value = test_value;1633}163416351636void ast_case_label_list::print(void) const1637{1638foreach_list_typed(ast_node, ast, link, & this->labels) {1639ast->print();1640}1641printf("\n");1642}164316441645ast_case_label_list::ast_case_label_list(void)1646{1647}164816491650void ast_case_statement::print(void) const1651{1652labels->print();1653foreach_list_typed(ast_node, ast, link, & this->stmts) {1654ast->print();1655printf("\n");1656}1657}165816591660ast_case_statement::ast_case_statement(ast_case_label_list *labels)1661{1662this->labels = labels;1663}166416651666void ast_case_statement_list::print(void) const1667{1668foreach_list_typed(ast_node, ast, link, & this->cases) {1669ast->print();1670}1671}167216731674ast_case_statement_list::ast_case_statement_list(void)1675{1676}167716781679void1680ast_iteration_statement::print(void) const1681{1682switch (mode) {1683case ast_for:1684printf("for( ");1685if (init_statement)1686init_statement->print();1687printf("; ");16881689if (condition)1690condition->print();1691printf("; ");16921693if (rest_expression)1694rest_expression->print();1695printf(") ");16961697body->print();1698break;16991700case ast_while:1701printf("while ( ");1702if (condition)1703condition->print();1704printf(") ");1705body->print();1706break;17071708case ast_do_while:1709printf("do ");1710body->print();1711printf("while ( ");1712if (condition)1713condition->print();1714printf("); ");1715break;1716}1717}171817191720ast_iteration_statement::ast_iteration_statement(int mode,1721ast_node *init,1722ast_node *condition,1723ast_expression *rest_expression,1724ast_node *body)1725{1726this->mode = ast_iteration_modes(mode);1727this->init_statement = init;1728this->condition = condition;1729this->rest_expression = rest_expression;1730this->body = body;1731}173217331734void1735ast_struct_specifier::print(void) const1736{1737printf("struct %s { ", name);1738foreach_list_typed(ast_node, ast, link, &this->declarations) {1739ast->print();1740}1741printf("} ");1742}174317441745ast_struct_specifier::ast_struct_specifier(const char *identifier,1746ast_declarator_list *declarator_list)1747: name(identifier), layout(NULL), declarations(), is_declaration(true),1748type(NULL)1749{1750this->declarations.push_degenerate_list_at_head(&declarator_list->link);1751}17521753void ast_subroutine_list::print(void) const1754{1755foreach_list_typed (ast_node, ast, link, & this->declarations) {1756if (&ast->link != this->declarations.get_head())1757printf(", ");1758ast->print();1759}1760}17611762static void1763set_shader_inout_layout(struct gl_shader *shader,1764struct _mesa_glsl_parse_state *state)1765{1766/* Should have been prevented by the parser. */1767if (shader->Stage != MESA_SHADER_GEOMETRY &&1768shader->Stage != MESA_SHADER_TESS_EVAL &&1769shader->Stage != MESA_SHADER_COMPUTE) {1770assert(!state->in_qualifier->flags.i);1771}17721773if (shader->Stage != MESA_SHADER_COMPUTE) {1774/* Should have been prevented by the parser. */1775assert(!state->cs_input_local_size_specified);1776assert(!state->cs_input_local_size_variable_specified);1777assert(state->cs_derivative_group == DERIVATIVE_GROUP_NONE);1778}17791780if (shader->Stage != MESA_SHADER_FRAGMENT) {1781/* Should have been prevented by the parser. */1782assert(!state->fs_uses_gl_fragcoord);1783assert(!state->fs_redeclares_gl_fragcoord);1784assert(!state->fs_pixel_center_integer);1785assert(!state->fs_origin_upper_left);1786assert(!state->fs_early_fragment_tests);1787assert(!state->fs_inner_coverage);1788assert(!state->fs_post_depth_coverage);1789assert(!state->fs_pixel_interlock_ordered);1790assert(!state->fs_pixel_interlock_unordered);1791assert(!state->fs_sample_interlock_ordered);1792assert(!state->fs_sample_interlock_unordered);1793}17941795for (unsigned i = 0; i < MAX_FEEDBACK_BUFFERS; i++) {1796if (state->out_qualifier->out_xfb_stride[i]) {1797unsigned xfb_stride;1798if (state->out_qualifier->out_xfb_stride[i]->1799process_qualifier_constant(state, "xfb_stride", &xfb_stride,1800true)) {1801shader->TransformFeedbackBufferStride[i] = xfb_stride;1802}1803}1804}18051806switch (shader->Stage) {1807case MESA_SHADER_TESS_CTRL:1808shader->info.TessCtrl.VerticesOut = 0;1809if (state->tcs_output_vertices_specified) {1810unsigned vertices;1811if (state->out_qualifier->vertices->1812process_qualifier_constant(state, "vertices", &vertices,1813false)) {18141815YYLTYPE loc = state->out_qualifier->vertices->get_location();1816if (vertices > state->Const.MaxPatchVertices) {1817_mesa_glsl_error(&loc, state, "vertices (%d) exceeds "1818"GL_MAX_PATCH_VERTICES", vertices);1819}1820shader->info.TessCtrl.VerticesOut = vertices;1821}1822}1823break;1824case MESA_SHADER_TESS_EVAL:1825shader->info.TessEval.PrimitiveMode = PRIM_UNKNOWN;1826if (state->in_qualifier->flags.q.prim_type)1827shader->info.TessEval.PrimitiveMode = state->in_qualifier->prim_type;18281829shader->info.TessEval.Spacing = TESS_SPACING_UNSPECIFIED;1830if (state->in_qualifier->flags.q.vertex_spacing)1831shader->info.TessEval.Spacing = state->in_qualifier->vertex_spacing;18321833shader->info.TessEval.VertexOrder = 0;1834if (state->in_qualifier->flags.q.ordering)1835shader->info.TessEval.VertexOrder = state->in_qualifier->ordering;18361837shader->info.TessEval.PointMode = -1;1838if (state->in_qualifier->flags.q.point_mode)1839shader->info.TessEval.PointMode = state->in_qualifier->point_mode;1840break;1841case MESA_SHADER_GEOMETRY:1842shader->info.Geom.VerticesOut = -1;1843if (state->out_qualifier->flags.q.max_vertices) {1844unsigned qual_max_vertices;1845if (state->out_qualifier->max_vertices->1846process_qualifier_constant(state, "max_vertices",1847&qual_max_vertices, true)) {18481849if (qual_max_vertices > state->Const.MaxGeometryOutputVertices) {1850YYLTYPE loc = state->out_qualifier->max_vertices->get_location();1851_mesa_glsl_error(&loc, state,1852"maximum output vertices (%d) exceeds "1853"GL_MAX_GEOMETRY_OUTPUT_VERTICES",1854qual_max_vertices);1855}1856shader->info.Geom.VerticesOut = qual_max_vertices;1857}1858}18591860if (state->gs_input_prim_type_specified) {1861shader->info.Geom.InputType = state->in_qualifier->prim_type;1862} else {1863shader->info.Geom.InputType = PRIM_UNKNOWN;1864}18651866if (state->out_qualifier->flags.q.prim_type) {1867shader->info.Geom.OutputType = state->out_qualifier->prim_type;1868} else {1869shader->info.Geom.OutputType = PRIM_UNKNOWN;1870}18711872shader->info.Geom.Invocations = 0;1873if (state->in_qualifier->flags.q.invocations) {1874unsigned invocations;1875if (state->in_qualifier->invocations->1876process_qualifier_constant(state, "invocations",1877&invocations, false)) {18781879YYLTYPE loc = state->in_qualifier->invocations->get_location();1880if (invocations > state->Const.MaxGeometryShaderInvocations) {1881_mesa_glsl_error(&loc, state,1882"invocations (%d) exceeds "1883"GL_MAX_GEOMETRY_SHADER_INVOCATIONS",1884invocations);1885}1886shader->info.Geom.Invocations = invocations;1887}1888}1889break;18901891case MESA_SHADER_COMPUTE:1892if (state->cs_input_local_size_specified) {1893for (int i = 0; i < 3; i++)1894shader->info.Comp.LocalSize[i] = state->cs_input_local_size[i];1895} else {1896for (int i = 0; i < 3; i++)1897shader->info.Comp.LocalSize[i] = 0;1898}18991900shader->info.Comp.LocalSizeVariable =1901state->cs_input_local_size_variable_specified;19021903shader->info.Comp.DerivativeGroup = state->cs_derivative_group;19041905if (state->NV_compute_shader_derivatives_enable) {1906/* We allow multiple cs_input_layout nodes, but do not store them in1907* a convenient place, so for now live with an empty location error.1908*/1909YYLTYPE loc = {0};1910if (shader->info.Comp.DerivativeGroup == DERIVATIVE_GROUP_QUADS) {1911if (shader->info.Comp.LocalSize[0] % 2 != 0) {1912_mesa_glsl_error(&loc, state, "derivative_group_quadsNV must be used with a "1913"local group size whose first dimension "1914"is a multiple of 2\n");1915}1916if (shader->info.Comp.LocalSize[1] % 2 != 0) {1917_mesa_glsl_error(&loc, state, "derivative_group_quadsNV must be used with a "1918"local group size whose second dimension "1919"is a multiple of 2\n");1920}1921} else if (shader->info.Comp.DerivativeGroup == DERIVATIVE_GROUP_LINEAR) {1922if ((shader->info.Comp.LocalSize[0] *1923shader->info.Comp.LocalSize[1] *1924shader->info.Comp.LocalSize[2]) % 4 != 0) {1925_mesa_glsl_error(&loc, state, "derivative_group_linearNV must be used with a "1926"local group size whose total number of invocations "1927"is a multiple of 4\n");1928}1929}1930}19311932break;19331934case MESA_SHADER_FRAGMENT:1935shader->redeclares_gl_fragcoord = state->fs_redeclares_gl_fragcoord;1936shader->uses_gl_fragcoord = state->fs_uses_gl_fragcoord;1937shader->pixel_center_integer = state->fs_pixel_center_integer;1938shader->origin_upper_left = state->fs_origin_upper_left;1939shader->ARB_fragment_coord_conventions_enable =1940state->ARB_fragment_coord_conventions_enable;1941shader->EarlyFragmentTests = state->fs_early_fragment_tests;1942shader->InnerCoverage = state->fs_inner_coverage;1943shader->PostDepthCoverage = state->fs_post_depth_coverage;1944shader->PixelInterlockOrdered = state->fs_pixel_interlock_ordered;1945shader->PixelInterlockUnordered = state->fs_pixel_interlock_unordered;1946shader->SampleInterlockOrdered = state->fs_sample_interlock_ordered;1947shader->SampleInterlockUnordered = state->fs_sample_interlock_unordered;1948shader->BlendSupport = state->fs_blend_support;1949break;19501951default:1952/* Nothing to do. */1953break;1954}19551956shader->bindless_sampler = state->bindless_sampler_specified;1957shader->bindless_image = state->bindless_image_specified;1958shader->bound_sampler = state->bound_sampler_specified;1959shader->bound_image = state->bound_image_specified;1960shader->redeclares_gl_layer = state->redeclares_gl_layer;1961shader->layer_viewport_relative = state->layer_viewport_relative;1962}19631964/* src can be NULL if only the symbols found in the exec_list should be1965* copied1966*/1967void1968_mesa_glsl_copy_symbols_from_table(struct exec_list *shader_ir,1969struct glsl_symbol_table *src,1970struct glsl_symbol_table *dest)1971{1972foreach_in_list (ir_instruction, ir, shader_ir) {1973switch (ir->ir_type) {1974case ir_type_function:1975dest->add_function((ir_function *) ir);1976break;1977case ir_type_variable: {1978ir_variable *const var = (ir_variable *) ir;19791980if (var->data.mode != ir_var_temporary)1981dest->add_variable(var);1982break;1983}1984default:1985break;1986}1987}19881989if (src != NULL) {1990/* Explicitly copy the gl_PerVertex interface definitions because these1991* are needed to check they are the same during the interstage link.1992* They can’t necessarily be found via the exec_list because the members1993* might not be referenced. The GL spec still requires that they match1994* in that case.1995*/1996const glsl_type *iface =1997src->get_interface("gl_PerVertex", ir_var_shader_in);1998if (iface)1999dest->add_interface(iface->name, iface, ir_var_shader_in);20002001iface = src->get_interface("gl_PerVertex", ir_var_shader_out);2002if (iface)2003dest->add_interface(iface->name, iface, ir_var_shader_out);2004}2005}20062007extern "C" {20082009static void2010assign_subroutine_indexes(struct _mesa_glsl_parse_state *state)2011{2012int j, k;2013int index = 0;20142015for (j = 0; j < state->num_subroutines; j++) {2016while (state->subroutines[j]->subroutine_index == -1) {2017for (k = 0; k < state->num_subroutines; k++) {2018if (state->subroutines[k]->subroutine_index == index)2019break;2020else if (k == state->num_subroutines - 1) {2021state->subroutines[j]->subroutine_index = index;2022}2023}2024index++;2025}2026}2027}20282029static void2030add_builtin_defines(struct _mesa_glsl_parse_state *state,2031void (*add_builtin_define)(struct glcpp_parser *, const char *, int),2032struct glcpp_parser *data,2033unsigned version,2034bool es)2035{2036unsigned gl_version = state->ctx->Extensions.Version;2037gl_api api = state->ctx->API;20382039if (gl_version != 0xff) {2040unsigned i;2041for (i = 0; i < state->num_supported_versions; i++) {2042if (state->supported_versions[i].ver == version &&2043state->supported_versions[i].es == es) {2044gl_version = state->supported_versions[i].gl_ver;2045break;2046}2047}20482049if (i == state->num_supported_versions)2050return;2051}20522053if (es)2054api = API_OPENGLES2;20552056for (unsigned i = 0;2057i < ARRAY_SIZE(_mesa_glsl_supported_extensions); ++i) {2058const _mesa_glsl_extension *extension2059= &_mesa_glsl_supported_extensions[i];2060if (extension->compatible_with_state(state, api, gl_version)) {2061add_builtin_define(data, extension->name, 1);2062}2063}2064}20652066/* Implements parsing checks that we can't do during parsing */2067static void2068do_late_parsing_checks(struct _mesa_glsl_parse_state *state)2069{2070if (state->stage == MESA_SHADER_COMPUTE && !state->has_compute_shader()) {2071YYLTYPE loc;2072memset(&loc, 0, sizeof(loc));2073_mesa_glsl_error(&loc, state, "Compute shaders require "2074"GLSL 4.30 or GLSL ES 3.10");2075}2076}20772078static void2079opt_shader_and_create_symbol_table(struct gl_context *ctx,2080struct glsl_symbol_table *source_symbols,2081struct gl_shader *shader)2082{2083assert(shader->CompileStatus != COMPILE_FAILURE &&2084!shader->ir->is_empty());20852086struct gl_shader_compiler_options *options =2087&ctx->Const.ShaderCompilerOptions[shader->Stage];20882089/* Do some optimization at compile time to reduce shader IR size2090* and reduce later work if the same shader is linked multiple times2091*/2092if (ctx->Const.GLSLOptimizeConservatively) {2093/* Run it just once. */2094do_common_optimization(shader->ir, false, false, options,2095ctx->Const.NativeIntegers);2096} else {2097/* Repeat it until it stops making changes. */2098while (do_common_optimization(shader->ir, false, false, options,2099ctx->Const.NativeIntegers))2100;2101}21022103validate_ir_tree(shader->ir);21042105enum ir_variable_mode other;2106switch (shader->Stage) {2107case MESA_SHADER_VERTEX:2108other = ir_var_shader_in;2109break;2110case MESA_SHADER_FRAGMENT:2111other = ir_var_shader_out;2112break;2113default:2114/* Something invalid to ensure optimize_dead_builtin_uniforms2115* doesn't remove anything other than uniforms or constants.2116*/2117other = ir_var_mode_count;2118break;2119}21202121optimize_dead_builtin_variables(shader->ir, other);21222123validate_ir_tree(shader->ir);21242125/* Retain any live IR, but trash the rest. */2126reparent_ir(shader->ir, shader->ir);21272128/* Destroy the symbol table. Create a new symbol table that contains only2129* the variables and functions that still exist in the IR. The symbol2130* table will be used later during linking.2131*2132* There must NOT be any freed objects still referenced by the symbol2133* table. That could cause the linker to dereference freed memory.2134*2135* We don't have to worry about types or interface-types here because those2136* are fly-weights that are looked up by glsl_type.2137*/2138_mesa_glsl_copy_symbols_from_table(shader->ir, source_symbols,2139shader->symbols);2140}21412142static bool2143can_skip_compile(struct gl_context *ctx, struct gl_shader *shader,2144const char *source, bool force_recompile,2145bool source_has_shader_include)2146{2147if (!force_recompile) {2148if (ctx->Cache) {2149char buf[41];2150disk_cache_compute_key(ctx->Cache, source, strlen(source),2151shader->sha1);2152if (disk_cache_has_key(ctx->Cache, shader->sha1)) {2153/* We've seen this shader before and know it compiles */2154if (ctx->_Shader->Flags & GLSL_CACHE_INFO) {2155_mesa_sha1_format(buf, shader->sha1);2156fprintf(stderr, "deferring compile of shader: %s\n", buf);2157}2158shader->CompileStatus = COMPILE_SKIPPED;21592160free((void *)shader->FallbackSource);21612162/* Copy pre-processed shader include to fallback source otherwise2163* we have no guarantee the shader include source tree has not2164* changed.2165*/2166shader->FallbackSource = source_has_shader_include ?2167strdup(source) : NULL;2168return true;2169}2170}2171} else {2172/* We should only ever end up here if a re-compile has been forced by a2173* shader cache miss. In which case we can skip the compile if its2174* already been done by a previous fallback or the initial compile call.2175*/2176if (shader->CompileStatus == COMPILE_SUCCESS)2177return true;2178}21792180return false;2181}21822183void2184_mesa_glsl_compile_shader(struct gl_context *ctx, struct gl_shader *shader,2185bool dump_ast, bool dump_hir, bool force_recompile)2186{2187const char *source = force_recompile && shader->FallbackSource ?2188shader->FallbackSource : shader->Source;21892190/* Note this will be true for shaders the have #include inside comments2191* however that should be rare enough not to worry about.2192*/2193bool source_has_shader_include =2194strstr(source, "#include") == NULL ? false : true;21952196/* If there was no shader include we can check the shader cache and skip2197* compilation before we run the preprocessor. We never skip compiling2198* shaders that use ARB_shading_language_include because we would need to2199* keep duplicate copies of the shader include source tree and paths.2200*/2201if (!source_has_shader_include &&2202can_skip_compile(ctx, shader, source, force_recompile, false))2203return;22042205struct _mesa_glsl_parse_state *state =2206new(shader) _mesa_glsl_parse_state(ctx, shader->Stage, shader);22072208if (ctx->Const.GenerateTemporaryNames)2209(void) p_atomic_cmpxchg(&ir_variable::temporaries_allocate_names,2210false, true);22112212if (!source_has_shader_include || !force_recompile) {2213state->error = glcpp_preprocess(state, &source, &state->info_log,2214add_builtin_defines, state, ctx);2215}22162217/* Now that we have run the preprocessor we can check the shader cache and2218* skip compilation if possible for those shaders that contained a shader2219* include.2220*/2221if (source_has_shader_include &&2222can_skip_compile(ctx, shader, source, force_recompile, true))2223return;22242225if (!state->error) {2226_mesa_glsl_lexer_ctor(state, source);2227_mesa_glsl_parse(state);2228_mesa_glsl_lexer_dtor(state);2229do_late_parsing_checks(state);2230}22312232if (dump_ast) {2233foreach_list_typed(ast_node, ast, link, &state->translation_unit) {2234ast->print();2235}2236printf("\n\n");2237}22382239ralloc_free(shader->ir);2240shader->ir = new(shader) exec_list;2241if (!state->error && !state->translation_unit.is_empty())2242_mesa_ast_to_hir(shader->ir, state);22432244if (!state->error) {2245validate_ir_tree(shader->ir);22462247/* Print out the unoptimized IR. */2248if (dump_hir) {2249_mesa_print_ir(stdout, shader->ir, state);2250}2251}22522253if (shader->InfoLog)2254ralloc_free(shader->InfoLog);22552256if (!state->error)2257set_shader_inout_layout(shader, state);22582259shader->symbols = new(shader->ir) glsl_symbol_table;2260shader->CompileStatus = state->error ? COMPILE_FAILURE : COMPILE_SUCCESS;2261shader->InfoLog = state->info_log;2262shader->Version = state->language_version;2263shader->IsES = state->es_shader;22642265struct gl_shader_compiler_options *options =2266&ctx->Const.ShaderCompilerOptions[shader->Stage];22672268if (!state->error && !shader->ir->is_empty()) {2269if (state->es_shader &&2270(options->LowerPrecisionFloat16 || options->LowerPrecisionInt16))2271lower_precision(options, shader->ir);2272lower_builtins(shader->ir);2273assign_subroutine_indexes(state);2274lower_subroutine(shader->ir, state);2275opt_shader_and_create_symbol_table(ctx, state->symbols, shader);2276}22772278if (!force_recompile) {2279free((void *)shader->FallbackSource);22802281/* Copy pre-processed shader include to fallback source otherwise we2282* have no guarantee the shader include source tree has not changed.2283*/2284shader->FallbackSource = source_has_shader_include ?2285strdup(source) : NULL;2286}22872288delete state->symbols;2289ralloc_free(state);22902291if (ctx->Cache && shader->CompileStatus == COMPILE_SUCCESS) {2292char sha1_buf[41];2293disk_cache_put_key(ctx->Cache, shader->sha1);2294if (ctx->_Shader->Flags & GLSL_CACHE_INFO) {2295_mesa_sha1_format(sha1_buf, shader->sha1);2296fprintf(stderr, "marking shader: %s\n", sha1_buf);2297}2298}2299}23002301} /* extern "C" */2302/**2303* Do the set of common optimizations passes2304*2305* \param ir List of instructions to be optimized2306* \param linked Is the shader linked? This enables2307* optimizations passes that remove code at2308* global scope and could cause linking to2309* fail.2310* \param uniform_locations_assigned Have locations already been assigned for2311* uniforms? This prevents the declarations2312* of unused uniforms from being removed.2313* The setting of this flag only matters if2314* \c linked is \c true.2315* \param options The driver's preferred shader options.2316* \param native_integers Selects optimizations that depend on the2317* implementations supporting integers2318* natively (as opposed to supporting2319* integers in floating point registers).2320*/2321bool2322do_common_optimization(exec_list *ir, bool linked,2323bool uniform_locations_assigned,2324const struct gl_shader_compiler_options *options,2325bool native_integers)2326{2327const bool debug = false;2328bool progress = false;23292330#define OPT(PASS, ...) do { \2331if (debug) { \2332fprintf(stderr, "START GLSL optimization %s\n", #PASS); \2333const bool opt_progress = PASS(__VA_ARGS__); \2334progress = opt_progress || progress; \2335if (opt_progress) \2336_mesa_print_ir(stderr, ir, NULL); \2337fprintf(stderr, "GLSL optimization %s: %s progress\n", \2338#PASS, opt_progress ? "made" : "no"); \2339} else { \2340progress = PASS(__VA_ARGS__) || progress; \2341} \2342} while (false)23432344OPT(lower_instructions, ir, SUB_TO_ADD_NEG);23452346if (linked) {2347OPT(do_function_inlining, ir);2348OPT(do_dead_functions, ir);2349OPT(do_structure_splitting, ir);2350}2351OPT(propagate_invariance, ir);2352OPT(do_if_simplification, ir);2353OPT(opt_flatten_nested_if_blocks, ir);2354OPT(opt_conditional_discard, ir);2355OPT(do_copy_propagation_elements, ir);23562357if (options->OptimizeForAOS && !linked)2358OPT(opt_flip_matrices, ir);23592360if (linked && options->OptimizeForAOS) {2361OPT(do_vectorize, ir);2362}23632364if (linked)2365OPT(do_dead_code, ir, uniform_locations_assigned);2366else2367OPT(do_dead_code_unlinked, ir);2368OPT(do_dead_code_local, ir);2369OPT(do_tree_grafting, ir);2370OPT(do_constant_propagation, ir);2371if (linked)2372OPT(do_constant_variable, ir);2373else2374OPT(do_constant_variable_unlinked, ir);2375OPT(do_constant_folding, ir);2376OPT(do_minmax_prune, ir);2377OPT(do_rebalance_tree, ir);2378OPT(do_algebraic, ir, native_integers, options);2379OPT(do_lower_jumps, ir, true, true, options->EmitNoMainReturn,2380options->EmitNoCont, options->EmitNoLoops);2381OPT(do_vec_index_to_swizzle, ir);2382OPT(lower_vector_insert, ir, false);2383OPT(optimize_swizzles, ir);23842385/* Some drivers only call do_common_optimization() once rather than in a2386* loop, and split arrays causes each element of a constant array to2387* dereference is own copy of the entire array initilizer. This IR is not2388* something that can be generated manually in a shader and is not2389* accounted for by NIR optimisations, the result is an exponential slow2390* down in compilation speed as a constant arrays element count grows. To2391* avoid that here we make sure to always clean up the mess split arrays2392* causes to constant arrays.2393*/2394bool array_split = optimize_split_arrays(ir, linked);2395if (array_split)2396do_constant_propagation(ir);2397progress |= array_split;23982399OPT(optimize_redundant_jumps, ir);24002401if (options->MaxUnrollIterations) {2402loop_state *ls = analyze_loop_variables(ir);2403if (ls->loop_found) {2404bool loop_progress = unroll_loops(ir, ls, options);2405while (loop_progress) {2406loop_progress = false;2407loop_progress |= do_constant_propagation(ir);2408loop_progress |= do_if_simplification(ir);24092410/* Some drivers only call do_common_optimization() once rather2411* than in a loop. So we must call do_lower_jumps() after2412* unrolling a loop because for drivers that use LLVM validation2413* will fail if a jump is not the last instruction in the block.2414* For example the following will fail LLVM validation:2415*2416* (loop (2417* ...2418* break2419* (assign (x) (var_ref v124) (expression int + (var_ref v124)2420* (constant int (1)) ) )2421* ))2422*/2423loop_progress |= do_lower_jumps(ir, true, true,2424options->EmitNoMainReturn,2425options->EmitNoCont,2426options->EmitNoLoops);2427}2428progress |= loop_progress;2429}2430delete ls;2431}24322433/* If the PIPE_CAP_GLSL_OPTIMIZE_CONSERVATIVELY cap is set, this pass will2434* only be called once rather than repeatedly until no further progress is2435* made.2436*2437* If an optimization pass fails to preserve the invariant flag, calling2438* the pass only once may result in incorrect code generation. Always call2439* propagate_invariance() last to avoid this possibility.2440*/2441OPT(propagate_invariance, ir);24422443#undef OPT24442445return progress;2446}244724482449