blob: 3d8f24bb4443b3456df449f2081034f174fc8cbe [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{
Ian Romanick13f782c2010-06-29 18:53:38 -0700476 /* Check that global variables defined in multiple shaders are consistent.
477 */
478 if (!cross_validate_globals(prog, shader_list, num_shaders, false))
479 return NULL;
480
481 /* Check that there is only a single definition of each function signature
482 * across all shaders.
483 */
484 for (unsigned i = 0; i < (num_shaders - 1); i++) {
485 foreach_list(node, shader_list[i]->ir) {
486 ir_function *const f = ((ir_instruction *) node)->as_function();
487
488 if (f == NULL)
489 continue;
490
491 for (unsigned j = i + 1; j < num_shaders; j++) {
492 ir_function *const other =
493 shader_list[j]->symbols->get_function(f->name);
494
495 /* If the other shader has no function (and therefore no function
496 * signatures) with the same name, skip to the next shader.
497 */
498 if (other == NULL)
499 continue;
500
501 foreach_iter (exec_list_iterator, iter, *f) {
502 ir_function_signature *sig =
503 (ir_function_signature *) iter.get();
504
505 if (!sig->is_defined || sig->is_built_in)
506 continue;
507
508 ir_function_signature *other_sig =
509 other->exact_matching_signature(& sig->parameters);
510
511 if ((other_sig != NULL) && other_sig->is_defined
512 && !other_sig->is_built_in) {
513 linker_error_printf(prog,
514 "function `%s' is multiply defined",
515 f->name);
516 return NULL;
517 }
518 }
519 }
520 }
521 }
522
523 /* Find the shader that defines main, and make a clone of it.
524 *
525 * Starting with the clone, search for undefined references. If one is
526 * found, find the shader that defines it. Clone the reference and add
527 * it to the shader. Repeat until there are no undefined references or
528 * until a reference cannot be resolved.
529 */
530
531
532 /* Resolve initializers for global variables in the linked shader.
533 */
Ian Romanick3fb87872010-07-09 14:09:34 -0700534
535 gl_shader *const linked = _mesa_new_shader(NULL, 0, shader_list[0]->Type);
536 linked->ir = new(linked) exec_list;
537 clone_ir_list(linked->ir, shader_list[0]->ir);
538
539 populate_symbol_table(linked);
540
541 return linked;
542}
543
544
Ian Romanick019a59b2010-06-21 16:10:42 -0700545struct uniform_node {
546 exec_node link;
547 struct gl_uniform *u;
548 unsigned slots;
549};
550
Ian Romanickabee16e2010-06-21 16:16:05 -0700551void
Eric Anholt849e1812010-06-30 11:49:17 -0700552assign_uniform_locations(struct gl_shader_program *prog)
Ian Romanick019a59b2010-06-21 16:10:42 -0700553{
554 /* */
555 exec_list uniforms;
556 unsigned total_uniforms = 0;
557 hash_table *ht = hash_table_ctor(32, hash_table_string_hash,
558 hash_table_string_compare);
559
Ian Romanickabee16e2010-06-21 16:16:05 -0700560 for (unsigned i = 0; i < prog->_NumLinkedShaders; i++) {
Ian Romanick019a59b2010-06-21 16:10:42 -0700561 unsigned next_position = 0;
562
Eric Anholt16b68b12010-06-30 11:05:43 -0700563 foreach_list(node, prog->_LinkedShaders[i]->ir) {
Ian Romanick019a59b2010-06-21 16:10:42 -0700564 ir_variable *const var = ((ir_instruction *) node)->as_variable();
565
566 if ((var == NULL) || (var->mode != ir_var_uniform))
567 continue;
568
569 const unsigned vec4_slots = (var->component_slots() + 3) / 4;
570 assert(vec4_slots != 0);
571
572 uniform_node *n = (uniform_node *) hash_table_find(ht, var->name);
573 if (n == NULL) {
574 n = (uniform_node *) calloc(1, sizeof(struct uniform_node));
575 n->u = (gl_uniform *) calloc(vec4_slots, sizeof(struct gl_uniform));
576 n->slots = vec4_slots;
577
578 n->u[0].Name = strdup(var->name);
579 for (unsigned j = 1; j < vec4_slots; j++)
580 n->u[j].Name = n->u[0].Name;
581
582 hash_table_insert(ht, n, n->u[0].Name);
583 uniforms.push_tail(& n->link);
584 total_uniforms += vec4_slots;
585 }
586
587 if (var->constant_value != NULL)
588 for (unsigned j = 0; j < vec4_slots; j++)
589 n->u[j].Initialized = true;
590
591 var->location = next_position;
592
593 for (unsigned j = 0; j < vec4_slots; j++) {
Ian Romanickabee16e2010-06-21 16:16:05 -0700594 switch (prog->_LinkedShaders[i]->Type) {
Ian Romanick019a59b2010-06-21 16:10:42 -0700595 case GL_VERTEX_SHADER:
596 n->u[j].VertPos = next_position;
597 break;
598 case GL_FRAGMENT_SHADER:
599 n->u[j].FragPos = next_position;
600 break;
601 case GL_GEOMETRY_SHADER:
602 /* FINISHME: Support geometry shaders. */
Ian Romanickabee16e2010-06-21 16:16:05 -0700603 assert(prog->_LinkedShaders[i]->Type != GL_GEOMETRY_SHADER);
Ian Romanick019a59b2010-06-21 16:10:42 -0700604 break;
605 }
606
607 next_position++;
608 }
609 }
610 }
611
612 gl_uniform_list *ul = (gl_uniform_list *)
613 calloc(1, sizeof(gl_uniform_list));
614
615 ul->Size = total_uniforms;
616 ul->NumUniforms = total_uniforms;
617 ul->Uniforms = (gl_uniform *) calloc(total_uniforms, sizeof(gl_uniform));
618
619 unsigned idx = 0;
620 uniform_node *next;
621 for (uniform_node *node = (uniform_node *) uniforms.head
622 ; node->link.next != NULL
623 ; node = next) {
624 next = (uniform_node *) node->link.next;
625
626 node->link.remove();
627 memcpy(&ul->Uniforms[idx], node->u, sizeof(gl_uniform) * node->slots);
628 idx += node->slots;
629
630 free(node->u);
631 free(node);
632 }
633
634 hash_table_dtor(ht);
635
Ian Romanickabee16e2010-06-21 16:16:05 -0700636 prog->Uniforms = ul;
Ian Romanick019a59b2010-06-21 16:10:42 -0700637}
638
639
Ian Romanick69846702010-06-22 17:29:19 -0700640/**
641 * Find a contiguous set of available bits in a bitmask
642 *
643 * \param used_mask Bits representing used (1) and unused (0) locations
644 * \param needed_count Number of contiguous bits needed.
645 *
646 * \return
647 * Base location of the available bits on success or -1 on failure.
648 */
649int
650find_available_slots(unsigned used_mask, unsigned needed_count)
651{
652 unsigned needed_mask = (1 << needed_count) - 1;
653 const int max_bit_to_test = (8 * sizeof(used_mask)) - needed_count;
654
655 /* The comparison to 32 is redundant, but without it GCC emits "warning:
656 * cannot optimize possibly infinite loops" for the loop below.
657 */
658 if ((needed_count == 0) || (max_bit_to_test < 0) || (max_bit_to_test > 32))
659 return -1;
660
661 for (int i = 0; i <= max_bit_to_test; i++) {
662 if ((needed_mask & ~used_mask) == needed_mask)
663 return i;
664
665 needed_mask <<= 1;
666 }
667
668 return -1;
669}
670
671
672bool
Eric Anholt849e1812010-06-30 11:49:17 -0700673assign_attribute_locations(gl_shader_program *prog, unsigned max_attribute_index)
Ian Romanick0ad22cd2010-06-21 17:18:31 -0700674{
Ian Romanick9342d262010-06-22 17:41:37 -0700675 /* Mark invalid attribute locations as being used.
676 */
677 unsigned used_locations = (max_attribute_index >= 32)
678 ? ~0 : ~((1 << max_attribute_index) - 1);
Ian Romanick0ad22cd2010-06-21 17:18:31 -0700679
Eric Anholt16b68b12010-06-30 11:05:43 -0700680 gl_shader *const sh = prog->_LinkedShaders[0];
Ian Romanick0ad22cd2010-06-21 17:18:31 -0700681 assert(sh->Type == GL_VERTEX_SHADER);
682
Ian Romanick69846702010-06-22 17:29:19 -0700683 /* Operate in a total of four passes.
Ian Romanick0ad22cd2010-06-21 17:18:31 -0700684 *
685 * 1. Invalidate the location assignments for all vertex shader inputs.
686 *
687 * 2. Assign locations for inputs that have user-defined (via
688 * glBindVertexAttribLocation) locatoins.
689 *
Ian Romanick69846702010-06-22 17:29:19 -0700690 * 3. Sort the attributes without assigned locations by number of slots
691 * required in decreasing order. Fragmentation caused by attribute
692 * locations assigned by the application may prevent large attributes
693 * from having enough contiguous space.
694 *
695 * 4. Assign locations to any inputs without assigned locations.
Ian Romanick0ad22cd2010-06-21 17:18:31 -0700696 */
697
698 invalidate_variable_locations(sh, ir_var_in, VERT_ATTRIB_GENERIC0);
699
Ian Romanick553dcdc2010-06-23 12:14:02 -0700700 if (prog->Attributes != NULL) {
701 for (unsigned i = 0; i < prog->Attributes->NumParameters; i++) {
Ian Romanick0ad22cd2010-06-21 17:18:31 -0700702 ir_variable *const var =
Ian Romanick553dcdc2010-06-23 12:14:02 -0700703 sh->symbols->get_variable(prog->Attributes->Parameters[i].Name);
Ian Romanick0ad22cd2010-06-21 17:18:31 -0700704
Ian Romanick69846702010-06-22 17:29:19 -0700705 /* Note: attributes that occupy multiple slots, such as arrays or
706 * matrices, may appear in the attrib array multiple times.
707 */
708 if ((var == NULL) || (var->location != -1))
Ian Romanick0ad22cd2010-06-21 17:18:31 -0700709 continue;
710
Ian Romanick69846702010-06-22 17:29:19 -0700711 /* From page 61 of the OpenGL 4.0 spec:
712 *
713 * "LinkProgram will fail if the attribute bindings assigned by
714 * BindAttribLocation do not leave not enough space to assign a
715 * location for an active matrix attribute or an active attribute
716 * array, both of which require multiple contiguous generic
717 * attributes."
718 *
719 * Previous versions of the spec contain similar language but omit the
720 * bit about attribute arrays.
721 *
722 * Page 61 of the OpenGL 4.0 spec also says:
723 *
724 * "It is possible for an application to bind more than one
725 * attribute name to the same location. This is referred to as
726 * aliasing. This will only work if only one of the aliased
727 * attributes is active in the executable program, or if no path
728 * through the shader consumes more than one attribute of a set
729 * of attributes aliased to the same location. A link error can
730 * occur if the linker determines that every path through the
731 * shader consumes multiple aliased attributes, but
732 * implementations are not required to generate an error in this
733 * case."
734 *
735 * These two paragraphs are either somewhat contradictory, or I don't
736 * fully understand one or both of them.
737 */
738 /* FINISHME: The code as currently written does not support attribute
739 * FINISHME: location aliasing (see comment above).
740 */
Ian Romanick553dcdc2010-06-23 12:14:02 -0700741 const int attr = prog->Attributes->Parameters[i].StateIndexes[0];
Ian Romanick69846702010-06-22 17:29:19 -0700742 const unsigned slots = count_attribute_slots(var->type);
743
744 /* Mask representing the contiguous slots that will be used by this
745 * attribute.
746 */
747 const unsigned use_mask = (1 << slots) - 1;
748
749 /* Generate a link error if the set of bits requested for this
750 * attribute overlaps any previously allocated bits.
751 */
752 if ((~(use_mask << attr) & used_locations) != used_locations) {
Ian Romanick553dcdc2010-06-23 12:14:02 -0700753 linker_error_printf(prog,
754 "insufficient contiguous attribute locations "
755 "available for vertex shader input `%s'",
756 var->name);
Ian Romanick69846702010-06-22 17:29:19 -0700757 return false;
758 }
Ian Romanick0ad22cd2010-06-21 17:18:31 -0700759
760 var->location = VERT_ATTRIB_GENERIC0 + attr;
Ian Romanick69846702010-06-22 17:29:19 -0700761 used_locations |= (use_mask << attr);
Ian Romanick0ad22cd2010-06-21 17:18:31 -0700762 }
763 }
764
Ian Romanick69846702010-06-22 17:29:19 -0700765 /* Temporary storage for the set of attributes that need locations assigned.
766 */
767 struct temp_attr {
768 unsigned slots;
769 ir_variable *var;
770
771 /* Used below in the call to qsort. */
772 static int compare(const void *a, const void *b)
773 {
774 const temp_attr *const l = (const temp_attr *) a;
775 const temp_attr *const r = (const temp_attr *) b;
776
777 /* Reversed because we want a descending order sort below. */
778 return r->slots - l->slots;
779 }
780 } to_assign[16];
781
782 unsigned num_attr = 0;
783
Eric Anholt16b68b12010-06-30 11:05:43 -0700784 foreach_list(node, sh->ir) {
Ian Romanick0ad22cd2010-06-21 17:18:31 -0700785 ir_variable *const var = ((ir_instruction *) node)->as_variable();
786
787 if ((var == NULL) || (var->mode != ir_var_in))
788 continue;
789
790 /* The location was explicitly assigned, nothing to do here.
791 */
792 if (var->location != -1)
793 continue;
794
Ian Romanick69846702010-06-22 17:29:19 -0700795 to_assign[num_attr].slots = count_attribute_slots(var->type);
796 to_assign[num_attr].var = var;
797 num_attr++;
Ian Romanick0ad22cd2010-06-21 17:18:31 -0700798 }
Ian Romanick69846702010-06-22 17:29:19 -0700799
800 /* If all of the attributes were assigned locations by the application (or
801 * are built-in attributes with fixed locations), return early. This should
802 * be the common case.
803 */
804 if (num_attr == 0)
805 return true;
806
807 qsort(to_assign, num_attr, sizeof(to_assign[0]), temp_attr::compare);
808
Ian Romanick982e3792010-06-29 18:58:20 -0700809 /* VERT_ATTRIB_GENERIC0 is a psdueo-alias for VERT_ATTRIB_POS. It can only
810 * be explicitly assigned by via glBindAttribLocation. Mark it as reserved
811 * to prevent it from being automatically allocated below.
812 */
Ian Romanick35c89202010-07-07 16:28:39 -0700813 used_locations |= (1 << 0);
Ian Romanick982e3792010-06-29 18:58:20 -0700814
Ian Romanick69846702010-06-22 17:29:19 -0700815 for (unsigned i = 0; i < num_attr; i++) {
816 /* Mask representing the contiguous slots that will be used by this
817 * attribute.
818 */
819 const unsigned use_mask = (1 << to_assign[i].slots) - 1;
820
821 int location = find_available_slots(used_locations, to_assign[i].slots);
822
823 if (location < 0) {
Ian Romanick553dcdc2010-06-23 12:14:02 -0700824 linker_error_printf(prog,
825 "insufficient contiguous attribute locations "
826 "available for vertex shader input `%s'",
827 to_assign[i].var->name);
Ian Romanick69846702010-06-22 17:29:19 -0700828 return false;
829 }
830
831 to_assign[i].var->location = VERT_ATTRIB_GENERIC0 + location;
832 used_locations |= (use_mask << location);
833 }
834
835 return true;
Ian Romanick0ad22cd2010-06-21 17:18:31 -0700836}
837
838
839void
Eric Anholt16b68b12010-06-30 11:05:43 -0700840assign_varying_locations(gl_shader *producer, gl_shader *consumer)
Ian Romanick0e59b262010-06-23 11:23:01 -0700841{
842 /* FINISHME: Set dynamically when geometry shader support is added. */
843 unsigned output_index = VERT_RESULT_VAR0;
844 unsigned input_index = FRAG_ATTRIB_VAR0;
845
846 /* Operate in a total of three passes.
847 *
848 * 1. Assign locations for any matching inputs and outputs.
849 *
850 * 2. Mark output variables in the producer that do not have locations as
851 * not being outputs. This lets the optimizer eliminate them.
852 *
853 * 3. Mark input variables in the consumer that do not have locations as
854 * not being inputs. This lets the optimizer eliminate them.
855 */
856
857 invalidate_variable_locations(producer, ir_var_out, VERT_RESULT_VAR0);
858 invalidate_variable_locations(consumer, ir_var_in, FRAG_ATTRIB_VAR0);
859
Eric Anholt16b68b12010-06-30 11:05:43 -0700860 foreach_list(node, producer->ir) {
Ian Romanick0e59b262010-06-23 11:23:01 -0700861 ir_variable *const output_var = ((ir_instruction *) node)->as_variable();
862
863 if ((output_var == NULL) || (output_var->mode != ir_var_out)
864 || (output_var->location != -1))
865 continue;
866
867 ir_variable *const input_var =
868 consumer->symbols->get_variable(output_var->name);
869
870 if ((input_var == NULL) || (input_var->mode != ir_var_in))
871 continue;
872
873 assert(input_var->location == -1);
874
875 /* FINISHME: Location assignment will need some changes when arrays,
876 * FINISHME: matrices, and structures are allowed as shader inputs /
877 * FINISHME: outputs.
878 */
879 output_var->location = output_index;
880 input_var->location = input_index;
881
882 output_index++;
883 input_index++;
884 }
885
Eric Anholt16b68b12010-06-30 11:05:43 -0700886 foreach_list(node, producer->ir) {
Ian Romanick0e59b262010-06-23 11:23:01 -0700887 ir_variable *const var = ((ir_instruction *) node)->as_variable();
888
889 if ((var == NULL) || (var->mode != ir_var_out))
890 continue;
891
892 /* An 'out' variable is only really a shader output if its value is read
893 * by the following stage.
894 */
895 var->shader_out = (var->location != -1);
896 }
897
Eric Anholt16b68b12010-06-30 11:05:43 -0700898 foreach_list(node, consumer->ir) {
Ian Romanick0e59b262010-06-23 11:23:01 -0700899 ir_variable *const var = ((ir_instruction *) node)->as_variable();
900
901 if ((var == NULL) || (var->mode != ir_var_in))
902 continue;
903
904 /* An 'in' variable is only really a shader input if its value is written
905 * by the previous stage.
906 */
907 var->shader_in = (var->location != -1);
908 }
909}
910
911
912void
Eric Anholt849e1812010-06-30 11:49:17 -0700913link_shaders(struct gl_shader_program *prog)
Ian Romanick832dfa52010-06-17 15:04:20 -0700914{
915 prog->LinkStatus = false;
916 prog->Validated = false;
917 prog->_Used = false;
918
Ian Romanickf36460e2010-06-23 12:07:22 -0700919 if (prog->InfoLog != NULL)
920 talloc_free(prog->InfoLog);
921
922 prog->InfoLog = talloc_strdup(NULL, "");
923
Ian Romanick832dfa52010-06-17 15:04:20 -0700924 /* Separate the shaders into groups based on their type.
925 */
Eric Anholt16b68b12010-06-30 11:05:43 -0700926 struct gl_shader **vert_shader_list;
Ian Romanick832dfa52010-06-17 15:04:20 -0700927 unsigned num_vert_shaders = 0;
Eric Anholt16b68b12010-06-30 11:05:43 -0700928 struct gl_shader **frag_shader_list;
Ian Romanick832dfa52010-06-17 15:04:20 -0700929 unsigned num_frag_shaders = 0;
930
Eric Anholt16b68b12010-06-30 11:05:43 -0700931 vert_shader_list = (struct gl_shader **)
932 calloc(2 * prog->NumShaders, sizeof(struct gl_shader *));
Ian Romanick832dfa52010-06-17 15:04:20 -0700933 frag_shader_list = &vert_shader_list[prog->NumShaders];
934
935 for (unsigned i = 0; i < prog->NumShaders; i++) {
936 switch (prog->Shaders[i]->Type) {
937 case GL_VERTEX_SHADER:
938 vert_shader_list[num_vert_shaders] = prog->Shaders[i];
939 num_vert_shaders++;
940 break;
941 case GL_FRAGMENT_SHADER:
942 frag_shader_list[num_frag_shaders] = prog->Shaders[i];
943 num_frag_shaders++;
944 break;
945 case GL_GEOMETRY_SHADER:
946 /* FINISHME: Support geometry shaders. */
947 assert(prog->Shaders[i]->Type != GL_GEOMETRY_SHADER);
948 break;
949 }
950 }
951
952 /* FINISHME: Implement intra-stage linking. */
Ian Romanickabee16e2010-06-21 16:16:05 -0700953 prog->_NumLinkedShaders = 0;
Ian Romanickcc22c5a2010-06-18 17:13:42 -0700954 if (num_vert_shaders > 0) {
Ian Romanick3fb87872010-07-09 14:09:34 -0700955 gl_shader *const sh =
956 link_intrastage_shaders(prog, vert_shader_list, num_vert_shaders);
957
958 if (sh == NULL)
959 goto done;
960
961 if (!validate_vertex_shader_executable(prog, sh))
962 goto done;
963
964 prog->_LinkedShaders[prog->_NumLinkedShaders] = sh;
Ian Romanickabee16e2010-06-21 16:16:05 -0700965 prog->_NumLinkedShaders++;
Ian Romanickcc22c5a2010-06-18 17:13:42 -0700966 }
967
968 if (num_frag_shaders > 0) {
Ian Romanick3fb87872010-07-09 14:09:34 -0700969 gl_shader *const sh =
970 link_intrastage_shaders(prog, frag_shader_list, num_frag_shaders);
971
972 if (sh == NULL)
973 goto done;
974
975 if (!validate_fragment_shader_executable(prog, sh))
976 goto done;
977
978 prog->_LinkedShaders[prog->_NumLinkedShaders] = sh;
Ian Romanickabee16e2010-06-21 16:16:05 -0700979 prog->_NumLinkedShaders++;
Ian Romanickcc22c5a2010-06-18 17:13:42 -0700980 }
981
Ian Romanick3ed850e2010-06-23 12:18:21 -0700982 /* Here begins the inter-stage linking phase. Some initial validation is
983 * performed, then locations are assigned for uniforms, attributes, and
984 * varyings.
985 */
Ian Romanicked1fe3d2010-06-23 12:09:14 -0700986 if (cross_validate_uniforms(prog)) {
Ian Romanick37101922010-06-18 19:02:10 -0700987 /* Validate the inputs of each stage with the output of the preceeding
988 * stage.
989 */
Ian Romanickabee16e2010-06-21 16:16:05 -0700990 for (unsigned i = 1; i < prog->_NumLinkedShaders; i++) {
Ian Romanickf36460e2010-06-23 12:07:22 -0700991 if (!cross_validate_outputs_to_inputs(prog,
992 prog->_LinkedShaders[i - 1],
Ian Romanickabee16e2010-06-21 16:16:05 -0700993 prog->_LinkedShaders[i]))
Ian Romanick37101922010-06-18 19:02:10 -0700994 goto done;
995 }
996
Ian Romanickcc22c5a2010-06-18 17:13:42 -0700997 prog->LinkStatus = true;
Ian Romanick37101922010-06-18 19:02:10 -0700998 }
Ian Romanick832dfa52010-06-17 15:04:20 -0700999
Ian Romanick13e10e42010-06-21 12:03:24 -07001000 /* FINISHME: Perform whole-program optimization here. */
1001
Ian Romanickabee16e2010-06-21 16:16:05 -07001002 assign_uniform_locations(prog);
Ian Romanick13e10e42010-06-21 12:03:24 -07001003
Ian Romanick0ad22cd2010-06-21 17:18:31 -07001004 if (prog->_LinkedShaders[0]->Type == GL_VERTEX_SHADER)
Ian Romanick9342d262010-06-22 17:41:37 -07001005 /* FINISHME: The value of the max_attribute_index parameter is
1006 * FINISHME: implementation dependent based on the value of
1007 * FINISHME: GL_MAX_VERTEX_ATTRIBS. GL_MAX_VERTEX_ATTRIBS must be
1008 * FINISHME: at least 16, so hardcode 16 for now.
1009 */
Ian Romanick553dcdc2010-06-23 12:14:02 -07001010 if (!assign_attribute_locations(prog, 16))
Ian Romanick69846702010-06-22 17:29:19 -07001011 goto done;
Ian Romanick13e10e42010-06-21 12:03:24 -07001012
Ian Romanick0e59b262010-06-23 11:23:01 -07001013 for (unsigned i = 1; i < prog->_NumLinkedShaders; i++)
1014 assign_varying_locations(prog->_LinkedShaders[i - 1],
1015 prog->_LinkedShaders[i]);
Ian Romanick13e10e42010-06-21 12:03:24 -07001016
1017 /* FINISHME: Assign fragment shader output locations. */
1018
Ian Romanick832dfa52010-06-17 15:04:20 -07001019done:
1020 free(vert_shader_list);
1021}