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_parse.h"
39 #include "tgsi/tgsi_util.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
;
90 assert(fullinst
->Instruction
.Opcode
< TGSI_OPCODE_LAST
);
91 info
->opcode_count
[fullinst
->Instruction
.Opcode
]++;
93 for (i
= 0; i
< fullinst
->Instruction
.NumSrcRegs
; i
++) {
94 const struct tgsi_full_src_register
*src
=
96 int ind
= src
->Register
.Index
;
98 /* Mark which inputs are effectively used */
99 if (src
->Register
.File
== TGSI_FILE_INPUT
) {
101 usage_mask
= tgsi_util_get_inst_usage_mask(fullinst
, i
);
102 if (src
->Register
.Indirect
) {
103 for (ind
= 0; ind
< info
->num_inputs
; ++ind
) {
104 info
->input_usage_mask
[ind
] |= usage_mask
;
108 assert(ind
< PIPE_MAX_SHADER_INPUTS
);
109 info
->input_usage_mask
[ind
] |= usage_mask
;
112 if (procType
== TGSI_PROCESSOR_FRAGMENT
&&
113 src
->Register
.File
== TGSI_FILE_INPUT
&&
114 info
->reads_position
&&
115 src
->Register
.Index
== 0 &&
116 (src
->Register
.SwizzleX
== TGSI_SWIZZLE_Z
||
117 src
->Register
.SwizzleY
== TGSI_SWIZZLE_Z
||
118 src
->Register
.SwizzleZ
== TGSI_SWIZZLE_Z
||
119 src
->Register
.SwizzleW
== TGSI_SWIZZLE_Z
)) {
120 info
->reads_z
= TRUE
;
124 /* check for indirect register reads */
125 if (src
->Register
.Indirect
) {
126 info
->indirect_files
|= (1 << src
->Register
.File
);
130 /* check for indirect register writes */
131 for (i
= 0; i
< fullinst
->Instruction
.NumDstRegs
; i
++) {
132 const struct tgsi_full_dst_register
*dst
= &fullinst
->Dst
[i
];
133 if (dst
->Register
.Indirect
) {
134 info
->indirect_files
|= (1 << dst
->Register
.File
);
138 info
->num_instructions
++;
142 case TGSI_TOKEN_TYPE_DECLARATION
:
144 const struct tgsi_full_declaration
*fulldecl
145 = &parse
.FullToken
.FullDeclaration
;
146 const uint file
= fulldecl
->Declaration
.File
;
148 for (reg
= fulldecl
->Range
.First
;
149 reg
<= fulldecl
->Range
.Last
;
152 /* only first 32 regs will appear in this bitfield */
153 info
->file_mask
[file
] |= (1 << reg
);
154 info
->file_count
[file
]++;
155 info
->file_max
[file
] = MAX2(info
->file_max
[file
], (int)reg
);
157 if (file
== TGSI_FILE_INPUT
) {
158 info
->input_semantic_name
[reg
] = (ubyte
)fulldecl
->Semantic
.Name
;
159 info
->input_semantic_index
[reg
] = (ubyte
)fulldecl
->Semantic
.Index
;
160 info
->input_interpolate
[reg
] = (ubyte
)fulldecl
->Declaration
.Interpolate
;
161 info
->input_centroid
[reg
] = (ubyte
)fulldecl
->Declaration
.Centroid
;
162 info
->input_cylindrical_wrap
[reg
] = (ubyte
)fulldecl
->Declaration
.CylindricalWrap
;
165 if (procType
== TGSI_PROCESSOR_FRAGMENT
&&
166 fulldecl
->Semantic
.Name
== TGSI_SEMANTIC_POSITION
)
167 info
->reads_position
= TRUE
;
169 else if (file
== TGSI_FILE_SYSTEM_VALUE
) {
170 unsigned index
= fulldecl
->Range
.First
;
171 unsigned semName
= fulldecl
->Semantic
.Name
;
173 info
->system_value_semantic_name
[index
] = semName
;
174 info
->num_system_values
= MAX2(info
->num_system_values
,
178 info->system_value_semantic_name[info->num_system_values++] =
179 fulldecl->Semantic.Name;
182 if (fulldecl
->Semantic
.Name
== TGSI_SEMANTIC_INSTANCEID
) {
183 info
->uses_instanceid
= TRUE
;
185 else if (fulldecl
->Semantic
.Name
== TGSI_SEMANTIC_VERTEXID
) {
186 info
->uses_vertexid
= TRUE
;
189 else if (file
== TGSI_FILE_OUTPUT
) {
190 info
->output_semantic_name
[reg
] = (ubyte
)fulldecl
->Semantic
.Name
;
191 info
->output_semantic_index
[reg
] = (ubyte
)fulldecl
->Semantic
.Index
;
194 if (procType
== TGSI_PROCESSOR_VERTEX
&&
195 fulldecl
->Semantic
.Name
== TGSI_SEMANTIC_CLIPDIST
) {
196 info
->num_written_clipdistance
+= util_bitcount(fulldecl
->Declaration
.UsageMask
);
198 /* extra info for special outputs */
199 if (procType
== TGSI_PROCESSOR_FRAGMENT
&&
200 fulldecl
->Semantic
.Name
== TGSI_SEMANTIC_POSITION
)
201 info
->writes_z
= TRUE
;
202 if (procType
== TGSI_PROCESSOR_FRAGMENT
&&
203 fulldecl
->Semantic
.Name
== TGSI_SEMANTIC_STENCIL
)
204 info
->writes_stencil
= TRUE
;
205 if (procType
== TGSI_PROCESSOR_VERTEX
&&
206 fulldecl
->Semantic
.Name
== TGSI_SEMANTIC_EDGEFLAG
) {
207 info
->writes_edgeflag
= TRUE
;
215 case TGSI_TOKEN_TYPE_IMMEDIATE
:
217 uint reg
= info
->immediate_count
++;
218 uint file
= TGSI_FILE_IMMEDIATE
;
220 info
->file_mask
[file
] |= (1 << reg
);
221 info
->file_count
[file
]++;
222 info
->file_max
[file
] = MAX2(info
->file_max
[file
], (int)reg
);
226 case TGSI_TOKEN_TYPE_PROPERTY
:
228 const struct tgsi_full_property
*fullprop
229 = &parse
.FullToken
.FullProperty
;
231 info
->properties
[info
->num_properties
].name
=
232 fullprop
->Property
.PropertyName
;
233 memcpy(info
->properties
[info
->num_properties
].data
,
234 fullprop
->u
, 8 * sizeof(unsigned));;
236 ++info
->num_properties
;
245 info
->uses_kill
= (info
->opcode_count
[TGSI_OPCODE_KIL
] ||
246 info
->opcode_count
[TGSI_OPCODE_KILP
]);
248 /* extract simple properties */
249 for (i
= 0; i
< info
->num_properties
; ++i
) {
250 switch (info
->properties
[i
].name
) {
251 case TGSI_PROPERTY_FS_COORD_ORIGIN
:
252 info
->origin_lower_left
= info
->properties
[i
].data
[0];
254 case TGSI_PROPERTY_FS_COORD_PIXEL_CENTER
:
255 info
->pixel_center_integer
= info
->properties
[i
].data
[0];
257 case TGSI_PROPERTY_FS_COLOR0_WRITES_ALL_CBUFS
:
258 info
->color0_writes_all_cbufs
= info
->properties
[i
].data
[0];
265 tgsi_parse_free (&parse
);
271 * Check if the given shader is a "passthrough" shader consisting of only
272 * MOV instructions of the form: MOV OUT[n], IN[n]
276 tgsi_is_passthrough_shader(const struct tgsi_token
*tokens
)
278 struct tgsi_parse_context parse
;
281 ** Setup to begin parsing input shader
283 if (tgsi_parse_init(&parse
, tokens
) != TGSI_PARSE_OK
) {
284 debug_printf("tgsi_parse_init() failed in tgsi_is_passthrough_shader()!\n");
289 ** Loop over incoming program tokens/instructions
291 while (!tgsi_parse_end_of_tokens(&parse
)) {
293 tgsi_parse_token(&parse
);
295 switch (parse
.FullToken
.Token
.Type
) {
296 case TGSI_TOKEN_TYPE_INSTRUCTION
:
298 struct tgsi_full_instruction
*fullinst
=
299 &parse
.FullToken
.FullInstruction
;
300 const struct tgsi_full_src_register
*src
=
302 const struct tgsi_full_dst_register
*dst
=
305 /* Do a whole bunch of checks for a simple move */
306 if (fullinst
->Instruction
.Opcode
!= TGSI_OPCODE_MOV
||
307 (src
->Register
.File
!= TGSI_FILE_INPUT
&&
308 src
->Register
.File
!= TGSI_FILE_SYSTEM_VALUE
) ||
309 dst
->Register
.File
!= TGSI_FILE_OUTPUT
||
310 src
->Register
.Index
!= dst
->Register
.Index
||
312 src
->Register
.Negate
||
313 src
->Register
.Absolute
||
315 src
->Register
.SwizzleX
!= TGSI_SWIZZLE_X
||
316 src
->Register
.SwizzleY
!= TGSI_SWIZZLE_Y
||
317 src
->Register
.SwizzleZ
!= TGSI_SWIZZLE_Z
||
318 src
->Register
.SwizzleW
!= TGSI_SWIZZLE_W
||
320 dst
->Register
.WriteMask
!= TGSI_WRITEMASK_XYZW
)
322 tgsi_parse_free(&parse
);
328 case TGSI_TOKEN_TYPE_DECLARATION
:
330 case TGSI_TOKEN_TYPE_IMMEDIATE
:
332 case TGSI_TOKEN_TYPE_PROPERTY
:
339 tgsi_parse_free(&parse
);
341 /* if we get here, it's a pass-through shader */