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 tessellation control program object.
292 _mesa_init_tess_ctrl_program(struct gl_context
*ctx
,
293 struct gl_tess_ctrl_program
*prog
,
294 GLenum target
, GLuint id
)
297 init_program_struct(&prog
->Base
, target
, id
);
305 * Initialize a new tessellation evaluation program object.
308 _mesa_init_tess_eval_program(struct gl_context
*ctx
,
309 struct gl_tess_eval_program
*prog
,
310 GLenum target
, GLuint id
)
313 init_program_struct(&prog
->Base
, target
, id
);
321 * Initialize a new geometry program object.
324 _mesa_init_geometry_program(struct gl_context
*ctx
,
325 struct gl_geometry_program
*prog
,
326 GLenum target
, GLuint id
)
329 init_program_struct(&prog
->Base
, target
, id
);
337 * Allocate and initialize a new fragment/vertex program object but
338 * don't put it into the program hash table. Called via
339 * ctx->Driver.NewProgram. May be overridden (ie. replaced) by a
340 * device driver function to implement OO deriviation with additional
341 * types not understood by this function.
344 * \param id program id/number
345 * \param target program target/type
346 * \return pointer to new program object
349 _mesa_new_program(struct gl_context
*ctx
, GLenum target
, GLuint id
)
351 struct gl_program
*prog
;
353 case GL_VERTEX_PROGRAM_ARB
: /* == GL_VERTEX_PROGRAM_NV */
354 prog
= _mesa_init_vertex_program(ctx
, CALLOC_STRUCT(gl_vertex_program
),
357 case GL_FRAGMENT_PROGRAM_NV
:
358 case GL_FRAGMENT_PROGRAM_ARB
:
359 prog
=_mesa_init_fragment_program(ctx
,
360 CALLOC_STRUCT(gl_fragment_program
),
363 case GL_GEOMETRY_PROGRAM_NV
:
364 prog
= _mesa_init_geometry_program(ctx
,
365 CALLOC_STRUCT(gl_geometry_program
),
368 case GL_TESS_CONTROL_PROGRAM_NV
:
369 prog
= _mesa_init_tess_ctrl_program(ctx
,
370 CALLOC_STRUCT(gl_tess_ctrl_program
),
373 case GL_TESS_EVALUATION_PROGRAM_NV
:
374 prog
= _mesa_init_tess_eval_program(ctx
,
375 CALLOC_STRUCT(gl_tess_eval_program
),
378 case GL_COMPUTE_PROGRAM_NV
:
379 prog
= _mesa_init_compute_program(ctx
,
380 CALLOC_STRUCT(gl_compute_program
),
384 _mesa_problem(ctx
, "bad target in _mesa_new_program");
392 * Delete a program and remove it from the hash table, ignoring the
394 * Called via ctx->Driver.DeleteProgram. May be wrapped (OO deriviation)
395 * by a device driver function.
398 _mesa_delete_program(struct gl_context
*ctx
, struct gl_program
*prog
)
402 assert(prog
->RefCount
==0);
404 if (prog
== &_mesa_DummyProgram
)
408 free(prog
->LocalParams
);
410 if (prog
->Instructions
) {
411 _mesa_free_instructions(prog
->Instructions
, prog
->NumInstructions
);
413 if (prog
->Parameters
) {
414 _mesa_free_parameter_list(prog
->Parameters
);
418 ralloc_free(prog
->nir
);
426 * Return the gl_program object for a given ID.
427 * Basically just a wrapper for _mesa_HashLookup() to avoid a lot of
431 _mesa_lookup_program(struct gl_context
*ctx
, GLuint id
)
434 return (struct gl_program
*) _mesa_HashLookup(ctx
->Shared
->Programs
, id
);
441 * Reference counting for vertex/fragment programs
442 * This is normally only called from the _mesa_reference_program() macro
443 * when there's a real pointer change.
446 _mesa_reference_program_(struct gl_context
*ctx
,
447 struct gl_program
**ptr
,
448 struct gl_program
*prog
)
454 if ((*ptr
)->Target
== GL_VERTEX_PROGRAM_ARB
)
455 assert(prog
->Target
== GL_VERTEX_PROGRAM_ARB
);
456 else if ((*ptr
)->Target
== GL_FRAGMENT_PROGRAM_ARB
)
457 assert(prog
->Target
== GL_FRAGMENT_PROGRAM_ARB
||
458 prog
->Target
== GL_FRAGMENT_PROGRAM_NV
);
459 else if ((*ptr
)->Target
== GL_GEOMETRY_PROGRAM_NV
)
460 assert(prog
->Target
== GL_GEOMETRY_PROGRAM_NV
);
465 GLboolean deleteFlag
;
467 /*mtx_lock(&(*ptr)->Mutex);*/
469 printf("Program %p ID=%u Target=%s Refcount-- to %d\n",
471 ((*ptr
)->Target
== GL_VERTEX_PROGRAM_ARB
? "VP" :
472 ((*ptr
)->Target
== GL_GEOMETRY_PROGRAM_NV
? "GP" : "FP")),
473 (*ptr
)->RefCount
- 1);
475 assert((*ptr
)->RefCount
> 0);
478 deleteFlag
= ((*ptr
)->RefCount
== 0);
479 /*mtx_lock(&(*ptr)->Mutex);*/
483 ctx
->Driver
.DeleteProgram(ctx
, *ptr
);
491 /*mtx_lock(&prog->Mutex);*/
494 printf("Program %p ID=%u Target=%s Refcount++ to %d\n",
496 (prog
->Target
== GL_VERTEX_PROGRAM_ARB
? "VP" :
497 (prog
->Target
== GL_GEOMETRY_PROGRAM_NV
? "GP" : "FP")),
500 /*mtx_unlock(&prog->Mutex);*/
508 * Return a copy of a program.
509 * XXX Problem here if the program object is actually OO-derivation
510 * made by a device driver.
513 _mesa_clone_program(struct gl_context
*ctx
, const struct gl_program
*prog
)
515 struct gl_program
*clone
;
517 clone
= ctx
->Driver
.NewProgram(ctx
, prog
->Target
, prog
->Id
);
521 assert(clone
->Target
== prog
->Target
);
522 assert(clone
->RefCount
== 1);
524 clone
->String
= (GLubyte
*) strdup((char *) prog
->String
);
525 clone
->Format
= prog
->Format
;
526 clone
->Instructions
= _mesa_alloc_instructions(prog
->NumInstructions
);
527 if (!clone
->Instructions
) {
528 _mesa_reference_program(ctx
, &clone
, NULL
);
531 _mesa_copy_instructions(clone
->Instructions
, prog
->Instructions
,
532 prog
->NumInstructions
);
533 clone
->InputsRead
= prog
->InputsRead
;
534 clone
->OutputsWritten
= prog
->OutputsWritten
;
535 clone
->SamplersUsed
= prog
->SamplersUsed
;
536 clone
->ShadowSamplers
= prog
->ShadowSamplers
;
537 memcpy(clone
->TexturesUsed
, prog
->TexturesUsed
, sizeof(prog
->TexturesUsed
));
539 if (prog
->Parameters
)
540 clone
->Parameters
= _mesa_clone_parameter_list(prog
->Parameters
);
541 if (prog
->LocalParams
) {
542 clone
->LocalParams
= malloc(MAX_PROGRAM_LOCAL_PARAMS
*
544 if (!clone
->LocalParams
) {
545 _mesa_reference_program(ctx
, &clone
, NULL
);
548 memcpy(clone
->LocalParams
, prog
->LocalParams
,
549 MAX_PROGRAM_LOCAL_PARAMS
* sizeof(float[4]));
551 clone
->IndirectRegisterFiles
= prog
->IndirectRegisterFiles
;
552 clone
->NumInstructions
= prog
->NumInstructions
;
553 clone
->NumTemporaries
= prog
->NumTemporaries
;
554 clone
->NumParameters
= prog
->NumParameters
;
555 clone
->NumAttributes
= prog
->NumAttributes
;
556 clone
->NumAddressRegs
= prog
->NumAddressRegs
;
557 clone
->NumNativeInstructions
= prog
->NumNativeInstructions
;
558 clone
->NumNativeTemporaries
= prog
->NumNativeTemporaries
;
559 clone
->NumNativeParameters
= prog
->NumNativeParameters
;
560 clone
->NumNativeAttributes
= prog
->NumNativeAttributes
;
561 clone
->NumNativeAddressRegs
= prog
->NumNativeAddressRegs
;
562 clone
->NumAluInstructions
= prog
->NumAluInstructions
;
563 clone
->NumTexInstructions
= prog
->NumTexInstructions
;
564 clone
->NumTexIndirections
= prog
->NumTexIndirections
;
565 clone
->NumNativeAluInstructions
= prog
->NumNativeAluInstructions
;
566 clone
->NumNativeTexInstructions
= prog
->NumNativeTexInstructions
;
567 clone
->NumNativeTexIndirections
= prog
->NumNativeTexIndirections
;
569 switch (prog
->Target
) {
570 case GL_VERTEX_PROGRAM_ARB
:
572 const struct gl_vertex_program
*vp
= gl_vertex_program_const(prog
);
573 struct gl_vertex_program
*vpc
= gl_vertex_program(clone
);
574 vpc
->IsPositionInvariant
= vp
->IsPositionInvariant
;
577 case GL_FRAGMENT_PROGRAM_ARB
:
579 const struct gl_fragment_program
*fp
= gl_fragment_program_const(prog
);
580 struct gl_fragment_program
*fpc
= gl_fragment_program(clone
);
581 fpc
->UsesKill
= fp
->UsesKill
;
582 fpc
->UsesDFdy
= fp
->UsesDFdy
;
583 fpc
->OriginUpperLeft
= fp
->OriginUpperLeft
;
584 fpc
->PixelCenterInteger
= fp
->PixelCenterInteger
;
587 case GL_GEOMETRY_PROGRAM_NV
:
589 const struct gl_geometry_program
*gp
= gl_geometry_program_const(prog
);
590 struct gl_geometry_program
*gpc
= gl_geometry_program(clone
);
591 gpc
->VerticesOut
= gp
->VerticesOut
;
592 gpc
->InputType
= gp
->InputType
;
593 gpc
->Invocations
= gp
->Invocations
;
594 gpc
->OutputType
= gp
->OutputType
;
595 gpc
->UsesEndPrimitive
= gp
->UsesEndPrimitive
;
596 gpc
->UsesStreams
= gp
->UsesStreams
;
599 case GL_TESS_CONTROL_PROGRAM_NV
:
601 const struct gl_tess_ctrl_program
*tcp
= gl_tess_ctrl_program_const(prog
);
602 struct gl_tess_ctrl_program
*tcpc
= gl_tess_ctrl_program(clone
);
603 tcpc
->VerticesOut
= tcp
->VerticesOut
;
606 case GL_TESS_EVALUATION_PROGRAM_NV
:
608 const struct gl_tess_eval_program
*tep
= gl_tess_eval_program_const(prog
);
609 struct gl_tess_eval_program
*tepc
= gl_tess_eval_program(clone
);
610 tepc
->PrimitiveMode
= tep
->PrimitiveMode
;
611 tepc
->Spacing
= tep
->Spacing
;
612 tepc
->VertexOrder
= tep
->VertexOrder
;
613 tepc
->PointMode
= tep
->PointMode
;
617 _mesa_problem(NULL
, "Unexpected target in _mesa_clone_program");
625 * Insert 'count' NOP instructions at 'start' in the given program.
626 * Adjust branch targets accordingly.
629 _mesa_insert_instructions(struct gl_program
*prog
, GLuint start
, GLuint count
)
631 const GLuint origLen
= prog
->NumInstructions
;
632 const GLuint newLen
= origLen
+ count
;
633 struct prog_instruction
*newInst
;
636 /* adjust branches */
637 for (i
= 0; i
< prog
->NumInstructions
; i
++) {
638 struct prog_instruction
*inst
= prog
->Instructions
+ i
;
639 if (inst
->BranchTarget
> 0) {
640 if ((GLuint
)inst
->BranchTarget
>= start
) {
641 inst
->BranchTarget
+= count
;
646 /* Alloc storage for new instructions */
647 newInst
= _mesa_alloc_instructions(newLen
);
652 /* Copy 'start' instructions into new instruction buffer */
653 _mesa_copy_instructions(newInst
, prog
->Instructions
, start
);
655 /* init the new instructions */
656 _mesa_init_instructions(newInst
+ start
, count
);
658 /* Copy the remaining/tail instructions to new inst buffer */
659 _mesa_copy_instructions(newInst
+ start
+ count
,
660 prog
->Instructions
+ start
,
663 /* free old instructions */
664 _mesa_free_instructions(prog
->Instructions
, origLen
);
666 /* install new instructions */
667 prog
->Instructions
= newInst
;
668 prog
->NumInstructions
= newLen
;
674 * Delete 'count' instructions at 'start' in the given program.
675 * Adjust branch targets accordingly.
678 _mesa_delete_instructions(struct gl_program
*prog
, GLuint start
, GLuint count
)
680 const GLuint origLen
= prog
->NumInstructions
;
681 const GLuint newLen
= origLen
- count
;
682 struct prog_instruction
*newInst
;
685 /* adjust branches */
686 for (i
= 0; i
< prog
->NumInstructions
; i
++) {
687 struct prog_instruction
*inst
= prog
->Instructions
+ i
;
688 if (inst
->BranchTarget
> 0) {
689 if (inst
->BranchTarget
> (GLint
) start
) {
690 inst
->BranchTarget
-= count
;
695 /* Alloc storage for new instructions */
696 newInst
= _mesa_alloc_instructions(newLen
);
701 /* Copy 'start' instructions into new instruction buffer */
702 _mesa_copy_instructions(newInst
, prog
->Instructions
, start
);
704 /* Copy the remaining/tail instructions to new inst buffer */
705 _mesa_copy_instructions(newInst
+ start
,
706 prog
->Instructions
+ start
+ count
,
709 /* free old instructions */
710 _mesa_free_instructions(prog
->Instructions
, origLen
);
712 /* install new instructions */
713 prog
->Instructions
= newInst
;
714 prog
->NumInstructions
= newLen
;
721 * Search instructions for registers that match (oldFile, oldIndex),
722 * replacing them with (newFile, newIndex).
725 replace_registers(struct prog_instruction
*inst
, GLuint numInst
,
726 GLuint oldFile
, GLuint oldIndex
,
727 GLuint newFile
, GLuint newIndex
)
730 for (i
= 0; i
< numInst
; i
++) {
732 for (j
= 0; j
< _mesa_num_inst_src_regs(inst
[i
].Opcode
); j
++) {
733 if (inst
[i
].SrcReg
[j
].File
== oldFile
&&
734 inst
[i
].SrcReg
[j
].Index
== oldIndex
) {
735 inst
[i
].SrcReg
[j
].File
= newFile
;
736 inst
[i
].SrcReg
[j
].Index
= newIndex
;
740 if (inst
[i
].DstReg
.File
== oldFile
&& inst
[i
].DstReg
.Index
== oldIndex
) {
741 inst
[i
].DstReg
.File
= newFile
;
742 inst
[i
].DstReg
.Index
= newIndex
;
749 * Search instructions for references to program parameters. When found,
750 * increment the parameter index by 'offset'.
751 * Used when combining programs.
754 adjust_param_indexes(struct prog_instruction
*inst
, GLuint numInst
,
758 for (i
= 0; i
< numInst
; i
++) {
759 for (j
= 0; j
< _mesa_num_inst_src_regs(inst
[i
].Opcode
); j
++) {
760 GLuint f
= inst
[i
].SrcReg
[j
].File
;
761 if (f
== PROGRAM_CONSTANT
||
762 f
== PROGRAM_UNIFORM
||
763 f
== PROGRAM_STATE_VAR
) {
764 inst
[i
].SrcReg
[j
].Index
+= offset
;
772 * Combine two programs into one. Fix instructions so the outputs of
773 * the first program go to the inputs of the second program.
776 _mesa_combine_programs(struct gl_context
*ctx
,
777 const struct gl_program
*progA
,
778 const struct gl_program
*progB
)
780 struct prog_instruction
*newInst
;
781 struct gl_program
*newProg
;
782 const GLuint lenA
= progA
->NumInstructions
- 1; /* omit END instr */
783 const GLuint lenB
= progB
->NumInstructions
;
784 const GLuint numParamsA
= _mesa_num_parameters(progA
->Parameters
);
785 const GLuint newLength
= lenA
+ lenB
;
786 GLboolean usedTemps
[MAX_PROGRAM_TEMPS
];
787 GLuint firstTemp
= 0;
788 GLbitfield64 inputsB
;
791 assert(progA
->Target
== progB
->Target
);
793 newInst
= _mesa_alloc_instructions(newLength
);
797 _mesa_copy_instructions(newInst
, progA
->Instructions
, lenA
);
798 _mesa_copy_instructions(newInst
+ lenA
, progB
->Instructions
, lenB
);
800 /* adjust branch / instruction addresses for B's instructions */
801 for (i
= 0; i
< lenB
; i
++) {
802 newInst
[lenA
+ i
].BranchTarget
+= lenA
;
805 newProg
= ctx
->Driver
.NewProgram(ctx
, progA
->Target
, 0);
806 newProg
->Instructions
= newInst
;
807 newProg
->NumInstructions
= newLength
;
809 /* find used temp regs (we may need new temps below) */
810 _mesa_find_used_registers(newProg
, PROGRAM_TEMPORARY
,
811 usedTemps
, MAX_PROGRAM_TEMPS
);
813 if (newProg
->Target
== GL_FRAGMENT_PROGRAM_ARB
) {
814 const struct gl_fragment_program
*fprogA
, *fprogB
;
815 struct gl_fragment_program
*newFprog
;
816 GLbitfield64 progB_inputsRead
= progB
->InputsRead
;
817 GLint progB_colorFile
, progB_colorIndex
;
819 fprogA
= gl_fragment_program_const(progA
);
820 fprogB
= gl_fragment_program_const(progB
);
821 newFprog
= gl_fragment_program(newProg
);
823 newFprog
->UsesKill
= fprogA
->UsesKill
|| fprogB
->UsesKill
;
824 newFprog
->UsesDFdy
= fprogA
->UsesDFdy
|| fprogB
->UsesDFdy
;
826 /* We'll do a search and replace for instances
827 * of progB_colorFile/progB_colorIndex below...
829 progB_colorFile
= PROGRAM_INPUT
;
830 progB_colorIndex
= VARYING_SLOT_COL0
;
833 * The fragment program may get color from a state var rather than
834 * a fragment input (vertex output) if it's constant.
835 * See the texenvprogram.c code.
836 * So, search the program's parameter list now to see if the program
837 * gets color from a state var instead of a conventional fragment
840 for (i
= 0; i
< progB
->Parameters
->NumParameters
; i
++) {
841 struct gl_program_parameter
*p
= &progB
->Parameters
->Parameters
[i
];
842 if (p
->Type
== PROGRAM_STATE_VAR
&&
843 p
->StateIndexes
[0] == STATE_INTERNAL
&&
844 p
->StateIndexes
[1] == STATE_CURRENT_ATTRIB
&&
845 (int) p
->StateIndexes
[2] == (int) VERT_ATTRIB_COLOR0
) {
846 progB_inputsRead
|= VARYING_BIT_COL0
;
847 progB_colorFile
= PROGRAM_STATE_VAR
;
848 progB_colorIndex
= i
;
853 /* Connect color outputs of fprogA to color inputs of fprogB, via a
854 * new temporary register.
856 if ((progA
->OutputsWritten
& BITFIELD64_BIT(FRAG_RESULT_COLOR
)) &&
857 (progB_inputsRead
& VARYING_BIT_COL0
)) {
858 GLint tempReg
= _mesa_find_free_register(usedTemps
, MAX_PROGRAM_TEMPS
,
861 _mesa_problem(ctx
, "No free temp regs found in "
862 "_mesa_combine_programs(), using 31");
865 firstTemp
= tempReg
+ 1;
867 /* replace writes to result.color[0] with tempReg */
868 replace_registers(newInst
, lenA
,
869 PROGRAM_OUTPUT
, FRAG_RESULT_COLOR
,
870 PROGRAM_TEMPORARY
, tempReg
);
871 /* replace reads from the input color with tempReg */
872 replace_registers(newInst
+ lenA
, lenB
,
873 progB_colorFile
, progB_colorIndex
, /* search for */
874 PROGRAM_TEMPORARY
, tempReg
/* replace with */ );
877 /* compute combined program's InputsRead */
878 inputsB
= progB_inputsRead
;
879 if (progA
->OutputsWritten
& BITFIELD64_BIT(FRAG_RESULT_COLOR
)) {
880 inputsB
&= ~(1 << VARYING_SLOT_COL0
);
882 newProg
->InputsRead
= progA
->InputsRead
| inputsB
;
883 newProg
->OutputsWritten
= progB
->OutputsWritten
;
884 newProg
->SamplersUsed
= progA
->SamplersUsed
| progB
->SamplersUsed
;
888 assert(0); /* XXX todo */
892 * Merge parameters (uniforms, constants, etc)
894 newProg
->Parameters
= _mesa_combine_parameter_lists(progA
->Parameters
,
897 adjust_param_indexes(newInst
+ lenA
, lenB
, numParamsA
);
905 * Populate the 'used' array with flags indicating which registers (TEMPs,
906 * INPUTs, OUTPUTs, etc, are used by the given program.
907 * \param file type of register to scan for
908 * \param used returns true/false flags for in use / free
909 * \param usedSize size of the 'used' array
912 _mesa_find_used_registers(const struct gl_program
*prog
,
913 gl_register_file file
,
914 GLboolean used
[], GLuint usedSize
)
918 memset(used
, 0, usedSize
);
920 for (i
= 0; i
< prog
->NumInstructions
; i
++) {
921 const struct prog_instruction
*inst
= prog
->Instructions
+ i
;
922 const GLuint n
= _mesa_num_inst_src_regs(inst
->Opcode
);
924 if (inst
->DstReg
.File
== file
) {
925 assert(inst
->DstReg
.Index
< usedSize
);
926 if(inst
->DstReg
.Index
< usedSize
)
927 used
[inst
->DstReg
.Index
] = GL_TRUE
;
930 for (j
= 0; j
< n
; j
++) {
931 if (inst
->SrcReg
[j
].File
== file
) {
932 assert(inst
->SrcReg
[j
].Index
< (GLint
) usedSize
);
933 if (inst
->SrcReg
[j
].Index
< (GLint
) usedSize
)
934 used
[inst
->SrcReg
[j
].Index
] = GL_TRUE
;
942 * Scan the given 'used' register flag array for the first entry
943 * that's >= firstReg.
944 * \param used vector of flags indicating registers in use (as returned
945 * by _mesa_find_used_registers())
946 * \param usedSize size of the 'used' array
947 * \param firstReg first register to start searching at
948 * \return index of unused register, or -1 if none.
951 _mesa_find_free_register(const GLboolean used
[],
952 GLuint usedSize
, GLuint firstReg
)
956 assert(firstReg
< usedSize
);
958 for (i
= firstReg
; i
< usedSize
; i
++)
968 * Check if the given register index is valid (doesn't exceed implementation-
970 * \return GL_TRUE if OK, GL_FALSE if bad index
973 _mesa_valid_register_index(const struct gl_context
*ctx
,
974 gl_shader_stage shaderType
,
975 gl_register_file file
, GLint index
)
977 const struct gl_program_constants
*c
;
979 assert(0 <= shaderType
&& shaderType
< MESA_SHADER_STAGES
);
980 c
= &ctx
->Const
.Program
[shaderType
];
983 case PROGRAM_UNDEFINED
:
984 return GL_TRUE
; /* XXX or maybe false? */
986 case PROGRAM_TEMPORARY
:
987 return index
>= 0 && index
< (GLint
) c
->MaxTemps
;
989 case PROGRAM_UNIFORM
:
990 case PROGRAM_STATE_VAR
:
991 /* aka constant buffer */
992 return index
>= 0 && index
< (GLint
) c
->MaxUniformComponents
/ 4;
994 case PROGRAM_CONSTANT
:
995 /* constant buffer w/ possible relative negative addressing */
996 return (index
> (int) c
->MaxUniformComponents
/ -4 &&
997 index
< (int) c
->MaxUniformComponents
/ 4);
1003 switch (shaderType
) {
1004 case MESA_SHADER_VERTEX
:
1005 return index
< VERT_ATTRIB_GENERIC0
+ (GLint
) c
->MaxAttribs
;
1006 case MESA_SHADER_FRAGMENT
:
1007 return index
< VARYING_SLOT_VAR0
+ (GLint
) ctx
->Const
.MaxVarying
;
1008 case MESA_SHADER_GEOMETRY
:
1009 return index
< VARYING_SLOT_VAR0
+ (GLint
) ctx
->Const
.MaxVarying
;
1014 case PROGRAM_OUTPUT
:
1018 switch (shaderType
) {
1019 case MESA_SHADER_VERTEX
:
1020 return index
< VARYING_SLOT_VAR0
+ (GLint
) ctx
->Const
.MaxVarying
;
1021 case MESA_SHADER_FRAGMENT
:
1022 return index
< FRAG_RESULT_DATA0
+ (GLint
) ctx
->Const
.MaxDrawBuffers
;
1023 case MESA_SHADER_GEOMETRY
:
1024 return index
< VARYING_SLOT_VAR0
+ (GLint
) ctx
->Const
.MaxVarying
;
1029 case PROGRAM_ADDRESS
:
1030 return index
>= 0 && index
< (GLint
) c
->MaxAddressRegs
;
1034 "unexpected register file in _mesa_valid_register_index()");
1042 * "Post-process" a GPU program. This is intended to be used for debugging.
1043 * Example actions include no-op'ing instructions or changing instruction
1047 _mesa_postprocess_program(struct gl_context
*ctx
, struct gl_program
*prog
)
1049 static const GLfloat white
[4] = { 0.5, 0.5, 0.5, 0.5 };
1051 GLuint whiteSwizzle
;
1052 GLint whiteIndex
= _mesa_add_unnamed_constant(prog
->Parameters
,
1053 (gl_constant_value
*) white
,
1058 for (i
= 0; i
< prog
->NumInstructions
; i
++) {
1059 struct prog_instruction
*inst
= prog
->Instructions
+ i
;
1060 const GLuint n
= _mesa_num_inst_src_regs(inst
->Opcode
);
1064 if (_mesa_is_tex_instruction(inst
->Opcode
)) {
1066 /* replace TEX/TXP/TXB with MOV */
1067 inst
->Opcode
= OPCODE_MOV
;
1068 inst
->DstReg
.WriteMask
= WRITEMASK_XYZW
;
1069 inst
->SrcReg
[0].Swizzle
= SWIZZLE_XYZW
;
1070 inst
->SrcReg
[0].Negate
= NEGATE_NONE
;
1074 /* disable shadow texture mode */
1075 inst
->TexShadow
= 0;
1079 if (inst
->Opcode
== OPCODE_TXP
) {
1081 inst
->Opcode
= OPCODE_MOV
;
1082 inst
->DstReg
.WriteMask
= WRITEMASK_XYZW
;
1083 inst
->SrcReg
[0].File
= PROGRAM_CONSTANT
;
1084 inst
->SrcReg
[0].Index
= whiteIndex
;
1085 inst
->SrcReg
[0].Swizzle
= SWIZZLE_XYZW
;
1086 inst
->SrcReg
[0].Negate
= NEGATE_NONE
;
1089 inst
->TexShadow
= 0;
1092 inst
->Opcode
= OPCODE_TEX
;
1093 inst
->TexShadow
= 0;
1100 /* Gets the minimum number of shader invocations per fragment.
1101 * This function is useful to determine if we need to do per
1102 * sample shading or per fragment shading.
1105 _mesa_get_min_invocations_per_fragment(struct gl_context
*ctx
,
1106 const struct gl_fragment_program
*prog
,
1107 bool ignore_sample_qualifier
)
1109 /* From ARB_sample_shading specification:
1110 * "Using gl_SampleID in a fragment shader causes the entire shader
1111 * to be evaluated per-sample."
1113 * "Using gl_SamplePosition in a fragment shader causes the entire
1114 * shader to be evaluated per-sample."
1116 * "If MULTISAMPLE or SAMPLE_SHADING_ARB is disabled, sample shading
1119 if (ctx
->Multisample
.Enabled
) {
1120 /* The ARB_gpu_shader5 specification says:
1122 * "Use of the "sample" qualifier on a fragment shader input
1123 * forces per-sample shading"
1125 if (prog
->IsSample
&& !ignore_sample_qualifier
)
1126 return MAX2(ctx
->DrawBuffer
->Visual
.samples
, 1);
1128 if (prog
->Base
.SystemValuesRead
& (SYSTEM_BIT_SAMPLE_ID
|
1129 SYSTEM_BIT_SAMPLE_POS
))
1130 return MAX2(ctx
->DrawBuffer
->Visual
.samples
, 1);
1131 else if (ctx
->Multisample
.SampleShading
)
1132 return MAX2(ceil(ctx
->Multisample
.MinSampleShadingValue
*
1133 ctx
->DrawBuffer
->Visual
.samples
), 1);