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_debug.h"
38 #include "util/u_math.h"
39 #include "tgsi/tgsi_parse.h"
40 #include "tgsi/tgsi_util.h"
41 #include "tgsi/tgsi_scan.h"
47 * Scan the given TGSI shader to collect information such as number of
48 * registers used, special instructions used, etc.
49 * \return info the result of the scan
52 tgsi_scan_shader(const struct tgsi_token
*tokens
,
53 struct tgsi_shader_info
*info
)
56 struct tgsi_parse_context parse
;
58 memset(info
, 0, sizeof(*info
));
59 for (i
= 0; i
< TGSI_FILE_COUNT
; i
++)
60 info
->file_max
[i
] = -1;
63 ** Setup to begin parsing input shader
65 if (tgsi_parse_init( &parse
, tokens
) != TGSI_PARSE_OK
) {
66 debug_printf("tgsi_parse_init() failed in tgsi_scan_shader()!\n");
69 procType
= parse
.FullHeader
.Processor
.Processor
;
70 assert(procType
== TGSI_PROCESSOR_FRAGMENT
||
71 procType
== TGSI_PROCESSOR_VERTEX
||
72 procType
== TGSI_PROCESSOR_GEOMETRY
||
73 procType
== TGSI_PROCESSOR_COMPUTE
);
77 ** Loop over incoming program tokens/instructions
79 while( !tgsi_parse_end_of_tokens( &parse
) ) {
83 tgsi_parse_token( &parse
);
85 switch( parse
.FullToken
.Token
.Type
) {
86 case TGSI_TOKEN_TYPE_INSTRUCTION
:
88 const struct tgsi_full_instruction
*fullinst
89 = &parse
.FullToken
.FullInstruction
;
92 assert(fullinst
->Instruction
.Opcode
< TGSI_OPCODE_LAST
);
93 info
->opcode_count
[fullinst
->Instruction
.Opcode
]++;
95 for (i
= 0; i
< fullinst
->Instruction
.NumSrcRegs
; i
++) {
96 const struct tgsi_full_src_register
*src
=
98 int ind
= src
->Register
.Index
;
100 /* Mark which inputs are effectively used */
101 if (src
->Register
.File
== TGSI_FILE_INPUT
) {
103 usage_mask
= tgsi_util_get_inst_usage_mask(fullinst
, i
);
104 if (src
->Register
.Indirect
) {
105 for (ind
= 0; ind
< info
->num_inputs
; ++ind
) {
106 info
->input_usage_mask
[ind
] |= usage_mask
;
110 assert(ind
< PIPE_MAX_SHADER_INPUTS
);
111 info
->input_usage_mask
[ind
] |= usage_mask
;
114 if (procType
== TGSI_PROCESSOR_FRAGMENT
&&
115 src
->Register
.File
== TGSI_FILE_INPUT
&&
116 info
->reads_position
&&
117 src
->Register
.Index
== 0 &&
118 (src
->Register
.SwizzleX
== TGSI_SWIZZLE_Z
||
119 src
->Register
.SwizzleY
== TGSI_SWIZZLE_Z
||
120 src
->Register
.SwizzleZ
== TGSI_SWIZZLE_Z
||
121 src
->Register
.SwizzleW
== TGSI_SWIZZLE_Z
)) {
122 info
->reads_z
= TRUE
;
126 /* check for indirect register reads */
127 if (src
->Register
.Indirect
) {
128 info
->indirect_files
|= (1 << src
->Register
.File
);
132 /* check for indirect register writes */
133 for (i
= 0; i
< fullinst
->Instruction
.NumDstRegs
; i
++) {
134 const struct tgsi_full_dst_register
*dst
= &fullinst
->Dst
[i
];
135 if (dst
->Register
.Indirect
) {
136 info
->indirect_files
|= (1 << dst
->Register
.File
);
140 info
->num_instructions
++;
144 case TGSI_TOKEN_TYPE_DECLARATION
:
146 const struct tgsi_full_declaration
*fulldecl
147 = &parse
.FullToken
.FullDeclaration
;
148 const uint file
= fulldecl
->Declaration
.File
;
150 for (reg
= fulldecl
->Range
.First
;
151 reg
<= fulldecl
->Range
.Last
;
154 /* only first 32 regs will appear in this bitfield */
155 info
->file_mask
[file
] |= (1 << reg
);
156 info
->file_count
[file
]++;
157 info
->file_max
[file
] = MAX2(info
->file_max
[file
], (int)reg
);
159 if (file
== TGSI_FILE_INPUT
) {
160 info
->input_semantic_name
[reg
] = (ubyte
)fulldecl
->Semantic
.Name
;
161 info
->input_semantic_index
[reg
] = (ubyte
)fulldecl
->Semantic
.Index
;
162 info
->input_interpolate
[reg
] = (ubyte
)fulldecl
->Interp
.Interpolate
;
163 info
->input_centroid
[reg
] = (ubyte
)fulldecl
->Interp
.Centroid
;
164 info
->input_cylindrical_wrap
[reg
] = (ubyte
)fulldecl
->Interp
.CylindricalWrap
;
167 if (procType
== TGSI_PROCESSOR_FRAGMENT
&&
168 fulldecl
->Semantic
.Name
== TGSI_SEMANTIC_POSITION
)
169 info
->reads_position
= TRUE
;
171 else if (file
== TGSI_FILE_SYSTEM_VALUE
) {
172 unsigned index
= fulldecl
->Range
.First
;
173 unsigned semName
= fulldecl
->Semantic
.Name
;
175 info
->system_value_semantic_name
[index
] = semName
;
176 info
->num_system_values
= MAX2(info
->num_system_values
,
180 info->system_value_semantic_name[info->num_system_values++] =
181 fulldecl->Semantic.Name;
184 if (fulldecl
->Semantic
.Name
== TGSI_SEMANTIC_INSTANCEID
) {
185 info
->uses_instanceid
= TRUE
;
187 else if (fulldecl
->Semantic
.Name
== TGSI_SEMANTIC_VERTEXID
) {
188 info
->uses_vertexid
= TRUE
;
189 } else if (fulldecl
->Semantic
.Name
== TGSI_SEMANTIC_PRIMID
) {
190 info
->uses_primid
= TRUE
;
193 else if (file
== TGSI_FILE_OUTPUT
) {
194 info
->output_semantic_name
[reg
] = (ubyte
)fulldecl
->Semantic
.Name
;
195 info
->output_semantic_index
[reg
] = (ubyte
)fulldecl
->Semantic
.Index
;
198 if (procType
== TGSI_PROCESSOR_VERTEX
&&
199 fulldecl
->Semantic
.Name
== TGSI_SEMANTIC_CLIPDIST
) {
200 info
->num_written_clipdistance
+= util_bitcount(fulldecl
->Declaration
.UsageMask
);
202 /* extra info for special outputs */
203 if (procType
== TGSI_PROCESSOR_FRAGMENT
&&
204 fulldecl
->Semantic
.Name
== TGSI_SEMANTIC_POSITION
)
205 info
->writes_z
= TRUE
;
206 if (procType
== TGSI_PROCESSOR_FRAGMENT
&&
207 fulldecl
->Semantic
.Name
== TGSI_SEMANTIC_STENCIL
)
208 info
->writes_stencil
= TRUE
;
209 if (procType
== TGSI_PROCESSOR_VERTEX
&&
210 fulldecl
->Semantic
.Name
== TGSI_SEMANTIC_EDGEFLAG
) {
211 info
->writes_edgeflag
= TRUE
;
219 case TGSI_TOKEN_TYPE_IMMEDIATE
:
221 uint reg
= info
->immediate_count
++;
222 uint file
= TGSI_FILE_IMMEDIATE
;
224 info
->file_mask
[file
] |= (1 << reg
);
225 info
->file_count
[file
]++;
226 info
->file_max
[file
] = MAX2(info
->file_max
[file
], (int)reg
);
230 case TGSI_TOKEN_TYPE_PROPERTY
:
232 const struct tgsi_full_property
*fullprop
233 = &parse
.FullToken
.FullProperty
;
235 info
->properties
[info
->num_properties
].name
=
236 fullprop
->Property
.PropertyName
;
237 memcpy(info
->properties
[info
->num_properties
].data
,
238 fullprop
->u
, 8 * sizeof(unsigned));;
240 ++info
->num_properties
;
249 info
->uses_kill
= (info
->opcode_count
[TGSI_OPCODE_KIL
] ||
250 info
->opcode_count
[TGSI_OPCODE_KILP
]);
252 /* extract simple properties */
253 for (i
= 0; i
< info
->num_properties
; ++i
) {
254 switch (info
->properties
[i
].name
) {
255 case TGSI_PROPERTY_FS_COORD_ORIGIN
:
256 info
->origin_lower_left
= info
->properties
[i
].data
[0];
258 case TGSI_PROPERTY_FS_COORD_PIXEL_CENTER
:
259 info
->pixel_center_integer
= info
->properties
[i
].data
[0];
261 case TGSI_PROPERTY_FS_COLOR0_WRITES_ALL_CBUFS
:
262 info
->color0_writes_all_cbufs
= info
->properties
[i
].data
[0];
269 tgsi_parse_free (&parse
);
275 * Check if the given shader is a "passthrough" shader consisting of only
276 * MOV instructions of the form: MOV OUT[n], IN[n]
280 tgsi_is_passthrough_shader(const struct tgsi_token
*tokens
)
282 struct tgsi_parse_context parse
;
285 ** Setup to begin parsing input shader
287 if (tgsi_parse_init(&parse
, tokens
) != TGSI_PARSE_OK
) {
288 debug_printf("tgsi_parse_init() failed in tgsi_is_passthrough_shader()!\n");
293 ** Loop over incoming program tokens/instructions
295 while (!tgsi_parse_end_of_tokens(&parse
)) {
297 tgsi_parse_token(&parse
);
299 switch (parse
.FullToken
.Token
.Type
) {
300 case TGSI_TOKEN_TYPE_INSTRUCTION
:
302 struct tgsi_full_instruction
*fullinst
=
303 &parse
.FullToken
.FullInstruction
;
304 const struct tgsi_full_src_register
*src
=
306 const struct tgsi_full_dst_register
*dst
=
309 /* Do a whole bunch of checks for a simple move */
310 if (fullinst
->Instruction
.Opcode
!= TGSI_OPCODE_MOV
||
311 (src
->Register
.File
!= TGSI_FILE_INPUT
&&
312 src
->Register
.File
!= TGSI_FILE_SYSTEM_VALUE
) ||
313 dst
->Register
.File
!= TGSI_FILE_OUTPUT
||
314 src
->Register
.Index
!= dst
->Register
.Index
||
316 src
->Register
.Negate
||
317 src
->Register
.Absolute
||
319 src
->Register
.SwizzleX
!= TGSI_SWIZZLE_X
||
320 src
->Register
.SwizzleY
!= TGSI_SWIZZLE_Y
||
321 src
->Register
.SwizzleZ
!= TGSI_SWIZZLE_Z
||
322 src
->Register
.SwizzleW
!= TGSI_SWIZZLE_W
||
324 dst
->Register
.WriteMask
!= TGSI_WRITEMASK_XYZW
)
326 tgsi_parse_free(&parse
);
332 case TGSI_TOKEN_TYPE_DECLARATION
:
334 case TGSI_TOKEN_TYPE_IMMEDIATE
:
336 case TGSI_TOKEN_TYPE_PROPERTY
:
343 tgsi_parse_free(&parse
);
345 /* if we get here, it's a pass-through shader */