Ian Romanick | 832dfa5 | 2010-06-17 15:04:20 -0700 | [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 linker.cpp |
| 26 | * GLSL linker implementation |
| 27 | * |
| 28 | * Given a set of shaders that are to be linked to generate a final program, |
| 29 | * there are three distinct stages. |
| 30 | * |
| 31 | * In the first stage shaders are partitioned into groups based on the shader |
| 32 | * type. All shaders of a particular type (e.g., vertex shaders) are linked |
| 33 | * together. |
| 34 | * |
| 35 | * - Undefined references in each shader are resolve to definitions in |
| 36 | * another shader. |
| 37 | * - Types and qualifiers of uniforms, outputs, and global variables defined |
| 38 | * in multiple shaders with the same name are verified to be the same. |
| 39 | * - Initializers for uniforms and global variables defined |
| 40 | * in multiple shaders with the same name are verified to be the same. |
| 41 | * |
| 42 | * The result, in the terminology of the GLSL spec, is a set of shader |
| 43 | * executables for each processing unit. |
| 44 | * |
| 45 | * After the first stage is complete, a series of semantic checks are performed |
| 46 | * on each of the shader executables. |
| 47 | * |
| 48 | * - Each shader executable must define a \c main function. |
| 49 | * - Each vertex shader executable must write to \c gl_Position. |
| 50 | * - Each fragment shader executable must write to either \c gl_FragData or |
| 51 | * \c gl_FragColor. |
| 52 | * |
| 53 | * In the final stage individual shader executables are linked to create a |
| 54 | * complete exectuable. |
| 55 | * |
| 56 | * - Types of uniforms defined in multiple shader stages with the same name |
| 57 | * are verified to be the same. |
| 58 | * - Initializers for uniforms defined in multiple shader stages with the |
| 59 | * same name are verified to be the same. |
| 60 | * - Types and qualifiers of outputs defined in one stage are verified to |
| 61 | * be the same as the types and qualifiers of inputs defined with the same |
| 62 | * name in a later stage. |
| 63 | * |
| 64 | * \author Ian Romanick <ian.d.romanick@intel.com> |
| 65 | */ |
Ian Romanick | f36460e | 2010-06-23 12:07:22 -0700 | [diff] [blame] | 66 | |
Brian Paul | ddf4b2e | 2015-02-24 16:56:54 -0700 | [diff] [blame] | 67 | #include <ctype.h> |
Chia-I Wu | bfd7c9a | 2010-08-23 17:51:42 +0800 | [diff] [blame] | 68 | #include "main/core.h" |
Ian Romanick | 832dfa5 | 2010-06-17 15:04:20 -0700 | [diff] [blame] | 69 | #include "glsl_symbol_table.h" |
Eric Anholt | faf3dba | 2013-06-12 16:57:11 -0700 | [diff] [blame] | 70 | #include "glsl_parser_extras.h" |
Ian Romanick | 832dfa5 | 2010-06-17 15:04:20 -0700 | [diff] [blame] | 71 | #include "ir.h" |
| 72 | #include "program.h" |
Aras Pranckevicius | 3174715 | 2010-07-29 12:40:49 +0300 | [diff] [blame] | 73 | #include "program/hash_table.h" |
Ian Romanick | 8fe8a81 | 2010-07-13 17:36:13 -0700 | [diff] [blame] | 74 | #include "linker.h" |
Paul Berry | 4b11b57 | 2012-12-17 14:20:35 -0800 | [diff] [blame] | 75 | #include "link_varyings.h" |
Ian Romanick | a7ba9a7 | 2010-07-20 13:36:32 -0700 | [diff] [blame] | 76 | #include "ir_optimization.h" |
Bryan Cain | 2548092 | 2013-02-15 09:46:50 -0600 | [diff] [blame] | 77 | #include "ir_rvalue_visitor.h" |
Tapani Pälli | eca9d16 | 2014-04-08 08:45:36 +0300 | [diff] [blame] | 78 | #include "ir_uniform.h" |
Ian Romanick | 832dfa5 | 2010-06-17 15:04:20 -0700 | [diff] [blame] | 79 | |
Ian Romanick | 3322fba | 2010-10-14 13:28:42 -0700 | [diff] [blame] | 80 | #include "main/shaderobj.h" |
Eric Anholt | 6065a87 | 2013-06-12 18:12:40 -0700 | [diff] [blame] | 81 | #include "main/enums.h" |
Brian Paul | 241c599 | 2014-12-15 16:41:58 -0700 | [diff] [blame] | 82 | |
Ian Romanick | 3322fba | 2010-10-14 13:28:42 -0700 | [diff] [blame] | 83 | |
Bryan Cain | 2548092 | 2013-02-15 09:46:50 -0600 | [diff] [blame] | 84 | void linker_error(gl_shader_program *, const char *, ...); |
| 85 | |
Eric Anholt | 10ef949 | 2013-09-20 11:03:44 -0700 | [diff] [blame] | 86 | namespace { |
| 87 | |
Ian Romanick | 832dfa5 | 2010-06-17 15:04:20 -0700 | [diff] [blame] | 88 | /** |
| 89 | * Visitor that determines whether or not a variable is ever written. |
| 90 | */ |
| 91 | class find_assignment_visitor : public ir_hierarchical_visitor { |
| 92 | public: |
| 93 | find_assignment_visitor(const char *name) |
| 94 | : name(name), found(false) |
| 95 | { |
| 96 | /* empty */ |
| 97 | } |
| 98 | |
| 99 | virtual ir_visitor_status visit_enter(ir_assignment *ir) |
| 100 | { |
| 101 | ir_variable *const var = ir->lhs->variable_referenced(); |
| 102 | |
| 103 | if (strcmp(name, var->name) == 0) { |
| 104 | found = true; |
| 105 | return visit_stop; |
| 106 | } |
| 107 | |
| 108 | return visit_continue_with_parent; |
| 109 | } |
| 110 | |
Eric Anholt | 18a6023 | 2010-08-23 11:29:25 -0700 | [diff] [blame] | 111 | virtual ir_visitor_status visit_enter(ir_call *ir) |
| 112 | { |
Kenneth Graunke | 48d0faa | 2014-01-10 16:39:17 -0800 | [diff] [blame] | 113 | foreach_two_lists(formal_node, &ir->callee->parameters, |
| 114 | actual_node, &ir->actual_parameters) { |
| 115 | ir_rvalue *param_rval = (ir_rvalue *) actual_node; |
| 116 | ir_variable *sig_param = (ir_variable *) formal_node; |
Eric Anholt | 18a6023 | 2010-08-23 11:29:25 -0700 | [diff] [blame] | 117 | |
Tapani Pälli | 33ee2c6 | 2013-12-12 13:51:01 +0200 | [diff] [blame] | 118 | if (sig_param->data.mode == ir_var_function_out || |
| 119 | sig_param->data.mode == ir_var_function_inout) { |
Eric Anholt | 18a6023 | 2010-08-23 11:29:25 -0700 | [diff] [blame] | 120 | ir_variable *var = param_rval->variable_referenced(); |
| 121 | if (var && strcmp(name, var->name) == 0) { |
| 122 | found = true; |
| 123 | return visit_stop; |
| 124 | } |
| 125 | } |
Eric Anholt | 18a6023 | 2010-08-23 11:29:25 -0700 | [diff] [blame] | 126 | } |
| 127 | |
Kenneth Graunke | d884f60 | 2012-03-20 15:56:37 -0700 | [diff] [blame] | 128 | if (ir->return_deref != NULL) { |
| 129 | ir_variable *const var = ir->return_deref->variable_referenced(); |
| 130 | |
| 131 | if (strcmp(name, var->name) == 0) { |
| 132 | found = true; |
| 133 | return visit_stop; |
| 134 | } |
| 135 | } |
| 136 | |
Eric Anholt | 18a6023 | 2010-08-23 11:29:25 -0700 | [diff] [blame] | 137 | return visit_continue_with_parent; |
| 138 | } |
| 139 | |
Ian Romanick | 832dfa5 | 2010-06-17 15:04:20 -0700 | [diff] [blame] | 140 | bool variable_found() |
| 141 | { |
| 142 | return found; |
| 143 | } |
| 144 | |
| 145 | private: |
| 146 | const char *name; /**< Find writes to a variable with this name. */ |
| 147 | bool found; /**< Was a write to the variable found? */ |
| 148 | }; |
| 149 | |
Ian Romanick | c93b8f1 | 2010-06-17 15:20:22 -0700 | [diff] [blame] | 150 | |
Ian Romanick | c33e78f | 2010-08-13 12:30:41 -0700 | [diff] [blame] | 151 | /** |
| 152 | * Visitor that determines whether or not a variable is ever read. |
| 153 | */ |
| 154 | class find_deref_visitor : public ir_hierarchical_visitor { |
| 155 | public: |
| 156 | find_deref_visitor(const char *name) |
| 157 | : name(name), found(false) |
| 158 | { |
| 159 | /* empty */ |
| 160 | } |
| 161 | |
| 162 | virtual ir_visitor_status visit(ir_dereference_variable *ir) |
| 163 | { |
| 164 | if (strcmp(this->name, ir->var->name) == 0) { |
| 165 | this->found = true; |
| 166 | return visit_stop; |
| 167 | } |
| 168 | |
| 169 | return visit_continue; |
| 170 | } |
| 171 | |
| 172 | bool variable_found() const |
| 173 | { |
| 174 | return this->found; |
| 175 | } |
| 176 | |
| 177 | private: |
| 178 | const char *name; /**< Find writes to a variable with this name. */ |
| 179 | bool found; /**< Was a write to the variable found? */ |
| 180 | }; |
| 181 | |
| 182 | |
Paul Berry | 7cfefe6 | 2013-07-30 21:13:48 -0700 | [diff] [blame] | 183 | class geom_array_resize_visitor : public ir_hierarchical_visitor { |
| 184 | public: |
| 185 | unsigned num_vertices; |
| 186 | gl_shader_program *prog; |
| 187 | |
| 188 | geom_array_resize_visitor(unsigned num_vertices, gl_shader_program *prog) |
| 189 | { |
| 190 | this->num_vertices = num_vertices; |
| 191 | this->prog = prog; |
| 192 | } |
| 193 | |
| 194 | virtual ~geom_array_resize_visitor() |
| 195 | { |
| 196 | /* empty */ |
| 197 | } |
| 198 | |
| 199 | virtual ir_visitor_status visit(ir_variable *var) |
| 200 | { |
Tapani Pälli | 33ee2c6 | 2013-12-12 13:51:01 +0200 | [diff] [blame] | 201 | if (!var->type->is_array() || var->data.mode != ir_var_shader_in) |
Paul Berry | 7cfefe6 | 2013-07-30 21:13:48 -0700 | [diff] [blame] | 202 | return visit_continue; |
| 203 | |
| 204 | unsigned size = var->type->length; |
| 205 | |
| 206 | /* Generate a link error if the shader has declared this array with an |
| 207 | * incorrect size. |
| 208 | */ |
| 209 | if (size && size != this->num_vertices) { |
| 210 | linker_error(this->prog, "size of array %s declared as %u, " |
| 211 | "but number of input vertices is %u\n", |
| 212 | var->name, size, this->num_vertices); |
| 213 | return visit_continue; |
| 214 | } |
| 215 | |
| 216 | /* Generate a link error if the shader attempts to access an input |
| 217 | * array using an index too large for its actual size assigned at link |
| 218 | * time. |
| 219 | */ |
Tapani Pälli | 447bb90 | 2013-12-12 15:08:59 +0200 | [diff] [blame] | 220 | if (var->data.max_array_access >= this->num_vertices) { |
Paul Berry | 7cfefe6 | 2013-07-30 21:13:48 -0700 | [diff] [blame] | 221 | linker_error(this->prog, "geometry shader accesses element %i of " |
| 222 | "%s, but only %i input vertices\n", |
Tapani Pälli | 447bb90 | 2013-12-12 15:08:59 +0200 | [diff] [blame] | 223 | var->data.max_array_access, var->name, this->num_vertices); |
Paul Berry | 7cfefe6 | 2013-07-30 21:13:48 -0700 | [diff] [blame] | 224 | return visit_continue; |
| 225 | } |
| 226 | |
Timothy Arceri | d67515b | 2015-04-30 20:45:54 +1000 | [diff] [blame] | 227 | var->type = glsl_type::get_array_instance(var->type->fields.array, |
Paul Berry | 7cfefe6 | 2013-07-30 21:13:48 -0700 | [diff] [blame] | 228 | this->num_vertices); |
Tapani Pälli | 447bb90 | 2013-12-12 15:08:59 +0200 | [diff] [blame] | 229 | var->data.max_array_access = this->num_vertices - 1; |
Paul Berry | 7cfefe6 | 2013-07-30 21:13:48 -0700 | [diff] [blame] | 230 | |
| 231 | return visit_continue; |
| 232 | } |
| 233 | |
| 234 | /* Dereferences of input variables need to be updated so that their type |
| 235 | * matches the newly assigned type of the variable they are accessing. */ |
| 236 | virtual ir_visitor_status visit(ir_dereference_variable *ir) |
| 237 | { |
| 238 | ir->type = ir->var->type; |
| 239 | return visit_continue; |
| 240 | } |
| 241 | |
| 242 | /* Dereferences of 2D input arrays need to be updated so that their type |
| 243 | * matches the newly assigned type of the array they are accessing. */ |
| 244 | virtual ir_visitor_status visit_leave(ir_dereference_array *ir) |
| 245 | { |
| 246 | const glsl_type *const vt = ir->array->type; |
| 247 | if (vt->is_array()) |
Timothy Arceri | d67515b | 2015-04-30 20:45:54 +1000 | [diff] [blame] | 248 | ir->type = vt->fields.array; |
Paul Berry | 7cfefe6 | 2013-07-30 21:13:48 -0700 | [diff] [blame] | 249 | return visit_continue; |
| 250 | } |
| 251 | }; |
| 252 | |
Chris Forbes | 7c758c5 | 2014-09-21 13:33:14 +1200 | [diff] [blame] | 253 | class tess_eval_array_resize_visitor : public ir_hierarchical_visitor { |
| 254 | public: |
| 255 | unsigned num_vertices; |
| 256 | gl_shader_program *prog; |
| 257 | |
| 258 | tess_eval_array_resize_visitor(unsigned num_vertices, gl_shader_program *prog) |
| 259 | { |
| 260 | this->num_vertices = num_vertices; |
| 261 | this->prog = prog; |
| 262 | } |
| 263 | |
| 264 | virtual ~tess_eval_array_resize_visitor() |
| 265 | { |
| 266 | /* empty */ |
| 267 | } |
| 268 | |
| 269 | virtual ir_visitor_status visit(ir_variable *var) |
| 270 | { |
| 271 | if (!var->type->is_array() || var->data.mode != ir_var_shader_in || var->data.patch) |
| 272 | return visit_continue; |
| 273 | |
| 274 | var->type = glsl_type::get_array_instance(var->type->fields.array, |
| 275 | this->num_vertices); |
| 276 | var->data.max_array_access = this->num_vertices - 1; |
| 277 | |
| 278 | return visit_continue; |
| 279 | } |
| 280 | |
| 281 | /* Dereferences of input variables need to be updated so that their type |
| 282 | * matches the newly assigned type of the variable they are accessing. */ |
| 283 | virtual ir_visitor_status visit(ir_dereference_variable *ir) |
| 284 | { |
| 285 | ir->type = ir->var->type; |
| 286 | return visit_continue; |
| 287 | } |
| 288 | |
| 289 | /* Dereferences of 2D input arrays need to be updated so that their type |
| 290 | * matches the newly assigned type of the array they are accessing. */ |
| 291 | virtual ir_visitor_status visit_leave(ir_dereference_array *ir) |
| 292 | { |
| 293 | const glsl_type *const vt = ir->array->type; |
| 294 | if (vt->is_array()) |
| 295 | ir->type = vt->fields.array; |
| 296 | return visit_continue; |
| 297 | } |
| 298 | }; |
| 299 | |
Chris Forbes | 8cf7297 | 2014-09-07 21:42:50 +1200 | [diff] [blame] | 300 | class barrier_use_visitor : public ir_hierarchical_visitor { |
| 301 | public: |
| 302 | barrier_use_visitor(gl_shader_program *prog) |
| 303 | : prog(prog), in_main(false), after_return(false), control_flow(0) |
| 304 | { |
| 305 | } |
| 306 | |
| 307 | virtual ~barrier_use_visitor() |
| 308 | { |
| 309 | /* empty */ |
| 310 | } |
| 311 | |
| 312 | virtual ir_visitor_status visit_enter(ir_function *ir) |
| 313 | { |
| 314 | if (strcmp(ir->name, "main") == 0) |
| 315 | in_main = true; |
| 316 | |
| 317 | return visit_continue; |
| 318 | } |
| 319 | |
Ian Romanick | 4ff9e59 | 2015-08-19 13:36:22 -0700 | [diff] [blame] | 320 | virtual ir_visitor_status visit_leave(ir_function *) |
Chris Forbes | 8cf7297 | 2014-09-07 21:42:50 +1200 | [diff] [blame] | 321 | { |
| 322 | in_main = false; |
| 323 | after_return = false; |
| 324 | return visit_continue; |
| 325 | } |
| 326 | |
Ian Romanick | 4ff9e59 | 2015-08-19 13:36:22 -0700 | [diff] [blame] | 327 | virtual ir_visitor_status visit_leave(ir_return *) |
Chris Forbes | 8cf7297 | 2014-09-07 21:42:50 +1200 | [diff] [blame] | 328 | { |
| 329 | after_return = true; |
| 330 | return visit_continue; |
| 331 | } |
| 332 | |
Ian Romanick | 4ff9e59 | 2015-08-19 13:36:22 -0700 | [diff] [blame] | 333 | virtual ir_visitor_status visit_enter(ir_if *) |
Chris Forbes | 8cf7297 | 2014-09-07 21:42:50 +1200 | [diff] [blame] | 334 | { |
| 335 | ++control_flow; |
| 336 | return visit_continue; |
| 337 | } |
| 338 | |
Ian Romanick | 4ff9e59 | 2015-08-19 13:36:22 -0700 | [diff] [blame] | 339 | virtual ir_visitor_status visit_leave(ir_if *) |
Chris Forbes | 8cf7297 | 2014-09-07 21:42:50 +1200 | [diff] [blame] | 340 | { |
| 341 | --control_flow; |
| 342 | return visit_continue; |
| 343 | } |
| 344 | |
Ian Romanick | 4ff9e59 | 2015-08-19 13:36:22 -0700 | [diff] [blame] | 345 | virtual ir_visitor_status visit_enter(ir_loop *) |
Chris Forbes | 8cf7297 | 2014-09-07 21:42:50 +1200 | [diff] [blame] | 346 | { |
| 347 | ++control_flow; |
| 348 | return visit_continue; |
| 349 | } |
| 350 | |
Ian Romanick | 4ff9e59 | 2015-08-19 13:36:22 -0700 | [diff] [blame] | 351 | virtual ir_visitor_status visit_leave(ir_loop *) |
Chris Forbes | 8cf7297 | 2014-09-07 21:42:50 +1200 | [diff] [blame] | 352 | { |
| 353 | --control_flow; |
| 354 | return visit_continue; |
| 355 | } |
| 356 | |
| 357 | /* FINISHME: `switch` is not expressed at the IR level -- it's already |
| 358 | * been lowered to a mess of `if`s. We'll correctly disallow any use of |
| 359 | * barrier() in a conditional path within the switch, but not in a path |
| 360 | * which is always hit. |
| 361 | */ |
| 362 | |
| 363 | virtual ir_visitor_status visit_enter(ir_call *ir) |
| 364 | { |
| 365 | if (ir->use_builtin && strcmp(ir->callee_name(), "barrier") == 0) { |
| 366 | /* Use of barrier(); determine if it is legal: */ |
| 367 | if (!in_main) { |
| 368 | linker_error(prog, "Builtin barrier() may only be used in main"); |
| 369 | return visit_stop; |
| 370 | } |
| 371 | |
| 372 | if (after_return) { |
| 373 | linker_error(prog, "Builtin barrier() may not be used after return"); |
| 374 | return visit_stop; |
| 375 | } |
| 376 | |
| 377 | if (control_flow != 0) { |
| 378 | linker_error(prog, "Builtin barrier() may not be used inside control flow"); |
| 379 | return visit_stop; |
| 380 | } |
| 381 | } |
| 382 | return visit_continue; |
| 383 | } |
| 384 | |
| 385 | private: |
| 386 | gl_shader_program *prog; |
| 387 | bool in_main, after_return; |
| 388 | int control_flow; |
| 389 | }; |
| 390 | |
Paul Berry | 1a33e02 | 2013-08-18 20:59:37 -0700 | [diff] [blame] | 391 | /** |
Iago Toral Quiroga | 7589683 | 2014-06-16 16:09:53 +0200 | [diff] [blame] | 392 | * Visitor that determines the highest stream id to which a (geometry) shader |
| 393 | * emits vertices. It also checks whether End{Stream}Primitive is ever called. |
Paul Berry | 1a33e02 | 2013-08-18 20:59:37 -0700 | [diff] [blame] | 394 | */ |
Iago Toral Quiroga | 7589683 | 2014-06-16 16:09:53 +0200 | [diff] [blame] | 395 | class find_emit_vertex_visitor : public ir_hierarchical_visitor { |
Paul Berry | 1a33e02 | 2013-08-18 20:59:37 -0700 | [diff] [blame] | 396 | public: |
Iago Toral Quiroga | 7589683 | 2014-06-16 16:09:53 +0200 | [diff] [blame] | 397 | find_emit_vertex_visitor(int max_allowed) |
| 398 | : max_stream_allowed(max_allowed), |
| 399 | invalid_stream_id(0), |
| 400 | invalid_stream_id_from_emit_vertex(false), |
| 401 | end_primitive_found(false), |
| 402 | uses_non_zero_stream(false) |
Paul Berry | 1a33e02 | 2013-08-18 20:59:37 -0700 | [diff] [blame] | 403 | { |
| 404 | /* empty */ |
| 405 | } |
| 406 | |
Iago Toral Quiroga | 7589683 | 2014-06-16 16:09:53 +0200 | [diff] [blame] | 407 | virtual ir_visitor_status visit_leave(ir_emit_vertex *ir) |
Paul Berry | 1a33e02 | 2013-08-18 20:59:37 -0700 | [diff] [blame] | 408 | { |
Iago Toral Quiroga | 7589683 | 2014-06-16 16:09:53 +0200 | [diff] [blame] | 409 | int stream_id = ir->stream_id(); |
| 410 | |
| 411 | if (stream_id < 0) { |
| 412 | invalid_stream_id = stream_id; |
| 413 | invalid_stream_id_from_emit_vertex = true; |
| 414 | return visit_stop; |
| 415 | } |
| 416 | |
| 417 | if (stream_id > max_stream_allowed) { |
| 418 | invalid_stream_id = stream_id; |
| 419 | invalid_stream_id_from_emit_vertex = true; |
| 420 | return visit_stop; |
| 421 | } |
| 422 | |
| 423 | if (stream_id != 0) |
| 424 | uses_non_zero_stream = true; |
| 425 | |
| 426 | return visit_continue; |
Paul Berry | 1a33e02 | 2013-08-18 20:59:37 -0700 | [diff] [blame] | 427 | } |
| 428 | |
Iago Toral Quiroga | 7589683 | 2014-06-16 16:09:53 +0200 | [diff] [blame] | 429 | virtual ir_visitor_status visit_leave(ir_end_primitive *ir) |
Paul Berry | 1a33e02 | 2013-08-18 20:59:37 -0700 | [diff] [blame] | 430 | { |
Iago Toral Quiroga | 7589683 | 2014-06-16 16:09:53 +0200 | [diff] [blame] | 431 | end_primitive_found = true; |
| 432 | |
| 433 | int stream_id = ir->stream_id(); |
| 434 | |
| 435 | if (stream_id < 0) { |
| 436 | invalid_stream_id = stream_id; |
| 437 | invalid_stream_id_from_emit_vertex = false; |
| 438 | return visit_stop; |
| 439 | } |
| 440 | |
| 441 | if (stream_id > max_stream_allowed) { |
| 442 | invalid_stream_id = stream_id; |
| 443 | invalid_stream_id_from_emit_vertex = false; |
| 444 | return visit_stop; |
| 445 | } |
| 446 | |
| 447 | if (stream_id != 0) |
| 448 | uses_non_zero_stream = true; |
| 449 | |
| 450 | return visit_continue; |
| 451 | } |
| 452 | |
| 453 | bool error() |
| 454 | { |
| 455 | return invalid_stream_id != 0; |
| 456 | } |
| 457 | |
| 458 | const char *error_func() |
| 459 | { |
| 460 | return invalid_stream_id_from_emit_vertex ? |
| 461 | "EmitStreamVertex" : "EndStreamPrimitive"; |
| 462 | } |
| 463 | |
| 464 | int error_stream() |
| 465 | { |
| 466 | return invalid_stream_id; |
| 467 | } |
| 468 | |
| 469 | bool uses_streams() |
| 470 | { |
| 471 | return uses_non_zero_stream; |
| 472 | } |
| 473 | |
| 474 | bool uses_end_primitive() |
| 475 | { |
| 476 | return end_primitive_found; |
Paul Berry | 1a33e02 | 2013-08-18 20:59:37 -0700 | [diff] [blame] | 477 | } |
| 478 | |
| 479 | private: |
Iago Toral Quiroga | 7589683 | 2014-06-16 16:09:53 +0200 | [diff] [blame] | 480 | int max_stream_allowed; |
| 481 | int invalid_stream_id; |
| 482 | bool invalid_stream_id_from_emit_vertex; |
| 483 | bool end_primitive_found; |
| 484 | bool uses_non_zero_stream; |
Paul Berry | 1a33e02 | 2013-08-18 20:59:37 -0700 | [diff] [blame] | 485 | }; |
| 486 | |
Tapani Pälli | 9350ea6 | 2015-05-19 15:01:49 +0300 | [diff] [blame] | 487 | /* Class that finds array derefs and check if indexes are dynamic. */ |
| 488 | class dynamic_sampler_array_indexing_visitor : public ir_hierarchical_visitor |
| 489 | { |
| 490 | public: |
| 491 | dynamic_sampler_array_indexing_visitor() : |
| 492 | dynamic_sampler_array_indexing(false) |
| 493 | { |
| 494 | } |
| 495 | |
| 496 | ir_visitor_status visit_enter(ir_dereference_array *ir) |
| 497 | { |
| 498 | if (!ir->variable_referenced()) |
| 499 | return visit_continue; |
| 500 | |
| 501 | if (!ir->variable_referenced()->type->contains_sampler()) |
| 502 | return visit_continue; |
| 503 | |
| 504 | if (!ir->array_index->constant_expression_value()) { |
| 505 | dynamic_sampler_array_indexing = true; |
| 506 | return visit_stop; |
| 507 | } |
| 508 | return visit_continue; |
| 509 | } |
| 510 | |
| 511 | bool uses_dynamic_sampler_array_indexing() |
| 512 | { |
| 513 | return dynamic_sampler_array_indexing; |
| 514 | } |
| 515 | |
| 516 | private: |
| 517 | bool dynamic_sampler_array_indexing; |
| 518 | }; |
| 519 | |
Eric Anholt | 10ef949 | 2013-09-20 11:03:44 -0700 | [diff] [blame] | 520 | } /* anonymous namespace */ |
Paul Berry | 1a33e02 | 2013-08-18 20:59:37 -0700 | [diff] [blame] | 521 | |
Ian Romanick | 0ad22cd | 2010-06-21 17:18:31 -0700 | [diff] [blame] | 522 | void |
Ian Romanick | 586e741 | 2011-07-28 14:04:09 -0700 | [diff] [blame] | 523 | linker_error(gl_shader_program *prog, const char *fmt, ...) |
Ian Romanick | f36460e | 2010-06-23 12:07:22 -0700 | [diff] [blame] | 524 | { |
| 525 | va_list ap; |
| 526 | |
Kenneth Graunke | d3073f5 | 2011-01-21 14:32:31 -0800 | [diff] [blame] | 527 | ralloc_strcat(&prog->InfoLog, "error: "); |
Ian Romanick | f36460e | 2010-06-23 12:07:22 -0700 | [diff] [blame] | 528 | va_start(ap, fmt); |
Kenneth Graunke | d3073f5 | 2011-01-21 14:32:31 -0800 | [diff] [blame] | 529 | ralloc_vasprintf_append(&prog->InfoLog, fmt, ap); |
Ian Romanick | f36460e | 2010-06-23 12:07:22 -0700 | [diff] [blame] | 530 | va_end(ap); |
Ian Romanick | 586e741 | 2011-07-28 14:04:09 -0700 | [diff] [blame] | 531 | |
| 532 | prog->LinkStatus = false; |
Ian Romanick | f36460e | 2010-06-23 12:07:22 -0700 | [diff] [blame] | 533 | } |
| 534 | |
| 535 | |
| 536 | void |
Ian Romanick | 379a32f | 2011-07-28 14:09:06 -0700 | [diff] [blame] | 537 | linker_warning(gl_shader_program *prog, const char *fmt, ...) |
| 538 | { |
| 539 | va_list ap; |
| 540 | |
Anuj Phogat | 80b4a36 | 2014-03-07 16:48:35 -0800 | [diff] [blame] | 541 | ralloc_strcat(&prog->InfoLog, "warning: "); |
Ian Romanick | 379a32f | 2011-07-28 14:09:06 -0700 | [diff] [blame] | 542 | va_start(ap, fmt); |
| 543 | ralloc_vasprintf_append(&prog->InfoLog, fmt, ap); |
| 544 | va_end(ap); |
| 545 | |
| 546 | } |
| 547 | |
| 548 | |
Paul Berry | b92900d | 2013-01-28 14:21:59 -0800 | [diff] [blame] | 549 | /** |
| 550 | * Given a string identifying a program resource, break it into a base name |
| 551 | * and an optional array index in square brackets. |
| 552 | * |
| 553 | * If an array index is present, \c out_base_name_end is set to point to the |
| 554 | * "[" that precedes the array index, and the array index itself is returned |
| 555 | * as a long. |
| 556 | * |
| 557 | * If no array index is present (or if the array index is negative or |
| 558 | * mal-formed), \c out_base_name_end, is set to point to the null terminator |
| 559 | * at the end of the input string, and -1 is returned. |
| 560 | * |
| 561 | * Only the final array index is parsed; if the string contains other array |
| 562 | * indices (or structure field accesses), they are left in the base name. |
| 563 | * |
| 564 | * No attempt is made to check that the base name is properly formed; |
| 565 | * typically the caller will look up the base name in a hash table, so |
| 566 | * ill-formed base names simply turn into hash table lookup failures. |
| 567 | */ |
| 568 | long |
| 569 | parse_program_resource_name(const GLchar *name, |
| 570 | const GLchar **out_base_name_end) |
| 571 | { |
| 572 | /* Section 7.3.1 ("Program Interfaces") of the OpenGL 4.3 spec says: |
| 573 | * |
| 574 | * "When an integer array element or block instance number is part of |
| 575 | * the name string, it will be specified in decimal form without a "+" |
| 576 | * or "-" sign or any extra leading zeroes. Additionally, the name |
| 577 | * string will not include white space anywhere in the string." |
| 578 | */ |
| 579 | |
| 580 | const size_t len = strlen(name); |
| 581 | *out_base_name_end = name + len; |
| 582 | |
| 583 | if (len == 0 || name[len-1] != ']') |
| 584 | return -1; |
| 585 | |
| 586 | /* Walk backwards over the string looking for a non-digit character. This |
| 587 | * had better be the opening bracket for an array index. |
| 588 | * |
| 589 | * Initially, i specifies the location of the ']'. Since the string may |
| 590 | * contain only the ']' charcater, walk backwards very carefully. |
| 591 | */ |
| 592 | unsigned i; |
| 593 | for (i = len - 1; (i > 0) && isdigit(name[i-1]); --i) |
| 594 | /* empty */ ; |
| 595 | |
| 596 | if ((i == 0) || name[i-1] != '[') |
| 597 | return -1; |
| 598 | |
| 599 | long array_index = strtol(&name[i], NULL, 10); |
| 600 | if (array_index < 0) |
| 601 | return -1; |
| 602 | |
Timothy Arceri | 09c440c | 2015-07-03 08:45:30 +1000 | [diff] [blame] | 603 | /* Check for leading zero */ |
| 604 | if (name[i] == '0' && name[i+1] != ']') |
| 605 | return -1; |
| 606 | |
Paul Berry | b92900d | 2013-01-28 14:21:59 -0800 | [diff] [blame] | 607 | *out_base_name_end = name + (i - 1); |
| 608 | return array_index; |
| 609 | } |
| 610 | |
| 611 | |
Ian Romanick | 379a32f | 2011-07-28 14:09:06 -0700 | [diff] [blame] | 612 | void |
Ian Romanick | 63974c0 | 2013-10-04 10:46:29 -0700 | [diff] [blame] | 613 | link_invalidate_variable_locations(exec_list *ir) |
Ian Romanick | 0ad22cd | 2010-06-21 17:18:31 -0700 | [diff] [blame] | 614 | { |
Matt Turner | 4d78446 | 2014-06-24 21:34:05 -0700 | [diff] [blame] | 615 | foreach_in_list(ir_instruction, node, ir) { |
| 616 | ir_variable *const var = node->as_variable(); |
Ian Romanick | 0ad22cd | 2010-06-21 17:18:31 -0700 | [diff] [blame] | 617 | |
Paul Berry | 50895d4 | 2012-12-05 07:17:07 -0800 | [diff] [blame] | 618 | if (var == NULL) |
| 619 | continue; |
| 620 | |
Ian Romanick | 63974c0 | 2013-10-04 10:46:29 -0700 | [diff] [blame] | 621 | /* Only assign locations for variables that lack an explicit location. |
| 622 | * Explicit locations are set for all built-in variables, generic vertex |
| 623 | * shader inputs (via layout(location=...)), and generic fragment shader |
| 624 | * outputs (also via layout(location=...)). |
| 625 | */ |
Tapani Pälli | 447bb90 | 2013-12-12 15:08:59 +0200 | [diff] [blame] | 626 | if (!var->data.explicit_location) { |
| 627 | var->data.location = -1; |
| 628 | var->data.location_frac = 0; |
Paul Berry | 50895d4 | 2012-12-05 07:17:07 -0800 | [diff] [blame] | 629 | } |
Ian Romanick | 0ad22cd | 2010-06-21 17:18:31 -0700 | [diff] [blame] | 630 | |
Ian Romanick | 63974c0 | 2013-10-04 10:46:29 -0700 | [diff] [blame] | 631 | /* ir_variable::is_unmatched_generic_inout is used by the linker while |
| 632 | * connecting outputs from one stage to inputs of the next stage. |
| 633 | * |
| 634 | * There are two implicit assumptions here. First, we assume that any |
| 635 | * built-in variable (i.e., non-generic in or out) will have |
| 636 | * explicit_location set. Second, we assume that any generic in or out |
| 637 | * will not have explicit_location set. |
| 638 | * |
| 639 | * This second assumption will only be valid until |
| 640 | * GL_ARB_separate_shader_objects is supported. When that extension is |
| 641 | * implemented, this function will need some modifications. |
Ian Romanick | 0ad22cd | 2010-06-21 17:18:31 -0700 | [diff] [blame] | 642 | */ |
Tapani Pälli | 447bb90 | 2013-12-12 15:08:59 +0200 | [diff] [blame] | 643 | if (!var->data.explicit_location) { |
| 644 | var->data.is_unmatched_generic_inout = 1; |
Paul Berry | 3e81c66 | 2012-12-05 10:47:55 -0800 | [diff] [blame] | 645 | } else { |
Tapani Pälli | 447bb90 | 2013-12-12 15:08:59 +0200 | [diff] [blame] | 646 | var->data.is_unmatched_generic_inout = 0; |
Paul Berry | 3e81c66 | 2012-12-05 10:47:55 -0800 | [diff] [blame] | 647 | } |
Ian Romanick | 0ad22cd | 2010-06-21 17:18:31 -0700 | [diff] [blame] | 648 | } |
| 649 | } |
| 650 | |
| 651 | |
Ian Romanick | c93b8f1 | 2010-06-17 15:20:22 -0700 | [diff] [blame] | 652 | /** |
Paul Berry | 44e07de | 2013-06-11 14:11:05 -0700 | [diff] [blame] | 653 | * Set UsesClipDistance and ClipDistanceArraySize based on the given shader. |
| 654 | * |
| 655 | * Also check for errors based on incorrect usage of gl_ClipVertex and |
| 656 | * gl_ClipDistance. |
| 657 | * |
| 658 | * Return false if an error was reported. |
| 659 | */ |
| 660 | static void |
Paul Berry | b30e25f | 2013-12-17 09:49:43 -0800 | [diff] [blame] | 661 | analyze_clip_usage(struct gl_shader_program *prog, |
Paul Berry | 44e07de | 2013-06-11 14:11:05 -0700 | [diff] [blame] | 662 | struct gl_shader *shader, GLboolean *UsesClipDistance, |
| 663 | GLuint *ClipDistanceArraySize) |
| 664 | { |
| 665 | *ClipDistanceArraySize = 0; |
| 666 | |
| 667 | if (!prog->IsES && prog->Version >= 130) { |
| 668 | /* From section 7.1 (Vertex Shader Special Variables) of the |
| 669 | * GLSL 1.30 spec: |
| 670 | * |
| 671 | * "It is an error for a shader to statically write both |
| 672 | * gl_ClipVertex and gl_ClipDistance." |
| 673 | * |
| 674 | * This does not apply to GLSL ES shaders, since GLSL ES defines neither |
| 675 | * gl_ClipVertex nor gl_ClipDistance. |
| 676 | */ |
| 677 | find_assignment_visitor clip_vertex("gl_ClipVertex"); |
| 678 | find_assignment_visitor clip_distance("gl_ClipDistance"); |
| 679 | |
| 680 | clip_vertex.run(shader->ir); |
| 681 | clip_distance.run(shader->ir); |
| 682 | if (clip_vertex.variable_found() && clip_distance.variable_found()) { |
| 683 | linker_error(prog, "%s shader writes to both `gl_ClipVertex' " |
Paul Berry | b30e25f | 2013-12-17 09:49:43 -0800 | [diff] [blame] | 684 | "and `gl_ClipDistance'\n", |
Paul Berry | e3b86f0 | 2014-01-07 10:58:56 -0800 | [diff] [blame] | 685 | _mesa_shader_stage_to_string(shader->Stage)); |
Paul Berry | 44e07de | 2013-06-11 14:11:05 -0700 | [diff] [blame] | 686 | return; |
| 687 | } |
| 688 | *UsesClipDistance = clip_distance.variable_found(); |
| 689 | ir_variable *clip_distance_var = |
| 690 | shader->symbols->get_variable("gl_ClipDistance"); |
| 691 | if (clip_distance_var) |
| 692 | *ClipDistanceArraySize = clip_distance_var->type->length; |
| 693 | } else { |
| 694 | *UsesClipDistance = false; |
| 695 | } |
| 696 | } |
| 697 | |
| 698 | |
| 699 | /** |
Paul Berry | 1ad54ae | 2011-09-17 09:42:02 -0700 | [diff] [blame] | 700 | * Verify that a vertex shader executable meets all semantic requirements. |
| 701 | * |
Paul Berry | 642e5b41 | 2012-01-04 13:57:52 -0800 | [diff] [blame] | 702 | * Also sets prog->Vert.UsesClipDistance and prog->Vert.ClipDistanceArraySize |
| 703 | * as a side effect. |
Ian Romanick | c93b8f1 | 2010-06-17 15:20:22 -0700 | [diff] [blame] | 704 | * |
| 705 | * \param shader Vertex shader executable to be verified |
| 706 | */ |
Paul Berry | b95d237 | 2013-07-27 11:08:31 -0700 | [diff] [blame] | 707 | void |
Eric Anholt | 849e181 | 2010-06-30 11:49:17 -0700 | [diff] [blame] | 708 | validate_vertex_shader_executable(struct gl_shader_program *prog, |
Eric Anholt | 16b68b1 | 2010-06-30 11:05:43 -0700 | [diff] [blame] | 709 | struct gl_shader *shader) |
Ian Romanick | 832dfa5 | 2010-06-17 15:04:20 -0700 | [diff] [blame] | 710 | { |
| 711 | if (shader == NULL) |
Paul Berry | b95d237 | 2013-07-27 11:08:31 -0700 | [diff] [blame] | 712 | return; |
Ian Romanick | 832dfa5 | 2010-06-17 15:04:20 -0700 | [diff] [blame] | 713 | |
Eric Anholt | f1c1c9e | 2012-03-19 22:43:27 -0700 | [diff] [blame] | 714 | /* From the GLSL 1.10 spec, page 48: |
| 715 | * |
| 716 | * "The variable gl_Position is available only in the vertex |
| 717 | * language and is intended for writing the homogeneous vertex |
| 718 | * position. All executions of a well-formed vertex shader |
| 719 | * executable must write a value into this variable. [...] The |
| 720 | * variable gl_Position is available only in the vertex |
| 721 | * language and is intended for writing the homogeneous vertex |
| 722 | * position. All executions of a well-formed vertex shader |
| 723 | * executable must write a value into this variable." |
| 724 | * |
| 725 | * while in GLSL 1.40 this text is changed to: |
| 726 | * |
| 727 | * "The variable gl_Position is available only in the vertex |
| 728 | * language and is intended for writing the homogeneous vertex |
| 729 | * position. It can be written at any time during shader |
| 730 | * execution. It may also be read back by a vertex shader |
| 731 | * after being written. This value will be used by primitive |
| 732 | * assembly, clipping, culling, and other fixed functionality |
| 733 | * operations, if present, that operate on primitives after |
| 734 | * vertex processing has occurred. Its value is undefined if |
| 735 | * the vertex shader executable does not write gl_Position." |
Paul Berry | 15ba2a5 | 2012-08-02 17:51:02 -0700 | [diff] [blame] | 736 | * |
Kalyan Kondapally | 78c9201 | 2014-09-08 11:10:42 +0300 | [diff] [blame] | 737 | * All GLSL ES Versions are similar to GLSL 1.40--failing to write to |
| 738 | * gl_Position is not an error. |
Eric Anholt | f1c1c9e | 2012-03-19 22:43:27 -0700 | [diff] [blame] | 739 | */ |
Kalyan Kondapally | dbc2d81 | 2014-09-10 20:20:23 -0700 | [diff] [blame] | 740 | if (prog->Version < (prog->IsES ? 300 : 140)) { |
Eric Anholt | f1c1c9e | 2012-03-19 22:43:27 -0700 | [diff] [blame] | 741 | find_assignment_visitor find("gl_Position"); |
| 742 | find.run(shader->ir); |
| 743 | if (!find.variable_found()) { |
Kalyan Kondapally | dbc2d81 | 2014-09-10 20:20:23 -0700 | [diff] [blame] | 744 | if (prog->IsES) { |
| 745 | linker_warning(prog, |
| 746 | "vertex shader does not write to `gl_Position'." |
| 747 | "It's value is undefined. \n"); |
| 748 | } else { |
| 749 | linker_error(prog, |
| 750 | "vertex shader does not write to `gl_Position'. \n"); |
| 751 | } |
Paul Berry | b95d237 | 2013-07-27 11:08:31 -0700 | [diff] [blame] | 752 | return; |
Eric Anholt | f1c1c9e | 2012-03-19 22:43:27 -0700 | [diff] [blame] | 753 | } |
Ian Romanick | 832dfa5 | 2010-06-17 15:04:20 -0700 | [diff] [blame] | 754 | } |
| 755 | |
Paul Berry | b30e25f | 2013-12-17 09:49:43 -0800 | [diff] [blame] | 756 | analyze_clip_usage(prog, shader, &prog->Vert.UsesClipDistance, |
Paul Berry | 44e07de | 2013-06-11 14:11:05 -0700 | [diff] [blame] | 757 | &prog->Vert.ClipDistanceArraySize); |
Ian Romanick | 832dfa5 | 2010-06-17 15:04:20 -0700 | [diff] [blame] | 758 | } |
| 759 | |
Chris Forbes | df16e0d | 2014-09-09 19:25:02 +1200 | [diff] [blame] | 760 | void |
| 761 | validate_tess_eval_shader_executable(struct gl_shader_program *prog, |
| 762 | struct gl_shader *shader) |
| 763 | { |
| 764 | if (shader == NULL) |
| 765 | return; |
| 766 | |
| 767 | analyze_clip_usage(prog, shader, &prog->TessEval.UsesClipDistance, |
| 768 | &prog->TessEval.ClipDistanceArraySize); |
| 769 | } |
| 770 | |
Ian Romanick | 832dfa5 | 2010-06-17 15:04:20 -0700 | [diff] [blame] | 771 | |
Ian Romanick | c93b8f1 | 2010-06-17 15:20:22 -0700 | [diff] [blame] | 772 | /** |
| 773 | * Verify that a fragment shader executable meets all semantic requirements |
| 774 | * |
| 775 | * \param shader Fragment shader executable to be verified |
| 776 | */ |
Paul Berry | b95d237 | 2013-07-27 11:08:31 -0700 | [diff] [blame] | 777 | void |
Eric Anholt | 849e181 | 2010-06-30 11:49:17 -0700 | [diff] [blame] | 778 | validate_fragment_shader_executable(struct gl_shader_program *prog, |
Eric Anholt | 16b68b1 | 2010-06-30 11:05:43 -0700 | [diff] [blame] | 779 | struct gl_shader *shader) |
Ian Romanick | 832dfa5 | 2010-06-17 15:04:20 -0700 | [diff] [blame] | 780 | { |
| 781 | if (shader == NULL) |
Paul Berry | b95d237 | 2013-07-27 11:08:31 -0700 | [diff] [blame] | 782 | return; |
Ian Romanick | 832dfa5 | 2010-06-17 15:04:20 -0700 | [diff] [blame] | 783 | |
Ian Romanick | 832dfa5 | 2010-06-17 15:04:20 -0700 | [diff] [blame] | 784 | find_assignment_visitor frag_color("gl_FragColor"); |
| 785 | find_assignment_visitor frag_data("gl_FragData"); |
| 786 | |
Eric Anholt | 16b68b1 | 2010-06-30 11:05:43 -0700 | [diff] [blame] | 787 | frag_color.run(shader->ir); |
| 788 | frag_data.run(shader->ir); |
Ian Romanick | 832dfa5 | 2010-06-17 15:04:20 -0700 | [diff] [blame] | 789 | |
Ian Romanick | 832dfa5 | 2010-06-17 15:04:20 -0700 | [diff] [blame] | 790 | if (frag_color.variable_found() && frag_data.variable_found()) { |
Ian Romanick | 586e741 | 2011-07-28 14:04:09 -0700 | [diff] [blame] | 791 | linker_error(prog, "fragment shader writes to both " |
| 792 | "`gl_FragColor' and `gl_FragData'\n"); |
Ian Romanick | 832dfa5 | 2010-06-17 15:04:20 -0700 | [diff] [blame] | 793 | } |
Ian Romanick | 832dfa5 | 2010-06-17 15:04:20 -0700 | [diff] [blame] | 794 | } |
| 795 | |
Bryan Cain | 2548092 | 2013-02-15 09:46:50 -0600 | [diff] [blame] | 796 | /** |
| 797 | * Verify that a geometry shader executable meets all semantic requirements |
| 798 | * |
Paul Berry | 44e07de | 2013-06-11 14:11:05 -0700 | [diff] [blame] | 799 | * Also sets prog->Geom.VerticesIn, prog->Geom.UsesClipDistance, and |
| 800 | * prog->Geom.ClipDistanceArraySize as a side effect. |
Bryan Cain | 2548092 | 2013-02-15 09:46:50 -0600 | [diff] [blame] | 801 | * |
| 802 | * \param shader Geometry shader executable to be verified |
| 803 | */ |
| 804 | void |
| 805 | validate_geometry_shader_executable(struct gl_shader_program *prog, |
| 806 | struct gl_shader *shader) |
| 807 | { |
| 808 | if (shader == NULL) |
| 809 | return; |
| 810 | |
| 811 | unsigned num_vertices = vertices_per_prim(prog->Geom.InputType); |
| 812 | prog->Geom.VerticesIn = num_vertices; |
Paul Berry | 44e07de | 2013-06-11 14:11:05 -0700 | [diff] [blame] | 813 | |
Paul Berry | b30e25f | 2013-12-17 09:49:43 -0800 | [diff] [blame] | 814 | analyze_clip_usage(prog, shader, &prog->Geom.UsesClipDistance, |
Paul Berry | 44e07de | 2013-06-11 14:11:05 -0700 | [diff] [blame] | 815 | &prog->Geom.ClipDistanceArraySize); |
Iago Toral Quiroga | 7589683 | 2014-06-16 16:09:53 +0200 | [diff] [blame] | 816 | } |
Paul Berry | 1a33e02 | 2013-08-18 20:59:37 -0700 | [diff] [blame] | 817 | |
Iago Toral Quiroga | 7589683 | 2014-06-16 16:09:53 +0200 | [diff] [blame] | 818 | /** |
| 819 | * Check if geometry shaders emit to non-zero streams and do corresponding |
| 820 | * validations. |
| 821 | */ |
| 822 | static void |
| 823 | validate_geometry_shader_emissions(struct gl_context *ctx, |
| 824 | struct gl_shader_program *prog) |
| 825 | { |
| 826 | if (prog->_LinkedShaders[MESA_SHADER_GEOMETRY] != NULL) { |
| 827 | find_emit_vertex_visitor emit_vertex(ctx->Const.MaxVertexStreams - 1); |
| 828 | emit_vertex.run(prog->_LinkedShaders[MESA_SHADER_GEOMETRY]->ir); |
| 829 | if (emit_vertex.error()) { |
| 830 | linker_error(prog, "Invalid call %s(%d). Accepted values for the " |
Andres Gomez | f9fc3ae | 2014-11-18 08:43:35 -0700 | [diff] [blame] | 831 | "stream parameter are in the range [0, %d].\n", |
Iago Toral Quiroga | 7589683 | 2014-06-16 16:09:53 +0200 | [diff] [blame] | 832 | emit_vertex.error_func(), |
| 833 | emit_vertex.error_stream(), |
| 834 | ctx->Const.MaxVertexStreams - 1); |
| 835 | } |
| 836 | prog->Geom.UsesStreams = emit_vertex.uses_streams(); |
| 837 | prog->Geom.UsesEndPrimitive = emit_vertex.uses_end_primitive(); |
| 838 | |
| 839 | /* From the ARB_gpu_shader5 spec: |
| 840 | * |
| 841 | * "Multiple vertex streams are supported only if the output primitive |
| 842 | * type is declared to be "points". A program will fail to link if it |
| 843 | * contains a geometry shader calling EmitStreamVertex() or |
| 844 | * EndStreamPrimitive() if its output primitive type is not "points". |
| 845 | * |
| 846 | * However, in the same spec: |
| 847 | * |
| 848 | * "The function EmitVertex() is equivalent to calling EmitStreamVertex() |
| 849 | * with <stream> set to zero." |
| 850 | * |
| 851 | * And: |
| 852 | * |
| 853 | * "The function EndPrimitive() is equivalent to calling |
| 854 | * EndStreamPrimitive() with <stream> set to zero." |
| 855 | * |
| 856 | * Since we can call EmitVertex() and EndPrimitive() when we output |
| 857 | * primitives other than points, calling EmitStreamVertex(0) or |
| 858 | * EmitEndPrimitive(0) should not produce errors. This it also what Nvidia |
| 859 | * does. Currently we only set prog->Geom.UsesStreams to TRUE when |
| 860 | * EmitStreamVertex() or EmitEndPrimitive() are called with a non-zero |
| 861 | * stream. |
| 862 | */ |
| 863 | if (prog->Geom.UsesStreams && prog->Geom.OutputType != GL_POINTS) { |
| 864 | linker_error(prog, "EmitStreamVertex(n) and EndStreamPrimitive(n) " |
Andres Gomez | f9fc3ae | 2014-11-18 08:43:35 -0700 | [diff] [blame] | 865 | "with n>0 requires point output\n"); |
Iago Toral Quiroga | 7589683 | 2014-06-16 16:09:53 +0200 | [diff] [blame] | 866 | } |
| 867 | } |
Bryan Cain | 2548092 | 2013-02-15 09:46:50 -0600 | [diff] [blame] | 868 | } |
| 869 | |
Timothy Arceri | 50859c6 | 2015-02-21 21:47:14 +1100 | [diff] [blame] | 870 | bool |
| 871 | validate_intrastage_arrays(struct gl_shader_program *prog, |
| 872 | ir_variable *const var, |
| 873 | ir_variable *const existing) |
| 874 | { |
| 875 | /* Consider the types to be "the same" if both types are arrays |
| 876 | * of the same type and one of the arrays is implicitly sized. |
| 877 | * In addition, set the type of the linked variable to the |
| 878 | * explicitly sized array. |
| 879 | */ |
Samuel Iglesias Gonsalvez | f3f64cd | 2015-03-18 15:32:03 +0100 | [diff] [blame] | 880 | if (var->type->is_array() && existing->type->is_array()) { |
| 881 | if ((var->type->fields.array == existing->type->fields.array) && |
| 882 | ((var->type->length == 0)|| (existing->type->length == 0))) { |
| 883 | if (var->type->length != 0) { |
| 884 | if (var->type->length <= existing->data.max_array_access) { |
| 885 | linker_error(prog, "%s `%s' declared as type " |
| 886 | "`%s' but outermost dimension has an index" |
| 887 | " of `%i'\n", |
| 888 | mode_string(var), |
| 889 | var->name, var->type->name, |
| 890 | existing->data.max_array_access); |
| 891 | } |
| 892 | existing->type = var->type; |
| 893 | return true; |
| 894 | } else if (existing->type->length != 0) { |
| 895 | if(existing->type->length <= var->data.max_array_access && |
| 896 | !existing->data.from_ssbo_unsized_array) { |
| 897 | linker_error(prog, "%s `%s' declared as type " |
| 898 | "`%s' but outermost dimension has an index" |
| 899 | " of `%i'\n", |
| 900 | mode_string(var), |
| 901 | var->name, existing->type->name, |
| 902 | var->data.max_array_access); |
| 903 | } |
| 904 | return true; |
Timothy Arceri | 50859c6 | 2015-02-21 21:47:14 +1100 | [diff] [blame] | 905 | } |
Samuel Iglesias Gonsalvez | f3f64cd | 2015-03-18 15:32:03 +0100 | [diff] [blame] | 906 | } else { |
| 907 | /* The arrays of structs could have different glsl_type pointers but |
| 908 | * they are actually the same type. Use record_compare() to check that. |
| 909 | */ |
| 910 | if (existing->type->fields.array->is_record() && |
| 911 | var->type->fields.array->is_record() && |
| 912 | existing->type->fields.array->record_compare(var->type->fields.array)) |
| 913 | return true; |
Timothy Arceri | 50859c6 | 2015-02-21 21:47:14 +1100 | [diff] [blame] | 914 | } |
| 915 | } |
| 916 | return false; |
| 917 | } |
| 918 | |
Ian Romanick | 832dfa5 | 2010-06-17 15:04:20 -0700 | [diff] [blame] | 919 | |
Ian Romanick | cc22c5a | 2010-06-18 17:13:42 -0700 | [diff] [blame] | 920 | /** |
Ian Romanick | e2e5d0d | 2010-06-29 18:47:11 -0700 | [diff] [blame] | 921 | * Perform validation of global variables used across multiple shaders |
Ian Romanick | cc22c5a | 2010-06-18 17:13:42 -0700 | [diff] [blame] | 922 | */ |
Paul Berry | b95d237 | 2013-07-27 11:08:31 -0700 | [diff] [blame] | 923 | void |
Ian Romanick | e2e5d0d | 2010-06-29 18:47:11 -0700 | [diff] [blame] | 924 | cross_validate_globals(struct gl_shader_program *prog, |
| 925 | struct gl_shader **shader_list, |
| 926 | unsigned num_shaders, |
| 927 | bool uniforms_only) |
Ian Romanick | cc22c5a | 2010-06-18 17:13:42 -0700 | [diff] [blame] | 928 | { |
| 929 | /* Examine all of the uniforms in all of the shaders and cross validate |
| 930 | * them. |
| 931 | */ |
Ian Romanick | e2e5d0d | 2010-06-29 18:47:11 -0700 | [diff] [blame] | 932 | glsl_symbol_table variables; |
| 933 | for (unsigned i = 0; i < num_shaders; i++) { |
Ian Romanick | 3322fba | 2010-10-14 13:28:42 -0700 | [diff] [blame] | 934 | if (shader_list[i] == NULL) |
| 935 | continue; |
| 936 | |
Matt Turner | 4d78446 | 2014-06-24 21:34:05 -0700 | [diff] [blame] | 937 | foreach_in_list(ir_instruction, node, shader_list[i]->ir) { |
| 938 | ir_variable *const var = node->as_variable(); |
Ian Romanick | cc22c5a | 2010-06-18 17:13:42 -0700 | [diff] [blame] | 939 | |
Ian Romanick | e2e5d0d | 2010-06-29 18:47:11 -0700 | [diff] [blame] | 940 | if (var == NULL) |
Ian Romanick | cc22c5a | 2010-06-18 17:13:42 -0700 | [diff] [blame] | 941 | continue; |
| 942 | |
Kristian Høgsberg | a78a589 | 2015-05-13 11:17:23 +0200 | [diff] [blame] | 943 | if (uniforms_only && (var->data.mode != ir_var_uniform && var->data.mode != ir_var_shader_storage)) |
Ian Romanick | e2e5d0d | 2010-06-29 18:47:11 -0700 | [diff] [blame] | 944 | continue; |
| 945 | |
Dave Airlie | 6026686 | 2015-04-20 10:27:36 +1000 | [diff] [blame] | 946 | /* don't cross validate subroutine uniforms */ |
| 947 | if (var->type->contains_subroutine()) |
| 948 | continue; |
| 949 | |
Ian Romanick | 7e2aa91 | 2010-07-19 17:12:42 -0700 | [diff] [blame] | 950 | /* Don't cross validate temporaries that are at global scope. These |
| 951 | * will eventually get pulled into the shaders 'main'. |
| 952 | */ |
Tapani Pälli | 33ee2c6 | 2013-12-12 13:51:01 +0200 | [diff] [blame] | 953 | if (var->data.mode == ir_var_temporary) |
Ian Romanick | 7e2aa91 | 2010-07-19 17:12:42 -0700 | [diff] [blame] | 954 | continue; |
| 955 | |
Ian Romanick | e2e5d0d | 2010-06-29 18:47:11 -0700 | [diff] [blame] | 956 | /* If a global with this name has already been seen, verify that the |
| 957 | * new instance has the same type. In addition, if the globals have |
Ian Romanick | cc22c5a | 2010-06-18 17:13:42 -0700 | [diff] [blame] | 958 | * initializers, the values of the initializers must be the same. |
| 959 | */ |
Ian Romanick | e2e5d0d | 2010-06-29 18:47:11 -0700 | [diff] [blame] | 960 | ir_variable *const existing = variables.get_variable(var->name); |
Ian Romanick | cc22c5a | 2010-06-18 17:13:42 -0700 | [diff] [blame] | 961 | if (existing != NULL) { |
Timothy Arceri | 50859c6 | 2015-02-21 21:47:14 +1100 | [diff] [blame] | 962 | /* Check if types match. Interface blocks have some special |
| 963 | * rules so we handle those elsewhere. |
| 964 | */ |
Timothy Arceri | 1a96d9e | 2015-02-24 17:28:51 +1100 | [diff] [blame] | 965 | if (var->type != existing->type && |
| 966 | !var->is_interface_instance()) { |
Timothy Arceri | 50859c6 | 2015-02-21 21:47:14 +1100 | [diff] [blame] | 967 | if (!validate_intrastage_arrays(prog, var, existing)) { |
| 968 | if (var->type->is_record() && existing->type->is_record() |
| 969 | && existing->type->record_compare(var->type)) { |
| 970 | existing->type = var->type; |
| 971 | } else { |
Samuel Iglesias Gonsalvez | f3f64cd | 2015-03-18 15:32:03 +0100 | [diff] [blame] | 972 | /* If it is an unsized array in a Shader Storage Block, |
| 973 | * two different shaders can access to different elements. |
| 974 | * Because of that, they might be converted to different |
| 975 | * sized arrays, then check that they are compatible but |
| 976 | * ignore the array size. |
| 977 | */ |
| 978 | if (!(var->data.mode == ir_var_shader_storage && |
| 979 | var->data.from_ssbo_unsized_array && |
| 980 | existing->data.mode == ir_var_shader_storage && |
| 981 | existing->data.from_ssbo_unsized_array && |
| 982 | var->type->gl_type == existing->type->gl_type)) { |
| 983 | linker_error(prog, "%s `%s' declared as type " |
| 984 | "`%s' and type `%s'\n", |
| 985 | mode_string(var), |
| 986 | var->name, var->type->name, |
| 987 | existing->type->name); |
| 988 | return; |
| 989 | } |
Timothy Arceri | da4fb3e | 2014-11-25 23:04:23 +1100 | [diff] [blame] | 990 | } |
Ian Romanick | a2711d6 | 2010-08-29 22:07:49 -0700 | [diff] [blame] | 991 | } |
Ian Romanick | cc22c5a | 2010-06-18 17:13:42 -0700 | [diff] [blame] | 992 | } |
| 993 | |
Tapani Pälli | 447bb90 | 2013-12-12 15:08:59 +0200 | [diff] [blame] | 994 | if (var->data.explicit_location) { |
| 995 | if (existing->data.explicit_location |
| 996 | && (var->data.location != existing->data.location)) { |
Ian Romanick | 586e741 | 2011-07-28 14:04:09 -0700 | [diff] [blame] | 997 | linker_error(prog, "explicit locations for %s " |
| 998 | "`%s' have differing values\n", |
| 999 | mode_string(var), var->name); |
Paul Berry | b95d237 | 2013-07-27 11:08:31 -0700 | [diff] [blame] | 1000 | return; |
Ian Romanick | 68a4fc9 | 2010-10-07 17:21:22 -0700 | [diff] [blame] | 1001 | } |
| 1002 | |
Tapani Pälli | 447bb90 | 2013-12-12 15:08:59 +0200 | [diff] [blame] | 1003 | existing->data.location = var->data.location; |
| 1004 | existing->data.explicit_location = true; |
Ian Romanick | 68a4fc9 | 2010-10-07 17:21:22 -0700 | [diff] [blame] | 1005 | } |
| 1006 | |
Kenneth Graunke | 9a9a830 | 2013-07-16 12:18:57 -0700 | [diff] [blame] | 1007 | /* From the GLSL 4.20 specification: |
| 1008 | * "A link error will result if two compilation units in a program |
| 1009 | * specify different integer-constant bindings for the same |
| 1010 | * opaque-uniform name. However, it is not an error to specify a |
| 1011 | * binding on some but not all declarations for the same name" |
| 1012 | */ |
Tapani Pälli | 447bb90 | 2013-12-12 15:08:59 +0200 | [diff] [blame] | 1013 | if (var->data.explicit_binding) { |
| 1014 | if (existing->data.explicit_binding && |
| 1015 | var->data.binding != existing->data.binding) { |
Kenneth Graunke | 9a9a830 | 2013-07-16 12:18:57 -0700 | [diff] [blame] | 1016 | linker_error(prog, "explicit bindings for %s " |
| 1017 | "`%s' have differing values\n", |
| 1018 | mode_string(var), var->name); |
Paul Berry | b95d237 | 2013-07-27 11:08:31 -0700 | [diff] [blame] | 1019 | return; |
Kenneth Graunke | 9a9a830 | 2013-07-16 12:18:57 -0700 | [diff] [blame] | 1020 | } |
| 1021 | |
Tapani Pälli | 447bb90 | 2013-12-12 15:08:59 +0200 | [diff] [blame] | 1022 | existing->data.binding = var->data.binding; |
| 1023 | existing->data.explicit_binding = true; |
Kenneth Graunke | 9a9a830 | 2013-07-16 12:18:57 -0700 | [diff] [blame] | 1024 | } |
| 1025 | |
Francisco Jerez | 5c11493 | 2013-09-11 12:14:46 -0700 | [diff] [blame] | 1026 | if (var->type->contains_atomic() && |
Tapani Pälli | 447bb90 | 2013-12-12 15:08:59 +0200 | [diff] [blame] | 1027 | var->data.atomic.offset != existing->data.atomic.offset) { |
Francisco Jerez | 5c11493 | 2013-09-11 12:14:46 -0700 | [diff] [blame] | 1028 | linker_error(prog, "offset specifications for %s " |
| 1029 | "`%s' have differing values\n", |
| 1030 | mode_string(var), var->name); |
| 1031 | return; |
| 1032 | } |
| 1033 | |
Ian Romanick | 46173f9 | 2011-10-31 13:07:06 -0700 | [diff] [blame] | 1034 | /* Validate layout qualifiers for gl_FragDepth. |
| 1035 | * |
| 1036 | * From the AMD/ARB_conservative_depth specs: |
| 1037 | * |
| 1038 | * "If gl_FragDepth is redeclared in any fragment shader in a |
| 1039 | * program, it must be redeclared in all fragment shaders in |
| 1040 | * that program that have static assignments to |
| 1041 | * gl_FragDepth. All redeclarations of gl_FragDepth in all |
| 1042 | * fragment shaders in a single program must have the same set |
| 1043 | * of qualifiers." |
| 1044 | */ |
| 1045 | if (strcmp(var->name, "gl_FragDepth") == 0) { |
Tapani Pälli | 447bb90 | 2013-12-12 15:08:59 +0200 | [diff] [blame] | 1046 | bool layout_declared = var->data.depth_layout != ir_depth_layout_none; |
Ian Romanick | 46173f9 | 2011-10-31 13:07:06 -0700 | [diff] [blame] | 1047 | bool layout_differs = |
Tapani Pälli | 447bb90 | 2013-12-12 15:08:59 +0200 | [diff] [blame] | 1048 | var->data.depth_layout != existing->data.depth_layout; |
Ian Romanick | 46173f9 | 2011-10-31 13:07:06 -0700 | [diff] [blame] | 1049 | |
| 1050 | if (layout_declared && layout_differs) { |
| 1051 | linker_error(prog, |
| 1052 | "All redeclarations of gl_FragDepth in all " |
| 1053 | "fragment shaders in a single program must have " |
Andres Gomez | f9fc3ae | 2014-11-18 08:43:35 -0700 | [diff] [blame] | 1054 | "the same set of qualifiers.\n"); |
Ian Romanick | 46173f9 | 2011-10-31 13:07:06 -0700 | [diff] [blame] | 1055 | } |
| 1056 | |
Tapani Pälli | 33ee2c6 | 2013-12-12 13:51:01 +0200 | [diff] [blame] | 1057 | if (var->data.used && layout_differs) { |
Ian Romanick | 46173f9 | 2011-10-31 13:07:06 -0700 | [diff] [blame] | 1058 | linker_error(prog, |
| 1059 | "If gl_FragDepth is redeclared with a layout " |
| 1060 | "qualifier in any fragment shader, it must be " |
| 1061 | "redeclared with the same layout qualifier in " |
| 1062 | "all fragment shaders that have assignments to " |
Andres Gomez | f9fc3ae | 2014-11-18 08:43:35 -0700 | [diff] [blame] | 1063 | "gl_FragDepth\n"); |
Ian Romanick | 46173f9 | 2011-10-31 13:07:06 -0700 | [diff] [blame] | 1064 | } |
| 1065 | } |
Chad Versace | addae33 | 2011-01-27 01:40:31 -0800 | [diff] [blame] | 1066 | |
Ian Romanick | f37b1ad | 2011-10-31 14:31:07 -0700 | [diff] [blame] | 1067 | /* Page 35 (page 41 of the PDF) of the GLSL 4.20 spec says: |
| 1068 | * |
| 1069 | * "If a shared global has multiple initializers, the |
| 1070 | * initializers must all be constant expressions, and they |
| 1071 | * must all have the same value. Otherwise, a link error will |
| 1072 | * result. (A shared global having only one initializer does |
| 1073 | * not require that initializer to be a constant expression.)" |
| 1074 | * |
| 1075 | * Previous to 4.20 the GLSL spec simply said that initializers |
| 1076 | * must have the same value. In this case of non-constant |
| 1077 | * initializers, this was impossible to determine. As a result, |
| 1078 | * no vendor actually implemented that behavior. The 4.20 |
| 1079 | * behavior matches the implemented behavior of at least one other |
| 1080 | * vendor, so we'll implement that for all GLSL versions. |
Ian Romanick | e2e5d0d | 2010-06-29 18:47:11 -0700 | [diff] [blame] | 1081 | */ |
Ian Romanick | f37b1ad | 2011-10-31 14:31:07 -0700 | [diff] [blame] | 1082 | if (var->constant_initializer != NULL) { |
| 1083 | if (existing->constant_initializer != NULL) { |
| 1084 | if (!var->constant_initializer->has_value(existing->constant_initializer)) { |
Ian Romanick | 586e741 | 2011-07-28 14:04:09 -0700 | [diff] [blame] | 1085 | linker_error(prog, "initializers for %s " |
| 1086 | "`%s' have differing values\n", |
| 1087 | mode_string(var), var->name); |
Paul Berry | b95d237 | 2013-07-27 11:08:31 -0700 | [diff] [blame] | 1088 | return; |
Ian Romanick | cc22c5a | 2010-06-18 17:13:42 -0700 | [diff] [blame] | 1089 | } |
Ian Romanick | f37b1ad | 2011-10-31 14:31:07 -0700 | [diff] [blame] | 1090 | } else { |
Ian Romanick | cc22c5a | 2010-06-18 17:13:42 -0700 | [diff] [blame] | 1091 | /* If the first-seen instance of a particular uniform did not |
| 1092 | * have an initializer but a later instance does, copy the |
| 1093 | * initializer to the version stored in the symbol table. |
| 1094 | */ |
Ian Romanick | de415b7 | 2010-07-14 13:22:12 -0700 | [diff] [blame] | 1095 | /* FINISHME: This is wrong. The constant_value field should |
| 1096 | * FINISHME: not be modified! Imagine a case where a shader |
| 1097 | * FINISHME: without an initializer is linked in two different |
| 1098 | * FINISHME: programs with shaders that have differing |
| 1099 | * FINISHME: initializers. Linking with the first will |
| 1100 | * FINISHME: modify the shader, and linking with the second |
| 1101 | * FINISHME: will fail. |
| 1102 | */ |
Ian Romanick | f37b1ad | 2011-10-31 14:31:07 -0700 | [diff] [blame] | 1103 | existing->constant_initializer = |
| 1104 | var->constant_initializer->clone(ralloc_parent(existing), |
| 1105 | NULL); |
| 1106 | } |
| 1107 | } |
| 1108 | |
Tapani Pälli | 447bb90 | 2013-12-12 15:08:59 +0200 | [diff] [blame] | 1109 | if (var->data.has_initializer) { |
| 1110 | if (existing->data.has_initializer |
Ian Romanick | f37b1ad | 2011-10-31 14:31:07 -0700 | [diff] [blame] | 1111 | && (var->constant_initializer == NULL |
| 1112 | || existing->constant_initializer == NULL)) { |
| 1113 | linker_error(prog, |
| 1114 | "shared global variable `%s' has multiple " |
| 1115 | "non-constant initializers.\n", |
| 1116 | var->name); |
Paul Berry | b95d237 | 2013-07-27 11:08:31 -0700 | [diff] [blame] | 1117 | return; |
Ian Romanick | f37b1ad | 2011-10-31 14:31:07 -0700 | [diff] [blame] | 1118 | } |
| 1119 | |
| 1120 | /* Some instance had an initializer, so keep track of that. In |
| 1121 | * this location, all sorts of initializers (constant or |
| 1122 | * otherwise) will propagate the existence to the variable |
| 1123 | * stored in the symbol table. |
| 1124 | */ |
Tapani Pälli | 447bb90 | 2013-12-12 15:08:59 +0200 | [diff] [blame] | 1125 | existing->data.has_initializer = true; |
Ian Romanick | cc22c5a | 2010-06-18 17:13:42 -0700 | [diff] [blame] | 1126 | } |
Chad Versace | 7528f14 | 2010-11-17 14:34:38 -0800 | [diff] [blame] | 1127 | |
Tapani Pälli | c1d3080 | 2013-12-12 12:57:57 +0200 | [diff] [blame] | 1128 | if (existing->data.invariant != var->data.invariant) { |
Ian Romanick | 586e741 | 2011-07-28 14:04:09 -0700 | [diff] [blame] | 1129 | linker_error(prog, "declarations for %s `%s' have " |
| 1130 | "mismatching invariant qualifiers\n", |
| 1131 | mode_string(var), var->name); |
Paul Berry | b95d237 | 2013-07-27 11:08:31 -0700 | [diff] [blame] | 1132 | return; |
Chad Versace | 7528f14 | 2010-11-17 14:34:38 -0800 | [diff] [blame] | 1133 | } |
Tapani Pälli | c1d3080 | 2013-12-12 12:57:57 +0200 | [diff] [blame] | 1134 | if (existing->data.centroid != var->data.centroid) { |
Ian Romanick | 586e741 | 2011-07-28 14:04:09 -0700 | [diff] [blame] | 1135 | linker_error(prog, "declarations for %s `%s' have " |
| 1136 | "mismatching centroid qualifiers\n", |
| 1137 | mode_string(var), var->name); |
Paul Berry | b95d237 | 2013-07-27 11:08:31 -0700 | [diff] [blame] | 1138 | return; |
Chad Versace | 61428dd | 2011-01-10 15:29:30 -0800 | [diff] [blame] | 1139 | } |
Tapani Pälli | c1d3080 | 2013-12-12 12:57:57 +0200 | [diff] [blame] | 1140 | if (existing->data.sample != var->data.sample) { |
Chris Forbes | 51c5fc8 | 2013-11-29 21:26:10 +1300 | [diff] [blame] | 1141 | linker_error(prog, "declarations for %s `%s` have " |
| 1142 | "mismatching sample qualifiers\n", |
| 1143 | mode_string(var), var->name); |
| 1144 | return; |
| 1145 | } |
Ian Romanick | cc22c5a | 2010-06-18 17:13:42 -0700 | [diff] [blame] | 1146 | } else |
Eric Anholt | 001eee5 | 2010-11-05 06:11:24 -0700 | [diff] [blame] | 1147 | variables.add_variable(var); |
Ian Romanick | cc22c5a | 2010-06-18 17:13:42 -0700 | [diff] [blame] | 1148 | } |
| 1149 | } |
Ian Romanick | cc22c5a | 2010-06-18 17:13:42 -0700 | [diff] [blame] | 1150 | } |
| 1151 | |
| 1152 | |
Ian Romanick | 3710192 | 2010-06-18 19:02:10 -0700 | [diff] [blame] | 1153 | /** |
Ian Romanick | e2e5d0d | 2010-06-29 18:47:11 -0700 | [diff] [blame] | 1154 | * Perform validation of uniforms used across multiple shader stages |
| 1155 | */ |
Paul Berry | b95d237 | 2013-07-27 11:08:31 -0700 | [diff] [blame] | 1156 | void |
Ian Romanick | e2e5d0d | 2010-06-29 18:47:11 -0700 | [diff] [blame] | 1157 | cross_validate_uniforms(struct gl_shader_program *prog) |
| 1158 | { |
Paul Berry | b95d237 | 2013-07-27 11:08:31 -0700 | [diff] [blame] | 1159 | cross_validate_globals(prog, prog->_LinkedShaders, |
Paul Berry | 665b8d7 | 2014-01-07 10:11:39 -0800 | [diff] [blame] | 1160 | MESA_SHADER_STAGES, true); |
Ian Romanick | e2e5d0d | 2010-06-29 18:47:11 -0700 | [diff] [blame] | 1161 | } |
| 1162 | |
Eric Anholt | f609cf7 | 2012-04-27 13:52:56 -0700 | [diff] [blame] | 1163 | /** |
| 1164 | * Accumulates the array of prog->UniformBlocks and checks that all |
| 1165 | * definitons of blocks agree on their contents. |
| 1166 | */ |
| 1167 | static bool |
| 1168 | interstage_cross_validate_uniform_blocks(struct gl_shader_program *prog) |
| 1169 | { |
| 1170 | unsigned max_num_uniform_blocks = 0; |
Paul Berry | 665b8d7 | 2014-01-07 10:11:39 -0800 | [diff] [blame] | 1171 | for (unsigned i = 0; i < MESA_SHADER_STAGES; i++) { |
Eric Anholt | f609cf7 | 2012-04-27 13:52:56 -0700 | [diff] [blame] | 1172 | if (prog->_LinkedShaders[i]) |
| 1173 | max_num_uniform_blocks += prog->_LinkedShaders[i]->NumUniformBlocks; |
| 1174 | } |
| 1175 | |
Paul Berry | 665b8d7 | 2014-01-07 10:11:39 -0800 | [diff] [blame] | 1176 | for (unsigned i = 0; i < MESA_SHADER_STAGES; i++) { |
Eric Anholt | f609cf7 | 2012-04-27 13:52:56 -0700 | [diff] [blame] | 1177 | struct gl_shader *sh = prog->_LinkedShaders[i]; |
| 1178 | |
| 1179 | prog->UniformBlockStageIndex[i] = ralloc_array(prog, int, |
| 1180 | max_num_uniform_blocks); |
| 1181 | for (unsigned int j = 0; j < max_num_uniform_blocks; j++) |
| 1182 | prog->UniformBlockStageIndex[i][j] = -1; |
| 1183 | |
| 1184 | if (sh == NULL) |
| 1185 | continue; |
| 1186 | |
| 1187 | for (unsigned int j = 0; j < sh->NumUniformBlocks; j++) { |
| 1188 | int index = link_cross_validate_uniform_block(prog, |
| 1189 | &prog->UniformBlocks, |
Samuel Iglesias Gonsalvez | 6668eb5 | 2015-09-11 12:29:37 +0200 | [diff] [blame] | 1190 | &prog->NumBufferInterfaceBlocks, |
Eric Anholt | f609cf7 | 2012-04-27 13:52:56 -0700 | [diff] [blame] | 1191 | &sh->UniformBlocks[j]); |
| 1192 | |
| 1193 | if (index == -1) { |
Andres Gomez | f9fc3ae | 2014-11-18 08:43:35 -0700 | [diff] [blame] | 1194 | linker_error(prog, "uniform block `%s' has mismatching definitions\n", |
Eric Anholt | f609cf7 | 2012-04-27 13:52:56 -0700 | [diff] [blame] | 1195 | sh->UniformBlocks[j].Name); |
| 1196 | return false; |
| 1197 | } |
| 1198 | |
| 1199 | prog->UniformBlockStageIndex[i][index] = j; |
| 1200 | } |
| 1201 | } |
| 1202 | |
| 1203 | return true; |
| 1204 | } |
Ian Romanick | e2e5d0d | 2010-06-29 18:47:11 -0700 | [diff] [blame] | 1205 | |
Ian Romanick | 3710192 | 2010-06-18 19:02:10 -0700 | [diff] [blame] | 1206 | |
Ian Romanick | 3fb8787 | 2010-07-09 14:09:34 -0700 | [diff] [blame] | 1207 | /** |
| 1208 | * Populates a shaders symbol table with all global declarations |
| 1209 | */ |
| 1210 | static void |
| 1211 | populate_symbol_table(gl_shader *sh) |
| 1212 | { |
| 1213 | sh->symbols = new(sh) glsl_symbol_table; |
| 1214 | |
Matt Turner | 4d78446 | 2014-06-24 21:34:05 -0700 | [diff] [blame] | 1215 | foreach_in_list(ir_instruction, inst, sh->ir) { |
Ian Romanick | 3fb8787 | 2010-07-09 14:09:34 -0700 | [diff] [blame] | 1216 | ir_variable *var; |
| 1217 | ir_function *func; |
| 1218 | |
| 1219 | if ((func = inst->as_function()) != NULL) { |
Eric Anholt | e8f5ebf | 2010-11-05 06:08:45 -0700 | [diff] [blame] | 1220 | sh->symbols->add_function(func); |
Ian Romanick | 3fb8787 | 2010-07-09 14:09:34 -0700 | [diff] [blame] | 1221 | } else if ((var = inst->as_variable()) != NULL) { |
Ian Romanick | a994824 | 2014-07-08 18:53:09 -0700 | [diff] [blame] | 1222 | if (var->data.mode != ir_var_temporary) |
| 1223 | sh->symbols->add_variable(var); |
Ian Romanick | 3fb8787 | 2010-07-09 14:09:34 -0700 | [diff] [blame] | 1224 | } |
| 1225 | } |
| 1226 | } |
| 1227 | |
| 1228 | |
| 1229 | /** |
Ian Romanick | 31a9786 | 2010-07-12 18:48:50 -0700 | [diff] [blame] | 1230 | * Remap variables referenced in an instruction tree |
| 1231 | * |
| 1232 | * This is used when instruction trees are cloned from one shader and placed in |
| 1233 | * another. These trees will contain references to \c ir_variable nodes that |
| 1234 | * do not exist in the target shader. This function finds these \c ir_variable |
| 1235 | * references and replaces the references with matching variables in the target |
| 1236 | * shader. |
| 1237 | * |
| 1238 | * If there is no matching variable in the target shader, a clone of the |
| 1239 | * \c ir_variable is made and added to the target shader. The new variable is |
| 1240 | * added to \b both the instruction stream and the symbol table. |
| 1241 | * |
| 1242 | * \param inst IR tree that is to be processed. |
| 1243 | * \param symbols Symbol table containing global scope symbols in the |
| 1244 | * linked shader. |
| 1245 | * \param instructions Instruction stream where new variable declarations |
| 1246 | * should be added. |
| 1247 | */ |
| 1248 | void |
Eric Anholt | 8273bd4 | 2010-08-04 12:34:56 -0700 | [diff] [blame] | 1249 | remap_variables(ir_instruction *inst, struct gl_shader *target, |
| 1250 | hash_table *temps) |
Ian Romanick | 31a9786 | 2010-07-12 18:48:50 -0700 | [diff] [blame] | 1251 | { |
| 1252 | class remap_visitor : public ir_hierarchical_visitor { |
| 1253 | public: |
Eric Anholt | 8273bd4 | 2010-08-04 12:34:56 -0700 | [diff] [blame] | 1254 | remap_visitor(struct gl_shader *target, |
Ian Romanick | 7e2aa91 | 2010-07-19 17:12:42 -0700 | [diff] [blame] | 1255 | hash_table *temps) |
Ian Romanick | 31a9786 | 2010-07-12 18:48:50 -0700 | [diff] [blame] | 1256 | { |
Eric Anholt | 8273bd4 | 2010-08-04 12:34:56 -0700 | [diff] [blame] | 1257 | this->target = target; |
| 1258 | this->symbols = target->symbols; |
| 1259 | this->instructions = target->ir; |
Ian Romanick | 7e2aa91 | 2010-07-19 17:12:42 -0700 | [diff] [blame] | 1260 | this->temps = temps; |
Ian Romanick | 31a9786 | 2010-07-12 18:48:50 -0700 | [diff] [blame] | 1261 | } |
| 1262 | |
| 1263 | virtual ir_visitor_status visit(ir_dereference_variable *ir) |
| 1264 | { |
Tapani Pälli | 33ee2c6 | 2013-12-12 13:51:01 +0200 | [diff] [blame] | 1265 | if (ir->var->data.mode == ir_var_temporary) { |
Ian Romanick | 7e2aa91 | 2010-07-19 17:12:42 -0700 | [diff] [blame] | 1266 | ir_variable *var = (ir_variable *) hash_table_find(temps, ir->var); |
| 1267 | |
| 1268 | assert(var != NULL); |
| 1269 | ir->var = var; |
| 1270 | return visit_continue; |
| 1271 | } |
| 1272 | |
Ian Romanick | 31a9786 | 2010-07-12 18:48:50 -0700 | [diff] [blame] | 1273 | ir_variable *const existing = |
| 1274 | this->symbols->get_variable(ir->var->name); |
| 1275 | if (existing != NULL) |
| 1276 | ir->var = existing; |
| 1277 | else { |
Eric Anholt | 8273bd4 | 2010-08-04 12:34:56 -0700 | [diff] [blame] | 1278 | ir_variable *copy = ir->var->clone(this->target, NULL); |
Ian Romanick | 31a9786 | 2010-07-12 18:48:50 -0700 | [diff] [blame] | 1279 | |
Eric Anholt | 001eee5 | 2010-11-05 06:11:24 -0700 | [diff] [blame] | 1280 | this->symbols->add_variable(copy); |
Ian Romanick | 31a9786 | 2010-07-12 18:48:50 -0700 | [diff] [blame] | 1281 | this->instructions->push_head(copy); |
Ian Romanick | 7e2aa91 | 2010-07-19 17:12:42 -0700 | [diff] [blame] | 1282 | ir->var = copy; |
Ian Romanick | 31a9786 | 2010-07-12 18:48:50 -0700 | [diff] [blame] | 1283 | } |
| 1284 | |
| 1285 | return visit_continue; |
| 1286 | } |
| 1287 | |
| 1288 | private: |
Eric Anholt | 8273bd4 | 2010-08-04 12:34:56 -0700 | [diff] [blame] | 1289 | struct gl_shader *target; |
Ian Romanick | 31a9786 | 2010-07-12 18:48:50 -0700 | [diff] [blame] | 1290 | glsl_symbol_table *symbols; |
| 1291 | exec_list *instructions; |
Ian Romanick | 7e2aa91 | 2010-07-19 17:12:42 -0700 | [diff] [blame] | 1292 | hash_table *temps; |
Ian Romanick | 31a9786 | 2010-07-12 18:48:50 -0700 | [diff] [blame] | 1293 | }; |
| 1294 | |
Eric Anholt | 8273bd4 | 2010-08-04 12:34:56 -0700 | [diff] [blame] | 1295 | remap_visitor v(target, temps); |
Ian Romanick | 31a9786 | 2010-07-12 18:48:50 -0700 | [diff] [blame] | 1296 | |
| 1297 | inst->accept(&v); |
| 1298 | } |
| 1299 | |
| 1300 | |
| 1301 | /** |
| 1302 | * Move non-declarations from one instruction stream to another |
| 1303 | * |
| 1304 | * The intended usage pattern of this function is to pass the pointer to the |
Eric Anholt | 62c4763 | 2010-07-29 13:52:25 -0700 | [diff] [blame] | 1305 | * head sentinel of a list (i.e., a pointer to the list cast to an \c exec_node |
Ian Romanick | 31a9786 | 2010-07-12 18:48:50 -0700 | [diff] [blame] | 1306 | * pointer) for \c last and \c false for \c make_copies on the first |
| 1307 | * call. Successive calls pass the return value of the previous call for |
| 1308 | * \c last and \c true for \c make_copies. |
| 1309 | * |
| 1310 | * \param instructions Source instruction stream |
| 1311 | * \param last Instruction after which new instructions should be |
| 1312 | * inserted in the target instruction stream |
| 1313 | * \param make_copies Flag selecting whether instructions in \c instructions |
| 1314 | * should be copied (via \c ir_instruction::clone) into the |
| 1315 | * target list or moved. |
| 1316 | * |
| 1317 | * \return |
| 1318 | * The new "last" instruction in the target instruction stream. This pointer |
| 1319 | * is suitable for use as the \c last parameter of a later call to this |
| 1320 | * function. |
| 1321 | */ |
| 1322 | exec_node * |
| 1323 | move_non_declarations(exec_list *instructions, exec_node *last, |
| 1324 | bool make_copies, gl_shader *target) |
| 1325 | { |
Ian Romanick | 7e2aa91 | 2010-07-19 17:12:42 -0700 | [diff] [blame] | 1326 | hash_table *temps = NULL; |
| 1327 | |
| 1328 | if (make_copies) |
| 1329 | temps = hash_table_ctor(0, hash_table_pointer_hash, |
| 1330 | hash_table_pointer_compare); |
| 1331 | |
Matt Turner | c6a16f6 | 2014-06-24 21:58:35 -0700 | [diff] [blame] | 1332 | foreach_in_list_safe(ir_instruction, inst, instructions) { |
Ian Romanick | 7e2aa91 | 2010-07-19 17:12:42 -0700 | [diff] [blame] | 1333 | if (inst->as_function()) |
Ian Romanick | 31a9786 | 2010-07-12 18:48:50 -0700 | [diff] [blame] | 1334 | continue; |
| 1335 | |
Ian Romanick | 7e2aa91 | 2010-07-19 17:12:42 -0700 | [diff] [blame] | 1336 | ir_variable *var = inst->as_variable(); |
Tapani Pälli | 33ee2c6 | 2013-12-12 13:51:01 +0200 | [diff] [blame] | 1337 | if ((var != NULL) && (var->data.mode != ir_var_temporary)) |
Ian Romanick | 7e2aa91 | 2010-07-19 17:12:42 -0700 | [diff] [blame] | 1338 | continue; |
| 1339 | |
| 1340 | assert(inst->as_assignment() |
Kenneth Graunke | d884f60 | 2012-03-20 15:56:37 -0700 | [diff] [blame] | 1341 | || inst->as_call() |
Kenneth Graunke | b45a68e | 2012-10-24 13:17:24 -0700 | [diff] [blame] | 1342 | || inst->as_if() /* for initializers with the ?: operator */ |
Tapani Pälli | 33ee2c6 | 2013-12-12 13:51:01 +0200 | [diff] [blame] | 1343 | || ((var != NULL) && (var->data.mode == ir_var_temporary))); |
Ian Romanick | 31a9786 | 2010-07-12 18:48:50 -0700 | [diff] [blame] | 1344 | |
| 1345 | if (make_copies) { |
Eric Anholt | 8273bd4 | 2010-08-04 12:34:56 -0700 | [diff] [blame] | 1346 | inst = inst->clone(target, NULL); |
Ian Romanick | 7e2aa91 | 2010-07-19 17:12:42 -0700 | [diff] [blame] | 1347 | |
| 1348 | if (var != NULL) |
| 1349 | hash_table_insert(temps, inst, var); |
| 1350 | else |
Eric Anholt | 8273bd4 | 2010-08-04 12:34:56 -0700 | [diff] [blame] | 1351 | remap_variables(inst, target, temps); |
Ian Romanick | 31a9786 | 2010-07-12 18:48:50 -0700 | [diff] [blame] | 1352 | } else { |
| 1353 | inst->remove(); |
| 1354 | } |
| 1355 | |
| 1356 | last->insert_after(inst); |
| 1357 | last = inst; |
| 1358 | } |
| 1359 | |
Ian Romanick | 7e2aa91 | 2010-07-19 17:12:42 -0700 | [diff] [blame] | 1360 | if (make_copies) |
| 1361 | hash_table_dtor(temps); |
| 1362 | |
Ian Romanick | 31a9786 | 2010-07-12 18:48:50 -0700 | [diff] [blame] | 1363 | return last; |
| 1364 | } |
| 1365 | |
Ian Romanick | 15ce87e | 2010-07-09 15:28:22 -0700 | [diff] [blame] | 1366 | |
| 1367 | /** |
Brian Paul | 84a1273 | 2012-02-02 20:10:40 -0700 | [diff] [blame] | 1368 | * This class is only used in link_intrastage_shaders() below but declaring |
| 1369 | * it inside that function leads to compiler warnings with some versions of |
| 1370 | * gcc. |
| 1371 | */ |
| 1372 | class array_sizing_visitor : public ir_hierarchical_visitor { |
| 1373 | public: |
Paul Berry | 15e05b9 | 2013-09-25 14:07:37 -0700 | [diff] [blame] | 1374 | array_sizing_visitor() |
| 1375 | : mem_ctx(ralloc_context(NULL)), |
| 1376 | unnamed_interfaces(hash_table_ctor(0, hash_table_pointer_hash, |
| 1377 | hash_table_pointer_compare)) |
| 1378 | { |
| 1379 | } |
| 1380 | |
| 1381 | ~array_sizing_visitor() |
| 1382 | { |
| 1383 | hash_table_dtor(this->unnamed_interfaces); |
| 1384 | ralloc_free(this->mem_ctx); |
| 1385 | } |
| 1386 | |
Brian Paul | 84a1273 | 2012-02-02 20:10:40 -0700 | [diff] [blame] | 1387 | virtual ir_visitor_status visit(ir_variable *var) |
| 1388 | { |
Samuel Iglesias Gonsalvez | f3f64cd | 2015-03-18 15:32:03 +0100 | [diff] [blame] | 1389 | fixup_type(&var->type, var->data.max_array_access, |
| 1390 | var->data.from_ssbo_unsized_array); |
Paul Berry | e226669 | 2013-09-23 10:44:19 -0700 | [diff] [blame] | 1391 | if (var->type->is_interface()) { |
| 1392 | if (interface_contains_unsized_arrays(var->type)) { |
| 1393 | const glsl_type *new_type = |
Ian Romanick | 21df016 | 2014-05-23 18:57:36 -0700 | [diff] [blame] | 1394 | resize_interface_members(var->type, |
Samuel Iglesias Gonsalvez | f3f64cd | 2015-03-18 15:32:03 +0100 | [diff] [blame] | 1395 | var->get_max_ifc_array_access(), |
| 1396 | var->is_in_shader_storage_block()); |
Paul Berry | e226669 | 2013-09-23 10:44:19 -0700 | [diff] [blame] | 1397 | var->type = new_type; |
| 1398 | var->change_interface_type(new_type); |
| 1399 | } |
| 1400 | } else if (var->type->is_array() && |
| 1401 | var->type->fields.array->is_interface()) { |
| 1402 | if (interface_contains_unsized_arrays(var->type->fields.array)) { |
| 1403 | const glsl_type *new_type = |
| 1404 | resize_interface_members(var->type->fields.array, |
Samuel Iglesias Gonsalvez | f3f64cd | 2015-03-18 15:32:03 +0100 | [diff] [blame] | 1405 | var->get_max_ifc_array_access(), |
| 1406 | var->is_in_shader_storage_block()); |
Paul Berry | e226669 | 2013-09-23 10:44:19 -0700 | [diff] [blame] | 1407 | var->change_interface_type(new_type); |
Timothy Arceri | 939dc28 | 2015-03-14 12:40:20 +1100 | [diff] [blame] | 1408 | var->type = update_interface_members_array(var->type, new_type); |
Paul Berry | e226669 | 2013-09-23 10:44:19 -0700 | [diff] [blame] | 1409 | } |
Paul Berry | 15e05b9 | 2013-09-25 14:07:37 -0700 | [diff] [blame] | 1410 | } else if (const glsl_type *ifc_type = var->get_interface_type()) { |
| 1411 | /* Store a pointer to the variable in the unnamed_interfaces |
| 1412 | * hashtable. |
| 1413 | */ |
| 1414 | ir_variable **interface_vars = (ir_variable **) |
| 1415 | hash_table_find(this->unnamed_interfaces, ifc_type); |
| 1416 | if (interface_vars == NULL) { |
| 1417 | interface_vars = rzalloc_array(mem_ctx, ir_variable *, |
| 1418 | ifc_type->length); |
| 1419 | hash_table_insert(this->unnamed_interfaces, interface_vars, |
| 1420 | ifc_type); |
| 1421 | } |
| 1422 | unsigned index = ifc_type->field_index(var->name); |
| 1423 | assert(index < ifc_type->length); |
| 1424 | assert(interface_vars[index] == NULL); |
| 1425 | interface_vars[index] = var; |
Brian Paul | 84a1273 | 2012-02-02 20:10:40 -0700 | [diff] [blame] | 1426 | } |
| 1427 | return visit_continue; |
| 1428 | } |
Paul Berry | e226669 | 2013-09-23 10:44:19 -0700 | [diff] [blame] | 1429 | |
Paul Berry | 15e05b9 | 2013-09-25 14:07:37 -0700 | [diff] [blame] | 1430 | /** |
| 1431 | * For each unnamed interface block that was discovered while running the |
| 1432 | * visitor, adjust the interface type to reflect the newly assigned array |
| 1433 | * sizes, and fix up the ir_variable nodes to point to the new interface |
| 1434 | * type. |
| 1435 | */ |
| 1436 | void fixup_unnamed_interface_types() |
| 1437 | { |
| 1438 | hash_table_call_foreach(this->unnamed_interfaces, |
| 1439 | fixup_unnamed_interface_type, NULL); |
| 1440 | } |
| 1441 | |
Paul Berry | e226669 | 2013-09-23 10:44:19 -0700 | [diff] [blame] | 1442 | private: |
| 1443 | /** |
| 1444 | * If the type pointed to by \c type represents an unsized array, replace |
| 1445 | * it with a sized array whose size is determined by max_array_access. |
| 1446 | */ |
Samuel Iglesias Gonsalvez | f3f64cd | 2015-03-18 15:32:03 +0100 | [diff] [blame] | 1447 | static void fixup_type(const glsl_type **type, unsigned max_array_access, |
| 1448 | bool from_ssbo_unsized_array) |
Paul Berry | e226669 | 2013-09-23 10:44:19 -0700 | [diff] [blame] | 1449 | { |
Samuel Iglesias Gonsalvez | f3f64cd | 2015-03-18 15:32:03 +0100 | [diff] [blame] | 1450 | if (!from_ssbo_unsized_array && (*type)->is_unsized_array()) { |
Paul Berry | e226669 | 2013-09-23 10:44:19 -0700 | [diff] [blame] | 1451 | *type = glsl_type::get_array_instance((*type)->fields.array, |
| 1452 | max_array_access + 1); |
| 1453 | assert(*type != NULL); |
| 1454 | } |
| 1455 | } |
| 1456 | |
Timothy Arceri | 939dc28 | 2015-03-14 12:40:20 +1100 | [diff] [blame] | 1457 | static const glsl_type * |
| 1458 | update_interface_members_array(const glsl_type *type, |
| 1459 | const glsl_type *new_interface_type) |
| 1460 | { |
| 1461 | const glsl_type *element_type = type->fields.array; |
| 1462 | if (element_type->is_array()) { |
| 1463 | const glsl_type *new_array_type = |
| 1464 | update_interface_members_array(element_type, new_interface_type); |
| 1465 | return glsl_type::get_array_instance(new_array_type, type->length); |
| 1466 | } else { |
| 1467 | return glsl_type::get_array_instance(new_interface_type, |
| 1468 | type->length); |
| 1469 | } |
| 1470 | } |
| 1471 | |
Paul Berry | e226669 | 2013-09-23 10:44:19 -0700 | [diff] [blame] | 1472 | /** |
| 1473 | * Determine whether the given interface type contains unsized arrays (if |
| 1474 | * it doesn't, array_sizing_visitor doesn't need to process it). |
| 1475 | */ |
| 1476 | static bool interface_contains_unsized_arrays(const glsl_type *type) |
| 1477 | { |
| 1478 | for (unsigned i = 0; i < type->length; i++) { |
| 1479 | const glsl_type *elem_type = type->fields.structure[i].type; |
Timothy Arceri | b59c592 | 2013-10-23 21:31:27 +1100 | [diff] [blame] | 1480 | if (elem_type->is_unsized_array()) |
Paul Berry | e226669 | 2013-09-23 10:44:19 -0700 | [diff] [blame] | 1481 | return true; |
| 1482 | } |
| 1483 | return false; |
| 1484 | } |
| 1485 | |
| 1486 | /** |
| 1487 | * Create a new interface type based on the given type, with unsized arrays |
| 1488 | * replaced by sized arrays whose size is determined by |
| 1489 | * max_ifc_array_access. |
| 1490 | */ |
| 1491 | static const glsl_type * |
| 1492 | resize_interface_members(const glsl_type *type, |
Samuel Iglesias Gonsalvez | f3f64cd | 2015-03-18 15:32:03 +0100 | [diff] [blame] | 1493 | const unsigned *max_ifc_array_access, |
| 1494 | bool is_ssbo) |
Paul Berry | e226669 | 2013-09-23 10:44:19 -0700 | [diff] [blame] | 1495 | { |
| 1496 | unsigned num_fields = type->length; |
| 1497 | glsl_struct_field *fields = new glsl_struct_field[num_fields]; |
| 1498 | memcpy(fields, type->fields.structure, |
| 1499 | num_fields * sizeof(*fields)); |
| 1500 | for (unsigned i = 0; i < num_fields; i++) { |
Samuel Iglesias Gonsalvez | f3f64cd | 2015-03-18 15:32:03 +0100 | [diff] [blame] | 1501 | /* If SSBO last member is unsized array, we don't replace it by a sized |
| 1502 | * array. |
| 1503 | */ |
| 1504 | if (is_ssbo && i == (num_fields - 1)) |
| 1505 | fixup_type(&fields[i].type, max_ifc_array_access[i], |
| 1506 | true); |
| 1507 | else |
| 1508 | fixup_type(&fields[i].type, max_ifc_array_access[i], |
| 1509 | false); |
Paul Berry | e226669 | 2013-09-23 10:44:19 -0700 | [diff] [blame] | 1510 | } |
| 1511 | glsl_interface_packing packing = |
| 1512 | (glsl_interface_packing) type->interface_packing; |
| 1513 | const glsl_type *new_ifc_type = |
| 1514 | glsl_type::get_interface_instance(fields, num_fields, |
| 1515 | packing, type->name); |
| 1516 | delete [] fields; |
| 1517 | return new_ifc_type; |
| 1518 | } |
Paul Berry | 15e05b9 | 2013-09-25 14:07:37 -0700 | [diff] [blame] | 1519 | |
| 1520 | static void fixup_unnamed_interface_type(const void *key, void *data, |
| 1521 | void *) |
| 1522 | { |
| 1523 | const glsl_type *ifc_type = (const glsl_type *) key; |
| 1524 | ir_variable **interface_vars = (ir_variable **) data; |
| 1525 | unsigned num_fields = ifc_type->length; |
| 1526 | glsl_struct_field *fields = new glsl_struct_field[num_fields]; |
| 1527 | memcpy(fields, ifc_type->fields.structure, |
| 1528 | num_fields * sizeof(*fields)); |
| 1529 | bool interface_type_changed = false; |
| 1530 | for (unsigned i = 0; i < num_fields; i++) { |
| 1531 | if (interface_vars[i] != NULL && |
| 1532 | fields[i].type != interface_vars[i]->type) { |
| 1533 | fields[i].type = interface_vars[i]->type; |
| 1534 | interface_type_changed = true; |
| 1535 | } |
| 1536 | } |
| 1537 | if (!interface_type_changed) { |
| 1538 | delete [] fields; |
| 1539 | return; |
| 1540 | } |
| 1541 | glsl_interface_packing packing = |
| 1542 | (glsl_interface_packing) ifc_type->interface_packing; |
| 1543 | const glsl_type *new_ifc_type = |
| 1544 | glsl_type::get_interface_instance(fields, num_fields, packing, |
| 1545 | ifc_type->name); |
| 1546 | delete [] fields; |
| 1547 | for (unsigned i = 0; i < num_fields; i++) { |
| 1548 | if (interface_vars[i] != NULL) |
| 1549 | interface_vars[i]->change_interface_type(new_ifc_type); |
| 1550 | } |
| 1551 | } |
| 1552 | |
| 1553 | /** |
| 1554 | * Memory context used to allocate the data in \c unnamed_interfaces. |
| 1555 | */ |
| 1556 | void *mem_ctx; |
| 1557 | |
| 1558 | /** |
| 1559 | * Hash table from const glsl_type * to an array of ir_variable *'s |
| 1560 | * pointing to the ir_variables constituting each unnamed interface block. |
| 1561 | */ |
| 1562 | hash_table *unnamed_interfaces; |
Brian Paul | 84a1273 | 2012-02-02 20:10:40 -0700 | [diff] [blame] | 1563 | }; |
| 1564 | |
Chris Forbes | 7c758c5 | 2014-09-21 13:33:14 +1200 | [diff] [blame] | 1565 | |
| 1566 | /** |
| 1567 | * Performs the cross-validation of tessellation control shader vertices and |
| 1568 | * layout qualifiers for the attached tessellation control shaders, |
| 1569 | * and propagates them to the linked TCS and linked shader program. |
| 1570 | */ |
| 1571 | static void |
| 1572 | link_tcs_out_layout_qualifiers(struct gl_shader_program *prog, |
| 1573 | struct gl_shader *linked_shader, |
| 1574 | struct gl_shader **shader_list, |
| 1575 | unsigned num_shaders) |
| 1576 | { |
| 1577 | linked_shader->TessCtrl.VerticesOut = 0; |
| 1578 | |
| 1579 | if (linked_shader->Stage != MESA_SHADER_TESS_CTRL) |
| 1580 | return; |
| 1581 | |
| 1582 | /* From the GLSL 4.0 spec (chapter 4.3.8.2): |
| 1583 | * |
| 1584 | * "All tessellation control shader layout declarations in a program |
| 1585 | * must specify the same output patch vertex count. There must be at |
| 1586 | * least one layout qualifier specifying an output patch vertex count |
| 1587 | * in any program containing tessellation control shaders; however, |
| 1588 | * such a declaration is not required in all tessellation control |
| 1589 | * shaders." |
| 1590 | */ |
| 1591 | |
| 1592 | for (unsigned i = 0; i < num_shaders; i++) { |
| 1593 | struct gl_shader *shader = shader_list[i]; |
| 1594 | |
| 1595 | if (shader->TessCtrl.VerticesOut != 0) { |
| 1596 | if (linked_shader->TessCtrl.VerticesOut != 0 && |
| 1597 | linked_shader->TessCtrl.VerticesOut != shader->TessCtrl.VerticesOut) { |
| 1598 | linker_error(prog, "tessellation control shader defined with " |
| 1599 | "conflicting output vertex count (%d and %d)\n", |
| 1600 | linked_shader->TessCtrl.VerticesOut, |
| 1601 | shader->TessCtrl.VerticesOut); |
| 1602 | return; |
| 1603 | } |
| 1604 | linked_shader->TessCtrl.VerticesOut = shader->TessCtrl.VerticesOut; |
| 1605 | } |
| 1606 | } |
| 1607 | |
| 1608 | /* Just do the intrastage -> interstage propagation right now, |
| 1609 | * since we already know we're in the right type of shader program |
| 1610 | * for doing it. |
| 1611 | */ |
| 1612 | if (linked_shader->TessCtrl.VerticesOut == 0) { |
| 1613 | linker_error(prog, "tessellation control shader didn't declare " |
| 1614 | "vertices out layout qualifier\n"); |
| 1615 | return; |
| 1616 | } |
| 1617 | prog->TessCtrl.VerticesOut = linked_shader->TessCtrl.VerticesOut; |
| 1618 | } |
| 1619 | |
| 1620 | |
| 1621 | /** |
| 1622 | * Performs the cross-validation of tessellation evaluation shader |
| 1623 | * primitive type, vertex spacing, ordering and point_mode layout qualifiers |
| 1624 | * for the attached tessellation evaluation shaders, and propagates them |
| 1625 | * to the linked TES and linked shader program. |
| 1626 | */ |
| 1627 | static void |
| 1628 | link_tes_in_layout_qualifiers(struct gl_shader_program *prog, |
| 1629 | struct gl_shader *linked_shader, |
| 1630 | struct gl_shader **shader_list, |
| 1631 | unsigned num_shaders) |
| 1632 | { |
| 1633 | linked_shader->TessEval.PrimitiveMode = PRIM_UNKNOWN; |
| 1634 | linked_shader->TessEval.Spacing = 0; |
| 1635 | linked_shader->TessEval.VertexOrder = 0; |
| 1636 | linked_shader->TessEval.PointMode = -1; |
| 1637 | |
| 1638 | if (linked_shader->Stage != MESA_SHADER_TESS_EVAL) |
| 1639 | return; |
| 1640 | |
| 1641 | /* From the GLSL 4.0 spec (chapter 4.3.8.1): |
| 1642 | * |
| 1643 | * "At least one tessellation evaluation shader (compilation unit) in |
| 1644 | * a program must declare a primitive mode in its input layout. |
| 1645 | * Declaration vertex spacing, ordering, and point mode identifiers is |
| 1646 | * optional. It is not required that all tessellation evaluation |
| 1647 | * shaders in a program declare a primitive mode. If spacing or |
| 1648 | * vertex ordering declarations are omitted, the tessellation |
| 1649 | * primitive generator will use equal spacing or counter-clockwise |
| 1650 | * vertex ordering, respectively. If a point mode declaration is |
| 1651 | * omitted, the tessellation primitive generator will produce lines or |
| 1652 | * triangles according to the primitive mode." |
| 1653 | */ |
| 1654 | |
| 1655 | for (unsigned i = 0; i < num_shaders; i++) { |
| 1656 | struct gl_shader *shader = shader_list[i]; |
| 1657 | |
| 1658 | if (shader->TessEval.PrimitiveMode != PRIM_UNKNOWN) { |
| 1659 | if (linked_shader->TessEval.PrimitiveMode != PRIM_UNKNOWN && |
| 1660 | linked_shader->TessEval.PrimitiveMode != shader->TessEval.PrimitiveMode) { |
| 1661 | linker_error(prog, "tessellation evaluation shader defined with " |
| 1662 | "conflicting input primitive modes.\n"); |
| 1663 | return; |
| 1664 | } |
| 1665 | linked_shader->TessEval.PrimitiveMode = shader->TessEval.PrimitiveMode; |
| 1666 | } |
| 1667 | |
| 1668 | if (shader->TessEval.Spacing != 0) { |
| 1669 | if (linked_shader->TessEval.Spacing != 0 && |
| 1670 | linked_shader->TessEval.Spacing != shader->TessEval.Spacing) { |
| 1671 | linker_error(prog, "tessellation evaluation shader defined with " |
| 1672 | "conflicting vertex spacing.\n"); |
| 1673 | return; |
| 1674 | } |
| 1675 | linked_shader->TessEval.Spacing = shader->TessEval.Spacing; |
| 1676 | } |
| 1677 | |
| 1678 | if (shader->TessEval.VertexOrder != 0) { |
| 1679 | if (linked_shader->TessEval.VertexOrder != 0 && |
| 1680 | linked_shader->TessEval.VertexOrder != shader->TessEval.VertexOrder) { |
| 1681 | linker_error(prog, "tessellation evaluation shader defined with " |
| 1682 | "conflicting ordering.\n"); |
| 1683 | return; |
| 1684 | } |
| 1685 | linked_shader->TessEval.VertexOrder = shader->TessEval.VertexOrder; |
| 1686 | } |
| 1687 | |
| 1688 | if (shader->TessEval.PointMode != -1) { |
| 1689 | if (linked_shader->TessEval.PointMode != -1 && |
| 1690 | linked_shader->TessEval.PointMode != shader->TessEval.PointMode) { |
| 1691 | linker_error(prog, "tessellation evaluation shader defined with " |
| 1692 | "conflicting point modes.\n"); |
| 1693 | return; |
| 1694 | } |
| 1695 | linked_shader->TessEval.PointMode = shader->TessEval.PointMode; |
| 1696 | } |
| 1697 | |
| 1698 | } |
| 1699 | |
| 1700 | /* Just do the intrastage -> interstage propagation right now, |
| 1701 | * since we already know we're in the right type of shader program |
| 1702 | * for doing it. |
| 1703 | */ |
| 1704 | if (linked_shader->TessEval.PrimitiveMode == PRIM_UNKNOWN) { |
| 1705 | linker_error(prog, |
| 1706 | "tessellation evaluation shader didn't declare input " |
| 1707 | "primitive modes.\n"); |
| 1708 | return; |
| 1709 | } |
| 1710 | prog->TessEval.PrimitiveMode = linked_shader->TessEval.PrimitiveMode; |
| 1711 | |
| 1712 | if (linked_shader->TessEval.Spacing == 0) |
| 1713 | linked_shader->TessEval.Spacing = GL_EQUAL; |
| 1714 | prog->TessEval.Spacing = linked_shader->TessEval.Spacing; |
| 1715 | |
| 1716 | if (linked_shader->TessEval.VertexOrder == 0) |
| 1717 | linked_shader->TessEval.VertexOrder = GL_CCW; |
| 1718 | prog->TessEval.VertexOrder = linked_shader->TessEval.VertexOrder; |
| 1719 | |
| 1720 | if (linked_shader->TessEval.PointMode == -1) |
| 1721 | linked_shader->TessEval.PointMode = GL_FALSE; |
| 1722 | prog->TessEval.PointMode = linked_shader->TessEval.PointMode; |
| 1723 | } |
| 1724 | |
| 1725 | |
Brian Paul | 84a1273 | 2012-02-02 20:10:40 -0700 | [diff] [blame] | 1726 | /** |
Anuj Phogat | 35f11e8 | 2014-02-05 15:01:58 -0800 | [diff] [blame] | 1727 | * Performs the cross-validation of layout qualifiers specified in |
| 1728 | * redeclaration of gl_FragCoord for the attached fragment shaders, |
| 1729 | * and propagates them to the linked FS and linked shader program. |
| 1730 | */ |
| 1731 | static void |
| 1732 | link_fs_input_layout_qualifiers(struct gl_shader_program *prog, |
| 1733 | struct gl_shader *linked_shader, |
| 1734 | struct gl_shader **shader_list, |
| 1735 | unsigned num_shaders) |
| 1736 | { |
| 1737 | linked_shader->redeclares_gl_fragcoord = false; |
| 1738 | linked_shader->uses_gl_fragcoord = false; |
| 1739 | linked_shader->origin_upper_left = false; |
| 1740 | linked_shader->pixel_center_integer = false; |
| 1741 | |
Anuj Phogat | 9bcb0a8 | 2014-02-10 14:12:40 -0800 | [diff] [blame] | 1742 | if (linked_shader->Stage != MESA_SHADER_FRAGMENT || |
| 1743 | (prog->Version < 150 && !prog->ARB_fragment_coord_conventions_enable)) |
Anuj Phogat | 35f11e8 | 2014-02-05 15:01:58 -0800 | [diff] [blame] | 1744 | return; |
| 1745 | |
| 1746 | for (unsigned i = 0; i < num_shaders; i++) { |
| 1747 | struct gl_shader *shader = shader_list[i]; |
| 1748 | /* From the GLSL 1.50 spec, page 39: |
| 1749 | * |
| 1750 | * "If gl_FragCoord is redeclared in any fragment shader in a program, |
| 1751 | * it must be redeclared in all the fragment shaders in that program |
| 1752 | * that have a static use gl_FragCoord." |
Anuj Phogat | 35f11e8 | 2014-02-05 15:01:58 -0800 | [diff] [blame] | 1753 | */ |
| 1754 | if ((linked_shader->redeclares_gl_fragcoord |
| 1755 | && !shader->redeclares_gl_fragcoord |
Anuj Phogat | d820831 | 2015-03-05 11:07:52 -0800 | [diff] [blame] | 1756 | && shader->uses_gl_fragcoord) |
Anuj Phogat | 35f11e8 | 2014-02-05 15:01:58 -0800 | [diff] [blame] | 1757 | || (shader->redeclares_gl_fragcoord |
| 1758 | && !linked_shader->redeclares_gl_fragcoord |
Anuj Phogat | d820831 | 2015-03-05 11:07:52 -0800 | [diff] [blame] | 1759 | && linked_shader->uses_gl_fragcoord)) { |
Anuj Phogat | 35f11e8 | 2014-02-05 15:01:58 -0800 | [diff] [blame] | 1760 | linker_error(prog, "fragment shader defined with conflicting " |
| 1761 | "layout qualifiers for gl_FragCoord\n"); |
| 1762 | } |
| 1763 | |
| 1764 | /* From the GLSL 1.50 spec, page 39: |
| 1765 | * |
| 1766 | * "All redeclarations of gl_FragCoord in all fragment shaders in a |
| 1767 | * single program must have the same set of qualifiers." |
| 1768 | */ |
| 1769 | if (linked_shader->redeclares_gl_fragcoord && shader->redeclares_gl_fragcoord |
| 1770 | && (shader->origin_upper_left != linked_shader->origin_upper_left |
| 1771 | || shader->pixel_center_integer != linked_shader->pixel_center_integer)) { |
| 1772 | linker_error(prog, "fragment shader defined with conflicting " |
| 1773 | "layout qualifiers for gl_FragCoord\n"); |
| 1774 | } |
| 1775 | |
Martin Peres | 87a4bc5 | 2015-05-21 15:51:09 +0300 | [diff] [blame] | 1776 | /* Update the linked shader state. Note that uses_gl_fragcoord should |
| 1777 | * accumulate the results. The other values should replace. If there |
Anuj Phogat | 35f11e8 | 2014-02-05 15:01:58 -0800 | [diff] [blame] | 1778 | * are multiple redeclarations, all the fields except uses_gl_fragcoord |
| 1779 | * are already known to be the same. |
| 1780 | */ |
| 1781 | if (shader->redeclares_gl_fragcoord || shader->uses_gl_fragcoord) { |
| 1782 | linked_shader->redeclares_gl_fragcoord = |
| 1783 | shader->redeclares_gl_fragcoord; |
| 1784 | linked_shader->uses_gl_fragcoord = linked_shader->uses_gl_fragcoord |
| 1785 | || shader->uses_gl_fragcoord; |
| 1786 | linked_shader->origin_upper_left = shader->origin_upper_left; |
| 1787 | linked_shader->pixel_center_integer = shader->pixel_center_integer; |
| 1788 | } |
Francisco Jerez | ce0e151 | 2015-01-28 17:42:37 +0200 | [diff] [blame] | 1789 | |
| 1790 | linked_shader->EarlyFragmentTests |= shader->EarlyFragmentTests; |
Anuj Phogat | 35f11e8 | 2014-02-05 15:01:58 -0800 | [diff] [blame] | 1791 | } |
| 1792 | } |
| 1793 | |
| 1794 | /** |
Eric Anholt | 6065a87 | 2013-06-12 18:12:40 -0700 | [diff] [blame] | 1795 | * Performs the cross-validation of geometry shader max_vertices and |
| 1796 | * primitive type layout qualifiers for the attached geometry shaders, |
| 1797 | * and propagates them to the linked GS and linked shader program. |
| 1798 | */ |
| 1799 | static void |
| 1800 | link_gs_inout_layout_qualifiers(struct gl_shader_program *prog, |
| 1801 | struct gl_shader *linked_shader, |
| 1802 | struct gl_shader **shader_list, |
| 1803 | unsigned num_shaders) |
| 1804 | { |
| 1805 | linked_shader->Geom.VerticesOut = 0; |
Jordan Justen | 3134020 | 2014-01-25 02:17:21 -0800 | [diff] [blame] | 1806 | linked_shader->Geom.Invocations = 0; |
Eric Anholt | 6065a87 | 2013-06-12 18:12:40 -0700 | [diff] [blame] | 1807 | linked_shader->Geom.InputType = PRIM_UNKNOWN; |
| 1808 | linked_shader->Geom.OutputType = PRIM_UNKNOWN; |
| 1809 | |
| 1810 | /* No in/out qualifiers defined for anything but GLSL 1.50+ |
| 1811 | * geometry shaders so far. |
| 1812 | */ |
Paul Berry | e3b86f0 | 2014-01-07 10:58:56 -0800 | [diff] [blame] | 1813 | if (linked_shader->Stage != MESA_SHADER_GEOMETRY || prog->Version < 150) |
Eric Anholt | 6065a87 | 2013-06-12 18:12:40 -0700 | [diff] [blame] | 1814 | return; |
| 1815 | |
| 1816 | /* From the GLSL 1.50 spec, page 46: |
| 1817 | * |
| 1818 | * "All geometry shader output layout declarations in a program |
| 1819 | * must declare the same layout and same value for |
| 1820 | * max_vertices. There must be at least one geometry output |
| 1821 | * layout declaration somewhere in a program, but not all |
| 1822 | * geometry shaders (compilation units) are required to |
| 1823 | * declare it." |
| 1824 | */ |
| 1825 | |
| 1826 | for (unsigned i = 0; i < num_shaders; i++) { |
| 1827 | struct gl_shader *shader = shader_list[i]; |
| 1828 | |
| 1829 | if (shader->Geom.InputType != PRIM_UNKNOWN) { |
| 1830 | if (linked_shader->Geom.InputType != PRIM_UNKNOWN && |
| 1831 | linked_shader->Geom.InputType != shader->Geom.InputType) { |
| 1832 | linker_error(prog, "geometry shader defined with conflicting " |
| 1833 | "input types\n"); |
| 1834 | return; |
| 1835 | } |
| 1836 | linked_shader->Geom.InputType = shader->Geom.InputType; |
| 1837 | } |
| 1838 | |
| 1839 | if (shader->Geom.OutputType != PRIM_UNKNOWN) { |
| 1840 | if (linked_shader->Geom.OutputType != PRIM_UNKNOWN && |
| 1841 | linked_shader->Geom.OutputType != shader->Geom.OutputType) { |
| 1842 | linker_error(prog, "geometry shader defined with conflicting " |
| 1843 | "output types\n"); |
| 1844 | return; |
| 1845 | } |
| 1846 | linked_shader->Geom.OutputType = shader->Geom.OutputType; |
| 1847 | } |
| 1848 | |
| 1849 | if (shader->Geom.VerticesOut != 0) { |
| 1850 | if (linked_shader->Geom.VerticesOut != 0 && |
| 1851 | linked_shader->Geom.VerticesOut != shader->Geom.VerticesOut) { |
| 1852 | linker_error(prog, "geometry shader defined with conflicting " |
| 1853 | "output vertex count (%d and %d)\n", |
| 1854 | linked_shader->Geom.VerticesOut, |
| 1855 | shader->Geom.VerticesOut); |
| 1856 | return; |
| 1857 | } |
| 1858 | linked_shader->Geom.VerticesOut = shader->Geom.VerticesOut; |
| 1859 | } |
Jordan Justen | 3134020 | 2014-01-25 02:17:21 -0800 | [diff] [blame] | 1860 | |
| 1861 | if (shader->Geom.Invocations != 0) { |
| 1862 | if (linked_shader->Geom.Invocations != 0 && |
| 1863 | linked_shader->Geom.Invocations != shader->Geom.Invocations) { |
| 1864 | linker_error(prog, "geometry shader defined with conflicting " |
| 1865 | "invocation count (%d and %d)\n", |
| 1866 | linked_shader->Geom.Invocations, |
| 1867 | shader->Geom.Invocations); |
| 1868 | return; |
| 1869 | } |
| 1870 | linked_shader->Geom.Invocations = shader->Geom.Invocations; |
| 1871 | } |
Eric Anholt | 6065a87 | 2013-06-12 18:12:40 -0700 | [diff] [blame] | 1872 | } |
| 1873 | |
| 1874 | /* Just do the intrastage -> interstage propagation right now, |
| 1875 | * since we already know we're in the right type of shader program |
| 1876 | * for doing it. |
| 1877 | */ |
| 1878 | if (linked_shader->Geom.InputType == PRIM_UNKNOWN) { |
| 1879 | linker_error(prog, |
| 1880 | "geometry shader didn't declare primitive input type\n"); |
| 1881 | return; |
| 1882 | } |
| 1883 | prog->Geom.InputType = linked_shader->Geom.InputType; |
| 1884 | |
| 1885 | if (linked_shader->Geom.OutputType == PRIM_UNKNOWN) { |
| 1886 | linker_error(prog, |
| 1887 | "geometry shader didn't declare primitive output type\n"); |
| 1888 | return; |
| 1889 | } |
| 1890 | prog->Geom.OutputType = linked_shader->Geom.OutputType; |
| 1891 | |
| 1892 | if (linked_shader->Geom.VerticesOut == 0) { |
| 1893 | linker_error(prog, |
| 1894 | "geometry shader didn't declare max_vertices\n"); |
| 1895 | return; |
| 1896 | } |
| 1897 | prog->Geom.VerticesOut = linked_shader->Geom.VerticesOut; |
Jordan Justen | 3134020 | 2014-01-25 02:17:21 -0800 | [diff] [blame] | 1898 | |
| 1899 | if (linked_shader->Geom.Invocations == 0) |
| 1900 | linked_shader->Geom.Invocations = 1; |
| 1901 | |
| 1902 | prog->Geom.Invocations = linked_shader->Geom.Invocations; |
Eric Anholt | 6065a87 | 2013-06-12 18:12:40 -0700 | [diff] [blame] | 1903 | } |
| 1904 | |
Paul Berry | 28ce604 | 2014-01-08 11:59:28 -0800 | [diff] [blame] | 1905 | |
| 1906 | /** |
| 1907 | * Perform cross-validation of compute shader local_size_{x,y,z} layout |
| 1908 | * qualifiers for the attached compute shaders, and propagate them to the |
| 1909 | * linked CS and linked shader program. |
| 1910 | */ |
| 1911 | static void |
| 1912 | link_cs_input_layout_qualifiers(struct gl_shader_program *prog, |
| 1913 | struct gl_shader *linked_shader, |
| 1914 | struct gl_shader **shader_list, |
| 1915 | unsigned num_shaders) |
| 1916 | { |
| 1917 | for (int i = 0; i < 3; i++) |
| 1918 | linked_shader->Comp.LocalSize[i] = 0; |
| 1919 | |
| 1920 | /* This function is called for all shader stages, but it only has an effect |
| 1921 | * for compute shaders. |
| 1922 | */ |
| 1923 | if (linked_shader->Stage != MESA_SHADER_COMPUTE) |
| 1924 | return; |
| 1925 | |
| 1926 | /* From the ARB_compute_shader spec, in the section describing local size |
| 1927 | * declarations: |
| 1928 | * |
| 1929 | * If multiple compute shaders attached to a single program object |
| 1930 | * declare local work-group size, the declarations must be identical; |
| 1931 | * otherwise a link-time error results. Furthermore, if a program |
| 1932 | * object contains any compute shaders, at least one must contain an |
| 1933 | * input layout qualifier specifying the local work sizes of the |
| 1934 | * program, or a link-time error will occur. |
| 1935 | */ |
| 1936 | for (unsigned sh = 0; sh < num_shaders; sh++) { |
| 1937 | struct gl_shader *shader = shader_list[sh]; |
| 1938 | |
| 1939 | if (shader->Comp.LocalSize[0] != 0) { |
| 1940 | if (linked_shader->Comp.LocalSize[0] != 0) { |
| 1941 | for (int i = 0; i < 3; i++) { |
| 1942 | if (linked_shader->Comp.LocalSize[i] != |
| 1943 | shader->Comp.LocalSize[i]) { |
| 1944 | linker_error(prog, "compute shader defined with conflicting " |
| 1945 | "local sizes\n"); |
| 1946 | return; |
| 1947 | } |
| 1948 | } |
| 1949 | } |
| 1950 | for (int i = 0; i < 3; i++) |
| 1951 | linked_shader->Comp.LocalSize[i] = shader->Comp.LocalSize[i]; |
| 1952 | } |
| 1953 | } |
| 1954 | |
| 1955 | /* Just do the intrastage -> interstage propagation right now, |
| 1956 | * since we already know we're in the right type of shader program |
| 1957 | * for doing it. |
| 1958 | */ |
| 1959 | if (linked_shader->Comp.LocalSize[0] == 0) { |
| 1960 | linker_error(prog, "compute shader didn't declare local size\n"); |
| 1961 | return; |
| 1962 | } |
| 1963 | for (int i = 0; i < 3; i++) |
| 1964 | prog->Comp.LocalSize[i] = linked_shader->Comp.LocalSize[i]; |
| 1965 | } |
| 1966 | |
| 1967 | |
Eric Anholt | 6065a87 | 2013-06-12 18:12:40 -0700 | [diff] [blame] | 1968 | /** |
Ian Romanick | 3fb8787 | 2010-07-09 14:09:34 -0700 | [diff] [blame] | 1969 | * Combine a group of shaders for a single stage to generate a linked shader |
| 1970 | * |
| 1971 | * \note |
| 1972 | * If this function is supplied a single shader, it is cloned, and the new |
| 1973 | * shader is returned. |
| 1974 | */ |
| 1975 | static struct gl_shader * |
Kenneth Graunke | 2da02e7 | 2010-11-17 11:03:57 -0800 | [diff] [blame] | 1976 | link_intrastage_shaders(void *mem_ctx, |
| 1977 | struct gl_context *ctx, |
Eric Anholt | 5d0f430 | 2010-08-18 12:02:35 -0700 | [diff] [blame] | 1978 | struct gl_shader_program *prog, |
Ian Romanick | 3fb8787 | 2010-07-09 14:09:34 -0700 | [diff] [blame] | 1979 | struct gl_shader **shader_list, |
| 1980 | unsigned num_shaders) |
| 1981 | { |
Eric Anholt | f609cf7 | 2012-04-27 13:52:56 -0700 | [diff] [blame] | 1982 | struct gl_uniform_block *uniform_blocks = NULL; |
Eric Anholt | f609cf7 | 2012-04-27 13:52:56 -0700 | [diff] [blame] | 1983 | |
Ian Romanick | 13f782c | 2010-06-29 18:53:38 -0700 | [diff] [blame] | 1984 | /* Check that global variables defined in multiple shaders are consistent. |
| 1985 | */ |
Paul Berry | b95d237 | 2013-07-27 11:08:31 -0700 | [diff] [blame] | 1986 | cross_validate_globals(prog, shader_list, num_shaders, false); |
| 1987 | if (!prog->LinkStatus) |
Ian Romanick | 13f782c | 2010-06-29 18:53:38 -0700 | [diff] [blame] | 1988 | return NULL; |
| 1989 | |
Jordan Justen | 4a0bcd9 | 2013-05-20 23:42:49 -0700 | [diff] [blame] | 1990 | /* Check that interface blocks defined in multiple shaders are consistent. |
| 1991 | */ |
Paul Berry | b95d237 | 2013-07-27 11:08:31 -0700 | [diff] [blame] | 1992 | validate_intrastage_interface_blocks(prog, (const gl_shader **)shader_list, |
| 1993 | num_shaders); |
| 1994 | if (!prog->LinkStatus) |
Jordan Justen | 4a0bcd9 | 2013-05-20 23:42:49 -0700 | [diff] [blame] | 1995 | return NULL; |
| 1996 | |
Paul Berry | 4682b9b | 2013-07-27 15:07:08 -0700 | [diff] [blame] | 1997 | /* Link up uniform blocks defined within this stage. */ |
| 1998 | const unsigned num_uniform_blocks = |
Samuel Iglesias Gonsalvez | a7b4ab4 | 2015-04-21 12:12:05 +0200 | [diff] [blame] | 1999 | link_uniform_blocks(mem_ctx, ctx, prog, shader_list, num_shaders, |
Ian Romanick | 514f8c7 | 2013-01-22 01:09:16 -0500 | [diff] [blame] | 2000 | &uniform_blocks); |
Juha-Pekka Heikkila | 088da37 | 2014-04-03 17:06:42 +0300 | [diff] [blame] | 2001 | if (!prog->LinkStatus) |
| 2002 | return NULL; |
Eric Anholt | f609cf7 | 2012-04-27 13:52:56 -0700 | [diff] [blame] | 2003 | |
Ian Romanick | 13f782c | 2010-06-29 18:53:38 -0700 | [diff] [blame] | 2004 | /* Check that there is only a single definition of each function signature |
| 2005 | * across all shaders. |
| 2006 | */ |
| 2007 | for (unsigned i = 0; i < (num_shaders - 1); i++) { |
Matt Turner | 4d78446 | 2014-06-24 21:34:05 -0700 | [diff] [blame] | 2008 | foreach_in_list(ir_instruction, node, shader_list[i]->ir) { |
| 2009 | ir_function *const f = node->as_function(); |
Ian Romanick | 13f782c | 2010-06-29 18:53:38 -0700 | [diff] [blame] | 2010 | |
| 2011 | if (f == NULL) |
| 2012 | continue; |
| 2013 | |
| 2014 | for (unsigned j = i + 1; j < num_shaders; j++) { |
| 2015 | ir_function *const other = |
| 2016 | shader_list[j]->symbols->get_function(f->name); |
| 2017 | |
| 2018 | /* If the other shader has no function (and therefore no function |
| 2019 | * signatures) with the same name, skip to the next shader. |
| 2020 | */ |
| 2021 | if (other == NULL) |
| 2022 | continue; |
| 2023 | |
Matt Turner | 4d78446 | 2014-06-24 21:34:05 -0700 | [diff] [blame] | 2024 | foreach_in_list(ir_function_signature, sig, &f->signatures) { |
Kenneth Graunke | 4b0bac0 | 2013-08-30 16:12:55 -0700 | [diff] [blame] | 2025 | if (!sig->is_defined || sig->is_builtin()) |
Ian Romanick | 13f782c | 2010-06-29 18:53:38 -0700 | [diff] [blame] | 2026 | continue; |
| 2027 | |
| 2028 | ir_function_signature *other_sig = |
Kenneth Graunke | 3e820e3 | 2013-08-30 23:11:55 -0700 | [diff] [blame] | 2029 | other->exact_matching_signature(NULL, &sig->parameters); |
Ian Romanick | 13f782c | 2010-06-29 18:53:38 -0700 | [diff] [blame] | 2030 | |
| 2031 | if ((other_sig != NULL) && other_sig->is_defined |
Kenneth Graunke | 4b0bac0 | 2013-08-30 16:12:55 -0700 | [diff] [blame] | 2032 | && !other_sig->is_builtin()) { |
Andres Gomez | f9fc3ae | 2014-11-18 08:43:35 -0700 | [diff] [blame] | 2033 | linker_error(prog, "function `%s' is multiply defined\n", |
Ian Romanick | 586e741 | 2011-07-28 14:04:09 -0700 | [diff] [blame] | 2034 | f->name); |
Ian Romanick | 13f782c | 2010-06-29 18:53:38 -0700 | [diff] [blame] | 2035 | return NULL; |
| 2036 | } |
| 2037 | } |
| 2038 | } |
| 2039 | } |
| 2040 | } |
| 2041 | |
| 2042 | /* Find the shader that defines main, and make a clone of it. |
| 2043 | * |
| 2044 | * Starting with the clone, search for undefined references. If one is |
| 2045 | * found, find the shader that defines it. Clone the reference and add |
| 2046 | * it to the shader. Repeat until there are no undefined references or |
| 2047 | * until a reference cannot be resolved. |
| 2048 | */ |
Ian Romanick | 15ce87e | 2010-07-09 15:28:22 -0700 | [diff] [blame] | 2049 | gl_shader *main = NULL; |
| 2050 | for (unsigned i = 0; i < num_shaders; i++) { |
Jordan Justen | c4d049f | 2015-08-17 12:22:34 -0700 | [diff] [blame] | 2051 | if (_mesa_get_main_function_signature(shader_list[i]) != NULL) { |
Ian Romanick | 15ce87e | 2010-07-09 15:28:22 -0700 | [diff] [blame] | 2052 | main = shader_list[i]; |
| 2053 | break; |
| 2054 | } |
| 2055 | } |
Ian Romanick | 13f782c | 2010-06-29 18:53:38 -0700 | [diff] [blame] | 2056 | |
Ian Romanick | 15ce87e | 2010-07-09 15:28:22 -0700 | [diff] [blame] | 2057 | if (main == NULL) { |
Ian Romanick | 586e741 | 2011-07-28 14:04:09 -0700 | [diff] [blame] | 2058 | linker_error(prog, "%s shader lacks `main'\n", |
Paul Berry | e3b86f0 | 2014-01-07 10:58:56 -0800 | [diff] [blame] | 2059 | _mesa_shader_stage_to_string(shader_list[0]->Stage)); |
Ian Romanick | 15ce87e | 2010-07-09 15:28:22 -0700 | [diff] [blame] | 2060 | return NULL; |
| 2061 | } |
| 2062 | |
Ian Romanick | 4a45595 | 2010-10-13 15:13:02 -0700 | [diff] [blame] | 2063 | gl_shader *linked = ctx->Driver.NewShader(NULL, 0, main->Type); |
Ian Romanick | 15ce87e | 2010-07-09 15:28:22 -0700 | [diff] [blame] | 2064 | linked->ir = new(linked) exec_list; |
Kenneth Graunke | 2da02e7 | 2010-11-17 11:03:57 -0800 | [diff] [blame] | 2065 | clone_ir_list(mem_ctx, linked->ir, main->ir); |
Ian Romanick | 15ce87e | 2010-07-09 15:28:22 -0700 | [diff] [blame] | 2066 | |
Eric Anholt | f609cf7 | 2012-04-27 13:52:56 -0700 | [diff] [blame] | 2067 | linked->UniformBlocks = uniform_blocks; |
| 2068 | linked->NumUniformBlocks = num_uniform_blocks; |
| 2069 | ralloc_steal(linked, linked->UniformBlocks); |
| 2070 | |
Anuj Phogat | 35f11e8 | 2014-02-05 15:01:58 -0800 | [diff] [blame] | 2071 | link_fs_input_layout_qualifiers(prog, linked, shader_list, num_shaders); |
Chris Forbes | 7c758c5 | 2014-09-21 13:33:14 +1200 | [diff] [blame] | 2072 | link_tcs_out_layout_qualifiers(prog, linked, shader_list, num_shaders); |
| 2073 | link_tes_in_layout_qualifiers(prog, linked, shader_list, num_shaders); |
Eric Anholt | 6065a87 | 2013-06-12 18:12:40 -0700 | [diff] [blame] | 2074 | link_gs_inout_layout_qualifiers(prog, linked, shader_list, num_shaders); |
Paul Berry | 28ce604 | 2014-01-08 11:59:28 -0800 | [diff] [blame] | 2075 | link_cs_input_layout_qualifiers(prog, linked, shader_list, num_shaders); |
Eric Anholt | 6065a87 | 2013-06-12 18:12:40 -0700 | [diff] [blame] | 2076 | |
Ian Romanick | 15ce87e | 2010-07-09 15:28:22 -0700 | [diff] [blame] | 2077 | populate_symbol_table(linked); |
Ian Romanick | 13f782c | 2010-06-29 18:53:38 -0700 | [diff] [blame] | 2078 | |
Andres Gomez | b0e0c26 | 2014-10-24 16:51:09 +0300 | [diff] [blame] | 2079 | /* The pointer to the main function in the final linked shader (i.e., the |
Ian Romanick | 31a9786 | 2010-07-12 18:48:50 -0700 | [diff] [blame] | 2080 | * copy of the original shader that contained the main function). |
| 2081 | */ |
Ian Romanick | 04d3323 | 2014-06-19 12:05:20 -0700 | [diff] [blame] | 2082 | ir_function_signature *const main_sig = |
Jordan Justen | c4d049f | 2015-08-17 12:22:34 -0700 | [diff] [blame] | 2083 | _mesa_get_main_function_signature(linked); |
Ian Romanick | 31a9786 | 2010-07-12 18:48:50 -0700 | [diff] [blame] | 2084 | |
| 2085 | /* Move any instructions other than variable declarations or function |
| 2086 | * declarations into main. |
| 2087 | */ |
Ian Romanick | 9303e35 | 2010-07-19 12:33:54 -0700 | [diff] [blame] | 2088 | exec_node *insertion_point = |
| 2089 | move_non_declarations(linked->ir, (exec_node *) &main_sig->body, false, |
| 2090 | linked); |
| 2091 | |
Ian Romanick | 31a9786 | 2010-07-12 18:48:50 -0700 | [diff] [blame] | 2092 | for (unsigned i = 0; i < num_shaders; i++) { |
Ian Romanick | 9303e35 | 2010-07-19 12:33:54 -0700 | [diff] [blame] | 2093 | if (shader_list[i] == main) |
| 2094 | continue; |
| 2095 | |
Ian Romanick | 31a9786 | 2010-07-12 18:48:50 -0700 | [diff] [blame] | 2096 | insertion_point = move_non_declarations(shader_list[i]->ir, |
Ian Romanick | 9303e35 | 2010-07-19 12:33:54 -0700 | [diff] [blame] | 2097 | insertion_point, true, linked); |
Ian Romanick | 31a9786 | 2010-07-12 18:48:50 -0700 | [diff] [blame] | 2098 | } |
| 2099 | |
Kenneth Graunke | 5b331f6 | 2013-11-23 12:11:34 -0800 | [diff] [blame] | 2100 | /* Check if any shader needs built-in functions. */ |
| 2101 | bool need_builtins = false; |
Ian Romanick | d5be2ac | 2010-07-20 11:29:46 -0700 | [diff] [blame] | 2102 | for (unsigned i = 0; i < num_shaders; i++) { |
Kenneth Graunke | 5b331f6 | 2013-11-23 12:11:34 -0800 | [diff] [blame] | 2103 | if (shader_list[i]->uses_builtin_functions) { |
| 2104 | need_builtins = true; |
| 2105 | break; |
| 2106 | } |
Ian Romanick | d5be2ac | 2010-07-20 11:29:46 -0700 | [diff] [blame] | 2107 | } |
| 2108 | |
Kenneth Graunke | 5b331f6 | 2013-11-23 12:11:34 -0800 | [diff] [blame] | 2109 | bool ok; |
| 2110 | if (need_builtins) { |
| 2111 | /* Make a temporary array one larger than shader_list, which will hold |
| 2112 | * the built-in function shader as well. |
| 2113 | */ |
| 2114 | gl_shader **linking_shaders = (gl_shader **) |
| 2115 | calloc(num_shaders + 1, sizeof(gl_shader *)); |
Ian Romanick | d5be2ac | 2010-07-20 11:29:46 -0700 | [diff] [blame] | 2116 | |
Juha-Pekka Heikkila | d2f0442 | 2014-05-07 16:20:12 +0300 | [diff] [blame] | 2117 | ok = linking_shaders != NULL; |
Kenneth Graunke | 5b331f6 | 2013-11-23 12:11:34 -0800 | [diff] [blame] | 2118 | |
Juha-Pekka Heikkila | d2f0442 | 2014-05-07 16:20:12 +0300 | [diff] [blame] | 2119 | if (ok) { |
| 2120 | memcpy(linking_shaders, shader_list, num_shaders * sizeof(gl_shader *)); |
| 2121 | linking_shaders[num_shaders] = _mesa_glsl_get_builtin_function_shader(); |
| 2122 | |
| 2123 | ok = link_function_calls(prog, linked, linking_shaders, num_shaders + 1); |
| 2124 | |
| 2125 | free(linking_shaders); |
| 2126 | } else { |
| 2127 | _mesa_error_no_memory(__func__); |
| 2128 | } |
Kenneth Graunke | 5b331f6 | 2013-11-23 12:11:34 -0800 | [diff] [blame] | 2129 | } else { |
| 2130 | ok = link_function_calls(prog, linked, shader_list, num_shaders); |
| 2131 | } |
| 2132 | |
| 2133 | |
| 2134 | if (!ok) { |
Marek Olšák | 95e0303 | 2015-09-27 21:28:22 +0200 | [diff] [blame] | 2135 | _mesa_delete_shader(ctx, linked); |
Kenneth Graunke | 7d2423a | 2013-08-02 00:35:05 -0700 | [diff] [blame] | 2136 | return NULL; |
Ian Romanick | 4a45595 | 2010-10-13 15:13:02 -0700 | [diff] [blame] | 2137 | } |
Ian Romanick | d5be2ac | 2010-07-20 11:29:46 -0700 | [diff] [blame] | 2138 | |
Paul Berry | c148ef6 | 2011-08-03 15:37:01 -0700 | [diff] [blame] | 2139 | /* At this point linked should contain all of the linked IR, so |
| 2140 | * validate it to make sure nothing went wrong. |
| 2141 | */ |
Kenneth Graunke | 7d2423a | 2013-08-02 00:35:05 -0700 | [diff] [blame] | 2142 | validate_ir_tree(linked->ir); |
Paul Berry | c148ef6 | 2011-08-03 15:37:01 -0700 | [diff] [blame] | 2143 | |
Paul Berry | 7cfefe6 | 2013-07-30 21:13:48 -0700 | [diff] [blame] | 2144 | /* Set the size of geometry shader input arrays */ |
Paul Berry | e3b86f0 | 2014-01-07 10:58:56 -0800 | [diff] [blame] | 2145 | if (linked->Stage == MESA_SHADER_GEOMETRY) { |
Paul Berry | 7cfefe6 | 2013-07-30 21:13:48 -0700 | [diff] [blame] | 2146 | unsigned num_vertices = vertices_per_prim(prog->Geom.InputType); |
| 2147 | geom_array_resize_visitor input_resize_visitor(num_vertices, prog); |
Matt Turner | 4d78446 | 2014-06-24 21:34:05 -0700 | [diff] [blame] | 2148 | foreach_in_list(ir_instruction, ir, linked->ir) { |
Paul Berry | 7cfefe6 | 2013-07-30 21:13:48 -0700 | [diff] [blame] | 2149 | ir->accept(&input_resize_visitor); |
| 2150 | } |
| 2151 | } |
| 2152 | |
Ian Romanick | ec08b5e | 2014-06-19 12:06:42 -0700 | [diff] [blame] | 2153 | if (ctx->Const.VertexID_is_zero_based) |
| 2154 | lower_vertex_id(linked); |
| 2155 | |
Chris Forbes | 8cf7297 | 2014-09-07 21:42:50 +1200 | [diff] [blame] | 2156 | /* Validate correct usage of barrier() in the tess control shader */ |
| 2157 | if (linked->Stage == MESA_SHADER_TESS_CTRL) { |
| 2158 | barrier_use_visitor visitor(prog); |
| 2159 | foreach_in_list(ir_instruction, ir, linked->ir) { |
| 2160 | ir->accept(&visitor); |
| 2161 | } |
| 2162 | } |
| 2163 | |
Ian Romanick | c87e9ef | 2011-01-25 12:04:08 -0800 | [diff] [blame] | 2164 | /* Make a pass over all variable declarations to ensure that arrays with |
Ian Romanick | 6f53921 | 2010-12-07 18:30:33 -0800 | [diff] [blame] | 2165 | * unspecified sizes have a size specified. The size is inferred from the |
| 2166 | * max_array_access field. |
| 2167 | */ |
Kenneth Graunke | 7d2423a | 2013-08-02 00:35:05 -0700 | [diff] [blame] | 2168 | array_sizing_visitor v; |
| 2169 | v.run(linked->ir); |
Paul Berry | 15e05b9 | 2013-09-25 14:07:37 -0700 | [diff] [blame] | 2170 | v.fixup_unnamed_interface_types(); |
Ian Romanick | 6f53921 | 2010-12-07 18:30:33 -0800 | [diff] [blame] | 2171 | |
Ian Romanick | 3fb8787 | 2010-07-09 14:09:34 -0700 | [diff] [blame] | 2172 | return linked; |
| 2173 | } |
| 2174 | |
Eric Anholt | a721abf | 2010-08-23 10:32:01 -0700 | [diff] [blame] | 2175 | /** |
| 2176 | * Update the sizes of linked shader uniform arrays to the maximum |
| 2177 | * array index used. |
| 2178 | * |
| 2179 | * From page 81 (page 95 of the PDF) of the OpenGL 2.1 spec: |
| 2180 | * |
| 2181 | * If one or more elements of an array are active, |
| 2182 | * GetActiveUniform will return the name of the array in name, |
| 2183 | * subject to the restrictions listed above. The type of the array |
| 2184 | * is returned in type. The size parameter contains the highest |
| 2185 | * array element index used, plus one. The compiler or linker |
| 2186 | * determines the highest index used. There will be only one |
| 2187 | * active uniform reported by the GL per uniform array. |
| 2188 | |
| 2189 | */ |
| 2190 | static void |
Eric Anholt | 586b4b5 | 2010-09-28 14:32:16 -0700 | [diff] [blame] | 2191 | update_array_sizes(struct gl_shader_program *prog) |
Eric Anholt | a721abf | 2010-08-23 10:32:01 -0700 | [diff] [blame] | 2192 | { |
Paul Berry | 665b8d7 | 2014-01-07 10:11:39 -0800 | [diff] [blame] | 2193 | for (unsigned i = 0; i < MESA_SHADER_STAGES; i++) { |
Ian Romanick | 3322fba | 2010-10-14 13:28:42 -0700 | [diff] [blame] | 2194 | if (prog->_LinkedShaders[i] == NULL) |
| 2195 | continue; |
| 2196 | |
Matt Turner | 4d78446 | 2014-06-24 21:34:05 -0700 | [diff] [blame] | 2197 | foreach_in_list(ir_instruction, node, prog->_LinkedShaders[i]->ir) { |
| 2198 | ir_variable *const var = node->as_variable(); |
Eric Anholt | a721abf | 2010-08-23 10:32:01 -0700 | [diff] [blame] | 2199 | |
Tapani Pälli | 33ee2c6 | 2013-12-12 13:51:01 +0200 | [diff] [blame] | 2200 | if ((var == NULL) || (var->data.mode != ir_var_uniform) || |
Eric Anholt | a721abf | 2010-08-23 10:32:01 -0700 | [diff] [blame] | 2201 | !var->type->is_array()) |
| 2202 | continue; |
| 2203 | |
Eric Anholt | 9feb403 | 2012-05-01 14:43:31 -0700 | [diff] [blame] | 2204 | /* GL_ARB_uniform_buffer_object says that std140 uniforms |
| 2205 | * will not be eliminated. Since we always do std140, just |
| 2206 | * don't resize arrays in UBOs. |
Francisco Jerez | 5c11493 | 2013-09-11 12:14:46 -0700 | [diff] [blame] | 2207 | * |
| 2208 | * Atomic counters are supposed to get deterministic |
| 2209 | * locations assigned based on the declaration ordering and |
| 2210 | * sizes, array compaction would mess that up. |
Dave Airlie | 6026686 | 2015-04-20 10:27:36 +1000 | [diff] [blame] | 2211 | * |
| 2212 | * Subroutine uniforms are not removed. |
Eric Anholt | 9feb403 | 2012-05-01 14:43:31 -0700 | [diff] [blame] | 2213 | */ |
Dave Airlie | 6026686 | 2015-04-20 10:27:36 +1000 | [diff] [blame] | 2214 | if (var->is_in_buffer_block() || var->type->contains_atomic() || |
| 2215 | var->type->contains_subroutine()) |
Eric Anholt | 9feb403 | 2012-05-01 14:43:31 -0700 | [diff] [blame] | 2216 | continue; |
| 2217 | |
Tapani Pälli | 447bb90 | 2013-12-12 15:08:59 +0200 | [diff] [blame] | 2218 | unsigned int size = var->data.max_array_access; |
Paul Berry | 665b8d7 | 2014-01-07 10:11:39 -0800 | [diff] [blame] | 2219 | for (unsigned j = 0; j < MESA_SHADER_STAGES; j++) { |
Ian Romanick | 3322fba | 2010-10-14 13:28:42 -0700 | [diff] [blame] | 2220 | if (prog->_LinkedShaders[j] == NULL) |
| 2221 | continue; |
| 2222 | |
Matt Turner | 4d78446 | 2014-06-24 21:34:05 -0700 | [diff] [blame] | 2223 | foreach_in_list(ir_instruction, node2, prog->_LinkedShaders[j]->ir) { |
| 2224 | ir_variable *other_var = node2->as_variable(); |
Eric Anholt | a721abf | 2010-08-23 10:32:01 -0700 | [diff] [blame] | 2225 | if (!other_var) |
| 2226 | continue; |
| 2227 | |
| 2228 | if (strcmp(var->name, other_var->name) == 0 && |
Tapani Pälli | 447bb90 | 2013-12-12 15:08:59 +0200 | [diff] [blame] | 2229 | other_var->data.max_array_access > size) { |
| 2230 | size = other_var->data.max_array_access; |
Eric Anholt | a721abf | 2010-08-23 10:32:01 -0700 | [diff] [blame] | 2231 | } |
| 2232 | } |
| 2233 | } |
Eric Anholt | 586b4b5 | 2010-09-28 14:32:16 -0700 | [diff] [blame] | 2234 | |
Fabian Bieler | 6368478 | 2013-06-14 13:37:07 +0200 | [diff] [blame] | 2235 | if (size + 1 != var->type->length) { |
Ian Romanick | 89d81ab | 2011-01-25 10:41:20 -0800 | [diff] [blame] | 2236 | /* If this is a built-in uniform (i.e., it's backed by some |
| 2237 | * fixed-function state), adjust the number of state slots to |
| 2238 | * match the new array size. The number of slots per array entry |
Bryan Cain | f18a086 | 2011-04-23 19:29:15 -0500 | [diff] [blame] | 2239 | * is not known. It seems safe to assume that the total number of |
Ian Romanick | 89d81ab | 2011-01-25 10:41:20 -0800 | [diff] [blame] | 2240 | * slots is an integer multiple of the number of array elements. |
| 2241 | * Determine the number of slots per array element by dividing by |
| 2242 | * the old (total) size. |
| 2243 | */ |
Ian Romanick | 5aa8d81 | 2014-05-14 19:47:28 -0700 | [diff] [blame] | 2244 | const unsigned num_slots = var->get_num_state_slots(); |
| 2245 | if (num_slots > 0) { |
| 2246 | var->set_num_state_slots((size + 1) |
| 2247 | * (num_slots / var->type->length)); |
Ian Romanick | 89d81ab | 2011-01-25 10:41:20 -0800 | [diff] [blame] | 2248 | } |
| 2249 | |
Eric Anholt | a721abf | 2010-08-23 10:32:01 -0700 | [diff] [blame] | 2250 | var->type = glsl_type::get_array_instance(var->type->fields.array, |
| 2251 | size + 1); |
| 2252 | /* FINISHME: We should update the types of array |
| 2253 | * dereferences of this variable now. |
| 2254 | */ |
| 2255 | } |
| 2256 | } |
| 2257 | } |
| 2258 | } |
| 2259 | |
Ian Romanick | 6984670 | 2010-06-22 17:29:19 -0700 | [diff] [blame] | 2260 | /** |
Chris Forbes | 7c758c5 | 2014-09-21 13:33:14 +1200 | [diff] [blame] | 2261 | * Resize tessellation evaluation per-vertex inputs to the size of |
| 2262 | * tessellation control per-vertex outputs. |
| 2263 | */ |
| 2264 | static void |
| 2265 | resize_tes_inputs(struct gl_context *ctx, |
| 2266 | struct gl_shader_program *prog) |
| 2267 | { |
| 2268 | if (prog->_LinkedShaders[MESA_SHADER_TESS_EVAL] == NULL) |
| 2269 | return; |
| 2270 | |
| 2271 | gl_shader *const tcs = prog->_LinkedShaders[MESA_SHADER_TESS_CTRL]; |
| 2272 | gl_shader *const tes = prog->_LinkedShaders[MESA_SHADER_TESS_EVAL]; |
| 2273 | |
| 2274 | /* If no control shader is present, then the TES inputs are statically |
| 2275 | * sized to MaxPatchVertices; the actual size of the arrays won't be |
| 2276 | * known until draw time. |
| 2277 | */ |
| 2278 | const int num_vertices = tcs |
| 2279 | ? tcs->TessCtrl.VerticesOut |
| 2280 | : ctx->Const.MaxPatchVertices; |
| 2281 | |
| 2282 | tess_eval_array_resize_visitor input_resize_visitor(num_vertices, prog); |
| 2283 | foreach_in_list(ir_instruction, ir, tes->ir) { |
| 2284 | ir->accept(&input_resize_visitor); |
| 2285 | } |
| 2286 | } |
| 2287 | |
| 2288 | /** |
Bryan Cain | f18a086 | 2011-04-23 19:29:15 -0500 | [diff] [blame] | 2289 | * Find a contiguous set of available bits in a bitmask. |
Ian Romanick | 6984670 | 2010-06-22 17:29:19 -0700 | [diff] [blame] | 2290 | * |
| 2291 | * \param used_mask Bits representing used (1) and unused (0) locations |
| 2292 | * \param needed_count Number of contiguous bits needed. |
| 2293 | * |
| 2294 | * \return |
| 2295 | * Base location of the available bits on success or -1 on failure. |
| 2296 | */ |
| 2297 | int |
| 2298 | find_available_slots(unsigned used_mask, unsigned needed_count) |
| 2299 | { |
| 2300 | unsigned needed_mask = (1 << needed_count) - 1; |
| 2301 | const int max_bit_to_test = (8 * sizeof(used_mask)) - needed_count; |
| 2302 | |
| 2303 | /* The comparison to 32 is redundant, but without it GCC emits "warning: |
| 2304 | * cannot optimize possibly infinite loops" for the loop below. |
| 2305 | */ |
| 2306 | if ((needed_count == 0) || (max_bit_to_test < 0) || (max_bit_to_test > 32)) |
| 2307 | return -1; |
| 2308 | |
| 2309 | for (int i = 0; i <= max_bit_to_test; i++) { |
| 2310 | if ((needed_mask & ~used_mask) == needed_mask) |
| 2311 | return i; |
| 2312 | |
| 2313 | needed_mask <<= 1; |
| 2314 | } |
| 2315 | |
| 2316 | return -1; |
| 2317 | } |
| 2318 | |
| 2319 | |
Ian Romanick | d32d4f7 | 2011-06-27 17:59:58 -0700 | [diff] [blame] | 2320 | /** |
Andres Gomez | b0e0c26 | 2014-10-24 16:51:09 +0300 | [diff] [blame] | 2321 | * Assign locations for either VS inputs or FS outputs |
Ian Romanick | d32d4f7 | 2011-06-27 17:59:58 -0700 | [diff] [blame] | 2322 | * |
| 2323 | * \param prog Shader program whose variables need locations assigned |
Tapani Pälli | b868971 | 2015-07-27 13:29:20 +0300 | [diff] [blame] | 2324 | * \param constants Driver specific constant values for the program. |
Ian Romanick | d32d4f7 | 2011-06-27 17:59:58 -0700 | [diff] [blame] | 2325 | * \param target_index Selector for the program target to receive location |
| 2326 | * assignmnets. Must be either \c MESA_SHADER_VERTEX or |
| 2327 | * \c MESA_SHADER_FRAGMENT. |
Ian Romanick | d32d4f7 | 2011-06-27 17:59:58 -0700 | [diff] [blame] | 2328 | * |
| 2329 | * \return |
| 2330 | * If locations are successfully assigned, true is returned. Otherwise an |
| 2331 | * error is emitted to the shader link log and false is returned. |
Ian Romanick | d32d4f7 | 2011-06-27 17:59:58 -0700 | [diff] [blame] | 2332 | */ |
Ian Romanick | 6984670 | 2010-06-22 17:29:19 -0700 | [diff] [blame] | 2333 | bool |
Ian Romanick | d32d4f7 | 2011-06-27 17:59:58 -0700 | [diff] [blame] | 2334 | assign_attribute_or_color_locations(gl_shader_program *prog, |
Tapani Pälli | b868971 | 2015-07-27 13:29:20 +0300 | [diff] [blame] | 2335 | struct gl_constants *constants, |
| 2336 | unsigned target_index) |
Ian Romanick | 0ad22cd | 2010-06-21 17:18:31 -0700 | [diff] [blame] | 2337 | { |
Tapani Pälli | b868971 | 2015-07-27 13:29:20 +0300 | [diff] [blame] | 2338 | /* Maximum number of generic locations. This corresponds to either the |
| 2339 | * maximum number of draw buffers or the maximum number of generic |
| 2340 | * attributes. |
| 2341 | */ |
| 2342 | unsigned max_index = (target_index == MESA_SHADER_VERTEX) ? |
| 2343 | constants->Program[target_index].MaxAttribs : |
| 2344 | MAX2(constants->MaxDrawBuffers, constants->MaxDualSourceDrawBuffers); |
| 2345 | |
Ian Romanick | d32d4f7 | 2011-06-27 17:59:58 -0700 | [diff] [blame] | 2346 | /* Mark invalid locations as being used. |
Ian Romanick | 9342d26 | 2010-06-22 17:41:37 -0700 | [diff] [blame] | 2347 | */ |
Ian Romanick | d32d4f7 | 2011-06-27 17:59:58 -0700 | [diff] [blame] | 2348 | unsigned used_locations = (max_index >= 32) |
| 2349 | ? ~0 : ~((1 << max_index) - 1); |
Kenneth Graunke | c3294ca | 2015-09-02 10:42:57 -0700 | [diff] [blame] | 2350 | unsigned double_storage_locations = 0; |
Ian Romanick | 0ad22cd | 2010-06-21 17:18:31 -0700 | [diff] [blame] | 2351 | |
Ian Romanick | d32d4f7 | 2011-06-27 17:59:58 -0700 | [diff] [blame] | 2352 | assert((target_index == MESA_SHADER_VERTEX) |
| 2353 | || (target_index == MESA_SHADER_FRAGMENT)); |
| 2354 | |
| 2355 | gl_shader *const sh = prog->_LinkedShaders[target_index]; |
| 2356 | if (sh == NULL) |
| 2357 | return true; |
Ian Romanick | 0ad22cd | 2010-06-21 17:18:31 -0700 | [diff] [blame] | 2358 | |
Ian Romanick | 6984670 | 2010-06-22 17:29:19 -0700 | [diff] [blame] | 2359 | /* Operate in a total of four passes. |
Ian Romanick | 0ad22cd | 2010-06-21 17:18:31 -0700 | [diff] [blame] | 2360 | * |
| 2361 | * 1. Invalidate the location assignments for all vertex shader inputs. |
| 2362 | * |
| 2363 | * 2. Assign locations for inputs that have user-defined (via |
Ian Romanick | b12b5d9 | 2011-11-04 16:08:52 -0700 | [diff] [blame] | 2364 | * glBindVertexAttribLocation) locations and outputs that have |
| 2365 | * user-defined locations (via glBindFragDataLocation). |
Ian Romanick | 0ad22cd | 2010-06-21 17:18:31 -0700 | [diff] [blame] | 2366 | * |
Ian Romanick | 6984670 | 2010-06-22 17:29:19 -0700 | [diff] [blame] | 2367 | * 3. Sort the attributes without assigned locations by number of slots |
| 2368 | * required in decreasing order. Fragmentation caused by attribute |
| 2369 | * locations assigned by the application may prevent large attributes |
| 2370 | * from having enough contiguous space. |
| 2371 | * |
| 2372 | * 4. Assign locations to any inputs without assigned locations. |
Ian Romanick | 0ad22cd | 2010-06-21 17:18:31 -0700 | [diff] [blame] | 2373 | */ |
| 2374 | |
Ian Romanick | d32d4f7 | 2011-06-27 17:59:58 -0700 | [diff] [blame] | 2375 | const int generic_base = (target_index == MESA_SHADER_VERTEX) |
Brian Paul | 7eb7d67 | 2011-07-07 16:47:59 -0600 | [diff] [blame] | 2376 | ? (int) VERT_ATTRIB_GENERIC0 : (int) FRAG_RESULT_DATA0; |
Ian Romanick | 0ad22cd | 2010-06-21 17:18:31 -0700 | [diff] [blame] | 2377 | |
Ian Romanick | d32d4f7 | 2011-06-27 17:59:58 -0700 | [diff] [blame] | 2378 | const enum ir_variable_mode direction = |
Paul Berry | 42a29d8 | 2013-01-11 14:39:32 -0800 | [diff] [blame] | 2379 | (target_index == MESA_SHADER_VERTEX) |
| 2380 | ? ir_var_shader_in : ir_var_shader_out; |
Ian Romanick | d32d4f7 | 2011-06-27 17:59:58 -0700 | [diff] [blame] | 2381 | |
| 2382 | |
Ian Romanick | 6984670 | 2010-06-22 17:29:19 -0700 | [diff] [blame] | 2383 | /* Temporary storage for the set of attributes that need locations assigned. |
| 2384 | */ |
| 2385 | struct temp_attr { |
| 2386 | unsigned slots; |
| 2387 | ir_variable *var; |
| 2388 | |
| 2389 | /* Used below in the call to qsort. */ |
| 2390 | static int compare(const void *a, const void *b) |
| 2391 | { |
| 2392 | const temp_attr *const l = (const temp_attr *) a; |
| 2393 | const temp_attr *const r = (const temp_attr *) b; |
| 2394 | |
| 2395 | /* Reversed because we want a descending order sort below. */ |
| 2396 | return r->slots - l->slots; |
| 2397 | } |
| 2398 | } to_assign[16]; |
| 2399 | |
| 2400 | unsigned num_attr = 0; |
| 2401 | |
Matt Turner | 4d78446 | 2014-06-24 21:34:05 -0700 | [diff] [blame] | 2402 | foreach_in_list(ir_instruction, node, sh->ir) { |
| 2403 | ir_variable *const var = node->as_variable(); |
Ian Romanick | 0ad22cd | 2010-06-21 17:18:31 -0700 | [diff] [blame] | 2404 | |
Tapani Pälli | 33ee2c6 | 2013-12-12 13:51:01 +0200 | [diff] [blame] | 2405 | if ((var == NULL) || (var->data.mode != (unsigned) direction)) |
Ian Romanick | 0ad22cd | 2010-06-21 17:18:31 -0700 | [diff] [blame] | 2406 | continue; |
| 2407 | |
Tapani Pälli | 447bb90 | 2013-12-12 15:08:59 +0200 | [diff] [blame] | 2408 | if (var->data.explicit_location) { |
| 2409 | if ((var->data.location >= (int)(max_index + generic_base)) |
| 2410 | || (var->data.location < 0)) { |
Ian Romanick | 586e741 | 2011-07-28 14:04:09 -0700 | [diff] [blame] | 2411 | linker_error(prog, |
| 2412 | "invalid explicit location %d specified for `%s'\n", |
Tapani Pälli | 447bb90 | 2013-12-12 15:08:59 +0200 | [diff] [blame] | 2413 | (var->data.location < 0) |
| 2414 | ? var->data.location |
| 2415 | : var->data.location - generic_base, |
Ian Romanick | 586e741 | 2011-07-28 14:04:09 -0700 | [diff] [blame] | 2416 | var->name); |
Ian Romanick | 68a4fc9 | 2010-10-07 17:21:22 -0700 | [diff] [blame] | 2417 | return false; |
Ian Romanick | 523b611 | 2011-08-17 15:40:03 -0700 | [diff] [blame] | 2418 | } |
| 2419 | } else if (target_index == MESA_SHADER_VERTEX) { |
| 2420 | unsigned binding; |
| 2421 | |
| 2422 | if (prog->AttributeBindings->get(binding, var->name)) { |
| 2423 | assert(binding >= VERT_ATTRIB_GENERIC0); |
Tapani Pälli | 447bb90 | 2013-12-12 15:08:59 +0200 | [diff] [blame] | 2424 | var->data.location = binding; |
| 2425 | var->data.is_unmatched_generic_inout = 0; |
Ian Romanick | 68a4fc9 | 2010-10-07 17:21:22 -0700 | [diff] [blame] | 2426 | } |
Ian Romanick | b12b5d9 | 2011-11-04 16:08:52 -0700 | [diff] [blame] | 2427 | } else if (target_index == MESA_SHADER_FRAGMENT) { |
| 2428 | unsigned binding; |
Dave Airlie | 1256a5d | 2012-03-24 13:33:41 +0000 | [diff] [blame] | 2429 | unsigned index; |
Ian Romanick | b12b5d9 | 2011-11-04 16:08:52 -0700 | [diff] [blame] | 2430 | |
| 2431 | if (prog->FragDataBindings->get(binding, var->name)) { |
| 2432 | assert(binding >= FRAG_RESULT_DATA0); |
Tapani Pälli | 447bb90 | 2013-12-12 15:08:59 +0200 | [diff] [blame] | 2433 | var->data.location = binding; |
| 2434 | var->data.is_unmatched_generic_inout = 0; |
Dave Airlie | 1256a5d | 2012-03-24 13:33:41 +0000 | [diff] [blame] | 2435 | |
| 2436 | if (prog->FragDataIndexBindings->get(index, var->name)) { |
Tapani Pälli | 447bb90 | 2013-12-12 15:08:59 +0200 | [diff] [blame] | 2437 | var->data.index = index; |
Dave Airlie | 1256a5d | 2012-03-24 13:33:41 +0000 | [diff] [blame] | 2438 | } |
Ian Romanick | b12b5d9 | 2011-11-04 16:08:52 -0700 | [diff] [blame] | 2439 | } |
Ian Romanick | 68a4fc9 | 2010-10-07 17:21:22 -0700 | [diff] [blame] | 2440 | } |
| 2441 | |
Tapani Pälli | e17056f | 2015-07-03 10:19:23 +0300 | [diff] [blame] | 2442 | /* From GL4.5 core spec, section 15.2 (Shader Execution): |
| 2443 | * |
| 2444 | * "Output binding assignments will cause LinkProgram to fail: |
| 2445 | * ... |
| 2446 | * If the program has an active output assigned to a location greater |
| 2447 | * than or equal to the value of MAX_DUAL_SOURCE_DRAW_BUFFERS and has |
| 2448 | * an active output assigned an index greater than or equal to one;" |
| 2449 | */ |
| 2450 | if (target_index == MESA_SHADER_FRAGMENT && var->data.index >= 1 && |
| 2451 | var->data.location - generic_base >= |
| 2452 | (int) constants->MaxDualSourceDrawBuffers) { |
| 2453 | linker_error(prog, |
| 2454 | "output location %d >= GL_MAX_DUAL_SOURCE_DRAW_BUFFERS " |
| 2455 | "with index %u for %s\n", |
| 2456 | var->data.location - generic_base, var->data.index, |
| 2457 | var->name); |
| 2458 | return false; |
| 2459 | } |
| 2460 | |
Dave Airlie | ad208d9 | 2015-04-30 10:42:06 +1000 | [diff] [blame] | 2461 | const unsigned slots = var->type->count_attribute_slots(); |
| 2462 | |
Ian Romanick | 9f0e98d | 2011-10-06 10:25:34 -0700 | [diff] [blame] | 2463 | /* If the variable is not a built-in and has a location statically |
| 2464 | * assigned in the shader (presumably via a layout qualifier), make sure |
| 2465 | * that it doesn't collide with other assigned locations. Otherwise, |
| 2466 | * add it to the list of variables that need linker-assigned locations. |
Ian Romanick | 0ad22cd | 2010-06-21 17:18:31 -0700 | [diff] [blame] | 2467 | */ |
Tapani Pälli | 447bb90 | 2013-12-12 15:08:59 +0200 | [diff] [blame] | 2468 | if (var->data.location != -1) { |
| 2469 | if (var->data.location >= generic_base && var->data.index < 1) { |
Ian Romanick | 523b611 | 2011-08-17 15:40:03 -0700 | [diff] [blame] | 2470 | /* From page 61 of the OpenGL 4.0 spec: |
| 2471 | * |
| 2472 | * "LinkProgram will fail if the attribute bindings assigned |
| 2473 | * by BindAttribLocation do not leave not enough space to |
| 2474 | * assign a location for an active matrix attribute or an |
| 2475 | * active attribute array, both of which require multiple |
| 2476 | * contiguous generic attributes." |
| 2477 | * |
Anuj Phogat | 8c61b6a | 2014-03-04 17:03:28 -0800 | [diff] [blame] | 2478 | * I think above text prohibits the aliasing of explicit and |
| 2479 | * automatic assignments. But, aliasing is allowed in manual |
| 2480 | * assignments of attribute locations. See below comments for |
| 2481 | * the details. |
Ian Romanick | 523b611 | 2011-08-17 15:40:03 -0700 | [diff] [blame] | 2482 | * |
Anuj Phogat | 8c61b6a | 2014-03-04 17:03:28 -0800 | [diff] [blame] | 2483 | * From OpenGL 4.0 spec, page 61: |
Ian Romanick | 523b611 | 2011-08-17 15:40:03 -0700 | [diff] [blame] | 2484 | * |
| 2485 | * "It is possible for an application to bind more than one |
| 2486 | * attribute name to the same location. This is referred to as |
| 2487 | * aliasing. This will only work if only one of the aliased |
| 2488 | * attributes is active in the executable program, or if no |
| 2489 | * path through the shader consumes more than one attribute of |
| 2490 | * a set of attributes aliased to the same location. A link |
| 2491 | * error can occur if the linker determines that every path |
| 2492 | * through the shader consumes multiple aliased attributes, |
| 2493 | * but implementations are not required to generate an error |
| 2494 | * in this case." |
| 2495 | * |
Anuj Phogat | 8c61b6a | 2014-03-04 17:03:28 -0800 | [diff] [blame] | 2496 | * From GLSL 4.30 spec, page 54: |
| 2497 | * |
| 2498 | * "A program will fail to link if any two non-vertex shader |
| 2499 | * input variables are assigned to the same location. For |
| 2500 | * vertex shaders, multiple input variables may be assigned |
| 2501 | * to the same location using either layout qualifiers or via |
| 2502 | * the OpenGL API. However, such aliasing is intended only to |
| 2503 | * support vertex shaders where each execution path accesses |
| 2504 | * at most one input per each location. Implementations are |
| 2505 | * permitted, but not required, to generate link-time errors |
| 2506 | * if they detect that every path through the vertex shader |
| 2507 | * executable accesses multiple inputs assigned to any single |
| 2508 | * location. For all shader types, a program will fail to link |
| 2509 | * if explicit location assignments leave the linker unable |
| 2510 | * to find space for other variables without explicit |
| 2511 | * assignments." |
| 2512 | * |
| 2513 | * From OpenGL ES 3.0 spec, page 56: |
| 2514 | * |
| 2515 | * "Binding more than one attribute name to the same location |
| 2516 | * is referred to as aliasing, and is not permitted in OpenGL |
| 2517 | * ES Shading Language 3.00 vertex shaders. LinkProgram will |
| 2518 | * fail when this condition exists. However, aliasing is |
| 2519 | * possible in OpenGL ES Shading Language 1.00 vertex shaders. |
| 2520 | * This will only work if only one of the aliased attributes |
| 2521 | * is active in the executable program, or if no path through |
| 2522 | * the shader consumes more than one attribute of a set of |
| 2523 | * attributes aliased to the same location. A link error can |
| 2524 | * occur if the linker determines that every path through the |
| 2525 | * shader consumes multiple aliased attributes, but implemen- |
| 2526 | * tations are not required to generate an error in this case." |
| 2527 | * |
| 2528 | * After looking at above references from OpenGL, OpenGL ES and |
| 2529 | * GLSL specifications, we allow aliasing of vertex input variables |
| 2530 | * in: OpenGL 2.0 (and above) and OpenGL ES 2.0. |
| 2531 | * |
| 2532 | * NOTE: This is not required by the spec but its worth mentioning |
| 2533 | * here that we're not doing anything to make sure that no path |
| 2534 | * through the vertex shader executable accesses multiple inputs |
| 2535 | * assigned to any single location. |
Ian Romanick | 523b611 | 2011-08-17 15:40:03 -0700 | [diff] [blame] | 2536 | */ |
Anuj Phogat | 8c61b6a | 2014-03-04 17:03:28 -0800 | [diff] [blame] | 2537 | |
Ian Romanick | 523b611 | 2011-08-17 15:40:03 -0700 | [diff] [blame] | 2538 | /* Mask representing the contiguous slots that will be used by |
| 2539 | * this attribute. |
| 2540 | */ |
Tapani Pälli | 447bb90 | 2013-12-12 15:08:59 +0200 | [diff] [blame] | 2541 | const unsigned attr = var->data.location - generic_base; |
Ian Romanick | 523b611 | 2011-08-17 15:40:03 -0700 | [diff] [blame] | 2542 | const unsigned use_mask = (1 << slots) - 1; |
Anuj Phogat | 8c61b6a | 2014-03-04 17:03:28 -0800 | [diff] [blame] | 2543 | const char *const string = (target_index == MESA_SHADER_VERTEX) |
| 2544 | ? "vertex shader input" : "fragment shader output"; |
| 2545 | |
| 2546 | /* Generate a link error if the requested locations for this |
| 2547 | * attribute exceed the maximum allowed attribute location. |
| 2548 | */ |
| 2549 | if (attr + slots > max_index) { |
| 2550 | linker_error(prog, |
| 2551 | "insufficient contiguous locations " |
Andres Gomez | f9fc3ae | 2014-11-18 08:43:35 -0700 | [diff] [blame] | 2552 | "available for %s `%s' %d %d %d\n", string, |
Anuj Phogat | 8c61b6a | 2014-03-04 17:03:28 -0800 | [diff] [blame] | 2553 | var->name, used_locations, use_mask, attr); |
| 2554 | return false; |
| 2555 | } |
Ian Romanick | 0ad22cd | 2010-06-21 17:18:31 -0700 | [diff] [blame] | 2556 | |
Ian Romanick | 523b611 | 2011-08-17 15:40:03 -0700 | [diff] [blame] | 2557 | /* Generate a link error if the set of bits requested for this |
| 2558 | * attribute overlaps any previously allocated bits. |
| 2559 | */ |
| 2560 | if ((~(use_mask << attr) & used_locations) != used_locations) { |
Anuj Phogat | 8c61b6a | 2014-03-04 17:03:28 -0800 | [diff] [blame] | 2561 | if (target_index == MESA_SHADER_FRAGMENT || |
| 2562 | (prog->IsES && prog->Version >= 300)) { |
| 2563 | linker_error(prog, |
| 2564 | "overlapping location is assigned " |
| 2565 | "to %s `%s' %d %d %d\n", string, |
| 2566 | var->name, used_locations, use_mask, attr); |
| 2567 | return false; |
| 2568 | } else { |
| 2569 | linker_warning(prog, |
| 2570 | "overlapping location is assigned " |
| 2571 | "to %s `%s' %d %d %d\n", string, |
| 2572 | var->name, used_locations, use_mask, attr); |
| 2573 | } |
Ian Romanick | 523b611 | 2011-08-17 15:40:03 -0700 | [diff] [blame] | 2574 | } |
| 2575 | |
| 2576 | used_locations |= (use_mask << attr); |
Kenneth Graunke | c3294ca | 2015-09-02 10:42:57 -0700 | [diff] [blame] | 2577 | |
| 2578 | /* From the GL 4.5 core spec, section 11.1.1 (Vertex Attributes): |
| 2579 | * |
| 2580 | * "A program with more than the value of MAX_VERTEX_ATTRIBS |
| 2581 | * active attribute variables may fail to link, unless |
| 2582 | * device-dependent optimizations are able to make the program |
| 2583 | * fit within available hardware resources. For the purposes |
| 2584 | * of this test, attribute variables of the type dvec3, dvec4, |
| 2585 | * dmat2x3, dmat2x4, dmat3, dmat3x4, dmat4x3, and dmat4 may |
| 2586 | * count as consuming twice as many attributes as equivalent |
| 2587 | * single-precision types. While these types use the same number |
| 2588 | * of generic attributes as their single-precision equivalents, |
| 2589 | * implementations are permitted to consume two single-precision |
| 2590 | * vectors of internal storage for each three- or four-component |
| 2591 | * double-precision vector." |
| 2592 | * |
| 2593 | * Mark this attribute slot as taking up twice as much space |
| 2594 | * so we can count it properly against limits. According to |
| 2595 | * issue (3) of the GL_ARB_vertex_attrib_64bit behavior, this |
| 2596 | * is optional behavior, but it seems preferable. |
| 2597 | */ |
| 2598 | const glsl_type *type = var->type->without_array(); |
| 2599 | if (type == glsl_type::dvec3_type || |
| 2600 | type == glsl_type::dvec4_type || |
| 2601 | type == glsl_type::dmat2x3_type || |
| 2602 | type == glsl_type::dmat2x4_type || |
| 2603 | type == glsl_type::dmat3_type || |
| 2604 | type == glsl_type::dmat3x4_type || |
| 2605 | type == glsl_type::dmat4x3_type || |
| 2606 | type == glsl_type::dmat4_type) { |
| 2607 | double_storage_locations |= (use_mask << attr); |
| 2608 | } |
Ian Romanick | 523b611 | 2011-08-17 15:40:03 -0700 | [diff] [blame] | 2609 | } |
| 2610 | |
| 2611 | continue; |
| 2612 | } |
| 2613 | |
| 2614 | to_assign[num_attr].slots = slots; |
Ian Romanick | 6984670 | 2010-06-22 17:29:19 -0700 | [diff] [blame] | 2615 | to_assign[num_attr].var = var; |
| 2616 | num_attr++; |
Ian Romanick | 0ad22cd | 2010-06-21 17:18:31 -0700 | [diff] [blame] | 2617 | } |
Ian Romanick | 6984670 | 2010-06-22 17:29:19 -0700 | [diff] [blame] | 2618 | |
Dave Airlie | ad208d9 | 2015-04-30 10:42:06 +1000 | [diff] [blame] | 2619 | if (target_index == MESA_SHADER_VERTEX) { |
Kenneth Graunke | c3294ca | 2015-09-02 10:42:57 -0700 | [diff] [blame] | 2620 | unsigned total_attribs_size = |
| 2621 | _mesa_bitcount(used_locations & ((1 << max_index) - 1)) + |
| 2622 | _mesa_bitcount(double_storage_locations); |
Dave Airlie | ad208d9 | 2015-04-30 10:42:06 +1000 | [diff] [blame] | 2623 | if (total_attribs_size > max_index) { |
| 2624 | linker_error(prog, |
| 2625 | "attempt to use %d vertex attribute slots only %d available ", |
| 2626 | total_attribs_size, max_index); |
| 2627 | return false; |
| 2628 | } |
| 2629 | } |
| 2630 | |
Ian Romanick | 6984670 | 2010-06-22 17:29:19 -0700 | [diff] [blame] | 2631 | /* If all of the attributes were assigned locations by the application (or |
| 2632 | * are built-in attributes with fixed locations), return early. This should |
| 2633 | * be the common case. |
| 2634 | */ |
| 2635 | if (num_attr == 0) |
| 2636 | return true; |
| 2637 | |
| 2638 | qsort(to_assign, num_attr, sizeof(to_assign[0]), temp_attr::compare); |
| 2639 | |
Ian Romanick | d32d4f7 | 2011-06-27 17:59:58 -0700 | [diff] [blame] | 2640 | if (target_index == MESA_SHADER_VERTEX) { |
| 2641 | /* VERT_ATTRIB_GENERIC0 is a pseudo-alias for VERT_ATTRIB_POS. It can |
| 2642 | * only be explicitly assigned by via glBindAttribLocation. Mark it as |
| 2643 | * reserved to prevent it from being automatically allocated below. |
| 2644 | */ |
| 2645 | find_deref_visitor find("gl_Vertex"); |
| 2646 | find.run(sh->ir); |
| 2647 | if (find.variable_found()) |
| 2648 | used_locations |= (1 << 0); |
| 2649 | } |
Ian Romanick | 982e379 | 2010-06-29 18:58:20 -0700 | [diff] [blame] | 2650 | |
Ian Romanick | 6984670 | 2010-06-22 17:29:19 -0700 | [diff] [blame] | 2651 | for (unsigned i = 0; i < num_attr; i++) { |
| 2652 | /* Mask representing the contiguous slots that will be used by this |
| 2653 | * attribute. |
| 2654 | */ |
| 2655 | const unsigned use_mask = (1 << to_assign[i].slots) - 1; |
| 2656 | |
| 2657 | int location = find_available_slots(used_locations, to_assign[i].slots); |
| 2658 | |
| 2659 | if (location < 0) { |
Ian Romanick | d32d4f7 | 2011-06-27 17:59:58 -0700 | [diff] [blame] | 2660 | const char *const string = (target_index == MESA_SHADER_VERTEX) |
| 2661 | ? "vertex shader input" : "fragment shader output"; |
| 2662 | |
Ian Romanick | 586e741 | 2011-07-28 14:04:09 -0700 | [diff] [blame] | 2663 | linker_error(prog, |
Dave Airlie | 7449ae4 | 2011-11-20 19:56:35 +0000 | [diff] [blame] | 2664 | "insufficient contiguous locations " |
Andres Gomez | f9fc3ae | 2014-11-18 08:43:35 -0700 | [diff] [blame] | 2665 | "available for %s `%s'\n", |
Ian Romanick | 586e741 | 2011-07-28 14:04:09 -0700 | [diff] [blame] | 2666 | string, to_assign[i].var->name); |
Ian Romanick | 6984670 | 2010-06-22 17:29:19 -0700 | [diff] [blame] | 2667 | return false; |
| 2668 | } |
| 2669 | |
Tapani Pälli | 447bb90 | 2013-12-12 15:08:59 +0200 | [diff] [blame] | 2670 | to_assign[i].var->data.location = generic_base + location; |
| 2671 | to_assign[i].var->data.is_unmatched_generic_inout = 0; |
Ian Romanick | 6984670 | 2010-06-22 17:29:19 -0700 | [diff] [blame] | 2672 | used_locations |= (use_mask << location); |
| 2673 | } |
| 2674 | |
| 2675 | return true; |
Ian Romanick | 0ad22cd | 2010-06-21 17:18:31 -0700 | [diff] [blame] | 2676 | } |
| 2677 | |
| 2678 | |
Ian Romanick | 40e114b | 2010-08-17 14:55:50 -0700 | [diff] [blame] | 2679 | /** |
Ian Romanick | cc90e62 | 2010-10-19 17:59:10 -0700 | [diff] [blame] | 2680 | * Demote shader inputs and outputs that are not used in other stages |
Ian Romanick | 40e114b | 2010-08-17 14:55:50 -0700 | [diff] [blame] | 2681 | */ |
| 2682 | void |
Ian Romanick | cc90e62 | 2010-10-19 17:59:10 -0700 | [diff] [blame] | 2683 | demote_shader_inputs_and_outputs(gl_shader *sh, enum ir_variable_mode mode) |
Ian Romanick | 40e114b | 2010-08-17 14:55:50 -0700 | [diff] [blame] | 2684 | { |
Matt Turner | 4d78446 | 2014-06-24 21:34:05 -0700 | [diff] [blame] | 2685 | foreach_in_list(ir_instruction, node, sh->ir) { |
| 2686 | ir_variable *const var = node->as_variable(); |
Ian Romanick | 40e114b | 2010-08-17 14:55:50 -0700 | [diff] [blame] | 2687 | |
Tapani Pälli | 33ee2c6 | 2013-12-12 13:51:01 +0200 | [diff] [blame] | 2688 | if ((var == NULL) || (var->data.mode != int(mode))) |
Ian Romanick | 40e114b | 2010-08-17 14:55:50 -0700 | [diff] [blame] | 2689 | continue; |
| 2690 | |
Ian Romanick | cc90e62 | 2010-10-19 17:59:10 -0700 | [diff] [blame] | 2691 | /* A shader 'in' or 'out' variable is only really an input or output if |
| 2692 | * its value is used by other shader stages. This will cause the variable |
| 2693 | * to have a location assigned. |
Ian Romanick | 40e114b | 2010-08-17 14:55:50 -0700 | [diff] [blame] | 2694 | */ |
Tapani Pälli | 447bb90 | 2013-12-12 15:08:59 +0200 | [diff] [blame] | 2695 | if (var->data.is_unmatched_generic_inout) { |
Ian Romanick | a994824 | 2014-07-08 18:53:09 -0700 | [diff] [blame] | 2696 | assert(var->data.mode != ir_var_temporary); |
Tapani Pälli | 33ee2c6 | 2013-12-12 13:51:01 +0200 | [diff] [blame] | 2697 | var->data.mode = ir_var_auto; |
Ian Romanick | 40e114b | 2010-08-17 14:55:50 -0700 | [diff] [blame] | 2698 | } |
| 2699 | } |
| 2700 | } |
| 2701 | |
| 2702 | |
Paul Berry | 871ddb9 | 2011-11-05 11:17:32 -0700 | [diff] [blame] | 2703 | /** |
Marek Olšák | ec174a4 | 2011-11-18 15:00:10 +0100 | [diff] [blame] | 2704 | * Store the gl_FragDepth layout in the gl_shader_program struct. |
| 2705 | */ |
| 2706 | static void |
| 2707 | store_fragdepth_layout(struct gl_shader_program *prog) |
| 2708 | { |
| 2709 | if (prog->_LinkedShaders[MESA_SHADER_FRAGMENT] == NULL) { |
| 2710 | return; |
| 2711 | } |
| 2712 | |
| 2713 | struct exec_list *ir = prog->_LinkedShaders[MESA_SHADER_FRAGMENT]->ir; |
| 2714 | |
| 2715 | /* We don't look up the gl_FragDepth symbol directly because if |
| 2716 | * gl_FragDepth is not used in the shader, it's removed from the IR. |
| 2717 | * However, the symbol won't be removed from the symbol table. |
| 2718 | * |
| 2719 | * We're only interested in the cases where the variable is NOT removed |
| 2720 | * from the IR. |
| 2721 | */ |
Matt Turner | 4d78446 | 2014-06-24 21:34:05 -0700 | [diff] [blame] | 2722 | foreach_in_list(ir_instruction, node, ir) { |
| 2723 | ir_variable *const var = node->as_variable(); |
Marek Olšák | ec174a4 | 2011-11-18 15:00:10 +0100 | [diff] [blame] | 2724 | |
Tapani Pälli | 33ee2c6 | 2013-12-12 13:51:01 +0200 | [diff] [blame] | 2725 | if (var == NULL || var->data.mode != ir_var_shader_out) { |
Marek Olšák | ec174a4 | 2011-11-18 15:00:10 +0100 | [diff] [blame] | 2726 | continue; |
| 2727 | } |
| 2728 | |
| 2729 | if (strcmp(var->name, "gl_FragDepth") == 0) { |
Tapani Pälli | 447bb90 | 2013-12-12 15:08:59 +0200 | [diff] [blame] | 2730 | switch (var->data.depth_layout) { |
Marek Olšák | ec174a4 | 2011-11-18 15:00:10 +0100 | [diff] [blame] | 2731 | case ir_depth_layout_none: |
| 2732 | prog->FragDepthLayout = FRAG_DEPTH_LAYOUT_NONE; |
| 2733 | return; |
| 2734 | case ir_depth_layout_any: |
| 2735 | prog->FragDepthLayout = FRAG_DEPTH_LAYOUT_ANY; |
| 2736 | return; |
| 2737 | case ir_depth_layout_greater: |
| 2738 | prog->FragDepthLayout = FRAG_DEPTH_LAYOUT_GREATER; |
| 2739 | return; |
| 2740 | case ir_depth_layout_less: |
| 2741 | prog->FragDepthLayout = FRAG_DEPTH_LAYOUT_LESS; |
| 2742 | return; |
| 2743 | case ir_depth_layout_unchanged: |
| 2744 | prog->FragDepthLayout = FRAG_DEPTH_LAYOUT_UNCHANGED; |
| 2745 | return; |
| 2746 | default: |
| 2747 | assert(0); |
| 2748 | return; |
| 2749 | } |
| 2750 | } |
| 2751 | } |
| 2752 | } |
| 2753 | |
| 2754 | /** |
Ian Romanick | 92f8159 | 2011-11-08 12:37:19 -0800 | [diff] [blame] | 2755 | * Validate the resources used by a program versus the implementation limits |
| 2756 | */ |
Paul Berry | b95d237 | 2013-07-27 11:08:31 -0700 | [diff] [blame] | 2757 | static void |
Ian Romanick | 92f8159 | 2011-11-08 12:37:19 -0800 | [diff] [blame] | 2758 | check_resources(struct gl_context *ctx, struct gl_shader_program *prog) |
| 2759 | { |
Paul Berry | 665b8d7 | 2014-01-07 10:11:39 -0800 | [diff] [blame] | 2760 | for (unsigned i = 0; i < MESA_SHADER_STAGES; i++) { |
Ian Romanick | 92f8159 | 2011-11-08 12:37:19 -0800 | [diff] [blame] | 2761 | struct gl_shader *sh = prog->_LinkedShaders[i]; |
| 2762 | |
| 2763 | if (sh == NULL) |
| 2764 | continue; |
| 2765 | |
Paul Berry | bce8bc0 | 2014-01-08 10:17:01 -0800 | [diff] [blame] | 2766 | if (sh->num_samplers > ctx->Const.Program[i].MaxTextureImageUnits) { |
Andres Gomez | f9fc3ae | 2014-11-18 08:43:35 -0700 | [diff] [blame] | 2767 | linker_error(prog, "Too many %s shader texture samplers\n", |
Paul Berry | 665b8d7 | 2014-01-07 10:11:39 -0800 | [diff] [blame] | 2768 | _mesa_shader_stage_to_string(i)); |
Ian Romanick | 92f8159 | 2011-11-08 12:37:19 -0800 | [diff] [blame] | 2769 | } |
| 2770 | |
Paul Berry | bce8bc0 | 2014-01-08 10:17:01 -0800 | [diff] [blame] | 2771 | if (sh->num_uniform_components > |
| 2772 | ctx->Const.Program[i].MaxUniformComponents) { |
Eric Anholt | 38e77e5 | 2013-05-23 11:10:15 -0700 | [diff] [blame] | 2773 | if (ctx->Const.GLSLSkipStrictMaxUniformLimitCheck) { |
| 2774 | linker_warning(prog, "Too many %s shader default uniform block " |
| 2775 | "components, but the driver will try to optimize " |
| 2776 | "them out; this is non-portable out-of-spec " |
| 2777 | "behavior\n", |
Paul Berry | 665b8d7 | 2014-01-07 10:11:39 -0800 | [diff] [blame] | 2778 | _mesa_shader_stage_to_string(i)); |
Eric Anholt | 38e77e5 | 2013-05-23 11:10:15 -0700 | [diff] [blame] | 2779 | } else { |
| 2780 | linker_error(prog, "Too many %s shader default uniform block " |
Andres Gomez | f9fc3ae | 2014-11-18 08:43:35 -0700 | [diff] [blame] | 2781 | "components\n", |
Paul Berry | 665b8d7 | 2014-01-07 10:11:39 -0800 | [diff] [blame] | 2782 | _mesa_shader_stage_to_string(i)); |
Eric Anholt | 38e77e5 | 2013-05-23 11:10:15 -0700 | [diff] [blame] | 2783 | } |
| 2784 | } |
| 2785 | |
| 2786 | if (sh->num_combined_uniform_components > |
Paul Berry | bce8bc0 | 2014-01-08 10:17:01 -0800 | [diff] [blame] | 2787 | ctx->Const.Program[i].MaxCombinedUniformComponents) { |
Marek Olšák | df809ae | 2011-12-10 04:14:46 +0100 | [diff] [blame] | 2788 | if (ctx->Const.GLSLSkipStrictMaxUniformLimitCheck) { |
| 2789 | linker_warning(prog, "Too many %s shader uniform components, " |
| 2790 | "but the driver will try to optimize them out; " |
| 2791 | "this is non-portable out-of-spec behavior\n", |
Paul Berry | 665b8d7 | 2014-01-07 10:11:39 -0800 | [diff] [blame] | 2792 | _mesa_shader_stage_to_string(i)); |
Marek Olšák | df809ae | 2011-12-10 04:14:46 +0100 | [diff] [blame] | 2793 | } else { |
Andres Gomez | f9fc3ae | 2014-11-18 08:43:35 -0700 | [diff] [blame] | 2794 | linker_error(prog, "Too many %s shader uniform components\n", |
Paul Berry | 665b8d7 | 2014-01-07 10:11:39 -0800 | [diff] [blame] | 2795 | _mesa_shader_stage_to_string(i)); |
Marek Olšák | df809ae | 2011-12-10 04:14:46 +0100 | [diff] [blame] | 2796 | } |
Ian Romanick | 92f8159 | 2011-11-08 12:37:19 -0800 | [diff] [blame] | 2797 | } |
| 2798 | } |
| 2799 | |
Paul Berry | 665b8d7 | 2014-01-07 10:11:39 -0800 | [diff] [blame] | 2800 | unsigned blocks[MESA_SHADER_STAGES] = {0}; |
Eric Anholt | 877a897 | 2012-06-25 12:47:01 -0700 | [diff] [blame] | 2801 | unsigned total_uniform_blocks = 0; |
Samuel Iglesias Gonsalvez | 138e4ae | 2015-04-22 16:58:45 +0200 | [diff] [blame] | 2802 | unsigned shader_blocks[MESA_SHADER_STAGES] = {0}; |
| 2803 | unsigned total_shader_storage_blocks = 0; |
Eric Anholt | 877a897 | 2012-06-25 12:47:01 -0700 | [diff] [blame] | 2804 | |
Samuel Iglesias Gonsalvez | 6668eb5 | 2015-09-11 12:29:37 +0200 | [diff] [blame] | 2805 | for (unsigned i = 0; i < prog->NumBufferInterfaceBlocks; i++) { |
Samuel Iglesias Gonsalvez | 203cd1b | 2015-06-23 08:53:36 +0200 | [diff] [blame] | 2806 | /* Don't check SSBOs for Uniform Block Size */ |
| 2807 | if (!prog->UniformBlocks[i].IsShaderStorage && |
| 2808 | prog->UniformBlocks[i].UniformBufferSize > ctx->Const.MaxUniformBlockSize) { |
Jose Fonseca | f734d25 | 2015-06-15 18:29:02 +0100 | [diff] [blame] | 2809 | linker_error(prog, "Uniform block %s too big (%d/%d)\n", |
| 2810 | prog->UniformBlocks[i].Name, |
| 2811 | prog->UniformBlocks[i].UniformBufferSize, |
| 2812 | ctx->Const.MaxUniformBlockSize); |
| 2813 | } |
| 2814 | |
Samuel Iglesias Gonsalvez | 203cd1b | 2015-06-23 08:53:36 +0200 | [diff] [blame] | 2815 | if (prog->UniformBlocks[i].IsShaderStorage && |
| 2816 | prog->UniformBlocks[i].UniformBufferSize > ctx->Const.MaxShaderStorageBlockSize) { |
| 2817 | linker_error(prog, "Shader storage block %s too big (%d/%d)\n", |
| 2818 | prog->UniformBlocks[i].Name, |
| 2819 | prog->UniformBlocks[i].UniformBufferSize, |
| 2820 | ctx->Const.MaxShaderStorageBlockSize); |
| 2821 | } |
| 2822 | |
Paul Berry | 665b8d7 | 2014-01-07 10:11:39 -0800 | [diff] [blame] | 2823 | for (unsigned j = 0; j < MESA_SHADER_STAGES; j++) { |
Eric Anholt | 877a897 | 2012-06-25 12:47:01 -0700 | [diff] [blame] | 2824 | if (prog->UniformBlockStageIndex[j][i] != -1) { |
Samuel Iglesias Gonsalvez | 138e4ae | 2015-04-22 16:58:45 +0200 | [diff] [blame] | 2825 | struct gl_shader *sh = prog->_LinkedShaders[j]; |
| 2826 | int stage_index = prog->UniformBlockStageIndex[j][i]; |
| 2827 | if (sh && sh->UniformBlocks[stage_index].IsShaderStorage) { |
| 2828 | shader_blocks[j]++; |
| 2829 | total_shader_storage_blocks++; |
| 2830 | } else { |
| 2831 | blocks[j]++; |
| 2832 | total_uniform_blocks++; |
| 2833 | } |
Eric Anholt | 877a897 | 2012-06-25 12:47:01 -0700 | [diff] [blame] | 2834 | } |
| 2835 | } |
| 2836 | |
| 2837 | if (total_uniform_blocks > ctx->Const.MaxCombinedUniformBlocks) { |
Andres Gomez | f9fc3ae | 2014-11-18 08:43:35 -0700 | [diff] [blame] | 2838 | linker_error(prog, "Too many combined uniform blocks (%d/%d)\n", |
Samuel Iglesias Gonsalvez | 7efb235 | 2015-09-11 12:31:10 +0200 | [diff] [blame] | 2839 | total_uniform_blocks, |
Eric Anholt | 877a897 | 2012-06-25 12:47:01 -0700 | [diff] [blame] | 2840 | ctx->Const.MaxCombinedUniformBlocks); |
| 2841 | } else { |
Paul Berry | 665b8d7 | 2014-01-07 10:11:39 -0800 | [diff] [blame] | 2842 | for (unsigned i = 0; i < MESA_SHADER_STAGES; i++) { |
Paul Berry | bce8bc0 | 2014-01-08 10:17:01 -0800 | [diff] [blame] | 2843 | const unsigned max_uniform_blocks = |
| 2844 | ctx->Const.Program[i].MaxUniformBlocks; |
| 2845 | if (blocks[i] > max_uniform_blocks) { |
Andres Gomez | f9fc3ae | 2014-11-18 08:43:35 -0700 | [diff] [blame] | 2846 | linker_error(prog, "Too many %s uniform blocks (%d/%d)\n", |
Paul Berry | 665b8d7 | 2014-01-07 10:11:39 -0800 | [diff] [blame] | 2847 | _mesa_shader_stage_to_string(i), |
Eric Anholt | 877a897 | 2012-06-25 12:47:01 -0700 | [diff] [blame] | 2848 | blocks[i], |
Paul Berry | bce8bc0 | 2014-01-08 10:17:01 -0800 | [diff] [blame] | 2849 | max_uniform_blocks); |
Eric Anholt | 877a897 | 2012-06-25 12:47:01 -0700 | [diff] [blame] | 2850 | break; |
| 2851 | } |
| 2852 | } |
| 2853 | } |
Samuel Iglesias Gonsalvez | 138e4ae | 2015-04-22 16:58:45 +0200 | [diff] [blame] | 2854 | |
| 2855 | if (total_shader_storage_blocks > ctx->Const.MaxCombinedShaderStorageBlocks) { |
| 2856 | linker_error(prog, "Too many combined shader storage blocks (%d/%d)\n", |
| 2857 | total_shader_storage_blocks, |
| 2858 | ctx->Const.MaxCombinedShaderStorageBlocks); |
| 2859 | } else { |
| 2860 | for (unsigned i = 0; i < MESA_SHADER_STAGES; i++) { |
| 2861 | const unsigned max_shader_storage_blocks = |
| 2862 | ctx->Const.Program[i].MaxShaderStorageBlocks; |
| 2863 | if (shader_blocks[i] > max_shader_storage_blocks) { |
| 2864 | linker_error(prog, "Too many %s shader storage blocks (%d/%d)\n", |
| 2865 | _mesa_shader_stage_to_string(i), |
| 2866 | shader_blocks[i], |
| 2867 | max_shader_storage_blocks); |
| 2868 | break; |
| 2869 | } |
| 2870 | } |
| 2871 | } |
Eric Anholt | 877a897 | 2012-06-25 12:47:01 -0700 | [diff] [blame] | 2872 | } |
Ian Romanick | 92f8159 | 2011-11-08 12:37:19 -0800 | [diff] [blame] | 2873 | } |
Paul Berry | 871ddb9 | 2011-11-05 11:17:32 -0700 | [diff] [blame] | 2874 | |
Dave Airlie | 6026686 | 2015-04-20 10:27:36 +1000 | [diff] [blame] | 2875 | static void |
Ian Romanick | 4ff9e59 | 2015-08-19 13:36:22 -0700 | [diff] [blame] | 2876 | link_calculate_subroutine_compat(struct gl_shader_program *prog) |
Dave Airlie | 6026686 | 2015-04-20 10:27:36 +1000 | [diff] [blame] | 2877 | { |
| 2878 | for (unsigned i = 0; i < MESA_SHADER_STAGES; i++) { |
| 2879 | struct gl_shader *sh = prog->_LinkedShaders[i]; |
| 2880 | int count; |
| 2881 | if (!sh) |
| 2882 | continue; |
| 2883 | |
| 2884 | for (unsigned j = 0; j < sh->NumSubroutineUniformRemapTable; j++) { |
| 2885 | struct gl_uniform_storage *uni = sh->SubroutineUniformRemapTable[j]; |
| 2886 | |
| 2887 | if (!uni) |
| 2888 | continue; |
| 2889 | |
| 2890 | count = 0; |
| 2891 | for (unsigned f = 0; f < sh->NumSubroutineFunctions; f++) { |
| 2892 | struct gl_subroutine_function *fn = &sh->SubroutineFunctions[f]; |
| 2893 | for (int k = 0; k < fn->num_compat_types; k++) { |
| 2894 | if (fn->types[k] == uni->type) { |
| 2895 | count++; |
| 2896 | break; |
| 2897 | } |
| 2898 | } |
| 2899 | } |
| 2900 | uni->num_compatible_subroutines = count; |
| 2901 | } |
| 2902 | } |
| 2903 | } |
| 2904 | |
| 2905 | static void |
Ian Romanick | 4ff9e59 | 2015-08-19 13:36:22 -0700 | [diff] [blame] | 2906 | check_subroutine_resources(struct gl_shader_program *prog) |
Dave Airlie | 6026686 | 2015-04-20 10:27:36 +1000 | [diff] [blame] | 2907 | { |
| 2908 | for (unsigned i = 0; i < MESA_SHADER_STAGES; i++) { |
| 2909 | struct gl_shader *sh = prog->_LinkedShaders[i]; |
| 2910 | |
| 2911 | if (sh) { |
| 2912 | if (sh->NumSubroutineUniformRemapTable > MAX_SUBROUTINE_UNIFORM_LOCATIONS) |
| 2913 | linker_error(prog, "Too many %s shader subroutine uniforms\n", |
| 2914 | _mesa_shader_stage_to_string(i)); |
| 2915 | } |
| 2916 | } |
| 2917 | } |
Francisco Jerez | e51158f | 2013-11-22 15:53:26 -0800 | [diff] [blame] | 2918 | /** |
| 2919 | * Validate shader image resources. |
| 2920 | */ |
| 2921 | static void |
| 2922 | check_image_resources(struct gl_context *ctx, struct gl_shader_program *prog) |
| 2923 | { |
| 2924 | unsigned total_image_units = 0; |
| 2925 | unsigned fragment_outputs = 0; |
Samuel Iglesias Gonsalvez | 138e4ae | 2015-04-22 16:58:45 +0200 | [diff] [blame] | 2926 | unsigned total_shader_storage_blocks = 0; |
Francisco Jerez | e51158f | 2013-11-22 15:53:26 -0800 | [diff] [blame] | 2927 | |
| 2928 | if (!ctx->Extensions.ARB_shader_image_load_store) |
| 2929 | return; |
| 2930 | |
| 2931 | for (unsigned i = 0; i < MESA_SHADER_STAGES; i++) { |
| 2932 | struct gl_shader *sh = prog->_LinkedShaders[i]; |
| 2933 | |
| 2934 | if (sh) { |
| 2935 | if (sh->NumImages > ctx->Const.Program[i].MaxImageUniforms) |
Timothy Arceri | b8f63b3 | 2015-08-12 17:01:52 +1000 | [diff] [blame] | 2936 | linker_error(prog, "Too many %s shader image uniforms (%u > %u)\n", |
| 2937 | _mesa_shader_stage_to_string(i), sh->NumImages, |
| 2938 | ctx->Const.Program[i].MaxImageUniforms); |
Francisco Jerez | e51158f | 2013-11-22 15:53:26 -0800 | [diff] [blame] | 2939 | |
| 2940 | total_image_units += sh->NumImages; |
| 2941 | |
Samuel Iglesias Gonsalvez | 6668eb5 | 2015-09-11 12:29:37 +0200 | [diff] [blame] | 2942 | for (unsigned j = 0; j < prog->NumBufferInterfaceBlocks; j++) { |
Samuel Iglesias Gonsalvez | 138e4ae | 2015-04-22 16:58:45 +0200 | [diff] [blame] | 2943 | int stage_index = prog->UniformBlockStageIndex[i][j]; |
| 2944 | if (stage_index != -1 && sh->UniformBlocks[stage_index].IsShaderStorage) |
| 2945 | total_shader_storage_blocks++; |
| 2946 | } |
| 2947 | |
Francisco Jerez | e51158f | 2013-11-22 15:53:26 -0800 | [diff] [blame] | 2948 | if (i == MESA_SHADER_FRAGMENT) { |
Matt Turner | 4d78446 | 2014-06-24 21:34:05 -0700 | [diff] [blame] | 2949 | foreach_in_list(ir_instruction, node, sh->ir) { |
| 2950 | ir_variable *var = node->as_variable(); |
Francisco Jerez | e51158f | 2013-11-22 15:53:26 -0800 | [diff] [blame] | 2951 | if (var && var->data.mode == ir_var_shader_out) |
| 2952 | fragment_outputs += var->type->count_attribute_slots(); |
| 2953 | } |
| 2954 | } |
| 2955 | } |
| 2956 | } |
| 2957 | |
| 2958 | if (total_image_units > ctx->Const.MaxCombinedImageUniforms) |
Andres Gomez | f9fc3ae | 2014-11-18 08:43:35 -0700 | [diff] [blame] | 2959 | linker_error(prog, "Too many combined image uniforms\n"); |
Francisco Jerez | e51158f | 2013-11-22 15:53:26 -0800 | [diff] [blame] | 2960 | |
Samuel Iglesias Gonsalvez | 138e4ae | 2015-04-22 16:58:45 +0200 | [diff] [blame] | 2961 | if (total_image_units + fragment_outputs + total_shader_storage_blocks > |
Francisco Jerez | 47e0d5b | 2015-08-17 19:10:46 +0300 | [diff] [blame] | 2962 | ctx->Const.MaxCombinedShaderOutputResources) |
Samuel Iglesias Gonsalvez | 138e4ae | 2015-04-22 16:58:45 +0200 | [diff] [blame] | 2963 | linker_error(prog, "Too many combined image uniforms, shader storage " |
| 2964 | " buffers and fragment outputs\n"); |
Francisco Jerez | e51158f | 2013-11-22 15:53:26 -0800 | [diff] [blame] | 2965 | } |
| 2966 | |
Tapani Pälli | eca9d16 | 2014-04-08 08:45:36 +0300 | [diff] [blame] | 2967 | |
| 2968 | /** |
| 2969 | * Initializes explicit location slots to INACTIVE_UNIFORM_EXPLICIT_LOCATION |
| 2970 | * for a variable, checks for overlaps between other uniforms using explicit |
| 2971 | * locations. |
| 2972 | */ |
| 2973 | static bool |
| 2974 | reserve_explicit_locations(struct gl_shader_program *prog, |
| 2975 | string_to_uint_map *map, ir_variable *var) |
| 2976 | { |
| 2977 | unsigned slots = var->type->uniform_locations(); |
| 2978 | unsigned max_loc = var->data.location + slots - 1; |
| 2979 | |
| 2980 | /* Resize remap table if locations do not fit in the current one. */ |
| 2981 | if (max_loc + 1 > prog->NumUniformRemapTable) { |
| 2982 | prog->UniformRemapTable = |
| 2983 | reralloc(prog, prog->UniformRemapTable, |
| 2984 | gl_uniform_storage *, |
| 2985 | max_loc + 1); |
| 2986 | |
| 2987 | if (!prog->UniformRemapTable) { |
Andres Gomez | f9fc3ae | 2014-11-18 08:43:35 -0700 | [diff] [blame] | 2988 | linker_error(prog, "Out of memory during linking.\n"); |
Tapani Pälli | eca9d16 | 2014-04-08 08:45:36 +0300 | [diff] [blame] | 2989 | return false; |
| 2990 | } |
| 2991 | |
| 2992 | /* Initialize allocated space. */ |
| 2993 | for (unsigned i = prog->NumUniformRemapTable; i < max_loc + 1; i++) |
| 2994 | prog->UniformRemapTable[i] = NULL; |
| 2995 | |
| 2996 | prog->NumUniformRemapTable = max_loc + 1; |
| 2997 | } |
| 2998 | |
| 2999 | for (unsigned i = 0; i < slots; i++) { |
| 3000 | unsigned loc = var->data.location + i; |
| 3001 | |
| 3002 | /* Check if location is already used. */ |
| 3003 | if (prog->UniformRemapTable[loc] == INACTIVE_UNIFORM_EXPLICIT_LOCATION) { |
| 3004 | |
| 3005 | /* Possibly same uniform from a different stage, this is ok. */ |
| 3006 | unsigned hash_loc; |
| 3007 | if (map->get(hash_loc, var->name) && hash_loc == loc - i) |
| 3008 | continue; |
| 3009 | |
| 3010 | /* ARB_explicit_uniform_location specification states: |
| 3011 | * |
| 3012 | * "No two default-block uniform variables in the program can have |
| 3013 | * the same location, even if they are unused, otherwise a compiler |
| 3014 | * or linker error will be generated." |
| 3015 | */ |
| 3016 | linker_error(prog, |
Neil Roberts | 352f8f2 | 2014-11-13 15:31:44 +0000 | [diff] [blame] | 3017 | "location qualifier for uniform %s overlaps " |
Andres Gomez | f9fc3ae | 2014-11-18 08:43:35 -0700 | [diff] [blame] | 3018 | "previously used location\n", |
Tapani Pälli | eca9d16 | 2014-04-08 08:45:36 +0300 | [diff] [blame] | 3019 | var->name); |
| 3020 | return false; |
| 3021 | } |
| 3022 | |
| 3023 | /* Initialize location as inactive before optimization |
| 3024 | * rounds and location assignment. |
| 3025 | */ |
| 3026 | prog->UniformRemapTable[loc] = INACTIVE_UNIFORM_EXPLICIT_LOCATION; |
| 3027 | } |
| 3028 | |
| 3029 | /* Note, base location used for arrays. */ |
| 3030 | map->put(var->data.location, var->name); |
| 3031 | |
| 3032 | return true; |
| 3033 | } |
| 3034 | |
Dave Airlie | 6026686 | 2015-04-20 10:27:36 +1000 | [diff] [blame] | 3035 | static bool |
| 3036 | reserve_subroutine_explicit_locations(struct gl_shader_program *prog, |
| 3037 | struct gl_shader *sh, |
| 3038 | ir_variable *var) |
| 3039 | { |
| 3040 | unsigned slots = var->type->uniform_locations(); |
| 3041 | unsigned max_loc = var->data.location + slots - 1; |
| 3042 | |
| 3043 | /* Resize remap table if locations do not fit in the current one. */ |
| 3044 | if (max_loc + 1 > sh->NumSubroutineUniformRemapTable) { |
| 3045 | sh->SubroutineUniformRemapTable = |
| 3046 | reralloc(sh, sh->SubroutineUniformRemapTable, |
| 3047 | gl_uniform_storage *, |
| 3048 | max_loc + 1); |
| 3049 | |
| 3050 | if (!sh->SubroutineUniformRemapTable) { |
| 3051 | linker_error(prog, "Out of memory during linking.\n"); |
| 3052 | return false; |
| 3053 | } |
| 3054 | |
| 3055 | /* Initialize allocated space. */ |
| 3056 | for (unsigned i = sh->NumSubroutineUniformRemapTable; i < max_loc + 1; i++) |
| 3057 | sh->SubroutineUniformRemapTable[i] = NULL; |
| 3058 | |
| 3059 | sh->NumSubroutineUniformRemapTable = max_loc + 1; |
| 3060 | } |
| 3061 | |
| 3062 | for (unsigned i = 0; i < slots; i++) { |
| 3063 | unsigned loc = var->data.location + i; |
| 3064 | |
| 3065 | /* Check if location is already used. */ |
| 3066 | if (sh->SubroutineUniformRemapTable[loc] == INACTIVE_UNIFORM_EXPLICIT_LOCATION) { |
| 3067 | |
| 3068 | /* ARB_explicit_uniform_location specification states: |
| 3069 | * "No two subroutine uniform variables can have the same location |
| 3070 | * in the same shader stage, otherwise a compiler or linker error |
| 3071 | * will be generated." |
| 3072 | */ |
| 3073 | linker_error(prog, |
| 3074 | "location qualifier for uniform %s overlaps " |
| 3075 | "previously used location\n", |
| 3076 | var->name); |
| 3077 | return false; |
| 3078 | } |
| 3079 | |
| 3080 | /* Initialize location as inactive before optimization |
| 3081 | * rounds and location assignment. |
| 3082 | */ |
| 3083 | sh->SubroutineUniformRemapTable[loc] = INACTIVE_UNIFORM_EXPLICIT_LOCATION; |
| 3084 | } |
| 3085 | |
| 3086 | return true; |
| 3087 | } |
Tapani Pälli | eca9d16 | 2014-04-08 08:45:36 +0300 | [diff] [blame] | 3088 | /** |
| 3089 | * Check and reserve all explicit uniform locations, called before |
| 3090 | * any optimizations happen to handle also inactive uniforms and |
| 3091 | * inactive array elements that may get trimmed away. |
| 3092 | */ |
| 3093 | static void |
| 3094 | check_explicit_uniform_locations(struct gl_context *ctx, |
| 3095 | struct gl_shader_program *prog) |
| 3096 | { |
| 3097 | if (!ctx->Extensions.ARB_explicit_uniform_location) |
| 3098 | return; |
| 3099 | |
| 3100 | /* This map is used to detect if overlapping explicit locations |
| 3101 | * occur with the same uniform (from different stage) or a different one. |
| 3102 | */ |
| 3103 | string_to_uint_map *uniform_map = new string_to_uint_map; |
| 3104 | |
| 3105 | if (!uniform_map) { |
Andres Gomez | f9fc3ae | 2014-11-18 08:43:35 -0700 | [diff] [blame] | 3106 | linker_error(prog, "Out of memory during linking.\n"); |
Tapani Pälli | eca9d16 | 2014-04-08 08:45:36 +0300 | [diff] [blame] | 3107 | return; |
| 3108 | } |
| 3109 | |
| 3110 | for (unsigned i = 0; i < MESA_SHADER_STAGES; i++) { |
| 3111 | struct gl_shader *sh = prog->_LinkedShaders[i]; |
| 3112 | |
| 3113 | if (!sh) |
| 3114 | continue; |
| 3115 | |
Matt Turner | 4d78446 | 2014-06-24 21:34:05 -0700 | [diff] [blame] | 3116 | foreach_in_list(ir_instruction, node, sh->ir) { |
| 3117 | ir_variable *var = node->as_variable(); |
Kristian Høgsberg | a78a589 | 2015-05-13 11:17:23 +0200 | [diff] [blame] | 3118 | if (var && (var->data.mode == ir_var_uniform || var->data.mode == ir_var_shader_storage) && |
Tapani Pälli | eca9d16 | 2014-04-08 08:45:36 +0300 | [diff] [blame] | 3119 | var->data.explicit_location) { |
Dave Airlie | 6026686 | 2015-04-20 10:27:36 +1000 | [diff] [blame] | 3120 | bool ret; |
| 3121 | if (var->type->is_subroutine()) |
| 3122 | ret = reserve_subroutine_explicit_locations(prog, sh, var); |
| 3123 | else |
| 3124 | ret = reserve_explicit_locations(prog, uniform_map, var); |
| 3125 | if (!ret) { |
Dave Airlie | 2d5d1f5 | 2014-09-02 09:54:36 +1000 | [diff] [blame] | 3126 | delete uniform_map; |
Tapani Pälli | eca9d16 | 2014-04-08 08:45:36 +0300 | [diff] [blame] | 3127 | return; |
Dave Airlie | 2d5d1f5 | 2014-09-02 09:54:36 +1000 | [diff] [blame] | 3128 | } |
Tapani Pälli | eca9d16 | 2014-04-08 08:45:36 +0300 | [diff] [blame] | 3129 | } |
| 3130 | } |
| 3131 | } |
| 3132 | |
| 3133 | delete uniform_map; |
| 3134 | } |
| 3135 | |
Tapani Pälli | c796ce4 | 2015-03-06 09:14:49 +0200 | [diff] [blame] | 3136 | static bool |
Samuel Iglesias Gonsalvez | f24e5e6 | 2015-09-16 15:47:34 +0200 | [diff] [blame] | 3137 | should_add_buffer_variable(struct gl_shader_program *shProg, |
| 3138 | GLenum type, const char *name) |
| 3139 | { |
| 3140 | bool found_interface = false; |
| 3141 | const char *block_name = NULL; |
| 3142 | |
| 3143 | /* These rules only apply to buffer variables. So we return |
| 3144 | * true for the rest of types. |
| 3145 | */ |
| 3146 | if (type != GL_BUFFER_VARIABLE) |
| 3147 | return true; |
| 3148 | |
| 3149 | for (unsigned i = 0; i < shProg->NumBufferInterfaceBlocks; i++) { |
| 3150 | block_name = shProg->UniformBlocks[i].Name; |
| 3151 | if (strncmp(block_name, name, strlen(block_name)) == 0) { |
| 3152 | found_interface = true; |
| 3153 | break; |
| 3154 | } |
| 3155 | } |
| 3156 | |
| 3157 | /* We remove the interface name from the buffer variable name, |
| 3158 | * including the dot that follows it. |
| 3159 | */ |
| 3160 | if (found_interface) |
| 3161 | name = name + strlen(block_name) + 1; |
| 3162 | |
| 3163 | /* From: ARB_program_interface_query extension: |
| 3164 | * |
| 3165 | * "For an active shader storage block member declared as an array, an |
| 3166 | * entry will be generated only for the first array element, regardless |
| 3167 | * of its type. For arrays of aggregate types, the enumeration rules are |
| 3168 | * applied recursively for the single enumerated array element. |
| 3169 | */ |
| 3170 | const char *first_dot = strchr(name, '.'); |
| 3171 | const char *first_square_bracket = strchr(name, '['); |
| 3172 | |
| 3173 | /* The buffer variable is on top level and it is not an array */ |
| 3174 | if (!first_square_bracket) { |
| 3175 | return true; |
| 3176 | /* The shader storage block member is a struct, then generate the entry */ |
| 3177 | } else if (first_dot && first_dot < first_square_bracket) { |
| 3178 | return true; |
| 3179 | } else { |
| 3180 | /* Shader storage block member is an array, only generate an entry for the |
| 3181 | * first array element. |
| 3182 | */ |
| 3183 | if (strncmp(first_square_bracket, "[0]", 3) == 0) |
| 3184 | return true; |
| 3185 | } |
| 3186 | |
| 3187 | return false; |
| 3188 | } |
| 3189 | |
| 3190 | static bool |
Tapani Pälli | c796ce4 | 2015-03-06 09:14:49 +0200 | [diff] [blame] | 3191 | add_program_resource(struct gl_shader_program *prog, GLenum type, |
| 3192 | const void *data, uint8_t stages) |
| 3193 | { |
| 3194 | assert(data); |
| 3195 | |
| 3196 | /* If resource already exists, do not add it again. */ |
| 3197 | for (unsigned i = 0; i < prog->NumProgramResourceList; i++) |
| 3198 | if (prog->ProgramResourceList[i].Data == data) |
| 3199 | return true; |
| 3200 | |
| 3201 | prog->ProgramResourceList = |
| 3202 | reralloc(prog, |
| 3203 | prog->ProgramResourceList, |
| 3204 | gl_program_resource, |
| 3205 | prog->NumProgramResourceList + 1); |
| 3206 | |
| 3207 | if (!prog->ProgramResourceList) { |
| 3208 | linker_error(prog, "Out of memory during linking.\n"); |
| 3209 | return false; |
| 3210 | } |
| 3211 | |
| 3212 | struct gl_program_resource *res = |
| 3213 | &prog->ProgramResourceList[prog->NumProgramResourceList]; |
| 3214 | |
| 3215 | res->Type = type; |
| 3216 | res->Data = data; |
| 3217 | res->StageReferences = stages; |
| 3218 | |
| 3219 | prog->NumProgramResourceList++; |
| 3220 | |
| 3221 | return true; |
| 3222 | } |
| 3223 | |
Tapani Pälli | 4639cea | 2015-09-04 11:30:34 +0300 | [diff] [blame] | 3224 | /* Function checks if a variable var is a packed varying and |
| 3225 | * if given name is part of packed varying's list. |
| 3226 | * |
| 3227 | * If a variable is a packed varying, it has a name like |
| 3228 | * 'packed:a,b,c' where a, b and c are separate variables. |
| 3229 | */ |
| 3230 | static bool |
| 3231 | included_in_packed_varying(ir_variable *var, const char *name) |
| 3232 | { |
| 3233 | if (strncmp(var->name, "packed:", 7) != 0) |
| 3234 | return false; |
| 3235 | |
| 3236 | char *list = strdup(var->name + 7); |
| 3237 | assert(list); |
| 3238 | |
| 3239 | bool found = false; |
| 3240 | char *saveptr; |
| 3241 | char *token = strtok_r(list, ",", &saveptr); |
| 3242 | while (token) { |
| 3243 | if (strcmp(token, name) == 0) { |
| 3244 | found = true; |
| 3245 | break; |
| 3246 | } |
| 3247 | token = strtok_r(NULL, ",", &saveptr); |
| 3248 | } |
| 3249 | free(list); |
| 3250 | return found; |
| 3251 | } |
| 3252 | |
Tapani Pälli | c796ce4 | 2015-03-06 09:14:49 +0200 | [diff] [blame] | 3253 | /** |
| 3254 | * Function builds a stage reference bitmask from variable name. |
| 3255 | */ |
| 3256 | static uint8_t |
Tapani Pälli | 18c5cdb | 2015-08-03 08:48:32 +0300 | [diff] [blame] | 3257 | build_stageref(struct gl_shader_program *shProg, const char *name, |
| 3258 | unsigned mode) |
Tapani Pälli | c796ce4 | 2015-03-06 09:14:49 +0200 | [diff] [blame] | 3259 | { |
| 3260 | uint8_t stages = 0; |
| 3261 | |
| 3262 | /* Note, that we assume MAX 8 stages, if there will be more stages, type |
| 3263 | * used for reference mask in gl_program_resource will need to be changed. |
| 3264 | */ |
| 3265 | assert(MESA_SHADER_STAGES < 8); |
| 3266 | |
| 3267 | for (unsigned i = 0; i < MESA_SHADER_STAGES; i++) { |
| 3268 | struct gl_shader *sh = shProg->_LinkedShaders[i]; |
| 3269 | if (!sh) |
| 3270 | continue; |
Tapani Pälli | ccaf37f | 2015-06-29 14:19:00 +0300 | [diff] [blame] | 3271 | |
| 3272 | /* Shader symbol table may contain variables that have |
| 3273 | * been optimized away. Search IR for the variable instead. |
| 3274 | */ |
| 3275 | foreach_in_list(ir_instruction, node, sh->ir) { |
| 3276 | ir_variable *var = node->as_variable(); |
Timothy Arceri | 75a96ce | 2015-07-04 15:43:15 +1000 | [diff] [blame] | 3277 | if (var) { |
| 3278 | unsigned baselen = strlen(var->name); |
Tapani Pälli | 18c5cdb | 2015-08-03 08:48:32 +0300 | [diff] [blame] | 3279 | |
Tapani Pälli | 4639cea | 2015-09-04 11:30:34 +0300 | [diff] [blame] | 3280 | if (included_in_packed_varying(var, name)) { |
| 3281 | stages |= (1 << i); |
| 3282 | break; |
| 3283 | } |
| 3284 | |
Tapani Pälli | 18c5cdb | 2015-08-03 08:48:32 +0300 | [diff] [blame] | 3285 | /* Type needs to match if specified, otherwise we might |
| 3286 | * pick a variable with same name but different interface. |
| 3287 | */ |
Timothy Arceri | 42d283a | 2015-08-05 21:05:52 +1000 | [diff] [blame] | 3288 | if (var->data.mode != mode) |
Tapani Pälli | 18c5cdb | 2015-08-03 08:48:32 +0300 | [diff] [blame] | 3289 | continue; |
| 3290 | |
Timothy Arceri | 75a96ce | 2015-07-04 15:43:15 +1000 | [diff] [blame] | 3291 | if (strncmp(var->name, name, baselen) == 0) { |
| 3292 | /* Check for exact name matches but also check for arrays and |
| 3293 | * structs. |
| 3294 | */ |
| 3295 | if (name[baselen] == '\0' || |
| 3296 | name[baselen] == '[' || |
| 3297 | name[baselen] == '.') { |
| 3298 | stages |= (1 << i); |
| 3299 | break; |
| 3300 | } |
| 3301 | } |
Tapani Pälli | ccaf37f | 2015-06-29 14:19:00 +0300 | [diff] [blame] | 3302 | } |
| 3303 | } |
Tapani Pälli | c796ce4 | 2015-03-06 09:14:49 +0200 | [diff] [blame] | 3304 | } |
| 3305 | return stages; |
| 3306 | } |
| 3307 | |
| 3308 | static bool |
| 3309 | add_interface_variables(struct gl_shader_program *shProg, |
Tapani Pälli | 4639cea | 2015-09-04 11:30:34 +0300 | [diff] [blame] | 3310 | exec_list *ir, GLenum programInterface) |
Tapani Pälli | c796ce4 | 2015-03-06 09:14:49 +0200 | [diff] [blame] | 3311 | { |
Tapani Pälli | 4639cea | 2015-09-04 11:30:34 +0300 | [diff] [blame] | 3312 | foreach_in_list(ir_instruction, node, ir) { |
Tapani Pälli | c796ce4 | 2015-03-06 09:14:49 +0200 | [diff] [blame] | 3313 | ir_variable *var = node->as_variable(); |
Tapani Pälli | 3706e5d | 2015-04-30 09:27:00 +0300 | [diff] [blame] | 3314 | uint8_t mask = 0; |
Tapani Pälli | c796ce4 | 2015-03-06 09:14:49 +0200 | [diff] [blame] | 3315 | |
| 3316 | if (!var) |
| 3317 | continue; |
| 3318 | |
| 3319 | switch (var->data.mode) { |
| 3320 | /* From GL 4.3 core spec, section 11.1.1 (Vertex Attributes): |
| 3321 | * "For GetActiveAttrib, all active vertex shader input variables |
| 3322 | * are enumerated, including the special built-in inputs gl_VertexID |
| 3323 | * and gl_InstanceID." |
| 3324 | */ |
| 3325 | case ir_var_system_value: |
| 3326 | if (var->data.location != SYSTEM_VALUE_VERTEX_ID && |
| 3327 | var->data.location != SYSTEM_VALUE_VERTEX_ID_ZERO_BASE && |
| 3328 | var->data.location != SYSTEM_VALUE_INSTANCE_ID) |
Tapani Pälli | 5917ca3 | 2015-04-21 08:25:16 +0300 | [diff] [blame] | 3329 | continue; |
Tapani Pälli | 3706e5d | 2015-04-30 09:27:00 +0300 | [diff] [blame] | 3330 | /* Mark special built-in inputs referenced by the vertex stage so |
| 3331 | * that they are considered active by the shader queries. |
| 3332 | */ |
| 3333 | mask = (1 << (MESA_SHADER_VERTEX)); |
Tapani Pälli | ed10f9c | 2015-04-21 20:11:43 +0300 | [diff] [blame] | 3334 | /* FALLTHROUGH */ |
Tapani Pälli | c796ce4 | 2015-03-06 09:14:49 +0200 | [diff] [blame] | 3335 | case ir_var_shader_in: |
Jose Fonseca | 037e0e7 | 2015-04-16 10:19:57 +0100 | [diff] [blame] | 3336 | if (programInterface != GL_PROGRAM_INPUT) |
Tapani Pälli | c796ce4 | 2015-03-06 09:14:49 +0200 | [diff] [blame] | 3337 | continue; |
| 3338 | break; |
| 3339 | case ir_var_shader_out: |
Jose Fonseca | 037e0e7 | 2015-04-16 10:19:57 +0100 | [diff] [blame] | 3340 | if (programInterface != GL_PROGRAM_OUTPUT) |
Tapani Pälli | c796ce4 | 2015-03-06 09:14:49 +0200 | [diff] [blame] | 3341 | continue; |
| 3342 | break; |
| 3343 | default: |
| 3344 | continue; |
| 3345 | }; |
| 3346 | |
Tapani Pälli | 4639cea | 2015-09-04 11:30:34 +0300 | [diff] [blame] | 3347 | /* Skip packed varyings, packed varyings are handled separately |
| 3348 | * by add_packed_varyings. |
| 3349 | */ |
| 3350 | if (strncmp(var->name, "packed:", 7) == 0) |
| 3351 | continue; |
| 3352 | |
Kenneth Graunke | 6218c68 | 2015-06-28 22:17:16 -0700 | [diff] [blame] | 3353 | if (!add_program_resource(shProg, programInterface, var, |
Tapani Pälli | 18c5cdb | 2015-08-03 08:48:32 +0300 | [diff] [blame] | 3354 | build_stageref(shProg, var->name, |
| 3355 | var->data.mode) | mask)) |
Tapani Pälli | c796ce4 | 2015-03-06 09:14:49 +0200 | [diff] [blame] | 3356 | return false; |
| 3357 | } |
| 3358 | return true; |
| 3359 | } |
| 3360 | |
Tapani Pälli | 4639cea | 2015-09-04 11:30:34 +0300 | [diff] [blame] | 3361 | static bool |
| 3362 | add_packed_varyings(struct gl_shader_program *shProg, int stage) |
| 3363 | { |
| 3364 | struct gl_shader *sh = shProg->_LinkedShaders[stage]; |
| 3365 | GLenum iface; |
| 3366 | |
| 3367 | if (!sh || !sh->packed_varyings) |
| 3368 | return true; |
| 3369 | |
| 3370 | foreach_in_list(ir_instruction, node, sh->packed_varyings) { |
| 3371 | ir_variable *var = node->as_variable(); |
| 3372 | if (var) { |
| 3373 | switch (var->data.mode) { |
| 3374 | case ir_var_shader_in: |
| 3375 | iface = GL_PROGRAM_INPUT; |
Tapani Pälli | 266d05a | 2015-09-25 09:56:39 +0300 | [diff] [blame] | 3376 | break; |
Tapani Pälli | 4639cea | 2015-09-04 11:30:34 +0300 | [diff] [blame] | 3377 | case ir_var_shader_out: |
| 3378 | iface = GL_PROGRAM_OUTPUT; |
Tapani Pälli | 266d05a | 2015-09-25 09:56:39 +0300 | [diff] [blame] | 3379 | break; |
| 3380 | default: |
| 3381 | unreachable("unexpected type"); |
Tapani Pälli | 4639cea | 2015-09-04 11:30:34 +0300 | [diff] [blame] | 3382 | } |
| 3383 | if (!add_program_resource(shProg, iface, var, |
| 3384 | build_stageref(shProg, var->name, |
| 3385 | var->data.mode))) |
| 3386 | return false; |
| 3387 | } |
| 3388 | } |
| 3389 | return true; |
| 3390 | } |
| 3391 | |
Tapani Pälli | c796ce4 | 2015-03-06 09:14:49 +0200 | [diff] [blame] | 3392 | /** |
| 3393 | * Builds up a list of program resources that point to existing |
| 3394 | * resource data. |
| 3395 | */ |
Tapani Pälli | 73afa31 | 2015-06-29 14:39:05 +0300 | [diff] [blame] | 3396 | void |
Ian Romanick | bd0245b | 2015-08-26 13:38:49 +0100 | [diff] [blame] | 3397 | build_program_resource_list(struct gl_shader_program *shProg) |
Tapani Pälli | c796ce4 | 2015-03-06 09:14:49 +0200 | [diff] [blame] | 3398 | { |
| 3399 | /* Rebuild resource list. */ |
| 3400 | if (shProg->ProgramResourceList) { |
| 3401 | ralloc_free(shProg->ProgramResourceList); |
| 3402 | shProg->ProgramResourceList = NULL; |
| 3403 | shProg->NumProgramResourceList = 0; |
| 3404 | } |
| 3405 | |
| 3406 | int input_stage = MESA_SHADER_STAGES, output_stage = 0; |
| 3407 | |
| 3408 | /* Determine first input and final output stage. These are used to |
| 3409 | * detect which variables should be enumerated in the resource list |
| 3410 | * for GL_PROGRAM_INPUT and GL_PROGRAM_OUTPUT. |
| 3411 | */ |
| 3412 | for (unsigned i = 0; i < MESA_SHADER_STAGES; i++) { |
| 3413 | if (!shProg->_LinkedShaders[i]) |
| 3414 | continue; |
| 3415 | if (input_stage == MESA_SHADER_STAGES) |
| 3416 | input_stage = i; |
| 3417 | output_stage = i; |
| 3418 | } |
| 3419 | |
| 3420 | /* Empty shader, no resources. */ |
| 3421 | if (input_stage == MESA_SHADER_STAGES && output_stage == 0) |
| 3422 | return; |
| 3423 | |
Tapani Pälli | 4639cea | 2015-09-04 11:30:34 +0300 | [diff] [blame] | 3424 | if (!add_packed_varyings(shProg, input_stage)) |
| 3425 | return; |
| 3426 | if (!add_packed_varyings(shProg, output_stage)) |
| 3427 | return; |
| 3428 | |
Tapani Pälli | c796ce4 | 2015-03-06 09:14:49 +0200 | [diff] [blame] | 3429 | /* Add inputs and outputs to the resource list. */ |
Tapani Pälli | 4639cea | 2015-09-04 11:30:34 +0300 | [diff] [blame] | 3430 | if (!add_interface_variables(shProg, shProg->_LinkedShaders[input_stage]->ir, |
Tapani Pälli | c796ce4 | 2015-03-06 09:14:49 +0200 | [diff] [blame] | 3431 | GL_PROGRAM_INPUT)) |
| 3432 | return; |
| 3433 | |
Tapani Pälli | 4639cea | 2015-09-04 11:30:34 +0300 | [diff] [blame] | 3434 | if (!add_interface_variables(shProg, shProg->_LinkedShaders[output_stage]->ir, |
Tapani Pälli | c796ce4 | 2015-03-06 09:14:49 +0200 | [diff] [blame] | 3435 | GL_PROGRAM_OUTPUT)) |
| 3436 | return; |
| 3437 | |
| 3438 | /* Add transform feedback varyings. */ |
| 3439 | if (shProg->LinkedTransformFeedback.NumVarying > 0) { |
| 3440 | for (int i = 0; i < shProg->LinkedTransformFeedback.NumVarying; i++) { |
Tapani Pälli | c796ce4 | 2015-03-06 09:14:49 +0200 | [diff] [blame] | 3441 | if (!add_program_resource(shProg, GL_TRANSFORM_FEEDBACK_VARYING, |
| 3442 | &shProg->LinkedTransformFeedback.Varyings[i], |
Timothy Arceri | 42d283a | 2015-08-05 21:05:52 +1000 | [diff] [blame] | 3443 | 0)) |
Tapani Pälli | c796ce4 | 2015-03-06 09:14:49 +0200 | [diff] [blame] | 3444 | return; |
| 3445 | } |
| 3446 | } |
| 3447 | |
| 3448 | /* Add uniforms from uniform storage. */ |
Martin Peres | 87a4bc5 | 2015-05-21 15:51:09 +0300 | [diff] [blame] | 3449 | for (unsigned i = 0; i < shProg->NumUniformStorage; i++) { |
Tapani Pälli | c796ce4 | 2015-03-06 09:14:49 +0200 | [diff] [blame] | 3450 | /* Do not add uniforms internally used by Mesa. */ |
| 3451 | if (shProg->UniformStorage[i].hidden) |
| 3452 | continue; |
| 3453 | |
| 3454 | uint8_t stageref = |
Timothy Arceri | 42d283a | 2015-08-05 21:05:52 +1000 | [diff] [blame] | 3455 | build_stageref(shProg, shProg->UniformStorage[i].name, |
| 3456 | ir_var_uniform); |
Tapani Pälli | 9f4eaba | 2015-05-11 13:24:20 +0300 | [diff] [blame] | 3457 | |
| 3458 | /* Add stagereferences for uniforms in a uniform block. */ |
| 3459 | int block_index = shProg->UniformStorage[i].block_index; |
| 3460 | if (block_index != -1) { |
| 3461 | for (unsigned j = 0; j < MESA_SHADER_STAGES; j++) { |
| 3462 | if (shProg->UniformBlockStageIndex[j][block_index] != -1) |
| 3463 | stageref |= (1 << j); |
| 3464 | } |
| 3465 | } |
| 3466 | |
Samuel Iglesias Gonsalvez | 9b477ad | 2015-05-06 08:11:02 +0200 | [diff] [blame] | 3467 | bool is_shader_storage = shProg->UniformStorage[i].is_shader_storage; |
| 3468 | GLenum type = is_shader_storage ? GL_BUFFER_VARIABLE : GL_UNIFORM; |
Samuel Iglesias Gonsalvez | f24e5e6 | 2015-09-16 15:47:34 +0200 | [diff] [blame] | 3469 | if (!should_add_buffer_variable(shProg, type, |
| 3470 | shProg->UniformStorage[i].name)) |
| 3471 | continue; |
| 3472 | |
Samuel Iglesias Gonsalvez | 9b477ad | 2015-05-06 08:11:02 +0200 | [diff] [blame] | 3473 | if (!add_program_resource(shProg, type, |
Tapani Pälli | c796ce4 | 2015-03-06 09:14:49 +0200 | [diff] [blame] | 3474 | &shProg->UniformStorage[i], stageref)) |
| 3475 | return; |
| 3476 | } |
| 3477 | |
Samuel Iglesias Gonsalvez | 9b477ad | 2015-05-06 08:11:02 +0200 | [diff] [blame] | 3478 | /* Add program uniform blocks and shader storage blocks. */ |
Samuel Iglesias Gonsalvez | 6668eb5 | 2015-09-11 12:29:37 +0200 | [diff] [blame] | 3479 | for (unsigned i = 0; i < shProg->NumBufferInterfaceBlocks; i++) { |
Samuel Iglesias Gonsalvez | 9b477ad | 2015-05-06 08:11:02 +0200 | [diff] [blame] | 3480 | bool is_shader_storage = shProg->UniformBlocks[i].IsShaderStorage; |
| 3481 | GLenum type = is_shader_storage ? GL_SHADER_STORAGE_BLOCK : GL_UNIFORM_BLOCK; |
| 3482 | if (!add_program_resource(shProg, type, |
Tapani Pälli | c796ce4 | 2015-03-06 09:14:49 +0200 | [diff] [blame] | 3483 | &shProg->UniformBlocks[i], 0)) |
| 3484 | return; |
| 3485 | } |
| 3486 | |
| 3487 | /* Add atomic counter buffers. */ |
| 3488 | for (unsigned i = 0; i < shProg->NumAtomicBuffers; i++) { |
| 3489 | if (!add_program_resource(shProg, GL_ATOMIC_COUNTER_BUFFER, |
| 3490 | &shProg->AtomicBuffers[i], 0)) |
| 3491 | return; |
| 3492 | } |
| 3493 | |
Dave Airlie | 6026686 | 2015-04-20 10:27:36 +1000 | [diff] [blame] | 3494 | for (unsigned i = 0; i < shProg->NumUniformStorage; i++) { |
| 3495 | GLenum type; |
| 3496 | if (!shProg->UniformStorage[i].hidden) |
| 3497 | continue; |
| 3498 | |
| 3499 | for (int j = MESA_SHADER_VERTEX; j < MESA_SHADER_STAGES; j++) { |
Timothy Arceri | 763cd8c | 2015-09-30 11:00:02 +1000 | [diff] [blame^] | 3500 | if (!shProg->UniformStorage[i].opaque[j].active) |
Dave Airlie | 6026686 | 2015-04-20 10:27:36 +1000 | [diff] [blame] | 3501 | continue; |
| 3502 | |
| 3503 | type = _mesa_shader_stage_to_subroutine_uniform((gl_shader_stage)j); |
| 3504 | /* add shader subroutines */ |
| 3505 | if (!add_program_resource(shProg, type, &shProg->UniformStorage[i], 0)) |
| 3506 | return; |
| 3507 | } |
| 3508 | } |
| 3509 | |
| 3510 | for (unsigned i = 0; i < MESA_SHADER_STAGES; i++) { |
| 3511 | struct gl_shader *sh = shProg->_LinkedShaders[i]; |
| 3512 | GLuint type; |
| 3513 | |
| 3514 | if (!sh) |
| 3515 | continue; |
| 3516 | |
| 3517 | type = _mesa_shader_stage_to_subroutine((gl_shader_stage)i); |
| 3518 | for (unsigned j = 0; j < sh->NumSubroutineFunctions; j++) { |
| 3519 | if (!add_program_resource(shProg, type, &sh->SubroutineFunctions[j], 0)) |
| 3520 | return; |
| 3521 | } |
| 3522 | } |
| 3523 | |
Tapani Pälli | c796ce4 | 2015-03-06 09:14:49 +0200 | [diff] [blame] | 3524 | /* TODO - following extensions will require more resource types: |
| 3525 | * |
| 3526 | * GL_ARB_shader_storage_buffer_object |
Tapani Pälli | c796ce4 | 2015-03-06 09:14:49 +0200 | [diff] [blame] | 3527 | */ |
| 3528 | } |
| 3529 | |
Tapani Pälli | 9350ea6 | 2015-05-19 15:01:49 +0300 | [diff] [blame] | 3530 | /** |
| 3531 | * This check is done to make sure we allow only constant expression |
| 3532 | * indexing and "constant-index-expression" (indexing with an expression |
| 3533 | * that includes loop induction variable). |
| 3534 | */ |
| 3535 | static bool |
| 3536 | validate_sampler_array_indexing(struct gl_context *ctx, |
| 3537 | struct gl_shader_program *prog) |
| 3538 | { |
| 3539 | dynamic_sampler_array_indexing_visitor v; |
| 3540 | for (unsigned i = 0; i < MESA_SHADER_STAGES; i++) { |
| 3541 | if (prog->_LinkedShaders[i] == NULL) |
| 3542 | continue; |
| 3543 | |
| 3544 | bool no_dynamic_indexing = |
| 3545 | ctx->Const.ShaderCompilerOptions[i].EmitNoIndirectSampler; |
| 3546 | |
| 3547 | /* Search for array derefs in shader. */ |
| 3548 | v.run(prog->_LinkedShaders[i]->ir); |
| 3549 | if (v.uses_dynamic_sampler_array_indexing()) { |
| 3550 | const char *msg = "sampler arrays indexed with non-constant " |
| 3551 | "expressions is forbidden in GLSL %s %u"; |
| 3552 | /* Backend has indicated that it has no dynamic indexing support. */ |
| 3553 | if (no_dynamic_indexing) { |
| 3554 | linker_error(prog, msg, prog->IsES ? "ES" : "", prog->Version); |
| 3555 | return false; |
| 3556 | } else { |
| 3557 | linker_warning(prog, msg, prog->IsES ? "ES" : "", prog->Version); |
| 3558 | } |
| 3559 | } |
| 3560 | } |
| 3561 | return true; |
| 3562 | } |
| 3563 | |
Ian Romanick | 4ff9e59 | 2015-08-19 13:36:22 -0700 | [diff] [blame] | 3564 | static void |
| 3565 | link_assign_subroutine_types(struct gl_shader_program *prog) |
Dave Airlie | 6026686 | 2015-04-20 10:27:36 +1000 | [diff] [blame] | 3566 | { |
| 3567 | for (unsigned i = 0; i < MESA_SHADER_STAGES; i++) { |
| 3568 | gl_shader *sh = prog->_LinkedShaders[i]; |
| 3569 | |
| 3570 | if (sh == NULL) |
| 3571 | continue; |
| 3572 | |
| 3573 | foreach_in_list(ir_instruction, node, sh->ir) { |
| 3574 | ir_function *fn = node->as_function(); |
| 3575 | if (!fn) |
| 3576 | continue; |
| 3577 | |
| 3578 | if (fn->is_subroutine) |
| 3579 | sh->NumSubroutineUniformTypes++; |
| 3580 | |
| 3581 | if (!fn->num_subroutine_types) |
| 3582 | continue; |
| 3583 | |
| 3584 | sh->SubroutineFunctions = reralloc(sh, sh->SubroutineFunctions, |
| 3585 | struct gl_subroutine_function, |
| 3586 | sh->NumSubroutineFunctions + 1); |
| 3587 | sh->SubroutineFunctions[sh->NumSubroutineFunctions].name = ralloc_strdup(sh, fn->name); |
| 3588 | sh->SubroutineFunctions[sh->NumSubroutineFunctions].num_compat_types = fn->num_subroutine_types; |
| 3589 | sh->SubroutineFunctions[sh->NumSubroutineFunctions].types = |
| 3590 | ralloc_array(sh, const struct glsl_type *, |
| 3591 | fn->num_subroutine_types); |
| 3592 | for (int j = 0; j < fn->num_subroutine_types; j++) |
| 3593 | sh->SubroutineFunctions[sh->NumSubroutineFunctions].types[j] = fn->subroutine_types[j]; |
| 3594 | sh->NumSubroutineFunctions++; |
| 3595 | } |
| 3596 | } |
| 3597 | } |
Tapani Pälli | c796ce4 | 2015-03-06 09:14:49 +0200 | [diff] [blame] | 3598 | |
Ian Romanick | 0e59b26 | 2010-06-23 11:23:01 -0700 | [diff] [blame] | 3599 | void |
Kristian Høgsberg | f9995b3 | 2010-10-12 12:26:10 -0400 | [diff] [blame] | 3600 | link_shaders(struct gl_context *ctx, struct gl_shader_program *prog) |
Ian Romanick | 832dfa5 | 2010-06-17 15:04:20 -0700 | [diff] [blame] | 3601 | { |
Paul Berry | 871ddb9 | 2011-11-05 11:17:32 -0700 | [diff] [blame] | 3602 | tfeedback_decl *tfeedback_decls = NULL; |
| 3603 | unsigned num_tfeedback_decls = prog->TransformFeedback.NumVarying; |
| 3604 | |
Kenneth Graunke | d3073f5 | 2011-01-21 14:32:31 -0800 | [diff] [blame] | 3605 | void *mem_ctx = ralloc_context(NULL); // temporary linker context |
Kenneth Graunke | 2da02e7 | 2010-11-17 11:03:57 -0800 | [diff] [blame] | 3606 | |
Paul Berry | b95d237 | 2013-07-27 11:08:31 -0700 | [diff] [blame] | 3607 | prog->LinkStatus = true; /* All error paths will set this to false */ |
Ian Romanick | 832dfa5 | 2010-06-17 15:04:20 -0700 | [diff] [blame] | 3608 | prog->Validated = false; |
| 3609 | prog->_Used = false; |
| 3610 | |
Anuj Phogat | 9bcb0a8 | 2014-02-10 14:12:40 -0800 | [diff] [blame] | 3611 | prog->ARB_fragment_coord_conventions_enable = false; |
Francisco Jerez | 5c11493 | 2013-09-11 12:14:46 -0700 | [diff] [blame] | 3612 | |
Ian Romanick | 832dfa5 | 2010-06-17 15:04:20 -0700 | [diff] [blame] | 3613 | /* Separate the shaders into groups based on their type. |
| 3614 | */ |
Paul Berry | cd18ba1 | 2014-01-07 08:56:57 -0800 | [diff] [blame] | 3615 | struct gl_shader **shader_list[MESA_SHADER_STAGES]; |
| 3616 | unsigned num_shaders[MESA_SHADER_STAGES]; |
Ian Romanick | 832dfa5 | 2010-06-17 15:04:20 -0700 | [diff] [blame] | 3617 | |
Paul Berry | cd18ba1 | 2014-01-07 08:56:57 -0800 | [diff] [blame] | 3618 | for (int i = 0; i < MESA_SHADER_STAGES; i++) { |
| 3619 | shader_list[i] = (struct gl_shader **) |
| 3620 | calloc(prog->NumShaders, sizeof(struct gl_shader *)); |
| 3621 | num_shaders[i] = 0; |
| 3622 | } |
Ian Romanick | 832dfa5 | 2010-06-17 15:04:20 -0700 | [diff] [blame] | 3623 | |
Ian Romanick | 25f51d3 | 2010-07-16 15:51:50 -0700 | [diff] [blame] | 3624 | unsigned min_version = UINT_MAX; |
| 3625 | unsigned max_version = 0; |
Paul Berry | a9f34dc | 2012-08-02 17:49:44 -0700 | [diff] [blame] | 3626 | const bool is_es_prog = |
| 3627 | (prog->NumShaders > 0 && prog->Shaders[0]->IsES) ? true : false; |
Ian Romanick | 832dfa5 | 2010-06-17 15:04:20 -0700 | [diff] [blame] | 3628 | for (unsigned i = 0; i < prog->NumShaders; i++) { |
Ian Romanick | 25f51d3 | 2010-07-16 15:51:50 -0700 | [diff] [blame] | 3629 | min_version = MIN2(min_version, prog->Shaders[i]->Version); |
| 3630 | max_version = MAX2(max_version, prog->Shaders[i]->Version); |
| 3631 | |
Paul Berry | a9f34dc | 2012-08-02 17:49:44 -0700 | [diff] [blame] | 3632 | if (prog->Shaders[i]->IsES != is_es_prog) { |
| 3633 | linker_error(prog, "all shaders must use same shading " |
| 3634 | "language version\n"); |
| 3635 | goto done; |
| 3636 | } |
| 3637 | |
Jose Fonseca | d01a7cda | 2015-03-18 14:21:15 +0000 | [diff] [blame] | 3638 | if (prog->Shaders[i]->ARB_fragment_coord_conventions_enable) { |
| 3639 | prog->ARB_fragment_coord_conventions_enable = true; |
| 3640 | } |
Anuj Phogat | 9bcb0a8 | 2014-02-10 14:12:40 -0800 | [diff] [blame] | 3641 | |
Paul Berry | cd18ba1 | 2014-01-07 08:56:57 -0800 | [diff] [blame] | 3642 | gl_shader_stage shader_type = prog->Shaders[i]->Stage; |
| 3643 | shader_list[shader_type][num_shaders[shader_type]] = prog->Shaders[i]; |
| 3644 | num_shaders[shader_type]++; |
Ian Romanick | 832dfa5 | 2010-06-17 15:04:20 -0700 | [diff] [blame] | 3645 | } |
| 3646 | |
Paul Berry | 672fab0 | 2013-10-13 18:01:11 -0700 | [diff] [blame] | 3647 | /* In desktop GLSL, different shader versions may be linked together. In |
| 3648 | * GLSL ES, all shader versions must be the same. |
Ian Romanick | 25f51d3 | 2010-07-16 15:51:50 -0700 | [diff] [blame] | 3649 | */ |
Paul Berry | 672fab0 | 2013-10-13 18:01:11 -0700 | [diff] [blame] | 3650 | if (is_es_prog && min_version != max_version) { |
Ian Romanick | 586e741 | 2011-07-28 14:04:09 -0700 | [diff] [blame] | 3651 | linker_error(prog, "all shaders must use same shading " |
| 3652 | "language version\n"); |
Ian Romanick | 25f51d3 | 2010-07-16 15:51:50 -0700 | [diff] [blame] | 3653 | goto done; |
| 3654 | } |
| 3655 | |
| 3656 | prog->Version = max_version; |
Paul Berry | 91c92bb | 2012-08-02 17:50:43 -0700 | [diff] [blame] | 3657 | prog->IsES = is_es_prog; |
Ian Romanick | 25f51d3 | 2010-07-16 15:51:50 -0700 | [diff] [blame] | 3658 | |
Tapani Pälli | 6967895 | 2015-09-03 14:20:46 +0300 | [diff] [blame] | 3659 | /* From OpenGL 4.5 Core specification (7.3 Program Objects): |
| 3660 | * "Linking can fail for a variety of reasons as specified in the OpenGL |
| 3661 | * Shading Language Specification, as well as any of the following |
| 3662 | * reasons: |
| 3663 | * |
| 3664 | * * No shader objects are attached to program. |
| 3665 | * |
| 3666 | * ..." |
| 3667 | * |
| 3668 | * Same rule applies for OpenGL ES >= 3.1. |
| 3669 | */ |
| 3670 | |
| 3671 | if (prog->NumShaders == 0 && |
| 3672 | ((ctx->API == API_OPENGL_CORE && ctx->Version >= 45) || |
| 3673 | (ctx->API == API_OPENGLES2 && ctx->Version >= 31))) { |
| 3674 | linker_error(prog, "No shader objects are attached to program.\n"); |
| 3675 | goto done; |
| 3676 | } |
| 3677 | |
Chris Forbes | 7c758c5 | 2014-09-21 13:33:14 +1200 | [diff] [blame] | 3678 | /* Some shaders have to be linked with some other shaders present. |
Fabian Bieler | bd85ba0 | 2013-05-24 23:26:54 +0200 | [diff] [blame] | 3679 | */ |
Paul Berry | cd18ba1 | 2014-01-07 08:56:57 -0800 | [diff] [blame] | 3680 | if (num_shaders[MESA_SHADER_GEOMETRY] > 0 && |
Ian Romanick | c557eb7 | 2014-01-23 18:26:29 -0800 | [diff] [blame] | 3681 | num_shaders[MESA_SHADER_VERTEX] == 0 && |
| 3682 | !prog->SeparateShader) { |
Fabian Bieler | bd85ba0 | 2013-05-24 23:26:54 +0200 | [diff] [blame] | 3683 | linker_error(prog, "Geometry shader must be linked with " |
| 3684 | "vertex shader\n"); |
| 3685 | goto done; |
| 3686 | } |
Chris Forbes | 7c758c5 | 2014-09-21 13:33:14 +1200 | [diff] [blame] | 3687 | if (num_shaders[MESA_SHADER_TESS_EVAL] > 0 && |
| 3688 | num_shaders[MESA_SHADER_VERTEX] == 0 && |
| 3689 | !prog->SeparateShader) { |
| 3690 | linker_error(prog, "Tessellation evaluation shader must be linked with " |
| 3691 | "vertex shader\n"); |
| 3692 | goto done; |
| 3693 | } |
| 3694 | if (num_shaders[MESA_SHADER_TESS_CTRL] > 0 && |
| 3695 | num_shaders[MESA_SHADER_VERTEX] == 0 && |
| 3696 | !prog->SeparateShader) { |
| 3697 | linker_error(prog, "Tessellation control shader must be linked with " |
| 3698 | "vertex shader\n"); |
| 3699 | goto done; |
| 3700 | } |
| 3701 | |
| 3702 | /* The spec is self-contradictory here. It allows linking without a tess |
| 3703 | * eval shader, but that can only be used with transform feedback and |
| 3704 | * rasterization disabled. However, transform feedback isn't allowed |
| 3705 | * with GL_PATCHES, so it can't be used. |
| 3706 | * |
| 3707 | * More investigation showed that the idea of transform feedback after |
| 3708 | * a tess control shader was dropped, because some hw vendors couldn't |
| 3709 | * support tessellation without a tess eval shader, but the linker section |
| 3710 | * wasn't updated to reflect that. |
| 3711 | * |
| 3712 | * All specifications (ARB_tessellation_shader, GL 4.0-4.5) have this |
| 3713 | * spec bug. |
| 3714 | * |
| 3715 | * Do what's reasonable and always require a tess eval shader if a tess |
| 3716 | * control shader is present. |
| 3717 | */ |
| 3718 | if (num_shaders[MESA_SHADER_TESS_CTRL] > 0 && |
| 3719 | num_shaders[MESA_SHADER_TESS_EVAL] == 0 && |
| 3720 | !prog->SeparateShader) { |
| 3721 | linker_error(prog, "Tessellation control shader must be linked with " |
| 3722 | "tessellation evaluation shader\n"); |
| 3723 | goto done; |
| 3724 | } |
Fabian Bieler | bd85ba0 | 2013-05-24 23:26:54 +0200 | [diff] [blame] | 3725 | |
Paul Berry | 1fe274b | 2014-01-08 11:40:23 -0800 | [diff] [blame] | 3726 | /* Compute shaders have additional restrictions. */ |
| 3727 | if (num_shaders[MESA_SHADER_COMPUTE] > 0 && |
| 3728 | num_shaders[MESA_SHADER_COMPUTE] != prog->NumShaders) { |
| 3729 | linker_error(prog, "Compute shaders may not be linked with any other " |
| 3730 | "type of shader\n"); |
| 3731 | } |
| 3732 | |
Paul Berry | 665b8d7 | 2014-01-07 10:11:39 -0800 | [diff] [blame] | 3733 | for (unsigned int i = 0; i < MESA_SHADER_STAGES; i++) { |
Ian Romanick | 3322fba | 2010-10-14 13:28:42 -0700 | [diff] [blame] | 3734 | if (prog->_LinkedShaders[i] != NULL) |
Marek Olšák | 95e0303 | 2015-09-27 21:28:22 +0200 | [diff] [blame] | 3735 | _mesa_delete_shader(ctx, prog->_LinkedShaders[i]); |
Ian Romanick | 3322fba | 2010-10-14 13:28:42 -0700 | [diff] [blame] | 3736 | |
| 3737 | prog->_LinkedShaders[i] = NULL; |
Eric Anholt | 5d0f430 | 2010-08-18 12:02:35 -0700 | [diff] [blame] | 3738 | } |
| 3739 | |
Ian Romanick | cd6764e | 2010-07-16 16:00:07 -0700 | [diff] [blame] | 3740 | /* Link all shaders for a particular stage and validate the result. |
| 3741 | */ |
Paul Berry | cd18ba1 | 2014-01-07 08:56:57 -0800 | [diff] [blame] | 3742 | for (int stage = 0; stage < MESA_SHADER_STAGES; stage++) { |
| 3743 | if (num_shaders[stage] > 0) { |
| 3744 | gl_shader *const sh = |
| 3745 | link_intrastage_shaders(mem_ctx, ctx, prog, shader_list[stage], |
| 3746 | num_shaders[stage]); |
Ian Romanick | 3fb8787 | 2010-07-09 14:09:34 -0700 | [diff] [blame] | 3747 | |
Ilia Mirkin | 5646f0f | 2015-05-17 17:56:44 -0400 | [diff] [blame] | 3748 | if (!prog->LinkStatus) { |
| 3749 | if (sh) |
Marek Olšák | 95e0303 | 2015-09-27 21:28:22 +0200 | [diff] [blame] | 3750 | _mesa_delete_shader(ctx, sh); |
Paul Berry | cd18ba1 | 2014-01-07 08:56:57 -0800 | [diff] [blame] | 3751 | goto done; |
Ilia Mirkin | 5646f0f | 2015-05-17 17:56:44 -0400 | [diff] [blame] | 3752 | } |
Ian Romanick | 3fb8787 | 2010-07-09 14:09:34 -0700 | [diff] [blame] | 3753 | |
Paul Berry | cd18ba1 | 2014-01-07 08:56:57 -0800 | [diff] [blame] | 3754 | switch (stage) { |
| 3755 | case MESA_SHADER_VERTEX: |
| 3756 | validate_vertex_shader_executable(prog, sh); |
| 3757 | break; |
Chris Forbes | df16e0d | 2014-09-09 19:25:02 +1200 | [diff] [blame] | 3758 | case MESA_SHADER_TESS_CTRL: |
| 3759 | /* nothing to be done */ |
| 3760 | break; |
| 3761 | case MESA_SHADER_TESS_EVAL: |
| 3762 | validate_tess_eval_shader_executable(prog, sh); |
| 3763 | break; |
Paul Berry | cd18ba1 | 2014-01-07 08:56:57 -0800 | [diff] [blame] | 3764 | case MESA_SHADER_GEOMETRY: |
| 3765 | validate_geometry_shader_executable(prog, sh); |
| 3766 | break; |
| 3767 | case MESA_SHADER_FRAGMENT: |
| 3768 | validate_fragment_shader_executable(prog, sh); |
| 3769 | break; |
| 3770 | } |
Ilia Mirkin | 5646f0f | 2015-05-17 17:56:44 -0400 | [diff] [blame] | 3771 | if (!prog->LinkStatus) { |
| 3772 | if (sh) |
Marek Olšák | 95e0303 | 2015-09-27 21:28:22 +0200 | [diff] [blame] | 3773 | _mesa_delete_shader(ctx, sh); |
Paul Berry | cd18ba1 | 2014-01-07 08:56:57 -0800 | [diff] [blame] | 3774 | goto done; |
Ilia Mirkin | 5646f0f | 2015-05-17 17:56:44 -0400 | [diff] [blame] | 3775 | } |
Ian Romanick | 3fb8787 | 2010-07-09 14:09:34 -0700 | [diff] [blame] | 3776 | |
Paul Berry | cd18ba1 | 2014-01-07 08:56:57 -0800 | [diff] [blame] | 3777 | _mesa_reference_shader(ctx, &prog->_LinkedShaders[stage], sh); |
| 3778 | } |
Ian Romanick | cc22c5a | 2010-06-18 17:13:42 -0700 | [diff] [blame] | 3779 | } |
| 3780 | |
Paul Berry | cd18ba1 | 2014-01-07 08:56:57 -0800 | [diff] [blame] | 3781 | if (num_shaders[MESA_SHADER_GEOMETRY] > 0) |
Paul Berry | 44b7ebe | 2013-10-23 12:55:24 -0700 | [diff] [blame] | 3782 | prog->LastClipDistanceArraySize = prog->Geom.ClipDistanceArraySize; |
Chris Forbes | df16e0d | 2014-09-09 19:25:02 +1200 | [diff] [blame] | 3783 | else if (num_shaders[MESA_SHADER_TESS_EVAL] > 0) |
| 3784 | prog->LastClipDistanceArraySize = prog->TessEval.ClipDistanceArraySize; |
Paul Berry | cd18ba1 | 2014-01-07 08:56:57 -0800 | [diff] [blame] | 3785 | else if (num_shaders[MESA_SHADER_VERTEX] > 0) |
| 3786 | prog->LastClipDistanceArraySize = prog->Vert.ClipDistanceArraySize; |
| 3787 | else |
| 3788 | prog->LastClipDistanceArraySize = 0; /* Not used */ |
Bryan Cain | 2548092 | 2013-02-15 09:46:50 -0600 | [diff] [blame] | 3789 | |
Ian Romanick | 3ed850e | 2010-06-23 12:18:21 -0700 | [diff] [blame] | 3790 | /* Here begins the inter-stage linking phase. Some initial validation is |
| 3791 | * performed, then locations are assigned for uniforms, attributes, and |
| 3792 | * varyings. |
| 3793 | */ |
Paul Berry | b95d237 | 2013-07-27 11:08:31 -0700 | [diff] [blame] | 3794 | cross_validate_uniforms(prog); |
| 3795 | if (!prog->LinkStatus) |
| 3796 | goto done; |
Ian Romanick | 3322fba | 2010-10-14 13:28:42 -0700 | [diff] [blame] | 3797 | |
Paul Berry | b95d237 | 2013-07-27 11:08:31 -0700 | [diff] [blame] | 3798 | unsigned prev; |
Ian Romanick | 3322fba | 2010-10-14 13:28:42 -0700 | [diff] [blame] | 3799 | |
Paul Berry | 28e526d | 2014-01-06 19:47:25 -0800 | [diff] [blame] | 3800 | for (prev = 0; prev <= MESA_SHADER_FRAGMENT; prev++) { |
Paul Berry | b95d237 | 2013-07-27 11:08:31 -0700 | [diff] [blame] | 3801 | if (prog->_LinkedShaders[prev] != NULL) |
| 3802 | break; |
| 3803 | } |
Ian Romanick | 3322fba | 2010-10-14 13:28:42 -0700 | [diff] [blame] | 3804 | |
Tapani Pälli | eca9d16 | 2014-04-08 08:45:36 +0300 | [diff] [blame] | 3805 | check_explicit_uniform_locations(ctx, prog); |
Ian Romanick | 4ff9e59 | 2015-08-19 13:36:22 -0700 | [diff] [blame] | 3806 | link_assign_subroutine_types(prog); |
Dave Airlie | 6026686 | 2015-04-20 10:27:36 +1000 | [diff] [blame] | 3807 | |
Tapani Pälli | eca9d16 | 2014-04-08 08:45:36 +0300 | [diff] [blame] | 3808 | if (!prog->LinkStatus) |
| 3809 | goto done; |
| 3810 | |
Chris Forbes | 7c758c5 | 2014-09-21 13:33:14 +1200 | [diff] [blame] | 3811 | resize_tes_inputs(ctx, prog); |
| 3812 | |
Paul Berry | b95d237 | 2013-07-27 11:08:31 -0700 | [diff] [blame] | 3813 | /* Validate the inputs of each stage with the output of the preceding |
| 3814 | * stage. |
| 3815 | */ |
Paul Berry | 28e526d | 2014-01-06 19:47:25 -0800 | [diff] [blame] | 3816 | for (unsigned i = prev + 1; i <= MESA_SHADER_FRAGMENT; i++) { |
Paul Berry | b95d237 | 2013-07-27 11:08:31 -0700 | [diff] [blame] | 3817 | if (prog->_LinkedShaders[i] == NULL) |
| 3818 | continue; |
Kenneth Graunke | 3ddfccb | 2013-05-20 23:46:16 -0700 | [diff] [blame] | 3819 | |
Paul Berry | 544e312 | 2013-11-15 14:23:45 -0800 | [diff] [blame] | 3820 | validate_interstage_inout_blocks(prog, prog->_LinkedShaders[prev], |
| 3821 | prog->_LinkedShaders[i]); |
Paul Berry | b95d237 | 2013-07-27 11:08:31 -0700 | [diff] [blame] | 3822 | if (!prog->LinkStatus) |
| 3823 | goto done; |
Ian Romanick | 3322fba | 2010-10-14 13:28:42 -0700 | [diff] [blame] | 3824 | |
Paul Berry | b95d237 | 2013-07-27 11:08:31 -0700 | [diff] [blame] | 3825 | cross_validate_outputs_to_inputs(prog, |
| 3826 | prog->_LinkedShaders[prev], |
| 3827 | prog->_LinkedShaders[i]); |
| 3828 | if (!prog->LinkStatus) |
| 3829 | goto done; |
Ian Romanick | 3710192 | 2010-06-18 19:02:10 -0700 | [diff] [blame] | 3830 | |
Paul Berry | b95d237 | 2013-07-27 11:08:31 -0700 | [diff] [blame] | 3831 | prev = i; |
Ian Romanick | 3710192 | 2010-06-18 19:02:10 -0700 | [diff] [blame] | 3832 | } |
Ian Romanick | 832dfa5 | 2010-06-17 15:04:20 -0700 | [diff] [blame] | 3833 | |
Paul Berry | 544e312 | 2013-11-15 14:23:45 -0800 | [diff] [blame] | 3834 | /* Cross-validate uniform blocks between shader stages */ |
| 3835 | validate_interstage_uniform_blocks(prog, prog->_LinkedShaders, |
Paul Berry | 665b8d7 | 2014-01-07 10:11:39 -0800 | [diff] [blame] | 3836 | MESA_SHADER_STAGES); |
Paul Berry | 544e312 | 2013-11-15 14:23:45 -0800 | [diff] [blame] | 3837 | if (!prog->LinkStatus) |
| 3838 | goto done; |
Jordan Justen | 5ebf547 | 2013-03-10 03:20:03 -0700 | [diff] [blame] | 3839 | |
Paul Berry | 665b8d7 | 2014-01-07 10:11:39 -0800 | [diff] [blame] | 3840 | for (unsigned int i = 0; i < MESA_SHADER_STAGES; i++) { |
Jordan Justen | 5ebf547 | 2013-03-10 03:20:03 -0700 | [diff] [blame] | 3841 | if (prog->_LinkedShaders[i] != NULL) |
| 3842 | lower_named_interface_blocks(mem_ctx, prog->_LinkedShaders[i]); |
| 3843 | } |
| 3844 | |
Eric Anholt | 3de1395 | 2012-05-04 13:08:46 -0700 | [diff] [blame] | 3845 | /* Implement the GLSL 1.30+ rule for discard vs infinite loops Do |
| 3846 | * it before optimization because we want most of the checks to get |
| 3847 | * dropped thanks to constant propagation. |
Paul Berry | 15ba2a5 | 2012-08-02 17:51:02 -0700 | [diff] [blame] | 3848 | * |
| 3849 | * This rule also applies to GLSL ES 3.00. |
Eric Anholt | 3de1395 | 2012-05-04 13:08:46 -0700 | [diff] [blame] | 3850 | */ |
Paul Berry | 15ba2a5 | 2012-08-02 17:51:02 -0700 | [diff] [blame] | 3851 | if (max_version >= (is_es_prog ? 300 : 130)) { |
Eric Anholt | 3de1395 | 2012-05-04 13:08:46 -0700 | [diff] [blame] | 3852 | struct gl_shader *sh = prog->_LinkedShaders[MESA_SHADER_FRAGMENT]; |
| 3853 | if (sh) { |
| 3854 | lower_discard_flow(sh->ir); |
| 3855 | } |
| 3856 | } |
| 3857 | |
Eric Anholt | f609cf7 | 2012-04-27 13:52:56 -0700 | [diff] [blame] | 3858 | if (!interstage_cross_validate_uniform_blocks(prog)) |
| 3859 | goto done; |
| 3860 | |
Eric Anholt | 2f4fe15 | 2010-08-10 13:06:49 -0700 | [diff] [blame] | 3861 | /* Do common optimization before assigning storage for attributes, |
| 3862 | * uniforms, and varyings. Later optimization could possibly make |
| 3863 | * some of that unused. |
| 3864 | */ |
Paul Berry | 665b8d7 | 2014-01-07 10:11:39 -0800 | [diff] [blame] | 3865 | for (unsigned i = 0; i < MESA_SHADER_STAGES; i++) { |
Ian Romanick | 3322fba | 2010-10-14 13:28:42 -0700 | [diff] [blame] | 3866 | if (prog->_LinkedShaders[i] == NULL) |
| 3867 | continue; |
| 3868 | |
Ian Romanick | 02c5ae1 | 2011-07-11 10:46:01 -0700 | [diff] [blame] | 3869 | detect_recursion_linked(prog, prog->_LinkedShaders[i]->ir); |
| 3870 | if (!prog->LinkStatus) |
| 3871 | goto done; |
| 3872 | |
Marek Olšák | 002211f | 2014-08-03 04:31:56 +0200 | [diff] [blame] | 3873 | if (ctx->Const.ShaderCompilerOptions[i].LowerClipDistance) { |
Paul Berry | 1839244 | 2012-12-04 11:11:02 -0800 | [diff] [blame] | 3874 | lower_clip_distance(prog->_LinkedShaders[i]); |
| 3875 | } |
Paul Berry | c06e325 | 2011-08-11 20:58:21 -0700 | [diff] [blame] | 3876 | |
Fabian Bieler | 73a9a15 | 2014-03-10 17:55:36 +0100 | [diff] [blame] | 3877 | if (ctx->Const.LowerTessLevel) { |
| 3878 | lower_tess_level(prog->_LinkedShaders[i]); |
| 3879 | } |
| 3880 | |
Kenneth Graunke | 169c645 | 2014-04-06 23:25:00 -0700 | [diff] [blame] | 3881 | while (do_common_optimization(prog->_LinkedShaders[i]->ir, true, false, |
Marek Olšák | 002211f | 2014-08-03 04:31:56 +0200 | [diff] [blame] | 3882 | &ctx->Const.ShaderCompilerOptions[i], |
Kenneth Graunke | 169c645 | 2014-04-06 23:25:00 -0700 | [diff] [blame] | 3883 | ctx->Const.NativeIntegers)) |
Eric Anholt | 2f4fe15 | 2010-08-10 13:06:49 -0700 | [diff] [blame] | 3884 | ; |
Kenneth Graunke | 4f22db5 | 2014-04-26 00:18:54 -0700 | [diff] [blame] | 3885 | |
| 3886 | lower_const_arrays_to_uniforms(prog->_LinkedShaders[i]->ir); |
Ian Romanick | a7ba9a7 | 2010-07-20 13:36:32 -0700 | [diff] [blame] | 3887 | } |
Ian Romanick | 13e10e4 | 2010-06-21 12:03:24 -0700 | [diff] [blame] | 3888 | |
Tapani Pälli | 9350ea6 | 2015-05-19 15:01:49 +0300 | [diff] [blame] | 3889 | /* Validation for special cases where we allow sampler array indexing |
| 3890 | * with loop induction variable. This check emits a warning or error |
| 3891 | * depending if backend can handle dynamic indexing. |
| 3892 | */ |
| 3893 | if ((!prog->IsES && prog->Version < 130) || |
| 3894 | (prog->IsES && prog->Version < 300)) { |
| 3895 | if (!validate_sampler_array_indexing(ctx, prog)) |
| 3896 | goto done; |
| 3897 | } |
| 3898 | |
Iago Toral Quiroga | 7589683 | 2014-06-16 16:09:53 +0200 | [diff] [blame] | 3899 | /* Check and validate stream emissions in geometry shaders */ |
| 3900 | validate_geometry_shader_emissions(ctx, prog); |
| 3901 | |
Paul Berry | 50895d4 | 2012-12-05 07:17:07 -0800 | [diff] [blame] | 3902 | /* Mark all generic shader inputs and outputs as unpaired. */ |
Ian Romanick | 6bdc1d9 | 2014-02-11 16:37:56 -0800 | [diff] [blame] | 3903 | for (unsigned i = MESA_SHADER_VERTEX; i <= MESA_SHADER_FRAGMENT; i++) { |
| 3904 | if (prog->_LinkedShaders[i] != NULL) { |
| 3905 | link_invalidate_variable_locations(prog->_LinkedShaders[i]->ir); |
| 3906 | } |
Paul Berry | 50895d4 | 2012-12-05 07:17:07 -0800 | [diff] [blame] | 3907 | } |
| 3908 | |
Tapani Pälli | b868971 | 2015-07-27 13:29:20 +0300 | [diff] [blame] | 3909 | if (!assign_attribute_or_color_locations(prog, &ctx->Const, |
| 3910 | MESA_SHADER_VERTEX)) { |
Ian Romanick | d32d4f7 | 2011-06-27 17:59:58 -0700 | [diff] [blame] | 3911 | goto done; |
| 3912 | } |
| 3913 | |
Tapani Pälli | b868971 | 2015-07-27 13:29:20 +0300 | [diff] [blame] | 3914 | if (!assign_attribute_or_color_locations(prog, &ctx->Const, |
| 3915 | MESA_SHADER_FRAGMENT)) { |
Ian Romanick | d32d4f7 | 2011-06-27 17:59:58 -0700 | [diff] [blame] | 3916 | goto done; |
Ian Romanick | 40e114b | 2010-08-17 14:55:50 -0700 | [diff] [blame] | 3917 | } |
| 3918 | |
Tapani Pälli | 993b9b6 | 2015-03-17 13:58:57 +0200 | [diff] [blame] | 3919 | unsigned first, last; |
| 3920 | |
| 3921 | first = MESA_SHADER_STAGES; |
| 3922 | last = 0; |
| 3923 | |
| 3924 | /* Determine first and last stage. */ |
| 3925 | for (unsigned i = 0; i < MESA_SHADER_STAGES; i++) { |
| 3926 | if (!prog->_LinkedShaders[i]) |
| 3927 | continue; |
| 3928 | if (first == MESA_SHADER_STAGES) |
| 3929 | first = i; |
| 3930 | last = i; |
Ian Romanick | 3322fba | 2010-10-14 13:28:42 -0700 | [diff] [blame] | 3931 | } |
| 3932 | |
Paul Berry | 871ddb9 | 2011-11-05 11:17:32 -0700 | [diff] [blame] | 3933 | if (num_tfeedback_decls != 0) { |
| 3934 | /* From GL_EXT_transform_feedback: |
| 3935 | * A program will fail to link if: |
| 3936 | * |
| 3937 | * * the <count> specified by TransformFeedbackVaryingsEXT is |
| 3938 | * non-zero, but the program object has no vertex or geometry |
| 3939 | * shader; |
| 3940 | */ |
Bryan Cain | 2548092 | 2013-02-15 09:46:50 -0600 | [diff] [blame] | 3941 | if (first == MESA_SHADER_FRAGMENT) { |
Paul Berry | 871ddb9 | 2011-11-05 11:17:32 -0700 | [diff] [blame] | 3942 | linker_error(prog, "Transform feedback varyings specified, but " |
Andres Gomez | f9fc3ae | 2014-11-18 08:43:35 -0700 | [diff] [blame] | 3943 | "no vertex or geometry shader is present.\n"); |
Paul Berry | 871ddb9 | 2011-11-05 11:17:32 -0700 | [diff] [blame] | 3944 | goto done; |
| 3945 | } |
| 3946 | |
| 3947 | tfeedback_decls = ralloc_array(mem_ctx, tfeedback_decl, |
| 3948 | prog->TransformFeedback.NumVarying); |
Paul Berry | 456279b | 2011-12-26 19:39:25 -0800 | [diff] [blame] | 3949 | if (!parse_tfeedback_decls(ctx, prog, mem_ctx, num_tfeedback_decls, |
Paul Berry | 871ddb9 | 2011-11-05 11:17:32 -0700 | [diff] [blame] | 3950 | prog->TransformFeedback.VaryingNames, |
| 3951 | tfeedback_decls)) |
| 3952 | goto done; |
| 3953 | } |
| 3954 | |
Marek Olšák | 284d954 | 2013-06-12 02:18:09 +0200 | [diff] [blame] | 3955 | /* Linking the stages in the opposite order (from fragment to vertex) |
| 3956 | * ensures that inter-shader outputs written to in an earlier stage are |
| 3957 | * eliminated if they are (transitively) not used in a later stage. |
| 3958 | */ |
Tapani Pälli | 993b9b6 | 2015-03-17 13:58:57 +0200 | [diff] [blame] | 3959 | int next; |
Ian Romanick | 13e10e4 | 2010-06-21 12:03:24 -0700 | [diff] [blame] | 3960 | |
Tapani Pälli | 993b9b6 | 2015-03-17 13:58:57 +0200 | [diff] [blame] | 3961 | if (first < MESA_SHADER_FRAGMENT) { |
Marek Olšák | 284d954 | 2013-06-12 02:18:09 +0200 | [diff] [blame] | 3962 | gl_shader *const sh = prog->_LinkedShaders[last]; |
| 3963 | |
Ian Romanick | a909b99 | 2014-12-01 14:07:30 -0800 | [diff] [blame] | 3964 | if (first == MESA_SHADER_GEOMETRY) { |
| 3965 | /* There was no vertex shader, but we still have to assign varying |
| 3966 | * locations for use by geometry shader inputs in SSO. |
| 3967 | * |
| 3968 | * If the shader is not separable (i.e., prog->SeparateShader is |
| 3969 | * false), linking will have already failed when first is |
| 3970 | * MESA_SHADER_GEOMETRY. |
| 3971 | */ |
| 3972 | if (!assign_varying_locations(ctx, mem_ctx, prog, |
Tapani Pälli | 993b9b6 | 2015-03-17 13:58:57 +0200 | [diff] [blame] | 3973 | NULL, prog->_LinkedShaders[first], |
Chris Forbes | 0e94f35 | 2014-09-07 18:19:15 +1200 | [diff] [blame] | 3974 | num_tfeedback_decls, tfeedback_decls)) |
Ian Romanick | a909b99 | 2014-12-01 14:07:30 -0800 | [diff] [blame] | 3975 | goto done; |
| 3976 | } |
| 3977 | |
Tapani Pälli | 993b9b6 | 2015-03-17 13:58:57 +0200 | [diff] [blame] | 3978 | if (last != MESA_SHADER_FRAGMENT && |
| 3979 | (num_tfeedback_decls != 0 || prog->SeparateShader)) { |
Marek Olšák | 284d954 | 2013-06-12 02:18:09 +0200 | [diff] [blame] | 3980 | /* There was no fragment shader, but we still have to assign varying |
| 3981 | * locations for use by transform feedback. |
| 3982 | */ |
| 3983 | if (!assign_varying_locations(ctx, mem_ctx, prog, |
| 3984 | sh, NULL, |
Chris Forbes | 0e94f35 | 2014-09-07 18:19:15 +1200 | [diff] [blame] | 3985 | num_tfeedback_decls, tfeedback_decls)) |
Marek Olšák | 284d954 | 2013-06-12 02:18:09 +0200 | [diff] [blame] | 3986 | goto done; |
| 3987 | } |
| 3988 | |
Marek Olšák | d13003f | 2013-08-09 22:34:45 +0200 | [diff] [blame] | 3989 | do_dead_builtin_varyings(ctx, sh, NULL, |
Marek Olšák | b3d8b4c | 2013-06-12 13:23:48 +0200 | [diff] [blame] | 3990 | num_tfeedback_decls, tfeedback_decls); |
| 3991 | |
Ian Romanick | 1ff5a2b | 2014-02-14 12:10:27 -0800 | [diff] [blame] | 3992 | if (!prog->SeparateShader) |
| 3993 | demote_shader_inputs_and_outputs(sh, ir_var_shader_out); |
Marek Olšák | 284d954 | 2013-06-12 02:18:09 +0200 | [diff] [blame] | 3994 | |
| 3995 | /* Eliminate code that is now dead due to unused outputs being demoted. |
Paul Berry | 871ddb9 | 2011-11-05 11:17:32 -0700 | [diff] [blame] | 3996 | */ |
Marek Olšák | 284d954 | 2013-06-12 02:18:09 +0200 | [diff] [blame] | 3997 | while (do_dead_code(sh->ir, false)) |
| 3998 | ; |
| 3999 | } |
| 4000 | else if (first == MESA_SHADER_FRAGMENT) { |
Marek Olšák | b3d8b4c | 2013-06-12 13:23:48 +0200 | [diff] [blame] | 4001 | /* If the program only contains a fragment shader... |
Marek Olšák | 284d954 | 2013-06-12 02:18:09 +0200 | [diff] [blame] | 4002 | */ |
| 4003 | gl_shader *const sh = prog->_LinkedShaders[first]; |
| 4004 | |
Marek Olšák | d13003f | 2013-08-09 22:34:45 +0200 | [diff] [blame] | 4005 | do_dead_builtin_varyings(ctx, NULL, sh, |
Marek Olšák | b3d8b4c | 2013-06-12 13:23:48 +0200 | [diff] [blame] | 4006 | num_tfeedback_decls, tfeedback_decls); |
| 4007 | |
Ian Romanick | 1ff5a2b | 2014-02-14 12:10:27 -0800 | [diff] [blame] | 4008 | if (prog->SeparateShader) { |
| 4009 | if (!assign_varying_locations(ctx, mem_ctx, prog, |
| 4010 | NULL /* producer */, |
| 4011 | sh /* consumer */, |
| 4012 | 0 /* num_tfeedback_decls */, |
Chris Forbes | 0e94f35 | 2014-09-07 18:19:15 +1200 | [diff] [blame] | 4013 | NULL /* tfeedback_decls */)) |
Ian Romanick | 1ff5a2b | 2014-02-14 12:10:27 -0800 | [diff] [blame] | 4014 | goto done; |
| 4015 | } else |
| 4016 | demote_shader_inputs_and_outputs(sh, ir_var_shader_in); |
Marek Olšák | 284d954 | 2013-06-12 02:18:09 +0200 | [diff] [blame] | 4017 | |
| 4018 | while (do_dead_code(sh->ir, false)) |
| 4019 | ; |
| 4020 | } |
| 4021 | |
| 4022 | next = last; |
| 4023 | for (int i = next - 1; i >= 0; i--) { |
| 4024 | if (prog->_LinkedShaders[i] == NULL) |
| 4025 | continue; |
| 4026 | |
| 4027 | gl_shader *const sh_i = prog->_LinkedShaders[i]; |
| 4028 | gl_shader *const sh_next = prog->_LinkedShaders[next]; |
| 4029 | |
| 4030 | if (!assign_varying_locations(ctx, mem_ctx, prog, sh_i, sh_next, |
| 4031 | next == MESA_SHADER_FRAGMENT ? num_tfeedback_decls : 0, |
Chris Forbes | 0e94f35 | 2014-09-07 18:19:15 +1200 | [diff] [blame] | 4032 | tfeedback_decls)) |
Paul Berry | 871ddb9 | 2011-11-05 11:17:32 -0700 | [diff] [blame] | 4033 | goto done; |
Marek Olšák | 284d954 | 2013-06-12 02:18:09 +0200 | [diff] [blame] | 4034 | |
Marek Olšák | d13003f | 2013-08-09 22:34:45 +0200 | [diff] [blame] | 4035 | do_dead_builtin_varyings(ctx, sh_i, sh_next, |
Marek Olšák | b3d8b4c | 2013-06-12 13:23:48 +0200 | [diff] [blame] | 4036 | next == MESA_SHADER_FRAGMENT ? num_tfeedback_decls : 0, |
| 4037 | tfeedback_decls); |
| 4038 | |
Marek Olšák | 284d954 | 2013-06-12 02:18:09 +0200 | [diff] [blame] | 4039 | demote_shader_inputs_and_outputs(sh_i, ir_var_shader_out); |
| 4040 | demote_shader_inputs_and_outputs(sh_next, ir_var_shader_in); |
| 4041 | |
| 4042 | /* Eliminate code that is now dead due to unused outputs being demoted. |
| 4043 | */ |
| 4044 | while (do_dead_code(sh_i->ir, false)) |
| 4045 | ; |
| 4046 | while (do_dead_code(sh_next->ir, false)) |
| 4047 | ; |
| 4048 | |
Marek Olšák | 3c55582 | 2013-06-13 03:17:22 +0200 | [diff] [blame] | 4049 | /* This must be done after all dead varyings are eliminated. */ |
Ian Romanick | 42305fb | 2013-09-10 12:00:34 -0500 | [diff] [blame] | 4050 | if (!check_against_output_limit(ctx, prog, sh_i)) |
| 4051 | goto done; |
| 4052 | if (!check_against_input_limit(ctx, prog, sh_next)) |
Marek Olšák | 3c55582 | 2013-06-13 03:17:22 +0200 | [diff] [blame] | 4053 | goto done; |
| 4054 | |
Marek Olšák | 284d954 | 2013-06-12 02:18:09 +0200 | [diff] [blame] | 4055 | next = i; |
Paul Berry | 871ddb9 | 2011-11-05 11:17:32 -0700 | [diff] [blame] | 4056 | } |
| 4057 | |
| 4058 | if (!store_tfeedback_info(ctx, prog, num_tfeedback_decls, tfeedback_decls)) |
| 4059 | goto done; |
| 4060 | |
Ian Romanick | 960d722 | 2011-10-21 11:21:02 -0700 | [diff] [blame] | 4061 | update_array_sizes(prog); |
Matt Turner | 9e2e7c7 | 2014-08-08 19:46:05 -0700 | [diff] [blame] | 4062 | link_assign_uniform_locations(prog, ctx->Const.UniformBooleanTrue); |
Francisco Jerez | 5c11493 | 2013-09-11 12:14:46 -0700 | [diff] [blame] | 4063 | link_assign_atomic_counter_resources(ctx, prog); |
Marek Olšák | ec174a4 | 2011-11-18 15:00:10 +0100 | [diff] [blame] | 4064 | store_fragdepth_layout(prog); |
Ian Romanick | 960d722 | 2011-10-21 11:21:02 -0700 | [diff] [blame] | 4065 | |
Ian Romanick | 4ff9e59 | 2015-08-19 13:36:22 -0700 | [diff] [blame] | 4066 | link_calculate_subroutine_compat(prog); |
Paul Berry | b95d237 | 2013-07-27 11:08:31 -0700 | [diff] [blame] | 4067 | check_resources(ctx, prog); |
Ian Romanick | 4ff9e59 | 2015-08-19 13:36:22 -0700 | [diff] [blame] | 4068 | check_subroutine_resources(prog); |
Francisco Jerez | e51158f | 2013-11-22 15:53:26 -0800 | [diff] [blame] | 4069 | check_image_resources(ctx, prog); |
Francisco Jerez | 5c11493 | 2013-09-11 12:14:46 -0700 | [diff] [blame] | 4070 | link_check_atomic_counter_resources(ctx, prog); |
| 4071 | |
Paul Berry | b95d237 | 2013-07-27 11:08:31 -0700 | [diff] [blame] | 4072 | if (!prog->LinkStatus) |
Ian Romanick | 92f8159 | 2011-11-08 12:37:19 -0800 | [diff] [blame] | 4073 | goto done; |
| 4074 | |
Ian Romanick | ce9171f | 2011-02-03 17:10:14 -0800 | [diff] [blame] | 4075 | /* OpenGL ES requires that a vertex shader and a fragment shader both be |
Anuj Phogat | 03597cf | 2013-12-19 14:17:19 -0800 | [diff] [blame] | 4076 | * present in a linked program. GL_ARB_ES2_compatibility doesn't say |
| 4077 | * anything about shader linking when one of the shaders (vertex or |
| 4078 | * fragment shader) is absent. So, the extension shouldn't change the |
| 4079 | * behavior specified in GLSL specification. |
Ian Romanick | ce9171f | 2011-02-03 17:10:14 -0800 | [diff] [blame] | 4080 | */ |
Ian Romanick | f64bfb2 | 2014-03-27 10:29:30 -0700 | [diff] [blame] | 4081 | if (!prog->SeparateShader && ctx->API == API_OPENGLES2) { |
Tapani Pälli | 08e9049 | 2015-09-03 14:26:48 +0300 | [diff] [blame] | 4082 | /* With ES < 3.1 one needs to have always vertex + fragment shader. */ |
| 4083 | if (ctx->Version < 31) { |
| 4084 | if (prog->_LinkedShaders[MESA_SHADER_VERTEX] == NULL) { |
| 4085 | linker_error(prog, "program lacks a vertex shader\n"); |
| 4086 | } else if (prog->_LinkedShaders[MESA_SHADER_FRAGMENT] == NULL) { |
| 4087 | linker_error(prog, "program lacks a fragment shader\n"); |
| 4088 | } |
| 4089 | } else { |
| 4090 | /* From OpenGL ES 3.1 specification (7.3 Program Objects): |
| 4091 | * "Linking can fail for a variety of reasons as specified in the |
| 4092 | * OpenGL ES Shading Language Specification, as well as any of the |
| 4093 | * following reasons: |
| 4094 | * |
| 4095 | * ... |
| 4096 | * |
| 4097 | * * program contains objects to form either a vertex shader or |
| 4098 | * fragment shader, and program is not separable, and does not |
| 4099 | * contain objects to form both a vertex shader and fragment |
| 4100 | * shader." |
| 4101 | */ |
| 4102 | if (!!prog->_LinkedShaders[MESA_SHADER_VERTEX] ^ |
| 4103 | !!prog->_LinkedShaders[MESA_SHADER_FRAGMENT]) { |
| 4104 | linker_error(prog, "Program needs to contain both vertex and " |
| 4105 | "fragment shaders.\n"); |
| 4106 | } |
Ian Romanick | ce9171f | 2011-02-03 17:10:14 -0800 | [diff] [blame] | 4107 | } |
| 4108 | } |
| 4109 | |
Ian Romanick | 13e10e4 | 2010-06-21 12:03:24 -0700 | [diff] [blame] | 4110 | /* FINISHME: Assign fragment shader output locations. */ |
| 4111 | |
Ian Romanick | 832dfa5 | 2010-06-17 15:04:20 -0700 | [diff] [blame] | 4112 | done: |
Paul Berry | 665b8d7 | 2014-01-07 10:11:39 -0800 | [diff] [blame] | 4113 | for (unsigned i = 0; i < MESA_SHADER_STAGES; i++) { |
Paul Berry | cd18ba1 | 2014-01-07 08:56:57 -0800 | [diff] [blame] | 4114 | free(shader_list[i]); |
Kenneth Graunke | 2da02e7 | 2010-11-17 11:03:57 -0800 | [diff] [blame] | 4115 | if (prog->_LinkedShaders[i] == NULL) |
| 4116 | continue; |
| 4117 | |
Paul Berry | d7fa9eb | 2013-11-22 12:37:22 -0800 | [diff] [blame] | 4118 | /* Do a final validation step to make sure that the IR wasn't |
| 4119 | * invalidated by any modifications performed after intrastage linking. |
| 4120 | */ |
| 4121 | validate_ir_tree(prog->_LinkedShaders[i]->ir); |
| 4122 | |
Kenneth Graunke | 2da02e7 | 2010-11-17 11:03:57 -0800 | [diff] [blame] | 4123 | /* Retain any live IR, but trash the rest. */ |
| 4124 | reparent_ir(prog->_LinkedShaders[i]->ir, prog->_LinkedShaders[i]->ir); |
Ian Romanick | 7bbcc0b | 2011-09-30 14:21:10 -0700 | [diff] [blame] | 4125 | |
| 4126 | /* The symbol table in the linked shaders may contain references to |
| 4127 | * variables that were removed (e.g., unused uniforms). Since it may |
| 4128 | * contain junk, there is no possible valid use. Delete it and set the |
| 4129 | * pointer to NULL. |
| 4130 | */ |
| 4131 | delete prog->_LinkedShaders[i]->symbols; |
| 4132 | prog->_LinkedShaders[i]->symbols = NULL; |
Kenneth Graunke | 2da02e7 | 2010-11-17 11:03:57 -0800 | [diff] [blame] | 4133 | } |
| 4134 | |
Kenneth Graunke | d3073f5 | 2011-01-21 14:32:31 -0800 | [diff] [blame] | 4135 | ralloc_free(mem_ctx); |
Ian Romanick | 832dfa5 | 2010-06-17 15:04:20 -0700 | [diff] [blame] | 4136 | } |