Subversion Repositories Kolibri OS

Rev

Go to most recent revision | Details | Last modification | View Log | RSS feed

Rev Author Line No. Line
1901 serge 1
/*
2
 * Copyright © 2010 Intel Corporation
3
 *
4
 * Permission is hereby granted, free of charge, to any person obtaining a
5
 * copy of this software and associated documentation files (the "Software"),
6
 * to deal in the Software without restriction, including without limitation
7
 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8
 * and/or sell copies of the Software, and to permit persons to whom the
9
 * Software is furnished to do so, subject to the following conditions:
10
 *
11
 * The above copyright notice and this permission notice (including the next
12
 * paragraph) shall be included in all copies or substantial portions of the
13
 * Software.
14
 *
15
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
18
 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
20
 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
21
 * DEALINGS IN THE SOFTWARE.
22
 */
23
 
24
/**
25
 * \file linker.cpp
26
 * GLSL linker implementation
27
 *
28
 * Given a set of shaders that are to be linked to generate a final program,
29
 * there are three distinct stages.
30
 *
31
 * In the first stage shaders are partitioned into groups based on the shader
32
 * type.  All shaders of a particular type (e.g., vertex shaders) are linked
33
 * together.
34
 *
35
 *   - Undefined references in each shader are resolve to definitions in
36
 *     another shader.
37
 *   - Types and qualifiers of uniforms, outputs, and global variables defined
38
 *     in multiple shaders with the same name are verified to be the same.
39
 *   - Initializers for uniforms and global variables defined
40
 *     in multiple shaders with the same name are verified to be the same.
41
 *
42
 * The result, in the terminology of the GLSL spec, is a set of shader
43
 * executables for each processing unit.
44
 *
45
 * After the first stage is complete, a series of semantic checks are performed
46
 * on each of the shader executables.
47
 *
48
 *   - Each shader executable must define a \c main function.
49
 *   - Each vertex shader executable must write to \c gl_Position.
50
 *   - Each fragment shader executable must write to either \c gl_FragData or
51
 *     \c gl_FragColor.
52
 *
53
 * In the final stage individual shader executables are linked to create a
54
 * complete exectuable.
55
 *
56
 *   - Types of uniforms defined in multiple shader stages with the same name
57
 *     are verified to be the same.
58
 *   - Initializers for uniforms defined in multiple shader stages with the
59
 *     same name are verified to be the same.
60
 *   - Types and qualifiers of outputs defined in one stage are verified to
61
 *     be the same as the types and qualifiers of inputs defined with the same
62
 *     name in a later stage.
63
 *
64
 * \author Ian Romanick 
65
 */
66
#include 
67
#include 
68
#include 
69
#include 
70
 
71
#include "main/core.h"
72
#include "glsl_symbol_table.h"
73
#include "ir.h"
74
#include "program.h"
75
#include "program/hash_table.h"
76
#include "linker.h"
77
#include "ir_optimization.h"
78
 
79
extern "C" {
80
#include "main/shaderobj.h"
81
}
82
 
83
/**
84
 * Visitor that determines whether or not a variable is ever written.
85
 */
86
class find_assignment_visitor : public ir_hierarchical_visitor {
87
public:
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
   virtual ir_visitor_status visit_enter(ir_call *ir)
107
   {
108
      exec_list_iterator sig_iter = ir->get_callee()->parameters.iterator();
109
      foreach_iter(exec_list_iterator, iter, *ir) {
110
	 ir_rvalue *param_rval = (ir_rvalue *)iter.get();
111
	 ir_variable *sig_param = (ir_variable *)sig_iter.get();
112
 
113
	 if (sig_param->mode == ir_var_out ||
114
	     sig_param->mode == ir_var_inout) {
115
	    ir_variable *var = param_rval->variable_referenced();
116
	    if (var && strcmp(name, var->name) == 0) {
117
	       found = true;
118
	       return visit_stop;
119
	    }
120
	 }
121
	 sig_iter.next();
122
      }
123
 
124
      return visit_continue_with_parent;
125
   }
126
 
127
   bool variable_found()
128
   {
129
      return found;
130
   }
131
 
132
private:
133
   const char *name;       /**< Find writes to a variable with this name. */
134
   bool found;             /**< Was a write to the variable found? */
135
};
136
 
137
 
138
/**
139
 * Visitor that determines whether or not a variable is ever read.
140
 */
141
class find_deref_visitor : public ir_hierarchical_visitor {
142
public:
143
   find_deref_visitor(const char *name)
144
      : name(name), found(false)
145
   {
146
      /* empty */
147
   }
148
 
149
   virtual ir_visitor_status visit(ir_dereference_variable *ir)
150
   {
151
      if (strcmp(this->name, ir->var->name) == 0) {
152
	 this->found = true;
153
	 return visit_stop;
154
      }
155
 
156
      return visit_continue;
157
   }
158
 
159
   bool variable_found() const
160
   {
161
      return this->found;
162
   }
163
 
164
private:
165
   const char *name;       /**< Find writes to a variable with this name. */
166
   bool found;             /**< Was a write to the variable found? */
167
};
168
 
169
 
170
void
171
linker_error_printf(gl_shader_program *prog, const char *fmt, ...)
172
{
173
   va_list ap;
174
 
175
   ralloc_strcat(&prog->InfoLog, "error: ");
176
   va_start(ap, fmt);
177
   ralloc_vasprintf_append(&prog->InfoLog, fmt, ap);
178
   va_end(ap);
179
}
180
 
181
 
182
void
183
invalidate_variable_locations(gl_shader *sh, enum ir_variable_mode mode,
184
			      int generic_base)
185
{
186
   foreach_list(node, sh->ir) {
187
      ir_variable *const var = ((ir_instruction *) node)->as_variable();
188
 
189
      if ((var == NULL) || (var->mode != (unsigned) mode))
190
	 continue;
191
 
192
      /* Only assign locations for generic attributes / varyings / etc.
193
       */
194
      if ((var->location >= generic_base) && !var->explicit_location)
195
	  var->location = -1;
196
   }
197
}
198
 
199
 
200
/**
201
 * Determine the number of attribute slots required for a particular type
202
 *
203
 * This code is here because it implements the language rules of a specific
204
 * GLSL version.  Since it's a property of the language and not a property of
205
 * types in general, it doesn't really belong in glsl_type.
206
 */
207
unsigned
208
count_attribute_slots(const glsl_type *t)
209
{
210
   /* From page 31 (page 37 of the PDF) of the GLSL 1.50 spec:
211
    *
212
    *     "A scalar input counts the same amount against this limit as a vec4,
213
    *     so applications may want to consider packing groups of four
214
    *     unrelated float inputs together into a vector to better utilize the
215
    *     capabilities of the underlying hardware. A matrix input will use up
216
    *     multiple locations.  The number of locations used will equal the
217
    *     number of columns in the matrix."
218
    *
219
    * The spec does not explicitly say how arrays are counted.  However, it
220
    * should be safe to assume the total number of slots consumed by an array
221
    * is the number of entries in the array multiplied by the number of slots
222
    * consumed by a single element of the array.
223
    */
224
 
225
   if (t->is_array())
226
      return t->array_size() * count_attribute_slots(t->element_type());
227
 
228
   if (t->is_matrix())
229
      return t->matrix_columns;
230
 
231
   return 1;
232
}
233
 
234
 
235
/**
236
 * Verify that a vertex shader executable meets all semantic requirements
237
 *
238
 * \param shader  Vertex shader executable to be verified
239
 */
240
bool
241
validate_vertex_shader_executable(struct gl_shader_program *prog,
242
				  struct gl_shader *shader)
243
{
244
   if (shader == NULL)
245
      return true;
246
 
247
   find_assignment_visitor find("gl_Position");
248
   find.run(shader->ir);
249
   if (!find.variable_found()) {
250
      linker_error_printf(prog,
251
			  "vertex shader does not write to `gl_Position'\n");
252
      return false;
253
   }
254
 
255
   return true;
256
}
257
 
