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