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"
35 #include "main/mfeatures.h"
37 #include "prog_cache.h"
38 #include "prog_parameter.h"
39 #include "prog_instruction.h"
43 * A pointer to this dummy program is put into the hash table when
44 * glGenPrograms is called.
46 struct gl_program _mesa_DummyProgram
;
50 * Init context's vertex/fragment program state
53 _mesa_init_program(struct gl_context
*ctx
)
58 * If this assertion fails, we need to increase the field
59 * size for register indexes (see INST_INDEX_BITS).
61 ASSERT(ctx
->Const
.VertexProgram
.MaxUniformComponents
/ 4
62 <= (1 << INST_INDEX_BITS
));
63 ASSERT(ctx
->Const
.FragmentProgram
.MaxUniformComponents
/ 4
64 <= (1 << INST_INDEX_BITS
));
66 ASSERT(ctx
->Const
.VertexProgram
.MaxTemps
<= (1 << INST_INDEX_BITS
));
67 ASSERT(ctx
->Const
.VertexProgram
.MaxLocalParams
<= (1 << INST_INDEX_BITS
));
68 ASSERT(ctx
->Const
.FragmentProgram
.MaxTemps
<= (1 << INST_INDEX_BITS
));
69 ASSERT(ctx
->Const
.FragmentProgram
.MaxLocalParams
<= (1 << INST_INDEX_BITS
));
71 ASSERT(ctx
->Const
.VertexProgram
.MaxUniformComponents
<= 4 * MAX_UNIFORMS
);
72 ASSERT(ctx
->Const
.FragmentProgram
.MaxUniformComponents
<= 4 * MAX_UNIFORMS
);
74 ASSERT(ctx
->Const
.VertexProgram
.MaxAddressOffset
<= (1 << INST_INDEX_BITS
));
75 ASSERT(ctx
->Const
.FragmentProgram
.MaxAddressOffset
<= (1 << INST_INDEX_BITS
));
77 /* If this fails, increase prog_instruction::TexSrcUnit size */
78 ASSERT(MAX_TEXTURE_UNITS
<= (1 << 5));
80 /* If this fails, increase prog_instruction::TexSrcTarget size */
81 ASSERT(NUM_TEXTURE_TARGETS
<= (1 << 4));
83 ctx
->Program
.ErrorPos
= -1;
84 ctx
->Program
.ErrorString
= _mesa_strdup("");
86 #if FEATURE_NV_vertex_program || FEATURE_ARB_vertex_program
87 ctx
->VertexProgram
.Enabled
= GL_FALSE
;
89 ctx
->VertexProgram
.PointSizeEnabled
=
90 (ctx
->API
== API_OPENGLES2
) ? GL_TRUE
: GL_FALSE
;
92 ctx
->VertexProgram
.PointSizeEnabled
= GL_FALSE
;
94 ctx
->VertexProgram
.TwoSideEnabled
= GL_FALSE
;
95 _mesa_reference_vertprog(ctx
, &ctx
->VertexProgram
.Current
,
96 ctx
->Shared
->DefaultVertexProgram
);
97 assert(ctx
->VertexProgram
.Current
);
98 for (i
= 0; i
< MAX_NV_VERTEX_PROGRAM_PARAMS
/ 4; i
++) {
99 ctx
->VertexProgram
.TrackMatrix
[i
] = GL_NONE
;
100 ctx
->VertexProgram
.TrackMatrixTransform
[i
] = GL_IDENTITY_NV
;
102 ctx
->VertexProgram
.Cache
= _mesa_new_program_cache();
105 #if FEATURE_NV_fragment_program || FEATURE_ARB_fragment_program
106 ctx
->FragmentProgram
.Enabled
= GL_FALSE
;
107 _mesa_reference_fragprog(ctx
, &ctx
->FragmentProgram
.Current
,
108 ctx
->Shared
->DefaultFragmentProgram
);
109 assert(ctx
->FragmentProgram
.Current
);
110 ctx
->FragmentProgram
.Cache
= _mesa_new_program_cache();
113 #if FEATURE_ARB_geometry_shader4
114 ctx
->GeometryProgram
.Enabled
= GL_FALSE
;
115 /* right now by default we don't have a geometry program */
116 _mesa_reference_geomprog(ctx
, &ctx
->GeometryProgram
.Current
,
118 ctx
->GeometryProgram
.Cache
= _mesa_new_program_cache();
121 /* XXX probably move this stuff */
122 #if FEATURE_ATI_fragment_shader
123 ctx
->ATIFragmentShader
.Enabled
= GL_FALSE
;
124 ctx
->ATIFragmentShader
.Current
= ctx
->Shared
->DefaultFragmentShader
;
125 assert(ctx
->ATIFragmentShader
.Current
);
126 ctx
->ATIFragmentShader
.Current
->RefCount
++;
132 * Free a context's vertex/fragment program state
135 _mesa_free_program_data(struct gl_context
*ctx
)
137 #if FEATURE_NV_vertex_program || FEATURE_ARB_vertex_program
138 _mesa_reference_vertprog(ctx
, &ctx
->VertexProgram
.Current
, NULL
);
139 _mesa_delete_program_cache(ctx
, ctx
->VertexProgram
.Cache
);
141 #if FEATURE_NV_fragment_program || FEATURE_ARB_fragment_program
142 _mesa_reference_fragprog(ctx
, &ctx
->FragmentProgram
.Current
, NULL
);
143 _mesa_delete_shader_cache(ctx
, ctx
->FragmentProgram
.Cache
);
145 #if FEATURE_ARB_geometry_shader4
146 _mesa_reference_geomprog(ctx
, &ctx
->GeometryProgram
.Current
, NULL
);
147 _mesa_delete_program_cache(ctx
, ctx
->GeometryProgram
.Cache
);
149 /* XXX probably move this stuff */
150 #if FEATURE_ATI_fragment_shader
151 if (ctx
->ATIFragmentShader
.Current
) {
152 ctx
->ATIFragmentShader
.Current
->RefCount
--;
153 if (ctx
->ATIFragmentShader
.Current
->RefCount
<= 0) {
154 free(ctx
->ATIFragmentShader
.Current
);
158 free((void *) ctx
->Program
.ErrorString
);
163 * Update the default program objects in the given context to reference those
164 * specified in the shared state and release those referencing the old
168 _mesa_update_default_objects_program(struct gl_context
*ctx
)
170 #if FEATURE_NV_vertex_program || FEATURE_ARB_vertex_program
171 _mesa_reference_vertprog(ctx
, &ctx
->VertexProgram
.Current
,
172 ctx
->Shared
->DefaultVertexProgram
);
173 assert(ctx
->VertexProgram
.Current
);
176 #if FEATURE_NV_fragment_program || FEATURE_ARB_fragment_program
177 _mesa_reference_fragprog(ctx
, &ctx
->FragmentProgram
.Current
,
178 ctx
->Shared
->DefaultFragmentProgram
);
179 assert(ctx
->FragmentProgram
.Current
);
182 #if FEATURE_ARB_geometry_shader4
183 _mesa_reference_geomprog(ctx
, &ctx
->GeometryProgram
.Current
,
184 ctx
->Shared
->DefaultGeometryProgram
);
187 /* XXX probably move this stuff */
188 #if FEATURE_ATI_fragment_shader
189 if (ctx
->ATIFragmentShader
.Current
) {
190 ctx
->ATIFragmentShader
.Current
->RefCount
--;
191 if (ctx
->ATIFragmentShader
.Current
->RefCount
<= 0) {
192 free(ctx
->ATIFragmentShader
.Current
);
195 ctx
->ATIFragmentShader
.Current
= (struct ati_fragment_shader
*) ctx
->Shared
->DefaultFragmentShader
;
196 assert(ctx
->ATIFragmentShader
.Current
);
197 ctx
->ATIFragmentShader
.Current
->RefCount
++;
203 * Set the vertex/fragment program error state (position and error string).
204 * This is generally called from within the parsers.
207 _mesa_set_program_error(struct gl_context
*ctx
, GLint pos
, const char *string
)
209 ctx
->Program
.ErrorPos
= pos
;
210 free((void *) ctx
->Program
.ErrorString
);
213 ctx
->Program
.ErrorString
= _mesa_strdup(string
);
218 * Find the line number and column for 'pos' within 'string'.
219 * Return a copy of the line which contains 'pos'. Free the line with
221 * \param string the program string
222 * \param pos the position within the string
223 * \param line returns the line number corresponding to 'pos'.
224 * \param col returns the column number corresponding to 'pos'.
225 * \return copy of the line containing 'pos'.
228 _mesa_find_line_column(const GLubyte
*string
, const GLubyte
*pos
,
229 GLint
*line
, GLint
*col
)
231 const GLubyte
*lineStart
= string
;
232 const GLubyte
*p
= string
;
239 if (*p
== (GLubyte
) '\n') {
246 *col
= (pos
- lineStart
) + 1;
248 /* return copy of this line */
249 while (*p
!= 0 && *p
!= '\n')
253 memcpy(s
, lineStart
, len
);
261 * Initialize a new vertex/fragment program object.
263 static struct gl_program
*
264 _mesa_init_program_struct( struct gl_context
*ctx
, struct gl_program
*prog
,
265 GLenum target
, GLuint id
)
270 memset(prog
, 0, sizeof(*prog
));
272 prog
->Target
= target
;
273 prog
->Resident
= GL_TRUE
;
275 prog
->Format
= GL_PROGRAM_FORMAT_ASCII_ARB
;
277 /* default mapping from samplers to texture units */
278 for (i
= 0; i
< MAX_SAMPLERS
; i
++)
279 prog
->SamplerUnits
[i
] = i
;
287 * Initialize a new fragment program object.
290 _mesa_init_fragment_program( struct gl_context
*ctx
, struct gl_fragment_program
*prog
,
291 GLenum target
, GLuint id
)
294 return _mesa_init_program_struct( ctx
, &prog
->Base
, target
, id
);
301 * Initialize a new vertex program object.
304 _mesa_init_vertex_program( struct gl_context
*ctx
, struct gl_vertex_program
*prog
,
305 GLenum target
, GLuint id
)
308 return _mesa_init_program_struct( ctx
, &prog
->Base
, target
, id
);
315 * Initialize a new geometry program object.
318 _mesa_init_geometry_program( struct gl_context
*ctx
, struct gl_geometry_program
*prog
,
319 GLenum target
, GLuint id
)
322 return _mesa_init_program_struct( ctx
, &prog
->Base
, target
, id
);
329 * Allocate and initialize a new fragment/vertex program object but
330 * don't put it into the program hash table. Called via
331 * ctx->Driver.NewProgram. May be overridden (ie. replaced) by a
332 * device driver function to implement OO deriviation with additional
333 * types not understood by this function.
336 * \param id program id/number
337 * \param target program target/type
338 * \return pointer to new program object
341 _mesa_new_program(struct gl_context
*ctx
, GLenum target
, GLuint id
)
343 struct gl_program
*prog
;
345 case GL_VERTEX_PROGRAM_ARB
: /* == GL_VERTEX_PROGRAM_NV */
346 case GL_VERTEX_STATE_PROGRAM_NV
:
347 prog
= _mesa_init_vertex_program(ctx
, CALLOC_STRUCT(gl_vertex_program
),
350 case GL_FRAGMENT_PROGRAM_NV
:
351 case GL_FRAGMENT_PROGRAM_ARB
:
352 prog
=_mesa_init_fragment_program(ctx
,
353 CALLOC_STRUCT(gl_fragment_program
),
356 case MESA_GEOMETRY_PROGRAM
:
357 prog
= _mesa_init_geometry_program(ctx
,
358 CALLOC_STRUCT(gl_geometry_program
),
362 _mesa_problem(ctx
, "bad target in _mesa_new_program");
370 * Delete a program and remove it from the hash table, ignoring the
372 * Called via ctx->Driver.DeleteProgram. May be wrapped (OO deriviation)
373 * by a device driver function.
376 _mesa_delete_program(struct gl_context
*ctx
, struct gl_program
*prog
)
380 ASSERT(prog
->RefCount
==0);
382 if (prog
== &_mesa_DummyProgram
)
388 if (prog
->Instructions
) {
389 _mesa_free_instructions(prog
->Instructions
, prog
->NumInstructions
);
391 if (prog
->Parameters
) {
392 _mesa_free_parameter_list(prog
->Parameters
);
400 * Return the gl_program object for a given ID.
401 * Basically just a wrapper for _mesa_HashLookup() to avoid a lot of
405 _mesa_lookup_program(struct gl_context
*ctx
, GLuint id
)
408 return (struct gl_program
*) _mesa_HashLookup(ctx
->Shared
->Programs
, id
);
415 * Reference counting for vertex/fragment programs
416 * This is normally only called from the _mesa_reference_program() macro
417 * when there's a real pointer change.
420 _mesa_reference_program_(struct gl_context
*ctx
,
421 struct gl_program
**ptr
,
422 struct gl_program
*prog
)
428 if ((*ptr
)->Target
== GL_VERTEX_PROGRAM_ARB
)
429 ASSERT(prog
->Target
== GL_VERTEX_PROGRAM_ARB
);
430 else if ((*ptr
)->Target
== GL_FRAGMENT_PROGRAM_ARB
)
431 ASSERT(prog
->Target
== GL_FRAGMENT_PROGRAM_ARB
||
432 prog
->Target
== GL_FRAGMENT_PROGRAM_NV
);
433 else if ((*ptr
)->Target
== MESA_GEOMETRY_PROGRAM
)
434 ASSERT(prog
->Target
== MESA_GEOMETRY_PROGRAM
);
439 GLboolean deleteFlag
;
441 /*_glthread_LOCK_MUTEX((*ptr)->Mutex);*/
443 printf("Program %p ID=%u Target=%s Refcount-- to %d\n",
445 ((*ptr
)->Target
== GL_VERTEX_PROGRAM_ARB
? "VP" :
446 ((*ptr
)->Target
== MESA_GEOMETRY_PROGRAM
? "GP" : "FP")),
447 (*ptr
)->RefCount
- 1);
449 ASSERT((*ptr
)->RefCount
> 0);
452 deleteFlag
= ((*ptr
)->RefCount
== 0);
453 /*_glthread_UNLOCK_MUTEX((*ptr)->Mutex);*/
457 ctx
->Driver
.DeleteProgram(ctx
, *ptr
);
465 /*_glthread_LOCK_MUTEX(prog->Mutex);*/
468 printf("Program %p ID=%u Target=%s Refcount++ to %d\n",
470 (prog
->Target
== GL_VERTEX_PROGRAM_ARB
? "VP" :
471 (prog
->Target
== MESA_GEOMETRY_PROGRAM
? "GP" : "FP")),
474 /*_glthread_UNLOCK_MUTEX(prog->Mutex);*/
482 * Return a copy of a program.
483 * XXX Problem here if the program object is actually OO-derivation
484 * made by a device driver.
487 _mesa_clone_program(struct gl_context
*ctx
, const struct gl_program
*prog
)
489 struct gl_program
*clone
;
491 clone
= ctx
->Driver
.NewProgram(ctx
, prog
->Target
, prog
->Id
);
495 assert(clone
->Target
== prog
->Target
);
496 assert(clone
->RefCount
== 1);
498 clone
->String
= (GLubyte
*) _mesa_strdup((char *) prog
->String
);
499 clone
->Format
= prog
->Format
;
500 clone
->Instructions
= _mesa_alloc_instructions(prog
->NumInstructions
);
501 if (!clone
->Instructions
) {
502 _mesa_reference_program(ctx
, &clone
, NULL
);
505 _mesa_copy_instructions(clone
->Instructions
, prog
->Instructions
,
506 prog
->NumInstructions
);
507 clone
->InputsRead
= prog
->InputsRead
;
508 clone
->OutputsWritten
= prog
->OutputsWritten
;
509 clone
->SamplersUsed
= prog
->SamplersUsed
;
510 clone
->ShadowSamplers
= prog
->ShadowSamplers
;
511 memcpy(clone
->TexturesUsed
, prog
->TexturesUsed
, sizeof(prog
->TexturesUsed
));
513 if (prog
->Parameters
)
514 clone
->Parameters
= _mesa_clone_parameter_list(prog
->Parameters
);
515 memcpy(clone
->LocalParams
, prog
->LocalParams
, sizeof(clone
->LocalParams
));
516 memcpy(clone
->LocalParams
, prog
->LocalParams
, sizeof(clone
->LocalParams
));
517 clone
->IndirectRegisterFiles
= prog
->IndirectRegisterFiles
;
518 clone
->NumInstructions
= prog
->NumInstructions
;
519 clone
->NumTemporaries
= prog
->NumTemporaries
;
520 clone
->NumParameters
= prog
->NumParameters
;
521 clone
->NumAttributes
= prog
->NumAttributes
;
522 clone
->NumAddressRegs
= prog
->NumAddressRegs
;
523 clone
->NumNativeInstructions
= prog
->NumNativeInstructions
;
524 clone
->NumNativeTemporaries
= prog
->NumNativeTemporaries
;
525 clone
->NumNativeParameters
= prog
->NumNativeParameters
;
526 clone
->NumNativeAttributes
= prog
->NumNativeAttributes
;
527 clone
->NumNativeAddressRegs
= prog
->NumNativeAddressRegs
;
528 clone
->NumAluInstructions
= prog
->NumAluInstructions
;
529 clone
->NumTexInstructions
= prog
->NumTexInstructions
;
530 clone
->NumTexIndirections
= prog
->NumTexIndirections
;
531 clone
->NumNativeAluInstructions
= prog
->NumNativeAluInstructions
;
532 clone
->NumNativeTexInstructions
= prog
->NumNativeTexInstructions
;
533 clone
->NumNativeTexIndirections
= prog
->NumNativeTexIndirections
;
535 switch (prog
->Target
) {
536 case GL_VERTEX_PROGRAM_ARB
:
538 const struct gl_vertex_program
*vp
= gl_vertex_program_const(prog
);
539 struct gl_vertex_program
*vpc
= gl_vertex_program(clone
);
540 vpc
->IsPositionInvariant
= vp
->IsPositionInvariant
;
541 vpc
->IsNVProgram
= vp
->IsNVProgram
;
544 case GL_FRAGMENT_PROGRAM_ARB
:
546 const struct gl_fragment_program
*fp
= gl_fragment_program_const(prog
);
547 struct gl_fragment_program
*fpc
= gl_fragment_program(clone
);
548 fpc
->UsesKill
= fp
->UsesKill
;
549 fpc
->UsesDFdy
= fp
->UsesDFdy
;
550 fpc
->OriginUpperLeft
= fp
->OriginUpperLeft
;
551 fpc
->PixelCenterInteger
= fp
->PixelCenterInteger
;
554 case MESA_GEOMETRY_PROGRAM
:
556 const struct gl_geometry_program
*gp
= gl_geometry_program_const(prog
);
557 struct gl_geometry_program
*gpc
= gl_geometry_program(clone
);
558 gpc
->VerticesOut
= gp
->VerticesOut
;
559 gpc
->InputType
= gp
->InputType
;
560 gpc
->OutputType
= gp
->OutputType
;
564 _mesa_problem(NULL
, "Unexpected target in _mesa_clone_program");
572 * Insert 'count' NOP instructions at 'start' in the given program.
573 * Adjust branch targets accordingly.
576 _mesa_insert_instructions(struct gl_program
*prog
, GLuint start
, GLuint count
)
578 const GLuint origLen
= prog
->NumInstructions
;
579 const GLuint newLen
= origLen
+ count
;
580 struct prog_instruction
*newInst
;
583 /* adjust branches */
584 for (i
= 0; i
< prog
->NumInstructions
; i
++) {
585 struct prog_instruction
*inst
= prog
->Instructions
+ i
;
586 if (inst
->BranchTarget
> 0) {
587 if ((GLuint
)inst
->BranchTarget
>= start
) {
588 inst
->BranchTarget
+= count
;
593 /* Alloc storage for new instructions */
594 newInst
= _mesa_alloc_instructions(newLen
);
599 /* Copy 'start' instructions into new instruction buffer */
600 _mesa_copy_instructions(newInst
, prog
->Instructions
, start
);
602 /* init the new instructions */
603 _mesa_init_instructions(newInst
+ start
, count
);
605 /* Copy the remaining/tail instructions to new inst buffer */
606 _mesa_copy_instructions(newInst
+ start
+ count
,
607 prog
->Instructions
+ start
,
610 /* free old instructions */
611 _mesa_free_instructions(prog
->Instructions
, origLen
);
613 /* install new instructions */
614 prog
->Instructions
= newInst
;
615 prog
->NumInstructions
= newLen
;
621 * Delete 'count' instructions at 'start' in the given program.
622 * Adjust branch targets accordingly.
625 _mesa_delete_instructions(struct gl_program
*prog
, GLuint start
, GLuint count
)
627 const GLuint origLen
= prog
->NumInstructions
;
628 const GLuint newLen
= origLen
- count
;
629 struct prog_instruction
*newInst
;
632 /* adjust branches */
633 for (i
= 0; i
< prog
->NumInstructions
; i
++) {
634 struct prog_instruction
*inst
= prog
->Instructions
+ i
;
635 if (inst
->BranchTarget
> 0) {
636 if (inst
->BranchTarget
> (GLint
) start
) {
637 inst
->BranchTarget
-= count
;
642 /* Alloc storage for new instructions */
643 newInst
= _mesa_alloc_instructions(newLen
);
648 /* Copy 'start' instructions into new instruction buffer */
649 _mesa_copy_instructions(newInst
, prog
->Instructions
, start
);
651 /* Copy the remaining/tail instructions to new inst buffer */
652 _mesa_copy_instructions(newInst
+ start
,
653 prog
->Instructions
+ start
+ count
,
656 /* free old instructions */
657 _mesa_free_instructions(prog
->Instructions
, origLen
);
659 /* install new instructions */
660 prog
->Instructions
= newInst
;
661 prog
->NumInstructions
= newLen
;
668 * Search instructions for registers that match (oldFile, oldIndex),
669 * replacing them with (newFile, newIndex).
672 replace_registers(struct prog_instruction
*inst
, GLuint numInst
,
673 GLuint oldFile
, GLuint oldIndex
,
674 GLuint newFile
, GLuint newIndex
)
677 for (i
= 0; i
< numInst
; i
++) {
679 for (j
= 0; j
< _mesa_num_inst_src_regs(inst
[i
].Opcode
); j
++) {
680 if (inst
[i
].SrcReg
[j
].File
== oldFile
&&
681 inst
[i
].SrcReg
[j
].Index
== oldIndex
) {
682 inst
[i
].SrcReg
[j
].File
= newFile
;
683 inst
[i
].SrcReg
[j
].Index
= newIndex
;
687 if (inst
[i
].DstReg
.File
== oldFile
&& inst
[i
].DstReg
.Index
== oldIndex
) {
688 inst
[i
].DstReg
.File
= newFile
;
689 inst
[i
].DstReg
.Index
= newIndex
;
696 * Search instructions for references to program parameters. When found,
697 * increment the parameter index by 'offset'.
698 * Used when combining programs.
701 adjust_param_indexes(struct prog_instruction
*inst
, GLuint numInst
,
705 for (i
= 0; i
< numInst
; i
++) {
706 for (j
= 0; j
< _mesa_num_inst_src_regs(inst
[i
].Opcode
); j
++) {
707 GLuint f
= inst
[i
].SrcReg
[j
].File
;
708 if (f
== PROGRAM_CONSTANT
||
709 f
== PROGRAM_UNIFORM
||
710 f
== PROGRAM_STATE_VAR
) {
711 inst
[i
].SrcReg
[j
].Index
+= offset
;
719 * Combine two programs into one. Fix instructions so the outputs of
720 * the first program go to the inputs of the second program.
723 _mesa_combine_programs(struct gl_context
*ctx
,
724 const struct gl_program
*progA
,
725 const struct gl_program
*progB
)
727 struct prog_instruction
*newInst
;
728 struct gl_program
*newProg
;
729 const GLuint lenA
= progA
->NumInstructions
- 1; /* omit END instr */
730 const GLuint lenB
= progB
->NumInstructions
;
731 const GLuint numParamsA
= _mesa_num_parameters(progA
->Parameters
);
732 const GLuint newLength
= lenA
+ lenB
;
733 GLboolean usedTemps
[MAX_PROGRAM_TEMPS
];
734 GLuint firstTemp
= 0;
738 ASSERT(progA
->Target
== progB
->Target
);
740 newInst
= _mesa_alloc_instructions(newLength
);
744 _mesa_copy_instructions(newInst
, progA
->Instructions
, lenA
);
745 _mesa_copy_instructions(newInst
+ lenA
, progB
->Instructions
, lenB
);
747 /* adjust branch / instruction addresses for B's instructions */
748 for (i
= 0; i
< lenB
; i
++) {
749 newInst
[lenA
+ i
].BranchTarget
+= lenA
;
752 newProg
= ctx
->Driver
.NewProgram(ctx
, progA
->Target
, 0);
753 newProg
->Instructions
= newInst
;
754 newProg
->NumInstructions
= newLength
;
756 /* find used temp regs (we may need new temps below) */
757 _mesa_find_used_registers(newProg
, PROGRAM_TEMPORARY
,
758 usedTemps
, MAX_PROGRAM_TEMPS
);
760 if (newProg
->Target
== GL_FRAGMENT_PROGRAM_ARB
) {
761 const struct gl_fragment_program
*fprogA
, *fprogB
;
762 struct gl_fragment_program
*newFprog
;
763 GLbitfield progB_inputsRead
= progB
->InputsRead
;
764 GLint progB_colorFile
, progB_colorIndex
;
766 fprogA
= gl_fragment_program_const(progA
);
767 fprogB
= gl_fragment_program_const(progB
);
768 newFprog
= gl_fragment_program(newProg
);
770 newFprog
->UsesKill
= fprogA
->UsesKill
|| fprogB
->UsesKill
;
771 newFprog
->UsesDFdy
= fprogA
->UsesDFdy
|| fprogB
->UsesDFdy
;
773 /* We'll do a search and replace for instances
774 * of progB_colorFile/progB_colorIndex below...
776 progB_colorFile
= PROGRAM_INPUT
;
777 progB_colorIndex
= FRAG_ATTRIB_COL0
;
780 * The fragment program may get color from a state var rather than
781 * a fragment input (vertex output) if it's constant.
782 * See the texenvprogram.c code.
783 * So, search the program's parameter list now to see if the program
784 * gets color from a state var instead of a conventional fragment
787 for (i
= 0; i
< progB
->Parameters
->NumParameters
; i
++) {
788 struct gl_program_parameter
*p
= &progB
->Parameters
->Parameters
[i
];
789 if (p
->Type
== PROGRAM_STATE_VAR
&&
790 p
->StateIndexes
[0] == STATE_INTERNAL
&&
791 p
->StateIndexes
[1] == STATE_CURRENT_ATTRIB
&&
792 (int) p
->StateIndexes
[2] == (int) VERT_ATTRIB_COLOR0
) {
793 progB_inputsRead
|= FRAG_BIT_COL0
;
794 progB_colorFile
= PROGRAM_STATE_VAR
;
795 progB_colorIndex
= i
;
800 /* Connect color outputs of fprogA to color inputs of fprogB, via a
801 * new temporary register.
803 if ((progA
->OutputsWritten
& BITFIELD64_BIT(FRAG_RESULT_COLOR
)) &&
804 (progB_inputsRead
& FRAG_BIT_COL0
)) {
805 GLint tempReg
= _mesa_find_free_register(usedTemps
, MAX_PROGRAM_TEMPS
,
808 _mesa_problem(ctx
, "No free temp regs found in "
809 "_mesa_combine_programs(), using 31");
812 firstTemp
= tempReg
+ 1;
814 /* replace writes to result.color[0] with tempReg */
815 replace_registers(newInst
, lenA
,
816 PROGRAM_OUTPUT
, FRAG_RESULT_COLOR
,
817 PROGRAM_TEMPORARY
, tempReg
);
818 /* replace reads from the input color with tempReg */
819 replace_registers(newInst
+ lenA
, lenB
,
820 progB_colorFile
, progB_colorIndex
, /* search for */
821 PROGRAM_TEMPORARY
, tempReg
/* replace with */ );
824 /* compute combined program's InputsRead */
825 inputsB
= progB_inputsRead
;
826 if (progA
->OutputsWritten
& BITFIELD64_BIT(FRAG_RESULT_COLOR
)) {
827 inputsB
&= ~(1 << FRAG_ATTRIB_COL0
);
829 newProg
->InputsRead
= progA
->InputsRead
| inputsB
;
830 newProg
->OutputsWritten
= progB
->OutputsWritten
;
831 newProg
->SamplersUsed
= progA
->SamplersUsed
| progB
->SamplersUsed
;
835 assert(0); /* XXX todo */
839 * Merge parameters (uniforms, constants, etc)
841 newProg
->Parameters
= _mesa_combine_parameter_lists(progA
->Parameters
,
844 adjust_param_indexes(newInst
+ lenA
, lenB
, numParamsA
);
852 * Populate the 'used' array with flags indicating which registers (TEMPs,
853 * INPUTs, OUTPUTs, etc, are used by the given program.
854 * \param file type of register to scan for
855 * \param used returns true/false flags for in use / free
856 * \param usedSize size of the 'used' array
859 _mesa_find_used_registers(const struct gl_program
*prog
,
860 gl_register_file file
,
861 GLboolean used
[], GLuint usedSize
)
865 memset(used
, 0, usedSize
);
867 for (i
= 0; i
< prog
->NumInstructions
; i
++) {
868 const struct prog_instruction
*inst
= prog
->Instructions
+ i
;
869 const GLuint n
= _mesa_num_inst_src_regs(inst
->Opcode
);
871 if (inst
->DstReg
.File
== file
) {
872 ASSERT(inst
->DstReg
.Index
< usedSize
);
873 if(inst
->DstReg
.Index
< usedSize
)
874 used
[inst
->DstReg
.Index
] = GL_TRUE
;
877 for (j
= 0; j
< n
; j
++) {
878 if (inst
->SrcReg
[j
].File
== file
) {
879 ASSERT(inst
->SrcReg
[j
].Index
< usedSize
);
880 if(inst
->SrcReg
[j
].Index
< usedSize
)
881 used
[inst
->SrcReg
[j
].Index
] = GL_TRUE
;
889 * Scan the given 'used' register flag array for the first entry
890 * that's >= firstReg.
891 * \param used vector of flags indicating registers in use (as returned
892 * by _mesa_find_used_registers())
893 * \param usedSize size of the 'used' array
894 * \param firstReg first register to start searching at
895 * \return index of unused register, or -1 if none.
898 _mesa_find_free_register(const GLboolean used
[],
899 GLuint usedSize
, GLuint firstReg
)
903 assert(firstReg
< usedSize
);
905 for (i
= firstReg
; i
< usedSize
; i
++)
915 * Check if the given register index is valid (doesn't exceed implementation-
917 * \return GL_TRUE if OK, GL_FALSE if bad index
920 _mesa_valid_register_index(const struct gl_context
*ctx
,
921 gl_shader_type shaderType
,
922 gl_register_file file
, GLint index
)
924 const struct gl_program_constants
*c
;
926 switch (shaderType
) {
927 case MESA_SHADER_VERTEX
:
928 c
= &ctx
->Const
.VertexProgram
;
930 case MESA_SHADER_FRAGMENT
:
931 c
= &ctx
->Const
.FragmentProgram
;
933 case MESA_SHADER_GEOMETRY
:
934 c
= &ctx
->Const
.GeometryProgram
;
938 "unexpected shader type in _mesa_valid_register_index()");
943 case PROGRAM_UNDEFINED
:
944 return GL_TRUE
; /* XXX or maybe false? */
946 case PROGRAM_TEMPORARY
:
947 return index
>= 0 && index
< c
->MaxTemps
;
949 case PROGRAM_ENV_PARAM
:
950 return index
>= 0 && index
< c
->MaxEnvParams
;
952 case PROGRAM_LOCAL_PARAM
:
953 return index
>= 0 && index
< c
->MaxLocalParams
;
955 case PROGRAM_NAMED_PARAM
:
956 return index
>= 0 && index
< c
->MaxParameters
;
958 case PROGRAM_UNIFORM
:
959 case PROGRAM_STATE_VAR
:
960 /* aka constant buffer */
961 return index
>= 0 && index
< c
->MaxUniformComponents
/ 4;
963 case PROGRAM_CONSTANT
:
964 /* constant buffer w/ possible relative negative addressing */
965 return (index
> (int) c
->MaxUniformComponents
/ -4 &&
966 index
< c
->MaxUniformComponents
/ 4);
972 switch (shaderType
) {
973 case MESA_SHADER_VERTEX
:
974 return index
< VERT_ATTRIB_GENERIC0
+ c
->MaxAttribs
;
975 case MESA_SHADER_FRAGMENT
:
976 return index
< FRAG_ATTRIB_VAR0
+ ctx
->Const
.MaxVarying
;
977 case MESA_SHADER_GEOMETRY
:
978 return index
< GEOM_ATTRIB_VAR0
+ ctx
->Const
.MaxVarying
;
987 switch (shaderType
) {
988 case MESA_SHADER_VERTEX
:
989 return index
< VERT_RESULT_VAR0
+ ctx
->Const
.MaxVarying
;
990 case MESA_SHADER_FRAGMENT
:
991 return index
< FRAG_RESULT_DATA0
+ ctx
->Const
.MaxDrawBuffers
;
992 case MESA_SHADER_GEOMETRY
:
993 return index
< GEOM_RESULT_VAR0
+ ctx
->Const
.MaxVarying
;
998 case PROGRAM_ADDRESS
:
999 return index
>= 0 && index
< c
->MaxAddressRegs
;
1003 "unexpected register file in _mesa_valid_register_index()");
1011 * "Post-process" a GPU program. This is intended to be used for debugging.
1012 * Example actions include no-op'ing instructions or changing instruction
1016 _mesa_postprocess_program(struct gl_context
*ctx
, struct gl_program
*prog
)
1018 static const GLfloat white
[4] = { 0.5, 0.5, 0.5, 0.5 };
1020 GLuint whiteSwizzle
;
1021 GLint whiteIndex
= _mesa_add_unnamed_constant(prog
->Parameters
,
1022 (gl_constant_value
*) white
,
1027 for (i
= 0; i
< prog
->NumInstructions
; i
++) {
1028 struct prog_instruction
*inst
= prog
->Instructions
+ i
;
1029 const GLuint n
= _mesa_num_inst_src_regs(inst
->Opcode
);
1033 if (_mesa_is_tex_instruction(inst
->Opcode
)) {
1035 /* replace TEX/TXP/TXB with MOV */
1036 inst
->Opcode
= OPCODE_MOV
;
1037 inst
->DstReg
.WriteMask
= WRITEMASK_XYZW
;
1038 inst
->SrcReg
[0].Swizzle
= SWIZZLE_XYZW
;
1039 inst
->SrcReg
[0].Negate
= NEGATE_NONE
;
1043 /* disable shadow texture mode */
1044 inst
->TexShadow
= 0;
1048 if (inst
->Opcode
== OPCODE_TXP
) {
1050 inst
->Opcode
= OPCODE_MOV
;
1051 inst
->DstReg
.WriteMask
= WRITEMASK_XYZW
;
1052 inst
->SrcReg
[0].File
= PROGRAM_CONSTANT
;
1053 inst
->SrcReg
[0].Index
= whiteIndex
;
1054 inst
->SrcReg
[0].Swizzle
= SWIZZLE_XYZW
;
1055 inst
->SrcReg
[0].Negate
= NEGATE_NONE
;
1058 inst
->TexShadow
= 0;
1061 inst
->Opcode
= OPCODE_TEX
;
1062 inst
->TexShadow
= 0;