258
 
259
/**
260
 * Verify that a fragment shader executable meets all semantic requirements
261
 *
262
 * \param shader  Fragment shader executable to be verified
263
 */
264
bool
265
validate_fragment_shader_executable(struct gl_shader_program *prog,
266
				    struct gl_shader *shader)
267
{
268
   if (shader == NULL)
269
      return true;
270
 
271
   find_assignment_visitor frag_color("gl_FragColor");
272
   find_assignment_visitor frag_data("gl_FragData");
273
 
274
   frag_color.run(shader->ir);
275
   frag_data.run(shader->ir);
276
 
277
   if (frag_color.variable_found() && frag_data.variable_found()) {
278
      linker_error_printf(prog,  "fragment shader writes to both "
279
			  "`gl_FragColor' and `gl_FragData'\n");
280
      return false;
281
   }
282
 
283
   return true;
284
}
285
 
286
 
287
/**
288
 * Generate a string describing the mode of a variable
289
 */
290
static const char *
291
mode_string(const ir_variable *var)
292
{
293
   switch (var->mode) {
294
   case ir_var_auto:
295
      return (var->read_only) ? "global constant" : "global variable";
296
 
297
   case ir_var_uniform: return "uniform";
298
   case ir_var_in:      return "shader input";
299
   case ir_var_out:     return "shader output";
300
   case ir_var_inout:   return "shader inout";
301
 
302
   case ir_var_temporary:
303
   default:
304
      assert(!"Should not get here.");
305
      return "invalid variable";
306
   }
307
}
308
 
309
 
310
/**
311
 * Perform validation of global variables used across multiple shaders
312
 */
313
bool
314
cross_validate_globals(struct gl_shader_program *prog,
315
		       struct gl_shader **shader_list,
316
		       unsigned num_shaders,
317
		       bool uniforms_only)
318
{
319
   /* Examine all of the uniforms in all of the shaders and cross validate
320
    * them.
321
    */
322
   glsl_symbol_table variables;
323
   for (unsigned i = 0; i < num_shaders; i++) {
324
      if (shader_list[i] == NULL)
325
	 continue;
326
 
327
      foreach_list(node, shader_list[i]->ir) {
328
	 ir_variable *const var = ((ir_instruction *) node)->as_variable();
329
 
330
	 if (var == NULL)
331
	    continue;
332
 
333
	 if (uniforms_only && (var->mode != ir_var_uniform))
334
	    continue;
335
 
336
	 /* Don't cross validate temporaries that are at global scope.  These
337
	  * will eventually get pulled into the shaders 'main'.
338
	  */
339
	 if (var->mode == ir_var_temporary)
340
	    continue;
341
 
342
	 /* If a global with this name has already been seen, verify that the
343
	  * new instance has the same type.  In addition, if the globals have
344
	  * initializers, the values of the initializers must be the same.
345
	  */
346
	 ir_variable *const existing = variables.get_variable(var->name);
347
	 if (existing != NULL) {
348
	    if (var->type != existing->type) {
349
	       /* Consider the types to be "the same" if both types are arrays
350
		* of the same type and one of the arrays is implicitly sized.
351
		* In addition, set the type of the linked variable to the
352
		* explicitly sized array.
353
		*/
354
	       if (var->type->is_array()
355
		   && existing->type->is_array()
356
		   && (var->type->fields.array == existing->type->fields.array)
357
		   && ((var->type->length == 0)
358
		       || (existing->type->length == 0))) {
359
		  if (var->type->length != 0) {
360
		     existing->type = var->type;
361
		  }
362
	       } else {
363
		  linker_error_printf(prog, "%s `%s' declared as type "
364
				      "`%s' and type `%s'\n",
365
				      mode_string(var),
366
				      var->name, var->type->name,
367
				      existing->type->name);
368
		  return false;
369
	       }
370
	    }
371
 
372
	    if (var->explicit_location) {
373
	       if (existing->explicit_location
374
		   && (var->location != existing->location)) {
375
		     linker_error_printf(prog, "explicit locations for %s "
376
					 "`%s' have differing values\n",
377
					 mode_string(var), var->name);
378
		     return false;
379
	       }
380
 
381
	       existing->location = var->location;
382
	       existing->explicit_location = true;
383
	    }
384
 
385
	    /* FINISHME: Handle non-constant initializers.
386
	     */
387
	    if (var->constant_value != NULL) {
388
	       if (existing->constant_value != NULL) {
389
		  if (!var->constant_value->has_value(existing->constant_value)) {
390
		     linker_error_printf(prog, "initializers for %s "
391
					 "`%s' have differing values\n",
392
					 mode_string(var), var->name);
393
		     return false;
394
		  }
395
	       } else
396
		  /* If the first-seen instance of a particular uniform did not
397
		   * have an initializer but a later instance does, copy the
398
		   * initializer to the version stored in the symbol table.
399
		   */
400
		  /* FINISHME: This is wrong.  The constant_value field should
401
		   * FINISHME: not be modified!  Imagine a case where a shader
402
		   * FINISHME: without an initializer is linked in two different
403
		   * FINISHME: programs with shaders that have differing
404
		   * FINISHME: initializers.  Linking with the first will
405
		   * FINISHME: modify the shader, and linking with the second
406
		   * FINISHME: will fail.
407
		   */
408
		  existing->constant_value =
409
		     var->constant_value->clone(ralloc_parent(existing), NULL);
410
	    }
411
 
412
	    if (existing->invariant != var->invariant) {
413
	       linker_error_printf(prog, "declarations for %s `%s' have "
414
	                           "mismatching invariant qualifiers\n",
415
	                           mode_string(var), var->name);
416
	       return false;
417
	    }
418
            if (existing->centroid != var->centroid) {
419
               linker_error_printf(prog, "declarations for %s `%s' have "
420
                                   "mismatching centroid qualifiers\n",
421
                                   mode_string(var), var->name);
422
               return false;
423
            }
424
	 } else
425
	    variables.add_variable(var);
426
      }
427
   }
428
 
429
   return true;
430
}
431
 
432
 
433
/**
434
 * Perform validation of uniforms used across multiple shader stages
435
 */
436
bool
437
cross_validate_uniforms(struct gl_shader_program *prog)
438
{
439
   return cross_validate_globals(prog, prog->_LinkedShaders,
440
				 MESA_SHADER_TYPES, true);
441
}
442
 
443
 
444
/**
445
 * Validate that outputs from one stage match inputs of another
446
 */
447
bool
448
cross_validate_outputs_to_inputs(struct gl_shader_program *prog,
449
				 gl_shader *producer, gl_shader *consumer)
