blob: eb10f90a9112f7fb1fcfe1f6c83340e03eaff2ff [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"
Eric Anholt3d601232010-06-24 17:08:53 -070080extern "C" {
Ian Romanick019a59b2010-06-21 16:10:42 -070081#include "hash_table.h"
Eric Anholt3d601232010-06-24 17:08:53 -070082}
Ian Romanick832dfa52010-06-17 15:04:20 -070083
84/**
85 * Visitor that determines whether or not a variable is ever written.
86 */
87class find_assignment_visitor : public ir_hierarchical_visitor {
88public:
89 find_assignment_visitor(const char *name)
90 : name(name), found(false)
91 {
92 /* empty */
93 }
94
95 virtual ir_visitor_status visit_enter(ir_assignment *ir)
96 {
97 ir_variable *const var = ir->lhs->variable_referenced();
98
99 if (strcmp(name, var->name) == 0) {
100 found = true;
101 return visit_stop;
102 }
103
104 return visit_continue_with_parent;
105 }
106
107 bool variable_found()
108 {
109 return found;
110 }
111
112private:
113 const char *name; /**< Find writes to a variable with this name. */
114 bool found; /**< Was a write to the variable found? */
115};
116
Ian Romanickc93b8f12010-06-17 15:20:22 -0700117
Ian Romanick0ad22cd2010-06-21 17:18:31 -0700118void
Eric Anholt849e1812010-06-30 11:49:17 -0700119linker_error_printf(gl_shader_program *prog, const char *fmt, ...)
Ian Romanickf36460e2010-06-23 12:07:22 -0700120{
121 va_list ap;
122
123 prog->InfoLog = talloc_strdup_append(prog->InfoLog, "error: ");
124 va_start(ap, fmt);
125 prog->InfoLog = talloc_vasprintf_append(prog->InfoLog, fmt, ap);
126 va_end(ap);
127}
128
129
130void
Eric Anholt16b68b12010-06-30 11:05:43 -0700131invalidate_variable_locations(gl_shader *sh, enum ir_variable_mode mode,
Ian Romanick0ad22cd2010-06-21 17:18:31 -0700132 int generic_base)
133{
Eric Anholt16b68b12010-06-30 11:05:43 -0700134 foreach_list(node, sh->ir) {
Ian Romanick0ad22cd2010-06-21 17:18:31 -0700135 ir_variable *const var = ((ir_instruction *) node)->as_variable();
136
137 if ((var == NULL) || (var->mode != (unsigned) mode))
138 continue;
139
140 /* Only assign locations for generic attributes / varyings / etc.
141 */
142 if (var->location >= generic_base)
143 var->location = -1;
144 }
145}
146
147
Ian Romanickc93b8f12010-06-17 15:20:22 -0700148/**
Ian Romanick69846702010-06-22 17:29:19 -0700149 * Determine the number of attribute slots required for a particular type
150 *
151 * This code is here because it implements the language rules of a specific
152 * GLSL version. Since it's a property of the language and not a property of
153 * types in general, it doesn't really belong in glsl_type.
154 */
155unsigned
156count_attribute_slots(const glsl_type *t)
157{
158 /* From page 31 (page 37 of the PDF) of the GLSL 1.50 spec:
159 *
160 * "A scalar input counts the same amount against this limit as a vec4,
161 * so applications may want to consider packing groups of four
162 * unrelated float inputs together into a vector to better utilize the
163 * capabilities of the underlying hardware. A matrix input will use up
164 * multiple locations. The number of locations used will equal the
165 * number of columns in the matrix."
166 *
167 * The spec does not explicitly say how arrays are counted. However, it
168 * should be safe to assume the total number of slots consumed by an array
169 * is the number of entries in the array multiplied by the number of slots
170 * consumed by a single element of the array.
171 */
172
173 if (t->is_array())
174 return t->array_size() * count_attribute_slots(t->element_type());
175
176 if (t->is_matrix())
177 return t->matrix_columns;
178
179 return 1;
180}
181
182
183/**
Ian Romanickc93b8f12010-06-17 15:20:22 -0700184 * Verify that a vertex shader executable meets all semantic requirements
185 *
186 * \param shader Vertex shader executable to be verified
187 */
Ian Romanick832dfa52010-06-17 15:04:20 -0700188bool
Eric Anholt849e1812010-06-30 11:49:17 -0700189validate_vertex_shader_executable(struct gl_shader_program *prog,
Eric Anholt16b68b12010-06-30 11:05:43 -0700190 struct gl_shader *shader)
Ian Romanick832dfa52010-06-17 15:04:20 -0700191{
192 if (shader == NULL)
193 return true;
194
195 if (!shader->symbols->get_function("main")) {
Ian Romanickf36460e2010-06-23 12:07:22 -0700196 linker_error_printf(prog, "vertex shader lacks `main'\n");
Ian Romanick832dfa52010-06-17 15:04:20 -0700197 return false;
198 }
199
200 find_assignment_visitor find("gl_Position");
Eric Anholt16b68b12010-06-30 11:05:43 -0700201 find.run(shader->ir);
Ian Romanick832dfa52010-06-17 15:04:20 -0700202 if (!find.variable_found()) {
Ian Romanickf36460e2010-06-23 12:07:22 -0700203 linker_error_printf(prog,
204 "vertex shader does not write to `gl_Position'\n");
Ian Romanick832dfa52010-06-17 15:04:20 -0700205 return false;
206 }
207
208 return true;
209}
210
211
Ian Romanickc93b8f12010-06-17 15:20:22 -0700212/**
213 * Verify that a fragment shader executable meets all semantic requirements
214 *
215 * \param shader Fragment shader executable to be verified
216 */
Ian Romanick832dfa52010-06-17 15:04:20 -0700217bool
Eric Anholt849e1812010-06-30 11:49:17 -0700218validate_fragment_shader_executable(struct gl_shader_program *prog,
Eric Anholt16b68b12010-06-30 11:05:43 -0700219 struct gl_shader *shader)
Ian Romanick832dfa52010-06-17 15:04:20 -0700220{
221 if (shader == NULL)
222 return true;
223
224 if (!shader->symbols->get_function("main")) {
Ian Romanickf36460e2010-06-23 12:07:22 -0700225 linker_error_printf(prog, "fragment shader lacks `main'\n");
Ian Romanick832dfa52010-06-17 15:04:20 -0700226 return false;
227 }
228
229 find_assignment_visitor frag_color("gl_FragColor");
230 find_assignment_visitor frag_data("gl_FragData");
231
Eric Anholt16b68b12010-06-30 11:05:43 -0700232 frag_color.run(shader->ir);
233 frag_data.run(shader->ir);
Ian Romanick832dfa52010-06-17 15:04:20 -0700234
Ian Romanick832dfa52010-06-17 15:04:20 -0700235 if (frag_color.variable_found() && frag_data.variable_found()) {
Ian Romanickf36460e2010-06-23 12:07:22 -0700236 linker_error_printf(prog, "fragment shader writes to both "
237 "`gl_FragColor' and `gl_FragData'\n");
Ian Romanick832dfa52010-06-17 15:04:20 -0700238 return false;
239 }
240
241 return true;
242}
243
244
Ian Romanickcc22c5a2010-06-18 17:13:42 -0700245/**
246 * Perform validation of uniforms used across multiple shader stages
247 */
248bool
Eric Anholt849e1812010-06-30 11:49:17 -0700249cross_validate_uniforms(struct gl_shader_program *prog)
Ian Romanickcc22c5a2010-06-18 17:13:42 -0700250{
251 /* Examine all of the uniforms in all of the shaders and cross validate
252 * them.
253 */
254 glsl_symbol_table uniforms;
Ian Romanicked1fe3d2010-06-23 12:09:14 -0700255 for (unsigned i = 0; i < prog->_NumLinkedShaders; i++) {
Eric Anholt16b68b12010-06-30 11:05:43 -0700256 foreach_list(node, prog->_LinkedShaders[i]->ir) {
Ian Romanickcc22c5a2010-06-18 17:13:42 -0700257 ir_variable *const var = ((ir_instruction *) node)->as_variable();
258
259 if ((var == NULL) || (var->mode != ir_var_uniform))
260 continue;
261
262 /* If a uniform with this name has already been seen, verify that the
263 * new instance has the same type. In addition, if the uniforms have
264 * initializers, the values of the initializers must be the same.
265 */
266 ir_variable *const existing = uniforms.get_variable(var->name);
267 if (existing != NULL) {
268 if (var->type != existing->type) {
Ian Romanickf36460e2010-06-23 12:07:22 -0700269 linker_error_printf(prog, "uniform `%s' declared as type "
270 "`%s' and type `%s'\n",
271 var->name, var->type->name,
272 existing->type->name);
Ian Romanickcc22c5a2010-06-18 17:13:42 -0700273 return false;
274 }
275
276 if (var->constant_value != NULL) {
277 if (existing->constant_value != NULL) {
278 if (!var->constant_value->has_value(existing->constant_value)) {
Ian Romanickf36460e2010-06-23 12:07:22 -0700279 linker_error_printf(prog, "initializers for uniform "
280 "`%s' have differing values\n",
281 var->name);
Ian Romanickcc22c5a2010-06-18 17:13:42 -0700282 return false;
283 }
284 } else
285 /* If the first-seen instance of a particular uniform did not
286 * have an initializer but a later instance does, copy the
287 * initializer to the version stored in the symbol table.
288 */
Eric Anholt4b6fd392010-06-23 11:37:12 -0700289 existing->constant_value =
290 (ir_constant *)var->constant_value->clone(NULL);
Ian Romanickcc22c5a2010-06-18 17:13:42 -0700291 }
292 } else
293 uniforms.add_variable(var->name, var);
294 }
295 }
296
297 return true;
298}
299
300
Ian Romanick37101922010-06-18 19:02:10 -0700301/**
302 * Validate that outputs from one stage match inputs of another
303 */
304bool
Eric Anholt849e1812010-06-30 11:49:17 -0700305cross_validate_outputs_to_inputs(struct gl_shader_program *prog,
Eric Anholt16b68b12010-06-30 11:05:43 -0700306 gl_shader *producer, gl_shader *consumer)
Ian Romanick37101922010-06-18 19:02:10 -0700307{
308 glsl_symbol_table parameters;
309 /* FINISHME: Figure these out dynamically. */
310 const char *const producer_stage = "vertex";
311 const char *const consumer_stage = "fragment";
312
313 /* Find all shader outputs in the "producer" stage.
314 */
Eric Anholt16b68b12010-06-30 11:05:43 -0700315 foreach_list(node, producer->ir) {
Ian Romanick37101922010-06-18 19:02:10 -0700316 ir_variable *const var = ((ir_instruction *) node)->as_variable();
317
318 /* FINISHME: For geometry shaders, this should also look for inout
319 * FINISHME: variables.
320 */
321 if ((var == NULL) || (var->mode != ir_var_out))
322 continue;
323
324 parameters.add_variable(var->name, var);
325 }
326
327
328 /* Find all shader inputs in the "consumer" stage. Any variables that have
329 * matching outputs already in the symbol table must have the same type and
330 * qualifiers.
331 */
Eric Anholt16b68b12010-06-30 11:05:43 -0700332 foreach_list(node, consumer->ir) {
Ian Romanick37101922010-06-18 19:02:10 -0700333 ir_variable *const input = ((ir_instruction *) node)->as_variable();
334
335 /* FINISHME: For geometry shaders, this should also look for inout
336 * FINISHME: variables.
337 */
338 if ((input == NULL) || (input->mode != ir_var_in))
339 continue;
340
341 ir_variable *const output = parameters.get_variable(input->name);
342 if (output != NULL) {
343 /* Check that the types match between stages.
344 */
345 if (input->type != output->type) {
Ian Romanickf36460e2010-06-23 12:07:22 -0700346 linker_error_printf(prog,
347 "%s shader output `%s' delcared as "
348 "type `%s', but %s shader input declared "
349 "as type `%s'\n",
350 producer_stage, output->name,
351 output->type->name,
352 consumer_stage, input->type->name);
Ian Romanick37101922010-06-18 19:02:10 -0700353 return false;
354 }
355
356 /* Check that all of the qualifiers match between stages.
357 */
358 if (input->centroid != output->centroid) {
Ian Romanickf36460e2010-06-23 12:07:22 -0700359 linker_error_printf(prog,
360 "%s shader output `%s' %s centroid qualifier, "
361 "but %s shader input %s centroid qualifier\n",
362 producer_stage,
363 output->name,
364 (output->centroid) ? "has" : "lacks",
365 consumer_stage,
366 (input->centroid) ? "has" : "lacks");
Ian Romanick37101922010-06-18 19:02:10 -0700367 return false;
368 }
369
370 if (input->invariant != output->invariant) {
Ian Romanickf36460e2010-06-23 12:07:22 -0700371 linker_error_printf(prog,
372 "%s shader output `%s' %s invariant qualifier, "
373 "but %s shader input %s invariant qualifier\n",
374 producer_stage,
375 output->name,
376 (output->invariant) ? "has" : "lacks",
377 consumer_stage,
378 (input->invariant) ? "has" : "lacks");
Ian Romanick37101922010-06-18 19:02:10 -0700379 return false;
380 }
381
382 if (input->interpolation != output->interpolation) {
Ian Romanickf36460e2010-06-23 12:07:22 -0700383 linker_error_printf(prog,
384 "%s shader output `%s' specifies %s "
385 "interpolation qualifier, "
386 "but %s shader input specifies %s "
387 "interpolation qualifier\n",
388 producer_stage,
389 output->name,
390 output->interpolation_string(),
391 consumer_stage,
392 input->interpolation_string());
Ian Romanick37101922010-06-18 19:02:10 -0700393 return false;
394 }
395 }
396 }
397
398 return true;
399}
400
401
Ian Romanick019a59b2010-06-21 16:10:42 -0700402struct uniform_node {
403 exec_node link;
404 struct gl_uniform *u;
405 unsigned slots;
406};
407
Ian Romanickabee16e2010-06-21 16:16:05 -0700408void
Eric Anholt849e1812010-06-30 11:49:17 -0700409assign_uniform_locations(struct gl_shader_program *prog)
Ian Romanick019a59b2010-06-21 16:10:42 -0700410{
411 /* */
412 exec_list uniforms;
413 unsigned total_uniforms = 0;
414 hash_table *ht = hash_table_ctor(32, hash_table_string_hash,
415 hash_table_string_compare);
416
Ian Romanickabee16e2010-06-21 16:16:05 -0700417 for (unsigned i = 0; i < prog->_NumLinkedShaders; i++) {
Ian Romanick019a59b2010-06-21 16:10:42 -0700418 unsigned next_position = 0;
419
Eric Anholt16b68b12010-06-30 11:05:43 -0700420 foreach_list(node, prog->_LinkedShaders[i]->ir) {
Ian Romanick019a59b2010-06-21 16:10:42 -0700421 ir_variable *const var = ((ir_instruction *) node)->as_variable();
422
423 if ((var == NULL) || (var->mode != ir_var_uniform))
424 continue;
425
426 const unsigned vec4_slots = (var->component_slots() + 3) / 4;
427 assert(vec4_slots != 0);
428
429 uniform_node *n = (uniform_node *) hash_table_find(ht, var->name);
430 if (n == NULL) {
431 n = (uniform_node *) calloc(1, sizeof(struct uniform_node));
432 n->u = (gl_uniform *) calloc(vec4_slots, sizeof(struct gl_uniform));
433 n->slots = vec4_slots;
434
435 n->u[0].Name = strdup(var->name);
436 for (unsigned j = 1; j < vec4_slots; j++)
437 n->u[j].Name = n->u[0].Name;
438
439 hash_table_insert(ht, n, n->u[0].Name);
440 uniforms.push_tail(& n->link);
441 total_uniforms += vec4_slots;
442 }
443
444 if (var->constant_value != NULL)
445 for (unsigned j = 0; j < vec4_slots; j++)
446 n->u[j].Initialized = true;
447
448 var->location = next_position;
449
450 for (unsigned j = 0; j < vec4_slots; j++) {
Ian Romanickabee16e2010-06-21 16:16:05 -0700451 switch (prog->_LinkedShaders[i]->Type) {
Ian Romanick019a59b2010-06-21 16:10:42 -0700452 case GL_VERTEX_SHADER:
453 n->u[j].VertPos = next_position;
454 break;
455 case GL_FRAGMENT_SHADER:
456 n->u[j].FragPos = next_position;
457 break;
458 case GL_GEOMETRY_SHADER:
459 /* FINISHME: Support geometry shaders. */
Ian Romanickabee16e2010-06-21 16:16:05 -0700460 assert(prog->_LinkedShaders[i]->Type != GL_GEOMETRY_SHADER);
Ian Romanick019a59b2010-06-21 16:10:42 -0700461 break;
462 }
463
464 next_position++;
465 }
466 }
467 }
468
469 gl_uniform_list *ul = (gl_uniform_list *)
470 calloc(1, sizeof(gl_uniform_list));
471
472 ul->Size = total_uniforms;
473 ul->NumUniforms = total_uniforms;
474 ul->Uniforms = (gl_uniform *) calloc(total_uniforms, sizeof(gl_uniform));
475
476 unsigned idx = 0;
477 uniform_node *next;
478 for (uniform_node *node = (uniform_node *) uniforms.head
479 ; node->link.next != NULL
480 ; node = next) {
481 next = (uniform_node *) node->link.next;
482
483 node->link.remove();
484 memcpy(&ul->Uniforms[idx], node->u, sizeof(gl_uniform) * node->slots);
485 idx += node->slots;
486
487 free(node->u);
488 free(node);
489 }
490
491 hash_table_dtor(ht);
492
Ian Romanickabee16e2010-06-21 16:16:05 -0700493 prog->Uniforms = ul;
Ian Romanick019a59b2010-06-21 16:10:42 -0700494}
495
496
Ian Romanick69846702010-06-22 17:29:19 -0700497/**
498 * Find a contiguous set of available bits in a bitmask
499 *
500 * \param used_mask Bits representing used (1) and unused (0) locations
501 * \param needed_count Number of contiguous bits needed.
502 *
503 * \return
504 * Base location of the available bits on success or -1 on failure.
505 */
506int
507find_available_slots(unsigned used_mask, unsigned needed_count)
508{
509 unsigned needed_mask = (1 << needed_count) - 1;
510 const int max_bit_to_test = (8 * sizeof(used_mask)) - needed_count;
511
512 /* The comparison to 32 is redundant, but without it GCC emits "warning:
513 * cannot optimize possibly infinite loops" for the loop below.
514 */
515 if ((needed_count == 0) || (max_bit_to_test < 0) || (max_bit_to_test > 32))
516 return -1;
517
518 for (int i = 0; i <= max_bit_to_test; i++) {
519 if ((needed_mask & ~used_mask) == needed_mask)
520 return i;
521
522 needed_mask <<= 1;
523 }
524
525 return -1;
526}
527
528
529bool
Eric Anholt849e1812010-06-30 11:49:17 -0700530assign_attribute_locations(gl_shader_program *prog, unsigned max_attribute_index)
Ian Romanick0ad22cd2010-06-21 17:18:31 -0700531{
Ian Romanick9342d262010-06-22 17:41:37 -0700532 /* Mark invalid attribute locations as being used.
533 */
534 unsigned used_locations = (max_attribute_index >= 32)
535 ? ~0 : ~((1 << max_attribute_index) - 1);
Ian Romanick0ad22cd2010-06-21 17:18:31 -0700536
Eric Anholt16b68b12010-06-30 11:05:43 -0700537 gl_shader *const sh = prog->_LinkedShaders[0];
Ian Romanick0ad22cd2010-06-21 17:18:31 -0700538 assert(sh->Type == GL_VERTEX_SHADER);
539
Ian Romanick69846702010-06-22 17:29:19 -0700540 /* Operate in a total of four passes.
Ian Romanick0ad22cd2010-06-21 17:18:31 -0700541 *
542 * 1. Invalidate the location assignments for all vertex shader inputs.
543 *
544 * 2. Assign locations for inputs that have user-defined (via
545 * glBindVertexAttribLocation) locatoins.
546 *
Ian Romanick69846702010-06-22 17:29:19 -0700547 * 3. Sort the attributes without assigned locations by number of slots
548 * required in decreasing order. Fragmentation caused by attribute
549 * locations assigned by the application may prevent large attributes
550 * from having enough contiguous space.
551 *
552 * 4. Assign locations to any inputs without assigned locations.
Ian Romanick0ad22cd2010-06-21 17:18:31 -0700553 */
554
555 invalidate_variable_locations(sh, ir_var_in, VERT_ATTRIB_GENERIC0);
556
Ian Romanick553dcdc2010-06-23 12:14:02 -0700557 if (prog->Attributes != NULL) {
558 for (unsigned i = 0; i < prog->Attributes->NumParameters; i++) {
Ian Romanick0ad22cd2010-06-21 17:18:31 -0700559 ir_variable *const var =
Ian Romanick553dcdc2010-06-23 12:14:02 -0700560 sh->symbols->get_variable(prog->Attributes->Parameters[i].Name);
Ian Romanick0ad22cd2010-06-21 17:18:31 -0700561
Ian Romanick69846702010-06-22 17:29:19 -0700562 /* Note: attributes that occupy multiple slots, such as arrays or
563 * matrices, may appear in the attrib array multiple times.
564 */
565 if ((var == NULL) || (var->location != -1))
Ian Romanick0ad22cd2010-06-21 17:18:31 -0700566 continue;
567
Ian Romanick69846702010-06-22 17:29:19 -0700568 /* From page 61 of the OpenGL 4.0 spec:
569 *
570 * "LinkProgram will fail if the attribute bindings assigned by
571 * BindAttribLocation do not leave not enough space to assign a
572 * location for an active matrix attribute or an active attribute
573 * array, both of which require multiple contiguous generic
574 * attributes."
575 *
576 * Previous versions of the spec contain similar language but omit the
577 * bit about attribute arrays.
578 *
579 * Page 61 of the OpenGL 4.0 spec also says:
580 *
581 * "It is possible for an application to bind more than one
582 * attribute name to the same location. This is referred to as
583 * aliasing. This will only work if only one of the aliased
584 * attributes is active in the executable program, or if no path
585 * through the shader consumes more than one attribute of a set
586 * of attributes aliased to the same location. A link error can
587 * occur if the linker determines that every path through the
588 * shader consumes multiple aliased attributes, but
589 * implementations are not required to generate an error in this
590 * case."
591 *
592 * These two paragraphs are either somewhat contradictory, or I don't
593 * fully understand one or both of them.
594 */
595 /* FINISHME: The code as currently written does not support attribute
596 * FINISHME: location aliasing (see comment above).
597 */
Ian Romanick553dcdc2010-06-23 12:14:02 -0700598 const int attr = prog->Attributes->Parameters[i].StateIndexes[0];
Ian Romanick69846702010-06-22 17:29:19 -0700599 const unsigned slots = count_attribute_slots(var->type);
600
601 /* Mask representing the contiguous slots that will be used by this
602 * attribute.
603 */
604 const unsigned use_mask = (1 << slots) - 1;
605
606 /* Generate a link error if the set of bits requested for this
607 * attribute overlaps any previously allocated bits.
608 */
609 if ((~(use_mask << attr) & used_locations) != used_locations) {
Ian Romanick553dcdc2010-06-23 12:14:02 -0700610 linker_error_printf(prog,
611 "insufficient contiguous attribute locations "
612 "available for vertex shader input `%s'",
613 var->name);
Ian Romanick69846702010-06-22 17:29:19 -0700614 return false;
615 }
Ian Romanick0ad22cd2010-06-21 17:18:31 -0700616
617 var->location = VERT_ATTRIB_GENERIC0 + attr;
Ian Romanick69846702010-06-22 17:29:19 -0700618 used_locations |= (use_mask << attr);
Ian Romanick0ad22cd2010-06-21 17:18:31 -0700619 }
620 }
621
Ian Romanick69846702010-06-22 17:29:19 -0700622 /* Temporary storage for the set of attributes that need locations assigned.
623 */
624 struct temp_attr {
625 unsigned slots;
626 ir_variable *var;
627
628 /* Used below in the call to qsort. */
629 static int compare(const void *a, const void *b)
630 {
631 const temp_attr *const l = (const temp_attr *) a;
632 const temp_attr *const r = (const temp_attr *) b;
633
634 /* Reversed because we want a descending order sort below. */
635 return r->slots - l->slots;
636 }
637 } to_assign[16];
638
639 unsigned num_attr = 0;
640
Eric Anholt16b68b12010-06-30 11:05:43 -0700641 foreach_list(node, sh->ir) {
Ian Romanick0ad22cd2010-06-21 17:18:31 -0700642 ir_variable *const var = ((ir_instruction *) node)->as_variable();
643
644 if ((var == NULL) || (var->mode != ir_var_in))
645 continue;
646
647 /* The location was explicitly assigned, nothing to do here.
648 */
649 if (var->location != -1)
650 continue;
651
Ian Romanick69846702010-06-22 17:29:19 -0700652 to_assign[num_attr].slots = count_attribute_slots(var->type);
653 to_assign[num_attr].var = var;
654 num_attr++;
Ian Romanick0ad22cd2010-06-21 17:18:31 -0700655 }
Ian Romanick69846702010-06-22 17:29:19 -0700656
657 /* If all of the attributes were assigned locations by the application (or
658 * are built-in attributes with fixed locations), return early. This should
659 * be the common case.
660 */
661 if (num_attr == 0)
662 return true;
663
664 qsort(to_assign, num_attr, sizeof(to_assign[0]), temp_attr::compare);
665
Ian Romanick982e3792010-06-29 18:58:20 -0700666 /* VERT_ATTRIB_GENERIC0 is a psdueo-alias for VERT_ATTRIB_POS. It can only
667 * be explicitly assigned by via glBindAttribLocation. Mark it as reserved
668 * to prevent it from being automatically allocated below.
669 */
Ian Romanick35c89202010-07-07 16:28:39 -0700670 used_locations |= (1 << 0);
Ian Romanick982e3792010-06-29 18:58:20 -0700671
Ian Romanick69846702010-06-22 17:29:19 -0700672 for (unsigned i = 0; i < num_attr; i++) {
673 /* Mask representing the contiguous slots that will be used by this
674 * attribute.
675 */
676 const unsigned use_mask = (1 << to_assign[i].slots) - 1;
677
678 int location = find_available_slots(used_locations, to_assign[i].slots);
679
680 if (location < 0) {
Ian Romanick553dcdc2010-06-23 12:14:02 -0700681 linker_error_printf(prog,
682 "insufficient contiguous attribute locations "
683 "available for vertex shader input `%s'",
684 to_assign[i].var->name);
Ian Romanick69846702010-06-22 17:29:19 -0700685 return false;
686 }
687
688 to_assign[i].var->location = VERT_ATTRIB_GENERIC0 + location;
689 used_locations |= (use_mask << location);
690 }
691
692 return true;
Ian Romanick0ad22cd2010-06-21 17:18:31 -0700693}
694
695
696void
Eric Anholt16b68b12010-06-30 11:05:43 -0700697assign_varying_locations(gl_shader *producer, gl_shader *consumer)
Ian Romanick0e59b262010-06-23 11:23:01 -0700698{
699 /* FINISHME: Set dynamically when geometry shader support is added. */
700 unsigned output_index = VERT_RESULT_VAR0;
701 unsigned input_index = FRAG_ATTRIB_VAR0;
702
703 /* Operate in a total of three passes.
704 *
705 * 1. Assign locations for any matching inputs and outputs.
706 *
707 * 2. Mark output variables in the producer that do not have locations as
708 * not being outputs. This lets the optimizer eliminate them.
709 *
710 * 3. Mark input variables in the consumer that do not have locations as
711 * not being inputs. This lets the optimizer eliminate them.
712 */
713
714 invalidate_variable_locations(producer, ir_var_out, VERT_RESULT_VAR0);
715 invalidate_variable_locations(consumer, ir_var_in, FRAG_ATTRIB_VAR0);
716
Eric Anholt16b68b12010-06-30 11:05:43 -0700717 foreach_list(node, producer->ir) {
Ian Romanick0e59b262010-06-23 11:23:01 -0700718 ir_variable *const output_var = ((ir_instruction *) node)->as_variable();
719
720 if ((output_var == NULL) || (output_var->mode != ir_var_out)
721 || (output_var->location != -1))
722 continue;
723
724 ir_variable *const input_var =
725 consumer->symbols->get_variable(output_var->name);
726
727 if ((input_var == NULL) || (input_var->mode != ir_var_in))
728 continue;
729
730 assert(input_var->location == -1);
731
732 /* FINISHME: Location assignment will need some changes when arrays,
733 * FINISHME: matrices, and structures are allowed as shader inputs /
734 * FINISHME: outputs.
735 */
736 output_var->location = output_index;
737 input_var->location = input_index;
738
739 output_index++;
740 input_index++;
741 }
742
Eric Anholt16b68b12010-06-30 11:05:43 -0700743 foreach_list(node, producer->ir) {
Ian Romanick0e59b262010-06-23 11:23:01 -0700744 ir_variable *const var = ((ir_instruction *) node)->as_variable();
745
746 if ((var == NULL) || (var->mode != ir_var_out))
747 continue;
748
749 /* An 'out' variable is only really a shader output if its value is read
750 * by the following stage.
751 */
752 var->shader_out = (var->location != -1);
753 }
754
Eric Anholt16b68b12010-06-30 11:05:43 -0700755 foreach_list(node, consumer->ir) {
Ian Romanick0e59b262010-06-23 11:23:01 -0700756 ir_variable *const var = ((ir_instruction *) node)->as_variable();
757
758 if ((var == NULL) || (var->mode != ir_var_in))
759 continue;
760
761 /* An 'in' variable is only really a shader input if its value is written
762 * by the previous stage.
763 */
764 var->shader_in = (var->location != -1);
765 }
766}
767
768
769void
Eric Anholt849e1812010-06-30 11:49:17 -0700770link_shaders(struct gl_shader_program *prog)
Ian Romanick832dfa52010-06-17 15:04:20 -0700771{
772 prog->LinkStatus = false;
773 prog->Validated = false;
774 prog->_Used = false;
775
Ian Romanickf36460e2010-06-23 12:07:22 -0700776 if (prog->InfoLog != NULL)
777 talloc_free(prog->InfoLog);
778
779 prog->InfoLog = talloc_strdup(NULL, "");
780
Ian Romanick832dfa52010-06-17 15:04:20 -0700781 /* Separate the shaders into groups based on their type.
782 */
Eric Anholt16b68b12010-06-30 11:05:43 -0700783 struct gl_shader **vert_shader_list;
Ian Romanick832dfa52010-06-17 15:04:20 -0700784 unsigned num_vert_shaders = 0;
Eric Anholt16b68b12010-06-30 11:05:43 -0700785 struct gl_shader **frag_shader_list;
Ian Romanick832dfa52010-06-17 15:04:20 -0700786 unsigned num_frag_shaders = 0;
787
Eric Anholt16b68b12010-06-30 11:05:43 -0700788 vert_shader_list = (struct gl_shader **)
789 calloc(2 * prog->NumShaders, sizeof(struct gl_shader *));
Ian Romanick832dfa52010-06-17 15:04:20 -0700790 frag_shader_list = &vert_shader_list[prog->NumShaders];
791
792 for (unsigned i = 0; i < prog->NumShaders; i++) {
793 switch (prog->Shaders[i]->Type) {
794 case GL_VERTEX_SHADER:
795 vert_shader_list[num_vert_shaders] = prog->Shaders[i];
796 num_vert_shaders++;
797 break;
798 case GL_FRAGMENT_SHADER:
799 frag_shader_list[num_frag_shaders] = prog->Shaders[i];
800 num_frag_shaders++;
801 break;
802 case GL_GEOMETRY_SHADER:
803 /* FINISHME: Support geometry shaders. */
804 assert(prog->Shaders[i]->Type != GL_GEOMETRY_SHADER);
805 break;
806 }
807 }
808
809 /* FINISHME: Implement intra-stage linking. */
810 assert(num_vert_shaders <= 1);
811 assert(num_frag_shaders <= 1);
812
813 /* Verify that each of the per-target executables is valid.
814 */
Ian Romanickf36460e2010-06-23 12:07:22 -0700815 if (!validate_vertex_shader_executable(prog, vert_shader_list[0])
816 || !validate_fragment_shader_executable(prog, frag_shader_list[0]))
Ian Romanick832dfa52010-06-17 15:04:20 -0700817 goto done;
818
819
Ian Romanickabee16e2010-06-21 16:16:05 -0700820 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 Romanick3ed850e2010-06-23 12:18:21 -0700832 /* Here begins the inter-stage linking phase. Some initial validation is
833 * performed, then locations are assigned for uniforms, attributes, and
834 * varyings.
835 */
Ian Romanicked1fe3d2010-06-23 12:09:14 -0700836 if (cross_validate_uniforms(prog)) {
Ian Romanick37101922010-06-18 19:02:10 -0700837 /* Validate the inputs of each stage with the output of the preceeding
838 * stage.
839 */
Ian Romanickabee16e2010-06-21 16:16:05 -0700840 for (unsigned i = 1; i < prog->_NumLinkedShaders; i++) {
Ian Romanickf36460e2010-06-23 12:07:22 -0700841 if (!cross_validate_outputs_to_inputs(prog,
842 prog->_LinkedShaders[i - 1],
Ian Romanickabee16e2010-06-21 16:16:05 -0700843 prog->_LinkedShaders[i]))
Ian Romanick37101922010-06-18 19:02:10 -0700844 goto done;
845 }
846
Ian Romanickcc22c5a2010-06-18 17:13:42 -0700847 prog->LinkStatus = true;
Ian Romanick37101922010-06-18 19:02:10 -0700848 }
Ian Romanick832dfa52010-06-17 15:04:20 -0700849
Ian Romanick13e10e42010-06-21 12:03:24 -0700850 /* FINISHME: Perform whole-program optimization here. */
851
Ian Romanickabee16e2010-06-21 16:16:05 -0700852 assign_uniform_locations(prog);
Ian Romanick13e10e42010-06-21 12:03:24 -0700853
Ian Romanick0ad22cd2010-06-21 17:18:31 -0700854 if (prog->_LinkedShaders[0]->Type == GL_VERTEX_SHADER)
Ian Romanick9342d262010-06-22 17:41:37 -0700855 /* FINISHME: The value of the max_attribute_index parameter is
856 * FINISHME: implementation dependent based on the value of
857 * FINISHME: GL_MAX_VERTEX_ATTRIBS. GL_MAX_VERTEX_ATTRIBS must be
858 * FINISHME: at least 16, so hardcode 16 for now.
859 */
Ian Romanick553dcdc2010-06-23 12:14:02 -0700860 if (!assign_attribute_locations(prog, 16))
Ian Romanick69846702010-06-22 17:29:19 -0700861 goto done;
Ian Romanick13e10e42010-06-21 12:03:24 -0700862
Ian Romanick0e59b262010-06-23 11:23:01 -0700863 for (unsigned i = 1; i < prog->_NumLinkedShaders; i++)
864 assign_varying_locations(prog->_LinkedShaders[i - 1],
865 prog->_LinkedShaders[i]);
Ian Romanick13e10e42010-06-21 12:03:24 -0700866
867 /* FINISHME: Assign fragment shader output locations. */
868
Ian Romanick832dfa52010-06-17 15:04:20 -0700869done:
870 free(vert_shader_list);
871}