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