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" |
Paul Berry | 4b11b57 | 2012-12-17 14:20:35 -0800 | [diff] [blame^] | 73 | #include "link_varyings.h" |
Ian Romanick | a7ba9a7 | 2010-07-20 13:36:32 -0700 | [diff] [blame] | 74 | #include "ir_optimization.h" |
Ian Romanick | 832dfa5 | 2010-06-17 15:04:20 -0700 | [diff] [blame] | 75 | |
Ian Romanick | 3322fba | 2010-10-14 13:28:42 -0700 | [diff] [blame] | 76 | extern "C" { |
| 77 | #include "main/shaderobj.h" |
| 78 | } |
| 79 | |
Ian Romanick | 832dfa5 | 2010-06-17 15:04:20 -0700 | [diff] [blame] | 80 | /** |
| 81 | * Visitor that determines whether or not a variable is ever written. |
| 82 | */ |
| 83 | class find_assignment_visitor : public ir_hierarchical_visitor { |
| 84 | public: |
| 85 | find_assignment_visitor(const char *name) |
| 86 | : name(name), found(false) |
| 87 | { |
| 88 | /* empty */ |
| 89 | } |
| 90 | |
| 91 | virtual ir_visitor_status visit_enter(ir_assignment *ir) |
| 92 | { |
| 93 | ir_variable *const var = ir->lhs->variable_referenced(); |
| 94 | |
| 95 | if (strcmp(name, var->name) == 0) { |
| 96 | found = true; |
| 97 | return visit_stop; |
| 98 | } |
| 99 | |
| 100 | return visit_continue_with_parent; |
| 101 | } |
| 102 | |
Eric Anholt | 18a6023 | 2010-08-23 11:29:25 -0700 | [diff] [blame] | 103 | virtual ir_visitor_status visit_enter(ir_call *ir) |
| 104 | { |
Kenneth Graunke | 82065fa | 2011-09-20 18:08:11 -0700 | [diff] [blame] | 105 | exec_list_iterator sig_iter = ir->callee->parameters.iterator(); |
Eric Anholt | 18a6023 | 2010-08-23 11:29:25 -0700 | [diff] [blame] | 106 | foreach_iter(exec_list_iterator, iter, *ir) { |
| 107 | ir_rvalue *param_rval = (ir_rvalue *)iter.get(); |
| 108 | ir_variable *sig_param = (ir_variable *)sig_iter.get(); |
| 109 | |
| 110 | if (sig_param->mode == ir_var_out || |
| 111 | sig_param->mode == ir_var_inout) { |
| 112 | ir_variable *var = param_rval->variable_referenced(); |
| 113 | if (var && strcmp(name, var->name) == 0) { |
| 114 | found = true; |
| 115 | return visit_stop; |
| 116 | } |
| 117 | } |
| 118 | sig_iter.next(); |
| 119 | } |
| 120 | |
Kenneth Graunke | d884f60 | 2012-03-20 15:56:37 -0700 | [diff] [blame] | 121 | if (ir->return_deref != NULL) { |
| 122 | ir_variable *const var = ir->return_deref->variable_referenced(); |
| 123 | |
| 124 | if (strcmp(name, var->name) == 0) { |
| 125 | found = true; |
| 126 | return visit_stop; |
| 127 | } |
| 128 | } |
| 129 | |
Eric Anholt | 18a6023 | 2010-08-23 11:29:25 -0700 | [diff] [blame] | 130 | return visit_continue_with_parent; |
| 131 | } |
| 132 | |
Ian Romanick | 832dfa5 | 2010-06-17 15:04:20 -0700 | [diff] [blame] | 133 | bool variable_found() |
| 134 | { |
| 135 | return found; |
| 136 | } |
| 137 | |
| 138 | private: |
| 139 | const char *name; /**< Find writes to a variable with this name. */ |
| 140 | bool found; /**< Was a write to the variable found? */ |
| 141 | }; |
| 142 | |
Ian Romanick | c93b8f1 | 2010-06-17 15:20:22 -0700 | [diff] [blame] | 143 | |
Ian Romanick | c33e78f | 2010-08-13 12:30:41 -0700 | [diff] [blame] | 144 | /** |
| 145 | * Visitor that determines whether or not a variable is ever read. |
| 146 | */ |
| 147 | class find_deref_visitor : public ir_hierarchical_visitor { |
| 148 | public: |
| 149 | find_deref_visitor(const char *name) |
| 150 | : name(name), found(false) |
| 151 | { |
| 152 | /* empty */ |
| 153 | } |
| 154 | |
| 155 | virtual ir_visitor_status visit(ir_dereference_variable *ir) |
| 156 | { |
| 157 | if (strcmp(this->name, ir->var->name) == 0) { |
| 158 | this->found = true; |
| 159 | return visit_stop; |
| 160 | } |
| 161 | |
| 162 | return visit_continue; |
| 163 | } |
| 164 | |
| 165 | bool variable_found() const |
| 166 | { |
| 167 | return this->found; |
| 168 | } |
| 169 | |
| 170 | private: |
| 171 | const char *name; /**< Find writes to a variable with this name. */ |
| 172 | bool found; /**< Was a write to the variable found? */ |
| 173 | }; |
| 174 | |
| 175 | |
Ian Romanick | 0ad22cd | 2010-06-21 17:18:31 -0700 | [diff] [blame] | 176 | void |
Ian Romanick | 586e741 | 2011-07-28 14:04:09 -0700 | [diff] [blame] | 177 | linker_error(gl_shader_program *prog, const char *fmt, ...) |
Ian Romanick | f36460e | 2010-06-23 12:07:22 -0700 | [diff] [blame] | 178 | { |
| 179 | va_list ap; |
| 180 | |
Kenneth Graunke | d3073f5 | 2011-01-21 14:32:31 -0800 | [diff] [blame] | 181 | ralloc_strcat(&prog->InfoLog, "error: "); |
Ian Romanick | f36460e | 2010-06-23 12:07:22 -0700 | [diff] [blame] | 182 | va_start(ap, fmt); |
Kenneth Graunke | d3073f5 | 2011-01-21 14:32:31 -0800 | [diff] [blame] | 183 | ralloc_vasprintf_append(&prog->InfoLog, fmt, ap); |
Ian Romanick | f36460e | 2010-06-23 12:07:22 -0700 | [diff] [blame] | 184 | va_end(ap); |
Ian Romanick | 586e741 | 2011-07-28 14:04:09 -0700 | [diff] [blame] | 185 | |
| 186 | prog->LinkStatus = false; |
Ian Romanick | f36460e | 2010-06-23 12:07:22 -0700 | [diff] [blame] | 187 | } |
| 188 | |
| 189 | |
| 190 | void |
Ian Romanick | 379a32f | 2011-07-28 14:09:06 -0700 | [diff] [blame] | 191 | linker_warning(gl_shader_program *prog, const char *fmt, ...) |
| 192 | { |
| 193 | va_list ap; |
| 194 | |
| 195 | ralloc_strcat(&prog->InfoLog, "error: "); |
| 196 | va_start(ap, fmt); |
| 197 | ralloc_vasprintf_append(&prog->InfoLog, fmt, ap); |
| 198 | va_end(ap); |
| 199 | |
| 200 | } |
| 201 | |
| 202 | |
| 203 | void |
Paul Berry | 50895d4 | 2012-12-05 07:17:07 -0800 | [diff] [blame] | 204 | link_invalidate_variable_locations(gl_shader *sh, int input_base, |
| 205 | int output_base) |
Ian Romanick | 0ad22cd | 2010-06-21 17:18:31 -0700 | [diff] [blame] | 206 | { |
Eric Anholt | 16b68b1 | 2010-06-30 11:05:43 -0700 | [diff] [blame] | 207 | foreach_list(node, sh->ir) { |
Ian Romanick | 0ad22cd | 2010-06-21 17:18:31 -0700 | [diff] [blame] | 208 | ir_variable *const var = ((ir_instruction *) node)->as_variable(); |
| 209 | |
Paul Berry | 50895d4 | 2012-12-05 07:17:07 -0800 | [diff] [blame] | 210 | if (var == NULL) |
| 211 | continue; |
| 212 | |
| 213 | int base; |
| 214 | switch (var->mode) { |
| 215 | case ir_var_in: |
| 216 | base = input_base; |
| 217 | break; |
| 218 | case ir_var_out: |
| 219 | base = output_base; |
| 220 | break; |
| 221 | default: |
| 222 | continue; |
| 223 | } |
Ian Romanick | 0ad22cd | 2010-06-21 17:18:31 -0700 | [diff] [blame] | 224 | |
| 225 | /* Only assign locations for generic attributes / varyings / etc. |
| 226 | */ |
Paul Berry | 50895d4 | 2012-12-05 07:17:07 -0800 | [diff] [blame] | 227 | if ((var->location >= base) && !var->explicit_location) |
| 228 | var->location = -1; |
Paul Berry | 3c9c17d | 2012-12-04 15:17:01 -0800 | [diff] [blame] | 229 | |
Paul Berry | 3e81c66 | 2012-12-05 10:47:55 -0800 | [diff] [blame] | 230 | if ((var->location == -1) && !var->explicit_location) { |
Paul Berry | 3c9c17d | 2012-12-04 15:17:01 -0800 | [diff] [blame] | 231 | var->is_unmatched_generic_inout = 1; |
Paul Berry | 3e81c66 | 2012-12-05 10:47:55 -0800 | [diff] [blame] | 232 | var->location_frac = 0; |
| 233 | } else { |
Paul Berry | 3c9c17d | 2012-12-04 15:17:01 -0800 | [diff] [blame] | 234 | var->is_unmatched_generic_inout = 0; |
Paul Berry | 3e81c66 | 2012-12-05 10:47:55 -0800 | [diff] [blame] | 235 | } |
Ian Romanick | 0ad22cd | 2010-06-21 17:18:31 -0700 | [diff] [blame] | 236 | } |
| 237 | } |
| 238 | |
| 239 | |
Ian Romanick | c93b8f1 | 2010-06-17 15:20:22 -0700 | [diff] [blame] | 240 | /** |
Ian Romanick | 6984670 | 2010-06-22 17:29:19 -0700 | [diff] [blame] | 241 | * Determine the number of attribute slots required for a particular type |
| 242 | * |
| 243 | * This code is here because it implements the language rules of a specific |
| 244 | * GLSL version. Since it's a property of the language and not a property of |
| 245 | * types in general, it doesn't really belong in glsl_type. |
| 246 | */ |
| 247 | unsigned |
| 248 | count_attribute_slots(const glsl_type *t) |
| 249 | { |
| 250 | /* From page 31 (page 37 of the PDF) of the GLSL 1.50 spec: |
| 251 | * |
| 252 | * "A scalar input counts the same amount against this limit as a vec4, |
| 253 | * so applications may want to consider packing groups of four |
| 254 | * unrelated float inputs together into a vector to better utilize the |
| 255 | * capabilities of the underlying hardware. A matrix input will use up |
| 256 | * multiple locations. The number of locations used will equal the |
| 257 | * number of columns in the matrix." |
| 258 | * |
| 259 | * The spec does not explicitly say how arrays are counted. However, it |
| 260 | * should be safe to assume the total number of slots consumed by an array |
| 261 | * is the number of entries in the array multiplied by the number of slots |
| 262 | * consumed by a single element of the array. |
| 263 | */ |
| 264 | |
| 265 | if (t->is_array()) |
| 266 | return t->array_size() * count_attribute_slots(t->element_type()); |
| 267 | |
| 268 | if (t->is_matrix()) |
| 269 | return t->matrix_columns; |
| 270 | |
| 271 | return 1; |
| 272 | } |
| 273 | |
| 274 | |
| 275 | /** |
Paul Berry | 1ad54ae | 2011-09-17 09:42:02 -0700 | [diff] [blame] | 276 | * Verify that a vertex shader executable meets all semantic requirements. |
| 277 | * |
Paul Berry | 642e5b41 | 2012-01-04 13:57:52 -0800 | [diff] [blame] | 278 | * Also sets prog->Vert.UsesClipDistance and prog->Vert.ClipDistanceArraySize |
| 279 | * as a side effect. |
Ian Romanick | c93b8f1 | 2010-06-17 15:20:22 -0700 | [diff] [blame] | 280 | * |
| 281 | * \param shader Vertex shader executable to be verified |
| 282 | */ |
Ian Romanick | 832dfa5 | 2010-06-17 15:04:20 -0700 | [diff] [blame] | 283 | bool |
Eric Anholt | 849e181 | 2010-06-30 11:49:17 -0700 | [diff] [blame] | 284 | validate_vertex_shader_executable(struct gl_shader_program *prog, |
Eric Anholt | 16b68b1 | 2010-06-30 11:05:43 -0700 | [diff] [blame] | 285 | struct gl_shader *shader) |
Ian Romanick | 832dfa5 | 2010-06-17 15:04:20 -0700 | [diff] [blame] | 286 | { |
| 287 | if (shader == NULL) |
| 288 | return true; |
| 289 | |
Eric Anholt | f1c1c9e | 2012-03-19 22:43:27 -0700 | [diff] [blame] | 290 | /* From the GLSL 1.10 spec, page 48: |
| 291 | * |
| 292 | * "The variable gl_Position is available only in the vertex |
| 293 | * language and is intended for writing the homogeneous vertex |
| 294 | * position. All executions of a well-formed vertex shader |
| 295 | * executable must write a value into this variable. [...] The |
| 296 | * variable gl_Position is available only in the vertex |
| 297 | * language and is intended for writing the homogeneous vertex |
| 298 | * position. All executions of a well-formed vertex shader |
| 299 | * executable must write a value into this variable." |
| 300 | * |
| 301 | * while in GLSL 1.40 this text is changed to: |
| 302 | * |
| 303 | * "The variable gl_Position is available only in the vertex |
| 304 | * language and is intended for writing the homogeneous vertex |
| 305 | * position. It can be written at any time during shader |
| 306 | * execution. It may also be read back by a vertex shader |
| 307 | * after being written. This value will be used by primitive |
| 308 | * assembly, clipping, culling, and other fixed functionality |
| 309 | * operations, if present, that operate on primitives after |
| 310 | * vertex processing has occurred. Its value is undefined if |
| 311 | * the vertex shader executable does not write gl_Position." |
Paul Berry | 15ba2a5 | 2012-08-02 17:51:02 -0700 | [diff] [blame] | 312 | * |
| 313 | * GLSL ES 3.00 is similar to GLSL 1.40--failing to write to gl_Position is |
| 314 | * not an error. |
Eric Anholt | f1c1c9e | 2012-03-19 22:43:27 -0700 | [diff] [blame] | 315 | */ |
Paul Berry | 15ba2a5 | 2012-08-02 17:51:02 -0700 | [diff] [blame] | 316 | if (prog->Version < (prog->IsES ? 300 : 140)) { |
Eric Anholt | f1c1c9e | 2012-03-19 22:43:27 -0700 | [diff] [blame] | 317 | find_assignment_visitor find("gl_Position"); |
| 318 | find.run(shader->ir); |
| 319 | if (!find.variable_found()) { |
| 320 | linker_error(prog, "vertex shader does not write to `gl_Position'\n"); |
| 321 | return false; |
| 322 | } |
Ian Romanick | 832dfa5 | 2010-06-17 15:04:20 -0700 | [diff] [blame] | 323 | } |
| 324 | |
Paul Berry | 642e5b41 | 2012-01-04 13:57:52 -0800 | [diff] [blame] | 325 | prog->Vert.ClipDistanceArraySize = 0; |
| 326 | |
Paul Berry | 15ba2a5 | 2012-08-02 17:51:02 -0700 | [diff] [blame] | 327 | if (!prog->IsES && prog->Version >= 130) { |
Paul Berry | b453ba2 | 2011-08-11 18:10:22 -0700 | [diff] [blame] | 328 | /* From section 7.1 (Vertex Shader Special Variables) of the |
| 329 | * GLSL 1.30 spec: |
| 330 | * |
| 331 | * "It is an error for a shader to statically write both |
| 332 | * gl_ClipVertex and gl_ClipDistance." |
Paul Berry | 15ba2a5 | 2012-08-02 17:51:02 -0700 | [diff] [blame] | 333 | * |
| 334 | * This does not apply to GLSL ES shaders, since GLSL ES defines neither |
| 335 | * gl_ClipVertex nor gl_ClipDistance. |
Paul Berry | b453ba2 | 2011-08-11 18:10:22 -0700 | [diff] [blame] | 336 | */ |
| 337 | find_assignment_visitor clip_vertex("gl_ClipVertex"); |
| 338 | find_assignment_visitor clip_distance("gl_ClipDistance"); |
| 339 | |
| 340 | clip_vertex.run(shader->ir); |
| 341 | clip_distance.run(shader->ir); |
| 342 | if (clip_vertex.variable_found() && clip_distance.variable_found()) { |
| 343 | linker_error(prog, "vertex shader writes to both `gl_ClipVertex' " |
| 344 | "and `gl_ClipDistance'\n"); |
| 345 | return false; |
| 346 | } |
Paul Berry | 1ad54ae | 2011-09-17 09:42:02 -0700 | [diff] [blame] | 347 | prog->Vert.UsesClipDistance = clip_distance.variable_found(); |
Paul Berry | 642e5b41 | 2012-01-04 13:57:52 -0800 | [diff] [blame] | 348 | ir_variable *clip_distance_var = |
| 349 | shader->symbols->get_variable("gl_ClipDistance"); |
| 350 | if (clip_distance_var) |
| 351 | prog->Vert.ClipDistanceArraySize = clip_distance_var->type->length; |
Paul Berry | b453ba2 | 2011-08-11 18:10:22 -0700 | [diff] [blame] | 352 | } |
| 353 | |
Ian Romanick | 832dfa5 | 2010-06-17 15:04:20 -0700 | [diff] [blame] | 354 | return true; |
| 355 | } |
| 356 | |
| 357 | |
Ian Romanick | c93b8f1 | 2010-06-17 15:20:22 -0700 | [diff] [blame] | 358 | /** |
| 359 | * Verify that a fragment shader executable meets all semantic requirements |
| 360 | * |
| 361 | * \param shader Fragment shader executable to be verified |
| 362 | */ |
Ian Romanick | 832dfa5 | 2010-06-17 15:04:20 -0700 | [diff] [blame] | 363 | bool |
Eric Anholt | 849e181 | 2010-06-30 11:49:17 -0700 | [diff] [blame] | 364 | validate_fragment_shader_executable(struct gl_shader_program *prog, |
Eric Anholt | 16b68b1 | 2010-06-30 11:05:43 -0700 | [diff] [blame] | 365 | struct gl_shader *shader) |
Ian Romanick | 832dfa5 | 2010-06-17 15:04:20 -0700 | [diff] [blame] | 366 | { |
| 367 | if (shader == NULL) |
| 368 | return true; |
| 369 | |
Ian Romanick | 832dfa5 | 2010-06-17 15:04:20 -0700 | [diff] [blame] | 370 | find_assignment_visitor frag_color("gl_FragColor"); |
| 371 | find_assignment_visitor frag_data("gl_FragData"); |
| 372 | |
Eric Anholt | 16b68b1 | 2010-06-30 11:05:43 -0700 | [diff] [blame] | 373 | frag_color.run(shader->ir); |
| 374 | frag_data.run(shader->ir); |
Ian Romanick | 832dfa5 | 2010-06-17 15:04:20 -0700 | [diff] [blame] | 375 | |
Ian Romanick | 832dfa5 | 2010-06-17 15:04:20 -0700 | [diff] [blame] | 376 | if (frag_color.variable_found() && frag_data.variable_found()) { |
Ian Romanick | 586e741 | 2011-07-28 14:04:09 -0700 | [diff] [blame] | 377 | linker_error(prog, "fragment shader writes to both " |
| 378 | "`gl_FragColor' and `gl_FragData'\n"); |
Ian Romanick | 832dfa5 | 2010-06-17 15:04:20 -0700 | [diff] [blame] | 379 | return false; |
| 380 | } |
| 381 | |
| 382 | return true; |
| 383 | } |
| 384 | |
| 385 | |
Ian Romanick | cc22c5a | 2010-06-18 17:13:42 -0700 | [diff] [blame] | 386 | /** |
Ian Romanick | e2e5d0d | 2010-06-29 18:47:11 -0700 | [diff] [blame] | 387 | * Generate a string describing the mode of a variable |
| 388 | */ |
| 389 | static const char * |
| 390 | mode_string(const ir_variable *var) |
| 391 | { |
| 392 | switch (var->mode) { |
| 393 | case ir_var_auto: |
| 394 | return (var->read_only) ? "global constant" : "global variable"; |
| 395 | |
| 396 | case ir_var_uniform: return "uniform"; |
| 397 | case ir_var_in: return "shader input"; |
| 398 | case ir_var_out: return "shader output"; |
| 399 | case ir_var_inout: return "shader inout"; |
Ian Romanick | 7e2aa91 | 2010-07-19 17:12:42 -0700 | [diff] [blame] | 400 | |
Kenneth Graunke | 819d57f | 2011-01-12 15:37:37 -0800 | [diff] [blame] | 401 | case ir_var_const_in: |
Ian Romanick | 7e2aa91 | 2010-07-19 17:12:42 -0700 | [diff] [blame] | 402 | case ir_var_temporary: |
Ian Romanick | e2e5d0d | 2010-06-29 18:47:11 -0700 | [diff] [blame] | 403 | default: |
| 404 | assert(!"Should not get here."); |
| 405 | return "invalid variable"; |
| 406 | } |
| 407 | } |
| 408 | |
| 409 | |
| 410 | /** |
| 411 | * Perform validation of global variables used across multiple shaders |
Ian Romanick | cc22c5a | 2010-06-18 17:13:42 -0700 | [diff] [blame] | 412 | */ |
| 413 | bool |
Ian Romanick | e2e5d0d | 2010-06-29 18:47:11 -0700 | [diff] [blame] | 414 | cross_validate_globals(struct gl_shader_program *prog, |
| 415 | struct gl_shader **shader_list, |
| 416 | unsigned num_shaders, |
| 417 | bool uniforms_only) |
Ian Romanick | cc22c5a | 2010-06-18 17:13:42 -0700 | [diff] [blame] | 418 | { |
| 419 | /* Examine all of the uniforms in all of the shaders and cross validate |
| 420 | * them. |
| 421 | */ |
Ian Romanick | e2e5d0d | 2010-06-29 18:47:11 -0700 | [diff] [blame] | 422 | glsl_symbol_table variables; |
| 423 | for (unsigned i = 0; i < num_shaders; i++) { |
Ian Romanick | 3322fba | 2010-10-14 13:28:42 -0700 | [diff] [blame] | 424 | if (shader_list[i] == NULL) |
| 425 | continue; |
| 426 | |
Ian Romanick | e2e5d0d | 2010-06-29 18:47:11 -0700 | [diff] [blame] | 427 | foreach_list(node, shader_list[i]->ir) { |
Ian Romanick | cc22c5a | 2010-06-18 17:13:42 -0700 | [diff] [blame] | 428 | ir_variable *const var = ((ir_instruction *) node)->as_variable(); |
| 429 | |
Ian Romanick | e2e5d0d | 2010-06-29 18:47:11 -0700 | [diff] [blame] | 430 | if (var == NULL) |
Ian Romanick | cc22c5a | 2010-06-18 17:13:42 -0700 | [diff] [blame] | 431 | continue; |
| 432 | |
Ian Romanick | e2e5d0d | 2010-06-29 18:47:11 -0700 | [diff] [blame] | 433 | if (uniforms_only && (var->mode != ir_var_uniform)) |
| 434 | continue; |
| 435 | |
Ian Romanick | 7e2aa91 | 2010-07-19 17:12:42 -0700 | [diff] [blame] | 436 | /* Don't cross validate temporaries that are at global scope. These |
| 437 | * will eventually get pulled into the shaders 'main'. |
| 438 | */ |
| 439 | if (var->mode == ir_var_temporary) |
| 440 | continue; |
| 441 | |
Ian Romanick | e2e5d0d | 2010-06-29 18:47:11 -0700 | [diff] [blame] | 442 | /* If a global with this name has already been seen, verify that the |
| 443 | * new instance has the same type. In addition, if the globals have |
Ian Romanick | cc22c5a | 2010-06-18 17:13:42 -0700 | [diff] [blame] | 444 | * initializers, the values of the initializers must be the same. |
| 445 | */ |
Ian Romanick | e2e5d0d | 2010-06-29 18:47:11 -0700 | [diff] [blame] | 446 | ir_variable *const existing = variables.get_variable(var->name); |
Ian Romanick | cc22c5a | 2010-06-18 17:13:42 -0700 | [diff] [blame] | 447 | if (existing != NULL) { |
| 448 | if (var->type != existing->type) { |
Ian Romanick | a2711d6 | 2010-08-29 22:07:49 -0700 | [diff] [blame] | 449 | /* Consider the types to be "the same" if both types are arrays |
| 450 | * of the same type and one of the arrays is implicitly sized. |
| 451 | * In addition, set the type of the linked variable to the |
| 452 | * explicitly sized array. |
| 453 | */ |
| 454 | if (var->type->is_array() |
| 455 | && existing->type->is_array() |
| 456 | && (var->type->fields.array == existing->type->fields.array) |
| 457 | && ((var->type->length == 0) |
| 458 | || (existing->type->length == 0))) { |
Ian Romanick | 0f4b2a0 | 2011-01-25 12:06:18 -0800 | [diff] [blame] | 459 | if (var->type->length != 0) { |
Ian Romanick | a2711d6 | 2010-08-29 22:07:49 -0700 | [diff] [blame] | 460 | existing->type = var->type; |
Ian Romanick | 6f53921 | 2010-12-07 18:30:33 -0800 | [diff] [blame] | 461 | } |
Ian Romanick | a2711d6 | 2010-08-29 22:07:49 -0700 | [diff] [blame] | 462 | } else { |
Ian Romanick | 586e741 | 2011-07-28 14:04:09 -0700 | [diff] [blame] | 463 | linker_error(prog, "%s `%s' declared as type " |
| 464 | "`%s' and type `%s'\n", |
| 465 | mode_string(var), |
| 466 | var->name, var->type->name, |
| 467 | existing->type->name); |
Ian Romanick | a2711d6 | 2010-08-29 22:07:49 -0700 | [diff] [blame] | 468 | return false; |
| 469 | } |
Ian Romanick | cc22c5a | 2010-06-18 17:13:42 -0700 | [diff] [blame] | 470 | } |
| 471 | |
Ian Romanick | 68a4fc9 | 2010-10-07 17:21:22 -0700 | [diff] [blame] | 472 | if (var->explicit_location) { |
| 473 | if (existing->explicit_location |
| 474 | && (var->location != existing->location)) { |
Ian Romanick | 586e741 | 2011-07-28 14:04:09 -0700 | [diff] [blame] | 475 | linker_error(prog, "explicit locations for %s " |
| 476 | "`%s' have differing values\n", |
| 477 | mode_string(var), var->name); |
Ian Romanick | 68a4fc9 | 2010-10-07 17:21:22 -0700 | [diff] [blame] | 478 | return false; |
| 479 | } |
| 480 | |
| 481 | existing->location = var->location; |
| 482 | existing->explicit_location = true; |
| 483 | } |
| 484 | |
Ian Romanick | 46173f9 | 2011-10-31 13:07:06 -0700 | [diff] [blame] | 485 | /* Validate layout qualifiers for gl_FragDepth. |
| 486 | * |
| 487 | * From the AMD/ARB_conservative_depth specs: |
| 488 | * |
| 489 | * "If gl_FragDepth is redeclared in any fragment shader in a |
| 490 | * program, it must be redeclared in all fragment shaders in |
| 491 | * that program that have static assignments to |
| 492 | * gl_FragDepth. All redeclarations of gl_FragDepth in all |
| 493 | * fragment shaders in a single program must have the same set |
| 494 | * of qualifiers." |
| 495 | */ |
| 496 | if (strcmp(var->name, "gl_FragDepth") == 0) { |
| 497 | bool layout_declared = var->depth_layout != ir_depth_layout_none; |
| 498 | bool layout_differs = |
| 499 | var->depth_layout != existing->depth_layout; |
| 500 | |
| 501 | if (layout_declared && layout_differs) { |
| 502 | linker_error(prog, |
| 503 | "All redeclarations of gl_FragDepth in all " |
| 504 | "fragment shaders in a single program must have " |
| 505 | "the same set of qualifiers."); |
| 506 | } |
| 507 | |
| 508 | if (var->used && layout_differs) { |
| 509 | linker_error(prog, |
| 510 | "If gl_FragDepth is redeclared with a layout " |
| 511 | "qualifier in any fragment shader, it must be " |
| 512 | "redeclared with the same layout qualifier in " |
| 513 | "all fragment shaders that have assignments to " |
| 514 | "gl_FragDepth"); |
| 515 | } |
| 516 | } |
Chad Versace | addae33 | 2011-01-27 01:40:31 -0800 | [diff] [blame] | 517 | |
Ian Romanick | f37b1ad | 2011-10-31 14:31:07 -0700 | [diff] [blame] | 518 | /* Page 35 (page 41 of the PDF) of the GLSL 4.20 spec says: |
| 519 | * |
| 520 | * "If a shared global has multiple initializers, the |
| 521 | * initializers must all be constant expressions, and they |
| 522 | * must all have the same value. Otherwise, a link error will |
| 523 | * result. (A shared global having only one initializer does |
| 524 | * not require that initializer to be a constant expression.)" |
| 525 | * |
| 526 | * Previous to 4.20 the GLSL spec simply said that initializers |
| 527 | * must have the same value. In this case of non-constant |
| 528 | * initializers, this was impossible to determine. As a result, |
| 529 | * no vendor actually implemented that behavior. The 4.20 |
| 530 | * behavior matches the implemented behavior of at least one other |
| 531 | * vendor, so we'll implement that for all GLSL versions. |
Ian Romanick | e2e5d0d | 2010-06-29 18:47:11 -0700 | [diff] [blame] | 532 | */ |
Ian Romanick | f37b1ad | 2011-10-31 14:31:07 -0700 | [diff] [blame] | 533 | if (var->constant_initializer != NULL) { |
| 534 | if (existing->constant_initializer != NULL) { |
| 535 | if (!var->constant_initializer->has_value(existing->constant_initializer)) { |
Ian Romanick | 586e741 | 2011-07-28 14:04:09 -0700 | [diff] [blame] | 536 | linker_error(prog, "initializers for %s " |
| 537 | "`%s' have differing values\n", |
| 538 | mode_string(var), var->name); |
Ian Romanick | cc22c5a | 2010-06-18 17:13:42 -0700 | [diff] [blame] | 539 | return false; |
| 540 | } |
Ian Romanick | f37b1ad | 2011-10-31 14:31:07 -0700 | [diff] [blame] | 541 | } else { |
Ian Romanick | cc22c5a | 2010-06-18 17:13:42 -0700 | [diff] [blame] | 542 | /* If the first-seen instance of a particular uniform did not |
| 543 | * have an initializer but a later instance does, copy the |
| 544 | * initializer to the version stored in the symbol table. |
| 545 | */ |
Ian Romanick | de415b7 | 2010-07-14 13:22:12 -0700 | [diff] [blame] | 546 | /* FINISHME: This is wrong. The constant_value field should |
| 547 | * FINISHME: not be modified! Imagine a case where a shader |
| 548 | * FINISHME: without an initializer is linked in two different |
| 549 | * FINISHME: programs with shaders that have differing |
| 550 | * FINISHME: initializers. Linking with the first will |
| 551 | * FINISHME: modify the shader, and linking with the second |
| 552 | * FINISHME: will fail. |
| 553 | */ |
Ian Romanick | f37b1ad | 2011-10-31 14:31:07 -0700 | [diff] [blame] | 554 | existing->constant_initializer = |
| 555 | var->constant_initializer->clone(ralloc_parent(existing), |
| 556 | NULL); |
| 557 | } |
| 558 | } |
| 559 | |
| 560 | if (var->has_initializer) { |
| 561 | if (existing->has_initializer |
| 562 | && (var->constant_initializer == NULL |
| 563 | || existing->constant_initializer == NULL)) { |
| 564 | linker_error(prog, |
| 565 | "shared global variable `%s' has multiple " |
| 566 | "non-constant initializers.\n", |
| 567 | var->name); |
| 568 | return false; |
| 569 | } |
| 570 | |
| 571 | /* Some instance had an initializer, so keep track of that. In |
| 572 | * this location, all sorts of initializers (constant or |
| 573 | * otherwise) will propagate the existence to the variable |
| 574 | * stored in the symbol table. |
| 575 | */ |
| 576 | existing->has_initializer = true; |
Ian Romanick | cc22c5a | 2010-06-18 17:13:42 -0700 | [diff] [blame] | 577 | } |
Chad Versace | 7528f14 | 2010-11-17 14:34:38 -0800 | [diff] [blame] | 578 | |
| 579 | if (existing->invariant != var->invariant) { |
Ian Romanick | 586e741 | 2011-07-28 14:04:09 -0700 | [diff] [blame] | 580 | linker_error(prog, "declarations for %s `%s' have " |
| 581 | "mismatching invariant qualifiers\n", |
| 582 | mode_string(var), var->name); |
Chad Versace | 7528f14 | 2010-11-17 14:34:38 -0800 | [diff] [blame] | 583 | return false; |
| 584 | } |
Chad Versace | 61428dd | 2011-01-10 15:29:30 -0800 | [diff] [blame] | 585 | if (existing->centroid != var->centroid) { |
Ian Romanick | 586e741 | 2011-07-28 14:04:09 -0700 | [diff] [blame] | 586 | linker_error(prog, "declarations for %s `%s' have " |
| 587 | "mismatching centroid qualifiers\n", |
| 588 | mode_string(var), var->name); |
Chad Versace | 61428dd | 2011-01-10 15:29:30 -0800 | [diff] [blame] | 589 | return false; |
| 590 | } |
Ian Romanick | cc22c5a | 2010-06-18 17:13:42 -0700 | [diff] [blame] | 591 | } else |
Eric Anholt | 001eee5 | 2010-11-05 06:11:24 -0700 | [diff] [blame] | 592 | variables.add_variable(var); |
Ian Romanick | cc22c5a | 2010-06-18 17:13:42 -0700 | [diff] [blame] | 593 | } |
| 594 | } |
| 595 | |
| 596 | return true; |
| 597 | } |
| 598 | |
| 599 | |
Ian Romanick | 3710192 | 2010-06-18 19:02:10 -0700 | [diff] [blame] | 600 | /** |
Ian Romanick | e2e5d0d | 2010-06-29 18:47:11 -0700 | [diff] [blame] | 601 | * Perform validation of uniforms used across multiple shader stages |
| 602 | */ |
| 603 | bool |
| 604 | cross_validate_uniforms(struct gl_shader_program *prog) |
| 605 | { |
| 606 | return cross_validate_globals(prog, prog->_LinkedShaders, |
Ian Romanick | 3322fba | 2010-10-14 13:28:42 -0700 | [diff] [blame] | 607 | MESA_SHADER_TYPES, true); |
Ian Romanick | e2e5d0d | 2010-06-29 18:47:11 -0700 | [diff] [blame] | 608 | } |
| 609 | |
Eric Anholt | f609cf7 | 2012-04-27 13:52:56 -0700 | [diff] [blame] | 610 | /** |
| 611 | * Accumulates the array of prog->UniformBlocks and checks that all |
| 612 | * definitons of blocks agree on their contents. |
| 613 | */ |
| 614 | static bool |
| 615 | interstage_cross_validate_uniform_blocks(struct gl_shader_program *prog) |
| 616 | { |
| 617 | unsigned max_num_uniform_blocks = 0; |
| 618 | for (unsigned i = 0; i < MESA_SHADER_TYPES; i++) { |
| 619 | if (prog->_LinkedShaders[i]) |
| 620 | max_num_uniform_blocks += prog->_LinkedShaders[i]->NumUniformBlocks; |
| 621 | } |
| 622 | |
| 623 | for (unsigned i = 0; i < MESA_SHADER_TYPES; i++) { |
| 624 | struct gl_shader *sh = prog->_LinkedShaders[i]; |
| 625 | |
| 626 | prog->UniformBlockStageIndex[i] = ralloc_array(prog, int, |
| 627 | max_num_uniform_blocks); |
| 628 | for (unsigned int j = 0; j < max_num_uniform_blocks; j++) |
| 629 | prog->UniformBlockStageIndex[i][j] = -1; |
| 630 | |
| 631 | if (sh == NULL) |
| 632 | continue; |
| 633 | |
| 634 | for (unsigned int j = 0; j < sh->NumUniformBlocks; j++) { |
| 635 | int index = link_cross_validate_uniform_block(prog, |
| 636 | &prog->UniformBlocks, |
| 637 | &prog->NumUniformBlocks, |
| 638 | &sh->UniformBlocks[j]); |
| 639 | |
| 640 | if (index == -1) { |
| 641 | linker_error(prog, "uniform block `%s' has mismatching definitions", |
| 642 | sh->UniformBlocks[j].Name); |
| 643 | return false; |
| 644 | } |
| 645 | |
| 646 | prog->UniformBlockStageIndex[i][index] = j; |
| 647 | } |
| 648 | } |
| 649 | |
| 650 | return true; |
| 651 | } |
Ian Romanick | e2e5d0d | 2010-06-29 18:47:11 -0700 | [diff] [blame] | 652 | |
Ian Romanick | 3710192 | 2010-06-18 19:02:10 -0700 | [diff] [blame] | 653 | |
Ian Romanick | 3fb8787 | 2010-07-09 14:09:34 -0700 | [diff] [blame] | 654 | /** |
| 655 | * Populates a shaders symbol table with all global declarations |
| 656 | */ |
| 657 | static void |
| 658 | populate_symbol_table(gl_shader *sh) |
| 659 | { |
| 660 | sh->symbols = new(sh) glsl_symbol_table; |
| 661 | |
| 662 | foreach_list(node, sh->ir) { |
| 663 | ir_instruction *const inst = (ir_instruction *) node; |
| 664 | ir_variable *var; |
| 665 | ir_function *func; |
| 666 | |
| 667 | if ((func = inst->as_function()) != NULL) { |
Eric Anholt | e8f5ebf | 2010-11-05 06:08:45 -0700 | [diff] [blame] | 668 | sh->symbols->add_function(func); |
Ian Romanick | 3fb8787 | 2010-07-09 14:09:34 -0700 | [diff] [blame] | 669 | } else if ((var = inst->as_variable()) != NULL) { |
Eric Anholt | 001eee5 | 2010-11-05 06:11:24 -0700 | [diff] [blame] | 670 | sh->symbols->add_variable(var); |
Ian Romanick | 3fb8787 | 2010-07-09 14:09:34 -0700 | [diff] [blame] | 671 | } |
| 672 | } |
| 673 | } |
| 674 | |
| 675 | |
| 676 | /** |
Ian Romanick | 31a9786 | 2010-07-12 18:48:50 -0700 | [diff] [blame] | 677 | * Remap variables referenced in an instruction tree |
| 678 | * |
| 679 | * This is used when instruction trees are cloned from one shader and placed in |
| 680 | * another. These trees will contain references to \c ir_variable nodes that |
| 681 | * do not exist in the target shader. This function finds these \c ir_variable |
| 682 | * references and replaces the references with matching variables in the target |
| 683 | * shader. |
| 684 | * |
| 685 | * If there is no matching variable in the target shader, a clone of the |
| 686 | * \c ir_variable is made and added to the target shader. The new variable is |
| 687 | * added to \b both the instruction stream and the symbol table. |
| 688 | * |
| 689 | * \param inst IR tree that is to be processed. |
| 690 | * \param symbols Symbol table containing global scope symbols in the |
| 691 | * linked shader. |
| 692 | * \param instructions Instruction stream where new variable declarations |
| 693 | * should be added. |
| 694 | */ |
| 695 | void |
Eric Anholt | 8273bd4 | 2010-08-04 12:34:56 -0700 | [diff] [blame] | 696 | remap_variables(ir_instruction *inst, struct gl_shader *target, |
| 697 | hash_table *temps) |
Ian Romanick | 31a9786 | 2010-07-12 18:48:50 -0700 | [diff] [blame] | 698 | { |
| 699 | class remap_visitor : public ir_hierarchical_visitor { |
| 700 | public: |
Eric Anholt | 8273bd4 | 2010-08-04 12:34:56 -0700 | [diff] [blame] | 701 | remap_visitor(struct gl_shader *target, |
Ian Romanick | 7e2aa91 | 2010-07-19 17:12:42 -0700 | [diff] [blame] | 702 | hash_table *temps) |
Ian Romanick | 31a9786 | 2010-07-12 18:48:50 -0700 | [diff] [blame] | 703 | { |
Eric Anholt | 8273bd4 | 2010-08-04 12:34:56 -0700 | [diff] [blame] | 704 | this->target = target; |
| 705 | this->symbols = target->symbols; |
| 706 | this->instructions = target->ir; |
Ian Romanick | 7e2aa91 | 2010-07-19 17:12:42 -0700 | [diff] [blame] | 707 | this->temps = temps; |
Ian Romanick | 31a9786 | 2010-07-12 18:48:50 -0700 | [diff] [blame] | 708 | } |
| 709 | |
| 710 | virtual ir_visitor_status visit(ir_dereference_variable *ir) |
| 711 | { |
Ian Romanick | 7e2aa91 | 2010-07-19 17:12:42 -0700 | [diff] [blame] | 712 | if (ir->var->mode == ir_var_temporary) { |
| 713 | ir_variable *var = (ir_variable *) hash_table_find(temps, ir->var); |
| 714 | |
| 715 | assert(var != NULL); |
| 716 | ir->var = var; |
| 717 | return visit_continue; |
| 718 | } |
| 719 | |
Ian Romanick | 31a9786 | 2010-07-12 18:48:50 -0700 | [diff] [blame] | 720 | ir_variable *const existing = |
| 721 | this->symbols->get_variable(ir->var->name); |
| 722 | if (existing != NULL) |
| 723 | ir->var = existing; |
| 724 | else { |
Eric Anholt | 8273bd4 | 2010-08-04 12:34:56 -0700 | [diff] [blame] | 725 | ir_variable *copy = ir->var->clone(this->target, NULL); |
Ian Romanick | 31a9786 | 2010-07-12 18:48:50 -0700 | [diff] [blame] | 726 | |
Eric Anholt | 001eee5 | 2010-11-05 06:11:24 -0700 | [diff] [blame] | 727 | this->symbols->add_variable(copy); |
Ian Romanick | 31a9786 | 2010-07-12 18:48:50 -0700 | [diff] [blame] | 728 | this->instructions->push_head(copy); |
Ian Romanick | 7e2aa91 | 2010-07-19 17:12:42 -0700 | [diff] [blame] | 729 | ir->var = copy; |
Ian Romanick | 31a9786 | 2010-07-12 18:48:50 -0700 | [diff] [blame] | 730 | } |
| 731 | |
| 732 | return visit_continue; |
| 733 | } |
| 734 | |
| 735 | private: |
Eric Anholt | 8273bd4 | 2010-08-04 12:34:56 -0700 | [diff] [blame] | 736 | struct gl_shader *target; |
Ian Romanick | 31a9786 | 2010-07-12 18:48:50 -0700 | [diff] [blame] | 737 | glsl_symbol_table *symbols; |
| 738 | exec_list *instructions; |
Ian Romanick | 7e2aa91 | 2010-07-19 17:12:42 -0700 | [diff] [blame] | 739 | hash_table *temps; |
Ian Romanick | 31a9786 | 2010-07-12 18:48:50 -0700 | [diff] [blame] | 740 | }; |
| 741 | |
Eric Anholt | 8273bd4 | 2010-08-04 12:34:56 -0700 | [diff] [blame] | 742 | remap_visitor v(target, temps); |
Ian Romanick | 31a9786 | 2010-07-12 18:48:50 -0700 | [diff] [blame] | 743 | |
| 744 | inst->accept(&v); |
| 745 | } |
| 746 | |
| 747 | |
| 748 | /** |
| 749 | * Move non-declarations from one instruction stream to another |
| 750 | * |
| 751 | * 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] | 752 | * 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] | 753 | * pointer) for \c last and \c false for \c make_copies on the first |
| 754 | * call. Successive calls pass the return value of the previous call for |
| 755 | * \c last and \c true for \c make_copies. |
| 756 | * |
| 757 | * \param instructions Source instruction stream |
| 758 | * \param last Instruction after which new instructions should be |
| 759 | * inserted in the target instruction stream |
| 760 | * \param make_copies Flag selecting whether instructions in \c instructions |
| 761 | * should be copied (via \c ir_instruction::clone) into the |
| 762 | * target list or moved. |
| 763 | * |
| 764 | * \return |
| 765 | * The new "last" instruction in the target instruction stream. This pointer |
| 766 | * is suitable for use as the \c last parameter of a later call to this |
| 767 | * function. |
| 768 | */ |
| 769 | exec_node * |
| 770 | move_non_declarations(exec_list *instructions, exec_node *last, |
| 771 | bool make_copies, gl_shader *target) |
| 772 | { |
Ian Romanick | 7e2aa91 | 2010-07-19 17:12:42 -0700 | [diff] [blame] | 773 | hash_table *temps = NULL; |
| 774 | |
| 775 | if (make_copies) |
| 776 | temps = hash_table_ctor(0, hash_table_pointer_hash, |
| 777 | hash_table_pointer_compare); |
| 778 | |
Ian Romanick | 303c99f | 2010-07-19 12:34:56 -0700 | [diff] [blame] | 779 | foreach_list_safe(node, instructions) { |
Ian Romanick | 31a9786 | 2010-07-12 18:48:50 -0700 | [diff] [blame] | 780 | ir_instruction *inst = (ir_instruction *) node; |
| 781 | |
Ian Romanick | 7e2aa91 | 2010-07-19 17:12:42 -0700 | [diff] [blame] | 782 | if (inst->as_function()) |
Ian Romanick | 31a9786 | 2010-07-12 18:48:50 -0700 | [diff] [blame] | 783 | continue; |
| 784 | |
Ian Romanick | 7e2aa91 | 2010-07-19 17:12:42 -0700 | [diff] [blame] | 785 | ir_variable *var = inst->as_variable(); |
| 786 | if ((var != NULL) && (var->mode != ir_var_temporary)) |
| 787 | continue; |
| 788 | |
| 789 | assert(inst->as_assignment() |
Kenneth Graunke | d884f60 | 2012-03-20 15:56:37 -0700 | [diff] [blame] | 790 | || inst->as_call() |
Kenneth Graunke | b45a68e | 2012-10-24 13:17:24 -0700 | [diff] [blame] | 791 | || inst->as_if() /* for initializers with the ?: operator */ |
Ian Romanick | 7e2aa91 | 2010-07-19 17:12:42 -0700 | [diff] [blame] | 792 | || ((var != NULL) && (var->mode == ir_var_temporary))); |
Ian Romanick | 31a9786 | 2010-07-12 18:48:50 -0700 | [diff] [blame] | 793 | |
| 794 | if (make_copies) { |
Eric Anholt | 8273bd4 | 2010-08-04 12:34:56 -0700 | [diff] [blame] | 795 | inst = inst->clone(target, NULL); |
Ian Romanick | 7e2aa91 | 2010-07-19 17:12:42 -0700 | [diff] [blame] | 796 | |
| 797 | if (var != NULL) |
| 798 | hash_table_insert(temps, inst, var); |
| 799 | else |
Eric Anholt | 8273bd4 | 2010-08-04 12:34:56 -0700 | [diff] [blame] | 800 | remap_variables(inst, target, temps); |
Ian Romanick | 31a9786 | 2010-07-12 18:48:50 -0700 | [diff] [blame] | 801 | } else { |
| 802 | inst->remove(); |
| 803 | } |
| 804 | |
| 805 | last->insert_after(inst); |
| 806 | last = inst; |
| 807 | } |
| 808 | |
Ian Romanick | 7e2aa91 | 2010-07-19 17:12:42 -0700 | [diff] [blame] | 809 | if (make_copies) |
| 810 | hash_table_dtor(temps); |
| 811 | |
Ian Romanick | 31a9786 | 2010-07-12 18:48:50 -0700 | [diff] [blame] | 812 | return last; |
| 813 | } |
| 814 | |
| 815 | /** |
Ian Romanick | 15ce87e | 2010-07-09 15:28:22 -0700 | [diff] [blame] | 816 | * Get the function signature for main from a shader |
| 817 | */ |
| 818 | static ir_function_signature * |
| 819 | get_main_function_signature(gl_shader *sh) |
| 820 | { |
| 821 | ir_function *const f = sh->symbols->get_function("main"); |
| 822 | if (f != NULL) { |
| 823 | exec_list void_parameters; |
| 824 | |
| 825 | /* Look for the 'void main()' signature and ensure that it's defined. |
| 826 | * This keeps the linker from accidentally pick a shader that just |
| 827 | * contains a prototype for main. |
| 828 | * |
| 829 | * We don't have to check for multiple definitions of main (in multiple |
| 830 | * shaders) because that would have already been caught above. |
| 831 | */ |
| 832 | ir_function_signature *sig = f->matching_signature(&void_parameters); |
| 833 | if ((sig != NULL) && sig->is_defined) { |
| 834 | return sig; |
| 835 | } |
| 836 | } |
| 837 | |
| 838 | return NULL; |
| 839 | } |
| 840 | |
| 841 | |
| 842 | /** |
Brian Paul | 84a1273 | 2012-02-02 20:10:40 -0700 | [diff] [blame] | 843 | * This class is only used in link_intrastage_shaders() below but declaring |
| 844 | * it inside that function leads to compiler warnings with some versions of |
| 845 | * gcc. |
| 846 | */ |
| 847 | class array_sizing_visitor : public ir_hierarchical_visitor { |
| 848 | public: |
| 849 | virtual ir_visitor_status visit(ir_variable *var) |
| 850 | { |
| 851 | if (var->type->is_array() && (var->type->length == 0)) { |
| 852 | const glsl_type *type = |
| 853 | glsl_type::get_array_instance(var->type->fields.array, |
| 854 | var->max_array_access + 1); |
| 855 | assert(type != NULL); |
| 856 | var->type = type; |
| 857 | } |
| 858 | return visit_continue; |
| 859 | } |
| 860 | }; |
| 861 | |
Brian Paul | 84a1273 | 2012-02-02 20:10:40 -0700 | [diff] [blame] | 862 | /** |
Ian Romanick | 3fb8787 | 2010-07-09 14:09:34 -0700 | [diff] [blame] | 863 | * Combine a group of shaders for a single stage to generate a linked shader |
| 864 | * |
| 865 | * \note |
| 866 | * If this function is supplied a single shader, it is cloned, and the new |
| 867 | * shader is returned. |
| 868 | */ |
| 869 | static struct gl_shader * |
Kenneth Graunke | 2da02e7 | 2010-11-17 11:03:57 -0800 | [diff] [blame] | 870 | link_intrastage_shaders(void *mem_ctx, |
| 871 | struct gl_context *ctx, |
Eric Anholt | 5d0f430 | 2010-08-18 12:02:35 -0700 | [diff] [blame] | 872 | struct gl_shader_program *prog, |
Ian Romanick | 3fb8787 | 2010-07-09 14:09:34 -0700 | [diff] [blame] | 873 | struct gl_shader **shader_list, |
| 874 | unsigned num_shaders) |
| 875 | { |
Eric Anholt | f609cf7 | 2012-04-27 13:52:56 -0700 | [diff] [blame] | 876 | struct gl_uniform_block *uniform_blocks = NULL; |
| 877 | unsigned num_uniform_blocks = 0; |
| 878 | |
Ian Romanick | 13f782c | 2010-06-29 18:53:38 -0700 | [diff] [blame] | 879 | /* Check that global variables defined in multiple shaders are consistent. |
| 880 | */ |
| 881 | if (!cross_validate_globals(prog, shader_list, num_shaders, false)) |
| 882 | return NULL; |
| 883 | |
Eric Anholt | f609cf7 | 2012-04-27 13:52:56 -0700 | [diff] [blame] | 884 | /* Check that uniform blocks between shaders for a stage agree. */ |
| 885 | for (unsigned i = 0; i < num_shaders; i++) { |
| 886 | struct gl_shader *sh = shader_list[i]; |
| 887 | |
| 888 | for (unsigned j = 0; j < shader_list[i]->NumUniformBlocks; j++) { |
Eric Anholt | 8ab5842 | 2012-05-01 15:10:14 -0700 | [diff] [blame] | 889 | link_assign_uniform_block_offsets(shader_list[i]); |
| 890 | |
Eric Anholt | f609cf7 | 2012-04-27 13:52:56 -0700 | [diff] [blame] | 891 | int index = link_cross_validate_uniform_block(mem_ctx, |
| 892 | &uniform_blocks, |
| 893 | &num_uniform_blocks, |
| 894 | &sh->UniformBlocks[j]); |
| 895 | if (index == -1) { |
| 896 | linker_error(prog, "uniform block `%s' has mismatching definitions", |
| 897 | sh->UniformBlocks[j].Name); |
| 898 | return NULL; |
| 899 | } |
| 900 | } |
| 901 | } |
| 902 | |
Ian Romanick | 13f782c | 2010-06-29 18:53:38 -0700 | [diff] [blame] | 903 | /* Check that there is only a single definition of each function signature |
| 904 | * across all shaders. |
| 905 | */ |
| 906 | for (unsigned i = 0; i < (num_shaders - 1); i++) { |
| 907 | foreach_list(node, shader_list[i]->ir) { |
| 908 | ir_function *const f = ((ir_instruction *) node)->as_function(); |
| 909 | |
| 910 | if (f == NULL) |
| 911 | continue; |
| 912 | |
| 913 | for (unsigned j = i + 1; j < num_shaders; j++) { |
| 914 | ir_function *const other = |
| 915 | shader_list[j]->symbols->get_function(f->name); |
| 916 | |
| 917 | /* If the other shader has no function (and therefore no function |
| 918 | * signatures) with the same name, skip to the next shader. |
| 919 | */ |
| 920 | if (other == NULL) |
| 921 | continue; |
| 922 | |
| 923 | foreach_iter (exec_list_iterator, iter, *f) { |
| 924 | ir_function_signature *sig = |
| 925 | (ir_function_signature *) iter.get(); |
| 926 | |
Kenneth Graunke | f412fac | 2010-09-05 01:48:11 -0700 | [diff] [blame] | 927 | if (!sig->is_defined || sig->is_builtin) |
Ian Romanick | 13f782c | 2010-06-29 18:53:38 -0700 | [diff] [blame] | 928 | continue; |
| 929 | |
| 930 | ir_function_signature *other_sig = |
| 931 | other->exact_matching_signature(& sig->parameters); |
| 932 | |
| 933 | if ((other_sig != NULL) && other_sig->is_defined |
Kenneth Graunke | f412fac | 2010-09-05 01:48:11 -0700 | [diff] [blame] | 934 | && !other_sig->is_builtin) { |
Ian Romanick | 586e741 | 2011-07-28 14:04:09 -0700 | [diff] [blame] | 935 | linker_error(prog, "function `%s' is multiply defined", |
| 936 | f->name); |
Ian Romanick | 13f782c | 2010-06-29 18:53:38 -0700 | [diff] [blame] | 937 | return NULL; |
| 938 | } |
| 939 | } |
| 940 | } |
| 941 | } |
| 942 | } |
| 943 | |
| 944 | /* Find the shader that defines main, and make a clone of it. |
| 945 | * |
| 946 | * Starting with the clone, search for undefined references. If one is |
| 947 | * found, find the shader that defines it. Clone the reference and add |
| 948 | * it to the shader. Repeat until there are no undefined references or |
| 949 | * until a reference cannot be resolved. |
| 950 | */ |
Ian Romanick | 15ce87e | 2010-07-09 15:28:22 -0700 | [diff] [blame] | 951 | gl_shader *main = NULL; |
| 952 | for (unsigned i = 0; i < num_shaders; i++) { |
| 953 | if (get_main_function_signature(shader_list[i]) != NULL) { |
| 954 | main = shader_list[i]; |
| 955 | break; |
| 956 | } |
| 957 | } |
Ian Romanick | 13f782c | 2010-06-29 18:53:38 -0700 | [diff] [blame] | 958 | |
Ian Romanick | 15ce87e | 2010-07-09 15:28:22 -0700 | [diff] [blame] | 959 | if (main == NULL) { |
Ian Romanick | 586e741 | 2011-07-28 14:04:09 -0700 | [diff] [blame] | 960 | linker_error(prog, "%s shader lacks `main'\n", |
| 961 | (shader_list[0]->Type == GL_VERTEX_SHADER) |
| 962 | ? "vertex" : "fragment"); |
Ian Romanick | 15ce87e | 2010-07-09 15:28:22 -0700 | [diff] [blame] | 963 | return NULL; |
| 964 | } |
| 965 | |
Ian Romanick | 4a45595 | 2010-10-13 15:13:02 -0700 | [diff] [blame] | 966 | gl_shader *linked = ctx->Driver.NewShader(NULL, 0, main->Type); |
Ian Romanick | 15ce87e | 2010-07-09 15:28:22 -0700 | [diff] [blame] | 967 | linked->ir = new(linked) exec_list; |
Kenneth Graunke | 2da02e7 | 2010-11-17 11:03:57 -0800 | [diff] [blame] | 968 | clone_ir_list(mem_ctx, linked->ir, main->ir); |
Ian Romanick | 15ce87e | 2010-07-09 15:28:22 -0700 | [diff] [blame] | 969 | |
Eric Anholt | f609cf7 | 2012-04-27 13:52:56 -0700 | [diff] [blame] | 970 | linked->UniformBlocks = uniform_blocks; |
| 971 | linked->NumUniformBlocks = num_uniform_blocks; |
| 972 | ralloc_steal(linked, linked->UniformBlocks); |
| 973 | |
Ian Romanick | 15ce87e | 2010-07-09 15:28:22 -0700 | [diff] [blame] | 974 | populate_symbol_table(linked); |
Ian Romanick | 13f782c | 2010-06-29 18:53:38 -0700 | [diff] [blame] | 975 | |
Ian Romanick | 31a9786 | 2010-07-12 18:48:50 -0700 | [diff] [blame] | 976 | /* The a pointer to the main function in the final linked shader (i.e., the |
| 977 | * copy of the original shader that contained the main function). |
| 978 | */ |
| 979 | ir_function_signature *const main_sig = get_main_function_signature(linked); |
| 980 | |
| 981 | /* Move any instructions other than variable declarations or function |
| 982 | * declarations into main. |
| 983 | */ |
Ian Romanick | 9303e35 | 2010-07-19 12:33:54 -0700 | [diff] [blame] | 984 | exec_node *insertion_point = |
| 985 | move_non_declarations(linked->ir, (exec_node *) &main_sig->body, false, |
| 986 | linked); |
| 987 | |
Ian Romanick | 31a9786 | 2010-07-12 18:48:50 -0700 | [diff] [blame] | 988 | for (unsigned i = 0; i < num_shaders; i++) { |
Ian Romanick | 9303e35 | 2010-07-19 12:33:54 -0700 | [diff] [blame] | 989 | if (shader_list[i] == main) |
| 990 | continue; |
| 991 | |
Ian Romanick | 31a9786 | 2010-07-12 18:48:50 -0700 | [diff] [blame] | 992 | insertion_point = move_non_declarations(shader_list[i]->ir, |
Ian Romanick | 9303e35 | 2010-07-19 12:33:54 -0700 | [diff] [blame] | 993 | insertion_point, true, linked); |
Ian Romanick | 31a9786 | 2010-07-12 18:48:50 -0700 | [diff] [blame] | 994 | } |
| 995 | |
Ian Romanick | 13f782c | 2010-06-29 18:53:38 -0700 | [diff] [blame] | 996 | /* Resolve initializers for global variables in the linked shader. |
| 997 | */ |
Ian Romanick | d5be2ac | 2010-07-20 11:29:46 -0700 | [diff] [blame] | 998 | unsigned num_linking_shaders = num_shaders; |
| 999 | for (unsigned i = 0; i < num_shaders; i++) |
| 1000 | num_linking_shaders += shader_list[i]->num_builtins_to_link; |
| 1001 | |
| 1002 | gl_shader **linking_shaders = |
| 1003 | (gl_shader **) calloc(num_linking_shaders, sizeof(gl_shader *)); |
| 1004 | |
| 1005 | memcpy(linking_shaders, shader_list, |
| 1006 | sizeof(linking_shaders[0]) * num_shaders); |
| 1007 | |
| 1008 | unsigned idx = num_shaders; |
| 1009 | for (unsigned i = 0; i < num_shaders; i++) { |
| 1010 | memcpy(&linking_shaders[idx], shader_list[i]->builtins_to_link, |
| 1011 | sizeof(linking_shaders[0]) * shader_list[i]->num_builtins_to_link); |
| 1012 | idx += shader_list[i]->num_builtins_to_link; |
| 1013 | } |
| 1014 | |
| 1015 | assert(idx == num_linking_shaders); |
| 1016 | |
Ian Romanick | 4a45595 | 2010-10-13 15:13:02 -0700 | [diff] [blame] | 1017 | if (!link_function_calls(prog, linked, linking_shaders, |
| 1018 | num_linking_shaders)) { |
| 1019 | ctx->Driver.DeleteShader(ctx, linked); |
| 1020 | linked = NULL; |
| 1021 | } |
Ian Romanick | d5be2ac | 2010-07-20 11:29:46 -0700 | [diff] [blame] | 1022 | |
| 1023 | free(linking_shaders); |
Ian Romanick | 3fb8787 | 2010-07-09 14:09:34 -0700 | [diff] [blame] | 1024 | |
Paul Berry | c148ef6 | 2011-08-03 15:37:01 -0700 | [diff] [blame] | 1025 | #ifdef DEBUG |
| 1026 | /* At this point linked should contain all of the linked IR, so |
| 1027 | * validate it to make sure nothing went wrong. |
| 1028 | */ |
| 1029 | if (linked) |
| 1030 | validate_ir_tree(linked->ir); |
| 1031 | #endif |
| 1032 | |
Ian Romanick | c87e9ef | 2011-01-25 12:04:08 -0800 | [diff] [blame] | 1033 | /* Make a pass over all variable declarations to ensure that arrays with |
Ian Romanick | 6f53921 | 2010-12-07 18:30:33 -0800 | [diff] [blame] | 1034 | * unspecified sizes have a size specified. The size is inferred from the |
| 1035 | * max_array_access field. |
| 1036 | */ |
Ian Romanick | 002cd2c | 2010-12-07 19:00:44 -0800 | [diff] [blame] | 1037 | if (linked != NULL) { |
Brian Paul | 84a1273 | 2012-02-02 20:10:40 -0700 | [diff] [blame] | 1038 | array_sizing_visitor v; |
Ian Romanick | 6f53921 | 2010-12-07 18:30:33 -0800 | [diff] [blame] | 1039 | |
Ian Romanick | c87e9ef | 2011-01-25 12:04:08 -0800 | [diff] [blame] | 1040 | v.run(linked->ir); |
Ian Romanick | 6f53921 | 2010-12-07 18:30:33 -0800 | [diff] [blame] | 1041 | } |
| 1042 | |
Ian Romanick | 3fb8787 | 2010-07-09 14:09:34 -0700 | [diff] [blame] | 1043 | return linked; |
| 1044 | } |
| 1045 | |
Eric Anholt | a721abf | 2010-08-23 10:32:01 -0700 | [diff] [blame] | 1046 | /** |
| 1047 | * Update the sizes of linked shader uniform arrays to the maximum |
| 1048 | * array index used. |
| 1049 | * |
| 1050 | * From page 81 (page 95 of the PDF) of the OpenGL 2.1 spec: |
| 1051 | * |
| 1052 | * If one or more elements of an array are active, |
| 1053 | * GetActiveUniform will return the name of the array in name, |
| 1054 | * subject to the restrictions listed above. The type of the array |
| 1055 | * is returned in type. The size parameter contains the highest |
| 1056 | * array element index used, plus one. The compiler or linker |
| 1057 | * determines the highest index used. There will be only one |
| 1058 | * active uniform reported by the GL per uniform array. |
| 1059 | |
| 1060 | */ |
| 1061 | static void |
Eric Anholt | 586b4b5 | 2010-09-28 14:32:16 -0700 | [diff] [blame] | 1062 | update_array_sizes(struct gl_shader_program *prog) |
Eric Anholt | a721abf | 2010-08-23 10:32:01 -0700 | [diff] [blame] | 1063 | { |
Ian Romanick | 3322fba | 2010-10-14 13:28:42 -0700 | [diff] [blame] | 1064 | for (unsigned i = 0; i < MESA_SHADER_TYPES; i++) { |
| 1065 | if (prog->_LinkedShaders[i] == NULL) |
| 1066 | continue; |
| 1067 | |
Eric Anholt | a721abf | 2010-08-23 10:32:01 -0700 | [diff] [blame] | 1068 | foreach_list(node, prog->_LinkedShaders[i]->ir) { |
| 1069 | ir_variable *const var = ((ir_instruction *) node)->as_variable(); |
| 1070 | |
Eric Anholt | 586b4b5 | 2010-09-28 14:32:16 -0700 | [diff] [blame] | 1071 | if ((var == NULL) || (var->mode != ir_var_uniform && |
| 1072 | var->mode != ir_var_in && |
| 1073 | var->mode != ir_var_out) || |
Eric Anholt | a721abf | 2010-08-23 10:32:01 -0700 | [diff] [blame] | 1074 | !var->type->is_array()) |
| 1075 | continue; |
| 1076 | |
Eric Anholt | 9feb403 | 2012-05-01 14:43:31 -0700 | [diff] [blame] | 1077 | /* GL_ARB_uniform_buffer_object says that std140 uniforms |
| 1078 | * will not be eliminated. Since we always do std140, just |
| 1079 | * don't resize arrays in UBOs. |
| 1080 | */ |
| 1081 | if (var->uniform_block != -1) |
| 1082 | continue; |
| 1083 | |
Eric Anholt | a721abf | 2010-08-23 10:32:01 -0700 | [diff] [blame] | 1084 | unsigned int size = var->max_array_access; |
Ian Romanick | 3322fba | 2010-10-14 13:28:42 -0700 | [diff] [blame] | 1085 | for (unsigned j = 0; j < MESA_SHADER_TYPES; j++) { |
| 1086 | if (prog->_LinkedShaders[j] == NULL) |
| 1087 | continue; |
| 1088 | |
Eric Anholt | a721abf | 2010-08-23 10:32:01 -0700 | [diff] [blame] | 1089 | foreach_list(node2, prog->_LinkedShaders[j]->ir) { |
| 1090 | ir_variable *other_var = ((ir_instruction *) node2)->as_variable(); |
| 1091 | if (!other_var) |
| 1092 | continue; |
| 1093 | |
| 1094 | if (strcmp(var->name, other_var->name) == 0 && |
| 1095 | other_var->max_array_access > size) { |
| 1096 | size = other_var->max_array_access; |
| 1097 | } |
| 1098 | } |
| 1099 | } |
Eric Anholt | 586b4b5 | 2010-09-28 14:32:16 -0700 | [diff] [blame] | 1100 | |
Eric Anholt | a721abf | 2010-08-23 10:32:01 -0700 | [diff] [blame] | 1101 | if (size + 1 != var->type->fields.array->length) { |
Ian Romanick | 89d81ab | 2011-01-25 10:41:20 -0800 | [diff] [blame] | 1102 | /* If this is a built-in uniform (i.e., it's backed by some |
| 1103 | * fixed-function state), adjust the number of state slots to |
| 1104 | * match the new array size. The number of slots per array entry |
Bryan Cain | f18a086 | 2011-04-23 19:29:15 -0500 | [diff] [blame] | 1105 | * 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] | 1106 | * slots is an integer multiple of the number of array elements. |
| 1107 | * Determine the number of slots per array element by dividing by |
| 1108 | * the old (total) size. |
| 1109 | */ |
| 1110 | if (var->num_state_slots > 0) { |
| 1111 | var->num_state_slots = (size + 1) |
| 1112 | * (var->num_state_slots / var->type->length); |
| 1113 | } |
| 1114 | |
Eric Anholt | a721abf | 2010-08-23 10:32:01 -0700 | [diff] [blame] | 1115 | var->type = glsl_type::get_array_instance(var->type->fields.array, |
| 1116 | size + 1); |
| 1117 | /* FINISHME: We should update the types of array |
| 1118 | * dereferences of this variable now. |
| 1119 | */ |
| 1120 | } |
| 1121 | } |
| 1122 | } |
| 1123 | } |
| 1124 | |
Ian Romanick | 6984670 | 2010-06-22 17:29:19 -0700 | [diff] [blame] | 1125 | /** |
Bryan Cain | f18a086 | 2011-04-23 19:29:15 -0500 | [diff] [blame] | 1126 | * Find a contiguous set of available bits in a bitmask. |
Ian Romanick | 6984670 | 2010-06-22 17:29:19 -0700 | [diff] [blame] | 1127 | * |
| 1128 | * \param used_mask Bits representing used (1) and unused (0) locations |
| 1129 | * \param needed_count Number of contiguous bits needed. |
| 1130 | * |
| 1131 | * \return |
| 1132 | * Base location of the available bits on success or -1 on failure. |
| 1133 | */ |
| 1134 | int |
| 1135 | find_available_slots(unsigned used_mask, unsigned needed_count) |
| 1136 | { |
| 1137 | unsigned needed_mask = (1 << needed_count) - 1; |
| 1138 | const int max_bit_to_test = (8 * sizeof(used_mask)) - needed_count; |
| 1139 | |
| 1140 | /* The comparison to 32 is redundant, but without it GCC emits "warning: |
| 1141 | * cannot optimize possibly infinite loops" for the loop below. |
| 1142 | */ |
| 1143 | if ((needed_count == 0) || (max_bit_to_test < 0) || (max_bit_to_test > 32)) |
| 1144 | return -1; |
| 1145 | |
| 1146 | for (int i = 0; i <= max_bit_to_test; i++) { |
| 1147 | if ((needed_mask & ~used_mask) == needed_mask) |
| 1148 | return i; |
| 1149 | |
| 1150 | needed_mask <<= 1; |
| 1151 | } |
| 1152 | |
| 1153 | return -1; |
| 1154 | } |
| 1155 | |
| 1156 | |
Ian Romanick | d32d4f7 | 2011-06-27 17:59:58 -0700 | [diff] [blame] | 1157 | /** |
| 1158 | * Assign locations for either VS inputs for FS outputs |
| 1159 | * |
| 1160 | * \param prog Shader program whose variables need locations assigned |
| 1161 | * \param target_index Selector for the program target to receive location |
| 1162 | * assignmnets. Must be either \c MESA_SHADER_VERTEX or |
| 1163 | * \c MESA_SHADER_FRAGMENT. |
| 1164 | * \param max_index Maximum number of generic locations. This corresponds |
| 1165 | * to either the maximum number of draw buffers or the |
| 1166 | * maximum number of generic attributes. |
| 1167 | * |
| 1168 | * \return |
| 1169 | * If locations are successfully assigned, true is returned. Otherwise an |
| 1170 | * error is emitted to the shader link log and false is returned. |
Ian Romanick | d32d4f7 | 2011-06-27 17:59:58 -0700 | [diff] [blame] | 1171 | */ |
Ian Romanick | 6984670 | 2010-06-22 17:29:19 -0700 | [diff] [blame] | 1172 | bool |
Ian Romanick | d32d4f7 | 2011-06-27 17:59:58 -0700 | [diff] [blame] | 1173 | assign_attribute_or_color_locations(gl_shader_program *prog, |
| 1174 | unsigned target_index, |
| 1175 | unsigned max_index) |
Ian Romanick | 0ad22cd | 2010-06-21 17:18:31 -0700 | [diff] [blame] | 1176 | { |
Ian Romanick | d32d4f7 | 2011-06-27 17:59:58 -0700 | [diff] [blame] | 1177 | /* Mark invalid locations as being used. |
Ian Romanick | 9342d26 | 2010-06-22 17:41:37 -0700 | [diff] [blame] | 1178 | */ |
Ian Romanick | d32d4f7 | 2011-06-27 17:59:58 -0700 | [diff] [blame] | 1179 | unsigned used_locations = (max_index >= 32) |
| 1180 | ? ~0 : ~((1 << max_index) - 1); |
Ian Romanick | 0ad22cd | 2010-06-21 17:18:31 -0700 | [diff] [blame] | 1181 | |
Ian Romanick | d32d4f7 | 2011-06-27 17:59:58 -0700 | [diff] [blame] | 1182 | assert((target_index == MESA_SHADER_VERTEX) |
| 1183 | || (target_index == MESA_SHADER_FRAGMENT)); |
| 1184 | |
| 1185 | gl_shader *const sh = prog->_LinkedShaders[target_index]; |
| 1186 | if (sh == NULL) |
| 1187 | return true; |
Ian Romanick | 0ad22cd | 2010-06-21 17:18:31 -0700 | [diff] [blame] | 1188 | |
Ian Romanick | 6984670 | 2010-06-22 17:29:19 -0700 | [diff] [blame] | 1189 | /* Operate in a total of four passes. |
Ian Romanick | 0ad22cd | 2010-06-21 17:18:31 -0700 | [diff] [blame] | 1190 | * |
| 1191 | * 1. Invalidate the location assignments for all vertex shader inputs. |
| 1192 | * |
| 1193 | * 2. Assign locations for inputs that have user-defined (via |
Ian Romanick | b12b5d9 | 2011-11-04 16:08:52 -0700 | [diff] [blame] | 1194 | * glBindVertexAttribLocation) locations and outputs that have |
| 1195 | * user-defined locations (via glBindFragDataLocation). |
Ian Romanick | 0ad22cd | 2010-06-21 17:18:31 -0700 | [diff] [blame] | 1196 | * |
Ian Romanick | 6984670 | 2010-06-22 17:29:19 -0700 | [diff] [blame] | 1197 | * 3. Sort the attributes without assigned locations by number of slots |
| 1198 | * required in decreasing order. Fragmentation caused by attribute |
| 1199 | * locations assigned by the application may prevent large attributes |
| 1200 | * from having enough contiguous space. |
| 1201 | * |
| 1202 | * 4. Assign locations to any inputs without assigned locations. |
Ian Romanick | 0ad22cd | 2010-06-21 17:18:31 -0700 | [diff] [blame] | 1203 | */ |
| 1204 | |
Ian Romanick | d32d4f7 | 2011-06-27 17:59:58 -0700 | [diff] [blame] | 1205 | const int generic_base = (target_index == MESA_SHADER_VERTEX) |
Brian Paul | 7eb7d67 | 2011-07-07 16:47:59 -0600 | [diff] [blame] | 1206 | ? (int) VERT_ATTRIB_GENERIC0 : (int) FRAG_RESULT_DATA0; |
Ian Romanick | 0ad22cd | 2010-06-21 17:18:31 -0700 | [diff] [blame] | 1207 | |
Ian Romanick | d32d4f7 | 2011-06-27 17:59:58 -0700 | [diff] [blame] | 1208 | const enum ir_variable_mode direction = |
| 1209 | (target_index == MESA_SHADER_VERTEX) ? ir_var_in : ir_var_out; |
| 1210 | |
| 1211 | |
Ian Romanick | 6984670 | 2010-06-22 17:29:19 -0700 | [diff] [blame] | 1212 | /* Temporary storage for the set of attributes that need locations assigned. |
| 1213 | */ |
| 1214 | struct temp_attr { |
| 1215 | unsigned slots; |
| 1216 | ir_variable *var; |
| 1217 | |
| 1218 | /* Used below in the call to qsort. */ |
| 1219 | static int compare(const void *a, const void *b) |
| 1220 | { |
| 1221 | const temp_attr *const l = (const temp_attr *) a; |
| 1222 | const temp_attr *const r = (const temp_attr *) b; |
| 1223 | |
| 1224 | /* Reversed because we want a descending order sort below. */ |
| 1225 | return r->slots - l->slots; |
| 1226 | } |
| 1227 | } to_assign[16]; |
| 1228 | |
| 1229 | unsigned num_attr = 0; |
| 1230 | |
Eric Anholt | 16b68b1 | 2010-06-30 11:05:43 -0700 | [diff] [blame] | 1231 | foreach_list(node, sh->ir) { |
Ian Romanick | 0ad22cd | 2010-06-21 17:18:31 -0700 | [diff] [blame] | 1232 | ir_variable *const var = ((ir_instruction *) node)->as_variable(); |
| 1233 | |
Brian Paul | 4470ff2 | 2011-07-19 21:10:25 -0600 | [diff] [blame] | 1234 | if ((var == NULL) || (var->mode != (unsigned) direction)) |
Ian Romanick | 0ad22cd | 2010-06-21 17:18:31 -0700 | [diff] [blame] | 1235 | continue; |
| 1236 | |
Ian Romanick | 68a4fc9 | 2010-10-07 17:21:22 -0700 | [diff] [blame] | 1237 | if (var->explicit_location) { |
Ian Romanick | d32d4f7 | 2011-06-27 17:59:58 -0700 | [diff] [blame] | 1238 | if ((var->location >= (int)(max_index + generic_base)) |
Ian Romanick | 68a4fc9 | 2010-10-07 17:21:22 -0700 | [diff] [blame] | 1239 | || (var->location < 0)) { |
Ian Romanick | 586e741 | 2011-07-28 14:04:09 -0700 | [diff] [blame] | 1240 | linker_error(prog, |
| 1241 | "invalid explicit location %d specified for `%s'\n", |
Ian Romanick | 523b611 | 2011-08-17 15:40:03 -0700 | [diff] [blame] | 1242 | (var->location < 0) |
| 1243 | ? var->location : var->location - generic_base, |
Ian Romanick | 586e741 | 2011-07-28 14:04:09 -0700 | [diff] [blame] | 1244 | var->name); |
Ian Romanick | 68a4fc9 | 2010-10-07 17:21:22 -0700 | [diff] [blame] | 1245 | return false; |
Ian Romanick | 523b611 | 2011-08-17 15:40:03 -0700 | [diff] [blame] | 1246 | } |
| 1247 | } else if (target_index == MESA_SHADER_VERTEX) { |
| 1248 | unsigned binding; |
| 1249 | |
| 1250 | if (prog->AttributeBindings->get(binding, var->name)) { |
| 1251 | assert(binding >= VERT_ATTRIB_GENERIC0); |
| 1252 | var->location = binding; |
Paul Berry | 3c9c17d | 2012-12-04 15:17:01 -0800 | [diff] [blame] | 1253 | var->is_unmatched_generic_inout = 0; |
Ian Romanick | 68a4fc9 | 2010-10-07 17:21:22 -0700 | [diff] [blame] | 1254 | } |
Ian Romanick | b12b5d9 | 2011-11-04 16:08:52 -0700 | [diff] [blame] | 1255 | } else if (target_index == MESA_SHADER_FRAGMENT) { |
| 1256 | unsigned binding; |
Dave Airlie | 1256a5d | 2012-03-24 13:33:41 +0000 | [diff] [blame] | 1257 | unsigned index; |
Ian Romanick | b12b5d9 | 2011-11-04 16:08:52 -0700 | [diff] [blame] | 1258 | |
| 1259 | if (prog->FragDataBindings->get(binding, var->name)) { |
| 1260 | assert(binding >= FRAG_RESULT_DATA0); |
| 1261 | var->location = binding; |
Paul Berry | 3c9c17d | 2012-12-04 15:17:01 -0800 | [diff] [blame] | 1262 | var->is_unmatched_generic_inout = 0; |
Dave Airlie | 1256a5d | 2012-03-24 13:33:41 +0000 | [diff] [blame] | 1263 | |
| 1264 | if (prog->FragDataIndexBindings->get(index, var->name)) { |
| 1265 | var->index = index; |
| 1266 | } |
Ian Romanick | b12b5d9 | 2011-11-04 16:08:52 -0700 | [diff] [blame] | 1267 | } |
Ian Romanick | 68a4fc9 | 2010-10-07 17:21:22 -0700 | [diff] [blame] | 1268 | } |
| 1269 | |
Ian Romanick | 9f0e98d | 2011-10-06 10:25:34 -0700 | [diff] [blame] | 1270 | /* If the variable is not a built-in and has a location statically |
| 1271 | * assigned in the shader (presumably via a layout qualifier), make sure |
| 1272 | * that it doesn't collide with other assigned locations. Otherwise, |
| 1273 | * add it to the list of variables that need linker-assigned locations. |
Ian Romanick | 0ad22cd | 2010-06-21 17:18:31 -0700 | [diff] [blame] | 1274 | */ |
Ian Romanick | 523b611 | 2011-08-17 15:40:03 -0700 | [diff] [blame] | 1275 | const unsigned slots = count_attribute_slots(var->type); |
| 1276 | if (var->location != -1) { |
Dave Airlie | 1256a5d | 2012-03-24 13:33:41 +0000 | [diff] [blame] | 1277 | if (var->location >= generic_base && var->index < 1) { |
Ian Romanick | 523b611 | 2011-08-17 15:40:03 -0700 | [diff] [blame] | 1278 | /* From page 61 of the OpenGL 4.0 spec: |
| 1279 | * |
| 1280 | * "LinkProgram will fail if the attribute bindings assigned |
| 1281 | * by BindAttribLocation do not leave not enough space to |
| 1282 | * assign a location for an active matrix attribute or an |
| 1283 | * active attribute array, both of which require multiple |
| 1284 | * contiguous generic attributes." |
| 1285 | * |
| 1286 | * Previous versions of the spec contain similar language but omit |
| 1287 | * the bit about attribute arrays. |
| 1288 | * |
| 1289 | * Page 61 of the OpenGL 4.0 spec also says: |
| 1290 | * |
| 1291 | * "It is possible for an application to bind more than one |
| 1292 | * attribute name to the same location. This is referred to as |
| 1293 | * aliasing. This will only work if only one of the aliased |
| 1294 | * attributes is active in the executable program, or if no |
| 1295 | * path through the shader consumes more than one attribute of |
| 1296 | * a set of attributes aliased to the same location. A link |
| 1297 | * error can occur if the linker determines that every path |
| 1298 | * through the shader consumes multiple aliased attributes, |
| 1299 | * but implementations are not required to generate an error |
| 1300 | * in this case." |
| 1301 | * |
| 1302 | * These two paragraphs are either somewhat contradictory, or I |
| 1303 | * don't fully understand one or both of them. |
| 1304 | */ |
| 1305 | /* FINISHME: The code as currently written does not support |
| 1306 | * FINISHME: attribute location aliasing (see comment above). |
| 1307 | */ |
| 1308 | /* Mask representing the contiguous slots that will be used by |
| 1309 | * this attribute. |
| 1310 | */ |
| 1311 | const unsigned attr = var->location - generic_base; |
| 1312 | const unsigned use_mask = (1 << slots) - 1; |
Ian Romanick | 0ad22cd | 2010-06-21 17:18:31 -0700 | [diff] [blame] | 1313 | |
Ian Romanick | 523b611 | 2011-08-17 15:40:03 -0700 | [diff] [blame] | 1314 | /* Generate a link error if the set of bits requested for this |
| 1315 | * attribute overlaps any previously allocated bits. |
| 1316 | */ |
| 1317 | if ((~(use_mask << attr) & used_locations) != used_locations) { |
Dave Airlie | 7449ae4 | 2011-11-20 19:56:35 +0000 | [diff] [blame] | 1318 | const char *const string = (target_index == MESA_SHADER_VERTEX) |
| 1319 | ? "vertex shader input" : "fragment shader output"; |
Ian Romanick | 523b611 | 2011-08-17 15:40:03 -0700 | [diff] [blame] | 1320 | linker_error(prog, |
Dave Airlie | 7449ae4 | 2011-11-20 19:56:35 +0000 | [diff] [blame] | 1321 | "insufficient contiguous locations " |
Dave Airlie | 1256a5d | 2012-03-24 13:33:41 +0000 | [diff] [blame] | 1322 | "available for %s `%s' %d %d %d", string, |
| 1323 | var->name, used_locations, use_mask, attr); |
Ian Romanick | 523b611 | 2011-08-17 15:40:03 -0700 | [diff] [blame] | 1324 | return false; |
| 1325 | } |
| 1326 | |
| 1327 | used_locations |= (use_mask << attr); |
| 1328 | } |
| 1329 | |
| 1330 | continue; |
| 1331 | } |
| 1332 | |
| 1333 | to_assign[num_attr].slots = slots; |
Ian Romanick | 6984670 | 2010-06-22 17:29:19 -0700 | [diff] [blame] | 1334 | to_assign[num_attr].var = var; |
| 1335 | num_attr++; |
Ian Romanick | 0ad22cd | 2010-06-21 17:18:31 -0700 | [diff] [blame] | 1336 | } |
Ian Romanick | 6984670 | 2010-06-22 17:29:19 -0700 | [diff] [blame] | 1337 | |
| 1338 | /* If all of the attributes were assigned locations by the application (or |
| 1339 | * are built-in attributes with fixed locations), return early. This should |
| 1340 | * be the common case. |
| 1341 | */ |
| 1342 | if (num_attr == 0) |
| 1343 | return true; |
| 1344 | |
| 1345 | qsort(to_assign, num_attr, sizeof(to_assign[0]), temp_attr::compare); |
| 1346 | |
Ian Romanick | d32d4f7 | 2011-06-27 17:59:58 -0700 | [diff] [blame] | 1347 | if (target_index == MESA_SHADER_VERTEX) { |
| 1348 | /* VERT_ATTRIB_GENERIC0 is a pseudo-alias for VERT_ATTRIB_POS. It can |
| 1349 | * only be explicitly assigned by via glBindAttribLocation. Mark it as |
| 1350 | * reserved to prevent it from being automatically allocated below. |
| 1351 | */ |
| 1352 | find_deref_visitor find("gl_Vertex"); |
| 1353 | find.run(sh->ir); |
| 1354 | if (find.variable_found()) |
| 1355 | used_locations |= (1 << 0); |
| 1356 | } |
Ian Romanick | 982e379 | 2010-06-29 18:58:20 -0700 | [diff] [blame] | 1357 | |
Ian Romanick | 6984670 | 2010-06-22 17:29:19 -0700 | [diff] [blame] | 1358 | for (unsigned i = 0; i < num_attr; i++) { |
| 1359 | /* Mask representing the contiguous slots that will be used by this |
| 1360 | * attribute. |
| 1361 | */ |
| 1362 | const unsigned use_mask = (1 << to_assign[i].slots) - 1; |
| 1363 | |
| 1364 | int location = find_available_slots(used_locations, to_assign[i].slots); |
| 1365 | |
| 1366 | if (location < 0) { |
Ian Romanick | d32d4f7 | 2011-06-27 17:59:58 -0700 | [diff] [blame] | 1367 | const char *const string = (target_index == MESA_SHADER_VERTEX) |
| 1368 | ? "vertex shader input" : "fragment shader output"; |
| 1369 | |
Ian Romanick | 586e741 | 2011-07-28 14:04:09 -0700 | [diff] [blame] | 1370 | linker_error(prog, |
Dave Airlie | 7449ae4 | 2011-11-20 19:56:35 +0000 | [diff] [blame] | 1371 | "insufficient contiguous locations " |
Ian Romanick | 586e741 | 2011-07-28 14:04:09 -0700 | [diff] [blame] | 1372 | "available for %s `%s'", |
| 1373 | string, to_assign[i].var->name); |
Ian Romanick | 6984670 | 2010-06-22 17:29:19 -0700 | [diff] [blame] | 1374 | return false; |
| 1375 | } |
| 1376 | |
Ian Romanick | d32d4f7 | 2011-06-27 17:59:58 -0700 | [diff] [blame] | 1377 | to_assign[i].var->location = generic_base + location; |
Paul Berry | 3c9c17d | 2012-12-04 15:17:01 -0800 | [diff] [blame] | 1378 | to_assign[i].var->is_unmatched_generic_inout = 0; |
Ian Romanick | 6984670 | 2010-06-22 17:29:19 -0700 | [diff] [blame] | 1379 | used_locations |= (use_mask << location); |
| 1380 | } |
| 1381 | |
| 1382 | return true; |
Ian Romanick | 0ad22cd | 2010-06-21 17:18:31 -0700 | [diff] [blame] | 1383 | } |
| 1384 | |
| 1385 | |
Ian Romanick | 40e114b | 2010-08-17 14:55:50 -0700 | [diff] [blame] | 1386 | /** |
Ian Romanick | cc90e62 | 2010-10-19 17:59:10 -0700 | [diff] [blame] | 1387 | * Demote shader inputs and outputs that are not used in other stages |
Ian Romanick | 40e114b | 2010-08-17 14:55:50 -0700 | [diff] [blame] | 1388 | */ |
| 1389 | void |
Ian Romanick | cc90e62 | 2010-10-19 17:59:10 -0700 | [diff] [blame] | 1390 | 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] | 1391 | { |
| 1392 | foreach_list(node, sh->ir) { |
| 1393 | ir_variable *const var = ((ir_instruction *) node)->as_variable(); |
| 1394 | |
Ian Romanick | cc90e62 | 2010-10-19 17:59:10 -0700 | [diff] [blame] | 1395 | if ((var == NULL) || (var->mode != int(mode))) |
Ian Romanick | 40e114b | 2010-08-17 14:55:50 -0700 | [diff] [blame] | 1396 | continue; |
| 1397 | |
Ian Romanick | cc90e62 | 2010-10-19 17:59:10 -0700 | [diff] [blame] | 1398 | /* A shader 'in' or 'out' variable is only really an input or output if |
| 1399 | * its value is used by other shader stages. This will cause the variable |
| 1400 | * to have a location assigned. |
Ian Romanick | 40e114b | 2010-08-17 14:55:50 -0700 | [diff] [blame] | 1401 | */ |
Paul Berry | 3c9c17d | 2012-12-04 15:17:01 -0800 | [diff] [blame] | 1402 | if (var->is_unmatched_generic_inout) { |
Ian Romanick | 40e114b | 2010-08-17 14:55:50 -0700 | [diff] [blame] | 1403 | var->mode = ir_var_auto; |
| 1404 | } |
| 1405 | } |
| 1406 | } |
| 1407 | |
| 1408 | |
Paul Berry | 871ddb9 | 2011-11-05 11:17:32 -0700 | [diff] [blame] | 1409 | /** |
Marek Olšák | ec174a4 | 2011-11-18 15:00:10 +0100 | [diff] [blame] | 1410 | * Store the gl_FragDepth layout in the gl_shader_program struct. |
| 1411 | */ |
| 1412 | static void |
| 1413 | store_fragdepth_layout(struct gl_shader_program *prog) |
| 1414 | { |
| 1415 | if (prog->_LinkedShaders[MESA_SHADER_FRAGMENT] == NULL) { |
| 1416 | return; |
| 1417 | } |
| 1418 | |
| 1419 | struct exec_list *ir = prog->_LinkedShaders[MESA_SHADER_FRAGMENT]->ir; |
| 1420 | |
| 1421 | /* We don't look up the gl_FragDepth symbol directly because if |
| 1422 | * gl_FragDepth is not used in the shader, it's removed from the IR. |
| 1423 | * However, the symbol won't be removed from the symbol table. |
| 1424 | * |
| 1425 | * We're only interested in the cases where the variable is NOT removed |
| 1426 | * from the IR. |
| 1427 | */ |
| 1428 | foreach_list(node, ir) { |
| 1429 | ir_variable *const var = ((ir_instruction *) node)->as_variable(); |
| 1430 | |
| 1431 | if (var == NULL || var->mode != ir_var_out) { |
| 1432 | continue; |
| 1433 | } |
| 1434 | |
| 1435 | if (strcmp(var->name, "gl_FragDepth") == 0) { |
| 1436 | switch (var->depth_layout) { |
| 1437 | case ir_depth_layout_none: |
| 1438 | prog->FragDepthLayout = FRAG_DEPTH_LAYOUT_NONE; |
| 1439 | return; |
| 1440 | case ir_depth_layout_any: |
| 1441 | prog->FragDepthLayout = FRAG_DEPTH_LAYOUT_ANY; |
| 1442 | return; |
| 1443 | case ir_depth_layout_greater: |
| 1444 | prog->FragDepthLayout = FRAG_DEPTH_LAYOUT_GREATER; |
| 1445 | return; |
| 1446 | case ir_depth_layout_less: |
| 1447 | prog->FragDepthLayout = FRAG_DEPTH_LAYOUT_LESS; |
| 1448 | return; |
| 1449 | case ir_depth_layout_unchanged: |
| 1450 | prog->FragDepthLayout = FRAG_DEPTH_LAYOUT_UNCHANGED; |
| 1451 | return; |
| 1452 | default: |
| 1453 | assert(0); |
| 1454 | return; |
| 1455 | } |
| 1456 | } |
| 1457 | } |
| 1458 | } |
| 1459 | |
| 1460 | /** |
Ian Romanick | 92f8159 | 2011-11-08 12:37:19 -0800 | [diff] [blame] | 1461 | * Validate the resources used by a program versus the implementation limits |
| 1462 | */ |
| 1463 | static bool |
| 1464 | check_resources(struct gl_context *ctx, struct gl_shader_program *prog) |
| 1465 | { |
| 1466 | static const char *const shader_names[MESA_SHADER_TYPES] = { |
| 1467 | "vertex", "fragment", "geometry" |
| 1468 | }; |
| 1469 | |
| 1470 | const unsigned max_samplers[MESA_SHADER_TYPES] = { |
| 1471 | ctx->Const.MaxVertexTextureImageUnits, |
| 1472 | ctx->Const.MaxTextureImageUnits, |
| 1473 | ctx->Const.MaxGeometryTextureImageUnits |
| 1474 | }; |
| 1475 | |
| 1476 | const unsigned max_uniform_components[MESA_SHADER_TYPES] = { |
| 1477 | ctx->Const.VertexProgram.MaxUniformComponents, |
| 1478 | ctx->Const.FragmentProgram.MaxUniformComponents, |
| 1479 | 0 /* FINISHME: Geometry shaders. */ |
| 1480 | }; |
| 1481 | |
Eric Anholt | 877a897 | 2012-06-25 12:47:01 -0700 | [diff] [blame] | 1482 | const unsigned max_uniform_blocks[MESA_SHADER_TYPES] = { |
| 1483 | ctx->Const.VertexProgram.MaxUniformBlocks, |
| 1484 | ctx->Const.FragmentProgram.MaxUniformBlocks, |
| 1485 | ctx->Const.GeometryProgram.MaxUniformBlocks, |
| 1486 | }; |
| 1487 | |
Ian Romanick | 92f8159 | 2011-11-08 12:37:19 -0800 | [diff] [blame] | 1488 | for (unsigned i = 0; i < MESA_SHADER_TYPES; i++) { |
| 1489 | struct gl_shader *sh = prog->_LinkedShaders[i]; |
| 1490 | |
| 1491 | if (sh == NULL) |
| 1492 | continue; |
| 1493 | |
| 1494 | if (sh->num_samplers > max_samplers[i]) { |
| 1495 | linker_error(prog, "Too many %s shader texture samplers", |
| 1496 | shader_names[i]); |
| 1497 | } |
| 1498 | |
| 1499 | if (sh->num_uniform_components > max_uniform_components[i]) { |
Marek Olšák | df809ae | 2011-12-10 04:14:46 +0100 | [diff] [blame] | 1500 | if (ctx->Const.GLSLSkipStrictMaxUniformLimitCheck) { |
| 1501 | linker_warning(prog, "Too many %s shader uniform components, " |
| 1502 | "but the driver will try to optimize them out; " |
| 1503 | "this is non-portable out-of-spec behavior\n", |
| 1504 | shader_names[i]); |
| 1505 | } else { |
| 1506 | linker_error(prog, "Too many %s shader uniform components", |
| 1507 | shader_names[i]); |
| 1508 | } |
Ian Romanick | 92f8159 | 2011-11-08 12:37:19 -0800 | [diff] [blame] | 1509 | } |
| 1510 | } |
| 1511 | |
Eric Anholt | 877a897 | 2012-06-25 12:47:01 -0700 | [diff] [blame] | 1512 | unsigned blocks[MESA_SHADER_TYPES] = {0}; |
| 1513 | unsigned total_uniform_blocks = 0; |
| 1514 | |
| 1515 | for (unsigned i = 0; i < prog->NumUniformBlocks; i++) { |
| 1516 | for (unsigned j = 0; j < MESA_SHADER_TYPES; j++) { |
| 1517 | if (prog->UniformBlockStageIndex[j][i] != -1) { |
| 1518 | blocks[j]++; |
| 1519 | total_uniform_blocks++; |
| 1520 | } |
| 1521 | } |
| 1522 | |
| 1523 | if (total_uniform_blocks > ctx->Const.MaxCombinedUniformBlocks) { |
| 1524 | linker_error(prog, "Too many combined uniform blocks (%d/%d)", |
| 1525 | prog->NumUniformBlocks, |
| 1526 | ctx->Const.MaxCombinedUniformBlocks); |
| 1527 | } else { |
| 1528 | for (unsigned i = 0; i < MESA_SHADER_TYPES; i++) { |
| 1529 | if (blocks[i] > max_uniform_blocks[i]) { |
| 1530 | linker_error(prog, "Too many %s uniform blocks (%d/%d)", |
| 1531 | shader_names[i], |
| 1532 | blocks[i], |
| 1533 | max_uniform_blocks[i]); |
| 1534 | break; |
| 1535 | } |
| 1536 | } |
| 1537 | } |
| 1538 | } |
| 1539 | |
Ian Romanick | 92f8159 | 2011-11-08 12:37:19 -0800 | [diff] [blame] | 1540 | return prog->LinkStatus; |
| 1541 | } |
Paul Berry | 871ddb9 | 2011-11-05 11:17:32 -0700 | [diff] [blame] | 1542 | |
Ian Romanick | 0e59b26 | 2010-06-23 11:23:01 -0700 | [diff] [blame] | 1543 | void |
Kristian Høgsberg | f9995b3 | 2010-10-12 12:26:10 -0400 | [diff] [blame] | 1544 | link_shaders(struct gl_context *ctx, struct gl_shader_program *prog) |
Ian Romanick | 832dfa5 | 2010-06-17 15:04:20 -0700 | [diff] [blame] | 1545 | { |
Paul Berry | 871ddb9 | 2011-11-05 11:17:32 -0700 | [diff] [blame] | 1546 | tfeedback_decl *tfeedback_decls = NULL; |
| 1547 | unsigned num_tfeedback_decls = prog->TransformFeedback.NumVarying; |
| 1548 | |
Kenneth Graunke | d3073f5 | 2011-01-21 14:32:31 -0800 | [diff] [blame] | 1549 | void *mem_ctx = ralloc_context(NULL); // temporary linker context |
Kenneth Graunke | 2da02e7 | 2010-11-17 11:03:57 -0800 | [diff] [blame] | 1550 | |
Ian Romanick | 832dfa5 | 2010-06-17 15:04:20 -0700 | [diff] [blame] | 1551 | prog->LinkStatus = false; |
| 1552 | prog->Validated = false; |
| 1553 | prog->_Used = false; |
| 1554 | |
Eric Anholt | f609cf7 | 2012-04-27 13:52:56 -0700 | [diff] [blame] | 1555 | ralloc_free(prog->InfoLog); |
Kenneth Graunke | d3073f5 | 2011-01-21 14:32:31 -0800 | [diff] [blame] | 1556 | prog->InfoLog = ralloc_strdup(NULL, ""); |
Ian Romanick | f36460e | 2010-06-23 12:07:22 -0700 | [diff] [blame] | 1557 | |
Eric Anholt | f609cf7 | 2012-04-27 13:52:56 -0700 | [diff] [blame] | 1558 | ralloc_free(prog->UniformBlocks); |
| 1559 | prog->UniformBlocks = NULL; |
| 1560 | prog->NumUniformBlocks = 0; |
| 1561 | for (int i = 0; i < MESA_SHADER_TYPES; i++) { |
| 1562 | ralloc_free(prog->UniformBlockStageIndex[i]); |
| 1563 | prog->UniformBlockStageIndex[i] = NULL; |
| 1564 | } |
| 1565 | |
Ian Romanick | 832dfa5 | 2010-06-17 15:04:20 -0700 | [diff] [blame] | 1566 | /* Separate the shaders into groups based on their type. |
| 1567 | */ |
Eric Anholt | 16b68b1 | 2010-06-30 11:05:43 -0700 | [diff] [blame] | 1568 | struct gl_shader **vert_shader_list; |
Ian Romanick | 832dfa5 | 2010-06-17 15:04:20 -0700 | [diff] [blame] | 1569 | unsigned num_vert_shaders = 0; |
Eric Anholt | 16b68b1 | 2010-06-30 11:05:43 -0700 | [diff] [blame] | 1570 | struct gl_shader **frag_shader_list; |
Ian Romanick | 832dfa5 | 2010-06-17 15:04:20 -0700 | [diff] [blame] | 1571 | unsigned num_frag_shaders = 0; |
| 1572 | |
Eric Anholt | 16b68b1 | 2010-06-30 11:05:43 -0700 | [diff] [blame] | 1573 | vert_shader_list = (struct gl_shader **) |
| 1574 | calloc(2 * prog->NumShaders, sizeof(struct gl_shader *)); |
Ian Romanick | 832dfa5 | 2010-06-17 15:04:20 -0700 | [diff] [blame] | 1575 | frag_shader_list = &vert_shader_list[prog->NumShaders]; |
| 1576 | |
Ian Romanick | 25f51d3 | 2010-07-16 15:51:50 -0700 | [diff] [blame] | 1577 | unsigned min_version = UINT_MAX; |
| 1578 | unsigned max_version = 0; |
Paul Berry | a9f34dc | 2012-08-02 17:49:44 -0700 | [diff] [blame] | 1579 | const bool is_es_prog = |
| 1580 | (prog->NumShaders > 0 && prog->Shaders[0]->IsES) ? true : false; |
Ian Romanick | 832dfa5 | 2010-06-17 15:04:20 -0700 | [diff] [blame] | 1581 | for (unsigned i = 0; i < prog->NumShaders; i++) { |
Ian Romanick | 25f51d3 | 2010-07-16 15:51:50 -0700 | [diff] [blame] | 1582 | min_version = MIN2(min_version, prog->Shaders[i]->Version); |
| 1583 | max_version = MAX2(max_version, prog->Shaders[i]->Version); |
| 1584 | |
Paul Berry | a9f34dc | 2012-08-02 17:49:44 -0700 | [diff] [blame] | 1585 | if (prog->Shaders[i]->IsES != is_es_prog) { |
| 1586 | linker_error(prog, "all shaders must use same shading " |
| 1587 | "language version\n"); |
| 1588 | goto done; |
| 1589 | } |
| 1590 | |
Ian Romanick | 832dfa5 | 2010-06-17 15:04:20 -0700 | [diff] [blame] | 1591 | switch (prog->Shaders[i]->Type) { |
| 1592 | case GL_VERTEX_SHADER: |
| 1593 | vert_shader_list[num_vert_shaders] = prog->Shaders[i]; |
| 1594 | num_vert_shaders++; |
| 1595 | break; |
| 1596 | case GL_FRAGMENT_SHADER: |
| 1597 | frag_shader_list[num_frag_shaders] = prog->Shaders[i]; |
| 1598 | num_frag_shaders++; |
| 1599 | break; |
| 1600 | case GL_GEOMETRY_SHADER: |
| 1601 | /* FINISHME: Support geometry shaders. */ |
| 1602 | assert(prog->Shaders[i]->Type != GL_GEOMETRY_SHADER); |
| 1603 | break; |
| 1604 | } |
| 1605 | } |
| 1606 | |
Ian Romanick | 25f51d3 | 2010-07-16 15:51:50 -0700 | [diff] [blame] | 1607 | /* Previous to GLSL version 1.30, different compilation units could mix and |
| 1608 | * match shading language versions. With GLSL 1.30 and later, the versions |
| 1609 | * of all shaders must match. |
Paul Berry | a9f34dc | 2012-08-02 17:49:44 -0700 | [diff] [blame] | 1610 | * |
| 1611 | * GLSL ES has never allowed mixing of shading language versions. |
Ian Romanick | 25f51d3 | 2010-07-16 15:51:50 -0700 | [diff] [blame] | 1612 | */ |
Paul Berry | a9f34dc | 2012-08-02 17:49:44 -0700 | [diff] [blame] | 1613 | if ((is_es_prog || max_version >= 130) |
Kenneth Graunke | 5a81d05 | 2010-08-31 09:33:58 -0700 | [diff] [blame] | 1614 | && min_version != max_version) { |
Ian Romanick | 586e741 | 2011-07-28 14:04:09 -0700 | [diff] [blame] | 1615 | linker_error(prog, "all shaders must use same shading " |
| 1616 | "language version\n"); |
Ian Romanick | 25f51d3 | 2010-07-16 15:51:50 -0700 | [diff] [blame] | 1617 | goto done; |
| 1618 | } |
| 1619 | |
| 1620 | prog->Version = max_version; |
Paul Berry | 91c92bb | 2012-08-02 17:50:43 -0700 | [diff] [blame] | 1621 | prog->IsES = is_es_prog; |
Ian Romanick | 25f51d3 | 2010-07-16 15:51:50 -0700 | [diff] [blame] | 1622 | |
Ian Romanick | 3322fba | 2010-10-14 13:28:42 -0700 | [diff] [blame] | 1623 | for (unsigned int i = 0; i < MESA_SHADER_TYPES; i++) { |
| 1624 | if (prog->_LinkedShaders[i] != NULL) |
| 1625 | ctx->Driver.DeleteShader(ctx, prog->_LinkedShaders[i]); |
| 1626 | |
| 1627 | prog->_LinkedShaders[i] = NULL; |
Eric Anholt | 5d0f430 | 2010-08-18 12:02:35 -0700 | [diff] [blame] | 1628 | } |
| 1629 | |
Ian Romanick | cd6764e | 2010-07-16 16:00:07 -0700 | [diff] [blame] | 1630 | /* Link all shaders for a particular stage and validate the result. |
| 1631 | */ |
Ian Romanick | cc22c5a | 2010-06-18 17:13:42 -0700 | [diff] [blame] | 1632 | if (num_vert_shaders > 0) { |
Ian Romanick | 3fb8787 | 2010-07-09 14:09:34 -0700 | [diff] [blame] | 1633 | gl_shader *const sh = |
Kenneth Graunke | 2da02e7 | 2010-11-17 11:03:57 -0800 | [diff] [blame] | 1634 | link_intrastage_shaders(mem_ctx, ctx, prog, vert_shader_list, |
| 1635 | num_vert_shaders); |
Ian Romanick | 3fb8787 | 2010-07-09 14:09:34 -0700 | [diff] [blame] | 1636 | |
| 1637 | if (sh == NULL) |
| 1638 | goto done; |
| 1639 | |
| 1640 | if (!validate_vertex_shader_executable(prog, sh)) |
Ian Romanick | f29ff6e | 2010-10-14 17:55:17 -0700 | [diff] [blame] | 1641 | goto done; |
Ian Romanick | 3fb8787 | 2010-07-09 14:09:34 -0700 | [diff] [blame] | 1642 | |
Ian Romanick | 3322fba | 2010-10-14 13:28:42 -0700 | [diff] [blame] | 1643 | _mesa_reference_shader(ctx, &prog->_LinkedShaders[MESA_SHADER_VERTEX], |
| 1644 | sh); |
Ian Romanick | cc22c5a | 2010-06-18 17:13:42 -0700 | [diff] [blame] | 1645 | } |
| 1646 | |
| 1647 | if (num_frag_shaders > 0) { |
Ian Romanick | 3fb8787 | 2010-07-09 14:09:34 -0700 | [diff] [blame] | 1648 | gl_shader *const sh = |
Kenneth Graunke | 2da02e7 | 2010-11-17 11:03:57 -0800 | [diff] [blame] | 1649 | link_intrastage_shaders(mem_ctx, ctx, prog, frag_shader_list, |
| 1650 | num_frag_shaders); |
Ian Romanick | 3fb8787 | 2010-07-09 14:09:34 -0700 | [diff] [blame] | 1651 | |
| 1652 | if (sh == NULL) |
| 1653 | goto done; |
| 1654 | |
| 1655 | if (!validate_fragment_shader_executable(prog, sh)) |
Ian Romanick | f29ff6e | 2010-10-14 17:55:17 -0700 | [diff] [blame] | 1656 | goto done; |
Ian Romanick | 3fb8787 | 2010-07-09 14:09:34 -0700 | [diff] [blame] | 1657 | |
Ian Romanick | 3322fba | 2010-10-14 13:28:42 -0700 | [diff] [blame] | 1658 | _mesa_reference_shader(ctx, &prog->_LinkedShaders[MESA_SHADER_FRAGMENT], |
| 1659 | sh); |
Ian Romanick | cc22c5a | 2010-06-18 17:13:42 -0700 | [diff] [blame] | 1660 | } |
| 1661 | |
Ian Romanick | 3ed850e | 2010-06-23 12:18:21 -0700 | [diff] [blame] | 1662 | /* Here begins the inter-stage linking phase. Some initial validation is |
| 1663 | * performed, then locations are assigned for uniforms, attributes, and |
| 1664 | * varyings. |
| 1665 | */ |
Ian Romanick | ed1fe3d | 2010-06-23 12:09:14 -0700 | [diff] [blame] | 1666 | if (cross_validate_uniforms(prog)) { |
Ian Romanick | 3322fba | 2010-10-14 13:28:42 -0700 | [diff] [blame] | 1667 | unsigned prev; |
| 1668 | |
| 1669 | for (prev = 0; prev < MESA_SHADER_TYPES; prev++) { |
| 1670 | if (prog->_LinkedShaders[prev] != NULL) |
| 1671 | break; |
| 1672 | } |
| 1673 | |
Bryan Cain | f18a086 | 2011-04-23 19:29:15 -0500 | [diff] [blame] | 1674 | /* Validate the inputs of each stage with the output of the preceding |
Ian Romanick | 3710192 | 2010-06-18 19:02:10 -0700 | [diff] [blame] | 1675 | * stage. |
| 1676 | */ |
Ian Romanick | 3322fba | 2010-10-14 13:28:42 -0700 | [diff] [blame] | 1677 | for (unsigned i = prev + 1; i < MESA_SHADER_TYPES; i++) { |
| 1678 | if (prog->_LinkedShaders[i] == NULL) |
| 1679 | continue; |
| 1680 | |
Ian Romanick | f36460e | 2010-06-23 12:07:22 -0700 | [diff] [blame] | 1681 | if (!cross_validate_outputs_to_inputs(prog, |
Ian Romanick | 3322fba | 2010-10-14 13:28:42 -0700 | [diff] [blame] | 1682 | prog->_LinkedShaders[prev], |
Ian Romanick | abee16e | 2010-06-21 16:16:05 -0700 | [diff] [blame] | 1683 | prog->_LinkedShaders[i])) |
Ian Romanick | 3710192 | 2010-06-18 19:02:10 -0700 | [diff] [blame] | 1684 | goto done; |
Ian Romanick | 3322fba | 2010-10-14 13:28:42 -0700 | [diff] [blame] | 1685 | |
| 1686 | prev = i; |
Ian Romanick | 3710192 | 2010-06-18 19:02:10 -0700 | [diff] [blame] | 1687 | } |
| 1688 | |
Ian Romanick | cc22c5a | 2010-06-18 17:13:42 -0700 | [diff] [blame] | 1689 | prog->LinkStatus = true; |
Ian Romanick | 3710192 | 2010-06-18 19:02:10 -0700 | [diff] [blame] | 1690 | } |
Ian Romanick | 832dfa5 | 2010-06-17 15:04:20 -0700 | [diff] [blame] | 1691 | |
Eric Anholt | 3de1395 | 2012-05-04 13:08:46 -0700 | [diff] [blame] | 1692 | /* Implement the GLSL 1.30+ rule for discard vs infinite loops Do |
| 1693 | * it before optimization because we want most of the checks to get |
| 1694 | * dropped thanks to constant propagation. |
Paul Berry | 15ba2a5 | 2012-08-02 17:51:02 -0700 | [diff] [blame] | 1695 | * |
| 1696 | * This rule also applies to GLSL ES 3.00. |
Eric Anholt | 3de1395 | 2012-05-04 13:08:46 -0700 | [diff] [blame] | 1697 | */ |
Paul Berry | 15ba2a5 | 2012-08-02 17:51:02 -0700 | [diff] [blame] | 1698 | if (max_version >= (is_es_prog ? 300 : 130)) { |
Eric Anholt | 3de1395 | 2012-05-04 13:08:46 -0700 | [diff] [blame] | 1699 | struct gl_shader *sh = prog->_LinkedShaders[MESA_SHADER_FRAGMENT]; |
| 1700 | if (sh) { |
| 1701 | lower_discard_flow(sh->ir); |
| 1702 | } |
| 1703 | } |
| 1704 | |
Eric Anholt | f609cf7 | 2012-04-27 13:52:56 -0700 | [diff] [blame] | 1705 | if (!interstage_cross_validate_uniform_blocks(prog)) |
| 1706 | goto done; |
| 1707 | |
Eric Anholt | 2f4fe15 | 2010-08-10 13:06:49 -0700 | [diff] [blame] | 1708 | /* Do common optimization before assigning storage for attributes, |
| 1709 | * uniforms, and varyings. Later optimization could possibly make |
| 1710 | * some of that unused. |
| 1711 | */ |
Ian Romanick | 3322fba | 2010-10-14 13:28:42 -0700 | [diff] [blame] | 1712 | for (unsigned i = 0; i < MESA_SHADER_TYPES; i++) { |
| 1713 | if (prog->_LinkedShaders[i] == NULL) |
| 1714 | continue; |
| 1715 | |
Ian Romanick | 02c5ae1 | 2011-07-11 10:46:01 -0700 | [diff] [blame] | 1716 | detect_recursion_linked(prog, prog->_LinkedShaders[i]->ir); |
| 1717 | if (!prog->LinkStatus) |
| 1718 | goto done; |
| 1719 | |
Paul Berry | 1839244 | 2012-12-04 11:11:02 -0800 | [diff] [blame] | 1720 | if (ctx->ShaderCompilerOptions[i].LowerClipDistance) { |
| 1721 | lower_clip_distance(prog->_LinkedShaders[i]); |
| 1722 | } |
Paul Berry | c06e325 | 2011-08-11 20:58:21 -0700 | [diff] [blame] | 1723 | |
Brian Paul | 7feabfe | 2012-03-20 17:43:12 -0600 | [diff] [blame] | 1724 | unsigned max_unroll = ctx->ShaderCompilerOptions[i].MaxUnrollIterations; |
| 1725 | |
| 1726 | while (do_common_optimization(prog->_LinkedShaders[i]->ir, true, false, max_unroll)) |
Eric Anholt | 2f4fe15 | 2010-08-10 13:06:49 -0700 | [diff] [blame] | 1727 | ; |
Ian Romanick | a7ba9a7 | 2010-07-20 13:36:32 -0700 | [diff] [blame] | 1728 | } |
Ian Romanick | 13e10e4 | 2010-06-21 12:03:24 -0700 | [diff] [blame] | 1729 | |
Paul Berry | 50895d4 | 2012-12-05 07:17:07 -0800 | [diff] [blame] | 1730 | /* Mark all generic shader inputs and outputs as unpaired. */ |
| 1731 | if (prog->_LinkedShaders[MESA_SHADER_VERTEX] != NULL) { |
| 1732 | link_invalidate_variable_locations( |
| 1733 | prog->_LinkedShaders[MESA_SHADER_VERTEX], |
| 1734 | VERT_ATTRIB_GENERIC0, VERT_RESULT_VAR0); |
| 1735 | } |
| 1736 | /* FINISHME: Geometry shaders not implemented yet */ |
| 1737 | if (prog->_LinkedShaders[MESA_SHADER_FRAGMENT] != NULL) { |
| 1738 | link_invalidate_variable_locations( |
| 1739 | prog->_LinkedShaders[MESA_SHADER_FRAGMENT], |
| 1740 | FRAG_ATTRIB_VAR0, FRAG_RESULT_DATA0); |
| 1741 | } |
| 1742 | |
Ian Romanick | d32d4f7 | 2011-06-27 17:59:58 -0700 | [diff] [blame] | 1743 | /* FINISHME: The value of the max_attribute_index parameter is |
| 1744 | * FINISHME: implementation dependent based on the value of |
| 1745 | * FINISHME: GL_MAX_VERTEX_ATTRIBS. GL_MAX_VERTEX_ATTRIBS must be |
| 1746 | * FINISHME: at least 16, so hardcode 16 for now. |
| 1747 | */ |
| 1748 | if (!assign_attribute_or_color_locations(prog, MESA_SHADER_VERTEX, 16)) { |
Ian Romanick | d32d4f7 | 2011-06-27 17:59:58 -0700 | [diff] [blame] | 1749 | goto done; |
| 1750 | } |
| 1751 | |
Dave Airlie | 1256a5d | 2012-03-24 13:33:41 +0000 | [diff] [blame] | 1752 | 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] | 1753 | goto done; |
Ian Romanick | 40e114b | 2010-08-17 14:55:50 -0700 | [diff] [blame] | 1754 | } |
| 1755 | |
Ian Romanick | 3322fba | 2010-10-14 13:28:42 -0700 | [diff] [blame] | 1756 | unsigned prev; |
| 1757 | for (prev = 0; prev < MESA_SHADER_TYPES; prev++) { |
| 1758 | if (prog->_LinkedShaders[prev] != NULL) |
| 1759 | break; |
| 1760 | } |
| 1761 | |
Paul Berry | 871ddb9 | 2011-11-05 11:17:32 -0700 | [diff] [blame] | 1762 | if (num_tfeedback_decls != 0) { |
| 1763 | /* From GL_EXT_transform_feedback: |
| 1764 | * A program will fail to link if: |
| 1765 | * |
| 1766 | * * the <count> specified by TransformFeedbackVaryingsEXT is |
| 1767 | * non-zero, but the program object has no vertex or geometry |
| 1768 | * shader; |
| 1769 | */ |
| 1770 | if (prev >= MESA_SHADER_FRAGMENT) { |
| 1771 | linker_error(prog, "Transform feedback varyings specified, but " |
| 1772 | "no vertex or geometry shader is present."); |
| 1773 | goto done; |
| 1774 | } |
| 1775 | |
| 1776 | tfeedback_decls = ralloc_array(mem_ctx, tfeedback_decl, |
| 1777 | prog->TransformFeedback.NumVarying); |
Paul Berry | 456279b | 2011-12-26 19:39:25 -0800 | [diff] [blame] | 1778 | if (!parse_tfeedback_decls(ctx, prog, mem_ctx, num_tfeedback_decls, |
Paul Berry | 871ddb9 | 2011-11-05 11:17:32 -0700 | [diff] [blame] | 1779 | prog->TransformFeedback.VaryingNames, |
| 1780 | tfeedback_decls)) |
| 1781 | goto done; |
| 1782 | } |
| 1783 | |
Ian Romanick | 3322fba | 2010-10-14 13:28:42 -0700 | [diff] [blame] | 1784 | for (unsigned i = prev + 1; i < MESA_SHADER_TYPES; i++) { |
| 1785 | if (prog->_LinkedShaders[i] == NULL) |
| 1786 | continue; |
| 1787 | |
Paul Berry | 871ddb9 | 2011-11-05 11:17:32 -0700 | [diff] [blame] | 1788 | if (!assign_varying_locations( |
Dave Airlie | 7d7a549 | 2012-12-15 13:24:52 +1000 | [diff] [blame] | 1789 | ctx, mem_ctx, prog, prog->_LinkedShaders[prev], prog->_LinkedShaders[i], |
Paul Berry | 871ddb9 | 2011-11-05 11:17:32 -0700 | [diff] [blame] | 1790 | i == MESA_SHADER_FRAGMENT ? num_tfeedback_decls : 0, |
| 1791 | tfeedback_decls)) |
Ian Romanick | de77324 | 2011-06-09 13:31:32 -0700 | [diff] [blame] | 1792 | goto done; |
Ian Romanick | de77324 | 2011-06-09 13:31:32 -0700 | [diff] [blame] | 1793 | |
Ian Romanick | 3322fba | 2010-10-14 13:28:42 -0700 | [diff] [blame] | 1794 | prev = i; |
| 1795 | } |
Ian Romanick | 13e10e4 | 2010-06-21 12:03:24 -0700 | [diff] [blame] | 1796 | |
Paul Berry | 871ddb9 | 2011-11-05 11:17:32 -0700 | [diff] [blame] | 1797 | if (prev != MESA_SHADER_FRAGMENT && num_tfeedback_decls != 0) { |
| 1798 | /* There was no fragment shader, but we still have to assign varying |
| 1799 | * locations for use by transform feedback. |
| 1800 | */ |
| 1801 | if (!assign_varying_locations( |
Dave Airlie | 7d7a549 | 2012-12-15 13:24:52 +1000 | [diff] [blame] | 1802 | ctx, mem_ctx, prog, prog->_LinkedShaders[prev], NULL, num_tfeedback_decls, |
Paul Berry | 871ddb9 | 2011-11-05 11:17:32 -0700 | [diff] [blame] | 1803 | tfeedback_decls)) |
| 1804 | goto done; |
| 1805 | } |
| 1806 | |
| 1807 | if (!store_tfeedback_info(ctx, prog, num_tfeedback_decls, tfeedback_decls)) |
| 1808 | goto done; |
| 1809 | |
Ian Romanick | cc90e62 | 2010-10-19 17:59:10 -0700 | [diff] [blame] | 1810 | if (prog->_LinkedShaders[MESA_SHADER_VERTEX] != NULL) { |
| 1811 | demote_shader_inputs_and_outputs(prog->_LinkedShaders[MESA_SHADER_VERTEX], |
| 1812 | ir_var_out); |
Ian Romanick | 960d722 | 2011-10-21 11:21:02 -0700 | [diff] [blame] | 1813 | |
| 1814 | /* Eliminate code that is now dead due to unused vertex outputs being |
| 1815 | * demoted. |
| 1816 | */ |
| 1817 | while (do_dead_code(prog->_LinkedShaders[MESA_SHADER_VERTEX]->ir, false)) |
| 1818 | ; |
Ian Romanick | cc90e62 | 2010-10-19 17:59:10 -0700 | [diff] [blame] | 1819 | } |
| 1820 | |
| 1821 | if (prog->_LinkedShaders[MESA_SHADER_GEOMETRY] != NULL) { |
| 1822 | gl_shader *const sh = prog->_LinkedShaders[MESA_SHADER_GEOMETRY]; |
| 1823 | |
| 1824 | demote_shader_inputs_and_outputs(sh, ir_var_in); |
| 1825 | demote_shader_inputs_and_outputs(sh, ir_var_inout); |
| 1826 | demote_shader_inputs_and_outputs(sh, ir_var_out); |
Ian Romanick | 960d722 | 2011-10-21 11:21:02 -0700 | [diff] [blame] | 1827 | |
| 1828 | /* Eliminate code that is now dead due to unused geometry outputs being |
| 1829 | * demoted. |
| 1830 | */ |
| 1831 | while (do_dead_code(prog->_LinkedShaders[MESA_SHADER_GEOMETRY]->ir, false)) |
| 1832 | ; |
Ian Romanick | cc90e62 | 2010-10-19 17:59:10 -0700 | [diff] [blame] | 1833 | } |
| 1834 | |
| 1835 | if (prog->_LinkedShaders[MESA_SHADER_FRAGMENT] != NULL) { |
| 1836 | gl_shader *const sh = prog->_LinkedShaders[MESA_SHADER_FRAGMENT]; |
| 1837 | |
| 1838 | demote_shader_inputs_and_outputs(sh, ir_var_in); |
Ian Romanick | 960d722 | 2011-10-21 11:21:02 -0700 | [diff] [blame] | 1839 | |
| 1840 | /* Eliminate code that is now dead due to unused fragment inputs being |
| 1841 | * demoted. This shouldn't actually do anything other than remove |
| 1842 | * declarations of the (now unused) global variables. |
| 1843 | */ |
| 1844 | while (do_dead_code(prog->_LinkedShaders[MESA_SHADER_FRAGMENT]->ir, false)) |
| 1845 | ; |
Ian Romanick | cc90e62 | 2010-10-19 17:59:10 -0700 | [diff] [blame] | 1846 | } |
| 1847 | |
Ian Romanick | 960d722 | 2011-10-21 11:21:02 -0700 | [diff] [blame] | 1848 | update_array_sizes(prog); |
Ian Romanick | 7199096 | 2011-10-18 16:01:49 -0700 | [diff] [blame] | 1849 | link_assign_uniform_locations(prog); |
Marek Olšák | ec174a4 | 2011-11-18 15:00:10 +0100 | [diff] [blame] | 1850 | store_fragdepth_layout(prog); |
Ian Romanick | 960d722 | 2011-10-21 11:21:02 -0700 | [diff] [blame] | 1851 | |
Ian Romanick | 92f8159 | 2011-11-08 12:37:19 -0800 | [diff] [blame] | 1852 | if (!check_resources(ctx, prog)) |
| 1853 | goto done; |
| 1854 | |
Ian Romanick | ce9171f | 2011-02-03 17:10:14 -0800 | [diff] [blame] | 1855 | /* 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] | 1856 | * present in a linked program. By checking prog->IsES, we also |
| 1857 | * catch the GL_ARB_ES2_compatibility case. |
Ian Romanick | ce9171f | 2011-02-03 17:10:14 -0800 | [diff] [blame] | 1858 | */ |
Eric Anholt | 57f7978 | 2011-07-22 12:57:47 -0700 | [diff] [blame] | 1859 | if (!prog->InternalSeparateShader && |
Paul Berry | 15ba2a5 | 2012-08-02 17:51:02 -0700 | [diff] [blame] | 1860 | (ctx->API == API_OPENGLES2 || prog->IsES)) { |
Ian Romanick | ce9171f | 2011-02-03 17:10:14 -0800 | [diff] [blame] | 1861 | if (prog->_LinkedShaders[MESA_SHADER_VERTEX] == NULL) { |
Ian Romanick | 586e741 | 2011-07-28 14:04:09 -0700 | [diff] [blame] | 1862 | linker_error(prog, "program lacks a vertex shader\n"); |
Ian Romanick | ce9171f | 2011-02-03 17:10:14 -0800 | [diff] [blame] | 1863 | } else if (prog->_LinkedShaders[MESA_SHADER_FRAGMENT] == NULL) { |
Ian Romanick | 586e741 | 2011-07-28 14:04:09 -0700 | [diff] [blame] | 1864 | linker_error(prog, "program lacks a fragment shader\n"); |
Ian Romanick | ce9171f | 2011-02-03 17:10:14 -0800 | [diff] [blame] | 1865 | } |
| 1866 | } |
| 1867 | |
Ian Romanick | 13e10e4 | 2010-06-21 12:03:24 -0700 | [diff] [blame] | 1868 | /* FINISHME: Assign fragment shader output locations. */ |
| 1869 | |
Ian Romanick | 832dfa5 | 2010-06-17 15:04:20 -0700 | [diff] [blame] | 1870 | done: |
| 1871 | free(vert_shader_list); |
Kenneth Graunke | 2da02e7 | 2010-11-17 11:03:57 -0800 | [diff] [blame] | 1872 | |
| 1873 | for (unsigned i = 0; i < MESA_SHADER_TYPES; i++) { |
| 1874 | if (prog->_LinkedShaders[i] == NULL) |
| 1875 | continue; |
| 1876 | |
| 1877 | /* Retain any live IR, but trash the rest. */ |
| 1878 | reparent_ir(prog->_LinkedShaders[i]->ir, prog->_LinkedShaders[i]->ir); |
Ian Romanick | 7bbcc0b | 2011-09-30 14:21:10 -0700 | [diff] [blame] | 1879 | |
| 1880 | /* The symbol table in the linked shaders may contain references to |
| 1881 | * variables that were removed (e.g., unused uniforms). Since it may |
| 1882 | * contain junk, there is no possible valid use. Delete it and set the |
| 1883 | * pointer to NULL. |
| 1884 | */ |
| 1885 | delete prog->_LinkedShaders[i]->symbols; |
| 1886 | prog->_LinkedShaders[i]->symbols = NULL; |
Kenneth Graunke | 2da02e7 | 2010-11-17 11:03:57 -0800 | [diff] [blame] | 1887 | } |
| 1888 | |
Kenneth Graunke | d3073f5 | 2011-01-21 14:32:31 -0800 | [diff] [blame] | 1889 | ralloc_free(mem_ctx); |
Ian Romanick | 832dfa5 | 2010-06-17 15:04:20 -0700 | [diff] [blame] | 1890 | } |