450
{
451
   glsl_symbol_table parameters;
452
   /* FINISHME: Figure these out dynamically. */
453
   const char *const producer_stage = "vertex";
454
   const char *const consumer_stage = "fragment";
455
 
456
   /* Find all shader outputs in the "producer" stage.
457
    */
458
   foreach_list(node, producer->ir) {
459
      ir_variable *const var = ((ir_instruction *) node)->as_variable();
460
 
461
      /* FINISHME: For geometry shaders, this should also look for inout
462
       * FINISHME: variables.
463
       */
464
      if ((var == NULL) || (var->mode != ir_var_out))
465
	 continue;
466
 
467
      parameters.add_variable(var);
468
   }
469
 
470
 
471
   /* Find all shader inputs in the "consumer" stage.  Any variables that have
472
    * matching outputs already in the symbol table must have the same type and
473
    * qualifiers.
474
    */
475
   foreach_list(node, consumer->ir) {
476
      ir_variable *const input = ((ir_instruction *) node)->as_variable();
477
 
478
      /* FINISHME: For geometry shaders, this should also look for inout
479
       * FINISHME: variables.
480
       */
481
      if ((input == NULL) || (input->mode != ir_var_in))
482
	 continue;
483
 
484
      ir_variable *const output = parameters.get_variable(input->name);
485
      if (output != NULL) {
486
	 /* Check that the types match between stages.
487
	  */
488
	 if (input->type != output->type) {
489
	    /* There is a bit of a special case for gl_TexCoord.  This
490
	     * built-in is unsized by default.  Appliations that variable
491
	     * access it must redeclare it with a size.  There is some
492
	     * language in the GLSL spec that implies the fragment shader
493
	     * and vertex shader do not have to agree on this size.  Other
494
	     * driver behave this way, and one or two applications seem to
495
	     * rely on it.
496
	     *
497
	     * Neither declaration needs to be modified here because the array
498
	     * sizes are fixed later when update_array_sizes is called.
499
	     *
500
	     * From page 48 (page 54 of the PDF) of the GLSL 1.10 spec:
501
	     *
502
	     *     "Unlike user-defined varying variables, the built-in
503
	     *     varying variables don't have a strict one-to-one
504
	     *     correspondence between the vertex language and the
505
	     *     fragment language."
506
	     */
507
	    if (!output->type->is_array()
508
		|| (strncmp("gl_", output->name, 3) != 0)) {
509
	       linker_error_printf(prog,
510
				   "%s shader output `%s' declared as "
511
				   "type `%s', but %s shader input declared "
512
				   "as type `%s'\n",
513
				   producer_stage, output->name,
514
				   output->type->name,
515
				   consumer_stage, input->type->name);
516
	       return false;
517
	    }
518
	 }
519
 
520
	 /* Check that all of the qualifiers match between stages.
521
	  */
522
	 if (input->centroid != output->centroid) {
523
	    linker_error_printf(prog,
524
				"%s shader output `%s' %s centroid qualifier, "
525
				"but %s shader input %s centroid qualifier\n",
526
				producer_stage,
527
				output->name,
528
				(output->centroid) ? "has" : "lacks",
529
				consumer_stage,
530
				(input->centroid) ? "has" : "lacks");
531
	    return false;
532
	 }
533
 
534
	 if (input->invariant != output->invariant) {
535
	    linker_error_printf(prog,
536
				"%s shader output `%s' %s invariant qualifier, "
537
				"but %s shader input %s invariant qualifier\n",
538
				producer_stage,
539
				output->name,
540
				(output->invariant) ? "has" : "lacks",
541
				consumer_stage,
542
				(input->invariant) ? "has" : "lacks");
543
	    return false;
544
	 }
545
 
546
	 if (input->interpolation != output->interpolation) {
547
	    linker_error_printf(prog,
548
				"%s shader output `%s' specifies %s "
549
				"interpolation qualifier, "
550
				"but %s shader input specifies %s "
551
				"interpolation qualifier\n",
552
				producer_stage,
553
				output->name,
554
				output->interpolation_string(),
555
				consumer_stage,
556
				input->interpolation_string());
557
	    return false;
558
	 }
559
      }
560
   }
561
 
562
   return true;
563
}
564
 
565
 
566
/**
567
 * Populates a shaders symbol table with all global declarations
568
 */
569
static void
570
populate_symbol_table(gl_shader *sh)
571
{
572
   sh->symbols = new(sh) glsl_symbol_table;
573
 
574
   foreach_list(node, sh->ir) {
575
      ir_instruction *const inst = (ir_instruction *) node;
576
      ir_variable *var;
577
      ir_function *func;
578
 
579
      if ((func = inst->as_function()) != NULL) {
580
	 sh->symbols->add_function(func);
581
      } else if ((var = inst->as_variable()) != NULL) {
582
	 sh->symbols->add_variable(var);
583
      }
584
   }
585
}
586
 
587
 
588
/**
589
 * Remap variables referenced in an instruction tree
590
 *
591
 * This is used when instruction trees are cloned from one shader and placed in
592
 * another.  These trees will contain references to \c ir_variable nodes that
593
 * do not exist in the target shader.  This function finds these \c ir_variable
594
 * references and replaces the references with matching variables in the target
595
 * shader.
596
 *
597
 * If there is no matching variable in the target shader, a clone of the
598
 * \c ir_variable is made and added to the target shader.  The new variable is
599
 * added to \b both the instruction stream and the symbol table.
600
 *
601
 * \param inst         IR tree that is to be processed.
602
 * \param symbols      Symbol table containing global scope symbols in the
603
 *                     linked shader.
604
 * \param instructions Instruction stream where new variable declarations
605
 *                     should be added.
606
 */
607
void
608
remap_variables(ir_instruction *inst, struct gl_shader *target,
609
		hash_table *temps)
610
{
611
   class remap_visitor : public ir_hierarchical_visitor {
612
   public:
613
	 remap_visitor(struct gl_shader *target,
614
		    hash_table *temps)
615
      {
616
	 this->target = target;
617
	 this->symbols = target->symbols;
618
	 this->instructions = target->ir;
619
	 this->temps = temps;
620
      }
621
 
622
      virtual ir_visitor_status visit(ir_dereference_variable *ir)
623
      {
624
	 if (ir->var->mode == ir_var_temporary) {
625
	    ir_variable *var = (ir_variable *) hash_table_find(temps, ir->var);
626
 
627
	    assert(var != NULL);
628
	    ir->var = var;
629
	    return visit_continue;
630
	 }
631
 
632
	 ir_variable *const existing =
633
	    this->symbols->get_variable(ir->var->name);
634
	 if (existing != NULL)
635
	    ir->var = existing;
636
	 else {
637
	    ir_variable *copy = ir->var->clone(this->target, NULL);
638
 
639
	    this->symbols->add_variable(copy);
640
	    this->instructions->push_head(copy);
641
	    ir->var = copy;
642
	 }
643
 
644
	 return visit_continue;
645
      }
646
 
647
   private:
648
      struct gl_shader *target;
649
      glsl_symbol_table *symbols;
650
      exec_list *instructions;
651
      hash_table *temps;
652
   };
653
 
654
   remap_visitor v(target, temps);
655
 
656
   inst->accept(&v);
657
}
658
 
659
 
660
/**
661
 * Move non-declarations from one instruction stream to another
662
 *
663
 * The intended usage pattern of this function is to pass the pointer to the
664
 * head sentinel of a list (i.e., a pointer to the list cast to an \c exec_node
665
 * pointer) for \c last and \c false for \c make_copies on the first
666
 * call.  Successive calls pass the return value of the previous call for
667
 * \c last and \c true for \c make_copies.
668
 *
669
 * \param instructions Source instruction stream
670
 * \param last         Instruction after which new instructions should be
671
 *                     inserted in the target instruction stream
672
 * \param make_copies  Flag selecting whether instructions in \c instructions
673
 *                     should be copied (via \c ir_instruction::clone) into the
674
 *                     target list or moved.
675
 *
676
 * \return
677
 * The new "last" instruction in the target instruction stream.  This pointer
678
 * is suitable for use as the \c last parameter of a later call to this
679
 * function.
680
 */
681
exec_node *
682
move_non_declarations(exec_list *instructions, exec_node *last,
683
		      bool make_copies, gl_shader *target)
684
{
685
   hash_table *temps = NULL;
686
 
687
   if (make_copies)
688
      temps = hash_table_ctor(0, hash_table_pointer_hash,
689
			      hash_table_pointer_compare);
690
 
691
   foreach_list_safe(node, instructions) {
692
      ir_instruction *inst = (ir_instruction *) node;
693
 
694
      if (inst->as_function())
695
	 continue;
696
 
697
      ir_variable *var = inst->as_variable();
698
      if ((var != NULL) && (var->mode != ir_var_temporary))
699
	 continue;
700
 
701
      assert(inst->as_assignment()
702
	     || ((var != NULL) && (var->mode == ir_var_temporary)));
703
 
704
      if (make_copies) {
705
	 inst = inst->clone(target, NULL);
706
 
707
	 if (var != NULL)
708
	    hash_table_insert(temps, inst, var);
709
	 else
710
	    remap_variables(inst, target, temps);
711
      } else {
712
	 inst->remove();
713
      }
714
 
715
      last->insert_after(inst);
716
      last = inst;
717
   }
718
 
719
   if (make_copies)
720
      hash_table_dtor(temps);
721
 
722
   return last;
723
}
724
 
