tgsi: s/true/TRUE/ in tgsi_scan.c
[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 info->num_inputs++;
251
252 /* Only interpolated varyings. Don't include POSITION.
253 * Don't include integer varyings, because they are not
254 * interpolated.
255 */
256 if (semName == TGSI_SEMANTIC_GENERIC ||
257 semName == TGSI_SEMANTIC_TEXCOORD ||
258 semName == TGSI_SEMANTIC_COLOR ||
259 semName == TGSI_SEMANTIC_BCOLOR ||
260 semName == TGSI_SEMANTIC_FOG ||
261 semName == TGSI_SEMANTIC_CLIPDIST ||
262 semName == TGSI_SEMANTIC_CULLDIST) {
263 switch (fulldecl->Interp.Interpolate) {
264 case TGSI_INTERPOLATE_COLOR:
265 case TGSI_INTERPOLATE_PERSPECTIVE:
266 switch (fulldecl->Interp.Location) {
267 case TGSI_INTERPOLATE_LOC_CENTER:
268 info->uses_persp_center = TRUE;
269 break;
270 case TGSI_INTERPOLATE_LOC_CENTROID:
271 info->uses_persp_centroid = TRUE;
272 break;
273 case TGSI_INTERPOLATE_LOC_SAMPLE:
274 info->uses_persp_sample = TRUE;
275 break;
276 }
277 break;
278 case TGSI_INTERPOLATE_LINEAR:
279 switch (fulldecl->Interp.Location) {
280 case TGSI_INTERPOLATE_LOC_CENTER:
281 info->uses_linear_center = TRUE;
282 break;
283 case TGSI_INTERPOLATE_LOC_CENTROID:
284 info->uses_linear_centroid = TRUE;
285 break;
286 case TGSI_INTERPOLATE_LOC_SAMPLE:
287 info->uses_linear_sample = TRUE;
288 break;
289 }
290 break;
291 /* TGSI_INTERPOLATE_CONSTANT doesn't do any interpolation. */
292 }
293 }
294
295 if (semName == TGSI_SEMANTIC_PRIMID)
296 info->uses_primid = TRUE;
297 else if (procType == TGSI_PROCESSOR_FRAGMENT) {
298 if (semName == TGSI_SEMANTIC_POSITION)
299 info->reads_position = TRUE;
300 else if (semName == TGSI_SEMANTIC_FACE)
301 info->uses_frontface = TRUE;
302 }
303 }
304 else if (file == TGSI_FILE_SYSTEM_VALUE) {
305 unsigned index = fulldecl->Range.First;
306
307 info->system_value_semantic_name[index] = semName;
308 info->num_system_values = MAX2(info->num_system_values, index + 1);
309
310 switch (semName) {
311 case TGSI_SEMANTIC_INSTANCEID:
312 info->uses_instanceid = TRUE;
313 break;
314 case TGSI_SEMANTIC_VERTEXID:
315 info->uses_vertexid = TRUE;
316 break;
317 case TGSI_SEMANTIC_VERTEXID_NOBASE:
318 info->uses_vertexid_nobase = TRUE;
319 break;
320 case TGSI_SEMANTIC_BASEVERTEX:
321 info->uses_basevertex = TRUE;
322 break;
323 case TGSI_SEMANTIC_PRIMID:
324 info->uses_primid = TRUE;
325 break;
326 case TGSI_SEMANTIC_INVOCATIONID:
327 info->uses_invocationid = TRUE;
328 break;
329 case TGSI_SEMANTIC_POSITION:
330 info->reads_position = TRUE;
331 break;
332 case TGSI_SEMANTIC_FACE:
333 info->uses_frontface = TRUE;
334 break;
335 case TGSI_SEMANTIC_SAMPLEMASK:
336 info->reads_samplemask = TRUE;
337 break;
338 }
339 }
340 else if (file == TGSI_FILE_OUTPUT) {
341 info->output_semantic_name[reg] = (ubyte) semName;
342 info->output_semantic_index[reg] = (ubyte) semIndex;
343 info->num_outputs++;
344
345 if (semName == TGSI_SEMANTIC_COLOR)
346 info->colors_written |= 1 << semIndex;
347
348 if (procType == TGSI_PROCESSOR_VERTEX ||
349 procType == TGSI_PROCESSOR_GEOMETRY ||
350 procType == TGSI_PROCESSOR_TESS_CTRL ||
351 procType == TGSI_PROCESSOR_TESS_EVAL) {
352 switch (semName) {
353 case TGSI_SEMANTIC_VIEWPORT_INDEX:
354 info->writes_viewport_index = TRUE;
355 break;
356 case TGSI_SEMANTIC_LAYER:
357 info->writes_layer = TRUE;
358 break;
359 case TGSI_SEMANTIC_PSIZE:
360 info->writes_psize = TRUE;
361 break;
362 case TGSI_SEMANTIC_CLIPVERTEX:
363 info->writes_clipvertex = TRUE;
364 break;
365 }
366 }
367
368 if (procType == TGSI_PROCESSOR_FRAGMENT) {
369 switch (semName) {
370 case TGSI_SEMANTIC_POSITION:
371 info->writes_z = TRUE;
372 break;
373 case TGSI_SEMANTIC_STENCIL:
374 info->writes_stencil = TRUE;
375 break;
376 case TGSI_SEMANTIC_SAMPLEMASK:
377 info->writes_samplemask = TRUE;
378 break;
379 }
380 }
381
382 if (procType == TGSI_PROCESSOR_VERTEX) {
383 if (semName == TGSI_SEMANTIC_EDGEFLAG) {
384 info->writes_edgeflag = TRUE;
385 }
386 }
387 } else if (file == TGSI_FILE_SAMPLER) {
388 info->samplers_declared |= 1 << reg;
389 }
390 }
391 }
392
393
394 static void
395 scan_immediate(struct tgsi_shader_info *info)
396 {
397 uint reg = info->immediate_count++;
398 uint file = TGSI_FILE_IMMEDIATE;
399
400 info->file_mask[file] |= (1 << reg);
401 info->file_count[file]++;
402 info->file_max[file] = MAX2(info->file_max[file], (int)reg);
403 }
404
405
406 static void
407 scan_property(struct tgsi_shader_info *info,
408 const struct tgsi_full_property *fullprop)
409 {
410 unsigned name = fullprop->Property.PropertyName;
411 unsigned value = fullprop->u[0].Data;
412
413 assert(name < Elements(info->properties));
414 info->properties[name] = value;
415
416 switch (name) {
417 case TGSI_PROPERTY_NUM_CLIPDIST_ENABLED:
418 info->num_written_clipdistance = value;
419 info->clipdist_writemask |= (1 << value) - 1;
420 break;
421 case TGSI_PROPERTY_NUM_CULLDIST_ENABLED:
422 info->num_written_culldistance = value;
423 info->culldist_writemask |= (1 << value) - 1;
424 break;
425 }
426 }
427
428
429 /**
430 * Scan the given TGSI shader to collect information such as number of
431 * registers used, special instructions used, etc.
432 * \return info the result of the scan
433 */
434 void
435 tgsi_scan_shader(const struct tgsi_token *tokens,
436 struct tgsi_shader_info *info)
437 {
438 uint procType, i;
439 struct tgsi_parse_context parse;
440 unsigned current_depth = 0;
441
442 memset(info, 0, sizeof(*info));
443 for (i = 0; i < TGSI_FILE_COUNT; i++)
444 info->file_max[i] = -1;
445 for (i = 0; i < Elements(info->const_file_max); i++)
446 info->const_file_max[i] = -1;
447 info->properties[TGSI_PROPERTY_GS_INVOCATIONS] = 1;
448
449 /**
450 ** Setup to begin parsing input shader
451 **/
452 if (tgsi_parse_init( &parse, tokens ) != TGSI_PARSE_OK) {
453 debug_printf("tgsi_parse_init() failed in tgsi_scan_shader()!\n");
454 return;
455 }
456 procType = parse.FullHeader.Processor.Processor;
457 assert(procType == TGSI_PROCESSOR_FRAGMENT ||
458 procType == TGSI_PROCESSOR_VERTEX ||
459 procType == TGSI_PROCESSOR_GEOMETRY ||
460 procType == TGSI_PROCESSOR_TESS_CTRL ||
461 procType == TGSI_PROCESSOR_TESS_EVAL ||
462 procType == TGSI_PROCESSOR_COMPUTE);
463 info->processor = procType;
464
465
466 /**
467 ** Loop over incoming program tokens/instructions
468 */
469 while( !tgsi_parse_end_of_tokens( &parse ) ) {
470
471 info->num_tokens++;
472
473 tgsi_parse_token( &parse );
474
475 switch( parse.FullToken.Token.Type ) {
476 case TGSI_TOKEN_TYPE_INSTRUCTION:
477 scan_instruction(info, &parse.FullToken.FullInstruction,
478 &current_depth);
479 break;
480 case TGSI_TOKEN_TYPE_DECLARATION:
481 scan_declaration(info, &parse.FullToken.FullDeclaration);
482 break;
483 case TGSI_TOKEN_TYPE_IMMEDIATE:
484 scan_immediate(info);
485 break;
486 case TGSI_TOKEN_TYPE_PROPERTY:
487 scan_property(info, &parse.FullToken.FullProperty);
488 break;
489 default:
490 assert(!"Unexpected TGSI token type");
491 }
492 }
493
494 info->uses_kill = (info->opcode_count[TGSI_OPCODE_KILL_IF] ||
495 info->opcode_count[TGSI_OPCODE_KILL]);
496
497 /* The dimensions of the IN decleration in geometry shader have
498 * to be deduced from the type of the input primitive.
499 */
500 if (procType == TGSI_PROCESSOR_GEOMETRY) {
501 unsigned input_primitive =
502 info->properties[TGSI_PROPERTY_GS_INPUT_PRIM];
503 int num_verts = u_vertices_per_prim(input_primitive);
504 int j;
505 info->file_count[TGSI_FILE_INPUT] = num_verts;
506 info->file_max[TGSI_FILE_INPUT] =
507 MAX2(info->file_max[TGSI_FILE_INPUT], num_verts - 1);
508 for (j = 0; j < num_verts; ++j) {
509 info->file_mask[TGSI_FILE_INPUT] |= (1 << j);
510 }
511 }
512
513 tgsi_parse_free (&parse);
514 }
515
516
517
518 /**
519 * Check if the given shader is a "passthrough" shader consisting of only
520 * MOV instructions of the form: MOV OUT[n], IN[n]
521 *
522 */
523 boolean
524 tgsi_is_passthrough_shader(const struct tgsi_token *tokens)
525 {
526 struct tgsi_parse_context parse;
527
528 /**
529 ** Setup to begin parsing input shader
530 **/
531 if (tgsi_parse_init(&parse, tokens) != TGSI_PARSE_OK) {
532 debug_printf("tgsi_parse_init() failed in tgsi_is_passthrough_shader()!\n");
533 return FALSE;
534 }
535
536 /**
537 ** Loop over incoming program tokens/instructions
538 */
539 while (!tgsi_parse_end_of_tokens(&parse)) {
540
541 tgsi_parse_token(&parse);
542
543 switch (parse.FullToken.Token.Type) {
544 case TGSI_TOKEN_TYPE_INSTRUCTION:
545 {
546 struct tgsi_full_instruction *fullinst =
547 &parse.FullToken.FullInstruction;
548 const struct tgsi_full_src_register *src =
549 &fullinst->Src[0];
550 const struct tgsi_full_dst_register *dst =
551 &fullinst->Dst[0];
552
553 /* Do a whole bunch of checks for a simple move */
554 if (fullinst->Instruction.Opcode != TGSI_OPCODE_MOV ||
555 (src->Register.File != TGSI_FILE_INPUT &&
556 src->Register.File != TGSI_FILE_SYSTEM_VALUE) ||
557 dst->Register.File != TGSI_FILE_OUTPUT ||
558 src->Register.Index != dst->Register.Index ||
559
560 src->Register.Negate ||
561 src->Register.Absolute ||
562
563 src->Register.SwizzleX != TGSI_SWIZZLE_X ||
564 src->Register.SwizzleY != TGSI_SWIZZLE_Y ||
565 src->Register.SwizzleZ != TGSI_SWIZZLE_Z ||
566 src->Register.SwizzleW != TGSI_SWIZZLE_W ||
567
568 dst->Register.WriteMask != TGSI_WRITEMASK_XYZW)
569 {
570 tgsi_parse_free(&parse);
571 return FALSE;
572 }
573 }
574 break;
575
576 case TGSI_TOKEN_TYPE_DECLARATION:
577 /* fall-through */
578 case TGSI_TOKEN_TYPE_IMMEDIATE:
579 /* fall-through */
580 case TGSI_TOKEN_TYPE_PROPERTY:
581 /* fall-through */
582 default:
583 ; /* no-op */
584 }
585 }
586
587 tgsi_parse_free(&parse);
588
589 /* if we get here, it's a pass-through shader */
590 return TRUE;
591 }