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 * THE AUTHORS 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(struct gl_context
*ctx
)
55 * If this assertion fails, we need to increase the field
56 * size for register indexes (see INST_INDEX_BITS).
58 ASSERT(ctx
->Const
.VertexProgram
.MaxUniformComponents
/ 4
59 <= (1 << INST_INDEX_BITS
));
60 ASSERT(ctx
->Const
.FragmentProgram
.MaxUniformComponents
/ 4
61 <= (1 << INST_INDEX_BITS
));
63 ASSERT(ctx
->Const
.VertexProgram
.MaxTemps
<= (1 << INST_INDEX_BITS
));
64 ASSERT(ctx
->Const
.VertexProgram
.MaxLocalParams
<= (1 << INST_INDEX_BITS
));
65 ASSERT(ctx
->Const
.FragmentProgram
.MaxTemps
<= (1 << INST_INDEX_BITS
));
66 ASSERT(ctx
->Const
.FragmentProgram
.MaxLocalParams
<= (1 << INST_INDEX_BITS
));
68 ASSERT(ctx
->Const
.VertexProgram
.MaxUniformComponents
<= 4 * MAX_UNIFORMS
);
69 ASSERT(ctx
->Const
.FragmentProgram
.MaxUniformComponents
<= 4 * MAX_UNIFORMS
);
71 ASSERT(ctx
->Const
.VertexProgram
.MaxAddressOffset
<= (1 << INST_INDEX_BITS
));
72 ASSERT(ctx
->Const
.FragmentProgram
.MaxAddressOffset
<= (1 << INST_INDEX_BITS
));
74 /* If this fails, increase prog_instruction::TexSrcUnit size */
75 ASSERT(MAX_TEXTURE_UNITS
<= (1 << 5));
77 /* If this fails, increase prog_instruction::TexSrcTarget size */
78 ASSERT(NUM_TEXTURE_TARGETS
<= (1 << 4));
80 ctx
->Program
.ErrorPos
= -1;
81 ctx
->Program
.ErrorString
= _mesa_strdup("");
83 ctx
->VertexProgram
.Enabled
= GL_FALSE
;
84 ctx
->VertexProgram
.PointSizeEnabled
=
85 (ctx
->API
== API_OPENGLES2
) ? GL_TRUE
: GL_FALSE
;
86 ctx
->VertexProgram
.TwoSideEnabled
= GL_FALSE
;
87 _mesa_reference_vertprog(ctx
, &ctx
->VertexProgram
.Current
,
88 ctx
->Shared
->DefaultVertexProgram
);
89 assert(ctx
->VertexProgram
.Current
);
90 ctx
->VertexProgram
.Cache
= _mesa_new_program_cache();
92 ctx
->FragmentProgram
.Enabled
= GL_FALSE
;
93 _mesa_reference_fragprog(ctx
, &ctx
->FragmentProgram
.Current
,
94 ctx
->Shared
->DefaultFragmentProgram
);
95 assert(ctx
->FragmentProgram
.Current
);
96 ctx
->FragmentProgram
.Cache
= _mesa_new_program_cache();
98 ctx
->GeometryProgram
.Enabled
= GL_FALSE
;
99 /* right now by default we don't have a geometry program */
100 _mesa_reference_geomprog(ctx
, &ctx
->GeometryProgram
.Current
,
102 ctx
->GeometryProgram
.Cache
= _mesa_new_program_cache();
104 /* XXX probably move this stuff */
105 ctx
->ATIFragmentShader
.Enabled
= GL_FALSE
;
106 ctx
->ATIFragmentShader
.Current
= ctx
->Shared
->DefaultFragmentShader
;
107 assert(ctx
->ATIFragmentShader
.Current
);
108 ctx
->ATIFragmentShader
.Current
->RefCount
++;
113 * Free a context's vertex/fragment program state
116 _mesa_free_program_data(struct gl_context
*ctx
)
118 _mesa_reference_vertprog(ctx
, &ctx
->VertexProgram
.Current
, NULL
);
119 _mesa_delete_program_cache(ctx
, ctx
->VertexProgram
.Cache
);
120 _mesa_reference_fragprog(ctx
, &ctx
->FragmentProgram
.Current
, NULL
);
121 _mesa_delete_shader_cache(ctx
, ctx
->FragmentProgram
.Cache
);
122 _mesa_reference_geomprog(ctx
, &ctx
->GeometryProgram
.Current
, NULL
);
123 _mesa_delete_program_cache(ctx
, ctx
->GeometryProgram
.Cache
);
125 /* XXX probably move this stuff */
126 if (ctx
->ATIFragmentShader
.Current
) {
127 ctx
->ATIFragmentShader
.Current
->RefCount
--;
128 if (ctx
->ATIFragmentShader
.Current
->RefCount
<= 0) {
129 free(ctx
->ATIFragmentShader
.Current
);
133 free((void *) ctx
->Program
.ErrorString
);
138 * Update the default program objects in the given context to reference those
139 * specified in the shared state and release those referencing the old
143 _mesa_update_default_objects_program(struct gl_context
*ctx
)
145 _mesa_reference_vertprog(ctx
, &ctx
->VertexProgram
.Current
,
146 ctx
->Shared
->DefaultVertexProgram
);
147 assert(ctx
->VertexProgram
.Current
);
149 _mesa_reference_fragprog(ctx
, &ctx
->FragmentProgram
.Current
,
150 ctx
->Shared
->DefaultFragmentProgram
);
151 assert(ctx
->FragmentProgram
.Current
);
153 _mesa_reference_geomprog(ctx
, &ctx
->GeometryProgram
.Current
,
154 ctx
->Shared
->DefaultGeometryProgram
);
156 /* XXX probably move this stuff */
157 if (ctx
->ATIFragmentShader
.Current
) {
158 ctx
->ATIFragmentShader
.Current
->RefCount
--;
159 if (ctx
->ATIFragmentShader
.Current
->RefCount
<= 0) {
160 free(ctx
->ATIFragmentShader
.Current
);
163 ctx
->ATIFragmentShader
.Current
= (struct ati_fragment_shader
*) ctx
->Shared
->DefaultFragmentShader
;
164 assert(ctx
->ATIFragmentShader
.Current
);
165 ctx
->ATIFragmentShader
.Current
->RefCount
++;
170 * Set the vertex/fragment program error state (position and error string).
171 * This is generally called from within the parsers.
174 _mesa_set_program_error(struct gl_context
*ctx
, GLint pos
, const char *string
)
176 ctx
->Program
.ErrorPos
= pos
;
177 free((void *) ctx
->Program
.ErrorString
);
180 ctx
->Program
.ErrorString
= _mesa_strdup(string
);
185 * Find the line number and column for 'pos' within 'string'.
186 * Return a copy of the line which contains 'pos'. Free the line with
188 * \param string the program string
189 * \param pos the position within the string
190 * \param line returns the line number corresponding to 'pos'.
191 * \param col returns the column number corresponding to 'pos'.
192 * \return copy of the line containing 'pos'.
195 _mesa_find_line_column(const GLubyte
*string
, const GLubyte
*pos
,
196 GLint
*line
, GLint
*col
)
198 const GLubyte
*lineStart
= string
;
199 const GLubyte
*p
= string
;
206 if (*p
== (GLubyte
) '\n') {
213 *col
= (pos
- lineStart
) + 1;
215 /* return copy of this line */
216 while (*p
!= 0 && *p
!= '\n')
220 memcpy(s
, lineStart
, len
);
228 * Initialize a new vertex/fragment program object.
230 static struct gl_program
*
231 _mesa_init_program_struct( struct gl_context
*ctx
, struct gl_program
*prog
,
232 GLenum target
, GLuint id
)
237 memset(prog
, 0, sizeof(*prog
));
239 prog
->Target
= target
;
241 prog
->Format
= GL_PROGRAM_FORMAT_ASCII_ARB
;
243 /* default mapping from samplers to texture units */
244 for (i
= 0; i
< MAX_SAMPLERS
; i
++)
245 prog
->SamplerUnits
[i
] = i
;
253 * Initialize a new fragment program object.
256 _mesa_init_fragment_program( struct gl_context
*ctx
, struct gl_fragment_program
*prog
,
257 GLenum target
, GLuint id
)
260 return _mesa_init_program_struct( ctx
, &prog
->Base
, target
, id
);
267 * Initialize a new vertex program object.
270 _mesa_init_vertex_program( struct gl_context
*ctx
, struct gl_vertex_program
*prog
,
271 GLenum target
, GLuint id
)
274 return _mesa_init_program_struct( ctx
, &prog
->Base
, target
, id
);
281 * Initialize a new geometry program object.
284 _mesa_init_geometry_program( struct gl_context
*ctx
, struct gl_geometry_program
*prog
,
285 GLenum target
, GLuint id
)
288 return _mesa_init_program_struct( ctx
, &prog
->Base
, target
, id
);
295 * Allocate and initialize a new fragment/vertex program object but
296 * don't put it into the program hash table. Called via
297 * ctx->Driver.NewProgram. May be overridden (ie. replaced) by a
298 * device driver function to implement OO deriviation with additional
299 * types not understood by this function.
302 * \param id program id/number
303 * \param target program target/type
304 * \return pointer to new program object
307 _mesa_new_program(struct gl_context
*ctx
, GLenum target
, GLuint id
)
309 struct gl_program
*prog
;
311 case GL_VERTEX_PROGRAM_ARB
: /* == GL_VERTEX_PROGRAM_NV */
312 prog
= _mesa_init_vertex_program(ctx
, CALLOC_STRUCT(gl_vertex_program
),
315 case GL_FRAGMENT_PROGRAM_NV
:
316 case GL_FRAGMENT_PROGRAM_ARB
:
317 prog
=_mesa_init_fragment_program(ctx
,
318 CALLOC_STRUCT(gl_fragment_program
),
321 case MESA_GEOMETRY_PROGRAM
:
322 prog
= _mesa_init_geometry_program(ctx
,
323 CALLOC_STRUCT(gl_geometry_program
),
327 _mesa_problem(ctx
, "bad target in _mesa_new_program");
335 * Delete a program and remove it from the hash table, ignoring the
337 * Called via ctx->Driver.DeleteProgram. May be wrapped (OO deriviation)
338 * by a device driver function.
341 _mesa_delete_program(struct gl_context
*ctx
, struct gl_program
*prog
)
345 ASSERT(prog
->RefCount
==0);
347 if (prog
== &_mesa_DummyProgram
)
352 if (prog
->Instructions
) {
353 _mesa_free_instructions(prog
->Instructions
, prog
->NumInstructions
);
355 if (prog
->Parameters
) {
356 _mesa_free_parameter_list(prog
->Parameters
);
364 * Return the gl_program object for a given ID.
365 * Basically just a wrapper for _mesa_HashLookup() to avoid a lot of
369 _mesa_lookup_program(struct gl_context
*ctx
, GLuint id
)
372 return (struct gl_program
*) _mesa_HashLookup(ctx
->Shared
->Programs
, id
);
379 * Reference counting for vertex/fragment programs
380 * This is normally only called from the _mesa_reference_program() macro
381 * when there's a real pointer change.
384 _mesa_reference_program_(struct gl_context
*ctx
,
385 struct gl_program
**ptr
,
386 struct gl_program
*prog
)
392 if ((*ptr
)->Target
== GL_VERTEX_PROGRAM_ARB
)
393 ASSERT(prog
->Target
== GL_VERTEX_PROGRAM_ARB
);
394 else if ((*ptr
)->Target
== GL_FRAGMENT_PROGRAM_ARB
)
395 ASSERT(prog
->Target
== GL_FRAGMENT_PROGRAM_ARB
||
396 prog
->Target
== GL_FRAGMENT_PROGRAM_NV
);
397 else if ((*ptr
)->Target
== MESA_GEOMETRY_PROGRAM
)
398 ASSERT(prog
->Target
== MESA_GEOMETRY_PROGRAM
);
403 GLboolean deleteFlag
;
405 /*_glthread_LOCK_MUTEX((*ptr)->Mutex);*/
407 printf("Program %p ID=%u Target=%s Refcount-- to %d\n",
409 ((*ptr
)->Target
== GL_VERTEX_PROGRAM_ARB
? "VP" :
410 ((*ptr
)->Target
== MESA_GEOMETRY_PROGRAM
? "GP" : "FP")),
411 (*ptr
)->RefCount
- 1);
413 ASSERT((*ptr
)->RefCount
> 0);
416 deleteFlag
= ((*ptr
)->RefCount
== 0);
417 /*_glthread_UNLOCK_MUTEX((*ptr)->Mutex);*/
421 ctx
->Driver
.DeleteProgram(ctx
, *ptr
);
429 /*_glthread_LOCK_MUTEX(prog->Mutex);*/
432 printf("Program %p ID=%u Target=%s Refcount++ to %d\n",
434 (prog
->Target
== GL_VERTEX_PROGRAM_ARB
? "VP" :
435 (prog
->Target
== MESA_GEOMETRY_PROGRAM
? "GP" : "FP")),
438 /*_glthread_UNLOCK_MUTEX(prog->Mutex);*/
446 * Return a copy of a program.
447 * XXX Problem here if the program object is actually OO-derivation
448 * made by a device driver.
451 _mesa_clone_program(struct gl_context
*ctx
, const struct gl_program
*prog
)
453 struct gl_program
*clone
;
455 clone
= ctx
->Driver
.NewProgram(ctx
, prog
->Target
, prog
->Id
);
459 assert(clone
->Target
== prog
->Target
);
460 assert(clone
->RefCount
== 1);
462 clone
->String
= (GLubyte
*) _mesa_strdup((char *) prog
->String
);
463 clone
->Format
= prog
->Format
;
464 clone
->Instructions
= _mesa_alloc_instructions(prog
->NumInstructions
);
465 if (!clone
->Instructions
) {
466 _mesa_reference_program(ctx
, &clone
, NULL
);
469 _mesa_copy_instructions(clone
->Instructions
, prog
->Instructions
,
470 prog
->NumInstructions
);
471 clone
->InputsRead
= prog
->InputsRead
;
472 clone
->OutputsWritten
= prog
->OutputsWritten
;
473 clone
->SamplersUsed
= prog
->SamplersUsed
;
474 clone
->ShadowSamplers
= prog
->ShadowSamplers
;
475 memcpy(clone
->TexturesUsed
, prog
->TexturesUsed
, sizeof(prog
->TexturesUsed
));
477 if (prog
->Parameters
)
478 clone
->Parameters
= _mesa_clone_parameter_list(prog
->Parameters
);
479 memcpy(clone
->LocalParams
, prog
->LocalParams
, sizeof(clone
->LocalParams
));
480 memcpy(clone
->LocalParams
, prog
->LocalParams
, sizeof(clone
->LocalParams
));
481 clone
->IndirectRegisterFiles
= prog
->IndirectRegisterFiles
;
482 clone
->NumInstructions
= prog
->NumInstructions
;
483 clone
->NumTemporaries
= prog
->NumTemporaries
;
484 clone
->NumParameters
= prog
->NumParameters
;
485 clone
->NumAttributes
= prog
->NumAttributes
;
486 clone
->NumAddressRegs
= prog
->NumAddressRegs
;
487 clone
->NumNativeInstructions
= prog
->NumNativeInstructions
;
488 clone
->NumNativeTemporaries
= prog
->NumNativeTemporaries
;
489 clone
->NumNativeParameters
= prog
->NumNativeParameters
;
490 clone
->NumNativeAttributes
= prog
->NumNativeAttributes
;
491 clone
->NumNativeAddressRegs
= prog
->NumNativeAddressRegs
;
492 clone
->NumAluInstructions
= prog
->NumAluInstructions
;
493 clone
->NumTexInstructions
= prog
->NumTexInstructions
;
494 clone
->NumTexIndirections
= prog
->NumTexIndirections
;
495 clone
->NumNativeAluInstructions
= prog
->NumNativeAluInstructions
;
496 clone
->NumNativeTexInstructions
= prog
->NumNativeTexInstructions
;
497 clone
->NumNativeTexIndirections
= prog
->NumNativeTexIndirections
;
499 switch (prog
->Target
) {
500 case GL_VERTEX_PROGRAM_ARB
:
502 const struct gl_vertex_program
*vp
= gl_vertex_program_const(prog
);
503 struct gl_vertex_program
*vpc
= gl_vertex_program(clone
);
504 vpc
->IsPositionInvariant
= vp
->IsPositionInvariant
;
507 case GL_FRAGMENT_PROGRAM_ARB
:
509 const struct gl_fragment_program
*fp
= gl_fragment_program_const(prog
);
510 struct gl_fragment_program
*fpc
= gl_fragment_program(clone
);
511 fpc
->UsesKill
= fp
->UsesKill
;
512 fpc
->UsesDFdy
= fp
->UsesDFdy
;
513 fpc
->OriginUpperLeft
= fp
->OriginUpperLeft
;
514 fpc
->PixelCenterInteger
= fp
->PixelCenterInteger
;
517 case MESA_GEOMETRY_PROGRAM
:
519 const struct gl_geometry_program
*gp
= gl_geometry_program_const(prog
);
520 struct gl_geometry_program
*gpc
= gl_geometry_program(clone
);
521 gpc
->VerticesOut
= gp
->VerticesOut
;
522 gpc
->InputType
= gp
->InputType
;
523 gpc
->OutputType
= gp
->OutputType
;
527 _mesa_problem(NULL
, "Unexpected target in _mesa_clone_program");
535 * Insert 'count' NOP instructions at 'start' in the given program.
536 * Adjust branch targets accordingly.
539 _mesa_insert_instructions(struct gl_program
*prog
, GLuint start
, GLuint count
)
541 const GLuint origLen
= prog
->NumInstructions
;
542 const GLuint newLen
= origLen
+ count
;
543 struct prog_instruction
*newInst
;
546 /* adjust branches */
547 for (i
= 0; i
< prog
->NumInstructions
; i
++) {
548 struct prog_instruction
*inst
= prog
->Instructions
+ i
;
549 if (inst
->BranchTarget
> 0) {
550 if ((GLuint
)inst
->BranchTarget
>= start
) {
551 inst
->BranchTarget
+= count
;
556 /* Alloc storage for new instructions */
557 newInst
= _mesa_alloc_instructions(newLen
);
562 /* Copy 'start' instructions into new instruction buffer */
563 _mesa_copy_instructions(newInst
, prog
->Instructions
, start
);
565 /* init the new instructions */
566 _mesa_init_instructions(newInst
+ start
, count
);
568 /* Copy the remaining/tail instructions to new inst buffer */
569 _mesa_copy_instructions(newInst
+ start
+ count
,
570 prog
->Instructions
+ start
,
573 /* free old instructions */
574 _mesa_free_instructions(prog
->Instructions
, origLen
);
576 /* install new instructions */
577 prog
->Instructions
= newInst
;
578 prog
->NumInstructions
= newLen
;
584 * Delete 'count' instructions at 'start' in the given program.
585 * Adjust branch targets accordingly.
588 _mesa_delete_instructions(struct gl_program
*prog
, GLuint start
, GLuint count
)
590 const GLuint origLen
= prog
->NumInstructions
;
591 const GLuint newLen
= origLen
- count
;
592 struct prog_instruction
*newInst
;
595 /* adjust branches */
596 for (i
= 0; i
< prog
->NumInstructions
; i
++) {
597 struct prog_instruction
*inst
= prog
->Instructions
+ i
;
598 if (inst
->BranchTarget
> 0) {
599 if (inst
->BranchTarget
> (GLint
) start
) {
600 inst
->BranchTarget
-= count
;
605 /* Alloc storage for new instructions */
606 newInst
= _mesa_alloc_instructions(newLen
);
611 /* Copy 'start' instructions into new instruction buffer */
612 _mesa_copy_instructions(newInst
, prog
->Instructions
, start
);
614 /* Copy the remaining/tail instructions to new inst buffer */
615 _mesa_copy_instructions(newInst
+ start
,
616 prog
->Instructions
+ start
+ count
,
619 /* free old instructions */
620 _mesa_free_instructions(prog
->Instructions
, origLen
);
622 /* install new instructions */
623 prog
->Instructions
= newInst
;
624 prog
->NumInstructions
= newLen
;
631 * Search instructions for registers that match (oldFile, oldIndex),
632 * replacing them with (newFile, newIndex).
635 replace_registers(struct prog_instruction
*inst
, GLuint numInst
,
636 GLuint oldFile
, GLuint oldIndex
,
637 GLuint newFile
, GLuint newIndex
)
640 for (i
= 0; i
< numInst
; i
++) {
642 for (j
= 0; j
< _mesa_num_inst_src_regs(inst
[i
].Opcode
); j
++) {
643 if (inst
[i
].SrcReg
[j
].File
== oldFile
&&
644 inst
[i
].SrcReg
[j
].Index
== oldIndex
) {
645 inst
[i
].SrcReg
[j
].File
= newFile
;
646 inst
[i
].SrcReg
[j
].Index
= newIndex
;
650 if (inst
[i
].DstReg
.File
== oldFile
&& inst
[i
].DstReg
.Index
== oldIndex
) {
651 inst
[i
].DstReg
.File
= newFile
;
652 inst
[i
].DstReg
.Index
= newIndex
;
659 * Search instructions for references to program parameters. When found,
660 * increment the parameter index by 'offset'.
661 * Used when combining programs.
664 adjust_param_indexes(struct prog_instruction
*inst
, GLuint numInst
,
668 for (i
= 0; i
< numInst
; i
++) {
669 for (j
= 0; j
< _mesa_num_inst_src_regs(inst
[i
].Opcode
); j
++) {
670 GLuint f
= inst
[i
].SrcReg
[j
].File
;
671 if (f
== PROGRAM_CONSTANT
||
672 f
== PROGRAM_UNIFORM
||
673 f
== PROGRAM_STATE_VAR
) {
674 inst
[i
].SrcReg
[j
].Index
+= offset
;
682 * Combine two programs into one. Fix instructions so the outputs of
683 * the first program go to the inputs of the second program.
686 _mesa_combine_programs(struct gl_context
*ctx
,
687 const struct gl_program
*progA
,
688 const struct gl_program
*progB
)
690 struct prog_instruction
*newInst
;
691 struct gl_program
*newProg
;
692 const GLuint lenA
= progA
->NumInstructions
- 1; /* omit END instr */
693 const GLuint lenB
= progB
->NumInstructions
;
694 const GLuint numParamsA
= _mesa_num_parameters(progA
->Parameters
);
695 const GLuint newLength
= lenA
+ lenB
;
696 GLboolean usedTemps
[MAX_PROGRAM_TEMPS
];
697 GLuint firstTemp
= 0;
698 GLbitfield64 inputsB
;
701 ASSERT(progA
->Target
== progB
->Target
);
703 newInst
= _mesa_alloc_instructions(newLength
);
707 _mesa_copy_instructions(newInst
, progA
->Instructions
, lenA
);
708 _mesa_copy_instructions(newInst
+ lenA
, progB
->Instructions
, lenB
);
710 /* adjust branch / instruction addresses for B's instructions */
711 for (i
= 0; i
< lenB
; i
++) {
712 newInst
[lenA
+ i
].BranchTarget
+= lenA
;
715 newProg
= ctx
->Driver
.NewProgram(ctx
, progA
->Target
, 0);
716 newProg
->Instructions
= newInst
;
717 newProg
->NumInstructions
= newLength
;
719 /* find used temp regs (we may need new temps below) */
720 _mesa_find_used_registers(newProg
, PROGRAM_TEMPORARY
,
721 usedTemps
, MAX_PROGRAM_TEMPS
);
723 if (newProg
->Target
== GL_FRAGMENT_PROGRAM_ARB
) {
724 const struct gl_fragment_program
*fprogA
, *fprogB
;
725 struct gl_fragment_program
*newFprog
;
726 GLbitfield64 progB_inputsRead
= progB
->InputsRead
;
727 GLint progB_colorFile
, progB_colorIndex
;
729 fprogA
= gl_fragment_program_const(progA
);
730 fprogB
= gl_fragment_program_const(progB
);
731 newFprog
= gl_fragment_program(newProg
);
733 newFprog
->UsesKill
= fprogA
->UsesKill
|| fprogB
->UsesKill
;
734 newFprog
->UsesDFdy
= fprogA
->UsesDFdy
|| fprogB
->UsesDFdy
;
736 /* We'll do a search and replace for instances
737 * of progB_colorFile/progB_colorIndex below...
739 progB_colorFile
= PROGRAM_INPUT
;
740 progB_colorIndex
= VARYING_SLOT_COL0
;
743 * The fragment program may get color from a state var rather than
744 * a fragment input (vertex output) if it's constant.
745 * See the texenvprogram.c code.
746 * So, search the program's parameter list now to see if the program
747 * gets color from a state var instead of a conventional fragment
750 for (i
= 0; i
< progB
->Parameters
->NumParameters
; i
++) {
751 struct gl_program_parameter
*p
= &progB
->Parameters
->Parameters
[i
];
752 if (p
->Type
== PROGRAM_STATE_VAR
&&
753 p
->StateIndexes
[0] == STATE_INTERNAL
&&
754 p
->StateIndexes
[1] == STATE_CURRENT_ATTRIB
&&
755 (int) p
->StateIndexes
[2] == (int) VERT_ATTRIB_COLOR0
) {
756 progB_inputsRead
|= VARYING_BIT_COL0
;
757 progB_colorFile
= PROGRAM_STATE_VAR
;
758 progB_colorIndex
= i
;
763 /* Connect color outputs of fprogA to color inputs of fprogB, via a
764 * new temporary register.
766 if ((progA
->OutputsWritten
& BITFIELD64_BIT(FRAG_RESULT_COLOR
)) &&
767 (progB_inputsRead
& VARYING_BIT_COL0
)) {
768 GLint tempReg
= _mesa_find_free_register(usedTemps
, MAX_PROGRAM_TEMPS
,
771 _mesa_problem(ctx
, "No free temp regs found in "
772 "_mesa_combine_programs(), using 31");
775 firstTemp
= tempReg
+ 1;
777 /* replace writes to result.color[0] with tempReg */
778 replace_registers(newInst
, lenA
,
779 PROGRAM_OUTPUT
, FRAG_RESULT_COLOR
,
780 PROGRAM_TEMPORARY
, tempReg
);
781 /* replace reads from the input color with tempReg */
782 replace_registers(newInst
+ lenA
, lenB
,
783 progB_colorFile
, progB_colorIndex
, /* search for */
784 PROGRAM_TEMPORARY
, tempReg
/* replace with */ );
787 /* compute combined program's InputsRead */
788 inputsB
= progB_inputsRead
;
789 if (progA
->OutputsWritten
& BITFIELD64_BIT(FRAG_RESULT_COLOR
)) {
790 inputsB
&= ~(1 << VARYING_SLOT_COL0
);
792 newProg
->InputsRead
= progA
->InputsRead
| inputsB
;
793 newProg
->OutputsWritten
= progB
->OutputsWritten
;
794 newProg
->SamplersUsed
= progA
->SamplersUsed
| progB
->SamplersUsed
;
798 assert(0); /* XXX todo */
802 * Merge parameters (uniforms, constants, etc)
804 newProg
->Parameters
= _mesa_combine_parameter_lists(progA
->Parameters
,
807 adjust_param_indexes(newInst
+ lenA
, lenB
, numParamsA
);
815 * Populate the 'used' array with flags indicating which registers (TEMPs,
816 * INPUTs, OUTPUTs, etc, are used by the given program.
817 * \param file type of register to scan for
818 * \param used returns true/false flags for in use / free
819 * \param usedSize size of the 'used' array
822 _mesa_find_used_registers(const struct gl_program
*prog
,
823 gl_register_file file
,
824 GLboolean used
[], GLuint usedSize
)
828 memset(used
, 0, usedSize
);
830 for (i
= 0; i
< prog
->NumInstructions
; i
++) {
831 const struct prog_instruction
*inst
= prog
->Instructions
+ i
;
832 const GLuint n
= _mesa_num_inst_src_regs(inst
->Opcode
);
834 if (inst
->DstReg
.File
== file
) {
835 ASSERT(inst
->DstReg
.Index
< usedSize
);
836 if(inst
->DstReg
.Index
< usedSize
)
837 used
[inst
->DstReg
.Index
] = GL_TRUE
;
840 for (j
= 0; j
< n
; j
++) {
841 if (inst
->SrcReg
[j
].File
== file
) {
842 ASSERT(inst
->SrcReg
[j
].Index
< (GLint
) usedSize
);
843 if (inst
->SrcReg
[j
].Index
< (GLint
) usedSize
)
844 used
[inst
->SrcReg
[j
].Index
] = GL_TRUE
;
852 * Scan the given 'used' register flag array for the first entry
853 * that's >= firstReg.
854 * \param used vector of flags indicating registers in use (as returned
855 * by _mesa_find_used_registers())
856 * \param usedSize size of the 'used' array
857 * \param firstReg first register to start searching at
858 * \return index of unused register, or -1 if none.
861 _mesa_find_free_register(const GLboolean used
[],
862 GLuint usedSize
, GLuint firstReg
)
866 assert(firstReg
< usedSize
);
868 for (i
= firstReg
; i
< usedSize
; i
++)
878 * Check if the given register index is valid (doesn't exceed implementation-
880 * \return GL_TRUE if OK, GL_FALSE if bad index
883 _mesa_valid_register_index(const struct gl_context
*ctx
,
884 gl_shader_type shaderType
,
885 gl_register_file file
, GLint index
)
887 const struct gl_program_constants
*c
;
889 switch (shaderType
) {
890 case MESA_SHADER_VERTEX
:
891 c
= &ctx
->Const
.VertexProgram
;
893 case MESA_SHADER_FRAGMENT
:
894 c
= &ctx
->Const
.FragmentProgram
;
896 case MESA_SHADER_GEOMETRY
:
897 c
= &ctx
->Const
.GeometryProgram
;
901 "unexpected shader type in _mesa_valid_register_index()");
906 case PROGRAM_UNDEFINED
:
907 return GL_TRUE
; /* XXX or maybe false? */
909 case PROGRAM_TEMPORARY
:
910 return index
>= 0 && index
< (GLint
) c
->MaxTemps
;
912 case PROGRAM_ENV_PARAM
:
913 return index
>= 0 && index
< (GLint
) c
->MaxEnvParams
;
915 case PROGRAM_LOCAL_PARAM
:
916 return index
>= 0 && index
< (GLint
) c
->MaxLocalParams
;
918 case PROGRAM_UNIFORM
:
919 case PROGRAM_STATE_VAR
:
920 /* aka constant buffer */
921 return index
>= 0 && index
< (GLint
) c
->MaxUniformComponents
/ 4;
923 case PROGRAM_CONSTANT
:
924 /* constant buffer w/ possible relative negative addressing */
925 return (index
> (int) c
->MaxUniformComponents
/ -4 &&
926 index
< (int) c
->MaxUniformComponents
/ 4);
932 switch (shaderType
) {
933 case MESA_SHADER_VERTEX
:
934 return index
< VERT_ATTRIB_GENERIC0
+ (GLint
) c
->MaxAttribs
;
935 case MESA_SHADER_FRAGMENT
:
936 return index
< VARYING_SLOT_VAR0
+ (GLint
) ctx
->Const
.MaxVarying
;
937 case MESA_SHADER_GEOMETRY
:
938 return index
< VARYING_SLOT_VAR0
+ (GLint
) ctx
->Const
.MaxVarying
;
947 switch (shaderType
) {
948 case MESA_SHADER_VERTEX
:
949 return index
< VARYING_SLOT_VAR0
+ (GLint
) ctx
->Const
.MaxVarying
;
950 case MESA_SHADER_FRAGMENT
:
951 return index
< FRAG_RESULT_DATA0
+ (GLint
) ctx
->Const
.MaxDrawBuffers
;
952 case MESA_SHADER_GEOMETRY
:
953 return index
< VARYING_SLOT_VAR0
+ (GLint
) ctx
->Const
.MaxVarying
;
958 case PROGRAM_ADDRESS
:
959 return index
>= 0 && index
< (GLint
) c
->MaxAddressRegs
;
963 "unexpected register file in _mesa_valid_register_index()");
971 * "Post-process" a GPU program. This is intended to be used for debugging.
972 * Example actions include no-op'ing instructions or changing instruction
976 _mesa_postprocess_program(struct gl_context
*ctx
, struct gl_program
*prog
)
978 static const GLfloat white
[4] = { 0.5, 0.5, 0.5, 0.5 };
981 GLint whiteIndex
= _mesa_add_unnamed_constant(prog
->Parameters
,
982 (gl_constant_value
*) white
,
987 for (i
= 0; i
< prog
->NumInstructions
; i
++) {
988 struct prog_instruction
*inst
= prog
->Instructions
+ i
;
989 const GLuint n
= _mesa_num_inst_src_regs(inst
->Opcode
);
993 if (_mesa_is_tex_instruction(inst
->Opcode
)) {
995 /* replace TEX/TXP/TXB with MOV */
996 inst
->Opcode
= OPCODE_MOV
;
997 inst
->DstReg
.WriteMask
= WRITEMASK_XYZW
;
998 inst
->SrcReg
[0].Swizzle
= SWIZZLE_XYZW
;
999 inst
->SrcReg
[0].Negate
= NEGATE_NONE
;
1003 /* disable shadow texture mode */
1004 inst
->TexShadow
= 0;
1008 if (inst
->Opcode
== OPCODE_TXP
) {
1010 inst
->Opcode
= OPCODE_MOV
;
1011 inst
->DstReg
.WriteMask
= WRITEMASK_XYZW
;
1012 inst
->SrcReg
[0].File
= PROGRAM_CONSTANT
;
1013 inst
->SrcReg
[0].Index
= whiteIndex
;
1014 inst
->SrcReg
[0].Swizzle
= SWIZZLE_XYZW
;
1015 inst
->SrcReg
[0].Negate
= NEGATE_NONE
;
1018 inst
->TexShadow
= 0;
1021 inst
->Opcode
= OPCODE_TEX
;
1022 inst
->TexShadow
= 0;