Path: blob/21.2-virgl/src/compiler/glsl/ast_function.cpp
4547 views
/*1* Copyright © 2010 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*/2223#include "glsl_symbol_table.h"24#include "ast.h"25#include "compiler/glsl_types.h"26#include "ir.h"27#include "main/mtypes.h"28#include "main/shaderobj.h"29#include "builtin_functions.h"3031static ir_rvalue *32convert_component(ir_rvalue *src, const glsl_type *desired_type);3334static unsigned35process_parameters(exec_list *instructions, exec_list *actual_parameters,36exec_list *parameters,37struct _mesa_glsl_parse_state *state)38{39void *mem_ctx = state;40unsigned count = 0;4142foreach_list_typed(ast_node, ast, link, parameters) {43/* We need to process the parameters first in order to know if we can44* raise or not a unitialized warning. Calling set_is_lhs silence the45* warning for now. Raising the warning or not will be checked at46* verify_parameter_modes.47*/48ast->set_is_lhs(true);49ir_rvalue *result = ast->hir(instructions, state);5051/* Error happened processing function parameter */52if (!result) {53actual_parameters->push_tail(ir_rvalue::error_value(mem_ctx));54count++;55continue;56}5758ir_constant *const constant =59result->constant_expression_value(mem_ctx);6061if (constant != NULL)62result = constant;6364actual_parameters->push_tail(result);65count++;66}6768return count;69}707172/**73* Generate a source prototype for a function signature74*75* \param return_type Return type of the function. May be \c NULL.76* \param name Name of the function.77* \param parameters List of \c ir_instruction nodes representing the78* parameter list for the function. This may be either a79* formal (\c ir_variable) or actual (\c ir_rvalue)80* parameter list. Only the type is used.81*82* \return83* A ralloced string representing the prototype of the function.84*/85char *86prototype_string(const glsl_type *return_type, const char *name,87exec_list *parameters)88{89char *str = NULL;9091if (return_type != NULL)92str = ralloc_asprintf(NULL, "%s ", return_type->name);9394ralloc_asprintf_append(&str, "%s(", name);9596const char *comma = "";97foreach_in_list(const ir_variable, param, parameters) {98ralloc_asprintf_append(&str, "%s%s", comma, param->type->name);99comma = ", ";100}101102ralloc_strcat(&str, ")");103return str;104}105106static bool107verify_image_parameter(YYLTYPE *loc, _mesa_glsl_parse_state *state,108const ir_variable *formal, const ir_variable *actual)109{110/**111* From the ARB_shader_image_load_store specification:112*113* "The values of image variables qualified with coherent,114* volatile, restrict, readonly, or writeonly may not be passed115* to functions whose formal parameters lack such116* qualifiers. [...] It is legal to have additional qualifiers117* on a formal parameter, but not to have fewer."118*/119if (actual->data.memory_coherent && !formal->data.memory_coherent) {120_mesa_glsl_error(loc, state,121"function call parameter `%s' drops "122"`coherent' qualifier", formal->name);123return false;124}125126if (actual->data.memory_volatile && !formal->data.memory_volatile) {127_mesa_glsl_error(loc, state,128"function call parameter `%s' drops "129"`volatile' qualifier", formal->name);130return false;131}132133if (actual->data.memory_restrict && !formal->data.memory_restrict) {134_mesa_glsl_error(loc, state,135"function call parameter `%s' drops "136"`restrict' qualifier", formal->name);137return false;138}139140if (actual->data.memory_read_only && !formal->data.memory_read_only) {141_mesa_glsl_error(loc, state,142"function call parameter `%s' drops "143"`readonly' qualifier", formal->name);144return false;145}146147if (actual->data.memory_write_only && !formal->data.memory_write_only) {148_mesa_glsl_error(loc, state,149"function call parameter `%s' drops "150"`writeonly' qualifier", formal->name);151return false;152}153154return true;155}156157static bool158verify_first_atomic_parameter(YYLTYPE *loc, _mesa_glsl_parse_state *state,159ir_variable *var)160{161if (!var ||162(!var->is_in_shader_storage_block() &&163var->data.mode != ir_var_shader_shared)) {164_mesa_glsl_error(loc, state, "First argument to atomic function "165"must be a buffer or shared variable");166return false;167}168return true;169}170171static bool172is_atomic_function(const char *func_name)173{174return !strcmp(func_name, "atomicAdd") ||175!strcmp(func_name, "atomicMin") ||176!strcmp(func_name, "atomicMax") ||177!strcmp(func_name, "atomicAnd") ||178!strcmp(func_name, "atomicOr") ||179!strcmp(func_name, "atomicXor") ||180!strcmp(func_name, "atomicExchange") ||181!strcmp(func_name, "atomicCompSwap");182}183184static bool185verify_atomic_image_parameter_qualifier(YYLTYPE *loc, _mesa_glsl_parse_state *state,186ir_variable *var)187{188if (!var ||189(var->data.image_format != PIPE_FORMAT_R32_UINT &&190var->data.image_format != PIPE_FORMAT_R32_SINT &&191var->data.image_format != PIPE_FORMAT_R32_FLOAT)) {192_mesa_glsl_error(loc, state, "Image atomic functions should use r32i/r32ui "193"format qualifier");194return false;195}196return true;197}198199static bool200is_atomic_image_function(const char *func_name)201{202return !strcmp(func_name, "imageAtomicAdd") ||203!strcmp(func_name, "imageAtomicMin") ||204!strcmp(func_name, "imageAtomicMax") ||205!strcmp(func_name, "imageAtomicAnd") ||206!strcmp(func_name, "imageAtomicOr") ||207!strcmp(func_name, "imageAtomicXor") ||208!strcmp(func_name, "imageAtomicExchange") ||209!strcmp(func_name, "imageAtomicCompSwap") ||210!strcmp(func_name, "imageAtomicIncWrap") ||211!strcmp(func_name, "imageAtomicDecWrap");212}213214215/**216* Verify that 'out' and 'inout' actual parameters are lvalues. Also, verify217* that 'const_in' formal parameters (an extension in our IR) correspond to218* ir_constant actual parameters.219*/220static bool221verify_parameter_modes(_mesa_glsl_parse_state *state,222ir_function_signature *sig,223exec_list &actual_ir_parameters,224exec_list &actual_ast_parameters)225{226exec_node *actual_ir_node = actual_ir_parameters.get_head_raw();227exec_node *actual_ast_node = actual_ast_parameters.get_head_raw();228229foreach_in_list(const ir_variable, formal, &sig->parameters) {230/* The lists must be the same length. */231assert(!actual_ir_node->is_tail_sentinel());232assert(!actual_ast_node->is_tail_sentinel());233234const ir_rvalue *const actual = (ir_rvalue *) actual_ir_node;235const ast_expression *const actual_ast =236exec_node_data(ast_expression, actual_ast_node, link);237238YYLTYPE loc = actual_ast->get_location();239240/* Verify that 'const_in' parameters are ir_constants. */241if (formal->data.mode == ir_var_const_in &&242actual->ir_type != ir_type_constant) {243_mesa_glsl_error(&loc, state,244"parameter `in %s' must be a constant expression",245formal->name);246return false;247}248249/* Verify that shader_in parameters are shader inputs */250if (formal->data.must_be_shader_input) {251const ir_rvalue *val = actual;252253/* GLSL 4.40 allows swizzles, while earlier GLSL versions do not. */254if (val->ir_type == ir_type_swizzle) {255if (!state->is_version(440, 0)) {256_mesa_glsl_error(&loc, state,257"parameter `%s` must not be swizzled",258formal->name);259return false;260}261val = ((ir_swizzle *)val)->val;262}263264for (;;) {265if (val->ir_type == ir_type_dereference_array) {266val = ((ir_dereference_array *)val)->array;267} else if (val->ir_type == ir_type_dereference_record &&268!state->es_shader) {269val = ((ir_dereference_record *)val)->record;270} else271break;272}273274ir_variable *var = NULL;275if (const ir_dereference_variable *deref_var = val->as_dereference_variable())276var = deref_var->variable_referenced();277278if (!var || var->data.mode != ir_var_shader_in) {279_mesa_glsl_error(&loc, state,280"parameter `%s` must be a shader input",281formal->name);282return false;283}284285var->data.must_be_shader_input = 1;286}287288/* Verify that 'out' and 'inout' actual parameters are lvalues. */289if (formal->data.mode == ir_var_function_out290|| formal->data.mode == ir_var_function_inout) {291const char *mode = NULL;292switch (formal->data.mode) {293case ir_var_function_out: mode = "out"; break;294case ir_var_function_inout: mode = "inout"; break;295default: assert(false); break;296}297298/* This AST-based check catches errors like f(i++). The IR-based299* is_lvalue() is insufficient because the actual parameter at the300* IR-level is just a temporary value, which is an l-value.301*/302if (actual_ast->non_lvalue_description != NULL) {303_mesa_glsl_error(&loc, state,304"function parameter '%s %s' references a %s",305mode, formal->name,306actual_ast->non_lvalue_description);307return false;308}309310ir_variable *var = actual->variable_referenced();311312if (var && formal->data.mode == ir_var_function_inout) {313if ((var->data.mode == ir_var_auto ||314var->data.mode == ir_var_shader_out) &&315!var->data.assigned &&316!is_gl_identifier(var->name)) {317_mesa_glsl_warning(&loc, state, "`%s' used uninitialized",318var->name);319}320}321322if (var)323var->data.assigned = true;324325if (var && var->data.read_only) {326_mesa_glsl_error(&loc, state,327"function parameter '%s %s' references the "328"read-only variable '%s'",329mode, formal->name,330actual->variable_referenced()->name);331return false;332} else if (!actual->is_lvalue(state)) {333_mesa_glsl_error(&loc, state,334"function parameter '%s %s' is not an lvalue",335mode, formal->name);336return false;337}338} else {339assert(formal->data.mode == ir_var_function_in ||340formal->data.mode == ir_var_const_in);341ir_variable *var = actual->variable_referenced();342if (var) {343if ((var->data.mode == ir_var_auto ||344var->data.mode == ir_var_shader_out) &&345!var->data.assigned &&346!is_gl_identifier(var->name)) {347_mesa_glsl_warning(&loc, state, "`%s' used uninitialized",348var->name);349}350}351}352353if (formal->type->is_image() &&354actual->variable_referenced()) {355if (!verify_image_parameter(&loc, state, formal,356actual->variable_referenced()))357return false;358}359360actual_ir_node = actual_ir_node->next;361actual_ast_node = actual_ast_node->next;362}363364/* The first parameter of atomic functions must be a buffer variable */365const char *func_name = sig->function_name();366bool is_atomic = is_atomic_function(func_name);367if (is_atomic) {368const ir_rvalue *const actual =369(ir_rvalue *) actual_ir_parameters.get_head_raw();370371const ast_expression *const actual_ast =372exec_node_data(ast_expression,373actual_ast_parameters.get_head_raw(), link);374YYLTYPE loc = actual_ast->get_location();375376if (!verify_first_atomic_parameter(&loc, state,377actual->variable_referenced())) {378return false;379}380} else if (is_atomic_image_function(func_name)) {381const ir_rvalue *const actual =382(ir_rvalue *) actual_ir_parameters.get_head_raw();383384const ast_expression *const actual_ast =385exec_node_data(ast_expression,386actual_ast_parameters.get_head_raw(), link);387YYLTYPE loc = actual_ast->get_location();388389if (!verify_atomic_image_parameter_qualifier(&loc, state,390actual->variable_referenced())) {391return false;392}393}394395return true;396}397398struct copy_index_deref_data {399void *mem_ctx;400exec_list *before_instructions;401};402403static void404copy_index_derefs_to_temps(ir_instruction *ir, void *data)405{406struct copy_index_deref_data *d = (struct copy_index_deref_data *)data;407408if (ir->ir_type == ir_type_dereference_array) {409ir_dereference_array *a = (ir_dereference_array *) ir;410ir = a->array->as_dereference();411412ir_rvalue *idx = a->array_index;413ir_variable *var = idx->variable_referenced();414415/* If the index is read only it cannot change so there is no need416* to copy it.417*/418if (!var || var->data.read_only || var->data.memory_read_only)419return;420421ir_variable *tmp = new(d->mem_ctx) ir_variable(idx->type, "idx_tmp",422ir_var_temporary);423d->before_instructions->push_tail(tmp);424425ir_dereference_variable *const deref_tmp_1 =426new(d->mem_ctx) ir_dereference_variable(tmp);427ir_assignment *const assignment =428new(d->mem_ctx) ir_assignment(deref_tmp_1,429idx->clone(d->mem_ctx, NULL));430d->before_instructions->push_tail(assignment);431432/* Replace the array index with a dereference of the new temporary */433ir_dereference_variable *const deref_tmp_2 =434new(d->mem_ctx) ir_dereference_variable(tmp);435a->array_index = deref_tmp_2;436}437}438439static void440fix_parameter(void *mem_ctx, ir_rvalue *actual, const glsl_type *formal_type,441exec_list *before_instructions, exec_list *after_instructions,442bool parameter_is_inout)443{444ir_expression *const expr = actual->as_expression();445446/* If the types match exactly and the parameter is not a vector-extract,447* nothing needs to be done to fix the parameter.448*/449if (formal_type == actual->type450&& (expr == NULL || expr->operation != ir_binop_vector_extract)451&& actual->as_dereference_variable())452return;453454/* An array index could also be an out variable so we need to make a copy455* of them before the function is called.456*/457if (!actual->as_dereference_variable()) {458struct copy_index_deref_data data;459data.mem_ctx = mem_ctx;460data.before_instructions = before_instructions;461462visit_tree(actual, copy_index_derefs_to_temps, &data);463}464465/* To convert an out parameter, we need to create a temporary variable to466* hold the value before conversion, and then perform the conversion after467* the function call returns.468*469* This has the effect of transforming code like this:470*471* void f(out int x);472* float value;473* f(value);474*475* Into IR that's equivalent to this:476*477* void f(out int x);478* float value;479* int out_parameter_conversion;480* f(out_parameter_conversion);481* value = float(out_parameter_conversion);482*483* If the parameter is an ir_expression of ir_binop_vector_extract,484* additional conversion is needed in the post-call re-write.485*/486ir_variable *tmp =487new(mem_ctx) ir_variable(formal_type, "inout_tmp", ir_var_temporary);488489before_instructions->push_tail(tmp);490491/* If the parameter is an inout parameter, copy the value of the actual492* parameter to the new temporary. Note that no type conversion is allowed493* here because inout parameters must match types exactly.494*/495if (parameter_is_inout) {496/* Inout parameters should never require conversion, since that would497* require an implicit conversion to exist both to and from the formal498* parameter type, and there are no bidirectional implicit conversions.499*/500assert (actual->type == formal_type);501502ir_dereference_variable *const deref_tmp_1 =503new(mem_ctx) ir_dereference_variable(tmp);504ir_assignment *const assignment =505new(mem_ctx) ir_assignment(deref_tmp_1, actual->clone(mem_ctx, NULL));506before_instructions->push_tail(assignment);507}508509/* Replace the parameter in the call with a dereference of the new510* temporary.511*/512ir_dereference_variable *const deref_tmp_2 =513new(mem_ctx) ir_dereference_variable(tmp);514actual->replace_with(deref_tmp_2);515516517/* Copy the temporary variable to the actual parameter with optional518* type conversion applied.519*/520ir_rvalue *rhs = new(mem_ctx) ir_dereference_variable(tmp);521if (actual->type != formal_type)522rhs = convert_component(rhs, actual->type);523524ir_rvalue *lhs = actual;525if (expr != NULL && expr->operation == ir_binop_vector_extract) {526lhs = new(mem_ctx) ir_dereference_array(expr->operands[0]->clone(mem_ctx,527NULL),528expr->operands[1]->clone(mem_ctx,529NULL));530}531532ir_assignment *const assignment_2 = new(mem_ctx) ir_assignment(lhs, rhs);533after_instructions->push_tail(assignment_2);534}535536/**537* Generate a function call.538*539* For non-void functions, this returns a dereference of the temporary540* variable which stores the return value for the call. For void functions,541* this returns NULL.542*/543static ir_rvalue *544generate_call(exec_list *instructions, ir_function_signature *sig,545exec_list *actual_parameters,546ir_variable *sub_var,547ir_rvalue *array_idx,548struct _mesa_glsl_parse_state *state)549{550void *ctx = state;551exec_list post_call_conversions;552553/* Perform implicit conversion of arguments. For out parameters, we need554* to place them in a temporary variable and do the conversion after the555* call takes place. Since we haven't emitted the call yet, we'll place556* the post-call conversions in a temporary exec_list, and emit them later.557*/558foreach_two_lists(formal_node, &sig->parameters,559actual_node, actual_parameters) {560ir_rvalue *actual = (ir_rvalue *) actual_node;561ir_variable *formal = (ir_variable *) formal_node;562563if (formal->type->is_numeric() || formal->type->is_boolean()) {564switch (formal->data.mode) {565case ir_var_const_in:566case ir_var_function_in: {567ir_rvalue *converted568= convert_component(actual, formal->type);569actual->replace_with(converted);570break;571}572case ir_var_function_out:573case ir_var_function_inout:574fix_parameter(ctx, actual, formal->type,575instructions, &post_call_conversions,576formal->data.mode == ir_var_function_inout);577break;578default:579assert (!"Illegal formal parameter mode");580break;581}582}583}584585/* Section 4.3.2 (Const) of the GLSL 1.10.59 spec says:586*587* "Initializers for const declarations must be formed from literal588* values, other const variables (not including function call589* paramaters), or expressions of these.590*591* Constructors may be used in such expressions, but function calls may592* not."593*594* Section 4.3.3 (Constant Expressions) of the GLSL 1.20.8 spec says:595*596* "A constant expression is one of597*598* ...599*600* - a built-in function call whose arguments are all constant601* expressions, with the exception of the texture lookup602* functions, the noise functions, and ftransform. The built-in603* functions dFdx, dFdy, and fwidth must return 0 when evaluated604* inside an initializer with an argument that is a constant605* expression."606*607* Section 5.10 (Constant Expressions) of the GLSL ES 1.00.17 spec says:608*609* "A constant expression is one of610*611* ...612*613* - a built-in function call whose arguments are all constant614* expressions, with the exception of the texture lookup615* functions."616*617* Section 4.3.3 (Constant Expressions) of the GLSL ES 3.00.4 spec says:618*619* "A constant expression is one of620*621* ...622*623* - a built-in function call whose arguments are all constant624* expressions, with the exception of the texture lookup625* functions. The built-in functions dFdx, dFdy, and fwidth must626* return 0 when evaluated inside an initializer with an argument627* that is a constant expression."628*629* If the function call is a constant expression, don't generate any630* instructions; just generate an ir_constant.631*/632if (state->is_version(120, 100) ||633state->ctx->Const.AllowGLSLBuiltinConstantExpression) {634ir_constant *value = sig->constant_expression_value(ctx,635actual_parameters,636NULL);637if (value != NULL) {638return value;639}640}641642ir_dereference_variable *deref = NULL;643if (!sig->return_type->is_void()) {644/* Create a new temporary to hold the return value. */645char *const name = ir_variable::temporaries_allocate_names646? ralloc_asprintf(ctx, "%s_retval", sig->function_name())647: NULL;648649ir_variable *var;650651var = new(ctx) ir_variable(sig->return_type, name, ir_var_temporary);652instructions->push_tail(var);653654ralloc_free(name);655656deref = new(ctx) ir_dereference_variable(var);657}658659ir_call *call = new(ctx) ir_call(sig, deref,660actual_parameters, sub_var, array_idx);661instructions->push_tail(call);662663/* Also emit any necessary out-parameter conversions. */664instructions->append_list(&post_call_conversions);665666return deref ? deref->clone(ctx, NULL) : NULL;667}668669/**670* Given a function name and parameter list, find the matching signature.671*/672static ir_function_signature *673match_function_by_name(const char *name,674exec_list *actual_parameters,675struct _mesa_glsl_parse_state *state)676{677ir_function *f = state->symbols->get_function(name);678ir_function_signature *local_sig = NULL;679ir_function_signature *sig = NULL;680681/* Is the function hidden by a record type constructor? */682if (state->symbols->get_type(name))683return sig; /* no match */684685/* Is the function hidden by a variable (impossible in 1.10)? */686if (!state->symbols->separate_function_namespace687&& state->symbols->get_variable(name))688return sig; /* no match */689690if (f != NULL) {691/* In desktop GL, the presence of a user-defined signature hides any692* built-in signatures, so we must ignore them. In contrast, in ES2693* user-defined signatures add new overloads, so we must consider them.694*/695bool allow_builtins = state->es_shader || !f->has_user_signature();696697/* Look for a match in the local shader. If exact, we're done. */698bool is_exact = false;699sig = local_sig = f->matching_signature(state, actual_parameters,700allow_builtins, &is_exact);701if (is_exact)702return sig;703704if (!allow_builtins)705return sig;706}707708/* Local shader has no exact candidates; check the built-ins. */709sig = _mesa_glsl_find_builtin_function(state, name, actual_parameters);710711/* if _mesa_glsl_find_builtin_function failed, fall back to the result712* of choose_best_inexact_overload() instead. This should only affect713* GLES.714*/715return sig ? sig : local_sig;716}717718static ir_function_signature *719match_subroutine_by_name(const char *name,720exec_list *actual_parameters,721struct _mesa_glsl_parse_state *state,722ir_variable **var_r)723{724void *ctx = state;725ir_function_signature *sig = NULL;726ir_function *f, *found = NULL;727const char *new_name;728ir_variable *var;729bool is_exact = false;730731new_name =732ralloc_asprintf(ctx, "%s_%s",733_mesa_shader_stage_to_subroutine_prefix(state->stage),734name);735var = state->symbols->get_variable(new_name);736if (!var)737return NULL;738739for (int i = 0; i < state->num_subroutine_types; i++) {740f = state->subroutine_types[i];741if (strcmp(f->name, var->type->without_array()->name))742continue;743found = f;744break;745}746747if (!found)748return NULL;749*var_r = var;750sig = found->matching_signature(state, actual_parameters,751false, &is_exact);752return sig;753}754755static ir_rvalue *756generate_array_index(void *mem_ctx, exec_list *instructions,757struct _mesa_glsl_parse_state *state, YYLTYPE loc,758const ast_expression *array, ast_expression *idx,759const char **function_name, exec_list *actual_parameters)760{761if (array->oper == ast_array_index) {762/* This handles arrays of arrays */763ir_rvalue *outer_array = generate_array_index(mem_ctx, instructions,764state, loc,765array->subexpressions[0],766array->subexpressions[1],767function_name,768actual_parameters);769ir_rvalue *outer_array_idx = idx->hir(instructions, state);770771YYLTYPE index_loc = idx->get_location();772return _mesa_ast_array_index_to_hir(mem_ctx, state, outer_array,773outer_array_idx, loc,774index_loc);775} else {776ir_variable *sub_var = NULL;777*function_name = array->primary_expression.identifier;778779if (!match_subroutine_by_name(*function_name, actual_parameters,780state, &sub_var)) {781_mesa_glsl_error(&loc, state, "Unknown subroutine `%s'",782*function_name);783*function_name = NULL; /* indicate error condition to caller */784return NULL;785}786787ir_rvalue *outer_array_idx = idx->hir(instructions, state);788return new(mem_ctx) ir_dereference_array(sub_var, outer_array_idx);789}790}791792static bool793function_exists(_mesa_glsl_parse_state *state,794struct glsl_symbol_table *symbols, const char *name)795{796ir_function *f = symbols->get_function(name);797if (f != NULL) {798foreach_in_list(ir_function_signature, sig, &f->signatures) {799if (sig->is_builtin() && !sig->is_builtin_available(state))800continue;801return true;802}803}804return false;805}806807static void808print_function_prototypes(_mesa_glsl_parse_state *state, YYLTYPE *loc,809ir_function *f)810{811if (f == NULL)812return;813814foreach_in_list(ir_function_signature, sig, &f->signatures) {815if (sig->is_builtin() && !sig->is_builtin_available(state))816continue;817818char *str = prototype_string(sig->return_type, f->name,819&sig->parameters);820_mesa_glsl_error(loc, state, " %s", str);821ralloc_free(str);822}823}824825/**826* Raise a "no matching function" error, listing all possible overloads the827* compiler considered so developers can figure out what went wrong.828*/829static void830no_matching_function_error(const char *name,831YYLTYPE *loc,832exec_list *actual_parameters,833_mesa_glsl_parse_state *state)834{835gl_shader *sh = _mesa_glsl_get_builtin_function_shader();836837if (!function_exists(state, state->symbols, name)838&& (!state->uses_builtin_functions839|| !function_exists(state, sh->symbols, name))) {840_mesa_glsl_error(loc, state, "no function with name '%s'", name);841} else {842char *str = prototype_string(NULL, name, actual_parameters);843_mesa_glsl_error(loc, state,844"no matching function for call to `%s';"845" candidates are:",846str);847ralloc_free(str);848849print_function_prototypes(state, loc,850state->symbols->get_function(name));851852if (state->uses_builtin_functions) {853print_function_prototypes(state, loc,854sh->symbols->get_function(name));855}856}857}858859/**860* Perform automatic type conversion of constructor parameters861*862* This implements the rules in the "Conversion and Scalar Constructors"863* section (GLSL 1.10 section 5.4.1), not the "Implicit Conversions" rules.864*/865static ir_rvalue *866convert_component(ir_rvalue *src, const glsl_type *desired_type)867{868void *ctx = ralloc_parent(src);869const unsigned a = desired_type->base_type;870const unsigned b = src->type->base_type;871ir_expression *result = NULL;872873if (src->type->is_error())874return src;875876assert(a <= GLSL_TYPE_IMAGE);877assert(b <= GLSL_TYPE_IMAGE);878879if (a == b)880return src;881882switch (a) {883case GLSL_TYPE_UINT:884switch (b) {885case GLSL_TYPE_INT:886result = new(ctx) ir_expression(ir_unop_i2u, src);887break;888case GLSL_TYPE_FLOAT:889result = new(ctx) ir_expression(ir_unop_f2u, src);890break;891case GLSL_TYPE_BOOL:892result = new(ctx) ir_expression(ir_unop_i2u,893new(ctx) ir_expression(ir_unop_b2i,894src));895break;896case GLSL_TYPE_DOUBLE:897result = new(ctx) ir_expression(ir_unop_d2u, src);898break;899case GLSL_TYPE_UINT64:900result = new(ctx) ir_expression(ir_unop_u642u, src);901break;902case GLSL_TYPE_INT64:903result = new(ctx) ir_expression(ir_unop_i642u, src);904break;905case GLSL_TYPE_SAMPLER:906result = new(ctx) ir_expression(ir_unop_unpack_sampler_2x32, src);907break;908case GLSL_TYPE_IMAGE:909result = new(ctx) ir_expression(ir_unop_unpack_image_2x32, src);910break;911}912break;913case GLSL_TYPE_INT:914switch (b) {915case GLSL_TYPE_UINT:916result = new(ctx) ir_expression(ir_unop_u2i, src);917break;918case GLSL_TYPE_FLOAT:919result = new(ctx) ir_expression(ir_unop_f2i, src);920break;921case GLSL_TYPE_BOOL:922result = new(ctx) ir_expression(ir_unop_b2i, src);923break;924case GLSL_TYPE_DOUBLE:925result = new(ctx) ir_expression(ir_unop_d2i, src);926break;927case GLSL_TYPE_UINT64:928result = new(ctx) ir_expression(ir_unop_u642i, src);929break;930case GLSL_TYPE_INT64:931result = new(ctx) ir_expression(ir_unop_i642i, src);932break;933}934break;935case GLSL_TYPE_FLOAT:936switch (b) {937case GLSL_TYPE_UINT:938result = new(ctx) ir_expression(ir_unop_u2f, desired_type, src, NULL);939break;940case GLSL_TYPE_INT:941result = new(ctx) ir_expression(ir_unop_i2f, desired_type, src, NULL);942break;943case GLSL_TYPE_BOOL:944result = new(ctx) ir_expression(ir_unop_b2f, desired_type, src, NULL);945break;946case GLSL_TYPE_DOUBLE:947result = new(ctx) ir_expression(ir_unop_d2f, desired_type, src, NULL);948break;949case GLSL_TYPE_UINT64:950result = new(ctx) ir_expression(ir_unop_u642f, desired_type, src, NULL);951break;952case GLSL_TYPE_INT64:953result = new(ctx) ir_expression(ir_unop_i642f, desired_type, src, NULL);954break;955}956break;957case GLSL_TYPE_BOOL:958switch (b) {959case GLSL_TYPE_UINT:960result = new(ctx) ir_expression(ir_unop_i2b,961new(ctx) ir_expression(ir_unop_u2i,962src));963break;964case GLSL_TYPE_INT:965result = new(ctx) ir_expression(ir_unop_i2b, desired_type, src, NULL);966break;967case GLSL_TYPE_FLOAT:968result = new(ctx) ir_expression(ir_unop_f2b, desired_type, src, NULL);969break;970case GLSL_TYPE_DOUBLE:971result = new(ctx) ir_expression(ir_unop_d2b, desired_type, src, NULL);972break;973case GLSL_TYPE_UINT64:974result = new(ctx) ir_expression(ir_unop_i642b,975new(ctx) ir_expression(ir_unop_u642i64,976src));977break;978case GLSL_TYPE_INT64:979result = new(ctx) ir_expression(ir_unop_i642b, desired_type, src, NULL);980break;981}982break;983case GLSL_TYPE_DOUBLE:984switch (b) {985case GLSL_TYPE_INT:986result = new(ctx) ir_expression(ir_unop_i2d, src);987break;988case GLSL_TYPE_UINT:989result = new(ctx) ir_expression(ir_unop_u2d, src);990break;991case GLSL_TYPE_BOOL:992result = new(ctx) ir_expression(ir_unop_f2d,993new(ctx) ir_expression(ir_unop_b2f,994src));995break;996case GLSL_TYPE_FLOAT:997result = new(ctx) ir_expression(ir_unop_f2d, desired_type, src, NULL);998break;999case GLSL_TYPE_UINT64:1000result = new(ctx) ir_expression(ir_unop_u642d, desired_type, src, NULL);1001break;1002case GLSL_TYPE_INT64:1003result = new(ctx) ir_expression(ir_unop_i642d, desired_type, src, NULL);1004break;1005}1006break;1007case GLSL_TYPE_UINT64:1008switch (b) {1009case GLSL_TYPE_INT:1010result = new(ctx) ir_expression(ir_unop_i2u64, src);1011break;1012case GLSL_TYPE_UINT:1013result = new(ctx) ir_expression(ir_unop_u2u64, src);1014break;1015case GLSL_TYPE_BOOL:1016result = new(ctx) ir_expression(ir_unop_i642u64,1017new(ctx) ir_expression(ir_unop_b2i64,1018src));1019break;1020case GLSL_TYPE_FLOAT:1021result = new(ctx) ir_expression(ir_unop_f2u64, src);1022break;1023case GLSL_TYPE_DOUBLE:1024result = new(ctx) ir_expression(ir_unop_d2u64, src);1025break;1026case GLSL_TYPE_INT64:1027result = new(ctx) ir_expression(ir_unop_i642u64, src);1028break;1029}1030break;1031case GLSL_TYPE_INT64:1032switch (b) {1033case GLSL_TYPE_INT:1034result = new(ctx) ir_expression(ir_unop_i2i64, src);1035break;1036case GLSL_TYPE_UINT:1037result = new(ctx) ir_expression(ir_unop_u2i64, src);1038break;1039case GLSL_TYPE_BOOL:1040result = new(ctx) ir_expression(ir_unop_b2i64, src);1041break;1042case GLSL_TYPE_FLOAT:1043result = new(ctx) ir_expression(ir_unop_f2i64, src);1044break;1045case GLSL_TYPE_DOUBLE:1046result = new(ctx) ir_expression(ir_unop_d2i64, src);1047break;1048case GLSL_TYPE_UINT64:1049result = new(ctx) ir_expression(ir_unop_u642i64, src);1050break;1051}1052break;1053case GLSL_TYPE_SAMPLER:1054switch (b) {1055case GLSL_TYPE_UINT:1056result = new(ctx)1057ir_expression(ir_unop_pack_sampler_2x32, desired_type, src);1058break;1059}1060break;1061case GLSL_TYPE_IMAGE:1062switch (b) {1063case GLSL_TYPE_UINT:1064result = new(ctx)1065ir_expression(ir_unop_pack_image_2x32, desired_type, src);1066break;1067}1068break;1069}10701071assert(result != NULL);1072assert(result->type == desired_type);10731074/* Try constant folding; it may fold in the conversion we just added. */1075ir_constant *const constant = result->constant_expression_value(ctx);1076return (constant != NULL) ? (ir_rvalue *) constant : (ir_rvalue *) result;1077}107810791080/**1081* Perform automatic type and constant conversion of constructor parameters1082*1083* This implements the rules in the "Implicit Conversions" rules, not the1084* "Conversion and Scalar Constructors".1085*1086* After attempting the implicit conversion, an attempt to convert into a1087* constant valued expression is also done.1088*1089* The \c from \c ir_rvalue is converted "in place".1090*1091* \param from Operand that is being converted1092* \param to Base type the operand will be converted to1093* \param state GLSL compiler state1094*1095* \return1096* If the attempt to convert into a constant expression succeeds, \c true is1097* returned. Otherwise \c false is returned.1098*/1099static bool1100implicitly_convert_component(ir_rvalue * &from, const glsl_base_type to,1101struct _mesa_glsl_parse_state *state)1102{1103void *mem_ctx = state;1104ir_rvalue *result = from;11051106if (to != from->type->base_type) {1107const glsl_type *desired_type =1108glsl_type::get_instance(to,1109from->type->vector_elements,1110from->type->matrix_columns);11111112if (from->type->can_implicitly_convert_to(desired_type, state)) {1113/* Even though convert_component() implements the constructor1114* conversion rules (not the implicit conversion rules), its safe1115* to use it here because we already checked that the implicit1116* conversion is legal.1117*/1118result = convert_component(from, desired_type);1119}1120}11211122ir_rvalue *const constant = result->constant_expression_value(mem_ctx);11231124if (constant != NULL)1125result = constant;11261127if (from != result) {1128from->replace_with(result);1129from = result;1130}11311132return constant != NULL;1133}113411351136/**1137* Dereference a specific component from a scalar, vector, or matrix1138*/1139static ir_rvalue *1140dereference_component(ir_rvalue *src, unsigned component)1141{1142void *ctx = ralloc_parent(src);1143assert(component < src->type->components());11441145/* If the source is a constant, just create a new constant instead of a1146* dereference of the existing constant.1147*/1148ir_constant *constant = src->as_constant();1149if (constant)1150return new(ctx) ir_constant(constant, component);11511152if (src->type->is_scalar()) {1153return src;1154} else if (src->type->is_vector()) {1155return new(ctx) ir_swizzle(src, component, 0, 0, 0, 1);1156} else {1157assert(src->type->is_matrix());11581159/* Dereference a row of the matrix, then call this function again to get1160* a specific element from that row.1161*/1162const int c = component / src->type->column_type()->vector_elements;1163const int r = component % src->type->column_type()->vector_elements;1164ir_constant *const col_index = new(ctx) ir_constant(c);1165ir_dereference *const col = new(ctx) ir_dereference_array(src,1166col_index);11671168col->type = src->type->column_type();11691170return dereference_component(col, r);1171}11721173assert(!"Should not get here.");1174return NULL;1175}117611771178static ir_rvalue *1179process_vec_mat_constructor(exec_list *instructions,1180const glsl_type *constructor_type,1181YYLTYPE *loc, exec_list *parameters,1182struct _mesa_glsl_parse_state *state)1183{1184void *ctx = state;11851186/* The ARB_shading_language_420pack spec says:1187*1188* "If an initializer is a list of initializers enclosed in curly braces,1189* the variable being declared must be a vector, a matrix, an array, or a1190* structure.1191*1192* int i = { 1 }; // illegal, i is not an aggregate"1193*/1194if (constructor_type->vector_elements <= 1) {1195_mesa_glsl_error(loc, state, "aggregates can only initialize vectors, "1196"matrices, arrays, and structs");1197return ir_rvalue::error_value(ctx);1198}11991200exec_list actual_parameters;1201const unsigned parameter_count =1202process_parameters(instructions, &actual_parameters, parameters, state);12031204if (parameter_count == 01205|| (constructor_type->is_vector() &&1206constructor_type->vector_elements != parameter_count)1207|| (constructor_type->is_matrix() &&1208constructor_type->matrix_columns != parameter_count)) {1209_mesa_glsl_error(loc, state, "%s constructor must have %u parameters",1210constructor_type->is_vector() ? "vector" : "matrix",1211constructor_type->vector_elements);1212return ir_rvalue::error_value(ctx);1213}12141215bool all_parameters_are_constant = true;12161217/* Type cast each parameter and, if possible, fold constants. */1218foreach_in_list_safe(ir_rvalue, ir, &actual_parameters) {1219/* Apply implicit conversions (not the scalar constructor rules, see the1220* spec quote above!) and attempt to convert the parameter to a constant1221* valued expression. After doing so, track whether or not all the1222* parameters to the constructor are trivially constant valued1223* expressions.1224*/1225all_parameters_are_constant &=1226implicitly_convert_component(ir, constructor_type->base_type, state);12271228if (constructor_type->is_matrix()) {1229if (ir->type != constructor_type->column_type()) {1230_mesa_glsl_error(loc, state, "type error in matrix constructor: "1231"expected: %s, found %s",1232constructor_type->column_type()->name,1233ir->type->name);1234return ir_rvalue::error_value(ctx);1235}1236} else if (ir->type != constructor_type->get_scalar_type()) {1237_mesa_glsl_error(loc, state, "type error in vector constructor: "1238"expected: %s, found %s",1239constructor_type->get_scalar_type()->name,1240ir->type->name);1241return ir_rvalue::error_value(ctx);1242}1243}12441245if (all_parameters_are_constant)1246return new(ctx) ir_constant(constructor_type, &actual_parameters);12471248ir_variable *var = new(ctx) ir_variable(constructor_type, "vec_mat_ctor",1249ir_var_temporary);1250instructions->push_tail(var);12511252int i = 0;12531254foreach_in_list(ir_rvalue, rhs, &actual_parameters) {1255ir_instruction *assignment = NULL;12561257if (var->type->is_matrix()) {1258ir_rvalue *lhs =1259new(ctx) ir_dereference_array(var, new(ctx) ir_constant(i));1260assignment = new(ctx) ir_assignment(lhs, rhs);1261} else {1262/* use writemask rather than index for vector */1263assert(var->type->is_vector());1264assert(i < 4);1265ir_dereference *lhs = new(ctx) ir_dereference_variable(var);1266assignment = new(ctx) ir_assignment(lhs, rhs, NULL,1267(unsigned)(1 << i));1268}12691270instructions->push_tail(assignment);12711272i++;1273}12741275return new(ctx) ir_dereference_variable(var);1276}127712781279static ir_rvalue *1280process_array_constructor(exec_list *instructions,1281const glsl_type *constructor_type,1282YYLTYPE *loc, exec_list *parameters,1283struct _mesa_glsl_parse_state *state)1284{1285void *ctx = state;1286/* Array constructors come in two forms: sized and unsized. Sized array1287* constructors look like 'vec4[2](a, b)', where 'a' and 'b' are vec41288* variables. In this case the number of parameters must exactly match the1289* specified size of the array.1290*1291* Unsized array constructors look like 'vec4[](a, b)', where 'a' and 'b'1292* are vec4 variables. In this case the size of the array being constructed1293* is determined by the number of parameters.1294*1295* From page 52 (page 58 of the PDF) of the GLSL 1.50 spec:1296*1297* "There must be exactly the same number of arguments as the size of1298* the array being constructed. If no size is present in the1299* constructor, then the array is explicitly sized to the number of1300* arguments provided. The arguments are assigned in order, starting at1301* element 0, to the elements of the constructed array. Each argument1302* must be the same type as the element type of the array, or be a type1303* that can be converted to the element type of the array according to1304* Section 4.1.10 "Implicit Conversions.""1305*/1306exec_list actual_parameters;1307const unsigned parameter_count =1308process_parameters(instructions, &actual_parameters, parameters, state);1309bool is_unsized_array = constructor_type->is_unsized_array();13101311if ((parameter_count == 0) ||1312(!is_unsized_array && (constructor_type->length != parameter_count))) {1313const unsigned min_param = is_unsized_array1314? 1 : constructor_type->length;13151316_mesa_glsl_error(loc, state, "array constructor must have %s %u "1317"parameter%s",1318is_unsized_array ? "at least" : "exactly",1319min_param, (min_param <= 1) ? "" : "s");1320return ir_rvalue::error_value(ctx);1321}13221323if (is_unsized_array) {1324constructor_type =1325glsl_type::get_array_instance(constructor_type->fields.array,1326parameter_count);1327assert(constructor_type != NULL);1328assert(constructor_type->length == parameter_count);1329}13301331bool all_parameters_are_constant = true;1332const glsl_type *element_type = constructor_type->fields.array;13331334/* Type cast each parameter and, if possible, fold constants. */1335foreach_in_list_safe(ir_rvalue, ir, &actual_parameters) {1336/* Apply implicit conversions (not the scalar constructor rules, see the1337* spec quote above!) and attempt to convert the parameter to a constant1338* valued expression. After doing so, track whether or not all the1339* parameters to the constructor are trivially constant valued1340* expressions.1341*/1342all_parameters_are_constant &=1343implicitly_convert_component(ir, element_type->base_type, state);13441345if (constructor_type->fields.array->is_unsized_array()) {1346/* As the inner parameters of the constructor are created without1347* knowledge of each other we need to check to make sure unsized1348* parameters of unsized constructors all end up with the same size.1349*1350* e.g we make sure to fail for a constructor like this:1351* vec4[][] a = vec4[][](vec4[](vec4(0.0), vec4(1.0)),1352* vec4[](vec4(0.0), vec4(1.0), vec4(1.0)),1353* vec4[](vec4(0.0), vec4(1.0)));1354*/1355if (element_type->is_unsized_array()) {1356/* This is the first parameter so just get the type */1357element_type = ir->type;1358} else if (element_type != ir->type) {1359_mesa_glsl_error(loc, state, "type error in array constructor: "1360"expected: %s, found %s",1361element_type->name,1362ir->type->name);1363return ir_rvalue::error_value(ctx);1364}1365} else if (ir->type != constructor_type->fields.array) {1366_mesa_glsl_error(loc, state, "type error in array constructor: "1367"expected: %s, found %s",1368constructor_type->fields.array->name,1369ir->type->name);1370return ir_rvalue::error_value(ctx);1371} else {1372element_type = ir->type;1373}1374}13751376if (constructor_type->fields.array->is_unsized_array()) {1377constructor_type =1378glsl_type::get_array_instance(element_type,1379parameter_count);1380assert(constructor_type != NULL);1381assert(constructor_type->length == parameter_count);1382}13831384if (all_parameters_are_constant)1385return new(ctx) ir_constant(constructor_type, &actual_parameters);13861387ir_variable *var = new(ctx) ir_variable(constructor_type, "array_ctor",1388ir_var_temporary);1389instructions->push_tail(var);13901391int i = 0;1392foreach_in_list(ir_rvalue, rhs, &actual_parameters) {1393ir_rvalue *lhs = new(ctx) ir_dereference_array(var,1394new(ctx) ir_constant(i));13951396ir_instruction *assignment = new(ctx) ir_assignment(lhs, rhs);1397instructions->push_tail(assignment);13981399i++;1400}14011402return new(ctx) ir_dereference_variable(var);1403}140414051406/**1407* Determine if a list consists of a single scalar r-value1408*/1409static bool1410single_scalar_parameter(exec_list *parameters)1411{1412const ir_rvalue *const p = (ir_rvalue *) parameters->get_head_raw();1413assert(((ir_rvalue *)p)->as_rvalue() != NULL);14141415return (p->type->is_scalar() && p->next->is_tail_sentinel());1416}141714181419/**1420* Generate inline code for a vector constructor1421*1422* The generated constructor code will consist of a temporary variable1423* declaration of the same type as the constructor. A sequence of assignments1424* from constructor parameters to the temporary will follow.1425*1426* \return1427* An \c ir_dereference_variable of the temprorary generated in the constructor1428* body.1429*/1430static ir_rvalue *1431emit_inline_vector_constructor(const glsl_type *type,1432exec_list *instructions,1433exec_list *parameters,1434void *ctx)1435{1436assert(!parameters->is_empty());14371438ir_variable *var = new(ctx) ir_variable(type, "vec_ctor", ir_var_temporary);1439instructions->push_tail(var);14401441/* There are three kinds of vector constructors.1442*1443* - Construct a vector from a single scalar by replicating that scalar to1444* all components of the vector.1445*1446* - Construct a vector from at least a matrix. This case should already1447* have been taken care of in ast_function_expression::hir by breaking1448* down the matrix into a series of column vectors.1449*1450* - Construct a vector from an arbirary combination of vectors and1451* scalars. The components of the constructor parameters are assigned1452* to the vector in order until the vector is full.1453*/1454const unsigned lhs_components = type->components();1455if (single_scalar_parameter(parameters)) {1456ir_rvalue *first_param = (ir_rvalue *)parameters->get_head_raw();1457ir_rvalue *rhs = new(ctx) ir_swizzle(first_param, 0, 0, 0, 0,1458lhs_components);1459ir_dereference_variable *lhs = new(ctx) ir_dereference_variable(var);1460const unsigned mask = (1U << lhs_components) - 1;14611462assert(rhs->type == lhs->type);14631464ir_instruction *inst = new(ctx) ir_assignment(lhs, rhs, NULL, mask);1465instructions->push_tail(inst);1466} else {1467unsigned base_component = 0;1468unsigned base_lhs_component = 0;1469ir_constant_data data;1470unsigned constant_mask = 0, constant_components = 0;14711472memset(&data, 0, sizeof(data));14731474foreach_in_list(ir_rvalue, param, parameters) {1475unsigned rhs_components = param->type->components();14761477/* Do not try to assign more components to the vector than it has! */1478if ((rhs_components + base_lhs_component) > lhs_components) {1479rhs_components = lhs_components - base_lhs_component;1480}14811482const ir_constant *const c = param->as_constant();1483if (c != NULL) {1484for (unsigned i = 0; i < rhs_components; i++) {1485switch (c->type->base_type) {1486case GLSL_TYPE_UINT:1487data.u[i + base_component] = c->get_uint_component(i);1488break;1489case GLSL_TYPE_INT:1490data.i[i + base_component] = c->get_int_component(i);1491break;1492case GLSL_TYPE_FLOAT:1493data.f[i + base_component] = c->get_float_component(i);1494break;1495case GLSL_TYPE_DOUBLE:1496data.d[i + base_component] = c->get_double_component(i);1497break;1498case GLSL_TYPE_BOOL:1499data.b[i + base_component] = c->get_bool_component(i);1500break;1501case GLSL_TYPE_UINT64:1502data.u64[i + base_component] = c->get_uint64_component(i);1503break;1504case GLSL_TYPE_INT64:1505data.i64[i + base_component] = c->get_int64_component(i);1506break;1507default:1508assert(!"Should not get here.");1509break;1510}1511}15121513/* Mask of fields to be written in the assignment. */1514constant_mask |= ((1U << rhs_components) - 1) << base_lhs_component;1515constant_components += rhs_components;15161517base_component += rhs_components;1518}1519/* Advance the component index by the number of components1520* that were just assigned.1521*/1522base_lhs_component += rhs_components;1523}15241525if (constant_mask != 0) {1526ir_dereference *lhs = new(ctx) ir_dereference_variable(var);1527const glsl_type *rhs_type =1528glsl_type::get_instance(var->type->base_type,1529constant_components,15301);1531ir_rvalue *rhs = new(ctx) ir_constant(rhs_type, &data);15321533ir_instruction *inst =1534new(ctx) ir_assignment(lhs, rhs, NULL, constant_mask);1535instructions->push_tail(inst);1536}15371538base_component = 0;1539foreach_in_list(ir_rvalue, param, parameters) {1540unsigned rhs_components = param->type->components();15411542/* Do not try to assign more components to the vector than it has! */1543if ((rhs_components + base_component) > lhs_components) {1544rhs_components = lhs_components - base_component;1545}15461547/* If we do not have any components left to copy, break out of the1548* loop. This can happen when initializing a vec4 with a mat3 as the1549* mat3 would have been broken into a series of column vectors.1550*/1551if (rhs_components == 0) {1552break;1553}15541555const ir_constant *const c = param->as_constant();1556if (c == NULL) {1557/* Mask of fields to be written in the assignment. */1558const unsigned write_mask = ((1U << rhs_components) - 1)1559<< base_component;15601561ir_dereference *lhs = new(ctx) ir_dereference_variable(var);15621563/* Generate a swizzle so that LHS and RHS sizes match. */1564ir_rvalue *rhs =1565new(ctx) ir_swizzle(param, 0, 1, 2, 3, rhs_components);15661567ir_instruction *inst =1568new(ctx) ir_assignment(lhs, rhs, NULL, write_mask);1569instructions->push_tail(inst);1570}15711572/* Advance the component index by the number of components that were1573* just assigned.1574*/1575base_component += rhs_components;1576}1577}1578return new(ctx) ir_dereference_variable(var);1579}158015811582/**1583* Generate assignment of a portion of a vector to a portion of a matrix column1584*1585* \param src_base First component of the source to be used in assignment1586* \param column Column of destination to be assiged1587* \param row_base First component of the destination column to be assigned1588* \param count Number of components to be assigned1589*1590* \note1591* \c src_base + \c count must be less than or equal to the number of1592* components in the source vector.1593*/1594static ir_instruction *1595assign_to_matrix_column(ir_variable *var, unsigned column, unsigned row_base,1596ir_rvalue *src, unsigned src_base, unsigned count,1597void *mem_ctx)1598{1599ir_constant *col_idx = new(mem_ctx) ir_constant(column);1600ir_dereference *column_ref = new(mem_ctx) ir_dereference_array(var,1601col_idx);16021603assert(column_ref->type->components() >= (row_base + count));1604assert(src->type->components() >= (src_base + count));16051606/* Generate a swizzle that extracts the number of components from the source1607* that are to be assigned to the column of the matrix.1608*/1609if (count < src->type->vector_elements) {1610src = new(mem_ctx) ir_swizzle(src,1611src_base + 0, src_base + 1,1612src_base + 2, src_base + 3,1613count);1614}16151616/* Mask of fields to be written in the assignment. */1617const unsigned write_mask = ((1U << count) - 1) << row_base;16181619return new(mem_ctx) ir_assignment(column_ref, src, NULL, write_mask);1620}162116221623/**1624* Generate inline code for a matrix constructor1625*1626* The generated constructor code will consist of a temporary variable1627* declaration of the same type as the constructor. A sequence of assignments1628* from constructor parameters to the temporary will follow.1629*1630* \return1631* An \c ir_dereference_variable of the temprorary generated in the constructor1632* body.1633*/1634static ir_rvalue *1635emit_inline_matrix_constructor(const glsl_type *type,1636exec_list *instructions,1637exec_list *parameters,1638void *ctx)1639{1640assert(!parameters->is_empty());16411642ir_variable *var = new(ctx) ir_variable(type, "mat_ctor", ir_var_temporary);1643instructions->push_tail(var);16441645/* There are three kinds of matrix constructors.1646*1647* - Construct a matrix from a single scalar by replicating that scalar to1648* along the diagonal of the matrix and setting all other components to1649* zero.1650*1651* - Construct a matrix from an arbirary combination of vectors and1652* scalars. The components of the constructor parameters are assigned1653* to the matrix in column-major order until the matrix is full.1654*1655* - Construct a matrix from a single matrix. The source matrix is copied1656* to the upper left portion of the constructed matrix, and the remaining1657* elements take values from the identity matrix.1658*/1659ir_rvalue *const first_param = (ir_rvalue *) parameters->get_head_raw();1660if (single_scalar_parameter(parameters)) {1661/* Assign the scalar to the X component of a vec4, and fill the remaining1662* components with zero.1663*/1664glsl_base_type param_base_type = first_param->type->base_type;1665assert(first_param->type->is_float() || first_param->type->is_double());1666ir_variable *rhs_var =1667new(ctx) ir_variable(glsl_type::get_instance(param_base_type, 4, 1),1668"mat_ctor_vec",1669ir_var_temporary);1670instructions->push_tail(rhs_var);16711672ir_constant_data zero;1673for (unsigned i = 0; i < 4; i++)1674if (first_param->type->is_float())1675zero.f[i] = 0.0;1676else1677zero.d[i] = 0.0;16781679ir_instruction *inst =1680new(ctx) ir_assignment(new(ctx) ir_dereference_variable(rhs_var),1681new(ctx) ir_constant(rhs_var->type, &zero));1682instructions->push_tail(inst);16831684ir_dereference *const rhs_ref =1685new(ctx) ir_dereference_variable(rhs_var);16861687inst = new(ctx) ir_assignment(rhs_ref, first_param, NULL, 0x01);1688instructions->push_tail(inst);16891690/* Assign the temporary vector to each column of the destination matrix1691* with a swizzle that puts the X component on the diagonal of the1692* matrix. In some cases this may mean that the X component does not1693* get assigned into the column at all (i.e., when the matrix has more1694* columns than rows).1695*/1696static const unsigned rhs_swiz[4][4] = {1697{ 0, 1, 1, 1 },1698{ 1, 0, 1, 1 },1699{ 1, 1, 0, 1 },1700{ 1, 1, 1, 0 }1701};17021703const unsigned cols_to_init = MIN2(type->matrix_columns,1704type->vector_elements);1705for (unsigned i = 0; i < cols_to_init; i++) {1706ir_constant *const col_idx = new(ctx) ir_constant(i);1707ir_rvalue *const col_ref = new(ctx) ir_dereference_array(var,1708col_idx);17091710ir_rvalue *const rhs_ref = new(ctx) ir_dereference_variable(rhs_var);1711ir_rvalue *const rhs = new(ctx) ir_swizzle(rhs_ref, rhs_swiz[i],1712type->vector_elements);17131714inst = new(ctx) ir_assignment(col_ref, rhs);1715instructions->push_tail(inst);1716}17171718for (unsigned i = cols_to_init; i < type->matrix_columns; i++) {1719ir_constant *const col_idx = new(ctx) ir_constant(i);1720ir_rvalue *const col_ref = new(ctx) ir_dereference_array(var,1721col_idx);17221723ir_rvalue *const rhs_ref = new(ctx) ir_dereference_variable(rhs_var);1724ir_rvalue *const rhs = new(ctx) ir_swizzle(rhs_ref, 1, 1, 1, 1,1725type->vector_elements);17261727inst = new(ctx) ir_assignment(col_ref, rhs);1728instructions->push_tail(inst);1729}1730} else if (first_param->type->is_matrix()) {1731/* From page 50 (56 of the PDF) of the GLSL 1.50 spec:1732*1733* "If a matrix is constructed from a matrix, then each component1734* (column i, row j) in the result that has a corresponding1735* component (column i, row j) in the argument will be initialized1736* from there. All other components will be initialized to the1737* identity matrix. If a matrix argument is given to a matrix1738* constructor, it is an error to have any other arguments."1739*/1740assert(first_param->next->is_tail_sentinel());1741ir_rvalue *const src_matrix = first_param;17421743/* If the source matrix is smaller, pre-initialize the relavent parts of1744* the destination matrix to the identity matrix.1745*/1746if ((src_matrix->type->matrix_columns < var->type->matrix_columns) ||1747(src_matrix->type->vector_elements < var->type->vector_elements)) {17481749/* If the source matrix has fewer rows, every column of the1750* destination must be initialized. Otherwise only the columns in1751* the destination that do not exist in the source must be1752* initialized.1753*/1754unsigned col =1755(src_matrix->type->vector_elements < var->type->vector_elements)1756? 0 : src_matrix->type->matrix_columns;17571758const glsl_type *const col_type = var->type->column_type();1759for (/* empty */; col < var->type->matrix_columns; col++) {1760ir_constant_data ident;17611762if (!col_type->is_double()) {1763ident.f[0] = 0.0f;1764ident.f[1] = 0.0f;1765ident.f[2] = 0.0f;1766ident.f[3] = 0.0f;1767ident.f[col] = 1.0f;1768} else {1769ident.d[0] = 0.0;1770ident.d[1] = 0.0;1771ident.d[2] = 0.0;1772ident.d[3] = 0.0;1773ident.d[col] = 1.0;1774}17751776ir_rvalue *const rhs = new(ctx) ir_constant(col_type, &ident);17771778ir_rvalue *const lhs =1779new(ctx) ir_dereference_array(var, new(ctx) ir_constant(col));17801781ir_instruction *inst = new(ctx) ir_assignment(lhs, rhs);1782instructions->push_tail(inst);1783}1784}17851786/* Assign columns from the source matrix to the destination matrix.1787*1788* Since the parameter will be used in the RHS of multiple assignments,1789* generate a temporary and copy the paramter there.1790*/1791ir_variable *const rhs_var =1792new(ctx) ir_variable(first_param->type, "mat_ctor_mat",1793ir_var_temporary);1794instructions->push_tail(rhs_var);17951796ir_dereference *const rhs_var_ref =1797new(ctx) ir_dereference_variable(rhs_var);1798ir_instruction *const inst =1799new(ctx) ir_assignment(rhs_var_ref, first_param);1800instructions->push_tail(inst);18011802const unsigned last_row = MIN2(src_matrix->type->vector_elements,1803var->type->vector_elements);1804const unsigned last_col = MIN2(src_matrix->type->matrix_columns,1805var->type->matrix_columns);18061807unsigned swiz[4] = { 0, 0, 0, 0 };1808for (unsigned i = 1; i < last_row; i++)1809swiz[i] = i;18101811const unsigned write_mask = (1U << last_row) - 1;18121813for (unsigned i = 0; i < last_col; i++) {1814ir_dereference *const lhs =1815new(ctx) ir_dereference_array(var, new(ctx) ir_constant(i));1816ir_rvalue *const rhs_col =1817new(ctx) ir_dereference_array(rhs_var, new(ctx) ir_constant(i));18181819/* If one matrix has columns that are smaller than the columns of the1820* other matrix, wrap the column access of the larger with a swizzle1821* so that the LHS and RHS of the assignment have the same size (and1822* therefore have the same type).1823*1824* It would be perfectly valid to unconditionally generate the1825* swizzles, this this will typically result in a more compact IR1826* tree.1827*/1828ir_rvalue *rhs;1829if (lhs->type->vector_elements != rhs_col->type->vector_elements) {1830rhs = new(ctx) ir_swizzle(rhs_col, swiz, last_row);1831} else {1832rhs = rhs_col;1833}18341835ir_instruction *inst =1836new(ctx) ir_assignment(lhs, rhs, NULL, write_mask);1837instructions->push_tail(inst);1838}1839} else {1840const unsigned cols = type->matrix_columns;1841const unsigned rows = type->vector_elements;1842unsigned remaining_slots = rows * cols;1843unsigned col_idx = 0;1844unsigned row_idx = 0;18451846foreach_in_list(ir_rvalue, rhs, parameters) {1847unsigned rhs_components = rhs->type->components();1848unsigned rhs_base = 0;18491850if (remaining_slots == 0)1851break;18521853/* Since the parameter might be used in the RHS of two assignments,1854* generate a temporary and copy the paramter there.1855*/1856ir_variable *rhs_var =1857new(ctx) ir_variable(rhs->type, "mat_ctor_vec", ir_var_temporary);1858instructions->push_tail(rhs_var);18591860ir_dereference *rhs_var_ref =1861new(ctx) ir_dereference_variable(rhs_var);1862ir_instruction *inst = new(ctx) ir_assignment(rhs_var_ref, rhs);1863instructions->push_tail(inst);18641865do {1866/* Assign the current parameter to as many components of the matrix1867* as it will fill.1868*1869* NOTE: A single vector parameter can span two matrix columns. A1870* single vec4, for example, can completely fill a mat2.1871*/1872unsigned count = MIN2(rows - row_idx,1873rhs_components - rhs_base);18741875rhs_var_ref = new(ctx) ir_dereference_variable(rhs_var);1876ir_instruction *inst = assign_to_matrix_column(var, col_idx,1877row_idx,1878rhs_var_ref,1879rhs_base,1880count, ctx);1881instructions->push_tail(inst);1882rhs_base += count;1883row_idx += count;1884remaining_slots -= count;18851886/* Sometimes, there is still data left in the parameters and1887* components left to be set in the destination but in other1888* column.1889*/1890if (row_idx >= rows) {1891row_idx = 0;1892col_idx++;1893}1894} while(remaining_slots > 0 && rhs_base < rhs_components);1895}1896}18971898return new(ctx) ir_dereference_variable(var);1899}190019011902static ir_rvalue *1903emit_inline_record_constructor(const glsl_type *type,1904exec_list *instructions,1905exec_list *parameters,1906void *mem_ctx)1907{1908ir_variable *const var =1909new(mem_ctx) ir_variable(type, "record_ctor", ir_var_temporary);1910ir_dereference_variable *const d =1911new(mem_ctx) ir_dereference_variable(var);19121913instructions->push_tail(var);19141915exec_node *node = parameters->get_head_raw();1916for (unsigned i = 0; i < type->length; i++) {1917assert(!node->is_tail_sentinel());19181919ir_dereference *const lhs =1920new(mem_ctx) ir_dereference_record(d->clone(mem_ctx, NULL),1921type->fields.structure[i].name);19221923ir_rvalue *const rhs = ((ir_instruction *) node)->as_rvalue();1924assert(rhs != NULL);19251926ir_instruction *const assign = new(mem_ctx) ir_assignment(lhs, rhs);19271928instructions->push_tail(assign);1929node = node->next;1930}19311932return d;1933}193419351936static ir_rvalue *1937process_record_constructor(exec_list *instructions,1938const glsl_type *constructor_type,1939YYLTYPE *loc, exec_list *parameters,1940struct _mesa_glsl_parse_state *state)1941{1942void *ctx = state;1943/* From page 32 (page 38 of the PDF) of the GLSL 1.20 spec:1944*1945* "The arguments to the constructor will be used to set the structure's1946* fields, in order, using one argument per field. Each argument must1947* be the same type as the field it sets, or be a type that can be1948* converted to the field's type according to Section 4.1.10 “Implicit1949* Conversions.”"1950*1951* From page 35 (page 41 of the PDF) of the GLSL 4.20 spec:1952*1953* "In all cases, the innermost initializer (i.e., not a list of1954* initializers enclosed in curly braces) applied to an object must1955* have the same type as the object being initialized or be a type that1956* can be converted to the object's type according to section 4.1.101957* "Implicit Conversions". In the latter case, an implicit conversion1958* will be done on the initializer before the assignment is done."1959*/1960exec_list actual_parameters;19611962const unsigned parameter_count =1963process_parameters(instructions, &actual_parameters, parameters,1964state);19651966if (parameter_count != constructor_type->length) {1967_mesa_glsl_error(loc, state,1968"%s parameters in constructor for `%s'",1969parameter_count > constructor_type->length1970? "too many": "insufficient",1971constructor_type->name);1972return ir_rvalue::error_value(ctx);1973}19741975bool all_parameters_are_constant = true;19761977int i = 0;1978/* Type cast each parameter and, if possible, fold constants. */1979foreach_in_list_safe(ir_rvalue, ir, &actual_parameters) {19801981const glsl_struct_field *struct_field =1982&constructor_type->fields.structure[i];19831984/* Apply implicit conversions (not the scalar constructor rules, see the1985* spec quote above!) and attempt to convert the parameter to a constant1986* valued expression. After doing so, track whether or not all the1987* parameters to the constructor are trivially constant valued1988* expressions.1989*/1990all_parameters_are_constant &=1991implicitly_convert_component(ir, struct_field->type->base_type,1992state);19931994if (ir->type != struct_field->type) {1995_mesa_glsl_error(loc, state,1996"parameter type mismatch in constructor for `%s.%s' "1997"(%s vs %s)",1998constructor_type->name,1999struct_field->name,2000ir->type->name,2001struct_field->type->name);2002return ir_rvalue::error_value(ctx);2003}20042005i++;2006}20072008if (all_parameters_are_constant) {2009return new(ctx) ir_constant(constructor_type, &actual_parameters);2010} else {2011return emit_inline_record_constructor(constructor_type, instructions,2012&actual_parameters, state);2013}2014}20152016ir_rvalue *2017ast_function_expression::handle_method(exec_list *instructions,2018struct _mesa_glsl_parse_state *state)2019{2020const ast_expression *field = subexpressions[0];2021ir_rvalue *op;2022ir_rvalue *result;2023void *ctx = state;2024/* Handle "method calls" in GLSL 1.20 - namely, array.length() */2025YYLTYPE loc = get_location();2026state->check_version(120, 300, &loc, "methods not supported");20272028const char *method;2029method = field->primary_expression.identifier;20302031/* This would prevent to raise "uninitialized variable" warnings when2032* calling array.length.2033*/2034field->subexpressions[0]->set_is_lhs(true);2035op = field->subexpressions[0]->hir(instructions, state);2036if (strcmp(method, "length") == 0) {2037if (!this->expressions.is_empty()) {2038_mesa_glsl_error(&loc, state, "length method takes no arguments");2039goto fail;2040}20412042if (op->type->is_array()) {2043if (op->type->is_unsized_array()) {2044if (!state->has_shader_storage_buffer_objects()) {2045_mesa_glsl_error(&loc, state,2046"length called on unsized array"2047" only available with"2048" ARB_shader_storage_buffer_object");2049goto fail;2050} else if (op->variable_referenced()->is_in_shader_storage_block()) {2051/* Calculate length of an unsized array in run-time */2052result = new(ctx)2053ir_expression(ir_unop_ssbo_unsized_array_length, op);2054} else {2055/* When actual size is known at link-time, this will be2056* replaced with a constant expression.2057*/2058result = new (ctx)2059ir_expression(ir_unop_implicitly_sized_array_length, op);2060}2061} else {2062result = new(ctx) ir_constant(op->type->array_size());2063}2064} else if (op->type->is_vector()) {2065if (state->has_420pack()) {2066/* .length() returns int. */2067result = new(ctx) ir_constant((int) op->type->vector_elements);2068} else {2069_mesa_glsl_error(&loc, state, "length method on matrix only"2070" available with ARB_shading_language_420pack");2071goto fail;2072}2073} else if (op->type->is_matrix()) {2074if (state->has_420pack()) {2075/* .length() returns int. */2076result = new(ctx) ir_constant((int) op->type->matrix_columns);2077} else {2078_mesa_glsl_error(&loc, state, "length method on matrix only"2079" available with ARB_shading_language_420pack");2080goto fail;2081}2082} else {2083_mesa_glsl_error(&loc, state, "length called on scalar.");2084goto fail;2085}2086} else {2087_mesa_glsl_error(&loc, state, "unknown method: `%s'", method);2088goto fail;2089}2090return result;2091fail:2092return ir_rvalue::error_value(ctx);2093}20942095static inline bool is_valid_constructor(const glsl_type *type,2096struct _mesa_glsl_parse_state *state)2097{2098return type->is_numeric() || type->is_boolean() ||2099(state->has_bindless() && (type->is_sampler() || type->is_image()));2100}21012102ir_rvalue *2103ast_function_expression::hir(exec_list *instructions,2104struct _mesa_glsl_parse_state *state)2105{2106void *ctx = state;2107/* There are three sorts of function calls.2108*2109* 1. constructors - The first subexpression is an ast_type_specifier.2110* 2. methods - Only the .length() method of array types.2111* 3. functions - Calls to regular old functions.2112*2113*/2114if (is_constructor()) {2115const ast_type_specifier *type =2116(ast_type_specifier *) subexpressions[0];2117YYLTYPE loc = type->get_location();2118const char *name;21192120const glsl_type *const constructor_type = type->glsl_type(& name, state);21212122/* constructor_type can be NULL if a variable with the same name as the2123* structure has come into scope.2124*/2125if (constructor_type == NULL) {2126_mesa_glsl_error(& loc, state, "unknown type `%s' (structure name "2127"may be shadowed by a variable with the same name)",2128type->type_name);2129return ir_rvalue::error_value(ctx);2130}213121322133/* Constructors for opaque types are illegal.2134*2135* From section 4.1.7 of the ARB_bindless_texture spec:2136*2137* "Samplers are represented using 64-bit integer handles, and may be "2138* converted to and from 64-bit integers using constructors."2139*2140* From section 4.1.X of the ARB_bindless_texture spec:2141*2142* "Images are represented using 64-bit integer handles, and may be2143* converted to and from 64-bit integers using constructors."2144*/2145if (constructor_type->contains_atomic() ||2146(!state->has_bindless() && constructor_type->contains_opaque())) {2147_mesa_glsl_error(& loc, state, "cannot construct %s type `%s'",2148state->has_bindless() ? "atomic" : "opaque",2149constructor_type->name);2150return ir_rvalue::error_value(ctx);2151}21522153if (constructor_type->is_subroutine()) {2154_mesa_glsl_error(& loc, state,2155"subroutine name cannot be a constructor `%s'",2156constructor_type->name);2157return ir_rvalue::error_value(ctx);2158}21592160if (constructor_type->is_array()) {2161if (!state->check_version(state->allow_glsl_120_subset_in_110 ? 110 : 120,2162300, &loc, "array constructors forbidden")) {2163return ir_rvalue::error_value(ctx);2164}21652166return process_array_constructor(instructions, constructor_type,2167& loc, &this->expressions, state);2168}216921702171/* There are two kinds of constructor calls. Constructors for arrays and2172* structures must have the exact number of arguments with matching types2173* in the correct order. These constructors follow essentially the same2174* type matching rules as functions.2175*2176* Constructors for built-in language types, such as mat4 and vec2, are2177* free form. The only requirements are that the parameters must provide2178* enough values of the correct scalar type and that no arguments are2179* given past the last used argument.2180*2181* When using the C-style initializer syntax from GLSL 4.20, constructors2182* must have the exact number of arguments with matching types in the2183* correct order.2184*/2185if (constructor_type->is_struct()) {2186return process_record_constructor(instructions, constructor_type,2187&loc, &this->expressions,2188state);2189}21902191if (!is_valid_constructor(constructor_type, state))2192return ir_rvalue::error_value(ctx);21932194/* Total number of components of the type being constructed. */2195const unsigned type_components = constructor_type->components();21962197/* Number of components from parameters that have actually been2198* consumed. This is used to perform several kinds of error checking.2199*/2200unsigned components_used = 0;22012202unsigned matrix_parameters = 0;2203unsigned nonmatrix_parameters = 0;2204exec_list actual_parameters;22052206foreach_list_typed(ast_node, ast, link, &this->expressions) {2207ir_rvalue *result = ast->hir(instructions, state);22082209/* From page 50 (page 56 of the PDF) of the GLSL 1.50 spec:2210*2211* "It is an error to provide extra arguments beyond this2212* last used argument."2213*/2214if (components_used >= type_components) {2215_mesa_glsl_error(& loc, state, "too many parameters to `%s' "2216"constructor",2217constructor_type->name);2218return ir_rvalue::error_value(ctx);2219}22202221if (!is_valid_constructor(result->type, state)) {2222_mesa_glsl_error(& loc, state, "cannot construct `%s' from a "2223"non-numeric data type",2224constructor_type->name);2225return ir_rvalue::error_value(ctx);2226}22272228/* Count the number of matrix and nonmatrix parameters. This2229* is used below to enforce some of the constructor rules.2230*/2231if (result->type->is_matrix())2232matrix_parameters++;2233else2234nonmatrix_parameters++;22352236actual_parameters.push_tail(result);2237components_used += result->type->components();2238}22392240/* From page 28 (page 34 of the PDF) of the GLSL 1.10 spec:2241*2242* "It is an error to construct matrices from other matrices. This2243* is reserved for future use."2244*/2245if (matrix_parameters > 02246&& constructor_type->is_matrix()2247&& !state->check_version(120, 100, &loc,2248"cannot construct `%s' from a matrix",2249constructor_type->name)) {2250return ir_rvalue::error_value(ctx);2251}22522253/* From page 50 (page 56 of the PDF) of the GLSL 1.50 spec:2254*2255* "If a matrix argument is given to a matrix constructor, it is2256* an error to have any other arguments."2257*/2258if ((matrix_parameters > 0)2259&& ((matrix_parameters + nonmatrix_parameters) > 1)2260&& constructor_type->is_matrix()) {2261_mesa_glsl_error(& loc, state, "for matrix `%s' constructor, "2262"matrix must be only parameter",2263constructor_type->name);2264return ir_rvalue::error_value(ctx);2265}22662267/* From page 28 (page 34 of the PDF) of the GLSL 1.10 spec:2268*2269* "In these cases, there must be enough components provided in the2270* arguments to provide an initializer for every component in the2271* constructed value."2272*/2273if (components_used < type_components && components_used != 12274&& matrix_parameters == 0) {2275_mesa_glsl_error(& loc, state, "too few components to construct "2276"`%s'",2277constructor_type->name);2278return ir_rvalue::error_value(ctx);2279}22802281/* Matrices can never be consumed as is by any constructor but matrix2282* constructors. If the constructor type is not matrix, always break the2283* matrix up into a series of column vectors.2284*/2285if (!constructor_type->is_matrix()) {2286foreach_in_list_safe(ir_rvalue, matrix, &actual_parameters) {2287if (!matrix->type->is_matrix())2288continue;22892290/* Create a temporary containing the matrix. */2291ir_variable *var = new(ctx) ir_variable(matrix->type, "matrix_tmp",2292ir_var_temporary);2293instructions->push_tail(var);2294instructions->push_tail(2295new(ctx) ir_assignment(new(ctx) ir_dereference_variable(var),2296matrix));2297var->constant_value = matrix->constant_expression_value(ctx);22982299/* Replace the matrix with dereferences of its columns. */2300for (int i = 0; i < matrix->type->matrix_columns; i++) {2301matrix->insert_before(2302new (ctx) ir_dereference_array(var,2303new(ctx) ir_constant(i)));2304}2305matrix->remove();2306}2307}23082309bool all_parameters_are_constant = true;23102311/* Type cast each parameter and, if possible, fold constants.*/2312foreach_in_list_safe(ir_rvalue, ir, &actual_parameters) {2313const glsl_type *desired_type;23142315/* From section 5.4.1 of the ARB_bindless_texture spec:2316*2317* "In the following four constructors, the low 32 bits of the sampler2318* type correspond to the .x component of the uvec2 and the high 322319* bits correspond to the .y component."2320*2321* uvec2(any sampler type) // Converts a sampler type to a2322* // pair of 32-bit unsigned integers2323* any sampler type(uvec2) // Converts a pair of 32-bit unsigned integers to2324* // a sampler type2325* uvec2(any image type) // Converts an image type to a2326* // pair of 32-bit unsigned integers2327* any image type(uvec2) // Converts a pair of 32-bit unsigned integers to2328* // an image type2329*/2330if (ir->type->is_sampler() || ir->type->is_image()) {2331/* Convert a sampler/image type to a pair of 32-bit unsigned2332* integers as defined by ARB_bindless_texture.2333*/2334if (constructor_type != glsl_type::uvec2_type) {2335_mesa_glsl_error(&loc, state, "sampler and image types can only "2336"be converted to a pair of 32-bit unsigned "2337"integers");2338}2339desired_type = glsl_type::uvec2_type;2340} else if (constructor_type->is_sampler() ||2341constructor_type->is_image()) {2342/* Convert a pair of 32-bit unsigned integers to a sampler or image2343* type as defined by ARB_bindless_texture.2344*/2345if (ir->type != glsl_type::uvec2_type) {2346_mesa_glsl_error(&loc, state, "sampler and image types can only "2347"be converted from a pair of 32-bit unsigned "2348"integers");2349}2350desired_type = constructor_type;2351} else {2352desired_type =2353glsl_type::get_instance(constructor_type->base_type,2354ir->type->vector_elements,2355ir->type->matrix_columns);2356}23572358ir_rvalue *result = convert_component(ir, desired_type);23592360/* Attempt to convert the parameter to a constant valued expression.2361* After doing so, track whether or not all the parameters to the2362* constructor are trivially constant valued expressions.2363*/2364ir_rvalue *const constant = result->constant_expression_value(ctx);23652366if (constant != NULL)2367result = constant;2368else2369all_parameters_are_constant = false;23702371if (result != ir) {2372ir->replace_with(result);2373}2374}23752376/* If all of the parameters are trivially constant, create a2377* constant representing the complete collection of parameters.2378*/2379if (all_parameters_are_constant) {2380return new(ctx) ir_constant(constructor_type, &actual_parameters);2381} else if (constructor_type->is_scalar()) {2382return dereference_component((ir_rvalue *)2383actual_parameters.get_head_raw(),23840);2385} else if (constructor_type->is_vector()) {2386return emit_inline_vector_constructor(constructor_type,2387instructions,2388&actual_parameters,2389ctx);2390} else {2391assert(constructor_type->is_matrix());2392return emit_inline_matrix_constructor(constructor_type,2393instructions,2394&actual_parameters,2395ctx);2396}2397} else if (subexpressions[0]->oper == ast_field_selection) {2398return handle_method(instructions, state);2399} else {2400const ast_expression *id = subexpressions[0];2401const char *func_name = NULL;2402YYLTYPE loc = get_location();2403exec_list actual_parameters;2404ir_variable *sub_var = NULL;2405ir_rvalue *array_idx = NULL;24062407process_parameters(instructions, &actual_parameters, &this->expressions,2408state);24092410if (id->oper == ast_array_index) {2411array_idx = generate_array_index(ctx, instructions, state, loc,2412id->subexpressions[0],2413id->subexpressions[1], &func_name,2414&actual_parameters);2415} else if (id->oper == ast_identifier) {2416func_name = id->primary_expression.identifier;2417} else {2418_mesa_glsl_error(&loc, state, "function name is not an identifier");2419}24202421/* an error was emitted earlier */2422if (!func_name)2423return ir_rvalue::error_value(ctx);24242425ir_function_signature *sig =2426match_function_by_name(func_name, &actual_parameters, state);24272428ir_rvalue *value = NULL;2429if (sig == NULL) {2430sig = match_subroutine_by_name(func_name, &actual_parameters,2431state, &sub_var);2432}24332434if (sig == NULL) {2435no_matching_function_error(func_name, &loc,2436&actual_parameters, state);2437value = ir_rvalue::error_value(ctx);2438} else if (!verify_parameter_modes(state, sig,2439actual_parameters,2440this->expressions)) {2441/* an error has already been emitted */2442value = ir_rvalue::error_value(ctx);2443} else if (sig->is_builtin() && strcmp(func_name, "ftransform") == 0) {2444/* ftransform refers to global variables, and we don't have any code2445* for remapping the variable references in the built-in shader.2446*/2447ir_variable *mvp =2448state->symbols->get_variable("gl_ModelViewProjectionMatrix");2449ir_variable *vtx = state->symbols->get_variable("gl_Vertex");2450value = new(ctx) ir_expression(ir_binop_mul, glsl_type::vec4_type,2451new(ctx) ir_dereference_variable(mvp),2452new(ctx) ir_dereference_variable(vtx));2453} else {2454bool is_begin_interlock = false;2455bool is_end_interlock = false;2456if (sig->is_builtin() &&2457state->stage == MESA_SHADER_FRAGMENT &&2458state->ARB_fragment_shader_interlock_enable) {2459is_begin_interlock = strcmp(func_name, "beginInvocationInterlockARB") == 0;2460is_end_interlock = strcmp(func_name, "endInvocationInterlockARB") == 0;2461}24622463if (sig->is_builtin() &&2464((state->stage == MESA_SHADER_TESS_CTRL &&2465strcmp(func_name, "barrier") == 0) ||2466is_begin_interlock || is_end_interlock)) {2467if (state->current_function == NULL ||2468strcmp(state->current_function->function_name(), "main") != 0) {2469_mesa_glsl_error(&loc, state,2470"%s() may only be used in main()", func_name);2471}24722473if (state->found_return) {2474_mesa_glsl_error(&loc, state,2475"%s() may not be used after return", func_name);2476}24772478if (instructions != &state->current_function->body) {2479_mesa_glsl_error(&loc, state,2480"%s() may not be used in control flow", func_name);2481}2482}24832484/* There can be only one begin/end interlock pair in the function. */2485if (is_begin_interlock) {2486if (state->found_begin_interlock)2487_mesa_glsl_error(&loc, state,2488"beginInvocationInterlockARB may not be used twice");2489state->found_begin_interlock = true;2490} else if (is_end_interlock) {2491if (!state->found_begin_interlock)2492_mesa_glsl_error(&loc, state,2493"endInvocationInterlockARB may not be used "2494"before beginInvocationInterlockARB");2495if (state->found_end_interlock)2496_mesa_glsl_error(&loc, state,2497"endInvocationInterlockARB may not be used twice");2498state->found_end_interlock = true;2499}25002501value = generate_call(instructions, sig, &actual_parameters, sub_var,2502array_idx, state);2503if (!value) {2504ir_variable *const tmp = new(ctx) ir_variable(glsl_type::void_type,2505"void_var",2506ir_var_temporary);2507instructions->push_tail(tmp);2508value = new(ctx) ir_dereference_variable(tmp);2509}2510}25112512return value;2513}25142515unreachable("not reached");2516}25172518bool2519ast_function_expression::has_sequence_subexpression() const2520{2521foreach_list_typed(const ast_node, ast, link, &this->expressions) {2522if (ast->has_sequence_subexpression())2523return true;2524}25252526return false;2527}25282529ir_rvalue *2530ast_aggregate_initializer::hir(exec_list *instructions,2531struct _mesa_glsl_parse_state *state)2532{2533void *ctx = state;2534YYLTYPE loc = this->get_location();25352536if (!this->constructor_type) {2537_mesa_glsl_error(&loc, state, "type of C-style initializer unknown");2538return ir_rvalue::error_value(ctx);2539}2540const glsl_type *const constructor_type = this->constructor_type;25412542if (!state->has_420pack()) {2543_mesa_glsl_error(&loc, state, "C-style initialization requires the "2544"GL_ARB_shading_language_420pack extension");2545return ir_rvalue::error_value(ctx);2546}25472548if (constructor_type->is_array()) {2549return process_array_constructor(instructions, constructor_type, &loc,2550&this->expressions, state);2551}25522553if (constructor_type->is_struct()) {2554return process_record_constructor(instructions, constructor_type, &loc,2555&this->expressions, state);2556}25572558return process_vec_mat_constructor(instructions, constructor_type, &loc,2559&this->expressions, state);2560}256125622563