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