2 * Mesa 3-D graphics library
5 * Copyright (C) 1999-2007 Brian Paul All Rights Reserved.
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:
14 * The above copyright notice and this permission notice shall be included
15 * in all copies or substantial portions of the Software.
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.
27 * Vertex and fragment program support functions.
32 #include "main/glheader.h"
33 #include "main/context.h"
34 #include "main/hash.h"
36 #include "prog_cache.h"
37 #include "prog_parameter.h"
38 #include "prog_instruction.h"
42 * A pointer to this dummy program is put into the hash table when
43 * glGenPrograms is called.
45 struct gl_program _mesa_DummyProgram
;
49 * Init context's vertex/fragment program state
52 _mesa_init_program(GLcontext
*ctx
)
56 ctx
->Program
.ErrorPos
= -1;
57 ctx
->Program
.ErrorString
= _mesa_strdup("");
59 #if FEATURE_NV_vertex_program || FEATURE_ARB_vertex_program
60 ctx
->VertexProgram
.Enabled
= GL_FALSE
;
62 ctx
->VertexProgram
.PointSizeEnabled
= GL_TRUE
;
64 ctx
->VertexProgram
.PointSizeEnabled
= GL_FALSE
;
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
;
74 ctx
->VertexProgram
.Cache
= _mesa_new_program_cache();
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();
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
++;
97 * Free a context's vertex/fragment program state
100 _mesa_free_program_data(GLcontext
*ctx
)
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
);
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
);
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
);
119 _mesa_free((void *) ctx
->Program
.ErrorString
);
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
129 _mesa_update_default_objects_program(GLcontext
*ctx
)
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
);
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
);
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
);
153 ctx
->ATIFragmentShader
.Current
= (struct ati_fragment_shader
*) ctx
->Shared
->DefaultFragmentShader
;
154 assert(ctx
->ATIFragmentShader
.Current
);
155 ctx
->ATIFragmentShader
.Current
->RefCount
++;
161 * Set the vertex/fragment program error state (position and error string).
162 * This is generally called from within the parsers.
165 _mesa_set_program_error(GLcontext
*ctx
, GLint pos
, const char *string
)
167 ctx
->Program
.ErrorPos
= pos
;
168 _mesa_free((void *) ctx
->Program
.ErrorString
);
171 ctx
->Program
.ErrorString
= _mesa_strdup(string
);
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
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'.
186 _mesa_find_line_column(const GLubyte
*string
, const GLubyte
*pos
,
187 GLint
*line
, GLint
*col
)
189 const GLubyte
*lineStart
= string
;
190 const GLubyte
*p
= string
;
197 if (*p
== (GLubyte
) '\n') {
204 *col
= (pos
- lineStart
) + 1;
206 /* return copy of this line */
207 while (*p
!= 0 && *p
!= '\n')
210 s
= (GLubyte
*) _mesa_malloc(len
+ 1);
211 _mesa_memcpy(s
, lineStart
, len
);
219 * Initialize a new vertex/fragment program object.
221 static struct gl_program
*
222 _mesa_init_program_struct( GLcontext
*ctx
, struct gl_program
*prog
,
223 GLenum target
, GLuint id
)
228 _mesa_bzero(prog
, sizeof(*prog
));
230 prog
->Target
= target
;
231 prog
->Resident
= GL_TRUE
;
233 prog
->Format
= GL_PROGRAM_FORMAT_ASCII_ARB
;
235 /* default mapping from samplers to texture units */
236 for (i
= 0; i
< MAX_SAMPLERS
; i
++)
237 prog
->SamplerUnits
[i
] = i
;
245 * Initialize a new fragment program object.
248 _mesa_init_fragment_program( GLcontext
*ctx
, struct gl_fragment_program
*prog
,
249 GLenum target
, GLuint id
)
252 return _mesa_init_program_struct( ctx
, &prog
->Base
, target
, id
);
259 * Initialize a new vertex program object.
262 _mesa_init_vertex_program( GLcontext
*ctx
, struct gl_vertex_program
*prog
,
263 GLenum target
, GLuint id
)
266 return _mesa_init_program_struct( ctx
, &prog
->Base
, target
, id
);
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.
280 * \param id program id/number
281 * \param target program target/type
282 * \return pointer to new program object
285 _mesa_new_program(GLcontext
*ctx
, GLenum target
, GLuint id
)
287 struct gl_program
*prog
;
289 case GL_VERTEX_PROGRAM_ARB
: /* == GL_VERTEX_PROGRAM_NV */
290 prog
= _mesa_init_vertex_program(ctx
, CALLOC_STRUCT(gl_vertex_program
),
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
),
300 _mesa_problem(ctx
, "bad target in _mesa_new_program");
308 * Delete a program and remove it from the hash table, ignoring the
310 * Called via ctx->Driver.DeleteProgram. May be wrapped (OO deriviation)
311 * by a device driver function.
314 _mesa_delete_program(GLcontext
*ctx
, struct gl_program
*prog
)
318 ASSERT(prog
->RefCount
==0);
320 if (prog
== &_mesa_DummyProgram
)
324 _mesa_free(prog
->String
);
326 _mesa_free_instructions(prog
->Instructions
, prog
->NumInstructions
);
328 if (prog
->Parameters
) {
329 _mesa_free_parameter_list(prog
->Parameters
);
332 _mesa_free_parameter_list(prog
->Varying
);
334 if (prog
->Attributes
) {
335 _mesa_free_parameter_list(prog
->Attributes
);
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
;
342 _mesa_free(vprog
->TnlData
);
350 * Return the gl_program object for a given ID.
351 * Basically just a wrapper for _mesa_HashLookup() to avoid a lot of
355 _mesa_lookup_program(GLcontext
*ctx
, GLuint id
)
358 return (struct gl_program
*) _mesa_HashLookup(ctx
->Shared
->Programs
, id
);
365 * Reference counting for vertex/fragment programs
368 _mesa_reference_program(GLcontext
*ctx
,
369 struct gl_program
**ptr
,
370 struct gl_program
*prog
)
375 if ((*ptr
)->Target
== GL_VERTEX_PROGRAM_ARB
)
376 ASSERT(prog
->Target
== GL_VERTEX_PROGRAM_ARB
);
377 else if ((*ptr
)->Target
== GL_FRAGMENT_PROGRAM_ARB
)
378 ASSERT(prog
->Target
== GL_FRAGMENT_PROGRAM_ARB
||
379 prog
->Target
== GL_FRAGMENT_PROGRAM_NV
);
382 return; /* no change */
385 GLboolean deleteFlag
;
387 /*_glthread_LOCK_MUTEX((*ptr)->Mutex);*/
389 printf("Program %p ID=%u Target=%s Refcount-- to %d\n",
391 ((*ptr
)->Target
== GL_VERTEX_PROGRAM_ARB
? "VP" : "FP"),
392 (*ptr
)->RefCount
- 1);
394 ASSERT((*ptr
)->RefCount
> 0);
397 deleteFlag
= ((*ptr
)->RefCount
== 0);
398 /*_glthread_UNLOCK_MUTEX((*ptr)->Mutex);*/
402 ctx
->Driver
.DeleteProgram(ctx
, *ptr
);
410 /*_glthread_LOCK_MUTEX(prog->Mutex);*/
413 printf("Program %p ID=%u Target=%s Refcount++ to %d\n",
415 (prog
->Target
== GL_VERTEX_PROGRAM_ARB
? "VP" : "FP"),
418 /*_glthread_UNLOCK_MUTEX(prog->Mutex);*/
426 * Return a copy of a program.
427 * XXX Problem here if the program object is actually OO-derivation
428 * made by a device driver.
431 _mesa_clone_program(GLcontext
*ctx
, const struct gl_program
*prog
)
433 struct gl_program
*clone
;
435 clone
= ctx
->Driver
.NewProgram(ctx
, prog
->Target
, prog
->Id
);
439 assert(clone
->Target
== prog
->Target
);
440 assert(clone
->RefCount
== 1);
442 clone
->String
= (GLubyte
*) _mesa_strdup((char *) prog
->String
);
443 clone
->Format
= prog
->Format
;
444 clone
->Instructions
= _mesa_alloc_instructions(prog
->NumInstructions
);
445 if (!clone
->Instructions
) {
446 _mesa_reference_program(ctx
, &clone
, NULL
);
449 _mesa_copy_instructions(clone
->Instructions
, prog
->Instructions
,
450 prog
->NumInstructions
);
451 clone
->InputsRead
= prog
->InputsRead
;
452 clone
->OutputsWritten
= prog
->OutputsWritten
;
453 clone
->SamplersUsed
= prog
->SamplersUsed
;
454 clone
->ShadowSamplers
= prog
->ShadowSamplers
;
455 memcpy(clone
->TexturesUsed
, prog
->TexturesUsed
, sizeof(prog
->TexturesUsed
));
457 if (prog
->Parameters
)
458 clone
->Parameters
= _mesa_clone_parameter_list(prog
->Parameters
);
459 memcpy(clone
->LocalParams
, prog
->LocalParams
, sizeof(clone
->LocalParams
));
461 clone
->Varying
= _mesa_clone_parameter_list(prog
->Varying
);
462 if (prog
->Attributes
)
463 clone
->Attributes
= _mesa_clone_parameter_list(prog
->Attributes
);
464 memcpy(clone
->LocalParams
, prog
->LocalParams
, sizeof(clone
->LocalParams
));
465 clone
->NumInstructions
= prog
->NumInstructions
;
466 clone
->NumTemporaries
= prog
->NumTemporaries
;
467 clone
->NumParameters
= prog
->NumParameters
;
468 clone
->NumAttributes
= prog
->NumAttributes
;
469 clone
->NumAddressRegs
= prog
->NumAddressRegs
;
470 clone
->NumNativeInstructions
= prog
->NumNativeInstructions
;
471 clone
->NumNativeTemporaries
= prog
->NumNativeTemporaries
;
472 clone
->NumNativeParameters
= prog
->NumNativeParameters
;
473 clone
->NumNativeAttributes
= prog
->NumNativeAttributes
;
474 clone
->NumNativeAddressRegs
= prog
->NumNativeAddressRegs
;
475 clone
->NumAluInstructions
= prog
->NumAluInstructions
;
476 clone
->NumTexInstructions
= prog
->NumTexInstructions
;
477 clone
->NumTexIndirections
= prog
->NumTexIndirections
;
478 clone
->NumNativeAluInstructions
= prog
->NumNativeAluInstructions
;
479 clone
->NumNativeTexInstructions
= prog
->NumNativeTexInstructions
;
480 clone
->NumNativeTexIndirections
= prog
->NumNativeTexIndirections
;
482 switch (prog
->Target
) {
483 case GL_VERTEX_PROGRAM_ARB
:
485 const struct gl_vertex_program
*vp
486 = (const struct gl_vertex_program
*) prog
;
487 struct gl_vertex_program
*vpc
= (struct gl_vertex_program
*) clone
;
488 vpc
->IsPositionInvariant
= vp
->IsPositionInvariant
;
491 case GL_FRAGMENT_PROGRAM_ARB
:
493 const struct gl_fragment_program
*fp
494 = (const struct gl_fragment_program
*) prog
;
495 struct gl_fragment_program
*fpc
= (struct gl_fragment_program
*) clone
;
496 fpc
->FogOption
= fp
->FogOption
;
497 fpc
->UsesKill
= fp
->UsesKill
;
501 _mesa_problem(NULL
, "Unexpected target in _mesa_clone_program");
509 * Insert 'count' NOP instructions at 'start' in the given program.
510 * Adjust branch targets accordingly.
513 _mesa_insert_instructions(struct gl_program
*prog
, GLuint start
, GLuint count
)
515 const GLuint origLen
= prog
->NumInstructions
;
516 const GLuint newLen
= origLen
+ count
;
517 struct prog_instruction
*newInst
;
520 /* adjust branches */
521 for (i
= 0; i
< prog
->NumInstructions
; i
++) {
522 struct prog_instruction
*inst
= prog
->Instructions
+ i
;
523 if (inst
->BranchTarget
> 0) {
524 if ((GLuint
)inst
->BranchTarget
>= start
) {
525 inst
->BranchTarget
+= count
;
530 /* Alloc storage for new instructions */
531 newInst
= _mesa_alloc_instructions(newLen
);
536 /* Copy 'start' instructions into new instruction buffer */
537 _mesa_copy_instructions(newInst
, prog
->Instructions
, start
);
539 /* init the new instructions */
540 _mesa_init_instructions(newInst
+ start
, count
);
542 /* Copy the remaining/tail instructions to new inst buffer */
543 _mesa_copy_instructions(newInst
+ start
+ count
,
544 prog
->Instructions
+ start
,
547 /* free old instructions */
548 _mesa_free_instructions(prog
->Instructions
, origLen
);
550 /* install new instructions */
551 prog
->Instructions
= newInst
;
552 prog
->NumInstructions
= newLen
;
558 * Delete 'count' instructions at 'start' in the given program.
559 * Adjust branch targets accordingly.
562 _mesa_delete_instructions(struct gl_program
*prog
, GLuint start
, GLuint count
)
564 const GLuint origLen
= prog
->NumInstructions
;
565 const GLuint newLen
= origLen
- count
;
566 struct prog_instruction
*newInst
;
569 /* adjust branches */
570 for (i
= 0; i
< prog
->NumInstructions
; i
++) {
571 struct prog_instruction
*inst
= prog
->Instructions
+ i
;
572 if (inst
->BranchTarget
> 0) {
573 if (inst
->BranchTarget
> start
) {
574 inst
->BranchTarget
-= count
;
579 /* Alloc storage for new instructions */
580 newInst
= _mesa_alloc_instructions(newLen
);
585 /* Copy 'start' instructions into new instruction buffer */
586 _mesa_copy_instructions(newInst
, prog
->Instructions
, start
);
588 /* Copy the remaining/tail instructions to new inst buffer */
589 _mesa_copy_instructions(newInst
+ start
,
590 prog
->Instructions
+ start
+ count
,
593 /* free old instructions */
594 _mesa_free_instructions(prog
->Instructions
, origLen
);
596 /* install new instructions */
597 prog
->Instructions
= newInst
;
598 prog
->NumInstructions
= newLen
;
605 * Search instructions for registers that match (oldFile, oldIndex),
606 * replacing them with (newFile, newIndex).
609 replace_registers(struct prog_instruction
*inst
, GLuint numInst
,
610 GLuint oldFile
, GLuint oldIndex
,
611 GLuint newFile
, GLuint newIndex
)
614 for (i
= 0; i
< numInst
; i
++) {
616 for (j
= 0; j
< _mesa_num_inst_src_regs(inst
->Opcode
); j
++) {
617 if (inst
[i
].SrcReg
[j
].File
== oldFile
&&
618 inst
[i
].SrcReg
[j
].Index
== oldIndex
) {
619 inst
[i
].SrcReg
[j
].File
= newFile
;
620 inst
[i
].SrcReg
[j
].Index
= newIndex
;
624 if (inst
[i
].DstReg
.File
== oldFile
&& inst
[i
].DstReg
.Index
== oldIndex
) {
625 inst
[i
].DstReg
.File
= newFile
;
626 inst
[i
].DstReg
.Index
= newIndex
;
633 * Search instructions for references to program parameters. When found,
634 * increment the parameter index by 'offset'.
635 * Used when combining programs.
638 adjust_param_indexes(struct prog_instruction
*inst
, GLuint numInst
,
642 for (i
= 0; i
< numInst
; i
++) {
643 for (j
= 0; j
< _mesa_num_inst_src_regs(inst
->Opcode
); j
++) {
644 GLuint f
= inst
[i
].SrcReg
[j
].File
;
645 if (f
== PROGRAM_CONSTANT
||
646 f
== PROGRAM_UNIFORM
||
647 f
== PROGRAM_STATE_VAR
) {
648 inst
[i
].SrcReg
[j
].Index
+= offset
;
656 * Combine two programs into one. Fix instructions so the outputs of
657 * the first program go to the inputs of the second program.
660 _mesa_combine_programs(GLcontext
*ctx
,
661 const struct gl_program
*progA
,
662 const struct gl_program
*progB
)
664 struct prog_instruction
*newInst
;
665 struct gl_program
*newProg
;
666 const GLuint lenA
= progA
->NumInstructions
- 1; /* omit END instr */
667 const GLuint lenB
= progB
->NumInstructions
;
668 const GLuint numParamsA
= _mesa_num_parameters(progA
->Parameters
);
669 const GLuint newLength
= lenA
+ lenB
;
673 ASSERT(progA
->Target
== progB
->Target
);
675 newInst
= _mesa_alloc_instructions(newLength
);
679 _mesa_copy_instructions(newInst
, progA
->Instructions
, lenA
);
680 _mesa_copy_instructions(newInst
+ lenA
, progB
->Instructions
, lenB
);
682 /* adjust branch / instruction addresses for B's instructions */
683 for (i
= 0; i
< lenB
; i
++) {
684 newInst
[lenA
+ i
].BranchTarget
+= lenA
;
687 newProg
= ctx
->Driver
.NewProgram(ctx
, progA
->Target
, 0);
688 newProg
->Instructions
= newInst
;
689 newProg
->NumInstructions
= newLength
;
691 if (newProg
->Target
== GL_FRAGMENT_PROGRAM_ARB
) {
692 struct gl_fragment_program
*fprogA
, *fprogB
, *newFprog
;
693 GLbitfield progB_inputsRead
= progB
->InputsRead
;
694 GLint progB_colorFile
, progB_colorIndex
;
696 fprogA
= (struct gl_fragment_program
*) progA
;
697 fprogB
= (struct gl_fragment_program
*) progB
;
698 newFprog
= (struct gl_fragment_program
*) newProg
;
700 newFprog
->UsesKill
= fprogA
->UsesKill
|| fprogB
->UsesKill
;
702 /* We'll do a search and replace for instances
703 * of progB_colorFile/progB_colorIndex below...
705 progB_colorFile
= PROGRAM_INPUT
;
706 progB_colorIndex
= FRAG_ATTRIB_COL0
;
709 * The fragment program may get color from a state var rather than
710 * a fragment input (vertex output) if it's constant.
711 * See the texenvprogram.c code.
712 * So, search the program's parameter list now to see if the program
713 * gets color from a state var instead of a conventional fragment
716 for (i
= 0; i
< progB
->Parameters
->NumParameters
; i
++) {
717 struct gl_program_parameter
*p
= &progB
->Parameters
->Parameters
[i
];
718 if (p
->Type
== PROGRAM_STATE_VAR
&&
719 p
->StateIndexes
[0] == STATE_INTERNAL
&&
720 p
->StateIndexes
[1] == STATE_CURRENT_ATTRIB
&&
721 p
->StateIndexes
[2] == VERT_ATTRIB_COLOR0
) {
722 progB_inputsRead
|= FRAG_BIT_COL0
;
723 progB_colorFile
= PROGRAM_STATE_VAR
;
724 progB_colorIndex
= i
;
729 /* Connect color outputs of fprogA to color inputs of fprogB, via a
730 * new temporary register.
732 if ((progA
->OutputsWritten
& (1 << FRAG_RESULT_COLR
)) &&
733 (progB_inputsRead
& FRAG_BIT_COL0
)) {
734 GLint tempReg
= _mesa_find_free_register(newProg
, PROGRAM_TEMPORARY
);
736 _mesa_problem(ctx
, "No free temp regs found in "
737 "_mesa_combine_programs(), using 31");
740 /* replace writes to result.color[0] with tempReg */
741 replace_registers(newInst
, lenA
,
742 PROGRAM_OUTPUT
, FRAG_RESULT_COLR
,
743 PROGRAM_TEMPORARY
, tempReg
);
744 /* replace reads from the input color with tempReg */
745 replace_registers(newInst
+ lenA
, lenB
,
746 progB_colorFile
, progB_colorIndex
, /* search for */
747 PROGRAM_TEMPORARY
, tempReg
/* replace with */ );
750 /* compute combined program's InputsRead */
751 inputsB
= progB_inputsRead
;
752 if (progA
->OutputsWritten
& (1 << FRAG_RESULT_COLR
)) {
753 inputsB
&= ~(1 << FRAG_ATTRIB_COL0
);
755 newProg
->InputsRead
= progA
->InputsRead
| inputsB
;
756 newProg
->OutputsWritten
= progB
->OutputsWritten
;
757 newProg
->SamplersUsed
= progA
->SamplersUsed
| progB
->SamplersUsed
;
761 assert(0); /* XXX todo */
765 * Merge parameters (uniforms, constants, etc)
767 newProg
->Parameters
= _mesa_combine_parameter_lists(progA
->Parameters
,
770 adjust_param_indexes(newInst
+ lenA
, lenB
, numParamsA
);
780 * Scan the given program to find a free register of the given type.
781 * \param regFile - PROGRAM_INPUT, PROGRAM_OUTPUT or PROGRAM_TEMPORARY
784 _mesa_find_free_register(const struct gl_program
*prog
, GLuint regFile
)
786 GLboolean used
[MAX_PROGRAM_TEMPS
];
789 assert(regFile
== PROGRAM_INPUT
||
790 regFile
== PROGRAM_OUTPUT
||
791 regFile
== PROGRAM_TEMPORARY
);
793 _mesa_memset(used
, 0, sizeof(used
));
795 for (i
= 0; i
< prog
->NumInstructions
; i
++) {
796 const struct prog_instruction
*inst
= prog
->Instructions
+ i
;
797 const GLuint n
= _mesa_num_inst_src_regs(inst
->Opcode
);
799 for (k
= 0; k
< n
; k
++) {
800 if (inst
->SrcReg
[k
].File
== regFile
) {
801 used
[inst
->SrcReg
[k
].Index
] = GL_TRUE
;
806 for (i
= 0; i
< MAX_PROGRAM_TEMPS
; i
++) {