blob: 04b4efd84baf8290707ab197e96a919fd728302f [file] [log] [blame]
Ian Romanick832dfa52010-06-17 15:04:20 -07001/*
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>
Ian Romanickf36460e2010-06-23 12:07:22 -070068#include <cstdarg>
69
70extern "C" {
71#include <talloc.h>
72}
Ian Romanick832dfa52010-06-17 15:04:20 -070073
Ian Romanick0ad22cd2010-06-21 17:18:31 -070074#include "main/mtypes.h"
Ian Romanick832dfa52010-06-17 15:04:20 -070075#include "glsl_symbol_table.h"
76#include "glsl_parser_extras.h"
77#include "ir.h"
Ian Romanick019a59b2010-06-21 16:10:42 -070078#include "ir_optimization.h"
Ian Romanick832dfa52010-06-17 15:04:20 -070079#include "program.h"
Ian Romanick019a59b2010-06-21 16:10:42 -070080#include "hash_table.h"
Ian Romanick3fb87872010-07-09 14:09:34 -070081#include "shader_api.h"
Ian Romanick832dfa52010-06-17 15:04:20 -070082
83/**
84 * Visitor that determines whether or not a variable is ever written.
85 */
86class find_assignment_visitor : public ir_hierarchical_visitor {
87public:
88 find_assignment_visitor(const char *name)
89 : name(name), found(false)
90 {
91 /* empty */
92 }
93
94 virtual ir_visitor_status visit_enter(ir_assignment *ir)
95 {
96 ir_variable *const var = ir->lhs->variable_referenced();
97
98 if (strcmp(name, var->name) == 0) {
99 found = true;
100 return visit_stop;
101 }
102
103 return visit_continue_with_parent;
104 }
105
106 bool variable_found()
107 {
108 return found;
109 }
110
111private:
112 const char *name; /**< Find writes to a variable with this name. */
113 bool found; /**< Was a write to the variable found? */
114};
115
Ian Romanickc93b8f12010-06-17 15:20:22 -0700116
Ian Romanick0ad22cd2010-06-21 17:18:31 -0700117void
Eric Anholt849e1812010-06-30 11:49:17 -0700118linker_error_printf(gl_shader_program *prog, const char *fmt, ...)
Ian Romanickf36460e2010-06-23 12:07:22 -0700119{
120 va_list ap;
121
122 prog->InfoLog = talloc_strdup_append(prog->InfoLog, "error: ");
123 va_start(ap, fmt);
124 prog->InfoLog = talloc_vasprintf_append(prog->InfoLog, fmt, ap);
125 va_end(ap);
126}
127
128
129void
Eric Anholt16b68b12010-06-30 11:05:43 -0700130invalidate_variable_locations(gl_shader *sh, enum ir_variable_mode mode,
Ian Romanick0ad22cd2010-06-21 17:18:31 -0700131 int generic_base)
132{
Eric Anholt16b68b12010-06-30 11:05:43 -0700133 foreach_list(node, sh->ir) {
Ian Romanick0ad22cd2010-06-21 17:18:31 -0700134 ir_variable *const var = ((ir_instruction *) node)->as_variable();
135
136 if ((var == NULL) || (var->mode != (unsigned) mode))
137 continue;
138
139 /* Only assign locations for generic attributes / varyings / etc.
140 */
141 if (var->location >= generic_base)
142 var->location = -1;
143 }
144}
145
146
Ian Romanickc93b8f12010-06-17 15:20:22 -0700147/**
Ian Romanick69846702010-06-22 17:29:19 -0700148 * Determine the number of attribute slots required for a particular type
149 *
150 * This code is here because it implements the language rules of a specific
151 * GLSL version. Since it's a property of the language and not a property of
152 * types in general, it doesn't really belong in glsl_type.
153 */
154unsigned
155count_attribute_slots(const glsl_type *t)
156{
157 /* From page 31 (page 37 of the PDF) of the GLSL 1.50 spec:
158 *
159 * "A scalar input counts the same amount against this limit as a vec4,
160 * so applications may want to consider packing groups of four
161 * unrelated float inputs together into a vector to better utilize the
162 * capabilities of the underlying hardware. A matrix input will use up
163 * multiple locations. The number of locations used will equal the
164 * number of columns in the matrix."
165 *
166 * The spec does not explicitly say how arrays are counted. However, it
167 * should be safe to assume the total number of slots consumed by an array
168 * is the number of entries in the array multiplied by the number of slots
169 * consumed by a single element of the array.
170 */
171
172 if (t->is_array())
173 return t->array_size() * count_attribute_slots(t->element_type());
174
175 if (t->is_matrix())
176 return t->matrix_columns;
177
178 return 1;
179}
180
181
182/**
Ian Romanickc93b8f12010-06-17 15:20:22 -0700183 * Verify that a vertex shader executable meets all semantic requirements
184 *
185 * \param shader Vertex shader executable to be verified
186 */
Ian Romanick832dfa52010-06-17 15:04:20 -0700187bool
Eric Anholt849e1812010-06-30 11:49:17 -0700188validate_vertex_shader_executable(struct gl_shader_program *prog,
Eric Anholt16b68b12010-06-30 11:05:43 -0700189 struct gl_shader *shader)
Ian Romanick832dfa52010-06-17 15:04:20 -0700190{
191 if (shader == NULL)
192 return true;
193
194 if (!shader->symbols->get_function("main")) {
Ian Romanickf36460e2010-06-23 12:07:22 -0700195 linker_error_printf(prog, "vertex shader lacks `main'\n");
Ian Romanick832dfa52010-06-17 15:04:20 -0700196 return false;
197 }
198
199 find_assignment_visitor find("gl_Position");
Eric Anholt16b68b12010-06-30 11:05:43 -0700200 find.run(shader->ir);
Ian Romanick832dfa52010-06-17 15:04:20 -0700201 if (!find.variable_found()) {
Ian Romanickf36460e2010-06-23 12:07:22 -0700202 linker_error_printf(prog,
203 "vertex shader does not write to `gl_Position'\n");
Ian Romanick832dfa52010-06-17 15:04:20 -0700204 return false;
205 }
206
207 return true;
208}
209
210
Ian Romanickc93b8f12010-06-17 15:20:22 -0700211/**
212 * Verify that a fragment shader executable meets all semantic requirements
213 *
214 * \param shader Fragment shader executable to be verified
215 */
Ian Romanick832dfa52010-06-17 15:04:20 -0700216bool
Eric Anholt849e1812010-06-30 11:49:17 -0700217validate_fragment_shader_executable(struct gl_shader_program *prog,
Eric Anholt16b68b12010-06-30 11:05:43 -0700218 struct gl_shader *shader)
Ian Romanick832dfa52010-06-17 15:04:20 -0700219{
220 if (shader == NULL)
221 return true;
222
223 if (!shader->symbols->get_function("main")) {
Ian Romanickf36460e2010-06-23 12:07:22 -0700224 linker_error_printf(prog, "fragment shader lacks `main'\n");
Ian Romanick832dfa52010-06-17 15:04:20 -0700225 return false;
226 }
227
228 find_assignment_visitor frag_color("gl_FragColor");
229 find_assignment_visitor frag_data("gl_FragData");
230
Eric Anholt16b68b12010-06-30 11:05:43 -0700231 frag_color.run(shader->ir);
232 frag_data.run(shader->ir);
Ian Romanick832dfa52010-06-17 15:04:20 -0700233
Ian Romanick832dfa52010-06-17 15:04:20 -0700234 if (frag_color.variable_found() && frag_data.variable_found()) {
Ian Romanickf36460e2010-06-23 12:07:22 -0700235 linker_error_printf(prog, "fragment shader writes to both "
236 "`gl_FragColor' and `gl_FragData'\n");
Ian Romanick832dfa52010-06-17 15:04:20 -0700237 return false;
238 }
239
240 return true;
241}
242
243
Ian Romanickcc22c5a2010-06-18 17:13:42 -0700244/**
Ian Romanicke2e5d0d2010-06-29 18:47:11 -0700245 * Generate a string describing the mode of a variable
246 */
247static const char *
248mode_string(const ir_variable *var)
249{
250 switch (var->mode) {
251 case ir_var_auto:
252 return (var->read_only) ? "global constant" : "global variable";
253
254 case ir_var_uniform: return "uniform";
255 case ir_var_in: return "shader input";
256 case ir_var_out: return "shader output";
257 case ir_var_inout: return "shader inout";
258 default:
259 assert(!"Should not get here.");
260 return "invalid variable";
261 }
262}
263
264
265/**
266 * Perform validation of global variables used across multiple shaders
Ian Romanickcc22c5a2010-06-18 17:13:42 -0700267 */
268bool
Ian Romanicke2e5d0d2010-06-29 18:47:11 -0700269cross_validate_globals(struct gl_shader_program *prog,
270 struct gl_shader **shader_list,
271 unsigned num_shaders,
272 bool uniforms_only)
Ian Romanickcc22c5a2010-06-18 17:13:42 -0700273{
274 /* Examine all of the uniforms in all of the shaders and cross validate
275 * them.
276 */
Ian Romanicke2e5d0d2010-06-29 18:47:11 -0700277 glsl_symbol_table variables;
278 for (unsigned i = 0; i < num_shaders; i++) {
279 foreach_list(node, shader_list[i]->ir) {
Ian Romanickcc22c5a2010-06-18 17:13:42 -0700280 ir_variable *const var = ((ir_instruction *) node)->as_variable();
281
Ian Romanicke2e5d0d2010-06-29 18:47:11 -0700282 if (var == NULL)
Ian Romanickcc22c5a2010-06-18 17:13:42 -0700283 continue;
284
Ian Romanicke2e5d0d2010-06-29 18:47:11 -0700285 if (uniforms_only && (var->mode != ir_var_uniform))
286 continue;
287
288 /* If a global with this name has already been seen, verify that the
289 * new instance has the same type. In addition, if the globals have
Ian Romanickcc22c5a2010-06-18 17:13:42 -0700290 * initializers, the values of the initializers must be the same.
291 */
Ian Romanicke2e5d0d2010-06-29 18:47:11 -0700292 ir_variable *const existing = variables.get_variable(var->name);
Ian Romanickcc22c5a2010-06-18 17:13:42 -0700293 if (existing != NULL) {
294 if (var->type != existing->type) {
Ian Romanicke2e5d0d2010-06-29 18:47:11 -0700295 linker_error_printf(prog, "%s `%s' declared as type "
Ian Romanickf36460e2010-06-23 12:07:22 -0700296 "`%s' and type `%s'\n",
Ian Romanicke2e5d0d2010-06-29 18:47:11 -0700297 mode_string(var),
Ian Romanickf36460e2010-06-23 12:07:22 -0700298 var->name, var->type->name,
299 existing->type->name);
Ian Romanickcc22c5a2010-06-18 17:13:42 -0700300 return false;
301 }
302
Ian Romanicke2e5d0d2010-06-29 18:47:11 -0700303 /* FINISHME: Handle non-constant initializers.
304 */
Ian Romanickcc22c5a2010-06-18 17:13:42 -0700305 if (var->constant_value != NULL) {
306 if (existing->constant_value != NULL) {
307 if (!var->constant_value->has_value(existing->constant_value)) {
Ian Romanicke2e5d0d2010-06-29 18:47:11 -0700308 linker_error_printf(prog, "initializers for %s "
Ian Romanickf36460e2010-06-23 12:07:22 -0700309 "`%s' have differing values\n",
Ian Romanicke2e5d0d2010-06-29 18:47:11 -0700310 mode_string(var), var->name);
Ian Romanickcc22c5a2010-06-18 17:13:42 -0700311 return false;
312 }
313 } else
314 /* If the first-seen instance of a particular uniform did not
315 * have an initializer but a later instance does, copy the
316 * initializer to the version stored in the symbol table.
317 */
Eric Anholt4b6fd392010-06-23 11:37:12 -0700318 existing->constant_value =
319 (ir_constant *)var->constant_value->clone(NULL);
Ian Romanickcc22c5a2010-06-18 17:13:42 -0700320 }
321 } else
Ian Romanicke2e5d0d2010-06-29 18:47:11 -0700322 variables.add_variable(var->name, var);
Ian Romanickcc22c5a2010-06-18 17:13:42 -0700323 }
324 }
325
326 return true;
327}
328
329
Ian Romanick37101922010-06-18 19:02:10 -0700330/**
Ian Romanicke2e5d0d2010-06-29 18:47:11 -0700331 * Perform validation of uniforms used across multiple shader stages
332 */
333bool
334cross_validate_uniforms(struct gl_shader_program *prog)
335{
336 return cross_validate_globals(prog, prog->_LinkedShaders,
337 prog->_NumLinkedShaders, true);
338}
339
340
341/**
Ian Romanick37101922010-06-18 19:02:10 -0700342 * Validate that outputs from one stage match inputs of another
343 */
344bool
Eric Anholt849e1812010-06-30 11:49:17 -0700345cross_validate_outputs_to_inputs(struct gl_shader_program *prog,
Eric Anholt16b68b12010-06-30 11:05:43 -0700346 gl_shader *producer, gl_shader *consumer)
Ian Romanick37101922010-06-18 19:02:10 -0700347{
348 glsl_symbol_table parameters;
349 /* FINISHME: Figure these out dynamically. */
350 const char *const producer_stage = "vertex";
351 const char *const consumer_stage = "fragment";
352
353 /* Find all shader outputs in the "producer" stage.
354 */
Eric Anholt16b68b12010-06-30 11:05:43 -0700355 foreach_list(node, producer->ir) {
Ian Romanick37101922010-06-18 19:02:10 -0700356 ir_variable *const var = ((ir_instruction *) node)->as_variable();
357
358 /* FINISHME: For geometry shaders, this should also look for inout
359 * FINISHME: variables.
360 */
361 if ((var == NULL) || (var->mode != ir_var_out))
362 continue;
363
364 parameters.add_variable(var->name, var);
365 }
366
367
368 /* Find all shader inputs in the "consumer" stage. Any variables that have
369 * matching outputs already in the symbol table must have the same type and
370 * qualifiers.
371 */
Eric Anholt16b68b12010-06-30 11:05:43 -0700372 foreach_list(node, consumer->ir) {
Ian Romanick37101922010-06-18 19:02:10 -0700373 ir_variable *const input = ((ir_instruction *) node)->as_variable();
374
375 /* FINISHME: For geometry shaders, this should also look for inout
376 * FINISHME: variables.
377 */
378 if ((input == NULL) || (input->mode != ir_var_in))
379 continue;
380
381 ir_variable *const output = parameters.get_variable(input->name);
382 if (output != NULL) {
383 /* Check that the types match between stages.
384 */
385 if (input->type != output->type) {
Ian Romanickf36460e2010-06-23 12:07:22 -0700386 linker_error_printf(prog,
387 "%s shader output `%s' delcared as "
388 "type `%s', but %s shader input declared "
389 "as type `%s'\n",
390 producer_stage, output->name,
391 output->type->name,
392 consumer_stage, input->type->name);
Ian Romanick37101922010-06-18 19:02:10 -0700393 return false;
394 }
395
396 /* Check that all of the qualifiers match between stages.
397 */
398 if (input->centroid != output->centroid) {
Ian Romanickf36460e2010-06-23 12:07:22 -0700399 linker_error_printf(prog,
400 "%s shader output `%s' %s centroid qualifier, "
401 "but %s shader input %s centroid qualifier\n",
402 producer_stage,
403 output->name,
404 (output->centroid) ? "has" : "lacks",
405 consumer_stage,
406 (input->centroid) ? "has" : "lacks");
Ian Romanick37101922010-06-18 19:02:10 -0700407 return false;
408 }
409
410 if (input->invariant != output->invariant) {
Ian Romanickf36460e2010-06-23 12:07:22 -0700411 linker_error_printf(prog,
412 "%s shader output `%s' %s invariant qualifier, "
413 "but %s shader input %s invariant qualifier\n",
414 producer_stage,
415 output->name,
416 (output->invariant) ? "has" : "lacks",
417 consumer_stage,
418 (input->invariant) ? "has" : "lacks");
Ian Romanick37101922010-06-18 19:02:10 -0700419 return false;
420 }
421
422 if (input->interpolation != output->interpolation) {
Ian Romanickf36460e2010-06-23 12:07:22 -0700423 linker_error_printf(prog,
424 "%s shader output `%s' specifies %s "
425 "interpolation qualifier, "
426 "but %s shader input specifies %s "
427 "interpolation qualifier\n",
428 producer_stage,
429 output->name,
430 output->interpolation_string(),
431 consumer_stage,
432 input->interpolation_string());
Ian Romanick37101922010-06-18 19:02:10 -0700433 return false;
434 }
435 }
436 }
437
438 return true;
439}
440
441
Ian Romanick3fb87872010-07-09 14:09:34 -0700442/**
443 * Populates a shaders symbol table with all global declarations
444 */
445static void
446populate_symbol_table(gl_shader *sh)
447{
448 sh->symbols = new(sh) glsl_symbol_table;
449
450 foreach_list(node, sh->ir) {
451 ir_instruction *const inst = (ir_instruction *) node;
452 ir_variable *var;
453 ir_function *func;
454
455 if ((func = inst->as_function()) != NULL) {
456 sh->symbols->add_function(func->name, func);
457 } else if ((var = inst->as_variable()) != NULL) {
458 sh->symbols->add_variable(var->name, var);
459 }
460 }
461}
462
463
464/**
465 * Combine a group of shaders for a single stage to generate a linked shader
466 *
467 * \note
468 * If this function is supplied a single shader, it is cloned, and the new
469 * shader is returned.
470 */
471static struct gl_shader *
472link_intrastage_shaders(struct gl_shader_program *prog,
473 struct gl_shader **shader_list,
474 unsigned num_shaders)
475{
476 (void) prog;
477 assert(num_shaders == 1);
478
479 gl_shader *const linked = _mesa_new_shader(NULL, 0, shader_list[0]->Type);
480 linked->ir = new(linked) exec_list;
481 clone_ir_list(linked->ir, shader_list[0]->ir);
482
483 populate_symbol_table(linked);
484
485 return linked;
486}
487
488
Ian Romanick019a59b2010-06-21 16:10:42 -0700489struct uniform_node {
490 exec_node link;
491 struct gl_uniform *u;
492 unsigned slots;
493};
494
Ian Romanickabee16e2010-06-21 16:16:05 -0700495void
Eric Anholt849e1812010-06-30 11:49:17 -0700496assign_uniform_locations(struct gl_shader_program *prog)
Ian Romanick019a59b2010-06-21 16:10:42 -0700497{
498 /* */
499 exec_list uniforms;
500 unsigned total_uniforms = 0;
501 hash_table *ht = hash_table_ctor(32, hash_table_string_hash,
502 hash_table_string_compare);
503
Ian Romanickabee16e2010-06-21 16:16:05 -0700504 for (unsigned i = 0; i < prog->_NumLinkedShaders; i++) {
Ian Romanick019a59b2010-06-21 16:10:42 -0700505 unsigned next_position = 0;
506
Eric Anholt16b68b12010-06-30 11:05:43 -0700507 foreach_list(node, prog->_LinkedShaders[i]->ir) {
Ian Romanick019a59b2010-06-21 16:10:42 -0700508 ir_variable *const var = ((ir_instruction *) node)->as_variable();
509
510 if ((var == NULL) || (var->mode != ir_var_uniform))
511 continue;
512
513 const unsigned vec4_slots = (var->component_slots() + 3) / 4;
514 assert(vec4_slots != 0);
515
516 uniform_node *n = (uniform_node *) hash_table_find(ht, var->name);
517 if (n == NULL) {
518 n = (uniform_node *) calloc(1, sizeof(struct uniform_node));
519 n->u = (gl_uniform *) calloc(vec4_slots, sizeof(struct gl_uniform));
520 n->slots = vec4_slots;
521
522 n->u[0].Name = strdup(var->name);
523 for (unsigned j = 1; j < vec4_slots; j++)
524 n->u[j].Name = n->u[0].Name;
525
526 hash_table_insert(ht, n, n->u[0].Name);
527 uniforms.push_tail(& n->link);
528 total_uniforms += vec4_slots;
529 }
530
531 if (var->constant_value != NULL)
532 for (unsigned j = 0; j < vec4_slots; j++)
533 n->u[j].Initialized = true;
534
535 var->location = next_position;
536
537 for (unsigned j = 0; j < vec4_slots; j++) {
Ian Romanickabee16e2010-06-21 16:16:05 -0700538 switch (prog->_LinkedShaders[i]->Type) {
Ian Romanick019a59b2010-06-21 16:10:42 -0700539 case GL_VERTEX_SHADER:
540 n->u[j].VertPos = next_position;
541 break;
542 case GL_FRAGMENT_SHADER:
543 n->u[j].FragPos = next_position;
544 break;
545 case GL_GEOMETRY_SHADER:
546 /* FINISHME: Support geometry shaders. */
Ian Romanickabee16e2010-06-21 16:16:05 -0700547 assert(prog->_LinkedShaders[i]->Type != GL_GEOMETRY_SHADER);
Ian Romanick019a59b2010-06-21 16:10:42 -0700548 break;
549 }
550
551 next_position++;
552 }
553 }
554 }
555
556 gl_uniform_list *ul = (gl_uniform_list *)
557 calloc(1, sizeof(gl_uniform_list));
558
559 ul->Size = total_uniforms;
560 ul->NumUniforms = total_uniforms;
561 ul->Uniforms = (gl_uniform *) calloc(total_uniforms, sizeof(gl_uniform));
562
563 unsigned idx = 0;
564 uniform_node *next;
565 for (uniform_node *node = (uniform_node *) uniforms.head
566 ; node->link.next != NULL
567 ; node = next) {
568 next = (uniform_node *) node->link.next;
569
570 node->link.remove();
571 memcpy(&ul->Uniforms[idx], node->u, sizeof(gl_uniform) * node->slots);
572 idx += node->slots;
573
574 free(node->u);
575 free(node);
576 }
577
578 hash_table_dtor(ht);
579
Ian Romanickabee16e2010-06-21 16:16:05 -0700580 prog->Uniforms = ul;
Ian Romanick019a59b2010-06-21 16:10:42 -0700581}
582
583
Ian Romanick69846702010-06-22 17:29:19 -0700584/**
585 * Find a contiguous set of available bits in a bitmask
586 *
587 * \param used_mask Bits representing used (1) and unused (0) locations
588 * \param needed_count Number of contiguous bits needed.
589 *
590 * \return
591 * Base location of the available bits on success or -1 on failure.
592 */
593int
594find_available_slots(unsigned used_mask, unsigned needed_count)
595{
596 unsigned needed_mask = (1 << needed_count) - 1;
597 const int max_bit_to_test = (8 * sizeof(used_mask)) - needed_count;
598
599 /* The comparison to 32 is redundant, but without it GCC emits "warning:
600 * cannot optimize possibly infinite loops" for the loop below.
601 */
602 if ((needed_count == 0) || (max_bit_to_test < 0) || (max_bit_to_test > 32))
603 return -1;
604
605 for (int i = 0; i <= max_bit_to_test; i++) {
606 if ((needed_mask & ~used_mask) == needed_mask)
607 return i;
608
609 needed_mask <<= 1;
610 }
611
612 return -1;
613}
614
615
616bool
Eric Anholt849e1812010-06-30 11:49:17 -0700617assign_attribute_locations(gl_shader_program *prog, unsigned max_attribute_index)
Ian Romanick0ad22cd2010-06-21 17:18:31 -0700618{
Ian Romanick9342d262010-06-22 17:41:37 -0700619 /* Mark invalid attribute locations as being used.
620 */
621 unsigned used_locations = (max_attribute_index >= 32)
622 ? ~0 : ~((1 << max_attribute_index) - 1);
Ian Romanick0ad22cd2010-06-21 17:18:31 -0700623
Eric Anholt16b68b12010-06-30 11:05:43 -0700624 gl_shader *const sh = prog->_LinkedShaders[0];
Ian Romanick0ad22cd2010-06-21 17:18:31 -0700625 assert(sh->Type == GL_VERTEX_SHADER);
626
Ian Romanick69846702010-06-22 17:29:19 -0700627 /* Operate in a total of four passes.
Ian Romanick0ad22cd2010-06-21 17:18:31 -0700628 *
629 * 1. Invalidate the location assignments for all vertex shader inputs.
630 *
631 * 2. Assign locations for inputs that have user-defined (via
632 * glBindVertexAttribLocation) locatoins.
633 *
Ian Romanick69846702010-06-22 17:29:19 -0700634 * 3. Sort the attributes without assigned locations by number of slots
635 * required in decreasing order. Fragmentation caused by attribute
636 * locations assigned by the application may prevent large attributes
637 * from having enough contiguous space.
638 *
639 * 4. Assign locations to any inputs without assigned locations.
Ian Romanick0ad22cd2010-06-21 17:18:31 -0700640 */
641
642 invalidate_variable_locations(sh, ir_var_in, VERT_ATTRIB_GENERIC0);
643
Ian Romanick553dcdc2010-06-23 12:14:02 -0700644 if (prog->Attributes != NULL) {
645 for (unsigned i = 0; i < prog->Attributes->NumParameters; i++) {
Ian Romanick0ad22cd2010-06-21 17:18:31 -0700646 ir_variable *const var =
Ian Romanick553dcdc2010-06-23 12:14:02 -0700647 sh->symbols->get_variable(prog->Attributes->Parameters[i].Name);
Ian Romanick0ad22cd2010-06-21 17:18:31 -0700648
Ian Romanick69846702010-06-22 17:29:19 -0700649 /* Note: attributes that occupy multiple slots, such as arrays or
650 * matrices, may appear in the attrib array multiple times.
651 */
652 if ((var == NULL) || (var->location != -1))
Ian Romanick0ad22cd2010-06-21 17:18:31 -0700653 continue;
654
Ian Romanick69846702010-06-22 17:29:19 -0700655 /* From page 61 of the OpenGL 4.0 spec:
656 *
657 * "LinkProgram will fail if the attribute bindings assigned by
658 * BindAttribLocation do not leave not enough space to assign a
659 * location for an active matrix attribute or an active attribute
660 * array, both of which require multiple contiguous generic
661 * attributes."
662 *
663 * Previous versions of the spec contain similar language but omit the
664 * bit about attribute arrays.
665 *
666 * Page 61 of the OpenGL 4.0 spec also says:
667 *
668 * "It is possible for an application to bind more than one
669 * attribute name to the same location. This is referred to as
670 * aliasing. This will only work if only one of the aliased
671 * attributes is active in the executable program, or if no path
672 * through the shader consumes more than one attribute of a set
673 * of attributes aliased to the same location. A link error can
674 * occur if the linker determines that every path through the
675 * shader consumes multiple aliased attributes, but
676 * implementations are not required to generate an error in this
677 * case."
678 *
679 * These two paragraphs are either somewhat contradictory, or I don't
680 * fully understand one or both of them.
681 */
682 /* FINISHME: The code as currently written does not support attribute
683 * FINISHME: location aliasing (see comment above).
684 */
Ian Romanick553dcdc2010-06-23 12:14:02 -0700685 const int attr = prog->Attributes->Parameters[i].StateIndexes[0];
Ian Romanick69846702010-06-22 17:29:19 -0700686 const unsigned slots = count_attribute_slots(var->type);
687
688 /* Mask representing the contiguous slots that will be used by this
689 * attribute.
690 */
691 const unsigned use_mask = (1 << slots) - 1;
692
693 /* Generate a link error if the set of bits requested for this
694 * attribute overlaps any previously allocated bits.
695 */
696 if ((~(use_mask << attr) & used_locations) != used_locations) {
Ian Romanick553dcdc2010-06-23 12:14:02 -0700697 linker_error_printf(prog,
698 "insufficient contiguous attribute locations "
699 "available for vertex shader input `%s'",
700 var->name);
Ian Romanick69846702010-06-22 17:29:19 -0700701 return false;
702 }
Ian Romanick0ad22cd2010-06-21 17:18:31 -0700703
704 var->location = VERT_ATTRIB_GENERIC0 + attr;
Ian Romanick69846702010-06-22 17:29:19 -0700705 used_locations |= (use_mask << attr);
Ian Romanick0ad22cd2010-06-21 17:18:31 -0700706 }
707 }
708
Ian Romanick69846702010-06-22 17:29:19 -0700709 /* Temporary storage for the set of attributes that need locations assigned.
710 */
711 struct temp_attr {
712 unsigned slots;
713 ir_variable *var;
714
715 /* Used below in the call to qsort. */
716 static int compare(const void *a, const void *b)
717 {
718 const temp_attr *const l = (const temp_attr *) a;
719 const temp_attr *const r = (const temp_attr *) b;
720
721 /* Reversed because we want a descending order sort below. */
722 return r->slots - l->slots;
723 }
724 } to_assign[16];
725
726 unsigned num_attr = 0;
727
Eric Anholt16b68b12010-06-30 11:05:43 -0700728 foreach_list(node, sh->ir) {
Ian Romanick0ad22cd2010-06-21 17:18:31 -0700729 ir_variable *const var = ((ir_instruction *) node)->as_variable();
730
731 if ((var == NULL) || (var->mode != ir_var_in))
732 continue;
733
734 /* The location was explicitly assigned, nothing to do here.
735 */
736 if (var->location != -1)
737 continue;
738
Ian Romanick69846702010-06-22 17:29:19 -0700739 to_assign[num_attr].slots = count_attribute_slots(var->type);
740 to_assign[num_attr].var = var;
741 num_attr++;
Ian Romanick0ad22cd2010-06-21 17:18:31 -0700742 }
Ian Romanick69846702010-06-22 17:29:19 -0700743
744 /* If all of the attributes were assigned locations by the application (or
745 * are built-in attributes with fixed locations), return early. This should
746 * be the common case.
747 */
748 if (num_attr == 0)
749 return true;
750
751 qsort(to_assign, num_attr, sizeof(to_assign[0]), temp_attr::compare);
752
Ian Romanick982e3792010-06-29 18:58:20 -0700753 /* VERT_ATTRIB_GENERIC0 is a psdueo-alias for VERT_ATTRIB_POS. It can only
754 * be explicitly assigned by via glBindAttribLocation. Mark it as reserved
755 * to prevent it from being automatically allocated below.
756 */
Ian Romanick35c89202010-07-07 16:28:39 -0700757 used_locations |= (1 << 0);
Ian Romanick982e3792010-06-29 18:58:20 -0700758
Ian Romanick69846702010-06-22 17:29:19 -0700759 for (unsigned i = 0; i < num_attr; i++) {
760 /* Mask representing the contiguous slots that will be used by this
761 * attribute.
762 */
763 const unsigned use_mask = (1 << to_assign[i].slots) - 1;
764
765 int location = find_available_slots(used_locations, to_assign[i].slots);
766
767 if (location < 0) {
Ian Romanick553dcdc2010-06-23 12:14:02 -0700768 linker_error_printf(prog,
769 "insufficient contiguous attribute locations "
770 "available for vertex shader input `%s'",
771 to_assign[i].var->name);
Ian Romanick69846702010-06-22 17:29:19 -0700772 return false;
773 }
774
775 to_assign[i].var->location = VERT_ATTRIB_GENERIC0 + location;
776 used_locations |= (use_mask << location);
777 }
778
779 return true;
Ian Romanick0ad22cd2010-06-21 17:18:31 -0700780}
781
782
783void
Eric Anholt16b68b12010-06-30 11:05:43 -0700784assign_varying_locations(gl_shader *producer, gl_shader *consumer)
Ian Romanick0e59b262010-06-23 11:23:01 -0700785{
786 /* FINISHME: Set dynamically when geometry shader support is added. */
787 unsigned output_index = VERT_RESULT_VAR0;
788 unsigned input_index = FRAG_ATTRIB_VAR0;
789
790 /* Operate in a total of three passes.
791 *
792 * 1. Assign locations for any matching inputs and outputs.
793 *
794 * 2. Mark output variables in the producer that do not have locations as
795 * not being outputs. This lets the optimizer eliminate them.
796 *
797 * 3. Mark input variables in the consumer that do not have locations as
798 * not being inputs. This lets the optimizer eliminate them.
799 */
800
801 invalidate_variable_locations(producer, ir_var_out, VERT_RESULT_VAR0);
802 invalidate_variable_locations(consumer, ir_var_in, FRAG_ATTRIB_VAR0);
803
Eric Anholt16b68b12010-06-30 11:05:43 -0700804 foreach_list(node, producer->ir) {
Ian Romanick0e59b262010-06-23 11:23:01 -0700805 ir_variable *const output_var = ((ir_instruction *) node)->as_variable();
806
807 if ((output_var == NULL) || (output_var->mode != ir_var_out)
808 || (output_var->location != -1))
809 continue;
810
811 ir_variable *const input_var =
812 consumer->symbols->get_variable(output_var->name);
813
814 if ((input_var == NULL) || (input_var->mode != ir_var_in))
815 continue;
816
817 assert(input_var->location == -1);
818
819 /* FINISHME: Location assignment will need some changes when arrays,
820 * FINISHME: matrices, and structures are allowed as shader inputs /
821 * FINISHME: outputs.
822 */
823 output_var->location = output_index;
824 input_var->location = input_index;
825
826 output_index++;
827 input_index++;
828 }
829
Eric Anholt16b68b12010-06-30 11:05:43 -0700830 foreach_list(node, producer->ir) {
Ian Romanick0e59b262010-06-23 11:23:01 -0700831 ir_variable *const var = ((ir_instruction *) node)->as_variable();
832
833 if ((var == NULL) || (var->mode != ir_var_out))
834 continue;
835
836 /* An 'out' variable is only really a shader output if its value is read
837 * by the following stage.
838 */
839 var->shader_out = (var->location != -1);
840 }
841
Eric Anholt16b68b12010-06-30 11:05:43 -0700842 foreach_list(node, consumer->ir) {
Ian Romanick0e59b262010-06-23 11:23:01 -0700843 ir_variable *const var = ((ir_instruction *) node)->as_variable();
844
845 if ((var == NULL) || (var->mode != ir_var_in))
846 continue;
847
848 /* An 'in' variable is only really a shader input if its value is written
849 * by the previous stage.
850 */
851 var->shader_in = (var->location != -1);
852 }
853}
854
855
856void
Eric Anholt849e1812010-06-30 11:49:17 -0700857link_shaders(struct gl_shader_program *prog)
Ian Romanick832dfa52010-06-17 15:04:20 -0700858{
859 prog->LinkStatus = false;
860 prog->Validated = false;
861 prog->_Used = false;
862
Ian Romanickf36460e2010-06-23 12:07:22 -0700863 if (prog->InfoLog != NULL)
864 talloc_free(prog->InfoLog);
865
866 prog->InfoLog = talloc_strdup(NULL, "");
867
Ian Romanick832dfa52010-06-17 15:04:20 -0700868 /* Separate the shaders into groups based on their type.
869 */
Eric Anholt16b68b12010-06-30 11:05:43 -0700870 struct gl_shader **vert_shader_list;
Ian Romanick832dfa52010-06-17 15:04:20 -0700871 unsigned num_vert_shaders = 0;
Eric Anholt16b68b12010-06-30 11:05:43 -0700872 struct gl_shader **frag_shader_list;
Ian Romanick832dfa52010-06-17 15:04:20 -0700873 unsigned num_frag_shaders = 0;
874
Eric Anholt16b68b12010-06-30 11:05:43 -0700875 vert_shader_list = (struct gl_shader **)
876 calloc(2 * prog->NumShaders, sizeof(struct gl_shader *));
Ian Romanick832dfa52010-06-17 15:04:20 -0700877 frag_shader_list = &vert_shader_list[prog->NumShaders];
878
879 for (unsigned i = 0; i < prog->NumShaders; i++) {
880 switch (prog->Shaders[i]->Type) {
881 case GL_VERTEX_SHADER:
882 vert_shader_list[num_vert_shaders] = prog->Shaders[i];
883 num_vert_shaders++;
884 break;
885 case GL_FRAGMENT_SHADER:
886 frag_shader_list[num_frag_shaders] = prog->Shaders[i];
887 num_frag_shaders++;
888 break;
889 case GL_GEOMETRY_SHADER:
890 /* FINISHME: Support geometry shaders. */
891 assert(prog->Shaders[i]->Type != GL_GEOMETRY_SHADER);
892 break;
893 }
894 }
895
896 /* FINISHME: Implement intra-stage linking. */
Ian Romanickabee16e2010-06-21 16:16:05 -0700897 prog->_NumLinkedShaders = 0;
Ian Romanickcc22c5a2010-06-18 17:13:42 -0700898 if (num_vert_shaders > 0) {
Ian Romanick3fb87872010-07-09 14:09:34 -0700899 gl_shader *const sh =
900 link_intrastage_shaders(prog, vert_shader_list, num_vert_shaders);
901
902 if (sh == NULL)
903 goto done;
904
905 if (!validate_vertex_shader_executable(prog, sh))
906 goto done;
907
908 prog->_LinkedShaders[prog->_NumLinkedShaders] = sh;
Ian Romanickabee16e2010-06-21 16:16:05 -0700909 prog->_NumLinkedShaders++;
Ian Romanickcc22c5a2010-06-18 17:13:42 -0700910 }
911
912 if (num_frag_shaders > 0) {
Ian Romanick3fb87872010-07-09 14:09:34 -0700913 gl_shader *const sh =
914 link_intrastage_shaders(prog, frag_shader_list, num_frag_shaders);
915
916 if (sh == NULL)
917 goto done;
918
919 if (!validate_fragment_shader_executable(prog, sh))
920 goto done;
921
922 prog->_LinkedShaders[prog->_NumLinkedShaders] = sh;
Ian Romanickabee16e2010-06-21 16:16:05 -0700923 prog->_NumLinkedShaders++;
Ian Romanickcc22c5a2010-06-18 17:13:42 -0700924 }
925
Ian Romanick3ed850e2010-06-23 12:18:21 -0700926 /* Here begins the inter-stage linking phase. Some initial validation is
927 * performed, then locations are assigned for uniforms, attributes, and
928 * varyings.
929 */
Ian Romanicked1fe3d2010-06-23 12:09:14 -0700930 if (cross_validate_uniforms(prog)) {
Ian Romanick37101922010-06-18 19:02:10 -0700931 /* Validate the inputs of each stage with the output of the preceeding
932 * stage.
933 */
Ian Romanickabee16e2010-06-21 16:16:05 -0700934 for (unsigned i = 1; i < prog->_NumLinkedShaders; i++) {
Ian Romanickf36460e2010-06-23 12:07:22 -0700935 if (!cross_validate_outputs_to_inputs(prog,
936 prog->_LinkedShaders[i - 1],
Ian Romanickabee16e2010-06-21 16:16:05 -0700937 prog->_LinkedShaders[i]))
Ian Romanick37101922010-06-18 19:02:10 -0700938 goto done;
939 }
940
Ian Romanickcc22c5a2010-06-18 17:13:42 -0700941 prog->LinkStatus = true;
Ian Romanick37101922010-06-18 19:02:10 -0700942 }
Ian Romanick832dfa52010-06-17 15:04:20 -0700943
Ian Romanick13e10e42010-06-21 12:03:24 -0700944 /* FINISHME: Perform whole-program optimization here. */
945
Ian Romanickabee16e2010-06-21 16:16:05 -0700946 assign_uniform_locations(prog);
Ian Romanick13e10e42010-06-21 12:03:24 -0700947
Ian Romanick0ad22cd2010-06-21 17:18:31 -0700948 if (prog->_LinkedShaders[0]->Type == GL_VERTEX_SHADER)
Ian Romanick9342d262010-06-22 17:41:37 -0700949 /* FINISHME: The value of the max_attribute_index parameter is
950 * FINISHME: implementation dependent based on the value of
951 * FINISHME: GL_MAX_VERTEX_ATTRIBS. GL_MAX_VERTEX_ATTRIBS must be
952 * FINISHME: at least 16, so hardcode 16 for now.
953 */
Ian Romanick553dcdc2010-06-23 12:14:02 -0700954 if (!assign_attribute_locations(prog, 16))
Ian Romanick69846702010-06-22 17:29:19 -0700955 goto done;
Ian Romanick13e10e42010-06-21 12:03:24 -0700956
Ian Romanick0e59b262010-06-23 11:23:01 -0700957 for (unsigned i = 1; i < prog->_NumLinkedShaders; i++)
958 assign_varying_locations(prog->_LinkedShaders[i - 1],
959 prog->_LinkedShaders[i]);
Ian Romanick13e10e42010-06-21 12:03:24 -0700960
961 /* FINISHME: Assign fragment shader output locations. */
962
Ian Romanick832dfa52010-06-17 15:04:20 -0700963done:
964 free(vert_shader_list);
965}