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