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