1 /**************************************************************************
3 * Copyright 2008 Tungsten Graphics, Inc., Cedar Park, Texas.
5 * Copyright 2008 VMware, Inc. All rights Reserved.
7 * Permission is hereby granted, free of charge, to any person obtaining a
8 * copy of this software and associated documentation files (the
9 * "Software"), to deal in the Software without restriction, including
10 * without limitation the rights to use, copy, modify, merge, publish,
11 * distribute, sub license, and/or sell copies of the Software, and to
12 * permit persons to whom the Software is furnished to do so, subject to
13 * the following conditions:
15 * The above copyright notice and this permission notice (including the
16 * next paragraph) shall be included in all copies or substantial portions
19 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
20 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
21 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT.
22 * IN NO EVENT SHALL TUNGSTEN GRAPHICS AND/OR ITS SUPPLIERS BE LIABLE FOR
23 * ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
24 * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
25 * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
27 **************************************************************************/
30 * TGSI program scan utility.
31 * Used to determine which registers and instructions are used by a shader.
37 #include "util/u_math.h"
38 #include "tgsi/tgsi_build.h"
39 #include "tgsi/tgsi_parse.h"
40 #include "tgsi/tgsi_scan.h"
46 * Scan the given TGSI shader to collect information such as number of
47 * registers used, special instructions used, etc.
48 * \return info the result of the scan
51 tgsi_scan_shader(const struct tgsi_token
*tokens
,
52 struct tgsi_shader_info
*info
)
55 struct tgsi_parse_context parse
;
57 memset(info
, 0, sizeof(*info
));
58 for (i
= 0; i
< TGSI_FILE_COUNT
; i
++)
59 info
->file_max
[i
] = -1;
62 ** Setup to begin parsing input shader
64 if (tgsi_parse_init( &parse
, tokens
) != TGSI_PARSE_OK
) {
65 debug_printf("tgsi_parse_init() failed in tgsi_scan_shader()!\n");
68 procType
= parse
.FullHeader
.Processor
.Processor
;
69 assert(procType
== TGSI_PROCESSOR_FRAGMENT
||
70 procType
== TGSI_PROCESSOR_VERTEX
||
71 procType
== TGSI_PROCESSOR_GEOMETRY
);
75 ** Loop over incoming program tokens/instructions
77 while( !tgsi_parse_end_of_tokens( &parse
) ) {
81 tgsi_parse_token( &parse
);
83 switch( parse
.FullToken
.Token
.Type
) {
84 case TGSI_TOKEN_TYPE_INSTRUCTION
:
86 const struct tgsi_full_instruction
*fullinst
87 = &parse
.FullToken
.FullInstruction
;
89 assert(fullinst
->Instruction
.Opcode
< TGSI_OPCODE_LAST
);
90 info
->opcode_count
[fullinst
->Instruction
.Opcode
]++;
92 /* special case: scan fragment shaders for use of the fog
93 * input/attribute. The X component is fog, the Y component
94 * is the front/back-face flag.
96 if (procType
== TGSI_PROCESSOR_FRAGMENT
) {
98 for (i
= 0; i
< fullinst
->Instruction
.NumSrcRegs
; i
++) {
99 const struct tgsi_full_src_register
*src
=
100 &fullinst
->FullSrcRegisters
[i
];
101 if (src
->SrcRegister
.File
== TGSI_FILE_INPUT
) {
102 const int ind
= src
->SrcRegister
.Index
;
103 if (info
->input_semantic_name
[ind
] == TGSI_SEMANTIC_FOG
) {
104 if (src
->SrcRegister
.SwizzleX
== TGSI_SWIZZLE_X
) {
105 info
->uses_fogcoord
= TRUE
;
107 else if (src
->SrcRegister
.SwizzleX
== TGSI_SWIZZLE_Y
) {
108 info
->uses_frontfacing
= TRUE
;
117 case TGSI_TOKEN_TYPE_DECLARATION
:
119 const struct tgsi_full_declaration
*fulldecl
120 = &parse
.FullToken
.FullDeclaration
;
121 const uint file
= fulldecl
->Declaration
.File
;
123 for (reg
= fulldecl
->DeclarationRange
.First
;
124 reg
<= fulldecl
->DeclarationRange
.Last
;
127 /* only first 32 regs will appear in this bitfield */
128 info
->file_mask
[file
] |= (1 << reg
);
129 info
->file_count
[file
]++;
130 info
->file_max
[file
] = MAX2(info
->file_max
[file
], (int)reg
);
132 if (file
== TGSI_FILE_INPUT
) {
133 info
->input_semantic_name
[reg
] = (ubyte
)fulldecl
->Semantic
.SemanticName
;
134 info
->input_semantic_index
[reg
] = (ubyte
)fulldecl
->Semantic
.SemanticIndex
;
135 info
->input_interpolate
[reg
] = (ubyte
)fulldecl
->Declaration
.Interpolate
;
138 else if (file
== TGSI_FILE_OUTPUT
) {
139 info
->output_semantic_name
[reg
] = (ubyte
)fulldecl
->Semantic
.SemanticName
;
140 info
->output_semantic_index
[reg
] = (ubyte
)fulldecl
->Semantic
.SemanticIndex
;
145 if (procType
== TGSI_PROCESSOR_FRAGMENT
&&
146 file
== TGSI_FILE_OUTPUT
&&
147 fulldecl
->Semantic
.SemanticName
== TGSI_SEMANTIC_POSITION
) {
148 info
->writes_z
= TRUE
;
154 case TGSI_TOKEN_TYPE_IMMEDIATE
:
156 uint reg
= info
->immediate_count
++;
157 uint file
= TGSI_FILE_IMMEDIATE
;
159 info
->file_mask
[file
] |= (1 << reg
);
160 info
->file_count
[file
]++;
161 info
->file_max
[file
] = MAX2(info
->file_max
[file
], (int)reg
);
170 info
->uses_kill
= (info
->opcode_count
[TGSI_OPCODE_KIL
] ||
171 info
->opcode_count
[TGSI_OPCODE_KILP
]);
173 tgsi_parse_free (&parse
);
179 * Check if the given shader is a "passthrough" shader consisting of only
180 * MOV instructions of the form: MOV OUT[n], IN[n]
184 tgsi_is_passthrough_shader(const struct tgsi_token
*tokens
)
186 struct tgsi_parse_context parse
;
189 ** Setup to begin parsing input shader
191 if (tgsi_parse_init(&parse
, tokens
) != TGSI_PARSE_OK
) {
192 debug_printf("tgsi_parse_init() failed in tgsi_is_passthrough_shader()!\n");
197 ** Loop over incoming program tokens/instructions
199 while (!tgsi_parse_end_of_tokens(&parse
)) {
201 tgsi_parse_token(&parse
);
203 switch (parse
.FullToken
.Token
.Type
) {
204 case TGSI_TOKEN_TYPE_INSTRUCTION
:
206 struct tgsi_full_instruction
*fullinst
=
207 &parse
.FullToken
.FullInstruction
;
208 const struct tgsi_full_src_register
*src
=
209 &fullinst
->FullSrcRegisters
[0];
210 const struct tgsi_full_dst_register
*dst
=
211 &fullinst
->FullDstRegisters
[0];
213 /* Do a whole bunch of checks for a simple move */
214 if (fullinst
->Instruction
.Opcode
!= TGSI_OPCODE_MOV
||
215 src
->SrcRegister
.File
!= TGSI_FILE_INPUT
||
216 dst
->DstRegister
.File
!= TGSI_FILE_OUTPUT
||
217 src
->SrcRegister
.Index
!= dst
->DstRegister
.Index
||
219 src
->SrcRegister
.Negate
||
220 src
->SrcRegisterExtMod
.Negate
||
221 src
->SrcRegisterExtMod
.Absolute
||
222 src
->SrcRegisterExtMod
.Scale2X
||
223 src
->SrcRegisterExtMod
.Bias
||
224 src
->SrcRegisterExtMod
.Complement
||
226 src
->SrcRegister
.SwizzleX
!= TGSI_SWIZZLE_X
||
227 src
->SrcRegister
.SwizzleY
!= TGSI_SWIZZLE_Y
||
228 src
->SrcRegister
.SwizzleZ
!= TGSI_SWIZZLE_Z
||
229 src
->SrcRegister
.SwizzleW
!= TGSI_SWIZZLE_W
||
231 src
->SrcRegisterExtSwz
.ExtSwizzleX
!= TGSI_EXTSWIZZLE_X
||
232 src
->SrcRegisterExtSwz
.ExtSwizzleY
!= TGSI_EXTSWIZZLE_Y
||
233 src
->SrcRegisterExtSwz
.ExtSwizzleZ
!= TGSI_EXTSWIZZLE_Z
||
234 src
->SrcRegisterExtSwz
.ExtSwizzleW
!= TGSI_EXTSWIZZLE_W
||
236 dst
->DstRegister
.WriteMask
!= TGSI_WRITEMASK_XYZW
)
238 tgsi_parse_free(&parse
);
244 case TGSI_TOKEN_TYPE_DECLARATION
:
246 case TGSI_TOKEN_TYPE_IMMEDIATE
:
253 tgsi_parse_free(&parse
);
255 /* if we get here, it's a pass-through shader */