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