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