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
].NegateBase
!= NEGATE_NONE
&&
279 inst
->SrcReg
[i
].NegateBase
!= 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
].NegateBase
== NEGATE_XYZW
) {
300 fullsrc
->SrcRegister
.Negate
= 1;
302 else if( inst
->SrcReg
[i
].NegateBase
!= NEGATE_NONE
) {
303 if( inst
->SrcReg
[i
].NegateBase
& NEGATE_X
) {
304 fullsrc
->SrcRegisterExtSwz
.NegateX
= 1;
306 if( inst
->SrcReg
[i
].NegateBase
& NEGATE_Y
) {
307 fullsrc
->SrcRegisterExtSwz
.NegateY
= 1;
309 if( inst
->SrcReg
[i
].NegateBase
& NEGATE_Z
) {
310 fullsrc
->SrcRegisterExtSwz
.NegateZ
= 1;
312 if( inst
->SrcReg
[i
].NegateBase
& NEGATE_W
) {
313 fullsrc
->SrcRegisterExtSwz
.NegateW
= 1;
317 if( inst
->SrcReg
[i
].Abs
) {
318 fullsrc
->SrcRegisterExtMod
.Absolute
= 1;
321 if( inst
->SrcReg
[i
].NegateAbs
) {
322 fullsrc
->SrcRegisterExtMod
.Negate
= 1;
325 if( inst
->SrcReg
[i
].RelAddr
) {
326 fullsrc
->SrcRegister
.Indirect
= 1;
328 fullsrc
->SrcRegisterInd
.File
= TGSI_FILE_ADDRESS
;
329 fullsrc
->SrcRegisterInd
.Index
= 0;
333 switch( inst
->Opcode
) {
335 fullinst
->Instruction
.Opcode
= TGSI_OPCODE_ARL
;
338 fullinst
->Instruction
.Opcode
= TGSI_OPCODE_ABS
;
341 fullinst
->Instruction
.Opcode
= TGSI_OPCODE_ADD
;
344 fullinst
->Instruction
.Opcode
= TGSI_OPCODE_BGNLOOP2
;
345 fullinst
->InstructionExtLabel
.Label
= inst
->BranchTarget
+ preamble_size
;
348 fullinst
->Instruction
.Opcode
= TGSI_OPCODE_BGNSUB
;
349 *insideSubroutine
= GL_TRUE
;
352 fullinst
->Instruction
.Opcode
= TGSI_OPCODE_BRA
;
355 fullinst
->Instruction
.Opcode
= TGSI_OPCODE_BRK
;
358 fullinst
->Instruction
.Opcode
= TGSI_OPCODE_CAL
;
359 fullinst
->InstructionExtLabel
.Label
= inst
->BranchTarget
+ preamble_size
;
362 fullinst
->Instruction
.Opcode
= TGSI_OPCODE_CMP
;
365 fullinst
->Instruction
.Opcode
= TGSI_OPCODE_CONT
;
368 fullinst
->Instruction
.Opcode
= TGSI_OPCODE_COS
;
371 fullinst
->Instruction
.Opcode
= TGSI_OPCODE_DDX
;
374 fullinst
->Instruction
.Opcode
= TGSI_OPCODE_DDY
;
377 fullinst
->Instruction
.Opcode
= TGSI_OPCODE_DP2
;
380 fullinst
->Instruction
.Opcode
= TGSI_OPCODE_DP2A
;
383 fullinst
->Instruction
.Opcode
= TGSI_OPCODE_DP3
;
386 fullinst
->Instruction
.Opcode
= TGSI_OPCODE_DP4
;
389 fullinst
->Instruction
.Opcode
= TGSI_OPCODE_DPH
;
392 fullinst
->Instruction
.Opcode
= TGSI_OPCODE_DST
;
395 fullinst
->Instruction
.Opcode
= TGSI_OPCODE_ELSE
;
396 fullinst
->InstructionExtLabel
.Label
= inst
->BranchTarget
+ preamble_size
;
399 fullinst
->Instruction
.Opcode
= TGSI_OPCODE_ENDIF
;
402 fullinst
->Instruction
.Opcode
= TGSI_OPCODE_ENDLOOP2
;
403 fullinst
->InstructionExtLabel
.Label
= inst
->BranchTarget
+ preamble_size
;
406 fullinst
->Instruction
.Opcode
= TGSI_OPCODE_ENDSUB
;
407 *insideSubroutine
= GL_FALSE
;
410 fullinst
->Instruction
.Opcode
= TGSI_OPCODE_EX2
;
413 fullinst
->Instruction
.Opcode
= TGSI_OPCODE_EXP
;
416 fullinst
->Instruction
.Opcode
= TGSI_OPCODE_FLR
;
419 fullinst
->Instruction
.Opcode
= TGSI_OPCODE_FRC
;
422 fullinst
->Instruction
.Opcode
= TGSI_OPCODE_IF
;
423 fullinst
->InstructionExtLabel
.Label
= inst
->BranchTarget
+ preamble_size
;
426 fullinst
->Instruction
.Opcode
= TGSI_OPCODE_TRUNC
;
430 fullinst
->Instruction
.Opcode
= TGSI_OPCODE_KIL
;
434 assert(inst
->DstReg
.CondMask
== COND_TR
);
435 fullinst
->Instruction
.Opcode
= TGSI_OPCODE_KILP
;
438 fullinst
->Instruction
.Opcode
= TGSI_OPCODE_LG2
;
441 fullinst
->Instruction
.Opcode
= TGSI_OPCODE_LOG
;
444 fullinst
->Instruction
.Opcode
= TGSI_OPCODE_LIT
;
447 fullinst
->Instruction
.Opcode
= TGSI_OPCODE_LRP
;
450 fullinst
->Instruction
.Opcode
= TGSI_OPCODE_MAD
;
453 fullinst
->Instruction
.Opcode
= TGSI_OPCODE_MAX
;
456 fullinst
->Instruction
.Opcode
= TGSI_OPCODE_MIN
;
459 fullinst
->Instruction
.Opcode
= TGSI_OPCODE_MOV
;
462 fullinst
->Instruction
.Opcode
= TGSI_OPCODE_MUL
;
465 fullinst
->Instruction
.Opcode
= TGSI_OPCODE_NOISE1
;
468 fullinst
->Instruction
.Opcode
= TGSI_OPCODE_NOISE2
;
471 fullinst
->Instruction
.Opcode
= TGSI_OPCODE_NOISE3
;
474 fullinst
->Instruction
.Opcode
= TGSI_OPCODE_NOISE4
;
477 fullinst
->Instruction
.Opcode
= TGSI_OPCODE_NOP
;
480 fullinst
->Instruction
.Opcode
= TGSI_OPCODE_NRM
;
483 fullinst
->Instruction
.Opcode
= TGSI_OPCODE_NRM4
;
486 fullinst
->Instruction
.Opcode
= TGSI_OPCODE_POW
;
489 fullinst
->Instruction
.Opcode
= TGSI_OPCODE_RCP
;
492 /* If RET is used inside main (not a real subroutine) we may want
493 * to execute END instead of RET. TBD...
495 if (1 /* *insideSubroutine */) {
496 fullinst
->Instruction
.Opcode
= TGSI_OPCODE_RET
;
499 /* inside main() pseudo-function */
500 fullinst
->Instruction
.Opcode
= TGSI_OPCODE_END
;
504 fullinst
->Instruction
.Opcode
= TGSI_OPCODE_RSQ
;
507 fullinst
->Instruction
.Opcode
= TGSI_OPCODE_SCS
;
508 fulldst
->DstRegister
.WriteMask
&= TGSI_WRITEMASK_XY
;
511 fullinst
->Instruction
.Opcode
= TGSI_OPCODE_SEQ
;
514 fullinst
->Instruction
.Opcode
= TGSI_OPCODE_SGE
;
517 fullinst
->Instruction
.Opcode
= TGSI_OPCODE_SGT
;
520 fullinst
->Instruction
.Opcode
= TGSI_OPCODE_SIN
;
523 fullinst
->Instruction
.Opcode
= TGSI_OPCODE_SLE
;
526 fullinst
->Instruction
.Opcode
= TGSI_OPCODE_SLT
;
529 fullinst
->Instruction
.Opcode
= TGSI_OPCODE_SNE
;
532 fullinst
->Instruction
.Opcode
= TGSI_OPCODE_SSG
;
535 fullinst
->Instruction
.Opcode
= TGSI_OPCODE_SUB
;
538 fullinst
->Instruction
.Opcode
= TGSI_OPCODE_SWZ
;
541 /* ordinary texture lookup */
542 fullinst
->Instruction
.Opcode
= TGSI_OPCODE_TEX
;
543 fullinst
->Instruction
.NumSrcRegs
= 2;
544 fullinst
->InstructionExtTexture
.Texture
=
545 map_texture_target( inst
->TexSrcTarget
, inst
->TexShadow
);
546 fullinst
->FullSrcRegisters
[1].SrcRegister
.File
= TGSI_FILE_SAMPLER
;
547 fullinst
->FullSrcRegisters
[1].SrcRegister
.Index
= inst
->TexSrcUnit
;
550 /* texture lookup with LOD bias */
551 fullinst
->Instruction
.Opcode
= TGSI_OPCODE_TXB
;
552 fullinst
->Instruction
.NumSrcRegs
= 2;
553 fullinst
->InstructionExtTexture
.Texture
=
554 map_texture_target( inst
->TexSrcTarget
, inst
->TexShadow
);
555 fullinst
->FullSrcRegisters
[1].SrcRegister
.File
= TGSI_FILE_SAMPLER
;
556 fullinst
->FullSrcRegisters
[1].SrcRegister
.Index
= inst
->TexSrcUnit
;
559 /* texture lookup with explicit partial derivatives */
560 fullinst
->Instruction
.Opcode
= TGSI_OPCODE_TXD
;
561 fullinst
->Instruction
.NumSrcRegs
= 4;
562 fullinst
->InstructionExtTexture
.Texture
=
563 map_texture_target( inst
->TexSrcTarget
, inst
->TexShadow
);
564 /* src[0] = coord, src[1] = d[strq]/dx, src[2] = d[strq]/dy */
565 fullinst
->FullSrcRegisters
[3].SrcRegister
.File
= TGSI_FILE_SAMPLER
;
566 fullinst
->FullSrcRegisters
[3].SrcRegister
.Index
= inst
->TexSrcUnit
;
569 /* texture lookup with explicit LOD */
570 fullinst
->Instruction
.Opcode
= TGSI_OPCODE_TXL
;
571 fullinst
->Instruction
.NumSrcRegs
= 2;
572 fullinst
->InstructionExtTexture
.Texture
=
573 map_texture_target( inst
->TexSrcTarget
, inst
->TexShadow
);
574 fullinst
->FullSrcRegisters
[1].SrcRegister
.File
= TGSI_FILE_SAMPLER
;
575 fullinst
->FullSrcRegisters
[1].SrcRegister
.Index
= inst
->TexSrcUnit
;
578 /* texture lookup with divide by Q component */
579 /* convert to TEX w/ special flag for division */
580 fullinst
->Instruction
.Opcode
= TGSI_OPCODE_TXP
;
581 fullinst
->Instruction
.NumSrcRegs
= 2;
582 fullinst
->InstructionExtTexture
.Texture
=
583 map_texture_target( inst
->TexSrcTarget
, inst
->TexShadow
);
584 fullinst
->FullSrcRegisters
[1].SrcRegister
.File
= TGSI_FILE_SAMPLER
;
585 fullinst
->FullSrcRegisters
[1].SrcRegister
.Index
= inst
->TexSrcUnit
;
588 fullinst
->Instruction
.Opcode
= TGSI_OPCODE_XPD
;
589 fulldst
->DstRegister
.WriteMask
&= TGSI_WRITEMASK_XYZ
;
592 fullinst
->Instruction
.Opcode
= TGSI_OPCODE_END
;
600 * \param usage_mask bitfield of TGSI_WRITEMASK_{XYZW} tokens
602 static struct tgsi_full_declaration
605 GLboolean interpolate_info
,
608 GLboolean semantic_info
,
609 GLuint semantic_name
,
610 GLbitfield semantic_index
,
611 GLbitfield input_flags
)
613 struct tgsi_full_declaration decl
;
615 assert(semantic_name
< TGSI_SEMANTIC_COUNT
);
617 decl
= tgsi_default_full_declaration();
618 decl
.Declaration
.File
= TGSI_FILE_INPUT
;
619 decl
.Declaration
.UsageMask
= usage_mask
;
620 decl
.Declaration
.Semantic
= semantic_info
;
621 decl
.DeclarationRange
.First
= index
;
622 decl
.DeclarationRange
.Last
= index
;
624 decl
.Semantic
.SemanticName
= semantic_name
;
625 decl
.Semantic
.SemanticIndex
= semantic_index
;
627 if (interpolate_info
) {
628 decl
.Declaration
.Interpolate
= interpolate
;
630 if (input_flags
& PROG_PARAM_BIT_CENTROID
)
631 decl
.Declaration
.Centroid
= 1;
632 if (input_flags
& PROG_PARAM_BIT_INVARIANT
)
633 decl
.Declaration
.Invariant
= 1;
639 * \param usage_mask bitfield of TGSI_WRITEMASK_{XYZW} tokens
641 static struct tgsi_full_declaration
644 GLuint semantic_name
,
645 GLuint semantic_index
,
647 GLbitfield output_flags
)
649 struct tgsi_full_declaration decl
;
651 assert(semantic_name
< TGSI_SEMANTIC_COUNT
);
653 decl
= tgsi_default_full_declaration();
654 decl
.Declaration
.File
= TGSI_FILE_OUTPUT
;
655 decl
.Declaration
.UsageMask
= usage_mask
;
656 decl
.Declaration
.Semantic
= 1;
657 decl
.DeclarationRange
.First
= index
;
658 decl
.DeclarationRange
.Last
= index
;
659 decl
.Semantic
.SemanticName
= semantic_name
;
660 decl
.Semantic
.SemanticIndex
= semantic_index
;
661 if (output_flags
& PROG_PARAM_BIT_CENTROID
)
662 decl
.Declaration
.Centroid
= 1;
663 if (output_flags
& PROG_PARAM_BIT_INVARIANT
)
664 decl
.Declaration
.Invariant
= 1;
670 static struct tgsi_full_declaration
675 struct tgsi_full_declaration decl
;
676 decl
= tgsi_default_full_declaration();
677 decl
.Declaration
.File
= TGSI_FILE_TEMPORARY
;
678 decl
.DeclarationRange
.First
= start_index
;
679 decl
.DeclarationRange
.Last
= end_index
;
683 static struct tgsi_full_declaration
688 struct tgsi_full_declaration decl
;
690 decl
= tgsi_default_full_declaration();
691 decl
.Declaration
.File
= TGSI_FILE_ADDRESS
;
692 decl
.DeclarationRange
.First
= start_index
;
693 decl
.DeclarationRange
.Last
= end_index
;
697 static struct tgsi_full_declaration
698 make_sampler_decl(GLuint index
)
700 struct tgsi_full_declaration decl
;
701 decl
= tgsi_default_full_declaration();
702 decl
.Declaration
.File
= TGSI_FILE_SAMPLER
;
703 decl
.DeclarationRange
.First
= index
;
704 decl
.DeclarationRange
.Last
= index
;
708 /** Reference into a constant buffer */
709 static struct tgsi_full_declaration
710 make_constant_decl(GLuint first
, GLuint last
)
712 struct tgsi_full_declaration decl
;
713 decl
= tgsi_default_full_declaration();
714 decl
.Declaration
.File
= TGSI_FILE_CONSTANT
;
715 decl
.DeclarationRange
.First
= first
;
716 decl
.DeclarationRange
.Last
= last
;
723 * Find the temporaries which are used in the given program.
726 find_temporaries(const struct gl_program
*program
,
727 GLboolean tempsUsed
[MAX_PROGRAM_TEMPS
])
731 for (i
= 0; i
< MAX_PROGRAM_TEMPS
; i
++)
732 tempsUsed
[i
] = GL_FALSE
;
734 for (i
= 0; i
< program
->NumInstructions
; i
++) {
735 const struct prog_instruction
*inst
= program
->Instructions
+ i
;
736 const GLuint n
= _mesa_num_inst_src_regs( inst
->Opcode
);
737 for (j
= 0; j
< n
; j
++) {
738 if (inst
->SrcReg
[j
].File
== PROGRAM_TEMPORARY
)
739 tempsUsed
[inst
->SrcReg
[j
].Index
] = GL_TRUE
;
740 if (inst
->DstReg
.File
== PROGRAM_TEMPORARY
)
741 tempsUsed
[inst
->DstReg
.Index
] = GL_TRUE
;
748 * Find an unused temporary in the tempsUsed array.
751 find_free_temporary(GLboolean tempsUsed
[MAX_PROGRAM_TEMPS
])
754 for (i
= 0; i
< MAX_PROGRAM_TEMPS
; i
++) {
756 tempsUsed
[i
] = GL_TRUE
;
764 /** helper for building simple TGSI instruction, one src register */
766 build_tgsi_instruction1(struct tgsi_full_instruction
*inst
,
768 int dstFile
, int dstIndex
, int writemask
,
769 int srcFile1
, int srcIndex1
)
771 *inst
= tgsi_default_full_instruction();
773 inst
->Instruction
.Opcode
= opcode
;
775 inst
->Instruction
.NumDstRegs
= 1;
776 inst
->FullDstRegisters
[0].DstRegister
.File
= dstFile
;
777 inst
->FullDstRegisters
[0].DstRegister
.Index
= dstIndex
;
778 inst
->FullDstRegisters
[0].DstRegister
.WriteMask
= writemask
;
780 inst
->Instruction
.NumSrcRegs
= 1;
781 inst
->FullSrcRegisters
[0].SrcRegister
.File
= srcFile1
;
782 inst
->FullSrcRegisters
[0].SrcRegister
.Index
= srcIndex1
;
786 /** helper for building simple TGSI instruction, two src registers */
788 build_tgsi_instruction2(struct tgsi_full_instruction
*inst
,
790 int dstFile
, int dstIndex
, int writemask
,
791 int srcFile1
, int srcIndex1
,
792 int srcFile2
, int srcIndex2
)
794 *inst
= tgsi_default_full_instruction();
796 inst
->Instruction
.Opcode
= opcode
;
798 inst
->Instruction
.NumDstRegs
= 1;
799 inst
->FullDstRegisters
[0].DstRegister
.File
= dstFile
;
800 inst
->FullDstRegisters
[0].DstRegister
.Index
= dstIndex
;
801 inst
->FullDstRegisters
[0].DstRegister
.WriteMask
= writemask
;
803 inst
->Instruction
.NumSrcRegs
= 2;
804 inst
->FullSrcRegisters
[0].SrcRegister
.File
= srcFile1
;
805 inst
->FullSrcRegisters
[0].SrcRegister
.Index
= srcIndex1
;
806 inst
->FullSrcRegisters
[1].SrcRegister
.File
= srcFile2
;
807 inst
->FullSrcRegisters
[1].SrcRegister
.Index
= srcIndex2
;
813 * Emit the TGSI instructions for inverting the WPOS y coordinate.
816 emit_inverted_wpos(struct tgsi_token
*tokens
,
820 struct tgsi_header
*header
, int maxTokens
)
822 struct tgsi_full_instruction fullinst
;
825 /* MOV wpos_temp.xzw, input[wpos]; */
826 build_tgsi_instruction1(&fullinst
,
828 TGSI_FILE_TEMPORARY
, wpos_temp
, WRITEMASK_XZW
,
831 ti
+= tgsi_build_full_instruction(&fullinst
,
836 /* SUB wpos_temp.y, const[winsize_const] - input[wpos_input]; */
837 build_tgsi_instruction2(&fullinst
,
839 TGSI_FILE_TEMPORARY
, wpos_temp
, WRITEMASK_Y
,
840 TGSI_FILE_CONSTANT
, winsize_const
,
841 TGSI_FILE_INPUT
, wpos_input
);
843 ti
+= tgsi_build_full_instruction(&fullinst
,
855 * Translate Mesa program to TGSI format.
856 * \param program the program to translate
857 * \param numInputs number of input registers used
858 * \param inputMapping maps Mesa fragment program inputs to TGSI generic
860 * \param inputSemanticName the TGSI_SEMANTIC flag for each input
861 * \param inputSemanticIndex the semantic index (ex: which texcoord) for each input
862 * \param interpMode the TGSI_INTERPOLATE_LINEAR/PERSP mode for each input
864 * \param numOutputs number of output registers used
865 * \param outputMapping maps Mesa fragment program outputs to TGSI
867 * \param outputSemanticName the TGSI_SEMANTIC flag for each output
868 * \param outputSemanticIndex the semantic index (ex: which texcoord) for each output
869 * \param tokens array to store translated tokens in
870 * \param maxTokens size of the tokens array
872 * \return number of tokens placed in 'tokens' buffer, or zero if error
875 st_translate_mesa_program(
878 const struct gl_program
*program
,
880 const GLuint inputMapping
[],
881 const ubyte inputSemanticName
[],
882 const ubyte inputSemanticIndex
[],
883 const GLuint interpMode
[],
884 const GLbitfield inputFlags
[],
886 const GLuint outputMapping
[],
887 const ubyte outputSemanticName
[],
888 const ubyte outputSemanticIndex
[],
889 const GLbitfield outputFlags
[],
890 struct tgsi_token
*tokens
,
894 GLuint ti
; /* token index */
895 struct tgsi_header
*header
;
896 struct tgsi_processor
*processor
;
897 GLuint preamble_size
= 0;
898 GLuint immediates
[1000];
899 GLuint numImmediates
= 0;
900 GLboolean insideSubroutine
= GL_FALSE
;
901 GLboolean indirectAccess
= GL_FALSE
;
902 GLboolean tempsUsed
[MAX_PROGRAM_TEMPS
+ 1];
903 GLint wposTemp
= -1, winHeightConst
= -1;
905 assert(procType
== TGSI_PROCESSOR_FRAGMENT
||
906 procType
== TGSI_PROCESSOR_VERTEX
);
908 find_temporaries(program
, tempsUsed
);
910 if (procType
== TGSI_PROCESSOR_FRAGMENT
) {
911 if (program
->InputsRead
& FRAG_BIT_WPOS
) {
912 /* Fragment program uses fragment position input.
913 * Need to replace instances of INPUT[WPOS] with temp T
914 * where T = INPUT[WPOS] by y is inverted.
916 static const gl_state_index winSizeState
[STATE_LENGTH
]
917 = { STATE_INTERNAL
, STATE_FB_SIZE
, 0, 0, 0 };
918 winHeightConst
= _mesa_add_state_reference(program
->Parameters
,
920 wposTemp
= find_free_temporary(tempsUsed
);
925 *(struct tgsi_version
*) &tokens
[0] = tgsi_build_version();
927 header
= (struct tgsi_header
*) &tokens
[1];
928 *header
= tgsi_build_header();
930 processor
= (struct tgsi_processor
*) &tokens
[2];
931 *processor
= tgsi_build_processor( procType
, header
);
936 * Declare input attributes.
938 if (procType
== TGSI_PROCESSOR_FRAGMENT
) {
939 for (i
= 0; i
< numInputs
; i
++) {
940 struct tgsi_full_declaration fulldecl
;
941 fulldecl
= make_input_decl(i
,
942 GL_TRUE
, interpMode
[i
],
944 GL_TRUE
, inputSemanticName
[i
],
945 inputSemanticIndex
[i
],
947 ti
+= tgsi_build_full_declaration(&fulldecl
,
955 /* XXX: this could probaby be merged with the clause above.
956 * the only difference is the semantic tags.
958 for (i
= 0; i
< numInputs
; i
++) {
959 struct tgsi_full_declaration fulldecl
;
960 fulldecl
= make_input_decl(i
,
965 ti
+= tgsi_build_full_declaration(&fulldecl
,
973 * Declare output attributes.
975 if (procType
== TGSI_PROCESSOR_FRAGMENT
) {
976 for (i
= 0; i
< numOutputs
; i
++) {
977 struct tgsi_full_declaration fulldecl
;
978 switch (outputSemanticName
[i
]) {
979 case TGSI_SEMANTIC_POSITION
:
980 fulldecl
= make_output_decl(i
,
981 TGSI_SEMANTIC_POSITION
, /* Z / Depth */
982 outputSemanticIndex
[i
],
986 case TGSI_SEMANTIC_COLOR
:
987 fulldecl
= make_output_decl(i
,
989 outputSemanticIndex
[i
],
997 ti
+= tgsi_build_full_declaration(&fulldecl
,
1005 for (i
= 0; i
< numOutputs
; i
++) {
1006 struct tgsi_full_declaration fulldecl
;
1007 fulldecl
= make_output_decl(i
,
1008 outputSemanticName
[i
],
1009 outputSemanticIndex
[i
],
1010 TGSI_WRITEMASK_XYZW
,
1012 ti
+= tgsi_build_full_declaration(&fulldecl
,
1019 /* temporary decls */
1021 GLboolean inside_range
= GL_FALSE
;
1022 GLuint start_range
= 0;
1024 tempsUsed
[MAX_PROGRAM_TEMPS
] = GL_FALSE
;
1025 for (i
= 0; i
< MAX_PROGRAM_TEMPS
+ 1; i
++) {
1026 if (tempsUsed
[i
] && !inside_range
) {
1027 inside_range
= GL_TRUE
;
1030 else if (!tempsUsed
[i
] && inside_range
) {
1031 struct tgsi_full_declaration fulldecl
;
1033 inside_range
= GL_FALSE
;
1034 fulldecl
= make_temp_decl( start_range
, i
- 1 );
1035 ti
+= tgsi_build_full_declaration(
1044 /* Declare address register.
1046 if (program
->NumAddressRegs
> 0) {
1047 struct tgsi_full_declaration fulldecl
;
1049 assert( program
->NumAddressRegs
== 1 );
1051 fulldecl
= make_addr_decl( 0, 0 );
1052 ti
+= tgsi_build_full_declaration(
1058 indirectAccess
= GL_TRUE
;
1061 /* immediates/literals */
1062 memset(immediates
, ~0, sizeof(immediates
));
1064 /* Emit immediates only when there is no address register in use.
1065 * FIXME: Be smarter and recognize param arrays -- indirect addressing is
1066 * only valid within the referenced array.
1068 if (program
->Parameters
&& !indirectAccess
) {
1069 for (i
= 0; i
< program
->Parameters
->NumParameters
; i
++) {
1070 if (program
->Parameters
->Parameters
[i
].Type
== PROGRAM_CONSTANT
) {
1071 struct tgsi_full_immediate fullimm
;
1073 fullimm
= make_immediate( program
->Parameters
->ParameterValues
[i
], 4 );
1074 ti
+= tgsi_build_full_immediate(
1079 immediates
[i
] = numImmediates
;
1085 /* constant buffer refs */
1086 if (program
->Parameters
) {
1087 GLint start
= -1, end
= -1;
1089 for (i
= 0; i
< program
->Parameters
->NumParameters
; i
++) {
1090 GLboolean emit
= (i
== program
->Parameters
->NumParameters
- 1);
1093 switch (program
->Parameters
->Parameters
[i
].Type
) {
1094 case PROGRAM_ENV_PARAM
:
1095 case PROGRAM_STATE_VAR
:
1096 case PROGRAM_NAMED_PARAM
:
1097 case PROGRAM_UNIFORM
:
1100 case PROGRAM_CONSTANT
:
1101 matches
= indirectAccess
;
1109 /* begin a sequence */
1114 /* continue sequence */
1120 /* end of sequence */
1125 if (emit
&& start
>= 0) {
1126 struct tgsi_full_declaration fulldecl
;
1128 fulldecl
= make_constant_decl( start
, end
);
1129 ti
+= tgsi_build_full_declaration(
1139 /* texture samplers */
1140 for (i
= 0; i
< ctx
->Const
.MaxTextureImageUnits
; i
++) {
1141 if (program
->SamplersUsed
& (1 << i
)) {
1142 struct tgsi_full_declaration fulldecl
;
1144 fulldecl
= make_sampler_decl( i
);
1145 ti
+= tgsi_build_full_declaration(
1153 /* invert WPOS fragment input */
1154 if (wposTemp
>= 0) {
1155 ti
+= emit_inverted_wpos(&tokens
[ti
], wposTemp
, winHeightConst
,
1156 inputMapping
[FRAG_ATTRIB_WPOS
],
1157 header
, maxTokens
- ti
);
1158 preamble_size
= 2; /* two instructions added */
1161 for (i
= 0; i
< program
->NumInstructions
; i
++) {
1162 struct tgsi_full_instruction fullinst
;
1164 compile_instruction(
1165 &program
->Instructions
[i
],
1176 ti
+= tgsi_build_full_instruction(
1184 if(!tgsi_sanity_check(tokens
)) {
1185 debug_printf("Due to sanity check failure(s) above the following shader program is invalid:\n");
1186 debug_printf("\nOriginal program:\n%s", program
->String
);
1187 debug_printf("\nMesa program:\n");
1188 _mesa_print_program(program
);
1189 debug_printf("\nTGSI program:\n");
1190 tgsi_dump(tokens
, 0);