725
/**
726
 * Get the function signature for main from a shader
727
 */
728
static ir_function_signature *
729
get_main_function_signature(gl_shader *sh)
730
{
731
   ir_function *const f = sh->symbols->get_function("main");
732
   if (f != NULL) {
733
      exec_list void_parameters;
734
 
735
      /* Look for the 'void main()' signature and ensure that it's defined.
736
       * This keeps the linker from accidentally pick a shader that just
737
       * contains a prototype for main.
738
       *
739
       * We don't have to check for multiple definitions of main (in multiple
740
       * shaders) because that would have already been caught above.
741
       */
742
      ir_function_signature *sig = f->matching_signature(&void_parameters);
743
      if ((sig != NULL) && sig->is_defined) {
744
	 return sig;
745
      }
746
   }
747
 
748
   return NULL;
749
}
750
 
751
 
752
/**
753
 * Combine a group of shaders for a single stage to generate a linked shader
754
 *
755
 * \note
756
 * If this function is supplied a single shader, it is cloned, and the new
757
 * shader is returned.
758
 */
759
static struct gl_shader *
760
link_intrastage_shaders(void *mem_ctx,
761
			struct gl_context *ctx,
762
			struct gl_shader_program *prog,
763
			struct gl_shader **shader_list,
764
			unsigned num_shaders)
765
{
766
   /* Check that global variables defined in multiple shaders are consistent.
767
    */
768
   if (!cross_validate_globals(prog, shader_list, num_shaders, false))
769
      return NULL;
770
 
771
   /* Check that there is only a single definition of each function signature
772
    * across all shaders.
773
    */
774
   for (unsigned i = 0; i < (num_shaders - 1); i++) {
775
      foreach_list(node, shader_list[i]->ir) {
776
	 ir_function *const f = ((ir_instruction *) node)->as_function();
777
 
778
	 if (f == NULL)
779
	    continue;
780
 
781
	 for (unsigned j = i + 1; j < num_shaders; j++) {
782
	    ir_function *const other =
783
	       shader_list[j]->symbols->get_function(f->name);
784
 
785
	    /* If the other shader has no function (and therefore no function
786
	     * signatures) with the same name, skip to the next shader.
787
	     */
788
	    if (other == NULL)
789
	       continue;
790
 
791
	    foreach_iter (exec_list_iterator, iter, *f) {
792
	       ir_function_signature *sig =
793
		  (ir_function_signature *) iter.get();
794
 
795
	       if (!sig->is_defined || sig->is_builtin)
796
		  continue;
797
 
798
	       ir_function_signature *other_sig =
799
		  other->exact_matching_signature(& sig->parameters);
800
 
801
	       if ((other_sig != NULL) && other_sig->is_defined
802
		   && !other_sig->is_builtin) {
803
		  linker_error_printf(prog,
804
				      "function `%s' is multiply defined",
805
				      f->name);
806
		  return NULL;
807
	       }
808
	    }
809
	 }
810
      }
811
   }
812
 
813
   /* Find the shader that defines main, and make a clone of it.
814
    *
815
    * Starting with the clone, search for undefined references.  If one is
816
    * found, find the shader that defines it.  Clone the reference and add
817
    * it to the shader.  Repeat until there are no undefined references or
818
    * until a reference cannot be resolved.
819
    */
820
   gl_shader *main = NULL;
821
   for (unsigned i = 0; i < num_shaders; i++) {
822
      if (get_main_function_signature(shader_list[i]) != NULL) {
823
	 main = shader_list[i];
824
	 break;
825
      }
826
   }
827
 
828
   if (main == NULL) {
829
      linker_error_printf(prog, "%s shader lacks `main'\n",
830
			  (shader_list[0]->Type == GL_VERTEX_SHADER)
831
			  ? "vertex" : "fragment");
832
      return NULL;
833
   }
834
 
835
   gl_shader *linked = ctx->Driver.NewShader(NULL, 0, main->Type);
836
   linked->ir = new(linked) exec_list;
837
   clone_ir_list(mem_ctx, linked->ir, main->ir);
838
 
839
   populate_symbol_table(linked);
840
 
841
   /* The a pointer to the main function in the final linked shader (i.e., the
842
    * copy of the original shader that contained the main function).
843
    */
844
   ir_function_signature *const main_sig = get_main_function_signature(linked);
845
 
846
   /* Move any instructions other than variable declarations or function
847
    * declarations into main.
848
    */
849
   exec_node *insertion_point =
850
      move_non_declarations(linked->ir, (exec_node *) &main_sig->body, false,
851
			    linked);
852
 
853
   for (unsigned i = 0; i < num_shaders; i++) {
854
      if (shader_list[i] == main)
855
	 continue;
856
 
857
      insertion_point = move_non_declarations(shader_list[i]->ir,
858
					      insertion_point, true, linked);
859
   }
860
 
861
   /* Resolve initializers for global variables in the linked shader.
862
    */
863
   unsigned num_linking_shaders = num_shaders;
864
   for (unsigned i = 0; i < num_shaders; i++)
865
      num_linking_shaders += shader_list[i]->num_builtins_to_link;
866
 
867
   gl_shader **linking_shaders =
868
      (gl_shader **) calloc(num_linking_shaders, sizeof(gl_shader *));
869
 
870
   memcpy(linking_shaders, shader_list,
871
	  sizeof(linking_shaders[0]) * num_shaders);
872
 
873
   unsigned idx = num_shaders;
874
   for (unsigned i = 0; i < num_shaders; i++) {
875
      memcpy(&linking_shaders[idx], shader_list[i]->builtins_to_link,
876
	     sizeof(linking_shaders[0]) * shader_list[i]->num_builtins_to_link);
877
      idx += shader_list[i]->num_builtins_to_link;
878
   }
879
 
880
   assert(idx == num_linking_shaders);
881
 
882
   if (!link_function_calls(prog, linked, linking_shaders,
883
			    num_linking_shaders)) {
884
      ctx->Driver.DeleteShader(ctx, linked);
885
      linked = NULL;
886
   }
887
 
888
   free(linking_shaders);
889
 
890
   /* Make a pass over all variable declarations to ensure that arrays with
891
    * unspecified sizes have a size specified.  The size is inferred from the
892
    * max_array_access field.
893
    */
894
   if (linked != NULL) {
895
      class array_sizing_visitor : public ir_hierarchical_visitor {
896
      public:
897
	 virtual ir_visitor_status visit(ir_variable *var)
898
	 {
899
	    if (var->type->is_array() && (var->type->length == 0)) {
900
	       const glsl_type *type =
901
		  glsl_type::get_array_instance(var->type->fields.array,
902
						var->max_array_access + 1);
903
 
904
	       assert(type != NULL);
905
	       var->type = type;
906
	    }
907
 
908
	    return visit_continue;
909
	 }
910
      } v;
911
 
912
      v.run(linked->ir);
913
   }
914
 
915
   return linked;
916
}
917
 
918
 
919
struct uniform_node {
920
   exec_node link;
921
   struct gl_uniform *u;
922
   unsigned slots;
923
};
924
 
