2a37b38d3c75cd136003f27c4d7a324ba39318a2
[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_debug.h"
38 #include "util/u_math.h"
39 #include "util/u_prim.h"
40 #include "tgsi/tgsi_parse.h"
41 #include "tgsi/tgsi_util.h"
42 #include "tgsi/tgsi_scan.h"
43
44
45
46
47 /**
48 * Scan the given TGSI shader to collect information such as number of
49 * registers used, special instructions used, etc.
50 * \return info the result of the scan
51 */
52 void
53 tgsi_scan_shader(const struct tgsi_token *tokens,
54 struct tgsi_shader_info *info)
55 {
56 uint procType, i;
57 struct tgsi_parse_context parse;
58
59 memset(info, 0, sizeof(*info));
60 for (i = 0; i < TGSI_FILE_COUNT; i++)
61 info->file_max[i] = -1;
62
63 /**
64 ** Setup to begin parsing input shader
65 **/
66 if (tgsi_parse_init( &parse, tokens ) != TGSI_PARSE_OK) {
67 debug_printf("tgsi_parse_init() failed in tgsi_scan_shader()!\n");
68 return;
69 }
70 procType = parse.FullHeader.Processor.Processor;
71 assert(procType == TGSI_PROCESSOR_FRAGMENT ||
72 procType == TGSI_PROCESSOR_VERTEX ||
73 procType == TGSI_PROCESSOR_GEOMETRY ||
74 procType == TGSI_PROCESSOR_COMPUTE);
75
76
77 /**
78 ** Loop over incoming program tokens/instructions
79 */
80 while( !tgsi_parse_end_of_tokens( &parse ) ) {
81
82 info->num_tokens++;
83
84 tgsi_parse_token( &parse );
85
86 switch( parse.FullToken.Token.Type ) {
87 case TGSI_TOKEN_TYPE_INSTRUCTION:
88 {
89 const struct tgsi_full_instruction *fullinst
90 = &parse.FullToken.FullInstruction;
91 uint i;
92
93 assert(fullinst->Instruction.Opcode < TGSI_OPCODE_LAST);
94 info->opcode_count[fullinst->Instruction.Opcode]++;
95
96 for (i = 0; i < fullinst->Instruction.NumSrcRegs; i++) {
97 const struct tgsi_full_src_register *src =
98 &fullinst->Src[i];
99 int ind = src->Register.Index;
100
101 /* Mark which inputs are effectively used */
102 if (src->Register.File == TGSI_FILE_INPUT) {
103 unsigned usage_mask;
104 usage_mask = tgsi_util_get_inst_usage_mask(fullinst, i);
105 if (src->Register.Indirect) {
106 for (ind = 0; ind < info->num_inputs; ++ind) {
107 info->input_usage_mask[ind] |= usage_mask;
108 }
109 } else {
110 assert(ind >= 0);
111 assert(ind < PIPE_MAX_SHADER_INPUTS);
112 info->input_usage_mask[ind] |= usage_mask;
113 }
114
115 if (procType == TGSI_PROCESSOR_FRAGMENT &&
116 src->Register.File == TGSI_FILE_INPUT &&
117 info->reads_position &&
118 src->Register.Index == 0 &&
119 (src->Register.SwizzleX == TGSI_SWIZZLE_Z ||
120 src->Register.SwizzleY == TGSI_SWIZZLE_Z ||
121 src->Register.SwizzleZ == TGSI_SWIZZLE_Z ||
122 src->Register.SwizzleW == TGSI_SWIZZLE_Z)) {
123 info->reads_z = TRUE;
124 }
125 }
126
127 /* check for indirect register reads */
128 if (src->Register.Indirect) {
129 info->indirect_files |= (1 << src->Register.File);
130 }
131 }
132
133 /* check for indirect register writes */
134 for (i = 0; i < fullinst->Instruction.NumDstRegs; i++) {
135 const struct tgsi_full_dst_register *dst = &fullinst->Dst[i];
136 if (dst->Register.Indirect) {
137 info->indirect_files |= (1 << dst->Register.File);
138 }
139 }
140
141 info->num_instructions++;
142 }
143 break;
144
145 case TGSI_TOKEN_TYPE_DECLARATION:
146 {
147 const struct tgsi_full_declaration *fulldecl
148 = &parse.FullToken.FullDeclaration;
149 const uint file = fulldecl->Declaration.File;
150 uint reg;
151 for (reg = fulldecl->Range.First;
152 reg <= fulldecl->Range.Last;
153 reg++) {
154
155 /* only first 32 regs will appear in this bitfield */
156 info->file_mask[file] |= (1 << reg);
157 info->file_count[file]++;
158 info->file_max[file] = MAX2(info->file_max[file], (int)reg);
159
160 if (file == TGSI_FILE_INPUT) {
161 info->input_semantic_name[reg] = (ubyte)fulldecl->Semantic.Name;
162 info->input_semantic_index[reg] = (ubyte)fulldecl->Semantic.Index;
163 info->input_interpolate[reg] = (ubyte)fulldecl->Interp.Interpolate;
164 info->input_centroid[reg] = (ubyte)fulldecl->Interp.Centroid;
165 info->input_cylindrical_wrap[reg] = (ubyte)fulldecl->Interp.CylindricalWrap;
166 info->num_inputs++;
167
168 if (procType == TGSI_PROCESSOR_FRAGMENT &&
169 fulldecl->Semantic.Name == TGSI_SEMANTIC_POSITION)
170 info->reads_position = TRUE;
171 }
172 else if (file == TGSI_FILE_SYSTEM_VALUE) {
173 unsigned index = fulldecl->Range.First;
174 unsigned semName = fulldecl->Semantic.Name;
175
176 info->system_value_semantic_name[index] = semName;
177 info->num_system_values = MAX2(info->num_system_values,
178 index + 1);
179
180 /*
181 info->system_value_semantic_name[info->num_system_values++] =
182 fulldecl->Semantic.Name;
183 */
184
185 if (fulldecl->Semantic.Name == TGSI_SEMANTIC_INSTANCEID) {
186 info->uses_instanceid = TRUE;
187 }
188 else if (fulldecl->Semantic.Name == TGSI_SEMANTIC_VERTEXID) {
189 info->uses_vertexid = TRUE;
190 } else if (fulldecl->Semantic.Name == TGSI_SEMANTIC_PRIMID) {
191 info->uses_primid = TRUE;
192 }
193 }
194 else if (file == TGSI_FILE_OUTPUT) {
195 info->output_semantic_name[reg] = (ubyte)fulldecl->Semantic.Name;
196 info->output_semantic_index[reg] = (ubyte)fulldecl->Semantic.Index;
197 info->num_outputs++;
198
199 if ((procType == TGSI_PROCESSOR_VERTEX || procType == TGSI_PROCESSOR_GEOMETRY) &&
200 fulldecl->Semantic.Name == TGSI_SEMANTIC_CLIPDIST) {
201 info->num_written_clipdistance += util_bitcount(fulldecl->Declaration.UsageMask);
202 }
203 if ((procType == TGSI_PROCESSOR_VERTEX || procType == TGSI_PROCESSOR_GEOMETRY) &&
204 fulldecl->Semantic.Name == TGSI_SEMANTIC_CULLDIST) {
205 info->num_written_culldistance += util_bitcount(fulldecl->Declaration.UsageMask);
206 }
207 /* extra info for special outputs */
208 if (procType == TGSI_PROCESSOR_FRAGMENT &&
209 fulldecl->Semantic.Name == TGSI_SEMANTIC_POSITION)
210 info->writes_z = TRUE;
211 if (procType == TGSI_PROCESSOR_FRAGMENT &&
212 fulldecl->Semantic.Name == TGSI_SEMANTIC_STENCIL)
213 info->writes_stencil = TRUE;
214 if (procType == TGSI_PROCESSOR_VERTEX &&
215 fulldecl->Semantic.Name == TGSI_SEMANTIC_EDGEFLAG) {
216 info->writes_edgeflag = TRUE;
217 }
218
219 if (procType == TGSI_PROCESSOR_GEOMETRY &&
220 fulldecl->Semantic.Name ==
221 TGSI_SEMANTIC_VIEWPORT_INDEX) {
222 info->writes_viewport_index = TRUE;
223 }
224 if (procType == TGSI_PROCESSOR_GEOMETRY &&
225 fulldecl->Semantic.Name ==
226 TGSI_SEMANTIC_LAYER) {
227 info->writes_layer = TRUE;
228 }
229 }
230
231 }
232 }
233 break;
234
235 case TGSI_TOKEN_TYPE_IMMEDIATE:
236 {
237 uint reg = info->immediate_count++;
238 uint file = TGSI_FILE_IMMEDIATE;
239
240 info->file_mask[file] |= (1 << reg);
241 info->file_count[file]++;
242 info->file_max[file] = MAX2(info->file_max[file], (int)reg);
243 }
244 break;
245
246 case TGSI_TOKEN_TYPE_PROPERTY:
247 {
248 const struct tgsi_full_property *fullprop
249 = &parse.FullToken.FullProperty;
250
251 info->properties[info->num_properties].name =
252 fullprop->Property.PropertyName;
253 memcpy(info->properties[info->num_properties].data,
254 fullprop->u, 8 * sizeof(unsigned));;
255
256 ++info->num_properties;
257 }
258 break;
259
260 default:
261 assert( 0 );
262 }
263 }
264
265 info->uses_kill = (info->opcode_count[TGSI_OPCODE_KIL] ||
266 info->opcode_count[TGSI_OPCODE_KILP]);
267
268 /* extract simple properties */
269 for (i = 0; i < info->num_properties; ++i) {
270 switch (info->properties[i].name) {
271 case TGSI_PROPERTY_FS_COORD_ORIGIN:
272 info->origin_lower_left = info->properties[i].data[0];
273 break;
274 case TGSI_PROPERTY_FS_COORD_PIXEL_CENTER:
275 info->pixel_center_integer = info->properties[i].data[0];
276 break;
277 case TGSI_PROPERTY_FS_COLOR0_WRITES_ALL_CBUFS:
278 info->color0_writes_all_cbufs = info->properties[i].data[0];
279 break;
280 case TGSI_PROPERTY_GS_INPUT_PRIM:
281 /* The dimensions of the IN decleration in geometry shader have
282 * to be deduced from the type of the input primitive.
283 */
284 if (procType == TGSI_PROCESSOR_GEOMETRY) {
285 unsigned input_primitive = info->properties[i].data[0];
286 int num_verts = u_vertices_per_prim(input_primitive);
287 unsigned j;
288 info->file_count[TGSI_FILE_INPUT] = num_verts;
289 info->file_max[TGSI_FILE_INPUT] =
290 MAX2(info->file_max[TGSI_FILE_INPUT], num_verts - 1);
291 for (j = 0; j < num_verts; ++j) {
292 info->file_mask[TGSI_FILE_INPUT] |= (1 << j);
293 }
294 }
295 break;
296 default:
297 ;
298 }
299 }
300
301 tgsi_parse_free (&parse);
302 }
303
304
305
306 /**
307 * Check if the given shader is a "passthrough" shader consisting of only
308 * MOV instructions of the form: MOV OUT[n], IN[n]
309 *
310 */
311 boolean
312 tgsi_is_passthrough_shader(const struct tgsi_token *tokens)
313 {
314 struct tgsi_parse_context parse;
315
316 /**
317 ** Setup to begin parsing input shader
318 **/
319 if (tgsi_parse_init(&parse, tokens) != TGSI_PARSE_OK) {
320 debug_printf("tgsi_parse_init() failed in tgsi_is_passthrough_shader()!\n");
321 return FALSE;
322 }
323
324 /**
325 ** Loop over incoming program tokens/instructions
326 */
327 while (!tgsi_parse_end_of_tokens(&parse)) {
328
329 tgsi_parse_token(&parse);
330
331 switch (parse.FullToken.Token.Type) {
332 case TGSI_TOKEN_TYPE_INSTRUCTION:
333 {
334 struct tgsi_full_instruction *fullinst =
335 &parse.FullToken.FullInstruction;
336 const struct tgsi_full_src_register *src =
337 &fullinst->Src[0];
338 const struct tgsi_full_dst_register *dst =
339 &fullinst->Dst[0];
340
341 /* Do a whole bunch of checks for a simple move */
342 if (fullinst->Instruction.Opcode != TGSI_OPCODE_MOV ||
343 (src->Register.File != TGSI_FILE_INPUT &&
344 src->Register.File != TGSI_FILE_SYSTEM_VALUE) ||
345 dst->Register.File != TGSI_FILE_OUTPUT ||
346 src->Register.Index != dst->Register.Index ||
347
348 src->Register.Negate ||
349 src->Register.Absolute ||
350
351 src->Register.SwizzleX != TGSI_SWIZZLE_X ||
352 src->Register.SwizzleY != TGSI_SWIZZLE_Y ||
353 src->Register.SwizzleZ != TGSI_SWIZZLE_Z ||
354 src->Register.SwizzleW != TGSI_SWIZZLE_W ||
355
356 dst->Register.WriteMask != TGSI_WRITEMASK_XYZW)
357 {
358 tgsi_parse_free(&parse);
359 return FALSE;
360 }
361 }
362 break;
363
364 case TGSI_TOKEN_TYPE_DECLARATION:
365 /* fall-through */
366 case TGSI_TOKEN_TYPE_IMMEDIATE:
367 /* fall-through */
368 case TGSI_TOKEN_TYPE_PROPERTY:
369 /* fall-through */
370 default:
371 ; /* no-op */
372 }
373 }
374
375 tgsi_parse_free(&parse);
376
377 /* if we get here, it's a pass-through shader */
378 return TRUE;
379 }