2 * Mesa 3-D graphics library
4 * Copyright (C) 1999-2007 Brian Paul All Rights Reserved.
6 * Permission is hereby granted, free of charge, to any person obtaining a
7 * copy of this software and associated documentation files (the "Software"),
8 * to deal in the Software without restriction, including without limitation
9 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
10 * and/or sell copies of the Software, and to permit persons to whom the
11 * Software is furnished to do so, subject to the following conditions:
13 * The above copyright notice and this permission notice shall be included
14 * in all copies or substantial portions of the Software.
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
17 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
19 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR
20 * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
21 * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
22 * 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/macros.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
)
56 * If this assertion fails, we need to increase the field
57 * size for register indexes (see INST_INDEX_BITS).
59 ASSERT(ctx
->Const
.Program
[MESA_SHADER_VERTEX
].MaxUniformComponents
/ 4
60 <= (1 << INST_INDEX_BITS
));
61 ASSERT(ctx
->Const
.Program
[MESA_SHADER_FRAGMENT
].MaxUniformComponents
/ 4
62 <= (1 << INST_INDEX_BITS
));
64 ASSERT(ctx
->Const
.Program
[MESA_SHADER_VERTEX
].MaxTemps
<= (1 << INST_INDEX_BITS
));
65 ASSERT(ctx
->Const
.Program
[MESA_SHADER_VERTEX
].MaxLocalParams
<= (1 << INST_INDEX_BITS
));
66 ASSERT(ctx
->Const
.Program
[MESA_SHADER_FRAGMENT
].MaxTemps
<= (1 << INST_INDEX_BITS
));
67 ASSERT(ctx
->Const
.Program
[MESA_SHADER_FRAGMENT
].MaxLocalParams
<= (1 << INST_INDEX_BITS
));
69 ASSERT(ctx
->Const
.Program
[MESA_SHADER_VERTEX
].MaxUniformComponents
<= 4 * MAX_UNIFORMS
);
70 ASSERT(ctx
->Const
.Program
[MESA_SHADER_FRAGMENT
].MaxUniformComponents
<= 4 * MAX_UNIFORMS
);
72 ASSERT(ctx
->Const
.Program
[MESA_SHADER_VERTEX
].MaxAddressOffset
<= (1 << INST_INDEX_BITS
));
73 ASSERT(ctx
->Const
.Program
[MESA_SHADER_FRAGMENT
].MaxAddressOffset
<= (1 << INST_INDEX_BITS
));
75 /* If this fails, increase prog_instruction::TexSrcUnit size */
76 STATIC_ASSERT(MAX_TEXTURE_UNITS
<= (1 << 5));
78 /* If this fails, increase prog_instruction::TexSrcTarget size */
79 STATIC_ASSERT(NUM_TEXTURE_TARGETS
<= (1 << 4));
81 ctx
->Program
.ErrorPos
= -1;
82 ctx
->Program
.ErrorString
= _mesa_strdup("");
84 ctx
->VertexProgram
.Enabled
= GL_FALSE
;
85 ctx
->VertexProgram
.PointSizeEnabled
=
86 (ctx
->API
== API_OPENGLES2
) ? GL_TRUE
: GL_FALSE
;
87 ctx
->VertexProgram
.TwoSideEnabled
= GL_FALSE
;
88 _mesa_reference_vertprog(ctx
, &ctx
->VertexProgram
.Current
,
89 ctx
->Shared
->DefaultVertexProgram
);
90 assert(ctx
->VertexProgram
.Current
);
91 ctx
->VertexProgram
.Cache
= _mesa_new_program_cache();
93 ctx
->FragmentProgram
.Enabled
= GL_FALSE
;
94 _mesa_reference_fragprog(ctx
, &ctx
->FragmentProgram
.Current
,
95 ctx
->Shared
->DefaultFragmentProgram
);
96 assert(ctx
->FragmentProgram
.Current
);
97 ctx
->FragmentProgram
.Cache
= _mesa_new_program_cache();
99 ctx
->GeometryProgram
.Enabled
= GL_FALSE
;
100 /* right now by default we don't have a geometry program */
101 _mesa_reference_geomprog(ctx
, &ctx
->GeometryProgram
.Current
,
103 ctx
->GeometryProgram
.Cache
= _mesa_new_program_cache();
105 /* XXX probably move this stuff */
106 ctx
->ATIFragmentShader
.Enabled
= GL_FALSE
;
107 ctx
->ATIFragmentShader
.Current
= ctx
->Shared
->DefaultFragmentShader
;
108 assert(ctx
->ATIFragmentShader
.Current
);
109 ctx
->ATIFragmentShader
.Current
->RefCount
++;
114 * Free a context's vertex/fragment program state
117 _mesa_free_program_data(struct gl_context
*ctx
)
119 _mesa_reference_vertprog(ctx
, &ctx
->VertexProgram
.Current
, NULL
);
120 _mesa_delete_program_cache(ctx
, ctx
->VertexProgram
.Cache
);
121 _mesa_reference_fragprog(ctx
, &ctx
->FragmentProgram
.Current
, NULL
);
122 _mesa_delete_shader_cache(ctx
, ctx
->FragmentProgram
.Cache
);
123 _mesa_reference_geomprog(ctx
, &ctx
->GeometryProgram
.Current
, NULL
);
124 _mesa_delete_program_cache(ctx
, ctx
->GeometryProgram
.Cache
);
126 /* XXX probably move this stuff */
127 if (ctx
->ATIFragmentShader
.Current
) {
128 ctx
->ATIFragmentShader
.Current
->RefCount
--;
129 if (ctx
->ATIFragmentShader
.Current
->RefCount
<= 0) {
130 free(ctx
->ATIFragmentShader
.Current
);
134 free((void *) ctx
->Program
.ErrorString
);
139 * Update the default program objects in the given context to reference those
140 * specified in the shared state and release those referencing the old
144 _mesa_update_default_objects_program(struct gl_context
*ctx
)
146 _mesa_reference_vertprog(ctx
, &ctx
->VertexProgram
.Current
,
147 ctx
->Shared
->DefaultVertexProgram
);
148 assert(ctx
->VertexProgram
.Current
);
150 _mesa_reference_fragprog(ctx
, &ctx
->FragmentProgram
.Current
,
151 ctx
->Shared
->DefaultFragmentProgram
);
152 assert(ctx
->FragmentProgram
.Current
);
154 _mesa_reference_geomprog(ctx
, &ctx
->GeometryProgram
.Current
,
155 ctx
->Shared
->DefaultGeometryProgram
);
157 /* XXX probably move this stuff */
158 if (ctx
->ATIFragmentShader
.Current
) {
159 ctx
->ATIFragmentShader
.Current
->RefCount
--;
160 if (ctx
->ATIFragmentShader
.Current
->RefCount
<= 0) {
161 free(ctx
->ATIFragmentShader
.Current
);
164 ctx
->ATIFragmentShader
.Current
= (struct ati_fragment_shader
*) ctx
->Shared
->DefaultFragmentShader
;
165 assert(ctx
->ATIFragmentShader
.Current
);
166 ctx
->ATIFragmentShader
.Current
->RefCount
++;
171 * Set the vertex/fragment program error state (position and error string).
172 * This is generally called from within the parsers.
175 _mesa_set_program_error(struct gl_context
*ctx
, GLint pos
, const char *string
)
177 ctx
->Program
.ErrorPos
= pos
;
178 free((void *) ctx
->Program
.ErrorString
);
181 ctx
->Program
.ErrorString
= _mesa_strdup(string
);
186 * Find the line number and column for 'pos' within 'string'.
187 * Return a copy of the line which contains 'pos'. Free the line with
189 * \param string the program string
190 * \param pos the position within the string
191 * \param line returns the line number corresponding to 'pos'.
192 * \param col returns the column number corresponding to 'pos'.
193 * \return copy of the line containing 'pos'.
196 _mesa_find_line_column(const GLubyte
*string
, const GLubyte
*pos
,
197 GLint
*line
, GLint
*col
)
199 const GLubyte
*lineStart
= string
;
200 const GLubyte
*p
= string
;
207 if (*p
== (GLubyte
) '\n') {
214 *col
= (pos
- lineStart
) + 1;
216 /* return copy of this line */
217 while (*p
!= 0 && *p
!= '\n')
221 memcpy(s
, lineStart
, len
);
229 * Initialize a new vertex/fragment program object.
231 static struct gl_program
*
232 _mesa_init_program_struct( struct gl_context
*ctx
, struct gl_program
*prog
,
233 GLenum target
, GLuint id
)
238 memset(prog
, 0, sizeof(*prog
));
240 prog
->Target
= target
;
242 prog
->Format
= GL_PROGRAM_FORMAT_ASCII_ARB
;
244 /* default mapping from samplers to texture units */
245 for (i
= 0; i
< MAX_SAMPLERS
; i
++)
246 prog
->SamplerUnits
[i
] = i
;
254 * Initialize a new fragment program object.
257 _mesa_init_fragment_program( struct gl_context
*ctx
, struct gl_fragment_program
*prog
,
258 GLenum target
, GLuint id
)
261 return _mesa_init_program_struct( ctx
, &prog
->Base
, target
, id
);
268 * Initialize a new vertex program object.
271 _mesa_init_vertex_program( struct gl_context
*ctx
, struct gl_vertex_program
*prog
,
272 GLenum target
, GLuint id
)
275 return _mesa_init_program_struct( ctx
, &prog
->Base
, target
, id
);
282 * Initialize a new compute program object.
285 _mesa_init_compute_program(struct gl_context
*ctx
,
286 struct gl_compute_program
*prog
, GLenum target
,
290 return _mesa_init_program_struct( ctx
, &prog
->Base
, target
, id
);
297 * Initialize a new geometry program object.
300 _mesa_init_geometry_program( struct gl_context
*ctx
, struct gl_geometry_program
*prog
,
301 GLenum target
, GLuint id
)
304 return _mesa_init_program_struct( ctx
, &prog
->Base
, target
, id
);
311 * Allocate and initialize a new fragment/vertex program object but
312 * don't put it into the program hash table. Called via
313 * ctx->Driver.NewProgram. May be overridden (ie. replaced) by a
314 * device driver function to implement OO deriviation with additional
315 * types not understood by this function.
318 * \param id program id/number
319 * \param target program target/type
320 * \return pointer to new program object
323 _mesa_new_program(struct gl_context
*ctx
, GLenum target
, GLuint id
)
325 struct gl_program
*prog
;
327 case GL_VERTEX_PROGRAM_ARB
: /* == GL_VERTEX_PROGRAM_NV */
328 prog
= _mesa_init_vertex_program(ctx
, CALLOC_STRUCT(gl_vertex_program
),
331 case GL_FRAGMENT_PROGRAM_NV
:
332 case GL_FRAGMENT_PROGRAM_ARB
:
333 prog
=_mesa_init_fragment_program(ctx
,
334 CALLOC_STRUCT(gl_fragment_program
),
337 case MESA_GEOMETRY_PROGRAM
:
338 prog
= _mesa_init_geometry_program(ctx
,
339 CALLOC_STRUCT(gl_geometry_program
),
342 case GL_COMPUTE_PROGRAM_NV
:
343 prog
= _mesa_init_compute_program(ctx
,
344 CALLOC_STRUCT(gl_compute_program
),
348 _mesa_problem(ctx
, "bad target in _mesa_new_program");
356 * Delete a program and remove it from the hash table, ignoring the
358 * Called via ctx->Driver.DeleteProgram. May be wrapped (OO deriviation)
359 * by a device driver function.
362 _mesa_delete_program(struct gl_context
*ctx
, struct gl_program
*prog
)
366 ASSERT(prog
->RefCount
==0);
368 if (prog
== &_mesa_DummyProgram
)
372 free(prog
->LocalParams
);
374 if (prog
->Instructions
) {
375 _mesa_free_instructions(prog
->Instructions
, prog
->NumInstructions
);
377 if (prog
->Parameters
) {
378 _mesa_free_parameter_list(prog
->Parameters
);
386 * Return the gl_program object for a given ID.
387 * Basically just a wrapper for _mesa_HashLookup() to avoid a lot of
391 _mesa_lookup_program(struct gl_context
*ctx
, GLuint id
)
394 return (struct gl_program
*) _mesa_HashLookup(ctx
->Shared
->Programs
, id
);
401 * Reference counting for vertex/fragment programs
402 * This is normally only called from the _mesa_reference_program() macro
403 * when there's a real pointer change.
406 _mesa_reference_program_(struct gl_context
*ctx
,
407 struct gl_program
**ptr
,
408 struct gl_program
*prog
)
414 if ((*ptr
)->Target
== GL_VERTEX_PROGRAM_ARB
)
415 ASSERT(prog
->Target
== GL_VERTEX_PROGRAM_ARB
);
416 else if ((*ptr
)->Target
== GL_FRAGMENT_PROGRAM_ARB
)
417 ASSERT(prog
->Target
== GL_FRAGMENT_PROGRAM_ARB
||
418 prog
->Target
== GL_FRAGMENT_PROGRAM_NV
);
419 else if ((*ptr
)->Target
== MESA_GEOMETRY_PROGRAM
)
420 ASSERT(prog
->Target
== MESA_GEOMETRY_PROGRAM
);
425 GLboolean deleteFlag
;
427 /*mtx_lock(&(*ptr)->Mutex);*/
429 printf("Program %p ID=%u Target=%s Refcount-- to %d\n",
431 ((*ptr
)->Target
== GL_VERTEX_PROGRAM_ARB
? "VP" :
432 ((*ptr
)->Target
== MESA_GEOMETRY_PROGRAM
? "GP" : "FP")),
433 (*ptr
)->RefCount
- 1);
435 ASSERT((*ptr
)->RefCount
> 0);
438 deleteFlag
= ((*ptr
)->RefCount
== 0);
439 /*mtx_lock(&(*ptr)->Mutex);*/
443 ctx
->Driver
.DeleteProgram(ctx
, *ptr
);
451 /*mtx_lock(&prog->Mutex);*/
454 printf("Program %p ID=%u Target=%s Refcount++ to %d\n",
456 (prog
->Target
== GL_VERTEX_PROGRAM_ARB
? "VP" :
457 (prog
->Target
== MESA_GEOMETRY_PROGRAM
? "GP" : "FP")),
460 /*mtx_unlock(&prog->Mutex);*/
468 * Return a copy of a program.
469 * XXX Problem here if the program object is actually OO-derivation
470 * made by a device driver.
473 _mesa_clone_program(struct gl_context
*ctx
, const struct gl_program
*prog
)
475 struct gl_program
*clone
;
477 clone
= ctx
->Driver
.NewProgram(ctx
, prog
->Target
, prog
->Id
);
481 assert(clone
->Target
== prog
->Target
);
482 assert(clone
->RefCount
== 1);
484 clone
->String
= (GLubyte
*) _mesa_strdup((char *) prog
->String
);
485 clone
->Format
= prog
->Format
;
486 clone
->Instructions
= _mesa_alloc_instructions(prog
->NumInstructions
);
487 if (!clone
->Instructions
) {
488 _mesa_reference_program(ctx
, &clone
, NULL
);
491 _mesa_copy_instructions(clone
->Instructions
, prog
->Instructions
,
492 prog
->NumInstructions
);
493 clone
->InputsRead
= prog
->InputsRead
;
494 clone
->OutputsWritten
= prog
->OutputsWritten
;
495 clone
->SamplersUsed
= prog
->SamplersUsed
;
496 clone
->ShadowSamplers
= prog
->ShadowSamplers
;
497 memcpy(clone
->TexturesUsed
, prog
->TexturesUsed
, sizeof(prog
->TexturesUsed
));
499 if (prog
->Parameters
)
500 clone
->Parameters
= _mesa_clone_parameter_list(prog
->Parameters
);
501 if (prog
->LocalParams
) {
502 clone
->LocalParams
= malloc(MAX_PROGRAM_LOCAL_PARAMS
*
504 if (!clone
->LocalParams
) {
505 _mesa_reference_program(ctx
, &clone
, NULL
);
508 memcpy(clone
->LocalParams
, prog
->LocalParams
,
509 MAX_PROGRAM_LOCAL_PARAMS
* sizeof(float[4]));
511 clone
->IndirectRegisterFiles
= prog
->IndirectRegisterFiles
;
512 clone
->NumInstructions
= prog
->NumInstructions
;
513 clone
->NumTemporaries
= prog
->NumTemporaries
;
514 clone
->NumParameters
= prog
->NumParameters
;
515 clone
->NumAttributes
= prog
->NumAttributes
;
516 clone
->NumAddressRegs
= prog
->NumAddressRegs
;
517 clone
->NumNativeInstructions
= prog
->NumNativeInstructions
;
518 clone
->NumNativeTemporaries
= prog
->NumNativeTemporaries
;
519 clone
->NumNativeParameters
= prog
->NumNativeParameters
;
520 clone
->NumNativeAttributes
= prog
->NumNativeAttributes
;
521 clone
->NumNativeAddressRegs
= prog
->NumNativeAddressRegs
;
522 clone
->NumAluInstructions
= prog
->NumAluInstructions
;
523 clone
->NumTexInstructions
= prog
->NumTexInstructions
;
524 clone
->NumTexIndirections
= prog
->NumTexIndirections
;
525 clone
->NumNativeAluInstructions
= prog
->NumNativeAluInstructions
;
526 clone
->NumNativeTexInstructions
= prog
->NumNativeTexInstructions
;
527 clone
->NumNativeTexIndirections
= prog
->NumNativeTexIndirections
;
529 switch (prog
->Target
) {
530 case GL_VERTEX_PROGRAM_ARB
:
532 const struct gl_vertex_program
*vp
= gl_vertex_program_const(prog
);
533 struct gl_vertex_program
*vpc
= gl_vertex_program(clone
);
534 vpc
->IsPositionInvariant
= vp
->IsPositionInvariant
;
537 case GL_FRAGMENT_PROGRAM_ARB
:
539 const struct gl_fragment_program
*fp
= gl_fragment_program_const(prog
);
540 struct gl_fragment_program
*fpc
= gl_fragment_program(clone
);
541 fpc
->UsesKill
= fp
->UsesKill
;
542 fpc
->UsesDFdy
= fp
->UsesDFdy
;
543 fpc
->OriginUpperLeft
= fp
->OriginUpperLeft
;
544 fpc
->PixelCenterInteger
= fp
->PixelCenterInteger
;
547 case MESA_GEOMETRY_PROGRAM
:
549 const struct gl_geometry_program
*gp
= gl_geometry_program_const(prog
);
550 struct gl_geometry_program
*gpc
= gl_geometry_program(clone
);
551 gpc
->VerticesOut
= gp
->VerticesOut
;
552 gpc
->InputType
= gp
->InputType
;
553 gpc
->Invocations
= gp
->Invocations
;
554 gpc
->OutputType
= gp
->OutputType
;
555 gpc
->UsesEndPrimitive
= gp
->UsesEndPrimitive
;
559 _mesa_problem(NULL
, "Unexpected target in _mesa_clone_program");
567 * Insert 'count' NOP instructions at 'start' in the given program.
568 * Adjust branch targets accordingly.
571 _mesa_insert_instructions(struct gl_program
*prog
, GLuint start
, GLuint count
)
573 const GLuint origLen
= prog
->NumInstructions
;
574 const GLuint newLen
= origLen
+ count
;
575 struct prog_instruction
*newInst
;
578 /* adjust branches */
579 for (i
= 0; i
< prog
->NumInstructions
; i
++) {
580 struct prog_instruction
*inst
= prog
->Instructions
+ i
;
581 if (inst
->BranchTarget
> 0) {
582 if ((GLuint
)inst
->BranchTarget
>= start
) {
583 inst
->BranchTarget
+= count
;
588 /* Alloc storage for new instructions */
589 newInst
= _mesa_alloc_instructions(newLen
);
594 /* Copy 'start' instructions into new instruction buffer */
595 _mesa_copy_instructions(newInst
, prog
->Instructions
, start
);
597 /* init the new instructions */
598 _mesa_init_instructions(newInst
+ start
, count
);
600 /* Copy the remaining/tail instructions to new inst buffer */
601 _mesa_copy_instructions(newInst
+ start
+ count
,
602 prog
->Instructions
+ start
,
605 /* free old instructions */
606 _mesa_free_instructions(prog
->Instructions
, origLen
);
608 /* install new instructions */
609 prog
->Instructions
= newInst
;
610 prog
->NumInstructions
= newLen
;
616 * Delete 'count' instructions at 'start' in the given program.
617 * Adjust branch targets accordingly.
620 _mesa_delete_instructions(struct gl_program
*prog
, GLuint start
, GLuint count
)
622 const GLuint origLen
= prog
->NumInstructions
;
623 const GLuint newLen
= origLen
- count
;
624 struct prog_instruction
*newInst
;
627 /* adjust branches */
628 for (i
= 0; i
< prog
->NumInstructions
; i
++) {
629 struct prog_instruction
*inst
= prog
->Instructions
+ i
;
630 if (inst
->BranchTarget
> 0) {
631 if (inst
->BranchTarget
> (GLint
) start
) {
632 inst
->BranchTarget
-= count
;
637 /* Alloc storage for new instructions */
638 newInst
= _mesa_alloc_instructions(newLen
);
643 /* Copy 'start' instructions into new instruction buffer */
644 _mesa_copy_instructions(newInst
, prog
->Instructions
, start
);
646 /* Copy the remaining/tail instructions to new inst buffer */
647 _mesa_copy_instructions(newInst
+ start
,
648 prog
->Instructions
+ start
+ count
,
651 /* free old instructions */
652 _mesa_free_instructions(prog
->Instructions
, origLen
);
654 /* install new instructions */
655 prog
->Instructions
= newInst
;
656 prog
->NumInstructions
= newLen
;
663 * Search instructions for registers that match (oldFile, oldIndex),
664 * replacing them with (newFile, newIndex).
667 replace_registers(struct prog_instruction
*inst
, GLuint numInst
,
668 GLuint oldFile
, GLuint oldIndex
,
669 GLuint newFile
, GLuint newIndex
)
672 for (i
= 0; i
< numInst
; i
++) {
674 for (j
= 0; j
< _mesa_num_inst_src_regs(inst
[i
].Opcode
); j
++) {
675 if (inst
[i
].SrcReg
[j
].File
== oldFile
&&
676 inst
[i
].SrcReg
[j
].Index
== oldIndex
) {
677 inst
[i
].SrcReg
[j
].File
= newFile
;
678 inst
[i
].SrcReg
[j
].Index
= newIndex
;
682 if (inst
[i
].DstReg
.File
== oldFile
&& inst
[i
].DstReg
.Index
== oldIndex
) {
683 inst
[i
].DstReg
.File
= newFile
;
684 inst
[i
].DstReg
.Index
= newIndex
;
691 * Search instructions for references to program parameters. When found,
692 * increment the parameter index by 'offset'.
693 * Used when combining programs.
696 adjust_param_indexes(struct prog_instruction
*inst
, GLuint numInst
,
700 for (i
= 0; i
< numInst
; i
++) {
701 for (j
= 0; j
< _mesa_num_inst_src_regs(inst
[i
].Opcode
); j
++) {
702 GLuint f
= inst
[i
].SrcReg
[j
].File
;
703 if (f
== PROGRAM_CONSTANT
||
704 f
== PROGRAM_UNIFORM
||
705 f
== PROGRAM_STATE_VAR
) {
706 inst
[i
].SrcReg
[j
].Index
+= offset
;
714 * Combine two programs into one. Fix instructions so the outputs of
715 * the first program go to the inputs of the second program.
718 _mesa_combine_programs(struct gl_context
*ctx
,
719 const struct gl_program
*progA
,
720 const struct gl_program
*progB
)
722 struct prog_instruction
*newInst
;
723 struct gl_program
*newProg
;
724 const GLuint lenA
= progA
->NumInstructions
- 1; /* omit END instr */
725 const GLuint lenB
= progB
->NumInstructions
;
726 const GLuint numParamsA
= _mesa_num_parameters(progA
->Parameters
);
727 const GLuint newLength
= lenA
+ lenB
;
728 GLboolean usedTemps
[MAX_PROGRAM_TEMPS
];
729 GLuint firstTemp
= 0;
730 GLbitfield64 inputsB
;
733 ASSERT(progA
->Target
== progB
->Target
);
735 newInst
= _mesa_alloc_instructions(newLength
);
739 _mesa_copy_instructions(newInst
, progA
->Instructions
, lenA
);
740 _mesa_copy_instructions(newInst
+ lenA
, progB
->Instructions
, lenB
);
742 /* adjust branch / instruction addresses for B's instructions */
743 for (i
= 0; i
< lenB
; i
++) {
744 newInst
[lenA
+ i
].BranchTarget
+= lenA
;
747 newProg
= ctx
->Driver
.NewProgram(ctx
, progA
->Target
, 0);
748 newProg
->Instructions
= newInst
;
749 newProg
->NumInstructions
= newLength
;
751 /* find used temp regs (we may need new temps below) */
752 _mesa_find_used_registers(newProg
, PROGRAM_TEMPORARY
,
753 usedTemps
, MAX_PROGRAM_TEMPS
);
755 if (newProg
->Target
== GL_FRAGMENT_PROGRAM_ARB
) {
756 const struct gl_fragment_program
*fprogA
, *fprogB
;
757 struct gl_fragment_program
*newFprog
;
758 GLbitfield64 progB_inputsRead
= progB
->InputsRead
;
759 GLint progB_colorFile
, progB_colorIndex
;
761 fprogA
= gl_fragment_program_const(progA
);
762 fprogB
= gl_fragment_program_const(progB
);
763 newFprog
= gl_fragment_program(newProg
);
765 newFprog
->UsesKill
= fprogA
->UsesKill
|| fprogB
->UsesKill
;
766 newFprog
->UsesDFdy
= fprogA
->UsesDFdy
|| fprogB
->UsesDFdy
;
768 /* We'll do a search and replace for instances
769 * of progB_colorFile/progB_colorIndex below...
771 progB_colorFile
= PROGRAM_INPUT
;
772 progB_colorIndex
= VARYING_SLOT_COL0
;
775 * The fragment program may get color from a state var rather than
776 * a fragment input (vertex output) if it's constant.
777 * See the texenvprogram.c code.
778 * So, search the program's parameter list now to see if the program
779 * gets color from a state var instead of a conventional fragment
782 for (i
= 0; i
< progB
->Parameters
->NumParameters
; i
++) {
783 struct gl_program_parameter
*p
= &progB
->Parameters
->Parameters
[i
];
784 if (p
->Type
== PROGRAM_STATE_VAR
&&
785 p
->StateIndexes
[0] == STATE_INTERNAL
&&
786 p
->StateIndexes
[1] == STATE_CURRENT_ATTRIB
&&
787 (int) p
->StateIndexes
[2] == (int) VERT_ATTRIB_COLOR0
) {
788 progB_inputsRead
|= VARYING_BIT_COL0
;
789 progB_colorFile
= PROGRAM_STATE_VAR
;
790 progB_colorIndex
= i
;
795 /* Connect color outputs of fprogA to color inputs of fprogB, via a
796 * new temporary register.
798 if ((progA
->OutputsWritten
& BITFIELD64_BIT(FRAG_RESULT_COLOR
)) &&
799 (progB_inputsRead
& VARYING_BIT_COL0
)) {
800 GLint tempReg
= _mesa_find_free_register(usedTemps
, MAX_PROGRAM_TEMPS
,
803 _mesa_problem(ctx
, "No free temp regs found in "
804 "_mesa_combine_programs(), using 31");
807 firstTemp
= tempReg
+ 1;
809 /* replace writes to result.color[0] with tempReg */
810 replace_registers(newInst
, lenA
,
811 PROGRAM_OUTPUT
, FRAG_RESULT_COLOR
,
812 PROGRAM_TEMPORARY
, tempReg
);
813 /* replace reads from the input color with tempReg */
814 replace_registers(newInst
+ lenA
, lenB
,
815 progB_colorFile
, progB_colorIndex
, /* search for */
816 PROGRAM_TEMPORARY
, tempReg
/* replace with */ );
819 /* compute combined program's InputsRead */
820 inputsB
= progB_inputsRead
;
821 if (progA
->OutputsWritten
& BITFIELD64_BIT(FRAG_RESULT_COLOR
)) {
822 inputsB
&= ~(1 << VARYING_SLOT_COL0
);
824 newProg
->InputsRead
= progA
->InputsRead
| inputsB
;
825 newProg
->OutputsWritten
= progB
->OutputsWritten
;
826 newProg
->SamplersUsed
= progA
->SamplersUsed
| progB
->SamplersUsed
;
830 assert(0); /* XXX todo */
834 * Merge parameters (uniforms, constants, etc)
836 newProg
->Parameters
= _mesa_combine_parameter_lists(progA
->Parameters
,
839 adjust_param_indexes(newInst
+ lenA
, lenB
, numParamsA
);
847 * Populate the 'used' array with flags indicating which registers (TEMPs,
848 * INPUTs, OUTPUTs, etc, are used by the given program.
849 * \param file type of register to scan for
850 * \param used returns true/false flags for in use / free
851 * \param usedSize size of the 'used' array
854 _mesa_find_used_registers(const struct gl_program
*prog
,
855 gl_register_file file
,
856 GLboolean used
[], GLuint usedSize
)
860 memset(used
, 0, usedSize
);
862 for (i
= 0; i
< prog
->NumInstructions
; i
++) {
863 const struct prog_instruction
*inst
= prog
->Instructions
+ i
;
864 const GLuint n
= _mesa_num_inst_src_regs(inst
->Opcode
);
866 if (inst
->DstReg
.File
== file
) {
867 ASSERT(inst
->DstReg
.Index
< usedSize
);
868 if(inst
->DstReg
.Index
< usedSize
)
869 used
[inst
->DstReg
.Index
] = GL_TRUE
;
872 for (j
= 0; j
< n
; j
++) {
873 if (inst
->SrcReg
[j
].File
== file
) {
874 ASSERT(inst
->SrcReg
[j
].Index
< (GLint
) usedSize
);
875 if (inst
->SrcReg
[j
].Index
< (GLint
) usedSize
)
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
++)
910 * Check if the given register index is valid (doesn't exceed implementation-
912 * \return GL_TRUE if OK, GL_FALSE if bad index
915 _mesa_valid_register_index(const struct gl_context
*ctx
,
916 gl_shader_stage shaderType
,
917 gl_register_file file
, GLint index
)
919 const struct gl_program_constants
*c
;
921 assert(0 <= shaderType
&& shaderType
< MESA_SHADER_STAGES
);
922 c
= &ctx
->Const
.Program
[shaderType
];
925 case PROGRAM_UNDEFINED
:
926 return GL_TRUE
; /* XXX or maybe false? */
928 case PROGRAM_TEMPORARY
:
929 return index
>= 0 && index
< (GLint
) c
->MaxTemps
;
931 case PROGRAM_UNIFORM
:
932 case PROGRAM_STATE_VAR
:
933 /* aka constant buffer */
934 return index
>= 0 && index
< (GLint
) c
->MaxUniformComponents
/ 4;
936 case PROGRAM_CONSTANT
:
937 /* constant buffer w/ possible relative negative addressing */
938 return (index
> (int) c
->MaxUniformComponents
/ -4 &&
939 index
< (int) c
->MaxUniformComponents
/ 4);
945 switch (shaderType
) {
946 case MESA_SHADER_VERTEX
:
947 return index
< VERT_ATTRIB_GENERIC0
+ (GLint
) c
->MaxAttribs
;
948 case MESA_SHADER_FRAGMENT
:
949 return index
< VARYING_SLOT_VAR0
+ (GLint
) ctx
->Const
.MaxVarying
;
950 case MESA_SHADER_GEOMETRY
:
951 return index
< VARYING_SLOT_VAR0
+ (GLint
) ctx
->Const
.MaxVarying
;
960 switch (shaderType
) {
961 case MESA_SHADER_VERTEX
:
962 return index
< VARYING_SLOT_VAR0
+ (GLint
) ctx
->Const
.MaxVarying
;
963 case MESA_SHADER_FRAGMENT
:
964 return index
< FRAG_RESULT_DATA0
+ (GLint
) ctx
->Const
.MaxDrawBuffers
;
965 case MESA_SHADER_GEOMETRY
:
966 return index
< VARYING_SLOT_VAR0
+ (GLint
) ctx
->Const
.MaxVarying
;
971 case PROGRAM_ADDRESS
:
972 return index
>= 0 && index
< (GLint
) c
->MaxAddressRegs
;
976 "unexpected register file in _mesa_valid_register_index()");
984 * "Post-process" a GPU program. This is intended to be used for debugging.
985 * Example actions include no-op'ing instructions or changing instruction
989 _mesa_postprocess_program(struct gl_context
*ctx
, struct gl_program
*prog
)
991 static const GLfloat white
[4] = { 0.5, 0.5, 0.5, 0.5 };
994 GLint whiteIndex
= _mesa_add_unnamed_constant(prog
->Parameters
,
995 (gl_constant_value
*) white
,
1000 for (i
= 0; i
< prog
->NumInstructions
; i
++) {
1001 struct prog_instruction
*inst
= prog
->Instructions
+ i
;
1002 const GLuint n
= _mesa_num_inst_src_regs(inst
->Opcode
);
1006 if (_mesa_is_tex_instruction(inst
->Opcode
)) {
1008 /* replace TEX/TXP/TXB with MOV */
1009 inst
->Opcode
= OPCODE_MOV
;
1010 inst
->DstReg
.WriteMask
= WRITEMASK_XYZW
;
1011 inst
->SrcReg
[0].Swizzle
= SWIZZLE_XYZW
;
1012 inst
->SrcReg
[0].Negate
= NEGATE_NONE
;
1016 /* disable shadow texture mode */
1017 inst
->TexShadow
= 0;
1021 if (inst
->Opcode
== OPCODE_TXP
) {
1023 inst
->Opcode
= OPCODE_MOV
;
1024 inst
->DstReg
.WriteMask
= WRITEMASK_XYZW
;
1025 inst
->SrcReg
[0].File
= PROGRAM_CONSTANT
;
1026 inst
->SrcReg
[0].Index
= whiteIndex
;
1027 inst
->SrcReg
[0].Swizzle
= SWIZZLE_XYZW
;
1028 inst
->SrcReg
[0].Negate
= NEGATE_NONE
;
1031 inst
->TexShadow
= 0;
1034 inst
->Opcode
= OPCODE_TEX
;
1035 inst
->TexShadow
= 0;
1042 /* Gets the minimum number of shader invocations per fragment.
1043 * This function is useful to determine if we need to do per
1044 * sample shading or per fragment shading.
1047 _mesa_get_min_invocations_per_fragment(struct gl_context
*ctx
,
1048 const struct gl_fragment_program
*prog
,
1049 bool ignore_sample_qualifier
)
1051 /* From ARB_sample_shading specification:
1052 * "Using gl_SampleID in a fragment shader causes the entire shader
1053 * to be evaluated per-sample."
1055 * "Using gl_SamplePosition in a fragment shader causes the entire
1056 * shader to be evaluated per-sample."
1058 * "If MULTISAMPLE or SAMPLE_SHADING_ARB is disabled, sample shading
1061 if (ctx
->Multisample
.Enabled
) {
1062 /* The ARB_gpu_shader5 specification says:
1064 * "Use of the "sample" qualifier on a fragment shader input
1065 * forces per-sample shading"
1067 if (prog
->IsSample
&& !ignore_sample_qualifier
)
1068 return MAX2(ctx
->DrawBuffer
->Visual
.samples
, 1);
1070 if (prog
->Base
.SystemValuesRead
& (SYSTEM_BIT_SAMPLE_ID
|
1071 SYSTEM_BIT_SAMPLE_POS
))
1072 return MAX2(ctx
->DrawBuffer
->Visual
.samples
, 1);
1073 else if (ctx
->Multisample
.SampleShading
)
1074 return MAX2(ceil(ctx
->Multisample
.MinSampleShadingValue
*
1075 ctx
->DrawBuffer
->Visual
.samples
), 1);