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