mesa: rename, reorder FRAG_RESULT_x tokens
[mesa.git] / src / mesa / shader / program.c
1 /*
2 * Mesa 3-D graphics library
3 * Version: 6.5.3
4 *
5 * Copyright (C) 1999-2007 Brian Paul All Rights Reserved.
6 *
7 * Permission is hereby granted, free of charge, to any person obtaining a
8 * copy of this software and associated documentation files (the "Software"),
9 * to deal in the Software without restriction, including without limitation
10 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
11 * and/or sell copies of the Software, and to permit persons to whom the
12 * Software is furnished to do so, subject to the following conditions:
13 *
14 * The above copyright notice and this permission notice shall be included
15 * in all copies or substantial portions of the Software.
16 *
17 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
18 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
20 * BRIAN PAUL BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
21 * AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
22 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
23 */
24
25 /**
26 * \file program.c
27 * Vertex and fragment program support functions.
28 * \author Brian Paul
29 */
30
31
32 #include "main/glheader.h"
33 #include "main/context.h"
34 #include "main/hash.h"
35 #include "program.h"
36 #include "prog_cache.h"
37 #include "prog_parameter.h"
38 #include "prog_instruction.h"
39
40
41 /**
42 * A pointer to this dummy program is put into the hash table when
43 * glGenPrograms is called.
44 */
45 struct gl_program _mesa_DummyProgram;
46
47
48 /**
49 * Init context's vertex/fragment program state
50 */
51 void
52 _mesa_init_program(GLcontext *ctx)
53 {
54 GLuint i;
55
56 /*
57 * If this assertion fails, we need to increase the field
58 * size for register indexes.
59 */
60 ASSERT(ctx->Const.VertexProgram.MaxUniformComponents / 4
61 <= (1 << INST_INDEX_BITS));
62 ASSERT(ctx->Const.FragmentProgram.MaxUniformComponents / 4
63 <= (1 << INST_INDEX_BITS));
64
65 ctx->Program.ErrorPos = -1;
66 ctx->Program.ErrorString = _mesa_strdup("");
67
68 #if FEATURE_NV_vertex_program || FEATURE_ARB_vertex_program
69 ctx->VertexProgram.Enabled = GL_FALSE;
70 #if FEATURE_es2_glsl
71 ctx->VertexProgram.PointSizeEnabled = GL_TRUE;
72 #else
73 ctx->VertexProgram.PointSizeEnabled = GL_FALSE;
74 #endif
75 ctx->VertexProgram.TwoSideEnabled = GL_FALSE;
76 _mesa_reference_vertprog(ctx, &ctx->VertexProgram.Current,
77 ctx->Shared->DefaultVertexProgram);
78 assert(ctx->VertexProgram.Current);
79 for (i = 0; i < MAX_NV_VERTEX_PROGRAM_PARAMS / 4; i++) {
80 ctx->VertexProgram.TrackMatrix[i] = GL_NONE;
81 ctx->VertexProgram.TrackMatrixTransform[i] = GL_IDENTITY_NV;
82 }
83 ctx->VertexProgram.Cache = _mesa_new_program_cache();
84 #endif
85
86 #if FEATURE_NV_fragment_program || FEATURE_ARB_fragment_program
87 ctx->FragmentProgram.Enabled = GL_FALSE;
88 _mesa_reference_fragprog(ctx, &ctx->FragmentProgram.Current,
89 ctx->Shared->DefaultFragmentProgram);
90 assert(ctx->FragmentProgram.Current);
91 ctx->FragmentProgram.Cache = _mesa_new_program_cache();
92 #endif
93
94
95 /* XXX probably move this stuff */
96 #if FEATURE_ATI_fragment_shader
97 ctx->ATIFragmentShader.Enabled = GL_FALSE;
98 ctx->ATIFragmentShader.Current = ctx->Shared->DefaultFragmentShader;
99 assert(ctx->ATIFragmentShader.Current);
100 ctx->ATIFragmentShader.Current->RefCount++;
101 #endif
102 }
103
104
105 /**
106 * Free a context's vertex/fragment program state
107 */
108 void
109 _mesa_free_program_data(GLcontext *ctx)
110 {
111 #if FEATURE_NV_vertex_program || FEATURE_ARB_vertex_program
112 _mesa_reference_vertprog(ctx, &ctx->VertexProgram.Current, NULL);
113 _mesa_delete_program_cache(ctx, ctx->VertexProgram.Cache);
114 #endif
115 #if FEATURE_NV_fragment_program || FEATURE_ARB_fragment_program
116 _mesa_reference_fragprog(ctx, &ctx->FragmentProgram.Current, NULL);
117 _mesa_delete_program_cache(ctx, ctx->FragmentProgram.Cache);
118 #endif
119 /* XXX probably move this stuff */
120 #if FEATURE_ATI_fragment_shader
121 if (ctx->ATIFragmentShader.Current) {
122 ctx->ATIFragmentShader.Current->RefCount--;
123 if (ctx->ATIFragmentShader.Current->RefCount <= 0) {
124 _mesa_free(ctx->ATIFragmentShader.Current);
125 }
126 }
127 #endif
128 _mesa_free((void *) ctx->Program.ErrorString);
129 }
130
131
132 /**
133 * Update the default program objects in the given context to reference those
134 * specified in the shared state and release those referencing the old
135 * shared state.
136 */
137 void
138 _mesa_update_default_objects_program(GLcontext *ctx)
139 {
140 #if FEATURE_NV_vertex_program || FEATURE_ARB_vertex_program
141 _mesa_reference_vertprog(ctx, &ctx->VertexProgram.Current,
142 (struct gl_vertex_program *)
143 ctx->Shared->DefaultVertexProgram);
144 assert(ctx->VertexProgram.Current);
145 #endif
146
147 #if FEATURE_NV_fragment_program || FEATURE_ARB_fragment_program
148 _mesa_reference_fragprog(ctx, &ctx->FragmentProgram.Current,
149 (struct gl_fragment_program *)
150 ctx->Shared->DefaultFragmentProgram);
151 assert(ctx->FragmentProgram.Current);
152 #endif
153
154 /* XXX probably move this stuff */
155 #if FEATURE_ATI_fragment_shader
156 if (ctx->ATIFragmentShader.Current) {
157 ctx->ATIFragmentShader.Current->RefCount--;
158 if (ctx->ATIFragmentShader.Current->RefCount <= 0) {
159 _mesa_free(ctx->ATIFragmentShader.Current);
160 }
161 }
162 ctx->ATIFragmentShader.Current = (struct ati_fragment_shader *) ctx->Shared->DefaultFragmentShader;
163 assert(ctx->ATIFragmentShader.Current);
164 ctx->ATIFragmentShader.Current->RefCount++;
165 #endif
166 }
167
168
169 /**
170 * Set the vertex/fragment program error state (position and error string).
171 * This is generally called from within the parsers.
172 */
173 void
174 _mesa_set_program_error(GLcontext *ctx, GLint pos, const char *string)
175 {
176 ctx->Program.ErrorPos = pos;
177 _mesa_free((void *) ctx->Program.ErrorString);
178 if (!string)
179 string = "";
180 ctx->Program.ErrorString = _mesa_strdup(string);
181 }
182
183
184 /**
185 * Find the line number and column for 'pos' within 'string'.
186 * Return a copy of the line which contains 'pos'. Free the line with
187 * _mesa_free().
188 * \param string the program string
189 * \param pos the position within the string
190 * \param line returns the line number corresponding to 'pos'.
191 * \param col returns the column number corresponding to 'pos'.
192 * \return copy of the line containing 'pos'.
193 */
194 const GLubyte *
195 _mesa_find_line_column(const GLubyte *string, const GLubyte *pos,
196 GLint *line, GLint *col)
197 {
198 const GLubyte *lineStart = string;
199 const GLubyte *p = string;
200 GLubyte *s;
201 int len;
202
203 *line = 1;
204
205 while (p != pos) {
206 if (*p == (GLubyte) '\n') {
207 (*line)++;
208 lineStart = p + 1;
209 }
210 p++;
211 }
212
213 *col = (pos - lineStart) + 1;
214
215 /* return copy of this line */
216 while (*p != 0 && *p != '\n')
217 p++;
218 len = p - lineStart;
219 s = (GLubyte *) _mesa_malloc(len + 1);
220 _mesa_memcpy(s, lineStart, len);
221 s[len] = 0;
222
223 return s;
224 }
225
226
227 /**
228 * Initialize a new vertex/fragment program object.
229 */
230 static struct gl_program *
231 _mesa_init_program_struct( GLcontext *ctx, struct gl_program *prog,
232 GLenum target, GLuint id)
233 {
234 (void) ctx;
235 if (prog) {
236 GLuint i;
237 _mesa_bzero(prog, sizeof(*prog));
238 prog->Id = id;
239 prog->Target = target;
240 prog->Resident = GL_TRUE;
241 prog->RefCount = 1;
242 prog->Format = GL_PROGRAM_FORMAT_ASCII_ARB;
243
244 /* default mapping from samplers to texture units */
245 for (i = 0; i < MAX_SAMPLERS; i++)
246 prog->SamplerUnits[i] = i;
247 }
248
249 return prog;
250 }
251
252
253 /**
254 * Initialize a new fragment program object.
255 */
256 struct gl_program *
257 _mesa_init_fragment_program( GLcontext *ctx, struct gl_fragment_program *prog,
258 GLenum target, GLuint id)
259 {
260 if (prog)
261 return _mesa_init_program_struct( ctx, &prog->Base, target, id );
262 else
263 return NULL;
264 }
265
266
267 /**
268 * Initialize a new vertex program object.
269 */
270 struct gl_program *
271 _mesa_init_vertex_program( GLcontext *ctx, struct gl_vertex_program *prog,
272 GLenum target, GLuint id)
273 {
274 if (prog)
275 return _mesa_init_program_struct( ctx, &prog->Base, target, id );
276 else
277 return NULL;
278 }
279
280
281 /**
282 * Allocate and initialize a new fragment/vertex program object but
283 * don't put it into the program hash table. Called via
284 * ctx->Driver.NewProgram. May be overridden (ie. replaced) by a
285 * device driver function to implement OO deriviation with additional
286 * types not understood by this function.
287 *
288 * \param ctx context
289 * \param id program id/number
290 * \param target program target/type
291 * \return pointer to new program object
292 */
293 struct gl_program *
294 _mesa_new_program(GLcontext *ctx, GLenum target, GLuint id)
295 {
296 struct gl_program *prog;
297 switch (target) {
298 case GL_VERTEX_PROGRAM_ARB: /* == GL_VERTEX_PROGRAM_NV */
299 prog = _mesa_init_vertex_program(ctx, CALLOC_STRUCT(gl_vertex_program),
300 target, id );
301 break;
302 case GL_FRAGMENT_PROGRAM_NV:
303 case GL_FRAGMENT_PROGRAM_ARB:
304 prog =_mesa_init_fragment_program(ctx,
305 CALLOC_STRUCT(gl_fragment_program),
306 target, id );
307 break;
308 default:
309 _mesa_problem(ctx, "bad target in _mesa_new_program");
310 prog = NULL;
311 }
312 return prog;
313 }
314
315
316 /**
317 * Delete a program and remove it from the hash table, ignoring the
318 * reference count.
319 * Called via ctx->Driver.DeleteProgram. May be wrapped (OO deriviation)
320 * by a device driver function.
321 */
322 void
323 _mesa_delete_program(GLcontext *ctx, struct gl_program *prog)
324 {
325 (void) ctx;
326 ASSERT(prog);
327 ASSERT(prog->RefCount==0);
328
329 if (prog == &_mesa_DummyProgram)
330 return;
331
332 if (prog->String)
333 _mesa_free(prog->String);
334
335 _mesa_free_instructions(prog->Instructions, prog->NumInstructions);
336
337 if (prog->Parameters) {
338 _mesa_free_parameter_list(prog->Parameters);
339 }
340 if (prog->Varying) {
341 _mesa_free_parameter_list(prog->Varying);
342 }
343 if (prog->Attributes) {
344 _mesa_free_parameter_list(prog->Attributes);
345 }
346
347 /* XXX this is a little ugly */
348 if (prog->Target == GL_VERTEX_PROGRAM_ARB) {
349 struct gl_vertex_program *vprog = (struct gl_vertex_program *) prog;
350 if (vprog->TnlData)
351 _mesa_free(vprog->TnlData);
352 }
353
354 _mesa_free(prog);
355 }
356
357
358 /**
359 * Return the gl_program object for a given ID.
360 * Basically just a wrapper for _mesa_HashLookup() to avoid a lot of
361 * casts elsewhere.
362 */
363 struct gl_program *
364 _mesa_lookup_program(GLcontext *ctx, GLuint id)
365 {
366 if (id)
367 return (struct gl_program *) _mesa_HashLookup(ctx->Shared->Programs, id);
368 else
369 return NULL;
370 }
371
372
373 /**
374 * Reference counting for vertex/fragment programs
375 */
376 void
377 _mesa_reference_program(GLcontext *ctx,
378 struct gl_program **ptr,
379 struct gl_program *prog)
380 {
381 assert(ptr);
382 if (*ptr && prog) {
383 /* sanity check */
384 if ((*ptr)->Target == GL_VERTEX_PROGRAM_ARB)
385 ASSERT(prog->Target == GL_VERTEX_PROGRAM_ARB);
386 else if ((*ptr)->Target == GL_FRAGMENT_PROGRAM_ARB)
387 ASSERT(prog->Target == GL_FRAGMENT_PROGRAM_ARB ||
388 prog->Target == GL_FRAGMENT_PROGRAM_NV);
389 }
390 if (*ptr == prog) {
391 return; /* no change */
392 }
393 if (*ptr) {
394 GLboolean deleteFlag;
395
396 /*_glthread_LOCK_MUTEX((*ptr)->Mutex);*/
397 #if 0
398 printf("Program %p ID=%u Target=%s Refcount-- to %d\n",
399 *ptr, (*ptr)->Id,
400 ((*ptr)->Target == GL_VERTEX_PROGRAM_ARB ? "VP" : "FP"),
401 (*ptr)->RefCount - 1);
402 #endif
403 ASSERT((*ptr)->RefCount > 0);
404 (*ptr)->RefCount--;
405
406 deleteFlag = ((*ptr)->RefCount == 0);
407 /*_glthread_UNLOCK_MUTEX((*ptr)->Mutex);*/
408
409 if (deleteFlag) {
410 ASSERT(ctx);
411 ctx->Driver.DeleteProgram(ctx, *ptr);
412 }
413
414 *ptr = NULL;
415 }
416
417 assert(!*ptr);
418 if (prog) {
419 /*_glthread_LOCK_MUTEX(prog->Mutex);*/
420 prog->RefCount++;
421 #if 0
422 printf("Program %p ID=%u Target=%s Refcount++ to %d\n",
423 prog, prog->Id,
424 (prog->Target == GL_VERTEX_PROGRAM_ARB ? "VP" : "FP"),
425 prog->RefCount);
426 #endif
427 /*_glthread_UNLOCK_MUTEX(prog->Mutex);*/
428 }
429
430 *ptr = prog;
431 }
432
433
434 /**
435 * Return a copy of a program.
436 * XXX Problem here if the program object is actually OO-derivation
437 * made by a device driver.
438 */
439 struct gl_program *
440 _mesa_clone_program(GLcontext *ctx, const struct gl_program *prog)
441 {
442 struct gl_program *clone;
443
444 clone = ctx->Driver.NewProgram(ctx, prog->Target, prog->Id);
445 if (!clone)
446 return NULL;
447
448 assert(clone->Target == prog->Target);
449 assert(clone->RefCount == 1);
450
451 clone->String = (GLubyte *) _mesa_strdup((char *) prog->String);
452 clone->Format = prog->Format;
453 clone->Instructions = _mesa_alloc_instructions(prog->NumInstructions);
454 if (!clone->Instructions) {
455 _mesa_reference_program(ctx, &clone, NULL);
456 return NULL;
457 }
458 _mesa_copy_instructions(clone->Instructions, prog->Instructions,
459 prog->NumInstructions);
460 clone->InputsRead = prog->InputsRead;
461 clone->OutputsWritten = prog->OutputsWritten;
462 clone->SamplersUsed = prog->SamplersUsed;
463 clone->ShadowSamplers = prog->ShadowSamplers;
464 memcpy(clone->TexturesUsed, prog->TexturesUsed, sizeof(prog->TexturesUsed));
465
466 if (prog->Parameters)
467 clone->Parameters = _mesa_clone_parameter_list(prog->Parameters);
468 memcpy(clone->LocalParams, prog->LocalParams, sizeof(clone->LocalParams));
469 if (prog->Varying)
470 clone->Varying = _mesa_clone_parameter_list(prog->Varying);
471 if (prog->Attributes)
472 clone->Attributes = _mesa_clone_parameter_list(prog->Attributes);
473 memcpy(clone->LocalParams, prog->LocalParams, sizeof(clone->LocalParams));
474 clone->NumInstructions = prog->NumInstructions;
475 clone->NumTemporaries = prog->NumTemporaries;
476 clone->NumParameters = prog->NumParameters;
477 clone->NumAttributes = prog->NumAttributes;
478 clone->NumAddressRegs = prog->NumAddressRegs;
479 clone->NumNativeInstructions = prog->NumNativeInstructions;
480 clone->NumNativeTemporaries = prog->NumNativeTemporaries;
481 clone->NumNativeParameters = prog->NumNativeParameters;
482 clone->NumNativeAttributes = prog->NumNativeAttributes;
483 clone->NumNativeAddressRegs = prog->NumNativeAddressRegs;
484 clone->NumAluInstructions = prog->NumAluInstructions;
485 clone->NumTexInstructions = prog->NumTexInstructions;
486 clone->NumTexIndirections = prog->NumTexIndirections;
487 clone->NumNativeAluInstructions = prog->NumNativeAluInstructions;
488 clone->NumNativeTexInstructions = prog->NumNativeTexInstructions;
489 clone->NumNativeTexIndirections = prog->NumNativeTexIndirections;
490
491 switch (prog->Target) {
492 case GL_VERTEX_PROGRAM_ARB:
493 {
494 const struct gl_vertex_program *vp
495 = (const struct gl_vertex_program *) prog;
496 struct gl_vertex_program *vpc = (struct gl_vertex_program *) clone;
497 vpc->IsPositionInvariant = vp->IsPositionInvariant;
498 }
499 break;
500 case GL_FRAGMENT_PROGRAM_ARB:
501 {
502 const struct gl_fragment_program *fp
503 = (const struct gl_fragment_program *) prog;
504 struct gl_fragment_program *fpc = (struct gl_fragment_program *) clone;
505 fpc->FogOption = fp->FogOption;
506 fpc->UsesKill = fp->UsesKill;
507 }
508 break;
509 default:
510 _mesa_problem(NULL, "Unexpected target in _mesa_clone_program");
511 }
512
513 return clone;
514 }
515
516
517 /**
518 * Insert 'count' NOP instructions at 'start' in the given program.
519 * Adjust branch targets accordingly.
520 */
521 GLboolean
522 _mesa_insert_instructions(struct gl_program *prog, GLuint start, GLuint count)
523 {
524 const GLuint origLen = prog->NumInstructions;
525 const GLuint newLen = origLen + count;
526 struct prog_instruction *newInst;
527 GLuint i;
528
529 /* adjust branches */
530 for (i = 0; i < prog->NumInstructions; i++) {
531 struct prog_instruction *inst = prog->Instructions + i;
532 if (inst->BranchTarget > 0) {
533 if ((GLuint)inst->BranchTarget >= start) {
534 inst->BranchTarget += count;
535 }
536 }
537 }
538
539 /* Alloc storage for new instructions */
540 newInst = _mesa_alloc_instructions(newLen);
541 if (!newInst) {
542 return GL_FALSE;
543 }
544
545 /* Copy 'start' instructions into new instruction buffer */
546 _mesa_copy_instructions(newInst, prog->Instructions, start);
547
548 /* init the new instructions */
549 _mesa_init_instructions(newInst + start, count);
550
551 /* Copy the remaining/tail instructions to new inst buffer */
552 _mesa_copy_instructions(newInst + start + count,
553 prog->Instructions + start,
554 origLen - start);
555
556 /* free old instructions */
557 _mesa_free_instructions(prog->Instructions, origLen);
558
559 /* install new instructions */
560 prog->Instructions = newInst;
561 prog->NumInstructions = newLen;
562
563 return GL_TRUE;
564 }
565
566 /**
567 * Delete 'count' instructions at 'start' in the given program.
568 * Adjust branch targets accordingly.
569 */
570 GLboolean
571 _mesa_delete_instructions(struct gl_program *prog, GLuint start, GLuint count)
572 {
573 const GLuint origLen = prog->NumInstructions;
574 const GLuint newLen = origLen - count;
575 struct prog_instruction *newInst;
576 GLuint i;
577
578 /* adjust branches */
579 for (i = 0; i < prog->NumInstructions; i++) {
580 struct prog_instruction *inst = prog->Instructions + i;
581 if (inst->BranchTarget > 0) {
582 if (inst->BranchTarget > start) {
583 inst->BranchTarget -= count;
584 }
585 }
586 }
587
588 /* Alloc storage for new instructions */
589 newInst = _mesa_alloc_instructions(newLen);
590 if (!newInst) {
591 return GL_FALSE;
592 }
593
594 /* Copy 'start' instructions into new instruction buffer */
595 _mesa_copy_instructions(newInst, prog->Instructions, start);
596
597 /* Copy the remaining/tail instructions to new inst buffer */
598 _mesa_copy_instructions(newInst + start,
599 prog->Instructions + start + count,
600 newLen - start);
601
602 /* free old instructions */
603 _mesa_free_instructions(prog->Instructions, origLen);
604
605 /* install new instructions */
606 prog->Instructions = newInst;
607 prog->NumInstructions = newLen;
608
609 return GL_TRUE;
610 }
611
612
613 /**
614 * Search instructions for registers that match (oldFile, oldIndex),
615 * replacing them with (newFile, newIndex).
616 */
617 static void
618 replace_registers(struct prog_instruction *inst, GLuint numInst,
619 GLuint oldFile, GLuint oldIndex,
620 GLuint newFile, GLuint newIndex)
621 {
622 GLuint i, j;
623 for (i = 0; i < numInst; i++) {
624 /* src regs */
625 for (j = 0; j < _mesa_num_inst_src_regs(inst->Opcode); j++) {
626 if (inst[i].SrcReg[j].File == oldFile &&
627 inst[i].SrcReg[j].Index == oldIndex) {
628 inst[i].SrcReg[j].File = newFile;
629 inst[i].SrcReg[j].Index = newIndex;
630 }
631 }
632 /* dst reg */
633 if (inst[i].DstReg.File == oldFile && inst[i].DstReg.Index == oldIndex) {
634 inst[i].DstReg.File = newFile;
635 inst[i].DstReg.Index = newIndex;
636 }
637 }
638 }
639
640
641 /**
642 * Search instructions for references to program parameters. When found,
643 * increment the parameter index by 'offset'.
644 * Used when combining programs.
645 */
646 static void
647 adjust_param_indexes(struct prog_instruction *inst, GLuint numInst,
648 GLuint offset)
649 {
650 GLuint i, j;
651 for (i = 0; i < numInst; i++) {
652 for (j = 0; j < _mesa_num_inst_src_regs(inst->Opcode); j++) {
653 GLuint f = inst[i].SrcReg[j].File;
654 if (f == PROGRAM_CONSTANT ||
655 f == PROGRAM_UNIFORM ||
656 f == PROGRAM_STATE_VAR) {
657 inst[i].SrcReg[j].Index += offset;
658 }
659 }
660 }
661 }
662
663
664 /**
665 * Combine two programs into one. Fix instructions so the outputs of
666 * the first program go to the inputs of the second program.
667 */
668 struct gl_program *
669 _mesa_combine_programs(GLcontext *ctx,
670 const struct gl_program *progA,
671 const struct gl_program *progB)
672 {
673 struct prog_instruction *newInst;
674 struct gl_program *newProg;
675 const GLuint lenA = progA->NumInstructions - 1; /* omit END instr */
676 const GLuint lenB = progB->NumInstructions;
677 const GLuint numParamsA = _mesa_num_parameters(progA->Parameters);
678 const GLuint newLength = lenA + lenB;
679 GLbitfield inputsB;
680 GLuint i;
681
682 ASSERT(progA->Target == progB->Target);
683
684 newInst = _mesa_alloc_instructions(newLength);
685 if (!newInst)
686 return GL_FALSE;
687
688 _mesa_copy_instructions(newInst, progA->Instructions, lenA);
689 _mesa_copy_instructions(newInst + lenA, progB->Instructions, lenB);
690
691 /* adjust branch / instruction addresses for B's instructions */
692 for (i = 0; i < lenB; i++) {
693 newInst[lenA + i].BranchTarget += lenA;
694 }
695
696 newProg = ctx->Driver.NewProgram(ctx, progA->Target, 0);
697 newProg->Instructions = newInst;
698 newProg->NumInstructions = newLength;
699
700 if (newProg->Target == GL_FRAGMENT_PROGRAM_ARB) {
701 struct gl_fragment_program *fprogA, *fprogB, *newFprog;
702 GLbitfield progB_inputsRead = progB->InputsRead;
703 GLint progB_colorFile, progB_colorIndex;
704
705 fprogA = (struct gl_fragment_program *) progA;
706 fprogB = (struct gl_fragment_program *) progB;
707 newFprog = (struct gl_fragment_program *) newProg;
708
709 newFprog->UsesKill = fprogA->UsesKill || fprogB->UsesKill;
710
711 /* We'll do a search and replace for instances
712 * of progB_colorFile/progB_colorIndex below...
713 */
714 progB_colorFile = PROGRAM_INPUT;
715 progB_colorIndex = FRAG_ATTRIB_COL0;
716
717 /*
718 * The fragment program may get color from a state var rather than
719 * a fragment input (vertex output) if it's constant.
720 * See the texenvprogram.c code.
721 * So, search the program's parameter list now to see if the program
722 * gets color from a state var instead of a conventional fragment
723 * input register.
724 */
725 for (i = 0; i < progB->Parameters->NumParameters; i++) {
726 struct gl_program_parameter *p = &progB->Parameters->Parameters[i];
727 if (p->Type == PROGRAM_STATE_VAR &&
728 p->StateIndexes[0] == STATE_INTERNAL &&
729 p->StateIndexes[1] == STATE_CURRENT_ATTRIB &&
730 p->StateIndexes[2] == VERT_ATTRIB_COLOR0) {
731 progB_inputsRead |= FRAG_BIT_COL0;
732 progB_colorFile = PROGRAM_STATE_VAR;
733 progB_colorIndex = i;
734 break;
735 }
736 }
737
738 /* Connect color outputs of fprogA to color inputs of fprogB, via a
739 * new temporary register.
740 */
741 if ((progA->OutputsWritten & (1 << FRAG_RESULT_COLOR)) &&
742 (progB_inputsRead & FRAG_BIT_COL0)) {
743 GLint tempReg = _mesa_find_free_register(newProg, PROGRAM_TEMPORARY);
744 if (tempReg < 0) {
745 _mesa_problem(ctx, "No free temp regs found in "
746 "_mesa_combine_programs(), using 31");
747 tempReg = 31;
748 }
749 /* replace writes to result.color[0] with tempReg */
750 replace_registers(newInst, lenA,
751 PROGRAM_OUTPUT, FRAG_RESULT_COLOR,
752 PROGRAM_TEMPORARY, tempReg);
753 /* replace reads from the input color with tempReg */
754 replace_registers(newInst + lenA, lenB,
755 progB_colorFile, progB_colorIndex, /* search for */
756 PROGRAM_TEMPORARY, tempReg /* replace with */ );
757 }
758
759 /* compute combined program's InputsRead */
760 inputsB = progB_inputsRead;
761 if (progA->OutputsWritten & (1 << FRAG_RESULT_COLOR)) {
762 inputsB &= ~(1 << FRAG_ATTRIB_COL0);
763 }
764 newProg->InputsRead = progA->InputsRead | inputsB;
765 newProg->OutputsWritten = progB->OutputsWritten;
766 newProg->SamplersUsed = progA->SamplersUsed | progB->SamplersUsed;
767 }
768 else {
769 /* vertex program */
770 assert(0); /* XXX todo */
771 }
772
773 /*
774 * Merge parameters (uniforms, constants, etc)
775 */
776 newProg->Parameters = _mesa_combine_parameter_lists(progA->Parameters,
777 progB->Parameters);
778
779 adjust_param_indexes(newInst + lenA, lenB, numParamsA);
780
781
782 return newProg;
783 }
784
785
786
787
788 /**
789 * Scan the given program to find a free register of the given type.
790 * \param regFile - PROGRAM_INPUT, PROGRAM_OUTPUT or PROGRAM_TEMPORARY
791 */
792 GLint
793 _mesa_find_free_register(const struct gl_program *prog, GLuint regFile)
794 {
795 GLboolean used[MAX_PROGRAM_TEMPS];
796 GLuint i, k;
797
798 assert(regFile == PROGRAM_INPUT ||
799 regFile == PROGRAM_OUTPUT ||
800 regFile == PROGRAM_TEMPORARY);
801
802 _mesa_memset(used, 0, sizeof(used));
803
804 for (i = 0; i < prog->NumInstructions; i++) {
805 const struct prog_instruction *inst = prog->Instructions + i;
806 const GLuint n = _mesa_num_inst_src_regs(inst->Opcode);
807
808 for (k = 0; k < n; k++) {
809 if (inst->SrcReg[k].File == regFile) {
810 used[inst->SrcReg[k].Index] = GL_TRUE;
811 }
812 }
813 }
814
815 for (i = 0; i < MAX_PROGRAM_TEMPS; i++) {
816 if (!used[i])
817 return i;
818 }
819
820 return -1;
821 }