925
/**
926
 * Update the sizes of linked shader uniform arrays to the maximum
927
 * array index used.
928
 *
929
 * From page 81 (page 95 of the PDF) of the OpenGL 2.1 spec:
930
 *
931
 *     If one or more elements of an array are active,
932
 *     GetActiveUniform will return the name of the array in name,
933
 *     subject to the restrictions listed above. The type of the array
934
 *     is returned in type. The size parameter contains the highest
935
 *     array element index used, plus one. The compiler or linker
936
 *     determines the highest index used.  There will be only one
937
 *     active uniform reported by the GL per uniform array.
938
 
939
 */
940
static void
941
update_array_sizes(struct gl_shader_program *prog)
942
{
943
   for (unsigned i = 0; i < MESA_SHADER_TYPES; i++) {
944
	 if (prog->_LinkedShaders[i] == NULL)
945
	    continue;
946
 
947
      foreach_list(node, prog->_LinkedShaders[i]->ir) {
948
	 ir_variable *const var = ((ir_instruction *) node)->as_variable();
949
 
950
	 if ((var == NULL) || (var->mode != ir_var_uniform &&
951
			       var->mode != ir_var_in &&
952
			       var->mode != ir_var_out) ||
953
	     !var->type->is_array())
954
	    continue;
955
 
956
	 unsigned int size = var->max_array_access;
957
	 for (unsigned j = 0; j < MESA_SHADER_TYPES; j++) {
958
	       if (prog->_LinkedShaders[j] == NULL)
959
		  continue;
960
 
961
	    foreach_list(node2, prog->_LinkedShaders[j]->ir) {
962
	       ir_variable *other_var = ((ir_instruction *) node2)->as_variable();
963
	       if (!other_var)
964
		  continue;
965
 
966
	       if (strcmp(var->name, other_var->name) == 0 &&
967
		   other_var->max_array_access > size) {
968
		  size = other_var->max_array_access;
969
	       }
970
	    }
971
	 }
972
 
973
	 if (size + 1 != var->type->fields.array->length) {
974
	    var->type = glsl_type::get_array_instance(var->type->fields.array,
975
						      size + 1);
976
	    /* FINISHME: We should update the types of array
977
	     * dereferences of this variable now.
978
	     */
979
	 }
980
      }
981
   }
982
}
983
 
984
static void
985
add_uniform(void *mem_ctx, exec_list *uniforms, struct hash_table *ht,
986
	    const char *name, const glsl_type *type, GLenum shader_type,
987
	    unsigned *next_shader_pos, unsigned *total_uniforms)
988
{
989
   if (type->is_record()) {
990
      for (unsigned int i = 0; i < type->length; i++) {
991
	 const glsl_type *field_type = type->fields.structure[i].type;
992
	 char *field_name = ralloc_asprintf(mem_ctx, "%s.%s", name,
993
					    type->fields.structure[i].name);
994
 
995
	 add_uniform(mem_ctx, uniforms, ht, field_name, field_type,
996
		     shader_type, next_shader_pos, total_uniforms);
997
      }
998
   } else {
999
      uniform_node *n = (uniform_node *) hash_table_find(ht, name);
1000
      unsigned int vec4_slots;
1001
      const glsl_type *array_elem_type = NULL;
1002
 
1003
      if (type->is_array()) {
1004
	 array_elem_type = type->fields.array;
1005
	 /* Array of structures. */
1006
	 if (array_elem_type->is_record()) {
1007
	    for (unsigned int i = 0; i < type->length; i++) {
1008
	       char *elem_name = ralloc_asprintf(mem_ctx, "%s[%d]", name, i);
1009
	       add_uniform(mem_ctx, uniforms, ht, elem_name, array_elem_type,
1010
			   shader_type, next_shader_pos, total_uniforms);
1011
	    }
1012
	    return;
1013
	 }
1014
      }
1015
 
1016
      /* Fix the storage size of samplers at 1 vec4 each. Be sure to pad out
1017
       * vectors to vec4 slots.
1018
       */
1019
      if (type->is_array()) {
1020
	 if (array_elem_type->is_sampler())
1021
	    vec4_slots = type->length;
1022
	 else
1023
	    vec4_slots = type->length * array_elem_type->matrix_columns;
1024
      } else if (type->is_sampler()) {
1025
	 vec4_slots = 1;
1026
      } else {
1027
	 vec4_slots = type->matrix_columns;
1028
      }
1029
 
1030
      if (n == NULL) {
1031
	 n = (uniform_node *) calloc(1, sizeof(struct uniform_node));
1032
	 n->u = (gl_uniform *) calloc(1, sizeof(struct gl_uniform));
1033
	 n->slots = vec4_slots;
1034
 
1035
	 n->u->Name = strdup(name);
1036
	 n->u->Type = type;
1037
	 n->u->VertPos = -1;
1038
	 n->u->FragPos = -1;
1039
	 n->u->GeomPos = -1;
1040
	 (*total_uniforms)++;
1041
 
1042
	 hash_table_insert(ht, n, name);
1043
	 uniforms->push_tail(& n->link);
1044
      }
1045
 
1046
      switch (shader_type) {
1047
      case GL_VERTEX_SHADER:
1048
	 n->u->VertPos = *next_shader_pos;
1049
	 break;
1050
      case GL_FRAGMENT_SHADER:
1051
	 n->u->FragPos = *next_shader_pos;
1052
	 break;
1053
      case GL_GEOMETRY_SHADER:
1054
	 n->u->GeomPos = *next_shader_pos;
1055
	 break;
1056
      }
1057
 
1058
      (*next_shader_pos) += vec4_slots;
1059
   }
1060
}
1061
 
1062
void
1063
assign_uniform_locations(struct gl_shader_program *prog)
1064
{
1065
   /* */
1066
   exec_list uniforms;
1067
   unsigned total_uniforms = 0;
1068
   hash_table *ht = hash_table_ctor(32, hash_table_string_hash,
1069
				    hash_table_string_compare);
1070
   void *mem_ctx = ralloc_context(NULL);
1071
 
1072
   for (unsigned i = 0; i < MESA_SHADER_TYPES; i++) {
1073
      if (prog->_LinkedShaders[i] == NULL)
1074
	 continue;
1075
 
1076
      unsigned next_position = 0;
1077
 
1078
      foreach_list(node, prog->_LinkedShaders[i]->ir) {
1079
	 ir_variable *const var = ((ir_instruction *) node)->as_variable();
1080
 
1081
	 if ((var == NULL) || (var->mode != ir_var_uniform))
1082
	    continue;
1083
 
1084
	 if (strncmp(var->name, "gl_", 3) == 0) {
1085
	    /* At the moment, we don't allocate uniform locations for
1086
	     * builtin uniforms.  It's permitted by spec, and we'll
1087
	     * likely switch to doing that at some point, but not yet.
1088
	     */
1089
	    continue;
1090
	 }
1091
 
1092
	 var->location = next_position;
1093
	 add_uniform(mem_ctx, &uniforms, ht, var->name, var->type,
1094
		     prog->_LinkedShaders[i]->Type,
1095
		     &next_position, &total_uniforms);
1096
      }
1097
   }
1098
 
1099
   ralloc_free(mem_ctx);
1100
 
1101
   gl_uniform_list *ul = (gl_uniform_list *)
1102
      calloc(1, sizeof(gl_uniform_list));
1103
 
1104
   ul->Size = total_uniforms;
1105
   ul->NumUniforms = total_uniforms;
1106
   ul->Uniforms = (gl_uniform *) calloc(total_uniforms, sizeof(gl_uniform));
1107
 
1108
   unsigned idx = 0;
1109
   uniform_node *next;
1110
   for (uniform_node *node = (uniform_node *) uniforms.head
1111
	   ; node->link.next != NULL
1112
	   ; node = next) {
1113
      next = (uniform_node *) node->link.next;
1114
 
1115
      node->link.remove();
1116
      memcpy(&ul->Uniforms[idx], node->u, sizeof(gl_uniform));
1117
      idx++;
1118
 
1119
      free(node->u);
1120
      free(node);
1121
   }
1122
 
1123
   hash_table_dtor(ht);
1124
 
1125
   prog->Uniforms = ul;
1126
}
1127
 
