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 _mesa_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 * Linking varying vars involves rearranging varying vars so that the
91 * vertex program's output varyings matches the order of the fragment
92 * program's input varyings.
93 * We'll then rewrite instructions to replace PROGRAM_VARYING with either
94 * PROGRAM_INPUT or PROGRAM_OUTPUT depending on whether it's a vertex or
96 * This is also where we set program Input/OutputFlags to indicate
97 * which inputs are centroid-sampled, invariant, etc.
100 link_varying_vars(GLcontext
*ctx
,
101 struct gl_shader_program
*shProg
, struct gl_program
*prog
)
103 GLuint
*map
, i
, firstVarying
, newFile
;
104 GLbitfield
*inOutFlags
;
106 map
= (GLuint
*) _mesa_malloc(prog
->Varying
->NumParameters
* sizeof(GLuint
));
110 /* Varying variables are treated like other vertex program outputs
111 * (and like other fragment program inputs). The position of the
112 * first varying differs for vertex/fragment programs...
113 * Also, replace File=PROGRAM_VARYING with File=PROGRAM_INPUT/OUTPUT.
115 if (prog
->Target
== GL_VERTEX_PROGRAM_ARB
) {
116 firstVarying
= VERT_RESULT_VAR0
;
117 newFile
= PROGRAM_OUTPUT
;
118 inOutFlags
= prog
->OutputFlags
;
121 assert(prog
->Target
== GL_FRAGMENT_PROGRAM_ARB
);
122 firstVarying
= FRAG_ATTRIB_VAR0
;
123 newFile
= PROGRAM_INPUT
;
124 inOutFlags
= prog
->InputFlags
;
127 for (i
= 0; i
< prog
->Varying
->NumParameters
; i
++) {
128 /* see if this varying is in the linked varying list */
129 const struct gl_program_parameter
*var
= prog
->Varying
->Parameters
+ i
;
130 GLint j
= _mesa_lookup_parameter_index(shProg
->Varying
, -1, var
->Name
);
132 /* varying is already in list, do some error checking */
133 const struct gl_program_parameter
*v
=
134 &shProg
->Varying
->Parameters
[j
];
135 if (var
->Size
!= v
->Size
) {
136 link_error(shProg
, "mismatched varying variable types");
140 if (!bits_agree(var
->Flags
, v
->Flags
, PROG_PARAM_BIT_CENTROID
)) {
142 _mesa_snprintf(msg
, sizeof(msg
),
143 "centroid modifier mismatch for '%s'", var
->Name
);
144 link_error(shProg
, msg
);
148 if (!bits_agree(var
->Flags
, v
->Flags
, PROG_PARAM_BIT_INVARIANT
)) {
150 _mesa_snprintf(msg
, sizeof(msg
),
151 "invariant modifier mismatch for '%s'", var
->Name
);
152 link_error(shProg
, msg
);
158 /* not already in linked list */
159 j
= _mesa_add_varying(shProg
->Varying
, var
->Name
, var
->Size
,
163 if (shProg
->Varying
->NumParameters
> ctx
->Const
.MaxVarying
) {
164 link_error(shProg
, "Too many varying variables");
169 /* Map varying[i] to varying[j].
170 * Note: the loop here takes care of arrays or large (sz>4) vars.
173 GLint sz
= var
->Size
;
175 inOutFlags
[firstVarying
+ j
] = var
->Flags
;
176 /*printf("Link varying from %d to %d\n", i, j);*/
180 i
--; /* go back one */
185 /* OK, now scan the program/shader instructions looking for varying vars,
186 * replacing the old index with the new index.
188 for (i
= 0; i
< prog
->NumInstructions
; i
++) {
189 struct prog_instruction
*inst
= prog
->Instructions
+ i
;
192 if (inst
->DstReg
.File
== PROGRAM_VARYING
) {
193 inst
->DstReg
.File
= newFile
;
194 inst
->DstReg
.Index
= map
[ inst
->DstReg
.Index
] + firstVarying
;
197 for (j
= 0; j
< 3; j
++) {
198 if (inst
->SrcReg
[j
].File
== PROGRAM_VARYING
) {
199 inst
->SrcReg
[j
].File
= newFile
;
200 inst
->SrcReg
[j
].Index
= map
[ inst
->SrcReg
[j
].Index
] + firstVarying
;
207 /* these will get recomputed before linking is completed */
208 prog
->InputsRead
= 0x0;
209 prog
->OutputsWritten
= 0x0;
216 * Build the shProg->Uniforms list.
217 * This is basically a list/index of all uniforms found in either/both of
218 * the vertex and fragment shaders.
221 * Each uniform has two indexes, one that points into the vertex
222 * program's parameter array and another that points into the fragment
223 * program's parameter array. When the user changes a uniform's value
224 * we have to change the value in the vertex and/or fragment program's
227 * This function will be called twice to set up the two uniform->parameter
230 * If a uniform is only present in the vertex program OR fragment program
231 * then the fragment/vertex parameter index, respectively, will be -1.
234 link_uniform_vars(GLcontext
*ctx
,
235 struct gl_shader_program
*shProg
,
236 struct gl_program
*prog
,
239 GLuint samplerMap
[200]; /* max number of samplers declared, not used */
242 for (i
= 0; i
< prog
->Parameters
->NumParameters
; i
++) {
243 const struct gl_program_parameter
*p
= prog
->Parameters
->Parameters
+ i
;
246 * XXX FIX NEEDED HERE
247 * We should also be adding a uniform if p->Type == PROGRAM_STATE_VAR.
248 * For example, modelview matrix, light pos, etc.
249 * Also, we need to update the state-var name-generator code to
250 * generate GLSL-style names, like "gl_LightSource[0].position".
251 * Furthermore, we'll need to fix the state-var's size/datatype info.
254 if ((p
->Type
== PROGRAM_UNIFORM
|| p
->Type
== PROGRAM_SAMPLER
)
256 /* add this uniform, indexing into the target's Parameters list */
257 struct gl_uniform
*uniform
=
258 _mesa_append_uniform(shProg
->Uniforms
, p
->Name
, prog
->Target
, i
);
260 uniform
->Initialized
= p
->Initialized
;
263 /* The samplerMap[] table we build here is used to remap/re-index
264 * sampler references by TEX instructions.
266 if (p
->Type
== PROGRAM_SAMPLER
&& p
->Used
) {
267 /* Allocate a new sampler index */
268 GLuint oldSampNum
= (GLuint
) prog
->Parameters
->ParameterValues
[i
][0];
269 GLuint newSampNum
= *numSamplers
;
270 if (newSampNum
>= ctx
->Const
.MaxTextureImageUnits
) {
272 _mesa_sprintf(s
, "Too many texture samplers (%u, max is %u)",
273 newSampNum
, ctx
->Const
.MaxTextureImageUnits
);
274 link_error(shProg
, s
);
277 /* save old->new mapping in the table */
278 if (oldSampNum
< Elements(samplerMap
))
279 samplerMap
[oldSampNum
] = newSampNum
;
280 /* update parameter's sampler index */
281 prog
->Parameters
->ParameterValues
[i
][0] = (GLfloat
) newSampNum
;
286 /* OK, now scan the program/shader instructions looking for texture
287 * instructions using sampler vars. Replace old sampler indexes with
290 prog
->SamplersUsed
= 0x0;
291 for (i
= 0; i
< prog
->NumInstructions
; i
++) {
292 struct prog_instruction
*inst
= prog
->Instructions
+ i
;
293 if (_mesa_is_tex_instruction(inst
->Opcode
)) {
294 /* here, inst->TexSrcUnit is really the sampler unit */
295 const GLint oldSampNum
= inst
->TexSrcUnit
;
298 printf("====== remap sampler from %d to %d\n",
299 inst
->TexSrcUnit
, samplerMap
[ inst
->TexSrcUnit
]);
302 if (oldSampNum
< Elements(samplerMap
)) {
303 const GLuint newSampNum
= samplerMap
[oldSampNum
];
304 inst
->TexSrcUnit
= newSampNum
;
305 prog
->SamplerTargets
[newSampNum
] = inst
->TexSrcTarget
;
306 prog
->SamplersUsed
|= (1 << newSampNum
);
307 if (inst
->TexShadow
) {
308 prog
->ShadowSamplers
|= (1 << newSampNum
);
319 * Resolve binding of generic vertex attributes.
320 * For example, if the vertex shader declared "attribute vec4 foobar" we'll
321 * allocate a generic vertex attribute for "foobar" and plug that value into
322 * the vertex program instructions.
323 * But if the user called glBindAttributeLocation(), those bindings will
327 _slang_resolve_attributes(struct gl_shader_program
*shProg
,
328 const struct gl_program
*origProg
,
329 struct gl_program
*linkedProg
)
331 GLint attribMap
[MAX_VERTEX_GENERIC_ATTRIBS
];
333 GLbitfield usedAttributes
; /* generics only, not legacy attributes */
334 GLbitfield inputsRead
= 0x0;
336 assert(origProg
!= linkedProg
);
337 assert(origProg
->Target
== GL_VERTEX_PROGRAM_ARB
);
338 assert(linkedProg
->Target
== GL_VERTEX_PROGRAM_ARB
);
340 if (!shProg
->Attributes
)
341 shProg
->Attributes
= _mesa_new_parameter_list();
343 if (linkedProg
->Attributes
) {
344 _mesa_free_parameter_list(linkedProg
->Attributes
);
346 linkedProg
->Attributes
= _mesa_new_parameter_list();
349 /* Build a bitmask indicating which attribute indexes have been
350 * explicitly bound by the user with glBindAttributeLocation().
352 usedAttributes
= 0x0;
353 for (i
= 0; i
< shProg
->Attributes
->NumParameters
; i
++) {
354 GLint attr
= shProg
->Attributes
->Parameters
[i
].StateIndexes
[0];
355 usedAttributes
|= (1 << attr
);
358 /* If gl_Vertex is used, that actually counts against the limit
359 * on generic vertex attributes. This avoids the ambiguity of
360 * whether glVertexAttrib4fv(0, v) sets legacy attribute 0 (vert pos)
361 * or generic attribute[0]. If gl_Vertex is used, we want the former.
363 if (origProg
->InputsRead
& VERT_BIT_POS
) {
364 usedAttributes
|= 0x1;
367 /* initialize the generic attribute map entries to -1 */
368 for (i
= 0; i
< MAX_VERTEX_GENERIC_ATTRIBS
; i
++) {
373 * Scan program for generic attribute references
375 for (i
= 0; i
< linkedProg
->NumInstructions
; i
++) {
376 struct prog_instruction
*inst
= linkedProg
->Instructions
+ i
;
377 for (j
= 0; j
< 3; j
++) {
378 if (inst
->SrcReg
[j
].File
== PROGRAM_INPUT
) {
379 inputsRead
|= (1 << inst
->SrcReg
[j
].Index
);
382 if (inst
->SrcReg
[j
].File
== PROGRAM_INPUT
&&
383 inst
->SrcReg
[j
].Index
>= VERT_ATTRIB_GENERIC0
) {
385 * OK, we've found a generic vertex attribute reference.
387 const GLint k
= inst
->SrcReg
[j
].Index
- VERT_ATTRIB_GENERIC0
;
389 GLint attr
= attribMap
[k
];
392 /* Need to figure out attribute mapping now.
394 const char *name
= origProg
->Attributes
->Parameters
[k
].Name
;
395 const GLint size
= origProg
->Attributes
->Parameters
[k
].Size
;
396 const GLenum type
=origProg
->Attributes
->Parameters
[k
].DataType
;
399 /* See if there's a user-defined attribute binding for
402 index
= _mesa_lookup_parameter_index(shProg
->Attributes
,
405 /* Found a user-defined binding */
406 attr
= shProg
->Attributes
->Parameters
[index
].StateIndexes
[0];
409 /* No user-defined binding, choose our own attribute number.
410 * Start at 1 since generic attribute 0 always aliases
413 for (attr
= 0; attr
< MAX_VERTEX_GENERIC_ATTRIBS
; attr
++) {
414 if (((1 << attr
) & usedAttributes
) == 0)
417 if (attr
== MAX_VERTEX_GENERIC_ATTRIBS
) {
418 link_error(shProg
, "Too many vertex attributes");
422 /* mark this attribute as used */
423 usedAttributes
|= (1 << attr
);
428 /* Save the final name->attrib binding so it can be queried
429 * with glGetAttributeLocation().
431 _mesa_add_attribute(linkedProg
->Attributes
, name
,
437 /* update the instruction's src reg */
438 inst
->SrcReg
[j
].Index
= VERT_ATTRIB_GENERIC0
+ attr
;
443 /* Handle pre-defined attributes here (gl_Vertex, gl_Normal, etc).
444 * When the user queries the active attributes we need to include both
445 * the user-defined attributes and the built-in ones.
447 for (i
= VERT_ATTRIB_POS
; i
< VERT_ATTRIB_GENERIC0
; i
++) {
448 if (inputsRead
& (1 << i
)) {
449 _mesa_add_attribute(linkedProg
->Attributes
,
450 _slang_vert_attrib_name(i
),
451 4, /* size in floats */
452 _slang_vert_attrib_type(i
),
453 -1 /* attrib/input */);
462 * Scan program instructions to update the program's NumTemporaries field.
463 * Note: this implemenation relies on the code generator allocating
464 * temps in increasing order (0, 1, 2, ... ).
467 _slang_count_temporaries(struct gl_program
*prog
)
472 for (i
= 0; i
< prog
->NumInstructions
; i
++) {
473 const struct prog_instruction
*inst
= prog
->Instructions
+ i
;
474 const GLuint numSrc
= _mesa_num_inst_src_regs(inst
->Opcode
);
475 for (j
= 0; j
< numSrc
; j
++) {
476 if (inst
->SrcReg
[j
].File
== PROGRAM_TEMPORARY
) {
477 if (maxIndex
< inst
->SrcReg
[j
].Index
)
478 maxIndex
= inst
->SrcReg
[j
].Index
;
480 if (inst
->DstReg
.File
== PROGRAM_TEMPORARY
) {
481 if (maxIndex
< (GLint
) inst
->DstReg
.Index
)
482 maxIndex
= inst
->DstReg
.Index
;
487 prog
->NumTemporaries
= (GLuint
) (maxIndex
+ 1);
492 * Scan program instructions to update the program's InputsRead and
493 * OutputsWritten fields.
496 _slang_update_inputs_outputs(struct gl_program
*prog
)
499 GLuint maxAddrReg
= 0;
501 prog
->InputsRead
= 0x0;
502 prog
->OutputsWritten
= 0x0;
504 for (i
= 0; i
< prog
->NumInstructions
; i
++) {
505 const struct prog_instruction
*inst
= prog
->Instructions
+ i
;
506 const GLuint numSrc
= _mesa_num_inst_src_regs(inst
->Opcode
);
507 for (j
= 0; j
< numSrc
; j
++) {
508 if (inst
->SrcReg
[j
].File
== PROGRAM_INPUT
) {
509 prog
->InputsRead
|= 1 << inst
->SrcReg
[j
].Index
;
511 else if (inst
->SrcReg
[j
].File
== PROGRAM_ADDRESS
) {
512 maxAddrReg
= MAX2(maxAddrReg
, (GLuint
) (inst
->SrcReg
[j
].Index
+ 1));
516 if (inst
->DstReg
.File
== PROGRAM_OUTPUT
) {
517 prog
->OutputsWritten
|= BITFIELD64_BIT(inst
->DstReg
.Index
);
518 if (inst
->DstReg
.RelAddr
) {
519 /* If the output attribute is indexed with relative addressing
520 * we know that it must be a varying or texcoord such as
521 * gl_TexCoord[i] = v; In this case, mark all the texcoords
522 * or varying outputs as being written. It's not an error if
523 * a vertex shader writes varying vars that aren't used by the
524 * fragment shader. But it is an error for a fragment shader
525 * to use varyings that are not written by the vertex shader.
527 if (prog
->Target
== GL_VERTEX_PROGRAM_ARB
) {
528 if (inst
->DstReg
.Index
== VERT_RESULT_TEX0
) {
529 /* mark all texcoord outputs as written */
530 const GLbitfield64 mask
=
531 BITFIELD64_RANGE(VERT_RESULT_TEX0
,
533 + MAX_TEXTURE_COORD_UNITS
- 1));
534 prog
->OutputsWritten
|= mask
;
536 else if (inst
->DstReg
.Index
== VERT_RESULT_VAR0
) {
537 /* mark all generic varying outputs as written */
538 const GLbitfield64 mask
=
539 BITFIELD64_RANGE(VERT_RESULT_VAR0
,
540 (VERT_RESULT_VAR0
+ MAX_VARYING
- 1));
541 prog
->OutputsWritten
|= mask
;
546 else if (inst
->DstReg
.File
== PROGRAM_ADDRESS
) {
547 maxAddrReg
= MAX2(maxAddrReg
, inst
->DstReg
.Index
+ 1);
550 prog
->NumAddressRegs
= maxAddrReg
;
556 * Remove extra #version directives from the concatenated source string.
557 * Disable the extra ones by converting first two chars to //, a comment.
558 * This is a bit of hack to work around a preprocessor bug that only
559 * allows one #version directive per source.
562 remove_extra_version_directives(GLchar
*source
)
566 char *ver
= _mesa_strstr(source
, "#version");
584 * Return a new shader whose source code is the concatenation of
585 * all the shader sources of the given type.
587 static struct gl_shader
*
588 concat_shaders(struct gl_shader_program
*shProg
, GLenum shaderType
)
590 struct gl_shader
*newShader
;
591 const struct gl_shader
*firstShader
= NULL
;
592 GLuint
*shaderLengths
;
594 GLuint totalLen
= 0, len
= 0;
597 shaderLengths
= (GLuint
*)_mesa_malloc(shProg
->NumShaders
* sizeof(GLuint
));
598 if (!shaderLengths
) {
602 /* compute total size of new shader source code */
603 for (i
= 0; i
< shProg
->NumShaders
; i
++) {
604 const struct gl_shader
*shader
= shProg
->Shaders
[i
];
605 if (shader
->Type
== shaderType
) {
606 shaderLengths
[i
] = _mesa_strlen(shader
->Source
);
607 totalLen
+= shaderLengths
[i
];
609 firstShader
= shader
;
614 _mesa_free(shaderLengths
);
618 source
= (GLchar
*) _mesa_malloc(totalLen
+ 1);
620 _mesa_free(shaderLengths
);
624 /* concatenate shaders */
625 for (i
= 0; i
< shProg
->NumShaders
; i
++) {
626 const struct gl_shader
*shader
= shProg
->Shaders
[i
];
627 if (shader
->Type
== shaderType
) {
628 _mesa_memcpy(source
+ len
, shader
->Source
, shaderLengths
[i
]);
629 len
+= shaderLengths
[i
];
634 _mesa_printf("---NEW CONCATENATED SHADER---:\n%s\n------------\n", source);
637 _mesa_free(shaderLengths
);
639 remove_extra_version_directives(source
);
641 newShader
= CALLOC_STRUCT(gl_shader
);
647 newShader
->Type
= shaderType
;
648 newShader
->Source
= source
;
649 newShader
->Pragmas
= firstShader
->Pragmas
;
656 * Search the shader program's list of shaders to find the one that
658 * This will involve shader concatenation and recompilation if needed.
660 static struct gl_shader
*
661 get_main_shader(GLcontext
*ctx
,
662 struct gl_shader_program
*shProg
, GLenum type
)
664 struct gl_shader
*shader
= NULL
;
668 * Look for a shader that defines main() and has no unresolved references.
670 for (i
= 0; i
< shProg
->NumShaders
; i
++) {
671 shader
= shProg
->Shaders
[i
];
672 if (shader
->Type
== type
&&
674 !shader
->UnresolvedRefs
) {
681 * There must have been unresolved references during the original
682 * compilation. Try concatenating all the shaders of the given type
683 * and recompile that.
685 shader
= concat_shaders(shProg
, type
);
688 _slang_compile(ctx
, shader
);
690 /* Finally, check if recompiling failed */
691 if (!shader
->CompileStatus
||
693 shader
->UnresolvedRefs
) {
694 link_error(shProg
, "Unresolved symbols");
695 _mesa_free_shader(ctx
, shader
);
705 * Shader linker. Currently:
707 * 1. The last attached vertex shader and fragment shader are linked.
708 * 2. Varying vars in the two shaders are combined so their locations
709 * agree between the vertex and fragment stages. They're treated as
710 * vertex program output attribs and as fragment program input attribs.
711 * 3. The vertex and fragment programs are cloned and modified to update
712 * src/dst register references so they use the new, linked varying
716 _slang_link(GLcontext
*ctx
,
717 GLhandleARB programObj
,
718 struct gl_shader_program
*shProg
)
720 const struct gl_vertex_program
*vertProg
= NULL
;
721 const struct gl_fragment_program
*fragProg
= NULL
;
722 GLuint numSamplers
= 0;
725 _mesa_clear_shader_program_data(ctx
, shProg
);
727 /* Initialize LinkStatus to "success". Will be cleared if error. */
728 shProg
->LinkStatus
= GL_TRUE
;
730 /* check that all programs compiled successfully */
731 for (i
= 0; i
< shProg
->NumShaders
; i
++) {
732 if (!shProg
->Shaders
[i
]->CompileStatus
) {
733 link_error(shProg
, "linking with uncompiled shader\n");
738 shProg
->Uniforms
= _mesa_new_uniform_list();
739 shProg
->Varying
= _mesa_new_parameter_list();
742 * Find the vertex and fragment shaders which define main()
745 struct gl_shader
*vertShader
, *fragShader
;
746 vertShader
= get_main_shader(ctx
, shProg
, GL_VERTEX_SHADER
);
747 fragShader
= get_main_shader(ctx
, shProg
, GL_FRAGMENT_SHADER
);
749 vertProg
= vertex_program(vertShader
->Program
);
751 fragProg
= fragment_program(fragShader
->Program
);
752 if (!shProg
->LinkStatus
)
757 /* must have both a vertex and fragment program for ES2 */
759 link_error(shProg
, "missing vertex shader\n");
763 link_error(shProg
, "missing fragment shader\n");
769 * Make copies of the vertex/fragment programs now since we'll be
770 * changing src/dst registers after merging the uniforms and varying vars.
772 _mesa_reference_vertprog(ctx
, &shProg
->VertexProgram
, NULL
);
774 struct gl_vertex_program
*linked_vprog
=
775 vertex_program(_mesa_clone_program(ctx
, &vertProg
->Base
));
776 shProg
->VertexProgram
= linked_vprog
; /* refcount OK */
777 /* vertex program ID not significant; just set Id for debugging purposes */
778 shProg
->VertexProgram
->Base
.Id
= shProg
->Name
;
779 ASSERT(shProg
->VertexProgram
->Base
.RefCount
== 1);
782 _mesa_reference_fragprog(ctx
, &shProg
->FragmentProgram
, NULL
);
784 struct gl_fragment_program
*linked_fprog
=
785 fragment_program(_mesa_clone_program(ctx
, &fragProg
->Base
));
786 shProg
->FragmentProgram
= linked_fprog
; /* refcount OK */
787 /* vertex program ID not significant; just set Id for debugging purposes */
788 shProg
->FragmentProgram
->Base
.Id
= shProg
->Name
;
789 ASSERT(shProg
->FragmentProgram
->Base
.RefCount
== 1);
792 /* link varying vars */
793 if (shProg
->VertexProgram
) {
794 if (!link_varying_vars(ctx
, shProg
, &shProg
->VertexProgram
->Base
))
797 if (shProg
->FragmentProgram
) {
798 if (!link_varying_vars(ctx
, shProg
, &shProg
->FragmentProgram
->Base
))
802 /* link uniform vars */
803 if (shProg
->VertexProgram
) {
804 if (!link_uniform_vars(ctx
, shProg
, &shProg
->VertexProgram
->Base
,
809 if (shProg
->FragmentProgram
) {
810 if (!link_uniform_vars(ctx
, shProg
, &shProg
->FragmentProgram
->Base
,
816 /*_mesa_print_uniforms(shProg->Uniforms);*/
818 if (shProg
->VertexProgram
) {
819 if (!_slang_resolve_attributes(shProg
, &vertProg
->Base
,
820 &shProg
->VertexProgram
->Base
)) {
825 if (shProg
->VertexProgram
) {
826 _slang_update_inputs_outputs(&shProg
->VertexProgram
->Base
);
827 _slang_count_temporaries(&shProg
->VertexProgram
->Base
);
828 if (!(shProg
->VertexProgram
->Base
.OutputsWritten
829 & BITFIELD64_BIT(VERT_RESULT_HPOS
))) {
830 /* the vertex program did not compute a vertex position */
832 "gl_Position was not written by vertex shader\n");
836 if (shProg
->FragmentProgram
) {
837 _slang_count_temporaries(&shProg
->FragmentProgram
->Base
);
838 _slang_update_inputs_outputs(&shProg
->FragmentProgram
->Base
);
841 /* Check that all the varying vars needed by the fragment shader are
842 * actually produced by the vertex shader.
844 if (shProg
->FragmentProgram
) {
845 const GLbitfield varyingRead
846 = shProg
->FragmentProgram
->Base
.InputsRead
>> FRAG_ATTRIB_VAR0
;
847 const GLbitfield64 varyingWritten
= shProg
->VertexProgram
?
848 shProg
->VertexProgram
->Base
.OutputsWritten
>> VERT_RESULT_VAR0
: 0x0;
849 if ((varyingRead
& varyingWritten
) != varyingRead
) {
851 "Fragment program using varying vars not written by vertex shader\n");
856 /* check that gl_FragColor and gl_FragData are not both written to */
857 if (shProg
->FragmentProgram
) {
858 const GLbitfield64 outputsWritten
=
859 shProg
->FragmentProgram
->Base
.OutputsWritten
;
860 if ((outputsWritten
& BITFIELD64_BIT(FRAG_RESULT_COLOR
)) &&
861 (outputsWritten
>= BITFIELD64_BIT(FRAG_RESULT_DATA0
))) {
862 link_error(shProg
, "Fragment program cannot write both gl_FragColor"
863 " and gl_FragData[].\n");
869 if (fragProg
&& shProg
->FragmentProgram
) {
870 /* Compute initial program's TexturesUsed info */
871 _mesa_update_shader_textures_used(&shProg
->FragmentProgram
->Base
);
873 /* notify driver that a new fragment program has been compiled/linked */
874 ctx
->Driver
.ProgramStringNotify(ctx
, GL_FRAGMENT_PROGRAM_ARB
,
875 &shProg
->FragmentProgram
->Base
);
876 if (ctx
->Shader
.Flags
& GLSL_DUMP
) {
877 _mesa_printf("Mesa pre-link fragment program:\n");
878 _mesa_print_program(&fragProg
->Base
);
879 _mesa_print_program_parameters(ctx
, &fragProg
->Base
);
881 _mesa_printf("Mesa post-link fragment program:\n");
882 _mesa_print_program(&shProg
->FragmentProgram
->Base
);
883 _mesa_print_program_parameters(ctx
, &shProg
->FragmentProgram
->Base
);
887 if (vertProg
&& shProg
->VertexProgram
) {
888 /* Compute initial program's TexturesUsed info */
889 _mesa_update_shader_textures_used(&shProg
->VertexProgram
->Base
);
891 /* notify driver that a new vertex program has been compiled/linked */
892 ctx
->Driver
.ProgramStringNotify(ctx
, GL_VERTEX_PROGRAM_ARB
,
893 &shProg
->VertexProgram
->Base
);
894 if (ctx
->Shader
.Flags
& GLSL_DUMP
) {
895 _mesa_printf("Mesa pre-link vertex program:\n");
896 _mesa_print_program(&vertProg
->Base
);
897 _mesa_print_program_parameters(ctx
, &vertProg
->Base
);
899 _mesa_printf("Mesa post-link vertex program:\n");
900 _mesa_print_program(&shProg
->VertexProgram
->Base
);
901 _mesa_print_program_parameters(ctx
, &shProg
->VertexProgram
->Base
);
907 if (shProg
->VertexProgram
)
908 _mesa_postprocess_program(ctx
, &shProg
->VertexProgram
->Base
);
909 if (shProg
->FragmentProgram
)
910 _mesa_postprocess_program(ctx
, &shProg
->FragmentProgram
->Base
);
913 if (ctx
->Shader
.Flags
& GLSL_DUMP
) {
914 _mesa_printf("Varying vars:\n");
915 _mesa_print_parameter_list(shProg
->Varying
);
916 if (shProg
->InfoLog
) {
917 _mesa_printf("Info Log: %s\n", shProg
->InfoLog
);
921 shProg
->LinkStatus
= (shProg
->VertexProgram
|| shProg
->FragmentProgram
);