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 | |
| 71 | extern "C" { |
| 72 | #include <talloc.h> |
| 73 | } |
Ian Romanick | 832dfa5 | 2010-06-17 15:04:20 -0700 | [diff] [blame] | 74 | |
Ian Romanick | 0ad22cd | 2010-06-21 17:18:31 -0700 | [diff] [blame] | 75 | #include "main/mtypes.h" |
Ian Romanick | 25f51d3 | 2010-07-16 15:51:50 -0700 | [diff] [blame] | 76 | #include "main/macros.h" |
Eric Anholt | afe125e | 2010-07-26 17:47:59 -0700 | [diff] [blame] | 77 | #include "main/shaderobj.h" |
Ian Romanick | 832dfa5 | 2010-06-17 15:04:20 -0700 | [diff] [blame] | 78 | #include "glsl_symbol_table.h" |
Ian Romanick | 832dfa5 | 2010-06-17 15:04:20 -0700 | [diff] [blame] | 79 | #include "ir.h" |
| 80 | #include "program.h" |
Aras Pranckevicius | 3174715 | 2010-07-29 12:40:49 +0300 | [diff] [blame] | 81 | #include "program/hash_table.h" |
Ian Romanick | 8fe8a81 | 2010-07-13 17:36:13 -0700 | [diff] [blame] | 82 | #include "linker.h" |
Ian Romanick | a7ba9a7 | 2010-07-20 13:36:32 -0700 | [diff] [blame] | 83 | #include "ir_optimization.h" |
Ian Romanick | 832dfa5 | 2010-06-17 15:04:20 -0700 | [diff] [blame] | 84 | |
| 85 | /** |
| 86 | * Visitor that determines whether or not a variable is ever written. |
| 87 | */ |
| 88 | class find_assignment_visitor : public ir_hierarchical_visitor { |
| 89 | public: |
| 90 | find_assignment_visitor(const char *name) |
| 91 | : name(name), found(false) |
| 92 | { |
| 93 | /* empty */ |
| 94 | } |
| 95 | |
| 96 | virtual ir_visitor_status visit_enter(ir_assignment *ir) |
| 97 | { |
| 98 | ir_variable *const var = ir->lhs->variable_referenced(); |
| 99 | |
| 100 | if (strcmp(name, var->name) == 0) { |
| 101 | found = true; |
| 102 | return visit_stop; |
| 103 | } |
| 104 | |
| 105 | return visit_continue_with_parent; |
| 106 | } |
| 107 | |
| 108 | bool variable_found() |
| 109 | { |
| 110 | return found; |
| 111 | } |
| 112 | |
| 113 | private: |
| 114 | const char *name; /**< Find writes to a variable with this name. */ |
| 115 | bool found; /**< Was a write to the variable found? */ |
| 116 | }; |
| 117 | |
Ian Romanick | c93b8f1 | 2010-06-17 15:20:22 -0700 | [diff] [blame] | 118 | |
Ian Romanick | c33e78f | 2010-08-13 12:30:41 -0700 | [diff] [blame^] | 119 | /** |
| 120 | * Visitor that determines whether or not a variable is ever read. |
| 121 | */ |
| 122 | class find_deref_visitor : public ir_hierarchical_visitor { |
| 123 | public: |
| 124 | find_deref_visitor(const char *name) |
| 125 | : name(name), found(false) |
| 126 | { |
| 127 | /* empty */ |
| 128 | } |
| 129 | |
| 130 | virtual ir_visitor_status visit(ir_dereference_variable *ir) |
| 131 | { |
| 132 | if (strcmp(this->name, ir->var->name) == 0) { |
| 133 | this->found = true; |
| 134 | return visit_stop; |
| 135 | } |
| 136 | |
| 137 | return visit_continue; |
| 138 | } |
| 139 | |
| 140 | bool variable_found() const |
| 141 | { |
| 142 | return this->found; |
| 143 | } |
| 144 | |
| 145 | private: |
| 146 | const char *name; /**< Find writes to a variable with this name. */ |
| 147 | bool found; /**< Was a write to the variable found? */ |
| 148 | }; |
| 149 | |
| 150 | |
Ian Romanick | 0ad22cd | 2010-06-21 17:18:31 -0700 | [diff] [blame] | 151 | void |
Eric Anholt | 849e181 | 2010-06-30 11:49:17 -0700 | [diff] [blame] | 152 | linker_error_printf(gl_shader_program *prog, const char *fmt, ...) |
Ian Romanick | f36460e | 2010-06-23 12:07:22 -0700 | [diff] [blame] | 153 | { |
| 154 | va_list ap; |
| 155 | |
| 156 | prog->InfoLog = talloc_strdup_append(prog->InfoLog, "error: "); |
| 157 | va_start(ap, fmt); |
| 158 | prog->InfoLog = talloc_vasprintf_append(prog->InfoLog, fmt, ap); |
| 159 | va_end(ap); |
| 160 | } |
| 161 | |
| 162 | |
| 163 | void |
Eric Anholt | 16b68b1 | 2010-06-30 11:05:43 -0700 | [diff] [blame] | 164 | invalidate_variable_locations(gl_shader *sh, enum ir_variable_mode mode, |
Ian Romanick | 0ad22cd | 2010-06-21 17:18:31 -0700 | [diff] [blame] | 165 | int generic_base) |
| 166 | { |
Eric Anholt | 16b68b1 | 2010-06-30 11:05:43 -0700 | [diff] [blame] | 167 | foreach_list(node, sh->ir) { |
Ian Romanick | 0ad22cd | 2010-06-21 17:18:31 -0700 | [diff] [blame] | 168 | ir_variable *const var = ((ir_instruction *) node)->as_variable(); |
| 169 | |
| 170 | if ((var == NULL) || (var->mode != (unsigned) mode)) |
| 171 | continue; |
| 172 | |
| 173 | /* Only assign locations for generic attributes / varyings / etc. |
| 174 | */ |
| 175 | if (var->location >= generic_base) |
| 176 | var->location = -1; |
| 177 | } |
| 178 | } |
| 179 | |
| 180 | |
Ian Romanick | c93b8f1 | 2010-06-17 15:20:22 -0700 | [diff] [blame] | 181 | /** |
Ian Romanick | 6984670 | 2010-06-22 17:29:19 -0700 | [diff] [blame] | 182 | * Determine the number of attribute slots required for a particular type |
| 183 | * |
| 184 | * This code is here because it implements the language rules of a specific |
| 185 | * GLSL version. Since it's a property of the language and not a property of |
| 186 | * types in general, it doesn't really belong in glsl_type. |
| 187 | */ |
| 188 | unsigned |
| 189 | count_attribute_slots(const glsl_type *t) |
| 190 | { |
| 191 | /* From page 31 (page 37 of the PDF) of the GLSL 1.50 spec: |
| 192 | * |
| 193 | * "A scalar input counts the same amount against this limit as a vec4, |
| 194 | * so applications may want to consider packing groups of four |
| 195 | * unrelated float inputs together into a vector to better utilize the |
| 196 | * capabilities of the underlying hardware. A matrix input will use up |
| 197 | * multiple locations. The number of locations used will equal the |
| 198 | * number of columns in the matrix." |
| 199 | * |
| 200 | * The spec does not explicitly say how arrays are counted. However, it |
| 201 | * should be safe to assume the total number of slots consumed by an array |
| 202 | * is the number of entries in the array multiplied by the number of slots |
| 203 | * consumed by a single element of the array. |
| 204 | */ |
| 205 | |
| 206 | if (t->is_array()) |
| 207 | return t->array_size() * count_attribute_slots(t->element_type()); |
| 208 | |
| 209 | if (t->is_matrix()) |
| 210 | return t->matrix_columns; |
| 211 | |
| 212 | return 1; |
| 213 | } |
| 214 | |
| 215 | |
| 216 | /** |
Ian Romanick | c93b8f1 | 2010-06-17 15:20:22 -0700 | [diff] [blame] | 217 | * Verify that a vertex shader executable meets all semantic requirements |
| 218 | * |
| 219 | * \param shader Vertex shader executable to be verified |
| 220 | */ |
Ian Romanick | 832dfa5 | 2010-06-17 15:04:20 -0700 | [diff] [blame] | 221 | bool |
Eric Anholt | 849e181 | 2010-06-30 11:49:17 -0700 | [diff] [blame] | 222 | validate_vertex_shader_executable(struct gl_shader_program *prog, |
Eric Anholt | 16b68b1 | 2010-06-30 11:05:43 -0700 | [diff] [blame] | 223 | struct gl_shader *shader) |
Ian Romanick | 832dfa5 | 2010-06-17 15:04:20 -0700 | [diff] [blame] | 224 | { |
| 225 | if (shader == NULL) |
| 226 | return true; |
| 227 | |
Ian Romanick | 832dfa5 | 2010-06-17 15:04:20 -0700 | [diff] [blame] | 228 | find_assignment_visitor find("gl_Position"); |
Eric Anholt | 16b68b1 | 2010-06-30 11:05:43 -0700 | [diff] [blame] | 229 | find.run(shader->ir); |
Ian Romanick | 832dfa5 | 2010-06-17 15:04:20 -0700 | [diff] [blame] | 230 | if (!find.variable_found()) { |
Ian Romanick | f36460e | 2010-06-23 12:07:22 -0700 | [diff] [blame] | 231 | linker_error_printf(prog, |
| 232 | "vertex shader does not write to `gl_Position'\n"); |
Ian Romanick | 832dfa5 | 2010-06-17 15:04:20 -0700 | [diff] [blame] | 233 | return false; |
| 234 | } |
| 235 | |
| 236 | return true; |
| 237 | } |
| 238 | |
| 239 | |
Ian Romanick | c93b8f1 | 2010-06-17 15:20:22 -0700 | [diff] [blame] | 240 | /** |
| 241 | * Verify that a fragment shader executable meets all semantic requirements |
| 242 | * |
| 243 | * \param shader Fragment shader executable to be verified |
| 244 | */ |
Ian Romanick | 832dfa5 | 2010-06-17 15:04:20 -0700 | [diff] [blame] | 245 | bool |
Eric Anholt | 849e181 | 2010-06-30 11:49:17 -0700 | [diff] [blame] | 246 | validate_fragment_shader_executable(struct gl_shader_program *prog, |
Eric Anholt | 16b68b1 | 2010-06-30 11:05:43 -0700 | [diff] [blame] | 247 | struct gl_shader *shader) |
Ian Romanick | 832dfa5 | 2010-06-17 15:04:20 -0700 | [diff] [blame] | 248 | { |
| 249 | if (shader == NULL) |
| 250 | return true; |
| 251 | |
Ian Romanick | 832dfa5 | 2010-06-17 15:04:20 -0700 | [diff] [blame] | 252 | find_assignment_visitor frag_color("gl_FragColor"); |
| 253 | find_assignment_visitor frag_data("gl_FragData"); |
| 254 | |
Eric Anholt | 16b68b1 | 2010-06-30 11:05:43 -0700 | [diff] [blame] | 255 | frag_color.run(shader->ir); |
| 256 | frag_data.run(shader->ir); |
Ian Romanick | 832dfa5 | 2010-06-17 15:04:20 -0700 | [diff] [blame] | 257 | |
Ian Romanick | 832dfa5 | 2010-06-17 15:04:20 -0700 | [diff] [blame] | 258 | if (frag_color.variable_found() && frag_data.variable_found()) { |
Ian Romanick | f36460e | 2010-06-23 12:07:22 -0700 | [diff] [blame] | 259 | linker_error_printf(prog, "fragment shader writes to both " |
| 260 | "`gl_FragColor' and `gl_FragData'\n"); |
Ian Romanick | 832dfa5 | 2010-06-17 15:04:20 -0700 | [diff] [blame] | 261 | return false; |
| 262 | } |
| 263 | |
| 264 | return true; |
| 265 | } |
| 266 | |
| 267 | |
Ian Romanick | cc22c5a | 2010-06-18 17:13:42 -0700 | [diff] [blame] | 268 | /** |
Ian Romanick | e2e5d0d | 2010-06-29 18:47:11 -0700 | [diff] [blame] | 269 | * Generate a string describing the mode of a variable |
| 270 | */ |
| 271 | static const char * |
| 272 | mode_string(const ir_variable *var) |
| 273 | { |
| 274 | switch (var->mode) { |
| 275 | case ir_var_auto: |
| 276 | return (var->read_only) ? "global constant" : "global variable"; |
| 277 | |
| 278 | case ir_var_uniform: return "uniform"; |
| 279 | case ir_var_in: return "shader input"; |
| 280 | case ir_var_out: return "shader output"; |
| 281 | case ir_var_inout: return "shader inout"; |
Ian Romanick | 7e2aa91 | 2010-07-19 17:12:42 -0700 | [diff] [blame] | 282 | |
| 283 | case ir_var_temporary: |
Ian Romanick | e2e5d0d | 2010-06-29 18:47:11 -0700 | [diff] [blame] | 284 | default: |
| 285 | assert(!"Should not get here."); |
| 286 | return "invalid variable"; |
| 287 | } |
| 288 | } |
| 289 | |
| 290 | |
| 291 | /** |
| 292 | * Perform validation of global variables used across multiple shaders |
Ian Romanick | cc22c5a | 2010-06-18 17:13:42 -0700 | [diff] [blame] | 293 | */ |
| 294 | bool |
Ian Romanick | e2e5d0d | 2010-06-29 18:47:11 -0700 | [diff] [blame] | 295 | cross_validate_globals(struct gl_shader_program *prog, |
| 296 | struct gl_shader **shader_list, |
| 297 | unsigned num_shaders, |
| 298 | bool uniforms_only) |
Ian Romanick | cc22c5a | 2010-06-18 17:13:42 -0700 | [diff] [blame] | 299 | { |
| 300 | /* Examine all of the uniforms in all of the shaders and cross validate |
| 301 | * them. |
| 302 | */ |
Ian Romanick | e2e5d0d | 2010-06-29 18:47:11 -0700 | [diff] [blame] | 303 | glsl_symbol_table variables; |
| 304 | for (unsigned i = 0; i < num_shaders; i++) { |
| 305 | foreach_list(node, shader_list[i]->ir) { |
Ian Romanick | cc22c5a | 2010-06-18 17:13:42 -0700 | [diff] [blame] | 306 | ir_variable *const var = ((ir_instruction *) node)->as_variable(); |
| 307 | |
Ian Romanick | e2e5d0d | 2010-06-29 18:47:11 -0700 | [diff] [blame] | 308 | if (var == NULL) |
Ian Romanick | cc22c5a | 2010-06-18 17:13:42 -0700 | [diff] [blame] | 309 | continue; |
| 310 | |
Ian Romanick | e2e5d0d | 2010-06-29 18:47:11 -0700 | [diff] [blame] | 311 | if (uniforms_only && (var->mode != ir_var_uniform)) |
| 312 | continue; |
| 313 | |
Ian Romanick | 7e2aa91 | 2010-07-19 17:12:42 -0700 | [diff] [blame] | 314 | /* Don't cross validate temporaries that are at global scope. These |
| 315 | * will eventually get pulled into the shaders 'main'. |
| 316 | */ |
| 317 | if (var->mode == ir_var_temporary) |
| 318 | continue; |
| 319 | |
Ian Romanick | e2e5d0d | 2010-06-29 18:47:11 -0700 | [diff] [blame] | 320 | /* If a global with this name has already been seen, verify that the |
| 321 | * new instance has the same type. In addition, if the globals have |
Ian Romanick | cc22c5a | 2010-06-18 17:13:42 -0700 | [diff] [blame] | 322 | * initializers, the values of the initializers must be the same. |
| 323 | */ |
Ian Romanick | e2e5d0d | 2010-06-29 18:47:11 -0700 | [diff] [blame] | 324 | ir_variable *const existing = variables.get_variable(var->name); |
Ian Romanick | cc22c5a | 2010-06-18 17:13:42 -0700 | [diff] [blame] | 325 | if (existing != NULL) { |
| 326 | if (var->type != existing->type) { |
Ian Romanick | e2e5d0d | 2010-06-29 18:47:11 -0700 | [diff] [blame] | 327 | linker_error_printf(prog, "%s `%s' declared as type " |
Ian Romanick | f36460e | 2010-06-23 12:07:22 -0700 | [diff] [blame] | 328 | "`%s' and type `%s'\n", |
Ian Romanick | e2e5d0d | 2010-06-29 18:47:11 -0700 | [diff] [blame] | 329 | mode_string(var), |
Ian Romanick | f36460e | 2010-06-23 12:07:22 -0700 | [diff] [blame] | 330 | var->name, var->type->name, |
| 331 | existing->type->name); |
Ian Romanick | cc22c5a | 2010-06-18 17:13:42 -0700 | [diff] [blame] | 332 | return false; |
| 333 | } |
| 334 | |
Ian Romanick | e2e5d0d | 2010-06-29 18:47:11 -0700 | [diff] [blame] | 335 | /* FINISHME: Handle non-constant initializers. |
| 336 | */ |
Ian Romanick | cc22c5a | 2010-06-18 17:13:42 -0700 | [diff] [blame] | 337 | if (var->constant_value != NULL) { |
| 338 | if (existing->constant_value != NULL) { |
| 339 | if (!var->constant_value->has_value(existing->constant_value)) { |
Ian Romanick | e2e5d0d | 2010-06-29 18:47:11 -0700 | [diff] [blame] | 340 | linker_error_printf(prog, "initializers for %s " |
Ian Romanick | f36460e | 2010-06-23 12:07:22 -0700 | [diff] [blame] | 341 | "`%s' have differing values\n", |
Ian Romanick | e2e5d0d | 2010-06-29 18:47:11 -0700 | [diff] [blame] | 342 | mode_string(var), var->name); |
Ian Romanick | cc22c5a | 2010-06-18 17:13:42 -0700 | [diff] [blame] | 343 | return false; |
| 344 | } |
| 345 | } else |
| 346 | /* If the first-seen instance of a particular uniform did not |
| 347 | * have an initializer but a later instance does, copy the |
| 348 | * initializer to the version stored in the symbol table. |
| 349 | */ |
Ian Romanick | de415b7 | 2010-07-14 13:22:12 -0700 | [diff] [blame] | 350 | /* FINISHME: This is wrong. The constant_value field should |
| 351 | * FINISHME: not be modified! Imagine a case where a shader |
| 352 | * FINISHME: without an initializer is linked in two different |
| 353 | * FINISHME: programs with shaders that have differing |
| 354 | * FINISHME: initializers. Linking with the first will |
| 355 | * FINISHME: modify the shader, and linking with the second |
| 356 | * FINISHME: will fail. |
| 357 | */ |
Eric Anholt | 8273bd4 | 2010-08-04 12:34:56 -0700 | [diff] [blame] | 358 | existing->constant_value = |
| 359 | var->constant_value->clone(talloc_parent(existing), NULL); |
Ian Romanick | cc22c5a | 2010-06-18 17:13:42 -0700 | [diff] [blame] | 360 | } |
| 361 | } else |
Ian Romanick | e2e5d0d | 2010-06-29 18:47:11 -0700 | [diff] [blame] | 362 | variables.add_variable(var->name, var); |
Ian Romanick | cc22c5a | 2010-06-18 17:13:42 -0700 | [diff] [blame] | 363 | } |
| 364 | } |
| 365 | |
| 366 | return true; |
| 367 | } |
| 368 | |
| 369 | |
Ian Romanick | 3710192 | 2010-06-18 19:02:10 -0700 | [diff] [blame] | 370 | /** |
Ian Romanick | e2e5d0d | 2010-06-29 18:47:11 -0700 | [diff] [blame] | 371 | * Perform validation of uniforms used across multiple shader stages |
| 372 | */ |
| 373 | bool |
| 374 | cross_validate_uniforms(struct gl_shader_program *prog) |
| 375 | { |
| 376 | return cross_validate_globals(prog, prog->_LinkedShaders, |
| 377 | prog->_NumLinkedShaders, true); |
| 378 | } |
| 379 | |
| 380 | |
| 381 | /** |
Ian Romanick | 3710192 | 2010-06-18 19:02:10 -0700 | [diff] [blame] | 382 | * Validate that outputs from one stage match inputs of another |
| 383 | */ |
| 384 | bool |
Eric Anholt | 849e181 | 2010-06-30 11:49:17 -0700 | [diff] [blame] | 385 | cross_validate_outputs_to_inputs(struct gl_shader_program *prog, |
Eric Anholt | 16b68b1 | 2010-06-30 11:05:43 -0700 | [diff] [blame] | 386 | gl_shader *producer, gl_shader *consumer) |
Ian Romanick | 3710192 | 2010-06-18 19:02:10 -0700 | [diff] [blame] | 387 | { |
| 388 | glsl_symbol_table parameters; |
| 389 | /* FINISHME: Figure these out dynamically. */ |
| 390 | const char *const producer_stage = "vertex"; |
| 391 | const char *const consumer_stage = "fragment"; |
| 392 | |
| 393 | /* Find all shader outputs in the "producer" stage. |
| 394 | */ |
Eric Anholt | 16b68b1 | 2010-06-30 11:05:43 -0700 | [diff] [blame] | 395 | foreach_list(node, producer->ir) { |
Ian Romanick | 3710192 | 2010-06-18 19:02:10 -0700 | [diff] [blame] | 396 | ir_variable *const var = ((ir_instruction *) node)->as_variable(); |
| 397 | |
| 398 | /* FINISHME: For geometry shaders, this should also look for inout |
| 399 | * FINISHME: variables. |
| 400 | */ |
| 401 | if ((var == NULL) || (var->mode != ir_var_out)) |
| 402 | continue; |
| 403 | |
| 404 | parameters.add_variable(var->name, var); |
| 405 | } |
| 406 | |
| 407 | |
| 408 | /* Find all shader inputs in the "consumer" stage. Any variables that have |
| 409 | * matching outputs already in the symbol table must have the same type and |
| 410 | * qualifiers. |
| 411 | */ |
Eric Anholt | 16b68b1 | 2010-06-30 11:05:43 -0700 | [diff] [blame] | 412 | foreach_list(node, consumer->ir) { |
Ian Romanick | 3710192 | 2010-06-18 19:02:10 -0700 | [diff] [blame] | 413 | ir_variable *const input = ((ir_instruction *) node)->as_variable(); |
| 414 | |
| 415 | /* FINISHME: For geometry shaders, this should also look for inout |
| 416 | * FINISHME: variables. |
| 417 | */ |
| 418 | if ((input == NULL) || (input->mode != ir_var_in)) |
| 419 | continue; |
| 420 | |
| 421 | ir_variable *const output = parameters.get_variable(input->name); |
| 422 | if (output != NULL) { |
| 423 | /* Check that the types match between stages. |
| 424 | */ |
| 425 | if (input->type != output->type) { |
Ian Romanick | f36460e | 2010-06-23 12:07:22 -0700 | [diff] [blame] | 426 | linker_error_printf(prog, |
| 427 | "%s shader output `%s' delcared as " |
| 428 | "type `%s', but %s shader input declared " |
| 429 | "as type `%s'\n", |
| 430 | producer_stage, output->name, |
| 431 | output->type->name, |
| 432 | consumer_stage, input->type->name); |
Ian Romanick | 3710192 | 2010-06-18 19:02:10 -0700 | [diff] [blame] | 433 | return false; |
| 434 | } |
| 435 | |
| 436 | /* Check that all of the qualifiers match between stages. |
| 437 | */ |
| 438 | if (input->centroid != output->centroid) { |
Ian Romanick | f36460e | 2010-06-23 12:07:22 -0700 | [diff] [blame] | 439 | linker_error_printf(prog, |
| 440 | "%s shader output `%s' %s centroid qualifier, " |
| 441 | "but %s shader input %s centroid qualifier\n", |
| 442 | producer_stage, |
| 443 | output->name, |
| 444 | (output->centroid) ? "has" : "lacks", |
| 445 | consumer_stage, |
| 446 | (input->centroid) ? "has" : "lacks"); |
Ian Romanick | 3710192 | 2010-06-18 19:02:10 -0700 | [diff] [blame] | 447 | return false; |
| 448 | } |
| 449 | |
| 450 | if (input->invariant != output->invariant) { |
Ian Romanick | f36460e | 2010-06-23 12:07:22 -0700 | [diff] [blame] | 451 | linker_error_printf(prog, |
| 452 | "%s shader output `%s' %s invariant qualifier, " |
| 453 | "but %s shader input %s invariant qualifier\n", |
| 454 | producer_stage, |
| 455 | output->name, |
| 456 | (output->invariant) ? "has" : "lacks", |
| 457 | consumer_stage, |
| 458 | (input->invariant) ? "has" : "lacks"); |
Ian Romanick | 3710192 | 2010-06-18 19:02:10 -0700 | [diff] [blame] | 459 | return false; |
| 460 | } |
| 461 | |
| 462 | if (input->interpolation != output->interpolation) { |
Ian Romanick | f36460e | 2010-06-23 12:07:22 -0700 | [diff] [blame] | 463 | linker_error_printf(prog, |
| 464 | "%s shader output `%s' specifies %s " |
| 465 | "interpolation qualifier, " |
| 466 | "but %s shader input specifies %s " |
| 467 | "interpolation qualifier\n", |
| 468 | producer_stage, |
| 469 | output->name, |
| 470 | output->interpolation_string(), |
| 471 | consumer_stage, |
| 472 | input->interpolation_string()); |
Ian Romanick | 3710192 | 2010-06-18 19:02:10 -0700 | [diff] [blame] | 473 | return false; |
| 474 | } |
| 475 | } |
| 476 | } |
| 477 | |
| 478 | return true; |
| 479 | } |
| 480 | |
| 481 | |
Ian Romanick | 3fb8787 | 2010-07-09 14:09:34 -0700 | [diff] [blame] | 482 | /** |
| 483 | * Populates a shaders symbol table with all global declarations |
| 484 | */ |
| 485 | static void |
| 486 | populate_symbol_table(gl_shader *sh) |
| 487 | { |
| 488 | sh->symbols = new(sh) glsl_symbol_table; |
| 489 | |
| 490 | foreach_list(node, sh->ir) { |
| 491 | ir_instruction *const inst = (ir_instruction *) node; |
| 492 | ir_variable *var; |
| 493 | ir_function *func; |
| 494 | |
| 495 | if ((func = inst->as_function()) != NULL) { |
| 496 | sh->symbols->add_function(func->name, func); |
| 497 | } else if ((var = inst->as_variable()) != NULL) { |
| 498 | sh->symbols->add_variable(var->name, var); |
| 499 | } |
| 500 | } |
| 501 | } |
| 502 | |
| 503 | |
| 504 | /** |
Ian Romanick | 31a9786 | 2010-07-12 18:48:50 -0700 | [diff] [blame] | 505 | * Remap variables referenced in an instruction tree |
| 506 | * |
| 507 | * This is used when instruction trees are cloned from one shader and placed in |
| 508 | * another. These trees will contain references to \c ir_variable nodes that |
| 509 | * do not exist in the target shader. This function finds these \c ir_variable |
| 510 | * references and replaces the references with matching variables in the target |
| 511 | * shader. |
| 512 | * |
| 513 | * If there is no matching variable in the target shader, a clone of the |
| 514 | * \c ir_variable is made and added to the target shader. The new variable is |
| 515 | * added to \b both the instruction stream and the symbol table. |
| 516 | * |
| 517 | * \param inst IR tree that is to be processed. |
| 518 | * \param symbols Symbol table containing global scope symbols in the |
| 519 | * linked shader. |
| 520 | * \param instructions Instruction stream where new variable declarations |
| 521 | * should be added. |
| 522 | */ |
| 523 | void |
Eric Anholt | 8273bd4 | 2010-08-04 12:34:56 -0700 | [diff] [blame] | 524 | remap_variables(ir_instruction *inst, struct gl_shader *target, |
| 525 | hash_table *temps) |
Ian Romanick | 31a9786 | 2010-07-12 18:48:50 -0700 | [diff] [blame] | 526 | { |
| 527 | class remap_visitor : public ir_hierarchical_visitor { |
| 528 | public: |
Eric Anholt | 8273bd4 | 2010-08-04 12:34:56 -0700 | [diff] [blame] | 529 | remap_visitor(struct gl_shader *target, |
Ian Romanick | 7e2aa91 | 2010-07-19 17:12:42 -0700 | [diff] [blame] | 530 | hash_table *temps) |
Ian Romanick | 31a9786 | 2010-07-12 18:48:50 -0700 | [diff] [blame] | 531 | { |
Eric Anholt | 8273bd4 | 2010-08-04 12:34:56 -0700 | [diff] [blame] | 532 | this->target = target; |
| 533 | this->symbols = target->symbols; |
| 534 | this->instructions = target->ir; |
Ian Romanick | 7e2aa91 | 2010-07-19 17:12:42 -0700 | [diff] [blame] | 535 | this->temps = temps; |
Ian Romanick | 31a9786 | 2010-07-12 18:48:50 -0700 | [diff] [blame] | 536 | } |
| 537 | |
| 538 | virtual ir_visitor_status visit(ir_dereference_variable *ir) |
| 539 | { |
Ian Romanick | 7e2aa91 | 2010-07-19 17:12:42 -0700 | [diff] [blame] | 540 | if (ir->var->mode == ir_var_temporary) { |
| 541 | ir_variable *var = (ir_variable *) hash_table_find(temps, ir->var); |
| 542 | |
| 543 | assert(var != NULL); |
| 544 | ir->var = var; |
| 545 | return visit_continue; |
| 546 | } |
| 547 | |
Ian Romanick | 31a9786 | 2010-07-12 18:48:50 -0700 | [diff] [blame] | 548 | ir_variable *const existing = |
| 549 | this->symbols->get_variable(ir->var->name); |
| 550 | if (existing != NULL) |
| 551 | ir->var = existing; |
| 552 | else { |
Eric Anholt | 8273bd4 | 2010-08-04 12:34:56 -0700 | [diff] [blame] | 553 | ir_variable *copy = ir->var->clone(this->target, NULL); |
Ian Romanick | 31a9786 | 2010-07-12 18:48:50 -0700 | [diff] [blame] | 554 | |
| 555 | this->symbols->add_variable(copy->name, copy); |
| 556 | this->instructions->push_head(copy); |
Ian Romanick | 7e2aa91 | 2010-07-19 17:12:42 -0700 | [diff] [blame] | 557 | ir->var = copy; |
Ian Romanick | 31a9786 | 2010-07-12 18:48:50 -0700 | [diff] [blame] | 558 | } |
| 559 | |
| 560 | return visit_continue; |
| 561 | } |
| 562 | |
| 563 | private: |
Eric Anholt | 8273bd4 | 2010-08-04 12:34:56 -0700 | [diff] [blame] | 564 | struct gl_shader *target; |
Ian Romanick | 31a9786 | 2010-07-12 18:48:50 -0700 | [diff] [blame] | 565 | glsl_symbol_table *symbols; |
| 566 | exec_list *instructions; |
Ian Romanick | 7e2aa91 | 2010-07-19 17:12:42 -0700 | [diff] [blame] | 567 | hash_table *temps; |
Ian Romanick | 31a9786 | 2010-07-12 18:48:50 -0700 | [diff] [blame] | 568 | }; |
| 569 | |
Eric Anholt | 8273bd4 | 2010-08-04 12:34:56 -0700 | [diff] [blame] | 570 | remap_visitor v(target, temps); |
Ian Romanick | 31a9786 | 2010-07-12 18:48:50 -0700 | [diff] [blame] | 571 | |
| 572 | inst->accept(&v); |
| 573 | } |
| 574 | |
| 575 | |
| 576 | /** |
| 577 | * Move non-declarations from one instruction stream to another |
| 578 | * |
| 579 | * 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] | 580 | * 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] | 581 | * pointer) for \c last and \c false for \c make_copies on the first |
| 582 | * call. Successive calls pass the return value of the previous call for |
| 583 | * \c last and \c true for \c make_copies. |
| 584 | * |
| 585 | * \param instructions Source instruction stream |
| 586 | * \param last Instruction after which new instructions should be |
| 587 | * inserted in the target instruction stream |
| 588 | * \param make_copies Flag selecting whether instructions in \c instructions |
| 589 | * should be copied (via \c ir_instruction::clone) into the |
| 590 | * target list or moved. |
| 591 | * |
| 592 | * \return |
| 593 | * The new "last" instruction in the target instruction stream. This pointer |
| 594 | * is suitable for use as the \c last parameter of a later call to this |
| 595 | * function. |
| 596 | */ |
| 597 | exec_node * |
| 598 | move_non_declarations(exec_list *instructions, exec_node *last, |
| 599 | bool make_copies, gl_shader *target) |
| 600 | { |
Ian Romanick | 7e2aa91 | 2010-07-19 17:12:42 -0700 | [diff] [blame] | 601 | hash_table *temps = NULL; |
| 602 | |
| 603 | if (make_copies) |
| 604 | temps = hash_table_ctor(0, hash_table_pointer_hash, |
| 605 | hash_table_pointer_compare); |
| 606 | |
Ian Romanick | 303c99f | 2010-07-19 12:34:56 -0700 | [diff] [blame] | 607 | foreach_list_safe(node, instructions) { |
Ian Romanick | 31a9786 | 2010-07-12 18:48:50 -0700 | [diff] [blame] | 608 | ir_instruction *inst = (ir_instruction *) node; |
| 609 | |
Ian Romanick | 7e2aa91 | 2010-07-19 17:12:42 -0700 | [diff] [blame] | 610 | if (inst->as_function()) |
Ian Romanick | 31a9786 | 2010-07-12 18:48:50 -0700 | [diff] [blame] | 611 | continue; |
| 612 | |
Ian Romanick | 7e2aa91 | 2010-07-19 17:12:42 -0700 | [diff] [blame] | 613 | ir_variable *var = inst->as_variable(); |
| 614 | if ((var != NULL) && (var->mode != ir_var_temporary)) |
| 615 | continue; |
| 616 | |
| 617 | assert(inst->as_assignment() |
| 618 | || ((var != NULL) && (var->mode == ir_var_temporary))); |
Ian Romanick | 31a9786 | 2010-07-12 18:48:50 -0700 | [diff] [blame] | 619 | |
| 620 | if (make_copies) { |
Eric Anholt | 8273bd4 | 2010-08-04 12:34:56 -0700 | [diff] [blame] | 621 | inst = inst->clone(target, NULL); |
Ian Romanick | 7e2aa91 | 2010-07-19 17:12:42 -0700 | [diff] [blame] | 622 | |
| 623 | if (var != NULL) |
| 624 | hash_table_insert(temps, inst, var); |
| 625 | else |
Eric Anholt | 8273bd4 | 2010-08-04 12:34:56 -0700 | [diff] [blame] | 626 | remap_variables(inst, target, temps); |
Ian Romanick | 31a9786 | 2010-07-12 18:48:50 -0700 | [diff] [blame] | 627 | } else { |
| 628 | inst->remove(); |
| 629 | } |
| 630 | |
| 631 | last->insert_after(inst); |
| 632 | last = inst; |
| 633 | } |
| 634 | |
Ian Romanick | 7e2aa91 | 2010-07-19 17:12:42 -0700 | [diff] [blame] | 635 | if (make_copies) |
| 636 | hash_table_dtor(temps); |
| 637 | |
Ian Romanick | 31a9786 | 2010-07-12 18:48:50 -0700 | [diff] [blame] | 638 | return last; |
| 639 | } |
| 640 | |
| 641 | /** |
Ian Romanick | 15ce87e | 2010-07-09 15:28:22 -0700 | [diff] [blame] | 642 | * Get the function signature for main from a shader |
| 643 | */ |
| 644 | static ir_function_signature * |
| 645 | get_main_function_signature(gl_shader *sh) |
| 646 | { |
| 647 | ir_function *const f = sh->symbols->get_function("main"); |
| 648 | if (f != NULL) { |
| 649 | exec_list void_parameters; |
| 650 | |
| 651 | /* Look for the 'void main()' signature and ensure that it's defined. |
| 652 | * This keeps the linker from accidentally pick a shader that just |
| 653 | * contains a prototype for main. |
| 654 | * |
| 655 | * We don't have to check for multiple definitions of main (in multiple |
| 656 | * shaders) because that would have already been caught above. |
| 657 | */ |
| 658 | ir_function_signature *sig = f->matching_signature(&void_parameters); |
| 659 | if ((sig != NULL) && sig->is_defined) { |
| 660 | return sig; |
| 661 | } |
| 662 | } |
| 663 | |
| 664 | return NULL; |
| 665 | } |
| 666 | |
| 667 | |
| 668 | /** |
Ian Romanick | 3fb8787 | 2010-07-09 14:09:34 -0700 | [diff] [blame] | 669 | * Combine a group of shaders for a single stage to generate a linked shader |
| 670 | * |
| 671 | * \note |
| 672 | * If this function is supplied a single shader, it is cloned, and the new |
| 673 | * shader is returned. |
| 674 | */ |
| 675 | static struct gl_shader * |
| 676 | link_intrastage_shaders(struct gl_shader_program *prog, |
| 677 | struct gl_shader **shader_list, |
| 678 | unsigned num_shaders) |
| 679 | { |
Ian Romanick | 13f782c | 2010-06-29 18:53:38 -0700 | [diff] [blame] | 680 | /* Check that global variables defined in multiple shaders are consistent. |
| 681 | */ |
| 682 | if (!cross_validate_globals(prog, shader_list, num_shaders, false)) |
| 683 | return NULL; |
| 684 | |
| 685 | /* Check that there is only a single definition of each function signature |
| 686 | * across all shaders. |
| 687 | */ |
| 688 | for (unsigned i = 0; i < (num_shaders - 1); i++) { |
| 689 | foreach_list(node, shader_list[i]->ir) { |
| 690 | ir_function *const f = ((ir_instruction *) node)->as_function(); |
| 691 | |
| 692 | if (f == NULL) |
| 693 | continue; |
| 694 | |
| 695 | for (unsigned j = i + 1; j < num_shaders; j++) { |
| 696 | ir_function *const other = |
| 697 | shader_list[j]->symbols->get_function(f->name); |
| 698 | |
| 699 | /* If the other shader has no function (and therefore no function |
| 700 | * signatures) with the same name, skip to the next shader. |
| 701 | */ |
| 702 | if (other == NULL) |
| 703 | continue; |
| 704 | |
| 705 | foreach_iter (exec_list_iterator, iter, *f) { |
| 706 | ir_function_signature *sig = |
| 707 | (ir_function_signature *) iter.get(); |
| 708 | |
| 709 | if (!sig->is_defined || sig->is_built_in) |
| 710 | continue; |
| 711 | |
| 712 | ir_function_signature *other_sig = |
| 713 | other->exact_matching_signature(& sig->parameters); |
| 714 | |
| 715 | if ((other_sig != NULL) && other_sig->is_defined |
| 716 | && !other_sig->is_built_in) { |
| 717 | linker_error_printf(prog, |
| 718 | "function `%s' is multiply defined", |
| 719 | f->name); |
| 720 | return NULL; |
| 721 | } |
| 722 | } |
| 723 | } |
| 724 | } |
| 725 | } |
| 726 | |
| 727 | /* Find the shader that defines main, and make a clone of it. |
| 728 | * |
| 729 | * Starting with the clone, search for undefined references. If one is |
| 730 | * found, find the shader that defines it. Clone the reference and add |
| 731 | * it to the shader. Repeat until there are no undefined references or |
| 732 | * until a reference cannot be resolved. |
| 733 | */ |
Ian Romanick | 15ce87e | 2010-07-09 15:28:22 -0700 | [diff] [blame] | 734 | gl_shader *main = NULL; |
| 735 | for (unsigned i = 0; i < num_shaders; i++) { |
| 736 | if (get_main_function_signature(shader_list[i]) != NULL) { |
| 737 | main = shader_list[i]; |
| 738 | break; |
| 739 | } |
| 740 | } |
Ian Romanick | 13f782c | 2010-06-29 18:53:38 -0700 | [diff] [blame] | 741 | |
Ian Romanick | 15ce87e | 2010-07-09 15:28:22 -0700 | [diff] [blame] | 742 | if (main == NULL) { |
| 743 | linker_error_printf(prog, "%s shader lacks `main'\n", |
| 744 | (shader_list[0]->Type == GL_VERTEX_SHADER) |
| 745 | ? "vertex" : "fragment"); |
| 746 | return NULL; |
| 747 | } |
| 748 | |
| 749 | gl_shader *const linked = _mesa_new_shader(NULL, 0, main->Type); |
| 750 | linked->ir = new(linked) exec_list; |
Eric Anholt | 8273bd4 | 2010-08-04 12:34:56 -0700 | [diff] [blame] | 751 | clone_ir_list(linked, linked->ir, main->ir); |
Ian Romanick | 15ce87e | 2010-07-09 15:28:22 -0700 | [diff] [blame] | 752 | |
| 753 | populate_symbol_table(linked); |
Ian Romanick | 13f782c | 2010-06-29 18:53:38 -0700 | [diff] [blame] | 754 | |
Ian Romanick | 31a9786 | 2010-07-12 18:48:50 -0700 | [diff] [blame] | 755 | /* The a pointer to the main function in the final linked shader (i.e., the |
| 756 | * copy of the original shader that contained the main function). |
| 757 | */ |
| 758 | ir_function_signature *const main_sig = get_main_function_signature(linked); |
| 759 | |
| 760 | /* Move any instructions other than variable declarations or function |
| 761 | * declarations into main. |
| 762 | */ |
Ian Romanick | 9303e35 | 2010-07-19 12:33:54 -0700 | [diff] [blame] | 763 | exec_node *insertion_point = |
| 764 | move_non_declarations(linked->ir, (exec_node *) &main_sig->body, false, |
| 765 | linked); |
| 766 | |
Ian Romanick | 31a9786 | 2010-07-12 18:48:50 -0700 | [diff] [blame] | 767 | for (unsigned i = 0; i < num_shaders; i++) { |
Ian Romanick | 9303e35 | 2010-07-19 12:33:54 -0700 | [diff] [blame] | 768 | if (shader_list[i] == main) |
| 769 | continue; |
| 770 | |
Ian Romanick | 31a9786 | 2010-07-12 18:48:50 -0700 | [diff] [blame] | 771 | insertion_point = move_non_declarations(shader_list[i]->ir, |
Ian Romanick | 9303e35 | 2010-07-19 12:33:54 -0700 | [diff] [blame] | 772 | insertion_point, true, linked); |
Ian Romanick | 31a9786 | 2010-07-12 18:48:50 -0700 | [diff] [blame] | 773 | } |
| 774 | |
Ian Romanick | 13f782c | 2010-06-29 18:53:38 -0700 | [diff] [blame] | 775 | /* Resolve initializers for global variables in the linked shader. |
| 776 | */ |
Ian Romanick | d5be2ac | 2010-07-20 11:29:46 -0700 | [diff] [blame] | 777 | unsigned num_linking_shaders = num_shaders; |
| 778 | for (unsigned i = 0; i < num_shaders; i++) |
| 779 | num_linking_shaders += shader_list[i]->num_builtins_to_link; |
| 780 | |
| 781 | gl_shader **linking_shaders = |
| 782 | (gl_shader **) calloc(num_linking_shaders, sizeof(gl_shader *)); |
| 783 | |
| 784 | memcpy(linking_shaders, shader_list, |
| 785 | sizeof(linking_shaders[0]) * num_shaders); |
| 786 | |
| 787 | unsigned idx = num_shaders; |
| 788 | for (unsigned i = 0; i < num_shaders; i++) { |
| 789 | memcpy(&linking_shaders[idx], shader_list[i]->builtins_to_link, |
| 790 | sizeof(linking_shaders[0]) * shader_list[i]->num_builtins_to_link); |
| 791 | idx += shader_list[i]->num_builtins_to_link; |
| 792 | } |
| 793 | |
| 794 | assert(idx == num_linking_shaders); |
| 795 | |
| 796 | link_function_calls(prog, linked, linking_shaders, num_linking_shaders); |
| 797 | |
| 798 | free(linking_shaders); |
Ian Romanick | 3fb8787 | 2010-07-09 14:09:34 -0700 | [diff] [blame] | 799 | |
Ian Romanick | 3fb8787 | 2010-07-09 14:09:34 -0700 | [diff] [blame] | 800 | return linked; |
| 801 | } |
| 802 | |
| 803 | |
Ian Romanick | 019a59b | 2010-06-21 16:10:42 -0700 | [diff] [blame] | 804 | struct uniform_node { |
| 805 | exec_node link; |
| 806 | struct gl_uniform *u; |
| 807 | unsigned slots; |
| 808 | }; |
| 809 | |
Ian Romanick | abee16e | 2010-06-21 16:16:05 -0700 | [diff] [blame] | 810 | void |
Eric Anholt | 849e181 | 2010-06-30 11:49:17 -0700 | [diff] [blame] | 811 | assign_uniform_locations(struct gl_shader_program *prog) |
Ian Romanick | 019a59b | 2010-06-21 16:10:42 -0700 | [diff] [blame] | 812 | { |
| 813 | /* */ |
| 814 | exec_list uniforms; |
| 815 | unsigned total_uniforms = 0; |
| 816 | hash_table *ht = hash_table_ctor(32, hash_table_string_hash, |
| 817 | hash_table_string_compare); |
| 818 | |
Ian Romanick | abee16e | 2010-06-21 16:16:05 -0700 | [diff] [blame] | 819 | for (unsigned i = 0; i < prog->_NumLinkedShaders; i++) { |
Ian Romanick | 019a59b | 2010-06-21 16:10:42 -0700 | [diff] [blame] | 820 | unsigned next_position = 0; |
| 821 | |
Eric Anholt | 16b68b1 | 2010-06-30 11:05:43 -0700 | [diff] [blame] | 822 | foreach_list(node, prog->_LinkedShaders[i]->ir) { |
Ian Romanick | 019a59b | 2010-06-21 16:10:42 -0700 | [diff] [blame] | 823 | ir_variable *const var = ((ir_instruction *) node)->as_variable(); |
| 824 | |
| 825 | if ((var == NULL) || (var->mode != ir_var_uniform)) |
| 826 | continue; |
| 827 | |
| 828 | const unsigned vec4_slots = (var->component_slots() + 3) / 4; |
Eric Anholt | 8d61a23 | 2010-08-05 16:00:46 -0700 | [diff] [blame] | 829 | if (vec4_slots == 0) { |
| 830 | /* If we've got a sampler or an aggregate of them, the size can |
| 831 | * end up zero. Don't allocate any space. |
| 832 | */ |
| 833 | continue; |
| 834 | } |
Ian Romanick | 019a59b | 2010-06-21 16:10:42 -0700 | [diff] [blame] | 835 | |
| 836 | uniform_node *n = (uniform_node *) hash_table_find(ht, var->name); |
| 837 | if (n == NULL) { |
| 838 | n = (uniform_node *) calloc(1, sizeof(struct uniform_node)); |
| 839 | n->u = (gl_uniform *) calloc(vec4_slots, sizeof(struct gl_uniform)); |
| 840 | n->slots = vec4_slots; |
| 841 | |
| 842 | n->u[0].Name = strdup(var->name); |
| 843 | for (unsigned j = 1; j < vec4_slots; j++) |
| 844 | n->u[j].Name = n->u[0].Name; |
| 845 | |
| 846 | hash_table_insert(ht, n, n->u[0].Name); |
| 847 | uniforms.push_tail(& n->link); |
| 848 | total_uniforms += vec4_slots; |
| 849 | } |
| 850 | |
| 851 | if (var->constant_value != NULL) |
| 852 | for (unsigned j = 0; j < vec4_slots; j++) |
| 853 | n->u[j].Initialized = true; |
| 854 | |
| 855 | var->location = next_position; |
| 856 | |
| 857 | for (unsigned j = 0; j < vec4_slots; j++) { |
Ian Romanick | abee16e | 2010-06-21 16:16:05 -0700 | [diff] [blame] | 858 | switch (prog->_LinkedShaders[i]->Type) { |
Ian Romanick | 019a59b | 2010-06-21 16:10:42 -0700 | [diff] [blame] | 859 | case GL_VERTEX_SHADER: |
| 860 | n->u[j].VertPos = next_position; |
| 861 | break; |
| 862 | case GL_FRAGMENT_SHADER: |
| 863 | n->u[j].FragPos = next_position; |
| 864 | break; |
| 865 | case GL_GEOMETRY_SHADER: |
| 866 | /* FINISHME: Support geometry shaders. */ |
Ian Romanick | abee16e | 2010-06-21 16:16:05 -0700 | [diff] [blame] | 867 | assert(prog->_LinkedShaders[i]->Type != GL_GEOMETRY_SHADER); |
Ian Romanick | 019a59b | 2010-06-21 16:10:42 -0700 | [diff] [blame] | 868 | break; |
| 869 | } |
| 870 | |
| 871 | next_position++; |
| 872 | } |
| 873 | } |
| 874 | } |
| 875 | |
| 876 | gl_uniform_list *ul = (gl_uniform_list *) |
| 877 | calloc(1, sizeof(gl_uniform_list)); |
| 878 | |
| 879 | ul->Size = total_uniforms; |
| 880 | ul->NumUniforms = total_uniforms; |
| 881 | ul->Uniforms = (gl_uniform *) calloc(total_uniforms, sizeof(gl_uniform)); |
| 882 | |
| 883 | unsigned idx = 0; |
| 884 | uniform_node *next; |
| 885 | for (uniform_node *node = (uniform_node *) uniforms.head |
| 886 | ; node->link.next != NULL |
| 887 | ; node = next) { |
| 888 | next = (uniform_node *) node->link.next; |
| 889 | |
| 890 | node->link.remove(); |
| 891 | memcpy(&ul->Uniforms[idx], node->u, sizeof(gl_uniform) * node->slots); |
| 892 | idx += node->slots; |
| 893 | |
| 894 | free(node->u); |
| 895 | free(node); |
| 896 | } |
| 897 | |
| 898 | hash_table_dtor(ht); |
| 899 | |
Ian Romanick | abee16e | 2010-06-21 16:16:05 -0700 | [diff] [blame] | 900 | prog->Uniforms = ul; |
Ian Romanick | 019a59b | 2010-06-21 16:10:42 -0700 | [diff] [blame] | 901 | } |
| 902 | |
| 903 | |
Ian Romanick | 6984670 | 2010-06-22 17:29:19 -0700 | [diff] [blame] | 904 | /** |
| 905 | * Find a contiguous set of available bits in a bitmask |
| 906 | * |
| 907 | * \param used_mask Bits representing used (1) and unused (0) locations |
| 908 | * \param needed_count Number of contiguous bits needed. |
| 909 | * |
| 910 | * \return |
| 911 | * Base location of the available bits on success or -1 on failure. |
| 912 | */ |
| 913 | int |
| 914 | find_available_slots(unsigned used_mask, unsigned needed_count) |
| 915 | { |
| 916 | unsigned needed_mask = (1 << needed_count) - 1; |
| 917 | const int max_bit_to_test = (8 * sizeof(used_mask)) - needed_count; |
| 918 | |
| 919 | /* The comparison to 32 is redundant, but without it GCC emits "warning: |
| 920 | * cannot optimize possibly infinite loops" for the loop below. |
| 921 | */ |
| 922 | if ((needed_count == 0) || (max_bit_to_test < 0) || (max_bit_to_test > 32)) |
| 923 | return -1; |
| 924 | |
| 925 | for (int i = 0; i <= max_bit_to_test; i++) { |
| 926 | if ((needed_mask & ~used_mask) == needed_mask) |
| 927 | return i; |
| 928 | |
| 929 | needed_mask <<= 1; |
| 930 | } |
| 931 | |
| 932 | return -1; |
| 933 | } |
| 934 | |
| 935 | |
| 936 | bool |
Eric Anholt | 849e181 | 2010-06-30 11:49:17 -0700 | [diff] [blame] | 937 | assign_attribute_locations(gl_shader_program *prog, unsigned max_attribute_index) |
Ian Romanick | 0ad22cd | 2010-06-21 17:18:31 -0700 | [diff] [blame] | 938 | { |
Ian Romanick | 9342d26 | 2010-06-22 17:41:37 -0700 | [diff] [blame] | 939 | /* Mark invalid attribute locations as being used. |
| 940 | */ |
| 941 | unsigned used_locations = (max_attribute_index >= 32) |
| 942 | ? ~0 : ~((1 << max_attribute_index) - 1); |
Ian Romanick | 0ad22cd | 2010-06-21 17:18:31 -0700 | [diff] [blame] | 943 | |
Eric Anholt | 16b68b1 | 2010-06-30 11:05:43 -0700 | [diff] [blame] | 944 | gl_shader *const sh = prog->_LinkedShaders[0]; |
Ian Romanick | 0ad22cd | 2010-06-21 17:18:31 -0700 | [diff] [blame] | 945 | assert(sh->Type == GL_VERTEX_SHADER); |
| 946 | |
Ian Romanick | 6984670 | 2010-06-22 17:29:19 -0700 | [diff] [blame] | 947 | /* Operate in a total of four passes. |
Ian Romanick | 0ad22cd | 2010-06-21 17:18:31 -0700 | [diff] [blame] | 948 | * |
| 949 | * 1. Invalidate the location assignments for all vertex shader inputs. |
| 950 | * |
| 951 | * 2. Assign locations for inputs that have user-defined (via |
| 952 | * glBindVertexAttribLocation) locatoins. |
| 953 | * |
Ian Romanick | 6984670 | 2010-06-22 17:29:19 -0700 | [diff] [blame] | 954 | * 3. Sort the attributes without assigned locations by number of slots |
| 955 | * required in decreasing order. Fragmentation caused by attribute |
| 956 | * locations assigned by the application may prevent large attributes |
| 957 | * from having enough contiguous space. |
| 958 | * |
| 959 | * 4. Assign locations to any inputs without assigned locations. |
Ian Romanick | 0ad22cd | 2010-06-21 17:18:31 -0700 | [diff] [blame] | 960 | */ |
| 961 | |
| 962 | invalidate_variable_locations(sh, ir_var_in, VERT_ATTRIB_GENERIC0); |
| 963 | |
Ian Romanick | 553dcdc | 2010-06-23 12:14:02 -0700 | [diff] [blame] | 964 | if (prog->Attributes != NULL) { |
| 965 | for (unsigned i = 0; i < prog->Attributes->NumParameters; i++) { |
Ian Romanick | 0ad22cd | 2010-06-21 17:18:31 -0700 | [diff] [blame] | 966 | ir_variable *const var = |
Ian Romanick | 553dcdc | 2010-06-23 12:14:02 -0700 | [diff] [blame] | 967 | sh->symbols->get_variable(prog->Attributes->Parameters[i].Name); |
Ian Romanick | 0ad22cd | 2010-06-21 17:18:31 -0700 | [diff] [blame] | 968 | |
Ian Romanick | 6984670 | 2010-06-22 17:29:19 -0700 | [diff] [blame] | 969 | /* Note: attributes that occupy multiple slots, such as arrays or |
| 970 | * matrices, may appear in the attrib array multiple times. |
| 971 | */ |
| 972 | if ((var == NULL) || (var->location != -1)) |
Ian Romanick | 0ad22cd | 2010-06-21 17:18:31 -0700 | [diff] [blame] | 973 | continue; |
| 974 | |
Ian Romanick | 6984670 | 2010-06-22 17:29:19 -0700 | [diff] [blame] | 975 | /* From page 61 of the OpenGL 4.0 spec: |
| 976 | * |
| 977 | * "LinkProgram will fail if the attribute bindings assigned by |
| 978 | * BindAttribLocation do not leave not enough space to assign a |
| 979 | * location for an active matrix attribute or an active attribute |
| 980 | * array, both of which require multiple contiguous generic |
| 981 | * attributes." |
| 982 | * |
| 983 | * Previous versions of the spec contain similar language but omit the |
| 984 | * bit about attribute arrays. |
| 985 | * |
| 986 | * Page 61 of the OpenGL 4.0 spec also says: |
| 987 | * |
| 988 | * "It is possible for an application to bind more than one |
| 989 | * attribute name to the same location. This is referred to as |
| 990 | * aliasing. This will only work if only one of the aliased |
| 991 | * attributes is active in the executable program, or if no path |
| 992 | * through the shader consumes more than one attribute of a set |
| 993 | * of attributes aliased to the same location. A link error can |
| 994 | * occur if the linker determines that every path through the |
| 995 | * shader consumes multiple aliased attributes, but |
| 996 | * implementations are not required to generate an error in this |
| 997 | * case." |
| 998 | * |
| 999 | * These two paragraphs are either somewhat contradictory, or I don't |
| 1000 | * fully understand one or both of them. |
| 1001 | */ |
| 1002 | /* FINISHME: The code as currently written does not support attribute |
| 1003 | * FINISHME: location aliasing (see comment above). |
| 1004 | */ |
Ian Romanick | 553dcdc | 2010-06-23 12:14:02 -0700 | [diff] [blame] | 1005 | const int attr = prog->Attributes->Parameters[i].StateIndexes[0]; |
Ian Romanick | 6984670 | 2010-06-22 17:29:19 -0700 | [diff] [blame] | 1006 | const unsigned slots = count_attribute_slots(var->type); |
| 1007 | |
| 1008 | /* Mask representing the contiguous slots that will be used by this |
| 1009 | * attribute. |
| 1010 | */ |
| 1011 | const unsigned use_mask = (1 << slots) - 1; |
| 1012 | |
| 1013 | /* Generate a link error if the set of bits requested for this |
| 1014 | * attribute overlaps any previously allocated bits. |
| 1015 | */ |
| 1016 | if ((~(use_mask << attr) & used_locations) != used_locations) { |
Ian Romanick | 553dcdc | 2010-06-23 12:14:02 -0700 | [diff] [blame] | 1017 | linker_error_printf(prog, |
| 1018 | "insufficient contiguous attribute locations " |
| 1019 | "available for vertex shader input `%s'", |
| 1020 | var->name); |
Ian Romanick | 6984670 | 2010-06-22 17:29:19 -0700 | [diff] [blame] | 1021 | return false; |
| 1022 | } |
Ian Romanick | 0ad22cd | 2010-06-21 17:18:31 -0700 | [diff] [blame] | 1023 | |
| 1024 | var->location = VERT_ATTRIB_GENERIC0 + attr; |
Ian Romanick | 6984670 | 2010-06-22 17:29:19 -0700 | [diff] [blame] | 1025 | used_locations |= (use_mask << attr); |
Ian Romanick | 0ad22cd | 2010-06-21 17:18:31 -0700 | [diff] [blame] | 1026 | } |
| 1027 | } |
| 1028 | |
Ian Romanick | 6984670 | 2010-06-22 17:29:19 -0700 | [diff] [blame] | 1029 | /* Temporary storage for the set of attributes that need locations assigned. |
| 1030 | */ |
| 1031 | struct temp_attr { |
| 1032 | unsigned slots; |
| 1033 | ir_variable *var; |
| 1034 | |
| 1035 | /* Used below in the call to qsort. */ |
| 1036 | static int compare(const void *a, const void *b) |
| 1037 | { |
| 1038 | const temp_attr *const l = (const temp_attr *) a; |
| 1039 | const temp_attr *const r = (const temp_attr *) b; |
| 1040 | |
| 1041 | /* Reversed because we want a descending order sort below. */ |
| 1042 | return r->slots - l->slots; |
| 1043 | } |
| 1044 | } to_assign[16]; |
| 1045 | |
| 1046 | unsigned num_attr = 0; |
| 1047 | |
Eric Anholt | 16b68b1 | 2010-06-30 11:05:43 -0700 | [diff] [blame] | 1048 | foreach_list(node, sh->ir) { |
Ian Romanick | 0ad22cd | 2010-06-21 17:18:31 -0700 | [diff] [blame] | 1049 | ir_variable *const var = ((ir_instruction *) node)->as_variable(); |
| 1050 | |
| 1051 | if ((var == NULL) || (var->mode != ir_var_in)) |
| 1052 | continue; |
| 1053 | |
| 1054 | /* The location was explicitly assigned, nothing to do here. |
| 1055 | */ |
| 1056 | if (var->location != -1) |
| 1057 | continue; |
| 1058 | |
Ian Romanick | 6984670 | 2010-06-22 17:29:19 -0700 | [diff] [blame] | 1059 | to_assign[num_attr].slots = count_attribute_slots(var->type); |
| 1060 | to_assign[num_attr].var = var; |
| 1061 | num_attr++; |
Ian Romanick | 0ad22cd | 2010-06-21 17:18:31 -0700 | [diff] [blame] | 1062 | } |
Ian Romanick | 6984670 | 2010-06-22 17:29:19 -0700 | [diff] [blame] | 1063 | |
| 1064 | /* If all of the attributes were assigned locations by the application (or |
| 1065 | * are built-in attributes with fixed locations), return early. This should |
| 1066 | * be the common case. |
| 1067 | */ |
| 1068 | if (num_attr == 0) |
| 1069 | return true; |
| 1070 | |
| 1071 | qsort(to_assign, num_attr, sizeof(to_assign[0]), temp_attr::compare); |
| 1072 | |
Ian Romanick | 982e379 | 2010-06-29 18:58:20 -0700 | [diff] [blame] | 1073 | /* VERT_ATTRIB_GENERIC0 is a psdueo-alias for VERT_ATTRIB_POS. It can only |
| 1074 | * be explicitly assigned by via glBindAttribLocation. Mark it as reserved |
| 1075 | * to prevent it from being automatically allocated below. |
| 1076 | */ |
Ian Romanick | c33e78f | 2010-08-13 12:30:41 -0700 | [diff] [blame^] | 1077 | find_deref_visitor find("gl_Vertex"); |
| 1078 | find.run(sh->ir); |
| 1079 | if (find.variable_found()) |
| 1080 | used_locations |= (1 << 0); |
Ian Romanick | 982e379 | 2010-06-29 18:58:20 -0700 | [diff] [blame] | 1081 | |
Ian Romanick | 6984670 | 2010-06-22 17:29:19 -0700 | [diff] [blame] | 1082 | for (unsigned i = 0; i < num_attr; i++) { |
| 1083 | /* Mask representing the contiguous slots that will be used by this |
| 1084 | * attribute. |
| 1085 | */ |
| 1086 | const unsigned use_mask = (1 << to_assign[i].slots) - 1; |
| 1087 | |
| 1088 | int location = find_available_slots(used_locations, to_assign[i].slots); |
| 1089 | |
| 1090 | if (location < 0) { |
Ian Romanick | 553dcdc | 2010-06-23 12:14:02 -0700 | [diff] [blame] | 1091 | linker_error_printf(prog, |
| 1092 | "insufficient contiguous attribute locations " |
| 1093 | "available for vertex shader input `%s'", |
| 1094 | to_assign[i].var->name); |
Ian Romanick | 6984670 | 2010-06-22 17:29:19 -0700 | [diff] [blame] | 1095 | return false; |
| 1096 | } |
| 1097 | |
| 1098 | to_assign[i].var->location = VERT_ATTRIB_GENERIC0 + location; |
| 1099 | used_locations |= (use_mask << location); |
| 1100 | } |
| 1101 | |
| 1102 | return true; |
Ian Romanick | 0ad22cd | 2010-06-21 17:18:31 -0700 | [diff] [blame] | 1103 | } |
| 1104 | |
| 1105 | |
| 1106 | void |
Eric Anholt | b706283 | 2010-07-28 13:52:23 -0700 | [diff] [blame] | 1107 | assign_varying_locations(struct gl_shader_program *prog, |
| 1108 | gl_shader *producer, gl_shader *consumer) |
Ian Romanick | 0e59b26 | 2010-06-23 11:23:01 -0700 | [diff] [blame] | 1109 | { |
| 1110 | /* FINISHME: Set dynamically when geometry shader support is added. */ |
| 1111 | unsigned output_index = VERT_RESULT_VAR0; |
| 1112 | unsigned input_index = FRAG_ATTRIB_VAR0; |
| 1113 | |
| 1114 | /* Operate in a total of three passes. |
| 1115 | * |
| 1116 | * 1. Assign locations for any matching inputs and outputs. |
| 1117 | * |
| 1118 | * 2. Mark output variables in the producer that do not have locations as |
| 1119 | * not being outputs. This lets the optimizer eliminate them. |
| 1120 | * |
| 1121 | * 3. Mark input variables in the consumer that do not have locations as |
| 1122 | * not being inputs. This lets the optimizer eliminate them. |
| 1123 | */ |
| 1124 | |
| 1125 | invalidate_variable_locations(producer, ir_var_out, VERT_RESULT_VAR0); |
| 1126 | invalidate_variable_locations(consumer, ir_var_in, FRAG_ATTRIB_VAR0); |
| 1127 | |
Eric Anholt | 16b68b1 | 2010-06-30 11:05:43 -0700 | [diff] [blame] | 1128 | foreach_list(node, producer->ir) { |
Ian Romanick | 0e59b26 | 2010-06-23 11:23:01 -0700 | [diff] [blame] | 1129 | ir_variable *const output_var = ((ir_instruction *) node)->as_variable(); |
| 1130 | |
| 1131 | if ((output_var == NULL) || (output_var->mode != ir_var_out) |
| 1132 | || (output_var->location != -1)) |
| 1133 | continue; |
| 1134 | |
| 1135 | ir_variable *const input_var = |
| 1136 | consumer->symbols->get_variable(output_var->name); |
| 1137 | |
| 1138 | if ((input_var == NULL) || (input_var->mode != ir_var_in)) |
| 1139 | continue; |
| 1140 | |
| 1141 | assert(input_var->location == -1); |
| 1142 | |
| 1143 | /* FINISHME: Location assignment will need some changes when arrays, |
| 1144 | * FINISHME: matrices, and structures are allowed as shader inputs / |
| 1145 | * FINISHME: outputs. |
| 1146 | */ |
| 1147 | output_var->location = output_index; |
| 1148 | input_var->location = input_index; |
| 1149 | |
| 1150 | output_index++; |
| 1151 | input_index++; |
| 1152 | } |
| 1153 | |
Eric Anholt | 16b68b1 | 2010-06-30 11:05:43 -0700 | [diff] [blame] | 1154 | foreach_list(node, producer->ir) { |
Ian Romanick | 0e59b26 | 2010-06-23 11:23:01 -0700 | [diff] [blame] | 1155 | ir_variable *const var = ((ir_instruction *) node)->as_variable(); |
| 1156 | |
| 1157 | if ((var == NULL) || (var->mode != ir_var_out)) |
| 1158 | continue; |
| 1159 | |
| 1160 | /* An 'out' variable is only really a shader output if its value is read |
| 1161 | * by the following stage. |
| 1162 | */ |
Eric Anholt | c10a685 | 2010-07-13 11:07:16 -0700 | [diff] [blame] | 1163 | if (var->location == -1) { |
Eric Anholt | c10a685 | 2010-07-13 11:07:16 -0700 | [diff] [blame] | 1164 | var->mode = ir_var_auto; |
| 1165 | } |
Ian Romanick | 0e59b26 | 2010-06-23 11:23:01 -0700 | [diff] [blame] | 1166 | } |
| 1167 | |
Eric Anholt | 16b68b1 | 2010-06-30 11:05:43 -0700 | [diff] [blame] | 1168 | foreach_list(node, consumer->ir) { |
Ian Romanick | 0e59b26 | 2010-06-23 11:23:01 -0700 | [diff] [blame] | 1169 | ir_variable *const var = ((ir_instruction *) node)->as_variable(); |
| 1170 | |
| 1171 | if ((var == NULL) || (var->mode != ir_var_in)) |
| 1172 | continue; |
| 1173 | |
Eric Anholt | b706283 | 2010-07-28 13:52:23 -0700 | [diff] [blame] | 1174 | if (var->location == -1) { |
| 1175 | if (prog->Version <= 120) { |
| 1176 | /* On page 25 (page 31 of the PDF) of the GLSL 1.20 spec: |
| 1177 | * |
| 1178 | * Only those varying variables used (i.e. read) in |
| 1179 | * the fragment shader executable must be written to |
| 1180 | * by the vertex shader executable; declaring |
| 1181 | * superfluous varying variables in a vertex shader is |
| 1182 | * permissible. |
| 1183 | * |
| 1184 | * We interpret this text as meaning that the VS must |
| 1185 | * write the variable for the FS to read it. See |
| 1186 | * "glsl1-varying read but not written" in piglit. |
| 1187 | */ |
| 1188 | |
| 1189 | linker_error_printf(prog, "fragment shader varying %s not written " |
| 1190 | "by vertex shader\n.", var->name); |
| 1191 | prog->LinkStatus = false; |
| 1192 | } |
| 1193 | |
| 1194 | /* An 'in' variable is only really a shader input if its |
| 1195 | * value is written by the previous stage. |
| 1196 | */ |
Eric Anholt | b706283 | 2010-07-28 13:52:23 -0700 | [diff] [blame] | 1197 | var->mode = ir_var_auto; |
| 1198 | } |
Ian Romanick | 0e59b26 | 2010-06-23 11:23:01 -0700 | [diff] [blame] | 1199 | } |
| 1200 | } |
| 1201 | |
| 1202 | |
| 1203 | void |
Eric Anholt | 849e181 | 2010-06-30 11:49:17 -0700 | [diff] [blame] | 1204 | link_shaders(struct gl_shader_program *prog) |
Ian Romanick | 832dfa5 | 2010-06-17 15:04:20 -0700 | [diff] [blame] | 1205 | { |
| 1206 | prog->LinkStatus = false; |
| 1207 | prog->Validated = false; |
| 1208 | prog->_Used = false; |
| 1209 | |
Ian Romanick | f36460e | 2010-06-23 12:07:22 -0700 | [diff] [blame] | 1210 | if (prog->InfoLog != NULL) |
| 1211 | talloc_free(prog->InfoLog); |
| 1212 | |
| 1213 | prog->InfoLog = talloc_strdup(NULL, ""); |
| 1214 | |
Ian Romanick | 832dfa5 | 2010-06-17 15:04:20 -0700 | [diff] [blame] | 1215 | /* Separate the shaders into groups based on their type. |
| 1216 | */ |
Eric Anholt | 16b68b1 | 2010-06-30 11:05:43 -0700 | [diff] [blame] | 1217 | struct gl_shader **vert_shader_list; |
Ian Romanick | 832dfa5 | 2010-06-17 15:04:20 -0700 | [diff] [blame] | 1218 | unsigned num_vert_shaders = 0; |
Eric Anholt | 16b68b1 | 2010-06-30 11:05:43 -0700 | [diff] [blame] | 1219 | struct gl_shader **frag_shader_list; |
Ian Romanick | 832dfa5 | 2010-06-17 15:04:20 -0700 | [diff] [blame] | 1220 | unsigned num_frag_shaders = 0; |
| 1221 | |
Eric Anholt | 16b68b1 | 2010-06-30 11:05:43 -0700 | [diff] [blame] | 1222 | vert_shader_list = (struct gl_shader **) |
| 1223 | calloc(2 * prog->NumShaders, sizeof(struct gl_shader *)); |
Ian Romanick | 832dfa5 | 2010-06-17 15:04:20 -0700 | [diff] [blame] | 1224 | frag_shader_list = &vert_shader_list[prog->NumShaders]; |
| 1225 | |
Ian Romanick | 25f51d3 | 2010-07-16 15:51:50 -0700 | [diff] [blame] | 1226 | unsigned min_version = UINT_MAX; |
| 1227 | unsigned max_version = 0; |
Ian Romanick | 832dfa5 | 2010-06-17 15:04:20 -0700 | [diff] [blame] | 1228 | for (unsigned i = 0; i < prog->NumShaders; i++) { |
Ian Romanick | 25f51d3 | 2010-07-16 15:51:50 -0700 | [diff] [blame] | 1229 | min_version = MIN2(min_version, prog->Shaders[i]->Version); |
| 1230 | max_version = MAX2(max_version, prog->Shaders[i]->Version); |
| 1231 | |
Ian Romanick | 832dfa5 | 2010-06-17 15:04:20 -0700 | [diff] [blame] | 1232 | switch (prog->Shaders[i]->Type) { |
| 1233 | case GL_VERTEX_SHADER: |
| 1234 | vert_shader_list[num_vert_shaders] = prog->Shaders[i]; |
| 1235 | num_vert_shaders++; |
| 1236 | break; |
| 1237 | case GL_FRAGMENT_SHADER: |
| 1238 | frag_shader_list[num_frag_shaders] = prog->Shaders[i]; |
| 1239 | num_frag_shaders++; |
| 1240 | break; |
| 1241 | case GL_GEOMETRY_SHADER: |
| 1242 | /* FINISHME: Support geometry shaders. */ |
| 1243 | assert(prog->Shaders[i]->Type != GL_GEOMETRY_SHADER); |
| 1244 | break; |
| 1245 | } |
| 1246 | } |
| 1247 | |
Ian Romanick | 25f51d3 | 2010-07-16 15:51:50 -0700 | [diff] [blame] | 1248 | /* Previous to GLSL version 1.30, different compilation units could mix and |
| 1249 | * match shading language versions. With GLSL 1.30 and later, the versions |
| 1250 | * of all shaders must match. |
| 1251 | */ |
| 1252 | assert(min_version >= 110); |
| 1253 | assert(max_version <= 130); |
| 1254 | if ((max_version >= 130) && (min_version != max_version)) { |
| 1255 | linker_error_printf(prog, "all shaders must use same shading " |
| 1256 | "language version\n"); |
| 1257 | goto done; |
| 1258 | } |
| 1259 | |
| 1260 | prog->Version = max_version; |
| 1261 | |
Ian Romanick | cd6764e | 2010-07-16 16:00:07 -0700 | [diff] [blame] | 1262 | /* Link all shaders for a particular stage and validate the result. |
| 1263 | */ |
Ian Romanick | abee16e | 2010-06-21 16:16:05 -0700 | [diff] [blame] | 1264 | prog->_NumLinkedShaders = 0; |
Ian Romanick | cc22c5a | 2010-06-18 17:13:42 -0700 | [diff] [blame] | 1265 | if (num_vert_shaders > 0) { |
Ian Romanick | 3fb8787 | 2010-07-09 14:09:34 -0700 | [diff] [blame] | 1266 | gl_shader *const sh = |
| 1267 | link_intrastage_shaders(prog, vert_shader_list, num_vert_shaders); |
| 1268 | |
| 1269 | if (sh == NULL) |
| 1270 | goto done; |
| 1271 | |
| 1272 | if (!validate_vertex_shader_executable(prog, sh)) |
| 1273 | goto done; |
| 1274 | |
| 1275 | prog->_LinkedShaders[prog->_NumLinkedShaders] = sh; |
Ian Romanick | abee16e | 2010-06-21 16:16:05 -0700 | [diff] [blame] | 1276 | prog->_NumLinkedShaders++; |
Ian Romanick | cc22c5a | 2010-06-18 17:13:42 -0700 | [diff] [blame] | 1277 | } |
| 1278 | |
| 1279 | if (num_frag_shaders > 0) { |
Ian Romanick | 3fb8787 | 2010-07-09 14:09:34 -0700 | [diff] [blame] | 1280 | gl_shader *const sh = |
| 1281 | link_intrastage_shaders(prog, frag_shader_list, num_frag_shaders); |
| 1282 | |
| 1283 | if (sh == NULL) |
| 1284 | goto done; |
| 1285 | |
| 1286 | if (!validate_fragment_shader_executable(prog, sh)) |
| 1287 | goto done; |
| 1288 | |
| 1289 | prog->_LinkedShaders[prog->_NumLinkedShaders] = sh; |
Ian Romanick | abee16e | 2010-06-21 16:16:05 -0700 | [diff] [blame] | 1290 | prog->_NumLinkedShaders++; |
Ian Romanick | cc22c5a | 2010-06-18 17:13:42 -0700 | [diff] [blame] | 1291 | } |
| 1292 | |
Ian Romanick | 3ed850e | 2010-06-23 12:18:21 -0700 | [diff] [blame] | 1293 | /* Here begins the inter-stage linking phase. Some initial validation is |
| 1294 | * performed, then locations are assigned for uniforms, attributes, and |
| 1295 | * varyings. |
| 1296 | */ |
Ian Romanick | ed1fe3d | 2010-06-23 12:09:14 -0700 | [diff] [blame] | 1297 | if (cross_validate_uniforms(prog)) { |
Ian Romanick | 3710192 | 2010-06-18 19:02:10 -0700 | [diff] [blame] | 1298 | /* Validate the inputs of each stage with the output of the preceeding |
| 1299 | * stage. |
| 1300 | */ |
Ian Romanick | abee16e | 2010-06-21 16:16:05 -0700 | [diff] [blame] | 1301 | for (unsigned i = 1; i < prog->_NumLinkedShaders; i++) { |
Ian Romanick | f36460e | 2010-06-23 12:07:22 -0700 | [diff] [blame] | 1302 | if (!cross_validate_outputs_to_inputs(prog, |
| 1303 | prog->_LinkedShaders[i - 1], |
Ian Romanick | abee16e | 2010-06-21 16:16:05 -0700 | [diff] [blame] | 1304 | prog->_LinkedShaders[i])) |
Ian Romanick | 3710192 | 2010-06-18 19:02:10 -0700 | [diff] [blame] | 1305 | goto done; |
| 1306 | } |
| 1307 | |
Ian Romanick | cc22c5a | 2010-06-18 17:13:42 -0700 | [diff] [blame] | 1308 | prog->LinkStatus = true; |
Ian Romanick | 3710192 | 2010-06-18 19:02:10 -0700 | [diff] [blame] | 1309 | } |
Ian Romanick | 832dfa5 | 2010-06-17 15:04:20 -0700 | [diff] [blame] | 1310 | |
Ian Romanick | 13e10e4 | 2010-06-21 12:03:24 -0700 | [diff] [blame] | 1311 | /* FINISHME: Perform whole-program optimization here. */ |
Ian Romanick | a7ba9a7 | 2010-07-20 13:36:32 -0700 | [diff] [blame] | 1312 | for (unsigned i = 0; i < prog->_NumLinkedShaders; i++) { |
| 1313 | /* Optimization passes */ |
| 1314 | bool progress; |
| 1315 | exec_list *ir = prog->_LinkedShaders[i]->ir; |
| 1316 | |
| 1317 | /* Lowering */ |
| 1318 | do_mat_op_to_vec(ir); |
| 1319 | do_mod_to_fract(ir); |
| 1320 | do_div_to_mul_rcp(ir); |
Eric Anholt | bc4034b | 2010-08-05 15:22:05 -0700 | [diff] [blame] | 1321 | do_explog_to_explog2(ir); |
Eric Anholt | 5854d45 | 2010-08-09 21:22:17 -0700 | [diff] [blame] | 1322 | do_sub_to_add_neg(ir); |
Ian Romanick | a7ba9a7 | 2010-07-20 13:36:32 -0700 | [diff] [blame] | 1323 | |
| 1324 | do { |
| 1325 | progress = false; |
| 1326 | |
| 1327 | progress = do_function_inlining(ir) || progress; |
Eric Anholt | 2e853ca | 2010-08-05 10:09:12 -0700 | [diff] [blame] | 1328 | progress = do_dead_functions(ir) || progress; |
Eric Anholt | 7f7eaf0 | 2010-08-05 11:01:09 -0700 | [diff] [blame] | 1329 | progress = do_structure_splitting(ir) || progress; |
Ian Romanick | a7ba9a7 | 2010-07-20 13:36:32 -0700 | [diff] [blame] | 1330 | progress = do_if_simplification(ir) || progress; |
| 1331 | progress = do_copy_propagation(ir) || progress; |
| 1332 | progress = do_dead_code_local(ir) || progress; |
Eric Anholt | 59c45e9 | 2010-07-27 14:32:21 -0700 | [diff] [blame] | 1333 | progress = do_dead_code(ir) || progress; |
Eric Anholt | 7846954 | 2010-07-30 17:04:49 -0700 | [diff] [blame] | 1334 | progress = do_tree_grafting(ir) || progress; |
Eric Anholt | 8bebbeb | 2010-08-09 17:03:46 -0700 | [diff] [blame] | 1335 | progress = do_constant_propagation(ir) || progress; |
Eric Anholt | e3a90b8 | 2010-08-04 17:12:14 -0700 | [diff] [blame] | 1336 | progress = do_constant_variable(ir) || progress; |
Ian Romanick | a7ba9a7 | 2010-07-20 13:36:32 -0700 | [diff] [blame] | 1337 | progress = do_constant_folding(ir) || progress; |
Eric Anholt | f6b03f3 | 2010-07-31 15:47:35 -0700 | [diff] [blame] | 1338 | progress = do_algebraic(ir) || progress; |
Ian Romanick | a7ba9a7 | 2010-07-20 13:36:32 -0700 | [diff] [blame] | 1339 | progress = do_if_return(ir) || progress; |
| 1340 | #if 0 |
| 1341 | if (ctx->Shader.EmitNoIfs) |
| 1342 | progress = do_if_to_cond_assign(ir) || progress; |
| 1343 | #endif |
| 1344 | |
| 1345 | progress = do_vec_index_to_swizzle(ir) || progress; |
| 1346 | /* Do this one after the previous to let the easier pass handle |
| 1347 | * constant vector indexing. |
| 1348 | */ |
| 1349 | progress = do_vec_index_to_cond_assign(ir) || progress; |
| 1350 | |
| 1351 | progress = do_swizzle_swizzle(ir) || progress; |
| 1352 | } while (progress); |
| 1353 | } |
Ian Romanick | 13e10e4 | 2010-06-21 12:03:24 -0700 | [diff] [blame] | 1354 | |
Ian Romanick | abee16e | 2010-06-21 16:16:05 -0700 | [diff] [blame] | 1355 | assign_uniform_locations(prog); |
Ian Romanick | 13e10e4 | 2010-06-21 12:03:24 -0700 | [diff] [blame] | 1356 | |
Ian Romanick | 0ad22cd | 2010-06-21 17:18:31 -0700 | [diff] [blame] | 1357 | if (prog->_LinkedShaders[0]->Type == GL_VERTEX_SHADER) |
Ian Romanick | 9342d26 | 2010-06-22 17:41:37 -0700 | [diff] [blame] | 1358 | /* FINISHME: The value of the max_attribute_index parameter is |
| 1359 | * FINISHME: implementation dependent based on the value of |
| 1360 | * FINISHME: GL_MAX_VERTEX_ATTRIBS. GL_MAX_VERTEX_ATTRIBS must be |
| 1361 | * FINISHME: at least 16, so hardcode 16 for now. |
| 1362 | */ |
Ian Romanick | 553dcdc | 2010-06-23 12:14:02 -0700 | [diff] [blame] | 1363 | if (!assign_attribute_locations(prog, 16)) |
Ian Romanick | 6984670 | 2010-06-22 17:29:19 -0700 | [diff] [blame] | 1364 | goto done; |
Ian Romanick | 13e10e4 | 2010-06-21 12:03:24 -0700 | [diff] [blame] | 1365 | |
Ian Romanick | 0e59b26 | 2010-06-23 11:23:01 -0700 | [diff] [blame] | 1366 | for (unsigned i = 1; i < prog->_NumLinkedShaders; i++) |
Eric Anholt | b706283 | 2010-07-28 13:52:23 -0700 | [diff] [blame] | 1367 | assign_varying_locations(prog, |
| 1368 | prog->_LinkedShaders[i - 1], |
Ian Romanick | 0e59b26 | 2010-06-23 11:23:01 -0700 | [diff] [blame] | 1369 | prog->_LinkedShaders[i]); |
Ian Romanick | 13e10e4 | 2010-06-21 12:03:24 -0700 | [diff] [blame] | 1370 | |
| 1371 | /* FINISHME: Assign fragment shader output locations. */ |
| 1372 | |
Ian Romanick | 832dfa5 | 2010-06-17 15:04:20 -0700 | [diff] [blame] | 1373 | done: |
| 1374 | free(vert_shader_list); |
| 1375 | } |