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
)
57 * If this assertion fails, we need to increase the field
58 * size for register indexes.
60 ASSERT(ctx
->Const
.VertexProgram
.MaxUniformComponents
/ 4
61 <= (1 << INST_INDEX_BITS
));
62 ASSERT(ctx
->Const
.FragmentProgram
.MaxUniformComponents
/ 4
63 <= (1 << INST_INDEX_BITS
));
65 /* If this fails, increase prog_instruction::TexSrcUnit size */
66 ASSERT(MAX_TEXTURE_UNITS
< (1 << 5));
68 /* If this fails, increase prog_instruction::TexSrcTarget size */
69 ASSERT(NUM_TEXTURE_TARGETS
< (1 << 3));
71 ctx
->Program
.ErrorPos
= -1;
72 ctx
->Program
.ErrorString
= _mesa_strdup("");
74 #if FEATURE_NV_vertex_program || FEATURE_ARB_vertex_program
75 ctx
->VertexProgram
.Enabled
= GL_FALSE
;
77 ctx
->VertexProgram
.PointSizeEnabled
=
78 (ctx
->API
== API_OPENGLES2
) ? GL_TRUE
: GL_FALSE
;
80 ctx
->VertexProgram
.PointSizeEnabled
= GL_FALSE
;
82 ctx
->VertexProgram
.TwoSideEnabled
= GL_FALSE
;
83 _mesa_reference_vertprog(ctx
, &ctx
->VertexProgram
.Current
,
84 ctx
->Shared
->DefaultVertexProgram
);
85 assert(ctx
->VertexProgram
.Current
);
86 for (i
= 0; i
< MAX_NV_VERTEX_PROGRAM_PARAMS
/ 4; i
++) {
87 ctx
->VertexProgram
.TrackMatrix
[i
] = GL_NONE
;
88 ctx
->VertexProgram
.TrackMatrixTransform
[i
] = GL_IDENTITY_NV
;
90 ctx
->VertexProgram
.Cache
= _mesa_new_program_cache();
93 #if FEATURE_NV_fragment_program || FEATURE_ARB_fragment_program
94 ctx
->FragmentProgram
.Enabled
= GL_FALSE
;
95 _mesa_reference_fragprog(ctx
, &ctx
->FragmentProgram
.Current
,
96 ctx
->Shared
->DefaultFragmentProgram
);
97 assert(ctx
->FragmentProgram
.Current
);
98 ctx
->FragmentProgram
.Cache
= _mesa_new_program_cache();
102 /* XXX probably move this stuff */
103 #if FEATURE_ATI_fragment_shader
104 ctx
->ATIFragmentShader
.Enabled
= GL_FALSE
;
105 ctx
->ATIFragmentShader
.Current
= ctx
->Shared
->DefaultFragmentShader
;
106 assert(ctx
->ATIFragmentShader
.Current
);
107 ctx
->ATIFragmentShader
.Current
->RefCount
++;
113 * Free a context's vertex/fragment program state
116 _mesa_free_program_data(GLcontext
*ctx
)
118 #if FEATURE_NV_vertex_program || FEATURE_ARB_vertex_program
119 _mesa_reference_vertprog(ctx
, &ctx
->VertexProgram
.Current
, NULL
);
120 _mesa_delete_program_cache(ctx
, ctx
->VertexProgram
.Cache
);
122 #if FEATURE_NV_fragment_program || FEATURE_ARB_fragment_program
123 _mesa_reference_fragprog(ctx
, &ctx
->FragmentProgram
.Current
, NULL
);
124 _mesa_delete_program_cache(ctx
, ctx
->FragmentProgram
.Cache
);
126 /* XXX probably move this stuff */
127 #if FEATURE_ATI_fragment_shader
128 if (ctx
->ATIFragmentShader
.Current
) {
129 ctx
->ATIFragmentShader
.Current
->RefCount
--;
130 if (ctx
->ATIFragmentShader
.Current
->RefCount
<= 0) {
131 free(ctx
->ATIFragmentShader
.Current
);
135 free((void *) ctx
->Program
.ErrorString
);
140 * Update the default program objects in the given context to reference those
141 * specified in the shared state and release those referencing the old
145 _mesa_update_default_objects_program(GLcontext
*ctx
)
147 #if FEATURE_NV_vertex_program || FEATURE_ARB_vertex_program
148 _mesa_reference_vertprog(ctx
, &ctx
->VertexProgram
.Current
,
149 (struct gl_vertex_program
*)
150 ctx
->Shared
->DefaultVertexProgram
);
151 assert(ctx
->VertexProgram
.Current
);
154 #if FEATURE_NV_fragment_program || FEATURE_ARB_fragment_program
155 _mesa_reference_fragprog(ctx
, &ctx
->FragmentProgram
.Current
,
156 (struct gl_fragment_program
*)
157 ctx
->Shared
->DefaultFragmentProgram
);
158 assert(ctx
->FragmentProgram
.Current
);
161 /* XXX probably move this stuff */
162 #if FEATURE_ATI_fragment_shader
163 if (ctx
->ATIFragmentShader
.Current
) {
164 ctx
->ATIFragmentShader
.Current
->RefCount
--;
165 if (ctx
->ATIFragmentShader
.Current
->RefCount
<= 0) {
166 free(ctx
->ATIFragmentShader
.Current
);
169 ctx
->ATIFragmentShader
.Current
= (struct ati_fragment_shader
*) ctx
->Shared
->DefaultFragmentShader
;
170 assert(ctx
->ATIFragmentShader
.Current
);
171 ctx
->ATIFragmentShader
.Current
->RefCount
++;
177 * Set the vertex/fragment program error state (position and error string).
178 * This is generally called from within the parsers.
181 _mesa_set_program_error(GLcontext
*ctx
, GLint pos
, const char *string
)
183 ctx
->Program
.ErrorPos
= pos
;
184 free((void *) ctx
->Program
.ErrorString
);
187 ctx
->Program
.ErrorString
= _mesa_strdup(string
);
192 * Find the line number and column for 'pos' within 'string'.
193 * Return a copy of the line which contains 'pos'. Free the line with
195 * \param string the program string
196 * \param pos the position within the string
197 * \param line returns the line number corresponding to 'pos'.
198 * \param col returns the column number corresponding to 'pos'.
199 * \return copy of the line containing 'pos'.
202 _mesa_find_line_column(const GLubyte
*string
, const GLubyte
*pos
,
203 GLint
*line
, GLint
*col
)
205 const GLubyte
*lineStart
= string
;
206 const GLubyte
*p
= string
;
213 if (*p
== (GLubyte
) '\n') {
220 *col
= (pos
- lineStart
) + 1;
222 /* return copy of this line */
223 while (*p
!= 0 && *p
!= '\n')
226 s
= (GLubyte
*) malloc(len
+ 1);
227 memcpy(s
, lineStart
, len
);
235 * Initialize a new vertex/fragment program object.
237 static struct gl_program
*
238 _mesa_init_program_struct( GLcontext
*ctx
, struct gl_program
*prog
,
239 GLenum target
, GLuint id
)
244 memset(prog
, 0, sizeof(*prog
));
246 prog
->Target
= target
;
247 prog
->Resident
= GL_TRUE
;
249 prog
->Format
= GL_PROGRAM_FORMAT_ASCII_ARB
;
251 /* default mapping from samplers to texture units */
252 for (i
= 0; i
< MAX_SAMPLERS
; i
++)
253 prog
->SamplerUnits
[i
] = i
;
261 * Initialize a new fragment program object.
264 _mesa_init_fragment_program( GLcontext
*ctx
, struct gl_fragment_program
*prog
,
265 GLenum target
, GLuint id
)
268 return _mesa_init_program_struct( ctx
, &prog
->Base
, target
, id
);
275 * Initialize a new vertex program object.
278 _mesa_init_vertex_program( GLcontext
*ctx
, struct gl_vertex_program
*prog
,
279 GLenum target
, GLuint id
)
282 return _mesa_init_program_struct( ctx
, &prog
->Base
, target
, id
);
289 * Allocate and initialize a new fragment/vertex program object but
290 * don't put it into the program hash table. Called via
291 * ctx->Driver.NewProgram. May be overridden (ie. replaced) by a
292 * device driver function to implement OO deriviation with additional
293 * types not understood by this function.
296 * \param id program id/number
297 * \param target program target/type
298 * \return pointer to new program object
301 _mesa_new_program(GLcontext
*ctx
, GLenum target
, GLuint id
)
303 struct gl_program
*prog
;
305 case GL_VERTEX_PROGRAM_ARB
: /* == GL_VERTEX_PROGRAM_NV */
306 case GL_VERTEX_STATE_PROGRAM_NV
:
307 prog
= _mesa_init_vertex_program(ctx
, CALLOC_STRUCT(gl_vertex_program
),
310 case GL_FRAGMENT_PROGRAM_NV
:
311 case GL_FRAGMENT_PROGRAM_ARB
:
312 prog
=_mesa_init_fragment_program(ctx
,
313 CALLOC_STRUCT(gl_fragment_program
),
317 _mesa_problem(ctx
, "bad target in _mesa_new_program");
325 * Delete a program and remove it from the hash table, ignoring the
327 * Called via ctx->Driver.DeleteProgram. May be wrapped (OO deriviation)
328 * by a device driver function.
331 _mesa_delete_program(GLcontext
*ctx
, struct gl_program
*prog
)
335 ASSERT(prog
->RefCount
==0);
337 if (prog
== &_mesa_DummyProgram
)
343 _mesa_free_instructions(prog
->Instructions
, prog
->NumInstructions
);
345 if (prog
->Parameters
) {
346 _mesa_free_parameter_list(prog
->Parameters
);
349 _mesa_free_parameter_list(prog
->Varying
);
351 if (prog
->Attributes
) {
352 _mesa_free_parameter_list(prog
->Attributes
);
360 * Return the gl_program object for a given ID.
361 * Basically just a wrapper for _mesa_HashLookup() to avoid a lot of
365 _mesa_lookup_program(GLcontext
*ctx
, GLuint id
)
368 return (struct gl_program
*) _mesa_HashLookup(ctx
->Shared
->Programs
, id
);
375 * Reference counting for vertex/fragment programs
378 _mesa_reference_program(GLcontext
*ctx
,
379 struct gl_program
**ptr
,
380 struct gl_program
*prog
)
385 if ((*ptr
)->Target
== GL_VERTEX_PROGRAM_ARB
)
386 ASSERT(prog
->Target
== GL_VERTEX_PROGRAM_ARB
);
387 else if ((*ptr
)->Target
== GL_FRAGMENT_PROGRAM_ARB
)
388 ASSERT(prog
->Target
== GL_FRAGMENT_PROGRAM_ARB
||
389 prog
->Target
== GL_FRAGMENT_PROGRAM_NV
);
392 return; /* no change */
395 GLboolean deleteFlag
;
397 /*_glthread_LOCK_MUTEX((*ptr)->Mutex);*/
399 printf("Program %p ID=%u Target=%s Refcount-- to %d\n",
401 ((*ptr
)->Target
== GL_VERTEX_PROGRAM_ARB
? "VP" : "FP"),
402 (*ptr
)->RefCount
- 1);
404 ASSERT((*ptr
)->RefCount
> 0);
407 deleteFlag
= ((*ptr
)->RefCount
== 0);
408 /*_glthread_UNLOCK_MUTEX((*ptr)->Mutex);*/
412 ctx
->Driver
.DeleteProgram(ctx
, *ptr
);
420 /*_glthread_LOCK_MUTEX(prog->Mutex);*/
423 printf("Program %p ID=%u Target=%s Refcount++ to %d\n",
425 (prog
->Target
== GL_VERTEX_PROGRAM_ARB
? "VP" : "FP"),
428 /*_glthread_UNLOCK_MUTEX(prog->Mutex);*/
436 * Return a copy of a program.
437 * XXX Problem here if the program object is actually OO-derivation
438 * made by a device driver.
441 _mesa_clone_program(GLcontext
*ctx
, const struct gl_program
*prog
)
443 struct gl_program
*clone
;
445 clone
= ctx
->Driver
.NewProgram(ctx
, prog
->Target
, prog
->Id
);
449 assert(clone
->Target
== prog
->Target
);
450 assert(clone
->RefCount
== 1);
452 clone
->String
= (GLubyte
*) _mesa_strdup((char *) prog
->String
);
453 clone
->Format
= prog
->Format
;
454 clone
->Instructions
= _mesa_alloc_instructions(prog
->NumInstructions
);
455 if (!clone
->Instructions
) {
456 _mesa_reference_program(ctx
, &clone
, NULL
);
459 _mesa_copy_instructions(clone
->Instructions
, prog
->Instructions
,
460 prog
->NumInstructions
);
461 clone
->InputsRead
= prog
->InputsRead
;
462 clone
->OutputsWritten
= prog
->OutputsWritten
;
463 clone
->SamplersUsed
= prog
->SamplersUsed
;
464 clone
->ShadowSamplers
= prog
->ShadowSamplers
;
465 memcpy(clone
->TexturesUsed
, prog
->TexturesUsed
, sizeof(prog
->TexturesUsed
));
467 if (prog
->Parameters
)
468 clone
->Parameters
= _mesa_clone_parameter_list(prog
->Parameters
);
469 memcpy(clone
->LocalParams
, prog
->LocalParams
, sizeof(clone
->LocalParams
));
471 clone
->Varying
= _mesa_clone_parameter_list(prog
->Varying
);
472 if (prog
->Attributes
)
473 clone
->Attributes
= _mesa_clone_parameter_list(prog
->Attributes
);
474 memcpy(clone
->LocalParams
, prog
->LocalParams
, sizeof(clone
->LocalParams
));
475 clone
->NumInstructions
= prog
->NumInstructions
;
476 clone
->NumTemporaries
= prog
->NumTemporaries
;
477 clone
->NumParameters
= prog
->NumParameters
;
478 clone
->NumAttributes
= prog
->NumAttributes
;
479 clone
->NumAddressRegs
= prog
->NumAddressRegs
;
480 clone
->NumNativeInstructions
= prog
->NumNativeInstructions
;
481 clone
->NumNativeTemporaries
= prog
->NumNativeTemporaries
;
482 clone
->NumNativeParameters
= prog
->NumNativeParameters
;
483 clone
->NumNativeAttributes
= prog
->NumNativeAttributes
;
484 clone
->NumNativeAddressRegs
= prog
->NumNativeAddressRegs
;
485 clone
->NumAluInstructions
= prog
->NumAluInstructions
;
486 clone
->NumTexInstructions
= prog
->NumTexInstructions
;
487 clone
->NumTexIndirections
= prog
->NumTexIndirections
;
488 clone
->NumNativeAluInstructions
= prog
->NumNativeAluInstructions
;
489 clone
->NumNativeTexInstructions
= prog
->NumNativeTexInstructions
;
490 clone
->NumNativeTexIndirections
= prog
->NumNativeTexIndirections
;
492 switch (prog
->Target
) {
493 case GL_VERTEX_PROGRAM_ARB
:
495 const struct gl_vertex_program
*vp
496 = (const struct gl_vertex_program
*) prog
;
497 struct gl_vertex_program
*vpc
= (struct gl_vertex_program
*) clone
;
498 vpc
->IsPositionInvariant
= vp
->IsPositionInvariant
;
499 vpc
->IsNVProgram
= vp
->IsNVProgram
;
502 case GL_FRAGMENT_PROGRAM_ARB
:
504 const struct gl_fragment_program
*fp
505 = (const struct gl_fragment_program
*) prog
;
506 struct gl_fragment_program
*fpc
= (struct gl_fragment_program
*) clone
;
507 fpc
->FogOption
= fp
->FogOption
;
508 fpc
->UsesKill
= fp
->UsesKill
;
509 fpc
->OriginUpperLeft
= fp
->OriginUpperLeft
;
510 fpc
->PixelCenterInteger
= fp
->PixelCenterInteger
;
514 _mesa_problem(NULL
, "Unexpected target in _mesa_clone_program");
522 * Insert 'count' NOP instructions at 'start' in the given program.
523 * Adjust branch targets accordingly.
526 _mesa_insert_instructions(struct gl_program
*prog
, GLuint start
, GLuint count
)
528 const GLuint origLen
= prog
->NumInstructions
;
529 const GLuint newLen
= origLen
+ count
;
530 struct prog_instruction
*newInst
;
533 /* adjust branches */
534 for (i
= 0; i
< prog
->NumInstructions
; i
++) {
535 struct prog_instruction
*inst
= prog
->Instructions
+ i
;
536 if (inst
->BranchTarget
> 0) {
537 if ((GLuint
)inst
->BranchTarget
>= start
) {
538 inst
->BranchTarget
+= count
;
543 /* Alloc storage for new instructions */
544 newInst
= _mesa_alloc_instructions(newLen
);
549 /* Copy 'start' instructions into new instruction buffer */
550 _mesa_copy_instructions(newInst
, prog
->Instructions
, start
);
552 /* init the new instructions */
553 _mesa_init_instructions(newInst
+ start
, count
);
555 /* Copy the remaining/tail instructions to new inst buffer */
556 _mesa_copy_instructions(newInst
+ start
+ count
,
557 prog
->Instructions
+ start
,
560 /* free old instructions */
561 _mesa_free_instructions(prog
->Instructions
, origLen
);
563 /* install new instructions */
564 prog
->Instructions
= newInst
;
565 prog
->NumInstructions
= newLen
;
571 * Delete 'count' instructions at 'start' in the given program.
572 * Adjust branch targets accordingly.
575 _mesa_delete_instructions(struct gl_program
*prog
, GLuint start
, GLuint count
)
577 const GLuint origLen
= prog
->NumInstructions
;
578 const GLuint newLen
= origLen
- count
;
579 struct prog_instruction
*newInst
;
582 /* adjust branches */
583 for (i
= 0; i
< prog
->NumInstructions
; i
++) {
584 struct prog_instruction
*inst
= prog
->Instructions
+ i
;
585 if (inst
->BranchTarget
> 0) {
586 if (inst
->BranchTarget
> (GLint
) start
) {
587 inst
->BranchTarget
-= count
;
592 /* Alloc storage for new instructions */
593 newInst
= _mesa_alloc_instructions(newLen
);
598 /* Copy 'start' instructions into new instruction buffer */
599 _mesa_copy_instructions(newInst
, prog
->Instructions
, start
);
601 /* Copy the remaining/tail instructions to new inst buffer */
602 _mesa_copy_instructions(newInst
+ start
,
603 prog
->Instructions
+ start
+ count
,
606 /* free old instructions */
607 _mesa_free_instructions(prog
->Instructions
, origLen
);
609 /* install new instructions */
610 prog
->Instructions
= newInst
;
611 prog
->NumInstructions
= newLen
;
618 * Search instructions for registers that match (oldFile, oldIndex),
619 * replacing them with (newFile, newIndex).
622 replace_registers(struct prog_instruction
*inst
, GLuint numInst
,
623 GLuint oldFile
, GLuint oldIndex
,
624 GLuint newFile
, GLuint newIndex
)
627 for (i
= 0; i
< numInst
; i
++) {
629 for (j
= 0; j
< _mesa_num_inst_src_regs(inst
[i
].Opcode
); j
++) {
630 if (inst
[i
].SrcReg
[j
].File
== oldFile
&&
631 inst
[i
].SrcReg
[j
].Index
== oldIndex
) {
632 inst
[i
].SrcReg
[j
].File
= newFile
;
633 inst
[i
].SrcReg
[j
].Index
= newIndex
;
637 if (inst
[i
].DstReg
.File
== oldFile
&& inst
[i
].DstReg
.Index
== oldIndex
) {
638 inst
[i
].DstReg
.File
= newFile
;
639 inst
[i
].DstReg
.Index
= newIndex
;
646 * Search instructions for references to program parameters. When found,
647 * increment the parameter index by 'offset'.
648 * Used when combining programs.
651 adjust_param_indexes(struct prog_instruction
*inst
, GLuint numInst
,
655 for (i
= 0; i
< numInst
; i
++) {
656 for (j
= 0; j
< _mesa_num_inst_src_regs(inst
[i
].Opcode
); j
++) {
657 GLuint f
= inst
[i
].SrcReg
[j
].File
;
658 if (f
== PROGRAM_CONSTANT
||
659 f
== PROGRAM_UNIFORM
||
660 f
== PROGRAM_STATE_VAR
) {
661 inst
[i
].SrcReg
[j
].Index
+= offset
;
669 * Combine two programs into one. Fix instructions so the outputs of
670 * the first program go to the inputs of the second program.
673 _mesa_combine_programs(GLcontext
*ctx
,
674 const struct gl_program
*progA
,
675 const struct gl_program
*progB
)
677 struct prog_instruction
*newInst
;
678 struct gl_program
*newProg
;
679 const GLuint lenA
= progA
->NumInstructions
- 1; /* omit END instr */
680 const GLuint lenB
= progB
->NumInstructions
;
681 const GLuint numParamsA
= _mesa_num_parameters(progA
->Parameters
);
682 const GLuint newLength
= lenA
+ lenB
;
683 GLboolean usedTemps
[MAX_PROGRAM_TEMPS
];
684 GLuint firstTemp
= 0;
688 ASSERT(progA
->Target
== progB
->Target
);
690 newInst
= _mesa_alloc_instructions(newLength
);
694 _mesa_copy_instructions(newInst
, progA
->Instructions
, lenA
);
695 _mesa_copy_instructions(newInst
+ lenA
, progB
->Instructions
, lenB
);
697 /* adjust branch / instruction addresses for B's instructions */
698 for (i
= 0; i
< lenB
; i
++) {
699 newInst
[lenA
+ i
].BranchTarget
+= lenA
;
702 newProg
= ctx
->Driver
.NewProgram(ctx
, progA
->Target
, 0);
703 newProg
->Instructions
= newInst
;
704 newProg
->NumInstructions
= newLength
;
706 /* find used temp regs (we may need new temps below) */
707 _mesa_find_used_registers(newProg
, PROGRAM_TEMPORARY
,
708 usedTemps
, MAX_PROGRAM_TEMPS
);
710 if (newProg
->Target
== GL_FRAGMENT_PROGRAM_ARB
) {
711 struct gl_fragment_program
*fprogA
, *fprogB
, *newFprog
;
712 GLbitfield progB_inputsRead
= progB
->InputsRead
;
713 GLint progB_colorFile
, progB_colorIndex
;
715 fprogA
= (struct gl_fragment_program
*) progA
;
716 fprogB
= (struct gl_fragment_program
*) progB
;
717 newFprog
= (struct gl_fragment_program
*) newProg
;
719 newFprog
->UsesKill
= fprogA
->UsesKill
|| fprogB
->UsesKill
;
721 /* We'll do a search and replace for instances
722 * of progB_colorFile/progB_colorIndex below...
724 progB_colorFile
= PROGRAM_INPUT
;
725 progB_colorIndex
= FRAG_ATTRIB_COL0
;
728 * The fragment program may get color from a state var rather than
729 * a fragment input (vertex output) if it's constant.
730 * See the texenvprogram.c code.
731 * So, search the program's parameter list now to see if the program
732 * gets color from a state var instead of a conventional fragment
735 for (i
= 0; i
< progB
->Parameters
->NumParameters
; i
++) {
736 struct gl_program_parameter
*p
= &progB
->Parameters
->Parameters
[i
];
737 if (p
->Type
== PROGRAM_STATE_VAR
&&
738 p
->StateIndexes
[0] == STATE_INTERNAL
&&
739 p
->StateIndexes
[1] == STATE_CURRENT_ATTRIB
&&
740 p
->StateIndexes
[2] == VERT_ATTRIB_COLOR0
) {
741 progB_inputsRead
|= FRAG_BIT_COL0
;
742 progB_colorFile
= PROGRAM_STATE_VAR
;
743 progB_colorIndex
= i
;
748 /* Connect color outputs of fprogA to color inputs of fprogB, via a
749 * new temporary register.
751 if ((progA
->OutputsWritten
& (1 << FRAG_RESULT_COLOR
)) &&
752 (progB_inputsRead
& FRAG_BIT_COL0
)) {
753 GLint tempReg
= _mesa_find_free_register(usedTemps
, MAX_PROGRAM_TEMPS
,
756 _mesa_problem(ctx
, "No free temp regs found in "
757 "_mesa_combine_programs(), using 31");
760 firstTemp
= tempReg
+ 1;
762 /* replace writes to result.color[0] with tempReg */
763 replace_registers(newInst
, lenA
,
764 PROGRAM_OUTPUT
, FRAG_RESULT_COLOR
,
765 PROGRAM_TEMPORARY
, tempReg
);
766 /* replace reads from the input color with tempReg */
767 replace_registers(newInst
+ lenA
, lenB
,
768 progB_colorFile
, progB_colorIndex
, /* search for */
769 PROGRAM_TEMPORARY
, tempReg
/* replace with */ );
772 /* compute combined program's InputsRead */
773 inputsB
= progB_inputsRead
;
774 if (progA
->OutputsWritten
& (1 << FRAG_RESULT_COLOR
)) {
775 inputsB
&= ~(1 << FRAG_ATTRIB_COL0
);
777 newProg
->InputsRead
= progA
->InputsRead
| inputsB
;
778 newProg
->OutputsWritten
= progB
->OutputsWritten
;
779 newProg
->SamplersUsed
= progA
->SamplersUsed
| progB
->SamplersUsed
;
783 assert(0); /* XXX todo */
787 * Merge parameters (uniforms, constants, etc)
789 newProg
->Parameters
= _mesa_combine_parameter_lists(progA
->Parameters
,
792 adjust_param_indexes(newInst
+ lenA
, lenB
, numParamsA
);
800 * Populate the 'used' array with flags indicating which registers (TEMPs,
801 * INPUTs, OUTPUTs, etc, are used by the given program.
802 * \param file type of register to scan for
803 * \param used returns true/false flags for in use / free
804 * \param usedSize size of the 'used' array
807 _mesa_find_used_registers(const struct gl_program
*prog
,
808 gl_register_file file
,
809 GLboolean used
[], GLuint usedSize
)
813 memset(used
, 0, usedSize
);
815 for (i
= 0; i
< prog
->NumInstructions
; i
++) {
816 const struct prog_instruction
*inst
= prog
->Instructions
+ i
;
817 const GLuint n
= _mesa_num_inst_src_regs(inst
->Opcode
);
819 if (inst
->DstReg
.File
== file
) {
820 used
[inst
->DstReg
.Index
] = GL_TRUE
;
823 for (j
= 0; j
< n
; j
++) {
824 if (inst
->SrcReg
[j
].File
== file
) {
825 used
[inst
->SrcReg
[j
].Index
] = GL_TRUE
;
833 * Scan the given 'used' register flag array for the first entry
834 * that's >= firstReg.
835 * \param used vector of flags indicating registers in use (as returned
836 * by _mesa_find_used_registers())
837 * \param usedSize size of the 'used' array
838 * \param firstReg first register to start searching at
839 * \return index of unused register, or -1 if none.
842 _mesa_find_free_register(const GLboolean used
[],
843 GLuint usedSize
, GLuint firstReg
)
847 assert(firstReg
< usedSize
);
849 for (i
= firstReg
; i
< usedSize
; i
++)
858 * "Post-process" a GPU program. This is intended to be used for debugging.
859 * Example actions include no-op'ing instructions or changing instruction
863 _mesa_postprocess_program(GLcontext
*ctx
, struct gl_program
*prog
)
865 static const GLfloat white
[4] = { 0.5, 0.5, 0.5, 0.5 };
868 GLint whiteIndex
= _mesa_add_unnamed_constant(prog
->Parameters
,
869 white
, 4, &whiteSwizzle
);
873 for (i
= 0; i
< prog
->NumInstructions
; i
++) {
874 struct prog_instruction
*inst
= prog
->Instructions
+ i
;
875 const GLuint n
= _mesa_num_inst_src_regs(inst
->Opcode
);
879 if (_mesa_is_tex_instruction(inst
->Opcode
)) {
881 /* replace TEX/TXP/TXB with MOV */
882 inst
->Opcode
= OPCODE_MOV
;
883 inst
->DstReg
.WriteMask
= WRITEMASK_XYZW
;
884 inst
->SrcReg
[0].Swizzle
= SWIZZLE_XYZW
;
885 inst
->SrcReg
[0].Negate
= NEGATE_NONE
;
889 /* disable shadow texture mode */
894 if (inst
->Opcode
== OPCODE_TXP
) {
896 inst
->Opcode
= OPCODE_MOV
;
897 inst
->DstReg
.WriteMask
= WRITEMASK_XYZW
;
898 inst
->SrcReg
[0].File
= PROGRAM_CONSTANT
;
899 inst
->SrcReg
[0].Index
= whiteIndex
;
900 inst
->SrcReg
[0].Swizzle
= SWIZZLE_XYZW
;
901 inst
->SrcReg
[0].Negate
= NEGATE_NONE
;
907 inst
->Opcode
= OPCODE_TEX
;