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