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(
108 const GLuint inputMapping
[],
109 const GLuint outputMapping
[],
110 const GLuint immediateMapping
[],
111 GLboolean indirectAccess
)
114 case TGSI_FILE_INPUT
:
115 if (procType
== TGSI_PROCESSOR_FRAGMENT
&&
116 index
== FRAG_ATTRIB_FOGC
) {
117 if (GET_SWZ(*swizzle
, 0) == SWIZZLE_X
) {
118 /* do nothing we're, ok */
119 } else if (GET_SWZ(*swizzle
, 0) == SWIZZLE_Y
) {
120 /* replace the swizzle with xxxx */
121 *swizzle
= MAKE_SWIZZLE4(SWIZZLE_X
,
125 /* register after fog */
126 return inputMapping
[index
] + 1;
128 *swizzle
= MAKE_SWIZZLE4(SWIZZLE_Z
,
132 /* register after frontface */
133 return inputMapping
[index
] + 2;
136 /* inputs are mapped according to the user-defined map */
137 return inputMapping
[index
];
139 case TGSI_FILE_OUTPUT
:
140 return outputMapping
[index
];
142 case TGSI_FILE_IMMEDIATE
:
145 assert(immediateMapping
[index
] != ~0);
146 return immediateMapping
[index
];
154 * Map mesa texture target to TGSI texture target.
161 switch( textarget
) {
162 case TEXTURE_1D_INDEX
:
164 return TGSI_TEXTURE_SHADOW1D
;
166 return TGSI_TEXTURE_1D
;
167 case TEXTURE_2D_INDEX
:
169 return TGSI_TEXTURE_SHADOW2D
;
171 return TGSI_TEXTURE_2D
;
172 case TEXTURE_3D_INDEX
:
173 return TGSI_TEXTURE_3D
;
174 case TEXTURE_CUBE_INDEX
:
175 return TGSI_TEXTURE_CUBE
;
176 case TEXTURE_RECT_INDEX
:
178 return TGSI_TEXTURE_SHADOWRECT
;
180 return TGSI_TEXTURE_RECT
;
185 return TGSI_TEXTURE_1D
;
194 return TGSI_SAT_NONE
;
195 case SATURATE_ZERO_ONE
:
196 return TGSI_SAT_ZERO_ONE
;
197 case SATURATE_PLUS_MINUS_ONE
:
198 return TGSI_SAT_MINUS_PLUS_ONE
;
201 return TGSI_SAT_NONE
;
209 assert( WRITEMASK_X
== TGSI_WRITEMASK_X
);
210 assert( WRITEMASK_Y
== TGSI_WRITEMASK_Y
);
211 assert( WRITEMASK_Z
== TGSI_WRITEMASK_Z
);
212 assert( WRITEMASK_W
== TGSI_WRITEMASK_W
);
213 assert( (writemask
& ~TGSI_WRITEMASK_XYZW
) == 0 );
218 static struct tgsi_full_immediate
219 make_immediate(const float *value
, uint size
)
221 struct tgsi_full_immediate imm
;
224 imm
= tgsi_default_full_immediate();
225 imm
.Immediate
.NrTokens
+= size
;
226 imm
.Immediate
.DataType
= TGSI_IMM_FLOAT32
;
228 for (i
= 0; i
< size
; i
++)
229 imm
.u
[i
].Float
= value
[i
];
236 const struct prog_instruction
*inst
,
237 struct tgsi_full_instruction
*fullinst
,
238 const GLuint inputMapping
[],
239 const GLuint outputMapping
[],
240 const GLuint immediateMapping
[],
241 GLboolean indirectAccess
,
242 GLuint preamble_size
,
244 GLboolean
*insideSubroutine
,
248 struct tgsi_full_dst_register
*fulldst
;
249 struct tgsi_full_src_register
*fullsrc
;
251 *fullinst
= tgsi_default_full_instruction();
253 fullinst
->Instruction
.Saturate
= convert_sat( inst
->SaturateMode
);
254 fullinst
->Instruction
.NumDstRegs
= _mesa_num_inst_dst_regs( inst
->Opcode
);
255 fullinst
->Instruction
.NumSrcRegs
= _mesa_num_inst_src_regs( inst
->Opcode
);
257 fulldst
= &fullinst
->FullDstRegisters
[0];
258 fulldst
->DstRegister
.File
= map_register_file( inst
->DstReg
.File
, 0, NULL
, GL_FALSE
);
259 fulldst
->DstRegister
.Index
= map_register_file_index(
261 fulldst
->DstRegister
.File
,
268 fulldst
->DstRegister
.WriteMask
= convert_writemask( inst
->DstReg
.WriteMask
);
269 if (inst
->DstReg
.RelAddr
) {
270 fulldst
->DstRegister
.Indirect
= 1;
271 fulldst
->DstRegisterInd
.File
= TGSI_FILE_ADDRESS
;
272 fulldst
->DstRegisterInd
.Index
= 0;
275 for (i
= 0; i
< fullinst
->Instruction
.NumSrcRegs
; i
++) {
277 GLuint swizzle
= inst
->SrcReg
[i
].Swizzle
;
279 fullsrc
= &fullinst
->FullSrcRegisters
[i
];
281 if (procType
== TGSI_PROCESSOR_FRAGMENT
&&
282 inst
->SrcReg
[i
].File
== PROGRAM_INPUT
&&
283 inst
->SrcReg
[i
].Index
== FRAG_ATTRIB_WPOS
) {
284 /* special case of INPUT[WPOS] */
285 fullsrc
->SrcRegister
.File
= TGSI_FILE_TEMPORARY
;
286 fullsrc
->SrcRegister
.Index
= wposTemp
;
289 /* any other src register */
290 fullsrc
->SrcRegister
.File
= map_register_file(
291 inst
->SrcReg
[i
].File
,
292 inst
->SrcReg
[i
].Index
,
295 fullsrc
->SrcRegister
.Index
= map_register_file_index(
297 fullsrc
->SrcRegister
.File
,
298 inst
->SrcReg
[i
].Index
,
306 /* swizzle (ext swizzle also depends on negation) */
309 GLboolean extended
= (inst
->SrcReg
[i
].Negate
!= NEGATE_NONE
&&
310 inst
->SrcReg
[i
].Negate
!= NEGATE_XYZW
);
311 for( j
= 0; j
< 4; j
++ ) {
312 swz
[j
] = GET_SWZ( swizzle
, j
);
313 if (swz
[j
] > SWIZZLE_W
)
317 for (j
= 0; j
< 4; j
++) {
318 tgsi_util_set_src_register_extswizzle(&fullsrc
->SrcRegisterExtSwz
,
323 for (j
= 0; j
< 4; j
++) {
324 tgsi_util_set_src_register_swizzle(&fullsrc
->SrcRegister
,
330 if( inst
->SrcReg
[i
].Negate
== NEGATE_XYZW
) {
331 fullsrc
->SrcRegister
.Negate
= 1;
333 else if( inst
->SrcReg
[i
].Negate
!= NEGATE_NONE
) {
334 if( inst
->SrcReg
[i
].Negate
& NEGATE_X
) {
335 fullsrc
->SrcRegisterExtSwz
.NegateX
= 1;
337 if( inst
->SrcReg
[i
].Negate
& NEGATE_Y
) {
338 fullsrc
->SrcRegisterExtSwz
.NegateY
= 1;
340 if( inst
->SrcReg
[i
].Negate
& NEGATE_Z
) {
341 fullsrc
->SrcRegisterExtSwz
.NegateZ
= 1;
343 if( inst
->SrcReg
[i
].Negate
& NEGATE_W
) {
344 fullsrc
->SrcRegisterExtSwz
.NegateW
= 1;
348 if( inst
->SrcReg
[i
].Abs
) {
349 fullsrc
->SrcRegisterExtMod
.Absolute
= 1;
352 if( inst
->SrcReg
[i
].RelAddr
) {
353 fullsrc
->SrcRegister
.Indirect
= 1;
355 fullsrc
->SrcRegisterInd
.File
= TGSI_FILE_ADDRESS
;
356 fullsrc
->SrcRegisterInd
.Index
= 0;
360 switch( inst
->Opcode
) {
362 fullinst
->Instruction
.Opcode
= TGSI_OPCODE_ARL
;
365 fullinst
->Instruction
.Opcode
= TGSI_OPCODE_ABS
;
368 fullinst
->Instruction
.Opcode
= TGSI_OPCODE_ADD
;
371 fullinst
->Instruction
.Opcode
= TGSI_OPCODE_BGNLOOP2
;
372 fullinst
->InstructionExtLabel
.Label
= inst
->BranchTarget
+ preamble_size
;
375 fullinst
->Instruction
.Opcode
= TGSI_OPCODE_BGNSUB
;
376 *insideSubroutine
= GL_TRUE
;
379 fullinst
->Instruction
.Opcode
= TGSI_OPCODE_BRA
;
382 fullinst
->Instruction
.Opcode
= TGSI_OPCODE_BRK
;
385 fullinst
->Instruction
.Opcode
= TGSI_OPCODE_CAL
;
386 fullinst
->InstructionExtLabel
.Label
= inst
->BranchTarget
+ preamble_size
;
389 fullinst
->Instruction
.Opcode
= TGSI_OPCODE_CMP
;
392 fullinst
->Instruction
.Opcode
= TGSI_OPCODE_CONT
;
395 fullinst
->Instruction
.Opcode
= TGSI_OPCODE_COS
;
398 fullinst
->Instruction
.Opcode
= TGSI_OPCODE_DDX
;
401 fullinst
->Instruction
.Opcode
= TGSI_OPCODE_DDY
;
404 fullinst
->Instruction
.Opcode
= TGSI_OPCODE_DP2
;
407 fullinst
->Instruction
.Opcode
= TGSI_OPCODE_DP2A
;
410 fullinst
->Instruction
.Opcode
= TGSI_OPCODE_DP3
;
413 fullinst
->Instruction
.Opcode
= TGSI_OPCODE_DP4
;
416 fullinst
->Instruction
.Opcode
= TGSI_OPCODE_DPH
;
419 fullinst
->Instruction
.Opcode
= TGSI_OPCODE_DST
;
422 fullinst
->Instruction
.Opcode
= TGSI_OPCODE_ELSE
;
423 fullinst
->InstructionExtLabel
.Label
= inst
->BranchTarget
+ preamble_size
;
426 fullinst
->Instruction
.Opcode
= TGSI_OPCODE_ENDIF
;
429 fullinst
->Instruction
.Opcode
= TGSI_OPCODE_ENDLOOP2
;
430 fullinst
->InstructionExtLabel
.Label
= inst
->BranchTarget
+ preamble_size
;
433 fullinst
->Instruction
.Opcode
= TGSI_OPCODE_ENDSUB
;
434 *insideSubroutine
= GL_FALSE
;
437 fullinst
->Instruction
.Opcode
= TGSI_OPCODE_EX2
;
440 fullinst
->Instruction
.Opcode
= TGSI_OPCODE_EXP
;
443 fullinst
->Instruction
.Opcode
= TGSI_OPCODE_FLR
;
446 fullinst
->Instruction
.Opcode
= TGSI_OPCODE_FRC
;
449 fullinst
->Instruction
.Opcode
= TGSI_OPCODE_IF
;
450 fullinst
->InstructionExtLabel
.Label
= inst
->BranchTarget
+ preamble_size
;
453 fullinst
->Instruction
.Opcode
= TGSI_OPCODE_TRUNC
;
457 fullinst
->Instruction
.Opcode
= TGSI_OPCODE_KIL
;
461 assert(inst
->DstReg
.CondMask
== COND_TR
);
462 fullinst
->Instruction
.Opcode
= TGSI_OPCODE_KILP
;
465 fullinst
->Instruction
.Opcode
= TGSI_OPCODE_LG2
;
468 fullinst
->Instruction
.Opcode
= TGSI_OPCODE_LOG
;
471 fullinst
->Instruction
.Opcode
= TGSI_OPCODE_LIT
;
474 fullinst
->Instruction
.Opcode
= TGSI_OPCODE_LRP
;
477 fullinst
->Instruction
.Opcode
= TGSI_OPCODE_MAD
;
480 fullinst
->Instruction
.Opcode
= TGSI_OPCODE_MAX
;
483 fullinst
->Instruction
.Opcode
= TGSI_OPCODE_MIN
;
486 fullinst
->Instruction
.Opcode
= TGSI_OPCODE_MOV
;
489 fullinst
->Instruction
.Opcode
= TGSI_OPCODE_MUL
;
492 fullinst
->Instruction
.Opcode
= TGSI_OPCODE_NOISE1
;
495 fullinst
->Instruction
.Opcode
= TGSI_OPCODE_NOISE2
;
498 fullinst
->Instruction
.Opcode
= TGSI_OPCODE_NOISE3
;
501 fullinst
->Instruction
.Opcode
= TGSI_OPCODE_NOISE4
;
504 fullinst
->Instruction
.Opcode
= TGSI_OPCODE_NOP
;
507 fullinst
->Instruction
.Opcode
= TGSI_OPCODE_NRM
;
510 fullinst
->Instruction
.Opcode
= TGSI_OPCODE_NRM4
;
513 fullinst
->Instruction
.Opcode
= TGSI_OPCODE_POW
;
516 fullinst
->Instruction
.Opcode
= TGSI_OPCODE_RCP
;
519 /* If RET is used inside main (not a real subroutine) we may want
520 * to execute END instead of RET. TBD...
522 if (1 /* *insideSubroutine */) {
523 fullinst
->Instruction
.Opcode
= TGSI_OPCODE_RET
;
526 /* inside main() pseudo-function */
527 fullinst
->Instruction
.Opcode
= TGSI_OPCODE_END
;
531 fullinst
->Instruction
.Opcode
= TGSI_OPCODE_RSQ
;
534 fullinst
->Instruction
.Opcode
= TGSI_OPCODE_SCS
;
535 fulldst
->DstRegister
.WriteMask
&= TGSI_WRITEMASK_XY
;
538 fullinst
->Instruction
.Opcode
= TGSI_OPCODE_SEQ
;
541 fullinst
->Instruction
.Opcode
= TGSI_OPCODE_SGE
;
544 fullinst
->Instruction
.Opcode
= TGSI_OPCODE_SGT
;
547 fullinst
->Instruction
.Opcode
= TGSI_OPCODE_SIN
;
550 fullinst
->Instruction
.Opcode
= TGSI_OPCODE_SLE
;
553 fullinst
->Instruction
.Opcode
= TGSI_OPCODE_SLT
;
556 fullinst
->Instruction
.Opcode
= TGSI_OPCODE_SNE
;
559 fullinst
->Instruction
.Opcode
= TGSI_OPCODE_SSG
;
562 fullinst
->Instruction
.Opcode
= TGSI_OPCODE_SUB
;
565 fullinst
->Instruction
.Opcode
= TGSI_OPCODE_SWZ
;
568 /* ordinary texture lookup */
569 fullinst
->Instruction
.Opcode
= TGSI_OPCODE_TEX
;
570 fullinst
->Instruction
.NumSrcRegs
= 2;
571 fullinst
->InstructionExtTexture
.Texture
=
572 map_texture_target( inst
->TexSrcTarget
, inst
->TexShadow
);
573 fullinst
->FullSrcRegisters
[1].SrcRegister
.File
= TGSI_FILE_SAMPLER
;
574 fullinst
->FullSrcRegisters
[1].SrcRegister
.Index
= inst
->TexSrcUnit
;
577 /* texture lookup with LOD bias */
578 fullinst
->Instruction
.Opcode
= TGSI_OPCODE_TXB
;
579 fullinst
->Instruction
.NumSrcRegs
= 2;
580 fullinst
->InstructionExtTexture
.Texture
=
581 map_texture_target( inst
->TexSrcTarget
, inst
->TexShadow
);
582 fullinst
->FullSrcRegisters
[1].SrcRegister
.File
= TGSI_FILE_SAMPLER
;
583 fullinst
->FullSrcRegisters
[1].SrcRegister
.Index
= inst
->TexSrcUnit
;
586 /* texture lookup with explicit partial derivatives */
587 fullinst
->Instruction
.Opcode
= TGSI_OPCODE_TXD
;
588 fullinst
->Instruction
.NumSrcRegs
= 4;
589 fullinst
->InstructionExtTexture
.Texture
=
590 map_texture_target( inst
->TexSrcTarget
, inst
->TexShadow
);
591 /* src[0] = coord, src[1] = d[strq]/dx, src[2] = d[strq]/dy */
592 fullinst
->FullSrcRegisters
[3].SrcRegister
.File
= TGSI_FILE_SAMPLER
;
593 fullinst
->FullSrcRegisters
[3].SrcRegister
.Index
= inst
->TexSrcUnit
;
596 /* texture lookup with explicit LOD */
597 fullinst
->Instruction
.Opcode
= TGSI_OPCODE_TXL
;
598 fullinst
->Instruction
.NumSrcRegs
= 2;
599 fullinst
->InstructionExtTexture
.Texture
=
600 map_texture_target( inst
->TexSrcTarget
, inst
->TexShadow
);
601 fullinst
->FullSrcRegisters
[1].SrcRegister
.File
= TGSI_FILE_SAMPLER
;
602 fullinst
->FullSrcRegisters
[1].SrcRegister
.Index
= inst
->TexSrcUnit
;
605 /* texture lookup with divide by Q component */
606 /* convert to TEX w/ special flag for division */
607 fullinst
->Instruction
.Opcode
= TGSI_OPCODE_TXP
;
608 fullinst
->Instruction
.NumSrcRegs
= 2;
609 fullinst
->InstructionExtTexture
.Texture
=
610 map_texture_target( inst
->TexSrcTarget
, inst
->TexShadow
);
611 fullinst
->FullSrcRegisters
[1].SrcRegister
.File
= TGSI_FILE_SAMPLER
;
612 fullinst
->FullSrcRegisters
[1].SrcRegister
.Index
= inst
->TexSrcUnit
;
615 fullinst
->Instruction
.Opcode
= TGSI_OPCODE_XPD
;
616 fulldst
->DstRegister
.WriteMask
&= TGSI_WRITEMASK_XYZ
;
619 fullinst
->Instruction
.Opcode
= TGSI_OPCODE_END
;
627 * \param usage_mask bitfield of TGSI_WRITEMASK_{XYZW} tokens
629 static struct tgsi_full_declaration
632 GLboolean interpolate_info
,
635 GLboolean semantic_info
,
636 GLuint semantic_name
,
637 GLbitfield semantic_index
,
638 GLbitfield input_flags
)
640 struct tgsi_full_declaration decl
;
642 assert(semantic_name
< TGSI_SEMANTIC_COUNT
);
644 decl
= tgsi_default_full_declaration();
645 decl
.Declaration
.File
= TGSI_FILE_INPUT
;
646 decl
.Declaration
.UsageMask
= usage_mask
;
647 decl
.Declaration
.Semantic
= semantic_info
;
648 decl
.DeclarationRange
.First
= index
;
649 decl
.DeclarationRange
.Last
= index
;
651 decl
.Semantic
.SemanticName
= semantic_name
;
652 decl
.Semantic
.SemanticIndex
= semantic_index
;
654 if (interpolate_info
) {
655 decl
.Declaration
.Interpolate
= interpolate
;
657 if (input_flags
& PROG_PARAM_BIT_CENTROID
)
658 decl
.Declaration
.Centroid
= 1;
659 if (input_flags
& PROG_PARAM_BIT_INVARIANT
)
660 decl
.Declaration
.Invariant
= 1;
666 * \param usage_mask bitfield of TGSI_WRITEMASK_{XYZW} tokens
668 static struct tgsi_full_declaration
671 GLuint semantic_name
,
672 GLuint semantic_index
,
674 GLbitfield output_flags
)
676 struct tgsi_full_declaration decl
;
678 assert(semantic_name
< TGSI_SEMANTIC_COUNT
);
680 decl
= tgsi_default_full_declaration();
681 decl
.Declaration
.File
= TGSI_FILE_OUTPUT
;
682 decl
.Declaration
.UsageMask
= usage_mask
;
683 decl
.Declaration
.Semantic
= 1;
684 decl
.DeclarationRange
.First
= index
;
685 decl
.DeclarationRange
.Last
= index
;
686 decl
.Semantic
.SemanticName
= semantic_name
;
687 decl
.Semantic
.SemanticIndex
= semantic_index
;
688 if (output_flags
& PROG_PARAM_BIT_CENTROID
)
689 decl
.Declaration
.Centroid
= 1;
690 if (output_flags
& PROG_PARAM_BIT_INVARIANT
)
691 decl
.Declaration
.Invariant
= 1;
697 static struct tgsi_full_declaration
702 struct tgsi_full_declaration decl
;
703 decl
= tgsi_default_full_declaration();
704 decl
.Declaration
.File
= TGSI_FILE_TEMPORARY
;
705 decl
.DeclarationRange
.First
= start_index
;
706 decl
.DeclarationRange
.Last
= end_index
;
710 static struct tgsi_full_declaration
715 struct tgsi_full_declaration decl
;
717 decl
= tgsi_default_full_declaration();
718 decl
.Declaration
.File
= TGSI_FILE_ADDRESS
;
719 decl
.DeclarationRange
.First
= start_index
;
720 decl
.DeclarationRange
.Last
= end_index
;
724 static struct tgsi_full_declaration
725 make_sampler_decl(GLuint index
)
727 struct tgsi_full_declaration decl
;
728 decl
= tgsi_default_full_declaration();
729 decl
.Declaration
.File
= TGSI_FILE_SAMPLER
;
730 decl
.DeclarationRange
.First
= index
;
731 decl
.DeclarationRange
.Last
= index
;
735 /** Reference into a constant buffer */
736 static struct tgsi_full_declaration
737 make_constant_decl(GLuint first
, GLuint last
)
739 struct tgsi_full_declaration decl
;
740 decl
= tgsi_default_full_declaration();
741 decl
.Declaration
.File
= TGSI_FILE_CONSTANT
;
742 decl
.DeclarationRange
.First
= first
;
743 decl
.DeclarationRange
.Last
= last
;
750 * Find the temporaries which are used in the given program.
753 find_temporaries(const struct gl_program
*program
,
754 GLboolean tempsUsed
[MAX_PROGRAM_TEMPS
])
758 for (i
= 0; i
< MAX_PROGRAM_TEMPS
; i
++)
759 tempsUsed
[i
] = GL_FALSE
;
761 for (i
= 0; i
< program
->NumInstructions
; i
++) {
762 const struct prog_instruction
*inst
= program
->Instructions
+ i
;
763 const GLuint n
= _mesa_num_inst_src_regs( inst
->Opcode
);
764 for (j
= 0; j
< n
; j
++) {
765 if (inst
->SrcReg
[j
].File
== PROGRAM_TEMPORARY
)
766 tempsUsed
[inst
->SrcReg
[j
].Index
] = GL_TRUE
;
767 if (inst
->DstReg
.File
== PROGRAM_TEMPORARY
)
768 tempsUsed
[inst
->DstReg
.Index
] = GL_TRUE
;
775 * Find an unused temporary in the tempsUsed array.
778 find_free_temporary(GLboolean tempsUsed
[MAX_PROGRAM_TEMPS
])
781 for (i
= 0; i
< MAX_PROGRAM_TEMPS
; i
++) {
783 tempsUsed
[i
] = GL_TRUE
;
791 /** helper for building simple TGSI instruction, one src register */
793 build_tgsi_instruction1(struct tgsi_full_instruction
*inst
,
795 int dstFile
, int dstIndex
, int writemask
,
796 int srcFile1
, int srcIndex1
)
798 *inst
= tgsi_default_full_instruction();
800 inst
->Instruction
.Opcode
= opcode
;
802 inst
->Instruction
.NumDstRegs
= 1;
803 inst
->FullDstRegisters
[0].DstRegister
.File
= dstFile
;
804 inst
->FullDstRegisters
[0].DstRegister
.Index
= dstIndex
;
805 inst
->FullDstRegisters
[0].DstRegister
.WriteMask
= writemask
;
807 inst
->Instruction
.NumSrcRegs
= 1;
808 inst
->FullSrcRegisters
[0].SrcRegister
.File
= srcFile1
;
809 inst
->FullSrcRegisters
[0].SrcRegister
.Index
= srcIndex1
;
813 /** helper for building simple TGSI instruction, two src registers */
815 build_tgsi_instruction2(struct tgsi_full_instruction
*inst
,
817 int dstFile
, int dstIndex
, int writemask
,
818 int srcFile1
, int srcIndex1
,
819 int srcFile2
, int srcIndex2
)
821 *inst
= tgsi_default_full_instruction();
823 inst
->Instruction
.Opcode
= opcode
;
825 inst
->Instruction
.NumDstRegs
= 1;
826 inst
->FullDstRegisters
[0].DstRegister
.File
= dstFile
;
827 inst
->FullDstRegisters
[0].DstRegister
.Index
= dstIndex
;
828 inst
->FullDstRegisters
[0].DstRegister
.WriteMask
= writemask
;
830 inst
->Instruction
.NumSrcRegs
= 2;
831 inst
->FullSrcRegisters
[0].SrcRegister
.File
= srcFile1
;
832 inst
->FullSrcRegisters
[0].SrcRegister
.Index
= srcIndex1
;
833 inst
->FullSrcRegisters
[1].SrcRegister
.File
= srcFile2
;
834 inst
->FullSrcRegisters
[1].SrcRegister
.Index
= srcIndex2
;
840 * Emit the TGSI instructions for inverting the WPOS y coordinate.
843 emit_inverted_wpos(struct tgsi_token
*tokens
,
847 struct tgsi_header
*header
, int maxTokens
)
849 struct tgsi_full_instruction fullinst
;
852 /* MOV wpos_temp.xzw, input[wpos]; */
853 build_tgsi_instruction1(&fullinst
,
855 TGSI_FILE_TEMPORARY
, wpos_temp
, WRITEMASK_XZW
,
858 ti
+= tgsi_build_full_instruction(&fullinst
,
863 /* SUB wpos_temp.y, const[winsize_const] - input[wpos_input]; */
864 build_tgsi_instruction2(&fullinst
,
866 TGSI_FILE_TEMPORARY
, wpos_temp
, WRITEMASK_Y
,
867 TGSI_FILE_CONSTANT
, winsize_const
,
868 TGSI_FILE_INPUT
, wpos_input
);
870 ti
+= tgsi_build_full_instruction(&fullinst
,
882 * Translate Mesa program to TGSI format.
883 * \param program the program to translate
884 * \param numInputs number of input registers used
885 * \param inputMapping maps Mesa fragment program inputs to TGSI generic
887 * \param inputSemanticName the TGSI_SEMANTIC flag for each input
888 * \param inputSemanticIndex the semantic index (ex: which texcoord) for each input
889 * \param interpMode the TGSI_INTERPOLATE_LINEAR/PERSP mode for each input
891 * \param numOutputs number of output registers used
892 * \param outputMapping maps Mesa fragment program outputs to TGSI
894 * \param outputSemanticName the TGSI_SEMANTIC flag for each output
895 * \param outputSemanticIndex the semantic index (ex: which texcoord) for each output
896 * \param tokens array to store translated tokens in
897 * \param maxTokens size of the tokens array
899 * \return number of tokens placed in 'tokens' buffer, or zero if error
902 st_translate_mesa_program(
905 const struct gl_program
*program
,
907 const GLuint inputMapping
[],
908 const ubyte inputSemanticName
[],
909 const ubyte inputSemanticIndex
[],
910 const GLuint interpMode
[],
911 const GLbitfield inputFlags
[],
913 const GLuint outputMapping
[],
914 const ubyte outputSemanticName
[],
915 const ubyte outputSemanticIndex
[],
916 const GLbitfield outputFlags
[],
917 struct tgsi_token
*tokens
,
921 GLuint ti
; /* token index */
922 struct tgsi_header
*header
;
923 struct tgsi_processor
*processor
;
924 GLuint preamble_size
= 0;
925 GLuint immediates
[1000];
926 GLuint numImmediates
= 0;
927 GLboolean insideSubroutine
= GL_FALSE
;
928 GLboolean indirectAccess
= GL_FALSE
;
929 GLboolean tempsUsed
[MAX_PROGRAM_TEMPS
+ 1];
930 GLint wposTemp
= -1, winHeightConst
= -1;
932 assert(procType
== TGSI_PROCESSOR_FRAGMENT
||
933 procType
== TGSI_PROCESSOR_VERTEX
);
935 find_temporaries(program
, tempsUsed
);
937 if (procType
== TGSI_PROCESSOR_FRAGMENT
) {
938 if (program
->InputsRead
& FRAG_BIT_WPOS
) {
939 /* Fragment program uses fragment position input.
940 * Need to replace instances of INPUT[WPOS] with temp T
941 * where T = INPUT[WPOS] by y is inverted.
943 static const gl_state_index winSizeState
[STATE_LENGTH
]
944 = { STATE_INTERNAL
, STATE_FB_SIZE
, 0, 0, 0 };
945 winHeightConst
= _mesa_add_state_reference(program
->Parameters
,
947 wposTemp
= find_free_temporary(tempsUsed
);
952 *(struct tgsi_version
*) &tokens
[0] = tgsi_build_version();
954 header
= (struct tgsi_header
*) &tokens
[1];
955 *header
= tgsi_build_header();
957 processor
= (struct tgsi_processor
*) &tokens
[2];
958 *processor
= tgsi_build_processor( procType
, header
);
963 * Declare input attributes.
965 if (procType
== TGSI_PROCESSOR_FRAGMENT
) {
966 for (i
= 0; i
< numInputs
; i
++) {
967 struct tgsi_full_declaration fulldecl
;
968 fulldecl
= make_input_decl(i
,
969 GL_TRUE
, interpMode
[i
],
971 GL_TRUE
, inputSemanticName
[i
],
972 inputSemanticIndex
[i
],
974 ti
+= tgsi_build_full_declaration(&fulldecl
,
982 /* XXX: this could probaby be merged with the clause above.
983 * the only difference is the semantic tags.
985 for (i
= 0; i
< numInputs
; i
++) {
986 struct tgsi_full_declaration fulldecl
;
987 fulldecl
= make_input_decl(i
,
992 ti
+= tgsi_build_full_declaration(&fulldecl
,
1000 * Declare output attributes.
1002 if (procType
== TGSI_PROCESSOR_FRAGMENT
) {
1003 for (i
= 0; i
< numOutputs
; i
++) {
1004 struct tgsi_full_declaration fulldecl
;
1005 switch (outputSemanticName
[i
]) {
1006 case TGSI_SEMANTIC_POSITION
:
1007 fulldecl
= make_output_decl(i
,
1008 TGSI_SEMANTIC_POSITION
, /* Z / Depth */
1009 outputSemanticIndex
[i
],
1013 case TGSI_SEMANTIC_COLOR
:
1014 fulldecl
= make_output_decl(i
,
1015 TGSI_SEMANTIC_COLOR
,
1016 outputSemanticIndex
[i
],
1017 TGSI_WRITEMASK_XYZW
,
1024 ti
+= tgsi_build_full_declaration(&fulldecl
,
1032 for (i
= 0; i
< numOutputs
; i
++) {
1033 struct tgsi_full_declaration fulldecl
;
1034 fulldecl
= make_output_decl(i
,
1035 outputSemanticName
[i
],
1036 outputSemanticIndex
[i
],
1037 TGSI_WRITEMASK_XYZW
,
1039 ti
+= tgsi_build_full_declaration(&fulldecl
,
1046 /* temporary decls */
1048 GLboolean inside_range
= GL_FALSE
;
1049 GLuint start_range
= 0;
1051 tempsUsed
[MAX_PROGRAM_TEMPS
] = GL_FALSE
;
1052 for (i
= 0; i
< MAX_PROGRAM_TEMPS
+ 1; i
++) {
1053 if (tempsUsed
[i
] && !inside_range
) {
1054 inside_range
= GL_TRUE
;
1057 else if (!tempsUsed
[i
] && inside_range
) {
1058 struct tgsi_full_declaration fulldecl
;
1060 inside_range
= GL_FALSE
;
1061 fulldecl
= make_temp_decl( start_range
, i
- 1 );
1062 ti
+= tgsi_build_full_declaration(
1071 /* Declare address register.
1073 if (program
->NumAddressRegs
> 0) {
1074 struct tgsi_full_declaration fulldecl
;
1076 assert( program
->NumAddressRegs
== 1 );
1078 fulldecl
= make_addr_decl( 0, 0 );
1079 ti
+= tgsi_build_full_declaration(
1085 indirectAccess
= GL_TRUE
;
1088 /* immediates/literals */
1089 memset(immediates
, ~0, sizeof(immediates
));
1091 /* Emit immediates only when there is no address register in use.
1092 * FIXME: Be smarter and recognize param arrays -- indirect addressing is
1093 * only valid within the referenced array.
1095 if (program
->Parameters
&& !indirectAccess
) {
1096 for (i
= 0; i
< program
->Parameters
->NumParameters
; i
++) {
1097 if (program
->Parameters
->Parameters
[i
].Type
== PROGRAM_CONSTANT
) {
1098 struct tgsi_full_immediate fullimm
;
1100 fullimm
= make_immediate( program
->Parameters
->ParameterValues
[i
], 4 );
1101 ti
+= tgsi_build_full_immediate(
1106 immediates
[i
] = numImmediates
;
1112 /* constant buffer refs */
1113 if (program
->Parameters
) {
1114 GLint start
= -1, end
= -1;
1116 for (i
= 0; i
< program
->Parameters
->NumParameters
; i
++) {
1117 GLboolean emit
= (i
== program
->Parameters
->NumParameters
- 1);
1120 switch (program
->Parameters
->Parameters
[i
].Type
) {
1121 case PROGRAM_ENV_PARAM
:
1122 case PROGRAM_STATE_VAR
:
1123 case PROGRAM_NAMED_PARAM
:
1124 case PROGRAM_UNIFORM
:
1127 case PROGRAM_CONSTANT
:
1128 matches
= indirectAccess
;
1136 /* begin a sequence */
1141 /* continue sequence */
1147 /* end of sequence */
1152 if (emit
&& start
>= 0) {
1153 struct tgsi_full_declaration fulldecl
;
1155 fulldecl
= make_constant_decl( start
, end
);
1156 ti
+= tgsi_build_full_declaration(
1166 /* texture samplers */
1167 for (i
= 0; i
< ctx
->Const
.MaxTextureImageUnits
; i
++) {
1168 if (program
->SamplersUsed
& (1 << i
)) {
1169 struct tgsi_full_declaration fulldecl
;
1171 fulldecl
= make_sampler_decl( i
);
1172 ti
+= tgsi_build_full_declaration(
1180 /* invert WPOS fragment input */
1181 if (wposTemp
>= 0) {
1182 ti
+= emit_inverted_wpos(&tokens
[ti
], wposTemp
, winHeightConst
,
1183 inputMapping
[FRAG_ATTRIB_WPOS
],
1184 header
, maxTokens
- ti
);
1185 preamble_size
= 2; /* two instructions added */
1188 for (i
= 0; i
< program
->NumInstructions
; i
++) {
1189 struct tgsi_full_instruction fullinst
;
1191 compile_instruction(
1192 &program
->Instructions
[i
],
1203 ti
+= tgsi_build_full_instruction(
1211 if(!tgsi_sanity_check(tokens
)) {
1212 debug_printf("Due to sanity check failure(s) above the following shader program is invalid:\n");
1213 debug_printf("\nOriginal program:\n%s", program
->String
);
1214 debug_printf("\nMesa program:\n");
1215 _mesa_print_program(program
);
1216 debug_printf("\nTGSI program:\n");
1217 tgsi_dump(tokens
, 0);