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"
40 #include "util/ralloc.h"
44 * A pointer to this dummy program is put into the hash table when
45 * glGenPrograms is called.
47 struct gl_program _mesa_DummyProgram
;
51 * Init context's vertex/fragment program state
54 _mesa_init_program(struct gl_context
*ctx
)
57 * If this assertion fails, we need to increase the field
58 * size for register indexes (see INST_INDEX_BITS).
60 assert(ctx
->Const
.Program
[MESA_SHADER_VERTEX
].MaxUniformComponents
/ 4
61 <= (1 << INST_INDEX_BITS
));
62 assert(ctx
->Const
.Program
[MESA_SHADER_FRAGMENT
].MaxUniformComponents
/ 4
63 <= (1 << INST_INDEX_BITS
));
65 assert(ctx
->Const
.Program
[MESA_SHADER_VERTEX
].MaxTemps
<= (1 << INST_INDEX_BITS
));
66 assert(ctx
->Const
.Program
[MESA_SHADER_VERTEX
].MaxLocalParams
<= (1 << INST_INDEX_BITS
));
67 assert(ctx
->Const
.Program
[MESA_SHADER_FRAGMENT
].MaxTemps
<= (1 << INST_INDEX_BITS
));
68 assert(ctx
->Const
.Program
[MESA_SHADER_FRAGMENT
].MaxLocalParams
<= (1 << INST_INDEX_BITS
));
70 assert(ctx
->Const
.Program
[MESA_SHADER_VERTEX
].MaxUniformComponents
<= 4 * MAX_UNIFORMS
);
71 assert(ctx
->Const
.Program
[MESA_SHADER_FRAGMENT
].MaxUniformComponents
<= 4 * MAX_UNIFORMS
);
73 assert(ctx
->Const
.Program
[MESA_SHADER_VERTEX
].MaxAddressOffset
<= (1 << INST_INDEX_BITS
));
74 assert(ctx
->Const
.Program
[MESA_SHADER_FRAGMENT
].MaxAddressOffset
<= (1 << INST_INDEX_BITS
));
76 /* If this fails, increase prog_instruction::TexSrcUnit size */
77 STATIC_ASSERT(MAX_TEXTURE_UNITS
<= (1 << 5));
79 /* If this fails, increase prog_instruction::TexSrcTarget size */
80 STATIC_ASSERT(NUM_TEXTURE_TARGETS
<= (1 << 4));
82 ctx
->Program
.ErrorPos
= -1;
83 ctx
->Program
.ErrorString
= strdup("");
85 ctx
->VertexProgram
.Enabled
= GL_FALSE
;
86 ctx
->VertexProgram
.PointSizeEnabled
=
87 (ctx
->API
== API_OPENGLES2
) ? GL_TRUE
: GL_FALSE
;
88 ctx
->VertexProgram
.TwoSideEnabled
= GL_FALSE
;
89 _mesa_reference_vertprog(ctx
, &ctx
->VertexProgram
.Current
,
90 ctx
->Shared
->DefaultVertexProgram
);
91 assert(ctx
->VertexProgram
.Current
);
92 ctx
->VertexProgram
.Cache
= _mesa_new_program_cache();
94 ctx
->FragmentProgram
.Enabled
= GL_FALSE
;
95 _mesa_reference_fragprog(ctx
, &ctx
->FragmentProgram
.Current
,
96 ctx
->Shared
->DefaultFragmentProgram
);
97 assert(ctx
->FragmentProgram
.Current
);
98 ctx
->FragmentProgram
.Cache
= _mesa_new_program_cache();
100 ctx
->GeometryProgram
.Enabled
= GL_FALSE
;
101 /* right now by default we don't have a geometry program */
102 _mesa_reference_geomprog(ctx
, &ctx
->GeometryProgram
.Current
,
105 _mesa_reference_compprog(ctx
, &ctx
->ComputeProgram
.Current
, NULL
);
107 /* XXX probably move this stuff */
108 ctx
->ATIFragmentShader
.Enabled
= GL_FALSE
;
109 ctx
->ATIFragmentShader
.Current
= ctx
->Shared
->DefaultFragmentShader
;
110 assert(ctx
->ATIFragmentShader
.Current
);
111 ctx
->ATIFragmentShader
.Current
->RefCount
++;
116 * Free a context's vertex/fragment program state
119 _mesa_free_program_data(struct gl_context
*ctx
)
121 _mesa_reference_vertprog(ctx
, &ctx
->VertexProgram
.Current
, NULL
);
122 _mesa_delete_program_cache(ctx
, ctx
->VertexProgram
.Cache
);
123 _mesa_reference_fragprog(ctx
, &ctx
->FragmentProgram
.Current
, NULL
);
124 _mesa_delete_shader_cache(ctx
, ctx
->FragmentProgram
.Cache
);
125 _mesa_reference_geomprog(ctx
, &ctx
->GeometryProgram
.Current
, NULL
);
126 _mesa_reference_compprog(ctx
, &ctx
->ComputeProgram
.Current
, NULL
);
128 /* XXX probably move this stuff */
129 if (ctx
->ATIFragmentShader
.Current
) {
130 ctx
->ATIFragmentShader
.Current
->RefCount
--;
131 if (ctx
->ATIFragmentShader
.Current
->RefCount
<= 0) {
132 free(ctx
->ATIFragmentShader
.Current
);
136 free((void *) ctx
->Program
.ErrorString
);
141 * Update the default program objects in the given context to reference those
142 * specified in the shared state and release those referencing the old
146 _mesa_update_default_objects_program(struct gl_context
*ctx
)
148 _mesa_reference_vertprog(ctx
, &ctx
->VertexProgram
.Current
,
149 ctx
->Shared
->DefaultVertexProgram
);
150 assert(ctx
->VertexProgram
.Current
);
152 _mesa_reference_fragprog(ctx
, &ctx
->FragmentProgram
.Current
,
153 ctx
->Shared
->DefaultFragmentProgram
);
154 assert(ctx
->FragmentProgram
.Current
);
156 _mesa_reference_geomprog(ctx
, &ctx
->GeometryProgram
.Current
,
157 ctx
->Shared
->DefaultGeometryProgram
);
159 /* XXX probably move this stuff */
160 if (ctx
->ATIFragmentShader
.Current
) {
161 ctx
->ATIFragmentShader
.Current
->RefCount
--;
162 if (ctx
->ATIFragmentShader
.Current
->RefCount
<= 0) {
163 free(ctx
->ATIFragmentShader
.Current
);
166 ctx
->ATIFragmentShader
.Current
= (struct ati_fragment_shader
*) ctx
->Shared
->DefaultFragmentShader
;
167 assert(ctx
->ATIFragmentShader
.Current
);
168 ctx
->ATIFragmentShader
.Current
->RefCount
++;
173 * Set the vertex/fragment program error state (position and error string).
174 * This is generally called from within the parsers.
177 _mesa_set_program_error(struct gl_context
*ctx
, GLint pos
, const char *string
)
179 ctx
->Program
.ErrorPos
= pos
;
180 free((void *) ctx
->Program
.ErrorString
);
183 ctx
->Program
.ErrorString
= strdup(string
);
188 * Find the line number and column for 'pos' within 'string'.
189 * Return a copy of the line which contains 'pos'. Free the line with
191 * \param string the program string
192 * \param pos the position within the string
193 * \param line returns the line number corresponding to 'pos'.
194 * \param col returns the column number corresponding to 'pos'.
195 * \return copy of the line containing 'pos'.
198 _mesa_find_line_column(const GLubyte
*string
, const GLubyte
*pos
,
199 GLint
*line
, GLint
*col
)
201 const GLubyte
*lineStart
= string
;
202 const GLubyte
*p
= string
;
209 if (*p
== (GLubyte
) '\n') {
216 *col
= (pos
- lineStart
) + 1;
218 /* return copy of this line */
219 while (*p
!= 0 && *p
!= '\n')
223 memcpy(s
, lineStart
, len
);
231 * Initialize a new gl_program object.
234 init_program_struct(struct gl_program
*prog
, GLenum target
, GLuint id
)
240 memset(prog
, 0, sizeof(*prog
));
242 prog
->Target
= target
;
244 prog
->Format
= GL_PROGRAM_FORMAT_ASCII_ARB
;
246 /* default mapping from samplers to texture units */
247 for (i
= 0; i
< MAX_SAMPLERS
; i
++)
248 prog
->SamplerUnits
[i
] = i
;
253 * Initialize a new fragment program object.
256 _mesa_init_fragment_program(struct gl_context
*ctx
,
257 struct gl_fragment_program
*prog
,
258 GLenum target
, GLuint id
)
261 init_program_struct(&prog
->Base
, target
, id
);
269 * Initialize a new vertex program object.
272 _mesa_init_vertex_program(struct gl_context
*ctx
,
273 struct gl_vertex_program
*prog
,
274 GLenum target
, GLuint id
)
277 init_program_struct(&prog
->Base
, target
, id
);
285 * Initialize a new compute program object.
288 _mesa_init_compute_program(struct gl_context
*ctx
,
289 struct gl_compute_program
*prog
,
290 GLenum target
, GLuint id
)
293 init_program_struct(&prog
->Base
, target
, id
);
301 * Initialize a new geometry program object.
304 _mesa_init_geometry_program(struct gl_context
*ctx
,
305 struct gl_geometry_program
*prog
,
306 GLenum target
, GLuint id
)
309 init_program_struct(&prog
->Base
, target
, id
);
317 * Allocate and initialize a new fragment/vertex program object but
318 * don't put it into the program hash table. Called via
319 * ctx->Driver.NewProgram. May be overridden (ie. replaced) by a
320 * device driver function to implement OO deriviation with additional
321 * types not understood by this function.
324 * \param id program id/number
325 * \param target program target/type
326 * \return pointer to new program object
329 _mesa_new_program(struct gl_context
*ctx
, GLenum target
, GLuint id
)
331 struct gl_program
*prog
;
333 case GL_VERTEX_PROGRAM_ARB
: /* == GL_VERTEX_PROGRAM_NV */
334 prog
= _mesa_init_vertex_program(ctx
, CALLOC_STRUCT(gl_vertex_program
),
337 case GL_FRAGMENT_PROGRAM_NV
:
338 case GL_FRAGMENT_PROGRAM_ARB
:
339 prog
=_mesa_init_fragment_program(ctx
,
340 CALLOC_STRUCT(gl_fragment_program
),
343 case MESA_GEOMETRY_PROGRAM
:
344 prog
= _mesa_init_geometry_program(ctx
,
345 CALLOC_STRUCT(gl_geometry_program
),
348 case GL_COMPUTE_PROGRAM_NV
:
349 prog
= _mesa_init_compute_program(ctx
,
350 CALLOC_STRUCT(gl_compute_program
),
354 _mesa_problem(ctx
, "bad target in _mesa_new_program");
362 * Delete a program and remove it from the hash table, ignoring the
364 * Called via ctx->Driver.DeleteProgram. May be wrapped (OO deriviation)
365 * by a device driver function.
368 _mesa_delete_program(struct gl_context
*ctx
, struct gl_program
*prog
)
372 assert(prog
->RefCount
==0);
374 if (prog
== &_mesa_DummyProgram
)
378 free(prog
->LocalParams
);
380 if (prog
->Instructions
) {
381 _mesa_free_instructions(prog
->Instructions
, prog
->NumInstructions
);
383 if (prog
->Parameters
) {
384 _mesa_free_parameter_list(prog
->Parameters
);
388 ralloc_free(prog
->nir
);
396 * Return the gl_program object for a given ID.
397 * Basically just a wrapper for _mesa_HashLookup() to avoid a lot of
401 _mesa_lookup_program(struct gl_context
*ctx
, GLuint id
)
404 return (struct gl_program
*) _mesa_HashLookup(ctx
->Shared
->Programs
, id
);
411 * Reference counting for vertex/fragment programs
412 * This is normally only called from the _mesa_reference_program() macro
413 * when there's a real pointer change.
416 _mesa_reference_program_(struct gl_context
*ctx
,
417 struct gl_program
**ptr
,
418 struct gl_program
*prog
)
424 if ((*ptr
)->Target
== GL_VERTEX_PROGRAM_ARB
)
425 assert(prog
->Target
== GL_VERTEX_PROGRAM_ARB
);
426 else if ((*ptr
)->Target
== GL_FRAGMENT_PROGRAM_ARB
)
427 assert(prog
->Target
== GL_FRAGMENT_PROGRAM_ARB
||
428 prog
->Target
== GL_FRAGMENT_PROGRAM_NV
);
429 else if ((*ptr
)->Target
== MESA_GEOMETRY_PROGRAM
)
430 assert(prog
->Target
== MESA_GEOMETRY_PROGRAM
);
435 GLboolean deleteFlag
;
437 /*mtx_lock(&(*ptr)->Mutex);*/
439 printf("Program %p ID=%u Target=%s Refcount-- to %d\n",
441 ((*ptr
)->Target
== GL_VERTEX_PROGRAM_ARB
? "VP" :
442 ((*ptr
)->Target
== MESA_GEOMETRY_PROGRAM
? "GP" : "FP")),
443 (*ptr
)->RefCount
- 1);
445 assert((*ptr
)->RefCount
> 0);
448 deleteFlag
= ((*ptr
)->RefCount
== 0);
449 /*mtx_lock(&(*ptr)->Mutex);*/
453 ctx
->Driver
.DeleteProgram(ctx
, *ptr
);
461 /*mtx_lock(&prog->Mutex);*/
464 printf("Program %p ID=%u Target=%s Refcount++ to %d\n",
466 (prog
->Target
== GL_VERTEX_PROGRAM_ARB
? "VP" :
467 (prog
->Target
== MESA_GEOMETRY_PROGRAM
? "GP" : "FP")),
470 /*mtx_unlock(&prog->Mutex);*/
478 * Return a copy of a program.
479 * XXX Problem here if the program object is actually OO-derivation
480 * made by a device driver.
483 _mesa_clone_program(struct gl_context
*ctx
, const struct gl_program
*prog
)
485 struct gl_program
*clone
;
487 clone
= ctx
->Driver
.NewProgram(ctx
, prog
->Target
, prog
->Id
);
491 assert(clone
->Target
== prog
->Target
);
492 assert(clone
->RefCount
== 1);
494 clone
->String
= (GLubyte
*) strdup((char *) prog
->String
);
495 clone
->Format
= prog
->Format
;
496 clone
->Instructions
= _mesa_alloc_instructions(prog
->NumInstructions
);
497 if (!clone
->Instructions
) {
498 _mesa_reference_program(ctx
, &clone
, NULL
);
501 _mesa_copy_instructions(clone
->Instructions
, prog
->Instructions
,
502 prog
->NumInstructions
);
503 clone
->InputsRead
= prog
->InputsRead
;
504 clone
->OutputsWritten
= prog
->OutputsWritten
;
505 clone
->SamplersUsed
= prog
->SamplersUsed
;
506 clone
->ShadowSamplers
= prog
->ShadowSamplers
;
507 memcpy(clone
->TexturesUsed
, prog
->TexturesUsed
, sizeof(prog
->TexturesUsed
));
509 if (prog
->Parameters
)
510 clone
->Parameters
= _mesa_clone_parameter_list(prog
->Parameters
);
511 if (prog
->LocalParams
) {
512 clone
->LocalParams
= malloc(MAX_PROGRAM_LOCAL_PARAMS
*
514 if (!clone
->LocalParams
) {
515 _mesa_reference_program(ctx
, &clone
, NULL
);
518 memcpy(clone
->LocalParams
, prog
->LocalParams
,
519 MAX_PROGRAM_LOCAL_PARAMS
* sizeof(float[4]));
521 clone
->IndirectRegisterFiles
= prog
->IndirectRegisterFiles
;
522 clone
->NumInstructions
= prog
->NumInstructions
;
523 clone
->NumTemporaries
= prog
->NumTemporaries
;
524 clone
->NumParameters
= prog
->NumParameters
;
525 clone
->NumAttributes
= prog
->NumAttributes
;
526 clone
->NumAddressRegs
= prog
->NumAddressRegs
;
527 clone
->NumNativeInstructions
= prog
->NumNativeInstructions
;
528 clone
->NumNativeTemporaries
= prog
->NumNativeTemporaries
;
529 clone
->NumNativeParameters
= prog
->NumNativeParameters
;
530 clone
->NumNativeAttributes
= prog
->NumNativeAttributes
;
531 clone
->NumNativeAddressRegs
= prog
->NumNativeAddressRegs
;
532 clone
->NumAluInstructions
= prog
->NumAluInstructions
;
533 clone
->NumTexInstructions
= prog
->NumTexInstructions
;
534 clone
->NumTexIndirections
= prog
->NumTexIndirections
;
535 clone
->NumNativeAluInstructions
= prog
->NumNativeAluInstructions
;
536 clone
->NumNativeTexInstructions
= prog
->NumNativeTexInstructions
;
537 clone
->NumNativeTexIndirections
= prog
->NumNativeTexIndirections
;
539 switch (prog
->Target
) {
540 case GL_VERTEX_PROGRAM_ARB
:
542 const struct gl_vertex_program
*vp
= gl_vertex_program_const(prog
);
543 struct gl_vertex_program
*vpc
= gl_vertex_program(clone
);
544 vpc
->IsPositionInvariant
= vp
->IsPositionInvariant
;
547 case GL_FRAGMENT_PROGRAM_ARB
:
549 const struct gl_fragment_program
*fp
= gl_fragment_program_const(prog
);
550 struct gl_fragment_program
*fpc
= gl_fragment_program(clone
);
551 fpc
->UsesKill
= fp
->UsesKill
;
552 fpc
->UsesDFdy
= fp
->UsesDFdy
;
553 fpc
->OriginUpperLeft
= fp
->OriginUpperLeft
;
554 fpc
->PixelCenterInteger
= fp
->PixelCenterInteger
;
557 case MESA_GEOMETRY_PROGRAM
:
559 const struct gl_geometry_program
*gp
= gl_geometry_program_const(prog
);
560 struct gl_geometry_program
*gpc
= gl_geometry_program(clone
);
561 gpc
->VerticesOut
= gp
->VerticesOut
;
562 gpc
->InputType
= gp
->InputType
;
563 gpc
->Invocations
= gp
->Invocations
;
564 gpc
->OutputType
= gp
->OutputType
;
565 gpc
->UsesEndPrimitive
= gp
->UsesEndPrimitive
;
566 gpc
->UsesStreams
= gp
->UsesStreams
;
570 _mesa_problem(NULL
, "Unexpected target in _mesa_clone_program");
578 * Insert 'count' NOP instructions at 'start' in the given program.
579 * Adjust branch targets accordingly.
582 _mesa_insert_instructions(struct gl_program
*prog
, GLuint start
, GLuint count
)
584 const GLuint origLen
= prog
->NumInstructions
;
585 const GLuint newLen
= origLen
+ count
;
586 struct prog_instruction
*newInst
;
589 /* adjust branches */
590 for (i
= 0; i
< prog
->NumInstructions
; i
++) {
591 struct prog_instruction
*inst
= prog
->Instructions
+ i
;
592 if (inst
->BranchTarget
> 0) {
593 if ((GLuint
)inst
->BranchTarget
>= start
) {
594 inst
->BranchTarget
+= count
;
599 /* Alloc storage for new instructions */
600 newInst
= _mesa_alloc_instructions(newLen
);
605 /* Copy 'start' instructions into new instruction buffer */
606 _mesa_copy_instructions(newInst
, prog
->Instructions
, start
);
608 /* init the new instructions */
609 _mesa_init_instructions(newInst
+ start
, count
);
611 /* Copy the remaining/tail instructions to new inst buffer */
612 _mesa_copy_instructions(newInst
+ start
+ count
,
613 prog
->Instructions
+ start
,
616 /* free old instructions */
617 _mesa_free_instructions(prog
->Instructions
, origLen
);
619 /* install new instructions */
620 prog
->Instructions
= newInst
;
621 prog
->NumInstructions
= newLen
;
627 * Delete 'count' instructions at 'start' in the given program.
628 * Adjust branch targets accordingly.
631 _mesa_delete_instructions(struct gl_program
*prog
, GLuint start
, GLuint count
)
633 const GLuint origLen
= prog
->NumInstructions
;
634 const GLuint newLen
= origLen
- count
;
635 struct prog_instruction
*newInst
;
638 /* adjust branches */
639 for (i
= 0; i
< prog
->NumInstructions
; i
++) {
640 struct prog_instruction
*inst
= prog
->Instructions
+ i
;
641 if (inst
->BranchTarget
> 0) {
642 if (inst
->BranchTarget
> (GLint
) start
) {
643 inst
->BranchTarget
-= count
;
648 /* Alloc storage for new instructions */
649 newInst
= _mesa_alloc_instructions(newLen
);
654 /* Copy 'start' instructions into new instruction buffer */
655 _mesa_copy_instructions(newInst
, prog
->Instructions
, start
);
657 /* Copy the remaining/tail instructions to new inst buffer */
658 _mesa_copy_instructions(newInst
+ start
,
659 prog
->Instructions
+ start
+ count
,
662 /* free old instructions */
663 _mesa_free_instructions(prog
->Instructions
, origLen
);
665 /* install new instructions */
666 prog
->Instructions
= newInst
;
667 prog
->NumInstructions
= newLen
;
674 * Search instructions for registers that match (oldFile, oldIndex),
675 * replacing them with (newFile, newIndex).
678 replace_registers(struct prog_instruction
*inst
, GLuint numInst
,
679 GLuint oldFile
, GLuint oldIndex
,
680 GLuint newFile
, GLuint newIndex
)
683 for (i
= 0; i
< numInst
; i
++) {
685 for (j
= 0; j
< _mesa_num_inst_src_regs(inst
[i
].Opcode
); j
++) {
686 if (inst
[i
].SrcReg
[j
].File
== oldFile
&&
687 inst
[i
].SrcReg
[j
].Index
== oldIndex
) {
688 inst
[i
].SrcReg
[j
].File
= newFile
;
689 inst
[i
].SrcReg
[j
].Index
= newIndex
;
693 if (inst
[i
].DstReg
.File
== oldFile
&& inst
[i
].DstReg
.Index
== oldIndex
) {
694 inst
[i
].DstReg
.File
= newFile
;
695 inst
[i
].DstReg
.Index
= newIndex
;
702 * Search instructions for references to program parameters. When found,
703 * increment the parameter index by 'offset'.
704 * Used when combining programs.
707 adjust_param_indexes(struct prog_instruction
*inst
, GLuint numInst
,
711 for (i
= 0; i
< numInst
; i
++) {
712 for (j
= 0; j
< _mesa_num_inst_src_regs(inst
[i
].Opcode
); j
++) {
713 GLuint f
= inst
[i
].SrcReg
[j
].File
;
714 if (f
== PROGRAM_CONSTANT
||
715 f
== PROGRAM_UNIFORM
||
716 f
== PROGRAM_STATE_VAR
) {
717 inst
[i
].SrcReg
[j
].Index
+= offset
;
725 * Combine two programs into one. Fix instructions so the outputs of
726 * the first program go to the inputs of the second program.
729 _mesa_combine_programs(struct gl_context
*ctx
,
730 const struct gl_program
*progA
,
731 const struct gl_program
*progB
)
733 struct prog_instruction
*newInst
;
734 struct gl_program
*newProg
;
735 const GLuint lenA
= progA
->NumInstructions
- 1; /* omit END instr */
736 const GLuint lenB
= progB
->NumInstructions
;
737 const GLuint numParamsA
= _mesa_num_parameters(progA
->Parameters
);
738 const GLuint newLength
= lenA
+ lenB
;
739 GLboolean usedTemps
[MAX_PROGRAM_TEMPS
];
740 GLuint firstTemp
= 0;
741 GLbitfield64 inputsB
;
744 assert(progA
->Target
== progB
->Target
);
746 newInst
= _mesa_alloc_instructions(newLength
);
750 _mesa_copy_instructions(newInst
, progA
->Instructions
, lenA
);
751 _mesa_copy_instructions(newInst
+ lenA
, progB
->Instructions
, lenB
);
753 /* adjust branch / instruction addresses for B's instructions */
754 for (i
= 0; i
< lenB
; i
++) {
755 newInst
[lenA
+ i
].BranchTarget
+= lenA
;
758 newProg
= ctx
->Driver
.NewProgram(ctx
, progA
->Target
, 0);
759 newProg
->Instructions
= newInst
;
760 newProg
->NumInstructions
= newLength
;
762 /* find used temp regs (we may need new temps below) */
763 _mesa_find_used_registers(newProg
, PROGRAM_TEMPORARY
,
764 usedTemps
, MAX_PROGRAM_TEMPS
);
766 if (newProg
->Target
== GL_FRAGMENT_PROGRAM_ARB
) {
767 const struct gl_fragment_program
*fprogA
, *fprogB
;
768 struct gl_fragment_program
*newFprog
;
769 GLbitfield64 progB_inputsRead
= progB
->InputsRead
;
770 GLint progB_colorFile
, progB_colorIndex
;
772 fprogA
= gl_fragment_program_const(progA
);
773 fprogB
= gl_fragment_program_const(progB
);
774 newFprog
= gl_fragment_program(newProg
);
776 newFprog
->UsesKill
= fprogA
->UsesKill
|| fprogB
->UsesKill
;
777 newFprog
->UsesDFdy
= fprogA
->UsesDFdy
|| fprogB
->UsesDFdy
;
779 /* We'll do a search and replace for instances
780 * of progB_colorFile/progB_colorIndex below...
782 progB_colorFile
= PROGRAM_INPUT
;
783 progB_colorIndex
= VARYING_SLOT_COL0
;
786 * The fragment program may get color from a state var rather than
787 * a fragment input (vertex output) if it's constant.
788 * See the texenvprogram.c code.
789 * So, search the program's parameter list now to see if the program
790 * gets color from a state var instead of a conventional fragment
793 for (i
= 0; i
< progB
->Parameters
->NumParameters
; i
++) {
794 struct gl_program_parameter
*p
= &progB
->Parameters
->Parameters
[i
];
795 if (p
->Type
== PROGRAM_STATE_VAR
&&
796 p
->StateIndexes
[0] == STATE_INTERNAL
&&
797 p
->StateIndexes
[1] == STATE_CURRENT_ATTRIB
&&
798 (int) p
->StateIndexes
[2] == (int) VERT_ATTRIB_COLOR0
) {
799 progB_inputsRead
|= VARYING_BIT_COL0
;
800 progB_colorFile
= PROGRAM_STATE_VAR
;
801 progB_colorIndex
= i
;
806 /* Connect color outputs of fprogA to color inputs of fprogB, via a
807 * new temporary register.
809 if ((progA
->OutputsWritten
& BITFIELD64_BIT(FRAG_RESULT_COLOR
)) &&
810 (progB_inputsRead
& VARYING_BIT_COL0
)) {
811 GLint tempReg
= _mesa_find_free_register(usedTemps
, MAX_PROGRAM_TEMPS
,
814 _mesa_problem(ctx
, "No free temp regs found in "
815 "_mesa_combine_programs(), using 31");
818 firstTemp
= tempReg
+ 1;
820 /* replace writes to result.color[0] with tempReg */
821 replace_registers(newInst
, lenA
,
822 PROGRAM_OUTPUT
, FRAG_RESULT_COLOR
,
823 PROGRAM_TEMPORARY
, tempReg
);
824 /* replace reads from the input color with tempReg */
825 replace_registers(newInst
+ lenA
, lenB
,
826 progB_colorFile
, progB_colorIndex
, /* search for */
827 PROGRAM_TEMPORARY
, tempReg
/* replace with */ );
830 /* compute combined program's InputsRead */
831 inputsB
= progB_inputsRead
;
832 if (progA
->OutputsWritten
& BITFIELD64_BIT(FRAG_RESULT_COLOR
)) {
833 inputsB
&= ~(1 << VARYING_SLOT_COL0
);
835 newProg
->InputsRead
= progA
->InputsRead
| inputsB
;
836 newProg
->OutputsWritten
= progB
->OutputsWritten
;
837 newProg
->SamplersUsed
= progA
->SamplersUsed
| progB
->SamplersUsed
;
841 assert(0); /* XXX todo */
845 * Merge parameters (uniforms, constants, etc)
847 newProg
->Parameters
= _mesa_combine_parameter_lists(progA
->Parameters
,
850 adjust_param_indexes(newInst
+ lenA
, lenB
, numParamsA
);
858 * Populate the 'used' array with flags indicating which registers (TEMPs,
859 * INPUTs, OUTPUTs, etc, are used by the given program.
860 * \param file type of register to scan for
861 * \param used returns true/false flags for in use / free
862 * \param usedSize size of the 'used' array
865 _mesa_find_used_registers(const struct gl_program
*prog
,
866 gl_register_file file
,
867 GLboolean used
[], GLuint usedSize
)
871 memset(used
, 0, usedSize
);
873 for (i
= 0; i
< prog
->NumInstructions
; i
++) {
874 const struct prog_instruction
*inst
= prog
->Instructions
+ i
;
875 const GLuint n
= _mesa_num_inst_src_regs(inst
->Opcode
);
877 if (inst
->DstReg
.File
== file
) {
878 assert(inst
->DstReg
.Index
< usedSize
);
879 if(inst
->DstReg
.Index
< usedSize
)
880 used
[inst
->DstReg
.Index
] = GL_TRUE
;
883 for (j
= 0; j
< n
; j
++) {
884 if (inst
->SrcReg
[j
].File
== file
) {
885 assert(inst
->SrcReg
[j
].Index
< (GLint
) usedSize
);
886 if (inst
->SrcReg
[j
].Index
< (GLint
) usedSize
)
887 used
[inst
->SrcReg
[j
].Index
] = GL_TRUE
;
895 * Scan the given 'used' register flag array for the first entry
896 * that's >= firstReg.
897 * \param used vector of flags indicating registers in use (as returned
898 * by _mesa_find_used_registers())
899 * \param usedSize size of the 'used' array
900 * \param firstReg first register to start searching at
901 * \return index of unused register, or -1 if none.
904 _mesa_find_free_register(const GLboolean used
[],
905 GLuint usedSize
, GLuint firstReg
)
909 assert(firstReg
< usedSize
);
911 for (i
= firstReg
; i
< usedSize
; i
++)
921 * Check if the given register index is valid (doesn't exceed implementation-
923 * \return GL_TRUE if OK, GL_FALSE if bad index
926 _mesa_valid_register_index(const struct gl_context
*ctx
,
927 gl_shader_stage shaderType
,
928 gl_register_file file
, GLint index
)
930 const struct gl_program_constants
*c
;
932 assert(0 <= shaderType
&& shaderType
< MESA_SHADER_STAGES
);
933 c
= &ctx
->Const
.Program
[shaderType
];
936 case PROGRAM_UNDEFINED
:
937 return GL_TRUE
; /* XXX or maybe false? */
939 case PROGRAM_TEMPORARY
:
940 return index
>= 0 && index
< (GLint
) c
->MaxTemps
;
942 case PROGRAM_UNIFORM
:
943 case PROGRAM_STATE_VAR
:
944 /* aka constant buffer */
945 return index
>= 0 && index
< (GLint
) c
->MaxUniformComponents
/ 4;
947 case PROGRAM_CONSTANT
:
948 /* constant buffer w/ possible relative negative addressing */
949 return (index
> (int) c
->MaxUniformComponents
/ -4 &&
950 index
< (int) c
->MaxUniformComponents
/ 4);
956 switch (shaderType
) {
957 case MESA_SHADER_VERTEX
:
958 return index
< VERT_ATTRIB_GENERIC0
+ (GLint
) c
->MaxAttribs
;
959 case MESA_SHADER_FRAGMENT
:
960 return index
< VARYING_SLOT_VAR0
+ (GLint
) ctx
->Const
.MaxVarying
;
961 case MESA_SHADER_GEOMETRY
:
962 return index
< VARYING_SLOT_VAR0
+ (GLint
) ctx
->Const
.MaxVarying
;
971 switch (shaderType
) {
972 case MESA_SHADER_VERTEX
:
973 return index
< VARYING_SLOT_VAR0
+ (GLint
) ctx
->Const
.MaxVarying
;
974 case MESA_SHADER_FRAGMENT
:
975 return index
< FRAG_RESULT_DATA0
+ (GLint
) ctx
->Const
.MaxDrawBuffers
;
976 case MESA_SHADER_GEOMETRY
:
977 return index
< VARYING_SLOT_VAR0
+ (GLint
) ctx
->Const
.MaxVarying
;
982 case PROGRAM_ADDRESS
:
983 return index
>= 0 && index
< (GLint
) c
->MaxAddressRegs
;
987 "unexpected register file in _mesa_valid_register_index()");
995 * "Post-process" a GPU program. This is intended to be used for debugging.
996 * Example actions include no-op'ing instructions or changing instruction
1000 _mesa_postprocess_program(struct gl_context
*ctx
, struct gl_program
*prog
)
1002 static const GLfloat white
[4] = { 0.5, 0.5, 0.5, 0.5 };
1004 GLuint whiteSwizzle
;
1005 GLint whiteIndex
= _mesa_add_unnamed_constant(prog
->Parameters
,
1006 (gl_constant_value
*) white
,
1011 for (i
= 0; i
< prog
->NumInstructions
; i
++) {
1012 struct prog_instruction
*inst
= prog
->Instructions
+ i
;
1013 const GLuint n
= _mesa_num_inst_src_regs(inst
->Opcode
);
1017 if (_mesa_is_tex_instruction(inst
->Opcode
)) {
1019 /* replace TEX/TXP/TXB with MOV */
1020 inst
->Opcode
= OPCODE_MOV
;
1021 inst
->DstReg
.WriteMask
= WRITEMASK_XYZW
;
1022 inst
->SrcReg
[0].Swizzle
= SWIZZLE_XYZW
;
1023 inst
->SrcReg
[0].Negate
= NEGATE_NONE
;
1027 /* disable shadow texture mode */
1028 inst
->TexShadow
= 0;
1032 if (inst
->Opcode
== OPCODE_TXP
) {
1034 inst
->Opcode
= OPCODE_MOV
;
1035 inst
->DstReg
.WriteMask
= WRITEMASK_XYZW
;
1036 inst
->SrcReg
[0].File
= PROGRAM_CONSTANT
;
1037 inst
->SrcReg
[0].Index
= whiteIndex
;
1038 inst
->SrcReg
[0].Swizzle
= SWIZZLE_XYZW
;
1039 inst
->SrcReg
[0].Negate
= NEGATE_NONE
;
1042 inst
->TexShadow
= 0;
1045 inst
->Opcode
= OPCODE_TEX
;
1046 inst
->TexShadow
= 0;
1053 /* Gets the minimum number of shader invocations per fragment.
1054 * This function is useful to determine if we need to do per
1055 * sample shading or per fragment shading.
1058 _mesa_get_min_invocations_per_fragment(struct gl_context
*ctx
,
1059 const struct gl_fragment_program
*prog
,
1060 bool ignore_sample_qualifier
)
1062 /* From ARB_sample_shading specification:
1063 * "Using gl_SampleID in a fragment shader causes the entire shader
1064 * to be evaluated per-sample."
1066 * "Using gl_SamplePosition in a fragment shader causes the entire
1067 * shader to be evaluated per-sample."
1069 * "If MULTISAMPLE or SAMPLE_SHADING_ARB is disabled, sample shading
1072 if (ctx
->Multisample
.Enabled
) {
1073 /* The ARB_gpu_shader5 specification says:
1075 * "Use of the "sample" qualifier on a fragment shader input
1076 * forces per-sample shading"
1078 if (prog
->IsSample
&& !ignore_sample_qualifier
)
1079 return MAX2(ctx
->DrawBuffer
->Visual
.samples
, 1);
1081 if (prog
->Base
.SystemValuesRead
& (SYSTEM_BIT_SAMPLE_ID
|
1082 SYSTEM_BIT_SAMPLE_POS
))
1083 return MAX2(ctx
->DrawBuffer
->Visual
.samples
, 1);
1084 else if (ctx
->Multisample
.SampleShading
)
1085 return MAX2(ceil(ctx
->Multisample
.MinSampleShadingValue
*
1086 ctx
->DrawBuffer
->Visual
.samples
), 1);