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 |
Ian Romanick | f6ee7bc | 2011-10-11 16:15:47 -0700 | [diff] [blame] | 194 | link_invalidate_variable_locations(gl_shader *sh, enum ir_variable_mode mode, |
| 195 | int generic_base) |
Ian Romanick | 0ad22cd | 2010-06-21 17:18:31 -0700 | [diff] [blame] | 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 | * |
Paul Berry | 642e5b41 | 2012-01-04 13:57:52 -0800 | [diff] [blame] | 249 | * Also sets prog->Vert.UsesClipDistance and prog->Vert.ClipDistanceArraySize |
| 250 | * as a side effect. |
Ian Romanick | c93b8f1 | 2010-06-17 15:20:22 -0700 | [diff] [blame] | 251 | * |
| 252 | * \param shader Vertex shader executable to be verified |
| 253 | */ |
Ian Romanick | 832dfa5 | 2010-06-17 15:04:20 -0700 | [diff] [blame] | 254 | bool |
Eric Anholt | 849e181 | 2010-06-30 11:49:17 -0700 | [diff] [blame] | 255 | validate_vertex_shader_executable(struct gl_shader_program *prog, |
Eric Anholt | 16b68b1 | 2010-06-30 11:05:43 -0700 | [diff] [blame] | 256 | struct gl_shader *shader) |
Ian Romanick | 832dfa5 | 2010-06-17 15:04:20 -0700 | [diff] [blame] | 257 | { |
| 258 | if (shader == NULL) |
| 259 | return true; |
| 260 | |
Ian Romanick | 832dfa5 | 2010-06-17 15:04:20 -0700 | [diff] [blame] | 261 | find_assignment_visitor find("gl_Position"); |
Eric Anholt | 16b68b1 | 2010-06-30 11:05:43 -0700 | [diff] [blame] | 262 | find.run(shader->ir); |
Ian Romanick | 832dfa5 | 2010-06-17 15:04:20 -0700 | [diff] [blame] | 263 | if (!find.variable_found()) { |
Ian Romanick | 586e741 | 2011-07-28 14:04:09 -0700 | [diff] [blame] | 264 | linker_error(prog, "vertex shader does not write to `gl_Position'\n"); |
Ian Romanick | 832dfa5 | 2010-06-17 15:04:20 -0700 | [diff] [blame] | 265 | return false; |
| 266 | } |
| 267 | |
Paul Berry | 642e5b41 | 2012-01-04 13:57:52 -0800 | [diff] [blame] | 268 | prog->Vert.ClipDistanceArraySize = 0; |
| 269 | |
Paul Berry | b453ba2 | 2011-08-11 18:10:22 -0700 | [diff] [blame] | 270 | if (prog->Version >= 130) { |
| 271 | /* From section 7.1 (Vertex Shader Special Variables) of the |
| 272 | * GLSL 1.30 spec: |
| 273 | * |
| 274 | * "It is an error for a shader to statically write both |
| 275 | * gl_ClipVertex and gl_ClipDistance." |
| 276 | */ |
| 277 | find_assignment_visitor clip_vertex("gl_ClipVertex"); |
| 278 | find_assignment_visitor clip_distance("gl_ClipDistance"); |
| 279 | |
| 280 | clip_vertex.run(shader->ir); |
| 281 | clip_distance.run(shader->ir); |
| 282 | if (clip_vertex.variable_found() && clip_distance.variable_found()) { |
| 283 | linker_error(prog, "vertex shader writes to both `gl_ClipVertex' " |
| 284 | "and `gl_ClipDistance'\n"); |
| 285 | return false; |
| 286 | } |
Paul Berry | 1ad54ae | 2011-09-17 09:42:02 -0700 | [diff] [blame] | 287 | prog->Vert.UsesClipDistance = clip_distance.variable_found(); |
Paul Berry | 642e5b41 | 2012-01-04 13:57:52 -0800 | [diff] [blame] | 288 | ir_variable *clip_distance_var = |
| 289 | shader->symbols->get_variable("gl_ClipDistance"); |
| 290 | if (clip_distance_var) |
| 291 | prog->Vert.ClipDistanceArraySize = clip_distance_var->type->length; |
Paul Berry | b453ba2 | 2011-08-11 18:10:22 -0700 | [diff] [blame] | 292 | } |
| 293 | |
Ian Romanick | 832dfa5 | 2010-06-17 15:04:20 -0700 | [diff] [blame] | 294 | return true; |
| 295 | } |
| 296 | |
| 297 | |
Ian Romanick | c93b8f1 | 2010-06-17 15:20:22 -0700 | [diff] [blame] | 298 | /** |
| 299 | * Verify that a fragment shader executable meets all semantic requirements |
| 300 | * |
| 301 | * \param shader Fragment shader executable to be verified |
| 302 | */ |
Ian Romanick | 832dfa5 | 2010-06-17 15:04:20 -0700 | [diff] [blame] | 303 | bool |
Eric Anholt | 849e181 | 2010-06-30 11:49:17 -0700 | [diff] [blame] | 304 | validate_fragment_shader_executable(struct gl_shader_program *prog, |
Eric Anholt | 16b68b1 | 2010-06-30 11:05:43 -0700 | [diff] [blame] | 305 | struct gl_shader *shader) |
Ian Romanick | 832dfa5 | 2010-06-17 15:04:20 -0700 | [diff] [blame] | 306 | { |
| 307 | if (shader == NULL) |
| 308 | return true; |
| 309 | |
Ian Romanick | 832dfa5 | 2010-06-17 15:04:20 -0700 | [diff] [blame] | 310 | find_assignment_visitor frag_color("gl_FragColor"); |
| 311 | find_assignment_visitor frag_data("gl_FragData"); |
| 312 | |
Eric Anholt | 16b68b1 | 2010-06-30 11:05:43 -0700 | [diff] [blame] | 313 | frag_color.run(shader->ir); |
| 314 | frag_data.run(shader->ir); |
Ian Romanick | 832dfa5 | 2010-06-17 15:04:20 -0700 | [diff] [blame] | 315 | |
Ian Romanick | 832dfa5 | 2010-06-17 15:04:20 -0700 | [diff] [blame] | 316 | if (frag_color.variable_found() && frag_data.variable_found()) { |
Ian Romanick | 586e741 | 2011-07-28 14:04:09 -0700 | [diff] [blame] | 317 | linker_error(prog, "fragment shader writes to both " |
| 318 | "`gl_FragColor' and `gl_FragData'\n"); |
Ian Romanick | 832dfa5 | 2010-06-17 15:04:20 -0700 | [diff] [blame] | 319 | return false; |
| 320 | } |
| 321 | |
| 322 | return true; |
| 323 | } |
| 324 | |
| 325 | |
Ian Romanick | cc22c5a | 2010-06-18 17:13:42 -0700 | [diff] [blame] | 326 | /** |
Ian Romanick | e2e5d0d | 2010-06-29 18:47:11 -0700 | [diff] [blame] | 327 | * Generate a string describing the mode of a variable |
| 328 | */ |
| 329 | static const char * |
| 330 | mode_string(const ir_variable *var) |
| 331 | { |
| 332 | switch (var->mode) { |
| 333 | case ir_var_auto: |
| 334 | return (var->read_only) ? "global constant" : "global variable"; |
| 335 | |
| 336 | case ir_var_uniform: return "uniform"; |
| 337 | case ir_var_in: return "shader input"; |
| 338 | case ir_var_out: return "shader output"; |
| 339 | case ir_var_inout: return "shader inout"; |
Ian Romanick | 7e2aa91 | 2010-07-19 17:12:42 -0700 | [diff] [blame] | 340 | |
Kenneth Graunke | 819d57f | 2011-01-12 15:37:37 -0800 | [diff] [blame] | 341 | case ir_var_const_in: |
Ian Romanick | 7e2aa91 | 2010-07-19 17:12:42 -0700 | [diff] [blame] | 342 | case ir_var_temporary: |
Ian Romanick | e2e5d0d | 2010-06-29 18:47:11 -0700 | [diff] [blame] | 343 | default: |
| 344 | assert(!"Should not get here."); |
| 345 | return "invalid variable"; |
| 346 | } |
| 347 | } |
| 348 | |
| 349 | |
| 350 | /** |
| 351 | * Perform validation of global variables used across multiple shaders |
Ian Romanick | cc22c5a | 2010-06-18 17:13:42 -0700 | [diff] [blame] | 352 | */ |
| 353 | bool |
Ian Romanick | e2e5d0d | 2010-06-29 18:47:11 -0700 | [diff] [blame] | 354 | cross_validate_globals(struct gl_shader_program *prog, |
| 355 | struct gl_shader **shader_list, |
| 356 | unsigned num_shaders, |
| 357 | bool uniforms_only) |
Ian Romanick | cc22c5a | 2010-06-18 17:13:42 -0700 | [diff] [blame] | 358 | { |
| 359 | /* Examine all of the uniforms in all of the shaders and cross validate |
| 360 | * them. |
| 361 | */ |
Ian Romanick | e2e5d0d | 2010-06-29 18:47:11 -0700 | [diff] [blame] | 362 | glsl_symbol_table variables; |
| 363 | for (unsigned i = 0; i < num_shaders; i++) { |
Ian Romanick | 3322fba | 2010-10-14 13:28:42 -0700 | [diff] [blame] | 364 | if (shader_list[i] == NULL) |
| 365 | continue; |
| 366 | |
Ian Romanick | e2e5d0d | 2010-06-29 18:47:11 -0700 | [diff] [blame] | 367 | foreach_list(node, shader_list[i]->ir) { |
Ian Romanick | cc22c5a | 2010-06-18 17:13:42 -0700 | [diff] [blame] | 368 | ir_variable *const var = ((ir_instruction *) node)->as_variable(); |
| 369 | |
Ian Romanick | e2e5d0d | 2010-06-29 18:47:11 -0700 | [diff] [blame] | 370 | if (var == NULL) |
Ian Romanick | cc22c5a | 2010-06-18 17:13:42 -0700 | [diff] [blame] | 371 | continue; |
| 372 | |
Ian Romanick | e2e5d0d | 2010-06-29 18:47:11 -0700 | [diff] [blame] | 373 | if (uniforms_only && (var->mode != ir_var_uniform)) |
| 374 | continue; |
| 375 | |
Ian Romanick | 7e2aa91 | 2010-07-19 17:12:42 -0700 | [diff] [blame] | 376 | /* Don't cross validate temporaries that are at global scope. These |
| 377 | * will eventually get pulled into the shaders 'main'. |
| 378 | */ |
| 379 | if (var->mode == ir_var_temporary) |
| 380 | continue; |
| 381 | |
Ian Romanick | e2e5d0d | 2010-06-29 18:47:11 -0700 | [diff] [blame] | 382 | /* If a global with this name has already been seen, verify that the |
| 383 | * new instance has the same type. In addition, if the globals have |
Ian Romanick | cc22c5a | 2010-06-18 17:13:42 -0700 | [diff] [blame] | 384 | * initializers, the values of the initializers must be the same. |
| 385 | */ |
Ian Romanick | e2e5d0d | 2010-06-29 18:47:11 -0700 | [diff] [blame] | 386 | ir_variable *const existing = variables.get_variable(var->name); |
Ian Romanick | cc22c5a | 2010-06-18 17:13:42 -0700 | [diff] [blame] | 387 | if (existing != NULL) { |
| 388 | if (var->type != existing->type) { |
Ian Romanick | a2711d6 | 2010-08-29 22:07:49 -0700 | [diff] [blame] | 389 | /* Consider the types to be "the same" if both types are arrays |
| 390 | * of the same type and one of the arrays is implicitly sized. |
| 391 | * In addition, set the type of the linked variable to the |
| 392 | * explicitly sized array. |
| 393 | */ |
| 394 | if (var->type->is_array() |
| 395 | && existing->type->is_array() |
| 396 | && (var->type->fields.array == existing->type->fields.array) |
| 397 | && ((var->type->length == 0) |
| 398 | || (existing->type->length == 0))) { |
Ian Romanick | 0f4b2a0 | 2011-01-25 12:06:18 -0800 | [diff] [blame] | 399 | if (var->type->length != 0) { |
Ian Romanick | a2711d6 | 2010-08-29 22:07:49 -0700 | [diff] [blame] | 400 | existing->type = var->type; |
Ian Romanick | 6f53921 | 2010-12-07 18:30:33 -0800 | [diff] [blame] | 401 | } |
Ian Romanick | a2711d6 | 2010-08-29 22:07:49 -0700 | [diff] [blame] | 402 | } else { |
Ian Romanick | 586e741 | 2011-07-28 14:04:09 -0700 | [diff] [blame] | 403 | linker_error(prog, "%s `%s' declared as type " |
| 404 | "`%s' and type `%s'\n", |
| 405 | mode_string(var), |
| 406 | var->name, var->type->name, |
| 407 | existing->type->name); |
Ian Romanick | a2711d6 | 2010-08-29 22:07:49 -0700 | [diff] [blame] | 408 | return false; |
| 409 | } |
Ian Romanick | cc22c5a | 2010-06-18 17:13:42 -0700 | [diff] [blame] | 410 | } |
| 411 | |
Ian Romanick | 68a4fc9 | 2010-10-07 17:21:22 -0700 | [diff] [blame] | 412 | if (var->explicit_location) { |
| 413 | if (existing->explicit_location |
| 414 | && (var->location != existing->location)) { |
Ian Romanick | 586e741 | 2011-07-28 14:04:09 -0700 | [diff] [blame] | 415 | linker_error(prog, "explicit locations for %s " |
| 416 | "`%s' have differing values\n", |
| 417 | mode_string(var), var->name); |
Ian Romanick | 68a4fc9 | 2010-10-07 17:21:22 -0700 | [diff] [blame] | 418 | return false; |
| 419 | } |
| 420 | |
| 421 | existing->location = var->location; |
| 422 | existing->explicit_location = true; |
| 423 | } |
| 424 | |
Ian Romanick | 46173f9 | 2011-10-31 13:07:06 -0700 | [diff] [blame] | 425 | /* Validate layout qualifiers for gl_FragDepth. |
| 426 | * |
| 427 | * From the AMD/ARB_conservative_depth specs: |
| 428 | * |
| 429 | * "If gl_FragDepth is redeclared in any fragment shader in a |
| 430 | * program, it must be redeclared in all fragment shaders in |
| 431 | * that program that have static assignments to |
| 432 | * gl_FragDepth. All redeclarations of gl_FragDepth in all |
| 433 | * fragment shaders in a single program must have the same set |
| 434 | * of qualifiers." |
| 435 | */ |
| 436 | if (strcmp(var->name, "gl_FragDepth") == 0) { |
| 437 | bool layout_declared = var->depth_layout != ir_depth_layout_none; |
| 438 | bool layout_differs = |
| 439 | var->depth_layout != existing->depth_layout; |
| 440 | |
| 441 | if (layout_declared && layout_differs) { |
| 442 | linker_error(prog, |
| 443 | "All redeclarations of gl_FragDepth in all " |
| 444 | "fragment shaders in a single program must have " |
| 445 | "the same set of qualifiers."); |
| 446 | } |
| 447 | |
| 448 | if (var->used && layout_differs) { |
| 449 | linker_error(prog, |
| 450 | "If gl_FragDepth is redeclared with a layout " |
| 451 | "qualifier in any fragment shader, it must be " |
| 452 | "redeclared with the same layout qualifier in " |
| 453 | "all fragment shaders that have assignments to " |
| 454 | "gl_FragDepth"); |
| 455 | } |
| 456 | } |
Chad Versace | addae33 | 2011-01-27 01:40:31 -0800 | [diff] [blame] | 457 | |
Ian Romanick | f37b1ad | 2011-10-31 14:31:07 -0700 | [diff] [blame] | 458 | /* Page 35 (page 41 of the PDF) of the GLSL 4.20 spec says: |
| 459 | * |
| 460 | * "If a shared global has multiple initializers, the |
| 461 | * initializers must all be constant expressions, and they |
| 462 | * must all have the same value. Otherwise, a link error will |
| 463 | * result. (A shared global having only one initializer does |
| 464 | * not require that initializer to be a constant expression.)" |
| 465 | * |
| 466 | * Previous to 4.20 the GLSL spec simply said that initializers |
| 467 | * must have the same value. In this case of non-constant |
| 468 | * initializers, this was impossible to determine. As a result, |
| 469 | * no vendor actually implemented that behavior. The 4.20 |
| 470 | * behavior matches the implemented behavior of at least one other |
| 471 | * vendor, so we'll implement that for all GLSL versions. |
Ian Romanick | e2e5d0d | 2010-06-29 18:47:11 -0700 | [diff] [blame] | 472 | */ |
Ian Romanick | f37b1ad | 2011-10-31 14:31:07 -0700 | [diff] [blame] | 473 | if (var->constant_initializer != NULL) { |
| 474 | if (existing->constant_initializer != NULL) { |
| 475 | if (!var->constant_initializer->has_value(existing->constant_initializer)) { |
Ian Romanick | 586e741 | 2011-07-28 14:04:09 -0700 | [diff] [blame] | 476 | linker_error(prog, "initializers for %s " |
| 477 | "`%s' have differing values\n", |
| 478 | mode_string(var), var->name); |
Ian Romanick | cc22c5a | 2010-06-18 17:13:42 -0700 | [diff] [blame] | 479 | return false; |
| 480 | } |
Ian Romanick | f37b1ad | 2011-10-31 14:31:07 -0700 | [diff] [blame] | 481 | } else { |
Ian Romanick | cc22c5a | 2010-06-18 17:13:42 -0700 | [diff] [blame] | 482 | /* If the first-seen instance of a particular uniform did not |
| 483 | * have an initializer but a later instance does, copy the |
| 484 | * initializer to the version stored in the symbol table. |
| 485 | */ |
Ian Romanick | de415b7 | 2010-07-14 13:22:12 -0700 | [diff] [blame] | 486 | /* FINISHME: This is wrong. The constant_value field should |
| 487 | * FINISHME: not be modified! Imagine a case where a shader |
| 488 | * FINISHME: without an initializer is linked in two different |
| 489 | * FINISHME: programs with shaders that have differing |
| 490 | * FINISHME: initializers. Linking with the first will |
| 491 | * FINISHME: modify the shader, and linking with the second |
| 492 | * FINISHME: will fail. |
| 493 | */ |
Ian Romanick | f37b1ad | 2011-10-31 14:31:07 -0700 | [diff] [blame] | 494 | existing->constant_initializer = |
| 495 | var->constant_initializer->clone(ralloc_parent(existing), |
| 496 | NULL); |
| 497 | } |
| 498 | } |
| 499 | |
| 500 | if (var->has_initializer) { |
| 501 | if (existing->has_initializer |
| 502 | && (var->constant_initializer == NULL |
| 503 | || existing->constant_initializer == NULL)) { |
| 504 | linker_error(prog, |
| 505 | "shared global variable `%s' has multiple " |
| 506 | "non-constant initializers.\n", |
| 507 | var->name); |
| 508 | return false; |
| 509 | } |
| 510 | |
| 511 | /* Some instance had an initializer, so keep track of that. In |
| 512 | * this location, all sorts of initializers (constant or |
| 513 | * otherwise) will propagate the existence to the variable |
| 514 | * stored in the symbol table. |
| 515 | */ |
| 516 | existing->has_initializer = true; |
Ian Romanick | cc22c5a | 2010-06-18 17:13:42 -0700 | [diff] [blame] | 517 | } |
Chad Versace | 7528f14 | 2010-11-17 14:34:38 -0800 | [diff] [blame] | 518 | |
| 519 | if (existing->invariant != var->invariant) { |
Ian Romanick | 586e741 | 2011-07-28 14:04:09 -0700 | [diff] [blame] | 520 | linker_error(prog, "declarations for %s `%s' have " |
| 521 | "mismatching invariant qualifiers\n", |
| 522 | mode_string(var), var->name); |
Chad Versace | 7528f14 | 2010-11-17 14:34:38 -0800 | [diff] [blame] | 523 | return false; |
| 524 | } |
Chad Versace | 61428dd | 2011-01-10 15:29:30 -0800 | [diff] [blame] | 525 | if (existing->centroid != var->centroid) { |
Ian Romanick | 586e741 | 2011-07-28 14:04:09 -0700 | [diff] [blame] | 526 | linker_error(prog, "declarations for %s `%s' have " |
| 527 | "mismatching centroid qualifiers\n", |
| 528 | mode_string(var), var->name); |
Chad Versace | 61428dd | 2011-01-10 15:29:30 -0800 | [diff] [blame] | 529 | return false; |
| 530 | } |
Ian Romanick | cc22c5a | 2010-06-18 17:13:42 -0700 | [diff] [blame] | 531 | } else |
Eric Anholt | 001eee5 | 2010-11-05 06:11:24 -0700 | [diff] [blame] | 532 | variables.add_variable(var); |
Ian Romanick | cc22c5a | 2010-06-18 17:13:42 -0700 | [diff] [blame] | 533 | } |
| 534 | } |
| 535 | |
| 536 | return true; |
| 537 | } |
| 538 | |
| 539 | |
Ian Romanick | 3710192 | 2010-06-18 19:02:10 -0700 | [diff] [blame] | 540 | /** |
Ian Romanick | e2e5d0d | 2010-06-29 18:47:11 -0700 | [diff] [blame] | 541 | * Perform validation of uniforms used across multiple shader stages |
| 542 | */ |
| 543 | bool |
| 544 | cross_validate_uniforms(struct gl_shader_program *prog) |
| 545 | { |
| 546 | return cross_validate_globals(prog, prog->_LinkedShaders, |
Ian Romanick | 3322fba | 2010-10-14 13:28:42 -0700 | [diff] [blame] | 547 | MESA_SHADER_TYPES, true); |
Ian Romanick | e2e5d0d | 2010-06-29 18:47:11 -0700 | [diff] [blame] | 548 | } |
| 549 | |
| 550 | |
| 551 | /** |
Ian Romanick | 3710192 | 2010-06-18 19:02:10 -0700 | [diff] [blame] | 552 | * Validate that outputs from one stage match inputs of another |
| 553 | */ |
| 554 | bool |
Eric Anholt | 849e181 | 2010-06-30 11:49:17 -0700 | [diff] [blame] | 555 | cross_validate_outputs_to_inputs(struct gl_shader_program *prog, |
Eric Anholt | 16b68b1 | 2010-06-30 11:05:43 -0700 | [diff] [blame] | 556 | gl_shader *producer, gl_shader *consumer) |
Ian Romanick | 3710192 | 2010-06-18 19:02:10 -0700 | [diff] [blame] | 557 | { |
| 558 | glsl_symbol_table parameters; |
| 559 | /* FINISHME: Figure these out dynamically. */ |
| 560 | const char *const producer_stage = "vertex"; |
| 561 | const char *const consumer_stage = "fragment"; |
| 562 | |
| 563 | /* Find all shader outputs in the "producer" stage. |
| 564 | */ |
Eric Anholt | 16b68b1 | 2010-06-30 11:05:43 -0700 | [diff] [blame] | 565 | foreach_list(node, producer->ir) { |
Ian Romanick | 3710192 | 2010-06-18 19:02:10 -0700 | [diff] [blame] | 566 | ir_variable *const var = ((ir_instruction *) node)->as_variable(); |
| 567 | |
| 568 | /* FINISHME: For geometry shaders, this should also look for inout |
| 569 | * FINISHME: variables. |
| 570 | */ |
| 571 | if ((var == NULL) || (var->mode != ir_var_out)) |
| 572 | continue; |
| 573 | |
Eric Anholt | 001eee5 | 2010-11-05 06:11:24 -0700 | [diff] [blame] | 574 | parameters.add_variable(var); |
Ian Romanick | 3710192 | 2010-06-18 19:02:10 -0700 | [diff] [blame] | 575 | } |
| 576 | |
| 577 | |
| 578 | /* Find all shader inputs in the "consumer" stage. Any variables that have |
| 579 | * matching outputs already in the symbol table must have the same type and |
| 580 | * qualifiers. |
| 581 | */ |
Eric Anholt | 16b68b1 | 2010-06-30 11:05:43 -0700 | [diff] [blame] | 582 | foreach_list(node, consumer->ir) { |
Ian Romanick | 3710192 | 2010-06-18 19:02:10 -0700 | [diff] [blame] | 583 | ir_variable *const input = ((ir_instruction *) node)->as_variable(); |
| 584 | |
| 585 | /* FINISHME: For geometry shaders, this should also look for inout |
| 586 | * FINISHME: variables. |
| 587 | */ |
| 588 | if ((input == NULL) || (input->mode != ir_var_in)) |
| 589 | continue; |
| 590 | |
| 591 | ir_variable *const output = parameters.get_variable(input->name); |
| 592 | if (output != NULL) { |
| 593 | /* Check that the types match between stages. |
| 594 | */ |
| 595 | if (input->type != output->type) { |
Ian Romanick | cb2b547 | 2010-12-13 15:16:39 -0800 | [diff] [blame] | 596 | /* There is a bit of a special case for gl_TexCoord. This |
Bryan Cain | f18a086 | 2011-04-23 19:29:15 -0500 | [diff] [blame] | 597 | * built-in is unsized by default. Applications that variable |
Ian Romanick | cb2b547 | 2010-12-13 15:16:39 -0800 | [diff] [blame] | 598 | * access it must redeclare it with a size. There is some |
| 599 | * language in the GLSL spec that implies the fragment shader |
| 600 | * and vertex shader do not have to agree on this size. Other |
| 601 | * driver behave this way, and one or two applications seem to |
| 602 | * rely on it. |
| 603 | * |
| 604 | * Neither declaration needs to be modified here because the array |
| 605 | * sizes are fixed later when update_array_sizes is called. |
| 606 | * |
| 607 | * From page 48 (page 54 of the PDF) of the GLSL 1.10 spec: |
| 608 | * |
| 609 | * "Unlike user-defined varying variables, the built-in |
| 610 | * varying variables don't have a strict one-to-one |
| 611 | * correspondence between the vertex language and the |
| 612 | * fragment language." |
| 613 | */ |
| 614 | if (!output->type->is_array() |
| 615 | || (strncmp("gl_", output->name, 3) != 0)) { |
Ian Romanick | 586e741 | 2011-07-28 14:04:09 -0700 | [diff] [blame] | 616 | linker_error(prog, |
| 617 | "%s shader output `%s' declared as type `%s', " |
| 618 | "but %s shader input declared as type `%s'\n", |
| 619 | producer_stage, output->name, |
| 620 | output->type->name, |
| 621 | consumer_stage, input->type->name); |
Ian Romanick | cb2b547 | 2010-12-13 15:16:39 -0800 | [diff] [blame] | 622 | return false; |
| 623 | } |
Ian Romanick | 3710192 | 2010-06-18 19:02:10 -0700 | [diff] [blame] | 624 | } |
| 625 | |
| 626 | /* Check that all of the qualifiers match between stages. |
| 627 | */ |
| 628 | if (input->centroid != output->centroid) { |
Ian Romanick | 586e741 | 2011-07-28 14:04:09 -0700 | [diff] [blame] | 629 | linker_error(prog, |
| 630 | "%s shader output `%s' %s centroid qualifier, " |
| 631 | "but %s shader input %s centroid qualifier\n", |
| 632 | producer_stage, |
| 633 | output->name, |
| 634 | (output->centroid) ? "has" : "lacks", |
| 635 | consumer_stage, |
| 636 | (input->centroid) ? "has" : "lacks"); |
Ian Romanick | 3710192 | 2010-06-18 19:02:10 -0700 | [diff] [blame] | 637 | return false; |
| 638 | } |
| 639 | |
| 640 | if (input->invariant != output->invariant) { |
Ian Romanick | 586e741 | 2011-07-28 14:04:09 -0700 | [diff] [blame] | 641 | linker_error(prog, |
| 642 | "%s shader output `%s' %s invariant qualifier, " |
| 643 | "but %s shader input %s invariant qualifier\n", |
| 644 | producer_stage, |
| 645 | output->name, |
| 646 | (output->invariant) ? "has" : "lacks", |
| 647 | consumer_stage, |
| 648 | (input->invariant) ? "has" : "lacks"); |
Ian Romanick | 3710192 | 2010-06-18 19:02:10 -0700 | [diff] [blame] | 649 | return false; |
| 650 | } |
| 651 | |
| 652 | if (input->interpolation != output->interpolation) { |
Ian Romanick | 586e741 | 2011-07-28 14:04:09 -0700 | [diff] [blame] | 653 | linker_error(prog, |
| 654 | "%s shader output `%s' specifies %s " |
| 655 | "interpolation qualifier, " |
| 656 | "but %s shader input specifies %s " |
| 657 | "interpolation qualifier\n", |
| 658 | producer_stage, |
| 659 | output->name, |
| 660 | output->interpolation_string(), |
| 661 | consumer_stage, |
| 662 | input->interpolation_string()); |
Ian Romanick | 3710192 | 2010-06-18 19:02:10 -0700 | [diff] [blame] | 663 | return false; |
| 664 | } |
| 665 | } |
| 666 | } |
| 667 | |
| 668 | return true; |
| 669 | } |
| 670 | |
| 671 | |
Ian Romanick | 3fb8787 | 2010-07-09 14:09:34 -0700 | [diff] [blame] | 672 | /** |
| 673 | * Populates a shaders symbol table with all global declarations |
| 674 | */ |
| 675 | static void |
| 676 | populate_symbol_table(gl_shader *sh) |
| 677 | { |
| 678 | sh->symbols = new(sh) glsl_symbol_table; |
| 679 | |
| 680 | foreach_list(node, sh->ir) { |
| 681 | ir_instruction *const inst = (ir_instruction *) node; |
| 682 | ir_variable *var; |
| 683 | ir_function *func; |
| 684 | |
| 685 | if ((func = inst->as_function()) != NULL) { |
Eric Anholt | e8f5ebf | 2010-11-05 06:08:45 -0700 | [diff] [blame] | 686 | sh->symbols->add_function(func); |
Ian Romanick | 3fb8787 | 2010-07-09 14:09:34 -0700 | [diff] [blame] | 687 | } else if ((var = inst->as_variable()) != NULL) { |
Eric Anholt | 001eee5 | 2010-11-05 06:11:24 -0700 | [diff] [blame] | 688 | sh->symbols->add_variable(var); |
Ian Romanick | 3fb8787 | 2010-07-09 14:09:34 -0700 | [diff] [blame] | 689 | } |
| 690 | } |
| 691 | } |
| 692 | |
| 693 | |
| 694 | /** |
Ian Romanick | 31a9786 | 2010-07-12 18:48:50 -0700 | [diff] [blame] | 695 | * Remap variables referenced in an instruction tree |
| 696 | * |
| 697 | * This is used when instruction trees are cloned from one shader and placed in |
| 698 | * another. These trees will contain references to \c ir_variable nodes that |
| 699 | * do not exist in the target shader. This function finds these \c ir_variable |
| 700 | * references and replaces the references with matching variables in the target |
| 701 | * shader. |
| 702 | * |
| 703 | * If there is no matching variable in the target shader, a clone of the |
| 704 | * \c ir_variable is made and added to the target shader. The new variable is |
| 705 | * added to \b both the instruction stream and the symbol table. |
| 706 | * |
| 707 | * \param inst IR tree that is to be processed. |
| 708 | * \param symbols Symbol table containing global scope symbols in the |
| 709 | * linked shader. |
| 710 | * \param instructions Instruction stream where new variable declarations |
| 711 | * should be added. |
| 712 | */ |
| 713 | void |
Eric Anholt | 8273bd4 | 2010-08-04 12:34:56 -0700 | [diff] [blame] | 714 | remap_variables(ir_instruction *inst, struct gl_shader *target, |
| 715 | hash_table *temps) |
Ian Romanick | 31a9786 | 2010-07-12 18:48:50 -0700 | [diff] [blame] | 716 | { |
| 717 | class remap_visitor : public ir_hierarchical_visitor { |
| 718 | public: |
Eric Anholt | 8273bd4 | 2010-08-04 12:34:56 -0700 | [diff] [blame] | 719 | remap_visitor(struct gl_shader *target, |
Ian Romanick | 7e2aa91 | 2010-07-19 17:12:42 -0700 | [diff] [blame] | 720 | hash_table *temps) |
Ian Romanick | 31a9786 | 2010-07-12 18:48:50 -0700 | [diff] [blame] | 721 | { |
Eric Anholt | 8273bd4 | 2010-08-04 12:34:56 -0700 | [diff] [blame] | 722 | this->target = target; |
| 723 | this->symbols = target->symbols; |
| 724 | this->instructions = target->ir; |
Ian Romanick | 7e2aa91 | 2010-07-19 17:12:42 -0700 | [diff] [blame] | 725 | this->temps = temps; |
Ian Romanick | 31a9786 | 2010-07-12 18:48:50 -0700 | [diff] [blame] | 726 | } |
| 727 | |
| 728 | virtual ir_visitor_status visit(ir_dereference_variable *ir) |
| 729 | { |
Ian Romanick | 7e2aa91 | 2010-07-19 17:12:42 -0700 | [diff] [blame] | 730 | if (ir->var->mode == ir_var_temporary) { |
| 731 | ir_variable *var = (ir_variable *) hash_table_find(temps, ir->var); |
| 732 | |
| 733 | assert(var != NULL); |
| 734 | ir->var = var; |
| 735 | return visit_continue; |
| 736 | } |
| 737 | |
Ian Romanick | 31a9786 | 2010-07-12 18:48:50 -0700 | [diff] [blame] | 738 | ir_variable *const existing = |
| 739 | this->symbols->get_variable(ir->var->name); |
| 740 | if (existing != NULL) |
| 741 | ir->var = existing; |
| 742 | else { |
Eric Anholt | 8273bd4 | 2010-08-04 12:34:56 -0700 | [diff] [blame] | 743 | ir_variable *copy = ir->var->clone(this->target, NULL); |
Ian Romanick | 31a9786 | 2010-07-12 18:48:50 -0700 | [diff] [blame] | 744 | |
Eric Anholt | 001eee5 | 2010-11-05 06:11:24 -0700 | [diff] [blame] | 745 | this->symbols->add_variable(copy); |
Ian Romanick | 31a9786 | 2010-07-12 18:48:50 -0700 | [diff] [blame] | 746 | this->instructions->push_head(copy); |
Ian Romanick | 7e2aa91 | 2010-07-19 17:12:42 -0700 | [diff] [blame] | 747 | ir->var = copy; |
Ian Romanick | 31a9786 | 2010-07-12 18:48:50 -0700 | [diff] [blame] | 748 | } |
| 749 | |
| 750 | return visit_continue; |
| 751 | } |
| 752 | |
| 753 | private: |
Eric Anholt | 8273bd4 | 2010-08-04 12:34:56 -0700 | [diff] [blame] | 754 | struct gl_shader *target; |
Ian Romanick | 31a9786 | 2010-07-12 18:48:50 -0700 | [diff] [blame] | 755 | glsl_symbol_table *symbols; |
| 756 | exec_list *instructions; |
Ian Romanick | 7e2aa91 | 2010-07-19 17:12:42 -0700 | [diff] [blame] | 757 | hash_table *temps; |
Ian Romanick | 31a9786 | 2010-07-12 18:48:50 -0700 | [diff] [blame] | 758 | }; |
| 759 | |
Eric Anholt | 8273bd4 | 2010-08-04 12:34:56 -0700 | [diff] [blame] | 760 | remap_visitor v(target, temps); |
Ian Romanick | 31a9786 | 2010-07-12 18:48:50 -0700 | [diff] [blame] | 761 | |
| 762 | inst->accept(&v); |
| 763 | } |
| 764 | |
| 765 | |
| 766 | /** |
| 767 | * Move non-declarations from one instruction stream to another |
| 768 | * |
| 769 | * 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] | 770 | * 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] | 771 | * pointer) for \c last and \c false for \c make_copies on the first |
| 772 | * call. Successive calls pass the return value of the previous call for |
| 773 | * \c last and \c true for \c make_copies. |
| 774 | * |
| 775 | * \param instructions Source instruction stream |
| 776 | * \param last Instruction after which new instructions should be |
| 777 | * inserted in the target instruction stream |
| 778 | * \param make_copies Flag selecting whether instructions in \c instructions |
| 779 | * should be copied (via \c ir_instruction::clone) into the |
| 780 | * target list or moved. |
| 781 | * |
| 782 | * \return |
| 783 | * The new "last" instruction in the target instruction stream. This pointer |
| 784 | * is suitable for use as the \c last parameter of a later call to this |
| 785 | * function. |
| 786 | */ |
| 787 | exec_node * |
| 788 | move_non_declarations(exec_list *instructions, exec_node *last, |
| 789 | bool make_copies, gl_shader *target) |
| 790 | { |
Ian Romanick | 7e2aa91 | 2010-07-19 17:12:42 -0700 | [diff] [blame] | 791 | hash_table *temps = NULL; |
| 792 | |
| 793 | if (make_copies) |
| 794 | temps = hash_table_ctor(0, hash_table_pointer_hash, |
| 795 | hash_table_pointer_compare); |
| 796 | |
Ian Romanick | 303c99f | 2010-07-19 12:34:56 -0700 | [diff] [blame] | 797 | foreach_list_safe(node, instructions) { |
Ian Romanick | 31a9786 | 2010-07-12 18:48:50 -0700 | [diff] [blame] | 798 | ir_instruction *inst = (ir_instruction *) node; |
| 799 | |
Ian Romanick | 7e2aa91 | 2010-07-19 17:12:42 -0700 | [diff] [blame] | 800 | if (inst->as_function()) |
Ian Romanick | 31a9786 | 2010-07-12 18:48:50 -0700 | [diff] [blame] | 801 | continue; |
| 802 | |
Ian Romanick | 7e2aa91 | 2010-07-19 17:12:42 -0700 | [diff] [blame] | 803 | ir_variable *var = inst->as_variable(); |
| 804 | if ((var != NULL) && (var->mode != ir_var_temporary)) |
| 805 | continue; |
| 806 | |
| 807 | assert(inst->as_assignment() |
| 808 | || ((var != NULL) && (var->mode == ir_var_temporary))); |
Ian Romanick | 31a9786 | 2010-07-12 18:48:50 -0700 | [diff] [blame] | 809 | |
| 810 | if (make_copies) { |
Eric Anholt | 8273bd4 | 2010-08-04 12:34:56 -0700 | [diff] [blame] | 811 | inst = inst->clone(target, NULL); |
Ian Romanick | 7e2aa91 | 2010-07-19 17:12:42 -0700 | [diff] [blame] | 812 | |
| 813 | if (var != NULL) |
| 814 | hash_table_insert(temps, inst, var); |
| 815 | else |
Eric Anholt | 8273bd4 | 2010-08-04 12:34:56 -0700 | [diff] [blame] | 816 | remap_variables(inst, target, temps); |
Ian Romanick | 31a9786 | 2010-07-12 18:48:50 -0700 | [diff] [blame] | 817 | } else { |
| 818 | inst->remove(); |
| 819 | } |
| 820 | |
| 821 | last->insert_after(inst); |
| 822 | last = inst; |
| 823 | } |
| 824 | |
Ian Romanick | 7e2aa91 | 2010-07-19 17:12:42 -0700 | [diff] [blame] | 825 | if (make_copies) |
| 826 | hash_table_dtor(temps); |
| 827 | |
Ian Romanick | 31a9786 | 2010-07-12 18:48:50 -0700 | [diff] [blame] | 828 | return last; |
| 829 | } |
| 830 | |
| 831 | /** |
Ian Romanick | 15ce87e | 2010-07-09 15:28:22 -0700 | [diff] [blame] | 832 | * Get the function signature for main from a shader |
| 833 | */ |
| 834 | static ir_function_signature * |
| 835 | get_main_function_signature(gl_shader *sh) |
| 836 | { |
| 837 | ir_function *const f = sh->symbols->get_function("main"); |
| 838 | if (f != NULL) { |
| 839 | exec_list void_parameters; |
| 840 | |
| 841 | /* Look for the 'void main()' signature and ensure that it's defined. |
| 842 | * This keeps the linker from accidentally pick a shader that just |
| 843 | * contains a prototype for main. |
| 844 | * |
| 845 | * We don't have to check for multiple definitions of main (in multiple |
| 846 | * shaders) because that would have already been caught above. |
| 847 | */ |
| 848 | ir_function_signature *sig = f->matching_signature(&void_parameters); |
| 849 | if ((sig != NULL) && sig->is_defined) { |
| 850 | return sig; |
| 851 | } |
| 852 | } |
| 853 | |
| 854 | return NULL; |
| 855 | } |
| 856 | |
| 857 | |
| 858 | /** |
Ian Romanick | 3fb8787 | 2010-07-09 14:09:34 -0700 | [diff] [blame] | 859 | * Combine a group of shaders for a single stage to generate a linked shader |
| 860 | * |
| 861 | * \note |
| 862 | * If this function is supplied a single shader, it is cloned, and the new |
| 863 | * shader is returned. |
| 864 | */ |
| 865 | static struct gl_shader * |
Kenneth Graunke | 2da02e7 | 2010-11-17 11:03:57 -0800 | [diff] [blame] | 866 | link_intrastage_shaders(void *mem_ctx, |
| 867 | struct gl_context *ctx, |
Eric Anholt | 5d0f430 | 2010-08-18 12:02:35 -0700 | [diff] [blame] | 868 | struct gl_shader_program *prog, |
Ian Romanick | 3fb8787 | 2010-07-09 14:09:34 -0700 | [diff] [blame] | 869 | struct gl_shader **shader_list, |
| 870 | unsigned num_shaders) |
| 871 | { |
Ian Romanick | 13f782c | 2010-06-29 18:53:38 -0700 | [diff] [blame] | 872 | /* Check that global variables defined in multiple shaders are consistent. |
| 873 | */ |
| 874 | if (!cross_validate_globals(prog, shader_list, num_shaders, false)) |
| 875 | return NULL; |
| 876 | |
| 877 | /* Check that there is only a single definition of each function signature |
| 878 | * across all shaders. |
| 879 | */ |
| 880 | for (unsigned i = 0; i < (num_shaders - 1); i++) { |
| 881 | foreach_list(node, shader_list[i]->ir) { |
| 882 | ir_function *const f = ((ir_instruction *) node)->as_function(); |
| 883 | |
| 884 | if (f == NULL) |
| 885 | continue; |
| 886 | |
| 887 | for (unsigned j = i + 1; j < num_shaders; j++) { |
| 888 | ir_function *const other = |
| 889 | shader_list[j]->symbols->get_function(f->name); |
| 890 | |
| 891 | /* If the other shader has no function (and therefore no function |
| 892 | * signatures) with the same name, skip to the next shader. |
| 893 | */ |
| 894 | if (other == NULL) |
| 895 | continue; |
| 896 | |
| 897 | foreach_iter (exec_list_iterator, iter, *f) { |
| 898 | ir_function_signature *sig = |
| 899 | (ir_function_signature *) iter.get(); |
| 900 | |
Kenneth Graunke | f412fac | 2010-09-05 01:48:11 -0700 | [diff] [blame] | 901 | if (!sig->is_defined || sig->is_builtin) |
Ian Romanick | 13f782c | 2010-06-29 18:53:38 -0700 | [diff] [blame] | 902 | continue; |
| 903 | |
| 904 | ir_function_signature *other_sig = |
| 905 | other->exact_matching_signature(& sig->parameters); |
| 906 | |
| 907 | if ((other_sig != NULL) && other_sig->is_defined |
Kenneth Graunke | f412fac | 2010-09-05 01:48:11 -0700 | [diff] [blame] | 908 | && !other_sig->is_builtin) { |
Ian Romanick | 586e741 | 2011-07-28 14:04:09 -0700 | [diff] [blame] | 909 | linker_error(prog, "function `%s' is multiply defined", |
| 910 | f->name); |
Ian Romanick | 13f782c | 2010-06-29 18:53:38 -0700 | [diff] [blame] | 911 | return NULL; |
| 912 | } |
| 913 | } |
| 914 | } |
| 915 | } |
| 916 | } |
| 917 | |
| 918 | /* Find the shader that defines main, and make a clone of it. |
| 919 | * |
| 920 | * Starting with the clone, search for undefined references. If one is |
| 921 | * found, find the shader that defines it. Clone the reference and add |
| 922 | * it to the shader. Repeat until there are no undefined references or |
| 923 | * until a reference cannot be resolved. |
| 924 | */ |
Ian Romanick | 15ce87e | 2010-07-09 15:28:22 -0700 | [diff] [blame] | 925 | gl_shader *main = NULL; |
| 926 | for (unsigned i = 0; i < num_shaders; i++) { |
| 927 | if (get_main_function_signature(shader_list[i]) != NULL) { |
| 928 | main = shader_list[i]; |
| 929 | break; |
| 930 | } |
| 931 | } |
Ian Romanick | 13f782c | 2010-06-29 18:53:38 -0700 | [diff] [blame] | 932 | |
Ian Romanick | 15ce87e | 2010-07-09 15:28:22 -0700 | [diff] [blame] | 933 | if (main == NULL) { |
Ian Romanick | 586e741 | 2011-07-28 14:04:09 -0700 | [diff] [blame] | 934 | linker_error(prog, "%s shader lacks `main'\n", |
| 935 | (shader_list[0]->Type == GL_VERTEX_SHADER) |
| 936 | ? "vertex" : "fragment"); |
Ian Romanick | 15ce87e | 2010-07-09 15:28:22 -0700 | [diff] [blame] | 937 | return NULL; |
| 938 | } |
| 939 | |
Ian Romanick | 4a45595 | 2010-10-13 15:13:02 -0700 | [diff] [blame] | 940 | gl_shader *linked = ctx->Driver.NewShader(NULL, 0, main->Type); |
Ian Romanick | 15ce87e | 2010-07-09 15:28:22 -0700 | [diff] [blame] | 941 | linked->ir = new(linked) exec_list; |
Kenneth Graunke | 2da02e7 | 2010-11-17 11:03:57 -0800 | [diff] [blame] | 942 | clone_ir_list(mem_ctx, linked->ir, main->ir); |
Ian Romanick | 15ce87e | 2010-07-09 15:28:22 -0700 | [diff] [blame] | 943 | |
| 944 | populate_symbol_table(linked); |
Ian Romanick | 13f782c | 2010-06-29 18:53:38 -0700 | [diff] [blame] | 945 | |
Ian Romanick | 31a9786 | 2010-07-12 18:48:50 -0700 | [diff] [blame] | 946 | /* The a pointer to the main function in the final linked shader (i.e., the |
| 947 | * copy of the original shader that contained the main function). |
| 948 | */ |
| 949 | ir_function_signature *const main_sig = get_main_function_signature(linked); |
| 950 | |
| 951 | /* Move any instructions other than variable declarations or function |
| 952 | * declarations into main. |
| 953 | */ |
Ian Romanick | 9303e35 | 2010-07-19 12:33:54 -0700 | [diff] [blame] | 954 | exec_node *insertion_point = |
| 955 | move_non_declarations(linked->ir, (exec_node *) &main_sig->body, false, |
| 956 | linked); |
| 957 | |
Ian Romanick | 31a9786 | 2010-07-12 18:48:50 -0700 | [diff] [blame] | 958 | for (unsigned i = 0; i < num_shaders; i++) { |
Ian Romanick | 9303e35 | 2010-07-19 12:33:54 -0700 | [diff] [blame] | 959 | if (shader_list[i] == main) |
| 960 | continue; |
| 961 | |
Ian Romanick | 31a9786 | 2010-07-12 18:48:50 -0700 | [diff] [blame] | 962 | insertion_point = move_non_declarations(shader_list[i]->ir, |
Ian Romanick | 9303e35 | 2010-07-19 12:33:54 -0700 | [diff] [blame] | 963 | insertion_point, true, linked); |
Ian Romanick | 31a9786 | 2010-07-12 18:48:50 -0700 | [diff] [blame] | 964 | } |
| 965 | |
Ian Romanick | 13f782c | 2010-06-29 18:53:38 -0700 | [diff] [blame] | 966 | /* Resolve initializers for global variables in the linked shader. |
| 967 | */ |
Ian Romanick | d5be2ac | 2010-07-20 11:29:46 -0700 | [diff] [blame] | 968 | unsigned num_linking_shaders = num_shaders; |
| 969 | for (unsigned i = 0; i < num_shaders; i++) |
| 970 | num_linking_shaders += shader_list[i]->num_builtins_to_link; |
| 971 | |
| 972 | gl_shader **linking_shaders = |
| 973 | (gl_shader **) calloc(num_linking_shaders, sizeof(gl_shader *)); |
| 974 | |
| 975 | memcpy(linking_shaders, shader_list, |
| 976 | sizeof(linking_shaders[0]) * num_shaders); |
| 977 | |
| 978 | unsigned idx = num_shaders; |
| 979 | for (unsigned i = 0; i < num_shaders; i++) { |
| 980 | memcpy(&linking_shaders[idx], shader_list[i]->builtins_to_link, |
| 981 | sizeof(linking_shaders[0]) * shader_list[i]->num_builtins_to_link); |
| 982 | idx += shader_list[i]->num_builtins_to_link; |
| 983 | } |
| 984 | |
| 985 | assert(idx == num_linking_shaders); |
| 986 | |
Ian Romanick | 4a45595 | 2010-10-13 15:13:02 -0700 | [diff] [blame] | 987 | if (!link_function_calls(prog, linked, linking_shaders, |
| 988 | num_linking_shaders)) { |
| 989 | ctx->Driver.DeleteShader(ctx, linked); |
| 990 | linked = NULL; |
| 991 | } |
Ian Romanick | d5be2ac | 2010-07-20 11:29:46 -0700 | [diff] [blame] | 992 | |
| 993 | free(linking_shaders); |
Ian Romanick | 3fb8787 | 2010-07-09 14:09:34 -0700 | [diff] [blame] | 994 | |
Paul Berry | c148ef6 | 2011-08-03 15:37:01 -0700 | [diff] [blame] | 995 | #ifdef DEBUG |
| 996 | /* At this point linked should contain all of the linked IR, so |
| 997 | * validate it to make sure nothing went wrong. |
| 998 | */ |
| 999 | if (linked) |
| 1000 | validate_ir_tree(linked->ir); |
| 1001 | #endif |
| 1002 | |
Ian Romanick | c87e9ef | 2011-01-25 12:04:08 -0800 | [diff] [blame] | 1003 | /* Make a pass over all variable declarations to ensure that arrays with |
Ian Romanick | 6f53921 | 2010-12-07 18:30:33 -0800 | [diff] [blame] | 1004 | * unspecified sizes have a size specified. The size is inferred from the |
| 1005 | * max_array_access field. |
| 1006 | */ |
Ian Romanick | 002cd2c | 2010-12-07 19:00:44 -0800 | [diff] [blame] | 1007 | if (linked != NULL) { |
Ian Romanick | c87e9ef | 2011-01-25 12:04:08 -0800 | [diff] [blame] | 1008 | class array_sizing_visitor : public ir_hierarchical_visitor { |
| 1009 | public: |
| 1010 | virtual ir_visitor_status visit(ir_variable *var) |
| 1011 | { |
| 1012 | if (var->type->is_array() && (var->type->length == 0)) { |
| 1013 | const glsl_type *type = |
| 1014 | glsl_type::get_array_instance(var->type->fields.array, |
Ian Romanick | 25b36e8 | 2011-02-15 18:17:53 -0800 | [diff] [blame] | 1015 | var->max_array_access + 1); |
Ian Romanick | 6f53921 | 2010-12-07 18:30:33 -0800 | [diff] [blame] | 1016 | |
Ian Romanick | c87e9ef | 2011-01-25 12:04:08 -0800 | [diff] [blame] | 1017 | assert(type != NULL); |
| 1018 | var->type = type; |
| 1019 | } |
Ian Romanick | 6f53921 | 2010-12-07 18:30:33 -0800 | [diff] [blame] | 1020 | |
Ian Romanick | c87e9ef | 2011-01-25 12:04:08 -0800 | [diff] [blame] | 1021 | return visit_continue; |
| 1022 | } |
| 1023 | } v; |
Ian Romanick | 6f53921 | 2010-12-07 18:30:33 -0800 | [diff] [blame] | 1024 | |
Ian Romanick | c87e9ef | 2011-01-25 12:04:08 -0800 | [diff] [blame] | 1025 | v.run(linked->ir); |
Ian Romanick | 6f53921 | 2010-12-07 18:30:33 -0800 | [diff] [blame] | 1026 | } |
| 1027 | |
Ian Romanick | 3fb8787 | 2010-07-09 14:09:34 -0700 | [diff] [blame] | 1028 | return linked; |
| 1029 | } |
| 1030 | |
Eric Anholt | a721abf | 2010-08-23 10:32:01 -0700 | [diff] [blame] | 1031 | /** |
| 1032 | * Update the sizes of linked shader uniform arrays to the maximum |
| 1033 | * array index used. |
| 1034 | * |
| 1035 | * From page 81 (page 95 of the PDF) of the OpenGL 2.1 spec: |
| 1036 | * |
| 1037 | * If one or more elements of an array are active, |
| 1038 | * GetActiveUniform will return the name of the array in name, |
| 1039 | * subject to the restrictions listed above. The type of the array |
| 1040 | * is returned in type. The size parameter contains the highest |
| 1041 | * array element index used, plus one. The compiler or linker |
| 1042 | * determines the highest index used. There will be only one |
| 1043 | * active uniform reported by the GL per uniform array. |
| 1044 | |
| 1045 | */ |
| 1046 | static void |
Eric Anholt | 586b4b5 | 2010-09-28 14:32:16 -0700 | [diff] [blame] | 1047 | update_array_sizes(struct gl_shader_program *prog) |
Eric Anholt | a721abf | 2010-08-23 10:32:01 -0700 | [diff] [blame] | 1048 | { |
Ian Romanick | 3322fba | 2010-10-14 13:28:42 -0700 | [diff] [blame] | 1049 | for (unsigned i = 0; i < MESA_SHADER_TYPES; i++) { |
| 1050 | if (prog->_LinkedShaders[i] == NULL) |
| 1051 | continue; |
| 1052 | |
Eric Anholt | a721abf | 2010-08-23 10:32:01 -0700 | [diff] [blame] | 1053 | foreach_list(node, prog->_LinkedShaders[i]->ir) { |
| 1054 | ir_variable *const var = ((ir_instruction *) node)->as_variable(); |
| 1055 | |
Eric Anholt | 586b4b5 | 2010-09-28 14:32:16 -0700 | [diff] [blame] | 1056 | if ((var == NULL) || (var->mode != ir_var_uniform && |
| 1057 | var->mode != ir_var_in && |
| 1058 | var->mode != ir_var_out) || |
Eric Anholt | a721abf | 2010-08-23 10:32:01 -0700 | [diff] [blame] | 1059 | !var->type->is_array()) |
| 1060 | continue; |
| 1061 | |
| 1062 | unsigned int size = var->max_array_access; |
Ian Romanick | 3322fba | 2010-10-14 13:28:42 -0700 | [diff] [blame] | 1063 | for (unsigned j = 0; j < MESA_SHADER_TYPES; j++) { |
| 1064 | if (prog->_LinkedShaders[j] == NULL) |
| 1065 | continue; |
| 1066 | |
Eric Anholt | a721abf | 2010-08-23 10:32:01 -0700 | [diff] [blame] | 1067 | foreach_list(node2, prog->_LinkedShaders[j]->ir) { |
| 1068 | ir_variable *other_var = ((ir_instruction *) node2)->as_variable(); |
| 1069 | if (!other_var) |
| 1070 | continue; |
| 1071 | |
| 1072 | if (strcmp(var->name, other_var->name) == 0 && |
| 1073 | other_var->max_array_access > size) { |
| 1074 | size = other_var->max_array_access; |
| 1075 | } |
| 1076 | } |
| 1077 | } |
Eric Anholt | 586b4b5 | 2010-09-28 14:32:16 -0700 | [diff] [blame] | 1078 | |
Eric Anholt | a721abf | 2010-08-23 10:32:01 -0700 | [diff] [blame] | 1079 | if (size + 1 != var->type->fields.array->length) { |
Ian Romanick | 89d81ab | 2011-01-25 10:41:20 -0800 | [diff] [blame] | 1080 | /* If this is a built-in uniform (i.e., it's backed by some |
| 1081 | * fixed-function state), adjust the number of state slots to |
| 1082 | * match the new array size. The number of slots per array entry |
Bryan Cain | f18a086 | 2011-04-23 19:29:15 -0500 | [diff] [blame] | 1083 | * 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] | 1084 | * slots is an integer multiple of the number of array elements. |
| 1085 | * Determine the number of slots per array element by dividing by |
| 1086 | * the old (total) size. |
| 1087 | */ |
| 1088 | if (var->num_state_slots > 0) { |
| 1089 | var->num_state_slots = (size + 1) |
| 1090 | * (var->num_state_slots / var->type->length); |
| 1091 | } |
| 1092 | |
Eric Anholt | a721abf | 2010-08-23 10:32:01 -0700 | [diff] [blame] | 1093 | var->type = glsl_type::get_array_instance(var->type->fields.array, |
| 1094 | size + 1); |
| 1095 | /* FINISHME: We should update the types of array |
| 1096 | * dereferences of this variable now. |
| 1097 | */ |
| 1098 | } |
| 1099 | } |
| 1100 | } |
| 1101 | } |
| 1102 | |
Ian Romanick | 6984670 | 2010-06-22 17:29:19 -0700 | [diff] [blame] | 1103 | /** |
Bryan Cain | f18a086 | 2011-04-23 19:29:15 -0500 | [diff] [blame] | 1104 | * Find a contiguous set of available bits in a bitmask. |
Ian Romanick | 6984670 | 2010-06-22 17:29:19 -0700 | [diff] [blame] | 1105 | * |
| 1106 | * \param used_mask Bits representing used (1) and unused (0) locations |
| 1107 | * \param needed_count Number of contiguous bits needed. |
| 1108 | * |
| 1109 | * \return |
| 1110 | * Base location of the available bits on success or -1 on failure. |
| 1111 | */ |
| 1112 | int |
| 1113 | find_available_slots(unsigned used_mask, unsigned needed_count) |
| 1114 | { |
| 1115 | unsigned needed_mask = (1 << needed_count) - 1; |
| 1116 | const int max_bit_to_test = (8 * sizeof(used_mask)) - needed_count; |
| 1117 | |
| 1118 | /* The comparison to 32 is redundant, but without it GCC emits "warning: |
| 1119 | * cannot optimize possibly infinite loops" for the loop below. |
| 1120 | */ |
| 1121 | if ((needed_count == 0) || (max_bit_to_test < 0) || (max_bit_to_test > 32)) |
| 1122 | return -1; |
| 1123 | |
| 1124 | for (int i = 0; i <= max_bit_to_test; i++) { |
| 1125 | if ((needed_mask & ~used_mask) == needed_mask) |
| 1126 | return i; |
| 1127 | |
| 1128 | needed_mask <<= 1; |
| 1129 | } |
| 1130 | |
| 1131 | return -1; |
| 1132 | } |
| 1133 | |
| 1134 | |
Ian Romanick | d32d4f7 | 2011-06-27 17:59:58 -0700 | [diff] [blame] | 1135 | /** |
| 1136 | * Assign locations for either VS inputs for FS outputs |
| 1137 | * |
| 1138 | * \param prog Shader program whose variables need locations assigned |
| 1139 | * \param target_index Selector for the program target to receive location |
| 1140 | * assignmnets. Must be either \c MESA_SHADER_VERTEX or |
| 1141 | * \c MESA_SHADER_FRAGMENT. |
| 1142 | * \param max_index Maximum number of generic locations. This corresponds |
| 1143 | * to either the maximum number of draw buffers or the |
| 1144 | * maximum number of generic attributes. |
| 1145 | * |
| 1146 | * \return |
| 1147 | * If locations are successfully assigned, true is returned. Otherwise an |
| 1148 | * error is emitted to the shader link log and false is returned. |
Ian Romanick | d32d4f7 | 2011-06-27 17:59:58 -0700 | [diff] [blame] | 1149 | */ |
Ian Romanick | 6984670 | 2010-06-22 17:29:19 -0700 | [diff] [blame] | 1150 | bool |
Ian Romanick | d32d4f7 | 2011-06-27 17:59:58 -0700 | [diff] [blame] | 1151 | assign_attribute_or_color_locations(gl_shader_program *prog, |
| 1152 | unsigned target_index, |
| 1153 | unsigned max_index) |
Ian Romanick | 0ad22cd | 2010-06-21 17:18:31 -0700 | [diff] [blame] | 1154 | { |
Ian Romanick | d32d4f7 | 2011-06-27 17:59:58 -0700 | [diff] [blame] | 1155 | /* Mark invalid locations as being used. |
Ian Romanick | 9342d26 | 2010-06-22 17:41:37 -0700 | [diff] [blame] | 1156 | */ |
Ian Romanick | d32d4f7 | 2011-06-27 17:59:58 -0700 | [diff] [blame] | 1157 | unsigned used_locations = (max_index >= 32) |
| 1158 | ? ~0 : ~((1 << max_index) - 1); |
Ian Romanick | 0ad22cd | 2010-06-21 17:18:31 -0700 | [diff] [blame] | 1159 | |
Ian Romanick | d32d4f7 | 2011-06-27 17:59:58 -0700 | [diff] [blame] | 1160 | assert((target_index == MESA_SHADER_VERTEX) |
| 1161 | || (target_index == MESA_SHADER_FRAGMENT)); |
| 1162 | |
| 1163 | gl_shader *const sh = prog->_LinkedShaders[target_index]; |
| 1164 | if (sh == NULL) |
| 1165 | return true; |
Ian Romanick | 0ad22cd | 2010-06-21 17:18:31 -0700 | [diff] [blame] | 1166 | |
Ian Romanick | 6984670 | 2010-06-22 17:29:19 -0700 | [diff] [blame] | 1167 | /* Operate in a total of four passes. |
Ian Romanick | 0ad22cd | 2010-06-21 17:18:31 -0700 | [diff] [blame] | 1168 | * |
| 1169 | * 1. Invalidate the location assignments for all vertex shader inputs. |
| 1170 | * |
| 1171 | * 2. Assign locations for inputs that have user-defined (via |
Ian Romanick | b12b5d9 | 2011-11-04 16:08:52 -0700 | [diff] [blame] | 1172 | * glBindVertexAttribLocation) locations and outputs that have |
| 1173 | * user-defined locations (via glBindFragDataLocation). |
Ian Romanick | 0ad22cd | 2010-06-21 17:18:31 -0700 | [diff] [blame] | 1174 | * |
Ian Romanick | 6984670 | 2010-06-22 17:29:19 -0700 | [diff] [blame] | 1175 | * 3. Sort the attributes without assigned locations by number of slots |
| 1176 | * required in decreasing order. Fragmentation caused by attribute |
| 1177 | * locations assigned by the application may prevent large attributes |
| 1178 | * from having enough contiguous space. |
| 1179 | * |
| 1180 | * 4. Assign locations to any inputs without assigned locations. |
Ian Romanick | 0ad22cd | 2010-06-21 17:18:31 -0700 | [diff] [blame] | 1181 | */ |
| 1182 | |
Ian Romanick | d32d4f7 | 2011-06-27 17:59:58 -0700 | [diff] [blame] | 1183 | const int generic_base = (target_index == MESA_SHADER_VERTEX) |
Brian Paul | 7eb7d67 | 2011-07-07 16:47:59 -0600 | [diff] [blame] | 1184 | ? (int) VERT_ATTRIB_GENERIC0 : (int) FRAG_RESULT_DATA0; |
Ian Romanick | 0ad22cd | 2010-06-21 17:18:31 -0700 | [diff] [blame] | 1185 | |
Ian Romanick | d32d4f7 | 2011-06-27 17:59:58 -0700 | [diff] [blame] | 1186 | const enum ir_variable_mode direction = |
| 1187 | (target_index == MESA_SHADER_VERTEX) ? ir_var_in : ir_var_out; |
| 1188 | |
| 1189 | |
Ian Romanick | f6ee7bc | 2011-10-11 16:15:47 -0700 | [diff] [blame] | 1190 | link_invalidate_variable_locations(sh, direction, generic_base); |
Ian Romanick | d32d4f7 | 2011-06-27 17:59:58 -0700 | [diff] [blame] | 1191 | |
Ian Romanick | 6984670 | 2010-06-22 17:29:19 -0700 | [diff] [blame] | 1192 | /* Temporary storage for the set of attributes that need locations assigned. |
| 1193 | */ |
| 1194 | struct temp_attr { |
| 1195 | unsigned slots; |
| 1196 | ir_variable *var; |
| 1197 | |
| 1198 | /* Used below in the call to qsort. */ |
| 1199 | static int compare(const void *a, const void *b) |
| 1200 | { |
| 1201 | const temp_attr *const l = (const temp_attr *) a; |
| 1202 | const temp_attr *const r = (const temp_attr *) b; |
| 1203 | |
| 1204 | /* Reversed because we want a descending order sort below. */ |
| 1205 | return r->slots - l->slots; |
| 1206 | } |
| 1207 | } to_assign[16]; |
| 1208 | |
| 1209 | unsigned num_attr = 0; |
| 1210 | |
Eric Anholt | 16b68b1 | 2010-06-30 11:05:43 -0700 | [diff] [blame] | 1211 | foreach_list(node, sh->ir) { |
Ian Romanick | 0ad22cd | 2010-06-21 17:18:31 -0700 | [diff] [blame] | 1212 | ir_variable *const var = ((ir_instruction *) node)->as_variable(); |
| 1213 | |
Brian Paul | 4470ff2 | 2011-07-19 21:10:25 -0600 | [diff] [blame] | 1214 | if ((var == NULL) || (var->mode != (unsigned) direction)) |
Ian Romanick | 0ad22cd | 2010-06-21 17:18:31 -0700 | [diff] [blame] | 1215 | continue; |
| 1216 | |
Ian Romanick | 68a4fc9 | 2010-10-07 17:21:22 -0700 | [diff] [blame] | 1217 | if (var->explicit_location) { |
Ian Romanick | d32d4f7 | 2011-06-27 17:59:58 -0700 | [diff] [blame] | 1218 | if ((var->location >= (int)(max_index + generic_base)) |
Ian Romanick | 68a4fc9 | 2010-10-07 17:21:22 -0700 | [diff] [blame] | 1219 | || (var->location < 0)) { |
Ian Romanick | 586e741 | 2011-07-28 14:04:09 -0700 | [diff] [blame] | 1220 | linker_error(prog, |
| 1221 | "invalid explicit location %d specified for `%s'\n", |
Ian Romanick | 523b611 | 2011-08-17 15:40:03 -0700 | [diff] [blame] | 1222 | (var->location < 0) |
| 1223 | ? var->location : var->location - generic_base, |
Ian Romanick | 586e741 | 2011-07-28 14:04:09 -0700 | [diff] [blame] | 1224 | var->name); |
Ian Romanick | 68a4fc9 | 2010-10-07 17:21:22 -0700 | [diff] [blame] | 1225 | return false; |
Ian Romanick | 523b611 | 2011-08-17 15:40:03 -0700 | [diff] [blame] | 1226 | } |
| 1227 | } else if (target_index == MESA_SHADER_VERTEX) { |
| 1228 | unsigned binding; |
| 1229 | |
| 1230 | if (prog->AttributeBindings->get(binding, var->name)) { |
| 1231 | assert(binding >= VERT_ATTRIB_GENERIC0); |
| 1232 | var->location = binding; |
Ian Romanick | 68a4fc9 | 2010-10-07 17:21:22 -0700 | [diff] [blame] | 1233 | } |
Ian Romanick | b12b5d9 | 2011-11-04 16:08:52 -0700 | [diff] [blame] | 1234 | } else if (target_index == MESA_SHADER_FRAGMENT) { |
| 1235 | unsigned binding; |
| 1236 | |
| 1237 | if (prog->FragDataBindings->get(binding, var->name)) { |
| 1238 | assert(binding >= FRAG_RESULT_DATA0); |
| 1239 | var->location = binding; |
| 1240 | } |
Ian Romanick | 68a4fc9 | 2010-10-07 17:21:22 -0700 | [diff] [blame] | 1241 | } |
| 1242 | |
Ian Romanick | 9f0e98d | 2011-10-06 10:25:34 -0700 | [diff] [blame] | 1243 | /* If the variable is not a built-in and has a location statically |
| 1244 | * assigned in the shader (presumably via a layout qualifier), make sure |
| 1245 | * that it doesn't collide with other assigned locations. Otherwise, |
| 1246 | * add it to the list of variables that need linker-assigned locations. |
Ian Romanick | 0ad22cd | 2010-06-21 17:18:31 -0700 | [diff] [blame] | 1247 | */ |
Ian Romanick | 523b611 | 2011-08-17 15:40:03 -0700 | [diff] [blame] | 1248 | const unsigned slots = count_attribute_slots(var->type); |
| 1249 | if (var->location != -1) { |
| 1250 | if (var->location >= generic_base) { |
| 1251 | /* From page 61 of the OpenGL 4.0 spec: |
| 1252 | * |
| 1253 | * "LinkProgram will fail if the attribute bindings assigned |
| 1254 | * by BindAttribLocation do not leave not enough space to |
| 1255 | * assign a location for an active matrix attribute or an |
| 1256 | * active attribute array, both of which require multiple |
| 1257 | * contiguous generic attributes." |
| 1258 | * |
| 1259 | * Previous versions of the spec contain similar language but omit |
| 1260 | * the bit about attribute arrays. |
| 1261 | * |
| 1262 | * Page 61 of the OpenGL 4.0 spec also says: |
| 1263 | * |
| 1264 | * "It is possible for an application to bind more than one |
| 1265 | * attribute name to the same location. This is referred to as |
| 1266 | * aliasing. This will only work if only one of the aliased |
| 1267 | * attributes is active in the executable program, or if no |
| 1268 | * path through the shader consumes more than one attribute of |
| 1269 | * a set of attributes aliased to the same location. A link |
| 1270 | * error can occur if the linker determines that every path |
| 1271 | * through the shader consumes multiple aliased attributes, |
| 1272 | * but implementations are not required to generate an error |
| 1273 | * in this case." |
| 1274 | * |
| 1275 | * These two paragraphs are either somewhat contradictory, or I |
| 1276 | * don't fully understand one or both of them. |
| 1277 | */ |
| 1278 | /* FINISHME: The code as currently written does not support |
| 1279 | * FINISHME: attribute location aliasing (see comment above). |
| 1280 | */ |
| 1281 | /* Mask representing the contiguous slots that will be used by |
| 1282 | * this attribute. |
| 1283 | */ |
| 1284 | const unsigned attr = var->location - generic_base; |
| 1285 | const unsigned use_mask = (1 << slots) - 1; |
Ian Romanick | 0ad22cd | 2010-06-21 17:18:31 -0700 | [diff] [blame] | 1286 | |
Ian Romanick | 523b611 | 2011-08-17 15:40:03 -0700 | [diff] [blame] | 1287 | /* Generate a link error if the set of bits requested for this |
| 1288 | * attribute overlaps any previously allocated bits. |
| 1289 | */ |
| 1290 | if ((~(use_mask << attr) & used_locations) != used_locations) { |
| 1291 | linker_error(prog, |
| 1292 | "insufficient contiguous attribute locations " |
| 1293 | "available for vertex shader input `%s'", |
| 1294 | var->name); |
| 1295 | return false; |
| 1296 | } |
| 1297 | |
| 1298 | used_locations |= (use_mask << attr); |
| 1299 | } |
| 1300 | |
| 1301 | continue; |
| 1302 | } |
| 1303 | |
| 1304 | to_assign[num_attr].slots = slots; |
Ian Romanick | 6984670 | 2010-06-22 17:29:19 -0700 | [diff] [blame] | 1305 | to_assign[num_attr].var = var; |
| 1306 | num_attr++; |
Ian Romanick | 0ad22cd | 2010-06-21 17:18:31 -0700 | [diff] [blame] | 1307 | } |
Ian Romanick | 6984670 | 2010-06-22 17:29:19 -0700 | [diff] [blame] | 1308 | |
| 1309 | /* If all of the attributes were assigned locations by the application (or |
| 1310 | * are built-in attributes with fixed locations), return early. This should |
| 1311 | * be the common case. |
| 1312 | */ |
| 1313 | if (num_attr == 0) |
| 1314 | return true; |
| 1315 | |
| 1316 | qsort(to_assign, num_attr, sizeof(to_assign[0]), temp_attr::compare); |
| 1317 | |
Ian Romanick | d32d4f7 | 2011-06-27 17:59:58 -0700 | [diff] [blame] | 1318 | if (target_index == MESA_SHADER_VERTEX) { |
| 1319 | /* VERT_ATTRIB_GENERIC0 is a pseudo-alias for VERT_ATTRIB_POS. It can |
| 1320 | * only be explicitly assigned by via glBindAttribLocation. Mark it as |
| 1321 | * reserved to prevent it from being automatically allocated below. |
| 1322 | */ |
| 1323 | find_deref_visitor find("gl_Vertex"); |
| 1324 | find.run(sh->ir); |
| 1325 | if (find.variable_found()) |
| 1326 | used_locations |= (1 << 0); |
| 1327 | } |
Ian Romanick | 982e379 | 2010-06-29 18:58:20 -0700 | [diff] [blame] | 1328 | |
Ian Romanick | 6984670 | 2010-06-22 17:29:19 -0700 | [diff] [blame] | 1329 | for (unsigned i = 0; i < num_attr; i++) { |
| 1330 | /* Mask representing the contiguous slots that will be used by this |
| 1331 | * attribute. |
| 1332 | */ |
| 1333 | const unsigned use_mask = (1 << to_assign[i].slots) - 1; |
| 1334 | |
| 1335 | int location = find_available_slots(used_locations, to_assign[i].slots); |
| 1336 | |
| 1337 | if (location < 0) { |
Ian Romanick | d32d4f7 | 2011-06-27 17:59:58 -0700 | [diff] [blame] | 1338 | const char *const string = (target_index == MESA_SHADER_VERTEX) |
| 1339 | ? "vertex shader input" : "fragment shader output"; |
| 1340 | |
Ian Romanick | 586e741 | 2011-07-28 14:04:09 -0700 | [diff] [blame] | 1341 | linker_error(prog, |
| 1342 | "insufficient contiguous attribute locations " |
| 1343 | "available for %s `%s'", |
| 1344 | string, to_assign[i].var->name); |
Ian Romanick | 6984670 | 2010-06-22 17:29:19 -0700 | [diff] [blame] | 1345 | return false; |
| 1346 | } |
| 1347 | |
Ian Romanick | d32d4f7 | 2011-06-27 17:59:58 -0700 | [diff] [blame] | 1348 | to_assign[i].var->location = generic_base + location; |
Ian Romanick | 6984670 | 2010-06-22 17:29:19 -0700 | [diff] [blame] | 1349 | used_locations |= (use_mask << location); |
| 1350 | } |
| 1351 | |
| 1352 | return true; |
Ian Romanick | 0ad22cd | 2010-06-21 17:18:31 -0700 | [diff] [blame] | 1353 | } |
| 1354 | |
| 1355 | |
Ian Romanick | 40e114b | 2010-08-17 14:55:50 -0700 | [diff] [blame] | 1356 | /** |
Ian Romanick | cc90e62 | 2010-10-19 17:59:10 -0700 | [diff] [blame] | 1357 | * Demote shader inputs and outputs that are not used in other stages |
Ian Romanick | 40e114b | 2010-08-17 14:55:50 -0700 | [diff] [blame] | 1358 | */ |
| 1359 | void |
Ian Romanick | cc90e62 | 2010-10-19 17:59:10 -0700 | [diff] [blame] | 1360 | 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] | 1361 | { |
| 1362 | foreach_list(node, sh->ir) { |
| 1363 | ir_variable *const var = ((ir_instruction *) node)->as_variable(); |
| 1364 | |
Ian Romanick | cc90e62 | 2010-10-19 17:59:10 -0700 | [diff] [blame] | 1365 | if ((var == NULL) || (var->mode != int(mode))) |
Ian Romanick | 40e114b | 2010-08-17 14:55:50 -0700 | [diff] [blame] | 1366 | continue; |
| 1367 | |
Ian Romanick | cc90e62 | 2010-10-19 17:59:10 -0700 | [diff] [blame] | 1368 | /* A shader 'in' or 'out' variable is only really an input or output if |
| 1369 | * its value is used by other shader stages. This will cause the variable |
| 1370 | * to have a location assigned. |
Ian Romanick | 40e114b | 2010-08-17 14:55:50 -0700 | [diff] [blame] | 1371 | */ |
| 1372 | if (var->location == -1) { |
| 1373 | var->mode = ir_var_auto; |
| 1374 | } |
| 1375 | } |
| 1376 | } |
| 1377 | |
| 1378 | |
Paul Berry | 871ddb9 | 2011-11-05 11:17:32 -0700 | [diff] [blame] | 1379 | /** |
| 1380 | * Data structure tracking information about a transform feedback declaration |
| 1381 | * during linking. |
| 1382 | */ |
| 1383 | class tfeedback_decl |
| 1384 | { |
| 1385 | public: |
Paul Berry | 456279b | 2011-12-26 19:39:25 -0800 | [diff] [blame] | 1386 | bool init(struct gl_context *ctx, struct gl_shader_program *prog, |
| 1387 | const void *mem_ctx, const char *input); |
Paul Berry | 871ddb9 | 2011-11-05 11:17:32 -0700 | [diff] [blame] | 1388 | static bool is_same(const tfeedback_decl &x, const tfeedback_decl &y); |
| 1389 | bool assign_location(struct gl_context *ctx, struct gl_shader_program *prog, |
| 1390 | ir_variable *output_var); |
Paul Berry | d3150eb | 2012-01-09 11:25:14 -0800 | [diff] [blame] | 1391 | bool store(struct gl_context *ctx, struct gl_shader_program *prog, |
Eric Anholt | 9d36c96 | 2012-01-02 17:08:13 -0800 | [diff] [blame] | 1392 | struct gl_transform_feedback_info *info, unsigned buffer, |
| 1393 | unsigned varying) const; |
Paul Berry | 871ddb9 | 2011-11-05 11:17:32 -0700 | [diff] [blame] | 1394 | |
| 1395 | |
| 1396 | /** |
| 1397 | * True if assign_location() has been called for this object. |
| 1398 | */ |
| 1399 | bool is_assigned() const |
| 1400 | { |
| 1401 | return this->location != -1; |
| 1402 | } |
| 1403 | |
| 1404 | /** |
| 1405 | * Determine whether this object refers to the variable var. |
| 1406 | */ |
| 1407 | bool matches_var(ir_variable *var) const |
| 1408 | { |
Paul Berry | 642e5b41 | 2012-01-04 13:57:52 -0800 | [diff] [blame] | 1409 | if (this->is_clip_distance_mesa) |
| 1410 | return strcmp(var->name, "gl_ClipDistanceMESA") == 0; |
| 1411 | else |
| 1412 | return strcmp(var->name, this->var_name) == 0; |
Paul Berry | 871ddb9 | 2011-11-05 11:17:32 -0700 | [diff] [blame] | 1413 | } |
| 1414 | |
| 1415 | /** |
| 1416 | * The total number of varying components taken up by this variable. Only |
| 1417 | * valid if is_assigned() is true. |
| 1418 | */ |
| 1419 | unsigned num_components() const |
| 1420 | { |
Paul Berry | 642e5b41 | 2012-01-04 13:57:52 -0800 | [diff] [blame] | 1421 | if (this->is_clip_distance_mesa) |
| 1422 | return this->size; |
Paul Berry | be4e9f7 | 2012-01-04 12:21:55 -0800 | [diff] [blame] | 1423 | else |
Paul Berry | 642e5b41 | 2012-01-04 13:57:52 -0800 | [diff] [blame] | 1424 | return this->vector_elements * this->matrix_columns * this->size; |
Paul Berry | 871ddb9 | 2011-11-05 11:17:32 -0700 | [diff] [blame] | 1425 | } |
| 1426 | |
| 1427 | private: |
| 1428 | /** |
| 1429 | * The name that was supplied to glTransformFeedbackVaryings. Used for |
Eric Anholt | 9d36c96 | 2012-01-02 17:08:13 -0800 | [diff] [blame] | 1430 | * error reporting and glGetTransformFeedbackVarying(). |
Paul Berry | 871ddb9 | 2011-11-05 11:17:32 -0700 | [diff] [blame] | 1431 | */ |
| 1432 | const char *orig_name; |
| 1433 | |
| 1434 | /** |
| 1435 | * The name of the variable, parsed from orig_name. |
| 1436 | */ |
Paul Berry | 913a5c2 | 2011-12-27 08:24:57 -0800 | [diff] [blame] | 1437 | const char *var_name; |
Paul Berry | 871ddb9 | 2011-11-05 11:17:32 -0700 | [diff] [blame] | 1438 | |
| 1439 | /** |
| 1440 | * True if the declaration in orig_name represents an array. |
| 1441 | */ |
Paul Berry | 33fe021 | 2012-01-03 20:41:34 -0800 | [diff] [blame] | 1442 | bool is_subscripted; |
Paul Berry | 871ddb9 | 2011-11-05 11:17:32 -0700 | [diff] [blame] | 1443 | |
| 1444 | /** |
Paul Berry | 33fe021 | 2012-01-03 20:41:34 -0800 | [diff] [blame] | 1445 | * If is_subscripted is true, the subscript that was specified in orig_name. |
Paul Berry | 871ddb9 | 2011-11-05 11:17:32 -0700 | [diff] [blame] | 1446 | */ |
Paul Berry | 33fe021 | 2012-01-03 20:41:34 -0800 | [diff] [blame] | 1447 | unsigned array_subscript; |
Paul Berry | 871ddb9 | 2011-11-05 11:17:32 -0700 | [diff] [blame] | 1448 | |
| 1449 | /** |
Paul Berry | 642e5b41 | 2012-01-04 13:57:52 -0800 | [diff] [blame] | 1450 | * True if the variable is gl_ClipDistance and the driver lowers |
| 1451 | * gl_ClipDistance to gl_ClipDistanceMESA. |
Paul Berry | 456279b | 2011-12-26 19:39:25 -0800 | [diff] [blame] | 1452 | */ |
Paul Berry | 642e5b41 | 2012-01-04 13:57:52 -0800 | [diff] [blame] | 1453 | bool is_clip_distance_mesa; |
Paul Berry | 456279b | 2011-12-26 19:39:25 -0800 | [diff] [blame] | 1454 | |
| 1455 | /** |
Paul Berry | 871ddb9 | 2011-11-05 11:17:32 -0700 | [diff] [blame] | 1456 | * The vertex shader output location that the linker assigned for this |
| 1457 | * variable. -1 if a location hasn't been assigned yet. |
| 1458 | */ |
| 1459 | int location; |
| 1460 | |
| 1461 | /** |
| 1462 | * If location != -1, the number of vector elements in this variable, or 1 |
| 1463 | * if this variable is a scalar. |
| 1464 | */ |
| 1465 | unsigned vector_elements; |
| 1466 | |
| 1467 | /** |
| 1468 | * If location != -1, the number of matrix columns in this variable, or 1 |
| 1469 | * if this variable is not a matrix. |
| 1470 | */ |
| 1471 | unsigned matrix_columns; |
Eric Anholt | 9d36c96 | 2012-01-02 17:08:13 -0800 | [diff] [blame] | 1472 | |
| 1473 | /** Type of the varying returned by glGetTransformFeedbackVarying() */ |
| 1474 | GLenum type; |
Paul Berry | 33fe021 | 2012-01-03 20:41:34 -0800 | [diff] [blame] | 1475 | |
| 1476 | /** |
| 1477 | * If location != -1, the size that should be returned by |
| 1478 | * glGetTransformFeedbackVarying(). |
| 1479 | */ |
| 1480 | unsigned size; |
Paul Berry | 871ddb9 | 2011-11-05 11:17:32 -0700 | [diff] [blame] | 1481 | }; |
| 1482 | |
| 1483 | |
| 1484 | /** |
| 1485 | * Initialize this object based on a string that was passed to |
| 1486 | * glTransformFeedbackVaryings. If there is a parse error, the error is |
| 1487 | * reported using linker_error(), and false is returned. |
| 1488 | */ |
| 1489 | bool |
Paul Berry | 456279b | 2011-12-26 19:39:25 -0800 | [diff] [blame] | 1490 | tfeedback_decl::init(struct gl_context *ctx, struct gl_shader_program *prog, |
| 1491 | const void *mem_ctx, const char *input) |
Paul Berry | 871ddb9 | 2011-11-05 11:17:32 -0700 | [diff] [blame] | 1492 | { |
| 1493 | /* We don't have to be pedantic about what is a valid GLSL variable name, |
| 1494 | * because any variable with an invalid name can't exist in the IR anyway. |
| 1495 | */ |
| 1496 | |
| 1497 | this->location = -1; |
| 1498 | this->orig_name = input; |
Paul Berry | 642e5b41 | 2012-01-04 13:57:52 -0800 | [diff] [blame] | 1499 | this->is_clip_distance_mesa = false; |
Paul Berry | 871ddb9 | 2011-11-05 11:17:32 -0700 | [diff] [blame] | 1500 | |
| 1501 | const char *bracket = strrchr(input, '['); |
| 1502 | |
| 1503 | if (bracket) { |
| 1504 | this->var_name = ralloc_strndup(mem_ctx, input, bracket - input); |
Paul Berry | 33fe021 | 2012-01-03 20:41:34 -0800 | [diff] [blame] | 1505 | if (sscanf(bracket, "[%u]", &this->array_subscript) != 1) { |
Paul Berry | 456279b | 2011-12-26 19:39:25 -0800 | [diff] [blame] | 1506 | linker_error(prog, "Cannot parse transform feedback varying %s", input); |
| 1507 | return false; |
Paul Berry | 871ddb9 | 2011-11-05 11:17:32 -0700 | [diff] [blame] | 1508 | } |
Paul Berry | 33fe021 | 2012-01-03 20:41:34 -0800 | [diff] [blame] | 1509 | this->is_subscripted = true; |
Paul Berry | 871ddb9 | 2011-11-05 11:17:32 -0700 | [diff] [blame] | 1510 | } else { |
| 1511 | this->var_name = ralloc_strdup(mem_ctx, input); |
Paul Berry | 33fe021 | 2012-01-03 20:41:34 -0800 | [diff] [blame] | 1512 | this->is_subscripted = false; |
Paul Berry | 871ddb9 | 2011-11-05 11:17:32 -0700 | [diff] [blame] | 1513 | } |
| 1514 | |
Paul Berry | 642e5b41 | 2012-01-04 13:57:52 -0800 | [diff] [blame] | 1515 | /* For drivers that lower gl_ClipDistance to gl_ClipDistanceMESA, this |
| 1516 | * class must behave specially to account for the fact that gl_ClipDistance |
| 1517 | * is converted from a float[8] to a vec4[2]. |
Paul Berry | 456279b | 2011-12-26 19:39:25 -0800 | [diff] [blame] | 1518 | */ |
| 1519 | if (ctx->ShaderCompilerOptions[MESA_SHADER_VERTEX].LowerClipDistance && |
| 1520 | strcmp(this->var_name, "gl_ClipDistance") == 0) { |
Paul Berry | 642e5b41 | 2012-01-04 13:57:52 -0800 | [diff] [blame] | 1521 | this->is_clip_distance_mesa = true; |
Paul Berry | 456279b | 2011-12-26 19:39:25 -0800 | [diff] [blame] | 1522 | } |
| 1523 | |
| 1524 | return true; |
Paul Berry | 871ddb9 | 2011-11-05 11:17:32 -0700 | [diff] [blame] | 1525 | } |
| 1526 | |
| 1527 | |
| 1528 | /** |
| 1529 | * Determine whether two tfeedback_decl objects refer to the same variable and |
| 1530 | * array index (if applicable). |
| 1531 | */ |
| 1532 | bool |
| 1533 | tfeedback_decl::is_same(const tfeedback_decl &x, const tfeedback_decl &y) |
| 1534 | { |
| 1535 | if (strcmp(x.var_name, y.var_name) != 0) |
| 1536 | return false; |
Paul Berry | 33fe021 | 2012-01-03 20:41:34 -0800 | [diff] [blame] | 1537 | if (x.is_subscripted != y.is_subscripted) |
Paul Berry | 871ddb9 | 2011-11-05 11:17:32 -0700 | [diff] [blame] | 1538 | return false; |
Paul Berry | 33fe021 | 2012-01-03 20:41:34 -0800 | [diff] [blame] | 1539 | if (x.is_subscripted && x.array_subscript != y.array_subscript) |
Paul Berry | 871ddb9 | 2011-11-05 11:17:32 -0700 | [diff] [blame] | 1540 | return false; |
| 1541 | return true; |
| 1542 | } |
| 1543 | |
| 1544 | |
| 1545 | /** |
| 1546 | * Assign a location for this tfeedback_decl object based on the location |
| 1547 | * assignment in output_var. |
| 1548 | * |
| 1549 | * If an error occurs, the error is reported through linker_error() and false |
| 1550 | * is returned. |
| 1551 | */ |
| 1552 | bool |
| 1553 | tfeedback_decl::assign_location(struct gl_context *ctx, |
| 1554 | struct gl_shader_program *prog, |
| 1555 | ir_variable *output_var) |
| 1556 | { |
| 1557 | if (output_var->type->is_array()) { |
| 1558 | /* Array variable */ |
Paul Berry | 871ddb9 | 2011-11-05 11:17:32 -0700 | [diff] [blame] | 1559 | const unsigned matrix_cols = |
| 1560 | output_var->type->fields.array->matrix_columns; |
Paul Berry | 642e5b41 | 2012-01-04 13:57:52 -0800 | [diff] [blame] | 1561 | unsigned actual_array_size = this->is_clip_distance_mesa ? |
| 1562 | prog->Vert.ClipDistanceArraySize : output_var->type->array_size(); |
Paul Berry | 33fe021 | 2012-01-03 20:41:34 -0800 | [diff] [blame] | 1563 | |
| 1564 | if (this->is_subscripted) { |
| 1565 | /* Check array bounds. */ |
Paul Berry | 642e5b41 | 2012-01-04 13:57:52 -0800 | [diff] [blame] | 1566 | if (this->array_subscript >= actual_array_size) { |
Paul Berry | 33fe021 | 2012-01-03 20:41:34 -0800 | [diff] [blame] | 1567 | linker_error(prog, "Transform feedback varying %s has index " |
Paul Berry | 642e5b41 | 2012-01-04 13:57:52 -0800 | [diff] [blame] | 1568 | "%i, but the array size is %u.", |
Paul Berry | 33fe021 | 2012-01-03 20:41:34 -0800 | [diff] [blame] | 1569 | this->orig_name, this->array_subscript, |
Paul Berry | 642e5b41 | 2012-01-04 13:57:52 -0800 | [diff] [blame] | 1570 | actual_array_size); |
Paul Berry | 33fe021 | 2012-01-03 20:41:34 -0800 | [diff] [blame] | 1571 | return false; |
| 1572 | } |
Paul Berry | 642e5b41 | 2012-01-04 13:57:52 -0800 | [diff] [blame] | 1573 | if (this->is_clip_distance_mesa) { |
| 1574 | this->location = |
| 1575 | output_var->location + this->array_subscript / 4; |
| 1576 | } else { |
| 1577 | this->location = |
| 1578 | output_var->location + this->array_subscript * matrix_cols; |
| 1579 | } |
Paul Berry | 33fe021 | 2012-01-03 20:41:34 -0800 | [diff] [blame] | 1580 | this->size = 1; |
| 1581 | } else { |
| 1582 | this->location = output_var->location; |
Paul Berry | 642e5b41 | 2012-01-04 13:57:52 -0800 | [diff] [blame] | 1583 | this->size = actual_array_size; |
Paul Berry | 33fe021 | 2012-01-03 20:41:34 -0800 | [diff] [blame] | 1584 | } |
Paul Berry | 871ddb9 | 2011-11-05 11:17:32 -0700 | [diff] [blame] | 1585 | this->vector_elements = output_var->type->fields.array->vector_elements; |
| 1586 | this->matrix_columns = matrix_cols; |
Paul Berry | 642e5b41 | 2012-01-04 13:57:52 -0800 | [diff] [blame] | 1587 | if (this->is_clip_distance_mesa) |
| 1588 | this->type = GL_FLOAT; |
| 1589 | else |
| 1590 | this->type = output_var->type->fields.array->gl_type; |
Paul Berry | 871ddb9 | 2011-11-05 11:17:32 -0700 | [diff] [blame] | 1591 | } else { |
| 1592 | /* Regular variable (scalar, vector, or matrix) */ |
Paul Berry | 33fe021 | 2012-01-03 20:41:34 -0800 | [diff] [blame] | 1593 | if (this->is_subscripted) { |
Paul Berry | 108cba2 | 2012-01-04 15:17:52 -0800 | [diff] [blame] | 1594 | linker_error(prog, "Transform feedback varying %s requested, " |
| 1595 | "but %s is not an array.", |
| 1596 | this->orig_name, this->var_name); |
Paul Berry | 871ddb9 | 2011-11-05 11:17:32 -0700 | [diff] [blame] | 1597 | return false; |
| 1598 | } |
| 1599 | this->location = output_var->location; |
Paul Berry | 33fe021 | 2012-01-03 20:41:34 -0800 | [diff] [blame] | 1600 | this->size = 1; |
Paul Berry | 871ddb9 | 2011-11-05 11:17:32 -0700 | [diff] [blame] | 1601 | this->vector_elements = output_var->type->vector_elements; |
| 1602 | this->matrix_columns = output_var->type->matrix_columns; |
Eric Anholt | 9d36c96 | 2012-01-02 17:08:13 -0800 | [diff] [blame] | 1603 | this->type = output_var->type->gl_type; |
Paul Berry | 871ddb9 | 2011-11-05 11:17:32 -0700 | [diff] [blame] | 1604 | } |
Paul Berry | 642e5b41 | 2012-01-04 13:57:52 -0800 | [diff] [blame] | 1605 | |
Paul Berry | 871ddb9 | 2011-11-05 11:17:32 -0700 | [diff] [blame] | 1606 | /* From GL_EXT_transform_feedback: |
| 1607 | * A program will fail to link if: |
| 1608 | * |
| 1609 | * * the total number of components to capture in any varying |
| 1610 | * variable in <varyings> is greater than the constant |
| 1611 | * MAX_TRANSFORM_FEEDBACK_SEPARATE_COMPONENTS_EXT and the |
| 1612 | * buffer mode is SEPARATE_ATTRIBS_EXT; |
| 1613 | */ |
| 1614 | if (prog->TransformFeedback.BufferMode == GL_SEPARATE_ATTRIBS && |
| 1615 | this->num_components() > |
| 1616 | ctx->Const.MaxTransformFeedbackSeparateComponents) { |
| 1617 | linker_error(prog, "Transform feedback varying %s exceeds " |
| 1618 | "MAX_TRANSFORM_FEEDBACK_SEPARATE_COMPONENTS.", |
| 1619 | this->orig_name); |
| 1620 | return false; |
| 1621 | } |
| 1622 | |
| 1623 | return true; |
| 1624 | } |
| 1625 | |
| 1626 | |
| 1627 | /** |
| 1628 | * Update gl_transform_feedback_info to reflect this tfeedback_decl. |
| 1629 | * |
| 1630 | * If an error occurs, the error is reported through linker_error() and false |
| 1631 | * is returned. |
| 1632 | */ |
| 1633 | bool |
Paul Berry | d3150eb | 2012-01-09 11:25:14 -0800 | [diff] [blame] | 1634 | tfeedback_decl::store(struct gl_context *ctx, struct gl_shader_program *prog, |
Paul Berry | 871ddb9 | 2011-11-05 11:17:32 -0700 | [diff] [blame] | 1635 | struct gl_transform_feedback_info *info, |
Eric Anholt | 9d36c96 | 2012-01-02 17:08:13 -0800 | [diff] [blame] | 1636 | unsigned buffer, unsigned varying) const |
Paul Berry | 871ddb9 | 2011-11-05 11:17:32 -0700 | [diff] [blame] | 1637 | { |
| 1638 | if (!this->is_assigned()) { |
| 1639 | /* From GL_EXT_transform_feedback: |
| 1640 | * A program will fail to link if: |
| 1641 | * |
| 1642 | * * any variable name specified in the <varyings> array is not |
| 1643 | * declared as an output in the geometry shader (if present) or |
| 1644 | * the vertex shader (if no geometry shader is present); |
| 1645 | */ |
| 1646 | linker_error(prog, "Transform feedback varying %s undeclared.", |
| 1647 | this->orig_name); |
| 1648 | return false; |
| 1649 | } |
Paul Berry | d3150eb | 2012-01-09 11:25:14 -0800 | [diff] [blame] | 1650 | |
| 1651 | /* From GL_EXT_transform_feedback: |
| 1652 | * A program will fail to link if: |
| 1653 | * |
| 1654 | * * the total number of components to capture is greater than |
| 1655 | * the constant MAX_TRANSFORM_FEEDBACK_INTERLEAVED_COMPONENTS_EXT |
| 1656 | * and the buffer mode is INTERLEAVED_ATTRIBS_EXT. |
| 1657 | */ |
| 1658 | if (prog->TransformFeedback.BufferMode == GL_INTERLEAVED_ATTRIBS && |
| 1659 | info->BufferStride[buffer] + this->num_components() > |
| 1660 | ctx->Const.MaxTransformFeedbackInterleavedComponents) { |
| 1661 | linker_error(prog, "The MAX_TRANSFORM_FEEDBACK_INTERLEAVED_COMPONENTS " |
| 1662 | "limit has been exceeded."); |
| 1663 | return false; |
| 1664 | } |
| 1665 | |
| 1666 | /* Verify that the checks on MAX_TRANSFORM_FEEDBACK_INTERLEAVED_COMPONENTS |
| 1667 | * and MAX_TRANSFORM_FEEDBACK_SEPARATE_COMPONENTS are sufficient to prevent |
| 1668 | * overflow of info->Outputs[]. In worst case we generate one entry in |
| 1669 | * Outputs[] per component so a conservative check is to verify that the |
| 1670 | * size of the array is greater than or equal to both |
| 1671 | * MAX_TRANSFORM_FEEDBACK_INTERLEAVED_COMPONENTS and |
| 1672 | * MAX_TRANSFORM_FEEDBACK_SEPARATE_COMPONENTS. |
| 1673 | */ |
| 1674 | assert(Elements(info->Outputs) >= |
| 1675 | ctx->Const.MaxTransformFeedbackInterleavedComponents); |
| 1676 | assert(Elements(info->Outputs) >= |
| 1677 | ctx->Const.MaxTransformFeedbackSeparateComponents); |
| 1678 | |
Paul Berry | 642e5b41 | 2012-01-04 13:57:52 -0800 | [diff] [blame] | 1679 | unsigned translated_size = this->size; |
| 1680 | if (this->is_clip_distance_mesa) |
| 1681 | translated_size = (translated_size + 3) / 4; |
Paul Berry | be4e9f7 | 2012-01-04 12:21:55 -0800 | [diff] [blame] | 1682 | unsigned components_so_far = 0; |
Paul Berry | 642e5b41 | 2012-01-04 13:57:52 -0800 | [diff] [blame] | 1683 | for (unsigned index = 0; index < translated_size; ++index) { |
Paul Berry | 33fe021 | 2012-01-03 20:41:34 -0800 | [diff] [blame] | 1684 | for (unsigned v = 0; v < this->matrix_columns; ++v) { |
Paul Berry | 642e5b41 | 2012-01-04 13:57:52 -0800 | [diff] [blame] | 1685 | unsigned num_components = this->vector_elements; |
| 1686 | info->Outputs[info->NumOutputs].ComponentOffset = 0; |
| 1687 | if (this->is_clip_distance_mesa) { |
| 1688 | if (this->is_subscripted) { |
| 1689 | num_components = 1; |
| 1690 | info->Outputs[info->NumOutputs].ComponentOffset = |
| 1691 | this->array_subscript % 4; |
| 1692 | } else { |
| 1693 | num_components = MIN2(4, this->size - components_so_far); |
| 1694 | } |
| 1695 | } |
Paul Berry | 33fe021 | 2012-01-03 20:41:34 -0800 | [diff] [blame] | 1696 | info->Outputs[info->NumOutputs].OutputRegister = |
| 1697 | this->location + v + index * this->matrix_columns; |
| 1698 | info->Outputs[info->NumOutputs].NumComponents = num_components; |
| 1699 | info->Outputs[info->NumOutputs].OutputBuffer = buffer; |
| 1700 | info->Outputs[info->NumOutputs].DstOffset = info->BufferStride[buffer]; |
Paul Berry | 33fe021 | 2012-01-03 20:41:34 -0800 | [diff] [blame] | 1701 | ++info->NumOutputs; |
| 1702 | info->BufferStride[buffer] += num_components; |
Paul Berry | be4e9f7 | 2012-01-04 12:21:55 -0800 | [diff] [blame] | 1703 | components_so_far += num_components; |
Paul Berry | 33fe021 | 2012-01-03 20:41:34 -0800 | [diff] [blame] | 1704 | } |
Paul Berry | 871ddb9 | 2011-11-05 11:17:32 -0700 | [diff] [blame] | 1705 | } |
Paul Berry | be4e9f7 | 2012-01-04 12:21:55 -0800 | [diff] [blame] | 1706 | assert(components_so_far == this->num_components()); |
Eric Anholt | 9d36c96 | 2012-01-02 17:08:13 -0800 | [diff] [blame] | 1707 | |
| 1708 | info->Varyings[varying].Name = ralloc_strdup(prog, this->orig_name); |
| 1709 | info->Varyings[varying].Type = this->type; |
Paul Berry | 33fe021 | 2012-01-03 20:41:34 -0800 | [diff] [blame] | 1710 | info->Varyings[varying].Size = this->size; |
Eric Anholt | 9d36c96 | 2012-01-02 17:08:13 -0800 | [diff] [blame] | 1711 | info->NumVarying++; |
| 1712 | |
Paul Berry | 871ddb9 | 2011-11-05 11:17:32 -0700 | [diff] [blame] | 1713 | return true; |
| 1714 | } |
| 1715 | |
| 1716 | |
| 1717 | /** |
| 1718 | * Parse all the transform feedback declarations that were passed to |
| 1719 | * glTransformFeedbackVaryings() and store them in tfeedback_decl objects. |
| 1720 | * |
| 1721 | * If an error occurs, the error is reported through linker_error() and false |
| 1722 | * is returned. |
| 1723 | */ |
| 1724 | static bool |
Paul Berry | 456279b | 2011-12-26 19:39:25 -0800 | [diff] [blame] | 1725 | parse_tfeedback_decls(struct gl_context *ctx, struct gl_shader_program *prog, |
| 1726 | const void *mem_ctx, unsigned num_names, |
| 1727 | char **varying_names, tfeedback_decl *decls) |
Paul Berry | 871ddb9 | 2011-11-05 11:17:32 -0700 | [diff] [blame] | 1728 | { |
| 1729 | for (unsigned i = 0; i < num_names; ++i) { |
Paul Berry | 456279b | 2011-12-26 19:39:25 -0800 | [diff] [blame] | 1730 | if (!decls[i].init(ctx, prog, mem_ctx, varying_names[i])) |
Paul Berry | 871ddb9 | 2011-11-05 11:17:32 -0700 | [diff] [blame] | 1731 | return false; |
| 1732 | /* From GL_EXT_transform_feedback: |
| 1733 | * A program will fail to link if: |
| 1734 | * |
| 1735 | * * any two entries in the <varyings> array specify the same varying |
| 1736 | * variable; |
| 1737 | * |
| 1738 | * We interpret this to mean "any two entries in the <varyings> array |
| 1739 | * specify the same varying variable and array index", since transform |
| 1740 | * feedback of arrays would be useless otherwise. |
| 1741 | */ |
| 1742 | for (unsigned j = 0; j < i; ++j) { |
| 1743 | if (tfeedback_decl::is_same(decls[i], decls[j])) { |
| 1744 | linker_error(prog, "Transform feedback varying %s specified " |
| 1745 | "more than once.", varying_names[i]); |
| 1746 | return false; |
| 1747 | } |
| 1748 | } |
| 1749 | } |
| 1750 | return true; |
| 1751 | } |
| 1752 | |
| 1753 | |
| 1754 | /** |
| 1755 | * Assign a location for a variable that is produced in one pipeline stage |
| 1756 | * (the "producer") and consumed in the next stage (the "consumer"). |
| 1757 | * |
| 1758 | * \param input_var is the input variable declaration in the consumer. |
| 1759 | * |
| 1760 | * \param output_var is the output variable declaration in the producer. |
| 1761 | * |
| 1762 | * \param input_index is the counter that keeps track of assigned input |
| 1763 | * locations in the consumer. |
| 1764 | * |
| 1765 | * \param output_index is the counter that keeps track of assigned output |
| 1766 | * locations in the producer. |
| 1767 | * |
| 1768 | * It is permissible for \c input_var to be NULL (this happens if a variable |
| 1769 | * is output by the producer and consumed by transform feedback, but not |
| 1770 | * consumed by the consumer). |
| 1771 | * |
| 1772 | * If the variable has already been assigned a location, this function has no |
| 1773 | * effect. |
| 1774 | */ |
| 1775 | void |
| 1776 | assign_varying_location(ir_variable *input_var, ir_variable *output_var, |
| 1777 | unsigned *input_index, unsigned *output_index) |
| 1778 | { |
| 1779 | if (output_var->location != -1) { |
| 1780 | /* Location already assigned. */ |
| 1781 | return; |
| 1782 | } |
| 1783 | |
| 1784 | if (input_var) { |
| 1785 | assert(input_var->location == -1); |
| 1786 | input_var->location = *input_index; |
| 1787 | } |
| 1788 | |
| 1789 | output_var->location = *output_index; |
| 1790 | |
| 1791 | /* FINISHME: Support for "varying" records in GLSL 1.50. */ |
| 1792 | assert(!output_var->type->is_record()); |
| 1793 | |
| 1794 | if (output_var->type->is_array()) { |
| 1795 | const unsigned slots = output_var->type->length |
| 1796 | * output_var->type->fields.array->matrix_columns; |
| 1797 | |
| 1798 | *output_index += slots; |
| 1799 | *input_index += slots; |
| 1800 | } else { |
| 1801 | const unsigned slots = output_var->type->matrix_columns; |
| 1802 | |
| 1803 | *output_index += slots; |
| 1804 | *input_index += slots; |
| 1805 | } |
| 1806 | } |
| 1807 | |
| 1808 | |
| 1809 | /** |
| 1810 | * Assign locations for all variables that are produced in one pipeline stage |
| 1811 | * (the "producer") and consumed in the next stage (the "consumer"). |
| 1812 | * |
| 1813 | * Variables produced by the producer may also be consumed by transform |
| 1814 | * feedback. |
| 1815 | * |
| 1816 | * \param num_tfeedback_decls is the number of declarations indicating |
| 1817 | * variables that may be consumed by transform feedback. |
| 1818 | * |
| 1819 | * \param tfeedback_decls is a pointer to an array of tfeedback_decl objects |
| 1820 | * representing the result of parsing the strings passed to |
| 1821 | * glTransformFeedbackVaryings(). assign_location() will be called for |
| 1822 | * each of these objects that matches one of the outputs of the |
| 1823 | * producer. |
| 1824 | * |
| 1825 | * When num_tfeedback_decls is nonzero, it is permissible for the consumer to |
| 1826 | * be NULL. In this case, varying locations are assigned solely based on the |
| 1827 | * requirements of transform feedback. |
| 1828 | */ |
Ian Romanick | de77324 | 2011-06-09 13:31:32 -0700 | [diff] [blame] | 1829 | bool |
| 1830 | assign_varying_locations(struct gl_context *ctx, |
| 1831 | struct gl_shader_program *prog, |
Paul Berry | 871ddb9 | 2011-11-05 11:17:32 -0700 | [diff] [blame] | 1832 | gl_shader *producer, gl_shader *consumer, |
| 1833 | unsigned num_tfeedback_decls, |
| 1834 | tfeedback_decl *tfeedback_decls) |
Ian Romanick | 0e59b26 | 2010-06-23 11:23:01 -0700 | [diff] [blame] | 1835 | { |
| 1836 | /* FINISHME: Set dynamically when geometry shader support is added. */ |
| 1837 | unsigned output_index = VERT_RESULT_VAR0; |
| 1838 | unsigned input_index = FRAG_ATTRIB_VAR0; |
| 1839 | |
| 1840 | /* Operate in a total of three passes. |
| 1841 | * |
| 1842 | * 1. Assign locations for any matching inputs and outputs. |
| 1843 | * |
| 1844 | * 2. Mark output variables in the producer that do not have locations as |
| 1845 | * not being outputs. This lets the optimizer eliminate them. |
| 1846 | * |
| 1847 | * 3. Mark input variables in the consumer that do not have locations as |
| 1848 | * not being inputs. This lets the optimizer eliminate them. |
| 1849 | */ |
| 1850 | |
Ian Romanick | f6ee7bc | 2011-10-11 16:15:47 -0700 | [diff] [blame] | 1851 | link_invalidate_variable_locations(producer, ir_var_out, VERT_RESULT_VAR0); |
Paul Berry | 871ddb9 | 2011-11-05 11:17:32 -0700 | [diff] [blame] | 1852 | if (consumer) |
| 1853 | link_invalidate_variable_locations(consumer, ir_var_in, FRAG_ATTRIB_VAR0); |
Ian Romanick | 0e59b26 | 2010-06-23 11:23:01 -0700 | [diff] [blame] | 1854 | |
Eric Anholt | 16b68b1 | 2010-06-30 11:05:43 -0700 | [diff] [blame] | 1855 | foreach_list(node, producer->ir) { |
Ian Romanick | 0e59b26 | 2010-06-23 11:23:01 -0700 | [diff] [blame] | 1856 | ir_variable *const output_var = ((ir_instruction *) node)->as_variable(); |
| 1857 | |
Paul Berry | 871ddb9 | 2011-11-05 11:17:32 -0700 | [diff] [blame] | 1858 | if ((output_var == NULL) || (output_var->mode != ir_var_out)) |
Ian Romanick | 0e59b26 | 2010-06-23 11:23:01 -0700 | [diff] [blame] | 1859 | continue; |
| 1860 | |
Paul Berry | 871ddb9 | 2011-11-05 11:17:32 -0700 | [diff] [blame] | 1861 | ir_variable *input_var = |
| 1862 | consumer ? consumer->symbols->get_variable(output_var->name) : NULL; |
Ian Romanick | 0e59b26 | 2010-06-23 11:23:01 -0700 | [diff] [blame] | 1863 | |
Paul Berry | 871ddb9 | 2011-11-05 11:17:32 -0700 | [diff] [blame] | 1864 | if (input_var && input_var->mode != ir_var_in) |
| 1865 | input_var = NULL; |
Ian Romanick | 0e59b26 | 2010-06-23 11:23:01 -0700 | [diff] [blame] | 1866 | |
Paul Berry | 871ddb9 | 2011-11-05 11:17:32 -0700 | [diff] [blame] | 1867 | if (input_var) { |
| 1868 | assign_varying_location(input_var, output_var, &input_index, |
| 1869 | &output_index); |
| 1870 | } |
Ian Romanick | 0e59b26 | 2010-06-23 11:23:01 -0700 | [diff] [blame] | 1871 | |
Paul Berry | 871ddb9 | 2011-11-05 11:17:32 -0700 | [diff] [blame] | 1872 | for (unsigned i = 0; i < num_tfeedback_decls; ++i) { |
| 1873 | if (!tfeedback_decls[i].is_assigned() && |
| 1874 | tfeedback_decls[i].matches_var(output_var)) { |
| 1875 | if (output_var->location == -1) { |
| 1876 | assign_varying_location(input_var, output_var, &input_index, |
| 1877 | &output_index); |
| 1878 | } |
| 1879 | if (!tfeedback_decls[i].assign_location(ctx, prog, output_var)) |
| 1880 | return false; |
| 1881 | } |
Ian Romanick | df869d9 | 2010-08-30 15:37:44 -0700 | [diff] [blame] | 1882 | } |
Ian Romanick | 0e59b26 | 2010-06-23 11:23:01 -0700 | [diff] [blame] | 1883 | } |
| 1884 | |
Ian Romanick | de77324 | 2011-06-09 13:31:32 -0700 | [diff] [blame] | 1885 | unsigned varying_vectors = 0; |
| 1886 | |
Paul Berry | 871ddb9 | 2011-11-05 11:17:32 -0700 | [diff] [blame] | 1887 | if (consumer) { |
| 1888 | foreach_list(node, consumer->ir) { |
| 1889 | ir_variable *const var = ((ir_instruction *) node)->as_variable(); |
Ian Romanick | 0e59b26 | 2010-06-23 11:23:01 -0700 | [diff] [blame] | 1890 | |
Paul Berry | 871ddb9 | 2011-11-05 11:17:32 -0700 | [diff] [blame] | 1891 | if ((var == NULL) || (var->mode != ir_var_in)) |
| 1892 | continue; |
Ian Romanick | 0e59b26 | 2010-06-23 11:23:01 -0700 | [diff] [blame] | 1893 | |
Paul Berry | 871ddb9 | 2011-11-05 11:17:32 -0700 | [diff] [blame] | 1894 | if (var->location == -1) { |
| 1895 | if (prog->Version <= 120) { |
| 1896 | /* On page 25 (page 31 of the PDF) of the GLSL 1.20 spec: |
| 1897 | * |
| 1898 | * Only those varying variables used (i.e. read) in |
| 1899 | * the fragment shader executable must be written to |
| 1900 | * by the vertex shader executable; declaring |
| 1901 | * superfluous varying variables in a vertex shader is |
| 1902 | * permissible. |
| 1903 | * |
| 1904 | * We interpret this text as meaning that the VS must |
| 1905 | * write the variable for the FS to read it. See |
| 1906 | * "glsl1-varying read but not written" in piglit. |
| 1907 | */ |
Eric Anholt | b706283 | 2010-07-28 13:52:23 -0700 | [diff] [blame] | 1908 | |
Paul Berry | 871ddb9 | 2011-11-05 11:17:32 -0700 | [diff] [blame] | 1909 | linker_error(prog, "fragment shader varying %s not written " |
| 1910 | "by vertex shader\n.", var->name); |
| 1911 | } |
Eric Anholt | b706283 | 2010-07-28 13:52:23 -0700 | [diff] [blame] | 1912 | |
Paul Berry | 871ddb9 | 2011-11-05 11:17:32 -0700 | [diff] [blame] | 1913 | /* An 'in' variable is only really a shader input if its |
| 1914 | * value is written by the previous stage. |
| 1915 | */ |
| 1916 | var->mode = ir_var_auto; |
| 1917 | } else { |
| 1918 | /* The packing rules are used for vertex shader inputs are also |
| 1919 | * used for fragment shader inputs. |
| 1920 | */ |
| 1921 | varying_vectors += count_attribute_slots(var->type); |
| 1922 | } |
Eric Anholt | b706283 | 2010-07-28 13:52:23 -0700 | [diff] [blame] | 1923 | } |
Ian Romanick | 0e59b26 | 2010-06-23 11:23:01 -0700 | [diff] [blame] | 1924 | } |
Ian Romanick | de77324 | 2011-06-09 13:31:32 -0700 | [diff] [blame] | 1925 | |
| 1926 | if (ctx->API == API_OPENGLES2 || prog->Version == 100) { |
| 1927 | if (varying_vectors > ctx->Const.MaxVarying) { |
Marek Olšák | df809ae | 2011-12-10 04:14:46 +0100 | [diff] [blame] | 1928 | if (ctx->Const.GLSLSkipStrictMaxVaryingLimitCheck) { |
| 1929 | linker_warning(prog, "shader uses too many varying vectors " |
| 1930 | "(%u > %u), but the driver will try to optimize " |
| 1931 | "them out; this is non-portable out-of-spec " |
| 1932 | "behavior\n", |
| 1933 | varying_vectors, ctx->Const.MaxVarying); |
| 1934 | } else { |
| 1935 | linker_error(prog, "shader uses too many varying vectors " |
| 1936 | "(%u > %u)\n", |
| 1937 | varying_vectors, ctx->Const.MaxVarying); |
| 1938 | return false; |
| 1939 | } |
Ian Romanick | de77324 | 2011-06-09 13:31:32 -0700 | [diff] [blame] | 1940 | } |
| 1941 | } else { |
| 1942 | const unsigned float_components = varying_vectors * 4; |
| 1943 | if (float_components > ctx->Const.MaxVarying * 4) { |
Marek Olšák | df809ae | 2011-12-10 04:14:46 +0100 | [diff] [blame] | 1944 | if (ctx->Const.GLSLSkipStrictMaxVaryingLimitCheck) { |
| 1945 | linker_warning(prog, "shader uses too many varying components " |
| 1946 | "(%u > %u), but the driver will try to optimize " |
| 1947 | "them out; this is non-portable out-of-spec " |
| 1948 | "behavior\n", |
| 1949 | float_components, ctx->Const.MaxVarying * 4); |
| 1950 | } else { |
| 1951 | linker_error(prog, "shader uses too many varying components " |
| 1952 | "(%u > %u)\n", |
| 1953 | float_components, ctx->Const.MaxVarying * 4); |
| 1954 | return false; |
| 1955 | } |
Ian Romanick | de77324 | 2011-06-09 13:31:32 -0700 | [diff] [blame] | 1956 | } |
| 1957 | } |
| 1958 | |
| 1959 | return true; |
Ian Romanick | 0e59b26 | 2010-06-23 11:23:01 -0700 | [diff] [blame] | 1960 | } |
| 1961 | |
| 1962 | |
Paul Berry | 871ddb9 | 2011-11-05 11:17:32 -0700 | [diff] [blame] | 1963 | /** |
| 1964 | * Store transform feedback location assignments into |
| 1965 | * prog->LinkedTransformFeedback based on the data stored in tfeedback_decls. |
| 1966 | * |
| 1967 | * If an error occurs, the error is reported through linker_error() and false |
| 1968 | * is returned. |
| 1969 | */ |
| 1970 | static bool |
| 1971 | store_tfeedback_info(struct gl_context *ctx, struct gl_shader_program *prog, |
| 1972 | unsigned num_tfeedback_decls, |
| 1973 | tfeedback_decl *tfeedback_decls) |
| 1974 | { |
Paul Berry | ebfad9f | 2011-12-29 15:55:01 -0800 | [diff] [blame] | 1975 | bool separate_attribs_mode = |
| 1976 | prog->TransformFeedback.BufferMode == GL_SEPARATE_ATTRIBS; |
Eric Anholt | 9d36c96 | 2012-01-02 17:08:13 -0800 | [diff] [blame] | 1977 | |
| 1978 | ralloc_free(prog->LinkedTransformFeedback.Varyings); |
| 1979 | |
| 1980 | memset(&prog->LinkedTransformFeedback, 0, |
| 1981 | sizeof(prog->LinkedTransformFeedback)); |
| 1982 | |
Paul Berry | 1be0fd8 | 2012-01-05 13:06:36 -0800 | [diff] [blame] | 1983 | prog->LinkedTransformFeedback.NumBuffers = |
| 1984 | separate_attribs_mode ? num_tfeedback_decls : 1; |
| 1985 | |
Eric Anholt | 9d36c96 | 2012-01-02 17:08:13 -0800 | [diff] [blame] | 1986 | prog->LinkedTransformFeedback.Varyings = |
Eric Anholt | 5a0f395 | 2012-01-12 13:10:26 -0800 | [diff] [blame^] | 1987 | rzalloc_array(prog, |
Eric Anholt | 9d36c96 | 2012-01-02 17:08:13 -0800 | [diff] [blame] | 1988 | struct gl_transform_feedback_varying_info, |
| 1989 | num_tfeedback_decls); |
| 1990 | |
Paul Berry | 871ddb9 | 2011-11-05 11:17:32 -0700 | [diff] [blame] | 1991 | for (unsigned i = 0; i < num_tfeedback_decls; ++i) { |
Paul Berry | ebfad9f | 2011-12-29 15:55:01 -0800 | [diff] [blame] | 1992 | unsigned buffer = separate_attribs_mode ? i : 0; |
Paul Berry | d3150eb | 2012-01-09 11:25:14 -0800 | [diff] [blame] | 1993 | if (!tfeedback_decls[i].store(ctx, prog, &prog->LinkedTransformFeedback, |
Eric Anholt | 9d36c96 | 2012-01-02 17:08:13 -0800 | [diff] [blame] | 1994 | buffer, i)) |
Paul Berry | 871ddb9 | 2011-11-05 11:17:32 -0700 | [diff] [blame] | 1995 | return false; |
Paul Berry | 871ddb9 | 2011-11-05 11:17:32 -0700 | [diff] [blame] | 1996 | } |
| 1997 | |
| 1998 | return true; |
| 1999 | } |
| 2000 | |
Ian Romanick | 92f8159 | 2011-11-08 12:37:19 -0800 | [diff] [blame] | 2001 | /** |
Marek Olšák | ec174a4 | 2011-11-18 15:00:10 +0100 | [diff] [blame] | 2002 | * Store the gl_FragDepth layout in the gl_shader_program struct. |
| 2003 | */ |
| 2004 | static void |
| 2005 | store_fragdepth_layout(struct gl_shader_program *prog) |
| 2006 | { |
| 2007 | if (prog->_LinkedShaders[MESA_SHADER_FRAGMENT] == NULL) { |
| 2008 | return; |
| 2009 | } |
| 2010 | |
| 2011 | struct exec_list *ir = prog->_LinkedShaders[MESA_SHADER_FRAGMENT]->ir; |
| 2012 | |
| 2013 | /* We don't look up the gl_FragDepth symbol directly because if |
| 2014 | * gl_FragDepth is not used in the shader, it's removed from the IR. |
| 2015 | * However, the symbol won't be removed from the symbol table. |
| 2016 | * |
| 2017 | * We're only interested in the cases where the variable is NOT removed |
| 2018 | * from the IR. |
| 2019 | */ |
| 2020 | foreach_list(node, ir) { |
| 2021 | ir_variable *const var = ((ir_instruction *) node)->as_variable(); |
| 2022 | |
| 2023 | if (var == NULL || var->mode != ir_var_out) { |
| 2024 | continue; |
| 2025 | } |
| 2026 | |
| 2027 | if (strcmp(var->name, "gl_FragDepth") == 0) { |
| 2028 | switch (var->depth_layout) { |
| 2029 | case ir_depth_layout_none: |
| 2030 | prog->FragDepthLayout = FRAG_DEPTH_LAYOUT_NONE; |
| 2031 | return; |
| 2032 | case ir_depth_layout_any: |
| 2033 | prog->FragDepthLayout = FRAG_DEPTH_LAYOUT_ANY; |
| 2034 | return; |
| 2035 | case ir_depth_layout_greater: |
| 2036 | prog->FragDepthLayout = FRAG_DEPTH_LAYOUT_GREATER; |
| 2037 | return; |
| 2038 | case ir_depth_layout_less: |
| 2039 | prog->FragDepthLayout = FRAG_DEPTH_LAYOUT_LESS; |
| 2040 | return; |
| 2041 | case ir_depth_layout_unchanged: |
| 2042 | prog->FragDepthLayout = FRAG_DEPTH_LAYOUT_UNCHANGED; |
| 2043 | return; |
| 2044 | default: |
| 2045 | assert(0); |
| 2046 | return; |
| 2047 | } |
| 2048 | } |
| 2049 | } |
| 2050 | } |
| 2051 | |
| 2052 | /** |
Ian Romanick | 92f8159 | 2011-11-08 12:37:19 -0800 | [diff] [blame] | 2053 | * Validate the resources used by a program versus the implementation limits |
| 2054 | */ |
| 2055 | static bool |
| 2056 | check_resources(struct gl_context *ctx, struct gl_shader_program *prog) |
| 2057 | { |
| 2058 | static const char *const shader_names[MESA_SHADER_TYPES] = { |
| 2059 | "vertex", "fragment", "geometry" |
| 2060 | }; |
| 2061 | |
| 2062 | const unsigned max_samplers[MESA_SHADER_TYPES] = { |
| 2063 | ctx->Const.MaxVertexTextureImageUnits, |
| 2064 | ctx->Const.MaxTextureImageUnits, |
| 2065 | ctx->Const.MaxGeometryTextureImageUnits |
| 2066 | }; |
| 2067 | |
| 2068 | const unsigned max_uniform_components[MESA_SHADER_TYPES] = { |
| 2069 | ctx->Const.VertexProgram.MaxUniformComponents, |
| 2070 | ctx->Const.FragmentProgram.MaxUniformComponents, |
| 2071 | 0 /* FINISHME: Geometry shaders. */ |
| 2072 | }; |
| 2073 | |
| 2074 | for (unsigned i = 0; i < MESA_SHADER_TYPES; i++) { |
| 2075 | struct gl_shader *sh = prog->_LinkedShaders[i]; |
| 2076 | |
| 2077 | if (sh == NULL) |
| 2078 | continue; |
| 2079 | |
| 2080 | if (sh->num_samplers > max_samplers[i]) { |
| 2081 | linker_error(prog, "Too many %s shader texture samplers", |
| 2082 | shader_names[i]); |
| 2083 | } |
| 2084 | |
| 2085 | if (sh->num_uniform_components > max_uniform_components[i]) { |
Marek Olšák | df809ae | 2011-12-10 04:14:46 +0100 | [diff] [blame] | 2086 | if (ctx->Const.GLSLSkipStrictMaxUniformLimitCheck) { |
| 2087 | linker_warning(prog, "Too many %s shader uniform components, " |
| 2088 | "but the driver will try to optimize them out; " |
| 2089 | "this is non-portable out-of-spec behavior\n", |
| 2090 | shader_names[i]); |
| 2091 | } else { |
| 2092 | linker_error(prog, "Too many %s shader uniform components", |
| 2093 | shader_names[i]); |
| 2094 | } |
Ian Romanick | 92f8159 | 2011-11-08 12:37:19 -0800 | [diff] [blame] | 2095 | } |
| 2096 | } |
| 2097 | |
| 2098 | return prog->LinkStatus; |
| 2099 | } |
Paul Berry | 871ddb9 | 2011-11-05 11:17:32 -0700 | [diff] [blame] | 2100 | |
Ian Romanick | 0e59b26 | 2010-06-23 11:23:01 -0700 | [diff] [blame] | 2101 | void |
Kristian Høgsberg | f9995b3 | 2010-10-12 12:26:10 -0400 | [diff] [blame] | 2102 | link_shaders(struct gl_context *ctx, struct gl_shader_program *prog) |
Ian Romanick | 832dfa5 | 2010-06-17 15:04:20 -0700 | [diff] [blame] | 2103 | { |
Paul Berry | 871ddb9 | 2011-11-05 11:17:32 -0700 | [diff] [blame] | 2104 | tfeedback_decl *tfeedback_decls = NULL; |
| 2105 | unsigned num_tfeedback_decls = prog->TransformFeedback.NumVarying; |
| 2106 | |
Kenneth Graunke | d3073f5 | 2011-01-21 14:32:31 -0800 | [diff] [blame] | 2107 | void *mem_ctx = ralloc_context(NULL); // temporary linker context |
Kenneth Graunke | 2da02e7 | 2010-11-17 11:03:57 -0800 | [diff] [blame] | 2108 | |
Ian Romanick | 832dfa5 | 2010-06-17 15:04:20 -0700 | [diff] [blame] | 2109 | prog->LinkStatus = false; |
| 2110 | prog->Validated = false; |
| 2111 | prog->_Used = false; |
| 2112 | |
Ian Romanick | f36460e | 2010-06-23 12:07:22 -0700 | [diff] [blame] | 2113 | if (prog->InfoLog != NULL) |
Kenneth Graunke | d3073f5 | 2011-01-21 14:32:31 -0800 | [diff] [blame] | 2114 | ralloc_free(prog->InfoLog); |
Ian Romanick | f36460e | 2010-06-23 12:07:22 -0700 | [diff] [blame] | 2115 | |
Kenneth Graunke | d3073f5 | 2011-01-21 14:32:31 -0800 | [diff] [blame] | 2116 | prog->InfoLog = ralloc_strdup(NULL, ""); |
Ian Romanick | f36460e | 2010-06-23 12:07:22 -0700 | [diff] [blame] | 2117 | |
Ian Romanick | 832dfa5 | 2010-06-17 15:04:20 -0700 | [diff] [blame] | 2118 | /* Separate the shaders into groups based on their type. |
| 2119 | */ |
Eric Anholt | 16b68b1 | 2010-06-30 11:05:43 -0700 | [diff] [blame] | 2120 | struct gl_shader **vert_shader_list; |
Ian Romanick | 832dfa5 | 2010-06-17 15:04:20 -0700 | [diff] [blame] | 2121 | unsigned num_vert_shaders = 0; |
Eric Anholt | 16b68b1 | 2010-06-30 11:05:43 -0700 | [diff] [blame] | 2122 | struct gl_shader **frag_shader_list; |
Ian Romanick | 832dfa5 | 2010-06-17 15:04:20 -0700 | [diff] [blame] | 2123 | unsigned num_frag_shaders = 0; |
| 2124 | |
Eric Anholt | 16b68b1 | 2010-06-30 11:05:43 -0700 | [diff] [blame] | 2125 | vert_shader_list = (struct gl_shader **) |
| 2126 | calloc(2 * prog->NumShaders, sizeof(struct gl_shader *)); |
Ian Romanick | 832dfa5 | 2010-06-17 15:04:20 -0700 | [diff] [blame] | 2127 | frag_shader_list = &vert_shader_list[prog->NumShaders]; |
| 2128 | |
Ian Romanick | 25f51d3 | 2010-07-16 15:51:50 -0700 | [diff] [blame] | 2129 | unsigned min_version = UINT_MAX; |
| 2130 | unsigned max_version = 0; |
Ian Romanick | 832dfa5 | 2010-06-17 15:04:20 -0700 | [diff] [blame] | 2131 | for (unsigned i = 0; i < prog->NumShaders; i++) { |
Ian Romanick | 25f51d3 | 2010-07-16 15:51:50 -0700 | [diff] [blame] | 2132 | min_version = MIN2(min_version, prog->Shaders[i]->Version); |
| 2133 | max_version = MAX2(max_version, prog->Shaders[i]->Version); |
| 2134 | |
Ian Romanick | 832dfa5 | 2010-06-17 15:04:20 -0700 | [diff] [blame] | 2135 | switch (prog->Shaders[i]->Type) { |
| 2136 | case GL_VERTEX_SHADER: |
| 2137 | vert_shader_list[num_vert_shaders] = prog->Shaders[i]; |
| 2138 | num_vert_shaders++; |
| 2139 | break; |
| 2140 | case GL_FRAGMENT_SHADER: |
| 2141 | frag_shader_list[num_frag_shaders] = prog->Shaders[i]; |
| 2142 | num_frag_shaders++; |
| 2143 | break; |
| 2144 | case GL_GEOMETRY_SHADER: |
| 2145 | /* FINISHME: Support geometry shaders. */ |
| 2146 | assert(prog->Shaders[i]->Type != GL_GEOMETRY_SHADER); |
| 2147 | break; |
| 2148 | } |
| 2149 | } |
| 2150 | |
Ian Romanick | 25f51d3 | 2010-07-16 15:51:50 -0700 | [diff] [blame] | 2151 | /* Previous to GLSL version 1.30, different compilation units could mix and |
| 2152 | * match shading language versions. With GLSL 1.30 and later, the versions |
| 2153 | * of all shaders must match. |
| 2154 | */ |
Kenneth Graunke | 5a81d05 | 2010-08-31 09:33:58 -0700 | [diff] [blame] | 2155 | assert(min_version >= 100); |
Ian Romanick | 25f51d3 | 2010-07-16 15:51:50 -0700 | [diff] [blame] | 2156 | assert(max_version <= 130); |
Kenneth Graunke | 5a81d05 | 2010-08-31 09:33:58 -0700 | [diff] [blame] | 2157 | if ((max_version >= 130 || min_version == 100) |
| 2158 | && min_version != max_version) { |
Ian Romanick | 586e741 | 2011-07-28 14:04:09 -0700 | [diff] [blame] | 2159 | linker_error(prog, "all shaders must use same shading " |
| 2160 | "language version\n"); |
Ian Romanick | 25f51d3 | 2010-07-16 15:51:50 -0700 | [diff] [blame] | 2161 | goto done; |
| 2162 | } |
| 2163 | |
| 2164 | prog->Version = max_version; |
| 2165 | |
Ian Romanick | 3322fba | 2010-10-14 13:28:42 -0700 | [diff] [blame] | 2166 | for (unsigned int i = 0; i < MESA_SHADER_TYPES; i++) { |
| 2167 | if (prog->_LinkedShaders[i] != NULL) |
| 2168 | ctx->Driver.DeleteShader(ctx, prog->_LinkedShaders[i]); |
| 2169 | |
| 2170 | prog->_LinkedShaders[i] = NULL; |
Eric Anholt | 5d0f430 | 2010-08-18 12:02:35 -0700 | [diff] [blame] | 2171 | } |
| 2172 | |
Ian Romanick | cd6764e | 2010-07-16 16:00:07 -0700 | [diff] [blame] | 2173 | /* Link all shaders for a particular stage and validate the result. |
| 2174 | */ |
Ian Romanick | cc22c5a | 2010-06-18 17:13:42 -0700 | [diff] [blame] | 2175 | if (num_vert_shaders > 0) { |
Ian Romanick | 3fb8787 | 2010-07-09 14:09:34 -0700 | [diff] [blame] | 2176 | gl_shader *const sh = |
Kenneth Graunke | 2da02e7 | 2010-11-17 11:03:57 -0800 | [diff] [blame] | 2177 | link_intrastage_shaders(mem_ctx, ctx, prog, vert_shader_list, |
| 2178 | num_vert_shaders); |
Ian Romanick | 3fb8787 | 2010-07-09 14:09:34 -0700 | [diff] [blame] | 2179 | |
| 2180 | if (sh == NULL) |
| 2181 | goto done; |
| 2182 | |
| 2183 | if (!validate_vertex_shader_executable(prog, sh)) |
Ian Romanick | f29ff6e | 2010-10-14 17:55:17 -0700 | [diff] [blame] | 2184 | goto done; |
Ian Romanick | 3fb8787 | 2010-07-09 14:09:34 -0700 | [diff] [blame] | 2185 | |
Ian Romanick | 3322fba | 2010-10-14 13:28:42 -0700 | [diff] [blame] | 2186 | _mesa_reference_shader(ctx, &prog->_LinkedShaders[MESA_SHADER_VERTEX], |
| 2187 | sh); |
Ian Romanick | cc22c5a | 2010-06-18 17:13:42 -0700 | [diff] [blame] | 2188 | } |
| 2189 | |
| 2190 | if (num_frag_shaders > 0) { |
Ian Romanick | 3fb8787 | 2010-07-09 14:09:34 -0700 | [diff] [blame] | 2191 | gl_shader *const sh = |
Kenneth Graunke | 2da02e7 | 2010-11-17 11:03:57 -0800 | [diff] [blame] | 2192 | link_intrastage_shaders(mem_ctx, ctx, prog, frag_shader_list, |
| 2193 | num_frag_shaders); |
Ian Romanick | 3fb8787 | 2010-07-09 14:09:34 -0700 | [diff] [blame] | 2194 | |
| 2195 | if (sh == NULL) |
| 2196 | goto done; |
| 2197 | |
| 2198 | if (!validate_fragment_shader_executable(prog, sh)) |
Ian Romanick | f29ff6e | 2010-10-14 17:55:17 -0700 | [diff] [blame] | 2199 | goto done; |
Ian Romanick | 3fb8787 | 2010-07-09 14:09:34 -0700 | [diff] [blame] | 2200 | |
Ian Romanick | 3322fba | 2010-10-14 13:28:42 -0700 | [diff] [blame] | 2201 | _mesa_reference_shader(ctx, &prog->_LinkedShaders[MESA_SHADER_FRAGMENT], |
| 2202 | sh); |
Ian Romanick | cc22c5a | 2010-06-18 17:13:42 -0700 | [diff] [blame] | 2203 | } |
| 2204 | |
Ian Romanick | 3ed850e | 2010-06-23 12:18:21 -0700 | [diff] [blame] | 2205 | /* Here begins the inter-stage linking phase. Some initial validation is |
| 2206 | * performed, then locations are assigned for uniforms, attributes, and |
| 2207 | * varyings. |
| 2208 | */ |
Ian Romanick | ed1fe3d | 2010-06-23 12:09:14 -0700 | [diff] [blame] | 2209 | if (cross_validate_uniforms(prog)) { |
Ian Romanick | 3322fba | 2010-10-14 13:28:42 -0700 | [diff] [blame] | 2210 | unsigned prev; |
| 2211 | |
| 2212 | for (prev = 0; prev < MESA_SHADER_TYPES; prev++) { |
| 2213 | if (prog->_LinkedShaders[prev] != NULL) |
| 2214 | break; |
| 2215 | } |
| 2216 | |
Bryan Cain | f18a086 | 2011-04-23 19:29:15 -0500 | [diff] [blame] | 2217 | /* Validate the inputs of each stage with the output of the preceding |
Ian Romanick | 3710192 | 2010-06-18 19:02:10 -0700 | [diff] [blame] | 2218 | * stage. |
| 2219 | */ |
Ian Romanick | 3322fba | 2010-10-14 13:28:42 -0700 | [diff] [blame] | 2220 | for (unsigned i = prev + 1; i < MESA_SHADER_TYPES; i++) { |
| 2221 | if (prog->_LinkedShaders[i] == NULL) |
| 2222 | continue; |
| 2223 | |
Ian Romanick | f36460e | 2010-06-23 12:07:22 -0700 | [diff] [blame] | 2224 | if (!cross_validate_outputs_to_inputs(prog, |
Ian Romanick | 3322fba | 2010-10-14 13:28:42 -0700 | [diff] [blame] | 2225 | prog->_LinkedShaders[prev], |
Ian Romanick | abee16e | 2010-06-21 16:16:05 -0700 | [diff] [blame] | 2226 | prog->_LinkedShaders[i])) |
Ian Romanick | 3710192 | 2010-06-18 19:02:10 -0700 | [diff] [blame] | 2227 | goto done; |
Ian Romanick | 3322fba | 2010-10-14 13:28:42 -0700 | [diff] [blame] | 2228 | |
| 2229 | prev = i; |
Ian Romanick | 3710192 | 2010-06-18 19:02:10 -0700 | [diff] [blame] | 2230 | } |
| 2231 | |
Ian Romanick | cc22c5a | 2010-06-18 17:13:42 -0700 | [diff] [blame] | 2232 | prog->LinkStatus = true; |
Ian Romanick | 3710192 | 2010-06-18 19:02:10 -0700 | [diff] [blame] | 2233 | } |
Ian Romanick | 832dfa5 | 2010-06-17 15:04:20 -0700 | [diff] [blame] | 2234 | |
Eric Anholt | 2f4fe15 | 2010-08-10 13:06:49 -0700 | [diff] [blame] | 2235 | /* Do common optimization before assigning storage for attributes, |
| 2236 | * uniforms, and varyings. Later optimization could possibly make |
| 2237 | * some of that unused. |
| 2238 | */ |
Ian Romanick | 3322fba | 2010-10-14 13:28:42 -0700 | [diff] [blame] | 2239 | for (unsigned i = 0; i < MESA_SHADER_TYPES; i++) { |
| 2240 | if (prog->_LinkedShaders[i] == NULL) |
| 2241 | continue; |
| 2242 | |
Ian Romanick | 02c5ae1 | 2011-07-11 10:46:01 -0700 | [diff] [blame] | 2243 | detect_recursion_linked(prog, prog->_LinkedShaders[i]->ir); |
| 2244 | if (!prog->LinkStatus) |
| 2245 | goto done; |
| 2246 | |
Paul Berry | c06e325 | 2011-08-11 20:58:21 -0700 | [diff] [blame] | 2247 | if (ctx->ShaderCompilerOptions[i].LowerClipDistance) |
| 2248 | lower_clip_distance(prog->_LinkedShaders[i]->ir); |
| 2249 | |
Ian Romanick | 1d5d67f | 2011-10-21 11:17:39 -0700 | [diff] [blame] | 2250 | while (do_common_optimization(prog->_LinkedShaders[i]->ir, true, false, 32)) |
Eric Anholt | 2f4fe15 | 2010-08-10 13:06:49 -0700 | [diff] [blame] | 2251 | ; |
Ian Romanick | a7ba9a7 | 2010-07-20 13:36:32 -0700 | [diff] [blame] | 2252 | } |
Ian Romanick | 13e10e4 | 2010-06-21 12:03:24 -0700 | [diff] [blame] | 2253 | |
Ian Romanick | d32d4f7 | 2011-06-27 17:59:58 -0700 | [diff] [blame] | 2254 | /* FINISHME: The value of the max_attribute_index parameter is |
| 2255 | * FINISHME: implementation dependent based on the value of |
| 2256 | * FINISHME: GL_MAX_VERTEX_ATTRIBS. GL_MAX_VERTEX_ATTRIBS must be |
| 2257 | * FINISHME: at least 16, so hardcode 16 for now. |
| 2258 | */ |
| 2259 | if (!assign_attribute_or_color_locations(prog, MESA_SHADER_VERTEX, 16)) { |
Ian Romanick | d32d4f7 | 2011-06-27 17:59:58 -0700 | [diff] [blame] | 2260 | goto done; |
| 2261 | } |
| 2262 | |
| 2263 | 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] | 2264 | goto done; |
Ian Romanick | 40e114b | 2010-08-17 14:55:50 -0700 | [diff] [blame] | 2265 | } |
| 2266 | |
Ian Romanick | 3322fba | 2010-10-14 13:28:42 -0700 | [diff] [blame] | 2267 | unsigned prev; |
| 2268 | for (prev = 0; prev < MESA_SHADER_TYPES; prev++) { |
| 2269 | if (prog->_LinkedShaders[prev] != NULL) |
| 2270 | break; |
| 2271 | } |
| 2272 | |
Paul Berry | 871ddb9 | 2011-11-05 11:17:32 -0700 | [diff] [blame] | 2273 | if (num_tfeedback_decls != 0) { |
| 2274 | /* From GL_EXT_transform_feedback: |
| 2275 | * A program will fail to link if: |
| 2276 | * |
| 2277 | * * the <count> specified by TransformFeedbackVaryingsEXT is |
| 2278 | * non-zero, but the program object has no vertex or geometry |
| 2279 | * shader; |
| 2280 | */ |
| 2281 | if (prev >= MESA_SHADER_FRAGMENT) { |
| 2282 | linker_error(prog, "Transform feedback varyings specified, but " |
| 2283 | "no vertex or geometry shader is present."); |
| 2284 | goto done; |
| 2285 | } |
| 2286 | |
| 2287 | tfeedback_decls = ralloc_array(mem_ctx, tfeedback_decl, |
| 2288 | prog->TransformFeedback.NumVarying); |
Paul Berry | 456279b | 2011-12-26 19:39:25 -0800 | [diff] [blame] | 2289 | if (!parse_tfeedback_decls(ctx, prog, mem_ctx, num_tfeedback_decls, |
Paul Berry | 871ddb9 | 2011-11-05 11:17:32 -0700 | [diff] [blame] | 2290 | prog->TransformFeedback.VaryingNames, |
| 2291 | tfeedback_decls)) |
| 2292 | goto done; |
| 2293 | } |
| 2294 | |
Ian Romanick | 3322fba | 2010-10-14 13:28:42 -0700 | [diff] [blame] | 2295 | for (unsigned i = prev + 1; i < MESA_SHADER_TYPES; i++) { |
| 2296 | if (prog->_LinkedShaders[i] == NULL) |
| 2297 | continue; |
| 2298 | |
Paul Berry | 871ddb9 | 2011-11-05 11:17:32 -0700 | [diff] [blame] | 2299 | if (!assign_varying_locations( |
| 2300 | ctx, prog, prog->_LinkedShaders[prev], prog->_LinkedShaders[i], |
| 2301 | i == MESA_SHADER_FRAGMENT ? num_tfeedback_decls : 0, |
| 2302 | tfeedback_decls)) |
Ian Romanick | de77324 | 2011-06-09 13:31:32 -0700 | [diff] [blame] | 2303 | goto done; |
Ian Romanick | de77324 | 2011-06-09 13:31:32 -0700 | [diff] [blame] | 2304 | |
Ian Romanick | 3322fba | 2010-10-14 13:28:42 -0700 | [diff] [blame] | 2305 | prev = i; |
| 2306 | } |
Ian Romanick | 13e10e4 | 2010-06-21 12:03:24 -0700 | [diff] [blame] | 2307 | |
Paul Berry | 871ddb9 | 2011-11-05 11:17:32 -0700 | [diff] [blame] | 2308 | if (prev != MESA_SHADER_FRAGMENT && num_tfeedback_decls != 0) { |
| 2309 | /* There was no fragment shader, but we still have to assign varying |
| 2310 | * locations for use by transform feedback. |
| 2311 | */ |
| 2312 | if (!assign_varying_locations( |
| 2313 | ctx, prog, prog->_LinkedShaders[prev], NULL, num_tfeedback_decls, |
| 2314 | tfeedback_decls)) |
| 2315 | goto done; |
| 2316 | } |
| 2317 | |
| 2318 | if (!store_tfeedback_info(ctx, prog, num_tfeedback_decls, tfeedback_decls)) |
| 2319 | goto done; |
| 2320 | |
Ian Romanick | cc90e62 | 2010-10-19 17:59:10 -0700 | [diff] [blame] | 2321 | if (prog->_LinkedShaders[MESA_SHADER_VERTEX] != NULL) { |
| 2322 | demote_shader_inputs_and_outputs(prog->_LinkedShaders[MESA_SHADER_VERTEX], |
| 2323 | ir_var_out); |
Ian Romanick | 960d722 | 2011-10-21 11:21:02 -0700 | [diff] [blame] | 2324 | |
| 2325 | /* Eliminate code that is now dead due to unused vertex outputs being |
| 2326 | * demoted. |
| 2327 | */ |
| 2328 | while (do_dead_code(prog->_LinkedShaders[MESA_SHADER_VERTEX]->ir, false)) |
| 2329 | ; |
Ian Romanick | cc90e62 | 2010-10-19 17:59:10 -0700 | [diff] [blame] | 2330 | } |
| 2331 | |
| 2332 | if (prog->_LinkedShaders[MESA_SHADER_GEOMETRY] != NULL) { |
| 2333 | gl_shader *const sh = prog->_LinkedShaders[MESA_SHADER_GEOMETRY]; |
| 2334 | |
| 2335 | demote_shader_inputs_and_outputs(sh, ir_var_in); |
| 2336 | demote_shader_inputs_and_outputs(sh, ir_var_inout); |
| 2337 | demote_shader_inputs_and_outputs(sh, ir_var_out); |
Ian Romanick | 960d722 | 2011-10-21 11:21:02 -0700 | [diff] [blame] | 2338 | |
| 2339 | /* Eliminate code that is now dead due to unused geometry outputs being |
| 2340 | * demoted. |
| 2341 | */ |
| 2342 | while (do_dead_code(prog->_LinkedShaders[MESA_SHADER_GEOMETRY]->ir, false)) |
| 2343 | ; |
Ian Romanick | cc90e62 | 2010-10-19 17:59:10 -0700 | [diff] [blame] | 2344 | } |
| 2345 | |
| 2346 | if (prog->_LinkedShaders[MESA_SHADER_FRAGMENT] != NULL) { |
| 2347 | gl_shader *const sh = prog->_LinkedShaders[MESA_SHADER_FRAGMENT]; |
| 2348 | |
| 2349 | demote_shader_inputs_and_outputs(sh, ir_var_in); |
Ian Romanick | 960d722 | 2011-10-21 11:21:02 -0700 | [diff] [blame] | 2350 | |
| 2351 | /* Eliminate code that is now dead due to unused fragment inputs being |
| 2352 | * demoted. This shouldn't actually do anything other than remove |
| 2353 | * declarations of the (now unused) global variables. |
| 2354 | */ |
| 2355 | while (do_dead_code(prog->_LinkedShaders[MESA_SHADER_FRAGMENT]->ir, false)) |
| 2356 | ; |
Ian Romanick | cc90e62 | 2010-10-19 17:59:10 -0700 | [diff] [blame] | 2357 | } |
| 2358 | |
Ian Romanick | 960d722 | 2011-10-21 11:21:02 -0700 | [diff] [blame] | 2359 | update_array_sizes(prog); |
Ian Romanick | 7199096 | 2011-10-18 16:01:49 -0700 | [diff] [blame] | 2360 | link_assign_uniform_locations(prog); |
Marek Olšák | ec174a4 | 2011-11-18 15:00:10 +0100 | [diff] [blame] | 2361 | store_fragdepth_layout(prog); |
Ian Romanick | 960d722 | 2011-10-21 11:21:02 -0700 | [diff] [blame] | 2362 | |
Ian Romanick | 92f8159 | 2011-11-08 12:37:19 -0800 | [diff] [blame] | 2363 | if (!check_resources(ctx, prog)) |
| 2364 | goto done; |
| 2365 | |
Ian Romanick | ce9171f | 2011-02-03 17:10:14 -0800 | [diff] [blame] | 2366 | /* OpenGL ES requires that a vertex shader and a fragment shader both be |
| 2367 | * present in a linked program. By checking for use of shading language |
| 2368 | * version 1.00, we also catch the GL_ARB_ES2_compatibility case. |
| 2369 | */ |
Eric Anholt | 57f7978 | 2011-07-22 12:57:47 -0700 | [diff] [blame] | 2370 | if (!prog->InternalSeparateShader && |
| 2371 | (ctx->API == API_OPENGLES2 || prog->Version == 100)) { |
Ian Romanick | ce9171f | 2011-02-03 17:10:14 -0800 | [diff] [blame] | 2372 | if (prog->_LinkedShaders[MESA_SHADER_VERTEX] == NULL) { |
Ian Romanick | 586e741 | 2011-07-28 14:04:09 -0700 | [diff] [blame] | 2373 | linker_error(prog, "program lacks a vertex shader\n"); |
Ian Romanick | ce9171f | 2011-02-03 17:10:14 -0800 | [diff] [blame] | 2374 | } else if (prog->_LinkedShaders[MESA_SHADER_FRAGMENT] == NULL) { |
Ian Romanick | 586e741 | 2011-07-28 14:04:09 -0700 | [diff] [blame] | 2375 | linker_error(prog, "program lacks a fragment shader\n"); |
Ian Romanick | ce9171f | 2011-02-03 17:10:14 -0800 | [diff] [blame] | 2376 | } |
| 2377 | } |
| 2378 | |
Ian Romanick | 13e10e4 | 2010-06-21 12:03:24 -0700 | [diff] [blame] | 2379 | /* FINISHME: Assign fragment shader output locations. */ |
| 2380 | |
Ian Romanick | 832dfa5 | 2010-06-17 15:04:20 -0700 | [diff] [blame] | 2381 | done: |
| 2382 | free(vert_shader_list); |
Kenneth Graunke | 2da02e7 | 2010-11-17 11:03:57 -0800 | [diff] [blame] | 2383 | |
| 2384 | for (unsigned i = 0; i < MESA_SHADER_TYPES; i++) { |
| 2385 | if (prog->_LinkedShaders[i] == NULL) |
| 2386 | continue; |
| 2387 | |
| 2388 | /* Retain any live IR, but trash the rest. */ |
| 2389 | reparent_ir(prog->_LinkedShaders[i]->ir, prog->_LinkedShaders[i]->ir); |
Ian Romanick | 7bbcc0b | 2011-09-30 14:21:10 -0700 | [diff] [blame] | 2390 | |
| 2391 | /* The symbol table in the linked shaders may contain references to |
| 2392 | * variables that were removed (e.g., unused uniforms). Since it may |
| 2393 | * contain junk, there is no possible valid use. Delete it and set the |
| 2394 | * pointer to NULL. |
| 2395 | */ |
| 2396 | delete prog->_LinkedShaders[i]->symbols; |
| 2397 | prog->_LinkedShaders[i]->symbols = NULL; |
Kenneth Graunke | 2da02e7 | 2010-11-17 11:03:57 -0800 | [diff] [blame] | 2398 | } |
| 2399 | |
Kenneth Graunke | d3073f5 | 2011-01-21 14:32:31 -0800 | [diff] [blame] | 2400 | ralloc_free(mem_ctx); |
Ian Romanick | 832dfa5 | 2010-06-17 15:04:20 -0700 | [diff] [blame] | 2401 | } |