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
= 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
,
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
);
124 /* XXX probably move this stuff */
125 if (ctx
->ATIFragmentShader
.Current
) {
126 ctx
->ATIFragmentShader
.Current
->RefCount
--;
127 if (ctx
->ATIFragmentShader
.Current
->RefCount
<= 0) {
128 free(ctx
->ATIFragmentShader
.Current
);
132 free((void *) ctx
->Program
.ErrorString
);
137 * Update the default program objects in the given context to reference those
138 * specified in the shared state and release those referencing the old
142 _mesa_update_default_objects_program(struct gl_context
*ctx
)
144 _mesa_reference_vertprog(ctx
, &ctx
->VertexProgram
.Current
,
145 ctx
->Shared
->DefaultVertexProgram
);
146 assert(ctx
->VertexProgram
.Current
);
148 _mesa_reference_fragprog(ctx
, &ctx
->FragmentProgram
.Current
,
149 ctx
->Shared
->DefaultFragmentProgram
);
150 assert(ctx
->FragmentProgram
.Current
);
152 _mesa_reference_geomprog(ctx
, &ctx
->GeometryProgram
.Current
,
153 ctx
->Shared
->DefaultGeometryProgram
);
155 /* XXX probably move this stuff */
156 if (ctx
->ATIFragmentShader
.Current
) {
157 ctx
->ATIFragmentShader
.Current
->RefCount
--;
158 if (ctx
->ATIFragmentShader
.Current
->RefCount
<= 0) {
159 free(ctx
->ATIFragmentShader
.Current
);
162 ctx
->ATIFragmentShader
.Current
= (struct ati_fragment_shader
*) ctx
->Shared
->DefaultFragmentShader
;
163 assert(ctx
->ATIFragmentShader
.Current
);
164 ctx
->ATIFragmentShader
.Current
->RefCount
++;
169 * Set the vertex/fragment program error state (position and error string).
170 * This is generally called from within the parsers.
173 _mesa_set_program_error(struct gl_context
*ctx
, GLint pos
, const char *string
)
175 ctx
->Program
.ErrorPos
= pos
;
176 free((void *) ctx
->Program
.ErrorString
);
179 ctx
->Program
.ErrorString
= strdup(string
);
184 * Find the line number and column for 'pos' within 'string'.
185 * Return a copy of the line which contains 'pos'. Free the line with
187 * \param string the program string
188 * \param pos the position within the string
189 * \param line returns the line number corresponding to 'pos'.
190 * \param col returns the column number corresponding to 'pos'.
191 * \return copy of the line containing 'pos'.
194 _mesa_find_line_column(const GLubyte
*string
, const GLubyte
*pos
,
195 GLint
*line
, GLint
*col
)
197 const GLubyte
*lineStart
= string
;
198 const GLubyte
*p
= string
;
205 if (*p
== (GLubyte
) '\n') {
212 *col
= (pos
- lineStart
) + 1;
214 /* return copy of this line */
215 while (*p
!= 0 && *p
!= '\n')
219 memcpy(s
, lineStart
, len
);
227 * Initialize a new gl_program object.
230 init_program_struct(struct gl_program
*prog
, GLenum target
, GLuint id
)
236 memset(prog
, 0, sizeof(*prog
));
238 prog
->Target
= target
;
240 prog
->Format
= GL_PROGRAM_FORMAT_ASCII_ARB
;
242 /* default mapping from samplers to texture units */
243 for (i
= 0; i
< MAX_SAMPLERS
; i
++)
244 prog
->SamplerUnits
[i
] = i
;
249 * Initialize a new fragment program object.
252 _mesa_init_fragment_program(struct gl_context
*ctx
,
253 struct gl_fragment_program
*prog
,
254 GLenum target
, GLuint id
)
257 init_program_struct(&prog
->Base
, target
, id
);
265 * Initialize a new vertex program object.
268 _mesa_init_vertex_program(struct gl_context
*ctx
,
269 struct gl_vertex_program
*prog
,
270 GLenum target
, GLuint id
)
273 init_program_struct(&prog
->Base
, target
, id
);
281 * Initialize a new compute program object.
284 _mesa_init_compute_program(struct gl_context
*ctx
,
285 struct gl_compute_program
*prog
,
286 GLenum target
, GLuint id
)
289 init_program_struct(&prog
->Base
, target
, id
);
297 * Initialize a new geometry program object.
300 _mesa_init_geometry_program(struct gl_context
*ctx
,
301 struct gl_geometry_program
*prog
,
302 GLenum target
, GLuint id
)
305 init_program_struct(&prog
->Base
, target
, id
);
313 * Allocate and initialize a new fragment/vertex program object but
314 * don't put it into the program hash table. Called via
315 * ctx->Driver.NewProgram. May be overridden (ie. replaced) by a
316 * device driver function to implement OO deriviation with additional
317 * types not understood by this function.
320 * \param id program id/number
321 * \param target program target/type
322 * \return pointer to new program object
325 _mesa_new_program(struct gl_context
*ctx
, GLenum target
, GLuint id
)
327 struct gl_program
*prog
;
329 case GL_VERTEX_PROGRAM_ARB
: /* == GL_VERTEX_PROGRAM_NV */
330 prog
= _mesa_init_vertex_program(ctx
, CALLOC_STRUCT(gl_vertex_program
),
333 case GL_FRAGMENT_PROGRAM_NV
:
334 case GL_FRAGMENT_PROGRAM_ARB
:
335 prog
=_mesa_init_fragment_program(ctx
,
336 CALLOC_STRUCT(gl_fragment_program
),
339 case MESA_GEOMETRY_PROGRAM
:
340 prog
= _mesa_init_geometry_program(ctx
,
341 CALLOC_STRUCT(gl_geometry_program
),
344 case GL_COMPUTE_PROGRAM_NV
:
345 prog
= _mesa_init_compute_program(ctx
,
346 CALLOC_STRUCT(gl_compute_program
),
350 _mesa_problem(ctx
, "bad target in _mesa_new_program");
358 * Delete a program and remove it from the hash table, ignoring the
360 * Called via ctx->Driver.DeleteProgram. May be wrapped (OO deriviation)
361 * by a device driver function.
364 _mesa_delete_program(struct gl_context
*ctx
, struct gl_program
*prog
)
368 assert(prog
->RefCount
==0);
370 if (prog
== &_mesa_DummyProgram
)
374 free(prog
->LocalParams
);
376 if (prog
->Instructions
) {
377 _mesa_free_instructions(prog
->Instructions
, prog
->NumInstructions
);
379 if (prog
->Parameters
) {
380 _mesa_free_parameter_list(prog
->Parameters
);
388 * Return the gl_program object for a given ID.
389 * Basically just a wrapper for _mesa_HashLookup() to avoid a lot of
393 _mesa_lookup_program(struct gl_context
*ctx
, GLuint id
)
396 return (struct gl_program
*) _mesa_HashLookup(ctx
->Shared
->Programs
, id
);
403 * Reference counting for vertex/fragment programs
404 * This is normally only called from the _mesa_reference_program() macro
405 * when there's a real pointer change.
408 _mesa_reference_program_(struct gl_context
*ctx
,
409 struct gl_program
**ptr
,
410 struct gl_program
*prog
)
416 if ((*ptr
)->Target
== GL_VERTEX_PROGRAM_ARB
)
417 assert(prog
->Target
== GL_VERTEX_PROGRAM_ARB
);
418 else if ((*ptr
)->Target
== GL_FRAGMENT_PROGRAM_ARB
)
419 assert(prog
->Target
== GL_FRAGMENT_PROGRAM_ARB
||
420 prog
->Target
== GL_FRAGMENT_PROGRAM_NV
);
421 else if ((*ptr
)->Target
== MESA_GEOMETRY_PROGRAM
)
422 assert(prog
->Target
== MESA_GEOMETRY_PROGRAM
);
427 GLboolean deleteFlag
;
429 /*mtx_lock(&(*ptr)->Mutex);*/
431 printf("Program %p ID=%u Target=%s Refcount-- to %d\n",
433 ((*ptr
)->Target
== GL_VERTEX_PROGRAM_ARB
? "VP" :
434 ((*ptr
)->Target
== MESA_GEOMETRY_PROGRAM
? "GP" : "FP")),
435 (*ptr
)->RefCount
- 1);
437 assert((*ptr
)->RefCount
> 0);
440 deleteFlag
= ((*ptr
)->RefCount
== 0);
441 /*mtx_lock(&(*ptr)->Mutex);*/
445 ctx
->Driver
.DeleteProgram(ctx
, *ptr
);
453 /*mtx_lock(&prog->Mutex);*/
456 printf("Program %p ID=%u Target=%s Refcount++ to %d\n",
458 (prog
->Target
== GL_VERTEX_PROGRAM_ARB
? "VP" :
459 (prog
->Target
== MESA_GEOMETRY_PROGRAM
? "GP" : "FP")),
462 /*mtx_unlock(&prog->Mutex);*/
470 * Return a copy of a program.
471 * XXX Problem here if the program object is actually OO-derivation
472 * made by a device driver.
475 _mesa_clone_program(struct gl_context
*ctx
, const struct gl_program
*prog
)
477 struct gl_program
*clone
;
479 clone
= ctx
->Driver
.NewProgram(ctx
, prog
->Target
, prog
->Id
);
483 assert(clone
->Target
== prog
->Target
);
484 assert(clone
->RefCount
== 1);
486 clone
->String
= (GLubyte
*) strdup((char *) prog
->String
);
487 clone
->Format
= prog
->Format
;
488 clone
->Instructions
= _mesa_alloc_instructions(prog
->NumInstructions
);
489 if (!clone
->Instructions
) {
490 _mesa_reference_program(ctx
, &clone
, NULL
);
493 _mesa_copy_instructions(clone
->Instructions
, prog
->Instructions
,
494 prog
->NumInstructions
);
495 clone
->InputsRead
= prog
->InputsRead
;
496 clone
->OutputsWritten
= prog
->OutputsWritten
;
497 clone
->SamplersUsed
= prog
->SamplersUsed
;
498 clone
->ShadowSamplers
= prog
->ShadowSamplers
;
499 memcpy(clone
->TexturesUsed
, prog
->TexturesUsed
, sizeof(prog
->TexturesUsed
));
501 if (prog
->Parameters
)
502 clone
->Parameters
= _mesa_clone_parameter_list(prog
->Parameters
);
503 if (prog
->LocalParams
) {
504 clone
->LocalParams
= malloc(MAX_PROGRAM_LOCAL_PARAMS
*
506 if (!clone
->LocalParams
) {
507 _mesa_reference_program(ctx
, &clone
, NULL
);
510 memcpy(clone
->LocalParams
, prog
->LocalParams
,
511 MAX_PROGRAM_LOCAL_PARAMS
* sizeof(float[4]));
513 clone
->IndirectRegisterFiles
= prog
->IndirectRegisterFiles
;
514 clone
->NumInstructions
= prog
->NumInstructions
;
515 clone
->NumTemporaries
= prog
->NumTemporaries
;
516 clone
->NumParameters
= prog
->NumParameters
;
517 clone
->NumAttributes
= prog
->NumAttributes
;
518 clone
->NumAddressRegs
= prog
->NumAddressRegs
;
519 clone
->NumNativeInstructions
= prog
->NumNativeInstructions
;
520 clone
->NumNativeTemporaries
= prog
->NumNativeTemporaries
;
521 clone
->NumNativeParameters
= prog
->NumNativeParameters
;
522 clone
->NumNativeAttributes
= prog
->NumNativeAttributes
;
523 clone
->NumNativeAddressRegs
= prog
->NumNativeAddressRegs
;
524 clone
->NumAluInstructions
= prog
->NumAluInstructions
;
525 clone
->NumTexInstructions
= prog
->NumTexInstructions
;
526 clone
->NumTexIndirections
= prog
->NumTexIndirections
;
527 clone
->NumNativeAluInstructions
= prog
->NumNativeAluInstructions
;
528 clone
->NumNativeTexInstructions
= prog
->NumNativeTexInstructions
;
529 clone
->NumNativeTexIndirections
= prog
->NumNativeTexIndirections
;
531 switch (prog
->Target
) {
532 case GL_VERTEX_PROGRAM_ARB
:
534 const struct gl_vertex_program
*vp
= gl_vertex_program_const(prog
);
535 struct gl_vertex_program
*vpc
= gl_vertex_program(clone
);
536 vpc
->IsPositionInvariant
= vp
->IsPositionInvariant
;
539 case GL_FRAGMENT_PROGRAM_ARB
:
541 const struct gl_fragment_program
*fp
= gl_fragment_program_const(prog
);
542 struct gl_fragment_program
*fpc
= gl_fragment_program(clone
);
543 fpc
->UsesKill
= fp
->UsesKill
;
544 fpc
->UsesDFdy
= fp
->UsesDFdy
;
545 fpc
->OriginUpperLeft
= fp
->OriginUpperLeft
;
546 fpc
->PixelCenterInteger
= fp
->PixelCenterInteger
;
549 case MESA_GEOMETRY_PROGRAM
:
551 const struct gl_geometry_program
*gp
= gl_geometry_program_const(prog
);
552 struct gl_geometry_program
*gpc
= gl_geometry_program(clone
);
553 gpc
->VerticesOut
= gp
->VerticesOut
;
554 gpc
->InputType
= gp
->InputType
;
555 gpc
->Invocations
= gp
->Invocations
;
556 gpc
->OutputType
= gp
->OutputType
;
557 gpc
->UsesEndPrimitive
= gp
->UsesEndPrimitive
;
558 gpc
->UsesStreams
= gp
->UsesStreams
;
562 _mesa_problem(NULL
, "Unexpected target in _mesa_clone_program");
570 * Insert 'count' NOP instructions at 'start' in the given program.
571 * Adjust branch targets accordingly.
574 _mesa_insert_instructions(struct gl_program
*prog
, GLuint start
, GLuint count
)
576 const GLuint origLen
= prog
->NumInstructions
;
577 const GLuint newLen
= origLen
+ count
;
578 struct prog_instruction
*newInst
;
581 /* adjust branches */
582 for (i
= 0; i
< prog
->NumInstructions
; i
++) {
583 struct prog_instruction
*inst
= prog
->Instructions
+ i
;
584 if (inst
->BranchTarget
> 0) {
585 if ((GLuint
)inst
->BranchTarget
>= start
) {
586 inst
->BranchTarget
+= count
;
591 /* Alloc storage for new instructions */
592 newInst
= _mesa_alloc_instructions(newLen
);
597 /* Copy 'start' instructions into new instruction buffer */
598 _mesa_copy_instructions(newInst
, prog
->Instructions
, start
);
600 /* init the new instructions */
601 _mesa_init_instructions(newInst
+ start
, count
);
603 /* Copy the remaining/tail instructions to new inst buffer */
604 _mesa_copy_instructions(newInst
+ start
+ count
,
605 prog
->Instructions
+ start
,
608 /* free old instructions */
609 _mesa_free_instructions(prog
->Instructions
, origLen
);
611 /* install new instructions */
612 prog
->Instructions
= newInst
;
613 prog
->NumInstructions
= newLen
;
619 * Delete 'count' instructions at 'start' in the given program.
620 * Adjust branch targets accordingly.
623 _mesa_delete_instructions(struct gl_program
*prog
, GLuint start
, GLuint count
)
625 const GLuint origLen
= prog
->NumInstructions
;
626 const GLuint newLen
= origLen
- count
;
627 struct prog_instruction
*newInst
;
630 /* adjust branches */
631 for (i
= 0; i
< prog
->NumInstructions
; i
++) {
632 struct prog_instruction
*inst
= prog
->Instructions
+ i
;
633 if (inst
->BranchTarget
> 0) {
634 if (inst
->BranchTarget
> (GLint
) start
) {
635 inst
->BranchTarget
-= count
;
640 /* Alloc storage for new instructions */
641 newInst
= _mesa_alloc_instructions(newLen
);
646 /* Copy 'start' instructions into new instruction buffer */
647 _mesa_copy_instructions(newInst
, prog
->Instructions
, start
);
649 /* Copy the remaining/tail instructions to new inst buffer */
650 _mesa_copy_instructions(newInst
+ start
,
651 prog
->Instructions
+ start
+ count
,
654 /* free old instructions */
655 _mesa_free_instructions(prog
->Instructions
, origLen
);
657 /* install new instructions */
658 prog
->Instructions
= newInst
;
659 prog
->NumInstructions
= newLen
;
666 * Search instructions for registers that match (oldFile, oldIndex),
667 * replacing them with (newFile, newIndex).
670 replace_registers(struct prog_instruction
*inst
, GLuint numInst
,
671 GLuint oldFile
, GLuint oldIndex
,
672 GLuint newFile
, GLuint newIndex
)
675 for (i
= 0; i
< numInst
; i
++) {
677 for (j
= 0; j
< _mesa_num_inst_src_regs(inst
[i
].Opcode
); j
++) {
678 if (inst
[i
].SrcReg
[j
].File
== oldFile
&&
679 inst
[i
].SrcReg
[j
].Index
== oldIndex
) {
680 inst
[i
].SrcReg
[j
].File
= newFile
;
681 inst
[i
].SrcReg
[j
].Index
= newIndex
;
685 if (inst
[i
].DstReg
.File
== oldFile
&& inst
[i
].DstReg
.Index
== oldIndex
) {
686 inst
[i
].DstReg
.File
= newFile
;
687 inst
[i
].DstReg
.Index
= newIndex
;
694 * Search instructions for references to program parameters. When found,
695 * increment the parameter index by 'offset'.
696 * Used when combining programs.
699 adjust_param_indexes(struct prog_instruction
*inst
, GLuint numInst
,
703 for (i
= 0; i
< numInst
; i
++) {
704 for (j
= 0; j
< _mesa_num_inst_src_regs(inst
[i
].Opcode
); j
++) {
705 GLuint f
= inst
[i
].SrcReg
[j
].File
;
706 if (f
== PROGRAM_CONSTANT
||
707 f
== PROGRAM_UNIFORM
||
708 f
== PROGRAM_STATE_VAR
) {
709 inst
[i
].SrcReg
[j
].Index
+= offset
;
717 * Combine two programs into one. Fix instructions so the outputs of
718 * the first program go to the inputs of the second program.
721 _mesa_combine_programs(struct gl_context
*ctx
,
722 const struct gl_program
*progA
,
723 const struct gl_program
*progB
)
725 struct prog_instruction
*newInst
;
726 struct gl_program
*newProg
;
727 const GLuint lenA
= progA
->NumInstructions
- 1; /* omit END instr */
728 const GLuint lenB
= progB
->NumInstructions
;
729 const GLuint numParamsA
= _mesa_num_parameters(progA
->Parameters
);
730 const GLuint newLength
= lenA
+ lenB
;
731 GLboolean usedTemps
[MAX_PROGRAM_TEMPS
];
732 GLuint firstTemp
= 0;
733 GLbitfield64 inputsB
;
736 assert(progA
->Target
== progB
->Target
);
738 newInst
= _mesa_alloc_instructions(newLength
);
742 _mesa_copy_instructions(newInst
, progA
->Instructions
, lenA
);
743 _mesa_copy_instructions(newInst
+ lenA
, progB
->Instructions
, lenB
);
745 /* adjust branch / instruction addresses for B's instructions */
746 for (i
= 0; i
< lenB
; i
++) {
747 newInst
[lenA
+ i
].BranchTarget
+= lenA
;
750 newProg
= ctx
->Driver
.NewProgram(ctx
, progA
->Target
, 0);
751 newProg
->Instructions
= newInst
;
752 newProg
->NumInstructions
= newLength
;
754 /* find used temp regs (we may need new temps below) */
755 _mesa_find_used_registers(newProg
, PROGRAM_TEMPORARY
,
756 usedTemps
, MAX_PROGRAM_TEMPS
);
758 if (newProg
->Target
== GL_FRAGMENT_PROGRAM_ARB
) {
759 const struct gl_fragment_program
*fprogA
, *fprogB
;
760 struct gl_fragment_program
*newFprog
;
761 GLbitfield64 progB_inputsRead
= progB
->InputsRead
;
762 GLint progB_colorFile
, progB_colorIndex
;
764 fprogA
= gl_fragment_program_const(progA
);
765 fprogB
= gl_fragment_program_const(progB
);
766 newFprog
= gl_fragment_program(newProg
);
768 newFprog
->UsesKill
= fprogA
->UsesKill
|| fprogB
->UsesKill
;
769 newFprog
->UsesDFdy
= fprogA
->UsesDFdy
|| fprogB
->UsesDFdy
;
771 /* We'll do a search and replace for instances
772 * of progB_colorFile/progB_colorIndex below...
774 progB_colorFile
= PROGRAM_INPUT
;
775 progB_colorIndex
= VARYING_SLOT_COL0
;
778 * The fragment program may get color from a state var rather than
779 * a fragment input (vertex output) if it's constant.
780 * See the texenvprogram.c code.
781 * So, search the program's parameter list now to see if the program
782 * gets color from a state var instead of a conventional fragment
785 for (i
= 0; i
< progB
->Parameters
->NumParameters
; i
++) {
786 struct gl_program_parameter
*p
= &progB
->Parameters
->Parameters
[i
];
787 if (p
->Type
== PROGRAM_STATE_VAR
&&
788 p
->StateIndexes
[0] == STATE_INTERNAL
&&
789 p
->StateIndexes
[1] == STATE_CURRENT_ATTRIB
&&
790 (int) p
->StateIndexes
[2] == (int) VERT_ATTRIB_COLOR0
) {
791 progB_inputsRead
|= VARYING_BIT_COL0
;
792 progB_colorFile
= PROGRAM_STATE_VAR
;
793 progB_colorIndex
= i
;
798 /* Connect color outputs of fprogA to color inputs of fprogB, via a
799 * new temporary register.
801 if ((progA
->OutputsWritten
& BITFIELD64_BIT(FRAG_RESULT_COLOR
)) &&
802 (progB_inputsRead
& VARYING_BIT_COL0
)) {
803 GLint tempReg
= _mesa_find_free_register(usedTemps
, MAX_PROGRAM_TEMPS
,
806 _mesa_problem(ctx
, "No free temp regs found in "
807 "_mesa_combine_programs(), using 31");
810 firstTemp
= tempReg
+ 1;
812 /* replace writes to result.color[0] with tempReg */
813 replace_registers(newInst
, lenA
,
814 PROGRAM_OUTPUT
, FRAG_RESULT_COLOR
,
815 PROGRAM_TEMPORARY
, tempReg
);
816 /* replace reads from the input color with tempReg */
817 replace_registers(newInst
+ lenA
, lenB
,
818 progB_colorFile
, progB_colorIndex
, /* search for */
819 PROGRAM_TEMPORARY
, tempReg
/* replace with */ );
822 /* compute combined program's InputsRead */
823 inputsB
= progB_inputsRead
;
824 if (progA
->OutputsWritten
& BITFIELD64_BIT(FRAG_RESULT_COLOR
)) {
825 inputsB
&= ~(1 << VARYING_SLOT_COL0
);
827 newProg
->InputsRead
= progA
->InputsRead
| inputsB
;
828 newProg
->OutputsWritten
= progB
->OutputsWritten
;
829 newProg
->SamplersUsed
= progA
->SamplersUsed
| progB
->SamplersUsed
;
833 assert(0); /* XXX todo */
837 * Merge parameters (uniforms, constants, etc)
839 newProg
->Parameters
= _mesa_combine_parameter_lists(progA
->Parameters
,
842 adjust_param_indexes(newInst
+ lenA
, lenB
, numParamsA
);
850 * Populate the 'used' array with flags indicating which registers (TEMPs,
851 * INPUTs, OUTPUTs, etc, are used by the given program.
852 * \param file type of register to scan for
853 * \param used returns true/false flags for in use / free
854 * \param usedSize size of the 'used' array
857 _mesa_find_used_registers(const struct gl_program
*prog
,
858 gl_register_file file
,
859 GLboolean used
[], GLuint usedSize
)
863 memset(used
, 0, usedSize
);
865 for (i
= 0; i
< prog
->NumInstructions
; i
++) {
866 const struct prog_instruction
*inst
= prog
->Instructions
+ i
;
867 const GLuint n
= _mesa_num_inst_src_regs(inst
->Opcode
);
869 if (inst
->DstReg
.File
== file
) {
870 assert(inst
->DstReg
.Index
< usedSize
);
871 if(inst
->DstReg
.Index
< usedSize
)
872 used
[inst
->DstReg
.Index
] = GL_TRUE
;
875 for (j
= 0; j
< n
; j
++) {
876 if (inst
->SrcReg
[j
].File
== file
) {
877 assert(inst
->SrcReg
[j
].Index
< (GLint
) usedSize
);
878 if (inst
->SrcReg
[j
].Index
< (GLint
) usedSize
)
879 used
[inst
->SrcReg
[j
].Index
] = GL_TRUE
;
887 * Scan the given 'used' register flag array for the first entry
888 * that's >= firstReg.
889 * \param used vector of flags indicating registers in use (as returned
890 * by _mesa_find_used_registers())
891 * \param usedSize size of the 'used' array
892 * \param firstReg first register to start searching at
893 * \return index of unused register, or -1 if none.
896 _mesa_find_free_register(const GLboolean used
[],
897 GLuint usedSize
, GLuint firstReg
)
901 assert(firstReg
< usedSize
);
903 for (i
= firstReg
; i
< usedSize
; i
++)
913 * Check if the given register index is valid (doesn't exceed implementation-
915 * \return GL_TRUE if OK, GL_FALSE if bad index
918 _mesa_valid_register_index(const struct gl_context
*ctx
,
919 gl_shader_stage shaderType
,
920 gl_register_file file
, GLint index
)
922 const struct gl_program_constants
*c
;
924 assert(0 <= shaderType
&& shaderType
< MESA_SHADER_STAGES
);
925 c
= &ctx
->Const
.Program
[shaderType
];
928 case PROGRAM_UNDEFINED
:
929 return GL_TRUE
; /* XXX or maybe false? */
931 case PROGRAM_TEMPORARY
:
932 return index
>= 0 && index
< (GLint
) c
->MaxTemps
;
934 case PROGRAM_UNIFORM
:
935 case PROGRAM_STATE_VAR
:
936 /* aka constant buffer */
937 return index
>= 0 && index
< (GLint
) c
->MaxUniformComponents
/ 4;
939 case PROGRAM_CONSTANT
:
940 /* constant buffer w/ possible relative negative addressing */
941 return (index
> (int) c
->MaxUniformComponents
/ -4 &&
942 index
< (int) c
->MaxUniformComponents
/ 4);
948 switch (shaderType
) {
949 case MESA_SHADER_VERTEX
:
950 return index
< VERT_ATTRIB_GENERIC0
+ (GLint
) c
->MaxAttribs
;
951 case MESA_SHADER_FRAGMENT
:
952 return index
< VARYING_SLOT_VAR0
+ (GLint
) ctx
->Const
.MaxVarying
;
953 case MESA_SHADER_GEOMETRY
:
954 return index
< VARYING_SLOT_VAR0
+ (GLint
) ctx
->Const
.MaxVarying
;
963 switch (shaderType
) {
964 case MESA_SHADER_VERTEX
:
965 return index
< VARYING_SLOT_VAR0
+ (GLint
) ctx
->Const
.MaxVarying
;
966 case MESA_SHADER_FRAGMENT
:
967 return index
< FRAG_RESULT_DATA0
+ (GLint
) ctx
->Const
.MaxDrawBuffers
;
968 case MESA_SHADER_GEOMETRY
:
969 return index
< VARYING_SLOT_VAR0
+ (GLint
) ctx
->Const
.MaxVarying
;
974 case PROGRAM_ADDRESS
:
975 return index
>= 0 && index
< (GLint
) c
->MaxAddressRegs
;
979 "unexpected register file in _mesa_valid_register_index()");
987 * "Post-process" a GPU program. This is intended to be used for debugging.
988 * Example actions include no-op'ing instructions or changing instruction
992 _mesa_postprocess_program(struct gl_context
*ctx
, struct gl_program
*prog
)
994 static const GLfloat white
[4] = { 0.5, 0.5, 0.5, 0.5 };
997 GLint whiteIndex
= _mesa_add_unnamed_constant(prog
->Parameters
,
998 (gl_constant_value
*) white
,
1003 for (i
= 0; i
< prog
->NumInstructions
; i
++) {
1004 struct prog_instruction
*inst
= prog
->Instructions
+ i
;
1005 const GLuint n
= _mesa_num_inst_src_regs(inst
->Opcode
);
1009 if (_mesa_is_tex_instruction(inst
->Opcode
)) {
1011 /* replace TEX/TXP/TXB with MOV */
1012 inst
->Opcode
= OPCODE_MOV
;
1013 inst
->DstReg
.WriteMask
= WRITEMASK_XYZW
;
1014 inst
->SrcReg
[0].Swizzle
= SWIZZLE_XYZW
;
1015 inst
->SrcReg
[0].Negate
= NEGATE_NONE
;
1019 /* disable shadow texture mode */
1020 inst
->TexShadow
= 0;
1024 if (inst
->Opcode
== OPCODE_TXP
) {
1026 inst
->Opcode
= OPCODE_MOV
;
1027 inst
->DstReg
.WriteMask
= WRITEMASK_XYZW
;
1028 inst
->SrcReg
[0].File
= PROGRAM_CONSTANT
;
1029 inst
->SrcReg
[0].Index
= whiteIndex
;
1030 inst
->SrcReg
[0].Swizzle
= SWIZZLE_XYZW
;
1031 inst
->SrcReg
[0].Negate
= NEGATE_NONE
;
1034 inst
->TexShadow
= 0;
1037 inst
->Opcode
= OPCODE_TEX
;
1038 inst
->TexShadow
= 0;
1045 /* Gets the minimum number of shader invocations per fragment.
1046 * This function is useful to determine if we need to do per
1047 * sample shading or per fragment shading.
1050 _mesa_get_min_invocations_per_fragment(struct gl_context
*ctx
,
1051 const struct gl_fragment_program
*prog
,
1052 bool ignore_sample_qualifier
)
1054 /* From ARB_sample_shading specification:
1055 * "Using gl_SampleID in a fragment shader causes the entire shader
1056 * to be evaluated per-sample."
1058 * "Using gl_SamplePosition in a fragment shader causes the entire
1059 * shader to be evaluated per-sample."
1061 * "If MULTISAMPLE or SAMPLE_SHADING_ARB is disabled, sample shading
1064 if (ctx
->Multisample
.Enabled
) {
1065 /* The ARB_gpu_shader5 specification says:
1067 * "Use of the "sample" qualifier on a fragment shader input
1068 * forces per-sample shading"
1070 if (prog
->IsSample
&& !ignore_sample_qualifier
)
1071 return MAX2(ctx
->DrawBuffer
->Visual
.samples
, 1);
1073 if (prog
->Base
.SystemValuesRead
& (SYSTEM_BIT_SAMPLE_ID
|
1074 SYSTEM_BIT_SAMPLE_POS
))
1075 return MAX2(ctx
->DrawBuffer
->Visual
.samples
, 1);
1076 else if (ctx
->Multisample
.SampleShading
)
1077 return MAX2(ceil(ctx
->Multisample
.MinSampleShadingValue
*
1078 ctx
->DrawBuffer
->Visual
.samples
), 1);