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 "pipe/p_debug.h"
47 * Map mesa register file to TGSI register file.
51 enum 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 return TGSI_FILE_IMMEDIATE
;
82 return TGSI_FILE_INPUT
;
84 return TGSI_FILE_OUTPUT
;
86 return TGSI_FILE_ADDRESS
;
89 return TGSI_FILE_NULL
;
94 * Map mesa register file index to TGSI index.
95 * Take special care when processing input and output indices.
96 * \param file one of TGSI_FILE_x
97 * \param index the mesa register file index
98 * \param inputMapping maps Mesa input indexes to TGSI input indexes
99 * \param outputMapping maps Mesa output indexes to TGSI output indexes
102 map_register_file_index(
105 const GLuint inputMapping
[],
106 const GLuint outputMapping
[],
107 const GLuint immediateMapping
[],
108 GLboolean indirectAccess
)
111 case TGSI_FILE_INPUT
:
112 /* inputs are mapped according to the user-defined map */
113 return inputMapping
[index
];
115 case TGSI_FILE_OUTPUT
:
116 return outputMapping
[index
];
118 case TGSI_FILE_IMMEDIATE
:
121 return immediateMapping
[index
];
129 * Map mesa texture target to TGSI texture target.
135 switch( textarget
) {
136 case TEXTURE_1D_INDEX
:
137 return TGSI_TEXTURE_1D
;
138 case TEXTURE_2D_INDEX
:
139 return TGSI_TEXTURE_2D
;
140 case TEXTURE_3D_INDEX
:
141 return TGSI_TEXTURE_3D
;
142 case TEXTURE_CUBE_INDEX
:
143 return TGSI_TEXTURE_CUBE
;
144 case TEXTURE_RECT_INDEX
:
145 return TGSI_TEXTURE_RECT
;
150 return TGSI_TEXTURE_1D
;
159 return TGSI_SAT_NONE
;
160 case SATURATE_ZERO_ONE
:
161 return TGSI_SAT_ZERO_ONE
;
162 case SATURATE_PLUS_MINUS_ONE
:
163 return TGSI_SAT_MINUS_PLUS_ONE
;
166 return TGSI_SAT_NONE
;
174 assert( WRITEMASK_X
== TGSI_WRITEMASK_X
);
175 assert( WRITEMASK_Y
== TGSI_WRITEMASK_Y
);
176 assert( WRITEMASK_Z
== TGSI_WRITEMASK_Z
);
177 assert( WRITEMASK_W
== TGSI_WRITEMASK_W
);
178 assert( (writemask
& ~TGSI_WRITEMASK_XYZW
) == 0 );
183 static struct tgsi_full_immediate
184 make_immediate(const float *value
, uint size
)
186 struct tgsi_full_immediate imm
;
188 imm
= tgsi_default_full_immediate();
189 imm
.Immediate
.Size
+= size
;
190 imm
.Immediate
.DataType
= TGSI_IMM_FLOAT32
;
191 imm
.u
.Pointer
= value
;
197 const struct prog_instruction
*inst
,
198 struct tgsi_full_instruction
*fullinst
,
199 const GLuint inputMapping
[],
200 const GLuint outputMapping
[],
201 const GLuint immediateMapping
[],
202 GLboolean indirectAccess
,
203 GLuint preamble_size
,
205 GLboolean
*insideSubroutine
)
208 struct tgsi_full_dst_register
*fulldst
;
209 struct tgsi_full_src_register
*fullsrc
;
211 *fullinst
= tgsi_default_full_instruction();
213 fullinst
->Instruction
.Saturate
= convert_sat( inst
->SaturateMode
);
214 fullinst
->Instruction
.NumDstRegs
= _mesa_num_inst_dst_regs( inst
->Opcode
);
215 fullinst
->Instruction
.NumSrcRegs
= _mesa_num_inst_src_regs( inst
->Opcode
);
217 fulldst
= &fullinst
->FullDstRegisters
[0];
218 fulldst
->DstRegister
.File
= map_register_file( inst
->DstReg
.File
, 0, NULL
, GL_FALSE
);
219 fulldst
->DstRegister
.Index
= map_register_file_index(
220 fulldst
->DstRegister
.File
,
226 fulldst
->DstRegister
.WriteMask
= convert_writemask( inst
->DstReg
.WriteMask
);
228 for (i
= 0; i
< fullinst
->Instruction
.NumSrcRegs
; i
++) {
231 fullsrc
= &fullinst
->FullSrcRegisters
[i
];
232 fullsrc
->SrcRegister
.File
= map_register_file(
233 inst
->SrcReg
[i
].File
,
234 inst
->SrcReg
[i
].Index
,
237 fullsrc
->SrcRegister
.Index
= map_register_file_index(
238 fullsrc
->SrcRegister
.File
,
239 inst
->SrcReg
[i
].Index
,
246 * This not at all the correct solution.
247 * FIXME: Roll this up in the above map functions
249 if (fullsrc
->SrcRegister
.File
== TGSI_FILE_IMMEDIATE
&& fullsrc
->SrcRegister
.Index
== ~0) {
250 fullsrc
->SrcRegister
.File
= TGSI_FILE_CONSTANT
;
251 fullsrc
->SrcRegister
.Index
= inst
->SrcReg
[i
].Index
;
254 /* swizzle (ext swizzle also depends on negation) */
257 GLboolean extended
= (inst
->SrcReg
[i
].NegateBase
!= NEGATE_NONE
&&
258 inst
->SrcReg
[i
].NegateBase
!= NEGATE_XYZW
);
259 for( j
= 0; j
< 4; j
++ ) {
260 swz
[j
] = GET_SWZ( inst
->SrcReg
[i
].Swizzle
, j
);
261 if (swz
[j
] > SWIZZLE_W
)
265 for (j
= 0; j
< 4; j
++) {
266 tgsi_util_set_src_register_extswizzle(&fullsrc
->SrcRegisterExtSwz
,
271 for (j
= 0; j
< 4; j
++) {
272 tgsi_util_set_src_register_swizzle(&fullsrc
->SrcRegister
,
278 if( inst
->SrcReg
[i
].NegateBase
== NEGATE_XYZW
) {
279 fullsrc
->SrcRegister
.Negate
= 1;
281 else if( inst
->SrcReg
[i
].NegateBase
!= NEGATE_NONE
) {
282 if( inst
->SrcReg
[i
].NegateBase
& NEGATE_X
) {
283 fullsrc
->SrcRegisterExtSwz
.NegateX
= 1;
285 if( inst
->SrcReg
[i
].NegateBase
& NEGATE_Y
) {
286 fullsrc
->SrcRegisterExtSwz
.NegateY
= 1;
288 if( inst
->SrcReg
[i
].NegateBase
& NEGATE_Z
) {
289 fullsrc
->SrcRegisterExtSwz
.NegateZ
= 1;
291 if( inst
->SrcReg
[i
].NegateBase
& NEGATE_W
) {
292 fullsrc
->SrcRegisterExtSwz
.NegateW
= 1;
296 if( inst
->SrcReg
[i
].Abs
) {
297 fullsrc
->SrcRegisterExtMod
.Absolute
= 1;
300 if( inst
->SrcReg
[i
].NegateAbs
) {
301 fullsrc
->SrcRegisterExtMod
.Negate
= 1;
304 if( inst
->SrcReg
[i
].RelAddr
) {
305 fullsrc
->SrcRegister
.Indirect
= 1;
307 fullsrc
->SrcRegisterInd
.File
= TGSI_FILE_ADDRESS
;
308 fullsrc
->SrcRegisterInd
.Index
= 0;
312 switch( inst
->Opcode
) {
314 fullinst
->Instruction
.Opcode
= TGSI_OPCODE_ARL
;
317 fullinst
->Instruction
.Opcode
= TGSI_OPCODE_ABS
;
320 fullinst
->Instruction
.Opcode
= TGSI_OPCODE_ADD
;
323 fullinst
->Instruction
.Opcode
= TGSI_OPCODE_BGNLOOP2
;
324 fullinst
->InstructionExtLabel
.Label
= inst
->BranchTarget
+ preamble_size
;
327 fullinst
->Instruction
.Opcode
= TGSI_OPCODE_BGNSUB
;
328 *insideSubroutine
= GL_TRUE
;
331 fullinst
->Instruction
.Opcode
= TGSI_OPCODE_BRA
;
334 fullinst
->Instruction
.Opcode
= TGSI_OPCODE_BRK
;
337 fullinst
->Instruction
.Opcode
= TGSI_OPCODE_CAL
;
338 fullinst
->InstructionExtLabel
.Label
= inst
->BranchTarget
+ preamble_size
;
341 fullinst
->Instruction
.Opcode
= TGSI_OPCODE_CMP
;
344 fullinst
->Instruction
.Opcode
= TGSI_OPCODE_CONT
;
347 fullinst
->Instruction
.Opcode
= TGSI_OPCODE_COS
;
350 fullinst
->Instruction
.Opcode
= TGSI_OPCODE_DDX
;
353 fullinst
->Instruction
.Opcode
= TGSI_OPCODE_DDY
;
356 fullinst
->Instruction
.Opcode
= TGSI_OPCODE_DP3
;
359 fullinst
->Instruction
.Opcode
= TGSI_OPCODE_DP4
;
362 fullinst
->Instruction
.Opcode
= TGSI_OPCODE_DPH
;
365 fullinst
->Instruction
.Opcode
= TGSI_OPCODE_DST
;
368 fullinst
->Instruction
.Opcode
= TGSI_OPCODE_ELSE
;
369 fullinst
->InstructionExtLabel
.Label
= inst
->BranchTarget
+ preamble_size
;
372 fullinst
->Instruction
.Opcode
= TGSI_OPCODE_ENDIF
;
375 fullinst
->Instruction
.Opcode
= TGSI_OPCODE_ENDLOOP2
;
376 fullinst
->InstructionExtLabel
.Label
= inst
->BranchTarget
+ preamble_size
;
379 fullinst
->Instruction
.Opcode
= TGSI_OPCODE_ENDSUB
;
380 *insideSubroutine
= GL_FALSE
;
383 fullinst
->Instruction
.Opcode
= TGSI_OPCODE_EX2
;
386 fullinst
->Instruction
.Opcode
= TGSI_OPCODE_EXP
;
389 fullinst
->Instruction
.Opcode
= TGSI_OPCODE_FLR
;
392 fullinst
->Instruction
.Opcode
= TGSI_OPCODE_FRC
;
395 fullinst
->Instruction
.Opcode
= TGSI_OPCODE_IF
;
396 fullinst
->InstructionExtLabel
.Label
= inst
->BranchTarget
+ preamble_size
;
399 fullinst
->Instruction
.Opcode
= TGSI_OPCODE_INT
;
403 fullinst
->Instruction
.Opcode
= TGSI_OPCODE_KIL
;
407 assert(inst
->DstReg
.CondMask
== COND_TR
);
408 fullinst
->Instruction
.Opcode
= TGSI_OPCODE_KILP
;
411 fullinst
->Instruction
.Opcode
= TGSI_OPCODE_LG2
;
414 fullinst
->Instruction
.Opcode
= TGSI_OPCODE_LOG
;
417 fullinst
->Instruction
.Opcode
= TGSI_OPCODE_LIT
;
420 fullinst
->Instruction
.Opcode
= TGSI_OPCODE_LRP
;
423 fullinst
->Instruction
.Opcode
= TGSI_OPCODE_MAD
;
426 fullinst
->Instruction
.Opcode
= TGSI_OPCODE_MAX
;
429 fullinst
->Instruction
.Opcode
= TGSI_OPCODE_MIN
;
432 fullinst
->Instruction
.Opcode
= TGSI_OPCODE_MOV
;
435 fullinst
->Instruction
.Opcode
= TGSI_OPCODE_MUL
;
438 fullinst
->Instruction
.Opcode
= TGSI_OPCODE_NOISE1
;
441 fullinst
->Instruction
.Opcode
= TGSI_OPCODE_NOISE2
;
444 fullinst
->Instruction
.Opcode
= TGSI_OPCODE_NOISE3
;
447 fullinst
->Instruction
.Opcode
= TGSI_OPCODE_NOISE4
;
450 fullinst
->Instruction
.Opcode
= TGSI_OPCODE_NOP
;
453 fullinst
->Instruction
.Opcode
= TGSI_OPCODE_POW
;
456 fullinst
->Instruction
.Opcode
= TGSI_OPCODE_RCP
;
459 /* If RET is used inside main (not a real subroutine) we may want
460 * to execute END instead of RET. TBD...
462 if (1 /* *insideSubroutine */) {
463 fullinst
->Instruction
.Opcode
= TGSI_OPCODE_RET
;
466 /* inside main() pseudo-function */
467 fullinst
->Instruction
.Opcode
= TGSI_OPCODE_END
;
471 fullinst
->Instruction
.Opcode
= TGSI_OPCODE_RSQ
;
472 tgsi_util_set_full_src_register_sign_mode(
473 &fullinst
->FullSrcRegisters
[0],
474 TGSI_UTIL_SIGN_CLEAR
);
477 fullinst
->Instruction
.Opcode
= TGSI_OPCODE_SCS
;
478 fulldst
->DstRegister
.WriteMask
&= TGSI_WRITEMASK_XY
;
481 fullinst
->Instruction
.Opcode
= TGSI_OPCODE_SEQ
;
484 fullinst
->Instruction
.Opcode
= TGSI_OPCODE_SGE
;
487 fullinst
->Instruction
.Opcode
= TGSI_OPCODE_SGT
;
490 fullinst
->Instruction
.Opcode
= TGSI_OPCODE_SIN
;
493 fullinst
->Instruction
.Opcode
= TGSI_OPCODE_SLE
;
496 fullinst
->Instruction
.Opcode
= TGSI_OPCODE_SLT
;
499 fullinst
->Instruction
.Opcode
= TGSI_OPCODE_SNE
;
502 fullinst
->Instruction
.Opcode
= TGSI_OPCODE_SUB
;
505 fullinst
->Instruction
.Opcode
= TGSI_OPCODE_SWZ
;
508 /* ordinary texture lookup */
509 fullinst
->Instruction
.Opcode
= TGSI_OPCODE_TEX
;
510 fullinst
->Instruction
.NumSrcRegs
= 2;
511 fullinst
->InstructionExtTexture
.Texture
= map_texture_target( inst
->TexSrcTarget
);
512 fullinst
->FullSrcRegisters
[1].SrcRegister
.File
= TGSI_FILE_SAMPLER
;
513 fullinst
->FullSrcRegisters
[1].SrcRegister
.Index
= inst
->TexSrcUnit
;
516 /* texture lookup with LOD bias */
517 fullinst
->Instruction
.Opcode
= TGSI_OPCODE_TXB
;
518 fullinst
->Instruction
.NumSrcRegs
= 2;
519 fullinst
->InstructionExtTexture
.Texture
= map_texture_target( inst
->TexSrcTarget
);
520 fullinst
->FullSrcRegisters
[1].SrcRegister
.File
= TGSI_FILE_SAMPLER
;
521 fullinst
->FullSrcRegisters
[1].SrcRegister
.Index
= inst
->TexSrcUnit
;
524 /* texture lookup with explicit partial derivatives */
525 fullinst
->Instruction
.Opcode
= TGSI_OPCODE_TXD
;
526 fullinst
->Instruction
.NumSrcRegs
= 4;
527 fullinst
->InstructionExtTexture
.Texture
= map_texture_target( inst
->TexSrcTarget
);
528 /* src[0] = coord, src[1] = d[strq]/dx, src[2] = d[strq]/dy */
529 fullinst
->FullSrcRegisters
[3].SrcRegister
.File
= TGSI_FILE_SAMPLER
;
530 fullinst
->FullSrcRegisters
[3].SrcRegister
.Index
= inst
->TexSrcUnit
;
533 /* texture lookup with explicit LOD */
534 fullinst
->Instruction
.Opcode
= TGSI_OPCODE_TXL
;
535 fullinst
->Instruction
.NumSrcRegs
= 2;
536 fullinst
->InstructionExtTexture
.Texture
= map_texture_target( inst
->TexSrcTarget
);
537 fullinst
->FullSrcRegisters
[1].SrcRegister
.File
= TGSI_FILE_SAMPLER
;
538 fullinst
->FullSrcRegisters
[1].SrcRegister
.Index
= inst
->TexSrcUnit
;
541 /* texture lookup with divide by Q component */
542 /* convert to TEX w/ special flag for division */
543 fullinst
->Instruction
.Opcode
= TGSI_OPCODE_TXP
;
544 fullinst
->Instruction
.NumSrcRegs
= 2;
545 fullinst
->InstructionExtTexture
.Texture
= map_texture_target( inst
->TexSrcTarget
);
546 fullinst
->FullSrcRegisters
[1].SrcRegister
.File
= TGSI_FILE_SAMPLER
;
547 fullinst
->FullSrcRegisters
[1].SrcRegister
.Index
= inst
->TexSrcUnit
;
550 fullinst
->Instruction
.Opcode
= TGSI_OPCODE_XPD
;
551 fulldst
->DstRegister
.WriteMask
&= TGSI_WRITEMASK_XYZ
;
554 fullinst
->Instruction
.Opcode
= TGSI_OPCODE_END
;
562 * \param usage_mask bitfield of TGSI_WRITEMASK_{XYZW} tokens
564 static struct tgsi_full_declaration
567 GLboolean interpolate_info
,
570 GLboolean semantic_info
,
571 GLuint semantic_name
,
572 GLbitfield semantic_index
)
574 struct tgsi_full_declaration decl
;
576 assert(semantic_name
< TGSI_SEMANTIC_COUNT
);
578 decl
= tgsi_default_full_declaration();
579 decl
.Declaration
.File
= TGSI_FILE_INPUT
;
580 decl
.Declaration
.UsageMask
= usage_mask
;
581 decl
.Declaration
.Semantic
= semantic_info
;
582 decl
.DeclarationRange
.First
= index
;
583 decl
.DeclarationRange
.Last
= index
;
585 decl
.Semantic
.SemanticName
= semantic_name
;
586 decl
.Semantic
.SemanticIndex
= semantic_index
;
588 if (interpolate_info
) {
589 decl
.Declaration
.Interpolate
= interpolate
;
596 * \param usage_mask bitfield of TGSI_WRITEMASK_{XYZW} tokens
598 static struct tgsi_full_declaration
601 GLuint semantic_name
,
602 GLuint semantic_index
,
603 GLbitfield usage_mask
)
605 struct tgsi_full_declaration decl
;
607 assert(semantic_name
< TGSI_SEMANTIC_COUNT
);
609 decl
= tgsi_default_full_declaration();
610 decl
.Declaration
.File
= TGSI_FILE_OUTPUT
;
611 decl
.Declaration
.UsageMask
= usage_mask
;
612 decl
.Declaration
.Semantic
= 1;
613 decl
.DeclarationRange
.First
= index
;
614 decl
.DeclarationRange
.Last
= index
;
615 decl
.Semantic
.SemanticName
= semantic_name
;
616 decl
.Semantic
.SemanticIndex
= semantic_index
;
622 static struct tgsi_full_declaration
627 struct tgsi_full_declaration decl
;
628 decl
= tgsi_default_full_declaration();
629 decl
.Declaration
.File
= TGSI_FILE_TEMPORARY
;
630 decl
.DeclarationRange
.First
= start_index
;
631 decl
.DeclarationRange
.Last
= end_index
;
635 static struct tgsi_full_declaration
640 struct tgsi_full_declaration decl
;
642 decl
= tgsi_default_full_declaration();
643 decl
.Declaration
.File
= TGSI_FILE_ADDRESS
;
644 decl
.DeclarationRange
.First
= start_index
;
645 decl
.DeclarationRange
.Last
= end_index
;
649 static struct tgsi_full_declaration
650 make_sampler_decl(GLuint index
)
652 struct tgsi_full_declaration decl
;
653 decl
= tgsi_default_full_declaration();
654 decl
.Declaration
.File
= TGSI_FILE_SAMPLER
;
655 decl
.DeclarationRange
.First
= index
;
656 decl
.DeclarationRange
.Last
= index
;
660 /** Reference into a constant buffer */
661 static struct tgsi_full_declaration
662 make_constant_decl(GLuint first
, GLuint last
)
664 struct tgsi_full_declaration decl
;
665 decl
= tgsi_default_full_declaration();
666 decl
.Declaration
.File
= TGSI_FILE_CONSTANT
;
667 decl
.DeclarationRange
.First
= first
;
668 decl
.DeclarationRange
.Last
= last
;
675 * Find the temporaries which are used in the given program.
678 find_temporaries(const struct gl_program
*program
,
679 GLboolean tempsUsed
[MAX_PROGRAM_TEMPS
])
683 for (i
= 0; i
< MAX_PROGRAM_TEMPS
; i
++)
684 tempsUsed
[i
] = GL_FALSE
;
686 for (i
= 0; i
< program
->NumInstructions
; i
++) {
687 const struct prog_instruction
*inst
= program
->Instructions
+ i
;
688 const GLuint n
= _mesa_num_inst_src_regs( inst
->Opcode
);
689 for (j
= 0; j
< n
; j
++) {
690 if (inst
->SrcReg
[j
].File
== PROGRAM_TEMPORARY
)
691 tempsUsed
[inst
->SrcReg
[j
].Index
] = GL_TRUE
;
692 if (inst
->DstReg
.File
== PROGRAM_TEMPORARY
)
693 tempsUsed
[inst
->DstReg
.Index
] = GL_TRUE
;
702 * Translate Mesa program to TGSI format.
703 * \param program the program to translate
704 * \param numInputs number of input registers used
705 * \param inputMapping maps Mesa fragment program inputs to TGSI generic
707 * \param inputSemanticName the TGSI_SEMANTIC flag for each input
708 * \param inputSemanticIndex the semantic index (ex: which texcoord) for each input
709 * \param interpMode the TGSI_INTERPOLATE_LINEAR/PERSP mode for each input
711 * \param numOutputs number of output registers used
712 * \param outputMapping maps Mesa fragment program outputs to TGSI
714 * \param outputSemanticName the TGSI_SEMANTIC flag for each output
715 * \param outputSemanticIndex the semantic index (ex: which texcoord) for each output
716 * \param tokens array to store translated tokens in
717 * \param maxTokens size of the tokens array
719 * \return number of tokens placed in 'tokens' buffer, or zero if error
722 tgsi_translate_mesa_program(
724 const struct gl_program
*program
,
726 const GLuint inputMapping
[],
727 const ubyte inputSemanticName
[],
728 const ubyte inputSemanticIndex
[],
729 const GLuint interpMode
[],
731 const GLuint outputMapping
[],
732 const ubyte outputSemanticName
[],
733 const ubyte outputSemanticIndex
[],
734 struct tgsi_token
*tokens
,
738 GLuint ti
; /* token index */
739 struct tgsi_header
*header
;
740 struct tgsi_processor
*processor
;
741 struct tgsi_full_instruction fullinst
;
742 GLuint preamble_size
= 0;
743 GLuint immediates
[1000];
744 GLuint numImmediates
= 0;
745 GLboolean insideSubroutine
= GL_FALSE
;
746 GLboolean indirectAccess
= GL_FALSE
;
748 assert(procType
== TGSI_PROCESSOR_FRAGMENT
||
749 procType
== TGSI_PROCESSOR_VERTEX
);
751 *(struct tgsi_version
*) &tokens
[0] = tgsi_build_version();
753 header
= (struct tgsi_header
*) &tokens
[1];
754 *header
= tgsi_build_header();
756 processor
= (struct tgsi_processor
*) &tokens
[2];
757 *processor
= tgsi_build_processor( procType
, header
);
762 * Declare input attributes.
764 if (procType
== TGSI_PROCESSOR_FRAGMENT
) {
765 for (i
= 0; i
< numInputs
; i
++) {
766 struct tgsi_full_declaration fulldecl
;
767 fulldecl
= make_input_decl(i
,
768 GL_TRUE
, interpMode
[i
],
770 GL_TRUE
, inputSemanticName
[i
],
771 inputSemanticIndex
[i
]);
772 ti
+= tgsi_build_full_declaration(&fulldecl
,
780 /* XXX: this could probaby be merged with the clause above.
781 * the only difference is the semantic tags.
783 for (i
= 0; i
< numInputs
; i
++) {
784 struct tgsi_full_declaration fulldecl
;
785 fulldecl
= make_input_decl(i
,
789 ti
+= tgsi_build_full_declaration(&fulldecl
,
797 * Declare output attributes.
799 if (procType
== TGSI_PROCESSOR_FRAGMENT
) {
800 for (i
= 0; i
< numOutputs
; i
++) {
801 struct tgsi_full_declaration fulldecl
;
802 switch (outputSemanticName
[i
]) {
803 case TGSI_SEMANTIC_POSITION
:
804 fulldecl
= make_output_decl(i
,
805 TGSI_SEMANTIC_POSITION
, /* Z / Depth */
806 outputSemanticIndex
[i
],
809 case TGSI_SEMANTIC_COLOR
:
810 fulldecl
= make_output_decl(i
,
812 outputSemanticIndex
[i
],
813 TGSI_WRITEMASK_XYZW
);
819 ti
+= tgsi_build_full_declaration(&fulldecl
,
827 for (i
= 0; i
< numOutputs
; i
++) {
828 struct tgsi_full_declaration fulldecl
;
829 fulldecl
= make_output_decl(i
,
830 outputSemanticName
[i
],
831 outputSemanticIndex
[i
],
832 TGSI_WRITEMASK_XYZW
);
833 ti
+= tgsi_build_full_declaration(&fulldecl
,
840 /* temporary decls */
842 GLboolean tempsUsed
[MAX_PROGRAM_TEMPS
+ 1];
843 GLboolean inside_range
= GL_FALSE
;
844 GLuint start_range
= 0;
846 find_temporaries(program
, tempsUsed
);
847 tempsUsed
[MAX_PROGRAM_TEMPS
] = GL_FALSE
;
848 for (i
= 0; i
< MAX_PROGRAM_TEMPS
+ 1; i
++) {
849 if (tempsUsed
[i
] && !inside_range
) {
850 inside_range
= GL_TRUE
;
853 else if (!tempsUsed
[i
] && inside_range
) {
854 struct tgsi_full_declaration fulldecl
;
856 inside_range
= GL_FALSE
;
857 fulldecl
= make_temp_decl( start_range
, i
- 1 );
858 ti
+= tgsi_build_full_declaration(
867 /* Declare address register.
869 if (program
->NumAddressRegs
> 0) {
870 struct tgsi_full_declaration fulldecl
;
872 assert( program
->NumAddressRegs
== 1 );
874 fulldecl
= make_addr_decl( 0, 0 );
875 ti
+= tgsi_build_full_declaration(
881 indirectAccess
= GL_TRUE
;
884 /* immediates/literals */
885 memset(immediates
, ~0, sizeof(immediates
));
887 /* Emit immediates only when there is no address register in use.
888 * FIXME: Be smarter and recognize param arrays -- indirect addressing is
889 * only valid within the referenced array.
891 if (program
->Parameters
&& !indirectAccess
) {
892 for (i
= 0; i
< program
->Parameters
->NumParameters
; i
++) {
893 if (program
->Parameters
->Parameters
[i
].Type
== PROGRAM_CONSTANT
) {
894 struct tgsi_full_immediate fullimm
;
896 fullimm
= make_immediate( program
->Parameters
->ParameterValues
[i
], 4 );
897 ti
+= tgsi_build_full_immediate(
902 immediates
[i
] = numImmediates
;
908 /* constant buffer refs */
909 if (program
->Parameters
) {
910 GLint start
= -1, end
= -1;
912 for (i
= 0; i
< program
->Parameters
->NumParameters
; i
++) {
913 GLboolean emit
= (i
== program
->Parameters
->NumParameters
- 1);
916 switch (program
->Parameters
->Parameters
[i
].Type
) {
917 case PROGRAM_ENV_PARAM
:
918 case PROGRAM_STATE_VAR
:
919 case PROGRAM_NAMED_PARAM
:
920 case PROGRAM_UNIFORM
:
923 case PROGRAM_CONSTANT
:
924 matches
= indirectAccess
;
932 /* begin a sequence */
937 /* continue sequence */
943 /* end of sequence */
948 if (emit
&& start
>= 0) {
949 struct tgsi_full_declaration fulldecl
;
951 fulldecl
= make_constant_decl( start
, end
);
952 ti
+= tgsi_build_full_declaration(
962 /* texture samplers */
963 for (i
= 0; i
< 8; i
++) {
964 if (program
->SamplersUsed
& (1 << i
)) {
965 struct tgsi_full_declaration fulldecl
;
967 fulldecl
= make_sampler_decl( i
);
968 ti
+= tgsi_build_full_declaration(
976 for (i
= 0; i
< program
->NumInstructions
; i
++) {
978 &program
->Instructions
[i
],
988 ti
+= tgsi_build_full_instruction(
996 if(!tgsi_sanity_check(tokens
)) {
997 debug_printf("Due to sanity check failure(s) above the following shader program is invalid:\n");
998 debug_printf("\nOriginal program:\n%s", program
->String
);
999 debug_printf("\nMesa program:\n");
1000 _mesa_print_program(program
);
1001 debug_printf("\nTGSI program:\n");
1002 tgsi_dump(tokens
, 0);