4f85d2fda6794255c6d1c30d2bb9e63b2dd7c024
[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 static void
48 scan_instruction(struct tgsi_shader_info *info,
49 const struct tgsi_full_instruction *fullinst,
50 unsigned *current_depth)
51 {
52 unsigned i;
53
54 assert(fullinst->Instruction.Opcode < TGSI_OPCODE_LAST);
55 info->opcode_count[fullinst->Instruction.Opcode]++;
56
57 switch (fullinst->Instruction.Opcode) {
58 case TGSI_OPCODE_IF:
59 case TGSI_OPCODE_UIF:
60 case TGSI_OPCODE_BGNLOOP:
61 (*current_depth)++;
62 info->max_depth = MAX2(info->max_depth, *current_depth);
63 break;
64 case TGSI_OPCODE_ENDIF:
65 case TGSI_OPCODE_ENDLOOP:
66 (*current_depth)--;
67 break;
68 default:
69 break;
70 }
71
72 if (fullinst->Instruction.Opcode == TGSI_OPCODE_INTERP_CENTROID ||
73 fullinst->Instruction.Opcode == TGSI_OPCODE_INTERP_OFFSET ||
74 fullinst->Instruction.Opcode == TGSI_OPCODE_INTERP_SAMPLE) {
75 const struct tgsi_full_src_register *src0 = &fullinst->Src[0];
76 unsigned input;
77
78 if (src0->Register.Indirect && src0->Indirect.ArrayID)
79 input = info->input_array_first[src0->Indirect.ArrayID];
80 else
81 input = src0->Register.Index;
82
83 /* For the INTERP opcodes, the interpolation is always
84 * PERSPECTIVE unless LINEAR is specified.
85 */
86 switch (info->input_interpolate[input]) {
87 case TGSI_INTERPOLATE_COLOR:
88 case TGSI_INTERPOLATE_CONSTANT:
89 case TGSI_INTERPOLATE_PERSPECTIVE:
90 switch (fullinst->Instruction.Opcode) {
91 case TGSI_OPCODE_INTERP_CENTROID:
92 info->uses_persp_opcode_interp_centroid = TRUE;
93 break;
94 case TGSI_OPCODE_INTERP_OFFSET:
95 info->uses_persp_opcode_interp_offset = TRUE;
96 break;
97 case TGSI_OPCODE_INTERP_SAMPLE:
98 info->uses_persp_opcode_interp_sample = TRUE;
99 break;
100 }
101 break;
102
103 case TGSI_INTERPOLATE_LINEAR:
104 switch (fullinst->Instruction.Opcode) {
105 case TGSI_OPCODE_INTERP_CENTROID:
106 info->uses_linear_opcode_interp_centroid = TRUE;
107 break;
108 case TGSI_OPCODE_INTERP_OFFSET:
109 info->uses_linear_opcode_interp_offset = TRUE;
110 break;
111 case TGSI_OPCODE_INTERP_SAMPLE:
112 info->uses_linear_opcode_interp_sample = TRUE;
113 break;
114 }
115 break;
116 }
117 }
118
119 if (fullinst->Instruction.Opcode >= TGSI_OPCODE_F2D &&
120 fullinst->Instruction.Opcode <= TGSI_OPCODE_DSSG)
121 info->uses_doubles = TRUE;
122
123 for (i = 0; i < fullinst->Instruction.NumSrcRegs; i++) {
124 const struct tgsi_full_src_register *src = &fullinst->Src[i];
125 int ind = src->Register.Index;
126
127 /* Mark which inputs are effectively used */
128 if (src->Register.File == TGSI_FILE_INPUT) {
129 unsigned usage_mask;
130 usage_mask = tgsi_util_get_inst_usage_mask(fullinst, i);
131 if (src->Register.Indirect) {
132 for (ind = 0; ind < info->num_inputs; ++ind) {
133 info->input_usage_mask[ind] |= usage_mask;
134 }
135 } else {
136 assert(ind >= 0);
137 assert(ind < PIPE_MAX_SHADER_INPUTS);
138 info->input_usage_mask[ind] |= usage_mask;
139 }
140
141 if (info->processor == TGSI_PROCESSOR_FRAGMENT &&
142 !src->Register.Indirect) {
143 unsigned name =
144 info->input_semantic_name[src->Register.Index];
145 unsigned index =
146 info->input_semantic_index[src->Register.Index];
147
148 if (name == TGSI_SEMANTIC_POSITION &&
149 (src->Register.SwizzleX == TGSI_SWIZZLE_Z ||
150 src->Register.SwizzleY == TGSI_SWIZZLE_Z ||
151 src->Register.SwizzleZ == TGSI_SWIZZLE_Z ||
152 src->Register.SwizzleW == TGSI_SWIZZLE_Z))
153 info->reads_z = TRUE;
154
155 if (name == TGSI_SEMANTIC_COLOR) {
156 unsigned mask =
157 (1 << src->Register.SwizzleX) |
158 (1 << src->Register.SwizzleY) |
159 (1 << src->Register.SwizzleZ) |
160 (1 << src->Register.SwizzleW);
161
162 info->colors_read |= mask << (index * 4);
163 }
164 }
165 }
166
167 /* check for indirect register reads */
168 if (src->Register.Indirect) {
169 info->indirect_files |= (1 << src->Register.File);
170 info->indirect_files_read |= (1 << src->Register.File);
171 }
172
173 /* MSAA samplers */
174 if (src->Register.File == TGSI_FILE_SAMPLER) {
175 assert(fullinst->Instruction.Texture);
176 assert(src->Register.Index < Elements(info->is_msaa_sampler));
177
178 if (fullinst->Instruction.Texture &&
179 (fullinst->Texture.Texture == TGSI_TEXTURE_2D_MSAA ||
180 fullinst->Texture.Texture == TGSI_TEXTURE_2D_ARRAY_MSAA)) {
181 info->is_msaa_sampler[src->Register.Index] = TRUE;
182 }
183 }
184 }
185
186 /* check for indirect register writes */
187 for (i = 0; i < fullinst->Instruction.NumDstRegs; i++) {
188 const struct tgsi_full_dst_register *dst = &fullinst->Dst[i];
189 if (dst->Register.Indirect) {
190 info->indirect_files |= (1 << dst->Register.File);
191 info->indirect_files_written |= (1 << dst->Register.File);
192 }
193 }
194
195 info->num_instructions++;
196 }
197
198
199 static void
200 scan_declaration(struct tgsi_shader_info *info,
201 const struct tgsi_full_declaration *fulldecl)
202 {
203 const uint file = fulldecl->Declaration.File;
204 const unsigned procType = info->processor;
205 uint reg;
206
207 if (fulldecl->Declaration.Array) {
208 unsigned array_id = fulldecl->Array.ArrayID;
209
210 switch (file) {
211 case TGSI_FILE_INPUT:
212 assert(array_id < ARRAY_SIZE(info->input_array_first));
213 info->input_array_first[array_id] = fulldecl->Range.First;
214 info->input_array_last[array_id] = fulldecl->Range.Last;
215 break;
216 case TGSI_FILE_OUTPUT:
217 assert(array_id < ARRAY_SIZE(info->output_array_first));
218 info->output_array_first[array_id] = fulldecl->Range.First;
219 info->output_array_last[array_id] = fulldecl->Range.Last;
220 break;
221 }
222 info->array_max[file] = MAX2(info->array_max[file], array_id);
223 }
224
225 for (reg = fulldecl->Range.First; reg <= fulldecl->Range.Last; reg++) {
226 unsigned semName = fulldecl->Semantic.Name;
227 unsigned semIndex = fulldecl->Semantic.Index +
228 (reg - fulldecl->Range.First);
229
230 /* only first 32 regs will appear in this bitfield */
231 info->file_mask[file] |= (1 << reg);
232 info->file_count[file]++;
233 info->file_max[file] = MAX2(info->file_max[file], (int)reg);
234
235 if (file == TGSI_FILE_CONSTANT) {
236 int buffer = 0;
237
238 if (fulldecl->Declaration.Dimension)
239 buffer = fulldecl->Dim.Index2D;
240
241 info->const_file_max[buffer] =
242 MAX2(info->const_file_max[buffer], (int)reg);
243 }
244 else if (file == TGSI_FILE_INPUT) {
245 info->input_semantic_name[reg] = (ubyte) semName;
246 info->input_semantic_index[reg] = (ubyte) semIndex;
247 info->input_interpolate[reg] = (ubyte)fulldecl->Interp.Interpolate;
248 info->input_interpolate_loc[reg] = (ubyte)fulldecl->Interp.Location;
249 info->input_cylindrical_wrap[reg] = (ubyte)fulldecl->Interp.CylindricalWrap;
250
251 /* Vertex shaders can have inputs with holes between them. */
252 if (info->processor == TGSI_PROCESSOR_VERTEX)
253 info->num_inputs = MAX2(info->num_inputs, reg + 1);
254 else {
255 info->num_inputs++;
256 assert(reg < info->num_inputs);
257 }
258
259 /* Only interpolated varyings. Don't include POSITION.
260 * Don't include integer varyings, because they are not
261 * interpolated.
262 */
263 if (semName == TGSI_SEMANTIC_GENERIC ||
264 semName == TGSI_SEMANTIC_TEXCOORD ||
265 semName == TGSI_SEMANTIC_COLOR ||
266 semName == TGSI_SEMANTIC_BCOLOR ||
267 semName == TGSI_SEMANTIC_FOG ||
268 semName == TGSI_SEMANTIC_CLIPDIST ||
269 semName == TGSI_SEMANTIC_CULLDIST) {
270 switch (fulldecl->Interp.Interpolate) {
271 case TGSI_INTERPOLATE_COLOR:
272 case TGSI_INTERPOLATE_PERSPECTIVE:
273 switch (fulldecl->Interp.Location) {
274 case TGSI_INTERPOLATE_LOC_CENTER:
275 info->uses_persp_center = TRUE;
276 break;
277 case TGSI_INTERPOLATE_LOC_CENTROID:
278 info->uses_persp_centroid = TRUE;
279 break;
280 case TGSI_INTERPOLATE_LOC_SAMPLE:
281 info->uses_persp_sample = TRUE;
282 break;
283 }
284 break;
285 case TGSI_INTERPOLATE_LINEAR:
286 switch (fulldecl->Interp.Location) {
287 case TGSI_INTERPOLATE_LOC_CENTER:
288 info->uses_linear_center = TRUE;
289 break;
290 case TGSI_INTERPOLATE_LOC_CENTROID:
291 info->uses_linear_centroid = TRUE;
292 break;
293 case TGSI_INTERPOLATE_LOC_SAMPLE:
294 info->uses_linear_sample = TRUE;
295 break;
296 }
297 break;
298 /* TGSI_INTERPOLATE_CONSTANT doesn't do any interpolation. */
299 }
300 }
301
302 if (semName == TGSI_SEMANTIC_PRIMID)
303 info->uses_primid = TRUE;
304 else if (procType == TGSI_PROCESSOR_FRAGMENT) {
305 if (semName == TGSI_SEMANTIC_POSITION)
306 info->reads_position = TRUE;
307 else if (semName == TGSI_SEMANTIC_FACE)
308 info->uses_frontface = TRUE;
309 }
310 }
311 else if (file == TGSI_FILE_SYSTEM_VALUE) {
312 unsigned index = fulldecl->Range.First;
313
314 info->system_value_semantic_name[index] = semName;
315 info->num_system_values = MAX2(info->num_system_values, index + 1);
316
317 switch (semName) {
318 case TGSI_SEMANTIC_INSTANCEID:
319 info->uses_instanceid = TRUE;
320 break;
321 case TGSI_SEMANTIC_VERTEXID:
322 info->uses_vertexid = TRUE;
323 break;
324 case TGSI_SEMANTIC_VERTEXID_NOBASE:
325 info->uses_vertexid_nobase = TRUE;
326 break;
327 case TGSI_SEMANTIC_BASEVERTEX:
328 info->uses_basevertex = TRUE;
329 break;
330 case TGSI_SEMANTIC_PRIMID:
331 info->uses_primid = TRUE;
332 break;
333 case TGSI_SEMANTIC_INVOCATIONID:
334 info->uses_invocationid = TRUE;
335 break;
336 case TGSI_SEMANTIC_POSITION:
337 info->reads_position = TRUE;
338 break;
339 case TGSI_SEMANTIC_FACE:
340 info->uses_frontface = TRUE;
341 break;
342 case TGSI_SEMANTIC_SAMPLEMASK:
343 info->reads_samplemask = TRUE;
344 break;
345 }
346 }
347 else if (file == TGSI_FILE_OUTPUT) {
348 info->output_semantic_name[reg] = (ubyte) semName;
349 info->output_semantic_index[reg] = (ubyte) semIndex;
350 info->num_outputs++;
351 assert(reg < info->num_outputs);
352
353 if (semName == TGSI_SEMANTIC_COLOR)
354 info->colors_written |= 1 << semIndex;
355
356 if (procType == TGSI_PROCESSOR_VERTEX ||
357 procType == TGSI_PROCESSOR_GEOMETRY ||
358 procType == TGSI_PROCESSOR_TESS_CTRL ||
359 procType == TGSI_PROCESSOR_TESS_EVAL) {
360 switch (semName) {
361 case TGSI_SEMANTIC_VIEWPORT_INDEX:
362 info->writes_viewport_index = TRUE;
363 break;
364 case TGSI_SEMANTIC_LAYER:
365 info->writes_layer = TRUE;
366 break;
367 case TGSI_SEMANTIC_PSIZE:
368 info->writes_psize = TRUE;
369 break;
370 case TGSI_SEMANTIC_CLIPVERTEX:
371 info->writes_clipvertex = TRUE;
372 break;
373 }
374 }
375
376 if (procType == TGSI_PROCESSOR_FRAGMENT) {
377 switch (semName) {
378 case TGSI_SEMANTIC_POSITION:
379 info->writes_z = TRUE;
380 break;
381 case TGSI_SEMANTIC_STENCIL:
382 info->writes_stencil = TRUE;
383 break;
384 case TGSI_SEMANTIC_SAMPLEMASK:
385 info->writes_samplemask = TRUE;
386 break;
387 }
388 }
389
390 if (procType == TGSI_PROCESSOR_VERTEX) {
391 if (semName == TGSI_SEMANTIC_EDGEFLAG) {
392 info->writes_edgeflag = TRUE;
393 }
394 }
395 } else if (file == TGSI_FILE_SAMPLER) {
396 info->samplers_declared |= 1 << reg;
397 }
398 }
399 }
400
401
402 static void
403 scan_immediate(struct tgsi_shader_info *info)
404 {
405 uint reg = info->immediate_count++;
406 uint file = TGSI_FILE_IMMEDIATE;
407
408 info->file_mask[file] |= (1 << reg);
409 info->file_count[file]++;
410 info->file_max[file] = MAX2(info->file_max[file], (int)reg);
411 }
412
413
414 static void
415 scan_property(struct tgsi_shader_info *info,
416 const struct tgsi_full_property *fullprop)
417 {
418 unsigned name = fullprop->Property.PropertyName;
419 unsigned value = fullprop->u[0].Data;
420
421 assert(name < Elements(info->properties));
422 info->properties[name] = value;
423
424 switch (name) {
425 case TGSI_PROPERTY_NUM_CLIPDIST_ENABLED:
426 info->num_written_clipdistance = value;
427 info->clipdist_writemask |= (1 << value) - 1;
428 break;
429 case TGSI_PROPERTY_NUM_CULLDIST_ENABLED:
430 info->num_written_culldistance = value;
431 info->culldist_writemask |= (1 << value) - 1;
432 break;
433 }
434 }
435
436
437 /**
438 * Scan the given TGSI shader to collect information such as number of
439 * registers used, special instructions used, etc.
440 * \return info the result of the scan
441 */
442 void
443 tgsi_scan_shader(const struct tgsi_token *tokens,
444 struct tgsi_shader_info *info)
445 {
446 uint procType, i;
447 struct tgsi_parse_context parse;
448 unsigned current_depth = 0;
449
450 memset(info, 0, sizeof(*info));
451 for (i = 0; i < TGSI_FILE_COUNT; i++)
452 info->file_max[i] = -1;
453 for (i = 0; i < Elements(info->const_file_max); i++)
454 info->const_file_max[i] = -1;
455 info->properties[TGSI_PROPERTY_GS_INVOCATIONS] = 1;
456
457 /**
458 ** Setup to begin parsing input shader
459 **/
460 if (tgsi_parse_init( &parse, tokens ) != TGSI_PARSE_OK) {
461 debug_printf("tgsi_parse_init() failed in tgsi_scan_shader()!\n");
462 return;
463 }
464 procType = parse.FullHeader.Processor.Processor;
465 assert(procType == TGSI_PROCESSOR_FRAGMENT ||
466 procType == TGSI_PROCESSOR_VERTEX ||
467 procType == TGSI_PROCESSOR_GEOMETRY ||
468 procType == TGSI_PROCESSOR_TESS_CTRL ||
469 procType == TGSI_PROCESSOR_TESS_EVAL ||
470 procType == TGSI_PROCESSOR_COMPUTE);
471 info->processor = procType;
472
473 /**
474 ** Loop over incoming program tokens/instructions
475 */
476 while (!tgsi_parse_end_of_tokens(&parse)) {
477 info->num_tokens++;
478
479 tgsi_parse_token( &parse );
480
481 switch( parse.FullToken.Token.Type ) {
482 case TGSI_TOKEN_TYPE_INSTRUCTION:
483 scan_instruction(info, &parse.FullToken.FullInstruction,
484 &current_depth);
485 break;
486 case TGSI_TOKEN_TYPE_DECLARATION:
487 scan_declaration(info, &parse.FullToken.FullDeclaration);
488 break;
489 case TGSI_TOKEN_TYPE_IMMEDIATE:
490 scan_immediate(info);
491 break;
492 case TGSI_TOKEN_TYPE_PROPERTY:
493 scan_property(info, &parse.FullToken.FullProperty);
494 break;
495 default:
496 assert(!"Unexpected TGSI token type");
497 }
498 }
499
500 info->uses_kill = (info->opcode_count[TGSI_OPCODE_KILL_IF] ||
501 info->opcode_count[TGSI_OPCODE_KILL]);
502
503 /* The dimensions of the IN decleration in geometry shader have
504 * to be deduced from the type of the input primitive.
505 */
506 if (procType == TGSI_PROCESSOR_GEOMETRY) {
507 unsigned input_primitive =
508 info->properties[TGSI_PROPERTY_GS_INPUT_PRIM];
509 int num_verts = u_vertices_per_prim(input_primitive);
510 int j;
511 info->file_count[TGSI_FILE_INPUT] = num_verts;
512 info->file_max[TGSI_FILE_INPUT] =
513 MAX2(info->file_max[TGSI_FILE_INPUT], num_verts - 1);
514 for (j = 0; j < num_verts; ++j) {
515 info->file_mask[TGSI_FILE_INPUT] |= (1 << j);
516 }
517 }
518
519 tgsi_parse_free(&parse);
520 }
521
522
523
524 /**
525 * Check if the given shader is a "passthrough" shader consisting of only
526 * MOV instructions of the form: MOV OUT[n], IN[n]
527 *
528 */
529 boolean
530 tgsi_is_passthrough_shader(const struct tgsi_token *tokens)
531 {
532 struct tgsi_parse_context parse;
533
534 /**
535 ** Setup to begin parsing input shader
536 **/
537 if (tgsi_parse_init(&parse, tokens) != TGSI_PARSE_OK) {
538 debug_printf("tgsi_parse_init() failed in tgsi_is_passthrough_shader()!\n");
539 return FALSE;
540 }
541
542 /**
543 ** Loop over incoming program tokens/instructions
544 */
545 while (!tgsi_parse_end_of_tokens(&parse)) {
546
547 tgsi_parse_token(&parse);
548
549 switch (parse.FullToken.Token.Type) {
550 case TGSI_TOKEN_TYPE_INSTRUCTION:
551 {
552 struct tgsi_full_instruction *fullinst =
553 &parse.FullToken.FullInstruction;
554 const struct tgsi_full_src_register *src =
555 &fullinst->Src[0];
556 const struct tgsi_full_dst_register *dst =
557 &fullinst->Dst[0];
558
559 /* Do a whole bunch of checks for a simple move */
560 if (fullinst->Instruction.Opcode != TGSI_OPCODE_MOV ||
561 (src->Register.File != TGSI_FILE_INPUT &&
562 src->Register.File != TGSI_FILE_SYSTEM_VALUE) ||
563 dst->Register.File != TGSI_FILE_OUTPUT ||
564 src->Register.Index != dst->Register.Index ||
565
566 src->Register.Negate ||
567 src->Register.Absolute ||
568
569 src->Register.SwizzleX != TGSI_SWIZZLE_X ||
570 src->Register.SwizzleY != TGSI_SWIZZLE_Y ||
571 src->Register.SwizzleZ != TGSI_SWIZZLE_Z ||
572 src->Register.SwizzleW != TGSI_SWIZZLE_W ||
573
574 dst->Register.WriteMask != TGSI_WRITEMASK_XYZW)
575 {
576 tgsi_parse_free(&parse);
577 return FALSE;
578 }
579 }
580 break;
581
582 case TGSI_TOKEN_TYPE_DECLARATION:
583 /* fall-through */
584 case TGSI_TOKEN_TYPE_IMMEDIATE:
585 /* fall-through */
586 case TGSI_TOKEN_TYPE_PROPERTY:
587 /* fall-through */
588 default:
589 ; /* no-op */
590 }
591 }
592
593 tgsi_parse_free(&parse);
594
595 /* if we get here, it's a pass-through shader */
596 return TRUE;
597 }