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