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 | */ |
| 66 | #include <cstdlib> |
| 67 | #include <cstdio> |
Ian Romanick | f36460e | 2010-06-23 12:07:22 -0700 | [diff] [blame] | 68 | #include <cstdarg> |
Ian Romanick | 25f51d3 | 2010-07-16 15:51:50 -0700 | [diff] [blame] | 69 | #include <climits> |
Ian Romanick | f36460e | 2010-06-23 12:07:22 -0700 | [diff] [blame] | 70 | |
Chia-I Wu | bfd7c9a | 2010-08-23 17:51:42 +0800 | [diff] [blame] | 71 | #include "main/core.h" |
Ian Romanick | 832dfa5 | 2010-06-17 15:04:20 -0700 | [diff] [blame] | 72 | #include "glsl_symbol_table.h" |
Ian Romanick | 832dfa5 | 2010-06-17 15:04:20 -0700 | [diff] [blame] | 73 | #include "ir.h" |
| 74 | #include "program.h" |
Aras Pranckevicius | 3174715 | 2010-07-29 12:40:49 +0300 | [diff] [blame] | 75 | #include "program/hash_table.h" |
Ian Romanick | 8fe8a81 | 2010-07-13 17:36:13 -0700 | [diff] [blame] | 76 | #include "linker.h" |
Ian Romanick | a7ba9a7 | 2010-07-20 13:36:32 -0700 | [diff] [blame] | 77 | #include "ir_optimization.h" |
Ian Romanick | 832dfa5 | 2010-06-17 15:04:20 -0700 | [diff] [blame] | 78 | |
Ian Romanick | 3322fba | 2010-10-14 13:28:42 -0700 | [diff] [blame] | 79 | extern "C" { |
| 80 | #include "main/shaderobj.h" |
| 81 | } |
| 82 | |
Ian Romanick | 832dfa5 | 2010-06-17 15:04:20 -0700 | [diff] [blame] | 83 | /** |
| 84 | * Visitor that determines whether or not a variable is ever written. |
| 85 | */ |
| 86 | class find_assignment_visitor : public ir_hierarchical_visitor { |
| 87 | public: |
| 88 | find_assignment_visitor(const char *name) |
| 89 | : name(name), found(false) |
| 90 | { |
| 91 | /* empty */ |
| 92 | } |
| 93 | |
| 94 | virtual ir_visitor_status visit_enter(ir_assignment *ir) |
| 95 | { |
| 96 | ir_variable *const var = ir->lhs->variable_referenced(); |
| 97 | |
| 98 | if (strcmp(name, var->name) == 0) { |
| 99 | found = true; |
| 100 | return visit_stop; |
| 101 | } |
| 102 | |
| 103 | return visit_continue_with_parent; |
| 104 | } |
| 105 | |
Eric Anholt | 18a6023 | 2010-08-23 11:29:25 -0700 | [diff] [blame] | 106 | virtual ir_visitor_status visit_enter(ir_call *ir) |
| 107 | { |
| 108 | exec_list_iterator sig_iter = ir->get_callee()->parameters.iterator(); |
| 109 | foreach_iter(exec_list_iterator, iter, *ir) { |
| 110 | ir_rvalue *param_rval = (ir_rvalue *)iter.get(); |
| 111 | ir_variable *sig_param = (ir_variable *)sig_iter.get(); |
| 112 | |
| 113 | if (sig_param->mode == ir_var_out || |
| 114 | sig_param->mode == ir_var_inout) { |
| 115 | ir_variable *var = param_rval->variable_referenced(); |
| 116 | if (var && strcmp(name, var->name) == 0) { |
| 117 | found = true; |
| 118 | return visit_stop; |
| 119 | } |
| 120 | } |
| 121 | sig_iter.next(); |
| 122 | } |
| 123 | |
| 124 | return visit_continue_with_parent; |
| 125 | } |
| 126 | |
Ian Romanick | 832dfa5 | 2010-06-17 15:04:20 -0700 | [diff] [blame] | 127 | bool variable_found() |
| 128 | { |
| 129 | return found; |
| 130 | } |
| 131 | |
| 132 | private: |
| 133 | const char *name; /**< Find writes to a variable with this name. */ |
| 134 | bool found; /**< Was a write to the variable found? */ |
| 135 | }; |
| 136 | |
Ian Romanick | c93b8f1 | 2010-06-17 15:20:22 -0700 | [diff] [blame] | 137 | |
Ian Romanick | c33e78f | 2010-08-13 12:30:41 -0700 | [diff] [blame] | 138 | /** |
| 139 | * Visitor that determines whether or not a variable is ever read. |
| 140 | */ |
| 141 | class find_deref_visitor : public ir_hierarchical_visitor { |
| 142 | public: |
| 143 | find_deref_visitor(const char *name) |
| 144 | : name(name), found(false) |
| 145 | { |
| 146 | /* empty */ |
| 147 | } |
| 148 | |
| 149 | virtual ir_visitor_status visit(ir_dereference_variable *ir) |
| 150 | { |
| 151 | if (strcmp(this->name, ir->var->name) == 0) { |
| 152 | this->found = true; |
| 153 | return visit_stop; |
| 154 | } |
| 155 | |
| 156 | return visit_continue; |
| 157 | } |
| 158 | |
| 159 | bool variable_found() const |
| 160 | { |
| 161 | return this->found; |
| 162 | } |
| 163 | |
| 164 | private: |
| 165 | const char *name; /**< Find writes to a variable with this name. */ |
| 166 | bool found; /**< Was a write to the variable found? */ |
| 167 | }; |
| 168 | |
| 169 | |
Ian Romanick | 0ad22cd | 2010-06-21 17:18:31 -0700 | [diff] [blame] | 170 | void |
Eric Anholt | 849e181 | 2010-06-30 11:49:17 -0700 | [diff] [blame] | 171 | linker_error_printf(gl_shader_program *prog, const char *fmt, ...) |
Ian Romanick | f36460e | 2010-06-23 12:07:22 -0700 | [diff] [blame] | 172 | { |
| 173 | va_list ap; |
| 174 | |
Kenneth Graunke | d3073f5 | 2011-01-21 14:32:31 -0800 | [diff] [blame^] | 175 | ralloc_strcat(&prog->InfoLog, "error: "); |
Ian Romanick | f36460e | 2010-06-23 12:07:22 -0700 | [diff] [blame] | 176 | va_start(ap, fmt); |
Kenneth Graunke | d3073f5 | 2011-01-21 14:32:31 -0800 | [diff] [blame^] | 177 | ralloc_vasprintf_append(&prog->InfoLog, fmt, ap); |
Ian Romanick | f36460e | 2010-06-23 12:07:22 -0700 | [diff] [blame] | 178 | va_end(ap); |
| 179 | } |
| 180 | |
| 181 | |
| 182 | void |
Eric Anholt | 16b68b1 | 2010-06-30 11:05:43 -0700 | [diff] [blame] | 183 | invalidate_variable_locations(gl_shader *sh, enum ir_variable_mode mode, |
Ian Romanick | 0ad22cd | 2010-06-21 17:18:31 -0700 | [diff] [blame] | 184 | int generic_base) |
| 185 | { |
Eric Anholt | 16b68b1 | 2010-06-30 11:05:43 -0700 | [diff] [blame] | 186 | foreach_list(node, sh->ir) { |
Ian Romanick | 0ad22cd | 2010-06-21 17:18:31 -0700 | [diff] [blame] | 187 | ir_variable *const var = ((ir_instruction *) node)->as_variable(); |
| 188 | |
| 189 | if ((var == NULL) || (var->mode != (unsigned) mode)) |
| 190 | continue; |
| 191 | |
| 192 | /* Only assign locations for generic attributes / varyings / etc. |
| 193 | */ |
Ian Romanick | 68a4fc9 | 2010-10-07 17:21:22 -0700 | [diff] [blame] | 194 | if ((var->location >= generic_base) && !var->explicit_location) |
Ian Romanick | 0ad22cd | 2010-06-21 17:18:31 -0700 | [diff] [blame] | 195 | var->location = -1; |
| 196 | } |
| 197 | } |
| 198 | |
| 199 | |
Ian Romanick | c93b8f1 | 2010-06-17 15:20:22 -0700 | [diff] [blame] | 200 | /** |
Ian Romanick | 6984670 | 2010-06-22 17:29:19 -0700 | [diff] [blame] | 201 | * Determine the number of attribute slots required for a particular type |
| 202 | * |
| 203 | * This code is here because it implements the language rules of a specific |
| 204 | * GLSL version. Since it's a property of the language and not a property of |
| 205 | * types in general, it doesn't really belong in glsl_type. |
| 206 | */ |
| 207 | unsigned |
| 208 | count_attribute_slots(const glsl_type *t) |
| 209 | { |
| 210 | /* From page 31 (page 37 of the PDF) of the GLSL 1.50 spec: |
| 211 | * |
| 212 | * "A scalar input counts the same amount against this limit as a vec4, |
| 213 | * so applications may want to consider packing groups of four |
| 214 | * unrelated float inputs together into a vector to better utilize the |
| 215 | * capabilities of the underlying hardware. A matrix input will use up |
| 216 | * multiple locations. The number of locations used will equal the |
| 217 | * number of columns in the matrix." |
| 218 | * |
| 219 | * The spec does not explicitly say how arrays are counted. However, it |
| 220 | * should be safe to assume the total number of slots consumed by an array |
| 221 | * is the number of entries in the array multiplied by the number of slots |
| 222 | * consumed by a single element of the array. |
| 223 | */ |
| 224 | |
| 225 | if (t->is_array()) |
| 226 | return t->array_size() * count_attribute_slots(t->element_type()); |
| 227 | |
| 228 | if (t->is_matrix()) |
| 229 | return t->matrix_columns; |
| 230 | |
| 231 | return 1; |
| 232 | } |
| 233 | |
| 234 | |
| 235 | /** |
Ian Romanick | c93b8f1 | 2010-06-17 15:20:22 -0700 | [diff] [blame] | 236 | * Verify that a vertex shader executable meets all semantic requirements |
| 237 | * |
| 238 | * \param shader Vertex shader executable to be verified |
| 239 | */ |
Ian Romanick | 832dfa5 | 2010-06-17 15:04:20 -0700 | [diff] [blame] | 240 | bool |
Eric Anholt | 849e181 | 2010-06-30 11:49:17 -0700 | [diff] [blame] | 241 | validate_vertex_shader_executable(struct gl_shader_program *prog, |
Eric Anholt | 16b68b1 | 2010-06-30 11:05:43 -0700 | [diff] [blame] | 242 | struct gl_shader *shader) |
Ian Romanick | 832dfa5 | 2010-06-17 15:04:20 -0700 | [diff] [blame] | 243 | { |
| 244 | if (shader == NULL) |
| 245 | return true; |
| 246 | |
Ian Romanick | 832dfa5 | 2010-06-17 15:04:20 -0700 | [diff] [blame] | 247 | find_assignment_visitor find("gl_Position"); |
Eric Anholt | 16b68b1 | 2010-06-30 11:05:43 -0700 | [diff] [blame] | 248 | find.run(shader->ir); |
Ian Romanick | 832dfa5 | 2010-06-17 15:04:20 -0700 | [diff] [blame] | 249 | if (!find.variable_found()) { |
Ian Romanick | f36460e | 2010-06-23 12:07:22 -0700 | [diff] [blame] | 250 | linker_error_printf(prog, |
| 251 | "vertex shader does not write to `gl_Position'\n"); |
Ian Romanick | 832dfa5 | 2010-06-17 15:04:20 -0700 | [diff] [blame] | 252 | return false; |
| 253 | } |
| 254 | |
| 255 | return true; |
| 256 | } |
| 257 | |
| 258 | |
Ian Romanick | c93b8f1 | 2010-06-17 15:20:22 -0700 | [diff] [blame] | 259 | /** |
| 260 | * Verify that a fragment shader executable meets all semantic requirements |
| 261 | * |
| 262 | * \param shader Fragment shader executable to be verified |
| 263 | */ |
Ian Romanick | 832dfa5 | 2010-06-17 15:04:20 -0700 | [diff] [blame] | 264 | bool |
Eric Anholt | 849e181 | 2010-06-30 11:49:17 -0700 | [diff] [blame] | 265 | validate_fragment_shader_executable(struct gl_shader_program *prog, |
Eric Anholt | 16b68b1 | 2010-06-30 11:05:43 -0700 | [diff] [blame] | 266 | struct gl_shader *shader) |
Ian Romanick | 832dfa5 | 2010-06-17 15:04:20 -0700 | [diff] [blame] | 267 | { |
| 268 | if (shader == NULL) |
| 269 | return true; |
| 270 | |
Ian Romanick | 832dfa5 | 2010-06-17 15:04:20 -0700 | [diff] [blame] | 271 | find_assignment_visitor frag_color("gl_FragColor"); |
| 272 | find_assignment_visitor frag_data("gl_FragData"); |
| 273 | |
Eric Anholt | 16b68b1 | 2010-06-30 11:05:43 -0700 | [diff] [blame] | 274 | frag_color.run(shader->ir); |
| 275 | frag_data.run(shader->ir); |
Ian Romanick | 832dfa5 | 2010-06-17 15:04:20 -0700 | [diff] [blame] | 276 | |
Ian Romanick | 832dfa5 | 2010-06-17 15:04:20 -0700 | [diff] [blame] | 277 | if (frag_color.variable_found() && frag_data.variable_found()) { |
Ian Romanick | f36460e | 2010-06-23 12:07:22 -0700 | [diff] [blame] | 278 | linker_error_printf(prog, "fragment shader writes to both " |
| 279 | "`gl_FragColor' and `gl_FragData'\n"); |
Ian Romanick | 832dfa5 | 2010-06-17 15:04:20 -0700 | [diff] [blame] | 280 | return false; |
| 281 | } |
| 282 | |
| 283 | return true; |
| 284 | } |
| 285 | |
| 286 | |
Ian Romanick | cc22c5a | 2010-06-18 17:13:42 -0700 | [diff] [blame] | 287 | /** |
Ian Romanick | e2e5d0d | 2010-06-29 18:47:11 -0700 | [diff] [blame] | 288 | * Generate a string describing the mode of a variable |
| 289 | */ |
| 290 | static const char * |
| 291 | mode_string(const ir_variable *var) |
| 292 | { |
| 293 | switch (var->mode) { |
| 294 | case ir_var_auto: |
| 295 | return (var->read_only) ? "global constant" : "global variable"; |
| 296 | |
| 297 | case ir_var_uniform: return "uniform"; |
| 298 | case ir_var_in: return "shader input"; |
| 299 | case ir_var_out: return "shader output"; |
| 300 | case ir_var_inout: return "shader inout"; |
Ian Romanick | 7e2aa91 | 2010-07-19 17:12:42 -0700 | [diff] [blame] | 301 | |
| 302 | case ir_var_temporary: |
Ian Romanick | e2e5d0d | 2010-06-29 18:47:11 -0700 | [diff] [blame] | 303 | default: |
| 304 | assert(!"Should not get here."); |
| 305 | return "invalid variable"; |
| 306 | } |
| 307 | } |
| 308 | |
| 309 | |
| 310 | /** |
| 311 | * Perform validation of global variables used across multiple shaders |
Ian Romanick | cc22c5a | 2010-06-18 17:13:42 -0700 | [diff] [blame] | 312 | */ |
| 313 | bool |
Ian Romanick | e2e5d0d | 2010-06-29 18:47:11 -0700 | [diff] [blame] | 314 | cross_validate_globals(struct gl_shader_program *prog, |
| 315 | struct gl_shader **shader_list, |
| 316 | unsigned num_shaders, |
| 317 | bool uniforms_only) |
Ian Romanick | cc22c5a | 2010-06-18 17:13:42 -0700 | [diff] [blame] | 318 | { |
| 319 | /* Examine all of the uniforms in all of the shaders and cross validate |
| 320 | * them. |
| 321 | */ |
Ian Romanick | e2e5d0d | 2010-06-29 18:47:11 -0700 | [diff] [blame] | 322 | glsl_symbol_table variables; |
| 323 | for (unsigned i = 0; i < num_shaders; i++) { |
Ian Romanick | 3322fba | 2010-10-14 13:28:42 -0700 | [diff] [blame] | 324 | if (shader_list[i] == NULL) |
| 325 | continue; |
| 326 | |
Ian Romanick | e2e5d0d | 2010-06-29 18:47:11 -0700 | [diff] [blame] | 327 | foreach_list(node, shader_list[i]->ir) { |
Ian Romanick | cc22c5a | 2010-06-18 17:13:42 -0700 | [diff] [blame] | 328 | ir_variable *const var = ((ir_instruction *) node)->as_variable(); |
| 329 | |
Ian Romanick | e2e5d0d | 2010-06-29 18:47:11 -0700 | [diff] [blame] | 330 | if (var == NULL) |
Ian Romanick | cc22c5a | 2010-06-18 17:13:42 -0700 | [diff] [blame] | 331 | continue; |
| 332 | |
Ian Romanick | e2e5d0d | 2010-06-29 18:47:11 -0700 | [diff] [blame] | 333 | if (uniforms_only && (var->mode != ir_var_uniform)) |
| 334 | continue; |
| 335 | |
Ian Romanick | 7e2aa91 | 2010-07-19 17:12:42 -0700 | [diff] [blame] | 336 | /* Don't cross validate temporaries that are at global scope. These |
| 337 | * will eventually get pulled into the shaders 'main'. |
| 338 | */ |
| 339 | if (var->mode == ir_var_temporary) |
| 340 | continue; |
| 341 | |
Ian Romanick | e2e5d0d | 2010-06-29 18:47:11 -0700 | [diff] [blame] | 342 | /* If a global with this name has already been seen, verify that the |
| 343 | * new instance has the same type. In addition, if the globals have |
Ian Romanick | cc22c5a | 2010-06-18 17:13:42 -0700 | [diff] [blame] | 344 | * initializers, the values of the initializers must be the same. |
| 345 | */ |
Ian Romanick | e2e5d0d | 2010-06-29 18:47:11 -0700 | [diff] [blame] | 346 | ir_variable *const existing = variables.get_variable(var->name); |
Ian Romanick | cc22c5a | 2010-06-18 17:13:42 -0700 | [diff] [blame] | 347 | if (existing != NULL) { |
| 348 | if (var->type != existing->type) { |
Ian Romanick | a2711d6 | 2010-08-29 22:07:49 -0700 | [diff] [blame] | 349 | /* Consider the types to be "the same" if both types are arrays |
| 350 | * of the same type and one of the arrays is implicitly sized. |
| 351 | * In addition, set the type of the linked variable to the |
| 352 | * explicitly sized array. |
| 353 | */ |
| 354 | if (var->type->is_array() |
| 355 | && existing->type->is_array() |
| 356 | && (var->type->fields.array == existing->type->fields.array) |
| 357 | && ((var->type->length == 0) |
| 358 | || (existing->type->length == 0))) { |
Ian Romanick | 0f4b2a0 | 2011-01-25 12:06:18 -0800 | [diff] [blame] | 359 | if (var->type->length != 0) { |
Ian Romanick | a2711d6 | 2010-08-29 22:07:49 -0700 | [diff] [blame] | 360 | existing->type = var->type; |
Ian Romanick | 6f53921 | 2010-12-07 18:30:33 -0800 | [diff] [blame] | 361 | } |
Ian Romanick | a2711d6 | 2010-08-29 22:07:49 -0700 | [diff] [blame] | 362 | } else { |
| 363 | linker_error_printf(prog, "%s `%s' declared as type " |
| 364 | "`%s' and type `%s'\n", |
| 365 | mode_string(var), |
| 366 | var->name, var->type->name, |
| 367 | existing->type->name); |
| 368 | return false; |
| 369 | } |
Ian Romanick | cc22c5a | 2010-06-18 17:13:42 -0700 | [diff] [blame] | 370 | } |
| 371 | |
Ian Romanick | 68a4fc9 | 2010-10-07 17:21:22 -0700 | [diff] [blame] | 372 | if (var->explicit_location) { |
| 373 | if (existing->explicit_location |
| 374 | && (var->location != existing->location)) { |
| 375 | linker_error_printf(prog, "explicit locations for %s " |
| 376 | "`%s' have differing values\n", |
| 377 | mode_string(var), var->name); |
| 378 | return false; |
| 379 | } |
| 380 | |
| 381 | existing->location = var->location; |
| 382 | existing->explicit_location = true; |
| 383 | } |
| 384 | |
Chad Versace | addae33 | 2011-01-27 01:40:31 -0800 | [diff] [blame] | 385 | /* Validate layout qualifiers for gl_FragDepth. |
| 386 | * |
| 387 | * From the AMD_conservative_depth spec: |
| 388 | * "If gl_FragDepth is redeclared in any fragment shader in |
| 389 | * a program, it must be redeclared in all fragment shaders in that |
| 390 | * program that have static assignments to gl_FragDepth. All |
| 391 | * redeclarations of gl_FragDepth in all fragment shaders in |
| 392 | * a single program must have the same set of qualifiers." |
| 393 | */ |
| 394 | if (strcmp(var->name, "gl_FragDepth") == 0) { |
| 395 | bool layout_declared = var->depth_layout != ir_depth_layout_none; |
| 396 | bool layout_differs = var->depth_layout != existing->depth_layout; |
| 397 | if (layout_declared && layout_differs) { |
| 398 | linker_error_printf(prog, |
| 399 | "All redeclarations of gl_FragDepth in all fragment shaders " |
| 400 | "in a single program must have the same set of qualifiers."); |
| 401 | } |
| 402 | if (var->used && layout_differs) { |
| 403 | linker_error_printf(prog, |
| 404 | "If gl_FragDepth is redeclared with a layout qualifier in" |
| 405 | "any fragment shader, it must be redeclared with the same" |
| 406 | "layout qualifier in all fragment shaders that have" |
| 407 | "assignments to gl_FragDepth"); |
| 408 | } |
| 409 | } |
| 410 | |
Ian Romanick | e2e5d0d | 2010-06-29 18:47:11 -0700 | [diff] [blame] | 411 | /* FINISHME: Handle non-constant initializers. |
| 412 | */ |
Ian Romanick | cc22c5a | 2010-06-18 17:13:42 -0700 | [diff] [blame] | 413 | if (var->constant_value != NULL) { |
| 414 | if (existing->constant_value != NULL) { |
| 415 | if (!var->constant_value->has_value(existing->constant_value)) { |
Ian Romanick | e2e5d0d | 2010-06-29 18:47:11 -0700 | [diff] [blame] | 416 | linker_error_printf(prog, "initializers for %s " |
Ian Romanick | f36460e | 2010-06-23 12:07:22 -0700 | [diff] [blame] | 417 | "`%s' have differing values\n", |
Ian Romanick | e2e5d0d | 2010-06-29 18:47:11 -0700 | [diff] [blame] | 418 | mode_string(var), var->name); |
Ian Romanick | cc22c5a | 2010-06-18 17:13:42 -0700 | [diff] [blame] | 419 | return false; |
| 420 | } |
| 421 | } else |
| 422 | /* If the first-seen instance of a particular uniform did not |
| 423 | * have an initializer but a later instance does, copy the |
| 424 | * initializer to the version stored in the symbol table. |
| 425 | */ |
Ian Romanick | de415b7 | 2010-07-14 13:22:12 -0700 | [diff] [blame] | 426 | /* FINISHME: This is wrong. The constant_value field should |
| 427 | * FINISHME: not be modified! Imagine a case where a shader |
| 428 | * FINISHME: without an initializer is linked in two different |
| 429 | * FINISHME: programs with shaders that have differing |
| 430 | * FINISHME: initializers. Linking with the first will |
| 431 | * FINISHME: modify the shader, and linking with the second |
| 432 | * FINISHME: will fail. |
| 433 | */ |
Eric Anholt | 8273bd4 | 2010-08-04 12:34:56 -0700 | [diff] [blame] | 434 | existing->constant_value = |
Kenneth Graunke | d3073f5 | 2011-01-21 14:32:31 -0800 | [diff] [blame^] | 435 | var->constant_value->clone(ralloc_parent(existing), NULL); |
Ian Romanick | cc22c5a | 2010-06-18 17:13:42 -0700 | [diff] [blame] | 436 | } |
Chad Versace | 7528f14 | 2010-11-17 14:34:38 -0800 | [diff] [blame] | 437 | |
| 438 | if (existing->invariant != var->invariant) { |
| 439 | linker_error_printf(prog, "declarations for %s `%s' have " |
| 440 | "mismatching invariant qualifiers\n", |
| 441 | mode_string(var), var->name); |
| 442 | return false; |
| 443 | } |
Chad Versace | 61428dd | 2011-01-10 15:29:30 -0800 | [diff] [blame] | 444 | if (existing->centroid != var->centroid) { |
| 445 | linker_error_printf(prog, "declarations for %s `%s' have " |
| 446 | "mismatching centroid qualifiers\n", |
| 447 | mode_string(var), var->name); |
| 448 | return false; |
| 449 | } |
Ian Romanick | cc22c5a | 2010-06-18 17:13:42 -0700 | [diff] [blame] | 450 | } else |
Eric Anholt | 001eee5 | 2010-11-05 06:11:24 -0700 | [diff] [blame] | 451 | variables.add_variable(var); |
Ian Romanick | cc22c5a | 2010-06-18 17:13:42 -0700 | [diff] [blame] | 452 | } |
| 453 | } |
| 454 | |
| 455 | return true; |
| 456 | } |
| 457 | |
| 458 | |
Ian Romanick | 3710192 | 2010-06-18 19:02:10 -0700 | [diff] [blame] | 459 | /** |
Ian Romanick | e2e5d0d | 2010-06-29 18:47:11 -0700 | [diff] [blame] | 460 | * Perform validation of uniforms used across multiple shader stages |
| 461 | */ |
| 462 | bool |
| 463 | cross_validate_uniforms(struct gl_shader_program *prog) |
| 464 | { |
| 465 | return cross_validate_globals(prog, prog->_LinkedShaders, |
Ian Romanick | 3322fba | 2010-10-14 13:28:42 -0700 | [diff] [blame] | 466 | MESA_SHADER_TYPES, true); |
Ian Romanick | e2e5d0d | 2010-06-29 18:47:11 -0700 | [diff] [blame] | 467 | } |
| 468 | |
| 469 | |
| 470 | /** |
Ian Romanick | 3710192 | 2010-06-18 19:02:10 -0700 | [diff] [blame] | 471 | * Validate that outputs from one stage match inputs of another |
| 472 | */ |
| 473 | bool |
Eric Anholt | 849e181 | 2010-06-30 11:49:17 -0700 | [diff] [blame] | 474 | cross_validate_outputs_to_inputs(struct gl_shader_program *prog, |
Eric Anholt | 16b68b1 | 2010-06-30 11:05:43 -0700 | [diff] [blame] | 475 | gl_shader *producer, gl_shader *consumer) |
Ian Romanick | 3710192 | 2010-06-18 19:02:10 -0700 | [diff] [blame] | 476 | { |
| 477 | glsl_symbol_table parameters; |
| 478 | /* FINISHME: Figure these out dynamically. */ |
| 479 | const char *const producer_stage = "vertex"; |
| 480 | const char *const consumer_stage = "fragment"; |
| 481 | |
| 482 | /* Find all shader outputs in the "producer" stage. |
| 483 | */ |
Eric Anholt | 16b68b1 | 2010-06-30 11:05:43 -0700 | [diff] [blame] | 484 | foreach_list(node, producer->ir) { |
Ian Romanick | 3710192 | 2010-06-18 19:02:10 -0700 | [diff] [blame] | 485 | ir_variable *const var = ((ir_instruction *) node)->as_variable(); |
| 486 | |
| 487 | /* FINISHME: For geometry shaders, this should also look for inout |
| 488 | * FINISHME: variables. |
| 489 | */ |
| 490 | if ((var == NULL) || (var->mode != ir_var_out)) |
| 491 | continue; |
| 492 | |
Eric Anholt | 001eee5 | 2010-11-05 06:11:24 -0700 | [diff] [blame] | 493 | parameters.add_variable(var); |
Ian Romanick | 3710192 | 2010-06-18 19:02:10 -0700 | [diff] [blame] | 494 | } |
| 495 | |
| 496 | |
| 497 | /* Find all shader inputs in the "consumer" stage. Any variables that have |
| 498 | * matching outputs already in the symbol table must have the same type and |
| 499 | * qualifiers. |
| 500 | */ |
Eric Anholt | 16b68b1 | 2010-06-30 11:05:43 -0700 | [diff] [blame] | 501 | foreach_list(node, consumer->ir) { |
Ian Romanick | 3710192 | 2010-06-18 19:02:10 -0700 | [diff] [blame] | 502 | ir_variable *const input = ((ir_instruction *) node)->as_variable(); |
| 503 | |
| 504 | /* FINISHME: For geometry shaders, this should also look for inout |
| 505 | * FINISHME: variables. |
| 506 | */ |
| 507 | if ((input == NULL) || (input->mode != ir_var_in)) |
| 508 | continue; |
| 509 | |
| 510 | ir_variable *const output = parameters.get_variable(input->name); |
| 511 | if (output != NULL) { |
| 512 | /* Check that the types match between stages. |
| 513 | */ |
| 514 | if (input->type != output->type) { |
Ian Romanick | cb2b547 | 2010-12-13 15:16:39 -0800 | [diff] [blame] | 515 | /* There is a bit of a special case for gl_TexCoord. This |
| 516 | * built-in is unsized by default. Appliations that variable |
| 517 | * access it must redeclare it with a size. There is some |
| 518 | * language in the GLSL spec that implies the fragment shader |
| 519 | * and vertex shader do not have to agree on this size. Other |
| 520 | * driver behave this way, and one or two applications seem to |
| 521 | * rely on it. |
| 522 | * |
| 523 | * Neither declaration needs to be modified here because the array |
| 524 | * sizes are fixed later when update_array_sizes is called. |
| 525 | * |
| 526 | * From page 48 (page 54 of the PDF) of the GLSL 1.10 spec: |
| 527 | * |
| 528 | * "Unlike user-defined varying variables, the built-in |
| 529 | * varying variables don't have a strict one-to-one |
| 530 | * correspondence between the vertex language and the |
| 531 | * fragment language." |
| 532 | */ |
| 533 | if (!output->type->is_array() |
| 534 | || (strncmp("gl_", output->name, 3) != 0)) { |
| 535 | linker_error_printf(prog, |
| 536 | "%s shader output `%s' declared as " |
| 537 | "type `%s', but %s shader input declared " |
| 538 | "as type `%s'\n", |
| 539 | producer_stage, output->name, |
| 540 | output->type->name, |
| 541 | consumer_stage, input->type->name); |
| 542 | return false; |
| 543 | } |
Ian Romanick | 3710192 | 2010-06-18 19:02:10 -0700 | [diff] [blame] | 544 | } |
| 545 | |
| 546 | /* Check that all of the qualifiers match between stages. |
| 547 | */ |
| 548 | if (input->centroid != output->centroid) { |
Ian Romanick | f36460e | 2010-06-23 12:07:22 -0700 | [diff] [blame] | 549 | linker_error_printf(prog, |
| 550 | "%s shader output `%s' %s centroid qualifier, " |
| 551 | "but %s shader input %s centroid qualifier\n", |
| 552 | producer_stage, |
| 553 | output->name, |
| 554 | (output->centroid) ? "has" : "lacks", |
| 555 | consumer_stage, |
| 556 | (input->centroid) ? "has" : "lacks"); |
Ian Romanick | 3710192 | 2010-06-18 19:02:10 -0700 | [diff] [blame] | 557 | return false; |
| 558 | } |
| 559 | |
| 560 | if (input->invariant != output->invariant) { |
Ian Romanick | f36460e | 2010-06-23 12:07:22 -0700 | [diff] [blame] | 561 | linker_error_printf(prog, |
| 562 | "%s shader output `%s' %s invariant qualifier, " |
| 563 | "but %s shader input %s invariant qualifier\n", |
| 564 | producer_stage, |
| 565 | output->name, |
| 566 | (output->invariant) ? "has" : "lacks", |
| 567 | consumer_stage, |
| 568 | (input->invariant) ? "has" : "lacks"); |
Ian Romanick | 3710192 | 2010-06-18 19:02:10 -0700 | [diff] [blame] | 569 | return false; |
| 570 | } |
| 571 | |
| 572 | if (input->interpolation != output->interpolation) { |
Ian Romanick | f36460e | 2010-06-23 12:07:22 -0700 | [diff] [blame] | 573 | linker_error_printf(prog, |
| 574 | "%s shader output `%s' specifies %s " |
| 575 | "interpolation qualifier, " |
| 576 | "but %s shader input specifies %s " |
| 577 | "interpolation qualifier\n", |
| 578 | producer_stage, |
| 579 | output->name, |
| 580 | output->interpolation_string(), |
| 581 | consumer_stage, |
| 582 | input->interpolation_string()); |
Ian Romanick | 3710192 | 2010-06-18 19:02:10 -0700 | [diff] [blame] | 583 | return false; |
| 584 | } |
| 585 | } |
| 586 | } |
| 587 | |
| 588 | return true; |
| 589 | } |
| 590 | |
| 591 | |
Ian Romanick | 3fb8787 | 2010-07-09 14:09:34 -0700 | [diff] [blame] | 592 | /** |
| 593 | * Populates a shaders symbol table with all global declarations |
| 594 | */ |
| 595 | static void |
| 596 | populate_symbol_table(gl_shader *sh) |
| 597 | { |
| 598 | sh->symbols = new(sh) glsl_symbol_table; |
| 599 | |
| 600 | foreach_list(node, sh->ir) { |
| 601 | ir_instruction *const inst = (ir_instruction *) node; |
| 602 | ir_variable *var; |
| 603 | ir_function *func; |
| 604 | |
| 605 | if ((func = inst->as_function()) != NULL) { |
Eric Anholt | e8f5ebf | 2010-11-05 06:08:45 -0700 | [diff] [blame] | 606 | sh->symbols->add_function(func); |
Ian Romanick | 3fb8787 | 2010-07-09 14:09:34 -0700 | [diff] [blame] | 607 | } else if ((var = inst->as_variable()) != NULL) { |
Eric Anholt | 001eee5 | 2010-11-05 06:11:24 -0700 | [diff] [blame] | 608 | sh->symbols->add_variable(var); |
Ian Romanick | 3fb8787 | 2010-07-09 14:09:34 -0700 | [diff] [blame] | 609 | } |
| 610 | } |
| 611 | } |
| 612 | |
| 613 | |
| 614 | /** |
Ian Romanick | 31a9786 | 2010-07-12 18:48:50 -0700 | [diff] [blame] | 615 | * Remap variables referenced in an instruction tree |
| 616 | * |
| 617 | * This is used when instruction trees are cloned from one shader and placed in |
| 618 | * another. These trees will contain references to \c ir_variable nodes that |
| 619 | * do not exist in the target shader. This function finds these \c ir_variable |
| 620 | * references and replaces the references with matching variables in the target |
| 621 | * shader. |
| 622 | * |
| 623 | * If there is no matching variable in the target shader, a clone of the |
| 624 | * \c ir_variable is made and added to the target shader. The new variable is |
| 625 | * added to \b both the instruction stream and the symbol table. |
| 626 | * |
| 627 | * \param inst IR tree that is to be processed. |
| 628 | * \param symbols Symbol table containing global scope symbols in the |
| 629 | * linked shader. |
| 630 | * \param instructions Instruction stream where new variable declarations |
| 631 | * should be added. |
| 632 | */ |
| 633 | void |
Eric Anholt | 8273bd4 | 2010-08-04 12:34:56 -0700 | [diff] [blame] | 634 | remap_variables(ir_instruction *inst, struct gl_shader *target, |
| 635 | hash_table *temps) |
Ian Romanick | 31a9786 | 2010-07-12 18:48:50 -0700 | [diff] [blame] | 636 | { |
| 637 | class remap_visitor : public ir_hierarchical_visitor { |
| 638 | public: |
Eric Anholt | 8273bd4 | 2010-08-04 12:34:56 -0700 | [diff] [blame] | 639 | remap_visitor(struct gl_shader *target, |
Ian Romanick | 7e2aa91 | 2010-07-19 17:12:42 -0700 | [diff] [blame] | 640 | hash_table *temps) |
Ian Romanick | 31a9786 | 2010-07-12 18:48:50 -0700 | [diff] [blame] | 641 | { |
Eric Anholt | 8273bd4 | 2010-08-04 12:34:56 -0700 | [diff] [blame] | 642 | this->target = target; |
| 643 | this->symbols = target->symbols; |
| 644 | this->instructions = target->ir; |
Ian Romanick | 7e2aa91 | 2010-07-19 17:12:42 -0700 | [diff] [blame] | 645 | this->temps = temps; |
Ian Romanick | 31a9786 | 2010-07-12 18:48:50 -0700 | [diff] [blame] | 646 | } |
| 647 | |
| 648 | virtual ir_visitor_status visit(ir_dereference_variable *ir) |
| 649 | { |
Ian Romanick | 7e2aa91 | 2010-07-19 17:12:42 -0700 | [diff] [blame] | 650 | if (ir->var->mode == ir_var_temporary) { |
| 651 | ir_variable *var = (ir_variable *) hash_table_find(temps, ir->var); |
| 652 | |
| 653 | assert(var != NULL); |
| 654 | ir->var = var; |
| 655 | return visit_continue; |
| 656 | } |
| 657 | |
Ian Romanick | 31a9786 | 2010-07-12 18:48:50 -0700 | [diff] [blame] | 658 | ir_variable *const existing = |
| 659 | this->symbols->get_variable(ir->var->name); |
| 660 | if (existing != NULL) |
| 661 | ir->var = existing; |
| 662 | else { |
Eric Anholt | 8273bd4 | 2010-08-04 12:34:56 -0700 | [diff] [blame] | 663 | ir_variable *copy = ir->var->clone(this->target, NULL); |
Ian Romanick | 31a9786 | 2010-07-12 18:48:50 -0700 | [diff] [blame] | 664 | |
Eric Anholt | 001eee5 | 2010-11-05 06:11:24 -0700 | [diff] [blame] | 665 | this->symbols->add_variable(copy); |
Ian Romanick | 31a9786 | 2010-07-12 18:48:50 -0700 | [diff] [blame] | 666 | this->instructions->push_head(copy); |
Ian Romanick | 7e2aa91 | 2010-07-19 17:12:42 -0700 | [diff] [blame] | 667 | ir->var = copy; |
Ian Romanick | 31a9786 | 2010-07-12 18:48:50 -0700 | [diff] [blame] | 668 | } |
| 669 | |
| 670 | return visit_continue; |
| 671 | } |
| 672 | |
| 673 | private: |
Eric Anholt | 8273bd4 | 2010-08-04 12:34:56 -0700 | [diff] [blame] | 674 | struct gl_shader *target; |
Ian Romanick | 31a9786 | 2010-07-12 18:48:50 -0700 | [diff] [blame] | 675 | glsl_symbol_table *symbols; |
| 676 | exec_list *instructions; |
Ian Romanick | 7e2aa91 | 2010-07-19 17:12:42 -0700 | [diff] [blame] | 677 | hash_table *temps; |
Ian Romanick | 31a9786 | 2010-07-12 18:48:50 -0700 | [diff] [blame] | 678 | }; |
| 679 | |
Eric Anholt | 8273bd4 | 2010-08-04 12:34:56 -0700 | [diff] [blame] | 680 | remap_visitor v(target, temps); |
Ian Romanick | 31a9786 | 2010-07-12 18:48:50 -0700 | [diff] [blame] | 681 | |
| 682 | inst->accept(&v); |
| 683 | } |
| 684 | |
| 685 | |
| 686 | /** |
| 687 | * Move non-declarations from one instruction stream to another |
| 688 | * |
| 689 | * 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] | 690 | * 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] | 691 | * pointer) for \c last and \c false for \c make_copies on the first |
| 692 | * call. Successive calls pass the return value of the previous call for |
| 693 | * \c last and \c true for \c make_copies. |
| 694 | * |
| 695 | * \param instructions Source instruction stream |
| 696 | * \param last Instruction after which new instructions should be |
| 697 | * inserted in the target instruction stream |
| 698 | * \param make_copies Flag selecting whether instructions in \c instructions |
| 699 | * should be copied (via \c ir_instruction::clone) into the |
| 700 | * target list or moved. |
| 701 | * |
| 702 | * \return |
| 703 | * The new "last" instruction in the target instruction stream. This pointer |
| 704 | * is suitable for use as the \c last parameter of a later call to this |
| 705 | * function. |
| 706 | */ |
| 707 | exec_node * |
| 708 | move_non_declarations(exec_list *instructions, exec_node *last, |
| 709 | bool make_copies, gl_shader *target) |
| 710 | { |
Ian Romanick | 7e2aa91 | 2010-07-19 17:12:42 -0700 | [diff] [blame] | 711 | hash_table *temps = NULL; |
| 712 | |
| 713 | if (make_copies) |
| 714 | temps = hash_table_ctor(0, hash_table_pointer_hash, |
| 715 | hash_table_pointer_compare); |
| 716 | |
Ian Romanick | 303c99f | 2010-07-19 12:34:56 -0700 | [diff] [blame] | 717 | foreach_list_safe(node, instructions) { |
Ian Romanick | 31a9786 | 2010-07-12 18:48:50 -0700 | [diff] [blame] | 718 | ir_instruction *inst = (ir_instruction *) node; |
| 719 | |
Ian Romanick | 7e2aa91 | 2010-07-19 17:12:42 -0700 | [diff] [blame] | 720 | if (inst->as_function()) |
Ian Romanick | 31a9786 | 2010-07-12 18:48:50 -0700 | [diff] [blame] | 721 | continue; |
| 722 | |
Ian Romanick | 7e2aa91 | 2010-07-19 17:12:42 -0700 | [diff] [blame] | 723 | ir_variable *var = inst->as_variable(); |
| 724 | if ((var != NULL) && (var->mode != ir_var_temporary)) |
| 725 | continue; |
| 726 | |
| 727 | assert(inst->as_assignment() |
| 728 | || ((var != NULL) && (var->mode == ir_var_temporary))); |
Ian Romanick | 31a9786 | 2010-07-12 18:48:50 -0700 | [diff] [blame] | 729 | |
| 730 | if (make_copies) { |
Eric Anholt | 8273bd4 | 2010-08-04 12:34:56 -0700 | [diff] [blame] | 731 | inst = inst->clone(target, NULL); |
Ian Romanick | 7e2aa91 | 2010-07-19 17:12:42 -0700 | [diff] [blame] | 732 | |
| 733 | if (var != NULL) |
| 734 | hash_table_insert(temps, inst, var); |
| 735 | else |
Eric Anholt | 8273bd4 | 2010-08-04 12:34:56 -0700 | [diff] [blame] | 736 | remap_variables(inst, target, temps); |
Ian Romanick | 31a9786 | 2010-07-12 18:48:50 -0700 | [diff] [blame] | 737 | } else { |
| 738 | inst->remove(); |
| 739 | } |
| 740 | |
| 741 | last->insert_after(inst); |
| 742 | last = inst; |
| 743 | } |
| 744 | |
Ian Romanick | 7e2aa91 | 2010-07-19 17:12:42 -0700 | [diff] [blame] | 745 | if (make_copies) |
| 746 | hash_table_dtor(temps); |
| 747 | |
Ian Romanick | 31a9786 | 2010-07-12 18:48:50 -0700 | [diff] [blame] | 748 | return last; |
| 749 | } |
| 750 | |
| 751 | /** |
Ian Romanick | 15ce87e | 2010-07-09 15:28:22 -0700 | [diff] [blame] | 752 | * Get the function signature for main from a shader |
| 753 | */ |
| 754 | static ir_function_signature * |
| 755 | get_main_function_signature(gl_shader *sh) |
| 756 | { |
| 757 | ir_function *const f = sh->symbols->get_function("main"); |
| 758 | if (f != NULL) { |
| 759 | exec_list void_parameters; |
| 760 | |
| 761 | /* Look for the 'void main()' signature and ensure that it's defined. |
| 762 | * This keeps the linker from accidentally pick a shader that just |
| 763 | * contains a prototype for main. |
| 764 | * |
| 765 | * We don't have to check for multiple definitions of main (in multiple |
| 766 | * shaders) because that would have already been caught above. |
| 767 | */ |
| 768 | ir_function_signature *sig = f->matching_signature(&void_parameters); |
| 769 | if ((sig != NULL) && sig->is_defined) { |
| 770 | return sig; |
| 771 | } |
| 772 | } |
| 773 | |
| 774 | return NULL; |
| 775 | } |
| 776 | |
| 777 | |
| 778 | /** |
Ian Romanick | 3fb8787 | 2010-07-09 14:09:34 -0700 | [diff] [blame] | 779 | * Combine a group of shaders for a single stage to generate a linked shader |
| 780 | * |
| 781 | * \note |
| 782 | * If this function is supplied a single shader, it is cloned, and the new |
| 783 | * shader is returned. |
| 784 | */ |
| 785 | static struct gl_shader * |
Kenneth Graunke | 2da02e7 | 2010-11-17 11:03:57 -0800 | [diff] [blame] | 786 | link_intrastage_shaders(void *mem_ctx, |
| 787 | struct gl_context *ctx, |
Eric Anholt | 5d0f430 | 2010-08-18 12:02:35 -0700 | [diff] [blame] | 788 | struct gl_shader_program *prog, |
Ian Romanick | 3fb8787 | 2010-07-09 14:09:34 -0700 | [diff] [blame] | 789 | struct gl_shader **shader_list, |
| 790 | unsigned num_shaders) |
| 791 | { |
Ian Romanick | 13f782c | 2010-06-29 18:53:38 -0700 | [diff] [blame] | 792 | /* Check that global variables defined in multiple shaders are consistent. |
| 793 | */ |
| 794 | if (!cross_validate_globals(prog, shader_list, num_shaders, false)) |
| 795 | return NULL; |
| 796 | |
| 797 | /* Check that there is only a single definition of each function signature |
| 798 | * across all shaders. |
| 799 | */ |
| 800 | for (unsigned i = 0; i < (num_shaders - 1); i++) { |
| 801 | foreach_list(node, shader_list[i]->ir) { |
| 802 | ir_function *const f = ((ir_instruction *) node)->as_function(); |
| 803 | |
| 804 | if (f == NULL) |
| 805 | continue; |
| 806 | |
| 807 | for (unsigned j = i + 1; j < num_shaders; j++) { |
| 808 | ir_function *const other = |
| 809 | shader_list[j]->symbols->get_function(f->name); |
| 810 | |
| 811 | /* If the other shader has no function (and therefore no function |
| 812 | * signatures) with the same name, skip to the next shader. |
| 813 | */ |
| 814 | if (other == NULL) |
| 815 | continue; |
| 816 | |
| 817 | foreach_iter (exec_list_iterator, iter, *f) { |
| 818 | ir_function_signature *sig = |
| 819 | (ir_function_signature *) iter.get(); |
| 820 | |
Kenneth Graunke | f412fac | 2010-09-05 01:48:11 -0700 | [diff] [blame] | 821 | if (!sig->is_defined || sig->is_builtin) |
Ian Romanick | 13f782c | 2010-06-29 18:53:38 -0700 | [diff] [blame] | 822 | continue; |
| 823 | |
| 824 | ir_function_signature *other_sig = |
| 825 | other->exact_matching_signature(& sig->parameters); |
| 826 | |
| 827 | if ((other_sig != NULL) && other_sig->is_defined |
Kenneth Graunke | f412fac | 2010-09-05 01:48:11 -0700 | [diff] [blame] | 828 | && !other_sig->is_builtin) { |
Ian Romanick | 13f782c | 2010-06-29 18:53:38 -0700 | [diff] [blame] | 829 | linker_error_printf(prog, |
| 830 | "function `%s' is multiply defined", |
| 831 | f->name); |
| 832 | return NULL; |
| 833 | } |
| 834 | } |
| 835 | } |
| 836 | } |
| 837 | } |
| 838 | |
| 839 | /* Find the shader that defines main, and make a clone of it. |
| 840 | * |
| 841 | * Starting with the clone, search for undefined references. If one is |
| 842 | * found, find the shader that defines it. Clone the reference and add |
| 843 | * it to the shader. Repeat until there are no undefined references or |
| 844 | * until a reference cannot be resolved. |
| 845 | */ |
Ian Romanick | 15ce87e | 2010-07-09 15:28:22 -0700 | [diff] [blame] | 846 | gl_shader *main = NULL; |
| 847 | for (unsigned i = 0; i < num_shaders; i++) { |
| 848 | if (get_main_function_signature(shader_list[i]) != NULL) { |
| 849 | main = shader_list[i]; |
| 850 | break; |
| 851 | } |
| 852 | } |
Ian Romanick | 13f782c | 2010-06-29 18:53:38 -0700 | [diff] [blame] | 853 | |
Ian Romanick | 15ce87e | 2010-07-09 15:28:22 -0700 | [diff] [blame] | 854 | if (main == NULL) { |
| 855 | linker_error_printf(prog, "%s shader lacks `main'\n", |
| 856 | (shader_list[0]->Type == GL_VERTEX_SHADER) |
| 857 | ? "vertex" : "fragment"); |
| 858 | return NULL; |
| 859 | } |
| 860 | |
Ian Romanick | 4a45595 | 2010-10-13 15:13:02 -0700 | [diff] [blame] | 861 | gl_shader *linked = ctx->Driver.NewShader(NULL, 0, main->Type); |
Ian Romanick | 15ce87e | 2010-07-09 15:28:22 -0700 | [diff] [blame] | 862 | linked->ir = new(linked) exec_list; |
Kenneth Graunke | 2da02e7 | 2010-11-17 11:03:57 -0800 | [diff] [blame] | 863 | clone_ir_list(mem_ctx, linked->ir, main->ir); |
Ian Romanick | 15ce87e | 2010-07-09 15:28:22 -0700 | [diff] [blame] | 864 | |
| 865 | populate_symbol_table(linked); |
Ian Romanick | 13f782c | 2010-06-29 18:53:38 -0700 | [diff] [blame] | 866 | |
Ian Romanick | 31a9786 | 2010-07-12 18:48:50 -0700 | [diff] [blame] | 867 | /* The a pointer to the main function in the final linked shader (i.e., the |
| 868 | * copy of the original shader that contained the main function). |
| 869 | */ |
| 870 | ir_function_signature *const main_sig = get_main_function_signature(linked); |
| 871 | |
| 872 | /* Move any instructions other than variable declarations or function |
| 873 | * declarations into main. |
| 874 | */ |
Ian Romanick | 9303e35 | 2010-07-19 12:33:54 -0700 | [diff] [blame] | 875 | exec_node *insertion_point = |
| 876 | move_non_declarations(linked->ir, (exec_node *) &main_sig->body, false, |
| 877 | linked); |
| 878 | |
Ian Romanick | 31a9786 | 2010-07-12 18:48:50 -0700 | [diff] [blame] | 879 | for (unsigned i = 0; i < num_shaders; i++) { |
Ian Romanick | 9303e35 | 2010-07-19 12:33:54 -0700 | [diff] [blame] | 880 | if (shader_list[i] == main) |
| 881 | continue; |
| 882 | |
Ian Romanick | 31a9786 | 2010-07-12 18:48:50 -0700 | [diff] [blame] | 883 | insertion_point = move_non_declarations(shader_list[i]->ir, |
Ian Romanick | 9303e35 | 2010-07-19 12:33:54 -0700 | [diff] [blame] | 884 | insertion_point, true, linked); |
Ian Romanick | 31a9786 | 2010-07-12 18:48:50 -0700 | [diff] [blame] | 885 | } |
| 886 | |
Ian Romanick | 13f782c | 2010-06-29 18:53:38 -0700 | [diff] [blame] | 887 | /* Resolve initializers for global variables in the linked shader. |
| 888 | */ |
Ian Romanick | d5be2ac | 2010-07-20 11:29:46 -0700 | [diff] [blame] | 889 | unsigned num_linking_shaders = num_shaders; |
| 890 | for (unsigned i = 0; i < num_shaders; i++) |
| 891 | num_linking_shaders += shader_list[i]->num_builtins_to_link; |
| 892 | |
| 893 | gl_shader **linking_shaders = |
| 894 | (gl_shader **) calloc(num_linking_shaders, sizeof(gl_shader *)); |
| 895 | |
| 896 | memcpy(linking_shaders, shader_list, |
| 897 | sizeof(linking_shaders[0]) * num_shaders); |
| 898 | |
| 899 | unsigned idx = num_shaders; |
| 900 | for (unsigned i = 0; i < num_shaders; i++) { |
| 901 | memcpy(&linking_shaders[idx], shader_list[i]->builtins_to_link, |
| 902 | sizeof(linking_shaders[0]) * shader_list[i]->num_builtins_to_link); |
| 903 | idx += shader_list[i]->num_builtins_to_link; |
| 904 | } |
| 905 | |
| 906 | assert(idx == num_linking_shaders); |
| 907 | |
Ian Romanick | 4a45595 | 2010-10-13 15:13:02 -0700 | [diff] [blame] | 908 | if (!link_function_calls(prog, linked, linking_shaders, |
| 909 | num_linking_shaders)) { |
| 910 | ctx->Driver.DeleteShader(ctx, linked); |
| 911 | linked = NULL; |
| 912 | } |
Ian Romanick | d5be2ac | 2010-07-20 11:29:46 -0700 | [diff] [blame] | 913 | |
| 914 | free(linking_shaders); |
Ian Romanick | 3fb8787 | 2010-07-09 14:09:34 -0700 | [diff] [blame] | 915 | |
Ian Romanick | c87e9ef | 2011-01-25 12:04:08 -0800 | [diff] [blame] | 916 | /* Make a pass over all variable declarations to ensure that arrays with |
Ian Romanick | 6f53921 | 2010-12-07 18:30:33 -0800 | [diff] [blame] | 917 | * unspecified sizes have a size specified. The size is inferred from the |
| 918 | * max_array_access field. |
| 919 | */ |
Ian Romanick | 002cd2c | 2010-12-07 19:00:44 -0800 | [diff] [blame] | 920 | if (linked != NULL) { |
Ian Romanick | c87e9ef | 2011-01-25 12:04:08 -0800 | [diff] [blame] | 921 | class array_sizing_visitor : public ir_hierarchical_visitor { |
| 922 | public: |
| 923 | virtual ir_visitor_status visit(ir_variable *var) |
| 924 | { |
| 925 | if (var->type->is_array() && (var->type->length == 0)) { |
| 926 | const glsl_type *type = |
| 927 | glsl_type::get_array_instance(var->type->fields.array, |
| 928 | var->max_array_access); |
Ian Romanick | 6f53921 | 2010-12-07 18:30:33 -0800 | [diff] [blame] | 929 | |
Ian Romanick | c87e9ef | 2011-01-25 12:04:08 -0800 | [diff] [blame] | 930 | assert(type != NULL); |
| 931 | var->type = type; |
| 932 | } |
Ian Romanick | 6f53921 | 2010-12-07 18:30:33 -0800 | [diff] [blame] | 933 | |
Ian Romanick | c87e9ef | 2011-01-25 12:04:08 -0800 | [diff] [blame] | 934 | return visit_continue; |
| 935 | } |
| 936 | } v; |
Ian Romanick | 6f53921 | 2010-12-07 18:30:33 -0800 | [diff] [blame] | 937 | |
Ian Romanick | c87e9ef | 2011-01-25 12:04:08 -0800 | [diff] [blame] | 938 | v.run(linked->ir); |
Ian Romanick | 6f53921 | 2010-12-07 18:30:33 -0800 | [diff] [blame] | 939 | } |
| 940 | |
Ian Romanick | 3fb8787 | 2010-07-09 14:09:34 -0700 | [diff] [blame] | 941 | return linked; |
| 942 | } |
| 943 | |
| 944 | |
Ian Romanick | 019a59b | 2010-06-21 16:10:42 -0700 | [diff] [blame] | 945 | struct uniform_node { |
| 946 | exec_node link; |
| 947 | struct gl_uniform *u; |
| 948 | unsigned slots; |
| 949 | }; |
| 950 | |
Eric Anholt | a721abf | 2010-08-23 10:32:01 -0700 | [diff] [blame] | 951 | /** |
| 952 | * Update the sizes of linked shader uniform arrays to the maximum |
| 953 | * array index used. |
| 954 | * |
| 955 | * From page 81 (page 95 of the PDF) of the OpenGL 2.1 spec: |
| 956 | * |
| 957 | * If one or more elements of an array are active, |
| 958 | * GetActiveUniform will return the name of the array in name, |
| 959 | * subject to the restrictions listed above. The type of the array |
| 960 | * is returned in type. The size parameter contains the highest |
| 961 | * array element index used, plus one. The compiler or linker |
| 962 | * determines the highest index used. There will be only one |
| 963 | * active uniform reported by the GL per uniform array. |
| 964 | |
| 965 | */ |
| 966 | static void |
Eric Anholt | 586b4b5 | 2010-09-28 14:32:16 -0700 | [diff] [blame] | 967 | update_array_sizes(struct gl_shader_program *prog) |
Eric Anholt | a721abf | 2010-08-23 10:32:01 -0700 | [diff] [blame] | 968 | { |
Ian Romanick | 3322fba | 2010-10-14 13:28:42 -0700 | [diff] [blame] | 969 | for (unsigned i = 0; i < MESA_SHADER_TYPES; i++) { |
| 970 | if (prog->_LinkedShaders[i] == NULL) |
| 971 | continue; |
| 972 | |
Eric Anholt | a721abf | 2010-08-23 10:32:01 -0700 | [diff] [blame] | 973 | foreach_list(node, prog->_LinkedShaders[i]->ir) { |
| 974 | ir_variable *const var = ((ir_instruction *) node)->as_variable(); |
| 975 | |
Eric Anholt | 586b4b5 | 2010-09-28 14:32:16 -0700 | [diff] [blame] | 976 | if ((var == NULL) || (var->mode != ir_var_uniform && |
| 977 | var->mode != ir_var_in && |
| 978 | var->mode != ir_var_out) || |
Eric Anholt | a721abf | 2010-08-23 10:32:01 -0700 | [diff] [blame] | 979 | !var->type->is_array()) |
| 980 | continue; |
| 981 | |
| 982 | unsigned int size = var->max_array_access; |
Ian Romanick | 3322fba | 2010-10-14 13:28:42 -0700 | [diff] [blame] | 983 | for (unsigned j = 0; j < MESA_SHADER_TYPES; j++) { |
| 984 | if (prog->_LinkedShaders[j] == NULL) |
| 985 | continue; |
| 986 | |
Eric Anholt | a721abf | 2010-08-23 10:32:01 -0700 | [diff] [blame] | 987 | foreach_list(node2, prog->_LinkedShaders[j]->ir) { |
| 988 | ir_variable *other_var = ((ir_instruction *) node2)->as_variable(); |
| 989 | if (!other_var) |
| 990 | continue; |
| 991 | |
| 992 | if (strcmp(var->name, other_var->name) == 0 && |
| 993 | other_var->max_array_access > size) { |
| 994 | size = other_var->max_array_access; |
| 995 | } |
| 996 | } |
| 997 | } |
Eric Anholt | 586b4b5 | 2010-09-28 14:32:16 -0700 | [diff] [blame] | 998 | |
Eric Anholt | a721abf | 2010-08-23 10:32:01 -0700 | [diff] [blame] | 999 | if (size + 1 != var->type->fields.array->length) { |
| 1000 | var->type = glsl_type::get_array_instance(var->type->fields.array, |
| 1001 | size + 1); |
| 1002 | /* FINISHME: We should update the types of array |
| 1003 | * dereferences of this variable now. |
| 1004 | */ |
| 1005 | } |
| 1006 | } |
| 1007 | } |
| 1008 | } |
| 1009 | |
Eric Anholt | 45388b5 | 2010-08-24 13:51:35 -0700 | [diff] [blame] | 1010 | static void |
| 1011 | add_uniform(void *mem_ctx, exec_list *uniforms, struct hash_table *ht, |
| 1012 | const char *name, const glsl_type *type, GLenum shader_type, |
| 1013 | unsigned *next_shader_pos, unsigned *total_uniforms) |
| 1014 | { |
| 1015 | if (type->is_record()) { |
| 1016 | for (unsigned int i = 0; i < type->length; i++) { |
| 1017 | const glsl_type *field_type = type->fields.structure[i].type; |
Kenneth Graunke | d3073f5 | 2011-01-21 14:32:31 -0800 | [diff] [blame^] | 1018 | char *field_name = ralloc_asprintf(mem_ctx, "%s.%s", name, |
Eric Anholt | 45388b5 | 2010-08-24 13:51:35 -0700 | [diff] [blame] | 1019 | type->fields.structure[i].name); |
| 1020 | |
| 1021 | add_uniform(mem_ctx, uniforms, ht, field_name, field_type, |
| 1022 | shader_type, next_shader_pos, total_uniforms); |
| 1023 | } |
| 1024 | } else { |
| 1025 | uniform_node *n = (uniform_node *) hash_table_find(ht, name); |
| 1026 | unsigned int vec4_slots; |
| 1027 | const glsl_type *array_elem_type = NULL; |
| 1028 | |
| 1029 | if (type->is_array()) { |
| 1030 | array_elem_type = type->fields.array; |
| 1031 | /* Array of structures. */ |
| 1032 | if (array_elem_type->is_record()) { |
| 1033 | for (unsigned int i = 0; i < type->length; i++) { |
Kenneth Graunke | d3073f5 | 2011-01-21 14:32:31 -0800 | [diff] [blame^] | 1034 | char *elem_name = ralloc_asprintf(mem_ctx, "%s[%d]", name, i); |
Eric Anholt | 45388b5 | 2010-08-24 13:51:35 -0700 | [diff] [blame] | 1035 | add_uniform(mem_ctx, uniforms, ht, elem_name, array_elem_type, |
| 1036 | shader_type, next_shader_pos, total_uniforms); |
| 1037 | } |
| 1038 | return; |
| 1039 | } |
| 1040 | } |
| 1041 | |
| 1042 | /* Fix the storage size of samplers at 1 vec4 each. Be sure to pad out |
| 1043 | * vectors to vec4 slots. |
| 1044 | */ |
| 1045 | if (type->is_array()) { |
| 1046 | if (array_elem_type->is_sampler()) |
| 1047 | vec4_slots = type->length; |
| 1048 | else |
| 1049 | vec4_slots = type->length * array_elem_type->matrix_columns; |
| 1050 | } else if (type->is_sampler()) { |
| 1051 | vec4_slots = 1; |
| 1052 | } else { |
| 1053 | vec4_slots = type->matrix_columns; |
| 1054 | } |
| 1055 | |
| 1056 | if (n == NULL) { |
| 1057 | n = (uniform_node *) calloc(1, sizeof(struct uniform_node)); |
| 1058 | n->u = (gl_uniform *) calloc(1, sizeof(struct gl_uniform)); |
| 1059 | n->slots = vec4_slots; |
| 1060 | |
| 1061 | n->u->Name = strdup(name); |
Eric Anholt | 0924ba0 | 2010-08-24 14:27:27 -0700 | [diff] [blame] | 1062 | n->u->Type = type; |
Eric Anholt | 45388b5 | 2010-08-24 13:51:35 -0700 | [diff] [blame] | 1063 | n->u->VertPos = -1; |
| 1064 | n->u->FragPos = -1; |
| 1065 | n->u->GeomPos = -1; |
| 1066 | (*total_uniforms)++; |
| 1067 | |
| 1068 | hash_table_insert(ht, n, name); |
| 1069 | uniforms->push_tail(& n->link); |
| 1070 | } |
| 1071 | |
| 1072 | switch (shader_type) { |
| 1073 | case GL_VERTEX_SHADER: |
| 1074 | n->u->VertPos = *next_shader_pos; |
| 1075 | break; |
| 1076 | case GL_FRAGMENT_SHADER: |
| 1077 | n->u->FragPos = *next_shader_pos; |
| 1078 | break; |
| 1079 | case GL_GEOMETRY_SHADER: |
| 1080 | n->u->GeomPos = *next_shader_pos; |
| 1081 | break; |
| 1082 | } |
| 1083 | |
| 1084 | (*next_shader_pos) += vec4_slots; |
| 1085 | } |
| 1086 | } |
| 1087 | |
Ian Romanick | abee16e | 2010-06-21 16:16:05 -0700 | [diff] [blame] | 1088 | void |
Eric Anholt | 849e181 | 2010-06-30 11:49:17 -0700 | [diff] [blame] | 1089 | assign_uniform_locations(struct gl_shader_program *prog) |
Ian Romanick | 019a59b | 2010-06-21 16:10:42 -0700 | [diff] [blame] | 1090 | { |
| 1091 | /* */ |
| 1092 | exec_list uniforms; |
| 1093 | unsigned total_uniforms = 0; |
| 1094 | hash_table *ht = hash_table_ctor(32, hash_table_string_hash, |
| 1095 | hash_table_string_compare); |
Kenneth Graunke | d3073f5 | 2011-01-21 14:32:31 -0800 | [diff] [blame^] | 1096 | void *mem_ctx = ralloc_context(NULL); |
Ian Romanick | 019a59b | 2010-06-21 16:10:42 -0700 | [diff] [blame] | 1097 | |
Ian Romanick | 3322fba | 2010-10-14 13:28:42 -0700 | [diff] [blame] | 1098 | for (unsigned i = 0; i < MESA_SHADER_TYPES; i++) { |
| 1099 | if (prog->_LinkedShaders[i] == NULL) |
| 1100 | continue; |
| 1101 | |
Ian Romanick | 019a59b | 2010-06-21 16:10:42 -0700 | [diff] [blame] | 1102 | unsigned next_position = 0; |
| 1103 | |
Eric Anholt | 16b68b1 | 2010-06-30 11:05:43 -0700 | [diff] [blame] | 1104 | foreach_list(node, prog->_LinkedShaders[i]->ir) { |
Ian Romanick | 019a59b | 2010-06-21 16:10:42 -0700 | [diff] [blame] | 1105 | ir_variable *const var = ((ir_instruction *) node)->as_variable(); |
| 1106 | |
| 1107 | if ((var == NULL) || (var->mode != ir_var_uniform)) |
| 1108 | continue; |
| 1109 | |
Eric Anholt | 45388b5 | 2010-08-24 13:51:35 -0700 | [diff] [blame] | 1110 | if (strncmp(var->name, "gl_", 3) == 0) { |
| 1111 | /* At the moment, we don't allocate uniform locations for |
| 1112 | * builtin uniforms. It's permitted by spec, and we'll |
| 1113 | * likely switch to doing that at some point, but not yet. |
Eric Anholt | 8d61a23 | 2010-08-05 16:00:46 -0700 | [diff] [blame] | 1114 | */ |
| 1115 | continue; |
| 1116 | } |
Ian Romanick | 019a59b | 2010-06-21 16:10:42 -0700 | [diff] [blame] | 1117 | |
Ian Romanick | 019a59b | 2010-06-21 16:10:42 -0700 | [diff] [blame] | 1118 | var->location = next_position; |
Eric Anholt | 45388b5 | 2010-08-24 13:51:35 -0700 | [diff] [blame] | 1119 | add_uniform(mem_ctx, &uniforms, ht, var->name, var->type, |
| 1120 | prog->_LinkedShaders[i]->Type, |
| 1121 | &next_position, &total_uniforms); |
Ian Romanick | 019a59b | 2010-06-21 16:10:42 -0700 | [diff] [blame] | 1122 | } |
| 1123 | } |
| 1124 | |
Kenneth Graunke | d3073f5 | 2011-01-21 14:32:31 -0800 | [diff] [blame^] | 1125 | ralloc_free(mem_ctx); |
Eric Anholt | 45388b5 | 2010-08-24 13:51:35 -0700 | [diff] [blame] | 1126 | |
Ian Romanick | 019a59b | 2010-06-21 16:10:42 -0700 | [diff] [blame] | 1127 | gl_uniform_list *ul = (gl_uniform_list *) |
| 1128 | calloc(1, sizeof(gl_uniform_list)); |
| 1129 | |
| 1130 | ul->Size = total_uniforms; |
| 1131 | ul->NumUniforms = total_uniforms; |
| 1132 | ul->Uniforms = (gl_uniform *) calloc(total_uniforms, sizeof(gl_uniform)); |
| 1133 | |
| 1134 | unsigned idx = 0; |
| 1135 | uniform_node *next; |
| 1136 | for (uniform_node *node = (uniform_node *) uniforms.head |
| 1137 | ; node->link.next != NULL |
| 1138 | ; node = next) { |
| 1139 | next = (uniform_node *) node->link.next; |
| 1140 | |
| 1141 | node->link.remove(); |
Eric Anholt | 45388b5 | 2010-08-24 13:51:35 -0700 | [diff] [blame] | 1142 | memcpy(&ul->Uniforms[idx], node->u, sizeof(gl_uniform)); |
| 1143 | idx++; |
Ian Romanick | 019a59b | 2010-06-21 16:10:42 -0700 | [diff] [blame] | 1144 | |
| 1145 | free(node->u); |
| 1146 | free(node); |
| 1147 | } |
| 1148 | |
| 1149 | hash_table_dtor(ht); |
| 1150 | |
Ian Romanick | abee16e | 2010-06-21 16:16:05 -0700 | [diff] [blame] | 1151 | prog->Uniforms = ul; |
Ian Romanick | 019a59b | 2010-06-21 16:10:42 -0700 | [diff] [blame] | 1152 | } |
| 1153 | |
| 1154 | |
Ian Romanick | 6984670 | 2010-06-22 17:29:19 -0700 | [diff] [blame] | 1155 | /** |
| 1156 | * Find a contiguous set of available bits in a bitmask |
| 1157 | * |
| 1158 | * \param used_mask Bits representing used (1) and unused (0) locations |
| 1159 | * \param needed_count Number of contiguous bits needed. |
| 1160 | * |
| 1161 | * \return |
| 1162 | * Base location of the available bits on success or -1 on failure. |
| 1163 | */ |
| 1164 | int |
| 1165 | find_available_slots(unsigned used_mask, unsigned needed_count) |
| 1166 | { |
| 1167 | unsigned needed_mask = (1 << needed_count) - 1; |
| 1168 | const int max_bit_to_test = (8 * sizeof(used_mask)) - needed_count; |
| 1169 | |
| 1170 | /* The comparison to 32 is redundant, but without it GCC emits "warning: |
| 1171 | * cannot optimize possibly infinite loops" for the loop below. |
| 1172 | */ |
| 1173 | if ((needed_count == 0) || (max_bit_to_test < 0) || (max_bit_to_test > 32)) |
| 1174 | return -1; |
| 1175 | |
| 1176 | for (int i = 0; i <= max_bit_to_test; i++) { |
| 1177 | if ((needed_mask & ~used_mask) == needed_mask) |
| 1178 | return i; |
| 1179 | |
| 1180 | needed_mask <<= 1; |
| 1181 | } |
| 1182 | |
| 1183 | return -1; |
| 1184 | } |
| 1185 | |
| 1186 | |
| 1187 | bool |
Eric Anholt | 849e181 | 2010-06-30 11:49:17 -0700 | [diff] [blame] | 1188 | assign_attribute_locations(gl_shader_program *prog, unsigned max_attribute_index) |
Ian Romanick | 0ad22cd | 2010-06-21 17:18:31 -0700 | [diff] [blame] | 1189 | { |
Ian Romanick | 9342d26 | 2010-06-22 17:41:37 -0700 | [diff] [blame] | 1190 | /* Mark invalid attribute locations as being used. |
| 1191 | */ |
| 1192 | unsigned used_locations = (max_attribute_index >= 32) |
| 1193 | ? ~0 : ~((1 << max_attribute_index) - 1); |
Ian Romanick | 0ad22cd | 2010-06-21 17:18:31 -0700 | [diff] [blame] | 1194 | |
Eric Anholt | 16b68b1 | 2010-06-30 11:05:43 -0700 | [diff] [blame] | 1195 | gl_shader *const sh = prog->_LinkedShaders[0]; |
Ian Romanick | 0ad22cd | 2010-06-21 17:18:31 -0700 | [diff] [blame] | 1196 | assert(sh->Type == GL_VERTEX_SHADER); |
| 1197 | |
Ian Romanick | 6984670 | 2010-06-22 17:29:19 -0700 | [diff] [blame] | 1198 | /* Operate in a total of four passes. |
Ian Romanick | 0ad22cd | 2010-06-21 17:18:31 -0700 | [diff] [blame] | 1199 | * |
| 1200 | * 1. Invalidate the location assignments for all vertex shader inputs. |
| 1201 | * |
| 1202 | * 2. Assign locations for inputs that have user-defined (via |
| 1203 | * glBindVertexAttribLocation) locatoins. |
| 1204 | * |
Ian Romanick | 6984670 | 2010-06-22 17:29:19 -0700 | [diff] [blame] | 1205 | * 3. Sort the attributes without assigned locations by number of slots |
| 1206 | * required in decreasing order. Fragmentation caused by attribute |
| 1207 | * locations assigned by the application may prevent large attributes |
| 1208 | * from having enough contiguous space. |
| 1209 | * |
| 1210 | * 4. Assign locations to any inputs without assigned locations. |
Ian Romanick | 0ad22cd | 2010-06-21 17:18:31 -0700 | [diff] [blame] | 1211 | */ |
| 1212 | |
| 1213 | invalidate_variable_locations(sh, ir_var_in, VERT_ATTRIB_GENERIC0); |
| 1214 | |
Ian Romanick | 553dcdc | 2010-06-23 12:14:02 -0700 | [diff] [blame] | 1215 | if (prog->Attributes != NULL) { |
| 1216 | for (unsigned i = 0; i < prog->Attributes->NumParameters; i++) { |
Ian Romanick | 0ad22cd | 2010-06-21 17:18:31 -0700 | [diff] [blame] | 1217 | ir_variable *const var = |
Ian Romanick | 553dcdc | 2010-06-23 12:14:02 -0700 | [diff] [blame] | 1218 | sh->symbols->get_variable(prog->Attributes->Parameters[i].Name); |
Ian Romanick | 0ad22cd | 2010-06-21 17:18:31 -0700 | [diff] [blame] | 1219 | |
Ian Romanick | 6984670 | 2010-06-22 17:29:19 -0700 | [diff] [blame] | 1220 | /* Note: attributes that occupy multiple slots, such as arrays or |
| 1221 | * matrices, may appear in the attrib array multiple times. |
| 1222 | */ |
| 1223 | if ((var == NULL) || (var->location != -1)) |
Ian Romanick | 0ad22cd | 2010-06-21 17:18:31 -0700 | [diff] [blame] | 1224 | continue; |
| 1225 | |
Ian Romanick | 6984670 | 2010-06-22 17:29:19 -0700 | [diff] [blame] | 1226 | /* From page 61 of the OpenGL 4.0 spec: |
| 1227 | * |
| 1228 | * "LinkProgram will fail if the attribute bindings assigned by |
| 1229 | * BindAttribLocation do not leave not enough space to assign a |
| 1230 | * location for an active matrix attribute or an active attribute |
| 1231 | * array, both of which require multiple contiguous generic |
| 1232 | * attributes." |
| 1233 | * |
| 1234 | * Previous versions of the spec contain similar language but omit the |
| 1235 | * bit about attribute arrays. |
| 1236 | * |
| 1237 | * Page 61 of the OpenGL 4.0 spec also says: |
| 1238 | * |
| 1239 | * "It is possible for an application to bind more than one |
| 1240 | * attribute name to the same location. This is referred to as |
| 1241 | * aliasing. This will only work if only one of the aliased |
| 1242 | * attributes is active in the executable program, or if no path |
| 1243 | * through the shader consumes more than one attribute of a set |
| 1244 | * of attributes aliased to the same location. A link error can |
| 1245 | * occur if the linker determines that every path through the |
| 1246 | * shader consumes multiple aliased attributes, but |
| 1247 | * implementations are not required to generate an error in this |
| 1248 | * case." |
| 1249 | * |
| 1250 | * These two paragraphs are either somewhat contradictory, or I don't |
| 1251 | * fully understand one or both of them. |
| 1252 | */ |
| 1253 | /* FINISHME: The code as currently written does not support attribute |
| 1254 | * FINISHME: location aliasing (see comment above). |
| 1255 | */ |
Ian Romanick | 553dcdc | 2010-06-23 12:14:02 -0700 | [diff] [blame] | 1256 | const int attr = prog->Attributes->Parameters[i].StateIndexes[0]; |
Ian Romanick | 6984670 | 2010-06-22 17:29:19 -0700 | [diff] [blame] | 1257 | const unsigned slots = count_attribute_slots(var->type); |
| 1258 | |
| 1259 | /* Mask representing the contiguous slots that will be used by this |
| 1260 | * attribute. |
| 1261 | */ |
| 1262 | const unsigned use_mask = (1 << slots) - 1; |
| 1263 | |
| 1264 | /* Generate a link error if the set of bits requested for this |
| 1265 | * attribute overlaps any previously allocated bits. |
| 1266 | */ |
| 1267 | if ((~(use_mask << attr) & used_locations) != used_locations) { |
Ian Romanick | 553dcdc | 2010-06-23 12:14:02 -0700 | [diff] [blame] | 1268 | linker_error_printf(prog, |
| 1269 | "insufficient contiguous attribute locations " |
| 1270 | "available for vertex shader input `%s'", |
| 1271 | var->name); |
Ian Romanick | 6984670 | 2010-06-22 17:29:19 -0700 | [diff] [blame] | 1272 | return false; |
| 1273 | } |
Ian Romanick | 0ad22cd | 2010-06-21 17:18:31 -0700 | [diff] [blame] | 1274 | |
| 1275 | var->location = VERT_ATTRIB_GENERIC0 + attr; |
Ian Romanick | 6984670 | 2010-06-22 17:29:19 -0700 | [diff] [blame] | 1276 | used_locations |= (use_mask << attr); |
Ian Romanick | 0ad22cd | 2010-06-21 17:18:31 -0700 | [diff] [blame] | 1277 | } |
| 1278 | } |
| 1279 | |
Ian Romanick | 6984670 | 2010-06-22 17:29:19 -0700 | [diff] [blame] | 1280 | /* Temporary storage for the set of attributes that need locations assigned. |
| 1281 | */ |
| 1282 | struct temp_attr { |
| 1283 | unsigned slots; |
| 1284 | ir_variable *var; |
| 1285 | |
| 1286 | /* Used below in the call to qsort. */ |
| 1287 | static int compare(const void *a, const void *b) |
| 1288 | { |
| 1289 | const temp_attr *const l = (const temp_attr *) a; |
| 1290 | const temp_attr *const r = (const temp_attr *) b; |
| 1291 | |
| 1292 | /* Reversed because we want a descending order sort below. */ |
| 1293 | return r->slots - l->slots; |
| 1294 | } |
| 1295 | } to_assign[16]; |
| 1296 | |
| 1297 | unsigned num_attr = 0; |
| 1298 | |
Eric Anholt | 16b68b1 | 2010-06-30 11:05:43 -0700 | [diff] [blame] | 1299 | foreach_list(node, sh->ir) { |
Ian Romanick | 0ad22cd | 2010-06-21 17:18:31 -0700 | [diff] [blame] | 1300 | ir_variable *const var = ((ir_instruction *) node)->as_variable(); |
| 1301 | |
| 1302 | if ((var == NULL) || (var->mode != ir_var_in)) |
| 1303 | continue; |
| 1304 | |
Ian Romanick | 68a4fc9 | 2010-10-07 17:21:22 -0700 | [diff] [blame] | 1305 | if (var->explicit_location) { |
| 1306 | const unsigned slots = count_attribute_slots(var->type); |
| 1307 | const unsigned use_mask = (1 << slots) - 1; |
| 1308 | const int attr = var->location - VERT_ATTRIB_GENERIC0; |
| 1309 | |
| 1310 | if ((var->location >= (int)(max_attribute_index + VERT_ATTRIB_GENERIC0)) |
| 1311 | || (var->location < 0)) { |
| 1312 | linker_error_printf(prog, |
| 1313 | "invalid explicit location %d specified for " |
| 1314 | "`%s'\n", |
| 1315 | (var->location < 0) ? var->location : attr, |
| 1316 | var->name); |
| 1317 | return false; |
| 1318 | } else if (var->location >= VERT_ATTRIB_GENERIC0) { |
| 1319 | used_locations |= (use_mask << attr); |
| 1320 | } |
| 1321 | } |
| 1322 | |
Ian Romanick | 0ad22cd | 2010-06-21 17:18:31 -0700 | [diff] [blame] | 1323 | /* The location was explicitly assigned, nothing to do here. |
| 1324 | */ |
| 1325 | if (var->location != -1) |
| 1326 | continue; |
| 1327 | |
Ian Romanick | 6984670 | 2010-06-22 17:29:19 -0700 | [diff] [blame] | 1328 | to_assign[num_attr].slots = count_attribute_slots(var->type); |
| 1329 | to_assign[num_attr].var = var; |
| 1330 | num_attr++; |
Ian Romanick | 0ad22cd | 2010-06-21 17:18:31 -0700 | [diff] [blame] | 1331 | } |
Ian Romanick | 6984670 | 2010-06-22 17:29:19 -0700 | [diff] [blame] | 1332 | |
| 1333 | /* If all of the attributes were assigned locations by the application (or |
| 1334 | * are built-in attributes with fixed locations), return early. This should |
| 1335 | * be the common case. |
| 1336 | */ |
| 1337 | if (num_attr == 0) |
| 1338 | return true; |
| 1339 | |
| 1340 | qsort(to_assign, num_attr, sizeof(to_assign[0]), temp_attr::compare); |
| 1341 | |
Ian Romanick | 982e379 | 2010-06-29 18:58:20 -0700 | [diff] [blame] | 1342 | /* VERT_ATTRIB_GENERIC0 is a psdueo-alias for VERT_ATTRIB_POS. It can only |
| 1343 | * be explicitly assigned by via glBindAttribLocation. Mark it as reserved |
| 1344 | * to prevent it from being automatically allocated below. |
| 1345 | */ |
Ian Romanick | c33e78f | 2010-08-13 12:30:41 -0700 | [diff] [blame] | 1346 | find_deref_visitor find("gl_Vertex"); |
| 1347 | find.run(sh->ir); |
| 1348 | if (find.variable_found()) |
| 1349 | used_locations |= (1 << 0); |
Ian Romanick | 982e379 | 2010-06-29 18:58:20 -0700 | [diff] [blame] | 1350 | |
Ian Romanick | 6984670 | 2010-06-22 17:29:19 -0700 | [diff] [blame] | 1351 | for (unsigned i = 0; i < num_attr; i++) { |
| 1352 | /* Mask representing the contiguous slots that will be used by this |
| 1353 | * attribute. |
| 1354 | */ |
| 1355 | const unsigned use_mask = (1 << to_assign[i].slots) - 1; |
| 1356 | |
| 1357 | int location = find_available_slots(used_locations, to_assign[i].slots); |
| 1358 | |
| 1359 | if (location < 0) { |
Ian Romanick | 553dcdc | 2010-06-23 12:14:02 -0700 | [diff] [blame] | 1360 | linker_error_printf(prog, |
| 1361 | "insufficient contiguous attribute locations " |
| 1362 | "available for vertex shader input `%s'", |
| 1363 | to_assign[i].var->name); |
Ian Romanick | 6984670 | 2010-06-22 17:29:19 -0700 | [diff] [blame] | 1364 | return false; |
| 1365 | } |
| 1366 | |
| 1367 | to_assign[i].var->location = VERT_ATTRIB_GENERIC0 + location; |
| 1368 | used_locations |= (use_mask << location); |
| 1369 | } |
| 1370 | |
| 1371 | return true; |
Ian Romanick | 0ad22cd | 2010-06-21 17:18:31 -0700 | [diff] [blame] | 1372 | } |
| 1373 | |
| 1374 | |
Ian Romanick | 40e114b | 2010-08-17 14:55:50 -0700 | [diff] [blame] | 1375 | /** |
Ian Romanick | cc90e62 | 2010-10-19 17:59:10 -0700 | [diff] [blame] | 1376 | * Demote shader inputs and outputs that are not used in other stages |
Ian Romanick | 40e114b | 2010-08-17 14:55:50 -0700 | [diff] [blame] | 1377 | */ |
| 1378 | void |
Ian Romanick | cc90e62 | 2010-10-19 17:59:10 -0700 | [diff] [blame] | 1379 | 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] | 1380 | { |
| 1381 | foreach_list(node, sh->ir) { |
| 1382 | ir_variable *const var = ((ir_instruction *) node)->as_variable(); |
| 1383 | |
Ian Romanick | cc90e62 | 2010-10-19 17:59:10 -0700 | [diff] [blame] | 1384 | if ((var == NULL) || (var->mode != int(mode))) |
Ian Romanick | 40e114b | 2010-08-17 14:55:50 -0700 | [diff] [blame] | 1385 | continue; |
| 1386 | |
Ian Romanick | cc90e62 | 2010-10-19 17:59:10 -0700 | [diff] [blame] | 1387 | /* A shader 'in' or 'out' variable is only really an input or output if |
| 1388 | * its value is used by other shader stages. This will cause the variable |
| 1389 | * to have a location assigned. |
Ian Romanick | 40e114b | 2010-08-17 14:55:50 -0700 | [diff] [blame] | 1390 | */ |
| 1391 | if (var->location == -1) { |
| 1392 | var->mode = ir_var_auto; |
| 1393 | } |
| 1394 | } |
| 1395 | } |
| 1396 | |
| 1397 | |
Ian Romanick | 0ad22cd | 2010-06-21 17:18:31 -0700 | [diff] [blame] | 1398 | void |
Eric Anholt | b706283 | 2010-07-28 13:52:23 -0700 | [diff] [blame] | 1399 | assign_varying_locations(struct gl_shader_program *prog, |
| 1400 | gl_shader *producer, gl_shader *consumer) |
Ian Romanick | 0e59b26 | 2010-06-23 11:23:01 -0700 | [diff] [blame] | 1401 | { |
| 1402 | /* FINISHME: Set dynamically when geometry shader support is added. */ |
| 1403 | unsigned output_index = VERT_RESULT_VAR0; |
| 1404 | unsigned input_index = FRAG_ATTRIB_VAR0; |
| 1405 | |
| 1406 | /* Operate in a total of three passes. |
| 1407 | * |
| 1408 | * 1. Assign locations for any matching inputs and outputs. |
| 1409 | * |
| 1410 | * 2. Mark output variables in the producer that do not have locations as |
| 1411 | * not being outputs. This lets the optimizer eliminate them. |
| 1412 | * |
| 1413 | * 3. Mark input variables in the consumer that do not have locations as |
| 1414 | * not being inputs. This lets the optimizer eliminate them. |
| 1415 | */ |
| 1416 | |
| 1417 | invalidate_variable_locations(producer, ir_var_out, VERT_RESULT_VAR0); |
| 1418 | invalidate_variable_locations(consumer, ir_var_in, FRAG_ATTRIB_VAR0); |
| 1419 | |
Eric Anholt | 16b68b1 | 2010-06-30 11:05:43 -0700 | [diff] [blame] | 1420 | foreach_list(node, producer->ir) { |
Ian Romanick | 0e59b26 | 2010-06-23 11:23:01 -0700 | [diff] [blame] | 1421 | ir_variable *const output_var = ((ir_instruction *) node)->as_variable(); |
| 1422 | |
| 1423 | if ((output_var == NULL) || (output_var->mode != ir_var_out) |
| 1424 | || (output_var->location != -1)) |
| 1425 | continue; |
| 1426 | |
| 1427 | ir_variable *const input_var = |
| 1428 | consumer->symbols->get_variable(output_var->name); |
| 1429 | |
| 1430 | if ((input_var == NULL) || (input_var->mode != ir_var_in)) |
| 1431 | continue; |
| 1432 | |
| 1433 | assert(input_var->location == -1); |
| 1434 | |
Ian Romanick | 0e59b26 | 2010-06-23 11:23:01 -0700 | [diff] [blame] | 1435 | output_var->location = output_index; |
| 1436 | input_var->location = input_index; |
| 1437 | |
Ian Romanick | df869d9 | 2010-08-30 15:37:44 -0700 | [diff] [blame] | 1438 | /* FINISHME: Support for "varying" records in GLSL 1.50. */ |
| 1439 | assert(!output_var->type->is_record()); |
| 1440 | |
| 1441 | if (output_var->type->is_array()) { |
| 1442 | const unsigned slots = output_var->type->length |
| 1443 | * output_var->type->fields.array->matrix_columns; |
| 1444 | |
| 1445 | output_index += slots; |
| 1446 | input_index += slots; |
| 1447 | } else { |
| 1448 | const unsigned slots = output_var->type->matrix_columns; |
| 1449 | |
| 1450 | output_index += slots; |
| 1451 | input_index += slots; |
| 1452 | } |
Ian Romanick | 0e59b26 | 2010-06-23 11:23:01 -0700 | [diff] [blame] | 1453 | } |
| 1454 | |
Eric Anholt | 16b68b1 | 2010-06-30 11:05:43 -0700 | [diff] [blame] | 1455 | foreach_list(node, consumer->ir) { |
Ian Romanick | 0e59b26 | 2010-06-23 11:23:01 -0700 | [diff] [blame] | 1456 | ir_variable *const var = ((ir_instruction *) node)->as_variable(); |
| 1457 | |
| 1458 | if ((var == NULL) || (var->mode != ir_var_in)) |
| 1459 | continue; |
| 1460 | |
Eric Anholt | b706283 | 2010-07-28 13:52:23 -0700 | [diff] [blame] | 1461 | if (var->location == -1) { |
| 1462 | if (prog->Version <= 120) { |
| 1463 | /* On page 25 (page 31 of the PDF) of the GLSL 1.20 spec: |
| 1464 | * |
| 1465 | * Only those varying variables used (i.e. read) in |
| 1466 | * the fragment shader executable must be written to |
| 1467 | * by the vertex shader executable; declaring |
| 1468 | * superfluous varying variables in a vertex shader is |
| 1469 | * permissible. |
| 1470 | * |
| 1471 | * We interpret this text as meaning that the VS must |
| 1472 | * write the variable for the FS to read it. See |
| 1473 | * "glsl1-varying read but not written" in piglit. |
| 1474 | */ |
| 1475 | |
| 1476 | linker_error_printf(prog, "fragment shader varying %s not written " |
| 1477 | "by vertex shader\n.", var->name); |
| 1478 | prog->LinkStatus = false; |
| 1479 | } |
| 1480 | |
| 1481 | /* An 'in' variable is only really a shader input if its |
| 1482 | * value is written by the previous stage. |
| 1483 | */ |
Eric Anholt | b706283 | 2010-07-28 13:52:23 -0700 | [diff] [blame] | 1484 | var->mode = ir_var_auto; |
| 1485 | } |
Ian Romanick | 0e59b26 | 2010-06-23 11:23:01 -0700 | [diff] [blame] | 1486 | } |
| 1487 | } |
| 1488 | |
| 1489 | |
| 1490 | void |
Kristian Høgsberg | f9995b3 | 2010-10-12 12:26:10 -0400 | [diff] [blame] | 1491 | link_shaders(struct gl_context *ctx, struct gl_shader_program *prog) |
Ian Romanick | 832dfa5 | 2010-06-17 15:04:20 -0700 | [diff] [blame] | 1492 | { |
Kenneth Graunke | d3073f5 | 2011-01-21 14:32:31 -0800 | [diff] [blame^] | 1493 | void *mem_ctx = ralloc_context(NULL); // temporary linker context |
Kenneth Graunke | 2da02e7 | 2010-11-17 11:03:57 -0800 | [diff] [blame] | 1494 | |
Ian Romanick | 832dfa5 | 2010-06-17 15:04:20 -0700 | [diff] [blame] | 1495 | prog->LinkStatus = false; |
| 1496 | prog->Validated = false; |
| 1497 | prog->_Used = false; |
| 1498 | |
Ian Romanick | f36460e | 2010-06-23 12:07:22 -0700 | [diff] [blame] | 1499 | if (prog->InfoLog != NULL) |
Kenneth Graunke | d3073f5 | 2011-01-21 14:32:31 -0800 | [diff] [blame^] | 1500 | ralloc_free(prog->InfoLog); |
Ian Romanick | f36460e | 2010-06-23 12:07:22 -0700 | [diff] [blame] | 1501 | |
Kenneth Graunke | d3073f5 | 2011-01-21 14:32:31 -0800 | [diff] [blame^] | 1502 | prog->InfoLog = ralloc_strdup(NULL, ""); |
Ian Romanick | f36460e | 2010-06-23 12:07:22 -0700 | [diff] [blame] | 1503 | |
Ian Romanick | 832dfa5 | 2010-06-17 15:04:20 -0700 | [diff] [blame] | 1504 | /* Separate the shaders into groups based on their type. |
| 1505 | */ |
Eric Anholt | 16b68b1 | 2010-06-30 11:05:43 -0700 | [diff] [blame] | 1506 | struct gl_shader **vert_shader_list; |
Ian Romanick | 832dfa5 | 2010-06-17 15:04:20 -0700 | [diff] [blame] | 1507 | unsigned num_vert_shaders = 0; |
Eric Anholt | 16b68b1 | 2010-06-30 11:05:43 -0700 | [diff] [blame] | 1508 | struct gl_shader **frag_shader_list; |
Ian Romanick | 832dfa5 | 2010-06-17 15:04:20 -0700 | [diff] [blame] | 1509 | unsigned num_frag_shaders = 0; |
| 1510 | |
Eric Anholt | 16b68b1 | 2010-06-30 11:05:43 -0700 | [diff] [blame] | 1511 | vert_shader_list = (struct gl_shader **) |
| 1512 | calloc(2 * prog->NumShaders, sizeof(struct gl_shader *)); |
Ian Romanick | 832dfa5 | 2010-06-17 15:04:20 -0700 | [diff] [blame] | 1513 | frag_shader_list = &vert_shader_list[prog->NumShaders]; |
| 1514 | |
Ian Romanick | 25f51d3 | 2010-07-16 15:51:50 -0700 | [diff] [blame] | 1515 | unsigned min_version = UINT_MAX; |
| 1516 | unsigned max_version = 0; |
Ian Romanick | 832dfa5 | 2010-06-17 15:04:20 -0700 | [diff] [blame] | 1517 | for (unsigned i = 0; i < prog->NumShaders; i++) { |
Ian Romanick | 25f51d3 | 2010-07-16 15:51:50 -0700 | [diff] [blame] | 1518 | min_version = MIN2(min_version, prog->Shaders[i]->Version); |
| 1519 | max_version = MAX2(max_version, prog->Shaders[i]->Version); |
| 1520 | |
Ian Romanick | 832dfa5 | 2010-06-17 15:04:20 -0700 | [diff] [blame] | 1521 | switch (prog->Shaders[i]->Type) { |
| 1522 | case GL_VERTEX_SHADER: |
| 1523 | vert_shader_list[num_vert_shaders] = prog->Shaders[i]; |
| 1524 | num_vert_shaders++; |
| 1525 | break; |
| 1526 | case GL_FRAGMENT_SHADER: |
| 1527 | frag_shader_list[num_frag_shaders] = prog->Shaders[i]; |
| 1528 | num_frag_shaders++; |
| 1529 | break; |
| 1530 | case GL_GEOMETRY_SHADER: |
| 1531 | /* FINISHME: Support geometry shaders. */ |
| 1532 | assert(prog->Shaders[i]->Type != GL_GEOMETRY_SHADER); |
| 1533 | break; |
| 1534 | } |
| 1535 | } |
| 1536 | |
Ian Romanick | 25f51d3 | 2010-07-16 15:51:50 -0700 | [diff] [blame] | 1537 | /* Previous to GLSL version 1.30, different compilation units could mix and |
| 1538 | * match shading language versions. With GLSL 1.30 and later, the versions |
| 1539 | * of all shaders must match. |
| 1540 | */ |
Kenneth Graunke | 5a81d05 | 2010-08-31 09:33:58 -0700 | [diff] [blame] | 1541 | assert(min_version >= 100); |
Ian Romanick | 25f51d3 | 2010-07-16 15:51:50 -0700 | [diff] [blame] | 1542 | assert(max_version <= 130); |
Kenneth Graunke | 5a81d05 | 2010-08-31 09:33:58 -0700 | [diff] [blame] | 1543 | if ((max_version >= 130 || min_version == 100) |
| 1544 | && min_version != max_version) { |
Ian Romanick | 25f51d3 | 2010-07-16 15:51:50 -0700 | [diff] [blame] | 1545 | linker_error_printf(prog, "all shaders must use same shading " |
| 1546 | "language version\n"); |
| 1547 | goto done; |
| 1548 | } |
| 1549 | |
| 1550 | prog->Version = max_version; |
| 1551 | |
Ian Romanick | 3322fba | 2010-10-14 13:28:42 -0700 | [diff] [blame] | 1552 | for (unsigned int i = 0; i < MESA_SHADER_TYPES; i++) { |
| 1553 | if (prog->_LinkedShaders[i] != NULL) |
| 1554 | ctx->Driver.DeleteShader(ctx, prog->_LinkedShaders[i]); |
| 1555 | |
| 1556 | prog->_LinkedShaders[i] = NULL; |
Eric Anholt | 5d0f430 | 2010-08-18 12:02:35 -0700 | [diff] [blame] | 1557 | } |
| 1558 | |
Ian Romanick | cd6764e | 2010-07-16 16:00:07 -0700 | [diff] [blame] | 1559 | /* Link all shaders for a particular stage and validate the result. |
| 1560 | */ |
Ian Romanick | cc22c5a | 2010-06-18 17:13:42 -0700 | [diff] [blame] | 1561 | if (num_vert_shaders > 0) { |
Ian Romanick | 3fb8787 | 2010-07-09 14:09:34 -0700 | [diff] [blame] | 1562 | gl_shader *const sh = |
Kenneth Graunke | 2da02e7 | 2010-11-17 11:03:57 -0800 | [diff] [blame] | 1563 | link_intrastage_shaders(mem_ctx, ctx, prog, vert_shader_list, |
| 1564 | num_vert_shaders); |
Ian Romanick | 3fb8787 | 2010-07-09 14:09:34 -0700 | [diff] [blame] | 1565 | |
| 1566 | if (sh == NULL) |
| 1567 | goto done; |
| 1568 | |
| 1569 | if (!validate_vertex_shader_executable(prog, sh)) |
Ian Romanick | f29ff6e | 2010-10-14 17:55:17 -0700 | [diff] [blame] | 1570 | goto done; |
Ian Romanick | 3fb8787 | 2010-07-09 14:09:34 -0700 | [diff] [blame] | 1571 | |
Ian Romanick | 3322fba | 2010-10-14 13:28:42 -0700 | [diff] [blame] | 1572 | _mesa_reference_shader(ctx, &prog->_LinkedShaders[MESA_SHADER_VERTEX], |
| 1573 | sh); |
Ian Romanick | cc22c5a | 2010-06-18 17:13:42 -0700 | [diff] [blame] | 1574 | } |
| 1575 | |
| 1576 | if (num_frag_shaders > 0) { |
Ian Romanick | 3fb8787 | 2010-07-09 14:09:34 -0700 | [diff] [blame] | 1577 | gl_shader *const sh = |
Kenneth Graunke | 2da02e7 | 2010-11-17 11:03:57 -0800 | [diff] [blame] | 1578 | link_intrastage_shaders(mem_ctx, ctx, prog, frag_shader_list, |
| 1579 | num_frag_shaders); |
Ian Romanick | 3fb8787 | 2010-07-09 14:09:34 -0700 | [diff] [blame] | 1580 | |
| 1581 | if (sh == NULL) |
| 1582 | goto done; |
| 1583 | |
| 1584 | if (!validate_fragment_shader_executable(prog, sh)) |
Ian Romanick | f29ff6e | 2010-10-14 17:55:17 -0700 | [diff] [blame] | 1585 | goto done; |
Ian Romanick | 3fb8787 | 2010-07-09 14:09:34 -0700 | [diff] [blame] | 1586 | |
Ian Romanick | 3322fba | 2010-10-14 13:28:42 -0700 | [diff] [blame] | 1587 | _mesa_reference_shader(ctx, &prog->_LinkedShaders[MESA_SHADER_FRAGMENT], |
| 1588 | sh); |
Ian Romanick | cc22c5a | 2010-06-18 17:13:42 -0700 | [diff] [blame] | 1589 | } |
| 1590 | |
Ian Romanick | 3ed850e | 2010-06-23 12:18:21 -0700 | [diff] [blame] | 1591 | /* Here begins the inter-stage linking phase. Some initial validation is |
| 1592 | * performed, then locations are assigned for uniforms, attributes, and |
| 1593 | * varyings. |
| 1594 | */ |
Ian Romanick | ed1fe3d | 2010-06-23 12:09:14 -0700 | [diff] [blame] | 1595 | if (cross_validate_uniforms(prog)) { |
Ian Romanick | 3322fba | 2010-10-14 13:28:42 -0700 | [diff] [blame] | 1596 | unsigned prev; |
| 1597 | |
| 1598 | for (prev = 0; prev < MESA_SHADER_TYPES; prev++) { |
| 1599 | if (prog->_LinkedShaders[prev] != NULL) |
| 1600 | break; |
| 1601 | } |
| 1602 | |
Ian Romanick | 3710192 | 2010-06-18 19:02:10 -0700 | [diff] [blame] | 1603 | /* Validate the inputs of each stage with the output of the preceeding |
| 1604 | * stage. |
| 1605 | */ |
Ian Romanick | 3322fba | 2010-10-14 13:28:42 -0700 | [diff] [blame] | 1606 | for (unsigned i = prev + 1; i < MESA_SHADER_TYPES; i++) { |
| 1607 | if (prog->_LinkedShaders[i] == NULL) |
| 1608 | continue; |
| 1609 | |
Ian Romanick | f36460e | 2010-06-23 12:07:22 -0700 | [diff] [blame] | 1610 | if (!cross_validate_outputs_to_inputs(prog, |
Ian Romanick | 3322fba | 2010-10-14 13:28:42 -0700 | [diff] [blame] | 1611 | prog->_LinkedShaders[prev], |
Ian Romanick | abee16e | 2010-06-21 16:16:05 -0700 | [diff] [blame] | 1612 | prog->_LinkedShaders[i])) |
Ian Romanick | 3710192 | 2010-06-18 19:02:10 -0700 | [diff] [blame] | 1613 | goto done; |
Ian Romanick | 3322fba | 2010-10-14 13:28:42 -0700 | [diff] [blame] | 1614 | |
| 1615 | prev = i; |
Ian Romanick | 3710192 | 2010-06-18 19:02:10 -0700 | [diff] [blame] | 1616 | } |
| 1617 | |
Ian Romanick | cc22c5a | 2010-06-18 17:13:42 -0700 | [diff] [blame] | 1618 | prog->LinkStatus = true; |
Ian Romanick | 3710192 | 2010-06-18 19:02:10 -0700 | [diff] [blame] | 1619 | } |
Ian Romanick | 832dfa5 | 2010-06-17 15:04:20 -0700 | [diff] [blame] | 1620 | |
Eric Anholt | 2f4fe15 | 2010-08-10 13:06:49 -0700 | [diff] [blame] | 1621 | /* Do common optimization before assigning storage for attributes, |
| 1622 | * uniforms, and varyings. Later optimization could possibly make |
| 1623 | * some of that unused. |
| 1624 | */ |
Ian Romanick | 3322fba | 2010-10-14 13:28:42 -0700 | [diff] [blame] | 1625 | for (unsigned i = 0; i < MESA_SHADER_TYPES; i++) { |
| 1626 | if (prog->_LinkedShaders[i] == NULL) |
| 1627 | continue; |
| 1628 | |
Luca Barbieri | e591c46 | 2010-09-05 22:29:58 +0200 | [diff] [blame] | 1629 | while (do_common_optimization(prog->_LinkedShaders[i]->ir, true, 32)) |
Eric Anholt | 2f4fe15 | 2010-08-10 13:06:49 -0700 | [diff] [blame] | 1630 | ; |
Ian Romanick | a7ba9a7 | 2010-07-20 13:36:32 -0700 | [diff] [blame] | 1631 | } |
Ian Romanick | 13e10e4 | 2010-06-21 12:03:24 -0700 | [diff] [blame] | 1632 | |
Eric Anholt | 586b4b5 | 2010-09-28 14:32:16 -0700 | [diff] [blame] | 1633 | update_array_sizes(prog); |
| 1634 | |
Ian Romanick | abee16e | 2010-06-21 16:16:05 -0700 | [diff] [blame] | 1635 | assign_uniform_locations(prog); |
Ian Romanick | 13e10e4 | 2010-06-21 12:03:24 -0700 | [diff] [blame] | 1636 | |
Ian Romanick | 3322fba | 2010-10-14 13:28:42 -0700 | [diff] [blame] | 1637 | if (prog->_LinkedShaders[MESA_SHADER_VERTEX] != NULL) { |
Ian Romanick | 9342d26 | 2010-06-22 17:41:37 -0700 | [diff] [blame] | 1638 | /* FINISHME: The value of the max_attribute_index parameter is |
| 1639 | * FINISHME: implementation dependent based on the value of |
| 1640 | * FINISHME: GL_MAX_VERTEX_ATTRIBS. GL_MAX_VERTEX_ATTRIBS must be |
| 1641 | * FINISHME: at least 16, so hardcode 16 for now. |
| 1642 | */ |
Ian Romanick | 4b5489d | 2010-10-07 17:20:15 -0700 | [diff] [blame] | 1643 | if (!assign_attribute_locations(prog, 16)) { |
| 1644 | prog->LinkStatus = false; |
Ian Romanick | 6984670 | 2010-06-22 17:29:19 -0700 | [diff] [blame] | 1645 | goto done; |
Ian Romanick | 4b5489d | 2010-10-07 17:20:15 -0700 | [diff] [blame] | 1646 | } |
Ian Romanick | 40e114b | 2010-08-17 14:55:50 -0700 | [diff] [blame] | 1647 | } |
| 1648 | |
Ian Romanick | 3322fba | 2010-10-14 13:28:42 -0700 | [diff] [blame] | 1649 | unsigned prev; |
| 1650 | for (prev = 0; prev < MESA_SHADER_TYPES; prev++) { |
| 1651 | if (prog->_LinkedShaders[prev] != NULL) |
| 1652 | break; |
| 1653 | } |
| 1654 | |
| 1655 | for (unsigned i = prev + 1; i < MESA_SHADER_TYPES; i++) { |
| 1656 | if (prog->_LinkedShaders[i] == NULL) |
| 1657 | continue; |
| 1658 | |
Eric Anholt | b706283 | 2010-07-28 13:52:23 -0700 | [diff] [blame] | 1659 | assign_varying_locations(prog, |
Ian Romanick | 3322fba | 2010-10-14 13:28:42 -0700 | [diff] [blame] | 1660 | prog->_LinkedShaders[prev], |
Ian Romanick | 0e59b26 | 2010-06-23 11:23:01 -0700 | [diff] [blame] | 1661 | prog->_LinkedShaders[i]); |
Ian Romanick | 3322fba | 2010-10-14 13:28:42 -0700 | [diff] [blame] | 1662 | prev = i; |
| 1663 | } |
Ian Romanick | 13e10e4 | 2010-06-21 12:03:24 -0700 | [diff] [blame] | 1664 | |
Ian Romanick | cc90e62 | 2010-10-19 17:59:10 -0700 | [diff] [blame] | 1665 | if (prog->_LinkedShaders[MESA_SHADER_VERTEX] != NULL) { |
| 1666 | demote_shader_inputs_and_outputs(prog->_LinkedShaders[MESA_SHADER_VERTEX], |
| 1667 | ir_var_out); |
| 1668 | } |
| 1669 | |
| 1670 | if (prog->_LinkedShaders[MESA_SHADER_GEOMETRY] != NULL) { |
| 1671 | gl_shader *const sh = prog->_LinkedShaders[MESA_SHADER_GEOMETRY]; |
| 1672 | |
| 1673 | demote_shader_inputs_and_outputs(sh, ir_var_in); |
| 1674 | demote_shader_inputs_and_outputs(sh, ir_var_inout); |
| 1675 | demote_shader_inputs_and_outputs(sh, ir_var_out); |
| 1676 | } |
| 1677 | |
| 1678 | if (prog->_LinkedShaders[MESA_SHADER_FRAGMENT] != NULL) { |
| 1679 | gl_shader *const sh = prog->_LinkedShaders[MESA_SHADER_FRAGMENT]; |
| 1680 | |
| 1681 | demote_shader_inputs_and_outputs(sh, ir_var_in); |
| 1682 | } |
| 1683 | |
Ian Romanick | 13e10e4 | 2010-06-21 12:03:24 -0700 | [diff] [blame] | 1684 | /* FINISHME: Assign fragment shader output locations. */ |
| 1685 | |
Ian Romanick | 832dfa5 | 2010-06-17 15:04:20 -0700 | [diff] [blame] | 1686 | done: |
| 1687 | free(vert_shader_list); |
Kenneth Graunke | 2da02e7 | 2010-11-17 11:03:57 -0800 | [diff] [blame] | 1688 | |
| 1689 | for (unsigned i = 0; i < MESA_SHADER_TYPES; i++) { |
| 1690 | if (prog->_LinkedShaders[i] == NULL) |
| 1691 | continue; |
| 1692 | |
| 1693 | /* Retain any live IR, but trash the rest. */ |
| 1694 | reparent_ir(prog->_LinkedShaders[i]->ir, prog->_LinkedShaders[i]->ir); |
| 1695 | } |
| 1696 | |
Kenneth Graunke | d3073f5 | 2011-01-21 14:32:31 -0800 | [diff] [blame^] | 1697 | ralloc_free(mem_ctx); |
Ian Romanick | 832dfa5 | 2010-06-17 15:04:20 -0700 | [diff] [blame] | 1698 | } |