Ian Romanick | a87ac25 | 2010-02-22 13:19:34 -0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright © 2010 Intel Corporation |
| 3 | * |
| 4 | * Permission is hereby granted, free of charge, to any person obtaining a |
| 5 | * copy of this software and associated documentation files (the "Software"), |
| 6 | * to deal in the Software without restriction, including without limitation |
| 7 | * the rights to use, copy, modify, merge, publish, distribute, sublicense, |
| 8 | * and/or sell copies of the Software, and to permit persons to whom the |
| 9 | * Software is furnished to do so, subject to the following conditions: |
| 10 | * |
| 11 | * The above copyright notice and this permission notice (including the next |
| 12 | * paragraph) shall be included in all copies or substantial portions of the |
| 13 | * Software. |
| 14 | * |
| 15 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 16 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 17 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL |
| 18 | * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 19 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING |
| 20 | * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER |
| 21 | * DEALINGS IN THE SOFTWARE. |
| 22 | */ |
| 23 | |
| 24 | /** |
| 25 | * \file ast_to_hir.c |
| 26 | * Convert abstract syntax to to high-level intermediate reprensentation (HIR). |
| 27 | * |
| 28 | * During the conversion to HIR, the majority of the symantic checking is |
| 29 | * preformed on the program. This includes: |
| 30 | * |
| 31 | * * Symbol table management |
| 32 | * * Type checking |
| 33 | * * Function binding |
| 34 | * |
| 35 | * The majority of this work could be done during parsing, and the parser could |
| 36 | * probably generate HIR directly. However, this results in frequent changes |
| 37 | * to the parser code. Since we do not assume that every system this complier |
| 38 | * is built on will have Flex and Bison installed, we have to store the code |
| 39 | * generated by these tools in our version control system. In other parts of |
| 40 | * the system we've seen problems where a parser was changed but the generated |
| 41 | * code was not committed, merge conflicts where created because two developers |
| 42 | * had slightly different versions of Bison installed, etc. |
| 43 | * |
| 44 | * I have also noticed that running Bison generated parsers in GDB is very |
| 45 | * irritating. When you get a segfault on '$$ = $1->foo', you can't very |
| 46 | * well 'print $1' in GDB. |
| 47 | * |
| 48 | * As a result, my preference is to put as little C code as possible in the |
| 49 | * parser (and lexer) sources. |
| 50 | */ |
Eric Anholt | ac95f2f | 2010-06-22 10:38:52 -0700 | [diff] [blame] | 51 | |
Ian Romanick | 8bde4ce | 2010-03-19 11:57:24 -0700 | [diff] [blame] | 52 | #include "glsl_symbol_table.h" |
Ian Romanick | a87ac25 | 2010-02-22 13:19:34 -0800 | [diff] [blame] | 53 | #include "glsl_parser_extras.h" |
| 54 | #include "ast.h" |
Emil Velikov | 24f984f | 2016-01-18 11:35:29 +0200 | [diff] [blame] | 55 | #include "compiler/glsl_types.h" |
Thomas Helland | 9f188be | 2016-08-16 22:10:21 +0200 | [diff] [blame] | 56 | #include "util/hash_table.h" |
Timothy Arceri | 598790e | 2016-03-10 11:51:48 +1100 | [diff] [blame] | 57 | #include "main/macros.h" |
Dave Airlie | 65ac360 | 2015-06-01 10:55:47 +1000 | [diff] [blame] | 58 | #include "main/shaderobj.h" |
Ian Romanick | a87ac25 | 2010-02-22 13:19:34 -0800 | [diff] [blame] | 59 | #include "ir.h" |
Eric Anholt | e9822f7 | 2014-03-05 17:05:54 -0800 | [diff] [blame] | 60 | #include "ir_builder.h" |
| 61 | |
| 62 | using namespace ir_builder; |
Ian Romanick | a87ac25 | 2010-02-22 13:19:34 -0800 | [diff] [blame] | 63 | |
Eric Anholt | 4b2a4cb | 2012-03-29 17:29:20 -0700 | [diff] [blame] | 64 | static void |
| 65 | detect_conflicting_assignments(struct _mesa_glsl_parse_state *state, |
Timothy Arceri | 222f66a | 2016-09-28 16:04:08 +1000 | [diff] [blame] | 66 | exec_list *instructions); |
Paul Berry | 719bf30 | 2013-10-15 15:13:59 -0700 | [diff] [blame] | 67 | static void |
| 68 | remove_per_vertex_blocks(exec_list *instructions, |
| 69 | _mesa_glsl_parse_state *state, ir_variable_mode mode); |
| 70 | |
Iago Toral Quiroga | 0f18945 | 2015-04-28 14:25:56 +0200 | [diff] [blame] | 71 | /** |
| 72 | * Visitor class that finds the first instance of any write-only variable that |
| 73 | * is ever read, if any |
| 74 | */ |
| 75 | class read_from_write_only_variable_visitor : public ir_hierarchical_visitor |
| 76 | { |
| 77 | public: |
| 78 | read_from_write_only_variable_visitor() : found(NULL) |
| 79 | { |
| 80 | } |
| 81 | |
| 82 | virtual ir_visitor_status visit(ir_dereference_variable *ir) |
| 83 | { |
| 84 | if (this->in_assignee) |
| 85 | return visit_continue; |
| 86 | |
| 87 | ir_variable *var = ir->variable_referenced(); |
| 88 | /* We can have image_write_only set on both images and buffer variables, |
| 89 | * but in the former there is a distinction between reads from |
| 90 | * the variable itself (write_only) and from the memory they point to |
| 91 | * (image_write_only), while in the case of buffer variables there is |
| 92 | * no such distinction, that is why this check here is limited to |
| 93 | * buffer variables alone. |
| 94 | */ |
| 95 | if (!var || var->data.mode != ir_var_shader_storage) |
| 96 | return visit_continue; |
| 97 | |
| 98 | if (var->data.image_write_only) { |
| 99 | found = var; |
| 100 | return visit_stop; |
| 101 | } |
| 102 | |
| 103 | return visit_continue; |
| 104 | } |
| 105 | |
| 106 | ir_variable *get_variable() { |
| 107 | return found; |
| 108 | } |
| 109 | |
Kenneth Graunke | c034dbe | 2016-01-11 14:51:38 -0800 | [diff] [blame] | 110 | virtual ir_visitor_status visit_enter(ir_expression *ir) |
| 111 | { |
| 112 | /* .length() doesn't actually read anything */ |
| 113 | if (ir->operation == ir_unop_ssbo_unsized_array_length) |
| 114 | return visit_continue_with_parent; |
| 115 | |
| 116 | return visit_continue; |
| 117 | } |
| 118 | |
Iago Toral Quiroga | 0f18945 | 2015-04-28 14:25:56 +0200 | [diff] [blame] | 119 | private: |
| 120 | ir_variable *found; |
| 121 | }; |
Eric Anholt | 4b2a4cb | 2012-03-29 17:29:20 -0700 | [diff] [blame] | 122 | |
Ian Romanick | d949a9a | 2010-03-10 09:55:22 -0800 | [diff] [blame] | 123 | void |
| 124 | _mesa_ast_to_hir(exec_list *instructions, struct _mesa_glsl_parse_state *state) |
| 125 | { |
Ian Romanick | adfb0cd | 2010-03-10 10:43:16 -0800 | [diff] [blame] | 126 | _mesa_glsl_initialize_variables(instructions, state); |
| 127 | |
Paul Berry | 53e572f | 2012-08-01 17:44:02 -0700 | [diff] [blame] | 128 | state->symbols->separate_function_namespace = state->language_version == 110; |
Kenneth Graunke | 814c89a | 2010-09-05 00:31:28 -0700 | [diff] [blame] | 129 | |
Ian Romanick | 41ec6a4 | 2010-03-19 17:08:05 -0700 | [diff] [blame] | 130 | state->current_function = NULL; |
| 131 | |
Paul Berry | 0d81b0e | 2011-07-29 15:28:52 -0700 | [diff] [blame] | 132 | state->toplevel_ir = instructions; |
| 133 | |
Eric Anholt | 624b7ba | 2013-06-12 14:03:49 -0700 | [diff] [blame] | 134 | state->gs_input_prim_type_specified = false; |
Fabian Bieler | 497eb29 | 2014-03-20 22:44:43 +0100 | [diff] [blame] | 135 | state->tcs_output_vertices_specified = false; |
Paul Berry | 0fa74e8 | 2014-01-06 09:09:31 -0800 | [diff] [blame] | 136 | state->cs_input_local_size_specified = false; |
Eric Anholt | 624b7ba | 2013-06-12 14:03:49 -0700 | [diff] [blame] | 137 | |
Kenneth Graunke | a044285 | 2010-08-23 14:52:06 -0700 | [diff] [blame] | 138 | /* Section 4.2 of the GLSL 1.20 specification states: |
| 139 | * "The built-in functions are scoped in a scope outside the global scope |
| 140 | * users declare global variables in. That is, a shader's global scope, |
| 141 | * available for user-defined functions and global variables, is nested |
| 142 | * inside the scope containing the built-in functions." |
| 143 | * |
| 144 | * Since built-in functions like ftransform() access built-in variables, |
| 145 | * it follows that those must be in the outer scope as well. |
| 146 | * |
| 147 | * We push scope here to create this nesting effect...but don't pop. |
| 148 | * This way, a shader's globals are still in the symbol table for use |
| 149 | * by the linker. |
| 150 | */ |
| 151 | state->symbols->push_scope(); |
| 152 | |
Ian Romanick | 2b97dc6 | 2010-05-10 17:42:05 -0700 | [diff] [blame] | 153 | foreach_list_typed (ast_node, ast, link, & state->translation_unit) |
Ian Romanick | 304ea90 | 2010-05-10 11:17:53 -0700 | [diff] [blame] | 154 | ast->hir(instructions, state); |
Ian Romanick | 02c5ae1 | 2011-07-11 10:46:01 -0700 | [diff] [blame] | 155 | |
| 156 | detect_recursion_unlinked(state, instructions); |
Eric Anholt | 4b2a4cb | 2012-03-29 17:29:20 -0700 | [diff] [blame] | 157 | detect_conflicting_assignments(state, instructions); |
Paul Berry | 0d81b0e | 2011-07-29 15:28:52 -0700 | [diff] [blame] | 158 | |
| 159 | state->toplevel_ir = NULL; |
Ian Romanick | c170c90 | 2013-06-07 17:05:22 -0700 | [diff] [blame] | 160 | |
| 161 | /* Move all of the variable declarations to the front of the IR list, and |
| 162 | * reverse the order. This has the (intended!) side effect that vertex |
| 163 | * shader inputs and fragment shader outputs will appear in the IR in the |
| 164 | * same order that they appeared in the shader code. This results in the |
| 165 | * locations being assigned in the declared order. Many (arguably buggy) |
| 166 | * applications depend on this behavior, and it matches what nearly all |
| 167 | * other drivers do. |
| 168 | */ |
Matt Turner | c6a16f6 | 2014-06-24 21:58:35 -0700 | [diff] [blame] | 169 | foreach_in_list_safe(ir_instruction, node, instructions) { |
| 170 | ir_variable *const var = node->as_variable(); |
Ian Romanick | c170c90 | 2013-06-07 17:05:22 -0700 | [diff] [blame] | 171 | |
| 172 | if (var == NULL) |
| 173 | continue; |
| 174 | |
| 175 | var->remove(); |
| 176 | instructions->push_head(var); |
| 177 | } |
Paul Berry | 719bf30 | 2013-10-15 15:13:59 -0700 | [diff] [blame] | 178 | |
Anuj Phogat | 35f11e8 | 2014-02-05 15:01:58 -0800 | [diff] [blame] | 179 | /* Figure out if gl_FragCoord is actually used in fragment shader */ |
| 180 | ir_variable *const var = state->symbols->get_variable("gl_FragCoord"); |
| 181 | if (var != NULL) |
| 182 | state->fs_uses_gl_fragcoord = var->data.used; |
| 183 | |
Paul Berry | 719bf30 | 2013-10-15 15:13:59 -0700 | [diff] [blame] | 184 | /* From section 7.1 (Built-In Language Variables) of the GLSL 4.10 spec: |
| 185 | * |
| 186 | * If multiple shaders using members of a built-in block belonging to |
| 187 | * the same interface are linked together in the same program, they |
| 188 | * must all redeclare the built-in block in the same way, as described |
| 189 | * in section 4.3.7 "Interface Blocks" for interface block matching, or |
| 190 | * a link error will result. |
| 191 | * |
| 192 | * The phrase "using members of a built-in block" implies that if two |
| 193 | * shaders are linked together and one of them *does not use* any members |
| 194 | * of the built-in block, then that shader does not need to have a matching |
| 195 | * redeclaration of the built-in block. |
| 196 | * |
| 197 | * This appears to be a clarification to the behaviour established for |
| 198 | * gl_PerVertex by GLSL 1.50, therefore implement it regardless of GLSL |
| 199 | * version. |
| 200 | * |
| 201 | * The definition of "interface" in section 4.3.7 that applies here is as |
| 202 | * follows: |
| 203 | * |
| 204 | * The boundary between adjacent programmable pipeline stages: This |
| 205 | * spans all the outputs in all compilation units of the first stage |
| 206 | * and all the inputs in all compilation units of the second stage. |
| 207 | * |
| 208 | * Therefore this rule applies to both inter- and intra-stage linking. |
| 209 | * |
| 210 | * The easiest way to implement this is to check whether the shader uses |
| 211 | * gl_PerVertex right after ast-to-ir conversion, and if it doesn't, simply |
| 212 | * remove all the relevant variable declaration from the IR, so that the |
| 213 | * linker won't see them and complain about mismatches. |
| 214 | */ |
| 215 | remove_per_vertex_blocks(instructions, state, ir_var_shader_in); |
| 216 | remove_per_vertex_blocks(instructions, state, ir_var_shader_out); |
Iago Toral Quiroga | 0f18945 | 2015-04-28 14:25:56 +0200 | [diff] [blame] | 217 | |
| 218 | /* Check that we don't have reads from write-only variables */ |
| 219 | read_from_write_only_variable_visitor v; |
| 220 | v.run(instructions); |
| 221 | ir_variable *error_var = v.get_variable(); |
| 222 | if (error_var) { |
| 223 | /* It would be nice to have proper location information, but for that |
| 224 | * we would need to check this as we process each kind of AST node |
| 225 | */ |
| 226 | YYLTYPE loc; |
| 227 | memset(&loc, 0, sizeof(loc)); |
| 228 | _mesa_glsl_error(&loc, state, "Read from write-only variable `%s'", |
| 229 | error_var->name); |
| 230 | } |
Ian Romanick | d949a9a | 2010-03-10 09:55:22 -0800 | [diff] [blame] | 231 | } |
| 232 | |
| 233 | |
Chris Forbes | 1ace51f | 2014-05-04 20:23:54 +1200 | [diff] [blame] | 234 | static ir_expression_operation |
Andres Gomez | d1509a5 | 2016-02-25 12:49:24 +0200 | [diff] [blame] | 235 | get_implicit_conversion_operation(const glsl_type *to, const glsl_type *from, |
| 236 | struct _mesa_glsl_parse_state *state) |
Chris Forbes | 1ace51f | 2014-05-04 20:23:54 +1200 | [diff] [blame] | 237 | { |
| 238 | switch (to->base_type) { |
| 239 | case GLSL_TYPE_FLOAT: |
| 240 | switch (from->base_type) { |
| 241 | case GLSL_TYPE_INT: return ir_unop_i2f; |
| 242 | case GLSL_TYPE_UINT: return ir_unop_u2f; |
| 243 | default: return (ir_expression_operation)0; |
| 244 | } |
| 245 | |
Chris Forbes | 240974e | 2014-05-04 20:23:55 +1200 | [diff] [blame] | 246 | case GLSL_TYPE_UINT: |
Ian Romanick | 90537e1 | 2016-06-21 10:26:34 -0700 | [diff] [blame] | 247 | if (!state->is_version(400, 0) && !state->ARB_gpu_shader5_enable |
| 248 | && !state->MESA_shader_integer_functions_enable) |
Chris Forbes | 240974e | 2014-05-04 20:23:55 +1200 | [diff] [blame] | 249 | return (ir_expression_operation)0; |
| 250 | switch (from->base_type) { |
| 251 | case GLSL_TYPE_INT: return ir_unop_i2u; |
| 252 | default: return (ir_expression_operation)0; |
| 253 | } |
| 254 | |
Dave Airlie | ba3bab2 | 2015-02-05 12:04:58 +0200 | [diff] [blame] | 255 | case GLSL_TYPE_DOUBLE: |
| 256 | if (!state->has_double()) |
| 257 | return (ir_expression_operation)0; |
| 258 | switch (from->base_type) { |
| 259 | case GLSL_TYPE_INT: return ir_unop_i2d; |
| 260 | case GLSL_TYPE_UINT: return ir_unop_u2d; |
| 261 | case GLSL_TYPE_FLOAT: return ir_unop_f2d; |
| 262 | default: return (ir_expression_operation)0; |
| 263 | } |
| 264 | |
Chris Forbes | 1ace51f | 2014-05-04 20:23:54 +1200 | [diff] [blame] | 265 | default: return (ir_expression_operation)0; |
| 266 | } |
| 267 | } |
| 268 | |
| 269 | |
Ian Romanick | 0104536 | 2010-03-29 16:17:56 -0700 | [diff] [blame] | 270 | /** |
| 271 | * If a conversion is available, convert one operand to a different type |
| 272 | * |
| 273 | * The \c from \c ir_rvalue is converted "in place". |
| 274 | * |
| 275 | * \param to Type that the operand it to be converted to |
| 276 | * \param from Operand that is being converted |
| 277 | * \param state GLSL compiler state |
| 278 | * |
| 279 | * \return |
| 280 | * If a conversion is possible (or unnecessary), \c true is returned. |
| 281 | * Otherwise \c false is returned. |
| 282 | */ |
Andres Gomez | 8f98a12 | 2016-08-02 14:26:23 +0300 | [diff] [blame] | 283 | static bool |
Ian Romanick | bfb09c2 | 2010-03-29 16:32:55 -0700 | [diff] [blame] | 284 | apply_implicit_conversion(const glsl_type *to, ir_rvalue * &from, |
Iago Toral Quiroga | 4472ab9 | 2014-04-14 09:14:23 +0200 | [diff] [blame] | 285 | struct _mesa_glsl_parse_state *state) |
Ian Romanick | 0104536 | 2010-03-29 16:17:56 -0700 | [diff] [blame] | 286 | { |
Kenneth Graunke | 953ff12 | 2010-06-25 13:14:37 -0700 | [diff] [blame] | 287 | void *ctx = state; |
Ian Romanick | bfb09c2 | 2010-03-29 16:32:55 -0700 | [diff] [blame] | 288 | if (to->base_type == from->type->base_type) |
Ian Romanick | 0104536 | 2010-03-29 16:17:56 -0700 | [diff] [blame] | 289 | return true; |
| 290 | |
Chris Forbes | 1ace51f | 2014-05-04 20:23:54 +1200 | [diff] [blame] | 291 | /* Prior to GLSL 1.20, there are no implicit conversions */ |
Paul Berry | e3ded7f | 2012-08-01 14:50:05 -0700 | [diff] [blame] | 292 | if (!state->is_version(120, 0)) |
Ian Romanick | 0104536 | 2010-03-29 16:17:56 -0700 | [diff] [blame] | 293 | return false; |
| 294 | |
Ilia Mirkin | 089f605 | 2016-01-27 13:52:41 -0500 | [diff] [blame] | 295 | /* ESSL does not allow implicit conversions */ |
| 296 | if (state->es_shader) |
| 297 | return false; |
| 298 | |
Ian Romanick | 0104536 | 2010-03-29 16:17:56 -0700 | [diff] [blame] | 299 | /* From page 27 (page 33 of the PDF) of the GLSL 1.50 spec: |
| 300 | * |
| 301 | * "There are no implicit array or structure conversions. For |
| 302 | * example, an array of int cannot be implicitly converted to an |
Chris Forbes | 1ace51f | 2014-05-04 20:23:54 +1200 | [diff] [blame] | 303 | * array of float. |
Ian Romanick | 0104536 | 2010-03-29 16:17:56 -0700 | [diff] [blame] | 304 | */ |
Chris Forbes | 1ace51f | 2014-05-04 20:23:54 +1200 | [diff] [blame] | 305 | if (!to->is_numeric() || !from->type->is_numeric()) |
Ian Romanick | 0104536 | 2010-03-29 16:17:56 -0700 | [diff] [blame] | 306 | return false; |
| 307 | |
Chris Forbes | 1ace51f | 2014-05-04 20:23:54 +1200 | [diff] [blame] | 308 | /* We don't actually want the specific type `to`, we want a type |
| 309 | * with the same base type as `to`, but the same vector width as |
| 310 | * `from`. |
Kenneth Graunke | 506199b | 2010-06-29 15:59:27 -0700 | [diff] [blame] | 311 | */ |
Chris Forbes | 1ace51f | 2014-05-04 20:23:54 +1200 | [diff] [blame] | 312 | to = glsl_type::get_instance(to->base_type, from->type->vector_elements, |
| 313 | from->type->matrix_columns); |
Kenneth Graunke | 506199b | 2010-06-29 15:59:27 -0700 | [diff] [blame] | 314 | |
Andres Gomez | d1509a5 | 2016-02-25 12:49:24 +0200 | [diff] [blame] | 315 | ir_expression_operation op = get_implicit_conversion_operation(to, from->type, state); |
Chris Forbes | 1ace51f | 2014-05-04 20:23:54 +1200 | [diff] [blame] | 316 | if (op) { |
| 317 | from = new(ctx) ir_expression(op, to, from, NULL); |
| 318 | return true; |
| 319 | } else { |
| 320 | return false; |
Ian Romanick | 0104536 | 2010-03-29 16:17:56 -0700 | [diff] [blame] | 321 | } |
Ian Romanick | 0104536 | 2010-03-29 16:17:56 -0700 | [diff] [blame] | 322 | } |
| 323 | |
| 324 | |
Ian Romanick | a87ac25 | 2010-02-22 13:19:34 -0800 | [diff] [blame] | 325 | static const struct glsl_type * |
Ian Romanick | bfb09c2 | 2010-03-29 16:32:55 -0700 | [diff] [blame] | 326 | arithmetic_result_type(ir_rvalue * &value_a, ir_rvalue * &value_b, |
Iago Toral Quiroga | 4472ab9 | 2014-04-14 09:14:23 +0200 | [diff] [blame] | 327 | bool multiply, |
| 328 | struct _mesa_glsl_parse_state *state, YYLTYPE *loc) |
Ian Romanick | a87ac25 | 2010-02-22 13:19:34 -0800 | [diff] [blame] | 329 | { |
Eric Anholt | 336b4ad | 2010-05-19 10:38:37 -0700 | [diff] [blame] | 330 | const glsl_type *type_a = value_a->type; |
| 331 | const glsl_type *type_b = value_b->type; |
Ian Romanick | 0104536 | 2010-03-29 16:17:56 -0700 | [diff] [blame] | 332 | |
Ian Romanick | a87ac25 | 2010-02-22 13:19:34 -0800 | [diff] [blame] | 333 | /* From GLSL 1.50 spec, page 56: |
| 334 | * |
| 335 | * "The arithmetic binary operators add (+), subtract (-), |
| 336 | * multiply (*), and divide (/) operate on integer and |
| 337 | * floating-point scalars, vectors, and matrices." |
| 338 | */ |
Ian Romanick | 60b54d9 | 2010-03-24 17:08:13 -0700 | [diff] [blame] | 339 | if (!type_a->is_numeric() || !type_b->is_numeric()) { |
Eric Anholt | a13bb14 | 2010-03-31 16:38:11 -1000 | [diff] [blame] | 340 | _mesa_glsl_error(loc, state, |
Iago Toral Quiroga | 4472ab9 | 2014-04-14 09:14:23 +0200 | [diff] [blame] | 341 | "operands to arithmetic operators must be numeric"); |
Ian Romanick | 0471e8b | 2010-03-26 14:33:41 -0700 | [diff] [blame] | 342 | return glsl_type::error_type; |
Ian Romanick | a87ac25 | 2010-02-22 13:19:34 -0800 | [diff] [blame] | 343 | } |
| 344 | |
| 345 | |
| 346 | /* "If one operand is floating-point based and the other is |
| 347 | * not, then the conversions from Section 4.1.10 "Implicit |
| 348 | * Conversions" are applied to the non-floating-point-based operand." |
Ian Romanick | a87ac25 | 2010-02-22 13:19:34 -0800 | [diff] [blame] | 349 | */ |
Ian Romanick | 0104536 | 2010-03-29 16:17:56 -0700 | [diff] [blame] | 350 | if (!apply_implicit_conversion(type_a, value_b, state) |
| 351 | && !apply_implicit_conversion(type_b, value_a, state)) { |
Eric Anholt | a13bb14 | 2010-03-31 16:38:11 -1000 | [diff] [blame] | 352 | _mesa_glsl_error(loc, state, |
Iago Toral Quiroga | 4472ab9 | 2014-04-14 09:14:23 +0200 | [diff] [blame] | 353 | "could not implicitly convert operands to " |
| 354 | "arithmetic operator"); |
Ian Romanick | 0104536 | 2010-03-29 16:17:56 -0700 | [diff] [blame] | 355 | return glsl_type::error_type; |
Ian Romanick | a87ac25 | 2010-02-22 13:19:34 -0800 | [diff] [blame] | 356 | } |
Eric Anholt | 336b4ad | 2010-05-19 10:38:37 -0700 | [diff] [blame] | 357 | type_a = value_a->type; |
| 358 | type_b = value_b->type; |
| 359 | |
Ian Romanick | a87ac25 | 2010-02-22 13:19:34 -0800 | [diff] [blame] | 360 | /* "If the operands are integer types, they must both be signed or |
| 361 | * both be unsigned." |
| 362 | * |
| 363 | * From this rule and the preceeding conversion it can be inferred that |
| 364 | * both types must be GLSL_TYPE_FLOAT, or GLSL_TYPE_UINT, or GLSL_TYPE_INT. |
Ian Romanick | 60b54d9 | 2010-03-24 17:08:13 -0700 | [diff] [blame] | 365 | * The is_numeric check above already filtered out the case where either |
| 366 | * type is not one of these, so now the base types need only be tested for |
| 367 | * equality. |
Ian Romanick | a87ac25 | 2010-02-22 13:19:34 -0800 | [diff] [blame] | 368 | */ |
| 369 | if (type_a->base_type != type_b->base_type) { |
Eric Anholt | a13bb14 | 2010-03-31 16:38:11 -1000 | [diff] [blame] | 370 | _mesa_glsl_error(loc, state, |
Iago Toral Quiroga | 4472ab9 | 2014-04-14 09:14:23 +0200 | [diff] [blame] | 371 | "base type mismatch for arithmetic operator"); |
Ian Romanick | 0471e8b | 2010-03-26 14:33:41 -0700 | [diff] [blame] | 372 | return glsl_type::error_type; |
Ian Romanick | a87ac25 | 2010-02-22 13:19:34 -0800 | [diff] [blame] | 373 | } |
| 374 | |
| 375 | /* "All arithmetic binary operators result in the same fundamental type |
| 376 | * (signed integer, unsigned integer, or floating-point) as the |
| 377 | * operands they operate on, after operand type conversion. After |
| 378 | * conversion, the following cases are valid |
| 379 | * |
| 380 | * * The two operands are scalars. In this case the operation is |
| 381 | * applied, resulting in a scalar." |
| 382 | */ |
Ian Romanick | cb36f8a | 2010-03-09 15:51:22 -0800 | [diff] [blame] | 383 | if (type_a->is_scalar() && type_b->is_scalar()) |
Ian Romanick | a87ac25 | 2010-02-22 13:19:34 -0800 | [diff] [blame] | 384 | return type_a; |
| 385 | |
| 386 | /* "* One operand is a scalar, and the other is a vector or matrix. |
| 387 | * In this case, the scalar operation is applied independently to each |
| 388 | * component of the vector or matrix, resulting in the same size |
| 389 | * vector or matrix." |
| 390 | */ |
Ian Romanick | cb36f8a | 2010-03-09 15:51:22 -0800 | [diff] [blame] | 391 | if (type_a->is_scalar()) { |
| 392 | if (!type_b->is_scalar()) |
Iago Toral Quiroga | 4472ab9 | 2014-04-14 09:14:23 +0200 | [diff] [blame] | 393 | return type_b; |
Ian Romanick | cb36f8a | 2010-03-09 15:51:22 -0800 | [diff] [blame] | 394 | } else if (type_b->is_scalar()) { |
Ian Romanick | a87ac25 | 2010-02-22 13:19:34 -0800 | [diff] [blame] | 395 | return type_a; |
| 396 | } |
| 397 | |
| 398 | /* All of the combinations of <scalar, scalar>, <vector, scalar>, |
| 399 | * <scalar, vector>, <scalar, matrix>, and <matrix, scalar> have been |
| 400 | * handled. |
| 401 | */ |
Ian Romanick | 60b54d9 | 2010-03-24 17:08:13 -0700 | [diff] [blame] | 402 | assert(!type_a->is_scalar()); |
| 403 | assert(!type_b->is_scalar()); |
Ian Romanick | a87ac25 | 2010-02-22 13:19:34 -0800 | [diff] [blame] | 404 | |
| 405 | /* "* The two operands are vectors of the same size. In this case, the |
| 406 | * operation is done component-wise resulting in the same size |
| 407 | * vector." |
| 408 | */ |
Ian Romanick | a2dd22f | 2010-03-09 15:55:16 -0800 | [diff] [blame] | 409 | if (type_a->is_vector() && type_b->is_vector()) { |
Eric Anholt | a13bb14 | 2010-03-31 16:38:11 -1000 | [diff] [blame] | 410 | if (type_a == type_b) { |
Iago Toral Quiroga | 4472ab9 | 2014-04-14 09:14:23 +0200 | [diff] [blame] | 411 | return type_a; |
Eric Anholt | a13bb14 | 2010-03-31 16:38:11 -1000 | [diff] [blame] | 412 | } else { |
Iago Toral Quiroga | 4472ab9 | 2014-04-14 09:14:23 +0200 | [diff] [blame] | 413 | _mesa_glsl_error(loc, state, |
| 414 | "vector size mismatch for arithmetic operator"); |
| 415 | return glsl_type::error_type; |
Eric Anholt | a13bb14 | 2010-03-31 16:38:11 -1000 | [diff] [blame] | 416 | } |
Ian Romanick | a87ac25 | 2010-02-22 13:19:34 -0800 | [diff] [blame] | 417 | } |
| 418 | |
| 419 | /* All of the combinations of <scalar, scalar>, <vector, scalar>, |
| 420 | * <scalar, vector>, <scalar, matrix>, <matrix, scalar>, and |
| 421 | * <vector, vector> have been handled. At least one of the operands must |
| 422 | * be matrix. Further, since there are no integer matrix types, the base |
| 423 | * type of both operands must be float. |
| 424 | */ |
Ian Romanick | 60b54d9 | 2010-03-24 17:08:13 -0700 | [diff] [blame] | 425 | assert(type_a->is_matrix() || type_b->is_matrix()); |
Dave Airlie | ba3bab2 | 2015-02-05 12:04:58 +0200 | [diff] [blame] | 426 | assert(type_a->base_type == GLSL_TYPE_FLOAT || |
| 427 | type_a->base_type == GLSL_TYPE_DOUBLE); |
| 428 | assert(type_b->base_type == GLSL_TYPE_FLOAT || |
| 429 | type_b->base_type == GLSL_TYPE_DOUBLE); |
Ian Romanick | a87ac25 | 2010-02-22 13:19:34 -0800 | [diff] [blame] | 430 | |
| 431 | /* "* The operator is add (+), subtract (-), or divide (/), and the |
| 432 | * operands are matrices with the same number of rows and the same |
| 433 | * number of columns. In this case, the operation is done component- |
| 434 | * wise resulting in the same size matrix." |
| 435 | * * The operator is multiply (*), where both operands are matrices or |
| 436 | * one operand is a vector and the other a matrix. A right vector |
| 437 | * operand is treated as a column vector and a left vector operand as a |
| 438 | * row vector. In all these cases, it is required that the number of |
| 439 | * columns of the left operand is equal to the number of rows of the |
| 440 | * right operand. Then, the multiply (*) operation does a linear |
| 441 | * algebraic multiply, yielding an object that has the same number of |
| 442 | * rows as the left operand and the same number of columns as the right |
| 443 | * operand. Section 5.10 "Vector and Matrix Operations" explains in |
| 444 | * more detail how vectors and matrices are operated on." |
| 445 | */ |
| 446 | if (! multiply) { |
Eric Anholt | a13bb14 | 2010-03-31 16:38:11 -1000 | [diff] [blame] | 447 | if (type_a == type_b) |
Iago Toral Quiroga | 4472ab9 | 2014-04-14 09:14:23 +0200 | [diff] [blame] | 448 | return type_a; |
Ian Romanick | a87ac25 | 2010-02-22 13:19:34 -0800 | [diff] [blame] | 449 | } else { |
Matt Turner | 73f6f9b | 2015-03-27 10:43:05 -0700 | [diff] [blame] | 450 | const glsl_type *type = glsl_type::get_mul_type(type_a, type_b); |
Eric Anholt | a13bb14 | 2010-03-31 16:38:11 -1000 | [diff] [blame] | 451 | |
Matt Turner | 73f6f9b | 2015-03-27 10:43:05 -0700 | [diff] [blame] | 452 | if (type == glsl_type::error_type) { |
| 453 | _mesa_glsl_error(loc, state, |
| 454 | "size mismatch for matrix multiplication"); |
Ian Romanick | a87ac25 | 2010-02-22 13:19:34 -0800 | [diff] [blame] | 455 | } |
Eric Anholt | a13bb14 | 2010-03-31 16:38:11 -1000 | [diff] [blame] | 456 | |
Matt Turner | 73f6f9b | 2015-03-27 10:43:05 -0700 | [diff] [blame] | 457 | return type; |
Ian Romanick | a87ac25 | 2010-02-22 13:19:34 -0800 | [diff] [blame] | 458 | } |
| 459 | |
| 460 | |
| 461 | /* "All other cases are illegal." |
| 462 | */ |
Eric Anholt | a13bb14 | 2010-03-31 16:38:11 -1000 | [diff] [blame] | 463 | _mesa_glsl_error(loc, state, "type mismatch"); |
Ian Romanick | 0471e8b | 2010-03-26 14:33:41 -0700 | [diff] [blame] | 464 | return glsl_type::error_type; |
Ian Romanick | a87ac25 | 2010-02-22 13:19:34 -0800 | [diff] [blame] | 465 | } |
| 466 | |
| 467 | |
| 468 | static const struct glsl_type * |
Eric Anholt | 65e1a7ac | 2010-03-31 16:45:20 -1000 | [diff] [blame] | 469 | unary_arithmetic_result_type(const struct glsl_type *type, |
Iago Toral Quiroga | 4472ab9 | 2014-04-14 09:14:23 +0200 | [diff] [blame] | 470 | struct _mesa_glsl_parse_state *state, YYLTYPE *loc) |
Ian Romanick | a87ac25 | 2010-02-22 13:19:34 -0800 | [diff] [blame] | 471 | { |
| 472 | /* From GLSL 1.50 spec, page 57: |
| 473 | * |
| 474 | * "The arithmetic unary operators negate (-), post- and pre-increment |
| 475 | * and decrement (-- and ++) operate on integer or floating-point |
| 476 | * values (including vectors and matrices). All unary operators work |
| 477 | * component-wise on their operands. These result with the same type |
| 478 | * they operated on." |
| 479 | */ |
Eric Anholt | 65e1a7ac | 2010-03-31 16:45:20 -1000 | [diff] [blame] | 480 | if (!type->is_numeric()) { |
| 481 | _mesa_glsl_error(loc, state, |
Iago Toral Quiroga | 4472ab9 | 2014-04-14 09:14:23 +0200 | [diff] [blame] | 482 | "operands to arithmetic operators must be numeric"); |
Ian Romanick | 0471e8b | 2010-03-26 14:33:41 -0700 | [diff] [blame] | 483 | return glsl_type::error_type; |
Eric Anholt | 65e1a7ac | 2010-03-31 16:45:20 -1000 | [diff] [blame] | 484 | } |
Ian Romanick | a87ac25 | 2010-02-22 13:19:34 -0800 | [diff] [blame] | 485 | |
| 486 | return type; |
| 487 | } |
| 488 | |
Chad Versace | cfdbf8b | 2010-10-15 11:28:05 -0700 | [diff] [blame] | 489 | /** |
| 490 | * \brief Return the result type of a bit-logic operation. |
| 491 | * |
| 492 | * If the given types to the bit-logic operator are invalid, return |
| 493 | * glsl_type::error_type. |
| 494 | * |
Kenneth Graunke | d54a70a | 2016-01-14 23:27:03 -0800 | [diff] [blame] | 495 | * \param value_a LHS of bit-logic op |
| 496 | * \param value_b RHS of bit-logic op |
Chad Versace | cfdbf8b | 2010-10-15 11:28:05 -0700 | [diff] [blame] | 497 | */ |
| 498 | static const struct glsl_type * |
Kenneth Graunke | d54a70a | 2016-01-14 23:27:03 -0800 | [diff] [blame] | 499 | bit_logic_result_type(ir_rvalue * &value_a, ir_rvalue * &value_b, |
Chad Versace | cfdbf8b | 2010-10-15 11:28:05 -0700 | [diff] [blame] | 500 | ast_operators op, |
| 501 | struct _mesa_glsl_parse_state *state, YYLTYPE *loc) |
| 502 | { |
Kenneth Graunke | d54a70a | 2016-01-14 23:27:03 -0800 | [diff] [blame] | 503 | const glsl_type *type_a = value_a->type; |
| 504 | const glsl_type *type_b = value_b->type; |
| 505 | |
Kenneth Graunke | 156b7d3 | 2015-10-20 19:51:56 -0700 | [diff] [blame] | 506 | if (!state->check_bitwise_operations_allowed(loc)) { |
| 507 | return glsl_type::error_type; |
| 508 | } |
Chad Versace | cfdbf8b | 2010-10-15 11:28:05 -0700 | [diff] [blame] | 509 | |
Kenneth Graunke | 156b7d3 | 2015-10-20 19:51:56 -0700 | [diff] [blame] | 510 | /* From page 50 (page 56 of PDF) of GLSL 1.30 spec: |
| 511 | * |
| 512 | * "The bitwise operators and (&), exclusive-or (^), and inclusive-or |
| 513 | * (|). The operands must be of type signed or unsigned integers or |
| 514 | * integer vectors." |
| 515 | */ |
| 516 | if (!type_a->is_integer()) { |
| 517 | _mesa_glsl_error(loc, state, "LHS of `%s' must be an integer", |
Chad Versace | cfdbf8b | 2010-10-15 11:28:05 -0700 | [diff] [blame] | 518 | ast_expression::operator_string(op)); |
Kenneth Graunke | 156b7d3 | 2015-10-20 19:51:56 -0700 | [diff] [blame] | 519 | return glsl_type::error_type; |
| 520 | } |
| 521 | if (!type_b->is_integer()) { |
| 522 | _mesa_glsl_error(loc, state, "RHS of `%s' must be an integer", |
| 523 | ast_expression::operator_string(op)); |
| 524 | return glsl_type::error_type; |
| 525 | } |
Chad Versace | cfdbf8b | 2010-10-15 11:28:05 -0700 | [diff] [blame] | 526 | |
Kenneth Graunke | d54a70a | 2016-01-14 23:27:03 -0800 | [diff] [blame] | 527 | /* Prior to GLSL 4.0 / GL_ARB_gpu_shader5, implicit conversions didn't |
| 528 | * make sense for bitwise operations, as they don't operate on floats. |
| 529 | * |
| 530 | * GLSL 4.0 added implicit int -> uint conversions, which are relevant |
| 531 | * here. It wasn't clear whether or not we should apply them to bitwise |
| 532 | * operations. However, Khronos has decided that they should in future |
| 533 | * language revisions. Applications also rely on this behavior. We opt |
| 534 | * to apply them in general, but issue a portability warning. |
| 535 | * |
| 536 | * See https://www.khronos.org/bugzilla/show_bug.cgi?id=1405 |
| 537 | */ |
| 538 | if (type_a->base_type != type_b->base_type) { |
| 539 | if (!apply_implicit_conversion(type_a, value_b, state) |
| 540 | && !apply_implicit_conversion(type_b, value_a, state)) { |
| 541 | _mesa_glsl_error(loc, state, |
| 542 | "could not implicitly convert operands to " |
| 543 | "`%s` operator", |
| 544 | ast_expression::operator_string(op)); |
| 545 | return glsl_type::error_type; |
| 546 | } else { |
| 547 | _mesa_glsl_warning(loc, state, |
| 548 | "some implementations may not support implicit " |
| 549 | "int -> uint conversions for `%s' operators; " |
| 550 | "consider casting explicitly for portability", |
| 551 | ast_expression::operator_string(op)); |
| 552 | } |
| 553 | type_a = value_a->type; |
| 554 | type_b = value_b->type; |
| 555 | } |
| 556 | |
Kenneth Graunke | 156b7d3 | 2015-10-20 19:51:56 -0700 | [diff] [blame] | 557 | /* "The fundamental types of the operands (signed or unsigned) must |
| 558 | * match," |
| 559 | */ |
| 560 | if (type_a->base_type != type_b->base_type) { |
| 561 | _mesa_glsl_error(loc, state, "operands of `%s' must have the same " |
| 562 | "base type", ast_expression::operator_string(op)); |
| 563 | return glsl_type::error_type; |
| 564 | } |
Chad Versace | cfdbf8b | 2010-10-15 11:28:05 -0700 | [diff] [blame] | 565 | |
Kenneth Graunke | 156b7d3 | 2015-10-20 19:51:56 -0700 | [diff] [blame] | 566 | /* "The operands cannot be vectors of differing size." */ |
| 567 | if (type_a->is_vector() && |
| 568 | type_b->is_vector() && |
| 569 | type_a->vector_elements != type_b->vector_elements) { |
| 570 | _mesa_glsl_error(loc, state, "operands of `%s' cannot be vectors of " |
| 571 | "different sizes", ast_expression::operator_string(op)); |
| 572 | return glsl_type::error_type; |
| 573 | } |
Chad Versace | cfdbf8b | 2010-10-15 11:28:05 -0700 | [diff] [blame] | 574 | |
Kenneth Graunke | 156b7d3 | 2015-10-20 19:51:56 -0700 | [diff] [blame] | 575 | /* "If one operand is a scalar and the other a vector, the scalar is |
| 576 | * applied component-wise to the vector, resulting in the same type as |
| 577 | * the vector. The fundamental types of the operands [...] will be the |
| 578 | * resulting fundamental type." |
| 579 | */ |
| 580 | if (type_a->is_scalar()) |
| 581 | return type_b; |
| 582 | else |
| 583 | return type_a; |
Chad Versace | cfdbf8b | 2010-10-15 11:28:05 -0700 | [diff] [blame] | 584 | } |
Ian Romanick | a87ac25 | 2010-02-22 13:19:34 -0800 | [diff] [blame] | 585 | |
| 586 | static const struct glsl_type * |
Kenneth Graunke | 511de1a | 2015-11-12 13:02:05 -0800 | [diff] [blame] | 587 | modulus_result_type(ir_rvalue * &value_a, ir_rvalue * &value_b, |
Iago Toral Quiroga | 4472ab9 | 2014-04-14 09:14:23 +0200 | [diff] [blame] | 588 | struct _mesa_glsl_parse_state *state, YYLTYPE *loc) |
Ian Romanick | a87ac25 | 2010-02-22 13:19:34 -0800 | [diff] [blame] | 589 | { |
Kenneth Graunke | 511de1a | 2015-11-12 13:02:05 -0800 | [diff] [blame] | 590 | const glsl_type *type_a = value_a->type; |
| 591 | const glsl_type *type_b = value_b->type; |
| 592 | |
Paul Berry | d4a2474 | 2012-08-02 08:18:12 -0700 | [diff] [blame] | 593 | if (!state->check_version(130, 300, loc, "operator '%%' is reserved")) { |
Chad Versace | 82f994f | 2011-02-04 12:18:56 -0800 | [diff] [blame] | 594 | return glsl_type::error_type; |
| 595 | } |
| 596 | |
Kenneth Graunke | 511de1a | 2015-11-12 13:02:05 -0800 | [diff] [blame] | 597 | /* Section 5.9 (Expressions) of the GLSL 4.00 specification says: |
| 598 | * |
Ian Romanick | a87ac25 | 2010-02-22 13:19:34 -0800 | [diff] [blame] | 599 | * "The operator modulus (%) operates on signed or unsigned integers or |
Kenneth Graunke | 511de1a | 2015-11-12 13:02:05 -0800 | [diff] [blame] | 600 | * integer vectors." |
Ian Romanick | a87ac25 | 2010-02-22 13:19:34 -0800 | [diff] [blame] | 601 | */ |
Kenneth Graunke | 8eb9753 | 2011-06-14 22:21:41 -0700 | [diff] [blame] | 602 | if (!type_a->is_integer()) { |
Paul Berry | 4d7899f | 2013-07-25 19:56:43 -0700 | [diff] [blame] | 603 | _mesa_glsl_error(loc, state, "LHS of operator %% must be an integer"); |
Kenneth Graunke | 8eb9753 | 2011-06-14 22:21:41 -0700 | [diff] [blame] | 604 | return glsl_type::error_type; |
| 605 | } |
| 606 | if (!type_b->is_integer()) { |
Paul Berry | 4d7899f | 2013-07-25 19:56:43 -0700 | [diff] [blame] | 607 | _mesa_glsl_error(loc, state, "RHS of operator %% must be an integer"); |
Kenneth Graunke | 8eb9753 | 2011-06-14 22:21:41 -0700 | [diff] [blame] | 608 | return glsl_type::error_type; |
| 609 | } |
Kenneth Graunke | 511de1a | 2015-11-12 13:02:05 -0800 | [diff] [blame] | 610 | |
| 611 | /* "If the fundamental types in the operands do not match, then the |
| 612 | * conversions from section 4.1.10 "Implicit Conversions" are applied |
| 613 | * to create matching types." |
| 614 | * |
| 615 | * Note that GLSL 4.00 (and GL_ARB_gpu_shader5) introduced implicit |
| 616 | * int -> uint conversion rules. Prior to that, there were no implicit |
| 617 | * conversions. So it's harmless to apply them universally - no implicit |
| 618 | * conversions will exist. If the types don't match, we'll receive false, |
| 619 | * and raise an error, satisfying the GLSL 1.50 spec, page 56: |
| 620 | * |
| 621 | * "The operand types must both be signed or unsigned." |
| 622 | */ |
| 623 | if (!apply_implicit_conversion(type_a, value_b, state) && |
| 624 | !apply_implicit_conversion(type_b, value_a, state)) { |
Kenneth Graunke | 8eb9753 | 2011-06-14 22:21:41 -0700 | [diff] [blame] | 625 | _mesa_glsl_error(loc, state, |
Kenneth Graunke | 511de1a | 2015-11-12 13:02:05 -0800 | [diff] [blame] | 626 | "could not implicitly convert operands to " |
| 627 | "modulus (%%) operator"); |
Ian Romanick | 0471e8b | 2010-03-26 14:33:41 -0700 | [diff] [blame] | 628 | return glsl_type::error_type; |
Ian Romanick | a87ac25 | 2010-02-22 13:19:34 -0800 | [diff] [blame] | 629 | } |
Kenneth Graunke | 511de1a | 2015-11-12 13:02:05 -0800 | [diff] [blame] | 630 | type_a = value_a->type; |
| 631 | type_b = value_b->type; |
Ian Romanick | a87ac25 | 2010-02-22 13:19:34 -0800 | [diff] [blame] | 632 | |
| 633 | /* "The operands cannot be vectors of differing size. If one operand is |
| 634 | * a scalar and the other vector, then the scalar is applied component- |
| 635 | * wise to the vector, resulting in the same type as the vector. If both |
| 636 | * are vectors of the same size, the result is computed component-wise." |
| 637 | */ |
Ian Romanick | a2dd22f | 2010-03-09 15:55:16 -0800 | [diff] [blame] | 638 | if (type_a->is_vector()) { |
| 639 | if (!type_b->is_vector() |
Iago Toral Quiroga | 4472ab9 | 2014-04-14 09:14:23 +0200 | [diff] [blame] | 640 | || (type_a->vector_elements == type_b->vector_elements)) |
| 641 | return type_a; |
Ian Romanick | a87ac25 | 2010-02-22 13:19:34 -0800 | [diff] [blame] | 642 | } else |
| 643 | return type_b; |
| 644 | |
| 645 | /* "The operator modulus (%) is not defined for any other data types |
| 646 | * (non-integer types)." |
| 647 | */ |
Eric Anholt | 65e1a7ac | 2010-03-31 16:45:20 -1000 | [diff] [blame] | 648 | _mesa_glsl_error(loc, state, "type mismatch"); |
Ian Romanick | 0471e8b | 2010-03-26 14:33:41 -0700 | [diff] [blame] | 649 | return glsl_type::error_type; |
Ian Romanick | a87ac25 | 2010-02-22 13:19:34 -0800 | [diff] [blame] | 650 | } |
| 651 | |
| 652 | |
| 653 | static const struct glsl_type * |
Ian Romanick | bfb09c2 | 2010-03-29 16:32:55 -0700 | [diff] [blame] | 654 | relational_result_type(ir_rvalue * &value_a, ir_rvalue * &value_b, |
Iago Toral Quiroga | 4472ab9 | 2014-04-14 09:14:23 +0200 | [diff] [blame] | 655 | struct _mesa_glsl_parse_state *state, YYLTYPE *loc) |
Ian Romanick | a87ac25 | 2010-02-22 13:19:34 -0800 | [diff] [blame] | 656 | { |
Eric Anholt | 336b4ad | 2010-05-19 10:38:37 -0700 | [diff] [blame] | 657 | const glsl_type *type_a = value_a->type; |
| 658 | const glsl_type *type_b = value_b->type; |
Ian Romanick | 0150f5f | 2010-03-29 16:20:07 -0700 | [diff] [blame] | 659 | |
Ian Romanick | a87ac25 | 2010-02-22 13:19:34 -0800 | [diff] [blame] | 660 | /* From GLSL 1.50 spec, page 56: |
| 661 | * "The relational operators greater than (>), less than (<), greater |
| 662 | * than or equal (>=), and less than or equal (<=) operate only on |
| 663 | * scalar integer and scalar floating-point expressions." |
| 664 | */ |
Ian Romanick | a6d653d | 2010-03-26 14:40:37 -0700 | [diff] [blame] | 665 | if (!type_a->is_numeric() |
| 666 | || !type_b->is_numeric() |
Ian Romanick | cb36f8a | 2010-03-09 15:51:22 -0800 | [diff] [blame] | 667 | || !type_a->is_scalar() |
Eric Anholt | 65e1a7ac | 2010-03-31 16:45:20 -1000 | [diff] [blame] | 668 | || !type_b->is_scalar()) { |
| 669 | _mesa_glsl_error(loc, state, |
Iago Toral Quiroga | 4472ab9 | 2014-04-14 09:14:23 +0200 | [diff] [blame] | 670 | "operands to relational operators must be scalar and " |
| 671 | "numeric"); |
Ian Romanick | 0471e8b | 2010-03-26 14:33:41 -0700 | [diff] [blame] | 672 | return glsl_type::error_type; |
Eric Anholt | 65e1a7ac | 2010-03-31 16:45:20 -1000 | [diff] [blame] | 673 | } |
Ian Romanick | a87ac25 | 2010-02-22 13:19:34 -0800 | [diff] [blame] | 674 | |
| 675 | /* "Either the operands' types must match, or the conversions from |
| 676 | * Section 4.1.10 "Implicit Conversions" will be applied to the integer |
| 677 | * operand, after which the types must match." |
Ian Romanick | a87ac25 | 2010-02-22 13:19:34 -0800 | [diff] [blame] | 678 | */ |
Ian Romanick | 0150f5f | 2010-03-29 16:20:07 -0700 | [diff] [blame] | 679 | if (!apply_implicit_conversion(type_a, value_b, state) |
| 680 | && !apply_implicit_conversion(type_b, value_a, state)) { |
Eric Anholt | 65e1a7ac | 2010-03-31 16:45:20 -1000 | [diff] [blame] | 681 | _mesa_glsl_error(loc, state, |
Iago Toral Quiroga | 4472ab9 | 2014-04-14 09:14:23 +0200 | [diff] [blame] | 682 | "could not implicitly convert operands to " |
| 683 | "relational operator"); |
Ian Romanick | 0150f5f | 2010-03-29 16:20:07 -0700 | [diff] [blame] | 684 | return glsl_type::error_type; |
Ian Romanick | a87ac25 | 2010-02-22 13:19:34 -0800 | [diff] [blame] | 685 | } |
Eric Anholt | 336b4ad | 2010-05-19 10:38:37 -0700 | [diff] [blame] | 686 | type_a = value_a->type; |
| 687 | type_b = value_b->type; |
Ian Romanick | a87ac25 | 2010-02-22 13:19:34 -0800 | [diff] [blame] | 688 | |
Eric Anholt | 65e1a7ac | 2010-03-31 16:45:20 -1000 | [diff] [blame] | 689 | if (type_a->base_type != type_b->base_type) { |
| 690 | _mesa_glsl_error(loc, state, "base type mismatch"); |
Ian Romanick | 0471e8b | 2010-03-26 14:33:41 -0700 | [diff] [blame] | 691 | return glsl_type::error_type; |
Eric Anholt | 65e1a7ac | 2010-03-31 16:45:20 -1000 | [diff] [blame] | 692 | } |
Ian Romanick | a87ac25 | 2010-02-22 13:19:34 -0800 | [diff] [blame] | 693 | |
| 694 | /* "The result is scalar Boolean." |
| 695 | */ |
Ian Romanick | 0471e8b | 2010-03-26 14:33:41 -0700 | [diff] [blame] | 696 | return glsl_type::bool_type; |
Ian Romanick | a87ac25 | 2010-02-22 13:19:34 -0800 | [diff] [blame] | 697 | } |
| 698 | |
Chad Versace | c0197ab | 2010-10-15 09:49:46 -0700 | [diff] [blame] | 699 | /** |
| 700 | * \brief Return the result type of a bit-shift operation. |
| 701 | * |
| 702 | * If the given types to the bit-shift operator are invalid, return |
| 703 | * glsl_type::error_type. |
| 704 | * |
| 705 | * \param type_a Type of LHS of bit-shift op |
| 706 | * \param type_b Type of RHS of bit-shift op |
| 707 | */ |
| 708 | static const struct glsl_type * |
| 709 | shift_result_type(const struct glsl_type *type_a, |
| 710 | const struct glsl_type *type_b, |
| 711 | ast_operators op, |
| 712 | struct _mesa_glsl_parse_state *state, YYLTYPE *loc) |
| 713 | { |
Paul Berry | 0d9bba6 | 2012-08-05 09:57:01 -0700 | [diff] [blame] | 714 | if (!state->check_bitwise_operations_allowed(loc)) { |
Chad Versace | c0197ab | 2010-10-15 09:49:46 -0700 | [diff] [blame] | 715 | return glsl_type::error_type; |
| 716 | } |
| 717 | |
| 718 | /* From page 50 (page 56 of the PDF) of the GLSL 1.30 spec: |
| 719 | * |
| 720 | * "The shift operators (<<) and (>>). For both operators, the operands |
| 721 | * must be signed or unsigned integers or integer vectors. One operand |
| 722 | * can be signed while the other is unsigned." |
| 723 | */ |
| 724 | if (!type_a->is_integer()) { |
| 725 | _mesa_glsl_error(loc, state, "LHS of operator %s must be an integer or " |
Iago Toral Quiroga | 4472ab9 | 2014-04-14 09:14:23 +0200 | [diff] [blame] | 726 | "integer vector", ast_expression::operator_string(op)); |
Chad Versace | c0197ab | 2010-10-15 09:49:46 -0700 | [diff] [blame] | 727 | return glsl_type::error_type; |
| 728 | |
| 729 | } |
| 730 | if (!type_b->is_integer()) { |
| 731 | _mesa_glsl_error(loc, state, "RHS of operator %s must be an integer or " |
Iago Toral Quiroga | 4472ab9 | 2014-04-14 09:14:23 +0200 | [diff] [blame] | 732 | "integer vector", ast_expression::operator_string(op)); |
Chad Versace | c0197ab | 2010-10-15 09:49:46 -0700 | [diff] [blame] | 733 | return glsl_type::error_type; |
| 734 | } |
| 735 | |
| 736 | /* "If the first operand is a scalar, the second operand has to be |
| 737 | * a scalar as well." |
| 738 | */ |
| 739 | if (type_a->is_scalar() && !type_b->is_scalar()) { |
Paul Berry | 4d7899f | 2013-07-25 19:56:43 -0700 | [diff] [blame] | 740 | _mesa_glsl_error(loc, state, "if the first operand of %s is scalar, the " |
Iago Toral Quiroga | 4472ab9 | 2014-04-14 09:14:23 +0200 | [diff] [blame] | 741 | "second must be scalar as well", |
| 742 | ast_expression::operator_string(op)); |
Chad Versace | c0197ab | 2010-10-15 09:49:46 -0700 | [diff] [blame] | 743 | return glsl_type::error_type; |
| 744 | } |
| 745 | |
| 746 | /* If both operands are vectors, check that they have same number of |
| 747 | * elements. |
| 748 | */ |
| 749 | if (type_a->is_vector() && |
| 750 | type_b->is_vector() && |
| 751 | type_a->vector_elements != type_b->vector_elements) { |
Paul Berry | 4d7899f | 2013-07-25 19:56:43 -0700 | [diff] [blame] | 752 | _mesa_glsl_error(loc, state, "vector operands to operator %s must " |
Iago Toral Quiroga | 4472ab9 | 2014-04-14 09:14:23 +0200 | [diff] [blame] | 753 | "have same number of elements", |
| 754 | ast_expression::operator_string(op)); |
Chad Versace | c0197ab | 2010-10-15 09:49:46 -0700 | [diff] [blame] | 755 | return glsl_type::error_type; |
| 756 | } |
| 757 | |
| 758 | /* "In all cases, the resulting type will be the same type as the left |
| 759 | * operand." |
| 760 | */ |
| 761 | return type_a; |
| 762 | } |
Ian Romanick | a87ac25 | 2010-02-22 13:19:34 -0800 | [diff] [blame] | 763 | |
Ian Romanick | 0bb1c3c | 2010-03-23 13:23:31 -0700 | [diff] [blame] | 764 | /** |
Chris Forbes | d563946 | 2014-08-31 19:35:46 +1200 | [diff] [blame] | 765 | * Returns the innermost array index expression in an rvalue tree. |
| 766 | * This is the largest indexing level -- if an array of blocks, then |
| 767 | * it is the block index rather than an indexing expression for an |
| 768 | * array-typed member of an array of blocks. |
| 769 | */ |
| 770 | static ir_rvalue * |
| 771 | find_innermost_array_index(ir_rvalue *rv) |
| 772 | { |
| 773 | ir_dereference_array *last = NULL; |
| 774 | while (rv) { |
| 775 | if (rv->as_dereference_array()) { |
| 776 | last = rv->as_dereference_array(); |
| 777 | rv = last->array; |
| 778 | } else if (rv->as_dereference_record()) |
| 779 | rv = rv->as_dereference_record()->record; |
| 780 | else if (rv->as_swizzle()) |
| 781 | rv = rv->as_swizzle()->val; |
| 782 | else |
| 783 | rv = NULL; |
| 784 | } |
| 785 | |
| 786 | if (last) |
| 787 | return last->array_index; |
| 788 | |
| 789 | return NULL; |
| 790 | } |
| 791 | |
| 792 | /** |
Ian Romanick | 0bb1c3c | 2010-03-23 13:23:31 -0700 | [diff] [blame] | 793 | * Validates that a value can be assigned to a location with a specified type |
| 794 | * |
| 795 | * Validates that \c rhs can be assigned to some location. If the types are |
| 796 | * not an exact match but an automatic conversion is possible, \c rhs will be |
| 797 | * converted. |
| 798 | * |
| 799 | * \return |
| 800 | * \c NULL if \c rhs cannot be assigned to a location with type \c lhs_type. |
| 801 | * Otherwise the actual RHS to be assigned will be returned. This may be |
| 802 | * \c rhs, or it may be \c rhs after some type conversion. |
| 803 | * |
| 804 | * \note |
| 805 | * In addition to being used for assignments, this function is used to |
| 806 | * type-check return values. |
| 807 | */ |
Chris Forbes | d563946 | 2014-08-31 19:35:46 +1200 | [diff] [blame] | 808 | static ir_rvalue * |
Eric Anholt | 336b4ad | 2010-05-19 10:38:37 -0700 | [diff] [blame] | 809 | validate_assignment(struct _mesa_glsl_parse_state *state, |
Chris Forbes | d563946 | 2014-08-31 19:35:46 +1200 | [diff] [blame] | 810 | YYLTYPE loc, ir_rvalue *lhs, |
Timothy Arceri | d1d3b1e | 2013-10-17 22:42:18 +1100 | [diff] [blame] | 811 | ir_rvalue *rhs, bool is_initializer) |
Ian Romanick | 0bb1c3c | 2010-03-23 13:23:31 -0700 | [diff] [blame] | 812 | { |
Ian Romanick | 0bb1c3c | 2010-03-23 13:23:31 -0700 | [diff] [blame] | 813 | /* If there is already some error in the RHS, just return it. Anything |
| 814 | * else will lead to an avalanche of error message back to the user. |
| 815 | */ |
Ian Romanick | ec53010 | 2010-12-10 15:47:11 -0800 | [diff] [blame] | 816 | if (rhs->type->is_error()) |
Ian Romanick | 0bb1c3c | 2010-03-23 13:23:31 -0700 | [diff] [blame] | 817 | return rhs; |
| 818 | |
Chris Forbes | d563946 | 2014-08-31 19:35:46 +1200 | [diff] [blame] | 819 | /* In the Tessellation Control Shader: |
| 820 | * If a per-vertex output variable is used as an l-value, it is an error |
| 821 | * if the expression indicating the vertex number is not the identifier |
| 822 | * `gl_InvocationID`. |
| 823 | */ |
Timothy Arceri | 7ebc3de | 2016-03-09 16:58:29 +1100 | [diff] [blame] | 824 | if (state->stage == MESA_SHADER_TESS_CTRL && !lhs->type->is_error()) { |
Chris Forbes | d563946 | 2014-08-31 19:35:46 +1200 | [diff] [blame] | 825 | ir_variable *var = lhs->variable_referenced(); |
Timothy Arceri | 87fb5aa | 2016-05-30 12:16:39 +1000 | [diff] [blame] | 826 | if (var && var->data.mode == ir_var_shader_out && !var->data.patch) { |
Chris Forbes | d563946 | 2014-08-31 19:35:46 +1200 | [diff] [blame] | 827 | ir_rvalue *index = find_innermost_array_index(lhs); |
| 828 | ir_variable *index_var = index ? index->variable_referenced() : NULL; |
| 829 | if (!index_var || strcmp(index_var->name, "gl_InvocationID") != 0) { |
| 830 | _mesa_glsl_error(&loc, state, |
| 831 | "Tessellation control shader outputs can only " |
| 832 | "be indexed by gl_InvocationID"); |
| 833 | return NULL; |
| 834 | } |
| 835 | } |
| 836 | } |
| 837 | |
Ian Romanick | 0bb1c3c | 2010-03-23 13:23:31 -0700 | [diff] [blame] | 838 | /* If the types are identical, the assignment can trivially proceed. |
| 839 | */ |
Chris Forbes | d563946 | 2014-08-31 19:35:46 +1200 | [diff] [blame] | 840 | if (rhs->type == lhs->type) |
Ian Romanick | 0bb1c3c | 2010-03-23 13:23:31 -0700 | [diff] [blame] | 841 | return rhs; |
| 842 | |
Timothy Arceri | b59c592 | 2013-10-23 21:31:27 +1100 | [diff] [blame] | 843 | /* If the array element types are the same and the LHS is unsized, |
Ian Romanick | 85caea2 | 2011-03-15 16:33:27 -0700 | [diff] [blame] | 844 | * the assignment is okay for initializers embedded in variable |
| 845 | * declarations. |
Ian Romanick | 0157f41 | 2010-04-02 17:44:39 -0700 | [diff] [blame] | 846 | * |
| 847 | * Note: Whole-array assignments are not permitted in GLSL 1.10, but this |
| 848 | * is handled by ir_dereference::is_lvalue. |
| 849 | */ |
Timothy Arceri | 3129359 | 2015-10-15 14:32:41 +1100 | [diff] [blame] | 850 | const glsl_type *lhs_t = lhs->type; |
| 851 | const glsl_type *rhs_t = rhs->type; |
| 852 | bool unsized_array = false; |
| 853 | while(lhs_t->is_array()) { |
| 854 | if (rhs_t == lhs_t) |
| 855 | break; /* the rest of the inner arrays match so break out early */ |
| 856 | if (!rhs_t->is_array()) { |
| 857 | unsized_array = false; |
| 858 | break; /* number of dimensions mismatch */ |
| 859 | } |
| 860 | if (lhs_t->length == rhs_t->length) { |
| 861 | lhs_t = lhs_t->fields.array; |
| 862 | rhs_t = rhs_t->fields.array; |
| 863 | continue; |
| 864 | } else if (lhs_t->is_unsized_array()) { |
| 865 | unsized_array = true; |
| 866 | } else { |
| 867 | unsized_array = false; |
| 868 | break; /* sized array mismatch */ |
| 869 | } |
| 870 | lhs_t = lhs_t->fields.array; |
| 871 | rhs_t = rhs_t->fields.array; |
| 872 | } |
| 873 | if (unsized_array) { |
Timothy Arceri | 3c9f009 | 2013-11-20 08:42:19 +1100 | [diff] [blame] | 874 | if (is_initializer) { |
| 875 | return rhs; |
| 876 | } else { |
| 877 | _mesa_glsl_error(&loc, state, |
| 878 | "implicitly sized arrays cannot be assigned"); |
| 879 | return NULL; |
| 880 | } |
Ian Romanick | 0157f41 | 2010-04-02 17:44:39 -0700 | [diff] [blame] | 881 | } |
| 882 | |
Eric Anholt | 336b4ad | 2010-05-19 10:38:37 -0700 | [diff] [blame] | 883 | /* Check for implicit conversion in GLSL 1.20 */ |
Chris Forbes | d563946 | 2014-08-31 19:35:46 +1200 | [diff] [blame] | 884 | if (apply_implicit_conversion(lhs->type, rhs, state)) { |
| 885 | if (rhs->type == lhs->type) |
Timothy Arceri | 222f66a | 2016-09-28 16:04:08 +1000 | [diff] [blame] | 886 | return rhs; |
Eric Anholt | 336b4ad | 2010-05-19 10:38:37 -0700 | [diff] [blame] | 887 | } |
| 888 | |
Timothy Arceri | d1d3b1e | 2013-10-17 22:42:18 +1100 | [diff] [blame] | 889 | _mesa_glsl_error(&loc, state, |
| 890 | "%s of type %s cannot be assigned to " |
| 891 | "variable of type %s", |
| 892 | is_initializer ? "initializer" : "value", |
Chris Forbes | d563946 | 2014-08-31 19:35:46 +1200 | [diff] [blame] | 893 | rhs->type->name, lhs->type->name); |
Timothy Arceri | d1d3b1e | 2013-10-17 22:42:18 +1100 | [diff] [blame] | 894 | |
Ian Romanick | 0bb1c3c | 2010-03-23 13:23:31 -0700 | [diff] [blame] | 895 | return NULL; |
| 896 | } |
| 897 | |
Eric Anholt | a313c29 | 2011-08-05 21:40:50 -0700 | [diff] [blame] | 898 | static void |
| 899 | mark_whole_array_access(ir_rvalue *access) |
| 900 | { |
| 901 | ir_dereference_variable *deref = access->as_dereference_variable(); |
| 902 | |
| 903 | if (deref && deref->var) { |
Tapani Pälli | 447bb90 | 2013-12-12 15:08:59 +0200 | [diff] [blame] | 904 | deref->var->data.max_array_access = deref->type->length - 1; |
Eric Anholt | a313c29 | 2011-08-05 21:40:50 -0700 | [diff] [blame] | 905 | } |
| 906 | } |
| 907 | |
Eric Anholt | e9822f7 | 2014-03-05 17:05:54 -0800 | [diff] [blame] | 908 | static bool |
Eric Anholt | 10a6852 | 2010-03-26 11:53:37 -0700 | [diff] [blame] | 909 | do_assignment(exec_list *instructions, struct _mesa_glsl_parse_state *state, |
Iago Toral Quiroga | 4472ab9 | 2014-04-14 09:14:23 +0200 | [diff] [blame] | 910 | const char *non_lvalue_description, |
| 911 | ir_rvalue *lhs, ir_rvalue *rhs, |
Eric Anholt | e9822f7 | 2014-03-05 17:05:54 -0800 | [diff] [blame] | 912 | ir_rvalue **out_rvalue, bool needs_rvalue, |
| 913 | bool is_initializer, |
Iago Toral Quiroga | 4472ab9 | 2014-04-14 09:14:23 +0200 | [diff] [blame] | 914 | YYLTYPE lhs_loc) |
Eric Anholt | 10a6852 | 2010-03-26 11:53:37 -0700 | [diff] [blame] | 915 | { |
Kenneth Graunke | 953ff12 | 2010-06-25 13:14:37 -0700 | [diff] [blame] | 916 | void *ctx = state; |
Eric Anholt | 10a6852 | 2010-03-26 11:53:37 -0700 | [diff] [blame] | 917 | bool error_emitted = (lhs->type->is_error() || rhs->type->is_error()); |
Ian Romanick | 89704eb | 2013-03-15 18:06:11 -0700 | [diff] [blame] | 918 | |
Eric Anholt | f2475ca | 2012-03-29 17:02:15 -0700 | [diff] [blame] | 919 | ir_variable *lhs_var = lhs->variable_referenced(); |
| 920 | if (lhs_var) |
Tapani Pälli | 33ee2c6 | 2013-12-12 13:51:01 +0200 | [diff] [blame] | 921 | lhs_var->data.assigned = true; |
Eric Anholt | f2475ca | 2012-03-29 17:02:15 -0700 | [diff] [blame] | 922 | |
Eric Anholt | 10a6852 | 2010-03-26 11:53:37 -0700 | [diff] [blame] | 923 | if (!error_emitted) { |
Ian Romanick | e9015e9 | 2011-12-23 09:56:29 -0800 | [diff] [blame] | 924 | if (non_lvalue_description != NULL) { |
| 925 | _mesa_glsl_error(&lhs_loc, state, |
| 926 | "assignment to %s", |
Iago Toral Quiroga | 4472ab9 | 2014-04-14 09:14:23 +0200 | [diff] [blame] | 927 | non_lvalue_description); |
| 928 | error_emitted = true; |
Iago Toral Quiroga | 995a719 | 2015-08-05 10:30:46 +0200 | [diff] [blame] | 929 | } else if (lhs_var != NULL && (lhs_var->data.read_only || |
| 930 | (lhs_var->data.mode == ir_var_shader_storage && |
| 931 | lhs_var->data.image_read_only))) { |
| 932 | /* We can have image_read_only set on both images and buffer variables, |
| 933 | * but in the former there is a distinction between assignments to |
| 934 | * the variable itself (read_only) and to the memory they point to |
| 935 | * (image_read_only), while in the case of buffer variables there is |
| 936 | * no such distinction, that is why this check here is limited to |
| 937 | * buffer variables alone. |
| 938 | */ |
Chad Versace | b66be75 | 2011-01-21 13:44:08 -0800 | [diff] [blame] | 939 | _mesa_glsl_error(&lhs_loc, state, |
| 940 | "assignment to read-only variable '%s'", |
Iago Toral Quiroga | a143fbb | 2014-05-09 08:50:03 +0200 | [diff] [blame] | 941 | lhs_var->name); |
Chad Versace | b66be75 | 2011-01-21 13:44:08 -0800 | [diff] [blame] | 942 | error_emitted = true; |
Paul Berry | 0d9bba6 | 2012-08-05 09:57:01 -0700 | [diff] [blame] | 943 | } else if (lhs->type->is_array() && |
Paul Berry | d4a2474 | 2012-08-02 08:18:12 -0700 | [diff] [blame] | 944 | !state->check_version(120, 300, &lhs_loc, |
Paul Berry | 0d9bba6 | 2012-08-05 09:57:01 -0700 | [diff] [blame] | 945 | "whole array assignment forbidden")) { |
Iago Toral Quiroga | 4472ab9 | 2014-04-14 09:14:23 +0200 | [diff] [blame] | 946 | /* From page 32 (page 38 of the PDF) of the GLSL 1.10 spec: |
| 947 | * |
| 948 | * "Other binary or unary expressions, non-dereferenced |
| 949 | * arrays, function names, swizzles with repeated fields, |
| 950 | * and constants cannot be l-values." |
Paul Berry | d4a2474 | 2012-08-02 08:18:12 -0700 | [diff] [blame] | 951 | * |
| 952 | * The restriction on arrays is lifted in GLSL 1.20 and GLSL ES 3.00. |
Iago Toral Quiroga | 4472ab9 | 2014-04-14 09:14:23 +0200 | [diff] [blame] | 953 | */ |
| 954 | error_emitted = true; |
Chad Versace | b66be75 | 2011-01-21 13:44:08 -0800 | [diff] [blame] | 955 | } else if (!lhs->is_lvalue()) { |
Iago Toral Quiroga | 4472ab9 | 2014-04-14 09:14:23 +0200 | [diff] [blame] | 956 | _mesa_glsl_error(& lhs_loc, state, "non-lvalue in assignment"); |
| 957 | error_emitted = true; |
Eric Anholt | 10a6852 | 2010-03-26 11:53:37 -0700 | [diff] [blame] | 958 | } |
| 959 | } |
| 960 | |
Ian Romanick | 85caea2 | 2011-03-15 16:33:27 -0700 | [diff] [blame] | 961 | ir_rvalue *new_rhs = |
Chris Forbes | d563946 | 2014-08-31 19:35:46 +1200 | [diff] [blame] | 962 | validate_assignment(state, lhs_loc, lhs, rhs, is_initializer); |
Timothy Arceri | d1d3b1e | 2013-10-17 22:42:18 +1100 | [diff] [blame] | 963 | if (new_rhs != NULL) { |
Eric Anholt | 10a6852 | 2010-03-26 11:53:37 -0700 | [diff] [blame] | 964 | rhs = new_rhs; |
Ian Romanick | 0157f41 | 2010-04-02 17:44:39 -0700 | [diff] [blame] | 965 | |
| 966 | /* If the LHS array was not declared with a size, it takes it size from |
| 967 | * the RHS. If the LHS is an l-value and a whole array, it must be a |
| 968 | * dereference of a variable. Any other case would require that the LHS |
| 969 | * is either not an l-value or not a whole array. |
| 970 | */ |
Timothy Arceri | b59c592 | 2013-10-23 21:31:27 +1100 | [diff] [blame] | 971 | if (lhs->type->is_unsized_array()) { |
Iago Toral Quiroga | 4472ab9 | 2014-04-14 09:14:23 +0200 | [diff] [blame] | 972 | ir_dereference *const d = lhs->as_dereference(); |
Ian Romanick | 0157f41 | 2010-04-02 17:44:39 -0700 | [diff] [blame] | 973 | |
Iago Toral Quiroga | 4472ab9 | 2014-04-14 09:14:23 +0200 | [diff] [blame] | 974 | assert(d != NULL); |
Ian Romanick | 0157f41 | 2010-04-02 17:44:39 -0700 | [diff] [blame] | 975 | |
Iago Toral Quiroga | 4472ab9 | 2014-04-14 09:14:23 +0200 | [diff] [blame] | 976 | ir_variable *const var = d->variable_referenced(); |
Ian Romanick | 0157f41 | 2010-04-02 17:44:39 -0700 | [diff] [blame] | 977 | |
Iago Toral Quiroga | 4472ab9 | 2014-04-14 09:14:23 +0200 | [diff] [blame] | 978 | assert(var != NULL); |
Ian Romanick | 0157f41 | 2010-04-02 17:44:39 -0700 | [diff] [blame] | 979 | |
Dave Airlie | 8c628ab | 2016-05-20 10:19:14 +1000 | [diff] [blame] | 980 | if (var->data.max_array_access >= rhs->type->array_size()) { |
Iago Toral Quiroga | 4472ab9 | 2014-04-14 09:14:23 +0200 | [diff] [blame] | 981 | /* FINISHME: This should actually log the location of the RHS. */ |
| 982 | _mesa_glsl_error(& lhs_loc, state, "array size must be > %u due to " |
| 983 | "previous access", |
| 984 | var->data.max_array_access); |
| 985 | } |
Ian Romanick | 63f3942 | 2010-04-05 14:35:47 -0700 | [diff] [blame] | 986 | |
Timothy Arceri | d67515b | 2015-04-30 20:45:54 +1000 | [diff] [blame] | 987 | var->type = glsl_type::get_array_instance(lhs->type->fields.array, |
Iago Toral Quiroga | 4472ab9 | 2014-04-14 09:14:23 +0200 | [diff] [blame] | 988 | rhs->type->array_size()); |
| 989 | d->type = var->type; |
Ian Romanick | 0157f41 | 2010-04-02 17:44:39 -0700 | [diff] [blame] | 990 | } |
Timothy Arceri | 3dc932d | 2014-01-23 23:19:54 +1100 | [diff] [blame] | 991 | if (lhs->type->is_array()) { |
| 992 | mark_whole_array_access(rhs); |
| 993 | mark_whole_array_access(lhs); |
| 994 | } |
Eric Anholt | 10a6852 | 2010-03-26 11:53:37 -0700 | [diff] [blame] | 995 | } |
| 996 | |
Eric Anholt | 2731a73 | 2010-06-23 14:51:14 -0700 | [diff] [blame] | 997 | /* Most callers of do_assignment (assign, add_assign, pre_inc/dec, |
| 998 | * but not post_inc) need the converted assigned value as an rvalue |
| 999 | * to handle things like: |
| 1000 | * |
| 1001 | * i = j += 1; |
Eric Anholt | 2731a73 | 2010-06-23 14:51:14 -0700 | [diff] [blame] | 1002 | */ |
Eric Anholt | e9822f7 | 2014-03-05 17:05:54 -0800 | [diff] [blame] | 1003 | if (needs_rvalue) { |
Kenneth Graunke | 9c676a6 | 2016-11-12 11:27:17 -0800 | [diff] [blame] | 1004 | ir_rvalue *rvalue; |
Eric Anholt | e9822f7 | 2014-03-05 17:05:54 -0800 | [diff] [blame] | 1005 | if (!error_emitted) { |
Kenneth Graunke | 9c676a6 | 2016-11-12 11:27:17 -0800 | [diff] [blame] | 1006 | ir_variable *var = new(ctx) ir_variable(rhs->type, "assignment_tmp", |
| 1007 | ir_var_temporary); |
| 1008 | instructions->push_tail(var); |
| 1009 | instructions->push_tail(assign(var, rhs)); |
Eric Anholt | 2731a73 | 2010-06-23 14:51:14 -0700 | [diff] [blame] | 1010 | |
Kenneth Graunke | 9c676a6 | 2016-11-12 11:27:17 -0800 | [diff] [blame] | 1011 | ir_dereference_variable *deref_var = |
| 1012 | new(ctx) ir_dereference_variable(var); |
| 1013 | instructions->push_tail(new(ctx) ir_assignment(lhs, deref_var)); |
| 1014 | rvalue = new(ctx) ir_dereference_variable(var); |
| 1015 | } else { |
| 1016 | rvalue = ir_rvalue::error_value(ctx); |
| 1017 | } |
Eric Anholt | e9822f7 | 2014-03-05 17:05:54 -0800 | [diff] [blame] | 1018 | *out_rvalue = rvalue; |
| 1019 | } else { |
| 1020 | if (!error_emitted) |
| 1021 | instructions->push_tail(new(ctx) ir_assignment(lhs, rhs)); |
| 1022 | *out_rvalue = NULL; |
Kenneth Graunke | 44a86e2 | 2014-01-24 10:47:49 -0800 | [diff] [blame] | 1023 | } |
Eric Anholt | e9822f7 | 2014-03-05 17:05:54 -0800 | [diff] [blame] | 1024 | |
| 1025 | return error_emitted; |
Eric Anholt | 10a6852 | 2010-03-26 11:53:37 -0700 | [diff] [blame] | 1026 | } |
Ian Romanick | 0bb1c3c | 2010-03-23 13:23:31 -0700 | [diff] [blame] | 1027 | |
Eric Anholt | de38f0e | 2010-03-26 12:14:54 -0700 | [diff] [blame] | 1028 | static ir_rvalue * |
Eric Anholt | 959a9ec | 2010-06-23 14:43:50 -0700 | [diff] [blame] | 1029 | get_lvalue_copy(exec_list *instructions, ir_rvalue *lvalue) |
Eric Anholt | de38f0e | 2010-03-26 12:14:54 -0700 | [diff] [blame] | 1030 | { |
Kenneth Graunke | d3073f5 | 2011-01-21 14:32:31 -0800 | [diff] [blame] | 1031 | void *ctx = ralloc_parent(lvalue); |
Eric Anholt | de38f0e | 2010-03-26 12:14:54 -0700 | [diff] [blame] | 1032 | ir_variable *var; |
Eric Anholt | de38f0e | 2010-03-26 12:14:54 -0700 | [diff] [blame] | 1033 | |
Ian Romanick | 7e2aa91 | 2010-07-19 17:12:42 -0700 | [diff] [blame] | 1034 | var = new(ctx) ir_variable(lvalue->type, "_post_incdec_tmp", |
Timothy Arceri | 222f66a | 2016-09-28 16:04:08 +1000 | [diff] [blame] | 1035 | ir_var_temporary); |
Eric Anholt | 43b5b03 | 2010-07-07 14:04:30 -0700 | [diff] [blame] | 1036 | instructions->push_tail(var); |
Eric Anholt | de38f0e | 2010-03-26 12:14:54 -0700 | [diff] [blame] | 1037 | |
Carl Worth | 1660a29 | 2010-06-23 18:11:51 -0700 | [diff] [blame] | 1038 | instructions->push_tail(new(ctx) ir_assignment(new(ctx) ir_dereference_variable(var), |
Timothy Arceri | 222f66a | 2016-09-28 16:04:08 +1000 | [diff] [blame] | 1039 | lvalue)); |
Eric Anholt | de38f0e | 2010-03-26 12:14:54 -0700 | [diff] [blame] | 1040 | |
Carl Worth | 1660a29 | 2010-06-23 18:11:51 -0700 | [diff] [blame] | 1041 | return new(ctx) ir_dereference_variable(var); |
Eric Anholt | de38f0e | 2010-03-26 12:14:54 -0700 | [diff] [blame] | 1042 | } |
| 1043 | |
| 1044 | |
Kenneth Graunke | fb9fb5f | 2010-03-26 00:25:36 -0700 | [diff] [blame] | 1045 | ir_rvalue * |
Iago Toral Quiroga | 4472ab9 | 2014-04-14 09:14:23 +0200 | [diff] [blame] | 1046 | ast_node::hir(exec_list *instructions, struct _mesa_glsl_parse_state *state) |
Ian Romanick | a87ac25 | 2010-02-22 13:19:34 -0800 | [diff] [blame] | 1047 | { |
Ian Romanick | 18238de | 2010-03-01 13:49:10 -0800 | [diff] [blame] | 1048 | (void) instructions; |
| 1049 | (void) state; |
| 1050 | |
| 1051 | return NULL; |
| 1052 | } |
| 1053 | |
Ian Romanick | 05e4601 | 2015-10-07 13:03:53 -0700 | [diff] [blame] | 1054 | bool |
| 1055 | ast_node::has_sequence_subexpression() const |
| 1056 | { |
| 1057 | return false; |
| 1058 | } |
| 1059 | |
Eric Anholt | e9822f7 | 2014-03-05 17:05:54 -0800 | [diff] [blame] | 1060 | void |
Alejandro Piñeiro | b9f90ef | 2016-04-19 11:15:54 +0200 | [diff] [blame] | 1061 | ast_node::set_is_lhs(bool /* new_value */) |
| 1062 | { |
| 1063 | } |
| 1064 | |
| 1065 | void |
Eric Anholt | e9822f7 | 2014-03-05 17:05:54 -0800 | [diff] [blame] | 1066 | ast_function_expression::hir_no_rvalue(exec_list *instructions, |
| 1067 | struct _mesa_glsl_parse_state *state) |
| 1068 | { |
| 1069 | (void)hir(instructions, state); |
| 1070 | } |
| 1071 | |
| 1072 | void |
| 1073 | ast_aggregate_initializer::hir_no_rvalue(exec_list *instructions, |
| 1074 | struct _mesa_glsl_parse_state *state) |
| 1075 | { |
| 1076 | (void)hir(instructions, state); |
| 1077 | } |
| 1078 | |
Eric Anholt | ff79633 | 2010-11-30 11:23:28 -0800 | [diff] [blame] | 1079 | static ir_rvalue * |
| 1080 | do_comparison(void *mem_ctx, int operation, ir_rvalue *op0, ir_rvalue *op1) |
| 1081 | { |
| 1082 | int join_op; |
Ian Romanick | 6d36be5 | 2010-12-02 12:17:36 -0800 | [diff] [blame] | 1083 | ir_rvalue *cmp = NULL; |
Eric Anholt | ff79633 | 2010-11-30 11:23:28 -0800 | [diff] [blame] | 1084 | |
| 1085 | if (operation == ir_binop_all_equal) |
| 1086 | join_op = ir_binop_logic_and; |
| 1087 | else |
| 1088 | join_op = ir_binop_logic_or; |
| 1089 | |
| 1090 | switch (op0->type->base_type) { |
| 1091 | case GLSL_TYPE_FLOAT: |
| 1092 | case GLSL_TYPE_UINT: |
| 1093 | case GLSL_TYPE_INT: |
| 1094 | case GLSL_TYPE_BOOL: |
Dave Airlie | ba3bab2 | 2015-02-05 12:04:58 +0200 | [diff] [blame] | 1095 | case GLSL_TYPE_DOUBLE: |
Eric Anholt | ff79633 | 2010-11-30 11:23:28 -0800 | [diff] [blame] | 1096 | return new(mem_ctx) ir_expression(operation, op0, op1); |
| 1097 | |
| 1098 | case GLSL_TYPE_ARRAY: { |
Eric Anholt | ff79633 | 2010-11-30 11:23:28 -0800 | [diff] [blame] | 1099 | for (unsigned int i = 0; i < op0->type->length; i++) { |
Iago Toral Quiroga | 4472ab9 | 2014-04-14 09:14:23 +0200 | [diff] [blame] | 1100 | ir_rvalue *e0, *e1, *result; |
Eric Anholt | ff79633 | 2010-11-30 11:23:28 -0800 | [diff] [blame] | 1101 | |
Iago Toral Quiroga | 4472ab9 | 2014-04-14 09:14:23 +0200 | [diff] [blame] | 1102 | e0 = new(mem_ctx) ir_dereference_array(op0->clone(mem_ctx, NULL), |
| 1103 | new(mem_ctx) ir_constant(i)); |
| 1104 | e1 = new(mem_ctx) ir_dereference_array(op1->clone(mem_ctx, NULL), |
| 1105 | new(mem_ctx) ir_constant(i)); |
| 1106 | result = do_comparison(mem_ctx, operation, e0, e1); |
Eric Anholt | ff79633 | 2010-11-30 11:23:28 -0800 | [diff] [blame] | 1107 | |
Iago Toral Quiroga | 4472ab9 | 2014-04-14 09:14:23 +0200 | [diff] [blame] | 1108 | if (cmp) { |
| 1109 | cmp = new(mem_ctx) ir_expression(join_op, cmp, result); |
| 1110 | } else { |
| 1111 | cmp = result; |
| 1112 | } |
Eric Anholt | ff79633 | 2010-11-30 11:23:28 -0800 | [diff] [blame] | 1113 | } |
Eric Anholt | b4f5856 | 2010-12-01 15:55:53 -0800 | [diff] [blame] | 1114 | |
| 1115 | mark_whole_array_access(op0); |
| 1116 | mark_whole_array_access(op1); |
Ian Romanick | 6d36be5 | 2010-12-02 12:17:36 -0800 | [diff] [blame] | 1117 | break; |
Eric Anholt | ff79633 | 2010-11-30 11:23:28 -0800 | [diff] [blame] | 1118 | } |
| 1119 | |
| 1120 | case GLSL_TYPE_STRUCT: { |
Eric Anholt | ff79633 | 2010-11-30 11:23:28 -0800 | [diff] [blame] | 1121 | for (unsigned int i = 0; i < op0->type->length; i++) { |
Iago Toral Quiroga | 4472ab9 | 2014-04-14 09:14:23 +0200 | [diff] [blame] | 1122 | ir_rvalue *e0, *e1, *result; |
| 1123 | const char *field_name = op0->type->fields.structure[i].name; |
Eric Anholt | ff79633 | 2010-11-30 11:23:28 -0800 | [diff] [blame] | 1124 | |
Iago Toral Quiroga | 4472ab9 | 2014-04-14 09:14:23 +0200 | [diff] [blame] | 1125 | e0 = new(mem_ctx) ir_dereference_record(op0->clone(mem_ctx, NULL), |
| 1126 | field_name); |
| 1127 | e1 = new(mem_ctx) ir_dereference_record(op1->clone(mem_ctx, NULL), |
| 1128 | field_name); |
| 1129 | result = do_comparison(mem_ctx, operation, e0, e1); |
Eric Anholt | ff79633 | 2010-11-30 11:23:28 -0800 | [diff] [blame] | 1130 | |
Iago Toral Quiroga | 4472ab9 | 2014-04-14 09:14:23 +0200 | [diff] [blame] | 1131 | if (cmp) { |
| 1132 | cmp = new(mem_ctx) ir_expression(join_op, cmp, result); |
| 1133 | } else { |
| 1134 | cmp = result; |
| 1135 | } |
Eric Anholt | ff79633 | 2010-11-30 11:23:28 -0800 | [diff] [blame] | 1136 | } |
Ian Romanick | 6d36be5 | 2010-12-02 12:17:36 -0800 | [diff] [blame] | 1137 | break; |
Eric Anholt | ff79633 | 2010-11-30 11:23:28 -0800 | [diff] [blame] | 1138 | } |
| 1139 | |
| 1140 | case GLSL_TYPE_ERROR: |
| 1141 | case GLSL_TYPE_VOID: |
| 1142 | case GLSL_TYPE_SAMPLER: |
Francisco Jerez | 8a2508e | 2013-11-25 13:50:47 -0800 | [diff] [blame] | 1143 | case GLSL_TYPE_IMAGE: |
Ian Romanick | 491364e | 2012-12-11 12:11:16 -0800 | [diff] [blame] | 1144 | case GLSL_TYPE_INTERFACE: |
Francisco Jerez | 26db3b9 | 2013-10-20 12:35:47 -0700 | [diff] [blame] | 1145 | case GLSL_TYPE_ATOMIC_UINT: |
Dave Airlie | 57f2429 | 2015-04-20 10:16:55 +1000 | [diff] [blame] | 1146 | case GLSL_TYPE_SUBROUTINE: |
Jason Ekstrand | 95ea9f7 | 2016-02-09 18:17:06 -0800 | [diff] [blame] | 1147 | case GLSL_TYPE_FUNCTION: |
Eric Anholt | ff79633 | 2010-11-30 11:23:28 -0800 | [diff] [blame] | 1148 | /* I assume a comparison of a struct containing a sampler just |
| 1149 | * ignores the sampler present in the type. |
| 1150 | */ |
Ian Romanick | 6d36be5 | 2010-12-02 12:17:36 -0800 | [diff] [blame] | 1151 | break; |
Eric Anholt | ff79633 | 2010-11-30 11:23:28 -0800 | [diff] [blame] | 1152 | } |
Eric Anholt | d56c974 | 2010-11-30 13:28:47 -0800 | [diff] [blame] | 1153 | |
Ian Romanick | 6d36be5 | 2010-12-02 12:17:36 -0800 | [diff] [blame] | 1154 | if (cmp == NULL) |
| 1155 | cmp = new(mem_ctx) ir_constant(true); |
| 1156 | |
| 1157 | return cmp; |
Eric Anholt | ff79633 | 2010-11-30 11:23:28 -0800 | [diff] [blame] | 1158 | } |
Ian Romanick | 18238de | 2010-03-01 13:49:10 -0800 | [diff] [blame] | 1159 | |
Eric Anholt | 0182270 | 2011-04-09 15:59:20 -1000 | [diff] [blame] | 1160 | /* For logical operations, we want to ensure that the operands are |
| 1161 | * scalar booleans. If it isn't, emit an error and return a constant |
| 1162 | * boolean to avoid triggering cascading error messages. |
| 1163 | */ |
| 1164 | ir_rvalue * |
| 1165 | get_scalar_boolean_operand(exec_list *instructions, |
Timothy Arceri | 222f66a | 2016-09-28 16:04:08 +1000 | [diff] [blame] | 1166 | struct _mesa_glsl_parse_state *state, |
| 1167 | ast_expression *parent_expr, |
| 1168 | int operand, |
| 1169 | const char *operand_name, |
| 1170 | bool *error_emitted) |
Eric Anholt | 0182270 | 2011-04-09 15:59:20 -1000 | [diff] [blame] | 1171 | { |
| 1172 | ast_expression *expr = parent_expr->subexpressions[operand]; |
| 1173 | void *ctx = state; |
| 1174 | ir_rvalue *val = expr->hir(instructions, state); |
| 1175 | |
| 1176 | if (val->type->is_boolean() && val->type->is_scalar()) |
| 1177 | return val; |
| 1178 | |
| 1179 | if (!*error_emitted) { |
| 1180 | YYLTYPE loc = expr->get_location(); |
| 1181 | _mesa_glsl_error(&loc, state, "%s of `%s' must be scalar boolean", |
Iago Toral Quiroga | 4472ab9 | 2014-04-14 09:14:23 +0200 | [diff] [blame] | 1182 | operand_name, |
| 1183 | parent_expr->operator_string(parent_expr->oper)); |
Eric Anholt | 0182270 | 2011-04-09 15:59:20 -1000 | [diff] [blame] | 1184 | *error_emitted = true; |
| 1185 | } |
| 1186 | |
| 1187 | return new(ctx) ir_constant(true); |
| 1188 | } |
| 1189 | |
Paul Berry | 93b9758 | 2011-09-06 10:01:51 -0700 | [diff] [blame] | 1190 | /** |
| 1191 | * If name refers to a builtin array whose maximum allowed size is less than |
| 1192 | * size, report an error and return true. Otherwise return false. |
| 1193 | */ |
Ian Romanick | 2c333a8 | 2013-03-15 14:33:01 -0700 | [diff] [blame] | 1194 | void |
Paul Berry | 93b9758 | 2011-09-06 10:01:51 -0700 | [diff] [blame] | 1195 | check_builtin_array_max_size(const char *name, unsigned size, |
| 1196 | YYLTYPE loc, struct _mesa_glsl_parse_state *state) |
| 1197 | { |
| 1198 | if ((strcmp("gl_TexCoord", name) == 0) |
| 1199 | && (size > state->Const.MaxTextureCoords)) { |
| 1200 | /* From page 54 (page 60 of the PDF) of the GLSL 1.20 spec: |
| 1201 | * |
| 1202 | * "The size [of gl_TexCoord] can be at most |
| 1203 | * gl_MaxTextureCoords." |
| 1204 | */ |
| 1205 | _mesa_glsl_error(&loc, state, "`gl_TexCoord' array size cannot " |
Paul Berry | 4d7899f | 2013-07-25 19:56:43 -0700 | [diff] [blame] | 1206 | "be larger than gl_MaxTextureCoords (%u)", |
Paul Berry | 93b9758 | 2011-09-06 10:01:51 -0700 | [diff] [blame] | 1207 | state->Const.MaxTextureCoords); |
Tobias Klausmann | d656736 | 2016-05-08 22:44:06 +0200 | [diff] [blame] | 1208 | } else if (strcmp("gl_ClipDistance", name) == 0) { |
| 1209 | state->clip_dist_size = size; |
| 1210 | if (size + state->cull_dist_size > state->Const.MaxClipPlanes) { |
| 1211 | /* From section 7.1 (Vertex Shader Special Variables) of the |
| 1212 | * GLSL 1.30 spec: |
| 1213 | * |
| 1214 | * "The gl_ClipDistance array is predeclared as unsized and |
| 1215 | * must be sized by the shader either redeclaring it with a |
| 1216 | * size or indexing it only with integral constant |
| 1217 | * expressions. ... The size can be at most |
| 1218 | * gl_MaxClipDistances." |
| 1219 | */ |
| 1220 | _mesa_glsl_error(&loc, state, "`gl_ClipDistance' array size cannot " |
| 1221 | "be larger than gl_MaxClipDistances (%u)", |
| 1222 | state->Const.MaxClipPlanes); |
| 1223 | } |
| 1224 | } else if (strcmp("gl_CullDistance", name) == 0) { |
| 1225 | state->cull_dist_size = size; |
| 1226 | if (size + state->clip_dist_size > state->Const.MaxClipPlanes) { |
| 1227 | /* From the ARB_cull_distance spec: |
| 1228 | * |
| 1229 | * "The gl_CullDistance array is predeclared as unsized and |
| 1230 | * must be sized by the shader either redeclaring it with |
| 1231 | * a size or indexing it only with integral constant |
| 1232 | * expressions. The size determines the number and set of |
| 1233 | * enabled cull distances and can be at most |
| 1234 | * gl_MaxCullDistances." |
| 1235 | */ |
| 1236 | _mesa_glsl_error(&loc, state, "`gl_CullDistance' array size cannot " |
| 1237 | "be larger than gl_MaxCullDistances (%u)", |
| 1238 | state->Const.MaxClipPlanes); |
| 1239 | } |
Paul Berry | 93b9758 | 2011-09-06 10:01:51 -0700 | [diff] [blame] | 1240 | } |
Paul Berry | 93b9758 | 2011-09-06 10:01:51 -0700 | [diff] [blame] | 1241 | } |
| 1242 | |
Paul Berry | 9abda92 | 2011-10-31 18:22:48 -0700 | [diff] [blame] | 1243 | /** |
| 1244 | * Create the constant 1, of a which is appropriate for incrementing and |
| 1245 | * decrementing values of the given GLSL type. For example, if type is vec4, |
| 1246 | * this creates a constant value of 1.0 having type float. |
| 1247 | * |
| 1248 | * If the given type is invalid for increment and decrement operators, return |
| 1249 | * a floating point 1--the error will be detected later. |
| 1250 | */ |
| 1251 | static ir_rvalue * |
| 1252 | constant_one_for_inc_dec(void *ctx, const glsl_type *type) |
| 1253 | { |
| 1254 | switch (type->base_type) { |
| 1255 | case GLSL_TYPE_UINT: |
| 1256 | return new(ctx) ir_constant((unsigned) 1); |
| 1257 | case GLSL_TYPE_INT: |
| 1258 | return new(ctx) ir_constant(1); |
| 1259 | default: |
| 1260 | case GLSL_TYPE_FLOAT: |
| 1261 | return new(ctx) ir_constant(1.0f); |
| 1262 | } |
| 1263 | } |
| 1264 | |
Kenneth Graunke | fb9fb5f | 2010-03-26 00:25:36 -0700 | [diff] [blame] | 1265 | ir_rvalue * |
Ian Romanick | 0044e7e | 2010-03-08 23:44:00 -0800 | [diff] [blame] | 1266 | ast_expression::hir(exec_list *instructions, |
Iago Toral Quiroga | 4472ab9 | 2014-04-14 09:14:23 +0200 | [diff] [blame] | 1267 | struct _mesa_glsl_parse_state *state) |
Ian Romanick | 18238de | 2010-03-01 13:49:10 -0800 | [diff] [blame] | 1268 | { |
Eric Anholt | e9822f7 | 2014-03-05 17:05:54 -0800 | [diff] [blame] | 1269 | return do_hir(instructions, state, true); |
| 1270 | } |
| 1271 | |
| 1272 | void |
| 1273 | ast_expression::hir_no_rvalue(exec_list *instructions, |
| 1274 | struct _mesa_glsl_parse_state *state) |
| 1275 | { |
| 1276 | do_hir(instructions, state, false); |
| 1277 | } |
| 1278 | |
Alejandro Piñeiro | 8568d02 | 2016-02-25 11:11:54 +0100 | [diff] [blame] | 1279 | void |
| 1280 | ast_expression::set_is_lhs(bool new_value) |
| 1281 | { |
| 1282 | /* is_lhs is tracked only to print "variable used uninitialized" warnings, |
Giuseppe Bilotta | 60a27ad | 2016-06-23 19:20:18 +0200 | [diff] [blame] | 1283 | * if we lack an identifier we can just skip it. |
Alejandro Piñeiro | 8568d02 | 2016-02-25 11:11:54 +0100 | [diff] [blame] | 1284 | */ |
| 1285 | if (this->primary_expression.identifier == NULL) |
| 1286 | return; |
| 1287 | |
| 1288 | this->is_lhs = new_value; |
| 1289 | |
| 1290 | /* We need to go through the subexpressions tree to cover cases like |
| 1291 | * ast_field_selection |
| 1292 | */ |
| 1293 | if (this->subexpressions[0] != NULL) |
| 1294 | this->subexpressions[0]->set_is_lhs(new_value); |
| 1295 | } |
| 1296 | |
Eric Anholt | e9822f7 | 2014-03-05 17:05:54 -0800 | [diff] [blame] | 1297 | ir_rvalue * |
| 1298 | ast_expression::do_hir(exec_list *instructions, |
| 1299 | struct _mesa_glsl_parse_state *state, |
| 1300 | bool needs_rvalue) |
| 1301 | { |
Kenneth Graunke | 953ff12 | 2010-06-25 13:14:37 -0700 | [diff] [blame] | 1302 | void *ctx = state; |
Ian Romanick | a87ac25 | 2010-02-22 13:19:34 -0800 | [diff] [blame] | 1303 | static const int operations[AST_NUM_OPERATORS] = { |
| 1304 | -1, /* ast_assign doesn't convert to ir_expression. */ |
| 1305 | -1, /* ast_plus doesn't convert to ir_expression. */ |
| 1306 | ir_unop_neg, |
| 1307 | ir_binop_add, |
| 1308 | ir_binop_sub, |
| 1309 | ir_binop_mul, |
| 1310 | ir_binop_div, |
| 1311 | ir_binop_mod, |
| 1312 | ir_binop_lshift, |
| 1313 | ir_binop_rshift, |
| 1314 | ir_binop_less, |
| 1315 | ir_binop_greater, |
| 1316 | ir_binop_lequal, |
| 1317 | ir_binop_gequal, |
Luca Barbieri | 4dfb899 | 2010-09-08 01:31:39 +0200 | [diff] [blame] | 1318 | ir_binop_all_equal, |
| 1319 | ir_binop_any_nequal, |
Ian Romanick | a87ac25 | 2010-02-22 13:19:34 -0800 | [diff] [blame] | 1320 | ir_binop_bit_and, |
| 1321 | ir_binop_bit_xor, |
| 1322 | ir_binop_bit_or, |
| 1323 | ir_unop_bit_not, |
| 1324 | ir_binop_logic_and, |
| 1325 | ir_binop_logic_xor, |
| 1326 | ir_binop_logic_or, |
| 1327 | ir_unop_logic_not, |
| 1328 | |
| 1329 | /* Note: The following block of expression types actually convert |
| 1330 | * to multiple IR instructions. |
| 1331 | */ |
| 1332 | ir_binop_mul, /* ast_mul_assign */ |
| 1333 | ir_binop_div, /* ast_div_assign */ |
| 1334 | ir_binop_mod, /* ast_mod_assign */ |
| 1335 | ir_binop_add, /* ast_add_assign */ |
| 1336 | ir_binop_sub, /* ast_sub_assign */ |
| 1337 | ir_binop_lshift, /* ast_ls_assign */ |
| 1338 | ir_binop_rshift, /* ast_rs_assign */ |
| 1339 | ir_binop_bit_and, /* ast_and_assign */ |
| 1340 | ir_binop_bit_xor, /* ast_xor_assign */ |
| 1341 | ir_binop_bit_or, /* ast_or_assign */ |
| 1342 | |
| 1343 | -1, /* ast_conditional doesn't convert to ir_expression. */ |
Eric Anholt | de38f0e | 2010-03-26 12:14:54 -0700 | [diff] [blame] | 1344 | ir_binop_add, /* ast_pre_inc. */ |
| 1345 | ir_binop_sub, /* ast_pre_dec. */ |
| 1346 | ir_binop_add, /* ast_post_inc. */ |
| 1347 | ir_binop_sub, /* ast_post_dec. */ |
Ian Romanick | a87ac25 | 2010-02-22 13:19:34 -0800 | [diff] [blame] | 1348 | -1, /* ast_field_selection doesn't conv to ir_expression. */ |
| 1349 | -1, /* ast_array_index doesn't convert to ir_expression. */ |
| 1350 | -1, /* ast_function_call doesn't conv to ir_expression. */ |
| 1351 | -1, /* ast_identifier doesn't convert to ir_expression. */ |
| 1352 | -1, /* ast_int_constant doesn't convert to ir_expression. */ |
| 1353 | -1, /* ast_uint_constant doesn't conv to ir_expression. */ |
| 1354 | -1, /* ast_float_constant doesn't conv to ir_expression. */ |
| 1355 | -1, /* ast_bool_constant doesn't conv to ir_expression. */ |
| 1356 | -1, /* ast_sequence doesn't convert to ir_expression. */ |
Andres Gomez | 3356ac2 | 2016-07-31 19:07:34 +0300 | [diff] [blame] | 1357 | -1, /* ast_aggregate shouldn't ever even get here. */ |
Ian Romanick | a87ac25 | 2010-02-22 13:19:34 -0800 | [diff] [blame] | 1358 | }; |
Kenneth Graunke | fb9fb5f | 2010-03-26 00:25:36 -0700 | [diff] [blame] | 1359 | ir_rvalue *result = NULL; |
Aras Pranckevicius | 1c325af | 2010-07-29 15:35:22 +0300 | [diff] [blame] | 1360 | ir_rvalue *op[3]; |
Ilia Mirkin | a32c87f | 2016-07-08 23:28:22 -0400 | [diff] [blame] | 1361 | const struct glsl_type *type, *orig_type; |
Ian Romanick | a87ac25 | 2010-02-22 13:19:34 -0800 | [diff] [blame] | 1362 | bool error_emitted = false; |
| 1363 | YYLTYPE loc; |
| 1364 | |
Ian Romanick | 18238de | 2010-03-01 13:49:10 -0800 | [diff] [blame] | 1365 | loc = this->get_location(); |
Ian Romanick | a87ac25 | 2010-02-22 13:19:34 -0800 | [diff] [blame] | 1366 | |
Ian Romanick | 18238de | 2010-03-01 13:49:10 -0800 | [diff] [blame] | 1367 | switch (this->oper) { |
Matt Turner | ae79e86 | 2013-06-29 19:27:50 -0700 | [diff] [blame] | 1368 | case ast_aggregate: |
Iago Toral Quiroga | 4472ab9 | 2014-04-14 09:14:23 +0200 | [diff] [blame] | 1369 | assert(!"ast_aggregate: Should never get here."); |
| 1370 | break; |
Matt Turner | ae79e86 | 2013-06-29 19:27:50 -0700 | [diff] [blame] | 1371 | |
Ian Romanick | 6652af3 | 2010-03-09 16:38:02 -0800 | [diff] [blame] | 1372 | case ast_assign: { |
Alejandro Piñeiro | 8568d02 | 2016-02-25 11:11:54 +0100 | [diff] [blame] | 1373 | this->subexpressions[0]->set_is_lhs(true); |
Ian Romanick | 18238de | 2010-03-01 13:49:10 -0800 | [diff] [blame] | 1374 | op[0] = this->subexpressions[0]->hir(instructions, state); |
| 1375 | op[1] = this->subexpressions[1]->hir(instructions, state); |
Ian Romanick | a87ac25 | 2010-02-22 13:19:34 -0800 | [diff] [blame] | 1376 | |
Eric Anholt | e9822f7 | 2014-03-05 17:05:54 -0800 | [diff] [blame] | 1377 | error_emitted = |
| 1378 | do_assignment(instructions, state, |
| 1379 | this->subexpressions[0]->non_lvalue_description, |
| 1380 | op[0], op[1], &result, needs_rvalue, false, |
| 1381 | this->subexpressions[0]->get_location()); |
Ian Romanick | a87ac25 | 2010-02-22 13:19:34 -0800 | [diff] [blame] | 1382 | break; |
Ian Romanick | 6652af3 | 2010-03-09 16:38:02 -0800 | [diff] [blame] | 1383 | } |
Ian Romanick | a87ac25 | 2010-02-22 13:19:34 -0800 | [diff] [blame] | 1384 | |
| 1385 | case ast_plus: |
Ian Romanick | 18238de | 2010-03-01 13:49:10 -0800 | [diff] [blame] | 1386 | op[0] = this->subexpressions[0]->hir(instructions, state); |
Ian Romanick | a87ac25 | 2010-02-22 13:19:34 -0800 | [diff] [blame] | 1387 | |
Carl Worth | c24bcad | 2010-07-21 11:23:51 -0700 | [diff] [blame] | 1388 | type = unary_arithmetic_result_type(op[0]->type, state, & loc); |
| 1389 | |
| 1390 | error_emitted = type->is_error(); |
Ian Romanick | a87ac25 | 2010-02-22 13:19:34 -0800 | [diff] [blame] | 1391 | |
| 1392 | result = op[0]; |
| 1393 | break; |
| 1394 | |
| 1395 | case ast_neg: |
Ian Romanick | 18238de | 2010-03-01 13:49:10 -0800 | [diff] [blame] | 1396 | op[0] = this->subexpressions[0]->hir(instructions, state); |
Ian Romanick | a87ac25 | 2010-02-22 13:19:34 -0800 | [diff] [blame] | 1397 | |
Eric Anholt | 65e1a7ac | 2010-03-31 16:45:20 -1000 | [diff] [blame] | 1398 | type = unary_arithmetic_result_type(op[0]->type, state, & loc); |
Ian Romanick | a87ac25 | 2010-02-22 13:19:34 -0800 | [diff] [blame] | 1399 | |
Eric Anholt | 65e1a7ac | 2010-03-31 16:45:20 -1000 | [diff] [blame] | 1400 | error_emitted = type->is_error(); |
Ian Romanick | a87ac25 | 2010-02-22 13:19:34 -0800 | [diff] [blame] | 1401 | |
Carl Worth | 1660a29 | 2010-06-23 18:11:51 -0700 | [diff] [blame] | 1402 | result = new(ctx) ir_expression(operations[this->oper], type, |
Iago Toral Quiroga | 4472ab9 | 2014-04-14 09:14:23 +0200 | [diff] [blame] | 1403 | op[0], NULL); |
Ian Romanick | a87ac25 | 2010-02-22 13:19:34 -0800 | [diff] [blame] | 1404 | break; |
| 1405 | |
| 1406 | case ast_add: |
| 1407 | case ast_sub: |
| 1408 | case ast_mul: |
| 1409 | case ast_div: |
Ian Romanick | 18238de | 2010-03-01 13:49:10 -0800 | [diff] [blame] | 1410 | op[0] = this->subexpressions[0]->hir(instructions, state); |
| 1411 | op[1] = this->subexpressions[1]->hir(instructions, state); |
Ian Romanick | a87ac25 | 2010-02-22 13:19:34 -0800 | [diff] [blame] | 1412 | |
Ian Romanick | bfb09c2 | 2010-03-29 16:32:55 -0700 | [diff] [blame] | 1413 | type = arithmetic_result_type(op[0], op[1], |
Iago Toral Quiroga | 4472ab9 | 2014-04-14 09:14:23 +0200 | [diff] [blame] | 1414 | (this->oper == ast_mul), |
| 1415 | state, & loc); |
Eric Anholt | a13bb14 | 2010-03-31 16:38:11 -1000 | [diff] [blame] | 1416 | error_emitted = type->is_error(); |
Ian Romanick | a87ac25 | 2010-02-22 13:19:34 -0800 | [diff] [blame] | 1417 | |
Carl Worth | 1660a29 | 2010-06-23 18:11:51 -0700 | [diff] [blame] | 1418 | result = new(ctx) ir_expression(operations[this->oper], type, |
Iago Toral Quiroga | 4472ab9 | 2014-04-14 09:14:23 +0200 | [diff] [blame] | 1419 | op[0], op[1]); |
Ian Romanick | a87ac25 | 2010-02-22 13:19:34 -0800 | [diff] [blame] | 1420 | break; |
| 1421 | |
| 1422 | case ast_mod: |
Ian Romanick | 18238de | 2010-03-01 13:49:10 -0800 | [diff] [blame] | 1423 | op[0] = this->subexpressions[0]->hir(instructions, state); |
| 1424 | op[1] = this->subexpressions[1]->hir(instructions, state); |
Ian Romanick | a87ac25 | 2010-02-22 13:19:34 -0800 | [diff] [blame] | 1425 | |
Kenneth Graunke | 511de1a | 2015-11-12 13:02:05 -0800 | [diff] [blame] | 1426 | type = modulus_result_type(op[0], op[1], state, &loc); |
Ian Romanick | a87ac25 | 2010-02-22 13:19:34 -0800 | [diff] [blame] | 1427 | |
Ian Romanick | 18238de | 2010-03-01 13:49:10 -0800 | [diff] [blame] | 1428 | assert(operations[this->oper] == ir_binop_mod); |
Ian Romanick | a87ac25 | 2010-02-22 13:19:34 -0800 | [diff] [blame] | 1429 | |
Carl Worth | 1660a29 | 2010-06-23 18:11:51 -0700 | [diff] [blame] | 1430 | result = new(ctx) ir_expression(operations[this->oper], type, |
Iago Toral Quiroga | 4472ab9 | 2014-04-14 09:14:23 +0200 | [diff] [blame] | 1431 | op[0], op[1]); |
Eric Anholt | 65e1a7ac | 2010-03-31 16:45:20 -1000 | [diff] [blame] | 1432 | error_emitted = type->is_error(); |
Ian Romanick | a87ac25 | 2010-02-22 13:19:34 -0800 | [diff] [blame] | 1433 | break; |
| 1434 | |
| 1435 | case ast_lshift: |
| 1436 | case ast_rshift: |
Paul Berry | 0d9bba6 | 2012-08-05 09:57:01 -0700 | [diff] [blame] | 1437 | if (!state->check_bitwise_operations_allowed(&loc)) { |
Chad Versace | 5c4c36f | 2010-10-08 16:22:28 -0700 | [diff] [blame] | 1438 | error_emitted = true; |
Chad Versace | 5c4c36f | 2010-10-08 16:22:28 -0700 | [diff] [blame] | 1439 | } |
| 1440 | |
Chad Versace | 5c4c36f | 2010-10-08 16:22:28 -0700 | [diff] [blame] | 1441 | op[0] = this->subexpressions[0]->hir(instructions, state); |
| 1442 | op[1] = this->subexpressions[1]->hir(instructions, state); |
Chad Versace | c0197ab | 2010-10-15 09:49:46 -0700 | [diff] [blame] | 1443 | type = shift_result_type(op[0]->type, op[1]->type, this->oper, state, |
| 1444 | &loc); |
Chad Versace | 5c4c36f | 2010-10-08 16:22:28 -0700 | [diff] [blame] | 1445 | result = new(ctx) ir_expression(operations[this->oper], type, |
| 1446 | op[0], op[1]); |
| 1447 | error_emitted = op[0]->type->is_error() || op[1]->type->is_error(); |
| 1448 | break; |
Ian Romanick | a87ac25 | 2010-02-22 13:19:34 -0800 | [diff] [blame] | 1449 | |
| 1450 | case ast_less: |
| 1451 | case ast_greater: |
| 1452 | case ast_lequal: |
| 1453 | case ast_gequal: |
Ian Romanick | 18238de | 2010-03-01 13:49:10 -0800 | [diff] [blame] | 1454 | op[0] = this->subexpressions[0]->hir(instructions, state); |
| 1455 | op[1] = this->subexpressions[1]->hir(instructions, state); |
Ian Romanick | a87ac25 | 2010-02-22 13:19:34 -0800 | [diff] [blame] | 1456 | |
Eric Anholt | 65e1a7ac | 2010-03-31 16:45:20 -1000 | [diff] [blame] | 1457 | type = relational_result_type(op[0], op[1], state, & loc); |
Ian Romanick | a87ac25 | 2010-02-22 13:19:34 -0800 | [diff] [blame] | 1458 | |
| 1459 | /* The relational operators must either generate an error or result |
| 1460 | * in a scalar boolean. See page 57 of the GLSL 1.50 spec. |
| 1461 | */ |
Ian Romanick | a43817a | 2010-03-26 14:27:23 -0700 | [diff] [blame] | 1462 | assert(type->is_error() |
Timothy Arceri | 222f66a | 2016-09-28 16:04:08 +1000 | [diff] [blame] | 1463 | || ((type->base_type == GLSL_TYPE_BOOL) |
| 1464 | && type->is_scalar())); |
Ian Romanick | a87ac25 | 2010-02-22 13:19:34 -0800 | [diff] [blame] | 1465 | |
Carl Worth | 1660a29 | 2010-06-23 18:11:51 -0700 | [diff] [blame] | 1466 | result = new(ctx) ir_expression(operations[this->oper], type, |
Iago Toral Quiroga | 4472ab9 | 2014-04-14 09:14:23 +0200 | [diff] [blame] | 1467 | op[0], op[1]); |
Eric Anholt | 65e1a7ac | 2010-03-31 16:45:20 -1000 | [diff] [blame] | 1468 | error_emitted = type->is_error(); |
Ian Romanick | a87ac25 | 2010-02-22 13:19:34 -0800 | [diff] [blame] | 1469 | break; |
| 1470 | |
| 1471 | case ast_nequal: |
| 1472 | case ast_equal: |
Ian Romanick | 6e659ca | 2010-03-29 15:11:05 -0700 | [diff] [blame] | 1473 | op[0] = this->subexpressions[0]->hir(instructions, state); |
| 1474 | op[1] = this->subexpressions[1]->hir(instructions, state); |
| 1475 | |
| 1476 | /* From page 58 (page 64 of the PDF) of the GLSL 1.50 spec: |
| 1477 | * |
| 1478 | * "The equality operators equal (==), and not equal (!=) |
| 1479 | * operate on all types. They result in a scalar Boolean. If |
| 1480 | * the operand types do not match, then there must be a |
| 1481 | * conversion from Section 4.1.10 "Implicit Conversions" |
| 1482 | * applied to one operand that can make them match, in which |
| 1483 | * case this conversion is done." |
| 1484 | */ |
Renaud Gaubert | 7b9ebf8 | 2015-07-11 19:38:10 +0200 | [diff] [blame] | 1485 | |
| 1486 | if (op[0]->type == glsl_type::void_type || op[1]->type == glsl_type::void_type) { |
| 1487 | _mesa_glsl_error(& loc, state, "`%s': wrong operand types: " |
| 1488 | "no operation `%1$s' exists that takes a left-hand " |
| 1489 | "operand of type 'void' or a right operand of type " |
| 1490 | "'void'", (this->oper == ast_equal) ? "==" : "!="); |
| 1491 | error_emitted = true; |
| 1492 | } else if ((!apply_implicit_conversion(op[0]->type, op[1], state) |
Iago Toral Quiroga | 4472ab9 | 2014-04-14 09:14:23 +0200 | [diff] [blame] | 1493 | && !apply_implicit_conversion(op[1]->type, op[0], state)) |
| 1494 | || (op[0]->type != op[1]->type)) { |
| 1495 | _mesa_glsl_error(& loc, state, "operands of `%s' must have the same " |
| 1496 | "type", (this->oper == ast_equal) ? "==" : "!="); |
| 1497 | error_emitted = true; |
Paul Berry | 0d9bba6 | 2012-08-05 09:57:01 -0700 | [diff] [blame] | 1498 | } else if ((op[0]->type->is_array() || op[1]->type->is_array()) && |
Paul Berry | d4a2474 | 2012-08-02 08:18:12 -0700 | [diff] [blame] | 1499 | !state->check_version(120, 300, &loc, |
Paul Berry | 0d9bba6 | 2012-08-05 09:57:01 -0700 | [diff] [blame] | 1500 | "array comparisons forbidden")) { |
Iago Toral Quiroga | 4472ab9 | 2014-04-14 09:14:23 +0200 | [diff] [blame] | 1501 | error_emitted = true; |
Andres Gomez | d068b38 | 2016-07-18 16:39:43 +0300 | [diff] [blame] | 1502 | } else if ((op[0]->type->contains_subroutine() || |
| 1503 | op[1]->type->contains_subroutine())) { |
| 1504 | _mesa_glsl_error(&loc, state, "subroutine comparisons forbidden"); |
| 1505 | error_emitted = true; |
Francisco Jerez | cc744a0 | 2013-09-20 14:58:03 -0700 | [diff] [blame] | 1506 | } else if ((op[0]->type->contains_opaque() || |
| 1507 | op[1]->type->contains_opaque())) { |
| 1508 | _mesa_glsl_error(&loc, state, "opaque type comparisons forbidden"); |
| 1509 | error_emitted = true; |
Ian Romanick | 6e659ca | 2010-03-29 15:11:05 -0700 | [diff] [blame] | 1510 | } |
| 1511 | |
Eric Anholt | 175829f | 2011-04-09 12:54:34 -1000 | [diff] [blame] | 1512 | if (error_emitted) { |
Iago Toral Quiroga | 4472ab9 | 2014-04-14 09:14:23 +0200 | [diff] [blame] | 1513 | result = new(ctx) ir_constant(false); |
Eric Anholt | 175829f | 2011-04-09 12:54:34 -1000 | [diff] [blame] | 1514 | } else { |
Iago Toral Quiroga | 4472ab9 | 2014-04-14 09:14:23 +0200 | [diff] [blame] | 1515 | result = do_comparison(ctx, operations[this->oper], op[0], op[1]); |
| 1516 | assert(result->type == glsl_type::bool_type); |
Eric Anholt | 175829f | 2011-04-09 12:54:34 -1000 | [diff] [blame] | 1517 | } |
Ian Romanick | a87ac25 | 2010-02-22 13:19:34 -0800 | [diff] [blame] | 1518 | break; |
| 1519 | |
| 1520 | case ast_bit_and: |
| 1521 | case ast_bit_xor: |
| 1522 | case ast_bit_or: |
Kenneth Graunke | 1eea963 | 2010-08-31 10:56:24 -0700 | [diff] [blame] | 1523 | op[0] = this->subexpressions[0]->hir(instructions, state); |
| 1524 | op[1] = this->subexpressions[1]->hir(instructions, state); |
Kenneth Graunke | d54a70a | 2016-01-14 23:27:03 -0800 | [diff] [blame] | 1525 | type = bit_logic_result_type(op[0], op[1], this->oper, state, &loc); |
Kenneth Graunke | 1eea963 | 2010-08-31 10:56:24 -0700 | [diff] [blame] | 1526 | result = new(ctx) ir_expression(operations[this->oper], type, |
Iago Toral Quiroga | 4472ab9 | 2014-04-14 09:14:23 +0200 | [diff] [blame] | 1527 | op[0], op[1]); |
Kenneth Graunke | 1eea963 | 2010-08-31 10:56:24 -0700 | [diff] [blame] | 1528 | error_emitted = op[0]->type->is_error() || op[1]->type->is_error(); |
| 1529 | break; |
| 1530 | |
Ian Romanick | a87ac25 | 2010-02-22 13:19:34 -0800 | [diff] [blame] | 1531 | case ast_bit_not: |
Kenneth Graunke | 1eea963 | 2010-08-31 10:56:24 -0700 | [diff] [blame] | 1532 | op[0] = this->subexpressions[0]->hir(instructions, state); |
| 1533 | |
Paul Berry | 0d9bba6 | 2012-08-05 09:57:01 -0700 | [diff] [blame] | 1534 | if (!state->check_bitwise_operations_allowed(&loc)) { |
Iago Toral Quiroga | 4472ab9 | 2014-04-14 09:14:23 +0200 | [diff] [blame] | 1535 | error_emitted = true; |
Kenneth Graunke | 1eea963 | 2010-08-31 10:56:24 -0700 | [diff] [blame] | 1536 | } |
| 1537 | |
| 1538 | if (!op[0]->type->is_integer()) { |
Iago Toral Quiroga | 4472ab9 | 2014-04-14 09:14:23 +0200 | [diff] [blame] | 1539 | _mesa_glsl_error(&loc, state, "operand of `~' must be an integer"); |
| 1540 | error_emitted = true; |
Kenneth Graunke | 1eea963 | 2010-08-31 10:56:24 -0700 | [diff] [blame] | 1541 | } |
| 1542 | |
Ian Romanick | 3946448 | 2011-12-23 17:16:43 -0800 | [diff] [blame] | 1543 | type = error_emitted ? glsl_type::error_type : op[0]->type; |
Kenneth Graunke | 1eea963 | 2010-08-31 10:56:24 -0700 | [diff] [blame] | 1544 | result = new(ctx) ir_expression(ir_unop_bit_not, type, op[0], NULL); |
Ian Romanick | a87ac25 | 2010-02-22 13:19:34 -0800 | [diff] [blame] | 1545 | break; |
| 1546 | |
Eric Anholt | 4950a68 | 2010-04-16 01:10:32 -0700 | [diff] [blame] | 1547 | case ast_logic_and: { |
Eric Anholt | 7ec0c97 | 2011-04-09 10:27:02 -1000 | [diff] [blame] | 1548 | exec_list rhs_instructions; |
Eric Anholt | 0182270 | 2011-04-09 15:59:20 -1000 | [diff] [blame] | 1549 | op[0] = get_scalar_boolean_operand(instructions, state, this, 0, |
Iago Toral Quiroga | 4472ab9 | 2014-04-14 09:14:23 +0200 | [diff] [blame] | 1550 | "LHS", &error_emitted); |
Eric Anholt | 7ec0c97 | 2011-04-09 10:27:02 -1000 | [diff] [blame] | 1551 | op[1] = get_scalar_boolean_operand(&rhs_instructions, state, this, 1, |
Iago Toral Quiroga | 4472ab9 | 2014-04-14 09:14:23 +0200 | [diff] [blame] | 1552 | "RHS", &error_emitted); |
Eric Anholt | b82c0c3 | 2010-03-31 09:11:39 -1000 | [diff] [blame] | 1553 | |
Eric Anholt | ead3589 | 2012-02-07 22:59:24 -0800 | [diff] [blame] | 1554 | if (rhs_instructions.is_empty()) { |
Iago Toral Quiroga | 4472ab9 | 2014-04-14 09:14:23 +0200 | [diff] [blame] | 1555 | result = new(ctx) ir_expression(ir_binop_logic_and, op[0], op[1]); |
| 1556 | type = result->type; |
Eric Anholt | 44b694e | 2010-04-16 13:49:04 -0700 | [diff] [blame] | 1557 | } else { |
Iago Toral Quiroga | 4472ab9 | 2014-04-14 09:14:23 +0200 | [diff] [blame] | 1558 | ir_variable *const tmp = new(ctx) ir_variable(glsl_type::bool_type, |
| 1559 | "and_tmp", |
| 1560 | ir_var_temporary); |
| 1561 | instructions->push_tail(tmp); |
Ian Romanick | 81d664f | 2010-07-12 15:18:55 -0700 | [diff] [blame] | 1562 | |
Iago Toral Quiroga | 4472ab9 | 2014-04-14 09:14:23 +0200 | [diff] [blame] | 1563 | ir_if *const stmt = new(ctx) ir_if(op[0]); |
| 1564 | instructions->push_tail(stmt); |
Eric Anholt | 4950a68 | 2010-04-16 01:10:32 -0700 | [diff] [blame] | 1565 | |
Iago Toral Quiroga | 4472ab9 | 2014-04-14 09:14:23 +0200 | [diff] [blame] | 1566 | stmt->then_instructions.append_list(&rhs_instructions); |
| 1567 | ir_dereference *const then_deref = new(ctx) ir_dereference_variable(tmp); |
| 1568 | ir_assignment *const then_assign = |
| 1569 | new(ctx) ir_assignment(then_deref, op[1]); |
| 1570 | stmt->then_instructions.push_tail(then_assign); |
Eric Anholt | 44b694e | 2010-04-16 13:49:04 -0700 | [diff] [blame] | 1571 | |
Iago Toral Quiroga | 4472ab9 | 2014-04-14 09:14:23 +0200 | [diff] [blame] | 1572 | ir_dereference *const else_deref = new(ctx) ir_dereference_variable(tmp); |
| 1573 | ir_assignment *const else_assign = |
| 1574 | new(ctx) ir_assignment(else_deref, new(ctx) ir_constant(false)); |
| 1575 | stmt->else_instructions.push_tail(else_assign); |
Eric Anholt | 44b694e | 2010-04-16 13:49:04 -0700 | [diff] [blame] | 1576 | |
Iago Toral Quiroga | 4472ab9 | 2014-04-14 09:14:23 +0200 | [diff] [blame] | 1577 | result = new(ctx) ir_dereference_variable(tmp); |
| 1578 | type = tmp->type; |
Eric Anholt | b82c0c3 | 2010-03-31 09:11:39 -1000 | [diff] [blame] | 1579 | } |
Eric Anholt | 4950a68 | 2010-04-16 01:10:32 -0700 | [diff] [blame] | 1580 | break; |
| 1581 | } |
| 1582 | |
| 1583 | case ast_logic_or: { |
Eric Anholt | 9e04b19 | 2011-04-09 10:27:02 -1000 | [diff] [blame] | 1584 | exec_list rhs_instructions; |
Eric Anholt | 0182270 | 2011-04-09 15:59:20 -1000 | [diff] [blame] | 1585 | op[0] = get_scalar_boolean_operand(instructions, state, this, 0, |
Iago Toral Quiroga | 4472ab9 | 2014-04-14 09:14:23 +0200 | [diff] [blame] | 1586 | "LHS", &error_emitted); |
Eric Anholt | 9e04b19 | 2011-04-09 10:27:02 -1000 | [diff] [blame] | 1587 | op[1] = get_scalar_boolean_operand(&rhs_instructions, state, this, 1, |
Iago Toral Quiroga | 4472ab9 | 2014-04-14 09:14:23 +0200 | [diff] [blame] | 1588 | "RHS", &error_emitted); |
Eric Anholt | 4950a68 | 2010-04-16 01:10:32 -0700 | [diff] [blame] | 1589 | |
Eric Anholt | ead3589 | 2012-02-07 22:59:24 -0800 | [diff] [blame] | 1590 | if (rhs_instructions.is_empty()) { |
Iago Toral Quiroga | 4472ab9 | 2014-04-14 09:14:23 +0200 | [diff] [blame] | 1591 | result = new(ctx) ir_expression(ir_binop_logic_or, op[0], op[1]); |
| 1592 | type = result->type; |
Eric Anholt | 44b694e | 2010-04-16 13:49:04 -0700 | [diff] [blame] | 1593 | } else { |
Iago Toral Quiroga | 4472ab9 | 2014-04-14 09:14:23 +0200 | [diff] [blame] | 1594 | ir_variable *const tmp = new(ctx) ir_variable(glsl_type::bool_type, |
| 1595 | "or_tmp", |
| 1596 | ir_var_temporary); |
| 1597 | instructions->push_tail(tmp); |
Eric Anholt | 4950a68 | 2010-04-16 01:10:32 -0700 | [diff] [blame] | 1598 | |
Iago Toral Quiroga | 4472ab9 | 2014-04-14 09:14:23 +0200 | [diff] [blame] | 1599 | ir_if *const stmt = new(ctx) ir_if(op[0]); |
| 1600 | instructions->push_tail(stmt); |
Ian Romanick | 81d664f | 2010-07-12 15:18:55 -0700 | [diff] [blame] | 1601 | |
Iago Toral Quiroga | 4472ab9 | 2014-04-14 09:14:23 +0200 | [diff] [blame] | 1602 | ir_dereference *const then_deref = new(ctx) ir_dereference_variable(tmp); |
| 1603 | ir_assignment *const then_assign = |
| 1604 | new(ctx) ir_assignment(then_deref, new(ctx) ir_constant(true)); |
| 1605 | stmt->then_instructions.push_tail(then_assign); |
Eric Anholt | 44b694e | 2010-04-16 13:49:04 -0700 | [diff] [blame] | 1606 | |
Iago Toral Quiroga | 4472ab9 | 2014-04-14 09:14:23 +0200 | [diff] [blame] | 1607 | stmt->else_instructions.append_list(&rhs_instructions); |
| 1608 | ir_dereference *const else_deref = new(ctx) ir_dereference_variable(tmp); |
| 1609 | ir_assignment *const else_assign = |
| 1610 | new(ctx) ir_assignment(else_deref, op[1]); |
| 1611 | stmt->else_instructions.push_tail(else_assign); |
Eric Anholt | 44b694e | 2010-04-16 13:49:04 -0700 | [diff] [blame] | 1612 | |
Iago Toral Quiroga | 4472ab9 | 2014-04-14 09:14:23 +0200 | [diff] [blame] | 1613 | result = new(ctx) ir_dereference_variable(tmp); |
| 1614 | type = tmp->type; |
Eric Anholt | 4950a68 | 2010-04-16 01:10:32 -0700 | [diff] [blame] | 1615 | } |
Eric Anholt | 4950a68 | 2010-04-16 01:10:32 -0700 | [diff] [blame] | 1616 | break; |
| 1617 | } |
| 1618 | |
| 1619 | case ast_logic_xor: |
Eric Anholt | 756c262 | 2011-04-09 14:57:17 -1000 | [diff] [blame] | 1620 | /* From page 33 (page 39 of the PDF) of the GLSL 1.10 spec: |
| 1621 | * |
| 1622 | * "The logical binary operators and (&&), or ( | | ), and |
| 1623 | * exclusive or (^^). They operate only on two Boolean |
| 1624 | * expressions and result in a Boolean expression." |
| 1625 | */ |
| 1626 | op[0] = get_scalar_boolean_operand(instructions, state, this, 0, "LHS", |
Iago Toral Quiroga | 4472ab9 | 2014-04-14 09:14:23 +0200 | [diff] [blame] | 1627 | &error_emitted); |
Eric Anholt | 756c262 | 2011-04-09 14:57:17 -1000 | [diff] [blame] | 1628 | op[1] = get_scalar_boolean_operand(instructions, state, this, 1, "RHS", |
Iago Toral Quiroga | 4472ab9 | 2014-04-14 09:14:23 +0200 | [diff] [blame] | 1629 | &error_emitted); |
Eric Anholt | 4950a68 | 2010-04-16 01:10:32 -0700 | [diff] [blame] | 1630 | |
Carl Worth | 1660a29 | 2010-06-23 18:11:51 -0700 | [diff] [blame] | 1631 | result = new(ctx) ir_expression(operations[this->oper], glsl_type::bool_type, |
Iago Toral Quiroga | 4472ab9 | 2014-04-14 09:14:23 +0200 | [diff] [blame] | 1632 | op[0], op[1]); |
Ian Romanick | a87ac25 | 2010-02-22 13:19:34 -0800 | [diff] [blame] | 1633 | break; |
| 1634 | |
Eric Anholt | a5827fe | 2010-03-31 16:50:55 -1000 | [diff] [blame] | 1635 | case ast_logic_not: |
Eric Anholt | 0182270 | 2011-04-09 15:59:20 -1000 | [diff] [blame] | 1636 | op[0] = get_scalar_boolean_operand(instructions, state, this, 0, |
Iago Toral Quiroga | 4472ab9 | 2014-04-14 09:14:23 +0200 | [diff] [blame] | 1637 | "operand", &error_emitted); |
Eric Anholt | a5827fe | 2010-03-31 16:50:55 -1000 | [diff] [blame] | 1638 | |
Carl Worth | 1660a29 | 2010-06-23 18:11:51 -0700 | [diff] [blame] | 1639 | result = new(ctx) ir_expression(operations[this->oper], glsl_type::bool_type, |
Iago Toral Quiroga | 4472ab9 | 2014-04-14 09:14:23 +0200 | [diff] [blame] | 1640 | op[0], NULL); |
Eric Anholt | a5827fe | 2010-03-31 16:50:55 -1000 | [diff] [blame] | 1641 | break; |
| 1642 | |
Ian Romanick | a87ac25 | 2010-02-22 13:19:34 -0800 | [diff] [blame] | 1643 | case ast_mul_assign: |
| 1644 | case ast_div_assign: |
| 1645 | case ast_add_assign: |
| 1646 | case ast_sub_assign: { |
Alejandro Piñeiro | 8568d02 | 2016-02-25 11:11:54 +0100 | [diff] [blame] | 1647 | this->subexpressions[0]->set_is_lhs(true); |
Ian Romanick | 18238de | 2010-03-01 13:49:10 -0800 | [diff] [blame] | 1648 | op[0] = this->subexpressions[0]->hir(instructions, state); |
| 1649 | op[1] = this->subexpressions[1]->hir(instructions, state); |
Ian Romanick | a87ac25 | 2010-02-22 13:19:34 -0800 | [diff] [blame] | 1650 | |
Ilia Mirkin | a32c87f | 2016-07-08 23:28:22 -0400 | [diff] [blame] | 1651 | orig_type = op[0]->type; |
Ian Romanick | bfb09c2 | 2010-03-29 16:32:55 -0700 | [diff] [blame] | 1652 | type = arithmetic_result_type(op[0], op[1], |
Iago Toral Quiroga | 4472ab9 | 2014-04-14 09:14:23 +0200 | [diff] [blame] | 1653 | (this->oper == ast_mul_assign), |
| 1654 | state, & loc); |
Ian Romanick | a87ac25 | 2010-02-22 13:19:34 -0800 | [diff] [blame] | 1655 | |
Ilia Mirkin | a32c87f | 2016-07-08 23:28:22 -0400 | [diff] [blame] | 1656 | if (type != orig_type) { |
| 1657 | _mesa_glsl_error(& loc, state, |
| 1658 | "could not implicitly convert " |
| 1659 | "%s to %s", type->name, orig_type->name); |
| 1660 | type = glsl_type::error_type; |
| 1661 | } |
| 1662 | |
Carl Worth | 1660a29 | 2010-06-23 18:11:51 -0700 | [diff] [blame] | 1663 | ir_rvalue *temp_rhs = new(ctx) ir_expression(operations[this->oper], type, |
Iago Toral Quiroga | 4472ab9 | 2014-04-14 09:14:23 +0200 | [diff] [blame] | 1664 | op[0], op[1]); |
Ian Romanick | a87ac25 | 2010-02-22 13:19:34 -0800 | [diff] [blame] | 1665 | |
Eric Anholt | e9822f7 | 2014-03-05 17:05:54 -0800 | [diff] [blame] | 1666 | error_emitted = |
| 1667 | do_assignment(instructions, state, |
| 1668 | this->subexpressions[0]->non_lvalue_description, |
| 1669 | op[0]->clone(ctx, NULL), temp_rhs, |
| 1670 | &result, needs_rvalue, false, |
| 1671 | this->subexpressions[0]->get_location()); |
Ian Romanick | a87ac25 | 2010-02-22 13:19:34 -0800 | [diff] [blame] | 1672 | |
| 1673 | /* GLSL 1.10 does not allow array assignment. However, we don't have to |
| 1674 | * explicitly test for this because none of the binary expression |
| 1675 | * operators allow array operands either. |
| 1676 | */ |
| 1677 | |
Ian Romanick | a87ac25 | 2010-02-22 13:19:34 -0800 | [diff] [blame] | 1678 | break; |
| 1679 | } |
| 1680 | |
Eric Anholt | 48a0e64 | 2010-03-26 11:57:46 -0700 | [diff] [blame] | 1681 | case ast_mod_assign: { |
Alejandro Piñeiro | 8568d02 | 2016-02-25 11:11:54 +0100 | [diff] [blame] | 1682 | this->subexpressions[0]->set_is_lhs(true); |
Eric Anholt | 48a0e64 | 2010-03-26 11:57:46 -0700 | [diff] [blame] | 1683 | op[0] = this->subexpressions[0]->hir(instructions, state); |
| 1684 | op[1] = this->subexpressions[1]->hir(instructions, state); |
| 1685 | |
Ilia Mirkin | a32c87f | 2016-07-08 23:28:22 -0400 | [diff] [blame] | 1686 | orig_type = op[0]->type; |
Kenneth Graunke | 511de1a | 2015-11-12 13:02:05 -0800 | [diff] [blame] | 1687 | type = modulus_result_type(op[0], op[1], state, &loc); |
Eric Anholt | 48a0e64 | 2010-03-26 11:57:46 -0700 | [diff] [blame] | 1688 | |
Ilia Mirkin | a32c87f | 2016-07-08 23:28:22 -0400 | [diff] [blame] | 1689 | if (type != orig_type) { |
| 1690 | _mesa_glsl_error(& loc, state, |
| 1691 | "could not implicitly convert " |
| 1692 | "%s to %s", type->name, orig_type->name); |
| 1693 | type = glsl_type::error_type; |
| 1694 | } |
| 1695 | |
Eric Anholt | 48a0e64 | 2010-03-26 11:57:46 -0700 | [diff] [blame] | 1696 | assert(operations[this->oper] == ir_binop_mod); |
| 1697 | |
Ian Romanick | 768b55a | 2010-08-13 16:46:43 -0700 | [diff] [blame] | 1698 | ir_rvalue *temp_rhs; |
Carl Worth | 1660a29 | 2010-06-23 18:11:51 -0700 | [diff] [blame] | 1699 | temp_rhs = new(ctx) ir_expression(operations[this->oper], type, |
Iago Toral Quiroga | 4472ab9 | 2014-04-14 09:14:23 +0200 | [diff] [blame] | 1700 | op[0], op[1]); |
Eric Anholt | 48a0e64 | 2010-03-26 11:57:46 -0700 | [diff] [blame] | 1701 | |
Eric Anholt | e9822f7 | 2014-03-05 17:05:54 -0800 | [diff] [blame] | 1702 | error_emitted = |
| 1703 | do_assignment(instructions, state, |
| 1704 | this->subexpressions[0]->non_lvalue_description, |
| 1705 | op[0]->clone(ctx, NULL), temp_rhs, |
| 1706 | &result, needs_rvalue, false, |
| 1707 | this->subexpressions[0]->get_location()); |
Eric Anholt | 48a0e64 | 2010-03-26 11:57:46 -0700 | [diff] [blame] | 1708 | break; |
| 1709 | } |
Ian Romanick | a87ac25 | 2010-02-22 13:19:34 -0800 | [diff] [blame] | 1710 | |
| 1711 | case ast_ls_assign: |
Chad Versace | 338ed6e | 2010-10-15 10:05:50 -0700 | [diff] [blame] | 1712 | case ast_rs_assign: { |
Alejandro Piñeiro | 8568d02 | 2016-02-25 11:11:54 +0100 | [diff] [blame] | 1713 | this->subexpressions[0]->set_is_lhs(true); |
Chad Versace | 338ed6e | 2010-10-15 10:05:50 -0700 | [diff] [blame] | 1714 | op[0] = this->subexpressions[0]->hir(instructions, state); |
| 1715 | op[1] = this->subexpressions[1]->hir(instructions, state); |
| 1716 | type = shift_result_type(op[0]->type, op[1]->type, this->oper, state, |
| 1717 | &loc); |
| 1718 | ir_rvalue *temp_rhs = new(ctx) ir_expression(operations[this->oper], |
| 1719 | type, op[0], op[1]); |
Eric Anholt | e9822f7 | 2014-03-05 17:05:54 -0800 | [diff] [blame] | 1720 | error_emitted = |
| 1721 | do_assignment(instructions, state, |
| 1722 | this->subexpressions[0]->non_lvalue_description, |
| 1723 | op[0]->clone(ctx, NULL), temp_rhs, |
| 1724 | &result, needs_rvalue, false, |
| 1725 | this->subexpressions[0]->get_location()); |
Ian Romanick | 251eb75 | 2010-03-29 14:15:05 -0700 | [diff] [blame] | 1726 | break; |
Chad Versace | 338ed6e | 2010-10-15 10:05:50 -0700 | [diff] [blame] | 1727 | } |
Ian Romanick | a87ac25 | 2010-02-22 13:19:34 -0800 | [diff] [blame] | 1728 | |
| 1729 | case ast_and_assign: |
| 1730 | case ast_xor_assign: |
Chad Versace | d03ac0f | 2010-10-15 12:08:28 -0700 | [diff] [blame] | 1731 | case ast_or_assign: { |
Alejandro Piñeiro | 8568d02 | 2016-02-25 11:11:54 +0100 | [diff] [blame] | 1732 | this->subexpressions[0]->set_is_lhs(true); |
Chad Versace | d03ac0f | 2010-10-15 12:08:28 -0700 | [diff] [blame] | 1733 | op[0] = this->subexpressions[0]->hir(instructions, state); |
| 1734 | op[1] = this->subexpressions[1]->hir(instructions, state); |
Ilia Mirkin | a32c87f | 2016-07-08 23:28:22 -0400 | [diff] [blame] | 1735 | |
| 1736 | orig_type = op[0]->type; |
Kenneth Graunke | d54a70a | 2016-01-14 23:27:03 -0800 | [diff] [blame] | 1737 | type = bit_logic_result_type(op[0], op[1], this->oper, state, &loc); |
Ilia Mirkin | a32c87f | 2016-07-08 23:28:22 -0400 | [diff] [blame] | 1738 | |
| 1739 | if (type != orig_type) { |
| 1740 | _mesa_glsl_error(& loc, state, |
| 1741 | "could not implicitly convert " |
| 1742 | "%s to %s", type->name, orig_type->name); |
| 1743 | type = glsl_type::error_type; |
| 1744 | } |
| 1745 | |
Chad Versace | d03ac0f | 2010-10-15 12:08:28 -0700 | [diff] [blame] | 1746 | ir_rvalue *temp_rhs = new(ctx) ir_expression(operations[this->oper], |
| 1747 | type, op[0], op[1]); |
Eric Anholt | e9822f7 | 2014-03-05 17:05:54 -0800 | [diff] [blame] | 1748 | error_emitted = |
| 1749 | do_assignment(instructions, state, |
| 1750 | this->subexpressions[0]->non_lvalue_description, |
| 1751 | op[0]->clone(ctx, NULL), temp_rhs, |
| 1752 | &result, needs_rvalue, false, |
| 1753 | this->subexpressions[0]->get_location()); |
Ian Romanick | 251eb75 | 2010-03-29 14:15:05 -0700 | [diff] [blame] | 1754 | break; |
Chad Versace | d03ac0f | 2010-10-15 12:08:28 -0700 | [diff] [blame] | 1755 | } |
Ian Romanick | a87ac25 | 2010-02-22 13:19:34 -0800 | [diff] [blame] | 1756 | |
Ian Romanick | 96f9cea | 2010-03-29 15:33:54 -0700 | [diff] [blame] | 1757 | case ast_conditional: { |
Ian Romanick | 96f9cea | 2010-03-29 15:33:54 -0700 | [diff] [blame] | 1758 | /* From page 59 (page 65 of the PDF) of the GLSL 1.50 spec: |
| 1759 | * |
| 1760 | * "The ternary selection operator (?:). It operates on three |
| 1761 | * expressions (exp1 ? exp2 : exp3). This operator evaluates the |
| 1762 | * first expression, which must result in a scalar Boolean." |
| 1763 | */ |
Eric Anholt | 0182270 | 2011-04-09 15:59:20 -1000 | [diff] [blame] | 1764 | op[0] = get_scalar_boolean_operand(instructions, state, this, 0, |
Iago Toral Quiroga | 4472ab9 | 2014-04-14 09:14:23 +0200 | [diff] [blame] | 1765 | "condition", &error_emitted); |
Ian Romanick | 96f9cea | 2010-03-29 15:33:54 -0700 | [diff] [blame] | 1766 | |
| 1767 | /* The :? operator is implemented by generating an anonymous temporary |
| 1768 | * followed by an if-statement. The last instruction in each branch of |
| 1769 | * the if-statement assigns a value to the anonymous temporary. This |
| 1770 | * temporary is the r-value of the expression. |
| 1771 | */ |
Ian Romanick | 0ad76c6 | 2010-06-11 12:56:26 -0700 | [diff] [blame] | 1772 | exec_list then_instructions; |
| 1773 | exec_list else_instructions; |
Ian Romanick | 96f9cea | 2010-03-29 15:33:54 -0700 | [diff] [blame] | 1774 | |
Ian Romanick | 0ad76c6 | 2010-06-11 12:56:26 -0700 | [diff] [blame] | 1775 | op[1] = this->subexpressions[1]->hir(&then_instructions, state); |
| 1776 | op[2] = this->subexpressions[2]->hir(&else_instructions, state); |
Ian Romanick | 96f9cea | 2010-03-29 15:33:54 -0700 | [diff] [blame] | 1777 | |
| 1778 | /* From page 59 (page 65 of the PDF) of the GLSL 1.50 spec: |
| 1779 | * |
| 1780 | * "The second and third expressions can be any type, as |
| 1781 | * long their types match, or there is a conversion in |
| 1782 | * Section 4.1.10 "Implicit Conversions" that can be applied |
| 1783 | * to one of the expressions to make their types match. This |
| 1784 | * resulting matching type is the type of the entire |
| 1785 | * expression." |
| 1786 | */ |
Ian Romanick | bfb09c2 | 2010-03-29 16:32:55 -0700 | [diff] [blame] | 1787 | if ((!apply_implicit_conversion(op[1]->type, op[2], state) |
Iago Toral Quiroga | 4472ab9 | 2014-04-14 09:14:23 +0200 | [diff] [blame] | 1788 | && !apply_implicit_conversion(op[2]->type, op[1], state)) |
| 1789 | || (op[1]->type != op[2]->type)) { |
| 1790 | YYLTYPE loc = this->subexpressions[1]->get_location(); |
Ian Romanick | 96f9cea | 2010-03-29 15:33:54 -0700 | [diff] [blame] | 1791 | |
Iago Toral Quiroga | 4472ab9 | 2014-04-14 09:14:23 +0200 | [diff] [blame] | 1792 | _mesa_glsl_error(& loc, state, "second and third operands of ?: " |
| 1793 | "operator must have matching types"); |
| 1794 | error_emitted = true; |
| 1795 | type = glsl_type::error_type; |
Ian Romanick | db9be2e | 2010-03-29 16:25:56 -0700 | [diff] [blame] | 1796 | } else { |
Iago Toral Quiroga | 4472ab9 | 2014-04-14 09:14:23 +0200 | [diff] [blame] | 1797 | type = op[1]->type; |
Ian Romanick | 96f9cea | 2010-03-29 15:33:54 -0700 | [diff] [blame] | 1798 | } |
| 1799 | |
Ian Romanick | f09fabc | 2010-09-07 14:30:06 -0700 | [diff] [blame] | 1800 | /* From page 33 (page 39 of the PDF) of the GLSL 1.10 spec: |
| 1801 | * |
| 1802 | * "The second and third expressions must be the same type, but can |
| 1803 | * be of any type other than an array." |
| 1804 | */ |
Paul Berry | 0d9bba6 | 2012-08-05 09:57:01 -0700 | [diff] [blame] | 1805 | if (type->is_array() && |
Paul Berry | d4a2474 | 2012-08-02 08:18:12 -0700 | [diff] [blame] | 1806 | !state->check_version(120, 300, &loc, |
Paul Berry | 4d7899f | 2013-07-25 19:56:43 -0700 | [diff] [blame] | 1807 | "second and third operands of ?: operator " |
Paul Berry | 0d9bba6 | 2012-08-05 09:57:01 -0700 | [diff] [blame] | 1808 | "cannot be arrays")) { |
Iago Toral Quiroga | 4472ab9 | 2014-04-14 09:14:23 +0200 | [diff] [blame] | 1809 | error_emitted = true; |
Ian Romanick | f09fabc | 2010-09-07 14:30:06 -0700 | [diff] [blame] | 1810 | } |
| 1811 | |
Francisco Jerez | f64edfd | 2014-12-04 10:42:11 +0200 | [diff] [blame] | 1812 | /* From section 4.1.7 of the GLSL 4.50 spec (Opaque Types): |
| 1813 | * |
| 1814 | * "Except for array indexing, structure member selection, and |
| 1815 | * parentheses, opaque variables are not allowed to be operands in |
| 1816 | * expressions; such use results in a compile-time error." |
| 1817 | */ |
| 1818 | if (type->contains_opaque()) { |
| 1819 | _mesa_glsl_error(&loc, state, "opaque variables cannot be operands " |
| 1820 | "of the ?: operator"); |
| 1821 | error_emitted = true; |
| 1822 | } |
| 1823 | |
Ian Romanick | 7825d3d | 2010-06-11 13:45:51 -0700 | [diff] [blame] | 1824 | ir_constant *cond_val = op[0]->constant_expression_value(); |
Ian Romanick | 0ad76c6 | 2010-06-11 12:56:26 -0700 | [diff] [blame] | 1825 | |
Ian Romanick | 7825d3d | 2010-06-11 13:45:51 -0700 | [diff] [blame] | 1826 | if (then_instructions.is_empty() |
Iago Toral Quiroga | 4472ab9 | 2014-04-14 09:14:23 +0200 | [diff] [blame] | 1827 | && else_instructions.is_empty() |
Kenneth Graunke | 0f06f12 | 2014-08-21 21:49:07 -0700 | [diff] [blame] | 1828 | && cond_val != NULL) { |
| 1829 | result = cond_val->value.b[0] ? op[1] : op[2]; |
Ian Romanick | 7825d3d | 2010-06-11 13:45:51 -0700 | [diff] [blame] | 1830 | } else { |
Kenneth Graunke | 9f1e250 | 2015-03-05 23:18:36 -0800 | [diff] [blame] | 1831 | /* The copy to conditional_tmp reads the whole array. */ |
| 1832 | if (type->is_array()) { |
| 1833 | mark_whole_array_access(op[1]); |
| 1834 | mark_whole_array_access(op[2]); |
| 1835 | } |
| 1836 | |
Iago Toral Quiroga | 4472ab9 | 2014-04-14 09:14:23 +0200 | [diff] [blame] | 1837 | ir_variable *const tmp = |
| 1838 | new(ctx) ir_variable(type, "conditional_tmp", ir_var_temporary); |
| 1839 | instructions->push_tail(tmp); |
Ian Romanick | 0ad76c6 | 2010-06-11 12:56:26 -0700 | [diff] [blame] | 1840 | |
Iago Toral Quiroga | 4472ab9 | 2014-04-14 09:14:23 +0200 | [diff] [blame] | 1841 | ir_if *const stmt = new(ctx) ir_if(op[0]); |
| 1842 | instructions->push_tail(stmt); |
Ian Romanick | 0ad76c6 | 2010-06-11 12:56:26 -0700 | [diff] [blame] | 1843 | |
Iago Toral Quiroga | 4472ab9 | 2014-04-14 09:14:23 +0200 | [diff] [blame] | 1844 | then_instructions.move_nodes_to(& stmt->then_instructions); |
| 1845 | ir_dereference *const then_deref = |
| 1846 | new(ctx) ir_dereference_variable(tmp); |
| 1847 | ir_assignment *const then_assign = |
| 1848 | new(ctx) ir_assignment(then_deref, op[1]); |
| 1849 | stmt->then_instructions.push_tail(then_assign); |
Ian Romanick | 0ad76c6 | 2010-06-11 12:56:26 -0700 | [diff] [blame] | 1850 | |
Iago Toral Quiroga | 4472ab9 | 2014-04-14 09:14:23 +0200 | [diff] [blame] | 1851 | else_instructions.move_nodes_to(& stmt->else_instructions); |
| 1852 | ir_dereference *const else_deref = |
| 1853 | new(ctx) ir_dereference_variable(tmp); |
| 1854 | ir_assignment *const else_assign = |
| 1855 | new(ctx) ir_assignment(else_deref, op[2]); |
| 1856 | stmt->else_instructions.push_tail(else_assign); |
Ian Romanick | 7825d3d | 2010-06-11 13:45:51 -0700 | [diff] [blame] | 1857 | |
Iago Toral Quiroga | 4472ab9 | 2014-04-14 09:14:23 +0200 | [diff] [blame] | 1858 | result = new(ctx) ir_dereference_variable(tmp); |
Ian Romanick | 7825d3d | 2010-06-11 13:45:51 -0700 | [diff] [blame] | 1859 | } |
Ian Romanick | 251eb75 | 2010-03-29 14:15:05 -0700 | [diff] [blame] | 1860 | break; |
Ian Romanick | 96f9cea | 2010-03-29 15:33:54 -0700 | [diff] [blame] | 1861 | } |
Ian Romanick | a87ac25 | 2010-02-22 13:19:34 -0800 | [diff] [blame] | 1862 | |
| 1863 | case ast_pre_inc: |
Eric Anholt | 76ea56c | 2010-03-26 12:16:54 -0700 | [diff] [blame] | 1864 | case ast_pre_dec: { |
Ian Romanick | fa0a9ac | 2011-12-23 09:56:03 -0800 | [diff] [blame] | 1865 | this->non_lvalue_description = (this->oper == ast_pre_inc) |
Iago Toral Quiroga | 4472ab9 | 2014-04-14 09:14:23 +0200 | [diff] [blame] | 1866 | ? "pre-increment operation" : "pre-decrement operation"; |
Ian Romanick | fa0a9ac | 2011-12-23 09:56:03 -0800 | [diff] [blame] | 1867 | |
Eric Anholt | 76ea56c | 2010-03-26 12:16:54 -0700 | [diff] [blame] | 1868 | op[0] = this->subexpressions[0]->hir(instructions, state); |
Paul Berry | 9abda92 | 2011-10-31 18:22:48 -0700 | [diff] [blame] | 1869 | op[1] = constant_one_for_inc_dec(ctx, op[0]->type); |
Eric Anholt | 76ea56c | 2010-03-26 12:16:54 -0700 | [diff] [blame] | 1870 | |
Eric Anholt | a13bb14 | 2010-03-31 16:38:11 -1000 | [diff] [blame] | 1871 | type = arithmetic_result_type(op[0], op[1], false, state, & loc); |
Eric Anholt | 76ea56c | 2010-03-26 12:16:54 -0700 | [diff] [blame] | 1872 | |
Ian Romanick | 768b55a | 2010-08-13 16:46:43 -0700 | [diff] [blame] | 1873 | ir_rvalue *temp_rhs; |
Carl Worth | 1660a29 | 2010-06-23 18:11:51 -0700 | [diff] [blame] | 1874 | temp_rhs = new(ctx) ir_expression(operations[this->oper], type, |
Iago Toral Quiroga | 4472ab9 | 2014-04-14 09:14:23 +0200 | [diff] [blame] | 1875 | op[0], op[1]); |
Eric Anholt | 76ea56c | 2010-03-26 12:16:54 -0700 | [diff] [blame] | 1876 | |
Eric Anholt | e9822f7 | 2014-03-05 17:05:54 -0800 | [diff] [blame] | 1877 | error_emitted = |
| 1878 | do_assignment(instructions, state, |
| 1879 | this->subexpressions[0]->non_lvalue_description, |
| 1880 | op[0]->clone(ctx, NULL), temp_rhs, |
| 1881 | &result, needs_rvalue, false, |
| 1882 | this->subexpressions[0]->get_location()); |
Eric Anholt | 76ea56c | 2010-03-26 12:16:54 -0700 | [diff] [blame] | 1883 | break; |
| 1884 | } |
Ian Romanick | a87ac25 | 2010-02-22 13:19:34 -0800 | [diff] [blame] | 1885 | |
| 1886 | case ast_post_inc: |
Eric Anholt | de38f0e | 2010-03-26 12:14:54 -0700 | [diff] [blame] | 1887 | case ast_post_dec: { |
Ian Romanick | fa0a9ac | 2011-12-23 09:56:03 -0800 | [diff] [blame] | 1888 | this->non_lvalue_description = (this->oper == ast_post_inc) |
Iago Toral Quiroga | 4472ab9 | 2014-04-14 09:14:23 +0200 | [diff] [blame] | 1889 | ? "post-increment operation" : "post-decrement operation"; |
Eric Anholt | de38f0e | 2010-03-26 12:14:54 -0700 | [diff] [blame] | 1890 | op[0] = this->subexpressions[0]->hir(instructions, state); |
Paul Berry | 9abda92 | 2011-10-31 18:22:48 -0700 | [diff] [blame] | 1891 | op[1] = constant_one_for_inc_dec(ctx, op[0]->type); |
Eric Anholt | de38f0e | 2010-03-26 12:14:54 -0700 | [diff] [blame] | 1892 | |
| 1893 | error_emitted = op[0]->type->is_error() || op[1]->type->is_error(); |
| 1894 | |
Eric Anholt | a13bb14 | 2010-03-31 16:38:11 -1000 | [diff] [blame] | 1895 | type = arithmetic_result_type(op[0], op[1], false, state, & loc); |
Eric Anholt | de38f0e | 2010-03-26 12:14:54 -0700 | [diff] [blame] | 1896 | |
Ian Romanick | 768b55a | 2010-08-13 16:46:43 -0700 | [diff] [blame] | 1897 | ir_rvalue *temp_rhs; |
Carl Worth | 1660a29 | 2010-06-23 18:11:51 -0700 | [diff] [blame] | 1898 | temp_rhs = new(ctx) ir_expression(operations[this->oper], type, |
Iago Toral Quiroga | 4472ab9 | 2014-04-14 09:14:23 +0200 | [diff] [blame] | 1899 | op[0], op[1]); |
Eric Anholt | de38f0e | 2010-03-26 12:14:54 -0700 | [diff] [blame] | 1900 | |
| 1901 | /* Get a temporary of a copy of the lvalue before it's modified. |
| 1902 | * This may get thrown away later. |
| 1903 | */ |
Eric Anholt | 8273bd4 | 2010-08-04 12:34:56 -0700 | [diff] [blame] | 1904 | result = get_lvalue_copy(instructions, op[0]->clone(ctx, NULL)); |
Eric Anholt | de38f0e | 2010-03-26 12:14:54 -0700 | [diff] [blame] | 1905 | |
Eric Anholt | e9822f7 | 2014-03-05 17:05:54 -0800 | [diff] [blame] | 1906 | ir_rvalue *junk_rvalue; |
| 1907 | error_emitted = |
| 1908 | do_assignment(instructions, state, |
| 1909 | this->subexpressions[0]->non_lvalue_description, |
| 1910 | op[0]->clone(ctx, NULL), temp_rhs, |
| 1911 | &junk_rvalue, false, false, |
| 1912 | this->subexpressions[0]->get_location()); |
Eric Anholt | de38f0e | 2010-03-26 12:14:54 -0700 | [diff] [blame] | 1913 | |
Ian Romanick | a87ac25 | 2010-02-22 13:19:34 -0800 | [diff] [blame] | 1914 | break; |
Eric Anholt | de38f0e | 2010-03-26 12:14:54 -0700 | [diff] [blame] | 1915 | } |
Ian Romanick | a87ac25 | 2010-02-22 13:19:34 -0800 | [diff] [blame] | 1916 | |
| 1917 | case ast_field_selection: |
Ian Romanick | 18238de | 2010-03-01 13:49:10 -0800 | [diff] [blame] | 1918 | result = _mesa_ast_field_selection_to_hir(this, instructions, state); |
Ian Romanick | a87ac25 | 2010-02-22 13:19:34 -0800 | [diff] [blame] | 1919 | break; |
| 1920 | |
Ian Romanick | 27e3cf8 | 2010-04-01 18:03:59 -0700 | [diff] [blame] | 1921 | case ast_array_index: { |
| 1922 | YYLTYPE index_loc = subexpressions[1]->get_location(); |
| 1923 | |
Alejandro Piñeiro | 8568d02 | 2016-02-25 11:11:54 +0100 | [diff] [blame] | 1924 | /* Getting if an array is being used uninitialized is beyond what we get |
| 1925 | * from ir_value.data.assigned. Setting is_lhs as true would force to |
| 1926 | * not raise a uninitialized warning when using an array |
| 1927 | */ |
| 1928 | subexpressions[0]->set_is_lhs(true); |
Ian Romanick | 27e3cf8 | 2010-04-01 18:03:59 -0700 | [diff] [blame] | 1929 | op[0] = subexpressions[0]->hir(instructions, state); |
| 1930 | op[1] = subexpressions[1]->hir(instructions, state); |
| 1931 | |
Ian Romanick | 46934ad | 2013-03-15 14:10:12 -0700 | [diff] [blame] | 1932 | result = _mesa_ast_array_index_to_hir(ctx, state, op[0], op[1], |
Iago Toral Quiroga | 4472ab9 | 2014-04-14 09:14:23 +0200 | [diff] [blame] | 1933 | loc, index_loc); |
Ian Romanick | b8a21cc | 2010-04-01 18:31:11 -0700 | [diff] [blame] | 1934 | |
Ian Romanick | 46934ad | 2013-03-15 14:10:12 -0700 | [diff] [blame] | 1935 | if (result->type->is_error()) |
Iago Toral Quiroga | 4472ab9 | 2014-04-14 09:14:23 +0200 | [diff] [blame] | 1936 | error_emitted = true; |
Ian Romanick | 27e3cf8 | 2010-04-01 18:03:59 -0700 | [diff] [blame] | 1937 | |
Ian Romanick | a87ac25 | 2010-02-22 13:19:34 -0800 | [diff] [blame] | 1938 | break; |
Ian Romanick | 27e3cf8 | 2010-04-01 18:03:59 -0700 | [diff] [blame] | 1939 | } |
Ian Romanick | a87ac25 | 2010-02-22 13:19:34 -0800 | [diff] [blame] | 1940 | |
Timothy Arceri | 3129359 | 2015-10-15 14:32:41 +1100 | [diff] [blame] | 1941 | case ast_unsized_array_dim: |
| 1942 | assert(!"ast_unsized_array_dim: Should never get here."); |
| 1943 | break; |
| 1944 | |
Ian Romanick | a87ac25 | 2010-02-22 13:19:34 -0800 | [diff] [blame] | 1945 | case ast_function_call: |
Ian Romanick | 7cfddf1 | 2010-03-10 13:26:52 -0800 | [diff] [blame] | 1946 | /* Should *NEVER* get here. ast_function_call should always be handled |
| 1947 | * by ast_function_expression::hir. |
Ian Romanick | a87ac25 | 2010-02-22 13:19:34 -0800 | [diff] [blame] | 1948 | */ |
Ian Romanick | 7cfddf1 | 2010-03-10 13:26:52 -0800 | [diff] [blame] | 1949 | assert(0); |
Ian Romanick | a87ac25 | 2010-02-22 13:19:34 -0800 | [diff] [blame] | 1950 | break; |
| 1951 | |
| 1952 | case ast_identifier: { |
| 1953 | /* ast_identifier can appear several places in a full abstract syntax |
| 1954 | * tree. This particular use must be at location specified in the grammar |
| 1955 | * as 'variable_identifier'. |
| 1956 | */ |
Iago Toral Quiroga | 1af0d9d | 2015-11-24 12:40:53 +0100 | [diff] [blame] | 1957 | ir_variable *var = |
Iago Toral Quiroga | 4472ab9 | 2014-04-14 09:14:23 +0200 | [diff] [blame] | 1958 | state->symbols->get_variable(this->primary_expression.identifier); |
Ian Romanick | a87ac25 | 2010-02-22 13:19:34 -0800 | [diff] [blame] | 1959 | |
Dave Airlie | c714731 | 2016-05-17 13:07:40 +1000 | [diff] [blame] | 1960 | if (var == NULL) { |
| 1961 | /* the identifier might be a subroutine name */ |
| 1962 | char *sub_name; |
| 1963 | sub_name = ralloc_asprintf(ctx, "%s_%s", _mesa_shader_stage_to_subroutine_prefix(state->stage), this->primary_expression.identifier); |
| 1964 | var = state->symbols->get_variable(sub_name); |
| 1965 | ralloc_free(sub_name); |
| 1966 | } |
| 1967 | |
Ian Romanick | a87ac25 | 2010-02-22 13:19:34 -0800 | [diff] [blame] | 1968 | if (var != NULL) { |
Iago Toral Quiroga | 4472ab9 | 2014-04-14 09:14:23 +0200 | [diff] [blame] | 1969 | var->data.used = true; |
| 1970 | result = new(ctx) ir_dereference_variable(var); |
Alejandro Piñeiro | dcd41ca | 2016-02-23 11:48:52 +0100 | [diff] [blame] | 1971 | |
| 1972 | if ((var->data.mode == ir_var_auto || var->data.mode == ir_var_shader_out) |
| 1973 | && !this->is_lhs |
Alejandro Piñeiro | cd7d631 | 2016-04-01 09:11:15 +0200 | [diff] [blame] | 1974 | && result->variable_referenced()->data.assigned != true |
| 1975 | && !is_gl_identifier(var->name)) { |
Alejandro Piñeiro | dcd41ca | 2016-02-23 11:48:52 +0100 | [diff] [blame] | 1976 | _mesa_glsl_warning(&loc, state, "`%s' used uninitialized", |
| 1977 | this->primary_expression.identifier); |
| 1978 | } |
Ian Romanick | a87ac25 | 2010-02-22 13:19:34 -0800 | [diff] [blame] | 1979 | } else { |
Iago Toral Quiroga | 4472ab9 | 2014-04-14 09:14:23 +0200 | [diff] [blame] | 1980 | _mesa_glsl_error(& loc, state, "`%s' undeclared", |
| 1981 | this->primary_expression.identifier); |
Ian Romanick | a87ac25 | 2010-02-22 13:19:34 -0800 | [diff] [blame] | 1982 | |
Iago Toral Quiroga | 4472ab9 | 2014-04-14 09:14:23 +0200 | [diff] [blame] | 1983 | result = ir_rvalue::error_value(ctx); |
| 1984 | error_emitted = true; |
Ian Romanick | a87ac25 | 2010-02-22 13:19:34 -0800 | [diff] [blame] | 1985 | } |
| 1986 | break; |
| 1987 | } |
| 1988 | |
| 1989 | case ast_int_constant: |
Carl Worth | 1660a29 | 2010-06-23 18:11:51 -0700 | [diff] [blame] | 1990 | result = new(ctx) ir_constant(this->primary_expression.int_constant); |
Ian Romanick | a87ac25 | 2010-02-22 13:19:34 -0800 | [diff] [blame] | 1991 | break; |
| 1992 | |
| 1993 | case ast_uint_constant: |
Carl Worth | 1660a29 | 2010-06-23 18:11:51 -0700 | [diff] [blame] | 1994 | result = new(ctx) ir_constant(this->primary_expression.uint_constant); |
Ian Romanick | a87ac25 | 2010-02-22 13:19:34 -0800 | [diff] [blame] | 1995 | break; |
| 1996 | |
| 1997 | case ast_float_constant: |
Carl Worth | 1660a29 | 2010-06-23 18:11:51 -0700 | [diff] [blame] | 1998 | result = new(ctx) ir_constant(this->primary_expression.float_constant); |
Ian Romanick | a87ac25 | 2010-02-22 13:19:34 -0800 | [diff] [blame] | 1999 | break; |
| 2000 | |
| 2001 | case ast_bool_constant: |
Carl Worth | 1660a29 | 2010-06-23 18:11:51 -0700 | [diff] [blame] | 2002 | result = new(ctx) ir_constant(bool(this->primary_expression.bool_constant)); |
Ian Romanick | a87ac25 | 2010-02-22 13:19:34 -0800 | [diff] [blame] | 2003 | break; |
| 2004 | |
Dave Airlie | ba3bab2 | 2015-02-05 12:04:58 +0200 | [diff] [blame] | 2005 | case ast_double_constant: |
| 2006 | result = new(ctx) ir_constant(this->primary_expression.double_constant); |
| 2007 | break; |
| 2008 | |
Ian Romanick | a87ac25 | 2010-02-22 13:19:34 -0800 | [diff] [blame] | 2009 | case ast_sequence: { |
Ian Romanick | a87ac25 | 2010-02-22 13:19:34 -0800 | [diff] [blame] | 2010 | /* It should not be possible to generate a sequence in the AST without |
| 2011 | * any expressions in it. |
| 2012 | */ |
Ian Romanick | 304ea90 | 2010-05-10 11:17:53 -0700 | [diff] [blame] | 2013 | assert(!this->expressions.is_empty()); |
Ian Romanick | a87ac25 | 2010-02-22 13:19:34 -0800 | [diff] [blame] | 2014 | |
| 2015 | /* The r-value of a sequence is the last expression in the sequence. If |
| 2016 | * the other expressions in the sequence do not have side-effects (and |
| 2017 | * therefore add instructions to the instruction list), they get dropped |
| 2018 | * on the floor. |
| 2019 | */ |
Matt Turner | 8151351 | 2016-07-25 15:10:42 -0700 | [diff] [blame] | 2020 | exec_node *previous_tail = NULL; |
Ian Romanick | 3d5cfcf | 2011-04-11 10:10:30 -0700 | [diff] [blame] | 2021 | YYLTYPE previous_operand_loc = loc; |
| 2022 | |
| 2023 | foreach_list_typed (ast_node, ast, link, &this->expressions) { |
Iago Toral Quiroga | 4472ab9 | 2014-04-14 09:14:23 +0200 | [diff] [blame] | 2024 | /* If one of the operands of comma operator does not generate any |
| 2025 | * code, we want to emit a warning. At each pass through the loop |
Matt Turner | 8151351 | 2016-07-25 15:10:42 -0700 | [diff] [blame] | 2026 | * previous_tail will point to the last instruction in the stream |
| 2027 | * *before* processing the previous operand. Naturally, |
| 2028 | * instructions->get_tail_raw() will point to the last instruction in |
| 2029 | * the stream *after* processing the previous operand. If the two |
Iago Toral Quiroga | 4472ab9 | 2014-04-14 09:14:23 +0200 | [diff] [blame] | 2030 | * pointers match, then the previous operand had no effect. |
| 2031 | * |
| 2032 | * The warning behavior here differs slightly from GCC. GCC will |
| 2033 | * only emit a warning if none of the left-hand operands have an |
| 2034 | * effect. However, it will emit a warning for each. I believe that |
| 2035 | * there are some cases in C (especially with GCC extensions) where |
| 2036 | * it is useful to have an intermediate step in a sequence have no |
| 2037 | * effect, but I don't think these cases exist in GLSL. Either way, |
| 2038 | * it would be a giant hassle to replicate that behavior. |
| 2039 | */ |
Matt Turner | 8151351 | 2016-07-25 15:10:42 -0700 | [diff] [blame] | 2040 | if (previous_tail == instructions->get_tail_raw()) { |
Iago Toral Quiroga | 4472ab9 | 2014-04-14 09:14:23 +0200 | [diff] [blame] | 2041 | _mesa_glsl_warning(&previous_operand_loc, state, |
| 2042 | "left-hand operand of comma expression has " |
| 2043 | "no effect"); |
| 2044 | } |
Ian Romanick | 3d5cfcf | 2011-04-11 10:10:30 -0700 | [diff] [blame] | 2045 | |
Matt Turner | 8151351 | 2016-07-25 15:10:42 -0700 | [diff] [blame] | 2046 | /* The tail is directly accessed instead of using the get_tail() |
Iago Toral Quiroga | 4472ab9 | 2014-04-14 09:14:23 +0200 | [diff] [blame] | 2047 | * method for performance reasons. get_tail() has extra code to |
| 2048 | * return NULL when the list is empty. We don't care about that |
Matt Turner | 8151351 | 2016-07-25 15:10:42 -0700 | [diff] [blame] | 2049 | * here, so using get_tail_raw() is fine. |
Iago Toral Quiroga | 4472ab9 | 2014-04-14 09:14:23 +0200 | [diff] [blame] | 2050 | */ |
Matt Turner | 8151351 | 2016-07-25 15:10:42 -0700 | [diff] [blame] | 2051 | previous_tail = instructions->get_tail_raw(); |
Iago Toral Quiroga | 4472ab9 | 2014-04-14 09:14:23 +0200 | [diff] [blame] | 2052 | previous_operand_loc = ast->get_location(); |
Ian Romanick | 3d5cfcf | 2011-04-11 10:10:30 -0700 | [diff] [blame] | 2053 | |
Iago Toral Quiroga | 4472ab9 | 2014-04-14 09:14:23 +0200 | [diff] [blame] | 2054 | result = ast->hir(instructions, state); |
Ian Romanick | 3d5cfcf | 2011-04-11 10:10:30 -0700 | [diff] [blame] | 2055 | } |
Ian Romanick | a87ac25 | 2010-02-22 13:19:34 -0800 | [diff] [blame] | 2056 | |
Ian Romanick | a87ac25 | 2010-02-22 13:19:34 -0800 | [diff] [blame] | 2057 | /* Any errors should have already been emitted in the loop above. |
| 2058 | */ |
| 2059 | error_emitted = true; |
| 2060 | break; |
| 2061 | } |
| 2062 | } |
Kenneth Graunke | 08ba977 | 2011-04-14 17:21:59 -0700 | [diff] [blame] | 2063 | type = NULL; /* use result->type, not type. */ |
Eric Anholt | e9822f7 | 2014-03-05 17:05:54 -0800 | [diff] [blame] | 2064 | assert(result != NULL || !needs_rvalue); |
Ian Romanick | a87ac25 | 2010-02-22 13:19:34 -0800 | [diff] [blame] | 2065 | |
Eric Anholt | e9822f7 | 2014-03-05 17:05:54 -0800 | [diff] [blame] | 2066 | if (result && result->type->is_error() && !error_emitted) |
Ian Romanick | 71d0bbf | 2010-03-23 13:21:19 -0700 | [diff] [blame] | 2067 | _mesa_glsl_error(& loc, state, "type mismatch"); |
Ian Romanick | a87ac25 | 2010-02-22 13:19:34 -0800 | [diff] [blame] | 2068 | |
| 2069 | return result; |
| 2070 | } |
| 2071 | |
Ian Romanick | 05e4601 | 2015-10-07 13:03:53 -0700 | [diff] [blame] | 2072 | bool |
| 2073 | ast_expression::has_sequence_subexpression() const |
| 2074 | { |
| 2075 | switch (this->oper) { |
| 2076 | case ast_plus: |
| 2077 | case ast_neg: |
| 2078 | case ast_bit_not: |
| 2079 | case ast_logic_not: |
| 2080 | case ast_pre_inc: |
| 2081 | case ast_pre_dec: |
| 2082 | case ast_post_inc: |
| 2083 | case ast_post_dec: |
| 2084 | return this->subexpressions[0]->has_sequence_subexpression(); |
| 2085 | |
| 2086 | case ast_assign: |
| 2087 | case ast_add: |
| 2088 | case ast_sub: |
| 2089 | case ast_mul: |
| 2090 | case ast_div: |
| 2091 | case ast_mod: |
| 2092 | case ast_lshift: |
| 2093 | case ast_rshift: |
| 2094 | case ast_less: |
| 2095 | case ast_greater: |
| 2096 | case ast_lequal: |
| 2097 | case ast_gequal: |
| 2098 | case ast_nequal: |
| 2099 | case ast_equal: |
| 2100 | case ast_bit_and: |
| 2101 | case ast_bit_xor: |
| 2102 | case ast_bit_or: |
| 2103 | case ast_logic_and: |
| 2104 | case ast_logic_or: |
| 2105 | case ast_logic_xor: |
| 2106 | case ast_array_index: |
| 2107 | case ast_mul_assign: |
| 2108 | case ast_div_assign: |
| 2109 | case ast_add_assign: |
| 2110 | case ast_sub_assign: |
| 2111 | case ast_mod_assign: |
| 2112 | case ast_ls_assign: |
| 2113 | case ast_rs_assign: |
| 2114 | case ast_and_assign: |
| 2115 | case ast_xor_assign: |
| 2116 | case ast_or_assign: |
| 2117 | return this->subexpressions[0]->has_sequence_subexpression() || |
| 2118 | this->subexpressions[1]->has_sequence_subexpression(); |
| 2119 | |
| 2120 | case ast_conditional: |
| 2121 | return this->subexpressions[0]->has_sequence_subexpression() || |
| 2122 | this->subexpressions[1]->has_sequence_subexpression() || |
| 2123 | this->subexpressions[2]->has_sequence_subexpression(); |
| 2124 | |
| 2125 | case ast_sequence: |
| 2126 | return true; |
| 2127 | |
| 2128 | case ast_field_selection: |
| 2129 | case ast_identifier: |
| 2130 | case ast_int_constant: |
| 2131 | case ast_uint_constant: |
| 2132 | case ast_float_constant: |
| 2133 | case ast_bool_constant: |
| 2134 | case ast_double_constant: |
| 2135 | return false; |
| 2136 | |
| 2137 | case ast_aggregate: |
Dave Airlie | 4336196 | 2016-05-03 17:16:27 +1000 | [diff] [blame] | 2138 | return false; |
Ian Romanick | 05e4601 | 2015-10-07 13:03:53 -0700 | [diff] [blame] | 2139 | |
| 2140 | case ast_function_call: |
| 2141 | unreachable("should be handled by ast_function_expression::hir"); |
Brian Paul | cb473c4 | 2015-10-15 07:26:49 -0600 | [diff] [blame] | 2142 | |
| 2143 | case ast_unsized_array_dim: |
| 2144 | unreachable("ast_unsized_array_dim: Should never get here."); |
Ian Romanick | 05e4601 | 2015-10-07 13:03:53 -0700 | [diff] [blame] | 2145 | } |
| 2146 | |
| 2147 | return false; |
| 2148 | } |
Ian Romanick | a87ac25 | 2010-02-22 13:19:34 -0800 | [diff] [blame] | 2149 | |
Kenneth Graunke | fb9fb5f | 2010-03-26 00:25:36 -0700 | [diff] [blame] | 2150 | ir_rvalue * |
Ian Romanick | 0044e7e | 2010-03-08 23:44:00 -0800 | [diff] [blame] | 2151 | ast_expression_statement::hir(exec_list *instructions, |
Iago Toral Quiroga | 4472ab9 | 2014-04-14 09:14:23 +0200 | [diff] [blame] | 2152 | struct _mesa_glsl_parse_state *state) |
Ian Romanick | a87ac25 | 2010-02-22 13:19:34 -0800 | [diff] [blame] | 2153 | { |
Ian Romanick | a87ac25 | 2010-02-22 13:19:34 -0800 | [diff] [blame] | 2154 | /* It is possible to have expression statements that don't have an |
| 2155 | * expression. This is the solitary semicolon: |
| 2156 | * |
| 2157 | * for (i = 0; i < 5; i++) |
| 2158 | * ; |
| 2159 | * |
| 2160 | * In this case the expression will be NULL. Test for NULL and don't do |
| 2161 | * anything in that case. |
| 2162 | */ |
Ian Romanick | 18238de | 2010-03-01 13:49:10 -0800 | [diff] [blame] | 2163 | if (expression != NULL) |
Eric Anholt | e9822f7 | 2014-03-05 17:05:54 -0800 | [diff] [blame] | 2164 | expression->hir_no_rvalue(instructions, state); |
Ian Romanick | a87ac25 | 2010-02-22 13:19:34 -0800 | [diff] [blame] | 2165 | |
| 2166 | /* Statements do not have r-values. |
| 2167 | */ |
| 2168 | return NULL; |
| 2169 | } |
| 2170 | |
| 2171 | |
Kenneth Graunke | fb9fb5f | 2010-03-26 00:25:36 -0700 | [diff] [blame] | 2172 | ir_rvalue * |
Ian Romanick | 0044e7e | 2010-03-08 23:44:00 -0800 | [diff] [blame] | 2173 | ast_compound_statement::hir(exec_list *instructions, |
Iago Toral Quiroga | 4472ab9 | 2014-04-14 09:14:23 +0200 | [diff] [blame] | 2174 | struct _mesa_glsl_parse_state *state) |
Ian Romanick | a87ac25 | 2010-02-22 13:19:34 -0800 | [diff] [blame] | 2175 | { |
Ian Romanick | 18238de | 2010-03-01 13:49:10 -0800 | [diff] [blame] | 2176 | if (new_scope) |
Ian Romanick | 8bde4ce | 2010-03-19 11:57:24 -0700 | [diff] [blame] | 2177 | state->symbols->push_scope(); |
Ian Romanick | a87ac25 | 2010-02-22 13:19:34 -0800 | [diff] [blame] | 2178 | |
Ian Romanick | 2b97dc6 | 2010-05-10 17:42:05 -0700 | [diff] [blame] | 2179 | foreach_list_typed (ast_node, ast, link, &this->statements) |
Ian Romanick | 304ea90 | 2010-05-10 11:17:53 -0700 | [diff] [blame] | 2180 | ast->hir(instructions, state); |
Ian Romanick | a87ac25 | 2010-02-22 13:19:34 -0800 | [diff] [blame] | 2181 | |
Ian Romanick | 18238de | 2010-03-01 13:49:10 -0800 | [diff] [blame] | 2182 | if (new_scope) |
Ian Romanick | 8bde4ce | 2010-03-19 11:57:24 -0700 | [diff] [blame] | 2183 | state->symbols->pop_scope(); |
Ian Romanick | a87ac25 | 2010-02-22 13:19:34 -0800 | [diff] [blame] | 2184 | |
| 2185 | /* Compound statements do not have r-values. |
| 2186 | */ |
| 2187 | return NULL; |
| 2188 | } |
| 2189 | |
Timothy Arceri | bfb4875 | 2014-01-23 23:16:41 +1100 | [diff] [blame] | 2190 | /** |
| 2191 | * Evaluate the given exec_node (which should be an ast_node representing |
| 2192 | * a single array dimension) and return its integer value. |
| 2193 | */ |
Ian Romanick | 7700c73 | 2014-02-18 09:38:04 -0800 | [diff] [blame] | 2194 | static unsigned |
Timothy Arceri | bfb4875 | 2014-01-23 23:16:41 +1100 | [diff] [blame] | 2195 | process_array_size(exec_node *node, |
| 2196 | struct _mesa_glsl_parse_state *state) |
| 2197 | { |
| 2198 | exec_list dummy_instructions; |
| 2199 | |
| 2200 | ast_node *array_size = exec_node_data(ast_node, node, link); |
Timothy Arceri | 3129359 | 2015-10-15 14:32:41 +1100 | [diff] [blame] | 2201 | |
| 2202 | /** |
| 2203 | * Dimensions other than the outermost dimension can by unsized if they |
| 2204 | * are immediately sized by a constructor or initializer. |
| 2205 | */ |
| 2206 | if (((ast_expression*)array_size)->oper == ast_unsized_array_dim) |
| 2207 | return 0; |
| 2208 | |
Iago Toral Quiroga | 4472ab9 | 2014-04-14 09:14:23 +0200 | [diff] [blame] | 2209 | ir_rvalue *const ir = array_size->hir(& dummy_instructions, state); |
Timothy Arceri | bfb4875 | 2014-01-23 23:16:41 +1100 | [diff] [blame] | 2210 | YYLTYPE loc = array_size->get_location(); |
| 2211 | |
| 2212 | if (ir == NULL) { |
| 2213 | _mesa_glsl_error(& loc, state, |
| 2214 | "array size could not be resolved"); |
| 2215 | return 0; |
| 2216 | } |
| 2217 | |
| 2218 | if (!ir->type->is_integer()) { |
| 2219 | _mesa_glsl_error(& loc, state, |
| 2220 | "array size must be integer type"); |
| 2221 | return 0; |
| 2222 | } |
| 2223 | |
| 2224 | if (!ir->type->is_scalar()) { |
| 2225 | _mesa_glsl_error(& loc, state, |
| 2226 | "array size must be scalar type"); |
| 2227 | return 0; |
| 2228 | } |
| 2229 | |
| 2230 | ir_constant *const size = ir->constant_expression_value(); |
Lars Hamre | 43c6f3f | 2016-03-23 10:14:23 -0400 | [diff] [blame] | 2231 | if (size == NULL || |
| 2232 | (state->is_version(120, 300) && |
| 2233 | array_size->has_sequence_subexpression())) { |
Timothy Arceri | bfb4875 | 2014-01-23 23:16:41 +1100 | [diff] [blame] | 2234 | _mesa_glsl_error(& loc, state, "array size must be a " |
| 2235 | "constant valued expression"); |
| 2236 | return 0; |
| 2237 | } |
| 2238 | |
| 2239 | if (size->value.i[0] <= 0) { |
| 2240 | _mesa_glsl_error(& loc, state, "array size must be > 0"); |
| 2241 | return 0; |
| 2242 | } |
| 2243 | |
| 2244 | assert(size->type == ir->type); |
| 2245 | |
| 2246 | /* If the array size is const (and we've verified that |
| 2247 | * it is) then no instructions should have been emitted |
| 2248 | * when we converted it to HIR. If they were emitted, |
| 2249 | * then either the array size isn't const after all, or |
| 2250 | * we are emitting unnecessary instructions. |
| 2251 | */ |
| 2252 | assert(dummy_instructions.is_empty()); |
| 2253 | |
| 2254 | return size->value.u[0]; |
| 2255 | } |
Ian Romanick | a87ac25 | 2010-02-22 13:19:34 -0800 | [diff] [blame] | 2256 | |
Ian Romanick | 28009cd | 2010-03-30 16:59:27 -0700 | [diff] [blame] | 2257 | static const glsl_type * |
Timothy Arceri | bfb4875 | 2014-01-23 23:16:41 +1100 | [diff] [blame] | 2258 | process_array_type(YYLTYPE *loc, const glsl_type *base, |
| 2259 | ast_array_specifier *array_specifier, |
| 2260 | struct _mesa_glsl_parse_state *state) |
Ian Romanick | 28009cd | 2010-03-30 16:59:27 -0700 | [diff] [blame] | 2261 | { |
Timothy Arceri | bfb4875 | 2014-01-23 23:16:41 +1100 | [diff] [blame] | 2262 | const glsl_type *array_type = base; |
Ian Romanick | 28009cd | 2010-03-30 16:59:27 -0700 | [diff] [blame] | 2263 | |
Timothy Arceri | bfb4875 | 2014-01-23 23:16:41 +1100 | [diff] [blame] | 2264 | if (array_specifier != NULL) { |
| 2265 | if (base->is_array()) { |
Ian Romanick | ee55b84 | 2013-04-08 16:53:46 -0700 | [diff] [blame] | 2266 | |
Timothy Arceri | bfb4875 | 2014-01-23 23:16:41 +1100 | [diff] [blame] | 2267 | /* From page 19 (page 25) of the GLSL 1.20 spec: |
| 2268 | * |
| 2269 | * "Only one-dimensional arrays may be declared." |
| 2270 | */ |
Timothy Arceri | 8da9e15 | 2015-06-06 09:10:55 +1000 | [diff] [blame] | 2271 | if (!state->check_arrays_of_arrays_allowed(loc)) { |
Timothy Arceri | bfb4875 | 2014-01-23 23:16:41 +1100 | [diff] [blame] | 2272 | return glsl_type::error_type; |
| 2273 | } |
Ian Romanick | 28009cd | 2010-03-30 16:59:27 -0700 | [diff] [blame] | 2274 | } |
Timothy Arceri | bfb4875 | 2014-01-23 23:16:41 +1100 | [diff] [blame] | 2275 | |
Matt Turner | d1f6f65 | 2016-06-27 14:42:57 -0700 | [diff] [blame] | 2276 | for (exec_node *node = array_specifier->array_dimensions.get_tail_raw(); |
Timothy Arceri | bfb4875 | 2014-01-23 23:16:41 +1100 | [diff] [blame] | 2277 | !node->is_head_sentinel(); node = node->prev) { |
| 2278 | unsigned array_size = process_array_size(node, state); |
Iago Toral Quiroga | 4472ab9 | 2014-04-14 09:14:23 +0200 | [diff] [blame] | 2279 | array_type = glsl_type::get_array_instance(array_type, array_size); |
Timothy Arceri | bfb4875 | 2014-01-23 23:16:41 +1100 | [diff] [blame] | 2280 | } |
Ian Romanick | 28009cd | 2010-03-30 16:59:27 -0700 | [diff] [blame] | 2281 | } |
| 2282 | |
Timothy Arceri | bfb4875 | 2014-01-23 23:16:41 +1100 | [diff] [blame] | 2283 | return array_type; |
Ian Romanick | 28009cd | 2010-03-30 16:59:27 -0700 | [diff] [blame] | 2284 | } |
| 2285 | |
Iago Toral Quiroga | 9a00e1a | 2015-11-05 08:18:46 +0200 | [diff] [blame] | 2286 | static bool |
| 2287 | precision_qualifier_allowed(const glsl_type *type) |
| 2288 | { |
| 2289 | /* Precision qualifiers apply to floating point, integer and opaque |
| 2290 | * types. |
| 2291 | * |
| 2292 | * Section 4.5.2 (Precision Qualifiers) of the GLSL 1.30 spec says: |
| 2293 | * "Any floating point or any integer declaration can have the type |
| 2294 | * preceded by one of these precision qualifiers [...] Literal |
| 2295 | * constants do not have precision qualifiers. Neither do Boolean |
| 2296 | * variables. |
| 2297 | * |
| 2298 | * Section 4.5 (Precision and Precision Qualifiers) of the GLSL 1.30 |
| 2299 | * spec also says: |
| 2300 | * |
| 2301 | * "Precision qualifiers are added for code portability with OpenGL |
| 2302 | * ES, not for functionality. They have the same syntax as in OpenGL |
| 2303 | * ES." |
| 2304 | * |
| 2305 | * Section 8 (Built-In Functions) of the GLSL ES 1.00 spec says: |
| 2306 | * |
| 2307 | * "uniform lowp sampler2D sampler; |
| 2308 | * highp vec2 coord; |
| 2309 | * ... |
| 2310 | * lowp vec4 col = texture2D (sampler, coord); |
| 2311 | * // texture2D returns lowp" |
| 2312 | * |
| 2313 | * From this, we infer that GLSL 1.30 (and later) should allow precision |
| 2314 | * qualifiers on sampler types just like float and integer types. |
| 2315 | */ |
Ian Romanick | 9c87282 | 2016-06-13 15:22:34 -0700 | [diff] [blame] | 2316 | const glsl_type *const t = type->without_array(); |
| 2317 | |
| 2318 | return (t->is_float() || t->is_integer() || t->contains_opaque()) && |
| 2319 | !t->is_record(); |
Iago Toral Quiroga | 9a00e1a | 2015-11-05 08:18:46 +0200 | [diff] [blame] | 2320 | } |
Ian Romanick | 28009cd | 2010-03-30 16:59:27 -0700 | [diff] [blame] | 2321 | |
Ian Romanick | d612a12 | 2010-03-31 16:22:06 -0700 | [diff] [blame] | 2322 | const glsl_type * |
| 2323 | ast_type_specifier::glsl_type(const char **name, |
Iago Toral Quiroga | 4472ab9 | 2014-04-14 09:14:23 +0200 | [diff] [blame] | 2324 | struct _mesa_glsl_parse_state *state) const |
Ian Romanick | a87ac25 | 2010-02-22 13:19:34 -0800 | [diff] [blame] | 2325 | { |
Ian Romanick | d612a12 | 2010-03-31 16:22:06 -0700 | [diff] [blame] | 2326 | const struct glsl_type *type; |
Ian Romanick | a87ac25 | 2010-02-22 13:19:34 -0800 | [diff] [blame] | 2327 | |
Kenneth Graunke | ca92ae2 | 2010-09-18 11:11:09 +0200 | [diff] [blame] | 2328 | type = state->symbols->get_type(this->type_name); |
| 2329 | *name = this->type_name; |
Ian Romanick | a87ac25 | 2010-02-22 13:19:34 -0800 | [diff] [blame] | 2330 | |
Timothy Arceri | bfb4875 | 2014-01-23 23:16:41 +1100 | [diff] [blame] | 2331 | YYLTYPE loc = this->get_location(); |
| 2332 | type = process_array_type(&loc, type, this->array_specifier, state); |
Ian Romanick | a87ac25 | 2010-02-22 13:19:34 -0800 | [diff] [blame] | 2333 | |
| 2334 | return type; |
| 2335 | } |
| 2336 | |
Iago Toral Quiroga | f84bc57 | 2015-11-10 08:22:07 +0200 | [diff] [blame] | 2337 | /** |
| 2338 | * From the OpenGL ES 3.0 spec, 4.5.4 Default Precision Qualifiers: |
| 2339 | * |
| 2340 | * "The precision statement |
| 2341 | * |
| 2342 | * precision precision-qualifier type; |
| 2343 | * |
| 2344 | * can be used to establish a default precision qualifier. The type field can |
| 2345 | * be either int or float or any of the sampler types, (...) If type is float, |
| 2346 | * the directive applies to non-precision-qualified floating point type |
| 2347 | * (scalar, vector, and matrix) declarations. If type is int, the directive |
| 2348 | * applies to all non-precision-qualified integer type (scalar, vector, signed, |
| 2349 | * and unsigned) declarations." |
| 2350 | * |
| 2351 | * We use the symbol table to keep the values of the default precisions for |
| 2352 | * each 'type' in each scope and we use the 'type' string from the precision |
| 2353 | * statement as key in the symbol table. When we want to retrieve the default |
| 2354 | * precision associated with a given glsl_type we need to know the type string |
| 2355 | * associated with it. This is what this function returns. |
| 2356 | */ |
| 2357 | static const char * |
| 2358 | get_type_name_for_precision_qualifier(const glsl_type *type) |
| 2359 | { |
| 2360 | switch (type->base_type) { |
| 2361 | case GLSL_TYPE_FLOAT: |
| 2362 | return "float"; |
| 2363 | case GLSL_TYPE_UINT: |
| 2364 | case GLSL_TYPE_INT: |
| 2365 | return "int"; |
| 2366 | case GLSL_TYPE_ATOMIC_UINT: |
| 2367 | return "atomic_uint"; |
| 2368 | case GLSL_TYPE_IMAGE: |
| 2369 | /* fallthrough */ |
| 2370 | case GLSL_TYPE_SAMPLER: { |
| 2371 | const unsigned type_idx = |
| 2372 | type->sampler_array + 2 * type->sampler_shadow; |
| 2373 | const unsigned offset = type->base_type == GLSL_TYPE_SAMPLER ? 0 : 4; |
| 2374 | assert(type_idx < 4); |
Jason Ekstrand | ac08912 | 2016-02-09 13:56:23 -0800 | [diff] [blame] | 2375 | switch (type->sampled_type) { |
Iago Toral Quiroga | f84bc57 | 2015-11-10 08:22:07 +0200 | [diff] [blame] | 2376 | case GLSL_TYPE_FLOAT: |
| 2377 | switch (type->sampler_dimensionality) { |
| 2378 | case GLSL_SAMPLER_DIM_1D: { |
| 2379 | assert(type->base_type == GLSL_TYPE_SAMPLER); |
| 2380 | static const char *const names[4] = { |
| 2381 | "sampler1D", "sampler1DArray", |
| 2382 | "sampler1DShadow", "sampler1DArrayShadow" |
| 2383 | }; |
| 2384 | return names[type_idx]; |
| 2385 | } |
| 2386 | case GLSL_SAMPLER_DIM_2D: { |
| 2387 | static const char *const names[8] = { |
| 2388 | "sampler2D", "sampler2DArray", |
| 2389 | "sampler2DShadow", "sampler2DArrayShadow", |
| 2390 | "image2D", "image2DArray", NULL, NULL |
| 2391 | }; |
| 2392 | return names[offset + type_idx]; |
| 2393 | } |
| 2394 | case GLSL_SAMPLER_DIM_3D: { |
| 2395 | static const char *const names[8] = { |
| 2396 | "sampler3D", NULL, NULL, NULL, |
| 2397 | "image3D", NULL, NULL, NULL |
| 2398 | }; |
| 2399 | return names[offset + type_idx]; |
| 2400 | } |
| 2401 | case GLSL_SAMPLER_DIM_CUBE: { |
| 2402 | static const char *const names[8] = { |
| 2403 | "samplerCube", "samplerCubeArray", |
| 2404 | "samplerCubeShadow", "samplerCubeArrayShadow", |
| 2405 | "imageCube", NULL, NULL, NULL |
| 2406 | }; |
| 2407 | return names[offset + type_idx]; |
| 2408 | } |
| 2409 | case GLSL_SAMPLER_DIM_MS: { |
| 2410 | assert(type->base_type == GLSL_TYPE_SAMPLER); |
| 2411 | static const char *const names[4] = { |
| 2412 | "sampler2DMS", "sampler2DMSArray", NULL, NULL |
| 2413 | }; |
| 2414 | return names[type_idx]; |
| 2415 | } |
| 2416 | case GLSL_SAMPLER_DIM_RECT: { |
| 2417 | assert(type->base_type == GLSL_TYPE_SAMPLER); |
| 2418 | static const char *const names[4] = { |
| 2419 | "samplerRect", NULL, "samplerRectShadow", NULL |
| 2420 | }; |
| 2421 | return names[type_idx]; |
| 2422 | } |
| 2423 | case GLSL_SAMPLER_DIM_BUF: { |
Samuel Pitoiset | bb37886 | 2016-03-29 23:11:07 +0200 | [diff] [blame] | 2424 | static const char *const names[8] = { |
| 2425 | "samplerBuffer", NULL, NULL, NULL, |
| 2426 | "imageBuffer", NULL, NULL, NULL |
Iago Toral Quiroga | f84bc57 | 2015-11-10 08:22:07 +0200 | [diff] [blame] | 2427 | }; |
Samuel Pitoiset | bb37886 | 2016-03-29 23:11:07 +0200 | [diff] [blame] | 2428 | return names[offset + type_idx]; |
Iago Toral Quiroga | f84bc57 | 2015-11-10 08:22:07 +0200 | [diff] [blame] | 2429 | } |
| 2430 | case GLSL_SAMPLER_DIM_EXTERNAL: { |
| 2431 | assert(type->base_type == GLSL_TYPE_SAMPLER); |
| 2432 | static const char *const names[4] = { |
| 2433 | "samplerExternalOES", NULL, NULL, NULL |
| 2434 | }; |
| 2435 | return names[type_idx]; |
| 2436 | } |
| 2437 | default: |
| 2438 | unreachable("Unsupported sampler/image dimensionality"); |
| 2439 | } /* sampler/image float dimensionality */ |
| 2440 | break; |
| 2441 | case GLSL_TYPE_INT: |
| 2442 | switch (type->sampler_dimensionality) { |
| 2443 | case GLSL_SAMPLER_DIM_1D: { |
| 2444 | assert(type->base_type == GLSL_TYPE_SAMPLER); |
| 2445 | static const char *const names[4] = { |
| 2446 | "isampler1D", "isampler1DArray", NULL, NULL |
| 2447 | }; |
| 2448 | return names[type_idx]; |
| 2449 | } |
| 2450 | case GLSL_SAMPLER_DIM_2D: { |
| 2451 | static const char *const names[8] = { |
| 2452 | "isampler2D", "isampler2DArray", NULL, NULL, |
| 2453 | "iimage2D", "iimage2DArray", NULL, NULL |
| 2454 | }; |
| 2455 | return names[offset + type_idx]; |
| 2456 | } |
| 2457 | case GLSL_SAMPLER_DIM_3D: { |
| 2458 | static const char *const names[8] = { |
| 2459 | "isampler3D", NULL, NULL, NULL, |
| 2460 | "iimage3D", NULL, NULL, NULL |
| 2461 | }; |
| 2462 | return names[offset + type_idx]; |
| 2463 | } |
| 2464 | case GLSL_SAMPLER_DIM_CUBE: { |
| 2465 | static const char *const names[8] = { |
| 2466 | "isamplerCube", "isamplerCubeArray", NULL, NULL, |
| 2467 | "iimageCube", NULL, NULL, NULL |
| 2468 | }; |
| 2469 | return names[offset + type_idx]; |
| 2470 | } |
| 2471 | case GLSL_SAMPLER_DIM_MS: { |
| 2472 | assert(type->base_type == GLSL_TYPE_SAMPLER); |
| 2473 | static const char *const names[4] = { |
| 2474 | "isampler2DMS", "isampler2DMSArray", NULL, NULL |
| 2475 | }; |
| 2476 | return names[type_idx]; |
| 2477 | } |
| 2478 | case GLSL_SAMPLER_DIM_RECT: { |
| 2479 | assert(type->base_type == GLSL_TYPE_SAMPLER); |
| 2480 | static const char *const names[4] = { |
| 2481 | "isamplerRect", NULL, "isamplerRectShadow", NULL |
| 2482 | }; |
| 2483 | return names[type_idx]; |
| 2484 | } |
| 2485 | case GLSL_SAMPLER_DIM_BUF: { |
Samuel Pitoiset | bb37886 | 2016-03-29 23:11:07 +0200 | [diff] [blame] | 2486 | static const char *const names[8] = { |
| 2487 | "isamplerBuffer", NULL, NULL, NULL, |
| 2488 | "iimageBuffer", NULL, NULL, NULL |
Iago Toral Quiroga | f84bc57 | 2015-11-10 08:22:07 +0200 | [diff] [blame] | 2489 | }; |
Samuel Pitoiset | bb37886 | 2016-03-29 23:11:07 +0200 | [diff] [blame] | 2490 | return names[offset + type_idx]; |
Iago Toral Quiroga | f84bc57 | 2015-11-10 08:22:07 +0200 | [diff] [blame] | 2491 | } |
| 2492 | default: |
| 2493 | unreachable("Unsupported isampler/iimage dimensionality"); |
| 2494 | } /* sampler/image int dimensionality */ |
| 2495 | break; |
| 2496 | case GLSL_TYPE_UINT: |
| 2497 | switch (type->sampler_dimensionality) { |
| 2498 | case GLSL_SAMPLER_DIM_1D: { |
| 2499 | assert(type->base_type == GLSL_TYPE_SAMPLER); |
| 2500 | static const char *const names[4] = { |
| 2501 | "usampler1D", "usampler1DArray", NULL, NULL |
| 2502 | }; |
| 2503 | return names[type_idx]; |
| 2504 | } |
| 2505 | case GLSL_SAMPLER_DIM_2D: { |
| 2506 | static const char *const names[8] = { |
| 2507 | "usampler2D", "usampler2DArray", NULL, NULL, |
| 2508 | "uimage2D", "uimage2DArray", NULL, NULL |
| 2509 | }; |
| 2510 | return names[offset + type_idx]; |
| 2511 | } |
| 2512 | case GLSL_SAMPLER_DIM_3D: { |
| 2513 | static const char *const names[8] = { |
| 2514 | "usampler3D", NULL, NULL, NULL, |
| 2515 | "uimage3D", NULL, NULL, NULL |
| 2516 | }; |
| 2517 | return names[offset + type_idx]; |
| 2518 | } |
| 2519 | case GLSL_SAMPLER_DIM_CUBE: { |
| 2520 | static const char *const names[8] = { |
| 2521 | "usamplerCube", "usamplerCubeArray", NULL, NULL, |
| 2522 | "uimageCube", NULL, NULL, NULL |
| 2523 | }; |
| 2524 | return names[offset + type_idx]; |
| 2525 | } |
| 2526 | case GLSL_SAMPLER_DIM_MS: { |
| 2527 | assert(type->base_type == GLSL_TYPE_SAMPLER); |
| 2528 | static const char *const names[4] = { |
| 2529 | "usampler2DMS", "usampler2DMSArray", NULL, NULL |
| 2530 | }; |
| 2531 | return names[type_idx]; |
| 2532 | } |
| 2533 | case GLSL_SAMPLER_DIM_RECT: { |
| 2534 | assert(type->base_type == GLSL_TYPE_SAMPLER); |
| 2535 | static const char *const names[4] = { |
| 2536 | "usamplerRect", NULL, "usamplerRectShadow", NULL |
| 2537 | }; |
| 2538 | return names[type_idx]; |
| 2539 | } |
| 2540 | case GLSL_SAMPLER_DIM_BUF: { |
Samuel Pitoiset | bb37886 | 2016-03-29 23:11:07 +0200 | [diff] [blame] | 2541 | static const char *const names[8] = { |
| 2542 | "usamplerBuffer", NULL, NULL, NULL, |
| 2543 | "uimageBuffer", NULL, NULL, NULL |
Iago Toral Quiroga | f84bc57 | 2015-11-10 08:22:07 +0200 | [diff] [blame] | 2544 | }; |
Samuel Pitoiset | bb37886 | 2016-03-29 23:11:07 +0200 | [diff] [blame] | 2545 | return names[offset + type_idx]; |
Iago Toral Quiroga | f84bc57 | 2015-11-10 08:22:07 +0200 | [diff] [blame] | 2546 | } |
| 2547 | default: |
| 2548 | unreachable("Unsupported usampler/uimage dimensionality"); |
| 2549 | } /* sampler/image uint dimensionality */ |
| 2550 | break; |
| 2551 | default: |
| 2552 | unreachable("Unsupported sampler/image type"); |
| 2553 | } /* sampler/image type */ |
| 2554 | break; |
| 2555 | } /* GLSL_TYPE_SAMPLER/GLSL_TYPE_IMAGE */ |
| 2556 | break; |
| 2557 | default: |
| 2558 | unreachable("Unsupported type"); |
| 2559 | } /* base type */ |
| 2560 | } |
| 2561 | |
| 2562 | static unsigned |
| 2563 | select_gles_precision(unsigned qual_precision, |
| 2564 | const glsl_type *type, |
| 2565 | struct _mesa_glsl_parse_state *state, YYLTYPE *loc) |
| 2566 | { |
| 2567 | /* Precision qualifiers do not have any meaning in Desktop GLSL. |
| 2568 | * In GLES we take the precision from the type qualifier if present, |
| 2569 | * otherwise, if the type of the variable allows precision qualifiers at |
| 2570 | * all, we look for the default precision qualifier for that type in the |
| 2571 | * current scope. |
| 2572 | */ |
| 2573 | assert(state->es_shader); |
| 2574 | |
| 2575 | unsigned precision = GLSL_PRECISION_NONE; |
| 2576 | if (qual_precision) { |
| 2577 | precision = qual_precision; |
| 2578 | } else if (precision_qualifier_allowed(type)) { |
| 2579 | const char *type_name = |
| 2580 | get_type_name_for_precision_qualifier(type->without_array()); |
| 2581 | assert(type_name != NULL); |
| 2582 | |
| 2583 | precision = |
| 2584 | state->symbols->get_default_precision_qualifier(type_name); |
| 2585 | if (precision == ast_precision_none) { |
| 2586 | _mesa_glsl_error(loc, state, |
| 2587 | "No precision specified in this scope for type `%s'", |
| 2588 | type->name); |
| 2589 | } |
| 2590 | } |
Tapani Pälli | d997d5c | 2016-10-07 08:23:41 +0300 | [diff] [blame] | 2591 | |
| 2592 | |
| 2593 | /* Section 4.1.7.3 (Atomic Counters) of the GLSL ES 3.10 spec says: |
| 2594 | * |
| 2595 | * "The default precision of all atomic types is highp. It is an error to |
| 2596 | * declare an atomic type with a different precision or to specify the |
| 2597 | * default precision for an atomic type to be lowp or mediump." |
| 2598 | */ |
| 2599 | if (type->base_type == GLSL_TYPE_ATOMIC_UINT && |
| 2600 | precision != ast_precision_high) { |
| 2601 | _mesa_glsl_error(loc, state, |
| 2602 | "atomic_uint can only have highp precision qualifier"); |
| 2603 | } |
| 2604 | |
Iago Toral Quiroga | f84bc57 | 2015-11-10 08:22:07 +0200 | [diff] [blame] | 2605 | return precision; |
| 2606 | } |
| 2607 | |
Ian Romanick | cabd457 | 2013-08-09 15:17:18 -0700 | [diff] [blame] | 2608 | const glsl_type * |
| 2609 | ast_fully_specified_type::glsl_type(const char **name, |
| 2610 | struct _mesa_glsl_parse_state *state) const |
| 2611 | { |
Iago Toral Quiroga | f84bc57 | 2015-11-10 08:22:07 +0200 | [diff] [blame] | 2612 | return this->specifier->glsl_type(name, state); |
Ian Romanick | cabd457 | 2013-08-09 15:17:18 -0700 | [diff] [blame] | 2613 | } |
Ian Romanick | a87ac25 | 2010-02-22 13:19:34 -0800 | [diff] [blame] | 2614 | |
Paul Berry | 1872055 | 2012-12-18 15:24:39 -0800 | [diff] [blame] | 2615 | /** |
| 2616 | * Determine whether a toplevel variable declaration declares a varying. This |
| 2617 | * function operates by examining the variable's mode and the shader target, |
| 2618 | * so it correctly identifies linkage variables regardless of whether they are |
| 2619 | * declared using the deprecated "varying" syntax or the new "in/out" syntax. |
| 2620 | * |
| 2621 | * Passing a non-toplevel variable declaration (e.g. a function parameter) to |
| 2622 | * this function will produce undefined results. |
| 2623 | */ |
| 2624 | static bool |
Paul Berry | 665b8d7 | 2014-01-07 10:11:39 -0800 | [diff] [blame] | 2625 | is_varying_var(ir_variable *var, gl_shader_stage target) |
Paul Berry | 1872055 | 2012-12-18 15:24:39 -0800 | [diff] [blame] | 2626 | { |
| 2627 | switch (target) { |
Paul Berry | 7963fde | 2013-12-16 13:09:20 -0800 | [diff] [blame] | 2628 | case MESA_SHADER_VERTEX: |
Tapani Pälli | 33ee2c6 | 2013-12-12 13:51:01 +0200 | [diff] [blame] | 2629 | return var->data.mode == ir_var_shader_out; |
Paul Berry | 7963fde | 2013-12-16 13:09:20 -0800 | [diff] [blame] | 2630 | case MESA_SHADER_FRAGMENT: |
Tapani Pälli | 33ee2c6 | 2013-12-12 13:51:01 +0200 | [diff] [blame] | 2631 | return var->data.mode == ir_var_shader_in; |
Paul Berry | 1872055 | 2012-12-18 15:24:39 -0800 | [diff] [blame] | 2632 | default: |
Tapani Pälli | 33ee2c6 | 2013-12-12 13:51:01 +0200 | [diff] [blame] | 2633 | return var->data.mode == ir_var_shader_out || var->data.mode == ir_var_shader_in; |
Paul Berry | 1872055 | 2012-12-18 15:24:39 -0800 | [diff] [blame] | 2634 | } |
| 2635 | } |
| 2636 | |
Dave Airlie | ab8ea1b | 2016-05-23 14:18:03 +1000 | [diff] [blame] | 2637 | static bool |
| 2638 | is_allowed_invariant(ir_variable *var, struct _mesa_glsl_parse_state *state) |
| 2639 | { |
| 2640 | if (is_varying_var(var, state->stage)) |
| 2641 | return true; |
| 2642 | |
| 2643 | /* From Section 4.6.1 ("The Invariant Qualifier") GLSL 1.20 spec: |
| 2644 | * "Only variables output from a vertex shader can be candidates |
| 2645 | * for invariance". |
| 2646 | */ |
| 2647 | if (!state->is_version(130, 0)) |
| 2648 | return false; |
| 2649 | |
| 2650 | /* |
| 2651 | * Later specs remove this language - so allowed invariant |
| 2652 | * on fragment shader outputs as well. |
| 2653 | */ |
| 2654 | if (state->stage == MESA_SHADER_FRAGMENT && |
| 2655 | var->data.mode == ir_var_shader_out) |
| 2656 | return true; |
| 2657 | return false; |
| 2658 | } |
Paul Berry | 1872055 | 2012-12-18 15:24:39 -0800 | [diff] [blame] | 2659 | |
Ian Romanick | bb47a4d | 2013-01-16 12:01:50 -0800 | [diff] [blame] | 2660 | /** |
| 2661 | * Matrix layout qualifiers are only allowed on certain types |
| 2662 | */ |
| 2663 | static void |
| 2664 | validate_matrix_layout_for_type(struct _mesa_glsl_parse_state *state, |
Iago Toral Quiroga | 4472ab9 | 2014-04-14 09:14:23 +0200 | [diff] [blame] | 2665 | YYLTYPE *loc, |
Matt Turner | d8ac987 | 2013-08-26 14:14:03 -0700 | [diff] [blame] | 2666 | const glsl_type *type, |
| 2667 | ir_variable *var) |
Ian Romanick | bb47a4d | 2013-01-16 12:01:50 -0800 | [diff] [blame] | 2668 | { |
Iago Toral Quiroga | 1146696 | 2015-06-05 09:11:53 +0200 | [diff] [blame] | 2669 | if (var && !var->is_in_buffer_block()) { |
Matt Turner | d8ac987 | 2013-08-26 14:14:03 -0700 | [diff] [blame] | 2670 | /* Layout qualifiers may only apply to interface blocks and fields in |
| 2671 | * them. |
| 2672 | */ |
| 2673 | _mesa_glsl_error(loc, state, |
| 2674 | "uniform block layout qualifiers row_major and " |
| 2675 | "column_major may not be applied to variables " |
| 2676 | "outside of uniform blocks"); |
Timothy Arceri | a01b8c7 | 2015-11-13 11:21:42 +1100 | [diff] [blame] | 2677 | } else if (!type->without_array()->is_matrix()) { |
Ian Romanick | dded321 | 2013-08-15 11:24:11 -0700 | [diff] [blame] | 2678 | /* The OpenGL ES 3.0 conformance tests did not originally allow |
| 2679 | * matrix layout qualifiers on non-matrices. However, the OpenGL |
| 2680 | * 4.4 and OpenGL ES 3.0 (revision TBD) specifications were |
| 2681 | * amended to specifically allow these layouts on all types. Emit |
| 2682 | * a warning so that people know their code may not be portable. |
| 2683 | */ |
| 2684 | _mesa_glsl_warning(loc, state, |
| 2685 | "uniform block layout qualifiers row_major and " |
| 2686 | "column_major applied to non-matrix types may " |
| 2687 | "be rejected by older compilers"); |
Ian Romanick | bb47a4d | 2013-01-16 12:01:50 -0800 | [diff] [blame] | 2688 | } |
| 2689 | } |
| 2690 | |
Kenneth Graunke | 5e5e120 | 2013-07-16 12:03:28 -0700 | [diff] [blame] | 2691 | static bool |
Timothy Arceri | f6a8c7e | 2016-03-11 23:00:16 +1100 | [diff] [blame] | 2692 | validate_xfb_buffer_qualifier(YYLTYPE *loc, |
| 2693 | struct _mesa_glsl_parse_state *state, |
| 2694 | unsigned xfb_buffer) { |
| 2695 | if (xfb_buffer >= state->Const.MaxTransformFeedbackBuffers) { |
| 2696 | _mesa_glsl_error(loc, state, |
| 2697 | "invalid xfb_buffer specified %d is larger than " |
| 2698 | "MAX_TRANSFORM_FEEDBACK_BUFFERS - 1 (%d).", |
| 2699 | xfb_buffer, |
| 2700 | state->Const.MaxTransformFeedbackBuffers - 1); |
| 2701 | return false; |
| 2702 | } |
| 2703 | |
| 2704 | return true; |
| 2705 | } |
| 2706 | |
Timothy Arceri | edddad0 | 2016-02-24 15:21:59 +1100 | [diff] [blame] | 2707 | /* From the ARB_enhanced_layouts spec: |
| 2708 | * |
| 2709 | * "Variables and block members qualified with *xfb_offset* can be |
| 2710 | * scalars, vectors, matrices, structures, and (sized) arrays of these. |
| 2711 | * The offset must be a multiple of the size of the first component of |
| 2712 | * the first qualified variable or block member, or a compile-time error |
| 2713 | * results. Further, if applied to an aggregate containing a double, |
| 2714 | * the offset must also be a multiple of 8, and the space taken in the |
| 2715 | * buffer will be a multiple of 8. |
| 2716 | */ |
| 2717 | static bool |
| 2718 | validate_xfb_offset_qualifier(YYLTYPE *loc, |
| 2719 | struct _mesa_glsl_parse_state *state, |
| 2720 | int xfb_offset, const glsl_type *type, |
| 2721 | unsigned component_size) { |
| 2722 | const glsl_type *t_without_array = type->without_array(); |
| 2723 | |
| 2724 | if (xfb_offset != -1 && type->is_unsized_array()) { |
| 2725 | _mesa_glsl_error(loc, state, |
| 2726 | "xfb_offset can't be used with unsized arrays."); |
| 2727 | return false; |
| 2728 | } |
| 2729 | |
| 2730 | /* Make sure nested structs don't contain unsized arrays, and validate |
| 2731 | * any xfb_offsets on interface members. |
| 2732 | */ |
| 2733 | if (t_without_array->is_record() || t_without_array->is_interface()) |
| 2734 | for (unsigned int i = 0; i < t_without_array->length; i++) { |
| 2735 | const glsl_type *member_t = t_without_array->fields.structure[i].type; |
| 2736 | |
| 2737 | /* When the interface block doesn't have an xfb_offset qualifier then |
| 2738 | * we apply the component size rules at the member level. |
| 2739 | */ |
| 2740 | if (xfb_offset == -1) |
| 2741 | component_size = member_t->contains_double() ? 8 : 4; |
| 2742 | |
| 2743 | int xfb_offset = t_without_array->fields.structure[i].offset; |
| 2744 | validate_xfb_offset_qualifier(loc, state, xfb_offset, member_t, |
| 2745 | component_size); |
| 2746 | } |
| 2747 | |
| 2748 | /* Nested structs or interface block without offset may not have had an |
| 2749 | * offset applied yet so return. |
| 2750 | */ |
| 2751 | if (xfb_offset == -1) { |
| 2752 | return true; |
| 2753 | } |
| 2754 | |
| 2755 | if (xfb_offset % component_size) { |
| 2756 | _mesa_glsl_error(loc, state, |
| 2757 | "invalid qualifier xfb_offset=%d must be a multiple " |
| 2758 | "of the first component size of the first qualified " |
| 2759 | "variable or block member. Or double if an aggregate " |
| 2760 | "that contains a double (%d).", |
| 2761 | xfb_offset, component_size); |
| 2762 | return false; |
| 2763 | } |
| 2764 | |
| 2765 | return true; |
| 2766 | } |
| 2767 | |
Timothy Arceri | f6a8c7e | 2016-03-11 23:00:16 +1100 | [diff] [blame] | 2768 | static bool |
Timothy Arceri | db3c36a | 2015-11-14 14:32:38 +1100 | [diff] [blame] | 2769 | validate_stream_qualifier(YYLTYPE *loc, struct _mesa_glsl_parse_state *state, |
| 2770 | unsigned stream) |
| 2771 | { |
| 2772 | if (stream >= state->ctx->Const.MaxVertexStreams) { |
| 2773 | _mesa_glsl_error(loc, state, |
| 2774 | "invalid stream specified %d is larger than " |
| 2775 | "MAX_VERTEX_STREAMS - 1 (%d).", |
| 2776 | stream, state->ctx->Const.MaxVertexStreams - 1); |
| 2777 | return false; |
| 2778 | } |
| 2779 | |
| 2780 | return true; |
| 2781 | } |
| 2782 | |
Timothy Arceri | 64710db | 2015-11-15 00:55:29 +1100 | [diff] [blame] | 2783 | static void |
| 2784 | apply_explicit_binding(struct _mesa_glsl_parse_state *state, |
| 2785 | YYLTYPE *loc, |
| 2786 | ir_variable *var, |
| 2787 | const glsl_type *type, |
| 2788 | const ast_type_qualifier *qual) |
Kenneth Graunke | 5e5e120 | 2013-07-16 12:03:28 -0700 | [diff] [blame] | 2789 | { |
Timothy Arceri | ad89748 | 2015-05-27 20:12:42 +1000 | [diff] [blame] | 2790 | if (!qual->flags.q.uniform && !qual->flags.q.buffer) { |
Kenneth Graunke | 5e5e120 | 2013-07-16 12:03:28 -0700 | [diff] [blame] | 2791 | _mesa_glsl_error(loc, state, |
Samuel Iglesias Gonsalvez | fa0a86c | 2015-03-18 09:02:51 +0100 | [diff] [blame] | 2792 | "the \"binding\" qualifier only applies to uniforms and " |
| 2793 | "shader storage buffer objects"); |
Timothy Arceri | 64710db | 2015-11-15 00:55:29 +1100 | [diff] [blame] | 2794 | return; |
Kenneth Graunke | 5e5e120 | 2013-07-16 12:03:28 -0700 | [diff] [blame] | 2795 | } |
| 2796 | |
Timothy Arceri | e74fe2a | 2015-11-15 00:42:44 +1100 | [diff] [blame] | 2797 | unsigned qual_binding; |
| 2798 | if (!process_qualifier_constant(state, loc, "binding", qual->binding, |
| 2799 | &qual_binding)) { |
Timothy Arceri | 64710db | 2015-11-15 00:55:29 +1100 | [diff] [blame] | 2800 | return; |
Kenneth Graunke | 5e5e120 | 2013-07-16 12:03:28 -0700 | [diff] [blame] | 2801 | } |
| 2802 | |
| 2803 | const struct gl_context *const ctx = state->ctx; |
Timothy Arceri | 1d401f9 | 2015-05-27 21:33:45 +1000 | [diff] [blame] | 2804 | unsigned elements = type->is_array() ? type->arrays_of_arrays_size() : 1; |
Timothy Arceri | e74fe2a | 2015-11-15 00:42:44 +1100 | [diff] [blame] | 2805 | unsigned max_index = qual_binding + elements - 1; |
Timothy Arceri | ad89748 | 2015-05-27 20:12:42 +1000 | [diff] [blame] | 2806 | const glsl_type *base_type = type->without_array(); |
Kenneth Graunke | 5e5e120 | 2013-07-16 12:03:28 -0700 | [diff] [blame] | 2807 | |
Timothy Arceri | ad89748 | 2015-05-27 20:12:42 +1000 | [diff] [blame] | 2808 | if (base_type->is_interface()) { |
Kenneth Graunke | 5e5e120 | 2013-07-16 12:03:28 -0700 | [diff] [blame] | 2809 | /* UBOs. From page 60 of the GLSL 4.20 specification: |
| 2810 | * "If the binding point for any uniform block instance is less than zero, |
| 2811 | * or greater than or equal to the implementation-dependent maximum |
| 2812 | * number of uniform buffer bindings, a compilation error will occur. |
| 2813 | * When the binding identifier is used with a uniform block instanced as |
| 2814 | * an array of size N, all elements of the array from binding through |
| 2815 | * binding + N – 1 must be within this range." |
| 2816 | * |
| 2817 | * The implementation-dependent maximum is GL_MAX_UNIFORM_BUFFER_BINDINGS. |
| 2818 | */ |
Timothy Arceri | ad89748 | 2015-05-27 20:12:42 +1000 | [diff] [blame] | 2819 | if (qual->flags.q.uniform && |
Samuel Iglesias Gonsalvez | fa0a86c | 2015-03-18 09:02:51 +0100 | [diff] [blame] | 2820 | max_index >= ctx->Const.MaxUniformBufferBindings) { |
Timothy Arceri | e74fe2a | 2015-11-15 00:42:44 +1100 | [diff] [blame] | 2821 | _mesa_glsl_error(loc, state, "layout(binding = %u) for %d UBOs exceeds " |
Paul Berry | 4d7899f | 2013-07-25 19:56:43 -0700 | [diff] [blame] | 2822 | "the maximum number of UBO binding points (%d)", |
Timothy Arceri | e74fe2a | 2015-11-15 00:42:44 +1100 | [diff] [blame] | 2823 | qual_binding, elements, |
Kenneth Graunke | 5e5e120 | 2013-07-16 12:03:28 -0700 | [diff] [blame] | 2824 | ctx->Const.MaxUniformBufferBindings); |
Timothy Arceri | 64710db | 2015-11-15 00:55:29 +1100 | [diff] [blame] | 2825 | return; |
Kenneth Graunke | 5e5e120 | 2013-07-16 12:03:28 -0700 | [diff] [blame] | 2826 | } |
Timothy Arceri | ad89748 | 2015-05-27 20:12:42 +1000 | [diff] [blame] | 2827 | |
Samuel Iglesias Gonsalvez | fa0a86c | 2015-03-18 09:02:51 +0100 | [diff] [blame] | 2828 | /* SSBOs. From page 67 of the GLSL 4.30 specification: |
| 2829 | * "If the binding point for any uniform or shader storage block instance |
| 2830 | * is less than zero, or greater than or equal to the |
| 2831 | * implementation-dependent maximum number of uniform buffer bindings, a |
| 2832 | * compile-time error will occur. When the binding identifier is used |
| 2833 | * with a uniform or shader storage block instanced as an array of size |
| 2834 | * N, all elements of the array from binding through binding + N – 1 must |
| 2835 | * be within this range." |
| 2836 | */ |
Timothy Arceri | ad89748 | 2015-05-27 20:12:42 +1000 | [diff] [blame] | 2837 | if (qual->flags.q.buffer && |
Samuel Iglesias Gonsalvez | fa0a86c | 2015-03-18 09:02:51 +0100 | [diff] [blame] | 2838 | max_index >= ctx->Const.MaxShaderStorageBufferBindings) { |
Timothy Arceri | e74fe2a | 2015-11-15 00:42:44 +1100 | [diff] [blame] | 2839 | _mesa_glsl_error(loc, state, "layout(binding = %u) for %d SSBOs exceeds " |
Samuel Iglesias Gonsalvez | fa0a86c | 2015-03-18 09:02:51 +0100 | [diff] [blame] | 2840 | "the maximum number of SSBO binding points (%d)", |
Timothy Arceri | e74fe2a | 2015-11-15 00:42:44 +1100 | [diff] [blame] | 2841 | qual_binding, elements, |
Samuel Iglesias Gonsalvez | fa0a86c | 2015-03-18 09:02:51 +0100 | [diff] [blame] | 2842 | ctx->Const.MaxShaderStorageBufferBindings); |
Timothy Arceri | 64710db | 2015-11-15 00:55:29 +1100 | [diff] [blame] | 2843 | return; |
Samuel Iglesias Gonsalvez | fa0a86c | 2015-03-18 09:02:51 +0100 | [diff] [blame] | 2844 | } |
Timothy Arceri | ad89748 | 2015-05-27 20:12:42 +1000 | [diff] [blame] | 2845 | } else if (base_type->is_sampler()) { |
Kenneth Graunke | 5e5e120 | 2013-07-16 12:03:28 -0700 | [diff] [blame] | 2846 | /* Samplers. From page 63 of the GLSL 4.20 specification: |
| 2847 | * "If the binding is less than zero, or greater than or equal to the |
| 2848 | * implementation-dependent maximum supported number of units, a |
| 2849 | * compilation error will occur. When the binding identifier is used |
| 2850 | * with an array of size N, all elements of the array from binding |
| 2851 | * through binding + N - 1 must be within this range." |
| 2852 | */ |
Ilia Mirkin | fccf012 | 2015-06-23 00:16:59 -0400 | [diff] [blame] | 2853 | unsigned limit = ctx->Const.MaxCombinedTextureImageUnits; |
Kenneth Graunke | 5e5e120 | 2013-07-16 12:03:28 -0700 | [diff] [blame] | 2854 | |
| 2855 | if (max_index >= limit) { |
| 2856 | _mesa_glsl_error(loc, state, "layout(binding = %d) for %d samplers " |
| 2857 | "exceeds the maximum number of texture image units " |
Timothy Arceri | e74fe2a | 2015-11-15 00:42:44 +1100 | [diff] [blame] | 2858 | "(%u)", qual_binding, elements, limit); |
Kenneth Graunke | 5e5e120 | 2013-07-16 12:03:28 -0700 | [diff] [blame] | 2859 | |
Timothy Arceri | 64710db | 2015-11-15 00:55:29 +1100 | [diff] [blame] | 2860 | return; |
Kenneth Graunke | 5e5e120 | 2013-07-16 12:03:28 -0700 | [diff] [blame] | 2861 | } |
Timothy Arceri | ad89748 | 2015-05-27 20:12:42 +1000 | [diff] [blame] | 2862 | } else if (base_type->contains_atomic()) { |
Francisco Jerez | e63bb29 | 2013-10-20 12:38:07 -0700 | [diff] [blame] | 2863 | assert(ctx->Const.MaxAtomicBufferBindings <= MAX_COMBINED_ATOMIC_BUFFERS); |
Timothy Arceri | e74fe2a | 2015-11-15 00:42:44 +1100 | [diff] [blame] | 2864 | if (qual_binding >= ctx->Const.MaxAtomicBufferBindings) { |
Francisco Jerez | e63bb29 | 2013-10-20 12:38:07 -0700 | [diff] [blame] | 2865 | _mesa_glsl_error(loc, state, "layout(binding = %d) exceeds the " |
| 2866 | " maximum number of atomic counter buffer bindings" |
Timothy Arceri | e74fe2a | 2015-11-15 00:42:44 +1100 | [diff] [blame] | 2867 | "(%u)", qual_binding, |
Francisco Jerez | e63bb29 | 2013-10-20 12:38:07 -0700 | [diff] [blame] | 2868 | ctx->Const.MaxAtomicBufferBindings); |
| 2869 | |
Timothy Arceri | 64710db | 2015-11-15 00:55:29 +1100 | [diff] [blame] | 2870 | return; |
Francisco Jerez | e63bb29 | 2013-10-20 12:38:07 -0700 | [diff] [blame] | 2871 | } |
Matt Turner | c200e60 | 2015-12-07 11:14:56 -0800 | [diff] [blame] | 2872 | } else if ((state->is_version(420, 310) || |
| 2873 | state->ARB_shading_language_420pack_enable) && |
| 2874 | base_type->is_image()) { |
Francisco Jerez | 241774a | 2015-08-17 01:25:11 +0300 | [diff] [blame] | 2875 | assert(ctx->Const.MaxImageUnits <= MAX_IMAGE_UNITS); |
| 2876 | if (max_index >= ctx->Const.MaxImageUnits) { |
| 2877 | _mesa_glsl_error(loc, state, "Image binding %d exceeds the " |
| 2878 | " maximum number of image units (%d)", max_index, |
| 2879 | ctx->Const.MaxImageUnits); |
Timothy Arceri | 64710db | 2015-11-15 00:55:29 +1100 | [diff] [blame] | 2880 | return; |
Francisco Jerez | 241774a | 2015-08-17 01:25:11 +0300 | [diff] [blame] | 2881 | } |
| 2882 | |
Kenneth Graunke | 5e5e120 | 2013-07-16 12:03:28 -0700 | [diff] [blame] | 2883 | } else { |
| 2884 | _mesa_glsl_error(loc, state, |
| 2885 | "the \"binding\" qualifier only applies to uniform " |
Francisco Jerez | 241774a | 2015-08-17 01:25:11 +0300 | [diff] [blame] | 2886 | "blocks, opaque variables, or arrays thereof"); |
Timothy Arceri | 64710db | 2015-11-15 00:55:29 +1100 | [diff] [blame] | 2887 | return; |
Kenneth Graunke | 5e5e120 | 2013-07-16 12:03:28 -0700 | [diff] [blame] | 2888 | } |
| 2889 | |
Timothy Arceri | 64710db | 2015-11-15 00:55:29 +1100 | [diff] [blame] | 2890 | var->data.explicit_binding = true; |
Timothy Arceri | e74fe2a | 2015-11-15 00:42:44 +1100 | [diff] [blame] | 2891 | var->data.binding = qual_binding; |
Timothy Arceri | 64710db | 2015-11-15 00:55:29 +1100 | [diff] [blame] | 2892 | |
| 2893 | return; |
Kenneth Graunke | 5e5e120 | 2013-07-16 12:03:28 -0700 | [diff] [blame] | 2894 | } |
| 2895 | |
Paul Berry | 81a5067 | 2013-10-22 14:54:10 -0700 | [diff] [blame] | 2896 | |
Andres Gomez | c750029 | 2016-03-24 01:13:26 +0200 | [diff] [blame] | 2897 | static void |
| 2898 | validate_interpolation_qualifier(struct _mesa_glsl_parse_state *state, |
| 2899 | YYLTYPE *loc, |
Kenneth Graunke | ac1181f | 2016-07-07 02:02:38 -0700 | [diff] [blame] | 2900 | const glsl_interp_mode interpolation, |
Andres Gomez | c750029 | 2016-03-24 01:13:26 +0200 | [diff] [blame] | 2901 | const struct ast_type_qualifier *qual, |
| 2902 | const struct glsl_type *var_type, |
| 2903 | ir_variable_mode mode) |
| 2904 | { |
| 2905 | /* Interpolation qualifiers can only apply to shader inputs or outputs, but |
| 2906 | * not to vertex shader inputs nor fragment shader outputs. |
| 2907 | * |
| 2908 | * From section 4.3 ("Storage Qualifiers") of the GLSL 1.30 spec: |
| 2909 | * "Outputs from a vertex shader (out) and inputs to a fragment |
| 2910 | * shader (in) can be further qualified with one or more of these |
| 2911 | * interpolation qualifiers" |
| 2912 | * ... |
| 2913 | * "These interpolation qualifiers may only precede the qualifiers in, |
| 2914 | * centroid in, out, or centroid out in a declaration. They do not apply |
| 2915 | * to the deprecated storage qualifiers varying or centroid |
| 2916 | * varying. They also do not apply to inputs into a vertex shader or |
| 2917 | * outputs from a fragment shader." |
| 2918 | * |
| 2919 | * From section 4.3 ("Storage Qualifiers") of the GLSL ES 3.00 spec: |
| 2920 | * "Outputs from a shader (out) and inputs to a shader (in) can be |
| 2921 | * further qualified with one of these interpolation qualifiers." |
| 2922 | * ... |
| 2923 | * "These interpolation qualifiers may only precede the qualifiers |
| 2924 | * in, centroid in, out, or centroid out in a declaration. They do |
| 2925 | * not apply to inputs into a vertex shader or outputs from a |
| 2926 | * fragment shader." |
| 2927 | */ |
| 2928 | if (state->is_version(130, 300) |
Kenneth Graunke | ac1181f | 2016-07-07 02:02:38 -0700 | [diff] [blame] | 2929 | && interpolation != INTERP_MODE_NONE) { |
Andres Gomez | c750029 | 2016-03-24 01:13:26 +0200 | [diff] [blame] | 2930 | const char *i = interpolation_string(interpolation); |
| 2931 | if (mode != ir_var_shader_in && mode != ir_var_shader_out) |
| 2932 | _mesa_glsl_error(loc, state, |
| 2933 | "interpolation qualifier `%s' can only be applied to " |
| 2934 | "shader inputs or outputs.", i); |
| 2935 | |
| 2936 | switch (state->stage) { |
| 2937 | case MESA_SHADER_VERTEX: |
| 2938 | if (mode == ir_var_shader_in) { |
| 2939 | _mesa_glsl_error(loc, state, |
| 2940 | "interpolation qualifier '%s' cannot be applied to " |
| 2941 | "vertex shader inputs", i); |
| 2942 | } |
| 2943 | break; |
| 2944 | case MESA_SHADER_FRAGMENT: |
| 2945 | if (mode == ir_var_shader_out) { |
| 2946 | _mesa_glsl_error(loc, state, |
| 2947 | "interpolation qualifier '%s' cannot be applied to " |
| 2948 | "fragment shader outputs", i); |
| 2949 | } |
| 2950 | break; |
| 2951 | default: |
| 2952 | break; |
| 2953 | } |
| 2954 | } |
| 2955 | |
| 2956 | /* Interpolation qualifiers cannot be applied to 'centroid' and |
| 2957 | * 'centroid varying'. |
| 2958 | * |
| 2959 | * From section 4.3 ("Storage Qualifiers") of the GLSL 1.30 spec: |
| 2960 | * "interpolation qualifiers may only precede the qualifiers in, |
| 2961 | * centroid in, out, or centroid out in a declaration. They do not apply |
| 2962 | * to the deprecated storage qualifiers varying or centroid varying." |
| 2963 | * |
| 2964 | * These deprecated storage qualifiers do not exist in GLSL ES 3.00. |
| 2965 | */ |
| 2966 | if (state->is_version(130, 0) |
Kenneth Graunke | ac1181f | 2016-07-07 02:02:38 -0700 | [diff] [blame] | 2967 | && interpolation != INTERP_MODE_NONE |
Andres Gomez | c750029 | 2016-03-24 01:13:26 +0200 | [diff] [blame] | 2968 | && qual->flags.q.varying) { |
| 2969 | |
| 2970 | const char *i = interpolation_string(interpolation); |
| 2971 | const char *s; |
| 2972 | if (qual->flags.q.centroid) |
| 2973 | s = "centroid varying"; |
| 2974 | else |
| 2975 | s = "varying"; |
| 2976 | |
| 2977 | _mesa_glsl_error(loc, state, |
| 2978 | "qualifier '%s' cannot be applied to the " |
| 2979 | "deprecated storage qualifier '%s'", i, s); |
| 2980 | } |
| 2981 | |
| 2982 | /* Integer fragment inputs must be qualified with 'flat'. In GLSL ES, |
| 2983 | * so must integer vertex outputs. |
| 2984 | * |
| 2985 | * From section 4.3.4 ("Inputs") of the GLSL 1.50 spec: |
| 2986 | * "Fragment shader inputs that are signed or unsigned integers or |
| 2987 | * integer vectors must be qualified with the interpolation qualifier |
| 2988 | * flat." |
| 2989 | * |
| 2990 | * From section 4.3.4 ("Input Variables") of the GLSL 3.00 ES spec: |
| 2991 | * "Fragment shader inputs that are, or contain, signed or unsigned |
| 2992 | * integers or integer vectors must be qualified with the |
| 2993 | * interpolation qualifier flat." |
| 2994 | * |
| 2995 | * From section 4.3.6 ("Output Variables") of the GLSL 3.00 ES spec: |
| 2996 | * "Vertex shader outputs that are, or contain, signed or unsigned |
| 2997 | * integers or integer vectors must be qualified with the |
| 2998 | * interpolation qualifier flat." |
| 2999 | * |
| 3000 | * Note that prior to GLSL 1.50, this requirement applied to vertex |
| 3001 | * outputs rather than fragment inputs. That creates problems in the |
| 3002 | * presence of geometry shaders, so we adopt the GLSL 1.50 rule for all |
| 3003 | * desktop GL shaders. For GLSL ES shaders, we follow the spec and |
| 3004 | * apply the restriction to both vertex outputs and fragment inputs. |
| 3005 | * |
| 3006 | * Note also that the desktop GLSL specs are missing the text "or |
| 3007 | * contain"; this is presumably an oversight, since there is no |
| 3008 | * reasonable way to interpolate a fragment shader input that contains |
| 3009 | * an integer. See Khronos bug #15671. |
| 3010 | */ |
| 3011 | if (state->is_version(130, 300) |
| 3012 | && var_type->contains_integer() |
Kenneth Graunke | ac1181f | 2016-07-07 02:02:38 -0700 | [diff] [blame] | 3013 | && interpolation != INTERP_MODE_FLAT |
Kenneth Graunke | 493237d | 2016-10-05 16:09:54 -0700 | [diff] [blame] | 3014 | && state->stage == MESA_SHADER_FRAGMENT |
| 3015 | && mode == ir_var_shader_in) { |
| 3016 | _mesa_glsl_error(loc, state, "if a fragment input is (or contains) " |
| 3017 | "an integer, then it must be qualified with 'flat'"); |
Andres Gomez | c750029 | 2016-03-24 01:13:26 +0200 | [diff] [blame] | 3018 | } |
| 3019 | |
| 3020 | /* Double fragment inputs must be qualified with 'flat'. |
| 3021 | * |
| 3022 | * From the "Overview" of the ARB_gpu_shader_fp64 extension spec: |
| 3023 | * "This extension does not support interpolation of double-precision |
| 3024 | * values; doubles used as fragment shader inputs must be qualified as |
| 3025 | * "flat"." |
| 3026 | * |
| 3027 | * From section 4.3.4 ("Inputs") of the GLSL 4.00 spec: |
| 3028 | * "Fragment shader inputs that are signed or unsigned integers, integer |
| 3029 | * vectors, or any double-precision floating-point type must be |
| 3030 | * qualified with the interpolation qualifier flat." |
| 3031 | * |
| 3032 | * Note that the GLSL specs are missing the text "or contain"; this is |
| 3033 | * presumably an oversight. See Khronos bug #15671. |
| 3034 | * |
| 3035 | * The 'double' type does not exist in GLSL ES so far. |
| 3036 | */ |
Timothy Arceri | db2a351 | 2016-05-28 11:56:17 +1000 | [diff] [blame] | 3037 | if (state->has_double() |
Andres Gomez | c750029 | 2016-03-24 01:13:26 +0200 | [diff] [blame] | 3038 | && var_type->contains_double() |
Kenneth Graunke | ac1181f | 2016-07-07 02:02:38 -0700 | [diff] [blame] | 3039 | && interpolation != INTERP_MODE_FLAT |
Andres Gomez | c750029 | 2016-03-24 01:13:26 +0200 | [diff] [blame] | 3040 | && state->stage == MESA_SHADER_FRAGMENT |
| 3041 | && mode == ir_var_shader_in) { |
| 3042 | _mesa_glsl_error(loc, state, "if a fragment input is (or contains) " |
| 3043 | "a double, then it must be qualified with 'flat'"); |
| 3044 | } |
| 3045 | } |
| 3046 | |
Kenneth Graunke | ac1181f | 2016-07-07 02:02:38 -0700 | [diff] [blame] | 3047 | static glsl_interp_mode |
Paul Berry | 81a5067 | 2013-10-22 14:54:10 -0700 | [diff] [blame] | 3048 | interpret_interpolation_qualifier(const struct ast_type_qualifier *qual, |
Andres Gomez | c750029 | 2016-03-24 01:13:26 +0200 | [diff] [blame] | 3049 | const struct glsl_type *var_type, |
Paul Berry | 81a5067 | 2013-10-22 14:54:10 -0700 | [diff] [blame] | 3050 | ir_variable_mode mode, |
| 3051 | struct _mesa_glsl_parse_state *state, |
| 3052 | YYLTYPE *loc) |
| 3053 | { |
Kenneth Graunke | ac1181f | 2016-07-07 02:02:38 -0700 | [diff] [blame] | 3054 | glsl_interp_mode interpolation; |
Paul Berry | 81a5067 | 2013-10-22 14:54:10 -0700 | [diff] [blame] | 3055 | if (qual->flags.q.flat) |
Kenneth Graunke | ac1181f | 2016-07-07 02:02:38 -0700 | [diff] [blame] | 3056 | interpolation = INTERP_MODE_FLAT; |
Paul Berry | 81a5067 | 2013-10-22 14:54:10 -0700 | [diff] [blame] | 3057 | else if (qual->flags.q.noperspective) |
Kenneth Graunke | ac1181f | 2016-07-07 02:02:38 -0700 | [diff] [blame] | 3058 | interpolation = INTERP_MODE_NOPERSPECTIVE; |
Paul Berry | 81a5067 | 2013-10-22 14:54:10 -0700 | [diff] [blame] | 3059 | else if (qual->flags.q.smooth) |
Kenneth Graunke | ac1181f | 2016-07-07 02:02:38 -0700 | [diff] [blame] | 3060 | interpolation = INTERP_MODE_SMOOTH; |
Andres Gomez | c750029 | 2016-03-24 01:13:26 +0200 | [diff] [blame] | 3061 | else if (state->es_shader && |
| 3062 | ((mode == ir_var_shader_in && |
| 3063 | state->stage != MESA_SHADER_VERTEX) || |
| 3064 | (mode == ir_var_shader_out && |
| 3065 | state->stage != MESA_SHADER_FRAGMENT))) |
Timothy Arceri | 07e6a37 | 2016-02-16 11:03:56 +1100 | [diff] [blame] | 3066 | /* Section 4.3.9 (Interpolation) of the GLSL ES 3.00 spec says: |
| 3067 | * |
| 3068 | * "When no interpolation qualifier is present, smooth interpolation |
| 3069 | * is used." |
| 3070 | */ |
Kenneth Graunke | ac1181f | 2016-07-07 02:02:38 -0700 | [diff] [blame] | 3071 | interpolation = INTERP_MODE_SMOOTH; |
Andres Gomez | c750029 | 2016-03-24 01:13:26 +0200 | [diff] [blame] | 3072 | else |
Kenneth Graunke | ac1181f | 2016-07-07 02:02:38 -0700 | [diff] [blame] | 3073 | interpolation = INTERP_MODE_NONE; |
Andres Gomez | c750029 | 2016-03-24 01:13:26 +0200 | [diff] [blame] | 3074 | |
| 3075 | validate_interpolation_qualifier(state, loc, |
| 3076 | interpolation, |
| 3077 | qual, var_type, mode); |
Paul Berry | 81a5067 | 2013-10-22 14:54:10 -0700 | [diff] [blame] | 3078 | |
| 3079 | return interpolation; |
| 3080 | } |
| 3081 | |
| 3082 | |
Ian Romanick | a87ac25 | 2010-02-22 13:19:34 -0800 | [diff] [blame] | 3083 | static void |
Timothy Arceri | d4fbf11 | 2015-11-13 15:43:13 +1100 | [diff] [blame] | 3084 | apply_explicit_location(const struct ast_type_qualifier *qual, |
| 3085 | ir_variable *var, |
| 3086 | struct _mesa_glsl_parse_state *state, |
| 3087 | YYLTYPE *loc) |
Ian Romanick | 8f00a77 | 2013-09-25 10:48:18 -0700 | [diff] [blame] | 3088 | { |
Ian Romanick | 8f00a77 | 2013-09-25 10:48:18 -0700 | [diff] [blame] | 3089 | bool fail = false; |
Ian Romanick | 8f00a77 | 2013-09-25 10:48:18 -0700 | [diff] [blame] | 3090 | |
Timothy Arceri | d1f2354 | 2015-11-13 15:10:57 +1100 | [diff] [blame] | 3091 | unsigned qual_location; |
| 3092 | if (!process_qualifier_constant(state, loc, "location", qual->location, |
| 3093 | &qual_location)) { |
| 3094 | return; |
| 3095 | } |
| 3096 | |
Tapani Pälli | e8fb8b1 | 2014-03-05 12:35:03 +0200 | [diff] [blame] | 3097 | /* Checks for GL_ARB_explicit_uniform_location. */ |
| 3098 | if (qual->flags.q.uniform) { |
| 3099 | if (!state->check_explicit_uniform_location_allowed(loc, var)) |
| 3100 | return; |
| 3101 | |
| 3102 | const struct gl_context *const ctx = state->ctx; |
Timothy Arceri | d1f2354 | 2015-11-13 15:10:57 +1100 | [diff] [blame] | 3103 | unsigned max_loc = qual_location + var->type->uniform_locations() - 1; |
Tapani Pälli | e8fb8b1 | 2014-03-05 12:35:03 +0200 | [diff] [blame] | 3104 | |
Tapani Pälli | e8fb8b1 | 2014-03-05 12:35:03 +0200 | [diff] [blame] | 3105 | if (max_loc >= ctx->Const.MaxUserAssignableUniformLocations) { |
| 3106 | _mesa_glsl_error(loc, state, "location(s) consumed by uniform %s " |
| 3107 | ">= MAX_UNIFORM_LOCATIONS (%u)", var->name, |
| 3108 | ctx->Const.MaxUserAssignableUniformLocations); |
| 3109 | return; |
| 3110 | } |
| 3111 | |
| 3112 | var->data.explicit_location = true; |
Timothy Arceri | d1f2354 | 2015-11-13 15:10:57 +1100 | [diff] [blame] | 3113 | var->data.location = qual_location; |
Tapani Pälli | e8fb8b1 | 2014-03-05 12:35:03 +0200 | [diff] [blame] | 3114 | return; |
| 3115 | } |
| 3116 | |
Ian Romanick | 4d14b19 | 2013-09-25 16:16:00 -0700 | [diff] [blame] | 3117 | /* Between GL_ARB_explicit_attrib_location an |
| 3118 | * GL_ARB_separate_shader_objects, the inputs and outputs of any shader |
| 3119 | * stage can be assigned explicit locations. The checking here associates |
| 3120 | * the correct extension with the correct stage's input / output: |
Ian Romanick | 8f00a77 | 2013-09-25 10:48:18 -0700 | [diff] [blame] | 3121 | * |
Ian Romanick | 4d14b19 | 2013-09-25 16:16:00 -0700 | [diff] [blame] | 3122 | * input output |
| 3123 | * ----- ------ |
| 3124 | * vertex explicit_loc sso |
Fabian Bieler | 497eb29 | 2014-03-20 22:44:43 +0100 | [diff] [blame] | 3125 | * tess control sso sso |
| 3126 | * tess eval sso sso |
Ian Romanick | 4d14b19 | 2013-09-25 16:16:00 -0700 | [diff] [blame] | 3127 | * geometry sso sso |
| 3128 | * fragment sso explicit_loc |
Ian Romanick | 8f00a77 | 2013-09-25 10:48:18 -0700 | [diff] [blame] | 3129 | */ |
Paul Berry | 665b8d7 | 2014-01-07 10:11:39 -0800 | [diff] [blame] | 3130 | switch (state->stage) { |
Paul Berry | 7963fde | 2013-12-16 13:09:20 -0800 | [diff] [blame] | 3131 | case MESA_SHADER_VERTEX: |
Tapani Pälli | 33ee2c6 | 2013-12-12 13:51:01 +0200 | [diff] [blame] | 3132 | if (var->data.mode == ir_var_shader_in) { |
Ian Romanick | 5cb80f0 | 2013-09-25 14:36:27 -0700 | [diff] [blame] | 3133 | if (!state->check_explicit_attrib_location_allowed(loc, var)) |
| 3134 | return; |
| 3135 | |
Ian Romanick | 9d6294f | 2013-09-25 13:53:56 -0700 | [diff] [blame] | 3136 | break; |
Ian Romanick | 8f00a77 | 2013-09-25 10:48:18 -0700 | [diff] [blame] | 3137 | } |
Ian Romanick | 9d6294f | 2013-09-25 13:53:56 -0700 | [diff] [blame] | 3138 | |
Ian Romanick | 4d14b19 | 2013-09-25 16:16:00 -0700 | [diff] [blame] | 3139 | if (var->data.mode == ir_var_shader_out) { |
| 3140 | if (!state->check_separate_shader_objects_allowed(loc, var)) |
| 3141 | return; |
| 3142 | |
| 3143 | break; |
| 3144 | } |
| 3145 | |
Ian Romanick | 9d6294f | 2013-09-25 13:53:56 -0700 | [diff] [blame] | 3146 | fail = true; |
Ian Romanick | 8f00a77 | 2013-09-25 10:48:18 -0700 | [diff] [blame] | 3147 | break; |
| 3148 | |
Fabian Bieler | 497eb29 | 2014-03-20 22:44:43 +0100 | [diff] [blame] | 3149 | case MESA_SHADER_TESS_CTRL: |
| 3150 | case MESA_SHADER_TESS_EVAL: |
Paul Berry | 7963fde | 2013-12-16 13:09:20 -0800 | [diff] [blame] | 3151 | case MESA_SHADER_GEOMETRY: |
Ian Romanick | 4d14b19 | 2013-09-25 16:16:00 -0700 | [diff] [blame] | 3152 | if (var->data.mode == ir_var_shader_in || var->data.mode == ir_var_shader_out) { |
| 3153 | if (!state->check_separate_shader_objects_allowed(loc, var)) |
| 3154 | return; |
| 3155 | |
| 3156 | break; |
| 3157 | } |
| 3158 | |
| 3159 | fail = true; |
| 3160 | break; |
Ian Romanick | 8f00a77 | 2013-09-25 10:48:18 -0700 | [diff] [blame] | 3161 | |
Paul Berry | 7963fde | 2013-12-16 13:09:20 -0800 | [diff] [blame] | 3162 | case MESA_SHADER_FRAGMENT: |
Ian Romanick | 4d14b19 | 2013-09-25 16:16:00 -0700 | [diff] [blame] | 3163 | if (var->data.mode == ir_var_shader_in) { |
| 3164 | if (!state->check_separate_shader_objects_allowed(loc, var)) |
| 3165 | return; |
| 3166 | |
| 3167 | break; |
| 3168 | } |
| 3169 | |
Tapani Pälli | 33ee2c6 | 2013-12-12 13:51:01 +0200 | [diff] [blame] | 3170 | if (var->data.mode == ir_var_shader_out) { |
Ian Romanick | 5cb80f0 | 2013-09-25 14:36:27 -0700 | [diff] [blame] | 3171 | if (!state->check_explicit_attrib_location_allowed(loc, var)) |
| 3172 | return; |
| 3173 | |
Ian Romanick | 9d6294f | 2013-09-25 13:53:56 -0700 | [diff] [blame] | 3174 | break; |
Ian Romanick | 8f00a77 | 2013-09-25 10:48:18 -0700 | [diff] [blame] | 3175 | } |
Ian Romanick | 9d6294f | 2013-09-25 13:53:56 -0700 | [diff] [blame] | 3176 | |
| 3177 | fail = true; |
Ian Romanick | 8f00a77 | 2013-09-25 10:48:18 -0700 | [diff] [blame] | 3178 | break; |
Paul Berry | c61ec8d | 2014-01-06 20:06:05 -0800 | [diff] [blame] | 3179 | |
| 3180 | case MESA_SHADER_COMPUTE: |
| 3181 | _mesa_glsl_error(loc, state, |
| 3182 | "compute shader variables cannot be given " |
| 3183 | "explicit locations"); |
| 3184 | return; |
Ian Romanick | 8f00a77 | 2013-09-25 10:48:18 -0700 | [diff] [blame] | 3185 | }; |
| 3186 | |
| 3187 | if (fail) { |
| 3188 | _mesa_glsl_error(loc, state, |
Ian Romanick | 9d6294f | 2013-09-25 13:53:56 -0700 | [diff] [blame] | 3189 | "%s cannot be given an explicit location in %s shader", |
| 3190 | mode_string(var), |
Iago Toral Quiroga | 4472ab9 | 2014-04-14 09:14:23 +0200 | [diff] [blame] | 3191 | _mesa_shader_stage_to_string(state->stage)); |
Ian Romanick | 8f00a77 | 2013-09-25 10:48:18 -0700 | [diff] [blame] | 3192 | } else { |
Tapani Pälli | 447bb90 | 2013-12-12 15:08:59 +0200 | [diff] [blame] | 3193 | var->data.explicit_location = true; |
Ian Romanick | 8f00a77 | 2013-09-25 10:48:18 -0700 | [diff] [blame] | 3194 | |
Timothy Arceri | 3994ef5 | 2015-10-22 16:18:44 +1100 | [diff] [blame] | 3195 | switch (state->stage) { |
| 3196 | case MESA_SHADER_VERTEX: |
| 3197 | var->data.location = (var->data.mode == ir_var_shader_in) |
Timothy Arceri | d1f2354 | 2015-11-13 15:10:57 +1100 | [diff] [blame] | 3198 | ? (qual_location + VERT_ATTRIB_GENERIC0) |
| 3199 | : (qual_location + VARYING_SLOT_VAR0); |
Timothy Arceri | 3994ef5 | 2015-10-22 16:18:44 +1100 | [diff] [blame] | 3200 | break; |
Ian Romanick | 4d14b19 | 2013-09-25 16:16:00 -0700 | [diff] [blame] | 3201 | |
Timothy Arceri | 3994ef5 | 2015-10-22 16:18:44 +1100 | [diff] [blame] | 3202 | case MESA_SHADER_TESS_CTRL: |
| 3203 | case MESA_SHADER_TESS_EVAL: |
| 3204 | case MESA_SHADER_GEOMETRY: |
| 3205 | if (var->data.patch) |
Timothy Arceri | d1f2354 | 2015-11-13 15:10:57 +1100 | [diff] [blame] | 3206 | var->data.location = qual_location + VARYING_SLOT_PATCH0; |
Timothy Arceri | 3994ef5 | 2015-10-22 16:18:44 +1100 | [diff] [blame] | 3207 | else |
Timothy Arceri | d1f2354 | 2015-11-13 15:10:57 +1100 | [diff] [blame] | 3208 | var->data.location = qual_location + VARYING_SLOT_VAR0; |
Timothy Arceri | 3994ef5 | 2015-10-22 16:18:44 +1100 | [diff] [blame] | 3209 | break; |
Ian Romanick | 4d14b19 | 2013-09-25 16:16:00 -0700 | [diff] [blame] | 3210 | |
Timothy Arceri | 3994ef5 | 2015-10-22 16:18:44 +1100 | [diff] [blame] | 3211 | case MESA_SHADER_FRAGMENT: |
| 3212 | var->data.location = (var->data.mode == ir_var_shader_out) |
Timothy Arceri | d1f2354 | 2015-11-13 15:10:57 +1100 | [diff] [blame] | 3213 | ? (qual_location + FRAG_RESULT_DATA0) |
| 3214 | : (qual_location + VARYING_SLOT_VAR0); |
Timothy Arceri | 3994ef5 | 2015-10-22 16:18:44 +1100 | [diff] [blame] | 3215 | break; |
| 3216 | case MESA_SHADER_COMPUTE: |
| 3217 | assert(!"Unexpected shader type"); |
| 3218 | break; |
Ian Romanick | 8f00a77 | 2013-09-25 10:48:18 -0700 | [diff] [blame] | 3219 | } |
| 3220 | |
Timothy Arceri | f7af69c | 2015-11-09 09:34:40 +1100 | [diff] [blame] | 3221 | /* Check if index was set for the uniform instead of the function */ |
| 3222 | if (qual->flags.q.explicit_index && qual->flags.q.subroutine) { |
| 3223 | _mesa_glsl_error(loc, state, "an index qualifier can only be " |
| 3224 | "used with subroutine functions"); |
| 3225 | return; |
| 3226 | } |
| 3227 | |
Timothy Arceri | efa34e4 | 2015-11-14 13:09:46 +1100 | [diff] [blame] | 3228 | unsigned qual_index; |
| 3229 | if (qual->flags.q.explicit_index && |
| 3230 | process_qualifier_constant(state, loc, "index", qual->index, |
| 3231 | &qual_index)) { |
Ian Romanick | 8f00a77 | 2013-09-25 10:48:18 -0700 | [diff] [blame] | 3232 | /* From the GLSL 4.30 specification, section 4.4.2 (Output |
| 3233 | * Layout Qualifiers): |
| 3234 | * |
| 3235 | * "It is also a compile-time error if a fragment shader |
| 3236 | * sets a layout index to less than 0 or greater than 1." |
| 3237 | * |
| 3238 | * Older specifications don't mandate a behavior; we take |
| 3239 | * this as a clarification and always generate the error. |
| 3240 | */ |
Timothy Arceri | efa34e4 | 2015-11-14 13:09:46 +1100 | [diff] [blame] | 3241 | if (qual_index > 1) { |
Ian Romanick | 8f00a77 | 2013-09-25 10:48:18 -0700 | [diff] [blame] | 3242 | _mesa_glsl_error(loc, state, |
| 3243 | "explicit index may only be 0 or 1"); |
| 3244 | } else { |
Tapani Pälli | 447bb90 | 2013-12-12 15:08:59 +0200 | [diff] [blame] | 3245 | var->data.explicit_index = true; |
Timothy Arceri | efa34e4 | 2015-11-14 13:09:46 +1100 | [diff] [blame] | 3246 | var->data.index = qual_index; |
Ian Romanick | 8f00a77 | 2013-09-25 10:48:18 -0700 | [diff] [blame] | 3247 | } |
| 3248 | } |
| 3249 | } |
| 3250 | } |
| 3251 | |
| 3252 | static void |
Francisco Jerez | 94a95e0 | 2013-11-25 15:57:56 -0800 | [diff] [blame] | 3253 | apply_image_qualifier_to_variable(const struct ast_type_qualifier *qual, |
| 3254 | ir_variable *var, |
| 3255 | struct _mesa_glsl_parse_state *state, |
| 3256 | YYLTYPE *loc) |
| 3257 | { |
Timothy Arceri | d67515b | 2015-04-30 20:45:54 +1000 | [diff] [blame] | 3258 | const glsl_type *base_type = var->type->without_array(); |
Francisco Jerez | 94a95e0 | 2013-11-25 15:57:56 -0800 | [diff] [blame] | 3259 | |
| 3260 | if (base_type->is_image()) { |
| 3261 | if (var->data.mode != ir_var_uniform && |
| 3262 | var->data.mode != ir_var_function_in) { |
| 3263 | _mesa_glsl_error(loc, state, "image variables may only be declared as " |
| 3264 | "function parameters or uniform-qualified " |
| 3265 | "global variables"); |
| 3266 | } |
| 3267 | |
Ian Romanick | 932b0ef | 2014-07-14 15:48:38 -0700 | [diff] [blame] | 3268 | var->data.image_read_only |= qual->flags.q.read_only; |
| 3269 | var->data.image_write_only |= qual->flags.q.write_only; |
| 3270 | var->data.image_coherent |= qual->flags.q.coherent; |
| 3271 | var->data.image_volatile |= qual->flags.q._volatile; |
| 3272 | var->data.image_restrict |= qual->flags.q.restrict_flag; |
Francisco Jerez | 94a95e0 | 2013-11-25 15:57:56 -0800 | [diff] [blame] | 3273 | var->data.read_only = true; |
| 3274 | |
| 3275 | if (qual->flags.q.explicit_image_format) { |
| 3276 | if (var->data.mode == ir_var_function_in) { |
| 3277 | _mesa_glsl_error(loc, state, "format qualifiers cannot be " |
| 3278 | "used on image function parameters"); |
| 3279 | } |
| 3280 | |
Jason Ekstrand | ac08912 | 2016-02-09 13:56:23 -0800 | [diff] [blame] | 3281 | if (qual->image_base_type != base_type->sampled_type) { |
Francisco Jerez | 94a95e0 | 2013-11-25 15:57:56 -0800 | [diff] [blame] | 3282 | _mesa_glsl_error(loc, state, "format qualifier doesn't match the " |
| 3283 | "base data type of the image"); |
| 3284 | } |
| 3285 | |
Ian Romanick | 932b0ef | 2014-07-14 15:48:38 -0700 | [diff] [blame] | 3286 | var->data.image_format = qual->image_format; |
Francisco Jerez | 94a95e0 | 2013-11-25 15:57:56 -0800 | [diff] [blame] | 3287 | } else { |
Francisco Jerez | 527ae5d | 2015-08-17 01:26:40 +0300 | [diff] [blame] | 3288 | if (var->data.mode == ir_var_uniform) { |
| 3289 | if (state->es_shader) { |
| 3290 | _mesa_glsl_error(loc, state, "all image uniforms " |
| 3291 | "must have a format layout qualifier"); |
| 3292 | |
| 3293 | } else if (!qual->flags.q.write_only) { |
| 3294 | _mesa_glsl_error(loc, state, "image uniforms not qualified with " |
| 3295 | "`writeonly' must have a format layout " |
| 3296 | "qualifier"); |
| 3297 | } |
Francisco Jerez | 94a95e0 | 2013-11-25 15:57:56 -0800 | [diff] [blame] | 3298 | } |
| 3299 | |
Ian Romanick | 932b0ef | 2014-07-14 15:48:38 -0700 | [diff] [blame] | 3300 | var->data.image_format = GL_NONE; |
Francisco Jerez | 94a95e0 | 2013-11-25 15:57:56 -0800 | [diff] [blame] | 3301 | } |
Francisco Jerez | ee7bf34 | 2015-08-17 01:27:43 +0300 | [diff] [blame] | 3302 | |
| 3303 | /* From page 70 of the GLSL ES 3.1 specification: |
| 3304 | * |
| 3305 | * "Except for image variables qualified with the format qualifiers |
| 3306 | * r32f, r32i, and r32ui, image variables must specify either memory |
| 3307 | * qualifier readonly or the memory qualifier writeonly." |
| 3308 | */ |
| 3309 | if (state->es_shader && |
| 3310 | var->data.image_format != GL_R32F && |
| 3311 | var->data.image_format != GL_R32I && |
| 3312 | var->data.image_format != GL_R32UI && |
| 3313 | !var->data.image_read_only && |
| 3314 | !var->data.image_write_only) { |
| 3315 | _mesa_glsl_error(loc, state, "image variables of format other than " |
| 3316 | "r32f, r32i or r32ui must be qualified `readonly' or " |
| 3317 | "`writeonly'"); |
| 3318 | } |
| 3319 | |
Francisco Jerez | b5994d2 | 2015-01-31 22:07:47 +0200 | [diff] [blame] | 3320 | } else if (qual->flags.q.read_only || |
| 3321 | qual->flags.q.write_only || |
| 3322 | qual->flags.q.coherent || |
| 3323 | qual->flags.q._volatile || |
| 3324 | qual->flags.q.restrict_flag || |
| 3325 | qual->flags.q.explicit_image_format) { |
| 3326 | _mesa_glsl_error(loc, state, "memory qualifiers may only be applied to " |
| 3327 | "images"); |
Francisco Jerez | 94a95e0 | 2013-11-25 15:57:56 -0800 | [diff] [blame] | 3328 | } |
| 3329 | } |
| 3330 | |
Anuj Phogat | 581e4ac | 2014-02-04 10:38:18 -0800 | [diff] [blame] | 3331 | static inline const char* |
| 3332 | get_layout_qualifier_string(bool origin_upper_left, bool pixel_center_integer) |
| 3333 | { |
| 3334 | if (origin_upper_left && pixel_center_integer) |
| 3335 | return "origin_upper_left, pixel_center_integer"; |
| 3336 | else if (origin_upper_left) |
| 3337 | return "origin_upper_left"; |
| 3338 | else if (pixel_center_integer) |
| 3339 | return "pixel_center_integer"; |
| 3340 | else |
| 3341 | return " "; |
| 3342 | } |
| 3343 | |
| 3344 | static inline bool |
| 3345 | is_conflicting_fragcoord_redeclaration(struct _mesa_glsl_parse_state *state, |
| 3346 | const struct ast_type_qualifier *qual) |
| 3347 | { |
| 3348 | /* If gl_FragCoord was previously declared, and the qualifiers were |
| 3349 | * different in any way, return true. |
| 3350 | */ |
| 3351 | if (state->fs_redeclares_gl_fragcoord) { |
| 3352 | return (state->fs_pixel_center_integer != qual->flags.q.pixel_center_integer |
| 3353 | || state->fs_origin_upper_left != qual->flags.q.origin_upper_left); |
| 3354 | } |
| 3355 | |
| 3356 | return false; |
| 3357 | } |
| 3358 | |
Timothy Arceri | dea0af8 | 2015-10-15 14:35:41 +1100 | [diff] [blame] | 3359 | static inline void |
| 3360 | validate_array_dimensions(const glsl_type *t, |
| 3361 | struct _mesa_glsl_parse_state *state, |
| 3362 | YYLTYPE *loc) { |
| 3363 | if (t->is_array()) { |
| 3364 | t = t->fields.array; |
| 3365 | while (t->is_array()) { |
| 3366 | if (t->is_unsized_array()) { |
| 3367 | _mesa_glsl_error(loc, state, |
| 3368 | "only the outermost array dimension can " |
| 3369 | "be unsized", |
| 3370 | t->name); |
| 3371 | break; |
| 3372 | } |
| 3373 | t = t->fields.array; |
| 3374 | } |
| 3375 | } |
| 3376 | } |
| 3377 | |
Francisco Jerez | 94a95e0 | 2013-11-25 15:57:56 -0800 | [diff] [blame] | 3378 | static void |
Timothy Arceri | 6498037 | 2015-11-12 17:43:52 +1100 | [diff] [blame] | 3379 | apply_layout_qualifier_to_variable(const struct ast_type_qualifier *qual, |
| 3380 | ir_variable *var, |
| 3381 | struct _mesa_glsl_parse_state *state, |
| 3382 | YYLTYPE *loc) |
| 3383 | { |
| 3384 | if (var->name != NULL && strcmp(var->name, "gl_FragCoord") == 0) { |
| 3385 | |
| 3386 | /* Section 4.3.8.1, page 39 of GLSL 1.50 spec says: |
| 3387 | * |
| 3388 | * "Within any shader, the first redeclarations of gl_FragCoord |
| 3389 | * must appear before any use of gl_FragCoord." |
| 3390 | * |
| 3391 | * Generate a compiler error if above condition is not met by the |
| 3392 | * fragment shader. |
| 3393 | */ |
| 3394 | ir_variable *earlier = state->symbols->get_variable("gl_FragCoord"); |
| 3395 | if (earlier != NULL && |
| 3396 | earlier->data.used && |
| 3397 | !state->fs_redeclares_gl_fragcoord) { |
| 3398 | _mesa_glsl_error(loc, state, |
| 3399 | "gl_FragCoord used before its first redeclaration " |
| 3400 | "in fragment shader"); |
| 3401 | } |
| 3402 | |
| 3403 | /* Make sure all gl_FragCoord redeclarations specify the same layout |
| 3404 | * qualifiers. |
| 3405 | */ |
| 3406 | if (is_conflicting_fragcoord_redeclaration(state, qual)) { |
| 3407 | const char *const qual_string = |
| 3408 | get_layout_qualifier_string(qual->flags.q.origin_upper_left, |
| 3409 | qual->flags.q.pixel_center_integer); |
| 3410 | |
| 3411 | const char *const state_string = |
| 3412 | get_layout_qualifier_string(state->fs_origin_upper_left, |
| 3413 | state->fs_pixel_center_integer); |
| 3414 | |
| 3415 | _mesa_glsl_error(loc, state, |
| 3416 | "gl_FragCoord redeclared with different layout " |
| 3417 | "qualifiers (%s) and (%s) ", |
| 3418 | state_string, |
| 3419 | qual_string); |
| 3420 | } |
| 3421 | state->fs_origin_upper_left = qual->flags.q.origin_upper_left; |
| 3422 | state->fs_pixel_center_integer = qual->flags.q.pixel_center_integer; |
| 3423 | state->fs_redeclares_gl_fragcoord_with_no_layout_qualifiers = |
| 3424 | !qual->flags.q.origin_upper_left && !qual->flags.q.pixel_center_integer; |
| 3425 | state->fs_redeclares_gl_fragcoord = |
| 3426 | state->fs_origin_upper_left || |
| 3427 | state->fs_pixel_center_integer || |
| 3428 | state->fs_redeclares_gl_fragcoord_with_no_layout_qualifiers; |
| 3429 | } |
| 3430 | |
| 3431 | var->data.pixel_center_integer = qual->flags.q.pixel_center_integer; |
| 3432 | var->data.origin_upper_left = qual->flags.q.origin_upper_left; |
| 3433 | if ((qual->flags.q.origin_upper_left || qual->flags.q.pixel_center_integer) |
| 3434 | && (strcmp(var->name, "gl_FragCoord") != 0)) { |
| 3435 | const char *const qual_string = (qual->flags.q.origin_upper_left) |
| 3436 | ? "origin_upper_left" : "pixel_center_integer"; |
| 3437 | |
| 3438 | _mesa_glsl_error(loc, state, |
Timothy Arceri | 222f66a | 2016-09-28 16:04:08 +1000 | [diff] [blame] | 3439 | "layout qualifier `%s' can only be applied to " |
| 3440 | "fragment shader input `gl_FragCoord'", |
| 3441 | qual_string); |
Timothy Arceri | 6498037 | 2015-11-12 17:43:52 +1100 | [diff] [blame] | 3442 | } |
| 3443 | |
| 3444 | if (qual->flags.q.explicit_location) { |
Timothy Arceri | d4fbf11 | 2015-11-13 15:43:13 +1100 | [diff] [blame] | 3445 | apply_explicit_location(qual, var, state, loc); |
Timothy Arceri | 9443857 | 2015-11-11 06:24:53 +1100 | [diff] [blame] | 3446 | |
| 3447 | if (qual->flags.q.explicit_component) { |
| 3448 | unsigned qual_component; |
| 3449 | if (process_qualifier_constant(state, loc, "component", |
| 3450 | qual->component, &qual_component)) { |
| 3451 | const glsl_type *type = var->type->without_array(); |
| 3452 | unsigned components = type->component_slots(); |
| 3453 | |
| 3454 | if (type->is_matrix() || type->is_record()) { |
| 3455 | _mesa_glsl_error(loc, state, "component layout qualifier " |
| 3456 | "cannot be applied to a matrix, a structure, " |
| 3457 | "a block, or an array containing any of " |
| 3458 | "these."); |
| 3459 | } else if (qual_component != 0 && |
| 3460 | (qual_component + components - 1) > 3) { |
| 3461 | _mesa_glsl_error(loc, state, "component overflow (%u > 3)", |
| 3462 | (qual_component + components - 1)); |
Dave Airlie | 35616a9 | 2016-06-09 07:02:07 +1000 | [diff] [blame] | 3463 | } else if (qual_component == 1 && type->is_64bit()) { |
Timothy Arceri | 9443857 | 2015-11-11 06:24:53 +1100 | [diff] [blame] | 3464 | /* We don't bother checking for 3 as it should be caught by the |
| 3465 | * overflow check above. |
| 3466 | */ |
| 3467 | _mesa_glsl_error(loc, state, "doubles cannot begin at " |
| 3468 | "component 1 or 3"); |
| 3469 | } else { |
| 3470 | var->data.explicit_component = true; |
| 3471 | var->data.location_frac = qual_component; |
| 3472 | } |
| 3473 | } |
| 3474 | } |
Timothy Arceri | 6498037 | 2015-11-12 17:43:52 +1100 | [diff] [blame] | 3475 | } else if (qual->flags.q.explicit_index) { |
Timothy Arceri | f7af69c | 2015-11-09 09:34:40 +1100 | [diff] [blame] | 3476 | if (!qual->flags.q.subroutine_def) |
| 3477 | _mesa_glsl_error(loc, state, |
| 3478 | "explicit index requires explicit location"); |
Timothy Arceri | 9443857 | 2015-11-11 06:24:53 +1100 | [diff] [blame] | 3479 | } else if (qual->flags.q.explicit_component) { |
| 3480 | _mesa_glsl_error(loc, state, |
| 3481 | "explicit component requires explicit location"); |
Timothy Arceri | 6498037 | 2015-11-12 17:43:52 +1100 | [diff] [blame] | 3482 | } |
| 3483 | |
Timothy Arceri | 64710db | 2015-11-15 00:55:29 +1100 | [diff] [blame] | 3484 | if (qual->flags.q.explicit_binding) { |
| 3485 | apply_explicit_binding(state, loc, var, var->type, qual); |
Timothy Arceri | 6498037 | 2015-11-12 17:43:52 +1100 | [diff] [blame] | 3486 | } |
| 3487 | |
| 3488 | if (state->stage == MESA_SHADER_GEOMETRY && |
| 3489 | qual->flags.q.out && qual->flags.q.stream) { |
Timothy Arceri | 17e224e | 2015-11-13 18:47:55 +1100 | [diff] [blame] | 3490 | unsigned qual_stream; |
| 3491 | if (process_qualifier_constant(state, loc, "stream", qual->stream, |
Timothy Arceri | db3c36a | 2015-11-14 14:32:38 +1100 | [diff] [blame] | 3492 | &qual_stream) && |
| 3493 | validate_stream_qualifier(loc, state, qual_stream)) { |
Timothy Arceri | 17e224e | 2015-11-13 18:47:55 +1100 | [diff] [blame] | 3494 | var->data.stream = qual_stream; |
| 3495 | } |
Timothy Arceri | 6498037 | 2015-11-12 17:43:52 +1100 | [diff] [blame] | 3496 | } |
| 3497 | |
Timothy Arceri | f6a8c7e | 2016-03-11 23:00:16 +1100 | [diff] [blame] | 3498 | if (qual->flags.q.out && qual->flags.q.xfb_buffer) { |
| 3499 | unsigned qual_xfb_buffer; |
| 3500 | if (process_qualifier_constant(state, loc, "xfb_buffer", |
| 3501 | qual->xfb_buffer, &qual_xfb_buffer) && |
| 3502 | validate_xfb_buffer_qualifier(loc, state, qual_xfb_buffer)) { |
| 3503 | var->data.xfb_buffer = qual_xfb_buffer; |
| 3504 | if (qual->flags.q.explicit_xfb_buffer) |
| 3505 | var->data.explicit_xfb_buffer = true; |
| 3506 | } |
| 3507 | } |
| 3508 | |
Timothy Arceri | edddad0 | 2016-02-24 15:21:59 +1100 | [diff] [blame] | 3509 | if (qual->flags.q.explicit_xfb_offset) { |
| 3510 | unsigned qual_xfb_offset; |
| 3511 | unsigned component_size = var->type->contains_double() ? 8 : 4; |
Timothy Arceri | 98d40b4 | 2016-06-01 09:21:01 +1000 | [diff] [blame] | 3512 | |
Timothy Arceri | edddad0 | 2016-02-24 15:21:59 +1100 | [diff] [blame] | 3513 | if (process_qualifier_constant(state, loc, "xfb_offset", |
| 3514 | qual->offset, &qual_xfb_offset) && |
| 3515 | validate_xfb_offset_qualifier(loc, state, (int) qual_xfb_offset, |
Timothy Arceri | 98d40b4 | 2016-06-01 09:21:01 +1000 | [diff] [blame] | 3516 | var->type, component_size)) { |
Timothy Arceri | edddad0 | 2016-02-24 15:21:59 +1100 | [diff] [blame] | 3517 | var->data.offset = qual_xfb_offset; |
| 3518 | var->data.explicit_xfb_offset = true; |
| 3519 | } |
| 3520 | } |
| 3521 | |
Timothy Arceri | 04a72e6 | 2016-02-13 14:53:45 +1100 | [diff] [blame] | 3522 | if (qual->flags.q.explicit_xfb_stride) { |
| 3523 | unsigned qual_xfb_stride; |
| 3524 | if (process_qualifier_constant(state, loc, "xfb_stride", |
| 3525 | qual->xfb_stride, &qual_xfb_stride)) { |
| 3526 | var->data.xfb_stride = qual_xfb_stride; |
| 3527 | var->data.explicit_xfb_stride = true; |
| 3528 | } |
| 3529 | } |
| 3530 | |
Timothy Arceri | 6498037 | 2015-11-12 17:43:52 +1100 | [diff] [blame] | 3531 | if (var->type->contains_atomic()) { |
| 3532 | if (var->data.mode == ir_var_uniform) { |
| 3533 | if (var->data.explicit_binding) { |
| 3534 | unsigned *offset = |
| 3535 | &state->atomic_counter_offsets[var->data.binding]; |
| 3536 | |
| 3537 | if (*offset % ATOMIC_COUNTER_SIZE) |
| 3538 | _mesa_glsl_error(loc, state, |
| 3539 | "misaligned atomic counter offset"); |
| 3540 | |
Timothy Arceri | 0d4cd04 | 2015-12-29 21:02:56 +1100 | [diff] [blame] | 3541 | var->data.offset = *offset; |
Timothy Arceri | 6498037 | 2015-11-12 17:43:52 +1100 | [diff] [blame] | 3542 | *offset += var->type->atomic_size(); |
| 3543 | |
| 3544 | } else { |
| 3545 | _mesa_glsl_error(loc, state, |
| 3546 | "atomic counters require explicit binding point"); |
| 3547 | } |
| 3548 | } else if (var->data.mode != ir_var_function_in) { |
| 3549 | _mesa_glsl_error(loc, state, "atomic counters may only be declared as " |
| 3550 | "function parameters or uniform-qualified " |
| 3551 | "global variables"); |
| 3552 | } |
| 3553 | } |
| 3554 | |
| 3555 | /* Is the 'layout' keyword used with parameters that allow relaxed checking. |
| 3556 | * Many implementations of GL_ARB_fragment_coord_conventions_enable and some |
| 3557 | * implementations (only Mesa?) GL_ARB_explicit_attrib_location_enable |
| 3558 | * allowed the layout qualifier to be used with 'varying' and 'attribute'. |
| 3559 | * These extensions and all following extensions that add the 'layout' |
| 3560 | * keyword have been modified to require the use of 'in' or 'out'. |
| 3561 | * |
| 3562 | * The following extension do not allow the deprecated keywords: |
| 3563 | * |
| 3564 | * GL_AMD_conservative_depth |
| 3565 | * GL_ARB_conservative_depth |
| 3566 | * GL_ARB_gpu_shader5 |
| 3567 | * GL_ARB_separate_shader_objects |
| 3568 | * GL_ARB_tessellation_shader |
| 3569 | * GL_ARB_transform_feedback3 |
| 3570 | * GL_ARB_uniform_buffer_object |
| 3571 | * |
| 3572 | * It is unknown whether GL_EXT_shader_image_load_store or GL_NV_gpu_shader5 |
| 3573 | * allow layout with the deprecated keywords. |
| 3574 | */ |
| 3575 | const bool relaxed_layout_qualifier_checking = |
| 3576 | state->ARB_fragment_coord_conventions_enable; |
| 3577 | |
| 3578 | const bool uses_deprecated_qualifier = qual->flags.q.attribute |
| 3579 | || qual->flags.q.varying; |
| 3580 | if (qual->has_layout() && uses_deprecated_qualifier) { |
| 3581 | if (relaxed_layout_qualifier_checking) { |
| 3582 | _mesa_glsl_warning(loc, state, |
| 3583 | "`layout' qualifier may not be used with " |
| 3584 | "`attribute' or `varying'"); |
| 3585 | } else { |
| 3586 | _mesa_glsl_error(loc, state, |
| 3587 | "`layout' qualifier may not be used with " |
| 3588 | "`attribute' or `varying'"); |
| 3589 | } |
| 3590 | } |
| 3591 | |
| 3592 | /* Layout qualifiers for gl_FragDepth, which are enabled by extension |
| 3593 | * AMD_conservative_depth. |
| 3594 | */ |
| 3595 | int depth_layout_count = qual->flags.q.depth_any |
| 3596 | + qual->flags.q.depth_greater |
| 3597 | + qual->flags.q.depth_less |
| 3598 | + qual->flags.q.depth_unchanged; |
| 3599 | if (depth_layout_count > 0 |
Ilia Mirkin | 87906cb | 2016-04-02 21:08:25 -0400 | [diff] [blame] | 3600 | && !state->is_version(420, 0) |
Timothy Arceri | 6498037 | 2015-11-12 17:43:52 +1100 | [diff] [blame] | 3601 | && !state->AMD_conservative_depth_enable |
| 3602 | && !state->ARB_conservative_depth_enable) { |
| 3603 | _mesa_glsl_error(loc, state, |
| 3604 | "extension GL_AMD_conservative_depth or " |
| 3605 | "GL_ARB_conservative_depth must be enabled " |
| 3606 | "to use depth layout qualifiers"); |
| 3607 | } else if (depth_layout_count > 0 |
| 3608 | && strcmp(var->name, "gl_FragDepth") != 0) { |
| 3609 | _mesa_glsl_error(loc, state, |
| 3610 | "depth layout qualifiers can be applied only to " |
| 3611 | "gl_FragDepth"); |
| 3612 | } else if (depth_layout_count > 1 |
| 3613 | && strcmp(var->name, "gl_FragDepth") == 0) { |
| 3614 | _mesa_glsl_error(loc, state, |
| 3615 | "at most one depth layout qualifier can be applied to " |
| 3616 | "gl_FragDepth"); |
| 3617 | } |
| 3618 | if (qual->flags.q.depth_any) |
| 3619 | var->data.depth_layout = ir_depth_layout_any; |
| 3620 | else if (qual->flags.q.depth_greater) |
| 3621 | var->data.depth_layout = ir_depth_layout_greater; |
| 3622 | else if (qual->flags.q.depth_less) |
| 3623 | var->data.depth_layout = ir_depth_layout_less; |
| 3624 | else if (qual->flags.q.depth_unchanged) |
| 3625 | var->data.depth_layout = ir_depth_layout_unchanged; |
| 3626 | else |
| 3627 | var->data.depth_layout = ir_depth_layout_none; |
| 3628 | |
| 3629 | if (qual->flags.q.std140 || |
| 3630 | qual->flags.q.std430 || |
| 3631 | qual->flags.q.packed || |
| 3632 | qual->flags.q.shared) { |
| 3633 | _mesa_glsl_error(loc, state, |
| 3634 | "uniform and shader storage block layout qualifiers " |
| 3635 | "std140, std430, packed, and shared can only be " |
| 3636 | "applied to uniform or shader storage blocks, not " |
| 3637 | "members"); |
| 3638 | } |
| 3639 | |
| 3640 | if (qual->flags.q.row_major || qual->flags.q.column_major) { |
| 3641 | validate_matrix_layout_for_type(state, loc, var->type, var); |
| 3642 | } |
| 3643 | |
| 3644 | /* From section 4.4.1.3 of the GLSL 4.50 specification (Fragment Shader |
| 3645 | * Inputs): |
| 3646 | * |
| 3647 | * "Fragment shaders also allow the following layout qualifier on in only |
| 3648 | * (not with variable declarations) |
| 3649 | * layout-qualifier-id |
| 3650 | * early_fragment_tests |
| 3651 | * [...]" |
| 3652 | */ |
| 3653 | if (qual->flags.q.early_fragment_tests) { |
| 3654 | _mesa_glsl_error(loc, state, "early_fragment_tests layout qualifier only " |
| 3655 | "valid in fragment shader input layout declaration."); |
| 3656 | } |
Plamena Manolova | 8481386 | 2016-12-06 21:32:36 +0200 | [diff] [blame] | 3657 | |
Lionel Landwerlin | 039d836 | 2016-11-30 14:47:41 +0000 | [diff] [blame] | 3658 | if (qual->flags.q.inner_coverage) { |
| 3659 | _mesa_glsl_error(loc, state, "inner_coverage layout qualifier only " |
| 3660 | "valid in fragment shader input layout declaration."); |
| 3661 | } |
| 3662 | |
Plamena Manolova | 8481386 | 2016-12-06 21:32:36 +0200 | [diff] [blame] | 3663 | if (qual->flags.q.post_depth_coverage) { |
| 3664 | _mesa_glsl_error(loc, state, "post_depth_coverage layout qualifier only " |
| 3665 | "valid in fragment shader input layout declaration."); |
| 3666 | } |
Timothy Arceri | 6498037 | 2015-11-12 17:43:52 +1100 | [diff] [blame] | 3667 | } |
| 3668 | |
| 3669 | static void |
Ian Romanick | a87ac25 | 2010-02-22 13:19:34 -0800 | [diff] [blame] | 3670 | apply_type_qualifier_to_variable(const struct ast_type_qualifier *qual, |
Iago Toral Quiroga | 4472ab9 | 2014-04-14 09:14:23 +0200 | [diff] [blame] | 3671 | ir_variable *var, |
| 3672 | struct _mesa_glsl_parse_state *state, |
| 3673 | YYLTYPE *loc, |
Paul Berry | c33be48 | 2012-12-18 14:49:34 -0800 | [diff] [blame] | 3674 | bool is_parameter) |
Ian Romanick | a87ac25 | 2010-02-22 13:19:34 -0800 | [diff] [blame] | 3675 | { |
Eric Anholt | f2e1423 | 2013-06-12 13:46:57 -0700 | [diff] [blame] | 3676 | STATIC_ASSERT(sizeof(qual->flags.q) <= sizeof(qual->flags.i)); |
| 3677 | |
Ian Romanick | bd33055 | 2011-01-07 18:34:58 -0800 | [diff] [blame] | 3678 | if (qual->flags.q.invariant) { |
Tapani Pälli | 33ee2c6 | 2013-12-12 13:51:01 +0200 | [diff] [blame] | 3679 | if (var->data.used) { |
Iago Toral Quiroga | 4472ab9 | 2014-04-14 09:14:23 +0200 | [diff] [blame] | 3680 | _mesa_glsl_error(loc, state, |
| 3681 | "variable `%s' may not be redeclared " |
| 3682 | "`invariant' after being used", |
| 3683 | var->name); |
Ian Romanick | bd33055 | 2011-01-07 18:34:58 -0800 | [diff] [blame] | 3684 | } else { |
Iago Toral Quiroga | 4472ab9 | 2014-04-14 09:14:23 +0200 | [diff] [blame] | 3685 | var->data.invariant = 1; |
Ian Romanick | bd33055 | 2011-01-07 18:34:58 -0800 | [diff] [blame] | 3686 | } |
| 3687 | } |
Ian Romanick | a87ac25 | 2010-02-22 13:19:34 -0800 | [diff] [blame] | 3688 | |
Chris Forbes | 4b756b2 | 2014-04-27 16:03:53 +1200 | [diff] [blame] | 3689 | if (qual->flags.q.precise) { |
| 3690 | if (var->data.used) { |
| 3691 | _mesa_glsl_error(loc, state, |
| 3692 | "variable `%s' may not be redeclared " |
| 3693 | "`precise' after being used", |
| 3694 | var->name); |
| 3695 | } else { |
| 3696 | var->data.precise = 1; |
| 3697 | } |
| 3698 | } |
| 3699 | |
Dave Airlie | 65ac360 | 2015-06-01 10:55:47 +1000 | [diff] [blame] | 3700 | if (qual->flags.q.subroutine && !qual->flags.q.uniform) { |
| 3701 | _mesa_glsl_error(loc, state, |
| 3702 | "`subroutine' may only be applied to uniforms, " |
| 3703 | "subroutine type declarations, or function definitions"); |
| 3704 | } |
| 3705 | |
Ian Romanick | e24d35a | 2010-10-05 16:38:47 -0700 | [diff] [blame] | 3706 | if (qual->flags.q.constant || qual->flags.q.attribute |
| 3707 | || qual->flags.q.uniform |
Paul Berry | 665b8d7 | 2014-01-07 10:11:39 -0800 | [diff] [blame] | 3708 | || (qual->flags.q.varying && (state->stage == MESA_SHADER_FRAGMENT))) |
Tapani Pälli | c1d3080 | 2013-12-12 12:57:57 +0200 | [diff] [blame] | 3709 | var->data.read_only = 1; |
Ian Romanick | a87ac25 | 2010-02-22 13:19:34 -0800 | [diff] [blame] | 3710 | |
Ian Romanick | e24d35a | 2010-10-05 16:38:47 -0700 | [diff] [blame] | 3711 | if (qual->flags.q.centroid) |
Tapani Pälli | c1d3080 | 2013-12-12 12:57:57 +0200 | [diff] [blame] | 3712 | var->data.centroid = 1; |
Ian Romanick | a87ac25 | 2010-02-22 13:19:34 -0800 | [diff] [blame] | 3713 | |
Chris Forbes | 51c5fc8 | 2013-11-29 21:26:10 +1300 | [diff] [blame] | 3714 | if (qual->flags.q.sample) |
Tapani Pälli | c1d3080 | 2013-12-12 12:57:57 +0200 | [diff] [blame] | 3715 | var->data.sample = 1; |
Chris Forbes | 51c5fc8 | 2013-11-29 21:26:10 +1300 | [diff] [blame] | 3716 | |
Iago Toral Quiroga | f84bc57 | 2015-11-10 08:22:07 +0200 | [diff] [blame] | 3717 | /* Precision qualifiers do not hold any meaning in Desktop GLSL */ |
| 3718 | if (state->es_shader) { |
| 3719 | var->data.precision = |
| 3720 | select_gles_precision(qual->precision, var->type, state, loc); |
| 3721 | } |
| 3722 | |
Fabian Bieler | 1009b33 | 2014-03-05 13:43:17 +0100 | [diff] [blame] | 3723 | if (qual->flags.q.patch) |
| 3724 | var->data.patch = 1; |
| 3725 | |
Paul Berry | 665b8d7 | 2014-01-07 10:11:39 -0800 | [diff] [blame] | 3726 | if (qual->flags.q.attribute && state->stage != MESA_SHADER_VERTEX) { |
Eric Anholt | 2e063f1 | 2010-03-28 00:56:22 -0700 | [diff] [blame] | 3727 | var->type = glsl_type::error_type; |
| 3728 | _mesa_glsl_error(loc, state, |
Iago Toral Quiroga | 4472ab9 | 2014-04-14 09:14:23 +0200 | [diff] [blame] | 3729 | "`attribute' variables may not be declared in the " |
| 3730 | "%s shader", |
| 3731 | _mesa_shader_stage_to_string(state->stage)); |
Eric Anholt | 2e063f1 | 2010-03-28 00:56:22 -0700 | [diff] [blame] | 3732 | } |
| 3733 | |
Chris Forbes | 91b8ecb | 2014-06-12 21:17:13 +1200 | [diff] [blame] | 3734 | /* Disallow layout qualifiers which may only appear on layout declarations. */ |
| 3735 | if (qual->flags.q.prim_type) { |
| 3736 | _mesa_glsl_error(loc, state, |
| 3737 | "Primitive type may only be specified on GS input or output " |
| 3738 | "layout declaration, not on variables."); |
| 3739 | } |
| 3740 | |
Ian Romanick | 5894898 | 2013-08-08 16:42:37 -0700 | [diff] [blame] | 3741 | /* Section 6.1.1 (Function Calling Conventions) of the GLSL 1.10 spec says: |
| 3742 | * |
| 3743 | * "However, the const qualifier cannot be used with out or inout." |
| 3744 | * |
| 3745 | * The same section of the GLSL 4.40 spec further clarifies this saying: |
| 3746 | * |
| 3747 | * "The const qualifier cannot be used with out or inout, or a |
| 3748 | * compile-time error results." |
| 3749 | */ |
| 3750 | if (is_parameter && qual->flags.q.constant && qual->flags.q.out) { |
| 3751 | _mesa_glsl_error(loc, state, |
| 3752 | "`const' may not be applied to `out' or `inout' " |
| 3753 | "function parameters"); |
| 3754 | } |
| 3755 | |
Ian Romanick | 7e2aa91 | 2010-07-19 17:12:42 -0700 | [diff] [blame] | 3756 | /* If there is no qualifier that changes the mode of the variable, leave |
| 3757 | * the setting alone. |
| 3758 | */ |
Ian Romanick | a994824 | 2014-07-08 18:53:09 -0700 | [diff] [blame] | 3759 | assert(var->data.mode != ir_var_temporary); |
Ian Romanick | e24d35a | 2010-10-05 16:38:47 -0700 | [diff] [blame] | 3760 | if (qual->flags.q.in && qual->flags.q.out) |
Francisco Jerez | 19e929a | 2016-07-19 20:10:21 -0700 | [diff] [blame] | 3761 | var->data.mode = is_parameter ? ir_var_function_inout : ir_var_shader_out; |
Paul Berry | 42a29d8 | 2013-01-11 14:39:32 -0800 | [diff] [blame] | 3762 | else if (qual->flags.q.in) |
Tapani Pälli | 33ee2c6 | 2013-12-12 13:51:01 +0200 | [diff] [blame] | 3763 | var->data.mode = is_parameter ? ir_var_function_in : ir_var_shader_in; |
Paul Berry | 42a29d8 | 2013-01-11 14:39:32 -0800 | [diff] [blame] | 3764 | else if (qual->flags.q.attribute |
Timothy Arceri | 222f66a | 2016-09-28 16:04:08 +1000 | [diff] [blame] | 3765 | || (qual->flags.q.varying && (state->stage == MESA_SHADER_FRAGMENT))) |
Tapani Pälli | 33ee2c6 | 2013-12-12 13:51:01 +0200 | [diff] [blame] | 3766 | var->data.mode = ir_var_shader_in; |
Paul Berry | 42a29d8 | 2013-01-11 14:39:32 -0800 | [diff] [blame] | 3767 | else if (qual->flags.q.out) |
Tapani Pälli | 33ee2c6 | 2013-12-12 13:51:01 +0200 | [diff] [blame] | 3768 | var->data.mode = is_parameter ? ir_var_function_out : ir_var_shader_out; |
Paul Berry | 665b8d7 | 2014-01-07 10:11:39 -0800 | [diff] [blame] | 3769 | else if (qual->flags.q.varying && (state->stage == MESA_SHADER_VERTEX)) |
Tapani Pälli | 33ee2c6 | 2013-12-12 13:51:01 +0200 | [diff] [blame] | 3770 | var->data.mode = ir_var_shader_out; |
Ian Romanick | e24d35a | 2010-10-05 16:38:47 -0700 | [diff] [blame] | 3771 | else if (qual->flags.q.uniform) |
Tapani Pälli | 33ee2c6 | 2013-12-12 13:51:01 +0200 | [diff] [blame] | 3772 | var->data.mode = ir_var_uniform; |
Kristian Høgsberg | 84fc5fe | 2015-05-13 10:53:46 +0200 | [diff] [blame] | 3773 | else if (qual->flags.q.buffer) |
| 3774 | var->data.mode = ir_var_shader_storage; |
Jordan Justen | fb3da12 | 2015-07-28 15:00:47 -0700 | [diff] [blame] | 3775 | else if (qual->flags.q.shared_storage) |
| 3776 | var->data.mode = ir_var_shader_shared; |
Ian Romanick | a87ac25 | 2010-02-22 13:19:34 -0800 | [diff] [blame] | 3777 | |
Francisco Jerez | 19e929a | 2016-07-19 20:10:21 -0700 | [diff] [blame] | 3778 | var->data.fb_fetch_output = state->stage == MESA_SHADER_FRAGMENT && |
| 3779 | qual->flags.q.in && qual->flags.q.out; |
| 3780 | |
Paul Berry | 665b8d7 | 2014-01-07 10:11:39 -0800 | [diff] [blame] | 3781 | if (!is_parameter && is_varying_var(var, state->stage)) { |
Paul Berry | 5a79bda | 2014-01-08 01:54:26 -0800 | [diff] [blame] | 3782 | /* User-defined ins/outs are not permitted in compute shaders. */ |
| 3783 | if (state->stage == MESA_SHADER_COMPUTE) { |
| 3784 | _mesa_glsl_error(loc, state, |
| 3785 | "user-defined input and output variables are not " |
| 3786 | "permitted in compute shaders"); |
| 3787 | } |
| 3788 | |
Paul Berry | 1872055 | 2012-12-18 15:24:39 -0800 | [diff] [blame] | 3789 | /* This variable is being used to link data between shader stages (in |
| 3790 | * pre-glsl-1.30 parlance, it's a "varying"). Check that it has a type |
| 3791 | * that is allowed for such purposes. |
| 3792 | * |
| 3793 | * From page 25 (page 31 of the PDF) of the GLSL 1.10 spec: |
| 3794 | * |
| 3795 | * "The varying qualifier can be used only with the data types |
| 3796 | * float, vec2, vec3, vec4, mat2, mat3, and mat4, or arrays of |
| 3797 | * these." |
| 3798 | * |
| 3799 | * This was relaxed in GLSL version 1.30 and GLSL ES version 3.00. From |
| 3800 | * page 31 (page 37 of the PDF) of the GLSL 1.30 spec: |
| 3801 | * |
| 3802 | * "Fragment inputs can only be signed and unsigned integers and |
| 3803 | * integer vectors, float, floating-point vectors, matrices, or |
| 3804 | * arrays of these. Structures cannot be input. |
| 3805 | * |
| 3806 | * Similar text exists in the section on vertex shader outputs. |
| 3807 | * |
| 3808 | * Similar text exists in the GLSL ES 3.00 spec, except that the GLSL ES |
Paul Berry | c6a50dd | 2013-01-04 10:43:01 -0800 | [diff] [blame] | 3809 | * 3.00 spec allows structs as well. Varying structs are also allowed |
| 3810 | * in GLSL 1.50. |
Paul Berry | 1872055 | 2012-12-18 15:24:39 -0800 | [diff] [blame] | 3811 | */ |
| 3812 | switch (var->type->get_scalar_type()->base_type) { |
| 3813 | case GLSL_TYPE_FLOAT: |
| 3814 | /* Ok in all GLSL versions */ |
| 3815 | break; |
| 3816 | case GLSL_TYPE_UINT: |
| 3817 | case GLSL_TYPE_INT: |
| 3818 | if (state->is_version(130, 300)) |
| 3819 | break; |
| 3820 | _mesa_glsl_error(loc, state, |
| 3821 | "varying variables must be of base type float in %s", |
| 3822 | state->get_version_string()); |
| 3823 | break; |
| 3824 | case GLSL_TYPE_STRUCT: |
Paul Berry | c6a50dd | 2013-01-04 10:43:01 -0800 | [diff] [blame] | 3825 | if (state->is_version(150, 300)) |
| 3826 | break; |
Paul Berry | 1872055 | 2012-12-18 15:24:39 -0800 | [diff] [blame] | 3827 | _mesa_glsl_error(loc, state, |
| 3828 | "varying variables may not be of type struct"); |
| 3829 | break; |
Dave Airlie | ba3bab2 | 2015-02-05 12:04:58 +0200 | [diff] [blame] | 3830 | case GLSL_TYPE_DOUBLE: |
| 3831 | break; |
Paul Berry | 1872055 | 2012-12-18 15:24:39 -0800 | [diff] [blame] | 3832 | default: |
| 3833 | _mesa_glsl_error(loc, state, "illegal type for a varying variable"); |
| 3834 | break; |
| 3835 | } |
| 3836 | } |
| 3837 | |
Ian Romanick | 86b4398 | 2011-01-06 10:49:56 -0800 | [diff] [blame] | 3838 | if (state->all_invariant && (state->current_function == NULL)) { |
Paul Berry | 665b8d7 | 2014-01-07 10:11:39 -0800 | [diff] [blame] | 3839 | switch (state->stage) { |
Paul Berry | 7963fde | 2013-12-16 13:09:20 -0800 | [diff] [blame] | 3840 | case MESA_SHADER_VERTEX: |
Iago Toral Quiroga | 4472ab9 | 2014-04-14 09:14:23 +0200 | [diff] [blame] | 3841 | if (var->data.mode == ir_var_shader_out) |
| 3842 | var->data.invariant = true; |
Fabian Bieler | 497eb29 | 2014-03-20 22:44:43 +0100 | [diff] [blame] | 3843 | break; |
| 3844 | case MESA_SHADER_TESS_CTRL: |
| 3845 | case MESA_SHADER_TESS_EVAL: |
Paul Berry | 7963fde | 2013-12-16 13:09:20 -0800 | [diff] [blame] | 3846 | case MESA_SHADER_GEOMETRY: |
Iago Toral Quiroga | 4472ab9 | 2014-04-14 09:14:23 +0200 | [diff] [blame] | 3847 | if ((var->data.mode == ir_var_shader_in) |
Tapani Pälli | 33ee2c6 | 2013-12-12 13:51:01 +0200 | [diff] [blame] | 3848 | || (var->data.mode == ir_var_shader_out)) |
Iago Toral Quiroga | 4472ab9 | 2014-04-14 09:14:23 +0200 | [diff] [blame] | 3849 | var->data.invariant = true; |
| 3850 | break; |
Paul Berry | 7963fde | 2013-12-16 13:09:20 -0800 | [diff] [blame] | 3851 | case MESA_SHADER_FRAGMENT: |
Iago Toral Quiroga | 4472ab9 | 2014-04-14 09:14:23 +0200 | [diff] [blame] | 3852 | if (var->data.mode == ir_var_shader_in) |
| 3853 | var->data.invariant = true; |
| 3854 | break; |
Paul Berry | c61ec8d | 2014-01-06 20:06:05 -0800 | [diff] [blame] | 3855 | case MESA_SHADER_COMPUTE: |
| 3856 | /* Invariance isn't meaningful in compute shaders. */ |
| 3857 | break; |
Ian Romanick | 86b4398 | 2011-01-06 10:49:56 -0800 | [diff] [blame] | 3858 | } |
| 3859 | } |
| 3860 | |
Tapani Pälli | 33ee2c6 | 2013-12-12 13:51:01 +0200 | [diff] [blame] | 3861 | var->data.interpolation = |
Andres Gomez | c750029 | 2016-03-24 01:13:26 +0200 | [diff] [blame] | 3862 | interpret_interpolation_qualifier(qual, var->type, |
| 3863 | (ir_variable_mode) var->data.mode, |
Paul Berry | 81a5067 | 2013-10-22 14:54:10 -0700 | [diff] [blame] | 3864 | state, loc); |
Eric Anholt | 916e206 | 2012-01-09 16:40:20 -0800 | [diff] [blame] | 3865 | |
Ian Romanick | 4bcff0c | 2011-01-07 16:53:59 -0800 | [diff] [blame] | 3866 | /* Does the declaration use the deprecated 'attribute' or 'varying' |
| 3867 | * keywords? |
| 3868 | */ |
| 3869 | const bool uses_deprecated_qualifier = qual->flags.q.attribute |
| 3870 | || qual->flags.q.varying; |
| 3871 | |
Chris Forbes | 5bbb028 | 2014-04-12 13:21:09 +1200 | [diff] [blame] | 3872 | |
| 3873 | /* Validate auxiliary storage qualifiers */ |
| 3874 | |
| 3875 | /* From section 4.3.4 of the GLSL 1.30 spec: |
| 3876 | * "It is an error to use centroid in in a vertex shader." |
| 3877 | * |
| 3878 | * From section 4.3.4 of the GLSL ES 3.00 spec: |
| 3879 | * "It is an error to use centroid in or interpolation qualifiers in |
| 3880 | * a vertex shader input." |
| 3881 | */ |
| 3882 | |
| 3883 | /* Section 4.3.6 of the GLSL 1.30 specification states: |
| 3884 | * "It is an error to use centroid out in a fragment shader." |
| 3885 | * |
| 3886 | * The GL_ARB_shading_language_420pack extension specification states: |
| 3887 | * "It is an error to use auxiliary storage qualifiers or interpolation |
| 3888 | * qualifiers on an output in a fragment shader." |
| 3889 | */ |
| 3890 | if (qual->flags.q.sample && (!is_varying_var(var, state->stage) || uses_deprecated_qualifier)) { |
| 3891 | _mesa_glsl_error(loc, state, |
| 3892 | "sample qualifier may only be used on `in` or `out` " |
| 3893 | "variables between shader stages"); |
| 3894 | } |
| 3895 | if (qual->flags.q.centroid && !is_varying_var(var, state->stage)) { |
| 3896 | _mesa_glsl_error(loc, state, |
| 3897 | "centroid qualifier may only be used with `in', " |
| 3898 | "`out' or `varying' variables between shader stages"); |
| 3899 | } |
| 3900 | |
Jordan Justen | 8b28b35 | 2015-03-15 13:53:06 -0700 | [diff] [blame] | 3901 | if (qual->flags.q.shared_storage && state->stage != MESA_SHADER_COMPUTE) { |
| 3902 | _mesa_glsl_error(loc, state, |
| 3903 | "the shared storage qualifiers can only be used with " |
| 3904 | "compute shaders"); |
| 3905 | } |
| 3906 | |
Francisco Jerez | b5994d2 | 2015-01-31 22:07:47 +0200 | [diff] [blame] | 3907 | apply_image_qualifier_to_variable(qual, var, state, loc); |
Ian Romanick | a87ac25 | 2010-02-22 13:19:34 -0800 | [diff] [blame] | 3908 | } |
| 3909 | |
Ian Romanick | 8e6cb9f | 2011-03-04 15:28:40 -0800 | [diff] [blame] | 3910 | /** |
| 3911 | * Get the variable that is being redeclared by this declaration |
| 3912 | * |
| 3913 | * Semantic checks to verify the validity of the redeclaration are also |
| 3914 | * performed. If semantic checks fail, compilation error will be emitted via |
| 3915 | * \c _mesa_glsl_error, but a non-\c NULL pointer will still be returned. |
| 3916 | * |
| 3917 | * \returns |
| 3918 | * A pointer to an existing variable in the current scope if the declaration |
| 3919 | * is a redeclaration, \c NULL otherwise. |
| 3920 | */ |
Paul Berry | 3699ff4 | 2013-09-27 20:38:29 -0700 | [diff] [blame] | 3921 | static ir_variable * |
| 3922 | get_variable_being_redeclared(ir_variable *var, YYLTYPE loc, |
Paul Berry | 1b4a737 | 2013-09-27 20:53:33 -0700 | [diff] [blame] | 3923 | struct _mesa_glsl_parse_state *state, |
| 3924 | bool allow_all_redeclarations) |
Ian Romanick | 8e6cb9f | 2011-03-04 15:28:40 -0800 | [diff] [blame] | 3925 | { |
| 3926 | /* Check if this declaration is actually a re-declaration, either to |
| 3927 | * resize an array or add qualifiers to an existing variable. |
| 3928 | * |
| 3929 | * This is allowed for variables in the current scope, or when at |
| 3930 | * global scope (for built-ins in the implicit outer scope). |
| 3931 | */ |
Paul Berry | 3699ff4 | 2013-09-27 20:38:29 -0700 | [diff] [blame] | 3932 | ir_variable *earlier = state->symbols->get_variable(var->name); |
Ian Romanick | 8e6cb9f | 2011-03-04 15:28:40 -0800 | [diff] [blame] | 3933 | if (earlier == NULL || |
| 3934 | (state->current_function != NULL && |
Iago Toral Quiroga | 4472ab9 | 2014-04-14 09:14:23 +0200 | [diff] [blame] | 3935 | !state->symbols->name_declared_this_scope(var->name))) { |
Ian Romanick | 8e6cb9f | 2011-03-04 15:28:40 -0800 | [diff] [blame] | 3936 | return NULL; |
| 3937 | } |
| 3938 | |
| 3939 | |
Ian Romanick | 8e6cb9f | 2011-03-04 15:28:40 -0800 | [diff] [blame] | 3940 | /* From page 24 (page 30 of the PDF) of the GLSL 1.50 spec, |
| 3941 | * |
| 3942 | * "It is legal to declare an array without a size and then |
| 3943 | * later re-declare the same name as an array of the same |
| 3944 | * type and specify a size." |
| 3945 | */ |
Timothy Arceri | b59c592 | 2013-10-23 21:31:27 +1100 | [diff] [blame] | 3946 | if (earlier->type->is_unsized_array() && var->type->is_array() |
Timothy Arceri | d67515b | 2015-04-30 20:45:54 +1000 | [diff] [blame] | 3947 | && (var->type->fields.array == earlier->type->fields.array)) { |
Ian Romanick | 8e6cb9f | 2011-03-04 15:28:40 -0800 | [diff] [blame] | 3948 | /* FINISHME: This doesn't match the qualifiers on the two |
| 3949 | * FINISHME: declarations. It's not 100% clear whether this is |
| 3950 | * FINISHME: required or not. |
| 3951 | */ |
| 3952 | |
Dave Airlie | 8c628ab | 2016-05-20 10:19:14 +1000 | [diff] [blame] | 3953 | const int size = var->type->array_size(); |
Paul Berry | 93b9758 | 2011-09-06 10:01:51 -0700 | [diff] [blame] | 3954 | check_builtin_array_max_size(var->name, size, loc, state); |
Tapani Pälli | 447bb90 | 2013-12-12 15:08:59 +0200 | [diff] [blame] | 3955 | if ((size > 0) && (size <= earlier->data.max_array_access)) { |
Iago Toral Quiroga | 4472ab9 | 2014-04-14 09:14:23 +0200 | [diff] [blame] | 3956 | _mesa_glsl_error(& loc, state, "array size must be > %u due to " |
| 3957 | "previous access", |
| 3958 | earlier->data.max_array_access); |
Ian Romanick | 8e6cb9f | 2011-03-04 15:28:40 -0800 | [diff] [blame] | 3959 | } |
| 3960 | |
| 3961 | earlier->type = var->type; |
| 3962 | delete var; |
| 3963 | var = NULL; |
Paul Berry | 417dc80 | 2013-08-06 12:17:17 -0700 | [diff] [blame] | 3964 | } else if ((state->ARB_fragment_coord_conventions_enable || |
Iago Toral Quiroga | 4472ab9 | 2014-04-14 09:14:23 +0200 | [diff] [blame] | 3965 | state->is_version(150, 0)) |
| 3966 | && strcmp(var->name, "gl_FragCoord") == 0 |
| 3967 | && earlier->type == var->type |
Marek Olšák | 4191c1a | 2016-01-02 20:16:16 +0100 | [diff] [blame] | 3968 | && var->data.mode == ir_var_shader_in) { |
Ian Romanick | 8e6cb9f | 2011-03-04 15:28:40 -0800 | [diff] [blame] | 3969 | /* Allow redeclaration of gl_FragCoord for ARB_fcc layout |
| 3970 | * qualifiers. |
| 3971 | */ |
Tapani Pälli | 33ee2c6 | 2013-12-12 13:51:01 +0200 | [diff] [blame] | 3972 | earlier->data.origin_upper_left = var->data.origin_upper_left; |
| 3973 | earlier->data.pixel_center_integer = var->data.pixel_center_integer; |
Ian Romanick | 8e6cb9f | 2011-03-04 15:28:40 -0800 | [diff] [blame] | 3974 | |
| 3975 | /* According to section 4.3.7 of the GLSL 1.30 spec, |
| 3976 | * the following built-in varaibles can be redeclared with an |
| 3977 | * interpolation qualifier: |
| 3978 | * * gl_FrontColor |
| 3979 | * * gl_BackColor |
| 3980 | * * gl_FrontSecondaryColor |
| 3981 | * * gl_BackSecondaryColor |
| 3982 | * * gl_Color |
| 3983 | * * gl_SecondaryColor |
| 3984 | */ |
Paul Berry | e3ded7f | 2012-08-01 14:50:05 -0700 | [diff] [blame] | 3985 | } else if (state->is_version(130, 0) |
Iago Toral Quiroga | 4472ab9 | 2014-04-14 09:14:23 +0200 | [diff] [blame] | 3986 | && (strcmp(var->name, "gl_FrontColor") == 0 |
| 3987 | || strcmp(var->name, "gl_BackColor") == 0 |
| 3988 | || strcmp(var->name, "gl_FrontSecondaryColor") == 0 |
| 3989 | || strcmp(var->name, "gl_BackSecondaryColor") == 0 |
| 3990 | || strcmp(var->name, "gl_Color") == 0 |
| 3991 | || strcmp(var->name, "gl_SecondaryColor") == 0) |
| 3992 | && earlier->type == var->type |
| 3993 | && earlier->data.mode == var->data.mode) { |
Tapani Pälli | 33ee2c6 | 2013-12-12 13:51:01 +0200 | [diff] [blame] | 3994 | earlier->data.interpolation = var->data.interpolation; |
Ian Romanick | 8e6cb9f | 2011-03-04 15:28:40 -0800 | [diff] [blame] | 3995 | |
| 3996 | /* Layout qualifiers for gl_FragDepth. */ |
Ilia Mirkin | 87906cb | 2016-04-02 21:08:25 -0400 | [diff] [blame] | 3997 | } else if ((state->is_version(420, 0) || |
| 3998 | state->AMD_conservative_depth_enable || |
Marek Olšák | 6b43d6f | 2011-11-19 16:41:08 +0100 | [diff] [blame] | 3999 | state->ARB_conservative_depth_enable) |
Iago Toral Quiroga | 4472ab9 | 2014-04-14 09:14:23 +0200 | [diff] [blame] | 4000 | && strcmp(var->name, "gl_FragDepth") == 0 |
| 4001 | && earlier->type == var->type |
| 4002 | && earlier->data.mode == var->data.mode) { |
Ian Romanick | 8e6cb9f | 2011-03-04 15:28:40 -0800 | [diff] [blame] | 4003 | |
| 4004 | /** From the AMD_conservative_depth spec: |
| 4005 | * Within any shader, the first redeclarations of gl_FragDepth |
| 4006 | * must appear before any use of gl_FragDepth. |
| 4007 | */ |
Tapani Pälli | 33ee2c6 | 2013-12-12 13:51:01 +0200 | [diff] [blame] | 4008 | if (earlier->data.used) { |
Iago Toral Quiroga | 4472ab9 | 2014-04-14 09:14:23 +0200 | [diff] [blame] | 4009 | _mesa_glsl_error(&loc, state, |
| 4010 | "the first redeclaration of gl_FragDepth " |
| 4011 | "must appear before any use of gl_FragDepth"); |
Ian Romanick | 8e6cb9f | 2011-03-04 15:28:40 -0800 | [diff] [blame] | 4012 | } |
| 4013 | |
| 4014 | /* Prevent inconsistent redeclaration of depth layout qualifier. */ |
Tapani Pälli | 447bb90 | 2013-12-12 15:08:59 +0200 | [diff] [blame] | 4015 | if (earlier->data.depth_layout != ir_depth_layout_none |
Iago Toral Quiroga | 4472ab9 | 2014-04-14 09:14:23 +0200 | [diff] [blame] | 4016 | && earlier->data.depth_layout != var->data.depth_layout) { |
| 4017 | _mesa_glsl_error(&loc, state, |
| 4018 | "gl_FragDepth: depth layout is declared here " |
| 4019 | "as '%s, but it was previously declared as " |
| 4020 | "'%s'", |
| 4021 | depth_layout_string(var->data.depth_layout), |
| 4022 | depth_layout_string(earlier->data.depth_layout)); |
Ian Romanick | 8e6cb9f | 2011-03-04 15:28:40 -0800 | [diff] [blame] | 4023 | } |
| 4024 | |
Tapani Pälli | 447bb90 | 2013-12-12 15:08:59 +0200 | [diff] [blame] | 4025 | earlier->data.depth_layout = var->data.depth_layout; |
Ian Romanick | 8e6cb9f | 2011-03-04 15:28:40 -0800 | [diff] [blame] | 4026 | |
Francisco Jerez | 6b3d23d | 2016-07-14 12:52:51 -0700 | [diff] [blame] | 4027 | } else if (state->has_framebuffer_fetch() && |
| 4028 | strcmp(var->name, "gl_LastFragData") == 0 && |
| 4029 | var->type == earlier->type && |
| 4030 | var->data.mode == ir_var_auto) { |
| 4031 | /* According to the EXT_shader_framebuffer_fetch spec: |
| 4032 | * |
| 4033 | * "By default, gl_LastFragData is declared with the mediump precision |
| 4034 | * qualifier. This can be changed by redeclaring the corresponding |
| 4035 | * variables with the desired precision qualifier." |
| 4036 | */ |
| 4037 | earlier->data.precision = var->data.precision; |
| 4038 | |
Paul Berry | 1b4a737 | 2013-09-27 20:53:33 -0700 | [diff] [blame] | 4039 | } else if (allow_all_redeclarations) { |
Tapani Pälli | 33ee2c6 | 2013-12-12 13:51:01 +0200 | [diff] [blame] | 4040 | if (earlier->data.mode != var->data.mode) { |
Paul Berry | 1b4a737 | 2013-09-27 20:53:33 -0700 | [diff] [blame] | 4041 | _mesa_glsl_error(&loc, state, |
| 4042 | "redeclaration of `%s' with incorrect qualifiers", |
| 4043 | var->name); |
| 4044 | } else if (earlier->type != var->type) { |
| 4045 | _mesa_glsl_error(&loc, state, |
| 4046 | "redeclaration of `%s' has incorrect type", |
| 4047 | var->name); |
| 4048 | } |
Ian Romanick | 8e6cb9f | 2011-03-04 15:28:40 -0800 | [diff] [blame] | 4049 | } else { |
Paul Berry | 3699ff4 | 2013-09-27 20:38:29 -0700 | [diff] [blame] | 4050 | _mesa_glsl_error(&loc, state, "`%s' redeclared", var->name); |
Ian Romanick | 8e6cb9f | 2011-03-04 15:28:40 -0800 | [diff] [blame] | 4051 | } |
| 4052 | |
| 4053 | return earlier; |
| 4054 | } |
Ian Romanick | a87ac25 | 2010-02-22 13:19:34 -0800 | [diff] [blame] | 4055 | |
Ian Romanick | 0292ffb | 2011-03-04 15:29:33 -0800 | [diff] [blame] | 4056 | /** |
| 4057 | * Generate the IR for an initializer in a variable declaration |
| 4058 | */ |
| 4059 | ir_rvalue * |
| 4060 | process_initializer(ir_variable *var, ast_declaration *decl, |
Timothy Arceri | 222f66a | 2016-09-28 16:04:08 +1000 | [diff] [blame] | 4061 | ast_fully_specified_type *type, |
| 4062 | exec_list *initializer_instructions, |
| 4063 | struct _mesa_glsl_parse_state *state) |
Ian Romanick | 0292ffb | 2011-03-04 15:29:33 -0800 | [diff] [blame] | 4064 | { |
| 4065 | ir_rvalue *result = NULL; |
| 4066 | |
| 4067 | YYLTYPE initializer_loc = decl->initializer->get_location(); |
| 4068 | |
| 4069 | /* From page 24 (page 30 of the PDF) of the GLSL 1.10 spec: |
| 4070 | * |
| 4071 | * "All uniform variables are read-only and are initialized either |
| 4072 | * directly by an application via API commands, or indirectly by |
| 4073 | * OpenGL." |
| 4074 | */ |
Tapani Pälli | 33ee2c6 | 2013-12-12 13:51:01 +0200 | [diff] [blame] | 4075 | if (var->data.mode == ir_var_uniform) { |
Paul Berry | 0d9bba6 | 2012-08-05 09:57:01 -0700 | [diff] [blame] | 4076 | state->check_version(120, 0, &initializer_loc, |
Iago Toral Quiroga | 7a1143f | 2015-10-07 09:28:43 +0200 | [diff] [blame] | 4077 | "cannot initialize uniform %s", |
| 4078 | var->name); |
Ian Romanick | 0292ffb | 2011-03-04 15:29:33 -0800 | [diff] [blame] | 4079 | } |
| 4080 | |
Samuel Iglesias Gonsalvez | 20b2907 | 2015-03-18 10:25:10 +0100 | [diff] [blame] | 4081 | /* Section 4.3.7 "Buffer Variables" of the GLSL 4.30 spec: |
| 4082 | * |
| 4083 | * "Buffer variables cannot have initializers." |
| 4084 | */ |
| 4085 | if (var->data.mode == ir_var_shader_storage) { |
Iago Toral Quiroga | 7a1143f | 2015-10-07 09:28:43 +0200 | [diff] [blame] | 4086 | _mesa_glsl_error(&initializer_loc, state, |
| 4087 | "cannot initialize buffer variable %s", |
| 4088 | var->name); |
Samuel Iglesias Gonsalvez | 20b2907 | 2015-03-18 10:25:10 +0100 | [diff] [blame] | 4089 | } |
| 4090 | |
Francisco Jerez | 7af167d | 2013-11-25 19:38:37 -0800 | [diff] [blame] | 4091 | /* From section 4.1.7 of the GLSL 4.40 spec: |
| 4092 | * |
| 4093 | * "Opaque variables [...] are initialized only through the |
| 4094 | * OpenGL API; they cannot be declared with an initializer in a |
| 4095 | * shader." |
| 4096 | */ |
| 4097 | if (var->type->contains_opaque()) { |
Iago Toral Quiroga | 7a1143f | 2015-10-07 09:28:43 +0200 | [diff] [blame] | 4098 | _mesa_glsl_error(&initializer_loc, state, |
| 4099 | "cannot initialize opaque variable %s", |
| 4100 | var->name); |
Ian Romanick | 0292ffb | 2011-03-04 15:29:33 -0800 | [diff] [blame] | 4101 | } |
| 4102 | |
Tapani Pälli | 33ee2c6 | 2013-12-12 13:51:01 +0200 | [diff] [blame] | 4103 | if ((var->data.mode == ir_var_shader_in) && (state->current_function == NULL)) { |
Iago Toral Quiroga | 7a1143f | 2015-10-07 09:28:43 +0200 | [diff] [blame] | 4104 | _mesa_glsl_error(&initializer_loc, state, |
| 4105 | "cannot initialize %s shader input / %s %s", |
| 4106 | _mesa_shader_stage_to_string(state->stage), |
| 4107 | (state->stage == MESA_SHADER_VERTEX) |
| 4108 | ? "attribute" : "varying", |
| 4109 | var->name); |
Ian Romanick | 0292ffb | 2011-03-04 15:29:33 -0800 | [diff] [blame] | 4110 | } |
| 4111 | |
Iago Toral Quiroga | f09c229 | 2015-10-07 09:21:36 +0200 | [diff] [blame] | 4112 | if (var->data.mode == ir_var_shader_out && state->current_function == NULL) { |
| 4113 | _mesa_glsl_error(&initializer_loc, state, |
Iago Toral Quiroga | 7a1143f | 2015-10-07 09:28:43 +0200 | [diff] [blame] | 4114 | "cannot initialize %s shader output %s", |
| 4115 | _mesa_shader_stage_to_string(state->stage), |
| 4116 | var->name); |
Iago Toral Quiroga | f09c229 | 2015-10-07 09:21:36 +0200 | [diff] [blame] | 4117 | } |
| 4118 | |
Paul Berry | 0da1a2c | 2014-01-21 15:41:26 -0800 | [diff] [blame] | 4119 | /* If the initializer is an ast_aggregate_initializer, recursively store |
| 4120 | * type information from the LHS into it, so that its hir() function can do |
| 4121 | * type checking. |
| 4122 | */ |
| 4123 | if (decl->initializer->oper == ast_aggregate) |
| 4124 | _mesa_ast_set_aggregate_type(var->type, decl->initializer); |
| 4125 | |
Ian Romanick | 0292ffb | 2011-03-04 15:29:33 -0800 | [diff] [blame] | 4126 | ir_dereference *const lhs = new(state) ir_dereference_variable(var); |
Iago Toral Quiroga | 4472ab9 | 2014-04-14 09:14:23 +0200 | [diff] [blame] | 4127 | ir_rvalue *rhs = decl->initializer->hir(initializer_instructions, state); |
Ian Romanick | 0292ffb | 2011-03-04 15:29:33 -0800 | [diff] [blame] | 4128 | |
| 4129 | /* Calculate the constant value if this is a const or uniform |
| 4130 | * declaration. |
Ian Romanick | bb329f2 | 2015-10-06 17:05:55 -0700 | [diff] [blame] | 4131 | * |
| 4132 | * Section 4.3 (Storage Qualifiers) of the GLSL ES 1.00.17 spec says: |
| 4133 | * |
| 4134 | * "Declarations of globals without a storage qualifier, or with |
| 4135 | * just the const qualifier, may include initializers, in which case |
| 4136 | * they will be initialized before the first line of main() is |
| 4137 | * executed. Such initializers must be a constant expression." |
| 4138 | * |
| 4139 | * The same section of the GLSL ES 3.00.4 spec has similar language. |
Ian Romanick | 0292ffb | 2011-03-04 15:29:33 -0800 | [diff] [blame] | 4140 | */ |
| 4141 | if (type->qualifier.flags.q.constant |
Ian Romanick | bb329f2 | 2015-10-06 17:05:55 -0700 | [diff] [blame] | 4142 | || type->qualifier.flags.q.uniform |
| 4143 | || (state->es_shader && state->current_function == NULL)) { |
Timothy Arceri | d1d3b1e | 2013-10-17 22:42:18 +1100 | [diff] [blame] | 4144 | ir_rvalue *new_rhs = validate_assignment(state, initializer_loc, |
Chris Forbes | d563946 | 2014-08-31 19:35:46 +1200 | [diff] [blame] | 4145 | lhs, rhs, true); |
Ian Romanick | 0292ffb | 2011-03-04 15:29:33 -0800 | [diff] [blame] | 4146 | if (new_rhs != NULL) { |
Iago Toral Quiroga | 4472ab9 | 2014-04-14 09:14:23 +0200 | [diff] [blame] | 4147 | rhs = new_rhs; |
Ian Romanick | 0292ffb | 2011-03-04 15:29:33 -0800 | [diff] [blame] | 4148 | |
Ian Romanick | 92635a8 | 2015-10-07 14:26:29 -0700 | [diff] [blame] | 4149 | /* Section 4.3.3 (Constant Expressions) of the GLSL ES 3.00.4 spec |
| 4150 | * says: |
| 4151 | * |
| 4152 | * "A constant expression is one of |
| 4153 | * |
| 4154 | * ... |
| 4155 | * |
| 4156 | * - an expression formed by an operator on operands that are |
| 4157 | * all constant expressions, including getting an element of |
| 4158 | * a constant array, or a field of a constant structure, or |
| 4159 | * components of a constant vector. However, the sequence |
| 4160 | * operator ( , ) and the assignment operators ( =, +=, ...) |
| 4161 | * are not included in the operators that can create a |
| 4162 | * constant expression." |
| 4163 | * |
| 4164 | * Section 12.43 (Sequence operator and constant expressions) says: |
| 4165 | * |
| 4166 | * "Should the following construct be allowed? |
| 4167 | * |
| 4168 | * float a[2,3]; |
| 4169 | * |
| 4170 | * The expression within the brackets uses the sequence operator |
| 4171 | * (',') and returns the integer 3 so the construct is declaring |
| 4172 | * a single-dimensional array of size 3. In some languages, the |
| 4173 | * construct declares a two-dimensional array. It would be |
| 4174 | * preferable to make this construct illegal to avoid confusion. |
| 4175 | * |
| 4176 | * One possibility is to change the definition of the sequence |
| 4177 | * operator so that it does not return a constant-expression and |
| 4178 | * hence cannot be used to declare an array size. |
| 4179 | * |
| 4180 | * RESOLUTION: The result of a sequence operator is not a |
| 4181 | * constant-expression." |
| 4182 | * |
| 4183 | * Section 4.3.3 (Constant Expressions) of the GLSL 4.30.9 spec |
| 4184 | * contains language almost identical to the section 4.3.3 in the |
| 4185 | * GLSL ES 3.00.4 spec. This is a new limitation for these GLSL |
| 4186 | * versions. |
| 4187 | */ |
Iago Toral Quiroga | 4472ab9 | 2014-04-14 09:14:23 +0200 | [diff] [blame] | 4188 | ir_constant *constant_value = rhs->constant_expression_value(); |
Ian Romanick | 92635a8 | 2015-10-07 14:26:29 -0700 | [diff] [blame] | 4189 | if (!constant_value || |
| 4190 | (state->is_version(430, 300) && |
| 4191 | decl->initializer->has_sequence_subexpression())) { |
Ian Romanick | bb329f2 | 2015-10-06 17:05:55 -0700 | [diff] [blame] | 4192 | const char *const variable_mode = |
| 4193 | (type->qualifier.flags.q.constant) |
| 4194 | ? "const" |
| 4195 | : ((type->qualifier.flags.q.uniform) ? "uniform" : "global"); |
| 4196 | |
Matt Turner | ba7b60d | 2013-05-23 11:48:40 -0700 | [diff] [blame] | 4197 | /* If ARB_shading_language_420pack is enabled, initializers of |
| 4198 | * const-qualified local variables do not have to be constant |
| 4199 | * expressions. Const-qualified global variables must still be |
| 4200 | * initialized with constant expressions. |
| 4201 | */ |
Matt Turner | 79da722 | 2015-12-07 14:11:01 -0800 | [diff] [blame] | 4202 | if (!state->has_420pack() |
Matt Turner | ba7b60d | 2013-05-23 11:48:40 -0700 | [diff] [blame] | 4203 | || state->current_function == NULL) { |
| 4204 | _mesa_glsl_error(& initializer_loc, state, |
| 4205 | "initializer of %s variable `%s' must be a " |
| 4206 | "constant expression", |
Ian Romanick | bb329f2 | 2015-10-06 17:05:55 -0700 | [diff] [blame] | 4207 | variable_mode, |
Matt Turner | ba7b60d | 2013-05-23 11:48:40 -0700 | [diff] [blame] | 4208 | decl->identifier); |
| 4209 | if (var->type->is_numeric()) { |
| 4210 | /* Reduce cascading errors. */ |
Ian Romanick | 3524d6d | 2015-10-07 12:52:58 -0700 | [diff] [blame] | 4211 | var->constant_value = type->qualifier.flags.q.constant |
| 4212 | ? ir_constant::zero(state, var->type) : NULL; |
Matt Turner | ba7b60d | 2013-05-23 11:48:40 -0700 | [diff] [blame] | 4213 | } |
| 4214 | } |
| 4215 | } else { |
Iago Toral Quiroga | 4472ab9 | 2014-04-14 09:14:23 +0200 | [diff] [blame] | 4216 | rhs = constant_value; |
Ian Romanick | 3524d6d | 2015-10-07 12:52:58 -0700 | [diff] [blame] | 4217 | var->constant_value = type->qualifier.flags.q.constant |
| 4218 | ? constant_value : NULL; |
Iago Toral Quiroga | 4472ab9 | 2014-04-14 09:14:23 +0200 | [diff] [blame] | 4219 | } |
Ian Romanick | 0292ffb | 2011-03-04 15:29:33 -0800 | [diff] [blame] | 4220 | } else { |
Iago Toral Quiroga | 4472ab9 | 2014-04-14 09:14:23 +0200 | [diff] [blame] | 4221 | if (var->type->is_numeric()) { |
| 4222 | /* Reduce cascading errors. */ |
Ian Romanick | 3524d6d | 2015-10-07 12:52:58 -0700 | [diff] [blame] | 4223 | var->constant_value = type->qualifier.flags.q.constant |
| 4224 | ? ir_constant::zero(state, var->type) : NULL; |
Iago Toral Quiroga | 4472ab9 | 2014-04-14 09:14:23 +0200 | [diff] [blame] | 4225 | } |
Ian Romanick | 0292ffb | 2011-03-04 15:29:33 -0800 | [diff] [blame] | 4226 | } |
| 4227 | } |
| 4228 | |
| 4229 | if (rhs && !rhs->type->is_error()) { |
Tapani Pälli | c1d3080 | 2013-12-12 12:57:57 +0200 | [diff] [blame] | 4230 | bool temp = var->data.read_only; |
Ian Romanick | 0292ffb | 2011-03-04 15:29:33 -0800 | [diff] [blame] | 4231 | if (type->qualifier.flags.q.constant) |
Iago Toral Quiroga | 4472ab9 | 2014-04-14 09:14:23 +0200 | [diff] [blame] | 4232 | var->data.read_only = false; |
Ian Romanick | 0292ffb | 2011-03-04 15:29:33 -0800 | [diff] [blame] | 4233 | |
| 4234 | /* Never emit code to initialize a uniform. |
| 4235 | */ |
| 4236 | const glsl_type *initializer_type; |
| 4237 | if (!type->qualifier.flags.q.uniform) { |
Iago Toral Quiroga | 4472ab9 | 2014-04-14 09:14:23 +0200 | [diff] [blame] | 4238 | do_assignment(initializer_instructions, state, |
Eric Anholt | e9822f7 | 2014-03-05 17:05:54 -0800 | [diff] [blame] | 4239 | NULL, |
| 4240 | lhs, rhs, |
| 4241 | &result, true, |
| 4242 | true, |
| 4243 | type->get_location()); |
Iago Toral Quiroga | 4472ab9 | 2014-04-14 09:14:23 +0200 | [diff] [blame] | 4244 | initializer_type = result->type; |
Ian Romanick | 0292ffb | 2011-03-04 15:29:33 -0800 | [diff] [blame] | 4245 | } else |
Iago Toral Quiroga | 4472ab9 | 2014-04-14 09:14:23 +0200 | [diff] [blame] | 4246 | initializer_type = rhs->type; |
Ian Romanick | 0292ffb | 2011-03-04 15:29:33 -0800 | [diff] [blame] | 4247 | |
Ian Romanick | f37b1ad | 2011-10-31 14:31:07 -0700 | [diff] [blame] | 4248 | var->constant_initializer = rhs->constant_expression_value(); |
Tapani Pälli | 447bb90 | 2013-12-12 15:08:59 +0200 | [diff] [blame] | 4249 | var->data.has_initializer = true; |
Ian Romanick | f37b1ad | 2011-10-31 14:31:07 -0700 | [diff] [blame] | 4250 | |
Ian Romanick | 0292ffb | 2011-03-04 15:29:33 -0800 | [diff] [blame] | 4251 | /* If the declared variable is an unsized array, it must inherrit |
| 4252 | * its full type from the initializer. A declaration such as |
| 4253 | * |
| 4254 | * uniform float a[] = float[](1.0, 2.0, 3.0, 3.0); |
| 4255 | * |
| 4256 | * becomes |
| 4257 | * |
| 4258 | * uniform float a[4] = float[](1.0, 2.0, 3.0, 3.0); |
| 4259 | * |
| 4260 | * The assignment generated in the if-statement (below) will also |
| 4261 | * automatically handle this case for non-uniforms. |
| 4262 | * |
| 4263 | * If the declared variable is not an array, the types must |
| 4264 | * already match exactly. As a result, the type assignment |
| 4265 | * here can be done unconditionally. For non-uniforms the call |
| 4266 | * to do_assignment can change the type of the initializer (via |
| 4267 | * the implicit conversion rules). For uniforms the initializer |
| 4268 | * must be a constant expression, and the type of that expression |
| 4269 | * was validated above. |
| 4270 | */ |
| 4271 | var->type = initializer_type; |
| 4272 | |
Tapani Pälli | c1d3080 | 2013-12-12 12:57:57 +0200 | [diff] [blame] | 4273 | var->data.read_only = temp; |
Ian Romanick | 0292ffb | 2011-03-04 15:29:33 -0800 | [diff] [blame] | 4274 | } |
| 4275 | |
| 4276 | return result; |
| 4277 | } |
| 4278 | |
Paul Berry | 7cfefe6 | 2013-07-30 21:13:48 -0700 | [diff] [blame] | 4279 | static void |
Fabian Bieler | 497eb29 | 2014-03-20 22:44:43 +0100 | [diff] [blame] | 4280 | validate_layout_qualifier_vertex_count(struct _mesa_glsl_parse_state *state, |
| 4281 | YYLTYPE loc, ir_variable *var, |
| 4282 | unsigned num_vertices, |
| 4283 | unsigned *size, |
| 4284 | const char *var_category) |
Paul Berry | 7cfefe6 | 2013-07-30 21:13:48 -0700 | [diff] [blame] | 4285 | { |
Timothy Arceri | b59c592 | 2013-10-23 21:31:27 +1100 | [diff] [blame] | 4286 | if (var->type->is_unsized_array()) { |
Paul Berry | 7cfefe6 | 2013-07-30 21:13:48 -0700 | [diff] [blame] | 4287 | /* Section 4.3.8.1 (Input Layout Qualifiers) of the GLSL 1.50 spec says: |
| 4288 | * |
| 4289 | * All geometry shader input unsized array declarations will be |
| 4290 | * sized by an earlier input layout qualifier, when present, as per |
| 4291 | * the following table. |
| 4292 | * |
| 4293 | * Followed by a table mapping each allowed input layout qualifier to |
| 4294 | * the corresponding input length. |
Fabian Bieler | 497eb29 | 2014-03-20 22:44:43 +0100 | [diff] [blame] | 4295 | * |
| 4296 | * Similarly for tessellation control shader outputs. |
Paul Berry | 7cfefe6 | 2013-07-30 21:13:48 -0700 | [diff] [blame] | 4297 | */ |
| 4298 | if (num_vertices != 0) |
| 4299 | var->type = glsl_type::get_array_instance(var->type->fields.array, |
| 4300 | num_vertices); |
| 4301 | } else { |
| 4302 | /* Section 4.3.8.1 (Input Layout Qualifiers) of the GLSL 1.50 spec |
| 4303 | * includes the following examples of compile-time errors: |
| 4304 | * |
| 4305 | * // code sequence within one shader... |
| 4306 | * in vec4 Color1[]; // size unknown |
| 4307 | * ...Color1.length()...// illegal, length() unknown |
| 4308 | * in vec4 Color2[2]; // size is 2 |
| 4309 | * ...Color1.length()...// illegal, Color1 still has no size |
| 4310 | * in vec4 Color3[3]; // illegal, input sizes are inconsistent |
| 4311 | * layout(lines) in; // legal, input size is 2, matching |
| 4312 | * in vec4 Color4[3]; // illegal, contradicts layout |
| 4313 | * ... |
| 4314 | * |
| 4315 | * To detect the case illustrated by Color3, we verify that the size of |
| 4316 | * an explicitly-sized array matches the size of any previously declared |
| 4317 | * explicitly-sized array. To detect the case illustrated by Color4, we |
| 4318 | * verify that the size of an explicitly-sized array is consistent with |
| 4319 | * any previously declared input layout. |
| 4320 | */ |
| 4321 | if (num_vertices != 0 && var->type->length != num_vertices) { |
| 4322 | _mesa_glsl_error(&loc, state, |
Fabian Bieler | 497eb29 | 2014-03-20 22:44:43 +0100 | [diff] [blame] | 4323 | "%s size contradicts previously declared layout " |
| 4324 | "(size is %u, but layout requires a size of %u)", |
| 4325 | var_category, var->type->length, num_vertices); |
| 4326 | } else if (*size != 0 && var->type->length != *size) { |
Paul Berry | 7cfefe6 | 2013-07-30 21:13:48 -0700 | [diff] [blame] | 4327 | _mesa_glsl_error(&loc, state, |
Fabian Bieler | 497eb29 | 2014-03-20 22:44:43 +0100 | [diff] [blame] | 4328 | "%s sizes are inconsistent (size is %u, but a " |
| 4329 | "previous declaration has size %u)", |
| 4330 | var_category, var->type->length, *size); |
Paul Berry | 7cfefe6 | 2013-07-30 21:13:48 -0700 | [diff] [blame] | 4331 | } else { |
Fabian Bieler | 497eb29 | 2014-03-20 22:44:43 +0100 | [diff] [blame] | 4332 | *size = var->type->length; |
Paul Berry | 7cfefe6 | 2013-07-30 21:13:48 -0700 | [diff] [blame] | 4333 | } |
| 4334 | } |
| 4335 | } |
| 4336 | |
Fabian Bieler | 497eb29 | 2014-03-20 22:44:43 +0100 | [diff] [blame] | 4337 | static void |
| 4338 | handle_tess_ctrl_shader_output_decl(struct _mesa_glsl_parse_state *state, |
| 4339 | YYLTYPE loc, ir_variable *var) |
| 4340 | { |
| 4341 | unsigned num_vertices = 0; |
| 4342 | |
| 4343 | if (state->tcs_output_vertices_specified) { |
Timothy Arceri | 02d2ab2 | 2015-11-14 15:13:28 +1100 | [diff] [blame] | 4344 | if (!state->out_qualifier->vertices-> |
| 4345 | process_qualifier_constant(state, "vertices", |
| 4346 | &num_vertices, false)) { |
| 4347 | return; |
| 4348 | } |
| 4349 | |
| 4350 | if (num_vertices > state->Const.MaxPatchVertices) { |
| 4351 | _mesa_glsl_error(&loc, state, "vertices (%d) exceeds " |
| 4352 | "GL_MAX_PATCH_VERTICES", num_vertices); |
| 4353 | return; |
| 4354 | } |
Fabian Bieler | 497eb29 | 2014-03-20 22:44:43 +0100 | [diff] [blame] | 4355 | } |
| 4356 | |
Fabian Bieler | 1009b33 | 2014-03-05 13:43:17 +0100 | [diff] [blame] | 4357 | if (!var->type->is_array() && !var->data.patch) { |
| 4358 | _mesa_glsl_error(&loc, state, |
| 4359 | "tessellation control shader outputs must be arrays"); |
| 4360 | |
| 4361 | /* To avoid cascading failures, short circuit the checks below. */ |
| 4362 | return; |
| 4363 | } |
| 4364 | |
| 4365 | if (var->data.patch) |
| 4366 | return; |
| 4367 | |
Kenneth Graunke | 1735584 | 2016-10-25 03:38:54 -0700 | [diff] [blame] | 4368 | var->data.tess_varying_implicit_sized_array = var->type->is_unsized_array(); |
| 4369 | |
Fabian Bieler | 497eb29 | 2014-03-20 22:44:43 +0100 | [diff] [blame] | 4370 | validate_layout_qualifier_vertex_count(state, loc, var, num_vertices, |
| 4371 | &state->tcs_output_size, |
Ilia Mirkin | 365d631 | 2015-08-21 15:08:15 -0400 | [diff] [blame] | 4372 | "tessellation control shader output"); |
Fabian Bieler | 497eb29 | 2014-03-20 22:44:43 +0100 | [diff] [blame] | 4373 | } |
| 4374 | |
| 4375 | /** |
Chris Forbes | 61846f2 | 2014-09-01 20:48:09 +1200 | [diff] [blame] | 4376 | * Do additional processing necessary for tessellation control/evaluation shader |
| 4377 | * input declarations. This covers both interface block arrays and bare input |
| 4378 | * variables. |
| 4379 | */ |
| 4380 | static void |
| 4381 | handle_tess_shader_input_decl(struct _mesa_glsl_parse_state *state, |
| 4382 | YYLTYPE loc, ir_variable *var) |
| 4383 | { |
| 4384 | if (!var->type->is_array() && !var->data.patch) { |
| 4385 | _mesa_glsl_error(&loc, state, |
| 4386 | "per-vertex tessellation shader inputs must be arrays"); |
| 4387 | /* Avoid cascading failures. */ |
| 4388 | return; |
| 4389 | } |
| 4390 | |
| 4391 | if (var->data.patch) |
| 4392 | return; |
| 4393 | |
Kenneth Graunke | 72b56e8 | 2016-08-31 00:16:24 -0700 | [diff] [blame] | 4394 | /* The ARB_tessellation_shader spec says: |
| 4395 | * |
| 4396 | * "Declaring an array size is optional. If no size is specified, it |
| 4397 | * will be taken from the implementation-dependent maximum patch size |
| 4398 | * (gl_MaxPatchVertices). If a size is specified, it must match the |
| 4399 | * maximum patch size; otherwise, a compile or link error will occur." |
| 4400 | * |
| 4401 | * This text appears twice, once for TCS inputs, and again for TES inputs. |
| 4402 | */ |
Chris Forbes | 61846f2 | 2014-09-01 20:48:09 +1200 | [diff] [blame] | 4403 | if (var->type->is_unsized_array()) { |
| 4404 | var->type = glsl_type::get_array_instance(var->type->fields.array, |
| 4405 | state->Const.MaxPatchVertices); |
Kenneth Graunke | 1735584 | 2016-10-25 03:38:54 -0700 | [diff] [blame] | 4406 | var->data.tess_varying_implicit_sized_array = true; |
Kenneth Graunke | 72b56e8 | 2016-08-31 00:16:24 -0700 | [diff] [blame] | 4407 | } else if (var->type->length != state->Const.MaxPatchVertices) { |
| 4408 | _mesa_glsl_error(&loc, state, |
| 4409 | "per-vertex tessellation shader input arrays must be " |
| 4410 | "sized to gl_MaxPatchVertices (%d).", |
| 4411 | state->Const.MaxPatchVertices); |
Chris Forbes | 61846f2 | 2014-09-01 20:48:09 +1200 | [diff] [blame] | 4412 | } |
| 4413 | } |
| 4414 | |
| 4415 | |
| 4416 | /** |
Fabian Bieler | 497eb29 | 2014-03-20 22:44:43 +0100 | [diff] [blame] | 4417 | * Do additional processing necessary for geometry shader input declarations |
| 4418 | * (this covers both interface blocks arrays and bare input variables). |
| 4419 | */ |
| 4420 | static void |
| 4421 | handle_geometry_shader_input_decl(struct _mesa_glsl_parse_state *state, |
| 4422 | YYLTYPE loc, ir_variable *var) |
| 4423 | { |
| 4424 | unsigned num_vertices = 0; |
| 4425 | |
| 4426 | if (state->gs_input_prim_type_specified) { |
| 4427 | num_vertices = vertices_per_prim(state->in_qualifier->prim_type); |
| 4428 | } |
| 4429 | |
| 4430 | /* Geometry shader input variables must be arrays. Caller should have |
| 4431 | * reported an error for this. |
| 4432 | */ |
| 4433 | if (!var->type->is_array()) { |
| 4434 | assert(state->error); |
| 4435 | |
| 4436 | /* To avoid cascading failures, short circuit the checks below. */ |
| 4437 | return; |
| 4438 | } |
| 4439 | |
| 4440 | validate_layout_qualifier_vertex_count(state, loc, var, num_vertices, |
| 4441 | &state->gs_input_size, |
| 4442 | "geometry shader input"); |
| 4443 | } |
Paul Berry | 1838df9 | 2013-09-27 17:18:14 -0700 | [diff] [blame] | 4444 | |
| 4445 | void |
| 4446 | validate_identifier(const char *identifier, YYLTYPE loc, |
| 4447 | struct _mesa_glsl_parse_state *state) |
| 4448 | { |
| 4449 | /* From page 15 (page 21 of the PDF) of the GLSL 1.10 spec, |
| 4450 | * |
| 4451 | * "Identifiers starting with "gl_" are reserved for use by |
| 4452 | * OpenGL, and may not be declared in a shader as either a |
| 4453 | * variable or a function." |
| 4454 | */ |
Brian Paul | a7aca39 | 2014-05-23 14:57:49 -0600 | [diff] [blame] | 4455 | if (is_gl_identifier(identifier)) { |
Paul Berry | 1838df9 | 2013-09-27 17:18:14 -0700 | [diff] [blame] | 4456 | _mesa_glsl_error(&loc, state, |
| 4457 | "identifier `%s' uses reserved `gl_' prefix", |
| 4458 | identifier); |
| 4459 | } else if (strstr(identifier, "__")) { |
| 4460 | /* From page 14 (page 20 of the PDF) of the GLSL 1.10 |
| 4461 | * spec: |
| 4462 | * |
| 4463 | * "In addition, all identifiers containing two |
| 4464 | * consecutive underscores (__) are reserved as |
| 4465 | * possible future keywords." |
Ian Romanick | 2c85fd5 | 2014-02-18 09:36:08 -0800 | [diff] [blame] | 4466 | * |
| 4467 | * The intention is that names containing __ are reserved for internal |
| 4468 | * use by the implementation, and names prefixed with GL_ are reserved |
| 4469 | * for use by Khronos. Names simply containing __ are dangerous to use, |
| 4470 | * but should be allowed. |
| 4471 | * |
| 4472 | * A future version of the GLSL specification will clarify this. |
Paul Berry | 1838df9 | 2013-09-27 17:18:14 -0700 | [diff] [blame] | 4473 | */ |
Ian Romanick | 2c85fd5 | 2014-02-18 09:36:08 -0800 | [diff] [blame] | 4474 | _mesa_glsl_warning(&loc, state, |
| 4475 | "identifier `%s' uses reserved `__' string", |
| 4476 | identifier); |
Paul Berry | 1838df9 | 2013-09-27 17:18:14 -0700 | [diff] [blame] | 4477 | } |
| 4478 | } |
| 4479 | |
Kenneth Graunke | fb9fb5f | 2010-03-26 00:25:36 -0700 | [diff] [blame] | 4480 | ir_rvalue * |
Ian Romanick | 0044e7e | 2010-03-08 23:44:00 -0800 | [diff] [blame] | 4481 | ast_declarator_list::hir(exec_list *instructions, |
Iago Toral Quiroga | 4472ab9 | 2014-04-14 09:14:23 +0200 | [diff] [blame] | 4482 | struct _mesa_glsl_parse_state *state) |
Ian Romanick | a87ac25 | 2010-02-22 13:19:34 -0800 | [diff] [blame] | 4483 | { |
Kenneth Graunke | 953ff12 | 2010-06-25 13:14:37 -0700 | [diff] [blame] | 4484 | void *ctx = state; |
Ian Romanick | a87ac25 | 2010-02-22 13:19:34 -0800 | [diff] [blame] | 4485 | const struct glsl_type *decl_type; |
| 4486 | const char *type_name = NULL; |
Eric Anholt | 8558459 | 2010-04-14 15:38:52 -0700 | [diff] [blame] | 4487 | ir_rvalue *result = NULL; |
Ian Romanick | c824e35 | 2010-04-23 15:55:19 -0700 | [diff] [blame] | 4488 | YYLTYPE loc = this->get_location(); |
Ian Romanick | a87ac25 | 2010-02-22 13:19:34 -0800 | [diff] [blame] | 4489 | |
Ian Romanick | 6f0823d | 2010-07-01 20:39:08 -0700 | [diff] [blame] | 4490 | /* From page 46 (page 52 of the PDF) of the GLSL 1.50 spec: |
| 4491 | * |
| 4492 | * "To ensure that a particular output variable is invariant, it is |
| 4493 | * necessary to use the invariant qualifier. It can either be used to |
| 4494 | * qualify a previously declared variable as being invariant |
| 4495 | * |
| 4496 | * invariant gl_Position; // make existing gl_Position be invariant" |
| 4497 | * |
| 4498 | * In these cases the parser will set the 'invariant' flag in the declarator |
| 4499 | * list, and the type will be NULL. |
| 4500 | */ |
| 4501 | if (this->invariant) { |
| 4502 | assert(this->type == NULL); |
| 4503 | |
| 4504 | if (state->current_function != NULL) { |
Iago Toral Quiroga | 4472ab9 | 2014-04-14 09:14:23 +0200 | [diff] [blame] | 4505 | _mesa_glsl_error(& loc, state, |
| 4506 | "all uses of `invariant' keyword must be at global " |
| 4507 | "scope"); |
Ian Romanick | 6f0823d | 2010-07-01 20:39:08 -0700 | [diff] [blame] | 4508 | } |
| 4509 | |
| 4510 | foreach_list_typed (ast_declaration, decl, link, &this->declarations) { |
Iago Toral Quiroga | 4472ab9 | 2014-04-14 09:14:23 +0200 | [diff] [blame] | 4511 | assert(decl->array_specifier == NULL); |
| 4512 | assert(decl->initializer == NULL); |
Ian Romanick | 6f0823d | 2010-07-01 20:39:08 -0700 | [diff] [blame] | 4513 | |
Iago Toral Quiroga | 4472ab9 | 2014-04-14 09:14:23 +0200 | [diff] [blame] | 4514 | ir_variable *const earlier = |
| 4515 | state->symbols->get_variable(decl->identifier); |
| 4516 | if (earlier == NULL) { |
| 4517 | _mesa_glsl_error(& loc, state, |
| 4518 | "undeclared variable `%s' cannot be marked " |
| 4519 | "invariant", decl->identifier); |
Dave Airlie | ab8ea1b | 2016-05-23 14:18:03 +1000 | [diff] [blame] | 4520 | } else if (!is_allowed_invariant(earlier, state)) { |
Chris Forbes | 0dfa6e7 | 2014-04-21 15:45:32 +1200 | [diff] [blame] | 4521 | _mesa_glsl_error(&loc, state, |
| 4522 | "`%s' cannot be marked invariant; interfaces between " |
| 4523 | "shader stages only.", decl->identifier); |
Iago Toral Quiroga | 4472ab9 | 2014-04-14 09:14:23 +0200 | [diff] [blame] | 4524 | } else if (earlier->data.used) { |
| 4525 | _mesa_glsl_error(& loc, state, |
| 4526 | "variable `%s' may not be redeclared " |
| 4527 | "`invariant' after being used", |
| 4528 | earlier->name); |
| 4529 | } else { |
| 4530 | earlier->data.invariant = true; |
| 4531 | } |
Ian Romanick | 6f0823d | 2010-07-01 20:39:08 -0700 | [diff] [blame] | 4532 | } |
| 4533 | |
| 4534 | /* Invariant redeclarations do not have r-values. |
| 4535 | */ |
| 4536 | return NULL; |
| 4537 | } |
| 4538 | |
Chris Forbes | 5ecffe5 | 2014-04-27 16:03:54 +1200 | [diff] [blame] | 4539 | if (this->precise) { |
| 4540 | assert(this->type == NULL); |
| 4541 | |
| 4542 | foreach_list_typed (ast_declaration, decl, link, &this->declarations) { |
| 4543 | assert(decl->array_specifier == NULL); |
| 4544 | assert(decl->initializer == NULL); |
| 4545 | |
| 4546 | ir_variable *const earlier = |
| 4547 | state->symbols->get_variable(decl->identifier); |
| 4548 | if (earlier == NULL) { |
| 4549 | _mesa_glsl_error(& loc, state, |
| 4550 | "undeclared variable `%s' cannot be marked " |
| 4551 | "precise", decl->identifier); |
Chris Forbes | d0495c6 | 2014-04-27 16:03:55 +1200 | [diff] [blame] | 4552 | } else if (state->current_function != NULL && |
| 4553 | !state->symbols->name_declared_this_scope(decl->identifier)) { |
| 4554 | /* Note: we have to check if we're in a function, since |
| 4555 | * builtins are treated as having come from another scope. |
| 4556 | */ |
| 4557 | _mesa_glsl_error(& loc, state, |
| 4558 | "variable `%s' from an outer scope may not be " |
| 4559 | "redeclared `precise' in this scope", |
| 4560 | earlier->name); |
Chris Forbes | 5ecffe5 | 2014-04-27 16:03:54 +1200 | [diff] [blame] | 4561 | } else if (earlier->data.used) { |
| 4562 | _mesa_glsl_error(& loc, state, |
| 4563 | "variable `%s' may not be redeclared " |
| 4564 | "`precise' after being used", |
| 4565 | earlier->name); |
| 4566 | } else { |
| 4567 | earlier->data.precise = true; |
| 4568 | } |
| 4569 | } |
| 4570 | |
| 4571 | /* Precise redeclarations do not have r-values either. */ |
| 4572 | return NULL; |
| 4573 | } |
| 4574 | |
Ian Romanick | 6f0823d | 2010-07-01 20:39:08 -0700 | [diff] [blame] | 4575 | assert(this->type != NULL); |
| 4576 | assert(!this->invariant); |
Chris Forbes | 4b756b2 | 2014-04-27 16:03:53 +1200 | [diff] [blame] | 4577 | assert(!this->precise); |
Ian Romanick | 6f0823d | 2010-07-01 20:39:08 -0700 | [diff] [blame] | 4578 | |
Ian Romanick | 3455ce6 | 2010-04-19 15:13:15 -0700 | [diff] [blame] | 4579 | /* The type specifier may contain a structure definition. Process that |
| 4580 | * before any of the variable declarations. |
| 4581 | */ |
| 4582 | (void) this->type->specifier->hir(instructions, state); |
| 4583 | |
Ian Romanick | cabd457 | 2013-08-09 15:17:18 -0700 | [diff] [blame] | 4584 | decl_type = this->type->glsl_type(& type_name, state); |
Francisco Jerez | e63bb29 | 2013-10-20 12:38:07 -0700 | [diff] [blame] | 4585 | |
Samuel Iglesias Gonsalvez | 9f651db | 2015-03-18 10:52:53 +0100 | [diff] [blame] | 4586 | /* Section 4.3.7 "Buffer Variables" of the GLSL 4.30 spec: |
| 4587 | * "Buffer variables may only be declared inside interface blocks |
| 4588 | * (section 4.3.9 “Interface Blocks”), which are then referred to as |
| 4589 | * shader storage blocks. It is a compile-time error to declare buffer |
| 4590 | * variables at global scope (outside a block)." |
| 4591 | */ |
| 4592 | if (type->qualifier.flags.q.buffer && !decl_type->is_interface()) { |
| 4593 | _mesa_glsl_error(&loc, state, |
| 4594 | "buffer variables cannot be declared outside " |
| 4595 | "interface blocks"); |
| 4596 | } |
| 4597 | |
Francisco Jerez | e63bb29 | 2013-10-20 12:38:07 -0700 | [diff] [blame] | 4598 | /* An offset-qualified atomic counter declaration sets the default |
| 4599 | * offset for the next declaration within the same atomic counter |
| 4600 | * buffer. |
| 4601 | */ |
| 4602 | if (decl_type && decl_type->contains_atomic()) { |
| 4603 | if (type->qualifier.flags.q.explicit_binding && |
Timothy Arceri | 02d2ab2 | 2015-11-14 15:13:28 +1100 | [diff] [blame] | 4604 | type->qualifier.flags.q.explicit_offset) { |
| 4605 | unsigned qual_binding; |
| 4606 | unsigned qual_offset; |
| 4607 | if (process_qualifier_constant(state, &loc, "binding", |
| 4608 | type->qualifier.binding, |
| 4609 | &qual_binding) |
| 4610 | && process_qualifier_constant(state, &loc, "offset", |
| 4611 | type->qualifier.offset, |
| 4612 | &qual_offset)) { |
| 4613 | state->atomic_counter_offsets[qual_binding] = qual_offset; |
| 4614 | } |
| 4615 | } |
Kenneth Graunke | e303e88 | 2016-04-10 22:50:05 -0700 | [diff] [blame] | 4616 | |
| 4617 | ast_type_qualifier allowed_atomic_qual_mask; |
| 4618 | allowed_atomic_qual_mask.flags.i = 0; |
| 4619 | allowed_atomic_qual_mask.flags.q.explicit_binding = 1; |
| 4620 | allowed_atomic_qual_mask.flags.q.explicit_offset = 1; |
| 4621 | allowed_atomic_qual_mask.flags.q.uniform = 1; |
| 4622 | |
Timothy Arceri | d3dc1b8 | 2016-07-30 16:33:26 +1000 | [diff] [blame] | 4623 | type->qualifier.validate_flags(&loc, state, allowed_atomic_qual_mask, |
| 4624 | "invalid layout qualifier for", |
| 4625 | "atomic_uint"); |
Francisco Jerez | e63bb29 | 2013-10-20 12:38:07 -0700 | [diff] [blame] | 4626 | } |
| 4627 | |
Ian Romanick | 304ea90 | 2010-05-10 11:17:53 -0700 | [diff] [blame] | 4628 | if (this->declarations.is_empty()) { |
Ian Romanick | f5ba4d0 | 2011-10-25 17:49:07 -0700 | [diff] [blame] | 4629 | /* If there is no structure involved in the program text, there are two |
| 4630 | * possible scenarios: |
| 4631 | * |
| 4632 | * - The program text contained something like 'vec4;'. This is an |
| 4633 | * empty declaration. It is valid but weird. Emit a warning. |
| 4634 | * |
| 4635 | * - The program text contained something like 'S;' and 'S' is not the |
| 4636 | * name of a known structure type. This is both invalid and weird. |
| 4637 | * Emit an error. |
| 4638 | * |
Ian Romanick | 830f4df | 2013-08-09 13:32:40 -0700 | [diff] [blame] | 4639 | * - The program text contained something like 'mediump float;' |
| 4640 | * when the programmer probably meant 'precision mediump |
| 4641 | * float;' Emit a warning with a description of what they |
| 4642 | * probably meant to do. |
| 4643 | * |
Ian Romanick | f5ba4d0 | 2011-10-25 17:49:07 -0700 | [diff] [blame] | 4644 | * Note that if decl_type is NULL and there is a structure involved, |
| 4645 | * there must have been some sort of error with the structure. In this |
| 4646 | * case we assume that an error was already generated on this line of |
| 4647 | * code for the structure. There is no need to generate an additional, |
| 4648 | * confusing error. |
| 4649 | */ |
| 4650 | assert(this->type->specifier->structure == NULL || decl_type != NULL |
Timothy Arceri | 222f66a | 2016-09-28 16:04:08 +1000 | [diff] [blame] | 4651 | || state->error); |
Kenneth Graunke | 6eec502 | 2013-07-15 15:58:29 -0700 | [diff] [blame] | 4652 | |
Ian Romanick | 830f4df | 2013-08-09 13:32:40 -0700 | [diff] [blame] | 4653 | if (decl_type == NULL) { |
| 4654 | _mesa_glsl_error(&loc, state, |
| 4655 | "invalid type `%s' in empty declaration", |
| 4656 | type_name); |
Timothy Arceri | 3fd4280 | 2016-02-05 13:08:19 +1100 | [diff] [blame] | 4657 | } else { |
| 4658 | if (decl_type->base_type == GLSL_TYPE_ARRAY) { |
Timothy Arceri | 2188c77 | 2016-03-08 20:35:41 +1100 | [diff] [blame] | 4659 | /* From Section 13.22 (Array Declarations) of the GLSL ES 3.2 |
| 4660 | * spec: |
| 4661 | * |
| 4662 | * "... any declaration that leaves the size undefined is |
| 4663 | * disallowed as this would add complexity and there are no |
| 4664 | * use-cases." |
| 4665 | */ |
| 4666 | if (state->es_shader && decl_type->is_unsized_array()) { |
| 4667 | _mesa_glsl_error(&loc, state, "array size must be explicitly " |
| 4668 | "or implicitly defined"); |
| 4669 | } |
| 4670 | |
Timothy Arceri | 3fd4280 | 2016-02-05 13:08:19 +1100 | [diff] [blame] | 4671 | /* From Section 4.12 (Empty Declarations) of the GLSL 4.5 spec: |
| 4672 | * |
| 4673 | * "The combinations of types and qualifiers that cause |
| 4674 | * compile-time or link-time errors are the same whether or not |
| 4675 | * the declaration is empty." |
| 4676 | */ |
| 4677 | validate_array_dimensions(decl_type, state, &loc); |
Ian Romanick | 830f4df | 2013-08-09 13:32:40 -0700 | [diff] [blame] | 4678 | } |
Timothy Arceri | 3fd4280 | 2016-02-05 13:08:19 +1100 | [diff] [blame] | 4679 | |
| 4680 | if (decl_type->base_type == GLSL_TYPE_ATOMIC_UINT) { |
| 4681 | /* Empty atomic counter declarations are allowed and useful |
| 4682 | * to set the default offset qualifier. |
| 4683 | */ |
| 4684 | return NULL; |
| 4685 | } else if (this->type->qualifier.precision != ast_precision_none) { |
| 4686 | if (this->type->specifier->structure != NULL) { |
| 4687 | _mesa_glsl_error(&loc, state, |
| 4688 | "precision qualifiers can't be applied " |
| 4689 | "to structures"); |
| 4690 | } else { |
| 4691 | static const char *const precision_names[] = { |
| 4692 | "highp", |
| 4693 | "highp", |
| 4694 | "mediump", |
| 4695 | "lowp" |
| 4696 | }; |
| 4697 | |
| 4698 | _mesa_glsl_warning(&loc, state, |
| 4699 | "empty declaration with precision " |
| 4700 | "qualifier, to set the default precision, " |
| 4701 | "use `precision %s %s;'", |
| 4702 | precision_names[this->type-> |
| 4703 | qualifier.precision], |
| 4704 | type_name); |
| 4705 | } |
| 4706 | } else if (this->type->specifier->structure == NULL) { |
| 4707 | _mesa_glsl_warning(&loc, state, "empty declaration"); |
| 4708 | } |
Kenneth Graunke | 6eec502 | 2013-07-15 15:58:29 -0700 | [diff] [blame] | 4709 | } |
Ian Romanick | c824e35 | 2010-04-23 15:55:19 -0700 | [diff] [blame] | 4710 | } |
Ian Romanick | a87ac25 | 2010-02-22 13:19:34 -0800 | [diff] [blame] | 4711 | |
Ian Romanick | 2b97dc6 | 2010-05-10 17:42:05 -0700 | [diff] [blame] | 4712 | foreach_list_typed (ast_declaration, decl, link, &this->declarations) { |
Ian Romanick | a87ac25 | 2010-02-22 13:19:34 -0800 | [diff] [blame] | 4713 | const struct glsl_type *var_type; |
Ian Romanick | 768b55a | 2010-08-13 16:46:43 -0700 | [diff] [blame] | 4714 | ir_variable *var; |
Dave Airlie | 65ac360 | 2015-06-01 10:55:47 +1000 | [diff] [blame] | 4715 | const char *identifier = decl->identifier; |
Ian Romanick | a87ac25 | 2010-02-22 13:19:34 -0800 | [diff] [blame] | 4716 | /* FINISHME: Emit a warning if a variable declaration shadows a |
| 4717 | * FINISHME: declaration at a higher scope. |
| 4718 | */ |
| 4719 | |
Ian Romanick | cec65a6 | 2010-03-23 12:28:44 -0700 | [diff] [blame] | 4720 | if ((decl_type == NULL) || decl_type->is_void()) { |
Iago Toral Quiroga | 4472ab9 | 2014-04-14 09:14:23 +0200 | [diff] [blame] | 4721 | if (type_name != NULL) { |
| 4722 | _mesa_glsl_error(& loc, state, |
| 4723 | "invalid type `%s' in declaration of `%s'", |
| 4724 | type_name, decl->identifier); |
| 4725 | } else { |
| 4726 | _mesa_glsl_error(& loc, state, |
| 4727 | "invalid type in declaration of `%s'", |
| 4728 | decl->identifier); |
| 4729 | } |
| 4730 | continue; |
Ian Romanick | a87ac25 | 2010-02-22 13:19:34 -0800 | [diff] [blame] | 4731 | } |
| 4732 | |
Dave Airlie | 65ac360 | 2015-06-01 10:55:47 +1000 | [diff] [blame] | 4733 | if (this->type->qualifier.flags.q.subroutine) { |
| 4734 | const glsl_type *t; |
| 4735 | const char *name; |
| 4736 | |
| 4737 | t = state->symbols->get_type(this->type->specifier->type_name); |
| 4738 | if (!t) |
| 4739 | _mesa_glsl_error(& loc, state, |
| 4740 | "invalid type in declaration of `%s'", |
| 4741 | decl->identifier); |
| 4742 | name = ralloc_asprintf(ctx, "%s_%s", _mesa_shader_stage_to_subroutine_prefix(state->stage), decl->identifier); |
| 4743 | |
| 4744 | identifier = name; |
| 4745 | |
| 4746 | } |
Timothy Arceri | bfb4875 | 2014-01-23 23:16:41 +1100 | [diff] [blame] | 4747 | var_type = process_array_type(&loc, decl_type, decl->array_specifier, |
| 4748 | state); |
Ian Romanick | a87ac25 | 2010-02-22 13:19:34 -0800 | [diff] [blame] | 4749 | |
Dave Airlie | 65ac360 | 2015-06-01 10:55:47 +1000 | [diff] [blame] | 4750 | var = new(ctx) ir_variable(var_type, identifier, ir_var_auto); |
Ian Romanick | a87ac25 | 2010-02-22 13:19:34 -0800 | [diff] [blame] | 4751 | |
Bryan Cain | 2548092 | 2013-02-15 09:46:50 -0600 | [diff] [blame] | 4752 | /* The 'varying in' and 'varying out' qualifiers can only be used with |
| 4753 | * ARB_geometry_shader4 and EXT_geometry_shader4, which we don't support |
| 4754 | * yet. |
| 4755 | */ |
| 4756 | if (this->type->qualifier.flags.q.varying) { |
| 4757 | if (this->type->qualifier.flags.q.in) { |
| 4758 | _mesa_glsl_error(& loc, state, |
| 4759 | "`varying in' qualifier in declaration of " |
| 4760 | "`%s' only valid for geometry shaders using " |
| 4761 | "ARB_geometry_shader4 or EXT_geometry_shader4", |
| 4762 | decl->identifier); |
| 4763 | } else if (this->type->qualifier.flags.q.out) { |
| 4764 | _mesa_glsl_error(& loc, state, |
| 4765 | "`varying out' qualifier in declaration of " |
| 4766 | "`%s' only valid for geometry shaders using " |
| 4767 | "ARB_geometry_shader4 or EXT_geometry_shader4", |
| 4768 | decl->identifier); |
| 4769 | } |
| 4770 | } |
| 4771 | |
Eric Anholt | 3f15150 | 2010-04-02 01:53:57 -1000 | [diff] [blame] | 4772 | /* From page 22 (page 28 of the PDF) of the GLSL 1.10 specification; |
| 4773 | * |
| 4774 | * "Global variables can only use the qualifiers const, |
Chris Forbes | 9fec560 | 2014-04-21 15:55:58 +1200 | [diff] [blame] | 4775 | * attribute, uniform, or varying. Only one may be |
Eric Anholt | 3f15150 | 2010-04-02 01:53:57 -1000 | [diff] [blame] | 4776 | * specified. |
| 4777 | * |
| 4778 | * Local variables can only use the qualifier const." |
| 4779 | * |
Paul Berry | d4a2474 | 2012-08-02 08:18:12 -0700 | [diff] [blame] | 4780 | * This is relaxed in GLSL 1.30 and GLSL ES 3.00. It is also relaxed by |
| 4781 | * any extension that adds the 'layout' keyword. |
Eric Anholt | 3f15150 | 2010-04-02 01:53:57 -1000 | [diff] [blame] | 4782 | */ |
Paul Berry | d4a2474 | 2012-08-02 08:18:12 -0700 | [diff] [blame] | 4783 | if (!state->is_version(130, 300) |
Iago Toral Quiroga | 4472ab9 | 2014-04-14 09:14:23 +0200 | [diff] [blame] | 4784 | && !state->has_explicit_attrib_location() |
| 4785 | && !state->has_separate_shader_objects() |
| 4786 | && !state->ARB_fragment_coord_conventions_enable) { |
| 4787 | if (this->type->qualifier.flags.q.out) { |
| 4788 | _mesa_glsl_error(& loc, state, |
| 4789 | "`out' qualifier in declaration of `%s' " |
| 4790 | "only valid for function parameters in %s", |
| 4791 | decl->identifier, state->get_version_string()); |
| 4792 | } |
| 4793 | if (this->type->qualifier.flags.q.in) { |
| 4794 | _mesa_glsl_error(& loc, state, |
| 4795 | "`in' qualifier in declaration of `%s' " |
| 4796 | "only valid for function parameters in %s", |
| 4797 | decl->identifier, state->get_version_string()); |
| 4798 | } |
| 4799 | /* FINISHME: Test for other invalid qualifiers. */ |
Eric Anholt | 3f15150 | 2010-04-02 01:53:57 -1000 | [diff] [blame] | 4800 | } |
| 4801 | |
Eric Anholt | 2e063f1 | 2010-03-28 00:56:22 -0700 | [diff] [blame] | 4802 | apply_type_qualifier_to_variable(& this->type->qualifier, var, state, |
Timothy Arceri | 222f66a | 2016-09-28 16:04:08 +1000 | [diff] [blame] | 4803 | & loc, false); |
Timothy Arceri | 6498037 | 2015-11-12 17:43:52 +1100 | [diff] [blame] | 4804 | apply_layout_qualifier_to_variable(&this->type->qualifier, var, state, |
| 4805 | &loc); |
Ian Romanick | a87ac25 | 2010-02-22 13:19:34 -0800 | [diff] [blame] | 4806 | |
Rob Clark | f78a6b1 | 2016-06-24 14:28:51 -0400 | [diff] [blame] | 4807 | if ((var->data.mode == ir_var_auto || var->data.mode == ir_var_temporary) |
| 4808 | && (var->type->is_numeric() || var->type->is_boolean()) |
| 4809 | && state->zero_init) { |
| 4810 | const ir_constant_data data = {0}; |
| 4811 | var->data.has_initializer = true; |
| 4812 | var->constant_initializer = new(var) ir_constant(var->type, &data); |
| 4813 | } |
| 4814 | |
Ian Romanick | e24d35a | 2010-10-05 16:38:47 -0700 | [diff] [blame] | 4815 | if (this->type->qualifier.flags.q.invariant) { |
Dave Airlie | ab8ea1b | 2016-05-23 14:18:03 +1000 | [diff] [blame] | 4816 | if (!is_allowed_invariant(var, state)) { |
Chris Forbes | 0dfa6e7 | 2014-04-21 15:45:32 +1200 | [diff] [blame] | 4817 | _mesa_glsl_error(&loc, state, |
| 4818 | "`%s' cannot be marked invariant; interfaces between " |
| 4819 | "shader stages only", var->name); |
Iago Toral Quiroga | 4472ab9 | 2014-04-14 09:14:23 +0200 | [diff] [blame] | 4820 | } |
Ian Romanick | 6f0823d | 2010-07-01 20:39:08 -0700 | [diff] [blame] | 4821 | } |
| 4822 | |
Ian Romanick | e1c1a3f | 2010-03-31 12:26:03 -0700 | [diff] [blame] | 4823 | if (state->current_function != NULL) { |
Iago Toral Quiroga | 4472ab9 | 2014-04-14 09:14:23 +0200 | [diff] [blame] | 4824 | const char *mode = NULL; |
| 4825 | const char *extra = ""; |
Ian Romanick | b168e53 | 2010-03-31 12:31:18 -0700 | [diff] [blame] | 4826 | |
Iago Toral Quiroga | 4472ab9 | 2014-04-14 09:14:23 +0200 | [diff] [blame] | 4827 | /* There is no need to check for 'inout' here because the parser will |
| 4828 | * only allow that in function parameter lists. |
| 4829 | */ |
| 4830 | if (this->type->qualifier.flags.q.attribute) { |
| 4831 | mode = "attribute"; |
Dave Airlie | 65ac360 | 2015-06-01 10:55:47 +1000 | [diff] [blame] | 4832 | } else if (this->type->qualifier.flags.q.subroutine) { |
| 4833 | mode = "subroutine uniform"; |
Iago Toral Quiroga | 4472ab9 | 2014-04-14 09:14:23 +0200 | [diff] [blame] | 4834 | } else if (this->type->qualifier.flags.q.uniform) { |
| 4835 | mode = "uniform"; |
| 4836 | } else if (this->type->qualifier.flags.q.varying) { |
| 4837 | mode = "varying"; |
| 4838 | } else if (this->type->qualifier.flags.q.in) { |
| 4839 | mode = "in"; |
| 4840 | extra = " or in function parameter list"; |
| 4841 | } else if (this->type->qualifier.flags.q.out) { |
| 4842 | mode = "out"; |
| 4843 | extra = " or in function parameter list"; |
| 4844 | } |
Ian Romanick | b168e53 | 2010-03-31 12:31:18 -0700 | [diff] [blame] | 4845 | |
Iago Toral Quiroga | 4472ab9 | 2014-04-14 09:14:23 +0200 | [diff] [blame] | 4846 | if (mode) { |
| 4847 | _mesa_glsl_error(& loc, state, |
| 4848 | "%s variable `%s' must be declared at " |
| 4849 | "global scope%s", |
| 4850 | mode, var->name, extra); |
| 4851 | } |
Tapani Pälli | 33ee2c6 | 2013-12-12 13:51:01 +0200 | [diff] [blame] | 4852 | } else if (var->data.mode == ir_var_shader_in) { |
Tapani Pälli | c1d3080 | 2013-12-12 12:57:57 +0200 | [diff] [blame] | 4853 | var->data.read_only = true; |
Chad Versace | 01a584d | 2011-01-20 14:12:16 -0800 | [diff] [blame] | 4854 | |
Iago Toral Quiroga | 4472ab9 | 2014-04-14 09:14:23 +0200 | [diff] [blame] | 4855 | if (state->stage == MESA_SHADER_VERTEX) { |
| 4856 | bool error_emitted = false; |
Ian Romanick | fb9f5b0 | 2010-03-29 17:16:35 -0700 | [diff] [blame] | 4857 | |
Iago Toral Quiroga | 4472ab9 | 2014-04-14 09:14:23 +0200 | [diff] [blame] | 4858 | /* From page 31 (page 37 of the PDF) of the GLSL 1.50 spec: |
| 4859 | * |
| 4860 | * "Vertex shader inputs can only be float, floating-point |
| 4861 | * vectors, matrices, signed and unsigned integers and integer |
| 4862 | * vectors. Vertex shader inputs can also form arrays of these |
| 4863 | * types, but not structures." |
| 4864 | * |
| 4865 | * From page 31 (page 27 of the PDF) of the GLSL 1.30 spec: |
| 4866 | * |
| 4867 | * "Vertex shader inputs can only be float, floating-point |
| 4868 | * vectors, matrices, signed and unsigned integers and integer |
| 4869 | * vectors. They cannot be arrays or structures." |
| 4870 | * |
| 4871 | * From page 23 (page 29 of the PDF) of the GLSL 1.20 spec: |
| 4872 | * |
| 4873 | * "The attribute qualifier can be used only with float, |
| 4874 | * floating-point vectors, and matrices. Attribute variables |
| 4875 | * cannot be declared as arrays or structures." |
Paul Berry | d4a2474 | 2012-08-02 08:18:12 -0700 | [diff] [blame] | 4876 | * |
| 4877 | * From page 33 (page 39 of the PDF) of the GLSL ES 3.00 spec: |
| 4878 | * |
| 4879 | * "Vertex shader inputs can only be float, floating-point |
| 4880 | * vectors, matrices, signed and unsigned integers and integer |
| 4881 | * vectors. Vertex shader inputs cannot be arrays or |
| 4882 | * structures." |
Iago Toral Quiroga | 4472ab9 | 2014-04-14 09:14:23 +0200 | [diff] [blame] | 4883 | */ |
Timothy Arceri | ca9e280 | 2014-08-18 21:46:44 -1000 | [diff] [blame] | 4884 | const glsl_type *check_type = var->type->without_array(); |
Ian Romanick | fb9f5b0 | 2010-03-29 17:16:35 -0700 | [diff] [blame] | 4885 | |
Iago Toral Quiroga | 4472ab9 | 2014-04-14 09:14:23 +0200 | [diff] [blame] | 4886 | switch (check_type->base_type) { |
| 4887 | case GLSL_TYPE_FLOAT: |
| 4888 | break; |
| 4889 | case GLSL_TYPE_UINT: |
| 4890 | case GLSL_TYPE_INT: |
| 4891 | if (state->is_version(120, 300)) |
| 4892 | break; |
Dave Airlie | 5d6190e | 2015-02-20 11:38:12 +1000 | [diff] [blame] | 4893 | case GLSL_TYPE_DOUBLE: |
| 4894 | if (check_type->base_type == GLSL_TYPE_DOUBLE && (state->is_version(410, 0) || state->ARB_vertex_attrib_64bit_enable)) |
| 4895 | break; |
Iago Toral Quiroga | 4472ab9 | 2014-04-14 09:14:23 +0200 | [diff] [blame] | 4896 | /* FALLTHROUGH */ |
| 4897 | default: |
| 4898 | _mesa_glsl_error(& loc, state, |
| 4899 | "vertex shader input / attribute cannot have " |
| 4900 | "type %s`%s'", |
| 4901 | var->type->is_array() ? "array of " : "", |
| 4902 | check_type->name); |
| 4903 | error_emitted = true; |
| 4904 | } |
Ian Romanick | fb9f5b0 | 2010-03-29 17:16:35 -0700 | [diff] [blame] | 4905 | |
Iago Toral Quiroga | 4472ab9 | 2014-04-14 09:14:23 +0200 | [diff] [blame] | 4906 | if (!error_emitted && var->type->is_array() && |
Paul Berry | b2265db | 2013-07-11 15:40:11 -0700 | [diff] [blame] | 4907 | !state->check_version(150, 0, &loc, |
Paul Berry | 0d9bba6 | 2012-08-05 09:57:01 -0700 | [diff] [blame] | 4908 | "vertex shader input / attribute " |
| 4909 | "cannot have array type")) { |
Iago Toral Quiroga | 4472ab9 | 2014-04-14 09:14:23 +0200 | [diff] [blame] | 4910 | error_emitted = true; |
| 4911 | } |
| 4912 | } else if (state->stage == MESA_SHADER_GEOMETRY) { |
Paul Berry | 05234e7 | 2013-04-10 07:04:33 -0700 | [diff] [blame] | 4913 | /* From section 4.3.4 (Inputs) of the GLSL 1.50 spec: |
| 4914 | * |
| 4915 | * Geometry shader input variables get the per-vertex values |
| 4916 | * written out by vertex shader output variables of the same |
| 4917 | * names. Since a geometry shader operates on a set of |
| 4918 | * vertices, each input varying variable (or input block, see |
| 4919 | * interface blocks below) needs to be declared as an array. |
| 4920 | */ |
| 4921 | if (!var->type->is_array()) { |
| 4922 | _mesa_glsl_error(&loc, state, |
| 4923 | "geometry shader inputs must be arrays"); |
| 4924 | } |
Paul Berry | 7cfefe6 | 2013-07-30 21:13:48 -0700 | [diff] [blame] | 4925 | |
| 4926 | handle_geometry_shader_input_decl(state, loc, var); |
Timothy Arceri | 94d669b | 2015-06-12 16:03:56 +1000 | [diff] [blame] | 4927 | } else if (state->stage == MESA_SHADER_FRAGMENT) { |
| 4928 | /* From section 4.3.4 (Input Variables) of the GLSL ES 3.10 spec: |
| 4929 | * |
| 4930 | * It is a compile-time error to declare a fragment shader |
| 4931 | * input with, or that contains, any of the following types: |
| 4932 | * |
| 4933 | * * A boolean type |
| 4934 | * * An opaque type |
| 4935 | * * An array of arrays |
| 4936 | * * An array of structures |
| 4937 | * * A structure containing an array |
| 4938 | * * A structure containing a structure |
| 4939 | */ |
| 4940 | if (state->es_shader) { |
| 4941 | const glsl_type *check_type = var->type->without_array(); |
| 4942 | if (check_type->is_boolean() || |
| 4943 | check_type->contains_opaque()) { |
| 4944 | _mesa_glsl_error(&loc, state, |
| 4945 | "fragment shader input cannot have type %s", |
| 4946 | check_type->name); |
| 4947 | } |
| 4948 | if (var->type->is_array() && |
| 4949 | var->type->fields.array->is_array()) { |
| 4950 | _mesa_glsl_error(&loc, state, |
| 4951 | "%s shader output " |
| 4952 | "cannot have an array of arrays", |
| 4953 | _mesa_shader_stage_to_string(state->stage)); |
| 4954 | } |
| 4955 | if (var->type->is_array() && |
| 4956 | var->type->fields.array->is_record()) { |
| 4957 | _mesa_glsl_error(&loc, state, |
| 4958 | "fragment shader input " |
| 4959 | "cannot have an array of structs"); |
| 4960 | } |
| 4961 | if (var->type->is_record()) { |
| 4962 | for (unsigned i = 0; i < var->type->length; i++) { |
| 4963 | if (var->type->fields.structure[i].type->is_array() || |
| 4964 | var->type->fields.structure[i].type->is_record()) |
| 4965 | _mesa_glsl_error(&loc, state, |
| 4966 | "fragement shader input cannot have " |
| 4967 | "a struct that contains an " |
| 4968 | "array or struct"); |
| 4969 | } |
| 4970 | } |
| 4971 | } |
Chris Forbes | 61846f2 | 2014-09-01 20:48:09 +1200 | [diff] [blame] | 4972 | } else if (state->stage == MESA_SHADER_TESS_CTRL || |
| 4973 | state->stage == MESA_SHADER_TESS_EVAL) { |
| 4974 | handle_tess_shader_input_decl(state, loc, var); |
Paul Berry | 05234e7 | 2013-04-10 07:04:33 -0700 | [diff] [blame] | 4975 | } |
Tapani Pälli | 3bbaf71 | 2014-08-11 12:03:54 +0300 | [diff] [blame] | 4976 | } else if (var->data.mode == ir_var_shader_out) { |
| 4977 | const glsl_type *check_type = var->type->without_array(); |
| 4978 | |
| 4979 | /* From section 4.3.6 (Output variables) of the GLSL 4.40 spec: |
| 4980 | * |
| 4981 | * It is a compile-time error to declare a vertex, tessellation |
| 4982 | * evaluation, tessellation control, or geometry shader output |
| 4983 | * that contains any of the following: |
| 4984 | * |
| 4985 | * * A Boolean type (bool, bvec2 ...) |
| 4986 | * * An opaque type |
| 4987 | */ |
| 4988 | if (check_type->is_boolean() || check_type->contains_opaque()) |
| 4989 | _mesa_glsl_error(&loc, state, |
| 4990 | "%s shader output cannot have type %s", |
| 4991 | _mesa_shader_stage_to_string(state->stage), |
| 4992 | check_type->name); |
| 4993 | |
| 4994 | /* From section 4.3.6 (Output variables) of the GLSL 4.40 spec: |
| 4995 | * |
| 4996 | * It is a compile-time error to declare a fragment shader output |
| 4997 | * that contains any of the following: |
| 4998 | * |
| 4999 | * * A Boolean type (bool, bvec2 ...) |
| 5000 | * * A double-precision scalar or vector (double, dvec2 ...) |
| 5001 | * * An opaque type |
| 5002 | * * Any matrix type |
| 5003 | * * A structure |
| 5004 | */ |
| 5005 | if (state->stage == MESA_SHADER_FRAGMENT) { |
| 5006 | if (check_type->is_record() || check_type->is_matrix()) |
| 5007 | _mesa_glsl_error(&loc, state, |
| 5008 | "fragment shader output " |
Timothy Arceri | faf7670 | 2015-06-10 18:35:08 +1000 | [diff] [blame] | 5009 | "cannot have struct or matrix type"); |
Tapani Pälli | 3bbaf71 | 2014-08-11 12:03:54 +0300 | [diff] [blame] | 5010 | switch (check_type->base_type) { |
| 5011 | case GLSL_TYPE_UINT: |
| 5012 | case GLSL_TYPE_INT: |
| 5013 | case GLSL_TYPE_FLOAT: |
| 5014 | break; |
| 5015 | default: |
| 5016 | _mesa_glsl_error(&loc, state, |
| 5017 | "fragment shader output cannot have " |
| 5018 | "type %s", check_type->name); |
| 5019 | } |
| 5020 | } |
Timothy Arceri | 3d78bde | 2015-06-10 18:46:22 +1000 | [diff] [blame] | 5021 | |
| 5022 | /* From section 4.3.6 (Output Variables) of the GLSL ES 3.10 spec: |
| 5023 | * |
| 5024 | * It is a compile-time error to declare a vertex shader output |
| 5025 | * with, or that contains, any of the following types: |
| 5026 | * |
| 5027 | * * A boolean type |
| 5028 | * * An opaque type |
| 5029 | * * An array of arrays |
| 5030 | * * An array of structures |
| 5031 | * * A structure containing an array |
| 5032 | * * A structure containing a structure |
| 5033 | * |
| 5034 | * It is a compile-time error to declare a fragment shader output |
| 5035 | * with, or that contains, any of the following types: |
| 5036 | * |
| 5037 | * * A boolean type |
| 5038 | * * An opaque type |
| 5039 | * * A matrix |
| 5040 | * * A structure |
| 5041 | * * An array of array |
Kenneth Graunke | bc7f1ed | 2017-01-02 02:56:52 -0800 | [diff] [blame] | 5042 | * |
| 5043 | * ES 3.20 updates this to apply to tessellation and geometry shaders |
| 5044 | * as well. Because there are per-vertex arrays in the new stages, |
| 5045 | * it strikes the "array of..." rules and replaces them with these: |
| 5046 | * |
| 5047 | * * For per-vertex-arrayed variables (applies to tessellation |
| 5048 | * control, tessellation evaluation and geometry shaders): |
| 5049 | * |
| 5050 | * * Per-vertex-arrayed arrays of arrays |
| 5051 | * * Per-vertex-arrayed arrays of structures |
| 5052 | * |
| 5053 | * * For non-per-vertex-arrayed variables: |
| 5054 | * |
| 5055 | * * An array of arrays |
| 5056 | * * An array of structures |
| 5057 | * |
| 5058 | * which basically says to unwrap the per-vertex aspect and apply |
| 5059 | * the old rules. |
Timothy Arceri | 3d78bde | 2015-06-10 18:46:22 +1000 | [diff] [blame] | 5060 | */ |
| 5061 | if (state->es_shader) { |
| 5062 | if (var->type->is_array() && |
| 5063 | var->type->fields.array->is_array()) { |
| 5064 | _mesa_glsl_error(&loc, state, |
| 5065 | "%s shader output " |
| 5066 | "cannot have an array of arrays", |
| 5067 | _mesa_shader_stage_to_string(state->stage)); |
| 5068 | } |
Kenneth Graunke | bc7f1ed | 2017-01-02 02:56:52 -0800 | [diff] [blame] | 5069 | if (state->stage <= MESA_SHADER_GEOMETRY) { |
| 5070 | const glsl_type *type = var->type; |
| 5071 | |
| 5072 | if (state->stage == MESA_SHADER_TESS_CTRL && |
| 5073 | !var->data.patch && var->type->is_array()) { |
| 5074 | type = var->type->fields.array; |
Timothy Arceri | 3d78bde | 2015-06-10 18:46:22 +1000 | [diff] [blame] | 5075 | } |
Kenneth Graunke | bc7f1ed | 2017-01-02 02:56:52 -0800 | [diff] [blame] | 5076 | |
| 5077 | if (type->is_array() && type->fields.array->is_record()) { |
| 5078 | _mesa_glsl_error(&loc, state, |
| 5079 | "%s shader output cannot have " |
| 5080 | "an array of structs", |
| 5081 | _mesa_shader_stage_to_string(state->stage)); |
| 5082 | } |
| 5083 | if (type->is_record()) { |
| 5084 | for (unsigned i = 0; i < type->length; i++) { |
| 5085 | if (type->fields.structure[i].type->is_array() || |
| 5086 | type->fields.structure[i].type->is_record()) |
Timothy Arceri | 3d78bde | 2015-06-10 18:46:22 +1000 | [diff] [blame] | 5087 | _mesa_glsl_error(&loc, state, |
Kenneth Graunke | bc7f1ed | 2017-01-02 02:56:52 -0800 | [diff] [blame] | 5088 | "%s shader output cannot have a " |
Timothy Arceri | 3d78bde | 2015-06-10 18:46:22 +1000 | [diff] [blame] | 5089 | "struct that contains an " |
Kenneth Graunke | bc7f1ed | 2017-01-02 02:56:52 -0800 | [diff] [blame] | 5090 | "array or struct", |
| 5091 | _mesa_shader_stage_to_string(state->stage)); |
Timothy Arceri | 3d78bde | 2015-06-10 18:46:22 +1000 | [diff] [blame] | 5092 | } |
| 5093 | } |
| 5094 | } |
| 5095 | } |
Fabian Bieler | 497eb29 | 2014-03-20 22:44:43 +0100 | [diff] [blame] | 5096 | |
| 5097 | if (state->stage == MESA_SHADER_TESS_CTRL) { |
| 5098 | handle_tess_ctrl_shader_output_decl(state, loc, var); |
| 5099 | } |
Dave Airlie | 65ac360 | 2015-06-01 10:55:47 +1000 | [diff] [blame] | 5100 | } else if (var->type->contains_subroutine()) { |
| 5101 | /* declare subroutine uniforms as hidden */ |
| 5102 | var->data.how_declared = ir_var_hidden; |
Ian Romanick | fb9f5b0 | 2010-03-29 17:16:35 -0700 | [diff] [blame] | 5103 | } |
| 5104 | |
Fabian Bieler | 1009b33 | 2014-03-05 13:43:17 +0100 | [diff] [blame] | 5105 | /* From section 4.3.4 of the GLSL 4.00 spec: |
| 5106 | * "Input variables may not be declared using the patch in qualifier |
| 5107 | * in tessellation control or geometry shaders." |
| 5108 | * |
| 5109 | * From section 4.3.6 of the GLSL 4.00 spec: |
| 5110 | * "It is an error to use patch out in a vertex, tessellation |
| 5111 | * evaluation, or geometry shader." |
| 5112 | * |
| 5113 | * This doesn't explicitly forbid using them in a fragment shader, but |
| 5114 | * that's probably just an oversight. |
| 5115 | */ |
| 5116 | if (state->stage != MESA_SHADER_TESS_EVAL |
| 5117 | && this->type->qualifier.flags.q.patch |
| 5118 | && this->type->qualifier.flags.q.in) { |
| 5119 | |
| 5120 | _mesa_glsl_error(&loc, state, "'patch in' can only be used in a " |
| 5121 | "tessellation evaluation shader"); |
| 5122 | } |
| 5123 | |
| 5124 | if (state->stage != MESA_SHADER_TESS_CTRL |
| 5125 | && this->type->qualifier.flags.q.patch |
| 5126 | && this->type->qualifier.flags.q.out) { |
| 5127 | |
| 5128 | _mesa_glsl_error(&loc, state, "'patch out' can only be used in a " |
| 5129 | "tessellation control shader"); |
| 5130 | } |
| 5131 | |
Chad Versace | 889e1a5 | 2011-01-16 22:38:45 -0800 | [diff] [blame] | 5132 | /* Precision qualifiers exists only in GLSL versions 1.00 and >= 1.30. |
| 5133 | */ |
Kenneth Graunke | 6eec502 | 2013-07-15 15:58:29 -0700 | [diff] [blame] | 5134 | if (this->type->qualifier.precision != ast_precision_none) { |
Paul Berry | 0d9bba6 | 2012-08-05 09:57:01 -0700 | [diff] [blame] | 5135 | state->check_precision_qualifiers_allowed(&loc); |
Chad Versace | 889e1a5 | 2011-01-16 22:38:45 -0800 | [diff] [blame] | 5136 | } |
| 5137 | |
Ian Romanick | 9c87282 | 2016-06-13 15:22:34 -0700 | [diff] [blame] | 5138 | if (this->type->qualifier.precision != ast_precision_none && |
| 5139 | !precision_qualifier_allowed(var->type)) { |
Chad Versace | 889e1a5 | 2011-01-16 22:38:45 -0800 | [diff] [blame] | 5140 | _mesa_glsl_error(&loc, state, |
Kenneth Graunke | 8752824 | 2011-03-26 23:37:09 -0700 | [diff] [blame] | 5141 | "precision qualifiers apply only to floating point" |
Francisco Jerez | 26b1141 | 2015-08-17 01:28:57 +0300 | [diff] [blame] | 5142 | ", integer and opaque types"); |
Chad Versace | 889e1a5 | 2011-01-16 22:38:45 -0800 | [diff] [blame] | 5143 | } |
| 5144 | |
Francisco Jerez | 7af167d | 2013-11-25 19:38:37 -0800 | [diff] [blame] | 5145 | /* From section 4.1.7 of the GLSL 4.40 spec: |
Paul Berry | f072210 | 2011-07-12 12:03:02 -0700 | [diff] [blame] | 5146 | * |
Francisco Jerez | 7af167d | 2013-11-25 19:38:37 -0800 | [diff] [blame] | 5147 | * "[Opaque types] can only be declared as function |
| 5148 | * parameters or uniform-qualified variables." |
Paul Berry | f072210 | 2011-07-12 12:03:02 -0700 | [diff] [blame] | 5149 | */ |
Francisco Jerez | 7af167d | 2013-11-25 19:38:37 -0800 | [diff] [blame] | 5150 | if (var_type->contains_opaque() && |
Paul Berry | f072210 | 2011-07-12 12:03:02 -0700 | [diff] [blame] | 5151 | !this->type->qualifier.flags.q.uniform) { |
Francisco Jerez | 7af167d | 2013-11-25 19:38:37 -0800 | [diff] [blame] | 5152 | _mesa_glsl_error(&loc, state, |
| 5153 | "opaque variables must be declared uniform"); |
Paul Berry | f072210 | 2011-07-12 12:03:02 -0700 | [diff] [blame] | 5154 | } |
| 5155 | |
Ian Romanick | e78e0fa | 2010-07-07 12:13:34 -0700 | [diff] [blame] | 5156 | /* Process the initializer and add its instructions to a temporary |
| 5157 | * list. This list will be added to the instruction stream (below) after |
| 5158 | * the declaration is added. This is done because in some cases (such as |
| 5159 | * redeclarations) the declaration may not actually be added to the |
| 5160 | * instruction stream. |
| 5161 | */ |
Eric Anholt | fa33d0b | 2010-07-29 13:50:17 -0700 | [diff] [blame] | 5162 | exec_list initializer_instructions; |
Brian Paul | f9cecca | 2014-05-23 14:59:33 -0600 | [diff] [blame] | 5163 | |
| 5164 | /* Examine var name here since var may get deleted in the next call */ |
Brian Paul | a7aca39 | 2014-05-23 14:57:49 -0600 | [diff] [blame] | 5165 | bool var_is_gl_id = is_gl_identifier(var->name); |
Brian Paul | f9cecca | 2014-05-23 14:59:33 -0600 | [diff] [blame] | 5166 | |
Paul Berry | 3699ff4 | 2013-09-27 20:38:29 -0700 | [diff] [blame] | 5167 | ir_variable *earlier = |
Paul Berry | 1b4a737 | 2013-09-27 20:53:33 -0700 | [diff] [blame] | 5168 | get_variable_being_redeclared(var, decl->get_location(), state, |
| 5169 | false /* allow_all_redeclarations */); |
Paul Berry | 2bbcf19 | 2013-11-13 16:53:18 -0800 | [diff] [blame] | 5170 | if (earlier != NULL) { |
Brian Paul | f9cecca | 2014-05-23 14:59:33 -0600 | [diff] [blame] | 5171 | if (var_is_gl_id && |
Tapani Pälli | 33ee2c6 | 2013-12-12 13:51:01 +0200 | [diff] [blame] | 5172 | earlier->data.how_declared == ir_var_declared_in_block) { |
Paul Berry | 2bbcf19 | 2013-11-13 16:53:18 -0800 | [diff] [blame] | 5173 | _mesa_glsl_error(&loc, state, |
| 5174 | "`%s' has already been redeclared using " |
Brian Paul | 14379a0 | 2014-10-17 13:31:53 -0600 | [diff] [blame] | 5175 | "gl_PerVertex", earlier->name); |
Paul Berry | 2bbcf19 | 2013-11-13 16:53:18 -0800 | [diff] [blame] | 5176 | } |
Tapani Pälli | 33ee2c6 | 2013-12-12 13:51:01 +0200 | [diff] [blame] | 5177 | earlier->data.how_declared = ir_var_declared_normally; |
Paul Berry | 2bbcf19 | 2013-11-13 16:53:18 -0800 | [diff] [blame] | 5178 | } |
Ian Romanick | 09a4ba0 | 2011-03-04 16:15:20 -0800 | [diff] [blame] | 5179 | |
Ian Romanick | 66faec4 | 2010-03-27 18:56:53 -0700 | [diff] [blame] | 5180 | if (decl->initializer != NULL) { |
Iago Toral Quiroga | 4472ab9 | 2014-04-14 09:14:23 +0200 | [diff] [blame] | 5181 | result = process_initializer((earlier == NULL) ? var : earlier, |
| 5182 | decl, this->type, |
| 5183 | &initializer_instructions, state); |
Timothy Arceri | dea0af8 | 2015-10-15 14:35:41 +1100 | [diff] [blame] | 5184 | } else { |
| 5185 | validate_array_dimensions(var_type, state, &loc); |
Ian Romanick | 1936015 | 2010-03-26 18:05:27 -0700 | [diff] [blame] | 5186 | } |
Ian Romanick | 17d86f4 | 2010-03-29 12:59:02 -0700 | [diff] [blame] | 5187 | |
Eric Anholt | 0ed6125 | 2010-03-31 09:29:33 -1000 | [diff] [blame] | 5188 | /* From page 23 (page 29 of the PDF) of the GLSL 1.10 spec: |
| 5189 | * |
| 5190 | * "It is an error to write to a const variable outside of |
| 5191 | * its declaration, so they must be initialized when |
| 5192 | * declared." |
| 5193 | */ |
Ian Romanick | e24d35a | 2010-10-05 16:38:47 -0700 | [diff] [blame] | 5194 | if (this->type->qualifier.flags.q.constant && decl->initializer == NULL) { |
Iago Toral Quiroga | 4472ab9 | 2014-04-14 09:14:23 +0200 | [diff] [blame] | 5195 | _mesa_glsl_error(& loc, state, |
| 5196 | "const declaration of `%s' must be initialized", |
| 5197 | decl->identifier); |
Eric Anholt | 0ed6125 | 2010-03-31 09:29:33 -1000 | [diff] [blame] | 5198 | } |
| 5199 | |
Timothy Arceri | da69964 | 2015-06-15 21:00:47 +1000 | [diff] [blame] | 5200 | if (state->es_shader) { |
Iago Toral Quiroga | 4472ab9 | 2014-04-14 09:14:23 +0200 | [diff] [blame] | 5201 | const glsl_type *const t = (earlier == NULL) |
| 5202 | ? var->type : earlier->type; |
Ian Romanick | 42624b1 | 2013-08-08 17:23:01 -0700 | [diff] [blame] | 5203 | |
Kenneth Graunke | 04026b4 | 2016-09-15 02:10:23 -0700 | [diff] [blame] | 5204 | /* Skip the unsized array check for TCS/TES/GS inputs & TCS outputs. |
| 5205 | * |
| 5206 | * The GL_OES_tessellation_shader spec says about inputs: |
| 5207 | * |
| 5208 | * "Declaring an array size is optional. If no size is specified, |
| 5209 | * it will be taken from the implementation-dependent maximum |
| 5210 | * patch size (gl_MaxPatchVertices)." |
| 5211 | * |
| 5212 | * and about TCS outputs: |
| 5213 | * |
| 5214 | * "If no size is specified, it will be taken from output patch |
| 5215 | * size declared in the shader." |
| 5216 | * |
| 5217 | * The GL_OES_geometry_shader spec says: |
| 5218 | * |
| 5219 | * "All geometry shader input unsized array declarations will be |
| 5220 | * sized by an earlier input primitive layout qualifier, when |
| 5221 | * present, as per the following table." |
| 5222 | */ |
Samuel Iglesias Gonsálvez | c208816 | 2017-02-09 13:54:46 +0100 | [diff] [blame^] | 5223 | const enum ir_variable_mode mode = (const enum ir_variable_mode) |
| 5224 | (earlier == NULL ? var->data.mode : earlier->data.mode); |
Kenneth Graunke | 04026b4 | 2016-09-15 02:10:23 -0700 | [diff] [blame] | 5225 | const bool implicitly_sized = |
Samuel Iglesias Gonsálvez | c208816 | 2017-02-09 13:54:46 +0100 | [diff] [blame^] | 5226 | (mode == ir_var_shader_in && |
Kenneth Graunke | 04026b4 | 2016-09-15 02:10:23 -0700 | [diff] [blame] | 5227 | state->stage >= MESA_SHADER_TESS_CTRL && |
| 5228 | state->stage <= MESA_SHADER_GEOMETRY) || |
Samuel Iglesias Gonsálvez | c208816 | 2017-02-09 13:54:46 +0100 | [diff] [blame^] | 5229 | (mode == ir_var_shader_out && |
Kenneth Graunke | 04026b4 | 2016-09-15 02:10:23 -0700 | [diff] [blame] | 5230 | state->stage == MESA_SHADER_TESS_CTRL); |
| 5231 | |
| 5232 | if (t->is_unsized_array() && !implicitly_sized) |
Ian Romanick | 42624b1 | 2013-08-08 17:23:01 -0700 | [diff] [blame] | 5233 | /* Section 10.17 of the GLSL ES 1.00 specification states that |
| 5234 | * unsized array declarations have been removed from the language. |
| 5235 | * Arrays that are sized using an initializer are still explicitly |
| 5236 | * sized. However, GLSL ES 1.00 does not allow array |
| 5237 | * initializers. That is only allowed in GLSL ES 3.00. |
| 5238 | * |
| 5239 | * Section 4.1.9 (Arrays) of the GLSL ES 3.00 spec says: |
| 5240 | * |
| 5241 | * "An array type can also be formed without specifying a size |
| 5242 | * if the definition includes an initializer: |
| 5243 | * |
| 5244 | * float x[] = float[2] (1.0, 2.0); // declares an array of size 2 |
| 5245 | * float y[] = float[] (1.0, 2.0, 3.0); // declares an array of size 3 |
| 5246 | * |
| 5247 | * float a[5]; |
| 5248 | * float b[] = a;" |
| 5249 | */ |
| 5250 | _mesa_glsl_error(& loc, state, |
| 5251 | "unsized array declarations are not allowed in " |
| 5252 | "GLSL ES"); |
| 5253 | } |
| 5254 | |
Ian Romanick | 09a4ba0 | 2011-03-04 16:15:20 -0800 | [diff] [blame] | 5255 | /* If the declaration is not a redeclaration, there are a few additional |
| 5256 | * semantic checks that must be applied. In addition, variable that was |
| 5257 | * created for the declaration should be added to the IR stream. |
| 5258 | */ |
| 5259 | if (earlier == NULL) { |
Paul Berry | 1838df9 | 2013-09-27 17:18:14 -0700 | [diff] [blame] | 5260 | validate_identifier(decl->identifier, loc, state); |
Ian Romanick | 09a4ba0 | 2011-03-04 16:15:20 -0800 | [diff] [blame] | 5261 | |
Iago Toral Quiroga | 4472ab9 | 2014-04-14 09:14:23 +0200 | [diff] [blame] | 5262 | /* Add the variable to the symbol table. Note that the initializer's |
| 5263 | * IR was already processed earlier (though it hasn't been emitted |
| 5264 | * yet), without the variable in scope. |
| 5265 | * |
| 5266 | * This differs from most C-like languages, but it follows the GLSL |
| 5267 | * specification. From page 28 (page 34 of the PDF) of the GLSL 1.50 |
| 5268 | * spec: |
| 5269 | * |
| 5270 | * "Within a declaration, the scope of a name starts immediately |
| 5271 | * after the initializer if present or immediately after the name |
| 5272 | * being declared if not." |
| 5273 | */ |
| 5274 | if (!state->symbols->add_variable(var)) { |
| 5275 | YYLTYPE loc = this->get_location(); |
| 5276 | _mesa_glsl_error(&loc, state, "name `%s' already taken in the " |
| 5277 | "current scope", decl->identifier); |
| 5278 | continue; |
| 5279 | } |
Ian Romanick | 09a4ba0 | 2011-03-04 16:15:20 -0800 | [diff] [blame] | 5280 | |
Iago Toral Quiroga | 4472ab9 | 2014-04-14 09:14:23 +0200 | [diff] [blame] | 5281 | /* Push the variable declaration to the top. It means that all the |
| 5282 | * variable declarations will appear in a funny last-to-first order, |
| 5283 | * but otherwise we run into trouble if a function is prototyped, a |
| 5284 | * global var is decled, then the function is defined with usage of |
| 5285 | * the global var. See glslparsertest's CorrectModule.frag. |
| 5286 | */ |
| 5287 | instructions->push_head(var); |
Ian Romanick | 5466b63 | 2010-07-01 12:46:55 -0700 | [diff] [blame] | 5288 | } |
| 5289 | |
Eric Anholt | fa33d0b | 2010-07-29 13:50:17 -0700 | [diff] [blame] | 5290 | instructions->append_list(&initializer_instructions); |
Ian Romanick | a87ac25 | 2010-02-22 13:19:34 -0800 | [diff] [blame] | 5291 | } |
| 5292 | |
Eric Anholt | 8558459 | 2010-04-14 15:38:52 -0700 | [diff] [blame] | 5293 | |
| 5294 | /* Generally, variable declarations do not have r-values. However, |
| 5295 | * one is used for the declaration in |
| 5296 | * |
| 5297 | * while (bool b = some_condition()) { |
| 5298 | * ... |
| 5299 | * } |
| 5300 | * |
| 5301 | * so we return the rvalue from the last seen declaration here. |
Ian Romanick | a87ac25 | 2010-02-22 13:19:34 -0800 | [diff] [blame] | 5302 | */ |
Eric Anholt | 8558459 | 2010-04-14 15:38:52 -0700 | [diff] [blame] | 5303 | return result; |
Ian Romanick | a87ac25 | 2010-02-22 13:19:34 -0800 | [diff] [blame] | 5304 | } |
| 5305 | |
| 5306 | |
Kenneth Graunke | fb9fb5f | 2010-03-26 00:25:36 -0700 | [diff] [blame] | 5307 | ir_rvalue * |
Ian Romanick | 0044e7e | 2010-03-08 23:44:00 -0800 | [diff] [blame] | 5308 | ast_parameter_declarator::hir(exec_list *instructions, |
Iago Toral Quiroga | 4472ab9 | 2014-04-14 09:14:23 +0200 | [diff] [blame] | 5309 | struct _mesa_glsl_parse_state *state) |
Ian Romanick | a87ac25 | 2010-02-22 13:19:34 -0800 | [diff] [blame] | 5310 | { |
Kenneth Graunke | 953ff12 | 2010-06-25 13:14:37 -0700 | [diff] [blame] | 5311 | void *ctx = state; |
Ian Romanick | a87ac25 | 2010-02-22 13:19:34 -0800 | [diff] [blame] | 5312 | const struct glsl_type *type; |
| 5313 | const char *name = NULL; |
Eric Anholt | 2e063f1 | 2010-03-28 00:56:22 -0700 | [diff] [blame] | 5314 | YYLTYPE loc = this->get_location(); |
Ian Romanick | a87ac25 | 2010-02-22 13:19:34 -0800 | [diff] [blame] | 5315 | |
Ian Romanick | cabd457 | 2013-08-09 15:17:18 -0700 | [diff] [blame] | 5316 | type = this->type->glsl_type(& name, state); |
Ian Romanick | a87ac25 | 2010-02-22 13:19:34 -0800 | [diff] [blame] | 5317 | |
| 5318 | if (type == NULL) { |
Ian Romanick | a87ac25 | 2010-02-22 13:19:34 -0800 | [diff] [blame] | 5319 | if (name != NULL) { |
Iago Toral Quiroga | 4472ab9 | 2014-04-14 09:14:23 +0200 | [diff] [blame] | 5320 | _mesa_glsl_error(& loc, state, |
| 5321 | "invalid type `%s' in declaration of `%s'", |
| 5322 | name, this->identifier); |
Ian Romanick | a87ac25 | 2010-02-22 13:19:34 -0800 | [diff] [blame] | 5323 | } else { |
Iago Toral Quiroga | 4472ab9 | 2014-04-14 09:14:23 +0200 | [diff] [blame] | 5324 | _mesa_glsl_error(& loc, state, |
| 5325 | "invalid type in declaration of `%s'", |
| 5326 | this->identifier); |
Ian Romanick | a87ac25 | 2010-02-22 13:19:34 -0800 | [diff] [blame] | 5327 | } |
| 5328 | |
Ian Romanick | 0471e8b | 2010-03-26 14:33:41 -0700 | [diff] [blame] | 5329 | type = glsl_type::error_type; |
Ian Romanick | a87ac25 | 2010-02-22 13:19:34 -0800 | [diff] [blame] | 5330 | } |
| 5331 | |
Eric Anholt | 068c80c | 2010-03-31 09:56:36 -1000 | [diff] [blame] | 5332 | /* From page 62 (page 68 of the PDF) of the GLSL 1.50 spec: |
| 5333 | * |
| 5334 | * "Functions that accept no input arguments need not use void in the |
| 5335 | * argument list because prototypes (or definitions) are required and |
| 5336 | * therefore there is no ambiguity when an empty argument list "( )" is |
| 5337 | * declared. The idiom "(void)" as a parameter list is provided for |
| 5338 | * convenience." |
| 5339 | * |
| 5340 | * Placing this check here prevents a void parameter being set up |
| 5341 | * for a function, which avoids tripping up checks for main taking |
| 5342 | * parameters and lookups of an unnamed symbol. |
| 5343 | */ |
Ian Romanick | cf37c9e | 2010-04-02 15:30:45 -0700 | [diff] [blame] | 5344 | if (type->is_void()) { |
| 5345 | if (this->identifier != NULL) |
Iago Toral Quiroga | 4472ab9 | 2014-04-14 09:14:23 +0200 | [diff] [blame] | 5346 | _mesa_glsl_error(& loc, state, |
| 5347 | "named parameter cannot have type `void'"); |
Ian Romanick | cf37c9e | 2010-04-02 15:30:45 -0700 | [diff] [blame] | 5348 | |
| 5349 | is_void = true; |
Eric Anholt | 068c80c | 2010-03-31 09:56:36 -1000 | [diff] [blame] | 5350 | return NULL; |
Ian Romanick | cf37c9e | 2010-04-02 15:30:45 -0700 | [diff] [blame] | 5351 | } |
Eric Anholt | 068c80c | 2010-03-31 09:56:36 -1000 | [diff] [blame] | 5352 | |
Ian Romanick | 45d8a70 | 2010-04-02 15:09:33 -0700 | [diff] [blame] | 5353 | if (formal_parameter && (this->identifier == NULL)) { |
| 5354 | _mesa_glsl_error(& loc, state, "formal parameter lacks a name"); |
| 5355 | return NULL; |
| 5356 | } |
| 5357 | |
Kenneth Graunke | e511a35 | 2010-08-21 15:30:34 -0700 | [diff] [blame] | 5358 | /* This only handles "vec4 foo[..]". The earlier specifier->glsl_type(...) |
| 5359 | * call already handled the "vec4[..] foo" case. |
| 5360 | */ |
Timothy Arceri | bfb4875 | 2014-01-23 23:16:41 +1100 | [diff] [blame] | 5361 | type = process_array_type(&loc, type, this->array_specifier, state); |
Kenneth Graunke | e511a35 | 2010-08-21 15:30:34 -0700 | [diff] [blame] | 5362 | |
Timothy Arceri | b59c592 | 2013-10-23 21:31:27 +1100 | [diff] [blame] | 5363 | if (!type->is_error() && type->is_unsized_array()) { |
Kenneth Graunke | e511a35 | 2010-08-21 15:30:34 -0700 | [diff] [blame] | 5364 | _mesa_glsl_error(&loc, state, "arrays passed as parameters must have " |
Iago Toral Quiroga | 4472ab9 | 2014-04-14 09:14:23 +0200 | [diff] [blame] | 5365 | "a declared size"); |
Kenneth Graunke | e511a35 | 2010-08-21 15:30:34 -0700 | [diff] [blame] | 5366 | type = glsl_type::error_type; |
| 5367 | } |
| 5368 | |
Ian Romanick | cf37c9e | 2010-04-02 15:30:45 -0700 | [diff] [blame] | 5369 | is_void = false; |
Paul Berry | 42a29d8 | 2013-01-11 14:39:32 -0800 | [diff] [blame] | 5370 | ir_variable *var = new(ctx) |
| 5371 | ir_variable(type, this->identifier, ir_var_function_in); |
Ian Romanick | a87ac25 | 2010-02-22 13:19:34 -0800 | [diff] [blame] | 5372 | |
Ian Romanick | cdb8d54 | 2010-03-11 14:48:51 -0800 | [diff] [blame] | 5373 | /* Apply any specified qualifiers to the parameter declaration. Note that |
| 5374 | * for function parameters the default mode is 'in'. |
| 5375 | */ |
Eric Anholt | f7561e8 | 2012-04-26 18:19:39 -0700 | [diff] [blame] | 5376 | apply_type_qualifier_to_variable(& this->type->qualifier, var, state, & loc, |
Iago Toral Quiroga | 4472ab9 | 2014-04-14 09:14:23 +0200 | [diff] [blame] | 5377 | true); |
Ian Romanick | a87ac25 | 2010-02-22 13:19:34 -0800 | [diff] [blame] | 5378 | |
Francisco Jerez | 7af167d | 2013-11-25 19:38:37 -0800 | [diff] [blame] | 5379 | /* From section 4.1.7 of the GLSL 4.40 spec: |
Paul Berry | f072210 | 2011-07-12 12:03:02 -0700 | [diff] [blame] | 5380 | * |
Francisco Jerez | 7af167d | 2013-11-25 19:38:37 -0800 | [diff] [blame] | 5381 | * "Opaque variables cannot be treated as l-values; hence cannot |
| 5382 | * be used as out or inout function parameters, nor can they be |
| 5383 | * assigned into." |
Paul Berry | f072210 | 2011-07-12 12:03:02 -0700 | [diff] [blame] | 5384 | */ |
Tapani Pälli | 33ee2c6 | 2013-12-12 13:51:01 +0200 | [diff] [blame] | 5385 | if ((var->data.mode == ir_var_function_inout || var->data.mode == ir_var_function_out) |
Francisco Jerez | 7af167d | 2013-11-25 19:38:37 -0800 | [diff] [blame] | 5386 | && type->contains_opaque()) { |
| 5387 | _mesa_glsl_error(&loc, state, "out and inout parameters cannot " |
| 5388 | "contain opaque variables"); |
Paul Berry | f072210 | 2011-07-12 12:03:02 -0700 | [diff] [blame] | 5389 | type = glsl_type::error_type; |
| 5390 | } |
| 5391 | |
Paul Berry | 00792e3 | 2011-09-10 07:48:46 -0700 | [diff] [blame] | 5392 | /* From page 39 (page 45 of the PDF) of the GLSL 1.10 spec: |
| 5393 | * |
| 5394 | * "When calling a function, expressions that do not evaluate to |
| 5395 | * l-values cannot be passed to parameters declared as out or inout." |
| 5396 | * |
| 5397 | * From page 32 (page 38 of the PDF) of the GLSL 1.10 spec: |
| 5398 | * |
| 5399 | * "Other binary or unary expressions, non-dereferenced arrays, |
| 5400 | * function names, swizzles with repeated fields, and constants |
| 5401 | * cannot be l-values." |
| 5402 | * |
| 5403 | * So for GLSL 1.10, passing an array as an out or inout parameter is not |
| 5404 | * allowed. This restriction is removed in GLSL 1.20, and in GLSL ES. |
| 5405 | */ |
Tapani Pälli | 33ee2c6 | 2013-12-12 13:51:01 +0200 | [diff] [blame] | 5406 | if ((var->data.mode == ir_var_function_inout || var->data.mode == ir_var_function_out) |
Paul Berry | 0d9bba6 | 2012-08-05 09:57:01 -0700 | [diff] [blame] | 5407 | && type->is_array() |
| 5408 | && !state->check_version(120, 100, &loc, |
Paul Berry | 4d7899f | 2013-07-25 19:56:43 -0700 | [diff] [blame] | 5409 | "arrays cannot be out or inout parameters")) { |
Paul Berry | 00792e3 | 2011-09-10 07:48:46 -0700 | [diff] [blame] | 5410 | type = glsl_type::error_type; |
| 5411 | } |
| 5412 | |
Ian Romanick | 0044e7e | 2010-03-08 23:44:00 -0800 | [diff] [blame] | 5413 | instructions->push_tail(var); |
Ian Romanick | a87ac25 | 2010-02-22 13:19:34 -0800 | [diff] [blame] | 5414 | |
| 5415 | /* Parameter declarations do not have r-values. |
| 5416 | */ |
| 5417 | return NULL; |
| 5418 | } |
| 5419 | |
| 5420 | |
Ian Romanick | 45d8a70 | 2010-04-02 15:09:33 -0700 | [diff] [blame] | 5421 | void |
Ian Romanick | 304ea90 | 2010-05-10 11:17:53 -0700 | [diff] [blame] | 5422 | ast_parameter_declarator::parameters_to_hir(exec_list *ast_parameters, |
Iago Toral Quiroga | 4472ab9 | 2014-04-14 09:14:23 +0200 | [diff] [blame] | 5423 | bool formal, |
| 5424 | exec_list *ir_parameters, |
| 5425 | _mesa_glsl_parse_state *state) |
Ian Romanick | a87ac25 | 2010-02-22 13:19:34 -0800 | [diff] [blame] | 5426 | { |
Ian Romanick | cf37c9e | 2010-04-02 15:30:45 -0700 | [diff] [blame] | 5427 | ast_parameter_declarator *void_param = NULL; |
| 5428 | unsigned count = 0; |
Ian Romanick | a87ac25 | 2010-02-22 13:19:34 -0800 | [diff] [blame] | 5429 | |
Ian Romanick | 2b97dc6 | 2010-05-10 17:42:05 -0700 | [diff] [blame] | 5430 | foreach_list_typed (ast_parameter_declarator, param, link, ast_parameters) { |
Ian Romanick | 45d8a70 | 2010-04-02 15:09:33 -0700 | [diff] [blame] | 5431 | param->formal_parameter = formal; |
Eric Anholt | 068c80c | 2010-03-31 09:56:36 -1000 | [diff] [blame] | 5432 | param->hir(ir_parameters, state); |
Ian Romanick | cf37c9e | 2010-04-02 15:30:45 -0700 | [diff] [blame] | 5433 | |
| 5434 | if (param->is_void) |
Iago Toral Quiroga | 4472ab9 | 2014-04-14 09:14:23 +0200 | [diff] [blame] | 5435 | void_param = param; |
Ian Romanick | cf37c9e | 2010-04-02 15:30:45 -0700 | [diff] [blame] | 5436 | |
| 5437 | count++; |
| 5438 | } |
| 5439 | |
| 5440 | if ((void_param != NULL) && (count > 1)) { |
| 5441 | YYLTYPE loc = void_param->get_location(); |
| 5442 | |
| 5443 | _mesa_glsl_error(& loc, state, |
Iago Toral Quiroga | 4472ab9 | 2014-04-14 09:14:23 +0200 | [diff] [blame] | 5444 | "`void' parameter must be only parameter"); |
Ian Romanick | a87ac25 | 2010-02-22 13:19:34 -0800 | [diff] [blame] | 5445 | } |
| 5446 | } |
| 5447 | |
| 5448 | |
Kenneth Graunke | 6fae1e4 | 2010-12-06 10:54:05 -0800 | [diff] [blame] | 5449 | void |
Paul Berry | 0d81b0e | 2011-07-29 15:28:52 -0700 | [diff] [blame] | 5450 | emit_function(_mesa_glsl_parse_state *state, ir_function *f) |
Kenneth Graunke | 6fae1e4 | 2010-12-06 10:54:05 -0800 | [diff] [blame] | 5451 | { |
Paul Berry | 0d81b0e | 2011-07-29 15:28:52 -0700 | [diff] [blame] | 5452 | /* IR invariants disallow function declarations or definitions |
| 5453 | * nested within other function definitions. But there is no |
| 5454 | * requirement about the relative order of function declarations |
| 5455 | * and definitions with respect to one another. So simply insert |
| 5456 | * the new ir_function block at the end of the toplevel instruction |
| 5457 | * list. |
| 5458 | */ |
| 5459 | state->toplevel_ir->push_tail(f); |
Kenneth Graunke | 6fae1e4 | 2010-12-06 10:54:05 -0800 | [diff] [blame] | 5460 | } |
| 5461 | |
| 5462 | |
Kenneth Graunke | fb9fb5f | 2010-03-26 00:25:36 -0700 | [diff] [blame] | 5463 | ir_rvalue * |
Ian Romanick | 92318a9 | 2010-03-31 18:23:21 -0700 | [diff] [blame] | 5464 | ast_function::hir(exec_list *instructions, |
Iago Toral Quiroga | 4472ab9 | 2014-04-14 09:14:23 +0200 | [diff] [blame] | 5465 | struct _mesa_glsl_parse_state *state) |
Ian Romanick | a87ac25 | 2010-02-22 13:19:34 -0800 | [diff] [blame] | 5466 | { |
Kenneth Graunke | 953ff12 | 2010-06-25 13:14:37 -0700 | [diff] [blame] | 5467 | void *ctx = state; |
Ian Romanick | 18238de | 2010-03-01 13:49:10 -0800 | [diff] [blame] | 5468 | ir_function *f = NULL; |
Ian Romanick | 92318a9 | 2010-03-31 18:23:21 -0700 | [diff] [blame] | 5469 | ir_function_signature *sig = NULL; |
| 5470 | exec_list hir_parameters; |
Dave Airlie | 65ac360 | 2015-06-01 10:55:47 +1000 | [diff] [blame] | 5471 | YYLTYPE loc = this->get_location(); |
Ian Romanick | a87ac25 | 2010-02-22 13:19:34 -0800 | [diff] [blame] | 5472 | |
Kenneth Graunke | ac04c25 | 2010-06-29 00:48:10 -0700 | [diff] [blame] | 5473 | const char *const name = identifier; |
Ian Romanick | a87ac25 | 2010-02-22 13:19:34 -0800 | [diff] [blame] | 5474 | |
Ian Romanick | 9a3bd5e | 2011-08-29 14:56:29 -0700 | [diff] [blame] | 5475 | /* New functions are always added to the top-level IR instruction stream, |
| 5476 | * so this instruction list pointer is ignored. See also emit_function |
| 5477 | * (called below). |
| 5478 | */ |
| 5479 | (void) instructions; |
| 5480 | |
Ian Romanick | 63b80f8 | 2010-09-01 06:34:58 -0700 | [diff] [blame] | 5481 | /* From page 21 (page 27 of the PDF) of the GLSL 1.20 spec, |
| 5482 | * |
| 5483 | * "Function declarations (prototypes) cannot occur inside of functions; |
| 5484 | * they must be at global scope, or for the built-in functions, outside |
| 5485 | * the global scope." |
| 5486 | * |
| 5487 | * From page 27 (page 33 of the PDF) of the GLSL ES 1.00.16 spec, |
| 5488 | * |
| 5489 | * "User defined functions may only be defined within the global scope." |
| 5490 | * |
| 5491 | * Note that this language does not appear in GLSL 1.10. |
| 5492 | */ |
Paul Berry | e3ded7f | 2012-08-01 14:50:05 -0700 | [diff] [blame] | 5493 | if ((state->current_function != NULL) && |
| 5494 | state->is_version(120, 100)) { |
Ian Romanick | 63b80f8 | 2010-09-01 06:34:58 -0700 | [diff] [blame] | 5495 | YYLTYPE loc = this->get_location(); |
| 5496 | _mesa_glsl_error(&loc, state, |
Timothy Arceri | 222f66a | 2016-09-28 16:04:08 +1000 | [diff] [blame] | 5497 | "declaration of function `%s' not allowed within " |
| 5498 | "function body", name); |
Ian Romanick | 63b80f8 | 2010-09-01 06:34:58 -0700 | [diff] [blame] | 5499 | } |
| 5500 | |
Paul Berry | 1838df9 | 2013-09-27 17:18:14 -0700 | [diff] [blame] | 5501 | validate_identifier(name, this->get_location(), state); |
Kenneth Graunke | edd180f | 2010-08-20 02:14:35 -0700 | [diff] [blame] | 5502 | |
Ian Romanick | a87ac25 | 2010-02-22 13:19:34 -0800 | [diff] [blame] | 5503 | /* Convert the list of function parameters to HIR now so that they can be |
| 5504 | * used below to compare this function's signature with previously seen |
| 5505 | * signatures for functions with the same name. |
| 5506 | */ |
Ian Romanick | 45d8a70 | 2010-04-02 15:09:33 -0700 | [diff] [blame] | 5507 | ast_parameter_declarator::parameters_to_hir(& this->parameters, |
Iago Toral Quiroga | 4472ab9 | 2014-04-14 09:14:23 +0200 | [diff] [blame] | 5508 | is_definition, |
| 5509 | & hir_parameters, state); |
Ian Romanick | a87ac25 | 2010-02-22 13:19:34 -0800 | [diff] [blame] | 5510 | |
Ian Romanick | e39cc69 | 2010-03-23 12:19:13 -0700 | [diff] [blame] | 5511 | const char *return_type_name; |
| 5512 | const glsl_type *return_type = |
Ian Romanick | cabd457 | 2013-08-09 15:17:18 -0700 | [diff] [blame] | 5513 | this->return_type->glsl_type(& return_type_name, state); |
Ian Romanick | e39cc69 | 2010-03-23 12:19:13 -0700 | [diff] [blame] | 5514 | |
Eric Anholt | 76e96d7 | 2010-08-23 13:26:52 -0700 | [diff] [blame] | 5515 | if (!return_type) { |
| 5516 | YYLTYPE loc = this->get_location(); |
| 5517 | _mesa_glsl_error(&loc, state, |
Iago Toral Quiroga | 4472ab9 | 2014-04-14 09:14:23 +0200 | [diff] [blame] | 5518 | "function `%s' has undeclared return type `%s'", |
| 5519 | name, return_type_name); |
Eric Anholt | 76e96d7 | 2010-08-23 13:26:52 -0700 | [diff] [blame] | 5520 | return_type = glsl_type::error_type; |
| 5521 | } |
Ian Romanick | e39cc69 | 2010-03-23 12:19:13 -0700 | [diff] [blame] | 5522 | |
Dave Airlie | 65ac360 | 2015-06-01 10:55:47 +1000 | [diff] [blame] | 5523 | /* ARB_shader_subroutine states: |
| 5524 | * "Subroutine declarations cannot be prototyped. It is an error to prepend |
| 5525 | * subroutine(...) to a function declaration." |
| 5526 | */ |
| 5527 | if (this->return_type->qualifier.flags.q.subroutine_def && !is_definition) { |
| 5528 | YYLTYPE loc = this->get_location(); |
| 5529 | _mesa_glsl_error(&loc, state, |
| 5530 | "function declaration `%s' cannot have subroutine prepended", |
| 5531 | name); |
| 5532 | } |
| 5533 | |
Kenneth Graunke | ac04c25 | 2010-06-29 00:48:10 -0700 | [diff] [blame] | 5534 | /* From page 56 (page 62 of the PDF) of the GLSL 1.30 spec: |
| 5535 | * "No qualifier is allowed on the return type of a function." |
| 5536 | */ |
Timothy Arceri | f7af69c | 2015-11-09 09:34:40 +1100 | [diff] [blame] | 5537 | if (this->return_type->has_qualifiers(state)) { |
Kenneth Graunke | ac04c25 | 2010-06-29 00:48:10 -0700 | [diff] [blame] | 5538 | YYLTYPE loc = this->get_location(); |
| 5539 | _mesa_glsl_error(& loc, state, |
Iago Toral Quiroga | 4472ab9 | 2014-04-14 09:14:23 +0200 | [diff] [blame] | 5540 | "function `%s' return type has qualifiers", name); |
Kenneth Graunke | ac04c25 | 2010-06-29 00:48:10 -0700 | [diff] [blame] | 5541 | } |
| 5542 | |
Ian Romanick | 1b35e33 | 2013-08-08 17:40:38 -0700 | [diff] [blame] | 5543 | /* Section 6.1 (Function Definitions) of the GLSL 1.20 spec says: |
| 5544 | * |
| 5545 | * "Arrays are allowed as arguments and as the return type. In both |
| 5546 | * cases, the array must be explicitly sized." |
| 5547 | */ |
Timothy Arceri | b59c592 | 2013-10-23 21:31:27 +1100 | [diff] [blame] | 5548 | if (return_type->is_unsized_array()) { |
Ian Romanick | 1b35e33 | 2013-08-08 17:40:38 -0700 | [diff] [blame] | 5549 | YYLTYPE loc = this->get_location(); |
| 5550 | _mesa_glsl_error(& loc, state, |
Iago Toral Quiroga | 4472ab9 | 2014-04-14 09:14:23 +0200 | [diff] [blame] | 5551 | "function `%s' return type array must be explicitly " |
| 5552 | "sized", name); |
Ian Romanick | 1b35e33 | 2013-08-08 17:40:38 -0700 | [diff] [blame] | 5553 | } |
| 5554 | |
Francisco Jerez | 7af167d | 2013-11-25 19:38:37 -0800 | [diff] [blame] | 5555 | /* From section 4.1.7 of the GLSL 4.40 spec: |
Paul Berry | f072210 | 2011-07-12 12:03:02 -0700 | [diff] [blame] | 5556 | * |
Francisco Jerez | 7af167d | 2013-11-25 19:38:37 -0800 | [diff] [blame] | 5557 | * "[Opaque types] can only be declared as function parameters |
| 5558 | * or uniform-qualified variables." |
Paul Berry | f072210 | 2011-07-12 12:03:02 -0700 | [diff] [blame] | 5559 | */ |
Francisco Jerez | 7af167d | 2013-11-25 19:38:37 -0800 | [diff] [blame] | 5560 | if (return_type->contains_opaque()) { |
Paul Berry | f072210 | 2011-07-12 12:03:02 -0700 | [diff] [blame] | 5561 | YYLTYPE loc = this->get_location(); |
| 5562 | _mesa_glsl_error(&loc, state, |
Francisco Jerez | 7af167d | 2013-11-25 19:38:37 -0800 | [diff] [blame] | 5563 | "function `%s' return type can't contain an opaque type", |
Paul Berry | f072210 | 2011-07-12 12:03:02 -0700 | [diff] [blame] | 5564 | name); |
| 5565 | } |
| 5566 | |
Dave Airlie | 6effdce | 2016-05-23 12:14:01 +1000 | [diff] [blame] | 5567 | /**/ |
| 5568 | if (return_type->is_subroutine()) { |
| 5569 | YYLTYPE loc = this->get_location(); |
| 5570 | _mesa_glsl_error(&loc, state, |
| 5571 | "function `%s' return type can't be a subroutine type", |
| 5572 | name); |
| 5573 | } |
| 5574 | |
| 5575 | |
Kenneth Graunke | 3d05177 | 2014-07-24 14:05:41 -0700 | [diff] [blame] | 5576 | /* Create an ir_function if one doesn't already exist. */ |
| 5577 | f = state->symbols->get_function(name); |
| 5578 | if (f == NULL) { |
| 5579 | f = new(ctx) ir_function(name); |
Dave Airlie | 65ac360 | 2015-06-01 10:55:47 +1000 | [diff] [blame] | 5580 | if (!this->return_type->qualifier.flags.q.subroutine) { |
| 5581 | if (!state->symbols->add_function(f)) { |
| 5582 | /* This function name shadows a non-function use of the same name. */ |
| 5583 | YYLTYPE loc = this->get_location(); |
| 5584 | _mesa_glsl_error(&loc, state, "function name `%s' conflicts with " |
| 5585 | "non-function", name); |
| 5586 | return NULL; |
| 5587 | } |
Kenneth Graunke | 3d05177 | 2014-07-24 14:05:41 -0700 | [diff] [blame] | 5588 | } |
Kenneth Graunke | 3d05177 | 2014-07-24 14:05:41 -0700 | [diff] [blame] | 5589 | emit_function(state, f); |
| 5590 | } |
| 5591 | |
Samuel Iglesias Gonsalvez | 187ace7 | 2014-11-25 16:36:53 +0100 | [diff] [blame] | 5592 | /* From GLSL ES 3.0 spec, chapter 6.1 "Function Definitions", page 71: |
| 5593 | * |
| 5594 | * "A shader cannot redefine or overload built-in functions." |
| 5595 | * |
| 5596 | * While in GLSL ES 1.0 specification, chapter 8 "Built-in Functions": |
| 5597 | * |
| 5598 | * "User code can overload the built-in functions but cannot redefine |
| 5599 | * them." |
| 5600 | */ |
| 5601 | if (state->es_shader && state->language_version >= 300) { |
| 5602 | /* Local shader has no exact candidates; check the built-ins. */ |
| 5603 | _mesa_glsl_initialize_builtin_functions(); |
Ian Romanick | bd0245b | 2015-08-26 13:38:49 +0100 | [diff] [blame] | 5604 | if (_mesa_glsl_find_builtin_function_by_name(name)) { |
Samuel Iglesias Gonsalvez | 187ace7 | 2014-11-25 16:36:53 +0100 | [diff] [blame] | 5605 | YYLTYPE loc = this->get_location(); |
| 5606 | _mesa_glsl_error(& loc, state, |
| 5607 | "A shader cannot redefine or overload built-in " |
| 5608 | "function `%s' in GLSL ES 3.00", name); |
| 5609 | return NULL; |
| 5610 | } |
| 5611 | } |
| 5612 | |
Ian Romanick | a87ac25 | 2010-02-22 13:19:34 -0800 | [diff] [blame] | 5613 | /* Verify that this function's signature either doesn't match a previously |
| 5614 | * seen signature for a function with the same name, or, if a match is found, |
| 5615 | * that the previously seen signature does not have an associated definition. |
| 5616 | */ |
Kenneth Graunke | 3d05177 | 2014-07-24 14:05:41 -0700 | [diff] [blame] | 5617 | if (state->es_shader || f->has_user_signature()) { |
Kenneth Graunke | 3e820e3 | 2013-08-30 23:11:55 -0700 | [diff] [blame] | 5618 | sig = f->exact_matching_signature(state, &hir_parameters); |
Kenneth Graunke | 0d605cb | 2010-04-28 12:04:23 -0700 | [diff] [blame] | 5619 | if (sig != NULL) { |
Iago Toral Quiroga | 4472ab9 | 2014-04-14 09:14:23 +0200 | [diff] [blame] | 5620 | const char *badvar = sig->qualifiers_match(&hir_parameters); |
| 5621 | if (badvar != NULL) { |
| 5622 | YYLTYPE loc = this->get_location(); |
Ian Romanick | a87ac25 | 2010-02-22 13:19:34 -0800 | [diff] [blame] | 5623 | |
Iago Toral Quiroga | 4472ab9 | 2014-04-14 09:14:23 +0200 | [diff] [blame] | 5624 | _mesa_glsl_error(&loc, state, "function `%s' parameter `%s' " |
| 5625 | "qualifiers don't match prototype", name, badvar); |
| 5626 | } |
Ian Romanick | a87ac25 | 2010-02-22 13:19:34 -0800 | [diff] [blame] | 5627 | |
Iago Toral Quiroga | 4472ab9 | 2014-04-14 09:14:23 +0200 | [diff] [blame] | 5628 | if (sig->return_type != return_type) { |
| 5629 | YYLTYPE loc = this->get_location(); |
Ian Romanick | a87ac25 | 2010-02-22 13:19:34 -0800 | [diff] [blame] | 5630 | |
Iago Toral Quiroga | 4472ab9 | 2014-04-14 09:14:23 +0200 | [diff] [blame] | 5631 | _mesa_glsl_error(&loc, state, "function `%s' return type doesn't " |
| 5632 | "match prototype", name); |
| 5633 | } |
Kenneth Graunke | 0d605cb | 2010-04-28 12:04:23 -0700 | [diff] [blame] | 5634 | |
Kenneth Graunke | 6c5cf8b | 2013-04-30 00:58:09 -0700 | [diff] [blame] | 5635 | if (sig->is_defined) { |
| 5636 | if (is_definition) { |
| 5637 | YYLTYPE loc = this->get_location(); |
| 5638 | _mesa_glsl_error(& loc, state, "function `%s' redefined", name); |
| 5639 | } else { |
| 5640 | /* We just encountered a prototype that exactly matches a |
| 5641 | * function that's already been defined. This is redundant, |
| 5642 | * and we should ignore it. |
| 5643 | */ |
| 5644 | return NULL; |
| 5645 | } |
Iago Toral Quiroga | 4472ab9 | 2014-04-14 09:14:23 +0200 | [diff] [blame] | 5646 | } |
Kenneth Graunke | 0d605cb | 2010-04-28 12:04:23 -0700 | [diff] [blame] | 5647 | } |
Ian Romanick | a87ac25 | 2010-02-22 13:19:34 -0800 | [diff] [blame] | 5648 | } |
| 5649 | |
Eric Anholt | ab372da | 2010-03-28 01:24:55 -0700 | [diff] [blame] | 5650 | /* Verify the return type of main() */ |
| 5651 | if (strcmp(name, "main") == 0) { |
Ian Romanick | 25711a8 | 2010-03-31 17:39:10 -0700 | [diff] [blame] | 5652 | if (! return_type->is_void()) { |
Iago Toral Quiroga | 4472ab9 | 2014-04-14 09:14:23 +0200 | [diff] [blame] | 5653 | YYLTYPE loc = this->get_location(); |
Eric Anholt | ab372da | 2010-03-28 01:24:55 -0700 | [diff] [blame] | 5654 | |
Iago Toral Quiroga | 4472ab9 | 2014-04-14 09:14:23 +0200 | [diff] [blame] | 5655 | _mesa_glsl_error(& loc, state, "main() must return void"); |
Eric Anholt | ab372da | 2010-03-28 01:24:55 -0700 | [diff] [blame] | 5656 | } |
Eric Anholt | 174cc03 | 2010-03-30 23:37:51 -1000 | [diff] [blame] | 5657 | |
Ian Romanick | 92318a9 | 2010-03-31 18:23:21 -0700 | [diff] [blame] | 5658 | if (!hir_parameters.is_empty()) { |
Iago Toral Quiroga | 4472ab9 | 2014-04-14 09:14:23 +0200 | [diff] [blame] | 5659 | YYLTYPE loc = this->get_location(); |
Eric Anholt | 174cc03 | 2010-03-30 23:37:51 -1000 | [diff] [blame] | 5660 | |
Iago Toral Quiroga | 4472ab9 | 2014-04-14 09:14:23 +0200 | [diff] [blame] | 5661 | _mesa_glsl_error(& loc, state, "main() must not take any parameters"); |
Eric Anholt | 174cc03 | 2010-03-30 23:37:51 -1000 | [diff] [blame] | 5662 | } |
Eric Anholt | ab372da | 2010-03-28 01:24:55 -0700 | [diff] [blame] | 5663 | } |
Ian Romanick | a87ac25 | 2010-02-22 13:19:34 -0800 | [diff] [blame] | 5664 | |
| 5665 | /* Finish storing the information about this new function in its signature. |
| 5666 | */ |
Ian Romanick | 92318a9 | 2010-03-31 18:23:21 -0700 | [diff] [blame] | 5667 | if (sig == NULL) { |
Carl Worth | 1660a29 | 2010-06-23 18:11:51 -0700 | [diff] [blame] | 5668 | sig = new(ctx) ir_function_signature(return_type); |
Ian Romanick | 92318a9 | 2010-03-31 18:23:21 -0700 | [diff] [blame] | 5669 | f->add_signature(sig); |
Ian Romanick | a87ac25 | 2010-02-22 13:19:34 -0800 | [diff] [blame] | 5670 | } |
| 5671 | |
Kenneth Graunke | bff6013 | 2010-04-28 12:44:24 -0700 | [diff] [blame] | 5672 | sig->replace_parameters(&hir_parameters); |
Ian Romanick | 92318a9 | 2010-03-31 18:23:21 -0700 | [diff] [blame] | 5673 | signature = sig; |
Ian Romanick | e29a585 | 2010-03-31 17:54:26 -0700 | [diff] [blame] | 5674 | |
Dave Airlie | 65ac360 | 2015-06-01 10:55:47 +1000 | [diff] [blame] | 5675 | if (this->return_type->qualifier.flags.q.subroutine_def) { |
| 5676 | int idx; |
| 5677 | |
Timothy Arceri | f7af69c | 2015-11-09 09:34:40 +1100 | [diff] [blame] | 5678 | if (this->return_type->qualifier.flags.q.explicit_index) { |
| 5679 | unsigned qual_index; |
| 5680 | if (process_qualifier_constant(state, &loc, "index", |
| 5681 | this->return_type->qualifier.index, |
| 5682 | &qual_index)) { |
| 5683 | if (!state->has_explicit_uniform_location()) { |
| 5684 | _mesa_glsl_error(&loc, state, "subroutine index requires " |
| 5685 | "GL_ARB_explicit_uniform_location or " |
| 5686 | "GLSL 4.30"); |
| 5687 | } else if (qual_index >= MAX_SUBROUTINES) { |
| 5688 | _mesa_glsl_error(&loc, state, |
| 5689 | "invalid subroutine index (%d) index must " |
| 5690 | "be a number between 0 and " |
| 5691 | "GL_MAX_SUBROUTINES - 1 (%d)", qual_index, |
| 5692 | MAX_SUBROUTINES - 1); |
| 5693 | } else { |
| 5694 | f->subroutine_index = qual_index; |
| 5695 | } |
| 5696 | } |
| 5697 | } |
| 5698 | |
Dave Airlie | 65ac360 | 2015-06-01 10:55:47 +1000 | [diff] [blame] | 5699 | f->num_subroutine_types = this->return_type->qualifier.subroutine_list->declarations.length(); |
| 5700 | f->subroutine_types = ralloc_array(state, const struct glsl_type *, |
| 5701 | f->num_subroutine_types); |
| 5702 | idx = 0; |
| 5703 | foreach_list_typed(ast_declaration, decl, link, &this->return_type->qualifier.subroutine_list->declarations) { |
| 5704 | const struct glsl_type *type; |
| 5705 | /* the subroutine type must be already declared */ |
| 5706 | type = state->symbols->get_type(decl->identifier); |
| 5707 | if (!type) { |
| 5708 | _mesa_glsl_error(& loc, state, "unknown type '%s' in subroutine function definition", decl->identifier); |
| 5709 | } |
Dave Airlie | b572b59 | 2016-05-06 11:28:40 +1000 | [diff] [blame] | 5710 | |
| 5711 | for (int i = 0; i < state->num_subroutine_types; i++) { |
| 5712 | ir_function *fn = state->subroutine_types[i]; |
| 5713 | ir_function_signature *tsig = NULL; |
| 5714 | |
| 5715 | if (strcmp(fn->name, decl->identifier)) |
| 5716 | continue; |
| 5717 | |
| 5718 | tsig = fn->matching_signature(state, &sig->parameters, |
| 5719 | false); |
| 5720 | if (!tsig) { |
| 5721 | _mesa_glsl_error(& loc, state, "subroutine type mismatch '%s' - signatures do not match\n", decl->identifier); |
| 5722 | } else { |
| 5723 | if (tsig->return_type != sig->return_type) { |
| 5724 | _mesa_glsl_error(& loc, state, "subroutine type mismatch '%s' - return types do not match\n", decl->identifier); |
| 5725 | } |
| 5726 | } |
| 5727 | } |
Dave Airlie | 65ac360 | 2015-06-01 10:55:47 +1000 | [diff] [blame] | 5728 | f->subroutine_types[idx++] = type; |
| 5729 | } |
| 5730 | state->subroutines = (ir_function **)reralloc(state, state->subroutines, |
| 5731 | ir_function *, |
| 5732 | state->num_subroutines + 1); |
| 5733 | state->subroutines[state->num_subroutines] = f; |
| 5734 | state->num_subroutines++; |
| 5735 | |
| 5736 | } |
| 5737 | |
| 5738 | if (this->return_type->qualifier.flags.q.subroutine) { |
| 5739 | if (!state->symbols->add_type(this->identifier, glsl_type::get_subroutine_instance(this->identifier))) { |
| 5740 | _mesa_glsl_error(& loc, state, "type '%s' previously defined", this->identifier); |
| 5741 | return NULL; |
| 5742 | } |
| 5743 | state->subroutine_types = (ir_function **)reralloc(state, state->subroutine_types, |
| 5744 | ir_function *, |
| 5745 | state->num_subroutine_types + 1); |
| 5746 | state->subroutine_types[state->num_subroutine_types] = f; |
| 5747 | state->num_subroutine_types++; |
| 5748 | |
| 5749 | f->is_subroutine = true; |
| 5750 | } |
| 5751 | |
Ian Romanick | 92318a9 | 2010-03-31 18:23:21 -0700 | [diff] [blame] | 5752 | /* Function declarations (prototypes) do not have r-values. |
| 5753 | */ |
| 5754 | return NULL; |
| 5755 | } |
| 5756 | |
| 5757 | |
| 5758 | ir_rvalue * |
| 5759 | ast_function_definition::hir(exec_list *instructions, |
Iago Toral Quiroga | 4472ab9 | 2014-04-14 09:14:23 +0200 | [diff] [blame] | 5760 | struct _mesa_glsl_parse_state *state) |
Ian Romanick | 92318a9 | 2010-03-31 18:23:21 -0700 | [diff] [blame] | 5761 | { |
| 5762 | prototype->is_definition = true; |
| 5763 | prototype->hir(instructions, state); |
| 5764 | |
| 5765 | ir_function_signature *signature = prototype->signature; |
Kenneth Graunke | 826a39c | 2010-08-20 02:04:52 -0700 | [diff] [blame] | 5766 | if (signature == NULL) |
| 5767 | return NULL; |
Ian Romanick | a87ac25 | 2010-02-22 13:19:34 -0800 | [diff] [blame] | 5768 | |
Ian Romanick | 41ec6a4 | 2010-03-19 17:08:05 -0700 | [diff] [blame] | 5769 | assert(state->current_function == NULL); |
| 5770 | state->current_function = signature; |
Kenneth Graunke | 6de8256 | 2010-06-29 09:59:40 -0700 | [diff] [blame] | 5771 | state->found_return = false; |
Ian Romanick | 41ec6a4 | 2010-03-19 17:08:05 -0700 | [diff] [blame] | 5772 | |
Ian Romanick | e29a585 | 2010-03-31 17:54:26 -0700 | [diff] [blame] | 5773 | /* Duplicate parameters declared in the prototype as concrete variables. |
| 5774 | * Add these to the symbol table. |
Ian Romanick | a87ac25 | 2010-02-22 13:19:34 -0800 | [diff] [blame] | 5775 | */ |
Ian Romanick | 8bde4ce | 2010-03-19 11:57:24 -0700 | [diff] [blame] | 5776 | state->symbols->push_scope(); |
Matt Turner | 4d78446 | 2014-06-24 21:34:05 -0700 | [diff] [blame] | 5777 | foreach_in_list(ir_variable, var, &signature->parameters) { |
| 5778 | assert(var->as_variable() != NULL); |
Ian Romanick | a87ac25 | 2010-02-22 13:19:34 -0800 | [diff] [blame] | 5779 | |
Ian Romanick | 3359e58 | 2010-03-19 15:38:52 -0700 | [diff] [blame] | 5780 | /* The only way a parameter would "exist" is if two parameters have |
| 5781 | * the same name. |
| 5782 | */ |
| 5783 | if (state->symbols->name_declared_this_scope(var->name)) { |
Iago Toral Quiroga | 4472ab9 | 2014-04-14 09:14:23 +0200 | [diff] [blame] | 5784 | YYLTYPE loc = this->get_location(); |
Ian Romanick | 3359e58 | 2010-03-19 15:38:52 -0700 | [diff] [blame] | 5785 | |
Iago Toral Quiroga | 4472ab9 | 2014-04-14 09:14:23 +0200 | [diff] [blame] | 5786 | _mesa_glsl_error(& loc, state, "parameter `%s' redeclared", var->name); |
Ian Romanick | 3359e58 | 2010-03-19 15:38:52 -0700 | [diff] [blame] | 5787 | } else { |
Iago Toral Quiroga | 4472ab9 | 2014-04-14 09:14:23 +0200 | [diff] [blame] | 5788 | state->symbols->add_variable(var); |
Ian Romanick | 3359e58 | 2010-03-19 15:38:52 -0700 | [diff] [blame] | 5789 | } |
Ian Romanick | a87ac25 | 2010-02-22 13:19:34 -0800 | [diff] [blame] | 5790 | } |
| 5791 | |
Kenneth Graunke | 9fa99f3 | 2010-04-21 12:30:22 -0700 | [diff] [blame] | 5792 | /* Convert the body of the function to HIR. */ |
Eric Anholt | 894ea97 | 2010-04-07 13:19:11 -0700 | [diff] [blame] | 5793 | this->body->hir(&signature->body, state); |
Kenneth Graunke | 9fa99f3 | 2010-04-21 12:30:22 -0700 | [diff] [blame] | 5794 | signature->is_defined = true; |
Ian Romanick | a87ac25 | 2010-02-22 13:19:34 -0800 | [diff] [blame] | 5795 | |
Ian Romanick | 8bde4ce | 2010-03-19 11:57:24 -0700 | [diff] [blame] | 5796 | state->symbols->pop_scope(); |
Ian Romanick | a87ac25 | 2010-02-22 13:19:34 -0800 | [diff] [blame] | 5797 | |
Ian Romanick | 41ec6a4 | 2010-03-19 17:08:05 -0700 | [diff] [blame] | 5798 | assert(state->current_function == signature); |
| 5799 | state->current_function = NULL; |
Ian Romanick | a87ac25 | 2010-02-22 13:19:34 -0800 | [diff] [blame] | 5800 | |
Kenneth Graunke | 6de8256 | 2010-06-29 09:59:40 -0700 | [diff] [blame] | 5801 | if (!signature->return_type->is_void() && !state->found_return) { |
| 5802 | YYLTYPE loc = this->get_location(); |
| 5803 | _mesa_glsl_error(& loc, state, "function `%s' has non-void return type " |
Iago Toral Quiroga | 4472ab9 | 2014-04-14 09:14:23 +0200 | [diff] [blame] | 5804 | "%s, but no return statement", |
| 5805 | signature->function_name(), |
| 5806 | signature->return_type->name); |
Kenneth Graunke | 6de8256 | 2010-06-29 09:59:40 -0700 | [diff] [blame] | 5807 | } |
| 5808 | |
Ian Romanick | a87ac25 | 2010-02-22 13:19:34 -0800 | [diff] [blame] | 5809 | /* Function definitions do not have r-values. |
| 5810 | */ |
| 5811 | return NULL; |
| 5812 | } |
Ian Romanick | 16a246c | 2010-03-19 16:45:19 -0700 | [diff] [blame] | 5813 | |
| 5814 | |
Kenneth Graunke | fb9fb5f | 2010-03-26 00:25:36 -0700 | [diff] [blame] | 5815 | ir_rvalue * |
Ian Romanick | 16a246c | 2010-03-19 16:45:19 -0700 | [diff] [blame] | 5816 | ast_jump_statement::hir(exec_list *instructions, |
Iago Toral Quiroga | 4472ab9 | 2014-04-14 09:14:23 +0200 | [diff] [blame] | 5817 | struct _mesa_glsl_parse_state *state) |
Ian Romanick | 16a246c | 2010-03-19 16:45:19 -0700 | [diff] [blame] | 5818 | { |
Kenneth Graunke | 953ff12 | 2010-06-25 13:14:37 -0700 | [diff] [blame] | 5819 | void *ctx = state; |
Ian Romanick | 16a246c | 2010-03-19 16:45:19 -0700 | [diff] [blame] | 5820 | |
Ian Romanick | c0e76d8 | 2010-04-05 16:53:19 -0700 | [diff] [blame] | 5821 | switch (mode) { |
| 5822 | case ast_return: { |
Ian Romanick | 16a246c | 2010-03-19 16:45:19 -0700 | [diff] [blame] | 5823 | ir_return *inst; |
Eric Anholt | aad7c77 | 2010-03-30 23:28:20 -1000 | [diff] [blame] | 5824 | assert(state->current_function); |
Ian Romanick | 16a246c | 2010-03-19 16:45:19 -0700 | [diff] [blame] | 5825 | |
| 5826 | if (opt_return_value) { |
Iago Toral Quiroga | 4472ab9 | 2014-04-14 09:14:23 +0200 | [diff] [blame] | 5827 | ir_rvalue *ret = opt_return_value->hir(instructions, state); |
Ian Romanick | 2db46fe | 2011-01-22 17:47:05 -0800 | [diff] [blame] | 5828 | |
Iago Toral Quiroga | 4472ab9 | 2014-04-14 09:14:23 +0200 | [diff] [blame] | 5829 | /* The value of the return type can be NULL if the shader says |
| 5830 | * 'return foo();' and foo() is a function that returns void. |
| 5831 | * |
| 5832 | * NOTE: The GLSL spec doesn't say that this is an error. The type |
| 5833 | * of the return value is void. If the return type of the function is |
| 5834 | * also void, then this should compile without error. Seriously. |
| 5835 | */ |
| 5836 | const glsl_type *const ret_type = |
| 5837 | (ret == NULL) ? glsl_type::void_type : ret->type; |
Ian Romanick | 16a246c | 2010-03-19 16:45:19 -0700 | [diff] [blame] | 5838 | |
Matt Turner | 1a1b03e | 2013-05-22 12:14:32 -0700 | [diff] [blame] | 5839 | /* Implicit conversions are not allowed for return values prior to |
| 5840 | * ARB_shading_language_420pack. |
| 5841 | */ |
| 5842 | if (state->current_function->return_type != ret_type) { |
Iago Toral Quiroga | 4472ab9 | 2014-04-14 09:14:23 +0200 | [diff] [blame] | 5843 | YYLTYPE loc = this->get_location(); |
Kenneth Graunke | 18707eb | 2010-06-28 23:38:04 -0700 | [diff] [blame] | 5844 | |
Matt Turner | 79da722 | 2015-12-07 14:11:01 -0800 | [diff] [blame] | 5845 | if (state->has_420pack()) { |
Matt Turner | 1a1b03e | 2013-05-22 12:14:32 -0700 | [diff] [blame] | 5846 | if (!apply_implicit_conversion(state->current_function->return_type, |
| 5847 | ret, state)) { |
| 5848 | _mesa_glsl_error(& loc, state, |
Paul Berry | 4d7899f | 2013-07-25 19:56:43 -0700 | [diff] [blame] | 5849 | "could not implicitly convert return value " |
Matt Turner | 1a1b03e | 2013-05-22 12:14:32 -0700 | [diff] [blame] | 5850 | "to %s, in function `%s'", |
| 5851 | state->current_function->return_type->name, |
| 5852 | state->current_function->function_name()); |
| 5853 | } |
| 5854 | } else { |
| 5855 | _mesa_glsl_error(& loc, state, |
| 5856 | "`return' with wrong type %s, in function `%s' " |
| 5857 | "returning %s", |
| 5858 | ret_type->name, |
| 5859 | state->current_function->function_name(), |
| 5860 | state->current_function->return_type->name); |
| 5861 | } |
Matt Turner | fcaa48d | 2013-05-22 14:57:04 -0700 | [diff] [blame] | 5862 | } else if (state->current_function->return_type->base_type == |
| 5863 | GLSL_TYPE_VOID) { |
| 5864 | YYLTYPE loc = this->get_location(); |
| 5865 | |
| 5866 | /* The ARB_shading_language_420pack, GLSL ES 3.0, and GLSL 4.20 |
| 5867 | * specs add a clarification: |
| 5868 | * |
| 5869 | * "A void function can only use return without a return argument, even if |
| 5870 | * the return argument has void type. Return statements only accept values: |
| 5871 | * |
| 5872 | * void func1() { } |
| 5873 | * void func2() { return func1(); } // illegal return statement" |
| 5874 | */ |
| 5875 | _mesa_glsl_error(& loc, state, |
| 5876 | "void functions can only use `return' without a " |
| 5877 | "return argument"); |
| 5878 | } |
Ian Romanick | 16a246c | 2010-03-19 16:45:19 -0700 | [diff] [blame] | 5879 | |
Iago Toral Quiroga | 4472ab9 | 2014-04-14 09:14:23 +0200 | [diff] [blame] | 5880 | inst = new(ctx) ir_return(ret); |
Ian Romanick | 16a246c | 2010-03-19 16:45:19 -0700 | [diff] [blame] | 5881 | } else { |
Iago Toral Quiroga | 4472ab9 | 2014-04-14 09:14:23 +0200 | [diff] [blame] | 5882 | if (state->current_function->return_type->base_type != |
| 5883 | GLSL_TYPE_VOID) { |
| 5884 | YYLTYPE loc = this->get_location(); |
Eric Anholt | aad7c77 | 2010-03-30 23:28:20 -1000 | [diff] [blame] | 5885 | |
Iago Toral Quiroga | 4472ab9 | 2014-04-14 09:14:23 +0200 | [diff] [blame] | 5886 | _mesa_glsl_error(& loc, state, |
| 5887 | "`return' with no value, in function %s returning " |
| 5888 | "non-void", |
| 5889 | state->current_function->function_name()); |
| 5890 | } |
| 5891 | inst = new(ctx) ir_return; |
Ian Romanick | 16a246c | 2010-03-19 16:45:19 -0700 | [diff] [blame] | 5892 | } |
| 5893 | |
Kenneth Graunke | 6de8256 | 2010-06-29 09:59:40 -0700 | [diff] [blame] | 5894 | state->found_return = true; |
Ian Romanick | 16a246c | 2010-03-19 16:45:19 -0700 | [diff] [blame] | 5895 | instructions->push_tail(inst); |
Ian Romanick | c0e76d8 | 2010-04-05 16:53:19 -0700 | [diff] [blame] | 5896 | break; |
Ian Romanick | 16a246c | 2010-03-19 16:45:19 -0700 | [diff] [blame] | 5897 | } |
| 5898 | |
Ian Romanick | c0e76d8 | 2010-04-05 16:53:19 -0700 | [diff] [blame] | 5899 | case ast_discard: |
Paul Berry | 665b8d7 | 2014-01-07 10:11:39 -0800 | [diff] [blame] | 5900 | if (state->stage != MESA_SHADER_FRAGMENT) { |
Iago Toral Quiroga | 4472ab9 | 2014-04-14 09:14:23 +0200 | [diff] [blame] | 5901 | YYLTYPE loc = this->get_location(); |
Eric Anholt | b980207 | 2010-03-30 23:40:14 -1000 | [diff] [blame] | 5902 | |
Iago Toral Quiroga | 4472ab9 | 2014-04-14 09:14:23 +0200 | [diff] [blame] | 5903 | _mesa_glsl_error(& loc, state, |
| 5904 | "`discard' may only appear in a fragment shader"); |
Eric Anholt | b980207 | 2010-03-30 23:40:14 -1000 | [diff] [blame] | 5905 | } |
Kenneth Graunke | 77049a7 | 2010-06-30 14:11:00 -0700 | [diff] [blame] | 5906 | instructions->push_tail(new(ctx) ir_discard); |
Ian Romanick | c0e76d8 | 2010-04-05 16:53:19 -0700 | [diff] [blame] | 5907 | break; |
| 5908 | |
| 5909 | case ast_break: |
| 5910 | case ast_continue: |
Dan McCabe | 5c02e2e | 2011-11-07 16:17:58 -0800 | [diff] [blame] | 5911 | if (mode == ast_continue && |
Iago Toral Quiroga | 4472ab9 | 2014-04-14 09:14:23 +0200 | [diff] [blame] | 5912 | state->loop_nesting_ast == NULL) { |
| 5913 | YYLTYPE loc = this->get_location(); |
Ian Romanick | 4cf20cd | 2010-04-05 17:13:47 -0700 | [diff] [blame] | 5914 | |
Iago Toral Quiroga | 4472ab9 | 2014-04-14 09:14:23 +0200 | [diff] [blame] | 5915 | _mesa_glsl_error(& loc, state, "continue may only appear in a loop"); |
Dan McCabe | 5c02e2e | 2011-11-07 16:17:58 -0800 | [diff] [blame] | 5916 | } else if (mode == ast_break && |
Iago Toral Quiroga | 4472ab9 | 2014-04-14 09:14:23 +0200 | [diff] [blame] | 5917 | state->loop_nesting_ast == NULL && |
| 5918 | state->switch_state.switch_nesting_ast == NULL) { |
| 5919 | YYLTYPE loc = this->get_location(); |
Ian Romanick | 4cf20cd | 2010-04-05 17:13:47 -0700 | [diff] [blame] | 5920 | |
Iago Toral Quiroga | 4472ab9 | 2014-04-14 09:14:23 +0200 | [diff] [blame] | 5921 | _mesa_glsl_error(& loc, state, |
| 5922 | "break may only appear in a loop or a switch"); |
Dan McCabe | 5c02e2e | 2011-11-07 16:17:58 -0800 | [diff] [blame] | 5923 | } else { |
Iago Toral Quiroga | 4472ab9 | 2014-04-14 09:14:23 +0200 | [diff] [blame] | 5924 | /* For a loop, inline the for loop expression again, since we don't |
| 5925 | * know where near the end of the loop body the normal copy of it is |
| 5926 | * going to be placed. Same goes for the condition for a do-while |
| 5927 | * loop. |
| 5928 | */ |
| 5929 | if (state->loop_nesting_ast != NULL && |
Tapani Pälli | 73dd50a | 2014-08-06 09:46:54 +0300 | [diff] [blame] | 5930 | mode == ast_continue && !state->switch_state.is_switch_innermost) { |
Paul Berry | 7f57408 | 2014-01-31 09:55:35 -0800 | [diff] [blame] | 5931 | if (state->loop_nesting_ast->rest_expression) { |
| 5932 | state->loop_nesting_ast->rest_expression->hir(instructions, |
| 5933 | state); |
| 5934 | } |
| 5935 | if (state->loop_nesting_ast->mode == |
| 5936 | ast_iteration_statement::ast_do_while) { |
| 5937 | state->loop_nesting_ast->condition_to_hir(instructions, state); |
| 5938 | } |
| 5939 | } |
Eric Anholt | 2d1ed7b | 2010-07-22 12:55:16 -0700 | [diff] [blame] | 5940 | |
Iago Toral Quiroga | 4472ab9 | 2014-04-14 09:14:23 +0200 | [diff] [blame] | 5941 | if (state->switch_state.is_switch_innermost && |
Tapani Pälli | 73dd50a | 2014-08-06 09:46:54 +0300 | [diff] [blame] | 5942 | mode == ast_continue) { |
| 5943 | /* Set 'continue_inside' to true. */ |
| 5944 | ir_rvalue *const true_val = new (ctx) ir_constant(true); |
| 5945 | ir_dereference_variable *deref_continue_inside_var = |
| 5946 | new(ctx) ir_dereference_variable(state->switch_state.continue_inside); |
| 5947 | instructions->push_tail(new(ctx) ir_assignment(deref_continue_inside_var, |
| 5948 | true_val)); |
| 5949 | |
| 5950 | /* Break out from the switch, continue for the loop will |
| 5951 | * be called right after switch. */ |
| 5952 | ir_loop_jump *const jump = |
| 5953 | new(ctx) ir_loop_jump(ir_loop_jump::jump_break); |
| 5954 | instructions->push_tail(jump); |
| 5955 | |
| 5956 | } else if (state->switch_state.is_switch_innermost && |
Iago Toral Quiroga | 4472ab9 | 2014-04-14 09:14:23 +0200 | [diff] [blame] | 5957 | mode == ast_break) { |
Tapani Pälli | 73dd50a | 2014-08-06 09:46:54 +0300 | [diff] [blame] | 5958 | /* Force break out of switch by inserting a break. */ |
| 5959 | ir_loop_jump *const jump = |
| 5960 | new(ctx) ir_loop_jump(ir_loop_jump::jump_break); |
| 5961 | instructions->push_tail(jump); |
| 5962 | } else { |
Iago Toral Quiroga | 4472ab9 | 2014-04-14 09:14:23 +0200 | [diff] [blame] | 5963 | ir_loop_jump *const jump = |
| 5964 | new(ctx) ir_loop_jump((mode == ast_break) |
| 5965 | ? ir_loop_jump::jump_break |
| 5966 | : ir_loop_jump::jump_continue); |
| 5967 | instructions->push_tail(jump); |
| 5968 | } |
Ian Romanick | 4cf20cd | 2010-04-05 17:13:47 -0700 | [diff] [blame] | 5969 | } |
| 5970 | |
Ian Romanick | c0e76d8 | 2010-04-05 16:53:19 -0700 | [diff] [blame] | 5971 | break; |
Eric Anholt | b980207 | 2010-03-30 23:40:14 -1000 | [diff] [blame] | 5972 | } |
| 5973 | |
Ian Romanick | 16a246c | 2010-03-19 16:45:19 -0700 | [diff] [blame] | 5974 | /* Jump instructions do not have r-values. |
| 5975 | */ |
| 5976 | return NULL; |
| 5977 | } |
Ian Romanick | 3c6fea3 | 2010-03-29 14:11:25 -0700 | [diff] [blame] | 5978 | |
| 5979 | |
| 5980 | ir_rvalue * |
| 5981 | ast_selection_statement::hir(exec_list *instructions, |
Iago Toral Quiroga | 4472ab9 | 2014-04-14 09:14:23 +0200 | [diff] [blame] | 5982 | struct _mesa_glsl_parse_state *state) |
Ian Romanick | 3c6fea3 | 2010-03-29 14:11:25 -0700 | [diff] [blame] | 5983 | { |
Kenneth Graunke | 953ff12 | 2010-06-25 13:14:37 -0700 | [diff] [blame] | 5984 | void *ctx = state; |
Carl Worth | 1660a29 | 2010-06-23 18:11:51 -0700 | [diff] [blame] | 5985 | |
Ian Romanick | 3c6fea3 | 2010-03-29 14:11:25 -0700 | [diff] [blame] | 5986 | ir_rvalue *const condition = this->condition->hir(instructions, state); |
Ian Romanick | 3c6fea3 | 2010-03-29 14:11:25 -0700 | [diff] [blame] | 5987 | |
| 5988 | /* From page 66 (page 72 of the PDF) of the GLSL 1.50 spec: |
| 5989 | * |
| 5990 | * "Any expression whose type evaluates to a Boolean can be used as the |
| 5991 | * conditional expression bool-expression. Vector types are not accepted |
| 5992 | * as the expression to if." |
| 5993 | * |
| 5994 | * The checks are separated so that higher quality diagnostics can be |
| 5995 | * generated for cases where both rules are violated. |
| 5996 | */ |
| 5997 | if (!condition->type->is_boolean() || !condition->type->is_scalar()) { |
| 5998 | YYLTYPE loc = this->condition->get_location(); |
| 5999 | |
| 6000 | _mesa_glsl_error(& loc, state, "if-statement condition must be scalar " |
Iago Toral Quiroga | 4472ab9 | 2014-04-14 09:14:23 +0200 | [diff] [blame] | 6001 | "boolean"); |
Ian Romanick | 3c6fea3 | 2010-03-29 14:11:25 -0700 | [diff] [blame] | 6002 | } |
| 6003 | |
Carl Worth | 1660a29 | 2010-06-23 18:11:51 -0700 | [diff] [blame] | 6004 | ir_if *const stmt = new(ctx) ir_if(condition); |
Ian Romanick | 3c6fea3 | 2010-03-29 14:11:25 -0700 | [diff] [blame] | 6005 | |
Kenneth Graunke | 665d75c | 2010-08-18 13:54:50 -0700 | [diff] [blame] | 6006 | if (then_statement != NULL) { |
| 6007 | state->symbols->push_scope(); |
Ian Romanick | 4f9d72f | 2010-05-10 11:10:26 -0700 | [diff] [blame] | 6008 | then_statement->hir(& stmt->then_instructions, state); |
Kenneth Graunke | 665d75c | 2010-08-18 13:54:50 -0700 | [diff] [blame] | 6009 | state->symbols->pop_scope(); |
| 6010 | } |
Ian Romanick | 3c6fea3 | 2010-03-29 14:11:25 -0700 | [diff] [blame] | 6011 | |
Kenneth Graunke | 665d75c | 2010-08-18 13:54:50 -0700 | [diff] [blame] | 6012 | if (else_statement != NULL) { |
| 6013 | state->symbols->push_scope(); |
Ian Romanick | 4f9d72f | 2010-05-10 11:10:26 -0700 | [diff] [blame] | 6014 | else_statement->hir(& stmt->else_instructions, state); |
Kenneth Graunke | 665d75c | 2010-08-18 13:54:50 -0700 | [diff] [blame] | 6015 | state->symbols->pop_scope(); |
| 6016 | } |
Ian Romanick | 3c6fea3 | 2010-03-29 14:11:25 -0700 | [diff] [blame] | 6017 | |
| 6018 | instructions->push_tail(stmt); |
| 6019 | |
| 6020 | /* if-statements do not have r-values. |
| 6021 | */ |
| 6022 | return NULL; |
| 6023 | } |
Ian Romanick | 9e7d010 | 2010-04-05 16:37:49 -0700 | [diff] [blame] | 6024 | |
| 6025 | |
Tapani Pälli | 6823380 | 2016-08-19 13:44:54 +0300 | [diff] [blame] | 6026 | /* Used for detection of duplicate case values, compare |
| 6027 | * given contents directly. |
| 6028 | */ |
| 6029 | static bool |
| 6030 | compare_case_value(const void *a, const void *b) |
| 6031 | { |
| 6032 | return *(unsigned *) a == *(unsigned *) b; |
| 6033 | } |
| 6034 | |
| 6035 | |
| 6036 | /* Used for detection of duplicate case values, just |
| 6037 | * returns key contents as is. |
| 6038 | */ |
| 6039 | static unsigned |
| 6040 | key_contents(const void *key) |
| 6041 | { |
| 6042 | return *(unsigned *) key; |
| 6043 | } |
| 6044 | |
| 6045 | |
Dan McCabe | 85beb39 | 2011-11-07 15:11:04 -0800 | [diff] [blame] | 6046 | ir_rvalue * |
| 6047 | ast_switch_statement::hir(exec_list *instructions, |
Iago Toral Quiroga | 4472ab9 | 2014-04-14 09:14:23 +0200 | [diff] [blame] | 6048 | struct _mesa_glsl_parse_state *state) |
Dan McCabe | 85beb39 | 2011-11-07 15:11:04 -0800 | [diff] [blame] | 6049 | { |
Dan McCabe | 5c02e2e | 2011-11-07 16:17:58 -0800 | [diff] [blame] | 6050 | void *ctx = state; |
| 6051 | |
| 6052 | ir_rvalue *const test_expression = |
| 6053 | this->test_expression->hir(instructions, state); |
| 6054 | |
| 6055 | /* From page 66 (page 55 of the PDF) of the GLSL 1.50 spec: |
| 6056 | * |
Iago Toral Quiroga | 1af0d9d | 2015-11-24 12:40:53 +0100 | [diff] [blame] | 6057 | * "The type of init-expression in a switch statement must be a |
| 6058 | * scalar integer." |
Dan McCabe | 5c02e2e | 2011-11-07 16:17:58 -0800 | [diff] [blame] | 6059 | */ |
Eric Anholt | bbbc7c7 | 2012-05-14 08:45:59 -0700 | [diff] [blame] | 6060 | if (!test_expression->type->is_scalar() || |
| 6061 | !test_expression->type->is_integer()) { |
Dan McCabe | 5c02e2e | 2011-11-07 16:17:58 -0800 | [diff] [blame] | 6062 | YYLTYPE loc = this->test_expression->get_location(); |
| 6063 | |
| 6064 | _mesa_glsl_error(& loc, |
Iago Toral Quiroga | 4472ab9 | 2014-04-14 09:14:23 +0200 | [diff] [blame] | 6065 | state, |
| 6066 | "switch-statement expression must be scalar " |
| 6067 | "integer"); |
Dan McCabe | 5c02e2e | 2011-11-07 16:17:58 -0800 | [diff] [blame] | 6068 | } |
| 6069 | |
| 6070 | /* Track the switch-statement nesting in a stack-like manner. |
| 6071 | */ |
Eric Anholt | 22d81f1 | 2012-01-28 11:26:02 -0800 | [diff] [blame] | 6072 | struct glsl_switch_state saved = state->switch_state; |
Dan McCabe | 5c02e2e | 2011-11-07 16:17:58 -0800 | [diff] [blame] | 6073 | |
Eric Anholt | 22d81f1 | 2012-01-28 11:26:02 -0800 | [diff] [blame] | 6074 | state->switch_state.is_switch_innermost = true; |
| 6075 | state->switch_state.switch_nesting_ast = this; |
Thomas Helland | 9f188be | 2016-08-16 22:10:21 +0200 | [diff] [blame] | 6076 | state->switch_state.labels_ht = |
| 6077 | _mesa_hash_table_create(NULL, key_contents, |
| 6078 | compare_case_value); |
Eric Anholt | 57e4437 | 2012-01-30 09:50:35 -0800 | [diff] [blame] | 6079 | state->switch_state.previous_default = NULL; |
Dan McCabe | 5c02e2e | 2011-11-07 16:17:58 -0800 | [diff] [blame] | 6080 | |
| 6081 | /* Initalize is_fallthru state to false. |
| 6082 | */ |
| 6083 | ir_rvalue *const is_fallthru_val = new (ctx) ir_constant(false); |
Eric Anholt | 22d81f1 | 2012-01-28 11:26:02 -0800 | [diff] [blame] | 6084 | state->switch_state.is_fallthru_var = |
| 6085 | new(ctx) ir_variable(glsl_type::bool_type, |
Iago Toral Quiroga | 4472ab9 | 2014-04-14 09:14:23 +0200 | [diff] [blame] | 6086 | "switch_is_fallthru_tmp", |
| 6087 | ir_var_temporary); |
Eric Anholt | 22d81f1 | 2012-01-28 11:26:02 -0800 | [diff] [blame] | 6088 | instructions->push_tail(state->switch_state.is_fallthru_var); |
Dan McCabe | 5c02e2e | 2011-11-07 16:17:58 -0800 | [diff] [blame] | 6089 | |
| 6090 | ir_dereference_variable *deref_is_fallthru_var = |
Eric Anholt | 22d81f1 | 2012-01-28 11:26:02 -0800 | [diff] [blame] | 6091 | new(ctx) ir_dereference_variable(state->switch_state.is_fallthru_var); |
Dan McCabe | 5c02e2e | 2011-11-07 16:17:58 -0800 | [diff] [blame] | 6092 | instructions->push_tail(new(ctx) ir_assignment(deref_is_fallthru_var, |
Iago Toral Quiroga | 4472ab9 | 2014-04-14 09:14:23 +0200 | [diff] [blame] | 6093 | is_fallthru_val)); |
Dan McCabe | 5c02e2e | 2011-11-07 16:17:58 -0800 | [diff] [blame] | 6094 | |
Tapani Pälli | 73dd50a | 2014-08-06 09:46:54 +0300 | [diff] [blame] | 6095 | /* Initialize continue_inside state to false. |
Dan McCabe | 5c02e2e | 2011-11-07 16:17:58 -0800 | [diff] [blame] | 6096 | */ |
Tapani Pälli | 73dd50a | 2014-08-06 09:46:54 +0300 | [diff] [blame] | 6097 | state->switch_state.continue_inside = |
Iago Toral Quiroga | 4472ab9 | 2014-04-14 09:14:23 +0200 | [diff] [blame] | 6098 | new(ctx) ir_variable(glsl_type::bool_type, |
Tapani Pälli | 73dd50a | 2014-08-06 09:46:54 +0300 | [diff] [blame] | 6099 | "continue_inside_tmp", |
Iago Toral Quiroga | 4472ab9 | 2014-04-14 09:14:23 +0200 | [diff] [blame] | 6100 | ir_var_temporary); |
Tapani Pälli | 73dd50a | 2014-08-06 09:46:54 +0300 | [diff] [blame] | 6101 | instructions->push_tail(state->switch_state.continue_inside); |
Dan McCabe | 5c02e2e | 2011-11-07 16:17:58 -0800 | [diff] [blame] | 6102 | |
Tapani Pälli | 73dd50a | 2014-08-06 09:46:54 +0300 | [diff] [blame] | 6103 | ir_rvalue *const false_val = new (ctx) ir_constant(false); |
| 6104 | ir_dereference_variable *deref_continue_inside_var = |
| 6105 | new(ctx) ir_dereference_variable(state->switch_state.continue_inside); |
| 6106 | instructions->push_tail(new(ctx) ir_assignment(deref_continue_inside_var, |
| 6107 | false_val)); |
Dan McCabe | 5c02e2e | 2011-11-07 16:17:58 -0800 | [diff] [blame] | 6108 | |
Tapani Pälli | 48deb4d | 2014-07-14 09:45:46 +0300 | [diff] [blame] | 6109 | state->switch_state.run_default = |
| 6110 | new(ctx) ir_variable(glsl_type::bool_type, |
| 6111 | "run_default_tmp", |
| 6112 | ir_var_temporary); |
| 6113 | instructions->push_tail(state->switch_state.run_default); |
| 6114 | |
Tapani Pälli | 73dd50a | 2014-08-06 09:46:54 +0300 | [diff] [blame] | 6115 | /* Loop around the switch is used for flow control. */ |
| 6116 | ir_loop * loop = new(ctx) ir_loop(); |
| 6117 | instructions->push_tail(loop); |
| 6118 | |
Dan McCabe | 5c02e2e | 2011-11-07 16:17:58 -0800 | [diff] [blame] | 6119 | /* Cache test expression. |
| 6120 | */ |
Tapani Pälli | 73dd50a | 2014-08-06 09:46:54 +0300 | [diff] [blame] | 6121 | test_to_hir(&loop->body_instructions, state); |
Eric Anholt | 5462f36 | 2012-05-14 08:37:50 -0700 | [diff] [blame] | 6122 | |
Dan McCabe | 5c02e2e | 2011-11-07 16:17:58 -0800 | [diff] [blame] | 6123 | /* Emit code for body of switch stmt. |
| 6124 | */ |
Tapani Pälli | 73dd50a | 2014-08-06 09:46:54 +0300 | [diff] [blame] | 6125 | body->hir(&loop->body_instructions, state); |
| 6126 | |
| 6127 | /* Insert a break at the end to exit loop. */ |
| 6128 | ir_loop_jump *jump = new(ctx) ir_loop_jump(ir_loop_jump::jump_break); |
| 6129 | loop->body_instructions.push_tail(jump); |
| 6130 | |
| 6131 | /* If we are inside loop, check if continue got called inside switch. */ |
| 6132 | if (state->loop_nesting_ast != NULL) { |
| 6133 | ir_dereference_variable *deref_continue_inside = |
| 6134 | new(ctx) ir_dereference_variable(state->switch_state.continue_inside); |
| 6135 | ir_if *irif = new(ctx) ir_if(deref_continue_inside); |
| 6136 | ir_loop_jump *jump = new(ctx) ir_loop_jump(ir_loop_jump::jump_continue); |
| 6137 | |
| 6138 | if (state->loop_nesting_ast != NULL) { |
| 6139 | if (state->loop_nesting_ast->rest_expression) { |
| 6140 | state->loop_nesting_ast->rest_expression->hir(&irif->then_instructions, |
| 6141 | state); |
| 6142 | } |
| 6143 | if (state->loop_nesting_ast->mode == |
| 6144 | ast_iteration_statement::ast_do_while) { |
| 6145 | state->loop_nesting_ast->condition_to_hir(&irif->then_instructions, state); |
| 6146 | } |
| 6147 | } |
| 6148 | irif->then_instructions.push_tail(jump); |
| 6149 | instructions->push_tail(irif); |
| 6150 | } |
Dan McCabe | 5c02e2e | 2011-11-07 16:17:58 -0800 | [diff] [blame] | 6151 | |
Thomas Helland | 9f188be | 2016-08-16 22:10:21 +0200 | [diff] [blame] | 6152 | _mesa_hash_table_destroy(state->switch_state.labels_ht, NULL); |
Eric Anholt | 1406321 | 2012-01-30 08:50:14 -0800 | [diff] [blame] | 6153 | |
Eric Anholt | 22d81f1 | 2012-01-28 11:26:02 -0800 | [diff] [blame] | 6154 | state->switch_state = saved; |
Dan McCabe | 5c02e2e | 2011-11-07 16:17:58 -0800 | [diff] [blame] | 6155 | |
Eric Anholt | 5462f36 | 2012-05-14 08:37:50 -0700 | [diff] [blame] | 6156 | /* Switch statements do not have r-values. */ |
| 6157 | return NULL; |
| 6158 | } |
Dan McCabe | 85beb39 | 2011-11-07 15:11:04 -0800 | [diff] [blame] | 6159 | |
| 6160 | |
Eric Anholt | 5462f36 | 2012-05-14 08:37:50 -0700 | [diff] [blame] | 6161 | void |
| 6162 | ast_switch_statement::test_to_hir(exec_list *instructions, |
Iago Toral Quiroga | 4472ab9 | 2014-04-14 09:14:23 +0200 | [diff] [blame] | 6163 | struct _mesa_glsl_parse_state *state) |
Eric Anholt | 5462f36 | 2012-05-14 08:37:50 -0700 | [diff] [blame] | 6164 | { |
| 6165 | void *ctx = state; |
Dan McCabe | 5c02e2e | 2011-11-07 16:17:58 -0800 | [diff] [blame] | 6166 | |
Alejandro Piñeiro | 8568d02 | 2016-02-25 11:11:54 +0100 | [diff] [blame] | 6167 | /* set to true to avoid a duplicate "use of uninitialized variable" warning |
| 6168 | * on the switch test case. The first one would be already raised when |
| 6169 | * getting the test_expression at ast_switch_statement::hir |
| 6170 | */ |
| 6171 | test_expression->set_is_lhs(true); |
Eric Anholt | 5462f36 | 2012-05-14 08:37:50 -0700 | [diff] [blame] | 6172 | /* Cache value of test expression. */ |
Timothy Arceri | 222f66a | 2016-09-28 16:04:08 +1000 | [diff] [blame] | 6173 | ir_rvalue *const test_val = test_expression->hir(instructions, state); |
Dan McCabe | 5c02e2e | 2011-11-07 16:17:58 -0800 | [diff] [blame] | 6174 | |
Eric Anholt | 9c4e9ce | 2012-05-14 08:51:03 -0700 | [diff] [blame] | 6175 | state->switch_state.test_var = new(ctx) ir_variable(test_val->type, |
Iago Toral Quiroga | 4472ab9 | 2014-04-14 09:14:23 +0200 | [diff] [blame] | 6176 | "switch_test_tmp", |
| 6177 | ir_var_temporary); |
Eric Anholt | 5462f36 | 2012-05-14 08:37:50 -0700 | [diff] [blame] | 6178 | ir_dereference_variable *deref_test_var = |
| 6179 | new(ctx) ir_dereference_variable(state->switch_state.test_var); |
Dan McCabe | 5c02e2e | 2011-11-07 16:17:58 -0800 | [diff] [blame] | 6180 | |
Eric Anholt | 5462f36 | 2012-05-14 08:37:50 -0700 | [diff] [blame] | 6181 | instructions->push_tail(state->switch_state.test_var); |
Eric Anholt | aa5ec13 | 2012-05-14 09:14:54 -0700 | [diff] [blame] | 6182 | instructions->push_tail(new(ctx) ir_assignment(deref_test_var, test_val)); |
Eric Anholt | 5462f36 | 2012-05-14 08:37:50 -0700 | [diff] [blame] | 6183 | } |
Dan McCabe | 5c02e2e | 2011-11-07 16:17:58 -0800 | [diff] [blame] | 6184 | |
| 6185 | |
Eric Anholt | 5462f36 | 2012-05-14 08:37:50 -0700 | [diff] [blame] | 6186 | ir_rvalue * |
| 6187 | ast_switch_body::hir(exec_list *instructions, |
Iago Toral Quiroga | 4472ab9 | 2014-04-14 09:14:23 +0200 | [diff] [blame] | 6188 | struct _mesa_glsl_parse_state *state) |
Eric Anholt | 5462f36 | 2012-05-14 08:37:50 -0700 | [diff] [blame] | 6189 | { |
| 6190 | if (stmts != NULL) |
| 6191 | stmts->hir(instructions, state); |
Eric Anholt | 22d81f1 | 2012-01-28 11:26:02 -0800 | [diff] [blame] | 6192 | |
Eric Anholt | 5462f36 | 2012-05-14 08:37:50 -0700 | [diff] [blame] | 6193 | /* Switch bodies do not have r-values. */ |
| 6194 | return NULL; |
| 6195 | } |
| 6196 | |
| 6197 | ir_rvalue * |
| 6198 | ast_case_statement_list::hir(exec_list *instructions, |
Iago Toral Quiroga | 4472ab9 | 2014-04-14 09:14:23 +0200 | [diff] [blame] | 6199 | struct _mesa_glsl_parse_state *state) |
Eric Anholt | 5462f36 | 2012-05-14 08:37:50 -0700 | [diff] [blame] | 6200 | { |
Tapani Pälli | 48deb4d | 2014-07-14 09:45:46 +0300 | [diff] [blame] | 6201 | exec_list default_case, after_default, tmp; |
| 6202 | |
| 6203 | foreach_list_typed (ast_case_statement, case_stmt, link, & this->cases) { |
| 6204 | case_stmt->hir(&tmp, state); |
| 6205 | |
| 6206 | /* Default case. */ |
| 6207 | if (state->switch_state.previous_default && default_case.is_empty()) { |
| 6208 | default_case.append_list(&tmp); |
| 6209 | continue; |
| 6210 | } |
| 6211 | |
| 6212 | /* If default case found, append 'after_default' list. */ |
| 6213 | if (!default_case.is_empty()) |
| 6214 | after_default.append_list(&tmp); |
| 6215 | else |
| 6216 | instructions->append_list(&tmp); |
| 6217 | } |
| 6218 | |
| 6219 | /* Handle the default case. This is done here because default might not be |
| 6220 | * the last case. We need to add checks against following cases first to see |
| 6221 | * if default should be chosen or not. |
| 6222 | */ |
| 6223 | if (!default_case.is_empty()) { |
| 6224 | |
Tapani Pälli | 48deb4d | 2014-07-14 09:45:46 +0300 | [diff] [blame] | 6225 | ir_rvalue *const true_val = new (state) ir_constant(true); |
| 6226 | ir_dereference_variable *deref_run_default_var = |
| 6227 | new(state) ir_dereference_variable(state->switch_state.run_default); |
| 6228 | |
| 6229 | /* Choose to run default case initially, following conditional |
| 6230 | * assignments might change this. |
| 6231 | */ |
| 6232 | ir_assignment *const init_var = |
| 6233 | new(state) ir_assignment(deref_run_default_var, true_val); |
| 6234 | instructions->push_tail(init_var); |
| 6235 | |
Tapani Pälli | d66acc7 | 2014-07-25 09:40:13 +0300 | [diff] [blame] | 6236 | /* Default case was the last one, no checks required. */ |
| 6237 | if (after_default.is_empty()) { |
| 6238 | instructions->append_list(&default_case); |
| 6239 | return NULL; |
| 6240 | } |
| 6241 | |
Tapani Pälli | 48deb4d | 2014-07-14 09:45:46 +0300 | [diff] [blame] | 6242 | foreach_in_list(ir_instruction, ir, &after_default) { |
| 6243 | ir_assignment *assign = ir->as_assignment(); |
| 6244 | |
| 6245 | if (!assign) |
| 6246 | continue; |
| 6247 | |
| 6248 | /* Clone the check between case label and init expression. */ |
| 6249 | ir_expression *exp = (ir_expression*) assign->condition; |
| 6250 | ir_expression *clone = exp->clone(state, NULL); |
| 6251 | |
| 6252 | ir_dereference_variable *deref_var = |
| 6253 | new(state) ir_dereference_variable(state->switch_state.run_default); |
| 6254 | ir_rvalue *const false_val = new (state) ir_constant(false); |
| 6255 | |
| 6256 | ir_assignment *const set_false = |
| 6257 | new(state) ir_assignment(deref_var, false_val, clone); |
| 6258 | |
| 6259 | instructions->push_tail(set_false); |
| 6260 | } |
| 6261 | |
| 6262 | /* Append default case and all cases after it. */ |
| 6263 | instructions->append_list(&default_case); |
| 6264 | instructions->append_list(&after_default); |
| 6265 | } |
Eric Anholt | 5462f36 | 2012-05-14 08:37:50 -0700 | [diff] [blame] | 6266 | |
| 6267 | /* Case statements do not have r-values. */ |
| 6268 | return NULL; |
| 6269 | } |
| 6270 | |
| 6271 | ir_rvalue * |
| 6272 | ast_case_statement::hir(exec_list *instructions, |
Iago Toral Quiroga | 4472ab9 | 2014-04-14 09:14:23 +0200 | [diff] [blame] | 6273 | struct _mesa_glsl_parse_state *state) |
Eric Anholt | 5462f36 | 2012-05-14 08:37:50 -0700 | [diff] [blame] | 6274 | { |
| 6275 | labels->hir(instructions, state); |
| 6276 | |
Eric Anholt | 5462f36 | 2012-05-14 08:37:50 -0700 | [diff] [blame] | 6277 | /* Guard case statements depending on fallthru state. */ |
| 6278 | ir_dereference_variable *const deref_fallthru_guard = |
| 6279 | new(state) ir_dereference_variable(state->switch_state.is_fallthru_var); |
| 6280 | ir_if *const test_fallthru = new(state) ir_if(deref_fallthru_guard); |
| 6281 | |
| 6282 | foreach_list_typed (ast_node, stmt, link, & this->stmts) |
| 6283 | stmt->hir(& test_fallthru->then_instructions, state); |
| 6284 | |
| 6285 | instructions->push_tail(test_fallthru); |
| 6286 | |
| 6287 | /* Case statements do not have r-values. */ |
| 6288 | return NULL; |
| 6289 | } |
Dan McCabe | 85beb39 | 2011-11-07 15:11:04 -0800 | [diff] [blame] | 6290 | |
| 6291 | |
Eric Anholt | 5462f36 | 2012-05-14 08:37:50 -0700 | [diff] [blame] | 6292 | ir_rvalue * |
| 6293 | ast_case_label_list::hir(exec_list *instructions, |
Iago Toral Quiroga | 4472ab9 | 2014-04-14 09:14:23 +0200 | [diff] [blame] | 6294 | struct _mesa_glsl_parse_state *state) |
Eric Anholt | 5462f36 | 2012-05-14 08:37:50 -0700 | [diff] [blame] | 6295 | { |
| 6296 | foreach_list_typed (ast_case_label, label, link, & this->labels) |
| 6297 | label->hir(instructions, state); |
Eric Anholt | 22d81f1 | 2012-01-28 11:26:02 -0800 | [diff] [blame] | 6298 | |
Eric Anholt | 5462f36 | 2012-05-14 08:37:50 -0700 | [diff] [blame] | 6299 | /* Case labels do not have r-values. */ |
| 6300 | return NULL; |
| 6301 | } |
Dan McCabe | 85beb39 | 2011-11-07 15:11:04 -0800 | [diff] [blame] | 6302 | |
Eric Anholt | 5462f36 | 2012-05-14 08:37:50 -0700 | [diff] [blame] | 6303 | ir_rvalue * |
| 6304 | ast_case_label::hir(exec_list *instructions, |
Iago Toral Quiroga | 4472ab9 | 2014-04-14 09:14:23 +0200 | [diff] [blame] | 6305 | struct _mesa_glsl_parse_state *state) |
Eric Anholt | 5462f36 | 2012-05-14 08:37:50 -0700 | [diff] [blame] | 6306 | { |
| 6307 | void *ctx = state; |
Dan McCabe | 85beb39 | 2011-11-07 15:11:04 -0800 | [diff] [blame] | 6308 | |
Eric Anholt | 5462f36 | 2012-05-14 08:37:50 -0700 | [diff] [blame] | 6309 | ir_dereference_variable *deref_fallthru_var = |
| 6310 | new(ctx) ir_dereference_variable(state->switch_state.is_fallthru_var); |
Dan McCabe | 5c02e2e | 2011-11-07 16:17:58 -0800 | [diff] [blame] | 6311 | |
Eric Anholt | 5462f36 | 2012-05-14 08:37:50 -0700 | [diff] [blame] | 6312 | ir_rvalue *const true_val = new(ctx) ir_constant(true); |
Dan McCabe | 5c02e2e | 2011-11-07 16:17:58 -0800 | [diff] [blame] | 6313 | |
Eric Anholt | 5462f36 | 2012-05-14 08:37:50 -0700 | [diff] [blame] | 6314 | /* If not default case, ... */ |
| 6315 | if (this->test_value != NULL) { |
| 6316 | /* Conditionally set fallthru state based on |
| 6317 | * comparison of cached test expression value to case label. |
| 6318 | */ |
| 6319 | ir_rvalue *const label_rval = this->test_value->hir(instructions, state); |
| 6320 | ir_constant *label_const = label_rval->constant_expression_value(); |
Eric Anholt | 22d81f1 | 2012-01-28 11:26:02 -0800 | [diff] [blame] | 6321 | |
Eric Anholt | 5462f36 | 2012-05-14 08:37:50 -0700 | [diff] [blame] | 6322 | if (!label_const) { |
Iago Toral Quiroga | 4472ab9 | 2014-04-14 09:14:23 +0200 | [diff] [blame] | 6323 | YYLTYPE loc = this->test_value->get_location(); |
Eric Anholt | 22d81f1 | 2012-01-28 11:26:02 -0800 | [diff] [blame] | 6324 | |
Iago Toral Quiroga | 4472ab9 | 2014-04-14 09:14:23 +0200 | [diff] [blame] | 6325 | _mesa_glsl_error(& loc, state, |
| 6326 | "switch statement case label must be a " |
| 6327 | "constant expression"); |
Eric Anholt | 22d81f1 | 2012-01-28 11:26:02 -0800 | [diff] [blame] | 6328 | |
Iago Toral Quiroga | 4472ab9 | 2014-04-14 09:14:23 +0200 | [diff] [blame] | 6329 | /* Stuff a dummy value in to allow processing to continue. */ |
| 6330 | label_const = new(ctx) ir_constant(0); |
Eric Anholt | 5462f36 | 2012-05-14 08:37:50 -0700 | [diff] [blame] | 6331 | } else { |
Thomas Helland | 9f188be | 2016-08-16 22:10:21 +0200 | [diff] [blame] | 6332 | hash_entry *entry = |
| 6333 | _mesa_hash_table_search(state->switch_state.labels_ht, |
| 6334 | (void *)(uintptr_t)&label_const->value.u[0]); |
Dan McCabe | 85beb39 | 2011-11-07 15:11:04 -0800 | [diff] [blame] | 6335 | |
Thomas Helland | 9f188be | 2016-08-16 22:10:21 +0200 | [diff] [blame] | 6336 | if (entry) { |
| 6337 | ast_expression *previous_label = (ast_expression *) entry->data; |
Iago Toral Quiroga | 4472ab9 | 2014-04-14 09:14:23 +0200 | [diff] [blame] | 6338 | YYLTYPE loc = this->test_value->get_location(); |
| 6339 | _mesa_glsl_error(& loc, state, "duplicate case value"); |
Dan McCabe | 85beb39 | 2011-11-07 15:11:04 -0800 | [diff] [blame] | 6340 | |
Iago Toral Quiroga | 4472ab9 | 2014-04-14 09:14:23 +0200 | [diff] [blame] | 6341 | loc = previous_label->get_location(); |
| 6342 | _mesa_glsl_error(& loc, state, "this is the previous case label"); |
| 6343 | } else { |
Thomas Helland | 9f188be | 2016-08-16 22:10:21 +0200 | [diff] [blame] | 6344 | _mesa_hash_table_insert(state->switch_state.labels_ht, |
| 6345 | (void *)(uintptr_t)&label_const->value.u[0], |
| 6346 | this->test_value); |
Iago Toral Quiroga | 4472ab9 | 2014-04-14 09:14:23 +0200 | [diff] [blame] | 6347 | } |
Eric Anholt | 5462f36 | 2012-05-14 08:37:50 -0700 | [diff] [blame] | 6348 | } |
Eric Anholt | 1406321 | 2012-01-30 08:50:14 -0800 | [diff] [blame] | 6349 | |
Eric Anholt | 5462f36 | 2012-05-14 08:37:50 -0700 | [diff] [blame] | 6350 | ir_dereference_variable *deref_test_var = |
Iago Toral Quiroga | 4472ab9 | 2014-04-14 09:14:23 +0200 | [diff] [blame] | 6351 | new(ctx) ir_dereference_variable(state->switch_state.test_var); |
Eric Anholt | 1406321 | 2012-01-30 08:50:14 -0800 | [diff] [blame] | 6352 | |
Tapani Pälli | 39cdf16 | 2014-06-12 12:48:43 +0300 | [diff] [blame] | 6353 | ir_expression *test_cond = new(ctx) ir_expression(ir_binop_all_equal, |
| 6354 | label_const, |
| 6355 | deref_test_var); |
| 6356 | |
| 6357 | /* |
| 6358 | * From GLSL 4.40 specification section 6.2 ("Selection"): |
| 6359 | * |
| 6360 | * "The type of the init-expression value in a switch statement must |
| 6361 | * be a scalar int or uint. The type of the constant-expression value |
| 6362 | * in a case label also must be a scalar int or uint. When any pair |
| 6363 | * of these values is tested for "equal value" and the types do not |
| 6364 | * match, an implicit conversion will be done to convert the int to a |
| 6365 | * uint (see section 4.1.10 “Implicit Conversions”) before the compare |
| 6366 | * is done." |
| 6367 | */ |
| 6368 | if (label_const->type != state->switch_state.test_var->type) { |
| 6369 | YYLTYPE loc = this->test_value->get_location(); |
| 6370 | |
| 6371 | const glsl_type *type_a = label_const->type; |
| 6372 | const glsl_type *type_b = state->switch_state.test_var->type; |
| 6373 | |
| 6374 | /* Check if int->uint implicit conversion is supported. */ |
| 6375 | bool integer_conversion_supported = |
| 6376 | glsl_type::int_type->can_implicitly_convert_to(glsl_type::uint_type, |
| 6377 | state); |
| 6378 | |
| 6379 | if ((!type_a->is_integer() || !type_b->is_integer()) || |
| 6380 | !integer_conversion_supported) { |
| 6381 | _mesa_glsl_error(&loc, state, "type mismatch with switch " |
| 6382 | "init-expression and case label (%s != %s)", |
| 6383 | type_a->name, type_b->name); |
| 6384 | } else { |
| 6385 | /* Conversion of the case label. */ |
| 6386 | if (type_a->base_type == GLSL_TYPE_INT) { |
| 6387 | if (!apply_implicit_conversion(glsl_type::uint_type, |
| 6388 | test_cond->operands[0], state)) |
| 6389 | _mesa_glsl_error(&loc, state, "implicit type conversion error"); |
| 6390 | } else { |
| 6391 | /* Conversion of the init-expression value. */ |
| 6392 | if (!apply_implicit_conversion(glsl_type::uint_type, |
| 6393 | test_cond->operands[1], state)) |
| 6394 | _mesa_glsl_error(&loc, state, "implicit type conversion error"); |
| 6395 | } |
| 6396 | } |
| 6397 | } |
Dan McCabe | 5c02e2e | 2011-11-07 16:17:58 -0800 | [diff] [blame] | 6398 | |
Eric Anholt | 5462f36 | 2012-05-14 08:37:50 -0700 | [diff] [blame] | 6399 | ir_assignment *set_fallthru_on_test = |
Iago Toral Quiroga | 4472ab9 | 2014-04-14 09:14:23 +0200 | [diff] [blame] | 6400 | new(ctx) ir_assignment(deref_fallthru_var, true_val, test_cond); |
Dan McCabe | 5c02e2e | 2011-11-07 16:17:58 -0800 | [diff] [blame] | 6401 | |
Eric Anholt | 5462f36 | 2012-05-14 08:37:50 -0700 | [diff] [blame] | 6402 | instructions->push_tail(set_fallthru_on_test); |
| 6403 | } else { /* default case */ |
| 6404 | if (state->switch_state.previous_default) { |
Iago Toral Quiroga | 4472ab9 | 2014-04-14 09:14:23 +0200 | [diff] [blame] | 6405 | YYLTYPE loc = this->get_location(); |
| 6406 | _mesa_glsl_error(& loc, state, |
| 6407 | "multiple default labels in one switch"); |
Eric Anholt | 22d81f1 | 2012-01-28 11:26:02 -0800 | [diff] [blame] | 6408 | |
Iago Toral Quiroga | 4472ab9 | 2014-04-14 09:14:23 +0200 | [diff] [blame] | 6409 | loc = state->switch_state.previous_default->get_location(); |
| 6410 | _mesa_glsl_error(& loc, state, "this is the first default label"); |
Eric Anholt | 5462f36 | 2012-05-14 08:37:50 -0700 | [diff] [blame] | 6411 | } |
| 6412 | state->switch_state.previous_default = this; |
Eric Anholt | 57e4437 | 2012-01-30 09:50:35 -0800 | [diff] [blame] | 6413 | |
Tapani Pälli | 48deb4d | 2014-07-14 09:45:46 +0300 | [diff] [blame] | 6414 | /* Set fallthru condition on 'run_default' bool. */ |
| 6415 | ir_dereference_variable *deref_run_default = |
| 6416 | new(ctx) ir_dereference_variable(state->switch_state.run_default); |
| 6417 | ir_rvalue *const cond_true = new(ctx) ir_constant(true); |
| 6418 | ir_expression *test_cond = new(ctx) ir_expression(ir_binop_all_equal, |
| 6419 | cond_true, |
| 6420 | deref_run_default); |
| 6421 | |
Eric Anholt | 5462f36 | 2012-05-14 08:37:50 -0700 | [diff] [blame] | 6422 | /* Set falltrhu state. */ |
| 6423 | ir_assignment *set_fallthru = |
Tapani Pälli | 48deb4d | 2014-07-14 09:45:46 +0300 | [diff] [blame] | 6424 | new(ctx) ir_assignment(deref_fallthru_var, true_val, test_cond); |
Eric Anholt | 57e4437 | 2012-01-30 09:50:35 -0800 | [diff] [blame] | 6425 | |
Eric Anholt | 5462f36 | 2012-05-14 08:37:50 -0700 | [diff] [blame] | 6426 | instructions->push_tail(set_fallthru); |
| 6427 | } |
Eric Anholt | 57e4437 | 2012-01-30 09:50:35 -0800 | [diff] [blame] | 6428 | |
Eric Anholt | 5462f36 | 2012-05-14 08:37:50 -0700 | [diff] [blame] | 6429 | /* Case statements do not have r-values. */ |
| 6430 | return NULL; |
| 6431 | } |
Eric Anholt | 22d81f1 | 2012-01-28 11:26:02 -0800 | [diff] [blame] | 6432 | |
Eric Anholt | 5462f36 | 2012-05-14 08:37:50 -0700 | [diff] [blame] | 6433 | void |
Paul Berry | 5679085 | 2014-01-31 09:50:37 -0800 | [diff] [blame] | 6434 | ast_iteration_statement::condition_to_hir(exec_list *instructions, |
Iago Toral Quiroga | 4472ab9 | 2014-04-14 09:14:23 +0200 | [diff] [blame] | 6435 | struct _mesa_glsl_parse_state *state) |
Eric Anholt | 5462f36 | 2012-05-14 08:37:50 -0700 | [diff] [blame] | 6436 | { |
| 6437 | void *ctx = state; |
Eric Anholt | 22d81f1 | 2012-01-28 11:26:02 -0800 | [diff] [blame] | 6438 | |
Eric Anholt | 5462f36 | 2012-05-14 08:37:50 -0700 | [diff] [blame] | 6439 | if (condition != NULL) { |
| 6440 | ir_rvalue *const cond = |
Iago Toral Quiroga | 4472ab9 | 2014-04-14 09:14:23 +0200 | [diff] [blame] | 6441 | condition->hir(instructions, state); |
Eric Anholt | 5462f36 | 2012-05-14 08:37:50 -0700 | [diff] [blame] | 6442 | |
| 6443 | if ((cond == NULL) |
Iago Toral Quiroga | 4472ab9 | 2014-04-14 09:14:23 +0200 | [diff] [blame] | 6444 | || !cond->type->is_boolean() || !cond->type->is_scalar()) { |
| 6445 | YYLTYPE loc = condition->get_location(); |
Eric Anholt | 5462f36 | 2012-05-14 08:37:50 -0700 | [diff] [blame] | 6446 | |
Iago Toral Quiroga | 4472ab9 | 2014-04-14 09:14:23 +0200 | [diff] [blame] | 6447 | _mesa_glsl_error(& loc, state, |
| 6448 | "loop condition must be scalar boolean"); |
Eric Anholt | 5462f36 | 2012-05-14 08:37:50 -0700 | [diff] [blame] | 6449 | } else { |
Iago Toral Quiroga | 4472ab9 | 2014-04-14 09:14:23 +0200 | [diff] [blame] | 6450 | /* As the first code in the loop body, generate a block that looks |
| 6451 | * like 'if (!condition) break;' as the loop termination condition. |
| 6452 | */ |
| 6453 | ir_rvalue *const not_cond = |
| 6454 | new(ctx) ir_expression(ir_unop_logic_not, cond); |
Eric Anholt | 5462f36 | 2012-05-14 08:37:50 -0700 | [diff] [blame] | 6455 | |
Iago Toral Quiroga | 4472ab9 | 2014-04-14 09:14:23 +0200 | [diff] [blame] | 6456 | ir_if *const if_stmt = new(ctx) ir_if(not_cond); |
Eric Anholt | 5462f36 | 2012-05-14 08:37:50 -0700 | [diff] [blame] | 6457 | |
Iago Toral Quiroga | 4472ab9 | 2014-04-14 09:14:23 +0200 | [diff] [blame] | 6458 | ir_jump *const break_stmt = |
| 6459 | new(ctx) ir_loop_jump(ir_loop_jump::jump_break); |
Eric Anholt | 5462f36 | 2012-05-14 08:37:50 -0700 | [diff] [blame] | 6460 | |
Iago Toral Quiroga | 4472ab9 | 2014-04-14 09:14:23 +0200 | [diff] [blame] | 6461 | if_stmt->then_instructions.push_tail(break_stmt); |
| 6462 | instructions->push_tail(if_stmt); |
Eric Anholt | 5462f36 | 2012-05-14 08:37:50 -0700 | [diff] [blame] | 6463 | } |
| 6464 | } |
| 6465 | } |
Dan McCabe | 85beb39 | 2011-11-07 15:11:04 -0800 | [diff] [blame] | 6466 | |
| 6467 | |
Eric Anholt | 5462f36 | 2012-05-14 08:37:50 -0700 | [diff] [blame] | 6468 | ir_rvalue * |
| 6469 | ast_iteration_statement::hir(exec_list *instructions, |
Iago Toral Quiroga | 4472ab9 | 2014-04-14 09:14:23 +0200 | [diff] [blame] | 6470 | struct _mesa_glsl_parse_state *state) |
Eric Anholt | 5462f36 | 2012-05-14 08:37:50 -0700 | [diff] [blame] | 6471 | { |
| 6472 | void *ctx = state; |
Carl Worth | 1660a29 | 2010-06-23 18:11:51 -0700 | [diff] [blame] | 6473 | |
Eric Anholt | 5462f36 | 2012-05-14 08:37:50 -0700 | [diff] [blame] | 6474 | /* For-loops and while-loops start a new scope, but do-while loops do not. |
| 6475 | */ |
| 6476 | if (mode != ast_do_while) |
| 6477 | state->symbols->push_scope(); |
Ian Romanick | 9e7d010 | 2010-04-05 16:37:49 -0700 | [diff] [blame] | 6478 | |
Eric Anholt | 5462f36 | 2012-05-14 08:37:50 -0700 | [diff] [blame] | 6479 | if (init_statement != NULL) |
| 6480 | init_statement->hir(instructions, state); |
Ian Romanick | 9e7d010 | 2010-04-05 16:37:49 -0700 | [diff] [blame] | 6481 | |
Eric Anholt | 5462f36 | 2012-05-14 08:37:50 -0700 | [diff] [blame] | 6482 | ir_loop *const stmt = new(ctx) ir_loop(); |
| 6483 | instructions->push_tail(stmt); |
Ian Romanick | 9e7d010 | 2010-04-05 16:37:49 -0700 | [diff] [blame] | 6484 | |
Eric Anholt | 5462f36 | 2012-05-14 08:37:50 -0700 | [diff] [blame] | 6485 | /* Track the current loop nesting. */ |
| 6486 | ast_iteration_statement *nesting_ast = state->loop_nesting_ast; |
Ian Romanick | 9e7d010 | 2010-04-05 16:37:49 -0700 | [diff] [blame] | 6487 | |
Eric Anholt | 5462f36 | 2012-05-14 08:37:50 -0700 | [diff] [blame] | 6488 | state->loop_nesting_ast = this; |
Ian Romanick | 9e7d010 | 2010-04-05 16:37:49 -0700 | [diff] [blame] | 6489 | |
Eric Anholt | 5462f36 | 2012-05-14 08:37:50 -0700 | [diff] [blame] | 6490 | /* Likewise, indicate that following code is closest to a loop, |
| 6491 | * NOT closest to a switch. |
| 6492 | */ |
| 6493 | bool saved_is_switch_innermost = state->switch_state.is_switch_innermost; |
| 6494 | state->switch_state.is_switch_innermost = false; |
Ian Romanick | 8c46ed2 | 2010-04-05 18:07:27 -0700 | [diff] [blame] | 6495 | |
Eric Anholt | 5462f36 | 2012-05-14 08:37:50 -0700 | [diff] [blame] | 6496 | if (mode != ast_do_while) |
Paul Berry | 5679085 | 2014-01-31 09:50:37 -0800 | [diff] [blame] | 6497 | condition_to_hir(&stmt->body_instructions, state); |
Ian Romanick | 8c46ed2 | 2010-04-05 18:07:27 -0700 | [diff] [blame] | 6498 | |
Eric Anholt | 5462f36 | 2012-05-14 08:37:50 -0700 | [diff] [blame] | 6499 | if (body != NULL) |
| 6500 | body->hir(& stmt->body_instructions, state); |
Carl Worth | 1660a29 | 2010-06-23 18:11:51 -0700 | [diff] [blame] | 6501 | |
Eric Anholt | 5462f36 | 2012-05-14 08:37:50 -0700 | [diff] [blame] | 6502 | if (rest_expression != NULL) |
| 6503 | rest_expression->hir(& stmt->body_instructions, state); |
Ian Romanick | 8c46ed2 | 2010-04-05 18:07:27 -0700 | [diff] [blame] | 6504 | |
Eric Anholt | 5462f36 | 2012-05-14 08:37:50 -0700 | [diff] [blame] | 6505 | if (mode == ast_do_while) |
Paul Berry | 5679085 | 2014-01-31 09:50:37 -0800 | [diff] [blame] | 6506 | condition_to_hir(&stmt->body_instructions, state); |
Ian Romanick | 8c46ed2 | 2010-04-05 18:07:27 -0700 | [diff] [blame] | 6507 | |
Eric Anholt | 5462f36 | 2012-05-14 08:37:50 -0700 | [diff] [blame] | 6508 | if (mode != ast_do_while) |
| 6509 | state->symbols->pop_scope(); |
Ian Romanick | 8c46ed2 | 2010-04-05 18:07:27 -0700 | [diff] [blame] | 6510 | |
Eric Anholt | 5462f36 | 2012-05-14 08:37:50 -0700 | [diff] [blame] | 6511 | /* Restore previous nesting before returning. */ |
| 6512 | state->loop_nesting_ast = nesting_ast; |
| 6513 | state->switch_state.is_switch_innermost = saved_is_switch_innermost; |
Ian Romanick | e9d0f26 | 2010-04-05 17:01:53 -0700 | [diff] [blame] | 6514 | |
Ian Romanick | 9e7d010 | 2010-04-05 16:37:49 -0700 | [diff] [blame] | 6515 | /* Loops do not have r-values. |
| 6516 | */ |
| 6517 | return NULL; |
| 6518 | } |
Ian Romanick | 3455ce6 | 2010-04-19 15:13:15 -0700 | [diff] [blame] | 6519 | |
| 6520 | |
Paul Berry | d5948f2 | 2013-02-12 12:36:41 -0800 | [diff] [blame] | 6521 | /** |
| 6522 | * Determine if the given type is valid for establishing a default precision |
| 6523 | * qualifier. |
| 6524 | * |
| 6525 | * From GLSL ES 3.00 section 4.5.4 ("Default Precision Qualifiers"): |
| 6526 | * |
| 6527 | * "The precision statement |
| 6528 | * |
| 6529 | * precision precision-qualifier type; |
| 6530 | * |
| 6531 | * can be used to establish a default precision qualifier. The type field |
| 6532 | * can be either int or float or any of the sampler types, and the |
| 6533 | * precision-qualifier can be lowp, mediump, or highp." |
| 6534 | * |
| 6535 | * GLSL ES 1.00 has similar language. GLSL 1.30 doesn't allow precision |
| 6536 | * qualifiers on sampler types, but this seems like an oversight (since the |
| 6537 | * intention of including these in GLSL 1.30 is to allow compatibility with ES |
| 6538 | * shaders). So we allow int, float, and all sampler types regardless of GLSL |
| 6539 | * version. |
| 6540 | */ |
| 6541 | static bool |
Ian Romanick | b15b62c | 2013-08-09 15:15:45 -0700 | [diff] [blame] | 6542 | is_valid_default_precision_type(const struct glsl_type *const type) |
Paul Berry | d5948f2 | 2013-02-12 12:36:41 -0800 | [diff] [blame] | 6543 | { |
Paul Berry | d5948f2 | 2013-02-12 12:36:41 -0800 | [diff] [blame] | 6544 | if (type == NULL) |
| 6545 | return false; |
| 6546 | |
| 6547 | switch (type->base_type) { |
| 6548 | case GLSL_TYPE_INT: |
| 6549 | case GLSL_TYPE_FLOAT: |
| 6550 | /* "int" and "float" are valid, but vectors and matrices are not. */ |
| 6551 | return type->vector_elements == 1 && type->matrix_columns == 1; |
| 6552 | case GLSL_TYPE_SAMPLER: |
Francisco Jerez | 26b1141 | 2015-08-17 01:28:57 +0300 | [diff] [blame] | 6553 | case GLSL_TYPE_IMAGE: |
| 6554 | case GLSL_TYPE_ATOMIC_UINT: |
Paul Berry | d5948f2 | 2013-02-12 12:36:41 -0800 | [diff] [blame] | 6555 | return true; |
| 6556 | default: |
| 6557 | return false; |
| 6558 | } |
| 6559 | } |
| 6560 | |
| 6561 | |
Ian Romanick | 3455ce6 | 2010-04-19 15:13:15 -0700 | [diff] [blame] | 6562 | ir_rvalue * |
| 6563 | ast_type_specifier::hir(exec_list *instructions, |
Iago Toral Quiroga | 4472ab9 | 2014-04-14 09:14:23 +0200 | [diff] [blame] | 6564 | struct _mesa_glsl_parse_state *state) |
Ian Romanick | 3455ce6 | 2010-04-19 15:13:15 -0700 | [diff] [blame] | 6565 | { |
Kenneth Graunke | 308d4c7 | 2013-07-15 15:39:35 -0700 | [diff] [blame] | 6566 | if (this->default_precision == ast_precision_none && this->structure == NULL) |
Chad Versace | 08a286c | 2011-01-16 21:44:57 -0800 | [diff] [blame] | 6567 | return NULL; |
| 6568 | |
| 6569 | YYLTYPE loc = this->get_location(); |
| 6570 | |
Chad Versace | 08a286c | 2011-01-16 21:44:57 -0800 | [diff] [blame] | 6571 | /* If this is a precision statement, check that the type to which it is |
| 6572 | * applied is either float or int. |
| 6573 | * |
| 6574 | * From section 4.5.3 of the GLSL 1.30 spec: |
| 6575 | * "The precision statement |
| 6576 | * precision precision-qualifier type; |
| 6577 | * can be used to establish a default precision qualifier. The type |
| 6578 | * field can be either int or float [...]. Any other types or |
| 6579 | * qualifiers will result in an error. |
| 6580 | */ |
Kenneth Graunke | 308d4c7 | 2013-07-15 15:39:35 -0700 | [diff] [blame] | 6581 | if (this->default_precision != ast_precision_none) { |
Kenneth Graunke | 6eec502 | 2013-07-15 15:58:29 -0700 | [diff] [blame] | 6582 | if (!state->check_precision_qualifiers_allowed(&loc)) |
| 6583 | return NULL; |
| 6584 | |
| 6585 | if (this->structure != NULL) { |
| 6586 | _mesa_glsl_error(&loc, state, |
| 6587 | "precision qualifiers do not apply to structures"); |
| 6588 | return NULL; |
| 6589 | } |
| 6590 | |
Timothy Arceri | b0c64d3 | 2014-01-23 23:22:01 +1100 | [diff] [blame] | 6591 | if (this->array_specifier != NULL) { |
Chad Versace | 08a286c | 2011-01-16 21:44:57 -0800 | [diff] [blame] | 6592 | _mesa_glsl_error(&loc, state, |
| 6593 | "default precision statements do not apply to " |
| 6594 | "arrays"); |
| 6595 | return NULL; |
| 6596 | } |
Ian Romanick | b15b62c | 2013-08-09 15:15:45 -0700 | [diff] [blame] | 6597 | |
| 6598 | const struct glsl_type *const type = |
| 6599 | state->symbols->get_type(this->type_name); |
| 6600 | if (!is_valid_default_precision_type(type)) { |
Chad Versace | 08a286c | 2011-01-16 21:44:57 -0800 | [diff] [blame] | 6601 | _mesa_glsl_error(&loc, state, |
Ian Romanick | 0b5fb6d | 2013-08-09 13:44:49 -0700 | [diff] [blame] | 6602 | "default precision statements apply only to " |
Francisco Jerez | 26b1141 | 2015-08-17 01:28:57 +0300 | [diff] [blame] | 6603 | "float, int, and opaque types"); |
Chad Versace | 08a286c | 2011-01-16 21:44:57 -0800 | [diff] [blame] | 6604 | return NULL; |
| 6605 | } |
| 6606 | |
Iago Toral Quiroga | e6629d8 | 2015-02-26 12:15:18 +0100 | [diff] [blame] | 6607 | if (state->es_shader) { |
Ian Romanick | cabd457 | 2013-08-09 15:17:18 -0700 | [diff] [blame] | 6608 | /* Section 4.5.3 (Default Precision Qualifiers) of the GLSL ES 1.00 |
| 6609 | * spec says: |
| 6610 | * |
Ian Romanick | cabd457 | 2013-08-09 15:17:18 -0700 | [diff] [blame] | 6611 | * "Non-precision qualified declarations will use the precision |
| 6612 | * qualifier specified in the most recent precision statement |
| 6613 | * that is still in scope. The precision statement has the same |
| 6614 | * scoping rules as variable declarations. If it is declared |
| 6615 | * inside a compound statement, its effect stops at the end of |
| 6616 | * the innermost statement it was declared in. Precision |
| 6617 | * statements in nested scopes override precision statements in |
| 6618 | * outer scopes. Multiple precision statements for the same basic |
| 6619 | * type can appear inside the same scope, with later statements |
| 6620 | * overriding earlier statements within that scope." |
| 6621 | * |
| 6622 | * Default precision specifications follow the same scope rules as |
Iago Toral Quiroga | e6629d8 | 2015-02-26 12:15:18 +0100 | [diff] [blame] | 6623 | * variables. So, we can track the state of the default precision |
| 6624 | * qualifiers in the symbol table, and the rules will just work. This |
Ian Romanick | cabd457 | 2013-08-09 15:17:18 -0700 | [diff] [blame] | 6625 | * is a slight abuse of the symbol table, but it has the semantics |
| 6626 | * that we want. |
| 6627 | */ |
Iago Toral Quiroga | e6629d8 | 2015-02-26 12:15:18 +0100 | [diff] [blame] | 6628 | state->symbols->add_default_precision_qualifier(this->type_name, |
| 6629 | this->default_precision); |
Ian Romanick | cabd457 | 2013-08-09 15:17:18 -0700 | [diff] [blame] | 6630 | } |
| 6631 | |
Chad Versace | 08a286c | 2011-01-16 21:44:57 -0800 | [diff] [blame] | 6632 | /* FINISHME: Translate precision statements into IR. */ |
| 6633 | return NULL; |
| 6634 | } |
| 6635 | |
Matt Turner | 1b0d6ae | 2013-06-29 19:29:16 -0700 | [diff] [blame] | 6636 | /* _mesa_ast_set_aggregate_type() sets the <structure> field so that |
| 6637 | * process_record_constructor() can do type-checking on C-style initializer |
| 6638 | * expressions of structs, but ast_struct_specifier should only be translated |
| 6639 | * to HIR if it is declaring the type of a structure. |
| 6640 | * |
| 6641 | * The ->is_declaration field is false for initializers of variables |
| 6642 | * declared separately from the struct's type definition. |
| 6643 | * |
| 6644 | * struct S { ... }; (is_declaration = true) |
| 6645 | * struct T { ... } t = { ... }; (is_declaration = true) |
| 6646 | * S s = { ... }; (is_declaration = false) |
| 6647 | */ |
| 6648 | if (this->structure != NULL && this->structure->is_declaration) |
Ian Romanick | 3455ce6 | 2010-04-19 15:13:15 -0700 | [diff] [blame] | 6649 | return this->structure->hir(instructions, state); |
Ian Romanick | 85ba37b | 2010-04-21 14:33:34 -0700 | [diff] [blame] | 6650 | |
| 6651 | return NULL; |
Ian Romanick | 3455ce6 | 2010-04-19 15:13:15 -0700 | [diff] [blame] | 6652 | } |
| 6653 | |
| 6654 | |
Ian Romanick | 51f740c | 2012-12-08 17:38:30 -0800 | [diff] [blame] | 6655 | /** |
| 6656 | * Process a structure or interface block tree into an array of structure fields |
| 6657 | * |
| 6658 | * After parsing, where there are some syntax differnces, structures and |
| 6659 | * interface blocks are almost identical. They are similar enough that the |
| 6660 | * AST for each can be processed the same way into a set of |
| 6661 | * \c glsl_struct_field to describe the members. |
| 6662 | * |
Paul Berry | e17d671 | 2013-10-22 15:03:29 -0700 | [diff] [blame] | 6663 | * If we're processing an interface block, var_mode should be the type of the |
Kristian Høgsberg | 84fc5fe | 2015-05-13 10:53:46 +0200 | [diff] [blame] | 6664 | * interface block (ir_var_shader_in, ir_var_shader_out, ir_var_uniform or |
| 6665 | * ir_var_shader_storage). If we're processing a structure, var_mode should be |
| 6666 | * ir_var_auto. |
Paul Berry | e17d671 | 2013-10-22 15:03:29 -0700 | [diff] [blame] | 6667 | * |
Ian Romanick | 51f740c | 2012-12-08 17:38:30 -0800 | [diff] [blame] | 6668 | * \return |
| 6669 | * The number of fields processed. A pointer to the array structure fields is |
| 6670 | * stored in \c *fields_ret. |
| 6671 | */ |
Emil Velikov | c704b89 | 2015-12-29 21:02:54 +1100 | [diff] [blame] | 6672 | static unsigned |
Timothy Arceri | 14d343b | 2015-11-13 09:49:31 +1100 | [diff] [blame] | 6673 | ast_process_struct_or_iface_block_members(exec_list *instructions, |
| 6674 | struct _mesa_glsl_parse_state *state, |
| 6675 | exec_list *declarations, |
Timothy Arceri | 14d343b | 2015-11-13 09:49:31 +1100 | [diff] [blame] | 6676 | glsl_struct_field **fields_ret, |
| 6677 | bool is_interface, |
| 6678 | enum glsl_matrix_layout matrix_layout, |
| 6679 | bool allow_reserved_names, |
| 6680 | ir_variable_mode var_mode, |
Timothy Arceri | 17e224e | 2015-11-13 18:47:55 +1100 | [diff] [blame] | 6681 | ast_type_qualifier *layout, |
Timothy Arceri | 0aeb9b3 | 2015-12-01 10:34:18 +1100 | [diff] [blame] | 6682 | unsigned block_stream, |
Timothy Arceri | f6a8c7e | 2016-03-11 23:00:16 +1100 | [diff] [blame] | 6683 | unsigned block_xfb_buffer, |
Timothy Arceri | edddad0 | 2016-02-24 15:21:59 +1100 | [diff] [blame] | 6684 | unsigned block_xfb_offset, |
Timothy Arceri | 037f68d | 2016-01-12 15:53:53 +1100 | [diff] [blame] | 6685 | unsigned expl_location, |
| 6686 | unsigned expl_align) |
Ian Romanick | 3455ce6 | 2010-04-19 15:13:15 -0700 | [diff] [blame] | 6687 | { |
Ian Romanick | 3455ce6 | 2010-04-19 15:13:15 -0700 | [diff] [blame] | 6688 | unsigned decl_count = 0; |
Timothy Arceri | 8abed7f | 2015-12-30 11:09:22 +1100 | [diff] [blame] | 6689 | unsigned next_offset = 0; |
Ian Romanick | 3455ce6 | 2010-04-19 15:13:15 -0700 | [diff] [blame] | 6690 | |
Ian Romanick | 51f740c | 2012-12-08 17:38:30 -0800 | [diff] [blame] | 6691 | /* Make an initial pass over the list of fields to determine how |
Ian Romanick | 3455ce6 | 2010-04-19 15:13:15 -0700 | [diff] [blame] | 6692 | * many there are. Each element in this list is an ast_declarator_list. |
| 6693 | * This means that we actually need to count the number of elements in the |
| 6694 | * 'declarations' list in each of the elements. |
| 6695 | */ |
Ian Romanick | 51f740c | 2012-12-08 17:38:30 -0800 | [diff] [blame] | 6696 | foreach_list_typed (ast_declarator_list, decl_list, link, declarations) { |
Connor Abbott | 58270c2 | 2014-07-08 12:21:00 -0700 | [diff] [blame] | 6697 | decl_count += decl_list->declarations.length(); |
Ian Romanick | 3455ce6 | 2010-04-19 15:13:15 -0700 | [diff] [blame] | 6698 | } |
| 6699 | |
Ian Romanick | 51f740c | 2012-12-08 17:38:30 -0800 | [diff] [blame] | 6700 | /* Allocate storage for the fields and process the field |
Ian Romanick | 3455ce6 | 2010-04-19 15:13:15 -0700 | [diff] [blame] | 6701 | * declarations. As the declarations are processed, try to also convert |
| 6702 | * the types to HIR. This ensures that structure definitions embedded in |
Ian Romanick | 51f740c | 2012-12-08 17:38:30 -0800 | [diff] [blame] | 6703 | * other structure definitions or in interface blocks are processed. |
Ian Romanick | 3455ce6 | 2010-04-19 15:13:15 -0700 | [diff] [blame] | 6704 | */ |
Marek Olšák | 52d2b28 | 2016-10-07 00:34:26 +0200 | [diff] [blame] | 6705 | glsl_struct_field *const fields = rzalloc_array(state, glsl_struct_field, |
| 6706 | decl_count); |
Ian Romanick | 3455ce6 | 2010-04-19 15:13:15 -0700 | [diff] [blame] | 6707 | |
Timothy Arceri | 0aeb9b3 | 2015-12-01 10:34:18 +1100 | [diff] [blame] | 6708 | bool first_member = true; |
Rob Clark | e93caca | 2016-02-16 13:35:58 -0500 | [diff] [blame] | 6709 | bool first_member_has_explicit_location = false; |
Timothy Arceri | 0aeb9b3 | 2015-12-01 10:34:18 +1100 | [diff] [blame] | 6710 | |
Ian Romanick | 3455ce6 | 2010-04-19 15:13:15 -0700 | [diff] [blame] | 6711 | unsigned i = 0; |
Ian Romanick | 51f740c | 2012-12-08 17:38:30 -0800 | [diff] [blame] | 6712 | foreach_list_typed (ast_declarator_list, decl_list, link, declarations) { |
Ian Romanick | 3455ce6 | 2010-04-19 15:13:15 -0700 | [diff] [blame] | 6713 | const char *type_name; |
Timothy Arceri | f8b5cc8 | 2015-11-13 10:49:48 +1100 | [diff] [blame] | 6714 | YYLTYPE loc = decl_list->get_location(); |
Ian Romanick | 3455ce6 | 2010-04-19 15:13:15 -0700 | [diff] [blame] | 6715 | |
| 6716 | decl_list->type->specifier->hir(instructions, state); |
| 6717 | |
Timothy Arceri | 98d3cc9 | 2016-02-11 15:45:05 +1100 | [diff] [blame] | 6718 | /* Section 4.1.8 (Structures) of the GLSL 1.10 spec says: |
| 6719 | * |
| 6720 | * "Anonymous structures are not supported; so embedded structures |
| 6721 | * must have a declarator. A name given to an embedded struct is |
| 6722 | * scoped at the same level as the struct it is embedded in." |
| 6723 | * |
| 6724 | * The same section of the GLSL 1.20 spec says: |
| 6725 | * |
| 6726 | * "Anonymous structures are not supported. Embedded structures are |
| 6727 | * not supported." |
| 6728 | * |
| 6729 | * The GLSL ES 1.00 and 3.00 specs have similar langauge. So, we allow |
| 6730 | * embedded structures in 1.10 only. |
Kenneth Graunke | c98deb1 | 2010-08-16 14:02:25 -0700 | [diff] [blame] | 6731 | */ |
Timothy Arceri | 98d3cc9 | 2016-02-11 15:45:05 +1100 | [diff] [blame] | 6732 | if (state->language_version != 110 && |
| 6733 | decl_list->type->specifier->structure != NULL) |
| 6734 | _mesa_glsl_error(&loc, state, |
| 6735 | "embedded structure declarations are not allowed"); |
Kenneth Graunke | c98deb1 | 2010-08-16 14:02:25 -0700 | [diff] [blame] | 6736 | |
Ian Romanick | 3455ce6 | 2010-04-19 15:13:15 -0700 | [diff] [blame] | 6737 | const glsl_type *decl_type = |
Ian Romanick | cabd457 | 2013-08-09 15:17:18 -0700 | [diff] [blame] | 6738 | decl_list->type->glsl_type(& type_name, state); |
Ian Romanick | 3455ce6 | 2010-04-19 15:13:15 -0700 | [diff] [blame] | 6739 | |
Timothy Arceri | c54865d | 2015-11-13 10:27:00 +1100 | [diff] [blame] | 6740 | const struct ast_type_qualifier *const qual = |
| 6741 | &decl_list->type->qualifier; |
| 6742 | |
| 6743 | /* From section 4.3.9 of the GLSL 4.40 spec: |
| 6744 | * |
| 6745 | * "[In interface blocks] opaque types are not allowed." |
| 6746 | * |
| 6747 | * It should be impossible for decl_type to be NULL here. Cases that |
| 6748 | * might naturally lead to decl_type being NULL, especially for the |
| 6749 | * is_interface case, will have resulted in compilation having |
| 6750 | * already halted due to a syntax error. |
| 6751 | */ |
| 6752 | assert(decl_type); |
| 6753 | |
Timothy Arceri | b600247 | 2016-02-11 15:14:21 +1100 | [diff] [blame] | 6754 | if (is_interface) { |
| 6755 | if (decl_type->contains_opaque()) { |
| 6756 | _mesa_glsl_error(&loc, state, "uniform/buffer in non-default " |
| 6757 | "interface block contains opaque variable"); |
| 6758 | } |
| 6759 | } else { |
| 6760 | if (decl_type->contains_atomic()) { |
| 6761 | /* From section 4.1.7.3 of the GLSL 4.40 spec: |
| 6762 | * |
| 6763 | * "Members of structures cannot be declared as atomic counter |
| 6764 | * types." |
| 6765 | */ |
| 6766 | _mesa_glsl_error(&loc, state, "atomic counter in structure"); |
| 6767 | } |
Timothy Arceri | c54865d | 2015-11-13 10:27:00 +1100 | [diff] [blame] | 6768 | |
Timothy Arceri | b600247 | 2016-02-11 15:14:21 +1100 | [diff] [blame] | 6769 | if (decl_type->contains_image()) { |
| 6770 | /* FINISHME: Same problem as with atomic counters. |
| 6771 | * FINISHME: Request clarification from Khronos and add |
| 6772 | * FINISHME: spec quotation here. |
| 6773 | */ |
| 6774 | _mesa_glsl_error(&loc, state, "image in structure"); |
| 6775 | } |
Timothy Arceri | c54865d | 2015-11-13 10:27:00 +1100 | [diff] [blame] | 6776 | } |
| 6777 | |
Timothy Arceri | 03bbddd | 2015-11-13 11:41:52 +1100 | [diff] [blame] | 6778 | if (qual->flags.q.explicit_binding) { |
| 6779 | _mesa_glsl_error(&loc, state, |
| 6780 | "binding layout qualifier cannot be applied " |
| 6781 | "to struct or interface block members"); |
| 6782 | } |
Timothy Arceri | c54865d | 2015-11-13 10:27:00 +1100 | [diff] [blame] | 6783 | |
Timothy Arceri | 0aeb9b3 | 2015-12-01 10:34:18 +1100 | [diff] [blame] | 6784 | if (is_interface) { |
| 6785 | if (!first_member) { |
| 6786 | if (!layout->flags.q.explicit_location && |
| 6787 | ((first_member_has_explicit_location && |
| 6788 | !qual->flags.q.explicit_location) || |
| 6789 | (!first_member_has_explicit_location && |
| 6790 | qual->flags.q.explicit_location))) { |
| 6791 | _mesa_glsl_error(&loc, state, |
| 6792 | "when block-level location layout qualifier " |
| 6793 | "is not supplied either all members must " |
| 6794 | "have a location layout qualifier or all " |
| 6795 | "members must not have a location layout " |
| 6796 | "qualifier"); |
| 6797 | } |
| 6798 | } else { |
| 6799 | first_member = false; |
| 6800 | first_member_has_explicit_location = |
| 6801 | qual->flags.q.explicit_location; |
| 6802 | } |
| 6803 | } |
| 6804 | |
Timothy Arceri | c54865d | 2015-11-13 10:27:00 +1100 | [diff] [blame] | 6805 | if (qual->flags.q.std140 || |
| 6806 | qual->flags.q.std430 || |
| 6807 | qual->flags.q.packed || |
| 6808 | qual->flags.q.shared) { |
| 6809 | _mesa_glsl_error(&loc, state, |
| 6810 | "uniform/shader storage block layout qualifiers " |
| 6811 | "std140, std430, packed, and shared can only be " |
| 6812 | "applied to uniform/shader storage blocks, not " |
| 6813 | "members"); |
| 6814 | } |
| 6815 | |
| 6816 | if (qual->flags.q.constant) { |
Timothy Arceri | c54865d | 2015-11-13 10:27:00 +1100 | [diff] [blame] | 6817 | _mesa_glsl_error(&loc, state, |
| 6818 | "const storage qualifier cannot be applied " |
| 6819 | "to struct or interface block members"); |
| 6820 | } |
| 6821 | |
| 6822 | /* From Section 4.4.2.3 (Geometry Outputs) of the GLSL 4.50 spec: |
| 6823 | * |
| 6824 | * "A block member may be declared with a stream identifier, but |
| 6825 | * the specified stream must match the stream associated with the |
| 6826 | * containing block." |
| 6827 | */ |
Timothy Arceri | 17e224e | 2015-11-13 18:47:55 +1100 | [diff] [blame] | 6828 | if (qual->flags.q.explicit_stream) { |
| 6829 | unsigned qual_stream; |
| 6830 | if (process_qualifier_constant(state, &loc, "stream", |
| 6831 | qual->stream, &qual_stream) && |
| 6832 | qual_stream != block_stream) { |
| 6833 | _mesa_glsl_error(&loc, state, "stream layout qualifier on " |
| 6834 | "interface block member does not match " |
Timothy Arceri | d018619 | 2016-01-19 14:35:50 +1100 | [diff] [blame] | 6835 | "the interface block (%u vs %u)", qual_stream, |
Timothy Arceri | 17e224e | 2015-11-13 18:47:55 +1100 | [diff] [blame] | 6836 | block_stream); |
| 6837 | } |
Timothy Arceri | c54865d | 2015-11-13 10:27:00 +1100 | [diff] [blame] | 6838 | } |
| 6839 | |
Timothy Arceri | f6a8c7e | 2016-03-11 23:00:16 +1100 | [diff] [blame] | 6840 | int xfb_buffer; |
| 6841 | unsigned explicit_xfb_buffer = 0; |
| 6842 | if (qual->flags.q.explicit_xfb_buffer) { |
| 6843 | unsigned qual_xfb_buffer; |
| 6844 | if (process_qualifier_constant(state, &loc, "xfb_buffer", |
| 6845 | qual->xfb_buffer, &qual_xfb_buffer)) { |
| 6846 | explicit_xfb_buffer = 1; |
| 6847 | if (qual_xfb_buffer != block_xfb_buffer) |
| 6848 | _mesa_glsl_error(&loc, state, "xfb_buffer layout qualifier on " |
| 6849 | "interface block member does not match " |
| 6850 | "the interface block (%u vs %u)", |
| 6851 | qual_xfb_buffer, block_xfb_buffer); |
| 6852 | } |
| 6853 | xfb_buffer = (int) qual_xfb_buffer; |
| 6854 | } else { |
| 6855 | if (layout) |
Dave Airlie | c952c0e | 2016-05-26 09:23:54 +1000 | [diff] [blame] | 6856 | explicit_xfb_buffer = layout->flags.q.explicit_xfb_buffer; |
Timothy Arceri | f6a8c7e | 2016-03-11 23:00:16 +1100 | [diff] [blame] | 6857 | xfb_buffer = (int) block_xfb_buffer; |
| 6858 | } |
| 6859 | |
Timothy Arceri | 04a72e6 | 2016-02-13 14:53:45 +1100 | [diff] [blame] | 6860 | int xfb_stride = -1; |
| 6861 | if (qual->flags.q.explicit_xfb_stride) { |
| 6862 | unsigned qual_xfb_stride; |
| 6863 | if (process_qualifier_constant(state, &loc, "xfb_stride", |
| 6864 | qual->xfb_stride, &qual_xfb_stride)) { |
| 6865 | xfb_stride = (int) qual_xfb_stride; |
| 6866 | } |
| 6867 | } |
| 6868 | |
Timothy Arceri | c54865d | 2015-11-13 10:27:00 +1100 | [diff] [blame] | 6869 | if (qual->flags.q.uniform && qual->has_interpolation()) { |
| 6870 | _mesa_glsl_error(&loc, state, |
| 6871 | "interpolation qualifiers cannot be used " |
| 6872 | "with uniform interface blocks"); |
| 6873 | } |
| 6874 | |
| 6875 | if ((qual->flags.q.uniform || !is_interface) && |
| 6876 | qual->has_auxiliary_storage()) { |
| 6877 | _mesa_glsl_error(&loc, state, |
| 6878 | "auxiliary storage qualifiers cannot be used " |
| 6879 | "in uniform blocks or structures."); |
| 6880 | } |
| 6881 | |
| 6882 | if (qual->flags.q.row_major || qual->flags.q.column_major) { |
| 6883 | if (!qual->flags.q.uniform && !qual->flags.q.buffer) { |
| 6884 | _mesa_glsl_error(&loc, state, |
| 6885 | "row_major and column_major can only be " |
| 6886 | "applied to interface blocks"); |
| 6887 | } else |
| 6888 | validate_matrix_layout_for_type(state, &loc, decl_type, NULL); |
| 6889 | } |
| 6890 | |
| 6891 | if (qual->flags.q.read_only && qual->flags.q.write_only) { |
| 6892 | _mesa_glsl_error(&loc, state, "buffer variable can't be both " |
| 6893 | "readonly and writeonly."); |
| 6894 | } |
| 6895 | |
Ian Romanick | 2b97dc6 | 2010-05-10 17:42:05 -0700 | [diff] [blame] | 6896 | foreach_list_typed (ast_declaration, decl, link, |
Iago Toral Quiroga | 4472ab9 | 2014-04-14 09:14:23 +0200 | [diff] [blame] | 6897 | &decl_list->declarations) { |
Timothy Arceri | f8b5cc8 | 2015-11-13 10:49:48 +1100 | [diff] [blame] | 6898 | YYLTYPE loc = decl->get_location(); |
| 6899 | |
Paul Berry | 9b5b032 | 2013-09-27 14:52:08 -0700 | [diff] [blame] | 6900 | if (!allow_reserved_names) |
| 6901 | validate_identifier(decl->identifier, loc, state); |
| 6902 | |
Iago Toral Quiroga | 12d510a | 2015-09-28 12:59:35 +0200 | [diff] [blame] | 6903 | const struct glsl_type *field_type = |
| 6904 | process_array_type(&loc, decl_type, decl->array_specifier, state); |
Timothy Arceri | dea0af8 | 2015-10-15 14:35:41 +1100 | [diff] [blame] | 6905 | validate_array_dimensions(field_type, state, &loc); |
Ian Romanick | 278c9af | 2013-04-08 16:37:04 -0700 | [diff] [blame] | 6906 | fields[i].type = field_type; |
Iago Toral Quiroga | 4472ab9 | 2014-04-14 09:14:23 +0200 | [diff] [blame] | 6907 | fields[i].name = decl->identifier; |
Paul Berry | 99512dc | 2013-10-22 15:11:51 -0700 | [diff] [blame] | 6908 | fields[i].interpolation = |
Andres Gomez | c750029 | 2016-03-24 01:13:26 +0200 | [diff] [blame] | 6909 | interpret_interpolation_qualifier(qual, field_type, |
| 6910 | var_mode, state, &loc); |
Paul Berry | 99512dc | 2013-10-22 15:11:51 -0700 | [diff] [blame] | 6911 | fields[i].centroid = qual->flags.q.centroid ? 1 : 0; |
Chris Forbes | 51c5fc8 | 2013-11-29 21:26:10 +1300 | [diff] [blame] | 6912 | fields[i].sample = qual->flags.q.sample ? 1 : 0; |
Fabian Bieler | 1009b33 | 2014-03-05 13:43:17 +0100 | [diff] [blame] | 6913 | fields[i].patch = qual->flags.q.patch ? 1 : 0; |
Iago Toral Quiroga | f84bc57 | 2015-11-10 08:22:07 +0200 | [diff] [blame] | 6914 | fields[i].precision = qual->precision; |
Timothy Arceri | edddad0 | 2016-02-24 15:21:59 +1100 | [diff] [blame] | 6915 | fields[i].offset = -1; |
Timothy Arceri | f6a8c7e | 2016-03-11 23:00:16 +1100 | [diff] [blame] | 6916 | fields[i].explicit_xfb_buffer = explicit_xfb_buffer; |
| 6917 | fields[i].xfb_buffer = xfb_buffer; |
Timothy Arceri | 04a72e6 | 2016-02-13 14:53:45 +1100 | [diff] [blame] | 6918 | fields[i].xfb_stride = xfb_stride; |
Ian Romanick | 17e6f19 | 2012-12-11 12:14:03 -0800 | [diff] [blame] | 6919 | |
Timothy Arceri | 0aeb9b3 | 2015-12-01 10:34:18 +1100 | [diff] [blame] | 6920 | if (qual->flags.q.explicit_location) { |
| 6921 | unsigned qual_location; |
| 6922 | if (process_qualifier_constant(state, &loc, "location", |
| 6923 | qual->location, &qual_location)) { |
Kenneth Graunke | d0cd504 | 2016-09-03 10:51:07 -0700 | [diff] [blame] | 6924 | fields[i].location = qual_location + |
| 6925 | (fields[i].patch ? VARYING_SLOT_PATCH0 : VARYING_SLOT_VAR0); |
Timothy Arceri | e58be8a | 2016-01-06 20:22:46 +1100 | [diff] [blame] | 6926 | expl_location = fields[i].location + |
| 6927 | fields[i].type->count_attribute_slots(false); |
Timothy Arceri | 0aeb9b3 | 2015-12-01 10:34:18 +1100 | [diff] [blame] | 6928 | } |
| 6929 | } else { |
| 6930 | if (layout && layout->flags.q.explicit_location) { |
| 6931 | fields[i].location = expl_location; |
Timothy Arceri | e58be8a | 2016-01-06 20:22:46 +1100 | [diff] [blame] | 6932 | expl_location += fields[i].type->count_attribute_slots(false); |
Timothy Arceri | 0aeb9b3 | 2015-12-01 10:34:18 +1100 | [diff] [blame] | 6933 | } else { |
| 6934 | fields[i].location = -1; |
| 6935 | } |
| 6936 | } |
| 6937 | |
Timothy Arceri | 8abed7f | 2015-12-30 11:09:22 +1100 | [diff] [blame] | 6938 | /* Offset can only be used with std430 and std140 layouts an initial |
| 6939 | * value of 0 is used for error detection. |
| 6940 | */ |
| 6941 | unsigned align = 0; |
| 6942 | unsigned size = 0; |
| 6943 | if (layout) { |
| 6944 | bool row_major; |
| 6945 | if (qual->flags.q.row_major || |
| 6946 | matrix_layout == GLSL_MATRIX_LAYOUT_ROW_MAJOR) { |
| 6947 | row_major = true; |
| 6948 | } else { |
| 6949 | row_major = false; |
| 6950 | } |
| 6951 | |
| 6952 | if(layout->flags.q.std140) { |
| 6953 | align = field_type->std140_base_alignment(row_major); |
| 6954 | size = field_type->std140_size(row_major); |
| 6955 | } else if (layout->flags.q.std430) { |
| 6956 | align = field_type->std430_base_alignment(row_major); |
| 6957 | size = field_type->std430_size(row_major); |
| 6958 | } |
| 6959 | } |
| 6960 | |
| 6961 | if (qual->flags.q.explicit_offset) { |
| 6962 | unsigned qual_offset; |
| 6963 | if (process_qualifier_constant(state, &loc, "offset", |
| 6964 | qual->offset, &qual_offset)) { |
| 6965 | if (align != 0 && size != 0) { |
| 6966 | if (next_offset > qual_offset) |
| 6967 | _mesa_glsl_error(&loc, state, "layout qualifier " |
| 6968 | "offset overlaps previous member"); |
| 6969 | |
| 6970 | if (qual_offset % align) { |
| 6971 | _mesa_glsl_error(&loc, state, "layout qualifier offset " |
| 6972 | "must be a multiple of the base " |
| 6973 | "alignment of %s", field_type->name); |
| 6974 | } |
Timothy Arceri | 9f24f42 | 2015-12-30 13:31:24 +1100 | [diff] [blame] | 6975 | fields[i].offset = qual_offset; |
Timothy Arceri | 8abed7f | 2015-12-30 11:09:22 +1100 | [diff] [blame] | 6976 | next_offset = glsl_align(qual_offset + size, align); |
| 6977 | } else { |
| 6978 | _mesa_glsl_error(&loc, state, "offset can only be used " |
| 6979 | "with std430 and std140 layouts"); |
| 6980 | } |
| 6981 | } |
Timothy Arceri | 037f68d | 2016-01-12 15:53:53 +1100 | [diff] [blame] | 6982 | } |
| 6983 | |
| 6984 | if (qual->flags.q.explicit_align || expl_align != 0) { |
| 6985 | unsigned offset = fields[i].offset != -1 ? fields[i].offset : |
| 6986 | next_offset; |
| 6987 | if (align == 0 || size == 0) { |
| 6988 | _mesa_glsl_error(&loc, state, "align can only be used with " |
| 6989 | "std430 and std140 layouts"); |
| 6990 | } else if (qual->flags.q.explicit_align) { |
| 6991 | unsigned member_align; |
| 6992 | if (process_qualifier_constant(state, &loc, "align", |
| 6993 | qual->align, &member_align)) { |
| 6994 | if (member_align == 0 || |
| 6995 | member_align & (member_align - 1)) { |
| 6996 | _mesa_glsl_error(&loc, state, "align layout qualifier " |
| 6997 | "in not a power of 2"); |
| 6998 | } else { |
| 6999 | fields[i].offset = glsl_align(offset, member_align); |
| 7000 | next_offset = glsl_align(fields[i].offset + size, align); |
| 7001 | } |
| 7002 | } |
| 7003 | } else { |
| 7004 | fields[i].offset = glsl_align(offset, expl_align); |
| 7005 | next_offset = glsl_align(fields[i].offset + size, align); |
| 7006 | } |
Timothy Arceri | 8f4ac20 | 2016-05-28 11:40:22 +1000 | [diff] [blame] | 7007 | } else if (!qual->flags.q.explicit_offset) { |
Timothy Arceri | 8abed7f | 2015-12-30 11:09:22 +1100 | [diff] [blame] | 7008 | if (align != 0 && size != 0) |
| 7009 | next_offset = glsl_align(next_offset + size, align); |
| 7010 | } |
| 7011 | |
Timothy Arceri | edddad0 | 2016-02-24 15:21:59 +1100 | [diff] [blame] | 7012 | /* From the ARB_enhanced_layouts spec: |
| 7013 | * |
| 7014 | * "The given offset applies to the first component of the first |
| 7015 | * member of the qualified entity. Then, within the qualified |
| 7016 | * entity, subsequent components are each assigned, in order, to |
| 7017 | * the next available offset aligned to a multiple of that |
| 7018 | * component's size. Aggregate types are flattened down to the |
| 7019 | * component level to get this sequence of components." |
| 7020 | */ |
| 7021 | if (qual->flags.q.explicit_xfb_offset) { |
| 7022 | unsigned xfb_offset; |
| 7023 | if (process_qualifier_constant(state, &loc, "xfb_offset", |
| 7024 | qual->offset, &xfb_offset)) { |
| 7025 | fields[i].offset = xfb_offset; |
| 7026 | block_xfb_offset = fields[i].offset + |
Timothy Arceri | 598790e | 2016-03-10 11:51:48 +1100 | [diff] [blame] | 7027 | MAX2(xfb_stride, (int) (4 * field_type->component_slots())); |
Timothy Arceri | edddad0 | 2016-02-24 15:21:59 +1100 | [diff] [blame] | 7028 | } |
| 7029 | } else { |
| 7030 | if (layout && layout->flags.q.explicit_xfb_offset) { |
Dave Airlie | 35616a9 | 2016-06-09 07:02:07 +1000 | [diff] [blame] | 7031 | unsigned align = field_type->is_64bit() ? 8 : 4; |
Timothy Arceri | edddad0 | 2016-02-24 15:21:59 +1100 | [diff] [blame] | 7032 | fields[i].offset = glsl_align(block_xfb_offset, align); |
Timothy Arceri | 598790e | 2016-03-10 11:51:48 +1100 | [diff] [blame] | 7033 | block_xfb_offset += |
| 7034 | MAX2(xfb_stride, (int) (4 * field_type->component_slots())); |
Timothy Arceri | edddad0 | 2016-02-24 15:21:59 +1100 | [diff] [blame] | 7035 | } |
| 7036 | } |
| 7037 | |
Ian Romanick | 68fa4ca | 2014-07-18 10:27:21 -0700 | [diff] [blame] | 7038 | /* Propogate row- / column-major information down the fields of the |
| 7039 | * structure or interface block. Structures need this data because |
| 7040 | * the structure may contain a structure that contains ... a matrix |
| 7041 | * that need the proper layout. |
| 7042 | */ |
Matt Turner | 6a4ff51 | 2016-05-16 14:49:38 -0700 | [diff] [blame] | 7043 | if (is_interface && layout && |
Timothy Arceri | d244986 | 2016-02-14 16:18:36 +1100 | [diff] [blame] | 7044 | (layout->flags.q.uniform || layout->flags.q.buffer) && |
| 7045 | (field_type->without_array()->is_matrix() |
| 7046 | || field_type->without_array()->is_record())) { |
Ian Romanick | d561e79 | 2014-07-18 11:23:06 -0700 | [diff] [blame] | 7047 | /* If no layout is specified for the field, inherit the layout |
| 7048 | * from the block. |
| 7049 | */ |
| 7050 | fields[i].matrix_layout = matrix_layout; |
| 7051 | |
Ian Romanick | 17e6f19 | 2012-12-11 12:14:03 -0800 | [diff] [blame] | 7052 | if (qual->flags.q.row_major) |
Ian Romanick | 814d694 | 2014-07-16 16:51:14 -0700 | [diff] [blame] | 7053 | fields[i].matrix_layout = GLSL_MATRIX_LAYOUT_ROW_MAJOR; |
Ian Romanick | 17e6f19 | 2012-12-11 12:14:03 -0800 | [diff] [blame] | 7054 | else if (qual->flags.q.column_major) |
Ian Romanick | 814d694 | 2014-07-16 16:51:14 -0700 | [diff] [blame] | 7055 | fields[i].matrix_layout = GLSL_MATRIX_LAYOUT_COLUMN_MAJOR; |
Ian Romanick | d561e79 | 2014-07-18 11:23:06 -0700 | [diff] [blame] | 7056 | |
Timothy Arceri | d244986 | 2016-02-14 16:18:36 +1100 | [diff] [blame] | 7057 | /* If we're processing an uniform or buffer block, the matrix |
| 7058 | * layout must be decided by this point. |
Ian Romanick | d561e79 | 2014-07-18 11:23:06 -0700 | [diff] [blame] | 7059 | */ |
Timothy Arceri | d244986 | 2016-02-14 16:18:36 +1100 | [diff] [blame] | 7060 | assert(fields[i].matrix_layout == GLSL_MATRIX_LAYOUT_ROW_MAJOR |
Ian Romanick | d561e79 | 2014-07-18 11:23:06 -0700 | [diff] [blame] | 7061 | || fields[i].matrix_layout == GLSL_MATRIX_LAYOUT_COLUMN_MAJOR); |
Ian Romanick | 17e6f19 | 2012-12-11 12:14:03 -0800 | [diff] [blame] | 7062 | } |
| 7063 | |
Iago Toral Quiroga | f1b647f | 2015-04-28 12:09:58 +0200 | [diff] [blame] | 7064 | /* Image qualifiers are allowed on buffer variables, which can only |
| 7065 | * be defined inside shader storage buffer objects |
| 7066 | */ |
| 7067 | if (layout && var_mode == ir_var_shader_storage) { |
Iago Toral Quiroga | f1b647f | 2015-04-28 12:09:58 +0200 | [diff] [blame] | 7068 | /* For readonly and writeonly qualifiers the field definition, |
| 7069 | * if set, overwrites the layout qualifier. |
| 7070 | */ |
Iago Toral Quiroga | f1b647f | 2015-04-28 12:09:58 +0200 | [diff] [blame] | 7071 | if (qual->flags.q.read_only) { |
Timothy Arceri | 4f4ca6b | 2015-11-13 11:28:20 +1100 | [diff] [blame] | 7072 | fields[i].image_read_only = true; |
| 7073 | fields[i].image_write_only = false; |
Iago Toral Quiroga | f1b647f | 2015-04-28 12:09:58 +0200 | [diff] [blame] | 7074 | } else if (qual->flags.q.write_only) { |
Timothy Arceri | 4f4ca6b | 2015-11-13 11:28:20 +1100 | [diff] [blame] | 7075 | fields[i].image_read_only = false; |
| 7076 | fields[i].image_write_only = true; |
| 7077 | } else { |
| 7078 | fields[i].image_read_only = layout->flags.q.read_only; |
| 7079 | fields[i].image_write_only = layout->flags.q.write_only; |
Iago Toral Quiroga | f1b647f | 2015-04-28 12:09:58 +0200 | [diff] [blame] | 7080 | } |
| 7081 | |
Iago Toral Quiroga | f1b647f | 2015-04-28 12:09:58 +0200 | [diff] [blame] | 7082 | /* For other qualifiers, we set the flag if either the layout |
| 7083 | * qualifier or the field qualifier are set |
| 7084 | */ |
| 7085 | fields[i].image_coherent = qual->flags.q.coherent || |
| 7086 | layout->flags.q.coherent; |
| 7087 | fields[i].image_volatile = qual->flags.q._volatile || |
| 7088 | layout->flags.q._volatile; |
| 7089 | fields[i].image_restrict = qual->flags.q.restrict_flag || |
| 7090 | layout->flags.q.restrict_flag; |
| 7091 | } |
| 7092 | |
Ian Romanick | 01c21c4 | 2014-06-25 17:24:13 -0700 | [diff] [blame] | 7093 | i++; |
Ian Romanick | 3455ce6 | 2010-04-19 15:13:15 -0700 | [diff] [blame] | 7094 | } |
| 7095 | } |
| 7096 | |
| 7097 | assert(i == decl_count); |
| 7098 | |
Ian Romanick | 51f740c | 2012-12-08 17:38:30 -0800 | [diff] [blame] | 7099 | *fields_ret = fields; |
| 7100 | return decl_count; |
| 7101 | } |
| 7102 | |
| 7103 | |
| 7104 | ir_rvalue * |
| 7105 | ast_struct_specifier::hir(exec_list *instructions, |
Iago Toral Quiroga | 4472ab9 | 2014-04-14 09:14:23 +0200 | [diff] [blame] | 7106 | struct _mesa_glsl_parse_state *state) |
Ian Romanick | 51f740c | 2012-12-08 17:38:30 -0800 | [diff] [blame] | 7107 | { |
| 7108 | YYLTYPE loc = this->get_location(); |
Ian Romanick | d9bb8b7 | 2013-08-13 09:15:01 -0700 | [diff] [blame] | 7109 | |
Timothy Arceri | e58be8a | 2016-01-06 20:22:46 +1100 | [diff] [blame] | 7110 | unsigned expl_location = 0; |
Timothy Arceri | 0aeb9b3 | 2015-12-01 10:34:18 +1100 | [diff] [blame] | 7111 | if (layout && layout->flags.q.explicit_location) { |
| 7112 | if (!process_qualifier_constant(state, &loc, "location", |
| 7113 | layout->location, &expl_location)) { |
| 7114 | return NULL; |
| 7115 | } else { |
| 7116 | expl_location = VARYING_SLOT_VAR0 + expl_location; |
| 7117 | } |
| 7118 | } |
| 7119 | |
Ian Romanick | 51f740c | 2012-12-08 17:38:30 -0800 | [diff] [blame] | 7120 | glsl_struct_field *fields; |
| 7121 | unsigned decl_count = |
Timothy Arceri | 14d343b | 2015-11-13 09:49:31 +1100 | [diff] [blame] | 7122 | ast_process_struct_or_iface_block_members(instructions, |
| 7123 | state, |
| 7124 | &this->declarations, |
Timothy Arceri | 14d343b | 2015-11-13 09:49:31 +1100 | [diff] [blame] | 7125 | &fields, |
| 7126 | false, |
| 7127 | GLSL_MATRIX_LAYOUT_INHERITED, |
| 7128 | false /* allow_reserved_names */, |
| 7129 | ir_var_auto, |
Timothy Arceri | 0aeb9b3 | 2015-12-01 10:34:18 +1100 | [diff] [blame] | 7130 | layout, |
| 7131 | 0, /* for interface only */ |
Timothy Arceri | f6a8c7e | 2016-03-11 23:00:16 +1100 | [diff] [blame] | 7132 | 0, /* for interface only */ |
Timothy Arceri | edddad0 | 2016-02-24 15:21:59 +1100 | [diff] [blame] | 7133 | 0, /* for interface only */ |
Timothy Arceri | 037f68d | 2016-01-12 15:53:53 +1100 | [diff] [blame] | 7134 | expl_location, |
| 7135 | 0 /* for interface only */); |
Ian Romanick | 51f740c | 2012-12-08 17:38:30 -0800 | [diff] [blame] | 7136 | |
Paul Berry | 78b072b | 2013-09-27 17:47:02 -0700 | [diff] [blame] | 7137 | validate_identifier(this->name, loc, state); |
| 7138 | |
Ian Romanick | 49e3577 | 2010-06-28 11:54:57 -0700 | [diff] [blame] | 7139 | const glsl_type *t = |
Kenneth Graunke | ca92ae2 | 2010-09-18 11:11:09 +0200 | [diff] [blame] | 7140 | glsl_type::get_record_instance(fields, decl_count, this->name); |
Ian Romanick | 1d28b61 | 2010-04-20 16:48:24 -0700 | [diff] [blame] | 7141 | |
Ian Romanick | a789ca6 | 2010-09-01 14:08:08 -0700 | [diff] [blame] | 7142 | if (!state->symbols->add_type(name, t)) { |
Dave Airlie | 3ca1c221 | 2016-05-17 10:58:53 +1000 | [diff] [blame] | 7143 | const glsl_type *match = state->symbols->get_type(name); |
| 7144 | /* allow struct matching for desktop GL - older UE4 does this */ |
Mark Janes | 9ca5ec2 | 2016-05-20 08:50:39 -0700 | [diff] [blame] | 7145 | if (match != NULL && state->is_version(130, 0) && match->record_compare(t, false)) |
Dave Airlie | 3ca1c221 | 2016-05-17 10:58:53 +1000 | [diff] [blame] | 7146 | _mesa_glsl_warning(& loc, state, "struct `%s' previously defined", name); |
| 7147 | else |
| 7148 | _mesa_glsl_error(& loc, state, "struct `%s' previously defined", name); |
Ian Romanick | ab89927 | 2010-04-23 13:24:08 -0700 | [diff] [blame] | 7149 | } else { |
Kenneth Graunke | eb63934 | 2011-02-27 01:17:29 -0800 | [diff] [blame] | 7150 | const glsl_type **s = reralloc(state, state->user_structures, |
Iago Toral Quiroga | 4472ab9 | 2014-04-14 09:14:23 +0200 | [diff] [blame] | 7151 | const glsl_type *, |
| 7152 | state->num_user_structures + 1); |
Ian Romanick | a2c6df5 | 2010-04-28 13:14:53 -0700 | [diff] [blame] | 7153 | if (s != NULL) { |
Iago Toral Quiroga | 4472ab9 | 2014-04-14 09:14:23 +0200 | [diff] [blame] | 7154 | s[state->num_user_structures] = t; |
| 7155 | state->user_structures = s; |
| 7156 | state->num_user_structures++; |
Ian Romanick | a2c6df5 | 2010-04-28 13:14:53 -0700 | [diff] [blame] | 7157 | } |
Ian Romanick | ab89927 | 2010-04-23 13:24:08 -0700 | [diff] [blame] | 7158 | } |
Ian Romanick | 3455ce6 | 2010-04-19 15:13:15 -0700 | [diff] [blame] | 7159 | |
| 7160 | /* Structure type definitions do not have r-values. |
| 7161 | */ |
| 7162 | return NULL; |
| 7163 | } |
Eric Anholt | 4b2a4cb | 2012-03-29 17:29:20 -0700 | [diff] [blame] | 7164 | |
Paul Berry | 8cb9cce | 2013-10-01 15:48:07 -0700 | [diff] [blame] | 7165 | |
| 7166 | /** |
| 7167 | * Visitor class which detects whether a given interface block has been used. |
| 7168 | */ |
| 7169 | class interface_block_usage_visitor : public ir_hierarchical_visitor |
| 7170 | { |
| 7171 | public: |
| 7172 | interface_block_usage_visitor(ir_variable_mode mode, const glsl_type *block) |
| 7173 | : mode(mode), block(block), found(false) |
| 7174 | { |
| 7175 | } |
| 7176 | |
| 7177 | virtual ir_visitor_status visit(ir_dereference_variable *ir) |
| 7178 | { |
Tapani Pälli | 33ee2c6 | 2013-12-12 13:51:01 +0200 | [diff] [blame] | 7179 | if (ir->var->data.mode == mode && ir->var->get_interface_type() == block) { |
Paul Berry | 8cb9cce | 2013-10-01 15:48:07 -0700 | [diff] [blame] | 7180 | found = true; |
| 7181 | return visit_stop; |
| 7182 | } |
| 7183 | return visit_continue; |
| 7184 | } |
| 7185 | |
| 7186 | bool usage_found() const |
| 7187 | { |
| 7188 | return this->found; |
| 7189 | } |
| 7190 | |
| 7191 | private: |
| 7192 | ir_variable_mode mode; |
| 7193 | const glsl_type *block; |
| 7194 | bool found; |
| 7195 | }; |
| 7196 | |
Samuel Iglesias Gonsalvez | f3f64cd | 2015-03-18 15:32:03 +0100 | [diff] [blame] | 7197 | static bool |
| 7198 | is_unsized_array_last_element(ir_variable *v) |
| 7199 | { |
| 7200 | const glsl_type *interface_type = v->get_interface_type(); |
| 7201 | int length = interface_type->length; |
| 7202 | |
| 7203 | assert(v->type->is_unsized_array()); |
| 7204 | |
| 7205 | /* Check if it is the last element of the interface */ |
| 7206 | if (strcmp(interface_type->fields.structure[length-1].name, v->name) == 0) |
| 7207 | return true; |
| 7208 | return false; |
| 7209 | } |
Paul Berry | 8cb9cce | 2013-10-01 15:48:07 -0700 | [diff] [blame] | 7210 | |
Eduardo Lima Mitev | 7f7f58f | 2016-05-05 13:52:37 +0200 | [diff] [blame] | 7211 | static void |
| 7212 | apply_memory_qualifiers(ir_variable *var, glsl_struct_field field) |
| 7213 | { |
| 7214 | var->data.image_read_only = field.image_read_only; |
| 7215 | var->data.image_write_only = field.image_write_only; |
| 7216 | var->data.image_coherent = field.image_coherent; |
| 7217 | var->data.image_volatile = field.image_volatile; |
| 7218 | var->data.image_restrict = field.image_restrict; |
| 7219 | } |
| 7220 | |
Eric Anholt | 2d03f48 | 2012-04-18 13:35:56 -0700 | [diff] [blame] | 7221 | ir_rvalue * |
Jordan Justen | c9f5854 | 2013-03-09 10:40:41 -0800 | [diff] [blame] | 7222 | ast_interface_block::hir(exec_list *instructions, |
Iago Toral Quiroga | 4472ab9 | 2014-04-14 09:14:23 +0200 | [diff] [blame] | 7223 | struct _mesa_glsl_parse_state *state) |
Eric Anholt | 2d03f48 | 2012-04-18 13:35:56 -0700 | [diff] [blame] | 7224 | { |
Ian Romanick | 17e6f19 | 2012-12-11 12:14:03 -0800 | [diff] [blame] | 7225 | YYLTYPE loc = this->get_location(); |
| 7226 | |
Iago Toral Quiroga | 5d655a4 | 2015-01-19 12:32:10 +0100 | [diff] [blame] | 7227 | /* Interface blocks must be declared at global scope */ |
| 7228 | if (state->current_function != NULL) { |
| 7229 | _mesa_glsl_error(&loc, state, |
| 7230 | "Interface block `%s' must be declared " |
| 7231 | "at global scope", |
| 7232 | this->block_name); |
| 7233 | } |
| 7234 | |
Timothy Arceri | f696b71 | 2016-07-30 14:51:21 +1000 | [diff] [blame] | 7235 | /* Validate qualifiers: |
| 7236 | * |
| 7237 | * - Layout Qualifiers as per the table in Section 4.4 |
| 7238 | * ("Layout Qualifiers") of the GLSL 4.50 spec. |
| 7239 | * |
| 7240 | * - Memory Qualifiers as per Section 4.10 ("Memory Qualifiers") of the |
| 7241 | * GLSL 4.50 spec: |
| 7242 | * |
| 7243 | * "Additionally, memory qualifiers may also be used in the declaration |
| 7244 | * of shader storage blocks" |
| 7245 | * |
| 7246 | * Note the table in Section 4.4 says std430 is allowed on both uniform and |
| 7247 | * buffer blocks however Section 4.4.5 (Uniform and Shader Storage Block |
| 7248 | * Layout Qualifiers) of the GLSL 4.50 spec says: |
| 7249 | * |
| 7250 | * "The std430 qualifier is supported only for shader storage blocks; |
| 7251 | * using std430 on a uniform block will result in a compile-time error." |
| 7252 | */ |
| 7253 | ast_type_qualifier allowed_blk_qualifiers; |
| 7254 | allowed_blk_qualifiers.flags.i = 0; |
| 7255 | if (this->layout.flags.q.buffer || this->layout.flags.q.uniform) { |
| 7256 | allowed_blk_qualifiers.flags.q.shared = 1; |
| 7257 | allowed_blk_qualifiers.flags.q.packed = 1; |
| 7258 | allowed_blk_qualifiers.flags.q.std140 = 1; |
| 7259 | allowed_blk_qualifiers.flags.q.row_major = 1; |
| 7260 | allowed_blk_qualifiers.flags.q.column_major = 1; |
| 7261 | allowed_blk_qualifiers.flags.q.explicit_align = 1; |
| 7262 | allowed_blk_qualifiers.flags.q.explicit_binding = 1; |
| 7263 | if (this->layout.flags.q.buffer) { |
| 7264 | allowed_blk_qualifiers.flags.q.buffer = 1; |
| 7265 | allowed_blk_qualifiers.flags.q.std430 = 1; |
| 7266 | allowed_blk_qualifiers.flags.q.coherent = 1; |
| 7267 | allowed_blk_qualifiers.flags.q._volatile = 1; |
| 7268 | allowed_blk_qualifiers.flags.q.restrict_flag = 1; |
| 7269 | allowed_blk_qualifiers.flags.q.read_only = 1; |
| 7270 | allowed_blk_qualifiers.flags.q.write_only = 1; |
| 7271 | } else { |
| 7272 | allowed_blk_qualifiers.flags.q.uniform = 1; |
| 7273 | } |
| 7274 | } else { |
| 7275 | /* Interface block */ |
| 7276 | assert(this->layout.flags.q.in || this->layout.flags.q.out); |
| 7277 | |
| 7278 | allowed_blk_qualifiers.flags.q.explicit_location = 1; |
| 7279 | if (this->layout.flags.q.out) { |
| 7280 | allowed_blk_qualifiers.flags.q.out = 1; |
| 7281 | if (state->stage == MESA_SHADER_GEOMETRY || |
| 7282 | state->stage == MESA_SHADER_TESS_CTRL || |
| 7283 | state->stage == MESA_SHADER_TESS_EVAL || |
| 7284 | state->stage == MESA_SHADER_VERTEX ) { |
| 7285 | allowed_blk_qualifiers.flags.q.explicit_xfb_offset = 1; |
| 7286 | allowed_blk_qualifiers.flags.q.explicit_xfb_buffer = 1; |
| 7287 | allowed_blk_qualifiers.flags.q.xfb_buffer = 1; |
| 7288 | allowed_blk_qualifiers.flags.q.explicit_xfb_stride = 1; |
| 7289 | allowed_blk_qualifiers.flags.q.xfb_stride = 1; |
| 7290 | if (state->stage == MESA_SHADER_GEOMETRY) { |
| 7291 | allowed_blk_qualifiers.flags.q.stream = 1; |
| 7292 | allowed_blk_qualifiers.flags.q.explicit_stream = 1; |
| 7293 | } |
| 7294 | if (state->stage == MESA_SHADER_TESS_CTRL) { |
| 7295 | allowed_blk_qualifiers.flags.q.patch = 1; |
| 7296 | } |
| 7297 | } |
| 7298 | } else { |
| 7299 | allowed_blk_qualifiers.flags.q.in = 1; |
| 7300 | if (state->stage == MESA_SHADER_TESS_EVAL) { |
| 7301 | allowed_blk_qualifiers.flags.q.patch = 1; |
| 7302 | } |
| 7303 | } |
Samuel Iglesias Gonsalvez | 8f0167c | 2015-08-31 07:45:53 +0200 | [diff] [blame] | 7304 | } |
| 7305 | |
Timothy Arceri | f696b71 | 2016-07-30 14:51:21 +1000 | [diff] [blame] | 7306 | this->layout.validate_flags(&loc, state, allowed_blk_qualifiers, |
| 7307 | "invalid qualifier for block", |
| 7308 | this->block_name); |
| 7309 | |
Jordan Justen | c9f5854 | 2013-03-09 10:40:41 -0800 | [diff] [blame] | 7310 | /* The ast_interface_block has a list of ast_declarator_lists. We |
Eric Anholt | 2d03f48 | 2012-04-18 13:35:56 -0700 | [diff] [blame] | 7311 | * need to turn those into ir_variables with an association |
| 7312 | * with this uniform block. |
| 7313 | */ |
Ian Romanick | 514f8c7 | 2013-01-22 01:09:16 -0500 | [diff] [blame] | 7314 | enum glsl_interface_packing packing; |
Ian Romanick | 9a971ab | 2012-12-13 02:13:30 -0800 | [diff] [blame] | 7315 | if (this->layout.flags.q.shared) { |
Ian Romanick | 514f8c7 | 2013-01-22 01:09:16 -0500 | [diff] [blame] | 7316 | packing = GLSL_INTERFACE_PACKING_SHARED; |
Ian Romanick | 9a971ab | 2012-12-13 02:13:30 -0800 | [diff] [blame] | 7317 | } else if (this->layout.flags.q.packed) { |
Ian Romanick | 514f8c7 | 2013-01-22 01:09:16 -0500 | [diff] [blame] | 7318 | packing = GLSL_INTERFACE_PACKING_PACKED; |
Samuel Iglesias Gonsalvez | 8f0167c | 2015-08-31 07:45:53 +0200 | [diff] [blame] | 7319 | } else if (this->layout.flags.q.std430) { |
| 7320 | packing = GLSL_INTERFACE_PACKING_STD430; |
Ian Romanick | 9a971ab | 2012-12-13 02:13:30 -0800 | [diff] [blame] | 7321 | } else { |
| 7322 | /* The default layout is std140. |
| 7323 | */ |
Ian Romanick | 514f8c7 | 2013-01-22 01:09:16 -0500 | [diff] [blame] | 7324 | packing = GLSL_INTERFACE_PACKING_STD140; |
Ian Romanick | 9a971ab | 2012-12-13 02:13:30 -0800 | [diff] [blame] | 7325 | } |
| 7326 | |
Jordan Justen | b24eeb0 | 2013-03-09 16:34:55 -0800 | [diff] [blame] | 7327 | ir_variable_mode var_mode; |
| 7328 | const char *iface_type_name; |
| 7329 | if (this->layout.flags.q.in) { |
| 7330 | var_mode = ir_var_shader_in; |
| 7331 | iface_type_name = "in"; |
| 7332 | } else if (this->layout.flags.q.out) { |
| 7333 | var_mode = ir_var_shader_out; |
| 7334 | iface_type_name = "out"; |
| 7335 | } else if (this->layout.flags.q.uniform) { |
| 7336 | var_mode = ir_var_uniform; |
| 7337 | iface_type_name = "uniform"; |
Kristian Høgsberg | 84fc5fe | 2015-05-13 10:53:46 +0200 | [diff] [blame] | 7338 | } else if (this->layout.flags.q.buffer) { |
| 7339 | var_mode = ir_var_shader_storage; |
| 7340 | iface_type_name = "buffer"; |
Jordan Justen | b24eeb0 | 2013-03-09 16:34:55 -0800 | [diff] [blame] | 7341 | } else { |
Emil Velikov | 4df6823 | 2013-07-08 18:29:17 +0100 | [diff] [blame] | 7342 | var_mode = ir_var_auto; |
| 7343 | iface_type_name = "UNKNOWN"; |
Jordan Justen | b24eeb0 | 2013-03-09 16:34:55 -0800 | [diff] [blame] | 7344 | assert(!"interface block layout qualifier not found!"); |
| 7345 | } |
| 7346 | |
Ian Romanick | d561e79 | 2014-07-18 11:23:06 -0700 | [diff] [blame] | 7347 | enum glsl_matrix_layout matrix_layout = GLSL_MATRIX_LAYOUT_INHERITED; |
| 7348 | if (this->layout.flags.q.row_major) |
| 7349 | matrix_layout = GLSL_MATRIX_LAYOUT_ROW_MAJOR; |
| 7350 | else if (this->layout.flags.q.column_major) |
| 7351 | matrix_layout = GLSL_MATRIX_LAYOUT_COLUMN_MAJOR; |
| 7352 | |
Paul Berry | e17d671 | 2013-10-22 15:03:29 -0700 | [diff] [blame] | 7353 | bool redeclaring_per_vertex = strcmp(this->block_name, "gl_PerVertex") == 0; |
Paul Berry | e17d671 | 2013-10-22 15:03:29 -0700 | [diff] [blame] | 7354 | exec_list declared_variables; |
| 7355 | glsl_struct_field *fields; |
Chris Forbes | b4ef7c5 | 2014-06-15 12:57:20 +1200 | [diff] [blame] | 7356 | |
Timothy Arceri | 8cf795d | 2015-11-13 09:45:36 +1100 | [diff] [blame] | 7357 | /* For blocks that accept memory qualifiers (i.e. shader storage), verify |
| 7358 | * that we don't have incompatible qualifiers |
| 7359 | */ |
| 7360 | if (this->layout.flags.q.read_only && this->layout.flags.q.write_only) { |
| 7361 | _mesa_glsl_error(&loc, state, |
| 7362 | "Interface block sets both readonly and writeonly"); |
| 7363 | } |
| 7364 | |
Timothy Arceri | 17e224e | 2015-11-13 18:47:55 +1100 | [diff] [blame] | 7365 | unsigned qual_stream; |
| 7366 | if (!process_qualifier_constant(state, &loc, "stream", this->layout.stream, |
Timothy Arceri | db3c36a | 2015-11-14 14:32:38 +1100 | [diff] [blame] | 7367 | &qual_stream) || |
| 7368 | !validate_stream_qualifier(&loc, state, qual_stream)) { |
Timothy Arceri | 17e224e | 2015-11-13 18:47:55 +1100 | [diff] [blame] | 7369 | /* If the stream qualifier is invalid it doesn't make sense to continue |
| 7370 | * on and try to compare stream layouts on member variables against it |
| 7371 | * so just return early. |
| 7372 | */ |
| 7373 | return NULL; |
| 7374 | } |
| 7375 | |
Timothy Arceri | f6a8c7e | 2016-03-11 23:00:16 +1100 | [diff] [blame] | 7376 | unsigned qual_xfb_buffer; |
| 7377 | if (!process_qualifier_constant(state, &loc, "xfb_buffer", |
| 7378 | layout.xfb_buffer, &qual_xfb_buffer) || |
| 7379 | !validate_xfb_buffer_qualifier(&loc, state, qual_xfb_buffer)) { |
| 7380 | return NULL; |
| 7381 | } |
| 7382 | |
Timothy Arceri | edddad0 | 2016-02-24 15:21:59 +1100 | [diff] [blame] | 7383 | unsigned qual_xfb_offset; |
| 7384 | if (layout.flags.q.explicit_xfb_offset) { |
| 7385 | if (!process_qualifier_constant(state, &loc, "xfb_offset", |
| 7386 | layout.offset, &qual_xfb_offset)) { |
| 7387 | return NULL; |
| 7388 | } |
| 7389 | } |
| 7390 | |
Timothy Arceri | 04a72e6 | 2016-02-13 14:53:45 +1100 | [diff] [blame] | 7391 | unsigned qual_xfb_stride; |
| 7392 | if (layout.flags.q.explicit_xfb_stride) { |
| 7393 | if (!process_qualifier_constant(state, &loc, "xfb_stride", |
| 7394 | layout.xfb_stride, &qual_xfb_stride)) { |
| 7395 | return NULL; |
| 7396 | } |
| 7397 | } |
| 7398 | |
Timothy Arceri | e58be8a | 2016-01-06 20:22:46 +1100 | [diff] [blame] | 7399 | unsigned expl_location = 0; |
Timothy Arceri | 0aeb9b3 | 2015-12-01 10:34:18 +1100 | [diff] [blame] | 7400 | if (layout.flags.q.explicit_location) { |
| 7401 | if (!process_qualifier_constant(state, &loc, "location", |
| 7402 | layout.location, &expl_location)) { |
| 7403 | return NULL; |
| 7404 | } else { |
Kenneth Graunke | d0cd504 | 2016-09-03 10:51:07 -0700 | [diff] [blame] | 7405 | expl_location += this->layout.flags.q.patch ? VARYING_SLOT_PATCH0 |
| 7406 | : VARYING_SLOT_VAR0; |
Timothy Arceri | 0aeb9b3 | 2015-12-01 10:34:18 +1100 | [diff] [blame] | 7407 | } |
| 7408 | } |
| 7409 | |
Timothy Arceri | 037f68d | 2016-01-12 15:53:53 +1100 | [diff] [blame] | 7410 | unsigned expl_align = 0; |
| 7411 | if (layout.flags.q.explicit_align) { |
| 7412 | if (!process_qualifier_constant(state, &loc, "align", |
| 7413 | layout.align, &expl_align)) { |
| 7414 | return NULL; |
| 7415 | } else { |
| 7416 | if (expl_align == 0 || expl_align & (expl_align - 1)) { |
| 7417 | _mesa_glsl_error(&loc, state, "align layout qualifier in not a " |
| 7418 | "power of 2."); |
| 7419 | return NULL; |
| 7420 | } |
| 7421 | } |
| 7422 | } |
| 7423 | |
Paul Berry | e17d671 | 2013-10-22 15:03:29 -0700 | [diff] [blame] | 7424 | unsigned int num_variables = |
Timothy Arceri | 14d343b | 2015-11-13 09:49:31 +1100 | [diff] [blame] | 7425 | ast_process_struct_or_iface_block_members(&declared_variables, |
| 7426 | state, |
| 7427 | &this->declarations, |
Timothy Arceri | 14d343b | 2015-11-13 09:49:31 +1100 | [diff] [blame] | 7428 | &fields, |
| 7429 | true, |
| 7430 | matrix_layout, |
| 7431 | redeclaring_per_vertex, |
| 7432 | var_mode, |
Timothy Arceri | 17e224e | 2015-11-13 18:47:55 +1100 | [diff] [blame] | 7433 | &this->layout, |
Timothy Arceri | 0aeb9b3 | 2015-12-01 10:34:18 +1100 | [diff] [blame] | 7434 | qual_stream, |
Timothy Arceri | f6a8c7e | 2016-03-11 23:00:16 +1100 | [diff] [blame] | 7435 | qual_xfb_buffer, |
Timothy Arceri | edddad0 | 2016-02-24 15:21:59 +1100 | [diff] [blame] | 7436 | qual_xfb_offset, |
Timothy Arceri | 037f68d | 2016-01-12 15:53:53 +1100 | [diff] [blame] | 7437 | expl_location, |
| 7438 | expl_align); |
Paul Berry | e17d671 | 2013-10-22 15:03:29 -0700 | [diff] [blame] | 7439 | |
Tapani Pälli | adc8cdf | 2015-01-19 12:28:17 +0200 | [diff] [blame] | 7440 | if (!redeclaring_per_vertex) { |
Paul Berry | f2dd3a0 | 2013-09-27 17:35:27 -0700 | [diff] [blame] | 7441 | validate_identifier(this->block_name, loc, state); |
| 7442 | |
Tapani Pälli | adc8cdf | 2015-01-19 12:28:17 +0200 | [diff] [blame] | 7443 | /* From section 4.3.9 ("Interface Blocks") of the GLSL 4.50 spec: |
| 7444 | * |
| 7445 | * "Block names have no other use within a shader beyond interface |
| 7446 | * matching; it is a compile-time error to use a block name at global |
| 7447 | * scope for anything other than as a block name." |
| 7448 | */ |
| 7449 | ir_variable *var = state->symbols->get_variable(this->block_name); |
| 7450 | if (var && !var->type->is_interface()) { |
| 7451 | _mesa_glsl_error(&loc, state, "Block name `%s' is " |
| 7452 | "already used in the scope.", |
| 7453 | this->block_name); |
| 7454 | } |
| 7455 | } |
| 7456 | |
Paul Berry | 79f5152 | 2013-10-02 11:21:04 -0700 | [diff] [blame] | 7457 | const glsl_type *earlier_per_vertex = NULL; |
| 7458 | if (redeclaring_per_vertex) { |
| 7459 | /* Find the previous declaration of gl_PerVertex. If we're redeclaring |
| 7460 | * the named interface block gl_in, we can find it by looking at the |
| 7461 | * previous declaration of gl_in. Otherwise we can find it by looking |
| 7462 | * at the previous decalartion of any of the built-in outputs, |
| 7463 | * e.g. gl_Position. |
| 7464 | * |
| 7465 | * Also check that the instance name and array-ness of the redeclaration |
| 7466 | * are correct. |
| 7467 | */ |
| 7468 | switch (var_mode) { |
| 7469 | case ir_var_shader_in: |
| 7470 | if (ir_variable *earlier_gl_in = |
| 7471 | state->symbols->get_variable("gl_in")) { |
| 7472 | earlier_per_vertex = earlier_gl_in->get_interface_type(); |
| 7473 | } else { |
| 7474 | _mesa_glsl_error(&loc, state, |
| 7475 | "redeclaration of gl_PerVertex input not allowed " |
| 7476 | "in the %s shader", |
Paul Berry | 665b8d7 | 2014-01-07 10:11:39 -0800 | [diff] [blame] | 7477 | _mesa_shader_stage_to_string(state->stage)); |
Paul Berry | 79f5152 | 2013-10-02 11:21:04 -0700 | [diff] [blame] | 7478 | } |
| 7479 | if (this->instance_name == NULL || |
Timothy Arceri | d337da8 | 2014-06-24 07:43:05 +1000 | [diff] [blame] | 7480 | strcmp(this->instance_name, "gl_in") != 0 || this->array_specifier == NULL || |
| 7481 | !this->array_specifier->is_single_dimension()) { |
Paul Berry | 79f5152 | 2013-10-02 11:21:04 -0700 | [diff] [blame] | 7482 | _mesa_glsl_error(&loc, state, |
| 7483 | "gl_PerVertex input must be redeclared as " |
| 7484 | "gl_in[]"); |
| 7485 | } |
| 7486 | break; |
| 7487 | case ir_var_shader_out: |
| 7488 | if (ir_variable *earlier_gl_Position = |
| 7489 | state->symbols->get_variable("gl_Position")) { |
| 7490 | earlier_per_vertex = earlier_gl_Position->get_interface_type(); |
Chris Forbes | 19f46d0 | 2014-09-12 21:27:26 +1200 | [diff] [blame] | 7491 | } else if (ir_variable *earlier_gl_out = |
| 7492 | state->symbols->get_variable("gl_out")) { |
| 7493 | earlier_per_vertex = earlier_gl_out->get_interface_type(); |
Paul Berry | 79f5152 | 2013-10-02 11:21:04 -0700 | [diff] [blame] | 7494 | } else { |
| 7495 | _mesa_glsl_error(&loc, state, |
| 7496 | "redeclaration of gl_PerVertex output not " |
| 7497 | "allowed in the %s shader", |
Paul Berry | 665b8d7 | 2014-01-07 10:11:39 -0800 | [diff] [blame] | 7498 | _mesa_shader_stage_to_string(state->stage)); |
Paul Berry | 79f5152 | 2013-10-02 11:21:04 -0700 | [diff] [blame] | 7499 | } |
Chris Forbes | 19f46d0 | 2014-09-12 21:27:26 +1200 | [diff] [blame] | 7500 | if (state->stage == MESA_SHADER_TESS_CTRL) { |
| 7501 | if (this->instance_name == NULL || |
| 7502 | strcmp(this->instance_name, "gl_out") != 0 || this->array_specifier == NULL) { |
| 7503 | _mesa_glsl_error(&loc, state, |
| 7504 | "gl_PerVertex output must be redeclared as " |
| 7505 | "gl_out[]"); |
| 7506 | } |
| 7507 | } else { |
| 7508 | if (this->instance_name != NULL) { |
| 7509 | _mesa_glsl_error(&loc, state, |
| 7510 | "gl_PerVertex output may not be redeclared with " |
| 7511 | "an instance name"); |
| 7512 | } |
Paul Berry | 79f5152 | 2013-10-02 11:21:04 -0700 | [diff] [blame] | 7513 | } |
| 7514 | break; |
| 7515 | default: |
| 7516 | _mesa_glsl_error(&loc, state, |
| 7517 | "gl_PerVertex must be declared as an input or an " |
| 7518 | "output"); |
| 7519 | break; |
| 7520 | } |
| 7521 | |
| 7522 | if (earlier_per_vertex == NULL) { |
| 7523 | /* An error has already been reported. Bail out to avoid null |
| 7524 | * dereferences later in this function. |
| 7525 | */ |
| 7526 | return NULL; |
| 7527 | } |
Paul Berry | 84b9fa8 | 2013-09-28 10:04:41 -0700 | [diff] [blame] | 7528 | |
| 7529 | /* Copy locations from the old gl_PerVertex interface block. */ |
| 7530 | for (unsigned i = 0; i < num_variables; i++) { |
| 7531 | int j = earlier_per_vertex->field_index(fields[i].name); |
| 7532 | if (j == -1) { |
| 7533 | _mesa_glsl_error(&loc, state, |
| 7534 | "redeclaration of gl_PerVertex must be a subset " |
| 7535 | "of the built-in members of gl_PerVertex"); |
| 7536 | } else { |
| 7537 | fields[i].location = |
| 7538 | earlier_per_vertex->fields.structure[j].location; |
Timothy Arceri | 9f24f42 | 2015-12-30 13:31:24 +1100 | [diff] [blame] | 7539 | fields[i].offset = |
| 7540 | earlier_per_vertex->fields.structure[j].offset; |
Paul Berry | 99512dc | 2013-10-22 15:11:51 -0700 | [diff] [blame] | 7541 | fields[i].interpolation = |
| 7542 | earlier_per_vertex->fields.structure[j].interpolation; |
| 7543 | fields[i].centroid = |
| 7544 | earlier_per_vertex->fields.structure[j].centroid; |
Chris Forbes | 51c5fc8 | 2013-11-29 21:26:10 +1300 | [diff] [blame] | 7545 | fields[i].sample = |
| 7546 | earlier_per_vertex->fields.structure[j].sample; |
Fabian Bieler | 1009b33 | 2014-03-05 13:43:17 +0100 | [diff] [blame] | 7547 | fields[i].patch = |
| 7548 | earlier_per_vertex->fields.structure[j].patch; |
Samuel Iglesias Gonsálvez | 688b58c | 2015-11-16 12:02:41 +0100 | [diff] [blame] | 7549 | fields[i].precision = |
| 7550 | earlier_per_vertex->fields.structure[j].precision; |
Timothy Arceri | 04d2f77 | 2016-02-24 15:18:09 +1100 | [diff] [blame] | 7551 | fields[i].explicit_xfb_buffer = |
| 7552 | earlier_per_vertex->fields.structure[j].explicit_xfb_buffer; |
Timothy Arceri | f6a8c7e | 2016-03-11 23:00:16 +1100 | [diff] [blame] | 7553 | fields[i].xfb_buffer = |
| 7554 | earlier_per_vertex->fields.structure[j].xfb_buffer; |
Timothy Arceri | edddad0 | 2016-02-24 15:21:59 +1100 | [diff] [blame] | 7555 | fields[i].xfb_stride = |
| 7556 | earlier_per_vertex->fields.structure[j].xfb_stride; |
Paul Berry | 84b9fa8 | 2013-09-28 10:04:41 -0700 | [diff] [blame] | 7557 | } |
| 7558 | } |
Paul Berry | 8cb9cce | 2013-10-01 15:48:07 -0700 | [diff] [blame] | 7559 | |
| 7560 | /* From section 7.1 ("Built-in Language Variables") of the GLSL 4.10 |
| 7561 | * spec: |
| 7562 | * |
| 7563 | * If a built-in interface block is redeclared, it must appear in |
| 7564 | * the shader before any use of any member included in the built-in |
| 7565 | * declaration, or a compilation error will result. |
| 7566 | * |
| 7567 | * This appears to be a clarification to the behaviour established for |
| 7568 | * gl_PerVertex by GLSL 1.50, therefore we implement this behaviour |
| 7569 | * regardless of GLSL version. |
| 7570 | */ |
| 7571 | interface_block_usage_visitor v(var_mode, earlier_per_vertex); |
| 7572 | v.run(instructions); |
| 7573 | if (v.usage_found()) { |
| 7574 | _mesa_glsl_error(&loc, state, |
| 7575 | "redeclaration of a built-in interface block must " |
| 7576 | "appear before any use of any member of the " |
| 7577 | "interface block"); |
| 7578 | } |
Paul Berry | 79f5152 | 2013-10-02 11:21:04 -0700 | [diff] [blame] | 7579 | } |
| 7580 | |
Ian Romanick | 17e6f19 | 2012-12-11 12:14:03 -0800 | [diff] [blame] | 7581 | const glsl_type *block_type = |
| 7582 | glsl_type::get_interface_instance(fields, |
| 7583 | num_variables, |
Ian Romanick | 514f8c7 | 2013-01-22 01:09:16 -0500 | [diff] [blame] | 7584 | packing, |
Iago Toral Quiroga | 537dce0 | 2016-10-21 13:15:41 +0200 | [diff] [blame] | 7585 | matrix_layout == |
| 7586 | GLSL_MATRIX_LAYOUT_ROW_MAJOR, |
Ian Romanick | 17e6f19 | 2012-12-11 12:14:03 -0800 | [diff] [blame] | 7587 | this->block_name); |
| 7588 | |
Timothy Arceri | 98d40b4 | 2016-06-01 09:21:01 +1000 | [diff] [blame] | 7589 | unsigned component_size = block_type->contains_double() ? 8 : 4; |
| 7590 | int xfb_offset = |
| 7591 | layout.flags.q.explicit_xfb_offset ? (int) qual_xfb_offset : -1; |
| 7592 | validate_xfb_offset_qualifier(&loc, state, xfb_offset, block_type, |
| 7593 | component_size); |
| 7594 | |
Jordan Justen | b24eeb0 | 2013-03-09 16:34:55 -0800 | [diff] [blame] | 7595 | if (!state->symbols->add_interface(block_type->name, block_type, var_mode)) { |
Ian Romanick | 5383661 | 2013-01-21 23:01:33 -0500 | [diff] [blame] | 7596 | YYLTYPE loc = this->get_location(); |
Paul Berry | 4d7899f | 2013-07-25 19:56:43 -0700 | [diff] [blame] | 7597 | _mesa_glsl_error(&loc, state, "interface block `%s' with type `%s' " |
| 7598 | "already taken in the current scope", |
Jordan Justen | b24eeb0 | 2013-03-09 16:34:55 -0800 | [diff] [blame] | 7599 | this->block_name, iface_type_name); |
Ian Romanick | 5383661 | 2013-01-21 23:01:33 -0500 | [diff] [blame] | 7600 | } |
| 7601 | |
Ian Romanick | 17e6f19 | 2012-12-11 12:14:03 -0800 | [diff] [blame] | 7602 | /* Since interface blocks cannot contain statements, it should be |
| 7603 | * impossible for the block to generate any instructions. |
| 7604 | */ |
| 7605 | assert(declared_variables.is_empty()); |
| 7606 | |
Paul Berry | 336351e | 2013-08-12 06:39:23 -0700 | [diff] [blame] | 7607 | /* From section 4.3.4 (Inputs) of the GLSL 1.50 spec: |
| 7608 | * |
| 7609 | * Geometry shader input variables get the per-vertex values written |
| 7610 | * out by vertex shader output variables of the same names. Since a |
| 7611 | * geometry shader operates on a set of vertices, each input varying |
| 7612 | * variable (or input block, see interface blocks below) needs to be |
| 7613 | * declared as an array. |
| 7614 | */ |
Timothy Arceri | b0c64d3 | 2014-01-23 23:22:01 +1100 | [diff] [blame] | 7615 | if (state->stage == MESA_SHADER_GEOMETRY && this->array_specifier == NULL && |
Paul Berry | 336351e | 2013-08-12 06:39:23 -0700 | [diff] [blame] | 7616 | var_mode == ir_var_shader_in) { |
| 7617 | _mesa_glsl_error(&loc, state, "geometry shader inputs must be arrays"); |
Chris Forbes | 61846f2 | 2014-09-01 20:48:09 +1200 | [diff] [blame] | 7618 | } else if ((state->stage == MESA_SHADER_TESS_CTRL || |
| 7619 | state->stage == MESA_SHADER_TESS_EVAL) && |
Kenneth Graunke | d82f8d9 | 2016-06-01 19:27:02 -0700 | [diff] [blame] | 7620 | !this->layout.flags.q.patch && |
Chris Forbes | 61846f2 | 2014-09-01 20:48:09 +1200 | [diff] [blame] | 7621 | this->array_specifier == NULL && |
| 7622 | var_mode == ir_var_shader_in) { |
| 7623 | _mesa_glsl_error(&loc, state, "per-vertex tessellation shader inputs must be arrays"); |
| 7624 | } else if (state->stage == MESA_SHADER_TESS_CTRL && |
Kenneth Graunke | d82f8d9 | 2016-06-01 19:27:02 -0700 | [diff] [blame] | 7625 | !this->layout.flags.q.patch && |
Chris Forbes | 61846f2 | 2014-09-01 20:48:09 +1200 | [diff] [blame] | 7626 | this->array_specifier == NULL && |
| 7627 | var_mode == ir_var_shader_out) { |
| 7628 | _mesa_glsl_error(&loc, state, "tessellation control shader outputs must be arrays"); |
Paul Berry | 336351e | 2013-08-12 06:39:23 -0700 | [diff] [blame] | 7629 | } |
| 7630 | |
Chris Forbes | 61846f2 | 2014-09-01 20:48:09 +1200 | [diff] [blame] | 7631 | |
Ian Romanick | 17e6f19 | 2012-12-11 12:14:03 -0800 | [diff] [blame] | 7632 | /* Page 39 (page 45 of the PDF) of section 4.3.7 in the GLSL ES 3.00 spec |
| 7633 | * says: |
| 7634 | * |
| 7635 | * "If an instance name (instance-name) is used, then it puts all the |
| 7636 | * members inside a scope within its own name space, accessed with the |
| 7637 | * field selector ( . ) operator (analogously to structures)." |
| 7638 | */ |
| 7639 | if (this->instance_name) { |
Paul Berry | 37d9766 | 2013-10-15 16:48:59 -0700 | [diff] [blame] | 7640 | if (redeclaring_per_vertex) { |
| 7641 | /* When a built-in in an unnamed interface block is redeclared, |
| 7642 | * get_variable_being_redeclared() calls |
| 7643 | * check_builtin_array_max_size() to make sure that built-in array |
| 7644 | * variables aren't redeclared to illegal sizes. But we're looking |
| 7645 | * at a redeclaration of a named built-in interface block. So we |
| 7646 | * have to manually call check_builtin_array_max_size() for all parts |
| 7647 | * of the interface that are arrays. |
| 7648 | */ |
| 7649 | for (unsigned i = 0; i < num_variables; i++) { |
| 7650 | if (fields[i].type->is_array()) { |
| 7651 | const unsigned size = fields[i].type->array_size(); |
| 7652 | check_builtin_array_max_size(fields[i].name, size, loc, state); |
| 7653 | } |
| 7654 | } |
| 7655 | } else { |
Paul Berry | 9fb6f59 | 2013-09-27 17:41:07 -0700 | [diff] [blame] | 7656 | validate_identifier(this->instance_name, loc, state); |
Paul Berry | 37d9766 | 2013-10-15 16:48:59 -0700 | [diff] [blame] | 7657 | } |
Paul Berry | 9fb6f59 | 2013-09-27 17:41:07 -0700 | [diff] [blame] | 7658 | |
Ian Romanick | 25e75b0 | 2013-01-21 23:06:45 -0500 | [diff] [blame] | 7659 | ir_variable *var; |
| 7660 | |
Timothy Arceri | b0c64d3 | 2014-01-23 23:22:01 +1100 | [diff] [blame] | 7661 | if (this->array_specifier != NULL) { |
Timothy Arceri | 3129359 | 2015-10-15 14:32:41 +1100 | [diff] [blame] | 7662 | const glsl_type *block_array_type = |
| 7663 | process_array_type(&loc, block_type, this->array_specifier, state); |
| 7664 | |
Paul Berry | 20ae8e0 | 2013-07-24 14:57:24 -0700 | [diff] [blame] | 7665 | /* Section 4.3.7 (Interface Blocks) of the GLSL 1.50 spec says: |
| 7666 | * |
| 7667 | * For uniform blocks declared an array, each individual array |
| 7668 | * element corresponds to a separate buffer object backing one |
| 7669 | * instance of the block. As the array size indicates the number |
| 7670 | * of buffer objects needed, uniform block array declarations |
| 7671 | * must specify an array size. |
| 7672 | * |
| 7673 | * And a few paragraphs later: |
| 7674 | * |
| 7675 | * Geometry shader input blocks must be declared as arrays and |
| 7676 | * follow the array declaration and linking rules for all |
| 7677 | * geometry shader inputs. All other input and output block |
| 7678 | * arrays must specify an array size. |
| 7679 | * |
Chris Forbes | 64a0ae8 | 2014-08-18 21:51:46 +1200 | [diff] [blame] | 7680 | * The same applies to tessellation shaders. |
| 7681 | * |
Paul Berry | 20ae8e0 | 2013-07-24 14:57:24 -0700 | [diff] [blame] | 7682 | * The upshot of this is that the only circumstance where an |
| 7683 | * interface array size *doesn't* need to be specified is on a |
Chris Forbes | 64a0ae8 | 2014-08-18 21:51:46 +1200 | [diff] [blame] | 7684 | * geometry shader input, tessellation control shader input, |
| 7685 | * tessellation control shader output, and tessellation evaluation |
| 7686 | * shader input. |
Paul Berry | 20ae8e0 | 2013-07-24 14:57:24 -0700 | [diff] [blame] | 7687 | */ |
Timothy Arceri | 3129359 | 2015-10-15 14:32:41 +1100 | [diff] [blame] | 7688 | if (block_array_type->is_unsized_array()) { |
Chris Forbes | 64a0ae8 | 2014-08-18 21:51:46 +1200 | [diff] [blame] | 7689 | bool allow_inputs = state->stage == MESA_SHADER_GEOMETRY || |
| 7690 | state->stage == MESA_SHADER_TESS_CTRL || |
| 7691 | state->stage == MESA_SHADER_TESS_EVAL; |
| 7692 | bool allow_outputs = state->stage == MESA_SHADER_TESS_CTRL; |
Paul Berry | 20ae8e0 | 2013-07-24 14:57:24 -0700 | [diff] [blame] | 7693 | |
Chris Forbes | 64a0ae8 | 2014-08-18 21:51:46 +1200 | [diff] [blame] | 7694 | if (this->layout.flags.q.in) { |
| 7695 | if (!allow_inputs) |
| 7696 | _mesa_glsl_error(&loc, state, |
| 7697 | "unsized input block arrays not allowed in " |
| 7698 | "%s shader", |
| 7699 | _mesa_shader_stage_to_string(state->stage)); |
| 7700 | } else if (this->layout.flags.q.out) { |
| 7701 | if (!allow_outputs) |
| 7702 | _mesa_glsl_error(&loc, state, |
| 7703 | "unsized output block arrays not allowed in " |
| 7704 | "%s shader", |
| 7705 | _mesa_shader_stage_to_string(state->stage)); |
| 7706 | } else { |
| 7707 | /* by elimination, this is a uniform block array */ |
| 7708 | _mesa_glsl_error(&loc, state, |
| 7709 | "unsized uniform block arrays not allowed in " |
| 7710 | "%s shader", |
| 7711 | _mesa_shader_stage_to_string(state->stage)); |
| 7712 | } |
Paul Berry | 20ae8e0 | 2013-07-24 14:57:24 -0700 | [diff] [blame] | 7713 | } |
| 7714 | |
Timothy Arceri | 6994ca2 | 2015-10-04 17:42:41 +1100 | [diff] [blame] | 7715 | /* From section 4.3.9 (Interface Blocks) of the GLSL ES 3.10 spec: |
Timothy Arceri | 0d2068a | 2015-06-10 19:40:07 +1000 | [diff] [blame] | 7716 | * |
| 7717 | * * Arrays of arrays of blocks are not allowed |
| 7718 | */ |
| 7719 | if (state->es_shader && block_array_type->is_array() && |
| 7720 | block_array_type->fields.array->is_array()) { |
| 7721 | _mesa_glsl_error(&loc, state, |
| 7722 | "arrays of arrays interface blocks are " |
| 7723 | "not allowed"); |
| 7724 | } |
| 7725 | |
Ian Romanick | 25e75b0 | 2013-01-21 23:06:45 -0500 | [diff] [blame] | 7726 | var = new(state) ir_variable(block_array_type, |
| 7727 | this->instance_name, |
Jordan Justen | b24eeb0 | 2013-03-09 16:34:55 -0800 | [diff] [blame] | 7728 | var_mode); |
Ian Romanick | 25e75b0 | 2013-01-21 23:06:45 -0500 | [diff] [blame] | 7729 | } else { |
| 7730 | var = new(state) ir_variable(block_type, |
| 7731 | this->instance_name, |
Jordan Justen | b24eeb0 | 2013-03-09 16:34:55 -0800 | [diff] [blame] | 7732 | var_mode); |
Ian Romanick | 25e75b0 | 2013-01-21 23:06:45 -0500 | [diff] [blame] | 7733 | } |
Ian Romanick | 17e6f19 | 2012-12-11 12:14:03 -0800 | [diff] [blame] | 7734 | |
Ian Romanick | d561e79 | 2014-07-18 11:23:06 -0700 | [diff] [blame] | 7735 | var->data.matrix_layout = matrix_layout == GLSL_MATRIX_LAYOUT_INHERITED |
| 7736 | ? GLSL_MATRIX_LAYOUT_COLUMN_MAJOR : matrix_layout; |
| 7737 | |
Timothy Arceri | 2cb149c | 2015-03-27 23:03:40 +1100 | [diff] [blame] | 7738 | if (var_mode == ir_var_shader_in || var_mode == ir_var_uniform) |
| 7739 | var->data.read_only = true; |
| 7740 | |
Kenneth Graunke | d82f8d9 | 2016-06-01 19:27:02 -0700 | [diff] [blame] | 7741 | var->data.patch = this->layout.flags.q.patch; |
| 7742 | |
Paul Berry | 665b8d7 | 2014-01-07 10:11:39 -0800 | [diff] [blame] | 7743 | if (state->stage == MESA_SHADER_GEOMETRY && var_mode == ir_var_shader_in) |
Paul Berry | 7cfefe6 | 2013-07-30 21:13:48 -0700 | [diff] [blame] | 7744 | handle_geometry_shader_input_decl(state, loc, var); |
Chris Forbes | 61846f2 | 2014-09-01 20:48:09 +1200 | [diff] [blame] | 7745 | else if ((state->stage == MESA_SHADER_TESS_CTRL || |
| 7746 | state->stage == MESA_SHADER_TESS_EVAL) && var_mode == ir_var_shader_in) |
| 7747 | handle_tess_shader_input_decl(state, loc, var); |
| 7748 | else if (state->stage == MESA_SHADER_TESS_CTRL && var_mode == ir_var_shader_out) |
| 7749 | handle_tess_ctrl_shader_output_decl(state, loc, var); |
Paul Berry | fc2330b | 2013-09-28 07:45:00 -0700 | [diff] [blame] | 7750 | |
Samuel Iglesias Gonsalvez | f45d39f6 | 2015-08-25 08:02:46 +0200 | [diff] [blame] | 7751 | for (unsigned i = 0; i < num_variables; i++) { |
Eduardo Lima Mitev | 7f7f58f | 2016-05-05 13:52:37 +0200 | [diff] [blame] | 7752 | if (var->data.mode == ir_var_shader_storage) |
| 7753 | apply_memory_qualifiers(var, fields[i]); |
Samuel Iglesias Gonsalvez | f45d39f6 | 2015-08-25 08:02:46 +0200 | [diff] [blame] | 7754 | } |
| 7755 | |
Paul Berry | 84b9fa8 | 2013-09-28 10:04:41 -0700 | [diff] [blame] | 7756 | if (ir_variable *earlier = |
| 7757 | state->symbols->get_variable(this->instance_name)) { |
| 7758 | if (!redeclaring_per_vertex) { |
| 7759 | _mesa_glsl_error(&loc, state, "`%s' redeclared", |
| 7760 | this->instance_name); |
| 7761 | } |
Tapani Pälli | 33ee2c6 | 2013-12-12 13:51:01 +0200 | [diff] [blame] | 7762 | earlier->data.how_declared = ir_var_declared_normally; |
Paul Berry | 84b9fa8 | 2013-09-28 10:04:41 -0700 | [diff] [blame] | 7763 | earlier->type = var->type; |
| 7764 | earlier->reinit_interface_type(block_type); |
Paul Berry | fc2330b | 2013-09-28 07:45:00 -0700 | [diff] [blame] | 7765 | delete var; |
| 7766 | } else { |
Timothy Arceri | 64710db | 2015-11-15 00:55:29 +1100 | [diff] [blame] | 7767 | if (this->layout.flags.q.explicit_binding) { |
Timothy Arceri | 6463d36 | 2015-11-23 10:07:30 +1100 | [diff] [blame] | 7768 | apply_explicit_binding(state, &loc, var, var->type, |
| 7769 | &this->layout); |
Timothy Arceri | 64710db | 2015-11-15 00:55:29 +1100 | [diff] [blame] | 7770 | } |
Ian Romanick | 625cf8c | 2014-04-02 18:58:54 -0700 | [diff] [blame] | 7771 | |
Timothy Arceri | 17e224e | 2015-11-13 18:47:55 +1100 | [diff] [blame] | 7772 | var->data.stream = qual_stream; |
Timothy Arceri | 0aeb9b3 | 2015-12-01 10:34:18 +1100 | [diff] [blame] | 7773 | if (layout.flags.q.explicit_location) { |
| 7774 | var->data.location = expl_location; |
| 7775 | var->data.explicit_location = true; |
| 7776 | } |
Timothy Arceri | 2832ca9 | 2015-10-17 20:22:14 +1100 | [diff] [blame] | 7777 | |
Paul Berry | fc2330b | 2013-09-28 07:45:00 -0700 | [diff] [blame] | 7778 | state->symbols->add_variable(var); |
| 7779 | instructions->push_tail(var); |
| 7780 | } |
Ian Romanick | 17e6f19 | 2012-12-11 12:14:03 -0800 | [diff] [blame] | 7781 | } else { |
Ian Romanick | 25e75b0 | 2013-01-21 23:06:45 -0500 | [diff] [blame] | 7782 | /* In order to have an array size, the block must also be declared with |
Chris Forbes | aeb03f8 | 2014-04-13 17:01:07 +1200 | [diff] [blame] | 7783 | * an instance name. |
Ian Romanick | 25e75b0 | 2013-01-21 23:06:45 -0500 | [diff] [blame] | 7784 | */ |
Timothy Arceri | b0c64d3 | 2014-01-23 23:22:01 +1100 | [diff] [blame] | 7785 | assert(this->array_specifier == NULL); |
Ian Romanick | 25e75b0 | 2013-01-21 23:06:45 -0500 | [diff] [blame] | 7786 | |
Ian Romanick | 17e6f19 | 2012-12-11 12:14:03 -0800 | [diff] [blame] | 7787 | for (unsigned i = 0; i < num_variables; i++) { |
| 7788 | ir_variable *var = |
| 7789 | new(state) ir_variable(fields[i].type, |
| 7790 | ralloc_strdup(state, fields[i].name), |
Jordan Justen | b24eeb0 | 2013-03-09 16:34:55 -0800 | [diff] [blame] | 7791 | var_mode); |
Tapani Pälli | 33ee2c6 | 2013-12-12 13:51:01 +0200 | [diff] [blame] | 7792 | var->data.interpolation = fields[i].interpolation; |
Tapani Pälli | c1d3080 | 2013-12-12 12:57:57 +0200 | [diff] [blame] | 7793 | var->data.centroid = fields[i].centroid; |
| 7794 | var->data.sample = fields[i].sample; |
Fabian Bieler | 1009b33 | 2014-03-05 13:43:17 +0100 | [diff] [blame] | 7795 | var->data.patch = fields[i].patch; |
Timothy Arceri | 17e224e | 2015-11-13 18:47:55 +1100 | [diff] [blame] | 7796 | var->data.stream = qual_stream; |
Timothy Arceri | 0aeb9b3 | 2015-12-01 10:34:18 +1100 | [diff] [blame] | 7797 | var->data.location = fields[i].location; |
Timothy Arceri | f6a8c7e | 2016-03-11 23:00:16 +1100 | [diff] [blame] | 7798 | |
Timothy Arceri | 0aeb9b3 | 2015-12-01 10:34:18 +1100 | [diff] [blame] | 7799 | if (fields[i].location != -1) |
| 7800 | var->data.explicit_location = true; |
Timothy Arceri | f6a8c7e | 2016-03-11 23:00:16 +1100 | [diff] [blame] | 7801 | |
| 7802 | var->data.explicit_xfb_buffer = fields[i].explicit_xfb_buffer; |
| 7803 | var->data.xfb_buffer = fields[i].xfb_buffer; |
| 7804 | |
Timothy Arceri | edddad0 | 2016-02-24 15:21:59 +1100 | [diff] [blame] | 7805 | if (fields[i].offset != -1) |
| 7806 | var->data.explicit_xfb_offset = true; |
| 7807 | var->data.offset = fields[i].offset; |
| 7808 | |
Paul Berry | 22d3ef2 | 2013-09-24 14:30:29 -0700 | [diff] [blame] | 7809 | var->init_interface_type(block_type); |
Ian Romanick | 17e6f19 | 2012-12-11 12:14:03 -0800 | [diff] [blame] | 7810 | |
Timothy Arceri | 2cb149c | 2015-03-27 23:03:40 +1100 | [diff] [blame] | 7811 | if (var_mode == ir_var_shader_in || var_mode == ir_var_uniform) |
| 7812 | var->data.read_only = true; |
| 7813 | |
Iago Toral Quiroga | f84bc57 | 2015-11-10 08:22:07 +0200 | [diff] [blame] | 7814 | /* Precision qualifiers do not have any meaning in Desktop GLSL */ |
| 7815 | if (state->es_shader) { |
| 7816 | var->data.precision = |
| 7817 | select_gles_precision(fields[i].precision, fields[i].type, |
| 7818 | state, &loc); |
| 7819 | } |
| 7820 | |
Ian Romanick | d561e79 | 2014-07-18 11:23:06 -0700 | [diff] [blame] | 7821 | if (fields[i].matrix_layout == GLSL_MATRIX_LAYOUT_INHERITED) { |
| 7822 | var->data.matrix_layout = matrix_layout == GLSL_MATRIX_LAYOUT_INHERITED |
| 7823 | ? GLSL_MATRIX_LAYOUT_COLUMN_MAJOR : matrix_layout; |
| 7824 | } else { |
| 7825 | var->data.matrix_layout = fields[i].matrix_layout; |
| 7826 | } |
| 7827 | |
Eduardo Lima Mitev | 7f7f58f | 2016-05-05 13:52:37 +0200 | [diff] [blame] | 7828 | if (var->data.mode == ir_var_shader_storage) |
| 7829 | apply_memory_qualifiers(var, fields[i]); |
Iago Toral Quiroga | f1b647f | 2015-04-28 12:09:58 +0200 | [diff] [blame] | 7830 | |
Brian Paul | 14379a0 | 2014-10-17 13:31:53 -0600 | [diff] [blame] | 7831 | /* Examine var name here since var may get deleted in the next call */ |
| 7832 | bool var_is_gl_id = is_gl_identifier(var->name); |
| 7833 | |
Paul Berry | 1b4a737 | 2013-09-27 20:53:33 -0700 | [diff] [blame] | 7834 | if (redeclaring_per_vertex) { |
| 7835 | ir_variable *earlier = |
| 7836 | get_variable_being_redeclared(var, loc, state, |
| 7837 | true /* allow_all_redeclarations */); |
Brian Paul | 14379a0 | 2014-10-17 13:31:53 -0600 | [diff] [blame] | 7838 | if (!var_is_gl_id || earlier == NULL) { |
Paul Berry | 1b4a737 | 2013-09-27 20:53:33 -0700 | [diff] [blame] | 7839 | _mesa_glsl_error(&loc, state, |
| 7840 | "redeclaration of gl_PerVertex can only " |
| 7841 | "include built-in variables"); |
Tapani Pälli | 33ee2c6 | 2013-12-12 13:51:01 +0200 | [diff] [blame] | 7842 | } else if (earlier->data.how_declared == ir_var_declared_normally) { |
Paul Berry | 2bbcf19 | 2013-11-13 16:53:18 -0800 | [diff] [blame] | 7843 | _mesa_glsl_error(&loc, state, |
Brian Paul | 14379a0 | 2014-10-17 13:31:53 -0600 | [diff] [blame] | 7844 | "`%s' has already been redeclared", |
| 7845 | earlier->name); |
Paul Berry | 1b4a737 | 2013-09-27 20:53:33 -0700 | [diff] [blame] | 7846 | } else { |
Tapani Pälli | 33ee2c6 | 2013-12-12 13:51:01 +0200 | [diff] [blame] | 7847 | earlier->data.how_declared = ir_var_declared_in_block; |
Paul Berry | 1b4a737 | 2013-09-27 20:53:33 -0700 | [diff] [blame] | 7848 | earlier->reinit_interface_type(block_type); |
| 7849 | } |
| 7850 | continue; |
| 7851 | } |
| 7852 | |
Paul Berry | 9bb60a1 | 2013-09-27 14:18:09 -0700 | [diff] [blame] | 7853 | if (state->symbols->get_variable(var->name) != NULL) |
| 7854 | _mesa_glsl_error(&loc, state, "`%s' redeclared", var->name); |
| 7855 | |
Samuel Iglesias Gonsalvez | fa0a86c | 2015-03-18 09:02:51 +0100 | [diff] [blame] | 7856 | /* Propagate the "binding" keyword into this UBO/SSBO's fields. |
| 7857 | * The UBO declaration itself doesn't get an ir_variable unless it |
Kenneth Graunke | f25d940 | 2013-07-17 18:06:57 -0700 | [diff] [blame] | 7858 | * has an instance name. This is ugly. |
| 7859 | */ |
Timothy Arceri | 64710db | 2015-11-15 00:55:29 +1100 | [diff] [blame] | 7860 | if (this->layout.flags.q.explicit_binding) { |
| 7861 | apply_explicit_binding(state, &loc, var, |
| 7862 | var->get_interface_type(), &this->layout); |
| 7863 | } |
Kenneth Graunke | f25d940 | 2013-07-17 18:06:57 -0700 | [diff] [blame] | 7864 | |
Samuel Iglesias Gonsalvez | f3f64cd | 2015-03-18 15:32:03 +0100 | [diff] [blame] | 7865 | if (var->type->is_unsized_array()) { |
Jose Maria Casanova Crespo | 1f76523 | 2017-02-10 14:25:27 +0100 | [diff] [blame] | 7866 | if (var->is_in_shader_storage_block() && |
| 7867 | is_unsized_array_last_element(var)) { |
| 7868 | var->data.from_ssbo_unsized_array = true; |
Timothy Arceri | b010fa8 | 2016-06-14 10:13:41 +1000 | [diff] [blame] | 7869 | } else { |
| 7870 | /* From GLSL ES 3.10 spec, section 4.1.9 "Arrays": |
| 7871 | * |
| 7872 | * "If an array is declared as the last member of a shader storage |
| 7873 | * block and the size is not specified at compile-time, it is |
| 7874 | * sized at run-time. In all other cases, arrays are sized only |
| 7875 | * at compile-time." |
Jose Maria Casanova Crespo | 1f76523 | 2017-02-10 14:25:27 +0100 | [diff] [blame] | 7876 | * |
| 7877 | * In desktop GLSL it is allowed to have unsized-arrays that are |
| 7878 | * not last, as long as we can determine that they are implicitly |
| 7879 | * sized. |
Timothy Arceri | b010fa8 | 2016-06-14 10:13:41 +1000 | [diff] [blame] | 7880 | */ |
| 7881 | if (state->es_shader) { |
| 7882 | _mesa_glsl_error(&loc, state, "unsized array `%s' " |
| 7883 | "definition: only last member of a shader " |
| 7884 | "storage block can be defined as unsized " |
| 7885 | "array", fields[i].name); |
| 7886 | } |
Samuel Iglesias Gonsalvez | f3f64cd | 2015-03-18 15:32:03 +0100 | [diff] [blame] | 7887 | } |
| 7888 | } |
| 7889 | |
Ian Romanick | 17e6f19 | 2012-12-11 12:14:03 -0800 | [diff] [blame] | 7890 | state->symbols->add_variable(var); |
| 7891 | instructions->push_tail(var); |
Eric Anholt | b3c093c | 2012-04-26 18:21:43 -0700 | [diff] [blame] | 7892 | } |
Paul Berry | 1b4a737 | 2013-09-27 20:53:33 -0700 | [diff] [blame] | 7893 | |
| 7894 | if (redeclaring_per_vertex && block_type != earlier_per_vertex) { |
| 7895 | /* From section 7.1 ("Built-in Language Variables") of the GLSL 4.10 spec: |
| 7896 | * |
| 7897 | * It is also a compilation error ... to redeclare a built-in |
| 7898 | * block and then use a member from that built-in block that was |
| 7899 | * not included in the redeclaration. |
| 7900 | * |
| 7901 | * This appears to be a clarification to the behaviour established |
| 7902 | * for gl_PerVertex by GLSL 1.50, therefore we implement this |
| 7903 | * behaviour regardless of GLSL version. |
| 7904 | * |
| 7905 | * To prevent the shader from using a member that was not included in |
| 7906 | * the redeclaration, we disable any ir_variables that are still |
| 7907 | * associated with the old declaration of gl_PerVertex (since we've |
| 7908 | * already updated all of the variables contained in the new |
| 7909 | * gl_PerVertex to point to it). |
| 7910 | * |
| 7911 | * As a side effect this will prevent |
| 7912 | * validate_intrastage_interface_blocks() from getting confused and |
| 7913 | * thinking there are conflicting definitions of gl_PerVertex in the |
| 7914 | * shader. |
| 7915 | */ |
Matt Turner | c6a16f6 | 2014-06-24 21:58:35 -0700 | [diff] [blame] | 7916 | foreach_in_list_safe(ir_instruction, node, instructions) { |
| 7917 | ir_variable *const var = node->as_variable(); |
Paul Berry | 1b4a737 | 2013-09-27 20:53:33 -0700 | [diff] [blame] | 7918 | if (var != NULL && |
Paul Berry | e8f6f24 | 2013-10-15 14:56:28 -0700 | [diff] [blame] | 7919 | var->get_interface_type() == earlier_per_vertex && |
Tapani Pälli | 33ee2c6 | 2013-12-12 13:51:01 +0200 | [diff] [blame] | 7920 | var->data.mode == var_mode) { |
| 7921 | if (var->data.how_declared == ir_var_declared_normally) { |
Paul Berry | 2bbcf19 | 2013-11-13 16:53:18 -0800 | [diff] [blame] | 7922 | _mesa_glsl_error(&loc, state, |
| 7923 | "redeclaration of gl_PerVertex cannot " |
| 7924 | "follow a redeclaration of `%s'", |
| 7925 | var->name); |
| 7926 | } |
Paul Berry | 1b4a737 | 2013-09-27 20:53:33 -0700 | [diff] [blame] | 7927 | state->symbols->disable_variable(var->name); |
| 7928 | var->remove(); |
| 7929 | } |
| 7930 | } |
| 7931 | } |
Eric Anholt | b3c093c | 2012-04-26 18:21:43 -0700 | [diff] [blame] | 7932 | } |
| 7933 | |
Eric Anholt | 2d03f48 | 2012-04-18 13:35:56 -0700 | [diff] [blame] | 7934 | return NULL; |
| 7935 | } |
| 7936 | |
Eric Anholt | 624b7ba | 2013-06-12 14:03:49 -0700 | [diff] [blame] | 7937 | |
| 7938 | ir_rvalue * |
Fabian Bieler | 497eb29 | 2014-03-20 22:44:43 +0100 | [diff] [blame] | 7939 | ast_tcs_output_layout::hir(exec_list *instructions, |
Timothy Arceri | 222f66a | 2016-09-28 16:04:08 +1000 | [diff] [blame] | 7940 | struct _mesa_glsl_parse_state *state) |
Fabian Bieler | 497eb29 | 2014-03-20 22:44:43 +0100 | [diff] [blame] | 7941 | { |
| 7942 | YYLTYPE loc = this->get_location(); |
| 7943 | |
Timothy Arceri | 02d2ab2 | 2015-11-14 15:13:28 +1100 | [diff] [blame] | 7944 | unsigned num_vertices; |
| 7945 | if (!state->out_qualifier->vertices-> |
| 7946 | process_qualifier_constant(state, "vertices", &num_vertices, |
| 7947 | false)) { |
| 7948 | /* return here to stop cascading incorrect error messages */ |
| 7949 | return NULL; |
Fabian Bieler | 497eb29 | 2014-03-20 22:44:43 +0100 | [diff] [blame] | 7950 | } |
| 7951 | |
| 7952 | /* If any shader outputs occurred before this declaration and specified an |
| 7953 | * array size, make sure the size they specified is consistent with the |
| 7954 | * primitive type. |
| 7955 | */ |
Fabian Bieler | 497eb29 | 2014-03-20 22:44:43 +0100 | [diff] [blame] | 7956 | if (state->tcs_output_size != 0 && state->tcs_output_size != num_vertices) { |
| 7957 | _mesa_glsl_error(&loc, state, |
Timothy Arceri | 222f66a | 2016-09-28 16:04:08 +1000 | [diff] [blame] | 7958 | "this tessellation control shader output layout " |
| 7959 | "specifies %u vertices, but a previous output " |
| 7960 | "is declared with size %u", |
| 7961 | num_vertices, state->tcs_output_size); |
Fabian Bieler | 497eb29 | 2014-03-20 22:44:43 +0100 | [diff] [blame] | 7962 | return NULL; |
| 7963 | } |
| 7964 | |
| 7965 | state->tcs_output_vertices_specified = true; |
| 7966 | |
| 7967 | /* If any shader outputs occurred before this declaration and did not |
| 7968 | * specify an array size, their size is determined now. |
| 7969 | */ |
| 7970 | foreach_in_list (ir_instruction, node, instructions) { |
| 7971 | ir_variable *var = node->as_variable(); |
| 7972 | if (var == NULL || var->data.mode != ir_var_shader_out) |
Timothy Arceri | 222f66a | 2016-09-28 16:04:08 +1000 | [diff] [blame] | 7973 | continue; |
Fabian Bieler | 497eb29 | 2014-03-20 22:44:43 +0100 | [diff] [blame] | 7974 | |
| 7975 | /* Note: Not all tessellation control shader output are arrays. */ |
Chris Forbes | 61846f2 | 2014-09-01 20:48:09 +1200 | [diff] [blame] | 7976 | if (!var->type->is_unsized_array() || var->data.patch) |
| 7977 | continue; |
Fabian Bieler | 497eb29 | 2014-03-20 22:44:43 +0100 | [diff] [blame] | 7978 | |
Dave Airlie | 8c628ab | 2016-05-20 10:19:14 +1000 | [diff] [blame] | 7979 | if (var->data.max_array_access >= (int)num_vertices) { |
Timothy Arceri | 222f66a | 2016-09-28 16:04:08 +1000 | [diff] [blame] | 7980 | _mesa_glsl_error(&loc, state, |
| 7981 | "this tessellation control shader output layout " |
| 7982 | "specifies %u vertices, but an access to element " |
| 7983 | "%u of output `%s' already exists", num_vertices, |
| 7984 | var->data.max_array_access, var->name); |
Fabian Bieler | 497eb29 | 2014-03-20 22:44:43 +0100 | [diff] [blame] | 7985 | } else { |
Timothy Arceri | 222f66a | 2016-09-28 16:04:08 +1000 | [diff] [blame] | 7986 | var->type = glsl_type::get_array_instance(var->type->fields.array, |
| 7987 | num_vertices); |
Fabian Bieler | 497eb29 | 2014-03-20 22:44:43 +0100 | [diff] [blame] | 7988 | } |
| 7989 | } |
| 7990 | |
| 7991 | return NULL; |
| 7992 | } |
| 7993 | |
| 7994 | |
| 7995 | ir_rvalue * |
Eric Anholt | 624b7ba | 2013-06-12 14:03:49 -0700 | [diff] [blame] | 7996 | ast_gs_input_layout::hir(exec_list *instructions, |
| 7997 | struct _mesa_glsl_parse_state *state) |
| 7998 | { |
| 7999 | YYLTYPE loc = this->get_location(); |
| 8000 | |
Andres Gomez | a5d6ae2 | 2016-11-11 21:03:02 +0200 | [diff] [blame] | 8001 | /* Should have been prevented by the parser. */ |
| 8002 | assert(!state->gs_input_prim_type_specified |
| 8003 | || state->in_qualifier->prim_type == this->prim_type); |
Eric Anholt | 624b7ba | 2013-06-12 14:03:49 -0700 | [diff] [blame] | 8004 | |
Paul Berry | 7cfefe6 | 2013-07-30 21:13:48 -0700 | [diff] [blame] | 8005 | /* If any shader inputs occurred before this declaration and specified an |
| 8006 | * array size, make sure the size they specified is consistent with the |
| 8007 | * primitive type. |
| 8008 | */ |
| 8009 | unsigned num_vertices = vertices_per_prim(this->prim_type); |
| 8010 | if (state->gs_input_size != 0 && state->gs_input_size != num_vertices) { |
| 8011 | _mesa_glsl_error(&loc, state, |
| 8012 | "this geometry shader input layout implies %u vertices" |
| 8013 | " per primitive, but a previous input is declared" |
| 8014 | " with size %u", num_vertices, state->gs_input_size); |
| 8015 | return NULL; |
| 8016 | } |
| 8017 | |
Eric Anholt | 624b7ba | 2013-06-12 14:03:49 -0700 | [diff] [blame] | 8018 | state->gs_input_prim_type_specified = true; |
Eric Anholt | 624b7ba | 2013-06-12 14:03:49 -0700 | [diff] [blame] | 8019 | |
Paul Berry | 7cfefe6 | 2013-07-30 21:13:48 -0700 | [diff] [blame] | 8020 | /* If any shader inputs occurred before this declaration and did not |
| 8021 | * specify an array size, their size is determined now. |
| 8022 | */ |
Matt Turner | 4d78446 | 2014-06-24 21:34:05 -0700 | [diff] [blame] | 8023 | foreach_in_list(ir_instruction, node, instructions) { |
| 8024 | ir_variable *var = node->as_variable(); |
Tapani Pälli | 33ee2c6 | 2013-12-12 13:51:01 +0200 | [diff] [blame] | 8025 | if (var == NULL || var->data.mode != ir_var_shader_in) |
Paul Berry | 7cfefe6 | 2013-07-30 21:13:48 -0700 | [diff] [blame] | 8026 | continue; |
| 8027 | |
| 8028 | /* Note: gl_PrimitiveIDIn has mode ir_var_shader_in, but it's not an |
| 8029 | * array; skip it. |
| 8030 | */ |
Paul Berry | 7cfefe6 | 2013-07-30 21:13:48 -0700 | [diff] [blame] | 8031 | |
Timothy Arceri | b59c592 | 2013-10-23 21:31:27 +1100 | [diff] [blame] | 8032 | if (var->type->is_unsized_array()) { |
Dave Airlie | 8c628ab | 2016-05-20 10:19:14 +1000 | [diff] [blame] | 8033 | if (var->data.max_array_access >= (int)num_vertices) { |
Paul Berry | 7cfefe6 | 2013-07-30 21:13:48 -0700 | [diff] [blame] | 8034 | _mesa_glsl_error(&loc, state, |
| 8035 | "this geometry shader input layout implies %u" |
| 8036 | " vertices, but an access to element %u of input" |
| 8037 | " `%s' already exists", num_vertices, |
Tapani Pälli | 447bb90 | 2013-12-12 15:08:59 +0200 | [diff] [blame] | 8038 | var->data.max_array_access, var->name); |
Paul Berry | 7cfefe6 | 2013-07-30 21:13:48 -0700 | [diff] [blame] | 8039 | } else { |
| 8040 | var->type = glsl_type::get_array_instance(var->type->fields.array, |
| 8041 | num_vertices); |
| 8042 | } |
| 8043 | } |
| 8044 | } |
| 8045 | |
Eric Anholt | 624b7ba | 2013-06-12 14:03:49 -0700 | [diff] [blame] | 8046 | return NULL; |
| 8047 | } |
| 8048 | |
| 8049 | |
Paul Berry | 0fa74e8 | 2014-01-06 09:09:31 -0800 | [diff] [blame] | 8050 | ir_rvalue * |
| 8051 | ast_cs_input_layout::hir(exec_list *instructions, |
| 8052 | struct _mesa_glsl_parse_state *state) |
| 8053 | { |
| 8054 | YYLTYPE loc = this->get_location(); |
| 8055 | |
Paul Berry | 0fa74e8 | 2014-01-06 09:09:31 -0800 | [diff] [blame] | 8056 | /* From the ARB_compute_shader specification: |
| 8057 | * |
| 8058 | * If the local size of the shader in any dimension is greater |
| 8059 | * than the maximum size supported by the implementation for that |
| 8060 | * dimension, a compile-time error results. |
| 8061 | * |
| 8062 | * It is not clear from the spec how the error should be reported if |
| 8063 | * the total size of the work group exceeds |
| 8064 | * MAX_COMPUTE_WORK_GROUP_INVOCATIONS, but it seems reasonable to |
| 8065 | * report it at compile time as well. |
| 8066 | */ |
| 8067 | GLuint64 total_invocations = 1; |
Timothy Arceri | 02d2ab2 | 2015-11-14 15:13:28 +1100 | [diff] [blame] | 8068 | unsigned qual_local_size[3]; |
Paul Berry | 0fa74e8 | 2014-01-06 09:09:31 -0800 | [diff] [blame] | 8069 | for (int i = 0; i < 3; i++) { |
Timothy Arceri | 02d2ab2 | 2015-11-14 15:13:28 +1100 | [diff] [blame] | 8070 | |
| 8071 | char *local_size_str = ralloc_asprintf(NULL, "invalid local_size_%c", |
| 8072 | 'x' + i); |
| 8073 | /* Infer a local_size of 1 for unspecified dimensions */ |
| 8074 | if (this->local_size[i] == NULL) { |
| 8075 | qual_local_size[i] = 1; |
| 8076 | } else if (!this->local_size[i]-> |
| 8077 | process_qualifier_constant(state, local_size_str, |
| 8078 | &qual_local_size[i], false)) { |
| 8079 | ralloc_free(local_size_str); |
| 8080 | return NULL; |
| 8081 | } |
| 8082 | ralloc_free(local_size_str); |
| 8083 | |
| 8084 | if (qual_local_size[i] > state->ctx->Const.MaxComputeWorkGroupSize[i]) { |
Paul Berry | 0fa74e8 | 2014-01-06 09:09:31 -0800 | [diff] [blame] | 8085 | _mesa_glsl_error(&loc, state, |
| 8086 | "local_size_%c exceeds MAX_COMPUTE_WORK_GROUP_SIZE" |
| 8087 | " (%d)", 'x' + i, |
| 8088 | state->ctx->Const.MaxComputeWorkGroupSize[i]); |
| 8089 | break; |
| 8090 | } |
Timothy Arceri | 02d2ab2 | 2015-11-14 15:13:28 +1100 | [diff] [blame] | 8091 | total_invocations *= qual_local_size[i]; |
Paul Berry | 0fa74e8 | 2014-01-06 09:09:31 -0800 | [diff] [blame] | 8092 | if (total_invocations > |
| 8093 | state->ctx->Const.MaxComputeWorkGroupInvocations) { |
| 8094 | _mesa_glsl_error(&loc, state, |
| 8095 | "product of local_sizes exceeds " |
| 8096 | "MAX_COMPUTE_WORK_GROUP_INVOCATIONS (%d)", |
| 8097 | state->ctx->Const.MaxComputeWorkGroupInvocations); |
| 8098 | break; |
| 8099 | } |
| 8100 | } |
| 8101 | |
Timothy Arceri | 02d2ab2 | 2015-11-14 15:13:28 +1100 | [diff] [blame] | 8102 | /* If any compute input layout declaration preceded this one, make sure it |
| 8103 | * was consistent with this one. |
| 8104 | */ |
| 8105 | if (state->cs_input_local_size_specified) { |
| 8106 | for (int i = 0; i < 3; i++) { |
| 8107 | if (state->cs_input_local_size[i] != qual_local_size[i]) { |
| 8108 | _mesa_glsl_error(&loc, state, |
| 8109 | "compute shader input layout does not match" |
| 8110 | " previous declaration"); |
| 8111 | return NULL; |
| 8112 | } |
| 8113 | } |
| 8114 | } |
| 8115 | |
Samuel Pitoiset | 008e785 | 2016-09-07 21:37:53 +0200 | [diff] [blame] | 8116 | /* The ARB_compute_variable_group_size spec says: |
| 8117 | * |
| 8118 | * If a compute shader including a *local_size_variable* qualifier also |
| 8119 | * declares a fixed local group size using the *local_size_x*, |
| 8120 | * *local_size_y*, or *local_size_z* qualifiers, a compile-time error |
| 8121 | * results |
| 8122 | */ |
| 8123 | if (state->cs_input_local_size_variable_specified) { |
| 8124 | _mesa_glsl_error(&loc, state, |
| 8125 | "compute shader can't include both a variable and a " |
| 8126 | "fixed local group size"); |
| 8127 | return NULL; |
| 8128 | } |
| 8129 | |
Paul Berry | 0fa74e8 | 2014-01-06 09:09:31 -0800 | [diff] [blame] | 8130 | state->cs_input_local_size_specified = true; |
| 8131 | for (int i = 0; i < 3; i++) |
Timothy Arceri | 02d2ab2 | 2015-11-14 15:13:28 +1100 | [diff] [blame] | 8132 | state->cs_input_local_size[i] = qual_local_size[i]; |
Paul Berry | 0fa74e8 | 2014-01-06 09:09:31 -0800 | [diff] [blame] | 8133 | |
| 8134 | /* We may now declare the built-in constant gl_WorkGroupSize (see |
| 8135 | * builtin_variable_generator::generate_constants() for why we didn't |
| 8136 | * declare it earlier). |
| 8137 | */ |
| 8138 | ir_variable *var = new(state->symbols) |
Jordan Justen | 307d22a | 2014-10-20 16:23:51 -0700 | [diff] [blame] | 8139 | ir_variable(glsl_type::uvec3_type, "gl_WorkGroupSize", ir_var_auto); |
Paul Berry | 0fa74e8 | 2014-01-06 09:09:31 -0800 | [diff] [blame] | 8140 | var->data.how_declared = ir_var_declared_implicitly; |
| 8141 | var->data.read_only = true; |
| 8142 | instructions->push_tail(var); |
| 8143 | state->symbols->add_variable(var); |
| 8144 | ir_constant_data data; |
| 8145 | memset(&data, 0, sizeof(data)); |
| 8146 | for (int i = 0; i < 3; i++) |
Timothy Arceri | 02d2ab2 | 2015-11-14 15:13:28 +1100 | [diff] [blame] | 8147 | data.u[i] = qual_local_size[i]; |
Jordan Justen | 307d22a | 2014-10-20 16:23:51 -0700 | [diff] [blame] | 8148 | var->constant_value = new(var) ir_constant(glsl_type::uvec3_type, &data); |
Paul Berry | 0fa74e8 | 2014-01-06 09:09:31 -0800 | [diff] [blame] | 8149 | var->constant_initializer = |
Jordan Justen | 307d22a | 2014-10-20 16:23:51 -0700 | [diff] [blame] | 8150 | new(var) ir_constant(glsl_type::uvec3_type, &data); |
Paul Berry | 0fa74e8 | 2014-01-06 09:09:31 -0800 | [diff] [blame] | 8151 | var->data.has_initializer = true; |
| 8152 | |
| 8153 | return NULL; |
| 8154 | } |
| 8155 | |
| 8156 | |
Eric Anholt | 4b2a4cb | 2012-03-29 17:29:20 -0700 | [diff] [blame] | 8157 | static void |
| 8158 | detect_conflicting_assignments(struct _mesa_glsl_parse_state *state, |
Iago Toral Quiroga | 4472ab9 | 2014-04-14 09:14:23 +0200 | [diff] [blame] | 8159 | exec_list *instructions) |
Eric Anholt | 4b2a4cb | 2012-03-29 17:29:20 -0700 | [diff] [blame] | 8160 | { |
| 8161 | bool gl_FragColor_assigned = false; |
| 8162 | bool gl_FragData_assigned = false; |
Ryan Houdek | 1d1d02f | 2015-11-05 10:59:32 -0600 | [diff] [blame] | 8163 | bool gl_FragSecondaryColor_assigned = false; |
| 8164 | bool gl_FragSecondaryData_assigned = false; |
Eric Anholt | 4b2a4cb | 2012-03-29 17:29:20 -0700 | [diff] [blame] | 8165 | bool user_defined_fs_output_assigned = false; |
| 8166 | ir_variable *user_defined_fs_output = NULL; |
| 8167 | |
| 8168 | /* It would be nice to have proper location information. */ |
| 8169 | YYLTYPE loc; |
| 8170 | memset(&loc, 0, sizeof(loc)); |
| 8171 | |
Matt Turner | 4d78446 | 2014-06-24 21:34:05 -0700 | [diff] [blame] | 8172 | foreach_in_list(ir_instruction, node, instructions) { |
| 8173 | ir_variable *var = node->as_variable(); |
Eric Anholt | 4b2a4cb | 2012-03-29 17:29:20 -0700 | [diff] [blame] | 8174 | |
Tapani Pälli | 33ee2c6 | 2013-12-12 13:51:01 +0200 | [diff] [blame] | 8175 | if (!var || !var->data.assigned) |
Iago Toral Quiroga | 4472ab9 | 2014-04-14 09:14:23 +0200 | [diff] [blame] | 8176 | continue; |
Eric Anholt | 4b2a4cb | 2012-03-29 17:29:20 -0700 | [diff] [blame] | 8177 | |
| 8178 | if (strcmp(var->name, "gl_FragColor") == 0) |
Iago Toral Quiroga | 4472ab9 | 2014-04-14 09:14:23 +0200 | [diff] [blame] | 8179 | gl_FragColor_assigned = true; |
Eric Anholt | 4b2a4cb | 2012-03-29 17:29:20 -0700 | [diff] [blame] | 8180 | else if (strcmp(var->name, "gl_FragData") == 0) |
Iago Toral Quiroga | 4472ab9 | 2014-04-14 09:14:23 +0200 | [diff] [blame] | 8181 | gl_FragData_assigned = true; |
Timothy Arceri | 222f66a | 2016-09-28 16:04:08 +1000 | [diff] [blame] | 8182 | else if (strcmp(var->name, "gl_SecondaryFragColorEXT") == 0) |
Ryan Houdek | 1d1d02f | 2015-11-05 10:59:32 -0600 | [diff] [blame] | 8183 | gl_FragSecondaryColor_assigned = true; |
Timothy Arceri | 222f66a | 2016-09-28 16:04:08 +1000 | [diff] [blame] | 8184 | else if (strcmp(var->name, "gl_SecondaryFragDataEXT") == 0) |
Ryan Houdek | 1d1d02f | 2015-11-05 10:59:32 -0600 | [diff] [blame] | 8185 | gl_FragSecondaryData_assigned = true; |
Brian Paul | a7aca39 | 2014-05-23 14:57:49 -0600 | [diff] [blame] | 8186 | else if (!is_gl_identifier(var->name)) { |
Iago Toral Quiroga | 4472ab9 | 2014-04-14 09:14:23 +0200 | [diff] [blame] | 8187 | if (state->stage == MESA_SHADER_FRAGMENT && |
| 8188 | var->data.mode == ir_var_shader_out) { |
| 8189 | user_defined_fs_output_assigned = true; |
| 8190 | user_defined_fs_output = var; |
| 8191 | } |
Eric Anholt | 4b2a4cb | 2012-03-29 17:29:20 -0700 | [diff] [blame] | 8192 | } |
| 8193 | } |
| 8194 | |
| 8195 | /* From the GLSL 1.30 spec: |
| 8196 | * |
| 8197 | * "If a shader statically assigns a value to gl_FragColor, it |
| 8198 | * may not assign a value to any element of gl_FragData. If a |
| 8199 | * shader statically writes a value to any element of |
| 8200 | * gl_FragData, it may not assign a value to |
| 8201 | * gl_FragColor. That is, a shader may assign values to either |
| 8202 | * gl_FragColor or gl_FragData, but not both. Multiple shaders |
| 8203 | * linked together must also consistently write just one of |
| 8204 | * these variables. Similarly, if user declared output |
| 8205 | * variables are in use (statically assigned to), then the |
| 8206 | * built-in variables gl_FragColor and gl_FragData may not be |
| 8207 | * assigned to. These incorrect usages all generate compile |
| 8208 | * time errors." |
| 8209 | */ |
| 8210 | if (gl_FragColor_assigned && gl_FragData_assigned) { |
| 8211 | _mesa_glsl_error(&loc, state, "fragment shader writes to both " |
Iago Toral Quiroga | 4472ab9 | 2014-04-14 09:14:23 +0200 | [diff] [blame] | 8212 | "`gl_FragColor' and `gl_FragData'"); |
Eric Anholt | 4b2a4cb | 2012-03-29 17:29:20 -0700 | [diff] [blame] | 8213 | } else if (gl_FragColor_assigned && user_defined_fs_output_assigned) { |
| 8214 | _mesa_glsl_error(&loc, state, "fragment shader writes to both " |
Iago Toral Quiroga | 4472ab9 | 2014-04-14 09:14:23 +0200 | [diff] [blame] | 8215 | "`gl_FragColor' and `%s'", |
| 8216 | user_defined_fs_output->name); |
Ryan Houdek | 1d1d02f | 2015-11-05 10:59:32 -0600 | [diff] [blame] | 8217 | } else if (gl_FragSecondaryColor_assigned && gl_FragSecondaryData_assigned) { |
| 8218 | _mesa_glsl_error(&loc, state, "fragment shader writes to both " |
| 8219 | "`gl_FragSecondaryColorEXT' and" |
| 8220 | " `gl_FragSecondaryDataEXT'"); |
| 8221 | } else if (gl_FragColor_assigned && gl_FragSecondaryData_assigned) { |
| 8222 | _mesa_glsl_error(&loc, state, "fragment shader writes to both " |
| 8223 | "`gl_FragColor' and" |
| 8224 | " `gl_FragSecondaryDataEXT'"); |
| 8225 | } else if (gl_FragData_assigned && gl_FragSecondaryColor_assigned) { |
| 8226 | _mesa_glsl_error(&loc, state, "fragment shader writes to both " |
| 8227 | "`gl_FragData' and" |
| 8228 | " `gl_FragSecondaryColorEXT'"); |
Eric Anholt | 4b2a4cb | 2012-03-29 17:29:20 -0700 | [diff] [blame] | 8229 | } else if (gl_FragData_assigned && user_defined_fs_output_assigned) { |
| 8230 | _mesa_glsl_error(&loc, state, "fragment shader writes to both " |
Iago Toral Quiroga | 4472ab9 | 2014-04-14 09:14:23 +0200 | [diff] [blame] | 8231 | "`gl_FragData' and `%s'", |
| 8232 | user_defined_fs_output->name); |
Eric Anholt | 4b2a4cb | 2012-03-29 17:29:20 -0700 | [diff] [blame] | 8233 | } |
Ryan Houdek | 1d1d02f | 2015-11-05 10:59:32 -0600 | [diff] [blame] | 8234 | |
| 8235 | if ((gl_FragSecondaryColor_assigned || gl_FragSecondaryData_assigned) && |
| 8236 | !state->EXT_blend_func_extended_enable) { |
| 8237 | _mesa_glsl_error(&loc, state, |
| 8238 | "Dual source blending requires EXT_blend_func_extended"); |
| 8239 | } |
Eric Anholt | 4b2a4cb | 2012-03-29 17:29:20 -0700 | [diff] [blame] | 8240 | } |
Paul Berry | 719bf30 | 2013-10-15 15:13:59 -0700 | [diff] [blame] | 8241 | |
| 8242 | |
| 8243 | static void |
| 8244 | remove_per_vertex_blocks(exec_list *instructions, |
| 8245 | _mesa_glsl_parse_state *state, ir_variable_mode mode) |
| 8246 | { |
| 8247 | /* Find the gl_PerVertex interface block of the appropriate (in/out) mode, |
| 8248 | * if it exists in this shader type. |
| 8249 | */ |
| 8250 | const glsl_type *per_vertex = NULL; |
| 8251 | switch (mode) { |
| 8252 | case ir_var_shader_in: |
| 8253 | if (ir_variable *gl_in = state->symbols->get_variable("gl_in")) |
| 8254 | per_vertex = gl_in->get_interface_type(); |
| 8255 | break; |
| 8256 | case ir_var_shader_out: |
| 8257 | if (ir_variable *gl_Position = |
| 8258 | state->symbols->get_variable("gl_Position")) { |
| 8259 | per_vertex = gl_Position->get_interface_type(); |
| 8260 | } |
| 8261 | break; |
| 8262 | default: |
| 8263 | assert(!"Unexpected mode"); |
| 8264 | break; |
| 8265 | } |
| 8266 | |
| 8267 | /* If we didn't find a built-in gl_PerVertex interface block, then we don't |
| 8268 | * need to do anything. |
| 8269 | */ |
| 8270 | if (per_vertex == NULL) |
| 8271 | return; |
| 8272 | |
| 8273 | /* If the interface block is used by the shader, then we don't need to do |
| 8274 | * anything. |
| 8275 | */ |
| 8276 | interface_block_usage_visitor v(mode, per_vertex); |
| 8277 | v.run(instructions); |
| 8278 | if (v.usage_found()) |
| 8279 | return; |
| 8280 | |
| 8281 | /* Remove any ir_variable declarations that refer to the interface block |
| 8282 | * we're removing. |
| 8283 | */ |
Matt Turner | c6a16f6 | 2014-06-24 21:58:35 -0700 | [diff] [blame] | 8284 | foreach_in_list_safe(ir_instruction, node, instructions) { |
| 8285 | ir_variable *const var = node->as_variable(); |
Paul Berry | 719bf30 | 2013-10-15 15:13:59 -0700 | [diff] [blame] | 8286 | if (var != NULL && var->get_interface_type() == per_vertex && |
Tapani Pälli | 33ee2c6 | 2013-12-12 13:51:01 +0200 | [diff] [blame] | 8287 | var->data.mode == mode) { |
Paul Berry | 719bf30 | 2013-10-15 15:13:59 -0700 | [diff] [blame] | 8288 | state->symbols->disable_variable(var->name); |
| 8289 | var->remove(); |
| 8290 | } |
| 8291 | } |
| 8292 | } |