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 | |
Chia-I Wu | bfd7c9a | 2010-08-23 17:51:42 +0800 | [diff] [blame] | 67 | #include "main/core.h" |
Ian Romanick | 832dfa5 | 2010-06-17 15:04:20 -0700 | [diff] [blame] | 68 | #include "glsl_symbol_table.h" |
Eric Anholt | faf3dba | 2013-06-12 16:57:11 -0700 | [diff] [blame] | 69 | #include "glsl_parser_extras.h" |
Ian Romanick | 832dfa5 | 2010-06-17 15:04:20 -0700 | [diff] [blame] | 70 | #include "ir.h" |
| 71 | #include "program.h" |
Aras Pranckevicius | 3174715 | 2010-07-29 12:40:49 +0300 | [diff] [blame] | 72 | #include "program/hash_table.h" |
Ian Romanick | 8fe8a81 | 2010-07-13 17:36:13 -0700 | [diff] [blame] | 73 | #include "linker.h" |
Paul Berry | 4b11b57 | 2012-12-17 14:20:35 -0800 | [diff] [blame] | 74 | #include "link_varyings.h" |
Ian Romanick | a7ba9a7 | 2010-07-20 13:36:32 -0700 | [diff] [blame] | 75 | #include "ir_optimization.h" |
Ian Romanick | 832dfa5 | 2010-06-17 15:04:20 -0700 | [diff] [blame] | 76 | |
Ian Romanick | 3322fba | 2010-10-14 13:28:42 -0700 | [diff] [blame] | 77 | extern "C" { |
| 78 | #include "main/shaderobj.h" |
| 79 | } |
| 80 | |
Ian Romanick | 832dfa5 | 2010-06-17 15:04:20 -0700 | [diff] [blame] | 81 | /** |
| 82 | * Visitor that determines whether or not a variable is ever written. |
| 83 | */ |
| 84 | class find_assignment_visitor : public ir_hierarchical_visitor { |
| 85 | public: |
| 86 | find_assignment_visitor(const char *name) |
| 87 | : name(name), found(false) |
| 88 | { |
| 89 | /* empty */ |
| 90 | } |
| 91 | |
| 92 | virtual ir_visitor_status visit_enter(ir_assignment *ir) |
| 93 | { |
| 94 | ir_variable *const var = ir->lhs->variable_referenced(); |
| 95 | |
| 96 | if (strcmp(name, var->name) == 0) { |
| 97 | found = true; |
| 98 | return visit_stop; |
| 99 | } |
| 100 | |
| 101 | return visit_continue_with_parent; |
| 102 | } |
| 103 | |
Eric Anholt | 18a6023 | 2010-08-23 11:29:25 -0700 | [diff] [blame] | 104 | virtual ir_visitor_status visit_enter(ir_call *ir) |
| 105 | { |
Kenneth Graunke | 82065fa | 2011-09-20 18:08:11 -0700 | [diff] [blame] | 106 | exec_list_iterator sig_iter = ir->callee->parameters.iterator(); |
Eric Anholt | 18a6023 | 2010-08-23 11:29:25 -0700 | [diff] [blame] | 107 | foreach_iter(exec_list_iterator, iter, *ir) { |
| 108 | ir_rvalue *param_rval = (ir_rvalue *)iter.get(); |
| 109 | ir_variable *sig_param = (ir_variable *)sig_iter.get(); |
| 110 | |
Paul Berry | 42a29d8 | 2013-01-11 14:39:32 -0800 | [diff] [blame] | 111 | if (sig_param->mode == ir_var_function_out || |
| 112 | sig_param->mode == ir_var_function_inout) { |
Eric Anholt | 18a6023 | 2010-08-23 11:29:25 -0700 | [diff] [blame] | 113 | ir_variable *var = param_rval->variable_referenced(); |
| 114 | if (var && strcmp(name, var->name) == 0) { |
| 115 | found = true; |
| 116 | return visit_stop; |
| 117 | } |
| 118 | } |
| 119 | sig_iter.next(); |
| 120 | } |
| 121 | |
Kenneth Graunke | d884f60 | 2012-03-20 15:56:37 -0700 | [diff] [blame] | 122 | if (ir->return_deref != NULL) { |
| 123 | ir_variable *const var = ir->return_deref->variable_referenced(); |
| 124 | |
| 125 | if (strcmp(name, var->name) == 0) { |
| 126 | found = true; |
| 127 | return visit_stop; |
| 128 | } |
| 129 | } |
| 130 | |
Eric Anholt | 18a6023 | 2010-08-23 11:29:25 -0700 | [diff] [blame] | 131 | return visit_continue_with_parent; |
| 132 | } |
| 133 | |
Ian Romanick | 832dfa5 | 2010-06-17 15:04:20 -0700 | [diff] [blame] | 134 | bool variable_found() |
| 135 | { |
| 136 | return found; |
| 137 | } |
| 138 | |
| 139 | private: |
| 140 | const char *name; /**< Find writes to a variable with this name. */ |
| 141 | bool found; /**< Was a write to the variable found? */ |
| 142 | }; |
| 143 | |
Ian Romanick | c93b8f1 | 2010-06-17 15:20:22 -0700 | [diff] [blame] | 144 | |
Ian Romanick | c33e78f | 2010-08-13 12:30:41 -0700 | [diff] [blame] | 145 | /** |
| 146 | * Visitor that determines whether or not a variable is ever read. |
| 147 | */ |
| 148 | class find_deref_visitor : public ir_hierarchical_visitor { |
| 149 | public: |
| 150 | find_deref_visitor(const char *name) |
| 151 | : name(name), found(false) |
| 152 | { |
| 153 | /* empty */ |
| 154 | } |
| 155 | |
| 156 | virtual ir_visitor_status visit(ir_dereference_variable *ir) |
| 157 | { |
| 158 | if (strcmp(this->name, ir->var->name) == 0) { |
| 159 | this->found = true; |
| 160 | return visit_stop; |
| 161 | } |
| 162 | |
| 163 | return visit_continue; |
| 164 | } |
| 165 | |
| 166 | bool variable_found() const |
| 167 | { |
| 168 | return this->found; |
| 169 | } |
| 170 | |
| 171 | private: |
| 172 | const char *name; /**< Find writes to a variable with this name. */ |
| 173 | bool found; /**< Was a write to the variable found? */ |
| 174 | }; |
| 175 | |
| 176 | |
Ian Romanick | 0ad22cd | 2010-06-21 17:18:31 -0700 | [diff] [blame] | 177 | void |
Ian Romanick | 586e741 | 2011-07-28 14:04:09 -0700 | [diff] [blame] | 178 | linker_error(gl_shader_program *prog, const char *fmt, ...) |
Ian Romanick | f36460e | 2010-06-23 12:07:22 -0700 | [diff] [blame] | 179 | { |
| 180 | va_list ap; |
| 181 | |
Kenneth Graunke | d3073f5 | 2011-01-21 14:32:31 -0800 | [diff] [blame] | 182 | ralloc_strcat(&prog->InfoLog, "error: "); |
Ian Romanick | f36460e | 2010-06-23 12:07:22 -0700 | [diff] [blame] | 183 | va_start(ap, fmt); |
Kenneth Graunke | d3073f5 | 2011-01-21 14:32:31 -0800 | [diff] [blame] | 184 | ralloc_vasprintf_append(&prog->InfoLog, fmt, ap); |
Ian Romanick | f36460e | 2010-06-23 12:07:22 -0700 | [diff] [blame] | 185 | va_end(ap); |
Ian Romanick | 586e741 | 2011-07-28 14:04:09 -0700 | [diff] [blame] | 186 | |
| 187 | prog->LinkStatus = false; |
Ian Romanick | f36460e | 2010-06-23 12:07:22 -0700 | [diff] [blame] | 188 | } |
| 189 | |
| 190 | |
| 191 | void |
Ian Romanick | 379a32f | 2011-07-28 14:09:06 -0700 | [diff] [blame] | 192 | linker_warning(gl_shader_program *prog, const char *fmt, ...) |
| 193 | { |
| 194 | va_list ap; |
| 195 | |
| 196 | ralloc_strcat(&prog->InfoLog, "error: "); |
| 197 | va_start(ap, fmt); |
| 198 | ralloc_vasprintf_append(&prog->InfoLog, fmt, ap); |
| 199 | va_end(ap); |
| 200 | |
| 201 | } |
| 202 | |
| 203 | |
Paul Berry | b92900d | 2013-01-28 14:21:59 -0800 | [diff] [blame] | 204 | /** |
| 205 | * Given a string identifying a program resource, break it into a base name |
| 206 | * and an optional array index in square brackets. |
| 207 | * |
| 208 | * If an array index is present, \c out_base_name_end is set to point to the |
| 209 | * "[" that precedes the array index, and the array index itself is returned |
| 210 | * as a long. |
| 211 | * |
| 212 | * If no array index is present (or if the array index is negative or |
| 213 | * mal-formed), \c out_base_name_end, is set to point to the null terminator |
| 214 | * at the end of the input string, and -1 is returned. |
| 215 | * |
| 216 | * Only the final array index is parsed; if the string contains other array |
| 217 | * indices (or structure field accesses), they are left in the base name. |
| 218 | * |
| 219 | * No attempt is made to check that the base name is properly formed; |
| 220 | * typically the caller will look up the base name in a hash table, so |
| 221 | * ill-formed base names simply turn into hash table lookup failures. |
| 222 | */ |
| 223 | long |
| 224 | parse_program_resource_name(const GLchar *name, |
| 225 | const GLchar **out_base_name_end) |
| 226 | { |
| 227 | /* Section 7.3.1 ("Program Interfaces") of the OpenGL 4.3 spec says: |
| 228 | * |
| 229 | * "When an integer array element or block instance number is part of |
| 230 | * the name string, it will be specified in decimal form without a "+" |
| 231 | * or "-" sign or any extra leading zeroes. Additionally, the name |
| 232 | * string will not include white space anywhere in the string." |
| 233 | */ |
| 234 | |
| 235 | const size_t len = strlen(name); |
| 236 | *out_base_name_end = name + len; |
| 237 | |
| 238 | if (len == 0 || name[len-1] != ']') |
| 239 | return -1; |
| 240 | |
| 241 | /* Walk backwards over the string looking for a non-digit character. This |
| 242 | * had better be the opening bracket for an array index. |
| 243 | * |
| 244 | * Initially, i specifies the location of the ']'. Since the string may |
| 245 | * contain only the ']' charcater, walk backwards very carefully. |
| 246 | */ |
| 247 | unsigned i; |
| 248 | for (i = len - 1; (i > 0) && isdigit(name[i-1]); --i) |
| 249 | /* empty */ ; |
| 250 | |
| 251 | if ((i == 0) || name[i-1] != '[') |
| 252 | return -1; |
| 253 | |
| 254 | long array_index = strtol(&name[i], NULL, 10); |
| 255 | if (array_index < 0) |
| 256 | return -1; |
| 257 | |
| 258 | *out_base_name_end = name + (i - 1); |
| 259 | return array_index; |
| 260 | } |
| 261 | |
| 262 | |
Ian Romanick | 379a32f | 2011-07-28 14:09:06 -0700 | [diff] [blame] | 263 | void |
Paul Berry | 50895d4 | 2012-12-05 07:17:07 -0800 | [diff] [blame] | 264 | link_invalidate_variable_locations(gl_shader *sh, int input_base, |
| 265 | int output_base) |
Ian Romanick | 0ad22cd | 2010-06-21 17:18:31 -0700 | [diff] [blame] | 266 | { |
Eric Anholt | 16b68b1 | 2010-06-30 11:05:43 -0700 | [diff] [blame] | 267 | foreach_list(node, sh->ir) { |
Ian Romanick | 0ad22cd | 2010-06-21 17:18:31 -0700 | [diff] [blame] | 268 | ir_variable *const var = ((ir_instruction *) node)->as_variable(); |
| 269 | |
Paul Berry | 50895d4 | 2012-12-05 07:17:07 -0800 | [diff] [blame] | 270 | if (var == NULL) |
| 271 | continue; |
| 272 | |
| 273 | int base; |
| 274 | switch (var->mode) { |
Paul Berry | 42a29d8 | 2013-01-11 14:39:32 -0800 | [diff] [blame] | 275 | case ir_var_shader_in: |
Paul Berry | 50895d4 | 2012-12-05 07:17:07 -0800 | [diff] [blame] | 276 | base = input_base; |
| 277 | break; |
Paul Berry | 42a29d8 | 2013-01-11 14:39:32 -0800 | [diff] [blame] | 278 | case ir_var_shader_out: |
Paul Berry | 50895d4 | 2012-12-05 07:17:07 -0800 | [diff] [blame] | 279 | base = output_base; |
| 280 | break; |
| 281 | default: |
| 282 | continue; |
| 283 | } |
Ian Romanick | 0ad22cd | 2010-06-21 17:18:31 -0700 | [diff] [blame] | 284 | |
| 285 | /* Only assign locations for generic attributes / varyings / etc. |
| 286 | */ |
Paul Berry | 50895d4 | 2012-12-05 07:17:07 -0800 | [diff] [blame] | 287 | if ((var->location >= base) && !var->explicit_location) |
| 288 | var->location = -1; |
Paul Berry | 3c9c17d | 2012-12-04 15:17:01 -0800 | [diff] [blame] | 289 | |
Paul Berry | 3e81c66 | 2012-12-05 10:47:55 -0800 | [diff] [blame] | 290 | if ((var->location == -1) && !var->explicit_location) { |
Paul Berry | 3c9c17d | 2012-12-04 15:17:01 -0800 | [diff] [blame] | 291 | var->is_unmatched_generic_inout = 1; |
Paul Berry | 3e81c66 | 2012-12-05 10:47:55 -0800 | [diff] [blame] | 292 | var->location_frac = 0; |
| 293 | } else { |
Paul Berry | 3c9c17d | 2012-12-04 15:17:01 -0800 | [diff] [blame] | 294 | var->is_unmatched_generic_inout = 0; |
Paul Berry | 3e81c66 | 2012-12-05 10:47:55 -0800 | [diff] [blame] | 295 | } |
Ian Romanick | 0ad22cd | 2010-06-21 17:18:31 -0700 | [diff] [blame] | 296 | } |
| 297 | } |
| 298 | |
| 299 | |
Ian Romanick | c93b8f1 | 2010-06-17 15:20:22 -0700 | [diff] [blame] | 300 | /** |
Ian Romanick | 6984670 | 2010-06-22 17:29:19 -0700 | [diff] [blame] | 301 | * Determine the number of attribute slots required for a particular type |
| 302 | * |
| 303 | * This code is here because it implements the language rules of a specific |
| 304 | * GLSL version. Since it's a property of the language and not a property of |
| 305 | * types in general, it doesn't really belong in glsl_type. |
| 306 | */ |
| 307 | unsigned |
| 308 | count_attribute_slots(const glsl_type *t) |
| 309 | { |
| 310 | /* From page 31 (page 37 of the PDF) of the GLSL 1.50 spec: |
| 311 | * |
| 312 | * "A scalar input counts the same amount against this limit as a vec4, |
| 313 | * so applications may want to consider packing groups of four |
| 314 | * unrelated float inputs together into a vector to better utilize the |
| 315 | * capabilities of the underlying hardware. A matrix input will use up |
| 316 | * multiple locations. The number of locations used will equal the |
| 317 | * number of columns in the matrix." |
| 318 | * |
| 319 | * The spec does not explicitly say how arrays are counted. However, it |
| 320 | * should be safe to assume the total number of slots consumed by an array |
| 321 | * is the number of entries in the array multiplied by the number of slots |
| 322 | * consumed by a single element of the array. |
| 323 | */ |
| 324 | |
| 325 | if (t->is_array()) |
| 326 | return t->array_size() * count_attribute_slots(t->element_type()); |
| 327 | |
| 328 | if (t->is_matrix()) |
| 329 | return t->matrix_columns; |
| 330 | |
| 331 | return 1; |
| 332 | } |
| 333 | |
| 334 | |
| 335 | /** |
Paul Berry | 1ad54ae | 2011-09-17 09:42:02 -0700 | [diff] [blame] | 336 | * Verify that a vertex shader executable meets all semantic requirements. |
| 337 | * |
Paul Berry | 642e5b41 | 2012-01-04 13:57:52 -0800 | [diff] [blame] | 338 | * Also sets prog->Vert.UsesClipDistance and prog->Vert.ClipDistanceArraySize |
| 339 | * as a side effect. |
Ian Romanick | c93b8f1 | 2010-06-17 15:20:22 -0700 | [diff] [blame] | 340 | * |
| 341 | * \param shader Vertex shader executable to be verified |
| 342 | */ |
Ian Romanick | 832dfa5 | 2010-06-17 15:04:20 -0700 | [diff] [blame] | 343 | bool |
Eric Anholt | 849e181 | 2010-06-30 11:49:17 -0700 | [diff] [blame] | 344 | validate_vertex_shader_executable(struct gl_shader_program *prog, |
Eric Anholt | 16b68b1 | 2010-06-30 11:05:43 -0700 | [diff] [blame] | 345 | struct gl_shader *shader) |
Ian Romanick | 832dfa5 | 2010-06-17 15:04:20 -0700 | [diff] [blame] | 346 | { |
| 347 | if (shader == NULL) |
| 348 | return true; |
| 349 | |
Eric Anholt | f1c1c9e | 2012-03-19 22:43:27 -0700 | [diff] [blame] | 350 | /* From the GLSL 1.10 spec, page 48: |
| 351 | * |
| 352 | * "The variable gl_Position is available only in the vertex |
| 353 | * language and is intended for writing the homogeneous vertex |
| 354 | * position. All executions of a well-formed vertex shader |
| 355 | * executable must write a value into this variable. [...] The |
| 356 | * variable gl_Position is available only in the vertex |
| 357 | * language and is intended for writing the homogeneous vertex |
| 358 | * position. All executions of a well-formed vertex shader |
| 359 | * executable must write a value into this variable." |
| 360 | * |
| 361 | * while in GLSL 1.40 this text is changed to: |
| 362 | * |
| 363 | * "The variable gl_Position is available only in the vertex |
| 364 | * language and is intended for writing the homogeneous vertex |
| 365 | * position. It can be written at any time during shader |
| 366 | * execution. It may also be read back by a vertex shader |
| 367 | * after being written. This value will be used by primitive |
| 368 | * assembly, clipping, culling, and other fixed functionality |
| 369 | * operations, if present, that operate on primitives after |
| 370 | * vertex processing has occurred. Its value is undefined if |
| 371 | * the vertex shader executable does not write gl_Position." |
Paul Berry | 15ba2a5 | 2012-08-02 17:51:02 -0700 | [diff] [blame] | 372 | * |
| 373 | * GLSL ES 3.00 is similar to GLSL 1.40--failing to write to gl_Position is |
| 374 | * not an error. |
Eric Anholt | f1c1c9e | 2012-03-19 22:43:27 -0700 | [diff] [blame] | 375 | */ |
Paul Berry | 15ba2a5 | 2012-08-02 17:51:02 -0700 | [diff] [blame] | 376 | if (prog->Version < (prog->IsES ? 300 : 140)) { |
Eric Anholt | f1c1c9e | 2012-03-19 22:43:27 -0700 | [diff] [blame] | 377 | find_assignment_visitor find("gl_Position"); |
| 378 | find.run(shader->ir); |
| 379 | if (!find.variable_found()) { |
| 380 | linker_error(prog, "vertex shader does not write to `gl_Position'\n"); |
| 381 | return false; |
| 382 | } |
Ian Romanick | 832dfa5 | 2010-06-17 15:04:20 -0700 | [diff] [blame] | 383 | } |
| 384 | |
Paul Berry | 642e5b41 | 2012-01-04 13:57:52 -0800 | [diff] [blame] | 385 | prog->Vert.ClipDistanceArraySize = 0; |
| 386 | |
Paul Berry | 15ba2a5 | 2012-08-02 17:51:02 -0700 | [diff] [blame] | 387 | if (!prog->IsES && prog->Version >= 130) { |
Paul Berry | b453ba2 | 2011-08-11 18:10:22 -0700 | [diff] [blame] | 388 | /* From section 7.1 (Vertex Shader Special Variables) of the |
| 389 | * GLSL 1.30 spec: |
| 390 | * |
| 391 | * "It is an error for a shader to statically write both |
| 392 | * gl_ClipVertex and gl_ClipDistance." |
Paul Berry | 15ba2a5 | 2012-08-02 17:51:02 -0700 | [diff] [blame] | 393 | * |
| 394 | * This does not apply to GLSL ES shaders, since GLSL ES defines neither |
| 395 | * gl_ClipVertex nor gl_ClipDistance. |
Paul Berry | b453ba2 | 2011-08-11 18:10:22 -0700 | [diff] [blame] | 396 | */ |
| 397 | find_assignment_visitor clip_vertex("gl_ClipVertex"); |
| 398 | find_assignment_visitor clip_distance("gl_ClipDistance"); |
| 399 | |
| 400 | clip_vertex.run(shader->ir); |
| 401 | clip_distance.run(shader->ir); |
| 402 | if (clip_vertex.variable_found() && clip_distance.variable_found()) { |
| 403 | linker_error(prog, "vertex shader writes to both `gl_ClipVertex' " |
| 404 | "and `gl_ClipDistance'\n"); |
| 405 | return false; |
| 406 | } |
Paul Berry | 1ad54ae | 2011-09-17 09:42:02 -0700 | [diff] [blame] | 407 | prog->Vert.UsesClipDistance = clip_distance.variable_found(); |
Paul Berry | 642e5b41 | 2012-01-04 13:57:52 -0800 | [diff] [blame] | 408 | ir_variable *clip_distance_var = |
| 409 | shader->symbols->get_variable("gl_ClipDistance"); |
| 410 | if (clip_distance_var) |
| 411 | prog->Vert.ClipDistanceArraySize = clip_distance_var->type->length; |
Paul Berry | b453ba2 | 2011-08-11 18:10:22 -0700 | [diff] [blame] | 412 | } |
| 413 | |
Ian Romanick | 832dfa5 | 2010-06-17 15:04:20 -0700 | [diff] [blame] | 414 | return true; |
| 415 | } |
| 416 | |
| 417 | |
Ian Romanick | c93b8f1 | 2010-06-17 15:20:22 -0700 | [diff] [blame] | 418 | /** |
| 419 | * Verify that a fragment shader executable meets all semantic requirements |
| 420 | * |
| 421 | * \param shader Fragment shader executable to be verified |
| 422 | */ |
Ian Romanick | 832dfa5 | 2010-06-17 15:04:20 -0700 | [diff] [blame] | 423 | bool |
Eric Anholt | 849e181 | 2010-06-30 11:49:17 -0700 | [diff] [blame] | 424 | validate_fragment_shader_executable(struct gl_shader_program *prog, |
Eric Anholt | 16b68b1 | 2010-06-30 11:05:43 -0700 | [diff] [blame] | 425 | struct gl_shader *shader) |
Ian Romanick | 832dfa5 | 2010-06-17 15:04:20 -0700 | [diff] [blame] | 426 | { |
| 427 | if (shader == NULL) |
| 428 | return true; |
| 429 | |
Ian Romanick | 832dfa5 | 2010-06-17 15:04:20 -0700 | [diff] [blame] | 430 | find_assignment_visitor frag_color("gl_FragColor"); |
| 431 | find_assignment_visitor frag_data("gl_FragData"); |
| 432 | |
Eric Anholt | 16b68b1 | 2010-06-30 11:05:43 -0700 | [diff] [blame] | 433 | frag_color.run(shader->ir); |
| 434 | frag_data.run(shader->ir); |
Ian Romanick | 832dfa5 | 2010-06-17 15:04:20 -0700 | [diff] [blame] | 435 | |
Ian Romanick | 832dfa5 | 2010-06-17 15:04:20 -0700 | [diff] [blame] | 436 | if (frag_color.variable_found() && frag_data.variable_found()) { |
Ian Romanick | 586e741 | 2011-07-28 14:04:09 -0700 | [diff] [blame] | 437 | linker_error(prog, "fragment shader writes to both " |
| 438 | "`gl_FragColor' and `gl_FragData'\n"); |
Ian Romanick | 832dfa5 | 2010-06-17 15:04:20 -0700 | [diff] [blame] | 439 | return false; |
| 440 | } |
| 441 | |
| 442 | return true; |
| 443 | } |
| 444 | |
| 445 | |
Ian Romanick | cc22c5a | 2010-06-18 17:13:42 -0700 | [diff] [blame] | 446 | /** |
Ian Romanick | e2e5d0d | 2010-06-29 18:47:11 -0700 | [diff] [blame] | 447 | * Generate a string describing the mode of a variable |
| 448 | */ |
| 449 | static const char * |
| 450 | mode_string(const ir_variable *var) |
| 451 | { |
| 452 | switch (var->mode) { |
| 453 | case ir_var_auto: |
| 454 | return (var->read_only) ? "global constant" : "global variable"; |
| 455 | |
Paul Berry | 42a29d8 | 2013-01-11 14:39:32 -0800 | [diff] [blame] | 456 | case ir_var_uniform: return "uniform"; |
| 457 | case ir_var_shader_in: return "shader input"; |
| 458 | case ir_var_shader_out: return "shader output"; |
Ian Romanick | 7e2aa91 | 2010-07-19 17:12:42 -0700 | [diff] [blame] | 459 | |
Kenneth Graunke | 819d57f | 2011-01-12 15:37:37 -0800 | [diff] [blame] | 460 | case ir_var_const_in: |
Ian Romanick | 7e2aa91 | 2010-07-19 17:12:42 -0700 | [diff] [blame] | 461 | case ir_var_temporary: |
Ian Romanick | e2e5d0d | 2010-06-29 18:47:11 -0700 | [diff] [blame] | 462 | default: |
| 463 | assert(!"Should not get here."); |
| 464 | return "invalid variable"; |
| 465 | } |
| 466 | } |
| 467 | |
| 468 | |
| 469 | /** |
| 470 | * Perform validation of global variables used across multiple shaders |
Ian Romanick | cc22c5a | 2010-06-18 17:13:42 -0700 | [diff] [blame] | 471 | */ |
| 472 | bool |
Ian Romanick | e2e5d0d | 2010-06-29 18:47:11 -0700 | [diff] [blame] | 473 | cross_validate_globals(struct gl_shader_program *prog, |
| 474 | struct gl_shader **shader_list, |
| 475 | unsigned num_shaders, |
| 476 | bool uniforms_only) |
Ian Romanick | cc22c5a | 2010-06-18 17:13:42 -0700 | [diff] [blame] | 477 | { |
| 478 | /* Examine all of the uniforms in all of the shaders and cross validate |
| 479 | * them. |
| 480 | */ |
Ian Romanick | e2e5d0d | 2010-06-29 18:47:11 -0700 | [diff] [blame] | 481 | glsl_symbol_table variables; |
| 482 | for (unsigned i = 0; i < num_shaders; i++) { |
Ian Romanick | 3322fba | 2010-10-14 13:28:42 -0700 | [diff] [blame] | 483 | if (shader_list[i] == NULL) |
| 484 | continue; |
| 485 | |
Ian Romanick | e2e5d0d | 2010-06-29 18:47:11 -0700 | [diff] [blame] | 486 | foreach_list(node, shader_list[i]->ir) { |
Ian Romanick | cc22c5a | 2010-06-18 17:13:42 -0700 | [diff] [blame] | 487 | ir_variable *const var = ((ir_instruction *) node)->as_variable(); |
| 488 | |
Ian Romanick | e2e5d0d | 2010-06-29 18:47:11 -0700 | [diff] [blame] | 489 | if (var == NULL) |
Ian Romanick | cc22c5a | 2010-06-18 17:13:42 -0700 | [diff] [blame] | 490 | continue; |
| 491 | |
Ian Romanick | e2e5d0d | 2010-06-29 18:47:11 -0700 | [diff] [blame] | 492 | if (uniforms_only && (var->mode != ir_var_uniform)) |
| 493 | continue; |
| 494 | |
Ian Romanick | 7e2aa91 | 2010-07-19 17:12:42 -0700 | [diff] [blame] | 495 | /* Don't cross validate temporaries that are at global scope. These |
| 496 | * will eventually get pulled into the shaders 'main'. |
| 497 | */ |
| 498 | if (var->mode == ir_var_temporary) |
| 499 | continue; |
| 500 | |
Ian Romanick | e2e5d0d | 2010-06-29 18:47:11 -0700 | [diff] [blame] | 501 | /* If a global with this name has already been seen, verify that the |
| 502 | * new instance has the same type. In addition, if the globals have |
Ian Romanick | cc22c5a | 2010-06-18 17:13:42 -0700 | [diff] [blame] | 503 | * initializers, the values of the initializers must be the same. |
| 504 | */ |
Ian Romanick | e2e5d0d | 2010-06-29 18:47:11 -0700 | [diff] [blame] | 505 | ir_variable *const existing = variables.get_variable(var->name); |
Ian Romanick | cc22c5a | 2010-06-18 17:13:42 -0700 | [diff] [blame] | 506 | if (existing != NULL) { |
| 507 | if (var->type != existing->type) { |
Ian Romanick | a2711d6 | 2010-08-29 22:07:49 -0700 | [diff] [blame] | 508 | /* Consider the types to be "the same" if both types are arrays |
| 509 | * of the same type and one of the arrays is implicitly sized. |
| 510 | * In addition, set the type of the linked variable to the |
| 511 | * explicitly sized array. |
| 512 | */ |
| 513 | if (var->type->is_array() |
| 514 | && existing->type->is_array() |
| 515 | && (var->type->fields.array == existing->type->fields.array) |
| 516 | && ((var->type->length == 0) |
| 517 | || (existing->type->length == 0))) { |
Ian Romanick | 0f4b2a0 | 2011-01-25 12:06:18 -0800 | [diff] [blame] | 518 | if (var->type->length != 0) { |
Ian Romanick | a2711d6 | 2010-08-29 22:07:49 -0700 | [diff] [blame] | 519 | existing->type = var->type; |
Ian Romanick | 6f53921 | 2010-12-07 18:30:33 -0800 | [diff] [blame] | 520 | } |
Ian Romanick | a2711d6 | 2010-08-29 22:07:49 -0700 | [diff] [blame] | 521 | } else { |
Ian Romanick | 586e741 | 2011-07-28 14:04:09 -0700 | [diff] [blame] | 522 | linker_error(prog, "%s `%s' declared as type " |
| 523 | "`%s' and type `%s'\n", |
| 524 | mode_string(var), |
| 525 | var->name, var->type->name, |
| 526 | existing->type->name); |
Ian Romanick | a2711d6 | 2010-08-29 22:07:49 -0700 | [diff] [blame] | 527 | return false; |
| 528 | } |
Ian Romanick | cc22c5a | 2010-06-18 17:13:42 -0700 | [diff] [blame] | 529 | } |
| 530 | |
Ian Romanick | 68a4fc9 | 2010-10-07 17:21:22 -0700 | [diff] [blame] | 531 | if (var->explicit_location) { |
| 532 | if (existing->explicit_location |
| 533 | && (var->location != existing->location)) { |
Ian Romanick | 586e741 | 2011-07-28 14:04:09 -0700 | [diff] [blame] | 534 | linker_error(prog, "explicit locations for %s " |
| 535 | "`%s' have differing values\n", |
| 536 | mode_string(var), var->name); |
Ian Romanick | 68a4fc9 | 2010-10-07 17:21:22 -0700 | [diff] [blame] | 537 | return false; |
| 538 | } |
| 539 | |
| 540 | existing->location = var->location; |
| 541 | existing->explicit_location = true; |
| 542 | } |
| 543 | |
Kenneth Graunke | 9a9a830 | 2013-07-16 12:18:57 -0700 | [diff] [blame] | 544 | /* From the GLSL 4.20 specification: |
| 545 | * "A link error will result if two compilation units in a program |
| 546 | * specify different integer-constant bindings for the same |
| 547 | * opaque-uniform name. However, it is not an error to specify a |
| 548 | * binding on some but not all declarations for the same name" |
| 549 | */ |
| 550 | if (var->explicit_binding) { |
| 551 | if (existing->explicit_binding && |
| 552 | var->binding != existing->binding) { |
| 553 | linker_error(prog, "explicit bindings for %s " |
| 554 | "`%s' have differing values\n", |
| 555 | mode_string(var), var->name); |
| 556 | return false; |
| 557 | } |
| 558 | |
| 559 | existing->binding = var->binding; |
| 560 | existing->explicit_binding = true; |
| 561 | } |
| 562 | |
Ian Romanick | 46173f9 | 2011-10-31 13:07:06 -0700 | [diff] [blame] | 563 | /* Validate layout qualifiers for gl_FragDepth. |
| 564 | * |
| 565 | * From the AMD/ARB_conservative_depth specs: |
| 566 | * |
| 567 | * "If gl_FragDepth is redeclared in any fragment shader in a |
| 568 | * program, it must be redeclared in all fragment shaders in |
| 569 | * that program that have static assignments to |
| 570 | * gl_FragDepth. All redeclarations of gl_FragDepth in all |
| 571 | * fragment shaders in a single program must have the same set |
| 572 | * of qualifiers." |
| 573 | */ |
| 574 | if (strcmp(var->name, "gl_FragDepth") == 0) { |
| 575 | bool layout_declared = var->depth_layout != ir_depth_layout_none; |
| 576 | bool layout_differs = |
| 577 | var->depth_layout != existing->depth_layout; |
| 578 | |
| 579 | if (layout_declared && layout_differs) { |
| 580 | linker_error(prog, |
| 581 | "All redeclarations of gl_FragDepth in all " |
| 582 | "fragment shaders in a single program must have " |
| 583 | "the same set of qualifiers."); |
| 584 | } |
| 585 | |
| 586 | if (var->used && layout_differs) { |
| 587 | linker_error(prog, |
| 588 | "If gl_FragDepth is redeclared with a layout " |
| 589 | "qualifier in any fragment shader, it must be " |
| 590 | "redeclared with the same layout qualifier in " |
| 591 | "all fragment shaders that have assignments to " |
| 592 | "gl_FragDepth"); |
| 593 | } |
| 594 | } |
Chad Versace | addae33 | 2011-01-27 01:40:31 -0800 | [diff] [blame] | 595 | |
Ian Romanick | f37b1ad | 2011-10-31 14:31:07 -0700 | [diff] [blame] | 596 | /* Page 35 (page 41 of the PDF) of the GLSL 4.20 spec says: |
| 597 | * |
| 598 | * "If a shared global has multiple initializers, the |
| 599 | * initializers must all be constant expressions, and they |
| 600 | * must all have the same value. Otherwise, a link error will |
| 601 | * result. (A shared global having only one initializer does |
| 602 | * not require that initializer to be a constant expression.)" |
| 603 | * |
| 604 | * Previous to 4.20 the GLSL spec simply said that initializers |
| 605 | * must have the same value. In this case of non-constant |
| 606 | * initializers, this was impossible to determine. As a result, |
| 607 | * no vendor actually implemented that behavior. The 4.20 |
| 608 | * behavior matches the implemented behavior of at least one other |
| 609 | * vendor, so we'll implement that for all GLSL versions. |
Ian Romanick | e2e5d0d | 2010-06-29 18:47:11 -0700 | [diff] [blame] | 610 | */ |
Ian Romanick | f37b1ad | 2011-10-31 14:31:07 -0700 | [diff] [blame] | 611 | if (var->constant_initializer != NULL) { |
| 612 | if (existing->constant_initializer != NULL) { |
| 613 | if (!var->constant_initializer->has_value(existing->constant_initializer)) { |
Ian Romanick | 586e741 | 2011-07-28 14:04:09 -0700 | [diff] [blame] | 614 | linker_error(prog, "initializers for %s " |
| 615 | "`%s' have differing values\n", |
| 616 | mode_string(var), var->name); |
Ian Romanick | cc22c5a | 2010-06-18 17:13:42 -0700 | [diff] [blame] | 617 | return false; |
| 618 | } |
Ian Romanick | f37b1ad | 2011-10-31 14:31:07 -0700 | [diff] [blame] | 619 | } else { |
Ian Romanick | cc22c5a | 2010-06-18 17:13:42 -0700 | [diff] [blame] | 620 | /* If the first-seen instance of a particular uniform did not |
| 621 | * have an initializer but a later instance does, copy the |
| 622 | * initializer to the version stored in the symbol table. |
| 623 | */ |
Ian Romanick | de415b7 | 2010-07-14 13:22:12 -0700 | [diff] [blame] | 624 | /* FINISHME: This is wrong. The constant_value field should |
| 625 | * FINISHME: not be modified! Imagine a case where a shader |
| 626 | * FINISHME: without an initializer is linked in two different |
| 627 | * FINISHME: programs with shaders that have differing |
| 628 | * FINISHME: initializers. Linking with the first will |
| 629 | * FINISHME: modify the shader, and linking with the second |
| 630 | * FINISHME: will fail. |
| 631 | */ |
Ian Romanick | f37b1ad | 2011-10-31 14:31:07 -0700 | [diff] [blame] | 632 | existing->constant_initializer = |
| 633 | var->constant_initializer->clone(ralloc_parent(existing), |
| 634 | NULL); |
| 635 | } |
| 636 | } |
| 637 | |
| 638 | if (var->has_initializer) { |
| 639 | if (existing->has_initializer |
| 640 | && (var->constant_initializer == NULL |
| 641 | || existing->constant_initializer == NULL)) { |
| 642 | linker_error(prog, |
| 643 | "shared global variable `%s' has multiple " |
| 644 | "non-constant initializers.\n", |
| 645 | var->name); |
| 646 | return false; |
| 647 | } |
| 648 | |
| 649 | /* Some instance had an initializer, so keep track of that. In |
| 650 | * this location, all sorts of initializers (constant or |
| 651 | * otherwise) will propagate the existence to the variable |
| 652 | * stored in the symbol table. |
| 653 | */ |
| 654 | existing->has_initializer = true; |
Ian Romanick | cc22c5a | 2010-06-18 17:13:42 -0700 | [diff] [blame] | 655 | } |
Chad Versace | 7528f14 | 2010-11-17 14:34:38 -0800 | [diff] [blame] | 656 | |
| 657 | if (existing->invariant != var->invariant) { |
Ian Romanick | 586e741 | 2011-07-28 14:04:09 -0700 | [diff] [blame] | 658 | linker_error(prog, "declarations for %s `%s' have " |
| 659 | "mismatching invariant qualifiers\n", |
| 660 | mode_string(var), var->name); |
Chad Versace | 7528f14 | 2010-11-17 14:34:38 -0800 | [diff] [blame] | 661 | return false; |
| 662 | } |
Chad Versace | 61428dd | 2011-01-10 15:29:30 -0800 | [diff] [blame] | 663 | if (existing->centroid != var->centroid) { |
Ian Romanick | 586e741 | 2011-07-28 14:04:09 -0700 | [diff] [blame] | 664 | linker_error(prog, "declarations for %s `%s' have " |
| 665 | "mismatching centroid qualifiers\n", |
| 666 | mode_string(var), var->name); |
Chad Versace | 61428dd | 2011-01-10 15:29:30 -0800 | [diff] [blame] | 667 | return false; |
| 668 | } |
Ian Romanick | cc22c5a | 2010-06-18 17:13:42 -0700 | [diff] [blame] | 669 | } else |
Eric Anholt | 001eee5 | 2010-11-05 06:11:24 -0700 | [diff] [blame] | 670 | variables.add_variable(var); |
Ian Romanick | cc22c5a | 2010-06-18 17:13:42 -0700 | [diff] [blame] | 671 | } |
| 672 | } |
| 673 | |
| 674 | return true; |
| 675 | } |
| 676 | |
| 677 | |
Ian Romanick | 3710192 | 2010-06-18 19:02:10 -0700 | [diff] [blame] | 678 | /** |
Ian Romanick | e2e5d0d | 2010-06-29 18:47:11 -0700 | [diff] [blame] | 679 | * Perform validation of uniforms used across multiple shader stages |
| 680 | */ |
| 681 | bool |
| 682 | cross_validate_uniforms(struct gl_shader_program *prog) |
| 683 | { |
| 684 | return cross_validate_globals(prog, prog->_LinkedShaders, |
Ian Romanick | 3322fba | 2010-10-14 13:28:42 -0700 | [diff] [blame] | 685 | MESA_SHADER_TYPES, true); |
Ian Romanick | e2e5d0d | 2010-06-29 18:47:11 -0700 | [diff] [blame] | 686 | } |
| 687 | |
Eric Anholt | f609cf7 | 2012-04-27 13:52:56 -0700 | [diff] [blame] | 688 | /** |
| 689 | * Accumulates the array of prog->UniformBlocks and checks that all |
| 690 | * definitons of blocks agree on their contents. |
| 691 | */ |
| 692 | static bool |
| 693 | interstage_cross_validate_uniform_blocks(struct gl_shader_program *prog) |
| 694 | { |
| 695 | unsigned max_num_uniform_blocks = 0; |
| 696 | for (unsigned i = 0; i < MESA_SHADER_TYPES; i++) { |
| 697 | if (prog->_LinkedShaders[i]) |
| 698 | max_num_uniform_blocks += prog->_LinkedShaders[i]->NumUniformBlocks; |
| 699 | } |
| 700 | |
| 701 | for (unsigned i = 0; i < MESA_SHADER_TYPES; i++) { |
| 702 | struct gl_shader *sh = prog->_LinkedShaders[i]; |
| 703 | |
| 704 | prog->UniformBlockStageIndex[i] = ralloc_array(prog, int, |
| 705 | max_num_uniform_blocks); |
| 706 | for (unsigned int j = 0; j < max_num_uniform_blocks; j++) |
| 707 | prog->UniformBlockStageIndex[i][j] = -1; |
| 708 | |
| 709 | if (sh == NULL) |
| 710 | continue; |
| 711 | |
| 712 | for (unsigned int j = 0; j < sh->NumUniformBlocks; j++) { |
| 713 | int index = link_cross_validate_uniform_block(prog, |
| 714 | &prog->UniformBlocks, |
| 715 | &prog->NumUniformBlocks, |
| 716 | &sh->UniformBlocks[j]); |
| 717 | |
| 718 | if (index == -1) { |
| 719 | linker_error(prog, "uniform block `%s' has mismatching definitions", |
| 720 | sh->UniformBlocks[j].Name); |
| 721 | return false; |
| 722 | } |
| 723 | |
| 724 | prog->UniformBlockStageIndex[i][index] = j; |
| 725 | } |
| 726 | } |
| 727 | |
| 728 | return true; |
| 729 | } |
Ian Romanick | e2e5d0d | 2010-06-29 18:47:11 -0700 | [diff] [blame] | 730 | |
Ian Romanick | 3710192 | 2010-06-18 19:02:10 -0700 | [diff] [blame] | 731 | |
Ian Romanick | 3fb8787 | 2010-07-09 14:09:34 -0700 | [diff] [blame] | 732 | /** |
| 733 | * Populates a shaders symbol table with all global declarations |
| 734 | */ |
| 735 | static void |
| 736 | populate_symbol_table(gl_shader *sh) |
| 737 | { |
| 738 | sh->symbols = new(sh) glsl_symbol_table; |
| 739 | |
| 740 | foreach_list(node, sh->ir) { |
| 741 | ir_instruction *const inst = (ir_instruction *) node; |
| 742 | ir_variable *var; |
| 743 | ir_function *func; |
| 744 | |
| 745 | if ((func = inst->as_function()) != NULL) { |
Eric Anholt | e8f5ebf | 2010-11-05 06:08:45 -0700 | [diff] [blame] | 746 | sh->symbols->add_function(func); |
Ian Romanick | 3fb8787 | 2010-07-09 14:09:34 -0700 | [diff] [blame] | 747 | } else if ((var = inst->as_variable()) != NULL) { |
Eric Anholt | 001eee5 | 2010-11-05 06:11:24 -0700 | [diff] [blame] | 748 | sh->symbols->add_variable(var); |
Ian Romanick | 3fb8787 | 2010-07-09 14:09:34 -0700 | [diff] [blame] | 749 | } |
| 750 | } |
| 751 | } |
| 752 | |
| 753 | |
| 754 | /** |
Ian Romanick | 31a9786 | 2010-07-12 18:48:50 -0700 | [diff] [blame] | 755 | * Remap variables referenced in an instruction tree |
| 756 | * |
| 757 | * This is used when instruction trees are cloned from one shader and placed in |
| 758 | * another. These trees will contain references to \c ir_variable nodes that |
| 759 | * do not exist in the target shader. This function finds these \c ir_variable |
| 760 | * references and replaces the references with matching variables in the target |
| 761 | * shader. |
| 762 | * |
| 763 | * If there is no matching variable in the target shader, a clone of the |
| 764 | * \c ir_variable is made and added to the target shader. The new variable is |
| 765 | * added to \b both the instruction stream and the symbol table. |
| 766 | * |
| 767 | * \param inst IR tree that is to be processed. |
| 768 | * \param symbols Symbol table containing global scope symbols in the |
| 769 | * linked shader. |
| 770 | * \param instructions Instruction stream where new variable declarations |
| 771 | * should be added. |
| 772 | */ |
| 773 | void |
Eric Anholt | 8273bd4 | 2010-08-04 12:34:56 -0700 | [diff] [blame] | 774 | remap_variables(ir_instruction *inst, struct gl_shader *target, |
| 775 | hash_table *temps) |
Ian Romanick | 31a9786 | 2010-07-12 18:48:50 -0700 | [diff] [blame] | 776 | { |
| 777 | class remap_visitor : public ir_hierarchical_visitor { |
| 778 | public: |
Eric Anholt | 8273bd4 | 2010-08-04 12:34:56 -0700 | [diff] [blame] | 779 | remap_visitor(struct gl_shader *target, |
Ian Romanick | 7e2aa91 | 2010-07-19 17:12:42 -0700 | [diff] [blame] | 780 | hash_table *temps) |
Ian Romanick | 31a9786 | 2010-07-12 18:48:50 -0700 | [diff] [blame] | 781 | { |
Eric Anholt | 8273bd4 | 2010-08-04 12:34:56 -0700 | [diff] [blame] | 782 | this->target = target; |
| 783 | this->symbols = target->symbols; |
| 784 | this->instructions = target->ir; |
Ian Romanick | 7e2aa91 | 2010-07-19 17:12:42 -0700 | [diff] [blame] | 785 | this->temps = temps; |
Ian Romanick | 31a9786 | 2010-07-12 18:48:50 -0700 | [diff] [blame] | 786 | } |
| 787 | |
| 788 | virtual ir_visitor_status visit(ir_dereference_variable *ir) |
| 789 | { |
Ian Romanick | 7e2aa91 | 2010-07-19 17:12:42 -0700 | [diff] [blame] | 790 | if (ir->var->mode == ir_var_temporary) { |
| 791 | ir_variable *var = (ir_variable *) hash_table_find(temps, ir->var); |
| 792 | |
| 793 | assert(var != NULL); |
| 794 | ir->var = var; |
| 795 | return visit_continue; |
| 796 | } |
| 797 | |
Ian Romanick | 31a9786 | 2010-07-12 18:48:50 -0700 | [diff] [blame] | 798 | ir_variable *const existing = |
| 799 | this->symbols->get_variable(ir->var->name); |
| 800 | if (existing != NULL) |
| 801 | ir->var = existing; |
| 802 | else { |
Eric Anholt | 8273bd4 | 2010-08-04 12:34:56 -0700 | [diff] [blame] | 803 | ir_variable *copy = ir->var->clone(this->target, NULL); |
Ian Romanick | 31a9786 | 2010-07-12 18:48:50 -0700 | [diff] [blame] | 804 | |
Eric Anholt | 001eee5 | 2010-11-05 06:11:24 -0700 | [diff] [blame] | 805 | this->symbols->add_variable(copy); |
Ian Romanick | 31a9786 | 2010-07-12 18:48:50 -0700 | [diff] [blame] | 806 | this->instructions->push_head(copy); |
Ian Romanick | 7e2aa91 | 2010-07-19 17:12:42 -0700 | [diff] [blame] | 807 | ir->var = copy; |
Ian Romanick | 31a9786 | 2010-07-12 18:48:50 -0700 | [diff] [blame] | 808 | } |
| 809 | |
| 810 | return visit_continue; |
| 811 | } |
| 812 | |
| 813 | private: |
Eric Anholt | 8273bd4 | 2010-08-04 12:34:56 -0700 | [diff] [blame] | 814 | struct gl_shader *target; |
Ian Romanick | 31a9786 | 2010-07-12 18:48:50 -0700 | [diff] [blame] | 815 | glsl_symbol_table *symbols; |
| 816 | exec_list *instructions; |
Ian Romanick | 7e2aa91 | 2010-07-19 17:12:42 -0700 | [diff] [blame] | 817 | hash_table *temps; |
Ian Romanick | 31a9786 | 2010-07-12 18:48:50 -0700 | [diff] [blame] | 818 | }; |
| 819 | |
Eric Anholt | 8273bd4 | 2010-08-04 12:34:56 -0700 | [diff] [blame] | 820 | remap_visitor v(target, temps); |
Ian Romanick | 31a9786 | 2010-07-12 18:48:50 -0700 | [diff] [blame] | 821 | |
| 822 | inst->accept(&v); |
| 823 | } |
| 824 | |
| 825 | |
| 826 | /** |
| 827 | * Move non-declarations from one instruction stream to another |
| 828 | * |
| 829 | * 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] | 830 | * 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] | 831 | * pointer) for \c last and \c false for \c make_copies on the first |
| 832 | * call. Successive calls pass the return value of the previous call for |
| 833 | * \c last and \c true for \c make_copies. |
| 834 | * |
| 835 | * \param instructions Source instruction stream |
| 836 | * \param last Instruction after which new instructions should be |
| 837 | * inserted in the target instruction stream |
| 838 | * \param make_copies Flag selecting whether instructions in \c instructions |
| 839 | * should be copied (via \c ir_instruction::clone) into the |
| 840 | * target list or moved. |
| 841 | * |
| 842 | * \return |
| 843 | * The new "last" instruction in the target instruction stream. This pointer |
| 844 | * is suitable for use as the \c last parameter of a later call to this |
| 845 | * function. |
| 846 | */ |
| 847 | exec_node * |
| 848 | move_non_declarations(exec_list *instructions, exec_node *last, |
| 849 | bool make_copies, gl_shader *target) |
| 850 | { |
Ian Romanick | 7e2aa91 | 2010-07-19 17:12:42 -0700 | [diff] [blame] | 851 | hash_table *temps = NULL; |
| 852 | |
| 853 | if (make_copies) |
| 854 | temps = hash_table_ctor(0, hash_table_pointer_hash, |
| 855 | hash_table_pointer_compare); |
| 856 | |
Ian Romanick | 303c99f | 2010-07-19 12:34:56 -0700 | [diff] [blame] | 857 | foreach_list_safe(node, instructions) { |
Ian Romanick | 31a9786 | 2010-07-12 18:48:50 -0700 | [diff] [blame] | 858 | ir_instruction *inst = (ir_instruction *) node; |
| 859 | |
Ian Romanick | 7e2aa91 | 2010-07-19 17:12:42 -0700 | [diff] [blame] | 860 | if (inst->as_function()) |
Ian Romanick | 31a9786 | 2010-07-12 18:48:50 -0700 | [diff] [blame] | 861 | continue; |
| 862 | |
Ian Romanick | 7e2aa91 | 2010-07-19 17:12:42 -0700 | [diff] [blame] | 863 | ir_variable *var = inst->as_variable(); |
| 864 | if ((var != NULL) && (var->mode != ir_var_temporary)) |
| 865 | continue; |
| 866 | |
| 867 | assert(inst->as_assignment() |
Kenneth Graunke | d884f60 | 2012-03-20 15:56:37 -0700 | [diff] [blame] | 868 | || inst->as_call() |
Kenneth Graunke | b45a68e | 2012-10-24 13:17:24 -0700 | [diff] [blame] | 869 | || inst->as_if() /* for initializers with the ?: operator */ |
Ian Romanick | 7e2aa91 | 2010-07-19 17:12:42 -0700 | [diff] [blame] | 870 | || ((var != NULL) && (var->mode == ir_var_temporary))); |
Ian Romanick | 31a9786 | 2010-07-12 18:48:50 -0700 | [diff] [blame] | 871 | |
| 872 | if (make_copies) { |
Eric Anholt | 8273bd4 | 2010-08-04 12:34:56 -0700 | [diff] [blame] | 873 | inst = inst->clone(target, NULL); |
Ian Romanick | 7e2aa91 | 2010-07-19 17:12:42 -0700 | [diff] [blame] | 874 | |
| 875 | if (var != NULL) |
| 876 | hash_table_insert(temps, inst, var); |
| 877 | else |
Eric Anholt | 8273bd4 | 2010-08-04 12:34:56 -0700 | [diff] [blame] | 878 | remap_variables(inst, target, temps); |
Ian Romanick | 31a9786 | 2010-07-12 18:48:50 -0700 | [diff] [blame] | 879 | } else { |
| 880 | inst->remove(); |
| 881 | } |
| 882 | |
| 883 | last->insert_after(inst); |
| 884 | last = inst; |
| 885 | } |
| 886 | |
Ian Romanick | 7e2aa91 | 2010-07-19 17:12:42 -0700 | [diff] [blame] | 887 | if (make_copies) |
| 888 | hash_table_dtor(temps); |
| 889 | |
Ian Romanick | 31a9786 | 2010-07-12 18:48:50 -0700 | [diff] [blame] | 890 | return last; |
| 891 | } |
| 892 | |
| 893 | /** |
Ian Romanick | 15ce87e | 2010-07-09 15:28:22 -0700 | [diff] [blame] | 894 | * Get the function signature for main from a shader |
| 895 | */ |
| 896 | static ir_function_signature * |
| 897 | get_main_function_signature(gl_shader *sh) |
| 898 | { |
| 899 | ir_function *const f = sh->symbols->get_function("main"); |
| 900 | if (f != NULL) { |
| 901 | exec_list void_parameters; |
| 902 | |
| 903 | /* Look for the 'void main()' signature and ensure that it's defined. |
| 904 | * This keeps the linker from accidentally pick a shader that just |
| 905 | * contains a prototype for main. |
| 906 | * |
| 907 | * We don't have to check for multiple definitions of main (in multiple |
| 908 | * shaders) because that would have already been caught above. |
| 909 | */ |
| 910 | ir_function_signature *sig = f->matching_signature(&void_parameters); |
| 911 | if ((sig != NULL) && sig->is_defined) { |
| 912 | return sig; |
| 913 | } |
| 914 | } |
| 915 | |
| 916 | return NULL; |
| 917 | } |
| 918 | |
| 919 | |
| 920 | /** |
Brian Paul | 84a1273 | 2012-02-02 20:10:40 -0700 | [diff] [blame] | 921 | * This class is only used in link_intrastage_shaders() below but declaring |
| 922 | * it inside that function leads to compiler warnings with some versions of |
| 923 | * gcc. |
| 924 | */ |
| 925 | class array_sizing_visitor : public ir_hierarchical_visitor { |
| 926 | public: |
| 927 | virtual ir_visitor_status visit(ir_variable *var) |
| 928 | { |
| 929 | if (var->type->is_array() && (var->type->length == 0)) { |
| 930 | const glsl_type *type = |
| 931 | glsl_type::get_array_instance(var->type->fields.array, |
| 932 | var->max_array_access + 1); |
| 933 | assert(type != NULL); |
| 934 | var->type = type; |
| 935 | } |
| 936 | return visit_continue; |
| 937 | } |
| 938 | }; |
| 939 | |
Brian Paul | 84a1273 | 2012-02-02 20:10:40 -0700 | [diff] [blame] | 940 | /** |
Ian Romanick | 3fb8787 | 2010-07-09 14:09:34 -0700 | [diff] [blame] | 941 | * Combine a group of shaders for a single stage to generate a linked shader |
| 942 | * |
| 943 | * \note |
| 944 | * If this function is supplied a single shader, it is cloned, and the new |
| 945 | * shader is returned. |
| 946 | */ |
| 947 | static struct gl_shader * |
Kenneth Graunke | 2da02e7 | 2010-11-17 11:03:57 -0800 | [diff] [blame] | 948 | link_intrastage_shaders(void *mem_ctx, |
| 949 | struct gl_context *ctx, |
Eric Anholt | 5d0f430 | 2010-08-18 12:02:35 -0700 | [diff] [blame] | 950 | struct gl_shader_program *prog, |
Ian Romanick | 3fb8787 | 2010-07-09 14:09:34 -0700 | [diff] [blame] | 951 | struct gl_shader **shader_list, |
| 952 | unsigned num_shaders) |
| 953 | { |
Eric Anholt | f609cf7 | 2012-04-27 13:52:56 -0700 | [diff] [blame] | 954 | struct gl_uniform_block *uniform_blocks = NULL; |
Eric Anholt | f609cf7 | 2012-04-27 13:52:56 -0700 | [diff] [blame] | 955 | |
Ian Romanick | 13f782c | 2010-06-29 18:53:38 -0700 | [diff] [blame] | 956 | /* Check that global variables defined in multiple shaders are consistent. |
| 957 | */ |
| 958 | if (!cross_validate_globals(prog, shader_list, num_shaders, false)) |
| 959 | return NULL; |
| 960 | |
Jordan Justen | 4a0bcd9 | 2013-05-20 23:42:49 -0700 | [diff] [blame] | 961 | /* Check that interface blocks defined in multiple shaders are consistent. |
| 962 | */ |
Paul Berry | 659ec1c | 2013-07-27 14:58:43 -0700 | [diff] [blame^] | 963 | if (!validate_intrastage_interface_blocks(prog, |
| 964 | (const gl_shader **)shader_list, |
Jordan Justen | 4a0bcd9 | 2013-05-20 23:42:49 -0700 | [diff] [blame] | 965 | num_shaders)) |
| 966 | return NULL; |
| 967 | |
Paul Berry | 4682b9b | 2013-07-27 15:07:08 -0700 | [diff] [blame] | 968 | /* Link up uniform blocks defined within this stage. */ |
| 969 | const unsigned num_uniform_blocks = |
Ian Romanick | 514f8c7 | 2013-01-22 01:09:16 -0500 | [diff] [blame] | 970 | link_uniform_blocks(mem_ctx, prog, shader_list, num_shaders, |
| 971 | &uniform_blocks); |
Eric Anholt | f609cf7 | 2012-04-27 13:52:56 -0700 | [diff] [blame] | 972 | |
Ian Romanick | 13f782c | 2010-06-29 18:53:38 -0700 | [diff] [blame] | 973 | /* Check that there is only a single definition of each function signature |
| 974 | * across all shaders. |
| 975 | */ |
| 976 | for (unsigned i = 0; i < (num_shaders - 1); i++) { |
| 977 | foreach_list(node, shader_list[i]->ir) { |
| 978 | ir_function *const f = ((ir_instruction *) node)->as_function(); |
| 979 | |
| 980 | if (f == NULL) |
| 981 | continue; |
| 982 | |
| 983 | for (unsigned j = i + 1; j < num_shaders; j++) { |
| 984 | ir_function *const other = |
| 985 | shader_list[j]->symbols->get_function(f->name); |
| 986 | |
| 987 | /* If the other shader has no function (and therefore no function |
| 988 | * signatures) with the same name, skip to the next shader. |
| 989 | */ |
| 990 | if (other == NULL) |
| 991 | continue; |
| 992 | |
| 993 | foreach_iter (exec_list_iterator, iter, *f) { |
| 994 | ir_function_signature *sig = |
| 995 | (ir_function_signature *) iter.get(); |
| 996 | |
Kenneth Graunke | f412fac | 2010-09-05 01:48:11 -0700 | [diff] [blame] | 997 | if (!sig->is_defined || sig->is_builtin) |
Ian Romanick | 13f782c | 2010-06-29 18:53:38 -0700 | [diff] [blame] | 998 | continue; |
| 999 | |
| 1000 | ir_function_signature *other_sig = |
| 1001 | other->exact_matching_signature(& sig->parameters); |
| 1002 | |
| 1003 | if ((other_sig != NULL) && other_sig->is_defined |
Kenneth Graunke | f412fac | 2010-09-05 01:48:11 -0700 | [diff] [blame] | 1004 | && !other_sig->is_builtin) { |
Ian Romanick | 586e741 | 2011-07-28 14:04:09 -0700 | [diff] [blame] | 1005 | linker_error(prog, "function `%s' is multiply defined", |
| 1006 | f->name); |
Ian Romanick | 13f782c | 2010-06-29 18:53:38 -0700 | [diff] [blame] | 1007 | return NULL; |
| 1008 | } |
| 1009 | } |
| 1010 | } |
| 1011 | } |
| 1012 | } |
| 1013 | |
| 1014 | /* Find the shader that defines main, and make a clone of it. |
| 1015 | * |
| 1016 | * Starting with the clone, search for undefined references. If one is |
| 1017 | * found, find the shader that defines it. Clone the reference and add |
| 1018 | * it to the shader. Repeat until there are no undefined references or |
| 1019 | * until a reference cannot be resolved. |
| 1020 | */ |
Ian Romanick | 15ce87e | 2010-07-09 15:28:22 -0700 | [diff] [blame] | 1021 | gl_shader *main = NULL; |
| 1022 | for (unsigned i = 0; i < num_shaders; i++) { |
| 1023 | if (get_main_function_signature(shader_list[i]) != NULL) { |
| 1024 | main = shader_list[i]; |
| 1025 | break; |
| 1026 | } |
| 1027 | } |
Ian Romanick | 13f782c | 2010-06-29 18:53:38 -0700 | [diff] [blame] | 1028 | |
Ian Romanick | 15ce87e | 2010-07-09 15:28:22 -0700 | [diff] [blame] | 1029 | if (main == NULL) { |
Ian Romanick | 586e741 | 2011-07-28 14:04:09 -0700 | [diff] [blame] | 1030 | linker_error(prog, "%s shader lacks `main'\n", |
Eric Anholt | faf3dba | 2013-06-12 16:57:11 -0700 | [diff] [blame] | 1031 | _mesa_glsl_shader_target_name(shader_list[0]->Type)); |
Ian Romanick | 15ce87e | 2010-07-09 15:28:22 -0700 | [diff] [blame] | 1032 | return NULL; |
| 1033 | } |
| 1034 | |
Ian Romanick | 4a45595 | 2010-10-13 15:13:02 -0700 | [diff] [blame] | 1035 | gl_shader *linked = ctx->Driver.NewShader(NULL, 0, main->Type); |
Ian Romanick | 15ce87e | 2010-07-09 15:28:22 -0700 | [diff] [blame] | 1036 | linked->ir = new(linked) exec_list; |
Kenneth Graunke | 2da02e7 | 2010-11-17 11:03:57 -0800 | [diff] [blame] | 1037 | clone_ir_list(mem_ctx, linked->ir, main->ir); |
Ian Romanick | 15ce87e | 2010-07-09 15:28:22 -0700 | [diff] [blame] | 1038 | |
Eric Anholt | f609cf7 | 2012-04-27 13:52:56 -0700 | [diff] [blame] | 1039 | linked->UniformBlocks = uniform_blocks; |
| 1040 | linked->NumUniformBlocks = num_uniform_blocks; |
| 1041 | ralloc_steal(linked, linked->UniformBlocks); |
| 1042 | |
Ian Romanick | 15ce87e | 2010-07-09 15:28:22 -0700 | [diff] [blame] | 1043 | populate_symbol_table(linked); |
Ian Romanick | 13f782c | 2010-06-29 18:53:38 -0700 | [diff] [blame] | 1044 | |
Ian Romanick | 31a9786 | 2010-07-12 18:48:50 -0700 | [diff] [blame] | 1045 | /* The a pointer to the main function in the final linked shader (i.e., the |
| 1046 | * copy of the original shader that contained the main function). |
| 1047 | */ |
| 1048 | ir_function_signature *const main_sig = get_main_function_signature(linked); |
| 1049 | |
| 1050 | /* Move any instructions other than variable declarations or function |
| 1051 | * declarations into main. |
| 1052 | */ |
Ian Romanick | 9303e35 | 2010-07-19 12:33:54 -0700 | [diff] [blame] | 1053 | exec_node *insertion_point = |
| 1054 | move_non_declarations(linked->ir, (exec_node *) &main_sig->body, false, |
| 1055 | linked); |
| 1056 | |
Ian Romanick | 31a9786 | 2010-07-12 18:48:50 -0700 | [diff] [blame] | 1057 | for (unsigned i = 0; i < num_shaders; i++) { |
Ian Romanick | 9303e35 | 2010-07-19 12:33:54 -0700 | [diff] [blame] | 1058 | if (shader_list[i] == main) |
| 1059 | continue; |
| 1060 | |
Ian Romanick | 31a9786 | 2010-07-12 18:48:50 -0700 | [diff] [blame] | 1061 | insertion_point = move_non_declarations(shader_list[i]->ir, |
Ian Romanick | 9303e35 | 2010-07-19 12:33:54 -0700 | [diff] [blame] | 1062 | insertion_point, true, linked); |
Ian Romanick | 31a9786 | 2010-07-12 18:48:50 -0700 | [diff] [blame] | 1063 | } |
| 1064 | |
Ian Romanick | 13f782c | 2010-06-29 18:53:38 -0700 | [diff] [blame] | 1065 | /* Resolve initializers for global variables in the linked shader. |
| 1066 | */ |
Ian Romanick | d5be2ac | 2010-07-20 11:29:46 -0700 | [diff] [blame] | 1067 | unsigned num_linking_shaders = num_shaders; |
| 1068 | for (unsigned i = 0; i < num_shaders; i++) |
| 1069 | num_linking_shaders += shader_list[i]->num_builtins_to_link; |
| 1070 | |
| 1071 | gl_shader **linking_shaders = |
| 1072 | (gl_shader **) calloc(num_linking_shaders, sizeof(gl_shader *)); |
| 1073 | |
| 1074 | memcpy(linking_shaders, shader_list, |
| 1075 | sizeof(linking_shaders[0]) * num_shaders); |
| 1076 | |
| 1077 | unsigned idx = num_shaders; |
| 1078 | for (unsigned i = 0; i < num_shaders; i++) { |
| 1079 | memcpy(&linking_shaders[idx], shader_list[i]->builtins_to_link, |
| 1080 | sizeof(linking_shaders[0]) * shader_list[i]->num_builtins_to_link); |
| 1081 | idx += shader_list[i]->num_builtins_to_link; |
| 1082 | } |
| 1083 | |
| 1084 | assert(idx == num_linking_shaders); |
| 1085 | |
Ian Romanick | 4a45595 | 2010-10-13 15:13:02 -0700 | [diff] [blame] | 1086 | if (!link_function_calls(prog, linked, linking_shaders, |
| 1087 | num_linking_shaders)) { |
| 1088 | ctx->Driver.DeleteShader(ctx, linked); |
| 1089 | linked = NULL; |
| 1090 | } |
Ian Romanick | d5be2ac | 2010-07-20 11:29:46 -0700 | [diff] [blame] | 1091 | |
| 1092 | free(linking_shaders); |
Ian Romanick | 3fb8787 | 2010-07-09 14:09:34 -0700 | [diff] [blame] | 1093 | |
Paul Berry | c148ef6 | 2011-08-03 15:37:01 -0700 | [diff] [blame] | 1094 | /* At this point linked should contain all of the linked IR, so |
| 1095 | * validate it to make sure nothing went wrong. |
| 1096 | */ |
| 1097 | if (linked) |
| 1098 | validate_ir_tree(linked->ir); |
Paul Berry | c148ef6 | 2011-08-03 15:37:01 -0700 | [diff] [blame] | 1099 | |
Ian Romanick | c87e9ef | 2011-01-25 12:04:08 -0800 | [diff] [blame] | 1100 | /* Make a pass over all variable declarations to ensure that arrays with |
Ian Romanick | 6f53921 | 2010-12-07 18:30:33 -0800 | [diff] [blame] | 1101 | * unspecified sizes have a size specified. The size is inferred from the |
| 1102 | * max_array_access field. |
| 1103 | */ |
Ian Romanick | 002cd2c | 2010-12-07 19:00:44 -0800 | [diff] [blame] | 1104 | if (linked != NULL) { |
Brian Paul | 84a1273 | 2012-02-02 20:10:40 -0700 | [diff] [blame] | 1105 | array_sizing_visitor v; |
Ian Romanick | 6f53921 | 2010-12-07 18:30:33 -0800 | [diff] [blame] | 1106 | |
Ian Romanick | c87e9ef | 2011-01-25 12:04:08 -0800 | [diff] [blame] | 1107 | v.run(linked->ir); |
Ian Romanick | 6f53921 | 2010-12-07 18:30:33 -0800 | [diff] [blame] | 1108 | } |
| 1109 | |
Ian Romanick | 3fb8787 | 2010-07-09 14:09:34 -0700 | [diff] [blame] | 1110 | return linked; |
| 1111 | } |
| 1112 | |
Eric Anholt | a721abf | 2010-08-23 10:32:01 -0700 | [diff] [blame] | 1113 | /** |
| 1114 | * Update the sizes of linked shader uniform arrays to the maximum |
| 1115 | * array index used. |
| 1116 | * |
| 1117 | * From page 81 (page 95 of the PDF) of the OpenGL 2.1 spec: |
| 1118 | * |
| 1119 | * If one or more elements of an array are active, |
| 1120 | * GetActiveUniform will return the name of the array in name, |
| 1121 | * subject to the restrictions listed above. The type of the array |
| 1122 | * is returned in type. The size parameter contains the highest |
| 1123 | * array element index used, plus one. The compiler or linker |
| 1124 | * determines the highest index used. There will be only one |
| 1125 | * active uniform reported by the GL per uniform array. |
| 1126 | |
| 1127 | */ |
| 1128 | static void |
Eric Anholt | 586b4b5 | 2010-09-28 14:32:16 -0700 | [diff] [blame] | 1129 | update_array_sizes(struct gl_shader_program *prog) |
Eric Anholt | a721abf | 2010-08-23 10:32:01 -0700 | [diff] [blame] | 1130 | { |
Ian Romanick | 3322fba | 2010-10-14 13:28:42 -0700 | [diff] [blame] | 1131 | for (unsigned i = 0; i < MESA_SHADER_TYPES; i++) { |
| 1132 | if (prog->_LinkedShaders[i] == NULL) |
| 1133 | continue; |
| 1134 | |
Eric Anholt | a721abf | 2010-08-23 10:32:01 -0700 | [diff] [blame] | 1135 | foreach_list(node, prog->_LinkedShaders[i]->ir) { |
| 1136 | ir_variable *const var = ((ir_instruction *) node)->as_variable(); |
| 1137 | |
Eric Anholt | 586b4b5 | 2010-09-28 14:32:16 -0700 | [diff] [blame] | 1138 | if ((var == NULL) || (var->mode != ir_var_uniform && |
Paul Berry | 42a29d8 | 2013-01-11 14:39:32 -0800 | [diff] [blame] | 1139 | var->mode != ir_var_shader_in && |
| 1140 | var->mode != ir_var_shader_out) || |
Eric Anholt | a721abf | 2010-08-23 10:32:01 -0700 | [diff] [blame] | 1141 | !var->type->is_array()) |
| 1142 | continue; |
| 1143 | |
Eric Anholt | 9feb403 | 2012-05-01 14:43:31 -0700 | [diff] [blame] | 1144 | /* GL_ARB_uniform_buffer_object says that std140 uniforms |
| 1145 | * will not be eliminated. Since we always do std140, just |
| 1146 | * don't resize arrays in UBOs. |
| 1147 | */ |
Ian Romanick | 13be1f4 | 2012-12-14 12:00:14 -0800 | [diff] [blame] | 1148 | if (var->is_in_uniform_block()) |
Eric Anholt | 9feb403 | 2012-05-01 14:43:31 -0700 | [diff] [blame] | 1149 | continue; |
| 1150 | |
Eric Anholt | a721abf | 2010-08-23 10:32:01 -0700 | [diff] [blame] | 1151 | unsigned int size = var->max_array_access; |
Ian Romanick | 3322fba | 2010-10-14 13:28:42 -0700 | [diff] [blame] | 1152 | for (unsigned j = 0; j < MESA_SHADER_TYPES; j++) { |
| 1153 | if (prog->_LinkedShaders[j] == NULL) |
| 1154 | continue; |
| 1155 | |
Eric Anholt | a721abf | 2010-08-23 10:32:01 -0700 | [diff] [blame] | 1156 | foreach_list(node2, prog->_LinkedShaders[j]->ir) { |
| 1157 | ir_variable *other_var = ((ir_instruction *) node2)->as_variable(); |
| 1158 | if (!other_var) |
| 1159 | continue; |
| 1160 | |
| 1161 | if (strcmp(var->name, other_var->name) == 0 && |
| 1162 | other_var->max_array_access > size) { |
| 1163 | size = other_var->max_array_access; |
| 1164 | } |
| 1165 | } |
| 1166 | } |
Eric Anholt | 586b4b5 | 2010-09-28 14:32:16 -0700 | [diff] [blame] | 1167 | |
Fabian Bieler | 6368478 | 2013-06-14 13:37:07 +0200 | [diff] [blame] | 1168 | if (size + 1 != var->type->length) { |
Ian Romanick | 89d81ab | 2011-01-25 10:41:20 -0800 | [diff] [blame] | 1169 | /* If this is a built-in uniform (i.e., it's backed by some |
| 1170 | * fixed-function state), adjust the number of state slots to |
| 1171 | * match the new array size. The number of slots per array entry |
Bryan Cain | f18a086 | 2011-04-23 19:29:15 -0500 | [diff] [blame] | 1172 | * 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] | 1173 | * slots is an integer multiple of the number of array elements. |
| 1174 | * Determine the number of slots per array element by dividing by |
| 1175 | * the old (total) size. |
| 1176 | */ |
| 1177 | if (var->num_state_slots > 0) { |
| 1178 | var->num_state_slots = (size + 1) |
| 1179 | * (var->num_state_slots / var->type->length); |
| 1180 | } |
| 1181 | |
Eric Anholt | a721abf | 2010-08-23 10:32:01 -0700 | [diff] [blame] | 1182 | var->type = glsl_type::get_array_instance(var->type->fields.array, |
| 1183 | size + 1); |
| 1184 | /* FINISHME: We should update the types of array |
| 1185 | * dereferences of this variable now. |
| 1186 | */ |
| 1187 | } |
| 1188 | } |
| 1189 | } |
| 1190 | } |
| 1191 | |
Ian Romanick | 6984670 | 2010-06-22 17:29:19 -0700 | [diff] [blame] | 1192 | /** |
Bryan Cain | f18a086 | 2011-04-23 19:29:15 -0500 | [diff] [blame] | 1193 | * Find a contiguous set of available bits in a bitmask. |
Ian Romanick | 6984670 | 2010-06-22 17:29:19 -0700 | [diff] [blame] | 1194 | * |
| 1195 | * \param used_mask Bits representing used (1) and unused (0) locations |
| 1196 | * \param needed_count Number of contiguous bits needed. |
| 1197 | * |
| 1198 | * \return |
| 1199 | * Base location of the available bits on success or -1 on failure. |
| 1200 | */ |
| 1201 | int |
| 1202 | find_available_slots(unsigned used_mask, unsigned needed_count) |
| 1203 | { |
| 1204 | unsigned needed_mask = (1 << needed_count) - 1; |
| 1205 | const int max_bit_to_test = (8 * sizeof(used_mask)) - needed_count; |
| 1206 | |
| 1207 | /* The comparison to 32 is redundant, but without it GCC emits "warning: |
| 1208 | * cannot optimize possibly infinite loops" for the loop below. |
| 1209 | */ |
| 1210 | if ((needed_count == 0) || (max_bit_to_test < 0) || (max_bit_to_test > 32)) |
| 1211 | return -1; |
| 1212 | |
| 1213 | for (int i = 0; i <= max_bit_to_test; i++) { |
| 1214 | if ((needed_mask & ~used_mask) == needed_mask) |
| 1215 | return i; |
| 1216 | |
| 1217 | needed_mask <<= 1; |
| 1218 | } |
| 1219 | |
| 1220 | return -1; |
| 1221 | } |
| 1222 | |
| 1223 | |
Ian Romanick | d32d4f7 | 2011-06-27 17:59:58 -0700 | [diff] [blame] | 1224 | /** |
| 1225 | * Assign locations for either VS inputs for FS outputs |
| 1226 | * |
| 1227 | * \param prog Shader program whose variables need locations assigned |
| 1228 | * \param target_index Selector for the program target to receive location |
| 1229 | * assignmnets. Must be either \c MESA_SHADER_VERTEX or |
| 1230 | * \c MESA_SHADER_FRAGMENT. |
| 1231 | * \param max_index Maximum number of generic locations. This corresponds |
| 1232 | * to either the maximum number of draw buffers or the |
| 1233 | * maximum number of generic attributes. |
| 1234 | * |
| 1235 | * \return |
| 1236 | * If locations are successfully assigned, true is returned. Otherwise an |
| 1237 | * error is emitted to the shader link log and false is returned. |
Ian Romanick | d32d4f7 | 2011-06-27 17:59:58 -0700 | [diff] [blame] | 1238 | */ |
Ian Romanick | 6984670 | 2010-06-22 17:29:19 -0700 | [diff] [blame] | 1239 | bool |
Ian Romanick | d32d4f7 | 2011-06-27 17:59:58 -0700 | [diff] [blame] | 1240 | assign_attribute_or_color_locations(gl_shader_program *prog, |
| 1241 | unsigned target_index, |
| 1242 | unsigned max_index) |
Ian Romanick | 0ad22cd | 2010-06-21 17:18:31 -0700 | [diff] [blame] | 1243 | { |
Ian Romanick | d32d4f7 | 2011-06-27 17:59:58 -0700 | [diff] [blame] | 1244 | /* Mark invalid locations as being used. |
Ian Romanick | 9342d26 | 2010-06-22 17:41:37 -0700 | [diff] [blame] | 1245 | */ |
Ian Romanick | d32d4f7 | 2011-06-27 17:59:58 -0700 | [diff] [blame] | 1246 | unsigned used_locations = (max_index >= 32) |
| 1247 | ? ~0 : ~((1 << max_index) - 1); |
Ian Romanick | 0ad22cd | 2010-06-21 17:18:31 -0700 | [diff] [blame] | 1248 | |
Ian Romanick | d32d4f7 | 2011-06-27 17:59:58 -0700 | [diff] [blame] | 1249 | assert((target_index == MESA_SHADER_VERTEX) |
| 1250 | || (target_index == MESA_SHADER_FRAGMENT)); |
| 1251 | |
| 1252 | gl_shader *const sh = prog->_LinkedShaders[target_index]; |
| 1253 | if (sh == NULL) |
| 1254 | return true; |
Ian Romanick | 0ad22cd | 2010-06-21 17:18:31 -0700 | [diff] [blame] | 1255 | |
Ian Romanick | 6984670 | 2010-06-22 17:29:19 -0700 | [diff] [blame] | 1256 | /* Operate in a total of four passes. |
Ian Romanick | 0ad22cd | 2010-06-21 17:18:31 -0700 | [diff] [blame] | 1257 | * |
| 1258 | * 1. Invalidate the location assignments for all vertex shader inputs. |
| 1259 | * |
| 1260 | * 2. Assign locations for inputs that have user-defined (via |
Ian Romanick | b12b5d9 | 2011-11-04 16:08:52 -0700 | [diff] [blame] | 1261 | * glBindVertexAttribLocation) locations and outputs that have |
| 1262 | * user-defined locations (via glBindFragDataLocation). |
Ian Romanick | 0ad22cd | 2010-06-21 17:18:31 -0700 | [diff] [blame] | 1263 | * |
Ian Romanick | 6984670 | 2010-06-22 17:29:19 -0700 | [diff] [blame] | 1264 | * 3. Sort the attributes without assigned locations by number of slots |
| 1265 | * required in decreasing order. Fragmentation caused by attribute |
| 1266 | * locations assigned by the application may prevent large attributes |
| 1267 | * from having enough contiguous space. |
| 1268 | * |
| 1269 | * 4. Assign locations to any inputs without assigned locations. |
Ian Romanick | 0ad22cd | 2010-06-21 17:18:31 -0700 | [diff] [blame] | 1270 | */ |
| 1271 | |
Ian Romanick | d32d4f7 | 2011-06-27 17:59:58 -0700 | [diff] [blame] | 1272 | const int generic_base = (target_index == MESA_SHADER_VERTEX) |
Brian Paul | 7eb7d67 | 2011-07-07 16:47:59 -0600 | [diff] [blame] | 1273 | ? (int) VERT_ATTRIB_GENERIC0 : (int) FRAG_RESULT_DATA0; |
Ian Romanick | 0ad22cd | 2010-06-21 17:18:31 -0700 | [diff] [blame] | 1274 | |
Ian Romanick | d32d4f7 | 2011-06-27 17:59:58 -0700 | [diff] [blame] | 1275 | const enum ir_variable_mode direction = |
Paul Berry | 42a29d8 | 2013-01-11 14:39:32 -0800 | [diff] [blame] | 1276 | (target_index == MESA_SHADER_VERTEX) |
| 1277 | ? ir_var_shader_in : ir_var_shader_out; |
Ian Romanick | d32d4f7 | 2011-06-27 17:59:58 -0700 | [diff] [blame] | 1278 | |
| 1279 | |
Ian Romanick | 6984670 | 2010-06-22 17:29:19 -0700 | [diff] [blame] | 1280 | /* Temporary storage for the set of attributes that need locations assigned. |
| 1281 | */ |
| 1282 | struct temp_attr { |
| 1283 | unsigned slots; |
| 1284 | ir_variable *var; |
| 1285 | |
| 1286 | /* Used below in the call to qsort. */ |
| 1287 | static int compare(const void *a, const void *b) |
| 1288 | { |
| 1289 | const temp_attr *const l = (const temp_attr *) a; |
| 1290 | const temp_attr *const r = (const temp_attr *) b; |
| 1291 | |
| 1292 | /* Reversed because we want a descending order sort below. */ |
| 1293 | return r->slots - l->slots; |
| 1294 | } |
| 1295 | } to_assign[16]; |
| 1296 | |
| 1297 | unsigned num_attr = 0; |
| 1298 | |
Eric Anholt | 16b68b1 | 2010-06-30 11:05:43 -0700 | [diff] [blame] | 1299 | foreach_list(node, sh->ir) { |
Ian Romanick | 0ad22cd | 2010-06-21 17:18:31 -0700 | [diff] [blame] | 1300 | ir_variable *const var = ((ir_instruction *) node)->as_variable(); |
| 1301 | |
Brian Paul | 4470ff2 | 2011-07-19 21:10:25 -0600 | [diff] [blame] | 1302 | if ((var == NULL) || (var->mode != (unsigned) direction)) |
Ian Romanick | 0ad22cd | 2010-06-21 17:18:31 -0700 | [diff] [blame] | 1303 | continue; |
| 1304 | |
Ian Romanick | 68a4fc9 | 2010-10-07 17:21:22 -0700 | [diff] [blame] | 1305 | if (var->explicit_location) { |
Ian Romanick | d32d4f7 | 2011-06-27 17:59:58 -0700 | [diff] [blame] | 1306 | if ((var->location >= (int)(max_index + generic_base)) |
Ian Romanick | 68a4fc9 | 2010-10-07 17:21:22 -0700 | [diff] [blame] | 1307 | || (var->location < 0)) { |
Ian Romanick | 586e741 | 2011-07-28 14:04:09 -0700 | [diff] [blame] | 1308 | linker_error(prog, |
| 1309 | "invalid explicit location %d specified for `%s'\n", |
Ian Romanick | 523b611 | 2011-08-17 15:40:03 -0700 | [diff] [blame] | 1310 | (var->location < 0) |
| 1311 | ? var->location : var->location - generic_base, |
Ian Romanick | 586e741 | 2011-07-28 14:04:09 -0700 | [diff] [blame] | 1312 | var->name); |
Ian Romanick | 68a4fc9 | 2010-10-07 17:21:22 -0700 | [diff] [blame] | 1313 | return false; |
Ian Romanick | 523b611 | 2011-08-17 15:40:03 -0700 | [diff] [blame] | 1314 | } |
| 1315 | } else if (target_index == MESA_SHADER_VERTEX) { |
| 1316 | unsigned binding; |
| 1317 | |
| 1318 | if (prog->AttributeBindings->get(binding, var->name)) { |
| 1319 | assert(binding >= VERT_ATTRIB_GENERIC0); |
| 1320 | var->location = binding; |
Paul Berry | 3c9c17d | 2012-12-04 15:17:01 -0800 | [diff] [blame] | 1321 | var->is_unmatched_generic_inout = 0; |
Ian Romanick | 68a4fc9 | 2010-10-07 17:21:22 -0700 | [diff] [blame] | 1322 | } |
Ian Romanick | b12b5d9 | 2011-11-04 16:08:52 -0700 | [diff] [blame] | 1323 | } else if (target_index == MESA_SHADER_FRAGMENT) { |
| 1324 | unsigned binding; |
Dave Airlie | 1256a5d | 2012-03-24 13:33:41 +0000 | [diff] [blame] | 1325 | unsigned index; |
Ian Romanick | b12b5d9 | 2011-11-04 16:08:52 -0700 | [diff] [blame] | 1326 | |
| 1327 | if (prog->FragDataBindings->get(binding, var->name)) { |
| 1328 | assert(binding >= FRAG_RESULT_DATA0); |
| 1329 | var->location = binding; |
Paul Berry | 3c9c17d | 2012-12-04 15:17:01 -0800 | [diff] [blame] | 1330 | var->is_unmatched_generic_inout = 0; |
Dave Airlie | 1256a5d | 2012-03-24 13:33:41 +0000 | [diff] [blame] | 1331 | |
| 1332 | if (prog->FragDataIndexBindings->get(index, var->name)) { |
| 1333 | var->index = index; |
| 1334 | } |
Ian Romanick | b12b5d9 | 2011-11-04 16:08:52 -0700 | [diff] [blame] | 1335 | } |
Ian Romanick | 68a4fc9 | 2010-10-07 17:21:22 -0700 | [diff] [blame] | 1336 | } |
| 1337 | |
Ian Romanick | 9f0e98d | 2011-10-06 10:25:34 -0700 | [diff] [blame] | 1338 | /* If the variable is not a built-in and has a location statically |
| 1339 | * assigned in the shader (presumably via a layout qualifier), make sure |
| 1340 | * that it doesn't collide with other assigned locations. Otherwise, |
| 1341 | * add it to the list of variables that need linker-assigned locations. |
Ian Romanick | 0ad22cd | 2010-06-21 17:18:31 -0700 | [diff] [blame] | 1342 | */ |
Ian Romanick | 523b611 | 2011-08-17 15:40:03 -0700 | [diff] [blame] | 1343 | const unsigned slots = count_attribute_slots(var->type); |
| 1344 | if (var->location != -1) { |
Dave Airlie | 1256a5d | 2012-03-24 13:33:41 +0000 | [diff] [blame] | 1345 | if (var->location >= generic_base && var->index < 1) { |
Ian Romanick | 523b611 | 2011-08-17 15:40:03 -0700 | [diff] [blame] | 1346 | /* From page 61 of the OpenGL 4.0 spec: |
| 1347 | * |
| 1348 | * "LinkProgram will fail if the attribute bindings assigned |
| 1349 | * by BindAttribLocation do not leave not enough space to |
| 1350 | * assign a location for an active matrix attribute or an |
| 1351 | * active attribute array, both of which require multiple |
| 1352 | * contiguous generic attributes." |
| 1353 | * |
| 1354 | * Previous versions of the spec contain similar language but omit |
| 1355 | * the bit about attribute arrays. |
| 1356 | * |
| 1357 | * Page 61 of the OpenGL 4.0 spec also says: |
| 1358 | * |
| 1359 | * "It is possible for an application to bind more than one |
| 1360 | * attribute name to the same location. This is referred to as |
| 1361 | * aliasing. This will only work if only one of the aliased |
| 1362 | * attributes is active in the executable program, or if no |
| 1363 | * path through the shader consumes more than one attribute of |
| 1364 | * a set of attributes aliased to the same location. A link |
| 1365 | * error can occur if the linker determines that every path |
| 1366 | * through the shader consumes multiple aliased attributes, |
| 1367 | * but implementations are not required to generate an error |
| 1368 | * in this case." |
| 1369 | * |
| 1370 | * These two paragraphs are either somewhat contradictory, or I |
| 1371 | * don't fully understand one or both of them. |
| 1372 | */ |
| 1373 | /* FINISHME: The code as currently written does not support |
| 1374 | * FINISHME: attribute location aliasing (see comment above). |
| 1375 | */ |
| 1376 | /* Mask representing the contiguous slots that will be used by |
| 1377 | * this attribute. |
| 1378 | */ |
| 1379 | const unsigned attr = var->location - generic_base; |
| 1380 | const unsigned use_mask = (1 << slots) - 1; |
Ian Romanick | 0ad22cd | 2010-06-21 17:18:31 -0700 | [diff] [blame] | 1381 | |
Ian Romanick | 523b611 | 2011-08-17 15:40:03 -0700 | [diff] [blame] | 1382 | /* Generate a link error if the set of bits requested for this |
| 1383 | * attribute overlaps any previously allocated bits. |
| 1384 | */ |
| 1385 | if ((~(use_mask << attr) & used_locations) != used_locations) { |
Dave Airlie | 7449ae4 | 2011-11-20 19:56:35 +0000 | [diff] [blame] | 1386 | const char *const string = (target_index == MESA_SHADER_VERTEX) |
| 1387 | ? "vertex shader input" : "fragment shader output"; |
Ian Romanick | 523b611 | 2011-08-17 15:40:03 -0700 | [diff] [blame] | 1388 | linker_error(prog, |
Dave Airlie | 7449ae4 | 2011-11-20 19:56:35 +0000 | [diff] [blame] | 1389 | "insufficient contiguous locations " |
Dave Airlie | 1256a5d | 2012-03-24 13:33:41 +0000 | [diff] [blame] | 1390 | "available for %s `%s' %d %d %d", string, |
| 1391 | var->name, used_locations, use_mask, attr); |
Ian Romanick | 523b611 | 2011-08-17 15:40:03 -0700 | [diff] [blame] | 1392 | return false; |
| 1393 | } |
| 1394 | |
| 1395 | used_locations |= (use_mask << attr); |
| 1396 | } |
| 1397 | |
| 1398 | continue; |
| 1399 | } |
| 1400 | |
| 1401 | to_assign[num_attr].slots = slots; |
Ian Romanick | 6984670 | 2010-06-22 17:29:19 -0700 | [diff] [blame] | 1402 | to_assign[num_attr].var = var; |
| 1403 | num_attr++; |
Ian Romanick | 0ad22cd | 2010-06-21 17:18:31 -0700 | [diff] [blame] | 1404 | } |
Ian Romanick | 6984670 | 2010-06-22 17:29:19 -0700 | [diff] [blame] | 1405 | |
| 1406 | /* If all of the attributes were assigned locations by the application (or |
| 1407 | * are built-in attributes with fixed locations), return early. This should |
| 1408 | * be the common case. |
| 1409 | */ |
| 1410 | if (num_attr == 0) |
| 1411 | return true; |
| 1412 | |
| 1413 | qsort(to_assign, num_attr, sizeof(to_assign[0]), temp_attr::compare); |
| 1414 | |
Ian Romanick | d32d4f7 | 2011-06-27 17:59:58 -0700 | [diff] [blame] | 1415 | if (target_index == MESA_SHADER_VERTEX) { |
| 1416 | /* VERT_ATTRIB_GENERIC0 is a pseudo-alias for VERT_ATTRIB_POS. It can |
| 1417 | * only be explicitly assigned by via glBindAttribLocation. Mark it as |
| 1418 | * reserved to prevent it from being automatically allocated below. |
| 1419 | */ |
| 1420 | find_deref_visitor find("gl_Vertex"); |
| 1421 | find.run(sh->ir); |
| 1422 | if (find.variable_found()) |
| 1423 | used_locations |= (1 << 0); |
| 1424 | } |
Ian Romanick | 982e379 | 2010-06-29 18:58:20 -0700 | [diff] [blame] | 1425 | |
Ian Romanick | 6984670 | 2010-06-22 17:29:19 -0700 | [diff] [blame] | 1426 | for (unsigned i = 0; i < num_attr; i++) { |
| 1427 | /* Mask representing the contiguous slots that will be used by this |
| 1428 | * attribute. |
| 1429 | */ |
| 1430 | const unsigned use_mask = (1 << to_assign[i].slots) - 1; |
| 1431 | |
| 1432 | int location = find_available_slots(used_locations, to_assign[i].slots); |
| 1433 | |
| 1434 | if (location < 0) { |
Ian Romanick | d32d4f7 | 2011-06-27 17:59:58 -0700 | [diff] [blame] | 1435 | const char *const string = (target_index == MESA_SHADER_VERTEX) |
| 1436 | ? "vertex shader input" : "fragment shader output"; |
| 1437 | |
Ian Romanick | 586e741 | 2011-07-28 14:04:09 -0700 | [diff] [blame] | 1438 | linker_error(prog, |
Dave Airlie | 7449ae4 | 2011-11-20 19:56:35 +0000 | [diff] [blame] | 1439 | "insufficient contiguous locations " |
Ian Romanick | 586e741 | 2011-07-28 14:04:09 -0700 | [diff] [blame] | 1440 | "available for %s `%s'", |
| 1441 | string, to_assign[i].var->name); |
Ian Romanick | 6984670 | 2010-06-22 17:29:19 -0700 | [diff] [blame] | 1442 | return false; |
| 1443 | } |
| 1444 | |
Ian Romanick | d32d4f7 | 2011-06-27 17:59:58 -0700 | [diff] [blame] | 1445 | to_assign[i].var->location = generic_base + location; |
Paul Berry | 3c9c17d | 2012-12-04 15:17:01 -0800 | [diff] [blame] | 1446 | to_assign[i].var->is_unmatched_generic_inout = 0; |
Ian Romanick | 6984670 | 2010-06-22 17:29:19 -0700 | [diff] [blame] | 1447 | used_locations |= (use_mask << location); |
| 1448 | } |
| 1449 | |
| 1450 | return true; |
Ian Romanick | 0ad22cd | 2010-06-21 17:18:31 -0700 | [diff] [blame] | 1451 | } |
| 1452 | |
| 1453 | |
Ian Romanick | 40e114b | 2010-08-17 14:55:50 -0700 | [diff] [blame] | 1454 | /** |
Ian Romanick | cc90e62 | 2010-10-19 17:59:10 -0700 | [diff] [blame] | 1455 | * Demote shader inputs and outputs that are not used in other stages |
Ian Romanick | 40e114b | 2010-08-17 14:55:50 -0700 | [diff] [blame] | 1456 | */ |
| 1457 | void |
Ian Romanick | cc90e62 | 2010-10-19 17:59:10 -0700 | [diff] [blame] | 1458 | 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] | 1459 | { |
| 1460 | foreach_list(node, sh->ir) { |
| 1461 | ir_variable *const var = ((ir_instruction *) node)->as_variable(); |
| 1462 | |
Ian Romanick | cc90e62 | 2010-10-19 17:59:10 -0700 | [diff] [blame] | 1463 | if ((var == NULL) || (var->mode != int(mode))) |
Ian Romanick | 40e114b | 2010-08-17 14:55:50 -0700 | [diff] [blame] | 1464 | continue; |
| 1465 | |
Ian Romanick | cc90e62 | 2010-10-19 17:59:10 -0700 | [diff] [blame] | 1466 | /* A shader 'in' or 'out' variable is only really an input or output if |
| 1467 | * its value is used by other shader stages. This will cause the variable |
| 1468 | * to have a location assigned. |
Ian Romanick | 40e114b | 2010-08-17 14:55:50 -0700 | [diff] [blame] | 1469 | */ |
Paul Berry | 3c9c17d | 2012-12-04 15:17:01 -0800 | [diff] [blame] | 1470 | if (var->is_unmatched_generic_inout) { |
Ian Romanick | 40e114b | 2010-08-17 14:55:50 -0700 | [diff] [blame] | 1471 | var->mode = ir_var_auto; |
| 1472 | } |
| 1473 | } |
| 1474 | } |
| 1475 | |
| 1476 | |
Paul Berry | 871ddb9 | 2011-11-05 11:17:32 -0700 | [diff] [blame] | 1477 | /** |
Marek Olšák | ec174a4 | 2011-11-18 15:00:10 +0100 | [diff] [blame] | 1478 | * Store the gl_FragDepth layout in the gl_shader_program struct. |
| 1479 | */ |
| 1480 | static void |
| 1481 | store_fragdepth_layout(struct gl_shader_program *prog) |
| 1482 | { |
| 1483 | if (prog->_LinkedShaders[MESA_SHADER_FRAGMENT] == NULL) { |
| 1484 | return; |
| 1485 | } |
| 1486 | |
| 1487 | struct exec_list *ir = prog->_LinkedShaders[MESA_SHADER_FRAGMENT]->ir; |
| 1488 | |
| 1489 | /* We don't look up the gl_FragDepth symbol directly because if |
| 1490 | * gl_FragDepth is not used in the shader, it's removed from the IR. |
| 1491 | * However, the symbol won't be removed from the symbol table. |
| 1492 | * |
| 1493 | * We're only interested in the cases where the variable is NOT removed |
| 1494 | * from the IR. |
| 1495 | */ |
| 1496 | foreach_list(node, ir) { |
| 1497 | ir_variable *const var = ((ir_instruction *) node)->as_variable(); |
| 1498 | |
Paul Berry | 42a29d8 | 2013-01-11 14:39:32 -0800 | [diff] [blame] | 1499 | if (var == NULL || var->mode != ir_var_shader_out) { |
Marek Olšák | ec174a4 | 2011-11-18 15:00:10 +0100 | [diff] [blame] | 1500 | continue; |
| 1501 | } |
| 1502 | |
| 1503 | if (strcmp(var->name, "gl_FragDepth") == 0) { |
| 1504 | switch (var->depth_layout) { |
| 1505 | case ir_depth_layout_none: |
| 1506 | prog->FragDepthLayout = FRAG_DEPTH_LAYOUT_NONE; |
| 1507 | return; |
| 1508 | case ir_depth_layout_any: |
| 1509 | prog->FragDepthLayout = FRAG_DEPTH_LAYOUT_ANY; |
| 1510 | return; |
| 1511 | case ir_depth_layout_greater: |
| 1512 | prog->FragDepthLayout = FRAG_DEPTH_LAYOUT_GREATER; |
| 1513 | return; |
| 1514 | case ir_depth_layout_less: |
| 1515 | prog->FragDepthLayout = FRAG_DEPTH_LAYOUT_LESS; |
| 1516 | return; |
| 1517 | case ir_depth_layout_unchanged: |
| 1518 | prog->FragDepthLayout = FRAG_DEPTH_LAYOUT_UNCHANGED; |
| 1519 | return; |
| 1520 | default: |
| 1521 | assert(0); |
| 1522 | return; |
| 1523 | } |
| 1524 | } |
| 1525 | } |
| 1526 | } |
| 1527 | |
| 1528 | /** |
Ian Romanick | 92f8159 | 2011-11-08 12:37:19 -0800 | [diff] [blame] | 1529 | * Validate the resources used by a program versus the implementation limits |
| 1530 | */ |
| 1531 | static bool |
| 1532 | check_resources(struct gl_context *ctx, struct gl_shader_program *prog) |
| 1533 | { |
| 1534 | static const char *const shader_names[MESA_SHADER_TYPES] = { |
Marek Olšák | 030ca23 | 2013-06-12 17:15:46 +0200 | [diff] [blame] | 1535 | "vertex", "geometry", "fragment" |
Ian Romanick | 92f8159 | 2011-11-08 12:37:19 -0800 | [diff] [blame] | 1536 | }; |
| 1537 | |
| 1538 | const unsigned max_samplers[MESA_SHADER_TYPES] = { |
Marek Olšák | 5e78433 | 2013-05-02 02:30:44 +0200 | [diff] [blame] | 1539 | ctx->Const.VertexProgram.MaxTextureImageUnits, |
Marek Olšák | 030ca23 | 2013-06-12 17:15:46 +0200 | [diff] [blame] | 1540 | ctx->Const.GeometryProgram.MaxTextureImageUnits, |
| 1541 | ctx->Const.FragmentProgram.MaxTextureImageUnits |
Ian Romanick | 92f8159 | 2011-11-08 12:37:19 -0800 | [diff] [blame] | 1542 | }; |
| 1543 | |
Eric Anholt | 38e77e5 | 2013-05-23 11:10:15 -0700 | [diff] [blame] | 1544 | const unsigned max_default_uniform_components[MESA_SHADER_TYPES] = { |
Ian Romanick | 92f8159 | 2011-11-08 12:37:19 -0800 | [diff] [blame] | 1545 | ctx->Const.VertexProgram.MaxUniformComponents, |
Marek Olšák | 030ca23 | 2013-06-12 17:15:46 +0200 | [diff] [blame] | 1546 | ctx->Const.GeometryProgram.MaxUniformComponents, |
| 1547 | ctx->Const.FragmentProgram.MaxUniformComponents |
Ian Romanick | 92f8159 | 2011-11-08 12:37:19 -0800 | [diff] [blame] | 1548 | }; |
| 1549 | |
Eric Anholt | 38e77e5 | 2013-05-23 11:10:15 -0700 | [diff] [blame] | 1550 | const unsigned max_combined_uniform_components[MESA_SHADER_TYPES] = { |
| 1551 | ctx->Const.VertexProgram.MaxCombinedUniformComponents, |
Marek Olšák | 030ca23 | 2013-06-12 17:15:46 +0200 | [diff] [blame] | 1552 | ctx->Const.GeometryProgram.MaxCombinedUniformComponents, |
| 1553 | ctx->Const.FragmentProgram.MaxCombinedUniformComponents |
Eric Anholt | 38e77e5 | 2013-05-23 11:10:15 -0700 | [diff] [blame] | 1554 | }; |
| 1555 | |
Eric Anholt | 877a897 | 2012-06-25 12:47:01 -0700 | [diff] [blame] | 1556 | const unsigned max_uniform_blocks[MESA_SHADER_TYPES] = { |
| 1557 | ctx->Const.VertexProgram.MaxUniformBlocks, |
Eric Anholt | 877a897 | 2012-06-25 12:47:01 -0700 | [diff] [blame] | 1558 | ctx->Const.GeometryProgram.MaxUniformBlocks, |
Marek Olšák | 030ca23 | 2013-06-12 17:15:46 +0200 | [diff] [blame] | 1559 | ctx->Const.FragmentProgram.MaxUniformBlocks |
Eric Anholt | 877a897 | 2012-06-25 12:47:01 -0700 | [diff] [blame] | 1560 | }; |
| 1561 | |
Ian Romanick | 92f8159 | 2011-11-08 12:37:19 -0800 | [diff] [blame] | 1562 | for (unsigned i = 0; i < MESA_SHADER_TYPES; i++) { |
| 1563 | struct gl_shader *sh = prog->_LinkedShaders[i]; |
| 1564 | |
| 1565 | if (sh == NULL) |
| 1566 | continue; |
| 1567 | |
| 1568 | if (sh->num_samplers > max_samplers[i]) { |
| 1569 | linker_error(prog, "Too many %s shader texture samplers", |
| 1570 | shader_names[i]); |
| 1571 | } |
| 1572 | |
Eric Anholt | 38e77e5 | 2013-05-23 11:10:15 -0700 | [diff] [blame] | 1573 | if (sh->num_uniform_components > max_default_uniform_components[i]) { |
| 1574 | if (ctx->Const.GLSLSkipStrictMaxUniformLimitCheck) { |
| 1575 | linker_warning(prog, "Too many %s shader default uniform block " |
| 1576 | "components, but the driver will try to optimize " |
| 1577 | "them out; this is non-portable out-of-spec " |
| 1578 | "behavior\n", |
| 1579 | shader_names[i]); |
| 1580 | } else { |
| 1581 | linker_error(prog, "Too many %s shader default uniform block " |
| 1582 | "components", |
| 1583 | shader_names[i]); |
| 1584 | } |
| 1585 | } |
| 1586 | |
| 1587 | if (sh->num_combined_uniform_components > |
| 1588 | max_combined_uniform_components[i]) { |
Marek Olšák | df809ae | 2011-12-10 04:14:46 +0100 | [diff] [blame] | 1589 | if (ctx->Const.GLSLSkipStrictMaxUniformLimitCheck) { |
| 1590 | linker_warning(prog, "Too many %s shader uniform components, " |
| 1591 | "but the driver will try to optimize them out; " |
| 1592 | "this is non-portable out-of-spec behavior\n", |
| 1593 | shader_names[i]); |
| 1594 | } else { |
| 1595 | linker_error(prog, "Too many %s shader uniform components", |
| 1596 | shader_names[i]); |
| 1597 | } |
Ian Romanick | 92f8159 | 2011-11-08 12:37:19 -0800 | [diff] [blame] | 1598 | } |
| 1599 | } |
| 1600 | |
Eric Anholt | 877a897 | 2012-06-25 12:47:01 -0700 | [diff] [blame] | 1601 | unsigned blocks[MESA_SHADER_TYPES] = {0}; |
| 1602 | unsigned total_uniform_blocks = 0; |
| 1603 | |
| 1604 | for (unsigned i = 0; i < prog->NumUniformBlocks; i++) { |
| 1605 | for (unsigned j = 0; j < MESA_SHADER_TYPES; j++) { |
| 1606 | if (prog->UniformBlockStageIndex[j][i] != -1) { |
| 1607 | blocks[j]++; |
| 1608 | total_uniform_blocks++; |
| 1609 | } |
| 1610 | } |
| 1611 | |
| 1612 | if (total_uniform_blocks > ctx->Const.MaxCombinedUniformBlocks) { |
| 1613 | linker_error(prog, "Too many combined uniform blocks (%d/%d)", |
| 1614 | prog->NumUniformBlocks, |
| 1615 | ctx->Const.MaxCombinedUniformBlocks); |
| 1616 | } else { |
| 1617 | for (unsigned i = 0; i < MESA_SHADER_TYPES; i++) { |
| 1618 | if (blocks[i] > max_uniform_blocks[i]) { |
| 1619 | linker_error(prog, "Too many %s uniform blocks (%d/%d)", |
| 1620 | shader_names[i], |
| 1621 | blocks[i], |
| 1622 | max_uniform_blocks[i]); |
| 1623 | break; |
| 1624 | } |
| 1625 | } |
| 1626 | } |
| 1627 | } |
| 1628 | |
Ian Romanick | 92f8159 | 2011-11-08 12:37:19 -0800 | [diff] [blame] | 1629 | return prog->LinkStatus; |
| 1630 | } |
Paul Berry | 871ddb9 | 2011-11-05 11:17:32 -0700 | [diff] [blame] | 1631 | |
Ian Romanick | 0e59b26 | 2010-06-23 11:23:01 -0700 | [diff] [blame] | 1632 | void |
Kristian Høgsberg | f9995b3 | 2010-10-12 12:26:10 -0400 | [diff] [blame] | 1633 | link_shaders(struct gl_context *ctx, struct gl_shader_program *prog) |
Ian Romanick | 832dfa5 | 2010-06-17 15:04:20 -0700 | [diff] [blame] | 1634 | { |
Paul Berry | 871ddb9 | 2011-11-05 11:17:32 -0700 | [diff] [blame] | 1635 | tfeedback_decl *tfeedback_decls = NULL; |
| 1636 | unsigned num_tfeedback_decls = prog->TransformFeedback.NumVarying; |
| 1637 | |
Kenneth Graunke | d3073f5 | 2011-01-21 14:32:31 -0800 | [diff] [blame] | 1638 | void *mem_ctx = ralloc_context(NULL); // temporary linker context |
Kenneth Graunke | 2da02e7 | 2010-11-17 11:03:57 -0800 | [diff] [blame] | 1639 | |
Ian Romanick | 832dfa5 | 2010-06-17 15:04:20 -0700 | [diff] [blame] | 1640 | prog->LinkStatus = false; |
| 1641 | prog->Validated = false; |
| 1642 | prog->_Used = false; |
| 1643 | |
Eric Anholt | f609cf7 | 2012-04-27 13:52:56 -0700 | [diff] [blame] | 1644 | ralloc_free(prog->InfoLog); |
Kenneth Graunke | d3073f5 | 2011-01-21 14:32:31 -0800 | [diff] [blame] | 1645 | prog->InfoLog = ralloc_strdup(NULL, ""); |
Ian Romanick | f36460e | 2010-06-23 12:07:22 -0700 | [diff] [blame] | 1646 | |
Eric Anholt | f609cf7 | 2012-04-27 13:52:56 -0700 | [diff] [blame] | 1647 | ralloc_free(prog->UniformBlocks); |
| 1648 | prog->UniformBlocks = NULL; |
| 1649 | prog->NumUniformBlocks = 0; |
| 1650 | for (int i = 0; i < MESA_SHADER_TYPES; i++) { |
| 1651 | ralloc_free(prog->UniformBlockStageIndex[i]); |
| 1652 | prog->UniformBlockStageIndex[i] = NULL; |
| 1653 | } |
| 1654 | |
Ian Romanick | 832dfa5 | 2010-06-17 15:04:20 -0700 | [diff] [blame] | 1655 | /* Separate the shaders into groups based on their type. |
| 1656 | */ |
Eric Anholt | 16b68b1 | 2010-06-30 11:05:43 -0700 | [diff] [blame] | 1657 | struct gl_shader **vert_shader_list; |
Ian Romanick | 832dfa5 | 2010-06-17 15:04:20 -0700 | [diff] [blame] | 1658 | unsigned num_vert_shaders = 0; |
Eric Anholt | 16b68b1 | 2010-06-30 11:05:43 -0700 | [diff] [blame] | 1659 | struct gl_shader **frag_shader_list; |
Ian Romanick | 832dfa5 | 2010-06-17 15:04:20 -0700 | [diff] [blame] | 1660 | unsigned num_frag_shaders = 0; |
| 1661 | |
Eric Anholt | 16b68b1 | 2010-06-30 11:05:43 -0700 | [diff] [blame] | 1662 | vert_shader_list = (struct gl_shader **) |
| 1663 | calloc(2 * prog->NumShaders, sizeof(struct gl_shader *)); |
Ian Romanick | 832dfa5 | 2010-06-17 15:04:20 -0700 | [diff] [blame] | 1664 | frag_shader_list = &vert_shader_list[prog->NumShaders]; |
| 1665 | |
Ian Romanick | 25f51d3 | 2010-07-16 15:51:50 -0700 | [diff] [blame] | 1666 | unsigned min_version = UINT_MAX; |
| 1667 | unsigned max_version = 0; |
Paul Berry | a9f34dc | 2012-08-02 17:49:44 -0700 | [diff] [blame] | 1668 | const bool is_es_prog = |
| 1669 | (prog->NumShaders > 0 && prog->Shaders[0]->IsES) ? true : false; |
Ian Romanick | 832dfa5 | 2010-06-17 15:04:20 -0700 | [diff] [blame] | 1670 | for (unsigned i = 0; i < prog->NumShaders; i++) { |
Ian Romanick | 25f51d3 | 2010-07-16 15:51:50 -0700 | [diff] [blame] | 1671 | min_version = MIN2(min_version, prog->Shaders[i]->Version); |
| 1672 | max_version = MAX2(max_version, prog->Shaders[i]->Version); |
| 1673 | |
Paul Berry | a9f34dc | 2012-08-02 17:49:44 -0700 | [diff] [blame] | 1674 | if (prog->Shaders[i]->IsES != is_es_prog) { |
| 1675 | linker_error(prog, "all shaders must use same shading " |
| 1676 | "language version\n"); |
| 1677 | goto done; |
| 1678 | } |
| 1679 | |
Ian Romanick | 832dfa5 | 2010-06-17 15:04:20 -0700 | [diff] [blame] | 1680 | switch (prog->Shaders[i]->Type) { |
| 1681 | case GL_VERTEX_SHADER: |
| 1682 | vert_shader_list[num_vert_shaders] = prog->Shaders[i]; |
| 1683 | num_vert_shaders++; |
| 1684 | break; |
| 1685 | case GL_FRAGMENT_SHADER: |
| 1686 | frag_shader_list[num_frag_shaders] = prog->Shaders[i]; |
| 1687 | num_frag_shaders++; |
| 1688 | break; |
| 1689 | case GL_GEOMETRY_SHADER: |
| 1690 | /* FINISHME: Support geometry shaders. */ |
| 1691 | assert(prog->Shaders[i]->Type != GL_GEOMETRY_SHADER); |
| 1692 | break; |
| 1693 | } |
| 1694 | } |
| 1695 | |
Ian Romanick | 25f51d3 | 2010-07-16 15:51:50 -0700 | [diff] [blame] | 1696 | /* Previous to GLSL version 1.30, different compilation units could mix and |
| 1697 | * match shading language versions. With GLSL 1.30 and later, the versions |
| 1698 | * of all shaders must match. |
Paul Berry | a9f34dc | 2012-08-02 17:49:44 -0700 | [diff] [blame] | 1699 | * |
| 1700 | * GLSL ES has never allowed mixing of shading language versions. |
Ian Romanick | 25f51d3 | 2010-07-16 15:51:50 -0700 | [diff] [blame] | 1701 | */ |
Paul Berry | a9f34dc | 2012-08-02 17:49:44 -0700 | [diff] [blame] | 1702 | if ((is_es_prog || max_version >= 130) |
Kenneth Graunke | 5a81d05 | 2010-08-31 09:33:58 -0700 | [diff] [blame] | 1703 | && min_version != max_version) { |
Ian Romanick | 586e741 | 2011-07-28 14:04:09 -0700 | [diff] [blame] | 1704 | linker_error(prog, "all shaders must use same shading " |
| 1705 | "language version\n"); |
Ian Romanick | 25f51d3 | 2010-07-16 15:51:50 -0700 | [diff] [blame] | 1706 | goto done; |
| 1707 | } |
| 1708 | |
| 1709 | prog->Version = max_version; |
Paul Berry | 91c92bb | 2012-08-02 17:50:43 -0700 | [diff] [blame] | 1710 | prog->IsES = is_es_prog; |
Ian Romanick | 25f51d3 | 2010-07-16 15:51:50 -0700 | [diff] [blame] | 1711 | |
Ian Romanick | 3322fba | 2010-10-14 13:28:42 -0700 | [diff] [blame] | 1712 | for (unsigned int i = 0; i < MESA_SHADER_TYPES; i++) { |
| 1713 | if (prog->_LinkedShaders[i] != NULL) |
| 1714 | ctx->Driver.DeleteShader(ctx, prog->_LinkedShaders[i]); |
| 1715 | |
| 1716 | prog->_LinkedShaders[i] = NULL; |
Eric Anholt | 5d0f430 | 2010-08-18 12:02:35 -0700 | [diff] [blame] | 1717 | } |
| 1718 | |
Ian Romanick | cd6764e | 2010-07-16 16:00:07 -0700 | [diff] [blame] | 1719 | /* Link all shaders for a particular stage and validate the result. |
| 1720 | */ |
Ian Romanick | cc22c5a | 2010-06-18 17:13:42 -0700 | [diff] [blame] | 1721 | if (num_vert_shaders > 0) { |
Ian Romanick | 3fb8787 | 2010-07-09 14:09:34 -0700 | [diff] [blame] | 1722 | gl_shader *const sh = |
Kenneth Graunke | 2da02e7 | 2010-11-17 11:03:57 -0800 | [diff] [blame] | 1723 | link_intrastage_shaders(mem_ctx, ctx, prog, vert_shader_list, |
| 1724 | num_vert_shaders); |
Ian Romanick | 3fb8787 | 2010-07-09 14:09:34 -0700 | [diff] [blame] | 1725 | |
| 1726 | if (sh == NULL) |
| 1727 | goto done; |
| 1728 | |
| 1729 | if (!validate_vertex_shader_executable(prog, sh)) |
Ian Romanick | f29ff6e | 2010-10-14 17:55:17 -0700 | [diff] [blame] | 1730 | goto done; |
Ian Romanick | 3fb8787 | 2010-07-09 14:09:34 -0700 | [diff] [blame] | 1731 | |
Ian Romanick | 3322fba | 2010-10-14 13:28:42 -0700 | [diff] [blame] | 1732 | _mesa_reference_shader(ctx, &prog->_LinkedShaders[MESA_SHADER_VERTEX], |
| 1733 | sh); |
Ian Romanick | cc22c5a | 2010-06-18 17:13:42 -0700 | [diff] [blame] | 1734 | } |
| 1735 | |
| 1736 | if (num_frag_shaders > 0) { |
Ian Romanick | 3fb8787 | 2010-07-09 14:09:34 -0700 | [diff] [blame] | 1737 | gl_shader *const sh = |
Kenneth Graunke | 2da02e7 | 2010-11-17 11:03:57 -0800 | [diff] [blame] | 1738 | link_intrastage_shaders(mem_ctx, ctx, prog, frag_shader_list, |
| 1739 | num_frag_shaders); |
Ian Romanick | 3fb8787 | 2010-07-09 14:09:34 -0700 | [diff] [blame] | 1740 | |
| 1741 | if (sh == NULL) |
| 1742 | goto done; |
| 1743 | |
| 1744 | if (!validate_fragment_shader_executable(prog, sh)) |
Ian Romanick | f29ff6e | 2010-10-14 17:55:17 -0700 | [diff] [blame] | 1745 | goto done; |
Ian Romanick | 3fb8787 | 2010-07-09 14:09:34 -0700 | [diff] [blame] | 1746 | |
Ian Romanick | 3322fba | 2010-10-14 13:28:42 -0700 | [diff] [blame] | 1747 | _mesa_reference_shader(ctx, &prog->_LinkedShaders[MESA_SHADER_FRAGMENT], |
| 1748 | sh); |
Ian Romanick | cc22c5a | 2010-06-18 17:13:42 -0700 | [diff] [blame] | 1749 | } |
| 1750 | |
Ian Romanick | 3ed850e | 2010-06-23 12:18:21 -0700 | [diff] [blame] | 1751 | /* Here begins the inter-stage linking phase. Some initial validation is |
| 1752 | * performed, then locations are assigned for uniforms, attributes, and |
| 1753 | * varyings. |
| 1754 | */ |
Ian Romanick | ed1fe3d | 2010-06-23 12:09:14 -0700 | [diff] [blame] | 1755 | if (cross_validate_uniforms(prog)) { |
Ian Romanick | 3322fba | 2010-10-14 13:28:42 -0700 | [diff] [blame] | 1756 | unsigned prev; |
| 1757 | |
| 1758 | for (prev = 0; prev < MESA_SHADER_TYPES; prev++) { |
| 1759 | if (prog->_LinkedShaders[prev] != NULL) |
| 1760 | break; |
| 1761 | } |
| 1762 | |
Bryan Cain | f18a086 | 2011-04-23 19:29:15 -0500 | [diff] [blame] | 1763 | /* Validate the inputs of each stage with the output of the preceding |
Ian Romanick | 3710192 | 2010-06-18 19:02:10 -0700 | [diff] [blame] | 1764 | * stage. |
| 1765 | */ |
Ian Romanick | 3322fba | 2010-10-14 13:28:42 -0700 | [diff] [blame] | 1766 | for (unsigned i = prev + 1; i < MESA_SHADER_TYPES; i++) { |
| 1767 | if (prog->_LinkedShaders[i] == NULL) |
| 1768 | continue; |
| 1769 | |
Kenneth Graunke | 3ddfccb | 2013-05-20 23:46:16 -0700 | [diff] [blame] | 1770 | if (!validate_interstage_interface_blocks(prog->_LinkedShaders[prev], |
| 1771 | prog->_LinkedShaders[i])) { |
| 1772 | linker_error(prog, "interface block mismatch between shader stages\n"); |
| 1773 | goto done; |
| 1774 | } |
| 1775 | |
Ian Romanick | f36460e | 2010-06-23 12:07:22 -0700 | [diff] [blame] | 1776 | if (!cross_validate_outputs_to_inputs(prog, |
Ian Romanick | 3322fba | 2010-10-14 13:28:42 -0700 | [diff] [blame] | 1777 | prog->_LinkedShaders[prev], |
Ian Romanick | abee16e | 2010-06-21 16:16:05 -0700 | [diff] [blame] | 1778 | prog->_LinkedShaders[i])) |
Ian Romanick | 3710192 | 2010-06-18 19:02:10 -0700 | [diff] [blame] | 1779 | goto done; |
Ian Romanick | 3322fba | 2010-10-14 13:28:42 -0700 | [diff] [blame] | 1780 | |
| 1781 | prev = i; |
Ian Romanick | 3710192 | 2010-06-18 19:02:10 -0700 | [diff] [blame] | 1782 | } |
| 1783 | |
Ian Romanick | cc22c5a | 2010-06-18 17:13:42 -0700 | [diff] [blame] | 1784 | prog->LinkStatus = true; |
Ian Romanick | 3710192 | 2010-06-18 19:02:10 -0700 | [diff] [blame] | 1785 | } |
Ian Romanick | 832dfa5 | 2010-06-17 15:04:20 -0700 | [diff] [blame] | 1786 | |
Jordan Justen | 5ebf547 | 2013-03-10 03:20:03 -0700 | [diff] [blame] | 1787 | |
| 1788 | for (unsigned int i = 0; i < MESA_SHADER_TYPES; i++) { |
| 1789 | if (prog->_LinkedShaders[i] != NULL) |
| 1790 | lower_named_interface_blocks(mem_ctx, prog->_LinkedShaders[i]); |
| 1791 | } |
| 1792 | |
Eric Anholt | 3de1395 | 2012-05-04 13:08:46 -0700 | [diff] [blame] | 1793 | /* Implement the GLSL 1.30+ rule for discard vs infinite loops Do |
| 1794 | * it before optimization because we want most of the checks to get |
| 1795 | * dropped thanks to constant propagation. |
Paul Berry | 15ba2a5 | 2012-08-02 17:51:02 -0700 | [diff] [blame] | 1796 | * |
| 1797 | * This rule also applies to GLSL ES 3.00. |
Eric Anholt | 3de1395 | 2012-05-04 13:08:46 -0700 | [diff] [blame] | 1798 | */ |
Paul Berry | 15ba2a5 | 2012-08-02 17:51:02 -0700 | [diff] [blame] | 1799 | if (max_version >= (is_es_prog ? 300 : 130)) { |
Eric Anholt | 3de1395 | 2012-05-04 13:08:46 -0700 | [diff] [blame] | 1800 | struct gl_shader *sh = prog->_LinkedShaders[MESA_SHADER_FRAGMENT]; |
| 1801 | if (sh) { |
| 1802 | lower_discard_flow(sh->ir); |
| 1803 | } |
| 1804 | } |
| 1805 | |
Eric Anholt | f609cf7 | 2012-04-27 13:52:56 -0700 | [diff] [blame] | 1806 | if (!interstage_cross_validate_uniform_blocks(prog)) |
| 1807 | goto done; |
| 1808 | |
Eric Anholt | 2f4fe15 | 2010-08-10 13:06:49 -0700 | [diff] [blame] | 1809 | /* Do common optimization before assigning storage for attributes, |
| 1810 | * uniforms, and varyings. Later optimization could possibly make |
| 1811 | * some of that unused. |
| 1812 | */ |
Ian Romanick | 3322fba | 2010-10-14 13:28:42 -0700 | [diff] [blame] | 1813 | for (unsigned i = 0; i < MESA_SHADER_TYPES; i++) { |
| 1814 | if (prog->_LinkedShaders[i] == NULL) |
| 1815 | continue; |
| 1816 | |
Ian Romanick | 02c5ae1 | 2011-07-11 10:46:01 -0700 | [diff] [blame] | 1817 | detect_recursion_linked(prog, prog->_LinkedShaders[i]->ir); |
| 1818 | if (!prog->LinkStatus) |
| 1819 | goto done; |
| 1820 | |
Paul Berry | 1839244 | 2012-12-04 11:11:02 -0800 | [diff] [blame] | 1821 | if (ctx->ShaderCompilerOptions[i].LowerClipDistance) { |
| 1822 | lower_clip_distance(prog->_LinkedShaders[i]); |
| 1823 | } |
Paul Berry | c06e325 | 2011-08-11 20:58:21 -0700 | [diff] [blame] | 1824 | |
Brian Paul | 7feabfe | 2012-03-20 17:43:12 -0600 | [diff] [blame] | 1825 | unsigned max_unroll = ctx->ShaderCompilerOptions[i].MaxUnrollIterations; |
| 1826 | |
Kenneth Graunke | b765740 | 2013-04-17 17:30:22 -0700 | [diff] [blame] | 1827 | while (do_common_optimization(prog->_LinkedShaders[i]->ir, true, false, max_unroll, &ctx->ShaderCompilerOptions[i])) |
Eric Anholt | 2f4fe15 | 2010-08-10 13:06:49 -0700 | [diff] [blame] | 1828 | ; |
Ian Romanick | a7ba9a7 | 2010-07-20 13:36:32 -0700 | [diff] [blame] | 1829 | } |
Ian Romanick | 13e10e4 | 2010-06-21 12:03:24 -0700 | [diff] [blame] | 1830 | |
Paul Berry | 50895d4 | 2012-12-05 07:17:07 -0800 | [diff] [blame] | 1831 | /* Mark all generic shader inputs and outputs as unpaired. */ |
| 1832 | if (prog->_LinkedShaders[MESA_SHADER_VERTEX] != NULL) { |
| 1833 | link_invalidate_variable_locations( |
| 1834 | prog->_LinkedShaders[MESA_SHADER_VERTEX], |
Paul Berry | 36b252e | 2013-02-23 07:22:01 -0800 | [diff] [blame] | 1835 | VERT_ATTRIB_GENERIC0, VARYING_SLOT_VAR0); |
Paul Berry | 50895d4 | 2012-12-05 07:17:07 -0800 | [diff] [blame] | 1836 | } |
| 1837 | /* FINISHME: Geometry shaders not implemented yet */ |
| 1838 | if (prog->_LinkedShaders[MESA_SHADER_FRAGMENT] != NULL) { |
| 1839 | link_invalidate_variable_locations( |
| 1840 | prog->_LinkedShaders[MESA_SHADER_FRAGMENT], |
Paul Berry | eed6baf | 2013-02-23 09:00:58 -0800 | [diff] [blame] | 1841 | VARYING_SLOT_VAR0, FRAG_RESULT_DATA0); |
Paul Berry | 50895d4 | 2012-12-05 07:17:07 -0800 | [diff] [blame] | 1842 | } |
| 1843 | |
Ian Romanick | d32d4f7 | 2011-06-27 17:59:58 -0700 | [diff] [blame] | 1844 | /* FINISHME: The value of the max_attribute_index parameter is |
| 1845 | * FINISHME: implementation dependent based on the value of |
| 1846 | * FINISHME: GL_MAX_VERTEX_ATTRIBS. GL_MAX_VERTEX_ATTRIBS must be |
| 1847 | * FINISHME: at least 16, so hardcode 16 for now. |
| 1848 | */ |
| 1849 | if (!assign_attribute_or_color_locations(prog, MESA_SHADER_VERTEX, 16)) { |
Ian Romanick | d32d4f7 | 2011-06-27 17:59:58 -0700 | [diff] [blame] | 1850 | goto done; |
| 1851 | } |
| 1852 | |
Dave Airlie | 1256a5d | 2012-03-24 13:33:41 +0000 | [diff] [blame] | 1853 | if (!assign_attribute_or_color_locations(prog, MESA_SHADER_FRAGMENT, MAX2(ctx->Const.MaxDrawBuffers, ctx->Const.MaxDualSourceDrawBuffers))) { |
Ian Romanick | d32d4f7 | 2011-06-27 17:59:58 -0700 | [diff] [blame] | 1854 | goto done; |
Ian Romanick | 40e114b | 2010-08-17 14:55:50 -0700 | [diff] [blame] | 1855 | } |
| 1856 | |
Marek Olšák | 284d954 | 2013-06-12 02:18:09 +0200 | [diff] [blame] | 1857 | unsigned first; |
| 1858 | for (first = 0; first < MESA_SHADER_TYPES; first++) { |
| 1859 | if (prog->_LinkedShaders[first] != NULL) |
Ian Romanick | 3322fba | 2010-10-14 13:28:42 -0700 | [diff] [blame] | 1860 | break; |
| 1861 | } |
| 1862 | |
Paul Berry | 871ddb9 | 2011-11-05 11:17:32 -0700 | [diff] [blame] | 1863 | if (num_tfeedback_decls != 0) { |
| 1864 | /* From GL_EXT_transform_feedback: |
| 1865 | * A program will fail to link if: |
| 1866 | * |
| 1867 | * * the <count> specified by TransformFeedbackVaryingsEXT is |
| 1868 | * non-zero, but the program object has no vertex or geometry |
| 1869 | * shader; |
| 1870 | */ |
Marek Olšák | 284d954 | 2013-06-12 02:18:09 +0200 | [diff] [blame] | 1871 | if (first >= MESA_SHADER_FRAGMENT) { |
Paul Berry | 871ddb9 | 2011-11-05 11:17:32 -0700 | [diff] [blame] | 1872 | linker_error(prog, "Transform feedback varyings specified, but " |
| 1873 | "no vertex or geometry shader is present."); |
| 1874 | goto done; |
| 1875 | } |
| 1876 | |
| 1877 | tfeedback_decls = ralloc_array(mem_ctx, tfeedback_decl, |
| 1878 | prog->TransformFeedback.NumVarying); |
Paul Berry | 456279b | 2011-12-26 19:39:25 -0800 | [diff] [blame] | 1879 | if (!parse_tfeedback_decls(ctx, prog, mem_ctx, num_tfeedback_decls, |
Paul Berry | 871ddb9 | 2011-11-05 11:17:32 -0700 | [diff] [blame] | 1880 | prog->TransformFeedback.VaryingNames, |
| 1881 | tfeedback_decls)) |
| 1882 | goto done; |
| 1883 | } |
| 1884 | |
Marek Olšák | 284d954 | 2013-06-12 02:18:09 +0200 | [diff] [blame] | 1885 | /* Linking the stages in the opposite order (from fragment to vertex) |
| 1886 | * ensures that inter-shader outputs written to in an earlier stage are |
| 1887 | * eliminated if they are (transitively) not used in a later stage. |
| 1888 | */ |
| 1889 | int last, next; |
| 1890 | for (last = MESA_SHADER_TYPES-1; last >= 0; last--) { |
| 1891 | if (prog->_LinkedShaders[last] != NULL) |
| 1892 | break; |
Ian Romanick | 3322fba | 2010-10-14 13:28:42 -0700 | [diff] [blame] | 1893 | } |
Ian Romanick | 13e10e4 | 2010-06-21 12:03:24 -0700 | [diff] [blame] | 1894 | |
Marek Olšák | 284d954 | 2013-06-12 02:18:09 +0200 | [diff] [blame] | 1895 | if (last >= 0 && last < MESA_SHADER_FRAGMENT) { |
| 1896 | gl_shader *const sh = prog->_LinkedShaders[last]; |
| 1897 | |
| 1898 | if (num_tfeedback_decls != 0) { |
| 1899 | /* There was no fragment shader, but we still have to assign varying |
| 1900 | * locations for use by transform feedback. |
| 1901 | */ |
| 1902 | if (!assign_varying_locations(ctx, mem_ctx, prog, |
| 1903 | sh, NULL, |
| 1904 | num_tfeedback_decls, tfeedback_decls)) |
| 1905 | goto done; |
| 1906 | } |
| 1907 | |
Marek Olšák | b3d8b4c | 2013-06-12 13:23:48 +0200 | [diff] [blame] | 1908 | do_dead_builtin_varyings(ctx, sh->ir, NULL, |
| 1909 | num_tfeedback_decls, tfeedback_decls); |
| 1910 | |
Marek Olšák | 284d954 | 2013-06-12 02:18:09 +0200 | [diff] [blame] | 1911 | demote_shader_inputs_and_outputs(sh, ir_var_shader_out); |
| 1912 | |
| 1913 | /* Eliminate code that is now dead due to unused outputs being demoted. |
Paul Berry | 871ddb9 | 2011-11-05 11:17:32 -0700 | [diff] [blame] | 1914 | */ |
Marek Olšák | 284d954 | 2013-06-12 02:18:09 +0200 | [diff] [blame] | 1915 | while (do_dead_code(sh->ir, false)) |
| 1916 | ; |
| 1917 | } |
| 1918 | else if (first == MESA_SHADER_FRAGMENT) { |
Marek Olšák | b3d8b4c | 2013-06-12 13:23:48 +0200 | [diff] [blame] | 1919 | /* If the program only contains a fragment shader... |
Marek Olšák | 284d954 | 2013-06-12 02:18:09 +0200 | [diff] [blame] | 1920 | */ |
| 1921 | gl_shader *const sh = prog->_LinkedShaders[first]; |
| 1922 | |
Marek Olšák | b3d8b4c | 2013-06-12 13:23:48 +0200 | [diff] [blame] | 1923 | do_dead_builtin_varyings(ctx, NULL, sh->ir, |
| 1924 | num_tfeedback_decls, tfeedback_decls); |
| 1925 | |
Marek Olšák | 284d954 | 2013-06-12 02:18:09 +0200 | [diff] [blame] | 1926 | demote_shader_inputs_and_outputs(sh, ir_var_shader_in); |
| 1927 | |
| 1928 | while (do_dead_code(sh->ir, false)) |
| 1929 | ; |
| 1930 | } |
| 1931 | |
| 1932 | next = last; |
| 1933 | for (int i = next - 1; i >= 0; i--) { |
| 1934 | if (prog->_LinkedShaders[i] == NULL) |
| 1935 | continue; |
| 1936 | |
| 1937 | gl_shader *const sh_i = prog->_LinkedShaders[i]; |
| 1938 | gl_shader *const sh_next = prog->_LinkedShaders[next]; |
| 1939 | |
| 1940 | if (!assign_varying_locations(ctx, mem_ctx, prog, sh_i, sh_next, |
| 1941 | next == MESA_SHADER_FRAGMENT ? num_tfeedback_decls : 0, |
| 1942 | tfeedback_decls)) |
Paul Berry | 871ddb9 | 2011-11-05 11:17:32 -0700 | [diff] [blame] | 1943 | goto done; |
Marek Olšák | 284d954 | 2013-06-12 02:18:09 +0200 | [diff] [blame] | 1944 | |
Marek Olšák | b3d8b4c | 2013-06-12 13:23:48 +0200 | [diff] [blame] | 1945 | do_dead_builtin_varyings(ctx, sh_i->ir, sh_next->ir, |
| 1946 | next == MESA_SHADER_FRAGMENT ? num_tfeedback_decls : 0, |
| 1947 | tfeedback_decls); |
| 1948 | |
Marek Olšák | 284d954 | 2013-06-12 02:18:09 +0200 | [diff] [blame] | 1949 | demote_shader_inputs_and_outputs(sh_i, ir_var_shader_out); |
| 1950 | demote_shader_inputs_and_outputs(sh_next, ir_var_shader_in); |
| 1951 | |
| 1952 | /* Eliminate code that is now dead due to unused outputs being demoted. |
| 1953 | */ |
| 1954 | while (do_dead_code(sh_i->ir, false)) |
| 1955 | ; |
| 1956 | while (do_dead_code(sh_next->ir, false)) |
| 1957 | ; |
| 1958 | |
Marek Olšák | 3c55582 | 2013-06-13 03:17:22 +0200 | [diff] [blame] | 1959 | /* This must be done after all dead varyings are eliminated. */ |
| 1960 | if (!check_against_varying_limit(ctx, prog, sh_next)) |
| 1961 | goto done; |
| 1962 | |
Marek Olšák | 284d954 | 2013-06-12 02:18:09 +0200 | [diff] [blame] | 1963 | next = i; |
Paul Berry | 871ddb9 | 2011-11-05 11:17:32 -0700 | [diff] [blame] | 1964 | } |
| 1965 | |
| 1966 | if (!store_tfeedback_info(ctx, prog, num_tfeedback_decls, tfeedback_decls)) |
| 1967 | goto done; |
| 1968 | |
Ian Romanick | 960d722 | 2011-10-21 11:21:02 -0700 | [diff] [blame] | 1969 | update_array_sizes(prog); |
Ian Romanick | 7199096 | 2011-10-18 16:01:49 -0700 | [diff] [blame] | 1970 | link_assign_uniform_locations(prog); |
Marek Olšák | ec174a4 | 2011-11-18 15:00:10 +0100 | [diff] [blame] | 1971 | store_fragdepth_layout(prog); |
Ian Romanick | 960d722 | 2011-10-21 11:21:02 -0700 | [diff] [blame] | 1972 | |
Ian Romanick | 92f8159 | 2011-11-08 12:37:19 -0800 | [diff] [blame] | 1973 | if (!check_resources(ctx, prog)) |
| 1974 | goto done; |
| 1975 | |
Ian Romanick | ce9171f | 2011-02-03 17:10:14 -0800 | [diff] [blame] | 1976 | /* OpenGL ES requires that a vertex shader and a fragment shader both be |
Paul Berry | 15ba2a5 | 2012-08-02 17:51:02 -0700 | [diff] [blame] | 1977 | * present in a linked program. By checking prog->IsES, we also |
| 1978 | * catch the GL_ARB_ES2_compatibility case. |
Ian Romanick | ce9171f | 2011-02-03 17:10:14 -0800 | [diff] [blame] | 1979 | */ |
Eric Anholt | 57f7978 | 2011-07-22 12:57:47 -0700 | [diff] [blame] | 1980 | if (!prog->InternalSeparateShader && |
Paul Berry | 15ba2a5 | 2012-08-02 17:51:02 -0700 | [diff] [blame] | 1981 | (ctx->API == API_OPENGLES2 || prog->IsES)) { |
Ian Romanick | ce9171f | 2011-02-03 17:10:14 -0800 | [diff] [blame] | 1982 | if (prog->_LinkedShaders[MESA_SHADER_VERTEX] == NULL) { |
Ian Romanick | 586e741 | 2011-07-28 14:04:09 -0700 | [diff] [blame] | 1983 | linker_error(prog, "program lacks a vertex shader\n"); |
Ian Romanick | ce9171f | 2011-02-03 17:10:14 -0800 | [diff] [blame] | 1984 | } else if (prog->_LinkedShaders[MESA_SHADER_FRAGMENT] == NULL) { |
Ian Romanick | 586e741 | 2011-07-28 14:04:09 -0700 | [diff] [blame] | 1985 | linker_error(prog, "program lacks a fragment shader\n"); |
Ian Romanick | ce9171f | 2011-02-03 17:10:14 -0800 | [diff] [blame] | 1986 | } |
| 1987 | } |
| 1988 | |
Ian Romanick | 13e10e4 | 2010-06-21 12:03:24 -0700 | [diff] [blame] | 1989 | /* FINISHME: Assign fragment shader output locations. */ |
| 1990 | |
Ian Romanick | 832dfa5 | 2010-06-17 15:04:20 -0700 | [diff] [blame] | 1991 | done: |
| 1992 | free(vert_shader_list); |
Kenneth Graunke | 2da02e7 | 2010-11-17 11:03:57 -0800 | [diff] [blame] | 1993 | |
| 1994 | for (unsigned i = 0; i < MESA_SHADER_TYPES; i++) { |
| 1995 | if (prog->_LinkedShaders[i] == NULL) |
| 1996 | continue; |
| 1997 | |
| 1998 | /* Retain any live IR, but trash the rest. */ |
| 1999 | reparent_ir(prog->_LinkedShaders[i]->ir, prog->_LinkedShaders[i]->ir); |
Ian Romanick | 7bbcc0b | 2011-09-30 14:21:10 -0700 | [diff] [blame] | 2000 | |
| 2001 | /* The symbol table in the linked shaders may contain references to |
| 2002 | * variables that were removed (e.g., unused uniforms). Since it may |
| 2003 | * contain junk, there is no possible valid use. Delete it and set the |
| 2004 | * pointer to NULL. |
| 2005 | */ |
| 2006 | delete prog->_LinkedShaders[i]->symbols; |
| 2007 | prog->_LinkedShaders[i]->symbols = NULL; |
Kenneth Graunke | 2da02e7 | 2010-11-17 11:03:57 -0800 | [diff] [blame] | 2008 | } |
| 2009 | |
Kenneth Graunke | d3073f5 | 2011-01-21 14:32:31 -0800 | [diff] [blame] | 2010 | ralloc_free(mem_ctx); |
Ian Romanick | 832dfa5 | 2010-06-17 15:04:20 -0700 | [diff] [blame] | 2011 | } |