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 | */ |
| 66 | #include <cstdlib> |
| 67 | #include <cstdio> |
| 68 | |
| 69 | #include "glsl_symbol_table.h" |
| 70 | #include "glsl_parser_extras.h" |
| 71 | #include "ir.h" |
| 72 | #include "program.h" |
| 73 | |
| 74 | /** |
| 75 | * Visitor that determines whether or not a variable is ever written. |
| 76 | */ |
| 77 | class find_assignment_visitor : public ir_hierarchical_visitor { |
| 78 | public: |
| 79 | find_assignment_visitor(const char *name) |
| 80 | : name(name), found(false) |
| 81 | { |
| 82 | /* empty */ |
| 83 | } |
| 84 | |
| 85 | virtual ir_visitor_status visit_enter(ir_assignment *ir) |
| 86 | { |
| 87 | ir_variable *const var = ir->lhs->variable_referenced(); |
| 88 | |
| 89 | if (strcmp(name, var->name) == 0) { |
| 90 | found = true; |
| 91 | return visit_stop; |
| 92 | } |
| 93 | |
| 94 | return visit_continue_with_parent; |
| 95 | } |
| 96 | |
| 97 | bool variable_found() |
| 98 | { |
| 99 | return found; |
| 100 | } |
| 101 | |
| 102 | private: |
| 103 | const char *name; /**< Find writes to a variable with this name. */ |
| 104 | bool found; /**< Was a write to the variable found? */ |
| 105 | }; |
| 106 | |
Ian Romanick | c93b8f1 | 2010-06-17 15:20:22 -0700 | [diff] [blame] | 107 | |
| 108 | /** |
| 109 | * Verify that a vertex shader executable meets all semantic requirements |
| 110 | * |
| 111 | * \param shader Vertex shader executable to be verified |
| 112 | */ |
Ian Romanick | 832dfa5 | 2010-06-17 15:04:20 -0700 | [diff] [blame] | 113 | bool |
| 114 | validate_vertex_shader_executable(struct glsl_shader *shader) |
| 115 | { |
| 116 | if (shader == NULL) |
| 117 | return true; |
| 118 | |
| 119 | if (!shader->symbols->get_function("main")) { |
| 120 | printf("error: vertex shader lacks `main'\n"); |
| 121 | return false; |
| 122 | } |
| 123 | |
| 124 | find_assignment_visitor find("gl_Position"); |
| 125 | find.run(&shader->ir); |
| 126 | if (!find.variable_found()) { |
| 127 | printf("error: vertex shader does not write to `gl_Position'\n"); |
| 128 | return false; |
| 129 | } |
| 130 | |
| 131 | return true; |
| 132 | } |
| 133 | |
| 134 | |
Ian Romanick | c93b8f1 | 2010-06-17 15:20:22 -0700 | [diff] [blame] | 135 | /** |
| 136 | * Verify that a fragment shader executable meets all semantic requirements |
| 137 | * |
| 138 | * \param shader Fragment shader executable to be verified |
| 139 | */ |
Ian Romanick | 832dfa5 | 2010-06-17 15:04:20 -0700 | [diff] [blame] | 140 | bool |
| 141 | validate_fragment_shader_executable(struct glsl_shader *shader) |
| 142 | { |
| 143 | if (shader == NULL) |
| 144 | return true; |
| 145 | |
| 146 | if (!shader->symbols->get_function("main")) { |
| 147 | printf("error: fragment shader lacks `main'\n"); |
| 148 | return false; |
| 149 | } |
| 150 | |
| 151 | find_assignment_visitor frag_color("gl_FragColor"); |
| 152 | find_assignment_visitor frag_data("gl_FragData"); |
| 153 | |
| 154 | frag_color.run(&shader->ir); |
| 155 | frag_data.run(&shader->ir); |
| 156 | |
| 157 | if (!frag_color.variable_found() && !frag_data.variable_found()) { |
| 158 | printf("error: fragment shader does not write to `gl_FragColor' or " |
| 159 | "`gl_FragData'\n"); |
| 160 | return false; |
| 161 | } |
| 162 | |
| 163 | if (frag_color.variable_found() && frag_data.variable_found()) { |
| 164 | printf("error: fragment shader write to both `gl_FragColor' and " |
| 165 | "`gl_FragData'\n"); |
| 166 | return false; |
| 167 | } |
| 168 | |
| 169 | return true; |
| 170 | } |
| 171 | |
| 172 | |
Ian Romanick | cc22c5a | 2010-06-18 17:13:42 -0700 | [diff] [blame] | 173 | /** |
| 174 | * Perform validation of uniforms used across multiple shader stages |
| 175 | */ |
| 176 | bool |
| 177 | cross_validate_uniforms(struct glsl_shader **shaders, unsigned num_shaders) |
| 178 | { |
| 179 | /* Examine all of the uniforms in all of the shaders and cross validate |
| 180 | * them. |
| 181 | */ |
| 182 | glsl_symbol_table uniforms; |
| 183 | for (unsigned i = 0; i < num_shaders; i++) { |
| 184 | foreach_list(node, &shaders[i]->ir) { |
| 185 | ir_variable *const var = ((ir_instruction *) node)->as_variable(); |
| 186 | |
| 187 | if ((var == NULL) || (var->mode != ir_var_uniform)) |
| 188 | continue; |
| 189 | |
| 190 | /* If a uniform with this name has already been seen, verify that the |
| 191 | * new instance has the same type. In addition, if the uniforms have |
| 192 | * initializers, the values of the initializers must be the same. |
| 193 | */ |
| 194 | ir_variable *const existing = uniforms.get_variable(var->name); |
| 195 | if (existing != NULL) { |
| 196 | if (var->type != existing->type) { |
| 197 | printf("error: uniform `%s' declared as type `%s' and " |
| 198 | "type `%s'\n", |
| 199 | var->name, var->type->name, existing->type->name); |
| 200 | return false; |
| 201 | } |
| 202 | |
| 203 | if (var->constant_value != NULL) { |
| 204 | if (existing->constant_value != NULL) { |
| 205 | if (!var->constant_value->has_value(existing->constant_value)) { |
| 206 | printf("error: initializers for uniform `%s' have " |
| 207 | "differing values\n", |
| 208 | var->name); |
| 209 | return false; |
| 210 | } |
| 211 | } else |
| 212 | /* If the first-seen instance of a particular uniform did not |
| 213 | * have an initializer but a later instance does, copy the |
| 214 | * initializer to the version stored in the symbol table. |
| 215 | */ |
| 216 | existing->constant_value = var->constant_value->clone(); |
| 217 | } |
| 218 | } else |
| 219 | uniforms.add_variable(var->name, var); |
| 220 | } |
| 221 | } |
| 222 | |
| 223 | return true; |
| 224 | } |
| 225 | |
| 226 | |
Ian Romanick | 3710192 | 2010-06-18 19:02:10 -0700 | [diff] [blame] | 227 | /** |
| 228 | * Validate that outputs from one stage match inputs of another |
| 229 | */ |
| 230 | bool |
| 231 | cross_validate_outputs_to_inputs(glsl_shader *producer, glsl_shader *consumer) |
| 232 | { |
| 233 | glsl_symbol_table parameters; |
| 234 | /* FINISHME: Figure these out dynamically. */ |
| 235 | const char *const producer_stage = "vertex"; |
| 236 | const char *const consumer_stage = "fragment"; |
| 237 | |
| 238 | /* Find all shader outputs in the "producer" stage. |
| 239 | */ |
| 240 | foreach_list(node, &producer->ir) { |
| 241 | ir_variable *const var = ((ir_instruction *) node)->as_variable(); |
| 242 | |
| 243 | /* FINISHME: For geometry shaders, this should also look for inout |
| 244 | * FINISHME: variables. |
| 245 | */ |
| 246 | if ((var == NULL) || (var->mode != ir_var_out)) |
| 247 | continue; |
| 248 | |
| 249 | parameters.add_variable(var->name, var); |
| 250 | } |
| 251 | |
| 252 | |
| 253 | /* Find all shader inputs in the "consumer" stage. Any variables that have |
| 254 | * matching outputs already in the symbol table must have the same type and |
| 255 | * qualifiers. |
| 256 | */ |
| 257 | foreach_list(node, &consumer->ir) { |
| 258 | ir_variable *const input = ((ir_instruction *) node)->as_variable(); |
| 259 | |
| 260 | /* FINISHME: For geometry shaders, this should also look for inout |
| 261 | * FINISHME: variables. |
| 262 | */ |
| 263 | if ((input == NULL) || (input->mode != ir_var_in)) |
| 264 | continue; |
| 265 | |
| 266 | ir_variable *const output = parameters.get_variable(input->name); |
| 267 | if (output != NULL) { |
| 268 | /* Check that the types match between stages. |
| 269 | */ |
| 270 | if (input->type != output->type) { |
| 271 | printf("error: %s shader output `%s' delcared as type `%s', but " |
| 272 | "%s shader input declared as type `%s'\n", |
| 273 | producer_stage, output->name, output->type->name, |
| 274 | consumer_stage, input->type->name); |
| 275 | return false; |
| 276 | } |
| 277 | |
| 278 | /* Check that all of the qualifiers match between stages. |
| 279 | */ |
| 280 | if (input->centroid != output->centroid) { |
| 281 | printf("error: %s shader output `%s' %s centroid qualifier, but " |
| 282 | "%s shader input %s centroid qualifier\n", |
| 283 | producer_stage, |
| 284 | output->name, |
| 285 | (output->centroid) ? "has" : "lacks", |
| 286 | consumer_stage, |
| 287 | (input->centroid) ? "has" : "lacks"); |
| 288 | return false; |
| 289 | } |
| 290 | |
| 291 | if (input->invariant != output->invariant) { |
| 292 | printf("error: %s shader output `%s' %s invariant qualifier, but " |
| 293 | "%s shader input %s invariant qualifier\n", |
| 294 | producer_stage, |
| 295 | output->name, |
| 296 | (output->invariant) ? "has" : "lacks", |
| 297 | consumer_stage, |
| 298 | (input->invariant) ? "has" : "lacks"); |
| 299 | return false; |
| 300 | } |
| 301 | |
| 302 | if (input->interpolation != output->interpolation) { |
| 303 | printf("error: %s shader output `%s' specifies %s interpolation " |
| 304 | "qualifier, " |
| 305 | "but %s shader input specifies %s interpolation " |
| 306 | "qualifier\n", |
| 307 | producer_stage, |
| 308 | output->name, |
| 309 | output->interpolation_string(), |
| 310 | consumer_stage, |
| 311 | input->interpolation_string()); |
| 312 | return false; |
| 313 | } |
| 314 | } |
| 315 | } |
| 316 | |
| 317 | return true; |
| 318 | } |
| 319 | |
| 320 | |
Ian Romanick | 832dfa5 | 2010-06-17 15:04:20 -0700 | [diff] [blame] | 321 | void |
| 322 | link_shaders(struct glsl_program *prog) |
| 323 | { |
| 324 | prog->LinkStatus = false; |
| 325 | prog->Validated = false; |
| 326 | prog->_Used = false; |
| 327 | |
| 328 | /* Separate the shaders into groups based on their type. |
| 329 | */ |
| 330 | struct glsl_shader **vert_shader_list; |
| 331 | unsigned num_vert_shaders = 0; |
| 332 | struct glsl_shader **frag_shader_list; |
| 333 | unsigned num_frag_shaders = 0; |
| 334 | |
| 335 | vert_shader_list = (struct glsl_shader **) |
Kenneth Graunke | c186b3f | 2010-06-17 15:37:26 -0700 | [diff] [blame] | 336 | calloc(2 * prog->NumShaders, sizeof(struct glsl_shader *)); |
Ian Romanick | 832dfa5 | 2010-06-17 15:04:20 -0700 | [diff] [blame] | 337 | frag_shader_list = &vert_shader_list[prog->NumShaders]; |
| 338 | |
| 339 | for (unsigned i = 0; i < prog->NumShaders; i++) { |
| 340 | switch (prog->Shaders[i]->Type) { |
| 341 | case GL_VERTEX_SHADER: |
| 342 | vert_shader_list[num_vert_shaders] = prog->Shaders[i]; |
| 343 | num_vert_shaders++; |
| 344 | break; |
| 345 | case GL_FRAGMENT_SHADER: |
| 346 | frag_shader_list[num_frag_shaders] = prog->Shaders[i]; |
| 347 | num_frag_shaders++; |
| 348 | break; |
| 349 | case GL_GEOMETRY_SHADER: |
| 350 | /* FINISHME: Support geometry shaders. */ |
| 351 | assert(prog->Shaders[i]->Type != GL_GEOMETRY_SHADER); |
| 352 | break; |
| 353 | } |
| 354 | } |
| 355 | |
| 356 | /* FINISHME: Implement intra-stage linking. */ |
| 357 | assert(num_vert_shaders <= 1); |
| 358 | assert(num_frag_shaders <= 1); |
| 359 | |
| 360 | /* Verify that each of the per-target executables is valid. |
| 361 | */ |
| 362 | if (!validate_vertex_shader_executable(vert_shader_list[0]) |
| 363 | || !validate_fragment_shader_executable(frag_shader_list[0])) |
| 364 | goto done; |
| 365 | |
| 366 | |
| 367 | /* FINISHME: Perform inter-stage linking. */ |
Ian Romanick | cc22c5a | 2010-06-18 17:13:42 -0700 | [diff] [blame] | 368 | glsl_shader *shader_executables[2]; |
| 369 | unsigned num_shader_executables; |
Ian Romanick | 832dfa5 | 2010-06-17 15:04:20 -0700 | [diff] [blame] | 370 | |
Ian Romanick | cc22c5a | 2010-06-18 17:13:42 -0700 | [diff] [blame] | 371 | num_shader_executables = 0; |
| 372 | if (num_vert_shaders > 0) { |
| 373 | shader_executables[num_shader_executables] = vert_shader_list[0]; |
| 374 | num_shader_executables++; |
| 375 | } |
| 376 | |
| 377 | if (num_frag_shaders > 0) { |
| 378 | shader_executables[num_shader_executables] = frag_shader_list[0]; |
| 379 | num_shader_executables++; |
| 380 | } |
| 381 | |
Ian Romanick | 3710192 | 2010-06-18 19:02:10 -0700 | [diff] [blame] | 382 | if (cross_validate_uniforms(shader_executables, num_shader_executables)) { |
| 383 | /* Validate the inputs of each stage with the output of the preceeding |
| 384 | * stage. |
| 385 | */ |
| 386 | for (unsigned i = 1; i < num_shader_executables; i++) { |
| 387 | if (!cross_validate_outputs_to_inputs(shader_executables[i - 1], |
| 388 | shader_executables[i])) |
| 389 | goto done; |
| 390 | } |
| 391 | |
Ian Romanick | cc22c5a | 2010-06-18 17:13:42 -0700 | [diff] [blame] | 392 | prog->LinkStatus = true; |
Ian Romanick | 3710192 | 2010-06-18 19:02:10 -0700 | [diff] [blame] | 393 | } |
Ian Romanick | 832dfa5 | 2010-06-17 15:04:20 -0700 | [diff] [blame] | 394 | |
Ian Romanick | 13e10e4 | 2010-06-21 12:03:24 -0700 | [diff] [blame] | 395 | /* FINISHME: Perform whole-program optimization here. */ |
| 396 | |
| 397 | /* FINISHME: Assign uniform locations. */ |
| 398 | |
| 399 | /* FINISHME: Assign vertex shader input locations. */ |
| 400 | |
| 401 | /* FINISHME: Assign vertex shader output / fragment shader input |
| 402 | * FINISHME: locations. |
| 403 | */ |
| 404 | |
| 405 | /* FINISHME: Assign fragment shader output locations. */ |
| 406 | |
| 407 | /* FINISHME: Generate code here. */ |
| 408 | |
Ian Romanick | 832dfa5 | 2010-06-17 15:04:20 -0700 | [diff] [blame] | 409 | done: |
| 410 | free(vert_shader_list); |
| 411 | } |