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