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();
101 #if FEATURE_ARB_geometry_shader4
102 ctx
->GeometryProgram
.Enabled
= GL_FALSE
;
103 /* right now by default we don't have a geometry program */
104 _mesa_reference_geomprog(ctx
, &ctx
->GeometryProgram
.Current
,
106 ctx
->GeometryProgram
.Cache
= _mesa_new_program_cache();
109 /* XXX probably move this stuff */
110 #if FEATURE_ATI_fragment_shader
111 ctx
->ATIFragmentShader
.Enabled
= GL_FALSE
;
112 ctx
->ATIFragmentShader
.Current
= ctx
->Shared
->DefaultFragmentShader
;
113 assert(ctx
->ATIFragmentShader
.Current
);
114 ctx
->ATIFragmentShader
.Current
->RefCount
++;
120 * Free a context's vertex/fragment program state
123 _mesa_free_program_data(GLcontext
*ctx
)
125 #if FEATURE_NV_vertex_program || FEATURE_ARB_vertex_program
126 _mesa_reference_vertprog(ctx
, &ctx
->VertexProgram
.Current
, NULL
);
127 _mesa_delete_program_cache(ctx
, ctx
->VertexProgram
.Cache
);
129 #if FEATURE_NV_fragment_program || FEATURE_ARB_fragment_program
130 _mesa_reference_fragprog(ctx
, &ctx
->FragmentProgram
.Current
, NULL
);
131 _mesa_delete_program_cache(ctx
, ctx
->FragmentProgram
.Cache
);
133 #if FEATURE_ARB_geometry_shader4
134 _mesa_reference_geomprog(ctx
, &ctx
->GeometryProgram
.Current
, NULL
);
135 _mesa_delete_program_cache(ctx
, ctx
->GeometryProgram
.Cache
);
137 /* XXX probably move this stuff */
138 #if FEATURE_ATI_fragment_shader
139 if (ctx
->ATIFragmentShader
.Current
) {
140 ctx
->ATIFragmentShader
.Current
->RefCount
--;
141 if (ctx
->ATIFragmentShader
.Current
->RefCount
<= 0) {
142 free(ctx
->ATIFragmentShader
.Current
);
146 free((void *) ctx
->Program
.ErrorString
);
151 * Update the default program objects in the given context to reference those
152 * specified in the shared state and release those referencing the old
156 _mesa_update_default_objects_program(GLcontext
*ctx
)
158 #if FEATURE_NV_vertex_program || FEATURE_ARB_vertex_program
159 _mesa_reference_vertprog(ctx
, &ctx
->VertexProgram
.Current
,
160 (struct gl_vertex_program
*)
161 ctx
->Shared
->DefaultVertexProgram
);
162 assert(ctx
->VertexProgram
.Current
);
165 #if FEATURE_NV_fragment_program || FEATURE_ARB_fragment_program
166 _mesa_reference_fragprog(ctx
, &ctx
->FragmentProgram
.Current
,
167 (struct gl_fragment_program
*)
168 ctx
->Shared
->DefaultFragmentProgram
);
169 assert(ctx
->FragmentProgram
.Current
);
172 #if FEATURE_ARB_geometry_shader4
173 _mesa_reference_geomprog(ctx
, &ctx
->GeometryProgram
.Current
,
174 (struct gl_geometry_program
*)
175 ctx
->Shared
->DefaultGeometryProgram
);
178 /* XXX probably move this stuff */
179 #if FEATURE_ATI_fragment_shader
180 if (ctx
->ATIFragmentShader
.Current
) {
181 ctx
->ATIFragmentShader
.Current
->RefCount
--;
182 if (ctx
->ATIFragmentShader
.Current
->RefCount
<= 0) {
183 free(ctx
->ATIFragmentShader
.Current
);
186 ctx
->ATIFragmentShader
.Current
= (struct ati_fragment_shader
*) ctx
->Shared
->DefaultFragmentShader
;
187 assert(ctx
->ATIFragmentShader
.Current
);
188 ctx
->ATIFragmentShader
.Current
->RefCount
++;
194 * Set the vertex/fragment program error state (position and error string).
195 * This is generally called from within the parsers.
198 _mesa_set_program_error(GLcontext
*ctx
, GLint pos
, const char *string
)
200 ctx
->Program
.ErrorPos
= pos
;
201 free((void *) ctx
->Program
.ErrorString
);
204 ctx
->Program
.ErrorString
= _mesa_strdup(string
);
209 * Find the line number and column for 'pos' within 'string'.
210 * Return a copy of the line which contains 'pos'. Free the line with
212 * \param string the program string
213 * \param pos the position within the string
214 * \param line returns the line number corresponding to 'pos'.
215 * \param col returns the column number corresponding to 'pos'.
216 * \return copy of the line containing 'pos'.
219 _mesa_find_line_column(const GLubyte
*string
, const GLubyte
*pos
,
220 GLint
*line
, GLint
*col
)
222 const GLubyte
*lineStart
= string
;
223 const GLubyte
*p
= string
;
230 if (*p
== (GLubyte
) '\n') {
237 *col
= (pos
- lineStart
) + 1;
239 /* return copy of this line */
240 while (*p
!= 0 && *p
!= '\n')
243 s
= (GLubyte
*) malloc(len
+ 1);
244 memcpy(s
, lineStart
, len
);
252 * Initialize a new vertex/fragment program object.
254 static struct gl_program
*
255 _mesa_init_program_struct( GLcontext
*ctx
, struct gl_program
*prog
,
256 GLenum target
, GLuint id
)
261 memset(prog
, 0, sizeof(*prog
));
263 prog
->Target
= target
;
264 prog
->Resident
= GL_TRUE
;
266 prog
->Format
= GL_PROGRAM_FORMAT_ASCII_ARB
;
268 /* default mapping from samplers to texture units */
269 for (i
= 0; i
< MAX_SAMPLERS
; i
++)
270 prog
->SamplerUnits
[i
] = i
;
278 * Initialize a new fragment program object.
281 _mesa_init_fragment_program( GLcontext
*ctx
, struct gl_fragment_program
*prog
,
282 GLenum target
, GLuint id
)
285 return _mesa_init_program_struct( ctx
, &prog
->Base
, target
, id
);
292 * Initialize a new vertex program object.
295 _mesa_init_vertex_program( GLcontext
*ctx
, struct gl_vertex_program
*prog
,
296 GLenum target
, GLuint id
)
299 return _mesa_init_program_struct( ctx
, &prog
->Base
, target
, id
);
306 * Initialize a new geometry program object.
309 _mesa_init_geometry_program( GLcontext
*ctx
, struct gl_geometry_program
*prog
,
310 GLenum target
, GLuint id
)
313 return _mesa_init_program_struct( ctx
, &prog
->Base
, target
, id
);
320 * Allocate and initialize a new fragment/vertex program object but
321 * don't put it into the program hash table. Called via
322 * ctx->Driver.NewProgram. May be overridden (ie. replaced) by a
323 * device driver function to implement OO deriviation with additional
324 * types not understood by this function.
327 * \param id program id/number
328 * \param target program target/type
329 * \return pointer to new program object
332 _mesa_new_program(GLcontext
*ctx
, GLenum target
, GLuint id
)
334 struct gl_program
*prog
;
336 case GL_VERTEX_PROGRAM_ARB
: /* == GL_VERTEX_PROGRAM_NV */
337 case GL_VERTEX_STATE_PROGRAM_NV
:
338 prog
= _mesa_init_vertex_program(ctx
, CALLOC_STRUCT(gl_vertex_program
),
341 case GL_FRAGMENT_PROGRAM_NV
:
342 case GL_FRAGMENT_PROGRAM_ARB
:
343 prog
=_mesa_init_fragment_program(ctx
,
344 CALLOC_STRUCT(gl_fragment_program
),
347 case MESA_GEOMETRY_PROGRAM
:
348 prog
= _mesa_init_geometry_program(ctx
,
349 CALLOC_STRUCT(gl_geometry_program
),
353 _mesa_problem(ctx
, "bad target in _mesa_new_program");
361 * Delete a program and remove it from the hash table, ignoring the
363 * Called via ctx->Driver.DeleteProgram. May be wrapped (OO deriviation)
364 * by a device driver function.
367 _mesa_delete_program(GLcontext
*ctx
, struct gl_program
*prog
)
371 ASSERT(prog
->RefCount
==0);
373 if (prog
== &_mesa_DummyProgram
)
379 _mesa_free_instructions(prog
->Instructions
, prog
->NumInstructions
);
381 if (prog
->Parameters
) {
382 _mesa_free_parameter_list(prog
->Parameters
);
385 _mesa_free_parameter_list(prog
->Varying
);
387 if (prog
->Attributes
) {
388 _mesa_free_parameter_list(prog
->Attributes
);
396 * Return the gl_program object for a given ID.
397 * Basically just a wrapper for _mesa_HashLookup() to avoid a lot of
401 _mesa_lookup_program(GLcontext
*ctx
, GLuint id
)
404 return (struct gl_program
*) _mesa_HashLookup(ctx
->Shared
->Programs
, id
);
411 * Reference counting for vertex/fragment programs
414 _mesa_reference_program(GLcontext
*ctx
,
415 struct gl_program
**ptr
,
416 struct gl_program
*prog
)
421 if ((*ptr
)->Target
== GL_VERTEX_PROGRAM_ARB
)
422 ASSERT(prog
->Target
== GL_VERTEX_PROGRAM_ARB
);
423 else if ((*ptr
)->Target
== GL_FRAGMENT_PROGRAM_ARB
)
424 ASSERT(prog
->Target
== GL_FRAGMENT_PROGRAM_ARB
||
425 prog
->Target
== GL_FRAGMENT_PROGRAM_NV
);
426 else if ((*ptr
)->Target
== MESA_GEOMETRY_PROGRAM
)
427 ASSERT(prog
->Target
== MESA_GEOMETRY_PROGRAM
);
430 return; /* no change */
433 GLboolean deleteFlag
;
435 /*_glthread_LOCK_MUTEX((*ptr)->Mutex);*/
437 printf("Program %p ID=%u Target=%s Refcount-- to %d\n",
439 ((*ptr
)->Target
== GL_VERTEX_PROGRAM_ARB
? "VP" :
440 ((*ptr
)->Target
== MESA_GEOMETRY_PROGRAM
? "GP" : "FP")),
441 (*ptr
)->RefCount
- 1);
443 ASSERT((*ptr
)->RefCount
> 0);
446 deleteFlag
= ((*ptr
)->RefCount
== 0);
447 /*_glthread_UNLOCK_MUTEX((*ptr)->Mutex);*/
451 ctx
->Driver
.DeleteProgram(ctx
, *ptr
);
459 /*_glthread_LOCK_MUTEX(prog->Mutex);*/
462 printf("Program %p ID=%u Target=%s Refcount++ to %d\n",
464 (prog
->Target
== GL_VERTEX_PROGRAM_ARB
? "VP" :
465 (prog
->Target
== MESA_GEOMETRY_PROGRAM
? "GP" : "FP")),
468 /*_glthread_UNLOCK_MUTEX(prog->Mutex);*/
476 * Return a copy of a program.
477 * XXX Problem here if the program object is actually OO-derivation
478 * made by a device driver.
481 _mesa_clone_program(GLcontext
*ctx
, const struct gl_program
*prog
)
483 struct gl_program
*clone
;
485 clone
= ctx
->Driver
.NewProgram(ctx
, prog
->Target
, prog
->Id
);
489 assert(clone
->Target
== prog
->Target
);
490 assert(clone
->RefCount
== 1);
492 clone
->String
= (GLubyte
*) _mesa_strdup((char *) prog
->String
);
493 clone
->Format
= prog
->Format
;
494 clone
->Instructions
= _mesa_alloc_instructions(prog
->NumInstructions
);
495 if (!clone
->Instructions
) {
496 _mesa_reference_program(ctx
, &clone
, NULL
);
499 _mesa_copy_instructions(clone
->Instructions
, prog
->Instructions
,
500 prog
->NumInstructions
);
501 clone
->InputsRead
= prog
->InputsRead
;
502 clone
->OutputsWritten
= prog
->OutputsWritten
;
503 clone
->SamplersUsed
= prog
->SamplersUsed
;
504 clone
->ShadowSamplers
= prog
->ShadowSamplers
;
505 memcpy(clone
->TexturesUsed
, prog
->TexturesUsed
, sizeof(prog
->TexturesUsed
));
507 if (prog
->Parameters
)
508 clone
->Parameters
= _mesa_clone_parameter_list(prog
->Parameters
);
509 memcpy(clone
->LocalParams
, prog
->LocalParams
, sizeof(clone
->LocalParams
));
511 clone
->Varying
= _mesa_clone_parameter_list(prog
->Varying
);
512 if (prog
->Attributes
)
513 clone
->Attributes
= _mesa_clone_parameter_list(prog
->Attributes
);
514 memcpy(clone
->LocalParams
, prog
->LocalParams
, sizeof(clone
->LocalParams
));
515 clone
->IndirectRegisterFiles
= prog
->IndirectRegisterFiles
;
516 clone
->NumInstructions
= prog
->NumInstructions
;
517 clone
->NumTemporaries
= prog
->NumTemporaries
;
518 clone
->NumParameters
= prog
->NumParameters
;
519 clone
->NumAttributes
= prog
->NumAttributes
;
520 clone
->NumAddressRegs
= prog
->NumAddressRegs
;
521 clone
->NumNativeInstructions
= prog
->NumNativeInstructions
;
522 clone
->NumNativeTemporaries
= prog
->NumNativeTemporaries
;
523 clone
->NumNativeParameters
= prog
->NumNativeParameters
;
524 clone
->NumNativeAttributes
= prog
->NumNativeAttributes
;
525 clone
->NumNativeAddressRegs
= prog
->NumNativeAddressRegs
;
526 clone
->NumAluInstructions
= prog
->NumAluInstructions
;
527 clone
->NumTexInstructions
= prog
->NumTexInstructions
;
528 clone
->NumTexIndirections
= prog
->NumTexIndirections
;
529 clone
->NumNativeAluInstructions
= prog
->NumNativeAluInstructions
;
530 clone
->NumNativeTexInstructions
= prog
->NumNativeTexInstructions
;
531 clone
->NumNativeTexIndirections
= prog
->NumNativeTexIndirections
;
533 switch (prog
->Target
) {
534 case GL_VERTEX_PROGRAM_ARB
:
536 const struct gl_vertex_program
*vp
537 = (const struct gl_vertex_program
*) prog
;
538 struct gl_vertex_program
*vpc
= (struct gl_vertex_program
*) clone
;
539 vpc
->IsPositionInvariant
= vp
->IsPositionInvariant
;
540 vpc
->IsNVProgram
= vp
->IsNVProgram
;
543 case GL_FRAGMENT_PROGRAM_ARB
:
545 const struct gl_fragment_program
*fp
546 = (const struct gl_fragment_program
*) prog
;
547 struct gl_fragment_program
*fpc
= (struct gl_fragment_program
*) clone
;
548 fpc
->FogOption
= fp
->FogOption
;
549 fpc
->UsesKill
= fp
->UsesKill
;
550 fpc
->OriginUpperLeft
= fp
->OriginUpperLeft
;
551 fpc
->PixelCenterInteger
= fp
->PixelCenterInteger
;
554 case MESA_GEOMETRY_PROGRAM
:
556 const struct gl_geometry_program
*gp
557 = (const struct gl_geometry_program
*) prog
;
558 struct gl_geometry_program
*gpc
= (struct gl_geometry_program
*) clone
;
559 gpc
->VerticesOut
= gp
->VerticesOut
;
560 gpc
->InputType
= gp
->InputType
;
561 gpc
->OutputType
= gp
->OutputType
;
565 _mesa_problem(NULL
, "Unexpected target in _mesa_clone_program");
573 * Insert 'count' NOP instructions at 'start' in the given program.
574 * Adjust branch targets accordingly.
577 _mesa_insert_instructions(struct gl_program
*prog
, GLuint start
, GLuint count
)
579 const GLuint origLen
= prog
->NumInstructions
;
580 const GLuint newLen
= origLen
+ count
;
581 struct prog_instruction
*newInst
;
584 /* adjust branches */
585 for (i
= 0; i
< prog
->NumInstructions
; i
++) {
586 struct prog_instruction
*inst
= prog
->Instructions
+ i
;
587 if (inst
->BranchTarget
> 0) {
588 if ((GLuint
)inst
->BranchTarget
>= start
) {
589 inst
->BranchTarget
+= count
;
594 /* Alloc storage for new instructions */
595 newInst
= _mesa_alloc_instructions(newLen
);
600 /* Copy 'start' instructions into new instruction buffer */
601 _mesa_copy_instructions(newInst
, prog
->Instructions
, start
);
603 /* init the new instructions */
604 _mesa_init_instructions(newInst
+ start
, count
);
606 /* Copy the remaining/tail instructions to new inst buffer */
607 _mesa_copy_instructions(newInst
+ start
+ count
,
608 prog
->Instructions
+ start
,
611 /* free old instructions */
612 _mesa_free_instructions(prog
->Instructions
, origLen
);
614 /* install new instructions */
615 prog
->Instructions
= newInst
;
616 prog
->NumInstructions
= newLen
;
622 * Delete 'count' instructions at 'start' in the given program.
623 * Adjust branch targets accordingly.
626 _mesa_delete_instructions(struct gl_program
*prog
, GLuint start
, GLuint count
)
628 const GLuint origLen
= prog
->NumInstructions
;
629 const GLuint newLen
= origLen
- count
;
630 struct prog_instruction
*newInst
;
633 /* adjust branches */
634 for (i
= 0; i
< prog
->NumInstructions
; i
++) {
635 struct prog_instruction
*inst
= prog
->Instructions
+ i
;
636 if (inst
->BranchTarget
> 0) {
637 if (inst
->BranchTarget
> (GLint
) start
) {
638 inst
->BranchTarget
-= count
;
643 /* Alloc storage for new instructions */
644 newInst
= _mesa_alloc_instructions(newLen
);
649 /* Copy 'start' instructions into new instruction buffer */
650 _mesa_copy_instructions(newInst
, prog
->Instructions
, start
);
652 /* Copy the remaining/tail instructions to new inst buffer */
653 _mesa_copy_instructions(newInst
+ start
,
654 prog
->Instructions
+ start
+ count
,
657 /* free old instructions */
658 _mesa_free_instructions(prog
->Instructions
, origLen
);
660 /* install new instructions */
661 prog
->Instructions
= newInst
;
662 prog
->NumInstructions
= newLen
;
669 * Search instructions for registers that match (oldFile, oldIndex),
670 * replacing them with (newFile, newIndex).
673 replace_registers(struct prog_instruction
*inst
, GLuint numInst
,
674 GLuint oldFile
, GLuint oldIndex
,
675 GLuint newFile
, GLuint newIndex
)
678 for (i
= 0; i
< numInst
; i
++) {
680 for (j
= 0; j
< _mesa_num_inst_src_regs(inst
[i
].Opcode
); j
++) {
681 if (inst
[i
].SrcReg
[j
].File
== oldFile
&&
682 inst
[i
].SrcReg
[j
].Index
== oldIndex
) {
683 inst
[i
].SrcReg
[j
].File
= newFile
;
684 inst
[i
].SrcReg
[j
].Index
= newIndex
;
688 if (inst
[i
].DstReg
.File
== oldFile
&& inst
[i
].DstReg
.Index
== oldIndex
) {
689 inst
[i
].DstReg
.File
= newFile
;
690 inst
[i
].DstReg
.Index
= newIndex
;
697 * Search instructions for references to program parameters. When found,
698 * increment the parameter index by 'offset'.
699 * Used when combining programs.
702 adjust_param_indexes(struct prog_instruction
*inst
, GLuint numInst
,
706 for (i
= 0; i
< numInst
; i
++) {
707 for (j
= 0; j
< _mesa_num_inst_src_regs(inst
[i
].Opcode
); j
++) {
708 GLuint f
= inst
[i
].SrcReg
[j
].File
;
709 if (f
== PROGRAM_CONSTANT
||
710 f
== PROGRAM_UNIFORM
||
711 f
== PROGRAM_STATE_VAR
) {
712 inst
[i
].SrcReg
[j
].Index
+= offset
;
720 * Combine two programs into one. Fix instructions so the outputs of
721 * the first program go to the inputs of the second program.
724 _mesa_combine_programs(GLcontext
*ctx
,
725 const struct gl_program
*progA
,
726 const struct gl_program
*progB
)
728 struct prog_instruction
*newInst
;
729 struct gl_program
*newProg
;
730 const GLuint lenA
= progA
->NumInstructions
- 1; /* omit END instr */
731 const GLuint lenB
= progB
->NumInstructions
;
732 const GLuint numParamsA
= _mesa_num_parameters(progA
->Parameters
);
733 const GLuint newLength
= lenA
+ lenB
;
734 GLboolean usedTemps
[MAX_PROGRAM_TEMPS
];
735 GLuint firstTemp
= 0;
739 ASSERT(progA
->Target
== progB
->Target
);
741 newInst
= _mesa_alloc_instructions(newLength
);
745 _mesa_copy_instructions(newInst
, progA
->Instructions
, lenA
);
746 _mesa_copy_instructions(newInst
+ lenA
, progB
->Instructions
, lenB
);
748 /* adjust branch / instruction addresses for B's instructions */
749 for (i
= 0; i
< lenB
; i
++) {
750 newInst
[lenA
+ i
].BranchTarget
+= lenA
;
753 newProg
= ctx
->Driver
.NewProgram(ctx
, progA
->Target
, 0);
754 newProg
->Instructions
= newInst
;
755 newProg
->NumInstructions
= newLength
;
757 /* find used temp regs (we may need new temps below) */
758 _mesa_find_used_registers(newProg
, PROGRAM_TEMPORARY
,
759 usedTemps
, MAX_PROGRAM_TEMPS
);
761 if (newProg
->Target
== GL_FRAGMENT_PROGRAM_ARB
) {
762 struct gl_fragment_program
*fprogA
, *fprogB
, *newFprog
;
763 GLbitfield progB_inputsRead
= progB
->InputsRead
;
764 GLint progB_colorFile
, progB_colorIndex
;
766 fprogA
= (struct gl_fragment_program
*) progA
;
767 fprogB
= (struct gl_fragment_program
*) progB
;
768 newFprog
= (struct gl_fragment_program
*) newProg
;
770 newFprog
->UsesKill
= fprogA
->UsesKill
|| fprogB
->UsesKill
;
772 /* We'll do a search and replace for instances
773 * of progB_colorFile/progB_colorIndex below...
775 progB_colorFile
= PROGRAM_INPUT
;
776 progB_colorIndex
= FRAG_ATTRIB_COL0
;
779 * The fragment program may get color from a state var rather than
780 * a fragment input (vertex output) if it's constant.
781 * See the texenvprogram.c code.
782 * So, search the program's parameter list now to see if the program
783 * gets color from a state var instead of a conventional fragment
786 for (i
= 0; i
< progB
->Parameters
->NumParameters
; i
++) {
787 struct gl_program_parameter
*p
= &progB
->Parameters
->Parameters
[i
];
788 if (p
->Type
== PROGRAM_STATE_VAR
&&
789 p
->StateIndexes
[0] == STATE_INTERNAL
&&
790 p
->StateIndexes
[1] == STATE_CURRENT_ATTRIB
&&
791 p
->StateIndexes
[2] == VERT_ATTRIB_COLOR0
) {
792 progB_inputsRead
|= FRAG_BIT_COL0
;
793 progB_colorFile
= PROGRAM_STATE_VAR
;
794 progB_colorIndex
= i
;
799 /* Connect color outputs of fprogA to color inputs of fprogB, via a
800 * new temporary register.
802 if ((progA
->OutputsWritten
& (1 << FRAG_RESULT_COLOR
)) &&
803 (progB_inputsRead
& FRAG_BIT_COL0
)) {
804 GLint tempReg
= _mesa_find_free_register(usedTemps
, MAX_PROGRAM_TEMPS
,
807 _mesa_problem(ctx
, "No free temp regs found in "
808 "_mesa_combine_programs(), using 31");
811 firstTemp
= tempReg
+ 1;
813 /* replace writes to result.color[0] with tempReg */
814 replace_registers(newInst
, lenA
,
815 PROGRAM_OUTPUT
, FRAG_RESULT_COLOR
,
816 PROGRAM_TEMPORARY
, tempReg
);
817 /* replace reads from the input color with tempReg */
818 replace_registers(newInst
+ lenA
, lenB
,
819 progB_colorFile
, progB_colorIndex
, /* search for */
820 PROGRAM_TEMPORARY
, tempReg
/* replace with */ );
823 /* compute combined program's InputsRead */
824 inputsB
= progB_inputsRead
;
825 if (progA
->OutputsWritten
& (1 << FRAG_RESULT_COLOR
)) {
826 inputsB
&= ~(1 << FRAG_ATTRIB_COL0
);
828 newProg
->InputsRead
= progA
->InputsRead
| inputsB
;
829 newProg
->OutputsWritten
= progB
->OutputsWritten
;
830 newProg
->SamplersUsed
= progA
->SamplersUsed
| progB
->SamplersUsed
;
834 assert(0); /* XXX todo */
838 * Merge parameters (uniforms, constants, etc)
840 newProg
->Parameters
= _mesa_combine_parameter_lists(progA
->Parameters
,
843 adjust_param_indexes(newInst
+ lenA
, lenB
, numParamsA
);
851 * Populate the 'used' array with flags indicating which registers (TEMPs,
852 * INPUTs, OUTPUTs, etc, are used by the given program.
853 * \param file type of register to scan for
854 * \param used returns true/false flags for in use / free
855 * \param usedSize size of the 'used' array
858 _mesa_find_used_registers(const struct gl_program
*prog
,
859 gl_register_file file
,
860 GLboolean used
[], GLuint usedSize
)
864 memset(used
, 0, usedSize
);
866 for (i
= 0; i
< prog
->NumInstructions
; i
++) {
867 const struct prog_instruction
*inst
= prog
->Instructions
+ i
;
868 const GLuint n
= _mesa_num_inst_src_regs(inst
->Opcode
);
870 if (inst
->DstReg
.File
== file
) {
871 used
[inst
->DstReg
.Index
] = GL_TRUE
;
874 for (j
= 0; j
< n
; j
++) {
875 if (inst
->SrcReg
[j
].File
== file
) {
876 used
[inst
->SrcReg
[j
].Index
] = GL_TRUE
;
884 * Scan the given 'used' register flag array for the first entry
885 * that's >= firstReg.
886 * \param used vector of flags indicating registers in use (as returned
887 * by _mesa_find_used_registers())
888 * \param usedSize size of the 'used' array
889 * \param firstReg first register to start searching at
890 * \return index of unused register, or -1 if none.
893 _mesa_find_free_register(const GLboolean used
[],
894 GLuint usedSize
, GLuint firstReg
)
898 assert(firstReg
< usedSize
);
900 for (i
= firstReg
; i
< usedSize
; i
++)
909 * "Post-process" a GPU program. This is intended to be used for debugging.
910 * Example actions include no-op'ing instructions or changing instruction
914 _mesa_postprocess_program(GLcontext
*ctx
, struct gl_program
*prog
)
916 static const GLfloat white
[4] = { 0.5, 0.5, 0.5, 0.5 };
919 GLint whiteIndex
= _mesa_add_unnamed_constant(prog
->Parameters
,
920 white
, 4, &whiteSwizzle
);
924 for (i
= 0; i
< prog
->NumInstructions
; i
++) {
925 struct prog_instruction
*inst
= prog
->Instructions
+ i
;
926 const GLuint n
= _mesa_num_inst_src_regs(inst
->Opcode
);
930 if (_mesa_is_tex_instruction(inst
->Opcode
)) {
932 /* replace TEX/TXP/TXB with MOV */
933 inst
->Opcode
= OPCODE_MOV
;
934 inst
->DstReg
.WriteMask
= WRITEMASK_XYZW
;
935 inst
->SrcReg
[0].Swizzle
= SWIZZLE_XYZW
;
936 inst
->SrcReg
[0].Negate
= NEGATE_NONE
;
940 /* disable shadow texture mode */
945 if (inst
->Opcode
== OPCODE_TXP
) {
947 inst
->Opcode
= OPCODE_MOV
;
948 inst
->DstReg
.WriteMask
= WRITEMASK_XYZW
;
949 inst
->SrcReg
[0].File
= PROGRAM_CONSTANT
;
950 inst
->SrcReg
[0].Index
= whiteIndex
;
951 inst
->SrcReg
[0].Swizzle
= SWIZZLE_XYZW
;
952 inst
->SrcReg
[0].Negate
= NEGATE_NONE
;
958 inst
->Opcode
= OPCODE_TEX
;