1128
 
1129
/**
1130
 * Find a contiguous set of available bits in a bitmask
1131
 *
1132
 * \param used_mask     Bits representing used (1) and unused (0) locations
1133
 * \param needed_count  Number of contiguous bits needed.
1134
 *
1135
 * \return
1136
 * Base location of the available bits on success or -1 on failure.
1137
 */
1138
int
1139
find_available_slots(unsigned used_mask, unsigned needed_count)
1140
{
1141
   unsigned needed_mask = (1 << needed_count) - 1;
1142
   const int max_bit_to_test = (8 * sizeof(used_mask)) - needed_count;
1143
 
1144
   /* The comparison to 32 is redundant, but without it GCC emits "warning:
1145
    * cannot optimize possibly infinite loops" for the loop below.
1146
    */
1147
   if ((needed_count == 0) || (max_bit_to_test < 0) || (max_bit_to_test > 32))
1148
      return -1;
1149
 
1150
   for (int i = 0; i <= max_bit_to_test; i++) {
1151
      if ((needed_mask & ~used_mask) == needed_mask)
1152
	 return i;
1153
 
1154
      needed_mask <<= 1;
1155
   }
1156
 
1157
   return -1;
1158
}
1159
 
1160
 
1161
bool
1162
assign_attribute_locations(gl_shader_program *prog, unsigned max_attribute_index)
1163
{
1164
   /* Mark invalid attribute locations as being used.
1165
    */
1166
   unsigned used_locations = (max_attribute_index >= 32)
1167
      ? ~0 : ~((1 << max_attribute_index) - 1);
1168
 
1169
   gl_shader *const sh = prog->_LinkedShaders[0];
1170
   assert(sh->Type == GL_VERTEX_SHADER);
1171
 
1172
   /* Operate in a total of four passes.
1173
    *
1174
    * 1. Invalidate the location assignments for all vertex shader inputs.
1175
    *
1176
    * 2. Assign locations for inputs that have user-defined (via
1177
    *    glBindVertexAttribLocation) locatoins.
1178
    *
1179
    * 3. Sort the attributes without assigned locations by number of slots
1180
    *    required in decreasing order.  Fragmentation caused by attribute
1181
    *    locations assigned by the application may prevent large attributes
1182
    *    from having enough contiguous space.
1183
    *
1184
    * 4. Assign locations to any inputs without assigned locations.
1185
    */
1186
 
1187
   invalidate_variable_locations(sh, ir_var_in, VERT_ATTRIB_GENERIC0);
1188
 
1189
   if (prog->Attributes != NULL) {
1190
      for (unsigned i = 0; i < prog->Attributes->NumParameters; i++) {
1191
	 ir_variable *const var =
1192
	    sh->symbols->get_variable(prog->Attributes->Parameters[i].Name);
1193
 
1194
	 /* Note: attributes that occupy multiple slots, such as arrays or
1195
	  * matrices, may appear in the attrib array multiple times.
1196
	  */
1197
	 if ((var == NULL) || (var->location != -1))
1198
	    continue;
1199
 
1200
	 /* From page 61 of the OpenGL 4.0 spec:
1201
	  *
1202
	  *     "LinkProgram will fail if the attribute bindings assigned by
1203
	  *     BindAttribLocation do not leave not enough space to assign a
1204
	  *     location for an active matrix attribute or an active attribute
1205
	  *     array, both of which require multiple contiguous generic
1206
	  *     attributes."
1207
	  *
1208
	  * Previous versions of the spec contain similar language but omit the
1209
	  * bit about attribute arrays.
1210
	  *
1211
	  * Page 61 of the OpenGL 4.0 spec also says:
1212
	  *
1213
	  *     "It is possible for an application to bind more than one
1214
	  *     attribute name to the same location. This is referred to as
1215
	  *     aliasing. This will only work if only one of the aliased
1216
	  *     attributes is active in the executable program, or if no path
1217
	  *     through the shader consumes more than one attribute of a set
1218
	  *     of attributes aliased to the same location. A link error can
1219
	  *     occur if the linker determines that every path through the
1220
	  *     shader consumes multiple aliased attributes, but
1221
	  *     implementations are not required to generate an error in this
1222
	  *     case."
1223
	  *
1224
	  * These two paragraphs are either somewhat contradictory, or I don't
1225
	  * fully understand one or both of them.
1226
	  */
1227
	 /* FINISHME: The code as currently written does not support attribute
1228
	  * FINISHME: location aliasing (see comment above).
1229
	  */
1230
	 const int attr = prog->Attributes->Parameters[i].StateIndexes[0];
1231
	 const unsigned slots = count_attribute_slots(var->type);
1232
 
1233
	 /* Mask representing the contiguous slots that will be used by this
1234
	  * attribute.
1235
	  */
1236
	 const unsigned use_mask = (1 << slots) - 1;
1237
 
1238
	 /* Generate a link error if the set of bits requested for this
1239
	  * attribute overlaps any previously allocated bits.
1240
	  */
1241
	 if ((~(use_mask << attr) & used_locations) != used_locations) {
1242
	    linker_error_printf(prog,
1243
				"insufficient contiguous attribute locations "
1244
				"available for vertex shader input `%s'",
1245
				var->name);
1246
	    return false;
1247
	 }
1248
 
1249
	 var->location = VERT_ATTRIB_GENERIC0 + attr;
1250
	 used_locations |= (use_mask << attr);
1251
      }
1252
   }
1253
 
1254
   /* Temporary storage for the set of attributes that need locations assigned.
1255
    */
1256
   struct temp_attr {
1257
      unsigned slots;
1258
      ir_variable *var;
1259
 
1260
      /* Used below in the call to qsort. */
1261
      static int compare(const void *a, const void *b)
1262
      {
1263
	 const temp_attr *const l = (const temp_attr *) a;
1264
	 const temp_attr *const r = (const temp_attr *) b;
1265
 
1266
	 /* Reversed because we want a descending order sort below. */
1267
	 return r->slots - l->slots;
1268
      }
1269
   } to_assign[16];
1270
 
1271
   unsigned num_attr = 0;
1272
 
1273
   foreach_list(node, sh->ir) {
1274
      ir_variable *const var = ((ir_instruction *) node)->as_variable();
1275
 
1276
      if ((var == NULL) || (var->mode != ir_var_in))
1277
	 continue;
1278
 
1279
      if (var->explicit_location) {
1280
	 const unsigned slots = count_attribute_slots(var->type);
1281
	 const unsigned use_mask = (1 << slots) - 1;
1282
	 const int attr = var->location - VERT_ATTRIB_GENERIC0;
1283
 
1284
	 if ((var->location >= (int)(max_attribute_index + VERT_ATTRIB_GENERIC0))
1285
	     || (var->location < 0)) {
1286
	    linker_error_printf(prog,
1287
				"invalid explicit location %d specified for "
1288
				"`%s'\n",
1289
				(var->location < 0) ? var->location : attr,
1290
				var->name);
1291
	    return false;
1292
	 } else if (var->location >= VERT_ATTRIB_GENERIC0) {
1293
	    used_locations |= (use_mask << attr);
1294
	 }
1295
      }
1296
 
1297
      /* The location was explicitly assigned, nothing to do here.
1298
       */
1299
      if (var->location != -1)
1300
	 continue;
1301
 
1302
      to_assign[num_attr].slots = count_attribute_slots(var->type);
1303
      to_assign[num_attr].var = var;
1304
      num_attr++;
1305
   }
1306
 
1307
   /* If all of the attributes were assigned locations by the application (or
1308
    * are built-in attributes with fixed locations), return early.  This should
1309
    * be the common case.
1310
    */
1311
   if (num_attr == 0)
1312
      return true;
1313
 
1314
   qsort(to_assign, num_attr, sizeof(to_assign[0]), temp_attr::compare);
1315
 
1316
   /* VERT_ATTRIB_GENERIC0 is a psdueo-alias for VERT_ATTRIB_POS.  It can only
1317
    * be explicitly assigned by via glBindAttribLocation.  Mark it as reserved
1318
    * to prevent it from being automatically allocated below.
1319
    */
1320
   find_deref_visitor find("gl_Vertex");
1321
   find.run(sh->ir);
1322
   if (find.variable_found())
1323
      used_locations |= (1 << 0);
1324
 
1325
   for (unsigned i = 0; i < num_attr; i++) {
1326
      /* Mask representing the contiguous slots that will be used by this
1327
       * attribute.
1328
       */
1329
      const unsigned use_mask = (1 << to_assign[i].slots) - 1;
1330
 
1331
      int location = find_available_slots(used_locations, to_assign[i].slots);
1332
 
1333
      if (location < 0) {
1334
	 linker_error_printf(prog,
1335
			     "insufficient contiguous attribute locations "
1336
			     "available for vertex shader input `%s'",
1337
			     to_assign[i].var->name);
1338
	 return false;
1339
      }
1340
 
1341
      to_assign[i].var->location = VERT_ATTRIB_GENERIC0 + location;
1342
      used_locations |= (use_mask << location);
1343
   }
1344
 
1345
   return true;
1346
}
1347
 
