tgsi: also count instructions in tgsi_scan_shader()
[mesa.git] / src / gallium / auxiliary / tgsi / tgsi_scan.c
1 /**************************************************************************
2 *
3 * Copyright 2008 Tungsten Graphics, Inc., Cedar Park, Texas.
4 * All Rights Reserved.
5 * Copyright 2008 VMware, Inc. All rights Reserved.
6 *
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:
14 *
15 * The above copyright notice and this permission notice (including the
16 * next paragraph) shall be included in all copies or substantial portions
17 * of the Software.
18 *
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.
26 *
27 **************************************************************************/
28
29 /**
30 * TGSI program scan utility.
31 * Used to determine which registers and instructions are used by a shader.
32 *
33 * Authors: Brian Paul
34 */
35
36
37 #include "util/u_math.h"
38 #include "tgsi/tgsi_parse.h"
39 #include "tgsi/tgsi_scan.h"
40
41
42
43
44 /**
45 * Scan the given TGSI shader to collect information such as number of
46 * registers used, special instructions used, etc.
47 * \return info the result of the scan
48 */
49 void
50 tgsi_scan_shader(const struct tgsi_token *tokens,
51 struct tgsi_shader_info *info)
52 {
53 uint procType, i;
54 struct tgsi_parse_context parse;
55
56 memset(info, 0, sizeof(*info));
57 for (i = 0; i < TGSI_FILE_COUNT; i++)
58 info->file_max[i] = -1;
59
60 /**
61 ** Setup to begin parsing input shader
62 **/
63 if (tgsi_parse_init( &parse, tokens ) != TGSI_PARSE_OK) {
64 debug_printf("tgsi_parse_init() failed in tgsi_scan_shader()!\n");
65 return;
66 }
67 procType = parse.FullHeader.Processor.Processor;
68 assert(procType == TGSI_PROCESSOR_FRAGMENT ||
69 procType == TGSI_PROCESSOR_VERTEX ||
70 procType == TGSI_PROCESSOR_GEOMETRY);
71
72
73 /**
74 ** Loop over incoming program tokens/instructions
75 */
76 while( !tgsi_parse_end_of_tokens( &parse ) ) {
77
78 info->num_tokens++;
79
80 tgsi_parse_token( &parse );
81
82 switch( parse.FullToken.Token.Type ) {
83 case TGSI_TOKEN_TYPE_INSTRUCTION:
84 {
85 const struct tgsi_full_instruction *fullinst
86 = &parse.FullToken.FullInstruction;
87
88 assert(fullinst->Instruction.Opcode < TGSI_OPCODE_LAST);
89 info->opcode_count[fullinst->Instruction.Opcode]++;
90
91 /* check if we read the frag shader FOG or FACE inputs */
92 if (procType == TGSI_PROCESSOR_FRAGMENT) {
93 uint i;
94 for (i = 0; i < fullinst->Instruction.NumSrcRegs; i++) {
95 const struct tgsi_full_src_register *src =
96 &fullinst->Src[i];
97 if (src->Register.File == TGSI_FILE_INPUT ||
98 src->Register.File == TGSI_FILE_SYSTEM_VALUE) {
99 const int ind = src->Register.Index;
100 if (info->input_semantic_name[ind] == TGSI_SEMANTIC_FOG) {
101 info->uses_fogcoord = TRUE;
102 }
103 else if (info->input_semantic_name[ind] == TGSI_SEMANTIC_FACE) {
104 info->uses_frontfacing = TRUE;
105 }
106 }
107 }
108 }
109
110 info->num_instructions++;
111 }
112 break;
113
114 case TGSI_TOKEN_TYPE_DECLARATION:
115 {
116 const struct tgsi_full_declaration *fulldecl
117 = &parse.FullToken.FullDeclaration;
118 const uint file = fulldecl->Declaration.File;
119 uint reg;
120 for (reg = fulldecl->Range.First;
121 reg <= fulldecl->Range.Last;
122 reg++) {
123
124 /* only first 32 regs will appear in this bitfield */
125 info->file_mask[file] |= (1 << reg);
126 info->file_count[file]++;
127 info->file_max[file] = MAX2(info->file_max[file], (int)reg);
128
129 if (file == TGSI_FILE_INPUT || file == TGSI_FILE_SYSTEM_VALUE) {
130 info->input_semantic_name[reg] = (ubyte)fulldecl->Semantic.Name;
131 info->input_semantic_index[reg] = (ubyte)fulldecl->Semantic.Index;
132 info->input_interpolate[reg] = (ubyte)fulldecl->Declaration.Interpolate;
133 info->input_cylindrical_wrap[reg] = (ubyte)fulldecl->Declaration.CylindricalWrap;
134 info->num_inputs++;
135 }
136 else if (file == TGSI_FILE_OUTPUT) {
137 info->output_semantic_name[reg] = (ubyte)fulldecl->Semantic.Name;
138 info->output_semantic_index[reg] = (ubyte)fulldecl->Semantic.Index;
139 info->num_outputs++;
140
141 /* extra info for special outputs */
142 if (procType == TGSI_PROCESSOR_FRAGMENT &&
143 fulldecl->Semantic.Name == TGSI_SEMANTIC_POSITION) {
144 info->writes_z = TRUE;
145 }
146 if (procType == TGSI_PROCESSOR_VERTEX &&
147 fulldecl->Semantic.Name == TGSI_SEMANTIC_EDGEFLAG) {
148 info->writes_edgeflag = TRUE;
149 }
150 }
151
152 }
153 }
154 break;
155
156 case TGSI_TOKEN_TYPE_IMMEDIATE:
157 {
158 uint reg = info->immediate_count++;
159 uint file = TGSI_FILE_IMMEDIATE;
160
161 info->file_mask[file] |= (1 << reg);
162 info->file_count[file]++;
163 info->file_max[file] = MAX2(info->file_max[file], (int)reg);
164 }
165 break;
166 case TGSI_TOKEN_TYPE_PROPERTY:
167 {
168 const struct tgsi_full_property *fullprop
169 = &parse.FullToken.FullProperty;
170
171 info->properties[info->num_properties].name =
172 fullprop->Property.PropertyName;
173 memcpy(info->properties[info->num_properties].data,
174 fullprop->u, 8 * sizeof(unsigned));;
175
176 ++info->num_properties;
177 }
178 break;
179
180 default:
181 assert( 0 );
182 }
183 }
184
185 info->uses_kill = (info->opcode_count[TGSI_OPCODE_KIL] ||
186 info->opcode_count[TGSI_OPCODE_KILP]);
187
188 tgsi_parse_free (&parse);
189 }
190
191
192
193 /**
194 * Check if the given shader is a "passthrough" shader consisting of only
195 * MOV instructions of the form: MOV OUT[n], IN[n]
196 *
197 */
198 boolean
199 tgsi_is_passthrough_shader(const struct tgsi_token *tokens)
200 {
201 struct tgsi_parse_context parse;
202
203 /**
204 ** Setup to begin parsing input shader
205 **/
206 if (tgsi_parse_init(&parse, tokens) != TGSI_PARSE_OK) {
207 debug_printf("tgsi_parse_init() failed in tgsi_is_passthrough_shader()!\n");
208 return FALSE;
209 }
210
211 /**
212 ** Loop over incoming program tokens/instructions
213 */
214 while (!tgsi_parse_end_of_tokens(&parse)) {
215
216 tgsi_parse_token(&parse);
217
218 switch (parse.FullToken.Token.Type) {
219 case TGSI_TOKEN_TYPE_INSTRUCTION:
220 {
221 struct tgsi_full_instruction *fullinst =
222 &parse.FullToken.FullInstruction;
223 const struct tgsi_full_src_register *src =
224 &fullinst->Src[0];
225 const struct tgsi_full_dst_register *dst =
226 &fullinst->Dst[0];
227
228 /* Do a whole bunch of checks for a simple move */
229 if (fullinst->Instruction.Opcode != TGSI_OPCODE_MOV ||
230 (src->Register.File != TGSI_FILE_INPUT &&
231 src->Register.File != TGSI_FILE_SYSTEM_VALUE) ||
232 dst->Register.File != TGSI_FILE_OUTPUT ||
233 src->Register.Index != dst->Register.Index ||
234
235 src->Register.Negate ||
236 src->Register.Absolute ||
237
238 src->Register.SwizzleX != TGSI_SWIZZLE_X ||
239 src->Register.SwizzleY != TGSI_SWIZZLE_Y ||
240 src->Register.SwizzleZ != TGSI_SWIZZLE_Z ||
241 src->Register.SwizzleW != TGSI_SWIZZLE_W ||
242
243 dst->Register.WriteMask != TGSI_WRITEMASK_XYZW)
244 {
245 tgsi_parse_free(&parse);
246 return FALSE;
247 }
248 }
249 break;
250
251 case TGSI_TOKEN_TYPE_DECLARATION:
252 /* fall-through */
253 case TGSI_TOKEN_TYPE_IMMEDIATE:
254 /* fall-through */
255 case TGSI_TOKEN_TYPE_PROPERTY:
256 /* fall-through */
257 default:
258 ; /* no-op */
259 }
260 }
261
262 tgsi_parse_free(&parse);
263
264 /* if we get here, it's a pass-through shader */
265 return TRUE;
266 }