2 * Mesa 3-D graphics library
5 * Copyright (C) 2008 Brian Paul All Rights Reserved.
6 * Copyright (C) 2009 VMware, Inc. All Rights Reserved.
8 * Permission is hereby granted, free of charge, to any person obtaining a
9 * copy of this software and associated documentation files (the "Software"),
10 * to deal in the Software without restriction, including without limitation
11 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
12 * and/or sell copies of the Software, and to permit persons to whom the
13 * Software is furnished to do so, subject to the following conditions:
15 * The above copyright notice and this permission notice shall be included
16 * in all copies or substantial portions of the Software.
18 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
19 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
20 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
21 * BRIAN PAUL BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
22 * AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
23 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
32 #include "main/imports.h"
33 #include "main/context.h"
34 #include "main/macros.h"
35 #include "shader/program.h"
36 #include "shader/prog_instruction.h"
37 #include "shader/prog_parameter.h"
38 #include "shader/prog_print.h"
39 #include "shader/prog_statevars.h"
40 #include "shader/prog_uniform.h"
41 #include "shader/shader_api.h"
42 #include "slang_builtin.h"
43 #include "slang_link.h"
47 static struct gl_vertex_program
*
48 vertex_program(struct gl_program
*prog
)
50 assert(prog
->Target
== GL_VERTEX_PROGRAM_ARB
);
51 return (struct gl_vertex_program
*) prog
;
56 static struct gl_fragment_program
*
57 fragment_program(struct gl_program
*prog
)
59 assert(prog
->Target
== GL_FRAGMENT_PROGRAM_ARB
);
60 return (struct gl_fragment_program
*) prog
;
65 * Record a linking error.
68 link_error(struct gl_shader_program
*shProg
, const char *msg
)
70 if (shProg
->InfoLog
) {
71 free(shProg
->InfoLog
);
73 shProg
->InfoLog
= _mesa_strdup(msg
);
74 shProg
->LinkStatus
= GL_FALSE
;
80 * Check if the given bit is either set or clear in both bitfields.
83 bits_agree(GLbitfield flags1
, GLbitfield flags2
, GLbitfield bit
)
85 return (flags1
& bit
) == (flags2
& bit
);
90 * Examine the outputs/varyings written by the vertex shader and
91 * append the names of those outputs onto the Varyings list.
92 * This will only capture the pre-defined/built-in varyings like
93 * gl_Position, not user-defined varyings.
96 update_varying_var_list(GLcontext
*ctx
, struct gl_shader_program
*shProg
)
98 if (shProg
->VertexProgram
) {
99 GLbitfield64 written
= shProg
->VertexProgram
->Base
.OutputsWritten
;
101 for (i
= 0; written
&& i
< VERT_RESULT_MAX
; i
++) {
102 if (written
& BITFIELD64_BIT(i
)) {
103 const char *name
= _slang_vertex_output_name(i
);
105 _mesa_add_varying(shProg
->Varying
, name
, 1, GL_FLOAT_VEC4
, 0x0);
106 written
&= ~BITFIELD64_BIT(i
);
114 * Do link error checking related to transform feedback.
117 link_transform_feedback(GLcontext
*ctx
, struct gl_shader_program
*shProg
)
119 GLbitfield varyingMask
;
120 GLuint totalComps
, maxComps
, i
;
122 if (shProg
->TransformFeedback
.NumVarying
== 0) {
127 /* Check that there's a vertex shader */
128 if (shProg
->TransformFeedback
.NumVarying
> 0 &&
129 !shProg
->VertexProgram
) {
130 link_error(shProg
, "Transform feedback without vertex shader");
134 /* Check that all named variables exist, and that none are duplicated.
135 * Also, build a count of the number of varying components to feedback.
139 for (i
= 0; i
< shProg
->TransformFeedback
.NumVarying
; i
++) {
140 const GLchar
*name
= shProg
->TransformFeedback
.VaryingNames
[i
];
141 GLint v
= _mesa_lookup_parameter_index(shProg
->Varying
, -1, name
);
142 struct gl_program_parameter
*p
;
146 _mesa_snprintf(msg
, sizeof(msg
),
147 "vertex shader does not emit %s", name
);
148 link_error(shProg
, msg
);
152 assert(v
< MAX_VARYING
);
154 /* already seen this varying name? */
155 if (varyingMask
& (1 << v
)) {
157 _mesa_snprintf(msg
, sizeof(msg
),
158 "duplicated transform feedback varying name: %s",
160 link_error(shProg
, msg
);
164 varyingMask
|= (1 << v
);
166 p
= &shProg
->Varying
->Parameters
[v
];
168 totalComps
+= _mesa_sizeof_glsl_type(p
->DataType
);
171 if (shProg
->TransformFeedback
.BufferMode
== GL_INTERLEAVED_ATTRIBS
)
172 maxComps
= ctx
->Const
.MaxTransformFeedbackInterleavedComponents
;
174 maxComps
= ctx
->Const
.MaxTransformFeedbackSeparateComponents
;
176 /* check max varying components against the limit */
177 if (totalComps
> maxComps
) {
179 _mesa_snprintf(msg
, sizeof(msg
),
180 "Too many feedback components: %u, max is %u",
181 totalComps
, maxComps
);
182 link_error(shProg
, msg
);
191 * Linking varying vars involves rearranging varying vars so that the
192 * vertex program's output varyings matches the order of the fragment
193 * program's input varyings.
194 * We'll then rewrite instructions to replace PROGRAM_VARYING with either
195 * PROGRAM_INPUT or PROGRAM_OUTPUT depending on whether it's a vertex or
197 * This is also where we set program Input/OutputFlags to indicate
198 * which inputs are centroid-sampled, invariant, etc.
201 link_varying_vars(GLcontext
*ctx
,
202 struct gl_shader_program
*shProg
, struct gl_program
*prog
)
204 GLuint
*map
, i
, firstVarying
, newFile
;
205 GLbitfield
*inOutFlags
;
207 map
= (GLuint
*) malloc(prog
->Varying
->NumParameters
* sizeof(GLuint
));
211 /* Varying variables are treated like other vertex program outputs
212 * (and like other fragment program inputs). The position of the
213 * first varying differs for vertex/fragment programs...
214 * Also, replace File=PROGRAM_VARYING with File=PROGRAM_INPUT/OUTPUT.
216 if (prog
->Target
== GL_VERTEX_PROGRAM_ARB
) {
217 firstVarying
= VERT_RESULT_VAR0
;
218 newFile
= PROGRAM_OUTPUT
;
219 inOutFlags
= prog
->OutputFlags
;
222 assert(prog
->Target
== GL_FRAGMENT_PROGRAM_ARB
);
223 firstVarying
= FRAG_ATTRIB_VAR0
;
224 newFile
= PROGRAM_INPUT
;
225 inOutFlags
= prog
->InputFlags
;
228 for (i
= 0; i
< prog
->Varying
->NumParameters
; i
++) {
229 /* see if this varying is in the linked varying list */
230 const struct gl_program_parameter
*var
= prog
->Varying
->Parameters
+ i
;
231 GLint j
= _mesa_lookup_parameter_index(shProg
->Varying
, -1, var
->Name
);
233 /* varying is already in list, do some error checking */
234 const struct gl_program_parameter
*v
=
235 &shProg
->Varying
->Parameters
[j
];
236 if (var
->Size
!= v
->Size
) {
237 link_error(shProg
, "mismatched varying variable types");
241 if (!bits_agree(var
->Flags
, v
->Flags
, PROG_PARAM_BIT_CENTROID
)) {
243 _mesa_snprintf(msg
, sizeof(msg
),
244 "centroid modifier mismatch for '%s'", var
->Name
);
245 link_error(shProg
, msg
);
249 if (!bits_agree(var
->Flags
, v
->Flags
, PROG_PARAM_BIT_INVARIANT
)) {
251 _mesa_snprintf(msg
, sizeof(msg
),
252 "invariant modifier mismatch for '%s'", var
->Name
);
253 link_error(shProg
, msg
);
259 /* not already in linked list */
260 j
= _mesa_add_varying(shProg
->Varying
, var
->Name
, var
->Size
,
261 var
->DataType
, var
->Flags
);
264 if (shProg
->Varying
->NumParameters
> ctx
->Const
.MaxVarying
) {
265 link_error(shProg
, "Too many varying variables");
270 /* Map varying[i] to varying[j].
271 * Note: the loop here takes care of arrays or large (sz>4) vars.
274 GLint sz
= var
->Size
;
276 inOutFlags
[firstVarying
+ j
] = var
->Flags
;
277 /*printf("Link varying from %d to %d\n", i, j);*/
281 i
--; /* go back one */
286 /* OK, now scan the program/shader instructions looking for varying vars,
287 * replacing the old index with the new index.
289 for (i
= 0; i
< prog
->NumInstructions
; i
++) {
290 struct prog_instruction
*inst
= prog
->Instructions
+ i
;
293 if (inst
->DstReg
.File
== PROGRAM_VARYING
) {
294 inst
->DstReg
.File
= newFile
;
295 inst
->DstReg
.Index
= map
[ inst
->DstReg
.Index
] + firstVarying
;
298 for (j
= 0; j
< 3; j
++) {
299 if (inst
->SrcReg
[j
].File
== PROGRAM_VARYING
) {
300 inst
->SrcReg
[j
].File
= newFile
;
301 inst
->SrcReg
[j
].Index
= map
[ inst
->SrcReg
[j
].Index
] + firstVarying
;
308 /* these will get recomputed before linking is completed */
309 prog
->InputsRead
= 0x0;
310 prog
->OutputsWritten
= 0x0;
317 * Build the shProg->Uniforms list.
318 * This is basically a list/index of all uniforms found in either/both of
319 * the vertex and fragment shaders.
322 * Each uniform has two indexes, one that points into the vertex
323 * program's parameter array and another that points into the fragment
324 * program's parameter array. When the user changes a uniform's value
325 * we have to change the value in the vertex and/or fragment program's
328 * This function will be called twice to set up the two uniform->parameter
331 * If a uniform is only present in the vertex program OR fragment program
332 * then the fragment/vertex parameter index, respectively, will be -1.
335 link_uniform_vars(GLcontext
*ctx
,
336 struct gl_shader_program
*shProg
,
337 struct gl_program
*prog
,
340 GLuint samplerMap
[200]; /* max number of samplers declared, not used */
343 for (i
= 0; i
< prog
->Parameters
->NumParameters
; i
++) {
344 const struct gl_program_parameter
*p
= prog
->Parameters
->Parameters
+ i
;
347 * XXX FIX NEEDED HERE
348 * We should also be adding a uniform if p->Type == PROGRAM_STATE_VAR.
349 * For example, modelview matrix, light pos, etc.
350 * Also, we need to update the state-var name-generator code to
351 * generate GLSL-style names, like "gl_LightSource[0].position".
352 * Furthermore, we'll need to fix the state-var's size/datatype info.
355 if ((p
->Type
== PROGRAM_UNIFORM
|| p
->Type
== PROGRAM_SAMPLER
)
357 /* add this uniform, indexing into the target's Parameters list */
358 struct gl_uniform
*uniform
=
359 _mesa_append_uniform(shProg
->Uniforms
, p
->Name
, prog
->Target
, i
);
361 uniform
->Initialized
= p
->Initialized
;
364 /* The samplerMap[] table we build here is used to remap/re-index
365 * sampler references by TEX instructions.
367 if (p
->Type
== PROGRAM_SAMPLER
&& p
->Used
) {
368 /* Allocate a new sampler index */
369 GLuint oldSampNum
= (GLuint
) prog
->Parameters
->ParameterValues
[i
][0];
370 GLuint newSampNum
= *numSamplers
;
371 if (newSampNum
>= ctx
->Const
.MaxTextureImageUnits
) {
373 _mesa_snprintf(s
, sizeof(s
),
374 "Too many texture samplers (%u, max is %u)",
375 newSampNum
, ctx
->Const
.MaxTextureImageUnits
);
376 link_error(shProg
, s
);
379 /* save old->new mapping in the table */
380 if (oldSampNum
< Elements(samplerMap
))
381 samplerMap
[oldSampNum
] = newSampNum
;
382 /* update parameter's sampler index */
383 prog
->Parameters
->ParameterValues
[i
][0] = (GLfloat
) newSampNum
;
388 /* OK, now scan the program/shader instructions looking for texture
389 * instructions using sampler vars. Replace old sampler indexes with
392 prog
->SamplersUsed
= 0x0;
393 for (i
= 0; i
< prog
->NumInstructions
; i
++) {
394 struct prog_instruction
*inst
= prog
->Instructions
+ i
;
395 if (_mesa_is_tex_instruction(inst
->Opcode
)) {
396 /* here, inst->TexSrcUnit is really the sampler unit */
397 const GLint oldSampNum
= inst
->TexSrcUnit
;
400 printf("====== remap sampler from %d to %d\n",
401 inst
->TexSrcUnit
, samplerMap
[ inst
->TexSrcUnit
]);
404 if (oldSampNum
< Elements(samplerMap
)) {
405 const GLuint newSampNum
= samplerMap
[oldSampNum
];
406 inst
->TexSrcUnit
= newSampNum
;
407 prog
->SamplerTargets
[newSampNum
] = inst
->TexSrcTarget
;
408 prog
->SamplersUsed
|= (1 << newSampNum
);
409 if (inst
->TexShadow
) {
410 prog
->ShadowSamplers
|= (1 << newSampNum
);
421 * Resolve binding of generic vertex attributes.
422 * For example, if the vertex shader declared "attribute vec4 foobar" we'll
423 * allocate a generic vertex attribute for "foobar" and plug that value into
424 * the vertex program instructions.
425 * But if the user called glBindAttributeLocation(), those bindings will
429 _slang_resolve_attributes(struct gl_shader_program
*shProg
,
430 const struct gl_program
*origProg
,
431 struct gl_program
*linkedProg
)
433 GLint attribMap
[MAX_VERTEX_GENERIC_ATTRIBS
];
435 GLbitfield usedAttributes
; /* generics only, not legacy attributes */
436 GLbitfield inputsRead
= 0x0;
438 assert(origProg
!= linkedProg
);
439 assert(origProg
->Target
== GL_VERTEX_PROGRAM_ARB
);
440 assert(linkedProg
->Target
== GL_VERTEX_PROGRAM_ARB
);
442 if (!shProg
->Attributes
)
443 shProg
->Attributes
= _mesa_new_parameter_list();
445 if (linkedProg
->Attributes
) {
446 _mesa_free_parameter_list(linkedProg
->Attributes
);
448 linkedProg
->Attributes
= _mesa_new_parameter_list();
451 /* Build a bitmask indicating which attribute indexes have been
452 * explicitly bound by the user with glBindAttributeLocation().
454 usedAttributes
= 0x0;
455 for (i
= 0; i
< shProg
->Attributes
->NumParameters
; i
++) {
456 GLint attr
= shProg
->Attributes
->Parameters
[i
].StateIndexes
[0];
457 usedAttributes
|= (1 << attr
);
460 /* If gl_Vertex is used, that actually counts against the limit
461 * on generic vertex attributes. This avoids the ambiguity of
462 * whether glVertexAttrib4fv(0, v) sets legacy attribute 0 (vert pos)
463 * or generic attribute[0]. If gl_Vertex is used, we want the former.
465 if (origProg
->InputsRead
& VERT_BIT_POS
) {
466 usedAttributes
|= 0x1;
469 /* initialize the generic attribute map entries to -1 */
470 for (i
= 0; i
< MAX_VERTEX_GENERIC_ATTRIBS
; i
++) {
475 * Scan program for generic attribute references
477 for (i
= 0; i
< linkedProg
->NumInstructions
; i
++) {
478 struct prog_instruction
*inst
= linkedProg
->Instructions
+ i
;
479 for (j
= 0; j
< 3; j
++) {
480 if (inst
->SrcReg
[j
].File
== PROGRAM_INPUT
) {
481 inputsRead
|= (1 << inst
->SrcReg
[j
].Index
);
484 if (inst
->SrcReg
[j
].File
== PROGRAM_INPUT
&&
485 inst
->SrcReg
[j
].Index
>= VERT_ATTRIB_GENERIC0
) {
487 * OK, we've found a generic vertex attribute reference.
489 const GLint k
= inst
->SrcReg
[j
].Index
- VERT_ATTRIB_GENERIC0
;
491 GLint attr
= attribMap
[k
];
494 /* Need to figure out attribute mapping now.
496 const char *name
= origProg
->Attributes
->Parameters
[k
].Name
;
497 const GLint size
= origProg
->Attributes
->Parameters
[k
].Size
;
498 const GLenum type
=origProg
->Attributes
->Parameters
[k
].DataType
;
501 /* See if there's a user-defined attribute binding for
504 index
= _mesa_lookup_parameter_index(shProg
->Attributes
,
507 /* Found a user-defined binding */
508 attr
= shProg
->Attributes
->Parameters
[index
].StateIndexes
[0];
511 /* No user-defined binding, choose our own attribute number.
512 * Start at 1 since generic attribute 0 always aliases
515 for (attr
= 0; attr
< MAX_VERTEX_GENERIC_ATTRIBS
; attr
++) {
516 if (((1 << attr
) & usedAttributes
) == 0)
519 if (attr
== MAX_VERTEX_GENERIC_ATTRIBS
) {
520 link_error(shProg
, "Too many vertex attributes");
524 /* mark this attribute as used */
525 usedAttributes
|= (1 << attr
);
530 /* Save the final name->attrib binding so it can be queried
531 * with glGetAttributeLocation().
533 _mesa_add_attribute(linkedProg
->Attributes
, name
,
539 /* update the instruction's src reg */
540 inst
->SrcReg
[j
].Index
= VERT_ATTRIB_GENERIC0
+ attr
;
545 /* Handle pre-defined attributes here (gl_Vertex, gl_Normal, etc).
546 * When the user queries the active attributes we need to include both
547 * the user-defined attributes and the built-in ones.
549 for (i
= VERT_ATTRIB_POS
; i
< VERT_ATTRIB_GENERIC0
; i
++) {
550 if (inputsRead
& (1 << i
)) {
551 _mesa_add_attribute(linkedProg
->Attributes
,
552 _slang_vert_attrib_name(i
),
553 4, /* size in floats */
554 _slang_vert_attrib_type(i
),
555 -1 /* attrib/input */);
564 * Scan program instructions to update the program's NumTemporaries field.
565 * Note: this implemenation relies on the code generator allocating
566 * temps in increasing order (0, 1, 2, ... ).
569 _slang_count_temporaries(struct gl_program
*prog
)
574 for (i
= 0; i
< prog
->NumInstructions
; i
++) {
575 const struct prog_instruction
*inst
= prog
->Instructions
+ i
;
576 const GLuint numSrc
= _mesa_num_inst_src_regs(inst
->Opcode
);
577 for (j
= 0; j
< numSrc
; j
++) {
578 if (inst
->SrcReg
[j
].File
== PROGRAM_TEMPORARY
) {
579 if (maxIndex
< inst
->SrcReg
[j
].Index
)
580 maxIndex
= inst
->SrcReg
[j
].Index
;
582 if (inst
->DstReg
.File
== PROGRAM_TEMPORARY
) {
583 if (maxIndex
< (GLint
) inst
->DstReg
.Index
)
584 maxIndex
= inst
->DstReg
.Index
;
589 prog
->NumTemporaries
= (GLuint
) (maxIndex
+ 1);
594 * Scan program instructions to update the program's InputsRead and
595 * OutputsWritten fields.
598 _slang_update_inputs_outputs(struct gl_program
*prog
)
601 GLuint maxAddrReg
= 0;
603 prog
->InputsRead
= 0x0;
604 prog
->OutputsWritten
= 0x0;
606 for (i
= 0; i
< prog
->NumInstructions
; i
++) {
607 const struct prog_instruction
*inst
= prog
->Instructions
+ i
;
608 const GLuint numSrc
= _mesa_num_inst_src_regs(inst
->Opcode
);
609 for (j
= 0; j
< numSrc
; j
++) {
610 if (inst
->SrcReg
[j
].File
== PROGRAM_INPUT
) {
611 prog
->InputsRead
|= 1 << inst
->SrcReg
[j
].Index
;
613 else if (inst
->SrcReg
[j
].File
== PROGRAM_ADDRESS
) {
614 maxAddrReg
= MAX2(maxAddrReg
, (GLuint
) (inst
->SrcReg
[j
].Index
+ 1));
618 if (inst
->DstReg
.File
== PROGRAM_OUTPUT
) {
619 prog
->OutputsWritten
|= BITFIELD64_BIT(inst
->DstReg
.Index
);
620 if (inst
->DstReg
.RelAddr
) {
621 /* If the output attribute is indexed with relative addressing
622 * we know that it must be a varying or texcoord such as
623 * gl_TexCoord[i] = v; In this case, mark all the texcoords
624 * or varying outputs as being written. It's not an error if
625 * a vertex shader writes varying vars that aren't used by the
626 * fragment shader. But it is an error for a fragment shader
627 * to use varyings that are not written by the vertex shader.
629 if (prog
->Target
== GL_VERTEX_PROGRAM_ARB
) {
630 if (inst
->DstReg
.Index
== VERT_RESULT_TEX0
) {
631 /* mark all texcoord outputs as written */
632 const GLbitfield64 mask
=
633 BITFIELD64_RANGE(VERT_RESULT_TEX0
,
635 + MAX_TEXTURE_COORD_UNITS
- 1));
636 prog
->OutputsWritten
|= mask
;
638 else if (inst
->DstReg
.Index
== VERT_RESULT_VAR0
) {
639 /* mark all generic varying outputs as written */
640 const GLbitfield64 mask
=
641 BITFIELD64_RANGE(VERT_RESULT_VAR0
,
642 (VERT_RESULT_VAR0
+ MAX_VARYING
- 1));
643 prog
->OutputsWritten
|= mask
;
648 else if (inst
->DstReg
.File
== PROGRAM_ADDRESS
) {
649 maxAddrReg
= MAX2(maxAddrReg
, inst
->DstReg
.Index
+ 1);
652 prog
->NumAddressRegs
= maxAddrReg
;
658 * Remove extra #version directives from the concatenated source string.
659 * Disable the extra ones by converting first two chars to //, a comment.
660 * This is a bit of hack to work around a preprocessor bug that only
661 * allows one #version directive per source.
664 remove_extra_version_directives(GLchar
*source
)
668 char *ver
= strstr(source
, "#version");
686 * Return a new shader whose source code is the concatenation of
687 * all the shader sources of the given type.
689 static struct gl_shader
*
690 concat_shaders(struct gl_shader_program
*shProg
, GLenum shaderType
)
692 struct gl_shader
*newShader
;
693 const struct gl_shader
*firstShader
= NULL
;
694 GLuint
*shaderLengths
;
696 GLuint totalLen
= 0, len
= 0;
699 shaderLengths
= (GLuint
*)malloc(shProg
->NumShaders
* sizeof(GLuint
));
700 if (!shaderLengths
) {
704 /* compute total size of new shader source code */
705 for (i
= 0; i
< shProg
->NumShaders
; i
++) {
706 const struct gl_shader
*shader
= shProg
->Shaders
[i
];
707 if (shader
->Type
== shaderType
) {
708 shaderLengths
[i
] = strlen(shader
->Source
);
709 totalLen
+= shaderLengths
[i
];
711 firstShader
= shader
;
720 source
= (GLchar
*) malloc(totalLen
+ 1);
726 /* concatenate shaders */
727 for (i
= 0; i
< shProg
->NumShaders
; i
++) {
728 const struct gl_shader
*shader
= shProg
->Shaders
[i
];
729 if (shader
->Type
== shaderType
) {
730 memcpy(source
+ len
, shader
->Source
, shaderLengths
[i
]);
731 len
+= shaderLengths
[i
];
736 printf("---NEW CONCATENATED SHADER---:\n%s\n------------\n", source);
741 remove_extra_version_directives(source
);
743 newShader
= CALLOC_STRUCT(gl_shader
);
749 newShader
->Type
= shaderType
;
750 newShader
->Source
= source
;
751 newShader
->Pragmas
= firstShader
->Pragmas
;
758 * Search the shader program's list of shaders to find the one that
760 * This will involve shader concatenation and recompilation if needed.
762 static struct gl_shader
*
763 get_main_shader(GLcontext
*ctx
,
764 struct gl_shader_program
*shProg
, GLenum type
)
766 struct gl_shader
*shader
= NULL
;
770 * Look for a shader that defines main() and has no unresolved references.
772 for (i
= 0; i
< shProg
->NumShaders
; i
++) {
773 shader
= shProg
->Shaders
[i
];
774 if (shader
->Type
== type
&&
776 !shader
->UnresolvedRefs
) {
783 * There must have been unresolved references during the original
784 * compilation. Try concatenating all the shaders of the given type
785 * and recompile that.
787 shader
= concat_shaders(shProg
, type
);
790 _slang_compile(ctx
, shader
);
792 /* Finally, check if recompiling failed */
793 if (!shader
->CompileStatus
||
795 shader
->UnresolvedRefs
) {
796 link_error(shProg
, "Unresolved symbols");
797 _mesa_free_shader(ctx
, shader
);
807 * Shader linker. Currently:
809 * 1. The last attached vertex shader and fragment shader are linked.
810 * 2. Varying vars in the two shaders are combined so their locations
811 * agree between the vertex and fragment stages. They're treated as
812 * vertex program output attribs and as fragment program input attribs.
813 * 3. The vertex and fragment programs are cloned and modified to update
814 * src/dst register references so they use the new, linked varying
818 _slang_link(GLcontext
*ctx
,
819 GLhandleARB programObj
,
820 struct gl_shader_program
*shProg
)
822 const struct gl_vertex_program
*vertProg
= NULL
;
823 const struct gl_fragment_program
*fragProg
= NULL
;
824 GLboolean vertNotify
= GL_TRUE
, fragNotify
= GL_TRUE
;
825 GLuint numSamplers
= 0;
828 _mesa_clear_shader_program_data(ctx
, shProg
);
830 /* Initialize LinkStatus to "success". Will be cleared if error. */
831 shProg
->LinkStatus
= GL_TRUE
;
833 /* check that all programs compiled successfully */
834 for (i
= 0; i
< shProg
->NumShaders
; i
++) {
835 if (!shProg
->Shaders
[i
]->CompileStatus
) {
836 link_error(shProg
, "linking with uncompiled shader\n");
841 shProg
->Uniforms
= _mesa_new_uniform_list();
842 shProg
->Varying
= _mesa_new_parameter_list();
845 * Find the vertex and fragment shaders which define main()
848 struct gl_shader
*vertShader
, *fragShader
;
849 vertShader
= get_main_shader(ctx
, shProg
, GL_VERTEX_SHADER
);
850 fragShader
= get_main_shader(ctx
, shProg
, GL_FRAGMENT_SHADER
);
852 vertProg
= vertex_program(vertShader
->Program
);
854 fragProg
= fragment_program(fragShader
->Program
);
855 if (!shProg
->LinkStatus
)
860 /* must have both a vertex and fragment program for ES2 */
861 if (ctx
->API
== API_OPENGLES2
) {
863 link_error(shProg
, "missing vertex shader\n");
867 link_error(shProg
, "missing fragment shader\n");
874 * Make copies of the vertex/fragment programs now since we'll be
875 * changing src/dst registers after merging the uniforms and varying vars.
877 _mesa_reference_vertprog(ctx
, &shProg
->VertexProgram
, NULL
);
879 struct gl_vertex_program
*linked_vprog
=
880 _mesa_clone_vertex_program(ctx
, vertProg
);
881 shProg
->VertexProgram
= linked_vprog
; /* refcount OK */
882 /* vertex program ID not significant; just set Id for debugging purposes */
883 shProg
->VertexProgram
->Base
.Id
= shProg
->Name
;
884 ASSERT(shProg
->VertexProgram
->Base
.RefCount
== 1);
887 _mesa_reference_fragprog(ctx
, &shProg
->FragmentProgram
, NULL
);
889 struct gl_fragment_program
*linked_fprog
=
890 _mesa_clone_fragment_program(ctx
, fragProg
);
891 shProg
->FragmentProgram
= linked_fprog
; /* refcount OK */
892 /* vertex program ID not significant; just set Id for debugging purposes */
893 shProg
->FragmentProgram
->Base
.Id
= shProg
->Name
;
894 ASSERT(shProg
->FragmentProgram
->Base
.RefCount
== 1);
897 /* link varying vars */
898 if (shProg
->VertexProgram
) {
899 if (!link_varying_vars(ctx
, shProg
, &shProg
->VertexProgram
->Base
))
902 if (shProg
->FragmentProgram
) {
903 if (!link_varying_vars(ctx
, shProg
, &shProg
->FragmentProgram
->Base
))
907 /* link uniform vars */
908 if (shProg
->VertexProgram
) {
909 if (!link_uniform_vars(ctx
, shProg
, &shProg
->VertexProgram
->Base
,
914 if (shProg
->FragmentProgram
) {
915 if (!link_uniform_vars(ctx
, shProg
, &shProg
->FragmentProgram
->Base
,
921 /*_mesa_print_uniforms(shProg->Uniforms);*/
923 if (shProg
->VertexProgram
) {
924 if (!_slang_resolve_attributes(shProg
, &vertProg
->Base
,
925 &shProg
->VertexProgram
->Base
)) {
930 if (shProg
->VertexProgram
) {
931 _slang_update_inputs_outputs(&shProg
->VertexProgram
->Base
);
932 _slang_count_temporaries(&shProg
->VertexProgram
->Base
);
933 if (!(shProg
->VertexProgram
->Base
.OutputsWritten
934 & BITFIELD64_BIT(VERT_RESULT_HPOS
))) {
935 /* the vertex program did not compute a vertex position */
937 "gl_Position was not written by vertex shader\n");
941 if (shProg
->FragmentProgram
) {
942 _slang_count_temporaries(&shProg
->FragmentProgram
->Base
);
943 _slang_update_inputs_outputs(&shProg
->FragmentProgram
->Base
);
946 /* Check that all the varying vars needed by the fragment shader are
947 * actually produced by the vertex shader.
949 if (shProg
->FragmentProgram
) {
950 const GLbitfield varyingRead
951 = shProg
->FragmentProgram
->Base
.InputsRead
>> FRAG_ATTRIB_VAR0
;
952 const GLbitfield64 varyingWritten
= shProg
->VertexProgram
?
953 shProg
->VertexProgram
->Base
.OutputsWritten
>> VERT_RESULT_VAR0
: 0x0;
954 if ((varyingRead
& varyingWritten
) != varyingRead
) {
956 "Fragment program using varying vars not written by vertex shader\n");
961 /* check that gl_FragColor and gl_FragData are not both written to */
962 if (shProg
->FragmentProgram
) {
963 const GLbitfield64 outputsWritten
=
964 shProg
->FragmentProgram
->Base
.OutputsWritten
;
965 if ((outputsWritten
& BITFIELD64_BIT(FRAG_RESULT_COLOR
)) &&
966 (outputsWritten
>= BITFIELD64_BIT(FRAG_RESULT_DATA0
))) {
967 link_error(shProg
, "Fragment program cannot write both gl_FragColor"
968 " and gl_FragData[].\n");
973 update_varying_var_list(ctx
, shProg
);
975 /* checks related to transform feedback */
976 if (!link_transform_feedback(ctx
, shProg
)) {
980 if (fragProg
&& shProg
->FragmentProgram
) {
981 /* Compute initial program's TexturesUsed info */
982 _mesa_update_shader_textures_used(&shProg
->FragmentProgram
->Base
);
984 /* notify driver that a new fragment program has been compiled/linked */
985 vertNotify
= ctx
->Driver
.ProgramStringNotify(ctx
, GL_FRAGMENT_PROGRAM_ARB
,
986 &shProg
->FragmentProgram
->Base
);
987 if (ctx
->Shader
.Flags
& GLSL_DUMP
) {
988 printf("Mesa pre-link fragment program:\n");
989 _mesa_print_program(&fragProg
->Base
);
990 _mesa_print_program_parameters(ctx
, &fragProg
->Base
);
992 printf("Mesa post-link fragment program:\n");
993 _mesa_print_program(&shProg
->FragmentProgram
->Base
);
994 _mesa_print_program_parameters(ctx
, &shProg
->FragmentProgram
->Base
);
998 if (vertProg
&& shProg
->VertexProgram
) {
999 /* Compute initial program's TexturesUsed info */
1000 _mesa_update_shader_textures_used(&shProg
->VertexProgram
->Base
);
1002 /* notify driver that a new vertex program has been compiled/linked */
1003 fragNotify
= ctx
->Driver
.ProgramStringNotify(ctx
, GL_VERTEX_PROGRAM_ARB
,
1004 &shProg
->VertexProgram
->Base
);
1005 if (ctx
->Shader
.Flags
& GLSL_DUMP
) {
1006 printf("Mesa pre-link vertex program:\n");
1007 _mesa_print_program(&vertProg
->Base
);
1008 _mesa_print_program_parameters(ctx
, &vertProg
->Base
);
1010 printf("Mesa post-link vertex program:\n");
1011 _mesa_print_program(&shProg
->VertexProgram
->Base
);
1012 _mesa_print_program_parameters(ctx
, &shProg
->VertexProgram
->Base
);
1018 if (shProg
->VertexProgram
)
1019 _mesa_postprocess_program(ctx
, &shProg
->VertexProgram
->Base
);
1020 if (shProg
->FragmentProgram
)
1021 _mesa_postprocess_program(ctx
, &shProg
->FragmentProgram
->Base
);
1024 if (ctx
->Shader
.Flags
& GLSL_DUMP
) {
1025 printf("Varying vars:\n");
1026 _mesa_print_parameter_list(shProg
->Varying
);
1027 if (shProg
->InfoLog
) {
1028 printf("Info Log: %s\n", shProg
->InfoLog
);
1032 if (!vertNotify
|| !fragNotify
) {
1033 /* driver rejected one/both of the vertex/fragment programs */
1034 if (!shProg
->InfoLog
) {
1036 "Vertex and/or fragment program rejected by driver\n");
1040 shProg
->LinkStatus
= (shProg
->VertexProgram
|| shProg
->FragmentProgram
);