1348
 
1349
/**
1350
 * Demote shader inputs and outputs that are not used in other stages
1351
 */
1352
void
1353
demote_shader_inputs_and_outputs(gl_shader *sh, enum ir_variable_mode mode)
1354
{
1355
   foreach_list(node, sh->ir) {
1356
      ir_variable *const var = ((ir_instruction *) node)->as_variable();
1357
 
1358
      if ((var == NULL) || (var->mode != int(mode)))
1359
	 continue;
1360
 
1361
      /* A shader 'in' or 'out' variable is only really an input or output if
1362
       * its value is used by other shader stages.  This will cause the variable
1363
       * to have a location assigned.
1364
       */
1365
      if (var->location == -1) {
1366
	 var->mode = ir_var_auto;
1367
      }
1368
   }
1369
}
1370
 
1371
 
1372
void
1373
assign_varying_locations(struct gl_shader_program *prog,
1374
			 gl_shader *producer, gl_shader *consumer)
1375
{
1376
   /* FINISHME: Set dynamically when geometry shader support is added. */
1377
   unsigned output_index = VERT_RESULT_VAR0;
1378
   unsigned input_index = FRAG_ATTRIB_VAR0;
1379
 
1380
   /* Operate in a total of three passes.
1381
    *
1382
    * 1. Assign locations for any matching inputs and outputs.
1383
    *
1384
    * 2. Mark output variables in the producer that do not have locations as
1385
    *    not being outputs.  This lets the optimizer eliminate them.
1386
    *
1387
    * 3. Mark input variables in the consumer that do not have locations as
1388
    *    not being inputs.  This lets the optimizer eliminate them.
1389
    */
1390
 
1391
   invalidate_variable_locations(producer, ir_var_out, VERT_RESULT_VAR0);
1392
   invalidate_variable_locations(consumer, ir_var_in, FRAG_ATTRIB_VAR0);
1393
 
1394
   foreach_list(node, producer->ir) {
1395
      ir_variable *const output_var = ((ir_instruction *) node)->as_variable();
1396
 
1397
      if ((output_var == NULL) || (output_var->mode != ir_var_out)
1398
	  || (output_var->location != -1))
1399
	 continue;
1400
 
1401
      ir_variable *const input_var =
1402
	 consumer->symbols->get_variable(output_var->name);
1403
 
1404
      if ((input_var == NULL) || (input_var->mode != ir_var_in))
1405
	 continue;
1406
 
1407
      assert(input_var->location == -1);
1408
 
1409
      output_var->location = output_index;
1410
      input_var->location = input_index;
1411
 
1412
      /* FINISHME: Support for "varying" records in GLSL 1.50. */
1413
      assert(!output_var->type->is_record());
1414
 
1415
      if (output_var->type->is_array()) {
1416
	 const unsigned slots = output_var->type->length
1417
	    * output_var->type->fields.array->matrix_columns;
1418
 
1419
	 output_index += slots;
1420
	 input_index += slots;
1421
      } else {
1422
	 const unsigned slots = output_var->type->matrix_columns;
1423
 
1424
	 output_index += slots;
1425
	 input_index += slots;
1426
      }
1427
   }
1428
 
1429
   foreach_list(node, consumer->ir) {
1430
      ir_variable *const var = ((ir_instruction *) node)->as_variable();
1431
 
1432
      if ((var == NULL) || (var->mode != ir_var_in))
1433
	 continue;
1434
 
1435
      if (var->location == -1) {
1436
	 if (prog->Version <= 120) {
1437
	    /* On page 25 (page 31 of the PDF) of the GLSL 1.20 spec:
1438
	     *
1439
	     *     Only those varying variables used (i.e. read) in
1440
	     *     the fragment shader executable must be written to
1441
	     *     by the vertex shader executable; declaring
1442
	     *     superfluous varying variables in a vertex shader is
1443
	     *     permissible.
1444
	     *
1445
	     * We interpret this text as meaning that the VS must
1446
	     * write the variable for the FS to read it.  See
1447
	     * "glsl1-varying read but not written" in piglit.
1448
	     */
1449
 
1450
	    linker_error_printf(prog, "fragment shader varying %s not written "
1451
				"by vertex shader\n.", var->name);
1452
	    prog->LinkStatus = false;
1453
	 }
1454
 
1455
	 /* An 'in' variable is only really a shader input if its
1456
	  * value is written by the previous stage.
1457
	  */
1458
	 var->mode = ir_var_auto;
1459
      }
1460
   }
1461
}
1462
 
1463
 
