glsl: new MESA_GLSL env var for GLSL debugging features
[mesa.git] / src / mesa / shader / slang / slang_link.c
1 /*
2 * Mesa 3-D graphics library
3 * Version: 7.3
4 *
5 * Copyright (C) 2008 Brian Paul All Rights Reserved.
6 * Copyright (C) 2009 VMware, Inc. All Rights Reserved.
7 *
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:
14 *
15 * The above copyright notice and this permission notice shall be included
16 * in all copies or substantial portions of the Software.
17 *
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.
24 */
25
26 /**
27 * \file slang_link.c
28 * GLSL linker
29 * \author Brian Paul
30 */
31
32 #include "main/imports.h"
33 #include "main/context.h"
34 #include "main/hash.h"
35 #include "main/macros.h"
36 #include "shader/program.h"
37 #include "shader/prog_instruction.h"
38 #include "shader/prog_parameter.h"
39 #include "shader/prog_print.h"
40 #include "shader/prog_statevars.h"
41 #include "shader/prog_uniform.h"
42 #include "shader/shader_api.h"
43 #include "slang_link.h"
44
45
46 /** cast wrapper */
47 static struct gl_vertex_program *
48 vertex_program(struct gl_program *prog)
49 {
50 assert(prog->Target == GL_VERTEX_PROGRAM_ARB);
51 return (struct gl_vertex_program *) prog;
52 }
53
54
55 /** cast wrapper */
56 static struct gl_fragment_program *
57 fragment_program(struct gl_program *prog)
58 {
59 assert(prog->Target == GL_FRAGMENT_PROGRAM_ARB);
60 return (struct gl_fragment_program *) prog;
61 }
62
63
64 /**
65 * Record a linking error.
66 */
67 static void
68 link_error(struct gl_shader_program *shProg, const char *msg)
69 {
70 if (shProg->InfoLog) {
71 _mesa_free(shProg->InfoLog);
72 }
73 shProg->InfoLog = _mesa_strdup(msg);
74 shProg->LinkStatus = GL_FALSE;
75 }
76
77
78
79 /**
80 * Check if the given bit is either set or clear in both bitfields.
81 */
82 static GLboolean
83 bits_agree(GLbitfield flags1, GLbitfield flags2, GLbitfield bit)
84 {
85 return (flags1 & bit) == (flags2 & bit);
86 }
87
88
89 /**
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
95 * fragment shader.
96 * This is also where we set program Input/OutputFlags to indicate
97 * which inputs are centroid-sampled, invariant, etc.
98 */
99 static GLboolean
100 link_varying_vars(struct gl_shader_program *shProg, struct gl_program *prog)
101 {
102 GLuint *map, i, firstVarying, newFile;
103 GLbitfield *inOutFlags;
104
105 map = (GLuint *) malloc(prog->Varying->NumParameters * sizeof(GLuint));
106 if (!map)
107 return GL_FALSE;
108
109 /* Varying variables are treated like other vertex program outputs
110 * (and like other fragment program inputs). The position of the
111 * first varying differs for vertex/fragment programs...
112 * Also, replace File=PROGRAM_VARYING with File=PROGRAM_INPUT/OUTPUT.
113 */
114 if (prog->Target == GL_VERTEX_PROGRAM_ARB) {
115 firstVarying = VERT_RESULT_VAR0;
116 newFile = PROGRAM_OUTPUT;
117 inOutFlags = prog->OutputFlags;
118 }
119 else {
120 assert(prog->Target == GL_FRAGMENT_PROGRAM_ARB);
121 firstVarying = FRAG_ATTRIB_VAR0;
122 newFile = PROGRAM_INPUT;
123 inOutFlags = prog->InputFlags;
124 }
125
126 for (i = 0; i < prog->Varying->NumParameters; i++) {
127 /* see if this varying is in the linked varying list */
128 const struct gl_program_parameter *var = prog->Varying->Parameters + i;
129 GLint j = _mesa_lookup_parameter_index(shProg->Varying, -1, var->Name);
130 if (j >= 0) {
131 /* varying is already in list, do some error checking */
132 const struct gl_program_parameter *v =
133 &shProg->Varying->Parameters[j];
134 if (var->Size != v->Size) {
135 link_error(shProg, "mismatched varying variable types");
136 return GL_FALSE;
137 }
138 if (!bits_agree(var->Flags, v->Flags, PROG_PARAM_BIT_CENTROID)) {
139 char msg[100];
140 _mesa_snprintf(msg, sizeof(msg),
141 "centroid modifier mismatch for '%s'", var->Name);
142 link_error(shProg, msg);
143 return GL_FALSE;
144 }
145 if (!bits_agree(var->Flags, v->Flags, PROG_PARAM_BIT_INVARIANT)) {
146 char msg[100];
147 _mesa_snprintf(msg, sizeof(msg),
148 "invariant modifier mismatch for '%s'", var->Name);
149 link_error(shProg, msg);
150 return GL_FALSE;
151 }
152 }
153 else {
154 /* not already in linked list */
155 j = _mesa_add_varying(shProg->Varying, var->Name, var->Size,
156 var->Flags);
157 }
158
159 /* Map varying[i] to varying[j].
160 * Plus, set prog->Input/OutputFlags[] as described above.
161 * Note: the loop here takes care of arrays or large (sz>4) vars.
162 */
163 {
164 GLint sz = var->Size;
165 while (sz > 0) {
166 inOutFlags[firstVarying + j] = var->Flags;
167 /*printf("Link varying from %d to %d\n", i, j);*/
168 map[i++] = j++;
169 sz -= 4;
170 }
171 i--; /* go back one */
172 }
173 }
174
175
176 /* OK, now scan the program/shader instructions looking for varying vars,
177 * replacing the old index with the new index.
178 */
179 for (i = 0; i < prog->NumInstructions; i++) {
180 struct prog_instruction *inst = prog->Instructions + i;
181 GLuint j;
182
183 if (inst->DstReg.File == PROGRAM_VARYING) {
184 inst->DstReg.File = newFile;
185 inst->DstReg.Index = map[ inst->DstReg.Index ] + firstVarying;
186 }
187
188 for (j = 0; j < 3; j++) {
189 if (inst->SrcReg[j].File == PROGRAM_VARYING) {
190 inst->SrcReg[j].File = newFile;
191 inst->SrcReg[j].Index = map[ inst->SrcReg[j].Index ] + firstVarying;
192 }
193 }
194 }
195
196 free(map);
197
198 /* these will get recomputed before linking is completed */
199 prog->InputsRead = 0x0;
200 prog->OutputsWritten = 0x0;
201
202 return GL_TRUE;
203 }
204
205
206 /**
207 * Build the shProg->Uniforms list.
208 * This is basically a list/index of all uniforms found in either/both of
209 * the vertex and fragment shaders.
210 *
211 * About uniforms:
212 * Each uniform has two indexes, one that points into the vertex
213 * program's parameter array and another that points into the fragment
214 * program's parameter array. When the user changes a uniform's value
215 * we have to change the value in the vertex and/or fragment program's
216 * parameter array.
217 *
218 * This function will be called twice to set up the two uniform->parameter
219 * mappings.
220 *
221 * If a uniform is only present in the vertex program OR fragment program
222 * then the fragment/vertex parameter index, respectively, will be -1.
223 */
224 static GLboolean
225 link_uniform_vars(GLcontext *ctx,
226 struct gl_shader_program *shProg,
227 struct gl_program *prog,
228 GLuint *numSamplers)
229 {
230 GLuint samplerMap[200]; /* max number of samplers declared, not used */
231 GLuint i;
232
233 for (i = 0; i < prog->Parameters->NumParameters; i++) {
234 const struct gl_program_parameter *p = prog->Parameters->Parameters + i;
235
236 /*
237 * XXX FIX NEEDED HERE
238 * We should also be adding a uniform if p->Type == PROGRAM_STATE_VAR.
239 * For example, modelview matrix, light pos, etc.
240 * Also, we need to update the state-var name-generator code to
241 * generate GLSL-style names, like "gl_LightSource[0].position".
242 * Furthermore, we'll need to fix the state-var's size/datatype info.
243 */
244
245 if ((p->Type == PROGRAM_UNIFORM || p->Type == PROGRAM_SAMPLER)
246 && p->Used) {
247 /* add this uniform, indexing into the target's Parameters list */
248 struct gl_uniform *uniform =
249 _mesa_append_uniform(shProg->Uniforms, p->Name, prog->Target, i);
250 if (uniform)
251 uniform->Initialized = p->Initialized;
252 }
253
254 /* The samplerMap[] table we build here is used to remap/re-index
255 * sampler references by TEX instructions.
256 */
257 if (p->Type == PROGRAM_SAMPLER && p->Used) {
258 /* Allocate a new sampler index */
259 GLuint oldSampNum = (GLuint) prog->Parameters->ParameterValues[i][0];
260 GLuint newSampNum = *numSamplers;
261 if (newSampNum >= ctx->Const.MaxTextureImageUnits) {
262 char s[100];
263 _mesa_sprintf(s, "Too many texture samplers (%u, max is %u)",
264 newSampNum, ctx->Const.MaxTextureImageUnits);
265 link_error(shProg, s);
266 return GL_FALSE;
267 }
268 /* save old->new mapping in the table */
269 if (oldSampNum < Elements(samplerMap))
270 samplerMap[oldSampNum] = newSampNum;
271 /* update parameter's sampler index */
272 prog->Parameters->ParameterValues[i][0] = (GLfloat) newSampNum;
273 (*numSamplers)++;
274 }
275 }
276
277 /* OK, now scan the program/shader instructions looking for texture
278 * instructions using sampler vars. Replace old sampler indexes with
279 * new ones.
280 */
281 prog->SamplersUsed = 0x0;
282 for (i = 0; i < prog->NumInstructions; i++) {
283 struct prog_instruction *inst = prog->Instructions + i;
284 if (_mesa_is_tex_instruction(inst->Opcode)) {
285 const GLint oldSampNum = inst->TexSrcUnit;
286
287 #if 0
288 printf("====== remap sampler from %d to %d\n",
289 inst->TexSrcUnit, samplerMap[ inst->TexSrcUnit ]);
290 #endif
291
292 /* here, texUnit is really samplerUnit */
293 if (oldSampNum < Elements(samplerMap)) {
294 const GLuint newSampNum = samplerMap[oldSampNum];
295 inst->TexSrcUnit = newSampNum;
296 prog->SamplerTargets[newSampNum] = inst->TexSrcTarget;
297 prog->SamplersUsed |= (1 << newSampNum);
298 }
299 }
300 }
301
302 return GL_TRUE;
303 }
304
305
306 /**
307 * Resolve binding of generic vertex attributes.
308 * For example, if the vertex shader declared "attribute vec4 foobar" we'll
309 * allocate a generic vertex attribute for "foobar" and plug that value into
310 * the vertex program instructions.
311 * But if the user called glBindAttributeLocation(), those bindings will
312 * have priority.
313 */
314 static GLboolean
315 _slang_resolve_attributes(struct gl_shader_program *shProg,
316 const struct gl_program *origProg,
317 struct gl_program *linkedProg)
318 {
319 GLint attribMap[MAX_VERTEX_ATTRIBS];
320 GLuint i, j;
321 GLbitfield usedAttributes; /* generics only, not legacy attributes */
322
323 assert(origProg != linkedProg);
324 assert(origProg->Target == GL_VERTEX_PROGRAM_ARB);
325 assert(linkedProg->Target == GL_VERTEX_PROGRAM_ARB);
326
327 if (!shProg->Attributes)
328 shProg->Attributes = _mesa_new_parameter_list();
329
330 if (linkedProg->Attributes) {
331 _mesa_free_parameter_list(linkedProg->Attributes);
332 }
333 linkedProg->Attributes = _mesa_new_parameter_list();
334
335
336 /* Build a bitmask indicating which attribute indexes have been
337 * explicitly bound by the user with glBindAttributeLocation().
338 */
339 usedAttributes = 0x0;
340 for (i = 0; i < shProg->Attributes->NumParameters; i++) {
341 GLint attr = shProg->Attributes->Parameters[i].StateIndexes[0];
342 usedAttributes |= (1 << attr);
343 }
344
345 /* If gl_Vertex is used, that actually counts against the limit
346 * on generic vertex attributes. This avoids the ambiguity of
347 * whether glVertexAttrib4fv(0, v) sets legacy attribute 0 (vert pos)
348 * or generic attribute[0]. If gl_Vertex is used, we want the former.
349 */
350 if (origProg->InputsRead & VERT_BIT_POS) {
351 usedAttributes |= 0x1;
352 }
353
354 /* initialize the generic attribute map entries to -1 */
355 for (i = 0; i < MAX_VERTEX_ATTRIBS; i++) {
356 attribMap[i] = -1;
357 }
358
359 /*
360 * Scan program for generic attribute references
361 */
362 for (i = 0; i < linkedProg->NumInstructions; i++) {
363 struct prog_instruction *inst = linkedProg->Instructions + i;
364 for (j = 0; j < 3; j++) {
365 if (inst->SrcReg[j].File == PROGRAM_INPUT &&
366 inst->SrcReg[j].Index >= VERT_ATTRIB_GENERIC0) {
367 /*
368 * OK, we've found a generic vertex attribute reference.
369 */
370 const GLint k = inst->SrcReg[j].Index - VERT_ATTRIB_GENERIC0;
371
372 GLint attr = attribMap[k];
373
374 if (attr < 0) {
375 /* Need to figure out attribute mapping now.
376 */
377 const char *name = origProg->Attributes->Parameters[k].Name;
378 const GLint size = origProg->Attributes->Parameters[k].Size;
379 const GLenum type =origProg->Attributes->Parameters[k].DataType;
380 GLint index;
381
382 /* See if there's a user-defined attribute binding for
383 * this name.
384 */
385 index = _mesa_lookup_parameter_index(shProg->Attributes,
386 -1, name);
387 if (index >= 0) {
388 /* Found a user-defined binding */
389 attr = shProg->Attributes->Parameters[index].StateIndexes[0];
390 }
391 else {
392 /* No user-defined binding, choose our own attribute number.
393 * Start at 1 since generic attribute 0 always aliases
394 * glVertex/position.
395 */
396 for (attr = 0; attr < MAX_VERTEX_ATTRIBS; attr++) {
397 if (((1 << attr) & usedAttributes) == 0)
398 break;
399 }
400 if (attr == MAX_VERTEX_ATTRIBS) {
401 link_error(shProg, "Too many vertex attributes");
402 return GL_FALSE;
403 }
404
405 /* mark this attribute as used */
406 usedAttributes |= (1 << attr);
407 }
408
409 attribMap[k] = attr;
410
411 /* Save the final name->attrib binding so it can be queried
412 * with glGetAttributeLocation().
413 */
414 _mesa_add_attribute(linkedProg->Attributes, name,
415 size, type, attr);
416 }
417
418 assert(attr >= 0);
419
420 /* update the instruction's src reg */
421 inst->SrcReg[j].Index = VERT_ATTRIB_GENERIC0 + attr;
422 }
423 }
424 }
425
426 return GL_TRUE;
427 }
428
429
430 /**
431 * Scan program instructions to update the program's NumTemporaries field.
432 * Note: this implemenation relies on the code generator allocating
433 * temps in increasing order (0, 1, 2, ... ).
434 */
435 static void
436 _slang_count_temporaries(struct gl_program *prog)
437 {
438 GLuint i, j;
439 GLint maxIndex = -1;
440
441 for (i = 0; i < prog->NumInstructions; i++) {
442 const struct prog_instruction *inst = prog->Instructions + i;
443 const GLuint numSrc = _mesa_num_inst_src_regs(inst->Opcode);
444 for (j = 0; j < numSrc; j++) {
445 if (inst->SrcReg[j].File == PROGRAM_TEMPORARY) {
446 if (maxIndex < inst->SrcReg[j].Index)
447 maxIndex = inst->SrcReg[j].Index;
448 }
449 if (inst->DstReg.File == PROGRAM_TEMPORARY) {
450 if (maxIndex < (GLint) inst->DstReg.Index)
451 maxIndex = inst->DstReg.Index;
452 }
453 }
454 }
455
456 prog->NumTemporaries = (GLuint) (maxIndex + 1);
457 }
458
459
460 /**
461 * Scan program instructions to update the program's InputsRead and
462 * OutputsWritten fields.
463 */
464 static void
465 _slang_update_inputs_outputs(struct gl_program *prog)
466 {
467 GLuint i, j;
468 GLuint maxAddrReg = 0;
469
470 prog->InputsRead = 0x0;
471 prog->OutputsWritten = 0x0;
472
473 for (i = 0; i < prog->NumInstructions; i++) {
474 const struct prog_instruction *inst = prog->Instructions + i;
475 const GLuint numSrc = _mesa_num_inst_src_regs(inst->Opcode);
476 for (j = 0; j < numSrc; j++) {
477 if (inst->SrcReg[j].File == PROGRAM_INPUT) {
478 prog->InputsRead |= 1 << inst->SrcReg[j].Index;
479 if (prog->Target == GL_FRAGMENT_PROGRAM_ARB &&
480 inst->SrcReg[j].Index == FRAG_ATTRIB_FOGC) {
481 /* The fragment shader FOGC input is used for fog,
482 * front-facing and sprite/point coord.
483 */
484 struct gl_fragment_program *fp = fragment_program(prog);
485 const GLint swz = GET_SWZ(inst->SrcReg[j].Swizzle, 0);
486 if (swz == SWIZZLE_X)
487 fp->UsesFogFragCoord = GL_TRUE;
488 else if (swz == SWIZZLE_Y)
489 fp->UsesFrontFacing = GL_TRUE;
490 else if (swz == SWIZZLE_Z || swz == SWIZZLE_W)
491 fp->UsesPointCoord = GL_TRUE;
492 }
493 }
494 else if (inst->SrcReg[j].File == PROGRAM_ADDRESS) {
495 maxAddrReg = MAX2(maxAddrReg, (GLuint) (inst->SrcReg[j].Index + 1));
496 }
497 }
498 if (inst->DstReg.File == PROGRAM_OUTPUT) {
499 prog->OutputsWritten |= 1 << inst->DstReg.Index;
500 }
501 else if (inst->DstReg.File == PROGRAM_ADDRESS) {
502 maxAddrReg = MAX2(maxAddrReg, inst->DstReg.Index + 1);
503 }
504 }
505
506 prog->NumAddressRegs = maxAddrReg;
507 }
508
509
510 /**
511 * Shader linker. Currently:
512 *
513 * 1. The last attached vertex shader and fragment shader are linked.
514 * 2. Varying vars in the two shaders are combined so their locations
515 * agree between the vertex and fragment stages. They're treated as
516 * vertex program output attribs and as fragment program input attribs.
517 * 3. The vertex and fragment programs are cloned and modified to update
518 * src/dst register references so they use the new, linked varying
519 * storage locations.
520 */
521 void
522 _slang_link(GLcontext *ctx,
523 GLhandleARB programObj,
524 struct gl_shader_program *shProg)
525 {
526 const struct gl_vertex_program *vertProg;
527 const struct gl_fragment_program *fragProg;
528 GLuint numSamplers = 0;
529 GLuint i;
530
531 _mesa_clear_shader_program_data(ctx, shProg);
532
533 /* check that all programs compiled successfully */
534 for (i = 0; i < shProg->NumShaders; i++) {
535 if (!shProg->Shaders[i]->CompileStatus) {
536 link_error(shProg, "linking with uncompiled shader\n");
537 return;
538 }
539 }
540
541 shProg->Uniforms = _mesa_new_uniform_list();
542 shProg->Varying = _mesa_new_parameter_list();
543
544 /**
545 * Find attached vertex, fragment shaders defining main()
546 */
547 vertProg = NULL;
548 fragProg = NULL;
549 for (i = 0; i < shProg->NumShaders; i++) {
550 struct gl_shader *shader = shProg->Shaders[i];
551 if (shader->Type == GL_VERTEX_SHADER) {
552 if (shader->Main)
553 vertProg = vertex_program(shader->Program);
554 }
555 else if (shader->Type == GL_FRAGMENT_SHADER) {
556 if (shader->Main)
557 fragProg = fragment_program(shader->Program);
558 }
559 else {
560 _mesa_problem(ctx, "unexpected shader target in slang_link()");
561 }
562 }
563
564 #if FEATURE_es2_glsl
565 /* must have both a vertex and fragment program for ES2 */
566 if (!vertProg) {
567 link_error(shProg, "missing vertex shader\n");
568 return;
569 }
570 if (!fragProg) {
571 link_error(shProg, "missing fragment shader\n");
572 return;
573 }
574 #endif
575
576 /*
577 * Make copies of the vertex/fragment programs now since we'll be
578 * changing src/dst registers after merging the uniforms and varying vars.
579 */
580 _mesa_reference_vertprog(ctx, &shProg->VertexProgram, NULL);
581 if (vertProg) {
582 struct gl_vertex_program *linked_vprog =
583 vertex_program(_mesa_clone_program(ctx, &vertProg->Base));
584 shProg->VertexProgram = linked_vprog; /* refcount OK */
585 ASSERT(shProg->VertexProgram->Base.RefCount == 1);
586 }
587
588 _mesa_reference_fragprog(ctx, &shProg->FragmentProgram, NULL);
589 if (fragProg) {
590 struct gl_fragment_program *linked_fprog =
591 fragment_program(_mesa_clone_program(ctx, &fragProg->Base));
592 shProg->FragmentProgram = linked_fprog; /* refcount OK */
593 ASSERT(shProg->FragmentProgram->Base.RefCount == 1);
594 }
595
596 /* link varying vars */
597 if (shProg->VertexProgram) {
598 if (!link_varying_vars(shProg, &shProg->VertexProgram->Base))
599 return;
600 }
601 if (shProg->FragmentProgram) {
602 if (!link_varying_vars(shProg, &shProg->FragmentProgram->Base))
603 return;
604 }
605
606 /* link uniform vars */
607 if (shProg->VertexProgram) {
608 if (!link_uniform_vars(ctx, shProg, &shProg->VertexProgram->Base,
609 &numSamplers)) {
610 return;
611 }
612 }
613 if (shProg->FragmentProgram) {
614 if (!link_uniform_vars(ctx, shProg, &shProg->FragmentProgram->Base,
615 &numSamplers)) {
616 return;
617 }
618 }
619
620 /*_mesa_print_uniforms(shProg->Uniforms);*/
621
622 if (shProg->VertexProgram) {
623 if (!_slang_resolve_attributes(shProg, &vertProg->Base,
624 &shProg->VertexProgram->Base)) {
625 return;
626 }
627 }
628
629 if (shProg->VertexProgram) {
630 _slang_update_inputs_outputs(&shProg->VertexProgram->Base);
631 _slang_count_temporaries(&shProg->VertexProgram->Base);
632 if (!(shProg->VertexProgram->Base.OutputsWritten & (1 << VERT_RESULT_HPOS))) {
633 /* the vertex program did not compute a vertex position */
634 link_error(shProg,
635 "gl_Position was not written by vertex shader\n");
636 return;
637 }
638 }
639 if (shProg->FragmentProgram) {
640 _slang_count_temporaries(&shProg->FragmentProgram->Base);
641 _slang_update_inputs_outputs(&shProg->FragmentProgram->Base);
642 }
643
644 /* Check that all the varying vars needed by the fragment shader are
645 * actually produced by the vertex shader.
646 */
647 if (shProg->FragmentProgram) {
648 const GLbitfield varyingRead
649 = shProg->FragmentProgram->Base.InputsRead >> FRAG_ATTRIB_VAR0;
650 const GLbitfield varyingWritten = shProg->VertexProgram ?
651 shProg->VertexProgram->Base.OutputsWritten >> VERT_RESULT_VAR0 : 0x0;
652 if ((varyingRead & varyingWritten) != varyingRead) {
653 link_error(shProg,
654 "Fragment program using varying vars not written by vertex shader\n");
655 return;
656 }
657 }
658
659 /* check that gl_FragColor and gl_FragData are not both written to */
660 if (shProg->FragmentProgram) {
661 GLbitfield outputsWritten = shProg->FragmentProgram->Base.OutputsWritten;
662 if ((outputsWritten & ((1 << FRAG_RESULT_COLR))) &&
663 (outputsWritten >= (1 << FRAG_RESULT_DATA0))) {
664 link_error(shProg, "Fragment program cannot write both gl_FragColor"
665 " and gl_FragData[].\n");
666 return;
667 }
668 }
669
670
671 if (fragProg && shProg->FragmentProgram) {
672 /* Compute initial program's TexturesUsed info */
673 _mesa_update_shader_textures_used(&shProg->FragmentProgram->Base);
674
675 /* notify driver that a new fragment program has been compiled/linked */
676 ctx->Driver.ProgramStringNotify(ctx, GL_FRAGMENT_PROGRAM_ARB,
677 &shProg->FragmentProgram->Base);
678 if (ctx->Shader.Flags & GLSL_DUMP) {
679 _mesa_printf("Mesa pre-link fragment program:\n");
680 _mesa_print_program(&fragProg->Base);
681 _mesa_print_program_parameters(ctx, &fragProg->Base);
682
683 _mesa_printf("Mesa post-link fragment program:\n");
684 _mesa_print_program(&shProg->FragmentProgram->Base);
685 _mesa_print_program_parameters(ctx, &shProg->FragmentProgram->Base);
686 }
687 }
688
689 if (vertProg && shProg->VertexProgram) {
690 /* Compute initial program's TexturesUsed info */
691 _mesa_update_shader_textures_used(&shProg->VertexProgram->Base);
692
693 /* notify driver that a new vertex program has been compiled/linked */
694 ctx->Driver.ProgramStringNotify(ctx, GL_VERTEX_PROGRAM_ARB,
695 &shProg->VertexProgram->Base);
696 if (ctx->Shader.Flags & GLSL_DUMP) {
697 _mesa_printf("Mesa pre-link vertex program:\n");
698 _mesa_print_program(&vertProg->Base);
699 _mesa_print_program_parameters(ctx, &vertProg->Base);
700
701 _mesa_printf("Mesa post-link vertex program:\n");
702 _mesa_print_program(&shProg->VertexProgram->Base);
703 _mesa_print_program_parameters(ctx, &shProg->VertexProgram->Base);
704 }
705 }
706
707 if (ctx->Shader.Flags & GLSL_DUMP) {
708 _mesa_printf("Varying vars:\n");
709 _mesa_print_parameter_list(shProg->Varying);
710 if (shProg->InfoLog) {
711 _mesa_printf("Info Log: %s\n", shProg->InfoLog);
712 }
713 }
714
715 shProg->LinkStatus = (shProg->VertexProgram || shProg->FragmentProgram);
716 }
717