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