1 /**************************************************************************
3 * Copyright 2007-2008 Tungsten Graphics, Inc., Cedar Park, Texas.
6 * Permission is hereby granted, free of charge, to any person obtaining a
7 * copy of this software and associated documentation files (the
8 * "Software"), to deal in the Software without restriction, including
9 * without limitation the rights to use, copy, modify, merge, publish,
10 * distribute, sub license, and/or sell copies of the Software, and to
11 * permit persons to whom the Software is furnished to do so, subject to
12 * the following conditions:
14 * The above copyright notice and this permission notice (including the
15 * next paragraph) shall be included in all copies or substantial portions
18 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
19 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
20 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT.
21 * IN NO EVENT SHALL TUNGSTEN GRAPHICS AND/OR ITS SUPPLIERS BE LIABLE FOR
22 * ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
23 * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
24 * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
26 **************************************************************************/
33 #include "pipe/p_compiler.h"
34 #include "pipe/p_shader_tokens.h"
35 #include "tgsi/tgsi_parse.h"
36 #include "tgsi/tgsi_build.h"
37 #include "tgsi/tgsi_util.h"
38 #include "tgsi/tgsi_dump.h"
39 #include "tgsi/tgsi_sanity.h"
40 #include "st_mesa_to_tgsi.h"
41 #include "shader/prog_instruction.h"
42 #include "shader/prog_parameter.h"
43 #include "shader/prog_print.h"
44 #include "util/u_debug.h"
47 * Map mesa register file to TGSI register file.
51 gl_register_file file
,
53 const GLuint immediateMapping
[],
54 GLboolean indirectAccess
)
57 case PROGRAM_UNDEFINED
:
58 return TGSI_FILE_NULL
;
59 case PROGRAM_TEMPORARY
:
60 return TGSI_FILE_TEMPORARY
;
61 /*case PROGRAM_LOCAL_PARAM:*/
62 /*case PROGRAM_ENV_PARAM:*/
64 /* Because of the longstanding problem with mesa arb shaders
65 * where constants, immediates and state variables are all
66 * bundled together as PROGRAM_STATE_VAR, we can't tell from the
67 * mesa register file whether this is a CONSTANT or an
68 * IMMEDIATE, hence we need all the other information.
70 case PROGRAM_STATE_VAR
:
71 case PROGRAM_NAMED_PARAM
:
73 if (!indirectAccess
&& immediateMapping
&& immediateMapping
[index
] != ~0)
74 return TGSI_FILE_IMMEDIATE
;
76 return TGSI_FILE_CONSTANT
;
77 case PROGRAM_CONSTANT
:
79 return TGSI_FILE_CONSTANT
;
80 assert(immediateMapping
[index
] != ~0);
81 return TGSI_FILE_IMMEDIATE
;
83 return TGSI_FILE_INPUT
;
85 return TGSI_FILE_OUTPUT
;
87 return TGSI_FILE_ADDRESS
;
90 return TGSI_FILE_NULL
;
95 * Map mesa register file index to TGSI index.
96 * Take special care when processing input and output indices.
97 * \param file one of TGSI_FILE_x
98 * \param index the mesa register file index
99 * \param inputMapping maps Mesa input indexes to TGSI input indexes
100 * \param outputMapping maps Mesa output indexes to TGSI output indexes
103 map_register_file_index(
106 const GLuint inputMapping
[],
107 const GLuint outputMapping
[],
108 const GLuint immediateMapping
[],
109 GLboolean indirectAccess
)
112 case TGSI_FILE_INPUT
:
113 /* inputs are mapped according to the user-defined map */
114 return inputMapping
[index
];
116 case TGSI_FILE_OUTPUT
:
117 return outputMapping
[index
];
119 case TGSI_FILE_IMMEDIATE
:
122 assert(immediateMapping
[index
] != ~0);
123 return immediateMapping
[index
];
131 * Map mesa texture target to TGSI texture target.
139 /* XXX remove this line after we've checked that the rest of gallium
140 * can handle the TGSI_TEXTURE_SHADOWx tokens.
144 switch( textarget
) {
145 case TEXTURE_1D_INDEX
:
147 return TGSI_TEXTURE_SHADOW1D
;
149 return TGSI_TEXTURE_1D
;
150 case TEXTURE_2D_INDEX
:
152 return TGSI_TEXTURE_SHADOW2D
;
154 return TGSI_TEXTURE_2D
;
155 case TEXTURE_3D_INDEX
:
156 return TGSI_TEXTURE_3D
;
157 case TEXTURE_CUBE_INDEX
:
158 return TGSI_TEXTURE_CUBE
;
159 case TEXTURE_RECT_INDEX
:
161 return TGSI_TEXTURE_SHADOWRECT
;
163 return TGSI_TEXTURE_RECT
;
168 return TGSI_TEXTURE_1D
;
177 return TGSI_SAT_NONE
;
178 case SATURATE_ZERO_ONE
:
179 return TGSI_SAT_ZERO_ONE
;
180 case SATURATE_PLUS_MINUS_ONE
:
181 return TGSI_SAT_MINUS_PLUS_ONE
;
184 return TGSI_SAT_NONE
;
192 assert( WRITEMASK_X
== TGSI_WRITEMASK_X
);
193 assert( WRITEMASK_Y
== TGSI_WRITEMASK_Y
);
194 assert( WRITEMASK_Z
== TGSI_WRITEMASK_Z
);
195 assert( WRITEMASK_W
== TGSI_WRITEMASK_W
);
196 assert( (writemask
& ~TGSI_WRITEMASK_XYZW
) == 0 );
201 static struct tgsi_full_immediate
202 make_immediate(const float *value
, uint size
)
204 struct tgsi_full_immediate imm
;
206 imm
= tgsi_default_full_immediate();
207 imm
.Immediate
.NrTokens
+= size
;
208 imm
.Immediate
.DataType
= TGSI_IMM_FLOAT32
;
209 imm
.u
.Pointer
= value
;
215 const struct prog_instruction
*inst
,
216 struct tgsi_full_instruction
*fullinst
,
217 const GLuint inputMapping
[],
218 const GLuint outputMapping
[],
219 const GLuint immediateMapping
[],
220 GLboolean indirectAccess
,
221 GLuint preamble_size
,
223 GLboolean
*insideSubroutine
,
227 struct tgsi_full_dst_register
*fulldst
;
228 struct tgsi_full_src_register
*fullsrc
;
230 *fullinst
= tgsi_default_full_instruction();
232 fullinst
->Instruction
.Saturate
= convert_sat( inst
->SaturateMode
);
233 fullinst
->Instruction
.NumDstRegs
= _mesa_num_inst_dst_regs( inst
->Opcode
);
234 fullinst
->Instruction
.NumSrcRegs
= _mesa_num_inst_src_regs( inst
->Opcode
);
236 fulldst
= &fullinst
->FullDstRegisters
[0];
237 fulldst
->DstRegister
.File
= map_register_file( inst
->DstReg
.File
, 0, NULL
, GL_FALSE
);
238 fulldst
->DstRegister
.Index
= map_register_file_index(
239 fulldst
->DstRegister
.File
,
245 fulldst
->DstRegister
.WriteMask
= convert_writemask( inst
->DstReg
.WriteMask
);
247 for (i
= 0; i
< fullinst
->Instruction
.NumSrcRegs
; i
++) {
250 fullsrc
= &fullinst
->FullSrcRegisters
[i
];
252 if (procType
== TGSI_PROCESSOR_FRAGMENT
&&
253 inst
->SrcReg
[i
].File
== PROGRAM_INPUT
&&
254 inst
->SrcReg
[i
].Index
== FRAG_ATTRIB_WPOS
) {
255 /* special case of INPUT[WPOS] */
256 fullsrc
->SrcRegister
.File
= TGSI_FILE_TEMPORARY
;
257 fullsrc
->SrcRegister
.Index
= wposTemp
;
260 /* any other src register */
261 fullsrc
->SrcRegister
.File
= map_register_file(
262 inst
->SrcReg
[i
].File
,
263 inst
->SrcReg
[i
].Index
,
266 fullsrc
->SrcRegister
.Index
= map_register_file_index(
267 fullsrc
->SrcRegister
.File
,
268 inst
->SrcReg
[i
].Index
,
275 /* swizzle (ext swizzle also depends on negation) */
278 GLboolean extended
= (inst
->SrcReg
[i
].Negate
!= NEGATE_NONE
&&
279 inst
->SrcReg
[i
].Negate
!= NEGATE_XYZW
);
280 for( j
= 0; j
< 4; j
++ ) {
281 swz
[j
] = GET_SWZ( inst
->SrcReg
[i
].Swizzle
, j
);
282 if (swz
[j
] > SWIZZLE_W
)
286 for (j
= 0; j
< 4; j
++) {
287 tgsi_util_set_src_register_extswizzle(&fullsrc
->SrcRegisterExtSwz
,
292 for (j
= 0; j
< 4; j
++) {
293 tgsi_util_set_src_register_swizzle(&fullsrc
->SrcRegister
,
299 if( inst
->SrcReg
[i
].Negate
== NEGATE_XYZW
) {
300 fullsrc
->SrcRegister
.Negate
= 1;
302 else if( inst
->SrcReg
[i
].Negate
!= NEGATE_NONE
) {
303 if( inst
->SrcReg
[i
].Negate
& NEGATE_X
) {
304 fullsrc
->SrcRegisterExtSwz
.NegateX
= 1;
306 if( inst
->SrcReg
[i
].Negate
& NEGATE_Y
) {
307 fullsrc
->SrcRegisterExtSwz
.NegateY
= 1;
309 if( inst
->SrcReg
[i
].Negate
& NEGATE_Z
) {
310 fullsrc
->SrcRegisterExtSwz
.NegateZ
= 1;
312 if( inst
->SrcReg
[i
].Negate
& NEGATE_W
) {
313 fullsrc
->SrcRegisterExtSwz
.NegateW
= 1;
317 if( inst
->SrcReg
[i
].Abs
) {
318 fullsrc
->SrcRegisterExtMod
.Absolute
= 1;
321 if( inst
->SrcReg
[i
].RelAddr
) {
322 fullsrc
->SrcRegister
.Indirect
= 1;
324 fullsrc
->SrcRegisterInd
.File
= TGSI_FILE_ADDRESS
;
325 fullsrc
->SrcRegisterInd
.Index
= 0;
329 switch( inst
->Opcode
) {
331 fullinst
->Instruction
.Opcode
= TGSI_OPCODE_ARL
;
334 fullinst
->Instruction
.Opcode
= TGSI_OPCODE_ABS
;
337 fullinst
->Instruction
.Opcode
= TGSI_OPCODE_ADD
;
340 fullinst
->Instruction
.Opcode
= TGSI_OPCODE_BGNLOOP2
;
341 fullinst
->InstructionExtLabel
.Label
= inst
->BranchTarget
+ preamble_size
;
344 fullinst
->Instruction
.Opcode
= TGSI_OPCODE_BGNSUB
;
345 *insideSubroutine
= GL_TRUE
;
348 fullinst
->Instruction
.Opcode
= TGSI_OPCODE_BRA
;
351 fullinst
->Instruction
.Opcode
= TGSI_OPCODE_BRK
;
354 fullinst
->Instruction
.Opcode
= TGSI_OPCODE_CAL
;
355 fullinst
->InstructionExtLabel
.Label
= inst
->BranchTarget
+ preamble_size
;
358 fullinst
->Instruction
.Opcode
= TGSI_OPCODE_CMP
;
361 fullinst
->Instruction
.Opcode
= TGSI_OPCODE_CONT
;
364 fullinst
->Instruction
.Opcode
= TGSI_OPCODE_COS
;
367 fullinst
->Instruction
.Opcode
= TGSI_OPCODE_DDX
;
370 fullinst
->Instruction
.Opcode
= TGSI_OPCODE_DDY
;
373 fullinst
->Instruction
.Opcode
= TGSI_OPCODE_DP2
;
376 fullinst
->Instruction
.Opcode
= TGSI_OPCODE_DP2A
;
379 fullinst
->Instruction
.Opcode
= TGSI_OPCODE_DP3
;
382 fullinst
->Instruction
.Opcode
= TGSI_OPCODE_DP4
;
385 fullinst
->Instruction
.Opcode
= TGSI_OPCODE_DPH
;
388 fullinst
->Instruction
.Opcode
= TGSI_OPCODE_DST
;
391 fullinst
->Instruction
.Opcode
= TGSI_OPCODE_ELSE
;
392 fullinst
->InstructionExtLabel
.Label
= inst
->BranchTarget
+ preamble_size
;
395 fullinst
->Instruction
.Opcode
= TGSI_OPCODE_ENDIF
;
398 fullinst
->Instruction
.Opcode
= TGSI_OPCODE_ENDLOOP2
;
399 fullinst
->InstructionExtLabel
.Label
= inst
->BranchTarget
+ preamble_size
;
402 fullinst
->Instruction
.Opcode
= TGSI_OPCODE_ENDSUB
;
403 *insideSubroutine
= GL_FALSE
;
406 fullinst
->Instruction
.Opcode
= TGSI_OPCODE_EX2
;
409 fullinst
->Instruction
.Opcode
= TGSI_OPCODE_EXP
;
412 fullinst
->Instruction
.Opcode
= TGSI_OPCODE_FLR
;
415 fullinst
->Instruction
.Opcode
= TGSI_OPCODE_FRC
;
418 fullinst
->Instruction
.Opcode
= TGSI_OPCODE_IF
;
419 fullinst
->InstructionExtLabel
.Label
= inst
->BranchTarget
+ preamble_size
;
422 fullinst
->Instruction
.Opcode
= TGSI_OPCODE_TRUNC
;
426 fullinst
->Instruction
.Opcode
= TGSI_OPCODE_KIL
;
430 assert(inst
->DstReg
.CondMask
== COND_TR
);
431 fullinst
->Instruction
.Opcode
= TGSI_OPCODE_KILP
;
434 fullinst
->Instruction
.Opcode
= TGSI_OPCODE_LG2
;
437 fullinst
->Instruction
.Opcode
= TGSI_OPCODE_LOG
;
440 fullinst
->Instruction
.Opcode
= TGSI_OPCODE_LIT
;
443 fullinst
->Instruction
.Opcode
= TGSI_OPCODE_LRP
;
446 fullinst
->Instruction
.Opcode
= TGSI_OPCODE_MAD
;
449 fullinst
->Instruction
.Opcode
= TGSI_OPCODE_MAX
;
452 fullinst
->Instruction
.Opcode
= TGSI_OPCODE_MIN
;
455 fullinst
->Instruction
.Opcode
= TGSI_OPCODE_MOV
;
458 fullinst
->Instruction
.Opcode
= TGSI_OPCODE_MUL
;
461 fullinst
->Instruction
.Opcode
= TGSI_OPCODE_NOISE1
;
464 fullinst
->Instruction
.Opcode
= TGSI_OPCODE_NOISE2
;
467 fullinst
->Instruction
.Opcode
= TGSI_OPCODE_NOISE3
;
470 fullinst
->Instruction
.Opcode
= TGSI_OPCODE_NOISE4
;
473 fullinst
->Instruction
.Opcode
= TGSI_OPCODE_NOP
;
476 fullinst
->Instruction
.Opcode
= TGSI_OPCODE_NRM
;
479 fullinst
->Instruction
.Opcode
= TGSI_OPCODE_NRM4
;
482 fullinst
->Instruction
.Opcode
= TGSI_OPCODE_POW
;
485 fullinst
->Instruction
.Opcode
= TGSI_OPCODE_RCP
;
488 /* If RET is used inside main (not a real subroutine) we may want
489 * to execute END instead of RET. TBD...
491 if (1 /* *insideSubroutine */) {
492 fullinst
->Instruction
.Opcode
= TGSI_OPCODE_RET
;
495 /* inside main() pseudo-function */
496 fullinst
->Instruction
.Opcode
= TGSI_OPCODE_END
;
500 fullinst
->Instruction
.Opcode
= TGSI_OPCODE_RSQ
;
503 fullinst
->Instruction
.Opcode
= TGSI_OPCODE_SCS
;
504 fulldst
->DstRegister
.WriteMask
&= TGSI_WRITEMASK_XY
;
507 fullinst
->Instruction
.Opcode
= TGSI_OPCODE_SEQ
;
510 fullinst
->Instruction
.Opcode
= TGSI_OPCODE_SGE
;
513 fullinst
->Instruction
.Opcode
= TGSI_OPCODE_SGT
;
516 fullinst
->Instruction
.Opcode
= TGSI_OPCODE_SIN
;
519 fullinst
->Instruction
.Opcode
= TGSI_OPCODE_SLE
;
522 fullinst
->Instruction
.Opcode
= TGSI_OPCODE_SLT
;
525 fullinst
->Instruction
.Opcode
= TGSI_OPCODE_SNE
;
528 fullinst
->Instruction
.Opcode
= TGSI_OPCODE_SSG
;
531 fullinst
->Instruction
.Opcode
= TGSI_OPCODE_SUB
;
534 fullinst
->Instruction
.Opcode
= TGSI_OPCODE_SWZ
;
537 /* ordinary texture lookup */
538 fullinst
->Instruction
.Opcode
= TGSI_OPCODE_TEX
;
539 fullinst
->Instruction
.NumSrcRegs
= 2;
540 fullinst
->InstructionExtTexture
.Texture
=
541 map_texture_target( inst
->TexSrcTarget
, inst
->TexShadow
);
542 fullinst
->FullSrcRegisters
[1].SrcRegister
.File
= TGSI_FILE_SAMPLER
;
543 fullinst
->FullSrcRegisters
[1].SrcRegister
.Index
= inst
->TexSrcUnit
;
546 /* texture lookup with LOD bias */
547 fullinst
->Instruction
.Opcode
= TGSI_OPCODE_TXB
;
548 fullinst
->Instruction
.NumSrcRegs
= 2;
549 fullinst
->InstructionExtTexture
.Texture
=
550 map_texture_target( inst
->TexSrcTarget
, inst
->TexShadow
);
551 fullinst
->FullSrcRegisters
[1].SrcRegister
.File
= TGSI_FILE_SAMPLER
;
552 fullinst
->FullSrcRegisters
[1].SrcRegister
.Index
= inst
->TexSrcUnit
;
555 /* texture lookup with explicit partial derivatives */
556 fullinst
->Instruction
.Opcode
= TGSI_OPCODE_TXD
;
557 fullinst
->Instruction
.NumSrcRegs
= 4;
558 fullinst
->InstructionExtTexture
.Texture
=
559 map_texture_target( inst
->TexSrcTarget
, inst
->TexShadow
);
560 /* src[0] = coord, src[1] = d[strq]/dx, src[2] = d[strq]/dy */
561 fullinst
->FullSrcRegisters
[3].SrcRegister
.File
= TGSI_FILE_SAMPLER
;
562 fullinst
->FullSrcRegisters
[3].SrcRegister
.Index
= inst
->TexSrcUnit
;
565 /* texture lookup with explicit LOD */
566 fullinst
->Instruction
.Opcode
= TGSI_OPCODE_TXL
;
567 fullinst
->Instruction
.NumSrcRegs
= 2;
568 fullinst
->InstructionExtTexture
.Texture
=
569 map_texture_target( inst
->TexSrcTarget
, inst
->TexShadow
);
570 fullinst
->FullSrcRegisters
[1].SrcRegister
.File
= TGSI_FILE_SAMPLER
;
571 fullinst
->FullSrcRegisters
[1].SrcRegister
.Index
= inst
->TexSrcUnit
;
574 /* texture lookup with divide by Q component */
575 /* convert to TEX w/ special flag for division */
576 fullinst
->Instruction
.Opcode
= TGSI_OPCODE_TXP
;
577 fullinst
->Instruction
.NumSrcRegs
= 2;
578 fullinst
->InstructionExtTexture
.Texture
=
579 map_texture_target( inst
->TexSrcTarget
, inst
->TexShadow
);
580 fullinst
->FullSrcRegisters
[1].SrcRegister
.File
= TGSI_FILE_SAMPLER
;
581 fullinst
->FullSrcRegisters
[1].SrcRegister
.Index
= inst
->TexSrcUnit
;
584 fullinst
->Instruction
.Opcode
= TGSI_OPCODE_XPD
;
585 fulldst
->DstRegister
.WriteMask
&= TGSI_WRITEMASK_XYZ
;
588 fullinst
->Instruction
.Opcode
= TGSI_OPCODE_END
;
596 * \param usage_mask bitfield of TGSI_WRITEMASK_{XYZW} tokens
598 static struct tgsi_full_declaration
601 GLboolean interpolate_info
,
604 GLboolean semantic_info
,
605 GLuint semantic_name
,
606 GLbitfield semantic_index
,
607 GLbitfield input_flags
)
609 struct tgsi_full_declaration decl
;
611 assert(semantic_name
< TGSI_SEMANTIC_COUNT
);
613 decl
= tgsi_default_full_declaration();
614 decl
.Declaration
.File
= TGSI_FILE_INPUT
;
615 decl
.Declaration
.UsageMask
= usage_mask
;
616 decl
.Declaration
.Semantic
= semantic_info
;
617 decl
.DeclarationRange
.First
= index
;
618 decl
.DeclarationRange
.Last
= index
;
620 decl
.Semantic
.SemanticName
= semantic_name
;
621 decl
.Semantic
.SemanticIndex
= semantic_index
;
623 if (interpolate_info
) {
624 decl
.Declaration
.Interpolate
= interpolate
;
626 if (input_flags
& PROG_PARAM_BIT_CENTROID
)
627 decl
.Declaration
.Centroid
= 1;
628 if (input_flags
& PROG_PARAM_BIT_INVARIANT
)
629 decl
.Declaration
.Invariant
= 1;
635 * \param usage_mask bitfield of TGSI_WRITEMASK_{XYZW} tokens
637 static struct tgsi_full_declaration
640 GLuint semantic_name
,
641 GLuint semantic_index
,
643 GLbitfield output_flags
)
645 struct tgsi_full_declaration decl
;
647 assert(semantic_name
< TGSI_SEMANTIC_COUNT
);
649 decl
= tgsi_default_full_declaration();
650 decl
.Declaration
.File
= TGSI_FILE_OUTPUT
;
651 decl
.Declaration
.UsageMask
= usage_mask
;
652 decl
.Declaration
.Semantic
= 1;
653 decl
.DeclarationRange
.First
= index
;
654 decl
.DeclarationRange
.Last
= index
;
655 decl
.Semantic
.SemanticName
= semantic_name
;
656 decl
.Semantic
.SemanticIndex
= semantic_index
;
657 if (output_flags
& PROG_PARAM_BIT_CENTROID
)
658 decl
.Declaration
.Centroid
= 1;
659 if (output_flags
& PROG_PARAM_BIT_INVARIANT
)
660 decl
.Declaration
.Invariant
= 1;
666 static struct tgsi_full_declaration
671 struct tgsi_full_declaration decl
;
672 decl
= tgsi_default_full_declaration();
673 decl
.Declaration
.File
= TGSI_FILE_TEMPORARY
;
674 decl
.DeclarationRange
.First
= start_index
;
675 decl
.DeclarationRange
.Last
= end_index
;
679 static struct tgsi_full_declaration
684 struct tgsi_full_declaration decl
;
686 decl
= tgsi_default_full_declaration();
687 decl
.Declaration
.File
= TGSI_FILE_ADDRESS
;
688 decl
.DeclarationRange
.First
= start_index
;
689 decl
.DeclarationRange
.Last
= end_index
;
693 static struct tgsi_full_declaration
694 make_sampler_decl(GLuint index
)
696 struct tgsi_full_declaration decl
;
697 decl
= tgsi_default_full_declaration();
698 decl
.Declaration
.File
= TGSI_FILE_SAMPLER
;
699 decl
.DeclarationRange
.First
= index
;
700 decl
.DeclarationRange
.Last
= index
;
704 /** Reference into a constant buffer */
705 static struct tgsi_full_declaration
706 make_constant_decl(GLuint first
, GLuint last
)
708 struct tgsi_full_declaration decl
;
709 decl
= tgsi_default_full_declaration();
710 decl
.Declaration
.File
= TGSI_FILE_CONSTANT
;
711 decl
.DeclarationRange
.First
= first
;
712 decl
.DeclarationRange
.Last
= last
;
719 * Find the temporaries which are used in the given program.
722 find_temporaries(const struct gl_program
*program
,
723 GLboolean tempsUsed
[MAX_PROGRAM_TEMPS
])
727 for (i
= 0; i
< MAX_PROGRAM_TEMPS
; i
++)
728 tempsUsed
[i
] = GL_FALSE
;
730 for (i
= 0; i
< program
->NumInstructions
; i
++) {
731 const struct prog_instruction
*inst
= program
->Instructions
+ i
;
732 const GLuint n
= _mesa_num_inst_src_regs( inst
->Opcode
);
733 for (j
= 0; j
< n
; j
++) {
734 if (inst
->SrcReg
[j
].File
== PROGRAM_TEMPORARY
)
735 tempsUsed
[inst
->SrcReg
[j
].Index
] = GL_TRUE
;
736 if (inst
->DstReg
.File
== PROGRAM_TEMPORARY
)
737 tempsUsed
[inst
->DstReg
.Index
] = GL_TRUE
;
744 * Find an unused temporary in the tempsUsed array.
747 find_free_temporary(GLboolean tempsUsed
[MAX_PROGRAM_TEMPS
])
750 for (i
= 0; i
< MAX_PROGRAM_TEMPS
; i
++) {
752 tempsUsed
[i
] = GL_TRUE
;
760 /** helper for building simple TGSI instruction, one src register */
762 build_tgsi_instruction1(struct tgsi_full_instruction
*inst
,
764 int dstFile
, int dstIndex
, int writemask
,
765 int srcFile1
, int srcIndex1
)
767 *inst
= tgsi_default_full_instruction();
769 inst
->Instruction
.Opcode
= opcode
;
771 inst
->Instruction
.NumDstRegs
= 1;
772 inst
->FullDstRegisters
[0].DstRegister
.File
= dstFile
;
773 inst
->FullDstRegisters
[0].DstRegister
.Index
= dstIndex
;
774 inst
->FullDstRegisters
[0].DstRegister
.WriteMask
= writemask
;
776 inst
->Instruction
.NumSrcRegs
= 1;
777 inst
->FullSrcRegisters
[0].SrcRegister
.File
= srcFile1
;
778 inst
->FullSrcRegisters
[0].SrcRegister
.Index
= srcIndex1
;
782 /** helper for building simple TGSI instruction, two src registers */
784 build_tgsi_instruction2(struct tgsi_full_instruction
*inst
,
786 int dstFile
, int dstIndex
, int writemask
,
787 int srcFile1
, int srcIndex1
,
788 int srcFile2
, int srcIndex2
)
790 *inst
= tgsi_default_full_instruction();
792 inst
->Instruction
.Opcode
= opcode
;
794 inst
->Instruction
.NumDstRegs
= 1;
795 inst
->FullDstRegisters
[0].DstRegister
.File
= dstFile
;
796 inst
->FullDstRegisters
[0].DstRegister
.Index
= dstIndex
;
797 inst
->FullDstRegisters
[0].DstRegister
.WriteMask
= writemask
;
799 inst
->Instruction
.NumSrcRegs
= 2;
800 inst
->FullSrcRegisters
[0].SrcRegister
.File
= srcFile1
;
801 inst
->FullSrcRegisters
[0].SrcRegister
.Index
= srcIndex1
;
802 inst
->FullSrcRegisters
[1].SrcRegister
.File
= srcFile2
;
803 inst
->FullSrcRegisters
[1].SrcRegister
.Index
= srcIndex2
;
809 * Emit the TGSI instructions for inverting the WPOS y coordinate.
812 emit_inverted_wpos(struct tgsi_token
*tokens
,
816 struct tgsi_header
*header
, int maxTokens
)
818 struct tgsi_full_instruction fullinst
;
821 /* MOV wpos_temp.xzw, input[wpos]; */
822 build_tgsi_instruction1(&fullinst
,
824 TGSI_FILE_TEMPORARY
, wpos_temp
, WRITEMASK_XZW
,
827 ti
+= tgsi_build_full_instruction(&fullinst
,
832 /* SUB wpos_temp.y, const[winsize_const] - input[wpos_input]; */
833 build_tgsi_instruction2(&fullinst
,
835 TGSI_FILE_TEMPORARY
, wpos_temp
, WRITEMASK_Y
,
836 TGSI_FILE_CONSTANT
, winsize_const
,
837 TGSI_FILE_INPUT
, wpos_input
);
839 ti
+= tgsi_build_full_instruction(&fullinst
,
851 * Translate Mesa program to TGSI format.
852 * \param program the program to translate
853 * \param numInputs number of input registers used
854 * \param inputMapping maps Mesa fragment program inputs to TGSI generic
856 * \param inputSemanticName the TGSI_SEMANTIC flag for each input
857 * \param inputSemanticIndex the semantic index (ex: which texcoord) for each input
858 * \param interpMode the TGSI_INTERPOLATE_LINEAR/PERSP mode for each input
860 * \param numOutputs number of output registers used
861 * \param outputMapping maps Mesa fragment program outputs to TGSI
863 * \param outputSemanticName the TGSI_SEMANTIC flag for each output
864 * \param outputSemanticIndex the semantic index (ex: which texcoord) for each output
865 * \param tokens array to store translated tokens in
866 * \param maxTokens size of the tokens array
868 * \return number of tokens placed in 'tokens' buffer, or zero if error
871 st_translate_mesa_program(
874 const struct gl_program
*program
,
876 const GLuint inputMapping
[],
877 const ubyte inputSemanticName
[],
878 const ubyte inputSemanticIndex
[],
879 const GLuint interpMode
[],
880 const GLbitfield inputFlags
[],
882 const GLuint outputMapping
[],
883 const ubyte outputSemanticName
[],
884 const ubyte outputSemanticIndex
[],
885 const GLbitfield outputFlags
[],
886 struct tgsi_token
*tokens
,
890 GLuint ti
; /* token index */
891 struct tgsi_header
*header
;
892 struct tgsi_processor
*processor
;
893 GLuint preamble_size
= 0;
894 GLuint immediates
[1000];
895 GLuint numImmediates
= 0;
896 GLboolean insideSubroutine
= GL_FALSE
;
897 GLboolean indirectAccess
= GL_FALSE
;
898 GLboolean tempsUsed
[MAX_PROGRAM_TEMPS
+ 1];
899 GLint wposTemp
= -1, winHeightConst
= -1;
901 assert(procType
== TGSI_PROCESSOR_FRAGMENT
||
902 procType
== TGSI_PROCESSOR_VERTEX
);
904 find_temporaries(program
, tempsUsed
);
906 if (procType
== TGSI_PROCESSOR_FRAGMENT
) {
907 if (program
->InputsRead
& FRAG_BIT_WPOS
) {
908 /* Fragment program uses fragment position input.
909 * Need to replace instances of INPUT[WPOS] with temp T
910 * where T = INPUT[WPOS] by y is inverted.
912 static const gl_state_index winSizeState
[STATE_LENGTH
]
913 = { STATE_INTERNAL
, STATE_FB_SIZE
, 0, 0, 0 };
914 winHeightConst
= _mesa_add_state_reference(program
->Parameters
,
916 wposTemp
= find_free_temporary(tempsUsed
);
921 *(struct tgsi_version
*) &tokens
[0] = tgsi_build_version();
923 header
= (struct tgsi_header
*) &tokens
[1];
924 *header
= tgsi_build_header();
926 processor
= (struct tgsi_processor
*) &tokens
[2];
927 *processor
= tgsi_build_processor( procType
, header
);
932 * Declare input attributes.
934 if (procType
== TGSI_PROCESSOR_FRAGMENT
) {
935 for (i
= 0; i
< numInputs
; i
++) {
936 struct tgsi_full_declaration fulldecl
;
937 fulldecl
= make_input_decl(i
,
938 GL_TRUE
, interpMode
[i
],
940 GL_TRUE
, inputSemanticName
[i
],
941 inputSemanticIndex
[i
],
943 ti
+= tgsi_build_full_declaration(&fulldecl
,
951 /* XXX: this could probaby be merged with the clause above.
952 * the only difference is the semantic tags.
954 for (i
= 0; i
< numInputs
; i
++) {
955 struct tgsi_full_declaration fulldecl
;
956 fulldecl
= make_input_decl(i
,
961 ti
+= tgsi_build_full_declaration(&fulldecl
,
969 * Declare output attributes.
971 if (procType
== TGSI_PROCESSOR_FRAGMENT
) {
972 for (i
= 0; i
< numOutputs
; i
++) {
973 struct tgsi_full_declaration fulldecl
;
974 switch (outputSemanticName
[i
]) {
975 case TGSI_SEMANTIC_POSITION
:
976 fulldecl
= make_output_decl(i
,
977 TGSI_SEMANTIC_POSITION
, /* Z / Depth */
978 outputSemanticIndex
[i
],
982 case TGSI_SEMANTIC_COLOR
:
983 fulldecl
= make_output_decl(i
,
985 outputSemanticIndex
[i
],
993 ti
+= tgsi_build_full_declaration(&fulldecl
,
1001 for (i
= 0; i
< numOutputs
; i
++) {
1002 struct tgsi_full_declaration fulldecl
;
1003 fulldecl
= make_output_decl(i
,
1004 outputSemanticName
[i
],
1005 outputSemanticIndex
[i
],
1006 TGSI_WRITEMASK_XYZW
,
1008 ti
+= tgsi_build_full_declaration(&fulldecl
,
1015 /* temporary decls */
1017 GLboolean inside_range
= GL_FALSE
;
1018 GLuint start_range
= 0;
1020 tempsUsed
[MAX_PROGRAM_TEMPS
] = GL_FALSE
;
1021 for (i
= 0; i
< MAX_PROGRAM_TEMPS
+ 1; i
++) {
1022 if (tempsUsed
[i
] && !inside_range
) {
1023 inside_range
= GL_TRUE
;
1026 else if (!tempsUsed
[i
] && inside_range
) {
1027 struct tgsi_full_declaration fulldecl
;
1029 inside_range
= GL_FALSE
;
1030 fulldecl
= make_temp_decl( start_range
, i
- 1 );
1031 ti
+= tgsi_build_full_declaration(
1040 /* Declare address register.
1042 if (program
->NumAddressRegs
> 0) {
1043 struct tgsi_full_declaration fulldecl
;
1045 assert( program
->NumAddressRegs
== 1 );
1047 fulldecl
= make_addr_decl( 0, 0 );
1048 ti
+= tgsi_build_full_declaration(
1054 indirectAccess
= GL_TRUE
;
1057 /* immediates/literals */
1058 memset(immediates
, ~0, sizeof(immediates
));
1060 /* Emit immediates only when there is no address register in use.
1061 * FIXME: Be smarter and recognize param arrays -- indirect addressing is
1062 * only valid within the referenced array.
1064 if (program
->Parameters
&& !indirectAccess
) {
1065 for (i
= 0; i
< program
->Parameters
->NumParameters
; i
++) {
1066 if (program
->Parameters
->Parameters
[i
].Type
== PROGRAM_CONSTANT
) {
1067 struct tgsi_full_immediate fullimm
;
1069 fullimm
= make_immediate( program
->Parameters
->ParameterValues
[i
], 4 );
1070 ti
+= tgsi_build_full_immediate(
1075 immediates
[i
] = numImmediates
;
1081 /* constant buffer refs */
1082 if (program
->Parameters
) {
1083 GLint start
= -1, end
= -1;
1085 for (i
= 0; i
< program
->Parameters
->NumParameters
; i
++) {
1086 GLboolean emit
= (i
== program
->Parameters
->NumParameters
- 1);
1089 switch (program
->Parameters
->Parameters
[i
].Type
) {
1090 case PROGRAM_ENV_PARAM
:
1091 case PROGRAM_STATE_VAR
:
1092 case PROGRAM_NAMED_PARAM
:
1093 case PROGRAM_UNIFORM
:
1096 case PROGRAM_CONSTANT
:
1097 matches
= indirectAccess
;
1105 /* begin a sequence */
1110 /* continue sequence */
1116 /* end of sequence */
1121 if (emit
&& start
>= 0) {
1122 struct tgsi_full_declaration fulldecl
;
1124 fulldecl
= make_constant_decl( start
, end
);
1125 ti
+= tgsi_build_full_declaration(
1135 /* texture samplers */
1136 for (i
= 0; i
< ctx
->Const
.MaxTextureImageUnits
; i
++) {
1137 if (program
->SamplersUsed
& (1 << i
)) {
1138 struct tgsi_full_declaration fulldecl
;
1140 fulldecl
= make_sampler_decl( i
);
1141 ti
+= tgsi_build_full_declaration(
1149 /* invert WPOS fragment input */
1150 if (wposTemp
>= 0) {
1151 ti
+= emit_inverted_wpos(&tokens
[ti
], wposTemp
, winHeightConst
,
1152 inputMapping
[FRAG_ATTRIB_WPOS
],
1153 header
, maxTokens
- ti
);
1154 preamble_size
= 2; /* two instructions added */
1157 for (i
= 0; i
< program
->NumInstructions
; i
++) {
1158 struct tgsi_full_instruction fullinst
;
1160 compile_instruction(
1161 &program
->Instructions
[i
],
1172 ti
+= tgsi_build_full_instruction(
1180 if(!tgsi_sanity_check(tokens
)) {
1181 debug_printf("Due to sanity check failure(s) above the following shader program is invalid:\n");
1182 debug_printf("\nOriginal program:\n%s", program
->String
);
1183 debug_printf("\nMesa program:\n");
1184 _mesa_print_program(program
);
1185 debug_printf("\nTGSI program:\n");
1186 tgsi_dump(tokens
, 0);