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