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> |
| 69 | |
| 70 | extern "C" { |
| 71 | #include <talloc.h> |
| 72 | } |
Ian Romanick | 832dfa5 | 2010-06-17 15:04:20 -0700 | [diff] [blame] | 73 | |
Ian Romanick | 0ad22cd | 2010-06-21 17:18:31 -0700 | [diff] [blame] | 74 | #include "main/mtypes.h" |
Ian Romanick | 832dfa5 | 2010-06-17 15:04:20 -0700 | [diff] [blame] | 75 | #include "glsl_symbol_table.h" |
| 76 | #include "glsl_parser_extras.h" |
| 77 | #include "ir.h" |
Ian Romanick | 019a59b | 2010-06-21 16:10:42 -0700 | [diff] [blame] | 78 | #include "ir_optimization.h" |
Ian Romanick | 832dfa5 | 2010-06-17 15:04:20 -0700 | [diff] [blame] | 79 | #include "program.h" |
Eric Anholt | 3d60123 | 2010-06-24 17:08:53 -0700 | [diff] [blame] | 80 | extern "C" { |
Ian Romanick | 019a59b | 2010-06-21 16:10:42 -0700 | [diff] [blame] | 81 | #include "hash_table.h" |
Eric Anholt | 3d60123 | 2010-06-24 17:08:53 -0700 | [diff] [blame] | 82 | } |
Ian Romanick | 832dfa5 | 2010-06-17 15:04:20 -0700 | [diff] [blame] | 83 | |
| 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 | |
| 107 | bool variable_found() |
| 108 | { |
| 109 | return found; |
| 110 | } |
| 111 | |
| 112 | private: |
| 113 | const char *name; /**< Find writes to a variable with this name. */ |
| 114 | bool found; /**< Was a write to the variable found? */ |
| 115 | }; |
| 116 | |
Ian Romanick | c93b8f1 | 2010-06-17 15:20:22 -0700 | [diff] [blame] | 117 | |
Ian Romanick | 0ad22cd | 2010-06-21 17:18:31 -0700 | [diff] [blame] | 118 | void |
Eric Anholt | 849e181 | 2010-06-30 11:49:17 -0700 | [diff] [blame] | 119 | linker_error_printf(gl_shader_program *prog, const char *fmt, ...) |
Ian Romanick | f36460e | 2010-06-23 12:07:22 -0700 | [diff] [blame] | 120 | { |
| 121 | va_list ap; |
| 122 | |
| 123 | prog->InfoLog = talloc_strdup_append(prog->InfoLog, "error: "); |
| 124 | va_start(ap, fmt); |
| 125 | prog->InfoLog = talloc_vasprintf_append(prog->InfoLog, fmt, ap); |
| 126 | va_end(ap); |
| 127 | } |
| 128 | |
| 129 | |
| 130 | void |
Eric Anholt | 16b68b1 | 2010-06-30 11:05:43 -0700 | [diff] [blame] | 131 | invalidate_variable_locations(gl_shader *sh, enum ir_variable_mode mode, |
Ian Romanick | 0ad22cd | 2010-06-21 17:18:31 -0700 | [diff] [blame] | 132 | int generic_base) |
| 133 | { |
Eric Anholt | 16b68b1 | 2010-06-30 11:05:43 -0700 | [diff] [blame] | 134 | foreach_list(node, sh->ir) { |
Ian Romanick | 0ad22cd | 2010-06-21 17:18:31 -0700 | [diff] [blame] | 135 | ir_variable *const var = ((ir_instruction *) node)->as_variable(); |
| 136 | |
| 137 | if ((var == NULL) || (var->mode != (unsigned) mode)) |
| 138 | continue; |
| 139 | |
| 140 | /* Only assign locations for generic attributes / varyings / etc. |
| 141 | */ |
| 142 | if (var->location >= generic_base) |
| 143 | var->location = -1; |
| 144 | } |
| 145 | } |
| 146 | |
| 147 | |
Ian Romanick | c93b8f1 | 2010-06-17 15:20:22 -0700 | [diff] [blame] | 148 | /** |
Ian Romanick | 6984670 | 2010-06-22 17:29:19 -0700 | [diff] [blame] | 149 | * Determine the number of attribute slots required for a particular type |
| 150 | * |
| 151 | * This code is here because it implements the language rules of a specific |
| 152 | * GLSL version. Since it's a property of the language and not a property of |
| 153 | * types in general, it doesn't really belong in glsl_type. |
| 154 | */ |
| 155 | unsigned |
| 156 | count_attribute_slots(const glsl_type *t) |
| 157 | { |
| 158 | /* From page 31 (page 37 of the PDF) of the GLSL 1.50 spec: |
| 159 | * |
| 160 | * "A scalar input counts the same amount against this limit as a vec4, |
| 161 | * so applications may want to consider packing groups of four |
| 162 | * unrelated float inputs together into a vector to better utilize the |
| 163 | * capabilities of the underlying hardware. A matrix input will use up |
| 164 | * multiple locations. The number of locations used will equal the |
| 165 | * number of columns in the matrix." |
| 166 | * |
| 167 | * The spec does not explicitly say how arrays are counted. However, it |
| 168 | * should be safe to assume the total number of slots consumed by an array |
| 169 | * is the number of entries in the array multiplied by the number of slots |
| 170 | * consumed by a single element of the array. |
| 171 | */ |
| 172 | |
| 173 | if (t->is_array()) |
| 174 | return t->array_size() * count_attribute_slots(t->element_type()); |
| 175 | |
| 176 | if (t->is_matrix()) |
| 177 | return t->matrix_columns; |
| 178 | |
| 179 | return 1; |
| 180 | } |
| 181 | |
| 182 | |
| 183 | /** |
Ian Romanick | c93b8f1 | 2010-06-17 15:20:22 -0700 | [diff] [blame] | 184 | * Verify that a vertex shader executable meets all semantic requirements |
| 185 | * |
| 186 | * \param shader Vertex shader executable to be verified |
| 187 | */ |
Ian Romanick | 832dfa5 | 2010-06-17 15:04:20 -0700 | [diff] [blame] | 188 | bool |
Eric Anholt | 849e181 | 2010-06-30 11:49:17 -0700 | [diff] [blame] | 189 | validate_vertex_shader_executable(struct gl_shader_program *prog, |
Eric Anholt | 16b68b1 | 2010-06-30 11:05:43 -0700 | [diff] [blame] | 190 | struct gl_shader *shader) |
Ian Romanick | 832dfa5 | 2010-06-17 15:04:20 -0700 | [diff] [blame] | 191 | { |
| 192 | if (shader == NULL) |
| 193 | return true; |
| 194 | |
| 195 | if (!shader->symbols->get_function("main")) { |
Ian Romanick | f36460e | 2010-06-23 12:07:22 -0700 | [diff] [blame] | 196 | linker_error_printf(prog, "vertex shader lacks `main'\n"); |
Ian Romanick | 832dfa5 | 2010-06-17 15:04:20 -0700 | [diff] [blame] | 197 | return false; |
| 198 | } |
| 199 | |
| 200 | find_assignment_visitor find("gl_Position"); |
Eric Anholt | 16b68b1 | 2010-06-30 11:05:43 -0700 | [diff] [blame] | 201 | find.run(shader->ir); |
Ian Romanick | 832dfa5 | 2010-06-17 15:04:20 -0700 | [diff] [blame] | 202 | if (!find.variable_found()) { |
Ian Romanick | f36460e | 2010-06-23 12:07:22 -0700 | [diff] [blame] | 203 | linker_error_printf(prog, |
| 204 | "vertex shader does not write to `gl_Position'\n"); |
Ian Romanick | 832dfa5 | 2010-06-17 15:04:20 -0700 | [diff] [blame] | 205 | return false; |
| 206 | } |
| 207 | |
| 208 | return true; |
| 209 | } |
| 210 | |
| 211 | |
Ian Romanick | c93b8f1 | 2010-06-17 15:20:22 -0700 | [diff] [blame] | 212 | /** |
| 213 | * Verify that a fragment shader executable meets all semantic requirements |
| 214 | * |
| 215 | * \param shader Fragment shader executable to be verified |
| 216 | */ |
Ian Romanick | 832dfa5 | 2010-06-17 15:04:20 -0700 | [diff] [blame] | 217 | bool |
Eric Anholt | 849e181 | 2010-06-30 11:49:17 -0700 | [diff] [blame] | 218 | validate_fragment_shader_executable(struct gl_shader_program *prog, |
Eric Anholt | 16b68b1 | 2010-06-30 11:05:43 -0700 | [diff] [blame] | 219 | struct gl_shader *shader) |
Ian Romanick | 832dfa5 | 2010-06-17 15:04:20 -0700 | [diff] [blame] | 220 | { |
| 221 | if (shader == NULL) |
| 222 | return true; |
| 223 | |
| 224 | if (!shader->symbols->get_function("main")) { |
Ian Romanick | f36460e | 2010-06-23 12:07:22 -0700 | [diff] [blame] | 225 | linker_error_printf(prog, "fragment shader lacks `main'\n"); |
Ian Romanick | 832dfa5 | 2010-06-17 15:04:20 -0700 | [diff] [blame] | 226 | return false; |
| 227 | } |
| 228 | |
| 229 | find_assignment_visitor frag_color("gl_FragColor"); |
| 230 | find_assignment_visitor frag_data("gl_FragData"); |
| 231 | |
Eric Anholt | 16b68b1 | 2010-06-30 11:05:43 -0700 | [diff] [blame] | 232 | frag_color.run(shader->ir); |
| 233 | frag_data.run(shader->ir); |
Ian Romanick | 832dfa5 | 2010-06-17 15:04:20 -0700 | [diff] [blame] | 234 | |
Ian Romanick | 832dfa5 | 2010-06-17 15:04:20 -0700 | [diff] [blame] | 235 | if (frag_color.variable_found() && frag_data.variable_found()) { |
Ian Romanick | f36460e | 2010-06-23 12:07:22 -0700 | [diff] [blame] | 236 | linker_error_printf(prog, "fragment shader writes to both " |
| 237 | "`gl_FragColor' and `gl_FragData'\n"); |
Ian Romanick | 832dfa5 | 2010-06-17 15:04:20 -0700 | [diff] [blame] | 238 | return false; |
| 239 | } |
| 240 | |
| 241 | return true; |
| 242 | } |
| 243 | |
| 244 | |
Ian Romanick | cc22c5a | 2010-06-18 17:13:42 -0700 | [diff] [blame] | 245 | /** |
| 246 | * Perform validation of uniforms used across multiple shader stages |
| 247 | */ |
| 248 | bool |
Eric Anholt | 849e181 | 2010-06-30 11:49:17 -0700 | [diff] [blame] | 249 | cross_validate_uniforms(struct gl_shader_program *prog) |
Ian Romanick | cc22c5a | 2010-06-18 17:13:42 -0700 | [diff] [blame] | 250 | { |
| 251 | /* Examine all of the uniforms in all of the shaders and cross validate |
| 252 | * them. |
| 253 | */ |
| 254 | glsl_symbol_table uniforms; |
Ian Romanick | ed1fe3d | 2010-06-23 12:09:14 -0700 | [diff] [blame] | 255 | for (unsigned i = 0; i < prog->_NumLinkedShaders; i++) { |
Eric Anholt | 16b68b1 | 2010-06-30 11:05:43 -0700 | [diff] [blame] | 256 | foreach_list(node, prog->_LinkedShaders[i]->ir) { |
Ian Romanick | cc22c5a | 2010-06-18 17:13:42 -0700 | [diff] [blame] | 257 | ir_variable *const var = ((ir_instruction *) node)->as_variable(); |
| 258 | |
| 259 | if ((var == NULL) || (var->mode != ir_var_uniform)) |
| 260 | continue; |
| 261 | |
| 262 | /* If a uniform with this name has already been seen, verify that the |
| 263 | * new instance has the same type. In addition, if the uniforms have |
| 264 | * initializers, the values of the initializers must be the same. |
| 265 | */ |
| 266 | ir_variable *const existing = uniforms.get_variable(var->name); |
| 267 | if (existing != NULL) { |
| 268 | if (var->type != existing->type) { |
Ian Romanick | f36460e | 2010-06-23 12:07:22 -0700 | [diff] [blame] | 269 | linker_error_printf(prog, "uniform `%s' declared as type " |
| 270 | "`%s' and type `%s'\n", |
| 271 | var->name, var->type->name, |
| 272 | existing->type->name); |
Ian Romanick | cc22c5a | 2010-06-18 17:13:42 -0700 | [diff] [blame] | 273 | return false; |
| 274 | } |
| 275 | |
| 276 | if (var->constant_value != NULL) { |
| 277 | if (existing->constant_value != NULL) { |
| 278 | if (!var->constant_value->has_value(existing->constant_value)) { |
Ian Romanick | f36460e | 2010-06-23 12:07:22 -0700 | [diff] [blame] | 279 | linker_error_printf(prog, "initializers for uniform " |
| 280 | "`%s' have differing values\n", |
| 281 | var->name); |
Ian Romanick | cc22c5a | 2010-06-18 17:13:42 -0700 | [diff] [blame] | 282 | return false; |
| 283 | } |
| 284 | } else |
| 285 | /* If the first-seen instance of a particular uniform did not |
| 286 | * have an initializer but a later instance does, copy the |
| 287 | * initializer to the version stored in the symbol table. |
| 288 | */ |
Eric Anholt | 4b6fd39 | 2010-06-23 11:37:12 -0700 | [diff] [blame] | 289 | existing->constant_value = |
| 290 | (ir_constant *)var->constant_value->clone(NULL); |
Ian Romanick | cc22c5a | 2010-06-18 17:13:42 -0700 | [diff] [blame] | 291 | } |
| 292 | } else |
| 293 | uniforms.add_variable(var->name, var); |
| 294 | } |
| 295 | } |
| 296 | |
| 297 | return true; |
| 298 | } |
| 299 | |
| 300 | |
Ian Romanick | 3710192 | 2010-06-18 19:02:10 -0700 | [diff] [blame] | 301 | /** |
| 302 | * Validate that outputs from one stage match inputs of another |
| 303 | */ |
| 304 | bool |
Eric Anholt | 849e181 | 2010-06-30 11:49:17 -0700 | [diff] [blame] | 305 | cross_validate_outputs_to_inputs(struct gl_shader_program *prog, |
Eric Anholt | 16b68b1 | 2010-06-30 11:05:43 -0700 | [diff] [blame] | 306 | gl_shader *producer, gl_shader *consumer) |
Ian Romanick | 3710192 | 2010-06-18 19:02:10 -0700 | [diff] [blame] | 307 | { |
| 308 | glsl_symbol_table parameters; |
| 309 | /* FINISHME: Figure these out dynamically. */ |
| 310 | const char *const producer_stage = "vertex"; |
| 311 | const char *const consumer_stage = "fragment"; |
| 312 | |
| 313 | /* Find all shader outputs in the "producer" stage. |
| 314 | */ |
Eric Anholt | 16b68b1 | 2010-06-30 11:05:43 -0700 | [diff] [blame] | 315 | foreach_list(node, producer->ir) { |
Ian Romanick | 3710192 | 2010-06-18 19:02:10 -0700 | [diff] [blame] | 316 | ir_variable *const var = ((ir_instruction *) node)->as_variable(); |
| 317 | |
| 318 | /* FINISHME: For geometry shaders, this should also look for inout |
| 319 | * FINISHME: variables. |
| 320 | */ |
| 321 | if ((var == NULL) || (var->mode != ir_var_out)) |
| 322 | continue; |
| 323 | |
| 324 | parameters.add_variable(var->name, var); |
| 325 | } |
| 326 | |
| 327 | |
| 328 | /* Find all shader inputs in the "consumer" stage. Any variables that have |
| 329 | * matching outputs already in the symbol table must have the same type and |
| 330 | * qualifiers. |
| 331 | */ |
Eric Anholt | 16b68b1 | 2010-06-30 11:05:43 -0700 | [diff] [blame] | 332 | foreach_list(node, consumer->ir) { |
Ian Romanick | 3710192 | 2010-06-18 19:02:10 -0700 | [diff] [blame] | 333 | ir_variable *const input = ((ir_instruction *) node)->as_variable(); |
| 334 | |
| 335 | /* FINISHME: For geometry shaders, this should also look for inout |
| 336 | * FINISHME: variables. |
| 337 | */ |
| 338 | if ((input == NULL) || (input->mode != ir_var_in)) |
| 339 | continue; |
| 340 | |
| 341 | ir_variable *const output = parameters.get_variable(input->name); |
| 342 | if (output != NULL) { |
| 343 | /* Check that the types match between stages. |
| 344 | */ |
| 345 | if (input->type != output->type) { |
Ian Romanick | f36460e | 2010-06-23 12:07:22 -0700 | [diff] [blame] | 346 | linker_error_printf(prog, |
| 347 | "%s shader output `%s' delcared as " |
| 348 | "type `%s', but %s shader input declared " |
| 349 | "as type `%s'\n", |
| 350 | producer_stage, output->name, |
| 351 | output->type->name, |
| 352 | consumer_stage, input->type->name); |
Ian Romanick | 3710192 | 2010-06-18 19:02:10 -0700 | [diff] [blame] | 353 | return false; |
| 354 | } |
| 355 | |
| 356 | /* Check that all of the qualifiers match between stages. |
| 357 | */ |
| 358 | if (input->centroid != output->centroid) { |
Ian Romanick | f36460e | 2010-06-23 12:07:22 -0700 | [diff] [blame] | 359 | linker_error_printf(prog, |
| 360 | "%s shader output `%s' %s centroid qualifier, " |
| 361 | "but %s shader input %s centroid qualifier\n", |
| 362 | producer_stage, |
| 363 | output->name, |
| 364 | (output->centroid) ? "has" : "lacks", |
| 365 | consumer_stage, |
| 366 | (input->centroid) ? "has" : "lacks"); |
Ian Romanick | 3710192 | 2010-06-18 19:02:10 -0700 | [diff] [blame] | 367 | return false; |
| 368 | } |
| 369 | |
| 370 | if (input->invariant != output->invariant) { |
Ian Romanick | f36460e | 2010-06-23 12:07:22 -0700 | [diff] [blame] | 371 | linker_error_printf(prog, |
| 372 | "%s shader output `%s' %s invariant qualifier, " |
| 373 | "but %s shader input %s invariant qualifier\n", |
| 374 | producer_stage, |
| 375 | output->name, |
| 376 | (output->invariant) ? "has" : "lacks", |
| 377 | consumer_stage, |
| 378 | (input->invariant) ? "has" : "lacks"); |
Ian Romanick | 3710192 | 2010-06-18 19:02:10 -0700 | [diff] [blame] | 379 | return false; |
| 380 | } |
| 381 | |
| 382 | if (input->interpolation != output->interpolation) { |
Ian Romanick | f36460e | 2010-06-23 12:07:22 -0700 | [diff] [blame] | 383 | linker_error_printf(prog, |
| 384 | "%s shader output `%s' specifies %s " |
| 385 | "interpolation qualifier, " |
| 386 | "but %s shader input specifies %s " |
| 387 | "interpolation qualifier\n", |
| 388 | producer_stage, |
| 389 | output->name, |
| 390 | output->interpolation_string(), |
| 391 | consumer_stage, |
| 392 | input->interpolation_string()); |
Ian Romanick | 3710192 | 2010-06-18 19:02:10 -0700 | [diff] [blame] | 393 | return false; |
| 394 | } |
| 395 | } |
| 396 | } |
| 397 | |
| 398 | return true; |
| 399 | } |
| 400 | |
| 401 | |
Ian Romanick | 019a59b | 2010-06-21 16:10:42 -0700 | [diff] [blame] | 402 | struct uniform_node { |
| 403 | exec_node link; |
| 404 | struct gl_uniform *u; |
| 405 | unsigned slots; |
| 406 | }; |
| 407 | |
Ian Romanick | abee16e | 2010-06-21 16:16:05 -0700 | [diff] [blame] | 408 | void |
Eric Anholt | 849e181 | 2010-06-30 11:49:17 -0700 | [diff] [blame] | 409 | assign_uniform_locations(struct gl_shader_program *prog) |
Ian Romanick | 019a59b | 2010-06-21 16:10:42 -0700 | [diff] [blame] | 410 | { |
| 411 | /* */ |
| 412 | exec_list uniforms; |
| 413 | unsigned total_uniforms = 0; |
| 414 | hash_table *ht = hash_table_ctor(32, hash_table_string_hash, |
| 415 | hash_table_string_compare); |
| 416 | |
Ian Romanick | abee16e | 2010-06-21 16:16:05 -0700 | [diff] [blame] | 417 | for (unsigned i = 0; i < prog->_NumLinkedShaders; i++) { |
Ian Romanick | 019a59b | 2010-06-21 16:10:42 -0700 | [diff] [blame] | 418 | unsigned next_position = 0; |
| 419 | |
Eric Anholt | 16b68b1 | 2010-06-30 11:05:43 -0700 | [diff] [blame] | 420 | foreach_list(node, prog->_LinkedShaders[i]->ir) { |
Ian Romanick | 019a59b | 2010-06-21 16:10:42 -0700 | [diff] [blame] | 421 | ir_variable *const var = ((ir_instruction *) node)->as_variable(); |
| 422 | |
| 423 | if ((var == NULL) || (var->mode != ir_var_uniform)) |
| 424 | continue; |
| 425 | |
| 426 | const unsigned vec4_slots = (var->component_slots() + 3) / 4; |
| 427 | assert(vec4_slots != 0); |
| 428 | |
| 429 | uniform_node *n = (uniform_node *) hash_table_find(ht, var->name); |
| 430 | if (n == NULL) { |
| 431 | n = (uniform_node *) calloc(1, sizeof(struct uniform_node)); |
| 432 | n->u = (gl_uniform *) calloc(vec4_slots, sizeof(struct gl_uniform)); |
| 433 | n->slots = vec4_slots; |
| 434 | |
| 435 | n->u[0].Name = strdup(var->name); |
| 436 | for (unsigned j = 1; j < vec4_slots; j++) |
| 437 | n->u[j].Name = n->u[0].Name; |
| 438 | |
| 439 | hash_table_insert(ht, n, n->u[0].Name); |
| 440 | uniforms.push_tail(& n->link); |
| 441 | total_uniforms += vec4_slots; |
| 442 | } |
| 443 | |
| 444 | if (var->constant_value != NULL) |
| 445 | for (unsigned j = 0; j < vec4_slots; j++) |
| 446 | n->u[j].Initialized = true; |
| 447 | |
| 448 | var->location = next_position; |
| 449 | |
| 450 | for (unsigned j = 0; j < vec4_slots; j++) { |
Ian Romanick | abee16e | 2010-06-21 16:16:05 -0700 | [diff] [blame] | 451 | switch (prog->_LinkedShaders[i]->Type) { |
Ian Romanick | 019a59b | 2010-06-21 16:10:42 -0700 | [diff] [blame] | 452 | case GL_VERTEX_SHADER: |
| 453 | n->u[j].VertPos = next_position; |
| 454 | break; |
| 455 | case GL_FRAGMENT_SHADER: |
| 456 | n->u[j].FragPos = next_position; |
| 457 | break; |
| 458 | case GL_GEOMETRY_SHADER: |
| 459 | /* FINISHME: Support geometry shaders. */ |
Ian Romanick | abee16e | 2010-06-21 16:16:05 -0700 | [diff] [blame] | 460 | assert(prog->_LinkedShaders[i]->Type != GL_GEOMETRY_SHADER); |
Ian Romanick | 019a59b | 2010-06-21 16:10:42 -0700 | [diff] [blame] | 461 | break; |
| 462 | } |
| 463 | |
| 464 | next_position++; |
| 465 | } |
| 466 | } |
| 467 | } |
| 468 | |
| 469 | gl_uniform_list *ul = (gl_uniform_list *) |
| 470 | calloc(1, sizeof(gl_uniform_list)); |
| 471 | |
| 472 | ul->Size = total_uniforms; |
| 473 | ul->NumUniforms = total_uniforms; |
| 474 | ul->Uniforms = (gl_uniform *) calloc(total_uniforms, sizeof(gl_uniform)); |
| 475 | |
| 476 | unsigned idx = 0; |
| 477 | uniform_node *next; |
| 478 | for (uniform_node *node = (uniform_node *) uniforms.head |
| 479 | ; node->link.next != NULL |
| 480 | ; node = next) { |
| 481 | next = (uniform_node *) node->link.next; |
| 482 | |
| 483 | node->link.remove(); |
| 484 | memcpy(&ul->Uniforms[idx], node->u, sizeof(gl_uniform) * node->slots); |
| 485 | idx += node->slots; |
| 486 | |
| 487 | free(node->u); |
| 488 | free(node); |
| 489 | } |
| 490 | |
| 491 | hash_table_dtor(ht); |
| 492 | |
Ian Romanick | abee16e | 2010-06-21 16:16:05 -0700 | [diff] [blame] | 493 | prog->Uniforms = ul; |
Ian Romanick | 019a59b | 2010-06-21 16:10:42 -0700 | [diff] [blame] | 494 | } |
| 495 | |
| 496 | |
Ian Romanick | 6984670 | 2010-06-22 17:29:19 -0700 | [diff] [blame] | 497 | /** |
| 498 | * Find a contiguous set of available bits in a bitmask |
| 499 | * |
| 500 | * \param used_mask Bits representing used (1) and unused (0) locations |
| 501 | * \param needed_count Number of contiguous bits needed. |
| 502 | * |
| 503 | * \return |
| 504 | * Base location of the available bits on success or -1 on failure. |
| 505 | */ |
| 506 | int |
| 507 | find_available_slots(unsigned used_mask, unsigned needed_count) |
| 508 | { |
| 509 | unsigned needed_mask = (1 << needed_count) - 1; |
| 510 | const int max_bit_to_test = (8 * sizeof(used_mask)) - needed_count; |
| 511 | |
| 512 | /* The comparison to 32 is redundant, but without it GCC emits "warning: |
| 513 | * cannot optimize possibly infinite loops" for the loop below. |
| 514 | */ |
| 515 | if ((needed_count == 0) || (max_bit_to_test < 0) || (max_bit_to_test > 32)) |
| 516 | return -1; |
| 517 | |
| 518 | for (int i = 0; i <= max_bit_to_test; i++) { |
| 519 | if ((needed_mask & ~used_mask) == needed_mask) |
| 520 | return i; |
| 521 | |
| 522 | needed_mask <<= 1; |
| 523 | } |
| 524 | |
| 525 | return -1; |
| 526 | } |
| 527 | |
| 528 | |
| 529 | bool |
Eric Anholt | 849e181 | 2010-06-30 11:49:17 -0700 | [diff] [blame] | 530 | assign_attribute_locations(gl_shader_program *prog, unsigned max_attribute_index) |
Ian Romanick | 0ad22cd | 2010-06-21 17:18:31 -0700 | [diff] [blame] | 531 | { |
Ian Romanick | 9342d26 | 2010-06-22 17:41:37 -0700 | [diff] [blame] | 532 | /* Mark invalid attribute locations as being used. |
| 533 | */ |
| 534 | unsigned used_locations = (max_attribute_index >= 32) |
| 535 | ? ~0 : ~((1 << max_attribute_index) - 1); |
Ian Romanick | 0ad22cd | 2010-06-21 17:18:31 -0700 | [diff] [blame] | 536 | |
Eric Anholt | 16b68b1 | 2010-06-30 11:05:43 -0700 | [diff] [blame] | 537 | gl_shader *const sh = prog->_LinkedShaders[0]; |
Ian Romanick | 0ad22cd | 2010-06-21 17:18:31 -0700 | [diff] [blame] | 538 | assert(sh->Type == GL_VERTEX_SHADER); |
| 539 | |
Ian Romanick | 6984670 | 2010-06-22 17:29:19 -0700 | [diff] [blame] | 540 | /* Operate in a total of four passes. |
Ian Romanick | 0ad22cd | 2010-06-21 17:18:31 -0700 | [diff] [blame] | 541 | * |
| 542 | * 1. Invalidate the location assignments for all vertex shader inputs. |
| 543 | * |
| 544 | * 2. Assign locations for inputs that have user-defined (via |
| 545 | * glBindVertexAttribLocation) locatoins. |
| 546 | * |
Ian Romanick | 6984670 | 2010-06-22 17:29:19 -0700 | [diff] [blame] | 547 | * 3. Sort the attributes without assigned locations by number of slots |
| 548 | * required in decreasing order. Fragmentation caused by attribute |
| 549 | * locations assigned by the application may prevent large attributes |
| 550 | * from having enough contiguous space. |
| 551 | * |
| 552 | * 4. Assign locations to any inputs without assigned locations. |
Ian Romanick | 0ad22cd | 2010-06-21 17:18:31 -0700 | [diff] [blame] | 553 | */ |
| 554 | |
| 555 | invalidate_variable_locations(sh, ir_var_in, VERT_ATTRIB_GENERIC0); |
| 556 | |
Ian Romanick | 553dcdc | 2010-06-23 12:14:02 -0700 | [diff] [blame] | 557 | if (prog->Attributes != NULL) { |
| 558 | for (unsigned i = 0; i < prog->Attributes->NumParameters; i++) { |
Ian Romanick | 0ad22cd | 2010-06-21 17:18:31 -0700 | [diff] [blame] | 559 | ir_variable *const var = |
Ian Romanick | 553dcdc | 2010-06-23 12:14:02 -0700 | [diff] [blame] | 560 | sh->symbols->get_variable(prog->Attributes->Parameters[i].Name); |
Ian Romanick | 0ad22cd | 2010-06-21 17:18:31 -0700 | [diff] [blame] | 561 | |
Ian Romanick | 6984670 | 2010-06-22 17:29:19 -0700 | [diff] [blame] | 562 | /* Note: attributes that occupy multiple slots, such as arrays or |
| 563 | * matrices, may appear in the attrib array multiple times. |
| 564 | */ |
| 565 | if ((var == NULL) || (var->location != -1)) |
Ian Romanick | 0ad22cd | 2010-06-21 17:18:31 -0700 | [diff] [blame] | 566 | continue; |
| 567 | |
Ian Romanick | 6984670 | 2010-06-22 17:29:19 -0700 | [diff] [blame] | 568 | /* From page 61 of the OpenGL 4.0 spec: |
| 569 | * |
| 570 | * "LinkProgram will fail if the attribute bindings assigned by |
| 571 | * BindAttribLocation do not leave not enough space to assign a |
| 572 | * location for an active matrix attribute or an active attribute |
| 573 | * array, both of which require multiple contiguous generic |
| 574 | * attributes." |
| 575 | * |
| 576 | * Previous versions of the spec contain similar language but omit the |
| 577 | * bit about attribute arrays. |
| 578 | * |
| 579 | * Page 61 of the OpenGL 4.0 spec also says: |
| 580 | * |
| 581 | * "It is possible for an application to bind more than one |
| 582 | * attribute name to the same location. This is referred to as |
| 583 | * aliasing. This will only work if only one of the aliased |
| 584 | * attributes is active in the executable program, or if no path |
| 585 | * through the shader consumes more than one attribute of a set |
| 586 | * of attributes aliased to the same location. A link error can |
| 587 | * occur if the linker determines that every path through the |
| 588 | * shader consumes multiple aliased attributes, but |
| 589 | * implementations are not required to generate an error in this |
| 590 | * case." |
| 591 | * |
| 592 | * These two paragraphs are either somewhat contradictory, or I don't |
| 593 | * fully understand one or both of them. |
| 594 | */ |
| 595 | /* FINISHME: The code as currently written does not support attribute |
| 596 | * FINISHME: location aliasing (see comment above). |
| 597 | */ |
Ian Romanick | 553dcdc | 2010-06-23 12:14:02 -0700 | [diff] [blame] | 598 | const int attr = prog->Attributes->Parameters[i].StateIndexes[0]; |
Ian Romanick | 6984670 | 2010-06-22 17:29:19 -0700 | [diff] [blame] | 599 | const unsigned slots = count_attribute_slots(var->type); |
| 600 | |
| 601 | /* Mask representing the contiguous slots that will be used by this |
| 602 | * attribute. |
| 603 | */ |
| 604 | const unsigned use_mask = (1 << slots) - 1; |
| 605 | |
| 606 | /* Generate a link error if the set of bits requested for this |
| 607 | * attribute overlaps any previously allocated bits. |
| 608 | */ |
| 609 | if ((~(use_mask << attr) & used_locations) != used_locations) { |
Ian Romanick | 553dcdc | 2010-06-23 12:14:02 -0700 | [diff] [blame] | 610 | linker_error_printf(prog, |
| 611 | "insufficient contiguous attribute locations " |
| 612 | "available for vertex shader input `%s'", |
| 613 | var->name); |
Ian Romanick | 6984670 | 2010-06-22 17:29:19 -0700 | [diff] [blame] | 614 | return false; |
| 615 | } |
Ian Romanick | 0ad22cd | 2010-06-21 17:18:31 -0700 | [diff] [blame] | 616 | |
| 617 | var->location = VERT_ATTRIB_GENERIC0 + attr; |
Ian Romanick | 6984670 | 2010-06-22 17:29:19 -0700 | [diff] [blame] | 618 | used_locations |= (use_mask << attr); |
Ian Romanick | 0ad22cd | 2010-06-21 17:18:31 -0700 | [diff] [blame] | 619 | } |
| 620 | } |
| 621 | |
Ian Romanick | 6984670 | 2010-06-22 17:29:19 -0700 | [diff] [blame] | 622 | /* Temporary storage for the set of attributes that need locations assigned. |
| 623 | */ |
| 624 | struct temp_attr { |
| 625 | unsigned slots; |
| 626 | ir_variable *var; |
| 627 | |
| 628 | /* Used below in the call to qsort. */ |
| 629 | static int compare(const void *a, const void *b) |
| 630 | { |
| 631 | const temp_attr *const l = (const temp_attr *) a; |
| 632 | const temp_attr *const r = (const temp_attr *) b; |
| 633 | |
| 634 | /* Reversed because we want a descending order sort below. */ |
| 635 | return r->slots - l->slots; |
| 636 | } |
| 637 | } to_assign[16]; |
| 638 | |
| 639 | unsigned num_attr = 0; |
| 640 | |
Eric Anholt | 16b68b1 | 2010-06-30 11:05:43 -0700 | [diff] [blame] | 641 | foreach_list(node, sh->ir) { |
Ian Romanick | 0ad22cd | 2010-06-21 17:18:31 -0700 | [diff] [blame] | 642 | ir_variable *const var = ((ir_instruction *) node)->as_variable(); |
| 643 | |
| 644 | if ((var == NULL) || (var->mode != ir_var_in)) |
| 645 | continue; |
| 646 | |
| 647 | /* The location was explicitly assigned, nothing to do here. |
| 648 | */ |
| 649 | if (var->location != -1) |
| 650 | continue; |
| 651 | |
Ian Romanick | 6984670 | 2010-06-22 17:29:19 -0700 | [diff] [blame] | 652 | to_assign[num_attr].slots = count_attribute_slots(var->type); |
| 653 | to_assign[num_attr].var = var; |
| 654 | num_attr++; |
Ian Romanick | 0ad22cd | 2010-06-21 17:18:31 -0700 | [diff] [blame] | 655 | } |
Ian Romanick | 6984670 | 2010-06-22 17:29:19 -0700 | [diff] [blame] | 656 | |
| 657 | /* If all of the attributes were assigned locations by the application (or |
| 658 | * are built-in attributes with fixed locations), return early. This should |
| 659 | * be the common case. |
| 660 | */ |
| 661 | if (num_attr == 0) |
| 662 | return true; |
| 663 | |
| 664 | qsort(to_assign, num_attr, sizeof(to_assign[0]), temp_attr::compare); |
| 665 | |
Ian Romanick | 982e379 | 2010-06-29 18:58:20 -0700 | [diff] [blame] | 666 | /* VERT_ATTRIB_GENERIC0 is a psdueo-alias for VERT_ATTRIB_POS. It can only |
| 667 | * be explicitly assigned by via glBindAttribLocation. Mark it as reserved |
| 668 | * to prevent it from being automatically allocated below. |
| 669 | */ |
| 670 | used_locations |= VERT_BIT_GENERIC0; |
| 671 | |
Ian Romanick | 6984670 | 2010-06-22 17:29:19 -0700 | [diff] [blame] | 672 | for (unsigned i = 0; i < num_attr; i++) { |
| 673 | /* Mask representing the contiguous slots that will be used by this |
| 674 | * attribute. |
| 675 | */ |
| 676 | const unsigned use_mask = (1 << to_assign[i].slots) - 1; |
| 677 | |
| 678 | int location = find_available_slots(used_locations, to_assign[i].slots); |
| 679 | |
| 680 | if (location < 0) { |
Ian Romanick | 553dcdc | 2010-06-23 12:14:02 -0700 | [diff] [blame] | 681 | linker_error_printf(prog, |
| 682 | "insufficient contiguous attribute locations " |
| 683 | "available for vertex shader input `%s'", |
| 684 | to_assign[i].var->name); |
Ian Romanick | 6984670 | 2010-06-22 17:29:19 -0700 | [diff] [blame] | 685 | return false; |
| 686 | } |
| 687 | |
| 688 | to_assign[i].var->location = VERT_ATTRIB_GENERIC0 + location; |
| 689 | used_locations |= (use_mask << location); |
| 690 | } |
| 691 | |
| 692 | return true; |
Ian Romanick | 0ad22cd | 2010-06-21 17:18:31 -0700 | [diff] [blame] | 693 | } |
| 694 | |
| 695 | |
| 696 | void |
Eric Anholt | 16b68b1 | 2010-06-30 11:05:43 -0700 | [diff] [blame] | 697 | assign_varying_locations(gl_shader *producer, gl_shader *consumer) |
Ian Romanick | 0e59b26 | 2010-06-23 11:23:01 -0700 | [diff] [blame] | 698 | { |
| 699 | /* FINISHME: Set dynamically when geometry shader support is added. */ |
| 700 | unsigned output_index = VERT_RESULT_VAR0; |
| 701 | unsigned input_index = FRAG_ATTRIB_VAR0; |
| 702 | |
| 703 | /* Operate in a total of three passes. |
| 704 | * |
| 705 | * 1. Assign locations for any matching inputs and outputs. |
| 706 | * |
| 707 | * 2. Mark output variables in the producer that do not have locations as |
| 708 | * not being outputs. This lets the optimizer eliminate them. |
| 709 | * |
| 710 | * 3. Mark input variables in the consumer that do not have locations as |
| 711 | * not being inputs. This lets the optimizer eliminate them. |
| 712 | */ |
| 713 | |
| 714 | invalidate_variable_locations(producer, ir_var_out, VERT_RESULT_VAR0); |
| 715 | invalidate_variable_locations(consumer, ir_var_in, FRAG_ATTRIB_VAR0); |
| 716 | |
Eric Anholt | 16b68b1 | 2010-06-30 11:05:43 -0700 | [diff] [blame] | 717 | foreach_list(node, producer->ir) { |
Ian Romanick | 0e59b26 | 2010-06-23 11:23:01 -0700 | [diff] [blame] | 718 | ir_variable *const output_var = ((ir_instruction *) node)->as_variable(); |
| 719 | |
| 720 | if ((output_var == NULL) || (output_var->mode != ir_var_out) |
| 721 | || (output_var->location != -1)) |
| 722 | continue; |
| 723 | |
| 724 | ir_variable *const input_var = |
| 725 | consumer->symbols->get_variable(output_var->name); |
| 726 | |
| 727 | if ((input_var == NULL) || (input_var->mode != ir_var_in)) |
| 728 | continue; |
| 729 | |
| 730 | assert(input_var->location == -1); |
| 731 | |
| 732 | /* FINISHME: Location assignment will need some changes when arrays, |
| 733 | * FINISHME: matrices, and structures are allowed as shader inputs / |
| 734 | * FINISHME: outputs. |
| 735 | */ |
| 736 | output_var->location = output_index; |
| 737 | input_var->location = input_index; |
| 738 | |
| 739 | output_index++; |
| 740 | input_index++; |
| 741 | } |
| 742 | |
Eric Anholt | 16b68b1 | 2010-06-30 11:05:43 -0700 | [diff] [blame] | 743 | foreach_list(node, producer->ir) { |
Ian Romanick | 0e59b26 | 2010-06-23 11:23:01 -0700 | [diff] [blame] | 744 | ir_variable *const var = ((ir_instruction *) node)->as_variable(); |
| 745 | |
| 746 | if ((var == NULL) || (var->mode != ir_var_out)) |
| 747 | continue; |
| 748 | |
| 749 | /* An 'out' variable is only really a shader output if its value is read |
| 750 | * by the following stage. |
| 751 | */ |
| 752 | var->shader_out = (var->location != -1); |
| 753 | } |
| 754 | |
Eric Anholt | 16b68b1 | 2010-06-30 11:05:43 -0700 | [diff] [blame] | 755 | foreach_list(node, consumer->ir) { |
Ian Romanick | 0e59b26 | 2010-06-23 11:23:01 -0700 | [diff] [blame] | 756 | ir_variable *const var = ((ir_instruction *) node)->as_variable(); |
| 757 | |
| 758 | if ((var == NULL) || (var->mode != ir_var_in)) |
| 759 | continue; |
| 760 | |
| 761 | /* An 'in' variable is only really a shader input if its value is written |
| 762 | * by the previous stage. |
| 763 | */ |
| 764 | var->shader_in = (var->location != -1); |
| 765 | } |
| 766 | } |
| 767 | |
| 768 | |
| 769 | void |
Eric Anholt | 849e181 | 2010-06-30 11:49:17 -0700 | [diff] [blame] | 770 | link_shaders(struct gl_shader_program *prog) |
Ian Romanick | 832dfa5 | 2010-06-17 15:04:20 -0700 | [diff] [blame] | 771 | { |
| 772 | prog->LinkStatus = false; |
| 773 | prog->Validated = false; |
| 774 | prog->_Used = false; |
| 775 | |
Ian Romanick | f36460e | 2010-06-23 12:07:22 -0700 | [diff] [blame] | 776 | if (prog->InfoLog != NULL) |
| 777 | talloc_free(prog->InfoLog); |
| 778 | |
| 779 | prog->InfoLog = talloc_strdup(NULL, ""); |
| 780 | |
Ian Romanick | 832dfa5 | 2010-06-17 15:04:20 -0700 | [diff] [blame] | 781 | /* Separate the shaders into groups based on their type. |
| 782 | */ |
Eric Anholt | 16b68b1 | 2010-06-30 11:05:43 -0700 | [diff] [blame] | 783 | struct gl_shader **vert_shader_list; |
Ian Romanick | 832dfa5 | 2010-06-17 15:04:20 -0700 | [diff] [blame] | 784 | unsigned num_vert_shaders = 0; |
Eric Anholt | 16b68b1 | 2010-06-30 11:05:43 -0700 | [diff] [blame] | 785 | struct gl_shader **frag_shader_list; |
Ian Romanick | 832dfa5 | 2010-06-17 15:04:20 -0700 | [diff] [blame] | 786 | unsigned num_frag_shaders = 0; |
| 787 | |
Eric Anholt | 16b68b1 | 2010-06-30 11:05:43 -0700 | [diff] [blame] | 788 | vert_shader_list = (struct gl_shader **) |
| 789 | calloc(2 * prog->NumShaders, sizeof(struct gl_shader *)); |
Ian Romanick | 832dfa5 | 2010-06-17 15:04:20 -0700 | [diff] [blame] | 790 | frag_shader_list = &vert_shader_list[prog->NumShaders]; |
| 791 | |
| 792 | for (unsigned i = 0; i < prog->NumShaders; i++) { |
| 793 | switch (prog->Shaders[i]->Type) { |
| 794 | case GL_VERTEX_SHADER: |
| 795 | vert_shader_list[num_vert_shaders] = prog->Shaders[i]; |
| 796 | num_vert_shaders++; |
| 797 | break; |
| 798 | case GL_FRAGMENT_SHADER: |
| 799 | frag_shader_list[num_frag_shaders] = prog->Shaders[i]; |
| 800 | num_frag_shaders++; |
| 801 | break; |
| 802 | case GL_GEOMETRY_SHADER: |
| 803 | /* FINISHME: Support geometry shaders. */ |
| 804 | assert(prog->Shaders[i]->Type != GL_GEOMETRY_SHADER); |
| 805 | break; |
| 806 | } |
| 807 | } |
| 808 | |
| 809 | /* FINISHME: Implement intra-stage linking. */ |
| 810 | assert(num_vert_shaders <= 1); |
| 811 | assert(num_frag_shaders <= 1); |
| 812 | |
| 813 | /* Verify that each of the per-target executables is valid. |
| 814 | */ |
Ian Romanick | f36460e | 2010-06-23 12:07:22 -0700 | [diff] [blame] | 815 | if (!validate_vertex_shader_executable(prog, vert_shader_list[0]) |
| 816 | || !validate_fragment_shader_executable(prog, frag_shader_list[0])) |
Ian Romanick | 832dfa5 | 2010-06-17 15:04:20 -0700 | [diff] [blame] | 817 | goto done; |
| 818 | |
| 819 | |
Ian Romanick | abee16e | 2010-06-21 16:16:05 -0700 | [diff] [blame] | 820 | prog->_NumLinkedShaders = 0; |
Ian Romanick | 832dfa5 | 2010-06-17 15:04:20 -0700 | [diff] [blame] | 821 | |
Ian Romanick | cc22c5a | 2010-06-18 17:13:42 -0700 | [diff] [blame] | 822 | if (num_vert_shaders > 0) { |
Ian Romanick | abee16e | 2010-06-21 16:16:05 -0700 | [diff] [blame] | 823 | prog->_LinkedShaders[prog->_NumLinkedShaders] = vert_shader_list[0]; |
| 824 | prog->_NumLinkedShaders++; |
Ian Romanick | cc22c5a | 2010-06-18 17:13:42 -0700 | [diff] [blame] | 825 | } |
| 826 | |
| 827 | if (num_frag_shaders > 0) { |
Ian Romanick | abee16e | 2010-06-21 16:16:05 -0700 | [diff] [blame] | 828 | prog->_LinkedShaders[prog->_NumLinkedShaders] = frag_shader_list[0]; |
| 829 | prog->_NumLinkedShaders++; |
Ian Romanick | cc22c5a | 2010-06-18 17:13:42 -0700 | [diff] [blame] | 830 | } |
| 831 | |
Ian Romanick | 3ed850e | 2010-06-23 12:18:21 -0700 | [diff] [blame] | 832 | /* Here begins the inter-stage linking phase. Some initial validation is |
| 833 | * performed, then locations are assigned for uniforms, attributes, and |
| 834 | * varyings. |
| 835 | */ |
Ian Romanick | ed1fe3d | 2010-06-23 12:09:14 -0700 | [diff] [blame] | 836 | if (cross_validate_uniforms(prog)) { |
Ian Romanick | 3710192 | 2010-06-18 19:02:10 -0700 | [diff] [blame] | 837 | /* Validate the inputs of each stage with the output of the preceeding |
| 838 | * stage. |
| 839 | */ |
Ian Romanick | abee16e | 2010-06-21 16:16:05 -0700 | [diff] [blame] | 840 | for (unsigned i = 1; i < prog->_NumLinkedShaders; i++) { |
Ian Romanick | f36460e | 2010-06-23 12:07:22 -0700 | [diff] [blame] | 841 | if (!cross_validate_outputs_to_inputs(prog, |
| 842 | prog->_LinkedShaders[i - 1], |
Ian Romanick | abee16e | 2010-06-21 16:16:05 -0700 | [diff] [blame] | 843 | prog->_LinkedShaders[i])) |
Ian Romanick | 3710192 | 2010-06-18 19:02:10 -0700 | [diff] [blame] | 844 | goto done; |
| 845 | } |
| 846 | |
Ian Romanick | cc22c5a | 2010-06-18 17:13:42 -0700 | [diff] [blame] | 847 | prog->LinkStatus = true; |
Ian Romanick | 3710192 | 2010-06-18 19:02:10 -0700 | [diff] [blame] | 848 | } |
Ian Romanick | 832dfa5 | 2010-06-17 15:04:20 -0700 | [diff] [blame] | 849 | |
Ian Romanick | 13e10e4 | 2010-06-21 12:03:24 -0700 | [diff] [blame] | 850 | /* FINISHME: Perform whole-program optimization here. */ |
| 851 | |
Ian Romanick | abee16e | 2010-06-21 16:16:05 -0700 | [diff] [blame] | 852 | assign_uniform_locations(prog); |
Ian Romanick | 13e10e4 | 2010-06-21 12:03:24 -0700 | [diff] [blame] | 853 | |
Ian Romanick | 0ad22cd | 2010-06-21 17:18:31 -0700 | [diff] [blame] | 854 | if (prog->_LinkedShaders[0]->Type == GL_VERTEX_SHADER) |
Ian Romanick | 9342d26 | 2010-06-22 17:41:37 -0700 | [diff] [blame] | 855 | /* FINISHME: The value of the max_attribute_index parameter is |
| 856 | * FINISHME: implementation dependent based on the value of |
| 857 | * FINISHME: GL_MAX_VERTEX_ATTRIBS. GL_MAX_VERTEX_ATTRIBS must be |
| 858 | * FINISHME: at least 16, so hardcode 16 for now. |
| 859 | */ |
Ian Romanick | 553dcdc | 2010-06-23 12:14:02 -0700 | [diff] [blame] | 860 | if (!assign_attribute_locations(prog, 16)) |
Ian Romanick | 6984670 | 2010-06-22 17:29:19 -0700 | [diff] [blame] | 861 | goto done; |
Ian Romanick | 13e10e4 | 2010-06-21 12:03:24 -0700 | [diff] [blame] | 862 | |
Ian Romanick | 0e59b26 | 2010-06-23 11:23:01 -0700 | [diff] [blame] | 863 | for (unsigned i = 1; i < prog->_NumLinkedShaders; i++) |
| 864 | assign_varying_locations(prog->_LinkedShaders[i - 1], |
| 865 | prog->_LinkedShaders[i]); |
Ian Romanick | 13e10e4 | 2010-06-21 12:03:24 -0700 | [diff] [blame] | 866 | |
| 867 | /* FINISHME: Assign fragment shader output locations. */ |
| 868 | |
Ian Romanick | 832dfa5 | 2010-06-17 15:04:20 -0700 | [diff] [blame] | 869 | done: |
| 870 | free(vert_shader_list); |
| 871 | } |