1464
void
1465
link_shaders(struct gl_context *ctx, struct gl_shader_program *prog)
1466
{
1467
   void *mem_ctx = ralloc_context(NULL); // temporary linker context
1468
 
1469
   prog->LinkStatus = false;
1470
   prog->Validated = false;
1471
   prog->_Used = false;
1472
 
1473
   if (prog->InfoLog != NULL)
1474
      ralloc_free(prog->InfoLog);
1475
 
1476
   prog->InfoLog = ralloc_strdup(NULL, "");
1477
 
1478
   /* Separate the shaders into groups based on their type.
1479
    */
1480
   struct gl_shader **vert_shader_list;
1481
   unsigned num_vert_shaders = 0;
1482
   struct gl_shader **frag_shader_list;
1483
   unsigned num_frag_shaders = 0;
1484
 
1485
   vert_shader_list = (struct gl_shader **)
1486
      calloc(2 * prog->NumShaders, sizeof(struct gl_shader *));
1487
   frag_shader_list =  &vert_shader_list[prog->NumShaders];
1488
 
1489
   unsigned min_version = UINT_MAX;
1490
   unsigned max_version = 0;
1491
   for (unsigned i = 0; i < prog->NumShaders; i++) {
1492
      min_version = MIN2(min_version, prog->Shaders[i]->Version);
1493
      max_version = MAX2(max_version, prog->Shaders[i]->Version);
1494
 
1495
      switch (prog->Shaders[i]->Type) {
1496
      case GL_VERTEX_SHADER:
1497
	 vert_shader_list[num_vert_shaders] = prog->Shaders[i];
1498
	 num_vert_shaders++;
1499
	 break;
1500
      case GL_FRAGMENT_SHADER:
1501
	 frag_shader_list[num_frag_shaders] = prog->Shaders[i];
1502
	 num_frag_shaders++;
1503
	 break;
1504
      case GL_GEOMETRY_SHADER:
1505
	 /* FINISHME: Support geometry shaders. */
1506
	 assert(prog->Shaders[i]->Type != GL_GEOMETRY_SHADER);
1507
	 break;
1508
      }
1509
   }
1510
 
1511
   /* Previous to GLSL version 1.30, different compilation units could mix and
1512
    * match shading language versions.  With GLSL 1.30 and later, the versions
1513
    * of all shaders must match.
1514
    */
1515
   assert(min_version >= 100);
1516
   assert(max_version <= 130);
1517
   if ((max_version >= 130 || min_version == 100)
1518
       && min_version != max_version) {
1519
      linker_error_printf(prog, "all shaders must use same shading "
1520
			  "language version\n");
1521
      goto done;
1522
   }
1523
 
1524
   prog->Version = max_version;
1525
 
1526
   for (unsigned int i = 0; i < MESA_SHADER_TYPES; i++) {
1527
      if (prog->_LinkedShaders[i] != NULL)
1528
	 ctx->Driver.DeleteShader(ctx, prog->_LinkedShaders[i]);
1529
 
1530
      prog->_LinkedShaders[i] = NULL;
1531
   }
1532
 
1533
   /* Link all shaders for a particular stage and validate the result.
1534
    */
1535
   if (num_vert_shaders > 0) {
1536
      gl_shader *const sh =
1537
	 link_intrastage_shaders(mem_ctx, ctx, prog, vert_shader_list,
1538
				 num_vert_shaders);
1539
 
1540
      if (sh == NULL)
1541
	 goto done;
1542
 
1543
      if (!validate_vertex_shader_executable(prog, sh))
1544
	 goto done;
1545
 
1546
      _mesa_reference_shader(ctx, &prog->_LinkedShaders[MESA_SHADER_VERTEX],
1547
			     sh);
1548
   }
1549
 
1550
   if (num_frag_shaders > 0) {
1551
      gl_shader *const sh =
1552
	 link_intrastage_shaders(mem_ctx, ctx, prog, frag_shader_list,
1553
				 num_frag_shaders);
1554
 
1555
      if (sh == NULL)
1556
	 goto done;
1557
 
1558
      if (!validate_fragment_shader_executable(prog, sh))
1559
	 goto done;
1560
 
1561
      _mesa_reference_shader(ctx, &prog->_LinkedShaders[MESA_SHADER_FRAGMENT],
1562
			     sh);
1563
   }
1564
 
1565
   /* Here begins the inter-stage linking phase.  Some initial validation is
1566
    * performed, then locations are assigned for uniforms, attributes, and
1567
    * varyings.
1568
    */
1569
   if (cross_validate_uniforms(prog)) {
1570
      unsigned prev;
1571
 
1572
      for (prev = 0; prev < MESA_SHADER_TYPES; prev++) {
1573
	 if (prog->_LinkedShaders[prev] != NULL)
1574
	    break;
1575
      }
1576
 
1577
      /* Validate the inputs of each stage with the output of the preceeding
1578
       * stage.
1579
       */
1580
      for (unsigned i = prev + 1; i < MESA_SHADER_TYPES; i++) {
1581
	 if (prog->_LinkedShaders[i] == NULL)
1582
	    continue;
1583
 
1584
	 if (!cross_validate_outputs_to_inputs(prog,
1585
					       prog->_LinkedShaders[prev],
1586
					       prog->_LinkedShaders[i]))
1587
	    goto done;
1588
 
1589
	 prev = i;
1590
      }
1591
 
1592
      prog->LinkStatus = true;
1593
   }
1594
 
1595
   /* Do common optimization before assigning storage for attributes,
1596
    * uniforms, and varyings.  Later optimization could possibly make
1597
    * some of that unused.
1598
    */
1599
   for (unsigned i = 0; i < MESA_SHADER_TYPES; i++) {
1600
      if (prog->_LinkedShaders[i] == NULL)
1601
	 continue;
1602
 
1603
      while (do_common_optimization(prog->_LinkedShaders[i]->ir, true, 32))
1604
	 ;
1605
   }
1606
 
1607
   update_array_sizes(prog);
1608
 
1609
   assign_uniform_locations(prog);
1610
 
1611
   if (prog->_LinkedShaders[MESA_SHADER_VERTEX] != NULL) {
1612
      /* FINISHME: The value of the max_attribute_index parameter is
1613
       * FINISHME: implementation dependent based on the value of
1614
       * FINISHME: GL_MAX_VERTEX_ATTRIBS.  GL_MAX_VERTEX_ATTRIBS must be
1615
       * FINISHME: at least 16, so hardcode 16 for now.
1616
       */
1617
      if (!assign_attribute_locations(prog, 16)) {
1618
	 prog->LinkStatus = false;
1619
	 goto done;
1620
      }
1621
   }
1622
 
1623
   unsigned prev;
1624
   for (prev = 0; prev < MESA_SHADER_TYPES; prev++) {
1625
      if (prog->_LinkedShaders[prev] != NULL)
1626
	 break;
1627
   }
1628
 
1629
   for (unsigned i = prev + 1; i < MESA_SHADER_TYPES; i++) {
1630
      if (prog->_LinkedShaders[i] == NULL)
1631
	 continue;
1632
 
1633
      assign_varying_locations(prog,
1634
			       prog->_LinkedShaders[prev],
1635
			       prog->_LinkedShaders[i]);
1636
      prev = i;
1637
   }
1638
 
1639
   if (prog->_LinkedShaders[MESA_SHADER_VERTEX] != NULL) {
1640
      demote_shader_inputs_and_outputs(prog->_LinkedShaders[MESA_SHADER_VERTEX],
1641
				       ir_var_out);
1642
   }
1643
 
1644
   if (prog->_LinkedShaders[MESA_SHADER_GEOMETRY] != NULL) {
1645
      gl_shader *const sh = prog->_LinkedShaders[MESA_SHADER_GEOMETRY];
1646
 
1647
      demote_shader_inputs_and_outputs(sh, ir_var_in);
1648
      demote_shader_inputs_and_outputs(sh, ir_var_inout);
1649
      demote_shader_inputs_and_outputs(sh, ir_var_out);
1650
   }
1651
 
1652
   if (prog->_LinkedShaders[MESA_SHADER_FRAGMENT] != NULL) {
1653
      gl_shader *const sh = prog->_LinkedShaders[MESA_SHADER_FRAGMENT];
1654
 
1655
      demote_shader_inputs_and_outputs(sh, ir_var_in);
1656
   }
1657
 
1658
   /* OpenGL ES requires that a vertex shader and a fragment shader both be
1659
    * present in a linked program.  By checking for use of shading language
1660
    * version 1.00, we also catch the GL_ARB_ES2_compatibility case.
1661
    */
1662
   if (ctx->API == API_OPENGLES2 || prog->Version == 100) {
1663
      if (prog->_LinkedShaders[MESA_SHADER_VERTEX] == NULL) {
1664
	 linker_error_printf(prog, "program lacks a vertex shader\n");
1665
	 prog->LinkStatus = false;
1666
      } else if (prog->_LinkedShaders[MESA_SHADER_FRAGMENT] == NULL) {
1667
	 linker_error_printf(prog, "program lacks a fragment shader\n");
1668
	 prog->LinkStatus = false;
1669
      }
1670
   }
1671
 
1672
   /* FINISHME: Assign fragment shader output locations. */
1673
 
1674
done:
1675
   free(vert_shader_list);
1676
 
1677
   for (unsigned i = 0; i < MESA_SHADER_TYPES; i++) {
1678
      if (prog->_LinkedShaders[i] == NULL)
1679
	 continue;
1680
 
1681
      /* Retain any live IR, but trash the rest. */
1682
      reparent_ir(prog->_LinkedShaders[i]->ir, prog->_LinkedShaders[i]->ir);
1683
   }
1684
 
1685
   ralloc_free(mem_ctx);
1686
}