2 * Copyright © 2010 Intel Corporation
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:
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
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.
26 * GLSL linker implementation
28 * Given a set of shaders that are to be linked to generate a final program,
29 * there are three distinct stages.
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
35 * - Undefined references in each shader are resolve to definitions in
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.
42 * The result, in the terminology of the GLSL spec, is a set of shader
43 * executables for each processing unit.
45 * After the first stage is complete, a series of semantic checks are performed
46 * on each of the shader executables.
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
53 * In the final stage individual shader executables are linked to create a
54 * complete exectuable.
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.
64 * \author Ian Romanick <ian.d.romanick@intel.com>
74 #include "main/mtypes.h"
75 #include "glsl_symbol_table.h"
76 #include "glsl_parser_extras.h"
78 #include "ir_optimization.h"
80 #include "hash_table.h"
81 #include "shader_api.h"
84 * Visitor that determines whether or not a variable is ever written.
86 class find_assignment_visitor
: public ir_hierarchical_visitor
{
88 find_assignment_visitor(const char *name
)
89 : name(name
), found(false)
94 virtual ir_visitor_status
visit_enter(ir_assignment
*ir
)
96 ir_variable
*const var
= ir
->lhs
->variable_referenced();
98 if (strcmp(name
, var
->name
) == 0) {
103 return visit_continue_with_parent
;
106 bool variable_found()
112 const char *name
; /**< Find writes to a variable with this name. */
113 bool found
; /**< Was a write to the variable found? */
118 linker_error_printf(gl_shader_program
*prog
, const char *fmt
, ...)
122 prog
->InfoLog
= talloc_strdup_append(prog
->InfoLog
, "error: ");
124 prog
->InfoLog
= talloc_vasprintf_append(prog
->InfoLog
, fmt
, ap
);
130 invalidate_variable_locations(gl_shader
*sh
, enum ir_variable_mode mode
,
133 foreach_list(node
, sh
->ir
) {
134 ir_variable
*const var
= ((ir_instruction
*) node
)->as_variable();
136 if ((var
== NULL
) || (var
->mode
!= (unsigned) mode
))
139 /* Only assign locations for generic attributes / varyings / etc.
141 if (var
->location
>= generic_base
)
148 * Determine the number of attribute slots required for a particular type
150 * This code is here because it implements the language rules of a specific
151 * GLSL version. Since it's a property of the language and not a property of
152 * types in general, it doesn't really belong in glsl_type.
155 count_attribute_slots(const glsl_type
*t
)
157 /* From page 31 (page 37 of the PDF) of the GLSL 1.50 spec:
159 * "A scalar input counts the same amount against this limit as a vec4,
160 * so applications may want to consider packing groups of four
161 * unrelated float inputs together into a vector to better utilize the
162 * capabilities of the underlying hardware. A matrix input will use up
163 * multiple locations. The number of locations used will equal the
164 * number of columns in the matrix."
166 * The spec does not explicitly say how arrays are counted. However, it
167 * should be safe to assume the total number of slots consumed by an array
168 * is the number of entries in the array multiplied by the number of slots
169 * consumed by a single element of the array.
173 return t
->array_size() * count_attribute_slots(t
->element_type());
176 return t
->matrix_columns
;
183 * Verify that a vertex shader executable meets all semantic requirements
185 * \param shader Vertex shader executable to be verified
188 validate_vertex_shader_executable(struct gl_shader_program
*prog
,
189 struct gl_shader
*shader
)
194 if (!shader
->symbols
->get_function("main")) {
195 linker_error_printf(prog
, "vertex shader lacks `main'\n");
199 find_assignment_visitor
find("gl_Position");
200 find
.run(shader
->ir
);
201 if (!find
.variable_found()) {
202 linker_error_printf(prog
,
203 "vertex shader does not write to `gl_Position'\n");
212 * Verify that a fragment shader executable meets all semantic requirements
214 * \param shader Fragment shader executable to be verified
217 validate_fragment_shader_executable(struct gl_shader_program
*prog
,
218 struct gl_shader
*shader
)
223 if (!shader
->symbols
->get_function("main")) {
224 linker_error_printf(prog
, "fragment shader lacks `main'\n");
228 find_assignment_visitor
frag_color("gl_FragColor");
229 find_assignment_visitor
frag_data("gl_FragData");
231 frag_color
.run(shader
->ir
);
232 frag_data
.run(shader
->ir
);
234 if (frag_color
.variable_found() && frag_data
.variable_found()) {
235 linker_error_printf(prog
, "fragment shader writes to both "
236 "`gl_FragColor' and `gl_FragData'\n");
245 * Perform validation of uniforms used across multiple shader stages
248 cross_validate_uniforms(struct gl_shader_program
*prog
)
250 /* Examine all of the uniforms in all of the shaders and cross validate
253 glsl_symbol_table uniforms
;
254 for (unsigned i
= 0; i
< prog
->_NumLinkedShaders
; i
++) {
255 foreach_list(node
, prog
->_LinkedShaders
[i
]->ir
) {
256 ir_variable
*const var
= ((ir_instruction
*) node
)->as_variable();
258 if ((var
== NULL
) || (var
->mode
!= ir_var_uniform
))
261 /* If a uniform with this name has already been seen, verify that the
262 * new instance has the same type. In addition, if the uniforms have
263 * initializers, the values of the initializers must be the same.
265 ir_variable
*const existing
= uniforms
.get_variable(var
->name
);
266 if (existing
!= NULL
) {
267 if (var
->type
!= existing
->type
) {
268 linker_error_printf(prog
, "uniform `%s' declared as type "
269 "`%s' and type `%s'\n",
270 var
->name
, var
->type
->name
,
271 existing
->type
->name
);
275 if (var
->constant_value
!= NULL
) {
276 if (existing
->constant_value
!= NULL
) {
277 if (!var
->constant_value
->has_value(existing
->constant_value
)) {
278 linker_error_printf(prog
, "initializers for uniform "
279 "`%s' have differing values\n",
284 /* If the first-seen instance of a particular uniform did not
285 * have an initializer but a later instance does, copy the
286 * initializer to the version stored in the symbol table.
288 existing
->constant_value
=
289 (ir_constant
*)var
->constant_value
->clone(NULL
);
292 uniforms
.add_variable(var
->name
, var
);
301 * Validate that outputs from one stage match inputs of another
304 cross_validate_outputs_to_inputs(struct gl_shader_program
*prog
,
305 gl_shader
*producer
, gl_shader
*consumer
)
307 glsl_symbol_table parameters
;
308 /* FINISHME: Figure these out dynamically. */
309 const char *const producer_stage
= "vertex";
310 const char *const consumer_stage
= "fragment";
312 /* Find all shader outputs in the "producer" stage.
314 foreach_list(node
, producer
->ir
) {
315 ir_variable
*const var
= ((ir_instruction
*) node
)->as_variable();
317 /* FINISHME: For geometry shaders, this should also look for inout
318 * FINISHME: variables.
320 if ((var
== NULL
) || (var
->mode
!= ir_var_out
))
323 parameters
.add_variable(var
->name
, var
);
327 /* Find all shader inputs in the "consumer" stage. Any variables that have
328 * matching outputs already in the symbol table must have the same type and
331 foreach_list(node
, consumer
->ir
) {
332 ir_variable
*const input
= ((ir_instruction
*) node
)->as_variable();
334 /* FINISHME: For geometry shaders, this should also look for inout
335 * FINISHME: variables.
337 if ((input
== NULL
) || (input
->mode
!= ir_var_in
))
340 ir_variable
*const output
= parameters
.get_variable(input
->name
);
341 if (output
!= NULL
) {
342 /* Check that the types match between stages.
344 if (input
->type
!= output
->type
) {
345 linker_error_printf(prog
,
346 "%s shader output `%s' delcared as "
347 "type `%s', but %s shader input declared "
349 producer_stage
, output
->name
,
351 consumer_stage
, input
->type
->name
);
355 /* Check that all of the qualifiers match between stages.
357 if (input
->centroid
!= output
->centroid
) {
358 linker_error_printf(prog
,
359 "%s shader output `%s' %s centroid qualifier, "
360 "but %s shader input %s centroid qualifier\n",
363 (output
->centroid
) ? "has" : "lacks",
365 (input
->centroid
) ? "has" : "lacks");
369 if (input
->invariant
!= output
->invariant
) {
370 linker_error_printf(prog
,
371 "%s shader output `%s' %s invariant qualifier, "
372 "but %s shader input %s invariant qualifier\n",
375 (output
->invariant
) ? "has" : "lacks",
377 (input
->invariant
) ? "has" : "lacks");
381 if (input
->interpolation
!= output
->interpolation
) {
382 linker_error_printf(prog
,
383 "%s shader output `%s' specifies %s "
384 "interpolation qualifier, "
385 "but %s shader input specifies %s "
386 "interpolation qualifier\n",
389 output
->interpolation_string(),
391 input
->interpolation_string());
402 * Populates a shaders symbol table with all global declarations
405 populate_symbol_table(gl_shader
*sh
)
407 sh
->symbols
= new(sh
) glsl_symbol_table
;
409 foreach_list(node
, sh
->ir
) {
410 ir_instruction
*const inst
= (ir_instruction
*) node
;
414 if ((func
= inst
->as_function()) != NULL
) {
415 sh
->symbols
->add_function(func
->name
, func
);
416 } else if ((var
= inst
->as_variable()) != NULL
) {
417 sh
->symbols
->add_variable(var
->name
, var
);
424 * Combine a group of shaders for a single stage to generate a linked shader
427 * If this function is supplied a single shader, it is cloned, and the new
428 * shader is returned.
430 static struct gl_shader
*
431 link_intrastage_shaders(struct gl_shader_program
*prog
,
432 struct gl_shader
**shader_list
,
433 unsigned num_shaders
)
436 assert(num_shaders
== 1);
438 gl_shader
*const linked
= _mesa_new_shader(NULL
, 0, shader_list
[0]->Type
);
439 linked
->ir
= new(linked
) exec_list
;
440 clone_ir_list(linked
->ir
, shader_list
[0]->ir
);
442 populate_symbol_table(linked
);
448 struct uniform_node
{
450 struct gl_uniform
*u
;
455 assign_uniform_locations(struct gl_shader_program
*prog
)
459 unsigned total_uniforms
= 0;
460 hash_table
*ht
= hash_table_ctor(32, hash_table_string_hash
,
461 hash_table_string_compare
);
463 for (unsigned i
= 0; i
< prog
->_NumLinkedShaders
; i
++) {
464 unsigned next_position
= 0;
466 foreach_list(node
, prog
->_LinkedShaders
[i
]->ir
) {
467 ir_variable
*const var
= ((ir_instruction
*) node
)->as_variable();
469 if ((var
== NULL
) || (var
->mode
!= ir_var_uniform
))
472 const unsigned vec4_slots
= (var
->component_slots() + 3) / 4;
473 assert(vec4_slots
!= 0);
475 uniform_node
*n
= (uniform_node
*) hash_table_find(ht
, var
->name
);
477 n
= (uniform_node
*) calloc(1, sizeof(struct uniform_node
));
478 n
->u
= (gl_uniform
*) calloc(vec4_slots
, sizeof(struct gl_uniform
));
479 n
->slots
= vec4_slots
;
481 n
->u
[0].Name
= strdup(var
->name
);
482 for (unsigned j
= 1; j
< vec4_slots
; j
++)
483 n
->u
[j
].Name
= n
->u
[0].Name
;
485 hash_table_insert(ht
, n
, n
->u
[0].Name
);
486 uniforms
.push_tail(& n
->link
);
487 total_uniforms
+= vec4_slots
;
490 if (var
->constant_value
!= NULL
)
491 for (unsigned j
= 0; j
< vec4_slots
; j
++)
492 n
->u
[j
].Initialized
= true;
494 var
->location
= next_position
;
496 for (unsigned j
= 0; j
< vec4_slots
; j
++) {
497 switch (prog
->_LinkedShaders
[i
]->Type
) {
498 case GL_VERTEX_SHADER
:
499 n
->u
[j
].VertPos
= next_position
;
501 case GL_FRAGMENT_SHADER
:
502 n
->u
[j
].FragPos
= next_position
;
504 case GL_GEOMETRY_SHADER
:
505 /* FINISHME: Support geometry shaders. */
506 assert(prog
->_LinkedShaders
[i
]->Type
!= GL_GEOMETRY_SHADER
);
515 gl_uniform_list
*ul
= (gl_uniform_list
*)
516 calloc(1, sizeof(gl_uniform_list
));
518 ul
->Size
= total_uniforms
;
519 ul
->NumUniforms
= total_uniforms
;
520 ul
->Uniforms
= (gl_uniform
*) calloc(total_uniforms
, sizeof(gl_uniform
));
524 for (uniform_node
*node
= (uniform_node
*) uniforms
.head
525 ; node
->link
.next
!= NULL
527 next
= (uniform_node
*) node
->link
.next
;
530 memcpy(&ul
->Uniforms
[idx
], node
->u
, sizeof(gl_uniform
) * node
->slots
);
544 * Find a contiguous set of available bits in a bitmask
546 * \param used_mask Bits representing used (1) and unused (0) locations
547 * \param needed_count Number of contiguous bits needed.
550 * Base location of the available bits on success or -1 on failure.
553 find_available_slots(unsigned used_mask
, unsigned needed_count
)
555 unsigned needed_mask
= (1 << needed_count
) - 1;
556 const int max_bit_to_test
= (8 * sizeof(used_mask
)) - needed_count
;
558 /* The comparison to 32 is redundant, but without it GCC emits "warning:
559 * cannot optimize possibly infinite loops" for the loop below.
561 if ((needed_count
== 0) || (max_bit_to_test
< 0) || (max_bit_to_test
> 32))
564 for (int i
= 0; i
<= max_bit_to_test
; i
++) {
565 if ((needed_mask
& ~used_mask
) == needed_mask
)
576 assign_attribute_locations(gl_shader_program
*prog
, unsigned max_attribute_index
)
578 /* Mark invalid attribute locations as being used.
580 unsigned used_locations
= (max_attribute_index
>= 32)
581 ? ~0 : ~((1 << max_attribute_index
) - 1);
583 gl_shader
*const sh
= prog
->_LinkedShaders
[0];
584 assert(sh
->Type
== GL_VERTEX_SHADER
);
586 /* Operate in a total of four passes.
588 * 1. Invalidate the location assignments for all vertex shader inputs.
590 * 2. Assign locations for inputs that have user-defined (via
591 * glBindVertexAttribLocation) locatoins.
593 * 3. Sort the attributes without assigned locations by number of slots
594 * required in decreasing order. Fragmentation caused by attribute
595 * locations assigned by the application may prevent large attributes
596 * from having enough contiguous space.
598 * 4. Assign locations to any inputs without assigned locations.
601 invalidate_variable_locations(sh
, ir_var_in
, VERT_ATTRIB_GENERIC0
);
603 if (prog
->Attributes
!= NULL
) {
604 for (unsigned i
= 0; i
< prog
->Attributes
->NumParameters
; i
++) {
605 ir_variable
*const var
=
606 sh
->symbols
->get_variable(prog
->Attributes
->Parameters
[i
].Name
);
608 /* Note: attributes that occupy multiple slots, such as arrays or
609 * matrices, may appear in the attrib array multiple times.
611 if ((var
== NULL
) || (var
->location
!= -1))
614 /* From page 61 of the OpenGL 4.0 spec:
616 * "LinkProgram will fail if the attribute bindings assigned by
617 * BindAttribLocation do not leave not enough space to assign a
618 * location for an active matrix attribute or an active attribute
619 * array, both of which require multiple contiguous generic
622 * Previous versions of the spec contain similar language but omit the
623 * bit about attribute arrays.
625 * Page 61 of the OpenGL 4.0 spec also says:
627 * "It is possible for an application to bind more than one
628 * attribute name to the same location. This is referred to as
629 * aliasing. This will only work if only one of the aliased
630 * attributes is active in the executable program, or if no path
631 * through the shader consumes more than one attribute of a set
632 * of attributes aliased to the same location. A link error can
633 * occur if the linker determines that every path through the
634 * shader consumes multiple aliased attributes, but
635 * implementations are not required to generate an error in this
638 * These two paragraphs are either somewhat contradictory, or I don't
639 * fully understand one or both of them.
641 /* FINISHME: The code as currently written does not support attribute
642 * FINISHME: location aliasing (see comment above).
644 const int attr
= prog
->Attributes
->Parameters
[i
].StateIndexes
[0];
645 const unsigned slots
= count_attribute_slots(var
->type
);
647 /* Mask representing the contiguous slots that will be used by this
650 const unsigned use_mask
= (1 << slots
) - 1;
652 /* Generate a link error if the set of bits requested for this
653 * attribute overlaps any previously allocated bits.
655 if ((~(use_mask
<< attr
) & used_locations
) != used_locations
) {
656 linker_error_printf(prog
,
657 "insufficient contiguous attribute locations "
658 "available for vertex shader input `%s'",
663 var
->location
= VERT_ATTRIB_GENERIC0
+ attr
;
664 used_locations
|= (use_mask
<< attr
);
668 /* Temporary storage for the set of attributes that need locations assigned.
674 /* Used below in the call to qsort. */
675 static int compare(const void *a
, const void *b
)
677 const temp_attr
*const l
= (const temp_attr
*) a
;
678 const temp_attr
*const r
= (const temp_attr
*) b
;
680 /* Reversed because we want a descending order sort below. */
681 return r
->slots
- l
->slots
;
685 unsigned num_attr
= 0;
687 foreach_list(node
, sh
->ir
) {
688 ir_variable
*const var
= ((ir_instruction
*) node
)->as_variable();
690 if ((var
== NULL
) || (var
->mode
!= ir_var_in
))
693 /* The location was explicitly assigned, nothing to do here.
695 if (var
->location
!= -1)
698 to_assign
[num_attr
].slots
= count_attribute_slots(var
->type
);
699 to_assign
[num_attr
].var
= var
;
703 /* If all of the attributes were assigned locations by the application (or
704 * are built-in attributes with fixed locations), return early. This should
705 * be the common case.
710 qsort(to_assign
, num_attr
, sizeof(to_assign
[0]), temp_attr::compare
);
712 /* VERT_ATTRIB_GENERIC0 is a psdueo-alias for VERT_ATTRIB_POS. It can only
713 * be explicitly assigned by via glBindAttribLocation. Mark it as reserved
714 * to prevent it from being automatically allocated below.
716 used_locations
|= (1 << 0);
718 for (unsigned i
= 0; i
< num_attr
; i
++) {
719 /* Mask representing the contiguous slots that will be used by this
722 const unsigned use_mask
= (1 << to_assign
[i
].slots
) - 1;
724 int location
= find_available_slots(used_locations
, to_assign
[i
].slots
);
727 linker_error_printf(prog
,
728 "insufficient contiguous attribute locations "
729 "available for vertex shader input `%s'",
730 to_assign
[i
].var
->name
);
734 to_assign
[i
].var
->location
= VERT_ATTRIB_GENERIC0
+ location
;
735 used_locations
|= (use_mask
<< location
);
743 assign_varying_locations(gl_shader
*producer
, gl_shader
*consumer
)
745 /* FINISHME: Set dynamically when geometry shader support is added. */
746 unsigned output_index
= VERT_RESULT_VAR0
;
747 unsigned input_index
= FRAG_ATTRIB_VAR0
;
749 /* Operate in a total of three passes.
751 * 1. Assign locations for any matching inputs and outputs.
753 * 2. Mark output variables in the producer that do not have locations as
754 * not being outputs. This lets the optimizer eliminate them.
756 * 3. Mark input variables in the consumer that do not have locations as
757 * not being inputs. This lets the optimizer eliminate them.
760 invalidate_variable_locations(producer
, ir_var_out
, VERT_RESULT_VAR0
);
761 invalidate_variable_locations(consumer
, ir_var_in
, FRAG_ATTRIB_VAR0
);
763 foreach_list(node
, producer
->ir
) {
764 ir_variable
*const output_var
= ((ir_instruction
*) node
)->as_variable();
766 if ((output_var
== NULL
) || (output_var
->mode
!= ir_var_out
)
767 || (output_var
->location
!= -1))
770 ir_variable
*const input_var
=
771 consumer
->symbols
->get_variable(output_var
->name
);
773 if ((input_var
== NULL
) || (input_var
->mode
!= ir_var_in
))
776 assert(input_var
->location
== -1);
778 /* FINISHME: Location assignment will need some changes when arrays,
779 * FINISHME: matrices, and structures are allowed as shader inputs /
782 output_var
->location
= output_index
;
783 input_var
->location
= input_index
;
789 foreach_list(node
, producer
->ir
) {
790 ir_variable
*const var
= ((ir_instruction
*) node
)->as_variable();
792 if ((var
== NULL
) || (var
->mode
!= ir_var_out
))
795 /* An 'out' variable is only really a shader output if its value is read
796 * by the following stage.
798 var
->shader_out
= (var
->location
!= -1);
801 foreach_list(node
, consumer
->ir
) {
802 ir_variable
*const var
= ((ir_instruction
*) node
)->as_variable();
804 if ((var
== NULL
) || (var
->mode
!= ir_var_in
))
807 /* An 'in' variable is only really a shader input if its value is written
808 * by the previous stage.
810 var
->shader_in
= (var
->location
!= -1);
816 link_shaders(struct gl_shader_program
*prog
)
818 prog
->LinkStatus
= false;
819 prog
->Validated
= false;
822 if (prog
->InfoLog
!= NULL
)
823 talloc_free(prog
->InfoLog
);
825 prog
->InfoLog
= talloc_strdup(NULL
, "");
827 /* Separate the shaders into groups based on their type.
829 struct gl_shader
**vert_shader_list
;
830 unsigned num_vert_shaders
= 0;
831 struct gl_shader
**frag_shader_list
;
832 unsigned num_frag_shaders
= 0;
834 vert_shader_list
= (struct gl_shader
**)
835 calloc(2 * prog
->NumShaders
, sizeof(struct gl_shader
*));
836 frag_shader_list
= &vert_shader_list
[prog
->NumShaders
];
838 for (unsigned i
= 0; i
< prog
->NumShaders
; i
++) {
839 switch (prog
->Shaders
[i
]->Type
) {
840 case GL_VERTEX_SHADER
:
841 vert_shader_list
[num_vert_shaders
] = prog
->Shaders
[i
];
844 case GL_FRAGMENT_SHADER
:
845 frag_shader_list
[num_frag_shaders
] = prog
->Shaders
[i
];
848 case GL_GEOMETRY_SHADER
:
849 /* FINISHME: Support geometry shaders. */
850 assert(prog
->Shaders
[i
]->Type
!= GL_GEOMETRY_SHADER
);
855 /* FINISHME: Implement intra-stage linking. */
856 prog
->_NumLinkedShaders
= 0;
857 if (num_vert_shaders
> 0) {
858 gl_shader
*const sh
=
859 link_intrastage_shaders(prog
, vert_shader_list
, num_vert_shaders
);
864 if (!validate_vertex_shader_executable(prog
, sh
))
867 prog
->_LinkedShaders
[prog
->_NumLinkedShaders
] = sh
;
868 prog
->_NumLinkedShaders
++;
871 if (num_frag_shaders
> 0) {
872 gl_shader
*const sh
=
873 link_intrastage_shaders(prog
, frag_shader_list
, num_frag_shaders
);
878 if (!validate_fragment_shader_executable(prog
, sh
))
881 prog
->_LinkedShaders
[prog
->_NumLinkedShaders
] = sh
;
882 prog
->_NumLinkedShaders
++;
885 /* Here begins the inter-stage linking phase. Some initial validation is
886 * performed, then locations are assigned for uniforms, attributes, and
889 if (cross_validate_uniforms(prog
)) {
890 /* Validate the inputs of each stage with the output of the preceeding
893 for (unsigned i
= 1; i
< prog
->_NumLinkedShaders
; i
++) {
894 if (!cross_validate_outputs_to_inputs(prog
,
895 prog
->_LinkedShaders
[i
- 1],
896 prog
->_LinkedShaders
[i
]))
900 prog
->LinkStatus
= true;
903 /* FINISHME: Perform whole-program optimization here. */
905 assign_uniform_locations(prog
);
907 if (prog
->_LinkedShaders
[0]->Type
== GL_VERTEX_SHADER
)
908 /* FINISHME: The value of the max_attribute_index parameter is
909 * FINISHME: implementation dependent based on the value of
910 * FINISHME: GL_MAX_VERTEX_ATTRIBS. GL_MAX_VERTEX_ATTRIBS must be
911 * FINISHME: at least 16, so hardcode 16 for now.
913 if (!assign_attribute_locations(prog
, 16))
916 for (unsigned i
= 1; i
< prog
->_NumLinkedShaders
; i
++)
917 assign_varying_locations(prog
->_LinkedShaders
[i
- 1],
918 prog
->_LinkedShaders
[i
]);
920 /* FINISHME: Assign fragment shader output locations. */
923 free(vert_shader_list
);