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 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.
137 switch( textarget
) {
138 case TEXTURE_1D_INDEX
:
139 return TGSI_TEXTURE_1D
;
140 case TEXTURE_2D_INDEX
:
141 return TGSI_TEXTURE_2D
;
142 case TEXTURE_3D_INDEX
:
143 return TGSI_TEXTURE_3D
;
144 case TEXTURE_CUBE_INDEX
:
145 return TGSI_TEXTURE_CUBE
;
146 case TEXTURE_RECT_INDEX
:
147 return TGSI_TEXTURE_RECT
;
152 return TGSI_TEXTURE_1D
;
161 return TGSI_SAT_NONE
;
162 case SATURATE_ZERO_ONE
:
163 return TGSI_SAT_ZERO_ONE
;
164 case SATURATE_PLUS_MINUS_ONE
:
165 return TGSI_SAT_MINUS_PLUS_ONE
;
168 return TGSI_SAT_NONE
;
176 assert( WRITEMASK_X
== TGSI_WRITEMASK_X
);
177 assert( WRITEMASK_Y
== TGSI_WRITEMASK_Y
);
178 assert( WRITEMASK_Z
== TGSI_WRITEMASK_Z
);
179 assert( WRITEMASK_W
== TGSI_WRITEMASK_W
);
180 assert( (writemask
& ~TGSI_WRITEMASK_XYZW
) == 0 );
185 static struct tgsi_full_immediate
186 make_immediate(const float *value
, uint size
)
188 struct tgsi_full_immediate imm
;
190 imm
= tgsi_default_full_immediate();
191 imm
.Immediate
.Size
+= size
;
192 imm
.Immediate
.DataType
= TGSI_IMM_FLOAT32
;
193 imm
.u
.Pointer
= value
;
199 const struct prog_instruction
*inst
,
200 struct tgsi_full_instruction
*fullinst
,
201 const GLuint inputMapping
[],
202 const GLuint outputMapping
[],
203 const GLuint immediateMapping
[],
204 GLboolean indirectAccess
,
205 GLuint preamble_size
,
207 GLboolean
*insideSubroutine
)
210 struct tgsi_full_dst_register
*fulldst
;
211 struct tgsi_full_src_register
*fullsrc
;
213 *fullinst
= tgsi_default_full_instruction();
215 fullinst
->Instruction
.Saturate
= convert_sat( inst
->SaturateMode
);
216 fullinst
->Instruction
.NumDstRegs
= _mesa_num_inst_dst_regs( inst
->Opcode
);
217 fullinst
->Instruction
.NumSrcRegs
= _mesa_num_inst_src_regs( inst
->Opcode
);
219 fulldst
= &fullinst
->FullDstRegisters
[0];
220 fulldst
->DstRegister
.File
= map_register_file( inst
->DstReg
.File
, 0, NULL
, GL_FALSE
);
221 fulldst
->DstRegister
.Index
= map_register_file_index(
222 fulldst
->DstRegister
.File
,
228 fulldst
->DstRegister
.WriteMask
= convert_writemask( inst
->DstReg
.WriteMask
);
230 for (i
= 0; i
< fullinst
->Instruction
.NumSrcRegs
; i
++) {
233 fullsrc
= &fullinst
->FullSrcRegisters
[i
];
234 fullsrc
->SrcRegister
.File
= map_register_file(
235 inst
->SrcReg
[i
].File
,
236 inst
->SrcReg
[i
].Index
,
239 fullsrc
->SrcRegister
.Index
= map_register_file_index(
240 fullsrc
->SrcRegister
.File
,
241 inst
->SrcReg
[i
].Index
,
248 /* swizzle (ext swizzle also depends on negation) */
251 GLboolean extended
= (inst
->SrcReg
[i
].NegateBase
!= NEGATE_NONE
&&
252 inst
->SrcReg
[i
].NegateBase
!= NEGATE_XYZW
);
253 for( j
= 0; j
< 4; j
++ ) {
254 swz
[j
] = GET_SWZ( inst
->SrcReg
[i
].Swizzle
, j
);
255 if (swz
[j
] > SWIZZLE_W
)
259 for (j
= 0; j
< 4; j
++) {
260 tgsi_util_set_src_register_extswizzle(&fullsrc
->SrcRegisterExtSwz
,
265 for (j
= 0; j
< 4; j
++) {
266 tgsi_util_set_src_register_swizzle(&fullsrc
->SrcRegister
,
272 if( inst
->SrcReg
[i
].NegateBase
== NEGATE_XYZW
) {
273 fullsrc
->SrcRegister
.Negate
= 1;
275 else if( inst
->SrcReg
[i
].NegateBase
!= NEGATE_NONE
) {
276 if( inst
->SrcReg
[i
].NegateBase
& NEGATE_X
) {
277 fullsrc
->SrcRegisterExtSwz
.NegateX
= 1;
279 if( inst
->SrcReg
[i
].NegateBase
& NEGATE_Y
) {
280 fullsrc
->SrcRegisterExtSwz
.NegateY
= 1;
282 if( inst
->SrcReg
[i
].NegateBase
& NEGATE_Z
) {
283 fullsrc
->SrcRegisterExtSwz
.NegateZ
= 1;
285 if( inst
->SrcReg
[i
].NegateBase
& NEGATE_W
) {
286 fullsrc
->SrcRegisterExtSwz
.NegateW
= 1;
290 if( inst
->SrcReg
[i
].Abs
) {
291 fullsrc
->SrcRegisterExtMod
.Absolute
= 1;
294 if( inst
->SrcReg
[i
].NegateAbs
) {
295 fullsrc
->SrcRegisterExtMod
.Negate
= 1;
298 if( inst
->SrcReg
[i
].RelAddr
) {
299 fullsrc
->SrcRegister
.Indirect
= 1;
301 fullsrc
->SrcRegisterInd
.File
= TGSI_FILE_ADDRESS
;
302 fullsrc
->SrcRegisterInd
.Index
= 0;
306 switch( inst
->Opcode
) {
308 fullinst
->Instruction
.Opcode
= TGSI_OPCODE_ARL
;
311 fullinst
->Instruction
.Opcode
= TGSI_OPCODE_ABS
;
314 fullinst
->Instruction
.Opcode
= TGSI_OPCODE_ADD
;
317 fullinst
->Instruction
.Opcode
= TGSI_OPCODE_BGNLOOP2
;
318 fullinst
->InstructionExtLabel
.Label
= inst
->BranchTarget
+ preamble_size
;
321 fullinst
->Instruction
.Opcode
= TGSI_OPCODE_BGNSUB
;
322 *insideSubroutine
= GL_TRUE
;
325 fullinst
->Instruction
.Opcode
= TGSI_OPCODE_BRA
;
328 fullinst
->Instruction
.Opcode
= TGSI_OPCODE_BRK
;
331 fullinst
->Instruction
.Opcode
= TGSI_OPCODE_CAL
;
332 fullinst
->InstructionExtLabel
.Label
= inst
->BranchTarget
+ preamble_size
;
335 fullinst
->Instruction
.Opcode
= TGSI_OPCODE_CMP
;
338 fullinst
->Instruction
.Opcode
= TGSI_OPCODE_CONT
;
341 fullinst
->Instruction
.Opcode
= TGSI_OPCODE_COS
;
344 fullinst
->Instruction
.Opcode
= TGSI_OPCODE_DDX
;
347 fullinst
->Instruction
.Opcode
= TGSI_OPCODE_DDY
;
350 fullinst
->Instruction
.Opcode
= TGSI_OPCODE_DP2
;
353 fullinst
->Instruction
.Opcode
= TGSI_OPCODE_DP2A
;
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_TRUNC
;
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_NRM
;
456 fullinst
->Instruction
.Opcode
= TGSI_OPCODE_NRM4
;
459 fullinst
->Instruction
.Opcode
= TGSI_OPCODE_POW
;
462 fullinst
->Instruction
.Opcode
= TGSI_OPCODE_RCP
;
465 /* If RET is used inside main (not a real subroutine) we may want
466 * to execute END instead of RET. TBD...
468 if (1 /* *insideSubroutine */) {
469 fullinst
->Instruction
.Opcode
= TGSI_OPCODE_RET
;
472 /* inside main() pseudo-function */
473 fullinst
->Instruction
.Opcode
= TGSI_OPCODE_END
;
477 fullinst
->Instruction
.Opcode
= TGSI_OPCODE_RSQ
;
478 tgsi_util_set_full_src_register_sign_mode(
479 &fullinst
->FullSrcRegisters
[0],
480 TGSI_UTIL_SIGN_CLEAR
);
483 fullinst
->Instruction
.Opcode
= TGSI_OPCODE_SCS
;
484 fulldst
->DstRegister
.WriteMask
&= TGSI_WRITEMASK_XY
;
487 fullinst
->Instruction
.Opcode
= TGSI_OPCODE_SEQ
;
490 fullinst
->Instruction
.Opcode
= TGSI_OPCODE_SGE
;
493 fullinst
->Instruction
.Opcode
= TGSI_OPCODE_SGT
;
496 fullinst
->Instruction
.Opcode
= TGSI_OPCODE_SIN
;
499 fullinst
->Instruction
.Opcode
= TGSI_OPCODE_SLE
;
502 fullinst
->Instruction
.Opcode
= TGSI_OPCODE_SLT
;
505 fullinst
->Instruction
.Opcode
= TGSI_OPCODE_SNE
;
508 fullinst
->Instruction
.Opcode
= TGSI_OPCODE_SSG
;
511 fullinst
->Instruction
.Opcode
= TGSI_OPCODE_SUB
;
514 fullinst
->Instruction
.Opcode
= TGSI_OPCODE_SWZ
;
517 /* ordinary texture lookup */
518 fullinst
->Instruction
.Opcode
= TGSI_OPCODE_TEX
;
519 fullinst
->Instruction
.NumSrcRegs
= 2;
520 fullinst
->InstructionExtTexture
.Texture
= map_texture_target( inst
->TexSrcTarget
);
521 fullinst
->FullSrcRegisters
[1].SrcRegister
.File
= TGSI_FILE_SAMPLER
;
522 fullinst
->FullSrcRegisters
[1].SrcRegister
.Index
= inst
->TexSrcUnit
;
525 /* texture lookup with LOD bias */
526 fullinst
->Instruction
.Opcode
= TGSI_OPCODE_TXB
;
527 fullinst
->Instruction
.NumSrcRegs
= 2;
528 fullinst
->InstructionExtTexture
.Texture
= map_texture_target( inst
->TexSrcTarget
);
529 fullinst
->FullSrcRegisters
[1].SrcRegister
.File
= TGSI_FILE_SAMPLER
;
530 fullinst
->FullSrcRegisters
[1].SrcRegister
.Index
= inst
->TexSrcUnit
;
533 /* texture lookup with explicit partial derivatives */
534 fullinst
->Instruction
.Opcode
= TGSI_OPCODE_TXD
;
535 fullinst
->Instruction
.NumSrcRegs
= 4;
536 fullinst
->InstructionExtTexture
.Texture
= map_texture_target( inst
->TexSrcTarget
);
537 /* src[0] = coord, src[1] = d[strq]/dx, src[2] = d[strq]/dy */
538 fullinst
->FullSrcRegisters
[3].SrcRegister
.File
= TGSI_FILE_SAMPLER
;
539 fullinst
->FullSrcRegisters
[3].SrcRegister
.Index
= inst
->TexSrcUnit
;
542 /* texture lookup with explicit LOD */
543 fullinst
->Instruction
.Opcode
= TGSI_OPCODE_TXL
;
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 /* texture lookup with divide by Q component */
551 /* convert to TEX w/ special flag for division */
552 fullinst
->Instruction
.Opcode
= TGSI_OPCODE_TXP
;
553 fullinst
->Instruction
.NumSrcRegs
= 2;
554 fullinst
->InstructionExtTexture
.Texture
= map_texture_target( inst
->TexSrcTarget
);
555 fullinst
->FullSrcRegisters
[1].SrcRegister
.File
= TGSI_FILE_SAMPLER
;
556 fullinst
->FullSrcRegisters
[1].SrcRegister
.Index
= inst
->TexSrcUnit
;
559 fullinst
->Instruction
.Opcode
= TGSI_OPCODE_XPD
;
560 fulldst
->DstRegister
.WriteMask
&= TGSI_WRITEMASK_XYZ
;
563 fullinst
->Instruction
.Opcode
= TGSI_OPCODE_END
;
571 * \param usage_mask bitfield of TGSI_WRITEMASK_{XYZW} tokens
573 static struct tgsi_full_declaration
576 GLboolean interpolate_info
,
579 GLboolean semantic_info
,
580 GLuint semantic_name
,
581 GLbitfield semantic_index
)
583 struct tgsi_full_declaration decl
;
585 assert(semantic_name
< TGSI_SEMANTIC_COUNT
);
587 decl
= tgsi_default_full_declaration();
588 decl
.Declaration
.File
= TGSI_FILE_INPUT
;
589 decl
.Declaration
.UsageMask
= usage_mask
;
590 decl
.Declaration
.Semantic
= semantic_info
;
591 decl
.DeclarationRange
.First
= index
;
592 decl
.DeclarationRange
.Last
= index
;
594 decl
.Semantic
.SemanticName
= semantic_name
;
595 decl
.Semantic
.SemanticIndex
= semantic_index
;
597 if (interpolate_info
) {
598 decl
.Declaration
.Interpolate
= interpolate
;
605 * \param usage_mask bitfield of TGSI_WRITEMASK_{XYZW} tokens
607 static struct tgsi_full_declaration
610 GLuint semantic_name
,
611 GLuint semantic_index
,
612 GLbitfield usage_mask
)
614 struct tgsi_full_declaration decl
;
616 assert(semantic_name
< TGSI_SEMANTIC_COUNT
);
618 decl
= tgsi_default_full_declaration();
619 decl
.Declaration
.File
= TGSI_FILE_OUTPUT
;
620 decl
.Declaration
.UsageMask
= usage_mask
;
621 decl
.Declaration
.Semantic
= 1;
622 decl
.DeclarationRange
.First
= index
;
623 decl
.DeclarationRange
.Last
= index
;
624 decl
.Semantic
.SemanticName
= semantic_name
;
625 decl
.Semantic
.SemanticIndex
= semantic_index
;
631 static struct tgsi_full_declaration
636 struct tgsi_full_declaration decl
;
637 decl
= tgsi_default_full_declaration();
638 decl
.Declaration
.File
= TGSI_FILE_TEMPORARY
;
639 decl
.DeclarationRange
.First
= start_index
;
640 decl
.DeclarationRange
.Last
= end_index
;
644 static struct tgsi_full_declaration
649 struct tgsi_full_declaration decl
;
651 decl
= tgsi_default_full_declaration();
652 decl
.Declaration
.File
= TGSI_FILE_ADDRESS
;
653 decl
.DeclarationRange
.First
= start_index
;
654 decl
.DeclarationRange
.Last
= end_index
;
658 static struct tgsi_full_declaration
659 make_sampler_decl(GLuint index
)
661 struct tgsi_full_declaration decl
;
662 decl
= tgsi_default_full_declaration();
663 decl
.Declaration
.File
= TGSI_FILE_SAMPLER
;
664 decl
.DeclarationRange
.First
= index
;
665 decl
.DeclarationRange
.Last
= index
;
669 /** Reference into a constant buffer */
670 static struct tgsi_full_declaration
671 make_constant_decl(GLuint first
, GLuint last
)
673 struct tgsi_full_declaration decl
;
674 decl
= tgsi_default_full_declaration();
675 decl
.Declaration
.File
= TGSI_FILE_CONSTANT
;
676 decl
.DeclarationRange
.First
= first
;
677 decl
.DeclarationRange
.Last
= last
;
684 * Find the temporaries which are used in the given program.
687 find_temporaries(const struct gl_program
*program
,
688 GLboolean tempsUsed
[MAX_PROGRAM_TEMPS
])
692 for (i
= 0; i
< MAX_PROGRAM_TEMPS
; i
++)
693 tempsUsed
[i
] = GL_FALSE
;
695 for (i
= 0; i
< program
->NumInstructions
; i
++) {
696 const struct prog_instruction
*inst
= program
->Instructions
+ i
;
697 const GLuint n
= _mesa_num_inst_src_regs( inst
->Opcode
);
698 for (j
= 0; j
< n
; j
++) {
699 if (inst
->SrcReg
[j
].File
== PROGRAM_TEMPORARY
)
700 tempsUsed
[inst
->SrcReg
[j
].Index
] = GL_TRUE
;
701 if (inst
->DstReg
.File
== PROGRAM_TEMPORARY
)
702 tempsUsed
[inst
->DstReg
.Index
] = GL_TRUE
;
711 * Translate Mesa program to TGSI format.
712 * \param program the program to translate
713 * \param numInputs number of input registers used
714 * \param inputMapping maps Mesa fragment program inputs to TGSI generic
716 * \param inputSemanticName the TGSI_SEMANTIC flag for each input
717 * \param inputSemanticIndex the semantic index (ex: which texcoord) for each input
718 * \param interpMode the TGSI_INTERPOLATE_LINEAR/PERSP mode for each input
720 * \param numOutputs number of output registers used
721 * \param outputMapping maps Mesa fragment program outputs to TGSI
723 * \param outputSemanticName the TGSI_SEMANTIC flag for each output
724 * \param outputSemanticIndex the semantic index (ex: which texcoord) for each output
725 * \param tokens array to store translated tokens in
726 * \param maxTokens size of the tokens array
728 * \return number of tokens placed in 'tokens' buffer, or zero if error
731 st_translate_mesa_program(
733 const struct gl_program
*program
,
735 const GLuint inputMapping
[],
736 const ubyte inputSemanticName
[],
737 const ubyte inputSemanticIndex
[],
738 const GLuint interpMode
[],
740 const GLuint outputMapping
[],
741 const ubyte outputSemanticName
[],
742 const ubyte outputSemanticIndex
[],
743 struct tgsi_token
*tokens
,
747 GLuint ti
; /* token index */
748 struct tgsi_header
*header
;
749 struct tgsi_processor
*processor
;
750 struct tgsi_full_instruction fullinst
;
751 GLuint preamble_size
= 0;
752 GLuint immediates
[1000];
753 GLuint numImmediates
= 0;
754 GLboolean insideSubroutine
= GL_FALSE
;
755 GLboolean indirectAccess
= GL_FALSE
;
757 assert(procType
== TGSI_PROCESSOR_FRAGMENT
||
758 procType
== TGSI_PROCESSOR_VERTEX
);
760 *(struct tgsi_version
*) &tokens
[0] = tgsi_build_version();
762 header
= (struct tgsi_header
*) &tokens
[1];
763 *header
= tgsi_build_header();
765 processor
= (struct tgsi_processor
*) &tokens
[2];
766 *processor
= tgsi_build_processor( procType
, header
);
771 * Declare input attributes.
773 if (procType
== TGSI_PROCESSOR_FRAGMENT
) {
774 for (i
= 0; i
< numInputs
; i
++) {
775 struct tgsi_full_declaration fulldecl
;
776 fulldecl
= make_input_decl(i
,
777 GL_TRUE
, interpMode
[i
],
779 GL_TRUE
, inputSemanticName
[i
],
780 inputSemanticIndex
[i
]);
781 ti
+= tgsi_build_full_declaration(&fulldecl
,
789 /* XXX: this could probaby be merged with the clause above.
790 * the only difference is the semantic tags.
792 for (i
= 0; i
< numInputs
; i
++) {
793 struct tgsi_full_declaration fulldecl
;
794 fulldecl
= make_input_decl(i
,
798 ti
+= tgsi_build_full_declaration(&fulldecl
,
806 * Declare output attributes.
808 if (procType
== TGSI_PROCESSOR_FRAGMENT
) {
809 for (i
= 0; i
< numOutputs
; i
++) {
810 struct tgsi_full_declaration fulldecl
;
811 switch (outputSemanticName
[i
]) {
812 case TGSI_SEMANTIC_POSITION
:
813 fulldecl
= make_output_decl(i
,
814 TGSI_SEMANTIC_POSITION
, /* Z / Depth */
815 outputSemanticIndex
[i
],
818 case TGSI_SEMANTIC_COLOR
:
819 fulldecl
= make_output_decl(i
,
821 outputSemanticIndex
[i
],
822 TGSI_WRITEMASK_XYZW
);
828 ti
+= tgsi_build_full_declaration(&fulldecl
,
836 for (i
= 0; i
< numOutputs
; i
++) {
837 struct tgsi_full_declaration fulldecl
;
838 fulldecl
= make_output_decl(i
,
839 outputSemanticName
[i
],
840 outputSemanticIndex
[i
],
841 TGSI_WRITEMASK_XYZW
);
842 ti
+= tgsi_build_full_declaration(&fulldecl
,
849 /* temporary decls */
851 GLboolean tempsUsed
[MAX_PROGRAM_TEMPS
+ 1];
852 GLboolean inside_range
= GL_FALSE
;
853 GLuint start_range
= 0;
855 find_temporaries(program
, tempsUsed
);
856 tempsUsed
[MAX_PROGRAM_TEMPS
] = GL_FALSE
;
857 for (i
= 0; i
< MAX_PROGRAM_TEMPS
+ 1; i
++) {
858 if (tempsUsed
[i
] && !inside_range
) {
859 inside_range
= GL_TRUE
;
862 else if (!tempsUsed
[i
] && inside_range
) {
863 struct tgsi_full_declaration fulldecl
;
865 inside_range
= GL_FALSE
;
866 fulldecl
= make_temp_decl( start_range
, i
- 1 );
867 ti
+= tgsi_build_full_declaration(
876 /* Declare address register.
878 if (program
->NumAddressRegs
> 0) {
879 struct tgsi_full_declaration fulldecl
;
881 assert( program
->NumAddressRegs
== 1 );
883 fulldecl
= make_addr_decl( 0, 0 );
884 ti
+= tgsi_build_full_declaration(
890 indirectAccess
= GL_TRUE
;
893 /* immediates/literals */
894 memset(immediates
, ~0, sizeof(immediates
));
896 /* Emit immediates only when there is no address register in use.
897 * FIXME: Be smarter and recognize param arrays -- indirect addressing is
898 * only valid within the referenced array.
900 if (program
->Parameters
&& !indirectAccess
) {
901 for (i
= 0; i
< program
->Parameters
->NumParameters
; i
++) {
902 if (program
->Parameters
->Parameters
[i
].Type
== PROGRAM_CONSTANT
) {
903 struct tgsi_full_immediate fullimm
;
905 fullimm
= make_immediate( program
->Parameters
->ParameterValues
[i
], 4 );
906 ti
+= tgsi_build_full_immediate(
911 immediates
[i
] = numImmediates
;
917 /* constant buffer refs */
918 if (program
->Parameters
) {
919 GLint start
= -1, end
= -1;
921 for (i
= 0; i
< program
->Parameters
->NumParameters
; i
++) {
922 GLboolean emit
= (i
== program
->Parameters
->NumParameters
- 1);
925 switch (program
->Parameters
->Parameters
[i
].Type
) {
926 case PROGRAM_ENV_PARAM
:
927 case PROGRAM_STATE_VAR
:
928 case PROGRAM_NAMED_PARAM
:
929 case PROGRAM_UNIFORM
:
932 case PROGRAM_CONSTANT
:
933 matches
= indirectAccess
;
941 /* begin a sequence */
946 /* continue sequence */
952 /* end of sequence */
957 if (emit
&& start
>= 0) {
958 struct tgsi_full_declaration fulldecl
;
960 fulldecl
= make_constant_decl( start
, end
);
961 ti
+= tgsi_build_full_declaration(
971 /* texture samplers */
972 for (i
= 0; i
< 8; i
++) {
973 if (program
->SamplersUsed
& (1 << i
)) {
974 struct tgsi_full_declaration fulldecl
;
976 fulldecl
= make_sampler_decl( i
);
977 ti
+= tgsi_build_full_declaration(
985 for (i
= 0; i
< program
->NumInstructions
; i
++) {
987 &program
->Instructions
[i
],
997 ti
+= tgsi_build_full_instruction(
1005 if(!tgsi_sanity_check(tokens
)) {
1006 debug_printf("Due to sanity check failure(s) above the following shader program is invalid:\n");
1007 debug_printf("\nOriginal program:\n%s", program
->String
);
1008 debug_printf("\nMesa program:\n");
1009 _mesa_print_program(program
);
1010 debug_printf("\nTGSI program:\n");
1011 tgsi_dump(tokens
, 0);