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