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 /* XXX probably move this stuff */
101 ctx
->ATIFragmentShader
.Enabled
= GL_FALSE
;
102 ctx
->ATIFragmentShader
.Current
= ctx
->Shared
->DefaultFragmentShader
;
103 assert(ctx
->ATIFragmentShader
.Current
);
104 ctx
->ATIFragmentShader
.Current
->RefCount
++;
109 * Free a context's vertex/fragment program state
112 _mesa_free_program_data(struct gl_context
*ctx
)
114 _mesa_reference_vertprog(ctx
, &ctx
->VertexProgram
.Current
, NULL
);
115 _mesa_delete_program_cache(ctx
, ctx
->VertexProgram
.Cache
);
116 _mesa_reference_fragprog(ctx
, &ctx
->FragmentProgram
.Current
, NULL
);
117 _mesa_delete_shader_cache(ctx
, ctx
->FragmentProgram
.Cache
);
119 /* XXX probably move this stuff */
120 if (ctx
->ATIFragmentShader
.Current
) {
121 ctx
->ATIFragmentShader
.Current
->RefCount
--;
122 if (ctx
->ATIFragmentShader
.Current
->RefCount
<= 0) {
123 free(ctx
->ATIFragmentShader
.Current
);
127 free((void *) ctx
->Program
.ErrorString
);
132 * Update the default program objects in the given context to reference those
133 * specified in the shared state and release those referencing the old
137 _mesa_update_default_objects_program(struct gl_context
*ctx
)
139 _mesa_reference_vertprog(ctx
, &ctx
->VertexProgram
.Current
,
140 ctx
->Shared
->DefaultVertexProgram
);
141 assert(ctx
->VertexProgram
.Current
);
143 _mesa_reference_fragprog(ctx
, &ctx
->FragmentProgram
.Current
,
144 ctx
->Shared
->DefaultFragmentProgram
);
145 assert(ctx
->FragmentProgram
.Current
);
147 /* XXX probably move this stuff */
148 if (ctx
->ATIFragmentShader
.Current
) {
149 ctx
->ATIFragmentShader
.Current
->RefCount
--;
150 if (ctx
->ATIFragmentShader
.Current
->RefCount
<= 0) {
151 free(ctx
->ATIFragmentShader
.Current
);
154 ctx
->ATIFragmentShader
.Current
= (struct ati_fragment_shader
*) ctx
->Shared
->DefaultFragmentShader
;
155 assert(ctx
->ATIFragmentShader
.Current
);
156 ctx
->ATIFragmentShader
.Current
->RefCount
++;
161 * Set the vertex/fragment program error state (position and error string).
162 * This is generally called from within the parsers.
165 _mesa_set_program_error(struct gl_context
*ctx
, GLint pos
, const char *string
)
167 ctx
->Program
.ErrorPos
= pos
;
168 free((void *) ctx
->Program
.ErrorString
);
171 ctx
->Program
.ErrorString
= strdup(string
);
176 * Find the line number and column for 'pos' within 'string'.
177 * Return a copy of the line which contains 'pos'. Free the line with
179 * \param string the program string
180 * \param pos the position within the string
181 * \param line returns the line number corresponding to 'pos'.
182 * \param col returns the column number corresponding to 'pos'.
183 * \return copy of the line containing 'pos'.
186 _mesa_find_line_column(const GLubyte
*string
, const GLubyte
*pos
,
187 GLint
*line
, GLint
*col
)
189 const GLubyte
*lineStart
= string
;
190 const GLubyte
*p
= string
;
197 if (*p
== (GLubyte
) '\n') {
204 *col
= (pos
- lineStart
) + 1;
206 /* return copy of this line */
207 while (*p
!= 0 && *p
!= '\n')
211 memcpy(s
, lineStart
, len
);
219 * Initialize a new gl_program object.
222 init_program_struct(struct gl_program
*prog
, GLenum target
, GLuint id
)
228 memset(prog
, 0, sizeof(*prog
));
230 prog
->Target
= target
;
232 prog
->Format
= GL_PROGRAM_FORMAT_ASCII_ARB
;
234 /* default mapping from samplers to texture units */
235 for (i
= 0; i
< MAX_SAMPLERS
; i
++)
236 prog
->SamplerUnits
[i
] = i
;
241 * Initialize a new fragment program object.
244 _mesa_init_fragment_program(struct gl_context
*ctx
,
245 struct gl_fragment_program
*prog
,
246 GLenum target
, GLuint id
)
249 init_program_struct(&prog
->Base
, target
, id
);
257 * Initialize a new vertex program object.
260 _mesa_init_vertex_program(struct gl_context
*ctx
,
261 struct gl_vertex_program
*prog
,
262 GLenum target
, GLuint id
)
265 init_program_struct(&prog
->Base
, target
, id
);
273 * Initialize a new compute program object.
276 _mesa_init_compute_program(struct gl_context
*ctx
,
277 struct gl_compute_program
*prog
,
278 GLenum target
, GLuint id
)
281 init_program_struct(&prog
->Base
, target
, id
);
289 * Initialize a new geometry program object.
292 _mesa_init_geometry_program(struct gl_context
*ctx
,
293 struct gl_geometry_program
*prog
,
294 GLenum target
, GLuint id
)
297 init_program_struct(&prog
->Base
, target
, id
);
305 * Allocate and initialize a new fragment/vertex program object but
306 * don't put it into the program hash table. Called via
307 * ctx->Driver.NewProgram. May be overridden (ie. replaced) by a
308 * device driver function to implement OO deriviation with additional
309 * types not understood by this function.
312 * \param id program id/number
313 * \param target program target/type
314 * \return pointer to new program object
317 _mesa_new_program(struct gl_context
*ctx
, GLenum target
, GLuint id
)
319 struct gl_program
*prog
;
321 case GL_VERTEX_PROGRAM_ARB
: /* == GL_VERTEX_PROGRAM_NV */
322 prog
= _mesa_init_vertex_program(ctx
, CALLOC_STRUCT(gl_vertex_program
),
325 case GL_FRAGMENT_PROGRAM_NV
:
326 case GL_FRAGMENT_PROGRAM_ARB
:
327 prog
=_mesa_init_fragment_program(ctx
,
328 CALLOC_STRUCT(gl_fragment_program
),
331 case GL_GEOMETRY_PROGRAM_NV
:
332 prog
= _mesa_init_geometry_program(ctx
,
333 CALLOC_STRUCT(gl_geometry_program
),
336 case GL_COMPUTE_PROGRAM_NV
:
337 prog
= _mesa_init_compute_program(ctx
,
338 CALLOC_STRUCT(gl_compute_program
),
342 _mesa_problem(ctx
, "bad target in _mesa_new_program");
350 * Delete a program and remove it from the hash table, ignoring the
352 * Called via ctx->Driver.DeleteProgram. May be wrapped (OO deriviation)
353 * by a device driver function.
356 _mesa_delete_program(struct gl_context
*ctx
, struct gl_program
*prog
)
360 assert(prog
->RefCount
==0);
362 if (prog
== &_mesa_DummyProgram
)
366 free(prog
->LocalParams
);
368 if (prog
->Instructions
) {
369 _mesa_free_instructions(prog
->Instructions
, prog
->NumInstructions
);
371 if (prog
->Parameters
) {
372 _mesa_free_parameter_list(prog
->Parameters
);
376 ralloc_free(prog
->nir
);
384 * Return the gl_program object for a given ID.
385 * Basically just a wrapper for _mesa_HashLookup() to avoid a lot of
389 _mesa_lookup_program(struct gl_context
*ctx
, GLuint id
)
392 return (struct gl_program
*) _mesa_HashLookup(ctx
->Shared
->Programs
, id
);
399 * Reference counting for vertex/fragment programs
400 * This is normally only called from the _mesa_reference_program() macro
401 * when there's a real pointer change.
404 _mesa_reference_program_(struct gl_context
*ctx
,
405 struct gl_program
**ptr
,
406 struct gl_program
*prog
)
412 if ((*ptr
)->Target
== GL_VERTEX_PROGRAM_ARB
)
413 assert(prog
->Target
== GL_VERTEX_PROGRAM_ARB
);
414 else if ((*ptr
)->Target
== GL_FRAGMENT_PROGRAM_ARB
)
415 assert(prog
->Target
== GL_FRAGMENT_PROGRAM_ARB
||
416 prog
->Target
== GL_FRAGMENT_PROGRAM_NV
);
417 else if ((*ptr
)->Target
== GL_GEOMETRY_PROGRAM_NV
)
418 assert(prog
->Target
== GL_GEOMETRY_PROGRAM_NV
);
423 GLboolean deleteFlag
;
425 /*mtx_lock(&(*ptr)->Mutex);*/
427 printf("Program %p ID=%u Target=%s Refcount-- to %d\n",
429 ((*ptr
)->Target
== GL_VERTEX_PROGRAM_ARB
? "VP" :
430 ((*ptr
)->Target
== GL_GEOMETRY_PROGRAM_NV
? "GP" : "FP")),
431 (*ptr
)->RefCount
- 1);
433 assert((*ptr
)->RefCount
> 0);
436 deleteFlag
= ((*ptr
)->RefCount
== 0);
437 /*mtx_lock(&(*ptr)->Mutex);*/
441 ctx
->Driver
.DeleteProgram(ctx
, *ptr
);
449 /*mtx_lock(&prog->Mutex);*/
452 printf("Program %p ID=%u Target=%s Refcount++ to %d\n",
454 (prog
->Target
== GL_VERTEX_PROGRAM_ARB
? "VP" :
455 (prog
->Target
== GL_GEOMETRY_PROGRAM_NV
? "GP" : "FP")),
458 /*mtx_unlock(&prog->Mutex);*/
466 * Return a copy of a program.
467 * XXX Problem here if the program object is actually OO-derivation
468 * made by a device driver.
471 _mesa_clone_program(struct gl_context
*ctx
, const struct gl_program
*prog
)
473 struct gl_program
*clone
;
475 clone
= ctx
->Driver
.NewProgram(ctx
, prog
->Target
, prog
->Id
);
479 assert(clone
->Target
== prog
->Target
);
480 assert(clone
->RefCount
== 1);
482 clone
->String
= (GLubyte
*) strdup((char *) prog
->String
);
483 clone
->Format
= prog
->Format
;
484 clone
->Instructions
= _mesa_alloc_instructions(prog
->NumInstructions
);
485 if (!clone
->Instructions
) {
486 _mesa_reference_program(ctx
, &clone
, NULL
);
489 _mesa_copy_instructions(clone
->Instructions
, prog
->Instructions
,
490 prog
->NumInstructions
);
491 clone
->InputsRead
= prog
->InputsRead
;
492 clone
->OutputsWritten
= prog
->OutputsWritten
;
493 clone
->SamplersUsed
= prog
->SamplersUsed
;
494 clone
->ShadowSamplers
= prog
->ShadowSamplers
;
495 memcpy(clone
->TexturesUsed
, prog
->TexturesUsed
, sizeof(prog
->TexturesUsed
));
497 if (prog
->Parameters
)
498 clone
->Parameters
= _mesa_clone_parameter_list(prog
->Parameters
);
499 if (prog
->LocalParams
) {
500 clone
->LocalParams
= malloc(MAX_PROGRAM_LOCAL_PARAMS
*
502 if (!clone
->LocalParams
) {
503 _mesa_reference_program(ctx
, &clone
, NULL
);
506 memcpy(clone
->LocalParams
, prog
->LocalParams
,
507 MAX_PROGRAM_LOCAL_PARAMS
* sizeof(float[4]));
509 clone
->IndirectRegisterFiles
= prog
->IndirectRegisterFiles
;
510 clone
->NumInstructions
= prog
->NumInstructions
;
511 clone
->NumTemporaries
= prog
->NumTemporaries
;
512 clone
->NumParameters
= prog
->NumParameters
;
513 clone
->NumAttributes
= prog
->NumAttributes
;
514 clone
->NumAddressRegs
= prog
->NumAddressRegs
;
515 clone
->NumNativeInstructions
= prog
->NumNativeInstructions
;
516 clone
->NumNativeTemporaries
= prog
->NumNativeTemporaries
;
517 clone
->NumNativeParameters
= prog
->NumNativeParameters
;
518 clone
->NumNativeAttributes
= prog
->NumNativeAttributes
;
519 clone
->NumNativeAddressRegs
= prog
->NumNativeAddressRegs
;
520 clone
->NumAluInstructions
= prog
->NumAluInstructions
;
521 clone
->NumTexInstructions
= prog
->NumTexInstructions
;
522 clone
->NumTexIndirections
= prog
->NumTexIndirections
;
523 clone
->NumNativeAluInstructions
= prog
->NumNativeAluInstructions
;
524 clone
->NumNativeTexInstructions
= prog
->NumNativeTexInstructions
;
525 clone
->NumNativeTexIndirections
= prog
->NumNativeTexIndirections
;
527 switch (prog
->Target
) {
528 case GL_VERTEX_PROGRAM_ARB
:
530 const struct gl_vertex_program
*vp
= gl_vertex_program_const(prog
);
531 struct gl_vertex_program
*vpc
= gl_vertex_program(clone
);
532 vpc
->IsPositionInvariant
= vp
->IsPositionInvariant
;
535 case GL_FRAGMENT_PROGRAM_ARB
:
537 const struct gl_fragment_program
*fp
= gl_fragment_program_const(prog
);
538 struct gl_fragment_program
*fpc
= gl_fragment_program(clone
);
539 fpc
->UsesKill
= fp
->UsesKill
;
540 fpc
->UsesDFdy
= fp
->UsesDFdy
;
541 fpc
->OriginUpperLeft
= fp
->OriginUpperLeft
;
542 fpc
->PixelCenterInteger
= fp
->PixelCenterInteger
;
545 case GL_GEOMETRY_PROGRAM_NV
:
547 const struct gl_geometry_program
*gp
= gl_geometry_program_const(prog
);
548 struct gl_geometry_program
*gpc
= gl_geometry_program(clone
);
549 gpc
->VerticesOut
= gp
->VerticesOut
;
550 gpc
->InputType
= gp
->InputType
;
551 gpc
->Invocations
= gp
->Invocations
;
552 gpc
->OutputType
= gp
->OutputType
;
553 gpc
->UsesEndPrimitive
= gp
->UsesEndPrimitive
;
554 gpc
->UsesStreams
= gp
->UsesStreams
;
558 _mesa_problem(NULL
, "Unexpected target in _mesa_clone_program");
566 * Insert 'count' NOP instructions at 'start' in the given program.
567 * Adjust branch targets accordingly.
570 _mesa_insert_instructions(struct gl_program
*prog
, GLuint start
, GLuint count
)
572 const GLuint origLen
= prog
->NumInstructions
;
573 const GLuint newLen
= origLen
+ count
;
574 struct prog_instruction
*newInst
;
577 /* adjust branches */
578 for (i
= 0; i
< prog
->NumInstructions
; i
++) {
579 struct prog_instruction
*inst
= prog
->Instructions
+ i
;
580 if (inst
->BranchTarget
> 0) {
581 if ((GLuint
)inst
->BranchTarget
>= start
) {
582 inst
->BranchTarget
+= count
;
587 /* Alloc storage for new instructions */
588 newInst
= _mesa_alloc_instructions(newLen
);
593 /* Copy 'start' instructions into new instruction buffer */
594 _mesa_copy_instructions(newInst
, prog
->Instructions
, start
);
596 /* init the new instructions */
597 _mesa_init_instructions(newInst
+ start
, count
);
599 /* Copy the remaining/tail instructions to new inst buffer */
600 _mesa_copy_instructions(newInst
+ start
+ count
,
601 prog
->Instructions
+ start
,
604 /* free old instructions */
605 _mesa_free_instructions(prog
->Instructions
, origLen
);
607 /* install new instructions */
608 prog
->Instructions
= newInst
;
609 prog
->NumInstructions
= newLen
;
615 * Delete 'count' instructions at 'start' in the given program.
616 * Adjust branch targets accordingly.
619 _mesa_delete_instructions(struct gl_program
*prog
, GLuint start
, GLuint count
)
621 const GLuint origLen
= prog
->NumInstructions
;
622 const GLuint newLen
= origLen
- count
;
623 struct prog_instruction
*newInst
;
626 /* adjust branches */
627 for (i
= 0; i
< prog
->NumInstructions
; i
++) {
628 struct prog_instruction
*inst
= prog
->Instructions
+ i
;
629 if (inst
->BranchTarget
> 0) {
630 if (inst
->BranchTarget
> (GLint
) start
) {
631 inst
->BranchTarget
-= count
;
636 /* Alloc storage for new instructions */
637 newInst
= _mesa_alloc_instructions(newLen
);
642 /* Copy 'start' instructions into new instruction buffer */
643 _mesa_copy_instructions(newInst
, prog
->Instructions
, start
);
645 /* Copy the remaining/tail instructions to new inst buffer */
646 _mesa_copy_instructions(newInst
+ start
,
647 prog
->Instructions
+ start
+ count
,
650 /* free old instructions */
651 _mesa_free_instructions(prog
->Instructions
, origLen
);
653 /* install new instructions */
654 prog
->Instructions
= newInst
;
655 prog
->NumInstructions
= newLen
;
662 * Search instructions for registers that match (oldFile, oldIndex),
663 * replacing them with (newFile, newIndex).
666 replace_registers(struct prog_instruction
*inst
, GLuint numInst
,
667 GLuint oldFile
, GLuint oldIndex
,
668 GLuint newFile
, GLuint newIndex
)
671 for (i
= 0; i
< numInst
; i
++) {
673 for (j
= 0; j
< _mesa_num_inst_src_regs(inst
[i
].Opcode
); j
++) {
674 if (inst
[i
].SrcReg
[j
].File
== oldFile
&&
675 inst
[i
].SrcReg
[j
].Index
== oldIndex
) {
676 inst
[i
].SrcReg
[j
].File
= newFile
;
677 inst
[i
].SrcReg
[j
].Index
= newIndex
;
681 if (inst
[i
].DstReg
.File
== oldFile
&& inst
[i
].DstReg
.Index
== oldIndex
) {
682 inst
[i
].DstReg
.File
= newFile
;
683 inst
[i
].DstReg
.Index
= newIndex
;
690 * Search instructions for references to program parameters. When found,
691 * increment the parameter index by 'offset'.
692 * Used when combining programs.
695 adjust_param_indexes(struct prog_instruction
*inst
, GLuint numInst
,
699 for (i
= 0; i
< numInst
; i
++) {
700 for (j
= 0; j
< _mesa_num_inst_src_regs(inst
[i
].Opcode
); j
++) {
701 GLuint f
= inst
[i
].SrcReg
[j
].File
;
702 if (f
== PROGRAM_CONSTANT
||
703 f
== PROGRAM_UNIFORM
||
704 f
== PROGRAM_STATE_VAR
) {
705 inst
[i
].SrcReg
[j
].Index
+= offset
;
713 * Combine two programs into one. Fix instructions so the outputs of
714 * the first program go to the inputs of the second program.
717 _mesa_combine_programs(struct gl_context
*ctx
,
718 const struct gl_program
*progA
,
719 const struct gl_program
*progB
)
721 struct prog_instruction
*newInst
;
722 struct gl_program
*newProg
;
723 const GLuint lenA
= progA
->NumInstructions
- 1; /* omit END instr */
724 const GLuint lenB
= progB
->NumInstructions
;
725 const GLuint numParamsA
= _mesa_num_parameters(progA
->Parameters
);
726 const GLuint newLength
= lenA
+ lenB
;
727 GLboolean usedTemps
[MAX_PROGRAM_TEMPS
];
728 GLuint firstTemp
= 0;
729 GLbitfield64 inputsB
;
732 assert(progA
->Target
== progB
->Target
);
734 newInst
= _mesa_alloc_instructions(newLength
);
738 _mesa_copy_instructions(newInst
, progA
->Instructions
, lenA
);
739 _mesa_copy_instructions(newInst
+ lenA
, progB
->Instructions
, lenB
);
741 /* adjust branch / instruction addresses for B's instructions */
742 for (i
= 0; i
< lenB
; i
++) {
743 newInst
[lenA
+ i
].BranchTarget
+= lenA
;
746 newProg
= ctx
->Driver
.NewProgram(ctx
, progA
->Target
, 0);
747 newProg
->Instructions
= newInst
;
748 newProg
->NumInstructions
= newLength
;
750 /* find used temp regs (we may need new temps below) */
751 _mesa_find_used_registers(newProg
, PROGRAM_TEMPORARY
,
752 usedTemps
, MAX_PROGRAM_TEMPS
);
754 if (newProg
->Target
== GL_FRAGMENT_PROGRAM_ARB
) {
755 const struct gl_fragment_program
*fprogA
, *fprogB
;
756 struct gl_fragment_program
*newFprog
;
757 GLbitfield64 progB_inputsRead
= progB
->InputsRead
;
758 GLint progB_colorFile
, progB_colorIndex
;
760 fprogA
= gl_fragment_program_const(progA
);
761 fprogB
= gl_fragment_program_const(progB
);
762 newFprog
= gl_fragment_program(newProg
);
764 newFprog
->UsesKill
= fprogA
->UsesKill
|| fprogB
->UsesKill
;
765 newFprog
->UsesDFdy
= fprogA
->UsesDFdy
|| fprogB
->UsesDFdy
;
767 /* We'll do a search and replace for instances
768 * of progB_colorFile/progB_colorIndex below...
770 progB_colorFile
= PROGRAM_INPUT
;
771 progB_colorIndex
= VARYING_SLOT_COL0
;
774 * The fragment program may get color from a state var rather than
775 * a fragment input (vertex output) if it's constant.
776 * See the texenvprogram.c code.
777 * So, search the program's parameter list now to see if the program
778 * gets color from a state var instead of a conventional fragment
781 for (i
= 0; i
< progB
->Parameters
->NumParameters
; i
++) {
782 struct gl_program_parameter
*p
= &progB
->Parameters
->Parameters
[i
];
783 if (p
->Type
== PROGRAM_STATE_VAR
&&
784 p
->StateIndexes
[0] == STATE_INTERNAL
&&
785 p
->StateIndexes
[1] == STATE_CURRENT_ATTRIB
&&
786 (int) p
->StateIndexes
[2] == (int) VERT_ATTRIB_COLOR0
) {
787 progB_inputsRead
|= VARYING_BIT_COL0
;
788 progB_colorFile
= PROGRAM_STATE_VAR
;
789 progB_colorIndex
= i
;
794 /* Connect color outputs of fprogA to color inputs of fprogB, via a
795 * new temporary register.
797 if ((progA
->OutputsWritten
& BITFIELD64_BIT(FRAG_RESULT_COLOR
)) &&
798 (progB_inputsRead
& VARYING_BIT_COL0
)) {
799 GLint tempReg
= _mesa_find_free_register(usedTemps
, MAX_PROGRAM_TEMPS
,
802 _mesa_problem(ctx
, "No free temp regs found in "
803 "_mesa_combine_programs(), using 31");
806 firstTemp
= tempReg
+ 1;
808 /* replace writes to result.color[0] with tempReg */
809 replace_registers(newInst
, lenA
,
810 PROGRAM_OUTPUT
, FRAG_RESULT_COLOR
,
811 PROGRAM_TEMPORARY
, tempReg
);
812 /* replace reads from the input color with tempReg */
813 replace_registers(newInst
+ lenA
, lenB
,
814 progB_colorFile
, progB_colorIndex
, /* search for */
815 PROGRAM_TEMPORARY
, tempReg
/* replace with */ );
818 /* compute combined program's InputsRead */
819 inputsB
= progB_inputsRead
;
820 if (progA
->OutputsWritten
& BITFIELD64_BIT(FRAG_RESULT_COLOR
)) {
821 inputsB
&= ~(1 << VARYING_SLOT_COL0
);
823 newProg
->InputsRead
= progA
->InputsRead
| inputsB
;
824 newProg
->OutputsWritten
= progB
->OutputsWritten
;
825 newProg
->SamplersUsed
= progA
->SamplersUsed
| progB
->SamplersUsed
;
829 assert(0); /* XXX todo */
833 * Merge parameters (uniforms, constants, etc)
835 newProg
->Parameters
= _mesa_combine_parameter_lists(progA
->Parameters
,
838 adjust_param_indexes(newInst
+ lenA
, lenB
, numParamsA
);
846 * Populate the 'used' array with flags indicating which registers (TEMPs,
847 * INPUTs, OUTPUTs, etc, are used by the given program.
848 * \param file type of register to scan for
849 * \param used returns true/false flags for in use / free
850 * \param usedSize size of the 'used' array
853 _mesa_find_used_registers(const struct gl_program
*prog
,
854 gl_register_file file
,
855 GLboolean used
[], GLuint usedSize
)
859 memset(used
, 0, usedSize
);
861 for (i
= 0; i
< prog
->NumInstructions
; i
++) {
862 const struct prog_instruction
*inst
= prog
->Instructions
+ i
;
863 const GLuint n
= _mesa_num_inst_src_regs(inst
->Opcode
);
865 if (inst
->DstReg
.File
== file
) {
866 assert(inst
->DstReg
.Index
< usedSize
);
867 if(inst
->DstReg
.Index
< usedSize
)
868 used
[inst
->DstReg
.Index
] = GL_TRUE
;
871 for (j
= 0; j
< n
; j
++) {
872 if (inst
->SrcReg
[j
].File
== file
) {
873 assert(inst
->SrcReg
[j
].Index
< (GLint
) usedSize
);
874 if (inst
->SrcReg
[j
].Index
< (GLint
) usedSize
)
875 used
[inst
->SrcReg
[j
].Index
] = GL_TRUE
;
883 * Scan the given 'used' register flag array for the first entry
884 * that's >= firstReg.
885 * \param used vector of flags indicating registers in use (as returned
886 * by _mesa_find_used_registers())
887 * \param usedSize size of the 'used' array
888 * \param firstReg first register to start searching at
889 * \return index of unused register, or -1 if none.
892 _mesa_find_free_register(const GLboolean used
[],
893 GLuint usedSize
, GLuint firstReg
)
897 assert(firstReg
< usedSize
);
899 for (i
= firstReg
; i
< usedSize
; i
++)
909 * Check if the given register index is valid (doesn't exceed implementation-
911 * \return GL_TRUE if OK, GL_FALSE if bad index
914 _mesa_valid_register_index(const struct gl_context
*ctx
,
915 gl_shader_stage shaderType
,
916 gl_register_file file
, GLint index
)
918 const struct gl_program_constants
*c
;
920 assert(0 <= shaderType
&& shaderType
< MESA_SHADER_STAGES
);
921 c
= &ctx
->Const
.Program
[shaderType
];
924 case PROGRAM_UNDEFINED
:
925 return GL_TRUE
; /* XXX or maybe false? */
927 case PROGRAM_TEMPORARY
:
928 return index
>= 0 && index
< (GLint
) c
->MaxTemps
;
930 case PROGRAM_UNIFORM
:
931 case PROGRAM_STATE_VAR
:
932 /* aka constant buffer */
933 return index
>= 0 && index
< (GLint
) c
->MaxUniformComponents
/ 4;
935 case PROGRAM_CONSTANT
:
936 /* constant buffer w/ possible relative negative addressing */
937 return (index
> (int) c
->MaxUniformComponents
/ -4 &&
938 index
< (int) c
->MaxUniformComponents
/ 4);
944 switch (shaderType
) {
945 case MESA_SHADER_VERTEX
:
946 return index
< VERT_ATTRIB_GENERIC0
+ (GLint
) c
->MaxAttribs
;
947 case MESA_SHADER_FRAGMENT
:
948 return index
< VARYING_SLOT_VAR0
+ (GLint
) ctx
->Const
.MaxVarying
;
949 case MESA_SHADER_GEOMETRY
:
950 return index
< VARYING_SLOT_VAR0
+ (GLint
) ctx
->Const
.MaxVarying
;
959 switch (shaderType
) {
960 case MESA_SHADER_VERTEX
:
961 return index
< VARYING_SLOT_VAR0
+ (GLint
) ctx
->Const
.MaxVarying
;
962 case MESA_SHADER_FRAGMENT
:
963 return index
< FRAG_RESULT_DATA0
+ (GLint
) ctx
->Const
.MaxDrawBuffers
;
964 case MESA_SHADER_GEOMETRY
:
965 return index
< VARYING_SLOT_VAR0
+ (GLint
) ctx
->Const
.MaxVarying
;
970 case PROGRAM_ADDRESS
:
971 return index
>= 0 && index
< (GLint
) c
->MaxAddressRegs
;
975 "unexpected register file in _mesa_valid_register_index()");
983 * "Post-process" a GPU program. This is intended to be used for debugging.
984 * Example actions include no-op'ing instructions or changing instruction
988 _mesa_postprocess_program(struct gl_context
*ctx
, struct gl_program
*prog
)
990 static const GLfloat white
[4] = { 0.5, 0.5, 0.5, 0.5 };
993 GLint whiteIndex
= _mesa_add_unnamed_constant(prog
->Parameters
,
994 (gl_constant_value
*) white
,
999 for (i
= 0; i
< prog
->NumInstructions
; i
++) {
1000 struct prog_instruction
*inst
= prog
->Instructions
+ i
;
1001 const GLuint n
= _mesa_num_inst_src_regs(inst
->Opcode
);
1005 if (_mesa_is_tex_instruction(inst
->Opcode
)) {
1007 /* replace TEX/TXP/TXB with MOV */
1008 inst
->Opcode
= OPCODE_MOV
;
1009 inst
->DstReg
.WriteMask
= WRITEMASK_XYZW
;
1010 inst
->SrcReg
[0].Swizzle
= SWIZZLE_XYZW
;
1011 inst
->SrcReg
[0].Negate
= NEGATE_NONE
;
1015 /* disable shadow texture mode */
1016 inst
->TexShadow
= 0;
1020 if (inst
->Opcode
== OPCODE_TXP
) {
1022 inst
->Opcode
= OPCODE_MOV
;
1023 inst
->DstReg
.WriteMask
= WRITEMASK_XYZW
;
1024 inst
->SrcReg
[0].File
= PROGRAM_CONSTANT
;
1025 inst
->SrcReg
[0].Index
= whiteIndex
;
1026 inst
->SrcReg
[0].Swizzle
= SWIZZLE_XYZW
;
1027 inst
->SrcReg
[0].Negate
= NEGATE_NONE
;
1030 inst
->TexShadow
= 0;
1033 inst
->Opcode
= OPCODE_TEX
;
1034 inst
->TexShadow
= 0;
1041 /* Gets the minimum number of shader invocations per fragment.
1042 * This function is useful to determine if we need to do per
1043 * sample shading or per fragment shading.
1046 _mesa_get_min_invocations_per_fragment(struct gl_context
*ctx
,
1047 const struct gl_fragment_program
*prog
,
1048 bool ignore_sample_qualifier
)
1050 /* From ARB_sample_shading specification:
1051 * "Using gl_SampleID in a fragment shader causes the entire shader
1052 * to be evaluated per-sample."
1054 * "Using gl_SamplePosition in a fragment shader causes the entire
1055 * shader to be evaluated per-sample."
1057 * "If MULTISAMPLE or SAMPLE_SHADING_ARB is disabled, sample shading
1060 if (ctx
->Multisample
.Enabled
) {
1061 /* The ARB_gpu_shader5 specification says:
1063 * "Use of the "sample" qualifier on a fragment shader input
1064 * forces per-sample shading"
1066 if (prog
->IsSample
&& !ignore_sample_qualifier
)
1067 return MAX2(ctx
->DrawBuffer
->Visual
.samples
, 1);
1069 if (prog
->Base
.SystemValuesRead
& (SYSTEM_BIT_SAMPLE_ID
|
1070 SYSTEM_BIT_SAMPLE_POS
))
1071 return MAX2(ctx
->DrawBuffer
->Visual
.samples
, 1);
1072 else if (ctx
->Multisample
.SampleShading
)
1073 return MAX2(ceil(ctx
->Multisample
.MinSampleShadingValue
*
1074 ctx
->DrawBuffer
->Visual
.samples
), 1);