blob: ebb1a81e7c9e1f2f7ef7f555a0620cefe5ecedd2 [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 Romanick832dfa52010-06-17 15:04:20 -070081
82/**
83 * Visitor that determines whether or not a variable is ever written.
84 */
85class find_assignment_visitor : public ir_hierarchical_visitor {
86public:
87 find_assignment_visitor(const char *name)
88 : name(name), found(false)
89 {
90 /* empty */
91 }
92
93 virtual ir_visitor_status visit_enter(ir_assignment *ir)
94 {
95 ir_variable *const var = ir->lhs->variable_referenced();
96
97 if (strcmp(name, var->name) == 0) {
98 found = true;
99 return visit_stop;
100 }
101
102 return visit_continue_with_parent;
103 }
104
105 bool variable_found()
106 {
107 return found;
108 }
109
110private:
111 const char *name; /**< Find writes to a variable with this name. */
112 bool found; /**< Was a write to the variable found? */
113};
114
Ian Romanickc93b8f12010-06-17 15:20:22 -0700115
Ian Romanick0ad22cd2010-06-21 17:18:31 -0700116void
Ian Romanickf36460e2010-06-23 12:07:22 -0700117linker_error_printf(glsl_program *prog, const char *fmt, ...)
118{
119 va_list ap;
120
121 prog->InfoLog = talloc_strdup_append(prog->InfoLog, "error: ");
122 va_start(ap, fmt);
123 prog->InfoLog = talloc_vasprintf_append(prog->InfoLog, fmt, ap);
124 va_end(ap);
125}
126
127
128void
Ian Romanick0ad22cd2010-06-21 17:18:31 -0700129invalidate_variable_locations(glsl_shader *sh, enum ir_variable_mode mode,
130 int generic_base)
131{
132 foreach_list(node, &sh->ir) {
133 ir_variable *const var = ((ir_instruction *) node)->as_variable();
134
135 if ((var == NULL) || (var->mode != (unsigned) mode))
136 continue;
137
138 /* Only assign locations for generic attributes / varyings / etc.
139 */
140 if (var->location >= generic_base)
141 var->location = -1;
142 }
143}
144
145
Ian Romanickc93b8f12010-06-17 15:20:22 -0700146/**
Ian Romanick69846702010-06-22 17:29:19 -0700147 * Determine the number of attribute slots required for a particular type
148 *
149 * This code is here because it implements the language rules of a specific
150 * GLSL version. Since it's a property of the language and not a property of
151 * types in general, it doesn't really belong in glsl_type.
152 */
153unsigned
154count_attribute_slots(const glsl_type *t)
155{
156 /* From page 31 (page 37 of the PDF) of the GLSL 1.50 spec:
157 *
158 * "A scalar input counts the same amount against this limit as a vec4,
159 * so applications may want to consider packing groups of four
160 * unrelated float inputs together into a vector to better utilize the
161 * capabilities of the underlying hardware. A matrix input will use up
162 * multiple locations. The number of locations used will equal the
163 * number of columns in the matrix."
164 *
165 * The spec does not explicitly say how arrays are counted. However, it
166 * should be safe to assume the total number of slots consumed by an array
167 * is the number of entries in the array multiplied by the number of slots
168 * consumed by a single element of the array.
169 */
170
171 if (t->is_array())
172 return t->array_size() * count_attribute_slots(t->element_type());
173
174 if (t->is_matrix())
175 return t->matrix_columns;
176
177 return 1;
178}
179
180
181/**
Ian Romanickc93b8f12010-06-17 15:20:22 -0700182 * Verify that a vertex shader executable meets all semantic requirements
183 *
184 * \param shader Vertex shader executable to be verified
185 */
Ian Romanick832dfa52010-06-17 15:04:20 -0700186bool
Ian Romanickf36460e2010-06-23 12:07:22 -0700187validate_vertex_shader_executable(struct glsl_program *prog,
188 struct glsl_shader *shader)
Ian Romanick832dfa52010-06-17 15:04:20 -0700189{
190 if (shader == NULL)
191 return true;
192
193 if (!shader->symbols->get_function("main")) {
Ian Romanickf36460e2010-06-23 12:07:22 -0700194 linker_error_printf(prog, "vertex shader lacks `main'\n");
Ian Romanick832dfa52010-06-17 15:04:20 -0700195 return false;
196 }
197
198 find_assignment_visitor find("gl_Position");
199 find.run(&shader->ir);
200 if (!find.variable_found()) {
Ian Romanickf36460e2010-06-23 12:07:22 -0700201 linker_error_printf(prog,
202 "vertex shader does not write to `gl_Position'\n");
Ian Romanick832dfa52010-06-17 15:04:20 -0700203 return false;
204 }
205
206 return true;
207}
208
209
Ian Romanickc93b8f12010-06-17 15:20:22 -0700210/**
211 * Verify that a fragment shader executable meets all semantic requirements
212 *
213 * \param shader Fragment shader executable to be verified
214 */
Ian Romanick832dfa52010-06-17 15:04:20 -0700215bool
Ian Romanickf36460e2010-06-23 12:07:22 -0700216validate_fragment_shader_executable(struct glsl_program *prog,
217 struct glsl_shader *shader)
Ian Romanick832dfa52010-06-17 15:04:20 -0700218{
219 if (shader == NULL)
220 return true;
221
222 if (!shader->symbols->get_function("main")) {
Ian Romanickf36460e2010-06-23 12:07:22 -0700223 linker_error_printf(prog, "fragment shader lacks `main'\n");
Ian Romanick832dfa52010-06-17 15:04:20 -0700224 return false;
225 }
226
227 find_assignment_visitor frag_color("gl_FragColor");
228 find_assignment_visitor frag_data("gl_FragData");
229
230 frag_color.run(&shader->ir);
231 frag_data.run(&shader->ir);
232
233 if (!frag_color.variable_found() && !frag_data.variable_found()) {
Ian Romanickf36460e2010-06-23 12:07:22 -0700234 linker_error_printf(prog, "fragment shader does not write to "
235 "`gl_FragColor' or `gl_FragData'\n");
Ian Romanick832dfa52010-06-17 15:04:20 -0700236 return false;
237 }
238
239 if (frag_color.variable_found() && frag_data.variable_found()) {
Ian Romanickf36460e2010-06-23 12:07:22 -0700240 linker_error_printf(prog, "fragment shader writes to both "
241 "`gl_FragColor' and `gl_FragData'\n");
Ian Romanick832dfa52010-06-17 15:04:20 -0700242 return false;
243 }
244
245 return true;
246}
247
248
Ian Romanickcc22c5a2010-06-18 17:13:42 -0700249/**
250 * Perform validation of uniforms used across multiple shader stages
251 */
252bool
Ian Romanicked1fe3d2010-06-23 12:09:14 -0700253cross_validate_uniforms(struct glsl_program *prog)
Ian Romanickcc22c5a2010-06-18 17:13:42 -0700254{
255 /* Examine all of the uniforms in all of the shaders and cross validate
256 * them.
257 */
258 glsl_symbol_table uniforms;
Ian Romanicked1fe3d2010-06-23 12:09:14 -0700259 for (unsigned i = 0; i < prog->_NumLinkedShaders; i++) {
260 foreach_list(node, &prog->_LinkedShaders[i]->ir) {
Ian Romanickcc22c5a2010-06-18 17:13:42 -0700261 ir_variable *const var = ((ir_instruction *) node)->as_variable();
262
263 if ((var == NULL) || (var->mode != ir_var_uniform))
264 continue;
265
266 /* If a uniform with this name has already been seen, verify that the
267 * new instance has the same type. In addition, if the uniforms have
268 * initializers, the values of the initializers must be the same.
269 */
270 ir_variable *const existing = uniforms.get_variable(var->name);
271 if (existing != NULL) {
272 if (var->type != existing->type) {
Ian Romanickf36460e2010-06-23 12:07:22 -0700273 linker_error_printf(prog, "uniform `%s' declared as type "
274 "`%s' and type `%s'\n",
275 var->name, var->type->name,
276 existing->type->name);
Ian Romanickcc22c5a2010-06-18 17:13:42 -0700277 return false;
278 }
279
280 if (var->constant_value != NULL) {
281 if (existing->constant_value != NULL) {
282 if (!var->constant_value->has_value(existing->constant_value)) {
Ian Romanickf36460e2010-06-23 12:07:22 -0700283 linker_error_printf(prog, "initializers for uniform "
284 "`%s' have differing values\n",
285 var->name);
Ian Romanickcc22c5a2010-06-18 17:13:42 -0700286 return false;
287 }
288 } else
289 /* If the first-seen instance of a particular uniform did not
290 * have an initializer but a later instance does, copy the
291 * initializer to the version stored in the symbol table.
292 */
293 existing->constant_value = var->constant_value->clone();
294 }
295 } else
296 uniforms.add_variable(var->name, var);
297 }
298 }
299
300 return true;
301}
302
303
Ian Romanick37101922010-06-18 19:02:10 -0700304/**
305 * Validate that outputs from one stage match inputs of another
306 */
307bool
Ian Romanickf36460e2010-06-23 12:07:22 -0700308cross_validate_outputs_to_inputs(struct glsl_program *prog,
309 glsl_shader *producer, glsl_shader *consumer)
Ian Romanick37101922010-06-18 19:02:10 -0700310{
311 glsl_symbol_table parameters;
312 /* FINISHME: Figure these out dynamically. */
313 const char *const producer_stage = "vertex";
314 const char *const consumer_stage = "fragment";
315
316 /* Find all shader outputs in the "producer" stage.
317 */
318 foreach_list(node, &producer->ir) {
319 ir_variable *const var = ((ir_instruction *) node)->as_variable();
320
321 /* FINISHME: For geometry shaders, this should also look for inout
322 * FINISHME: variables.
323 */
324 if ((var == NULL) || (var->mode != ir_var_out))
325 continue;
326
327 parameters.add_variable(var->name, var);
328 }
329
330
331 /* Find all shader inputs in the "consumer" stage. Any variables that have
332 * matching outputs already in the symbol table must have the same type and
333 * qualifiers.
334 */
335 foreach_list(node, &consumer->ir) {
336 ir_variable *const input = ((ir_instruction *) node)->as_variable();
337
338 /* FINISHME: For geometry shaders, this should also look for inout
339 * FINISHME: variables.
340 */
341 if ((input == NULL) || (input->mode != ir_var_in))
342 continue;
343
344 ir_variable *const output = parameters.get_variable(input->name);
345 if (output != NULL) {
346 /* Check that the types match between stages.
347 */
348 if (input->type != output->type) {
Ian Romanickf36460e2010-06-23 12:07:22 -0700349 linker_error_printf(prog,
350 "%s shader output `%s' delcared as "
351 "type `%s', but %s shader input declared "
352 "as type `%s'\n",
353 producer_stage, output->name,
354 output->type->name,
355 consumer_stage, input->type->name);
Ian Romanick37101922010-06-18 19:02:10 -0700356 return false;
357 }
358
359 /* Check that all of the qualifiers match between stages.
360 */
361 if (input->centroid != output->centroid) {
Ian Romanickf36460e2010-06-23 12:07:22 -0700362 linker_error_printf(prog,
363 "%s shader output `%s' %s centroid qualifier, "
364 "but %s shader input %s centroid qualifier\n",
365 producer_stage,
366 output->name,
367 (output->centroid) ? "has" : "lacks",
368 consumer_stage,
369 (input->centroid) ? "has" : "lacks");
Ian Romanick37101922010-06-18 19:02:10 -0700370 return false;
371 }
372
373 if (input->invariant != output->invariant) {
Ian Romanickf36460e2010-06-23 12:07:22 -0700374 linker_error_printf(prog,
375 "%s shader output `%s' %s invariant qualifier, "
376 "but %s shader input %s invariant qualifier\n",
377 producer_stage,
378 output->name,
379 (output->invariant) ? "has" : "lacks",
380 consumer_stage,
381 (input->invariant) ? "has" : "lacks");
Ian Romanick37101922010-06-18 19:02:10 -0700382 return false;
383 }
384
385 if (input->interpolation != output->interpolation) {
Ian Romanickf36460e2010-06-23 12:07:22 -0700386 linker_error_printf(prog,
387 "%s shader output `%s' specifies %s "
388 "interpolation qualifier, "
389 "but %s shader input specifies %s "
390 "interpolation qualifier\n",
391 producer_stage,
392 output->name,
393 output->interpolation_string(),
394 consumer_stage,
395 input->interpolation_string());
Ian Romanick37101922010-06-18 19:02:10 -0700396 return false;
397 }
398 }
399 }
400
401 return true;
402}
403
404
Ian Romanick019a59b2010-06-21 16:10:42 -0700405struct uniform_node {
406 exec_node link;
407 struct gl_uniform *u;
408 unsigned slots;
409};
410
Ian Romanickabee16e2010-06-21 16:16:05 -0700411void
412assign_uniform_locations(struct glsl_program *prog)
Ian Romanick019a59b2010-06-21 16:10:42 -0700413{
414 /* */
415 exec_list uniforms;
416 unsigned total_uniforms = 0;
417 hash_table *ht = hash_table_ctor(32, hash_table_string_hash,
418 hash_table_string_compare);
419
Ian Romanickabee16e2010-06-21 16:16:05 -0700420 for (unsigned i = 0; i < prog->_NumLinkedShaders; i++) {
Ian Romanick019a59b2010-06-21 16:10:42 -0700421 unsigned next_position = 0;
422
Ian Romanickabee16e2010-06-21 16:16:05 -0700423 foreach_list(node, &prog->_LinkedShaders[i]->ir) {
Ian Romanick019a59b2010-06-21 16:10:42 -0700424 ir_variable *const var = ((ir_instruction *) node)->as_variable();
425
426 if ((var == NULL) || (var->mode != ir_var_uniform))
427 continue;
428
429 const unsigned vec4_slots = (var->component_slots() + 3) / 4;
430 assert(vec4_slots != 0);
431
432 uniform_node *n = (uniform_node *) hash_table_find(ht, var->name);
433 if (n == NULL) {
434 n = (uniform_node *) calloc(1, sizeof(struct uniform_node));
435 n->u = (gl_uniform *) calloc(vec4_slots, sizeof(struct gl_uniform));
436 n->slots = vec4_slots;
437
438 n->u[0].Name = strdup(var->name);
439 for (unsigned j = 1; j < vec4_slots; j++)
440 n->u[j].Name = n->u[0].Name;
441
442 hash_table_insert(ht, n, n->u[0].Name);
443 uniforms.push_tail(& n->link);
444 total_uniforms += vec4_slots;
445 }
446
447 if (var->constant_value != NULL)
448 for (unsigned j = 0; j < vec4_slots; j++)
449 n->u[j].Initialized = true;
450
451 var->location = next_position;
452
453 for (unsigned j = 0; j < vec4_slots; j++) {
Ian Romanickabee16e2010-06-21 16:16:05 -0700454 switch (prog->_LinkedShaders[i]->Type) {
Ian Romanick019a59b2010-06-21 16:10:42 -0700455 case GL_VERTEX_SHADER:
456 n->u[j].VertPos = next_position;
457 break;
458 case GL_FRAGMENT_SHADER:
459 n->u[j].FragPos = next_position;
460 break;
461 case GL_GEOMETRY_SHADER:
462 /* FINISHME: Support geometry shaders. */
Ian Romanickabee16e2010-06-21 16:16:05 -0700463 assert(prog->_LinkedShaders[i]->Type != GL_GEOMETRY_SHADER);
Ian Romanick019a59b2010-06-21 16:10:42 -0700464 break;
465 }
466
467 next_position++;
468 }
469 }
470 }
471
472 gl_uniform_list *ul = (gl_uniform_list *)
473 calloc(1, sizeof(gl_uniform_list));
474
475 ul->Size = total_uniforms;
476 ul->NumUniforms = total_uniforms;
477 ul->Uniforms = (gl_uniform *) calloc(total_uniforms, sizeof(gl_uniform));
478
479 unsigned idx = 0;
480 uniform_node *next;
481 for (uniform_node *node = (uniform_node *) uniforms.head
482 ; node->link.next != NULL
483 ; node = next) {
484 next = (uniform_node *) node->link.next;
485
486 node->link.remove();
487 memcpy(&ul->Uniforms[idx], node->u, sizeof(gl_uniform) * node->slots);
488 idx += node->slots;
489
490 free(node->u);
491 free(node);
492 }
493
494 hash_table_dtor(ht);
495
Ian Romanickabee16e2010-06-21 16:16:05 -0700496 prog->Uniforms = ul;
Ian Romanick019a59b2010-06-21 16:10:42 -0700497}
498
499
Ian Romanick69846702010-06-22 17:29:19 -0700500/**
501 * Find a contiguous set of available bits in a bitmask
502 *
503 * \param used_mask Bits representing used (1) and unused (0) locations
504 * \param needed_count Number of contiguous bits needed.
505 *
506 * \return
507 * Base location of the available bits on success or -1 on failure.
508 */
509int
510find_available_slots(unsigned used_mask, unsigned needed_count)
511{
512 unsigned needed_mask = (1 << needed_count) - 1;
513 const int max_bit_to_test = (8 * sizeof(used_mask)) - needed_count;
514
515 /* The comparison to 32 is redundant, but without it GCC emits "warning:
516 * cannot optimize possibly infinite loops" for the loop below.
517 */
518 if ((needed_count == 0) || (max_bit_to_test < 0) || (max_bit_to_test > 32))
519 return -1;
520
521 for (int i = 0; i <= max_bit_to_test; i++) {
522 if ((needed_mask & ~used_mask) == needed_mask)
523 return i;
524
525 needed_mask <<= 1;
526 }
527
528 return -1;
529}
530
531
532bool
Ian Romanick553dcdc2010-06-23 12:14:02 -0700533assign_attribute_locations(glsl_program *prog, unsigned max_attribute_index)
Ian Romanick0ad22cd2010-06-21 17:18:31 -0700534{
Ian Romanick9342d262010-06-22 17:41:37 -0700535 /* Mark invalid attribute locations as being used.
536 */
537 unsigned used_locations = (max_attribute_index >= 32)
538 ? ~0 : ~((1 << max_attribute_index) - 1);
Ian Romanick0ad22cd2010-06-21 17:18:31 -0700539
Ian Romanick553dcdc2010-06-23 12:14:02 -0700540 glsl_shader *const sh = prog->_LinkedShaders[0];
Ian Romanick0ad22cd2010-06-21 17:18:31 -0700541 assert(sh->Type == GL_VERTEX_SHADER);
542
Ian Romanick69846702010-06-22 17:29:19 -0700543 /* Operate in a total of four passes.
Ian Romanick0ad22cd2010-06-21 17:18:31 -0700544 *
545 * 1. Invalidate the location assignments for all vertex shader inputs.
546 *
547 * 2. Assign locations for inputs that have user-defined (via
548 * glBindVertexAttribLocation) locatoins.
549 *
Ian Romanick69846702010-06-22 17:29:19 -0700550 * 3. Sort the attributes without assigned locations by number of slots
551 * required in decreasing order. Fragmentation caused by attribute
552 * locations assigned by the application may prevent large attributes
553 * from having enough contiguous space.
554 *
555 * 4. Assign locations to any inputs without assigned locations.
Ian Romanick0ad22cd2010-06-21 17:18:31 -0700556 */
557
558 invalidate_variable_locations(sh, ir_var_in, VERT_ATTRIB_GENERIC0);
559
Ian Romanick553dcdc2010-06-23 12:14:02 -0700560 if (prog->Attributes != NULL) {
561 for (unsigned i = 0; i < prog->Attributes->NumParameters; i++) {
Ian Romanick0ad22cd2010-06-21 17:18:31 -0700562 ir_variable *const var =
Ian Romanick553dcdc2010-06-23 12:14:02 -0700563 sh->symbols->get_variable(prog->Attributes->Parameters[i].Name);
Ian Romanick0ad22cd2010-06-21 17:18:31 -0700564
Ian Romanick69846702010-06-22 17:29:19 -0700565 /* Note: attributes that occupy multiple slots, such as arrays or
566 * matrices, may appear in the attrib array multiple times.
567 */
568 if ((var == NULL) || (var->location != -1))
Ian Romanick0ad22cd2010-06-21 17:18:31 -0700569 continue;
570
Ian Romanick69846702010-06-22 17:29:19 -0700571 /* From page 61 of the OpenGL 4.0 spec:
572 *
573 * "LinkProgram will fail if the attribute bindings assigned by
574 * BindAttribLocation do not leave not enough space to assign a
575 * location for an active matrix attribute or an active attribute
576 * array, both of which require multiple contiguous generic
577 * attributes."
578 *
579 * Previous versions of the spec contain similar language but omit the
580 * bit about attribute arrays.
581 *
582 * Page 61 of the OpenGL 4.0 spec also says:
583 *
584 * "It is possible for an application to bind more than one
585 * attribute name to the same location. This is referred to as
586 * aliasing. This will only work if only one of the aliased
587 * attributes is active in the executable program, or if no path
588 * through the shader consumes more than one attribute of a set
589 * of attributes aliased to the same location. A link error can
590 * occur if the linker determines that every path through the
591 * shader consumes multiple aliased attributes, but
592 * implementations are not required to generate an error in this
593 * case."
594 *
595 * These two paragraphs are either somewhat contradictory, or I don't
596 * fully understand one or both of them.
597 */
598 /* FINISHME: The code as currently written does not support attribute
599 * FINISHME: location aliasing (see comment above).
600 */
Ian Romanick553dcdc2010-06-23 12:14:02 -0700601 const int attr = prog->Attributes->Parameters[i].StateIndexes[0];
Ian Romanick69846702010-06-22 17:29:19 -0700602 const unsigned slots = count_attribute_slots(var->type);
603
604 /* Mask representing the contiguous slots that will be used by this
605 * attribute.
606 */
607 const unsigned use_mask = (1 << slots) - 1;
608
609 /* Generate a link error if the set of bits requested for this
610 * attribute overlaps any previously allocated bits.
611 */
612 if ((~(use_mask << attr) & used_locations) != used_locations) {
Ian Romanick553dcdc2010-06-23 12:14:02 -0700613 linker_error_printf(prog,
614 "insufficient contiguous attribute locations "
615 "available for vertex shader input `%s'",
616 var->name);
Ian Romanick69846702010-06-22 17:29:19 -0700617 return false;
618 }
Ian Romanick0ad22cd2010-06-21 17:18:31 -0700619
620 var->location = VERT_ATTRIB_GENERIC0 + attr;
Ian Romanick69846702010-06-22 17:29:19 -0700621 used_locations |= (use_mask << attr);
Ian Romanick0ad22cd2010-06-21 17:18:31 -0700622 }
623 }
624
Ian Romanick69846702010-06-22 17:29:19 -0700625 /* Temporary storage for the set of attributes that need locations assigned.
626 */
627 struct temp_attr {
628 unsigned slots;
629 ir_variable *var;
630
631 /* Used below in the call to qsort. */
632 static int compare(const void *a, const void *b)
633 {
634 const temp_attr *const l = (const temp_attr *) a;
635 const temp_attr *const r = (const temp_attr *) b;
636
637 /* Reversed because we want a descending order sort below. */
638 return r->slots - l->slots;
639 }
640 } to_assign[16];
641
642 unsigned num_attr = 0;
643
Ian Romanick0ad22cd2010-06-21 17:18:31 -0700644 foreach_list(node, &sh->ir) {
645 ir_variable *const var = ((ir_instruction *) node)->as_variable();
646
647 if ((var == NULL) || (var->mode != ir_var_in))
648 continue;
649
650 /* The location was explicitly assigned, nothing to do here.
651 */
652 if (var->location != -1)
653 continue;
654
Ian Romanick69846702010-06-22 17:29:19 -0700655 to_assign[num_attr].slots = count_attribute_slots(var->type);
656 to_assign[num_attr].var = var;
657 num_attr++;
Ian Romanick0ad22cd2010-06-21 17:18:31 -0700658 }
Ian Romanick69846702010-06-22 17:29:19 -0700659
660 /* If all of the attributes were assigned locations by the application (or
661 * are built-in attributes with fixed locations), return early. This should
662 * be the common case.
663 */
664 if (num_attr == 0)
665 return true;
666
667 qsort(to_assign, num_attr, sizeof(to_assign[0]), temp_attr::compare);
668
669 for (unsigned i = 0; i < num_attr; i++) {
670 /* Mask representing the contiguous slots that will be used by this
671 * attribute.
672 */
673 const unsigned use_mask = (1 << to_assign[i].slots) - 1;
674
675 int location = find_available_slots(used_locations, to_assign[i].slots);
676
677 if (location < 0) {
Ian Romanick553dcdc2010-06-23 12:14:02 -0700678 linker_error_printf(prog,
679 "insufficient contiguous attribute locations "
680 "available for vertex shader input `%s'",
681 to_assign[i].var->name);
Ian Romanick69846702010-06-22 17:29:19 -0700682 return false;
683 }
684
685 to_assign[i].var->location = VERT_ATTRIB_GENERIC0 + location;
686 used_locations |= (use_mask << location);
687 }
688
689 return true;
Ian Romanick0ad22cd2010-06-21 17:18:31 -0700690}
691
692
693void
Ian Romanick0e59b262010-06-23 11:23:01 -0700694assign_varying_locations(glsl_shader *producer, glsl_shader *consumer)
695{
696 /* FINISHME: Set dynamically when geometry shader support is added. */
697 unsigned output_index = VERT_RESULT_VAR0;
698 unsigned input_index = FRAG_ATTRIB_VAR0;
699
700 /* Operate in a total of three passes.
701 *
702 * 1. Assign locations for any matching inputs and outputs.
703 *
704 * 2. Mark output variables in the producer that do not have locations as
705 * not being outputs. This lets the optimizer eliminate them.
706 *
707 * 3. Mark input variables in the consumer that do not have locations as
708 * not being inputs. This lets the optimizer eliminate them.
709 */
710
711 invalidate_variable_locations(producer, ir_var_out, VERT_RESULT_VAR0);
712 invalidate_variable_locations(consumer, ir_var_in, FRAG_ATTRIB_VAR0);
713
714 foreach_list(node, &producer->ir) {
715 ir_variable *const output_var = ((ir_instruction *) node)->as_variable();
716
717 if ((output_var == NULL) || (output_var->mode != ir_var_out)
718 || (output_var->location != -1))
719 continue;
720
721 ir_variable *const input_var =
722 consumer->symbols->get_variable(output_var->name);
723
724 if ((input_var == NULL) || (input_var->mode != ir_var_in))
725 continue;
726
727 assert(input_var->location == -1);
728
729 /* FINISHME: Location assignment will need some changes when arrays,
730 * FINISHME: matrices, and structures are allowed as shader inputs /
731 * FINISHME: outputs.
732 */
733 output_var->location = output_index;
734 input_var->location = input_index;
735
736 output_index++;
737 input_index++;
738 }
739
740 foreach_list(node, &producer->ir) {
741 ir_variable *const var = ((ir_instruction *) node)->as_variable();
742
743 if ((var == NULL) || (var->mode != ir_var_out))
744 continue;
745
746 /* An 'out' variable is only really a shader output if its value is read
747 * by the following stage.
748 */
749 var->shader_out = (var->location != -1);
750 }
751
752 foreach_list(node, &consumer->ir) {
753 ir_variable *const var = ((ir_instruction *) node)->as_variable();
754
755 if ((var == NULL) || (var->mode != ir_var_in))
756 continue;
757
758 /* An 'in' variable is only really a shader input if its value is written
759 * by the previous stage.
760 */
761 var->shader_in = (var->location != -1);
762 }
763}
764
765
766void
Ian Romanick832dfa52010-06-17 15:04:20 -0700767link_shaders(struct glsl_program *prog)
768{
769 prog->LinkStatus = false;
770 prog->Validated = false;
771 prog->_Used = false;
772
Ian Romanickf36460e2010-06-23 12:07:22 -0700773 if (prog->InfoLog != NULL)
774 talloc_free(prog->InfoLog);
775
776 prog->InfoLog = talloc_strdup(NULL, "");
777
Ian Romanick832dfa52010-06-17 15:04:20 -0700778 /* Separate the shaders into groups based on their type.
779 */
780 struct glsl_shader **vert_shader_list;
781 unsigned num_vert_shaders = 0;
782 struct glsl_shader **frag_shader_list;
783 unsigned num_frag_shaders = 0;
784
785 vert_shader_list = (struct glsl_shader **)
Kenneth Graunkec186b3f2010-06-17 15:37:26 -0700786 calloc(2 * prog->NumShaders, sizeof(struct glsl_shader *));
Ian Romanick832dfa52010-06-17 15:04:20 -0700787 frag_shader_list = &vert_shader_list[prog->NumShaders];
788
789 for (unsigned i = 0; i < prog->NumShaders; i++) {
790 switch (prog->Shaders[i]->Type) {
791 case GL_VERTEX_SHADER:
792 vert_shader_list[num_vert_shaders] = prog->Shaders[i];
793 num_vert_shaders++;
794 break;
795 case GL_FRAGMENT_SHADER:
796 frag_shader_list[num_frag_shaders] = prog->Shaders[i];
797 num_frag_shaders++;
798 break;
799 case GL_GEOMETRY_SHADER:
800 /* FINISHME: Support geometry shaders. */
801 assert(prog->Shaders[i]->Type != GL_GEOMETRY_SHADER);
802 break;
803 }
804 }
805
806 /* FINISHME: Implement intra-stage linking. */
807 assert(num_vert_shaders <= 1);
808 assert(num_frag_shaders <= 1);
809
810 /* Verify that each of the per-target executables is valid.
811 */
Ian Romanickf36460e2010-06-23 12:07:22 -0700812 if (!validate_vertex_shader_executable(prog, vert_shader_list[0])
813 || !validate_fragment_shader_executable(prog, frag_shader_list[0]))
Ian Romanick832dfa52010-06-17 15:04:20 -0700814 goto done;
815
816
817 /* FINISHME: Perform inter-stage linking. */
Ian Romanickabee16e2010-06-21 16:16:05 -0700818 prog->_LinkedShaders = (struct glsl_shader **)
819 calloc(2, sizeof(struct glsl_shader *));
820 prog->_NumLinkedShaders = 0;
Ian Romanick832dfa52010-06-17 15:04:20 -0700821
Ian Romanickcc22c5a2010-06-18 17:13:42 -0700822 if (num_vert_shaders > 0) {
Ian Romanickabee16e2010-06-21 16:16:05 -0700823 prog->_LinkedShaders[prog->_NumLinkedShaders] = vert_shader_list[0];
824 prog->_NumLinkedShaders++;
Ian Romanickcc22c5a2010-06-18 17:13:42 -0700825 }
826
827 if (num_frag_shaders > 0) {
Ian Romanickabee16e2010-06-21 16:16:05 -0700828 prog->_LinkedShaders[prog->_NumLinkedShaders] = frag_shader_list[0];
829 prog->_NumLinkedShaders++;
Ian Romanickcc22c5a2010-06-18 17:13:42 -0700830 }
831
Ian Romanicked1fe3d2010-06-23 12:09:14 -0700832 if (cross_validate_uniforms(prog)) {
Ian Romanick37101922010-06-18 19:02:10 -0700833 /* Validate the inputs of each stage with the output of the preceeding
834 * stage.
835 */
Ian Romanickabee16e2010-06-21 16:16:05 -0700836 for (unsigned i = 1; i < prog->_NumLinkedShaders; i++) {
Ian Romanickf36460e2010-06-23 12:07:22 -0700837 if (!cross_validate_outputs_to_inputs(prog,
838 prog->_LinkedShaders[i - 1],
Ian Romanickabee16e2010-06-21 16:16:05 -0700839 prog->_LinkedShaders[i]))
Ian Romanick37101922010-06-18 19:02:10 -0700840 goto done;
841 }
842
Ian Romanickcc22c5a2010-06-18 17:13:42 -0700843 prog->LinkStatus = true;
Ian Romanick37101922010-06-18 19:02:10 -0700844 }
Ian Romanick832dfa52010-06-17 15:04:20 -0700845
Ian Romanick13e10e42010-06-21 12:03:24 -0700846 /* FINISHME: Perform whole-program optimization here. */
847
Ian Romanickabee16e2010-06-21 16:16:05 -0700848 assign_uniform_locations(prog);
Ian Romanick13e10e42010-06-21 12:03:24 -0700849
Ian Romanick0ad22cd2010-06-21 17:18:31 -0700850 if (prog->_LinkedShaders[0]->Type == GL_VERTEX_SHADER)
Ian Romanick9342d262010-06-22 17:41:37 -0700851 /* FINISHME: The value of the max_attribute_index parameter is
852 * FINISHME: implementation dependent based on the value of
853 * FINISHME: GL_MAX_VERTEX_ATTRIBS. GL_MAX_VERTEX_ATTRIBS must be
854 * FINISHME: at least 16, so hardcode 16 for now.
855 */
Ian Romanick553dcdc2010-06-23 12:14:02 -0700856 if (!assign_attribute_locations(prog, 16))
Ian Romanick69846702010-06-22 17:29:19 -0700857 goto done;
Ian Romanick13e10e42010-06-21 12:03:24 -0700858
Ian Romanick0e59b262010-06-23 11:23:01 -0700859 for (unsigned i = 1; i < prog->_NumLinkedShaders; i++)
860 assign_varying_locations(prog->_LinkedShaders[i - 1],
861 prog->_LinkedShaders[i]);
Ian Romanick13e10e42010-06-21 12:03:24 -0700862
863 /* FINISHME: Assign fragment shader output locations. */
864
865 /* FINISHME: Generate code here. */
866
Ian Romanick832dfa52010-06-17 15:04:20 -0700867done:
868 free(vert_shader_list);
869}