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